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__
32 #define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \
36 /// @file syscall_emul.hh
38 /// This file defines objects used to emulate syscalls from the target
39 /// application on the host machine.
44 #include <sys/fcntl.h> // for O_BINARY
48 #include "arch/isa_traits.hh" // for Addr
49 #include "base/chunk_generator.hh"
50 #include "base/intmath.hh" // for RoundUp
51 #include "base/misc.hh"
52 #include "base/trace.hh"
53 #include "cpu/base.hh"
54 #include "cpu/exec_context.hh"
55 #include "mem/translating_port.hh"
56 #include "mem/page_table.hh"
57 #include "sim/process.hh"
60 /// System call descriptor.
66 /// Typedef for target syscall handler functions.
67 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
68 Process *, ExecContext *);
70 const char *name; //!< Syscall name (e.g., "open").
71 FuncPtr funcPtr; //!< Pointer to emulation function.
72 int flags; //!< Flags (see Flags enum).
74 /// Flag values for controlling syscall behavior.
76 /// Don't set return regs according to funcPtr return value.
77 /// Used for syscalls with non-standard return conventions
78 /// that explicitly set the ExecContext regs (e.g.,
80 SuppressReturnValue = 1
84 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
85 : name(_name), funcPtr(_funcPtr), flags(_flags)
89 /// Emulate the syscall. Public interface for calling through funcPtr.
90 void doSyscall(int callnum, Process *proc, ExecContext *xc);
98 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
100 bufPtr = new uint8_t[size];
101 // clear out buffer: in case we only partially populate this,
102 // and then do a copyOut(), we want to make sure we don't
103 // introduce any random junk into the simulated address space
104 memset(bufPtr, 0, size);
107 virtual ~BaseBufferArg() { delete [] bufPtr; }
110 // copy data into simulator space (read from target memory)
112 virtual bool copyIn(TranslatingPort *memport)
114 memport->readBlob(addr, bufPtr, size);
115 return true; // no EFAULT detection for now
119 // copy data out of simulator space (write to target memory)
121 virtual bool copyOut(TranslatingPort *memport)
123 memport->writeBlob(addr, bufPtr, size);
124 return true; // no EFAULT detection for now
134 class BufferArg : public BaseBufferArg
137 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
138 void *bufferPtr() { return bufPtr; }
142 class TypedBufferArg : public BaseBufferArg
145 // user can optionally specify a specific number of bytes to
146 // allocate to deal with those structs that have variable-size
148 TypedBufferArg(Addr _addr, int _size = sizeof(T))
149 : BaseBufferArg(_addr, _size)
153 operator T*() { return (T *)bufPtr; }
155 // dereference operators
156 T &operator*() { return *((T *)bufPtr); }
157 T* operator->() { return (T *)bufPtr; }
158 T &operator[](int i) { return ((T *)bufPtr)[i]; }
161 //////////////////////////////////////////////////////////////////////
163 // The following emulation functions are generic enough that they
164 // don't need to be recompiled for different emulated OS's. They are
165 // defined in sim/syscall_emul.cc.
167 //////////////////////////////////////////////////////////////////////
170 /// Handler for unimplemented syscalls that we haven't thought about.
171 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
172 Process *p, ExecContext *xc);
174 /// Handler for unimplemented syscalls that we never intend to
175 /// implement (signal handling, etc.) and should not affect the correct
176 /// behavior of the program. Print a warning only if the appropriate
177 /// trace flag is enabled. Return success to the target program.
178 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
179 Process *p, ExecContext *xc);
181 /// Target exit() handler: terminate simulation.
182 SyscallReturn exitFunc(SyscallDesc *desc, int num,
183 Process *p, ExecContext *xc);
185 /// Target getpagesize() handler.
186 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
187 Process *p, ExecContext *xc);
189 /// Target obreak() handler: set brk address.
190 SyscallReturn obreakFunc(SyscallDesc *desc, int num,
191 Process *p, ExecContext *xc);
193 /// Target close() handler.
194 SyscallReturn closeFunc(SyscallDesc *desc, int num,
195 Process *p, ExecContext *xc);
197 /// Target read() handler.
198 SyscallReturn readFunc(SyscallDesc *desc, int num,
199 Process *p, ExecContext *xc);
201 /// Target write() handler.
202 SyscallReturn writeFunc(SyscallDesc *desc, int num,
203 Process *p, ExecContext *xc);
205 /// Target lseek() handler.
206 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
207 Process *p, ExecContext *xc);
209 /// Target munmap() handler.
210 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
211 Process *p, ExecContext *xc);
213 /// Target gethostname() handler.
214 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
215 Process *p, ExecContext *xc);
217 /// Target unlink() handler.
218 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
219 Process *p, ExecContext *xc);
221 /// Target rename() handler.
222 SyscallReturn renameFunc(SyscallDesc *desc, int num,
223 Process *p, ExecContext *xc);
226 /// Target truncate() handler.
227 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
228 Process *p, ExecContext *xc);
231 /// Target ftruncate() handler.
232 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
233 Process *p, ExecContext *xc);
236 /// Target chown() handler.
237 SyscallReturn chownFunc(SyscallDesc *desc, int num,
238 Process *p, ExecContext *xc);
241 /// Target fchown() handler.
242 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
243 Process *p, ExecContext *xc);
245 /// Target fnctl() handler.
246 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
247 Process *process, ExecContext *xc);
249 /// Target setuid() handler.
250 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
251 Process *p, ExecContext *xc);
253 /// Target getpid() handler.
254 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
255 Process *p, ExecContext *xc);
257 /// Target getuid() handler.
258 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
259 Process *p, ExecContext *xc);
261 /// Target getgid() handler.
262 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
263 Process *p, ExecContext *xc);
265 /// Target getppid() handler.
266 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
267 Process *p, ExecContext *xc);
269 /// Target geteuid() handler.
270 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
271 Process *p, ExecContext *xc);
273 /// Target getegid() handler.
274 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
275 Process *p, ExecContext *xc);
279 /// Pseudo Funcs - These functions use a different return convension,
280 /// returning a second value in a register other than the normal return register
281 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
282 Process *process, ExecContext *xc);
284 /// Target getpidPseudo() handler.
285 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
286 Process *p, ExecContext *xc);
288 /// Target getuidPseudo() handler.
289 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
290 Process *p, ExecContext *xc);
292 /// Target getgidPseudo() handler.
293 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
294 Process *p, ExecContext *xc);
297 /// This struct is used to build an target-OS-dependent table that
298 /// maps the target's open() flags to the host open() flags.
299 struct OpenFlagTransTable {
300 int tgtFlag; //!< Target system flag value.
301 int hostFlag; //!< Corresponding host system flag value.
306 /// A readable name for 1,000,000, for converting microseconds to seconds.
307 const int one_million = 1000000;
309 /// Approximate seconds since the epoch (1/1/1970). About a billion,
310 /// by my reckoning. We want to keep this a constant (not use the
311 /// real-world time) to keep simulations repeatable.
312 const unsigned seconds_since_epoch = 1000000000;
314 /// Helper function to convert current elapsed time to seconds and
316 template <class T1, class T2>
318 getElapsedTime(T1 &sec, T2 &usec)
320 int elapsed_usecs = curTick / Clock::Int::us;
321 sec = elapsed_usecs / one_million;
322 usec = elapsed_usecs % one_million;
325 //////////////////////////////////////////////////////////////////////
327 // The following emulation functions are generic, but need to be
328 // templated to account for differences in types, constants, etc.
330 //////////////////////////////////////////////////////////////////////
332 /// Target ioctl() handler. For the most part, programs call ioctl()
333 /// only to find out if their stdout is a tty, to determine whether to
334 /// do line or block buffering.
337 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
340 int fd = xc->getSyscallArg(0);
341 unsigned req = xc->getSyscallArg(1);
343 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
345 if (fd < 0 || process->sim_fd(fd) < 0) {
346 // doesn't map to any simulator fd: not a valid target fd
362 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
363 fd, req, xc->readPC());
367 /// Target open() handler.
370 openFunc(SyscallDesc *desc, int callnum, Process *process,
375 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
378 if (path == "/dev/sysdev0") {
379 // This is a memory-mapped high-resolution timer device on Alpha.
380 // We don't support it, so just punt.
381 warn("Ignoring open(%s, ...)\n", path);
385 int tgtFlags = xc->getSyscallArg(1);
386 int mode = xc->getSyscallArg(2);
389 // translate open flags
390 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
391 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
392 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
393 hostFlags |= OS::openFlagTable[i].hostFlag;
397 // any target flags left?
399 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
402 hostFlags |= O_BINARY;
405 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
408 int fd = open(path.c_str(), hostFlags, mode);
410 return (fd == -1) ? -errno : process->alloc_fd(fd);
414 /// Target chmod() handler.
417 chmodFunc(SyscallDesc *desc, int callnum, Process *process,
422 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
425 uint32_t mode = xc->getSyscallArg(1);
428 // XXX translate mode flags via OS::something???
432 int result = chmod(path.c_str(), hostMode);
440 /// Target fchmod() handler.
443 fchmodFunc(SyscallDesc *desc, int callnum, Process *process,
446 int fd = xc->getSyscallArg(0);
447 if (fd < 0 || process->sim_fd(fd) < 0) {
448 // doesn't map to any simulator fd: not a valid target fd
452 uint32_t mode = xc->getSyscallArg(1);
455 // XXX translate mode flags via OS::someting???
459 int result = fchmod(process->sim_fd(fd), hostMode);
467 /// Target stat() handler.
470 statFunc(SyscallDesc *desc, int callnum, Process *process,
475 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
479 int result = stat(path.c_str(), &hostBuf);
484 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
490 /// Target fstat64() handler.
493 fstat64Func(SyscallDesc *desc, int callnum, Process *process,
496 int fd = xc->getSyscallArg(0);
497 if (fd < 0 || process->sim_fd(fd) < 0) {
498 // doesn't map to any simulator fd: not a valid target fd
504 int result = fstat(process->sim_fd(fd), &hostBuf);
506 struct stat64 hostBuf;
507 int result = fstat64(process->sim_fd(fd), &hostBuf);
513 OS::copyOutStat64Buf(xc->getMemPort(), fd, xc->getSyscallArg(1), &hostBuf);
519 /// Target lstat() handler.
522 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
527 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
531 int result = lstat(path.c_str(), &hostBuf);
536 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
541 /// Target lstat64() handler.
544 lstat64Func(SyscallDesc *desc, int callnum, Process *process,
549 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
554 int result = lstat(path.c_str(), &hostBuf);
556 struct stat64 hostBuf;
557 int result = lstat64(path.c_str(), &hostBuf);
563 OS::copyOutStat64Buf(xc->getMemPort(), -1, xc->getSyscallArg(1), &hostBuf);
568 /// Target fstat() handler.
571 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
574 int fd = process->sim_fd(xc->getSyscallArg(0));
576 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
582 int result = fstat(fd, &hostBuf);
587 OS::copyOutStatBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
593 /// Target statfs() handler.
596 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
601 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
604 struct statfs hostBuf;
605 int result = statfs(path.c_str(), &hostBuf);
610 OS::copyOutStatfsBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
616 /// Target fstatfs() handler.
619 fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
622 int fd = process->sim_fd(xc->getSyscallArg(0));
627 struct statfs hostBuf;
628 int result = fstatfs(fd, &hostBuf);
633 OS::copyOutStatfsBuf(xc->getMemPort(), xc->getSyscallArg(1), &hostBuf);
639 /// Target writev() handler.
642 writevFunc(SyscallDesc *desc, int callnum, Process *process,
645 int fd = xc->getSyscallArg(0);
646 if (fd < 0 || process->sim_fd(fd) < 0) {
647 // doesn't map to any simulator fd: not a valid target fd
651 TranslatingPort *p = xc->getMemPort();
652 uint64_t tiov_base = xc->getSyscallArg(1);
653 size_t count = xc->getSyscallArg(2);
654 struct iovec hiov[count];
655 for (int i = 0; i < count; ++i)
657 typename OS::tgt_iovec tiov;
659 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
660 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
661 hiov[i].iov_len = gtoh(tiov.iov_len);
662 hiov[i].iov_base = new char [hiov[i].iov_len];
663 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
667 int result = writev(process->sim_fd(fd), hiov, count);
669 for (int i = 0; i < count; ++i)
671 delete [] (char *)hiov[i].iov_base;
681 /// Target mmap() handler.
683 /// We don't really handle mmap(). If the target is mmaping an
684 /// anonymous region or /dev/zero, we can get away with doing basically
685 /// nothing (since memory is initialized to zero and the simulator
686 /// doesn't really check addresses anyway). Always print a warning,
687 /// since this could be seriously broken if we're not mapping
690 /// Someday we should explicitly check for /dev/zero in open, flag the
691 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
695 mmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
697 Addr start = xc->getSyscallArg(0);
698 uint64_t length = xc->getSyscallArg(1);
699 // int prot = xc->getSyscallArg(2);
700 int flags = xc->getSyscallArg(3);
701 // int fd = p->sim_fd(xc->getSyscallArg(4));
702 // int offset = xc->getSyscallArg(5);
704 if ((start % TheISA::VMPageSize) != 0 ||
705 (length % TheISA::VMPageSize) != 0) {
706 warn("mmap failing: arguments not page-aligned: "
707 "start 0x%x length 0x%x",
713 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
717 // pick next address from our "mmap region"
719 p->pTable->allocate(start, length);
720 p->mmap_end += length;
722 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
723 warn("allowing mmap of file @ fd %d. "
724 "This will break if not /dev/zero.", xc->getSyscallArg(4));
730 /// Target getrlimit() handler.
733 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
736 unsigned resource = xc->getSyscallArg(0);
737 TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
740 case OS::TGT_RLIMIT_STACK:
741 // max stack size in bytes: make up a number (2MB for now)
742 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
743 rlp->rlim_cur = htog(rlp->rlim_cur);
744 rlp->rlim_max = htog(rlp->rlim_max);
748 std::cerr << "getrlimitFunc: unimplemented resource " << resource
754 rlp.copyOut(xc->getMemPort());
758 /// Target gettimeofday() handler.
761 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
764 TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
766 getElapsedTime(tp->tv_sec, tp->tv_usec);
767 tp->tv_sec += seconds_since_epoch;
768 tp->tv_sec = htog(tp->tv_sec);
769 tp->tv_usec = htog(tp->tv_usec);
771 tp.copyOut(xc->getMemPort());
777 /// Target utimes() handler.
780 utimesFunc(SyscallDesc *desc, int callnum, Process *process,
785 if (!xc->getMemPort()->tryReadString(path, xc->getSyscallArg(0)))
788 TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1));
789 tp.copyIn(xc->getMemPort());
791 struct timeval hostTimeval[2];
792 for (int i = 0; i < 2; ++i)
794 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
795 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
797 int result = utimes(path.c_str(), hostTimeval);
804 /// Target getrusage() function.
807 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
810 int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
811 TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
813 if (who != OS::TGT_RUSAGE_SELF) {
814 // don't really handle THREAD or CHILDREN, but just warn and
816 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
820 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
821 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
822 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
824 rup->ru_stime.tv_sec = 0;
825 rup->ru_stime.tv_usec = 0;
837 rup->ru_nsignals = 0;
841 rup.copyOut(xc->getMemPort());
849 #endif // __SIM_SYSCALL_EMUL_HH__