2 * Copyright (c) 2012-2013, 2015 ARM Limited
3 * Copyright (c) 2015 Advanced Micro Devices, Inc.
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder. You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
15 * Copyright (c) 2003-2005 The Regents of The University of Michigan
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * Authors: Steve Reinhardt
45 #ifndef __SIM_SYSCALL_EMUL_HH__
46 #define __SIM_SYSCALL_EMUL_HH__
48 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
49 defined(__FreeBSD__) || defined(__CYGWIN__) || \
53 /// @file syscall_emul.hh
55 /// This file defines objects used to emulate syscalls from the target
56 /// application on the host machine.
59 #include <sys/fcntl.h> // for O_BINARY
69 #include "base/chunk_generator.hh"
70 #include "base/intmath.hh" // for RoundUp
71 #include "base/misc.hh"
72 #include "base/trace.hh"
73 #include "base/types.hh"
74 #include "config/the_isa.hh"
75 #include "cpu/base.hh"
76 #include "cpu/thread_context.hh"
77 #include "debug/SyscallVerbose.hh"
78 #include "mem/page_table.hh"
79 #include "sim/byteswap.hh"
80 #include "sim/emul_driver.hh"
81 #include "sim/process.hh"
82 #include "sim/syscall_emul_buf.hh"
83 #include "sim/syscallreturn.hh"
84 #include "sim/system.hh"
87 /// System call descriptor.
93 /// Typedef for target syscall handler functions.
94 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
95 LiveProcess *, ThreadContext *);
97 const char *name; //!< Syscall name (e.g., "open").
98 FuncPtr funcPtr; //!< Pointer to emulation function.
99 int flags; //!< Flags (see Flags enum).
100 bool warned; //!< Have we warned about unimplemented syscall?
102 /// Flag values for controlling syscall behavior.
104 /// Don't set return regs according to funcPtr return value.
105 /// Used for syscalls with non-standard return conventions
106 /// that explicitly set the ThreadContext regs (e.g.,
108 SuppressReturnValue = 1,
113 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
114 : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
118 /// Emulate the syscall. Public interface for calling through funcPtr.
119 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
121 /// Is the WarnOnce flag set?
122 bool warnOnce() const { return (flags & WarnOnce); }
126 //////////////////////////////////////////////////////////////////////
128 // The following emulation functions are generic enough that they
129 // don't need to be recompiled for different emulated OS's. They are
130 // defined in sim/syscall_emul.cc.
132 //////////////////////////////////////////////////////////////////////
135 /// Handler for unimplemented syscalls that we haven't thought about.
136 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
137 LiveProcess *p, ThreadContext *tc);
139 /// Handler for unimplemented syscalls that we never intend to
140 /// implement (signal handling, etc.) and should not affect the correct
141 /// behavior of the program. Print a warning only if the appropriate
142 /// trace flag is enabled. Return success to the target program.
143 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
144 LiveProcess *p, ThreadContext *tc);
146 /// Target exit() handler: terminate current context.
147 SyscallReturn exitFunc(SyscallDesc *desc, int num,
148 LiveProcess *p, ThreadContext *tc);
150 /// Target exit_group() handler: terminate simulation. (exit all threads)
151 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
152 LiveProcess *p, ThreadContext *tc);
154 /// Target getpagesize() handler.
155 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
156 LiveProcess *p, ThreadContext *tc);
158 /// Target brk() handler: set brk address.
159 SyscallReturn brkFunc(SyscallDesc *desc, int num,
160 LiveProcess *p, ThreadContext *tc);
162 /// Target close() handler.
163 SyscallReturn closeFunc(SyscallDesc *desc, int num,
164 LiveProcess *p, ThreadContext *tc);
166 /// Target read() handler.
167 SyscallReturn readFunc(SyscallDesc *desc, int num,
168 LiveProcess *p, ThreadContext *tc);
170 /// Target write() handler.
171 SyscallReturn writeFunc(SyscallDesc *desc, int num,
172 LiveProcess *p, ThreadContext *tc);
174 /// Target lseek() handler.
175 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
176 LiveProcess *p, ThreadContext *tc);
178 /// Target _llseek() handler.
179 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
180 LiveProcess *p, ThreadContext *tc);
182 /// Target munmap() handler.
183 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
184 LiveProcess *p, ThreadContext *tc);
186 /// Target gethostname() handler.
187 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
188 LiveProcess *p, ThreadContext *tc);
190 /// Target getcwd() handler.
191 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
192 LiveProcess *p, ThreadContext *tc);
194 /// Target readlink() handler.
195 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
196 LiveProcess *p, ThreadContext *tc,
198 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
199 LiveProcess *p, ThreadContext *tc);
201 /// Target unlink() handler.
202 SyscallReturn unlinkHelper(SyscallDesc *desc, int num,
203 LiveProcess *p, ThreadContext *tc,
205 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
206 LiveProcess *p, ThreadContext *tc);
208 /// Target mkdir() handler.
209 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
210 LiveProcess *p, ThreadContext *tc);
212 /// Target rename() handler.
213 SyscallReturn renameFunc(SyscallDesc *desc, int num,
214 LiveProcess *p, ThreadContext *tc);
217 /// Target truncate() handler.
218 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
219 LiveProcess *p, ThreadContext *tc);
222 /// Target ftruncate() handler.
223 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
224 LiveProcess *p, ThreadContext *tc);
227 /// Target truncate64() handler.
228 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
229 LiveProcess *p, ThreadContext *tc);
231 /// Target ftruncate64() handler.
232 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
233 LiveProcess *p, ThreadContext *tc);
236 /// Target umask() handler.
237 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
238 LiveProcess *p, ThreadContext *tc);
241 /// Target chown() handler.
242 SyscallReturn chownFunc(SyscallDesc *desc, int num,
243 LiveProcess *p, ThreadContext *tc);
246 /// Target fchown() handler.
247 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
248 LiveProcess *p, ThreadContext *tc);
250 /// Target dup() handler.
251 SyscallReturn dupFunc(SyscallDesc *desc, int num,
252 LiveProcess *process, ThreadContext *tc);
254 /// Target fnctl() handler.
255 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
256 LiveProcess *process, ThreadContext *tc);
258 /// Target fcntl64() handler.
259 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
260 LiveProcess *process, ThreadContext *tc);
262 /// Target setuid() handler.
263 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
264 LiveProcess *p, ThreadContext *tc);
266 /// Target getpid() handler.
267 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
268 LiveProcess *p, ThreadContext *tc);
270 /// Target getuid() handler.
271 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
272 LiveProcess *p, ThreadContext *tc);
274 /// Target getgid() handler.
275 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
276 LiveProcess *p, ThreadContext *tc);
278 /// Target getppid() handler.
279 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
280 LiveProcess *p, ThreadContext *tc);
282 /// Target geteuid() handler.
283 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
284 LiveProcess *p, ThreadContext *tc);
286 /// Target getegid() handler.
287 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
288 LiveProcess *p, ThreadContext *tc);
290 /// Target clone() handler.
291 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
292 LiveProcess *p, ThreadContext *tc);
294 /// Target access() handler
295 SyscallReturn accessFunc(SyscallDesc *desc, int num,
296 LiveProcess *p, ThreadContext *tc);
297 SyscallReturn accessFunc(SyscallDesc *desc, int num,
298 LiveProcess *p, ThreadContext *tc,
301 /// Futex system call
302 /// Implemented by Daniel Sanchez
303 /// Used by printf's in multi-threaded apps
306 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
312 int index_timeout = 3;
314 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
315 int op = process->getSyscallArg(tc, index_op);
316 int val = process->getSyscallArg(tc, index_val);
317 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
319 std::map<uint64_t, std::list<ThreadContext *> * >
320 &futex_map = tc->getSystemPtr()->futexMap;
322 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
325 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
327 if (op == OS::TGT_FUTEX_WAIT) {
329 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
330 "we'll wait indefinitely");
333 uint8_t *buf = new uint8_t[sizeof(int)];
334 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
335 int mem_val = *((int *)buf);
339 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
340 "expected: %d\n", mem_val, val);
341 return -OS::TGT_EWOULDBLOCK;
344 // Queue the thread context
345 std::list<ThreadContext *> * tcWaitList;
346 if (futex_map.count(uaddr)) {
347 tcWaitList = futex_map.find(uaddr)->second;
349 tcWaitList = new std::list<ThreadContext *>();
350 futex_map.insert(std::pair< uint64_t,
351 std::list<ThreadContext *> * >(uaddr, tcWaitList));
353 tcWaitList->push_back(tc);
354 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
358 } else if (op == OS::TGT_FUTEX_WAKE){
360 std::list<ThreadContext *> * tcWaitList;
361 if (futex_map.count(uaddr)) {
362 tcWaitList = futex_map.find(uaddr)->second;
363 while (tcWaitList->size() > 0 && wokenUp < val) {
364 tcWaitList->front()->activate();
365 tcWaitList->pop_front();
368 if(tcWaitList->empty()) {
369 futex_map.erase(uaddr);
373 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
374 "thread contexts\n", wokenUp);
377 warn("sys_futex: op %d is not implemented, just returning...", op);
384 /// Pseudo Funcs - These functions use a different return convension,
385 /// returning a second value in a register other than the normal return register
386 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
387 LiveProcess *process, ThreadContext *tc);
389 /// Target getpidPseudo() handler.
390 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
391 LiveProcess *p, ThreadContext *tc);
393 /// Target getuidPseudo() handler.
394 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
395 LiveProcess *p, ThreadContext *tc);
397 /// Target getgidPseudo() handler.
398 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
399 LiveProcess *p, ThreadContext *tc);
402 /// A readable name for 1,000,000, for converting microseconds to seconds.
403 const int one_million = 1000000;
404 /// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
405 const int one_billion = 1000000000;
407 /// Approximate seconds since the epoch (1/1/1970). About a billion,
408 /// by my reckoning. We want to keep this a constant (not use the
409 /// real-world time) to keep simulations repeatable.
410 const unsigned seconds_since_epoch = 1000000000;
412 /// Helper function to convert current elapsed time to seconds and
414 template <class T1, class T2>
416 getElapsedTimeMicro(T1 &sec, T2 &usec)
418 uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
419 sec = elapsed_usecs / one_million;
420 usec = elapsed_usecs % one_million;
423 /// Helper function to convert current elapsed time to seconds and
425 template <class T1, class T2>
427 getElapsedTimeNano(T1 &sec, T2 &nsec)
429 uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
430 sec = elapsed_nsecs / one_billion;
431 nsec = elapsed_nsecs % one_billion;
434 //////////////////////////////////////////////////////////////////////
436 // The following emulation functions are generic, but need to be
437 // templated to account for differences in types, constants, etc.
439 //////////////////////////////////////////////////////////////////////
442 typedef struct stat hst_stat;
443 typedef struct stat hst_stat64;
445 typedef struct stat hst_stat;
446 typedef struct stat64 hst_stat64;
449 //// Helper function to convert a host stat buffer to a target stat
450 //// buffer. Also copies the target buffer out to the simulated
451 //// memory space. Used by stat(), fstat(), and lstat().
453 template <typename target_stat, typename host_stat>
455 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
457 using namespace TheISA;
462 tgt->st_dev = host->st_dev;
463 tgt->st_dev = TheISA::htog(tgt->st_dev);
464 tgt->st_ino = host->st_ino;
465 tgt->st_ino = TheISA::htog(tgt->st_ino);
466 tgt->st_mode = host->st_mode;
468 // Claim to be a character device
469 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
470 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
472 tgt->st_mode = TheISA::htog(tgt->st_mode);
473 tgt->st_nlink = host->st_nlink;
474 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
475 tgt->st_uid = host->st_uid;
476 tgt->st_uid = TheISA::htog(tgt->st_uid);
477 tgt->st_gid = host->st_gid;
478 tgt->st_gid = TheISA::htog(tgt->st_gid);
480 tgt->st_rdev = 0x880d;
482 tgt->st_rdev = host->st_rdev;
483 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
484 tgt->st_size = host->st_size;
485 tgt->st_size = TheISA::htog(tgt->st_size);
486 tgt->st_atimeX = host->st_atime;
487 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
488 tgt->st_mtimeX = host->st_mtime;
489 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
490 tgt->st_ctimeX = host->st_ctime;
491 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
492 // Force the block size to be 8k. This helps to ensure buffered io works
493 // consistently across different hosts.
494 tgt->st_blksize = 0x2000;
495 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
496 tgt->st_blocks = host->st_blocks;
497 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
502 template <typename target_stat, typename host_stat64>
504 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
506 using namespace TheISA;
508 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
509 #if defined(STAT_HAVE_NSEC)
510 tgt->st_atime_nsec = host->st_atime_nsec;
511 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
512 tgt->st_mtime_nsec = host->st_mtime_nsec;
513 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
514 tgt->st_ctime_nsec = host->st_ctime_nsec;
515 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
517 tgt->st_atime_nsec = 0;
518 tgt->st_mtime_nsec = 0;
519 tgt->st_ctime_nsec = 0;
523 //Here are a couple convenience functions
526 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
527 hst_stat *host, bool fakeTTY = false)
529 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
530 tgt_stat_buf tgt(addr);
531 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
537 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
538 hst_stat64 *host, bool fakeTTY = false)
540 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
541 tgt_stat_buf tgt(addr);
542 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
546 /// Target ioctl() handler. For the most part, programs call ioctl()
547 /// only to find out if their stdout is a tty, to determine whether to
548 /// do line or block buffering. We always claim that output fds are
549 /// not TTYs to provide repeatable results.
552 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
556 int tgt_fd = process->getSyscallArg(tc, index);
557 unsigned req = process->getSyscallArg(tc, index);
559 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
561 FDEntry *fde = process->getFDEntry(tgt_fd);
564 // doesn't map to any simulator fd: not a valid target fd
568 if (fde->driver != NULL) {
569 return fde->driver->ioctl(process, tc, req);
572 if (OS::isTtyReq(req)) {
576 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
577 tgt_fd, req, tc->pcState());
583 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
584 ThreadContext *tc, int index)
588 if (!tc->getMemProxy().tryReadString(path,
589 process->getSyscallArg(tc, index)))
592 int tgtFlags = process->getSyscallArg(tc, index);
593 int mode = process->getSyscallArg(tc, index);
596 // translate open flags
597 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
598 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
599 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
600 hostFlags |= OS::openFlagTable[i].hostFlag;
604 // any target flags left?
606 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
609 hostFlags |= O_BINARY;
612 // Adjust path for current working directory
613 path = process->fullPath(path);
615 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
617 if (startswith(path, "/dev/")) {
618 std::string filename = path.substr(strlen("/dev/"));
619 if (filename == "sysdev0") {
620 // This is a memory-mapped high-resolution timer device on Alpha.
621 // We don't support it, so just punt.
622 warn("Ignoring open(%s, ...)\n", path);
626 EmulatedDriver *drv = process->findDriver(filename);
628 // the driver's open method will allocate a fd from the
629 // process if necessary.
630 return drv->open(process, tc, mode, hostFlags);
633 // fall through here for pass through to host devices, such as
639 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
640 startswith(path, "/platform/") || startswith(path, "/sys/")) {
641 // It's a proc/sys entry and requires special handling
642 fd = OS::openSpecialFile(path, process, tc);
643 local_errno = ENOENT;
646 fd = open(path.c_str(), hostFlags, mode);
653 return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
656 /// Target open() handler.
659 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
662 return openFunc<OS>(desc, callnum, process, tc, 0);
665 /// Target openat() handler.
668 openatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
672 int dirfd = process->getSyscallArg(tc, index);
673 if (dirfd != OS::TGT_AT_FDCWD)
674 warn("openat: first argument not AT_FDCWD; unlikely to work");
675 return openFunc<OS>(desc, callnum, process, tc, 1);
678 /// Target unlinkat() handler.
681 unlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
685 int dirfd = process->getSyscallArg(tc, index);
686 if (dirfd != OS::TGT_AT_FDCWD)
687 warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
689 return unlinkHelper(desc, callnum, process, tc, 1);
692 /// Target facessat() handler
695 faccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
699 int dirfd = process->getSyscallArg(tc, index);
700 if (dirfd != OS::TGT_AT_FDCWD)
701 warn("faccessat: first argument not AT_FDCWD; unlikely to work");
702 return accessFunc(desc, callnum, process, tc, 1);
705 /// Target readlinkat() handler
708 readlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
712 int dirfd = process->getSyscallArg(tc, index);
713 if (dirfd != OS::TGT_AT_FDCWD)
714 warn("openat: first argument not AT_FDCWD; unlikely to work");
715 return readlinkFunc(desc, callnum, process, tc, 1);
718 /// Target renameat() handler.
721 renameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
726 int olddirfd = process->getSyscallArg(tc, index);
727 if (olddirfd != OS::TGT_AT_FDCWD)
728 warn("renameat: first argument not AT_FDCWD; unlikely to work");
730 std::string old_name;
732 if (!tc->getMemProxy().tryReadString(old_name,
733 process->getSyscallArg(tc, index)))
736 int newdirfd = process->getSyscallArg(tc, index);
737 if (newdirfd != OS::TGT_AT_FDCWD)
738 warn("renameat: third argument not AT_FDCWD; unlikely to work");
740 std::string new_name;
742 if (!tc->getMemProxy().tryReadString(new_name,
743 process->getSyscallArg(tc, index)))
746 // Adjust path for current working directory
747 old_name = process->fullPath(old_name);
748 new_name = process->fullPath(new_name);
750 int result = rename(old_name.c_str(), new_name.c_str());
751 return (result == -1) ? -errno : result;
754 /// Target sysinfo() handler.
757 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
762 TypedBufferArg<typename OS::tgt_sysinfo>
763 sysinfo(process->getSyscallArg(tc, index));
765 sysinfo->uptime=seconds_since_epoch;
766 sysinfo->totalram=process->system->memSize();
768 sysinfo.copyOut(tc->getMemProxy());
773 /// Target chmod() handler.
776 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
782 if (!tc->getMemProxy().tryReadString(path,
783 process->getSyscallArg(tc, index))) {
787 uint32_t mode = process->getSyscallArg(tc, index);
790 // XXX translate mode flags via OS::something???
793 // Adjust path for current working directory
794 path = process->fullPath(path);
797 int result = chmod(path.c_str(), hostMode);
805 /// Target fchmod() handler.
808 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
812 int tgt_fd = process->getSyscallArg(tc, index);
813 uint32_t mode = process->getSyscallArg(tc, index);
815 int sim_fd = process->getSimFD(tgt_fd);
821 // XXX translate mode flags via OS::someting???
825 int result = fchmod(sim_fd, hostMode);
832 /// Target mremap() handler.
835 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
838 Addr start = process->getSyscallArg(tc, index);
839 uint64_t old_length = process->getSyscallArg(tc, index);
840 uint64_t new_length = process->getSyscallArg(tc, index);
841 uint64_t flags = process->getSyscallArg(tc, index);
842 uint64_t provided_address = 0;
843 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
845 if (use_provided_address)
846 provided_address = process->getSyscallArg(tc, index);
848 if ((start % TheISA::PageBytes != 0) ||
849 (provided_address % TheISA::PageBytes != 0)) {
850 warn("mremap failing: arguments not page aligned");
854 new_length = roundUp(new_length, TheISA::PageBytes);
856 if (new_length > old_length) {
857 if ((start + old_length) == process->mmap_end &&
858 (!use_provided_address || provided_address == start)) {
859 uint64_t diff = new_length - old_length;
860 process->allocateMem(process->mmap_end, diff);
861 process->mmap_end += diff;
864 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
865 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
868 uint64_t new_start = use_provided_address ?
869 provided_address : process->mmap_end;
870 process->pTable->remap(start, old_length, new_start);
871 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
872 new_start, new_start + new_length,
873 new_length - old_length);
874 // add on the remaining unallocated pages
875 process->allocateMem(new_start + old_length,
876 new_length - old_length,
877 use_provided_address /* clobber */);
878 if (!use_provided_address)
879 process->mmap_end += new_length;
880 if (use_provided_address &&
881 new_start + new_length > process->mmap_end) {
882 // something fishy going on here, at least notify the user
883 // @todo: increase mmap_end?
884 warn("mmap region limit exceeded with MREMAP_FIXED\n");
886 warn("returning %08p as start\n", new_start);
891 if (use_provided_address && provided_address != start)
892 process->pTable->remap(start, new_length, provided_address);
893 process->pTable->unmap(start + new_length, old_length - new_length);
894 return use_provided_address ? provided_address : start;
898 /// Target stat() handler.
901 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
907 if (!tc->getMemProxy().tryReadString(path,
908 process->getSyscallArg(tc, index))) {
911 Addr bufPtr = process->getSyscallArg(tc, index);
913 // Adjust path for current working directory
914 path = process->fullPath(path);
917 int result = stat(path.c_str(), &hostBuf);
922 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
928 /// Target stat64() handler.
931 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
937 if (!tc->getMemProxy().tryReadString(path,
938 process->getSyscallArg(tc, index)))
940 Addr bufPtr = process->getSyscallArg(tc, index);
942 // Adjust path for current working directory
943 path = process->fullPath(path);
947 int result = stat(path.c_str(), &hostBuf);
949 struct stat64 hostBuf;
950 int result = stat64(path.c_str(), &hostBuf);
956 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
962 /// Target fstatat64() handler.
965 fstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
969 int dirfd = process->getSyscallArg(tc, index);
970 if (dirfd != OS::TGT_AT_FDCWD)
971 warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
974 if (!tc->getMemProxy().tryReadString(path,
975 process->getSyscallArg(tc, index)))
977 Addr bufPtr = process->getSyscallArg(tc, index);
979 // Adjust path for current working directory
980 path = process->fullPath(path);
984 int result = stat(path.c_str(), &hostBuf);
986 struct stat64 hostBuf;
987 int result = stat64(path.c_str(), &hostBuf);
993 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
999 /// Target fstat64() handler.
1002 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1006 int tgt_fd = process->getSyscallArg(tc, index);
1007 Addr bufPtr = process->getSyscallArg(tc, index);
1009 int sim_fd = process->getSimFD(tgt_fd);
1014 struct stat hostBuf;
1015 int result = fstat(sim_fd, &hostBuf);
1017 struct stat64 hostBuf;
1018 int result = fstat64(sim_fd, &hostBuf);
1024 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1030 /// Target lstat() handler.
1033 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1039 if (!tc->getMemProxy().tryReadString(path,
1040 process->getSyscallArg(tc, index))) {
1043 Addr bufPtr = process->getSyscallArg(tc, index);
1045 // Adjust path for current working directory
1046 path = process->fullPath(path);
1048 struct stat hostBuf;
1049 int result = lstat(path.c_str(), &hostBuf);
1054 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1059 /// Target lstat64() handler.
1062 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1068 if (!tc->getMemProxy().tryReadString(path,
1069 process->getSyscallArg(tc, index))) {
1072 Addr bufPtr = process->getSyscallArg(tc, index);
1074 // Adjust path for current working directory
1075 path = process->fullPath(path);
1078 struct stat hostBuf;
1079 int result = lstat(path.c_str(), &hostBuf);
1081 struct stat64 hostBuf;
1082 int result = lstat64(path.c_str(), &hostBuf);
1088 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1093 /// Target fstat() handler.
1096 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1100 int tgt_fd = process->getSyscallArg(tc, index);
1101 Addr bufPtr = process->getSyscallArg(tc, index);
1103 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", tgt_fd);
1105 int sim_fd = process->getSimFD(tgt_fd);
1109 struct stat hostBuf;
1110 int result = fstat(sim_fd, &hostBuf);
1115 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1121 /// Target statfs() handler.
1124 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1130 if (!tc->getMemProxy().tryReadString(path,
1131 process->getSyscallArg(tc, index))) {
1134 Addr bufPtr = process->getSyscallArg(tc, index);
1136 // Adjust path for current working directory
1137 path = process->fullPath(path);
1139 struct statfs hostBuf;
1140 int result = statfs(path.c_str(), &hostBuf);
1145 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1151 /// Target fstatfs() handler.
1154 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1158 int tgt_fd = process->getSyscallArg(tc, index);
1159 Addr bufPtr = process->getSyscallArg(tc, index);
1161 int sim_fd = process->getSimFD(tgt_fd);
1165 struct statfs hostBuf;
1166 int result = fstatfs(sim_fd, &hostBuf);
1171 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1177 /// Target writev() handler.
1180 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1184 int tgt_fd = process->getSyscallArg(tc, index);
1186 int sim_fd = process->getSimFD(tgt_fd);
1190 SETranslatingPortProxy &p = tc->getMemProxy();
1191 uint64_t tiov_base = process->getSyscallArg(tc, index);
1192 size_t count = process->getSyscallArg(tc, index);
1193 struct iovec hiov[count];
1194 for (size_t i = 0; i < count; ++i) {
1195 typename OS::tgt_iovec tiov;
1197 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1198 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1199 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1200 hiov[i].iov_base = new char [hiov[i].iov_len];
1201 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1205 int result = writev(sim_fd, hiov, count);
1207 for (size_t i = 0; i < count; ++i)
1208 delete [] (char *)hiov[i].iov_base;
1217 /// Target mmap() handler.
1219 /// We don't really handle mmap(). If the target is mmaping an
1220 /// anonymous region or /dev/zero, we can get away with doing basically
1221 /// nothing (since memory is initialized to zero and the simulator
1222 /// doesn't really check addresses anyway).
1226 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1229 Addr start = p->getSyscallArg(tc, index);
1230 uint64_t length = p->getSyscallArg(tc, index);
1231 index++; // int prot = p->getSyscallArg(tc, index);
1232 int flags = p->getSyscallArg(tc, index);
1233 int tgt_fd = p->getSyscallArg(tc, index);
1234 int offset = p->getSyscallArg(tc, index);
1236 if (length > 0x100000000ULL)
1237 warn("mmap length argument %#x is unreasonably large.\n", length);
1239 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1240 FDEntry *fde = p->getFDEntry(tgt_fd);
1241 if (!fde || fde->fd < 0) {
1242 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1246 if (fde->filename != "/dev/zero") {
1247 // This is very likely broken, but leave a warning here
1248 // (rather than panic) in case /dev/zero is known by
1249 // another name on some platform
1250 warn("allowing mmap of file %s; mmap not supported on files"
1251 " other than /dev/zero\n", fde->filename);
1255 length = roundUp(length, TheISA::PageBytes);
1257 if ((start % TheISA::PageBytes) != 0 ||
1258 (offset % TheISA::PageBytes) != 0) {
1259 warn("mmap failing: arguments not page-aligned: "
1260 "start 0x%x offset 0x%x",
1265 // are we ok with clobbering existing mappings? only set this to
1266 // true if the user has been warned.
1267 bool clobber = false;
1269 // try to use the caller-provided address if there is one
1270 bool use_provided_address = (start != 0);
1272 if (use_provided_address) {
1273 // check to see if the desired address is already in use
1274 if (!p->pTable->isUnmapped(start, length)) {
1275 // there are existing mappings in the desired range
1276 // whether we clobber them or not depends on whether the caller
1277 // specified MAP_FIXED
1278 if (flags & OS::TGT_MAP_FIXED) {
1279 // MAP_FIXED specified: map attempt fails
1282 // MAP_FIXED not specified: ignore suggested start address
1283 warn("mmap: ignoring suggested map address 0x%x\n", start);
1284 use_provided_address = false;
1289 if (!use_provided_address) {
1290 // no address provided, or provided address unusable:
1291 // pick next address from our "mmap region"
1292 if (OS::mmapGrowsDown()) {
1293 start = p->mmap_end - length;
1294 p->mmap_end = start;
1296 start = p->mmap_end;
1297 p->mmap_end += length;
1301 p->allocateMem(start, length, clobber);
1306 /// Target getrlimit() handler.
1309 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1313 unsigned resource = process->getSyscallArg(tc, index);
1314 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1317 case OS::TGT_RLIMIT_STACK:
1318 // max stack size in bytes: make up a number (8MB for now)
1319 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1320 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1321 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1324 case OS::TGT_RLIMIT_DATA:
1325 // max data segment size in bytes: make up a number
1326 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1327 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1328 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1332 warn("getrlimit: unimplemented resource %d", resource);
1337 rlp.copyOut(tc->getMemProxy());
1341 /// Target clock_gettime() function.
1344 clock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1347 //int clk_id = p->getSyscallArg(tc, index);
1348 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
1350 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
1351 tp->tv_sec += seconds_since_epoch;
1352 tp->tv_sec = TheISA::htog(tp->tv_sec);
1353 tp->tv_nsec = TheISA::htog(tp->tv_nsec);
1355 tp.copyOut(tc->getMemProxy());
1360 /// Target gettimeofday() handler.
1363 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1367 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1369 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1370 tp->tv_sec += seconds_since_epoch;
1371 tp->tv_sec = TheISA::htog(tp->tv_sec);
1372 tp->tv_usec = TheISA::htog(tp->tv_usec);
1374 tp.copyOut(tc->getMemProxy());
1380 /// Target utimes() handler.
1383 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1389 if (!tc->getMemProxy().tryReadString(path,
1390 process->getSyscallArg(tc, index))) {
1394 TypedBufferArg<typename OS::timeval [2]>
1395 tp(process->getSyscallArg(tc, index));
1396 tp.copyIn(tc->getMemProxy());
1398 struct timeval hostTimeval[2];
1399 for (int i = 0; i < 2; ++i)
1401 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1402 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1405 // Adjust path for current working directory
1406 path = process->fullPath(path);
1408 int result = utimes(path.c_str(), hostTimeval);
1415 /// Target getrusage() function.
1418 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1422 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1423 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1425 rup->ru_utime.tv_sec = 0;
1426 rup->ru_utime.tv_usec = 0;
1427 rup->ru_stime.tv_sec = 0;
1428 rup->ru_stime.tv_usec = 0;
1436 rup->ru_inblock = 0;
1437 rup->ru_oublock = 0;
1440 rup->ru_nsignals = 0;
1445 case OS::TGT_RUSAGE_SELF:
1446 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1447 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1448 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1451 case OS::TGT_RUSAGE_CHILDREN:
1452 // do nothing. We have no child processes, so they take no time.
1456 // don't really handle THREAD or CHILDREN, but just warn and
1458 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1462 rup.copyOut(tc->getMemProxy());
1467 /// Target times() function.
1470 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1474 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1476 // Fill in the time structure (in clocks)
1477 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1478 bufp->tms_utime = clocks;
1479 bufp->tms_stime = 0;
1480 bufp->tms_cutime = 0;
1481 bufp->tms_cstime = 0;
1483 // Convert to host endianness
1484 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1487 bufp.copyOut(tc->getMemProxy());
1489 // Return clock ticks since system boot
1493 /// Target time() function.
1496 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1499 typename OS::time_t sec, usec;
1500 getElapsedTimeMicro(sec, usec);
1501 sec += seconds_since_epoch;
1504 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1506 typename OS::time_t t = sec;
1507 t = TheISA::htog(t);
1508 SETranslatingPortProxy &p = tc->getMemProxy();
1509 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1515 #endif // __SIM_SYSCALL_EMUL_HH__