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
45 #include "base/intmath.hh" // for RoundUp
46 #include "mem/functional/functional.hh"
47 #include "targetarch/isa_traits.hh" // for Addr
49 #include "base/trace.hh"
50 #include "cpu/exec_context.hh"
51 #include "sim/process.hh"
54 /// System call descriptor.
60 /// Typedef for target syscall handler functions.
61 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
62 Process *, ExecContext *);
64 const char *name; //!< Syscall name (e.g., "open").
65 FuncPtr funcPtr; //!< Pointer to emulation function.
66 int flags; //!< Flags (see Flags enum).
68 /// Flag values for controlling syscall behavior.
70 /// Don't set return regs according to funcPtr return value.
71 /// Used for syscalls with non-standard return conventions
72 /// that explicitly set the ExecContext regs (e.g.,
74 SuppressReturnValue = 1
78 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
79 : name(_name), funcPtr(_funcPtr), flags(_flags)
83 /// Emulate the syscall. Public interface for calling through funcPtr.
84 void doSyscall(int callnum, Process *proc, ExecContext *xc);
92 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
94 bufPtr = new uint8_t[size];
95 // clear out buffer: in case we only partially populate this,
96 // and then do a copyOut(), we want to make sure we don't
97 // introduce any random junk into the simulated address space
98 memset(bufPtr, 0, size);
101 virtual ~BaseBufferArg() { delete [] bufPtr; }
104 // copy data into simulator space (read from target memory)
106 virtual bool copyIn(FunctionalMemory *mem)
108 mem->access(Read, addr, bufPtr, size);
109 return true; // no EFAULT detection for now
113 // copy data out of simulator space (write to target memory)
115 virtual bool copyOut(FunctionalMemory *mem)
117 mem->access(Write, addr, bufPtr, size);
118 return true; // no EFAULT detection for now
128 class BufferArg : public BaseBufferArg
131 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
132 void *bufferPtr() { return bufPtr; }
136 class TypedBufferArg : public BaseBufferArg
139 // user can optionally specify a specific number of bytes to
140 // allocate to deal with those structs that have variable-size
142 TypedBufferArg(Addr _addr, int _size = sizeof(T))
143 : BaseBufferArg(_addr, _size)
147 operator T*() { return (T *)bufPtr; }
149 // dereference operators
150 T &operator*() { return *((T *)bufPtr); }
151 T* operator->() { return (T *)bufPtr; }
152 T &operator[](int i) { return ((T *)bufPtr)[i]; }
155 //////////////////////////////////////////////////////////////////////
157 // The following emulation functions are generic enough that they
158 // don't need to be recompiled for different emulated OS's. They are
159 // defined in sim/syscall_emul.cc.
161 //////////////////////////////////////////////////////////////////////
164 /// Handler for unimplemented syscalls that we haven't thought about.
165 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
166 Process *p, ExecContext *xc);
168 /// Handler for unimplemented syscalls that we never intend to
169 /// implement (signal handling, etc.) and should not affect the correct
170 /// behavior of the program. Print a warning only if the appropriate
171 /// trace flag is enabled. Return success to the target program.
172 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
173 Process *p, ExecContext *xc);
175 /// Target exit() handler: terminate simulation.
176 SyscallReturn exitFunc(SyscallDesc *desc, int num,
177 Process *p, ExecContext *xc);
179 /// Target getpagesize() handler.
180 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
181 Process *p, ExecContext *xc);
183 /// Target obreak() handler: set brk address.
184 SyscallReturn obreakFunc(SyscallDesc *desc, int num,
185 Process *p, ExecContext *xc);
187 /// Target close() handler.
188 SyscallReturn closeFunc(SyscallDesc *desc, int num,
189 Process *p, ExecContext *xc);
191 /// Target read() handler.
192 SyscallReturn readFunc(SyscallDesc *desc, int num,
193 Process *p, ExecContext *xc);
195 /// Target write() handler.
196 SyscallReturn writeFunc(SyscallDesc *desc, int num,
197 Process *p, ExecContext *xc);
199 /// Target lseek() handler.
200 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
201 Process *p, ExecContext *xc);
203 /// Target munmap() handler.
204 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
205 Process *p, ExecContext *xc);
207 /// Target gethostname() handler.
208 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
209 Process *p, ExecContext *xc);
211 /// Target unlink() handler.
212 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
213 Process *p, ExecContext *xc);
215 /// Target rename() handler.
216 SyscallReturn renameFunc(SyscallDesc *desc, int num,
217 Process *p, ExecContext *xc);
220 /// Target truncate() handler.
221 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
222 Process *p, ExecContext *xc);
225 /// Target ftruncate() handler.
226 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
227 Process *p, ExecContext *xc);
230 /// Target chown() handler.
231 SyscallReturn chownFunc(SyscallDesc *desc, int num,
232 Process *p, ExecContext *xc);
235 /// Target fchown() handler.
236 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
237 Process *p, ExecContext *xc);
239 /// This struct is used to build an target-OS-dependent table that
240 /// maps the target's open() flags to the host open() flags.
241 struct OpenFlagTransTable {
242 int tgtFlag; //!< Target system flag value.
243 int hostFlag; //!< Corresponding host system flag value.
248 /// A readable name for 1,000,000, for converting microseconds to seconds.
249 const int one_million = 1000000;
251 /// Approximate seconds since the epoch (1/1/1970). About a billion,
252 /// by my reckoning. We want to keep this a constant (not use the
253 /// real-world time) to keep simulations repeatable.
254 const unsigned seconds_since_epoch = 1000000000;
256 /// Helper function to convert current elapsed time to seconds and
258 template <class T1, class T2>
260 getElapsedTime(T1 &sec, T2 &usec)
262 int elapsed_usecs = curTick / Clock::Int::us;
263 sec = elapsed_usecs / one_million;
264 usec = elapsed_usecs % one_million;
267 //////////////////////////////////////////////////////////////////////
269 // The following emulation functions are generic, but need to be
270 // templated to account for differences in types, constants, etc.
272 //////////////////////////////////////////////////////////////////////
274 /// Target ioctl() handler. For the most part, programs call ioctl()
275 /// only to find out if their stdout is a tty, to determine whether to
276 /// do line or block buffering.
279 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
282 int fd = xc->getSyscallArg(0);
283 unsigned req = xc->getSyscallArg(1);
285 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
287 if (fd < 0 || process->sim_fd(fd) < 0) {
288 // doesn't map to any simulator fd: not a valid target fd
304 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
305 fd, req, xc->readPC());
309 /// Target open() handler.
312 openFunc(SyscallDesc *desc, int callnum, Process *process,
317 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
320 if (path == "/dev/sysdev0") {
321 // This is a memory-mapped high-resolution timer device on Alpha.
322 // We don't support it, so just punt.
323 warn("Ignoring open(%s, ...)\n", path);
327 int tgtFlags = xc->getSyscallArg(1);
328 int mode = xc->getSyscallArg(2);
331 // translate open flags
332 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
333 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
334 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
335 hostFlags |= OS::openFlagTable[i].hostFlag;
339 // any target flags left?
341 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
344 hostFlags |= O_BINARY;
347 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
350 int fd = open(path.c_str(), hostFlags, mode);
352 return (fd == -1) ? -errno : process->alloc_fd(fd);
356 /// Target chmod() handler.
359 chmodFunc(SyscallDesc *desc, int callnum, Process *process,
364 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
367 uint32_t mode = xc->getSyscallArg(1);
370 // XXX translate mode flags via OS::something???
374 int result = chmod(path.c_str(), hostMode);
382 /// Target fchmod() handler.
385 fchmodFunc(SyscallDesc *desc, int callnum, Process *process,
388 int fd = xc->getSyscallArg(0);
389 if (fd < 0 || process->sim_fd(fd) < 0) {
390 // doesn't map to any simulator fd: not a valid target fd
394 uint32_t mode = xc->getSyscallArg(1);
397 // XXX translate mode flags via OS::someting???
401 int result = fchmod(process->sim_fd(fd), hostMode);
409 /// Target stat() handler.
412 statFunc(SyscallDesc *desc, int callnum, Process *process,
417 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
421 int result = stat(path.c_str(), &hostBuf);
426 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
432 /// Target fstat64() handler.
435 fstat64Func(SyscallDesc *desc, int callnum, Process *process,
438 int fd = xc->getSyscallArg(0);
439 if (fd < 0 || process->sim_fd(fd) < 0) {
440 // doesn't map to any simulator fd: not a valid target fd
444 struct stat64 hostBuf;
445 int result = fstat64(process->sim_fd(fd), &hostBuf);
450 OS::copyOutStat64Buf(xc->mem, xc->getSyscallArg(1), &hostBuf);
456 /// Target lstat() handler.
459 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
464 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
468 int result = lstat(path.c_str(), &hostBuf);
473 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
478 /// Target lstat64() handler.
481 lstat64Func(SyscallDesc *desc, int callnum, Process *process,
486 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
489 struct stat64 hostBuf;
490 int result = lstat64(path.c_str(), &hostBuf);
495 OS::copyOutStat64Buf(xc->mem, xc->getSyscallArg(1), &hostBuf);
500 /// Target fstat() handler.
503 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
506 int fd = process->sim_fd(xc->getSyscallArg(0));
508 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
514 int result = fstat(fd, &hostBuf);
519 OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
525 /// Target statfs() handler.
528 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
533 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
536 struct statfs hostBuf;
537 int result = statfs(path.c_str(), &hostBuf);
542 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
548 /// Target fstatfs() handler.
551 fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
554 int fd = process->sim_fd(xc->getSyscallArg(0));
559 struct statfs hostBuf;
560 int result = fstatfs(fd, &hostBuf);
565 OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
571 /// Target writev() handler.
574 writevFunc(SyscallDesc *desc, int callnum, Process *process,
577 int fd = xc->getSyscallArg(0);
578 if (fd < 0 || process->sim_fd(fd) < 0) {
579 // doesn't map to any simulator fd: not a valid target fd
583 uint64_t tiov_base = xc->getSyscallArg(1);
584 size_t count = xc->getSyscallArg(2);
585 struct iovec hiov[count];
586 for (int i = 0; i < count; ++i)
588 typename OS::tgt_iovec tiov;
589 xc->mem->access(Read, tiov_base + i*sizeof(typename OS::tgt_iovec),
590 &tiov, sizeof(typename OS::tgt_iovec));
591 hiov[i].iov_len = tiov.iov_len;
592 hiov[i].iov_base = new char [hiov[i].iov_len];
593 xc->mem->access(Read, tiov.iov_base,
594 hiov[i].iov_base, hiov[i].iov_len);
597 int result = writev(process->sim_fd(fd), hiov, count);
599 for (int i = 0; i < count; ++i)
601 delete [] (char *)hiov[i].iov_base;
611 /// Target mmap() handler.
613 /// We don't really handle mmap(). If the target is mmaping an
614 /// anonymous region or /dev/zero, we can get away with doing basically
615 /// nothing (since memory is initialized to zero and the simulator
616 /// doesn't really check addresses anyway). Always print a warning,
617 /// since this could be seriously broken if we're not mapping
620 /// Someday we should explicitly check for /dev/zero in open, flag the
621 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
625 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
627 Addr start = xc->getSyscallArg(0);
628 uint64_t length = xc->getSyscallArg(1);
629 // int prot = xc->getSyscallArg(2);
630 int flags = xc->getSyscallArg(3);
631 // int fd = p->sim_fd(xc->getSyscallArg(4));
632 // int offset = xc->getSyscallArg(5);
635 // user didn't give an address... pick one from our "mmap region"
637 p->mmap_end += roundUp(length, VMPageSize);
638 if (p->nxm_start != 0) {
639 //If we have an nxm space, make sure we haven't colided
640 assert(p->mmap_end < p->nxm_start);
644 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
645 warn("allowing mmap of file @ fd %d. "
646 "This will break if not /dev/zero.", xc->getSyscallArg(4));
652 /// Target getrlimit() handler.
655 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
658 unsigned resource = xc->getSyscallArg(0);
659 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
662 case OS::RLIMIT_STACK:
663 // max stack size in bytes: make up a number (2MB for now)
664 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
668 std::cerr << "getrlimitFunc: unimplemented resource " << resource
674 rlp.copyOut(xc->mem);
678 /// Target gettimeofday() handler.
681 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
684 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
686 getElapsedTime(tp->tv_sec, tp->tv_usec);
687 tp->tv_sec += seconds_since_epoch;
695 /// Target utimes() handler.
698 utimesFunc(SyscallDesc *desc, int callnum, Process *process,
703 if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
706 TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1));
709 struct timeval hostTimeval[2];
710 for (int i = 0; i < 2; ++i)
712 hostTimeval[i].tv_sec = (*tp)[i].tv_sec;
713 hostTimeval[i].tv_usec = (*tp)[i].tv_usec;
715 int result = utimes(path.c_str(), hostTimeval);
723 /// Target getrusage() function.
726 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
729 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
730 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
732 if (who != OS::RUSAGE_SELF) {
733 // don't really handle THREAD or CHILDREN, but just warn and
735 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
739 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
740 rup->ru_stime.tv_sec = 0;
741 rup->ru_stime.tv_usec = 0;
753 rup->ru_nsignals = 0;
757 rup.copyOut(xc->mem);
762 #endif // __SIM_SYSCALL_EMUL_HH__