syscall_emul.hh revision 5074
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__
471809SN/A#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
533113Sgblack@eecs.umich.edu#include "sim/host.hh"	// for Addr
542474SN/A#include "base/chunk_generator.hh"
55360SN/A#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
75360SN/A    const char *name;	//!< Syscall name (e.g., "open").
76360SN/A    FuncPtr funcPtr;	//!< Pointer to emulation function.
77360SN/A    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);
120360SN/A        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);
129360SN/A        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) { }
143360SN/A    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
161502SN/A    T &operator*()	 { return *((T *)bufPtr); }
162360SN/A    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
186378SN/A/// Target exit() handler: terminate simulation.
1871706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1883114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
189378SN/A
190378SN/A/// Target getpagesize() handler.
1911706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1923114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
193378SN/A
194378SN/A/// Target obreak() handler: set brk address.
1951706SN/ASyscallReturn obreakFunc(SyscallDesc *desc, int num,
1963114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
197378SN/A
198378SN/A/// Target close() handler.
1991706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2003114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
201378SN/A
202378SN/A/// Target read() handler.
2031706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2043114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
205378SN/A
206378SN/A/// Target write() handler.
2071706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2083114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
209378SN/A
210378SN/A/// Target lseek() handler.
2111706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2123114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
213378SN/A
2144118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2154118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2164118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2174118Sgblack@eecs.umich.edu
218378SN/A/// Target munmap() handler.
2191706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2203114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
221378SN/A
222378SN/A/// Target gethostname() handler.
2231706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
225360SN/A
226511SN/A/// Target unlink() handler.
2271706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2283114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
229511SN/A
230511SN/A/// Target rename() handler.
2311706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2323114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2331706SN/A
2341706SN/A
2351706SN/A/// Target truncate() handler.
2361706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2373114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2381706SN/A
2391706SN/A
2401706SN/A/// Target ftruncate() handler.
2411706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2423114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2431706SN/A
244511SN/A
2451999SN/A/// Target chown() handler.
2461999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2473114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2481999SN/A
2491999SN/A
2501999SN/A/// Target fchown() handler.
2511999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2523114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2531999SN/A
2543079Sstever@eecs.umich.edu/// Target dup() handler.
2553079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2563114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2573079Sstever@eecs.umich.edu
2582093SN/A/// Target fnctl() handler.
2592093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2603114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2612093SN/A
2622687Sksewell@umich.edu/// Target fcntl64() handler.
2632687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2643114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2652687Sksewell@umich.edu
2662238SN/A/// Target setuid() handler.
2672238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2692238SN/A
2702238SN/A/// Target getpid() handler.
2712238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2723114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2732238SN/A
2742238SN/A/// Target getuid() handler.
2752238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2763114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2772238SN/A
2782238SN/A/// Target getgid() handler.
2792238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2812238SN/A
2822238SN/A/// Target getppid() handler.
2832238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2852238SN/A
2862238SN/A/// Target geteuid() handler.
2872238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2883114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2892238SN/A
2902238SN/A/// Target getegid() handler.
2912238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2923114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2932238SN/A
2942238SN/A
2952238SN/A
2962238SN/A/// Pseudo Funcs  - These functions use a different return convension,
2972238SN/A/// returning a second value in a register other than the normal return register
2982238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
2993114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3002238SN/A
3012238SN/A/// Target getpidPseudo() handler.
3022238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3033114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3042238SN/A
3052238SN/A/// Target getuidPseudo() handler.
3062238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3073114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3082238SN/A
3092238SN/A/// Target getgidPseudo() handler.
3102238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3113114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3122238SN/A
3132238SN/A
3141354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3151354SN/Aconst int one_million = 1000000;
3161354SN/A
3171354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3181354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3191354SN/A/// real-world time) to keep simulations repeatable.
3201354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3211354SN/A
3221354SN/A/// Helper function to convert current elapsed time to seconds and
3231354SN/A/// microseconds.
3241354SN/Atemplate <class T1, class T2>
3251354SN/Avoid
3261354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3271354SN/A{
3281609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3291354SN/A    sec = elapsed_usecs / one_million;
3301354SN/A    usec = elapsed_usecs % one_million;
3311354SN/A}
3321354SN/A
333360SN/A//////////////////////////////////////////////////////////////////////
334360SN/A//
335360SN/A// The following emulation functions are generic, but need to be
336360SN/A// templated to account for differences in types, constants, etc.
337360SN/A//
338360SN/A//////////////////////////////////////////////////////////////////////
339360SN/A
3403113Sgblack@eecs.umich.edu#if NO_STAT64
3413113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3423113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3433113Sgblack@eecs.umich.edu#else
3443113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3453113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3463113Sgblack@eecs.umich.edu#endif
3473113Sgblack@eecs.umich.edu
3483113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3493113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3503113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3513113Sgblack@eecs.umich.edu
3523113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3533113Sgblack@eecs.umich.edustatic void
3543113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3553113Sgblack@eecs.umich.edu{
3564189Sgblack@eecs.umich.edu    using namespace TheISA;
3574189Sgblack@eecs.umich.edu
3583113Sgblack@eecs.umich.edu    if (fakeTTY)
3593113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3603113Sgblack@eecs.umich.edu    else
3613113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3623113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3633113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3643113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3653277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
3663277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
3673277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
3683277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
3693277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
3703277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
3713277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
3723277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
3733113Sgblack@eecs.umich.edu    if (fakeTTY)
3743113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
3753113Sgblack@eecs.umich.edu    else
3763113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
3773113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
3783113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
3793113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
3803114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
3813113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
3823114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
3833113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
3843114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
3853113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
3864061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
3874061Sgblack@eecs.umich.edu    // consistently across different hosts.
3884061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
3893113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
3903113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
3913113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
3923113Sgblack@eecs.umich.edu}
3933113Sgblack@eecs.umich.edu
3943113Sgblack@eecs.umich.edu// Same for stat64
3953113Sgblack@eecs.umich.edu
3963113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
3973113Sgblack@eecs.umich.edustatic void
3983113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
3993113Sgblack@eecs.umich.edu{
4004189Sgblack@eecs.umich.edu    using namespace TheISA;
4014189Sgblack@eecs.umich.edu
4023113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4033113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4043113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4053113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4063113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4073113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4083113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4093113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4103113Sgblack@eecs.umich.edu#else
4113113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4123113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4133113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4143113Sgblack@eecs.umich.edu#endif
4153113Sgblack@eecs.umich.edu}
4163113Sgblack@eecs.umich.edu
4173113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4183113Sgblack@eecs.umich.edutemplate<class OS>
4193113Sgblack@eecs.umich.edustatic void
4203113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4213113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4223113Sgblack@eecs.umich.edu{
4233113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4243113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4253113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4263113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4273113Sgblack@eecs.umich.edu}
4283113Sgblack@eecs.umich.edu
4293113Sgblack@eecs.umich.edutemplate<class OS>
4303113Sgblack@eecs.umich.edustatic void
4313113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4323113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4333113Sgblack@eecs.umich.edu{
4343113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4353113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4363113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4373113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4383113Sgblack@eecs.umich.edu}
4393113Sgblack@eecs.umich.edu
440378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
441378SN/A/// only to find out if their stdout is a tty, to determine whether to
442378SN/A/// do line or block buffering.
443360SN/Atemplate <class OS>
4441450SN/ASyscallReturn
4453114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4462680Sktlim@umich.edu          ThreadContext *tc)
447360SN/A{
4482680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4492680Sktlim@umich.edu    unsigned req = tc->getSyscallArg(1);
450360SN/A
4511969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
452360SN/A
453360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
454360SN/A        // doesn't map to any simulator fd: not a valid target fd
4551458SN/A        return -EBADF;
456360SN/A    }
457360SN/A
458360SN/A    switch (req) {
4594131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
4604131Sbinkertn@umich.edu      case OS::TIOCGETP_:
4614131Sbinkertn@umich.edu      case OS::TIOCSETP_:
4624131Sbinkertn@umich.edu      case OS::TIOCSETN_:
4634131Sbinkertn@umich.edu      case OS::TIOCSETC_:
4644131Sbinkertn@umich.edu      case OS::TIOCGETC_:
4654131Sbinkertn@umich.edu      case OS::TIOCGETS_:
4664131Sbinkertn@umich.edu      case OS::TIOCGETA_:
4671458SN/A        return -ENOTTY;
468360SN/A
469360SN/A      default:
4701706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
4712680Sktlim@umich.edu              fd, req, tc->readPC());
472360SN/A    }
473360SN/A}
474360SN/A
475378SN/A/// Target open() handler.
476360SN/Atemplate <class OS>
4771450SN/ASyscallReturn
4783114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4792680Sktlim@umich.edu         ThreadContext *tc)
480360SN/A{
481360SN/A    std::string path;
482360SN/A
4832680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4841458SN/A        return -EFAULT;
485360SN/A
486360SN/A    if (path == "/dev/sysdev0") {
487360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
488360SN/A        // We don't support it, so just punt.
4891706SN/A        warn("Ignoring open(%s, ...)\n", path);
4901458SN/A        return -ENOENT;
491360SN/A    }
492360SN/A
4932680Sktlim@umich.edu    int tgtFlags = tc->getSyscallArg(1);
4942680Sktlim@umich.edu    int mode = tc->getSyscallArg(2);
495360SN/A    int hostFlags = 0;
496360SN/A
497360SN/A    // translate open flags
498360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
499360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
500360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
501360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
502360SN/A        }
503360SN/A    }
504360SN/A
505360SN/A    // any target flags left?
506360SN/A    if (tgtFlags != 0)
5071706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
508360SN/A
509360SN/A#ifdef __CYGWIN32__
510360SN/A    hostFlags |= O_BINARY;
511360SN/A#endif
512360SN/A
5133669Sbinkertn@umich.edu    // Adjust path for current working directory
5143669Sbinkertn@umich.edu    path = process->fullPath(path);
5153669Sbinkertn@umich.edu
5161706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5171706SN/A
518360SN/A    // open the file
519360SN/A    int fd = open(path.c_str(), hostFlags, mode);
520360SN/A
5211970SN/A    return (fd == -1) ? -errno : process->alloc_fd(fd);
522360SN/A}
523360SN/A
524360SN/A
5251999SN/A/// Target chmod() handler.
5261999SN/Atemplate <class OS>
5271999SN/ASyscallReturn
5283114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5292680Sktlim@umich.edu          ThreadContext *tc)
5301999SN/A{
5311999SN/A    std::string path;
5321999SN/A
5332680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5341999SN/A        return -EFAULT;
5351999SN/A
5362680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5371999SN/A    mode_t hostMode = 0;
5381999SN/A
5391999SN/A    // XXX translate mode flags via OS::something???
5401999SN/A    hostMode = mode;
5411999SN/A
5423669Sbinkertn@umich.edu    // Adjust path for current working directory
5433669Sbinkertn@umich.edu    path = process->fullPath(path);
5443669Sbinkertn@umich.edu
5451999SN/A    // do the chmod
5461999SN/A    int result = chmod(path.c_str(), hostMode);
5471999SN/A    if (result < 0)
5482218SN/A        return -errno;
5491999SN/A
5501999SN/A    return 0;
5511999SN/A}
5521999SN/A
5531999SN/A
5541999SN/A/// Target fchmod() handler.
5551999SN/Atemplate <class OS>
5561999SN/ASyscallReturn
5573114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5582680Sktlim@umich.edu           ThreadContext *tc)
5591999SN/A{
5602680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5611999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5621999SN/A        // doesn't map to any simulator fd: not a valid target fd
5631999SN/A        return -EBADF;
5641999SN/A    }
5651999SN/A
5662680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5671999SN/A    mode_t hostMode = 0;
5681999SN/A
5691999SN/A    // XXX translate mode flags via OS::someting???
5701999SN/A    hostMode = mode;
5711999SN/A
5721999SN/A    // do the fchmod
5731999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
5741999SN/A    if (result < 0)
5752218SN/A        return -errno;
5761999SN/A
5771999SN/A    return 0;
5781999SN/A}
5791999SN/A
5801999SN/A
581378SN/A/// Target stat() handler.
582360SN/Atemplate <class OS>
5831450SN/ASyscallReturn
5843114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5852680Sktlim@umich.edu         ThreadContext *tc)
586360SN/A{
587360SN/A    std::string path;
588360SN/A
5892680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5902400SN/A    return -EFAULT;
591360SN/A
5923669Sbinkertn@umich.edu    // Adjust path for current working directory
5933669Sbinkertn@umich.edu    path = process->fullPath(path);
5943669Sbinkertn@umich.edu
595360SN/A    struct stat hostBuf;
596360SN/A    int result = stat(path.c_str(), &hostBuf);
597360SN/A
598360SN/A    if (result < 0)
5992218SN/A        return -errno;
600360SN/A
6013113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
602360SN/A
6031458SN/A    return 0;
604360SN/A}
605360SN/A
606360SN/A
6075074Ssaidi@eecs.umich.edu/// Target stat64() handler.
6085074Ssaidi@eecs.umich.edutemplate <class OS>
6095074Ssaidi@eecs.umich.eduSyscallReturn
6105074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6115074Ssaidi@eecs.umich.edu           ThreadContext *tc)
6125074Ssaidi@eecs.umich.edu{
6135074Ssaidi@eecs.umich.edu    std::string path;
6145074Ssaidi@eecs.umich.edu
6155074Ssaidi@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6165074Ssaidi@eecs.umich.edu        return -EFAULT;
6175074Ssaidi@eecs.umich.edu
6185074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
6195074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
6205074Ssaidi@eecs.umich.edu
6215074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
6225074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
6235074Ssaidi@eecs.umich.edu
6245074Ssaidi@eecs.umich.edu    if (result < 0)
6255074Ssaidi@eecs.umich.edu        return -errno;
6265074Ssaidi@eecs.umich.edu
6275074Ssaidi@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6285074Ssaidi@eecs.umich.edu
6295074Ssaidi@eecs.umich.edu    return 0;
6305074Ssaidi@eecs.umich.edu}
6315074Ssaidi@eecs.umich.edu
6325074Ssaidi@eecs.umich.edu
6331999SN/A/// Target fstat64() handler.
6341999SN/Atemplate <class OS>
6351999SN/ASyscallReturn
6363114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6372680Sktlim@umich.edu            ThreadContext *tc)
6381999SN/A{
6392680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
6401999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6411999SN/A        // doesn't map to any simulator fd: not a valid target fd
6421999SN/A        return -EBADF;
6431999SN/A    }
6441999SN/A
6452764Sstever@eecs.umich.edu#if NO_STAT64
6462064SN/A    struct stat  hostBuf;
6472064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
6482064SN/A#else
6492064SN/A    struct stat64  hostBuf;
6501999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
6512064SN/A#endif
6521999SN/A
6531999SN/A    if (result < 0)
6542218SN/A        return -errno;
6551999SN/A
6563114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
6573114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
6581999SN/A
6591999SN/A    return 0;
6601999SN/A}
6611999SN/A
6621999SN/A
663378SN/A/// Target lstat() handler.
664360SN/Atemplate <class OS>
6651450SN/ASyscallReturn
6663114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6672680Sktlim@umich.edu          ThreadContext *tc)
668360SN/A{
669360SN/A    std::string path;
670360SN/A
6712680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(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 = lstat(path.c_str(), &hostBuf);
679360SN/A
680360SN/A    if (result < 0)
6811458SN/A        return -errno;
682360SN/A
6833113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
684360SN/A
6851458SN/A    return 0;
686360SN/A}
687360SN/A
6881999SN/A/// Target lstat64() handler.
6891999SN/Atemplate <class OS>
6901999SN/ASyscallReturn
6913114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6922680Sktlim@umich.edu            ThreadContext *tc)
6931999SN/A{
6941999SN/A    std::string path;
6951999SN/A
6962680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6972400SN/A      return -EFAULT;
6981999SN/A
6993669Sbinkertn@umich.edu    // Adjust path for current working directory
7003669Sbinkertn@umich.edu    path = process->fullPath(path);
7013669Sbinkertn@umich.edu
7022764Sstever@eecs.umich.edu#if NO_STAT64
7032064SN/A    struct stat hostBuf;
7042064SN/A    int result = lstat(path.c_str(), &hostBuf);
7052064SN/A#else
7061999SN/A    struct stat64 hostBuf;
7071999SN/A    int result = lstat64(path.c_str(), &hostBuf);
7082064SN/A#endif
7091999SN/A
7101999SN/A    if (result < 0)
7111999SN/A        return -errno;
7121999SN/A
7133114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
7141999SN/A
7151999SN/A    return 0;
7161999SN/A}
7171999SN/A
718378SN/A/// Target fstat() handler.
719360SN/Atemplate <class OS>
7201450SN/ASyscallReturn
7213114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7222680Sktlim@umich.edu          ThreadContext *tc)
723360SN/A{
7242680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
725360SN/A
7261969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
727360SN/A
728360SN/A    if (fd < 0)
7291458SN/A        return -EBADF;
730360SN/A
731360SN/A    struct stat hostBuf;
732360SN/A    int result = fstat(fd, &hostBuf);
733360SN/A
734360SN/A    if (result < 0)
7351458SN/A        return -errno;
736360SN/A
7373114Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
7383114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7392021SN/A
7401458SN/A    return 0;
741360SN/A}
742360SN/A
743360SN/A
7441706SN/A/// Target statfs() handler.
7451706SN/Atemplate <class OS>
7461706SN/ASyscallReturn
7473114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7482680Sktlim@umich.edu           ThreadContext *tc)
7491706SN/A{
7501706SN/A    std::string path;
7511706SN/A
7522680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7532400SN/A      return -EFAULT;
7541706SN/A
7553669Sbinkertn@umich.edu    // Adjust path for current working directory
7563669Sbinkertn@umich.edu    path = process->fullPath(path);
7573669Sbinkertn@umich.edu
7581706SN/A    struct statfs hostBuf;
7591706SN/A    int result = statfs(path.c_str(), &hostBuf);
7601706SN/A
7611706SN/A    if (result < 0)
7622218SN/A        return -errno;
7631706SN/A
7643114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
7653114Sgblack@eecs.umich.edu            (Addr)(tc->getSyscallArg(1)), &hostBuf);
7661706SN/A
7671706SN/A    return 0;
7681706SN/A}
7691706SN/A
7701706SN/A
7711706SN/A/// Target fstatfs() handler.
7721706SN/Atemplate <class OS>
7731706SN/ASyscallReturn
7743114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7752680Sktlim@umich.edu            ThreadContext *tc)
7761706SN/A{
7772680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
7781706SN/A
7791706SN/A    if (fd < 0)
7801706SN/A        return -EBADF;
7811706SN/A
7821706SN/A    struct statfs hostBuf;
7831706SN/A    int result = fstatfs(fd, &hostBuf);
7841706SN/A
7851706SN/A    if (result < 0)
7862218SN/A        return -errno;
7871706SN/A
7883114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1),
7893114Sgblack@eecs.umich.edu        &hostBuf);
7901706SN/A
7911706SN/A    return 0;
7921706SN/A}
7931706SN/A
7941706SN/A
7951999SN/A/// Target writev() handler.
7961999SN/Atemplate <class OS>
7971999SN/ASyscallReturn
7983114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7992680Sktlim@umich.edu           ThreadContext *tc)
8001999SN/A{
8012680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
8021999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8031999SN/A        // doesn't map to any simulator fd: not a valid target fd
8041999SN/A        return -EBADF;
8051999SN/A    }
8061999SN/A
8072680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
8082680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
8092680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
8101999SN/A    struct iovec hiov[count];
8111999SN/A    for (int i = 0; i < count; ++i)
8121999SN/A    {
8131999SN/A        typename OS::tgt_iovec tiov;
8142461SN/A
8152461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
8162461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
8172091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
8181999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
8192461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
8202461SN/A                    hiov[i].iov_len);
8211999SN/A    }
8221999SN/A
8231999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
8241999SN/A
8251999SN/A    for (int i = 0; i < count; ++i)
8261999SN/A    {
8271999SN/A        delete [] (char *)hiov[i].iov_base;
8281999SN/A    }
8291999SN/A
8301999SN/A    if (result < 0)
8312218SN/A        return -errno;
8321999SN/A
8331999SN/A    return 0;
8341999SN/A}
8351999SN/A
8361999SN/A
837378SN/A/// Target mmap() handler.
838378SN/A///
839378SN/A/// We don't really handle mmap().  If the target is mmaping an
840378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
841378SN/A/// nothing (since memory is initialized to zero and the simulator
842378SN/A/// doesn't really check addresses anyway).  Always print a warning,
843378SN/A/// since this could be seriously broken if we're not mapping
844378SN/A/// /dev/zero.
845360SN/A//
846378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
847378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
848378SN/A/// anything else.
849360SN/Atemplate <class OS>
8501450SN/ASyscallReturn
8513114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
852360SN/A{
8532680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
8542680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
8552680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
8562680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
8572680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
8582680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
859360SN/A
8602544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
8612544SN/A        (length % TheISA::VMPageSize) != 0) {
8622544SN/A        warn("mmap failing: arguments not page-aligned: "
8632544SN/A             "start 0x%x length 0x%x",
8642544SN/A             start, length);
8652544SN/A        return -EINVAL;
866360SN/A    }
867360SN/A
8682544SN/A    if (start != 0) {
8692544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
8702544SN/A             start, p->mmap_end);
8712544SN/A    }
8722544SN/A
8732544SN/A    // pick next address from our "mmap region"
8742544SN/A    start = p->mmap_end;
8752544SN/A    p->pTable->allocate(start, length);
8762544SN/A    p->mmap_end += length;
8772544SN/A
8782553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
8791969SN/A        warn("allowing mmap of file @ fd %d. "
8802680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
881360SN/A    }
882360SN/A
8831458SN/A    return start;
884360SN/A}
885360SN/A
886378SN/A/// Target getrlimit() handler.
887360SN/Atemplate <class OS>
8881450SN/ASyscallReturn
8893114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8902680Sktlim@umich.edu        ThreadContext *tc)
891360SN/A{
8922680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
8932680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
894360SN/A
895360SN/A    switch (resource) {
8962064SN/A        case OS::TGT_RLIMIT_STACK:
8972064SN/A            // max stack size in bytes: make up a number (2MB for now)
8982064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
8992091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
9002091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
9012064SN/A            break;
902360SN/A
9032064SN/A        default:
9042064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
9052064SN/A                << std::endl;
9062064SN/A            abort();
9072064SN/A            break;
908360SN/A    }
909360SN/A
9102680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
9111458SN/A    return 0;
912360SN/A}
913360SN/A
914378SN/A/// Target gettimeofday() handler.
915360SN/Atemplate <class OS>
9161450SN/ASyscallReturn
9173114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9182680Sktlim@umich.edu        ThreadContext *tc)
919360SN/A{
9202680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
921360SN/A
922360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
923360SN/A    tp->tv_sec += seconds_since_epoch;
9242091SN/A    tp->tv_sec = htog(tp->tv_sec);
9252091SN/A    tp->tv_usec = htog(tp->tv_usec);
926360SN/A
9272680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
928360SN/A
9291458SN/A    return 0;
930360SN/A}
931360SN/A
932360SN/A
9331999SN/A/// Target utimes() handler.
9341999SN/Atemplate <class OS>
9351999SN/ASyscallReturn
9363114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9372680Sktlim@umich.edu           ThreadContext *tc)
9381999SN/A{
9391999SN/A    std::string path;
9401999SN/A
9412680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
9422400SN/A      return -EFAULT;
9431999SN/A
9442680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
9452680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
9461999SN/A
9471999SN/A    struct timeval hostTimeval[2];
9481999SN/A    for (int i = 0; i < 2; ++i)
9491999SN/A    {
9502091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
9512091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
9521999SN/A    }
9533669Sbinkertn@umich.edu
9543669Sbinkertn@umich.edu    // Adjust path for current working directory
9553669Sbinkertn@umich.edu    path = process->fullPath(path);
9563669Sbinkertn@umich.edu
9571999SN/A    int result = utimes(path.c_str(), hostTimeval);
9581999SN/A
9591999SN/A    if (result < 0)
9601999SN/A        return -errno;
9611999SN/A
9621999SN/A    return 0;
9631999SN/A}
964378SN/A/// Target getrusage() function.
965360SN/Atemplate <class OS>
9661450SN/ASyscallReturn
9673114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9682680Sktlim@umich.edu              ThreadContext *tc)
969360SN/A{
9702680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
9712680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
972360SN/A
9733670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
9743670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
975360SN/A    rup->ru_stime.tv_sec = 0;
976360SN/A    rup->ru_stime.tv_usec = 0;
977360SN/A    rup->ru_maxrss = 0;
978360SN/A    rup->ru_ixrss = 0;
979360SN/A    rup->ru_idrss = 0;
980360SN/A    rup->ru_isrss = 0;
981360SN/A    rup->ru_minflt = 0;
982360SN/A    rup->ru_majflt = 0;
983360SN/A    rup->ru_nswap = 0;
984360SN/A    rup->ru_inblock = 0;
985360SN/A    rup->ru_oublock = 0;
986360SN/A    rup->ru_msgsnd = 0;
987360SN/A    rup->ru_msgrcv = 0;
988360SN/A    rup->ru_nsignals = 0;
989360SN/A    rup->ru_nvcsw = 0;
990360SN/A    rup->ru_nivcsw = 0;
991360SN/A
9923670Sbinkertn@umich.edu    switch (who) {
9933670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
9943670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
9953670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
9963670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
9973670Sbinkertn@umich.edu        break;
9983670Sbinkertn@umich.edu
9993670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
10003670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
10013670Sbinkertn@umich.edu        break;
10023670Sbinkertn@umich.edu
10033670Sbinkertn@umich.edu      default:
10043670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
10053670Sbinkertn@umich.edu        // plow ahead
10063670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
10073670Sbinkertn@umich.edu             who);
10083670Sbinkertn@umich.edu    }
10093670Sbinkertn@umich.edu
10102680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1011360SN/A
10121458SN/A    return 0;
1013360SN/A}
1014360SN/A
10152553SN/A
10162553SN/A
10172553SN/A
10181354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1019