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 "config/the_isa.hh"
59 #include "cpu/base.hh"
60 #include "cpu/thread_context.hh"
61 #include "mem/translating_port.hh"
62 #include "mem/page_table.hh"
63 #include "sim/system.hh"
64 #include "sim/process.hh"
67 /// System call descriptor.
73 /// Typedef for target syscall handler functions.
74 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
75 LiveProcess *, ThreadContext *);
77 const char *name; //!< Syscall name (e.g., "open").
78 FuncPtr funcPtr; //!< Pointer to emulation function.
79 int flags; //!< Flags (see Flags enum).
81 /// Flag values for controlling syscall behavior.
83 /// Don't set return regs according to funcPtr return value.
84 /// Used for syscalls with non-standard return conventions
85 /// that explicitly set the ThreadContext regs (e.g.,
87 SuppressReturnValue = 1
91 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
92 : name(_name), funcPtr(_funcPtr), flags(_flags)
96 /// Emulate the syscall. Public interface for calling through funcPtr.
97 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
101 class BaseBufferArg {
105 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
107 bufPtr = new uint8_t[size];
108 // clear out buffer: in case we only partially populate this,
109 // and then do a copyOut(), we want to make sure we don't
110 // introduce any random junk into the simulated address space
111 memset(bufPtr, 0, size);
114 virtual ~BaseBufferArg() { delete [] bufPtr; }
117 // copy data into simulator space (read from target memory)
119 virtual bool copyIn(TranslatingPort *memport)
121 memport->readBlob(addr, bufPtr, size);
122 return true; // no EFAULT detection for now
126 // copy data out of simulator space (write to target memory)
128 virtual bool copyOut(TranslatingPort *memport)
130 memport->writeBlob(addr, bufPtr, size);
131 return true; // no EFAULT detection for now
141 class BufferArg : public BaseBufferArg
144 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
145 void *bufferPtr() { return bufPtr; }
149 class TypedBufferArg : public BaseBufferArg
152 // user can optionally specify a specific number of bytes to
153 // allocate to deal with those structs that have variable-size
155 TypedBufferArg(Addr _addr, int _size = sizeof(T))
156 : BaseBufferArg(_addr, _size)
160 operator T*() { return (T *)bufPtr; }
162 // dereference operators
163 T &operator*() { return *((T *)bufPtr); }
164 T* operator->() { return (T *)bufPtr; }
165 T &operator[](int i) { return ((T *)bufPtr)[i]; }
168 //////////////////////////////////////////////////////////////////////
170 // The following emulation functions are generic enough that they
171 // don't need to be recompiled for different emulated OS's. They are
172 // defined in sim/syscall_emul.cc.
174 //////////////////////////////////////////////////////////////////////
177 /// Handler for unimplemented syscalls that we haven't thought about.
178 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
179 LiveProcess *p, ThreadContext *tc);
181 /// Handler for unimplemented syscalls that we never intend to
182 /// implement (signal handling, etc.) and should not affect the correct
183 /// behavior of the program. Print a warning only if the appropriate
184 /// trace flag is enabled. Return success to the target program.
185 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
186 LiveProcess *p, ThreadContext *tc);
188 /// Target exit() handler: terminate current context.
189 SyscallReturn exitFunc(SyscallDesc *desc, int num,
190 LiveProcess *p, ThreadContext *tc);
192 /// Target exit_group() handler: terminate simulation. (exit all threads)
193 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
194 LiveProcess *p, ThreadContext *tc);
196 /// Target getpagesize() handler.
197 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
198 LiveProcess *p, ThreadContext *tc);
200 /// Target brk() handler: set brk address.
201 SyscallReturn brkFunc(SyscallDesc *desc, int num,
202 LiveProcess *p, ThreadContext *tc);
204 /// Target close() handler.
205 SyscallReturn closeFunc(SyscallDesc *desc, int num,
206 LiveProcess *p, ThreadContext *tc);
208 /// Target read() handler.
209 SyscallReturn readFunc(SyscallDesc *desc, int num,
210 LiveProcess *p, ThreadContext *tc);
212 /// Target write() handler.
213 SyscallReturn writeFunc(SyscallDesc *desc, int num,
214 LiveProcess *p, ThreadContext *tc);
216 /// Target lseek() handler.
217 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
218 LiveProcess *p, ThreadContext *tc);
220 /// Target _llseek() handler.
221 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
222 LiveProcess *p, ThreadContext *tc);
224 /// Target munmap() handler.
225 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
226 LiveProcess *p, ThreadContext *tc);
228 /// Target gethostname() handler.
229 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
230 LiveProcess *p, ThreadContext *tc);
232 /// Target getcwd() handler.
233 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
234 LiveProcess *p, ThreadContext *tc);
236 /// Target unlink() handler.
237 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
238 LiveProcess *p, ThreadContext *tc);
240 /// Target unlink() handler.
241 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
242 LiveProcess *p, ThreadContext *tc);
244 /// Target mkdir() handler.
245 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
246 LiveProcess *p, ThreadContext *tc);
248 /// Target rename() handler.
249 SyscallReturn renameFunc(SyscallDesc *desc, int num,
250 LiveProcess *p, ThreadContext *tc);
253 /// Target truncate() handler.
254 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
255 LiveProcess *p, ThreadContext *tc);
258 /// Target ftruncate() handler.
259 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
260 LiveProcess *p, ThreadContext *tc);
263 /// Target ftruncate64() handler.
264 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
265 LiveProcess *p, ThreadContext *tc);
268 /// Target umask() handler.
269 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
270 LiveProcess *p, ThreadContext *tc);
273 /// Target chown() handler.
274 SyscallReturn chownFunc(SyscallDesc *desc, int num,
275 LiveProcess *p, ThreadContext *tc);
278 /// Target fchown() handler.
279 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
280 LiveProcess *p, ThreadContext *tc);
282 /// Target dup() handler.
283 SyscallReturn dupFunc(SyscallDesc *desc, int num,
284 LiveProcess *process, ThreadContext *tc);
286 /// Target fnctl() handler.
287 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
288 LiveProcess *process, ThreadContext *tc);
290 /// Target fcntl64() handler.
291 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
292 LiveProcess *process, ThreadContext *tc);
294 /// Target setuid() handler.
295 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
296 LiveProcess *p, ThreadContext *tc);
298 /// Target getpid() handler.
299 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
300 LiveProcess *p, ThreadContext *tc);
302 /// Target getuid() handler.
303 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
304 LiveProcess *p, ThreadContext *tc);
306 /// Target getgid() handler.
307 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
308 LiveProcess *p, ThreadContext *tc);
310 /// Target getppid() handler.
311 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
312 LiveProcess *p, ThreadContext *tc);
314 /// Target geteuid() handler.
315 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
316 LiveProcess *p, ThreadContext *tc);
318 /// Target getegid() handler.
319 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
320 LiveProcess *p, ThreadContext *tc);
322 /// Target clone() handler.
323 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
324 LiveProcess *p, ThreadContext *tc);
327 /// Pseudo Funcs - These functions use a different return convension,
328 /// returning a second value in a register other than the normal return register
329 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
330 LiveProcess *process, ThreadContext *tc);
332 /// Target getpidPseudo() handler.
333 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
334 LiveProcess *p, ThreadContext *tc);
336 /// Target getuidPseudo() handler.
337 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
338 LiveProcess *p, ThreadContext *tc);
340 /// Target getgidPseudo() handler.
341 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
342 LiveProcess *p, ThreadContext *tc);
345 /// A readable name for 1,000,000, for converting microseconds to seconds.
346 const int one_million = 1000000;
348 /// Approximate seconds since the epoch (1/1/1970). About a billion,
349 /// by my reckoning. We want to keep this a constant (not use the
350 /// real-world time) to keep simulations repeatable.
351 const unsigned seconds_since_epoch = 1000000000;
353 /// Helper function to convert current elapsed time to seconds and
355 template <class T1, class T2>
357 getElapsedTime(T1 &sec, T2 &usec)
359 int elapsed_usecs = curTick / Clock::Int::us;
360 sec = elapsed_usecs / one_million;
361 usec = elapsed_usecs % one_million;
364 //////////////////////////////////////////////////////////////////////
366 // The following emulation functions are generic, but need to be
367 // templated to account for differences in types, constants, etc.
369 //////////////////////////////////////////////////////////////////////
372 typedef struct stat hst_stat;
373 typedef struct stat hst_stat64;
375 typedef struct stat hst_stat;
376 typedef struct stat64 hst_stat64;
379 //// Helper function to convert a host stat buffer to a target stat
380 //// buffer. Also copies the target buffer out to the simulated
381 //// memory space. Used by stat(), fstat(), and lstat().
383 template <typename target_stat, typename host_stat>
385 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
387 using namespace TheISA;
392 tgt->st_dev = host->st_dev;
393 tgt->st_dev = htog(tgt->st_dev);
394 tgt->st_ino = host->st_ino;
395 tgt->st_ino = htog(tgt->st_ino);
396 tgt->st_mode = host->st_mode;
398 // Claim to be a character device
399 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
400 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
402 tgt->st_mode = htog(tgt->st_mode);
403 tgt->st_nlink = host->st_nlink;
404 tgt->st_nlink = htog(tgt->st_nlink);
405 tgt->st_uid = host->st_uid;
406 tgt->st_uid = htog(tgt->st_uid);
407 tgt->st_gid = host->st_gid;
408 tgt->st_gid = htog(tgt->st_gid);
410 tgt->st_rdev = 0x880d;
412 tgt->st_rdev = host->st_rdev;
413 tgt->st_rdev = htog(tgt->st_rdev);
414 tgt->st_size = host->st_size;
415 tgt->st_size = htog(tgt->st_size);
416 tgt->st_atimeX = host->st_atime;
417 tgt->st_atimeX = htog(tgt->st_atimeX);
418 tgt->st_mtimeX = host->st_mtime;
419 tgt->st_mtimeX = htog(tgt->st_mtimeX);
420 tgt->st_ctimeX = host->st_ctime;
421 tgt->st_ctimeX = htog(tgt->st_ctimeX);
422 // Force the block size to be 8k. This helps to ensure buffered io works
423 // consistently across different hosts.
424 tgt->st_blksize = 0x2000;
425 tgt->st_blksize = htog(tgt->st_blksize);
426 tgt->st_blocks = host->st_blocks;
427 tgt->st_blocks = htog(tgt->st_blocks);
432 template <typename target_stat, typename host_stat64>
434 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
436 using namespace TheISA;
438 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
439 #if defined(STAT_HAVE_NSEC)
440 tgt->st_atime_nsec = host->st_atime_nsec;
441 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
442 tgt->st_mtime_nsec = host->st_mtime_nsec;
443 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
444 tgt->st_ctime_nsec = host->st_ctime_nsec;
445 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
447 tgt->st_atime_nsec = 0;
448 tgt->st_mtime_nsec = 0;
449 tgt->st_ctime_nsec = 0;
453 //Here are a couple convenience functions
456 copyOutStatBuf(TranslatingPort * mem, Addr addr,
457 hst_stat *host, bool fakeTTY = false)
459 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
460 tgt_stat_buf tgt(addr);
461 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
467 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
468 hst_stat64 *host, bool fakeTTY = false)
470 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
471 tgt_stat_buf tgt(addr);
472 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
476 /// Target ioctl() handler. For the most part, programs call ioctl()
477 /// only to find out if their stdout is a tty, to determine whether to
478 /// do line or block buffering.
481 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
484 int fd = process->getSyscallArg(tc, 0);
485 unsigned req = process->getSyscallArg(tc, 1);
487 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
489 if (fd < 0 || process->sim_fd(fd) < 0) {
490 // doesn't map to any simulator fd: not a valid target fd
495 case OS::TIOCISATTY_:
506 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
507 fd, req, tc->readPC());
511 /// Target open() handler.
514 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
519 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
522 if (path == "/dev/sysdev0") {
523 // This is a memory-mapped high-resolution timer device on Alpha.
524 // We don't support it, so just punt.
525 warn("Ignoring open(%s, ...)\n", path);
529 int tgtFlags = process->getSyscallArg(tc, 1);
530 int mode = process->getSyscallArg(tc, 2);
533 // translate open flags
534 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
535 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
536 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
537 hostFlags |= OS::openFlagTable[i].hostFlag;
541 // any target flags left?
543 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
546 hostFlags |= O_BINARY;
549 // Adjust path for current working directory
550 path = process->fullPath(path);
552 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
555 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
556 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
557 // It's a proc/sys entery and requires special handling
558 fd = OS::openSpecialFile(path, process, tc);
559 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
562 fd = open(path.c_str(), hostFlags, mode);
563 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
568 /// Target sysinfo() handler.
571 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
575 TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0));
577 sysinfo->uptime=seconds_since_epoch;
578 sysinfo->totalram=process->system->memSize();
580 sysinfo.copyOut(tc->getMemPort());
585 /// Target chmod() handler.
588 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
593 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
596 uint32_t mode = process->getSyscallArg(tc, 1);
599 // XXX translate mode flags via OS::something???
602 // Adjust path for current working directory
603 path = process->fullPath(path);
606 int result = chmod(path.c_str(), hostMode);
614 /// Target fchmod() handler.
617 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
620 int fd = process->getSyscallArg(tc, 0);
621 if (fd < 0 || process->sim_fd(fd) < 0) {
622 // doesn't map to any simulator fd: not a valid target fd
626 uint32_t mode = process->getSyscallArg(tc, 1);
629 // XXX translate mode flags via OS::someting???
633 int result = fchmod(process->sim_fd(fd), hostMode);
640 /// Target mremap() handler.
643 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
645 Addr start = process->getSyscallArg(tc, 0);
646 uint64_t old_length = process->getSyscallArg(tc, 1);
647 uint64_t new_length = process->getSyscallArg(tc, 2);
648 uint64_t flags = process->getSyscallArg(tc, 3);
650 if ((start % TheISA::VMPageSize != 0) ||
651 (new_length % TheISA::VMPageSize != 0)) {
652 warn("mremap failing: arguments not page aligned");
656 if (new_length > old_length) {
657 if ((start + old_length) == process->mmap_end) {
658 uint64_t diff = new_length - old_length;
659 process->pTable->allocate(process->mmap_end, diff);
660 process->mmap_end += diff;
663 // sys/mman.h defined MREMAP_MAYMOVE
665 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
668 process->pTable->remap(start, old_length, process->mmap_end);
669 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
670 process->mmap_end, process->mmap_end + new_length, new_length);
671 start = process->mmap_end;
672 // add on the remaining unallocated pages
673 process->pTable->allocate(start + old_length, new_length - old_length);
674 process->mmap_end += new_length;
675 warn("returning %08p as start\n", start);
680 process->pTable->deallocate(start + new_length, old_length -
686 /// Target stat() handler.
689 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
694 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
697 // Adjust path for current working directory
698 path = process->fullPath(path);
701 int result = stat(path.c_str(), &hostBuf);
706 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
713 /// Target stat64() handler.
716 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
721 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
724 // Adjust path for current working directory
725 path = process->fullPath(path);
729 int result = stat(path.c_str(), &hostBuf);
731 struct stat64 hostBuf;
732 int result = stat64(path.c_str(), &hostBuf);
738 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
745 /// Target fstat64() handler.
748 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
751 int fd = process->getSyscallArg(tc, 0);
752 if (fd < 0 || process->sim_fd(fd) < 0) {
753 // doesn't map to any simulator fd: not a valid target fd
759 int result = fstat(process->sim_fd(fd), &hostBuf);
761 struct stat64 hostBuf;
762 int result = fstat64(process->sim_fd(fd), &hostBuf);
768 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
769 &hostBuf, (fd == 1));
775 /// Target lstat() handler.
778 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
783 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
786 // Adjust path for current working directory
787 path = process->fullPath(path);
790 int result = lstat(path.c_str(), &hostBuf);
795 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
801 /// Target lstat64() handler.
804 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
809 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
812 // Adjust path for current working directory
813 path = process->fullPath(path);
817 int result = lstat(path.c_str(), &hostBuf);
819 struct stat64 hostBuf;
820 int result = lstat64(path.c_str(), &hostBuf);
826 copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
832 /// Target fstat() handler.
835 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
838 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
840 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
846 int result = fstat(fd, &hostBuf);
851 copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
852 &hostBuf, (fd == 1));
858 /// Target statfs() handler.
861 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
866 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
869 // Adjust path for current working directory
870 path = process->fullPath(path);
872 struct statfs hostBuf;
873 int result = statfs(path.c_str(), &hostBuf);
878 OS::copyOutStatfsBuf(tc->getMemPort(),
879 (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
885 /// Target fstatfs() handler.
888 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
891 int fd = process->sim_fd(process->getSyscallArg(tc, 0));
896 struct statfs hostBuf;
897 int result = fstatfs(fd, &hostBuf);
902 OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
909 /// Target writev() handler.
912 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
915 int fd = process->getSyscallArg(tc, 0);
916 if (fd < 0 || process->sim_fd(fd) < 0) {
917 // doesn't map to any simulator fd: not a valid target fd
921 TranslatingPort *p = tc->getMemPort();
922 uint64_t tiov_base = process->getSyscallArg(tc, 1);
923 size_t count = process->getSyscallArg(tc, 2);
924 struct iovec hiov[count];
925 for (size_t i = 0; i < count; ++i) {
926 typename OS::tgt_iovec tiov;
928 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
929 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
930 hiov[i].iov_len = gtoh(tiov.iov_len);
931 hiov[i].iov_base = new char [hiov[i].iov_len];
932 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
936 int result = writev(process->sim_fd(fd), hiov, count);
938 for (size_t i = 0; i < count; ++i)
939 delete [] (char *)hiov[i].iov_base;
948 /// Target mmap() handler.
950 /// We don't really handle mmap(). If the target is mmaping an
951 /// anonymous region or /dev/zero, we can get away with doing basically
952 /// nothing (since memory is initialized to zero and the simulator
953 /// doesn't really check addresses anyway). Always print a warning,
954 /// since this could be seriously broken if we're not mapping
957 /// Someday we should explicitly check for /dev/zero in open, flag the
958 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
962 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
964 Addr start = p->getSyscallArg(tc, 0);
965 uint64_t length = p->getSyscallArg(tc, 1);
966 // int prot = p->getSyscallArg(tc, 2);
967 int flags = p->getSyscallArg(tc, 3);
968 // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
969 // int offset = p->getSyscallArg(tc, 5);
972 if ((start % TheISA::VMPageSize) != 0 ||
973 (length % TheISA::VMPageSize) != 0) {
974 warn("mmap failing: arguments not page-aligned: "
975 "start 0x%x length 0x%x",
981 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
985 // pick next address from our "mmap region"
986 if (OS::mmapGrowsDown()) {
987 start = p->mmap_end - length;
991 p->mmap_end += length;
993 p->pTable->allocate(start, length);
995 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
996 warn("allowing mmap of file @ fd %d. "
997 "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
1003 /// Target getrlimit() handler.
1006 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1009 unsigned resource = process->getSyscallArg(tc, 0);
1010 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
1013 case OS::TGT_RLIMIT_STACK:
1014 // max stack size in bytes: make up a number (8MB for now)
1015 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1016 rlp->rlim_cur = htog(rlp->rlim_cur);
1017 rlp->rlim_max = htog(rlp->rlim_max);
1020 case OS::TGT_RLIMIT_DATA:
1021 // max data segment size in bytes: make up a number
1022 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1023 rlp->rlim_cur = htog(rlp->rlim_cur);
1024 rlp->rlim_max = htog(rlp->rlim_max);
1028 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1034 rlp.copyOut(tc->getMemPort());
1038 /// Target gettimeofday() handler.
1041 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1044 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1046 getElapsedTime(tp->tv_sec, tp->tv_usec);
1047 tp->tv_sec += seconds_since_epoch;
1048 tp->tv_sec = TheISA::htog(tp->tv_sec);
1049 tp->tv_usec = TheISA::htog(tp->tv_usec);
1051 tp.copyOut(tc->getMemPort());
1057 /// Target utimes() handler.
1060 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1065 if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
1068 TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
1069 tp.copyIn(tc->getMemPort());
1071 struct timeval hostTimeval[2];
1072 for (int i = 0; i < 2; ++i)
1074 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1075 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1078 // Adjust path for current working directory
1079 path = process->fullPath(path);
1081 int result = utimes(path.c_str(), hostTimeval);
1088 /// Target getrusage() function.
1091 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1094 int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN
1095 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1097 rup->ru_utime.tv_sec = 0;
1098 rup->ru_utime.tv_usec = 0;
1099 rup->ru_stime.tv_sec = 0;
1100 rup->ru_stime.tv_usec = 0;
1108 rup->ru_inblock = 0;
1109 rup->ru_oublock = 0;
1112 rup->ru_nsignals = 0;
1117 case OS::TGT_RUSAGE_SELF:
1118 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1119 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1120 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1123 case OS::TGT_RUSAGE_CHILDREN:
1124 // do nothing. We have no child processes, so they take no time.
1128 // don't really handle THREAD or CHILDREN, but just warn and
1130 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1134 rup.copyOut(tc->getMemPort());
1139 /// Target times() function.
1142 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1145 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, 0));
1147 // Fill in the time structure (in clocks)
1148 int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s;
1149 bufp->tms_utime = clocks;
1150 bufp->tms_stime = 0;
1151 bufp->tms_cutime = 0;
1152 bufp->tms_cstime = 0;
1154 // Convert to host endianness
1155 bufp->tms_utime = htog(bufp->tms_utime);
1158 bufp.copyOut(tc->getMemPort());
1160 // Return clock ticks since system boot
1164 /// Target time() function.
1167 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1170 typename OS::time_t sec, usec;
1171 getElapsedTime(sec, usec);
1172 sec += seconds_since_epoch;
1174 Addr taddr = (Addr)process->getSyscallArg(tc, 0);
1176 typename OS::time_t t = sec;
1178 TranslatingPort *p = tc->getMemPort();
1179 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1185 #endif // __SIM_SYSCALL_EMUL_HH__