syscall_emul.hh revision 7823
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 441809SN/A#ifdef __CYGWIN32__ 455543Ssaidi@eecs.umich.edu#include <sys/fcntl.h> // for O_BINARY 461809SN/A#endif 473113Sgblack@eecs.umich.edu#include <sys/stat.h> 487075Snate@binkert.org#include <errno.h> 493113Sgblack@eecs.umich.edu#include <fcntl.h> 501999SN/A#include <sys/uio.h> 517075Snate@binkert.org#include <sys/time.h> 527075Snate@binkert.org 537075Snate@binkert.org#include <string> 54360SN/A 552474SN/A#include "base/chunk_generator.hh" 565543Ssaidi@eecs.umich.edu#include "base/intmath.hh" // for RoundUp 572462SN/A#include "base/misc.hh" 581354SN/A#include "base/trace.hh" 596216Snate@binkert.org#include "base/types.hh" 606658Snate@binkert.org#include "config/the_isa.hh" 612474SN/A#include "cpu/base.hh" 622680Sktlim@umich.edu#include "cpu/thread_context.hh" 632474SN/A#include "mem/translating_port.hh" 642474SN/A#include "mem/page_table.hh" 657678Sgblack@eecs.umich.edu#include "sim/byteswap.hh" 666640Svince@csl.cornell.edu#include "sim/system.hh" 671354SN/A#include "sim/process.hh" 68360SN/A 69360SN/A/// 70360SN/A/// System call descriptor. 71360SN/A/// 72360SN/Aclass SyscallDesc { 73360SN/A 74360SN/A public: 75360SN/A 76378SN/A /// Typedef for target syscall handler functions. 771450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 783114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 79360SN/A 805543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 815543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 825543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 83360SN/A 84360SN/A /// Flag values for controlling syscall behavior. 85360SN/A enum Flags { 86360SN/A /// Don't set return regs according to funcPtr return value. 87360SN/A /// Used for syscalls with non-standard return conventions 882680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 89360SN/A /// sigreturn). 90360SN/A SuppressReturnValue = 1 91360SN/A }; 92360SN/A 93360SN/A /// Constructor. 94360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 95360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 96360SN/A { 97360SN/A } 98360SN/A 99360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 1003114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 101360SN/A}; 102360SN/A 103360SN/A 104360SN/Aclass BaseBufferArg { 105360SN/A 106360SN/A public: 107360SN/A 108360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 109360SN/A { 110360SN/A bufPtr = new uint8_t[size]; 111360SN/A // clear out buffer: in case we only partially populate this, 112360SN/A // and then do a copyOut(), we want to make sure we don't 113360SN/A // introduce any random junk into the simulated address space 114360SN/A memset(bufPtr, 0, size); 115360SN/A } 116360SN/A 117360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 118360SN/A 119360SN/A // 120360SN/A // copy data into simulator space (read from target memory) 121360SN/A // 1222400SN/A virtual bool copyIn(TranslatingPort *memport) 123360SN/A { 1242461SN/A memport->readBlob(addr, bufPtr, size); 1255543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 126360SN/A } 127360SN/A 128360SN/A // 129360SN/A // copy data out of simulator space (write to target memory) 130360SN/A // 1312400SN/A virtual bool copyOut(TranslatingPort *memport) 132360SN/A { 1332461SN/A memport->writeBlob(addr, bufPtr, size); 1345543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 135360SN/A } 136360SN/A 137360SN/A protected: 138360SN/A Addr addr; 139360SN/A int size; 140360SN/A uint8_t *bufPtr; 141360SN/A}; 142360SN/A 143360SN/A 144360SN/Aclass BufferArg : public BaseBufferArg 145360SN/A{ 146360SN/A public: 147360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1485543Ssaidi@eecs.umich.edu void *bufferPtr() { return bufPtr; } 149360SN/A}; 150360SN/A 151360SN/Atemplate <class T> 152360SN/Aclass TypedBufferArg : public BaseBufferArg 153360SN/A{ 154360SN/A public: 155360SN/A // user can optionally specify a specific number of bytes to 156360SN/A // allocate to deal with those structs that have variable-size 157360SN/A // arrays at the end 158360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 159360SN/A : BaseBufferArg(_addr, _size) 160360SN/A { } 161360SN/A 162360SN/A // type case 163360SN/A operator T*() { return (T *)bufPtr; } 164360SN/A 165360SN/A // dereference operators 1665543Ssaidi@eecs.umich.edu T &operator*() { return *((T *)bufPtr); } 1675543Ssaidi@eecs.umich.edu T* operator->() { return (T *)bufPtr; } 168502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 169360SN/A}; 170360SN/A 171360SN/A////////////////////////////////////////////////////////////////////// 172360SN/A// 173360SN/A// The following emulation functions are generic enough that they 174360SN/A// don't need to be recompiled for different emulated OS's. They are 175360SN/A// defined in sim/syscall_emul.cc. 176360SN/A// 177360SN/A////////////////////////////////////////////////////////////////////// 178360SN/A 179360SN/A 180378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1811706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1823114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 183378SN/A 184378SN/A/// Handler for unimplemented syscalls that we never intend to 185378SN/A/// implement (signal handling, etc.) and should not affect the correct 186378SN/A/// behavior of the program. Print a warning only if the appropriate 187378SN/A/// trace flag is enabled. Return success to the target program. 1881706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1893114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 190360SN/A 1916109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1921706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1933114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 194378SN/A 1956109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1966109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1976109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 1986109Ssanchezd@stanford.edu 199378SN/A/// Target getpagesize() handler. 2001706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 2013114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 202378SN/A 2035748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2045748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2055748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 206378SN/A 207378SN/A/// Target close() handler. 2081706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2093114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 210378SN/A 211378SN/A/// Target read() handler. 2121706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2133114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 214378SN/A 215378SN/A/// Target write() handler. 2161706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2173114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 218378SN/A 219378SN/A/// Target lseek() handler. 2201706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2213114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 222378SN/A 2234118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2244118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2254118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2264118Sgblack@eecs.umich.edu 227378SN/A/// Target munmap() handler. 2281706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2293114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 230378SN/A 231378SN/A/// Target gethostname() handler. 2321706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2333114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 234360SN/A 2355513SMichael.Adler@intel.com/// Target getcwd() handler. 2365513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2375513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2385513SMichael.Adler@intel.com 2395513SMichael.Adler@intel.com/// Target unlink() handler. 2405513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2415513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2425513SMichael.Adler@intel.com 243511SN/A/// Target unlink() handler. 2441706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2453114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 246511SN/A 2475513SMichael.Adler@intel.com/// Target mkdir() handler. 2485513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2495513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2505513SMichael.Adler@intel.com 251511SN/A/// Target rename() handler. 2521706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2533114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2541706SN/A 2551706SN/A 2561706SN/A/// Target truncate() handler. 2571706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2583114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2591706SN/A 2601706SN/A 2611706SN/A/// Target ftruncate() handler. 2621706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2633114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2641706SN/A 265511SN/A 2666703Svince@csl.cornell.edu/// Target truncate64() handler. 2676703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2686703Svince@csl.cornell.edu LiveProcess *p, ThreadContext *tc); 2696703Svince@csl.cornell.edu 2706685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2716685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2726685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2736685Stjones1@inf.ed.ac.uk 2746685Stjones1@inf.ed.ac.uk 2755513SMichael.Adler@intel.com/// Target umask() handler. 2765513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2775513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2785513SMichael.Adler@intel.com 2795513SMichael.Adler@intel.com 2801999SN/A/// Target chown() handler. 2811999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2823114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2831999SN/A 2841999SN/A 2851999SN/A/// Target fchown() handler. 2861999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2873114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2881999SN/A 2893079Sstever@eecs.umich.edu/// Target dup() handler. 2903079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2913114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2923079Sstever@eecs.umich.edu 2932093SN/A/// Target fnctl() handler. 2942093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2953114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2962093SN/A 2972687Sksewell@umich.edu/// Target fcntl64() handler. 2982687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2993114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3002687Sksewell@umich.edu 3012238SN/A/// Target setuid() handler. 3022238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 3033114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3042238SN/A 3052238SN/A/// Target getpid() handler. 3062238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3073114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3082238SN/A 3092238SN/A/// Target getuid() handler. 3102238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3113114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3122238SN/A 3132238SN/A/// Target getgid() handler. 3142238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3153114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3162238SN/A 3172238SN/A/// Target getppid() handler. 3182238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3193114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3202238SN/A 3212238SN/A/// Target geteuid() handler. 3222238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3233114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3242238SN/A 3252238SN/A/// Target getegid() handler. 3262238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3273114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3282238SN/A 3296109Ssanchezd@stanford.edu/// Target clone() handler. 3306109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3316109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3322238SN/A 3332238SN/A 3342238SN/A/// Pseudo Funcs - These functions use a different return convension, 3352238SN/A/// returning a second value in a register other than the normal return register 3362238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3373114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3382238SN/A 3392238SN/A/// Target getpidPseudo() handler. 3402238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3413114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3422238SN/A 3432238SN/A/// Target getuidPseudo() handler. 3442238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3453114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3462238SN/A 3472238SN/A/// Target getgidPseudo() handler. 3482238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3493114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3502238SN/A 3512238SN/A 3521354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3531354SN/Aconst int one_million = 1000000; 3541354SN/A 3551354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3561354SN/A/// by my reckoning. We want to keep this a constant (not use the 3571354SN/A/// real-world time) to keep simulations repeatable. 3581354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3591354SN/A 3601354SN/A/// Helper function to convert current elapsed time to seconds and 3611354SN/A/// microseconds. 3621354SN/Atemplate <class T1, class T2> 3631354SN/Avoid 3641354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3651354SN/A{ 3667823Ssteve.reinhardt@amd.com int elapsed_usecs = curTick() / SimClock::Int::us; 3671354SN/A sec = elapsed_usecs / one_million; 3681354SN/A usec = elapsed_usecs % one_million; 3691354SN/A} 3701354SN/A 371360SN/A////////////////////////////////////////////////////////////////////// 372360SN/A// 373360SN/A// The following emulation functions are generic, but need to be 374360SN/A// templated to account for differences in types, constants, etc. 375360SN/A// 376360SN/A////////////////////////////////////////////////////////////////////// 377360SN/A 3783113Sgblack@eecs.umich.edu#if NO_STAT64 3793113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3803113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3813113Sgblack@eecs.umich.edu#else 3823113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3833113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3843113Sgblack@eecs.umich.edu#endif 3853113Sgblack@eecs.umich.edu 3863113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3873113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3883113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3893113Sgblack@eecs.umich.edu 3903113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3913113Sgblack@eecs.umich.edustatic void 3923113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3933113Sgblack@eecs.umich.edu{ 3944189Sgblack@eecs.umich.edu using namespace TheISA; 3954189Sgblack@eecs.umich.edu 3963113Sgblack@eecs.umich.edu if (fakeTTY) 3973113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 3983113Sgblack@eecs.umich.edu else 3993113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 4003113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 4013113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 4023113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 4033277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4045515SMichael.Adler@intel.com if (fakeTTY) { 4055515SMichael.Adler@intel.com // Claim to be a character device 4065515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4075515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4085515SMichael.Adler@intel.com } 4093277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 4103277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4113277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 4123277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4133277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4143277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4153277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4163113Sgblack@eecs.umich.edu if (fakeTTY) 4173113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4183113Sgblack@eecs.umich.edu else 4193113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4203113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4213113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4223113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4233114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4243113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4253114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4263113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4273114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4283113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4294061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4304061Sgblack@eecs.umich.edu // consistently across different hosts. 4314061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4323113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4333113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4343113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4353113Sgblack@eecs.umich.edu} 4363113Sgblack@eecs.umich.edu 4373113Sgblack@eecs.umich.edu// Same for stat64 4383113Sgblack@eecs.umich.edu 4393113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4403113Sgblack@eecs.umich.edustatic void 4413113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4423113Sgblack@eecs.umich.edu{ 4434189Sgblack@eecs.umich.edu using namespace TheISA; 4444189Sgblack@eecs.umich.edu 4453113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4463113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4473113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4483113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4493113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4503113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4513113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4523113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4533113Sgblack@eecs.umich.edu#else 4543113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4553113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4563113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4573113Sgblack@eecs.umich.edu#endif 4583113Sgblack@eecs.umich.edu} 4593113Sgblack@eecs.umich.edu 4603113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4613113Sgblack@eecs.umich.edutemplate<class OS> 4623113Sgblack@eecs.umich.edustatic void 4633113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4643113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4653113Sgblack@eecs.umich.edu{ 4663113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4673113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4683113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4693113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4703113Sgblack@eecs.umich.edu} 4713113Sgblack@eecs.umich.edu 4723113Sgblack@eecs.umich.edutemplate<class OS> 4733113Sgblack@eecs.umich.edustatic void 4743113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4753113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4763113Sgblack@eecs.umich.edu{ 4773113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4783113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4796686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4803113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4813113Sgblack@eecs.umich.edu} 4823113Sgblack@eecs.umich.edu 483378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 484378SN/A/// only to find out if their stdout is a tty, to determine whether to 485378SN/A/// do line or block buffering. 486360SN/Atemplate <class OS> 4871450SN/ASyscallReturn 4883114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4892680Sktlim@umich.edu ThreadContext *tc) 490360SN/A{ 4916701Sgblack@eecs.umich.edu int index = 0; 4926701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 4936701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 494360SN/A 4951969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 496360SN/A 497360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 498360SN/A // doesn't map to any simulator fd: not a valid target fd 4991458SN/A return -EBADF; 500360SN/A } 501360SN/A 502360SN/A switch (req) { 5034131Sbinkertn@umich.edu case OS::TIOCISATTY_: 5044131Sbinkertn@umich.edu case OS::TIOCGETP_: 5054131Sbinkertn@umich.edu case OS::TIOCSETP_: 5064131Sbinkertn@umich.edu case OS::TIOCSETN_: 5074131Sbinkertn@umich.edu case OS::TIOCSETC_: 5084131Sbinkertn@umich.edu case OS::TIOCGETC_: 5094131Sbinkertn@umich.edu case OS::TIOCGETS_: 5104131Sbinkertn@umich.edu case OS::TIOCGETA_: 5116689Stjones1@inf.ed.ac.uk case OS::TCSETAW_: 5121458SN/A return -ENOTTY; 513360SN/A 514360SN/A default: 5157720Sgblack@eecs.umich.edu fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n", 5167720Sgblack@eecs.umich.edu fd, req, tc->pcState()); 517360SN/A } 518360SN/A} 519360SN/A 520378SN/A/// Target open() handler. 521360SN/Atemplate <class OS> 5221450SN/ASyscallReturn 5233114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5242680Sktlim@umich.edu ThreadContext *tc) 525360SN/A{ 526360SN/A std::string path; 527360SN/A 5286701Sgblack@eecs.umich.edu int index = 0; 5296701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 5306701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 5311458SN/A return -EFAULT; 532360SN/A 533360SN/A if (path == "/dev/sysdev0") { 534360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 535360SN/A // We don't support it, so just punt. 5361706SN/A warn("Ignoring open(%s, ...)\n", path); 5371458SN/A return -ENOENT; 538360SN/A } 539360SN/A 5406701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 5416701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 542360SN/A int hostFlags = 0; 543360SN/A 544360SN/A // translate open flags 545360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 546360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 547360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 548360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 549360SN/A } 550360SN/A } 551360SN/A 552360SN/A // any target flags left? 553360SN/A if (tgtFlags != 0) 5541706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 555360SN/A 556360SN/A#ifdef __CYGWIN32__ 557360SN/A hostFlags |= O_BINARY; 558360SN/A#endif 559360SN/A 5603669Sbinkertn@umich.edu // Adjust path for current working directory 5613669Sbinkertn@umich.edu path = process->fullPath(path); 5623669Sbinkertn@umich.edu 5631706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5641706SN/A 5655795Ssaidi@eecs.umich.edu int fd; 5665795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5675795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5685795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5695795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5705795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5715795Ssaidi@eecs.umich.edu } else { 5725795Ssaidi@eecs.umich.edu // open the file 5735795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5745795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5755795Ssaidi@eecs.umich.edu } 576360SN/A 577360SN/A} 578360SN/A 5796640Svince@csl.cornell.edu/// Target sysinfo() handler. 5806640Svince@csl.cornell.edutemplate <class OS> 5816640Svince@csl.cornell.eduSyscallReturn 5826640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5836640Svince@csl.cornell.edu ThreadContext *tc) 5846640Svince@csl.cornell.edu{ 5856640Svince@csl.cornell.edu 5866701Sgblack@eecs.umich.edu int index = 0; 5876701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 5886701Sgblack@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 5896640Svince@csl.cornell.edu 5906701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 5916701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 5926640Svince@csl.cornell.edu 5936701Sgblack@eecs.umich.edu sysinfo.copyOut(tc->getMemPort()); 5946640Svince@csl.cornell.edu 5956701Sgblack@eecs.umich.edu return 0; 5966640Svince@csl.cornell.edu} 597360SN/A 5981999SN/A/// Target chmod() handler. 5991999SN/Atemplate <class OS> 6001999SN/ASyscallReturn 6013114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6022680Sktlim@umich.edu ThreadContext *tc) 6031999SN/A{ 6041999SN/A std::string path; 6051999SN/A 6066701Sgblack@eecs.umich.edu int index = 0; 6076701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 6086701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 6091999SN/A return -EFAULT; 6106701Sgblack@eecs.umich.edu } 6111999SN/A 6126701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6131999SN/A mode_t hostMode = 0; 6141999SN/A 6151999SN/A // XXX translate mode flags via OS::something??? 6161999SN/A hostMode = mode; 6171999SN/A 6183669Sbinkertn@umich.edu // Adjust path for current working directory 6193669Sbinkertn@umich.edu path = process->fullPath(path); 6203669Sbinkertn@umich.edu 6211999SN/A // do the chmod 6221999SN/A int result = chmod(path.c_str(), hostMode); 6231999SN/A if (result < 0) 6242218SN/A return -errno; 6251999SN/A 6261999SN/A return 0; 6271999SN/A} 6281999SN/A 6291999SN/A 6301999SN/A/// Target fchmod() handler. 6311999SN/Atemplate <class OS> 6321999SN/ASyscallReturn 6333114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6342680Sktlim@umich.edu ThreadContext *tc) 6351999SN/A{ 6366701Sgblack@eecs.umich.edu int index = 0; 6376701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6381999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6391999SN/A // doesn't map to any simulator fd: not a valid target fd 6401999SN/A return -EBADF; 6411999SN/A } 6421999SN/A 6436701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6441999SN/A mode_t hostMode = 0; 6451999SN/A 6461999SN/A // XXX translate mode flags via OS::someting??? 6471999SN/A hostMode = mode; 6481999SN/A 6491999SN/A // do the fchmod 6501999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6511999SN/A if (result < 0) 6522218SN/A return -errno; 6531999SN/A 6541999SN/A return 0; 6551999SN/A} 6561999SN/A 6575877Shsul@eecs.umich.edu/// Target mremap() handler. 6585877Shsul@eecs.umich.edutemplate <class OS> 6595877Shsul@eecs.umich.eduSyscallReturn 6605877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6615877Shsul@eecs.umich.edu{ 6626701Sgblack@eecs.umich.edu int index = 0; 6636701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 6646701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 6656701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 6666701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 6675877Shsul@eecs.umich.edu 6685877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6695877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6705877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6715877Shsul@eecs.umich.edu return -EINVAL; 6725877Shsul@eecs.umich.edu } 6735877Shsul@eecs.umich.edu 6745877Shsul@eecs.umich.edu if (new_length > old_length) { 6755877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6765877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6775877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6785877Shsul@eecs.umich.edu process->mmap_end += diff; 6795877Shsul@eecs.umich.edu return start; 6805877Shsul@eecs.umich.edu } else { 6815877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6825877Shsul@eecs.umich.edu if (!(flags & 1)) { 6835877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6845877Shsul@eecs.umich.edu return -ENOMEM; 6855877Shsul@eecs.umich.edu } else { 6865877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6875877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6885877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6895877Shsul@eecs.umich.edu start = process->mmap_end; 6905877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6915877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6925877Shsul@eecs.umich.edu process->mmap_end += new_length; 6935877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6945877Shsul@eecs.umich.edu return start; 6955877Shsul@eecs.umich.edu } 6965877Shsul@eecs.umich.edu } 6975877Shsul@eecs.umich.edu } else { 6985877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 6995877Shsul@eecs.umich.edu new_length); 7005877Shsul@eecs.umich.edu return start; 7015877Shsul@eecs.umich.edu } 7025877Shsul@eecs.umich.edu} 7031999SN/A 704378SN/A/// Target stat() handler. 705360SN/Atemplate <class OS> 7061450SN/ASyscallReturn 7073114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7082680Sktlim@umich.edu ThreadContext *tc) 709360SN/A{ 710360SN/A std::string path; 711360SN/A 7126701Sgblack@eecs.umich.edu int index = 0; 7136701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7146701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7156701Sgblack@eecs.umich.edu return -EFAULT; 7166701Sgblack@eecs.umich.edu } 7176701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 718360SN/A 7193669Sbinkertn@umich.edu // Adjust path for current working directory 7203669Sbinkertn@umich.edu path = process->fullPath(path); 7213669Sbinkertn@umich.edu 722360SN/A struct stat hostBuf; 723360SN/A int result = stat(path.c_str(), &hostBuf); 724360SN/A 725360SN/A if (result < 0) 7262218SN/A return -errno; 727360SN/A 7286701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 729360SN/A 7301458SN/A return 0; 731360SN/A} 732360SN/A 733360SN/A 7345074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7355074Ssaidi@eecs.umich.edutemplate <class OS> 7365074Ssaidi@eecs.umich.eduSyscallReturn 7375074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7385074Ssaidi@eecs.umich.edu ThreadContext *tc) 7395074Ssaidi@eecs.umich.edu{ 7405074Ssaidi@eecs.umich.edu std::string path; 7415074Ssaidi@eecs.umich.edu 7426701Sgblack@eecs.umich.edu int index = 0; 7436701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7446701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 7455074Ssaidi@eecs.umich.edu return -EFAULT; 7466701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7475074Ssaidi@eecs.umich.edu 7485074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7495074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7505074Ssaidi@eecs.umich.edu 7515208Ssaidi@eecs.umich.edu#if NO_STAT64 7525208Ssaidi@eecs.umich.edu struct stat hostBuf; 7535208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7545208Ssaidi@eecs.umich.edu#else 7555074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7565074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7575208Ssaidi@eecs.umich.edu#endif 7585074Ssaidi@eecs.umich.edu 7595074Ssaidi@eecs.umich.edu if (result < 0) 7605074Ssaidi@eecs.umich.edu return -errno; 7615074Ssaidi@eecs.umich.edu 7626701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 7635074Ssaidi@eecs.umich.edu 7645074Ssaidi@eecs.umich.edu return 0; 7655074Ssaidi@eecs.umich.edu} 7665074Ssaidi@eecs.umich.edu 7675074Ssaidi@eecs.umich.edu 7681999SN/A/// Target fstat64() handler. 7691999SN/Atemplate <class OS> 7701999SN/ASyscallReturn 7713114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7722680Sktlim@umich.edu ThreadContext *tc) 7731999SN/A{ 7746701Sgblack@eecs.umich.edu int index = 0; 7756701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 7766701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7771999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7781999SN/A // doesn't map to any simulator fd: not a valid target fd 7791999SN/A return -EBADF; 7801999SN/A } 7811999SN/A 7822764Sstever@eecs.umich.edu#if NO_STAT64 7832064SN/A struct stat hostBuf; 7842064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7852064SN/A#else 7862064SN/A struct stat64 hostBuf; 7871999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7882064SN/A#endif 7891999SN/A 7901999SN/A if (result < 0) 7912218SN/A return -errno; 7921999SN/A 7936701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 7941999SN/A 7951999SN/A return 0; 7961999SN/A} 7971999SN/A 7981999SN/A 799378SN/A/// Target lstat() handler. 800360SN/Atemplate <class OS> 8011450SN/ASyscallReturn 8023114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8032680Sktlim@umich.edu ThreadContext *tc) 804360SN/A{ 805360SN/A std::string path; 806360SN/A 8076701Sgblack@eecs.umich.edu int index = 0; 8086701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8096701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8106701Sgblack@eecs.umich.edu return -EFAULT; 8116701Sgblack@eecs.umich.edu } 8126701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 813360SN/A 8143669Sbinkertn@umich.edu // Adjust path for current working directory 8153669Sbinkertn@umich.edu path = process->fullPath(path); 8163669Sbinkertn@umich.edu 817360SN/A struct stat hostBuf; 818360SN/A int result = lstat(path.c_str(), &hostBuf); 819360SN/A 820360SN/A if (result < 0) 8211458SN/A return -errno; 822360SN/A 8236701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 824360SN/A 8251458SN/A return 0; 826360SN/A} 827360SN/A 8281999SN/A/// Target lstat64() handler. 8291999SN/Atemplate <class OS> 8301999SN/ASyscallReturn 8313114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 8322680Sktlim@umich.edu ThreadContext *tc) 8331999SN/A{ 8341999SN/A std::string path; 8351999SN/A 8366701Sgblack@eecs.umich.edu int index = 0; 8376701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8386701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8396701Sgblack@eecs.umich.edu return -EFAULT; 8406701Sgblack@eecs.umich.edu } 8416701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8421999SN/A 8433669Sbinkertn@umich.edu // Adjust path for current working directory 8443669Sbinkertn@umich.edu path = process->fullPath(path); 8453669Sbinkertn@umich.edu 8462764Sstever@eecs.umich.edu#if NO_STAT64 8472064SN/A struct stat hostBuf; 8482064SN/A int result = lstat(path.c_str(), &hostBuf); 8492064SN/A#else 8501999SN/A struct stat64 hostBuf; 8511999SN/A int result = lstat64(path.c_str(), &hostBuf); 8522064SN/A#endif 8531999SN/A 8541999SN/A if (result < 0) 8551999SN/A return -errno; 8561999SN/A 8576701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 8581999SN/A 8591999SN/A return 0; 8601999SN/A} 8611999SN/A 862378SN/A/// Target fstat() handler. 863360SN/Atemplate <class OS> 8641450SN/ASyscallReturn 8653114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8662680Sktlim@umich.edu ThreadContext *tc) 867360SN/A{ 8686701Sgblack@eecs.umich.edu int index = 0; 8696701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 8706701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 871360SN/A 8721969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 873360SN/A 874360SN/A if (fd < 0) 8751458SN/A return -EBADF; 876360SN/A 877360SN/A struct stat hostBuf; 878360SN/A int result = fstat(fd, &hostBuf); 879360SN/A 880360SN/A if (result < 0) 8811458SN/A return -errno; 882360SN/A 8836701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 8842021SN/A 8851458SN/A return 0; 886360SN/A} 887360SN/A 888360SN/A 8891706SN/A/// Target statfs() handler. 8901706SN/Atemplate <class OS> 8911706SN/ASyscallReturn 8923114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8932680Sktlim@umich.edu ThreadContext *tc) 8941706SN/A{ 8951706SN/A std::string path; 8961706SN/A 8976701Sgblack@eecs.umich.edu int index = 0; 8986701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8996701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9006701Sgblack@eecs.umich.edu return -EFAULT; 9016701Sgblack@eecs.umich.edu } 9026701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9031706SN/A 9043669Sbinkertn@umich.edu // Adjust path for current working directory 9053669Sbinkertn@umich.edu path = process->fullPath(path); 9063669Sbinkertn@umich.edu 9071706SN/A struct statfs hostBuf; 9081706SN/A int result = statfs(path.c_str(), &hostBuf); 9091706SN/A 9101706SN/A if (result < 0) 9112218SN/A return -errno; 9121706SN/A 9136701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9141706SN/A 9151706SN/A return 0; 9161706SN/A} 9171706SN/A 9181706SN/A 9191706SN/A/// Target fstatfs() handler. 9201706SN/Atemplate <class OS> 9211706SN/ASyscallReturn 9223114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9232680Sktlim@umich.edu ThreadContext *tc) 9241706SN/A{ 9256701Sgblack@eecs.umich.edu int index = 0; 9266701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 9276701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9281706SN/A 9291706SN/A if (fd < 0) 9301706SN/A return -EBADF; 9311706SN/A 9321706SN/A struct statfs hostBuf; 9331706SN/A int result = fstatfs(fd, &hostBuf); 9341706SN/A 9351706SN/A if (result < 0) 9362218SN/A return -errno; 9371706SN/A 9386701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9391706SN/A 9401706SN/A return 0; 9411706SN/A} 9421706SN/A 9431706SN/A 9441999SN/A/// Target writev() handler. 9451999SN/Atemplate <class OS> 9461999SN/ASyscallReturn 9473114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9482680Sktlim@umich.edu ThreadContext *tc) 9491999SN/A{ 9506701Sgblack@eecs.umich.edu int index = 0; 9516701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 9521999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9531999SN/A // doesn't map to any simulator fd: not a valid target fd 9541999SN/A return -EBADF; 9551999SN/A } 9561999SN/A 9572680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9586701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 9596701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 9601999SN/A struct iovec hiov[count]; 9616227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9621999SN/A typename OS::tgt_iovec tiov; 9632461SN/A 9642461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9652461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9662091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9671999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9682461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9692461SN/A hiov[i].iov_len); 9701999SN/A } 9711999SN/A 9721999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9731999SN/A 9746227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9751999SN/A delete [] (char *)hiov[i].iov_base; 9761999SN/A 9771999SN/A if (result < 0) 9782218SN/A return -errno; 9791999SN/A 9801999SN/A return 0; 9811999SN/A} 9821999SN/A 9831999SN/A 984378SN/A/// Target mmap() handler. 985378SN/A/// 986378SN/A/// We don't really handle mmap(). If the target is mmaping an 987378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 988378SN/A/// nothing (since memory is initialized to zero and the simulator 989378SN/A/// doesn't really check addresses anyway). Always print a warning, 990378SN/A/// since this could be seriously broken if we're not mapping 991378SN/A/// /dev/zero. 992360SN/A// 993378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 994378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 995378SN/A/// anything else. 996360SN/Atemplate <class OS> 9971450SN/ASyscallReturn 9983114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 999360SN/A{ 10006701Sgblack@eecs.umich.edu int index = 0; 10016701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 10026701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 10036701Sgblack@eecs.umich.edu index++; // int prot = p->getSyscallArg(tc, index); 10046701Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, index); 10056701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 10066701Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, index); 1007360SN/A 10085877Shsul@eecs.umich.edu 10092544SN/A if ((start % TheISA::VMPageSize) != 0 || 10102544SN/A (length % TheISA::VMPageSize) != 0) { 10112544SN/A warn("mmap failing: arguments not page-aligned: " 10122544SN/A "start 0x%x length 0x%x", 10132544SN/A start, length); 10142544SN/A return -EINVAL; 1015360SN/A } 1016360SN/A 10172544SN/A if (start != 0) { 10182544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 10192544SN/A start, p->mmap_end); 10202544SN/A } 10212544SN/A 10222544SN/A // pick next address from our "mmap region" 10236672Sgblack@eecs.umich.edu if (OS::mmapGrowsDown()) { 10246672Sgblack@eecs.umich.edu start = p->mmap_end - length; 10256672Sgblack@eecs.umich.edu p->mmap_end = start; 10266672Sgblack@eecs.umich.edu } else { 10276672Sgblack@eecs.umich.edu start = p->mmap_end; 10286672Sgblack@eecs.umich.edu p->mmap_end += length; 10296672Sgblack@eecs.umich.edu } 10302544SN/A p->pTable->allocate(start, length); 10312544SN/A 10322553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 10331969SN/A warn("allowing mmap of file @ fd %d. " 10346701Sgblack@eecs.umich.edu "This will break if not /dev/zero.", fd); 1035360SN/A } 1036360SN/A 10371458SN/A return start; 1038360SN/A} 1039360SN/A 1040378SN/A/// Target getrlimit() handler. 1041360SN/Atemplate <class OS> 10421450SN/ASyscallReturn 10433114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10442680Sktlim@umich.edu ThreadContext *tc) 1045360SN/A{ 10466701Sgblack@eecs.umich.edu int index = 0; 10476701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 10486701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1049360SN/A 1050360SN/A switch (resource) { 10512064SN/A case OS::TGT_RLIMIT_STACK: 10525877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10532064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10542091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10552091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10562064SN/A break; 1057360SN/A 10585877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10595877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10605877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10615877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10625877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10635877Shsul@eecs.umich.edu break; 10645877Shsul@eecs.umich.edu 10652064SN/A default: 10662064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10672064SN/A << std::endl; 10682064SN/A abort(); 10692064SN/A break; 1070360SN/A } 1071360SN/A 10722680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10731458SN/A return 0; 1074360SN/A} 1075360SN/A 1076378SN/A/// Target gettimeofday() handler. 1077360SN/Atemplate <class OS> 10781450SN/ASyscallReturn 10793114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10802680Sktlim@umich.edu ThreadContext *tc) 1081360SN/A{ 10826701Sgblack@eecs.umich.edu int index = 0; 10836701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1084360SN/A 1085360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1086360SN/A tp->tv_sec += seconds_since_epoch; 10876109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10886109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1089360SN/A 10902680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1091360SN/A 10921458SN/A return 0; 1093360SN/A} 1094360SN/A 1095360SN/A 10961999SN/A/// Target utimes() handler. 10971999SN/Atemplate <class OS> 10981999SN/ASyscallReturn 10993114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11002680Sktlim@umich.edu ThreadContext *tc) 11011999SN/A{ 11021999SN/A std::string path; 11031999SN/A 11046701Sgblack@eecs.umich.edu int index = 0; 11056701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 11066701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11076701Sgblack@eecs.umich.edu return -EFAULT; 11086701Sgblack@eecs.umich.edu } 11091999SN/A 11106701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 11116701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 11122680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 11131999SN/A 11141999SN/A struct timeval hostTimeval[2]; 11151999SN/A for (int i = 0; i < 2; ++i) 11161999SN/A { 11172091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 11182091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 11191999SN/A } 11203669Sbinkertn@umich.edu 11213669Sbinkertn@umich.edu // Adjust path for current working directory 11223669Sbinkertn@umich.edu path = process->fullPath(path); 11233669Sbinkertn@umich.edu 11241999SN/A int result = utimes(path.c_str(), hostTimeval); 11251999SN/A 11261999SN/A if (result < 0) 11271999SN/A return -errno; 11281999SN/A 11291999SN/A return 0; 11301999SN/A} 1131378SN/A/// Target getrusage() function. 1132360SN/Atemplate <class OS> 11331450SN/ASyscallReturn 11343114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11352680Sktlim@umich.edu ThreadContext *tc) 1136360SN/A{ 11376701Sgblack@eecs.umich.edu int index = 0; 11386701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 11396701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1140360SN/A 11413670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 11423670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1143360SN/A rup->ru_stime.tv_sec = 0; 1144360SN/A rup->ru_stime.tv_usec = 0; 1145360SN/A rup->ru_maxrss = 0; 1146360SN/A rup->ru_ixrss = 0; 1147360SN/A rup->ru_idrss = 0; 1148360SN/A rup->ru_isrss = 0; 1149360SN/A rup->ru_minflt = 0; 1150360SN/A rup->ru_majflt = 0; 1151360SN/A rup->ru_nswap = 0; 1152360SN/A rup->ru_inblock = 0; 1153360SN/A rup->ru_oublock = 0; 1154360SN/A rup->ru_msgsnd = 0; 1155360SN/A rup->ru_msgrcv = 0; 1156360SN/A rup->ru_nsignals = 0; 1157360SN/A rup->ru_nvcsw = 0; 1158360SN/A rup->ru_nivcsw = 0; 1159360SN/A 11603670Sbinkertn@umich.edu switch (who) { 11613670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11623670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11633670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11643670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11653670Sbinkertn@umich.edu break; 11663670Sbinkertn@umich.edu 11673670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11683670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11693670Sbinkertn@umich.edu break; 11703670Sbinkertn@umich.edu 11713670Sbinkertn@umich.edu default: 11723670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11733670Sbinkertn@umich.edu // plow ahead 11743670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11753670Sbinkertn@umich.edu who); 11763670Sbinkertn@umich.edu } 11773670Sbinkertn@umich.edu 11782680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1179360SN/A 11801458SN/A return 0; 1181360SN/A} 1182360SN/A 11836683Stjones1@inf.ed.ac.uk/// Target times() function. 11846683Stjones1@inf.ed.ac.uktemplate <class OS> 11856683Stjones1@inf.ed.ac.ukSyscallReturn 11866683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11876683Stjones1@inf.ed.ac.uk ThreadContext *tc) 11886683Stjones1@inf.ed.ac.uk{ 11896701Sgblack@eecs.umich.edu int index = 0; 11906701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 11916683Stjones1@inf.ed.ac.uk 11926683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 11937823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 11946683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 11956683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 11966683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 11976683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 11986683Stjones1@inf.ed.ac.uk 11996683Stjones1@inf.ed.ac.uk // Convert to host endianness 12006683Stjones1@inf.ed.ac.uk bufp->tms_utime = htog(bufp->tms_utime); 12016683Stjones1@inf.ed.ac.uk 12026683Stjones1@inf.ed.ac.uk // Write back 12036683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemPort()); 12046683Stjones1@inf.ed.ac.uk 12056683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 12066683Stjones1@inf.ed.ac.uk return clocks; 12076683Stjones1@inf.ed.ac.uk} 12082553SN/A 12096684Stjones1@inf.ed.ac.uk/// Target time() function. 12106684Stjones1@inf.ed.ac.uktemplate <class OS> 12116684Stjones1@inf.ed.ac.ukSyscallReturn 12126684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 12136684Stjones1@inf.ed.ac.uk ThreadContext *tc) 12146684Stjones1@inf.ed.ac.uk{ 12156684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 12166684Stjones1@inf.ed.ac.uk getElapsedTime(sec, usec); 12176684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 12186684Stjones1@inf.ed.ac.uk 12196701Sgblack@eecs.umich.edu int index = 0; 12206701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 12216684Stjones1@inf.ed.ac.uk if(taddr != 0) { 12226684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 12236684Stjones1@inf.ed.ac.uk t = htog(t); 12246684Stjones1@inf.ed.ac.uk TranslatingPort *p = tc->getMemPort(); 12256684Stjones1@inf.ed.ac.uk p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 12266684Stjones1@inf.ed.ac.uk } 12276684Stjones1@inf.ed.ac.uk return sec; 12286684Stjones1@inf.ed.ac.uk} 12292553SN/A 12302553SN/A 12311354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1232