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__
33 /// @file syscall_emul.hh
35 /// This file defines objects used to emulate syscalls from the target
36 /// application on the host machine.
41 #include "base/intmath.hh" // for RoundUp
42 #include "mem/functional/functional.hh"
43 #include "targetarch/isa_traits.hh" // for Addr
45 #include "base/trace.hh"
46 #include "cpu/exec_context.hh"
47 #include "sim/process.hh"
50 /// System call descriptor.
56 /// Typedef for target syscall handler functions.
57 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
58 Process *, ExecContext *);
60 const char *name; //!< Syscall name (e.g., "open").
61 FuncPtr funcPtr; //!< Pointer to emulation function.
62 int flags; //!< Flags (see Flags enum).
64 /// Flag values for controlling syscall behavior.
66 /// Don't set return regs according to funcPtr return value.
67 /// Used for syscalls with non-standard return conventions
68 /// that explicitly set the ExecContext regs (e.g.,
70 SuppressReturnValue = 1
74 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
75 : name(_name), funcPtr(_funcPtr), flags(_flags)
79 /// Emulate the syscall. Public interface for calling through funcPtr.
80 void doSyscall(int callnum, Process *proc, ExecContext *xc);
88 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
90 bufPtr = new uint8_t[size];
91 // clear out buffer: in case we only partially populate this,
92 // and then do a copyOut(), we want to make sure we don't
93 // introduce any random junk into the simulated address space
94 memset(bufPtr, 0, size);
97 virtual ~BaseBufferArg() { delete [] bufPtr; }
100 // copy data into simulator space (read from target memory)
102 virtual bool copyIn(FunctionalMemory *mem)
104 mem->access(Read, addr, bufPtr, size);
105 return true; // no EFAULT detection for now
109 // copy data out of simulator space (write to target memory)
111 virtual bool copyOut(FunctionalMemory *mem)
113 mem->access(Write, addr, bufPtr, size);
114 return true; // no EFAULT detection for now
124 class BufferArg : public BaseBufferArg
127 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
128 void *bufferPtr() { return bufPtr; }
132 class TypedBufferArg : public BaseBufferArg
135 // user can optionally specify a specific number of bytes to
136 // allocate to deal with those structs that have variable-size
138 TypedBufferArg(Addr _addr, int _size = sizeof(T))
139 : BaseBufferArg(_addr, _size)
143 operator T*() { return (T *)bufPtr; }
145 // dereference operators
146 T &operator*() { return *((T *)bufPtr); }
147 T* operator->() { return (T *)bufPtr; }
148 T &operator[](int i) { return ((T *)bufPtr)[i]; }
151 //////////////////////////////////////////////////////////////////////
153 // The following emulation functions are generic enough that they
154 // don't need to be recompiled for different emulated OS's. They are
155 // defined in sim/syscall_emul.cc.
157 //////////////////////////////////////////////////////////////////////
160 /// Handler for unimplemented syscalls that we haven't thought about.
161 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
162 Process *p, ExecContext *xc);
164 /// Handler for unimplemented syscalls that we never intend to
165 /// implement (signal handling, etc.) and should not affect the correct
166 /// behavior of the program. Print a warning only if the appropriate
167 /// trace flag is enabled. Return success to the target program.
168 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
169 Process *p, ExecContext *xc);
171 /// Target exit() handler: terminate simulation.
172 SyscallReturn exitFunc(SyscallDesc *desc, int num,
173 Process *p, ExecContext *xc);
175 /// Target getpagesize() handler.
176 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
177 Process *p, ExecContext *xc);
179 /// Target obreak() handler: set brk address.
180 SyscallReturn obreakFunc(SyscallDesc *desc, int num,
181 Process *p, ExecContext *xc);
183 /// Target close() handler.
184 SyscallReturn closeFunc(SyscallDesc *desc, int num,
185 Process *p, ExecContext *xc);
187 /// Target read() handler.
188 SyscallReturn readFunc(SyscallDesc *desc, int num,
189 Process *p, ExecContext *xc);
191 /// Target write() handler.
192 SyscallReturn writeFunc(SyscallDesc *desc, int num,
193 Process *p, ExecContext *xc);
195 /// Target lseek() handler.
196 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
197 Process *p, ExecContext *xc);
199 /// Target munmap() handler.
200 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
201 Process *p, ExecContext *xc);
203 /// Target gethostname() handler.
204 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
205 Process *p, ExecContext *xc);
207 /// Target unlink() handler.
208 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
209 Process *p, ExecContext *xc);
211 /// Target rename() handler.
212 SyscallReturn renameFunc(SyscallDesc *desc, int num,
213 Process *p, ExecContext *xc);
216 /// Target truncate() handler.
217 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
218 Process *p, ExecContext *xc);
221 /// Target ftruncate() handler.
222 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
223 Process *p, ExecContext *xc);
226 /// This struct is used to build an target-OS-dependent table that
227 /// maps the target's open() flags to the host open() flags.
228 struct OpenFlagTransTable {
229 int tgtFlag; //!< Target system flag value.
230 int hostFlag; //!< Corresponding host system flag value.
235 /// A readable name for 1,000,000, for converting microseconds to seconds.
236 const int one_million = 1000000;
238 /// Approximate seconds since the epoch (1/1/1970). About a billion,
239 /// by my reckoning. We want to keep this a constant (not use the
240 /// real-world time) to keep simulations repeatable.
241 const unsigned seconds_since_epoch = 1000000000;
243 /// Helper function to convert current elapsed time to seconds and
245 template <class T1, class T2>
247 getElapsedTime(T1 &sec, T2 &usec)
249 int elapsed_usecs = curTick / Clock::Int::us;
250 sec = elapsed_usecs / one_million;
251 usec = elapsed_usecs % one_million;
254 //////////////////////////////////////////////////////////////////////
256 // The following emulation functions are generic, but need to be
257 // templated to account for differences in types, constants, etc.
259 //////////////////////////////////////////////////////////////////////
261 /// Target ioctl() handler. For the most part, programs call ioctl()
262 /// only to find out if their stdout is a tty, to determine whether to
263 /// do line or block buffering.
266 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
269 int fd = xc->getSyscallArg(0);
270 unsigned req = xc->getSyscallArg(1);
272 // DPRINTFR(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
274 if (fd < 0 || process->sim_fd(fd) < 0) {
275 // doesn't map to any simulator fd: not a valid target fd
291 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
292 fd, req, xc->readPC());
296 /// Target open() handler.
299 openFunc(SyscallDesc *desc, int callnum, Process *process,
304 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
307 if (path == "/dev/sysdev0") {
308 // This is a memory-mapped high-resolution timer device on Alpha.
309 // We don't support it, so just punt.
310 warn("Ignoring open(%s, ...)\n", path);
314 int tgtFlags = xc->getSyscallArg(1);
315 int mode = xc->getSyscallArg(2);
318 // translate open flags
319 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
320 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
321 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
322 hostFlags |= OS::openFlagTable[i].hostFlag;
326 // any target flags left?
328 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
331 hostFlags |= O_BINARY;
334 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
337 int fd = open(path.c_str(), hostFlags, mode);
339 return (fd == -1) ? -errno : process->open_fd(fd);
343 /// Target stat() handler.
346 statFunc(SyscallDesc *desc, int callnum, Process *process,
351 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
355 int result = stat(path.c_str(), &hostBuf);
360 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
366 /// Target lstat() handler.
369 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
374 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
378 int result = lstat(path.c_str(), &hostBuf);
383 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
388 /// Target fstat() handler.
391 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
394 int fd = process->sim_fd(xc->getSyscallArg(0));
396 // DPRINTFR(SyscallVerbose, "fstat(%d, ...)\n", fd);
402 int result = fstat(fd, &hostBuf);
407 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
413 /// Target statfs() handler.
416 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
421 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
424 struct statfs hostBuf;
425 int result = statfs(path.c_str(), &hostBuf);
430 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
436 /// Target fstatfs() handler.
439 fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
442 int fd = process->sim_fd(xc->getSyscallArg(0));
447 struct statfs hostBuf;
448 int result = fstatfs(fd, &hostBuf);
453 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
459 /// Target mmap() handler.
461 /// We don't really handle mmap(). If the target is mmaping an
462 /// anonymous region or /dev/zero, we can get away with doing basically
463 /// nothing (since memory is initialized to zero and the simulator
464 /// doesn't really check addresses anyway). Always print a warning,
465 /// since this could be seriously broken if we're not mapping
468 /// Someday we should explicitly check for /dev/zero in open, flag the
469 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
473 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
475 Addr start = xc->getSyscallArg(0);
476 uint64_t length = xc->getSyscallArg(1);
477 // int prot = xc->getSyscallArg(2);
478 int flags = xc->getSyscallArg(3);
479 // int fd = p->sim_fd(xc->getSyscallArg(4));
480 // int offset = xc->getSyscallArg(5);
483 // user didn't give an address... pick one from our "mmap region"
485 p->mmap_end += RoundUp<Addr>(length, VMPageSize);
486 if (p->nxm_start != 0) {
487 //If we have an nxm space, make sure we haven't colided
488 assert(p->mmap_end < p->nxm_start);
492 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
493 DPRINTF(SyscallWarnings, "Warning: allowing mmap of file @ fd %d. "
494 "This will break if not /dev/zero.", xc->getSyscallArg(4));
500 /// Target getrlimit() handler.
503 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
506 unsigned resource = xc->getSyscallArg(0);
507 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
510 case OS::RLIMIT_STACK:
511 // max stack size in bytes: make up a number (2MB for now)
512 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
516 std::cerr << "getrlimitFunc: unimplemented resource " << resource
522 rlp.copyOut(xc->mem);
526 /// Target gettimeofday() handler.
529 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
532 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
534 getElapsedTime(tp->tv_sec, tp->tv_usec);
535 tp->tv_sec += seconds_since_epoch;
543 /// Target getrusage() function.
546 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
549 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
550 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
552 if (who != OS::RUSAGE_SELF) {
553 // don't really handle THREAD or CHILDREN, but just warn and
555 DCOUT(SyscallWarnings)
556 << "Warning: getrusage() only supports RUSAGE_SELF."
557 << " Parameter " << who << " ignored." << std::endl;
560 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
561 rup->ru_stime.tv_sec = 0;
562 rup->ru_stime.tv_usec = 0;
574 rup->ru_nsignals = 0;
578 rup.copyOut(xc->mem);
583 #endif // __SIM_SYSCALL_EMUL_HH__