syscall_emul.hh revision 4118
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{
3563113Sgblack@eecs.umich.edu    if (fakeTTY)
3573113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3583113Sgblack@eecs.umich.edu    else
3593113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3603113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3613113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3623113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3633277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
3643277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
3653277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
3663277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
3673277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
3683277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
3693277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
3703277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
3713113Sgblack@eecs.umich.edu    if (fakeTTY)
3723113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
3733113Sgblack@eecs.umich.edu    else
3743113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
3753113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
3763113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
3773113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
3783114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
3793113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
3803114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
3813113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
3823114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
3833113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
3844061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
3854061Sgblack@eecs.umich.edu    // consistently across different hosts.
3864061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
3873113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
3883113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
3893113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
3903113Sgblack@eecs.umich.edu}
3913113Sgblack@eecs.umich.edu
3923113Sgblack@eecs.umich.edu// Same for stat64
3933113Sgblack@eecs.umich.edu
3943113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
3953113Sgblack@eecs.umich.edustatic void
3963113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
3973113Sgblack@eecs.umich.edu{
3983113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
3993113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4003113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4013113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4023113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4033113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4043113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4053113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4063113Sgblack@eecs.umich.edu#else
4073113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4083113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4093113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4103113Sgblack@eecs.umich.edu#endif
4113113Sgblack@eecs.umich.edu}
4123113Sgblack@eecs.umich.edu
4133113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4143113Sgblack@eecs.umich.edutemplate<class OS>
4153113Sgblack@eecs.umich.edustatic void
4163113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4173113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4183113Sgblack@eecs.umich.edu{
4193113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4203113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4213113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4223113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4233113Sgblack@eecs.umich.edu}
4243113Sgblack@eecs.umich.edu
4253113Sgblack@eecs.umich.edutemplate<class OS>
4263113Sgblack@eecs.umich.edustatic void
4273113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4283113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4293113Sgblack@eecs.umich.edu{
4303113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4313113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4323113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4333113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4343113Sgblack@eecs.umich.edu}
4353113Sgblack@eecs.umich.edu
436378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
437378SN/A/// only to find out if their stdout is a tty, to determine whether to
438378SN/A/// do line or block buffering.
439360SN/Atemplate <class OS>
4401450SN/ASyscallReturn
4413114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4422680Sktlim@umich.edu          ThreadContext *tc)
443360SN/A{
4442680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4452680Sktlim@umich.edu    unsigned req = tc->getSyscallArg(1);
446360SN/A
4471969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
448360SN/A
449360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
450360SN/A        // doesn't map to any simulator fd: not a valid target fd
4511458SN/A        return -EBADF;
452360SN/A    }
453360SN/A
454360SN/A    switch (req) {
4552553SN/A      case OS::TIOCISATTY:
4562553SN/A      case OS::TIOCGETP:
4572553SN/A      case OS::TIOCSETP:
4582553SN/A      case OS::TIOCSETN:
4592553SN/A      case OS::TIOCSETC:
4602553SN/A      case OS::TIOCGETC:
4612553SN/A      case OS::TIOCGETS:
4622553SN/A      case OS::TIOCGETA:
4631458SN/A        return -ENOTTY;
464360SN/A
465360SN/A      default:
4661706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
4672680Sktlim@umich.edu              fd, req, tc->readPC());
468360SN/A    }
469360SN/A}
470360SN/A
471378SN/A/// Target open() handler.
472360SN/Atemplate <class OS>
4731450SN/ASyscallReturn
4743114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4752680Sktlim@umich.edu         ThreadContext *tc)
476360SN/A{
477360SN/A    std::string path;
478360SN/A
4792680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4801458SN/A        return -EFAULT;
481360SN/A
482360SN/A    if (path == "/dev/sysdev0") {
483360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
484360SN/A        // We don't support it, so just punt.
4851706SN/A        warn("Ignoring open(%s, ...)\n", path);
4861458SN/A        return -ENOENT;
487360SN/A    }
488360SN/A
4892680Sktlim@umich.edu    int tgtFlags = tc->getSyscallArg(1);
4902680Sktlim@umich.edu    int mode = tc->getSyscallArg(2);
491360SN/A    int hostFlags = 0;
492360SN/A
493360SN/A    // translate open flags
494360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
495360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
496360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
497360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
498360SN/A        }
499360SN/A    }
500360SN/A
501360SN/A    // any target flags left?
502360SN/A    if (tgtFlags != 0)
5031706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
504360SN/A
505360SN/A#ifdef __CYGWIN32__
506360SN/A    hostFlags |= O_BINARY;
507360SN/A#endif
508360SN/A
5093669Sbinkertn@umich.edu    // Adjust path for current working directory
5103669Sbinkertn@umich.edu    path = process->fullPath(path);
5113669Sbinkertn@umich.edu
5121706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5131706SN/A
514360SN/A    // open the file
515360SN/A    int fd = open(path.c_str(), hostFlags, mode);
516360SN/A
5171970SN/A    return (fd == -1) ? -errno : process->alloc_fd(fd);
518360SN/A}
519360SN/A
520360SN/A
5211999SN/A/// Target chmod() handler.
5221999SN/Atemplate <class OS>
5231999SN/ASyscallReturn
5243114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5252680Sktlim@umich.edu          ThreadContext *tc)
5261999SN/A{
5271999SN/A    std::string path;
5281999SN/A
5292680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5301999SN/A        return -EFAULT;
5311999SN/A
5322680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5331999SN/A    mode_t hostMode = 0;
5341999SN/A
5351999SN/A    // XXX translate mode flags via OS::something???
5361999SN/A    hostMode = mode;
5371999SN/A
5383669Sbinkertn@umich.edu    // Adjust path for current working directory
5393669Sbinkertn@umich.edu    path = process->fullPath(path);
5403669Sbinkertn@umich.edu
5411999SN/A    // do the chmod
5421999SN/A    int result = chmod(path.c_str(), hostMode);
5431999SN/A    if (result < 0)
5442218SN/A        return -errno;
5451999SN/A
5461999SN/A    return 0;
5471999SN/A}
5481999SN/A
5491999SN/A
5501999SN/A/// Target fchmod() handler.
5511999SN/Atemplate <class OS>
5521999SN/ASyscallReturn
5533114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5542680Sktlim@umich.edu           ThreadContext *tc)
5551999SN/A{
5562680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5571999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5581999SN/A        // doesn't map to any simulator fd: not a valid target fd
5591999SN/A        return -EBADF;
5601999SN/A    }
5611999SN/A
5622680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5631999SN/A    mode_t hostMode = 0;
5641999SN/A
5651999SN/A    // XXX translate mode flags via OS::someting???
5661999SN/A    hostMode = mode;
5671999SN/A
5681999SN/A    // do the fchmod
5691999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
5701999SN/A    if (result < 0)
5712218SN/A        return -errno;
5721999SN/A
5731999SN/A    return 0;
5741999SN/A}
5751999SN/A
5761999SN/A
577378SN/A/// Target stat() handler.
578360SN/Atemplate <class OS>
5791450SN/ASyscallReturn
5803114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5812680Sktlim@umich.edu         ThreadContext *tc)
582360SN/A{
583360SN/A    std::string path;
584360SN/A
5852680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5862400SN/A    return -EFAULT;
587360SN/A
5883669Sbinkertn@umich.edu    // Adjust path for current working directory
5893669Sbinkertn@umich.edu    path = process->fullPath(path);
5903669Sbinkertn@umich.edu
591360SN/A    struct stat hostBuf;
592360SN/A    int result = stat(path.c_str(), &hostBuf);
593360SN/A
594360SN/A    if (result < 0)
5952218SN/A        return -errno;
596360SN/A
5973113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
598360SN/A
5991458SN/A    return 0;
600360SN/A}
601360SN/A
602360SN/A
6031999SN/A/// Target fstat64() handler.
6041999SN/Atemplate <class OS>
6051999SN/ASyscallReturn
6063114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6072680Sktlim@umich.edu            ThreadContext *tc)
6081999SN/A{
6092680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
6101999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6111999SN/A        // doesn't map to any simulator fd: not a valid target fd
6121999SN/A        return -EBADF;
6131999SN/A    }
6141999SN/A
6152764Sstever@eecs.umich.edu#if NO_STAT64
6162064SN/A    struct stat  hostBuf;
6172064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
6182064SN/A#else
6192064SN/A    struct stat64  hostBuf;
6201999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
6212064SN/A#endif
6221999SN/A
6231999SN/A    if (result < 0)
6242218SN/A        return -errno;
6251999SN/A
6263114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
6273114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
6281999SN/A
6291999SN/A    return 0;
6301999SN/A}
6311999SN/A
6321999SN/A
633378SN/A/// Target lstat() handler.
634360SN/Atemplate <class OS>
6351450SN/ASyscallReturn
6363114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6372680Sktlim@umich.edu          ThreadContext *tc)
638360SN/A{
639360SN/A    std::string path;
640360SN/A
6412680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6422400SN/A      return -EFAULT;
643360SN/A
6443669Sbinkertn@umich.edu    // Adjust path for current working directory
6453669Sbinkertn@umich.edu    path = process->fullPath(path);
6463669Sbinkertn@umich.edu
647360SN/A    struct stat hostBuf;
648360SN/A    int result = lstat(path.c_str(), &hostBuf);
649360SN/A
650360SN/A    if (result < 0)
6511458SN/A        return -errno;
652360SN/A
6533113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
654360SN/A
6551458SN/A    return 0;
656360SN/A}
657360SN/A
6581999SN/A/// Target lstat64() handler.
6591999SN/Atemplate <class OS>
6601999SN/ASyscallReturn
6613114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6622680Sktlim@umich.edu            ThreadContext *tc)
6631999SN/A{
6641999SN/A    std::string path;
6651999SN/A
6662680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6672400SN/A      return -EFAULT;
6681999SN/A
6693669Sbinkertn@umich.edu    // Adjust path for current working directory
6703669Sbinkertn@umich.edu    path = process->fullPath(path);
6713669Sbinkertn@umich.edu
6722764Sstever@eecs.umich.edu#if NO_STAT64
6732064SN/A    struct stat hostBuf;
6742064SN/A    int result = lstat(path.c_str(), &hostBuf);
6752064SN/A#else
6761999SN/A    struct stat64 hostBuf;
6771999SN/A    int result = lstat64(path.c_str(), &hostBuf);
6782064SN/A#endif
6791999SN/A
6801999SN/A    if (result < 0)
6811999SN/A        return -errno;
6821999SN/A
6833114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6841999SN/A
6851999SN/A    return 0;
6861999SN/A}
6871999SN/A
688378SN/A/// Target fstat() handler.
689360SN/Atemplate <class OS>
6901450SN/ASyscallReturn
6913114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6922680Sktlim@umich.edu          ThreadContext *tc)
693360SN/A{
6942680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
695360SN/A
6961969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
697360SN/A
698360SN/A    if (fd < 0)
6991458SN/A        return -EBADF;
700360SN/A
701360SN/A    struct stat hostBuf;
702360SN/A    int result = fstat(fd, &hostBuf);
703360SN/A
704360SN/A    if (result < 0)
7051458SN/A        return -errno;
706360SN/A
7073114Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
7083114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7092021SN/A
7101458SN/A    return 0;
711360SN/A}
712360SN/A
713360SN/A
7141706SN/A/// Target statfs() handler.
7151706SN/Atemplate <class OS>
7161706SN/ASyscallReturn
7173114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7182680Sktlim@umich.edu           ThreadContext *tc)
7191706SN/A{
7201706SN/A    std::string path;
7211706SN/A
7222680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7232400SN/A      return -EFAULT;
7241706SN/A
7253669Sbinkertn@umich.edu    // Adjust path for current working directory
7263669Sbinkertn@umich.edu    path = process->fullPath(path);
7273669Sbinkertn@umich.edu
7281706SN/A    struct statfs hostBuf;
7291706SN/A    int result = statfs(path.c_str(), &hostBuf);
7301706SN/A
7311706SN/A    if (result < 0)
7322218SN/A        return -errno;
7331706SN/A
7343114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
7353114Sgblack@eecs.umich.edu            (Addr)(tc->getSyscallArg(1)), &hostBuf);
7361706SN/A
7371706SN/A    return 0;
7381706SN/A}
7391706SN/A
7401706SN/A
7411706SN/A/// Target fstatfs() handler.
7421706SN/Atemplate <class OS>
7431706SN/ASyscallReturn
7443114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7452680Sktlim@umich.edu            ThreadContext *tc)
7461706SN/A{
7472680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
7481706SN/A
7491706SN/A    if (fd < 0)
7501706SN/A        return -EBADF;
7511706SN/A
7521706SN/A    struct statfs hostBuf;
7531706SN/A    int result = fstatfs(fd, &hostBuf);
7541706SN/A
7551706SN/A    if (result < 0)
7562218SN/A        return -errno;
7571706SN/A
7583114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1),
7593114Sgblack@eecs.umich.edu        &hostBuf);
7601706SN/A
7611706SN/A    return 0;
7621706SN/A}
7631706SN/A
7641706SN/A
7651999SN/A/// Target writev() handler.
7661999SN/Atemplate <class OS>
7671999SN/ASyscallReturn
7683114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7692680Sktlim@umich.edu           ThreadContext *tc)
7701999SN/A{
7712680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
7721999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7731999SN/A        // doesn't map to any simulator fd: not a valid target fd
7741999SN/A        return -EBADF;
7751999SN/A    }
7761999SN/A
7772680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
7782680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
7792680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
7801999SN/A    struct iovec hiov[count];
7811999SN/A    for (int i = 0; i < count; ++i)
7821999SN/A    {
7831999SN/A        typename OS::tgt_iovec tiov;
7842461SN/A
7852461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
7862461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
7872091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
7881999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
7892461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
7902461SN/A                    hiov[i].iov_len);
7911999SN/A    }
7921999SN/A
7931999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
7941999SN/A
7951999SN/A    for (int i = 0; i < count; ++i)
7961999SN/A    {
7971999SN/A        delete [] (char *)hiov[i].iov_base;
7981999SN/A    }
7991999SN/A
8001999SN/A    if (result < 0)
8012218SN/A        return -errno;
8021999SN/A
8031999SN/A    return 0;
8041999SN/A}
8051999SN/A
8061999SN/A
807378SN/A/// Target mmap() handler.
808378SN/A///
809378SN/A/// We don't really handle mmap().  If the target is mmaping an
810378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
811378SN/A/// nothing (since memory is initialized to zero and the simulator
812378SN/A/// doesn't really check addresses anyway).  Always print a warning,
813378SN/A/// since this could be seriously broken if we're not mapping
814378SN/A/// /dev/zero.
815360SN/A//
816378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
817378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
818378SN/A/// anything else.
819360SN/Atemplate <class OS>
8201450SN/ASyscallReturn
8213114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
822360SN/A{
8232680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
8242680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
8252680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
8262680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
8272680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
8282680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
829360SN/A
8302544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
8312544SN/A        (length % TheISA::VMPageSize) != 0) {
8322544SN/A        warn("mmap failing: arguments not page-aligned: "
8332544SN/A             "start 0x%x length 0x%x",
8342544SN/A             start, length);
8352544SN/A        return -EINVAL;
836360SN/A    }
837360SN/A
8382544SN/A    if (start != 0) {
8392544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
8402544SN/A             start, p->mmap_end);
8412544SN/A    }
8422544SN/A
8432544SN/A    // pick next address from our "mmap region"
8442544SN/A    start = p->mmap_end;
8452544SN/A    p->pTable->allocate(start, length);
8462544SN/A    p->mmap_end += length;
8472544SN/A
8482553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
8491969SN/A        warn("allowing mmap of file @ fd %d. "
8502680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
851360SN/A    }
852360SN/A
8531458SN/A    return start;
854360SN/A}
855360SN/A
856378SN/A/// Target getrlimit() handler.
857360SN/Atemplate <class OS>
8581450SN/ASyscallReturn
8593114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8602680Sktlim@umich.edu        ThreadContext *tc)
861360SN/A{
8622680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
8632680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
864360SN/A
865360SN/A    switch (resource) {
8662064SN/A        case OS::TGT_RLIMIT_STACK:
8672064SN/A            // max stack size in bytes: make up a number (2MB for now)
8682064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
8692091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
8702091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
8712064SN/A            break;
872360SN/A
8732064SN/A        default:
8742064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
8752064SN/A                << std::endl;
8762064SN/A            abort();
8772064SN/A            break;
878360SN/A    }
879360SN/A
8802680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
8811458SN/A    return 0;
882360SN/A}
883360SN/A
884378SN/A/// Target gettimeofday() handler.
885360SN/Atemplate <class OS>
8861450SN/ASyscallReturn
8873114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8882680Sktlim@umich.edu        ThreadContext *tc)
889360SN/A{
8902680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
891360SN/A
892360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
893360SN/A    tp->tv_sec += seconds_since_epoch;
8942091SN/A    tp->tv_sec = htog(tp->tv_sec);
8952091SN/A    tp->tv_usec = htog(tp->tv_usec);
896360SN/A
8972680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
898360SN/A
8991458SN/A    return 0;
900360SN/A}
901360SN/A
902360SN/A
9031999SN/A/// Target utimes() handler.
9041999SN/Atemplate <class OS>
9051999SN/ASyscallReturn
9063114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9072680Sktlim@umich.edu           ThreadContext *tc)
9081999SN/A{
9091999SN/A    std::string path;
9101999SN/A
9112680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
9122400SN/A      return -EFAULT;
9131999SN/A
9142680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
9152680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
9161999SN/A
9171999SN/A    struct timeval hostTimeval[2];
9181999SN/A    for (int i = 0; i < 2; ++i)
9191999SN/A    {
9202091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
9212091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
9221999SN/A    }
9233669Sbinkertn@umich.edu
9243669Sbinkertn@umich.edu    // Adjust path for current working directory
9253669Sbinkertn@umich.edu    path = process->fullPath(path);
9263669Sbinkertn@umich.edu
9271999SN/A    int result = utimes(path.c_str(), hostTimeval);
9281999SN/A
9291999SN/A    if (result < 0)
9301999SN/A        return -errno;
9311999SN/A
9321999SN/A    return 0;
9331999SN/A}
934378SN/A/// Target getrusage() function.
935360SN/Atemplate <class OS>
9361450SN/ASyscallReturn
9373114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9382680Sktlim@umich.edu              ThreadContext *tc)
939360SN/A{
9402680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
9412680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
942360SN/A
9433670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
9443670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
945360SN/A    rup->ru_stime.tv_sec = 0;
946360SN/A    rup->ru_stime.tv_usec = 0;
947360SN/A    rup->ru_maxrss = 0;
948360SN/A    rup->ru_ixrss = 0;
949360SN/A    rup->ru_idrss = 0;
950360SN/A    rup->ru_isrss = 0;
951360SN/A    rup->ru_minflt = 0;
952360SN/A    rup->ru_majflt = 0;
953360SN/A    rup->ru_nswap = 0;
954360SN/A    rup->ru_inblock = 0;
955360SN/A    rup->ru_oublock = 0;
956360SN/A    rup->ru_msgsnd = 0;
957360SN/A    rup->ru_msgrcv = 0;
958360SN/A    rup->ru_nsignals = 0;
959360SN/A    rup->ru_nvcsw = 0;
960360SN/A    rup->ru_nivcsw = 0;
961360SN/A
9623670Sbinkertn@umich.edu    switch (who) {
9633670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
9643670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
9653670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
9663670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
9673670Sbinkertn@umich.edu        break;
9683670Sbinkertn@umich.edu
9693670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
9703670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
9713670Sbinkertn@umich.edu        break;
9723670Sbinkertn@umich.edu
9733670Sbinkertn@umich.edu      default:
9743670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
9753670Sbinkertn@umich.edu        // plow ahead
9763670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
9773670Sbinkertn@umich.edu             who);
9783670Sbinkertn@umich.edu    }
9793670Sbinkertn@umich.edu
9802680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
981360SN/A
9821458SN/A    return 0;
983360SN/A}
984360SN/A
9852553SN/A
9862553SN/A
9872553SN/A
9881354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
989