syscall_emul.hh revision 6701
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" 586658Snate@binkert.org#include "config/the_isa.hh" 592474SN/A#include "cpu/base.hh" 602680Sktlim@umich.edu#include "cpu/thread_context.hh" 612474SN/A#include "mem/translating_port.hh" 622474SN/A#include "mem/page_table.hh" 636640Svince@csl.cornell.edu#include "sim/system.hh" 641354SN/A#include "sim/process.hh" 65360SN/A 66360SN/A/// 67360SN/A/// System call descriptor. 68360SN/A/// 69360SN/Aclass SyscallDesc { 70360SN/A 71360SN/A public: 72360SN/A 73378SN/A /// Typedef for target syscall handler functions. 741450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 753114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 76360SN/A 775543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 785543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 795543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 80360SN/A 81360SN/A /// Flag values for controlling syscall behavior. 82360SN/A enum Flags { 83360SN/A /// Don't set return regs according to funcPtr return value. 84360SN/A /// Used for syscalls with non-standard return conventions 852680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 86360SN/A /// sigreturn). 87360SN/A SuppressReturnValue = 1 88360SN/A }; 89360SN/A 90360SN/A /// Constructor. 91360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 92360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 93360SN/A { 94360SN/A } 95360SN/A 96360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 973114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 98360SN/A}; 99360SN/A 100360SN/A 101360SN/Aclass BaseBufferArg { 102360SN/A 103360SN/A public: 104360SN/A 105360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 106360SN/A { 107360SN/A bufPtr = new uint8_t[size]; 108360SN/A // clear out buffer: in case we only partially populate this, 109360SN/A // and then do a copyOut(), we want to make sure we don't 110360SN/A // introduce any random junk into the simulated address space 111360SN/A memset(bufPtr, 0, size); 112360SN/A } 113360SN/A 114360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 115360SN/A 116360SN/A // 117360SN/A // copy data into simulator space (read from target memory) 118360SN/A // 1192400SN/A virtual bool copyIn(TranslatingPort *memport) 120360SN/A { 1212461SN/A memport->readBlob(addr, bufPtr, size); 1225543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 123360SN/A } 124360SN/A 125360SN/A // 126360SN/A // copy data out of simulator space (write to target memory) 127360SN/A // 1282400SN/A virtual bool copyOut(TranslatingPort *memport) 129360SN/A { 1302461SN/A memport->writeBlob(addr, bufPtr, size); 1315543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 132360SN/A } 133360SN/A 134360SN/A protected: 135360SN/A Addr addr; 136360SN/A int size; 137360SN/A uint8_t *bufPtr; 138360SN/A}; 139360SN/A 140360SN/A 141360SN/Aclass BufferArg : public BaseBufferArg 142360SN/A{ 143360SN/A public: 144360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1455543Ssaidi@eecs.umich.edu void *bufferPtr() { return bufPtr; } 146360SN/A}; 147360SN/A 148360SN/Atemplate <class T> 149360SN/Aclass TypedBufferArg : public BaseBufferArg 150360SN/A{ 151360SN/A public: 152360SN/A // user can optionally specify a specific number of bytes to 153360SN/A // allocate to deal with those structs that have variable-size 154360SN/A // arrays at the end 155360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 156360SN/A : BaseBufferArg(_addr, _size) 157360SN/A { } 158360SN/A 159360SN/A // type case 160360SN/A operator T*() { return (T *)bufPtr; } 161360SN/A 162360SN/A // dereference operators 1635543Ssaidi@eecs.umich.edu T &operator*() { return *((T *)bufPtr); } 1645543Ssaidi@eecs.umich.edu T* operator->() { return (T *)bufPtr; } 165502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 166360SN/A}; 167360SN/A 168360SN/A////////////////////////////////////////////////////////////////////// 169360SN/A// 170360SN/A// The following emulation functions are generic enough that they 171360SN/A// don't need to be recompiled for different emulated OS's. They are 172360SN/A// defined in sim/syscall_emul.cc. 173360SN/A// 174360SN/A////////////////////////////////////////////////////////////////////// 175360SN/A 176360SN/A 177378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1781706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1793114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 180378SN/A 181378SN/A/// Handler for unimplemented syscalls that we never intend to 182378SN/A/// implement (signal handling, etc.) and should not affect the correct 183378SN/A/// behavior of the program. Print a warning only if the appropriate 184378SN/A/// trace flag is enabled. Return success to the target program. 1851706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1863114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 187360SN/A 1886109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1891706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1903114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 191378SN/A 1926109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1936109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1946109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 1956109Ssanchezd@stanford.edu 196378SN/A/// Target getpagesize() handler. 1971706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1983114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 199378SN/A 2005748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2015748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2025748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 203378SN/A 204378SN/A/// Target close() handler. 2051706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2063114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 207378SN/A 208378SN/A/// Target read() handler. 2091706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2103114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 211378SN/A 212378SN/A/// Target write() handler. 2131706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2143114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 215378SN/A 216378SN/A/// Target lseek() handler. 2171706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2183114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 219378SN/A 2204118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2214118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2224118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2234118Sgblack@eecs.umich.edu 224378SN/A/// Target munmap() handler. 2251706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2263114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 227378SN/A 228378SN/A/// Target gethostname() handler. 2291706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2303114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 231360SN/A 2325513SMichael.Adler@intel.com/// Target getcwd() handler. 2335513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2345513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2355513SMichael.Adler@intel.com 2365513SMichael.Adler@intel.com/// Target unlink() handler. 2375513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2385513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2395513SMichael.Adler@intel.com 240511SN/A/// Target unlink() handler. 2411706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2423114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 243511SN/A 2445513SMichael.Adler@intel.com/// Target mkdir() handler. 2455513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2465513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2475513SMichael.Adler@intel.com 248511SN/A/// Target rename() handler. 2491706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2503114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2511706SN/A 2521706SN/A 2531706SN/A/// Target truncate() handler. 2541706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2553114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2561706SN/A 2571706SN/A 2581706SN/A/// Target ftruncate() handler. 2591706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2603114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2611706SN/A 262511SN/A 2636685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2646685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2656685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2666685Stjones1@inf.ed.ac.uk 2676685Stjones1@inf.ed.ac.uk 2685513SMichael.Adler@intel.com/// Target umask() handler. 2695513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2705513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2715513SMichael.Adler@intel.com 2725513SMichael.Adler@intel.com 2731999SN/A/// Target chown() handler. 2741999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2753114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2761999SN/A 2771999SN/A 2781999SN/A/// Target fchown() handler. 2791999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2803114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2811999SN/A 2823079Sstever@eecs.umich.edu/// Target dup() handler. 2833079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2843114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2853079Sstever@eecs.umich.edu 2862093SN/A/// Target fnctl() handler. 2872093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2883114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2892093SN/A 2902687Sksewell@umich.edu/// Target fcntl64() handler. 2912687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2923114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2932687Sksewell@umich.edu 2942238SN/A/// Target setuid() handler. 2952238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2963114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2972238SN/A 2982238SN/A/// Target getpid() handler. 2992238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3003114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3012238SN/A 3022238SN/A/// Target getuid() handler. 3032238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3043114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3052238SN/A 3062238SN/A/// Target getgid() handler. 3072238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3083114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3092238SN/A 3102238SN/A/// Target getppid() handler. 3112238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3123114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3132238SN/A 3142238SN/A/// Target geteuid() handler. 3152238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3163114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3172238SN/A 3182238SN/A/// Target getegid() handler. 3192238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3203114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3212238SN/A 3226109Ssanchezd@stanford.edu/// Target clone() handler. 3236109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3246109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3252238SN/A 3262238SN/A 3272238SN/A/// Pseudo Funcs - These functions use a different return convension, 3282238SN/A/// returning a second value in a register other than the normal return register 3292238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3303114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3312238SN/A 3322238SN/A/// Target getpidPseudo() handler. 3332238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3343114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3352238SN/A 3362238SN/A/// Target getuidPseudo() handler. 3372238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3383114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3392238SN/A 3402238SN/A/// Target getgidPseudo() handler. 3412238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3423114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3432238SN/A 3442238SN/A 3451354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3461354SN/Aconst int one_million = 1000000; 3471354SN/A 3481354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3491354SN/A/// by my reckoning. We want to keep this a constant (not use the 3501354SN/A/// real-world time) to keep simulations repeatable. 3511354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3521354SN/A 3531354SN/A/// Helper function to convert current elapsed time to seconds and 3541354SN/A/// microseconds. 3551354SN/Atemplate <class T1, class T2> 3561354SN/Avoid 3571354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3581354SN/A{ 3591609SN/A int elapsed_usecs = curTick / Clock::Int::us; 3601354SN/A sec = elapsed_usecs / one_million; 3611354SN/A usec = elapsed_usecs % one_million; 3621354SN/A} 3631354SN/A 364360SN/A////////////////////////////////////////////////////////////////////// 365360SN/A// 366360SN/A// The following emulation functions are generic, but need to be 367360SN/A// templated to account for differences in types, constants, etc. 368360SN/A// 369360SN/A////////////////////////////////////////////////////////////////////// 370360SN/A 3713113Sgblack@eecs.umich.edu#if NO_STAT64 3723113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3733113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3743113Sgblack@eecs.umich.edu#else 3753113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3763113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3773113Sgblack@eecs.umich.edu#endif 3783113Sgblack@eecs.umich.edu 3793113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3803113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3813113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3823113Sgblack@eecs.umich.edu 3833113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3843113Sgblack@eecs.umich.edustatic void 3853113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3863113Sgblack@eecs.umich.edu{ 3874189Sgblack@eecs.umich.edu using namespace TheISA; 3884189Sgblack@eecs.umich.edu 3893113Sgblack@eecs.umich.edu if (fakeTTY) 3903113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 3913113Sgblack@eecs.umich.edu else 3923113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 3933113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 3943113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 3953113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 3963277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 3975515SMichael.Adler@intel.com if (fakeTTY) { 3985515SMichael.Adler@intel.com // Claim to be a character device 3995515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4005515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4015515SMichael.Adler@intel.com } 4023277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 4033277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4043277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 4053277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4063277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4073277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4083277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4093113Sgblack@eecs.umich.edu if (fakeTTY) 4103113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4113113Sgblack@eecs.umich.edu else 4123113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4133113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4143113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4153113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4163114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4173113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4183114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4193113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4203114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4213113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4224061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4234061Sgblack@eecs.umich.edu // consistently across different hosts. 4244061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4253113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4263113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4273113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4283113Sgblack@eecs.umich.edu} 4293113Sgblack@eecs.umich.edu 4303113Sgblack@eecs.umich.edu// Same for stat64 4313113Sgblack@eecs.umich.edu 4323113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4333113Sgblack@eecs.umich.edustatic void 4343113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4353113Sgblack@eecs.umich.edu{ 4364189Sgblack@eecs.umich.edu using namespace TheISA; 4374189Sgblack@eecs.umich.edu 4383113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4393113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4403113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4413113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4423113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4433113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4443113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4453113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4463113Sgblack@eecs.umich.edu#else 4473113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4483113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4493113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4503113Sgblack@eecs.umich.edu#endif 4513113Sgblack@eecs.umich.edu} 4523113Sgblack@eecs.umich.edu 4533113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4543113Sgblack@eecs.umich.edutemplate<class OS> 4553113Sgblack@eecs.umich.edustatic void 4563113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4573113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4583113Sgblack@eecs.umich.edu{ 4593113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4603113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4613113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4623113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4633113Sgblack@eecs.umich.edu} 4643113Sgblack@eecs.umich.edu 4653113Sgblack@eecs.umich.edutemplate<class OS> 4663113Sgblack@eecs.umich.edustatic void 4673113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4683113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4693113Sgblack@eecs.umich.edu{ 4703113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4713113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4726686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4733113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4743113Sgblack@eecs.umich.edu} 4753113Sgblack@eecs.umich.edu 476378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 477378SN/A/// only to find out if their stdout is a tty, to determine whether to 478378SN/A/// do line or block buffering. 479360SN/Atemplate <class OS> 4801450SN/ASyscallReturn 4813114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4822680Sktlim@umich.edu ThreadContext *tc) 483360SN/A{ 4846701Sgblack@eecs.umich.edu int index = 0; 4856701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 4866701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 487360SN/A 4881969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 489360SN/A 490360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 491360SN/A // doesn't map to any simulator fd: not a valid target fd 4921458SN/A return -EBADF; 493360SN/A } 494360SN/A 495360SN/A switch (req) { 4964131Sbinkertn@umich.edu case OS::TIOCISATTY_: 4974131Sbinkertn@umich.edu case OS::TIOCGETP_: 4984131Sbinkertn@umich.edu case OS::TIOCSETP_: 4994131Sbinkertn@umich.edu case OS::TIOCSETN_: 5004131Sbinkertn@umich.edu case OS::TIOCSETC_: 5014131Sbinkertn@umich.edu case OS::TIOCGETC_: 5024131Sbinkertn@umich.edu case OS::TIOCGETS_: 5034131Sbinkertn@umich.edu case OS::TIOCGETA_: 5046689Stjones1@inf.ed.ac.uk case OS::TCSETAW_: 5051458SN/A return -ENOTTY; 506360SN/A 507360SN/A default: 5081706SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 5092680Sktlim@umich.edu fd, req, tc->readPC()); 510360SN/A } 511360SN/A} 512360SN/A 513378SN/A/// Target open() handler. 514360SN/Atemplate <class OS> 5151450SN/ASyscallReturn 5163114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5172680Sktlim@umich.edu ThreadContext *tc) 518360SN/A{ 519360SN/A std::string path; 520360SN/A 5216701Sgblack@eecs.umich.edu int index = 0; 5226701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 5236701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 5241458SN/A return -EFAULT; 525360SN/A 526360SN/A if (path == "/dev/sysdev0") { 527360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 528360SN/A // We don't support it, so just punt. 5291706SN/A warn("Ignoring open(%s, ...)\n", path); 5301458SN/A return -ENOENT; 531360SN/A } 532360SN/A 5336701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 5346701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 535360SN/A int hostFlags = 0; 536360SN/A 537360SN/A // translate open flags 538360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 539360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 540360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 541360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 542360SN/A } 543360SN/A } 544360SN/A 545360SN/A // any target flags left? 546360SN/A if (tgtFlags != 0) 5471706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 548360SN/A 549360SN/A#ifdef __CYGWIN32__ 550360SN/A hostFlags |= O_BINARY; 551360SN/A#endif 552360SN/A 5533669Sbinkertn@umich.edu // Adjust path for current working directory 5543669Sbinkertn@umich.edu path = process->fullPath(path); 5553669Sbinkertn@umich.edu 5561706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5571706SN/A 5585795Ssaidi@eecs.umich.edu int fd; 5595795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5605795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5615795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5625795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5635795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5645795Ssaidi@eecs.umich.edu } else { 5655795Ssaidi@eecs.umich.edu // open the file 5665795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5675795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5685795Ssaidi@eecs.umich.edu } 569360SN/A 570360SN/A} 571360SN/A 5726640Svince@csl.cornell.edu/// Target sysinfo() handler. 5736640Svince@csl.cornell.edutemplate <class OS> 5746640Svince@csl.cornell.eduSyscallReturn 5756640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5766640Svince@csl.cornell.edu ThreadContext *tc) 5776640Svince@csl.cornell.edu{ 5786640Svince@csl.cornell.edu 5796701Sgblack@eecs.umich.edu int index = 0; 5806701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 5816701Sgblack@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 5826640Svince@csl.cornell.edu 5836701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 5846701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 5856640Svince@csl.cornell.edu 5866701Sgblack@eecs.umich.edu sysinfo.copyOut(tc->getMemPort()); 5876640Svince@csl.cornell.edu 5886701Sgblack@eecs.umich.edu return 0; 5896640Svince@csl.cornell.edu} 590360SN/A 5911999SN/A/// Target chmod() handler. 5921999SN/Atemplate <class OS> 5931999SN/ASyscallReturn 5943114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5952680Sktlim@umich.edu ThreadContext *tc) 5961999SN/A{ 5971999SN/A std::string path; 5981999SN/A 5996701Sgblack@eecs.umich.edu int index = 0; 6006701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 6016701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 6021999SN/A return -EFAULT; 6036701Sgblack@eecs.umich.edu } 6041999SN/A 6056701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6061999SN/A mode_t hostMode = 0; 6071999SN/A 6081999SN/A // XXX translate mode flags via OS::something??? 6091999SN/A hostMode = mode; 6101999SN/A 6113669Sbinkertn@umich.edu // Adjust path for current working directory 6123669Sbinkertn@umich.edu path = process->fullPath(path); 6133669Sbinkertn@umich.edu 6141999SN/A // do the chmod 6151999SN/A int result = chmod(path.c_str(), hostMode); 6161999SN/A if (result < 0) 6172218SN/A return -errno; 6181999SN/A 6191999SN/A return 0; 6201999SN/A} 6211999SN/A 6221999SN/A 6231999SN/A/// Target fchmod() handler. 6241999SN/Atemplate <class OS> 6251999SN/ASyscallReturn 6263114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6272680Sktlim@umich.edu ThreadContext *tc) 6281999SN/A{ 6296701Sgblack@eecs.umich.edu int index = 0; 6306701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6311999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6321999SN/A // doesn't map to any simulator fd: not a valid target fd 6331999SN/A return -EBADF; 6341999SN/A } 6351999SN/A 6366701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6371999SN/A mode_t hostMode = 0; 6381999SN/A 6391999SN/A // XXX translate mode flags via OS::someting??? 6401999SN/A hostMode = mode; 6411999SN/A 6421999SN/A // do the fchmod 6431999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6441999SN/A if (result < 0) 6452218SN/A return -errno; 6461999SN/A 6471999SN/A return 0; 6481999SN/A} 6491999SN/A 6505877Shsul@eecs.umich.edu/// Target mremap() handler. 6515877Shsul@eecs.umich.edutemplate <class OS> 6525877Shsul@eecs.umich.eduSyscallReturn 6535877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6545877Shsul@eecs.umich.edu{ 6556701Sgblack@eecs.umich.edu int index = 0; 6566701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 6576701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 6586701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 6596701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 6605877Shsul@eecs.umich.edu 6615877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6625877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6635877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6645877Shsul@eecs.umich.edu return -EINVAL; 6655877Shsul@eecs.umich.edu } 6665877Shsul@eecs.umich.edu 6675877Shsul@eecs.umich.edu if (new_length > old_length) { 6685877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6695877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6705877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6715877Shsul@eecs.umich.edu process->mmap_end += diff; 6725877Shsul@eecs.umich.edu return start; 6735877Shsul@eecs.umich.edu } else { 6745877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6755877Shsul@eecs.umich.edu if (!(flags & 1)) { 6765877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6775877Shsul@eecs.umich.edu return -ENOMEM; 6785877Shsul@eecs.umich.edu } else { 6795877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6805877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6815877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6825877Shsul@eecs.umich.edu start = process->mmap_end; 6835877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6845877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6855877Shsul@eecs.umich.edu process->mmap_end += new_length; 6865877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6875877Shsul@eecs.umich.edu return start; 6885877Shsul@eecs.umich.edu } 6895877Shsul@eecs.umich.edu } 6905877Shsul@eecs.umich.edu } else { 6915877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 6925877Shsul@eecs.umich.edu new_length); 6935877Shsul@eecs.umich.edu return start; 6945877Shsul@eecs.umich.edu } 6955877Shsul@eecs.umich.edu} 6961999SN/A 697378SN/A/// Target stat() handler. 698360SN/Atemplate <class OS> 6991450SN/ASyscallReturn 7003114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7012680Sktlim@umich.edu ThreadContext *tc) 702360SN/A{ 703360SN/A std::string path; 704360SN/A 7056701Sgblack@eecs.umich.edu int index = 0; 7066701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7076701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7086701Sgblack@eecs.umich.edu return -EFAULT; 7096701Sgblack@eecs.umich.edu } 7106701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 711360SN/A 7123669Sbinkertn@umich.edu // Adjust path for current working directory 7133669Sbinkertn@umich.edu path = process->fullPath(path); 7143669Sbinkertn@umich.edu 715360SN/A struct stat hostBuf; 716360SN/A int result = stat(path.c_str(), &hostBuf); 717360SN/A 718360SN/A if (result < 0) 7192218SN/A return -errno; 720360SN/A 7216701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 722360SN/A 7231458SN/A return 0; 724360SN/A} 725360SN/A 726360SN/A 7275074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7285074Ssaidi@eecs.umich.edutemplate <class OS> 7295074Ssaidi@eecs.umich.eduSyscallReturn 7305074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7315074Ssaidi@eecs.umich.edu ThreadContext *tc) 7325074Ssaidi@eecs.umich.edu{ 7335074Ssaidi@eecs.umich.edu std::string path; 7345074Ssaidi@eecs.umich.edu 7356701Sgblack@eecs.umich.edu int index = 0; 7366701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7376701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 7385074Ssaidi@eecs.umich.edu return -EFAULT; 7396701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7405074Ssaidi@eecs.umich.edu 7415074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7425074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7435074Ssaidi@eecs.umich.edu 7445208Ssaidi@eecs.umich.edu#if NO_STAT64 7455208Ssaidi@eecs.umich.edu struct stat hostBuf; 7465208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7475208Ssaidi@eecs.umich.edu#else 7485074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7495074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7505208Ssaidi@eecs.umich.edu#endif 7515074Ssaidi@eecs.umich.edu 7525074Ssaidi@eecs.umich.edu if (result < 0) 7535074Ssaidi@eecs.umich.edu return -errno; 7545074Ssaidi@eecs.umich.edu 7556701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 7565074Ssaidi@eecs.umich.edu 7575074Ssaidi@eecs.umich.edu return 0; 7585074Ssaidi@eecs.umich.edu} 7595074Ssaidi@eecs.umich.edu 7605074Ssaidi@eecs.umich.edu 7611999SN/A/// Target fstat64() handler. 7621999SN/Atemplate <class OS> 7631999SN/ASyscallReturn 7643114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7652680Sktlim@umich.edu ThreadContext *tc) 7661999SN/A{ 7676701Sgblack@eecs.umich.edu int index = 0; 7686701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 7696701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7701999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7711999SN/A // doesn't map to any simulator fd: not a valid target fd 7721999SN/A return -EBADF; 7731999SN/A } 7741999SN/A 7752764Sstever@eecs.umich.edu#if NO_STAT64 7762064SN/A struct stat hostBuf; 7772064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7782064SN/A#else 7792064SN/A struct stat64 hostBuf; 7801999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7812064SN/A#endif 7821999SN/A 7831999SN/A if (result < 0) 7842218SN/A return -errno; 7851999SN/A 7866701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 7871999SN/A 7881999SN/A return 0; 7891999SN/A} 7901999SN/A 7911999SN/A 792378SN/A/// Target lstat() handler. 793360SN/Atemplate <class OS> 7941450SN/ASyscallReturn 7953114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7962680Sktlim@umich.edu ThreadContext *tc) 797360SN/A{ 798360SN/A std::string path; 799360SN/A 8006701Sgblack@eecs.umich.edu int index = 0; 8016701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8026701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8036701Sgblack@eecs.umich.edu return -EFAULT; 8046701Sgblack@eecs.umich.edu } 8056701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 806360SN/A 8073669Sbinkertn@umich.edu // Adjust path for current working directory 8083669Sbinkertn@umich.edu path = process->fullPath(path); 8093669Sbinkertn@umich.edu 810360SN/A struct stat hostBuf; 811360SN/A int result = lstat(path.c_str(), &hostBuf); 812360SN/A 813360SN/A if (result < 0) 8141458SN/A return -errno; 815360SN/A 8166701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 817360SN/A 8181458SN/A return 0; 819360SN/A} 820360SN/A 8211999SN/A/// Target lstat64() handler. 8221999SN/Atemplate <class OS> 8231999SN/ASyscallReturn 8243114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 8252680Sktlim@umich.edu ThreadContext *tc) 8261999SN/A{ 8271999SN/A std::string path; 8281999SN/A 8296701Sgblack@eecs.umich.edu int index = 0; 8306701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8316701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8326701Sgblack@eecs.umich.edu return -EFAULT; 8336701Sgblack@eecs.umich.edu } 8346701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8351999SN/A 8363669Sbinkertn@umich.edu // Adjust path for current working directory 8373669Sbinkertn@umich.edu path = process->fullPath(path); 8383669Sbinkertn@umich.edu 8392764Sstever@eecs.umich.edu#if NO_STAT64 8402064SN/A struct stat hostBuf; 8412064SN/A int result = lstat(path.c_str(), &hostBuf); 8422064SN/A#else 8431999SN/A struct stat64 hostBuf; 8441999SN/A int result = lstat64(path.c_str(), &hostBuf); 8452064SN/A#endif 8461999SN/A 8471999SN/A if (result < 0) 8481999SN/A return -errno; 8491999SN/A 8506701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 8511999SN/A 8521999SN/A return 0; 8531999SN/A} 8541999SN/A 855378SN/A/// Target fstat() handler. 856360SN/Atemplate <class OS> 8571450SN/ASyscallReturn 8583114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8592680Sktlim@umich.edu ThreadContext *tc) 860360SN/A{ 8616701Sgblack@eecs.umich.edu int index = 0; 8626701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 8636701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 864360SN/A 8651969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 866360SN/A 867360SN/A if (fd < 0) 8681458SN/A return -EBADF; 869360SN/A 870360SN/A struct stat hostBuf; 871360SN/A int result = fstat(fd, &hostBuf); 872360SN/A 873360SN/A if (result < 0) 8741458SN/A return -errno; 875360SN/A 8766701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 8772021SN/A 8781458SN/A return 0; 879360SN/A} 880360SN/A 881360SN/A 8821706SN/A/// Target statfs() handler. 8831706SN/Atemplate <class OS> 8841706SN/ASyscallReturn 8853114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8862680Sktlim@umich.edu ThreadContext *tc) 8871706SN/A{ 8881706SN/A std::string path; 8891706SN/A 8906701Sgblack@eecs.umich.edu int index = 0; 8916701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8926701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8936701Sgblack@eecs.umich.edu return -EFAULT; 8946701Sgblack@eecs.umich.edu } 8956701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8961706SN/A 8973669Sbinkertn@umich.edu // Adjust path for current working directory 8983669Sbinkertn@umich.edu path = process->fullPath(path); 8993669Sbinkertn@umich.edu 9001706SN/A struct statfs hostBuf; 9011706SN/A int result = statfs(path.c_str(), &hostBuf); 9021706SN/A 9031706SN/A if (result < 0) 9042218SN/A return -errno; 9051706SN/A 9066701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9071706SN/A 9081706SN/A return 0; 9091706SN/A} 9101706SN/A 9111706SN/A 9121706SN/A/// Target fstatfs() handler. 9131706SN/Atemplate <class OS> 9141706SN/ASyscallReturn 9153114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9162680Sktlim@umich.edu ThreadContext *tc) 9171706SN/A{ 9186701Sgblack@eecs.umich.edu int index = 0; 9196701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 9206701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9211706SN/A 9221706SN/A if (fd < 0) 9231706SN/A return -EBADF; 9241706SN/A 9251706SN/A struct statfs hostBuf; 9261706SN/A int result = fstatfs(fd, &hostBuf); 9271706SN/A 9281706SN/A if (result < 0) 9292218SN/A return -errno; 9301706SN/A 9316701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9321706SN/A 9331706SN/A return 0; 9341706SN/A} 9351706SN/A 9361706SN/A 9371999SN/A/// Target writev() handler. 9381999SN/Atemplate <class OS> 9391999SN/ASyscallReturn 9403114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9412680Sktlim@umich.edu ThreadContext *tc) 9421999SN/A{ 9436701Sgblack@eecs.umich.edu int index = 0; 9446701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 9451999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9461999SN/A // doesn't map to any simulator fd: not a valid target fd 9471999SN/A return -EBADF; 9481999SN/A } 9491999SN/A 9502680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9516701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 9526701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 9531999SN/A struct iovec hiov[count]; 9546227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9551999SN/A typename OS::tgt_iovec tiov; 9562461SN/A 9572461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9582461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9592091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9601999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9612461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9622461SN/A hiov[i].iov_len); 9631999SN/A } 9641999SN/A 9651999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9661999SN/A 9676227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9681999SN/A delete [] (char *)hiov[i].iov_base; 9691999SN/A 9701999SN/A if (result < 0) 9712218SN/A return -errno; 9721999SN/A 9731999SN/A return 0; 9741999SN/A} 9751999SN/A 9761999SN/A 977378SN/A/// Target mmap() handler. 978378SN/A/// 979378SN/A/// We don't really handle mmap(). If the target is mmaping an 980378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 981378SN/A/// nothing (since memory is initialized to zero and the simulator 982378SN/A/// doesn't really check addresses anyway). Always print a warning, 983378SN/A/// since this could be seriously broken if we're not mapping 984378SN/A/// /dev/zero. 985360SN/A// 986378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 987378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 988378SN/A/// anything else. 989360SN/Atemplate <class OS> 9901450SN/ASyscallReturn 9913114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 992360SN/A{ 9936701Sgblack@eecs.umich.edu int index = 0; 9946701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 9956701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 9966701Sgblack@eecs.umich.edu index++; // int prot = p->getSyscallArg(tc, index); 9976701Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, index); 9986701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 9996701Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, index); 1000360SN/A 10015877Shsul@eecs.umich.edu 10022544SN/A if ((start % TheISA::VMPageSize) != 0 || 10032544SN/A (length % TheISA::VMPageSize) != 0) { 10042544SN/A warn("mmap failing: arguments not page-aligned: " 10052544SN/A "start 0x%x length 0x%x", 10062544SN/A start, length); 10072544SN/A return -EINVAL; 1008360SN/A } 1009360SN/A 10102544SN/A if (start != 0) { 10112544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 10122544SN/A start, p->mmap_end); 10132544SN/A } 10142544SN/A 10152544SN/A // pick next address from our "mmap region" 10166672Sgblack@eecs.umich.edu if (OS::mmapGrowsDown()) { 10176672Sgblack@eecs.umich.edu start = p->mmap_end - length; 10186672Sgblack@eecs.umich.edu p->mmap_end = start; 10196672Sgblack@eecs.umich.edu } else { 10206672Sgblack@eecs.umich.edu start = p->mmap_end; 10216672Sgblack@eecs.umich.edu p->mmap_end += length; 10226672Sgblack@eecs.umich.edu } 10232544SN/A p->pTable->allocate(start, length); 10242544SN/A 10252553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 10261969SN/A warn("allowing mmap of file @ fd %d. " 10276701Sgblack@eecs.umich.edu "This will break if not /dev/zero.", fd); 1028360SN/A } 1029360SN/A 10301458SN/A return start; 1031360SN/A} 1032360SN/A 1033378SN/A/// Target getrlimit() handler. 1034360SN/Atemplate <class OS> 10351450SN/ASyscallReturn 10363114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10372680Sktlim@umich.edu ThreadContext *tc) 1038360SN/A{ 10396701Sgblack@eecs.umich.edu int index = 0; 10406701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 10416701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1042360SN/A 1043360SN/A switch (resource) { 10442064SN/A case OS::TGT_RLIMIT_STACK: 10455877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10462064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10472091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10482091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10492064SN/A break; 1050360SN/A 10515877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10525877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10535877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10545877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10555877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10565877Shsul@eecs.umich.edu break; 10575877Shsul@eecs.umich.edu 10582064SN/A default: 10592064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10602064SN/A << std::endl; 10612064SN/A abort(); 10622064SN/A break; 1063360SN/A } 1064360SN/A 10652680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10661458SN/A return 0; 1067360SN/A} 1068360SN/A 1069378SN/A/// Target gettimeofday() handler. 1070360SN/Atemplate <class OS> 10711450SN/ASyscallReturn 10723114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10732680Sktlim@umich.edu ThreadContext *tc) 1074360SN/A{ 10756701Sgblack@eecs.umich.edu int index = 0; 10766701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1077360SN/A 1078360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1079360SN/A tp->tv_sec += seconds_since_epoch; 10806109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10816109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1082360SN/A 10832680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1084360SN/A 10851458SN/A return 0; 1086360SN/A} 1087360SN/A 1088360SN/A 10891999SN/A/// Target utimes() handler. 10901999SN/Atemplate <class OS> 10911999SN/ASyscallReturn 10923114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10932680Sktlim@umich.edu ThreadContext *tc) 10941999SN/A{ 10951999SN/A std::string path; 10961999SN/A 10976701Sgblack@eecs.umich.edu int index = 0; 10986701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 10996701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11006701Sgblack@eecs.umich.edu return -EFAULT; 11016701Sgblack@eecs.umich.edu } 11021999SN/A 11036701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 11046701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 11052680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 11061999SN/A 11071999SN/A struct timeval hostTimeval[2]; 11081999SN/A for (int i = 0; i < 2; ++i) 11091999SN/A { 11102091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 11112091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 11121999SN/A } 11133669Sbinkertn@umich.edu 11143669Sbinkertn@umich.edu // Adjust path for current working directory 11153669Sbinkertn@umich.edu path = process->fullPath(path); 11163669Sbinkertn@umich.edu 11171999SN/A int result = utimes(path.c_str(), hostTimeval); 11181999SN/A 11191999SN/A if (result < 0) 11201999SN/A return -errno; 11211999SN/A 11221999SN/A return 0; 11231999SN/A} 1124378SN/A/// Target getrusage() function. 1125360SN/Atemplate <class OS> 11261450SN/ASyscallReturn 11273114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11282680Sktlim@umich.edu ThreadContext *tc) 1129360SN/A{ 11306701Sgblack@eecs.umich.edu int index = 0; 11316701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 11326701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1133360SN/A 11343670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 11353670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1136360SN/A rup->ru_stime.tv_sec = 0; 1137360SN/A rup->ru_stime.tv_usec = 0; 1138360SN/A rup->ru_maxrss = 0; 1139360SN/A rup->ru_ixrss = 0; 1140360SN/A rup->ru_idrss = 0; 1141360SN/A rup->ru_isrss = 0; 1142360SN/A rup->ru_minflt = 0; 1143360SN/A rup->ru_majflt = 0; 1144360SN/A rup->ru_nswap = 0; 1145360SN/A rup->ru_inblock = 0; 1146360SN/A rup->ru_oublock = 0; 1147360SN/A rup->ru_msgsnd = 0; 1148360SN/A rup->ru_msgrcv = 0; 1149360SN/A rup->ru_nsignals = 0; 1150360SN/A rup->ru_nvcsw = 0; 1151360SN/A rup->ru_nivcsw = 0; 1152360SN/A 11533670Sbinkertn@umich.edu switch (who) { 11543670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11553670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11563670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11573670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11583670Sbinkertn@umich.edu break; 11593670Sbinkertn@umich.edu 11603670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11613670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11623670Sbinkertn@umich.edu break; 11633670Sbinkertn@umich.edu 11643670Sbinkertn@umich.edu default: 11653670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11663670Sbinkertn@umich.edu // plow ahead 11673670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11683670Sbinkertn@umich.edu who); 11693670Sbinkertn@umich.edu } 11703670Sbinkertn@umich.edu 11712680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1172360SN/A 11731458SN/A return 0; 1174360SN/A} 1175360SN/A 11766683Stjones1@inf.ed.ac.uk/// Target times() function. 11776683Stjones1@inf.ed.ac.uktemplate <class OS> 11786683Stjones1@inf.ed.ac.ukSyscallReturn 11796683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11806683Stjones1@inf.ed.ac.uk ThreadContext *tc) 11816683Stjones1@inf.ed.ac.uk{ 11826701Sgblack@eecs.umich.edu int index = 0; 11836701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 11846683Stjones1@inf.ed.ac.uk 11856683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 11866683Stjones1@inf.ed.ac.uk int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s; 11876683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 11886683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 11896683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 11906683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 11916683Stjones1@inf.ed.ac.uk 11926683Stjones1@inf.ed.ac.uk // Convert to host endianness 11936683Stjones1@inf.ed.ac.uk bufp->tms_utime = htog(bufp->tms_utime); 11946683Stjones1@inf.ed.ac.uk 11956683Stjones1@inf.ed.ac.uk // Write back 11966683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemPort()); 11976683Stjones1@inf.ed.ac.uk 11986683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 11996683Stjones1@inf.ed.ac.uk return clocks; 12006683Stjones1@inf.ed.ac.uk} 12012553SN/A 12026684Stjones1@inf.ed.ac.uk/// Target time() function. 12036684Stjones1@inf.ed.ac.uktemplate <class OS> 12046684Stjones1@inf.ed.ac.ukSyscallReturn 12056684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 12066684Stjones1@inf.ed.ac.uk ThreadContext *tc) 12076684Stjones1@inf.ed.ac.uk{ 12086684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 12096684Stjones1@inf.ed.ac.uk getElapsedTime(sec, usec); 12106684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 12116684Stjones1@inf.ed.ac.uk 12126701Sgblack@eecs.umich.edu int index = 0; 12136701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 12146684Stjones1@inf.ed.ac.uk if(taddr != 0) { 12156684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 12166684Stjones1@inf.ed.ac.uk t = htog(t); 12176684Stjones1@inf.ed.ac.uk TranslatingPort *p = tc->getMemPort(); 12186684Stjones1@inf.ed.ac.uk p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 12196684Stjones1@inf.ed.ac.uk } 12206684Stjones1@inf.ed.ac.uk return sec; 12216684Stjones1@inf.ed.ac.uk} 12222553SN/A 12232553SN/A 12241354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1225