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);
337 /// Futex system call
338 /// Implemented by Daniel Sanchez
339 /// Used by printf's in multi-threaded apps
342 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
348 int index_timeout = 3;
350 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
351 int op = process->getSyscallArg(tc, index_op);
352 int val = process->getSyscallArg(tc, index_val);
353 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
355 std::map<uint64_t, std::list<ThreadContext *> * >
356 &futex_map = tc->getSystemPtr()->futexMap;
358 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
362 if (op == OS::TGT_FUTEX_WAIT) {
364 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
365 "we'll wait indefinitely");
368 uint8_t *buf = new uint8_t[sizeof(int)];
369 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
370 int mem_val = *((int *)buf);
374 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
375 "expected: %d\n", mem_val, val);
376 return -OS::TGT_EWOULDBLOCK;
379 // Queue the thread context
380 std::list<ThreadContext *> * tcWaitList;
381 if (futex_map.count(uaddr)) {
382 tcWaitList = futex_map.find(uaddr)->second;
384 tcWaitList = new std::list<ThreadContext *>();
385 futex_map.insert(std::pair< uint64_t,
386 std::list<ThreadContext *> * >(uaddr, tcWaitList));
388 tcWaitList->push_back(tc);
389 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
393 } else if (op == OS::TGT_FUTEX_WAKE){
395 std::list<ThreadContext *> * tcWaitList;
396 if (futex_map.count(uaddr)) {
397 tcWaitList = futex_map.find(uaddr)->second;
398 while (tcWaitList->size() > 0 && wokenUp < val) {
399 tcWaitList->front()->activate();
400 tcWaitList->pop_front();
403 if(tcWaitList->empty()) {
404 futex_map.erase(uaddr);
408 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
409 "thread contexts\n", wokenUp);
412 warn("sys_futex: op %d is not implemented, just returning...");
419 /// Pseudo Funcs - These functions use a different return convension,
420 /// returning a second value in a register other than the normal return register
421 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
422 LiveProcess *process, ThreadContext *tc);
424 /// Target getpidPseudo() handler.
425 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
426 LiveProcess *p, ThreadContext *tc);
428 /// Target getuidPseudo() handler.
429 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
430 LiveProcess *p, ThreadContext *tc);
432 /// Target getgidPseudo() handler.
433 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
434 LiveProcess *p, ThreadContext *tc);
437 /// A readable name for 1,000,000, for converting microseconds to seconds.
438 const int one_million = 1000000;
440 /// Approximate seconds since the epoch (1/1/1970). About a billion,
441 /// by my reckoning. We want to keep this a constant (not use the
442 /// real-world time) to keep simulations repeatable.
443 const unsigned seconds_since_epoch = 1000000000;
445 /// Helper function to convert current elapsed time to seconds and
447 template <class T1, class T2>
449 getElapsedTime(T1 &sec, T2 &usec)
451 int elapsed_usecs = curTick() / SimClock::Int::us;
452 sec = elapsed_usecs / one_million;
453 usec = elapsed_usecs % one_million;
456 //////////////////////////////////////////////////////////////////////
458 // The following emulation functions are generic, but need to be
459 // templated to account for differences in types, constants, etc.
461 //////////////////////////////////////////////////////////////////////
464 typedef struct stat hst_stat;
465 typedef struct stat hst_stat64;
467 typedef struct stat hst_stat;
468 typedef struct stat64 hst_stat64;
471 //// Helper function to convert a host stat buffer to a target stat
472 //// buffer. Also copies the target buffer out to the simulated
473 //// memory space. Used by stat(), fstat(), and lstat().
475 template <typename target_stat, typename host_stat>
477 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
479 using namespace TheISA;
484 tgt->st_dev = host->st_dev;
485 tgt->st_dev = TheISA::htog(tgt->st_dev);
486 tgt->st_ino = host->st_ino;
487 tgt->st_ino = TheISA::htog(tgt->st_ino);
488 tgt->st_mode = host->st_mode;
490 // Claim to be a character device
491 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
492 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
494 tgt->st_mode = TheISA::htog(tgt->st_mode);
495 tgt->st_nlink = host->st_nlink;
496 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
497 tgt->st_uid = host->st_uid;
498 tgt->st_uid = TheISA::htog(tgt->st_uid);
499 tgt->st_gid = host->st_gid;
500 tgt->st_gid = TheISA::htog(tgt->st_gid);
502 tgt->st_rdev = 0x880d;
504 tgt->st_rdev = host->st_rdev;
505 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
506 tgt->st_size = host->st_size;
507 tgt->st_size = TheISA::htog(tgt->st_size);
508 tgt->st_atimeX = host->st_atime;
509 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
510 tgt->st_mtimeX = host->st_mtime;
511 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
512 tgt->st_ctimeX = host->st_ctime;
513 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
514 // Force the block size to be 8k. This helps to ensure buffered io works
515 // consistently across different hosts.
516 tgt->st_blksize = 0x2000;
517 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
518 tgt->st_blocks = host->st_blocks;
519 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
524 template <typename target_stat, typename host_stat64>
526 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
528 using namespace TheISA;
530 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
531 #if defined(STAT_HAVE_NSEC)
532 tgt->st_atime_nsec = host->st_atime_nsec;
533 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
534 tgt->st_mtime_nsec = host->st_mtime_nsec;
535 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
536 tgt->st_ctime_nsec = host->st_ctime_nsec;
537 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
539 tgt->st_atime_nsec = 0;
540 tgt->st_mtime_nsec = 0;
541 tgt->st_ctime_nsec = 0;
545 //Here are a couple convenience functions
548 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
549 hst_stat *host, bool fakeTTY = false)
551 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
552 tgt_stat_buf tgt(addr);
553 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
559 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
560 hst_stat64 *host, bool fakeTTY = false)
562 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
563 tgt_stat_buf tgt(addr);
564 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
568 /// Target ioctl() handler. For the most part, programs call ioctl()
569 /// only to find out if their stdout is a tty, to determine whether to
570 /// do line or block buffering. We always claim that output fds are
571 /// not TTYs to provide repeatable results.
574 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
578 int fd = process->getSyscallArg(tc, index);
579 unsigned req = process->getSyscallArg(tc, index);
581 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
583 if (fd < 0 || process->sim_fd(fd) < 0) {
584 // doesn't map to any simulator fd: not a valid target fd
588 if (OS::isTtyReq(req)) {
592 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
593 fd, req, tc->pcState());
597 /// Target open() handler.
600 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
606 if (!tc->getMemProxy().tryReadString(path,
607 process->getSyscallArg(tc, index)))
610 if (path == "/dev/sysdev0") {
611 // This is a memory-mapped high-resolution timer device on Alpha.
612 // We don't support it, so just punt.
613 warn("Ignoring open(%s, ...)\n", path);
617 int tgtFlags = process->getSyscallArg(tc, index);
618 int mode = process->getSyscallArg(tc, index);
621 // translate open flags
622 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
623 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
624 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
625 hostFlags |= OS::openFlagTable[i].hostFlag;
629 // any target flags left?
631 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
634 hostFlags |= O_BINARY;
637 // Adjust path for current working directory
638 path = process->fullPath(path);
640 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
644 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
645 startswith(path, "/platform/") || startswith(path, "/sys/")) {
646 // It's a proc/sys entry and requires special handling
647 fd = OS::openSpecialFile(path, process, tc);
648 local_errno = ENOENT;
651 fd = open(path.c_str(), hostFlags, mode);
658 return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
661 /// Target sysinfo() handler.
664 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
669 TypedBufferArg<typename OS::tgt_sysinfo>
670 sysinfo(process->getSyscallArg(tc, index));
672 sysinfo->uptime=seconds_since_epoch;
673 sysinfo->totalram=process->system->memSize();
675 sysinfo.copyOut(tc->getMemProxy());
680 /// Target chmod() handler.
683 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
689 if (!tc->getMemProxy().tryReadString(path,
690 process->getSyscallArg(tc, index))) {
694 uint32_t mode = process->getSyscallArg(tc, index);
697 // XXX translate mode flags via OS::something???
700 // Adjust path for current working directory
701 path = process->fullPath(path);
704 int result = chmod(path.c_str(), hostMode);
712 /// Target fchmod() handler.
715 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
719 int fd = process->getSyscallArg(tc, index);
720 if (fd < 0 || process->sim_fd(fd) < 0) {
721 // doesn't map to any simulator fd: not a valid target fd
725 uint32_t mode = process->getSyscallArg(tc, index);
728 // XXX translate mode flags via OS::someting???
732 int result = fchmod(process->sim_fd(fd), hostMode);
739 /// Target mremap() handler.
742 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
745 Addr start = process->getSyscallArg(tc, index);
746 uint64_t old_length = process->getSyscallArg(tc, index);
747 uint64_t new_length = process->getSyscallArg(tc, index);
748 uint64_t flags = process->getSyscallArg(tc, index);
750 if ((start % TheISA::VMPageSize != 0) ||
751 (new_length % TheISA::VMPageSize != 0)) {
752 warn("mremap failing: arguments not page aligned");
756 if (new_length > old_length) {
757 if ((start + old_length) == process->mmap_end) {
758 uint64_t diff = new_length - old_length;
759 process->allocateMem(process->mmap_end, diff);
760 process->mmap_end += diff;
763 // sys/mman.h defined MREMAP_MAYMOVE
765 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
768 process->pTable->remap(start, old_length, process->mmap_end);
769 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
770 process->mmap_end, process->mmap_end + new_length, new_length);
771 start = process->mmap_end;
772 // add on the remaining unallocated pages
773 process->allocateMem(start + old_length,
774 new_length - old_length);
775 process->mmap_end += new_length;
776 warn("returning %08p as start\n", start);
781 process->pTable->unmap(start + new_length, old_length - new_length);
786 /// Target stat() handler.
789 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
795 if (!tc->getMemProxy().tryReadString(path,
796 process->getSyscallArg(tc, index))) {
799 Addr bufPtr = process->getSyscallArg(tc, index);
801 // Adjust path for current working directory
802 path = process->fullPath(path);
805 int result = stat(path.c_str(), &hostBuf);
810 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
816 /// Target stat64() handler.
819 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
825 if (!tc->getMemProxy().tryReadString(path,
826 process->getSyscallArg(tc, index)))
828 Addr bufPtr = process->getSyscallArg(tc, index);
830 // Adjust path for current working directory
831 path = process->fullPath(path);
835 int result = stat(path.c_str(), &hostBuf);
837 struct stat64 hostBuf;
838 int result = stat64(path.c_str(), &hostBuf);
844 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
850 /// Target fstat64() handler.
853 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
857 int fd = process->getSyscallArg(tc, index);
858 Addr bufPtr = process->getSyscallArg(tc, index);
859 if (fd < 0 || process->sim_fd(fd) < 0) {
860 // doesn't map to any simulator fd: not a valid target fd
866 int result = fstat(process->sim_fd(fd), &hostBuf);
868 struct stat64 hostBuf;
869 int result = fstat64(process->sim_fd(fd), &hostBuf);
875 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
881 /// Target lstat() handler.
884 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
890 if (!tc->getMemProxy().tryReadString(path,
891 process->getSyscallArg(tc, index))) {
894 Addr bufPtr = process->getSyscallArg(tc, index);
896 // Adjust path for current working directory
897 path = process->fullPath(path);
900 int result = lstat(path.c_str(), &hostBuf);
905 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
910 /// Target lstat64() handler.
913 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
919 if (!tc->getMemProxy().tryReadString(path,
920 process->getSyscallArg(tc, index))) {
923 Addr bufPtr = process->getSyscallArg(tc, index);
925 // Adjust path for current working directory
926 path = process->fullPath(path);
930 int result = lstat(path.c_str(), &hostBuf);
932 struct stat64 hostBuf;
933 int result = lstat64(path.c_str(), &hostBuf);
939 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
944 /// Target fstat() handler.
947 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
951 int fd = process->sim_fd(process->getSyscallArg(tc, index));
952 Addr bufPtr = process->getSyscallArg(tc, index);
954 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
960 int result = fstat(fd, &hostBuf);
965 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
971 /// Target statfs() handler.
974 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
980 if (!tc->getMemProxy().tryReadString(path,
981 process->getSyscallArg(tc, index))) {
984 Addr bufPtr = process->getSyscallArg(tc, index);
986 // Adjust path for current working directory
987 path = process->fullPath(path);
989 struct statfs hostBuf;
990 int result = statfs(path.c_str(), &hostBuf);
995 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1001 /// Target fstatfs() handler.
1004 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1008 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1009 Addr bufPtr = process->getSyscallArg(tc, index);
1014 struct statfs hostBuf;
1015 int result = fstatfs(fd, &hostBuf);
1020 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1026 /// Target writev() handler.
1029 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1033 int fd = process->getSyscallArg(tc, index);
1034 if (fd < 0 || process->sim_fd(fd) < 0) {
1035 // doesn't map to any simulator fd: not a valid target fd
1039 SETranslatingPortProxy &p = tc->getMemProxy();
1040 uint64_t tiov_base = process->getSyscallArg(tc, index);
1041 size_t count = process->getSyscallArg(tc, index);
1042 struct iovec hiov[count];
1043 for (size_t i = 0; i < count; ++i) {
1044 typename OS::tgt_iovec tiov;
1046 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1047 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1048 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1049 hiov[i].iov_base = new char [hiov[i].iov_len];
1050 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1054 int result = writev(process->sim_fd(fd), hiov, count);
1056 for (size_t i = 0; i < count; ++i)
1057 delete [] (char *)hiov[i].iov_base;
1066 /// Target mmap() handler.
1068 /// We don't really handle mmap(). If the target is mmaping an
1069 /// anonymous region or /dev/zero, we can get away with doing basically
1070 /// nothing (since memory is initialized to zero and the simulator
1071 /// doesn't really check addresses anyway).
1075 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1078 Addr start = p->getSyscallArg(tc, index);
1079 uint64_t length = p->getSyscallArg(tc, index);
1080 index++; // int prot = p->getSyscallArg(tc, index);
1081 int flags = p->getSyscallArg(tc, index);
1082 int tgt_fd = p->getSyscallArg(tc, index);
1083 // int offset = p->getSyscallArg(tc, index);
1085 if (length > 0x100000000ULL)
1086 warn("mmap length argument %#x is unreasonably large.\n", length);
1088 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1089 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1090 if (!fd_map || fd_map->fd < 0) {
1091 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1095 if (fd_map->filename != "/dev/zero") {
1096 // This is very likely broken, but leave a warning here
1097 // (rather than panic) in case /dev/zero is known by
1098 // another name on some platform
1099 warn("allowing mmap of file %s; mmap not supported on files"
1100 " other than /dev/zero\n", fd_map->filename);
1104 if ((start % TheISA::VMPageSize) != 0 ||
1105 (length % TheISA::VMPageSize) != 0) {
1106 warn("mmap failing: arguments not page-aligned: "
1107 "start 0x%x length 0x%x",
1112 // are we ok with clobbering existing mappings? only set this to
1113 // true if the user has been warned.
1114 bool clobber = false;
1116 // try to use the caller-provided address if there is one
1117 bool use_provided_address = (start != 0);
1119 if (use_provided_address) {
1120 // check to see if the desired address is already in use
1121 if (!p->pTable->isUnmapped(start, length)) {
1122 // there are existing mappings in the desired range
1123 // whether we clobber them or not depends on whether the caller
1124 // specified MAP_FIXED
1125 if (flags & OS::TGT_MAP_FIXED) {
1126 // MAP_FIXED specified: clobber existing mappings
1127 warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
1131 // MAP_FIXED not specified: ignore suggested start address
1132 warn("mmap: ignoring suggested map address 0x%x\n", start);
1133 use_provided_address = false;
1138 if (!use_provided_address) {
1139 // no address provided, or provided address unusable:
1140 // pick next address from our "mmap region"
1141 if (OS::mmapGrowsDown()) {
1142 start = p->mmap_end - length;
1143 p->mmap_end = start;
1145 start = p->mmap_end;
1146 p->mmap_end += length;
1150 p->allocateMem(start, length, clobber);
1155 /// Target getrlimit() handler.
1158 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1162 unsigned resource = process->getSyscallArg(tc, index);
1163 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1166 case OS::TGT_RLIMIT_STACK:
1167 // max stack size in bytes: make up a number (8MB for now)
1168 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1169 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1170 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1173 case OS::TGT_RLIMIT_DATA:
1174 // max data segment size in bytes: make up a number
1175 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1176 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1177 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1181 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1187 rlp.copyOut(tc->getMemProxy());
1191 /// Target gettimeofday() handler.
1194 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1198 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1200 getElapsedTime(tp->tv_sec, tp->tv_usec);
1201 tp->tv_sec += seconds_since_epoch;
1202 tp->tv_sec = TheISA::htog(tp->tv_sec);
1203 tp->tv_usec = TheISA::htog(tp->tv_usec);
1205 tp.copyOut(tc->getMemProxy());
1211 /// Target utimes() handler.
1214 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1220 if (!tc->getMemProxy().tryReadString(path,
1221 process->getSyscallArg(tc, index))) {
1225 TypedBufferArg<typename OS::timeval [2]>
1226 tp(process->getSyscallArg(tc, index));
1227 tp.copyIn(tc->getMemProxy());
1229 struct timeval hostTimeval[2];
1230 for (int i = 0; i < 2; ++i)
1232 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1233 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1236 // Adjust path for current working directory
1237 path = process->fullPath(path);
1239 int result = utimes(path.c_str(), hostTimeval);
1246 /// Target getrusage() function.
1249 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1253 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1254 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1256 rup->ru_utime.tv_sec = 0;
1257 rup->ru_utime.tv_usec = 0;
1258 rup->ru_stime.tv_sec = 0;
1259 rup->ru_stime.tv_usec = 0;
1267 rup->ru_inblock = 0;
1268 rup->ru_oublock = 0;
1271 rup->ru_nsignals = 0;
1276 case OS::TGT_RUSAGE_SELF:
1277 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1278 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1279 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1282 case OS::TGT_RUSAGE_CHILDREN:
1283 // do nothing. We have no child processes, so they take no time.
1287 // don't really handle THREAD or CHILDREN, but just warn and
1289 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1293 rup.copyOut(tc->getMemProxy());
1298 /// Target times() function.
1301 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1305 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1307 // Fill in the time structure (in clocks)
1308 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1309 bufp->tms_utime = clocks;
1310 bufp->tms_stime = 0;
1311 bufp->tms_cutime = 0;
1312 bufp->tms_cstime = 0;
1314 // Convert to host endianness
1315 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1318 bufp.copyOut(tc->getMemProxy());
1320 // Return clock ticks since system boot
1324 /// Target time() function.
1327 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1330 typename OS::time_t sec, usec;
1331 getElapsedTime(sec, usec);
1332 sec += seconds_since_epoch;
1335 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1337 typename OS::time_t t = sec;
1338 t = TheISA::htog(t);
1339 SETranslatingPortProxy &p = tc->getMemProxy();
1340 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1346 #endif // __SIM_SYSCALL_EMUL_HH__