2 * Copyright (c) 2003-2004 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_mem/functional_memory.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 int (*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 int unimplementedFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
163 /// Handler for unimplemented syscalls that we never intend to
164 /// implement (signal handling, etc.) and should not affect the correct
165 /// behavior of the program. Print a warning only if the appropriate
166 /// trace flag is enabled. Return success to the target program.
167 int ignoreFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
169 /// Target exit() handler: terminate simulation.
170 int exitFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
172 /// Target getpagesize() handler.
173 int getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
175 /// Target obreak() handler: set brk address.
176 int obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
178 /// Target close() handler.
179 int closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
181 /// Target read() handler.
182 int readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
184 /// Target write() handler.
185 int writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
187 /// Target lseek() handler.
188 int lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
190 /// Target munmap() handler.
191 int munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
193 /// Target gethostname() handler.
194 int gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
196 /// Target unlink() handler.
197 int unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
199 /// Target rename() handler.
200 int renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
202 /// This struct is used to build an target-OS-dependent table that
203 /// maps the target's open() flags to the host open() flags.
204 struct OpenFlagTransTable {
205 int tgtFlag; //!< Target system flag value.
206 int hostFlag; //!< Corresponding host system flag value.
211 /// A readable name for 1,000,000, for converting microseconds to seconds.
212 const int one_million = 1000000;
214 /// Approximate seconds since the epoch (1/1/1970). About a billion,
215 /// by my reckoning. We want to keep this a constant (not use the
216 /// real-world time) to keep simulations repeatable.
217 const unsigned seconds_since_epoch = 1000000000;
219 /// Helper function to convert current elapsed time to seconds and
221 template <class T1, class T2>
223 getElapsedTime(T1 &sec, T2 &usec)
225 int cycles_per_usec = ticksPerSecond / one_million;
227 int elapsed_usecs = curTick / cycles_per_usec;
228 sec = elapsed_usecs / one_million;
229 usec = elapsed_usecs % one_million;
232 //////////////////////////////////////////////////////////////////////
234 // The following emulation functions are generic, but need to be
235 // templated to account for differences in types, constants, etc.
237 //////////////////////////////////////////////////////////////////////
239 /// Target ioctl() handler. For the most part, programs call ioctl()
240 /// only to find out if their stdout is a tty, to determine whether to
241 /// do line or block buffering.
244 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
247 int fd = xc->getSyscallArg(0);
248 unsigned req = xc->getSyscallArg(1);
250 // DPRINTFR(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
252 if (fd < 0 || process->sim_fd(fd) < 0) {
253 // doesn't map to any simulator fd: not a valid target fd
269 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", fd, req, xc->readPC());
273 /// Target open() handler.
276 openFunc(SyscallDesc *desc, int callnum, Process *process,
281 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
284 if (path == "/dev/sysdev0") {
285 // This is a memory-mapped high-resolution timer device on Alpha.
286 // We don't support it, so just punt.
287 DCOUT(SyscallWarnings) << "Ignoring open(" << path << ", ...)" << std::endl;
291 int tgtFlags = xc->getSyscallArg(1);
292 int mode = xc->getSyscallArg(2);
295 // translate open flags
296 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
297 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
298 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
299 hostFlags |= OS::openFlagTable[i].hostFlag;
303 // any target flags left?
305 std::cerr << "Syscall: open: cannot decode flags: " << tgtFlags << std::endl;
308 hostFlags |= O_BINARY;
312 int fd = open(path.c_str(), hostFlags, mode);
314 return (fd == -1) ? -errno : process->open_fd(fd);
318 /// Target stat() handler.
321 statFunc(SyscallDesc *desc, int callnum, Process *process,
326 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
330 int result = stat(path.c_str(), &hostBuf);
335 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
341 /// Target lstat() handler.
344 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
349 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
353 int result = lstat(path.c_str(), &hostBuf);
358 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
363 /// Target fstat() handler.
366 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
369 int fd = process->sim_fd(xc->getSyscallArg(0));
371 // DPRINTFR(SyscallVerbose, "fstat(%d, ...)\n", fd);
377 int result = fstat(fd, &hostBuf);
382 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
388 /// Target mmap() handler.
390 /// We don't really handle mmap(). If the target is mmaping an
391 /// anonymous region or /dev/zero, we can get away with doing basically
392 /// nothing (since memory is initialized to zero and the simulator
393 /// doesn't really check addresses anyway). Always print a warning,
394 /// since this could be seriously broken if we're not mapping
397 /// Someday we should explicitly check for /dev/zero in open, flag the
398 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
402 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
404 Addr start = xc->getSyscallArg(0);
405 uint64_t length = xc->getSyscallArg(1);
406 // int prot = xc->getSyscallArg(2);
407 int flags = xc->getSyscallArg(3);
408 // int fd = p->sim_fd(xc->getSyscallArg(4));
409 // int offset = xc->getSyscallArg(5);
412 // user didn't give an address... pick one from our "mmap region"
413 start = p->mmap_base;
414 p->mmap_base += RoundUp<Addr>(length, VMPageSize);
417 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
418 DPRINTF(SyscallWarnings, "Warning: allowing mmap of file @ fd %d. "
419 "This will break if not /dev/zero.", xc->getSyscallArg(4));
425 /// Target getrlimit() handler.
428 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
431 unsigned resource = xc->getSyscallArg(0);
432 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
435 case OS::RLIMIT_STACK:
436 // max stack size in bytes: make up a number (2MB for now)
437 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
441 std::cerr << "getrlimitFunc: unimplemented resource " << resource << std::endl;
446 rlp.copyOut(xc->mem);
450 /// Target gettimeofday() handler.
453 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
456 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
458 getElapsedTime(tp->tv_sec, tp->tv_usec);
459 tp->tv_sec += seconds_since_epoch;
467 /// Target getrusage() function.
470 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
473 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
474 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
476 if (who != OS::RUSAGE_SELF) {
477 // don't really handle THREAD or CHILDREN, but just warn and
479 DCOUT(SyscallWarnings)
480 << "Warning: getrusage() only supports RUSAGE_SELF."
481 << " Parameter " << who << " ignored." << std::endl;
484 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
485 rup->ru_stime.tv_sec = 0;
486 rup->ru_stime.tv_usec = 0;
498 rup->ru_nsignals = 0;
502 rup.copyOut(xc->mem);
507 #endif // __SIM_SYSCALL_EMUL_HH__