syscall_emul.hh revision 2687
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 2522687Sksewell@umich.edu/// Target fcntl64() handler. 2532687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2542687Sksewell@umich.edu Process *process, ThreadContext *tc); 2552687Sksewell@umich.edu 2562238SN/A/// Target setuid() handler. 2572238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2582680Sktlim@umich.edu Process *p, ThreadContext *tc); 2592238SN/A 2602238SN/A/// Target getpid() handler. 2612238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 2622680Sktlim@umich.edu Process *p, ThreadContext *tc); 2632238SN/A 2642238SN/A/// Target getuid() handler. 2652238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 2662680Sktlim@umich.edu Process *p, ThreadContext *tc); 2672238SN/A 2682238SN/A/// Target getgid() handler. 2692238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 2702680Sktlim@umich.edu Process *p, ThreadContext *tc); 2712238SN/A 2722238SN/A/// Target getppid() handler. 2732238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 2742680Sktlim@umich.edu Process *p, ThreadContext *tc); 2752238SN/A 2762238SN/A/// Target geteuid() handler. 2772238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2782680Sktlim@umich.edu Process *p, ThreadContext *tc); 2792238SN/A 2802238SN/A/// Target getegid() handler. 2812238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 2822680Sktlim@umich.edu Process *p, ThreadContext *tc); 2832238SN/A 2842238SN/A 2852238SN/A 2862238SN/A/// Pseudo Funcs - These functions use a different return convension, 2872238SN/A/// returning a second value in a register other than the normal return register 2882238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 2892680Sktlim@umich.edu Process *process, ThreadContext *tc); 2902238SN/A 2912238SN/A/// Target getpidPseudo() handler. 2922238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 2932680Sktlim@umich.edu Process *p, ThreadContext *tc); 2942238SN/A 2952238SN/A/// Target getuidPseudo() handler. 2962238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 2972680Sktlim@umich.edu Process *p, ThreadContext *tc); 2982238SN/A 2992238SN/A/// Target getgidPseudo() handler. 3002238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3012680Sktlim@umich.edu Process *p, ThreadContext *tc); 3022238SN/A 3032238SN/A 3041354SN/A/// This struct is used to build an target-OS-dependent table that 3051354SN/A/// maps the target's open() flags to the host open() flags. 3061354SN/Astruct OpenFlagTransTable { 3071354SN/A int tgtFlag; //!< Target system flag value. 3081354SN/A int hostFlag; //!< Corresponding host system flag value. 3091354SN/A}; 3101354SN/A 3111354SN/A 3121354SN/A 3131354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3141354SN/Aconst int one_million = 1000000; 3151354SN/A 3161354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3171354SN/A/// by my reckoning. We want to keep this a constant (not use the 3181354SN/A/// real-world time) to keep simulations repeatable. 3191354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3201354SN/A 3211354SN/A/// Helper function to convert current elapsed time to seconds and 3221354SN/A/// microseconds. 3231354SN/Atemplate <class T1, class T2> 3241354SN/Avoid 3251354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3261354SN/A{ 3271609SN/A int elapsed_usecs = curTick / Clock::Int::us; 3281354SN/A sec = elapsed_usecs / one_million; 3291354SN/A usec = elapsed_usecs % one_million; 3301354SN/A} 3311354SN/A 332360SN/A////////////////////////////////////////////////////////////////////// 333360SN/A// 334360SN/A// The following emulation functions are generic, but need to be 335360SN/A// templated to account for differences in types, constants, etc. 336360SN/A// 337360SN/A////////////////////////////////////////////////////////////////////// 338360SN/A 339378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 340378SN/A/// only to find out if their stdout is a tty, to determine whether to 341378SN/A/// do line or block buffering. 342360SN/Atemplate <class OS> 3431450SN/ASyscallReturn 344360SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process, 3452680Sktlim@umich.edu ThreadContext *tc) 346360SN/A{ 3472680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 3482680Sktlim@umich.edu unsigned req = tc->getSyscallArg(1); 349360SN/A 3501969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 351360SN/A 352360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 353360SN/A // doesn't map to any simulator fd: not a valid target fd 3541458SN/A return -EBADF; 355360SN/A } 356360SN/A 357360SN/A switch (req) { 3582553SN/A case OS::TIOCISATTY: 3592553SN/A case OS::TIOCGETP: 3602553SN/A case OS::TIOCSETP: 3612553SN/A case OS::TIOCSETN: 3622553SN/A case OS::TIOCSETC: 3632553SN/A case OS::TIOCGETC: 3642553SN/A case OS::TIOCGETS: 3652553SN/A case OS::TIOCGETA: 3661458SN/A return -ENOTTY; 367360SN/A 368360SN/A default: 3691706SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 3702680Sktlim@umich.edu fd, req, tc->readPC()); 371360SN/A } 372360SN/A} 373360SN/A 374378SN/A/// Target open() handler. 375360SN/Atemplate <class OS> 3761450SN/ASyscallReturn 377360SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process, 3782680Sktlim@umich.edu ThreadContext *tc) 379360SN/A{ 380360SN/A std::string path; 381360SN/A 3822680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 3831458SN/A return -EFAULT; 384360SN/A 385360SN/A if (path == "/dev/sysdev0") { 386360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 387360SN/A // We don't support it, so just punt. 3881706SN/A warn("Ignoring open(%s, ...)\n", path); 3891458SN/A return -ENOENT; 390360SN/A } 391360SN/A 3922680Sktlim@umich.edu int tgtFlags = tc->getSyscallArg(1); 3932680Sktlim@umich.edu int mode = tc->getSyscallArg(2); 394360SN/A int hostFlags = 0; 395360SN/A 396360SN/A // translate open flags 397360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 398360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 399360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 400360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 401360SN/A } 402360SN/A } 403360SN/A 404360SN/A // any target flags left? 405360SN/A if (tgtFlags != 0) 4061706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 407360SN/A 408360SN/A#ifdef __CYGWIN32__ 409360SN/A hostFlags |= O_BINARY; 410360SN/A#endif 411360SN/A 4121706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 4131706SN/A 414360SN/A // open the file 415360SN/A int fd = open(path.c_str(), hostFlags, mode); 416360SN/A 4171970SN/A return (fd == -1) ? -errno : process->alloc_fd(fd); 418360SN/A} 419360SN/A 420360SN/A 4211999SN/A/// Target chmod() handler. 4221999SN/Atemplate <class OS> 4231999SN/ASyscallReturn 4241999SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process, 4252680Sktlim@umich.edu ThreadContext *tc) 4261999SN/A{ 4271999SN/A std::string path; 4281999SN/A 4292680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 4301999SN/A return -EFAULT; 4311999SN/A 4322680Sktlim@umich.edu uint32_t mode = tc->getSyscallArg(1); 4331999SN/A mode_t hostMode = 0; 4341999SN/A 4351999SN/A // XXX translate mode flags via OS::something??? 4361999SN/A hostMode = mode; 4371999SN/A 4381999SN/A // do the chmod 4391999SN/A int result = chmod(path.c_str(), hostMode); 4401999SN/A if (result < 0) 4412218SN/A return -errno; 4421999SN/A 4431999SN/A return 0; 4441999SN/A} 4451999SN/A 4461999SN/A 4471999SN/A/// Target fchmod() handler. 4481999SN/Atemplate <class OS> 4491999SN/ASyscallReturn 4501999SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *process, 4512680Sktlim@umich.edu ThreadContext *tc) 4521999SN/A{ 4532680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 4541999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 4551999SN/A // doesn't map to any simulator fd: not a valid target fd 4561999SN/A return -EBADF; 4571999SN/A } 4581999SN/A 4592680Sktlim@umich.edu uint32_t mode = tc->getSyscallArg(1); 4601999SN/A mode_t hostMode = 0; 4611999SN/A 4621999SN/A // XXX translate mode flags via OS::someting??? 4631999SN/A hostMode = mode; 4641999SN/A 4651999SN/A // do the fchmod 4661999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 4671999SN/A if (result < 0) 4682218SN/A return -errno; 4691999SN/A 4701999SN/A return 0; 4711999SN/A} 4721999SN/A 4731999SN/A 474378SN/A/// Target stat() handler. 475360SN/Atemplate <class OS> 4761450SN/ASyscallReturn 477360SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process, 4782680Sktlim@umich.edu ThreadContext *tc) 479360SN/A{ 480360SN/A std::string path; 481360SN/A 4822680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 4832400SN/A return -EFAULT; 484360SN/A 485360SN/A struct stat hostBuf; 486360SN/A int result = stat(path.c_str(), &hostBuf); 487360SN/A 488360SN/A if (result < 0) 4892218SN/A return -errno; 490360SN/A 4912680Sktlim@umich.edu OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 492360SN/A 4931458SN/A return 0; 494360SN/A} 495360SN/A 496360SN/A 4971999SN/A/// Target fstat64() handler. 4981999SN/Atemplate <class OS> 4991999SN/ASyscallReturn 5001999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *process, 5012680Sktlim@umich.edu ThreadContext *tc) 5021999SN/A{ 5032680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 5041999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 5051999SN/A // doesn't map to any simulator fd: not a valid target fd 5061999SN/A return -EBADF; 5071999SN/A } 5081999SN/A 5092067SN/A#if BSD_HOST 5102064SN/A struct stat hostBuf; 5112064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 5122064SN/A#else 5132064SN/A struct stat64 hostBuf; 5141999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 5152064SN/A#endif 5161999SN/A 5171999SN/A if (result < 0) 5182218SN/A return -errno; 5191999SN/A 5202680Sktlim@umich.edu OS::copyOutStat64Buf(tc->getMemPort(), fd, tc->getSyscallArg(1), &hostBuf); 5211999SN/A 5221999SN/A return 0; 5231999SN/A} 5241999SN/A 5251999SN/A 526378SN/A/// Target lstat() handler. 527360SN/Atemplate <class OS> 5281450SN/ASyscallReturn 529360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process, 5302680Sktlim@umich.edu ThreadContext *tc) 531360SN/A{ 532360SN/A std::string path; 533360SN/A 5342680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 5352400SN/A return -EFAULT; 536360SN/A 537360SN/A struct stat hostBuf; 538360SN/A int result = lstat(path.c_str(), &hostBuf); 539360SN/A 540360SN/A if (result < 0) 5411458SN/A return -errno; 542360SN/A 5432680Sktlim@umich.edu OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 544360SN/A 5451458SN/A return 0; 546360SN/A} 547360SN/A 5481999SN/A/// Target lstat64() handler. 5491999SN/Atemplate <class OS> 5501999SN/ASyscallReturn 5511999SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 5522680Sktlim@umich.edu ThreadContext *tc) 5531999SN/A{ 5541999SN/A std::string path; 5551999SN/A 5562680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 5572400SN/A return -EFAULT; 5581999SN/A 5592067SN/A#if BSD_HOST 5602064SN/A struct stat hostBuf; 5612064SN/A int result = lstat(path.c_str(), &hostBuf); 5622064SN/A#else 5631999SN/A struct stat64 hostBuf; 5641999SN/A int result = lstat64(path.c_str(), &hostBuf); 5652064SN/A#endif 5661999SN/A 5671999SN/A if (result < 0) 5681999SN/A return -errno; 5691999SN/A 5702680Sktlim@umich.edu OS::copyOutStat64Buf(tc->getMemPort(), -1, tc->getSyscallArg(1), &hostBuf); 5711999SN/A 5721999SN/A return 0; 5731999SN/A} 5741999SN/A 575378SN/A/// Target fstat() handler. 576360SN/Atemplate <class OS> 5771450SN/ASyscallReturn 578360SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *process, 5792680Sktlim@umich.edu ThreadContext *tc) 580360SN/A{ 5812680Sktlim@umich.edu int fd = process->sim_fd(tc->getSyscallArg(0)); 582360SN/A 5831969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 584360SN/A 585360SN/A if (fd < 0) 5861458SN/A return -EBADF; 587360SN/A 588360SN/A struct stat hostBuf; 589360SN/A int result = fstat(fd, &hostBuf); 590360SN/A 591360SN/A if (result < 0) 5921458SN/A return -errno; 593360SN/A 5942680Sktlim@umich.edu OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 5952021SN/A 5961458SN/A return 0; 597360SN/A} 598360SN/A 599360SN/A 6001706SN/A/// Target statfs() handler. 6011706SN/Atemplate <class OS> 6021706SN/ASyscallReturn 6031706SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 6042680Sktlim@umich.edu ThreadContext *tc) 6051706SN/A{ 6061706SN/A std::string path; 6071706SN/A 6082680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 6092400SN/A return -EFAULT; 6101706SN/A 6111706SN/A struct statfs hostBuf; 6121706SN/A int result = statfs(path.c_str(), &hostBuf); 6131706SN/A 6141706SN/A if (result < 0) 6152218SN/A return -errno; 6161706SN/A 6172680Sktlim@umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 6181706SN/A 6191706SN/A return 0; 6201706SN/A} 6211706SN/A 6221706SN/A 6231706SN/A/// Target fstatfs() handler. 6241706SN/Atemplate <class OS> 6251706SN/ASyscallReturn 6261706SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 6272680Sktlim@umich.edu ThreadContext *tc) 6281706SN/A{ 6292680Sktlim@umich.edu int fd = process->sim_fd(tc->getSyscallArg(0)); 6301706SN/A 6311706SN/A if (fd < 0) 6321706SN/A return -EBADF; 6331706SN/A 6341706SN/A struct statfs hostBuf; 6351706SN/A int result = fstatfs(fd, &hostBuf); 6361706SN/A 6371706SN/A if (result < 0) 6382218SN/A return -errno; 6391706SN/A 6402680Sktlim@umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf); 6411706SN/A 6421706SN/A return 0; 6431706SN/A} 6441706SN/A 6451706SN/A 6461999SN/A/// Target writev() handler. 6471999SN/Atemplate <class OS> 6481999SN/ASyscallReturn 6491999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process, 6502680Sktlim@umich.edu ThreadContext *tc) 6511999SN/A{ 6522680Sktlim@umich.edu int fd = tc->getSyscallArg(0); 6531999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6541999SN/A // doesn't map to any simulator fd: not a valid target fd 6551999SN/A return -EBADF; 6561999SN/A } 6571999SN/A 6582680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 6592680Sktlim@umich.edu uint64_t tiov_base = tc->getSyscallArg(1); 6602680Sktlim@umich.edu size_t count = tc->getSyscallArg(2); 6611999SN/A struct iovec hiov[count]; 6621999SN/A for (int i = 0; i < count; ++i) 6631999SN/A { 6641999SN/A typename OS::tgt_iovec tiov; 6652461SN/A 6662461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 6672461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 6682091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 6691999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 6702461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 6712461SN/A hiov[i].iov_len); 6721999SN/A } 6731999SN/A 6741999SN/A int result = writev(process->sim_fd(fd), hiov, count); 6751999SN/A 6761999SN/A for (int i = 0; i < count; ++i) 6771999SN/A { 6781999SN/A delete [] (char *)hiov[i].iov_base; 6791999SN/A } 6801999SN/A 6811999SN/A if (result < 0) 6822218SN/A return -errno; 6831999SN/A 6841999SN/A return 0; 6851999SN/A} 6861999SN/A 6871999SN/A 688378SN/A/// Target mmap() handler. 689378SN/A/// 690378SN/A/// We don't really handle mmap(). If the target is mmaping an 691378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 692378SN/A/// nothing (since memory is initialized to zero and the simulator 693378SN/A/// doesn't really check addresses anyway). Always print a warning, 694378SN/A/// since this could be seriously broken if we're not mapping 695378SN/A/// /dev/zero. 696360SN/A// 697378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 698378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 699378SN/A/// anything else. 700360SN/Atemplate <class OS> 7011450SN/ASyscallReturn 7022680Sktlim@umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 703360SN/A{ 7042680Sktlim@umich.edu Addr start = tc->getSyscallArg(0); 7052680Sktlim@umich.edu uint64_t length = tc->getSyscallArg(1); 7062680Sktlim@umich.edu // int prot = tc->getSyscallArg(2); 7072680Sktlim@umich.edu int flags = tc->getSyscallArg(3); 7082680Sktlim@umich.edu // int fd = p->sim_fd(tc->getSyscallArg(4)); 7092680Sktlim@umich.edu // int offset = tc->getSyscallArg(5); 710360SN/A 7112544SN/A if ((start % TheISA::VMPageSize) != 0 || 7122544SN/A (length % TheISA::VMPageSize) != 0) { 7132544SN/A warn("mmap failing: arguments not page-aligned: " 7142544SN/A "start 0x%x length 0x%x", 7152544SN/A start, length); 7162544SN/A return -EINVAL; 717360SN/A } 718360SN/A 7192544SN/A if (start != 0) { 7202544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 7212544SN/A start, p->mmap_end); 7222544SN/A } 7232544SN/A 7242544SN/A // pick next address from our "mmap region" 7252544SN/A start = p->mmap_end; 7262544SN/A p->pTable->allocate(start, length); 7272544SN/A p->mmap_end += length; 7282544SN/A 7292553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 7301969SN/A warn("allowing mmap of file @ fd %d. " 7312680Sktlim@umich.edu "This will break if not /dev/zero.", tc->getSyscallArg(4)); 732360SN/A } 733360SN/A 7341458SN/A return start; 735360SN/A} 736360SN/A 737378SN/A/// Target getrlimit() handler. 738360SN/Atemplate <class OS> 7391450SN/ASyscallReturn 740360SN/AgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 7412680Sktlim@umich.edu ThreadContext *tc) 742360SN/A{ 7432680Sktlim@umich.edu unsigned resource = tc->getSyscallArg(0); 7442680Sktlim@umich.edu TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1)); 745360SN/A 746360SN/A switch (resource) { 7472064SN/A case OS::TGT_RLIMIT_STACK: 7482064SN/A // max stack size in bytes: make up a number (2MB for now) 7492064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 7502091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 7512091SN/A rlp->rlim_max = htog(rlp->rlim_max); 7522064SN/A break; 753360SN/A 7542064SN/A default: 7552064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 7562064SN/A << std::endl; 7572064SN/A abort(); 7582064SN/A break; 759360SN/A } 760360SN/A 7612680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 7621458SN/A return 0; 763360SN/A} 764360SN/A 765378SN/A/// Target gettimeofday() handler. 766360SN/Atemplate <class OS> 7671450SN/ASyscallReturn 768360SN/AgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 7692680Sktlim@umich.edu ThreadContext *tc) 770360SN/A{ 7712680Sktlim@umich.edu TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0)); 772360SN/A 773360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 774360SN/A tp->tv_sec += seconds_since_epoch; 7752091SN/A tp->tv_sec = htog(tp->tv_sec); 7762091SN/A tp->tv_usec = htog(tp->tv_usec); 777360SN/A 7782680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 779360SN/A 7801458SN/A return 0; 781360SN/A} 782360SN/A 783360SN/A 7841999SN/A/// Target utimes() handler. 7851999SN/Atemplate <class OS> 7861999SN/ASyscallReturn 7871999SN/AutimesFunc(SyscallDesc *desc, int callnum, Process *process, 7882680Sktlim@umich.edu ThreadContext *tc) 7891999SN/A{ 7901999SN/A std::string path; 7911999SN/A 7922680Sktlim@umich.edu if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0))) 7932400SN/A return -EFAULT; 7941999SN/A 7952680Sktlim@umich.edu TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1)); 7962680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 7971999SN/A 7981999SN/A struct timeval hostTimeval[2]; 7991999SN/A for (int i = 0; i < 2; ++i) 8001999SN/A { 8012091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 8022091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 8031999SN/A } 8041999SN/A int result = utimes(path.c_str(), hostTimeval); 8051999SN/A 8061999SN/A if (result < 0) 8071999SN/A return -errno; 8081999SN/A 8091999SN/A return 0; 8101999SN/A} 811378SN/A/// Target getrusage() function. 812360SN/Atemplate <class OS> 8131450SN/ASyscallReturn 814360SN/AgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 8152680Sktlim@umich.edu ThreadContext *tc) 816360SN/A{ 8172680Sktlim@umich.edu int who = tc->getSyscallArg(0); // THREAD, SELF, or CHILDREN 8182680Sktlim@umich.edu TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1)); 819360SN/A 8202553SN/A if (who != OS::TGT_RUSAGE_SELF) { 821360SN/A // don't really handle THREAD or CHILDREN, but just warn and 822360SN/A // plow ahead 8231969SN/A warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 8241969SN/A who); 825360SN/A } 826360SN/A 827360SN/A getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 8282091SN/A rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 8292091SN/A rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 8302091SN/A 831360SN/A rup->ru_stime.tv_sec = 0; 832360SN/A rup->ru_stime.tv_usec = 0; 833360SN/A rup->ru_maxrss = 0; 834360SN/A rup->ru_ixrss = 0; 835360SN/A rup->ru_idrss = 0; 836360SN/A rup->ru_isrss = 0; 837360SN/A rup->ru_minflt = 0; 838360SN/A rup->ru_majflt = 0; 839360SN/A rup->ru_nswap = 0; 840360SN/A rup->ru_inblock = 0; 841360SN/A rup->ru_oublock = 0; 842360SN/A rup->ru_msgsnd = 0; 843360SN/A rup->ru_msgrcv = 0; 844360SN/A rup->ru_nsignals = 0; 845360SN/A rup->ru_nvcsw = 0; 846360SN/A rup->ru_nivcsw = 0; 847360SN/A 8482680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 849360SN/A 8501458SN/A return 0; 851360SN/A} 852360SN/A 8532553SN/A 8542553SN/A 8552553SN/A 8561354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 857