2 * Copyright (c) 2012-2013 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Authors: Steve Reinhardt
44 #ifndef __SIM_SYSCALL_EMUL_HH__
45 #define __SIM_SYSCALL_EMUL_HH__
47 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
48 defined(__FreeBSD__) || defined(__CYGWIN__) || \
52 /// @file syscall_emul.hh
54 /// This file defines objects used to emulate syscalls from the target
55 /// application on the host machine.
58 #include <sys/fcntl.h> // for O_BINARY
68 #include "base/chunk_generator.hh"
69 #include "base/intmath.hh" // for RoundUp
70 #include "base/misc.hh"
71 #include "base/trace.hh"
72 #include "base/types.hh"
73 #include "config/the_isa.hh"
74 #include "cpu/base.hh"
75 #include "cpu/thread_context.hh"
76 #include "debug/SyscallVerbose.hh"
77 #include "mem/page_table.hh"
78 #include "mem/se_translating_port_proxy.hh"
79 #include "sim/byteswap.hh"
80 #include "sim/process.hh"
81 #include "sim/syscallreturn.hh"
82 #include "sim/system.hh"
85 /// System call descriptor.
91 /// Typedef for target syscall handler functions.
92 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
93 LiveProcess *, ThreadContext *);
95 const char *name; //!< Syscall name (e.g., "open").
96 FuncPtr funcPtr; //!< Pointer to emulation function.
97 int flags; //!< Flags (see Flags enum).
99 /// Flag values for controlling syscall behavior.
101 /// Don't set return regs according to funcPtr return value.
102 /// Used for syscalls with non-standard return conventions
103 /// that explicitly set the ThreadContext regs (e.g.,
105 SuppressReturnValue = 1
109 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
110 : name(_name), funcPtr(_funcPtr), flags(_flags)
114 /// Emulate the syscall. Public interface for calling through funcPtr.
115 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
119 class BaseBufferArg {
123 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
125 bufPtr = new uint8_t[size];
126 // clear out buffer: in case we only partially populate this,
127 // and then do a copyOut(), we want to make sure we don't
128 // introduce any random junk into the simulated address space
129 memset(bufPtr, 0, size);
132 virtual ~BaseBufferArg() { delete [] bufPtr; }
135 // copy data into simulator space (read from target memory)
137 virtual bool copyIn(SETranslatingPortProxy &memproxy)
139 memproxy.readBlob(addr, bufPtr, size);
140 return true; // no EFAULT detection for now
144 // copy data out of simulator space (write to target memory)
146 virtual bool copyOut(SETranslatingPortProxy &memproxy)
148 memproxy.writeBlob(addr, bufPtr, size);
149 return true; // no EFAULT detection for now
159 class BufferArg : public BaseBufferArg
162 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
163 void *bufferPtr() { return bufPtr; }
167 class TypedBufferArg : public BaseBufferArg
170 // user can optionally specify a specific number of bytes to
171 // allocate to deal with those structs that have variable-size
173 TypedBufferArg(Addr _addr, int _size = sizeof(T))
174 : BaseBufferArg(_addr, _size)
178 operator T*() { return (T *)bufPtr; }
180 // dereference operators
181 T &operator*() { return *((T *)bufPtr); }
182 T* operator->() { return (T *)bufPtr; }
183 T &operator[](int i) { return ((T *)bufPtr)[i]; }
186 //////////////////////////////////////////////////////////////////////
188 // The following emulation functions are generic enough that they
189 // don't need to be recompiled for different emulated OS's. They are
190 // defined in sim/syscall_emul.cc.
192 //////////////////////////////////////////////////////////////////////
195 /// Handler for unimplemented syscalls that we haven't thought about.
196 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
197 LiveProcess *p, ThreadContext *tc);
199 /// Handler for unimplemented syscalls that we never intend to
200 /// implement (signal handling, etc.) and should not affect the correct
201 /// behavior of the program. Print a warning only if the appropriate
202 /// trace flag is enabled. Return success to the target program.
203 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
204 LiveProcess *p, ThreadContext *tc);
205 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
206 LiveProcess *p, ThreadContext *tc);
208 /// Target exit() handler: terminate current context.
209 SyscallReturn exitFunc(SyscallDesc *desc, int num,
210 LiveProcess *p, ThreadContext *tc);
212 /// Target exit_group() handler: terminate simulation. (exit all threads)
213 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
214 LiveProcess *p, ThreadContext *tc);
216 /// Target getpagesize() handler.
217 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
218 LiveProcess *p, ThreadContext *tc);
220 /// Target brk() handler: set brk address.
221 SyscallReturn brkFunc(SyscallDesc *desc, int num,
222 LiveProcess *p, ThreadContext *tc);
224 /// Target close() handler.
225 SyscallReturn closeFunc(SyscallDesc *desc, int num,
226 LiveProcess *p, ThreadContext *tc);
228 /// Target read() handler.
229 SyscallReturn readFunc(SyscallDesc *desc, int num,
230 LiveProcess *p, ThreadContext *tc);
232 /// Target write() handler.
233 SyscallReturn writeFunc(SyscallDesc *desc, int num,
234 LiveProcess *p, ThreadContext *tc);
236 /// Target lseek() handler.
237 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
238 LiveProcess *p, ThreadContext *tc);
240 /// Target _llseek() handler.
241 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
242 LiveProcess *p, ThreadContext *tc);
244 /// Target munmap() handler.
245 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
246 LiveProcess *p, ThreadContext *tc);
248 /// Target gethostname() handler.
249 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
250 LiveProcess *p, ThreadContext *tc);
252 /// Target getcwd() handler.
253 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
254 LiveProcess *p, ThreadContext *tc);
256 /// Target readlink() handler.
257 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
258 LiveProcess *p, ThreadContext *tc,
260 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
261 LiveProcess *p, ThreadContext *tc);
263 /// Target unlink() handler.
264 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
265 LiveProcess *p, ThreadContext *tc);
267 /// Target mkdir() handler.
268 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
269 LiveProcess *p, ThreadContext *tc);
271 /// Target rename() handler.
272 SyscallReturn renameFunc(SyscallDesc *desc, int num,
273 LiveProcess *p, ThreadContext *tc);
276 /// Target truncate() handler.
277 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
278 LiveProcess *p, ThreadContext *tc);
281 /// Target ftruncate() handler.
282 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
283 LiveProcess *p, ThreadContext *tc);
286 /// Target truncate64() handler.
287 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
288 LiveProcess *p, ThreadContext *tc);
290 /// Target ftruncate64() handler.
291 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
292 LiveProcess *p, ThreadContext *tc);
295 /// Target umask() handler.
296 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
297 LiveProcess *p, ThreadContext *tc);
300 /// Target chown() handler.
301 SyscallReturn chownFunc(SyscallDesc *desc, int num,
302 LiveProcess *p, ThreadContext *tc);
305 /// Target fchown() handler.
306 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
307 LiveProcess *p, ThreadContext *tc);
309 /// Target dup() handler.
310 SyscallReturn dupFunc(SyscallDesc *desc, int num,
311 LiveProcess *process, ThreadContext *tc);
313 /// Target fnctl() handler.
314 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
315 LiveProcess *process, ThreadContext *tc);
317 /// Target fcntl64() handler.
318 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
319 LiveProcess *process, ThreadContext *tc);
321 /// Target setuid() handler.
322 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
323 LiveProcess *p, ThreadContext *tc);
325 /// Target getpid() handler.
326 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
327 LiveProcess *p, ThreadContext *tc);
329 /// Target getuid() handler.
330 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
331 LiveProcess *p, ThreadContext *tc);
333 /// Target getgid() handler.
334 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
335 LiveProcess *p, ThreadContext *tc);
337 /// Target getppid() handler.
338 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
339 LiveProcess *p, ThreadContext *tc);
341 /// Target geteuid() handler.
342 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
343 LiveProcess *p, ThreadContext *tc);
345 /// Target getegid() handler.
346 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
347 LiveProcess *p, ThreadContext *tc);
349 /// Target clone() handler.
350 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
351 LiveProcess *p, ThreadContext *tc);
353 /// Target access() handler
354 SyscallReturn accessFunc(SyscallDesc *desc, int num,
355 LiveProcess *p, ThreadContext *tc);
356 SyscallReturn accessFunc(SyscallDesc *desc, int num,
357 LiveProcess *p, ThreadContext *tc,
360 /// Futex system call
361 /// Implemented by Daniel Sanchez
362 /// Used by printf's in multi-threaded apps
365 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
371 int index_timeout = 3;
373 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
374 int op = process->getSyscallArg(tc, index_op);
375 int val = process->getSyscallArg(tc, index_val);
376 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
378 std::map<uint64_t, std::list<ThreadContext *> * >
379 &futex_map = tc->getSystemPtr()->futexMap;
381 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
384 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
386 if (op == OS::TGT_FUTEX_WAIT) {
388 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
389 "we'll wait indefinitely");
392 uint8_t *buf = new uint8_t[sizeof(int)];
393 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
394 int mem_val = *((int *)buf);
398 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
399 "expected: %d\n", mem_val, val);
400 return -OS::TGT_EWOULDBLOCK;
403 // Queue the thread context
404 std::list<ThreadContext *> * tcWaitList;
405 if (futex_map.count(uaddr)) {
406 tcWaitList = futex_map.find(uaddr)->second;
408 tcWaitList = new std::list<ThreadContext *>();
409 futex_map.insert(std::pair< uint64_t,
410 std::list<ThreadContext *> * >(uaddr, tcWaitList));
412 tcWaitList->push_back(tc);
413 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
417 } else if (op == OS::TGT_FUTEX_WAKE){
419 std::list<ThreadContext *> * tcWaitList;
420 if (futex_map.count(uaddr)) {
421 tcWaitList = futex_map.find(uaddr)->second;
422 while (tcWaitList->size() > 0 && wokenUp < val) {
423 tcWaitList->front()->activate();
424 tcWaitList->pop_front();
427 if(tcWaitList->empty()) {
428 futex_map.erase(uaddr);
432 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
433 "thread contexts\n", wokenUp);
436 warn("sys_futex: op %d is not implemented, just returning...", op);
443 /// Pseudo Funcs - These functions use a different return convension,
444 /// returning a second value in a register other than the normal return register
445 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
446 LiveProcess *process, ThreadContext *tc);
448 /// Target getpidPseudo() handler.
449 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
450 LiveProcess *p, ThreadContext *tc);
452 /// Target getuidPseudo() handler.
453 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
454 LiveProcess *p, ThreadContext *tc);
456 /// Target getgidPseudo() handler.
457 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
458 LiveProcess *p, ThreadContext *tc);
461 /// A readable name for 1,000,000, for converting microseconds to seconds.
462 const int one_million = 1000000;
464 /// Approximate seconds since the epoch (1/1/1970). About a billion,
465 /// by my reckoning. We want to keep this a constant (not use the
466 /// real-world time) to keep simulations repeatable.
467 const unsigned seconds_since_epoch = 1000000000;
469 /// Helper function to convert current elapsed time to seconds and
471 template <class T1, class T2>
473 getElapsedTime(T1 &sec, T2 &usec)
475 int elapsed_usecs = curTick() / SimClock::Int::us;
476 sec = elapsed_usecs / one_million;
477 usec = elapsed_usecs % one_million;
480 //////////////////////////////////////////////////////////////////////
482 // The following emulation functions are generic, but need to be
483 // templated to account for differences in types, constants, etc.
485 //////////////////////////////////////////////////////////////////////
488 typedef struct stat hst_stat;
489 typedef struct stat hst_stat64;
491 typedef struct stat hst_stat;
492 typedef struct stat64 hst_stat64;
495 //// Helper function to convert a host stat buffer to a target stat
496 //// buffer. Also copies the target buffer out to the simulated
497 //// memory space. Used by stat(), fstat(), and lstat().
499 template <typename target_stat, typename host_stat>
501 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
503 using namespace TheISA;
508 tgt->st_dev = host->st_dev;
509 tgt->st_dev = TheISA::htog(tgt->st_dev);
510 tgt->st_ino = host->st_ino;
511 tgt->st_ino = TheISA::htog(tgt->st_ino);
512 tgt->st_mode = host->st_mode;
514 // Claim to be a character device
515 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
516 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
518 tgt->st_mode = TheISA::htog(tgt->st_mode);
519 tgt->st_nlink = host->st_nlink;
520 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
521 tgt->st_uid = host->st_uid;
522 tgt->st_uid = TheISA::htog(tgt->st_uid);
523 tgt->st_gid = host->st_gid;
524 tgt->st_gid = TheISA::htog(tgt->st_gid);
526 tgt->st_rdev = 0x880d;
528 tgt->st_rdev = host->st_rdev;
529 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
530 tgt->st_size = host->st_size;
531 tgt->st_size = TheISA::htog(tgt->st_size);
532 tgt->st_atimeX = host->st_atime;
533 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
534 tgt->st_mtimeX = host->st_mtime;
535 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
536 tgt->st_ctimeX = host->st_ctime;
537 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
538 // Force the block size to be 8k. This helps to ensure buffered io works
539 // consistently across different hosts.
540 tgt->st_blksize = 0x2000;
541 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
542 tgt->st_blocks = host->st_blocks;
543 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
548 template <typename target_stat, typename host_stat64>
550 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
552 using namespace TheISA;
554 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
555 #if defined(STAT_HAVE_NSEC)
556 tgt->st_atime_nsec = host->st_atime_nsec;
557 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
558 tgt->st_mtime_nsec = host->st_mtime_nsec;
559 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
560 tgt->st_ctime_nsec = host->st_ctime_nsec;
561 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
563 tgt->st_atime_nsec = 0;
564 tgt->st_mtime_nsec = 0;
565 tgt->st_ctime_nsec = 0;
569 //Here are a couple convenience functions
572 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
573 hst_stat *host, bool fakeTTY = false)
575 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
576 tgt_stat_buf tgt(addr);
577 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
583 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
584 hst_stat64 *host, bool fakeTTY = false)
586 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
587 tgt_stat_buf tgt(addr);
588 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
592 /// Target ioctl() handler. For the most part, programs call ioctl()
593 /// only to find out if their stdout is a tty, to determine whether to
594 /// do line or block buffering. We always claim that output fds are
595 /// not TTYs to provide repeatable results.
598 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
602 int fd = process->getSyscallArg(tc, index);
603 unsigned req = process->getSyscallArg(tc, index);
605 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
607 if (fd < 0 || process->sim_fd(fd) < 0) {
608 // doesn't map to any simulator fd: not a valid target fd
612 if (OS::isTtyReq(req)) {
616 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
617 fd, req, tc->pcState());
623 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
624 ThreadContext *tc, int index)
628 if (!tc->getMemProxy().tryReadString(path,
629 process->getSyscallArg(tc, index)))
632 if (path == "/dev/sysdev0") {
633 // This is a memory-mapped high-resolution timer device on Alpha.
634 // We don't support it, so just punt.
635 warn("Ignoring open(%s, ...)\n", path);
639 int tgtFlags = process->getSyscallArg(tc, index);
640 int mode = process->getSyscallArg(tc, index);
643 // translate open flags
644 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
645 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
646 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
647 hostFlags |= OS::openFlagTable[i].hostFlag;
651 // any target flags left?
653 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
656 hostFlags |= O_BINARY;
659 // Adjust path for current working directory
660 path = process->fullPath(path);
662 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
666 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
667 startswith(path, "/platform/") || startswith(path, "/sys/")) {
668 // It's a proc/sys entry and requires special handling
669 fd = OS::openSpecialFile(path, process, tc);
670 local_errno = ENOENT;
673 fd = open(path.c_str(), hostFlags, mode);
680 return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
683 /// Target open() handler.
686 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
689 return openFunc<OS>(desc, callnum, process, tc, 0);
692 /// Target openat() handler.
695 openatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
699 int dirfd = process->getSyscallArg(tc, index);
700 if (dirfd != OS::TGT_AT_FDCWD)
701 warn("openat: first argument not AT_FDCWD; unlikely to work");
702 return openFunc<OS>(desc, callnum, process, tc, 1);
705 /// Target facessat() handler
708 faccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
712 int dirfd = process->getSyscallArg(tc, index);
713 if (dirfd != OS::TGT_AT_FDCWD)
714 warn("faccessat: first argument not AT_FDCWD; unlikely to work");
715 return accessFunc(desc, callnum, process, tc, 1);
718 /// Target readlinkat() handler
721 readlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
725 int dirfd = process->getSyscallArg(tc, index);
726 if (dirfd != OS::TGT_AT_FDCWD)
727 warn("openat: first argument not AT_FDCWD; unlikely to work");
728 return readlinkFunc(desc, callnum, process, tc, 1);
731 /// Target sysinfo() handler.
734 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
739 TypedBufferArg<typename OS::tgt_sysinfo>
740 sysinfo(process->getSyscallArg(tc, index));
742 sysinfo->uptime=seconds_since_epoch;
743 sysinfo->totalram=process->system->memSize();
745 sysinfo.copyOut(tc->getMemProxy());
750 /// Target chmod() handler.
753 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
759 if (!tc->getMemProxy().tryReadString(path,
760 process->getSyscallArg(tc, index))) {
764 uint32_t mode = process->getSyscallArg(tc, index);
767 // XXX translate mode flags via OS::something???
770 // Adjust path for current working directory
771 path = process->fullPath(path);
774 int result = chmod(path.c_str(), hostMode);
782 /// Target fchmod() handler.
785 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
789 int fd = process->getSyscallArg(tc, index);
790 if (fd < 0 || process->sim_fd(fd) < 0) {
791 // doesn't map to any simulator fd: not a valid target fd
795 uint32_t mode = process->getSyscallArg(tc, index);
798 // XXX translate mode flags via OS::someting???
802 int result = fchmod(process->sim_fd(fd), hostMode);
809 /// Target mremap() handler.
812 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
815 Addr start = process->getSyscallArg(tc, index);
816 uint64_t old_length = process->getSyscallArg(tc, index);
817 uint64_t new_length = process->getSyscallArg(tc, index);
818 uint64_t flags = process->getSyscallArg(tc, index);
819 uint64_t provided_address = 0;
820 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
822 if (use_provided_address)
823 provided_address = process->getSyscallArg(tc, index);
825 if ((start % TheISA::PageBytes != 0) ||
826 (provided_address % TheISA::PageBytes != 0)) {
827 warn("mremap failing: arguments not page aligned");
831 new_length = roundUp(new_length, TheISA::PageBytes);
833 if (new_length > old_length) {
834 if ((start + old_length) == process->mmap_end &&
835 (!use_provided_address || provided_address == start)) {
836 uint64_t diff = new_length - old_length;
837 process->allocateMem(process->mmap_end, diff);
838 process->mmap_end += diff;
841 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
842 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
845 uint64_t new_start = use_provided_address ?
846 provided_address : process->mmap_end;
847 process->pTable->remap(start, old_length, new_start);
848 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
849 new_start, new_start + new_length,
850 new_length - old_length);
851 // add on the remaining unallocated pages
852 process->allocateMem(new_start + old_length,
853 new_length - old_length,
854 use_provided_address /* clobber */);
855 if (!use_provided_address)
856 process->mmap_end += new_length;
857 if (use_provided_address &&
858 new_start + new_length > process->mmap_end) {
859 // something fishy going on here, at least notify the user
860 // @todo: increase mmap_end?
861 warn("mmap region limit exceeded with MREMAP_FIXED\n");
863 warn("returning %08p as start\n", new_start);
868 if (use_provided_address && provided_address != start)
869 process->pTable->remap(start, new_length, provided_address);
870 process->pTable->unmap(start + new_length, old_length - new_length);
871 return use_provided_address ? provided_address : start;
875 /// Target stat() handler.
878 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
884 if (!tc->getMemProxy().tryReadString(path,
885 process->getSyscallArg(tc, index))) {
888 Addr bufPtr = process->getSyscallArg(tc, index);
890 // Adjust path for current working directory
891 path = process->fullPath(path);
894 int result = stat(path.c_str(), &hostBuf);
899 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
905 /// Target stat64() handler.
908 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
914 if (!tc->getMemProxy().tryReadString(path,
915 process->getSyscallArg(tc, index)))
917 Addr bufPtr = process->getSyscallArg(tc, index);
919 // Adjust path for current working directory
920 path = process->fullPath(path);
924 int result = stat(path.c_str(), &hostBuf);
926 struct stat64 hostBuf;
927 int result = stat64(path.c_str(), &hostBuf);
933 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
939 /// Target fstatat64() handler.
942 fstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
946 int dirfd = process->getSyscallArg(tc, index);
947 if (dirfd != OS::TGT_AT_FDCWD)
948 warn("openat: first argument not AT_FDCWD; unlikely to work");
951 if (!tc->getMemProxy().tryReadString(path,
952 process->getSyscallArg(tc, index)))
954 Addr bufPtr = process->getSyscallArg(tc, index);
956 // Adjust path for current working directory
957 path = process->fullPath(path);
961 int result = stat(path.c_str(), &hostBuf);
963 struct stat64 hostBuf;
964 int result = stat64(path.c_str(), &hostBuf);
970 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
976 /// Target fstat64() handler.
979 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
983 int fd = process->getSyscallArg(tc, index);
984 Addr bufPtr = process->getSyscallArg(tc, index);
985 if (fd < 0 || process->sim_fd(fd) < 0) {
986 // doesn't map to any simulator fd: not a valid target fd
992 int result = fstat(process->sim_fd(fd), &hostBuf);
994 struct stat64 hostBuf;
995 int result = fstat64(process->sim_fd(fd), &hostBuf);
1001 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
1007 /// Target lstat() handler.
1010 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1016 if (!tc->getMemProxy().tryReadString(path,
1017 process->getSyscallArg(tc, index))) {
1020 Addr bufPtr = process->getSyscallArg(tc, index);
1022 // Adjust path for current working directory
1023 path = process->fullPath(path);
1025 struct stat hostBuf;
1026 int result = lstat(path.c_str(), &hostBuf);
1031 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1036 /// Target lstat64() handler.
1039 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1045 if (!tc->getMemProxy().tryReadString(path,
1046 process->getSyscallArg(tc, index))) {
1049 Addr bufPtr = process->getSyscallArg(tc, index);
1051 // Adjust path for current working directory
1052 path = process->fullPath(path);
1055 struct stat hostBuf;
1056 int result = lstat(path.c_str(), &hostBuf);
1058 struct stat64 hostBuf;
1059 int result = lstat64(path.c_str(), &hostBuf);
1065 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1070 /// Target fstat() handler.
1073 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1077 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1078 Addr bufPtr = process->getSyscallArg(tc, index);
1080 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1085 struct stat hostBuf;
1086 int result = fstat(fd, &hostBuf);
1091 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
1097 /// Target statfs() handler.
1100 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1106 if (!tc->getMemProxy().tryReadString(path,
1107 process->getSyscallArg(tc, index))) {
1110 Addr bufPtr = process->getSyscallArg(tc, index);
1112 // Adjust path for current working directory
1113 path = process->fullPath(path);
1115 struct statfs hostBuf;
1116 int result = statfs(path.c_str(), &hostBuf);
1121 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1127 /// Target fstatfs() handler.
1130 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1134 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1135 Addr bufPtr = process->getSyscallArg(tc, index);
1140 struct statfs hostBuf;
1141 int result = fstatfs(fd, &hostBuf);
1146 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1152 /// Target writev() handler.
1155 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1159 int fd = process->getSyscallArg(tc, index);
1160 if (fd < 0 || process->sim_fd(fd) < 0) {
1161 // doesn't map to any simulator fd: not a valid target fd
1165 SETranslatingPortProxy &p = tc->getMemProxy();
1166 uint64_t tiov_base = process->getSyscallArg(tc, index);
1167 size_t count = process->getSyscallArg(tc, index);
1168 struct iovec hiov[count];
1169 for (size_t i = 0; i < count; ++i) {
1170 typename OS::tgt_iovec tiov;
1172 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1173 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1174 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1175 hiov[i].iov_base = new char [hiov[i].iov_len];
1176 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1180 int result = writev(process->sim_fd(fd), hiov, count);
1182 for (size_t i = 0; i < count; ++i)
1183 delete [] (char *)hiov[i].iov_base;
1192 /// Target mmap() handler.
1194 /// We don't really handle mmap(). If the target is mmaping an
1195 /// anonymous region or /dev/zero, we can get away with doing basically
1196 /// nothing (since memory is initialized to zero and the simulator
1197 /// doesn't really check addresses anyway).
1201 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1204 Addr start = p->getSyscallArg(tc, index);
1205 uint64_t length = p->getSyscallArg(tc, index);
1206 index++; // int prot = p->getSyscallArg(tc, index);
1207 int flags = p->getSyscallArg(tc, index);
1208 int tgt_fd = p->getSyscallArg(tc, index);
1209 int offset = p->getSyscallArg(tc, index);
1211 if (length > 0x100000000ULL)
1212 warn("mmap length argument %#x is unreasonably large.\n", length);
1214 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1215 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1216 if (!fd_map || fd_map->fd < 0) {
1217 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1221 if (fd_map->filename != "/dev/zero") {
1222 // This is very likely broken, but leave a warning here
1223 // (rather than panic) in case /dev/zero is known by
1224 // another name on some platform
1225 warn("allowing mmap of file %s; mmap not supported on files"
1226 " other than /dev/zero\n", fd_map->filename);
1230 length = roundUp(length, TheISA::PageBytes);
1232 if ((start % TheISA::PageBytes) != 0 ||
1233 (offset % TheISA::PageBytes) != 0) {
1234 warn("mmap failing: arguments not page-aligned: "
1235 "start 0x%x offset 0x%x",
1240 // are we ok with clobbering existing mappings? only set this to
1241 // true if the user has been warned.
1242 bool clobber = false;
1244 // try to use the caller-provided address if there is one
1245 bool use_provided_address = (start != 0);
1247 if (use_provided_address) {
1248 // check to see if the desired address is already in use
1249 if (!p->pTable->isUnmapped(start, length)) {
1250 // there are existing mappings in the desired range
1251 // whether we clobber them or not depends on whether the caller
1252 // specified MAP_FIXED
1253 if (flags & OS::TGT_MAP_FIXED) {
1254 // MAP_FIXED specified: map attempt fails
1257 // MAP_FIXED not specified: ignore suggested start address
1258 warn("mmap: ignoring suggested map address 0x%x\n", start);
1259 use_provided_address = false;
1264 if (!use_provided_address) {
1265 // no address provided, or provided address unusable:
1266 // pick next address from our "mmap region"
1267 if (OS::mmapGrowsDown()) {
1268 start = p->mmap_end - length;
1269 p->mmap_end = start;
1271 start = p->mmap_end;
1272 p->mmap_end += length;
1276 p->allocateMem(start, length, clobber);
1281 /// Target getrlimit() handler.
1284 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1288 unsigned resource = process->getSyscallArg(tc, index);
1289 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1292 case OS::TGT_RLIMIT_STACK:
1293 // max stack size in bytes: make up a number (8MB for now)
1294 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1295 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1296 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1299 case OS::TGT_RLIMIT_DATA:
1300 // max data segment size in bytes: make up a number
1301 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1302 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1303 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1307 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1313 rlp.copyOut(tc->getMemProxy());
1317 /// Target gettimeofday() handler.
1320 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1324 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1326 getElapsedTime(tp->tv_sec, tp->tv_usec);
1327 tp->tv_sec += seconds_since_epoch;
1328 tp->tv_sec = TheISA::htog(tp->tv_sec);
1329 tp->tv_usec = TheISA::htog(tp->tv_usec);
1331 tp.copyOut(tc->getMemProxy());
1337 /// Target utimes() handler.
1340 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1346 if (!tc->getMemProxy().tryReadString(path,
1347 process->getSyscallArg(tc, index))) {
1351 TypedBufferArg<typename OS::timeval [2]>
1352 tp(process->getSyscallArg(tc, index));
1353 tp.copyIn(tc->getMemProxy());
1355 struct timeval hostTimeval[2];
1356 for (int i = 0; i < 2; ++i)
1358 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1359 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1362 // Adjust path for current working directory
1363 path = process->fullPath(path);
1365 int result = utimes(path.c_str(), hostTimeval);
1372 /// Target getrusage() function.
1375 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1379 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1380 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1382 rup->ru_utime.tv_sec = 0;
1383 rup->ru_utime.tv_usec = 0;
1384 rup->ru_stime.tv_sec = 0;
1385 rup->ru_stime.tv_usec = 0;
1393 rup->ru_inblock = 0;
1394 rup->ru_oublock = 0;
1397 rup->ru_nsignals = 0;
1402 case OS::TGT_RUSAGE_SELF:
1403 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1404 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1405 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1408 case OS::TGT_RUSAGE_CHILDREN:
1409 // do nothing. We have no child processes, so they take no time.
1413 // don't really handle THREAD or CHILDREN, but just warn and
1415 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1419 rup.copyOut(tc->getMemProxy());
1424 /// Target times() function.
1427 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1431 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1433 // Fill in the time structure (in clocks)
1434 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1435 bufp->tms_utime = clocks;
1436 bufp->tms_stime = 0;
1437 bufp->tms_cutime = 0;
1438 bufp->tms_cstime = 0;
1440 // Convert to host endianness
1441 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1444 bufp.copyOut(tc->getMemProxy());
1446 // Return clock ticks since system boot
1450 /// Target time() function.
1453 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1456 typename OS::time_t sec, usec;
1457 getElapsedTime(sec, usec);
1458 sec += seconds_since_epoch;
1461 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1463 typename OS::time_t t = sec;
1464 t = TheISA::htog(t);
1465 SETranslatingPortProxy &p = tc->getMemProxy();
1466 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1472 #endif // __SIM_SYSCALL_EMUL_HH__