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 "debug/SyscallVerbose.hh"
64 #include "mem/page_table.hh"
65 #include "mem/se_translating_port_proxy.hh"
66 #include "sim/byteswap.hh"
67 #include "sim/process.hh"
68 #include "sim/syscallreturn.hh"
69 #include "sim/system.hh"
72 /// System call descriptor.
78 /// Typedef for target syscall handler functions.
79 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
80 LiveProcess *, ThreadContext *);
82 const char *name; //!< Syscall name (e.g., "open").
83 FuncPtr funcPtr; //!< Pointer to emulation function.
84 int flags; //!< Flags (see Flags enum).
86 /// Flag values for controlling syscall behavior.
88 /// Don't set return regs according to funcPtr return value.
89 /// Used for syscalls with non-standard return conventions
90 /// that explicitly set the ThreadContext regs (e.g.,
92 SuppressReturnValue = 1
96 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
97 : name(_name), funcPtr(_funcPtr), flags(_flags)
101 /// Emulate the syscall. Public interface for calling through funcPtr.
102 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
106 class BaseBufferArg {
110 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
112 bufPtr = new uint8_t[size];
113 // clear out buffer: in case we only partially populate this,
114 // and then do a copyOut(), we want to make sure we don't
115 // introduce any random junk into the simulated address space
116 memset(bufPtr, 0, size);
119 virtual ~BaseBufferArg() { delete [] bufPtr; }
122 // copy data into simulator space (read from target memory)
124 virtual bool copyIn(SETranslatingPortProxy* memproxy)
126 memproxy->readBlob(addr, bufPtr, size);
127 return true; // no EFAULT detection for now
131 // copy data out of simulator space (write to target memory)
133 virtual bool copyOut(SETranslatingPortProxy* memproxy)
135 memproxy->writeBlob(addr, bufPtr, size);
136 return true; // no EFAULT detection for now
146 class BufferArg : public BaseBufferArg
149 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
150 void *bufferPtr() { return bufPtr; }
154 class TypedBufferArg : public BaseBufferArg
157 // user can optionally specify a specific number of bytes to
158 // allocate to deal with those structs that have variable-size
160 TypedBufferArg(Addr _addr, int _size = sizeof(T))
161 : BaseBufferArg(_addr, _size)
165 operator T*() { return (T *)bufPtr; }
167 // dereference operators
168 T &operator*() { return *((T *)bufPtr); }
169 T* operator->() { return (T *)bufPtr; }
170 T &operator[](int i) { return ((T *)bufPtr)[i]; }
173 //////////////////////////////////////////////////////////////////////
175 // The following emulation functions are generic enough that they
176 // don't need to be recompiled for different emulated OS's. They are
177 // defined in sim/syscall_emul.cc.
179 //////////////////////////////////////////////////////////////////////
182 /// Handler for unimplemented syscalls that we haven't thought about.
183 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
184 LiveProcess *p, ThreadContext *tc);
186 /// Handler for unimplemented syscalls that we never intend to
187 /// implement (signal handling, etc.) and should not affect the correct
188 /// behavior of the program. Print a warning only if the appropriate
189 /// trace flag is enabled. Return success to the target program.
190 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
191 LiveProcess *p, ThreadContext *tc);
192 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
193 LiveProcess *p, ThreadContext *tc);
195 /// Target exit() handler: terminate current context.
196 SyscallReturn exitFunc(SyscallDesc *desc, int num,
197 LiveProcess *p, ThreadContext *tc);
199 /// Target exit_group() handler: terminate simulation. (exit all threads)
200 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
201 LiveProcess *p, ThreadContext *tc);
203 /// Target getpagesize() handler.
204 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
205 LiveProcess *p, ThreadContext *tc);
207 /// Target brk() handler: set brk address.
208 SyscallReturn brkFunc(SyscallDesc *desc, int num,
209 LiveProcess *p, ThreadContext *tc);
211 /// Target close() handler.
212 SyscallReturn closeFunc(SyscallDesc *desc, int num,
213 LiveProcess *p, ThreadContext *tc);
215 /// Target read() handler.
216 SyscallReturn readFunc(SyscallDesc *desc, int num,
217 LiveProcess *p, ThreadContext *tc);
219 /// Target write() handler.
220 SyscallReturn writeFunc(SyscallDesc *desc, int num,
221 LiveProcess *p, ThreadContext *tc);
223 /// Target lseek() handler.
224 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
225 LiveProcess *p, ThreadContext *tc);
227 /// Target _llseek() handler.
228 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
229 LiveProcess *p, ThreadContext *tc);
231 /// Target munmap() handler.
232 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
233 LiveProcess *p, ThreadContext *tc);
235 /// Target gethostname() handler.
236 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
237 LiveProcess *p, ThreadContext *tc);
239 /// Target getcwd() handler.
240 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
241 LiveProcess *p, ThreadContext *tc);
243 /// Target unlink() handler.
244 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
245 LiveProcess *p, ThreadContext *tc);
247 /// Target unlink() handler.
248 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
249 LiveProcess *p, ThreadContext *tc);
251 /// Target mkdir() handler.
252 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
253 LiveProcess *p, ThreadContext *tc);
255 /// Target rename() handler.
256 SyscallReturn renameFunc(SyscallDesc *desc, int num,
257 LiveProcess *p, ThreadContext *tc);
260 /// Target truncate() handler.
261 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
262 LiveProcess *p, ThreadContext *tc);
265 /// Target ftruncate() handler.
266 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
267 LiveProcess *p, ThreadContext *tc);
270 /// Target truncate64() handler.
271 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
272 LiveProcess *p, ThreadContext *tc);
274 /// Target ftruncate64() handler.
275 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
276 LiveProcess *p, ThreadContext *tc);
279 /// Target umask() handler.
280 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
281 LiveProcess *p, ThreadContext *tc);
284 /// Target chown() handler.
285 SyscallReturn chownFunc(SyscallDesc *desc, int num,
286 LiveProcess *p, ThreadContext *tc);
289 /// Target fchown() handler.
290 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
291 LiveProcess *p, ThreadContext *tc);
293 /// Target dup() handler.
294 SyscallReturn dupFunc(SyscallDesc *desc, int num,
295 LiveProcess *process, ThreadContext *tc);
297 /// Target fnctl() handler.
298 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
299 LiveProcess *process, ThreadContext *tc);
301 /// Target fcntl64() handler.
302 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
303 LiveProcess *process, ThreadContext *tc);
305 /// Target setuid() handler.
306 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
307 LiveProcess *p, ThreadContext *tc);
309 /// Target getpid() handler.
310 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
311 LiveProcess *p, ThreadContext *tc);
313 /// Target getuid() handler.
314 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
315 LiveProcess *p, ThreadContext *tc);
317 /// Target getgid() handler.
318 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
319 LiveProcess *p, ThreadContext *tc);
321 /// Target getppid() handler.
322 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
323 LiveProcess *p, ThreadContext *tc);
325 /// Target geteuid() handler.
326 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
327 LiveProcess *p, ThreadContext *tc);
329 /// Target getegid() handler.
330 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
331 LiveProcess *p, ThreadContext *tc);
333 /// Target clone() handler.
334 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
335 LiveProcess *p, ThreadContext *tc);
338 /// Pseudo Funcs - These functions use a different return convension,
339 /// returning a second value in a register other than the normal return register
340 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
341 LiveProcess *process, ThreadContext *tc);
343 /// Target getpidPseudo() handler.
344 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
345 LiveProcess *p, ThreadContext *tc);
347 /// Target getuidPseudo() handler.
348 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
349 LiveProcess *p, ThreadContext *tc);
351 /// Target getgidPseudo() handler.
352 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
353 LiveProcess *p, ThreadContext *tc);
356 /// A readable name for 1,000,000, for converting microseconds to seconds.
357 const int one_million = 1000000;
359 /// Approximate seconds since the epoch (1/1/1970). About a billion,
360 /// by my reckoning. We want to keep this a constant (not use the
361 /// real-world time) to keep simulations repeatable.
362 const unsigned seconds_since_epoch = 1000000000;
364 /// Helper function to convert current elapsed time to seconds and
366 template <class T1, class T2>
368 getElapsedTime(T1 &sec, T2 &usec)
370 int elapsed_usecs = curTick() / SimClock::Int::us;
371 sec = elapsed_usecs / one_million;
372 usec = elapsed_usecs % one_million;
375 //////////////////////////////////////////////////////////////////////
377 // The following emulation functions are generic, but need to be
378 // templated to account for differences in types, constants, etc.
380 //////////////////////////////////////////////////////////////////////
383 typedef struct stat hst_stat;
384 typedef struct stat hst_stat64;
386 typedef struct stat hst_stat;
387 typedef struct stat64 hst_stat64;
390 //// Helper function to convert a host stat buffer to a target stat
391 //// buffer. Also copies the target buffer out to the simulated
392 //// memory space. Used by stat(), fstat(), and lstat().
394 template <typename target_stat, typename host_stat>
396 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
398 using namespace TheISA;
403 tgt->st_dev = host->st_dev;
404 tgt->st_dev = TheISA::htog(tgt->st_dev);
405 tgt->st_ino = host->st_ino;
406 tgt->st_ino = TheISA::htog(tgt->st_ino);
407 tgt->st_mode = host->st_mode;
409 // Claim to be a character device
410 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
411 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
413 tgt->st_mode = TheISA::htog(tgt->st_mode);
414 tgt->st_nlink = host->st_nlink;
415 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
416 tgt->st_uid = host->st_uid;
417 tgt->st_uid = TheISA::htog(tgt->st_uid);
418 tgt->st_gid = host->st_gid;
419 tgt->st_gid = TheISA::htog(tgt->st_gid);
421 tgt->st_rdev = 0x880d;
423 tgt->st_rdev = host->st_rdev;
424 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
425 tgt->st_size = host->st_size;
426 tgt->st_size = TheISA::htog(tgt->st_size);
427 tgt->st_atimeX = host->st_atime;
428 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
429 tgt->st_mtimeX = host->st_mtime;
430 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
431 tgt->st_ctimeX = host->st_ctime;
432 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
433 // Force the block size to be 8k. This helps to ensure buffered io works
434 // consistently across different hosts.
435 tgt->st_blksize = 0x2000;
436 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
437 tgt->st_blocks = host->st_blocks;
438 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
443 template <typename target_stat, typename host_stat64>
445 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
447 using namespace TheISA;
449 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
450 #if defined(STAT_HAVE_NSEC)
451 tgt->st_atime_nsec = host->st_atime_nsec;
452 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
453 tgt->st_mtime_nsec = host->st_mtime_nsec;
454 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
455 tgt->st_ctime_nsec = host->st_ctime_nsec;
456 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
458 tgt->st_atime_nsec = 0;
459 tgt->st_mtime_nsec = 0;
460 tgt->st_ctime_nsec = 0;
464 //Here are a couple convenience functions
467 copyOutStatBuf(SETranslatingPortProxy* mem, Addr addr,
468 hst_stat *host, bool fakeTTY = false)
470 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
471 tgt_stat_buf tgt(addr);
472 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
478 copyOutStat64Buf(SETranslatingPortProxy* mem, Addr addr,
479 hst_stat64 *host, bool fakeTTY = false)
481 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
482 tgt_stat_buf tgt(addr);
483 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
487 /// Target ioctl() handler. For the most part, programs call ioctl()
488 /// only to find out if their stdout is a tty, to determine whether to
489 /// do line or block buffering.
492 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
496 int fd = process->getSyscallArg(tc, index);
497 unsigned req = process->getSyscallArg(tc, index);
499 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
501 if (fd < 0 || process->sim_fd(fd) < 0) {
502 // doesn't map to any simulator fd: not a valid target fd
507 case OS::TIOCISATTY_:
519 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
520 fd, req, tc->pcState());
524 /// Target open() handler.
527 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
533 if (!tc->getMemProxy()->tryReadString(path,
534 process->getSyscallArg(tc, index)))
537 if (path == "/dev/sysdev0") {
538 // This is a memory-mapped high-resolution timer device on Alpha.
539 // We don't support it, so just punt.
540 warn("Ignoring open(%s, ...)\n", path);
544 int tgtFlags = process->getSyscallArg(tc, index);
545 int mode = process->getSyscallArg(tc, index);
548 // translate open flags
549 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
550 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
551 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
552 hostFlags |= OS::openFlagTable[i].hostFlag;
556 // any target flags left?
558 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
561 hostFlags |= O_BINARY;
564 // Adjust path for current working directory
565 path = process->fullPath(path);
567 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
570 if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
571 !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
572 // It's a proc/sys entery and requires special handling
573 fd = OS::openSpecialFile(path, process, tc);
574 return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
577 fd = open(path.c_str(), hostFlags, mode);
578 return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
583 /// Target sysinfo() handler.
586 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
591 TypedBufferArg<typename OS::tgt_sysinfo>
592 sysinfo(process->getSyscallArg(tc, index));
594 sysinfo->uptime=seconds_since_epoch;
595 sysinfo->totalram=process->system->memSize();
597 sysinfo.copyOut(tc->getMemProxy());
602 /// Target chmod() handler.
605 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
611 if (!tc->getMemProxy()->tryReadString(path,
612 process->getSyscallArg(tc, index))) {
616 uint32_t mode = process->getSyscallArg(tc, index);
619 // XXX translate mode flags via OS::something???
622 // Adjust path for current working directory
623 path = process->fullPath(path);
626 int result = chmod(path.c_str(), hostMode);
634 /// Target fchmod() handler.
637 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
641 int fd = process->getSyscallArg(tc, index);
642 if (fd < 0 || process->sim_fd(fd) < 0) {
643 // doesn't map to any simulator fd: not a valid target fd
647 uint32_t mode = process->getSyscallArg(tc, index);
650 // XXX translate mode flags via OS::someting???
654 int result = fchmod(process->sim_fd(fd), hostMode);
661 /// Target mremap() handler.
664 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
667 Addr start = process->getSyscallArg(tc, index);
668 uint64_t old_length = process->getSyscallArg(tc, index);
669 uint64_t new_length = process->getSyscallArg(tc, index);
670 uint64_t flags = process->getSyscallArg(tc, index);
672 if ((start % TheISA::VMPageSize != 0) ||
673 (new_length % TheISA::VMPageSize != 0)) {
674 warn("mremap failing: arguments not page aligned");
678 if (new_length > old_length) {
679 if ((start + old_length) == process->mmap_end) {
680 uint64_t diff = new_length - old_length;
681 process->allocateMem(process->mmap_end, diff);
682 process->mmap_end += diff;
685 // sys/mman.h defined MREMAP_MAYMOVE
687 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
690 process->pTable->remap(start, old_length, process->mmap_end);
691 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
692 process->mmap_end, process->mmap_end + new_length, new_length);
693 start = process->mmap_end;
694 // add on the remaining unallocated pages
695 process->allocateMem(start + old_length,
696 new_length - old_length);
697 process->mmap_end += new_length;
698 warn("returning %08p as start\n", start);
703 process->pTable->unmap(start + new_length, old_length - new_length);
708 /// Target stat() handler.
711 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
717 if (!tc->getMemProxy()->tryReadString(path,
718 process->getSyscallArg(tc, index))) {
721 Addr bufPtr = process->getSyscallArg(tc, index);
723 // Adjust path for current working directory
724 path = process->fullPath(path);
727 int result = stat(path.c_str(), &hostBuf);
732 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
738 /// Target stat64() handler.
741 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
747 if (!tc->getMemProxy()->tryReadString(path,
748 process->getSyscallArg(tc, index)))
750 Addr bufPtr = process->getSyscallArg(tc, index);
752 // Adjust path for current working directory
753 path = process->fullPath(path);
757 int result = stat(path.c_str(), &hostBuf);
759 struct stat64 hostBuf;
760 int result = stat64(path.c_str(), &hostBuf);
766 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
772 /// Target fstat64() handler.
775 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
779 int fd = process->getSyscallArg(tc, index);
780 Addr bufPtr = process->getSyscallArg(tc, index);
781 if (fd < 0 || process->sim_fd(fd) < 0) {
782 // doesn't map to any simulator fd: not a valid target fd
788 int result = fstat(process->sim_fd(fd), &hostBuf);
790 struct stat64 hostBuf;
791 int result = fstat64(process->sim_fd(fd), &hostBuf);
797 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
803 /// Target lstat() handler.
806 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
812 if (!tc->getMemProxy()->tryReadString(path,
813 process->getSyscallArg(tc, index))) {
816 Addr bufPtr = process->getSyscallArg(tc, index);
818 // Adjust path for current working directory
819 path = process->fullPath(path);
822 int result = lstat(path.c_str(), &hostBuf);
827 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
832 /// Target lstat64() handler.
835 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
841 if (!tc->getMemProxy()->tryReadString(path,
842 process->getSyscallArg(tc, index))) {
845 Addr bufPtr = process->getSyscallArg(tc, index);
847 // Adjust path for current working directory
848 path = process->fullPath(path);
852 int result = lstat(path.c_str(), &hostBuf);
854 struct stat64 hostBuf;
855 int result = lstat64(path.c_str(), &hostBuf);
861 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
866 /// Target fstat() handler.
869 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
873 int fd = process->sim_fd(process->getSyscallArg(tc, index));
874 Addr bufPtr = process->getSyscallArg(tc, index);
876 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
882 int result = fstat(fd, &hostBuf);
887 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
893 /// Target statfs() handler.
896 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
902 if (!tc->getMemProxy()->tryReadString(path,
903 process->getSyscallArg(tc, index))) {
906 Addr bufPtr = process->getSyscallArg(tc, index);
908 // Adjust path for current working directory
909 path = process->fullPath(path);
911 struct statfs hostBuf;
912 int result = statfs(path.c_str(), &hostBuf);
917 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
923 /// Target fstatfs() handler.
926 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
930 int fd = process->sim_fd(process->getSyscallArg(tc, index));
931 Addr bufPtr = process->getSyscallArg(tc, index);
936 struct statfs hostBuf;
937 int result = fstatfs(fd, &hostBuf);
942 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
948 /// Target writev() handler.
951 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
955 int fd = process->getSyscallArg(tc, index);
956 if (fd < 0 || process->sim_fd(fd) < 0) {
957 // doesn't map to any simulator fd: not a valid target fd
961 SETranslatingPortProxy *p = tc->getMemProxy();
962 uint64_t tiov_base = process->getSyscallArg(tc, index);
963 size_t count = process->getSyscallArg(tc, index);
964 struct iovec hiov[count];
965 for (size_t i = 0; i < count; ++i) {
966 typename OS::tgt_iovec tiov;
968 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
969 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
970 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
971 hiov[i].iov_base = new char [hiov[i].iov_len];
972 p->readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
976 int result = writev(process->sim_fd(fd), hiov, count);
978 for (size_t i = 0; i < count; ++i)
979 delete [] (char *)hiov[i].iov_base;
988 /// Target mmap() handler.
990 /// We don't really handle mmap(). If the target is mmaping an
991 /// anonymous region or /dev/zero, we can get away with doing basically
992 /// nothing (since memory is initialized to zero and the simulator
993 /// doesn't really check addresses anyway).
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 tgt_fd = p->getSyscallArg(tc, index);
1005 // int offset = p->getSyscallArg(tc, index);
1007 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1008 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1009 if (!fd_map || fd_map->fd < 0) {
1010 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1014 if (fd_map->filename != "/dev/zero") {
1015 // This is very likely broken, but leave a warning here
1016 // (rather than panic) in case /dev/zero is known by
1017 // another name on some platform
1018 warn("allowing mmap of file %s; mmap not supported on files"
1019 " other than /dev/zero\n", fd_map->filename);
1023 if ((start % TheISA::VMPageSize) != 0 ||
1024 (length % TheISA::VMPageSize) != 0) {
1025 warn("mmap failing: arguments not page-aligned: "
1026 "start 0x%x length 0x%x",
1031 // are we ok with clobbering existing mappings? only set this to
1032 // true if the user has been warned.
1033 bool clobber = false;
1035 // try to use the caller-provided address if there is one
1036 bool use_provided_address = (start != 0);
1038 if (use_provided_address) {
1039 // check to see if the desired address is already in use
1040 if (!p->pTable->isUnmapped(start, length)) {
1041 // there are existing mappings in the desired range
1042 // whether we clobber them or not depends on whether the caller
1043 // specified MAP_FIXED
1044 if (flags & OS::TGT_MAP_FIXED) {
1045 // MAP_FIXED specified: clobber existing mappings
1046 warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
1050 // MAP_FIXED not specified: ignore suggested start address
1051 warn("mmap: ignoring suggested map address 0x%x\n", start);
1052 use_provided_address = false;
1057 if (!use_provided_address) {
1058 // no address provided, or provided address unusable:
1059 // pick next address from our "mmap region"
1060 if (OS::mmapGrowsDown()) {
1061 start = p->mmap_end - length;
1062 p->mmap_end = start;
1064 start = p->mmap_end;
1065 p->mmap_end += length;
1069 p->allocateMem(start, length, clobber);
1074 /// Target getrlimit() handler.
1077 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1081 unsigned resource = process->getSyscallArg(tc, index);
1082 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1085 case OS::TGT_RLIMIT_STACK:
1086 // max stack size in bytes: make up a number (8MB for now)
1087 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1088 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1089 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1092 case OS::TGT_RLIMIT_DATA:
1093 // max data segment size in bytes: make up a number
1094 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1095 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1096 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1100 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1106 rlp.copyOut(tc->getMemProxy());
1110 /// Target gettimeofday() handler.
1113 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1117 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1119 getElapsedTime(tp->tv_sec, tp->tv_usec);
1120 tp->tv_sec += seconds_since_epoch;
1121 tp->tv_sec = TheISA::htog(tp->tv_sec);
1122 tp->tv_usec = TheISA::htog(tp->tv_usec);
1124 tp.copyOut(tc->getMemProxy());
1130 /// Target utimes() handler.
1133 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1139 if (!tc->getMemProxy()->tryReadString(path,
1140 process->getSyscallArg(tc, index))) {
1144 TypedBufferArg<typename OS::timeval [2]>
1145 tp(process->getSyscallArg(tc, index));
1146 tp.copyIn(tc->getMemProxy());
1148 struct timeval hostTimeval[2];
1149 for (int i = 0; i < 2; ++i)
1151 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1152 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1155 // Adjust path for current working directory
1156 path = process->fullPath(path);
1158 int result = utimes(path.c_str(), hostTimeval);
1165 /// Target getrusage() function.
1168 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1172 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1173 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1175 rup->ru_utime.tv_sec = 0;
1176 rup->ru_utime.tv_usec = 0;
1177 rup->ru_stime.tv_sec = 0;
1178 rup->ru_stime.tv_usec = 0;
1186 rup->ru_inblock = 0;
1187 rup->ru_oublock = 0;
1190 rup->ru_nsignals = 0;
1195 case OS::TGT_RUSAGE_SELF:
1196 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1197 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1198 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1201 case OS::TGT_RUSAGE_CHILDREN:
1202 // do nothing. We have no child processes, so they take no time.
1206 // don't really handle THREAD or CHILDREN, but just warn and
1208 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1212 rup.copyOut(tc->getMemProxy());
1217 /// Target times() function.
1220 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1224 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1226 // Fill in the time structure (in clocks)
1227 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1228 bufp->tms_utime = clocks;
1229 bufp->tms_stime = 0;
1230 bufp->tms_cutime = 0;
1231 bufp->tms_cstime = 0;
1233 // Convert to host endianness
1234 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1237 bufp.copyOut(tc->getMemProxy());
1239 // Return clock ticks since system boot
1243 /// Target time() function.
1246 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1249 typename OS::time_t sec, usec;
1250 getElapsedTime(sec, usec);
1251 sec += seconds_since_epoch;
1254 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1256 typename OS::time_t t = sec;
1257 t = TheISA::htog(t);
1258 SETranslatingPortProxy *p = tc->getMemProxy();
1259 p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1265 #endif // __SIM_SYSCALL_EMUL_HH__