syscall_emul.hh revision 6689
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_:
5036689Stjones1@inf.ed.ac.uk      case OS::TCSETAW_:
5041458SN/A        return -ENOTTY;
505360SN/A
506360SN/A      default:
5071706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
5082680Sktlim@umich.edu              fd, req, tc->readPC());
509360SN/A    }
510360SN/A}
511360SN/A
512378SN/A/// Target open() handler.
513360SN/Atemplate <class OS>
5141450SN/ASyscallReturn
5153114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5162680Sktlim@umich.edu         ThreadContext *tc)
517360SN/A{
518360SN/A    std::string path;
519360SN/A
5205958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5211458SN/A        return -EFAULT;
522360SN/A
523360SN/A    if (path == "/dev/sysdev0") {
524360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
525360SN/A        // We don't support it, so just punt.
5261706SN/A        warn("Ignoring open(%s, ...)\n", path);
5271458SN/A        return -ENOENT;
528360SN/A    }
529360SN/A
5305958Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, 1);
5315958Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, 2);
532360SN/A    int hostFlags = 0;
533360SN/A
534360SN/A    // translate open flags
535360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
536360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
537360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
538360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
539360SN/A        }
540360SN/A    }
541360SN/A
542360SN/A    // any target flags left?
543360SN/A    if (tgtFlags != 0)
5441706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
545360SN/A
546360SN/A#ifdef __CYGWIN32__
547360SN/A    hostFlags |= O_BINARY;
548360SN/A#endif
549360SN/A
5503669Sbinkertn@umich.edu    // Adjust path for current working directory
5513669Sbinkertn@umich.edu    path = process->fullPath(path);
5523669Sbinkertn@umich.edu
5531706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5541706SN/A
5555795Ssaidi@eecs.umich.edu    int fd;
5565795Ssaidi@eecs.umich.edu    if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
5575795Ssaidi@eecs.umich.edu        !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
5585795Ssaidi@eecs.umich.edu        // It's a proc/sys entery and requires special handling
5595795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
5605795Ssaidi@eecs.umich.edu        return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5615795Ssaidi@eecs.umich.edu     } else {
5625795Ssaidi@eecs.umich.edu        // open the file
5635795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
5645795Ssaidi@eecs.umich.edu        return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5655795Ssaidi@eecs.umich.edu     }
566360SN/A
567360SN/A}
568360SN/A
5696640Svince@csl.cornell.edu/// Target sysinfo() handler.
5706640Svince@csl.cornell.edutemplate <class OS>
5716640Svince@csl.cornell.eduSyscallReturn
5726640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5736640Svince@csl.cornell.edu         ThreadContext *tc)
5746640Svince@csl.cornell.edu{
5756640Svince@csl.cornell.edu
5766640Svince@csl.cornell.edu   TypedBufferArg<typename OS::tgt_sysinfo> sysinfo(process->getSyscallArg(tc, 0));
5776640Svince@csl.cornell.edu
5786640Svince@csl.cornell.edu   sysinfo->uptime=seconds_since_epoch;
5796640Svince@csl.cornell.edu   sysinfo->totalram=process->system->memSize();
5806640Svince@csl.cornell.edu
5816640Svince@csl.cornell.edu   sysinfo.copyOut(tc->getMemPort());
5826640Svince@csl.cornell.edu
5836640Svince@csl.cornell.edu   return 0;
5846640Svince@csl.cornell.edu}
585360SN/A
5861999SN/A/// Target chmod() handler.
5871999SN/Atemplate <class OS>
5881999SN/ASyscallReturn
5893114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5902680Sktlim@umich.edu          ThreadContext *tc)
5911999SN/A{
5921999SN/A    std::string path;
5931999SN/A
5945958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
5951999SN/A        return -EFAULT;
5961999SN/A
5975958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
5981999SN/A    mode_t hostMode = 0;
5991999SN/A
6001999SN/A    // XXX translate mode flags via OS::something???
6011999SN/A    hostMode = mode;
6021999SN/A
6033669Sbinkertn@umich.edu    // Adjust path for current working directory
6043669Sbinkertn@umich.edu    path = process->fullPath(path);
6053669Sbinkertn@umich.edu
6061999SN/A    // do the chmod
6071999SN/A    int result = chmod(path.c_str(), hostMode);
6081999SN/A    if (result < 0)
6092218SN/A        return -errno;
6101999SN/A
6111999SN/A    return 0;
6121999SN/A}
6131999SN/A
6141999SN/A
6151999SN/A/// Target fchmod() handler.
6161999SN/Atemplate <class OS>
6171999SN/ASyscallReturn
6183114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6192680Sktlim@umich.edu           ThreadContext *tc)
6201999SN/A{
6215958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
6221999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6231999SN/A        // doesn't map to any simulator fd: not a valid target fd
6241999SN/A        return -EBADF;
6251999SN/A    }
6261999SN/A
6275958Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, 1);
6281999SN/A    mode_t hostMode = 0;
6291999SN/A
6301999SN/A    // XXX translate mode flags via OS::someting???
6311999SN/A    hostMode = mode;
6321999SN/A
6331999SN/A    // do the fchmod
6341999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
6351999SN/A    if (result < 0)
6362218SN/A        return -errno;
6371999SN/A
6381999SN/A    return 0;
6391999SN/A}
6401999SN/A
6415877Shsul@eecs.umich.edu/// Target mremap() handler.
6425877Shsul@eecs.umich.edutemplate <class OS>
6435877Shsul@eecs.umich.eduSyscallReturn
6445877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
6455877Shsul@eecs.umich.edu{
6465958Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, 0);
6475958Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, 1);
6485958Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, 2);
6495958Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, 3);
6505877Shsul@eecs.umich.edu
6515877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
6525877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
6535877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
6545877Shsul@eecs.umich.edu        return -EINVAL;
6555877Shsul@eecs.umich.edu    }
6565877Shsul@eecs.umich.edu
6575877Shsul@eecs.umich.edu    if (new_length > old_length) {
6585877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
6595877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
6605877Shsul@eecs.umich.edu            process->pTable->allocate(process->mmap_end, diff);
6615877Shsul@eecs.umich.edu            process->mmap_end += diff;
6625877Shsul@eecs.umich.edu            return start;
6635877Shsul@eecs.umich.edu        } else {
6645877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
6655877Shsul@eecs.umich.edu            if (!(flags & 1)) {
6665877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
6675877Shsul@eecs.umich.edu                return -ENOMEM;
6685877Shsul@eecs.umich.edu            } else {
6695877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
6705877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
6715877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
6725877Shsul@eecs.umich.edu                start = process->mmap_end;
6735877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
6745877Shsul@eecs.umich.edu                process->pTable->allocate(start + old_length, new_length - old_length);
6755877Shsul@eecs.umich.edu                process->mmap_end += new_length;
6765877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
6775877Shsul@eecs.umich.edu                return start;
6785877Shsul@eecs.umich.edu            }
6795877Shsul@eecs.umich.edu        }
6805877Shsul@eecs.umich.edu    } else {
6815877Shsul@eecs.umich.edu        process->pTable->deallocate(start + new_length, old_length -
6825877Shsul@eecs.umich.edu                new_length);
6835877Shsul@eecs.umich.edu        return start;
6845877Shsul@eecs.umich.edu    }
6855877Shsul@eecs.umich.edu}
6861999SN/A
687378SN/A/// Target stat() handler.
688360SN/Atemplate <class OS>
6891450SN/ASyscallReturn
6903114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6912680Sktlim@umich.edu         ThreadContext *tc)
692360SN/A{
693360SN/A    std::string path;
694360SN/A
6955958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
6962400SN/A    return -EFAULT;
697360SN/A
6983669Sbinkertn@umich.edu    // Adjust path for current working directory
6993669Sbinkertn@umich.edu    path = process->fullPath(path);
7003669Sbinkertn@umich.edu
701360SN/A    struct stat hostBuf;
702360SN/A    int result = stat(path.c_str(), &hostBuf);
703360SN/A
704360SN/A    if (result < 0)
7052218SN/A        return -errno;
706360SN/A
7075958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7085958Sgblack@eecs.umich.edu            &hostBuf);
709360SN/A
7101458SN/A    return 0;
711360SN/A}
712360SN/A
713360SN/A
7145074Ssaidi@eecs.umich.edu/// Target stat64() handler.
7155074Ssaidi@eecs.umich.edutemplate <class OS>
7165074Ssaidi@eecs.umich.eduSyscallReturn
7175074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7185074Ssaidi@eecs.umich.edu           ThreadContext *tc)
7195074Ssaidi@eecs.umich.edu{
7205074Ssaidi@eecs.umich.edu    std::string path;
7215074Ssaidi@eecs.umich.edu
7225958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7235074Ssaidi@eecs.umich.edu        return -EFAULT;
7245074Ssaidi@eecs.umich.edu
7255074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
7265074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
7275074Ssaidi@eecs.umich.edu
7285208Ssaidi@eecs.umich.edu#if NO_STAT64
7295208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
7305208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
7315208Ssaidi@eecs.umich.edu#else
7325074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
7335074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
7345208Ssaidi@eecs.umich.edu#endif
7355074Ssaidi@eecs.umich.edu
7365074Ssaidi@eecs.umich.edu    if (result < 0)
7375074Ssaidi@eecs.umich.edu        return -errno;
7385074Ssaidi@eecs.umich.edu
7395958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7405958Sgblack@eecs.umich.edu            &hostBuf);
7415074Ssaidi@eecs.umich.edu
7425074Ssaidi@eecs.umich.edu    return 0;
7435074Ssaidi@eecs.umich.edu}
7445074Ssaidi@eecs.umich.edu
7455074Ssaidi@eecs.umich.edu
7461999SN/A/// Target fstat64() handler.
7471999SN/Atemplate <class OS>
7481999SN/ASyscallReturn
7493114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7502680Sktlim@umich.edu            ThreadContext *tc)
7511999SN/A{
7525958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
7531999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7541999SN/A        // doesn't map to any simulator fd: not a valid target fd
7551999SN/A        return -EBADF;
7561999SN/A    }
7571999SN/A
7582764Sstever@eecs.umich.edu#if NO_STAT64
7592064SN/A    struct stat  hostBuf;
7602064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
7612064SN/A#else
7622064SN/A    struct stat64  hostBuf;
7631999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
7642064SN/A#endif
7651999SN/A
7661999SN/A    if (result < 0)
7672218SN/A        return -errno;
7681999SN/A
7695958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7703114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
7711999SN/A
7721999SN/A    return 0;
7731999SN/A}
7741999SN/A
7751999SN/A
776378SN/A/// Target lstat() handler.
777360SN/Atemplate <class OS>
7781450SN/ASyscallReturn
7793114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7802680Sktlim@umich.edu          ThreadContext *tc)
781360SN/A{
782360SN/A    std::string path;
783360SN/A
7845958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
7852400SN/A      return -EFAULT;
786360SN/A
7873669Sbinkertn@umich.edu    // Adjust path for current working directory
7883669Sbinkertn@umich.edu    path = process->fullPath(path);
7893669Sbinkertn@umich.edu
790360SN/A    struct stat hostBuf;
791360SN/A    int result = lstat(path.c_str(), &hostBuf);
792360SN/A
793360SN/A    if (result < 0)
7941458SN/A        return -errno;
795360SN/A
7965958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
7975958Sgblack@eecs.umich.edu            &hostBuf);
798360SN/A
7991458SN/A    return 0;
800360SN/A}
801360SN/A
8021999SN/A/// Target lstat64() handler.
8031999SN/Atemplate <class OS>
8041999SN/ASyscallReturn
8053114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8062680Sktlim@umich.edu            ThreadContext *tc)
8071999SN/A{
8081999SN/A    std::string path;
8091999SN/A
8105958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8112400SN/A      return -EFAULT;
8121999SN/A
8133669Sbinkertn@umich.edu    // Adjust path for current working directory
8143669Sbinkertn@umich.edu    path = process->fullPath(path);
8153669Sbinkertn@umich.edu
8162764Sstever@eecs.umich.edu#if NO_STAT64
8172064SN/A    struct stat hostBuf;
8182064SN/A    int result = lstat(path.c_str(), &hostBuf);
8192064SN/A#else
8201999SN/A    struct stat64 hostBuf;
8211999SN/A    int result = lstat64(path.c_str(), &hostBuf);
8222064SN/A#endif
8231999SN/A
8241999SN/A    if (result < 0)
8251999SN/A        return -errno;
8261999SN/A
8275958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8285958Sgblack@eecs.umich.edu            &hostBuf);
8291999SN/A
8301999SN/A    return 0;
8311999SN/A}
8321999SN/A
833378SN/A/// Target fstat() handler.
834360SN/Atemplate <class OS>
8351450SN/ASyscallReturn
8363114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8372680Sktlim@umich.edu          ThreadContext *tc)
838360SN/A{
8395958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
840360SN/A
8411969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
842360SN/A
843360SN/A    if (fd < 0)
8441458SN/A        return -EBADF;
845360SN/A
846360SN/A    struct stat hostBuf;
847360SN/A    int result = fstat(fd, &hostBuf);
848360SN/A
849360SN/A    if (result < 0)
8501458SN/A        return -errno;
851360SN/A
8525958Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), process->getSyscallArg(tc, 1),
8533114Sgblack@eecs.umich.edu        &hostBuf, (fd == 1));
8542021SN/A
8551458SN/A    return 0;
856360SN/A}
857360SN/A
858360SN/A
8591706SN/A/// Target statfs() handler.
8601706SN/Atemplate <class OS>
8611706SN/ASyscallReturn
8623114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8632680Sktlim@umich.edu           ThreadContext *tc)
8641706SN/A{
8651706SN/A    std::string path;
8661706SN/A
8675958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
8682400SN/A      return -EFAULT;
8691706SN/A
8703669Sbinkertn@umich.edu    // Adjust path for current working directory
8713669Sbinkertn@umich.edu    path = process->fullPath(path);
8723669Sbinkertn@umich.edu
8731706SN/A    struct statfs hostBuf;
8741706SN/A    int result = statfs(path.c_str(), &hostBuf);
8751706SN/A
8761706SN/A    if (result < 0)
8772218SN/A        return -errno;
8781706SN/A
8793114Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(),
8805958Sgblack@eecs.umich.edu            (Addr)(process->getSyscallArg(tc, 1)), &hostBuf);
8811706SN/A
8821706SN/A    return 0;
8831706SN/A}
8841706SN/A
8851706SN/A
8861706SN/A/// Target fstatfs() handler.
8871706SN/Atemplate <class OS>
8881706SN/ASyscallReturn
8893114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8902680Sktlim@umich.edu            ThreadContext *tc)
8911706SN/A{
8925958Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, 0));
8931706SN/A
8941706SN/A    if (fd < 0)
8951706SN/A        return -EBADF;
8961706SN/A
8971706SN/A    struct statfs hostBuf;
8981706SN/A    int result = fstatfs(fd, &hostBuf);
8991706SN/A
9001706SN/A    if (result < 0)
9012218SN/A        return -errno;
9021706SN/A
9035958Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), process->getSyscallArg(tc, 1),
9043114Sgblack@eecs.umich.edu        &hostBuf);
9051706SN/A
9061706SN/A    return 0;
9071706SN/A}
9081706SN/A
9091706SN/A
9101999SN/A/// Target writev() handler.
9111999SN/Atemplate <class OS>
9121999SN/ASyscallReturn
9133114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9142680Sktlim@umich.edu           ThreadContext *tc)
9151999SN/A{
9165958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, 0);
9171999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9181999SN/A        // doesn't map to any simulator fd: not a valid target fd
9191999SN/A        return -EBADF;
9201999SN/A    }
9211999SN/A
9222680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
9235958Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, 1);
9245958Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, 2);
9251999SN/A    struct iovec hiov[count];
9266227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
9271999SN/A        typename OS::tgt_iovec tiov;
9282461SN/A
9292461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
9302461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
9312091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
9321999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
9332461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
9342461SN/A                    hiov[i].iov_len);
9351999SN/A    }
9361999SN/A
9371999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
9381999SN/A
9396227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
9401999SN/A        delete [] (char *)hiov[i].iov_base;
9411999SN/A
9421999SN/A    if (result < 0)
9432218SN/A        return -errno;
9441999SN/A
9451999SN/A    return 0;
9461999SN/A}
9471999SN/A
9481999SN/A
949378SN/A/// Target mmap() handler.
950378SN/A///
951378SN/A/// We don't really handle mmap().  If the target is mmaping an
952378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
953378SN/A/// nothing (since memory is initialized to zero and the simulator
954378SN/A/// doesn't really check addresses anyway).  Always print a warning,
955378SN/A/// since this could be seriously broken if we're not mapping
956378SN/A/// /dev/zero.
957360SN/A//
958378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
959378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
960378SN/A/// anything else.
961360SN/Atemplate <class OS>
9621450SN/ASyscallReturn
9633114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
964360SN/A{
9655958Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, 0);
9665958Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, 1);
9675958Sgblack@eecs.umich.edu    // int prot = p->getSyscallArg(tc, 2);
9685958Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, 3);
9695958Sgblack@eecs.umich.edu    // int fd = p->sim_fd(p->getSyscallArg(tc, 4));
9705958Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, 5);
971360SN/A
9725877Shsul@eecs.umich.edu
9732544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
9742544SN/A        (length % TheISA::VMPageSize) != 0) {
9752544SN/A        warn("mmap failing: arguments not page-aligned: "
9762544SN/A             "start 0x%x length 0x%x",
9772544SN/A             start, length);
9782544SN/A        return -EINVAL;
979360SN/A    }
980360SN/A
9812544SN/A    if (start != 0) {
9822544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
9832544SN/A             start, p->mmap_end);
9842544SN/A    }
9852544SN/A
9862544SN/A    // pick next address from our "mmap region"
9876672Sgblack@eecs.umich.edu    if (OS::mmapGrowsDown()) {
9886672Sgblack@eecs.umich.edu        start = p->mmap_end - length;
9896672Sgblack@eecs.umich.edu        p->mmap_end = start;
9906672Sgblack@eecs.umich.edu    } else {
9916672Sgblack@eecs.umich.edu        start = p->mmap_end;
9926672Sgblack@eecs.umich.edu        p->mmap_end += length;
9936672Sgblack@eecs.umich.edu    }
9942544SN/A    p->pTable->allocate(start, length);
9952544SN/A
9962553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
9971969SN/A        warn("allowing mmap of file @ fd %d. "
9985958Sgblack@eecs.umich.edu             "This will break if not /dev/zero.", p->getSyscallArg(tc, 4));
999360SN/A    }
1000360SN/A
10011458SN/A    return start;
1002360SN/A}
1003360SN/A
1004378SN/A/// Target getrlimit() handler.
1005360SN/Atemplate <class OS>
10061450SN/ASyscallReturn
10073114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10082680Sktlim@umich.edu        ThreadContext *tc)
1009360SN/A{
10105958Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, 0);
10115958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, 1));
1012360SN/A
1013360SN/A    switch (resource) {
10142064SN/A        case OS::TGT_RLIMIT_STACK:
10155877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
10162064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
10172091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
10182091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
10192064SN/A            break;
1020360SN/A
10215877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
10225877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
10235877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
10245877Shsul@eecs.umich.edu            rlp->rlim_cur = htog(rlp->rlim_cur);
10255877Shsul@eecs.umich.edu            rlp->rlim_max = htog(rlp->rlim_max);
10265877Shsul@eecs.umich.edu            break;
10275877Shsul@eecs.umich.edu
10282064SN/A        default:
10292064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
10302064SN/A                << std::endl;
10312064SN/A            abort();
10322064SN/A            break;
1033360SN/A    }
1034360SN/A
10352680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
10361458SN/A    return 0;
1037360SN/A}
1038360SN/A
1039378SN/A/// Target gettimeofday() handler.
1040360SN/Atemplate <class OS>
10411450SN/ASyscallReturn
10423114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10432680Sktlim@umich.edu        ThreadContext *tc)
1044360SN/A{
10455958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, 0));
1046360SN/A
1047360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1048360SN/A    tp->tv_sec += seconds_since_epoch;
10496109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
10506109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1051360SN/A
10522680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
1053360SN/A
10541458SN/A    return 0;
1055360SN/A}
1056360SN/A
1057360SN/A
10581999SN/A/// Target utimes() handler.
10591999SN/Atemplate <class OS>
10601999SN/ASyscallReturn
10613114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10622680Sktlim@umich.edu           ThreadContext *tc)
10631999SN/A{
10641999SN/A    std::string path;
10651999SN/A
10665958Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path, process->getSyscallArg(tc, 0)))
10672400SN/A      return -EFAULT;
10681999SN/A
10695958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(process->getSyscallArg(tc, 1));
10702680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
10711999SN/A
10721999SN/A    struct timeval hostTimeval[2];
10731999SN/A    for (int i = 0; i < 2; ++i)
10741999SN/A    {
10752091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
10762091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
10771999SN/A    }
10783669Sbinkertn@umich.edu
10793669Sbinkertn@umich.edu    // Adjust path for current working directory
10803669Sbinkertn@umich.edu    path = process->fullPath(path);
10813669Sbinkertn@umich.edu
10821999SN/A    int result = utimes(path.c_str(), hostTimeval);
10831999SN/A
10841999SN/A    if (result < 0)
10851999SN/A        return -errno;
10861999SN/A
10871999SN/A    return 0;
10881999SN/A}
1089378SN/A/// Target getrusage() function.
1090360SN/Atemplate <class OS>
10911450SN/ASyscallReturn
10923114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10932680Sktlim@umich.edu              ThreadContext *tc)
1094360SN/A{
10955958Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, 0);     // THREAD, SELF, or CHILDREN
10965958Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, 1));
1097360SN/A
10983670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
10993670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1100360SN/A    rup->ru_stime.tv_sec = 0;
1101360SN/A    rup->ru_stime.tv_usec = 0;
1102360SN/A    rup->ru_maxrss = 0;
1103360SN/A    rup->ru_ixrss = 0;
1104360SN/A    rup->ru_idrss = 0;
1105360SN/A    rup->ru_isrss = 0;
1106360SN/A    rup->ru_minflt = 0;
1107360SN/A    rup->ru_majflt = 0;
1108360SN/A    rup->ru_nswap = 0;
1109360SN/A    rup->ru_inblock = 0;
1110360SN/A    rup->ru_oublock = 0;
1111360SN/A    rup->ru_msgsnd = 0;
1112360SN/A    rup->ru_msgrcv = 0;
1113360SN/A    rup->ru_nsignals = 0;
1114360SN/A    rup->ru_nvcsw = 0;
1115360SN/A    rup->ru_nivcsw = 0;
1116360SN/A
11173670Sbinkertn@umich.edu    switch (who) {
11183670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
11193670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
11203670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
11213670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
11223670Sbinkertn@umich.edu        break;
11233670Sbinkertn@umich.edu
11243670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
11253670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
11263670Sbinkertn@umich.edu        break;
11273670Sbinkertn@umich.edu
11283670Sbinkertn@umich.edu      default:
11293670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
11303670Sbinkertn@umich.edu        // plow ahead
11313670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
11323670Sbinkertn@umich.edu             who);
11333670Sbinkertn@umich.edu    }
11343670Sbinkertn@umich.edu
11352680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1136360SN/A
11371458SN/A    return 0;
1138360SN/A}
1139360SN/A
11406683Stjones1@inf.ed.ac.uk/// Target times() function.
11416683Stjones1@inf.ed.ac.uktemplate <class OS>
11426683Stjones1@inf.ed.ac.ukSyscallReturn
11436683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11446683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
11456683Stjones1@inf.ed.ac.uk{
11466683Stjones1@inf.ed.ac.uk    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, 0));
11476683Stjones1@inf.ed.ac.uk
11486683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
11496683Stjones1@inf.ed.ac.uk    int64_t clocks = curTick * OS::_SC_CLK_TCK / Clock::Int::s;
11506683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
11516683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
11526683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
11536683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
11546683Stjones1@inf.ed.ac.uk
11556683Stjones1@inf.ed.ac.uk    // Convert to host endianness
11566683Stjones1@inf.ed.ac.uk    bufp->tms_utime = htog(bufp->tms_utime);
11576683Stjones1@inf.ed.ac.uk
11586683Stjones1@inf.ed.ac.uk    // Write back
11596683Stjones1@inf.ed.ac.uk    bufp.copyOut(tc->getMemPort());
11606683Stjones1@inf.ed.ac.uk
11616683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
11626683Stjones1@inf.ed.ac.uk    return clocks;
11636683Stjones1@inf.ed.ac.uk}
11642553SN/A
11656684Stjones1@inf.ed.ac.uk/// Target time() function.
11666684Stjones1@inf.ed.ac.uktemplate <class OS>
11676684Stjones1@inf.ed.ac.ukSyscallReturn
11686684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11696684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
11706684Stjones1@inf.ed.ac.uk{
11716684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
11726684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
11736684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
11746684Stjones1@inf.ed.ac.uk
11756684Stjones1@inf.ed.ac.uk    Addr taddr = (Addr)process->getSyscallArg(tc, 0);
11766684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
11776684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
11786684Stjones1@inf.ed.ac.uk        t = htog(t);
11796684Stjones1@inf.ed.ac.uk        TranslatingPort *p = tc->getMemPort();
11806684Stjones1@inf.ed.ac.uk        p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
11816684Stjones1@inf.ed.ac.uk    }
11826684Stjones1@inf.ed.ac.uk    return sec;
11836684Stjones1@inf.ed.ac.uk}
11842553SN/A
11852553SN/A
11861354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1187