2 * Copyright (c) 2003 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 __SYSCALL_EMUL_HH__
30 #define __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.
40 #include "base/intmath.hh" // for RoundUp
41 #include "targetarch/isa_traits.hh" // for Addr
42 #include "mem/functional_mem/functional_memory.hh"
48 /// System call descriptor.
54 /// Typedef for target syscall handler functions.
55 typedef int (*FuncPtr)(SyscallDesc *, int num,
56 Process *, ExecContext *);
58 const char *name; //!< Syscall name (e.g., "open").
59 FuncPtr funcPtr; //!< Pointer to emulation function.
60 int flags; //!< Flags (see Flags enum).
62 /// Flag values for controlling syscall behavior.
64 /// Don't set return regs according to funcPtr return value.
65 /// Used for syscalls with non-standard return conventions
66 /// that explicitly set the ExecContext regs (e.g.,
68 SuppressReturnValue = 1
72 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
73 : name(_name), funcPtr(_funcPtr), flags(_flags)
77 /// Emulate the syscall. Public interface for calling through funcPtr.
78 void doSyscall(int callnum, Process *proc, ExecContext *xc);
86 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
88 bufPtr = new uint8_t[size];
89 // clear out buffer: in case we only partially populate this,
90 // and then do a copyOut(), we want to make sure we don't
91 // introduce any random junk into the simulated address space
92 memset(bufPtr, 0, size);
95 virtual ~BaseBufferArg() { delete [] bufPtr; }
98 // copy data into simulator space (read from target memory)
100 virtual bool copyIn(FunctionalMemory *mem)
102 mem->access(Read, addr, bufPtr, size);
103 return true; // no EFAULT detection for now
107 // copy data out of simulator space (write to target memory)
109 virtual bool copyOut(FunctionalMemory *mem)
111 mem->access(Write, addr, bufPtr, size);
112 return true; // no EFAULT detection for now
122 class BufferArg : public BaseBufferArg
125 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
126 void *bufferPtr() { return bufPtr; }
130 class TypedBufferArg : public BaseBufferArg
133 // user can optionally specify a specific number of bytes to
134 // allocate to deal with those structs that have variable-size
136 TypedBufferArg(Addr _addr, int _size = sizeof(T))
137 : BaseBufferArg(_addr, _size)
141 operator T*() { return (T *)bufPtr; }
143 // dereference operators
144 T &operator*() { return *((T *)bufPtr); }
145 T* operator->() { return (T *)bufPtr; }
146 T &operator[](int i) { return ((T *)bufPtr)[i]; }
149 //////////////////////////////////////////////////////////////////////
151 // The following emulation functions are generic enough that they
152 // don't need to be recompiled for different emulated OS's. They are
153 // defined in sim/syscall_emul.cc.
155 //////////////////////////////////////////////////////////////////////
158 /// Handler for unimplemented syscalls that we haven't thought about.
159 int unimplementedFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
161 /// Handler for unimplemented syscalls that we never intend to
162 /// implement (signal handling, etc.) and should not affect the correct
163 /// behavior of the program. Print a warning only if the appropriate
164 /// trace flag is enabled. Return success to the target program.
165 int ignoreFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
167 /// Target exit() handler: terminate simulation.
168 int exitFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
170 /// Target getpagesize() handler.
171 int getpagesizeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
173 /// Target obreak() handler: set brk address.
174 int obreakFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
176 /// Target close() handler.
177 int closeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
179 /// Target read() handler.
180 int readFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
182 /// Target write() handler.
183 int writeFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
185 /// Target lseek() handler.
186 int lseekFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
188 /// Target munmap() handler.
189 int munmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
191 /// Target gethostname() handler.
192 int gethostnameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
194 //////////////////////////////////////////////////////////////////////
196 // The following emulation functions are generic, but need to be
197 // templated to account for differences in types, constants, etc.
199 //////////////////////////////////////////////////////////////////////
201 /// Target ioctl() handler. For the most part, programs call ioctl()
202 /// only to find out if their stdout is a tty, to determine whether to
203 /// do line or block buffering.
206 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
209 int fd = xc->getSyscallArg(0);
210 unsigned req = xc->getSyscallArg(1);
212 // DPRINTFR(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
214 if (fd < 0 || process->sim_fd(fd) < 0) {
215 // doesn't map to any simulator fd: not a valid target fd
229 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...)\n", fd, req);
233 /// This struct is used to build an target-OS-dependent table that
234 /// maps the target's open() flags to the host open() flags.
235 struct OpenFlagTransTable {
236 int tgtFlag; //!< Target system flag value.
237 int hostFlag; //!< Corresponding host system flag value.
241 /// Target open() handler.
244 openFunc(SyscallDesc *desc, int callnum, Process *process,
249 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
252 if (path == "/dev/sysdev0") {
253 // This is a memory-mapped high-resolution timer device on Alpha.
254 // We don't support it, so just punt.
255 DCOUT(SyscallWarnings) << "Ignoring open(" << path << ", ...)" << endl;
259 int tgtFlags = xc->getSyscallArg(1);
260 int mode = xc->getSyscallArg(2);
263 // translate open flags
264 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
265 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
266 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
267 hostFlags |= OS::openFlagTable[i].hostFlag;
271 // any target flags left?
273 cerr << "Syscall: open: cannot decode flags: " << tgtFlags << endl;
276 hostFlags |= O_BINARY;
280 int fd = open(path.c_str(), hostFlags, mode);
282 return (fd == -1) ? -errno : process->open_fd(fd);
286 /// Target stat() handler.
289 statFunc(SyscallDesc *desc, int callnum, Process *process,
294 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
298 int result = stat(path.c_str(), &hostBuf);
303 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
309 /// Target lstat() handler.
312 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
317 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
321 int result = lstat(path.c_str(), &hostBuf);
326 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
331 /// Target fstat() handler.
334 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
337 int fd = process->sim_fd(xc->getSyscallArg(0));
339 // DPRINTFR(SyscallVerbose, "fstat(%d, ...)\n", fd);
345 int result = fstat(fd, &hostBuf);
350 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
356 /// Target mmap() handler.
358 /// We don't really handle mmap(). If the target is mmaping an
359 /// anonymous region or /dev/zero, we can get away with doing basically
360 /// nothing (since memory is initialized to zero and the simulator
361 /// doesn't really check addresses anyway). Always print a warning,
362 /// since this could be seriously broken if we're not mapping
365 /// Someday we should explicitly check for /dev/zero in open, flag the
366 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
370 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
372 Addr start = xc->getSyscallArg(0);
373 uint64_t length = xc->getSyscallArg(1);
374 // int prot = xc->getSyscallArg(2);
375 int flags = xc->getSyscallArg(3);
376 // int fd = p->sim_fd(xc->getSyscallArg(4));
377 // int offset = xc->getSyscallArg(5);
380 // user didn't give an address... pick one from our "mmap region"
381 start = p->mmap_base;
382 p->mmap_base += RoundUp<Addr>(length, VMPageSize);
385 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
386 DPRINTF(SyscallWarnings, "Warning: allowing mmap of file @ fd %d. "
387 "This will break if not /dev/zero.", xc->getSyscallArg(4));
393 /// Target getrlimit() handler.
396 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
399 unsigned resource = xc->getSyscallArg(0);
400 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
403 case OS::RLIMIT_STACK:
404 // max stack size in bytes: make up a number (2MB for now)
405 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
409 cerr << "getrlimitFunc: unimplemented resource " << resource << endl;
414 rlp.copyOut(xc->mem);
418 /// A readable name for 1,000,000, for converting microseconds to seconds.
419 const int one_million = 1000000;
421 /// Approximate seconds since the epoch (1/1/1970). About a billion,
422 /// by my reckoning. We want to keep this a constant (not use the
423 /// real-world time) to keep simulations repeatable.
424 const unsigned seconds_since_epoch = 1000000000;
426 /// Helper function to convert current elapsed time to seconds and
428 template <class T1, class T2>
430 getElapsedTime(T1 &sec, T2 &usec)
432 int cycles_per_usec = ticksPerSecond / one_million;
434 int elapsed_usecs = curTick / cycles_per_usec;
435 sec = elapsed_usecs / one_million;
436 usec = elapsed_usecs % one_million;
440 /// Target gettimeofday() handler.
443 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
446 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
448 getElapsedTime(tp->tv_sec, tp->tv_usec);
449 tp->tv_sec += seconds_since_epoch;
457 /// Target getrusage() function.
460 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
463 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
464 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
466 if (who != OS::RUSAGE_SELF) {
467 // don't really handle THREAD or CHILDREN, but just warn and
469 DCOUT(SyscallWarnings)
470 << "Warning: getrusage() only supports RUSAGE_SELF."
471 << " Parameter " << who << " ignored." << endl;
474 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
475 rup->ru_stime.tv_sec = 0;
476 rup->ru_stime.tv_usec = 0;
488 rup->ru_nsignals = 0;
492 rup.copyOut(xc->mem);
499 #endif // __SYSCALL_EMUL_HH__