syscall_emul.hh revision 8149
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); 1908149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num, 1918149SChris.Emmons@ARM.com LiveProcess *p, ThreadContext *tc); 192360SN/A 1936109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1941706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1953114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 196378SN/A 1976109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1986109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1996109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 2006109Ssanchezd@stanford.edu 201378SN/A/// Target getpagesize() handler. 2021706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 2033114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 204378SN/A 2055748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2065748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2075748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 208378SN/A 209378SN/A/// Target close() handler. 2101706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2113114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 212378SN/A 213378SN/A/// Target read() handler. 2141706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2153114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 216378SN/A 217378SN/A/// Target write() handler. 2181706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2193114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 220378SN/A 221378SN/A/// Target lseek() handler. 2221706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2233114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 224378SN/A 2254118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2264118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2274118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2284118Sgblack@eecs.umich.edu 229378SN/A/// Target munmap() handler. 2301706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2313114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 232378SN/A 233378SN/A/// Target gethostname() handler. 2341706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2353114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 236360SN/A 2375513SMichael.Adler@intel.com/// Target getcwd() handler. 2385513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2395513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2405513SMichael.Adler@intel.com 2415513SMichael.Adler@intel.com/// Target unlink() handler. 2425513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2435513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2445513SMichael.Adler@intel.com 245511SN/A/// Target unlink() handler. 2461706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2473114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 248511SN/A 2495513SMichael.Adler@intel.com/// Target mkdir() handler. 2505513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2515513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2525513SMichael.Adler@intel.com 253511SN/A/// Target rename() handler. 2541706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2553114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2561706SN/A 2571706SN/A 2581706SN/A/// Target truncate() handler. 2591706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2603114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2611706SN/A 2621706SN/A 2631706SN/A/// Target ftruncate() handler. 2641706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2653114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2661706SN/A 267511SN/A 2686703Svince@csl.cornell.edu/// Target truncate64() handler. 2696703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2706703Svince@csl.cornell.edu LiveProcess *p, ThreadContext *tc); 2716703Svince@csl.cornell.edu 2726685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2736685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2746685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2756685Stjones1@inf.ed.ac.uk 2766685Stjones1@inf.ed.ac.uk 2775513SMichael.Adler@intel.com/// Target umask() handler. 2785513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2795513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2805513SMichael.Adler@intel.com 2815513SMichael.Adler@intel.com 2821999SN/A/// Target chown() handler. 2831999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2843114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2851999SN/A 2861999SN/A 2871999SN/A/// Target fchown() handler. 2881999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2893114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2901999SN/A 2913079Sstever@eecs.umich.edu/// Target dup() handler. 2923079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2933114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2943079Sstever@eecs.umich.edu 2952093SN/A/// Target fnctl() handler. 2962093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2973114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2982093SN/A 2992687Sksewell@umich.edu/// Target fcntl64() handler. 3002687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 3013114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3022687Sksewell@umich.edu 3032238SN/A/// Target setuid() handler. 3042238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 3053114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3062238SN/A 3072238SN/A/// Target getpid() handler. 3082238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3093114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3102238SN/A 3112238SN/A/// Target getuid() handler. 3122238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3133114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3142238SN/A 3152238SN/A/// Target getgid() handler. 3162238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3173114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3182238SN/A 3192238SN/A/// Target getppid() handler. 3202238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3213114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3222238SN/A 3232238SN/A/// Target geteuid() handler. 3242238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3253114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3262238SN/A 3272238SN/A/// Target getegid() handler. 3282238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3293114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3302238SN/A 3316109Ssanchezd@stanford.edu/// Target clone() handler. 3326109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3336109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3342238SN/A 3352238SN/A 3362238SN/A/// Pseudo Funcs - These functions use a different return convension, 3372238SN/A/// returning a second value in a register other than the normal return register 3382238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3393114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3402238SN/A 3412238SN/A/// Target getpidPseudo() handler. 3422238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3433114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3442238SN/A 3452238SN/A/// Target getuidPseudo() handler. 3462238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3473114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3482238SN/A 3492238SN/A/// Target getgidPseudo() handler. 3502238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3513114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3522238SN/A 3532238SN/A 3541354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3551354SN/Aconst int one_million = 1000000; 3561354SN/A 3571354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3581354SN/A/// by my reckoning. We want to keep this a constant (not use the 3591354SN/A/// real-world time) to keep simulations repeatable. 3601354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3611354SN/A 3621354SN/A/// Helper function to convert current elapsed time to seconds and 3631354SN/A/// microseconds. 3641354SN/Atemplate <class T1, class T2> 3651354SN/Avoid 3661354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3671354SN/A{ 3687823Ssteve.reinhardt@amd.com int elapsed_usecs = curTick() / SimClock::Int::us; 3691354SN/A sec = elapsed_usecs / one_million; 3701354SN/A usec = elapsed_usecs % one_million; 3711354SN/A} 3721354SN/A 373360SN/A////////////////////////////////////////////////////////////////////// 374360SN/A// 375360SN/A// The following emulation functions are generic, but need to be 376360SN/A// templated to account for differences in types, constants, etc. 377360SN/A// 378360SN/A////////////////////////////////////////////////////////////////////// 379360SN/A 3803113Sgblack@eecs.umich.edu#if NO_STAT64 3813113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3823113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3833113Sgblack@eecs.umich.edu#else 3843113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3853113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3863113Sgblack@eecs.umich.edu#endif 3873113Sgblack@eecs.umich.edu 3883113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3893113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3903113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3913113Sgblack@eecs.umich.edu 3923113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3933113Sgblack@eecs.umich.edustatic void 3943113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3953113Sgblack@eecs.umich.edu{ 3964189Sgblack@eecs.umich.edu using namespace TheISA; 3974189Sgblack@eecs.umich.edu 3983113Sgblack@eecs.umich.edu if (fakeTTY) 3993113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 4003113Sgblack@eecs.umich.edu else 4013113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 4023113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 4033113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 4043113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 4053277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4065515SMichael.Adler@intel.com if (fakeTTY) { 4075515SMichael.Adler@intel.com // Claim to be a character device 4085515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4095515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4105515SMichael.Adler@intel.com } 4113277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 4123277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4133277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 4143277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4153277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4163277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4173277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4183113Sgblack@eecs.umich.edu if (fakeTTY) 4193113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4203113Sgblack@eecs.umich.edu else 4213113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4223113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4233113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4243113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4253114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4263113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4273114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4283113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4293114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4303113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4314061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4324061Sgblack@eecs.umich.edu // consistently across different hosts. 4334061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4343113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4353113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4363113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4373113Sgblack@eecs.umich.edu} 4383113Sgblack@eecs.umich.edu 4393113Sgblack@eecs.umich.edu// Same for stat64 4403113Sgblack@eecs.umich.edu 4413113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4423113Sgblack@eecs.umich.edustatic void 4433113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4443113Sgblack@eecs.umich.edu{ 4454189Sgblack@eecs.umich.edu using namespace TheISA; 4464189Sgblack@eecs.umich.edu 4473113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4483113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4493113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4503113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4513113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4523113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4533113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4543113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4553113Sgblack@eecs.umich.edu#else 4563113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4573113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4583113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4593113Sgblack@eecs.umich.edu#endif 4603113Sgblack@eecs.umich.edu} 4613113Sgblack@eecs.umich.edu 4623113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4633113Sgblack@eecs.umich.edutemplate<class OS> 4643113Sgblack@eecs.umich.edustatic void 4653113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4663113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4673113Sgblack@eecs.umich.edu{ 4683113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4693113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4703113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4713113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4723113Sgblack@eecs.umich.edu} 4733113Sgblack@eecs.umich.edu 4743113Sgblack@eecs.umich.edutemplate<class OS> 4753113Sgblack@eecs.umich.edustatic void 4763113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4773113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4783113Sgblack@eecs.umich.edu{ 4793113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4803113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4816686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4823113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4833113Sgblack@eecs.umich.edu} 4843113Sgblack@eecs.umich.edu 485378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 486378SN/A/// only to find out if their stdout is a tty, to determine whether to 487378SN/A/// do line or block buffering. 488360SN/Atemplate <class OS> 4891450SN/ASyscallReturn 4903114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4912680Sktlim@umich.edu ThreadContext *tc) 492360SN/A{ 4936701Sgblack@eecs.umich.edu int index = 0; 4946701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 4956701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 496360SN/A 4971969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 498360SN/A 499360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 500360SN/A // doesn't map to any simulator fd: not a valid target fd 5011458SN/A return -EBADF; 502360SN/A } 503360SN/A 504360SN/A switch (req) { 5054131Sbinkertn@umich.edu case OS::TIOCISATTY_: 5064131Sbinkertn@umich.edu case OS::TIOCGETP_: 5074131Sbinkertn@umich.edu case OS::TIOCSETP_: 5084131Sbinkertn@umich.edu case OS::TIOCSETN_: 5094131Sbinkertn@umich.edu case OS::TIOCSETC_: 5104131Sbinkertn@umich.edu case OS::TIOCGETC_: 5114131Sbinkertn@umich.edu case OS::TIOCGETS_: 5124131Sbinkertn@umich.edu case OS::TIOCGETA_: 5136689Stjones1@inf.ed.ac.uk case OS::TCSETAW_: 5141458SN/A return -ENOTTY; 515360SN/A 516360SN/A default: 5177720Sgblack@eecs.umich.edu fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n", 5187720Sgblack@eecs.umich.edu fd, req, tc->pcState()); 519360SN/A } 520360SN/A} 521360SN/A 522378SN/A/// Target open() handler. 523360SN/Atemplate <class OS> 5241450SN/ASyscallReturn 5253114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5262680Sktlim@umich.edu ThreadContext *tc) 527360SN/A{ 528360SN/A std::string path; 529360SN/A 5306701Sgblack@eecs.umich.edu int index = 0; 5316701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 5326701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 5331458SN/A return -EFAULT; 534360SN/A 535360SN/A if (path == "/dev/sysdev0") { 536360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 537360SN/A // We don't support it, so just punt. 5381706SN/A warn("Ignoring open(%s, ...)\n", path); 5391458SN/A return -ENOENT; 540360SN/A } 541360SN/A 5426701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 5436701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 544360SN/A int hostFlags = 0; 545360SN/A 546360SN/A // translate open flags 547360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 548360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 549360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 550360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 551360SN/A } 552360SN/A } 553360SN/A 554360SN/A // any target flags left? 555360SN/A if (tgtFlags != 0) 5561706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 557360SN/A 558360SN/A#ifdef __CYGWIN32__ 559360SN/A hostFlags |= O_BINARY; 560360SN/A#endif 561360SN/A 5623669Sbinkertn@umich.edu // Adjust path for current working directory 5633669Sbinkertn@umich.edu path = process->fullPath(path); 5643669Sbinkertn@umich.edu 5651706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5661706SN/A 5675795Ssaidi@eecs.umich.edu int fd; 5685795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5695795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5705795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5715795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5725795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5735795Ssaidi@eecs.umich.edu } else { 5745795Ssaidi@eecs.umich.edu // open the file 5755795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5765795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5775795Ssaidi@eecs.umich.edu } 578360SN/A 579360SN/A} 580360SN/A 5816640Svince@csl.cornell.edu/// Target sysinfo() handler. 5826640Svince@csl.cornell.edutemplate <class OS> 5836640Svince@csl.cornell.eduSyscallReturn 5846640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5856640Svince@csl.cornell.edu ThreadContext *tc) 5866640Svince@csl.cornell.edu{ 5876640Svince@csl.cornell.edu 5886701Sgblack@eecs.umich.edu int index = 0; 5896701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 5906701Sgblack@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 5916640Svince@csl.cornell.edu 5926701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 5936701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 5946640Svince@csl.cornell.edu 5956701Sgblack@eecs.umich.edu sysinfo.copyOut(tc->getMemPort()); 5966640Svince@csl.cornell.edu 5976701Sgblack@eecs.umich.edu return 0; 5986640Svince@csl.cornell.edu} 599360SN/A 6001999SN/A/// Target chmod() handler. 6011999SN/Atemplate <class OS> 6021999SN/ASyscallReturn 6033114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6042680Sktlim@umich.edu ThreadContext *tc) 6051999SN/A{ 6061999SN/A std::string path; 6071999SN/A 6086701Sgblack@eecs.umich.edu int index = 0; 6096701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 6106701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 6111999SN/A return -EFAULT; 6126701Sgblack@eecs.umich.edu } 6131999SN/A 6146701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6151999SN/A mode_t hostMode = 0; 6161999SN/A 6171999SN/A // XXX translate mode flags via OS::something??? 6181999SN/A hostMode = mode; 6191999SN/A 6203669Sbinkertn@umich.edu // Adjust path for current working directory 6213669Sbinkertn@umich.edu path = process->fullPath(path); 6223669Sbinkertn@umich.edu 6231999SN/A // do the chmod 6241999SN/A int result = chmod(path.c_str(), hostMode); 6251999SN/A if (result < 0) 6262218SN/A return -errno; 6271999SN/A 6281999SN/A return 0; 6291999SN/A} 6301999SN/A 6311999SN/A 6321999SN/A/// Target fchmod() handler. 6331999SN/Atemplate <class OS> 6341999SN/ASyscallReturn 6353114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6362680Sktlim@umich.edu ThreadContext *tc) 6371999SN/A{ 6386701Sgblack@eecs.umich.edu int index = 0; 6396701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6401999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6411999SN/A // doesn't map to any simulator fd: not a valid target fd 6421999SN/A return -EBADF; 6431999SN/A } 6441999SN/A 6456701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6461999SN/A mode_t hostMode = 0; 6471999SN/A 6481999SN/A // XXX translate mode flags via OS::someting??? 6491999SN/A hostMode = mode; 6501999SN/A 6511999SN/A // do the fchmod 6521999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6531999SN/A if (result < 0) 6542218SN/A return -errno; 6551999SN/A 6561999SN/A return 0; 6571999SN/A} 6581999SN/A 6595877Shsul@eecs.umich.edu/// Target mremap() handler. 6605877Shsul@eecs.umich.edutemplate <class OS> 6615877Shsul@eecs.umich.eduSyscallReturn 6625877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6635877Shsul@eecs.umich.edu{ 6646701Sgblack@eecs.umich.edu int index = 0; 6656701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 6666701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 6676701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 6686701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 6695877Shsul@eecs.umich.edu 6705877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6715877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6725877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6735877Shsul@eecs.umich.edu return -EINVAL; 6745877Shsul@eecs.umich.edu } 6755877Shsul@eecs.umich.edu 6765877Shsul@eecs.umich.edu if (new_length > old_length) { 6775877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6785877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6795877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6805877Shsul@eecs.umich.edu process->mmap_end += diff; 6815877Shsul@eecs.umich.edu return start; 6825877Shsul@eecs.umich.edu } else { 6835877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6845877Shsul@eecs.umich.edu if (!(flags & 1)) { 6855877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6865877Shsul@eecs.umich.edu return -ENOMEM; 6875877Shsul@eecs.umich.edu } else { 6885877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6895877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6905877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6915877Shsul@eecs.umich.edu start = process->mmap_end; 6925877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6935877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6945877Shsul@eecs.umich.edu process->mmap_end += new_length; 6955877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6965877Shsul@eecs.umich.edu return start; 6975877Shsul@eecs.umich.edu } 6985877Shsul@eecs.umich.edu } 6995877Shsul@eecs.umich.edu } else { 7005877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 7015877Shsul@eecs.umich.edu new_length); 7025877Shsul@eecs.umich.edu return start; 7035877Shsul@eecs.umich.edu } 7045877Shsul@eecs.umich.edu} 7051999SN/A 706378SN/A/// Target stat() handler. 707360SN/Atemplate <class OS> 7081450SN/ASyscallReturn 7093114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7102680Sktlim@umich.edu ThreadContext *tc) 711360SN/A{ 712360SN/A std::string path; 713360SN/A 7146701Sgblack@eecs.umich.edu int index = 0; 7156701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7166701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7176701Sgblack@eecs.umich.edu return -EFAULT; 7186701Sgblack@eecs.umich.edu } 7196701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 720360SN/A 7213669Sbinkertn@umich.edu // Adjust path for current working directory 7223669Sbinkertn@umich.edu path = process->fullPath(path); 7233669Sbinkertn@umich.edu 724360SN/A struct stat hostBuf; 725360SN/A int result = stat(path.c_str(), &hostBuf); 726360SN/A 727360SN/A if (result < 0) 7282218SN/A return -errno; 729360SN/A 7306701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 731360SN/A 7321458SN/A return 0; 733360SN/A} 734360SN/A 735360SN/A 7365074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7375074Ssaidi@eecs.umich.edutemplate <class OS> 7385074Ssaidi@eecs.umich.eduSyscallReturn 7395074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7405074Ssaidi@eecs.umich.edu ThreadContext *tc) 7415074Ssaidi@eecs.umich.edu{ 7425074Ssaidi@eecs.umich.edu std::string path; 7435074Ssaidi@eecs.umich.edu 7446701Sgblack@eecs.umich.edu int index = 0; 7456701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7466701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 7475074Ssaidi@eecs.umich.edu return -EFAULT; 7486701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7495074Ssaidi@eecs.umich.edu 7505074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7515074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7525074Ssaidi@eecs.umich.edu 7535208Ssaidi@eecs.umich.edu#if NO_STAT64 7545208Ssaidi@eecs.umich.edu struct stat hostBuf; 7555208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7565208Ssaidi@eecs.umich.edu#else 7575074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7585074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7595208Ssaidi@eecs.umich.edu#endif 7605074Ssaidi@eecs.umich.edu 7615074Ssaidi@eecs.umich.edu if (result < 0) 7625074Ssaidi@eecs.umich.edu return -errno; 7635074Ssaidi@eecs.umich.edu 7646701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 7655074Ssaidi@eecs.umich.edu 7665074Ssaidi@eecs.umich.edu return 0; 7675074Ssaidi@eecs.umich.edu} 7685074Ssaidi@eecs.umich.edu 7695074Ssaidi@eecs.umich.edu 7701999SN/A/// Target fstat64() handler. 7711999SN/Atemplate <class OS> 7721999SN/ASyscallReturn 7733114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7742680Sktlim@umich.edu ThreadContext *tc) 7751999SN/A{ 7766701Sgblack@eecs.umich.edu int index = 0; 7776701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 7786701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7791999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7801999SN/A // doesn't map to any simulator fd: not a valid target fd 7811999SN/A return -EBADF; 7821999SN/A } 7831999SN/A 7842764Sstever@eecs.umich.edu#if NO_STAT64 7852064SN/A struct stat hostBuf; 7862064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7872064SN/A#else 7882064SN/A struct stat64 hostBuf; 7891999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7902064SN/A#endif 7911999SN/A 7921999SN/A if (result < 0) 7932218SN/A return -errno; 7941999SN/A 7956701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 7961999SN/A 7971999SN/A return 0; 7981999SN/A} 7991999SN/A 8001999SN/A 801378SN/A/// Target lstat() handler. 802360SN/Atemplate <class OS> 8031450SN/ASyscallReturn 8043114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8052680Sktlim@umich.edu ThreadContext *tc) 806360SN/A{ 807360SN/A std::string path; 808360SN/A 8096701Sgblack@eecs.umich.edu int index = 0; 8106701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8116701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8126701Sgblack@eecs.umich.edu return -EFAULT; 8136701Sgblack@eecs.umich.edu } 8146701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 815360SN/A 8163669Sbinkertn@umich.edu // Adjust path for current working directory 8173669Sbinkertn@umich.edu path = process->fullPath(path); 8183669Sbinkertn@umich.edu 819360SN/A struct stat hostBuf; 820360SN/A int result = lstat(path.c_str(), &hostBuf); 821360SN/A 822360SN/A if (result < 0) 8231458SN/A return -errno; 824360SN/A 8256701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 826360SN/A 8271458SN/A return 0; 828360SN/A} 829360SN/A 8301999SN/A/// Target lstat64() handler. 8311999SN/Atemplate <class OS> 8321999SN/ASyscallReturn 8333114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 8342680Sktlim@umich.edu ThreadContext *tc) 8351999SN/A{ 8361999SN/A std::string path; 8371999SN/A 8386701Sgblack@eecs.umich.edu int index = 0; 8396701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8406701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8416701Sgblack@eecs.umich.edu return -EFAULT; 8426701Sgblack@eecs.umich.edu } 8436701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8441999SN/A 8453669Sbinkertn@umich.edu // Adjust path for current working directory 8463669Sbinkertn@umich.edu path = process->fullPath(path); 8473669Sbinkertn@umich.edu 8482764Sstever@eecs.umich.edu#if NO_STAT64 8492064SN/A struct stat hostBuf; 8502064SN/A int result = lstat(path.c_str(), &hostBuf); 8512064SN/A#else 8521999SN/A struct stat64 hostBuf; 8531999SN/A int result = lstat64(path.c_str(), &hostBuf); 8542064SN/A#endif 8551999SN/A 8561999SN/A if (result < 0) 8571999SN/A return -errno; 8581999SN/A 8596701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 8601999SN/A 8611999SN/A return 0; 8621999SN/A} 8631999SN/A 864378SN/A/// Target fstat() handler. 865360SN/Atemplate <class OS> 8661450SN/ASyscallReturn 8673114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8682680Sktlim@umich.edu ThreadContext *tc) 869360SN/A{ 8706701Sgblack@eecs.umich.edu int index = 0; 8716701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 8726701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 873360SN/A 8741969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 875360SN/A 876360SN/A if (fd < 0) 8771458SN/A return -EBADF; 878360SN/A 879360SN/A struct stat hostBuf; 880360SN/A int result = fstat(fd, &hostBuf); 881360SN/A 882360SN/A if (result < 0) 8831458SN/A return -errno; 884360SN/A 8856701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 8862021SN/A 8871458SN/A return 0; 888360SN/A} 889360SN/A 890360SN/A 8911706SN/A/// Target statfs() handler. 8921706SN/Atemplate <class OS> 8931706SN/ASyscallReturn 8943114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8952680Sktlim@umich.edu ThreadContext *tc) 8961706SN/A{ 8971706SN/A std::string path; 8981706SN/A 8996701Sgblack@eecs.umich.edu int index = 0; 9006701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 9016701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9026701Sgblack@eecs.umich.edu return -EFAULT; 9036701Sgblack@eecs.umich.edu } 9046701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9051706SN/A 9063669Sbinkertn@umich.edu // Adjust path for current working directory 9073669Sbinkertn@umich.edu path = process->fullPath(path); 9083669Sbinkertn@umich.edu 9091706SN/A struct statfs hostBuf; 9101706SN/A int result = statfs(path.c_str(), &hostBuf); 9111706SN/A 9121706SN/A if (result < 0) 9132218SN/A return -errno; 9141706SN/A 9156701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9161706SN/A 9171706SN/A return 0; 9181706SN/A} 9191706SN/A 9201706SN/A 9211706SN/A/// Target fstatfs() handler. 9221706SN/Atemplate <class OS> 9231706SN/ASyscallReturn 9243114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9252680Sktlim@umich.edu ThreadContext *tc) 9261706SN/A{ 9276701Sgblack@eecs.umich.edu int index = 0; 9286701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 9296701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9301706SN/A 9311706SN/A if (fd < 0) 9321706SN/A return -EBADF; 9331706SN/A 9341706SN/A struct statfs hostBuf; 9351706SN/A int result = fstatfs(fd, &hostBuf); 9361706SN/A 9371706SN/A if (result < 0) 9382218SN/A return -errno; 9391706SN/A 9406701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9411706SN/A 9421706SN/A return 0; 9431706SN/A} 9441706SN/A 9451706SN/A 9461999SN/A/// Target writev() handler. 9471999SN/Atemplate <class OS> 9481999SN/ASyscallReturn 9493114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9502680Sktlim@umich.edu ThreadContext *tc) 9511999SN/A{ 9526701Sgblack@eecs.umich.edu int index = 0; 9536701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 9541999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9551999SN/A // doesn't map to any simulator fd: not a valid target fd 9561999SN/A return -EBADF; 9571999SN/A } 9581999SN/A 9592680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9606701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 9616701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 9621999SN/A struct iovec hiov[count]; 9636227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9641999SN/A typename OS::tgt_iovec tiov; 9652461SN/A 9662461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9672461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9682091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9691999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9702461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9712461SN/A hiov[i].iov_len); 9721999SN/A } 9731999SN/A 9741999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9751999SN/A 9766227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9771999SN/A delete [] (char *)hiov[i].iov_base; 9781999SN/A 9791999SN/A if (result < 0) 9802218SN/A return -errno; 9811999SN/A 9821999SN/A return 0; 9831999SN/A} 9841999SN/A 9851999SN/A 986378SN/A/// Target mmap() handler. 987378SN/A/// 988378SN/A/// We don't really handle mmap(). If the target is mmaping an 989378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 990378SN/A/// nothing (since memory is initialized to zero and the simulator 991378SN/A/// doesn't really check addresses anyway). Always print a warning, 992378SN/A/// since this could be seriously broken if we're not mapping 993378SN/A/// /dev/zero. 994360SN/A// 995378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 996378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 997378SN/A/// anything else. 998360SN/Atemplate <class OS> 9991450SN/ASyscallReturn 10003114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 1001360SN/A{ 10026701Sgblack@eecs.umich.edu int index = 0; 10036701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 10046701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 10056701Sgblack@eecs.umich.edu index++; // int prot = p->getSyscallArg(tc, index); 10066701Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, index); 10076701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 10086701Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, index); 1009360SN/A 10105877Shsul@eecs.umich.edu 10112544SN/A if ((start % TheISA::VMPageSize) != 0 || 10122544SN/A (length % TheISA::VMPageSize) != 0) { 10132544SN/A warn("mmap failing: arguments not page-aligned: " 10142544SN/A "start 0x%x length 0x%x", 10152544SN/A start, length); 10162544SN/A return -EINVAL; 1017360SN/A } 1018360SN/A 10192544SN/A if (start != 0) { 10202544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 10212544SN/A start, p->mmap_end); 10222544SN/A } 10232544SN/A 10242544SN/A // pick next address from our "mmap region" 10256672Sgblack@eecs.umich.edu if (OS::mmapGrowsDown()) { 10266672Sgblack@eecs.umich.edu start = p->mmap_end - length; 10276672Sgblack@eecs.umich.edu p->mmap_end = start; 10286672Sgblack@eecs.umich.edu } else { 10296672Sgblack@eecs.umich.edu start = p->mmap_end; 10306672Sgblack@eecs.umich.edu p->mmap_end += length; 10316672Sgblack@eecs.umich.edu } 10322544SN/A p->pTable->allocate(start, length); 10332544SN/A 10342553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 10351969SN/A warn("allowing mmap of file @ fd %d. " 10366701Sgblack@eecs.umich.edu "This will break if not /dev/zero.", fd); 1037360SN/A } 1038360SN/A 10391458SN/A return start; 1040360SN/A} 1041360SN/A 1042378SN/A/// Target getrlimit() handler. 1043360SN/Atemplate <class OS> 10441450SN/ASyscallReturn 10453114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10462680Sktlim@umich.edu ThreadContext *tc) 1047360SN/A{ 10486701Sgblack@eecs.umich.edu int index = 0; 10496701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 10506701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1051360SN/A 1052360SN/A switch (resource) { 10532064SN/A case OS::TGT_RLIMIT_STACK: 10545877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10552064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10562091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10572091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10582064SN/A break; 1059360SN/A 10605877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10615877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10625877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10635877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10645877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10655877Shsul@eecs.umich.edu break; 10665877Shsul@eecs.umich.edu 10672064SN/A default: 10682064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10692064SN/A << std::endl; 10702064SN/A abort(); 10712064SN/A break; 1072360SN/A } 1073360SN/A 10742680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10751458SN/A return 0; 1076360SN/A} 1077360SN/A 1078378SN/A/// Target gettimeofday() handler. 1079360SN/Atemplate <class OS> 10801450SN/ASyscallReturn 10813114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10822680Sktlim@umich.edu ThreadContext *tc) 1083360SN/A{ 10846701Sgblack@eecs.umich.edu int index = 0; 10856701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1086360SN/A 1087360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1088360SN/A tp->tv_sec += seconds_since_epoch; 10896109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10906109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1091360SN/A 10922680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1093360SN/A 10941458SN/A return 0; 1095360SN/A} 1096360SN/A 1097360SN/A 10981999SN/A/// Target utimes() handler. 10991999SN/Atemplate <class OS> 11001999SN/ASyscallReturn 11013114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11022680Sktlim@umich.edu ThreadContext *tc) 11031999SN/A{ 11041999SN/A std::string path; 11051999SN/A 11066701Sgblack@eecs.umich.edu int index = 0; 11076701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 11086701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11096701Sgblack@eecs.umich.edu return -EFAULT; 11106701Sgblack@eecs.umich.edu } 11111999SN/A 11126701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 11136701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 11142680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 11151999SN/A 11161999SN/A struct timeval hostTimeval[2]; 11171999SN/A for (int i = 0; i < 2; ++i) 11181999SN/A { 11192091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 11202091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 11211999SN/A } 11223669Sbinkertn@umich.edu 11233669Sbinkertn@umich.edu // Adjust path for current working directory 11243669Sbinkertn@umich.edu path = process->fullPath(path); 11253669Sbinkertn@umich.edu 11261999SN/A int result = utimes(path.c_str(), hostTimeval); 11271999SN/A 11281999SN/A if (result < 0) 11291999SN/A return -errno; 11301999SN/A 11311999SN/A return 0; 11321999SN/A} 1133378SN/A/// Target getrusage() function. 1134360SN/Atemplate <class OS> 11351450SN/ASyscallReturn 11363114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11372680Sktlim@umich.edu ThreadContext *tc) 1138360SN/A{ 11396701Sgblack@eecs.umich.edu int index = 0; 11406701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 11416701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1142360SN/A 11433670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 11443670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1145360SN/A rup->ru_stime.tv_sec = 0; 1146360SN/A rup->ru_stime.tv_usec = 0; 1147360SN/A rup->ru_maxrss = 0; 1148360SN/A rup->ru_ixrss = 0; 1149360SN/A rup->ru_idrss = 0; 1150360SN/A rup->ru_isrss = 0; 1151360SN/A rup->ru_minflt = 0; 1152360SN/A rup->ru_majflt = 0; 1153360SN/A rup->ru_nswap = 0; 1154360SN/A rup->ru_inblock = 0; 1155360SN/A rup->ru_oublock = 0; 1156360SN/A rup->ru_msgsnd = 0; 1157360SN/A rup->ru_msgrcv = 0; 1158360SN/A rup->ru_nsignals = 0; 1159360SN/A rup->ru_nvcsw = 0; 1160360SN/A rup->ru_nivcsw = 0; 1161360SN/A 11623670Sbinkertn@umich.edu switch (who) { 11633670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11643670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11653670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11663670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11673670Sbinkertn@umich.edu break; 11683670Sbinkertn@umich.edu 11693670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11703670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11713670Sbinkertn@umich.edu break; 11723670Sbinkertn@umich.edu 11733670Sbinkertn@umich.edu default: 11743670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11753670Sbinkertn@umich.edu // plow ahead 11763670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11773670Sbinkertn@umich.edu who); 11783670Sbinkertn@umich.edu } 11793670Sbinkertn@umich.edu 11802680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1181360SN/A 11821458SN/A return 0; 1183360SN/A} 1184360SN/A 11856683Stjones1@inf.ed.ac.uk/// Target times() function. 11866683Stjones1@inf.ed.ac.uktemplate <class OS> 11876683Stjones1@inf.ed.ac.ukSyscallReturn 11886683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11896683Stjones1@inf.ed.ac.uk ThreadContext *tc) 11906683Stjones1@inf.ed.ac.uk{ 11916701Sgblack@eecs.umich.edu int index = 0; 11926701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 11936683Stjones1@inf.ed.ac.uk 11946683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 11957823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 11966683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 11976683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 11986683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 11996683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 12006683Stjones1@inf.ed.ac.uk 12016683Stjones1@inf.ed.ac.uk // Convert to host endianness 12026683Stjones1@inf.ed.ac.uk bufp->tms_utime = htog(bufp->tms_utime); 12036683Stjones1@inf.ed.ac.uk 12046683Stjones1@inf.ed.ac.uk // Write back 12056683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemPort()); 12066683Stjones1@inf.ed.ac.uk 12076683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 12086683Stjones1@inf.ed.ac.uk return clocks; 12096683Stjones1@inf.ed.ac.uk} 12102553SN/A 12116684Stjones1@inf.ed.ac.uk/// Target time() function. 12126684Stjones1@inf.ed.ac.uktemplate <class OS> 12136684Stjones1@inf.ed.ac.ukSyscallReturn 12146684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 12156684Stjones1@inf.ed.ac.uk ThreadContext *tc) 12166684Stjones1@inf.ed.ac.uk{ 12176684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 12186684Stjones1@inf.ed.ac.uk getElapsedTime(sec, usec); 12196684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 12206684Stjones1@inf.ed.ac.uk 12216701Sgblack@eecs.umich.edu int index = 0; 12226701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 12236684Stjones1@inf.ed.ac.uk if(taddr != 0) { 12246684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 12256684Stjones1@inf.ed.ac.uk t = htog(t); 12266684Stjones1@inf.ed.ac.uk TranslatingPort *p = tc->getMemPort(); 12276684Stjones1@inf.ed.ac.uk p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 12286684Stjones1@inf.ed.ac.uk } 12296684Stjones1@inf.ed.ac.uk return sec; 12306684Stjones1@inf.ed.ac.uk} 12312553SN/A 12322553SN/A 12331354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1234