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 "mem/page_table.hh"
64 #include "mem/translating_port.hh"
65 #include "sim/byteswap.hh"
66 #include "sim/process.hh"
67 #include "sim/system.hh"
70 /// System call descriptor.
76 /// Typedef for target syscall handler functions.
77 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
78 LiveProcess *, ThreadContext *);
80 const char *name; //!< Syscall name (e.g., "open").
81 FuncPtr funcPtr; //!< Pointer to emulation function.
82 int flags; //!< Flags (see Flags enum).
84 /// Flag values for controlling syscall behavior.
86 /// Don't set return regs according to funcPtr return value.
87 /// Used for syscalls with non-standard return conventions
88 /// that explicitly set the ThreadContext regs (e.g.,
90 SuppressReturnValue = 1
94 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
95 : name(_name), funcPtr(_funcPtr), flags(_flags)
99 /// Emulate the syscall. Public interface for calling through funcPtr.
100 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
104 class BaseBufferArg {
108 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
110 bufPtr = new uint8_t[size];
111 // clear out buffer: in case we only partially populate this,
112 // and then do a copyOut(), we want to make sure we don't
113 // introduce any random junk into the simulated address space
114 memset(bufPtr, 0, size);
117 virtual ~BaseBufferArg() { delete [] bufPtr; }
120 // copy data into simulator space (read from target memory)
122 virtual bool copyIn(TranslatingPort *memport)
124 memport->readBlob(addr, bufPtr, size);
125 return true; // no EFAULT detection for now
129 // copy data out of simulator space (write to target memory)
131 virtual bool copyOut(TranslatingPort *memport)
133 memport->writeBlob(addr, bufPtr, size);
134 return true; // no EFAULT detection for now
144 class BufferArg : public BaseBufferArg
147 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
148 void *bufferPtr() { return bufPtr; }
152 class TypedBufferArg : public BaseBufferArg
155 // user can optionally specify a specific number of bytes to
156 // allocate to deal with those structs that have variable-size
158 TypedBufferArg(Addr _addr, int _size = sizeof(T))
159 : BaseBufferArg(_addr, _size)
163 operator T*() { return (T *)bufPtr; }
165 // dereference operators
166 T &operator*() { return *((T *)bufPtr); }
167 T* operator->() { return (T *)bufPtr; }
168 T &operator[](int i) { return ((T *)bufPtr)[i]; }
171 //////////////////////////////////////////////////////////////////////
173 // The following emulation functions are generic enough that they
174 // don't need to be recompiled for different emulated OS's. They are
175 // defined in sim/syscall_emul.cc.
177 //////////////////////////////////////////////////////////////////////
180 /// Handler for unimplemented syscalls that we haven't thought about.
181 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
182 LiveProcess *p, ThreadContext *tc);
184 /// Handler for unimplemented syscalls that we never intend to
185 /// implement (signal handling, etc.) and should not affect the correct
186 /// behavior of the program. Print a warning only if the appropriate
187 /// trace flag is enabled. Return success to the target program.
188 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
189 LiveProcess *p, ThreadContext *tc);
190 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
191 LiveProcess *p, ThreadContext *tc);
193 /// Target exit() handler: terminate current context.
194 SyscallReturn exitFunc(SyscallDesc *desc, int num,
195 LiveProcess *p, ThreadContext *tc);
197 /// Target exit_group() handler: terminate simulation. (exit all threads)
198 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
199 LiveProcess *p, ThreadContext *tc);
201 /// Target getpagesize() handler.
202 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
203 LiveProcess *p, ThreadContext *tc);
205 /// Target brk() handler: set brk address.
206 SyscallReturn brkFunc(SyscallDesc *desc, int num,
207 LiveProcess *p, ThreadContext *tc);
209 /// Target close() handler.
210 SyscallReturn closeFunc(SyscallDesc *desc, int num,
211 LiveProcess *p, ThreadContext *tc);
213 /// Target read() handler.
214 SyscallReturn readFunc(SyscallDesc *desc, int num,
215 LiveProcess *p, ThreadContext *tc);
217 /// Target write() handler.
218 SyscallReturn writeFunc(SyscallDesc *desc, int num,
219 LiveProcess *p, ThreadContext *tc);
221 /// Target lseek() handler.
222 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
223 LiveProcess *p, ThreadContext *tc);
225 /// Target _llseek() handler.
226 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
227 LiveProcess *p, ThreadContext *tc);
229 /// Target munmap() handler.
230 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
231 LiveProcess *p, ThreadContext *tc);
233 /// Target gethostname() handler.
234 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
235 LiveProcess *p, ThreadContext *tc);
237 /// Target getcwd() handler.
238 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
239 LiveProcess *p, ThreadContext *tc);
241 /// Target unlink() handler.
242 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
243 LiveProcess *p, ThreadContext *tc);
245 /// Target unlink() handler.
246 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
247 LiveProcess *p, ThreadContext *tc);
249 /// Target mkdir() handler.
250 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
251 LiveProcess *p, ThreadContext *tc);
253 /// Target rename() handler.
254 SyscallReturn renameFunc(SyscallDesc *desc, int num,
255 LiveProcess *p, ThreadContext *tc);
258 /// Target truncate() handler.
259 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
260 LiveProcess *p, ThreadContext *tc);
263 /// Target ftruncate() handler.
264 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
265 LiveProcess *p, ThreadContext *tc);
268 /// Target truncate64() handler.
269 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
270 LiveProcess *p, ThreadContext *tc);
272 /// Target ftruncate64() handler.
273 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
274 LiveProcess *p, ThreadContext *tc);
277 /// Target umask() handler.
278 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
279 LiveProcess *p, ThreadContext *tc);
282 /// Target chown() handler.
283 SyscallReturn chownFunc(SyscallDesc *desc, int num,
284 LiveProcess *p, ThreadContext *tc);
287 /// Target fchown() handler.
288 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
289 LiveProcess *p, ThreadContext *tc);
291 /// Target dup() handler.
292 SyscallReturn dupFunc(SyscallDesc *desc, int num,
293 LiveProcess *process, ThreadContext *tc);
295 /// Target fnctl() handler.
296 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
297 LiveProcess *process, ThreadContext *tc);
299 /// Target fcntl64() handler.
300 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
301 LiveProcess *process, ThreadContext *tc);
303 /// Target setuid() handler.
304 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
305 LiveProcess *p, ThreadContext *tc);
307 /// Target getpid() handler.
308 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
309 LiveProcess *p, ThreadContext *tc);
311 /// Target getuid() handler.
312 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
313 LiveProcess *p, ThreadContext *tc);
315 /// Target getgid() handler.
316 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
317 LiveProcess *p, ThreadContext *tc);
319 /// Target getppid() handler.
320 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
321 LiveProcess *p, ThreadContext *tc);
323 /// Target geteuid() handler.
324 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
325 LiveProcess *p, ThreadContext *tc);
327 /// Target getegid() handler.
328 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
329 LiveProcess *p, ThreadContext *tc);
331 /// Target clone() handler.
332 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
333 LiveProcess *p, ThreadContext *tc);
336 /// Pseudo Funcs - These functions use a different return convension,
337 /// returning a second value in a register other than the normal return register
338 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
339 LiveProcess *process, ThreadContext *tc);
341 /// Target getpidPseudo() handler.
342 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
343 LiveProcess *p, ThreadContext *tc);
345 /// Target getuidPseudo() handler.
346 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
347 LiveProcess *p, ThreadContext *tc);
349 /// Target getgidPseudo() handler.
350 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
351 LiveProcess *p, ThreadContext *tc);
354 /// A readable name for 1,000,000, for converting microseconds to seconds.
355 const int one_million = 1000000;
357 /// Approximate seconds since the epoch (1/1/1970). About a billion,
358 /// by my reckoning. We want to keep this a constant (not use the
359 /// real-world time) to keep simulations repeatable.
360 const unsigned seconds_since_epoch = 1000000000;
362 /// Helper function to convert current elapsed time to seconds and
364 template <class T1, class T2>
366 getElapsedTime(T1 &sec, T2 &usec)
368 int elapsed_usecs = curTick() / SimClock::Int::us;
369 sec = elapsed_usecs / one_million;
370 usec = elapsed_usecs % one_million;
373 //////////////////////////////////////////////////////////////////////
375 // The following emulation functions are generic, but need to be
376 // templated to account for differences in types, constants, etc.
378 //////////////////////////////////////////////////////////////////////
381 typedef struct stat hst_stat;
382 typedef struct stat hst_stat64;
384 typedef struct stat hst_stat;
385 typedef struct stat64 hst_stat64;
388 //// Helper function to convert a host stat buffer to a target stat
389 //// buffer. Also copies the target buffer out to the simulated
390 //// memory space. Used by stat(), fstat(), and lstat().
392 template <typename target_stat, typename host_stat>
394 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
396 using namespace TheISA;
401 tgt->st_dev = host->st_dev;
402 tgt->st_dev = htog(tgt->st_dev);
403 tgt->st_ino = host->st_ino;
404 tgt->st_ino = htog(tgt->st_ino);
405 tgt->st_mode = host->st_mode;
407 // Claim to be a character device
408 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
409 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
411 tgt->st_mode = htog(tgt->st_mode);
412 tgt->st_nlink = host->st_nlink;
413 tgt->st_nlink = htog(tgt->st_nlink);
414 tgt->st_uid = host->st_uid;
415 tgt->st_uid = htog(tgt->st_uid);
416 tgt->st_gid = host->st_gid;
417 tgt->st_gid = htog(tgt->st_gid);
419 tgt->st_rdev = 0x880d;
421 tgt->st_rdev = host->st_rdev;
422 tgt->st_rdev = htog(tgt->st_rdev);
423 tgt->st_size = host->st_size;
424 tgt->st_size = htog(tgt->st_size);
425 tgt->st_atimeX = host->st_atime;
426 tgt->st_atimeX = htog(tgt->st_atimeX);
427 tgt->st_mtimeX = host->st_mtime;
428 tgt->st_mtimeX = htog(tgt->st_mtimeX);
429 tgt->st_ctimeX = host->st_ctime;
430 tgt->st_ctimeX = htog(tgt->st_ctimeX);
431 // Force the block size to be 8k. This helps to ensure buffered io works
432 // consistently across different hosts.
433 tgt->st_blksize = 0x2000;
434 tgt->st_blksize = htog(tgt->st_blksize);
435 tgt->st_blocks = host->st_blocks;
436 tgt->st_blocks = htog(tgt->st_blocks);
441 template <typename target_stat, typename host_stat64>
443 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
445 using namespace TheISA;
447 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
448 #if defined(STAT_HAVE_NSEC)
449 tgt->st_atime_nsec = host->st_atime_nsec;
450 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
451 tgt->st_mtime_nsec = host->st_mtime_nsec;
452 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
453 tgt->st_ctime_nsec = host->st_ctime_nsec;
454 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
456 tgt->st_atime_nsec = 0;
457 tgt->st_mtime_nsec = 0;
458 tgt->st_ctime_nsec = 0;
462 //Here are a couple convenience functions
465 copyOutStatBuf(TranslatingPort * mem, Addr addr,
466 hst_stat *host, bool fakeTTY = false)
468 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
469 tgt_stat_buf tgt(addr);
470 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
476 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
477 hst_stat64 *host, bool fakeTTY = false)
479 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
480 tgt_stat_buf tgt(addr);
481 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
485 /// Target ioctl() handler. For the most part, programs call ioctl()
486 /// only to find out if their stdout is a tty, to determine whether to
487 /// do line or block buffering.
490 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
494 int fd = process->getSyscallArg(tc, index);
495 unsigned req = process->getSyscallArg(tc, index);
497 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
499 if (fd < 0 || process->sim_fd(fd) < 0) {
500 // doesn't map to any simulator fd: not a valid target fd
505 case OS::TIOCISATTY_:
517 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
518 fd, req, tc->pcState());
522 /// Target open() handler.
525 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
531 if (!tc->getMemPort()->tryReadString(path,
532 process->getSyscallArg(tc, index)))
535 if (path == "/dev/sysdev0") {
536 // This is a memory-mapped high-resolution timer device on Alpha.
537 // We don't support it, so just punt.
538 warn("Ignoring open(%s, ...)\n", path);
542 int tgtFlags = process->getSyscallArg(tc, index);
543 int mode = process->getSyscallArg(tc, index);
546 // translate open flags
547 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
548 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
549 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
550 hostFlags |= OS::openFlagTable[i].hostFlag;
554 // any target flags left?
556 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
559 hostFlags |= O_BINARY;
562 // Adjust path for current working directory
563 path = process->fullPath(path);
565 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
568 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
569 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
570 // It's a proc/sys entery and requires special handling
571 fd = OS::openSpecialFile(path, process, tc);
572 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
575 fd = open(path.c_str(), hostFlags, mode);
576 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
581 /// Target sysinfo() handler.
584 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
589 TypedBufferArg<typename OS::tgt_sysinfo>
590 sysinfo(process->getSyscallArg(tc, index));
592 sysinfo->uptime=seconds_since_epoch;
593 sysinfo->totalram=process->system->memSize();
595 sysinfo.copyOut(tc->getMemPort());
600 /// Target chmod() handler.
603 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
609 if (!tc->getMemPort()->tryReadString(path,
610 process->getSyscallArg(tc, index))) {
614 uint32_t mode = process->getSyscallArg(tc, index);
617 // XXX translate mode flags via OS::something???
620 // Adjust path for current working directory
621 path = process->fullPath(path);
624 int result = chmod(path.c_str(), hostMode);
632 /// Target fchmod() handler.
635 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
639 int fd = process->getSyscallArg(tc, index);
640 if (fd < 0 || process->sim_fd(fd) < 0) {
641 // doesn't map to any simulator fd: not a valid target fd
645 uint32_t mode = process->getSyscallArg(tc, index);
648 // XXX translate mode flags via OS::someting???
652 int result = fchmod(process->sim_fd(fd), hostMode);
659 /// Target mremap() handler.
662 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
665 Addr start = process->getSyscallArg(tc, index);
666 uint64_t old_length = process->getSyscallArg(tc, index);
667 uint64_t new_length = process->getSyscallArg(tc, index);
668 uint64_t flags = process->getSyscallArg(tc, index);
670 if ((start % TheISA::VMPageSize != 0) ||
671 (new_length % TheISA::VMPageSize != 0)) {
672 warn("mremap failing: arguments not page aligned");
676 if (new_length > old_length) {
677 if ((start + old_length) == process->mmap_end) {
678 uint64_t diff = new_length - old_length;
679 process->pTable->allocate(process->mmap_end, diff);
680 process->mmap_end += diff;
683 // sys/mman.h defined MREMAP_MAYMOVE
685 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
688 process->pTable->remap(start, old_length, process->mmap_end);
689 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
690 process->mmap_end, process->mmap_end + new_length, new_length);
691 start = process->mmap_end;
692 // add on the remaining unallocated pages
693 process->pTable->allocate(start + old_length, new_length - old_length);
694 process->mmap_end += new_length;
695 warn("returning %08p as start\n", start);
700 process->pTable->deallocate(start + new_length, old_length -
706 /// Target stat() handler.
709 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
715 if (!tc->getMemPort()->tryReadString(path,
716 process->getSyscallArg(tc, index))) {
719 Addr bufPtr = process->getSyscallArg(tc, index);
721 // Adjust path for current working directory
722 path = process->fullPath(path);
725 int result = stat(path.c_str(), &hostBuf);
730 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
736 /// Target stat64() handler.
739 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
745 if (!tc->getMemPort()->tryReadString(path,
746 process->getSyscallArg(tc, index)))
748 Addr bufPtr = process->getSyscallArg(tc, index);
750 // Adjust path for current working directory
751 path = process->fullPath(path);
755 int result = stat(path.c_str(), &hostBuf);
757 struct stat64 hostBuf;
758 int result = stat64(path.c_str(), &hostBuf);
764 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
770 /// Target fstat64() handler.
773 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
777 int fd = process->getSyscallArg(tc, index);
778 Addr bufPtr = process->getSyscallArg(tc, index);
779 if (fd < 0 || process->sim_fd(fd) < 0) {
780 // doesn't map to any simulator fd: not a valid target fd
786 int result = fstat(process->sim_fd(fd), &hostBuf);
788 struct stat64 hostBuf;
789 int result = fstat64(process->sim_fd(fd), &hostBuf);
795 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
801 /// Target lstat() handler.
804 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
810 if (!tc->getMemPort()->tryReadString(path,
811 process->getSyscallArg(tc, index))) {
814 Addr bufPtr = process->getSyscallArg(tc, index);
816 // Adjust path for current working directory
817 path = process->fullPath(path);
820 int result = lstat(path.c_str(), &hostBuf);
825 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
830 /// Target lstat64() handler.
833 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
839 if (!tc->getMemPort()->tryReadString(path,
840 process->getSyscallArg(tc, index))) {
843 Addr bufPtr = process->getSyscallArg(tc, index);
845 // Adjust path for current working directory
846 path = process->fullPath(path);
850 int result = lstat(path.c_str(), &hostBuf);
852 struct stat64 hostBuf;
853 int result = lstat64(path.c_str(), &hostBuf);
859 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
864 /// Target fstat() handler.
867 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
871 int fd = process->sim_fd(process->getSyscallArg(tc, index));
872 Addr bufPtr = process->getSyscallArg(tc, index);
874 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
880 int result = fstat(fd, &hostBuf);
885 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
891 /// Target statfs() handler.
894 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
900 if (!tc->getMemPort()->tryReadString(path,
901 process->getSyscallArg(tc, index))) {
904 Addr bufPtr = process->getSyscallArg(tc, index);
906 // Adjust path for current working directory
907 path = process->fullPath(path);
909 struct statfs hostBuf;
910 int result = statfs(path.c_str(), &hostBuf);
915 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
921 /// Target fstatfs() handler.
924 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
928 int fd = process->sim_fd(process->getSyscallArg(tc, index));
929 Addr bufPtr = process->getSyscallArg(tc, index);
934 struct statfs hostBuf;
935 int result = fstatfs(fd, &hostBuf);
940 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
946 /// Target writev() handler.
949 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
953 int fd = process->getSyscallArg(tc, index);
954 if (fd < 0 || process->sim_fd(fd) < 0) {
955 // doesn't map to any simulator fd: not a valid target fd
959 TranslatingPort *p = tc->getMemPort();
960 uint64_t tiov_base = process->getSyscallArg(tc, index);
961 size_t count = process->getSyscallArg(tc, index);
962 struct iovec hiov[count];
963 for (size_t i = 0; i < count; ++i) {
964 typename OS::tgt_iovec tiov;
966 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
967 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
968 hiov[i].iov_len = gtoh(tiov.iov_len);
969 hiov[i].iov_base = new char [hiov[i].iov_len];
970 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
974 int result = writev(process->sim_fd(fd), hiov, count);
976 for (size_t i = 0; i < count; ++i)
977 delete [] (char *)hiov[i].iov_base;
986 /// Target mmap() handler.
988 /// We don't really handle mmap(). If the target is mmaping an
989 /// anonymous region or /dev/zero, we can get away with doing basically
990 /// nothing (since memory is initialized to zero and the simulator
991 /// doesn't really check addresses anyway). Always print a warning,
992 /// since this could be seriously broken if we're not mapping
995 /// Someday we should explicitly check for /dev/zero in open, flag the
996 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
1000 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1003 Addr start = p->getSyscallArg(tc, index);
1004 uint64_t length = p->getSyscallArg(tc, index);
1005 index++; // int prot = p->getSyscallArg(tc, index);
1006 int flags = p->getSyscallArg(tc, index);
1007 int fd = p->sim_fd(p->getSyscallArg(tc, index));
1008 // int offset = p->getSyscallArg(tc, index);
1011 if ((start % TheISA::VMPageSize) != 0 ||
1012 (length % TheISA::VMPageSize) != 0) {
1013 warn("mmap failing: arguments not page-aligned: "
1014 "start 0x%x length 0x%x",
1020 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
1021 start, p->mmap_end);
1024 // pick next address from our "mmap region"
1025 if (OS::mmapGrowsDown()) {
1026 start = p->mmap_end - length;
1027 p->mmap_end = start;
1029 start = p->mmap_end;
1030 p->mmap_end += length;
1032 p->pTable->allocate(start, length);
1034 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1035 warn("allowing mmap of file @ fd %d. "
1036 "This will break if not /dev/zero.", fd);
1042 /// Target getrlimit() handler.
1045 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1049 unsigned resource = process->getSyscallArg(tc, index);
1050 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1053 case OS::TGT_RLIMIT_STACK:
1054 // max stack size in bytes: make up a number (8MB for now)
1055 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1056 rlp->rlim_cur = htog(rlp->rlim_cur);
1057 rlp->rlim_max = htog(rlp->rlim_max);
1060 case OS::TGT_RLIMIT_DATA:
1061 // max data segment size in bytes: make up a number
1062 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1063 rlp->rlim_cur = htog(rlp->rlim_cur);
1064 rlp->rlim_max = htog(rlp->rlim_max);
1068 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1074 rlp.copyOut(tc->getMemPort());
1078 /// Target gettimeofday() handler.
1081 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1085 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1087 getElapsedTime(tp->tv_sec, tp->tv_usec);
1088 tp->tv_sec += seconds_since_epoch;
1089 tp->tv_sec = TheISA::htog(tp->tv_sec);
1090 tp->tv_usec = TheISA::htog(tp->tv_usec);
1092 tp.copyOut(tc->getMemPort());
1098 /// Target utimes() handler.
1101 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1107 if (!tc->getMemPort()->tryReadString(path,
1108 process->getSyscallArg(tc, index))) {
1112 TypedBufferArg<typename OS::timeval [2]>
1113 tp(process->getSyscallArg(tc, index));
1114 tp.copyIn(tc->getMemPort());
1116 struct timeval hostTimeval[2];
1117 for (int i = 0; i < 2; ++i)
1119 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1120 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1123 // Adjust path for current working directory
1124 path = process->fullPath(path);
1126 int result = utimes(path.c_str(), hostTimeval);
1133 /// Target getrusage() function.
1136 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1140 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1141 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1143 rup->ru_utime.tv_sec = 0;
1144 rup->ru_utime.tv_usec = 0;
1145 rup->ru_stime.tv_sec = 0;
1146 rup->ru_stime.tv_usec = 0;
1154 rup->ru_inblock = 0;
1155 rup->ru_oublock = 0;
1158 rup->ru_nsignals = 0;
1163 case OS::TGT_RUSAGE_SELF:
1164 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1165 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1166 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1169 case OS::TGT_RUSAGE_CHILDREN:
1170 // do nothing. We have no child processes, so they take no time.
1174 // don't really handle THREAD or CHILDREN, but just warn and
1176 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1180 rup.copyOut(tc->getMemPort());
1185 /// Target times() function.
1188 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1192 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1194 // Fill in the time structure (in clocks)
1195 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1196 bufp->tms_utime = clocks;
1197 bufp->tms_stime = 0;
1198 bufp->tms_cutime = 0;
1199 bufp->tms_cstime = 0;
1201 // Convert to host endianness
1202 bufp->tms_utime = htog(bufp->tms_utime);
1205 bufp.copyOut(tc->getMemPort());
1207 // Return clock ticks since system boot
1211 /// Target time() function.
1214 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1217 typename OS::time_t sec, usec;
1218 getElapsedTime(sec, usec);
1219 sec += seconds_since_epoch;
1222 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1224 typename OS::time_t t = sec;
1226 TranslatingPort *p = tc->getMemPort();
1227 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1233 #endif // __SIM_SYSCALL_EMUL_HH__