syscall_emul.hh revision 10485
1360SN/A/*
21458SN/A * Copyright (c) 2012-2013 ARM Limited
3360SN/A * All rights reserved
4360SN/A *
5360SN/A * The license below extends only to copyright in the software and shall
6360SN/A * not be construed as granting a license to any other intellectual
7360SN/A * property including but not limited to intellectual property relating
8360SN/A * to a hardware implementation of the functionality of the software
9360SN/A * licensed hereunder.  You may use the software subject to the license
10360SN/A * terms below provided that you ensure that this notice is replicated
11360SN/A * unmodified and in its entirety in all distributions of the software,
12360SN/A * modified or unmodified, in source code or in binary form.
13360SN/A *
14360SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
15360SN/A * All rights reserved.
16360SN/A *
17360SN/A * Redistribution and use in source and binary forms, with or without
18360SN/A * modification, are permitted provided that the following conditions are
19360SN/A * met: redistributions of source code must retain the above copyright
20360SN/A * notice, this list of conditions and the following disclaimer;
21360SN/A * redistributions in binary form must reproduce the above copyright
22360SN/A * notice, this list of conditions and the following disclaimer in the
23360SN/A * documentation and/or other materials provided with the distribution;
24360SN/A * neither the name of the copyright holders nor the names of its
25360SN/A * contributors may be used to endorse or promote products derived from
26360SN/A * this software without specific prior written permission.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
292665Ssaidi@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321354SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331354SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
352764Sstever@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
362764Sstever@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372064SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39360SN/A *
40360SN/A * Authors: Steve Reinhardt
41360SN/A *          Kevin Lim
42360SN/A */
43360SN/A
441354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
45360SN/A#define __SIM_SYSCALL_EMUL_HH__
461809SN/A
475543Ssaidi@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
481809SN/A  defined(__FreeBSD__) || defined(__CYGWIN__) || \
493113Sgblack@eecs.umich.edu  defined(__NetBSD__))
503113Sgblack@eecs.umich.edu
511999SN/A///
52360SN/A/// @file syscall_emul.hh
535543Ssaidi@eecs.umich.edu///
542474SN/A/// This file defines objects used to emulate syscalls from the target
555543Ssaidi@eecs.umich.edu/// application on the host machine.
562462SN/A
571354SN/A#ifdef __CYGWIN32__
582474SN/A#include <sys/fcntl.h>  // for O_BINARY
592680Sktlim@umich.edu#endif
602474SN/A#include <sys/stat.h>
612474SN/A#include <sys/time.h>
621354SN/A#include <sys/uio.h>
63360SN/A#include <fcntl.h>
64360SN/A
65360SN/A#include <cerrno>
66360SN/A#include <string>
67360SN/A
68360SN/A#include "base/chunk_generator.hh"
69360SN/A#include "base/intmath.hh"      // for RoundUp
70360SN/A#include "base/misc.hh"
71378SN/A#include "base/trace.hh"
721450SN/A#include "base/types.hh"
733114Sgblack@eecs.umich.edu#include "config/the_isa.hh"
74360SN/A#include "cpu/base.hh"
755543Ssaidi@eecs.umich.edu#include "cpu/thread_context.hh"
765543Ssaidi@eecs.umich.edu#include "debug/SyscallVerbose.hh"
775543Ssaidi@eecs.umich.edu#include "mem/page_table.hh"
78360SN/A#include "mem/se_translating_port_proxy.hh"
79360SN/A#include "sim/byteswap.hh"
80360SN/A#include "sim/process.hh"
81360SN/A#include "sim/syscallreturn.hh"
82360SN/A#include "sim/system.hh"
832680Sktlim@umich.edu
84360SN/A///
85360SN/A/// System call descriptor.
86360SN/A///
87360SN/Aclass SyscallDesc {
88360SN/A
89360SN/A  public:
90360SN/A
91360SN/A    /// Typedef for target syscall handler functions.
92360SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
93360SN/A                           LiveProcess *, ThreadContext *);
94360SN/A
953114Sgblack@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
96360SN/A    FuncPtr funcPtr;    //!< Pointer to emulation function.
97360SN/A    int flags;          //!< Flags (see Flags enum).
98360SN/A
99360SN/A    /// Flag values for controlling syscall behavior.
100360SN/A    enum Flags {
101360SN/A        /// Don't set return regs according to funcPtr return value.
102360SN/A        /// Used for syscalls with non-standard return conventions
103360SN/A        /// that explicitly set the ThreadContext regs (e.g.,
104360SN/A        /// sigreturn).
105360SN/A        SuppressReturnValue = 1
106360SN/A    };
107360SN/A
108360SN/A    /// Constructor.
109360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
110360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
111360SN/A    {
112360SN/A    }
113360SN/A
114360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
115360SN/A    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
116360SN/A};
1172400SN/A
118360SN/A
1192461SN/Aclass BaseBufferArg {
1205543Ssaidi@eecs.umich.edu
121360SN/A  public:
122360SN/A
123360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
124360SN/A    {
125360SN/A        bufPtr = new uint8_t[size];
1262400SN/A        // clear out buffer: in case we only partially populate this,
127360SN/A        // and then do a copyOut(), we want to make sure we don't
1282461SN/A        // introduce any random junk into the simulated address space
1295543Ssaidi@eecs.umich.edu        memset(bufPtr, 0, size);
130360SN/A    }
131360SN/A
132360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
133360SN/A
134360SN/A    //
135360SN/A    // copy data into simulator space (read from target memory)
136360SN/A    //
137360SN/A    virtual bool copyIn(SETranslatingPortProxy &memproxy)
138360SN/A    {
139360SN/A        memproxy.readBlob(addr, bufPtr, size);
140360SN/A        return true;    // no EFAULT detection for now
141360SN/A    }
142360SN/A
1435543Ssaidi@eecs.umich.edu    //
144360SN/A    // copy data out of simulator space (write to target memory)
145360SN/A    //
146360SN/A    virtual bool copyOut(SETranslatingPortProxy &memproxy)
147360SN/A    {
148360SN/A        memproxy.writeBlob(addr, bufPtr, size);
149360SN/A        return true;    // no EFAULT detection for now
150360SN/A    }
151360SN/A
152360SN/A  protected:
153360SN/A    Addr addr;
154360SN/A    int size;
155360SN/A    uint8_t *bufPtr;
156360SN/A};
157360SN/A
158360SN/A
159360SN/Aclass BufferArg : public BaseBufferArg
160360SN/A{
1615543Ssaidi@eecs.umich.edu  public:
1625543Ssaidi@eecs.umich.edu    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
163502SN/A    void *bufferPtr()   { return bufPtr; }
164360SN/A};
165360SN/A
166360SN/Atemplate <class T>
167360SN/Aclass TypedBufferArg : public BaseBufferArg
168360SN/A{
169360SN/A  public:
170360SN/A    // user can optionally specify a specific number of bytes to
171360SN/A    // allocate to deal with those structs that have variable-size
172360SN/A    // arrays at the end
173360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
174360SN/A        : BaseBufferArg(_addr, _size)
175378SN/A    { }
1761706SN/A
1773114Sgblack@eecs.umich.edu    // type case
178378SN/A    operator T*() { return (T *)bufPtr; }
179378SN/A
180378SN/A    // dereference operators
181378SN/A    T &operator*()       { return *((T *)bufPtr); }
182378SN/A    T* operator->()      { return (T *)bufPtr; }
1831706SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
1843114Sgblack@eecs.umich.edu};
185360SN/A
186378SN/A//////////////////////////////////////////////////////////////////////
1871706SN/A//
1883114Sgblack@eecs.umich.edu// The following emulation functions are generic enough that they
189378SN/A// don't need to be recompiled for different emulated OS's.  They are
190378SN/A// defined in sim/syscall_emul.cc.
1911706SN/A//
1923114Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////
193378SN/A
1945748SSteve.Reinhardt@amd.com
1955748SSteve.Reinhardt@amd.com/// Handler for unimplemented syscalls that we haven't thought about.
1965748SSteve.Reinhardt@amd.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
197378SN/A                                LiveProcess *p, ThreadContext *tc);
198378SN/A
1991706SN/A/// Handler for unimplemented syscalls that we never intend to
2003114Sgblack@eecs.umich.edu/// implement (signal handling, etc.) and should not affect the correct
201378SN/A/// behavior of the program.  Print a warning only if the appropriate
202378SN/A/// trace flag is enabled.  Return success to the target program.
2031706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
2043114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
205378SN/ASyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
206378SN/A                         LiveProcess *p, ThreadContext *tc);
2071706SN/A
2083114Sgblack@eecs.umich.edu/// Target exit() handler: terminate current context.
209378SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
210378SN/A                       LiveProcess *p, ThreadContext *tc);
2111706SN/A
2123114Sgblack@eecs.umich.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
213378SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2144118Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
2154118Sgblack@eecs.umich.edu
2164118Sgblack@eecs.umich.edu/// Target getpagesize() handler.
2174118Sgblack@eecs.umich.eduSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
218378SN/A                              LiveProcess *p, ThreadContext *tc);
2191706SN/A
2203114Sgblack@eecs.umich.edu/// Target brk() handler: set brk address.
221378SN/ASyscallReturn brkFunc(SyscallDesc *desc, int num,
222378SN/A                      LiveProcess *p, ThreadContext *tc);
2231706SN/A
2243114Sgblack@eecs.umich.edu/// Target close() handler.
225360SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2265513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2275513SMichael.Adler@intel.com
2285513SMichael.Adler@intel.com/// Target read() handler.
2295513SMichael.Adler@intel.comSyscallReturn readFunc(SyscallDesc *desc, int num,
2305513SMichael.Adler@intel.com                       LiveProcess *p, ThreadContext *tc);
2315513SMichael.Adler@intel.com
2325513SMichael.Adler@intel.com/// Target write() handler.
2335513SMichael.Adler@intel.comSyscallReturn writeFunc(SyscallDesc *desc, int num,
234511SN/A                        LiveProcess *p, ThreadContext *tc);
2351706SN/A
2363114Sgblack@eecs.umich.edu/// Target lseek() handler.
237511SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2385513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2395513SMichael.Adler@intel.com
2405513SMichael.Adler@intel.com/// Target _llseek() handler.
2415513SMichael.Adler@intel.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
242511SN/A                        LiveProcess *p, ThreadContext *tc);
2431706SN/A
2443114Sgblack@eecs.umich.edu/// Target munmap() handler.
2451706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2461706SN/A                         LiveProcess *p, ThreadContext *tc);
2471706SN/A
2481706SN/A/// Target gethostname() handler.
2493114Sgblack@eecs.umich.eduSyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2501706SN/A                              LiveProcess *p, ThreadContext *tc);
2511706SN/A
2521706SN/A/// Target getcwd() handler.
2531706SN/ASyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2543114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2551706SN/A
256511SN/A/// Target readlink() handler.
2575513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2585513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc,
2595513SMichael.Adler@intel.com                           int index = 0);
2605513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2615513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2621999SN/A
2631999SN/A/// Target unlink() handler.
2643114Sgblack@eecs.umich.eduSyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2651999SN/A                         LiveProcess *p, ThreadContext *tc);
2661999SN/A
2671999SN/A/// Target mkdir() handler.
2681999SN/ASyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2693114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2701999SN/A
2713079Sstever@eecs.umich.edu/// Target rename() handler.
2723079Sstever@eecs.umich.eduSyscallReturn renameFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2743079Sstever@eecs.umich.edu
2752093SN/A
2762093SN/A/// Target truncate() handler.
2773114Sgblack@eecs.umich.eduSyscallReturn truncateFunc(SyscallDesc *desc, int num,
2782093SN/A                           LiveProcess *p, ThreadContext *tc);
2792687Sksewell@umich.edu
2802687Sksewell@umich.edu
2813114Sgblack@eecs.umich.edu/// Target ftruncate() handler.
2822687Sksewell@umich.eduSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2832238SN/A                            LiveProcess *p, ThreadContext *tc);
2842238SN/A
2853114Sgblack@eecs.umich.edu
2862238SN/A/// Target truncate64() handler.
2872238SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num,
2882238SN/A                             LiveProcess *p, ThreadContext *tc);
2893114Sgblack@eecs.umich.edu
2902238SN/A/// Target ftruncate64() handler.
2912238SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2922238SN/A                              LiveProcess *p, ThreadContext *tc);
2933114Sgblack@eecs.umich.edu
2942238SN/A
2952238SN/A/// Target umask() handler.
2962238SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num,
2973114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2982238SN/A
2992238SN/A
3002238SN/A/// Target chown() handler.
3013114Sgblack@eecs.umich.eduSyscallReturn chownFunc(SyscallDesc *desc, int num,
3022238SN/A                        LiveProcess *p, ThreadContext *tc);
3032238SN/A
3042238SN/A
3053114Sgblack@eecs.umich.edu/// Target fchown() handler.
3062238SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
3072238SN/A                         LiveProcess *p, ThreadContext *tc);
3082238SN/A
3093114Sgblack@eecs.umich.edu/// Target dup() handler.
3102238SN/ASyscallReturn dupFunc(SyscallDesc *desc, int num,
3112238SN/A                      LiveProcess *process, ThreadContext *tc);
3122238SN/A
3132238SN/A/// Target fnctl() handler.
3142238SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
3152238SN/A                        LiveProcess *process, ThreadContext *tc);
3163114Sgblack@eecs.umich.edu
3172238SN/A/// Target fcntl64() handler.
3182238SN/ASyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3192238SN/A                        LiveProcess *process, ThreadContext *tc);
3203114Sgblack@eecs.umich.edu
3212238SN/A/// Target setuid() handler.
3222238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3232238SN/A                               LiveProcess *p, ThreadContext *tc);
3243114Sgblack@eecs.umich.edu
3252238SN/A/// Target getpid() handler.
3262238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3272238SN/A                               LiveProcess *p, ThreadContext *tc);
3283114Sgblack@eecs.umich.edu
3292238SN/A/// Target getuid() handler.
3302238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3311354SN/A                               LiveProcess *p, ThreadContext *tc);
3321354SN/A
3331354SN/A/// Target getgid() handler.
3341354SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3351354SN/A                               LiveProcess *p, ThreadContext *tc);
3361354SN/A
3371354SN/A/// Target getppid() handler.
3381354SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3391354SN/A                               LiveProcess *p, ThreadContext *tc);
3401354SN/A
3411354SN/A/// Target geteuid() handler.
3421354SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3431354SN/A                               LiveProcess *p, ThreadContext *tc);
3441354SN/A
3451609SN/A/// Target getegid() handler.
3461354SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3471354SN/A                               LiveProcess *p, ThreadContext *tc);
3481354SN/A
3491354SN/A/// Target clone() handler.
350360SN/ASyscallReturn cloneFunc(SyscallDesc *desc, int num,
351360SN/A                               LiveProcess *p, ThreadContext *tc);
352360SN/A
353360SN/A/// Target access() handler
354360SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num,
355360SN/A                               LiveProcess *p, ThreadContext *tc);
356360SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num,
3573113Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc,
3583113Sgblack@eecs.umich.edu                               int index);
3593113Sgblack@eecs.umich.edu
3603113Sgblack@eecs.umich.edu/// Futex system call
3613113Sgblack@eecs.umich.edu///  Implemented by Daniel Sanchez
3623113Sgblack@eecs.umich.edu///  Used by printf's in multi-threaded apps
3633113Sgblack@eecs.umich.edutemplate <class OS>
3643113Sgblack@eecs.umich.eduSyscallReturn
3653113Sgblack@eecs.umich.edufutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3663113Sgblack@eecs.umich.edu          ThreadContext *tc)
3673113Sgblack@eecs.umich.edu{
3683113Sgblack@eecs.umich.edu    int index_uaddr = 0;
3693113Sgblack@eecs.umich.edu    int index_op = 1;
3703113Sgblack@eecs.umich.edu    int index_val = 2;
3713113Sgblack@eecs.umich.edu    int index_timeout = 3;
3723113Sgblack@eecs.umich.edu
3734189Sgblack@eecs.umich.edu    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3744189Sgblack@eecs.umich.edu    int op = process->getSyscallArg(tc, index_op);
3753113Sgblack@eecs.umich.edu    int val = process->getSyscallArg(tc, index_val);
3763113Sgblack@eecs.umich.edu    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3773113Sgblack@eecs.umich.edu
3783113Sgblack@eecs.umich.edu    std::map<uint64_t, std::list<ThreadContext *> * >
3793113Sgblack@eecs.umich.edu        &futex_map = tc->getSystemPtr()->futexMap;
3803113Sgblack@eecs.umich.edu
3813113Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3823277Sgblack@eecs.umich.edu            uaddr, op, val);
3835515SMichael.Adler@intel.com
3845515SMichael.Adler@intel.com    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3855515SMichael.Adler@intel.com
3865515SMichael.Adler@intel.com    if (op == OS::TGT_FUTEX_WAIT) {
3875515SMichael.Adler@intel.com        if (timeout != 0) {
3883277Sgblack@eecs.umich.edu            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3893277Sgblack@eecs.umich.edu                 "we'll wait indefinitely");
3903277Sgblack@eecs.umich.edu        }
3913277Sgblack@eecs.umich.edu
3923277Sgblack@eecs.umich.edu        uint8_t *buf = new uint8_t[sizeof(int)];
3933277Sgblack@eecs.umich.edu        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3943277Sgblack@eecs.umich.edu        int mem_val = *((int *)buf);
3953113Sgblack@eecs.umich.edu        delete buf;
3963113Sgblack@eecs.umich.edu
3973113Sgblack@eecs.umich.edu        if(val != mem_val) {
3983113Sgblack@eecs.umich.edu            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3993113Sgblack@eecs.umich.edu                                    "expected: %d\n", mem_val, val);
4003113Sgblack@eecs.umich.edu            return -OS::TGT_EWOULDBLOCK;
4013113Sgblack@eecs.umich.edu        }
4023114Sgblack@eecs.umich.edu
4033113Sgblack@eecs.umich.edu        // Queue the thread context
4043114Sgblack@eecs.umich.edu        std::list<ThreadContext *> * tcWaitList;
4053113Sgblack@eecs.umich.edu        if (futex_map.count(uaddr)) {
4063114Sgblack@eecs.umich.edu            tcWaitList = futex_map.find(uaddr)->second;
4073113Sgblack@eecs.umich.edu        } else {
4084061Sgblack@eecs.umich.edu            tcWaitList = new std::list<ThreadContext *>();
4094061Sgblack@eecs.umich.edu            futex_map.insert(std::pair< uint64_t,
4104061Sgblack@eecs.umich.edu                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
4113113Sgblack@eecs.umich.edu        }
4123113Sgblack@eecs.umich.edu        tcWaitList->push_back(tc);
4133113Sgblack@eecs.umich.edu        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
4143113Sgblack@eecs.umich.edu                                "thread context\n");
4153113Sgblack@eecs.umich.edu        tc->suspend();
4163113Sgblack@eecs.umich.edu        return 0;
4173113Sgblack@eecs.umich.edu    } else if (op == OS::TGT_FUTEX_WAKE){
4183113Sgblack@eecs.umich.edu        int wokenUp = 0;
4193113Sgblack@eecs.umich.edu        std::list<ThreadContext *> * tcWaitList;
4203113Sgblack@eecs.umich.edu        if (futex_map.count(uaddr)) {
4213113Sgblack@eecs.umich.edu            tcWaitList = futex_map.find(uaddr)->second;
4224189Sgblack@eecs.umich.edu            while (tcWaitList->size() > 0 && wokenUp < val) {
4234189Sgblack@eecs.umich.edu                tcWaitList->front()->activate();
4243113Sgblack@eecs.umich.edu                tcWaitList->pop_front();
4253113Sgblack@eecs.umich.edu                wokenUp++;
4263113Sgblack@eecs.umich.edu            }
4273113Sgblack@eecs.umich.edu            if(tcWaitList->empty()) {
4283113Sgblack@eecs.umich.edu                futex_map.erase(uaddr);
4293113Sgblack@eecs.umich.edu                delete tcWaitList;
4303113Sgblack@eecs.umich.edu            }
4313113Sgblack@eecs.umich.edu        }
4323113Sgblack@eecs.umich.edu        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4333113Sgblack@eecs.umich.edu                                "thread contexts\n", wokenUp);
4343113Sgblack@eecs.umich.edu        return wokenUp;
4353113Sgblack@eecs.umich.edu    } else {
4363113Sgblack@eecs.umich.edu        warn("sys_futex: op %d is not implemented, just returning...", op);
4373113Sgblack@eecs.umich.edu        return 0;
4383113Sgblack@eecs.umich.edu    }
4393113Sgblack@eecs.umich.edu
4403113Sgblack@eecs.umich.edu}
4413113Sgblack@eecs.umich.edu
4423113Sgblack@eecs.umich.edu/// Target getdents() handler.
4433113Sgblack@eecs.umich.eduSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
4443113Sgblack@eecs.umich.edu                           LiveProcess *process, ThreadContext *tc);
4453113Sgblack@eecs.umich.edu
4463113Sgblack@eecs.umich.edu/// Target getdents64() handler.
4473113Sgblack@eecs.umich.eduSyscallReturn getdents64Func(SyscallDesc *desc, int num,
4483113Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4493113Sgblack@eecs.umich.edu
4503113Sgblack@eecs.umich.edu
4513113Sgblack@eecs.umich.edu/// Pseudo Funcs  - These functions use a different return convension,
4523113Sgblack@eecs.umich.edu/// returning a second value in a register other than the normal return register
4533113Sgblack@eecs.umich.eduSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4543113Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4553113Sgblack@eecs.umich.edu
4563113Sgblack@eecs.umich.edu/// Target getpidPseudo() handler.
4573113Sgblack@eecs.umich.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4583113Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4593113Sgblack@eecs.umich.edu
4603113Sgblack@eecs.umich.edu/// Target getuidPseudo() handler.
4613113Sgblack@eecs.umich.eduSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
462378SN/A                               LiveProcess *p, ThreadContext *tc);
463378SN/A
464378SN/A/// Target getgidPseudo() handler.
465360SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4661450SN/A                               LiveProcess *p, ThreadContext *tc);
4673114Sgblack@eecs.umich.edu
4682680Sktlim@umich.edu
469360SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4705958Sgblack@eecs.umich.educonst int one_million = 1000000;
4715958Sgblack@eecs.umich.edu
472360SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4731969SN/A/// by my reckoning.  We want to keep this a constant (not use the
474360SN/A/// real-world time) to keep simulations repeatable.
475360SN/Aconst unsigned seconds_since_epoch = 1000000000;
476360SN/A
4771458SN/A/// Helper function to convert current elapsed time to seconds and
478360SN/A/// microseconds.
479360SN/Atemplate <class T1, class T2>
480360SN/Avoid
4814131Sbinkertn@umich.edugetElapsedTime(T1 &sec, T2 &usec)
4824131Sbinkertn@umich.edu{
4834131Sbinkertn@umich.edu    int elapsed_usecs = curTick() / SimClock::Int::us;
4844131Sbinkertn@umich.edu    sec = elapsed_usecs / one_million;
4854131Sbinkertn@umich.edu    usec = elapsed_usecs % one_million;
4864131Sbinkertn@umich.edu}
4874131Sbinkertn@umich.edu
4884131Sbinkertn@umich.edu//////////////////////////////////////////////////////////////////////
4891458SN/A//
490360SN/A// The following emulation functions are generic, but need to be
491360SN/A// templated to account for differences in types, constants, etc.
4921706SN/A//
4932680Sktlim@umich.edu//////////////////////////////////////////////////////////////////////
494360SN/A
495360SN/A#if NO_STAT64
496360SN/A    typedef struct stat hst_stat;
497378SN/A    typedef struct stat hst_stat64;
498360SN/A#else
4991450SN/A    typedef struct stat hst_stat;
5003114Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
5012680Sktlim@umich.edu#endif
502360SN/A
503360SN/A//// Helper function to convert a host stat buffer to a target stat
504360SN/A//// buffer.  Also copies the target buffer out to the simulated
5055958Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
5061458SN/A
507360SN/Atemplate <typename target_stat, typename host_stat>
508360SN/Astatic void
509360SN/AconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
510360SN/A{
5111706SN/A    using namespace TheISA;
5121458SN/A
513360SN/A    if (fakeTTY)
514360SN/A        tgt->st_dev = 0xA;
5155958Sgblack@eecs.umich.edu    else
5165958Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
517360SN/A    tgt->st_dev = TheISA::htog(tgt->st_dev);
518360SN/A    tgt->st_ino = host->st_ino;
519360SN/A    tgt->st_ino = TheISA::htog(tgt->st_ino);
520360SN/A    tgt->st_mode = host->st_mode;
521360SN/A    if (fakeTTY) {
522360SN/A        // Claim to be a character device
523360SN/A        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
524360SN/A        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
525360SN/A    }
526360SN/A    tgt->st_mode = TheISA::htog(tgt->st_mode);
527360SN/A    tgt->st_nlink = host->st_nlink;
528360SN/A    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5291706SN/A    tgt->st_uid = host->st_uid;
530360SN/A    tgt->st_uid = TheISA::htog(tgt->st_uid);
531360SN/A    tgt->st_gid = host->st_gid;
532360SN/A    tgt->st_gid = TheISA::htog(tgt->st_gid);
533360SN/A    if (fakeTTY)
534360SN/A        tgt->st_rdev = 0x880d;
5353669Sbinkertn@umich.edu    else
5363669Sbinkertn@umich.edu        tgt->st_rdev = host->st_rdev;
5373669Sbinkertn@umich.edu    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5381706SN/A    tgt->st_size = host->st_size;
5391706SN/A    tgt->st_size = TheISA::htog(tgt->st_size);
5405795Ssaidi@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5415795Ssaidi@eecs.umich.edu    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5425795Ssaidi@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5435795Ssaidi@eecs.umich.edu    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5445795Ssaidi@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5455795Ssaidi@eecs.umich.edu    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5465795Ssaidi@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5475795Ssaidi@eecs.umich.edu    // consistently across different hosts.
5485795Ssaidi@eecs.umich.edu    tgt->st_blksize = 0x2000;
5495795Ssaidi@eecs.umich.edu    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5505795Ssaidi@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
551360SN/A    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
552360SN/A}
553360SN/A
554360SN/A// Same for stat64
5551999SN/A
5561999SN/Atemplate <typename target_stat, typename host_stat64>
5571999SN/Astatic void
5583114Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5592680Sktlim@umich.edu{
5601999SN/A    using namespace TheISA;
5611999SN/A
5621999SN/A    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5635958Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5641999SN/A    tgt->st_atime_nsec = host->st_atime_nsec;
5651999SN/A    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5665958Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5671999SN/A    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5681999SN/A    tgt->st_ctime_nsec = host->st_ctime_nsec;
5691999SN/A    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5701999SN/A#else
5711999SN/A    tgt->st_atime_nsec = 0;
5723669Sbinkertn@umich.edu    tgt->st_mtime_nsec = 0;
5733669Sbinkertn@umich.edu    tgt->st_ctime_nsec = 0;
5743669Sbinkertn@umich.edu#endif
5751999SN/A}
5761999SN/A
5771999SN/A//Here are a couple convenience functions
5782218SN/Atemplate<class OS>
5791999SN/Astatic void
5801999SN/AcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5811999SN/A        hst_stat *host, bool fakeTTY = false)
5821999SN/A{
5831999SN/A    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5841999SN/A    tgt_stat_buf tgt(addr);
5851999SN/A    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5861999SN/A    tgt.copyOut(mem);
5873114Sgblack@eecs.umich.edu}
5882680Sktlim@umich.edu
5891999SN/Atemplate<class OS>
5905958Sgblack@eecs.umich.edustatic void
5911999SN/AcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5921999SN/A        hst_stat64 *host, bool fakeTTY = false)
5931999SN/A{
5941999SN/A    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5951999SN/A    tgt_stat_buf tgt(addr);
5965958Sgblack@eecs.umich.edu    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5971999SN/A    tgt.copyOut(mem);
5981999SN/A}
5991999SN/A
6001999SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
6011999SN/A/// only to find out if their stdout is a tty, to determine whether to
6021999SN/A/// do line or block buffering.  We always claim that output fds are
6031999SN/A/// not TTYs to provide repeatable results.
6041999SN/Atemplate <class OS>
6052218SN/ASyscallReturn
6061999SN/AioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6071999SN/A          ThreadContext *tc)
6081999SN/A{
6091999SN/A    int index = 0;
6105877Shsul@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6115877Shsul@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
6125877Shsul@eecs.umich.edu
6135877Shsul@eecs.umich.edu    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
6145877Shsul@eecs.umich.edu
6155958Sgblack@eecs.umich.edu    if (fd < 0 || process->sim_fd(fd) < 0) {
6165958Sgblack@eecs.umich.edu        // doesn't map to any simulator fd: not a valid target fd
6175958Sgblack@eecs.umich.edu        return -EBADF;
6185958Sgblack@eecs.umich.edu    }
6195877Shsul@eecs.umich.edu
6205877Shsul@eecs.umich.edu    if (OS::isTtyReq(req)) {
6215877Shsul@eecs.umich.edu        return -ENOTTY;
6225877Shsul@eecs.umich.edu    }
6235877Shsul@eecs.umich.edu
6245877Shsul@eecs.umich.edu    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
6255877Shsul@eecs.umich.edu         fd, req, tc->pcState());
6265877Shsul@eecs.umich.edu    return -ENOTTY;
6275877Shsul@eecs.umich.edu}
6285877Shsul@eecs.umich.edu
6295877Shsul@eecs.umich.edutemplate <class OS>
6305877Shsul@eecs.umich.edustatic SyscallReturn
6315877Shsul@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6325877Shsul@eecs.umich.edu         ThreadContext *tc, int index)
6335877Shsul@eecs.umich.edu{
6345877Shsul@eecs.umich.edu    std::string path;
6355877Shsul@eecs.umich.edu
6365877Shsul@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
6375877Shsul@eecs.umich.edu                process->getSyscallArg(tc, index)))
6385877Shsul@eecs.umich.edu        return -EFAULT;
6395877Shsul@eecs.umich.edu
6405877Shsul@eecs.umich.edu    if (path == "/dev/sysdev0") {
6415877Shsul@eecs.umich.edu        // This is a memory-mapped high-resolution timer device on Alpha.
6425877Shsul@eecs.umich.edu        // We don't support it, so just punt.
6435877Shsul@eecs.umich.edu        warn("Ignoring open(%s, ...)\n", path);
6445877Shsul@eecs.umich.edu        return -ENOENT;
6455877Shsul@eecs.umich.edu    }
6465877Shsul@eecs.umich.edu
6475877Shsul@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6485877Shsul@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
6495877Shsul@eecs.umich.edu    int hostFlags = 0;
6505877Shsul@eecs.umich.edu
6515877Shsul@eecs.umich.edu    // translate open flags
6525877Shsul@eecs.umich.edu    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
6535877Shsul@eecs.umich.edu        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
6545877Shsul@eecs.umich.edu            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
6551999SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
656378SN/A        }
657360SN/A    }
6581450SN/A
6593114Sgblack@eecs.umich.edu    // any target flags left?
6602680Sktlim@umich.edu    if (tgtFlags != 0)
661360SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
662360SN/A
663360SN/A#ifdef __CYGWIN32__
6645958Sgblack@eecs.umich.edu    hostFlags |= O_BINARY;
6652400SN/A#endif
666360SN/A
6673669Sbinkertn@umich.edu    // Adjust path for current working directory
6683669Sbinkertn@umich.edu    path = process->fullPath(path);
6693669Sbinkertn@umich.edu
670360SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
671360SN/A
672360SN/A    int fd;
673360SN/A    int local_errno;
6742218SN/A    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
675360SN/A        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6765958Sgblack@eecs.umich.edu        // It's a proc/sys entry and requires special handling
6775958Sgblack@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
678360SN/A        local_errno = ENOENT;
6791458SN/A     } else {
680360SN/A        // open the file
681360SN/A        fd = open(path.c_str(), hostFlags, mode);
682360SN/A        local_errno = errno;
6835074Ssaidi@eecs.umich.edu     }
6845074Ssaidi@eecs.umich.edu
6855074Ssaidi@eecs.umich.edu    if (fd == -1)
6865074Ssaidi@eecs.umich.edu        return -local_errno;
6875074Ssaidi@eecs.umich.edu
6885074Ssaidi@eecs.umich.edu    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
6895074Ssaidi@eecs.umich.edu}
6905074Ssaidi@eecs.umich.edu
6915958Sgblack@eecs.umich.edu/// Target open() handler.
6925074Ssaidi@eecs.umich.edutemplate <class OS>
6935074Ssaidi@eecs.umich.eduSyscallReturn
6945074Ssaidi@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6955074Ssaidi@eecs.umich.edu         ThreadContext *tc)
6965074Ssaidi@eecs.umich.edu{
6975208Ssaidi@eecs.umich.edu    return openFunc<OS>(desc, callnum, process, tc, 0);
6985208Ssaidi@eecs.umich.edu}
6995208Ssaidi@eecs.umich.edu
7005208Ssaidi@eecs.umich.edu/// Target openat() handler.
7015074Ssaidi@eecs.umich.edutemplate <class OS>
7025074Ssaidi@eecs.umich.eduSyscallReturn
7035208Ssaidi@eecs.umich.eduopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7045074Ssaidi@eecs.umich.edu         ThreadContext *tc)
7055074Ssaidi@eecs.umich.edu{
7065074Ssaidi@eecs.umich.edu    int index = 0;
7075074Ssaidi@eecs.umich.edu    int dirfd = process->getSyscallArg(tc, index);
7085958Sgblack@eecs.umich.edu    if (dirfd != OS::TGT_AT_FDCWD)
7095958Sgblack@eecs.umich.edu        warn("openat: first argument not AT_FDCWD; unlikely to work");
7105074Ssaidi@eecs.umich.edu    return openFunc<OS>(desc, callnum, process, tc, 1);
7115074Ssaidi@eecs.umich.edu}
7125074Ssaidi@eecs.umich.edu
7135074Ssaidi@eecs.umich.edu/// Target facessat() handler
7145074Ssaidi@eecs.umich.edutemplate <class OS>
7151999SN/ASyscallReturn
7161999SN/AfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7171999SN/A        ThreadContext *tc)
7183114Sgblack@eecs.umich.edu{
7192680Sktlim@umich.edu    int index = 0;
7201999SN/A    int dirfd = process->getSyscallArg(tc, index);
7215958Sgblack@eecs.umich.edu    if (dirfd != OS::TGT_AT_FDCWD)
7221999SN/A        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
7231999SN/A    return accessFunc(desc, callnum, process, tc, 1);
7241999SN/A}
7251999SN/A
7261999SN/A/// Target readlinkat() handler
7272764Sstever@eecs.umich.edutemplate <class OS>
7282064SN/ASyscallReturn
7292064SN/AreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7302064SN/A        ThreadContext *tc)
7312064SN/A{
7321999SN/A    int index = 0;
7332064SN/A    int dirfd = process->getSyscallArg(tc, index);
7341999SN/A    if (dirfd != OS::TGT_AT_FDCWD)
7351999SN/A        warn("openat: first argument not AT_FDCWD; unlikely to work");
7362218SN/A    return readlinkFunc(desc, callnum, process, tc, 1);
7371999SN/A}
7385958Sgblack@eecs.umich.edu
7393114Sgblack@eecs.umich.edu/// Target sysinfo() handler.
7401999SN/Atemplate <class OS>
7411999SN/ASyscallReturn
7421999SN/AsysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7431999SN/A         ThreadContext *tc)
7441999SN/A{
745378SN/A
746360SN/A    int index = 0;
7471450SN/A    TypedBufferArg<typename OS::tgt_sysinfo>
7483114Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
7492680Sktlim@umich.edu
750360SN/A    sysinfo->uptime=seconds_since_epoch;
751360SN/A    sysinfo->totalram=process->system->memSize();
752360SN/A
7535958Sgblack@eecs.umich.edu    sysinfo.copyOut(tc->getMemProxy());
7542400SN/A
755360SN/A    return 0;
7563669Sbinkertn@umich.edu}
7573669Sbinkertn@umich.edu
7583669Sbinkertn@umich.edu/// Target chmod() handler.
759360SN/Atemplate <class OS>
760360SN/ASyscallReturn
761360SN/AchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
762360SN/A          ThreadContext *tc)
7631458SN/A{
764360SN/A    std::string path;
7655958Sgblack@eecs.umich.edu
7665958Sgblack@eecs.umich.edu    int index = 0;
767360SN/A    if (!tc->getMemProxy().tryReadString(path,
7681458SN/A                process->getSyscallArg(tc, index))) {
769360SN/A        return -EFAULT;
770360SN/A    }
7711999SN/A
7721999SN/A    uint32_t mode = process->getSyscallArg(tc, index);
7731999SN/A    mode_t hostMode = 0;
7743114Sgblack@eecs.umich.edu
7752680Sktlim@umich.edu    // XXX translate mode flags via OS::something???
7761999SN/A    hostMode = mode;
7771999SN/A
7781999SN/A    // Adjust path for current working directory
7795958Sgblack@eecs.umich.edu    path = process->fullPath(path);
7802400SN/A
7811999SN/A    // do the chmod
7823669Sbinkertn@umich.edu    int result = chmod(path.c_str(), hostMode);
7833669Sbinkertn@umich.edu    if (result < 0)
7843669Sbinkertn@umich.edu        return -errno;
7852764Sstever@eecs.umich.edu
7862064SN/A    return 0;
7872064SN/A}
7882064SN/A
7891999SN/A
7901999SN/A/// Target fchmod() handler.
7912064SN/Atemplate <class OS>
7921999SN/ASyscallReturn
7931999SN/AfchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7941999SN/A           ThreadContext *tc)
7951999SN/A{
7965958Sgblack@eecs.umich.edu    int index = 0;
7975958Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7981999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7991999SN/A        // doesn't map to any simulator fd: not a valid target fd
8001999SN/A        return -EBADF;
8011999SN/A    }
802378SN/A
803360SN/A    uint32_t mode = process->getSyscallArg(tc, index);
8041450SN/A    mode_t hostMode = 0;
8053114Sgblack@eecs.umich.edu
8062680Sktlim@umich.edu    // XXX translate mode flags via OS::someting???
807360SN/A    hostMode = mode;
8085958Sgblack@eecs.umich.edu
809360SN/A    // do the fchmod
8101969SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
811360SN/A    if (result < 0)
812360SN/A        return -errno;
8131458SN/A
814360SN/A    return 0;
815360SN/A}
816360SN/A
817360SN/A/// Target mremap() handler.
818360SN/Atemplate <class OS>
8191458SN/ASyscallReturn
820360SN/AmremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8215958Sgblack@eecs.umich.edu{
8223114Sgblack@eecs.umich.edu    int index = 0;
8232021SN/A    Addr start = process->getSyscallArg(tc, index);
8241458SN/A    uint64_t old_length = process->getSyscallArg(tc, index);
825360SN/A    uint64_t new_length = process->getSyscallArg(tc, index);
826360SN/A    uint64_t flags = process->getSyscallArg(tc, index);
827360SN/A    uint64_t provided_address = 0;
8281706SN/A    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
8291706SN/A
8301706SN/A    if (use_provided_address)
8313114Sgblack@eecs.umich.edu        provided_address = process->getSyscallArg(tc, index);
8322680Sktlim@umich.edu
8331706SN/A    if ((start % TheISA::PageBytes != 0) ||
8341706SN/A        (new_length % TheISA::PageBytes != 0) ||
8351706SN/A        (provided_address % TheISA::PageBytes != 0)) {
8365958Sgblack@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8372400SN/A        return -EINVAL;
8381706SN/A    }
8393669Sbinkertn@umich.edu
8403669Sbinkertn@umich.edu    if (new_length > old_length) {
8413669Sbinkertn@umich.edu        if ((start + old_length) == process->mmap_end &&
8421706SN/A            (!use_provided_address || provided_address == start)) {
8431706SN/A            uint64_t diff = new_length - old_length;
8441706SN/A            process->allocateMem(process->mmap_end, diff);
8451706SN/A            process->mmap_end += diff;
8462218SN/A            return start;
8471706SN/A        } else {
8483114Sgblack@eecs.umich.edu            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8495958Sgblack@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8501706SN/A                return -ENOMEM;
8511706SN/A            } else {
8521706SN/A                uint64_t new_start = use_provided_address ?
8531706SN/A                    provided_address : process->mmap_end;
8541706SN/A                process->pTable->remap(start, old_length, new_start);
8551706SN/A                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
8561706SN/A                     new_start, new_start + new_length,
8571706SN/A                     new_length - old_length);
8583114Sgblack@eecs.umich.edu                // add on the remaining unallocated pages
8592680Sktlim@umich.edu                process->allocateMem(new_start + old_length,
8601706SN/A                                     new_length - old_length,
8615958Sgblack@eecs.umich.edu                                     use_provided_address /* clobber */);
8621706SN/A                if (!use_provided_address)
8631706SN/A                    process->mmap_end += new_length;
8641706SN/A                if (use_provided_address &&
8651706SN/A                    new_start + new_length > process->mmap_end) {
8661706SN/A                    // something fishy going on here, at least notify the user
8671706SN/A                    // @todo: increase mmap_end?
8681706SN/A                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
8691706SN/A                }
8702218SN/A                warn("returning %08p as start\n", new_start);
8711706SN/A                return new_start;
8725958Sgblack@eecs.umich.edu            }
8733114Sgblack@eecs.umich.edu        }
8741706SN/A    } else {
8751706SN/A        if (use_provided_address && provided_address != start)
8761706SN/A            process->pTable->remap(start, new_length, provided_address);
8771706SN/A        process->pTable->unmap(start + new_length, old_length - new_length);
8781706SN/A        return use_provided_address ? provided_address : start;
8791999SN/A    }
8801999SN/A}
8811999SN/A
8823114Sgblack@eecs.umich.edu/// Target stat() handler.
8832680Sktlim@umich.edutemplate <class OS>
8841999SN/ASyscallReturn
8855958Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8861999SN/A         ThreadContext *tc)
8871999SN/A{
8881999SN/A    std::string path;
8891999SN/A
8901999SN/A    int index = 0;
8912680Sktlim@umich.edu    if (!tc->getMemProxy().tryReadString(path,
8925958Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8935958Sgblack@eecs.umich.edu        return -EFAULT;
8941999SN/A    }
8951999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
8961999SN/A
8971999SN/A    // Adjust path for current working directory
8982461SN/A    path = process->fullPath(path);
8992461SN/A
9002461SN/A    struct stat hostBuf;
9012091SN/A    int result = stat(path.c_str(), &hostBuf);
9021999SN/A
9032461SN/A    if (result < 0)
9042461SN/A        return -errno;
9051999SN/A
9061999SN/A    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9071999SN/A
9081999SN/A    return 0;
9091999SN/A}
9101999SN/A
9111999SN/A
9121999SN/A/// Target stat64() handler.
9131999SN/Atemplate <class OS>
9141999SN/ASyscallReturn
9152218SN/Astat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9161999SN/A           ThreadContext *tc)
9171999SN/A{
9181999SN/A    std::string path;
9191999SN/A
9201999SN/A    int index = 0;
921378SN/A    if (!tc->getMemProxy().tryReadString(path,
922378SN/A                process->getSyscallArg(tc, index)))
923378SN/A        return -EFAULT;
924378SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
925378SN/A
926378SN/A    // Adjust path for current working directory
927378SN/A    path = process->fullPath(path);
928378SN/A
929360SN/A#if NO_STAT64
930378SN/A    struct stat  hostBuf;
931378SN/A    int result = stat(path.c_str(), &hostBuf);
932378SN/A#else
933360SN/A    struct stat64 hostBuf;
9341450SN/A    int result = stat64(path.c_str(), &hostBuf);
9353114Sgblack@eecs.umich.edu#endif
936360SN/A
9375958Sgblack@eecs.umich.edu    if (result < 0)
9385958Sgblack@eecs.umich.edu        return -errno;
9395958Sgblack@eecs.umich.edu
9405958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9415958Sgblack@eecs.umich.edu
9425958Sgblack@eecs.umich.edu    return 0;
943360SN/A}
9445877Shsul@eecs.umich.edu
9452544SN/A
9462544SN/A/// Target fstatat64() handler.
9472544SN/Atemplate <class OS>
9482544SN/ASyscallReturn
9492544SN/Afstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9502544SN/A              ThreadContext *tc)
951360SN/A{
952360SN/A    int index = 0;
9532544SN/A    int dirfd = process->getSyscallArg(tc, index);
9542544SN/A    if (dirfd != OS::TGT_AT_FDCWD)
9552544SN/A        warn("openat: first argument not AT_FDCWD; unlikely to work");
9562544SN/A
9572544SN/A    std::string path;
9582544SN/A    if (!tc->getMemProxy().tryReadString(path,
9592544SN/A                process->getSyscallArg(tc, index)))
9602544SN/A        return -EFAULT;
9612544SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
9622544SN/A
9632553SN/A    // Adjust path for current working directory
9641969SN/A    path = process->fullPath(path);
9655958Sgblack@eecs.umich.edu
966360SN/A#if NO_STAT64
967360SN/A    struct stat  hostBuf;
9681458SN/A    int result = stat(path.c_str(), &hostBuf);
969360SN/A#else
970360SN/A    struct stat64 hostBuf;
971378SN/A    int result = stat64(path.c_str(), &hostBuf);
972360SN/A#endif
9731450SN/A
9743114Sgblack@eecs.umich.edu    if (result < 0)
9752680Sktlim@umich.edu        return -errno;
976360SN/A
9775958Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9785958Sgblack@eecs.umich.edu
979360SN/A    return 0;
980360SN/A}
9812064SN/A
9825877Shsul@eecs.umich.edu
9832064SN/A/// Target fstat64() handler.
9842091SN/Atemplate <class OS>
9852091SN/ASyscallReturn
9862064SN/Afstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
987360SN/A            ThreadContext *tc)
9885877Shsul@eecs.umich.edu{
9895877Shsul@eecs.umich.edu    int index = 0;
9905877Shsul@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9915877Shsul@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9925877Shsul@eecs.umich.edu    if (fd < 0 || process->sim_fd(fd) < 0) {
9935877Shsul@eecs.umich.edu        // doesn't map to any simulator fd: not a valid target fd
9945877Shsul@eecs.umich.edu        return -EBADF;
9952064SN/A    }
9962064SN/A
9972064SN/A#if NO_STAT64
9982064SN/A    struct stat  hostBuf;
9992064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
1000360SN/A#else
1001360SN/A    struct stat64  hostBuf;
10022680Sktlim@umich.edu    int result = fstat64(process->sim_fd(fd), &hostBuf);
10031458SN/A#endif
1004360SN/A
1005360SN/A    if (result < 0)
1006378SN/A        return -errno;
1007360SN/A
10081450SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10093114Sgblack@eecs.umich.edu
10102680Sktlim@umich.edu    return 0;
1011360SN/A}
10125958Sgblack@eecs.umich.edu
1013360SN/A
1014360SN/A/// Target lstat() handler.
1015360SN/Atemplate <class OS>
10162091SN/ASyscallReturn
10172091SN/AlstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1018360SN/A          ThreadContext *tc)
10192680Sktlim@umich.edu{
1020360SN/A    std::string path;
10211458SN/A
1022360SN/A    int index = 0;
1023360SN/A    if (!tc->getMemProxy().tryReadString(path,
1024360SN/A                process->getSyscallArg(tc, index))) {
10251999SN/A        return -EFAULT;
10261999SN/A    }
10271999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
10283114Sgblack@eecs.umich.edu
10292680Sktlim@umich.edu    // Adjust path for current working directory
10301999SN/A    path = process->fullPath(path);
10311999SN/A
10321999SN/A    struct stat hostBuf;
10335958Sgblack@eecs.umich.edu    int result = lstat(path.c_str(), &hostBuf);
10342400SN/A
10351999SN/A    if (result < 0)
10365958Sgblack@eecs.umich.edu        return -errno;
10372680Sktlim@umich.edu
10381999SN/A    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10391999SN/A
10401999SN/A    return 0;
10411999SN/A}
10422091SN/A
10432091SN/A/// Target lstat64() handler.
10441999SN/Atemplate <class OS>
10453669Sbinkertn@umich.eduSyscallReturn
10463669Sbinkertn@umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10473669Sbinkertn@umich.edu            ThreadContext *tc)
10483669Sbinkertn@umich.edu{
10491999SN/A    std::string path;
10501999SN/A
10511999SN/A    int index = 0;
10521999SN/A    if (!tc->getMemProxy().tryReadString(path,
10531999SN/A                process->getSyscallArg(tc, index))) {
10541999SN/A        return -EFAULT;
10551999SN/A    }
1056378SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
1057360SN/A
10581450SN/A    // Adjust path for current working directory
10593114Sgblack@eecs.umich.edu    path = process->fullPath(path);
10602680Sktlim@umich.edu
1061360SN/A#if NO_STAT64
10625958Sgblack@eecs.umich.edu    struct stat hostBuf;
10635958Sgblack@eecs.umich.edu    int result = lstat(path.c_str(), &hostBuf);
1064360SN/A#else
10653670Sbinkertn@umich.edu    struct stat64 hostBuf;
10663670Sbinkertn@umich.edu    int result = lstat64(path.c_str(), &hostBuf);
1067360SN/A#endif
1068360SN/A
1069360SN/A    if (result < 0)
1070360SN/A        return -errno;
1071360SN/A
1072360SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1073360SN/A
1074360SN/A    return 0;
1075360SN/A}
1076360SN/A
1077360SN/A/// Target fstat() handler.
1078360SN/Atemplate <class OS>
1079360SN/ASyscallReturn
1080360SN/AfstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1081360SN/A          ThreadContext *tc)
1082360SN/A{
1083360SN/A    int index = 0;
10843670Sbinkertn@umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10853670Sbinkertn@umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10863670Sbinkertn@umich.edu
10873670Sbinkertn@umich.edu    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
10883670Sbinkertn@umich.edu
10893670Sbinkertn@umich.edu    if (fd < 0)
10903670Sbinkertn@umich.edu        return -EBADF;
10913670Sbinkertn@umich.edu
10923670Sbinkertn@umich.edu    struct stat hostBuf;
10933670Sbinkertn@umich.edu    int result = fstat(fd, &hostBuf);
10943670Sbinkertn@umich.edu
10953670Sbinkertn@umich.edu    if (result < 0)
10963670Sbinkertn@umich.edu        return -errno;
10973670Sbinkertn@umich.edu
10983670Sbinkertn@umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10993670Sbinkertn@umich.edu
11003670Sbinkertn@umich.edu    return 0;
11013670Sbinkertn@umich.edu}
11022680Sktlim@umich.edu
1103360SN/A
11041458SN/A/// Target statfs() handler.
1105360SN/Atemplate <class OS>
1106360SN/ASyscallReturn
11072553SN/AstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11082553SN/A           ThreadContext *tc)
11092553SN/A{
11101354SN/A    std::string path;
1111
1112    int index = 0;
1113    if (!tc->getMemProxy().tryReadString(path,
1114                process->getSyscallArg(tc, index))) {
1115        return -EFAULT;
1116    }
1117    Addr bufPtr = process->getSyscallArg(tc, index);
1118
1119    // Adjust path for current working directory
1120    path = process->fullPath(path);
1121
1122    struct statfs hostBuf;
1123    int result = statfs(path.c_str(), &hostBuf);
1124
1125    if (result < 0)
1126        return -errno;
1127
1128    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1129
1130    return 0;
1131}
1132
1133
1134/// Target fstatfs() handler.
1135template <class OS>
1136SyscallReturn
1137fstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1138            ThreadContext *tc)
1139{
1140    int index = 0;
1141    int fd = process->sim_fd(process->getSyscallArg(tc, index));
1142    Addr bufPtr = process->getSyscallArg(tc, index);
1143
1144    if (fd < 0)
1145        return -EBADF;
1146
1147    struct statfs hostBuf;
1148    int result = fstatfs(fd, &hostBuf);
1149
1150    if (result < 0)
1151        return -errno;
1152
1153    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
1154
1155    return 0;
1156}
1157
1158
1159/// Target writev() handler.
1160template <class OS>
1161SyscallReturn
1162writevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1163           ThreadContext *tc)
1164{
1165    int index = 0;
1166    int fd = process->getSyscallArg(tc, index);
1167    if (fd < 0 || process->sim_fd(fd) < 0) {
1168        // doesn't map to any simulator fd: not a valid target fd
1169        return -EBADF;
1170    }
1171
1172    SETranslatingPortProxy &p = tc->getMemProxy();
1173    uint64_t tiov_base = process->getSyscallArg(tc, index);
1174    size_t count = process->getSyscallArg(tc, index);
1175    struct iovec hiov[count];
1176    for (size_t i = 0; i < count; ++i) {
1177        typename OS::tgt_iovec tiov;
1178
1179        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
1180                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
1181        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
1182        hiov[i].iov_base = new char [hiov[i].iov_len];
1183        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
1184                   hiov[i].iov_len);
1185    }
1186
1187    int result = writev(process->sim_fd(fd), hiov, count);
1188
1189    for (size_t i = 0; i < count; ++i)
1190        delete [] (char *)hiov[i].iov_base;
1191
1192    if (result < 0)
1193        return -errno;
1194
1195    return 0;
1196}
1197
1198
1199/// Target mmap() handler.
1200///
1201/// We don't really handle mmap().  If the target is mmaping an
1202/// anonymous region or /dev/zero, we can get away with doing basically
1203/// nothing (since memory is initialized to zero and the simulator
1204/// doesn't really check addresses anyway).
1205///
1206template <class OS>
1207SyscallReturn
1208mmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1209{
1210    int index = 0;
1211    Addr start = p->getSyscallArg(tc, index);
1212    uint64_t length = p->getSyscallArg(tc, index);
1213    index++; // int prot = p->getSyscallArg(tc, index);
1214    int flags = p->getSyscallArg(tc, index);
1215    int tgt_fd = p->getSyscallArg(tc, index);
1216    // int offset = p->getSyscallArg(tc, index);
1217
1218    if (length > 0x100000000ULL)
1219        warn("mmap length argument %#x is unreasonably large.\n", length);
1220
1221    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
1222        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
1223        if (!fd_map || fd_map->fd < 0) {
1224            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
1225            return -EBADF;
1226        }
1227
1228        if (fd_map->filename != "/dev/zero") {
1229            // This is very likely broken, but leave a warning here
1230            // (rather than panic) in case /dev/zero is known by
1231            // another name on some platform
1232            warn("allowing mmap of file %s; mmap not supported on files"
1233                 " other than /dev/zero\n", fd_map->filename);
1234        }
1235    }
1236
1237    if ((start  % TheISA::PageBytes) != 0 ||
1238        (length % TheISA::PageBytes) != 0) {
1239        warn("mmap failing: arguments not page-aligned: "
1240             "start 0x%x length 0x%x",
1241             start, length);
1242        return -EINVAL;
1243    }
1244
1245    // are we ok with clobbering existing mappings?  only set this to
1246    // true if the user has been warned.
1247    bool clobber = false;
1248
1249    // try to use the caller-provided address if there is one
1250    bool use_provided_address = (start != 0);
1251
1252    if (use_provided_address) {
1253        // check to see if the desired address is already in use
1254        if (!p->pTable->isUnmapped(start, length)) {
1255            // there are existing mappings in the desired range
1256            // whether we clobber them or not depends on whether the caller
1257            // specified MAP_FIXED
1258            if (flags & OS::TGT_MAP_FIXED) {
1259                // MAP_FIXED specified: map attempt fails
1260                return -EINVAL;
1261            } else {
1262                // MAP_FIXED not specified: ignore suggested start address
1263                warn("mmap: ignoring suggested map address 0x%x\n", start);
1264                use_provided_address = false;
1265            }
1266        }
1267    }
1268
1269    if (!use_provided_address) {
1270        // no address provided, or provided address unusable:
1271        // pick next address from our "mmap region"
1272        if (OS::mmapGrowsDown()) {
1273            start = p->mmap_end - length;
1274            p->mmap_end = start;
1275        } else {
1276            start = p->mmap_end;
1277            p->mmap_end += length;
1278        }
1279    }
1280
1281    p->allocateMem(start, length, clobber);
1282
1283    return start;
1284}
1285
1286/// Target getrlimit() handler.
1287template <class OS>
1288SyscallReturn
1289getrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1290        ThreadContext *tc)
1291{
1292    int index = 0;
1293    unsigned resource = process->getSyscallArg(tc, index);
1294    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1295
1296    switch (resource) {
1297        case OS::TGT_RLIMIT_STACK:
1298            // max stack size in bytes: make up a number (8MB for now)
1299            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1300            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1301            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1302            break;
1303
1304        case OS::TGT_RLIMIT_DATA:
1305            // max data segment size in bytes: make up a number
1306            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
1307            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1308            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
1309            break;
1310
1311        default:
1312            std::cerr << "getrlimitFunc: unimplemented resource " << resource
1313                << std::endl;
1314            abort();
1315            break;
1316    }
1317
1318    rlp.copyOut(tc->getMemProxy());
1319    return 0;
1320}
1321
1322/// Target gettimeofday() handler.
1323template <class OS>
1324SyscallReturn
1325gettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1326        ThreadContext *tc)
1327{
1328    int index = 0;
1329    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1330
1331    getElapsedTime(tp->tv_sec, tp->tv_usec);
1332    tp->tv_sec += seconds_since_epoch;
1333    tp->tv_sec = TheISA::htog(tp->tv_sec);
1334    tp->tv_usec = TheISA::htog(tp->tv_usec);
1335
1336    tp.copyOut(tc->getMemProxy());
1337
1338    return 0;
1339}
1340
1341
1342/// Target utimes() handler.
1343template <class OS>
1344SyscallReturn
1345utimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1346           ThreadContext *tc)
1347{
1348    std::string path;
1349
1350    int index = 0;
1351    if (!tc->getMemProxy().tryReadString(path,
1352                process->getSyscallArg(tc, index))) {
1353        return -EFAULT;
1354    }
1355
1356    TypedBufferArg<typename OS::timeval [2]>
1357        tp(process->getSyscallArg(tc, index));
1358    tp.copyIn(tc->getMemProxy());
1359
1360    struct timeval hostTimeval[2];
1361    for (int i = 0; i < 2; ++i)
1362    {
1363        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
1364        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
1365    }
1366
1367    // Adjust path for current working directory
1368    path = process->fullPath(path);
1369
1370    int result = utimes(path.c_str(), hostTimeval);
1371
1372    if (result < 0)
1373        return -errno;
1374
1375    return 0;
1376}
1377/// Target getrusage() function.
1378template <class OS>
1379SyscallReturn
1380getrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1381              ThreadContext *tc)
1382{
1383    int index = 0;
1384    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
1385    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1386
1387    rup->ru_utime.tv_sec = 0;
1388    rup->ru_utime.tv_usec = 0;
1389    rup->ru_stime.tv_sec = 0;
1390    rup->ru_stime.tv_usec = 0;
1391    rup->ru_maxrss = 0;
1392    rup->ru_ixrss = 0;
1393    rup->ru_idrss = 0;
1394    rup->ru_isrss = 0;
1395    rup->ru_minflt = 0;
1396    rup->ru_majflt = 0;
1397    rup->ru_nswap = 0;
1398    rup->ru_inblock = 0;
1399    rup->ru_oublock = 0;
1400    rup->ru_msgsnd = 0;
1401    rup->ru_msgrcv = 0;
1402    rup->ru_nsignals = 0;
1403    rup->ru_nvcsw = 0;
1404    rup->ru_nivcsw = 0;
1405
1406    switch (who) {
1407      case OS::TGT_RUSAGE_SELF:
1408        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1409        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1410        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
1411        break;
1412
1413      case OS::TGT_RUSAGE_CHILDREN:
1414        // do nothing.  We have no child processes, so they take no time.
1415        break;
1416
1417      default:
1418        // don't really handle THREAD or CHILDREN, but just warn and
1419        // plow ahead
1420        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
1421             who);
1422    }
1423
1424    rup.copyOut(tc->getMemProxy());
1425
1426    return 0;
1427}
1428
1429/// Target times() function.
1430template <class OS>
1431SyscallReturn
1432timesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1433           ThreadContext *tc)
1434{
1435    int index = 0;
1436    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
1437
1438    // Fill in the time structure (in clocks)
1439    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
1440    bufp->tms_utime = clocks;
1441    bufp->tms_stime = 0;
1442    bufp->tms_cutime = 0;
1443    bufp->tms_cstime = 0;
1444
1445    // Convert to host endianness
1446    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
1447
1448    // Write back
1449    bufp.copyOut(tc->getMemProxy());
1450
1451    // Return clock ticks since system boot
1452    return clocks;
1453}
1454
1455/// Target time() function.
1456template <class OS>
1457SyscallReturn
1458timeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1459           ThreadContext *tc)
1460{
1461    typename OS::time_t sec, usec;
1462    getElapsedTime(sec, usec);
1463    sec += seconds_since_epoch;
1464
1465    int index = 0;
1466    Addr taddr = (Addr)process->getSyscallArg(tc, index);
1467    if(taddr != 0) {
1468        typename OS::time_t t = sec;
1469        t = TheISA::htog(t);
1470        SETranslatingPortProxy &p = tc->getMemProxy();
1471        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
1472    }
1473    return sec;
1474}
1475
1476
1477#endif // __SIM_SYSCALL_EMUL_HH__
1478