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.
28 * Authors: Steve Reinhardt
32 #ifndef __SIM_SYSCALL_EMUL_HH__
33 #define __SIM_SYSCALL_EMUL_HH__
35 #define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
36 defined(__FreeBSD__) || defined(__CYGWIN__))
39 /// @file syscall_emul.hh
41 /// This file defines objects used to emulate syscalls from the target
42 /// application on the host machine.
47 #include <sys/fcntl.h> // for O_BINARY
51 #include "arch/isa_traits.hh" // for Addr
52 #include "base/chunk_generator.hh"
53 #include "base/intmath.hh" // for RoundUp
54 #include "base/misc.hh"
55 #include "base/trace.hh"
56 #include "cpu/base.hh"
57 #include "cpu/thread_context.hh"
58 #include "mem/translating_port.hh"
59 #include "mem/page_table.hh"
60 #include "sim/process.hh"
63 /// System call descriptor.
69 /// Typedef for target syscall handler functions.
70 typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
71 Process *, ThreadContext *);
73 const char *name; //!< Syscall name (e.g., "open").
74 FuncPtr funcPtr; //!< Pointer to emulation function.
75 int flags; //!< Flags (see Flags enum).
77 /// Flag values for controlling syscall behavior.
79 /// Don't set return regs according to funcPtr return value.
80 /// Used for syscalls with non-standard return conventions
81 /// that explicitly set the ThreadContext regs (e.g.,
83 SuppressReturnValue = 1
87 SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
88 : name(_name), funcPtr(_funcPtr), flags(_flags)
92 /// Emulate the syscall. Public interface for calling through funcPtr.
93 void doSyscall(int callnum, Process *proc, ThreadContext *tc);
101 BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
103 bufPtr = new uint8_t[size];
104 // clear out buffer: in case we only partially populate this,
105 // and then do a copyOut(), we want to make sure we don't
106 // introduce any random junk into the simulated address space
107 memset(bufPtr, 0, size);
110 virtual ~BaseBufferArg() { delete [] bufPtr; }
113 // copy data into simulator space (read from target memory)
115 virtual bool copyIn(TranslatingPort *memport)
117 memport->readBlob(addr, bufPtr, size);
118 return true; // no EFAULT detection for now
122 // copy data out of simulator space (write to target memory)
124 virtual bool copyOut(TranslatingPort *memport)
126 memport->writeBlob(addr, bufPtr, size);
127 return true; // no EFAULT detection for now
137 class BufferArg : public BaseBufferArg
140 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
141 void *bufferPtr() { return bufPtr; }
145 class TypedBufferArg : public BaseBufferArg
148 // user can optionally specify a specific number of bytes to
149 // allocate to deal with those structs that have variable-size
151 TypedBufferArg(Addr _addr, int _size = sizeof(T))
152 : BaseBufferArg(_addr, _size)
156 operator T*() { return (T *)bufPtr; }
158 // dereference operators
159 T &operator*() { return *((T *)bufPtr); }
160 T* operator->() { return (T *)bufPtr; }
161 T &operator[](int i) { return ((T *)bufPtr)[i]; }
164 //////////////////////////////////////////////////////////////////////
166 // The following emulation functions are generic enough that they
167 // don't need to be recompiled for different emulated OS's. They are
168 // defined in sim/syscall_emul.cc.
170 //////////////////////////////////////////////////////////////////////
173 /// Handler for unimplemented syscalls that we haven't thought about.
174 SyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
175 Process *p, ThreadContext *tc);
177 /// Handler for unimplemented syscalls that we never intend to
178 /// implement (signal handling, etc.) and should not affect the correct
179 /// behavior of the program. Print a warning only if the appropriate
180 /// trace flag is enabled. Return success to the target program.
181 SyscallReturn ignoreFunc(SyscallDesc *desc, int num,
182 Process *p, ThreadContext *tc);
184 /// Target exit() handler: terminate simulation.
185 SyscallReturn exitFunc(SyscallDesc *desc, int num,
186 Process *p, ThreadContext *tc);
188 /// Target getpagesize() handler.
189 SyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
190 Process *p, ThreadContext *tc);
192 /// Target obreak() handler: set brk address.
193 SyscallReturn obreakFunc(SyscallDesc *desc, int num,
194 Process *p, ThreadContext *tc);
196 /// Target close() handler.
197 SyscallReturn closeFunc(SyscallDesc *desc, int num,
198 Process *p, ThreadContext *tc);
200 /// Target read() handler.
201 SyscallReturn readFunc(SyscallDesc *desc, int num,
202 Process *p, ThreadContext *tc);
204 /// Target write() handler.
205 SyscallReturn writeFunc(SyscallDesc *desc, int num,
206 Process *p, ThreadContext *tc);
208 /// Target lseek() handler.
209 SyscallReturn lseekFunc(SyscallDesc *desc, int num,
210 Process *p, ThreadContext *tc);
212 /// Target munmap() handler.
213 SyscallReturn munmapFunc(SyscallDesc *desc, int num,
214 Process *p, ThreadContext *tc);
216 /// Target gethostname() handler.
217 SyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
218 Process *p, ThreadContext *tc);
220 /// Target unlink() handler.
221 SyscallReturn unlinkFunc(SyscallDesc *desc, int num,
222 Process *p, ThreadContext *tc);
224 /// Target rename() handler.
225 SyscallReturn renameFunc(SyscallDesc *desc, int num,
226 Process *p, ThreadContext *tc);
229 /// Target truncate() handler.
230 SyscallReturn truncateFunc(SyscallDesc *desc, int num,
231 Process *p, ThreadContext *tc);
234 /// Target ftruncate() handler.
235 SyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
236 Process *p, ThreadContext *tc);
239 /// Target chown() handler.
240 SyscallReturn chownFunc(SyscallDesc *desc, int num,
241 Process *p, ThreadContext *tc);
244 /// Target fchown() handler.
245 SyscallReturn fchownFunc(SyscallDesc *desc, int num,
246 Process *p, ThreadContext *tc);
248 /// Target fnctl() handler.
249 SyscallReturn fcntlFunc(SyscallDesc *desc, int num,
250 Process *process, ThreadContext *tc);
252 /// Target fcntl64() handler.
253 SyscallReturn fcntl64Func(SyscallDesc *desc, int num,
254 Process *process, ThreadContext *tc);
256 /// Target setuid() handler.
257 SyscallReturn setuidFunc(SyscallDesc *desc, int num,
258 Process *p, ThreadContext *tc);
260 /// Target getpid() handler.
261 SyscallReturn getpidFunc(SyscallDesc *desc, int num,
262 Process *p, ThreadContext *tc);
264 /// Target getuid() handler.
265 SyscallReturn getuidFunc(SyscallDesc *desc, int num,
266 Process *p, ThreadContext *tc);
268 /// Target getgid() handler.
269 SyscallReturn getgidFunc(SyscallDesc *desc, int num,
270 Process *p, ThreadContext *tc);
272 /// Target getppid() handler.
273 SyscallReturn getppidFunc(SyscallDesc *desc, int num,
274 Process *p, ThreadContext *tc);
276 /// Target geteuid() handler.
277 SyscallReturn geteuidFunc(SyscallDesc *desc, int num,
278 Process *p, ThreadContext *tc);
280 /// Target getegid() handler.
281 SyscallReturn getegidFunc(SyscallDesc *desc, int num,
282 Process *p, ThreadContext *tc);
286 /// Pseudo Funcs - These functions use a different return convension,
287 /// returning a second value in a register other than the normal return register
288 SyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
289 Process *process, ThreadContext *tc);
291 /// Target getpidPseudo() handler.
292 SyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
293 Process *p, ThreadContext *tc);
295 /// Target getuidPseudo() handler.
296 SyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
297 Process *p, ThreadContext *tc);
299 /// Target getgidPseudo() handler.
300 SyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
301 Process *p, ThreadContext *tc);
304 /// This struct is used to build an target-OS-dependent table that
305 /// maps the target's open() flags to the host open() flags.
306 struct OpenFlagTransTable {
307 int tgtFlag; //!< Target system flag value.
308 int hostFlag; //!< Corresponding host system flag value.
313 /// A readable name for 1,000,000, for converting microseconds to seconds.
314 const int one_million = 1000000;
316 /// Approximate seconds since the epoch (1/1/1970). About a billion,
317 /// by my reckoning. We want to keep this a constant (not use the
318 /// real-world time) to keep simulations repeatable.
319 const unsigned seconds_since_epoch = 1000000000;
321 /// Helper function to convert current elapsed time to seconds and
323 template <class T1, class T2>
325 getElapsedTime(T1 &sec, T2 &usec)
327 int elapsed_usecs = curTick / Clock::Int::us;
328 sec = elapsed_usecs / one_million;
329 usec = elapsed_usecs % one_million;
332 //////////////////////////////////////////////////////////////////////
334 // The following emulation functions are generic, but need to be
335 // templated to account for differences in types, constants, etc.
337 //////////////////////////////////////////////////////////////////////
339 /// Target ioctl() handler. For the most part, programs call ioctl()
340 /// only to find out if their stdout is a tty, to determine whether to
341 /// do line or block buffering.
344 ioctlFunc(SyscallDesc *desc, int callnum, Process *process,
347 int fd = tc->getSyscallArg(0);
348 unsigned req = tc->getSyscallArg(1);
350 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
352 if (fd < 0 || process->sim_fd(fd) < 0) {
353 // doesn't map to any simulator fd: not a valid target fd
369 fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
370 fd, req, tc->readPC());
374 /// Target open() handler.
377 openFunc(SyscallDesc *desc, int callnum, Process *process,
382 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
385 if (path == "/dev/sysdev0") {
386 // This is a memory-mapped high-resolution timer device on Alpha.
387 // We don't support it, so just punt.
388 warn("Ignoring open(%s, ...)\n", path);
392 int tgtFlags = tc->getSyscallArg(1);
393 int mode = tc->getSyscallArg(2);
396 // translate open flags
397 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
398 if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
399 tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
400 hostFlags |= OS::openFlagTable[i].hostFlag;
404 // any target flags left?
406 warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
409 hostFlags |= O_BINARY;
412 DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
415 int fd = open(path.c_str(), hostFlags, mode);
417 return (fd == -1) ? -errno : process->alloc_fd(fd);
421 /// Target chmod() handler.
424 chmodFunc(SyscallDesc *desc, int callnum, Process *process,
429 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
432 uint32_t mode = tc->getSyscallArg(1);
435 // XXX translate mode flags via OS::something???
439 int result = chmod(path.c_str(), hostMode);
447 /// Target fchmod() handler.
450 fchmodFunc(SyscallDesc *desc, int callnum, Process *process,
453 int fd = tc->getSyscallArg(0);
454 if (fd < 0 || process->sim_fd(fd) < 0) {
455 // doesn't map to any simulator fd: not a valid target fd
459 uint32_t mode = tc->getSyscallArg(1);
462 // XXX translate mode flags via OS::someting???
466 int result = fchmod(process->sim_fd(fd), hostMode);
474 /// Target stat() handler.
477 statFunc(SyscallDesc *desc, int callnum, Process *process,
482 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
486 int result = stat(path.c_str(), &hostBuf);
491 OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
497 /// Target fstat64() handler.
500 fstat64Func(SyscallDesc *desc, int callnum, Process *process,
503 int fd = tc->getSyscallArg(0);
504 if (fd < 0 || process->sim_fd(fd) < 0) {
505 // doesn't map to any simulator fd: not a valid target fd
511 int result = fstat(process->sim_fd(fd), &hostBuf);
513 struct stat64 hostBuf;
514 int result = fstat64(process->sim_fd(fd), &hostBuf);
520 OS::copyOutStat64Buf(tc->getMemPort(), fd, tc->getSyscallArg(1), &hostBuf);
526 /// Target lstat() handler.
529 lstatFunc(SyscallDesc *desc, int callnum, Process *process,
534 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
538 int result = lstat(path.c_str(), &hostBuf);
543 OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
548 /// Target lstat64() handler.
551 lstat64Func(SyscallDesc *desc, int callnum, Process *process,
556 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
561 int result = lstat(path.c_str(), &hostBuf);
563 struct stat64 hostBuf;
564 int result = lstat64(path.c_str(), &hostBuf);
570 OS::copyOutStat64Buf(tc->getMemPort(), -1, tc->getSyscallArg(1), &hostBuf);
575 /// Target fstat() handler.
578 fstatFunc(SyscallDesc *desc, int callnum, Process *process,
581 int fd = process->sim_fd(tc->getSyscallArg(0));
583 DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
589 int result = fstat(fd, &hostBuf);
594 OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
600 /// Target statfs() handler.
603 statfsFunc(SyscallDesc *desc, int callnum, Process *process,
608 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
611 struct statfs hostBuf;
612 int result = statfs(path.c_str(), &hostBuf);
617 OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
623 /// Target fstatfs() handler.
626 fstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
629 int fd = process->sim_fd(tc->getSyscallArg(0));
634 struct statfs hostBuf;
635 int result = fstatfs(fd, &hostBuf);
640 OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
646 /// Target writev() handler.
649 writevFunc(SyscallDesc *desc, int callnum, Process *process,
652 int fd = tc->getSyscallArg(0);
653 if (fd < 0 || process->sim_fd(fd) < 0) {
654 // doesn't map to any simulator fd: not a valid target fd
658 TranslatingPort *p = tc->getMemPort();
659 uint64_t tiov_base = tc->getSyscallArg(1);
660 size_t count = tc->getSyscallArg(2);
661 struct iovec hiov[count];
662 for (int i = 0; i < count; ++i)
664 typename OS::tgt_iovec tiov;
666 p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
667 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
668 hiov[i].iov_len = gtoh(tiov.iov_len);
669 hiov[i].iov_base = new char [hiov[i].iov_len];
670 p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
674 int result = writev(process->sim_fd(fd), hiov, count);
676 for (int i = 0; i < count; ++i)
678 delete [] (char *)hiov[i].iov_base;
688 /// Target mmap() handler.
690 /// We don't really handle mmap(). If the target is mmaping an
691 /// anonymous region or /dev/zero, we can get away with doing basically
692 /// nothing (since memory is initialized to zero and the simulator
693 /// doesn't really check addresses anyway). Always print a warning,
694 /// since this could be seriously broken if we're not mapping
697 /// Someday we should explicitly check for /dev/zero in open, flag the
698 /// file descriptor, and fail (or implement!) a non-anonymous mmap to
702 mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
704 Addr start = tc->getSyscallArg(0);
705 uint64_t length = tc->getSyscallArg(1);
706 // int prot = tc->getSyscallArg(2);
707 int flags = tc->getSyscallArg(3);
708 // int fd = p->sim_fd(tc->getSyscallArg(4));
709 // int offset = tc->getSyscallArg(5);
711 if ((start % TheISA::VMPageSize) != 0 ||
712 (length % TheISA::VMPageSize) != 0) {
713 warn("mmap failing: arguments not page-aligned: "
714 "start 0x%x length 0x%x",
720 warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
724 // pick next address from our "mmap region"
726 p->pTable->allocate(start, length);
727 p->mmap_end += length;
729 if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
730 warn("allowing mmap of file @ fd %d. "
731 "This will break if not /dev/zero.", tc->getSyscallArg(4));
737 /// Target getrlimit() handler.
740 getrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
743 unsigned resource = tc->getSyscallArg(0);
744 TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
747 case OS::TGT_RLIMIT_STACK:
748 // max stack size in bytes: make up a number (2MB for now)
749 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
750 rlp->rlim_cur = htog(rlp->rlim_cur);
751 rlp->rlim_max = htog(rlp->rlim_max);
755 std::cerr << "getrlimitFunc: unimplemented resource " << resource
761 rlp.copyOut(tc->getMemPort());
765 /// Target gettimeofday() handler.
768 gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
771 TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
773 getElapsedTime(tp->tv_sec, tp->tv_usec);
774 tp->tv_sec += seconds_since_epoch;
775 tp->tv_sec = htog(tp->tv_sec);
776 tp->tv_usec = htog(tp->tv_usec);
778 tp.copyOut(tc->getMemPort());
784 /// Target utimes() handler.
787 utimesFunc(SyscallDesc *desc, int callnum, Process *process,
792 if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
795 TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
796 tp.copyIn(tc->getMemPort());
798 struct timeval hostTimeval[2];
799 for (int i = 0; i < 2; ++i)
801 hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
802 hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
804 int result = utimes(path.c_str(), hostTimeval);
811 /// Target getrusage() function.
814 getrusageFunc(SyscallDesc *desc, int callnum, Process *process,
817 int who = tc->getSyscallArg(0); // THREAD, SELF, or CHILDREN
818 TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
820 if (who != OS::TGT_RUSAGE_SELF) {
821 // don't really handle THREAD or CHILDREN, but just warn and
823 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.",
827 getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
828 rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
829 rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
831 rup->ru_stime.tv_sec = 0;
832 rup->ru_stime.tv_usec = 0;
844 rup->ru_nsignals = 0;
848 rup.copyOut(tc->getMemPort());
856 #endif // __SIM_SYSCALL_EMUL_HH__