syscall_emul.hh revision 5208
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
6215208Ssaidi@eecs.umich.edu#if NO_STAT64
6225208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
6235208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
6245208Ssaidi@eecs.umich.edu#else
6255074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
6265074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
6275208Ssaidi@eecs.umich.edu#endif
6285074Ssaidi@eecs.umich.edu
6295074Ssaidi@eecs.umich.edu    if (result < 0)
6305074Ssaidi@eecs.umich.edu        return -errno;
6315074Ssaidi@eecs.umich.edu
6325074Ssaidi@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6335074Ssaidi@eecs.umich.edu
6345074Ssaidi@eecs.umich.edu    return 0;
6355074Ssaidi@eecs.umich.edu}
6365074Ssaidi@eecs.umich.edu
6375074Ssaidi@eecs.umich.edu
6381999SN/A/// Target fstat64() handler.
6391999SN/Atemplate <class OS>
6401999SN/ASyscallReturn
6413114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6422680Sktlim@umich.edu            ThreadContext *tc)
6431999SN/A{
6442680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
6451999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6461999SN/A        // doesn't map to any simulator fd: not a valid target fd
6471999SN/A        return -EBADF;
6481999SN/A    }
6491999SN/A
6502764Sstever@eecs.umich.edu#if NO_STAT64
6512064SN/A    struct stat  hostBuf;
6522064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
6532064SN/A#else
6542064SN/A    struct stat64  hostBuf;
6551999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
6562064SN/A#endif
6571999SN/A
6581999SN/A    if (result < 0)
6592218SN/A        return -errno;
6601999SN/A
6613114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
6623114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
6631999SN/A
6641999SN/A    return 0;
6651999SN/A}
6661999SN/A
6671999SN/A
668378SN/A/// Target lstat() handler.
669360SN/Atemplate <class OS>
6701450SN/ASyscallReturn
6713114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6722680Sktlim@umich.edu          ThreadContext *tc)
673360SN/A{
674360SN/A    std::string path;
675360SN/A
6762680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6772400SN/A      return -EFAULT;
678360SN/A
6793669Sbinkertn@umich.edu    // Adjust path for current working directory
6803669Sbinkertn@umich.edu    path = process->fullPath(path);
6813669Sbinkertn@umich.edu
682360SN/A    struct stat hostBuf;
683360SN/A    int result = lstat(path.c_str(), &hostBuf);
684360SN/A
685360SN/A    if (result < 0)
6861458SN/A        return -errno;
687360SN/A
6883113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
689360SN/A
6901458SN/A    return 0;
691360SN/A}
692360SN/A
6931999SN/A/// Target lstat64() handler.
6941999SN/Atemplate <class OS>
6951999SN/ASyscallReturn
6963114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6972680Sktlim@umich.edu            ThreadContext *tc)
6981999SN/A{
6991999SN/A    std::string path;
7001999SN/A
7012680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7022400SN/A      return -EFAULT;
7031999SN/A
7043669Sbinkertn@umich.edu    // Adjust path for current working directory
7053669Sbinkertn@umich.edu    path = process->fullPath(path);
7063669Sbinkertn@umich.edu
7072764Sstever@eecs.umich.edu#if NO_STAT64
7082064SN/A    struct stat hostBuf;
7092064SN/A    int result = lstat(path.c_str(), &hostBuf);
7102064SN/A#else
7111999SN/A    struct stat64 hostBuf;
7121999SN/A    int result = lstat64(path.c_str(), &hostBuf);
7132064SN/A#endif
7141999SN/A
7151999SN/A    if (result < 0)
7161999SN/A        return -errno;
7171999SN/A
7183114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
7191999SN/A
7201999SN/A    return 0;
7211999SN/A}
7221999SN/A
723378SN/A/// Target fstat() handler.
724360SN/Atemplate <class OS>
7251450SN/ASyscallReturn
7263114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7272680Sktlim@umich.edu          ThreadContext *tc)
728360SN/A{
7292680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
730360SN/A
7311969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
732360SN/A
733360SN/A    if (fd < 0)
7341458SN/A        return -EBADF;
735360SN/A
736360SN/A    struct stat hostBuf;
737360SN/A    int result = fstat(fd, &hostBuf);
738360SN/A
739360SN/A    if (result < 0)
7401458SN/A        return -errno;
741360SN/A
7423114Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
7433114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7442021SN/A
7451458SN/A    return 0;
746360SN/A}
747360SN/A
748360SN/A
7491706SN/A/// Target statfs() handler.
7501706SN/Atemplate <class OS>
7511706SN/ASyscallReturn
7523114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7532680Sktlim@umich.edu           ThreadContext *tc)
7541706SN/A{
7551706SN/A    std::string path;
7561706SN/A
7572680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7582400SN/A      return -EFAULT;
7591706SN/A
7603669Sbinkertn@umich.edu    // Adjust path for current working directory
7613669Sbinkertn@umich.edu    path = process->fullPath(path);
7623669Sbinkertn@umich.edu
7631706SN/A    struct statfs hostBuf;
7641706SN/A    int result = statfs(path.c_str(), &hostBuf);
7651706SN/A
7661706SN/A    if (result < 0)
7672218SN/A        return -errno;
7681706SN/A
7693114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
7703114Sgblack@eecs.umich.edu            (Addr)(tc->getSyscallArg(1)), &hostBuf);
7711706SN/A
7721706SN/A    return 0;
7731706SN/A}
7741706SN/A
7751706SN/A
7761706SN/A/// Target fstatfs() handler.
7771706SN/Atemplate <class OS>
7781706SN/ASyscallReturn
7793114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7802680Sktlim@umich.edu            ThreadContext *tc)
7811706SN/A{
7822680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
7831706SN/A
7841706SN/A    if (fd < 0)
7851706SN/A        return -EBADF;
7861706SN/A
7871706SN/A    struct statfs hostBuf;
7881706SN/A    int result = fstatfs(fd, &hostBuf);
7891706SN/A
7901706SN/A    if (result < 0)
7912218SN/A        return -errno;
7921706SN/A
7933114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1),
7943114Sgblack@eecs.umich.edu        &hostBuf);
7951706SN/A
7961706SN/A    return 0;
7971706SN/A}
7981706SN/A
7991706SN/A
8001999SN/A/// Target writev() handler.
8011999SN/Atemplate <class OS>
8021999SN/ASyscallReturn
8033114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8042680Sktlim@umich.edu           ThreadContext *tc)
8051999SN/A{
8062680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
8071999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8081999SN/A        // doesn't map to any simulator fd: not a valid target fd
8091999SN/A        return -EBADF;
8101999SN/A    }
8111999SN/A
8122680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
8132680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
8142680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
8151999SN/A    struct iovec hiov[count];
8161999SN/A    for (int i = 0; i < count; ++i)
8171999SN/A    {
8181999SN/A        typename OS::tgt_iovec tiov;
8192461SN/A
8202461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
8212461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
8222091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
8231999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
8242461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
8252461SN/A                    hiov[i].iov_len);
8261999SN/A    }
8271999SN/A
8281999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
8291999SN/A
8301999SN/A    for (int i = 0; i < count; ++i)
8311999SN/A    {
8321999SN/A        delete [] (char *)hiov[i].iov_base;
8331999SN/A    }
8341999SN/A
8351999SN/A    if (result < 0)
8362218SN/A        return -errno;
8371999SN/A
8381999SN/A    return 0;
8391999SN/A}
8401999SN/A
8411999SN/A
842378SN/A/// Target mmap() handler.
843378SN/A///
844378SN/A/// We don't really handle mmap().  If the target is mmaping an
845378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
846378SN/A/// nothing (since memory is initialized to zero and the simulator
847378SN/A/// doesn't really check addresses anyway).  Always print a warning,
848378SN/A/// since this could be seriously broken if we're not mapping
849378SN/A/// /dev/zero.
850360SN/A//
851378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
852378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
853378SN/A/// anything else.
854360SN/Atemplate <class OS>
8551450SN/ASyscallReturn
8563114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
857360SN/A{
8582680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
8592680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
8602680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
8612680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
8622680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
8632680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
864360SN/A
8652544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
8662544SN/A        (length % TheISA::VMPageSize) != 0) {
8672544SN/A        warn("mmap failing: arguments not page-aligned: "
8682544SN/A             "start 0x%x length 0x%x",
8692544SN/A             start, length);
8702544SN/A        return -EINVAL;
871360SN/A    }
872360SN/A
8732544SN/A    if (start != 0) {
8742544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
8752544SN/A             start, p->mmap_end);
8762544SN/A    }
8772544SN/A
8782544SN/A    // pick next address from our "mmap region"
8792544SN/A    start = p->mmap_end;
8802544SN/A    p->pTable->allocate(start, length);
8812544SN/A    p->mmap_end += length;
8822544SN/A
8832553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
8841969SN/A        warn("allowing mmap of file @ fd %d. "
8852680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
886360SN/A    }
887360SN/A
8881458SN/A    return start;
889360SN/A}
890360SN/A
891378SN/A/// Target getrlimit() handler.
892360SN/Atemplate <class OS>
8931450SN/ASyscallReturn
8943114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8952680Sktlim@umich.edu        ThreadContext *tc)
896360SN/A{
8972680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
8982680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
899360SN/A
900360SN/A    switch (resource) {
9012064SN/A        case OS::TGT_RLIMIT_STACK:
9022064SN/A            // max stack size in bytes: make up a number (2MB for now)
9032064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
9042091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
9052091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
9062064SN/A            break;
907360SN/A
9082064SN/A        default:
9092064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
9102064SN/A                << std::endl;
9112064SN/A            abort();
9122064SN/A            break;
913360SN/A    }
914360SN/A
9152680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
9161458SN/A    return 0;
917360SN/A}
918360SN/A
919378SN/A/// Target gettimeofday() handler.
920360SN/Atemplate <class OS>
9211450SN/ASyscallReturn
9223114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9232680Sktlim@umich.edu        ThreadContext *tc)
924360SN/A{
9252680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
926360SN/A
927360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
928360SN/A    tp->tv_sec += seconds_since_epoch;
9292091SN/A    tp->tv_sec = htog(tp->tv_sec);
9302091SN/A    tp->tv_usec = htog(tp->tv_usec);
931360SN/A
9322680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
933360SN/A
9341458SN/A    return 0;
935360SN/A}
936360SN/A
937360SN/A
9381999SN/A/// Target utimes() handler.
9391999SN/Atemplate <class OS>
9401999SN/ASyscallReturn
9413114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9422680Sktlim@umich.edu           ThreadContext *tc)
9431999SN/A{
9441999SN/A    std::string path;
9451999SN/A
9462680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
9472400SN/A      return -EFAULT;
9481999SN/A
9492680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
9502680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
9511999SN/A
9521999SN/A    struct timeval hostTimeval[2];
9531999SN/A    for (int i = 0; i < 2; ++i)
9541999SN/A    {
9552091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
9562091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
9571999SN/A    }
9583669Sbinkertn@umich.edu
9593669Sbinkertn@umich.edu    // Adjust path for current working directory
9603669Sbinkertn@umich.edu    path = process->fullPath(path);
9613669Sbinkertn@umich.edu
9621999SN/A    int result = utimes(path.c_str(), hostTimeval);
9631999SN/A
9641999SN/A    if (result < 0)
9651999SN/A        return -errno;
9661999SN/A
9671999SN/A    return 0;
9681999SN/A}
969378SN/A/// Target getrusage() function.
970360SN/Atemplate <class OS>
9711450SN/ASyscallReturn
9723114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9732680Sktlim@umich.edu              ThreadContext *tc)
974360SN/A{
9752680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
9762680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
977360SN/A
9783670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
9793670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
980360SN/A    rup->ru_stime.tv_sec = 0;
981360SN/A    rup->ru_stime.tv_usec = 0;
982360SN/A    rup->ru_maxrss = 0;
983360SN/A    rup->ru_ixrss = 0;
984360SN/A    rup->ru_idrss = 0;
985360SN/A    rup->ru_isrss = 0;
986360SN/A    rup->ru_minflt = 0;
987360SN/A    rup->ru_majflt = 0;
988360SN/A    rup->ru_nswap = 0;
989360SN/A    rup->ru_inblock = 0;
990360SN/A    rup->ru_oublock = 0;
991360SN/A    rup->ru_msgsnd = 0;
992360SN/A    rup->ru_msgrcv = 0;
993360SN/A    rup->ru_nsignals = 0;
994360SN/A    rup->ru_nvcsw = 0;
995360SN/A    rup->ru_nivcsw = 0;
996360SN/A
9973670Sbinkertn@umich.edu    switch (who) {
9983670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
9993670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
10003670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
10013670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
10023670Sbinkertn@umich.edu        break;
10033670Sbinkertn@umich.edu
10043670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
10053670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
10063670Sbinkertn@umich.edu        break;
10073670Sbinkertn@umich.edu
10083670Sbinkertn@umich.edu      default:
10093670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
10103670Sbinkertn@umich.edu        // plow ahead
10113670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
10123670Sbinkertn@umich.edu             who);
10133670Sbinkertn@umich.edu    }
10143670Sbinkertn@umich.edu
10152680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1016360SN/A
10171458SN/A    return 0;
1018360SN/A}
1019360SN/A
10202553SN/A
10212553SN/A
10222553SN/A
10231354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1024