syscall_emul.hh revision 6109
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
535543Ssaidi@eecs.umich.edu#include "sim/host.hh"  // for Addr
542474SN/A#include "base/chunk_generator.hh"
555543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
562462SN/A#include "base/misc.hh"
571354SN/A#include "base/trace.hh"
582474SN/A#include "cpu/base.hh"
592680Sktlim@umich.edu#include "cpu/thread_context.hh"
602474SN/A#include "mem/translating_port.hh"
612474SN/A#include "mem/page_table.hh"
621354SN/A#include "sim/process.hh"
63360SN/A
64360SN/A///
65360SN/A/// System call descriptor.
66360SN/A///
67360SN/Aclass SyscallDesc {
68360SN/A
69360SN/A  public:
70360SN/A
71378SN/A    /// Typedef for target syscall handler functions.
721450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
733114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
74360SN/A
755543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
765543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
775543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
78360SN/A
79360SN/A    /// Flag values for controlling syscall behavior.
80360SN/A    enum Flags {
81360SN/A        /// Don't set return regs according to funcPtr return value.
82360SN/A        /// Used for syscalls with non-standard return conventions
832680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
84360SN/A        /// sigreturn).
85360SN/A        SuppressReturnValue = 1
86360SN/A    };
87360SN/A
88360SN/A    /// Constructor.
89360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
90360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
91360SN/A    {
92360SN/A    }
93360SN/A
94360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
953114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
96360SN/A};
97360SN/A
98360SN/A
99360SN/Aclass BaseBufferArg {
100360SN/A
101360SN/A  public:
102360SN/A
103360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
104360SN/A    {
105360SN/A        bufPtr = new uint8_t[size];
106360SN/A        // clear out buffer: in case we only partially populate this,
107360SN/A        // and then do a copyOut(), we want to make sure we don't
108360SN/A        // introduce any random junk into the simulated address space
109360SN/A        memset(bufPtr, 0, size);
110360SN/A    }
111360SN/A
112360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
113360SN/A
114360SN/A    //
115360SN/A    // copy data into simulator space (read from target memory)
116360SN/A    //
1172400SN/A    virtual bool copyIn(TranslatingPort *memport)
118360SN/A    {
1192461SN/A        memport->readBlob(addr, bufPtr, size);
1205543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
121360SN/A    }
122360SN/A
123360SN/A    //
124360SN/A    // copy data out of simulator space (write to target memory)
125360SN/A    //
1262400SN/A    virtual bool copyOut(TranslatingPort *memport)
127360SN/A    {
1282461SN/A        memport->writeBlob(addr, bufPtr, size);
1295543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
130360SN/A    }
131360SN/A
132360SN/A  protected:
133360SN/A    Addr addr;
134360SN/A    int size;
135360SN/A    uint8_t *bufPtr;
136360SN/A};
137360SN/A
138360SN/A
139360SN/Aclass BufferArg : public BaseBufferArg
140360SN/A{
141360SN/A  public:
142360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1435543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
144360SN/A};
145360SN/A
146360SN/Atemplate <class T>
147360SN/Aclass TypedBufferArg : public BaseBufferArg
148360SN/A{
149360SN/A  public:
150360SN/A    // user can optionally specify a specific number of bytes to
151360SN/A    // allocate to deal with those structs that have variable-size
152360SN/A    // arrays at the end
153360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
154360SN/A        : BaseBufferArg(_addr, _size)
155360SN/A    { }
156360SN/A
157360SN/A    // type case
158360SN/A    operator T*() { return (T *)bufPtr; }
159360SN/A
160360SN/A    // dereference operators
1615543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1625543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
163502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
164360SN/A};
165360SN/A
166360SN/A//////////////////////////////////////////////////////////////////////
167360SN/A//
168360SN/A// The following emulation functions are generic enough that they
169360SN/A// don't need to be recompiled for different emulated OS's.  They are
170360SN/A// defined in sim/syscall_emul.cc.
171360SN/A//
172360SN/A//////////////////////////////////////////////////////////////////////
173360SN/A
174360SN/A
175378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1761706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1773114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
178378SN/A
179378SN/A/// Handler for unimplemented syscalls that we never intend to
180378SN/A/// implement (signal handling, etc.) and should not affect the correct
181378SN/A/// behavior of the program.  Print a warning only if the appropriate
182378SN/A/// trace flag is enabled.  Return success to the target program.
1831706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
185360SN/A
1866109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1871706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1883114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
189378SN/A
1906109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1916109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1926109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1936109Ssanchezd@stanford.edu
194378SN/A/// Target getpagesize() handler.
1951706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1963114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
197378SN/A
1985748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1995748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2005748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
201378SN/A
202378SN/A/// Target close() handler.
2031706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2043114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
205378SN/A
206378SN/A/// Target read() handler.
2071706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2083114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
209378SN/A
210378SN/A/// Target write() handler.
2111706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2123114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
213378SN/A
214378SN/A/// Target lseek() handler.
2151706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2163114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
217378SN/A
2184118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2194118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2204118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2214118Sgblack@eecs.umich.edu
222378SN/A/// Target munmap() handler.
2231706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
225378SN/A
226378SN/A/// Target gethostname() handler.
2271706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2283114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
229360SN/A
2305513SMichael.Adler@intel.com/// Target getcwd() handler.
2315513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2325513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2335513SMichael.Adler@intel.com
2345513SMichael.Adler@intel.com/// Target unlink() handler.
2355513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2365513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2375513SMichael.Adler@intel.com
238511SN/A/// Target unlink() handler.
2391706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2403114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
241511SN/A
2425513SMichael.Adler@intel.com/// Target mkdir() handler.
2435513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2445513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2455513SMichael.Adler@intel.com
246511SN/A/// Target rename() handler.
2471706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2483114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2491706SN/A
2501706SN/A
2511706SN/A/// Target truncate() handler.
2521706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2533114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2541706SN/A
2551706SN/A
2561706SN/A/// Target ftruncate() handler.
2571706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2583114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2591706SN/A
260511SN/A
2615513SMichael.Adler@intel.com/// Target umask() handler.
2625513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2635513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2645513SMichael.Adler@intel.com
2655513SMichael.Adler@intel.com
2661999SN/A/// Target chown() handler.
2671999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2691999SN/A
2701999SN/A
2711999SN/A/// Target fchown() handler.
2721999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2741999SN/A
2753079Sstever@eecs.umich.edu/// Target dup() handler.
2763079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2773114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2783079Sstever@eecs.umich.edu
2792093SN/A/// Target fnctl() handler.
2802093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2813114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2822093SN/A
2832687Sksewell@umich.edu/// Target fcntl64() handler.
2842687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2853114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2862687Sksewell@umich.edu
2872238SN/A/// Target setuid() handler.
2882238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2893114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2902238SN/A
2912238SN/A/// Target getpid() handler.
2922238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2933114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2942238SN/A
2952238SN/A/// Target getuid() handler.
2962238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2973114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2982238SN/A
2992238SN/A/// Target getgid() handler.
3002238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3013114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3022238SN/A
3032238SN/A/// Target getppid() handler.
3042238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3053114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3062238SN/A
3072238SN/A/// Target geteuid() handler.
3082238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3093114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3102238SN/A
3112238SN/A/// Target getegid() handler.
3122238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3133114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3142238SN/A
3156109Ssanchezd@stanford.edu/// Target clone() handler.
3166109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3176109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3182238SN/A
3192238SN/A
3202238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3212238SN/A/// returning a second value in a register other than the normal return register
3222238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3233114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3242238SN/A
3252238SN/A/// Target getpidPseudo() handler.
3262238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3273114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3282238SN/A
3292238SN/A/// Target getuidPseudo() handler.
3302238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3313114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3322238SN/A
3332238SN/A/// Target getgidPseudo() handler.
3342238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3353114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3362238SN/A
3372238SN/A
3381354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3391354SN/Aconst int one_million = 1000000;
3401354SN/A
3411354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3421354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3431354SN/A/// real-world time) to keep simulations repeatable.
3441354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3451354SN/A
3461354SN/A/// Helper function to convert current elapsed time to seconds and
3471354SN/A/// microseconds.
3481354SN/Atemplate <class T1, class T2>
3491354SN/Avoid
3501354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3511354SN/A{
3521609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3531354SN/A    sec = elapsed_usecs / one_million;
3541354SN/A    usec = elapsed_usecs % one_million;
3551354SN/A}
3561354SN/A
357360SN/A//////////////////////////////////////////////////////////////////////
358360SN/A//
359360SN/A// The following emulation functions are generic, but need to be
360360SN/A// templated to account for differences in types, constants, etc.
361360SN/A//
362360SN/A//////////////////////////////////////////////////////////////////////
363360SN/A
3643113Sgblack@eecs.umich.edu#if NO_STAT64
3653113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3663113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3673113Sgblack@eecs.umich.edu#else
3683113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3693113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3703113Sgblack@eecs.umich.edu#endif
3713113Sgblack@eecs.umich.edu
3723113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3733113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3743113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3753113Sgblack@eecs.umich.edu
3763113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3773113Sgblack@eecs.umich.edustatic void
3783113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3793113Sgblack@eecs.umich.edu{
3804189Sgblack@eecs.umich.edu    using namespace TheISA;
3814189Sgblack@eecs.umich.edu
3823113Sgblack@eecs.umich.edu    if (fakeTTY)
3833113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3843113Sgblack@eecs.umich.edu    else
3853113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3863113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3873113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3883113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3893277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
3905515SMichael.Adler@intel.com    if (fakeTTY) {
3915515SMichael.Adler@intel.com        // Claim to be a character device
3925515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
3935515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
3945515SMichael.Adler@intel.com    }
3953277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
3963277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
3973277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
3983277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
3993277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
4003277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4013277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
4023113Sgblack@eecs.umich.edu    if (fakeTTY)
4033113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4043113Sgblack@eecs.umich.edu    else
4053113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4063113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
4073113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4083113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
4093114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4103113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
4113114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4123113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
4133114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4143113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
4154061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4164061Sgblack@eecs.umich.edu    // consistently across different hosts.
4174061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4183113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
4193113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4203113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
4213113Sgblack@eecs.umich.edu}
4223113Sgblack@eecs.umich.edu
4233113Sgblack@eecs.umich.edu// Same for stat64
4243113Sgblack@eecs.umich.edu
4253113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4263113Sgblack@eecs.umich.edustatic void
4273113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4283113Sgblack@eecs.umich.edu{
4294189Sgblack@eecs.umich.edu    using namespace TheISA;
4304189Sgblack@eecs.umich.edu
4313113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4323113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4333113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4343113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4353113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4363113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4373113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4383113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4393113Sgblack@eecs.umich.edu#else
4403113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4413113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4423113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4433113Sgblack@eecs.umich.edu#endif
4443113Sgblack@eecs.umich.edu}
4453113Sgblack@eecs.umich.edu
4463113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4473113Sgblack@eecs.umich.edutemplate<class OS>
4483113Sgblack@eecs.umich.edustatic void
4493113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4503113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4513113Sgblack@eecs.umich.edu{
4523113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4533113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4543113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4553113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4563113Sgblack@eecs.umich.edu}
4573113Sgblack@eecs.umich.edu
4583113Sgblack@eecs.umich.edutemplate<class OS>
4593113Sgblack@eecs.umich.edustatic void
4603113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4613113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4623113Sgblack@eecs.umich.edu{
4633113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4643113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4653113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4663113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4673113Sgblack@eecs.umich.edu}
4683113Sgblack@eecs.umich.edu
469378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
470378SN/A/// only to find out if their stdout is a tty, to determine whether to
471378SN/A/// do line or block buffering.
472360SN/Atemplate <class OS>
4731450SN/ASyscallReturn
4743114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4752680Sktlim@umich.edu          ThreadContext *tc)
476360SN/A{
4775958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
4785958Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, 1);
479360SN/A
4801969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
481360SN/A
482360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
483360SN/A        // doesn't map to any simulator fd: not a valid target fd
4841458SN/A        return -EBADF;
485360SN/A    }
486360SN/A
487360SN/A    switch (req) {
4884131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
4894131Sbinkertn@umich.edu      case OS::TIOCGETP_:
4904131Sbinkertn@umich.edu      case OS::TIOCSETP_:
4914131Sbinkertn@umich.edu      case OS::TIOCSETN_:
4924131Sbinkertn@umich.edu      case OS::TIOCSETC_:
4934131Sbinkertn@umich.edu      case OS::TIOCGETC_:
4944131Sbinkertn@umich.edu      case OS::TIOCGETS_:
4954131Sbinkertn@umich.edu      case OS::TIOCGETA_:
4961458SN/A        return -ENOTTY;
497360SN/A
498360SN/A      default:
4991706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
5002680Sktlim@umich.edu              fd, req, tc->readPC());
501360SN/A    }
502360SN/A}
503360SN/A
504378SN/A/// Target open() handler.
505360SN/Atemplate <class OS>
5061450SN/ASyscallReturn
5073114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5082680Sktlim@umich.edu         ThreadContext *tc)
509360SN/A{
510360SN/A    std::string path;
511360SN/A
5125958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5131458SN/A        return -EFAULT;
514360SN/A
515360SN/A    if (path == "/dev/sysdev0") {
516360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
517360SN/A        // We don't support it, so just punt.
5181706SN/A        warn("Ignoring open(%s, ...)\n", path);
5191458SN/A        return -ENOENT;
520360SN/A    }
521360SN/A
5225958Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, 1);
5235958Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, 2);
524360SN/A    int hostFlags = 0;
525360SN/A
526360SN/A    // translate open flags
527360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
528360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
529360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
530360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
531360SN/A        }
532360SN/A    }
533360SN/A
534360SN/A    // any target flags left?
535360SN/A    if (tgtFlags != 0)
5361706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
537360SN/A
538360SN/A#ifdef __CYGWIN32__
539360SN/A    hostFlags |= O_BINARY;
540360SN/A#endif
541360SN/A
5423669Sbinkertn@umich.edu    // Adjust path for current working directory
5433669Sbinkertn@umich.edu    path = process->fullPath(path);
5443669Sbinkertn@umich.edu
5451706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5461706SN/A
5475795Ssaidi@eecs.umich.edu    int fd;
5485795Ssaidi@eecs.umich.edu    if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
5495795Ssaidi@eecs.umich.edu        !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
5505795Ssaidi@eecs.umich.edu        // It's a proc/sys entery and requires special handling
5515795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
5525795Ssaidi@eecs.umich.edu        return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5535795Ssaidi@eecs.umich.edu     } else {
5545795Ssaidi@eecs.umich.edu        // open the file
5555795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
5565795Ssaidi@eecs.umich.edu        return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5575795Ssaidi@eecs.umich.edu     }
558360SN/A
559360SN/A}
560360SN/A
561360SN/A
5621999SN/A/// Target chmod() handler.
5631999SN/Atemplate <class OS>
5641999SN/ASyscallReturn
5653114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5662680Sktlim@umich.edu          ThreadContext *tc)
5671999SN/A{
5681999SN/A    std::string path;
5691999SN/A
5705958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5711999SN/A        return -EFAULT;
5721999SN/A
5735958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
5741999SN/A    mode_t hostMode = 0;
5751999SN/A
5761999SN/A    // XXX translate mode flags via OS::something???
5771999SN/A    hostMode = mode;
5781999SN/A
5793669Sbinkertn@umich.edu    // Adjust path for current working directory
5803669Sbinkertn@umich.edu    path = process->fullPath(path);
5813669Sbinkertn@umich.edu
5821999SN/A    // do the chmod
5831999SN/A    int result = chmod(path.c_str(), hostMode);
5841999SN/A    if (result < 0)
5852218SN/A        return -errno;
5861999SN/A
5871999SN/A    return 0;
5881999SN/A}
5891999SN/A
5901999SN/A
5911999SN/A/// Target fchmod() handler.
5921999SN/Atemplate <class OS>
5931999SN/ASyscallReturn
5943114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5952680Sktlim@umich.edu           ThreadContext *tc)
5961999SN/A{
5975958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
5981999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5991999SN/A        // doesn't map to any simulator fd: not a valid target fd
6001999SN/A        return -EBADF;
6011999SN/A    }
6021999SN/A
6035958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
6041999SN/A    mode_t hostMode = 0;
6051999SN/A
6061999SN/A    // XXX translate mode flags via OS::someting???
6071999SN/A    hostMode = mode;
6081999SN/A
6091999SN/A    // do the fchmod
6101999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
6111999SN/A    if (result < 0)
6122218SN/A        return -errno;
6131999SN/A
6141999SN/A    return 0;
6151999SN/A}
6161999SN/A
6175877Shsul@eecs.umich.edu/// Target mremap() handler.
6185877Shsul@eecs.umich.edutemplate <class OS>
6195877Shsul@eecs.umich.eduSyscallReturn
6205877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
6215877Shsul@eecs.umich.edu{
6225958Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, 0);
6235958Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, 1);
6245958Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, 2);
6255958Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, 3);
6265877Shsul@eecs.umich.edu
6275877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
6285877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
6295877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
6305877Shsul@eecs.umich.edu        return -EINVAL;
6315877Shsul@eecs.umich.edu    }
6325877Shsul@eecs.umich.edu
6335877Shsul@eecs.umich.edu    if (new_length > old_length) {
6345877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
6355877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
6365877Shsul@eecs.umich.edu            process->pTable->allocate(process->mmap_end, diff);
6375877Shsul@eecs.umich.edu            process->mmap_end += diff;
6385877Shsul@eecs.umich.edu            return start;
6395877Shsul@eecs.umich.edu        } else {
6405877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
6415877Shsul@eecs.umich.edu            if (!(flags & 1)) {
6425877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
6435877Shsul@eecs.umich.edu                return -ENOMEM;
6445877Shsul@eecs.umich.edu            } else {
6455877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
6465877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
6475877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
6485877Shsul@eecs.umich.edu                start = process->mmap_end;
6495877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
6505877Shsul@eecs.umich.edu                process->pTable->allocate(start + old_length, new_length - old_length);
6515877Shsul@eecs.umich.edu                process->mmap_end += new_length;
6525877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
6535877Shsul@eecs.umich.edu                return start;
6545877Shsul@eecs.umich.edu            }
6555877Shsul@eecs.umich.edu        }
6565877Shsul@eecs.umich.edu    } else {
6575877Shsul@eecs.umich.edu        process->pTable->deallocate(start + new_length, old_length -
6585877Shsul@eecs.umich.edu                new_length);
6595877Shsul@eecs.umich.edu        return start;
6605877Shsul@eecs.umich.edu    }
6615877Shsul@eecs.umich.edu}
6621999SN/A
663378SN/A/// Target stat() handler.
664360SN/Atemplate <class OS>
6651450SN/ASyscallReturn
6663114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6672680Sktlim@umich.edu         ThreadContext *tc)
668360SN/A{
669360SN/A    std::string path;
670360SN/A
6715958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
6722400SN/A    return -EFAULT;
673360SN/A
6743669Sbinkertn@umich.edu    // Adjust path for current working directory
6753669Sbinkertn@umich.edu    path = process->fullPath(path);
6763669Sbinkertn@umich.edu
677360SN/A    struct stat hostBuf;
678360SN/A    int result = stat(path.c_str(), &hostBuf);
679360SN/A
680360SN/A    if (result < 0)
6812218SN/A        return -errno;
682360SN/A
6835958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
6845958Sgblack@eecs.umich.edu            &hostBuf);
685360SN/A
6861458SN/A    return 0;
687360SN/A}
688360SN/A
689360SN/A
6905074Ssaidi@eecs.umich.edu/// Target stat64() handler.
6915074Ssaidi@eecs.umich.edutemplate <class OS>
6925074Ssaidi@eecs.umich.eduSyscallReturn
6935074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6945074Ssaidi@eecs.umich.edu           ThreadContext *tc)
6955074Ssaidi@eecs.umich.edu{
6965074Ssaidi@eecs.umich.edu    std::string path;
6975074Ssaidi@eecs.umich.edu
6985958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
6995074Ssaidi@eecs.umich.edu        return -EFAULT;
7005074Ssaidi@eecs.umich.edu
7015074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
7025074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
7035074Ssaidi@eecs.umich.edu
7045208Ssaidi@eecs.umich.edu#if NO_STAT64
7055208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
7065208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
7075208Ssaidi@eecs.umich.edu#else
7085074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
7095074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
7105208Ssaidi@eecs.umich.edu#endif
7115074Ssaidi@eecs.umich.edu
7125074Ssaidi@eecs.umich.edu    if (result < 0)
7135074Ssaidi@eecs.umich.edu        return -errno;
7145074Ssaidi@eecs.umich.edu
7155958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7165958Sgblack@eecs.umich.edu            &hostBuf);
7175074Ssaidi@eecs.umich.edu
7185074Ssaidi@eecs.umich.edu    return 0;
7195074Ssaidi@eecs.umich.edu}
7205074Ssaidi@eecs.umich.edu
7215074Ssaidi@eecs.umich.edu
7221999SN/A/// Target fstat64() handler.
7231999SN/Atemplate <class OS>
7241999SN/ASyscallReturn
7253114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7262680Sktlim@umich.edu            ThreadContext *tc)
7271999SN/A{
7285958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
7291999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7301999SN/A        // doesn't map to any simulator fd: not a valid target fd
7311999SN/A        return -EBADF;
7321999SN/A    }
7331999SN/A
7342764Sstever@eecs.umich.edu#if NO_STAT64
7352064SN/A    struct stat  hostBuf;
7362064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
7372064SN/A#else
7382064SN/A    struct stat64  hostBuf;
7391999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
7402064SN/A#endif
7411999SN/A
7421999SN/A    if (result < 0)
7432218SN/A        return -errno;
7441999SN/A
7455958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7463114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7471999SN/A
7481999SN/A    return 0;
7491999SN/A}
7501999SN/A
7511999SN/A
752378SN/A/// Target lstat() handler.
753360SN/Atemplate <class OS>
7541450SN/ASyscallReturn
7553114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7562680Sktlim@umich.edu          ThreadContext *tc)
757360SN/A{
758360SN/A    std::string path;
759360SN/A
7605958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7612400SN/A      return -EFAULT;
762360SN/A
7633669Sbinkertn@umich.edu    // Adjust path for current working directory
7643669Sbinkertn@umich.edu    path = process->fullPath(path);
7653669Sbinkertn@umich.edu
766360SN/A    struct stat hostBuf;
767360SN/A    int result = lstat(path.c_str(), &hostBuf);
768360SN/A
769360SN/A    if (result < 0)
7701458SN/A        return -errno;
771360SN/A
7725958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7735958Sgblack@eecs.umich.edu            &hostBuf);
774360SN/A
7751458SN/A    return 0;
776360SN/A}
777360SN/A
7781999SN/A/// Target lstat64() handler.
7791999SN/Atemplate <class OS>
7801999SN/ASyscallReturn
7813114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7822680Sktlim@umich.edu            ThreadContext *tc)
7831999SN/A{
7841999SN/A    std::string path;
7851999SN/A
7865958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7872400SN/A      return -EFAULT;
7881999SN/A
7893669Sbinkertn@umich.edu    // Adjust path for current working directory
7903669Sbinkertn@umich.edu    path = process->fullPath(path);
7913669Sbinkertn@umich.edu
7922764Sstever@eecs.umich.edu#if NO_STAT64
7932064SN/A    struct stat hostBuf;
7942064SN/A    int result = lstat(path.c_str(), &hostBuf);
7952064SN/A#else
7961999SN/A    struct stat64 hostBuf;
7971999SN/A    int result = lstat64(path.c_str(), &hostBuf);
7982064SN/A#endif
7991999SN/A
8001999SN/A    if (result < 0)
8011999SN/A        return -errno;
8021999SN/A
8035958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8045958Sgblack@eecs.umich.edu            &hostBuf);
8051999SN/A
8061999SN/A    return 0;
8071999SN/A}
8081999SN/A
809378SN/A/// Target fstat() handler.
810360SN/Atemplate <class OS>
8111450SN/ASyscallReturn
8123114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8132680Sktlim@umich.edu          ThreadContext *tc)
814360SN/A{
8155958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
816360SN/A
8171969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
818360SN/A
819360SN/A    if (fd < 0)
8201458SN/A        return -EBADF;
821360SN/A
822360SN/A    struct stat hostBuf;
823360SN/A    int result = fstat(fd, &hostBuf);
824360SN/A
825360SN/A    if (result < 0)
8261458SN/A        return -errno;
827360SN/A
8285958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8293114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
8302021SN/A
8311458SN/A    return 0;
832360SN/A}
833360SN/A
834360SN/A
8351706SN/A/// Target statfs() handler.
8361706SN/Atemplate <class OS>
8371706SN/ASyscallReturn
8383114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8392680Sktlim@umich.edu           ThreadContext *tc)
8401706SN/A{
8411706SN/A    std::string path;
8421706SN/A
8435958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8442400SN/A      return -EFAULT;
8451706SN/A
8463669Sbinkertn@umich.edu    // Adjust path for current working directory
8473669Sbinkertn@umich.edu    path = process->fullPath(path);
8483669Sbinkertn@umich.edu
8491706SN/A    struct statfs hostBuf;
8501706SN/A    int result = statfs(path.c_str(), &hostBuf);
8511706SN/A
8521706SN/A    if (result < 0)
8532218SN/A        return -errno;
8541706SN/A
8553114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
8565958Sgblack@eecs.umich.edu            (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
8571706SN/A
8581706SN/A    return 0;
8591706SN/A}
8601706SN/A
8611706SN/A
8621706SN/A/// Target fstatfs() handler.
8631706SN/Atemplate <class OS>
8641706SN/ASyscallReturn
8653114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8662680Sktlim@umich.edu            ThreadContext *tc)
8671706SN/A{
8685958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
8691706SN/A
8701706SN/A    if (fd < 0)
8711706SN/A        return -EBADF;
8721706SN/A
8731706SN/A    struct statfs hostBuf;
8741706SN/A    int result = fstatfs(fd, &hostBuf);
8751706SN/A
8761706SN/A    if (result < 0)
8772218SN/A        return -errno;
8781706SN/A
8795958Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
8803114Sgblack@eecs.umich.edu        &hostBuf);
8811706SN/A
8821706SN/A    return 0;
8831706SN/A}
8841706SN/A
8851706SN/A
8861999SN/A/// Target writev() handler.
8871999SN/Atemplate <class OS>
8881999SN/ASyscallReturn
8893114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8902680Sktlim@umich.edu           ThreadContext *tc)
8911999SN/A{
8925958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
8931999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8941999SN/A        // doesn't map to any simulator fd: not a valid target fd
8951999SN/A        return -EBADF;
8961999SN/A    }
8971999SN/A
8982680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
8995958Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, 1);
9005958Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, 2);
9011999SN/A    struct iovec hiov[count];
9021999SN/A    for (int i = 0; i < count; ++i)
9031999SN/A    {
9041999SN/A        typename OS::tgt_iovec tiov;
9052461SN/A
9062461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
9072461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
9082091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
9091999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
9102461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
9112461SN/A                    hiov[i].iov_len);
9121999SN/A    }
9131999SN/A
9141999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
9151999SN/A
9161999SN/A    for (int i = 0; i < count; ++i)
9171999SN/A    {
9181999SN/A        delete [] (char *)hiov[i].iov_base;
9191999SN/A    }
9201999SN/A
9211999SN/A    if (result < 0)
9222218SN/A        return -errno;
9231999SN/A
9241999SN/A    return 0;
9251999SN/A}
9261999SN/A
9271999SN/A
928378SN/A/// Target mmap() handler.
929378SN/A///
930378SN/A/// We don't really handle mmap().  If the target is mmaping an
931378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
932378SN/A/// nothing (since memory is initialized to zero and the simulator
933378SN/A/// doesn't really check addresses anyway).  Always print a warning,
934378SN/A/// since this could be seriously broken if we're not mapping
935378SN/A/// /dev/zero.
936360SN/A//
937378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
938378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
939378SN/A/// anything else.
940360SN/Atemplate <class OS>
9411450SN/ASyscallReturn
9423114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
943360SN/A{
9445958Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, 0);
9455958Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, 1);
9465958Sgblack@eecs.umich.edu    // int prot = p->getSyscallArg(tc, 2);
9475958Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, 3);
9485958Sgblack@eecs.umich.edu    // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
9495958Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, 5);
950360SN/A
9515877Shsul@eecs.umich.edu
9522544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
9532544SN/A        (length % TheISA::VMPageSize) != 0) {
9542544SN/A        warn("mmap failing: arguments not page-aligned: "
9552544SN/A             "start 0x%x length 0x%x",
9562544SN/A             start, length);
9572544SN/A        return -EINVAL;
958360SN/A    }
959360SN/A
9602544SN/A    if (start != 0) {
9612544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
9622544SN/A             start, p->mmap_end);
9632544SN/A    }
9642544SN/A
9652544SN/A    // pick next address from our "mmap region"
9662544SN/A    start = p->mmap_end;
9672544SN/A    p->pTable->allocate(start, length);
9682544SN/A    p->mmap_end += length;
9692544SN/A
9702553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
9711969SN/A        warn("allowing mmap of file @ fd %d. "
9725958Sgblack@eecs.umich.edu             "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
973360SN/A    }
974360SN/A
9751458SN/A    return start;
976360SN/A}
977360SN/A
978378SN/A/// Target getrlimit() handler.
979360SN/Atemplate <class OS>
9801450SN/ASyscallReturn
9813114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9822680Sktlim@umich.edu        ThreadContext *tc)
983360SN/A{
9845958Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, 0);
9855958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
986360SN/A
987360SN/A    switch (resource) {
9882064SN/A        case OS::TGT_RLIMIT_STACK:
9895877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
9902064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
9912091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
9922091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
9932064SN/A            break;
994360SN/A
9955877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
9965877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
9975877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
9985877Shsul@eecs.umich.edu            rlp->rlim_cur = htog(rlp->rlim_cur);
9995877Shsul@eecs.umich.edu            rlp->rlim_max = htog(rlp->rlim_max);
10005877Shsul@eecs.umich.edu            break;
10015877Shsul@eecs.umich.edu
10022064SN/A        default:
10032064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
10042064SN/A                << std::endl;
10052064SN/A            abort();
10062064SN/A            break;
1007360SN/A    }
1008360SN/A
10092680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
10101458SN/A    return 0;
1011360SN/A}
1012360SN/A
1013378SN/A/// Target gettimeofday() handler.
1014360SN/Atemplate <class OS>
10151450SN/ASyscallReturn
10163114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10172680Sktlim@umich.edu        ThreadContext *tc)
1018360SN/A{
10195958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1020360SN/A
1021360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1022360SN/A    tp->tv_sec += seconds_since_epoch;
10236109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
10246109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1025360SN/A
10262680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
1027360SN/A
10281458SN/A    return 0;
1029360SN/A}
1030360SN/A
1031360SN/A
10321999SN/A/// Target utimes() handler.
10331999SN/Atemplate <class OS>
10341999SN/ASyscallReturn
10353114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10362680Sktlim@umich.edu           ThreadContext *tc)
10371999SN/A{
10381999SN/A    std::string path;
10391999SN/A
10405958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
10412400SN/A      return -EFAULT;
10421999SN/A
10435958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
10442680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
10451999SN/A
10461999SN/A    struct timeval hostTimeval[2];
10471999SN/A    for (int i = 0; i < 2; ++i)
10481999SN/A    {
10492091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
10502091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
10511999SN/A    }
10523669Sbinkertn@umich.edu
10533669Sbinkertn@umich.edu    // Adjust path for current working directory
10543669Sbinkertn@umich.edu    path = process->fullPath(path);
10553669Sbinkertn@umich.edu
10561999SN/A    int result = utimes(path.c_str(), hostTimeval);
10571999SN/A
10581999SN/A    if (result < 0)
10591999SN/A        return -errno;
10601999SN/A
10611999SN/A    return 0;
10621999SN/A}
1063378SN/A/// Target getrusage() function.
1064360SN/Atemplate <class OS>
10651450SN/ASyscallReturn
10663114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10672680Sktlim@umich.edu              ThreadContext *tc)
1068360SN/A{
10695958Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, 0);     // THREAD, SELF, or CHILDREN
10705958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1071360SN/A
10723670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
10733670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1074360SN/A    rup->ru_stime.tv_sec = 0;
1075360SN/A    rup->ru_stime.tv_usec = 0;
1076360SN/A    rup->ru_maxrss = 0;
1077360SN/A    rup->ru_ixrss = 0;
1078360SN/A    rup->ru_idrss = 0;
1079360SN/A    rup->ru_isrss = 0;
1080360SN/A    rup->ru_minflt = 0;
1081360SN/A    rup->ru_majflt = 0;
1082360SN/A    rup->ru_nswap = 0;
1083360SN/A    rup->ru_inblock = 0;
1084360SN/A    rup->ru_oublock = 0;
1085360SN/A    rup->ru_msgsnd = 0;
1086360SN/A    rup->ru_msgrcv = 0;
1087360SN/A    rup->ru_nsignals = 0;
1088360SN/A    rup->ru_nvcsw = 0;
1089360SN/A    rup->ru_nivcsw = 0;
1090360SN/A
10913670Sbinkertn@umich.edu    switch (who) {
10923670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
10933670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
10943670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
10953670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
10963670Sbinkertn@umich.edu        break;
10973670Sbinkertn@umich.edu
10983670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
10993670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
11003670Sbinkertn@umich.edu        break;
11013670Sbinkertn@umich.edu
11023670Sbinkertn@umich.edu      default:
11033670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
11043670Sbinkertn@umich.edu        // plow ahead
11053670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
11063670Sbinkertn@umich.edu             who);
11073670Sbinkertn@umich.edu    }
11083670Sbinkertn@umich.edu
11092680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1110360SN/A
11111458SN/A    return 0;
1112360SN/A}
1113360SN/A
11142553SN/A
11152553SN/A
11162553SN/A
11171354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1118