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/emul_driver.hh"
81 #include "sim/process.hh"
82 #include "sim/syscallreturn.hh"
83 #include "sim/system.hh"
86 /// System call descriptor.
92 /// Typedef for target syscall handler functions.
93 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
94 LiveProcess *, ThreadContext *);
96 const char *name; //!< Syscall name (e.g., "open").
97 FuncPtr funcPtr; //!< Pointer to emulation function.
98 int flags; //!< Flags (see Flags enum).
100 /// Flag values for controlling syscall behavior.
102 /// Don't set return regs according to funcPtr return value.
103 /// Used for syscalls with non-standard return conventions
104 /// that explicitly set the ThreadContext regs (e.g.,
106 SuppressReturnValue = 1
110 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
111 : name(_name), funcPtr(_funcPtr), flags(_flags)
115 /// Emulate the syscall. Public interface for calling through funcPtr.
116 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
120 class BaseBufferArg {
124 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
126 bufPtr = new uint8_t[size];
127 // clear out buffer: in case we only partially populate this,
128 // and then do a copyOut(), we want to make sure we don't
129 // introduce any random junk into the simulated address space
130 memset(bufPtr, 0, size);
133 virtual ~BaseBufferArg() { delete [] bufPtr; }
136 // copy data into simulator space (read from target memory)
138 virtual bool copyIn(SETranslatingPortProxy &memproxy)
140 memproxy.readBlob(addr, bufPtr, size);
141 return true; // no EFAULT detection for now
145 // copy data out of simulator space (write to target memory)
147 virtual bool copyOut(SETranslatingPortProxy &memproxy)
149 memproxy.writeBlob(addr, bufPtr, size);
150 return true; // no EFAULT detection for now
160 class BufferArg : public BaseBufferArg
163 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
164 void *bufferPtr() { return bufPtr; }
168 class TypedBufferArg : public BaseBufferArg
171 // user can optionally specify a specific number of bytes to
172 // allocate to deal with those structs that have variable-size
174 TypedBufferArg(Addr _addr, int _size = sizeof(T))
175 : BaseBufferArg(_addr, _size)
179 operator T*() { return (T *)bufPtr; }
181 // dereference operators
182 T &operator*() { return *((T *)bufPtr); }
183 T* operator->() { return (T *)bufPtr; }
184 T &operator[](int i) { return ((T *)bufPtr)[i]; }
187 //////////////////////////////////////////////////////////////////////
189 // The following emulation functions are generic enough that they
190 // don't need to be recompiled for different emulated OS's. They are
191 // defined in sim/syscall_emul.cc.
193 //////////////////////////////////////////////////////////////////////
196 /// Handler for unimplemented syscalls that we haven't thought about.
197 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
198 LiveProcess *p, ThreadContext *tc);
200 /// Handler for unimplemented syscalls that we never intend to
201 /// implement (signal handling, etc.) and should not affect the correct
202 /// behavior of the program. Print a warning only if the appropriate
203 /// trace flag is enabled. Return success to the target program.
204 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
205 LiveProcess *p, ThreadContext *tc);
206 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
207 LiveProcess *p, ThreadContext *tc);
209 /// Target exit() handler: terminate current context.
210 SyscallReturn exitFunc(SyscallDesc *desc, int num,
211 LiveProcess *p, ThreadContext *tc);
213 /// Target exit_group() handler: terminate simulation. (exit all threads)
214 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
215 LiveProcess *p, ThreadContext *tc);
217 /// Target getpagesize() handler.
218 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
219 LiveProcess *p, ThreadContext *tc);
221 /// Target brk() handler: set brk address.
222 SyscallReturn brkFunc(SyscallDesc *desc, int num,
223 LiveProcess *p, ThreadContext *tc);
225 /// Target close() handler.
226 SyscallReturn closeFunc(SyscallDesc *desc, int num,
227 LiveProcess *p, ThreadContext *tc);
229 /// Target read() handler.
230 SyscallReturn readFunc(SyscallDesc *desc, int num,
231 LiveProcess *p, ThreadContext *tc);
233 /// Target write() handler.
234 SyscallReturn writeFunc(SyscallDesc *desc, int num,
235 LiveProcess *p, ThreadContext *tc);
237 /// Target lseek() handler.
238 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
239 LiveProcess *p, ThreadContext *tc);
241 /// Target _llseek() handler.
242 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
243 LiveProcess *p, ThreadContext *tc);
245 /// Target munmap() handler.
246 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
247 LiveProcess *p, ThreadContext *tc);
249 /// Target gethostname() handler.
250 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
251 LiveProcess *p, ThreadContext *tc);
253 /// Target getcwd() handler.
254 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
255 LiveProcess *p, ThreadContext *tc);
257 /// Target readlink() handler.
258 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
259 LiveProcess *p, ThreadContext *tc,
261 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
262 LiveProcess *p, ThreadContext *tc);
264 /// Target unlink() handler.
265 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
266 LiveProcess *p, ThreadContext *tc);
268 /// Target mkdir() handler.
269 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
270 LiveProcess *p, ThreadContext *tc);
272 /// Target rename() handler.
273 SyscallReturn renameFunc(SyscallDesc *desc, int num,
274 LiveProcess *p, ThreadContext *tc);
277 /// Target truncate() handler.
278 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
279 LiveProcess *p, ThreadContext *tc);
282 /// Target ftruncate() handler.
283 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
284 LiveProcess *p, ThreadContext *tc);
287 /// Target truncate64() handler.
288 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
289 LiveProcess *p, ThreadContext *tc);
291 /// Target ftruncate64() handler.
292 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
293 LiveProcess *p, ThreadContext *tc);
296 /// Target umask() handler.
297 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
298 LiveProcess *p, ThreadContext *tc);
301 /// Target chown() handler.
302 SyscallReturn chownFunc(SyscallDesc *desc, int num,
303 LiveProcess *p, ThreadContext *tc);
306 /// Target fchown() handler.
307 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
308 LiveProcess *p, ThreadContext *tc);
310 /// Target dup() handler.
311 SyscallReturn dupFunc(SyscallDesc *desc, int num,
312 LiveProcess *process, ThreadContext *tc);
314 /// Target fnctl() handler.
315 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
316 LiveProcess *process, ThreadContext *tc);
318 /// Target fcntl64() handler.
319 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
320 LiveProcess *process, ThreadContext *tc);
322 /// Target setuid() handler.
323 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
324 LiveProcess *p, ThreadContext *tc);
326 /// Target getpid() handler.
327 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
328 LiveProcess *p, ThreadContext *tc);
330 /// Target getuid() handler.
331 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
332 LiveProcess *p, ThreadContext *tc);
334 /// Target getgid() handler.
335 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
336 LiveProcess *p, ThreadContext *tc);
338 /// Target getppid() handler.
339 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
340 LiveProcess *p, ThreadContext *tc);
342 /// Target geteuid() handler.
343 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
344 LiveProcess *p, ThreadContext *tc);
346 /// Target getegid() handler.
347 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
348 LiveProcess *p, ThreadContext *tc);
350 /// Target clone() handler.
351 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
352 LiveProcess *p, ThreadContext *tc);
354 /// Target access() handler
355 SyscallReturn accessFunc(SyscallDesc *desc, int num,
356 LiveProcess *p, ThreadContext *tc);
357 SyscallReturn accessFunc(SyscallDesc *desc, int num,
358 LiveProcess *p, ThreadContext *tc,
361 /// Futex system call
362 /// Implemented by Daniel Sanchez
363 /// Used by printf's in multi-threaded apps
366 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
372 int index_timeout = 3;
374 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
375 int op = process->getSyscallArg(tc, index_op);
376 int val = process->getSyscallArg(tc, index_val);
377 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
379 std::map<uint64_t, std::list<ThreadContext *> * >
380 &futex_map = tc->getSystemPtr()->futexMap;
382 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
385 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
387 if (op == OS::TGT_FUTEX_WAIT) {
389 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
390 "we'll wait indefinitely");
393 uint8_t *buf = new uint8_t[sizeof(int)];
394 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
395 int mem_val = *((int *)buf);
399 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
400 "expected: %d\n", mem_val, val);
401 return -OS::TGT_EWOULDBLOCK;
404 // Queue the thread context
405 std::list<ThreadContext *> * tcWaitList;
406 if (futex_map.count(uaddr)) {
407 tcWaitList = futex_map.find(uaddr)->second;
409 tcWaitList = new std::list<ThreadContext *>();
410 futex_map.insert(std::pair< uint64_t,
411 std::list<ThreadContext *> * >(uaddr, tcWaitList));
413 tcWaitList->push_back(tc);
414 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
418 } else if (op == OS::TGT_FUTEX_WAKE){
420 std::list<ThreadContext *> * tcWaitList;
421 if (futex_map.count(uaddr)) {
422 tcWaitList = futex_map.find(uaddr)->second;
423 while (tcWaitList->size() > 0 && wokenUp < val) {
424 tcWaitList->front()->activate();
425 tcWaitList->pop_front();
428 if(tcWaitList->empty()) {
429 futex_map.erase(uaddr);
433 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
434 "thread contexts\n", wokenUp);
437 warn("sys_futex: op %d is not implemented, just returning...", op);
444 /// Pseudo Funcs - These functions use a different return convension,
445 /// returning a second value in a register other than the normal return register
446 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
447 LiveProcess *process, ThreadContext *tc);
449 /// Target getpidPseudo() handler.
450 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
451 LiveProcess *p, ThreadContext *tc);
453 /// Target getuidPseudo() handler.
454 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
455 LiveProcess *p, ThreadContext *tc);
457 /// Target getgidPseudo() handler.
458 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
459 LiveProcess *p, ThreadContext *tc);
462 /// A readable name for 1,000,000, for converting microseconds to seconds.
463 const int one_million = 1000000;
465 /// Approximate seconds since the epoch (1/1/1970). About a billion,
466 /// by my reckoning. We want to keep this a constant (not use the
467 /// real-world time) to keep simulations repeatable.
468 const unsigned seconds_since_epoch = 1000000000;
470 /// Helper function to convert current elapsed time to seconds and
472 template <class T1, class T2>
474 getElapsedTime(T1 &sec, T2 &usec)
476 int elapsed_usecs = curTick() / SimClock::Int::us;
477 sec = elapsed_usecs / one_million;
478 usec = elapsed_usecs % one_million;
481 //////////////////////////////////////////////////////////////////////
483 // The following emulation functions are generic, but need to be
484 // templated to account for differences in types, constants, etc.
486 //////////////////////////////////////////////////////////////////////
489 typedef struct stat hst_stat;
490 typedef struct stat hst_stat64;
492 typedef struct stat hst_stat;
493 typedef struct stat64 hst_stat64;
496 //// Helper function to convert a host stat buffer to a target stat
497 //// buffer. Also copies the target buffer out to the simulated
498 //// memory space. Used by stat(), fstat(), and lstat().
500 template <typename target_stat, typename host_stat>
502 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
504 using namespace TheISA;
509 tgt->st_dev = host->st_dev;
510 tgt->st_dev = TheISA::htog(tgt->st_dev);
511 tgt->st_ino = host->st_ino;
512 tgt->st_ino = TheISA::htog(tgt->st_ino);
513 tgt->st_mode = host->st_mode;
515 // Claim to be a character device
516 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
517 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
519 tgt->st_mode = TheISA::htog(tgt->st_mode);
520 tgt->st_nlink = host->st_nlink;
521 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
522 tgt->st_uid = host->st_uid;
523 tgt->st_uid = TheISA::htog(tgt->st_uid);
524 tgt->st_gid = host->st_gid;
525 tgt->st_gid = TheISA::htog(tgt->st_gid);
527 tgt->st_rdev = 0x880d;
529 tgt->st_rdev = host->st_rdev;
530 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
531 tgt->st_size = host->st_size;
532 tgt->st_size = TheISA::htog(tgt->st_size);
533 tgt->st_atimeX = host->st_atime;
534 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
535 tgt->st_mtimeX = host->st_mtime;
536 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
537 tgt->st_ctimeX = host->st_ctime;
538 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
539 // Force the block size to be 8k. This helps to ensure buffered io works
540 // consistently across different hosts.
541 tgt->st_blksize = 0x2000;
542 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
543 tgt->st_blocks = host->st_blocks;
544 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
549 template <typename target_stat, typename host_stat64>
551 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
553 using namespace TheISA;
555 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
556 #if defined(STAT_HAVE_NSEC)
557 tgt->st_atime_nsec = host->st_atime_nsec;
558 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
559 tgt->st_mtime_nsec = host->st_mtime_nsec;
560 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
561 tgt->st_ctime_nsec = host->st_ctime_nsec;
562 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
564 tgt->st_atime_nsec = 0;
565 tgt->st_mtime_nsec = 0;
566 tgt->st_ctime_nsec = 0;
570 //Here are a couple convenience functions
573 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
574 hst_stat *host, bool fakeTTY = false)
576 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
577 tgt_stat_buf tgt(addr);
578 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
584 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
585 hst_stat64 *host, bool fakeTTY = false)
587 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
588 tgt_stat_buf tgt(addr);
589 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
593 /// Target ioctl() handler. For the most part, programs call ioctl()
594 /// only to find out if their stdout is a tty, to determine whether to
595 /// do line or block buffering. We always claim that output fds are
596 /// not TTYs to provide repeatable results.
599 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
603 int fd = process->getSyscallArg(tc, index);
604 unsigned req = process->getSyscallArg(tc, index);
606 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
608 Process::FdMap *fdObj = process->sim_fd_obj(fd);
611 // doesn't map to any simulator fd: not a valid target fd
615 if (fdObj->driver != NULL) {
616 return fdObj->driver->ioctl(process, tc, req);
619 if (OS::isTtyReq(req)) {
623 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
624 fd, req, tc->pcState());
630 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
631 ThreadContext *tc, int index)
635 if (!tc->getMemProxy().tryReadString(path,
636 process->getSyscallArg(tc, index)))
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());
664 if (startswith(path, "/dev/")) {
665 std::string filename = path.substr(strlen("/dev/"));
666 if (filename == "sysdev0") {
667 // This is a memory-mapped high-resolution timer device on Alpha.
668 // We don't support it, so just punt.
669 warn("Ignoring open(%s, ...)\n", path);
673 EmulatedDriver *drv = process->findDriver(filename);
675 // the driver's open method will allocate a fd from the
676 // process if necessary.
677 return drv->open(process, tc, mode, hostFlags);
680 // fall through here for pass through to host devices, such as
686 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
687 startswith(path, "/platform/") || startswith(path, "/sys/")) {
688 // It's a proc/sys entry and requires special handling
689 fd = OS::openSpecialFile(path, process, tc);
690 local_errno = ENOENT;
693 fd = open(path.c_str(), hostFlags, mode);
700 return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
703 /// Target open() handler.
706 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
709 return openFunc<OS>(desc, callnum, process, tc, 0);
712 /// Target openat() handler.
715 openatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
719 int dirfd = process->getSyscallArg(tc, index);
720 if (dirfd != OS::TGT_AT_FDCWD)
721 warn("openat: first argument not AT_FDCWD; unlikely to work");
722 return openFunc<OS>(desc, callnum, process, tc, 1);
725 /// Target facessat() handler
728 faccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
732 int dirfd = process->getSyscallArg(tc, index);
733 if (dirfd != OS::TGT_AT_FDCWD)
734 warn("faccessat: first argument not AT_FDCWD; unlikely to work");
735 return accessFunc(desc, callnum, process, tc, 1);
738 /// Target readlinkat() handler
741 readlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
745 int dirfd = process->getSyscallArg(tc, index);
746 if (dirfd != OS::TGT_AT_FDCWD)
747 warn("openat: first argument not AT_FDCWD; unlikely to work");
748 return readlinkFunc(desc, callnum, process, tc, 1);
751 /// Target sysinfo() handler.
754 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
759 TypedBufferArg<typename OS::tgt_sysinfo>
760 sysinfo(process->getSyscallArg(tc, index));
762 sysinfo->uptime=seconds_since_epoch;
763 sysinfo->totalram=process->system->memSize();
765 sysinfo.copyOut(tc->getMemProxy());
770 /// Target chmod() handler.
773 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
779 if (!tc->getMemProxy().tryReadString(path,
780 process->getSyscallArg(tc, index))) {
784 uint32_t mode = process->getSyscallArg(tc, index);
787 // XXX translate mode flags via OS::something???
790 // Adjust path for current working directory
791 path = process->fullPath(path);
794 int result = chmod(path.c_str(), hostMode);
802 /// Target fchmod() handler.
805 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
809 int fd = process->getSyscallArg(tc, index);
810 if (fd < 0 || process->sim_fd(fd) < 0) {
811 // doesn't map to any simulator fd: not a valid target fd
815 uint32_t mode = process->getSyscallArg(tc, index);
818 // XXX translate mode flags via OS::someting???
822 int result = fchmod(process->sim_fd(fd), hostMode);
829 /// Target mremap() handler.
832 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
835 Addr start = process->getSyscallArg(tc, index);
836 uint64_t old_length = process->getSyscallArg(tc, index);
837 uint64_t new_length = process->getSyscallArg(tc, index);
838 uint64_t flags = process->getSyscallArg(tc, index);
839 uint64_t provided_address = 0;
840 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
842 if (use_provided_address)
843 provided_address = process->getSyscallArg(tc, index);
845 if ((start % TheISA::PageBytes != 0) ||
846 (provided_address % TheISA::PageBytes != 0)) {
847 warn("mremap failing: arguments not page aligned");
851 new_length = roundUp(new_length, TheISA::PageBytes);
853 if (new_length > old_length) {
854 if ((start + old_length) == process->mmap_end &&
855 (!use_provided_address || provided_address == start)) {
856 uint64_t diff = new_length - old_length;
857 process->allocateMem(process->mmap_end, diff);
858 process->mmap_end += diff;
861 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
862 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
865 uint64_t new_start = use_provided_address ?
866 provided_address : process->mmap_end;
867 process->pTable->remap(start, old_length, new_start);
868 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
869 new_start, new_start + new_length,
870 new_length - old_length);
871 // add on the remaining unallocated pages
872 process->allocateMem(new_start + old_length,
873 new_length - old_length,
874 use_provided_address /* clobber */);
875 if (!use_provided_address)
876 process->mmap_end += new_length;
877 if (use_provided_address &&
878 new_start + new_length > process->mmap_end) {
879 // something fishy going on here, at least notify the user
880 // @todo: increase mmap_end?
881 warn("mmap region limit exceeded with MREMAP_FIXED\n");
883 warn("returning %08p as start\n", new_start);
888 if (use_provided_address && provided_address != start)
889 process->pTable->remap(start, new_length, provided_address);
890 process->pTable->unmap(start + new_length, old_length - new_length);
891 return use_provided_address ? provided_address : start;
895 /// Target stat() handler.
898 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
904 if (!tc->getMemProxy().tryReadString(path,
905 process->getSyscallArg(tc, index))) {
908 Addr bufPtr = process->getSyscallArg(tc, index);
910 // Adjust path for current working directory
911 path = process->fullPath(path);
914 int result = stat(path.c_str(), &hostBuf);
919 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
925 /// Target stat64() handler.
928 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
934 if (!tc->getMemProxy().tryReadString(path,
935 process->getSyscallArg(tc, index)))
937 Addr bufPtr = process->getSyscallArg(tc, index);
939 // Adjust path for current working directory
940 path = process->fullPath(path);
944 int result = stat(path.c_str(), &hostBuf);
946 struct stat64 hostBuf;
947 int result = stat64(path.c_str(), &hostBuf);
953 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
959 /// Target fstatat64() handler.
962 fstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
966 int dirfd = process->getSyscallArg(tc, index);
967 if (dirfd != OS::TGT_AT_FDCWD)
968 warn("openat: first argument not AT_FDCWD; unlikely to work");
971 if (!tc->getMemProxy().tryReadString(path,
972 process->getSyscallArg(tc, index)))
974 Addr bufPtr = process->getSyscallArg(tc, index);
976 // Adjust path for current working directory
977 path = process->fullPath(path);
981 int result = stat(path.c_str(), &hostBuf);
983 struct stat64 hostBuf;
984 int result = stat64(path.c_str(), &hostBuf);
990 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
996 /// Target fstat64() handler.
999 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1003 int fd = process->getSyscallArg(tc, index);
1004 Addr bufPtr = process->getSyscallArg(tc, index);
1005 if (fd < 0 || process->sim_fd(fd) < 0) {
1006 // doesn't map to any simulator fd: not a valid target fd
1011 struct stat hostBuf;
1012 int result = fstat(process->sim_fd(fd), &hostBuf);
1014 struct stat64 hostBuf;
1015 int result = fstat64(process->sim_fd(fd), &hostBuf);
1021 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
1027 /// Target lstat() handler.
1030 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1036 if (!tc->getMemProxy().tryReadString(path,
1037 process->getSyscallArg(tc, index))) {
1040 Addr bufPtr = process->getSyscallArg(tc, index);
1042 // Adjust path for current working directory
1043 path = process->fullPath(path);
1045 struct stat hostBuf;
1046 int result = lstat(path.c_str(), &hostBuf);
1051 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1056 /// Target lstat64() handler.
1059 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1065 if (!tc->getMemProxy().tryReadString(path,
1066 process->getSyscallArg(tc, index))) {
1069 Addr bufPtr = process->getSyscallArg(tc, index);
1071 // Adjust path for current working directory
1072 path = process->fullPath(path);
1075 struct stat hostBuf;
1076 int result = lstat(path.c_str(), &hostBuf);
1078 struct stat64 hostBuf;
1079 int result = lstat64(path.c_str(), &hostBuf);
1085 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1090 /// Target fstat() handler.
1093 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1097 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1098 Addr bufPtr = process->getSyscallArg(tc, index);
1100 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1105 struct stat hostBuf;
1106 int result = fstat(fd, &hostBuf);
1111 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
1117 /// Target statfs() handler.
1120 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1126 if (!tc->getMemProxy().tryReadString(path,
1127 process->getSyscallArg(tc, index))) {
1130 Addr bufPtr = process->getSyscallArg(tc, index);
1132 // Adjust path for current working directory
1133 path = process->fullPath(path);
1135 struct statfs hostBuf;
1136 int result = statfs(path.c_str(), &hostBuf);
1141 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1147 /// Target fstatfs() handler.
1150 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1154 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1155 Addr bufPtr = process->getSyscallArg(tc, index);
1160 struct statfs hostBuf;
1161 int result = fstatfs(fd, &hostBuf);
1166 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1172 /// Target writev() handler.
1175 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1179 int fd = process->getSyscallArg(tc, index);
1180 if (fd < 0 || process->sim_fd(fd) < 0) {
1181 // doesn't map to any simulator fd: not a valid target fd
1185 SETranslatingPortProxy &p = tc->getMemProxy();
1186 uint64_t tiov_base = process->getSyscallArg(tc, index);
1187 size_t count = process->getSyscallArg(tc, index);
1188 struct iovec hiov[count];
1189 for (size_t i = 0; i < count; ++i) {
1190 typename OS::tgt_iovec tiov;
1192 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1193 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1194 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1195 hiov[i].iov_base = new char [hiov[i].iov_len];
1196 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1200 int result = writev(process->sim_fd(fd), hiov, count);
1202 for (size_t i = 0; i < count; ++i)
1203 delete [] (char *)hiov[i].iov_base;
1212 /// Target mmap() handler.
1214 /// We don't really handle mmap(). If the target is mmaping an
1215 /// anonymous region or /dev/zero, we can get away with doing basically
1216 /// nothing (since memory is initialized to zero and the simulator
1217 /// doesn't really check addresses anyway).
1221 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1224 Addr start = p->getSyscallArg(tc, index);
1225 uint64_t length = p->getSyscallArg(tc, index);
1226 index++; // int prot = p->getSyscallArg(tc, index);
1227 int flags = p->getSyscallArg(tc, index);
1228 int tgt_fd = p->getSyscallArg(tc, index);
1229 int offset = p->getSyscallArg(tc, index);
1231 if (length > 0x100000000ULL)
1232 warn("mmap length argument %#x is unreasonably large.\n", length);
1234 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1235 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1236 if (!fd_map || fd_map->fd < 0) {
1237 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1241 if (fd_map->filename != "/dev/zero") {
1242 // This is very likely broken, but leave a warning here
1243 // (rather than panic) in case /dev/zero is known by
1244 // another name on some platform
1245 warn("allowing mmap of file %s; mmap not supported on files"
1246 " other than /dev/zero\n", fd_map->filename);
1250 length = roundUp(length, TheISA::PageBytes);
1252 if ((start % TheISA::PageBytes) != 0 ||
1253 (offset % TheISA::PageBytes) != 0) {
1254 warn("mmap failing: arguments not page-aligned: "
1255 "start 0x%x offset 0x%x",
1260 // are we ok with clobbering existing mappings? only set this to
1261 // true if the user has been warned.
1262 bool clobber = false;
1264 // try to use the caller-provided address if there is one
1265 bool use_provided_address = (start != 0);
1267 if (use_provided_address) {
1268 // check to see if the desired address is already in use
1269 if (!p->pTable->isUnmapped(start, length)) {
1270 // there are existing mappings in the desired range
1271 // whether we clobber them or not depends on whether the caller
1272 // specified MAP_FIXED
1273 if (flags & OS::TGT_MAP_FIXED) {
1274 // MAP_FIXED specified: map attempt fails
1277 // MAP_FIXED not specified: ignore suggested start address
1278 warn("mmap: ignoring suggested map address 0x%x\n", start);
1279 use_provided_address = false;
1284 if (!use_provided_address) {
1285 // no address provided, or provided address unusable:
1286 // pick next address from our "mmap region"
1287 if (OS::mmapGrowsDown()) {
1288 start = p->mmap_end - length;
1289 p->mmap_end = start;
1291 start = p->mmap_end;
1292 p->mmap_end += length;
1296 p->allocateMem(start, length, clobber);
1301 /// Target getrlimit() handler.
1304 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1308 unsigned resource = process->getSyscallArg(tc, index);
1309 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1312 case OS::TGT_RLIMIT_STACK:
1313 // max stack size in bytes: make up a number (8MB for now)
1314 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1315 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1316 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1319 case OS::TGT_RLIMIT_DATA:
1320 // max data segment size in bytes: make up a number
1321 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1322 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1323 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1327 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1333 rlp.copyOut(tc->getMemProxy());
1337 /// Target gettimeofday() handler.
1340 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1344 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1346 getElapsedTime(tp->tv_sec, tp->tv_usec);
1347 tp->tv_sec += seconds_since_epoch;
1348 tp->tv_sec = TheISA::htog(tp->tv_sec);
1349 tp->tv_usec = TheISA::htog(tp->tv_usec);
1351 tp.copyOut(tc->getMemProxy());
1357 /// Target utimes() handler.
1360 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1366 if (!tc->getMemProxy().tryReadString(path,
1367 process->getSyscallArg(tc, index))) {
1371 TypedBufferArg<typename OS::timeval [2]>
1372 tp(process->getSyscallArg(tc, index));
1373 tp.copyIn(tc->getMemProxy());
1375 struct timeval hostTimeval[2];
1376 for (int i = 0; i < 2; ++i)
1378 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1379 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1382 // Adjust path for current working directory
1383 path = process->fullPath(path);
1385 int result = utimes(path.c_str(), hostTimeval);
1392 /// Target getrusage() function.
1395 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1399 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1400 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1402 rup->ru_utime.tv_sec = 0;
1403 rup->ru_utime.tv_usec = 0;
1404 rup->ru_stime.tv_sec = 0;
1405 rup->ru_stime.tv_usec = 0;
1413 rup->ru_inblock = 0;
1414 rup->ru_oublock = 0;
1417 rup->ru_nsignals = 0;
1422 case OS::TGT_RUSAGE_SELF:
1423 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1424 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1425 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1428 case OS::TGT_RUSAGE_CHILDREN:
1429 // do nothing. We have no child processes, so they take no time.
1433 // don't really handle THREAD or CHILDREN, but just warn and
1435 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1439 rup.copyOut(tc->getMemProxy());
1444 /// Target times() function.
1447 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1451 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1453 // Fill in the time structure (in clocks)
1454 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1455 bufp->tms_utime = clocks;
1456 bufp->tms_stime = 0;
1457 bufp->tms_cutime = 0;
1458 bufp->tms_cstime = 0;
1460 // Convert to host endianness
1461 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1464 bufp.copyOut(tc->getMemProxy());
1466 // Return clock ticks since system boot
1470 /// Target time() function.
1473 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1476 typename OS::time_t sec, usec;
1477 getElapsedTime(sec, usec);
1478 sec += seconds_since_epoch;
1481 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1483 typename OS::time_t t = sec;
1484 t = TheISA::htog(t);
1485 SETranslatingPortProxy &p = tc->getMemProxy();
1486 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1492 #endif // __SIM_SYSCALL_EMUL_HH__