2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Steve Reinhardt
32 #ifndef __SIM_SYSCALL_EMUL_HH__
33 #define __SIM_SYSCALL_EMUL_HH__
35 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
36 defined(__FreeBSD__) || defined(__CYGWIN__))
39 /// @file syscall_emul.hh
41 /// This file defines objects used to emulate syscalls from the target
42 /// application on the host machine.
45 #include <sys/fcntl.h> // for O_BINARY
55 #include "base/chunk_generator.hh"
56 #include "base/intmath.hh" // for RoundUp
57 #include "base/misc.hh"
58 #include "base/trace.hh"
59 #include "base/types.hh"
60 #include "config/the_isa.hh"
61 #include "cpu/base.hh"
62 #include "cpu/thread_context.hh"
63 #include "debug/SyscallVerbose.hh"
64 #include "mem/page_table.hh"
65 #include "mem/translating_port.hh"
66 #include "sim/byteswap.hh"
67 #include "sim/process.hh"
68 #include "sim/system.hh"
71 /// System call descriptor.
77 /// Typedef for target syscall handler functions.
78 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
79 LiveProcess *, ThreadContext *);
81 const char *name; //!< Syscall name (e.g., "open").
82 FuncPtr funcPtr; //!< Pointer to emulation function.
83 int flags; //!< Flags (see Flags enum).
85 /// Flag values for controlling syscall behavior.
87 /// Don't set return regs according to funcPtr return value.
88 /// Used for syscalls with non-standard return conventions
89 /// that explicitly set the ThreadContext regs (e.g.,
91 SuppressReturnValue = 1
95 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
96 : name(_name), funcPtr(_funcPtr), flags(_flags)
100 /// Emulate the syscall. Public interface for calling through funcPtr.
101 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
105 class BaseBufferArg {
109 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
111 bufPtr = new uint8_t[size];
112 // clear out buffer: in case we only partially populate this,
113 // and then do a copyOut(), we want to make sure we don't
114 // introduce any random junk into the simulated address space
115 memset(bufPtr, 0, size);
118 virtual ~BaseBufferArg() { delete [] bufPtr; }
121 // copy data into simulator space (read from target memory)
123 virtual bool copyIn(TranslatingPort *memport)
125 memport->readBlob(addr, bufPtr, size);
126 return true; // no EFAULT detection for now
130 // copy data out of simulator space (write to target memory)
132 virtual bool copyOut(TranslatingPort *memport)
134 memport->writeBlob(addr, bufPtr, size);
135 return true; // no EFAULT detection for now
145 class BufferArg : public BaseBufferArg
148 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
149 void *bufferPtr() { return bufPtr; }
153 class TypedBufferArg : public BaseBufferArg
156 // user can optionally specify a specific number of bytes to
157 // allocate to deal with those structs that have variable-size
159 TypedBufferArg(Addr _addr, int _size = sizeof(T))
160 : BaseBufferArg(_addr, _size)
164 operator T*() { return (T *)bufPtr; }
166 // dereference operators
167 T &operator*() { return *((T *)bufPtr); }
168 T* operator->() { return (T *)bufPtr; }
169 T &operator[](int i) { return ((T *)bufPtr)[i]; }
172 //////////////////////////////////////////////////////////////////////
174 // The following emulation functions are generic enough that they
175 // don't need to be recompiled for different emulated OS's. They are
176 // defined in sim/syscall_emul.cc.
178 //////////////////////////////////////////////////////////////////////
181 /// Handler for unimplemented syscalls that we haven't thought about.
182 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
183 LiveProcess *p, ThreadContext *tc);
185 /// Handler for unimplemented syscalls that we never intend to
186 /// implement (signal handling, etc.) and should not affect the correct
187 /// behavior of the program. Print a warning only if the appropriate
188 /// trace flag is enabled. Return success to the target program.
189 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
190 LiveProcess *p, ThreadContext *tc);
191 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
192 LiveProcess *p, ThreadContext *tc);
194 /// Target exit() handler: terminate current context.
195 SyscallReturn exitFunc(SyscallDesc *desc, int num,
196 LiveProcess *p, ThreadContext *tc);
198 /// Target exit_group() handler: terminate simulation. (exit all threads)
199 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
200 LiveProcess *p, ThreadContext *tc);
202 /// Target getpagesize() handler.
203 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
204 LiveProcess *p, ThreadContext *tc);
206 /// Target brk() handler: set brk address.
207 SyscallReturn brkFunc(SyscallDesc *desc, int num,
208 LiveProcess *p, ThreadContext *tc);
210 /// Target close() handler.
211 SyscallReturn closeFunc(SyscallDesc *desc, int num,
212 LiveProcess *p, ThreadContext *tc);
214 /// Target read() handler.
215 SyscallReturn readFunc(SyscallDesc *desc, int num,
216 LiveProcess *p, ThreadContext *tc);
218 /// Target write() handler.
219 SyscallReturn writeFunc(SyscallDesc *desc, int num,
220 LiveProcess *p, ThreadContext *tc);
222 /// Target lseek() handler.
223 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
224 LiveProcess *p, ThreadContext *tc);
226 /// Target _llseek() handler.
227 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
228 LiveProcess *p, ThreadContext *tc);
230 /// Target munmap() handler.
231 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
232 LiveProcess *p, ThreadContext *tc);
234 /// Target gethostname() handler.
235 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
236 LiveProcess *p, ThreadContext *tc);
238 /// Target getcwd() handler.
239 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
240 LiveProcess *p, ThreadContext *tc);
242 /// Target unlink() handler.
243 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
244 LiveProcess *p, ThreadContext *tc);
246 /// Target unlink() handler.
247 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
248 LiveProcess *p, ThreadContext *tc);
250 /// Target mkdir() handler.
251 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
252 LiveProcess *p, ThreadContext *tc);
254 /// Target rename() handler.
255 SyscallReturn renameFunc(SyscallDesc *desc, int num,
256 LiveProcess *p, ThreadContext *tc);
259 /// Target truncate() handler.
260 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
261 LiveProcess *p, ThreadContext *tc);
264 /// Target ftruncate() handler.
265 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
266 LiveProcess *p, ThreadContext *tc);
269 /// Target truncate64() handler.
270 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
271 LiveProcess *p, ThreadContext *tc);
273 /// Target ftruncate64() handler.
274 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
275 LiveProcess *p, ThreadContext *tc);
278 /// Target umask() handler.
279 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
280 LiveProcess *p, ThreadContext *tc);
283 /// Target chown() handler.
284 SyscallReturn chownFunc(SyscallDesc *desc, int num,
285 LiveProcess *p, ThreadContext *tc);
288 /// Target fchown() handler.
289 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
290 LiveProcess *p, ThreadContext *tc);
292 /// Target dup() handler.
293 SyscallReturn dupFunc(SyscallDesc *desc, int num,
294 LiveProcess *process, ThreadContext *tc);
296 /// Target fnctl() handler.
297 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
298 LiveProcess *process, ThreadContext *tc);
300 /// Target fcntl64() handler.
301 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
302 LiveProcess *process, ThreadContext *tc);
304 /// Target setuid() handler.
305 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
306 LiveProcess *p, ThreadContext *tc);
308 /// Target getpid() handler.
309 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
310 LiveProcess *p, ThreadContext *tc);
312 /// Target getuid() handler.
313 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
314 LiveProcess *p, ThreadContext *tc);
316 /// Target getgid() handler.
317 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
318 LiveProcess *p, ThreadContext *tc);
320 /// Target getppid() handler.
321 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
322 LiveProcess *p, ThreadContext *tc);
324 /// Target geteuid() handler.
325 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
326 LiveProcess *p, ThreadContext *tc);
328 /// Target getegid() handler.
329 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
330 LiveProcess *p, ThreadContext *tc);
332 /// Target clone() handler.
333 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
334 LiveProcess *p, ThreadContext *tc);
337 /// Pseudo Funcs - These functions use a different return convension,
338 /// returning a second value in a register other than the normal return register
339 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
340 LiveProcess *process, ThreadContext *tc);
342 /// Target getpidPseudo() handler.
343 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
344 LiveProcess *p, ThreadContext *tc);
346 /// Target getuidPseudo() handler.
347 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
348 LiveProcess *p, ThreadContext *tc);
350 /// Target getgidPseudo() handler.
351 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
352 LiveProcess *p, ThreadContext *tc);
355 /// A readable name for 1,000,000, for converting microseconds to seconds.
356 const int one_million = 1000000;
358 /// Approximate seconds since the epoch (1/1/1970). About a billion,
359 /// by my reckoning. We want to keep this a constant (not use the
360 /// real-world time) to keep simulations repeatable.
361 const unsigned seconds_since_epoch = 1000000000;
363 /// Helper function to convert current elapsed time to seconds and
365 template <class T1, class T2>
367 getElapsedTime(T1 &sec, T2 &usec)
369 int elapsed_usecs = curTick() / SimClock::Int::us;
370 sec = elapsed_usecs / one_million;
371 usec = elapsed_usecs % one_million;
374 //////////////////////////////////////////////////////////////////////
376 // The following emulation functions are generic, but need to be
377 // templated to account for differences in types, constants, etc.
379 //////////////////////////////////////////////////////////////////////
382 typedef struct stat hst_stat;
383 typedef struct stat hst_stat64;
385 typedef struct stat hst_stat;
386 typedef struct stat64 hst_stat64;
389 //// Helper function to convert a host stat buffer to a target stat
390 //// buffer. Also copies the target buffer out to the simulated
391 //// memory space. Used by stat(), fstat(), and lstat().
393 template <typename target_stat, typename host_stat>
395 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
397 using namespace TheISA;
402 tgt->st_dev = host->st_dev;
403 tgt->st_dev = htog(tgt->st_dev);
404 tgt->st_ino = host->st_ino;
405 tgt->st_ino = htog(tgt->st_ino);
406 tgt->st_mode = host->st_mode;
408 // Claim to be a character device
409 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
410 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
412 tgt->st_mode = htog(tgt->st_mode);
413 tgt->st_nlink = host->st_nlink;
414 tgt->st_nlink = htog(tgt->st_nlink);
415 tgt->st_uid = host->st_uid;
416 tgt->st_uid = htog(tgt->st_uid);
417 tgt->st_gid = host->st_gid;
418 tgt->st_gid = htog(tgt->st_gid);
420 tgt->st_rdev = 0x880d;
422 tgt->st_rdev = host->st_rdev;
423 tgt->st_rdev = htog(tgt->st_rdev);
424 tgt->st_size = host->st_size;
425 tgt->st_size = htog(tgt->st_size);
426 tgt->st_atimeX = host->st_atime;
427 tgt->st_atimeX = htog(tgt->st_atimeX);
428 tgt->st_mtimeX = host->st_mtime;
429 tgt->st_mtimeX = htog(tgt->st_mtimeX);
430 tgt->st_ctimeX = host->st_ctime;
431 tgt->st_ctimeX = htog(tgt->st_ctimeX);
432 // Force the block size to be 8k. This helps to ensure buffered io works
433 // consistently across different hosts.
434 tgt->st_blksize = 0x2000;
435 tgt->st_blksize = htog(tgt->st_blksize);
436 tgt->st_blocks = host->st_blocks;
437 tgt->st_blocks = htog(tgt->st_blocks);
442 template <typename target_stat, typename host_stat64>
444 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
446 using namespace TheISA;
448 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
449 #if defined(STAT_HAVE_NSEC)
450 tgt->st_atime_nsec = host->st_atime_nsec;
451 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
452 tgt->st_mtime_nsec = host->st_mtime_nsec;
453 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
454 tgt->st_ctime_nsec = host->st_ctime_nsec;
455 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
457 tgt->st_atime_nsec = 0;
458 tgt->st_mtime_nsec = 0;
459 tgt->st_ctime_nsec = 0;
463 //Here are a couple convenience functions
466 copyOutStatBuf(TranslatingPort * mem, Addr addr,
467 hst_stat *host, bool fakeTTY = false)
469 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
470 tgt_stat_buf tgt(addr);
471 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
477 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
478 hst_stat64 *host, bool fakeTTY = false)
480 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
481 tgt_stat_buf tgt(addr);
482 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
486 /// Target ioctl() handler. For the most part, programs call ioctl()
487 /// only to find out if their stdout is a tty, to determine whether to
488 /// do line or block buffering.
491 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
495 int fd = process->getSyscallArg(tc, index);
496 unsigned req = process->getSyscallArg(tc, index);
498 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
500 if (fd < 0 || process->sim_fd(fd) < 0) {
501 // doesn't map to any simulator fd: not a valid target fd
506 case OS::TIOCISATTY_:
518 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
519 fd, req, tc->pcState());
523 /// Target open() handler.
526 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
532 if (!tc->getMemPort()->tryReadString(path,
533 process->getSyscallArg(tc, index)))
536 if (path == "/dev/sysdev0") {
537 // This is a memory-mapped high-resolution timer device on Alpha.
538 // We don't support it, so just punt.
539 warn("Ignoring open(%s, ...)\n", path);
543 int tgtFlags = process->getSyscallArg(tc, index);
544 int mode = process->getSyscallArg(tc, index);
547 // translate open flags
548 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
549 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
550 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
551 hostFlags |= OS::openFlagTable[i].hostFlag;
555 // any target flags left?
557 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
560 hostFlags |= O_BINARY;
563 // Adjust path for current working directory
564 path = process->fullPath(path);
566 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
569 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
570 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
571 // It's a proc/sys entery and requires special handling
572 fd = OS::openSpecialFile(path, process, tc);
573 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
576 fd = open(path.c_str(), hostFlags, mode);
577 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
582 /// Target sysinfo() handler.
585 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
590 TypedBufferArg<typename OS::tgt_sysinfo>
591 sysinfo(process->getSyscallArg(tc, index));
593 sysinfo->uptime=seconds_since_epoch;
594 sysinfo->totalram=process->system->memSize();
596 sysinfo.copyOut(tc->getMemPort());
601 /// Target chmod() handler.
604 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
610 if (!tc->getMemPort()->tryReadString(path,
611 process->getSyscallArg(tc, index))) {
615 uint32_t mode = process->getSyscallArg(tc, index);
618 // XXX translate mode flags via OS::something???
621 // Adjust path for current working directory
622 path = process->fullPath(path);
625 int result = chmod(path.c_str(), hostMode);
633 /// Target fchmod() handler.
636 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
640 int fd = process->getSyscallArg(tc, index);
641 if (fd < 0 || process->sim_fd(fd) < 0) {
642 // doesn't map to any simulator fd: not a valid target fd
646 uint32_t mode = process->getSyscallArg(tc, index);
649 // XXX translate mode flags via OS::someting???
653 int result = fchmod(process->sim_fd(fd), hostMode);
660 /// Target mremap() handler.
663 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
666 Addr start = process->getSyscallArg(tc, index);
667 uint64_t old_length = process->getSyscallArg(tc, index);
668 uint64_t new_length = process->getSyscallArg(tc, index);
669 uint64_t flags = process->getSyscallArg(tc, index);
671 if ((start % TheISA::VMPageSize != 0) ||
672 (new_length % TheISA::VMPageSize != 0)) {
673 warn("mremap failing: arguments not page aligned");
677 if (new_length > old_length) {
678 if ((start + old_length) == process->mmap_end) {
679 uint64_t diff = new_length - old_length;
680 process->pTable->allocate(process->mmap_end, diff);
681 process->mmap_end += diff;
684 // sys/mman.h defined MREMAP_MAYMOVE
686 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
689 process->pTable->remap(start, old_length, process->mmap_end);
690 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
691 process->mmap_end, process->mmap_end + new_length, new_length);
692 start = process->mmap_end;
693 // add on the remaining unallocated pages
694 process->pTable->allocate(start + old_length, new_length - old_length);
695 process->mmap_end += new_length;
696 warn("returning %08p as start\n", start);
701 process->pTable->deallocate(start + new_length, old_length -
707 /// Target stat() handler.
710 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
716 if (!tc->getMemPort()->tryReadString(path,
717 process->getSyscallArg(tc, index))) {
720 Addr bufPtr = process->getSyscallArg(tc, index);
722 // Adjust path for current working directory
723 path = process->fullPath(path);
726 int result = stat(path.c_str(), &hostBuf);
731 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
737 /// Target stat64() handler.
740 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
746 if (!tc->getMemPort()->tryReadString(path,
747 process->getSyscallArg(tc, index)))
749 Addr bufPtr = process->getSyscallArg(tc, index);
751 // Adjust path for current working directory
752 path = process->fullPath(path);
756 int result = stat(path.c_str(), &hostBuf);
758 struct stat64 hostBuf;
759 int result = stat64(path.c_str(), &hostBuf);
765 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
771 /// Target fstat64() handler.
774 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
778 int fd = process->getSyscallArg(tc, index);
779 Addr bufPtr = process->getSyscallArg(tc, index);
780 if (fd < 0 || process->sim_fd(fd) < 0) {
781 // doesn't map to any simulator fd: not a valid target fd
787 int result = fstat(process->sim_fd(fd), &hostBuf);
789 struct stat64 hostBuf;
790 int result = fstat64(process->sim_fd(fd), &hostBuf);
796 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
802 /// Target lstat() handler.
805 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
811 if (!tc->getMemPort()->tryReadString(path,
812 process->getSyscallArg(tc, index))) {
815 Addr bufPtr = process->getSyscallArg(tc, index);
817 // Adjust path for current working directory
818 path = process->fullPath(path);
821 int result = lstat(path.c_str(), &hostBuf);
826 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
831 /// Target lstat64() handler.
834 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
840 if (!tc->getMemPort()->tryReadString(path,
841 process->getSyscallArg(tc, index))) {
844 Addr bufPtr = process->getSyscallArg(tc, index);
846 // Adjust path for current working directory
847 path = process->fullPath(path);
851 int result = lstat(path.c_str(), &hostBuf);
853 struct stat64 hostBuf;
854 int result = lstat64(path.c_str(), &hostBuf);
860 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
865 /// Target fstat() handler.
868 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
872 int fd = process->sim_fd(process->getSyscallArg(tc, index));
873 Addr bufPtr = process->getSyscallArg(tc, index);
875 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
881 int result = fstat(fd, &hostBuf);
886 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
892 /// Target statfs() handler.
895 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
901 if (!tc->getMemPort()->tryReadString(path,
902 process->getSyscallArg(tc, index))) {
905 Addr bufPtr = process->getSyscallArg(tc, index);
907 // Adjust path for current working directory
908 path = process->fullPath(path);
910 struct statfs hostBuf;
911 int result = statfs(path.c_str(), &hostBuf);
916 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
922 /// Target fstatfs() handler.
925 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
929 int fd = process->sim_fd(process->getSyscallArg(tc, index));
930 Addr bufPtr = process->getSyscallArg(tc, index);
935 struct statfs hostBuf;
936 int result = fstatfs(fd, &hostBuf);
941 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
947 /// Target writev() handler.
950 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
954 int fd = process->getSyscallArg(tc, index);
955 if (fd < 0 || process->sim_fd(fd) < 0) {
956 // doesn't map to any simulator fd: not a valid target fd
960 TranslatingPort *p = tc->getMemPort();
961 uint64_t tiov_base = process->getSyscallArg(tc, index);
962 size_t count = process->getSyscallArg(tc, index);
963 struct iovec hiov[count];
964 for (size_t i = 0; i < count; ++i) {
965 typename OS::tgt_iovec tiov;
967 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
968 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
969 hiov[i].iov_len = gtoh(tiov.iov_len);
970 hiov[i].iov_base = new char [hiov[i].iov_len];
971 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
975 int result = writev(process->sim_fd(fd), hiov, count);
977 for (size_t i = 0; i < count; ++i)
978 delete [] (char *)hiov[i].iov_base;
987 /// Target mmap() handler.
989 /// We don't really handle mmap(). If the target is mmaping an
990 /// anonymous region or /dev/zero, we can get away with doing basically
991 /// nothing (since memory is initialized to zero and the simulator
992 /// doesn't really check addresses anyway).
996 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
999 Addr start = p->getSyscallArg(tc, index);
1000 uint64_t length = p->getSyscallArg(tc, index);
1001 index++; // int prot = p->getSyscallArg(tc, index);
1002 int flags = p->getSyscallArg(tc, index);
1003 int tgt_fd = p->getSyscallArg(tc, index);
1004 // int offset = p->getSyscallArg(tc, index);
1006 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1007 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1008 if (!fd_map || fd_map->fd < 0) {
1009 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1013 if (fd_map->filename != "/dev/zero") {
1014 // This is very likely broken, but leave a warning here
1015 // (rather than panic) in case /dev/zero is known by
1016 // another name on some platform
1017 warn("allowing mmap of file %s; mmap not supported on files"
1018 " other than /dev/zero\n", fd_map->filename);
1022 if ((start % TheISA::VMPageSize) != 0 ||
1023 (length % TheISA::VMPageSize) != 0) {
1024 warn("mmap failing: arguments not page-aligned: "
1025 "start 0x%x length 0x%x",
1031 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
1032 start, p->mmap_end);
1035 // pick next address from our "mmap region"
1036 if (OS::mmapGrowsDown()) {
1037 start = p->mmap_end - length;
1038 p->mmap_end = start;
1040 start = p->mmap_end;
1041 p->mmap_end += length;
1043 p->pTable->allocate(start, length);
1048 /// Target getrlimit() handler.
1051 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1055 unsigned resource = process->getSyscallArg(tc, index);
1056 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1059 case OS::TGT_RLIMIT_STACK:
1060 // max stack size in bytes: make up a number (8MB for now)
1061 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1062 rlp->rlim_cur = htog(rlp->rlim_cur);
1063 rlp->rlim_max = htog(rlp->rlim_max);
1066 case OS::TGT_RLIMIT_DATA:
1067 // max data segment size in bytes: make up a number
1068 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1069 rlp->rlim_cur = htog(rlp->rlim_cur);
1070 rlp->rlim_max = htog(rlp->rlim_max);
1074 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1080 rlp.copyOut(tc->getMemPort());
1084 /// Target gettimeofday() handler.
1087 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1091 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1093 getElapsedTime(tp->tv_sec, tp->tv_usec);
1094 tp->tv_sec += seconds_since_epoch;
1095 tp->tv_sec = TheISA::htog(tp->tv_sec);
1096 tp->tv_usec = TheISA::htog(tp->tv_usec);
1098 tp.copyOut(tc->getMemPort());
1104 /// Target utimes() handler.
1107 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1113 if (!tc->getMemPort()->tryReadString(path,
1114 process->getSyscallArg(tc, index))) {
1118 TypedBufferArg<typename OS::timeval [2]>
1119 tp(process->getSyscallArg(tc, index));
1120 tp.copyIn(tc->getMemPort());
1122 struct timeval hostTimeval[2];
1123 for (int i = 0; i < 2; ++i)
1125 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1126 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1129 // Adjust path for current working directory
1130 path = process->fullPath(path);
1132 int result = utimes(path.c_str(), hostTimeval);
1139 /// Target getrusage() function.
1142 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1146 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1147 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1149 rup->ru_utime.tv_sec = 0;
1150 rup->ru_utime.tv_usec = 0;
1151 rup->ru_stime.tv_sec = 0;
1152 rup->ru_stime.tv_usec = 0;
1160 rup->ru_inblock = 0;
1161 rup->ru_oublock = 0;
1164 rup->ru_nsignals = 0;
1169 case OS::TGT_RUSAGE_SELF:
1170 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1171 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1172 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1175 case OS::TGT_RUSAGE_CHILDREN:
1176 // do nothing. We have no child processes, so they take no time.
1180 // don't really handle THREAD or CHILDREN, but just warn and
1182 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1186 rup.copyOut(tc->getMemPort());
1191 /// Target times() function.
1194 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1198 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1200 // Fill in the time structure (in clocks)
1201 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1202 bufp->tms_utime = clocks;
1203 bufp->tms_stime = 0;
1204 bufp->tms_cutime = 0;
1205 bufp->tms_cstime = 0;
1207 // Convert to host endianness
1208 bufp->tms_utime = htog(bufp->tms_utime);
1211 bufp.copyOut(tc->getMemPort());
1213 // Return clock ticks since system boot
1217 /// Target time() function.
1220 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1223 typename OS::time_t sec, usec;
1224 getElapsedTime(sec, usec);
1225 sec += seconds_since_epoch;
1228 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1230 typename OS::time_t t = sec;
1232 TranslatingPort *p = tc->getMemPort();
1233 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1239 #endif // __SIM_SYSCALL_EMUL_HH__