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.
29 #ifndef __SIM_SYSCALL_EMUL_HH__
30 #define __SIM_SYSCALL_EMUL_HH__
32 #define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \
36 /// @file syscall_emul.hh
38 /// This file defines objects used to emulate syscalls from the target
39 /// application on the host machine.
44 #include <sys/fcntl.h> // for O_BINARY
48 #include "base/intmath.hh" // for RoundUp
49 #include "mem/functional/functional.hh"
50 #include "arch/isa_traits.hh" // for Addr
52 #include "base/trace.hh"
53 #include "cpu/exec_context.hh"
54 #include "sim/process.hh"
57 /// System call descriptor.
63 /// Typedef for target syscall handler functions.
64 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
65 Process *, ExecContext *);
67 const char *name; //!< Syscall name (e.g., "open").
68 FuncPtr funcPtr; //!< Pointer to emulation function.
69 int flags; //!< Flags (see Flags enum).
71 /// Flag values for controlling syscall behavior.
73 /// Don't set return regs according to funcPtr return value.
74 /// Used for syscalls with non-standard return conventions
75 /// that explicitly set the ExecContext regs (e.g.,
77 SuppressReturnValue = 1
81 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
82 : name(_name), funcPtr(_funcPtr), flags(_flags)
86 /// Emulate the syscall. Public interface for calling through funcPtr.
87 void doSyscall(int callnum, Process *proc, ExecContext *xc);
95 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
97 bufPtr = new uint8_t[size];
98 // clear out buffer: in case we only partially populate this,
99 // and then do a copyOut(), we want to make sure we don't
100 // introduce any random junk into the simulated address space
101 memset(bufPtr, 0, size);
104 virtual ~BaseBufferArg() { delete [] bufPtr; }
107 // copy data into simulator space (read from target memory)
109 virtual bool copyIn(FunctionalMemory *mem)
111 mem->access(Read, addr, bufPtr, size);
112 return true; // no EFAULT detection for now
116 // copy data out of simulator space (write to target memory)
118 virtual bool copyOut(FunctionalMemory *mem)
120 mem->access(Write, addr, bufPtr, size);
121 return true; // no EFAULT detection for now
131 class BufferArg : public BaseBufferArg
134 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
135 void *bufferPtr() { return bufPtr; }
139 class TypedBufferArg : public BaseBufferArg
142 // user can optionally specify a specific number of bytes to
143 // allocate to deal with those structs that have variable-size
145 TypedBufferArg(Addr _addr, int _size = sizeof(T))
146 : BaseBufferArg(_addr, _size)
150 operator T*() { return (T *)bufPtr; }
152 // dereference operators
153 T &operator*() { return *((T *)bufPtr); }
154 T* operator->() { return (T *)bufPtr; }
155 T &operator[](int i) { return ((T *)bufPtr)[i]; }
158 //////////////////////////////////////////////////////////////////////
160 // The following emulation functions are generic enough that they
161 // don't need to be recompiled for different emulated OS's. They are
162 // defined in sim/syscall_emul.cc.
164 //////////////////////////////////////////////////////////////////////
167 /// Handler for unimplemented syscalls that we haven't thought about.
168 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
169 Process *p, ExecContext *xc);
171 /// Handler for unimplemented syscalls that we never intend to
172 /// implement (signal handling, etc.) and should not affect the correct
173 /// behavior of the program. Print a warning only if the appropriate
174 /// trace flag is enabled. Return success to the target program.
175 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
176 Process *p, ExecContext *xc);
178 /// Target exit() handler: terminate simulation.
179 SyscallReturn exitFunc(SyscallDesc *desc, int num,
180 Process *p, ExecContext *xc);
182 /// Target getpagesize() handler.
183 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
184 Process *p, ExecContext *xc);
186 /// Target obreak() handler: set brk address.
187 SyscallReturn obreakFunc(SyscallDesc *desc, int num,
188 Process *p, ExecContext *xc);
190 /// Target close() handler.
191 SyscallReturn closeFunc(SyscallDesc *desc, int num,
192 Process *p, ExecContext *xc);
194 /// Target read() handler.
195 SyscallReturn readFunc(SyscallDesc *desc, int num,
196 Process *p, ExecContext *xc);
198 /// Target write() handler.
199 SyscallReturn writeFunc(SyscallDesc *desc, int num,
200 Process *p, ExecContext *xc);
202 /// Target lseek() handler.
203 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
204 Process *p, ExecContext *xc);
206 /// Target munmap() handler.
207 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
208 Process *p, ExecContext *xc);
210 /// Target gethostname() handler.
211 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
212 Process *p, ExecContext *xc);
214 /// Target unlink() handler.
215 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
216 Process *p, ExecContext *xc);
218 /// Target rename() handler.
219 SyscallReturn renameFunc(SyscallDesc *desc, int num,
220 Process *p, ExecContext *xc);
223 /// Target truncate() handler.
224 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
225 Process *p, ExecContext *xc);
228 /// Target ftruncate() handler.
229 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
230 Process *p, ExecContext *xc);
233 /// Target chown() handler.
234 SyscallReturn chownFunc(SyscallDesc *desc, int num,
235 Process *p, ExecContext *xc);
238 /// Target fchown() handler.
239 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
240 Process *p, ExecContext *xc);
242 /// Target fnctl() handler.
243 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
244 Process *process, ExecContext *xc);
246 /// This struct is used to build an target-OS-dependent table that
247 /// maps the target's open() flags to the host open() flags.
248 struct OpenFlagTransTable {
249 int tgtFlag; //!< Target system flag value.
250 int hostFlag; //!< Corresponding host system flag value.
255 /// A readable name for 1,000,000, for converting microseconds to seconds.
256 const int one_million = 1000000;
258 /// Approximate seconds since the epoch (1/1/1970). About a billion,
259 /// by my reckoning. We want to keep this a constant (not use the
260 /// real-world time) to keep simulations repeatable.
261 const unsigned seconds_since_epoch = 1000000000;
263 /// Helper function to convert current elapsed time to seconds and
265 template <class T1, class T2>
267 getElapsedTime(T1 &sec, T2 &usec)
269 int elapsed_usecs = curTick / Clock::Int::us;
270 sec = elapsed_usecs / one_million;
271 usec = elapsed_usecs % one_million;
274 //////////////////////////////////////////////////////////////////////
276 // The following emulation functions are generic, but need to be
277 // templated to account for differences in types, constants, etc.
279 //////////////////////////////////////////////////////////////////////
281 /// Target ioctl() handler. For the most part, programs call ioctl()
282 /// only to find out if their stdout is a tty, to determine whether to
283 /// do line or block buffering.
286 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
289 int fd = xc->getSyscallArg(0);
290 unsigned req = xc->getSyscallArg(1);
292 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
294 if (fd < 0 || process->sim_fd(fd) < 0) {
295 // doesn't map to any simulator fd: not a valid target fd
311 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
312 fd, req, xc->readPC());
316 /// Target open() handler.
319 openFunc(SyscallDesc *desc, int callnum, Process *process,
324 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
327 if (path == "/dev/sysdev0") {
328 // This is a memory-mapped high-resolution timer device on Alpha.
329 // We don't support it, so just punt.
330 warn("Ignoring open(%s, ...)\n", path);
334 int tgtFlags = xc->getSyscallArg(1);
335 int mode = xc->getSyscallArg(2);
338 // translate open flags
339 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
340 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
341 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
342 hostFlags |= OS::openFlagTable[i].hostFlag;
346 // any target flags left?
348 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
351 hostFlags |= O_BINARY;
354 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
357 int fd = open(path.c_str(), hostFlags, mode);
359 return (fd == -1) ? -errno : process->alloc_fd(fd);
363 /// Target chmod() handler.
366 chmodFunc(SyscallDesc *desc, int callnum, Process *process,
371 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
374 uint32_t mode = xc->getSyscallArg(1);
377 // XXX translate mode flags via OS::something???
381 int result = chmod(path.c_str(), hostMode);
389 /// Target fchmod() handler.
392 fchmodFunc(SyscallDesc *desc, int callnum, Process *process,
395 int fd = xc->getSyscallArg(0);
396 if (fd < 0 || process->sim_fd(fd) < 0) {
397 // doesn't map to any simulator fd: not a valid target fd
401 uint32_t mode = xc->getSyscallArg(1);
404 // XXX translate mode flags via OS::someting???
408 int result = fchmod(process->sim_fd(fd), hostMode);
416 /// Target stat() handler.
419 statFunc(SyscallDesc *desc, int callnum, Process *process,
424 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
428 int result = stat(path.c_str(), &hostBuf);
433 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
439 /// Target fstat64() handler.
442 fstat64Func(SyscallDesc *desc, int callnum, Process *process,
445 int fd = xc->getSyscallArg(0);
446 if (fd < 0 || process->sim_fd(fd) < 0) {
447 // doesn't map to any simulator fd: not a valid target fd
453 int result = fstat(process->sim_fd(fd), &hostBuf);
455 struct stat64 hostBuf;
456 int result = fstat64(process->sim_fd(fd), &hostBuf);
462 OS::copyOutStat64Buf(xc->mem, fd, xc->getSyscallArg(1), &hostBuf);
468 /// Target lstat() handler.
471 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
476 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
480 int result = lstat(path.c_str(), &hostBuf);
485 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
490 /// Target lstat64() handler.
493 lstat64Func(SyscallDesc *desc, int callnum, Process *process,
498 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
503 int result = lstat(path.c_str(), &hostBuf);
505 struct stat64 hostBuf;
506 int result = lstat64(path.c_str(), &hostBuf);
512 OS::copyOutStat64Buf(xc->mem, -1, xc->getSyscallArg(1), &hostBuf);
517 /// Target fstat() handler.
520 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
523 int fd = process->sim_fd(xc->getSyscallArg(0));
525 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
531 int result = fstat(fd, &hostBuf);
536 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
541 /// Target statfs() handler.
544 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
549 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
552 struct statfs hostBuf;
553 int result = statfs(path.c_str(), &hostBuf);
558 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
564 /// Target fstatfs() handler.
567 fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
570 int fd = process->sim_fd(xc->getSyscallArg(0));
575 struct statfs hostBuf;
576 int result = fstatfs(fd, &hostBuf);
581 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
587 /// Target writev() handler.
590 writevFunc(SyscallDesc *desc, int callnum, Process *process,
593 int fd = xc->getSyscallArg(0);
594 if (fd < 0 || process->sim_fd(fd) < 0) {
595 // doesn't map to any simulator fd: not a valid target fd
599 uint64_t tiov_base = xc->getSyscallArg(1);
600 size_t count = xc->getSyscallArg(2);
601 struct iovec hiov[count];
602 for (int i = 0; i < count; ++i)
604 typename OS::tgt_iovec tiov;
605 xc->mem->access(Read, tiov_base + i*sizeof(typename OS::tgt_iovec),
606 &tiov, sizeof(typename OS::tgt_iovec));
607 hiov[i].iov_len = gtoh(tiov.iov_len);
608 hiov[i].iov_base = new char [hiov[i].iov_len];
609 xc->mem->access(Read, gtoh(tiov.iov_base),
610 hiov[i].iov_base, hiov[i].iov_len);
613 int result = writev(process->sim_fd(fd), hiov, count);
615 for (int i = 0; i < count; ++i)
617 delete [] (char *)hiov[i].iov_base;
627 /// Target mmap() handler.
629 /// We don't really handle mmap(). If the target is mmaping an
630 /// anonymous region or /dev/zero, we can get away with doing basically
631 /// nothing (since memory is initialized to zero and the simulator
632 /// doesn't really check addresses anyway). Always print a warning,
633 /// since this could be seriously broken if we're not mapping
636 /// Someday we should explicitly check for /dev/zero in open, flag the
637 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
641 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
643 Addr start = xc->getSyscallArg(0);
644 uint64_t length = xc->getSyscallArg(1);
645 // int prot = xc->getSyscallArg(2);
646 int flags = xc->getSyscallArg(3);
647 // int fd = p->sim_fd(xc->getSyscallArg(4));
648 // int offset = xc->getSyscallArg(5);
651 // user didn't give an address... pick one from our "mmap region"
653 p->mmap_end += roundUp(length, TheISA::VMPageSize);
654 if (p->nxm_start != 0) {
655 //If we have an nxm space, make sure we haven't colided
656 assert(p->mmap_end < p->nxm_start);
660 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
661 warn("allowing mmap of file @ fd %d. "
662 "This will break if not /dev/zero.", xc->getSyscallArg(4));
668 /// Target getrlimit() handler.
671 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
674 unsigned resource = xc->getSyscallArg(0);
675 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
678 case OS::TGT_RLIMIT_STACK:
679 // max stack size in bytes: make up a number (2MB for now)
680 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
681 rlp->rlim_cur = htog(rlp->rlim_cur);
682 rlp->rlim_max = htog(rlp->rlim_max);
686 std::cerr << "getrlimitFunc: unimplemented resource " << resource
692 rlp.copyOut(xc->mem);
696 /// Target gettimeofday() handler.
699 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
702 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
704 getElapsedTime(tp->tv_sec, tp->tv_usec);
705 tp->tv_sec += seconds_since_epoch;
706 tp->tv_sec = htog(tp->tv_sec);
707 tp->tv_usec = htog(tp->tv_usec);
715 /// Target utimes() handler.
718 utimesFunc(SyscallDesc *desc, int callnum, Process *process,
723 if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault)
726 TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1));
729 struct timeval hostTimeval[2];
730 for (int i = 0; i < 2; ++i)
732 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
733 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
735 int result = utimes(path.c_str(), hostTimeval);
742 /// Target getrusage() function.
745 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
748 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
749 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
751 if (who != OS::TGT_RUSAGE_SELF) {
752 // don't really handle THREAD or CHILDREN, but just warn and
754 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
758 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
759 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
760 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
762 rup->ru_stime.tv_sec = 0;
763 rup->ru_stime.tv_usec = 0;
775 rup->ru_nsignals = 0;
779 rup.copyOut(xc->mem);
784 #endif // __SIM_SYSCALL_EMUL_HH__