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/system.hh"
66 #include "sim/process.hh"
69 /// System call descriptor.
75 /// Typedef for target syscall handler functions.
76 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
77 LiveProcess *, ThreadContext *);
79 const char *name; //!< Syscall name (e.g., "open").
80 FuncPtr funcPtr; //!< Pointer to emulation function.
81 int flags; //!< Flags (see Flags enum).
83 /// Flag values for controlling syscall behavior.
85 /// Don't set return regs according to funcPtr return value.
86 /// Used for syscalls with non-standard return conventions
87 /// that explicitly set the ThreadContext regs (e.g.,
89 SuppressReturnValue = 1
93 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
94 : name(_name), funcPtr(_funcPtr), flags(_flags)
98 /// Emulate the syscall. Public interface for calling through funcPtr.
99 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
103 class BaseBufferArg {
107 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
109 bufPtr = new uint8_t[size];
110 // clear out buffer: in case we only partially populate this,
111 // and then do a copyOut(), we want to make sure we don't
112 // introduce any random junk into the simulated address space
113 memset(bufPtr, 0, size);
116 virtual ~BaseBufferArg() { delete [] bufPtr; }
119 // copy data into simulator space (read from target memory)
121 virtual bool copyIn(TranslatingPort *memport)
123 memport->readBlob(addr, bufPtr, size);
124 return true; // no EFAULT detection for now
128 // copy data out of simulator space (write to target memory)
130 virtual bool copyOut(TranslatingPort *memport)
132 memport->writeBlob(addr, bufPtr, size);
133 return true; // no EFAULT detection for now
143 class BufferArg : public BaseBufferArg
146 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
147 void *bufferPtr() { return bufPtr; }
151 class TypedBufferArg : public BaseBufferArg
154 // user can optionally specify a specific number of bytes to
155 // allocate to deal with those structs that have variable-size
157 TypedBufferArg(Addr _addr, int _size = sizeof(T))
158 : BaseBufferArg(_addr, _size)
162 operator T*() { return (T *)bufPtr; }
164 // dereference operators
165 T &operator*() { return *((T *)bufPtr); }
166 T* operator->() { return (T *)bufPtr; }
167 T &operator[](int i) { return ((T *)bufPtr)[i]; }
170 //////////////////////////////////////////////////////////////////////
172 // The following emulation functions are generic enough that they
173 // don't need to be recompiled for different emulated OS's. They are
174 // defined in sim/syscall_emul.cc.
176 //////////////////////////////////////////////////////////////////////
179 /// Handler for unimplemented syscalls that we haven't thought about.
180 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
181 LiveProcess *p, ThreadContext *tc);
183 /// Handler for unimplemented syscalls that we never intend to
184 /// implement (signal handling, etc.) and should not affect the correct
185 /// behavior of the program. Print a warning only if the appropriate
186 /// trace flag is enabled. Return success to the target program.
187 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
188 LiveProcess *p, ThreadContext *tc);
190 /// Target exit() handler: terminate current context.
191 SyscallReturn exitFunc(SyscallDesc *desc, int num,
192 LiveProcess *p, ThreadContext *tc);
194 /// Target exit_group() handler: terminate simulation. (exit all threads)
195 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
196 LiveProcess *p, ThreadContext *tc);
198 /// Target getpagesize() handler.
199 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
200 LiveProcess *p, ThreadContext *tc);
202 /// Target brk() handler: set brk address.
203 SyscallReturn brkFunc(SyscallDesc *desc, int num,
204 LiveProcess *p, ThreadContext *tc);
206 /// Target close() handler.
207 SyscallReturn closeFunc(SyscallDesc *desc, int num,
208 LiveProcess *p, ThreadContext *tc);
210 /// Target read() handler.
211 SyscallReturn readFunc(SyscallDesc *desc, int num,
212 LiveProcess *p, ThreadContext *tc);
214 /// Target write() handler.
215 SyscallReturn writeFunc(SyscallDesc *desc, int num,
216 LiveProcess *p, ThreadContext *tc);
218 /// Target lseek() handler.
219 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
220 LiveProcess *p, ThreadContext *tc);
222 /// Target _llseek() handler.
223 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
224 LiveProcess *p, ThreadContext *tc);
226 /// Target munmap() handler.
227 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
228 LiveProcess *p, ThreadContext *tc);
230 /// Target gethostname() handler.
231 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
232 LiveProcess *p, ThreadContext *tc);
234 /// Target getcwd() handler.
235 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
236 LiveProcess *p, ThreadContext *tc);
238 /// Target unlink() handler.
239 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
240 LiveProcess *p, ThreadContext *tc);
242 /// Target unlink() handler.
243 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
244 LiveProcess *p, ThreadContext *tc);
246 /// Target mkdir() handler.
247 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
248 LiveProcess *p, ThreadContext *tc);
250 /// Target rename() handler.
251 SyscallReturn renameFunc(SyscallDesc *desc, int num,
252 LiveProcess *p, ThreadContext *tc);
255 /// Target truncate() handler.
256 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
257 LiveProcess *p, ThreadContext *tc);
260 /// Target ftruncate() handler.
261 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
262 LiveProcess *p, ThreadContext *tc);
265 /// Target truncate64() handler.
266 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
267 LiveProcess *p, ThreadContext *tc);
269 /// Target ftruncate64() handler.
270 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
271 LiveProcess *p, ThreadContext *tc);
274 /// Target umask() handler.
275 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
276 LiveProcess *p, ThreadContext *tc);
279 /// Target chown() handler.
280 SyscallReturn chownFunc(SyscallDesc *desc, int num,
281 LiveProcess *p, ThreadContext *tc);
284 /// Target fchown() handler.
285 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
286 LiveProcess *p, ThreadContext *tc);
288 /// Target dup() handler.
289 SyscallReturn dupFunc(SyscallDesc *desc, int num,
290 LiveProcess *process, ThreadContext *tc);
292 /// Target fnctl() handler.
293 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
294 LiveProcess *process, ThreadContext *tc);
296 /// Target fcntl64() handler.
297 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
298 LiveProcess *process, ThreadContext *tc);
300 /// Target setuid() handler.
301 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
302 LiveProcess *p, ThreadContext *tc);
304 /// Target getpid() handler.
305 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
306 LiveProcess *p, ThreadContext *tc);
308 /// Target getuid() handler.
309 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
310 LiveProcess *p, ThreadContext *tc);
312 /// Target getgid() handler.
313 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
314 LiveProcess *p, ThreadContext *tc);
316 /// Target getppid() handler.
317 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
318 LiveProcess *p, ThreadContext *tc);
320 /// Target geteuid() handler.
321 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
322 LiveProcess *p, ThreadContext *tc);
324 /// Target getegid() handler.
325 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
326 LiveProcess *p, ThreadContext *tc);
328 /// Target clone() handler.
329 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
330 LiveProcess *p, ThreadContext *tc);
333 /// Pseudo Funcs - These functions use a different return convension,
334 /// returning a second value in a register other than the normal return register
335 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
336 LiveProcess *process, ThreadContext *tc);
338 /// Target getpidPseudo() handler.
339 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
340 LiveProcess *p, ThreadContext *tc);
342 /// Target getuidPseudo() handler.
343 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
344 LiveProcess *p, ThreadContext *tc);
346 /// Target getgidPseudo() handler.
347 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
348 LiveProcess *p, ThreadContext *tc);
351 /// A readable name for 1,000,000, for converting microseconds to seconds.
352 const int one_million = 1000000;
354 /// Approximate seconds since the epoch (1/1/1970). About a billion,
355 /// by my reckoning. We want to keep this a constant (not use the
356 /// real-world time) to keep simulations repeatable.
357 const unsigned seconds_since_epoch = 1000000000;
359 /// Helper function to convert current elapsed time to seconds and
361 template <class T1, class T2>
363 getElapsedTime(T1 &sec, T2 &usec)
365 int elapsed_usecs = curTick / SimClock::Int::us;
366 sec = elapsed_usecs / one_million;
367 usec = elapsed_usecs % one_million;
370 //////////////////////////////////////////////////////////////////////
372 // The following emulation functions are generic, but need to be
373 // templated to account for differences in types, constants, etc.
375 //////////////////////////////////////////////////////////////////////
378 typedef struct stat hst_stat;
379 typedef struct stat hst_stat64;
381 typedef struct stat hst_stat;
382 typedef struct stat64 hst_stat64;
385 //// Helper function to convert a host stat buffer to a target stat
386 //// buffer. Also copies the target buffer out to the simulated
387 //// memory space. Used by stat(), fstat(), and lstat().
389 template <typename target_stat, typename host_stat>
391 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
393 using namespace TheISA;
398 tgt->st_dev = host->st_dev;
399 tgt->st_dev = htog(tgt->st_dev);
400 tgt->st_ino = host->st_ino;
401 tgt->st_ino = htog(tgt->st_ino);
402 tgt->st_mode = host->st_mode;
404 // Claim to be a character device
405 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
406 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
408 tgt->st_mode = htog(tgt->st_mode);
409 tgt->st_nlink = host->st_nlink;
410 tgt->st_nlink = htog(tgt->st_nlink);
411 tgt->st_uid = host->st_uid;
412 tgt->st_uid = htog(tgt->st_uid);
413 tgt->st_gid = host->st_gid;
414 tgt->st_gid = htog(tgt->st_gid);
416 tgt->st_rdev = 0x880d;
418 tgt->st_rdev = host->st_rdev;
419 tgt->st_rdev = htog(tgt->st_rdev);
420 tgt->st_size = host->st_size;
421 tgt->st_size = htog(tgt->st_size);
422 tgt->st_atimeX = host->st_atime;
423 tgt->st_atimeX = htog(tgt->st_atimeX);
424 tgt->st_mtimeX = host->st_mtime;
425 tgt->st_mtimeX = htog(tgt->st_mtimeX);
426 tgt->st_ctimeX = host->st_ctime;
427 tgt->st_ctimeX = htog(tgt->st_ctimeX);
428 // Force the block size to be 8k. This helps to ensure buffered io works
429 // consistently across different hosts.
430 tgt->st_blksize = 0x2000;
431 tgt->st_blksize = htog(tgt->st_blksize);
432 tgt->st_blocks = host->st_blocks;
433 tgt->st_blocks = htog(tgt->st_blocks);
438 template <typename target_stat, typename host_stat64>
440 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
442 using namespace TheISA;
444 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
445 #if defined(STAT_HAVE_NSEC)
446 tgt->st_atime_nsec = host->st_atime_nsec;
447 tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
448 tgt->st_mtime_nsec = host->st_mtime_nsec;
449 tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
450 tgt->st_ctime_nsec = host->st_ctime_nsec;
451 tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
453 tgt->st_atime_nsec = 0;
454 tgt->st_mtime_nsec = 0;
455 tgt->st_ctime_nsec = 0;
459 //Here are a couple convenience functions
462 copyOutStatBuf(TranslatingPort * mem, Addr addr,
463 hst_stat *host, bool fakeTTY = false)
465 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
466 tgt_stat_buf tgt(addr);
467 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
473 copyOutStat64Buf(TranslatingPort * mem, Addr addr,
474 hst_stat64 *host, bool fakeTTY = false)
476 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
477 tgt_stat_buf tgt(addr);
478 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
482 /// Target ioctl() handler. For the most part, programs call ioctl()
483 /// only to find out if their stdout is a tty, to determine whether to
484 /// do line or block buffering.
487 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
491 int fd = process->getSyscallArg(tc, index);
492 unsigned req = process->getSyscallArg(tc, index);
494 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
496 if (fd < 0 || process->sim_fd(fd) < 0) {
497 // doesn't map to any simulator fd: not a valid target fd
502 case OS::TIOCISATTY_:
514 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
515 fd, req, tc->readPC());
519 /// Target open() handler.
522 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
528 if (!tc->getMemPort()->tryReadString(path,
529 process->getSyscallArg(tc, index)))
532 if (path == "/dev/sysdev0") {
533 // This is a memory-mapped high-resolution timer device on Alpha.
534 // We don't support it, so just punt.
535 warn("Ignoring open(%s, ...)\n", path);
539 int tgtFlags = process->getSyscallArg(tc, index);
540 int mode = process->getSyscallArg(tc, index);
543 // translate open flags
544 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
545 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
546 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
547 hostFlags |= OS::openFlagTable[i].hostFlag;
551 // any target flags left?
553 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
556 hostFlags |= O_BINARY;
559 // Adjust path for current working directory
560 path = process->fullPath(path);
562 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
565 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
566 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
567 // It's a proc/sys entery and requires special handling
568 fd = OS::openSpecialFile(path, process, tc);
569 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
572 fd = open(path.c_str(), hostFlags, mode);
573 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
578 /// Target sysinfo() handler.
581 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
586 TypedBufferArg<typename OS::tgt_sysinfo>
587 sysinfo(process->getSyscallArg(tc, index));
589 sysinfo->uptime=seconds_since_epoch;
590 sysinfo->totalram=process->system->memSize();
592 sysinfo.copyOut(tc->getMemPort());
597 /// Target chmod() handler.
600 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
606 if (!tc->getMemPort()->tryReadString(path,
607 process->getSyscallArg(tc, index))) {
611 uint32_t mode = process->getSyscallArg(tc, index);
614 // XXX translate mode flags via OS::something???
617 // Adjust path for current working directory
618 path = process->fullPath(path);
621 int result = chmod(path.c_str(), hostMode);
629 /// Target fchmod() handler.
632 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
636 int fd = process->getSyscallArg(tc, index);
637 if (fd < 0 || process->sim_fd(fd) < 0) {
638 // doesn't map to any simulator fd: not a valid target fd
642 uint32_t mode = process->getSyscallArg(tc, index);
645 // XXX translate mode flags via OS::someting???
649 int result = fchmod(process->sim_fd(fd), hostMode);
656 /// Target mremap() handler.
659 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
662 Addr start = process->getSyscallArg(tc, index);
663 uint64_t old_length = process->getSyscallArg(tc, index);
664 uint64_t new_length = process->getSyscallArg(tc, index);
665 uint64_t flags = process->getSyscallArg(tc, index);
667 if ((start % TheISA::VMPageSize != 0) ||
668 (new_length % TheISA::VMPageSize != 0)) {
669 warn("mremap failing: arguments not page aligned");
673 if (new_length > old_length) {
674 if ((start + old_length) == process->mmap_end) {
675 uint64_t diff = new_length - old_length;
676 process->pTable->allocate(process->mmap_end, diff);
677 process->mmap_end += diff;
680 // sys/mman.h defined MREMAP_MAYMOVE
682 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
685 process->pTable->remap(start, old_length, process->mmap_end);
686 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
687 process->mmap_end, process->mmap_end + new_length, new_length);
688 start = process->mmap_end;
689 // add on the remaining unallocated pages
690 process->pTable->allocate(start + old_length, new_length - old_length);
691 process->mmap_end += new_length;
692 warn("returning %08p as start\n", start);
697 process->pTable->deallocate(start + new_length, old_length -
703 /// Target stat() handler.
706 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
712 if (!tc->getMemPort()->tryReadString(path,
713 process->getSyscallArg(tc, index))) {
716 Addr bufPtr = process->getSyscallArg(tc, index);
718 // Adjust path for current working directory
719 path = process->fullPath(path);
722 int result = stat(path.c_str(), &hostBuf);
727 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
733 /// Target stat64() handler.
736 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
742 if (!tc->getMemPort()->tryReadString(path,
743 process->getSyscallArg(tc, index)))
745 Addr bufPtr = process->getSyscallArg(tc, index);
747 // Adjust path for current working directory
748 path = process->fullPath(path);
752 int result = stat(path.c_str(), &hostBuf);
754 struct stat64 hostBuf;
755 int result = stat64(path.c_str(), &hostBuf);
761 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
767 /// Target fstat64() handler.
770 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
774 int fd = process->getSyscallArg(tc, index);
775 Addr bufPtr = process->getSyscallArg(tc, index);
776 if (fd < 0 || process->sim_fd(fd) < 0) {
777 // doesn't map to any simulator fd: not a valid target fd
783 int result = fstat(process->sim_fd(fd), &hostBuf);
785 struct stat64 hostBuf;
786 int result = fstat64(process->sim_fd(fd), &hostBuf);
792 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
798 /// Target lstat() handler.
801 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
807 if (!tc->getMemPort()->tryReadString(path,
808 process->getSyscallArg(tc, index))) {
811 Addr bufPtr = process->getSyscallArg(tc, index);
813 // Adjust path for current working directory
814 path = process->fullPath(path);
817 int result = lstat(path.c_str(), &hostBuf);
822 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
827 /// Target lstat64() handler.
830 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
836 if (!tc->getMemPort()->tryReadString(path,
837 process->getSyscallArg(tc, index))) {
840 Addr bufPtr = process->getSyscallArg(tc, index);
842 // Adjust path for current working directory
843 path = process->fullPath(path);
847 int result = lstat(path.c_str(), &hostBuf);
849 struct stat64 hostBuf;
850 int result = lstat64(path.c_str(), &hostBuf);
856 copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
861 /// Target fstat() handler.
864 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
868 int fd = process->sim_fd(process->getSyscallArg(tc, index));
869 Addr bufPtr = process->getSyscallArg(tc, index);
871 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
877 int result = fstat(fd, &hostBuf);
882 copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
888 /// Target statfs() handler.
891 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
897 if (!tc->getMemPort()->tryReadString(path,
898 process->getSyscallArg(tc, index))) {
901 Addr bufPtr = process->getSyscallArg(tc, index);
903 // Adjust path for current working directory
904 path = process->fullPath(path);
906 struct statfs hostBuf;
907 int result = statfs(path.c_str(), &hostBuf);
912 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
918 /// Target fstatfs() handler.
921 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
925 int fd = process->sim_fd(process->getSyscallArg(tc, index));
926 Addr bufPtr = process->getSyscallArg(tc, index);
931 struct statfs hostBuf;
932 int result = fstatfs(fd, &hostBuf);
937 OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
943 /// Target writev() handler.
946 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
950 int fd = process->getSyscallArg(tc, index);
951 if (fd < 0 || process->sim_fd(fd) < 0) {
952 // doesn't map to any simulator fd: not a valid target fd
956 TranslatingPort *p = tc->getMemPort();
957 uint64_t tiov_base = process->getSyscallArg(tc, index);
958 size_t count = process->getSyscallArg(tc, index);
959 struct iovec hiov[count];
960 for (size_t i = 0; i < count; ++i) {
961 typename OS::tgt_iovec tiov;
963 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
964 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
965 hiov[i].iov_len = gtoh(tiov.iov_len);
966 hiov[i].iov_base = new char [hiov[i].iov_len];
967 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
971 int result = writev(process->sim_fd(fd), hiov, count);
973 for (size_t i = 0; i < count; ++i)
974 delete [] (char *)hiov[i].iov_base;
983 /// Target mmap() handler.
985 /// We don't really handle mmap(). If the target is mmaping an
986 /// anonymous region or /dev/zero, we can get away with doing basically
987 /// nothing (since memory is initialized to zero and the simulator
988 /// doesn't really check addresses anyway). Always print a warning,
989 /// since this could be seriously broken if we're not mapping
992 /// Someday we should explicitly check for /dev/zero in open, flag the
993 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
997 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1000 Addr start = p->getSyscallArg(tc, index);
1001 uint64_t length = p->getSyscallArg(tc, index);
1002 index++; // int prot = p->getSyscallArg(tc, index);
1003 int flags = p->getSyscallArg(tc, index);
1004 int fd = p->sim_fd(p->getSyscallArg(tc, index));
1005 // int offset = p->getSyscallArg(tc, index);
1008 if ((start % TheISA::VMPageSize) != 0 ||
1009 (length % TheISA::VMPageSize) != 0) {
1010 warn("mmap failing: arguments not page-aligned: "
1011 "start 0x%x length 0x%x",
1017 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
1018 start, p->mmap_end);
1021 // pick next address from our "mmap region"
1022 if (OS::mmapGrowsDown()) {
1023 start = p->mmap_end - length;
1024 p->mmap_end = start;
1026 start = p->mmap_end;
1027 p->mmap_end += length;
1029 p->pTable->allocate(start, length);
1031 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1032 warn("allowing mmap of file @ fd %d. "
1033 "This will break if not /dev/zero.", fd);
1039 /// Target getrlimit() handler.
1042 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1046 unsigned resource = process->getSyscallArg(tc, index);
1047 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1050 case OS::TGT_RLIMIT_STACK:
1051 // max stack size in bytes: make up a number (8MB for now)
1052 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1053 rlp->rlim_cur = htog(rlp->rlim_cur);
1054 rlp->rlim_max = htog(rlp->rlim_max);
1057 case OS::TGT_RLIMIT_DATA:
1058 // max data segment size in bytes: make up a number
1059 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1060 rlp->rlim_cur = htog(rlp->rlim_cur);
1061 rlp->rlim_max = htog(rlp->rlim_max);
1065 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1071 rlp.copyOut(tc->getMemPort());
1075 /// Target gettimeofday() handler.
1078 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1082 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1084 getElapsedTime(tp->tv_sec, tp->tv_usec);
1085 tp->tv_sec += seconds_since_epoch;
1086 tp->tv_sec = TheISA::htog(tp->tv_sec);
1087 tp->tv_usec = TheISA::htog(tp->tv_usec);
1089 tp.copyOut(tc->getMemPort());
1095 /// Target utimes() handler.
1098 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1104 if (!tc->getMemPort()->tryReadString(path,
1105 process->getSyscallArg(tc, index))) {
1109 TypedBufferArg<typename OS::timeval [2]>
1110 tp(process->getSyscallArg(tc, index));
1111 tp.copyIn(tc->getMemPort());
1113 struct timeval hostTimeval[2];
1114 for (int i = 0; i < 2; ++i)
1116 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
1117 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
1120 // Adjust path for current working directory
1121 path = process->fullPath(path);
1123 int result = utimes(path.c_str(), hostTimeval);
1130 /// Target getrusage() function.
1133 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1137 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1138 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1140 rup->ru_utime.tv_sec = 0;
1141 rup->ru_utime.tv_usec = 0;
1142 rup->ru_stime.tv_sec = 0;
1143 rup->ru_stime.tv_usec = 0;
1151 rup->ru_inblock = 0;
1152 rup->ru_oublock = 0;
1155 rup->ru_nsignals = 0;
1160 case OS::TGT_RUSAGE_SELF:
1161 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1162 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
1163 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
1166 case OS::TGT_RUSAGE_CHILDREN:
1167 // do nothing. We have no child processes, so they take no time.
1171 // don't really handle THREAD or CHILDREN, but just warn and
1173 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1177 rup.copyOut(tc->getMemPort());
1182 /// Target times() function.
1185 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1189 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1191 // Fill in the time structure (in clocks)
1192 int64_t clocks = curTick * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1193 bufp->tms_utime = clocks;
1194 bufp->tms_stime = 0;
1195 bufp->tms_cutime = 0;
1196 bufp->tms_cstime = 0;
1198 // Convert to host endianness
1199 bufp->tms_utime = htog(bufp->tms_utime);
1202 bufp.copyOut(tc->getMemPort());
1204 // Return clock ticks since system boot
1208 /// Target time() function.
1211 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1214 typename OS::time_t sec, usec;
1215 getElapsedTime(sec, usec);
1216 sec += seconds_since_epoch;
1219 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1221 typename OS::time_t t = sec;
1223 TranslatingPort *p = tc->getMemPort();
1224 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1230 #endif // __SIM_SYSCALL_EMUL_HH__