syscall_emul.hh revision 2093
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 488229Snate@binkert.org#include "base/intmath.hh" // for RoundUp 498229Snate@binkert.org#include "mem/functional/functional.hh" 503113Sgblack@eecs.umich.edu#include "arch/isa_traits.hh" // for Addr 517075Snate@binkert.org 528229Snate@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 638232Snate@binkert.org /// Typedef for target syscall handler functions. 648229Snate@binkert.org typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 652474SN/A Process *, ExecContext *); 667678Sgblack@eecs.umich.edu 678229Snate@binkert.org const char *name; //!< Syscall name (e.g., "open"). 686640Svince@csl.cornell.edu 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., 76360SN/A /// sigreturn). 77378SN/A SuppressReturnValue = 1 781450SN/A }; 793114Sgblack@eecs.umich.edu 80360SN/A /// Constructor. 815543Ssaidi@eecs.umich.edu SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 825543Ssaidi@eecs.umich.edu : name(_name), funcPtr(_funcPtr), flags(_flags) 835543Ssaidi@eecs.umich.edu { 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); 88360SN/A}; 892680Sktlim@umich.edu 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 100360SN/A // introduce any random junk into the simulated address space 1013114Sgblack@eecs.umich.edu 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 122360SN/A } 1232400SN/A 124360SN/A protected: 1252461SN/A Addr addr; 1265543Ssaidi@eecs.umich.edu int size; 127360SN/A uint8_t *bufPtr; 128360SN/A}; 129360SN/A 130360SN/A 131360SN/Aclass BufferArg : public BaseBufferArg 1322400SN/A{ 133360SN/A public: 1342461SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1355543Ssaidi@eecs.umich.edu 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 { } 148360SN/A 1495543Ssaidi@eecs.umich.edu // 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 166360SN/A 1675543Ssaidi@eecs.umich.edu/// Handler for unimplemented syscalls that we haven't thought about. 1685543Ssaidi@eecs.umich.eduSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 169502SN/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, 180360SN/A Process *p, ExecContext *xc); 181378SN/A 1821706SN/A/// Target getpagesize() handler. 1833114Sgblack@eecs.umich.eduSyscallReturn 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, 188378SN/A Process *p, ExecContext *xc); 1891706SN/A 1903114Sgblack@eecs.umich.edu/// Target close() handler. 1918149SChris.Emmons@ARM.comSyscallReturn closeFunc(SyscallDesc *desc, int num, 1928149SChris.Emmons@ARM.com Process *p, ExecContext *xc); 193360SN/A 1946109Ssanchezd@stanford.edu/// Target read() handler. 1951706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 1963114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 197378SN/A 1986109Ssanchezd@stanford.edu/// Target write() handler. 1996109Ssanchezd@stanford.eduSyscallReturn writeFunc(SyscallDesc *desc, int num, 2006109Ssanchezd@stanford.edu Process *p, ExecContext *xc); 2016109Ssanchezd@stanford.edu 202378SN/A/// Target lseek() handler. 2031706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2043114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 205378SN/A 2065748SSteve.Reinhardt@amd.com/// Target munmap() handler. 2075748SSteve.Reinhardt@amd.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 2085748SSteve.Reinhardt@amd.com Process *p, ExecContext *xc); 209378SN/A 210378SN/A/// Target gethostname() handler. 2111706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2123114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 213378SN/A 214378SN/A/// Target unlink() handler. 2151706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2163114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 217378SN/A 218378SN/A/// Target rename() handler. 2191706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2203114Sgblack@eecs.umich.edu Process *p, ExecContext *xc); 221378SN/A 222378SN/A 2231706SN/A/// Target truncate() handler. 2243114Sgblack@eecs.umich.eduSyscallReturn truncateFunc(SyscallDesc *desc, int num, 225378SN/A Process *p, ExecContext *xc); 2264118Sgblack@eecs.umich.edu 2274118Sgblack@eecs.umich.edu 2284118Sgblack@eecs.umich.edu/// Target ftruncate() handler. 2294118Sgblack@eecs.umich.eduSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 230378SN/A Process *p, ExecContext *xc); 2311706SN/A 2323114Sgblack@eecs.umich.edu 233378SN/A/// Target chown() handler. 234378SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2351706SN/A Process *p, ExecContext *xc); 2363114Sgblack@eecs.umich.edu 237360SN/A 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. 2435513SMichael.Adler@intel.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2445513SMichael.Adler@intel.com Process *process, ExecContext *xc); 2455513SMichael.Adler@intel.com 246511SN/A/// This struct is used to build an target-OS-dependent table that 2471706SN/A/// maps the target's open() flags to the host open() flags. 2483114Sgblack@eecs.umich.edustruct OpenFlagTransTable { 249511SN/A int tgtFlag; //!< Target system flag value. 2505513SMichael.Adler@intel.com int hostFlag; //!< Corresponding host system flag value. 2515513SMichael.Adler@intel.com}; 2525513SMichael.Adler@intel.com 2535513SMichael.Adler@intel.com 254511SN/A 2551706SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 2563114Sgblack@eecs.umich.educonst int one_million = 1000000; 2571706SN/A 2581706SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 2591706SN/A/// by my reckoning. We want to keep this a constant (not use the 2601706SN/A/// real-world time) to keep simulations repeatable. 2613114Sgblack@eecs.umich.educonst unsigned seconds_since_epoch = 1000000000; 2621706SN/A 2631706SN/A/// Helper function to convert current elapsed time to seconds and 2641706SN/A/// microseconds. 2651706SN/Atemplate <class T1, class T2> 2663114Sgblack@eecs.umich.eduvoid 2671706SN/AgetElapsedTime(T1 &sec, T2 &usec) 268511SN/A{ 2696703Svince@csl.cornell.edu int elapsed_usecs = curTick / Clock::Int::us; 2706703Svince@csl.cornell.edu sec = elapsed_usecs / one_million; 2716703Svince@csl.cornell.edu usec = elapsed_usecs % one_million; 2726703Svince@csl.cornell.edu} 2736685Stjones1@inf.ed.ac.uk 2746685Stjones1@inf.ed.ac.uk////////////////////////////////////////////////////////////////////// 2756685Stjones1@inf.ed.ac.uk// 2766685Stjones1@inf.ed.ac.uk// The following emulation functions are generic, but need to be 2776685Stjones1@inf.ed.ac.uk// templated to account for differences in types, constants, etc. 2785513SMichael.Adler@intel.com// 2795513SMichael.Adler@intel.com////////////////////////////////////////////////////////////////////// 2805513SMichael.Adler@intel.com 2815513SMichael.Adler@intel.com/// Target ioctl() handler. For the most part, programs call ioctl() 2825513SMichael.Adler@intel.com/// only to find out if their stdout is a tty, to determine whether to 2831999SN/A/// do line or block buffering. 2841999SN/Atemplate <class OS> 2853114Sgblack@eecs.umich.eduSyscallReturn 2861999SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process, 2871999SN/A ExecContext *xc) 2881999SN/A{ 2891999SN/A int fd = xc->getSyscallArg(0); 2903114Sgblack@eecs.umich.edu unsigned req = xc->getSyscallArg(1); 2911999SN/A 2923079Sstever@eecs.umich.edu DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 2933079Sstever@eecs.umich.edu 2943114Sgblack@eecs.umich.edu if (fd < 0 || process->sim_fd(fd) < 0) { 2953079Sstever@eecs.umich.edu // doesn't map to any simulator fd: not a valid target fd 2962093SN/A return -EBADF; 2972093SN/A } 2983114Sgblack@eecs.umich.edu 2992093SN/A switch (req) { 3002687Sksewell@umich.edu case OS::TIOCISATTY: 3012687Sksewell@umich.edu case OS::TIOCGETP: 3023114Sgblack@eecs.umich.edu case OS::TIOCSETP: 3032687Sksewell@umich.edu case OS::TIOCSETN: 3042238SN/A case OS::TIOCSETC: 3052238SN/A case OS::TIOCGETC: 3063114Sgblack@eecs.umich.edu case OS::TIOCGETS: 3072238SN/A case OS::TIOCGETA: 3082238SN/A return -ENOTTY; 3092238SN/A 3103114Sgblack@eecs.umich.edu default: 3112238SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 3122238SN/A fd, req, xc->readPC()); 3132238SN/A } 3143114Sgblack@eecs.umich.edu} 3152238SN/A 3162238SN/A/// Target open() handler. 3172238SN/Atemplate <class OS> 3183114Sgblack@eecs.umich.eduSyscallReturn 3192238SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process, 3202238SN/A ExecContext *xc) 3212238SN/A{ 3223114Sgblack@eecs.umich.edu std::string path; 3232238SN/A 3242238SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 3252238SN/A return -EFAULT; 3263114Sgblack@eecs.umich.edu 3272238SN/A if (path == "/dev/sysdev0") { 3282238SN/A // This is a memory-mapped high-resolution timer device on Alpha. 3292238SN/A // We don't support it, so just punt. 3303114Sgblack@eecs.umich.edu warn("Ignoring open(%s, ...)\n", path); 3312238SN/A return -ENOENT; 3326109Ssanchezd@stanford.edu } 3336109Ssanchezd@stanford.edu 3346109Ssanchezd@stanford.edu int tgtFlags = xc->getSyscallArg(1); 3352238SN/A int mode = xc->getSyscallArg(2); 3362238SN/A int hostFlags = 0; 3372238SN/A 3382238SN/A // translate open flags 3392238SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 3403114Sgblack@eecs.umich.edu if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 3412238SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 3422238SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 3432238SN/A } 3443114Sgblack@eecs.umich.edu } 3452238SN/A 3462238SN/A // any target flags left? 3472238SN/A if (tgtFlags != 0) 3483114Sgblack@eecs.umich.edu warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 3492238SN/A 3502238SN/A#ifdef __CYGWIN32__ 3512238SN/A hostFlags |= O_BINARY; 3523114Sgblack@eecs.umich.edu#endif 3532238SN/A 3542238SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 3551354SN/A 3561354SN/A // open the file 3571354SN/A int fd = open(path.c_str(), hostFlags, mode); 3581354SN/A 3591354SN/A return (fd == -1) ? -errno : process->alloc_fd(fd); 3601354SN/A} 3611354SN/A 3621354SN/A 3631354SN/A/// Target chmod() handler. 3641354SN/Atemplate <class OS> 3651354SN/ASyscallReturn 3661354SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process, 3671354SN/A ExecContext *xc) 3681354SN/A{ 3697823Ssteve.reinhardt@amd.com std::string path; 3701354SN/A 3711354SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 3721354SN/A return -EFAULT; 3731354SN/A 374360SN/A uint32_t mode = xc->getSyscallArg(1); 375360SN/A mode_t hostMode = 0; 376360SN/A 377360SN/A // XXX translate mode flags via OS::something??? 378360SN/A hostMode = mode; 379360SN/A 380360SN/A // do the chmod 3813113Sgblack@eecs.umich.edu int result = chmod(path.c_str(), hostMode); 3823113Sgblack@eecs.umich.edu if (result < 0) 3833113Sgblack@eecs.umich.edu return errno; 3843113Sgblack@eecs.umich.edu 3853113Sgblack@eecs.umich.edu return 0; 3863113Sgblack@eecs.umich.edu} 3873113Sgblack@eecs.umich.edu 3883113Sgblack@eecs.umich.edu 3893113Sgblack@eecs.umich.edu/// Target fchmod() handler. 3903113Sgblack@eecs.umich.edutemplate <class OS> 3913113Sgblack@eecs.umich.eduSyscallReturn 3923113Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *process, 3933113Sgblack@eecs.umich.edu ExecContext *xc) 3943113Sgblack@eecs.umich.edu{ 3953113Sgblack@eecs.umich.edu int fd = xc->getSyscallArg(0); 3963113Sgblack@eecs.umich.edu if (fd < 0 || process->sim_fd(fd) < 0) { 3974189Sgblack@eecs.umich.edu // doesn't map to any simulator fd: not a valid target fd 3984189Sgblack@eecs.umich.edu return -EBADF; 3993113Sgblack@eecs.umich.edu } 4003113Sgblack@eecs.umich.edu 4013113Sgblack@eecs.umich.edu uint32_t mode = xc->getSyscallArg(1); 4023113Sgblack@eecs.umich.edu mode_t hostMode = 0; 4033113Sgblack@eecs.umich.edu 4043113Sgblack@eecs.umich.edu // XXX translate mode flags via OS::someting??? 4053113Sgblack@eecs.umich.edu hostMode = mode; 4063277Sgblack@eecs.umich.edu 4075515SMichael.Adler@intel.com // do the fchmod 4085515SMichael.Adler@intel.com int result = fchmod(process->sim_fd(fd), hostMode); 4095515SMichael.Adler@intel.com if (result < 0) 4105515SMichael.Adler@intel.com return errno; 4115515SMichael.Adler@intel.com 4123277Sgblack@eecs.umich.edu return 0; 4133277Sgblack@eecs.umich.edu} 4143277Sgblack@eecs.umich.edu 4153277Sgblack@eecs.umich.edu 4163277Sgblack@eecs.umich.edu/// Target stat() handler. 4173277Sgblack@eecs.umich.edutemplate <class OS> 4183277Sgblack@eecs.umich.eduSyscallReturn 4193113Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, Process *process, 4203113Sgblack@eecs.umich.edu ExecContext *xc) 4213113Sgblack@eecs.umich.edu{ 4223113Sgblack@eecs.umich.edu std::string path; 4233113Sgblack@eecs.umich.edu 4243113Sgblack@eecs.umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 4253113Sgblack@eecs.umich.edu return -EFAULT; 4263114Sgblack@eecs.umich.edu 4273113Sgblack@eecs.umich.edu struct stat hostBuf; 4283114Sgblack@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 4293113Sgblack@eecs.umich.edu 4303114Sgblack@eecs.umich.edu if (result < 0) 4313113Sgblack@eecs.umich.edu return errno; 4324061Sgblack@eecs.umich.edu 4334061Sgblack@eecs.umich.edu OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 4344061Sgblack@eecs.umich.edu 4353113Sgblack@eecs.umich.edu return 0; 4363113Sgblack@eecs.umich.edu} 4373113Sgblack@eecs.umich.edu 4383113Sgblack@eecs.umich.edu 4393113Sgblack@eecs.umich.edu/// Target fstat64() handler. 4403113Sgblack@eecs.umich.edutemplate <class OS> 4413113Sgblack@eecs.umich.eduSyscallReturn 4423113Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, Process *process, 4433113Sgblack@eecs.umich.edu ExecContext *xc) 4443113Sgblack@eecs.umich.edu{ 4453113Sgblack@eecs.umich.edu int fd = xc->getSyscallArg(0); 4464189Sgblack@eecs.umich.edu if (fd < 0 || process->sim_fd(fd) < 0) { 4474189Sgblack@eecs.umich.edu // doesn't map to any simulator fd: not a valid target fd 4483113Sgblack@eecs.umich.edu return -EBADF; 4493113Sgblack@eecs.umich.edu } 4503113Sgblack@eecs.umich.edu 4513113Sgblack@eecs.umich.edu#if BSD_HOST 4523113Sgblack@eecs.umich.edu struct stat hostBuf; 4533113Sgblack@eecs.umich.edu int result = fstat(process->sim_fd(fd), &hostBuf); 4543113Sgblack@eecs.umich.edu#else 4553113Sgblack@eecs.umich.edu struct stat64 hostBuf; 4563113Sgblack@eecs.umich.edu int result = fstat64(process->sim_fd(fd), &hostBuf); 4573113Sgblack@eecs.umich.edu#endif 4583113Sgblack@eecs.umich.edu 4593113Sgblack@eecs.umich.edu if (result < 0) 4603113Sgblack@eecs.umich.edu return errno; 4613113Sgblack@eecs.umich.edu 4623113Sgblack@eecs.umich.edu OS::copyOutStat64Buf(xc->mem, fd, xc->getSyscallArg(1), &hostBuf); 4633113Sgblack@eecs.umich.edu 4643113Sgblack@eecs.umich.edu return 0; 4653113Sgblack@eecs.umich.edu} 4663113Sgblack@eecs.umich.edu 4673113Sgblack@eecs.umich.edu 4683113Sgblack@eecs.umich.edu/// Target lstat() handler. 4693113Sgblack@eecs.umich.edutemplate <class OS> 4703113Sgblack@eecs.umich.eduSyscallReturn 4713113Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, Process *process, 4723113Sgblack@eecs.umich.edu ExecContext *xc) 4733113Sgblack@eecs.umich.edu{ 4743113Sgblack@eecs.umich.edu std::string path; 4753113Sgblack@eecs.umich.edu 4763113Sgblack@eecs.umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 4773113Sgblack@eecs.umich.edu return -EFAULT; 4783113Sgblack@eecs.umich.edu 4793113Sgblack@eecs.umich.edu struct stat hostBuf; 4803113Sgblack@eecs.umich.edu int result = lstat(path.c_str(), &hostBuf); 4813113Sgblack@eecs.umich.edu 4826686Stjones1@inf.ed.ac.uk if (result < 0) 4833113Sgblack@eecs.umich.edu return -errno; 4843113Sgblack@eecs.umich.edu 4853113Sgblack@eecs.umich.edu OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 486378SN/A 487378SN/A return 0; 488378SN/A} 489360SN/A 4901450SN/A/// Target lstat64() handler. 4913114Sgblack@eecs.umich.edutemplate <class OS> 4922680Sktlim@umich.eduSyscallReturn 493360SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 4946701Sgblack@eecs.umich.edu ExecContext *xc) 4956701Sgblack@eecs.umich.edu{ 4966701Sgblack@eecs.umich.edu std::string path; 497360SN/A 4981969SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 499360SN/A return -EFAULT; 500360SN/A 501360SN/A#if BSD_HOST 5021458SN/A struct stat hostBuf; 503360SN/A int result = lstat(path.c_str(), &hostBuf); 504360SN/A#else 505360SN/A struct stat64 hostBuf; 5064131Sbinkertn@umich.edu int result = lstat64(path.c_str(), &hostBuf); 5074131Sbinkertn@umich.edu#endif 5084131Sbinkertn@umich.edu 5094131Sbinkertn@umich.edu if (result < 0) 5104131Sbinkertn@umich.edu return -errno; 5114131Sbinkertn@umich.edu 5124131Sbinkertn@umich.edu OS::copyOutStat64Buf(xc->mem, -1, xc->getSyscallArg(1), &hostBuf); 5134131Sbinkertn@umich.edu 5146689Stjones1@inf.ed.ac.uk return 0; 5151458SN/A} 516360SN/A 517360SN/A/// Target fstat() handler. 5187720Sgblack@eecs.umich.edutemplate <class OS> 5197720Sgblack@eecs.umich.eduSyscallReturn 520360SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *process, 521360SN/A ExecContext *xc) 522360SN/A{ 523378SN/A int fd = process->sim_fd(xc->getSyscallArg(0)); 524360SN/A 5251450SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 5263114Sgblack@eecs.umich.edu 5272680Sktlim@umich.edu if (fd < 0) 528360SN/A return -EBADF; 529360SN/A 530360SN/A struct stat hostBuf; 5316701Sgblack@eecs.umich.edu int result = fstat(fd, &hostBuf); 5326701Sgblack@eecs.umich.edu 5336701Sgblack@eecs.umich.edu if (result < 0) 5341458SN/A return -errno; 535360SN/A 536360SN/A OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 537360SN/A return 0; 538360SN/A} 5391706SN/A 5401458SN/A 541360SN/A/// Target statfs() handler. 542360SN/Atemplate <class OS> 5436701Sgblack@eecs.umich.eduSyscallReturn 5446701Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, Process *process, 545360SN/A ExecContext *xc) 546360SN/A{ 547360SN/A std::string path; 548360SN/A 549360SN/A if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 550360SN/A return -EFAULT; 551360SN/A 552360SN/A struct statfs hostBuf; 553360SN/A int result = statfs(path.c_str(), &hostBuf); 554360SN/A 555360SN/A if (result < 0) 556360SN/A return errno; 5571706SN/A 558360SN/A OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 559360SN/A 560360SN/A return 0; 561360SN/A} 562360SN/A 5633669Sbinkertn@umich.edu 5643669Sbinkertn@umich.edu/// Target fstatfs() handler. 5653669Sbinkertn@umich.edutemplate <class OS> 5661706SN/ASyscallReturn 5671706SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 5685795Ssaidi@eecs.umich.edu ExecContext *xc) 5695795Ssaidi@eecs.umich.edu{ 5705795Ssaidi@eecs.umich.edu int fd = process->sim_fd(xc->getSyscallArg(0)); 5715795Ssaidi@eecs.umich.edu 5725795Ssaidi@eecs.umich.edu if (fd < 0) 5735795Ssaidi@eecs.umich.edu return -EBADF; 5745795Ssaidi@eecs.umich.edu 5755795Ssaidi@eecs.umich.edu struct statfs hostBuf; 5765795Ssaidi@eecs.umich.edu int result = fstatfs(fd, &hostBuf); 5775795Ssaidi@eecs.umich.edu 5785795Ssaidi@eecs.umich.edu if (result < 0) 579360SN/A return errno; 580360SN/A 581360SN/A OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf); 5826640Svince@csl.cornell.edu 5836640Svince@csl.cornell.edu return 0; 5846640Svince@csl.cornell.edu} 5856640Svince@csl.cornell.edu 5866640Svince@csl.cornell.edu 5876640Svince@csl.cornell.edu/// Target writev() handler. 5886640Svince@csl.cornell.edutemplate <class OS> 5896701Sgblack@eecs.umich.eduSyscallReturn 5906701Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, Process *process, 5916701Sgblack@eecs.umich.edu ExecContext *xc) 5926640Svince@csl.cornell.edu{ 5936701Sgblack@eecs.umich.edu int fd = xc->getSyscallArg(0); 5946701Sgblack@eecs.umich.edu if (fd < 0 || process->sim_fd(fd) < 0) { 5956640Svince@csl.cornell.edu // doesn't map to any simulator fd: not a valid target fd 5966701Sgblack@eecs.umich.edu return -EBADF; 5976640Svince@csl.cornell.edu } 5986701Sgblack@eecs.umich.edu 5996640Svince@csl.cornell.edu uint64_t tiov_base = xc->getSyscallArg(1); 600360SN/A size_t count = xc->getSyscallArg(2); 6011999SN/A struct iovec hiov[count]; 6021999SN/A for (int i = 0; i < count; ++i) 6031999SN/A { 6043114Sgblack@eecs.umich.edu typename OS::tgt_iovec tiov; 6052680Sktlim@umich.edu xc->mem->access(Read, tiov_base + i*sizeof(typename OS::tgt_iovec), 6061999SN/A &tiov, sizeof(typename OS::tgt_iovec)); 6071999SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 6081999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 6096701Sgblack@eecs.umich.edu xc->mem->access(Read, gtoh(tiov.iov_base), 6106701Sgblack@eecs.umich.edu hiov[i].iov_base, hiov[i].iov_len); 6116701Sgblack@eecs.umich.edu } 6121999SN/A 6136701Sgblack@eecs.umich.edu int result = writev(process->sim_fd(fd), hiov, count); 6141999SN/A 6156701Sgblack@eecs.umich.edu for (int i = 0; i < count; ++i) 6161999SN/A { 6171999SN/A delete [] (char *)hiov[i].iov_base; 6181999SN/A } 6191999SN/A 6201999SN/A if (result < 0) 6213669Sbinkertn@umich.edu return errno; 6223669Sbinkertn@umich.edu 6233669Sbinkertn@umich.edu return 0; 6241999SN/A} 6251999SN/A 6261999SN/A 6272218SN/A/// Target mmap() handler. 6281999SN/A/// 6291999SN/A/// We don't really handle mmap(). If the target is mmaping an 6301999SN/A/// anonymous region or /dev/zero, we can get away with doing basically 6311999SN/A/// nothing (since memory is initialized to zero and the simulator 6321999SN/A/// doesn't really check addresses anyway). Always print a warning, 6331999SN/A/// since this could be seriously broken if we're not mapping 6341999SN/A/// /dev/zero. 6351999SN/A// 6363114Sgblack@eecs.umich.edu/// Someday we should explicitly check for /dev/zero in open, flag the 6372680Sktlim@umich.edu/// file descriptor, and fail (or implement!) a non-anonymous mmap to 6381999SN/A/// anything else. 6396701Sgblack@eecs.umich.edutemplate <class OS> 6406701Sgblack@eecs.umich.eduSyscallReturn 6411999SN/AmmapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc) 6421999SN/A{ 6431999SN/A Addr start = xc->getSyscallArg(0); 6441999SN/A uint64_t length = xc->getSyscallArg(1); 6451999SN/A // int prot = xc->getSyscallArg(2); 6466701Sgblack@eecs.umich.edu int flags = xc->getSyscallArg(3); 6471999SN/A // int fd = p->sim_fd(xc->getSyscallArg(4)); 6481999SN/A // int offset = xc->getSyscallArg(5); 6491999SN/A 6501999SN/A if (start == 0) { 6511999SN/A // user didn't give an address... pick one from our "mmap region" 6521999SN/A start = p->mmap_end; 6531999SN/A p->mmap_end += roundUp(length, VMPageSize); 6541999SN/A if (p->nxm_start != 0) { 6552218SN/A //If we have an nxm space, make sure we haven't colided 6561999SN/A assert(p->mmap_end < p->nxm_start); 6571999SN/A } 6581999SN/A } 6591999SN/A 6605877Shsul@eecs.umich.edu if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 6615877Shsul@eecs.umich.edu warn("allowing mmap of file @ fd %d. " 6625877Shsul@eecs.umich.edu "This will break if not /dev/zero.", xc->getSyscallArg(4)); 6635877Shsul@eecs.umich.edu } 6645877Shsul@eecs.umich.edu 6656701Sgblack@eecs.umich.edu return start; 6666701Sgblack@eecs.umich.edu} 6676701Sgblack@eecs.umich.edu 6686701Sgblack@eecs.umich.edu/// Target getrlimit() handler. 6696701Sgblack@eecs.umich.edutemplate <class OS> 6705877Shsul@eecs.umich.eduSyscallReturn 6715877Shsul@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 6725877Shsul@eecs.umich.edu ExecContext *xc) 6735877Shsul@eecs.umich.edu{ 6745877Shsul@eecs.umich.edu unsigned resource = xc->getSyscallArg(0); 6755877Shsul@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1)); 6765877Shsul@eecs.umich.edu 6775877Shsul@eecs.umich.edu switch (resource) { 6785877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_STACK: 6795877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (2MB for now) 6808601Ssteve.reinhardt@amd.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 6815877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 6825877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 6835877Shsul@eecs.umich.edu break; 6845877Shsul@eecs.umich.edu 6855877Shsul@eecs.umich.edu default: 6865877Shsul@eecs.umich.edu std::cerr << "getrlimitFunc: unimplemented resource " << resource 6875877Shsul@eecs.umich.edu << std::endl; 6885877Shsul@eecs.umich.edu abort(); 6895877Shsul@eecs.umich.edu break; 6905877Shsul@eecs.umich.edu } 6915877Shsul@eecs.umich.edu 6925877Shsul@eecs.umich.edu rlp.copyOut(xc->mem); 6935877Shsul@eecs.umich.edu return 0; 6948601Ssteve.reinhardt@amd.com} 6958601Ssteve.reinhardt@amd.com 6965877Shsul@eecs.umich.edu/// Target gettimeofday() handler. 6975877Shsul@eecs.umich.edutemplate <class OS> 6985877Shsul@eecs.umich.eduSyscallReturn 6995877Shsul@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 7005877Shsul@eecs.umich.edu ExecContext *xc) 7015877Shsul@eecs.umich.edu{ 7028601Ssteve.reinhardt@amd.com TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0)); 7035877Shsul@eecs.umich.edu 7045877Shsul@eecs.umich.edu getElapsedTime(tp->tv_sec, tp->tv_usec); 7055877Shsul@eecs.umich.edu tp->tv_sec += seconds_since_epoch; 7061999SN/A tp->tv_sec = htog(tp->tv_sec); 707378SN/A tp->tv_usec = htog(tp->tv_usec); 708360SN/A 7091450SN/A tp.copyOut(xc->mem); 7103114Sgblack@eecs.umich.edu 7112680Sktlim@umich.edu return 0; 712360SN/A} 713360SN/A 714360SN/A 7156701Sgblack@eecs.umich.edu/// Target utimes() handler. 7166701Sgblack@eecs.umich.edutemplate <class OS> 7176701Sgblack@eecs.umich.eduSyscallReturn 7186701Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, Process *process, 7196701Sgblack@eecs.umich.edu ExecContext *xc) 7206701Sgblack@eecs.umich.edu{ 721360SN/A std::string path; 7223669Sbinkertn@umich.edu 7233669Sbinkertn@umich.edu if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault) 7243669Sbinkertn@umich.edu return -EFAULT; 725360SN/A 726360SN/A TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1)); 727360SN/A tp.copyIn(xc->mem); 728360SN/A 7292218SN/A struct timeval hostTimeval[2]; 730360SN/A for (int i = 0; i < 2; ++i) 7316701Sgblack@eecs.umich.edu { 732360SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 7331458SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 734360SN/A } 735360SN/A int result = utimes(path.c_str(), hostTimeval); 736360SN/A 7375074Ssaidi@eecs.umich.edu if (result < 0) 7385074Ssaidi@eecs.umich.edu return -errno; 7395074Ssaidi@eecs.umich.edu 7405074Ssaidi@eecs.umich.edu return 0; 7415074Ssaidi@eecs.umich.edu} 7425074Ssaidi@eecs.umich.edu/// Target getrusage() function. 7435074Ssaidi@eecs.umich.edutemplate <class OS> 7445074Ssaidi@eecs.umich.eduSyscallReturn 7456701Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 7466701Sgblack@eecs.umich.edu ExecContext *xc) 7476701Sgblack@eecs.umich.edu{ 7485074Ssaidi@eecs.umich.edu int who = xc->getSyscallArg(0); // THREAD, SELF, or CHILDREN 7496701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1)); 7505074Ssaidi@eecs.umich.edu 7515074Ssaidi@eecs.umich.edu if (who != OS::TGT_RUSAGE_SELF) { 7525074Ssaidi@eecs.umich.edu // don't really handle THREAD or CHILDREN, but just warn and 7535074Ssaidi@eecs.umich.edu // plow ahead 7545208Ssaidi@eecs.umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 7555208Ssaidi@eecs.umich.edu who); 7565208Ssaidi@eecs.umich.edu } 7575208Ssaidi@eecs.umich.edu 7585074Ssaidi@eecs.umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 7595074Ssaidi@eecs.umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 7605208Ssaidi@eecs.umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 7615074Ssaidi@eecs.umich.edu 7625074Ssaidi@eecs.umich.edu rup->ru_stime.tv_sec = 0; 7635074Ssaidi@eecs.umich.edu rup->ru_stime.tv_usec = 0; 7645074Ssaidi@eecs.umich.edu rup->ru_maxrss = 0; 7656701Sgblack@eecs.umich.edu rup->ru_ixrss = 0; 7665074Ssaidi@eecs.umich.edu rup->ru_idrss = 0; 7675074Ssaidi@eecs.umich.edu rup->ru_isrss = 0; 7685074Ssaidi@eecs.umich.edu rup->ru_minflt = 0; 7695074Ssaidi@eecs.umich.edu rup->ru_majflt = 0; 7705074Ssaidi@eecs.umich.edu rup->ru_nswap = 0; 7711999SN/A rup->ru_inblock = 0; 7721999SN/A rup->ru_oublock = 0; 7731999SN/A rup->ru_msgsnd = 0; 7743114Sgblack@eecs.umich.edu rup->ru_msgrcv = 0; 7752680Sktlim@umich.edu rup->ru_nsignals = 0; 7761999SN/A rup->ru_nvcsw = 0; 7776701Sgblack@eecs.umich.edu rup->ru_nivcsw = 0; 7786701Sgblack@eecs.umich.edu 7796701Sgblack@eecs.umich.edu rup.copyOut(xc->mem); 7801999SN/A 7811999SN/A return 0; 7821999SN/A} 7831999SN/A 7841999SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 7852764Sstever@eecs.umich.edu