syscall_emul.hh revision 2680
1360SN/A/* 21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 3360SN/A * All rights reserved. 4360SN/A * 5360SN/A * Redistribution and use in source and binary forms, with or without 6360SN/A * modification, are permitted provided that the following conditions are 7360SN/A * met: redistributions of source code must retain the above copyright 8360SN/A * notice, this list of conditions and the following disclaimer; 9360SN/A * redistributions in binary form must reproduce the above copyright 10360SN/A * notice, this list of conditions and the following disclaimer in the 11360SN/A * documentation and/or other materials provided with the distribution; 12360SN/A * neither the name of the copyright holders nor the names of its 13360SN/A * contributors may be used to endorse or promote products derived from 14360SN/A * this software without specific prior written permission. 15360SN/A * 16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292665Ssaidi@eecs.umich.edu * Kevin Lim 30360SN/A */ 31360SN/A 321354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 331354SN/A#define __SIM_SYSCALL_EMUL_HH__ 34360SN/A 352064SN/A#define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \ 362064SN/A defined(__FreeBSD__)) 372064SN/A 38360SN/A/// 39360SN/A/// @file syscall_emul.hh 40360SN/A/// 41360SN/A/// This file defines objects used to emulate syscalls from the target 42360SN/A/// application on the host machine. 43360SN/A 441354SN/A#include <errno.h> 45360SN/A#include <string> 461809SN/A#ifdef __CYGWIN32__ 471809SN/A#include <sys/fcntl.h> // for O_BINARY 481809SN/A#endif 491999SN/A#include <sys/uio.h> 50360SN/A 512474SN/A#include "arch/isa_traits.hh" // for Addr 522474SN/A#include "base/chunk_generator.hh" 53360SN/A#include "base/intmath.hh" // for RoundUp 542462SN/A#include "base/misc.hh" 551354SN/A#include "base/trace.hh" 562474SN/A#include "cpu/base.hh" 572680Sktlim@umich.edu#include "cpu/thread_context.hh" 582474SN/A#include "mem/translating_port.hh" 592474SN/A#include "mem/page_table.hh" 601354SN/A#include "sim/process.hh" 61360SN/A 62360SN/A/// 63360SN/A/// System call descriptor. 64360SN/A/// 65360SN/Aclass SyscallDesc { 66360SN/A 67360SN/A public: 68360SN/A 69378SN/A /// Typedef for target syscall handler functions. 701450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 712680Sktlim@umich.edu Process *, ThreadContext *); 72360SN/A 73360SN/A const char *name; //!< Syscall name (e.g., "open"). 74360SN/A FuncPtr funcPtr; //!< Pointer to emulation function. 75360SN/A int flags; //!< Flags (see Flags enum). 76360SN/A 77360SN/A /// Flag values for controlling syscall behavior. 78360SN/A enum Flags { 79360SN/A /// Don't set return regs according to funcPtr return value. 80360SN/A /// Used for syscalls with non-standard return conventions 812680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 82360SN/A /// sigreturn). 83360SN/A SuppressReturnValue = 1 84360SN/A }; 85360SN/A 86360SN/A /// Constructor. 87360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 88360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 89360SN/A { 90360SN/A } 91360SN/A 92360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 932680Sktlim@umich.edu void doSyscall(int callnum, Process *proc, ThreadContext *tc); 94360SN/A}; 95360SN/A 96360SN/A 97360SN/Aclass BaseBufferArg { 98360SN/A 99360SN/A public: 100360SN/A 101360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 102360SN/A { 103360SN/A bufPtr = new uint8_t[size]; 104360SN/A // clear out buffer: in case we only partially populate this, 105360SN/A // and then do a copyOut(), we want to make sure we don't 106360SN/A // introduce any random junk into the simulated address space 107360SN/A memset(bufPtr, 0, size); 108360SN/A } 109360SN/A 110360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 111360SN/A 112360SN/A // 113360SN/A // copy data into simulator space (read from target memory) 114360SN/A // 1152400SN/A virtual bool copyIn(TranslatingPort *memport) 116360SN/A { 1172461SN/A memport->readBlob(addr, bufPtr, size); 118360SN/A return true; // no EFAULT detection for now 119360SN/A } 120360SN/A 121360SN/A // 122360SN/A // copy data out of simulator space (write to target memory) 123360SN/A // 1242400SN/A virtual bool copyOut(TranslatingPort *memport) 125360SN/A { 1262461SN/A memport->writeBlob(addr, bufPtr, size); 127360SN/A return true; // no EFAULT detection for now 128360SN/A } 129360SN/A 130360SN/A protected: 131360SN/A Addr addr; 132360SN/A int size; 133360SN/A uint8_t *bufPtr; 134360SN/A}; 135360SN/A 136360SN/A 137360SN/Aclass BufferArg : public BaseBufferArg 138360SN/A{ 139360SN/A public: 140360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 141360SN/A void *bufferPtr() { return bufPtr; } 142360SN/A}; 143360SN/A 144360SN/Atemplate <class T> 145360SN/Aclass TypedBufferArg : public BaseBufferArg 146360SN/A{ 147360SN/A public: 148360SN/A // user can optionally specify a specific number of bytes to 149360SN/A // allocate to deal with those structs that have variable-size 150360SN/A // arrays at the end 151360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 152360SN/A : BaseBufferArg(_addr, _size) 153360SN/A { } 154360SN/A 155360SN/A // type case 156360SN/A operator T*() { return (T *)bufPtr; } 157360SN/A 158360SN/A // dereference operators 159502SN/A T &operator*() { return *((T *)bufPtr); } 160360SN/A T* operator->() { return (T *)bufPtr; } 161502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 162360SN/A}; 163360SN/A 164360SN/A////////////////////////////////////////////////////////////////////// 165360SN/A// 166360SN/A// The following emulation functions are generic enough that they 167360SN/A// don't need to be recompiled for different emulated OS's. They are 168360SN/A// defined in sim/syscall_emul.cc. 169360SN/A// 170360SN/A////////////////////////////////////////////////////////////////////// 171360SN/A 172360SN/A 173378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1741706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1752680Sktlim@umich.edu Process *p, ThreadContext *tc); 176378SN/A 177378SN/A/// Handler for unimplemented syscalls that we never intend to 178378SN/A/// implement (signal handling, etc.) and should not affect the correct 179378SN/A/// behavior of the program. Print a warning only if the appropriate 180378SN/A/// trace flag is enabled. Return success to the target program. 1811706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1822680Sktlim@umich.edu Process *p, ThreadContext *tc); 183360SN/A 184378SN/A/// Target exit() handler: terminate simulation. 1851706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1862680Sktlim@umich.edu Process *p, ThreadContext *tc); 187378SN/A 188378SN/A/// Target getpagesize() handler. 1891706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1902680Sktlim@umich.edu Process *p, ThreadContext *tc); 191378SN/A 192378SN/A/// Target obreak() handler: set brk address. 1931706SN/ASyscallReturn obreakFunc(SyscallDesc *desc, int num, 1942680Sktlim@umich.edu Process *p, ThreadContext *tc); 195378SN/A 196378SN/A/// Target close() handler. 1971706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 1982680Sktlim@umich.edu Process *p, ThreadContext *tc); 199378SN/A 200378SN/A/// Target read() handler. 2011706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2022680Sktlim@umich.edu Process *p, ThreadContext *tc); 203378SN/A 204378SN/A/// Target write() handler. 2051706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2062680Sktlim@umich.edu Process *p, ThreadContext *tc); 207378SN/A 208378SN/A/// Target lseek() handler. 2091706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2102680Sktlim@umich.edu Process *p, ThreadContext *tc); 211378SN/A 212378SN/A/// Target munmap() handler. 2131706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2142680Sktlim@umich.edu Process *p, ThreadContext *tc); 215378SN/A 216378SN/A/// Target gethostname() handler. 2171706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2182680Sktlim@umich.edu Process *p, ThreadContext *tc); 219360SN/A 220511SN/A/// Target unlink() handler. 2211706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2222680Sktlim@umich.edu Process *p, ThreadContext *tc); 223511SN/A 224511SN/A/// Target rename() handler. 2251706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2262680Sktlim@umich.edu Process *p, ThreadContext *tc); 2271706SN/A 2281706SN/A 2291706SN/A/// Target truncate() handler. 2301706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2312680Sktlim@umich.edu Process *p, ThreadContext *tc); 2321706SN/A 2331706SN/A 2341706SN/A/// Target ftruncate() handler. 2351706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2362680Sktlim@umich.edu Process *p, ThreadContext *tc); 2371706SN/A 238511SN/A 2391999SN/A/// Target chown() handler. 2401999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2412680Sktlim@umich.edu Process *p, ThreadContext *tc); 2421999SN/A 2431999SN/A 2441999SN/A/// Target fchown() handler. 2451999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2462680Sktlim@umich.edu Process *p, ThreadContext *tc); 2471999SN/A 2482093SN/A/// Target fnctl() handler. 2492093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2502680Sktlim@umich.edu Process *process, ThreadContext *tc); 2512093SN/A 2522238SN/A/// Target setuid() handler. 2532238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2542680Sktlim@umich.edu Process *p, ThreadContext *tc); 2552238SN/A 2562238SN/A/// Target getpid() handler. 2572238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 2582680Sktlim@umich.edu Process *p, ThreadContext *tc); 2592238SN/A 2602238SN/A/// Target getuid() handler. 2612238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 2622680Sktlim@umich.edu Process *p, ThreadContext *tc); 2632238SN/A 2642238SN/A/// Target getgid() handler. 2652238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 2662680Sktlim@umich.edu Process *p, ThreadContext *tc); 2672238SN/A 2682238SN/A/// Target getppid() handler. 2692238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 2702680Sktlim@umich.edu Process *p, ThreadContext *tc); 2712238SN/A 2722238SN/A/// Target geteuid() handler. 2732238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2742680Sktlim@umich.edu Process *p, ThreadContext *tc); 2752238SN/A 2762238SN/A/// Target getegid() handler. 2772238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 2782680Sktlim@umich.edu Process *p, ThreadContext *tc); 2792238SN/A 2802238SN/A 2812238SN/A 2822238SN/A/// Pseudo Funcs - These functions use a different return convension, 2832238SN/A/// returning a second value in a register other than the normal return register 2842238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 2852680Sktlim@umich.edu Process *process, ThreadContext *tc); 2862238SN/A 2872238SN/A/// Target getpidPseudo() handler. 2882238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 2892680Sktlim@umich.edu Process *p, ThreadContext *tc); 2902238SN/A 2912238SN/A/// Target getuidPseudo() handler. 2922238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 2932680Sktlim@umich.edu Process *p, ThreadContext *tc); 2942238SN/A 2952238SN/A/// Target getgidPseudo() handler. 2962238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 2972680Sktlim@umich.edu Process *p, ThreadContext *tc); 2982238SN/A 2992238SN/A 3001354SN/A/// This struct is used to build an target-OS-dependent table that 3011354SN/A/// maps the target's open() flags to the host open() flags. 3021354SN/Astruct OpenFlagTransTable { 3031354SN/A int tgtFlag; //!< Target system flag value. 3041354SN/A int hostFlag; //!< Corresponding host system flag value. 3051354SN/A}; 3061354SN/A 3071354SN/A 3081354SN/A 3091354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3101354SN/Aconst int one_million = 1000000; 3111354SN/A 3121354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3131354SN/A/// by my reckoning. We want to keep this a constant (not use the 3141354SN/A/// real-world time) to keep simulations repeatable. 3151354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3161354SN/A 3171354SN/A/// Helper function to convert current elapsed time to seconds and 3181354SN/A/// microseconds. 3191354SN/Atemplate <class T1, class T2> 3201354SN/Avoid 3211354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3221354SN/A{ 3231609SN/A int elapsed_usecs = curTick / Clock::Int::us; 3241354SN/A sec = elapsed_usecs / one_million; 3251354SN/A usec = elapsed_usecs % one_million; 3261354SN/A} 3271354SN/A 328360SN/A////////////////////////////////////////////////////////////////////// 329360SN/A// 330360SN/A// The following emulation functions are generic, but need to be 331360SN/A// templated to account for differences in types, constants, etc. 332360SN/A// 333360SN/A////////////////////////////////////////////////////////////////////// 334360SN/A 335378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 336378SN/A/// only to find out if their stdout is a tty, to determine whether to 337378SN/A/// do line or block buffering. 338360SN/Atemplate <class OS> 3391450SN/ASyscallReturn 340360SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process, 3412680Sktlim@umich.edu ThreadContext *tc) 342360SN/A{ 3432680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 3442680Sktlim@umich.edu unsigned req = tc->getSyscallArg(1); 345360SN/A 3461969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 347360SN/A 348360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 349360SN/A // doesn't map to any simulator fd: not a valid target fd 3501458SN/A return -EBADF; 351360SN/A } 352360SN/A 353360SN/A switch (req) { 3542553SN/A case OS::TIOCISATTY: 3552553SN/A case OS::TIOCGETP: 3562553SN/A case OS::TIOCSETP: 3572553SN/A case OS::TIOCSETN: 3582553SN/A case OS::TIOCSETC: 3592553SN/A case OS::TIOCGETC: 3602553SN/A case OS::TIOCGETS: 3612553SN/A case OS::TIOCGETA: 3621458SN/A return -ENOTTY; 363360SN/A 364360SN/A default: 3651706SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 3662680Sktlim@umich.edu fd, req, tc->readPC()); 367360SN/A } 368360SN/A} 369360SN/A 370378SN/A/// Target open() handler. 371360SN/Atemplate <class OS> 3721450SN/ASyscallReturn 373360SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process, 3742680Sktlim@umich.edu ThreadContext *tc) 375360SN/A{ 376360SN/A std::string path; 377360SN/A 3782680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 3791458SN/A return -EFAULT; 380360SN/A 381360SN/A if (path == "/dev/sysdev0") { 382360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 383360SN/A // We don't support it, so just punt. 3841706SN/A warn("Ignoring open(%s, ...)\n", path); 3851458SN/A return -ENOENT; 386360SN/A } 387360SN/A 3882680Sktlim@umich.edu int tgtFlags = tc->getSyscallArg(1); 3892680Sktlim@umich.edu int mode = tc->getSyscallArg(2); 390360SN/A int hostFlags = 0; 391360SN/A 392360SN/A // translate open flags 393360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 394360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 395360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 396360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 397360SN/A } 398360SN/A } 399360SN/A 400360SN/A // any target flags left? 401360SN/A if (tgtFlags != 0) 4021706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 403360SN/A 404360SN/A#ifdef __CYGWIN32__ 405360SN/A hostFlags |= O_BINARY; 406360SN/A#endif 407360SN/A 4081706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 4091706SN/A 410360SN/A // open the file 411360SN/A int fd = open(path.c_str(), hostFlags, mode); 412360SN/A 4131970SN/A return (fd == -1) ? -errno : process->alloc_fd(fd); 414360SN/A} 415360SN/A 416360SN/A 4171999SN/A/// Target chmod() handler. 4181999SN/Atemplate <class OS> 4191999SN/ASyscallReturn 4201999SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process, 4212680Sktlim@umich.edu ThreadContext *tc) 4221999SN/A{ 4231999SN/A std::string path; 4241999SN/A 4252680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 4261999SN/A return -EFAULT; 4271999SN/A 4282680Sktlim@umich.edu uint32_t mode = tc->getSyscallArg(1); 4291999SN/A mode_t hostMode = 0; 4301999SN/A 4311999SN/A // XXX translate mode flags via OS::something??? 4321999SN/A hostMode = mode; 4331999SN/A 4341999SN/A // do the chmod 4351999SN/A int result = chmod(path.c_str(), hostMode); 4361999SN/A if (result < 0) 4372218SN/A return -errno; 4381999SN/A 4391999SN/A return 0; 4401999SN/A} 4411999SN/A 4421999SN/A 4431999SN/A/// Target fchmod() handler. 4441999SN/Atemplate <class OS> 4451999SN/ASyscallReturn 4461999SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *process, 4472680Sktlim@umich.edu ThreadContext *tc) 4481999SN/A{ 4492680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 4501999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 4511999SN/A // doesn't map to any simulator fd: not a valid target fd 4521999SN/A return -EBADF; 4531999SN/A } 4541999SN/A 4552680Sktlim@umich.edu uint32_t mode = tc->getSyscallArg(1); 4561999SN/A mode_t hostMode = 0; 4571999SN/A 4581999SN/A // XXX translate mode flags via OS::someting??? 4591999SN/A hostMode = mode; 4601999SN/A 4611999SN/A // do the fchmod 4621999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 4631999SN/A if (result < 0) 4642218SN/A return -errno; 4651999SN/A 4661999SN/A return 0; 4671999SN/A} 4681999SN/A 4691999SN/A 470378SN/A/// Target stat() handler. 471360SN/Atemplate <class OS> 4721450SN/ASyscallReturn 473360SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process, 4742680Sktlim@umich.edu ThreadContext *tc) 475360SN/A{ 476360SN/A std::string path; 477360SN/A 4782680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 4792400SN/A return -EFAULT; 480360SN/A 481360SN/A struct stat hostBuf; 482360SN/A int result = stat(path.c_str(), &hostBuf); 483360SN/A 484360SN/A if (result < 0) 4852218SN/A return -errno; 486360SN/A 4872680Sktlim@umich.edu OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 488360SN/A 4891458SN/A return 0; 490360SN/A} 491360SN/A 492360SN/A 4931999SN/A/// Target fstat64() handler. 4941999SN/Atemplate <class OS> 4951999SN/ASyscallReturn 4961999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *process, 4972680Sktlim@umich.edu ThreadContext *tc) 4981999SN/A{ 4992680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 5001999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 5011999SN/A // doesn't map to any simulator fd: not a valid target fd 5021999SN/A return -EBADF; 5031999SN/A } 5041999SN/A 5052067SN/A#if BSD_HOST 5062064SN/A struct stat hostBuf; 5072064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 5082064SN/A#else 5092064SN/A struct stat64 hostBuf; 5101999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 5112064SN/A#endif 5121999SN/A 5131999SN/A if (result < 0) 5142218SN/A return -errno; 5151999SN/A 5162680Sktlim@umich.edu OS::copyOutStat64Buf(tc->getMemPort(), fd, tc->getSyscallArg(1), &hostBuf); 5171999SN/A 5181999SN/A return 0; 5191999SN/A} 5201999SN/A 5211999SN/A 522378SN/A/// Target lstat() handler. 523360SN/Atemplate <class OS> 5241450SN/ASyscallReturn 525360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process, 5262680Sktlim@umich.edu ThreadContext *tc) 527360SN/A{ 528360SN/A std::string path; 529360SN/A 5302680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 5312400SN/A return -EFAULT; 532360SN/A 533360SN/A struct stat hostBuf; 534360SN/A int result = lstat(path.c_str(), &hostBuf); 535360SN/A 536360SN/A if (result < 0) 5371458SN/A return -errno; 538360SN/A 5392680Sktlim@umich.edu OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 540360SN/A 5411458SN/A return 0; 542360SN/A} 543360SN/A 5441999SN/A/// Target lstat64() handler. 5451999SN/Atemplate <class OS> 5461999SN/ASyscallReturn 5471999SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 5482680Sktlim@umich.edu ThreadContext *tc) 5491999SN/A{ 5501999SN/A std::string path; 5511999SN/A 5522680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 5532400SN/A return -EFAULT; 5541999SN/A 5552067SN/A#if BSD_HOST 5562064SN/A struct stat hostBuf; 5572064SN/A int result = lstat(path.c_str(), &hostBuf); 5582064SN/A#else 5591999SN/A struct stat64 hostBuf; 5601999SN/A int result = lstat64(path.c_str(), &hostBuf); 5612064SN/A#endif 5621999SN/A 5631999SN/A if (result < 0) 5641999SN/A return -errno; 5651999SN/A 5662680Sktlim@umich.edu OS::copyOutStat64Buf(tc->getMemPort(), -1, tc->getSyscallArg(1), &hostBuf); 5671999SN/A 5681999SN/A return 0; 5691999SN/A} 5701999SN/A 571378SN/A/// Target fstat() handler. 572360SN/Atemplate <class OS> 5731450SN/ASyscallReturn 574360SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *process, 5752680Sktlim@umich.edu ThreadContext *tc) 576360SN/A{ 5772680Sktlim@umich.edu int fd = process->sim_fd(tc->getSyscallArg(0)); 578360SN/A 5791969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 580360SN/A 581360SN/A if (fd < 0) 5821458SN/A return -EBADF; 583360SN/A 584360SN/A struct stat hostBuf; 585360SN/A int result = fstat(fd, &hostBuf); 586360SN/A 587360SN/A if (result < 0) 5881458SN/A return -errno; 589360SN/A 5902680Sktlim@umich.edu OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 5912021SN/A 5921458SN/A return 0; 593360SN/A} 594360SN/A 595360SN/A 5961706SN/A/// Target statfs() handler. 5971706SN/Atemplate <class OS> 5981706SN/ASyscallReturn 5991706SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 6002680Sktlim@umich.edu ThreadContext *tc) 6011706SN/A{ 6021706SN/A std::string path; 6031706SN/A 6042680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 6052400SN/A return -EFAULT; 6061706SN/A 6071706SN/A struct statfs hostBuf; 6081706SN/A int result = statfs(path.c_str(), &hostBuf); 6091706SN/A 6101706SN/A if (result < 0) 6112218SN/A return -errno; 6121706SN/A 6132680Sktlim@umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 6141706SN/A 6151706SN/A return 0; 6161706SN/A} 6171706SN/A 6181706SN/A 6191706SN/A/// Target fstatfs() handler. 6201706SN/Atemplate <class OS> 6211706SN/ASyscallReturn 6221706SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 6232680Sktlim@umich.edu ThreadContext *tc) 6241706SN/A{ 6252680Sktlim@umich.edu int fd = process->sim_fd(tc->getSyscallArg(0)); 6261706SN/A 6271706SN/A if (fd < 0) 6281706SN/A return -EBADF; 6291706SN/A 6301706SN/A struct statfs hostBuf; 6311706SN/A int result = fstatfs(fd, &hostBuf); 6321706SN/A 6331706SN/A if (result < 0) 6342218SN/A return -errno; 6351706SN/A 6362680Sktlim@umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 6371706SN/A 6381706SN/A return 0; 6391706SN/A} 6401706SN/A 6411706SN/A 6421999SN/A/// Target writev() handler. 6431999SN/Atemplate <class OS> 6441999SN/ASyscallReturn 6451999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process, 6462680Sktlim@umich.edu ThreadContext *tc) 6471999SN/A{ 6482680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 6491999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6501999SN/A // doesn't map to any simulator fd: not a valid target fd 6511999SN/A return -EBADF; 6521999SN/A } 6531999SN/A 6542680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 6552680Sktlim@umich.edu uint64_t tiov_base = tc->getSyscallArg(1); 6562680Sktlim@umich.edu size_t count = tc->getSyscallArg(2); 6571999SN/A struct iovec hiov[count]; 6581999SN/A for (int i = 0; i < count; ++i) 6591999SN/A { 6601999SN/A typename OS::tgt_iovec tiov; 6612461SN/A 6622461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 6632461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 6642091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 6651999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 6662461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 6672461SN/A hiov[i].iov_len); 6681999SN/A } 6691999SN/A 6701999SN/A int result = writev(process->sim_fd(fd), hiov, count); 6711999SN/A 6721999SN/A for (int i = 0; i < count; ++i) 6731999SN/A { 6741999SN/A delete [] (char *)hiov[i].iov_base; 6751999SN/A } 6761999SN/A 6771999SN/A if (result < 0) 6782218SN/A return -errno; 6791999SN/A 6801999SN/A return 0; 6811999SN/A} 6821999SN/A 6831999SN/A 684378SN/A/// Target mmap() handler. 685378SN/A/// 686378SN/A/// We don't really handle mmap(). If the target is mmaping an 687378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 688378SN/A/// nothing (since memory is initialized to zero and the simulator 689378SN/A/// doesn't really check addresses anyway). Always print a warning, 690378SN/A/// since this could be seriously broken if we're not mapping 691378SN/A/// /dev/zero. 692360SN/A// 693378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 694378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 695378SN/A/// anything else. 696360SN/Atemplate <class OS> 6971450SN/ASyscallReturn 6982680Sktlim@umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 699360SN/A{ 7002680Sktlim@umich.edu Addr start = tc->getSyscallArg(0); 7012680Sktlim@umich.edu uint64_t length = tc->getSyscallArg(1); 7022680Sktlim@umich.edu // int prot = tc->getSyscallArg(2); 7032680Sktlim@umich.edu int flags = tc->getSyscallArg(3); 7042680Sktlim@umich.edu // int fd = p->sim_fd(tc->getSyscallArg(4)); 7052680Sktlim@umich.edu // int offset = tc->getSyscallArg(5); 706360SN/A 7072544SN/A if ((start % TheISA::VMPageSize) != 0 || 7082544SN/A (length % TheISA::VMPageSize) != 0) { 7092544SN/A warn("mmap failing: arguments not page-aligned: " 7102544SN/A "start 0x%x length 0x%x", 7112544SN/A start, length); 7122544SN/A return -EINVAL; 713360SN/A } 714360SN/A 7152544SN/A if (start != 0) { 7162544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 7172544SN/A start, p->mmap_end); 7182544SN/A } 7192544SN/A 7202544SN/A // pick next address from our "mmap region" 7212544SN/A start = p->mmap_end; 7222544SN/A p->pTable->allocate(start, length); 7232544SN/A p->mmap_end += length; 7242544SN/A 7252553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 7261969SN/A warn("allowing mmap of file @ fd %d. " 7272680Sktlim@umich.edu "This will break if not /dev/zero.", tc->getSyscallArg(4)); 728360SN/A } 729360SN/A 7301458SN/A return start; 731360SN/A} 732360SN/A 733378SN/A/// Target getrlimit() handler. 734360SN/Atemplate <class OS> 7351450SN/ASyscallReturn 736360SN/AgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 7372680Sktlim@umich.edu ThreadContext *tc) 738360SN/A{ 7392680Sktlim@umich.edu unsigned resource = tc->getSyscallArg(0); 7402680Sktlim@umich.edu TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1)); 741360SN/A 742360SN/A switch (resource) { 7432064SN/A case OS::TGT_RLIMIT_STACK: 7442064SN/A // max stack size in bytes: make up a number (2MB for now) 7452064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 7462091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 7472091SN/A rlp->rlim_max = htog(rlp->rlim_max); 7482064SN/A break; 749360SN/A 7502064SN/A default: 7512064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 7522064SN/A << std::endl; 7532064SN/A abort(); 7542064SN/A break; 755360SN/A } 756360SN/A 7572680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 7581458SN/A return 0; 759360SN/A} 760360SN/A 761378SN/A/// Target gettimeofday() handler. 762360SN/Atemplate <class OS> 7631450SN/ASyscallReturn 764360SN/AgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 7652680Sktlim@umich.edu ThreadContext *tc) 766360SN/A{ 7672680Sktlim@umich.edu TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0)); 768360SN/A 769360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 770360SN/A tp->tv_sec += seconds_since_epoch; 7712091SN/A tp->tv_sec = htog(tp->tv_sec); 7722091SN/A tp->tv_usec = htog(tp->tv_usec); 773360SN/A 7742680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 775360SN/A 7761458SN/A return 0; 777360SN/A} 778360SN/A 779360SN/A 7801999SN/A/// Target utimes() handler. 7811999SN/Atemplate <class OS> 7821999SN/ASyscallReturn 7831999SN/AutimesFunc(SyscallDesc *desc, int callnum, Process *process, 7842680Sktlim@umich.edu ThreadContext *tc) 7851999SN/A{ 7861999SN/A std::string path; 7871999SN/A 7882680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 7892400SN/A return -EFAULT; 7901999SN/A 7912680Sktlim@umich.edu TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1)); 7922680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 7931999SN/A 7941999SN/A struct timeval hostTimeval[2]; 7951999SN/A for (int i = 0; i < 2; ++i) 7961999SN/A { 7972091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 7982091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 7991999SN/A } 8001999SN/A int result = utimes(path.c_str(), hostTimeval); 8011999SN/A 8021999SN/A if (result < 0) 8031999SN/A return -errno; 8041999SN/A 8051999SN/A return 0; 8061999SN/A} 807378SN/A/// Target getrusage() function. 808360SN/Atemplate <class OS> 8091450SN/ASyscallReturn 810360SN/AgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 8112680Sktlim@umich.edu ThreadContext *tc) 812360SN/A{ 8132680Sktlim@umich.edu int who = tc->getSyscallArg(0); // THREAD, SELF, or CHILDREN 8142680Sktlim@umich.edu TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1)); 815360SN/A 8162553SN/A if (who != OS::TGT_RUSAGE_SELF) { 817360SN/A // don't really handle THREAD or CHILDREN, but just warn and 818360SN/A // plow ahead 8191969SN/A warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 8201969SN/A who); 821360SN/A } 822360SN/A 823360SN/A getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 8242091SN/A rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 8252091SN/A rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 8262091SN/A 827360SN/A rup->ru_stime.tv_sec = 0; 828360SN/A rup->ru_stime.tv_usec = 0; 829360SN/A rup->ru_maxrss = 0; 830360SN/A rup->ru_ixrss = 0; 831360SN/A rup->ru_idrss = 0; 832360SN/A rup->ru_isrss = 0; 833360SN/A rup->ru_minflt = 0; 834360SN/A rup->ru_majflt = 0; 835360SN/A rup->ru_nswap = 0; 836360SN/A rup->ru_inblock = 0; 837360SN/A rup->ru_oublock = 0; 838360SN/A rup->ru_msgsnd = 0; 839360SN/A rup->ru_msgrcv = 0; 840360SN/A rup->ru_nsignals = 0; 841360SN/A rup->ru_nvcsw = 0; 842360SN/A rup->ru_nivcsw = 0; 843360SN/A 8442680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 845360SN/A 8461458SN/A return 0; 847360SN/A} 848360SN/A 8492553SN/A 8502553SN/A 8512553SN/A 8521354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 853