syscall_emul.hh revision 2238
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 292665Ssaidi@eecs.umich.edu#ifndef __SIM_SYSCALL_EMUL_HH__ 30360SN/A#define __SIM_SYSCALL_EMUL_HH__ 31360SN/A 321354SN/A#define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \ 331354SN/A defined(__FreeBSD__)) 34360SN/A 352764Sstever@eecs.umich.edu/// 362764Sstever@eecs.umich.edu/// @file syscall_emul.hh 372064SN/A/// 38360SN/A/// This file defines objects used to emulate syscalls from the target 39360SN/A/// application on the host machine. 40360SN/A 41360SN/A#include <errno.h> 42360SN/A#include <string> 43360SN/A#ifdef __CYGWIN32__ 441809SN/A#include <sys/fcntl.h> // for O_BINARY 455543Ssaidi@eecs.umich.edu#endif 461809SN/A#include <sys/uio.h> 473113Sgblack@eecs.umich.edu 487075Snate@binkert.org#include "base/intmath.hh" // for RoundUp 493113Sgblack@eecs.umich.edu#include "mem/functional/functional.hh" 501999SN/A#include "arch/isa_traits.hh" // for Addr 517075Snate@binkert.org 527075Snate@binkert.org#include "base/trace.hh" 537075Snate@binkert.org#include "cpu/exec_context.hh" 54360SN/A#include "sim/process.hh" 552474SN/A 565543Ssaidi@eecs.umich.edu/// 572462SN/A/// System call descriptor. 581354SN/A/// 596216Snate@binkert.orgclass SyscallDesc { 606658Snate@binkert.org 612474SN/A public: 622680Sktlim@umich.edu 632474SN/A /// Typedef for target syscall handler functions. 642474SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 657678Sgblack@eecs.umich.edu Process *, ExecContext *); 666640Svince@csl.cornell.edu 671354SN/A const char *name; //!< Syscall name (e.g., "open"). 68360SN/A FuncPtr funcPtr; //!< Pointer to emulation function. 69360SN/A int flags; //!< Flags (see Flags enum). 70360SN/A 71360SN/A /// Flag values for controlling syscall behavior. 72360SN/A enum Flags { 73360SN/A /// Don't set return regs according to funcPtr return value. 74360SN/A /// Used for syscalls with non-standard return conventions 75360SN/A /// that explicitly set the ExecContext regs (e.g., 76378SN/A /// sigreturn). 771450SN/A SuppressReturnValue = 1 783114Sgblack@eecs.umich.edu }; 79360SN/A 805543Ssaidi@eecs.umich.edu /// Constructor. 815543Ssaidi@eecs.umich.edu SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 825543Ssaidi@eecs.umich.edu : name(_name), funcPtr(_funcPtr), flags(_flags) 83360SN/A { 84360SN/A } 85360SN/A 86360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 87360SN/A void doSyscall(int callnum, Process *proc, ExecContext *xc); 882680Sktlim@umich.edu}; 89360SN/A 90360SN/A 91360SN/Aclass BaseBufferArg { 92360SN/A 93360SN/A public: 94360SN/A 95360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 96360SN/A { 97360SN/A bufPtr = new uint8_t[size]; 98360SN/A // clear out buffer: in case we only partially populate this, 99360SN/A // and then do a copyOut(), we want to make sure we don't 1003114Sgblack@eecs.umich.edu // introduce any random junk into the simulated address space 101360SN/A memset(bufPtr, 0, size); 102360SN/A } 103360SN/A 104360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 105360SN/A 106360SN/A // 107360SN/A // copy data into simulator space (read from target memory) 108360SN/A // 109360SN/A virtual bool copyIn(FunctionalMemory *mem) 110360SN/A { 111360SN/A mem->access(Read, addr, bufPtr, size); 112360SN/A return true; // no EFAULT detection for now 113360SN/A } 114360SN/A 115360SN/A // 116360SN/A // copy data out of simulator space (write to target memory) 117360SN/A // 118360SN/A virtual bool copyOut(FunctionalMemory *mem) 119360SN/A { 120360SN/A mem->access(Write, addr, bufPtr, size); 121360SN/A return true; // no EFAULT detection for now 1222400SN/A } 123360SN/A 1242461SN/A protected: 1255543Ssaidi@eecs.umich.edu Addr addr; 126360SN/A int size; 127360SN/A uint8_t *bufPtr; 128360SN/A}; 129360SN/A 130360SN/A 1312400SN/Aclass BufferArg : public BaseBufferArg 132360SN/A{ 1332461SN/A public: 1345543Ssaidi@eecs.umich.edu BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 135360SN/A void *bufferPtr() { return bufPtr; } 136360SN/A}; 137360SN/A 138360SN/Atemplate <class T> 139360SN/Aclass TypedBufferArg : public BaseBufferArg 140360SN/A{ 141360SN/A public: 142360SN/A // user can optionally specify a specific number of bytes to 143360SN/A // allocate to deal with those structs that have variable-size 144360SN/A // arrays at the end 145360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 146360SN/A : BaseBufferArg(_addr, _size) 147360SN/A { } 1485543Ssaidi@eecs.umich.edu 149360SN/A // type case 150360SN/A operator T*() { return (T *)bufPtr; } 151360SN/A 152360SN/A // dereference operators 153360SN/A T &operator*() { return *((T *)bufPtr); } 154360SN/A T* operator->() { return (T *)bufPtr; } 155360SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 156360SN/A}; 157360SN/A 158360SN/A////////////////////////////////////////////////////////////////////// 159360SN/A// 160360SN/A// The following emulation functions are generic enough that they 161360SN/A// don't need to be recompiled for different emulated OS's. They are 162360SN/A// defined in sim/syscall_emul.cc. 163360SN/A// 164360SN/A////////////////////////////////////////////////////////////////////// 165360SN/A 1665543Ssaidi@eecs.umich.edu 1675543Ssaidi@eecs.umich.edu/// Handler for unimplemented syscalls that we haven't thought about. 168502SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 169360SN/A Process *p, ExecContext *xc); 170360SN/A 171360SN/A/// Handler for unimplemented syscalls that we never intend to 172360SN/A/// implement (signal handling, etc.) and should not affect the correct 173360SN/A/// behavior of the program. Print a warning only if the appropriate 174360SN/A/// trace flag is enabled. Return success to the target program. 175360SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 176360SN/A Process *p, ExecContext *xc); 177360SN/A 178360SN/A/// Target exit() handler: terminate simulation. 179360SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 180378SN/A Process *p, ExecContext *xc); 1811706SN/A 1823114Sgblack@eecs.umich.edu/// Target getpagesize() handler. 183378SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 184378SN/A Process *p, ExecContext *xc); 185378SN/A 186378SN/A/// Target obreak() handler: set brk address. 187378SN/ASyscallReturn obreakFunc(SyscallDesc *desc, int num, 1881706SN/A Process *p, ExecContext *xc); 1893114Sgblack@eecs.umich.edu 190360SN/A/// Target close() handler. 1916109Ssanchezd@stanford.eduSyscallReturn closeFunc(SyscallDesc *desc, int num, 1921706SN/A Process *p, ExecContext *xc); 1933114Sgblack@eecs.umich.edu 194378SN/A/// Target read() handler. 1956109Ssanchezd@stanford.eduSyscallReturn readFunc(SyscallDesc *desc, int num, 1966109Ssanchezd@stanford.edu Process *p, ExecContext *xc); 1976109Ssanchezd@stanford.edu 1986109Ssanchezd@stanford.edu/// Target write() handler. 199378SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2001706SN/A Process *p, ExecContext *xc); 2013114Sgblack@eecs.umich.edu 202378SN/A/// Target lseek() handler. 2035748SSteve.Reinhardt@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 2045748SSteve.Reinhardt@amd.com Process *p, ExecContext *xc); 2055748SSteve.Reinhardt@amd.com 206378SN/A/// Target munmap() handler. 207378SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2081706SN/A Process *p, ExecContext *xc); 2093114Sgblack@eecs.umich.edu 210378SN/A/// Target gethostname() handler. 211378SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2121706SN/A Process *p, ExecContext *xc); 2133114Sgblack@eecs.umich.edu 214378SN/A/// Target unlink() handler. 215378SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2161706SN/A Process *p, ExecContext *xc); 2173114Sgblack@eecs.umich.edu 218378SN/A/// Target rename() handler. 219378SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2201706SN/A Process *p, ExecContext *xc); 2213114Sgblack@eecs.umich.edu 222378SN/A 2234118Sgblack@eecs.umich.edu/// Target truncate() handler. 2244118Sgblack@eecs.umich.eduSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2254118Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 2264118Sgblack@eecs.umich.edu 227378SN/A 2281706SN/A/// Target ftruncate() handler. 2293114Sgblack@eecs.umich.eduSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 230378SN/A Process *p, ExecContext *xc); 231378SN/A 2321706SN/A 2333114Sgblack@eecs.umich.edu/// Target chown() handler. 234360SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2355513SMichael.Adler@intel.com Process *p, ExecContext *xc); 2365513SMichael.Adler@intel.com 2375513SMichael.Adler@intel.com 2385513SMichael.Adler@intel.com/// Target fchown() handler. 2395513SMichael.Adler@intel.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2405513SMichael.Adler@intel.com Process *p, ExecContext *xc); 2415513SMichael.Adler@intel.com 2425513SMichael.Adler@intel.com/// Target fnctl() handler. 243511SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2441706SN/A Process *process, ExecContext *xc); 2453114Sgblack@eecs.umich.edu 246511SN/A/// Target setuid() handler. 2475513SMichael.Adler@intel.comSyscallReturn setuidFunc(SyscallDesc *desc, int num, 2485513SMichael.Adler@intel.com Process *p, ExecContext *xc); 2495513SMichael.Adler@intel.com 2505513SMichael.Adler@intel.com/// Target getpid() handler. 251511SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 2521706SN/A Process *p, ExecContext *xc); 2533114Sgblack@eecs.umich.edu 2541706SN/A/// Target getuid() handler. 2551706SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 2561706SN/A Process *p, ExecContext *xc); 2571706SN/A 2583114Sgblack@eecs.umich.edu/// Target getgid() handler. 2591706SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 2601706SN/A Process *p, ExecContext *xc); 2611706SN/A 2621706SN/A/// Target getppid() handler. 2633114Sgblack@eecs.umich.eduSyscallReturn getppidFunc(SyscallDesc *desc, int num, 2641706SN/A Process *p, ExecContext *xc); 265511SN/A 2666703Svince@csl.cornell.edu/// Target geteuid() handler. 2676703Svince@csl.cornell.eduSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2686703Svince@csl.cornell.edu Process *p, ExecContext *xc); 2696703Svince@csl.cornell.edu 2706685Stjones1@inf.ed.ac.uk/// Target getegid() handler. 2716685Stjones1@inf.ed.ac.ukSyscallReturn getegidFunc(SyscallDesc *desc, int num, 2726685Stjones1@inf.ed.ac.uk Process *p, ExecContext *xc); 2736685Stjones1@inf.ed.ac.uk 2746685Stjones1@inf.ed.ac.uk 2755513SMichael.Adler@intel.com 2765513SMichael.Adler@intel.com/// Pseudo Funcs - These functions use a different return convension, 2775513SMichael.Adler@intel.com/// returning a second value in a register other than the normal return register 2785513SMichael.Adler@intel.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 2795513SMichael.Adler@intel.com Process *process, ExecContext *xc); 2801999SN/A 2811999SN/A/// Target getpidPseudo() handler. 2823114Sgblack@eecs.umich.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 2831999SN/A Process *p, ExecContext *xc); 2841999SN/A 2851999SN/A/// Target getuidPseudo() handler. 2861999SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 2873114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 2881999SN/A 2893079Sstever@eecs.umich.edu/// Target getgidPseudo() handler. 2903079Sstever@eecs.umich.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 2913114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 2923079Sstever@eecs.umich.edu 2932093SN/A 2942093SN/A/// This struct is used to build an target-OS-dependent table that 2953114Sgblack@eecs.umich.edu/// maps the target's open() flags to the host open() flags. 2962093SN/Astruct OpenFlagTransTable { 2972687Sksewell@umich.edu int tgtFlag; //!< Target system flag value. 2982687Sksewell@umich.edu int hostFlag; //!< Corresponding host system flag value. 2993114Sgblack@eecs.umich.edu}; 3002687Sksewell@umich.edu 3012238SN/A 3022238SN/A 3033114Sgblack@eecs.umich.edu/// A readable name for 1,000,000, for converting microseconds to seconds. 3042238SN/Aconst int one_million = 1000000; 3052238SN/A 3062238SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3073114Sgblack@eecs.umich.edu/// by my reckoning. We want to keep this a constant (not use the 3082238SN/A/// real-world time) to keep simulations repeatable. 3092238SN/Aconst unsigned seconds_since_epoch = 1000000000; 3102238SN/A 3113114Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 3122238SN/A/// microseconds. 3132238SN/Atemplate <class T1, class T2> 3142238SN/Avoid 3153114Sgblack@eecs.umich.edugetElapsedTime(T1 &sec, T2 &usec) 3162238SN/A{ 3172238SN/A int elapsed_usecs = curTick / Clock::Int::us; 3182238SN/A sec = elapsed_usecs / one_million; 3193114Sgblack@eecs.umich.edu usec = elapsed_usecs % one_million; 3202238SN/A} 3212238SN/A 3222238SN/A////////////////////////////////////////////////////////////////////// 3233114Sgblack@eecs.umich.edu// 3242238SN/A// The following emulation functions are generic, but need to be 3252238SN/A// templated to account for differences in types, constants, etc. 3262238SN/A// 3273114Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////// 3282238SN/A 3296109Ssanchezd@stanford.edu/// Target ioctl() handler. For the most part, programs call ioctl() 3306109Ssanchezd@stanford.edu/// only to find out if their stdout is a tty, to determine whether to 3316109Ssanchezd@stanford.edu/// do line or block buffering. 3322238SN/Atemplate <class OS> 3332238SN/ASyscallReturn 3342238SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process, 3352238SN/A ExecContext *xc) 3362238SN/A{ 3373114Sgblack@eecs.umich.edu int fd = xc->getSyscallArg(0); 3382238SN/A unsigned req = xc->getSyscallArg(1); 3392238SN/A 3402238SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 3413114Sgblack@eecs.umich.edu 3422238SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 3432238SN/A // doesn't map to any simulator fd: not a valid target fd 3442238SN/A return -EBADF; 3453114Sgblack@eecs.umich.edu } 3462238SN/A 3472238SN/A switch (req) { 3482238SN/A case OS::TIOCISATTY: 3493114Sgblack@eecs.umich.edu case OS::TIOCGETP: 3502238SN/A case OS::TIOCSETP: 3512238SN/A case OS::TIOCSETN: 3521354SN/A case OS::TIOCSETC: 3531354SN/A case OS::TIOCGETC: 3541354SN/A case OS::TIOCGETS: 3551354SN/A case OS::TIOCGETA: 3561354SN/A return -ENOTTY; 3571354SN/A 3581354SN/A default: 3591354SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 3601354SN/A fd, req, xc->readPC()); 3611354SN/A } 3621354SN/A} 3631354SN/A 3641354SN/A/// Target open() handler. 3651354SN/Atemplate <class OS> 3667064Snate@binkert.orgSyscallReturn 3671354SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process, 3681354SN/A ExecContext *xc) 3691354SN/A{ 3701354SN/A std::string path; 371360SN/A 372360SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 373360SN/A return -EFAULT; 374360SN/A 375360SN/A if (path == "/dev/sysdev0") { 376360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 377360SN/A // We don't support it, so just punt. 3783113Sgblack@eecs.umich.edu warn("Ignoring open(%s, ...)\n", path); 3793113Sgblack@eecs.umich.edu return -ENOENT; 3803113Sgblack@eecs.umich.edu } 3813113Sgblack@eecs.umich.edu 3823113Sgblack@eecs.umich.edu int tgtFlags = xc->getSyscallArg(1); 3833113Sgblack@eecs.umich.edu int mode = xc->getSyscallArg(2); 3843113Sgblack@eecs.umich.edu int hostFlags = 0; 3853113Sgblack@eecs.umich.edu 3863113Sgblack@eecs.umich.edu // translate open flags 3873113Sgblack@eecs.umich.edu for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 3883113Sgblack@eecs.umich.edu if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 3893113Sgblack@eecs.umich.edu tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 3903113Sgblack@eecs.umich.edu hostFlags |= OS::openFlagTable[i].hostFlag; 3913113Sgblack@eecs.umich.edu } 3923113Sgblack@eecs.umich.edu } 3933113Sgblack@eecs.umich.edu 3944189Sgblack@eecs.umich.edu // any target flags left? 3954189Sgblack@eecs.umich.edu if (tgtFlags != 0) 3963113Sgblack@eecs.umich.edu warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 3973113Sgblack@eecs.umich.edu 3983113Sgblack@eecs.umich.edu#ifdef __CYGWIN32__ 3993113Sgblack@eecs.umich.edu hostFlags |= O_BINARY; 4003113Sgblack@eecs.umich.edu#endif 4013113Sgblack@eecs.umich.edu 4023113Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 4033277Sgblack@eecs.umich.edu 4045515SMichael.Adler@intel.com // open the file 4055515SMichael.Adler@intel.com int fd = open(path.c_str(), hostFlags, mode); 4065515SMichael.Adler@intel.com 4075515SMichael.Adler@intel.com return (fd == -1) ? -errno : process->alloc_fd(fd); 4085515SMichael.Adler@intel.com} 4093277Sgblack@eecs.umich.edu 4103277Sgblack@eecs.umich.edu 4113277Sgblack@eecs.umich.edu/// Target chmod() handler. 4123277Sgblack@eecs.umich.edutemplate <class OS> 4133277Sgblack@eecs.umich.eduSyscallReturn 4143277Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, Process *process, 4153277Sgblack@eecs.umich.edu ExecContext *xc) 4163113Sgblack@eecs.umich.edu{ 4173113Sgblack@eecs.umich.edu std::string path; 4183113Sgblack@eecs.umich.edu 4193113Sgblack@eecs.umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 4203113Sgblack@eecs.umich.edu return -EFAULT; 4213113Sgblack@eecs.umich.edu 4223113Sgblack@eecs.umich.edu uint32_t mode = xc->getSyscallArg(1); 4233114Sgblack@eecs.umich.edu mode_t hostMode = 0; 4243113Sgblack@eecs.umich.edu 4253114Sgblack@eecs.umich.edu // XXX translate mode flags via OS::something??? 4263113Sgblack@eecs.umich.edu hostMode = mode; 4273114Sgblack@eecs.umich.edu 4283113Sgblack@eecs.umich.edu // do the chmod 4294061Sgblack@eecs.umich.edu int result = chmod(path.c_str(), hostMode); 4304061Sgblack@eecs.umich.edu if (result < 0) 4314061Sgblack@eecs.umich.edu return -errno; 4323113Sgblack@eecs.umich.edu 4333113Sgblack@eecs.umich.edu return 0; 4343113Sgblack@eecs.umich.edu} 4353113Sgblack@eecs.umich.edu 4363113Sgblack@eecs.umich.edu 4373113Sgblack@eecs.umich.edu/// Target fchmod() handler. 4383113Sgblack@eecs.umich.edutemplate <class OS> 4393113Sgblack@eecs.umich.eduSyscallReturn 4403113Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *process, 4413113Sgblack@eecs.umich.edu ExecContext *xc) 4423113Sgblack@eecs.umich.edu{ 4434189Sgblack@eecs.umich.edu int fd = xc->getSyscallArg(0); 4444189Sgblack@eecs.umich.edu if (fd < 0 || process->sim_fd(fd) < 0) { 4453113Sgblack@eecs.umich.edu // doesn't map to any simulator fd: not a valid target fd 4463113Sgblack@eecs.umich.edu return -EBADF; 4473113Sgblack@eecs.umich.edu } 4483113Sgblack@eecs.umich.edu 4493113Sgblack@eecs.umich.edu uint32_t mode = xc->getSyscallArg(1); 4503113Sgblack@eecs.umich.edu mode_t hostMode = 0; 4513113Sgblack@eecs.umich.edu 4523113Sgblack@eecs.umich.edu // XXX translate mode flags via OS::someting??? 4533113Sgblack@eecs.umich.edu hostMode = mode; 4543113Sgblack@eecs.umich.edu 4553113Sgblack@eecs.umich.edu // do the fchmod 4563113Sgblack@eecs.umich.edu int result = fchmod(process->sim_fd(fd), hostMode); 4573113Sgblack@eecs.umich.edu if (result < 0) 4583113Sgblack@eecs.umich.edu return -errno; 4593113Sgblack@eecs.umich.edu 4603113Sgblack@eecs.umich.edu return 0; 4613113Sgblack@eecs.umich.edu} 4623113Sgblack@eecs.umich.edu 4633113Sgblack@eecs.umich.edu 4643113Sgblack@eecs.umich.edu/// Target stat() handler. 4653113Sgblack@eecs.umich.edutemplate <class OS> 4663113Sgblack@eecs.umich.eduSyscallReturn 4673113Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, Process *process, 4683113Sgblack@eecs.umich.edu ExecContext *xc) 4693113Sgblack@eecs.umich.edu{ 4703113Sgblack@eecs.umich.edu std::string path; 4713113Sgblack@eecs.umich.edu 4723113Sgblack@eecs.umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 4733113Sgblack@eecs.umich.edu return -EFAULT; 4743113Sgblack@eecs.umich.edu 4753113Sgblack@eecs.umich.edu struct stat hostBuf; 4763113Sgblack@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 4773113Sgblack@eecs.umich.edu 4783113Sgblack@eecs.umich.edu if (result < 0) 4796686Stjones1@inf.ed.ac.uk return -errno; 4803113Sgblack@eecs.umich.edu 4813113Sgblack@eecs.umich.edu OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 4823113Sgblack@eecs.umich.edu 483378SN/A return 0; 484378SN/A} 485378SN/A 486360SN/A 4871450SN/A/// Target fstat64() handler. 4883114Sgblack@eecs.umich.edutemplate <class OS> 4892680Sktlim@umich.eduSyscallReturn 490360SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *process, 4916701Sgblack@eecs.umich.edu ExecContext *xc) 4926701Sgblack@eecs.umich.edu{ 4936701Sgblack@eecs.umich.edu int fd = xc->getSyscallArg(0); 494360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 4951969SN/A // doesn't map to any simulator fd: not a valid target fd 496360SN/A return -EBADF; 497360SN/A } 498360SN/A 4991458SN/A#if BSD_HOST 500360SN/A struct stat hostBuf; 501360SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 502360SN/A#else 5034131Sbinkertn@umich.edu struct stat64 hostBuf; 5044131Sbinkertn@umich.edu int result = fstat64(process->sim_fd(fd), &hostBuf); 5054131Sbinkertn@umich.edu#endif 5064131Sbinkertn@umich.edu 5074131Sbinkertn@umich.edu if (result < 0) 5084131Sbinkertn@umich.edu return -errno; 5094131Sbinkertn@umich.edu 5104131Sbinkertn@umich.edu OS::copyOutStat64Buf(xc->mem, fd, xc->getSyscallArg(1), &hostBuf); 5116689Stjones1@inf.ed.ac.uk 5121458SN/A return 0; 513360SN/A} 514360SN/A 5157720Sgblack@eecs.umich.edu 5167720Sgblack@eecs.umich.edu/// Target lstat() handler. 517360SN/Atemplate <class OS> 518360SN/ASyscallReturn 519360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process, 520378SN/A ExecContext *xc) 521360SN/A{ 5221450SN/A std::string path; 5233114Sgblack@eecs.umich.edu 5242680Sktlim@umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 525360SN/A return -EFAULT; 526360SN/A 527360SN/A struct stat hostBuf; 5286701Sgblack@eecs.umich.edu int result = lstat(path.c_str(), &hostBuf); 5296701Sgblack@eecs.umich.edu 5306701Sgblack@eecs.umich.edu if (result < 0) 5311458SN/A return -errno; 532360SN/A 533360SN/A OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 534360SN/A 535360SN/A return 0; 5361706SN/A} 5371458SN/A 538360SN/A/// Target lstat64() handler. 539360SN/Atemplate <class OS> 5406701Sgblack@eecs.umich.eduSyscallReturn 5416701Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, Process *process, 542360SN/A ExecContext *xc) 543360SN/A{ 544360SN/A std::string path; 545360SN/A 546360SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 547360SN/A return -EFAULT; 548360SN/A 549360SN/A#if BSD_HOST 550360SN/A struct stat hostBuf; 551360SN/A int result = lstat(path.c_str(), &hostBuf); 552360SN/A#else 553360SN/A struct stat64 hostBuf; 5541706SN/A int result = lstat64(path.c_str(), &hostBuf); 555360SN/A#endif 556360SN/A 557360SN/A if (result < 0) 558360SN/A return -errno; 559360SN/A 5603669Sbinkertn@umich.edu OS::copyOutStat64Buf(xc->mem, -1, xc->getSyscallArg(1), &hostBuf); 5613669Sbinkertn@umich.edu 5623669Sbinkertn@umich.edu return 0; 5631706SN/A} 5641706SN/A 5655795Ssaidi@eecs.umich.edu/// Target fstat() handler. 5665795Ssaidi@eecs.umich.edutemplate <class OS> 5675795Ssaidi@eecs.umich.eduSyscallReturn 5685795Ssaidi@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *process, 5695795Ssaidi@eecs.umich.edu ExecContext *xc) 5705795Ssaidi@eecs.umich.edu{ 5715795Ssaidi@eecs.umich.edu int fd = process->sim_fd(xc->getSyscallArg(0)); 5725795Ssaidi@eecs.umich.edu 5735795Ssaidi@eecs.umich.edu DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 5745795Ssaidi@eecs.umich.edu 5755795Ssaidi@eecs.umich.edu if (fd < 0) 576360SN/A return -EBADF; 577360SN/A 578360SN/A struct stat hostBuf; 5796640Svince@csl.cornell.edu int result = fstat(fd, &hostBuf); 5806640Svince@csl.cornell.edu 5816640Svince@csl.cornell.edu if (result < 0) 5826640Svince@csl.cornell.edu return -errno; 5836640Svince@csl.cornell.edu 5846640Svince@csl.cornell.edu OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 5856640Svince@csl.cornell.edu return 0; 5866701Sgblack@eecs.umich.edu} 5876701Sgblack@eecs.umich.edu 5886701Sgblack@eecs.umich.edu 5896640Svince@csl.cornell.edu/// Target statfs() handler. 5906701Sgblack@eecs.umich.edutemplate <class OS> 5916701Sgblack@eecs.umich.eduSyscallReturn 5926640Svince@csl.cornell.edustatfsFunc(SyscallDesc *desc, int callnum, Process *process, 5936701Sgblack@eecs.umich.edu ExecContext *xc) 5946640Svince@csl.cornell.edu{ 5956701Sgblack@eecs.umich.edu std::string path; 5966640Svince@csl.cornell.edu 597360SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 5981999SN/A return -EFAULT; 5991999SN/A 6001999SN/A struct statfs hostBuf; 6013114Sgblack@eecs.umich.edu int result = statfs(path.c_str(), &hostBuf); 6022680Sktlim@umich.edu 6031999SN/A if (result < 0) 6041999SN/A return -errno; 6051999SN/A 6066701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 6076701Sgblack@eecs.umich.edu 6086701Sgblack@eecs.umich.edu return 0; 6091999SN/A} 6106701Sgblack@eecs.umich.edu 6111999SN/A 6126701Sgblack@eecs.umich.edu/// Target fstatfs() handler. 6131999SN/Atemplate <class OS> 6141999SN/ASyscallReturn 6151999SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 6161999SN/A ExecContext *xc) 6171999SN/A{ 6183669Sbinkertn@umich.edu int fd = process->sim_fd(xc->getSyscallArg(0)); 6193669Sbinkertn@umich.edu 6203669Sbinkertn@umich.edu if (fd < 0) 6211999SN/A return -EBADF; 6221999SN/A 6231999SN/A struct statfs hostBuf; 6242218SN/A int result = fstatfs(fd, &hostBuf); 6251999SN/A 6261999SN/A if (result < 0) 6271999SN/A return -errno; 6281999SN/A 6291999SN/A OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 6301999SN/A 6311999SN/A return 0; 6321999SN/A} 6333114Sgblack@eecs.umich.edu 6342680Sktlim@umich.edu 6351999SN/A/// Target writev() handler. 6366701Sgblack@eecs.umich.edutemplate <class OS> 6376701Sgblack@eecs.umich.eduSyscallReturn 6381999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process, 6391999SN/A ExecContext *xc) 6401999SN/A{ 6411999SN/A int fd = xc->getSyscallArg(0); 6421999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6436701Sgblack@eecs.umich.edu // doesn't map to any simulator fd: not a valid target fd 6441999SN/A return -EBADF; 6451999SN/A } 6461999SN/A 6471999SN/A uint64_t tiov_base = xc->getSyscallArg(1); 6481999SN/A size_t count = xc->getSyscallArg(2); 6491999SN/A struct iovec hiov[count]; 6501999SN/A for (int i = 0; i < count; ++i) 6511999SN/A { 6522218SN/A typename OS::tgt_iovec tiov; 6531999SN/A xc->mem->access(Read, tiov_base + i*sizeof(typename OS::tgt_iovec), 6541999SN/A &tiov, sizeof(typename OS::tgt_iovec)); 6551999SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 6561999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 6575877Shsul@eecs.umich.edu xc->mem->access(Read, gtoh(tiov.iov_base), 6585877Shsul@eecs.umich.edu hiov[i].iov_base, hiov[i].iov_len); 6595877Shsul@eecs.umich.edu } 6605877Shsul@eecs.umich.edu 6615877Shsul@eecs.umich.edu int result = writev(process->sim_fd(fd), hiov, count); 6626701Sgblack@eecs.umich.edu 6636701Sgblack@eecs.umich.edu for (int i = 0; i < count; ++i) 6646701Sgblack@eecs.umich.edu { 6656701Sgblack@eecs.umich.edu delete [] (char *)hiov[i].iov_base; 6666701Sgblack@eecs.umich.edu } 6675877Shsul@eecs.umich.edu 6685877Shsul@eecs.umich.edu if (result < 0) 6695877Shsul@eecs.umich.edu return -errno; 6705877Shsul@eecs.umich.edu 6715877Shsul@eecs.umich.edu return 0; 6725877Shsul@eecs.umich.edu} 6735877Shsul@eecs.umich.edu 6745877Shsul@eecs.umich.edu 6755877Shsul@eecs.umich.edu/// Target mmap() handler. 6765877Shsul@eecs.umich.edu/// 6775877Shsul@eecs.umich.edu/// We don't really handle mmap(). If the target is mmaping an 6785877Shsul@eecs.umich.edu/// anonymous region or /dev/zero, we can get away with doing basically 6795877Shsul@eecs.umich.edu/// nothing (since memory is initialized to zero and the simulator 6805877Shsul@eecs.umich.edu/// doesn't really check addresses anyway). Always print a warning, 6815877Shsul@eecs.umich.edu/// since this could be seriously broken if we're not mapping 6825877Shsul@eecs.umich.edu/// /dev/zero. 6835877Shsul@eecs.umich.edu// 6845877Shsul@eecs.umich.edu/// Someday we should explicitly check for /dev/zero in open, flag the 6855877Shsul@eecs.umich.edu/// file descriptor, and fail (or implement!) a non-anonymous mmap to 6865877Shsul@eecs.umich.edu/// anything else. 6875877Shsul@eecs.umich.edutemplate <class OS> 6885877Shsul@eecs.umich.eduSyscallReturn 6895877Shsul@eecs.umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc) 6905877Shsul@eecs.umich.edu{ 6915877Shsul@eecs.umich.edu Addr start = xc->getSyscallArg(0); 6925877Shsul@eecs.umich.edu uint64_t length = xc->getSyscallArg(1); 6935877Shsul@eecs.umich.edu // int prot = xc->getSyscallArg(2); 6945877Shsul@eecs.umich.edu int flags = xc->getSyscallArg(3); 6955877Shsul@eecs.umich.edu // int fd = p->sim_fd(xc->getSyscallArg(4)); 6965877Shsul@eecs.umich.edu // int offset = xc->getSyscallArg(5); 6975877Shsul@eecs.umich.edu 6985877Shsul@eecs.umich.edu if (start == 0) { 6995877Shsul@eecs.umich.edu // user didn't give an address... pick one from our "mmap region" 7005877Shsul@eecs.umich.edu start = p->mmap_end; 7015877Shsul@eecs.umich.edu p->mmap_end += roundUp(length, TheISA::VMPageSize); 7025877Shsul@eecs.umich.edu if (p->nxm_start != 0) { 7031999SN/A //If we have an nxm space, make sure we haven't colided 704378SN/A assert(p->mmap_end < p->nxm_start); 705360SN/A } 7061450SN/A } 7073114Sgblack@eecs.umich.edu 7082680Sktlim@umich.edu if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 709360SN/A warn("allowing mmap of file @ fd %d. " 710360SN/A "This will break if not /dev/zero.", xc->getSyscallArg(4)); 711360SN/A } 7126701Sgblack@eecs.umich.edu 7136701Sgblack@eecs.umich.edu return start; 7146701Sgblack@eecs.umich.edu} 7156701Sgblack@eecs.umich.edu 7166701Sgblack@eecs.umich.edu/// Target getrlimit() handler. 7176701Sgblack@eecs.umich.edutemplate <class OS> 718360SN/ASyscallReturn 7193669Sbinkertn@umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 7203669Sbinkertn@umich.edu ExecContext *xc) 7213669Sbinkertn@umich.edu{ 722360SN/A unsigned resource = xc->getSyscallArg(0); 723360SN/A TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1)); 724360SN/A 725360SN/A switch (resource) { 7262218SN/A case OS::TGT_RLIMIT_STACK: 727360SN/A // max stack size in bytes: make up a number (2MB for now) 7286701Sgblack@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 729360SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 7301458SN/A rlp->rlim_max = htog(rlp->rlim_max); 731360SN/A break; 732360SN/A 733360SN/A default: 7345074Ssaidi@eecs.umich.edu std::cerr << "getrlimitFunc: unimplemented resource " << resource 7355074Ssaidi@eecs.umich.edu << std::endl; 7365074Ssaidi@eecs.umich.edu abort(); 7375074Ssaidi@eecs.umich.edu break; 7385074Ssaidi@eecs.umich.edu } 7395074Ssaidi@eecs.umich.edu 7405074Ssaidi@eecs.umich.edu rlp.copyOut(xc->mem); 7415074Ssaidi@eecs.umich.edu return 0; 7426701Sgblack@eecs.umich.edu} 7436701Sgblack@eecs.umich.edu 7446701Sgblack@eecs.umich.edu/// Target gettimeofday() handler. 7455074Ssaidi@eecs.umich.edutemplate <class OS> 7466701Sgblack@eecs.umich.eduSyscallReturn 7475074Ssaidi@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 7485074Ssaidi@eecs.umich.edu ExecContext *xc) 7495074Ssaidi@eecs.umich.edu{ 7505074Ssaidi@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0)); 7515208Ssaidi@eecs.umich.edu 7525208Ssaidi@eecs.umich.edu getElapsedTime(tp->tv_sec, tp->tv_usec); 7535208Ssaidi@eecs.umich.edu tp->tv_sec += seconds_since_epoch; 7545208Ssaidi@eecs.umich.edu tp->tv_sec = htog(tp->tv_sec); 7555074Ssaidi@eecs.umich.edu tp->tv_usec = htog(tp->tv_usec); 7565074Ssaidi@eecs.umich.edu 7575208Ssaidi@eecs.umich.edu tp.copyOut(xc->mem); 7585074Ssaidi@eecs.umich.edu 7595074Ssaidi@eecs.umich.edu return 0; 7605074Ssaidi@eecs.umich.edu} 7615074Ssaidi@eecs.umich.edu 7626701Sgblack@eecs.umich.edu 7635074Ssaidi@eecs.umich.edu/// Target utimes() handler. 7645074Ssaidi@eecs.umich.edutemplate <class OS> 7655074Ssaidi@eecs.umich.eduSyscallReturn 7665074Ssaidi@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, Process *process, 7675074Ssaidi@eecs.umich.edu ExecContext *xc) 7681999SN/A{ 7691999SN/A std::string path; 7701999SN/A 7713114Sgblack@eecs.umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != NoFault) 7722680Sktlim@umich.edu return -EFAULT; 7731999SN/A 7746701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1)); 7756701Sgblack@eecs.umich.edu tp.copyIn(xc->mem); 7766701Sgblack@eecs.umich.edu 7771999SN/A struct timeval hostTimeval[2]; 7781999SN/A for (int i = 0; i < 2; ++i) 7791999SN/A { 7801999SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 7811999SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 7822764Sstever@eecs.umich.edu } 7832064SN/A int result = utimes(path.c_str(), hostTimeval); 7842064SN/A 7852064SN/A if (result < 0) 7862064SN/A return -errno; 7871999SN/A 7882064SN/A return 0; 7891999SN/A} 7901999SN/A/// Target getrusage() function. 7912218SN/Atemplate <class OS> 7921999SN/ASyscallReturn 7936701Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 7941999SN/A ExecContext *xc) 7951999SN/A{ 7961999SN/A int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN 7971999SN/A TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1)); 7981999SN/A 799378SN/A if (who != OS::TGT_RUSAGE_SELF) { 800360SN/A // don't really handle THREAD or CHILDREN, but just warn and 8011450SN/A // plow ahead 8023114Sgblack@eecs.umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 8032680Sktlim@umich.edu who); 804360SN/A } 805360SN/A 806360SN/A getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 8076701Sgblack@eecs.umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 8086701Sgblack@eecs.umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 8096701Sgblack@eecs.umich.edu 8106701Sgblack@eecs.umich.edu rup->ru_stime.tv_sec = 0; 8116701Sgblack@eecs.umich.edu rup->ru_stime.tv_usec = 0; 8126701Sgblack@eecs.umich.edu rup->ru_maxrss = 0; 813360SN/A rup->ru_ixrss = 0; 8143669Sbinkertn@umich.edu rup->ru_idrss = 0; 8153669Sbinkertn@umich.edu rup->ru_isrss = 0; 8163669Sbinkertn@umich.edu rup->ru_minflt = 0; 817360SN/A rup->ru_majflt = 0; 818360SN/A rup->ru_nswap = 0; 819360SN/A rup->ru_inblock = 0; 820360SN/A rup->ru_oublock = 0; 8211458SN/A rup->ru_msgsnd = 0; 822360SN/A rup->ru_msgrcv = 0; 8236701Sgblack@eecs.umich.edu rup->ru_nsignals = 0; 824360SN/A rup->ru_nvcsw = 0; 8251458SN/A rup->ru_nivcsw = 0; 826360SN/A 827360SN/A rup.copyOut(xc->mem); 8281999SN/A 8291999SN/A return 0; 8301999SN/A} 8313114Sgblack@eecs.umich.edu 8322680Sktlim@umich.edu#endif // __SIM_SYSCALL_EMUL_HH__ 8331999SN/A