syscall_emul.hh revision 8600
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
441809SN/A#ifdef __CYGWIN32__
455543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
461809SN/A#endif
473113Sgblack@eecs.umich.edu#include <sys/stat.h>
488229Snate@binkert.org#include <sys/time.h>
498229Snate@binkert.org#include <sys/uio.h>
503113Sgblack@eecs.umich.edu#include <fcntl.h>
517075Snate@binkert.org
528229Snate@binkert.org#include <cerrno>
537075Snate@binkert.org#include <string>
54360SN/A
552474SN/A#include "base/chunk_generator.hh"
565543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
572462SN/A#include "base/misc.hh"
581354SN/A#include "base/trace.hh"
596216Snate@binkert.org#include "base/types.hh"
606658Snate@binkert.org#include "config/the_isa.hh"
612474SN/A#include "cpu/base.hh"
622680Sktlim@umich.edu#include "cpu/thread_context.hh"
638232Snate@binkert.org#include "debug/SyscallVerbose.hh"
648229Snate@binkert.org#include "mem/page_table.hh"
652474SN/A#include "mem/translating_port.hh"
667678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
678229Snate@binkert.org#include "sim/process.hh"
686640Svince@csl.cornell.edu#include "sim/system.hh"
69360SN/A
70360SN/A///
71360SN/A/// System call descriptor.
72360SN/A///
73360SN/Aclass SyscallDesc {
74360SN/A
75360SN/A  public:
76360SN/A
77378SN/A    /// Typedef for target syscall handler functions.
781450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
793114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
80360SN/A
815543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
825543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
835543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
84360SN/A
85360SN/A    /// Flag values for controlling syscall behavior.
86360SN/A    enum Flags {
87360SN/A        /// Don't set return regs according to funcPtr return value.
88360SN/A        /// Used for syscalls with non-standard return conventions
892680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
90360SN/A        /// sigreturn).
91360SN/A        SuppressReturnValue = 1
92360SN/A    };
93360SN/A
94360SN/A    /// Constructor.
95360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
96360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
97360SN/A    {
98360SN/A    }
99360SN/A
100360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1013114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
102360SN/A};
103360SN/A
104360SN/A
105360SN/Aclass BaseBufferArg {
106360SN/A
107360SN/A  public:
108360SN/A
109360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
110360SN/A    {
111360SN/A        bufPtr = new uint8_t[size];
112360SN/A        // clear out buffer: in case we only partially populate this,
113360SN/A        // and then do a copyOut(), we want to make sure we don't
114360SN/A        // introduce any random junk into the simulated address space
115360SN/A        memset(bufPtr, 0, size);
116360SN/A    }
117360SN/A
118360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
119360SN/A
120360SN/A    //
121360SN/A    // copy data into simulator space (read from target memory)
122360SN/A    //
1232400SN/A    virtual bool copyIn(TranslatingPort *memport)
124360SN/A    {
1252461SN/A        memport->readBlob(addr, bufPtr, size);
1265543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
127360SN/A    }
128360SN/A
129360SN/A    //
130360SN/A    // copy data out of simulator space (write to target memory)
131360SN/A    //
1322400SN/A    virtual bool copyOut(TranslatingPort *memport)
133360SN/A    {
1342461SN/A        memport->writeBlob(addr, bufPtr, size);
1355543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
136360SN/A    }
137360SN/A
138360SN/A  protected:
139360SN/A    Addr addr;
140360SN/A    int size;
141360SN/A    uint8_t *bufPtr;
142360SN/A};
143360SN/A
144360SN/A
145360SN/Aclass BufferArg : public BaseBufferArg
146360SN/A{
147360SN/A  public:
148360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1495543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
150360SN/A};
151360SN/A
152360SN/Atemplate <class T>
153360SN/Aclass TypedBufferArg : public BaseBufferArg
154360SN/A{
155360SN/A  public:
156360SN/A    // user can optionally specify a specific number of bytes to
157360SN/A    // allocate to deal with those structs that have variable-size
158360SN/A    // arrays at the end
159360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
160360SN/A        : BaseBufferArg(_addr, _size)
161360SN/A    { }
162360SN/A
163360SN/A    // type case
164360SN/A    operator T*() { return (T *)bufPtr; }
165360SN/A
166360SN/A    // dereference operators
1675543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1685543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
169502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
170360SN/A};
171360SN/A
172360SN/A//////////////////////////////////////////////////////////////////////
173360SN/A//
174360SN/A// The following emulation functions are generic enough that they
175360SN/A// don't need to be recompiled for different emulated OS's.  They are
176360SN/A// defined in sim/syscall_emul.cc.
177360SN/A//
178360SN/A//////////////////////////////////////////////////////////////////////
179360SN/A
180360SN/A
181378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1821706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1833114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
184378SN/A
185378SN/A/// Handler for unimplemented syscalls that we never intend to
186378SN/A/// implement (signal handling, etc.) and should not affect the correct
187378SN/A/// behavior of the program.  Print a warning only if the appropriate
188378SN/A/// trace flag is enabled.  Return success to the target program.
1891706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1903114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1918149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
1928149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
193360SN/A
1946109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1951706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1963114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
197378SN/A
1986109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1996109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2006109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
2016109Ssanchezd@stanford.edu
202378SN/A/// Target getpagesize() handler.
2031706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
2043114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
205378SN/A
2065748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2075748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2085748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
209378SN/A
210378SN/A/// Target close() handler.
2111706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2123114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
213378SN/A
214378SN/A/// Target read() handler.
2151706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2163114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
217378SN/A
218378SN/A/// Target write() handler.
2191706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2203114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
221378SN/A
222378SN/A/// Target lseek() handler.
2231706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
225378SN/A
2264118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2274118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2284118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2294118Sgblack@eecs.umich.edu
230378SN/A/// Target munmap() handler.
2311706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2323114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
233378SN/A
234378SN/A/// Target gethostname() handler.
2351706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2363114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
237360SN/A
2385513SMichael.Adler@intel.com/// Target getcwd() handler.
2395513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2405513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2415513SMichael.Adler@intel.com
2425513SMichael.Adler@intel.com/// Target unlink() handler.
2435513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2445513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2455513SMichael.Adler@intel.com
246511SN/A/// Target unlink() handler.
2471706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2483114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
249511SN/A
2505513SMichael.Adler@intel.com/// Target mkdir() handler.
2515513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2525513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2535513SMichael.Adler@intel.com
254511SN/A/// Target rename() handler.
2551706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2563114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2571706SN/A
2581706SN/A
2591706SN/A/// Target truncate() handler.
2601706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2613114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2621706SN/A
2631706SN/A
2641706SN/A/// Target ftruncate() handler.
2651706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2663114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2671706SN/A
268511SN/A
2696703Svince@csl.cornell.edu/// Target truncate64() handler.
2706703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2716703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2726703Svince@csl.cornell.edu
2736685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2746685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2756685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2766685Stjones1@inf.ed.ac.uk
2776685Stjones1@inf.ed.ac.uk
2785513SMichael.Adler@intel.com/// Target umask() handler.
2795513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2805513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2815513SMichael.Adler@intel.com
2825513SMichael.Adler@intel.com
2831999SN/A/// Target chown() handler.
2841999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2853114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2861999SN/A
2871999SN/A
2881999SN/A/// Target fchown() handler.
2891999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2903114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2911999SN/A
2923079Sstever@eecs.umich.edu/// Target dup() handler.
2933079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2943114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2953079Sstever@eecs.umich.edu
2962093SN/A/// Target fnctl() handler.
2972093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2983114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2992093SN/A
3002687Sksewell@umich.edu/// Target fcntl64() handler.
3012687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3023114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3032687Sksewell@umich.edu
3042238SN/A/// Target setuid() handler.
3052238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3063114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3072238SN/A
3082238SN/A/// Target getpid() handler.
3092238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3103114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3112238SN/A
3122238SN/A/// Target getuid() handler.
3132238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3143114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3152238SN/A
3162238SN/A/// Target getgid() handler.
3172238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3183114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3192238SN/A
3202238SN/A/// Target getppid() handler.
3212238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3223114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3232238SN/A
3242238SN/A/// Target geteuid() handler.
3252238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3263114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3272238SN/A
3282238SN/A/// Target getegid() handler.
3292238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3303114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3312238SN/A
3326109Ssanchezd@stanford.edu/// Target clone() handler.
3336109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3346109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3352238SN/A
3362238SN/A
3372238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3382238SN/A/// returning a second value in a register other than the normal return register
3392238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3403114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3412238SN/A
3422238SN/A/// Target getpidPseudo() handler.
3432238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3443114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3452238SN/A
3462238SN/A/// Target getuidPseudo() handler.
3472238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3483114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3492238SN/A
3502238SN/A/// Target getgidPseudo() handler.
3512238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3523114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3532238SN/A
3542238SN/A
3551354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3561354SN/Aconst int one_million = 1000000;
3571354SN/A
3581354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3591354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3601354SN/A/// real-world time) to keep simulations repeatable.
3611354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3621354SN/A
3631354SN/A/// Helper function to convert current elapsed time to seconds and
3641354SN/A/// microseconds.
3651354SN/Atemplate <class T1, class T2>
3661354SN/Avoid
3671354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3681354SN/A{
3697823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
3701354SN/A    sec = elapsed_usecs / one_million;
3711354SN/A    usec = elapsed_usecs % one_million;
3721354SN/A}
3731354SN/A
374360SN/A//////////////////////////////////////////////////////////////////////
375360SN/A//
376360SN/A// The following emulation functions are generic, but need to be
377360SN/A// templated to account for differences in types, constants, etc.
378360SN/A//
379360SN/A//////////////////////////////////////////////////////////////////////
380360SN/A
3813113Sgblack@eecs.umich.edu#if NO_STAT64
3823113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3833113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
3843113Sgblack@eecs.umich.edu#else
3853113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
3863113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
3873113Sgblack@eecs.umich.edu#endif
3883113Sgblack@eecs.umich.edu
3893113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
3903113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
3913113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
3923113Sgblack@eecs.umich.edu
3933113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
3943113Sgblack@eecs.umich.edustatic void
3953113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
3963113Sgblack@eecs.umich.edu{
3974189Sgblack@eecs.umich.edu    using namespace TheISA;
3984189Sgblack@eecs.umich.edu
3993113Sgblack@eecs.umich.edu    if (fakeTTY)
4003113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4013113Sgblack@eecs.umich.edu    else
4023113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4033113Sgblack@eecs.umich.edu    tgt->st_dev = htog(tgt->st_dev);
4043113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4053113Sgblack@eecs.umich.edu    tgt->st_ino = htog(tgt->st_ino);
4063277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4075515SMichael.Adler@intel.com    if (fakeTTY) {
4085515SMichael.Adler@intel.com        // Claim to be a character device
4095515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4105515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4115515SMichael.Adler@intel.com    }
4123277Sgblack@eecs.umich.edu    tgt->st_mode = htog(tgt->st_mode);
4133277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4143277Sgblack@eecs.umich.edu    tgt->st_nlink = htog(tgt->st_nlink);
4153277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4163277Sgblack@eecs.umich.edu    tgt->st_uid = htog(tgt->st_uid);
4173277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4183277Sgblack@eecs.umich.edu    tgt->st_gid = htog(tgt->st_gid);
4193113Sgblack@eecs.umich.edu    if (fakeTTY)
4203113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4213113Sgblack@eecs.umich.edu    else
4223113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4233113Sgblack@eecs.umich.edu    tgt->st_rdev = htog(tgt->st_rdev);
4243113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4253113Sgblack@eecs.umich.edu    tgt->st_size = htog(tgt->st_size);
4263114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4273113Sgblack@eecs.umich.edu    tgt->st_atimeX = htog(tgt->st_atimeX);
4283114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4293113Sgblack@eecs.umich.edu    tgt->st_mtimeX = htog(tgt->st_mtimeX);
4303114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4313113Sgblack@eecs.umich.edu    tgt->st_ctimeX = htog(tgt->st_ctimeX);
4324061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4334061Sgblack@eecs.umich.edu    // consistently across different hosts.
4344061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4353113Sgblack@eecs.umich.edu    tgt->st_blksize = htog(tgt->st_blksize);
4363113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4373113Sgblack@eecs.umich.edu    tgt->st_blocks = htog(tgt->st_blocks);
4383113Sgblack@eecs.umich.edu}
4393113Sgblack@eecs.umich.edu
4403113Sgblack@eecs.umich.edu// Same for stat64
4413113Sgblack@eecs.umich.edu
4423113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4433113Sgblack@eecs.umich.edustatic void
4443113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4453113Sgblack@eecs.umich.edu{
4464189Sgblack@eecs.umich.edu    using namespace TheISA;
4474189Sgblack@eecs.umich.edu
4483113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4493113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4503113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4513113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = htog(tgt->st_atime_nsec);
4523113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4533113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = htog(tgt->st_mtime_nsec);
4543113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4553113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = htog(tgt->st_ctime_nsec);
4563113Sgblack@eecs.umich.edu#else
4573113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4583113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4593113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4603113Sgblack@eecs.umich.edu#endif
4613113Sgblack@eecs.umich.edu}
4623113Sgblack@eecs.umich.edu
4633113Sgblack@eecs.umich.edu//Here are a couple convenience functions
4643113Sgblack@eecs.umich.edutemplate<class OS>
4653113Sgblack@eecs.umich.edustatic void
4663113Sgblack@eecs.umich.educopyOutStatBuf(TranslatingPort * mem, Addr addr,
4673113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
4683113Sgblack@eecs.umich.edu{
4693113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
4703113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4713113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
4723113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4733113Sgblack@eecs.umich.edu}
4743113Sgblack@eecs.umich.edu
4753113Sgblack@eecs.umich.edutemplate<class OS>
4763113Sgblack@eecs.umich.edustatic void
4773113Sgblack@eecs.umich.educopyOutStat64Buf(TranslatingPort * mem, Addr addr,
4783113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
4793113Sgblack@eecs.umich.edu{
4803113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
4813113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
4826686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
4833113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
4843113Sgblack@eecs.umich.edu}
4853113Sgblack@eecs.umich.edu
486378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
487378SN/A/// only to find out if their stdout is a tty, to determine whether to
488378SN/A/// do line or block buffering.
489360SN/Atemplate <class OS>
4901450SN/ASyscallReturn
4913114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
4922680Sktlim@umich.edu          ThreadContext *tc)
493360SN/A{
4946701Sgblack@eecs.umich.edu    int index = 0;
4956701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
4966701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
497360SN/A
4981969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
499360SN/A
500360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
501360SN/A        // doesn't map to any simulator fd: not a valid target fd
5021458SN/A        return -EBADF;
503360SN/A    }
504360SN/A
505360SN/A    switch (req) {
5064131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
5074131Sbinkertn@umich.edu      case OS::TIOCGETP_:
5084131Sbinkertn@umich.edu      case OS::TIOCSETP_:
5094131Sbinkertn@umich.edu      case OS::TIOCSETN_:
5104131Sbinkertn@umich.edu      case OS::TIOCSETC_:
5114131Sbinkertn@umich.edu      case OS::TIOCGETC_:
5124131Sbinkertn@umich.edu      case OS::TIOCGETS_:
5134131Sbinkertn@umich.edu      case OS::TIOCGETA_:
5146689Stjones1@inf.ed.ac.uk      case OS::TCSETAW_:
5151458SN/A        return -ENOTTY;
516360SN/A
517360SN/A      default:
5187720Sgblack@eecs.umich.edu        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5197720Sgblack@eecs.umich.edu              fd, req, tc->pcState());
520360SN/A    }
521360SN/A}
522360SN/A
523378SN/A/// Target open() handler.
524360SN/Atemplate <class OS>
5251450SN/ASyscallReturn
5263114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5272680Sktlim@umich.edu         ThreadContext *tc)
528360SN/A{
529360SN/A    std::string path;
530360SN/A
5316701Sgblack@eecs.umich.edu    int index = 0;
5326701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
5336701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5341458SN/A        return -EFAULT;
535360SN/A
536360SN/A    if (path == "/dev/sysdev0") {
537360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
538360SN/A        // We don't support it, so just punt.
5391706SN/A        warn("Ignoring open(%s, ...)\n", path);
5401458SN/A        return -ENOENT;
541360SN/A    }
542360SN/A
5436701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5446701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
545360SN/A    int hostFlags = 0;
546360SN/A
547360SN/A    // translate open flags
548360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
549360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
550360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
551360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
552360SN/A        }
553360SN/A    }
554360SN/A
555360SN/A    // any target flags left?
556360SN/A    if (tgtFlags != 0)
5571706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
558360SN/A
559360SN/A#ifdef __CYGWIN32__
560360SN/A    hostFlags |= O_BINARY;
561360SN/A#endif
562360SN/A
5633669Sbinkertn@umich.edu    // Adjust path for current working directory
5643669Sbinkertn@umich.edu    path = process->fullPath(path);
5653669Sbinkertn@umich.edu
5661706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5671706SN/A
5685795Ssaidi@eecs.umich.edu    int fd;
5695795Ssaidi@eecs.umich.edu    if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
5705795Ssaidi@eecs.umich.edu        !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
5715795Ssaidi@eecs.umich.edu        // It's a proc/sys entery and requires special handling
5725795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
5735795Ssaidi@eecs.umich.edu        return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5745795Ssaidi@eecs.umich.edu     } else {
5755795Ssaidi@eecs.umich.edu        // open the file
5765795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
5775795Ssaidi@eecs.umich.edu        return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
5785795Ssaidi@eecs.umich.edu     }
579360SN/A
580360SN/A}
581360SN/A
5826640Svince@csl.cornell.edu/// Target sysinfo() handler.
5836640Svince@csl.cornell.edutemplate <class OS>
5846640Svince@csl.cornell.eduSyscallReturn
5856640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5866640Svince@csl.cornell.edu         ThreadContext *tc)
5876640Svince@csl.cornell.edu{
5886640Svince@csl.cornell.edu
5896701Sgblack@eecs.umich.edu    int index = 0;
5906701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
5916701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
5926640Svince@csl.cornell.edu
5936701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
5946701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
5956640Svince@csl.cornell.edu
5966701Sgblack@eecs.umich.edu    sysinfo.copyOut(tc->getMemPort());
5976640Svince@csl.cornell.edu
5986701Sgblack@eecs.umich.edu    return 0;
5996640Svince@csl.cornell.edu}
600360SN/A
6011999SN/A/// Target chmod() handler.
6021999SN/Atemplate <class OS>
6031999SN/ASyscallReturn
6043114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6052680Sktlim@umich.edu          ThreadContext *tc)
6061999SN/A{
6071999SN/A    std::string path;
6081999SN/A
6096701Sgblack@eecs.umich.edu    int index = 0;
6106701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
6116701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
6121999SN/A        return -EFAULT;
6136701Sgblack@eecs.umich.edu    }
6141999SN/A
6156701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
6161999SN/A    mode_t hostMode = 0;
6171999SN/A
6181999SN/A    // XXX translate mode flags via OS::something???
6191999SN/A    hostMode = mode;
6201999SN/A
6213669Sbinkertn@umich.edu    // Adjust path for current working directory
6223669Sbinkertn@umich.edu    path = process->fullPath(path);
6233669Sbinkertn@umich.edu
6241999SN/A    // do the chmod
6251999SN/A    int result = chmod(path.c_str(), hostMode);
6261999SN/A    if (result < 0)
6272218SN/A        return -errno;
6281999SN/A
6291999SN/A    return 0;
6301999SN/A}
6311999SN/A
6321999SN/A
6331999SN/A/// Target fchmod() handler.
6341999SN/Atemplate <class OS>
6351999SN/ASyscallReturn
6363114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6372680Sktlim@umich.edu           ThreadContext *tc)
6381999SN/A{
6396701Sgblack@eecs.umich.edu    int index = 0;
6406701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6411999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6421999SN/A        // doesn't map to any simulator fd: not a valid target fd
6431999SN/A        return -EBADF;
6441999SN/A    }
6451999SN/A
6466701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
6471999SN/A    mode_t hostMode = 0;
6481999SN/A
6491999SN/A    // XXX translate mode flags via OS::someting???
6501999SN/A    hostMode = mode;
6511999SN/A
6521999SN/A    // do the fchmod
6531999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
6541999SN/A    if (result < 0)
6552218SN/A        return -errno;
6561999SN/A
6571999SN/A    return 0;
6581999SN/A}
6591999SN/A
6605877Shsul@eecs.umich.edu/// Target mremap() handler.
6615877Shsul@eecs.umich.edutemplate <class OS>
6625877Shsul@eecs.umich.eduSyscallReturn
6635877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
6645877Shsul@eecs.umich.edu{
6656701Sgblack@eecs.umich.edu    int index = 0;
6666701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
6676701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
6686701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
6696701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
6705877Shsul@eecs.umich.edu
6715877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
6725877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
6735877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
6745877Shsul@eecs.umich.edu        return -EINVAL;
6755877Shsul@eecs.umich.edu    }
6765877Shsul@eecs.umich.edu
6775877Shsul@eecs.umich.edu    if (new_length > old_length) {
6785877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
6795877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
6805877Shsul@eecs.umich.edu            process->pTable->allocate(process->mmap_end, diff);
6815877Shsul@eecs.umich.edu            process->mmap_end += diff;
6825877Shsul@eecs.umich.edu            return start;
6835877Shsul@eecs.umich.edu        } else {
6845877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
6855877Shsul@eecs.umich.edu            if (!(flags & 1)) {
6865877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
6875877Shsul@eecs.umich.edu                return -ENOMEM;
6885877Shsul@eecs.umich.edu            } else {
6895877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
6905877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
6915877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
6925877Shsul@eecs.umich.edu                start = process->mmap_end;
6935877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
6945877Shsul@eecs.umich.edu                process->pTable->allocate(start + old_length, new_length - old_length);
6955877Shsul@eecs.umich.edu                process->mmap_end += new_length;
6965877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
6975877Shsul@eecs.umich.edu                return start;
6985877Shsul@eecs.umich.edu            }
6995877Shsul@eecs.umich.edu        }
7005877Shsul@eecs.umich.edu    } else {
7015877Shsul@eecs.umich.edu        process->pTable->deallocate(start + new_length, old_length -
7025877Shsul@eecs.umich.edu                new_length);
7035877Shsul@eecs.umich.edu        return start;
7045877Shsul@eecs.umich.edu    }
7055877Shsul@eecs.umich.edu}
7061999SN/A
707378SN/A/// Target stat() handler.
708360SN/Atemplate <class OS>
7091450SN/ASyscallReturn
7103114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7112680Sktlim@umich.edu         ThreadContext *tc)
712360SN/A{
713360SN/A    std::string path;
714360SN/A
7156701Sgblack@eecs.umich.edu    int index = 0;
7166701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
7176701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7186701Sgblack@eecs.umich.edu        return -EFAULT;
7196701Sgblack@eecs.umich.edu    }
7206701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
721360SN/A
7223669Sbinkertn@umich.edu    // Adjust path for current working directory
7233669Sbinkertn@umich.edu    path = process->fullPath(path);
7243669Sbinkertn@umich.edu
725360SN/A    struct stat hostBuf;
726360SN/A    int result = stat(path.c_str(), &hostBuf);
727360SN/A
728360SN/A    if (result < 0)
7292218SN/A        return -errno;
730360SN/A
7316701Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
732360SN/A
7331458SN/A    return 0;
734360SN/A}
735360SN/A
736360SN/A
7375074Ssaidi@eecs.umich.edu/// Target stat64() handler.
7385074Ssaidi@eecs.umich.edutemplate <class OS>
7395074Ssaidi@eecs.umich.eduSyscallReturn
7405074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7415074Ssaidi@eecs.umich.edu           ThreadContext *tc)
7425074Ssaidi@eecs.umich.edu{
7435074Ssaidi@eecs.umich.edu    std::string path;
7445074Ssaidi@eecs.umich.edu
7456701Sgblack@eecs.umich.edu    int index = 0;
7466701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
7476701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
7485074Ssaidi@eecs.umich.edu        return -EFAULT;
7496701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
7505074Ssaidi@eecs.umich.edu
7515074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
7525074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
7535074Ssaidi@eecs.umich.edu
7545208Ssaidi@eecs.umich.edu#if NO_STAT64
7555208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
7565208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
7575208Ssaidi@eecs.umich.edu#else
7585074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
7595074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
7605208Ssaidi@eecs.umich.edu#endif
7615074Ssaidi@eecs.umich.edu
7625074Ssaidi@eecs.umich.edu    if (result < 0)
7635074Ssaidi@eecs.umich.edu        return -errno;
7645074Ssaidi@eecs.umich.edu
7656701Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
7665074Ssaidi@eecs.umich.edu
7675074Ssaidi@eecs.umich.edu    return 0;
7685074Ssaidi@eecs.umich.edu}
7695074Ssaidi@eecs.umich.edu
7705074Ssaidi@eecs.umich.edu
7711999SN/A/// Target fstat64() handler.
7721999SN/Atemplate <class OS>
7731999SN/ASyscallReturn
7743114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
7752680Sktlim@umich.edu            ThreadContext *tc)
7761999SN/A{
7776701Sgblack@eecs.umich.edu    int index = 0;
7786701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7796701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
7801999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7811999SN/A        // doesn't map to any simulator fd: not a valid target fd
7821999SN/A        return -EBADF;
7831999SN/A    }
7841999SN/A
7852764Sstever@eecs.umich.edu#if NO_STAT64
7862064SN/A    struct stat  hostBuf;
7872064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
7882064SN/A#else
7892064SN/A    struct stat64  hostBuf;
7901999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
7912064SN/A#endif
7921999SN/A
7931999SN/A    if (result < 0)
7942218SN/A        return -errno;
7951999SN/A
7966701Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
7971999SN/A
7981999SN/A    return 0;
7991999SN/A}
8001999SN/A
8011999SN/A
802378SN/A/// Target lstat() handler.
803360SN/Atemplate <class OS>
8041450SN/ASyscallReturn
8053114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8062680Sktlim@umich.edu          ThreadContext *tc)
807360SN/A{
808360SN/A    std::string path;
809360SN/A
8106701Sgblack@eecs.umich.edu    int index = 0;
8116701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
8126701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8136701Sgblack@eecs.umich.edu        return -EFAULT;
8146701Sgblack@eecs.umich.edu    }
8156701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
816360SN/A
8173669Sbinkertn@umich.edu    // Adjust path for current working directory
8183669Sbinkertn@umich.edu    path = process->fullPath(path);
8193669Sbinkertn@umich.edu
820360SN/A    struct stat hostBuf;
821360SN/A    int result = lstat(path.c_str(), &hostBuf);
822360SN/A
823360SN/A    if (result < 0)
8241458SN/A        return -errno;
825360SN/A
8266701Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
827360SN/A
8281458SN/A    return 0;
829360SN/A}
830360SN/A
8311999SN/A/// Target lstat64() handler.
8321999SN/Atemplate <class OS>
8331999SN/ASyscallReturn
8343114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8352680Sktlim@umich.edu            ThreadContext *tc)
8361999SN/A{
8371999SN/A    std::string path;
8381999SN/A
8396701Sgblack@eecs.umich.edu    int index = 0;
8406701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
8416701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8426701Sgblack@eecs.umich.edu        return -EFAULT;
8436701Sgblack@eecs.umich.edu    }
8446701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8451999SN/A
8463669Sbinkertn@umich.edu    // Adjust path for current working directory
8473669Sbinkertn@umich.edu    path = process->fullPath(path);
8483669Sbinkertn@umich.edu
8492764Sstever@eecs.umich.edu#if NO_STAT64
8502064SN/A    struct stat hostBuf;
8512064SN/A    int result = lstat(path.c_str(), &hostBuf);
8522064SN/A#else
8531999SN/A    struct stat64 hostBuf;
8541999SN/A    int result = lstat64(path.c_str(), &hostBuf);
8552064SN/A#endif
8561999SN/A
8571999SN/A    if (result < 0)
8581999SN/A        return -errno;
8591999SN/A
8606701Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemPort(), bufPtr, &hostBuf);
8611999SN/A
8621999SN/A    return 0;
8631999SN/A}
8641999SN/A
865378SN/A/// Target fstat() handler.
866360SN/Atemplate <class OS>
8671450SN/ASyscallReturn
8683114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8692680Sktlim@umich.edu          ThreadContext *tc)
870360SN/A{
8716701Sgblack@eecs.umich.edu    int index = 0;
8726701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
8736701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
874360SN/A
8751969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
876360SN/A
877360SN/A    if (fd < 0)
8781458SN/A        return -EBADF;
879360SN/A
880360SN/A    struct stat hostBuf;
881360SN/A    int result = fstat(fd, &hostBuf);
882360SN/A
883360SN/A    if (result < 0)
8841458SN/A        return -errno;
885360SN/A
8866701Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemPort(), bufPtr, &hostBuf, (fd == 1));
8872021SN/A
8881458SN/A    return 0;
889360SN/A}
890360SN/A
891360SN/A
8921706SN/A/// Target statfs() handler.
8931706SN/Atemplate <class OS>
8941706SN/ASyscallReturn
8953114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8962680Sktlim@umich.edu           ThreadContext *tc)
8971706SN/A{
8981706SN/A    std::string path;
8991706SN/A
9006701Sgblack@eecs.umich.edu    int index = 0;
9016701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
9026701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9036701Sgblack@eecs.umich.edu        return -EFAULT;
9046701Sgblack@eecs.umich.edu    }
9056701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9061706SN/A
9073669Sbinkertn@umich.edu    // Adjust path for current working directory
9083669Sbinkertn@umich.edu    path = process->fullPath(path);
9093669Sbinkertn@umich.edu
9101706SN/A    struct statfs hostBuf;
9111706SN/A    int result = statfs(path.c_str(), &hostBuf);
9121706SN/A
9131706SN/A    if (result < 0)
9142218SN/A        return -errno;
9151706SN/A
9166701Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
9171706SN/A
9181706SN/A    return 0;
9191706SN/A}
9201706SN/A
9211706SN/A
9221706SN/A/// Target fstatfs() handler.
9231706SN/Atemplate <class OS>
9241706SN/ASyscallReturn
9253114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9262680Sktlim@umich.edu            ThreadContext *tc)
9271706SN/A{
9286701Sgblack@eecs.umich.edu    int index = 0;
9296701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
9306701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9311706SN/A
9321706SN/A    if (fd < 0)
9331706SN/A        return -EBADF;
9341706SN/A
9351706SN/A    struct statfs hostBuf;
9361706SN/A    int result = fstatfs(fd, &hostBuf);
9371706SN/A
9381706SN/A    if (result < 0)
9392218SN/A        return -errno;
9401706SN/A
9416701Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), bufPtr, &hostBuf);
9421706SN/A
9431706SN/A    return 0;
9441706SN/A}
9451706SN/A
9461706SN/A
9471999SN/A/// Target writev() handler.
9481999SN/Atemplate <class OS>
9491999SN/ASyscallReturn
9503114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9512680Sktlim@umich.edu           ThreadContext *tc)
9521999SN/A{
9536701Sgblack@eecs.umich.edu    int index = 0;
9546701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9551999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9561999SN/A        // doesn't map to any simulator fd: not a valid target fd
9571999SN/A        return -EBADF;
9581999SN/A    }
9591999SN/A
9602680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
9616701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
9626701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
9631999SN/A    struct iovec hiov[count];
9646227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
9651999SN/A        typename OS::tgt_iovec tiov;
9662461SN/A
9672461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
9682461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
9692091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
9701999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
9712461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
9722461SN/A                    hiov[i].iov_len);
9731999SN/A    }
9741999SN/A
9751999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
9761999SN/A
9776227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
9781999SN/A        delete [] (char *)hiov[i].iov_base;
9791999SN/A
9801999SN/A    if (result < 0)
9812218SN/A        return -errno;
9821999SN/A
9831999SN/A    return 0;
9841999SN/A}
9851999SN/A
9861999SN/A
987378SN/A/// Target mmap() handler.
988378SN/A///
989378SN/A/// We don't really handle mmap().  If the target is mmaping an
990378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
991378SN/A/// nothing (since memory is initialized to zero and the simulator
9928324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
9938324Ssteve.reinhardt@amd.com///
994360SN/Atemplate <class OS>
9951450SN/ASyscallReturn
9963114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
997360SN/A{
9986701Sgblack@eecs.umich.edu    int index = 0;
9996701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
10006701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
10016701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
10026701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
10038324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
10046701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1005360SN/A
10068324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
10078324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
10088324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
10098324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
10108324Ssteve.reinhardt@amd.com            return -EBADF;
10118324Ssteve.reinhardt@amd.com        }
10128324Ssteve.reinhardt@amd.com
10138324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
10148324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
10158324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
10168324Ssteve.reinhardt@amd.com            // another name on some platform
10178324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
10188324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
10198324Ssteve.reinhardt@amd.com        }
10208324Ssteve.reinhardt@amd.com    }
10215877Shsul@eecs.umich.edu
10222544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
10232544SN/A        (length % TheISA::VMPageSize) != 0) {
10242544SN/A        warn("mmap failing: arguments not page-aligned: "
10252544SN/A             "start 0x%x length 0x%x",
10262544SN/A             start, length);
10272544SN/A        return -EINVAL;
1028360SN/A    }
1029360SN/A
10308600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
10318600Ssteve.reinhardt@amd.com    // true if the user has been warned.
10328600Ssteve.reinhardt@amd.com    bool clobber = false;
10338600Ssteve.reinhardt@amd.com
10348600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
10358600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
10368600Ssteve.reinhardt@amd.com
10378600Ssteve.reinhardt@amd.com    if (use_provided_address) {
10388600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
10398600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
10408600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
10418600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
10428600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
10438600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
10448600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
10458600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
10468600Ssteve.reinhardt@amd.com                     start);
10478600Ssteve.reinhardt@amd.com                clobber = true;
10488600Ssteve.reinhardt@amd.com            } else {
10498600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
10508600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
10518600Ssteve.reinhardt@amd.com                use_provided_address = false;
10528600Ssteve.reinhardt@amd.com            }
10538600Ssteve.reinhardt@amd.com        }
10542544SN/A    }
10552544SN/A
10568600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
10578600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
10588600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
10598600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
10608600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
10618600Ssteve.reinhardt@amd.com            p->mmap_end = start;
10628600Ssteve.reinhardt@amd.com        } else {
10638600Ssteve.reinhardt@amd.com            start = p->mmap_end;
10648600Ssteve.reinhardt@amd.com            p->mmap_end += length;
10658600Ssteve.reinhardt@amd.com        }
10666672Sgblack@eecs.umich.edu    }
10678600Ssteve.reinhardt@amd.com
10688600Ssteve.reinhardt@amd.com    p->pTable->allocate(start, length, clobber);
10692544SN/A
10701458SN/A    return start;
1071360SN/A}
1072360SN/A
1073378SN/A/// Target getrlimit() handler.
1074360SN/Atemplate <class OS>
10751450SN/ASyscallReturn
10763114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10772680Sktlim@umich.edu        ThreadContext *tc)
1078360SN/A{
10796701Sgblack@eecs.umich.edu    int index = 0;
10806701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
10816701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1082360SN/A
1083360SN/A    switch (resource) {
10842064SN/A        case OS::TGT_RLIMIT_STACK:
10855877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
10862064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
10872091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
10882091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
10892064SN/A            break;
1090360SN/A
10915877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
10925877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
10935877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
10945877Shsul@eecs.umich.edu            rlp->rlim_cur = htog(rlp->rlim_cur);
10955877Shsul@eecs.umich.edu            rlp->rlim_max = htog(rlp->rlim_max);
10965877Shsul@eecs.umich.edu            break;
10975877Shsul@eecs.umich.edu
10982064SN/A        default:
10992064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
11002064SN/A                << std::endl;
11012064SN/A            abort();
11022064SN/A            break;
1103360SN/A    }
1104360SN/A
11052680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
11061458SN/A    return 0;
1107360SN/A}
1108360SN/A
1109378SN/A/// Target gettimeofday() handler.
1110360SN/Atemplate <class OS>
11111450SN/ASyscallReturn
11123114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11132680Sktlim@umich.edu        ThreadContext *tc)
1114360SN/A{
11156701Sgblack@eecs.umich.edu    int index = 0;
11166701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1117360SN/A
1118360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1119360SN/A    tp->tv_sec += seconds_since_epoch;
11206109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
11216109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1122360SN/A
11232680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
1124360SN/A
11251458SN/A    return 0;
1126360SN/A}
1127360SN/A
1128360SN/A
11291999SN/A/// Target utimes() handler.
11301999SN/Atemplate <class OS>
11311999SN/ASyscallReturn
11323114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11332680Sktlim@umich.edu           ThreadContext *tc)
11341999SN/A{
11351999SN/A    std::string path;
11361999SN/A
11376701Sgblack@eecs.umich.edu    int index = 0;
11386701Sgblack@eecs.umich.edu    if (!tc->getMemPort()->tryReadString(path,
11396701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11406701Sgblack@eecs.umich.edu        return -EFAULT;
11416701Sgblack@eecs.umich.edu    }
11421999SN/A
11436701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
11446701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
11452680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
11461999SN/A
11471999SN/A    struct timeval hostTimeval[2];
11481999SN/A    for (int i = 0; i < 2; ++i)
11491999SN/A    {
11502091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
11512091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
11521999SN/A    }
11533669Sbinkertn@umich.edu
11543669Sbinkertn@umich.edu    // Adjust path for current working directory
11553669Sbinkertn@umich.edu    path = process->fullPath(path);
11563669Sbinkertn@umich.edu
11571999SN/A    int result = utimes(path.c_str(), hostTimeval);
11581999SN/A
11591999SN/A    if (result < 0)
11601999SN/A        return -errno;
11611999SN/A
11621999SN/A    return 0;
11631999SN/A}
1164378SN/A/// Target getrusage() function.
1165360SN/Atemplate <class OS>
11661450SN/ASyscallReturn
11673114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11682680Sktlim@umich.edu              ThreadContext *tc)
1169360SN/A{
11706701Sgblack@eecs.umich.edu    int index = 0;
11716701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
11726701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1173360SN/A
11743670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
11753670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1176360SN/A    rup->ru_stime.tv_sec = 0;
1177360SN/A    rup->ru_stime.tv_usec = 0;
1178360SN/A    rup->ru_maxrss = 0;
1179360SN/A    rup->ru_ixrss = 0;
1180360SN/A    rup->ru_idrss = 0;
1181360SN/A    rup->ru_isrss = 0;
1182360SN/A    rup->ru_minflt = 0;
1183360SN/A    rup->ru_majflt = 0;
1184360SN/A    rup->ru_nswap = 0;
1185360SN/A    rup->ru_inblock = 0;
1186360SN/A    rup->ru_oublock = 0;
1187360SN/A    rup->ru_msgsnd = 0;
1188360SN/A    rup->ru_msgrcv = 0;
1189360SN/A    rup->ru_nsignals = 0;
1190360SN/A    rup->ru_nvcsw = 0;
1191360SN/A    rup->ru_nivcsw = 0;
1192360SN/A
11933670Sbinkertn@umich.edu    switch (who) {
11943670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
11953670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
11963670Sbinkertn@umich.edu        rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
11973670Sbinkertn@umich.edu        rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
11983670Sbinkertn@umich.edu        break;
11993670Sbinkertn@umich.edu
12003670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
12013670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
12023670Sbinkertn@umich.edu        break;
12033670Sbinkertn@umich.edu
12043670Sbinkertn@umich.edu      default:
12053670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
12063670Sbinkertn@umich.edu        // plow ahead
12073670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
12083670Sbinkertn@umich.edu             who);
12093670Sbinkertn@umich.edu    }
12103670Sbinkertn@umich.edu
12112680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
1212360SN/A
12131458SN/A    return 0;
1214360SN/A}
1215360SN/A
12166683Stjones1@inf.ed.ac.uk/// Target times() function.
12176683Stjones1@inf.ed.ac.uktemplate <class OS>
12186683Stjones1@inf.ed.ac.ukSyscallReturn
12196683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12206683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
12216683Stjones1@inf.ed.ac.uk{
12226701Sgblack@eecs.umich.edu    int index = 0;
12236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
12246683Stjones1@inf.ed.ac.uk
12256683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
12267823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
12276683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
12286683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
12296683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
12306683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
12316683Stjones1@inf.ed.ac.uk
12326683Stjones1@inf.ed.ac.uk    // Convert to host endianness
12336683Stjones1@inf.ed.ac.uk    bufp->tms_utime = htog(bufp->tms_utime);
12346683Stjones1@inf.ed.ac.uk
12356683Stjones1@inf.ed.ac.uk    // Write back
12366683Stjones1@inf.ed.ac.uk    bufp.copyOut(tc->getMemPort());
12376683Stjones1@inf.ed.ac.uk
12386683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
12396683Stjones1@inf.ed.ac.uk    return clocks;
12406683Stjones1@inf.ed.ac.uk}
12412553SN/A
12426684Stjones1@inf.ed.ac.uk/// Target time() function.
12436684Stjones1@inf.ed.ac.uktemplate <class OS>
12446684Stjones1@inf.ed.ac.ukSyscallReturn
12456684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12466684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
12476684Stjones1@inf.ed.ac.uk{
12486684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
12496684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
12506684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
12516684Stjones1@inf.ed.ac.uk
12526701Sgblack@eecs.umich.edu    int index = 0;
12536701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
12546684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
12556684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
12566684Stjones1@inf.ed.ac.uk        t = htog(t);
12576684Stjones1@inf.ed.ac.uk        TranslatingPort *p = tc->getMemPort();
12586684Stjones1@inf.ed.ac.uk        p->writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
12596684Stjones1@inf.ed.ac.uk    }
12606684Stjones1@inf.ed.ac.uk    return sec;
12616684Stjones1@inf.ed.ac.uk}
12622553SN/A
12632553SN/A
12641354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1265