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 <sys/fcntl.h> // for O_BINARY
44 #include "base/intmath.hh" // for RoundUp
45 #include "mem/functional/functional.hh"
46 #include "targetarch/isa_traits.hh" // for Addr
48 #include "base/trace.hh"
49 #include "cpu/exec_context.hh"
50 #include "sim/process.hh"
53 /// System call descriptor.
59 /// Typedef for target syscall handler functions.
60 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
61 Process *, ExecContext *);
63 const char *name; //!< Syscall name (e.g., "open").
64 FuncPtr funcPtr; //!< Pointer to emulation function.
65 int flags; //!< Flags (see Flags enum).
67 /// Flag values for controlling syscall behavior.
69 /// Don't set return regs according to funcPtr return value.
70 /// Used for syscalls with non-standard return conventions
71 /// that explicitly set the ExecContext regs (e.g.,
73 SuppressReturnValue = 1
77 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
78 : name(_name), funcPtr(_funcPtr), flags(_flags)
82 /// Emulate the syscall. Public interface for calling through funcPtr.
83 void doSyscall(int callnum, Process *proc, ExecContext *xc);
91 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
93 bufPtr = new uint8_t[size];
94 // clear out buffer: in case we only partially populate this,
95 // and then do a copyOut(), we want to make sure we don't
96 // introduce any random junk into the simulated address space
97 memset(bufPtr, 0, size);
100 virtual ~BaseBufferArg() { delete [] bufPtr; }
103 // copy data into simulator space (read from target memory)
105 virtual bool copyIn(FunctionalMemory *mem)
107 mem->access(Read, addr, bufPtr, size);
108 return true; // no EFAULT detection for now
112 // copy data out of simulator space (write to target memory)
114 virtual bool copyOut(FunctionalMemory *mem)
116 mem->access(Write, addr, bufPtr, size);
117 return true; // no EFAULT detection for now
127 class BufferArg : public BaseBufferArg
130 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
131 void *bufferPtr() { return bufPtr; }
135 class TypedBufferArg : public BaseBufferArg
138 // user can optionally specify a specific number of bytes to
139 // allocate to deal with those structs that have variable-size
141 TypedBufferArg(Addr _addr, int _size = sizeof(T))
142 : BaseBufferArg(_addr, _size)
146 operator T*() { return (T *)bufPtr; }
148 // dereference operators
149 T &operator*() { return *((T *)bufPtr); }
150 T* operator->() { return (T *)bufPtr; }
151 T &operator[](int i) { return ((T *)bufPtr)[i]; }
154 //////////////////////////////////////////////////////////////////////
156 // The following emulation functions are generic enough that they
157 // don't need to be recompiled for different emulated OS's. They are
158 // defined in sim/syscall_emul.cc.
160 //////////////////////////////////////////////////////////////////////
163 /// Handler for unimplemented syscalls that we haven't thought about.
164 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
165 Process *p, ExecContext *xc);
167 /// Handler for unimplemented syscalls that we never intend to
168 /// implement (signal handling, etc.) and should not affect the correct
169 /// behavior of the program. Print a warning only if the appropriate
170 /// trace flag is enabled. Return success to the target program.
171 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
172 Process *p, ExecContext *xc);
174 /// Target exit() handler: terminate simulation.
175 SyscallReturn exitFunc(SyscallDesc *desc, int num,
176 Process *p, ExecContext *xc);
178 /// Target getpagesize() handler.
179 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
180 Process *p, ExecContext *xc);
182 /// Target obreak() handler: set brk address.
183 SyscallReturn obreakFunc(SyscallDesc *desc, int num,
184 Process *p, ExecContext *xc);
186 /// Target close() handler.
187 SyscallReturn closeFunc(SyscallDesc *desc, int num,
188 Process *p, ExecContext *xc);
190 /// Target read() handler.
191 SyscallReturn readFunc(SyscallDesc *desc, int num,
192 Process *p, ExecContext *xc);
194 /// Target write() handler.
195 SyscallReturn writeFunc(SyscallDesc *desc, int num,
196 Process *p, ExecContext *xc);
198 /// Target lseek() handler.
199 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
200 Process *p, ExecContext *xc);
202 /// Target munmap() handler.
203 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
204 Process *p, ExecContext *xc);
206 /// Target gethostname() handler.
207 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
208 Process *p, ExecContext *xc);
210 /// Target unlink() handler.
211 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
212 Process *p, ExecContext *xc);
214 /// Target rename() handler.
215 SyscallReturn renameFunc(SyscallDesc *desc, int num,
216 Process *p, ExecContext *xc);
219 /// Target truncate() handler.
220 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
221 Process *p, ExecContext *xc);
224 /// Target ftruncate() handler.
225 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
226 Process *p, ExecContext *xc);
229 /// This struct is used to build an target-OS-dependent table that
230 /// maps the target's open() flags to the host open() flags.
231 struct OpenFlagTransTable {
232 int tgtFlag; //!< Target system flag value.
233 int hostFlag; //!< Corresponding host system flag value.
238 /// A readable name for 1,000,000, for converting microseconds to seconds.
239 const int one_million = 1000000;
241 /// Approximate seconds since the epoch (1/1/1970). About a billion,
242 /// by my reckoning. We want to keep this a constant (not use the
243 /// real-world time) to keep simulations repeatable.
244 const unsigned seconds_since_epoch = 1000000000;
246 /// Helper function to convert current elapsed time to seconds and
248 template <class T1, class T2>
250 getElapsedTime(T1 &sec, T2 &usec)
252 int elapsed_usecs = curTick / Clock::Int::us;
253 sec = elapsed_usecs / one_million;
254 usec = elapsed_usecs % one_million;
257 //////////////////////////////////////////////////////////////////////
259 // The following emulation functions are generic, but need to be
260 // templated to account for differences in types, constants, etc.
262 //////////////////////////////////////////////////////////////////////
264 /// Target ioctl() handler. For the most part, programs call ioctl()
265 /// only to find out if their stdout is a tty, to determine whether to
266 /// do line or block buffering.
269 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
272 int fd = xc->getSyscallArg(0);
273 unsigned req = xc->getSyscallArg(1);
275 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
277 if (fd < 0 || process->sim_fd(fd) < 0) {
278 // doesn't map to any simulator fd: not a valid target fd
294 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
295 fd, req, xc->readPC());
299 /// Target open() handler.
302 openFunc(SyscallDesc *desc, int callnum, Process *process,
307 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
310 if (path == "/dev/sysdev0") {
311 // This is a memory-mapped high-resolution timer device on Alpha.
312 // We don't support it, so just punt.
313 warn("Ignoring open(%s, ...)\n", path);
317 int tgtFlags = xc->getSyscallArg(1);
318 int mode = xc->getSyscallArg(2);
321 // translate open flags
322 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
323 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
324 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
325 hostFlags |= OS::openFlagTable[i].hostFlag;
329 // any target flags left?
331 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
334 hostFlags |= O_BINARY;
337 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
340 int fd = open(path.c_str(), hostFlags, mode);
342 return (fd == -1) ? -errno : process->alloc_fd(fd);
346 /// Target stat() handler.
349 statFunc(SyscallDesc *desc, int callnum, Process *process,
354 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
358 int result = stat(path.c_str(), &hostBuf);
363 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
369 /// Target lstat() handler.
372 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
377 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
381 int result = lstat(path.c_str(), &hostBuf);
386 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
391 /// Target fstat() handler.
394 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
397 int fd = process->sim_fd(xc->getSyscallArg(0));
399 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
405 int result = fstat(fd, &hostBuf);
410 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
416 /// Target statfs() handler.
419 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
424 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
427 struct statfs hostBuf;
428 int result = statfs(path.c_str(), &hostBuf);
433 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
439 /// Target fstatfs() handler.
442 fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
445 int fd = process->sim_fd(xc->getSyscallArg(0));
450 struct statfs hostBuf;
451 int result = fstatfs(fd, &hostBuf);
456 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
462 /// Target mmap() handler.
464 /// We don't really handle mmap(). If the target is mmaping an
465 /// anonymous region or /dev/zero, we can get away with doing basically
466 /// nothing (since memory is initialized to zero and the simulator
467 /// doesn't really check addresses anyway). Always print a warning,
468 /// since this could be seriously broken if we're not mapping
471 /// Someday we should explicitly check for /dev/zero in open, flag the
472 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
476 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
478 Addr start = xc->getSyscallArg(0);
479 uint64_t length = xc->getSyscallArg(1);
480 // int prot = xc->getSyscallArg(2);
481 int flags = xc->getSyscallArg(3);
482 // int fd = p->sim_fd(xc->getSyscallArg(4));
483 // int offset = xc->getSyscallArg(5);
486 // user didn't give an address... pick one from our "mmap region"
488 p->mmap_end += RoundUp<Addr>(length, VMPageSize);
489 if (p->nxm_start != 0) {
490 //If we have an nxm space, make sure we haven't colided
491 assert(p->mmap_end < p->nxm_start);
495 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
496 warn("allowing mmap of file @ fd %d. "
497 "This will break if not /dev/zero.", xc->getSyscallArg(4));
503 /// Target getrlimit() handler.
506 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
509 unsigned resource = xc->getSyscallArg(0);
510 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
513 case OS::RLIMIT_STACK:
514 // max stack size in bytes: make up a number (2MB for now)
515 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
519 std::cerr << "getrlimitFunc: unimplemented resource " << resource
525 rlp.copyOut(xc->mem);
529 /// Target gettimeofday() handler.
532 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
535 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
537 getElapsedTime(tp->tv_sec, tp->tv_usec);
538 tp->tv_sec += seconds_since_epoch;
546 /// Target getrusage() function.
549 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
552 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
553 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
555 if (who != OS::RUSAGE_SELF) {
556 // don't really handle THREAD or CHILDREN, but just warn and
558 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
562 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
563 rup->ru_stime.tv_sec = 0;
564 rup->ru_stime.tv_usec = 0;
576 rup->ru_nsignals = 0;
580 rup.copyOut(xc->mem);
585 #endif // __SIM_SYSCALL_EMUL_HH__