2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Steve Reinhardt
32 #ifndef __SIM_SYSCALL_EMUL_HH__
33 #define __SIM_SYSCALL_EMUL_HH__
35 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
36 defined(__FreeBSD__) || defined(__CYGWIN__) || \
40 /// @file syscall_emul.hh
42 /// This file defines objects used to emulate syscalls from the target
43 /// application on the host machine.
46 #include <sys/fcntl.h> // for O_BINARY
56 #include "base/chunk_generator.hh"
57 #include "base/intmath.hh" // for RoundUp
58 #include "base/misc.hh"
59 #include "base/trace.hh"
60 #include "base/types.hh"
61 #include "config/the_isa.hh"
62 #include "cpu/base.hh"
63 #include "cpu/thread_context.hh"
64 #include "debug/SyscallVerbose.hh"
65 #include "mem/page_table.hh"
66 #include "mem/se_translating_port_proxy.hh"
67 #include "sim/byteswap.hh"
68 #include "sim/process.hh"
69 #include "sim/syscallreturn.hh"
70 #include "sim/system.hh"
73 /// System call descriptor.
79 /// Typedef for target syscall handler functions.
80 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
81 LiveProcess *, ThreadContext *);
83 const char *name; //!< Syscall name (e.g., "open").
84 FuncPtr funcPtr; //!< Pointer to emulation function.
85 int flags; //!< Flags (see Flags enum).
87 /// Flag values for controlling syscall behavior.
89 /// Don't set return regs according to funcPtr return value.
90 /// Used for syscalls with non-standard return conventions
91 /// that explicitly set the ThreadContext regs (e.g.,
93 SuppressReturnValue = 1
97 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
98 : name(_name), funcPtr(_funcPtr), flags(_flags)
102 /// Emulate the syscall. Public interface for calling through funcPtr.
103 void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
107 class BaseBufferArg {
111 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
113 bufPtr = new uint8_t[size];
114 // clear out buffer: in case we only partially populate this,
115 // and then do a copyOut(), we want to make sure we don't
116 // introduce any random junk into the simulated address space
117 memset(bufPtr, 0, size);
120 virtual ~BaseBufferArg() { delete [] bufPtr; }
123 // copy data into simulator space (read from target memory)
125 virtual bool copyIn(SETranslatingPortProxy &memproxy)
127 memproxy.readBlob(addr, bufPtr, size);
128 return true; // no EFAULT detection for now
132 // copy data out of simulator space (write to target memory)
134 virtual bool copyOut(SETranslatingPortProxy &memproxy)
136 memproxy.writeBlob(addr, bufPtr, size);
137 return true; // no EFAULT detection for now
147 class BufferArg : public BaseBufferArg
150 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
151 void *bufferPtr() { return bufPtr; }
155 class TypedBufferArg : public BaseBufferArg
158 // user can optionally specify a specific number of bytes to
159 // allocate to deal with those structs that have variable-size
161 TypedBufferArg(Addr _addr, int _size = sizeof(T))
162 : BaseBufferArg(_addr, _size)
166 operator T*() { return (T *)bufPtr; }
168 // dereference operators
169 T &operator*() { return *((T *)bufPtr); }
170 T* operator->() { return (T *)bufPtr; }
171 T &operator[](int i) { return ((T *)bufPtr)[i]; }
174 //////////////////////////////////////////////////////////////////////
176 // The following emulation functions are generic enough that they
177 // don't need to be recompiled for different emulated OS's. They are
178 // defined in sim/syscall_emul.cc.
180 //////////////////////////////////////////////////////////////////////
183 /// Handler for unimplemented syscalls that we haven't thought about.
184 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
185 LiveProcess *p, ThreadContext *tc);
187 /// Handler for unimplemented syscalls that we never intend to
188 /// implement (signal handling, etc.) and should not affect the correct
189 /// behavior of the program. Print a warning only if the appropriate
190 /// trace flag is enabled. Return success to the target program.
191 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
192 LiveProcess *p, ThreadContext *tc);
193 SyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
194 LiveProcess *p, ThreadContext *tc);
196 /// Target exit() handler: terminate current context.
197 SyscallReturn exitFunc(SyscallDesc *desc, int num,
198 LiveProcess *p, ThreadContext *tc);
200 /// Target exit_group() handler: terminate simulation. (exit all threads)
201 SyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
202 LiveProcess *p, ThreadContext *tc);
204 /// Target getpagesize() handler.
205 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
206 LiveProcess *p, ThreadContext *tc);
208 /// Target brk() handler: set brk address.
209 SyscallReturn brkFunc(SyscallDesc *desc, int num,
210 LiveProcess *p, ThreadContext *tc);
212 /// Target close() handler.
213 SyscallReturn closeFunc(SyscallDesc *desc, int num,
214 LiveProcess *p, ThreadContext *tc);
216 /// Target read() handler.
217 SyscallReturn readFunc(SyscallDesc *desc, int num,
218 LiveProcess *p, ThreadContext *tc);
220 /// Target write() handler.
221 SyscallReturn writeFunc(SyscallDesc *desc, int num,
222 LiveProcess *p, ThreadContext *tc);
224 /// Target lseek() handler.
225 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
226 LiveProcess *p, ThreadContext *tc);
228 /// Target _llseek() handler.
229 SyscallReturn _llseekFunc(SyscallDesc *desc, int num,
230 LiveProcess *p, ThreadContext *tc);
232 /// Target munmap() handler.
233 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
234 LiveProcess *p, ThreadContext *tc);
236 /// Target gethostname() handler.
237 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
238 LiveProcess *p, ThreadContext *tc);
240 /// Target getcwd() handler.
241 SyscallReturn getcwdFunc(SyscallDesc *desc, int num,
242 LiveProcess *p, ThreadContext *tc);
244 /// Target unlink() handler.
245 SyscallReturn readlinkFunc(SyscallDesc *desc, int num,
246 LiveProcess *p, ThreadContext *tc);
248 /// Target unlink() handler.
249 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
250 LiveProcess *p, ThreadContext *tc);
252 /// Target mkdir() handler.
253 SyscallReturn mkdirFunc(SyscallDesc *desc, int num,
254 LiveProcess *p, ThreadContext *tc);
256 /// Target rename() handler.
257 SyscallReturn renameFunc(SyscallDesc *desc, int num,
258 LiveProcess *p, ThreadContext *tc);
261 /// Target truncate() handler.
262 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
263 LiveProcess *p, ThreadContext *tc);
266 /// Target ftruncate() handler.
267 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
268 LiveProcess *p, ThreadContext *tc);
271 /// Target truncate64() handler.
272 SyscallReturn truncate64Func(SyscallDesc *desc, int num,
273 LiveProcess *p, ThreadContext *tc);
275 /// Target ftruncate64() handler.
276 SyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
277 LiveProcess *p, ThreadContext *tc);
280 /// Target umask() handler.
281 SyscallReturn umaskFunc(SyscallDesc *desc, int num,
282 LiveProcess *p, ThreadContext *tc);
285 /// Target chown() handler.
286 SyscallReturn chownFunc(SyscallDesc *desc, int num,
287 LiveProcess *p, ThreadContext *tc);
290 /// Target fchown() handler.
291 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
292 LiveProcess *p, ThreadContext *tc);
294 /// Target dup() handler.
295 SyscallReturn dupFunc(SyscallDesc *desc, int num,
296 LiveProcess *process, ThreadContext *tc);
298 /// Target fnctl() handler.
299 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
300 LiveProcess *process, ThreadContext *tc);
302 /// Target fcntl64() handler.
303 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
304 LiveProcess *process, ThreadContext *tc);
306 /// Target setuid() handler.
307 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
308 LiveProcess *p, ThreadContext *tc);
310 /// Target getpid() handler.
311 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
312 LiveProcess *p, ThreadContext *tc);
314 /// Target getuid() handler.
315 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
316 LiveProcess *p, ThreadContext *tc);
318 /// Target getgid() handler.
319 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
320 LiveProcess *p, ThreadContext *tc);
322 /// Target getppid() handler.
323 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
324 LiveProcess *p, ThreadContext *tc);
326 /// Target geteuid() handler.
327 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
328 LiveProcess *p, ThreadContext *tc);
330 /// Target getegid() handler.
331 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
332 LiveProcess *p, ThreadContext *tc);
334 /// Target clone() handler.
335 SyscallReturn cloneFunc(SyscallDesc *desc, int num,
336 LiveProcess *p, ThreadContext *tc);
338 /// Futex system call
339 /// Implemented by Daniel Sanchez
340 /// Used by printf's in multi-threaded apps
343 futexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
349 int index_timeout = 3;
351 uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
352 int op = process->getSyscallArg(tc, index_op);
353 int val = process->getSyscallArg(tc, index_val);
354 uint64_t timeout = process->getSyscallArg(tc, index_timeout);
356 std::map<uint64_t, std::list<ThreadContext *> * >
357 &futex_map = tc->getSystemPtr()->futexMap;
359 DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
362 op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
364 if (op == OS::TGT_FUTEX_WAIT) {
366 warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
367 "we'll wait indefinitely");
370 uint8_t *buf = new uint8_t[sizeof(int)];
371 tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
372 int mem_val = *((int *)buf);
376 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
377 "expected: %d\n", mem_val, val);
378 return -OS::TGT_EWOULDBLOCK;
381 // Queue the thread context
382 std::list<ThreadContext *> * tcWaitList;
383 if (futex_map.count(uaddr)) {
384 tcWaitList = futex_map.find(uaddr)->second;
386 tcWaitList = new std::list<ThreadContext *>();
387 futex_map.insert(std::pair< uint64_t,
388 std::list<ThreadContext *> * >(uaddr, tcWaitList));
390 tcWaitList->push_back(tc);
391 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
395 } else if (op == OS::TGT_FUTEX_WAKE){
397 std::list<ThreadContext *> * tcWaitList;
398 if (futex_map.count(uaddr)) {
399 tcWaitList = futex_map.find(uaddr)->second;
400 while (tcWaitList->size() > 0 && wokenUp < val) {
401 tcWaitList->front()->activate();
402 tcWaitList->pop_front();
405 if(tcWaitList->empty()) {
406 futex_map.erase(uaddr);
410 DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
411 "thread contexts\n", wokenUp);
414 warn("sys_futex: op %d is not implemented, just returning...", op);
421 /// Pseudo Funcs - These functions use a different return convension,
422 /// returning a second value in a register other than the normal return register
423 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
424 LiveProcess *process, ThreadContext *tc);
426 /// Target getpidPseudo() handler.
427 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
428 LiveProcess *p, ThreadContext *tc);
430 /// Target getuidPseudo() handler.
431 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
432 LiveProcess *p, ThreadContext *tc);
434 /// Target getgidPseudo() handler.
435 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
436 LiveProcess *p, ThreadContext *tc);
439 /// A readable name for 1,000,000, for converting microseconds to seconds.
440 const int one_million = 1000000;
442 /// Approximate seconds since the epoch (1/1/1970). About a billion,
443 /// by my reckoning. We want to keep this a constant (not use the
444 /// real-world time) to keep simulations repeatable.
445 const unsigned seconds_since_epoch = 1000000000;
447 /// Helper function to convert current elapsed time to seconds and
449 template <class T1, class T2>
451 getElapsedTime(T1 &sec, T2 &usec)
453 int elapsed_usecs = curTick() / SimClock::Int::us;
454 sec = elapsed_usecs / one_million;
455 usec = elapsed_usecs % one_million;
458 //////////////////////////////////////////////////////////////////////
460 // The following emulation functions are generic, but need to be
461 // templated to account for differences in types, constants, etc.
463 //////////////////////////////////////////////////////////////////////
466 typedef struct stat hst_stat;
467 typedef struct stat hst_stat64;
469 typedef struct stat hst_stat;
470 typedef struct stat64 hst_stat64;
473 //// Helper function to convert a host stat buffer to a target stat
474 //// buffer. Also copies the target buffer out to the simulated
475 //// memory space. Used by stat(), fstat(), and lstat().
477 template <typename target_stat, typename host_stat>
479 convertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
481 using namespace TheISA;
486 tgt->st_dev = host->st_dev;
487 tgt->st_dev = TheISA::htog(tgt->st_dev);
488 tgt->st_ino = host->st_ino;
489 tgt->st_ino = TheISA::htog(tgt->st_ino);
490 tgt->st_mode = host->st_mode;
492 // Claim to be a character device
493 tgt->st_mode &= ~S_IFMT; // Clear S_IFMT
494 tgt->st_mode |= S_IFCHR; // Set S_IFCHR
496 tgt->st_mode = TheISA::htog(tgt->st_mode);
497 tgt->st_nlink = host->st_nlink;
498 tgt->st_nlink = TheISA::htog(tgt->st_nlink);
499 tgt->st_uid = host->st_uid;
500 tgt->st_uid = TheISA::htog(tgt->st_uid);
501 tgt->st_gid = host->st_gid;
502 tgt->st_gid = TheISA::htog(tgt->st_gid);
504 tgt->st_rdev = 0x880d;
506 tgt->st_rdev = host->st_rdev;
507 tgt->st_rdev = TheISA::htog(tgt->st_rdev);
508 tgt->st_size = host->st_size;
509 tgt->st_size = TheISA::htog(tgt->st_size);
510 tgt->st_atimeX = host->st_atime;
511 tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
512 tgt->st_mtimeX = host->st_mtime;
513 tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
514 tgt->st_ctimeX = host->st_ctime;
515 tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
516 // Force the block size to be 8k. This helps to ensure buffered io works
517 // consistently across different hosts.
518 tgt->st_blksize = 0x2000;
519 tgt->st_blksize = TheISA::htog(tgt->st_blksize);
520 tgt->st_blocks = host->st_blocks;
521 tgt->st_blocks = TheISA::htog(tgt->st_blocks);
526 template <typename target_stat, typename host_stat64>
528 convertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
530 using namespace TheISA;
532 convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
533 #if defined(STAT_HAVE_NSEC)
534 tgt->st_atime_nsec = host->st_atime_nsec;
535 tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
536 tgt->st_mtime_nsec = host->st_mtime_nsec;
537 tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
538 tgt->st_ctime_nsec = host->st_ctime_nsec;
539 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
541 tgt->st_atime_nsec = 0;
542 tgt->st_mtime_nsec = 0;
543 tgt->st_ctime_nsec = 0;
547 //Here are a couple convenience functions
550 copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
551 hst_stat *host, bool fakeTTY = false)
553 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
554 tgt_stat_buf tgt(addr);
555 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
561 copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
562 hst_stat64 *host, bool fakeTTY = false)
564 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
565 tgt_stat_buf tgt(addr);
566 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
570 /// Target ioctl() handler. For the most part, programs call ioctl()
571 /// only to find out if their stdout is a tty, to determine whether to
572 /// do line or block buffering. We always claim that output fds are
573 /// not TTYs to provide repeatable results.
576 ioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
580 int fd = process->getSyscallArg(tc, index);
581 unsigned req = process->getSyscallArg(tc, index);
583 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
585 if (fd < 0 || process->sim_fd(fd) < 0) {
586 // doesn't map to any simulator fd: not a valid target fd
590 if (OS::isTtyReq(req)) {
594 warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
595 fd, req, tc->pcState());
599 /// Target open() handler.
602 openFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
608 if (!tc->getMemProxy().tryReadString(path,
609 process->getSyscallArg(tc, index)))
612 if (path == "/dev/sysdev0") {
613 // This is a memory-mapped high-resolution timer device on Alpha.
614 // We don't support it, so just punt.
615 warn("Ignoring open(%s, ...)\n", path);
619 int tgtFlags = process->getSyscallArg(tc, index);
620 int mode = process->getSyscallArg(tc, index);
623 // translate open flags
624 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
625 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
626 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
627 hostFlags |= OS::openFlagTable[i].hostFlag;
631 // any target flags left?
633 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
636 hostFlags |= O_BINARY;
639 // Adjust path for current working directory
640 path = process->fullPath(path);
642 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
646 if (startswith(path, "/proc/") || startswith(path, "/system/") ||
647 startswith(path, "/platform/") || startswith(path, "/sys/")) {
648 // It's a proc/sys entry and requires special handling
649 fd = OS::openSpecialFile(path, process, tc);
650 local_errno = ENOENT;
653 fd = open(path.c_str(), hostFlags, mode);
660 return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
663 /// Target sysinfo() handler.
666 sysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
671 TypedBufferArg<typename OS::tgt_sysinfo>
672 sysinfo(process->getSyscallArg(tc, index));
674 sysinfo->uptime=seconds_since_epoch;
675 sysinfo->totalram=process->system->memSize();
677 sysinfo.copyOut(tc->getMemProxy());
682 /// Target chmod() handler.
685 chmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
691 if (!tc->getMemProxy().tryReadString(path,
692 process->getSyscallArg(tc, index))) {
696 uint32_t mode = process->getSyscallArg(tc, index);
699 // XXX translate mode flags via OS::something???
702 // Adjust path for current working directory
703 path = process->fullPath(path);
706 int result = chmod(path.c_str(), hostMode);
714 /// Target fchmod() handler.
717 fchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
721 int fd = process->getSyscallArg(tc, index);
722 if (fd < 0 || process->sim_fd(fd) < 0) {
723 // doesn't map to any simulator fd: not a valid target fd
727 uint32_t mode = process->getSyscallArg(tc, index);
730 // XXX translate mode flags via OS::someting???
734 int result = fchmod(process->sim_fd(fd), hostMode);
741 /// Target mremap() handler.
744 mremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
747 Addr start = process->getSyscallArg(tc, index);
748 uint64_t old_length = process->getSyscallArg(tc, index);
749 uint64_t new_length = process->getSyscallArg(tc, index);
750 uint64_t flags = process->getSyscallArg(tc, index);
752 if ((start % TheISA::VMPageSize != 0) ||
753 (new_length % TheISA::VMPageSize != 0)) {
754 warn("mremap failing: arguments not page aligned");
758 if (new_length > old_length) {
759 if ((start + old_length) == process->mmap_end) {
760 uint64_t diff = new_length - old_length;
761 process->allocateMem(process->mmap_end, diff);
762 process->mmap_end += diff;
765 // sys/mman.h defined MREMAP_MAYMOVE
767 warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
770 process->pTable->remap(start, old_length, process->mmap_end);
771 warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
772 process->mmap_end, process->mmap_end + new_length, new_length);
773 start = process->mmap_end;
774 // add on the remaining unallocated pages
775 process->allocateMem(start + old_length,
776 new_length - old_length);
777 process->mmap_end += new_length;
778 warn("returning %08p as start\n", start);
783 process->pTable->unmap(start + new_length, old_length - new_length);
788 /// Target stat() handler.
791 statFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
797 if (!tc->getMemProxy().tryReadString(path,
798 process->getSyscallArg(tc, index))) {
801 Addr bufPtr = process->getSyscallArg(tc, index);
803 // Adjust path for current working directory
804 path = process->fullPath(path);
807 int result = stat(path.c_str(), &hostBuf);
812 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
818 /// Target stat64() handler.
821 stat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
827 if (!tc->getMemProxy().tryReadString(path,
828 process->getSyscallArg(tc, index)))
830 Addr bufPtr = process->getSyscallArg(tc, index);
832 // Adjust path for current working directory
833 path = process->fullPath(path);
837 int result = stat(path.c_str(), &hostBuf);
839 struct stat64 hostBuf;
840 int result = stat64(path.c_str(), &hostBuf);
846 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
852 /// Target fstat64() handler.
855 fstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
859 int fd = process->getSyscallArg(tc, index);
860 Addr bufPtr = process->getSyscallArg(tc, index);
861 if (fd < 0 || process->sim_fd(fd) < 0) {
862 // doesn't map to any simulator fd: not a valid target fd
868 int result = fstat(process->sim_fd(fd), &hostBuf);
870 struct stat64 hostBuf;
871 int result = fstat64(process->sim_fd(fd), &hostBuf);
877 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
883 /// Target lstat() handler.
886 lstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
892 if (!tc->getMemProxy().tryReadString(path,
893 process->getSyscallArg(tc, index))) {
896 Addr bufPtr = process->getSyscallArg(tc, index);
898 // Adjust path for current working directory
899 path = process->fullPath(path);
902 int result = lstat(path.c_str(), &hostBuf);
907 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
912 /// Target lstat64() handler.
915 lstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
921 if (!tc->getMemProxy().tryReadString(path,
922 process->getSyscallArg(tc, index))) {
925 Addr bufPtr = process->getSyscallArg(tc, index);
927 // Adjust path for current working directory
928 path = process->fullPath(path);
932 int result = lstat(path.c_str(), &hostBuf);
934 struct stat64 hostBuf;
935 int result = lstat64(path.c_str(), &hostBuf);
941 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
946 /// Target fstat() handler.
949 fstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
953 int fd = process->sim_fd(process->getSyscallArg(tc, index));
954 Addr bufPtr = process->getSyscallArg(tc, index);
956 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
962 int result = fstat(fd, &hostBuf);
967 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
973 /// Target statfs() handler.
976 statfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
982 if (!tc->getMemProxy().tryReadString(path,
983 process->getSyscallArg(tc, index))) {
986 Addr bufPtr = process->getSyscallArg(tc, index);
988 // Adjust path for current working directory
989 path = process->fullPath(path);
991 struct statfs hostBuf;
992 int result = statfs(path.c_str(), &hostBuf);
997 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1003 /// Target fstatfs() handler.
1006 fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1010 int fd = process->sim_fd(process->getSyscallArg(tc, index));
1011 Addr bufPtr = process->getSyscallArg(tc, index);
1016 struct statfs hostBuf;
1017 int result = fstatfs(fd, &hostBuf);
1022 OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1028 /// Target writev() handler.
1031 writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1035 int fd = process->getSyscallArg(tc, index);
1036 if (fd < 0 || process->sim_fd(fd) < 0) {
1037 // doesn't map to any simulator fd: not a valid target fd
1041 SETranslatingPortProxy &p = tc->getMemProxy();
1042 uint64_t tiov_base = process->getSyscallArg(tc, index);
1043 size_t count = process->getSyscallArg(tc, index);
1044 struct iovec hiov[count];
1045 for (size_t i = 0; i < count; ++i) {
1046 typename OS::tgt_iovec tiov;
1048 p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1049 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1050 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1051 hiov[i].iov_base = new char [hiov[i].iov_len];
1052 p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1056 int result = writev(process->sim_fd(fd), hiov, count);
1058 for (size_t i = 0; i < count; ++i)
1059 delete [] (char *)hiov[i].iov_base;
1068 /// Target mmap() handler.
1070 /// We don't really handle mmap(). If the target is mmaping an
1071 /// anonymous region or /dev/zero, we can get away with doing basically
1072 /// nothing (since memory is initialized to zero and the simulator
1073 /// doesn't really check addresses anyway).
1077 mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1080 Addr start = p->getSyscallArg(tc, index);
1081 uint64_t length = p->getSyscallArg(tc, index);
1082 index++; // int prot = p->getSyscallArg(tc, index);
1083 int flags = p->getSyscallArg(tc, index);
1084 int tgt_fd = p->getSyscallArg(tc, index);
1085 // int offset = p->getSyscallArg(tc, index);
1087 if (length > 0x100000000ULL)
1088 warn("mmap length argument %#x is unreasonably large.\n", length);
1090 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1091 Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1092 if (!fd_map || fd_map->fd < 0) {
1093 warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1097 if (fd_map->filename != "/dev/zero") {
1098 // This is very likely broken, but leave a warning here
1099 // (rather than panic) in case /dev/zero is known by
1100 // another name on some platform
1101 warn("allowing mmap of file %s; mmap not supported on files"
1102 " other than /dev/zero\n", fd_map->filename);
1106 if ((start % TheISA::VMPageSize) != 0 ||
1107 (length % TheISA::VMPageSize) != 0) {
1108 warn("mmap failing: arguments not page-aligned: "
1109 "start 0x%x length 0x%x",
1114 // are we ok with clobbering existing mappings? only set this to
1115 // true if the user has been warned.
1116 bool clobber = false;
1118 // try to use the caller-provided address if there is one
1119 bool use_provided_address = (start != 0);
1121 if (use_provided_address) {
1122 // check to see if the desired address is already in use
1123 if (!p->pTable->isUnmapped(start, length)) {
1124 // there are existing mappings in the desired range
1125 // whether we clobber them or not depends on whether the caller
1126 // specified MAP_FIXED
1127 if (flags & OS::TGT_MAP_FIXED) {
1128 // MAP_FIXED specified: clobber existing mappings
1129 warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
1133 // MAP_FIXED not specified: ignore suggested start address
1134 warn("mmap: ignoring suggested map address 0x%x\n", start);
1135 use_provided_address = false;
1140 if (!use_provided_address) {
1141 // no address provided, or provided address unusable:
1142 // pick next address from our "mmap region"
1143 if (OS::mmapGrowsDown()) {
1144 start = p->mmap_end - length;
1145 p->mmap_end = start;
1147 start = p->mmap_end;
1148 p->mmap_end += length;
1152 p->allocateMem(start, length, clobber);
1157 /// Target getrlimit() handler.
1160 getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1164 unsigned resource = process->getSyscallArg(tc, index);
1165 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1168 case OS::TGT_RLIMIT_STACK:
1169 // max stack size in bytes: make up a number (8MB for now)
1170 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1171 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1172 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1175 case OS::TGT_RLIMIT_DATA:
1176 // max data segment size in bytes: make up a number
1177 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1178 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1179 rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1183 std::cerr << "getrlimitFunc: unimplemented resource " << resource
1189 rlp.copyOut(tc->getMemProxy());
1193 /// Target gettimeofday() handler.
1196 gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1200 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1202 getElapsedTime(tp->tv_sec, tp->tv_usec);
1203 tp->tv_sec += seconds_since_epoch;
1204 tp->tv_sec = TheISA::htog(tp->tv_sec);
1205 tp->tv_usec = TheISA::htog(tp->tv_usec);
1207 tp.copyOut(tc->getMemProxy());
1213 /// Target utimes() handler.
1216 utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1222 if (!tc->getMemProxy().tryReadString(path,
1223 process->getSyscallArg(tc, index))) {
1227 TypedBufferArg<typename OS::timeval [2]>
1228 tp(process->getSyscallArg(tc, index));
1229 tp.copyIn(tc->getMemProxy());
1231 struct timeval hostTimeval[2];
1232 for (int i = 0; i < 2; ++i)
1234 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1235 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1238 // Adjust path for current working directory
1239 path = process->fullPath(path);
1241 int result = utimes(path.c_str(), hostTimeval);
1248 /// Target getrusage() function.
1251 getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1255 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1256 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1258 rup->ru_utime.tv_sec = 0;
1259 rup->ru_utime.tv_usec = 0;
1260 rup->ru_stime.tv_sec = 0;
1261 rup->ru_stime.tv_usec = 0;
1269 rup->ru_inblock = 0;
1270 rup->ru_oublock = 0;
1273 rup->ru_nsignals = 0;
1278 case OS::TGT_RUSAGE_SELF:
1279 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1280 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1281 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1284 case OS::TGT_RUSAGE_CHILDREN:
1285 // do nothing. We have no child processes, so they take no time.
1289 // don't really handle THREAD or CHILDREN, but just warn and
1291 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
1295 rup.copyOut(tc->getMemProxy());
1300 /// Target times() function.
1303 timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1307 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1309 // Fill in the time structure (in clocks)
1310 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1311 bufp->tms_utime = clocks;
1312 bufp->tms_stime = 0;
1313 bufp->tms_cutime = 0;
1314 bufp->tms_cstime = 0;
1316 // Convert to host endianness
1317 bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1320 bufp.copyOut(tc->getMemProxy());
1322 // Return clock ticks since system boot
1326 /// Target time() function.
1329 timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1332 typename OS::time_t sec, usec;
1333 getElapsedTime(sec, usec);
1334 sec += seconds_since_epoch;
1337 Addr taddr = (Addr)process->getSyscallArg(tc, index);
1339 typename OS::time_t t = sec;
1340 t = TheISA::htog(t);
1341 SETranslatingPortProxy &p = tc->getMemProxy();
1342 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1348 #endif // __SIM_SYSCALL_EMUL_HH__