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 /// Target unlink() handler.
195 int unlinkFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
197 /// Target rename() handler.
198 int renameFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc);
200 //////////////////////////////////////////////////////////////////////
202 // The following emulation functions are generic, but need to be
203 // templated to account for differences in types, constants, etc.
205 //////////////////////////////////////////////////////////////////////
207 /// Target ioctl() handler. For the most part, programs call ioctl()
208 /// only to find out if their stdout is a tty, to determine whether to
209 /// do line or block buffering.
212 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
215 int fd = xc->getSyscallArg(0);
216 unsigned req = xc->getSyscallArg(1);
218 // DPRINTFR(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
220 if (fd < 0 || process->sim_fd(fd) < 0) {
221 // doesn't map to any simulator fd: not a valid target fd
237 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", fd, req, xc->readPC());
241 /// This struct is used to build an target-OS-dependent table that
242 /// maps the target's open() flags to the host open() flags.
243 struct OpenFlagTransTable {
244 int tgtFlag; //!< Target system flag value.
245 int hostFlag; //!< Corresponding host system flag value.
249 /// Target open() handler.
252 openFunc(SyscallDesc *desc, int callnum, Process *process,
257 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
260 if (path == "/dev/sysdev0") {
261 // This is a memory-mapped high-resolution timer device on Alpha.
262 // We don't support it, so just punt.
263 DCOUT(SyscallWarnings) << "Ignoring open(" << path << ", ...)" << endl;
267 int tgtFlags = xc->getSyscallArg(1);
268 int mode = xc->getSyscallArg(2);
271 // translate open flags
272 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
273 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
274 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
275 hostFlags |= OS::openFlagTable[i].hostFlag;
279 // any target flags left?
281 cerr << "Syscall: open: cannot decode flags: " << tgtFlags << endl;
284 hostFlags |= O_BINARY;
288 int fd = open(path.c_str(), hostFlags, mode);
290 return (fd == -1) ? -errno : process->open_fd(fd);
294 /// Target stat() handler.
297 statFunc(SyscallDesc *desc, int callnum, Process *process,
302 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
306 int result = stat(path.c_str(), &hostBuf);
311 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
317 /// Target lstat() handler.
320 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
325 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
329 int result = lstat(path.c_str(), &hostBuf);
334 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
339 /// Target fstat() handler.
342 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
345 int fd = process->sim_fd(xc->getSyscallArg(0));
347 // DPRINTFR(SyscallVerbose, "fstat(%d, ...)\n", fd);
353 int result = fstat(fd, &hostBuf);
358 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
364 /// Target mmap() handler.
366 /// We don't really handle mmap(). If the target is mmaping an
367 /// anonymous region or /dev/zero, we can get away with doing basically
368 /// nothing (since memory is initialized to zero and the simulator
369 /// doesn't really check addresses anyway). Always print a warning,
370 /// since this could be seriously broken if we're not mapping
373 /// Someday we should explicitly check for /dev/zero in open, flag the
374 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
378 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
380 Addr start = xc->getSyscallArg(0);
381 uint64_t length = xc->getSyscallArg(1);
382 // int prot = xc->getSyscallArg(2);
383 int flags = xc->getSyscallArg(3);
384 // int fd = p->sim_fd(xc->getSyscallArg(4));
385 // int offset = xc->getSyscallArg(5);
388 // user didn't give an address... pick one from our "mmap region"
389 start = p->mmap_base;
390 p->mmap_base += RoundUp<Addr>(length, VMPageSize);
393 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
394 DPRINTF(SyscallWarnings, "Warning: allowing mmap of file @ fd %d. "
395 "This will break if not /dev/zero.", xc->getSyscallArg(4));
401 /// Target getrlimit() handler.
404 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
407 unsigned resource = xc->getSyscallArg(0);
408 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
411 case OS::RLIMIT_STACK:
412 // max stack size in bytes: make up a number (2MB for now)
413 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
417 cerr << "getrlimitFunc: unimplemented resource " << resource << endl;
422 rlp.copyOut(xc->mem);
426 /// A readable name for 1,000,000, for converting microseconds to seconds.
427 const int one_million = 1000000;
429 /// Approximate seconds since the epoch (1/1/1970). About a billion,
430 /// by my reckoning. We want to keep this a constant (not use the
431 /// real-world time) to keep simulations repeatable.
432 const unsigned seconds_since_epoch = 1000000000;
434 /// Helper function to convert current elapsed time to seconds and
436 template <class T1, class T2>
438 getElapsedTime(T1 &sec, T2 &usec)
440 int cycles_per_usec = ticksPerSecond / one_million;
442 int elapsed_usecs = curTick / cycles_per_usec;
443 sec = elapsed_usecs / one_million;
444 usec = elapsed_usecs % one_million;
448 /// Target gettimeofday() handler.
451 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
454 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
456 getElapsedTime(tp->tv_sec, tp->tv_usec);
457 tp->tv_sec += seconds_since_epoch;
465 /// Target getrusage() function.
468 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
471 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
472 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
474 if (who != OS::RUSAGE_SELF) {
475 // don't really handle THREAD or CHILDREN, but just warn and
477 DCOUT(SyscallWarnings)
478 << "Warning: getrusage() only supports RUSAGE_SELF."
479 << " Parameter " << who << " ignored." << endl;
482 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
483 rup->ru_stime.tv_sec = 0;
484 rup->ru_stime.tv_usec = 0;
496 rup->ru_nsignals = 0;
500 rup.copyOut(xc->mem);
507 #endif // __SYSCALL_EMUL_HH__