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 unlinkFunc(SyscallDesc *desc, int num,
199 LiveProcess *p, ThreadContext *tc);
201 /// Target mkdir() handler.
202 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
203 LiveProcess *p, ThreadContext *tc);
205 /// Target rename() handler.
206 SyscallReturn renameFunc(SyscallDesc *desc, int num,
207 LiveProcess *p, ThreadContext *tc);
210 /// Target truncate() handler.
211 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
212 LiveProcess *p, ThreadContext *tc);
215 /// Target ftruncate() handler.
216 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
217 LiveProcess *p, ThreadContext *tc);
220 /// Target truncate64() handler.
221 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
222 LiveProcess *p, ThreadContext *tc);
224 /// Target ftruncate64() handler.
225 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
226 LiveProcess *p, ThreadContext *tc);
229 /// Target umask() handler.
230 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
231 LiveProcess *p, ThreadContext *tc);
234 /// Target chown() handler.
235 SyscallReturn chownFunc(SyscallDesc *desc, int num,
236 LiveProcess *p, ThreadContext *tc);
239 /// Target fchown() handler.
240 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
241 LiveProcess *p, ThreadContext *tc);
243 /// Target dup() handler.
244 SyscallReturn dupFunc(SyscallDesc *desc, int num,
245 LiveProcess *process, ThreadContext *tc);
247 /// Target fnctl() handler.
248 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
249 LiveProcess *process, ThreadContext *tc);
251 /// Target fcntl64() handler.
252 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
253 LiveProcess *process, ThreadContext *tc);
255 /// Target setuid() handler.
256 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
257 LiveProcess *p, ThreadContext *tc);
259 /// Target getpid() handler.
260 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
261 LiveProcess *p, ThreadContext *tc);
263 /// Target getuid() handler.
264 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
265 LiveProcess *p, ThreadContext *tc);
267 /// Target getgid() handler.
268 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
269 LiveProcess *p, ThreadContext *tc);
271 /// Target getppid() handler.
272 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
273 LiveProcess *p, ThreadContext *tc);
275 /// Target geteuid() handler.
276 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
277 LiveProcess *p, ThreadContext *tc);
279 /// Target getegid() handler.
280 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
281 LiveProcess *p, ThreadContext *tc);
283 /// Target clone() handler.
284 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
285 LiveProcess *p, ThreadContext *tc);
287 /// Target access() handler
288 SyscallReturn accessFunc(SyscallDesc *desc, int num,
289 LiveProcess *p, ThreadContext *tc);
290 SyscallReturn accessFunc(SyscallDesc *desc, int num,
291 LiveProcess *p, ThreadContext *tc,
294 /// Futex system call
295 /// Implemented by Daniel Sanchez
296 /// Used by printf's in multi-threaded apps
299 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
305 int index_timeout = 3;
307 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
308 int op = process->getSyscallArg(tc, index_op);
309 int val = process->getSyscallArg(tc, index_val);
310 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
312 std::map<uint64_t, std::list<ThreadContext *> * >
313 &futex_map = tc->getSystemPtr()->futexMap;
315 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
318 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
320 if (op == OS::TGT_FUTEX_WAIT) {
322 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
323 "we'll wait indefinitely");
326 uint8_t *buf = new uint8_t[sizeof(int)];
327 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
328 int mem_val = *((int *)buf);
332 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
333 "expected: %d\n", mem_val, val);
334 return -OS::TGT_EWOULDBLOCK;
337 // Queue the thread context
338 std::list<ThreadContext *> * tcWaitList;
339 if (futex_map.count(uaddr)) {
340 tcWaitList = futex_map.find(uaddr)->second;
342 tcWaitList = new std::list<ThreadContext *>();
343 futex_map.insert(std::pair< uint64_t,
344 std::list<ThreadContext *> * >(uaddr, tcWaitList));
346 tcWaitList->push_back(tc);
347 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
351 } else if (op == OS::TGT_FUTEX_WAKE){
353 std::list<ThreadContext *> * tcWaitList;
354 if (futex_map.count(uaddr)) {
355 tcWaitList = futex_map.find(uaddr)->second;
356 while (tcWaitList->size() > 0 && wokenUp < val) {
357 tcWaitList->front()->activate();
358 tcWaitList->pop_front();
361 if(tcWaitList->empty()) {
362 futex_map.erase(uaddr);
366 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
367 "thread contexts\n", wokenUp);
370 warn("sys_futex: op %d is not implemented, just returning...", op);
377 /// Pseudo Funcs - These functions use a different return convension,
378 /// returning a second value in a register other than the normal return register
379 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
380 LiveProcess *process, ThreadContext *tc);
382 /// Target getpidPseudo() handler.
383 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
384 LiveProcess *p, ThreadContext *tc);
386 /// Target getuidPseudo() handler.
387 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
388 LiveProcess *p, ThreadContext *tc);
390 /// Target getgidPseudo() handler.
391 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
392 LiveProcess *p, ThreadContext *tc);
395 /// A readable name for 1,000,000, for converting microseconds to seconds.
396 const int one_million = 1000000;
398 /// Approximate seconds since the epoch (1/1/1970). About a billion,
399 /// by my reckoning. We want to keep this a constant (not use the
400 /// real-world time) to keep simulations repeatable.
401 const unsigned seconds_since_epoch = 1000000000;
403 /// Helper function to convert current elapsed time to seconds and
405 template <class T1, class T2>
407 getElapsedTime(T1 &sec, T2 &usec)
409 int elapsed_usecs = curTick() / SimClock::Int::us;
410 sec = elapsed_usecs / one_million;
411 usec = elapsed_usecs % one_million;
414 //////////////////////////////////////////////////////////////////////
416 // The following emulation functions are generic, but need to be
417 // templated to account for differences in types, constants, etc.
419 //////////////////////////////////////////////////////////////////////
422 typedef struct stat hst_stat;
423 typedef struct stat hst_stat64;
425 typedef struct stat hst_stat;
426 typedef struct stat64 hst_stat64;
429 //// Helper function to convert a host stat buffer to a target stat
430 //// buffer. Also copies the target buffer out to the simulated
431 //// memory space. Used by stat(), fstat(), and lstat().
433 template <typename target_stat, typename host_stat>
435 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
437 using namespace TheISA;
442 tgt->st_dev = host->st_dev;
443 tgt->st_dev = TheISA::htog(tgt->st_dev);
444 tgt->st_ino = host->st_ino;
445 tgt->st_ino = TheISA::htog(tgt->st_ino);
446 tgt->st_mode = host->st_mode;
448 // Claim to be a character device
449 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
450 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
452 tgt->st_mode = TheISA::htog(tgt->st_mode);
453 tgt->st_nlink = host->st_nlink;
454 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
455 tgt->st_uid = host->st_uid;
456 tgt->st_uid = TheISA::htog(tgt->st_uid);
457 tgt->st_gid = host->st_gid;
458 tgt->st_gid = TheISA::htog(tgt->st_gid);
460 tgt->st_rdev = 0x880d;
462 tgt->st_rdev = host->st_rdev;
463 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
464 tgt->st_size = host->st_size;
465 tgt->st_size = TheISA::htog(tgt->st_size);
466 tgt->st_atimeX = host->st_atime;
467 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
468 tgt->st_mtimeX = host->st_mtime;
469 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
470 tgt->st_ctimeX = host->st_ctime;
471 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
472 // Force the block size to be 8k. This helps to ensure buffered io works
473 // consistently across different hosts.
474 tgt->st_blksize = 0x2000;
475 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
476 tgt->st_blocks = host->st_blocks;
477 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
482 template <typename target_stat, typename host_stat64>
484 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
486 using namespace TheISA;
488 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
489 #if defined(STAT_HAVE_NSEC)
490 tgt->st_atime_nsec = host->st_atime_nsec;
491 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
492 tgt->st_mtime_nsec = host->st_mtime_nsec;
493 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
494 tgt->st_ctime_nsec = host->st_ctime_nsec;
495 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
497 tgt->st_atime_nsec = 0;
498 tgt->st_mtime_nsec = 0;
499 tgt->st_ctime_nsec = 0;
503 //Here are a couple convenience functions
506 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
507 hst_stat *host, bool fakeTTY = false)
509 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
510 tgt_stat_buf tgt(addr);
511 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
517 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
518 hst_stat64 *host, bool fakeTTY = false)
520 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
521 tgt_stat_buf tgt(addr);
522 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
526 /// Target ioctl() handler. For the most part, programs call ioctl()
527 /// only to find out if their stdout is a tty, to determine whether to
528 /// do line or block buffering. We always claim that output fds are
529 /// not TTYs to provide repeatable results.
532 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
536 int fd = process->getSyscallArg(tc, index);
537 unsigned req = process->getSyscallArg(tc, index);
539 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
541 Process::FdMap *fdObj = process->sim_fd_obj(fd);
544 // doesn't map to any simulator fd: not a valid target fd
548 if (fdObj->driver != NULL) {
549 return fdObj->driver->ioctl(process, tc, req);
552 if (OS::isTtyReq(req)) {
556 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
557 fd, req, tc->pcState());
563 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
564 ThreadContext *tc, int index)
568 if (!tc->getMemProxy().tryReadString(path,
569 process->getSyscallArg(tc, index)))
572 int tgtFlags = process->getSyscallArg(tc, index);
573 int mode = process->getSyscallArg(tc, index);
576 // translate open flags
577 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
578 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
579 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
580 hostFlags |= OS::openFlagTable[i].hostFlag;
584 // any target flags left?
586 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
589 hostFlags |= O_BINARY;
592 // Adjust path for current working directory
593 path = process->fullPath(path);
595 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
597 if (startswith(path, "/dev/")) {
598 std::string filename = path.substr(strlen("/dev/"));
599 if (filename == "sysdev0") {
600 // This is a memory-mapped high-resolution timer device on Alpha.
601 // We don't support it, so just punt.
602 warn("Ignoring open(%s, ...)\n", path);
606 EmulatedDriver *drv = process->findDriver(filename);
608 // the driver's open method will allocate a fd from the
609 // process if necessary.
610 return drv->open(process, tc, mode, hostFlags);
613 // fall through here for pass through to host devices, such as
619 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
620 startswith(path, "/platform/") || startswith(path, "/sys/")) {
621 // It's a proc/sys entry and requires special handling
622 fd = OS::openSpecialFile(path, process, tc);
623 local_errno = ENOENT;
626 fd = open(path.c_str(), hostFlags, mode);
633 return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
636 /// Target open() handler.
639 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
642 return openFunc<OS>(desc, callnum, process, tc, 0);
645 /// Target openat() handler.
648 openatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
652 int dirfd = process->getSyscallArg(tc, index);
653 if (dirfd != OS::TGT_AT_FDCWD)
654 warn("openat: first argument not AT_FDCWD; unlikely to work");
655 return openFunc<OS>(desc, callnum, process, tc, 1);
658 /// Target facessat() handler
661 faccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
665 int dirfd = process->getSyscallArg(tc, index);
666 if (dirfd != OS::TGT_AT_FDCWD)
667 warn("faccessat: first argument not AT_FDCWD; unlikely to work");
668 return accessFunc(desc, callnum, process, tc, 1);
671 /// Target readlinkat() handler
674 readlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
678 int dirfd = process->getSyscallArg(tc, index);
679 if (dirfd != OS::TGT_AT_FDCWD)
680 warn("openat: first argument not AT_FDCWD; unlikely to work");
681 return readlinkFunc(desc, callnum, process, tc, 1);
684 /// Target sysinfo() handler.
687 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
692 TypedBufferArg<typename OS::tgt_sysinfo>
693 sysinfo(process->getSyscallArg(tc, index));
695 sysinfo->uptime=seconds_since_epoch;
696 sysinfo->totalram=process->system->memSize();
698 sysinfo.copyOut(tc->getMemProxy());
703 /// Target chmod() handler.
706 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
712 if (!tc->getMemProxy().tryReadString(path,
713 process->getSyscallArg(tc, index))) {
717 uint32_t mode = process->getSyscallArg(tc, index);
720 // XXX translate mode flags via OS::something???
723 // Adjust path for current working directory
724 path = process->fullPath(path);
727 int result = chmod(path.c_str(), hostMode);
735 /// Target fchmod() handler.
738 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
742 int fd = process->getSyscallArg(tc, index);
743 if (fd < 0 || process->sim_fd(fd) < 0) {
744 // doesn't map to any simulator fd: not a valid target fd
748 uint32_t mode = process->getSyscallArg(tc, index);
751 // XXX translate mode flags via OS::someting???
755 int result = fchmod(process->sim_fd(fd), hostMode);
762 /// Target mremap() handler.
765 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
768 Addr start = process->getSyscallArg(tc, index);
769 uint64_t old_length = process->getSyscallArg(tc, index);
770 uint64_t new_length = process->getSyscallArg(tc, index);
771 uint64_t flags = process->getSyscallArg(tc, index);
772 uint64_t provided_address = 0;
773 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
775 if (use_provided_address)
776 provided_address = process->getSyscallArg(tc, index);
778 if ((start % TheISA::PageBytes != 0) ||
779 (provided_address % TheISA::PageBytes != 0)) {
780 warn("mremap failing: arguments not page aligned");
784 new_length = roundUp(new_length, TheISA::PageBytes);
786 if (new_length > old_length) {
787 if ((start + old_length) == process->mmap_end &&
788 (!use_provided_address || provided_address == start)) {
789 uint64_t diff = new_length - old_length;
790 process->allocateMem(process->mmap_end, diff);
791 process->mmap_end += diff;
794 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
795 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
798 uint64_t new_start = use_provided_address ?
799 provided_address : process->mmap_end;
800 process->pTable->remap(start, old_length, new_start);
801 warn("mremapping to new vaddr %08p-%08p, adding %d\n",
802 new_start, new_start + new_length,
803 new_length - old_length);
804 // add on the remaining unallocated pages
805 process->allocateMem(new_start + old_length,
806 new_length - old_length,
807 use_provided_address /* clobber */);
808 if (!use_provided_address)
809 process->mmap_end += new_length;
810 if (use_provided_address &&
811 new_start + new_length > process->mmap_end) {
812 // something fishy going on here, at least notify the user
813 // @todo: increase mmap_end?
814 warn("mmap region limit exceeded with MREMAP_FIXED\n");
816 warn("returning %08p as start\n", new_start);
821 if (use_provided_address && provided_address != start)
822 process->pTable->remap(start, new_length, provided_address);
823 process->pTable->unmap(start + new_length, old_length - new_length);
824 return use_provided_address ? provided_address : start;
828 /// Target stat() handler.
831 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
837 if (!tc->getMemProxy().tryReadString(path,
838 process->getSyscallArg(tc, index))) {
841 Addr bufPtr = process->getSyscallArg(tc, index);
843 // Adjust path for current working directory
844 path = process->fullPath(path);
847 int result = stat(path.c_str(), &hostBuf);
852 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
858 /// Target stat64() handler.
861 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
867 if (!tc->getMemProxy().tryReadString(path,
868 process->getSyscallArg(tc, index)))
870 Addr bufPtr = process->getSyscallArg(tc, index);
872 // Adjust path for current working directory
873 path = process->fullPath(path);
877 int result = stat(path.c_str(), &hostBuf);
879 struct stat64 hostBuf;
880 int result = stat64(path.c_str(), &hostBuf);
886 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
892 /// Target fstatat64() handler.
895 fstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
899 int dirfd = process->getSyscallArg(tc, index);
900 if (dirfd != OS::TGT_AT_FDCWD)
901 warn("openat: first argument not AT_FDCWD; unlikely to work");
904 if (!tc->getMemProxy().tryReadString(path,
905 process->getSyscallArg(tc, index)))
907 Addr bufPtr = process->getSyscallArg(tc, index);
909 // Adjust path for current working directory
910 path = process->fullPath(path);
914 int result = stat(path.c_str(), &hostBuf);
916 struct stat64 hostBuf;
917 int result = stat64(path.c_str(), &hostBuf);
923 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
929 /// Target fstat64() handler.
932 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
936 int fd = process->getSyscallArg(tc, index);
937 Addr bufPtr = process->getSyscallArg(tc, index);
938 if (fd < 0 || process->sim_fd(fd) < 0) {
939 // doesn't map to any simulator fd: not a valid target fd
945 int result = fstat(process->sim_fd(fd), &hostBuf);
947 struct stat64 hostBuf;
948 int result = fstat64(process->sim_fd(fd), &hostBuf);
954 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
960 /// Target lstat() handler.
963 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
969 if (!tc->getMemProxy().tryReadString(path,
970 process->getSyscallArg(tc, index))) {
973 Addr bufPtr = process->getSyscallArg(tc, index);
975 // Adjust path for current working directory
976 path = process->fullPath(path);
979 int result = lstat(path.c_str(), &hostBuf);
984 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
989 /// Target lstat64() handler.
992 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
998 if (!tc->getMemProxy().tryReadString(path,
999 process->getSyscallArg(tc, index))) {
1002 Addr bufPtr = process->getSyscallArg(tc, index);
1004 // Adjust path for current working directory
1005 path = process->fullPath(path);
1008 struct stat hostBuf;
1009 int result = lstat(path.c_str(), &hostBuf);
1011 struct stat64 hostBuf;
1012 int result = lstat64(path.c_str(), &hostBuf);
1018 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1023 /// Target fstat() handler.
1026 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1030 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1031 Addr bufPtr = process->getSyscallArg(tc, index);
1033 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1038 struct stat hostBuf;
1039 int result = fstat(fd, &hostBuf);
1044 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
1050 /// Target statfs() handler.
1053 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1059 if (!tc->getMemProxy().tryReadString(path,
1060 process->getSyscallArg(tc, index))) {
1063 Addr bufPtr = process->getSyscallArg(tc, index);
1065 // Adjust path for current working directory
1066 path = process->fullPath(path);
1068 struct statfs hostBuf;
1069 int result = statfs(path.c_str(), &hostBuf);
1074 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1080 /// Target fstatfs() handler.
1083 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1087 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1088 Addr bufPtr = process->getSyscallArg(tc, index);
1093 struct statfs hostBuf;
1094 int result = fstatfs(fd, &hostBuf);
1099 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1105 /// Target writev() handler.
1108 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1112 int fd = process->getSyscallArg(tc, index);
1113 if (fd < 0 || process->sim_fd(fd) < 0) {
1114 // doesn't map to any simulator fd: not a valid target fd
1118 SETranslatingPortProxy &p = tc->getMemProxy();
1119 uint64_t tiov_base = process->getSyscallArg(tc, index);
1120 size_t count = process->getSyscallArg(tc, index);
1121 struct iovec hiov[count];
1122 for (size_t i = 0; i < count; ++i) {
1123 typename OS::tgt_iovec tiov;
1125 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1126 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1127 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1128 hiov[i].iov_base = new char [hiov[i].iov_len];
1129 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1133 int result = writev(process->sim_fd(fd), hiov, count);
1135 for (size_t i = 0; i < count; ++i)
1136 delete [] (char *)hiov[i].iov_base;
1145 /// Target mmap() handler.
1147 /// We don't really handle mmap(). If the target is mmaping an
1148 /// anonymous region or /dev/zero, we can get away with doing basically
1149 /// nothing (since memory is initialized to zero and the simulator
1150 /// doesn't really check addresses anyway).
1154 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1157 Addr start = p->getSyscallArg(tc, index);
1158 uint64_t length = p->getSyscallArg(tc, index);
1159 index++; // int prot = p->getSyscallArg(tc, index);
1160 int flags = p->getSyscallArg(tc, index);
1161 int tgt_fd = p->getSyscallArg(tc, index);
1162 int offset = p->getSyscallArg(tc, index);
1164 if (length > 0x100000000ULL)
1165 warn("mmap length argument %#x is unreasonably large.\n", length);
1167 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1168 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1169 if (!fd_map || fd_map->fd < 0) {
1170 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1174 if (fd_map->filename != "/dev/zero") {
1175 // This is very likely broken, but leave a warning here
1176 // (rather than panic) in case /dev/zero is known by
1177 // another name on some platform
1178 warn("allowing mmap of file %s; mmap not supported on files"
1179 " other than /dev/zero\n", fd_map->filename);
1183 length = roundUp(length, TheISA::PageBytes);
1185 if ((start % TheISA::PageBytes) != 0 ||
1186 (offset % TheISA::PageBytes) != 0) {
1187 warn("mmap failing: arguments not page-aligned: "
1188 "start 0x%x offset 0x%x",
1193 // are we ok with clobbering existing mappings? only set this to
1194 // true if the user has been warned.
1195 bool clobber = false;
1197 // try to use the caller-provided address if there is one
1198 bool use_provided_address = (start != 0);
1200 if (use_provided_address) {
1201 // check to see if the desired address is already in use
1202 if (!p->pTable->isUnmapped(start, length)) {
1203 // there are existing mappings in the desired range
1204 // whether we clobber them or not depends on whether the caller
1205 // specified MAP_FIXED
1206 if (flags & OS::TGT_MAP_FIXED) {
1207 // MAP_FIXED specified: map attempt fails
1210 // MAP_FIXED not specified: ignore suggested start address
1211 warn("mmap: ignoring suggested map address 0x%x\n", start);
1212 use_provided_address = false;
1217 if (!use_provided_address) {
1218 // no address provided, or provided address unusable:
1219 // pick next address from our "mmap region"
1220 if (OS::mmapGrowsDown()) {
1221 start = p->mmap_end - length;
1222 p->mmap_end = start;
1224 start = p->mmap_end;
1225 p->mmap_end += length;
1229 p->allocateMem(start, length, clobber);
1234 /// Target getrlimit() handler.
1237 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1241 unsigned resource = process->getSyscallArg(tc, index);
1242 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1245 case OS::TGT_RLIMIT_STACK:
1246 // max stack size in bytes: make up a number (8MB for now)
1247 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1248 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1249 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1252 case OS::TGT_RLIMIT_DATA:
1253 // max data segment size in bytes: make up a number
1254 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1255 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1256 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1260 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1266 rlp.copyOut(tc->getMemProxy());
1270 /// Target gettimeofday() handler.
1273 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1277 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1279 getElapsedTime(tp->tv_sec, tp->tv_usec);
1280 tp->tv_sec += seconds_since_epoch;
1281 tp->tv_sec = TheISA::htog(tp->tv_sec);
1282 tp->tv_usec = TheISA::htog(tp->tv_usec);
1284 tp.copyOut(tc->getMemProxy());
1290 /// Target utimes() handler.
1293 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1299 if (!tc->getMemProxy().tryReadString(path,
1300 process->getSyscallArg(tc, index))) {
1304 TypedBufferArg<typename OS::timeval [2]>
1305 tp(process->getSyscallArg(tc, index));
1306 tp.copyIn(tc->getMemProxy());
1308 struct timeval hostTimeval[2];
1309 for (int i = 0; i < 2; ++i)
1311 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1312 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1315 // Adjust path for current working directory
1316 path = process->fullPath(path);
1318 int result = utimes(path.c_str(), hostTimeval);
1325 /// Target getrusage() function.
1328 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1332 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1333 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1335 rup->ru_utime.tv_sec = 0;
1336 rup->ru_utime.tv_usec = 0;
1337 rup->ru_stime.tv_sec = 0;
1338 rup->ru_stime.tv_usec = 0;
1346 rup->ru_inblock = 0;
1347 rup->ru_oublock = 0;
1350 rup->ru_nsignals = 0;
1355 case OS::TGT_RUSAGE_SELF:
1356 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1357 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1358 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1361 case OS::TGT_RUSAGE_CHILDREN:
1362 // do nothing. We have no child processes, so they take no time.
1366 // don't really handle THREAD or CHILDREN, but just warn and
1368 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1372 rup.copyOut(tc->getMemProxy());
1377 /// Target times() function.
1380 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1384 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1386 // Fill in the time structure (in clocks)
1387 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1388 bufp->tms_utime = clocks;
1389 bufp->tms_stime = 0;
1390 bufp->tms_cutime = 0;
1391 bufp->tms_cstime = 0;
1393 // Convert to host endianness
1394 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1397 bufp.copyOut(tc->getMemProxy());
1399 // Return clock ticks since system boot
1403 /// Target time() function.
1406 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1409 typename OS::time_t sec, usec;
1410 getElapsedTime(sec, usec);
1411 sec += seconds_since_epoch;
1414 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1416 typename OS::time_t t = sec;
1417 t = TheISA::htog(t);
1418 SETranslatingPortProxy &p = tc->getMemProxy();
1419 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1425 #endif // __SIM_SYSCALL_EMUL_HH__