syscall_emul.hh revision 6658
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
2635513SMichael.Adler@intel.com/// Target umask() handler.
2645513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2655513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2665513SMichael.Adler@intel.com
2675513SMichael.Adler@intel.com
2681999SN/A/// Target chown() handler.
2691999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2703114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2711999SN/A
2721999SN/A
2731999SN/A/// Target fchown() handler.
2741999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2753114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2761999SN/A
2773079Sstever@eecs.umich.edu/// Target dup() handler.
2783079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2793114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2803079Sstever@eecs.umich.edu
2812093SN/A/// Target fnctl() handler.
2822093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2833114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2842093SN/A
2852687Sksewell@umich.edu/// Target fcntl64() handler.
2862687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2873114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2882687Sksewell@umich.edu
2892238SN/A/// Target setuid() handler.
2902238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2913114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2922238SN/A
2932238SN/A/// Target getpid() handler.
2942238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2953114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2962238SN/A
2972238SN/A/// Target getuid() handler.
2982238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2993114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3002238SN/A
3012238SN/A/// Target getgid() handler.
3022238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3033114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3042238SN/A
3052238SN/A/// Target getppid() handler.
3062238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3073114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3082238SN/A
3092238SN/A/// Target geteuid() handler.
3102238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3113114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3122238SN/A
3132238SN/A/// Target getegid() handler.
3142238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3153114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3162238SN/A
3176109Ssanchezd@stanford.edu/// Target clone() handler.
3186109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3196109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3202238SN/A
3212238SN/A
3222238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3232238SN/A/// returning a second value in a register other than the normal return register
3242238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3253114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3262238SN/A
3272238SN/A/// Target getpidPseudo() handler.
3282238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3293114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3302238SN/A
3312238SN/A/// Target getuidPseudo() handler.
3322238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3333114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3342238SN/A
3352238SN/A/// Target getgidPseudo() handler.
3362238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3373114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3382238SN/A
3392238SN/A
3401354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3411354SN/Aconst int one_million = 1000000;
3421354SN/A
3431354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3441354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3451354SN/A/// real-world time) to keep simulations repeatable.
3461354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3471354SN/A
3481354SN/A/// Helper function to convert current elapsed time to seconds and
3491354SN/A/// microseconds.
3501354SN/Atemplate <class T1, class T2>
3511354SN/Avoid
3521354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3531354SN/A{
3541609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3551354SN/A    sec = elapsed_usecs / one_million;
3561354SN/A    usec = elapsed_usecs % one_million;
3571354SN/A}
3581354SN/A
359360SN/A//////////////////////////////////////////////////////////////////////
360360SN/A//
361360SN/A// The following emulation functions are generic, but need to be
362360SN/A// templated to account for differences in types, constants, etc.
363360SN/A//
364360SN/A//////////////////////////////////////////////////////////////////////
365360SN/A
3663113Sgblack@eecs.umich.edu#if NO_STAT64
3673113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3683113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3693113Sgblack@eecs.umich.edu#else
3703113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3713113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3723113Sgblack@eecs.umich.edu#endif
3733113Sgblack@eecs.umich.edu
3743113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3753113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3763113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3773113Sgblack@eecs.umich.edu
3783113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3793113Sgblack@eecs.umich.edustatic void
3803113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3813113Sgblack@eecs.umich.edu{
3824189Sgblack@eecs.umich.edu    using namespace TheISA;
3834189Sgblack@eecs.umich.edu
3843113Sgblack@eecs.umich.edu    if (fakeTTY)
3853113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3863113Sgblack@eecs.umich.edu    else
3873113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3883113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3893113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3903113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3913277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
3925515SMichael.Adler@intel.com    if (fakeTTY) {
3935515SMichael.Adler@intel.com        // Claim to be a character device
3945515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
3955515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
3965515SMichael.Adler@intel.com    }
3973277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
3983277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
3993277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
4003277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4013277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
4023277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4033277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
4043113Sgblack@eecs.umich.edu    if (fakeTTY)
4053113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4063113Sgblack@eecs.umich.edu    else
4073113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4083113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
4093113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4103113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
4113114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4123113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
4133114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4143113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
4153114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4163113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
4174061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4184061Sgblack@eecs.umich.edu    // consistently across different hosts.
4194061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4203113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
4213113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4223113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
4233113Sgblack@eecs.umich.edu}
4243113Sgblack@eecs.umich.edu
4253113Sgblack@eecs.umich.edu// Same for stat64
4263113Sgblack@eecs.umich.edu
4273113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4283113Sgblack@eecs.umich.edustatic void
4293113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4303113Sgblack@eecs.umich.edu{
4314189Sgblack@eecs.umich.edu    using namespace TheISA;
4324189Sgblack@eecs.umich.edu
4333113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4343113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4353113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4363113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4373113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4383113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4393113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4403113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4413113Sgblack@eecs.umich.edu#else
4423113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4433113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4443113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4453113Sgblack@eecs.umich.edu#endif
4463113Sgblack@eecs.umich.edu}
4473113Sgblack@eecs.umich.edu
4483113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4493113Sgblack@eecs.umich.edutemplate<class OS>
4503113Sgblack@eecs.umich.edustatic void
4513113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4523113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4533113Sgblack@eecs.umich.edu{
4543113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4553113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4563113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4573113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4583113Sgblack@eecs.umich.edu}
4593113Sgblack@eecs.umich.edu
4603113Sgblack@eecs.umich.edutemplate<class OS>
4613113Sgblack@eecs.umich.edustatic void
4623113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4633113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4643113Sgblack@eecs.umich.edu{
4653113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4663113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4673113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4683113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4693113Sgblack@eecs.umich.edu}
4703113Sgblack@eecs.umich.edu
471378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
472378SN/A/// only to find out if their stdout is a tty, to determine whether to
473378SN/A/// do line or block buffering.
474360SN/Atemplate <class OS>
4751450SN/ASyscallReturn
4763114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4772680Sktlim@umich.edu          ThreadContext *tc)
478360SN/A{
4795958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
4805958Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, 1);
481360SN/A
4821969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
483360SN/A
484360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
485360SN/A        // doesn't map to any simulator fd: not a valid target fd
4861458SN/A        return -EBADF;
487360SN/A    }
488360SN/A
489360SN/A    switch (req) {
4904131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
4914131Sbinkertn@umich.edu      case OS::TIOCGETP_:
4924131Sbinkertn@umich.edu      case OS::TIOCSETP_:
4934131Sbinkertn@umich.edu      case OS::TIOCSETN_:
4944131Sbinkertn@umich.edu      case OS::TIOCSETC_:
4954131Sbinkertn@umich.edu      case OS::TIOCGETC_:
4964131Sbinkertn@umich.edu      case OS::TIOCGETS_:
4974131Sbinkertn@umich.edu      case OS::TIOCGETA_:
4981458SN/A        return -ENOTTY;
499360SN/A
500360SN/A      default:
5011706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
5022680Sktlim@umich.edu              fd, req, tc->readPC());
503360SN/A    }
504360SN/A}
505360SN/A
506378SN/A/// Target open() handler.
507360SN/Atemplate <class OS>
5081450SN/ASyscallReturn
5093114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5102680Sktlim@umich.edu         ThreadContext *tc)
511360SN/A{
512360SN/A    std::string path;
513360SN/A
5145958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5151458SN/A        return -EFAULT;
516360SN/A
517360SN/A    if (path == "/dev/sysdev0") {
518360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
519360SN/A        // We don't support it, so just punt.
5201706SN/A        warn("Ignoring open(%s, ...)\n", path);
5211458SN/A        return -ENOENT;
522360SN/A    }
523360SN/A
5245958Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, 1);
5255958Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, 2);
526360SN/A    int hostFlags = 0;
527360SN/A
528360SN/A    // translate open flags
529360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
530360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
531360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
532360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
533360SN/A        }
534360SN/A    }
535360SN/A
536360SN/A    // any target flags left?
537360SN/A    if (tgtFlags != 0)
5381706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
539360SN/A
540360SN/A#ifdef __CYGWIN32__
541360SN/A    hostFlags |= O_BINARY;
542360SN/A#endif
543360SN/A
5443669Sbinkertn@umich.edu    // Adjust path for current working directory
5453669Sbinkertn@umich.edu    path = process->fullPath(path);
5463669Sbinkertn@umich.edu
5471706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5481706SN/A
5495795Ssaidi@eecs.umich.edu    int fd;
5505795Ssaidi@eecs.umich.edu    if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
5515795Ssaidi@eecs.umich.edu        !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
5525795Ssaidi@eecs.umich.edu        // It's a proc/sys entery and requires special handling
5535795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
5545795Ssaidi@eecs.umich.edu        return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5555795Ssaidi@eecs.umich.edu     } else {
5565795Ssaidi@eecs.umich.edu        // open the file
5575795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
5585795Ssaidi@eecs.umich.edu        return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5595795Ssaidi@eecs.umich.edu     }
560360SN/A
561360SN/A}
562360SN/A
5636640Svince@csl.cornell.edu/// Target sysinfo() handler.
5646640Svince@csl.cornell.edutemplate <class OS>
5656640Svince@csl.cornell.eduSyscallReturn
5666640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5676640Svince@csl.cornell.edu         ThreadContext *tc)
5686640Svince@csl.cornell.edu{
5696640Svince@csl.cornell.edu
5706640Svince@csl.cornell.edu   TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0));
5716640Svince@csl.cornell.edu
5726640Svince@csl.cornell.edu   sysinfo->uptime=seconds_since_epoch;
5736640Svince@csl.cornell.edu   sysinfo->totalram=process->system->memSize();
5746640Svince@csl.cornell.edu
5756640Svince@csl.cornell.edu   sysinfo.copyOut(tc->getMemPort());
5766640Svince@csl.cornell.edu
5776640Svince@csl.cornell.edu   return 0;
5786640Svince@csl.cornell.edu}
579360SN/A
5801999SN/A/// Target chmod() handler.
5811999SN/Atemplate <class OS>
5821999SN/ASyscallReturn
5833114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5842680Sktlim@umich.edu          ThreadContext *tc)
5851999SN/A{
5861999SN/A    std::string path;
5871999SN/A
5885958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5891999SN/A        return -EFAULT;
5901999SN/A
5915958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
5921999SN/A    mode_t hostMode = 0;
5931999SN/A
5941999SN/A    // XXX translate mode flags via OS::something???
5951999SN/A    hostMode = mode;
5961999SN/A
5973669Sbinkertn@umich.edu    // Adjust path for current working directory
5983669Sbinkertn@umich.edu    path = process->fullPath(path);
5993669Sbinkertn@umich.edu
6001999SN/A    // do the chmod
6011999SN/A    int result = chmod(path.c_str(), hostMode);
6021999SN/A    if (result < 0)
6032218SN/A        return -errno;
6041999SN/A
6051999SN/A    return 0;
6061999SN/A}
6071999SN/A
6081999SN/A
6091999SN/A/// Target fchmod() handler.
6101999SN/Atemplate <class OS>
6111999SN/ASyscallReturn
6123114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6132680Sktlim@umich.edu           ThreadContext *tc)
6141999SN/A{
6155958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
6161999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6171999SN/A        // doesn't map to any simulator fd: not a valid target fd
6181999SN/A        return -EBADF;
6191999SN/A    }
6201999SN/A
6215958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
6221999SN/A    mode_t hostMode = 0;
6231999SN/A
6241999SN/A    // XXX translate mode flags via OS::someting???
6251999SN/A    hostMode = mode;
6261999SN/A
6271999SN/A    // do the fchmod
6281999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
6291999SN/A    if (result < 0)
6302218SN/A        return -errno;
6311999SN/A
6321999SN/A    return 0;
6331999SN/A}
6341999SN/A
6355877Shsul@eecs.umich.edu/// Target mremap() handler.
6365877Shsul@eecs.umich.edutemplate <class OS>
6375877Shsul@eecs.umich.eduSyscallReturn
6385877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
6395877Shsul@eecs.umich.edu{
6405958Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, 0);
6415958Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, 1);
6425958Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, 2);
6435958Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, 3);
6445877Shsul@eecs.umich.edu
6455877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
6465877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
6475877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
6485877Shsul@eecs.umich.edu        return -EINVAL;
6495877Shsul@eecs.umich.edu    }
6505877Shsul@eecs.umich.edu
6515877Shsul@eecs.umich.edu    if (new_length > old_length) {
6525877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
6535877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
6545877Shsul@eecs.umich.edu            process->pTable->allocate(process->mmap_end, diff);
6555877Shsul@eecs.umich.edu            process->mmap_end += diff;
6565877Shsul@eecs.umich.edu            return start;
6575877Shsul@eecs.umich.edu        } else {
6585877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
6595877Shsul@eecs.umich.edu            if (!(flags & 1)) {
6605877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
6615877Shsul@eecs.umich.edu                return -ENOMEM;
6625877Shsul@eecs.umich.edu            } else {
6635877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
6645877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
6655877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
6665877Shsul@eecs.umich.edu                start = process->mmap_end;
6675877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
6685877Shsul@eecs.umich.edu                process->pTable->allocate(start + old_length, new_length - old_length);
6695877Shsul@eecs.umich.edu                process->mmap_end += new_length;
6705877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
6715877Shsul@eecs.umich.edu                return start;
6725877Shsul@eecs.umich.edu            }
6735877Shsul@eecs.umich.edu        }
6745877Shsul@eecs.umich.edu    } else {
6755877Shsul@eecs.umich.edu        process->pTable->deallocate(start + new_length, old_length -
6765877Shsul@eecs.umich.edu                new_length);
6775877Shsul@eecs.umich.edu        return start;
6785877Shsul@eecs.umich.edu    }
6795877Shsul@eecs.umich.edu}
6801999SN/A
681378SN/A/// Target stat() handler.
682360SN/Atemplate <class OS>
6831450SN/ASyscallReturn
6843114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6852680Sktlim@umich.edu         ThreadContext *tc)
686360SN/A{
687360SN/A    std::string path;
688360SN/A
6895958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
6902400SN/A    return -EFAULT;
691360SN/A
6923669Sbinkertn@umich.edu    // Adjust path for current working directory
6933669Sbinkertn@umich.edu    path = process->fullPath(path);
6943669Sbinkertn@umich.edu
695360SN/A    struct stat hostBuf;
696360SN/A    int result = stat(path.c_str(), &hostBuf);
697360SN/A
698360SN/A    if (result < 0)
6992218SN/A        return -errno;
700360SN/A
7015958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7025958Sgblack@eecs.umich.edu            &hostBuf);
703360SN/A
7041458SN/A    return 0;
705360SN/A}
706360SN/A
707360SN/A
7085074Ssaidi@eecs.umich.edu/// Target stat64() handler.
7095074Ssaidi@eecs.umich.edutemplate <class OS>
7105074Ssaidi@eecs.umich.eduSyscallReturn
7115074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7125074Ssaidi@eecs.umich.edu           ThreadContext *tc)
7135074Ssaidi@eecs.umich.edu{
7145074Ssaidi@eecs.umich.edu    std::string path;
7155074Ssaidi@eecs.umich.edu
7165958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7175074Ssaidi@eecs.umich.edu        return -EFAULT;
7185074Ssaidi@eecs.umich.edu
7195074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
7205074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
7215074Ssaidi@eecs.umich.edu
7225208Ssaidi@eecs.umich.edu#if NO_STAT64
7235208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
7245208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
7255208Ssaidi@eecs.umich.edu#else
7265074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
7275074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
7285208Ssaidi@eecs.umich.edu#endif
7295074Ssaidi@eecs.umich.edu
7305074Ssaidi@eecs.umich.edu    if (result < 0)
7315074Ssaidi@eecs.umich.edu        return -errno;
7325074Ssaidi@eecs.umich.edu
7335958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7345958Sgblack@eecs.umich.edu            &hostBuf);
7355074Ssaidi@eecs.umich.edu
7365074Ssaidi@eecs.umich.edu    return 0;
7375074Ssaidi@eecs.umich.edu}
7385074Ssaidi@eecs.umich.edu
7395074Ssaidi@eecs.umich.edu
7401999SN/A/// Target fstat64() handler.
7411999SN/Atemplate <class OS>
7421999SN/ASyscallReturn
7433114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7442680Sktlim@umich.edu            ThreadContext *tc)
7451999SN/A{
7465958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
7471999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7481999SN/A        // doesn't map to any simulator fd: not a valid target fd
7491999SN/A        return -EBADF;
7501999SN/A    }
7511999SN/A
7522764Sstever@eecs.umich.edu#if NO_STAT64
7532064SN/A    struct stat  hostBuf;
7542064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
7552064SN/A#else
7562064SN/A    struct stat64  hostBuf;
7571999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
7582064SN/A#endif
7591999SN/A
7601999SN/A    if (result < 0)
7612218SN/A        return -errno;
7621999SN/A
7635958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7643114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7651999SN/A
7661999SN/A    return 0;
7671999SN/A}
7681999SN/A
7691999SN/A
770378SN/A/// Target lstat() handler.
771360SN/Atemplate <class OS>
7721450SN/ASyscallReturn
7733114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7742680Sktlim@umich.edu          ThreadContext *tc)
775360SN/A{
776360SN/A    std::string path;
777360SN/A
7785958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7792400SN/A      return -EFAULT;
780360SN/A
7813669Sbinkertn@umich.edu    // Adjust path for current working directory
7823669Sbinkertn@umich.edu    path = process->fullPath(path);
7833669Sbinkertn@umich.edu
784360SN/A    struct stat hostBuf;
785360SN/A    int result = lstat(path.c_str(), &hostBuf);
786360SN/A
787360SN/A    if (result < 0)
7881458SN/A        return -errno;
789360SN/A
7905958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7915958Sgblack@eecs.umich.edu            &hostBuf);
792360SN/A
7931458SN/A    return 0;
794360SN/A}
795360SN/A
7961999SN/A/// Target lstat64() handler.
7971999SN/Atemplate <class OS>
7981999SN/ASyscallReturn
7993114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8002680Sktlim@umich.edu            ThreadContext *tc)
8011999SN/A{
8021999SN/A    std::string path;
8031999SN/A
8045958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8052400SN/A      return -EFAULT;
8061999SN/A
8073669Sbinkertn@umich.edu    // Adjust path for current working directory
8083669Sbinkertn@umich.edu    path = process->fullPath(path);
8093669Sbinkertn@umich.edu
8102764Sstever@eecs.umich.edu#if NO_STAT64
8112064SN/A    struct stat hostBuf;
8122064SN/A    int result = lstat(path.c_str(), &hostBuf);
8132064SN/A#else
8141999SN/A    struct stat64 hostBuf;
8151999SN/A    int result = lstat64(path.c_str(), &hostBuf);
8162064SN/A#endif
8171999SN/A
8181999SN/A    if (result < 0)
8191999SN/A        return -errno;
8201999SN/A
8215958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8225958Sgblack@eecs.umich.edu            &hostBuf);
8231999SN/A
8241999SN/A    return 0;
8251999SN/A}
8261999SN/A
827378SN/A/// Target fstat() handler.
828360SN/Atemplate <class OS>
8291450SN/ASyscallReturn
8303114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8312680Sktlim@umich.edu          ThreadContext *tc)
832360SN/A{
8335958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
834360SN/A
8351969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
836360SN/A
837360SN/A    if (fd < 0)
8381458SN/A        return -EBADF;
839360SN/A
840360SN/A    struct stat hostBuf;
841360SN/A    int result = fstat(fd, &hostBuf);
842360SN/A
843360SN/A    if (result < 0)
8441458SN/A        return -errno;
845360SN/A
8465958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8473114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
8482021SN/A
8491458SN/A    return 0;
850360SN/A}
851360SN/A
852360SN/A
8531706SN/A/// Target statfs() handler.
8541706SN/Atemplate <class OS>
8551706SN/ASyscallReturn
8563114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8572680Sktlim@umich.edu           ThreadContext *tc)
8581706SN/A{
8591706SN/A    std::string path;
8601706SN/A
8615958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8622400SN/A      return -EFAULT;
8631706SN/A
8643669Sbinkertn@umich.edu    // Adjust path for current working directory
8653669Sbinkertn@umich.edu    path = process->fullPath(path);
8663669Sbinkertn@umich.edu
8671706SN/A    struct statfs hostBuf;
8681706SN/A    int result = statfs(path.c_str(), &hostBuf);
8691706SN/A
8701706SN/A    if (result < 0)
8712218SN/A        return -errno;
8721706SN/A
8733114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
8745958Sgblack@eecs.umich.edu            (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
8751706SN/A
8761706SN/A    return 0;
8771706SN/A}
8781706SN/A
8791706SN/A
8801706SN/A/// Target fstatfs() handler.
8811706SN/Atemplate <class OS>
8821706SN/ASyscallReturn
8833114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8842680Sktlim@umich.edu            ThreadContext *tc)
8851706SN/A{
8865958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
8871706SN/A
8881706SN/A    if (fd < 0)
8891706SN/A        return -EBADF;
8901706SN/A
8911706SN/A    struct statfs hostBuf;
8921706SN/A    int result = fstatfs(fd, &hostBuf);
8931706SN/A
8941706SN/A    if (result < 0)
8952218SN/A        return -errno;
8961706SN/A
8975958Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
8983114Sgblack@eecs.umich.edu        &hostBuf);
8991706SN/A
9001706SN/A    return 0;
9011706SN/A}
9021706SN/A
9031706SN/A
9041999SN/A/// Target writev() handler.
9051999SN/Atemplate <class OS>
9061999SN/ASyscallReturn
9073114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9082680Sktlim@umich.edu           ThreadContext *tc)
9091999SN/A{
9105958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
9111999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9121999SN/A        // doesn't map to any simulator fd: not a valid target fd
9131999SN/A        return -EBADF;
9141999SN/A    }
9151999SN/A
9162680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
9175958Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, 1);
9185958Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, 2);
9191999SN/A    struct iovec hiov[count];
9206227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
9211999SN/A        typename OS::tgt_iovec tiov;
9222461SN/A
9232461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
9242461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
9252091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
9261999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
9272461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
9282461SN/A                    hiov[i].iov_len);
9291999SN/A    }
9301999SN/A
9311999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
9321999SN/A
9336227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
9341999SN/A        delete [] (char *)hiov[i].iov_base;
9351999SN/A
9361999SN/A    if (result < 0)
9372218SN/A        return -errno;
9381999SN/A
9391999SN/A    return 0;
9401999SN/A}
9411999SN/A
9421999SN/A
943378SN/A/// Target mmap() handler.
944378SN/A///
945378SN/A/// We don't really handle mmap().  If the target is mmaping an
946378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
947378SN/A/// nothing (since memory is initialized to zero and the simulator
948378SN/A/// doesn't really check addresses anyway).  Always print a warning,
949378SN/A/// since this could be seriously broken if we're not mapping
950378SN/A/// /dev/zero.
951360SN/A//
952378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
953378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
954378SN/A/// anything else.
955360SN/Atemplate <class OS>
9561450SN/ASyscallReturn
9573114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
958360SN/A{
9595958Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, 0);
9605958Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, 1);
9615958Sgblack@eecs.umich.edu    // int prot = p->getSyscallArg(tc, 2);
9625958Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, 3);
9635958Sgblack@eecs.umich.edu    // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
9645958Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, 5);
965360SN/A
9665877Shsul@eecs.umich.edu
9672544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
9682544SN/A        (length % TheISA::VMPageSize) != 0) {
9692544SN/A        warn("mmap failing: arguments not page-aligned: "
9702544SN/A             "start 0x%x length 0x%x",
9712544SN/A             start, length);
9722544SN/A        return -EINVAL;
973360SN/A    }
974360SN/A
9752544SN/A    if (start != 0) {
9762544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
9772544SN/A             start, p->mmap_end);
9782544SN/A    }
9792544SN/A
9802544SN/A    // pick next address from our "mmap region"
9812544SN/A    start = p->mmap_end;
9822544SN/A    p->pTable->allocate(start, length);
9832544SN/A    p->mmap_end += length;
9842544SN/A
9852553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
9861969SN/A        warn("allowing mmap of file @ fd %d. "
9875958Sgblack@eecs.umich.edu             "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
988360SN/A    }
989360SN/A
9901458SN/A    return start;
991360SN/A}
992360SN/A
993378SN/A/// Target getrlimit() handler.
994360SN/Atemplate <class OS>
9951450SN/ASyscallReturn
9963114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9972680Sktlim@umich.edu        ThreadContext *tc)
998360SN/A{
9995958Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, 0);
10005958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
1001360SN/A
1002360SN/A    switch (resource) {
10032064SN/A        case OS::TGT_RLIMIT_STACK:
10045877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
10052064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
10062091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
10072091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
10082064SN/A            break;
1009360SN/A
10105877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
10115877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
10125877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
10135877Shsul@eecs.umich.edu            rlp->rlim_cur = htog(rlp->rlim_cur);
10145877Shsul@eecs.umich.edu            rlp->rlim_max = htog(rlp->rlim_max);
10155877Shsul@eecs.umich.edu            break;
10165877Shsul@eecs.umich.edu
10172064SN/A        default:
10182064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
10192064SN/A                << std::endl;
10202064SN/A            abort();
10212064SN/A            break;
1022360SN/A    }
1023360SN/A
10242680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
10251458SN/A    return 0;
1026360SN/A}
1027360SN/A
1028378SN/A/// Target gettimeofday() handler.
1029360SN/Atemplate <class OS>
10301450SN/ASyscallReturn
10313114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10322680Sktlim@umich.edu        ThreadContext *tc)
1033360SN/A{
10345958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1035360SN/A
1036360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1037360SN/A    tp->tv_sec += seconds_since_epoch;
10386109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
10396109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1040360SN/A
10412680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
1042360SN/A
10431458SN/A    return 0;
1044360SN/A}
1045360SN/A
1046360SN/A
10471999SN/A/// Target utimes() handler.
10481999SN/Atemplate <class OS>
10491999SN/ASyscallReturn
10503114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10512680Sktlim@umich.edu           ThreadContext *tc)
10521999SN/A{
10531999SN/A    std::string path;
10541999SN/A
10555958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
10562400SN/A      return -EFAULT;
10571999SN/A
10585958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
10592680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
10601999SN/A
10611999SN/A    struct timeval hostTimeval[2];
10621999SN/A    for (int i = 0; i < 2; ++i)
10631999SN/A    {
10642091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
10652091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
10661999SN/A    }
10673669Sbinkertn@umich.edu
10683669Sbinkertn@umich.edu    // Adjust path for current working directory
10693669Sbinkertn@umich.edu    path = process->fullPath(path);
10703669Sbinkertn@umich.edu
10711999SN/A    int result = utimes(path.c_str(), hostTimeval);
10721999SN/A
10731999SN/A    if (result < 0)
10741999SN/A        return -errno;
10751999SN/A
10761999SN/A    return 0;
10771999SN/A}
1078378SN/A/// Target getrusage() function.
1079360SN/Atemplate <class OS>
10801450SN/ASyscallReturn
10813114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10822680Sktlim@umich.edu              ThreadContext *tc)
1083360SN/A{
10845958Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, 0);     // THREAD, SELF, or CHILDREN
10855958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1086360SN/A
10873670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
10883670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1089360SN/A    rup->ru_stime.tv_sec = 0;
1090360SN/A    rup->ru_stime.tv_usec = 0;
1091360SN/A    rup->ru_maxrss = 0;
1092360SN/A    rup->ru_ixrss = 0;
1093360SN/A    rup->ru_idrss = 0;
1094360SN/A    rup->ru_isrss = 0;
1095360SN/A    rup->ru_minflt = 0;
1096360SN/A    rup->ru_majflt = 0;
1097360SN/A    rup->ru_nswap = 0;
1098360SN/A    rup->ru_inblock = 0;
1099360SN/A    rup->ru_oublock = 0;
1100360SN/A    rup->ru_msgsnd = 0;
1101360SN/A    rup->ru_msgrcv = 0;
1102360SN/A    rup->ru_nsignals = 0;
1103360SN/A    rup->ru_nvcsw = 0;
1104360SN/A    rup->ru_nivcsw = 0;
1105360SN/A
11063670Sbinkertn@umich.edu    switch (who) {
11073670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
11083670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
11093670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
11103670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
11113670Sbinkertn@umich.edu        break;
11123670Sbinkertn@umich.edu
11133670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
11143670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
11153670Sbinkertn@umich.edu        break;
11163670Sbinkertn@umich.edu
11173670Sbinkertn@umich.edu      default:
11183670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
11193670Sbinkertn@umich.edu        // plow ahead
11203670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
11213670Sbinkertn@umich.edu             who);
11223670Sbinkertn@umich.edu    }
11233670Sbinkertn@umich.edu
11242680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1125360SN/A
11261458SN/A    return 0;
1127360SN/A}
1128360SN/A
11292553SN/A
11302553SN/A
11312553SN/A
11321354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1133