syscall_emul.hh revision 7075
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" 656640Svince@csl.cornell.edu#include "sim/system.hh" 661354SN/A#include "sim/process.hh" 67360SN/A 68360SN/A/// 69360SN/A/// System call descriptor. 70360SN/A/// 71360SN/Aclass SyscallDesc { 72360SN/A 73360SN/A public: 74360SN/A 75378SN/A /// Typedef for target syscall handler functions. 761450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 773114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 78360SN/A 795543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 805543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 815543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 82360SN/A 83360SN/A /// Flag values for controlling syscall behavior. 84360SN/A enum Flags { 85360SN/A /// Don't set return regs according to funcPtr return value. 86360SN/A /// Used for syscalls with non-standard return conventions 872680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 88360SN/A /// sigreturn). 89360SN/A SuppressReturnValue = 1 90360SN/A }; 91360SN/A 92360SN/A /// Constructor. 93360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 94360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 95360SN/A { 96360SN/A } 97360SN/A 98360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 993114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 100360SN/A}; 101360SN/A 102360SN/A 103360SN/Aclass BaseBufferArg { 104360SN/A 105360SN/A public: 106360SN/A 107360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 108360SN/A { 109360SN/A bufPtr = new uint8_t[size]; 110360SN/A // clear out buffer: in case we only partially populate this, 111360SN/A // and then do a copyOut(), we want to make sure we don't 112360SN/A // introduce any random junk into the simulated address space 113360SN/A memset(bufPtr, 0, size); 114360SN/A } 115360SN/A 116360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 117360SN/A 118360SN/A // 119360SN/A // copy data into simulator space (read from target memory) 120360SN/A // 1212400SN/A virtual bool copyIn(TranslatingPort *memport) 122360SN/A { 1232461SN/A memport->readBlob(addr, bufPtr, size); 1245543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 125360SN/A } 126360SN/A 127360SN/A // 128360SN/A // copy data out of simulator space (write to target memory) 129360SN/A // 1302400SN/A virtual bool copyOut(TranslatingPort *memport) 131360SN/A { 1322461SN/A memport->writeBlob(addr, bufPtr, size); 1335543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 134360SN/A } 135360SN/A 136360SN/A protected: 137360SN/A Addr addr; 138360SN/A int size; 139360SN/A uint8_t *bufPtr; 140360SN/A}; 141360SN/A 142360SN/A 143360SN/Aclass BufferArg : public BaseBufferArg 144360SN/A{ 145360SN/A public: 146360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1475543Ssaidi@eecs.umich.edu void *bufferPtr() { return bufPtr; } 148360SN/A}; 149360SN/A 150360SN/Atemplate <class T> 151360SN/Aclass TypedBufferArg : public BaseBufferArg 152360SN/A{ 153360SN/A public: 154360SN/A // user can optionally specify a specific number of bytes to 155360SN/A // allocate to deal with those structs that have variable-size 156360SN/A // arrays at the end 157360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 158360SN/A : BaseBufferArg(_addr, _size) 159360SN/A { } 160360SN/A 161360SN/A // type case 162360SN/A operator T*() { return (T *)bufPtr; } 163360SN/A 164360SN/A // dereference operators 1655543Ssaidi@eecs.umich.edu T &operator*() { return *((T *)bufPtr); } 1665543Ssaidi@eecs.umich.edu T* operator->() { return (T *)bufPtr; } 167502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 168360SN/A}; 169360SN/A 170360SN/A////////////////////////////////////////////////////////////////////// 171360SN/A// 172360SN/A// The following emulation functions are generic enough that they 173360SN/A// don't need to be recompiled for different emulated OS's. They are 174360SN/A// defined in sim/syscall_emul.cc. 175360SN/A// 176360SN/A////////////////////////////////////////////////////////////////////// 177360SN/A 178360SN/A 179378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1801706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1813114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 182378SN/A 183378SN/A/// Handler for unimplemented syscalls that we never intend to 184378SN/A/// implement (signal handling, etc.) and should not affect the correct 185378SN/A/// behavior of the program. Print a warning only if the appropriate 186378SN/A/// trace flag is enabled. Return success to the target program. 1871706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1883114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 189360SN/A 1906109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1911706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1923114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 193378SN/A 1946109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1956109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1966109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 1976109Ssanchezd@stanford.edu 198378SN/A/// Target getpagesize() handler. 1991706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 2003114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 201378SN/A 2025748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2035748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2045748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 205378SN/A 206378SN/A/// Target close() handler. 2071706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2083114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 209378SN/A 210378SN/A/// Target read() handler. 2111706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2123114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 213378SN/A 214378SN/A/// Target write() handler. 2151706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2163114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 217378SN/A 218378SN/A/// Target lseek() handler. 2191706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2203114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 221378SN/A 2224118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2234118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2244118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2254118Sgblack@eecs.umich.edu 226378SN/A/// Target munmap() handler. 2271706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2283114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 229378SN/A 230378SN/A/// Target gethostname() handler. 2311706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2323114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 233360SN/A 2345513SMichael.Adler@intel.com/// Target getcwd() handler. 2355513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2365513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2375513SMichael.Adler@intel.com 2385513SMichael.Adler@intel.com/// Target unlink() handler. 2395513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2405513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2415513SMichael.Adler@intel.com 242511SN/A/// Target unlink() handler. 2431706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2443114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 245511SN/A 2465513SMichael.Adler@intel.com/// Target mkdir() handler. 2475513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2485513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2495513SMichael.Adler@intel.com 250511SN/A/// Target rename() handler. 2511706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2523114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2531706SN/A 2541706SN/A 2551706SN/A/// Target truncate() handler. 2561706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2573114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2581706SN/A 2591706SN/A 2601706SN/A/// Target ftruncate() handler. 2611706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2623114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2631706SN/A 264511SN/A 2656703Svince@csl.cornell.edu/// Target truncate64() handler. 2666703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2676703Svince@csl.cornell.edu LiveProcess *p, ThreadContext *tc); 2686703Svince@csl.cornell.edu 2696685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2706685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2716685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2726685Stjones1@inf.ed.ac.uk 2736685Stjones1@inf.ed.ac.uk 2745513SMichael.Adler@intel.com/// Target umask() handler. 2755513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2765513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2775513SMichael.Adler@intel.com 2785513SMichael.Adler@intel.com 2791999SN/A/// Target chown() handler. 2801999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2813114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2821999SN/A 2831999SN/A 2841999SN/A/// Target fchown() handler. 2851999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2863114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2871999SN/A 2883079Sstever@eecs.umich.edu/// Target dup() handler. 2893079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2903114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2913079Sstever@eecs.umich.edu 2922093SN/A/// Target fnctl() handler. 2932093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2943114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2952093SN/A 2962687Sksewell@umich.edu/// Target fcntl64() handler. 2972687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2983114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2992687Sksewell@umich.edu 3002238SN/A/// Target setuid() handler. 3012238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 3023114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3032238SN/A 3042238SN/A/// Target getpid() handler. 3052238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3063114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3072238SN/A 3082238SN/A/// Target getuid() handler. 3092238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3103114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3112238SN/A 3122238SN/A/// Target getgid() handler. 3132238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3143114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3152238SN/A 3162238SN/A/// Target getppid() handler. 3172238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3183114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3192238SN/A 3202238SN/A/// Target geteuid() handler. 3212238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3223114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3232238SN/A 3242238SN/A/// Target getegid() handler. 3252238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3263114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3272238SN/A 3286109Ssanchezd@stanford.edu/// Target clone() handler. 3296109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3306109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3312238SN/A 3322238SN/A 3332238SN/A/// Pseudo Funcs - These functions use a different return convension, 3342238SN/A/// returning a second value in a register other than the normal return register 3352238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3363114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3372238SN/A 3382238SN/A/// Target getpidPseudo() handler. 3392238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3403114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3412238SN/A 3422238SN/A/// Target getuidPseudo() handler. 3432238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3443114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3452238SN/A 3462238SN/A/// Target getgidPseudo() handler. 3472238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3483114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3492238SN/A 3502238SN/A 3511354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3521354SN/Aconst int one_million = 1000000; 3531354SN/A 3541354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3551354SN/A/// by my reckoning. We want to keep this a constant (not use the 3561354SN/A/// real-world time) to keep simulations repeatable. 3571354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3581354SN/A 3591354SN/A/// Helper function to convert current elapsed time to seconds and 3601354SN/A/// microseconds. 3611354SN/Atemplate <class T1, class T2> 3621354SN/Avoid 3631354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3641354SN/A{ 3657064Snate@binkert.org int elapsed_usecs = curTick / SimClock::Int::us; 3661354SN/A sec = elapsed_usecs / one_million; 3671354SN/A usec = elapsed_usecs % one_million; 3681354SN/A} 3691354SN/A 370360SN/A////////////////////////////////////////////////////////////////////// 371360SN/A// 372360SN/A// The following emulation functions are generic, but need to be 373360SN/A// templated to account for differences in types, constants, etc. 374360SN/A// 375360SN/A////////////////////////////////////////////////////////////////////// 376360SN/A 3773113Sgblack@eecs.umich.edu#if NO_STAT64 3783113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3793113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3803113Sgblack@eecs.umich.edu#else 3813113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3823113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3833113Sgblack@eecs.umich.edu#endif 3843113Sgblack@eecs.umich.edu 3853113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3863113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3873113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3883113Sgblack@eecs.umich.edu 3893113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3903113Sgblack@eecs.umich.edustatic void 3913113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3923113Sgblack@eecs.umich.edu{ 3934189Sgblack@eecs.umich.edu using namespace TheISA; 3944189Sgblack@eecs.umich.edu 3953113Sgblack@eecs.umich.edu if (fakeTTY) 3963113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 3973113Sgblack@eecs.umich.edu else 3983113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 3993113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 4003113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 4013113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 4023277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4035515SMichael.Adler@intel.com if (fakeTTY) { 4045515SMichael.Adler@intel.com // Claim to be a character device 4055515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4065515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4075515SMichael.Adler@intel.com } 4083277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 4093277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4103277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 4113277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4123277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4133277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4143277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4153113Sgblack@eecs.umich.edu if (fakeTTY) 4163113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4173113Sgblack@eecs.umich.edu else 4183113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4193113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4203113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4213113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4223114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4233113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4243114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4253113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4263114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4273113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4284061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4294061Sgblack@eecs.umich.edu // consistently across different hosts. 4304061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4313113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4323113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4333113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4343113Sgblack@eecs.umich.edu} 4353113Sgblack@eecs.umich.edu 4363113Sgblack@eecs.umich.edu// Same for stat64 4373113Sgblack@eecs.umich.edu 4383113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4393113Sgblack@eecs.umich.edustatic void 4403113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4413113Sgblack@eecs.umich.edu{ 4424189Sgblack@eecs.umich.edu using namespace TheISA; 4434189Sgblack@eecs.umich.edu 4443113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4453113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4463113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4473113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4483113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4493113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4503113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4513113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4523113Sgblack@eecs.umich.edu#else 4533113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4543113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4553113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4563113Sgblack@eecs.umich.edu#endif 4573113Sgblack@eecs.umich.edu} 4583113Sgblack@eecs.umich.edu 4593113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4603113Sgblack@eecs.umich.edutemplate<class OS> 4613113Sgblack@eecs.umich.edustatic void 4623113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4633113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4643113Sgblack@eecs.umich.edu{ 4653113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4663113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4673113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4683113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4693113Sgblack@eecs.umich.edu} 4703113Sgblack@eecs.umich.edu 4713113Sgblack@eecs.umich.edutemplate<class OS> 4723113Sgblack@eecs.umich.edustatic void 4733113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4743113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4753113Sgblack@eecs.umich.edu{ 4763113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4773113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4786686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4793113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4803113Sgblack@eecs.umich.edu} 4813113Sgblack@eecs.umich.edu 482378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 483378SN/A/// only to find out if their stdout is a tty, to determine whether to 484378SN/A/// do line or block buffering. 485360SN/Atemplate <class OS> 4861450SN/ASyscallReturn 4873114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4882680Sktlim@umich.edu ThreadContext *tc) 489360SN/A{ 4906701Sgblack@eecs.umich.edu int index = 0; 4916701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 4926701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 493360SN/A 4941969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 495360SN/A 496360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 497360SN/A // doesn't map to any simulator fd: not a valid target fd 4981458SN/A return -EBADF; 499360SN/A } 500360SN/A 501360SN/A switch (req) { 5024131Sbinkertn@umich.edu case OS::TIOCISATTY_: 5034131Sbinkertn@umich.edu case OS::TIOCGETP_: 5044131Sbinkertn@umich.edu case OS::TIOCSETP_: 5054131Sbinkertn@umich.edu case OS::TIOCSETN_: 5064131Sbinkertn@umich.edu case OS::TIOCSETC_: 5074131Sbinkertn@umich.edu case OS::TIOCGETC_: 5084131Sbinkertn@umich.edu case OS::TIOCGETS_: 5094131Sbinkertn@umich.edu case OS::TIOCGETA_: 5106689Stjones1@inf.ed.ac.uk case OS::TCSETAW_: 5111458SN/A return -ENOTTY; 512360SN/A 513360SN/A default: 5141706SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 5152680Sktlim@umich.edu fd, req, tc->readPC()); 516360SN/A } 517360SN/A} 518360SN/A 519378SN/A/// Target open() handler. 520360SN/Atemplate <class OS> 5211450SN/ASyscallReturn 5223114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5232680Sktlim@umich.edu ThreadContext *tc) 524360SN/A{ 525360SN/A std::string path; 526360SN/A 5276701Sgblack@eecs.umich.edu int index = 0; 5286701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 5296701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 5301458SN/A return -EFAULT; 531360SN/A 532360SN/A if (path == "/dev/sysdev0") { 533360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 534360SN/A // We don't support it, so just punt. 5351706SN/A warn("Ignoring open(%s, ...)\n", path); 5361458SN/A return -ENOENT; 537360SN/A } 538360SN/A 5396701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 5406701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 541360SN/A int hostFlags = 0; 542360SN/A 543360SN/A // translate open flags 544360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 545360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 546360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 547360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 548360SN/A } 549360SN/A } 550360SN/A 551360SN/A // any target flags left? 552360SN/A if (tgtFlags != 0) 5531706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 554360SN/A 555360SN/A#ifdef __CYGWIN32__ 556360SN/A hostFlags |= O_BINARY; 557360SN/A#endif 558360SN/A 5593669Sbinkertn@umich.edu // Adjust path for current working directory 5603669Sbinkertn@umich.edu path = process->fullPath(path); 5613669Sbinkertn@umich.edu 5621706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5631706SN/A 5645795Ssaidi@eecs.umich.edu int fd; 5655795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5665795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5675795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5685795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5695795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5705795Ssaidi@eecs.umich.edu } else { 5715795Ssaidi@eecs.umich.edu // open the file 5725795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5735795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5745795Ssaidi@eecs.umich.edu } 575360SN/A 576360SN/A} 577360SN/A 5786640Svince@csl.cornell.edu/// Target sysinfo() handler. 5796640Svince@csl.cornell.edutemplate <class OS> 5806640Svince@csl.cornell.eduSyscallReturn 5816640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5826640Svince@csl.cornell.edu ThreadContext *tc) 5836640Svince@csl.cornell.edu{ 5846640Svince@csl.cornell.edu 5856701Sgblack@eecs.umich.edu int index = 0; 5866701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 5876701Sgblack@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 5886640Svince@csl.cornell.edu 5896701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 5906701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 5916640Svince@csl.cornell.edu 5926701Sgblack@eecs.umich.edu sysinfo.copyOut(tc->getMemPort()); 5936640Svince@csl.cornell.edu 5946701Sgblack@eecs.umich.edu return 0; 5956640Svince@csl.cornell.edu} 596360SN/A 5971999SN/A/// Target chmod() handler. 5981999SN/Atemplate <class OS> 5991999SN/ASyscallReturn 6003114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6012680Sktlim@umich.edu ThreadContext *tc) 6021999SN/A{ 6031999SN/A std::string path; 6041999SN/A 6056701Sgblack@eecs.umich.edu int index = 0; 6066701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 6076701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 6081999SN/A return -EFAULT; 6096701Sgblack@eecs.umich.edu } 6101999SN/A 6116701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6121999SN/A mode_t hostMode = 0; 6131999SN/A 6141999SN/A // XXX translate mode flags via OS::something??? 6151999SN/A hostMode = mode; 6161999SN/A 6173669Sbinkertn@umich.edu // Adjust path for current working directory 6183669Sbinkertn@umich.edu path = process->fullPath(path); 6193669Sbinkertn@umich.edu 6201999SN/A // do the chmod 6211999SN/A int result = chmod(path.c_str(), hostMode); 6221999SN/A if (result < 0) 6232218SN/A return -errno; 6241999SN/A 6251999SN/A return 0; 6261999SN/A} 6271999SN/A 6281999SN/A 6291999SN/A/// Target fchmod() handler. 6301999SN/Atemplate <class OS> 6311999SN/ASyscallReturn 6323114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6332680Sktlim@umich.edu ThreadContext *tc) 6341999SN/A{ 6356701Sgblack@eecs.umich.edu int index = 0; 6366701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6371999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6381999SN/A // doesn't map to any simulator fd: not a valid target fd 6391999SN/A return -EBADF; 6401999SN/A } 6411999SN/A 6426701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6431999SN/A mode_t hostMode = 0; 6441999SN/A 6451999SN/A // XXX translate mode flags via OS::someting??? 6461999SN/A hostMode = mode; 6471999SN/A 6481999SN/A // do the fchmod 6491999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6501999SN/A if (result < 0) 6512218SN/A return -errno; 6521999SN/A 6531999SN/A return 0; 6541999SN/A} 6551999SN/A 6565877Shsul@eecs.umich.edu/// Target mremap() handler. 6575877Shsul@eecs.umich.edutemplate <class OS> 6585877Shsul@eecs.umich.eduSyscallReturn 6595877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6605877Shsul@eecs.umich.edu{ 6616701Sgblack@eecs.umich.edu int index = 0; 6626701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 6636701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 6646701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 6656701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 6665877Shsul@eecs.umich.edu 6675877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6685877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6695877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6705877Shsul@eecs.umich.edu return -EINVAL; 6715877Shsul@eecs.umich.edu } 6725877Shsul@eecs.umich.edu 6735877Shsul@eecs.umich.edu if (new_length > old_length) { 6745877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6755877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6765877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6775877Shsul@eecs.umich.edu process->mmap_end += diff; 6785877Shsul@eecs.umich.edu return start; 6795877Shsul@eecs.umich.edu } else { 6805877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6815877Shsul@eecs.umich.edu if (!(flags & 1)) { 6825877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6835877Shsul@eecs.umich.edu return -ENOMEM; 6845877Shsul@eecs.umich.edu } else { 6855877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6865877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6875877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6885877Shsul@eecs.umich.edu start = process->mmap_end; 6895877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6905877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6915877Shsul@eecs.umich.edu process->mmap_end += new_length; 6925877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6935877Shsul@eecs.umich.edu return start; 6945877Shsul@eecs.umich.edu } 6955877Shsul@eecs.umich.edu } 6965877Shsul@eecs.umich.edu } else { 6975877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 6985877Shsul@eecs.umich.edu new_length); 6995877Shsul@eecs.umich.edu return start; 7005877Shsul@eecs.umich.edu } 7015877Shsul@eecs.umich.edu} 7021999SN/A 703378SN/A/// Target stat() handler. 704360SN/Atemplate <class OS> 7051450SN/ASyscallReturn 7063114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7072680Sktlim@umich.edu ThreadContext *tc) 708360SN/A{ 709360SN/A std::string path; 710360SN/A 7116701Sgblack@eecs.umich.edu int index = 0; 7126701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7136701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7146701Sgblack@eecs.umich.edu return -EFAULT; 7156701Sgblack@eecs.umich.edu } 7166701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 717360SN/A 7183669Sbinkertn@umich.edu // Adjust path for current working directory 7193669Sbinkertn@umich.edu path = process->fullPath(path); 7203669Sbinkertn@umich.edu 721360SN/A struct stat hostBuf; 722360SN/A int result = stat(path.c_str(), &hostBuf); 723360SN/A 724360SN/A if (result < 0) 7252218SN/A return -errno; 726360SN/A 7276701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 728360SN/A 7291458SN/A return 0; 730360SN/A} 731360SN/A 732360SN/A 7335074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7345074Ssaidi@eecs.umich.edutemplate <class OS> 7355074Ssaidi@eecs.umich.eduSyscallReturn 7365074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7375074Ssaidi@eecs.umich.edu ThreadContext *tc) 7385074Ssaidi@eecs.umich.edu{ 7395074Ssaidi@eecs.umich.edu std::string path; 7405074Ssaidi@eecs.umich.edu 7416701Sgblack@eecs.umich.edu int index = 0; 7426701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7436701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 7445074Ssaidi@eecs.umich.edu return -EFAULT; 7456701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7465074Ssaidi@eecs.umich.edu 7475074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7485074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7495074Ssaidi@eecs.umich.edu 7505208Ssaidi@eecs.umich.edu#if NO_STAT64 7515208Ssaidi@eecs.umich.edu struct stat hostBuf; 7525208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7535208Ssaidi@eecs.umich.edu#else 7545074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7555074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7565208Ssaidi@eecs.umich.edu#endif 7575074Ssaidi@eecs.umich.edu 7585074Ssaidi@eecs.umich.edu if (result < 0) 7595074Ssaidi@eecs.umich.edu return -errno; 7605074Ssaidi@eecs.umich.edu 7616701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 7625074Ssaidi@eecs.umich.edu 7635074Ssaidi@eecs.umich.edu return 0; 7645074Ssaidi@eecs.umich.edu} 7655074Ssaidi@eecs.umich.edu 7665074Ssaidi@eecs.umich.edu 7671999SN/A/// Target fstat64() handler. 7681999SN/Atemplate <class OS> 7691999SN/ASyscallReturn 7703114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7712680Sktlim@umich.edu ThreadContext *tc) 7721999SN/A{ 7736701Sgblack@eecs.umich.edu int index = 0; 7746701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 7756701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7761999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7771999SN/A // doesn't map to any simulator fd: not a valid target fd 7781999SN/A return -EBADF; 7791999SN/A } 7801999SN/A 7812764Sstever@eecs.umich.edu#if NO_STAT64 7822064SN/A struct stat hostBuf; 7832064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7842064SN/A#else 7852064SN/A struct stat64 hostBuf; 7861999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7872064SN/A#endif 7881999SN/A 7891999SN/A if (result < 0) 7902218SN/A return -errno; 7911999SN/A 7926701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 7931999SN/A 7941999SN/A return 0; 7951999SN/A} 7961999SN/A 7971999SN/A 798378SN/A/// Target lstat() handler. 799360SN/Atemplate <class OS> 8001450SN/ASyscallReturn 8013114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8022680Sktlim@umich.edu ThreadContext *tc) 803360SN/A{ 804360SN/A std::string path; 805360SN/A 8066701Sgblack@eecs.umich.edu int index = 0; 8076701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8086701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8096701Sgblack@eecs.umich.edu return -EFAULT; 8106701Sgblack@eecs.umich.edu } 8116701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 812360SN/A 8133669Sbinkertn@umich.edu // Adjust path for current working directory 8143669Sbinkertn@umich.edu path = process->fullPath(path); 8153669Sbinkertn@umich.edu 816360SN/A struct stat hostBuf; 817360SN/A int result = lstat(path.c_str(), &hostBuf); 818360SN/A 819360SN/A if (result < 0) 8201458SN/A return -errno; 821360SN/A 8226701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 823360SN/A 8241458SN/A return 0; 825360SN/A} 826360SN/A 8271999SN/A/// Target lstat64() handler. 8281999SN/Atemplate <class OS> 8291999SN/ASyscallReturn 8303114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 8312680Sktlim@umich.edu ThreadContext *tc) 8321999SN/A{ 8331999SN/A std::string path; 8341999SN/A 8356701Sgblack@eecs.umich.edu int index = 0; 8366701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8376701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8386701Sgblack@eecs.umich.edu return -EFAULT; 8396701Sgblack@eecs.umich.edu } 8406701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8411999SN/A 8423669Sbinkertn@umich.edu // Adjust path for current working directory 8433669Sbinkertn@umich.edu path = process->fullPath(path); 8443669Sbinkertn@umich.edu 8452764Sstever@eecs.umich.edu#if NO_STAT64 8462064SN/A struct stat hostBuf; 8472064SN/A int result = lstat(path.c_str(), &hostBuf); 8482064SN/A#else 8491999SN/A struct stat64 hostBuf; 8501999SN/A int result = lstat64(path.c_str(), &hostBuf); 8512064SN/A#endif 8521999SN/A 8531999SN/A if (result < 0) 8541999SN/A return -errno; 8551999SN/A 8566701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 8571999SN/A 8581999SN/A return 0; 8591999SN/A} 8601999SN/A 861378SN/A/// Target fstat() handler. 862360SN/Atemplate <class OS> 8631450SN/ASyscallReturn 8643114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8652680Sktlim@umich.edu ThreadContext *tc) 866360SN/A{ 8676701Sgblack@eecs.umich.edu int index = 0; 8686701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 8696701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 870360SN/A 8711969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 872360SN/A 873360SN/A if (fd < 0) 8741458SN/A return -EBADF; 875360SN/A 876360SN/A struct stat hostBuf; 877360SN/A int result = fstat(fd, &hostBuf); 878360SN/A 879360SN/A if (result < 0) 8801458SN/A return -errno; 881360SN/A 8826701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 8832021SN/A 8841458SN/A return 0; 885360SN/A} 886360SN/A 887360SN/A 8881706SN/A/// Target statfs() handler. 8891706SN/Atemplate <class OS> 8901706SN/ASyscallReturn 8913114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8922680Sktlim@umich.edu ThreadContext *tc) 8931706SN/A{ 8941706SN/A std::string path; 8951706SN/A 8966701Sgblack@eecs.umich.edu int index = 0; 8976701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8986701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8996701Sgblack@eecs.umich.edu return -EFAULT; 9006701Sgblack@eecs.umich.edu } 9016701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9021706SN/A 9033669Sbinkertn@umich.edu // Adjust path for current working directory 9043669Sbinkertn@umich.edu path = process->fullPath(path); 9053669Sbinkertn@umich.edu 9061706SN/A struct statfs hostBuf; 9071706SN/A int result = statfs(path.c_str(), &hostBuf); 9081706SN/A 9091706SN/A if (result < 0) 9102218SN/A return -errno; 9111706SN/A 9126701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9131706SN/A 9141706SN/A return 0; 9151706SN/A} 9161706SN/A 9171706SN/A 9181706SN/A/// Target fstatfs() handler. 9191706SN/Atemplate <class OS> 9201706SN/ASyscallReturn 9213114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9222680Sktlim@umich.edu ThreadContext *tc) 9231706SN/A{ 9246701Sgblack@eecs.umich.edu int index = 0; 9256701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 9266701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9271706SN/A 9281706SN/A if (fd < 0) 9291706SN/A return -EBADF; 9301706SN/A 9311706SN/A struct statfs hostBuf; 9321706SN/A int result = fstatfs(fd, &hostBuf); 9331706SN/A 9341706SN/A if (result < 0) 9352218SN/A return -errno; 9361706SN/A 9376701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9381706SN/A 9391706SN/A return 0; 9401706SN/A} 9411706SN/A 9421706SN/A 9431999SN/A/// Target writev() handler. 9441999SN/Atemplate <class OS> 9451999SN/ASyscallReturn 9463114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9472680Sktlim@umich.edu ThreadContext *tc) 9481999SN/A{ 9496701Sgblack@eecs.umich.edu int index = 0; 9506701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 9511999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9521999SN/A // doesn't map to any simulator fd: not a valid target fd 9531999SN/A return -EBADF; 9541999SN/A } 9551999SN/A 9562680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9576701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 9586701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 9591999SN/A struct iovec hiov[count]; 9606227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9611999SN/A typename OS::tgt_iovec tiov; 9622461SN/A 9632461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9642461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9652091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9661999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9672461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9682461SN/A hiov[i].iov_len); 9691999SN/A } 9701999SN/A 9711999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9721999SN/A 9736227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9741999SN/A delete [] (char *)hiov[i].iov_base; 9751999SN/A 9761999SN/A if (result < 0) 9772218SN/A return -errno; 9781999SN/A 9791999SN/A return 0; 9801999SN/A} 9811999SN/A 9821999SN/A 983378SN/A/// Target mmap() handler. 984378SN/A/// 985378SN/A/// We don't really handle mmap(). If the target is mmaping an 986378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 987378SN/A/// nothing (since memory is initialized to zero and the simulator 988378SN/A/// doesn't really check addresses anyway). Always print a warning, 989378SN/A/// since this could be seriously broken if we're not mapping 990378SN/A/// /dev/zero. 991360SN/A// 992378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 993378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 994378SN/A/// anything else. 995360SN/Atemplate <class OS> 9961450SN/ASyscallReturn 9973114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 998360SN/A{ 9996701Sgblack@eecs.umich.edu int index = 0; 10006701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 10016701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 10026701Sgblack@eecs.umich.edu index++; // int prot = p->getSyscallArg(tc, index); 10036701Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, index); 10046701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 10056701Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, index); 1006360SN/A 10075877Shsul@eecs.umich.edu 10082544SN/A if ((start % TheISA::VMPageSize) != 0 || 10092544SN/A (length % TheISA::VMPageSize) != 0) { 10102544SN/A warn("mmap failing: arguments not page-aligned: " 10112544SN/A "start 0x%x length 0x%x", 10122544SN/A start, length); 10132544SN/A return -EINVAL; 1014360SN/A } 1015360SN/A 10162544SN/A if (start != 0) { 10172544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 10182544SN/A start, p->mmap_end); 10192544SN/A } 10202544SN/A 10212544SN/A // pick next address from our "mmap region" 10226672Sgblack@eecs.umich.edu if (OS::mmapGrowsDown()) { 10236672Sgblack@eecs.umich.edu start = p->mmap_end - length; 10246672Sgblack@eecs.umich.edu p->mmap_end = start; 10256672Sgblack@eecs.umich.edu } else { 10266672Sgblack@eecs.umich.edu start = p->mmap_end; 10276672Sgblack@eecs.umich.edu p->mmap_end += length; 10286672Sgblack@eecs.umich.edu } 10292544SN/A p->pTable->allocate(start, length); 10302544SN/A 10312553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 10321969SN/A warn("allowing mmap of file @ fd %d. " 10336701Sgblack@eecs.umich.edu "This will break if not /dev/zero.", fd); 1034360SN/A } 1035360SN/A 10361458SN/A return start; 1037360SN/A} 1038360SN/A 1039378SN/A/// Target getrlimit() handler. 1040360SN/Atemplate <class OS> 10411450SN/ASyscallReturn 10423114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10432680Sktlim@umich.edu ThreadContext *tc) 1044360SN/A{ 10456701Sgblack@eecs.umich.edu int index = 0; 10466701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 10476701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1048360SN/A 1049360SN/A switch (resource) { 10502064SN/A case OS::TGT_RLIMIT_STACK: 10515877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10522064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10532091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10542091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10552064SN/A break; 1056360SN/A 10575877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10585877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10595877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10605877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10615877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10625877Shsul@eecs.umich.edu break; 10635877Shsul@eecs.umich.edu 10642064SN/A default: 10652064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10662064SN/A << std::endl; 10672064SN/A abort(); 10682064SN/A break; 1069360SN/A } 1070360SN/A 10712680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10721458SN/A return 0; 1073360SN/A} 1074360SN/A 1075378SN/A/// Target gettimeofday() handler. 1076360SN/Atemplate <class OS> 10771450SN/ASyscallReturn 10783114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10792680Sktlim@umich.edu ThreadContext *tc) 1080360SN/A{ 10816701Sgblack@eecs.umich.edu int index = 0; 10826701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1083360SN/A 1084360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1085360SN/A tp->tv_sec += seconds_since_epoch; 10866109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10876109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1088360SN/A 10892680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1090360SN/A 10911458SN/A return 0; 1092360SN/A} 1093360SN/A 1094360SN/A 10951999SN/A/// Target utimes() handler. 10961999SN/Atemplate <class OS> 10971999SN/ASyscallReturn 10983114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10992680Sktlim@umich.edu ThreadContext *tc) 11001999SN/A{ 11011999SN/A std::string path; 11021999SN/A 11036701Sgblack@eecs.umich.edu int index = 0; 11046701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 11056701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11066701Sgblack@eecs.umich.edu return -EFAULT; 11076701Sgblack@eecs.umich.edu } 11081999SN/A 11096701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 11106701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 11112680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 11121999SN/A 11131999SN/A struct timeval hostTimeval[2]; 11141999SN/A for (int i = 0; i < 2; ++i) 11151999SN/A { 11162091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 11172091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 11181999SN/A } 11193669Sbinkertn@umich.edu 11203669Sbinkertn@umich.edu // Adjust path for current working directory 11213669Sbinkertn@umich.edu path = process->fullPath(path); 11223669Sbinkertn@umich.edu 11231999SN/A int result = utimes(path.c_str(), hostTimeval); 11241999SN/A 11251999SN/A if (result < 0) 11261999SN/A return -errno; 11271999SN/A 11281999SN/A return 0; 11291999SN/A} 1130378SN/A/// Target getrusage() function. 1131360SN/Atemplate <class OS> 11321450SN/ASyscallReturn 11333114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11342680Sktlim@umich.edu ThreadContext *tc) 1135360SN/A{ 11366701Sgblack@eecs.umich.edu int index = 0; 11376701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 11386701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1139360SN/A 11403670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 11413670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1142360SN/A rup->ru_stime.tv_sec = 0; 1143360SN/A rup->ru_stime.tv_usec = 0; 1144360SN/A rup->ru_maxrss = 0; 1145360SN/A rup->ru_ixrss = 0; 1146360SN/A rup->ru_idrss = 0; 1147360SN/A rup->ru_isrss = 0; 1148360SN/A rup->ru_minflt = 0; 1149360SN/A rup->ru_majflt = 0; 1150360SN/A rup->ru_nswap = 0; 1151360SN/A rup->ru_inblock = 0; 1152360SN/A rup->ru_oublock = 0; 1153360SN/A rup->ru_msgsnd = 0; 1154360SN/A rup->ru_msgrcv = 0; 1155360SN/A rup->ru_nsignals = 0; 1156360SN/A rup->ru_nvcsw = 0; 1157360SN/A rup->ru_nivcsw = 0; 1158360SN/A 11593670Sbinkertn@umich.edu switch (who) { 11603670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11613670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11623670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11633670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11643670Sbinkertn@umich.edu break; 11653670Sbinkertn@umich.edu 11663670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11673670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11683670Sbinkertn@umich.edu break; 11693670Sbinkertn@umich.edu 11703670Sbinkertn@umich.edu default: 11713670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11723670Sbinkertn@umich.edu // plow ahead 11733670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11743670Sbinkertn@umich.edu who); 11753670Sbinkertn@umich.edu } 11763670Sbinkertn@umich.edu 11772680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1178360SN/A 11791458SN/A return 0; 1180360SN/A} 1181360SN/A 11826683Stjones1@inf.ed.ac.uk/// Target times() function. 11836683Stjones1@inf.ed.ac.uktemplate <class OS> 11846683Stjones1@inf.ed.ac.ukSyscallReturn 11856683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11866683Stjones1@inf.ed.ac.uk ThreadContext *tc) 11876683Stjones1@inf.ed.ac.uk{ 11886701Sgblack@eecs.umich.edu int index = 0; 11896701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 11906683Stjones1@inf.ed.ac.uk 11916683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 11927064Snate@binkert.org int64_t clocks = curTick * OS::M5_SC_CLK_TCK / SimClock::Int::s; 11936683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 11946683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 11956683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 11966683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 11976683Stjones1@inf.ed.ac.uk 11986683Stjones1@inf.ed.ac.uk // Convert to host endianness 11996683Stjones1@inf.ed.ac.uk bufp->tms_utime = htog(bufp->tms_utime); 12006683Stjones1@inf.ed.ac.uk 12016683Stjones1@inf.ed.ac.uk // Write back 12026683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemPort()); 12036683Stjones1@inf.ed.ac.uk 12046683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 12056683Stjones1@inf.ed.ac.uk return clocks; 12066683Stjones1@inf.ed.ac.uk} 12072553SN/A 12086684Stjones1@inf.ed.ac.uk/// Target time() function. 12096684Stjones1@inf.ed.ac.uktemplate <class OS> 12106684Stjones1@inf.ed.ac.ukSyscallReturn 12116684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 12126684Stjones1@inf.ed.ac.uk ThreadContext *tc) 12136684Stjones1@inf.ed.ac.uk{ 12146684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 12156684Stjones1@inf.ed.ac.uk getElapsedTime(sec, usec); 12166684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 12176684Stjones1@inf.ed.ac.uk 12186701Sgblack@eecs.umich.edu int index = 0; 12196701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 12206684Stjones1@inf.ed.ac.uk if(taddr != 0) { 12216684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 12226684Stjones1@inf.ed.ac.uk t = htog(t); 12236684Stjones1@inf.ed.ac.uk TranslatingPort *p = tc->getMemPort(); 12246684Stjones1@inf.ed.ac.uk p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 12256684Stjones1@inf.ed.ac.uk } 12266684Stjones1@inf.ed.ac.uk return sec; 12276684Stjones1@inf.ed.ac.uk} 12282553SN/A 12292553SN/A 12301354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1231