syscall_emul.hh revision 8232
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> 488229Snate@binkert.org#include <sys/time.h> 498229Snate@binkert.org#include <sys/uio.h> 503113Sgblack@eecs.umich.edu#include <fcntl.h> 517075Snate@binkert.org 528229Snate@binkert.org#include <cerrno> 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" 638232Snate@binkert.org#include "debug/SyscallVerbose.hh" 648229Snate@binkert.org#include "mem/page_table.hh" 652474SN/A#include "mem/translating_port.hh" 667678Sgblack@eecs.umich.edu#include "sim/byteswap.hh" 678229Snate@binkert.org#include "sim/process.hh" 686640Svince@csl.cornell.edu#include "sim/system.hh" 69360SN/A 70360SN/A/// 71360SN/A/// System call descriptor. 72360SN/A/// 73360SN/Aclass SyscallDesc { 74360SN/A 75360SN/A public: 76360SN/A 77378SN/A /// Typedef for target syscall handler functions. 781450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 793114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 80360SN/A 815543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 825543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 835543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 84360SN/A 85360SN/A /// Flag values for controlling syscall behavior. 86360SN/A enum Flags { 87360SN/A /// Don't set return regs according to funcPtr return value. 88360SN/A /// Used for syscalls with non-standard return conventions 892680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 90360SN/A /// sigreturn). 91360SN/A SuppressReturnValue = 1 92360SN/A }; 93360SN/A 94360SN/A /// Constructor. 95360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 96360SN/A : name(_name), funcPtr(_funcPtr), flags(_flags) 97360SN/A { 98360SN/A } 99360SN/A 100360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 1013114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 102360SN/A}; 103360SN/A 104360SN/A 105360SN/Aclass BaseBufferArg { 106360SN/A 107360SN/A public: 108360SN/A 109360SN/A BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size) 110360SN/A { 111360SN/A bufPtr = new uint8_t[size]; 112360SN/A // clear out buffer: in case we only partially populate this, 113360SN/A // and then do a copyOut(), we want to make sure we don't 114360SN/A // introduce any random junk into the simulated address space 115360SN/A memset(bufPtr, 0, size); 116360SN/A } 117360SN/A 118360SN/A virtual ~BaseBufferArg() { delete [] bufPtr; } 119360SN/A 120360SN/A // 121360SN/A // copy data into simulator space (read from target memory) 122360SN/A // 1232400SN/A virtual bool copyIn(TranslatingPort *memport) 124360SN/A { 1252461SN/A memport->readBlob(addr, bufPtr, size); 1265543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 127360SN/A } 128360SN/A 129360SN/A // 130360SN/A // copy data out of simulator space (write to target memory) 131360SN/A // 1322400SN/A virtual bool copyOut(TranslatingPort *memport) 133360SN/A { 1342461SN/A memport->writeBlob(addr, bufPtr, size); 1355543Ssaidi@eecs.umich.edu return true; // no EFAULT detection for now 136360SN/A } 137360SN/A 138360SN/A protected: 139360SN/A Addr addr; 140360SN/A int size; 141360SN/A uint8_t *bufPtr; 142360SN/A}; 143360SN/A 144360SN/A 145360SN/Aclass BufferArg : public BaseBufferArg 146360SN/A{ 147360SN/A public: 148360SN/A BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { } 1495543Ssaidi@eecs.umich.edu void *bufferPtr() { return bufPtr; } 150360SN/A}; 151360SN/A 152360SN/Atemplate <class T> 153360SN/Aclass TypedBufferArg : public BaseBufferArg 154360SN/A{ 155360SN/A public: 156360SN/A // user can optionally specify a specific number of bytes to 157360SN/A // allocate to deal with those structs that have variable-size 158360SN/A // arrays at the end 159360SN/A TypedBufferArg(Addr _addr, int _size = sizeof(T)) 160360SN/A : BaseBufferArg(_addr, _size) 161360SN/A { } 162360SN/A 163360SN/A // type case 164360SN/A operator T*() { return (T *)bufPtr; } 165360SN/A 166360SN/A // dereference operators 1675543Ssaidi@eecs.umich.edu T &operator*() { return *((T *)bufPtr); } 1685543Ssaidi@eecs.umich.edu T* operator->() { return (T *)bufPtr; } 169502SN/A T &operator[](int i) { return ((T *)bufPtr)[i]; } 170360SN/A}; 171360SN/A 172360SN/A////////////////////////////////////////////////////////////////////// 173360SN/A// 174360SN/A// The following emulation functions are generic enough that they 175360SN/A// don't need to be recompiled for different emulated OS's. They are 176360SN/A// defined in sim/syscall_emul.cc. 177360SN/A// 178360SN/A////////////////////////////////////////////////////////////////////// 179360SN/A 180360SN/A 181378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1821706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1833114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 184378SN/A 185378SN/A/// Handler for unimplemented syscalls that we never intend to 186378SN/A/// implement (signal handling, etc.) and should not affect the correct 187378SN/A/// behavior of the program. Print a warning only if the appropriate 188378SN/A/// trace flag is enabled. Return success to the target program. 1891706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1903114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 1918149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num, 1928149SChris.Emmons@ARM.com LiveProcess *p, ThreadContext *tc); 193360SN/A 1946109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1951706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1963114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 197378SN/A 1986109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1996109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 2006109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 2016109Ssanchezd@stanford.edu 202378SN/A/// Target getpagesize() handler. 2031706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 2043114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 205378SN/A 2065748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 2075748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 2085748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 209378SN/A 210378SN/A/// Target close() handler. 2111706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 2123114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 213378SN/A 214378SN/A/// Target read() handler. 2151706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 2163114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 217378SN/A 218378SN/A/// Target write() handler. 2191706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 2203114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 221378SN/A 222378SN/A/// Target lseek() handler. 2231706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 2243114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 225378SN/A 2264118Sgblack@eecs.umich.edu/// Target _llseek() handler. 2274118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 2284118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2294118Sgblack@eecs.umich.edu 230378SN/A/// Target munmap() handler. 2311706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 2323114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 233378SN/A 234378SN/A/// Target gethostname() handler. 2351706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2363114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 237360SN/A 2385513SMichael.Adler@intel.com/// Target getcwd() handler. 2395513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2405513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2415513SMichael.Adler@intel.com 2425513SMichael.Adler@intel.com/// Target unlink() handler. 2435513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2445513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2455513SMichael.Adler@intel.com 246511SN/A/// Target unlink() handler. 2471706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2483114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 249511SN/A 2505513SMichael.Adler@intel.com/// Target mkdir() handler. 2515513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2525513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2535513SMichael.Adler@intel.com 254511SN/A/// Target rename() handler. 2551706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2563114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2571706SN/A 2581706SN/A 2591706SN/A/// Target truncate() handler. 2601706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2613114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2621706SN/A 2631706SN/A 2641706SN/A/// Target ftruncate() handler. 2651706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2663114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2671706SN/A 268511SN/A 2696703Svince@csl.cornell.edu/// Target truncate64() handler. 2706703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2716703Svince@csl.cornell.edu LiveProcess *p, ThreadContext *tc); 2726703Svince@csl.cornell.edu 2736685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2746685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2756685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2766685Stjones1@inf.ed.ac.uk 2776685Stjones1@inf.ed.ac.uk 2785513SMichael.Adler@intel.com/// Target umask() handler. 2795513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2805513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2815513SMichael.Adler@intel.com 2825513SMichael.Adler@intel.com 2831999SN/A/// Target chown() handler. 2841999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2853114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2861999SN/A 2871999SN/A 2881999SN/A/// Target fchown() handler. 2891999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2903114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2911999SN/A 2923079Sstever@eecs.umich.edu/// Target dup() handler. 2933079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2943114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2953079Sstever@eecs.umich.edu 2962093SN/A/// Target fnctl() handler. 2972093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2983114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2992093SN/A 3002687Sksewell@umich.edu/// Target fcntl64() handler. 3012687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 3023114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3032687Sksewell@umich.edu 3042238SN/A/// Target setuid() handler. 3052238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 3063114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3072238SN/A 3082238SN/A/// Target getpid() handler. 3092238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3103114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3112238SN/A 3122238SN/A/// Target getuid() handler. 3132238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3143114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3152238SN/A 3162238SN/A/// Target getgid() handler. 3172238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3183114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3192238SN/A 3202238SN/A/// Target getppid() handler. 3212238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3223114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3232238SN/A 3242238SN/A/// Target geteuid() handler. 3252238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3263114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3272238SN/A 3282238SN/A/// Target getegid() handler. 3292238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3303114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3312238SN/A 3326109Ssanchezd@stanford.edu/// Target clone() handler. 3336109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3346109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3352238SN/A 3362238SN/A 3372238SN/A/// Pseudo Funcs - These functions use a different return convension, 3382238SN/A/// returning a second value in a register other than the normal return register 3392238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3403114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 3412238SN/A 3422238SN/A/// Target getpidPseudo() handler. 3432238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3443114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3452238SN/A 3462238SN/A/// Target getuidPseudo() handler. 3472238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3483114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3492238SN/A 3502238SN/A/// Target getgidPseudo() handler. 3512238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3523114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3532238SN/A 3542238SN/A 3551354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3561354SN/Aconst int one_million = 1000000; 3571354SN/A 3581354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3591354SN/A/// by my reckoning. We want to keep this a constant (not use the 3601354SN/A/// real-world time) to keep simulations repeatable. 3611354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3621354SN/A 3631354SN/A/// Helper function to convert current elapsed time to seconds and 3641354SN/A/// microseconds. 3651354SN/Atemplate <class T1, class T2> 3661354SN/Avoid 3671354SN/AgetElapsedTime(T1 &sec, T2 &usec) 3681354SN/A{ 3697823Ssteve.reinhardt@amd.com int elapsed_usecs = curTick() / SimClock::Int::us; 3701354SN/A sec = elapsed_usecs / one_million; 3711354SN/A usec = elapsed_usecs % one_million; 3721354SN/A} 3731354SN/A 374360SN/A////////////////////////////////////////////////////////////////////// 375360SN/A// 376360SN/A// The following emulation functions are generic, but need to be 377360SN/A// templated to account for differences in types, constants, etc. 378360SN/A// 379360SN/A////////////////////////////////////////////////////////////////////// 380360SN/A 3813113Sgblack@eecs.umich.edu#if NO_STAT64 3823113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3833113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 3843113Sgblack@eecs.umich.edu#else 3853113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 3863113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 3873113Sgblack@eecs.umich.edu#endif 3883113Sgblack@eecs.umich.edu 3893113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 3903113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 3913113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 3923113Sgblack@eecs.umich.edu 3933113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 3943113Sgblack@eecs.umich.edustatic void 3953113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 3963113Sgblack@eecs.umich.edu{ 3974189Sgblack@eecs.umich.edu using namespace TheISA; 3984189Sgblack@eecs.umich.edu 3993113Sgblack@eecs.umich.edu if (fakeTTY) 4003113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 4013113Sgblack@eecs.umich.edu else 4023113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 4033113Sgblack@eecs.umich.edu tgt->st_dev = htog(tgt->st_dev); 4043113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 4053113Sgblack@eecs.umich.edu tgt->st_ino = htog(tgt->st_ino); 4063277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4075515SMichael.Adler@intel.com if (fakeTTY) { 4085515SMichael.Adler@intel.com // Claim to be a character device 4095515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4105515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4115515SMichael.Adler@intel.com } 4123277Sgblack@eecs.umich.edu tgt->st_mode = htog(tgt->st_mode); 4133277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4143277Sgblack@eecs.umich.edu tgt->st_nlink = htog(tgt->st_nlink); 4153277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4163277Sgblack@eecs.umich.edu tgt->st_uid = htog(tgt->st_uid); 4173277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4183277Sgblack@eecs.umich.edu tgt->st_gid = htog(tgt->st_gid); 4193113Sgblack@eecs.umich.edu if (fakeTTY) 4203113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4213113Sgblack@eecs.umich.edu else 4223113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4233113Sgblack@eecs.umich.edu tgt->st_rdev = htog(tgt->st_rdev); 4243113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4253113Sgblack@eecs.umich.edu tgt->st_size = htog(tgt->st_size); 4263114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4273113Sgblack@eecs.umich.edu tgt->st_atimeX = htog(tgt->st_atimeX); 4283114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4293113Sgblack@eecs.umich.edu tgt->st_mtimeX = htog(tgt->st_mtimeX); 4303114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4313113Sgblack@eecs.umich.edu tgt->st_ctimeX = htog(tgt->st_ctimeX); 4324061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 4334061Sgblack@eecs.umich.edu // consistently across different hosts. 4344061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4353113Sgblack@eecs.umich.edu tgt->st_blksize = htog(tgt->st_blksize); 4363113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4373113Sgblack@eecs.umich.edu tgt->st_blocks = htog(tgt->st_blocks); 4383113Sgblack@eecs.umich.edu} 4393113Sgblack@eecs.umich.edu 4403113Sgblack@eecs.umich.edu// Same for stat64 4413113Sgblack@eecs.umich.edu 4423113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4433113Sgblack@eecs.umich.edustatic void 4443113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4453113Sgblack@eecs.umich.edu{ 4464189Sgblack@eecs.umich.edu using namespace TheISA; 4474189Sgblack@eecs.umich.edu 4483113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4493113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4503113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4513113Sgblack@eecs.umich.edu tgt->st_atime_nsec = htog(tgt->st_atime_nsec); 4523113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4533113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec); 4543113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4553113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec); 4563113Sgblack@eecs.umich.edu#else 4573113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4583113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4593113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4603113Sgblack@eecs.umich.edu#endif 4613113Sgblack@eecs.umich.edu} 4623113Sgblack@eecs.umich.edu 4633113Sgblack@eecs.umich.edu//Here are a couple convenience functions 4643113Sgblack@eecs.umich.edutemplate<class OS> 4653113Sgblack@eecs.umich.edustatic void 4663113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr, 4673113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 4683113Sgblack@eecs.umich.edu{ 4693113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 4703113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4713113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 4723113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4733113Sgblack@eecs.umich.edu} 4743113Sgblack@eecs.umich.edu 4753113Sgblack@eecs.umich.edutemplate<class OS> 4763113Sgblack@eecs.umich.edustatic void 4773113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr, 4783113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 4793113Sgblack@eecs.umich.edu{ 4803113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 4813113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 4826686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 4833113Sgblack@eecs.umich.edu tgt.copyOut(mem); 4843113Sgblack@eecs.umich.edu} 4853113Sgblack@eecs.umich.edu 486378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 487378SN/A/// only to find out if their stdout is a tty, to determine whether to 488378SN/A/// do line or block buffering. 489360SN/Atemplate <class OS> 4901450SN/ASyscallReturn 4913114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 4922680Sktlim@umich.edu ThreadContext *tc) 493360SN/A{ 4946701Sgblack@eecs.umich.edu int index = 0; 4956701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 4966701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 497360SN/A 4981969SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req); 499360SN/A 500360SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 501360SN/A // doesn't map to any simulator fd: not a valid target fd 5021458SN/A return -EBADF; 503360SN/A } 504360SN/A 505360SN/A switch (req) { 5064131Sbinkertn@umich.edu case OS::TIOCISATTY_: 5074131Sbinkertn@umich.edu case OS::TIOCGETP_: 5084131Sbinkertn@umich.edu case OS::TIOCSETP_: 5094131Sbinkertn@umich.edu case OS::TIOCSETN_: 5104131Sbinkertn@umich.edu case OS::TIOCSETC_: 5114131Sbinkertn@umich.edu case OS::TIOCGETC_: 5124131Sbinkertn@umich.edu case OS::TIOCGETS_: 5134131Sbinkertn@umich.edu case OS::TIOCGETA_: 5146689Stjones1@inf.ed.ac.uk case OS::TCSETAW_: 5151458SN/A return -ENOTTY; 516360SN/A 517360SN/A default: 5187720Sgblack@eecs.umich.edu fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n", 5197720Sgblack@eecs.umich.edu fd, req, tc->pcState()); 520360SN/A } 521360SN/A} 522360SN/A 523378SN/A/// Target open() handler. 524360SN/Atemplate <class OS> 5251450SN/ASyscallReturn 5263114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5272680Sktlim@umich.edu ThreadContext *tc) 528360SN/A{ 529360SN/A std::string path; 530360SN/A 5316701Sgblack@eecs.umich.edu int index = 0; 5326701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 5336701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 5341458SN/A return -EFAULT; 535360SN/A 536360SN/A if (path == "/dev/sysdev0") { 537360SN/A // This is a memory-mapped high-resolution timer device on Alpha. 538360SN/A // We don't support it, so just punt. 5391706SN/A warn("Ignoring open(%s, ...)\n", path); 5401458SN/A return -ENOENT; 541360SN/A } 542360SN/A 5436701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 5446701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 545360SN/A int hostFlags = 0; 546360SN/A 547360SN/A // translate open flags 548360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 549360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 550360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 551360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 552360SN/A } 553360SN/A } 554360SN/A 555360SN/A // any target flags left? 556360SN/A if (tgtFlags != 0) 5571706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 558360SN/A 559360SN/A#ifdef __CYGWIN32__ 560360SN/A hostFlags |= O_BINARY; 561360SN/A#endif 562360SN/A 5633669Sbinkertn@umich.edu // Adjust path for current working directory 5643669Sbinkertn@umich.edu path = process->fullPath(path); 5653669Sbinkertn@umich.edu 5661706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 5671706SN/A 5685795Ssaidi@eecs.umich.edu int fd; 5695795Ssaidi@eecs.umich.edu if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") || 5705795Ssaidi@eecs.umich.edu !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) { 5715795Ssaidi@eecs.umich.edu // It's a proc/sys entery and requires special handling 5725795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 5735795Ssaidi@eecs.umich.edu return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5745795Ssaidi@eecs.umich.edu } else { 5755795Ssaidi@eecs.umich.edu // open the file 5765795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 5775795Ssaidi@eecs.umich.edu return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false); 5785795Ssaidi@eecs.umich.edu } 579360SN/A 580360SN/A} 581360SN/A 5826640Svince@csl.cornell.edu/// Target sysinfo() handler. 5836640Svince@csl.cornell.edutemplate <class OS> 5846640Svince@csl.cornell.eduSyscallReturn 5856640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5866640Svince@csl.cornell.edu ThreadContext *tc) 5876640Svince@csl.cornell.edu{ 5886640Svince@csl.cornell.edu 5896701Sgblack@eecs.umich.edu int index = 0; 5906701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 5916701Sgblack@eecs.umich.edu sysinfo(process->getSyscallArg(tc, index)); 5926640Svince@csl.cornell.edu 5936701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 5946701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 5956640Svince@csl.cornell.edu 5966701Sgblack@eecs.umich.edu sysinfo.copyOut(tc->getMemPort()); 5976640Svince@csl.cornell.edu 5986701Sgblack@eecs.umich.edu return 0; 5996640Svince@csl.cornell.edu} 600360SN/A 6011999SN/A/// Target chmod() handler. 6021999SN/Atemplate <class OS> 6031999SN/ASyscallReturn 6043114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6052680Sktlim@umich.edu ThreadContext *tc) 6061999SN/A{ 6071999SN/A std::string path; 6081999SN/A 6096701Sgblack@eecs.umich.edu int index = 0; 6106701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 6116701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 6121999SN/A return -EFAULT; 6136701Sgblack@eecs.umich.edu } 6141999SN/A 6156701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6161999SN/A mode_t hostMode = 0; 6171999SN/A 6181999SN/A // XXX translate mode flags via OS::something??? 6191999SN/A hostMode = mode; 6201999SN/A 6213669Sbinkertn@umich.edu // Adjust path for current working directory 6223669Sbinkertn@umich.edu path = process->fullPath(path); 6233669Sbinkertn@umich.edu 6241999SN/A // do the chmod 6251999SN/A int result = chmod(path.c_str(), hostMode); 6261999SN/A if (result < 0) 6272218SN/A return -errno; 6281999SN/A 6291999SN/A return 0; 6301999SN/A} 6311999SN/A 6321999SN/A 6331999SN/A/// Target fchmod() handler. 6341999SN/Atemplate <class OS> 6351999SN/ASyscallReturn 6363114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 6372680Sktlim@umich.edu ThreadContext *tc) 6381999SN/A{ 6396701Sgblack@eecs.umich.edu int index = 0; 6406701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 6411999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 6421999SN/A // doesn't map to any simulator fd: not a valid target fd 6431999SN/A return -EBADF; 6441999SN/A } 6451999SN/A 6466701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 6471999SN/A mode_t hostMode = 0; 6481999SN/A 6491999SN/A // XXX translate mode flags via OS::someting??? 6501999SN/A hostMode = mode; 6511999SN/A 6521999SN/A // do the fchmod 6531999SN/A int result = fchmod(process->sim_fd(fd), hostMode); 6541999SN/A if (result < 0) 6552218SN/A return -errno; 6561999SN/A 6571999SN/A return 0; 6581999SN/A} 6591999SN/A 6605877Shsul@eecs.umich.edu/// Target mremap() handler. 6615877Shsul@eecs.umich.edutemplate <class OS> 6625877Shsul@eecs.umich.eduSyscallReturn 6635877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 6645877Shsul@eecs.umich.edu{ 6656701Sgblack@eecs.umich.edu int index = 0; 6666701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 6676701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 6686701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 6696701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 6705877Shsul@eecs.umich.edu 6715877Shsul@eecs.umich.edu if ((start % TheISA::VMPageSize != 0) || 6725877Shsul@eecs.umich.edu (new_length % TheISA::VMPageSize != 0)) { 6735877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 6745877Shsul@eecs.umich.edu return -EINVAL; 6755877Shsul@eecs.umich.edu } 6765877Shsul@eecs.umich.edu 6775877Shsul@eecs.umich.edu if (new_length > old_length) { 6785877Shsul@eecs.umich.edu if ((start + old_length) == process->mmap_end) { 6795877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 6805877Shsul@eecs.umich.edu process->pTable->allocate(process->mmap_end, diff); 6815877Shsul@eecs.umich.edu process->mmap_end += diff; 6825877Shsul@eecs.umich.edu return start; 6835877Shsul@eecs.umich.edu } else { 6845877Shsul@eecs.umich.edu // sys/mman.h defined MREMAP_MAYMOVE 6855877Shsul@eecs.umich.edu if (!(flags & 1)) { 6865877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 6875877Shsul@eecs.umich.edu return -ENOMEM; 6885877Shsul@eecs.umich.edu } else { 6895877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, process->mmap_end); 6905877Shsul@eecs.umich.edu warn("mremapping to totally new vaddr %08p-%08p, adding %d\n", 6915877Shsul@eecs.umich.edu process->mmap_end, process->mmap_end + new_length, new_length); 6925877Shsul@eecs.umich.edu start = process->mmap_end; 6935877Shsul@eecs.umich.edu // add on the remaining unallocated pages 6945877Shsul@eecs.umich.edu process->pTable->allocate(start + old_length, new_length - old_length); 6955877Shsul@eecs.umich.edu process->mmap_end += new_length; 6965877Shsul@eecs.umich.edu warn("returning %08p as start\n", start); 6975877Shsul@eecs.umich.edu return start; 6985877Shsul@eecs.umich.edu } 6995877Shsul@eecs.umich.edu } 7005877Shsul@eecs.umich.edu } else { 7015877Shsul@eecs.umich.edu process->pTable->deallocate(start + new_length, old_length - 7025877Shsul@eecs.umich.edu new_length); 7035877Shsul@eecs.umich.edu return start; 7045877Shsul@eecs.umich.edu } 7055877Shsul@eecs.umich.edu} 7061999SN/A 707378SN/A/// Target stat() handler. 708360SN/Atemplate <class OS> 7091450SN/ASyscallReturn 7103114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7112680Sktlim@umich.edu ThreadContext *tc) 712360SN/A{ 713360SN/A std::string path; 714360SN/A 7156701Sgblack@eecs.umich.edu int index = 0; 7166701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7176701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7186701Sgblack@eecs.umich.edu return -EFAULT; 7196701Sgblack@eecs.umich.edu } 7206701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 721360SN/A 7223669Sbinkertn@umich.edu // Adjust path for current working directory 7233669Sbinkertn@umich.edu path = process->fullPath(path); 7243669Sbinkertn@umich.edu 725360SN/A struct stat hostBuf; 726360SN/A int result = stat(path.c_str(), &hostBuf); 727360SN/A 728360SN/A if (result < 0) 7292218SN/A return -errno; 730360SN/A 7316701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 732360SN/A 7331458SN/A return 0; 734360SN/A} 735360SN/A 736360SN/A 7375074Ssaidi@eecs.umich.edu/// Target stat64() handler. 7385074Ssaidi@eecs.umich.edutemplate <class OS> 7395074Ssaidi@eecs.umich.eduSyscallReturn 7405074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7415074Ssaidi@eecs.umich.edu ThreadContext *tc) 7425074Ssaidi@eecs.umich.edu{ 7435074Ssaidi@eecs.umich.edu std::string path; 7445074Ssaidi@eecs.umich.edu 7456701Sgblack@eecs.umich.edu int index = 0; 7466701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 7476701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 7485074Ssaidi@eecs.umich.edu return -EFAULT; 7496701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7505074Ssaidi@eecs.umich.edu 7515074Ssaidi@eecs.umich.edu // Adjust path for current working directory 7525074Ssaidi@eecs.umich.edu path = process->fullPath(path); 7535074Ssaidi@eecs.umich.edu 7545208Ssaidi@eecs.umich.edu#if NO_STAT64 7555208Ssaidi@eecs.umich.edu struct stat hostBuf; 7565208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 7575208Ssaidi@eecs.umich.edu#else 7585074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 7595074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 7605208Ssaidi@eecs.umich.edu#endif 7615074Ssaidi@eecs.umich.edu 7625074Ssaidi@eecs.umich.edu if (result < 0) 7635074Ssaidi@eecs.umich.edu return -errno; 7645074Ssaidi@eecs.umich.edu 7656701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 7665074Ssaidi@eecs.umich.edu 7675074Ssaidi@eecs.umich.edu return 0; 7685074Ssaidi@eecs.umich.edu} 7695074Ssaidi@eecs.umich.edu 7705074Ssaidi@eecs.umich.edu 7711999SN/A/// Target fstat64() handler. 7721999SN/Atemplate <class OS> 7731999SN/ASyscallReturn 7743114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 7752680Sktlim@umich.edu ThreadContext *tc) 7761999SN/A{ 7776701Sgblack@eecs.umich.edu int index = 0; 7786701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 7796701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 7801999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 7811999SN/A // doesn't map to any simulator fd: not a valid target fd 7821999SN/A return -EBADF; 7831999SN/A } 7841999SN/A 7852764Sstever@eecs.umich.edu#if NO_STAT64 7862064SN/A struct stat hostBuf; 7872064SN/A int result = fstat(process->sim_fd(fd), &hostBuf); 7882064SN/A#else 7892064SN/A struct stat64 hostBuf; 7901999SN/A int result = fstat64(process->sim_fd(fd), &hostBuf); 7912064SN/A#endif 7921999SN/A 7931999SN/A if (result < 0) 7942218SN/A return -errno; 7951999SN/A 7966701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 7971999SN/A 7981999SN/A return 0; 7991999SN/A} 8001999SN/A 8011999SN/A 802378SN/A/// Target lstat() handler. 803360SN/Atemplate <class OS> 8041450SN/ASyscallReturn 8053114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8062680Sktlim@umich.edu ThreadContext *tc) 807360SN/A{ 808360SN/A std::string path; 809360SN/A 8106701Sgblack@eecs.umich.edu int index = 0; 8116701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8126701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8136701Sgblack@eecs.umich.edu return -EFAULT; 8146701Sgblack@eecs.umich.edu } 8156701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 816360SN/A 8173669Sbinkertn@umich.edu // Adjust path for current working directory 8183669Sbinkertn@umich.edu path = process->fullPath(path); 8193669Sbinkertn@umich.edu 820360SN/A struct stat hostBuf; 821360SN/A int result = lstat(path.c_str(), &hostBuf); 822360SN/A 823360SN/A if (result < 0) 8241458SN/A return -errno; 825360SN/A 8266701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 827360SN/A 8281458SN/A return 0; 829360SN/A} 830360SN/A 8311999SN/A/// Target lstat64() handler. 8321999SN/Atemplate <class OS> 8331999SN/ASyscallReturn 8343114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 8352680Sktlim@umich.edu ThreadContext *tc) 8361999SN/A{ 8371999SN/A std::string path; 8381999SN/A 8396701Sgblack@eecs.umich.edu int index = 0; 8406701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 8416701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8426701Sgblack@eecs.umich.edu return -EFAULT; 8436701Sgblack@eecs.umich.edu } 8446701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 8451999SN/A 8463669Sbinkertn@umich.edu // Adjust path for current working directory 8473669Sbinkertn@umich.edu path = process->fullPath(path); 8483669Sbinkertn@umich.edu 8492764Sstever@eecs.umich.edu#if NO_STAT64 8502064SN/A struct stat hostBuf; 8512064SN/A int result = lstat(path.c_str(), &hostBuf); 8522064SN/A#else 8531999SN/A struct stat64 hostBuf; 8541999SN/A int result = lstat64(path.c_str(), &hostBuf); 8552064SN/A#endif 8561999SN/A 8571999SN/A if (result < 0) 8581999SN/A return -errno; 8591999SN/A 8606701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf); 8611999SN/A 8621999SN/A return 0; 8631999SN/A} 8641999SN/A 865378SN/A/// Target fstat() handler. 866360SN/Atemplate <class OS> 8671450SN/ASyscallReturn 8683114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8692680Sktlim@umich.edu ThreadContext *tc) 870360SN/A{ 8716701Sgblack@eecs.umich.edu int index = 0; 8726701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 8736701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 874360SN/A 8751969SN/A DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd); 876360SN/A 877360SN/A if (fd < 0) 8781458SN/A return -EBADF; 879360SN/A 880360SN/A struct stat hostBuf; 881360SN/A int result = fstat(fd, &hostBuf); 882360SN/A 883360SN/A if (result < 0) 8841458SN/A return -errno; 885360SN/A 8866701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1)); 8872021SN/A 8881458SN/A return 0; 889360SN/A} 890360SN/A 891360SN/A 8921706SN/A/// Target statfs() handler. 8931706SN/Atemplate <class OS> 8941706SN/ASyscallReturn 8953114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8962680Sktlim@umich.edu ThreadContext *tc) 8971706SN/A{ 8981706SN/A std::string path; 8991706SN/A 9006701Sgblack@eecs.umich.edu int index = 0; 9016701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 9026701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9036701Sgblack@eecs.umich.edu return -EFAULT; 9046701Sgblack@eecs.umich.edu } 9056701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9061706SN/A 9073669Sbinkertn@umich.edu // Adjust path for current working directory 9083669Sbinkertn@umich.edu path = process->fullPath(path); 9093669Sbinkertn@umich.edu 9101706SN/A struct statfs hostBuf; 9111706SN/A int result = statfs(path.c_str(), &hostBuf); 9121706SN/A 9131706SN/A if (result < 0) 9142218SN/A return -errno; 9151706SN/A 9166701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9171706SN/A 9181706SN/A return 0; 9191706SN/A} 9201706SN/A 9211706SN/A 9221706SN/A/// Target fstatfs() handler. 9231706SN/Atemplate <class OS> 9241706SN/ASyscallReturn 9253114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9262680Sktlim@umich.edu ThreadContext *tc) 9271706SN/A{ 9286701Sgblack@eecs.umich.edu int index = 0; 9296701Sgblack@eecs.umich.edu int fd = process->sim_fd(process->getSyscallArg(tc, index)); 9306701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9311706SN/A 9321706SN/A if (fd < 0) 9331706SN/A return -EBADF; 9341706SN/A 9351706SN/A struct statfs hostBuf; 9361706SN/A int result = fstatfs(fd, &hostBuf); 9371706SN/A 9381706SN/A if (result < 0) 9392218SN/A return -errno; 9401706SN/A 9416701Sgblack@eecs.umich.edu OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf); 9421706SN/A 9431706SN/A return 0; 9441706SN/A} 9451706SN/A 9461706SN/A 9471999SN/A/// Target writev() handler. 9481999SN/Atemplate <class OS> 9491999SN/ASyscallReturn 9503114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9512680Sktlim@umich.edu ThreadContext *tc) 9521999SN/A{ 9536701Sgblack@eecs.umich.edu int index = 0; 9546701Sgblack@eecs.umich.edu int fd = process->getSyscallArg(tc, index); 9551999SN/A if (fd < 0 || process->sim_fd(fd) < 0) { 9561999SN/A // doesn't map to any simulator fd: not a valid target fd 9571999SN/A return -EBADF; 9581999SN/A } 9591999SN/A 9602680Sktlim@umich.edu TranslatingPort *p = tc->getMemPort(); 9616701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 9626701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 9631999SN/A struct iovec hiov[count]; 9646227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 9651999SN/A typename OS::tgt_iovec tiov; 9662461SN/A 9672461SN/A p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 9682461SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 9692091SN/A hiov[i].iov_len = gtoh(tiov.iov_len); 9701999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 9712461SN/A p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 9722461SN/A hiov[i].iov_len); 9731999SN/A } 9741999SN/A 9751999SN/A int result = writev(process->sim_fd(fd), hiov, count); 9761999SN/A 9776227Snate@binkert.org for (size_t i = 0; i < count; ++i) 9781999SN/A delete [] (char *)hiov[i].iov_base; 9791999SN/A 9801999SN/A if (result < 0) 9812218SN/A return -errno; 9821999SN/A 9831999SN/A return 0; 9841999SN/A} 9851999SN/A 9861999SN/A 987378SN/A/// Target mmap() handler. 988378SN/A/// 989378SN/A/// We don't really handle mmap(). If the target is mmaping an 990378SN/A/// anonymous region or /dev/zero, we can get away with doing basically 991378SN/A/// nothing (since memory is initialized to zero and the simulator 992378SN/A/// doesn't really check addresses anyway). Always print a warning, 993378SN/A/// since this could be seriously broken if we're not mapping 994378SN/A/// /dev/zero. 995360SN/A// 996378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the 997378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to 998378SN/A/// anything else. 999360SN/Atemplate <class OS> 10001450SN/ASyscallReturn 10013114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 1002360SN/A{ 10036701Sgblack@eecs.umich.edu int index = 0; 10046701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 10056701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 10066701Sgblack@eecs.umich.edu index++; // int prot = p->getSyscallArg(tc, index); 10076701Sgblack@eecs.umich.edu int flags = p->getSyscallArg(tc, index); 10086701Sgblack@eecs.umich.edu int fd = p->sim_fd(p->getSyscallArg(tc, index)); 10096701Sgblack@eecs.umich.edu // int offset = p->getSyscallArg(tc, index); 1010360SN/A 10115877Shsul@eecs.umich.edu 10122544SN/A if ((start % TheISA::VMPageSize) != 0 || 10132544SN/A (length % TheISA::VMPageSize) != 0) { 10142544SN/A warn("mmap failing: arguments not page-aligned: " 10152544SN/A "start 0x%x length 0x%x", 10162544SN/A start, length); 10172544SN/A return -EINVAL; 1018360SN/A } 1019360SN/A 10202544SN/A if (start != 0) { 10212544SN/A warn("mmap: ignoring suggested map address 0x%x, using 0x%x", 10222544SN/A start, p->mmap_end); 10232544SN/A } 10242544SN/A 10252544SN/A // pick next address from our "mmap region" 10266672Sgblack@eecs.umich.edu if (OS::mmapGrowsDown()) { 10276672Sgblack@eecs.umich.edu start = p->mmap_end - length; 10286672Sgblack@eecs.umich.edu p->mmap_end = start; 10296672Sgblack@eecs.umich.edu } else { 10306672Sgblack@eecs.umich.edu start = p->mmap_end; 10316672Sgblack@eecs.umich.edu p->mmap_end += length; 10326672Sgblack@eecs.umich.edu } 10332544SN/A p->pTable->allocate(start, length); 10342544SN/A 10352553SN/A if (!(flags & OS::TGT_MAP_ANONYMOUS)) { 10361969SN/A warn("allowing mmap of file @ fd %d. " 10376701Sgblack@eecs.umich.edu "This will break if not /dev/zero.", fd); 1038360SN/A } 1039360SN/A 10401458SN/A return start; 1041360SN/A} 1042360SN/A 1043378SN/A/// Target getrlimit() handler. 1044360SN/Atemplate <class OS> 10451450SN/ASyscallReturn 10463114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10472680Sktlim@umich.edu ThreadContext *tc) 1048360SN/A{ 10496701Sgblack@eecs.umich.edu int index = 0; 10506701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 10516701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1052360SN/A 1053360SN/A switch (resource) { 10542064SN/A case OS::TGT_RLIMIT_STACK: 10555877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 10562064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 10572091SN/A rlp->rlim_cur = htog(rlp->rlim_cur); 10582091SN/A rlp->rlim_max = htog(rlp->rlim_max); 10592064SN/A break; 1060360SN/A 10615877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 10625877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 10635877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 10645877Shsul@eecs.umich.edu rlp->rlim_cur = htog(rlp->rlim_cur); 10655877Shsul@eecs.umich.edu rlp->rlim_max = htog(rlp->rlim_max); 10665877Shsul@eecs.umich.edu break; 10675877Shsul@eecs.umich.edu 10682064SN/A default: 10692064SN/A std::cerr << "getrlimitFunc: unimplemented resource " << resource 10702064SN/A << std::endl; 10712064SN/A abort(); 10722064SN/A break; 1073360SN/A } 1074360SN/A 10752680Sktlim@umich.edu rlp.copyOut(tc->getMemPort()); 10761458SN/A return 0; 1077360SN/A} 1078360SN/A 1079378SN/A/// Target gettimeofday() handler. 1080360SN/Atemplate <class OS> 10811450SN/ASyscallReturn 10823114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10832680Sktlim@umich.edu ThreadContext *tc) 1084360SN/A{ 10856701Sgblack@eecs.umich.edu int index = 0; 10866701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1087360SN/A 1088360SN/A getElapsedTime(tp->tv_sec, tp->tv_usec); 1089360SN/A tp->tv_sec += seconds_since_epoch; 10906109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 10916109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1092360SN/A 10932680Sktlim@umich.edu tp.copyOut(tc->getMemPort()); 1094360SN/A 10951458SN/A return 0; 1096360SN/A} 1097360SN/A 1098360SN/A 10991999SN/A/// Target utimes() handler. 11001999SN/Atemplate <class OS> 11011999SN/ASyscallReturn 11023114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11032680Sktlim@umich.edu ThreadContext *tc) 11041999SN/A{ 11051999SN/A std::string path; 11061999SN/A 11076701Sgblack@eecs.umich.edu int index = 0; 11086701Sgblack@eecs.umich.edu if (!tc->getMemPort()->tryReadString(path, 11096701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11106701Sgblack@eecs.umich.edu return -EFAULT; 11116701Sgblack@eecs.umich.edu } 11121999SN/A 11136701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 11146701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 11152680Sktlim@umich.edu tp.copyIn(tc->getMemPort()); 11161999SN/A 11171999SN/A struct timeval hostTimeval[2]; 11181999SN/A for (int i = 0; i < 2; ++i) 11191999SN/A { 11202091SN/A hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec); 11212091SN/A hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec); 11221999SN/A } 11233669Sbinkertn@umich.edu 11243669Sbinkertn@umich.edu // Adjust path for current working directory 11253669Sbinkertn@umich.edu path = process->fullPath(path); 11263669Sbinkertn@umich.edu 11271999SN/A int result = utimes(path.c_str(), hostTimeval); 11281999SN/A 11291999SN/A if (result < 0) 11301999SN/A return -errno; 11311999SN/A 11321999SN/A return 0; 11331999SN/A} 1134378SN/A/// Target getrusage() function. 1135360SN/Atemplate <class OS> 11361450SN/ASyscallReturn 11373114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11382680Sktlim@umich.edu ThreadContext *tc) 1139360SN/A{ 11406701Sgblack@eecs.umich.edu int index = 0; 11416701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 11426701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1143360SN/A 11443670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 11453670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1146360SN/A rup->ru_stime.tv_sec = 0; 1147360SN/A rup->ru_stime.tv_usec = 0; 1148360SN/A rup->ru_maxrss = 0; 1149360SN/A rup->ru_ixrss = 0; 1150360SN/A rup->ru_idrss = 0; 1151360SN/A rup->ru_isrss = 0; 1152360SN/A rup->ru_minflt = 0; 1153360SN/A rup->ru_majflt = 0; 1154360SN/A rup->ru_nswap = 0; 1155360SN/A rup->ru_inblock = 0; 1156360SN/A rup->ru_oublock = 0; 1157360SN/A rup->ru_msgsnd = 0; 1158360SN/A rup->ru_msgrcv = 0; 1159360SN/A rup->ru_nsignals = 0; 1160360SN/A rup->ru_nvcsw = 0; 1161360SN/A rup->ru_nivcsw = 0; 1162360SN/A 11633670Sbinkertn@umich.edu switch (who) { 11643670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 11653670Sbinkertn@umich.edu getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 11663670Sbinkertn@umich.edu rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec); 11673670Sbinkertn@umich.edu rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec); 11683670Sbinkertn@umich.edu break; 11693670Sbinkertn@umich.edu 11703670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 11713670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 11723670Sbinkertn@umich.edu break; 11733670Sbinkertn@umich.edu 11743670Sbinkertn@umich.edu default: 11753670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 11763670Sbinkertn@umich.edu // plow ahead 11773670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 11783670Sbinkertn@umich.edu who); 11793670Sbinkertn@umich.edu } 11803670Sbinkertn@umich.edu 11812680Sktlim@umich.edu rup.copyOut(tc->getMemPort()); 1182360SN/A 11831458SN/A return 0; 1184360SN/A} 1185360SN/A 11866683Stjones1@inf.ed.ac.uk/// Target times() function. 11876683Stjones1@inf.ed.ac.uktemplate <class OS> 11886683Stjones1@inf.ed.ac.ukSyscallReturn 11896683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11906683Stjones1@inf.ed.ac.uk ThreadContext *tc) 11916683Stjones1@inf.ed.ac.uk{ 11926701Sgblack@eecs.umich.edu int index = 0; 11936701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 11946683Stjones1@inf.ed.ac.uk 11956683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 11967823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 11976683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 11986683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 11996683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 12006683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 12016683Stjones1@inf.ed.ac.uk 12026683Stjones1@inf.ed.ac.uk // Convert to host endianness 12036683Stjones1@inf.ed.ac.uk bufp->tms_utime = htog(bufp->tms_utime); 12046683Stjones1@inf.ed.ac.uk 12056683Stjones1@inf.ed.ac.uk // Write back 12066683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemPort()); 12076683Stjones1@inf.ed.ac.uk 12086683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 12096683Stjones1@inf.ed.ac.uk return clocks; 12106683Stjones1@inf.ed.ac.uk} 12112553SN/A 12126684Stjones1@inf.ed.ac.uk/// Target time() function. 12136684Stjones1@inf.ed.ac.uktemplate <class OS> 12146684Stjones1@inf.ed.ac.ukSyscallReturn 12156684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 12166684Stjones1@inf.ed.ac.uk ThreadContext *tc) 12176684Stjones1@inf.ed.ac.uk{ 12186684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 12196684Stjones1@inf.ed.ac.uk getElapsedTime(sec, usec); 12206684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 12216684Stjones1@inf.ed.ac.uk 12226701Sgblack@eecs.umich.edu int index = 0; 12236701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 12246684Stjones1@inf.ed.ac.uk if(taddr != 0) { 12256684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 12266684Stjones1@inf.ed.ac.uk t = htog(t); 12276684Stjones1@inf.ed.ac.uk TranslatingPort *p = tc->getMemPort(); 12286684Stjones1@inf.ed.ac.uk p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 12296684Stjones1@inf.ed.ac.uk } 12306684Stjones1@inf.ed.ac.uk return sec; 12316684Stjones1@inf.ed.ac.uk} 12322553SN/A 12332553SN/A 12341354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1235