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.
47 #include <sys/fcntl.h> // for O_BINARY
53 #include "base/chunk_generator.hh"
54 #include "base/intmath.hh" // for RoundUp
55 #include "base/misc.hh"
56 #include "base/trace.hh"
57 #include "base/types.hh"
58 #include "cpu/base.hh"
59 #include "cpu/thread_context.hh"
60 #include "mem/translating_port.hh"
61 #include "mem/page_table.hh"
62 #include "sim/system.hh"
63 #include "sim/process.hh"
66 /// System call descriptor.
72 /// Typedef for target syscall handler functions.
73 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
74 LiveProcess *, ThreadContext *);
76 const char *name; //!< Syscall name (e.g., "open").
77 FuncPtr funcPtr; //!< Pointer to emulation function.
78 int flags; //!< Flags (see Flags enum).
80 /// Flag values for controlling syscall behavior.
82 /// Don't set return regs according to funcPtr return value.
83 /// Used for syscalls with non-standard return conventions
84 /// that explicitly set the ThreadContext regs (e.g.,
86 SuppressReturnValue = 1
90 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
91 : name(_name), funcPtr(_funcPtr), flags(_flags)
95 /// Emulate the syscall. Public interface for calling through funcPtr.
96 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
100 class BaseBufferArg {
104 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
106 bufPtr = new uint8_t[size];
107 // clear out buffer: in case we only partially populate this,
108 // and then do a copyOut(), we want to make sure we don't
109 // introduce any random junk into the simulated address space
110 memset(bufPtr, 0, size);
113 virtual ~BaseBufferArg() { delete [] bufPtr; }
116 // copy data into simulator space (read from target memory)
118 virtual bool copyIn(TranslatingPort *memport)
120 memport->readBlob(addr, bufPtr, size);
121 return true; // no EFAULT detection for now
125 // copy data out of simulator space (write to target memory)
127 virtual bool copyOut(TranslatingPort *memport)
129 memport->writeBlob(addr, bufPtr, size);
130 return true; // no EFAULT detection for now
140 class BufferArg : public BaseBufferArg
143 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
144 void *bufferPtr() { return bufPtr; }
148 class TypedBufferArg : public BaseBufferArg
151 // user can optionally specify a specific number of bytes to
152 // allocate to deal with those structs that have variable-size
154 TypedBufferArg(Addr _addr, int _size = sizeof(T))
155 : BaseBufferArg(_addr, _size)
159 operator T*() { return (T *)bufPtr; }
161 // dereference operators
162 T &operator*() { return *((T *)bufPtr); }
163 T* operator->() { return (T *)bufPtr; }
164 T &operator[](int i) { return ((T *)bufPtr)[i]; }
167 //////////////////////////////////////////////////////////////////////
169 // The following emulation functions are generic enough that they
170 // don't need to be recompiled for different emulated OS's. They are
171 // defined in sim/syscall_emul.cc.
173 //////////////////////////////////////////////////////////////////////
176 /// Handler for unimplemented syscalls that we haven't thought about.
177 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
178 LiveProcess *p, ThreadContext *tc);
180 /// Handler for unimplemented syscalls that we never intend to
181 /// implement (signal handling, etc.) and should not affect the correct
182 /// behavior of the program. Print a warning only if the appropriate
183 /// trace flag is enabled. Return success to the target program.
184 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
185 LiveProcess *p, ThreadContext *tc);
187 /// Target exit() handler: terminate current context.
188 SyscallReturn exitFunc(SyscallDesc *desc, int num,
189 LiveProcess *p, ThreadContext *tc);
191 /// Target exit_group() handler: terminate simulation. (exit all threads)
192 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
193 LiveProcess *p, ThreadContext *tc);
195 /// Target getpagesize() handler.
196 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
197 LiveProcess *p, ThreadContext *tc);
199 /// Target brk() handler: set brk address.
200 SyscallReturn brkFunc(SyscallDesc *desc, int num,
201 LiveProcess *p, ThreadContext *tc);
203 /// Target close() handler.
204 SyscallReturn closeFunc(SyscallDesc *desc, int num,
205 LiveProcess *p, ThreadContext *tc);
207 /// Target read() handler.
208 SyscallReturn readFunc(SyscallDesc *desc, int num,
209 LiveProcess *p, ThreadContext *tc);
211 /// Target write() handler.
212 SyscallReturn writeFunc(SyscallDesc *desc, int num,
213 LiveProcess *p, ThreadContext *tc);
215 /// Target lseek() handler.
216 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
217 LiveProcess *p, ThreadContext *tc);
219 /// Target _llseek() handler.
220 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
221 LiveProcess *p, ThreadContext *tc);
223 /// Target munmap() handler.
224 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
225 LiveProcess *p, ThreadContext *tc);
227 /// Target gethostname() handler.
228 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
229 LiveProcess *p, ThreadContext *tc);
231 /// Target getcwd() handler.
232 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
233 LiveProcess *p, ThreadContext *tc);
235 /// Target unlink() handler.
236 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
237 LiveProcess *p, ThreadContext *tc);
239 /// Target unlink() handler.
240 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
241 LiveProcess *p, ThreadContext *tc);
243 /// Target mkdir() handler.
244 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
245 LiveProcess *p, ThreadContext *tc);
247 /// Target rename() handler.
248 SyscallReturn renameFunc(SyscallDesc *desc, int num,
249 LiveProcess *p, ThreadContext *tc);
252 /// Target truncate() handler.
253 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
254 LiveProcess *p, ThreadContext *tc);
257 /// Target ftruncate() handler.
258 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
259 LiveProcess *p, ThreadContext *tc);
262 /// Target umask() handler.
263 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
264 LiveProcess *p, ThreadContext *tc);
267 /// Target chown() handler.
268 SyscallReturn chownFunc(SyscallDesc *desc, int num,
269 LiveProcess *p, ThreadContext *tc);
272 /// Target fchown() handler.
273 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
274 LiveProcess *p, ThreadContext *tc);
276 /// Target dup() handler.
277 SyscallReturn dupFunc(SyscallDesc *desc, int num,
278 LiveProcess *process, ThreadContext *tc);
280 /// Target fnctl() handler.
281 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
282 LiveProcess *process, ThreadContext *tc);
284 /// Target fcntl64() handler.
285 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
286 LiveProcess *process, ThreadContext *tc);
288 /// Target setuid() handler.
289 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
290 LiveProcess *p, ThreadContext *tc);
292 /// Target getpid() handler.
293 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
294 LiveProcess *p, ThreadContext *tc);
296 /// Target getuid() handler.
297 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
298 LiveProcess *p, ThreadContext *tc);
300 /// Target getgid() handler.
301 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
302 LiveProcess *p, ThreadContext *tc);
304 /// Target getppid() handler.
305 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
306 LiveProcess *p, ThreadContext *tc);
308 /// Target geteuid() handler.
309 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
310 LiveProcess *p, ThreadContext *tc);
312 /// Target getegid() handler.
313 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
314 LiveProcess *p, ThreadContext *tc);
316 /// Target clone() handler.
317 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
318 LiveProcess *p, ThreadContext *tc);
321 /// Pseudo Funcs - These functions use a different return convension,
322 /// returning a second value in a register other than the normal return register
323 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
324 LiveProcess *process, ThreadContext *tc);
326 /// Target getpidPseudo() handler.
327 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
328 LiveProcess *p, ThreadContext *tc);
330 /// Target getuidPseudo() handler.
331 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
332 LiveProcess *p, ThreadContext *tc);
334 /// Target getgidPseudo() handler.
335 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
336 LiveProcess *p, ThreadContext *tc);
339 /// A readable name for 1,000,000, for converting microseconds to seconds.
340 const int one_million = 1000000;
342 /// Approximate seconds since the epoch (1/1/1970). About a billion,
343 /// by my reckoning. We want to keep this a constant (not use the
344 /// real-world time) to keep simulations repeatable.
345 const unsigned seconds_since_epoch = 1000000000;
347 /// Helper function to convert current elapsed time to seconds and
349 template <class T1, class T2>
351 getElapsedTime(T1 &sec, T2 &usec)
353 int elapsed_usecs = curTick / Clock::Int::us;
354 sec = elapsed_usecs / one_million;
355 usec = elapsed_usecs % one_million;
358 //////////////////////////////////////////////////////////////////////
360 // The following emulation functions are generic, but need to be
361 // templated to account for differences in types, constants, etc.
363 //////////////////////////////////////////////////////////////////////
366 typedef struct stat hst_stat;
367 typedef struct stat hst_stat64;
369 typedef struct stat hst_stat;
370 typedef struct stat64 hst_stat64;
373 //// Helper function to convert a host stat buffer to a target stat
374 //// buffer. Also copies the target buffer out to the simulated
375 //// memory space. Used by stat(), fstat(), and lstat().
377 template <typename target_stat, typename host_stat>
379 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
381 using namespace TheISA;
386 tgt->st_dev = host->st_dev;
387 tgt->st_dev = htog(tgt->st_dev);
388 tgt->st_ino = host->st_ino;
389 tgt->st_ino = htog(tgt->st_ino);
390 tgt->st_mode = host->st_mode;
392 // Claim to be a character device
393 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
394 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
396 tgt->st_mode = htog(tgt->st_mode);
397 tgt->st_nlink = host->st_nlink;
398 tgt->st_nlink = htog(tgt->st_nlink);
399 tgt->st_uid = host->st_uid;
400 tgt->st_uid = htog(tgt->st_uid);
401 tgt->st_gid = host->st_gid;
402 tgt->st_gid = htog(tgt->st_gid);
404 tgt->st_rdev = 0x880d;
406 tgt->st_rdev = host->st_rdev;
407 tgt->st_rdev = htog(tgt->st_rdev);
408 tgt->st_size = host->st_size;
409 tgt->st_size = htog(tgt->st_size);
410 tgt->st_atimeX = host->st_atime;
411 tgt->st_atimeX = htog(tgt->st_atimeX);
412 tgt->st_mtimeX = host->st_mtime;
413 tgt->st_mtimeX = htog(tgt->st_mtimeX);
414 tgt->st_ctimeX = host->st_ctime;
415 tgt->st_ctimeX = htog(tgt->st_ctimeX);
416 // Force the block size to be 8k. This helps to ensure buffered io works
417 // consistently across different hosts.
418 tgt->st_blksize = 0x2000;
419 tgt->st_blksize = htog(tgt->st_blksize);
420 tgt->st_blocks = host->st_blocks;
421 tgt->st_blocks = htog(tgt->st_blocks);
426 template <typename target_stat, typename host_stat64>
428 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
430 using namespace TheISA;
432 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
433 #if defined(STAT_HAVE_NSEC)
434 tgt->st_atime_nsec = host->st_atime_nsec;
435 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
436 tgt->st_mtime_nsec = host->st_mtime_nsec;
437 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
438 tgt->st_ctime_nsec = host->st_ctime_nsec;
439 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
441 tgt->st_atime_nsec = 0;
442 tgt->st_mtime_nsec = 0;
443 tgt->st_ctime_nsec = 0;
447 //Here are a couple convenience functions
450 copyOutStatBuf(TranslatingPort * mem, Addr addr,
451 hst_stat *host, bool fakeTTY = false)
453 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
454 tgt_stat_buf tgt(addr);
455 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
461 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
462 hst_stat64 *host, bool fakeTTY = false)
464 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
465 tgt_stat_buf tgt(addr);
466 convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
470 /// Target ioctl() handler. For the most part, programs call ioctl()
471 /// only to find out if their stdout is a tty, to determine whether to
472 /// do line or block buffering.
475 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
478 int fd = process->getSyscallArg(tc, 0);
479 unsigned req = process->getSyscallArg(tc, 1);
481 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
483 if (fd < 0 || process->sim_fd(fd) < 0) {
484 // doesn't map to any simulator fd: not a valid target fd
489 case OS::TIOCISATTY_:
500 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
501 fd, req, tc->readPC());
505 /// Target open() handler.
508 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
513 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
516 if (path == "/dev/sysdev0") {
517 // This is a memory-mapped high-resolution timer device on Alpha.
518 // We don't support it, so just punt.
519 warn("Ignoring open(%s, ...)\n", path);
523 int tgtFlags = process->getSyscallArg(tc, 1);
524 int mode = process->getSyscallArg(tc, 2);
527 // translate open flags
528 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
529 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
530 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
531 hostFlags |= OS::openFlagTable[i].hostFlag;
535 // any target flags left?
537 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
540 hostFlags |= O_BINARY;
543 // Adjust path for current working directory
544 path = process->fullPath(path);
546 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
549 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
550 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
551 // It's a proc/sys entery and requires special handling
552 fd = OS::openSpecialFile(path, process, tc);
553 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
556 fd = open(path.c_str(), hostFlags, mode);
557 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
562 /// Target sysinfo() handler.
565 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
569 TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0));
571 sysinfo->uptime=seconds_since_epoch;
572 sysinfo->totalram=process->system->memSize();
574 sysinfo.copyOut(tc->getMemPort());
579 /// Target chmod() handler.
582 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
587 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
590 uint32_t mode = process->getSyscallArg(tc, 1);
593 // XXX translate mode flags via OS::something???
596 // Adjust path for current working directory
597 path = process->fullPath(path);
600 int result = chmod(path.c_str(), hostMode);
608 /// Target fchmod() handler.
611 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
614 int fd = process->getSyscallArg(tc, 0);
615 if (fd < 0 || process->sim_fd(fd) < 0) {
616 // doesn't map to any simulator fd: not a valid target fd
620 uint32_t mode = process->getSyscallArg(tc, 1);
623 // XXX translate mode flags via OS::someting???
627 int result = fchmod(process->sim_fd(fd), hostMode);
634 /// Target mremap() handler.
637 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
639 Addr start = process->getSyscallArg(tc, 0);
640 uint64_t old_length = process->getSyscallArg(tc, 1);
641 uint64_t new_length = process->getSyscallArg(tc, 2);
642 uint64_t flags = process->getSyscallArg(tc, 3);
644 if ((start % TheISA::VMPageSize != 0) ||
645 (new_length % TheISA::VMPageSize != 0)) {
646 warn("mremap failing: arguments not page aligned");
650 if (new_length > old_length) {
651 if ((start + old_length) == process->mmap_end) {
652 uint64_t diff = new_length - old_length;
653 process->pTable->allocate(process->mmap_end, diff);
654 process->mmap_end += diff;
657 // sys/mman.h defined MREMAP_MAYMOVE
659 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
662 process->pTable->remap(start, old_length, process->mmap_end);
663 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
664 process->mmap_end, process->mmap_end + new_length, new_length);
665 start = process->mmap_end;
666 // add on the remaining unallocated pages
667 process->pTable->allocate(start + old_length, new_length - old_length);
668 process->mmap_end += new_length;
669 warn("returning %08p as start\n", start);
674 process->pTable->deallocate(start + new_length, old_length -
680 /// Target stat() handler.
683 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
688 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
691 // Adjust path for current working directory
692 path = process->fullPath(path);
695 int result = stat(path.c_str(), &hostBuf);
700 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
707 /// Target stat64() handler.
710 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
715 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
718 // Adjust path for current working directory
719 path = process->fullPath(path);
723 int result = stat(path.c_str(), &hostBuf);
725 struct stat64 hostBuf;
726 int result = stat64(path.c_str(), &hostBuf);
732 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
739 /// Target fstat64() handler.
742 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
745 int fd = process->getSyscallArg(tc, 0);
746 if (fd < 0 || process->sim_fd(fd) < 0) {
747 // doesn't map to any simulator fd: not a valid target fd
753 int result = fstat(process->sim_fd(fd), &hostBuf);
755 struct stat64 hostBuf;
756 int result = fstat64(process->sim_fd(fd), &hostBuf);
762 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
763 &hostBuf, (fd == 1));
769 /// Target lstat() handler.
772 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
777 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
780 // Adjust path for current working directory
781 path = process->fullPath(path);
784 int result = lstat(path.c_str(), &hostBuf);
789 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
795 /// Target lstat64() handler.
798 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
803 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
806 // Adjust path for current working directory
807 path = process->fullPath(path);
811 int result = lstat(path.c_str(), &hostBuf);
813 struct stat64 hostBuf;
814 int result = lstat64(path.c_str(), &hostBuf);
820 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
826 /// Target fstat() handler.
829 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
832 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
834 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
840 int result = fstat(fd, &hostBuf);
845 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
846 &hostBuf, (fd == 1));
852 /// Target statfs() handler.
855 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
860 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
863 // Adjust path for current working directory
864 path = process->fullPath(path);
866 struct statfs hostBuf;
867 int result = statfs(path.c_str(), &hostBuf);
872 OS::copyOutStatfsBuf(tc->getMemPort(),
873 (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
879 /// Target fstatfs() handler.
882 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
885 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
890 struct statfs hostBuf;
891 int result = fstatfs(fd, &hostBuf);
896 OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
903 /// Target writev() handler.
906 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
909 int fd = process->getSyscallArg(tc, 0);
910 if (fd < 0 || process->sim_fd(fd) < 0) {
911 // doesn't map to any simulator fd: not a valid target fd
915 TranslatingPort *p = tc->getMemPort();
916 uint64_t tiov_base = process->getSyscallArg(tc, 1);
917 size_t count = process->getSyscallArg(tc, 2);
918 struct iovec hiov[count];
919 for (size_t i = 0; i < count; ++i) {
920 typename OS::tgt_iovec tiov;
922 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
923 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
924 hiov[i].iov_len = gtoh(tiov.iov_len);
925 hiov[i].iov_base = new char [hiov[i].iov_len];
926 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
930 int result = writev(process->sim_fd(fd), hiov, count);
932 for (size_t i = 0; i < count; ++i)
933 delete [] (char *)hiov[i].iov_base;
942 /// Target mmap() handler.
944 /// We don't really handle mmap(). If the target is mmaping an
945 /// anonymous region or /dev/zero, we can get away with doing basically
946 /// nothing (since memory is initialized to zero and the simulator
947 /// doesn't really check addresses anyway). Always print a warning,
948 /// since this could be seriously broken if we're not mapping
951 /// Someday we should explicitly check for /dev/zero in open, flag the
952 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
956 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
958 Addr start = p->getSyscallArg(tc, 0);
959 uint64_t length = p->getSyscallArg(tc, 1);
960 // int prot = p->getSyscallArg(tc, 2);
961 int flags = p->getSyscallArg(tc, 3);
962 // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
963 // int offset = p->getSyscallArg(tc, 5);
966 if ((start % TheISA::VMPageSize) != 0 ||
967 (length % TheISA::VMPageSize) != 0) {
968 warn("mmap failing: arguments not page-aligned: "
969 "start 0x%x length 0x%x",
975 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
979 // pick next address from our "mmap region"
981 p->pTable->allocate(start, length);
982 p->mmap_end += length;
984 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
985 warn("allowing mmap of file @ fd %d. "
986 "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
992 /// Target getrlimit() handler.
995 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
998 unsigned resource = process->getSyscallArg(tc, 0);
999 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
1002 case OS::TGT_RLIMIT_STACK:
1003 // max stack size in bytes: make up a number (8MB for now)
1004 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1005 rlp->rlim_cur = htog(rlp->rlim_cur);
1006 rlp->rlim_max = htog(rlp->rlim_max);
1009 case OS::TGT_RLIMIT_DATA:
1010 // max data segment size in bytes: make up a number
1011 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1012 rlp->rlim_cur = htog(rlp->rlim_cur);
1013 rlp->rlim_max = htog(rlp->rlim_max);
1017 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1023 rlp.copyOut(tc->getMemPort());
1027 /// Target gettimeofday() handler.
1030 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1033 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1035 getElapsedTime(tp->tv_sec, tp->tv_usec);
1036 tp->tv_sec += seconds_since_epoch;
1037 tp->tv_sec = TheISA::htog(tp->tv_sec);
1038 tp->tv_usec = TheISA::htog(tp->tv_usec);
1040 tp.copyOut(tc->getMemPort());
1046 /// Target utimes() handler.
1049 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1054 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
1057 TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
1058 tp.copyIn(tc->getMemPort());
1060 struct timeval hostTimeval[2];
1061 for (int i = 0; i < 2; ++i)
1063 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1064 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1067 // Adjust path for current working directory
1068 path = process->fullPath(path);
1070 int result = utimes(path.c_str(), hostTimeval);
1077 /// Target getrusage() function.
1080 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1083 int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN
1084 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1086 rup->ru_utime.tv_sec = 0;
1087 rup->ru_utime.tv_usec = 0;
1088 rup->ru_stime.tv_sec = 0;
1089 rup->ru_stime.tv_usec = 0;
1097 rup->ru_inblock = 0;
1098 rup->ru_oublock = 0;
1101 rup->ru_nsignals = 0;
1106 case OS::TGT_RUSAGE_SELF:
1107 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1108 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1109 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1112 case OS::TGT_RUSAGE_CHILDREN:
1113 // do nothing. We have no child processes, so they take no time.
1117 // don't really handle THREAD or CHILDREN, but just warn and
1119 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1123 rup.copyOut(tc->getMemPort());
1131 #endif // __SIM_SYSCALL_EMUL_HH__