syscall_emul.hh revision 5515
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
2265513SMichael.Adler@intel.com/// Target getcwd() handler.
2275513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2285513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2295513SMichael.Adler@intel.com
2305513SMichael.Adler@intel.com/// Target unlink() handler.
2315513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2325513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2335513SMichael.Adler@intel.com
234511SN/A/// Target unlink() handler.
2351706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2363114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
237511SN/A
2385513SMichael.Adler@intel.com/// Target mkdir() handler.
2395513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2405513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2415513SMichael.Adler@intel.com
242511SN/A/// Target rename() handler.
2431706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2443114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2451706SN/A
2461706SN/A
2471706SN/A/// Target truncate() handler.
2481706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2493114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2501706SN/A
2511706SN/A
2521706SN/A/// Target ftruncate() handler.
2531706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2543114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2551706SN/A
256511SN/A
2575513SMichael.Adler@intel.com/// Target umask() handler.
2585513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2595513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2605513SMichael.Adler@intel.com
2615513SMichael.Adler@intel.com
2621999SN/A/// Target chown() handler.
2631999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2643114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2651999SN/A
2661999SN/A
2671999SN/A/// Target fchown() handler.
2681999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2693114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2701999SN/A
2713079Sstever@eecs.umich.edu/// Target dup() handler.
2723079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2743079Sstever@eecs.umich.edu
2752093SN/A/// Target fnctl() handler.
2762093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2773114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2782093SN/A
2792687Sksewell@umich.edu/// Target fcntl64() handler.
2802687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2813114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2822687Sksewell@umich.edu
2832238SN/A/// Target setuid() handler.
2842238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2853114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2862238SN/A
2872238SN/A/// Target getpid() handler.
2882238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2893114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2902238SN/A
2912238SN/A/// Target getuid() handler.
2922238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2933114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2942238SN/A
2952238SN/A/// Target getgid() handler.
2962238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2973114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2982238SN/A
2992238SN/A/// Target getppid() handler.
3002238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3013114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3022238SN/A
3032238SN/A/// Target geteuid() handler.
3042238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3053114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3062238SN/A
3072238SN/A/// Target getegid() handler.
3082238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3093114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3102238SN/A
3112238SN/A
3122238SN/A
3132238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3142238SN/A/// returning a second value in a register other than the normal return register
3152238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3163114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3172238SN/A
3182238SN/A/// Target getpidPseudo() handler.
3192238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3203114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3212238SN/A
3222238SN/A/// Target getuidPseudo() handler.
3232238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3243114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3252238SN/A
3262238SN/A/// Target getgidPseudo() handler.
3272238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3283114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3292238SN/A
3302238SN/A
3311354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3321354SN/Aconst int one_million = 1000000;
3331354SN/A
3341354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3351354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3361354SN/A/// real-world time) to keep simulations repeatable.
3371354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3381354SN/A
3391354SN/A/// Helper function to convert current elapsed time to seconds and
3401354SN/A/// microseconds.
3411354SN/Atemplate <class T1, class T2>
3421354SN/Avoid
3431354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3441354SN/A{
3451609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3461354SN/A    sec = elapsed_usecs / one_million;
3471354SN/A    usec = elapsed_usecs % one_million;
3481354SN/A}
3491354SN/A
350360SN/A//////////////////////////////////////////////////////////////////////
351360SN/A//
352360SN/A// The following emulation functions are generic, but need to be
353360SN/A// templated to account for differences in types, constants, etc.
354360SN/A//
355360SN/A//////////////////////////////////////////////////////////////////////
356360SN/A
3573113Sgblack@eecs.umich.edu#if NO_STAT64
3583113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3593113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3603113Sgblack@eecs.umich.edu#else
3613113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3623113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3633113Sgblack@eecs.umich.edu#endif
3643113Sgblack@eecs.umich.edu
3653113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3663113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3673113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3683113Sgblack@eecs.umich.edu
3693113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3703113Sgblack@eecs.umich.edustatic void
3713113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3723113Sgblack@eecs.umich.edu{
3734189Sgblack@eecs.umich.edu    using namespace TheISA;
3744189Sgblack@eecs.umich.edu
3753113Sgblack@eecs.umich.edu    if (fakeTTY)
3763113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3773113Sgblack@eecs.umich.edu    else
3783113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3793113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3803113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3813113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3823277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
3835515SMichael.Adler@intel.com    if (fakeTTY) {
3845515SMichael.Adler@intel.com        // Claim to be a character device
3855515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
3865515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
3875515SMichael.Adler@intel.com    }
3883277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
3893277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
3903277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
3913277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
3923277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
3933277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
3943277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
3953113Sgblack@eecs.umich.edu    if (fakeTTY)
3963113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
3973113Sgblack@eecs.umich.edu    else
3983113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
3993113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
4003113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4013113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
4023114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4033113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
4043114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4053113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
4063114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4073113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
4084061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4094061Sgblack@eecs.umich.edu    // consistently across different hosts.
4104061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4113113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
4123113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4133113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
4143113Sgblack@eecs.umich.edu}
4153113Sgblack@eecs.umich.edu
4163113Sgblack@eecs.umich.edu// Same for stat64
4173113Sgblack@eecs.umich.edu
4183113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4193113Sgblack@eecs.umich.edustatic void
4203113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4213113Sgblack@eecs.umich.edu{
4224189Sgblack@eecs.umich.edu    using namespace TheISA;
4234189Sgblack@eecs.umich.edu
4243113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4253113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4263113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4273113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4283113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4293113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4303113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4313113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4323113Sgblack@eecs.umich.edu#else
4333113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4343113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4353113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4363113Sgblack@eecs.umich.edu#endif
4373113Sgblack@eecs.umich.edu}
4383113Sgblack@eecs.umich.edu
4393113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4403113Sgblack@eecs.umich.edutemplate<class OS>
4413113Sgblack@eecs.umich.edustatic void
4423113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4433113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4443113Sgblack@eecs.umich.edu{
4453113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4463113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4473113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4483113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4493113Sgblack@eecs.umich.edu}
4503113Sgblack@eecs.umich.edu
4513113Sgblack@eecs.umich.edutemplate<class OS>
4523113Sgblack@eecs.umich.edustatic void
4533113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4543113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4553113Sgblack@eecs.umich.edu{
4563113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4573113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4583113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4593113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4603113Sgblack@eecs.umich.edu}
4613113Sgblack@eecs.umich.edu
462378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
463378SN/A/// only to find out if their stdout is a tty, to determine whether to
464378SN/A/// do line or block buffering.
465360SN/Atemplate <class OS>
4661450SN/ASyscallReturn
4673114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4682680Sktlim@umich.edu          ThreadContext *tc)
469360SN/A{
4702680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4712680Sktlim@umich.edu    unsigned req = tc->getSyscallArg(1);
472360SN/A
4731969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
474360SN/A
475360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
476360SN/A        // doesn't map to any simulator fd: not a valid target fd
4771458SN/A        return -EBADF;
478360SN/A    }
479360SN/A
480360SN/A    switch (req) {
4814131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
4824131Sbinkertn@umich.edu      case OS::TIOCGETP_:
4834131Sbinkertn@umich.edu      case OS::TIOCSETP_:
4844131Sbinkertn@umich.edu      case OS::TIOCSETN_:
4854131Sbinkertn@umich.edu      case OS::TIOCSETC_:
4864131Sbinkertn@umich.edu      case OS::TIOCGETC_:
4874131Sbinkertn@umich.edu      case OS::TIOCGETS_:
4884131Sbinkertn@umich.edu      case OS::TIOCGETA_:
4891458SN/A        return -ENOTTY;
490360SN/A
491360SN/A      default:
4921706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
4932680Sktlim@umich.edu              fd, req, tc->readPC());
494360SN/A    }
495360SN/A}
496360SN/A
497378SN/A/// Target open() handler.
498360SN/Atemplate <class OS>
4991450SN/ASyscallReturn
5003114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5012680Sktlim@umich.edu         ThreadContext *tc)
502360SN/A{
503360SN/A    std::string path;
504360SN/A
5052680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5061458SN/A        return -EFAULT;
507360SN/A
508360SN/A    if (path == "/dev/sysdev0") {
509360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
510360SN/A        // We don't support it, so just punt.
5111706SN/A        warn("Ignoring open(%s, ...)\n", path);
5121458SN/A        return -ENOENT;
513360SN/A    }
514360SN/A
5152680Sktlim@umich.edu    int tgtFlags = tc->getSyscallArg(1);
5162680Sktlim@umich.edu    int mode = tc->getSyscallArg(2);
517360SN/A    int hostFlags = 0;
518360SN/A
519360SN/A    // translate open flags
520360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
521360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
522360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
523360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
524360SN/A        }
525360SN/A    }
526360SN/A
527360SN/A    // any target flags left?
528360SN/A    if (tgtFlags != 0)
5291706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
530360SN/A
531360SN/A#ifdef __CYGWIN32__
532360SN/A    hostFlags |= O_BINARY;
533360SN/A#endif
534360SN/A
5353669Sbinkertn@umich.edu    // Adjust path for current working directory
5363669Sbinkertn@umich.edu    path = process->fullPath(path);
5373669Sbinkertn@umich.edu
5381706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5391706SN/A
540360SN/A    // open the file
541360SN/A    int fd = open(path.c_str(), hostFlags, mode);
542360SN/A
5435282Srstrong@cs.ucsd.edu    return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
544360SN/A}
545360SN/A
546360SN/A
5471999SN/A/// Target chmod() handler.
5481999SN/Atemplate <class OS>
5491999SN/ASyscallReturn
5503114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5512680Sktlim@umich.edu          ThreadContext *tc)
5521999SN/A{
5531999SN/A    std::string path;
5541999SN/A
5552680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5561999SN/A        return -EFAULT;
5571999SN/A
5582680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5591999SN/A    mode_t hostMode = 0;
5601999SN/A
5611999SN/A    // XXX translate mode flags via OS::something???
5621999SN/A    hostMode = mode;
5631999SN/A
5643669Sbinkertn@umich.edu    // Adjust path for current working directory
5653669Sbinkertn@umich.edu    path = process->fullPath(path);
5663669Sbinkertn@umich.edu
5671999SN/A    // do the chmod
5681999SN/A    int result = chmod(path.c_str(), hostMode);
5691999SN/A    if (result < 0)
5702218SN/A        return -errno;
5711999SN/A
5721999SN/A    return 0;
5731999SN/A}
5741999SN/A
5751999SN/A
5761999SN/A/// Target fchmod() handler.
5771999SN/Atemplate <class OS>
5781999SN/ASyscallReturn
5793114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5802680Sktlim@umich.edu           ThreadContext *tc)
5811999SN/A{
5822680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5831999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5841999SN/A        // doesn't map to any simulator fd: not a valid target fd
5851999SN/A        return -EBADF;
5861999SN/A    }
5871999SN/A
5882680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
5891999SN/A    mode_t hostMode = 0;
5901999SN/A
5911999SN/A    // XXX translate mode flags via OS::someting???
5921999SN/A    hostMode = mode;
5931999SN/A
5941999SN/A    // do the fchmod
5951999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
5961999SN/A    if (result < 0)
5972218SN/A        return -errno;
5981999SN/A
5991999SN/A    return 0;
6001999SN/A}
6011999SN/A
6021999SN/A
603378SN/A/// Target stat() handler.
604360SN/Atemplate <class OS>
6051450SN/ASyscallReturn
6063114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6072680Sktlim@umich.edu         ThreadContext *tc)
608360SN/A{
609360SN/A    std::string path;
610360SN/A
6112680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6122400SN/A    return -EFAULT;
613360SN/A
6143669Sbinkertn@umich.edu    // Adjust path for current working directory
6153669Sbinkertn@umich.edu    path = process->fullPath(path);
6163669Sbinkertn@umich.edu
617360SN/A    struct stat hostBuf;
618360SN/A    int result = stat(path.c_str(), &hostBuf);
619360SN/A
620360SN/A    if (result < 0)
6212218SN/A        return -errno;
622360SN/A
6233113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
624360SN/A
6251458SN/A    return 0;
626360SN/A}
627360SN/A
628360SN/A
6295074Ssaidi@eecs.umich.edu/// Target stat64() handler.
6305074Ssaidi@eecs.umich.edutemplate <class OS>
6315074Ssaidi@eecs.umich.eduSyscallReturn
6325074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6335074Ssaidi@eecs.umich.edu           ThreadContext *tc)
6345074Ssaidi@eecs.umich.edu{
6355074Ssaidi@eecs.umich.edu    std::string path;
6365074Ssaidi@eecs.umich.edu
6375074Ssaidi@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6385074Ssaidi@eecs.umich.edu        return -EFAULT;
6395074Ssaidi@eecs.umich.edu
6405074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
6415074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
6425074Ssaidi@eecs.umich.edu
6435208Ssaidi@eecs.umich.edu#if NO_STAT64
6445208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
6455208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
6465208Ssaidi@eecs.umich.edu#else
6475074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
6485074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
6495208Ssaidi@eecs.umich.edu#endif
6505074Ssaidi@eecs.umich.edu
6515074Ssaidi@eecs.umich.edu    if (result < 0)
6525074Ssaidi@eecs.umich.edu        return -errno;
6535074Ssaidi@eecs.umich.edu
6545074Ssaidi@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6555074Ssaidi@eecs.umich.edu
6565074Ssaidi@eecs.umich.edu    return 0;
6575074Ssaidi@eecs.umich.edu}
6585074Ssaidi@eecs.umich.edu
6595074Ssaidi@eecs.umich.edu
6601999SN/A/// Target fstat64() handler.
6611999SN/Atemplate <class OS>
6621999SN/ASyscallReturn
6633114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
6642680Sktlim@umich.edu            ThreadContext *tc)
6651999SN/A{
6662680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
6671999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6681999SN/A        // doesn't map to any simulator fd: not a valid target fd
6691999SN/A        return -EBADF;
6701999SN/A    }
6711999SN/A
6722764Sstever@eecs.umich.edu#if NO_STAT64
6732064SN/A    struct stat  hostBuf;
6742064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
6752064SN/A#else
6762064SN/A    struct stat64  hostBuf;
6771999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
6782064SN/A#endif
6791999SN/A
6801999SN/A    if (result < 0)
6812218SN/A        return -errno;
6821999SN/A
6833114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
6843114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
6851999SN/A
6861999SN/A    return 0;
6871999SN/A}
6881999SN/A
6891999SN/A
690378SN/A/// Target lstat() handler.
691360SN/Atemplate <class OS>
6921450SN/ASyscallReturn
6933114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6942680Sktlim@umich.edu          ThreadContext *tc)
695360SN/A{
696360SN/A    std::string path;
697360SN/A
6982680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6992400SN/A      return -EFAULT;
700360SN/A
7013669Sbinkertn@umich.edu    // Adjust path for current working directory
7023669Sbinkertn@umich.edu    path = process->fullPath(path);
7033669Sbinkertn@umich.edu
704360SN/A    struct stat hostBuf;
705360SN/A    int result = lstat(path.c_str(), &hostBuf);
706360SN/A
707360SN/A    if (result < 0)
7081458SN/A        return -errno;
709360SN/A
7103113Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
711360SN/A
7121458SN/A    return 0;
713360SN/A}
714360SN/A
7151999SN/A/// Target lstat64() handler.
7161999SN/Atemplate <class OS>
7171999SN/ASyscallReturn
7183114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7192680Sktlim@umich.edu            ThreadContext *tc)
7201999SN/A{
7211999SN/A    std::string path;
7221999SN/A
7232680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7242400SN/A      return -EFAULT;
7251999SN/A
7263669Sbinkertn@umich.edu    // Adjust path for current working directory
7273669Sbinkertn@umich.edu    path = process->fullPath(path);
7283669Sbinkertn@umich.edu
7292764Sstever@eecs.umich.edu#if NO_STAT64
7302064SN/A    struct stat hostBuf;
7312064SN/A    int result = lstat(path.c_str(), &hostBuf);
7322064SN/A#else
7331999SN/A    struct stat64 hostBuf;
7341999SN/A    int result = lstat64(path.c_str(), &hostBuf);
7352064SN/A#endif
7361999SN/A
7371999SN/A    if (result < 0)
7381999SN/A        return -errno;
7391999SN/A
7403114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
7411999SN/A
7421999SN/A    return 0;
7431999SN/A}
7441999SN/A
745378SN/A/// Target fstat() handler.
746360SN/Atemplate <class OS>
7471450SN/ASyscallReturn
7483114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7492680Sktlim@umich.edu          ThreadContext *tc)
750360SN/A{
7512680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
752360SN/A
7531969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
754360SN/A
755360SN/A    if (fd < 0)
7561458SN/A        return -EBADF;
757360SN/A
758360SN/A    struct stat hostBuf;
759360SN/A    int result = fstat(fd, &hostBuf);
760360SN/A
761360SN/A    if (result < 0)
7621458SN/A        return -errno;
763360SN/A
7643114Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), tc->getSyscallArg(1),
7653114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7662021SN/A
7671458SN/A    return 0;
768360SN/A}
769360SN/A
770360SN/A
7711706SN/A/// Target statfs() handler.
7721706SN/Atemplate <class OS>
7731706SN/ASyscallReturn
7743114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7752680Sktlim@umich.edu           ThreadContext *tc)
7761706SN/A{
7771706SN/A    std::string path;
7781706SN/A
7792680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7802400SN/A      return -EFAULT;
7811706SN/A
7823669Sbinkertn@umich.edu    // Adjust path for current working directory
7833669Sbinkertn@umich.edu    path = process->fullPath(path);
7843669Sbinkertn@umich.edu
7851706SN/A    struct statfs hostBuf;
7861706SN/A    int result = statfs(path.c_str(), &hostBuf);
7871706SN/A
7881706SN/A    if (result < 0)
7892218SN/A        return -errno;
7901706SN/A
7913114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
7923114Sgblack@eecs.umich.edu            (Addr)(tc->getSyscallArg(1)), &hostBuf);
7931706SN/A
7941706SN/A    return 0;
7951706SN/A}
7961706SN/A
7971706SN/A
7981706SN/A/// Target fstatfs() handler.
7991706SN/Atemplate <class OS>
8001706SN/ASyscallReturn
8013114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8022680Sktlim@umich.edu            ThreadContext *tc)
8031706SN/A{
8042680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
8051706SN/A
8061706SN/A    if (fd < 0)
8071706SN/A        return -EBADF;
8081706SN/A
8091706SN/A    struct statfs hostBuf;
8101706SN/A    int result = fstatfs(fd, &hostBuf);
8111706SN/A
8121706SN/A    if (result < 0)
8132218SN/A        return -errno;
8141706SN/A
8153114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1),
8163114Sgblack@eecs.umich.edu        &hostBuf);
8171706SN/A
8181706SN/A    return 0;
8191706SN/A}
8201706SN/A
8211706SN/A
8221999SN/A/// Target writev() handler.
8231999SN/Atemplate <class OS>
8241999SN/ASyscallReturn
8253114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8262680Sktlim@umich.edu           ThreadContext *tc)
8271999SN/A{
8282680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
8291999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8301999SN/A        // doesn't map to any simulator fd: not a valid target fd
8311999SN/A        return -EBADF;
8321999SN/A    }
8331999SN/A
8342680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
8352680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
8362680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
8371999SN/A    struct iovec hiov[count];
8381999SN/A    for (int i = 0; i < count; ++i)
8391999SN/A    {
8401999SN/A        typename OS::tgt_iovec tiov;
8412461SN/A
8422461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
8432461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
8442091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
8451999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
8462461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
8472461SN/A                    hiov[i].iov_len);
8481999SN/A    }
8491999SN/A
8501999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
8511999SN/A
8521999SN/A    for (int i = 0; i < count; ++i)
8531999SN/A    {
8541999SN/A        delete [] (char *)hiov[i].iov_base;
8551999SN/A    }
8561999SN/A
8571999SN/A    if (result < 0)
8582218SN/A        return -errno;
8591999SN/A
8601999SN/A    return 0;
8611999SN/A}
8621999SN/A
8631999SN/A
864378SN/A/// Target mmap() handler.
865378SN/A///
866378SN/A/// We don't really handle mmap().  If the target is mmaping an
867378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
868378SN/A/// nothing (since memory is initialized to zero and the simulator
869378SN/A/// doesn't really check addresses anyway).  Always print a warning,
870378SN/A/// since this could be seriously broken if we're not mapping
871378SN/A/// /dev/zero.
872360SN/A//
873378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
874378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
875378SN/A/// anything else.
876360SN/Atemplate <class OS>
8771450SN/ASyscallReturn
8783114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
879360SN/A{
8802680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
8812680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
8822680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
8832680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
8842680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
8852680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
886360SN/A
8872544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
8882544SN/A        (length % TheISA::VMPageSize) != 0) {
8892544SN/A        warn("mmap failing: arguments not page-aligned: "
8902544SN/A             "start 0x%x length 0x%x",
8912544SN/A             start, length);
8922544SN/A        return -EINVAL;
893360SN/A    }
894360SN/A
8952544SN/A    if (start != 0) {
8962544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
8972544SN/A             start, p->mmap_end);
8982544SN/A    }
8992544SN/A
9002544SN/A    // pick next address from our "mmap region"
9012544SN/A    start = p->mmap_end;
9022544SN/A    p->pTable->allocate(start, length);
9032544SN/A    p->mmap_end += length;
9042544SN/A
9052553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
9061969SN/A        warn("allowing mmap of file @ fd %d. "
9072680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
908360SN/A    }
909360SN/A
9101458SN/A    return start;
911360SN/A}
912360SN/A
913378SN/A/// Target getrlimit() handler.
914360SN/Atemplate <class OS>
9151450SN/ASyscallReturn
9163114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9172680Sktlim@umich.edu        ThreadContext *tc)
918360SN/A{
9192680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
9202680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
921360SN/A
922360SN/A    switch (resource) {
9232064SN/A        case OS::TGT_RLIMIT_STACK:
9242064SN/A            // max stack size in bytes: make up a number (2MB for now)
9252064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
9262091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
9272091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
9282064SN/A            break;
929360SN/A
9302064SN/A        default:
9312064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
9322064SN/A                << std::endl;
9332064SN/A            abort();
9342064SN/A            break;
935360SN/A    }
936360SN/A
9372680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
9381458SN/A    return 0;
939360SN/A}
940360SN/A
941378SN/A/// Target gettimeofday() handler.
942360SN/Atemplate <class OS>
9431450SN/ASyscallReturn
9443114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9452680Sktlim@umich.edu        ThreadContext *tc)
946360SN/A{
9472680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
948360SN/A
949360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
950360SN/A    tp->tv_sec += seconds_since_epoch;
9512091SN/A    tp->tv_sec = htog(tp->tv_sec);
9522091SN/A    tp->tv_usec = htog(tp->tv_usec);
953360SN/A
9542680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
955360SN/A
9561458SN/A    return 0;
957360SN/A}
958360SN/A
959360SN/A
9601999SN/A/// Target utimes() handler.
9611999SN/Atemplate <class OS>
9621999SN/ASyscallReturn
9633114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9642680Sktlim@umich.edu           ThreadContext *tc)
9651999SN/A{
9661999SN/A    std::string path;
9671999SN/A
9682680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
9692400SN/A      return -EFAULT;
9701999SN/A
9712680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
9722680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
9731999SN/A
9741999SN/A    struct timeval hostTimeval[2];
9751999SN/A    for (int i = 0; i < 2; ++i)
9761999SN/A    {
9772091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
9782091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
9791999SN/A    }
9803669Sbinkertn@umich.edu
9813669Sbinkertn@umich.edu    // Adjust path for current working directory
9823669Sbinkertn@umich.edu    path = process->fullPath(path);
9833669Sbinkertn@umich.edu
9841999SN/A    int result = utimes(path.c_str(), hostTimeval);
9851999SN/A
9861999SN/A    if (result < 0)
9871999SN/A        return -errno;
9881999SN/A
9891999SN/A    return 0;
9901999SN/A}
991378SN/A/// Target getrusage() function.
992360SN/Atemplate <class OS>
9931450SN/ASyscallReturn
9943114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9952680Sktlim@umich.edu              ThreadContext *tc)
996360SN/A{
9972680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
9982680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
999360SN/A
10003670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
10013670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1002360SN/A    rup->ru_stime.tv_sec = 0;
1003360SN/A    rup->ru_stime.tv_usec = 0;
1004360SN/A    rup->ru_maxrss = 0;
1005360SN/A    rup->ru_ixrss = 0;
1006360SN/A    rup->ru_idrss = 0;
1007360SN/A    rup->ru_isrss = 0;
1008360SN/A    rup->ru_minflt = 0;
1009360SN/A    rup->ru_majflt = 0;
1010360SN/A    rup->ru_nswap = 0;
1011360SN/A    rup->ru_inblock = 0;
1012360SN/A    rup->ru_oublock = 0;
1013360SN/A    rup->ru_msgsnd = 0;
1014360SN/A    rup->ru_msgrcv = 0;
1015360SN/A    rup->ru_nsignals = 0;
1016360SN/A    rup->ru_nvcsw = 0;
1017360SN/A    rup->ru_nivcsw = 0;
1018360SN/A
10193670Sbinkertn@umich.edu    switch (who) {
10203670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
10213670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
10223670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
10233670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
10243670Sbinkertn@umich.edu        break;
10253670Sbinkertn@umich.edu
10263670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
10273670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
10283670Sbinkertn@umich.edu        break;
10293670Sbinkertn@umich.edu
10303670Sbinkertn@umich.edu      default:
10313670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
10323670Sbinkertn@umich.edu        // plow ahead
10333670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
10343670Sbinkertn@umich.edu             who);
10353670Sbinkertn@umich.edu    }
10363670Sbinkertn@umich.edu
10372680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1038360SN/A
10391458SN/A    return 0;
1040360SN/A}
1041360SN/A
10422553SN/A
10432553SN/A
10442553SN/A
10451354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1046