syscall_emul.hh revision 6686
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__
475543Ssaidi@eecs.umich.edu#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
532474SN/A#include "base/chunk_generator.hh"
545543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
552462SN/A#include "base/misc.hh"
561354SN/A#include "base/trace.hh"
576216Snate@binkert.org#include "base/types.hh"
586658Snate@binkert.org#include "config/the_isa.hh"
592474SN/A#include "cpu/base.hh"
602680Sktlim@umich.edu#include "cpu/thread_context.hh"
612474SN/A#include "mem/translating_port.hh"
622474SN/A#include "mem/page_table.hh"
636640Svince@csl.cornell.edu#include "sim/system.hh"
641354SN/A#include "sim/process.hh"
65360SN/A
66360SN/A///
67360SN/A/// System call descriptor.
68360SN/A///
69360SN/Aclass SyscallDesc {
70360SN/A
71360SN/A  public:
72360SN/A
73378SN/A    /// Typedef for target syscall handler functions.
741450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
753114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
76360SN/A
775543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
785543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
795543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
80360SN/A
81360SN/A    /// Flag values for controlling syscall behavior.
82360SN/A    enum Flags {
83360SN/A        /// Don't set return regs according to funcPtr return value.
84360SN/A        /// Used for syscalls with non-standard return conventions
852680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
86360SN/A        /// sigreturn).
87360SN/A        SuppressReturnValue = 1
88360SN/A    };
89360SN/A
90360SN/A    /// Constructor.
91360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
92360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
93360SN/A    {
94360SN/A    }
95360SN/A
96360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
973114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
98360SN/A};
99360SN/A
100360SN/A
101360SN/Aclass BaseBufferArg {
102360SN/A
103360SN/A  public:
104360SN/A
105360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
106360SN/A    {
107360SN/A        bufPtr = new uint8_t[size];
108360SN/A        // clear out buffer: in case we only partially populate this,
109360SN/A        // and then do a copyOut(), we want to make sure we don't
110360SN/A        // introduce any random junk into the simulated address space
111360SN/A        memset(bufPtr, 0, size);
112360SN/A    }
113360SN/A
114360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
115360SN/A
116360SN/A    //
117360SN/A    // copy data into simulator space (read from target memory)
118360SN/A    //
1192400SN/A    virtual bool copyIn(TranslatingPort *memport)
120360SN/A    {
1212461SN/A        memport->readBlob(addr, bufPtr, size);
1225543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
123360SN/A    }
124360SN/A
125360SN/A    //
126360SN/A    // copy data out of simulator space (write to target memory)
127360SN/A    //
1282400SN/A    virtual bool copyOut(TranslatingPort *memport)
129360SN/A    {
1302461SN/A        memport->writeBlob(addr, bufPtr, size);
1315543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
132360SN/A    }
133360SN/A
134360SN/A  protected:
135360SN/A    Addr addr;
136360SN/A    int size;
137360SN/A    uint8_t *bufPtr;
138360SN/A};
139360SN/A
140360SN/A
141360SN/Aclass BufferArg : public BaseBufferArg
142360SN/A{
143360SN/A  public:
144360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1455543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
146360SN/A};
147360SN/A
148360SN/Atemplate <class T>
149360SN/Aclass TypedBufferArg : public BaseBufferArg
150360SN/A{
151360SN/A  public:
152360SN/A    // user can optionally specify a specific number of bytes to
153360SN/A    // allocate to deal with those structs that have variable-size
154360SN/A    // arrays at the end
155360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
156360SN/A        : BaseBufferArg(_addr, _size)
157360SN/A    { }
158360SN/A
159360SN/A    // type case
160360SN/A    operator T*() { return (T *)bufPtr; }
161360SN/A
162360SN/A    // dereference operators
1635543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1645543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
165502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
166360SN/A};
167360SN/A
168360SN/A//////////////////////////////////////////////////////////////////////
169360SN/A//
170360SN/A// The following emulation functions are generic enough that they
171360SN/A// don't need to be recompiled for different emulated OS's.  They are
172360SN/A// defined in sim/syscall_emul.cc.
173360SN/A//
174360SN/A//////////////////////////////////////////////////////////////////////
175360SN/A
176360SN/A
177378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1781706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1793114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
180378SN/A
181378SN/A/// Handler for unimplemented syscalls that we never intend to
182378SN/A/// implement (signal handling, etc.) and should not affect the correct
183378SN/A/// behavior of the program.  Print a warning only if the appropriate
184378SN/A/// trace flag is enabled.  Return success to the target program.
1851706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1863114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
187360SN/A
1886109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1891706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1903114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
191378SN/A
1926109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1936109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1946109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1956109Ssanchezd@stanford.edu
196378SN/A/// Target getpagesize() handler.
1971706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1983114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
199378SN/A
2005748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2015748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2025748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
203378SN/A
204378SN/A/// Target close() handler.
2051706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2063114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
207378SN/A
208378SN/A/// Target read() handler.
2091706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2103114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
211378SN/A
212378SN/A/// Target write() handler.
2131706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2143114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
215378SN/A
216378SN/A/// Target lseek() handler.
2171706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2183114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
219378SN/A
2204118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2214118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2224118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2234118Sgblack@eecs.umich.edu
224378SN/A/// Target munmap() handler.
2251706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2263114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
227378SN/A
228378SN/A/// Target gethostname() handler.
2291706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2303114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
231360SN/A
2325513SMichael.Adler@intel.com/// Target getcwd() handler.
2335513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2345513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2355513SMichael.Adler@intel.com
2365513SMichael.Adler@intel.com/// Target unlink() handler.
2375513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2385513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2395513SMichael.Adler@intel.com
240511SN/A/// Target unlink() handler.
2411706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2423114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
243511SN/A
2445513SMichael.Adler@intel.com/// Target mkdir() handler.
2455513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2465513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2475513SMichael.Adler@intel.com
248511SN/A/// Target rename() handler.
2491706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2503114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2511706SN/A
2521706SN/A
2531706SN/A/// Target truncate() handler.
2541706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2553114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2561706SN/A
2571706SN/A
2581706SN/A/// Target ftruncate() handler.
2591706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2603114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2611706SN/A
262511SN/A
2636685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2646685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2656685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2666685Stjones1@inf.ed.ac.uk
2676685Stjones1@inf.ed.ac.uk
2685513SMichael.Adler@intel.com/// Target umask() handler.
2695513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2705513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2715513SMichael.Adler@intel.com
2725513SMichael.Adler@intel.com
2731999SN/A/// Target chown() handler.
2741999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2753114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2761999SN/A
2771999SN/A
2781999SN/A/// Target fchown() handler.
2791999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2811999SN/A
2823079Sstever@eecs.umich.edu/// Target dup() handler.
2833079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2853079Sstever@eecs.umich.edu
2862093SN/A/// Target fnctl() handler.
2872093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2883114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2892093SN/A
2902687Sksewell@umich.edu/// Target fcntl64() handler.
2912687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2923114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2932687Sksewell@umich.edu
2942238SN/A/// Target setuid() handler.
2952238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2963114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2972238SN/A
2982238SN/A/// Target getpid() handler.
2992238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3003114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3012238SN/A
3022238SN/A/// Target getuid() handler.
3032238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3043114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3052238SN/A
3062238SN/A/// Target getgid() handler.
3072238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3083114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3092238SN/A
3102238SN/A/// Target getppid() handler.
3112238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3123114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3132238SN/A
3142238SN/A/// Target geteuid() handler.
3152238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3163114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3172238SN/A
3182238SN/A/// Target getegid() handler.
3192238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3203114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3212238SN/A
3226109Ssanchezd@stanford.edu/// Target clone() handler.
3236109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3246109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3252238SN/A
3262238SN/A
3272238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3282238SN/A/// returning a second value in a register other than the normal return register
3292238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3303114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3312238SN/A
3322238SN/A/// Target getpidPseudo() handler.
3332238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3343114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3352238SN/A
3362238SN/A/// Target getuidPseudo() handler.
3372238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3383114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3392238SN/A
3402238SN/A/// Target getgidPseudo() handler.
3412238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3423114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3432238SN/A
3442238SN/A
3451354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3461354SN/Aconst int one_million = 1000000;
3471354SN/A
3481354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3491354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3501354SN/A/// real-world time) to keep simulations repeatable.
3511354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3521354SN/A
3531354SN/A/// Helper function to convert current elapsed time to seconds and
3541354SN/A/// microseconds.
3551354SN/Atemplate <class T1, class T2>
3561354SN/Avoid
3571354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3581354SN/A{
3591609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3601354SN/A    sec = elapsed_usecs / one_million;
3611354SN/A    usec = elapsed_usecs % one_million;
3621354SN/A}
3631354SN/A
364360SN/A//////////////////////////////////////////////////////////////////////
365360SN/A//
366360SN/A// The following emulation functions are generic, but need to be
367360SN/A// templated to account for differences in types, constants, etc.
368360SN/A//
369360SN/A//////////////////////////////////////////////////////////////////////
370360SN/A
3713113Sgblack@eecs.umich.edu#if NO_STAT64
3723113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3733113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3743113Sgblack@eecs.umich.edu#else
3753113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3763113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3773113Sgblack@eecs.umich.edu#endif
3783113Sgblack@eecs.umich.edu
3793113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3803113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3813113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3823113Sgblack@eecs.umich.edu
3833113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3843113Sgblack@eecs.umich.edustatic void
3853113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3863113Sgblack@eecs.umich.edu{
3874189Sgblack@eecs.umich.edu    using namespace TheISA;
3884189Sgblack@eecs.umich.edu
3893113Sgblack@eecs.umich.edu    if (fakeTTY)
3903113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
3913113Sgblack@eecs.umich.edu    else
3923113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
3933113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
3943113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
3953113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
3963277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
3975515SMichael.Adler@intel.com    if (fakeTTY) {
3985515SMichael.Adler@intel.com        // Claim to be a character device
3995515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4005515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4015515SMichael.Adler@intel.com    }
4023277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
4033277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4043277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
4053277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4063277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
4073277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4083277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
4093113Sgblack@eecs.umich.edu    if (fakeTTY)
4103113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4113113Sgblack@eecs.umich.edu    else
4123113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4133113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
4143113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4153113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
4163114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4173113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
4183114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4193113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
4203114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4213113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
4224061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4234061Sgblack@eecs.umich.edu    // consistently across different hosts.
4244061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4253113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
4263113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4273113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
4283113Sgblack@eecs.umich.edu}
4293113Sgblack@eecs.umich.edu
4303113Sgblack@eecs.umich.edu// Same for stat64
4313113Sgblack@eecs.umich.edu
4323113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4333113Sgblack@eecs.umich.edustatic void
4343113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4353113Sgblack@eecs.umich.edu{
4364189Sgblack@eecs.umich.edu    using namespace TheISA;
4374189Sgblack@eecs.umich.edu
4383113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4393113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4403113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4413113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4423113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4433113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4443113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4453113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4463113Sgblack@eecs.umich.edu#else
4473113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4483113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4493113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4503113Sgblack@eecs.umich.edu#endif
4513113Sgblack@eecs.umich.edu}
4523113Sgblack@eecs.umich.edu
4533113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4543113Sgblack@eecs.umich.edutemplate<class OS>
4553113Sgblack@eecs.umich.edustatic void
4563113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4573113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4583113Sgblack@eecs.umich.edu{
4593113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4603113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4613113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4623113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4633113Sgblack@eecs.umich.edu}
4643113Sgblack@eecs.umich.edu
4653113Sgblack@eecs.umich.edutemplate<class OS>
4663113Sgblack@eecs.umich.edustatic void
4673113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4683113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4693113Sgblack@eecs.umich.edu{
4703113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4713113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4726686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4733113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4743113Sgblack@eecs.umich.edu}
4753113Sgblack@eecs.umich.edu
476378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
477378SN/A/// only to find out if their stdout is a tty, to determine whether to
478378SN/A/// do line or block buffering.
479360SN/Atemplate <class OS>
4801450SN/ASyscallReturn
4813114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4822680Sktlim@umich.edu          ThreadContext *tc)
483360SN/A{
4845958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
4855958Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, 1);
486360SN/A
4871969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
488360SN/A
489360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
490360SN/A        // doesn't map to any simulator fd: not a valid target fd
4911458SN/A        return -EBADF;
492360SN/A    }
493360SN/A
494360SN/A    switch (req) {
4954131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
4964131Sbinkertn@umich.edu      case OS::TIOCGETP_:
4974131Sbinkertn@umich.edu      case OS::TIOCSETP_:
4984131Sbinkertn@umich.edu      case OS::TIOCSETN_:
4994131Sbinkertn@umich.edu      case OS::TIOCSETC_:
5004131Sbinkertn@umich.edu      case OS::TIOCGETC_:
5014131Sbinkertn@umich.edu      case OS::TIOCGETS_:
5024131Sbinkertn@umich.edu      case OS::TIOCGETA_:
5031458SN/A        return -ENOTTY;
504360SN/A
505360SN/A      default:
5061706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
5072680Sktlim@umich.edu              fd, req, tc->readPC());
508360SN/A    }
509360SN/A}
510360SN/A
511378SN/A/// Target open() handler.
512360SN/Atemplate <class OS>
5131450SN/ASyscallReturn
5143114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5152680Sktlim@umich.edu         ThreadContext *tc)
516360SN/A{
517360SN/A    std::string path;
518360SN/A
5195958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5201458SN/A        return -EFAULT;
521360SN/A
522360SN/A    if (path == "/dev/sysdev0") {
523360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
524360SN/A        // We don't support it, so just punt.
5251706SN/A        warn("Ignoring open(%s, ...)\n", path);
5261458SN/A        return -ENOENT;
527360SN/A    }
528360SN/A
5295958Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, 1);
5305958Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, 2);
531360SN/A    int hostFlags = 0;
532360SN/A
533360SN/A    // translate open flags
534360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
535360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
536360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
537360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
538360SN/A        }
539360SN/A    }
540360SN/A
541360SN/A    // any target flags left?
542360SN/A    if (tgtFlags != 0)
5431706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
544360SN/A
545360SN/A#ifdef __CYGWIN32__
546360SN/A    hostFlags |= O_BINARY;
547360SN/A#endif
548360SN/A
5493669Sbinkertn@umich.edu    // Adjust path for current working directory
5503669Sbinkertn@umich.edu    path = process->fullPath(path);
5513669Sbinkertn@umich.edu
5521706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5531706SN/A
5545795Ssaidi@eecs.umich.edu    int fd;
5555795Ssaidi@eecs.umich.edu    if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
5565795Ssaidi@eecs.umich.edu        !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
5575795Ssaidi@eecs.umich.edu        // It's a proc/sys entery and requires special handling
5585795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
5595795Ssaidi@eecs.umich.edu        return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5605795Ssaidi@eecs.umich.edu     } else {
5615795Ssaidi@eecs.umich.edu        // open the file
5625795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
5635795Ssaidi@eecs.umich.edu        return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5645795Ssaidi@eecs.umich.edu     }
565360SN/A
566360SN/A}
567360SN/A
5686640Svince@csl.cornell.edu/// Target sysinfo() handler.
5696640Svince@csl.cornell.edutemplate <class OS>
5706640Svince@csl.cornell.eduSyscallReturn
5716640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5726640Svince@csl.cornell.edu         ThreadContext *tc)
5736640Svince@csl.cornell.edu{
5746640Svince@csl.cornell.edu
5756640Svince@csl.cornell.edu   TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0));
5766640Svince@csl.cornell.edu
5776640Svince@csl.cornell.edu   sysinfo->uptime=seconds_since_epoch;
5786640Svince@csl.cornell.edu   sysinfo->totalram=process->system->memSize();
5796640Svince@csl.cornell.edu
5806640Svince@csl.cornell.edu   sysinfo.copyOut(tc->getMemPort());
5816640Svince@csl.cornell.edu
5826640Svince@csl.cornell.edu   return 0;
5836640Svince@csl.cornell.edu}
584360SN/A
5851999SN/A/// Target chmod() handler.
5861999SN/Atemplate <class OS>
5871999SN/ASyscallReturn
5883114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5892680Sktlim@umich.edu          ThreadContext *tc)
5901999SN/A{
5911999SN/A    std::string path;
5921999SN/A
5935958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5941999SN/A        return -EFAULT;
5951999SN/A
5965958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
5971999SN/A    mode_t hostMode = 0;
5981999SN/A
5991999SN/A    // XXX translate mode flags via OS::something???
6001999SN/A    hostMode = mode;
6011999SN/A
6023669Sbinkertn@umich.edu    // Adjust path for current working directory
6033669Sbinkertn@umich.edu    path = process->fullPath(path);
6043669Sbinkertn@umich.edu
6051999SN/A    // do the chmod
6061999SN/A    int result = chmod(path.c_str(), hostMode);
6071999SN/A    if (result < 0)
6082218SN/A        return -errno;
6091999SN/A
6101999SN/A    return 0;
6111999SN/A}
6121999SN/A
6131999SN/A
6141999SN/A/// Target fchmod() handler.
6151999SN/Atemplate <class OS>
6161999SN/ASyscallReturn
6173114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6182680Sktlim@umich.edu           ThreadContext *tc)
6191999SN/A{
6205958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
6211999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6221999SN/A        // doesn't map to any simulator fd: not a valid target fd
6231999SN/A        return -EBADF;
6241999SN/A    }
6251999SN/A
6265958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
6271999SN/A    mode_t hostMode = 0;
6281999SN/A
6291999SN/A    // XXX translate mode flags via OS::someting???
6301999SN/A    hostMode = mode;
6311999SN/A
6321999SN/A    // do the fchmod
6331999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
6341999SN/A    if (result < 0)
6352218SN/A        return -errno;
6361999SN/A
6371999SN/A    return 0;
6381999SN/A}
6391999SN/A
6405877Shsul@eecs.umich.edu/// Target mremap() handler.
6415877Shsul@eecs.umich.edutemplate <class OS>
6425877Shsul@eecs.umich.eduSyscallReturn
6435877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
6445877Shsul@eecs.umich.edu{
6455958Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, 0);
6465958Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, 1);
6475958Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, 2);
6485958Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, 3);
6495877Shsul@eecs.umich.edu
6505877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
6515877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
6525877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
6535877Shsul@eecs.umich.edu        return -EINVAL;
6545877Shsul@eecs.umich.edu    }
6555877Shsul@eecs.umich.edu
6565877Shsul@eecs.umich.edu    if (new_length > old_length) {
6575877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
6585877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
6595877Shsul@eecs.umich.edu            process->pTable->allocate(process->mmap_end, diff);
6605877Shsul@eecs.umich.edu            process->mmap_end += diff;
6615877Shsul@eecs.umich.edu            return start;
6625877Shsul@eecs.umich.edu        } else {
6635877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
6645877Shsul@eecs.umich.edu            if (!(flags & 1)) {
6655877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
6665877Shsul@eecs.umich.edu                return -ENOMEM;
6675877Shsul@eecs.umich.edu            } else {
6685877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
6695877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
6705877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
6715877Shsul@eecs.umich.edu                start = process->mmap_end;
6725877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
6735877Shsul@eecs.umich.edu                process->pTable->allocate(start + old_length, new_length - old_length);
6745877Shsul@eecs.umich.edu                process->mmap_end += new_length;
6755877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
6765877Shsul@eecs.umich.edu                return start;
6775877Shsul@eecs.umich.edu            }
6785877Shsul@eecs.umich.edu        }
6795877Shsul@eecs.umich.edu    } else {
6805877Shsul@eecs.umich.edu        process->pTable->deallocate(start + new_length, old_length -
6815877Shsul@eecs.umich.edu                new_length);
6825877Shsul@eecs.umich.edu        return start;
6835877Shsul@eecs.umich.edu    }
6845877Shsul@eecs.umich.edu}
6851999SN/A
686378SN/A/// Target stat() handler.
687360SN/Atemplate <class OS>
6881450SN/ASyscallReturn
6893114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6902680Sktlim@umich.edu         ThreadContext *tc)
691360SN/A{
692360SN/A    std::string path;
693360SN/A
6945958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
6952400SN/A    return -EFAULT;
696360SN/A
6973669Sbinkertn@umich.edu    // Adjust path for current working directory
6983669Sbinkertn@umich.edu    path = process->fullPath(path);
6993669Sbinkertn@umich.edu
700360SN/A    struct stat hostBuf;
701360SN/A    int result = stat(path.c_str(), &hostBuf);
702360SN/A
703360SN/A    if (result < 0)
7042218SN/A        return -errno;
705360SN/A
7065958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7075958Sgblack@eecs.umich.edu            &hostBuf);
708360SN/A
7091458SN/A    return 0;
710360SN/A}
711360SN/A
712360SN/A
7135074Ssaidi@eecs.umich.edu/// Target stat64() handler.
7145074Ssaidi@eecs.umich.edutemplate <class OS>
7155074Ssaidi@eecs.umich.eduSyscallReturn
7165074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7175074Ssaidi@eecs.umich.edu           ThreadContext *tc)
7185074Ssaidi@eecs.umich.edu{
7195074Ssaidi@eecs.umich.edu    std::string path;
7205074Ssaidi@eecs.umich.edu
7215958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7225074Ssaidi@eecs.umich.edu        return -EFAULT;
7235074Ssaidi@eecs.umich.edu
7245074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
7255074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
7265074Ssaidi@eecs.umich.edu
7275208Ssaidi@eecs.umich.edu#if NO_STAT64
7285208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
7295208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
7305208Ssaidi@eecs.umich.edu#else
7315074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
7325074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
7335208Ssaidi@eecs.umich.edu#endif
7345074Ssaidi@eecs.umich.edu
7355074Ssaidi@eecs.umich.edu    if (result < 0)
7365074Ssaidi@eecs.umich.edu        return -errno;
7375074Ssaidi@eecs.umich.edu
7385958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7395958Sgblack@eecs.umich.edu            &hostBuf);
7405074Ssaidi@eecs.umich.edu
7415074Ssaidi@eecs.umich.edu    return 0;
7425074Ssaidi@eecs.umich.edu}
7435074Ssaidi@eecs.umich.edu
7445074Ssaidi@eecs.umich.edu
7451999SN/A/// Target fstat64() handler.
7461999SN/Atemplate <class OS>
7471999SN/ASyscallReturn
7483114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7492680Sktlim@umich.edu            ThreadContext *tc)
7501999SN/A{
7515958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
7521999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7531999SN/A        // doesn't map to any simulator fd: not a valid target fd
7541999SN/A        return -EBADF;
7551999SN/A    }
7561999SN/A
7572764Sstever@eecs.umich.edu#if NO_STAT64
7582064SN/A    struct stat  hostBuf;
7592064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
7602064SN/A#else
7612064SN/A    struct stat64  hostBuf;
7621999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
7632064SN/A#endif
7641999SN/A
7651999SN/A    if (result < 0)
7662218SN/A        return -errno;
7671999SN/A
7685958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7693114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7701999SN/A
7711999SN/A    return 0;
7721999SN/A}
7731999SN/A
7741999SN/A
775378SN/A/// Target lstat() handler.
776360SN/Atemplate <class OS>
7771450SN/ASyscallReturn
7783114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7792680Sktlim@umich.edu          ThreadContext *tc)
780360SN/A{
781360SN/A    std::string path;
782360SN/A
7835958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7842400SN/A      return -EFAULT;
785360SN/A
7863669Sbinkertn@umich.edu    // Adjust path for current working directory
7873669Sbinkertn@umich.edu    path = process->fullPath(path);
7883669Sbinkertn@umich.edu
789360SN/A    struct stat hostBuf;
790360SN/A    int result = lstat(path.c_str(), &hostBuf);
791360SN/A
792360SN/A    if (result < 0)
7931458SN/A        return -errno;
794360SN/A
7955958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7965958Sgblack@eecs.umich.edu            &hostBuf);
797360SN/A
7981458SN/A    return 0;
799360SN/A}
800360SN/A
8011999SN/A/// Target lstat64() handler.
8021999SN/Atemplate <class OS>
8031999SN/ASyscallReturn
8043114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8052680Sktlim@umich.edu            ThreadContext *tc)
8061999SN/A{
8071999SN/A    std::string path;
8081999SN/A
8095958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8102400SN/A      return -EFAULT;
8111999SN/A
8123669Sbinkertn@umich.edu    // Adjust path for current working directory
8133669Sbinkertn@umich.edu    path = process->fullPath(path);
8143669Sbinkertn@umich.edu
8152764Sstever@eecs.umich.edu#if NO_STAT64
8162064SN/A    struct stat hostBuf;
8172064SN/A    int result = lstat(path.c_str(), &hostBuf);
8182064SN/A#else
8191999SN/A    struct stat64 hostBuf;
8201999SN/A    int result = lstat64(path.c_str(), &hostBuf);
8212064SN/A#endif
8221999SN/A
8231999SN/A    if (result < 0)
8241999SN/A        return -errno;
8251999SN/A
8265958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8275958Sgblack@eecs.umich.edu            &hostBuf);
8281999SN/A
8291999SN/A    return 0;
8301999SN/A}
8311999SN/A
832378SN/A/// Target fstat() handler.
833360SN/Atemplate <class OS>
8341450SN/ASyscallReturn
8353114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8362680Sktlim@umich.edu          ThreadContext *tc)
837360SN/A{
8385958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
839360SN/A
8401969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
841360SN/A
842360SN/A    if (fd < 0)
8431458SN/A        return -EBADF;
844360SN/A
845360SN/A    struct stat hostBuf;
846360SN/A    int result = fstat(fd, &hostBuf);
847360SN/A
848360SN/A    if (result < 0)
8491458SN/A        return -errno;
850360SN/A
8515958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8523114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
8532021SN/A
8541458SN/A    return 0;
855360SN/A}
856360SN/A
857360SN/A
8581706SN/A/// Target statfs() handler.
8591706SN/Atemplate <class OS>
8601706SN/ASyscallReturn
8613114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8622680Sktlim@umich.edu           ThreadContext *tc)
8631706SN/A{
8641706SN/A    std::string path;
8651706SN/A
8665958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8672400SN/A      return -EFAULT;
8681706SN/A
8693669Sbinkertn@umich.edu    // Adjust path for current working directory
8703669Sbinkertn@umich.edu    path = process->fullPath(path);
8713669Sbinkertn@umich.edu
8721706SN/A    struct statfs hostBuf;
8731706SN/A    int result = statfs(path.c_str(), &hostBuf);
8741706SN/A
8751706SN/A    if (result < 0)
8762218SN/A        return -errno;
8771706SN/A
8783114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
8795958Sgblack@eecs.umich.edu            (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
8801706SN/A
8811706SN/A    return 0;
8821706SN/A}
8831706SN/A
8841706SN/A
8851706SN/A/// Target fstatfs() handler.
8861706SN/Atemplate <class OS>
8871706SN/ASyscallReturn
8883114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8892680Sktlim@umich.edu            ThreadContext *tc)
8901706SN/A{
8915958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
8921706SN/A
8931706SN/A    if (fd < 0)
8941706SN/A        return -EBADF;
8951706SN/A
8961706SN/A    struct statfs hostBuf;
8971706SN/A    int result = fstatfs(fd, &hostBuf);
8981706SN/A
8991706SN/A    if (result < 0)
9002218SN/A        return -errno;
9011706SN/A
9025958Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
9033114Sgblack@eecs.umich.edu        &hostBuf);
9041706SN/A
9051706SN/A    return 0;
9061706SN/A}
9071706SN/A
9081706SN/A
9091999SN/A/// Target writev() handler.
9101999SN/Atemplate <class OS>
9111999SN/ASyscallReturn
9123114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9132680Sktlim@umich.edu           ThreadContext *tc)
9141999SN/A{
9155958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
9161999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9171999SN/A        // doesn't map to any simulator fd: not a valid target fd
9181999SN/A        return -EBADF;
9191999SN/A    }
9201999SN/A
9212680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
9225958Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, 1);
9235958Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, 2);
9241999SN/A    struct iovec hiov[count];
9256227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
9261999SN/A        typename OS::tgt_iovec tiov;
9272461SN/A
9282461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
9292461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
9302091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
9311999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
9322461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
9332461SN/A                    hiov[i].iov_len);
9341999SN/A    }
9351999SN/A
9361999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
9371999SN/A
9386227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
9391999SN/A        delete [] (char *)hiov[i].iov_base;
9401999SN/A
9411999SN/A    if (result < 0)
9422218SN/A        return -errno;
9431999SN/A
9441999SN/A    return 0;
9451999SN/A}
9461999SN/A
9471999SN/A
948378SN/A/// Target mmap() handler.
949378SN/A///
950378SN/A/// We don't really handle mmap().  If the target is mmaping an
951378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
952378SN/A/// nothing (since memory is initialized to zero and the simulator
953378SN/A/// doesn't really check addresses anyway).  Always print a warning,
954378SN/A/// since this could be seriously broken if we're not mapping
955378SN/A/// /dev/zero.
956360SN/A//
957378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
958378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
959378SN/A/// anything else.
960360SN/Atemplate <class OS>
9611450SN/ASyscallReturn
9623114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
963360SN/A{
9645958Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, 0);
9655958Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, 1);
9665958Sgblack@eecs.umich.edu    // int prot = p->getSyscallArg(tc, 2);
9675958Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, 3);
9685958Sgblack@eecs.umich.edu    // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
9695958Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, 5);
970360SN/A
9715877Shsul@eecs.umich.edu
9722544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
9732544SN/A        (length % TheISA::VMPageSize) != 0) {
9742544SN/A        warn("mmap failing: arguments not page-aligned: "
9752544SN/A             "start 0x%x length 0x%x",
9762544SN/A             start, length);
9772544SN/A        return -EINVAL;
978360SN/A    }
979360SN/A
9802544SN/A    if (start != 0) {
9812544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
9822544SN/A             start, p->mmap_end);
9832544SN/A    }
9842544SN/A
9852544SN/A    // pick next address from our "mmap region"
9866672Sgblack@eecs.umich.edu    if (OS::mmapGrowsDown()) {
9876672Sgblack@eecs.umich.edu        start = p->mmap_end - length;
9886672Sgblack@eecs.umich.edu        p->mmap_end = start;
9896672Sgblack@eecs.umich.edu    } else {
9906672Sgblack@eecs.umich.edu        start = p->mmap_end;
9916672Sgblack@eecs.umich.edu        p->mmap_end += length;
9926672Sgblack@eecs.umich.edu    }
9932544SN/A    p->pTable->allocate(start, length);
9942544SN/A
9952553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
9961969SN/A        warn("allowing mmap of file @ fd %d. "
9975958Sgblack@eecs.umich.edu             "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
998360SN/A    }
999360SN/A
10001458SN/A    return start;
1001360SN/A}
1002360SN/A
1003378SN/A/// Target getrlimit() handler.
1004360SN/Atemplate <class OS>
10051450SN/ASyscallReturn
10063114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10072680Sktlim@umich.edu        ThreadContext *tc)
1008360SN/A{
10095958Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, 0);
10105958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
1011360SN/A
1012360SN/A    switch (resource) {
10132064SN/A        case OS::TGT_RLIMIT_STACK:
10145877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
10152064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
10162091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
10172091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
10182064SN/A            break;
1019360SN/A
10205877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
10215877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
10225877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
10235877Shsul@eecs.umich.edu            rlp->rlim_cur = htog(rlp->rlim_cur);
10245877Shsul@eecs.umich.edu            rlp->rlim_max = htog(rlp->rlim_max);
10255877Shsul@eecs.umich.edu            break;
10265877Shsul@eecs.umich.edu
10272064SN/A        default:
10282064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
10292064SN/A                << std::endl;
10302064SN/A            abort();
10312064SN/A            break;
1032360SN/A    }
1033360SN/A
10342680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
10351458SN/A    return 0;
1036360SN/A}
1037360SN/A
1038378SN/A/// Target gettimeofday() handler.
1039360SN/Atemplate <class OS>
10401450SN/ASyscallReturn
10413114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10422680Sktlim@umich.edu        ThreadContext *tc)
1043360SN/A{
10445958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1045360SN/A
1046360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1047360SN/A    tp->tv_sec += seconds_since_epoch;
10486109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
10496109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1050360SN/A
10512680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
1052360SN/A
10531458SN/A    return 0;
1054360SN/A}
1055360SN/A
1056360SN/A
10571999SN/A/// Target utimes() handler.
10581999SN/Atemplate <class OS>
10591999SN/ASyscallReturn
10603114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10612680Sktlim@umich.edu           ThreadContext *tc)
10621999SN/A{
10631999SN/A    std::string path;
10641999SN/A
10655958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
10662400SN/A      return -EFAULT;
10671999SN/A
10685958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
10692680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
10701999SN/A
10711999SN/A    struct timeval hostTimeval[2];
10721999SN/A    for (int i = 0; i < 2; ++i)
10731999SN/A    {
10742091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
10752091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
10761999SN/A    }
10773669Sbinkertn@umich.edu
10783669Sbinkertn@umich.edu    // Adjust path for current working directory
10793669Sbinkertn@umich.edu    path = process->fullPath(path);
10803669Sbinkertn@umich.edu
10811999SN/A    int result = utimes(path.c_str(), hostTimeval);
10821999SN/A
10831999SN/A    if (result < 0)
10841999SN/A        return -errno;
10851999SN/A
10861999SN/A    return 0;
10871999SN/A}
1088378SN/A/// Target getrusage() function.
1089360SN/Atemplate <class OS>
10901450SN/ASyscallReturn
10913114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10922680Sktlim@umich.edu              ThreadContext *tc)
1093360SN/A{
10945958Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, 0);     // THREAD, SELF, or CHILDREN
10955958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1096360SN/A
10973670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
10983670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1099360SN/A    rup->ru_stime.tv_sec = 0;
1100360SN/A    rup->ru_stime.tv_usec = 0;
1101360SN/A    rup->ru_maxrss = 0;
1102360SN/A    rup->ru_ixrss = 0;
1103360SN/A    rup->ru_idrss = 0;
1104360SN/A    rup->ru_isrss = 0;
1105360SN/A    rup->ru_minflt = 0;
1106360SN/A    rup->ru_majflt = 0;
1107360SN/A    rup->ru_nswap = 0;
1108360SN/A    rup->ru_inblock = 0;
1109360SN/A    rup->ru_oublock = 0;
1110360SN/A    rup->ru_msgsnd = 0;
1111360SN/A    rup->ru_msgrcv = 0;
1112360SN/A    rup->ru_nsignals = 0;
1113360SN/A    rup->ru_nvcsw = 0;
1114360SN/A    rup->ru_nivcsw = 0;
1115360SN/A
11163670Sbinkertn@umich.edu    switch (who) {
11173670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
11183670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
11193670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
11203670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
11213670Sbinkertn@umich.edu        break;
11223670Sbinkertn@umich.edu
11233670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
11243670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
11253670Sbinkertn@umich.edu        break;
11263670Sbinkertn@umich.edu
11273670Sbinkertn@umich.edu      default:
11283670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
11293670Sbinkertn@umich.edu        // plow ahead
11303670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
11313670Sbinkertn@umich.edu             who);
11323670Sbinkertn@umich.edu    }
11333670Sbinkertn@umich.edu
11342680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1135360SN/A
11361458SN/A    return 0;
1137360SN/A}
1138360SN/A
11396683Stjones1@inf.ed.ac.uk/// Target times() function.
11406683Stjones1@inf.ed.ac.uktemplate <class OS>
11416683Stjones1@inf.ed.ac.ukSyscallReturn
11426683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11436683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
11446683Stjones1@inf.ed.ac.uk{
11456683Stjones1@inf.ed.ac.uk    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, 0));
11466683Stjones1@inf.ed.ac.uk
11476683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
11486683Stjones1@inf.ed.ac.uk    int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s;
11496683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
11506683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
11516683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
11526683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
11536683Stjones1@inf.ed.ac.uk
11546683Stjones1@inf.ed.ac.uk    // Convert to host endianness
11556683Stjones1@inf.ed.ac.uk    bufp->tms_utime = htog(bufp->tms_utime);
11566683Stjones1@inf.ed.ac.uk
11576683Stjones1@inf.ed.ac.uk    // Write back
11586683Stjones1@inf.ed.ac.uk    bufp.copyOut(tc->getMemPort());
11596683Stjones1@inf.ed.ac.uk
11606683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
11616683Stjones1@inf.ed.ac.uk    return clocks;
11626683Stjones1@inf.ed.ac.uk}
11632553SN/A
11646684Stjones1@inf.ed.ac.uk/// Target time() function.
11656684Stjones1@inf.ed.ac.uktemplate <class OS>
11666684Stjones1@inf.ed.ac.ukSyscallReturn
11676684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11686684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
11696684Stjones1@inf.ed.ac.uk{
11706684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
11716684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
11726684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
11736684Stjones1@inf.ed.ac.uk
11746684Stjones1@inf.ed.ac.uk    Addr taddr = (Addr)process->getSyscallArg(tc, 0);
11756684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
11766684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
11776684Stjones1@inf.ed.ac.uk        t = htog(t);
11786684Stjones1@inf.ed.ac.uk        TranslatingPort *p = tc->getMemPort();
11796684Stjones1@inf.ed.ac.uk        p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
11806684Stjones1@inf.ed.ac.uk    }
11816684Stjones1@inf.ed.ac.uk    return sec;
11826684Stjones1@inf.ed.ac.uk}
11832553SN/A
11842553SN/A
11851354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1186