syscall_emul.hh revision 6703
1360SN/A/* 21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 3360SN/A * All rights reserved. 4360SN/A * 5360SN/A * Redistribution and use in source and binary forms, with or without 6360SN/A * modification, are permitted provided that the following conditions are 7360SN/A * met: redistributions of source code must retain the above copyright 8360SN/A * notice, this list of conditions and the following disclaimer; 9360SN/A * redistributions in binary form must reproduce the above copyright 10360SN/A * notice, this list of conditions and the following disclaimer in the 11360SN/A * documentation and/or other materials provided with the distribution; 12360SN/A * neither the name of the copyright holders nor the names of its 13360SN/A * contributors may be used to endorse or promote products derived from 14360SN/A * this software without specific prior written permission. 15360SN/A * 16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 292665Ssaidi@eecs.umich.edu * Kevin Lim 30360SN/A */ 31360SN/A 321354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 331354SN/A#define __SIM_SYSCALL_EMUL_HH__ 34360SN/A 352764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \ 362764Sstever@eecs.umich.edu defined(__FreeBSD__) || defined(__CYGWIN__)) 372064SN/A 38360SN/A/// 39360SN/A/// @file syscall_emul.hh 40360SN/A/// 41360SN/A/// This file defines objects used to emulate syscalls from the target 42360SN/A/// application on the host machine. 43360SN/A 441354SN/A#include <errno.h> 45360SN/A#include <string> 461809SN/A#ifdef __CYGWIN32__ 475543Ssaidi@eecs.umich.edu#include <sys/fcntl.h> // for O_BINARY 481809SN/A#endif 493113Sgblack@eecs.umich.edu#include <sys/stat.h> 503113Sgblack@eecs.umich.edu#include <fcntl.h> 511999SN/A#include <sys/uio.h> 52360SN/A 532474SN/A#include "base/chunk_generator.hh" 545543Ssaidi@eecs.umich.edu#include "base/intmath.hh" // for RoundUp 552462SN/A#include "base/misc.hh" 561354SN/A#include "base/trace.hh" 576216Snate@binkert.org#include "base/types.hh" 586658Snate@binkert.org#include "config/the_isa.hh" 592474SN/A#include "cpu/base.hh" 602680Sktlim@umich.edu#include "cpu/thread_context.hh" 612474SN/A#include "mem/translating_port.hh" 622474SN/A#include "mem/page_table.hh" 636640Svince@csl.cornell.edu#include "sim/system.hh" 641354SN/A#include "sim/process.hh" 65360SN/A 66360SN/A/// 67360SN/A/// System call descriptor. 68360SN/A/// 69360SN/Aclass SyscallDesc { 70360SN/A 71360SN/A public: 72360SN/A 73378SN/A /// Typedef for target syscall handler functions. 741450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 753114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 76360SN/A 775543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 785543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 795543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 80360SN/A 81360SN/A /// Flag values for controlling syscall behavior. 82360SN/A enum Flags { 83360SN/A /// Don't set return regs according to funcPtr return value. 84360SN/A /// Used for syscalls with non-standard return conventions 852680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 86360SN/A /// sigreturn). 87360SN/A SuppressReturnValue = 1 88360SN/A }; 89360SN/A 90360SN/A /// Constructor. 91360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 92360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 93360SN/A { 94360SN/A } 95360SN/A 96360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 973114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 98360SN/A}; 99360SN/A 100360SN/A 101360SN/Aclass BaseBufferArg { 102360SN/A 103360SN/A public: 104360SN/A 105360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 106360SN/A { 107360SN/A bufPtr = new uint8_t[size]; 108360SN/A // clear out buffer: in case we only partially populate this, 109360SN/A // and then do a copyOut(), we want to make sure we don't 110360SN/A // introduce any random junk into the simulated address space 111360SN/A memset(bufPtr, 0, size); 112360SN/A } 113360SN/A 114360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 115360SN/A 116360SN/A // 117360SN/A // copy data into simulator space (read from target memory) 118360SN/A // 1192400SN/A virtual bool copyIn(TranslatingPort *memport) 120360SN/A { 1212461SN/A memport->readBlob(addr, bufPtr, size); 1225543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 123360SN/A } 124360SN/A 125360SN/A // 126360SN/A // copy data out of simulator space (write to target memory) 127360SN/A // 1282400SN/A virtual bool copyOut(TranslatingPort *memport) 129360SN/A { 1302461SN/A memport->writeBlob(addr, bufPtr, size); 1315543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 132360SN/A } 133360SN/A 134360SN/A protected: 135360SN/A Addr addr; 136360SN/A int size; 137360SN/A uint8_t *bufPtr; 138360SN/A}; 139360SN/A 140360SN/A 141360SN/Aclass BufferArg : public BaseBufferArg 142360SN/A{ 143360SN/A public: 144360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1455543Ssaidi@eecs.umich.edu void *bufferPtr() { return bufPtr; } 146360SN/A}; 147360SN/A 148360SN/Atemplate <class T> 149360SN/Aclass TypedBufferArg : public BaseBufferArg 150360SN/A{ 151360SN/A public: 152360SN/A // user can optionally specify a specific number of bytes to 153360SN/A // allocate to deal with those structs that have variable-size 154360SN/A // arrays at the end 155360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 156360SN/A : BaseBufferArg(_addr, _size) 157360SN/A { } 158360SN/A 159360SN/A // type case 160360SN/A operator T*() { return (T *)bufPtr; } 161360SN/A 162360SN/A // dereference operators 1635543Ssaidi@eecs.umich.edu T &operator*() { return *((T *)bufPtr); } 1645543Ssaidi@eecs.umich.edu T* operator->() { return (T *)bufPtr; } 165502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 166360SN/A}; 167360SN/A 168360SN/A////////////////////////////////////////////////////////////////////// 169360SN/A// 170360SN/A// The following emulation functions are generic enough that they 171360SN/A// don't need to be recompiled for different emulated OS's. They are 172360SN/A// defined in sim/syscall_emul.cc. 173360SN/A// 174360SN/A////////////////////////////////////////////////////////////////////// 175360SN/A 176360SN/A 177378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1781706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1793114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 180378SN/A 181378SN/A/// Handler for unimplemented syscalls that we never intend to 182378SN/A/// implement (signal handling, etc.) and should not affect the correct 183378SN/A/// behavior of the program. Print a warning only if the appropriate 184378SN/A/// trace flag is enabled. Return success to the target program. 1851706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1863114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 187360SN/A 1886109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1891706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1903114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 191378SN/A 1926109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1936109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1946109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 1956109Ssanchezd@stanford.edu 196378SN/A/// Target getpagesize() handler. 1971706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1983114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 199378SN/A 2005748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2015748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2025748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 203378SN/A 204378SN/A/// Target close() handler. 2051706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2063114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 207378SN/A 208378SN/A/// Target read() handler. 2091706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2103114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 211378SN/A 212378SN/A/// Target write() handler. 2131706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2143114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 215378SN/A 216378SN/A/// Target lseek() handler. 2171706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2183114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 219378SN/A 2204118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2214118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2224118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2234118Sgblack@eecs.umich.edu 224378SN/A/// Target munmap() handler. 2251706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2263114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 227378SN/A 228378SN/A/// Target gethostname() handler. 2291706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2303114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 231360SN/A 2325513SMichael.Adler@intel.com/// Target getcwd() handler. 2335513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2345513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2355513SMichael.Adler@intel.com 2365513SMichael.Adler@intel.com/// Target unlink() handler. 2375513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2385513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2395513SMichael.Adler@intel.com 240511SN/A/// Target unlink() handler. 2411706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2423114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 243511SN/A 2445513SMichael.Adler@intel.com/// Target mkdir() handler. 2455513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2465513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2475513SMichael.Adler@intel.com 248511SN/A/// Target rename() handler. 2491706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2503114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2511706SN/A 2521706SN/A 2531706SN/A/// Target truncate() handler. 2541706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2553114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2561706SN/A 2571706SN/A 2581706SN/A/// Target ftruncate() handler. 2591706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2603114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2611706SN/A 262511SN/A 2636703Svince@csl.cornell.edu/// Target truncate64() handler. 2646703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2656703Svince@csl.cornell.edu LiveProcess *p, ThreadContext *tc); 2666703Svince@csl.cornell.edu 2676685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2686685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2696685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2706685Stjones1@inf.ed.ac.uk 2716685Stjones1@inf.ed.ac.uk 2725513SMichael.Adler@intel.com/// Target umask() handler. 2735513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2745513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2755513SMichael.Adler@intel.com 2765513SMichael.Adler@intel.com 2771999SN/A/// Target chown() handler. 2781999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2793114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2801999SN/A 2811999SN/A 2821999SN/A/// Target fchown() handler. 2831999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2843114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2851999SN/A 2863079Sstever@eecs.umich.edu/// Target dup() handler. 2873079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2883114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2893079Sstever@eecs.umich.edu 2902093SN/A/// Target fnctl() handler. 2912093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2923114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2932093SN/A 2942687Sksewell@umich.edu/// Target fcntl64() handler. 2952687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2963114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2972687Sksewell@umich.edu 2982238SN/A/// Target setuid() handler. 2992238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 3003114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3012238SN/A 3022238SN/A/// Target getpid() handler. 3032238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3043114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3052238SN/A 3062238SN/A/// Target getuid() handler. 3072238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3083114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3092238SN/A 3102238SN/A/// Target getgid() handler. 3112238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3123114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3132238SN/A 3142238SN/A/// Target getppid() handler. 3152238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3163114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3172238SN/A 3182238SN/A/// Target geteuid() handler. 3192238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3203114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3212238SN/A 3222238SN/A/// Target getegid() handler. 3232238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3243114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3252238SN/A 3266109Ssanchezd@stanford.edu/// Target clone() handler. 3276109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3286109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3292238SN/A 3302238SN/A 3312238SN/A/// Pseudo Funcs - These functions use a different return convension, 3322238SN/A/// returning a second value in a register other than the normal return register 3332238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3343114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3352238SN/A 3362238SN/A/// Target getpidPseudo() handler. 3372238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3383114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3392238SN/A 3402238SN/A/// Target getuidPseudo() handler. 3412238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3423114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3432238SN/A 3442238SN/A/// Target getgidPseudo() handler. 3452238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3463114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3472238SN/A 3482238SN/A 3491354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3501354SN/Aconst int one_million = 1000000; 3511354SN/A 3521354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3531354SN/A/// by my reckoning. We want to keep this a constant (not use the 3541354SN/A/// real-world time) to keep simulations repeatable. 3551354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3561354SN/A 3571354SN/A/// Helper function to convert current elapsed time to seconds and 3581354SN/A/// microseconds. 3591354SN/Atemplate <class T1, class T2> 3601354SN/Avoid 3611354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3621354SN/A{ 3631609SN/A int elapsed_usecs = curTick / Clock::Int::us; 3641354SN/A sec = elapsed_usecs / one_million; 3651354SN/A usec = elapsed_usecs % one_million; 3661354SN/A} 3671354SN/A 368360SN/A////////////////////////////////////////////////////////////////////// 369360SN/A// 370360SN/A// The following emulation functions are generic, but need to be 371360SN/A// templated to account for differences in types, constants, etc. 372360SN/A// 373360SN/A////////////////////////////////////////////////////////////////////// 374360SN/A 3753113Sgblack@eecs.umich.edu#if NO_STAT64 3763113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3773113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3783113Sgblack@eecs.umich.edu#else 3793113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3803113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3813113Sgblack@eecs.umich.edu#endif 3823113Sgblack@eecs.umich.edu 3833113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3843113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3853113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3863113Sgblack@eecs.umich.edu 3873113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3883113Sgblack@eecs.umich.edustatic void 3893113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3903113Sgblack@eecs.umich.edu{ 3914189Sgblack@eecs.umich.edu using namespace TheISA; 3924189Sgblack@eecs.umich.edu 3933113Sgblack@eecs.umich.edu if (fakeTTY) 3943113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 3953113Sgblack@eecs.umich.edu else 3963113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 3973113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 3983113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 3993113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 4003277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4015515SMichael.Adler@intel.com if (fakeTTY) { 4025515SMichael.Adler@intel.com // Claim to be a character device 4035515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4045515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4055515SMichael.Adler@intel.com } 4063277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 4073277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4083277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 4093277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4103277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4113277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4123277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4133113Sgblack@eecs.umich.edu if (fakeTTY) 4143113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4153113Sgblack@eecs.umich.edu else 4163113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4173113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4183113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4193113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4203114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4213113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4223114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4233113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4243114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4253113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4264061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4274061Sgblack@eecs.umich.edu // consistently across different hosts. 4284061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4293113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4303113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4313113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4323113Sgblack@eecs.umich.edu} 4333113Sgblack@eecs.umich.edu 4343113Sgblack@eecs.umich.edu// Same for stat64 4353113Sgblack@eecs.umich.edu 4363113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4373113Sgblack@eecs.umich.edustatic void 4383113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4393113Sgblack@eecs.umich.edu{ 4404189Sgblack@eecs.umich.edu using namespace TheISA; 4414189Sgblack@eecs.umich.edu 4423113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4433113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4443113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4453113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4463113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4473113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4483113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4493113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4503113Sgblack@eecs.umich.edu#else 4513113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4523113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4533113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4543113Sgblack@eecs.umich.edu#endif 4553113Sgblack@eecs.umich.edu} 4563113Sgblack@eecs.umich.edu 4573113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4583113Sgblack@eecs.umich.edutemplate<class OS> 4593113Sgblack@eecs.umich.edustatic void 4603113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4613113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4623113Sgblack@eecs.umich.edu{ 4633113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4643113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4653113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4663113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4673113Sgblack@eecs.umich.edu} 4683113Sgblack@eecs.umich.edu 4693113Sgblack@eecs.umich.edutemplate<class OS> 4703113Sgblack@eecs.umich.edustatic void 4713113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4723113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4733113Sgblack@eecs.umich.edu{ 4743113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4753113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4766686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4773113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4783113Sgblack@eecs.umich.edu} 4793113Sgblack@eecs.umich.edu 480378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 481378SN/A/// only to find out if their stdout is a tty, to determine whether to 482378SN/A/// do line or block buffering. 483360SN/Atemplate <class OS> 4841450SN/ASyscallReturn 4853114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4862680Sktlim@umich.edu ThreadContext *tc) 487360SN/A{ 4886701Sgblack@eecs.umich.edu int index = 0; 4896701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 4906701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 491360SN/A 4921969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 493360SN/A 494360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 495360SN/A // doesn't map to any simulator fd: not a valid target fd 4961458SN/A return -EBADF; 497360SN/A } 498360SN/A 499360SN/A switch (req) { 5004131Sbinkertn@umich.edu case OS::TIOCISATTY_: 5014131Sbinkertn@umich.edu case OS::TIOCGETP_: 5024131Sbinkertn@umich.edu case OS::TIOCSETP_: 5034131Sbinkertn@umich.edu case OS::TIOCSETN_: 5044131Sbinkertn@umich.edu case OS::TIOCSETC_: 5054131Sbinkertn@umich.edu case OS::TIOCGETC_: 5064131Sbinkertn@umich.edu case OS::TIOCGETS_: 5074131Sbinkertn@umich.edu case OS::TIOCGETA_: 5086689Stjones1@inf.ed.ac.uk case OS::TCSETAW_: 5091458SN/A return -ENOTTY; 510360SN/A 511360SN/A default: 5121706SN/A fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n", 5132680Sktlim@umich.edu fd, req, tc->readPC()); 514360SN/A } 515360SN/A} 516360SN/A 517378SN/A/// Target open() handler. 518360SN/Atemplate <class OS> 5191450SN/ASyscallReturn 5203114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5212680Sktlim@umich.edu ThreadContext *tc) 522360SN/A{ 523360SN/A std::string path; 524360SN/A 5256701Sgblack@eecs.umich.edu int index = 0; 5266701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 5276701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 5281458SN/A return -EFAULT; 529360SN/A 530360SN/A if (path == "/dev/sysdev0") { 531360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 532360SN/A // We don't support it, so just punt. 5331706SN/A warn("Ignoring open(%s, ...)\n", path); 5341458SN/A return -ENOENT; 535360SN/A } 536360SN/A 5376701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 5386701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 539360SN/A int hostFlags = 0; 540360SN/A 541360SN/A // translate open flags 542360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 543360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 544360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 545360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 546360SN/A } 547360SN/A } 548360SN/A 549360SN/A // any target flags left? 550360SN/A if (tgtFlags != 0) 5511706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 552360SN/A 553360SN/A#ifdef __CYGWIN32__ 554360SN/A hostFlags |= O_BINARY; 555360SN/A#endif 556360SN/A 5573669Sbinkertn@umich.edu // Adjust path for current working directory 5583669Sbinkertn@umich.edu path = process->fullPath(path); 5593669Sbinkertn@umich.edu 5601706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5611706SN/A 5625795Ssaidi@eecs.umich.edu int fd; 5635795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5645795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5655795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5665795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5675795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5685795Ssaidi@eecs.umich.edu } else { 5695795Ssaidi@eecs.umich.edu // open the file 5705795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5715795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5725795Ssaidi@eecs.umich.edu } 573360SN/A 574360SN/A} 575360SN/A 5766640Svince@csl.cornell.edu/// Target sysinfo() handler. 5776640Svince@csl.cornell.edutemplate <class OS> 5786640Svince@csl.cornell.eduSyscallReturn 5796640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5806640Svince@csl.cornell.edu ThreadContext *tc) 5816640Svince@csl.cornell.edu{ 5826640Svince@csl.cornell.edu 5836701Sgblack@eecs.umich.edu int index = 0; 5846701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 5856701Sgblack@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 5866640Svince@csl.cornell.edu 5876701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 5886701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 5896640Svince@csl.cornell.edu 5906701Sgblack@eecs.umich.edu sysinfo.copyOut(tc->getMemPort()); 5916640Svince@csl.cornell.edu 5926701Sgblack@eecs.umich.edu return 0; 5936640Svince@csl.cornell.edu} 594360SN/A 5951999SN/A/// Target chmod() handler. 5961999SN/Atemplate <class OS> 5971999SN/ASyscallReturn 5983114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5992680Sktlim@umich.edu ThreadContext *tc) 6001999SN/A{ 6011999SN/A std::string path; 6021999SN/A 6036701Sgblack@eecs.umich.edu int index = 0; 6046701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 6056701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 6061999SN/A return -EFAULT; 6076701Sgblack@eecs.umich.edu } 6081999SN/A 6096701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6101999SN/A mode_t hostMode = 0; 6111999SN/A 6121999SN/A // XXX translate mode flags via OS::something??? 6131999SN/A hostMode = mode; 6141999SN/A 6153669Sbinkertn@umich.edu // Adjust path for current working directory 6163669Sbinkertn@umich.edu path = process->fullPath(path); 6173669Sbinkertn@umich.edu 6181999SN/A // do the chmod 6191999SN/A int result = chmod(path.c_str(), hostMode); 6201999SN/A if (result < 0) 6212218SN/A return -errno; 6221999SN/A 6231999SN/A return 0; 6241999SN/A} 6251999SN/A 6261999SN/A 6271999SN/A/// Target fchmod() handler. 6281999SN/Atemplate <class OS> 6291999SN/ASyscallReturn 6303114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6312680Sktlim@umich.edu ThreadContext *tc) 6321999SN/A{ 6336701Sgblack@eecs.umich.edu int index = 0; 6346701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6351999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6361999SN/A // doesn't map to any simulator fd: not a valid target fd 6371999SN/A return -EBADF; 6381999SN/A } 6391999SN/A 6406701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6411999SN/A mode_t hostMode = 0; 6421999SN/A 6431999SN/A // XXX translate mode flags via OS::someting??? 6441999SN/A hostMode = mode; 6451999SN/A 6461999SN/A // do the fchmod 6471999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6481999SN/A if (result < 0) 6492218SN/A return -errno; 6501999SN/A 6511999SN/A return 0; 6521999SN/A} 6531999SN/A 6545877Shsul@eecs.umich.edu/// Target mremap() handler. 6555877Shsul@eecs.umich.edutemplate <class OS> 6565877Shsul@eecs.umich.eduSyscallReturn 6575877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6585877Shsul@eecs.umich.edu{ 6596701Sgblack@eecs.umich.edu int index = 0; 6606701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 6616701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 6626701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 6636701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 6645877Shsul@eecs.umich.edu 6655877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6665877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6675877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6685877Shsul@eecs.umich.edu return -EINVAL; 6695877Shsul@eecs.umich.edu } 6705877Shsul@eecs.umich.edu 6715877Shsul@eecs.umich.edu if (new_length > old_length) { 6725877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6735877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6745877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6755877Shsul@eecs.umich.edu process->mmap_end += diff; 6765877Shsul@eecs.umich.edu return start; 6775877Shsul@eecs.umich.edu } else { 6785877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6795877Shsul@eecs.umich.edu if (!(flags & 1)) { 6805877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6815877Shsul@eecs.umich.edu return -ENOMEM; 6825877Shsul@eecs.umich.edu } else { 6835877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6845877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6855877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6865877Shsul@eecs.umich.edu start = process->mmap_end; 6875877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6885877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6895877Shsul@eecs.umich.edu process->mmap_end += new_length; 6905877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6915877Shsul@eecs.umich.edu return start; 6925877Shsul@eecs.umich.edu } 6935877Shsul@eecs.umich.edu } 6945877Shsul@eecs.umich.edu } else { 6955877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 6965877Shsul@eecs.umich.edu new_length); 6975877Shsul@eecs.umich.edu return start; 6985877Shsul@eecs.umich.edu } 6995877Shsul@eecs.umich.edu} 7001999SN/A 701378SN/A/// Target stat() handler. 702360SN/Atemplate <class OS> 7031450SN/ASyscallReturn 7043114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7052680Sktlim@umich.edu ThreadContext *tc) 706360SN/A{ 707360SN/A std::string path; 708360SN/A 7096701Sgblack@eecs.umich.edu int index = 0; 7106701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7116701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7126701Sgblack@eecs.umich.edu return -EFAULT; 7136701Sgblack@eecs.umich.edu } 7146701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 715360SN/A 7163669Sbinkertn@umich.edu // Adjust path for current working directory 7173669Sbinkertn@umich.edu path = process->fullPath(path); 7183669Sbinkertn@umich.edu 719360SN/A struct stat hostBuf; 720360SN/A int result = stat(path.c_str(), &hostBuf); 721360SN/A 722360SN/A if (result < 0) 7232218SN/A return -errno; 724360SN/A 7256701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 726360SN/A 7271458SN/A return 0; 728360SN/A} 729360SN/A 730360SN/A 7315074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7325074Ssaidi@eecs.umich.edutemplate <class OS> 7335074Ssaidi@eecs.umich.eduSyscallReturn 7345074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7355074Ssaidi@eecs.umich.edu ThreadContext *tc) 7365074Ssaidi@eecs.umich.edu{ 7375074Ssaidi@eecs.umich.edu std::string path; 7385074Ssaidi@eecs.umich.edu 7396701Sgblack@eecs.umich.edu int index = 0; 7406701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7416701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 7425074Ssaidi@eecs.umich.edu return -EFAULT; 7436701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7445074Ssaidi@eecs.umich.edu 7455074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7465074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7475074Ssaidi@eecs.umich.edu 7485208Ssaidi@eecs.umich.edu#if NO_STAT64 7495208Ssaidi@eecs.umich.edu struct stat hostBuf; 7505208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7515208Ssaidi@eecs.umich.edu#else 7525074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7535074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7545208Ssaidi@eecs.umich.edu#endif 7555074Ssaidi@eecs.umich.edu 7565074Ssaidi@eecs.umich.edu if (result < 0) 7575074Ssaidi@eecs.umich.edu return -errno; 7585074Ssaidi@eecs.umich.edu 7596701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 7605074Ssaidi@eecs.umich.edu 7615074Ssaidi@eecs.umich.edu return 0; 7625074Ssaidi@eecs.umich.edu} 7635074Ssaidi@eecs.umich.edu 7645074Ssaidi@eecs.umich.edu 7651999SN/A/// Target fstat64() handler. 7661999SN/Atemplate <class OS> 7671999SN/ASyscallReturn 7683114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7692680Sktlim@umich.edu ThreadContext *tc) 7701999SN/A{ 7716701Sgblack@eecs.umich.edu int index = 0; 7726701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 7736701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7741999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7751999SN/A // doesn't map to any simulator fd: not a valid target fd 7761999SN/A return -EBADF; 7771999SN/A } 7781999SN/A 7792764Sstever@eecs.umich.edu#if NO_STAT64 7802064SN/A struct stat hostBuf; 7812064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7822064SN/A#else 7832064SN/A struct stat64 hostBuf; 7841999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7852064SN/A#endif 7861999SN/A 7871999SN/A if (result < 0) 7882218SN/A return -errno; 7891999SN/A 7906701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 7911999SN/A 7921999SN/A return 0; 7931999SN/A} 7941999SN/A 7951999SN/A 796378SN/A/// Target lstat() handler. 797360SN/Atemplate <class OS> 7981450SN/ASyscallReturn 7993114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8002680Sktlim@umich.edu ThreadContext *tc) 801360SN/A{ 802360SN/A std::string path; 803360SN/A 8046701Sgblack@eecs.umich.edu int index = 0; 8056701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8066701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8076701Sgblack@eecs.umich.edu return -EFAULT; 8086701Sgblack@eecs.umich.edu } 8096701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 810360SN/A 8113669Sbinkertn@umich.edu // Adjust path for current working directory 8123669Sbinkertn@umich.edu path = process->fullPath(path); 8133669Sbinkertn@umich.edu 814360SN/A struct stat hostBuf; 815360SN/A int result = lstat(path.c_str(), &hostBuf); 816360SN/A 817360SN/A if (result < 0) 8181458SN/A return -errno; 819360SN/A 8206701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 821360SN/A 8221458SN/A return 0; 823360SN/A} 824360SN/A 8251999SN/A/// Target lstat64() handler. 8261999SN/Atemplate <class OS> 8271999SN/ASyscallReturn 8283114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 8292680Sktlim@umich.edu ThreadContext *tc) 8301999SN/A{ 8311999SN/A std::string path; 8321999SN/A 8336701Sgblack@eecs.umich.edu int index = 0; 8346701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8356701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8366701Sgblack@eecs.umich.edu return -EFAULT; 8376701Sgblack@eecs.umich.edu } 8386701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8391999SN/A 8403669Sbinkertn@umich.edu // Adjust path for current working directory 8413669Sbinkertn@umich.edu path = process->fullPath(path); 8423669Sbinkertn@umich.edu 8432764Sstever@eecs.umich.edu#if NO_STAT64 8442064SN/A struct stat hostBuf; 8452064SN/A int result = lstat(path.c_str(), &hostBuf); 8462064SN/A#else 8471999SN/A struct stat64 hostBuf; 8481999SN/A int result = lstat64(path.c_str(), &hostBuf); 8492064SN/A#endif 8501999SN/A 8511999SN/A if (result < 0) 8521999SN/A return -errno; 8531999SN/A 8546701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 8551999SN/A 8561999SN/A return 0; 8571999SN/A} 8581999SN/A 859378SN/A/// Target fstat() handler. 860360SN/Atemplate <class OS> 8611450SN/ASyscallReturn 8623114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8632680Sktlim@umich.edu ThreadContext *tc) 864360SN/A{ 8656701Sgblack@eecs.umich.edu int index = 0; 8666701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 8676701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 868360SN/A 8691969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 870360SN/A 871360SN/A if (fd < 0) 8721458SN/A return -EBADF; 873360SN/A 874360SN/A struct stat hostBuf; 875360SN/A int result = fstat(fd, &hostBuf); 876360SN/A 877360SN/A if (result < 0) 8781458SN/A return -errno; 879360SN/A 8806701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 8812021SN/A 8821458SN/A return 0; 883360SN/A} 884360SN/A 885360SN/A 8861706SN/A/// Target statfs() handler. 8871706SN/Atemplate <class OS> 8881706SN/ASyscallReturn 8893114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8902680Sktlim@umich.edu ThreadContext *tc) 8911706SN/A{ 8921706SN/A std::string path; 8931706SN/A 8946701Sgblack@eecs.umich.edu int index = 0; 8956701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8966701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8976701Sgblack@eecs.umich.edu return -EFAULT; 8986701Sgblack@eecs.umich.edu } 8996701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9001706SN/A 9013669Sbinkertn@umich.edu // Adjust path for current working directory 9023669Sbinkertn@umich.edu path = process->fullPath(path); 9033669Sbinkertn@umich.edu 9041706SN/A struct statfs hostBuf; 9051706SN/A int result = statfs(path.c_str(), &hostBuf); 9061706SN/A 9071706SN/A if (result < 0) 9082218SN/A return -errno; 9091706SN/A 9106701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9111706SN/A 9121706SN/A return 0; 9131706SN/A} 9141706SN/A 9151706SN/A 9161706SN/A/// Target fstatfs() handler. 9171706SN/Atemplate <class OS> 9181706SN/ASyscallReturn 9193114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9202680Sktlim@umich.edu ThreadContext *tc) 9211706SN/A{ 9226701Sgblack@eecs.umich.edu int index = 0; 9236701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 9246701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9251706SN/A 9261706SN/A if (fd < 0) 9271706SN/A return -EBADF; 9281706SN/A 9291706SN/A struct statfs hostBuf; 9301706SN/A int result = fstatfs(fd, &hostBuf); 9311706SN/A 9321706SN/A if (result < 0) 9332218SN/A return -errno; 9341706SN/A 9356701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9361706SN/A 9371706SN/A return 0; 9381706SN/A} 9391706SN/A 9401706SN/A 9411999SN/A/// Target writev() handler. 9421999SN/Atemplate <class OS> 9431999SN/ASyscallReturn 9443114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9452680Sktlim@umich.edu ThreadContext *tc) 9461999SN/A{ 9476701Sgblack@eecs.umich.edu int index = 0; 9486701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 9491999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9501999SN/A // doesn't map to any simulator fd: not a valid target fd 9511999SN/A return -EBADF; 9521999SN/A } 9531999SN/A 9542680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9556701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 9566701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 9571999SN/A struct iovec hiov[count]; 9586227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9591999SN/A typename OS::tgt_iovec tiov; 9602461SN/A 9612461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9622461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9632091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9641999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9652461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9662461SN/A hiov[i].iov_len); 9671999SN/A } 9681999SN/A 9691999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9701999SN/A 9716227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9721999SN/A delete [] (char *)hiov[i].iov_base; 9731999SN/A 9741999SN/A if (result < 0) 9752218SN/A return -errno; 9761999SN/A 9771999SN/A return 0; 9781999SN/A} 9791999SN/A 9801999SN/A 981378SN/A/// Target mmap() handler. 982378SN/A/// 983378SN/A/// We don't really handle mmap(). If the target is mmaping an 984378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 985378SN/A/// nothing (since memory is initialized to zero and the simulator 986378SN/A/// doesn't really check addresses anyway). Always print a warning, 987378SN/A/// since this could be seriously broken if we're not mapping 988378SN/A/// /dev/zero. 989360SN/A// 990378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 991378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 992378SN/A/// anything else. 993360SN/Atemplate <class OS> 9941450SN/ASyscallReturn 9953114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 996360SN/A{ 9976701Sgblack@eecs.umich.edu int index = 0; 9986701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 9996701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 10006701Sgblack@eecs.umich.edu index++; // int prot = p->getSyscallArg(tc, index); 10016701Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, index); 10026701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 10036701Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, index); 1004360SN/A 10055877Shsul@eecs.umich.edu 10062544SN/A if ((start % TheISA::VMPageSize) != 0 || 10072544SN/A (length % TheISA::VMPageSize) != 0) { 10082544SN/A warn("mmap failing: arguments not page-aligned: " 10092544SN/A "start 0x%x length 0x%x", 10102544SN/A start, length); 10112544SN/A return -EINVAL; 1012360SN/A } 1013360SN/A 10142544SN/A if (start != 0) { 10152544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 10162544SN/A start, p->mmap_end); 10172544SN/A } 10182544SN/A 10192544SN/A // pick next address from our "mmap region" 10206672Sgblack@eecs.umich.edu if (OS::mmapGrowsDown()) { 10216672Sgblack@eecs.umich.edu start = p->mmap_end - length; 10226672Sgblack@eecs.umich.edu p->mmap_end = start; 10236672Sgblack@eecs.umich.edu } else { 10246672Sgblack@eecs.umich.edu start = p->mmap_end; 10256672Sgblack@eecs.umich.edu p->mmap_end += length; 10266672Sgblack@eecs.umich.edu } 10272544SN/A p->pTable->allocate(start, length); 10282544SN/A 10292553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 10301969SN/A warn("allowing mmap of file @ fd %d. " 10316701Sgblack@eecs.umich.edu "This will break if not /dev/zero.", fd); 1032360SN/A } 1033360SN/A 10341458SN/A return start; 1035360SN/A} 1036360SN/A 1037378SN/A/// Target getrlimit() handler. 1038360SN/Atemplate <class OS> 10391450SN/ASyscallReturn 10403114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10412680Sktlim@umich.edu ThreadContext *tc) 1042360SN/A{ 10436701Sgblack@eecs.umich.edu int index = 0; 10446701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 10456701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1046360SN/A 1047360SN/A switch (resource) { 10482064SN/A case OS::TGT_RLIMIT_STACK: 10495877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10502064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10512091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10522091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10532064SN/A break; 1054360SN/A 10555877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10565877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10575877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10585877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10595877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10605877Shsul@eecs.umich.edu break; 10615877Shsul@eecs.umich.edu 10622064SN/A default: 10632064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10642064SN/A << std::endl; 10652064SN/A abort(); 10662064SN/A break; 1067360SN/A } 1068360SN/A 10692680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10701458SN/A return 0; 1071360SN/A} 1072360SN/A 1073378SN/A/// Target gettimeofday() handler. 1074360SN/Atemplate <class OS> 10751450SN/ASyscallReturn 10763114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10772680Sktlim@umich.edu ThreadContext *tc) 1078360SN/A{ 10796701Sgblack@eecs.umich.edu int index = 0; 10806701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1081360SN/A 1082360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1083360SN/A tp->tv_sec += seconds_since_epoch; 10846109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10856109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1086360SN/A 10872680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1088360SN/A 10891458SN/A return 0; 1090360SN/A} 1091360SN/A 1092360SN/A 10931999SN/A/// Target utimes() handler. 10941999SN/Atemplate <class OS> 10951999SN/ASyscallReturn 10963114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10972680Sktlim@umich.edu ThreadContext *tc) 10981999SN/A{ 10991999SN/A std::string path; 11001999SN/A 11016701Sgblack@eecs.umich.edu int index = 0; 11026701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 11036701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11046701Sgblack@eecs.umich.edu return -EFAULT; 11056701Sgblack@eecs.umich.edu } 11061999SN/A 11076701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 11086701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 11092680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 11101999SN/A 11111999SN/A struct timeval hostTimeval[2]; 11121999SN/A for (int i = 0; i < 2; ++i) 11131999SN/A { 11142091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 11152091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 11161999SN/A } 11173669Sbinkertn@umich.edu 11183669Sbinkertn@umich.edu // Adjust path for current working directory 11193669Sbinkertn@umich.edu path = process->fullPath(path); 11203669Sbinkertn@umich.edu 11211999SN/A int result = utimes(path.c_str(), hostTimeval); 11221999SN/A 11231999SN/A if (result < 0) 11241999SN/A return -errno; 11251999SN/A 11261999SN/A return 0; 11271999SN/A} 1128378SN/A/// Target getrusage() function. 1129360SN/Atemplate <class OS> 11301450SN/ASyscallReturn 11313114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11322680Sktlim@umich.edu ThreadContext *tc) 1133360SN/A{ 11346701Sgblack@eecs.umich.edu int index = 0; 11356701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 11366701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1137360SN/A 11383670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 11393670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1140360SN/A rup->ru_stime.tv_sec = 0; 1141360SN/A rup->ru_stime.tv_usec = 0; 1142360SN/A rup->ru_maxrss = 0; 1143360SN/A rup->ru_ixrss = 0; 1144360SN/A rup->ru_idrss = 0; 1145360SN/A rup->ru_isrss = 0; 1146360SN/A rup->ru_minflt = 0; 1147360SN/A rup->ru_majflt = 0; 1148360SN/A rup->ru_nswap = 0; 1149360SN/A rup->ru_inblock = 0; 1150360SN/A rup->ru_oublock = 0; 1151360SN/A rup->ru_msgsnd = 0; 1152360SN/A rup->ru_msgrcv = 0; 1153360SN/A rup->ru_nsignals = 0; 1154360SN/A rup->ru_nvcsw = 0; 1155360SN/A rup->ru_nivcsw = 0; 1156360SN/A 11573670Sbinkertn@umich.edu switch (who) { 11583670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11593670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11603670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11613670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11623670Sbinkertn@umich.edu break; 11633670Sbinkertn@umich.edu 11643670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11653670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11663670Sbinkertn@umich.edu break; 11673670Sbinkertn@umich.edu 11683670Sbinkertn@umich.edu default: 11693670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11703670Sbinkertn@umich.edu // plow ahead 11713670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11723670Sbinkertn@umich.edu who); 11733670Sbinkertn@umich.edu } 11743670Sbinkertn@umich.edu 11752680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1176360SN/A 11771458SN/A return 0; 1178360SN/A} 1179360SN/A 11806683Stjones1@inf.ed.ac.uk/// Target times() function. 11816683Stjones1@inf.ed.ac.uktemplate <class OS> 11826683Stjones1@inf.ed.ac.ukSyscallReturn 11836683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11846683Stjones1@inf.ed.ac.uk ThreadContext *tc) 11856683Stjones1@inf.ed.ac.uk{ 11866701Sgblack@eecs.umich.edu int index = 0; 11876701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 11886683Stjones1@inf.ed.ac.uk 11896683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 11906683Stjones1@inf.ed.ac.uk int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s; 11916683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 11926683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 11936683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 11946683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 11956683Stjones1@inf.ed.ac.uk 11966683Stjones1@inf.ed.ac.uk // Convert to host endianness 11976683Stjones1@inf.ed.ac.uk bufp->tms_utime = htog(bufp->tms_utime); 11986683Stjones1@inf.ed.ac.uk 11996683Stjones1@inf.ed.ac.uk // Write back 12006683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemPort()); 12016683Stjones1@inf.ed.ac.uk 12026683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 12036683Stjones1@inf.ed.ac.uk return clocks; 12046683Stjones1@inf.ed.ac.uk} 12052553SN/A 12066684Stjones1@inf.ed.ac.uk/// Target time() function. 12076684Stjones1@inf.ed.ac.uktemplate <class OS> 12086684Stjones1@inf.ed.ac.ukSyscallReturn 12096684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 12106684Stjones1@inf.ed.ac.uk ThreadContext *tc) 12116684Stjones1@inf.ed.ac.uk{ 12126684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 12136684Stjones1@inf.ed.ac.uk getElapsedTime(sec, usec); 12146684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 12156684Stjones1@inf.ed.ac.uk 12166701Sgblack@eecs.umich.edu int index = 0; 12176701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 12186684Stjones1@inf.ed.ac.uk if(taddr != 0) { 12196684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 12206684Stjones1@inf.ed.ac.uk t = htog(t); 12216684Stjones1@inf.ed.ac.uk TranslatingPort *p = tc->getMemPort(); 12226684Stjones1@inf.ed.ac.uk p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 12236684Stjones1@inf.ed.ac.uk } 12246684Stjones1@inf.ed.ac.uk return sec; 12256684Stjones1@inf.ed.ac.uk} 12262553SN/A 12272553SN/A 12281354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1229