syscall_emul.hh revision 3114
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
214378SN/A/// Target munmap() handler.
2151706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2163114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
217378SN/A
218378SN/A/// Target gethostname() handler.
2191706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2203114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
221360SN/A
222511SN/A/// Target unlink() handler.
2231706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
225511SN/A
226511SN/A/// Target rename() handler.
2271706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2283114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2291706SN/A
2301706SN/A
2311706SN/A/// Target truncate() handler.
2321706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2333114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2341706SN/A
2351706SN/A
2361706SN/A/// Target ftruncate() handler.
2371706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2383114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2391706SN/A
240511SN/A
2411999SN/A/// Target chown() handler.
2421999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2433114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2441999SN/A
2451999SN/A
2461999SN/A/// Target fchown() handler.
2471999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2483114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2491999SN/A
2503079Sstever@eecs.umich.edu/// Target dup() handler.
2513079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2523114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2533079Sstever@eecs.umich.edu
2542093SN/A/// Target fnctl() handler.
2552093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2563114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2572093SN/A
2582687Sksewell@umich.edu/// Target fcntl64() handler.
2592687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2603114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2612687Sksewell@umich.edu
2622238SN/A/// Target setuid() handler.
2632238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2643114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2652238SN/A
2662238SN/A/// Target getpid() handler.
2672238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2692238SN/A
2702238SN/A/// Target getuid() handler.
2712238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2723114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2732238SN/A
2742238SN/A/// Target getgid() handler.
2752238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2763114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2772238SN/A
2782238SN/A/// Target getppid() handler.
2792238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2812238SN/A
2822238SN/A/// Target geteuid() handler.
2832238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2852238SN/A
2862238SN/A/// Target getegid() handler.
2872238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2883114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2892238SN/A
2902238SN/A
2912238SN/A
2922238SN/A/// Pseudo Funcs  - These functions use a different return convension,
2932238SN/A/// returning a second value in a register other than the normal return register
2942238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
2953114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
2962238SN/A
2972238SN/A/// Target getpidPseudo() handler.
2982238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
2993114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3002238SN/A
3012238SN/A/// Target getuidPseudo() handler.
3022238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3033114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3042238SN/A
3052238SN/A/// Target getgidPseudo() handler.
3062238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3073114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3082238SN/A
3092238SN/A
3101354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3111354SN/Aconst int one_million = 1000000;
3121354SN/A
3131354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3141354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3151354SN/A/// real-world time) to keep simulations repeatable.
3161354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3171354SN/A
3181354SN/A/// Helper function to convert current elapsed time to seconds and
3191354SN/A/// microseconds.
3201354SN/Atemplate <class T1, class T2>
3211354SN/Avoid
3221354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3231354SN/A{
3241609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3251354SN/A    sec = elapsed_usecs / one_million;
3261354SN/A    usec = elapsed_usecs % one_million;
3271354SN/A}
3281354SN/A
329360SN/A//////////////////////////////////////////////////////////////////////
330360SN/A//
331360SN/A// The following emulation functions are generic, but need to be
332360SN/A// templated to account for differences in types, constants, etc.
333360SN/A//
334360SN/A//////////////////////////////////////////////////////////////////////
335360SN/A
3363113Sgblack@eecs.umich.edu#if NO_STAT64
3373113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3383113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3393113Sgblack@eecs.umich.edu#else
3403113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3413113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3423113Sgblack@eecs.umich.edu#endif
3433113Sgblack@eecs.umich.edu
3443113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3453113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3463113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3473113Sgblack@eecs.umich.edu
3483113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3493113Sgblack@eecs.umich.edustatic void
3503113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3513113Sgblack@eecs.umich.edu{
3523113Sgblack@eecs.umich.edu    if (fakeTTY)
3533113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3543113Sgblack@eecs.umich.edu    else
3553113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3563113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3573113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3583113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3593113Sgblack@eecs.umich.edu    if (fakeTTY)
3603113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
3613113Sgblack@eecs.umich.edu    else
3623113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
3633113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
3643113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
3653113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
3663114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
3673113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
3683114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
3693113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
3703114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
3713113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
3723113Sgblack@eecs.umich.edu    tgt->st_blksize = host->st_blksize;
3733113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
3743113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
3753113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
3763113Sgblack@eecs.umich.edu}
3773113Sgblack@eecs.umich.edu
3783113Sgblack@eecs.umich.edu// Same for stat64
3793113Sgblack@eecs.umich.edu
3803113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
3813113Sgblack@eecs.umich.edustatic void
3823113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
3833113Sgblack@eecs.umich.edu{
3843113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
3853113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
3863113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
3873113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
3883113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
3893113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
3903113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
3913113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
3923113Sgblack@eecs.umich.edu#else
3933113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
3943113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
3953113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
3963113Sgblack@eecs.umich.edu#endif
3973113Sgblack@eecs.umich.edu}
3983113Sgblack@eecs.umich.edu
3993113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4003113Sgblack@eecs.umich.edutemplate<class OS>
4013113Sgblack@eecs.umich.edustatic void
4023113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4033113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4043113Sgblack@eecs.umich.edu{
4053113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4063113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4073113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4083113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4093113Sgblack@eecs.umich.edu}
4103113Sgblack@eecs.umich.edu
4113113Sgblack@eecs.umich.edutemplate<class OS>
4123113Sgblack@eecs.umich.edustatic void
4133113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4143113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4153113Sgblack@eecs.umich.edu{
4163113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4173113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4183113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4193113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4203113Sgblack@eecs.umich.edu}
4213113Sgblack@eecs.umich.edu
422378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
423378SN/A/// only to find out if their stdout is a tty, to determine whether to
424378SN/A/// do line or block buffering.
425360SN/Atemplate <class OS>
4261450SN/ASyscallReturn
4273114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4282680Sktlim@umich.edu          ThreadContext *tc)
429360SN/A{
4302680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4312680Sktlim@umich.edu    unsigned req = tc->getSyscallArg(1);
432360SN/A
4331969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
434360SN/A
435360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
436360SN/A        // doesn't map to any simulator fd: not a valid target fd
4371458SN/A        return -EBADF;
438360SN/A    }
439360SN/A
440360SN/A    switch (req) {
4412553SN/A      case OS::TIOCISATTY:
4422553SN/A      case OS::TIOCGETP:
4432553SN/A      case OS::TIOCSETP:
4442553SN/A      case OS::TIOCSETN:
4452553SN/A      case OS::TIOCSETC:
4462553SN/A      case OS::TIOCGETC:
4472553SN/A      case OS::TIOCGETS:
4482553SN/A      case OS::TIOCGETA:
4491458SN/A        return -ENOTTY;
450360SN/A
451360SN/A      default:
4521706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
4532680Sktlim@umich.edu              fd, req, tc->readPC());
454360SN/A    }
455360SN/A}
456360SN/A
457378SN/A/// Target open() handler.
458360SN/Atemplate <class OS>
4591450SN/ASyscallReturn
4603114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4612680Sktlim@umich.edu         ThreadContext *tc)
462360SN/A{
463360SN/A    std::string path;
464360SN/A
4652680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4661458SN/A        return -EFAULT;
467360SN/A
468360SN/A    if (path == "/dev/sysdev0") {
469360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
470360SN/A        // We don't support it, so just punt.
4711706SN/A        warn("Ignoring open(%s, ...)\n", path);
4721458SN/A        return -ENOENT;
473360SN/A    }
474360SN/A
4752680Sktlim@umich.edu    int tgtFlags = tc->getSyscallArg(1);
4762680Sktlim@umich.edu    int mode = tc->getSyscallArg(2);
477360SN/A    int hostFlags = 0;
478360SN/A
479360SN/A    // translate open flags
480360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
481360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
482360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
483360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
484360SN/A        }
485360SN/A    }
486360SN/A
487360SN/A    // any target flags left?
488360SN/A    if (tgtFlags != 0)
4891706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
490360SN/A
491360SN/A#ifdef __CYGWIN32__
492360SN/A    hostFlags |= O_BINARY;
493360SN/A#endif
494360SN/A
4951706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
4961706SN/A
497360SN/A    // open the file
498360SN/A    int fd = open(path.c_str(), hostFlags, mode);
499360SN/A
5001970SN/A    return (fd == -1) ? -errno : process->alloc_fd(fd);
501360SN/A}
502360SN/A
503360SN/A
5041999SN/A/// Target chmod() handler.
5051999SN/Atemplate <class OS>
5061999SN/ASyscallReturn
5073114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5082680Sktlim@umich.edu          ThreadContext *tc)
5091999SN/A{
5101999SN/A    std::string path;
5111999SN/A
5122680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5131999SN/A        return -EFAULT;
5141999SN/A
5152680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5161999SN/A    mode_t hostMode = 0;
5171999SN/A
5181999SN/A    // XXX translate mode flags via OS::something???
5191999SN/A    hostMode = mode;
5201999SN/A
5211999SN/A    // do the chmod
5221999SN/A    int result = chmod(path.c_str(), hostMode);
5231999SN/A    if (result < 0)
5242218SN/A        return -errno;
5251999SN/A
5261999SN/A    return 0;
5271999SN/A}
5281999SN/A
5291999SN/A
5301999SN/A/// Target fchmod() handler.
5311999SN/Atemplate <class OS>
5321999SN/ASyscallReturn
5333114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5342680Sktlim@umich.edu           ThreadContext *tc)
5351999SN/A{
5362680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5371999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5381999SN/A        // doesn't map to any simulator fd: not a valid target fd
5391999SN/A        return -EBADF;
5401999SN/A    }
5411999SN/A
5422680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5431999SN/A    mode_t hostMode = 0;
5441999SN/A
5451999SN/A    // XXX translate mode flags via OS::someting???
5461999SN/A    hostMode = mode;
5471999SN/A
5481999SN/A    // do the fchmod
5491999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
5501999SN/A    if (result < 0)
5512218SN/A        return -errno;
5521999SN/A
5531999SN/A    return 0;
5541999SN/A}
5551999SN/A
5561999SN/A
557378SN/A/// Target stat() handler.
558360SN/Atemplate <class OS>
5591450SN/ASyscallReturn
5603114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5612680Sktlim@umich.edu         ThreadContext *tc)
562360SN/A{
563360SN/A    std::string path;
564360SN/A
5652680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5662400SN/A    return -EFAULT;
567360SN/A
568360SN/A    struct stat hostBuf;
569360SN/A    int result = stat(path.c_str(), &hostBuf);
570360SN/A
571360SN/A    if (result < 0)
5722218SN/A        return -errno;
573360SN/A
5743113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
575360SN/A
5761458SN/A    return 0;
577360SN/A}
578360SN/A
579360SN/A
5801999SN/A/// Target fstat64() handler.
5811999SN/Atemplate <class OS>
5821999SN/ASyscallReturn
5833114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
5842680Sktlim@umich.edu            ThreadContext *tc)
5851999SN/A{
5862680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5871999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5881999SN/A        // doesn't map to any simulator fd: not a valid target fd
5891999SN/A        return -EBADF;
5901999SN/A    }
5911999SN/A
5922764Sstever@eecs.umich.edu#if NO_STAT64
5932064SN/A    struct stat  hostBuf;
5942064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
5952064SN/A#else
5962064SN/A    struct stat64  hostBuf;
5971999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
5982064SN/A#endif
5991999SN/A
6001999SN/A    if (result < 0)
6012218SN/A        return -errno;
6021999SN/A
6033114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
6043114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
6051999SN/A
6061999SN/A    return 0;
6071999SN/A}
6081999SN/A
6091999SN/A
610378SN/A/// Target lstat() handler.
611360SN/Atemplate <class OS>
6121450SN/ASyscallReturn
6133114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6142680Sktlim@umich.edu          ThreadContext *tc)
615360SN/A{
616360SN/A    std::string path;
617360SN/A
6182680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6192400SN/A      return -EFAULT;
620360SN/A
621360SN/A    struct stat hostBuf;
622360SN/A    int result = lstat(path.c_str(), &hostBuf);
623360SN/A
624360SN/A    if (result < 0)
6251458SN/A        return -errno;
626360SN/A
6273113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
628360SN/A
6291458SN/A    return 0;
630360SN/A}
631360SN/A
6321999SN/A/// Target lstat64() handler.
6331999SN/Atemplate <class OS>
6341999SN/ASyscallReturn
6353114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6362680Sktlim@umich.edu            ThreadContext *tc)
6371999SN/A{
6381999SN/A    std::string path;
6391999SN/A
6402680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6412400SN/A      return -EFAULT;
6421999SN/A
6432764Sstever@eecs.umich.edu#if NO_STAT64
6442064SN/A    struct stat hostBuf;
6452064SN/A    int result = lstat(path.c_str(), &hostBuf);
6462064SN/A#else
6471999SN/A    struct stat64 hostBuf;
6481999SN/A    int result = lstat64(path.c_str(), &hostBuf);
6492064SN/A#endif
6501999SN/A
6511999SN/A    if (result < 0)
6521999SN/A        return -errno;
6531999SN/A
6543114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6551999SN/A
6561999SN/A    return 0;
6571999SN/A}
6581999SN/A
659378SN/A/// Target fstat() handler.
660360SN/Atemplate <class OS>
6611450SN/ASyscallReturn
6623114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6632680Sktlim@umich.edu          ThreadContext *tc)
664360SN/A{
6652680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
666360SN/A
6671969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
668360SN/A
669360SN/A    if (fd < 0)
6701458SN/A        return -EBADF;
671360SN/A
672360SN/A    struct stat hostBuf;
673360SN/A    int result = fstat(fd, &hostBuf);
674360SN/A
675360SN/A    if (result < 0)
6761458SN/A        return -errno;
677360SN/A
6783114Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
6793114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
6802021SN/A
6811458SN/A    return 0;
682360SN/A}
683360SN/A
684360SN/A
6851706SN/A/// Target statfs() handler.
6861706SN/Atemplate <class OS>
6871706SN/ASyscallReturn
6883114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6892680Sktlim@umich.edu           ThreadContext *tc)
6901706SN/A{
6911706SN/A    std::string path;
6921706SN/A
6932680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6942400SN/A      return -EFAULT;
6951706SN/A
6961706SN/A    struct statfs hostBuf;
6971706SN/A    int result = statfs(path.c_str(), &hostBuf);
6981706SN/A
6991706SN/A    if (result < 0)
7002218SN/A        return -errno;
7011706SN/A
7023114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
7033114Sgblack@eecs.umich.edu            (Addr)(tc->getSyscallArg(1)), &hostBuf);
7041706SN/A
7051706SN/A    return 0;
7061706SN/A}
7071706SN/A
7081706SN/A
7091706SN/A/// Target fstatfs() handler.
7101706SN/Atemplate <class OS>
7111706SN/ASyscallReturn
7123114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7132680Sktlim@umich.edu            ThreadContext *tc)
7141706SN/A{
7152680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
7161706SN/A
7171706SN/A    if (fd < 0)
7181706SN/A        return -EBADF;
7191706SN/A
7201706SN/A    struct statfs hostBuf;
7211706SN/A    int result = fstatfs(fd, &hostBuf);
7221706SN/A
7231706SN/A    if (result < 0)
7242218SN/A        return -errno;
7251706SN/A
7263114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1),
7273114Sgblack@eecs.umich.edu        &hostBuf);
7281706SN/A
7291706SN/A    return 0;
7301706SN/A}
7311706SN/A
7321706SN/A
7331999SN/A/// Target writev() handler.
7341999SN/Atemplate <class OS>
7351999SN/ASyscallReturn
7363114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7372680Sktlim@umich.edu           ThreadContext *tc)
7381999SN/A{
7392680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
7401999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7411999SN/A        // doesn't map to any simulator fd: not a valid target fd
7421999SN/A        return -EBADF;
7431999SN/A    }
7441999SN/A
7452680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
7462680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
7472680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
7481999SN/A    struct iovec hiov[count];
7491999SN/A    for (int i = 0; i < count; ++i)
7501999SN/A    {
7511999SN/A        typename OS::tgt_iovec tiov;
7522461SN/A
7532461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
7542461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
7552091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
7561999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
7572461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
7582461SN/A                    hiov[i].iov_len);
7591999SN/A    }
7601999SN/A
7611999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
7621999SN/A
7631999SN/A    for (int i = 0; i < count; ++i)
7641999SN/A    {
7651999SN/A        delete [] (char *)hiov[i].iov_base;
7661999SN/A    }
7671999SN/A
7681999SN/A    if (result < 0)
7692218SN/A        return -errno;
7701999SN/A
7711999SN/A    return 0;
7721999SN/A}
7731999SN/A
7741999SN/A
775378SN/A/// Target mmap() handler.
776378SN/A///
777378SN/A/// We don't really handle mmap().  If the target is mmaping an
778378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
779378SN/A/// nothing (since memory is initialized to zero and the simulator
780378SN/A/// doesn't really check addresses anyway).  Always print a warning,
781378SN/A/// since this could be seriously broken if we're not mapping
782378SN/A/// /dev/zero.
783360SN/A//
784378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
785378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
786378SN/A/// anything else.
787360SN/Atemplate <class OS>
7881450SN/ASyscallReturn
7893114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
790360SN/A{
7912680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
7922680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
7932680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
7942680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
7952680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
7962680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
797360SN/A
7982544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
7992544SN/A        (length % TheISA::VMPageSize) != 0) {
8002544SN/A        warn("mmap failing: arguments not page-aligned: "
8012544SN/A             "start 0x%x length 0x%x",
8022544SN/A             start, length);
8032544SN/A        return -EINVAL;
804360SN/A    }
805360SN/A
8062544SN/A    if (start != 0) {
8072544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
8082544SN/A             start, p->mmap_end);
8092544SN/A    }
8102544SN/A
8112544SN/A    // pick next address from our "mmap region"
8122544SN/A    start = p->mmap_end;
8132544SN/A    p->pTable->allocate(start, length);
8142544SN/A    p->mmap_end += length;
8152544SN/A
8162553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
8171969SN/A        warn("allowing mmap of file @ fd %d. "
8182680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
819360SN/A    }
820360SN/A
8211458SN/A    return start;
822360SN/A}
823360SN/A
824378SN/A/// Target getrlimit() handler.
825360SN/Atemplate <class OS>
8261450SN/ASyscallReturn
8273114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8282680Sktlim@umich.edu        ThreadContext *tc)
829360SN/A{
8302680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
8312680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
832360SN/A
833360SN/A    switch (resource) {
8342064SN/A        case OS::TGT_RLIMIT_STACK:
8352064SN/A            // max stack size in bytes: make up a number (2MB for now)
8362064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
8372091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
8382091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
8392064SN/A            break;
840360SN/A
8412064SN/A        default:
8422064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
8432064SN/A                << std::endl;
8442064SN/A            abort();
8452064SN/A            break;
846360SN/A    }
847360SN/A
8482680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
8491458SN/A    return 0;
850360SN/A}
851360SN/A
852378SN/A/// Target gettimeofday() handler.
853360SN/Atemplate <class OS>
8541450SN/ASyscallReturn
8553114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8562680Sktlim@umich.edu        ThreadContext *tc)
857360SN/A{
8582680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
859360SN/A
860360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
861360SN/A    tp->tv_sec += seconds_since_epoch;
8622091SN/A    tp->tv_sec = htog(tp->tv_sec);
8632091SN/A    tp->tv_usec = htog(tp->tv_usec);
864360SN/A
8652680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
866360SN/A
8671458SN/A    return 0;
868360SN/A}
869360SN/A
870360SN/A
8711999SN/A/// Target utimes() handler.
8721999SN/Atemplate <class OS>
8731999SN/ASyscallReturn
8743114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8752680Sktlim@umich.edu           ThreadContext *tc)
8761999SN/A{
8771999SN/A    std::string path;
8781999SN/A
8792680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
8802400SN/A      return -EFAULT;
8811999SN/A
8822680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
8832680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
8841999SN/A
8851999SN/A    struct timeval hostTimeval[2];
8861999SN/A    for (int i = 0; i < 2; ++i)
8871999SN/A    {
8882091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
8892091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
8901999SN/A    }
8911999SN/A    int result = utimes(path.c_str(), hostTimeval);
8921999SN/A
8931999SN/A    if (result < 0)
8941999SN/A        return -errno;
8951999SN/A
8961999SN/A    return 0;
8971999SN/A}
898378SN/A/// Target getrusage() function.
899360SN/Atemplate <class OS>
9001450SN/ASyscallReturn
9013114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9022680Sktlim@umich.edu              ThreadContext *tc)
903360SN/A{
9042680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
9052680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
906360SN/A
9072553SN/A    if (who != OS::TGT_RUSAGE_SELF) {
908360SN/A        // don't really handle THREAD or CHILDREN, but just warn and
909360SN/A        // plow ahead
9101969SN/A        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
9111969SN/A             who);
912360SN/A    }
913360SN/A
914360SN/A    getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
9152091SN/A    rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
9162091SN/A    rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
9172091SN/A
918360SN/A    rup->ru_stime.tv_sec = 0;
919360SN/A    rup->ru_stime.tv_usec = 0;
920360SN/A    rup->ru_maxrss = 0;
921360SN/A    rup->ru_ixrss = 0;
922360SN/A    rup->ru_idrss = 0;
923360SN/A    rup->ru_isrss = 0;
924360SN/A    rup->ru_minflt = 0;
925360SN/A    rup->ru_majflt = 0;
926360SN/A    rup->ru_nswap = 0;
927360SN/A    rup->ru_inblock = 0;
928360SN/A    rup->ru_oublock = 0;
929360SN/A    rup->ru_msgsnd = 0;
930360SN/A    rup->ru_msgrcv = 0;
931360SN/A    rup->ru_nsignals = 0;
932360SN/A    rup->ru_nvcsw = 0;
933360SN/A    rup->ru_nivcsw = 0;
934360SN/A
9352680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
936360SN/A
9371458SN/A    return 0;
938360SN/A}
939360SN/A
9402553SN/A
9412553SN/A
9422553SN/A
9431354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
944