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/process.hh"
65 /// System call descriptor.
71 /// Typedef for target syscall handler functions.
72 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
73 LiveProcess *, ThreadContext *);
75 const char *name; //!< Syscall name (e.g., "open").
76 FuncPtr funcPtr; //!< Pointer to emulation function.
77 int flags; //!< Flags (see Flags enum).
79 /// Flag values for controlling syscall behavior.
81 /// Don't set return regs according to funcPtr return value.
82 /// Used for syscalls with non-standard return conventions
83 /// that explicitly set the ThreadContext regs (e.g.,
85 SuppressReturnValue = 1
89 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
90 : name(_name), funcPtr(_funcPtr), flags(_flags)
94 /// Emulate the syscall. Public interface for calling through funcPtr.
95 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
103 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
105 bufPtr = new uint8_t[size];
106 // clear out buffer: in case we only partially populate this,
107 // and then do a copyOut(), we want to make sure we don't
108 // introduce any random junk into the simulated address space
109 memset(bufPtr, 0, size);
112 virtual ~BaseBufferArg() { delete [] bufPtr; }
115 // copy data into simulator space (read from target memory)
117 virtual bool copyIn(TranslatingPort *memport)
119 memport->readBlob(addr, bufPtr, size);
120 return true; // no EFAULT detection for now
124 // copy data out of simulator space (write to target memory)
126 virtual bool copyOut(TranslatingPort *memport)
128 memport->writeBlob(addr, bufPtr, size);
129 return true; // no EFAULT detection for now
139 class BufferArg : public BaseBufferArg
142 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
143 void *bufferPtr() { return bufPtr; }
147 class TypedBufferArg : public BaseBufferArg
150 // user can optionally specify a specific number of bytes to
151 // allocate to deal with those structs that have variable-size
153 TypedBufferArg(Addr _addr, int _size = sizeof(T))
154 : BaseBufferArg(_addr, _size)
158 operator T*() { return (T *)bufPtr; }
160 // dereference operators
161 T &operator*() { return *((T *)bufPtr); }
162 T* operator->() { return (T *)bufPtr; }
163 T &operator[](int i) { return ((T *)bufPtr)[i]; }
166 //////////////////////////////////////////////////////////////////////
168 // The following emulation functions are generic enough that they
169 // don't need to be recompiled for different emulated OS's. They are
170 // defined in sim/syscall_emul.cc.
172 //////////////////////////////////////////////////////////////////////
175 /// Handler for unimplemented syscalls that we haven't thought about.
176 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
177 LiveProcess *p, ThreadContext *tc);
179 /// Handler for unimplemented syscalls that we never intend to
180 /// implement (signal handling, etc.) and should not affect the correct
181 /// behavior of the program. Print a warning only if the appropriate
182 /// trace flag is enabled. Return success to the target program.
183 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
184 LiveProcess *p, ThreadContext *tc);
186 /// Target exit() handler: terminate current context.
187 SyscallReturn exitFunc(SyscallDesc *desc, int num,
188 LiveProcess *p, ThreadContext *tc);
190 /// Target exit_group() handler: terminate simulation. (exit all threads)
191 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
192 LiveProcess *p, ThreadContext *tc);
194 /// Target getpagesize() handler.
195 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
196 LiveProcess *p, ThreadContext *tc);
198 /// Target brk() handler: set brk address.
199 SyscallReturn brkFunc(SyscallDesc *desc, int num,
200 LiveProcess *p, ThreadContext *tc);
202 /// Target close() handler.
203 SyscallReturn closeFunc(SyscallDesc *desc, int num,
204 LiveProcess *p, ThreadContext *tc);
206 /// Target read() handler.
207 SyscallReturn readFunc(SyscallDesc *desc, int num,
208 LiveProcess *p, ThreadContext *tc);
210 /// Target write() handler.
211 SyscallReturn writeFunc(SyscallDesc *desc, int num,
212 LiveProcess *p, ThreadContext *tc);
214 /// Target lseek() handler.
215 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
216 LiveProcess *p, ThreadContext *tc);
218 /// Target _llseek() handler.
219 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
220 LiveProcess *p, ThreadContext *tc);
222 /// Target munmap() handler.
223 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
224 LiveProcess *p, ThreadContext *tc);
226 /// Target gethostname() handler.
227 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
228 LiveProcess *p, ThreadContext *tc);
230 /// Target getcwd() handler.
231 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
232 LiveProcess *p, ThreadContext *tc);
234 /// Target unlink() handler.
235 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
236 LiveProcess *p, ThreadContext *tc);
238 /// Target unlink() handler.
239 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
240 LiveProcess *p, ThreadContext *tc);
242 /// Target mkdir() handler.
243 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
244 LiveProcess *p, ThreadContext *tc);
246 /// Target rename() handler.
247 SyscallReturn renameFunc(SyscallDesc *desc, int num,
248 LiveProcess *p, ThreadContext *tc);
251 /// Target truncate() handler.
252 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
253 LiveProcess *p, ThreadContext *tc);
256 /// Target ftruncate() handler.
257 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
258 LiveProcess *p, ThreadContext *tc);
261 /// Target umask() handler.
262 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
263 LiveProcess *p, ThreadContext *tc);
266 /// Target chown() handler.
267 SyscallReturn chownFunc(SyscallDesc *desc, int num,
268 LiveProcess *p, ThreadContext *tc);
271 /// Target fchown() handler.
272 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
273 LiveProcess *p, ThreadContext *tc);
275 /// Target dup() handler.
276 SyscallReturn dupFunc(SyscallDesc *desc, int num,
277 LiveProcess *process, ThreadContext *tc);
279 /// Target fnctl() handler.
280 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
281 LiveProcess *process, ThreadContext *tc);
283 /// Target fcntl64() handler.
284 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
285 LiveProcess *process, ThreadContext *tc);
287 /// Target setuid() handler.
288 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
289 LiveProcess *p, ThreadContext *tc);
291 /// Target getpid() handler.
292 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
293 LiveProcess *p, ThreadContext *tc);
295 /// Target getuid() handler.
296 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
297 LiveProcess *p, ThreadContext *tc);
299 /// Target getgid() handler.
300 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
301 LiveProcess *p, ThreadContext *tc);
303 /// Target getppid() handler.
304 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
305 LiveProcess *p, ThreadContext *tc);
307 /// Target geteuid() handler.
308 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
309 LiveProcess *p, ThreadContext *tc);
311 /// Target getegid() handler.
312 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
313 LiveProcess *p, ThreadContext *tc);
315 /// Target clone() handler.
316 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
317 LiveProcess *p, ThreadContext *tc);
320 /// Pseudo Funcs - These functions use a different return convension,
321 /// returning a second value in a register other than the normal return register
322 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
323 LiveProcess *process, ThreadContext *tc);
325 /// Target getpidPseudo() handler.
326 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
327 LiveProcess *p, ThreadContext *tc);
329 /// Target getuidPseudo() handler.
330 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
331 LiveProcess *p, ThreadContext *tc);
333 /// Target getgidPseudo() handler.
334 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
335 LiveProcess *p, ThreadContext *tc);
338 /// A readable name for 1,000,000, for converting microseconds to seconds.
339 const int one_million = 1000000;
341 /// Approximate seconds since the epoch (1/1/1970). About a billion,
342 /// by my reckoning. We want to keep this a constant (not use the
343 /// real-world time) to keep simulations repeatable.
344 const unsigned seconds_since_epoch = 1000000000;
346 /// Helper function to convert current elapsed time to seconds and
348 template <class T1, class T2>
350 getElapsedTime(T1 &sec, T2 &usec)
352 int elapsed_usecs = curTick / Clock::Int::us;
353 sec = elapsed_usecs / one_million;
354 usec = elapsed_usecs % one_million;
357 //////////////////////////////////////////////////////////////////////
359 // The following emulation functions are generic, but need to be
360 // templated to account for differences in types, constants, etc.
362 //////////////////////////////////////////////////////////////////////
365 typedef struct stat hst_stat;
366 typedef struct stat hst_stat64;
368 typedef struct stat hst_stat;
369 typedef struct stat64 hst_stat64;
372 //// Helper function to convert a host stat buffer to a target stat
373 //// buffer. Also copies the target buffer out to the simulated
374 //// memory space. Used by stat(), fstat(), and lstat().
376 template <typename target_stat, typename host_stat>
378 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
380 using namespace TheISA;
385 tgt->st_dev = host->st_dev;
386 tgt->st_dev = htog(tgt->st_dev);
387 tgt->st_ino = host->st_ino;
388 tgt->st_ino = htog(tgt->st_ino);
389 tgt->st_mode = host->st_mode;
391 // Claim to be a character device
392 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
393 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
395 tgt->st_mode = htog(tgt->st_mode);
396 tgt->st_nlink = host->st_nlink;
397 tgt->st_nlink = htog(tgt->st_nlink);
398 tgt->st_uid = host->st_uid;
399 tgt->st_uid = htog(tgt->st_uid);
400 tgt->st_gid = host->st_gid;
401 tgt->st_gid = htog(tgt->st_gid);
403 tgt->st_rdev = 0x880d;
405 tgt->st_rdev = host->st_rdev;
406 tgt->st_rdev = htog(tgt->st_rdev);
407 tgt->st_size = host->st_size;
408 tgt->st_size = htog(tgt->st_size);
409 tgt->st_atimeX = host->st_atime;
410 tgt->st_atimeX = htog(tgt->st_atimeX);
411 tgt->st_mtimeX = host->st_mtime;
412 tgt->st_mtimeX = htog(tgt->st_mtimeX);
413 tgt->st_ctimeX = host->st_ctime;
414 tgt->st_ctimeX = htog(tgt->st_ctimeX);
415 // Force the block size to be 8k. This helps to ensure buffered io works
416 // consistently across different hosts.
417 tgt->st_blksize = 0x2000;
418 tgt->st_blksize = htog(tgt->st_blksize);
419 tgt->st_blocks = host->st_blocks;
420 tgt->st_blocks = htog(tgt->st_blocks);
425 template <typename target_stat, typename host_stat64>
427 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
429 using namespace TheISA;
431 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
432 #if defined(STAT_HAVE_NSEC)
433 tgt->st_atime_nsec = host->st_atime_nsec;
434 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
435 tgt->st_mtime_nsec = host->st_mtime_nsec;
436 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
437 tgt->st_ctime_nsec = host->st_ctime_nsec;
438 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
440 tgt->st_atime_nsec = 0;
441 tgt->st_mtime_nsec = 0;
442 tgt->st_ctime_nsec = 0;
446 //Here are a couple convenience functions
449 copyOutStatBuf(TranslatingPort * mem, Addr addr,
450 hst_stat *host, bool fakeTTY = false)
452 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
453 tgt_stat_buf tgt(addr);
454 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
460 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
461 hst_stat64 *host, bool fakeTTY = false)
463 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
464 tgt_stat_buf tgt(addr);
465 convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
469 /// Target ioctl() handler. For the most part, programs call ioctl()
470 /// only to find out if their stdout is a tty, to determine whether to
471 /// do line or block buffering.
474 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
477 int fd = process->getSyscallArg(tc, 0);
478 unsigned req = process->getSyscallArg(tc, 1);
480 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
482 if (fd < 0 || process->sim_fd(fd) < 0) {
483 // doesn't map to any simulator fd: not a valid target fd
488 case OS::TIOCISATTY_:
499 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
500 fd, req, tc->readPC());
504 /// Target open() handler.
507 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
512 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
515 if (path == "/dev/sysdev0") {
516 // This is a memory-mapped high-resolution timer device on Alpha.
517 // We don't support it, so just punt.
518 warn("Ignoring open(%s, ...)\n", path);
522 int tgtFlags = process->getSyscallArg(tc, 1);
523 int mode = process->getSyscallArg(tc, 2);
526 // translate open flags
527 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
528 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
529 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
530 hostFlags |= OS::openFlagTable[i].hostFlag;
534 // any target flags left?
536 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
539 hostFlags |= O_BINARY;
542 // Adjust path for current working directory
543 path = process->fullPath(path);
545 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
548 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
549 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
550 // It's a proc/sys entery and requires special handling
551 fd = OS::openSpecialFile(path, process, tc);
552 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
555 fd = open(path.c_str(), hostFlags, mode);
556 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
562 /// Target chmod() handler.
565 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
570 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
573 uint32_t mode = process->getSyscallArg(tc, 1);
576 // XXX translate mode flags via OS::something???
579 // Adjust path for current working directory
580 path = process->fullPath(path);
583 int result = chmod(path.c_str(), hostMode);
591 /// Target fchmod() handler.
594 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
597 int fd = process->getSyscallArg(tc, 0);
598 if (fd < 0 || process->sim_fd(fd) < 0) {
599 // doesn't map to any simulator fd: not a valid target fd
603 uint32_t mode = process->getSyscallArg(tc, 1);
606 // XXX translate mode flags via OS::someting???
610 int result = fchmod(process->sim_fd(fd), hostMode);
617 /// Target mremap() handler.
620 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
622 Addr start = process->getSyscallArg(tc, 0);
623 uint64_t old_length = process->getSyscallArg(tc, 1);
624 uint64_t new_length = process->getSyscallArg(tc, 2);
625 uint64_t flags = process->getSyscallArg(tc, 3);
627 if ((start % TheISA::VMPageSize != 0) ||
628 (new_length % TheISA::VMPageSize != 0)) {
629 warn("mremap failing: arguments not page aligned");
633 if (new_length > old_length) {
634 if ((start + old_length) == process->mmap_end) {
635 uint64_t diff = new_length - old_length;
636 process->pTable->allocate(process->mmap_end, diff);
637 process->mmap_end += diff;
640 // sys/mman.h defined MREMAP_MAYMOVE
642 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
645 process->pTable->remap(start, old_length, process->mmap_end);
646 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
647 process->mmap_end, process->mmap_end + new_length, new_length);
648 start = process->mmap_end;
649 // add on the remaining unallocated pages
650 process->pTable->allocate(start + old_length, new_length - old_length);
651 process->mmap_end += new_length;
652 warn("returning %08p as start\n", start);
657 process->pTable->deallocate(start + new_length, old_length -
663 /// Target stat() handler.
666 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
671 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
674 // Adjust path for current working directory
675 path = process->fullPath(path);
678 int result = stat(path.c_str(), &hostBuf);
683 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
690 /// Target stat64() handler.
693 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
698 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
701 // Adjust path for current working directory
702 path = process->fullPath(path);
706 int result = stat(path.c_str(), &hostBuf);
708 struct stat64 hostBuf;
709 int result = stat64(path.c_str(), &hostBuf);
715 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
722 /// Target fstat64() handler.
725 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
728 int fd = process->getSyscallArg(tc, 0);
729 if (fd < 0 || process->sim_fd(fd) < 0) {
730 // doesn't map to any simulator fd: not a valid target fd
736 int result = fstat(process->sim_fd(fd), &hostBuf);
738 struct stat64 hostBuf;
739 int result = fstat64(process->sim_fd(fd), &hostBuf);
745 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
746 &hostBuf, (fd == 1));
752 /// Target lstat() handler.
755 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
760 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
763 // Adjust path for current working directory
764 path = process->fullPath(path);
767 int result = lstat(path.c_str(), &hostBuf);
772 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
778 /// Target lstat64() handler.
781 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
786 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
789 // Adjust path for current working directory
790 path = process->fullPath(path);
794 int result = lstat(path.c_str(), &hostBuf);
796 struct stat64 hostBuf;
797 int result = lstat64(path.c_str(), &hostBuf);
803 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
809 /// Target fstat() handler.
812 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
815 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
817 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
823 int result = fstat(fd, &hostBuf);
828 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
829 &hostBuf, (fd == 1));
835 /// Target statfs() handler.
838 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
843 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
846 // Adjust path for current working directory
847 path = process->fullPath(path);
849 struct statfs hostBuf;
850 int result = statfs(path.c_str(), &hostBuf);
855 OS::copyOutStatfsBuf(tc->getMemPort(),
856 (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
862 /// Target fstatfs() handler.
865 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
868 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
873 struct statfs hostBuf;
874 int result = fstatfs(fd, &hostBuf);
879 OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
886 /// Target writev() handler.
889 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
892 int fd = process->getSyscallArg(tc, 0);
893 if (fd < 0 || process->sim_fd(fd) < 0) {
894 // doesn't map to any simulator fd: not a valid target fd
898 TranslatingPort *p = tc->getMemPort();
899 uint64_t tiov_base = process->getSyscallArg(tc, 1);
900 size_t count = process->getSyscallArg(tc, 2);
901 struct iovec hiov[count];
902 for (size_t i = 0; i < count; ++i) {
903 typename OS::tgt_iovec tiov;
905 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
906 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
907 hiov[i].iov_len = gtoh(tiov.iov_len);
908 hiov[i].iov_base = new char [hiov[i].iov_len];
909 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
913 int result = writev(process->sim_fd(fd), hiov, count);
915 for (size_t i = 0; i < count; ++i)
916 delete [] (char *)hiov[i].iov_base;
925 /// Target mmap() handler.
927 /// We don't really handle mmap(). If the target is mmaping an
928 /// anonymous region or /dev/zero, we can get away with doing basically
929 /// nothing (since memory is initialized to zero and the simulator
930 /// doesn't really check addresses anyway). Always print a warning,
931 /// since this could be seriously broken if we're not mapping
934 /// Someday we should explicitly check for /dev/zero in open, flag the
935 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
939 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
941 Addr start = p->getSyscallArg(tc, 0);
942 uint64_t length = p->getSyscallArg(tc, 1);
943 // int prot = p->getSyscallArg(tc, 2);
944 int flags = p->getSyscallArg(tc, 3);
945 // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
946 // int offset = p->getSyscallArg(tc, 5);
949 if ((start % TheISA::VMPageSize) != 0 ||
950 (length % TheISA::VMPageSize) != 0) {
951 warn("mmap failing: arguments not page-aligned: "
952 "start 0x%x length 0x%x",
958 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
962 // pick next address from our "mmap region"
964 p->pTable->allocate(start, length);
965 p->mmap_end += length;
967 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
968 warn("allowing mmap of file @ fd %d. "
969 "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
975 /// Target getrlimit() handler.
978 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
981 unsigned resource = process->getSyscallArg(tc, 0);
982 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
985 case OS::TGT_RLIMIT_STACK:
986 // max stack size in bytes: make up a number (8MB for now)
987 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
988 rlp->rlim_cur = htog(rlp->rlim_cur);
989 rlp->rlim_max = htog(rlp->rlim_max);
992 case OS::TGT_RLIMIT_DATA:
993 // max data segment size in bytes: make up a number
994 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
995 rlp->rlim_cur = htog(rlp->rlim_cur);
996 rlp->rlim_max = htog(rlp->rlim_max);
1000 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1006 rlp.copyOut(tc->getMemPort());
1010 /// Target gettimeofday() handler.
1013 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1016 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1018 getElapsedTime(tp->tv_sec, tp->tv_usec);
1019 tp->tv_sec += seconds_since_epoch;
1020 tp->tv_sec = TheISA::htog(tp->tv_sec);
1021 tp->tv_usec = TheISA::htog(tp->tv_usec);
1023 tp.copyOut(tc->getMemPort());
1029 /// Target utimes() handler.
1032 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1037 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
1040 TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
1041 tp.copyIn(tc->getMemPort());
1043 struct timeval hostTimeval[2];
1044 for (int i = 0; i < 2; ++i)
1046 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1047 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1050 // Adjust path for current working directory
1051 path = process->fullPath(path);
1053 int result = utimes(path.c_str(), hostTimeval);
1060 /// Target getrusage() function.
1063 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1066 int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN
1067 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1069 rup->ru_utime.tv_sec = 0;
1070 rup->ru_utime.tv_usec = 0;
1071 rup->ru_stime.tv_sec = 0;
1072 rup->ru_stime.tv_usec = 0;
1080 rup->ru_inblock = 0;
1081 rup->ru_oublock = 0;
1084 rup->ru_nsignals = 0;
1089 case OS::TGT_RUSAGE_SELF:
1090 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1091 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1092 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1095 case OS::TGT_RUSAGE_CHILDREN:
1096 // do nothing. We have no child processes, so they take no time.
1100 // don't really handle THREAD or CHILDREN, but just warn and
1102 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1106 rup.copyOut(tc->getMemPort());
1114 #endif // __SIM_SYSCALL_EMUL_HH__