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/translating_port.hh"
64 #include "mem/page_table.hh"
65 #include "sim/byteswap.hh"
66 #include "sim/system.hh"
67 #include "sim/process.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);
191 /// Target exit() handler: terminate current context.
192 SyscallReturn exitFunc(SyscallDesc *desc, int num,
193 LiveProcess *p, ThreadContext *tc);
195 /// Target exit_group() handler: terminate simulation. (exit all threads)
196 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
197 LiveProcess *p, ThreadContext *tc);
199 /// Target getpagesize() handler.
200 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
201 LiveProcess *p, ThreadContext *tc);
203 /// Target brk() handler: set brk address.
204 SyscallReturn brkFunc(SyscallDesc *desc, int num,
205 LiveProcess *p, ThreadContext *tc);
207 /// Target close() handler.
208 SyscallReturn closeFunc(SyscallDesc *desc, int num,
209 LiveProcess *p, ThreadContext *tc);
211 /// Target read() handler.
212 SyscallReturn readFunc(SyscallDesc *desc, int num,
213 LiveProcess *p, ThreadContext *tc);
215 /// Target write() handler.
216 SyscallReturn writeFunc(SyscallDesc *desc, int num,
217 LiveProcess *p, ThreadContext *tc);
219 /// Target lseek() handler.
220 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
221 LiveProcess *p, ThreadContext *tc);
223 /// Target _llseek() handler.
224 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
225 LiveProcess *p, ThreadContext *tc);
227 /// Target munmap() handler.
228 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
229 LiveProcess *p, ThreadContext *tc);
231 /// Target gethostname() handler.
232 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
233 LiveProcess *p, ThreadContext *tc);
235 /// Target getcwd() handler.
236 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
237 LiveProcess *p, ThreadContext *tc);
239 /// Target unlink() handler.
240 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
241 LiveProcess *p, ThreadContext *tc);
243 /// Target unlink() handler.
244 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
245 LiveProcess *p, ThreadContext *tc);
247 /// Target mkdir() handler.
248 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
249 LiveProcess *p, ThreadContext *tc);
251 /// Target rename() handler.
252 SyscallReturn renameFunc(SyscallDesc *desc, int num,
253 LiveProcess *p, ThreadContext *tc);
256 /// Target truncate() handler.
257 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
258 LiveProcess *p, ThreadContext *tc);
261 /// Target ftruncate() handler.
262 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
263 LiveProcess *p, ThreadContext *tc);
266 /// Target truncate64() handler.
267 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
268 LiveProcess *p, ThreadContext *tc);
270 /// Target ftruncate64() handler.
271 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
272 LiveProcess *p, ThreadContext *tc);
275 /// Target umask() handler.
276 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
277 LiveProcess *p, ThreadContext *tc);
280 /// Target chown() handler.
281 SyscallReturn chownFunc(SyscallDesc *desc, int num,
282 LiveProcess *p, ThreadContext *tc);
285 /// Target fchown() handler.
286 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
287 LiveProcess *p, ThreadContext *tc);
289 /// Target dup() handler.
290 SyscallReturn dupFunc(SyscallDesc *desc, int num,
291 LiveProcess *process, ThreadContext *tc);
293 /// Target fnctl() handler.
294 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
295 LiveProcess *process, ThreadContext *tc);
297 /// Target fcntl64() handler.
298 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
299 LiveProcess *process, ThreadContext *tc);
301 /// Target setuid() handler.
302 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
303 LiveProcess *p, ThreadContext *tc);
305 /// Target getpid() handler.
306 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
307 LiveProcess *p, ThreadContext *tc);
309 /// Target getuid() handler.
310 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
311 LiveProcess *p, ThreadContext *tc);
313 /// Target getgid() handler.
314 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
315 LiveProcess *p, ThreadContext *tc);
317 /// Target getppid() handler.
318 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
319 LiveProcess *p, ThreadContext *tc);
321 /// Target geteuid() handler.
322 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
323 LiveProcess *p, ThreadContext *tc);
325 /// Target getegid() handler.
326 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
327 LiveProcess *p, ThreadContext *tc);
329 /// Target clone() handler.
330 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
331 LiveProcess *p, ThreadContext *tc);
334 /// Pseudo Funcs - These functions use a different return convension,
335 /// returning a second value in a register other than the normal return register
336 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
337 LiveProcess *process, ThreadContext *tc);
339 /// Target getpidPseudo() handler.
340 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
341 LiveProcess *p, ThreadContext *tc);
343 /// Target getuidPseudo() handler.
344 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
345 LiveProcess *p, ThreadContext *tc);
347 /// Target getgidPseudo() handler.
348 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
349 LiveProcess *p, ThreadContext *tc);
352 /// A readable name for 1,000,000, for converting microseconds to seconds.
353 const int one_million = 1000000;
355 /// Approximate seconds since the epoch (1/1/1970). About a billion,
356 /// by my reckoning. We want to keep this a constant (not use the
357 /// real-world time) to keep simulations repeatable.
358 const unsigned seconds_since_epoch = 1000000000;
360 /// Helper function to convert current elapsed time to seconds and
362 template <class T1, class T2>
364 getElapsedTime(T1 &sec, T2 &usec)
366 int elapsed_usecs = curTick() / SimClock::Int::us;
367 sec = elapsed_usecs / one_million;
368 usec = elapsed_usecs % one_million;
371 //////////////////////////////////////////////////////////////////////
373 // The following emulation functions are generic, but need to be
374 // templated to account for differences in types, constants, etc.
376 //////////////////////////////////////////////////////////////////////
379 typedef struct stat hst_stat;
380 typedef struct stat hst_stat64;
382 typedef struct stat hst_stat;
383 typedef struct stat64 hst_stat64;
386 //// Helper function to convert a host stat buffer to a target stat
387 //// buffer. Also copies the target buffer out to the simulated
388 //// memory space. Used by stat(), fstat(), and lstat().
390 template <typename target_stat, typename host_stat>
392 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
394 using namespace TheISA;
399 tgt->st_dev = host->st_dev;
400 tgt->st_dev = htog(tgt->st_dev);
401 tgt->st_ino = host->st_ino;
402 tgt->st_ino = htog(tgt->st_ino);
403 tgt->st_mode = host->st_mode;
405 // Claim to be a character device
406 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
407 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
409 tgt->st_mode = htog(tgt->st_mode);
410 tgt->st_nlink = host->st_nlink;
411 tgt->st_nlink = htog(tgt->st_nlink);
412 tgt->st_uid = host->st_uid;
413 tgt->st_uid = htog(tgt->st_uid);
414 tgt->st_gid = host->st_gid;
415 tgt->st_gid = htog(tgt->st_gid);
417 tgt->st_rdev = 0x880d;
419 tgt->st_rdev = host->st_rdev;
420 tgt->st_rdev = htog(tgt->st_rdev);
421 tgt->st_size = host->st_size;
422 tgt->st_size = htog(tgt->st_size);
423 tgt->st_atimeX = host->st_atime;
424 tgt->st_atimeX = htog(tgt->st_atimeX);
425 tgt->st_mtimeX = host->st_mtime;
426 tgt->st_mtimeX = htog(tgt->st_mtimeX);
427 tgt->st_ctimeX = host->st_ctime;
428 tgt->st_ctimeX = htog(tgt->st_ctimeX);
429 // Force the block size to be 8k. This helps to ensure buffered io works
430 // consistently across different hosts.
431 tgt->st_blksize = 0x2000;
432 tgt->st_blksize = htog(tgt->st_blksize);
433 tgt->st_blocks = host->st_blocks;
434 tgt->st_blocks = htog(tgt->st_blocks);
439 template <typename target_stat, typename host_stat64>
441 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
443 using namespace TheISA;
445 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
446 #if defined(STAT_HAVE_NSEC)
447 tgt->st_atime_nsec = host->st_atime_nsec;
448 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
449 tgt->st_mtime_nsec = host->st_mtime_nsec;
450 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
451 tgt->st_ctime_nsec = host->st_ctime_nsec;
452 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
454 tgt->st_atime_nsec = 0;
455 tgt->st_mtime_nsec = 0;
456 tgt->st_ctime_nsec = 0;
460 //Here are a couple convenience functions
463 copyOutStatBuf(TranslatingPort * mem, Addr addr,
464 hst_stat *host, bool fakeTTY = false)
466 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
467 tgt_stat_buf tgt(addr);
468 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
474 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
475 hst_stat64 *host, bool fakeTTY = false)
477 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
478 tgt_stat_buf tgt(addr);
479 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
483 /// Target ioctl() handler. For the most part, programs call ioctl()
484 /// only to find out if their stdout is a tty, to determine whether to
485 /// do line or block buffering.
488 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
492 int fd = process->getSyscallArg(tc, index);
493 unsigned req = process->getSyscallArg(tc, index);
495 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
497 if (fd < 0 || process->sim_fd(fd) < 0) {
498 // doesn't map to any simulator fd: not a valid target fd
503 case OS::TIOCISATTY_:
515 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
516 fd, req, tc->pcState());
520 /// Target open() handler.
523 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
529 if (!tc->getMemPort()->tryReadString(path,
530 process->getSyscallArg(tc, index)))
533 if (path == "/dev/sysdev0") {
534 // This is a memory-mapped high-resolution timer device on Alpha.
535 // We don't support it, so just punt.
536 warn("Ignoring open(%s, ...)\n", path);
540 int tgtFlags = process->getSyscallArg(tc, index);
541 int mode = process->getSyscallArg(tc, index);
544 // translate open flags
545 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
546 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
547 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
548 hostFlags |= OS::openFlagTable[i].hostFlag;
552 // any target flags left?
554 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
557 hostFlags |= O_BINARY;
560 // Adjust path for current working directory
561 path = process->fullPath(path);
563 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
566 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
567 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
568 // It's a proc/sys entery and requires special handling
569 fd = OS::openSpecialFile(path, process, tc);
570 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
573 fd = open(path.c_str(), hostFlags, mode);
574 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
579 /// Target sysinfo() handler.
582 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
587 TypedBufferArg<typename OS::tgt_sysinfo>
588 sysinfo(process->getSyscallArg(tc, index));
590 sysinfo->uptime=seconds_since_epoch;
591 sysinfo->totalram=process->system->memSize();
593 sysinfo.copyOut(tc->getMemPort());
598 /// Target chmod() handler.
601 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
607 if (!tc->getMemPort()->tryReadString(path,
608 process->getSyscallArg(tc, index))) {
612 uint32_t mode = process->getSyscallArg(tc, index);
615 // XXX translate mode flags via OS::something???
618 // Adjust path for current working directory
619 path = process->fullPath(path);
622 int result = chmod(path.c_str(), hostMode);
630 /// Target fchmod() handler.
633 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
637 int fd = process->getSyscallArg(tc, index);
638 if (fd < 0 || process->sim_fd(fd) < 0) {
639 // doesn't map to any simulator fd: not a valid target fd
643 uint32_t mode = process->getSyscallArg(tc, index);
646 // XXX translate mode flags via OS::someting???
650 int result = fchmod(process->sim_fd(fd), hostMode);
657 /// Target mremap() handler.
660 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
663 Addr start = process->getSyscallArg(tc, index);
664 uint64_t old_length = process->getSyscallArg(tc, index);
665 uint64_t new_length = process->getSyscallArg(tc, index);
666 uint64_t flags = process->getSyscallArg(tc, index);
668 if ((start % TheISA::VMPageSize != 0) ||
669 (new_length % TheISA::VMPageSize != 0)) {
670 warn("mremap failing: arguments not page aligned");
674 if (new_length > old_length) {
675 if ((start + old_length) == process->mmap_end) {
676 uint64_t diff = new_length - old_length;
677 process->pTable->allocate(process->mmap_end, diff);
678 process->mmap_end += diff;
681 // sys/mman.h defined MREMAP_MAYMOVE
683 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
686 process->pTable->remap(start, old_length, process->mmap_end);
687 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
688 process->mmap_end, process->mmap_end + new_length, new_length);
689 start = process->mmap_end;
690 // add on the remaining unallocated pages
691 process->pTable->allocate(start + old_length, new_length - old_length);
692 process->mmap_end += new_length;
693 warn("returning %08p as start\n", start);
698 process->pTable->deallocate(start + new_length, old_length -
704 /// Target stat() handler.
707 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
713 if (!tc->getMemPort()->tryReadString(path,
714 process->getSyscallArg(tc, index))) {
717 Addr bufPtr = process->getSyscallArg(tc, index);
719 // Adjust path for current working directory
720 path = process->fullPath(path);
723 int result = stat(path.c_str(), &hostBuf);
728 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
734 /// Target stat64() handler.
737 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
743 if (!tc->getMemPort()->tryReadString(path,
744 process->getSyscallArg(tc, index)))
746 Addr bufPtr = process->getSyscallArg(tc, index);
748 // Adjust path for current working directory
749 path = process->fullPath(path);
753 int result = stat(path.c_str(), &hostBuf);
755 struct stat64 hostBuf;
756 int result = stat64(path.c_str(), &hostBuf);
762 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
768 /// Target fstat64() handler.
771 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
775 int fd = process->getSyscallArg(tc, index);
776 Addr bufPtr = process->getSyscallArg(tc, index);
777 if (fd < 0 || process->sim_fd(fd) < 0) {
778 // doesn't map to any simulator fd: not a valid target fd
784 int result = fstat(process->sim_fd(fd), &hostBuf);
786 struct stat64 hostBuf;
787 int result = fstat64(process->sim_fd(fd), &hostBuf);
793 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
799 /// Target lstat() handler.
802 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
808 if (!tc->getMemPort()->tryReadString(path,
809 process->getSyscallArg(tc, index))) {
812 Addr bufPtr = process->getSyscallArg(tc, index);
814 // Adjust path for current working directory
815 path = process->fullPath(path);
818 int result = lstat(path.c_str(), &hostBuf);
823 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
828 /// Target lstat64() handler.
831 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
837 if (!tc->getMemPort()->tryReadString(path,
838 process->getSyscallArg(tc, index))) {
841 Addr bufPtr = process->getSyscallArg(tc, index);
843 // Adjust path for current working directory
844 path = process->fullPath(path);
848 int result = lstat(path.c_str(), &hostBuf);
850 struct stat64 hostBuf;
851 int result = lstat64(path.c_str(), &hostBuf);
857 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
862 /// Target fstat() handler.
865 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
869 int fd = process->sim_fd(process->getSyscallArg(tc, index));
870 Addr bufPtr = process->getSyscallArg(tc, index);
872 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
878 int result = fstat(fd, &hostBuf);
883 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
889 /// Target statfs() handler.
892 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
898 if (!tc->getMemPort()->tryReadString(path,
899 process->getSyscallArg(tc, index))) {
902 Addr bufPtr = process->getSyscallArg(tc, index);
904 // Adjust path for current working directory
905 path = process->fullPath(path);
907 struct statfs hostBuf;
908 int result = statfs(path.c_str(), &hostBuf);
913 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
919 /// Target fstatfs() handler.
922 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
926 int fd = process->sim_fd(process->getSyscallArg(tc, index));
927 Addr bufPtr = process->getSyscallArg(tc, index);
932 struct statfs hostBuf;
933 int result = fstatfs(fd, &hostBuf);
938 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
944 /// Target writev() handler.
947 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
951 int fd = process->getSyscallArg(tc, index);
952 if (fd < 0 || process->sim_fd(fd) < 0) {
953 // doesn't map to any simulator fd: not a valid target fd
957 TranslatingPort *p = tc->getMemPort();
958 uint64_t tiov_base = process->getSyscallArg(tc, index);
959 size_t count = process->getSyscallArg(tc, index);
960 struct iovec hiov[count];
961 for (size_t i = 0; i < count; ++i) {
962 typename OS::tgt_iovec tiov;
964 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
965 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
966 hiov[i].iov_len = gtoh(tiov.iov_len);
967 hiov[i].iov_base = new char [hiov[i].iov_len];
968 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
972 int result = writev(process->sim_fd(fd), hiov, count);
974 for (size_t i = 0; i < count; ++i)
975 delete [] (char *)hiov[i].iov_base;
984 /// Target mmap() handler.
986 /// We don't really handle mmap(). If the target is mmaping an
987 /// anonymous region or /dev/zero, we can get away with doing basically
988 /// nothing (since memory is initialized to zero and the simulator
989 /// doesn't really check addresses anyway). Always print a warning,
990 /// since this could be seriously broken if we're not mapping
993 /// Someday we should explicitly check for /dev/zero in open, flag the
994 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
998 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1001 Addr start = p->getSyscallArg(tc, index);
1002 uint64_t length = p->getSyscallArg(tc, index);
1003 index++; // int prot = p->getSyscallArg(tc, index);
1004 int flags = p->getSyscallArg(tc, index);
1005 int fd = p->sim_fd(p->getSyscallArg(tc, index));
1006 // int offset = p->getSyscallArg(tc, index);
1009 if ((start % TheISA::VMPageSize) != 0 ||
1010 (length % TheISA::VMPageSize) != 0) {
1011 warn("mmap failing: arguments not page-aligned: "
1012 "start 0x%x length 0x%x",
1018 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
1019 start, p->mmap_end);
1022 // pick next address from our "mmap region"
1023 if (OS::mmapGrowsDown()) {
1024 start = p->mmap_end - length;
1025 p->mmap_end = start;
1027 start = p->mmap_end;
1028 p->mmap_end += length;
1030 p->pTable->allocate(start, length);
1032 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1033 warn("allowing mmap of file @ fd %d. "
1034 "This will break if not /dev/zero.", fd);
1040 /// Target getrlimit() handler.
1043 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1047 unsigned resource = process->getSyscallArg(tc, index);
1048 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1051 case OS::TGT_RLIMIT_STACK:
1052 // max stack size in bytes: make up a number (8MB for now)
1053 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1054 rlp->rlim_cur = htog(rlp->rlim_cur);
1055 rlp->rlim_max = htog(rlp->rlim_max);
1058 case OS::TGT_RLIMIT_DATA:
1059 // max data segment size in bytes: make up a number
1060 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1061 rlp->rlim_cur = htog(rlp->rlim_cur);
1062 rlp->rlim_max = htog(rlp->rlim_max);
1066 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1072 rlp.copyOut(tc->getMemPort());
1076 /// Target gettimeofday() handler.
1079 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1083 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1085 getElapsedTime(tp->tv_sec, tp->tv_usec);
1086 tp->tv_sec += seconds_since_epoch;
1087 tp->tv_sec = TheISA::htog(tp->tv_sec);
1088 tp->tv_usec = TheISA::htog(tp->tv_usec);
1090 tp.copyOut(tc->getMemPort());
1096 /// Target utimes() handler.
1099 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1105 if (!tc->getMemPort()->tryReadString(path,
1106 process->getSyscallArg(tc, index))) {
1110 TypedBufferArg<typename OS::timeval [2]>
1111 tp(process->getSyscallArg(tc, index));
1112 tp.copyIn(tc->getMemPort());
1114 struct timeval hostTimeval[2];
1115 for (int i = 0; i < 2; ++i)
1117 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1118 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1121 // Adjust path for current working directory
1122 path = process->fullPath(path);
1124 int result = utimes(path.c_str(), hostTimeval);
1131 /// Target getrusage() function.
1134 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1138 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1139 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1141 rup->ru_utime.tv_sec = 0;
1142 rup->ru_utime.tv_usec = 0;
1143 rup->ru_stime.tv_sec = 0;
1144 rup->ru_stime.tv_usec = 0;
1152 rup->ru_inblock = 0;
1153 rup->ru_oublock = 0;
1156 rup->ru_nsignals = 0;
1161 case OS::TGT_RUSAGE_SELF:
1162 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1163 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1164 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1167 case OS::TGT_RUSAGE_CHILDREN:
1168 // do nothing. We have no child processes, so they take no time.
1172 // don't really handle THREAD or CHILDREN, but just warn and
1174 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1178 rup.copyOut(tc->getMemPort());
1183 /// Target times() function.
1186 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1190 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1192 // Fill in the time structure (in clocks)
1193 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1194 bufp->tms_utime = clocks;
1195 bufp->tms_stime = 0;
1196 bufp->tms_cutime = 0;
1197 bufp->tms_cstime = 0;
1199 // Convert to host endianness
1200 bufp->tms_utime = htog(bufp->tms_utime);
1203 bufp.copyOut(tc->getMemPort());
1205 // Return clock ticks since system boot
1209 /// Target time() function.
1212 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1215 typename OS::time_t sec, usec;
1216 getElapsedTime(sec, usec);
1217 sec += seconds_since_epoch;
1220 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1222 typename OS::time_t t = sec;
1224 TranslatingPort *p = tc->getMemPort();
1225 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1231 #endif // __SIM_SYSCALL_EMUL_HH__