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 "sim/byteswap.hh"
79 #include "sim/emul_driver.hh"
80 #include "sim/process.hh"
81 #include "sim/syscall_emul_buf.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 //////////////////////////////////////////////////////////////////////
122 // The following emulation functions are generic enough that they
123 // don't need to be recompiled for different emulated OS's. They are
124 // defined in sim/syscall_emul.cc.
126 //////////////////////////////////////////////////////////////////////
129 /// Handler for unimplemented syscalls that we haven't thought about.
130 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
131 LiveProcess *p, ThreadContext *tc);
133 /// Handler for unimplemented syscalls that we never intend to
134 /// implement (signal handling, etc.) and should not affect the correct
135 /// behavior of the program. Print a warning only if the appropriate
136 /// trace flag is enabled. Return success to the target program.
137 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
138 LiveProcess *p, ThreadContext *tc);
139 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
140 LiveProcess *p, ThreadContext *tc);
142 /// Target exit() handler: terminate current context.
143 SyscallReturn exitFunc(SyscallDesc *desc, int num,
144 LiveProcess *p, ThreadContext *tc);
146 /// Target exit_group() handler: terminate simulation. (exit all threads)
147 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
148 LiveProcess *p, ThreadContext *tc);
150 /// Target getpagesize() handler.
151 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
152 LiveProcess *p, ThreadContext *tc);
154 /// Target brk() handler: set brk address.
155 SyscallReturn brkFunc(SyscallDesc *desc, int num,
156 LiveProcess *p, ThreadContext *tc);
158 /// Target close() handler.
159 SyscallReturn closeFunc(SyscallDesc *desc, int num,
160 LiveProcess *p, ThreadContext *tc);
162 /// Target read() handler.
163 SyscallReturn readFunc(SyscallDesc *desc, int num,
164 LiveProcess *p, ThreadContext *tc);
166 /// Target write() handler.
167 SyscallReturn writeFunc(SyscallDesc *desc, int num,
168 LiveProcess *p, ThreadContext *tc);
170 /// Target lseek() handler.
171 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
172 LiveProcess *p, ThreadContext *tc);
174 /// Target _llseek() handler.
175 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
176 LiveProcess *p, ThreadContext *tc);
178 /// Target munmap() handler.
179 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
180 LiveProcess *p, ThreadContext *tc);
182 /// Target gethostname() handler.
183 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
184 LiveProcess *p, ThreadContext *tc);
186 /// Target getcwd() handler.
187 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
188 LiveProcess *p, ThreadContext *tc);
190 /// Target readlink() handler.
191 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
192 LiveProcess *p, ThreadContext *tc,
194 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
195 LiveProcess *p, ThreadContext *tc);
197 /// Target unlink() handler.
198 SyscallReturn unlinkHelper(SyscallDesc *desc, int num,
199 LiveProcess *p, ThreadContext *tc,
201 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
202 LiveProcess *p, ThreadContext *tc);
204 /// Target mkdir() handler.
205 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
206 LiveProcess *p, ThreadContext *tc);
208 /// Target rename() handler.
209 SyscallReturn renameFunc(SyscallDesc *desc, int num,
210 LiveProcess *p, ThreadContext *tc);
213 /// Target truncate() handler.
214 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
215 LiveProcess *p, ThreadContext *tc);
218 /// Target ftruncate() handler.
219 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
220 LiveProcess *p, ThreadContext *tc);
223 /// Target truncate64() handler.
224 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
225 LiveProcess *p, ThreadContext *tc);
227 /// Target ftruncate64() handler.
228 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
229 LiveProcess *p, ThreadContext *tc);
232 /// Target umask() handler.
233 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
234 LiveProcess *p, ThreadContext *tc);
237 /// Target chown() handler.
238 SyscallReturn chownFunc(SyscallDesc *desc, int num,
239 LiveProcess *p, ThreadContext *tc);
242 /// Target fchown() handler.
243 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
244 LiveProcess *p, ThreadContext *tc);
246 /// Target dup() handler.
247 SyscallReturn dupFunc(SyscallDesc *desc, int num,
248 LiveProcess *process, ThreadContext *tc);
250 /// Target fnctl() handler.
251 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
252 LiveProcess *process, ThreadContext *tc);
254 /// Target fcntl64() handler.
255 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
256 LiveProcess *process, ThreadContext *tc);
258 /// Target setuid() handler.
259 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
260 LiveProcess *p, ThreadContext *tc);
262 /// Target getpid() handler.
263 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
264 LiveProcess *p, ThreadContext *tc);
266 /// Target getuid() handler.
267 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
268 LiveProcess *p, ThreadContext *tc);
270 /// Target getgid() handler.
271 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
272 LiveProcess *p, ThreadContext *tc);
274 /// Target getppid() handler.
275 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
276 LiveProcess *p, ThreadContext *tc);
278 /// Target geteuid() handler.
279 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
280 LiveProcess *p, ThreadContext *tc);
282 /// Target getegid() handler.
283 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
284 LiveProcess *p, ThreadContext *tc);
286 /// Target clone() handler.
287 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
288 LiveProcess *p, ThreadContext *tc);
290 /// Target access() handler
291 SyscallReturn accessFunc(SyscallDesc *desc, int num,
292 LiveProcess *p, ThreadContext *tc);
293 SyscallReturn accessFunc(SyscallDesc *desc, int num,
294 LiveProcess *p, ThreadContext *tc,
297 /// Futex system call
298 /// Implemented by Daniel Sanchez
299 /// Used by printf's in multi-threaded apps
302 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
308 int index_timeout = 3;
310 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
311 int op = process->getSyscallArg(tc, index_op);
312 int val = process->getSyscallArg(tc, index_val);
313 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
315 std::map<uint64_t, std::list<ThreadContext *> * >
316 &futex_map = tc->getSystemPtr()->futexMap;
318 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
321 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
323 if (op == OS::TGT_FUTEX_WAIT) {
325 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
326 "we'll wait indefinitely");
329 uint8_t *buf = new uint8_t[sizeof(int)];
330 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
331 int mem_val = *((int *)buf);
335 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
336 "expected: %d\n", mem_val, val);
337 return -OS::TGT_EWOULDBLOCK;
340 // Queue the thread context
341 std::list<ThreadContext *> * tcWaitList;
342 if (futex_map.count(uaddr)) {
343 tcWaitList = futex_map.find(uaddr)->second;
345 tcWaitList = new std::list<ThreadContext *>();
346 futex_map.insert(std::pair< uint64_t,
347 std::list<ThreadContext *> * >(uaddr, tcWaitList));
349 tcWaitList->push_back(tc);
350 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
354 } else if (op == OS::TGT_FUTEX_WAKE){
356 std::list<ThreadContext *> * tcWaitList;
357 if (futex_map.count(uaddr)) {
358 tcWaitList = futex_map.find(uaddr)->second;
359 while (tcWaitList->size() > 0 && wokenUp < val) {
360 tcWaitList->front()->activate();
361 tcWaitList->pop_front();
364 if(tcWaitList->empty()) {
365 futex_map.erase(uaddr);
369 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
370 "thread contexts\n", wokenUp);
373 warn("sys_futex: op %d is not implemented, just returning...", op);
380 /// Pseudo Funcs - These functions use a different return convension,
381 /// returning a second value in a register other than the normal return register
382 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
383 LiveProcess *process, ThreadContext *tc);
385 /// Target getpidPseudo() handler.
386 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
387 LiveProcess *p, ThreadContext *tc);
389 /// Target getuidPseudo() handler.
390 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
391 LiveProcess *p, ThreadContext *tc);
393 /// Target getgidPseudo() handler.
394 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
395 LiveProcess *p, ThreadContext *tc);
398 /// A readable name for 1,000,000, for converting microseconds to seconds.
399 const int one_million = 1000000;
401 /// Approximate seconds since the epoch (1/1/1970). About a billion,
402 /// by my reckoning. We want to keep this a constant (not use the
403 /// real-world time) to keep simulations repeatable.
404 const unsigned seconds_since_epoch = 1000000000;
406 /// Helper function to convert current elapsed time to seconds and
408 template <class T1, class T2>
410 getElapsedTime(T1 &sec, T2 &usec)
412 int elapsed_usecs = curTick() / SimClock::Int::us;
413 sec = elapsed_usecs / one_million;
414 usec = elapsed_usecs % one_million;
417 //////////////////////////////////////////////////////////////////////
419 // The following emulation functions are generic, but need to be
420 // templated to account for differences in types, constants, etc.
422 //////////////////////////////////////////////////////////////////////
425 typedef struct stat hst_stat;
426 typedef struct stat hst_stat64;
428 typedef struct stat hst_stat;
429 typedef struct stat64 hst_stat64;
432 //// Helper function to convert a host stat buffer to a target stat
433 //// buffer. Also copies the target buffer out to the simulated
434 //// memory space. Used by stat(), fstat(), and lstat().
436 template <typename target_stat, typename host_stat>
438 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
440 using namespace TheISA;
445 tgt->st_dev = host->st_dev;
446 tgt->st_dev = TheISA::htog(tgt->st_dev);
447 tgt->st_ino = host->st_ino;
448 tgt->st_ino = TheISA::htog(tgt->st_ino);
449 tgt->st_mode = host->st_mode;
451 // Claim to be a character device
452 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
453 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
455 tgt->st_mode = TheISA::htog(tgt->st_mode);
456 tgt->st_nlink = host->st_nlink;
457 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
458 tgt->st_uid = host->st_uid;
459 tgt->st_uid = TheISA::htog(tgt->st_uid);
460 tgt->st_gid = host->st_gid;
461 tgt->st_gid = TheISA::htog(tgt->st_gid);
463 tgt->st_rdev = 0x880d;
465 tgt->st_rdev = host->st_rdev;
466 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
467 tgt->st_size = host->st_size;
468 tgt->st_size = TheISA::htog(tgt->st_size);
469 tgt->st_atimeX = host->st_atime;
470 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
471 tgt->st_mtimeX = host->st_mtime;
472 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
473 tgt->st_ctimeX = host->st_ctime;
474 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
475 // Force the block size to be 8k. This helps to ensure buffered io works
476 // consistently across different hosts.
477 tgt->st_blksize = 0x2000;
478 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
479 tgt->st_blocks = host->st_blocks;
480 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
485 template <typename target_stat, typename host_stat64>
487 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
489 using namespace TheISA;
491 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
492 #if defined(STAT_HAVE_NSEC)
493 tgt->st_atime_nsec = host->st_atime_nsec;
494 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
495 tgt->st_mtime_nsec = host->st_mtime_nsec;
496 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
497 tgt->st_ctime_nsec = host->st_ctime_nsec;
498 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
500 tgt->st_atime_nsec = 0;
501 tgt->st_mtime_nsec = 0;
502 tgt->st_ctime_nsec = 0;
506 //Here are a couple convenience functions
509 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
510 hst_stat *host, bool fakeTTY = false)
512 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
513 tgt_stat_buf tgt(addr);
514 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
520 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
521 hst_stat64 *host, bool fakeTTY = false)
523 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
524 tgt_stat_buf tgt(addr);
525 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
529 /// Target ioctl() handler. For the most part, programs call ioctl()
530 /// only to find out if their stdout is a tty, to determine whether to
531 /// do line or block buffering. We always claim that output fds are
532 /// not TTYs to provide repeatable results.
535 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
539 int fd = process->getSyscallArg(tc, index);
540 unsigned req = process->getSyscallArg(tc, index);
542 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
544 Process::FdMap *fdObj = process->sim_fd_obj(fd);
547 // doesn't map to any simulator fd: not a valid target fd
551 if (fdObj->driver != NULL) {
552 return fdObj->driver->ioctl(process, tc, req);
555 if (OS::isTtyReq(req)) {
559 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
560 fd, req, tc->pcState());
566 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
567 ThreadContext *tc, int index)
571 if (!tc->getMemProxy().tryReadString(path,
572 process->getSyscallArg(tc, index)))
575 int tgtFlags = process->getSyscallArg(tc, index);
576 int mode = process->getSyscallArg(tc, index);
579 // translate open flags
580 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
581 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
582 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
583 hostFlags |= OS::openFlagTable[i].hostFlag;
587 // any target flags left?
589 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
592 hostFlags |= O_BINARY;
595 // Adjust path for current working directory
596 path = process->fullPath(path);
598 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
600 if (startswith(path, "/dev/")) {
601 std::string filename = path.substr(strlen("/dev/"));
602 if (filename == "sysdev0") {
603 // This is a memory-mapped high-resolution timer device on Alpha.
604 // We don't support it, so just punt.
605 warn("Ignoring open(%s, ...)\n", path);
609 EmulatedDriver *drv = process->findDriver(filename);
611 // the driver's open method will allocate a fd from the
612 // process if necessary.
613 return drv->open(process, tc, mode, hostFlags);
616 // fall through here for pass through to host devices, such as
622 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
623 startswith(path, "/platform/") || startswith(path, "/sys/")) {
624 // It's a proc/sys entry and requires special handling
625 fd = OS::openSpecialFile(path, process, tc);
626 local_errno = ENOENT;
629 fd = open(path.c_str(), hostFlags, mode);
636 return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
639 /// Target open() handler.
642 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
645 return openFunc<OS>(desc, callnum, process, tc, 0);
648 /// Target openat() handler.
651 openatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
655 int dirfd = process->getSyscallArg(tc, index);
656 if (dirfd != OS::TGT_AT_FDCWD)
657 warn("openat: first argument not AT_FDCWD; unlikely to work");
658 return openFunc<OS>(desc, callnum, process, tc, 1);
661 /// Target unlinkat() handler.
664 unlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
668 int dirfd = process->getSyscallArg(tc, index);
669 if (dirfd != OS::TGT_AT_FDCWD)
670 warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
672 return unlinkHelper(desc, callnum, process, tc, 1);
675 /// Target facessat() handler
678 faccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
682 int dirfd = process->getSyscallArg(tc, index);
683 if (dirfd != OS::TGT_AT_FDCWD)
684 warn("faccessat: first argument not AT_FDCWD; unlikely to work");
685 return accessFunc(desc, callnum, process, tc, 1);
688 /// Target readlinkat() handler
691 readlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
695 int dirfd = process->getSyscallArg(tc, index);
696 if (dirfd != OS::TGT_AT_FDCWD)
697 warn("openat: first argument not AT_FDCWD; unlikely to work");
698 return readlinkFunc(desc, callnum, process, tc, 1);
701 /// Target sysinfo() handler.
704 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
709 TypedBufferArg<typename OS::tgt_sysinfo>
710 sysinfo(process->getSyscallArg(tc, index));
712 sysinfo->uptime=seconds_since_epoch;
713 sysinfo->totalram=process->system->memSize();
715 sysinfo.copyOut(tc->getMemProxy());
720 /// Target chmod() handler.
723 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
729 if (!tc->getMemProxy().tryReadString(path,
730 process->getSyscallArg(tc, index))) {
734 uint32_t mode = process->getSyscallArg(tc, index);
737 // XXX translate mode flags via OS::something???
740 // Adjust path for current working directory
741 path = process->fullPath(path);
744 int result = chmod(path.c_str(), hostMode);
752 /// Target fchmod() handler.
755 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
759 int fd = process->getSyscallArg(tc, index);
760 if (fd < 0 || process->sim_fd(fd) < 0) {
761 // doesn't map to any simulator fd: not a valid target fd
765 uint32_t mode = process->getSyscallArg(tc, index);
768 // XXX translate mode flags via OS::someting???
772 int result = fchmod(process->sim_fd(fd), hostMode);
779 /// Target mremap() handler.
782 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
785 Addr start = process->getSyscallArg(tc, index);
786 uint64_t old_length = process->getSyscallArg(tc, index);
787 uint64_t new_length = process->getSyscallArg(tc, index);
788 uint64_t flags = process->getSyscallArg(tc, index);
789 uint64_t provided_address = 0;
790 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
792 if (use_provided_address)
793 provided_address = process->getSyscallArg(tc, index);
795 if ((start % TheISA::PageBytes != 0) ||
796 (provided_address % TheISA::PageBytes != 0)) {
797 warn("mremap failing: arguments not page aligned");
801 new_length = roundUp(new_length, TheISA::PageBytes);
803 if (new_length > old_length) {
804 if ((start + old_length) == process->mmap_end &&
805 (!use_provided_address || provided_address == start)) {
806 uint64_t diff = new_length - old_length;
807 process->allocateMem(process->mmap_end, diff);
808 process->mmap_end += diff;
811 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
812 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
815 uint64_t new_start = use_provided_address ?
816 provided_address : process->mmap_end;
817 process->pTable->remap(start, old_length, new_start);
818 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
819 new_start, new_start + new_length,
820 new_length - old_length);
821 // add on the remaining unallocated pages
822 process->allocateMem(new_start + old_length,
823 new_length - old_length,
824 use_provided_address /* clobber */);
825 if (!use_provided_address)
826 process->mmap_end += new_length;
827 if (use_provided_address &&
828 new_start + new_length > process->mmap_end) {
829 // something fishy going on here, at least notify the user
830 // @todo: increase mmap_end?
831 warn("mmap region limit exceeded with MREMAP_FIXED\n");
833 warn("returning %08p as start\n", new_start);
838 if (use_provided_address && provided_address != start)
839 process->pTable->remap(start, new_length, provided_address);
840 process->pTable->unmap(start + new_length, old_length - new_length);
841 return use_provided_address ? provided_address : start;
845 /// Target stat() handler.
848 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
854 if (!tc->getMemProxy().tryReadString(path,
855 process->getSyscallArg(tc, index))) {
858 Addr bufPtr = process->getSyscallArg(tc, index);
860 // Adjust path for current working directory
861 path = process->fullPath(path);
864 int result = stat(path.c_str(), &hostBuf);
869 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
875 /// Target stat64() handler.
878 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
884 if (!tc->getMemProxy().tryReadString(path,
885 process->getSyscallArg(tc, index)))
887 Addr bufPtr = process->getSyscallArg(tc, index);
889 // Adjust path for current working directory
890 path = process->fullPath(path);
894 int result = stat(path.c_str(), &hostBuf);
896 struct stat64 hostBuf;
897 int result = stat64(path.c_str(), &hostBuf);
903 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
909 /// Target fstatat64() handler.
912 fstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
916 int dirfd = process->getSyscallArg(tc, index);
917 if (dirfd != OS::TGT_AT_FDCWD)
918 warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
921 if (!tc->getMemProxy().tryReadString(path,
922 process->getSyscallArg(tc, index)))
924 Addr bufPtr = process->getSyscallArg(tc, index);
926 // Adjust path for current working directory
927 path = process->fullPath(path);
931 int result = stat(path.c_str(), &hostBuf);
933 struct stat64 hostBuf;
934 int result = stat64(path.c_str(), &hostBuf);
940 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
946 /// Target fstat64() handler.
949 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
953 int fd = process->getSyscallArg(tc, index);
954 Addr bufPtr = process->getSyscallArg(tc, index);
955 if (fd < 0 || process->sim_fd(fd) < 0) {
956 // doesn't map to any simulator fd: not a valid target fd
962 int result = fstat(process->sim_fd(fd), &hostBuf);
964 struct stat64 hostBuf;
965 int result = fstat64(process->sim_fd(fd), &hostBuf);
971 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
977 /// Target lstat() handler.
980 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
986 if (!tc->getMemProxy().tryReadString(path,
987 process->getSyscallArg(tc, index))) {
990 Addr bufPtr = process->getSyscallArg(tc, index);
992 // Adjust path for current working directory
993 path = process->fullPath(path);
996 int result = lstat(path.c_str(), &hostBuf);
1001 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1006 /// Target lstat64() handler.
1009 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
1015 if (!tc->getMemProxy().tryReadString(path,
1016 process->getSyscallArg(tc, index))) {
1019 Addr bufPtr = process->getSyscallArg(tc, index);
1021 // Adjust path for current working directory
1022 path = process->fullPath(path);
1025 struct stat hostBuf;
1026 int result = lstat(path.c_str(), &hostBuf);
1028 struct stat64 hostBuf;
1029 int result = lstat64(path.c_str(), &hostBuf);
1035 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1040 /// Target fstat() handler.
1043 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1047 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1048 Addr bufPtr = process->getSyscallArg(tc, index);
1050 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1055 struct stat hostBuf;
1056 int result = fstat(fd, &hostBuf);
1061 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
1067 /// Target statfs() handler.
1070 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1076 if (!tc->getMemProxy().tryReadString(path,
1077 process->getSyscallArg(tc, index))) {
1080 Addr bufPtr = process->getSyscallArg(tc, index);
1082 // Adjust path for current working directory
1083 path = process->fullPath(path);
1085 struct statfs hostBuf;
1086 int result = statfs(path.c_str(), &hostBuf);
1091 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1097 /// Target fstatfs() handler.
1100 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1104 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1105 Addr bufPtr = process->getSyscallArg(tc, index);
1110 struct statfs hostBuf;
1111 int result = fstatfs(fd, &hostBuf);
1116 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1122 /// Target writev() handler.
1125 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1129 int fd = process->getSyscallArg(tc, index);
1130 if (fd < 0 || process->sim_fd(fd) < 0) {
1131 // doesn't map to any simulator fd: not a valid target fd
1135 SETranslatingPortProxy &p = tc->getMemProxy();
1136 uint64_t tiov_base = process->getSyscallArg(tc, index);
1137 size_t count = process->getSyscallArg(tc, index);
1138 struct iovec hiov[count];
1139 for (size_t i = 0; i < count; ++i) {
1140 typename OS::tgt_iovec tiov;
1142 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1143 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1144 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1145 hiov[i].iov_base = new char [hiov[i].iov_len];
1146 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1150 int result = writev(process->sim_fd(fd), hiov, count);
1152 for (size_t i = 0; i < count; ++i)
1153 delete [] (char *)hiov[i].iov_base;
1162 /// Target mmap() handler.
1164 /// We don't really handle mmap(). If the target is mmaping an
1165 /// anonymous region or /dev/zero, we can get away with doing basically
1166 /// nothing (since memory is initialized to zero and the simulator
1167 /// doesn't really check addresses anyway).
1171 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1174 Addr start = p->getSyscallArg(tc, index);
1175 uint64_t length = p->getSyscallArg(tc, index);
1176 index++; // int prot = p->getSyscallArg(tc, index);
1177 int flags = p->getSyscallArg(tc, index);
1178 int tgt_fd = p->getSyscallArg(tc, index);
1179 int offset = p->getSyscallArg(tc, index);
1181 if (length > 0x100000000ULL)
1182 warn("mmap length argument %#x is unreasonably large.\n", length);
1184 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1185 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1186 if (!fd_map || fd_map->fd < 0) {
1187 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1191 if (fd_map->filename != "/dev/zero") {
1192 // This is very likely broken, but leave a warning here
1193 // (rather than panic) in case /dev/zero is known by
1194 // another name on some platform
1195 warn("allowing mmap of file %s; mmap not supported on files"
1196 " other than /dev/zero\n", fd_map->filename);
1200 length = roundUp(length, TheISA::PageBytes);
1202 if ((start % TheISA::PageBytes) != 0 ||
1203 (offset % TheISA::PageBytes) != 0) {
1204 warn("mmap failing: arguments not page-aligned: "
1205 "start 0x%x offset 0x%x",
1210 // are we ok with clobbering existing mappings? only set this to
1211 // true if the user has been warned.
1212 bool clobber = false;
1214 // try to use the caller-provided address if there is one
1215 bool use_provided_address = (start != 0);
1217 if (use_provided_address) {
1218 // check to see if the desired address is already in use
1219 if (!p->pTable->isUnmapped(start, length)) {
1220 // there are existing mappings in the desired range
1221 // whether we clobber them or not depends on whether the caller
1222 // specified MAP_FIXED
1223 if (flags & OS::TGT_MAP_FIXED) {
1224 // MAP_FIXED specified: map attempt fails
1227 // MAP_FIXED not specified: ignore suggested start address
1228 warn("mmap: ignoring suggested map address 0x%x\n", start);
1229 use_provided_address = false;
1234 if (!use_provided_address) {
1235 // no address provided, or provided address unusable:
1236 // pick next address from our "mmap region"
1237 if (OS::mmapGrowsDown()) {
1238 start = p->mmap_end - length;
1239 p->mmap_end = start;
1241 start = p->mmap_end;
1242 p->mmap_end += length;
1246 p->allocateMem(start, length, clobber);
1251 /// Target getrlimit() handler.
1254 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1258 unsigned resource = process->getSyscallArg(tc, index);
1259 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1262 case OS::TGT_RLIMIT_STACK:
1263 // max stack size in bytes: make up a number (8MB for now)
1264 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1265 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1266 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1269 case OS::TGT_RLIMIT_DATA:
1270 // max data segment size in bytes: make up a number
1271 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1272 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1273 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1277 warn("getrlimit: unimplemented resource %d", resource);
1282 rlp.copyOut(tc->getMemProxy());
1286 /// Target gettimeofday() handler.
1289 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1293 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1295 getElapsedTime(tp->tv_sec, tp->tv_usec);
1296 tp->tv_sec += seconds_since_epoch;
1297 tp->tv_sec = TheISA::htog(tp->tv_sec);
1298 tp->tv_usec = TheISA::htog(tp->tv_usec);
1300 tp.copyOut(tc->getMemProxy());
1306 /// Target utimes() handler.
1309 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1315 if (!tc->getMemProxy().tryReadString(path,
1316 process->getSyscallArg(tc, index))) {
1320 TypedBufferArg<typename OS::timeval [2]>
1321 tp(process->getSyscallArg(tc, index));
1322 tp.copyIn(tc->getMemProxy());
1324 struct timeval hostTimeval[2];
1325 for (int i = 0; i < 2; ++i)
1327 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1328 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1331 // Adjust path for current working directory
1332 path = process->fullPath(path);
1334 int result = utimes(path.c_str(), hostTimeval);
1341 /// Target getrusage() function.
1344 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1348 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1349 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1351 rup->ru_utime.tv_sec = 0;
1352 rup->ru_utime.tv_usec = 0;
1353 rup->ru_stime.tv_sec = 0;
1354 rup->ru_stime.tv_usec = 0;
1362 rup->ru_inblock = 0;
1363 rup->ru_oublock = 0;
1366 rup->ru_nsignals = 0;
1371 case OS::TGT_RUSAGE_SELF:
1372 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1373 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1374 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1377 case OS::TGT_RUSAGE_CHILDREN:
1378 // do nothing. We have no child processes, so they take no time.
1382 // don't really handle THREAD or CHILDREN, but just warn and
1384 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1388 rup.copyOut(tc->getMemProxy());
1393 /// Target times() function.
1396 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1400 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1402 // Fill in the time structure (in clocks)
1403 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1404 bufp->tms_utime = clocks;
1405 bufp->tms_stime = 0;
1406 bufp->tms_cutime = 0;
1407 bufp->tms_cstime = 0;
1409 // Convert to host endianness
1410 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1413 bufp.copyOut(tc->getMemProxy());
1415 // Return clock ticks since system boot
1419 /// Target time() function.
1422 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1425 typename OS::time_t sec, usec;
1426 getElapsedTime(sec, usec);
1427 sec += seconds_since_epoch;
1430 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1432 typename OS::time_t t = sec;
1433 t = TheISA::htog(t);
1434 SETranslatingPortProxy &p = tc->getMemProxy();
1435 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1441 #endif // __SIM_SYSCALL_EMUL_HH__