syscall_emul.hh revision 6640
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 352764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \ 362764Sstever@eecs.umich.edu defined(__FreeBSD__) || defined(__CYGWIN__)) 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__ 475543Ssaidi@eecs.umich.edu#include <sys/fcntl.h> // for O_BINARY 481809SN/A#endif 493113Sgblack@eecs.umich.edu#include <sys/stat.h> 503113Sgblack@eecs.umich.edu#include <fcntl.h> 511999SN/A#include <sys/uio.h> 52360SN/A 532474SN/A#include "base/chunk_generator.hh" 545543Ssaidi@eecs.umich.edu#include "base/intmath.hh" // for RoundUp 552462SN/A#include "base/misc.hh" 561354SN/A#include "base/trace.hh" 576216Snate@binkert.org#include "base/types.hh" 582474SN/A#include "cpu/base.hh" 592680Sktlim@umich.edu#include "cpu/thread_context.hh" 602474SN/A#include "mem/translating_port.hh" 612474SN/A#include "mem/page_table.hh" 626640Svince@csl.cornell.edu#include "sim/system.hh" 631354SN/A#include "sim/process.hh" 64360SN/A 65360SN/A/// 66360SN/A/// System call descriptor. 67360SN/A/// 68360SN/Aclass SyscallDesc { 69360SN/A 70360SN/A public: 71360SN/A 72378SN/A /// Typedef for target syscall handler functions. 731450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 743114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 75360SN/A 765543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 775543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 785543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 79360SN/A 80360SN/A /// Flag values for controlling syscall behavior. 81360SN/A enum Flags { 82360SN/A /// Don't set return regs according to funcPtr return value. 83360SN/A /// Used for syscalls with non-standard return conventions 842680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 85360SN/A /// sigreturn). 86360SN/A SuppressReturnValue = 1 87360SN/A }; 88360SN/A 89360SN/A /// Constructor. 90360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 91360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 92360SN/A { 93360SN/A } 94360SN/A 95360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 963114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 97360SN/A}; 98360SN/A 99360SN/A 100360SN/Aclass BaseBufferArg { 101360SN/A 102360SN/A public: 103360SN/A 104360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 105360SN/A { 106360SN/A bufPtr = new uint8_t[size]; 107360SN/A // clear out buffer: in case we only partially populate this, 108360SN/A // and then do a copyOut(), we want to make sure we don't 109360SN/A // introduce any random junk into the simulated address space 110360SN/A memset(bufPtr, 0, size); 111360SN/A } 112360SN/A 113360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 114360SN/A 115360SN/A // 116360SN/A // copy data into simulator space (read from target memory) 117360SN/A // 1182400SN/A virtual bool copyIn(TranslatingPort *memport) 119360SN/A { 1202461SN/A memport->readBlob(addr, bufPtr, size); 1215543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 122360SN/A } 123360SN/A 124360SN/A // 125360SN/A // copy data out of simulator space (write to target memory) 126360SN/A // 1272400SN/A virtual bool copyOut(TranslatingPort *memport) 128360SN/A { 1292461SN/A memport->writeBlob(addr, bufPtr, size); 1305543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 131360SN/A } 132360SN/A 133360SN/A protected: 134360SN/A Addr addr; 135360SN/A int size; 136360SN/A uint8_t *bufPtr; 137360SN/A}; 138360SN/A 139360SN/A 140360SN/Aclass BufferArg : public BaseBufferArg 141360SN/A{ 142360SN/A public: 143360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1445543Ssaidi@eecs.umich.edu void *bufferPtr() { return bufPtr; } 145360SN/A}; 146360SN/A 147360SN/Atemplate <class T> 148360SN/Aclass TypedBufferArg : public BaseBufferArg 149360SN/A{ 150360SN/A public: 151360SN/A // user can optionally specify a specific number of bytes to 152360SN/A // allocate to deal with those structs that have variable-size 153360SN/A // arrays at the end 154360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 155360SN/A : BaseBufferArg(_addr, _size) 156360SN/A { } 157360SN/A 158360SN/A // type case 159360SN/A operator T*() { return (T *)bufPtr; } 160360SN/A 161360SN/A // dereference operators 1625543Ssaidi@eecs.umich.edu T &operator*() { return *((T *)bufPtr); } 1635543Ssaidi@eecs.umich.edu T* operator->() { return (T *)bufPtr; } 164502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 165360SN/A}; 166360SN/A 167360SN/A////////////////////////////////////////////////////////////////////// 168360SN/A// 169360SN/A// The following emulation functions are generic enough that they 170360SN/A// don't need to be recompiled for different emulated OS's. They are 171360SN/A// defined in sim/syscall_emul.cc. 172360SN/A// 173360SN/A////////////////////////////////////////////////////////////////////// 174360SN/A 175360SN/A 176378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1771706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1783114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 179378SN/A 180378SN/A/// Handler for unimplemented syscalls that we never intend to 181378SN/A/// implement (signal handling, etc.) and should not affect the correct 182378SN/A/// behavior of the program. Print a warning only if the appropriate 183378SN/A/// trace flag is enabled. Return success to the target program. 1841706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1853114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 186360SN/A 1876109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1881706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1893114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 190378SN/A 1916109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1926109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1936109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 1946109Ssanchezd@stanford.edu 195378SN/A/// Target getpagesize() handler. 1961706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1973114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 198378SN/A 1995748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2005748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2015748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 202378SN/A 203378SN/A/// Target close() handler. 2041706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2053114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 206378SN/A 207378SN/A/// Target read() handler. 2081706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2093114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 210378SN/A 211378SN/A/// Target write() handler. 2121706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2133114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 214378SN/A 215378SN/A/// Target lseek() handler. 2161706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2173114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 218378SN/A 2194118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2204118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2214118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2224118Sgblack@eecs.umich.edu 223378SN/A/// Target munmap() handler. 2241706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2253114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 226378SN/A 227378SN/A/// Target gethostname() handler. 2281706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2293114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 230360SN/A 2315513SMichael.Adler@intel.com/// Target getcwd() handler. 2325513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2335513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2345513SMichael.Adler@intel.com 2355513SMichael.Adler@intel.com/// Target unlink() handler. 2365513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2375513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2385513SMichael.Adler@intel.com 239511SN/A/// Target unlink() handler. 2401706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2413114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 242511SN/A 2435513SMichael.Adler@intel.com/// Target mkdir() handler. 2445513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2455513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2465513SMichael.Adler@intel.com 247511SN/A/// Target rename() handler. 2481706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2493114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2501706SN/A 2511706SN/A 2521706SN/A/// Target truncate() handler. 2531706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2543114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2551706SN/A 2561706SN/A 2571706SN/A/// Target ftruncate() handler. 2581706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2593114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2601706SN/A 261511SN/A 2625513SMichael.Adler@intel.com/// Target umask() handler. 2635513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2645513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2655513SMichael.Adler@intel.com 2665513SMichael.Adler@intel.com 2671999SN/A/// Target chown() handler. 2681999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2693114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2701999SN/A 2711999SN/A 2721999SN/A/// Target fchown() handler. 2731999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2743114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2751999SN/A 2763079Sstever@eecs.umich.edu/// Target dup() handler. 2773079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2783114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2793079Sstever@eecs.umich.edu 2802093SN/A/// Target fnctl() handler. 2812093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2823114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2832093SN/A 2842687Sksewell@umich.edu/// Target fcntl64() handler. 2852687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2863114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2872687Sksewell@umich.edu 2882238SN/A/// Target setuid() handler. 2892238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2903114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2912238SN/A 2922238SN/A/// Target getpid() handler. 2932238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 2943114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2952238SN/A 2962238SN/A/// Target getuid() handler. 2972238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 2983114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2992238SN/A 3002238SN/A/// Target getgid() handler. 3012238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3023114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3032238SN/A 3042238SN/A/// Target getppid() handler. 3052238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3063114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3072238SN/A 3082238SN/A/// Target geteuid() handler. 3092238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3103114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3112238SN/A 3122238SN/A/// Target getegid() handler. 3132238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3143114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3152238SN/A 3166109Ssanchezd@stanford.edu/// Target clone() handler. 3176109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3186109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3192238SN/A 3202238SN/A 3212238SN/A/// Pseudo Funcs - These functions use a different return convension, 3222238SN/A/// returning a second value in a register other than the normal return register 3232238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3243114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3252238SN/A 3262238SN/A/// Target getpidPseudo() handler. 3272238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3283114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3292238SN/A 3302238SN/A/// Target getuidPseudo() handler. 3312238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3323114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3332238SN/A 3342238SN/A/// Target getgidPseudo() handler. 3352238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3363114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3372238SN/A 3382238SN/A 3391354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3401354SN/Aconst int one_million = 1000000; 3411354SN/A 3421354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3431354SN/A/// by my reckoning. We want to keep this a constant (not use the 3441354SN/A/// real-world time) to keep simulations repeatable. 3451354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3461354SN/A 3471354SN/A/// Helper function to convert current elapsed time to seconds and 3481354SN/A/// microseconds. 3491354SN/Atemplate <class T1, class T2> 3501354SN/Avoid 3511354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3521354SN/A{ 3531609SN/A int elapsed_usecs = curTick / Clock::Int::us; 3541354SN/A sec = elapsed_usecs / one_million; 3551354SN/A usec = elapsed_usecs % one_million; 3561354SN/A} 3571354SN/A 358360SN/A////////////////////////////////////////////////////////////////////// 359360SN/A// 360360SN/A// The following emulation functions are generic, but need to be 361360SN/A// templated to account for differences in types, constants, etc. 362360SN/A// 363360SN/A////////////////////////////////////////////////////////////////////// 364360SN/A 3653113Sgblack@eecs.umich.edu#if NO_STAT64 3663113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3673113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3683113Sgblack@eecs.umich.edu#else 3693113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3703113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3713113Sgblack@eecs.umich.edu#endif 3723113Sgblack@eecs.umich.edu 3733113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3743113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3753113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3763113Sgblack@eecs.umich.edu 3773113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3783113Sgblack@eecs.umich.edustatic void 3793113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3803113Sgblack@eecs.umich.edu{ 3814189Sgblack@eecs.umich.edu using namespace TheISA; 3824189Sgblack@eecs.umich.edu 3833113Sgblack@eecs.umich.edu if (fakeTTY) 3843113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 3853113Sgblack@eecs.umich.edu else 3863113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 3873113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 3883113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 3893113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 3903277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 3915515SMichael.Adler@intel.com if (fakeTTY) { 3925515SMichael.Adler@intel.com // Claim to be a character device 3935515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 3945515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 3955515SMichael.Adler@intel.com } 3963277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 3973277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 3983277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 3993277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4003277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4013277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4023277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4033113Sgblack@eecs.umich.edu if (fakeTTY) 4043113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4053113Sgblack@eecs.umich.edu else 4063113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4073113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4083113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4093113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4103114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4113113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4123114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4133113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4143114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4153113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4164061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4174061Sgblack@eecs.umich.edu // consistently across different hosts. 4184061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4193113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4203113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4213113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4223113Sgblack@eecs.umich.edu} 4233113Sgblack@eecs.umich.edu 4243113Sgblack@eecs.umich.edu// Same for stat64 4253113Sgblack@eecs.umich.edu 4263113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4273113Sgblack@eecs.umich.edustatic void 4283113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4293113Sgblack@eecs.umich.edu{ 4304189Sgblack@eecs.umich.edu using namespace TheISA; 4314189Sgblack@eecs.umich.edu 4323113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4333113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4343113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4353113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4363113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4373113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4383113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4393113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4403113Sgblack@eecs.umich.edu#else 4413113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4423113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4433113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4443113Sgblack@eecs.umich.edu#endif 4453113Sgblack@eecs.umich.edu} 4463113Sgblack@eecs.umich.edu 4473113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4483113Sgblack@eecs.umich.edutemplate<class OS> 4493113Sgblack@eecs.umich.edustatic void 4503113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4513113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4523113Sgblack@eecs.umich.edu{ 4533113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4543113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4553113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4563113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4573113Sgblack@eecs.umich.edu} 4583113Sgblack@eecs.umich.edu 4593113Sgblack@eecs.umich.edutemplate<class OS> 4603113Sgblack@eecs.umich.edustatic void 4613113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4623113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4633113Sgblack@eecs.umich.edu{ 4643113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4653113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4663113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4673113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4683113Sgblack@eecs.umich.edu} 4693113Sgblack@eecs.umich.edu 470378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 471378SN/A/// only to find out if their stdout is a tty, to determine whether to 472378SN/A/// do line or block buffering. 473360SN/Atemplate <class OS> 4741450SN/ASyscallReturn 4753114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4762680Sktlim@umich.edu ThreadContext *tc) 477360SN/A{ 4785958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 4795958Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, 1); 480360SN/A 4811969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 482360SN/A 483360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 484360SN/A // doesn't map to any simulator fd: not a valid target fd 4851458SN/A return -EBADF; 486360SN/A } 487360SN/A 488360SN/A switch (req) { 4894131Sbinkertn@umich.edu case OS::TIOCISATTY_: 4904131Sbinkertn@umich.edu case OS::TIOCGETP_: 4914131Sbinkertn@umich.edu case OS::TIOCSETP_: 4924131Sbinkertn@umich.edu case OS::TIOCSETN_: 4934131Sbinkertn@umich.edu case OS::TIOCSETC_: 4944131Sbinkertn@umich.edu case OS::TIOCGETC_: 4954131Sbinkertn@umich.edu case OS::TIOCGETS_: 4964131Sbinkertn@umich.edu case OS::TIOCGETA_: 4971458SN/A return -ENOTTY; 498360SN/A 499360SN/A default: 5001706SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 5012680Sktlim@umich.edu fd, req, tc->readPC()); 502360SN/A } 503360SN/A} 504360SN/A 505378SN/A/// Target open() handler. 506360SN/Atemplate <class OS> 5071450SN/ASyscallReturn 5083114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5092680Sktlim@umich.edu ThreadContext *tc) 510360SN/A{ 511360SN/A std::string path; 512360SN/A 5135958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 5141458SN/A return -EFAULT; 515360SN/A 516360SN/A if (path == "/dev/sysdev0") { 517360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 518360SN/A // We don't support it, so just punt. 5191706SN/A warn("Ignoring open(%s, ...)\n", path); 5201458SN/A return -ENOENT; 521360SN/A } 522360SN/A 5235958Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, 1); 5245958Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, 2); 525360SN/A int hostFlags = 0; 526360SN/A 527360SN/A // translate open flags 528360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 529360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 530360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 531360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 532360SN/A } 533360SN/A } 534360SN/A 535360SN/A // any target flags left? 536360SN/A if (tgtFlags != 0) 5371706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 538360SN/A 539360SN/A#ifdef __CYGWIN32__ 540360SN/A hostFlags |= O_BINARY; 541360SN/A#endif 542360SN/A 5433669Sbinkertn@umich.edu // Adjust path for current working directory 5443669Sbinkertn@umich.edu path = process->fullPath(path); 5453669Sbinkertn@umich.edu 5461706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5471706SN/A 5485795Ssaidi@eecs.umich.edu int fd; 5495795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5505795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5515795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5525795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5535795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5545795Ssaidi@eecs.umich.edu } else { 5555795Ssaidi@eecs.umich.edu // open the file 5565795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5575795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5585795Ssaidi@eecs.umich.edu } 559360SN/A 560360SN/A} 561360SN/A 5626640Svince@csl.cornell.edu/// Target sysinfo() handler. 5636640Svince@csl.cornell.edutemplate <class OS> 5646640Svince@csl.cornell.eduSyscallReturn 5656640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5666640Svince@csl.cornell.edu ThreadContext *tc) 5676640Svince@csl.cornell.edu{ 5686640Svince@csl.cornell.edu 5696640Svince@csl.cornell.edu TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0)); 5706640Svince@csl.cornell.edu 5716640Svince@csl.cornell.edu sysinfo->uptime=seconds_since_epoch; 5726640Svince@csl.cornell.edu sysinfo->totalram=process->system->memSize(); 5736640Svince@csl.cornell.edu 5746640Svince@csl.cornell.edu sysinfo.copyOut(tc->getMemPort()); 5756640Svince@csl.cornell.edu 5766640Svince@csl.cornell.edu return 0; 5776640Svince@csl.cornell.edu} 578360SN/A 5791999SN/A/// Target chmod() handler. 5801999SN/Atemplate <class OS> 5811999SN/ASyscallReturn 5823114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5832680Sktlim@umich.edu ThreadContext *tc) 5841999SN/A{ 5851999SN/A std::string path; 5861999SN/A 5875958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 5881999SN/A return -EFAULT; 5891999SN/A 5905958Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, 1); 5911999SN/A mode_t hostMode = 0; 5921999SN/A 5931999SN/A // XXX translate mode flags via OS::something??? 5941999SN/A hostMode = mode; 5951999SN/A 5963669Sbinkertn@umich.edu // Adjust path for current working directory 5973669Sbinkertn@umich.edu path = process->fullPath(path); 5983669Sbinkertn@umich.edu 5991999SN/A // do the chmod 6001999SN/A int result = chmod(path.c_str(), hostMode); 6011999SN/A if (result < 0) 6022218SN/A return -errno; 6031999SN/A 6041999SN/A return 0; 6051999SN/A} 6061999SN/A 6071999SN/A 6081999SN/A/// Target fchmod() handler. 6091999SN/Atemplate <class OS> 6101999SN/ASyscallReturn 6113114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6122680Sktlim@umich.edu ThreadContext *tc) 6131999SN/A{ 6145958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 6151999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6161999SN/A // doesn't map to any simulator fd: not a valid target fd 6171999SN/A return -EBADF; 6181999SN/A } 6191999SN/A 6205958Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, 1); 6211999SN/A mode_t hostMode = 0; 6221999SN/A 6231999SN/A // XXX translate mode flags via OS::someting??? 6241999SN/A hostMode = mode; 6251999SN/A 6261999SN/A // do the fchmod 6271999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6281999SN/A if (result < 0) 6292218SN/A return -errno; 6301999SN/A 6311999SN/A return 0; 6321999SN/A} 6331999SN/A 6345877Shsul@eecs.umich.edu/// Target mremap() handler. 6355877Shsul@eecs.umich.edutemplate <class OS> 6365877Shsul@eecs.umich.eduSyscallReturn 6375877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6385877Shsul@eecs.umich.edu{ 6395958Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, 0); 6405958Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, 1); 6415958Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, 2); 6425958Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, 3); 6435877Shsul@eecs.umich.edu 6445877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6455877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6465877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6475877Shsul@eecs.umich.edu return -EINVAL; 6485877Shsul@eecs.umich.edu } 6495877Shsul@eecs.umich.edu 6505877Shsul@eecs.umich.edu if (new_length > old_length) { 6515877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6525877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6535877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6545877Shsul@eecs.umich.edu process->mmap_end += diff; 6555877Shsul@eecs.umich.edu return start; 6565877Shsul@eecs.umich.edu } else { 6575877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6585877Shsul@eecs.umich.edu if (!(flags & 1)) { 6595877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6605877Shsul@eecs.umich.edu return -ENOMEM; 6615877Shsul@eecs.umich.edu } else { 6625877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6635877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6645877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6655877Shsul@eecs.umich.edu start = process->mmap_end; 6665877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6675877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6685877Shsul@eecs.umich.edu process->mmap_end += new_length; 6695877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6705877Shsul@eecs.umich.edu return start; 6715877Shsul@eecs.umich.edu } 6725877Shsul@eecs.umich.edu } 6735877Shsul@eecs.umich.edu } else { 6745877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 6755877Shsul@eecs.umich.edu new_length); 6765877Shsul@eecs.umich.edu return start; 6775877Shsul@eecs.umich.edu } 6785877Shsul@eecs.umich.edu} 6791999SN/A 680378SN/A/// Target stat() handler. 681360SN/Atemplate <class OS> 6821450SN/ASyscallReturn 6833114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6842680Sktlim@umich.edu ThreadContext *tc) 685360SN/A{ 686360SN/A std::string path; 687360SN/A 6885958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 6892400SN/A return -EFAULT; 690360SN/A 6913669Sbinkertn@umich.edu // Adjust path for current working directory 6923669Sbinkertn@umich.edu path = process->fullPath(path); 6933669Sbinkertn@umich.edu 694360SN/A struct stat hostBuf; 695360SN/A int result = stat(path.c_str(), &hostBuf); 696360SN/A 697360SN/A if (result < 0) 6982218SN/A return -errno; 699360SN/A 7005958Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 7015958Sgblack@eecs.umich.edu &hostBuf); 702360SN/A 7031458SN/A return 0; 704360SN/A} 705360SN/A 706360SN/A 7075074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7085074Ssaidi@eecs.umich.edutemplate <class OS> 7095074Ssaidi@eecs.umich.eduSyscallReturn 7105074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7115074Ssaidi@eecs.umich.edu ThreadContext *tc) 7125074Ssaidi@eecs.umich.edu{ 7135074Ssaidi@eecs.umich.edu std::string path; 7145074Ssaidi@eecs.umich.edu 7155958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 7165074Ssaidi@eecs.umich.edu return -EFAULT; 7175074Ssaidi@eecs.umich.edu 7185074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7195074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7205074Ssaidi@eecs.umich.edu 7215208Ssaidi@eecs.umich.edu#if NO_STAT64 7225208Ssaidi@eecs.umich.edu struct stat hostBuf; 7235208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7245208Ssaidi@eecs.umich.edu#else 7255074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7265074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7275208Ssaidi@eecs.umich.edu#endif 7285074Ssaidi@eecs.umich.edu 7295074Ssaidi@eecs.umich.edu if (result < 0) 7305074Ssaidi@eecs.umich.edu return -errno; 7315074Ssaidi@eecs.umich.edu 7325958Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 7335958Sgblack@eecs.umich.edu &hostBuf); 7345074Ssaidi@eecs.umich.edu 7355074Ssaidi@eecs.umich.edu return 0; 7365074Ssaidi@eecs.umich.edu} 7375074Ssaidi@eecs.umich.edu 7385074Ssaidi@eecs.umich.edu 7391999SN/A/// Target fstat64() handler. 7401999SN/Atemplate <class OS> 7411999SN/ASyscallReturn 7423114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7432680Sktlim@umich.edu ThreadContext *tc) 7441999SN/A{ 7455958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 7461999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7471999SN/A // doesn't map to any simulator fd: not a valid target fd 7481999SN/A return -EBADF; 7491999SN/A } 7501999SN/A 7512764Sstever@eecs.umich.edu#if NO_STAT64 7522064SN/A struct stat hostBuf; 7532064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7542064SN/A#else 7552064SN/A struct stat64 hostBuf; 7561999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7572064SN/A#endif 7581999SN/A 7591999SN/A if (result < 0) 7602218SN/A return -errno; 7611999SN/A 7625958Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 7633114Sgblack@eecs.umich.edu &hostBuf, (fd == 1)); 7641999SN/A 7651999SN/A return 0; 7661999SN/A} 7671999SN/A 7681999SN/A 769378SN/A/// Target lstat() handler. 770360SN/Atemplate <class OS> 7711450SN/ASyscallReturn 7723114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7732680Sktlim@umich.edu ThreadContext *tc) 774360SN/A{ 775360SN/A std::string path; 776360SN/A 7775958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 7782400SN/A return -EFAULT; 779360SN/A 7803669Sbinkertn@umich.edu // Adjust path for current working directory 7813669Sbinkertn@umich.edu path = process->fullPath(path); 7823669Sbinkertn@umich.edu 783360SN/A struct stat hostBuf; 784360SN/A int result = lstat(path.c_str(), &hostBuf); 785360SN/A 786360SN/A if (result < 0) 7871458SN/A return -errno; 788360SN/A 7895958Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 7905958Sgblack@eecs.umich.edu &hostBuf); 791360SN/A 7921458SN/A return 0; 793360SN/A} 794360SN/A 7951999SN/A/// Target lstat64() handler. 7961999SN/Atemplate <class OS> 7971999SN/ASyscallReturn 7983114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7992680Sktlim@umich.edu ThreadContext *tc) 8001999SN/A{ 8011999SN/A std::string path; 8021999SN/A 8035958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 8042400SN/A return -EFAULT; 8051999SN/A 8063669Sbinkertn@umich.edu // Adjust path for current working directory 8073669Sbinkertn@umich.edu path = process->fullPath(path); 8083669Sbinkertn@umich.edu 8092764Sstever@eecs.umich.edu#if NO_STAT64 8102064SN/A struct stat hostBuf; 8112064SN/A int result = lstat(path.c_str(), &hostBuf); 8122064SN/A#else 8131999SN/A struct stat64 hostBuf; 8141999SN/A int result = lstat64(path.c_str(), &hostBuf); 8152064SN/A#endif 8161999SN/A 8171999SN/A if (result < 0) 8181999SN/A return -errno; 8191999SN/A 8205958Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 8215958Sgblack@eecs.umich.edu &hostBuf); 8221999SN/A 8231999SN/A return 0; 8241999SN/A} 8251999SN/A 826378SN/A/// Target fstat() handler. 827360SN/Atemplate <class OS> 8281450SN/ASyscallReturn 8293114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8302680Sktlim@umich.edu ThreadContext *tc) 831360SN/A{ 8325958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 833360SN/A 8341969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 835360SN/A 836360SN/A if (fd < 0) 8371458SN/A return -EBADF; 838360SN/A 839360SN/A struct stat hostBuf; 840360SN/A int result = fstat(fd, &hostBuf); 841360SN/A 842360SN/A if (result < 0) 8431458SN/A return -errno; 844360SN/A 8455958Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1), 8463114Sgblack@eecs.umich.edu &hostBuf, (fd == 1)); 8472021SN/A 8481458SN/A return 0; 849360SN/A} 850360SN/A 851360SN/A 8521706SN/A/// Target statfs() handler. 8531706SN/Atemplate <class OS> 8541706SN/ASyscallReturn 8553114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8562680Sktlim@umich.edu ThreadContext *tc) 8571706SN/A{ 8581706SN/A std::string path; 8591706SN/A 8605958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 8612400SN/A return -EFAULT; 8621706SN/A 8633669Sbinkertn@umich.edu // Adjust path for current working directory 8643669Sbinkertn@umich.edu path = process->fullPath(path); 8653669Sbinkertn@umich.edu 8661706SN/A struct statfs hostBuf; 8671706SN/A int result = statfs(path.c_str(), &hostBuf); 8681706SN/A 8691706SN/A if (result < 0) 8702218SN/A return -errno; 8711706SN/A 8723114Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), 8735958Sgblack@eecs.umich.edu (Addr)(process->getSyscallArg(tc, 1)), &hostBuf); 8741706SN/A 8751706SN/A return 0; 8761706SN/A} 8771706SN/A 8781706SN/A 8791706SN/A/// Target fstatfs() handler. 8801706SN/Atemplate <class OS> 8811706SN/ASyscallReturn 8823114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8832680Sktlim@umich.edu ThreadContext *tc) 8841706SN/A{ 8855958Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, 0)); 8861706SN/A 8871706SN/A if (fd < 0) 8881706SN/A return -EBADF; 8891706SN/A 8901706SN/A struct statfs hostBuf; 8911706SN/A int result = fstatfs(fd, &hostBuf); 8921706SN/A 8931706SN/A if (result < 0) 8942218SN/A return -errno; 8951706SN/A 8965958Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1), 8973114Sgblack@eecs.umich.edu &hostBuf); 8981706SN/A 8991706SN/A return 0; 9001706SN/A} 9011706SN/A 9021706SN/A 9031999SN/A/// Target writev() handler. 9041999SN/Atemplate <class OS> 9051999SN/ASyscallReturn 9063114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9072680Sktlim@umich.edu ThreadContext *tc) 9081999SN/A{ 9095958Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, 0); 9101999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9111999SN/A // doesn't map to any simulator fd: not a valid target fd 9121999SN/A return -EBADF; 9131999SN/A } 9141999SN/A 9152680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9165958Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, 1); 9175958Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, 2); 9181999SN/A struct iovec hiov[count]; 9196227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9201999SN/A typename OS::tgt_iovec tiov; 9212461SN/A 9222461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9232461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9242091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9251999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9262461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9272461SN/A hiov[i].iov_len); 9281999SN/A } 9291999SN/A 9301999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9311999SN/A 9326227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9331999SN/A delete [] (char *)hiov[i].iov_base; 9341999SN/A 9351999SN/A if (result < 0) 9362218SN/A return -errno; 9371999SN/A 9381999SN/A return 0; 9391999SN/A} 9401999SN/A 9411999SN/A 942378SN/A/// Target mmap() handler. 943378SN/A/// 944378SN/A/// We don't really handle mmap(). If the target is mmaping an 945378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 946378SN/A/// nothing (since memory is initialized to zero and the simulator 947378SN/A/// doesn't really check addresses anyway). Always print a warning, 948378SN/A/// since this could be seriously broken if we're not mapping 949378SN/A/// /dev/zero. 950360SN/A// 951378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 952378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 953378SN/A/// anything else. 954360SN/Atemplate <class OS> 9551450SN/ASyscallReturn 9563114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 957360SN/A{ 9585958Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, 0); 9595958Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, 1); 9605958Sgblack@eecs.umich.edu // int prot = p->getSyscallArg(tc, 2); 9615958Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, 3); 9625958Sgblack@eecs.umich.edu // int fd = p->sim_fd(p->getSyscallArg(tc, 4)); 9635958Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, 5); 964360SN/A 9655877Shsul@eecs.umich.edu 9662544SN/A if ((start % TheISA::VMPageSize) != 0 || 9672544SN/A (length % TheISA::VMPageSize) != 0) { 9682544SN/A warn("mmap failing: arguments not page-aligned: " 9692544SN/A "start 0x%x length 0x%x", 9702544SN/A start, length); 9712544SN/A return -EINVAL; 972360SN/A } 973360SN/A 9742544SN/A if (start != 0) { 9752544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 9762544SN/A start, p->mmap_end); 9772544SN/A } 9782544SN/A 9792544SN/A // pick next address from our "mmap region" 9802544SN/A start = p->mmap_end; 9812544SN/A p->pTable->allocate(start, length); 9822544SN/A p->mmap_end += length; 9832544SN/A 9842553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 9851969SN/A warn("allowing mmap of file @ fd %d. " 9865958Sgblack@eecs.umich.edu "This will break if not /dev/zero.", p->getSyscallArg(tc, 4)); 987360SN/A } 988360SN/A 9891458SN/A return start; 990360SN/A} 991360SN/A 992378SN/A/// Target getrlimit() handler. 993360SN/Atemplate <class OS> 9941450SN/ASyscallReturn 9953114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9962680Sktlim@umich.edu ThreadContext *tc) 997360SN/A{ 9985958Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, 0); 9995958Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1)); 1000360SN/A 1001360SN/A switch (resource) { 10022064SN/A case OS::TGT_RLIMIT_STACK: 10035877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10042064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10052091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10062091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10072064SN/A break; 1008360SN/A 10095877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10105877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10115877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10125877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10135877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10145877Shsul@eecs.umich.edu break; 10155877Shsul@eecs.umich.edu 10162064SN/A default: 10172064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10182064SN/A << std::endl; 10192064SN/A abort(); 10202064SN/A break; 1021360SN/A } 1022360SN/A 10232680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10241458SN/A return 0; 1025360SN/A} 1026360SN/A 1027378SN/A/// Target gettimeofday() handler. 1028360SN/Atemplate <class OS> 10291450SN/ASyscallReturn 10303114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10312680Sktlim@umich.edu ThreadContext *tc) 1032360SN/A{ 10335958Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0)); 1034360SN/A 1035360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1036360SN/A tp->tv_sec += seconds_since_epoch; 10376109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10386109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1039360SN/A 10402680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1041360SN/A 10421458SN/A return 0; 1043360SN/A} 1044360SN/A 1045360SN/A 10461999SN/A/// Target utimes() handler. 10471999SN/Atemplate <class OS> 10481999SN/ASyscallReturn 10493114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10502680Sktlim@umich.edu ThreadContext *tc) 10511999SN/A{ 10521999SN/A std::string path; 10531999SN/A 10545958Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0))) 10552400SN/A return -EFAULT; 10561999SN/A 10575958Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1)); 10582680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 10591999SN/A 10601999SN/A struct timeval hostTimeval[2]; 10611999SN/A for (int i = 0; i < 2; ++i) 10621999SN/A { 10632091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 10642091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 10651999SN/A } 10663669Sbinkertn@umich.edu 10673669Sbinkertn@umich.edu // Adjust path for current working directory 10683669Sbinkertn@umich.edu path = process->fullPath(path); 10693669Sbinkertn@umich.edu 10701999SN/A int result = utimes(path.c_str(), hostTimeval); 10711999SN/A 10721999SN/A if (result < 0) 10731999SN/A return -errno; 10741999SN/A 10751999SN/A return 0; 10761999SN/A} 1077378SN/A/// Target getrusage() function. 1078360SN/Atemplate <class OS> 10791450SN/ASyscallReturn 10803114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10812680Sktlim@umich.edu ThreadContext *tc) 1082360SN/A{ 10835958Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, 0); // THREAD, SELF, or CHILDREN 10845958Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1)); 1085360SN/A 10863670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 10873670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1088360SN/A rup->ru_stime.tv_sec = 0; 1089360SN/A rup->ru_stime.tv_usec = 0; 1090360SN/A rup->ru_maxrss = 0; 1091360SN/A rup->ru_ixrss = 0; 1092360SN/A rup->ru_idrss = 0; 1093360SN/A rup->ru_isrss = 0; 1094360SN/A rup->ru_minflt = 0; 1095360SN/A rup->ru_majflt = 0; 1096360SN/A rup->ru_nswap = 0; 1097360SN/A rup->ru_inblock = 0; 1098360SN/A rup->ru_oublock = 0; 1099360SN/A rup->ru_msgsnd = 0; 1100360SN/A rup->ru_msgrcv = 0; 1101360SN/A rup->ru_nsignals = 0; 1102360SN/A rup->ru_nvcsw = 0; 1103360SN/A rup->ru_nivcsw = 0; 1104360SN/A 11053670Sbinkertn@umich.edu switch (who) { 11063670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11073670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11083670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11093670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11103670Sbinkertn@umich.edu break; 11113670Sbinkertn@umich.edu 11123670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11133670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11143670Sbinkertn@umich.edu break; 11153670Sbinkertn@umich.edu 11163670Sbinkertn@umich.edu default: 11173670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11183670Sbinkertn@umich.edu // plow ahead 11193670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11203670Sbinkertn@umich.edu who); 11213670Sbinkertn@umich.edu } 11223670Sbinkertn@umich.edu 11232680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1124360SN/A 11251458SN/A return 0; 1126360SN/A} 1127360SN/A 11282553SN/A 11292553SN/A 11302553SN/A 11311354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1132