syscall_emul.hh revision 9112
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"
658706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
667678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
678229Snate@binkert.org#include "sim/process.hh"
688766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
696640Svince@csl.cornell.edu#include "sim/system.hh"
70360SN/A
71360SN/A///
72360SN/A/// System call descriptor.
73360SN/A///
74360SN/Aclass SyscallDesc {
75360SN/A
76360SN/A  public:
77360SN/A
78378SN/A    /// Typedef for target syscall handler functions.
791450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
803114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
81360SN/A
825543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
835543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
845543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
85360SN/A
86360SN/A    /// Flag values for controlling syscall behavior.
87360SN/A    enum Flags {
88360SN/A        /// Don't set return regs according to funcPtr return value.
89360SN/A        /// Used for syscalls with non-standard return conventions
902680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
91360SN/A        /// sigreturn).
92360SN/A        SuppressReturnValue = 1
93360SN/A    };
94360SN/A
95360SN/A    /// Constructor.
96360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
97360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
98360SN/A    {
99360SN/A    }
100360SN/A
101360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1023114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
103360SN/A};
104360SN/A
105360SN/A
106360SN/Aclass BaseBufferArg {
107360SN/A
108360SN/A  public:
109360SN/A
110360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
111360SN/A    {
112360SN/A        bufPtr = new uint8_t[size];
113360SN/A        // clear out buffer: in case we only partially populate this,
114360SN/A        // and then do a copyOut(), we want to make sure we don't
115360SN/A        // introduce any random junk into the simulated address space
116360SN/A        memset(bufPtr, 0, size);
117360SN/A    }
118360SN/A
119360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
120360SN/A
121360SN/A    //
122360SN/A    // copy data into simulator space (read from target memory)
123360SN/A    //
1248852Sandreas.hansson@arm.com    virtual bool copyIn(SETranslatingPortProxy &memproxy)
125360SN/A    {
1268852Sandreas.hansson@arm.com        memproxy.readBlob(addr, bufPtr, size);
1275543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
128360SN/A    }
129360SN/A
130360SN/A    //
131360SN/A    // copy data out of simulator space (write to target memory)
132360SN/A    //
1338852Sandreas.hansson@arm.com    virtual bool copyOut(SETranslatingPortProxy &memproxy)
134360SN/A    {
1358852Sandreas.hansson@arm.com        memproxy.writeBlob(addr, bufPtr, size);
1365543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
137360SN/A    }
138360SN/A
139360SN/A  protected:
140360SN/A    Addr addr;
141360SN/A    int size;
142360SN/A    uint8_t *bufPtr;
143360SN/A};
144360SN/A
145360SN/A
146360SN/Aclass BufferArg : public BaseBufferArg
147360SN/A{
148360SN/A  public:
149360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1505543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
151360SN/A};
152360SN/A
153360SN/Atemplate <class T>
154360SN/Aclass TypedBufferArg : public BaseBufferArg
155360SN/A{
156360SN/A  public:
157360SN/A    // user can optionally specify a specific number of bytes to
158360SN/A    // allocate to deal with those structs that have variable-size
159360SN/A    // arrays at the end
160360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
161360SN/A        : BaseBufferArg(_addr, _size)
162360SN/A    { }
163360SN/A
164360SN/A    // type case
165360SN/A    operator T*() { return (T *)bufPtr; }
166360SN/A
167360SN/A    // dereference operators
1685543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1695543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
170502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
171360SN/A};
172360SN/A
173360SN/A//////////////////////////////////////////////////////////////////////
174360SN/A//
175360SN/A// The following emulation functions are generic enough that they
176360SN/A// don't need to be recompiled for different emulated OS's.  They are
177360SN/A// defined in sim/syscall_emul.cc.
178360SN/A//
179360SN/A//////////////////////////////////////////////////////////////////////
180360SN/A
181360SN/A
182378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1831706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
185378SN/A
186378SN/A/// Handler for unimplemented syscalls that we never intend to
187378SN/A/// implement (signal handling, etc.) and should not affect the correct
188378SN/A/// behavior of the program.  Print a warning only if the appropriate
189378SN/A/// trace flag is enabled.  Return success to the target program.
1901706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1913114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1928149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
1938149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
194360SN/A
1956109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1961706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1973114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
198378SN/A
1996109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
2006109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2016109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
2026109Ssanchezd@stanford.edu
203378SN/A/// Target getpagesize() handler.
2041706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
2053114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
206378SN/A
2075748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2085748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2095748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
210378SN/A
211378SN/A/// Target close() handler.
2121706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2133114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
214378SN/A
215378SN/A/// Target read() handler.
2161706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2173114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
218378SN/A
219378SN/A/// Target write() handler.
2201706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2213114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
222378SN/A
223378SN/A/// Target lseek() handler.
2241706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2253114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
226378SN/A
2274118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2284118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2294118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2304118Sgblack@eecs.umich.edu
231378SN/A/// Target munmap() handler.
2321706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2333114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
234378SN/A
235378SN/A/// Target gethostname() handler.
2361706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2373114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
238360SN/A
2395513SMichael.Adler@intel.com/// Target getcwd() handler.
2405513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2415513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2425513SMichael.Adler@intel.com
2435513SMichael.Adler@intel.com/// Target unlink() handler.
2445513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2455513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2465513SMichael.Adler@intel.com
247511SN/A/// Target unlink() handler.
2481706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2493114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
250511SN/A
2515513SMichael.Adler@intel.com/// Target mkdir() handler.
2525513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2535513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2545513SMichael.Adler@intel.com
255511SN/A/// Target rename() handler.
2561706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2573114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2581706SN/A
2591706SN/A
2601706SN/A/// Target truncate() handler.
2611706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2623114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2631706SN/A
2641706SN/A
2651706SN/A/// Target ftruncate() handler.
2661706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2673114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2681706SN/A
269511SN/A
2706703Svince@csl.cornell.edu/// Target truncate64() handler.
2716703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2726703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2736703Svince@csl.cornell.edu
2746685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2756685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2766685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2776685Stjones1@inf.ed.ac.uk
2786685Stjones1@inf.ed.ac.uk
2795513SMichael.Adler@intel.com/// Target umask() handler.
2805513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2815513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2825513SMichael.Adler@intel.com
2835513SMichael.Adler@intel.com
2841999SN/A/// Target chown() handler.
2851999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2863114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2871999SN/A
2881999SN/A
2891999SN/A/// Target fchown() handler.
2901999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2913114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2921999SN/A
2933079Sstever@eecs.umich.edu/// Target dup() handler.
2943079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2953114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2963079Sstever@eecs.umich.edu
2972093SN/A/// Target fnctl() handler.
2982093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2993114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3002093SN/A
3012687Sksewell@umich.edu/// Target fcntl64() handler.
3022687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3033114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3042687Sksewell@umich.edu
3052238SN/A/// Target setuid() handler.
3062238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3073114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3082238SN/A
3092238SN/A/// Target getpid() handler.
3102238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3113114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3122238SN/A
3132238SN/A/// Target getuid() handler.
3142238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3153114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3162238SN/A
3172238SN/A/// Target getgid() handler.
3182238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3193114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3202238SN/A
3212238SN/A/// Target getppid() handler.
3222238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3233114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3242238SN/A
3252238SN/A/// Target geteuid() handler.
3262238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3273114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3282238SN/A
3292238SN/A/// Target getegid() handler.
3302238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3313114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3322238SN/A
3336109Ssanchezd@stanford.edu/// Target clone() handler.
3346109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3356109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3362238SN/A
3379112Smarc.orr@gmail.com/// Futex system call
3389112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3399112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3409112Smarc.orr@gmail.comtemplate <class OS>
3419112Smarc.orr@gmail.comSyscallReturn
3429112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3439112Smarc.orr@gmail.com          ThreadContext *tc)
3449112Smarc.orr@gmail.com{
3459112Smarc.orr@gmail.com    int index_uaddr = 0;
3469112Smarc.orr@gmail.com    int index_op = 1;
3479112Smarc.orr@gmail.com    int index_val = 2;
3489112Smarc.orr@gmail.com    int index_timeout = 3;
3499112Smarc.orr@gmail.com
3509112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3519112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3529112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3539112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3549112Smarc.orr@gmail.com
3559112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3569112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3579112Smarc.orr@gmail.com
3589112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3599112Smarc.orr@gmail.com            uaddr, op, val);
3609112Smarc.orr@gmail.com
3619112Smarc.orr@gmail.com
3629112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3639112Smarc.orr@gmail.com        if (timeout != 0) {
3649112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3659112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3669112Smarc.orr@gmail.com        }
3679112Smarc.orr@gmail.com
3689112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3699112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3709112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3719112Smarc.orr@gmail.com        delete buf;
3729112Smarc.orr@gmail.com
3739112Smarc.orr@gmail.com        if(val != mem_val) {
3749112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3759112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3769112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3779112Smarc.orr@gmail.com        }
3789112Smarc.orr@gmail.com
3799112Smarc.orr@gmail.com        // Queue the thread context
3809112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3819112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3829112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3839112Smarc.orr@gmail.com        } else {
3849112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3859112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3869112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3879112Smarc.orr@gmail.com        }
3889112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3899112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3909112Smarc.orr@gmail.com                                "thread context\n");
3919112Smarc.orr@gmail.com        tc->suspend();
3929112Smarc.orr@gmail.com        return 0;
3939112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3949112Smarc.orr@gmail.com        int wokenUp = 0;
3959112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3969112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3979112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3989112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3999112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4009112Smarc.orr@gmail.com                tcWaitList->pop_front();
4019112Smarc.orr@gmail.com                wokenUp++;
4029112Smarc.orr@gmail.com            }
4039112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4049112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4059112Smarc.orr@gmail.com                delete tcWaitList;
4069112Smarc.orr@gmail.com            }
4079112Smarc.orr@gmail.com        }
4089112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4099112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4109112Smarc.orr@gmail.com        return wokenUp;
4119112Smarc.orr@gmail.com    } else {
4129112Smarc.orr@gmail.com        warn("sys_futex: op %d is not implemented, just returning...");
4139112Smarc.orr@gmail.com        return 0;
4149112Smarc.orr@gmail.com    }
4159112Smarc.orr@gmail.com
4169112Smarc.orr@gmail.com}
4179112Smarc.orr@gmail.com
4182238SN/A
4192238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4202238SN/A/// returning a second value in a register other than the normal return register
4212238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4223114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4232238SN/A
4242238SN/A/// Target getpidPseudo() handler.
4252238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4263114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4272238SN/A
4282238SN/A/// Target getuidPseudo() handler.
4292238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4303114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4312238SN/A
4322238SN/A/// Target getgidPseudo() handler.
4332238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4343114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4352238SN/A
4362238SN/A
4371354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4381354SN/Aconst int one_million = 1000000;
4391354SN/A
4401354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4411354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4421354SN/A/// real-world time) to keep simulations repeatable.
4431354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4441354SN/A
4451354SN/A/// Helper function to convert current elapsed time to seconds and
4461354SN/A/// microseconds.
4471354SN/Atemplate <class T1, class T2>
4481354SN/Avoid
4491354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4501354SN/A{
4517823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4521354SN/A    sec = elapsed_usecs / one_million;
4531354SN/A    usec = elapsed_usecs % one_million;
4541354SN/A}
4551354SN/A
456360SN/A//////////////////////////////////////////////////////////////////////
457360SN/A//
458360SN/A// The following emulation functions are generic, but need to be
459360SN/A// templated to account for differences in types, constants, etc.
460360SN/A//
461360SN/A//////////////////////////////////////////////////////////////////////
462360SN/A
4633113Sgblack@eecs.umich.edu#if NO_STAT64
4643113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4653113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4663113Sgblack@eecs.umich.edu#else
4673113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4683113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4693113Sgblack@eecs.umich.edu#endif
4703113Sgblack@eecs.umich.edu
4713113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4723113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4733113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4743113Sgblack@eecs.umich.edu
4753113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4763113Sgblack@eecs.umich.edustatic void
4773113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4783113Sgblack@eecs.umich.edu{
4794189Sgblack@eecs.umich.edu    using namespace TheISA;
4804189Sgblack@eecs.umich.edu
4813113Sgblack@eecs.umich.edu    if (fakeTTY)
4823113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4833113Sgblack@eecs.umich.edu    else
4843113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4858737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4863113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4878737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4883277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4895515SMichael.Adler@intel.com    if (fakeTTY) {
4905515SMichael.Adler@intel.com        // Claim to be a character device
4915515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4925515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4935515SMichael.Adler@intel.com    }
4948737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4953277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4968737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4973277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4988737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4993277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5008737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5013113Sgblack@eecs.umich.edu    if (fakeTTY)
5023113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5033113Sgblack@eecs.umich.edu    else
5043113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5058737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5063113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5078737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5083114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5098737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5103114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5118737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5123114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5138737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5144061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5154061Sgblack@eecs.umich.edu    // consistently across different hosts.
5164061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5178737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5183113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5198737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5203113Sgblack@eecs.umich.edu}
5213113Sgblack@eecs.umich.edu
5223113Sgblack@eecs.umich.edu// Same for stat64
5233113Sgblack@eecs.umich.edu
5243113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5253113Sgblack@eecs.umich.edustatic void
5263113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5273113Sgblack@eecs.umich.edu{
5284189Sgblack@eecs.umich.edu    using namespace TheISA;
5294189Sgblack@eecs.umich.edu
5303113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5313113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5323113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5338737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5343113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5358737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5363113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5378737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5383113Sgblack@eecs.umich.edu#else
5393113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5403113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5413113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5423113Sgblack@eecs.umich.edu#endif
5433113Sgblack@eecs.umich.edu}
5443113Sgblack@eecs.umich.edu
5453113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5463113Sgblack@eecs.umich.edutemplate<class OS>
5473113Sgblack@eecs.umich.edustatic void
5488852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5493113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5503113Sgblack@eecs.umich.edu{
5513113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5523113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5533113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5543113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5553113Sgblack@eecs.umich.edu}
5563113Sgblack@eecs.umich.edu
5573113Sgblack@eecs.umich.edutemplate<class OS>
5583113Sgblack@eecs.umich.edustatic void
5598852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5603113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5613113Sgblack@eecs.umich.edu{
5623113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5633113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5646686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5653113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5663113Sgblack@eecs.umich.edu}
5673113Sgblack@eecs.umich.edu
568378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
569378SN/A/// only to find out if their stdout is a tty, to determine whether to
570378SN/A/// do line or block buffering.
571360SN/Atemplate <class OS>
5721450SN/ASyscallReturn
5733114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5742680Sktlim@umich.edu          ThreadContext *tc)
575360SN/A{
5766701Sgblack@eecs.umich.edu    int index = 0;
5776701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5786701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
579360SN/A
5801969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
581360SN/A
582360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
583360SN/A        // doesn't map to any simulator fd: not a valid target fd
5841458SN/A        return -EBADF;
585360SN/A    }
586360SN/A
587360SN/A    switch (req) {
5884131Sbinkertn@umich.edu      case OS::TIOCISATTY_:
5894131Sbinkertn@umich.edu      case OS::TIOCGETP_:
5904131Sbinkertn@umich.edu      case OS::TIOCSETP_:
5914131Sbinkertn@umich.edu      case OS::TIOCSETN_:
5924131Sbinkertn@umich.edu      case OS::TIOCSETC_:
5934131Sbinkertn@umich.edu      case OS::TIOCGETC_:
5944131Sbinkertn@umich.edu      case OS::TIOCGETS_:
5954131Sbinkertn@umich.edu      case OS::TIOCGETA_:
5966689Stjones1@inf.ed.ac.uk      case OS::TCSETAW_:
5971458SN/A        return -ENOTTY;
598360SN/A
599360SN/A      default:
6007720Sgblack@eecs.umich.edu        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
6017720Sgblack@eecs.umich.edu              fd, req, tc->pcState());
602360SN/A    }
603360SN/A}
604360SN/A
605378SN/A/// Target open() handler.
606360SN/Atemplate <class OS>
6071450SN/ASyscallReturn
6083114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6092680Sktlim@umich.edu         ThreadContext *tc)
610360SN/A{
611360SN/A    std::string path;
612360SN/A
6136701Sgblack@eecs.umich.edu    int index = 0;
6148852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6156701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6161458SN/A        return -EFAULT;
617360SN/A
618360SN/A    if (path == "/dev/sysdev0") {
619360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
620360SN/A        // We don't support it, so just punt.
6211706SN/A        warn("Ignoring open(%s, ...)\n", path);
6221458SN/A        return -ENOENT;
623360SN/A    }
624360SN/A
6256701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6266701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
627360SN/A    int hostFlags = 0;
628360SN/A
629360SN/A    // translate open flags
630360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
631360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
632360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
633360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
634360SN/A        }
635360SN/A    }
636360SN/A
637360SN/A    // any target flags left?
638360SN/A    if (tgtFlags != 0)
6391706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
640360SN/A
641360SN/A#ifdef __CYGWIN32__
642360SN/A    hostFlags |= O_BINARY;
643360SN/A#endif
644360SN/A
6453669Sbinkertn@umich.edu    // Adjust path for current working directory
6463669Sbinkertn@umich.edu    path = process->fullPath(path);
6473669Sbinkertn@umich.edu
6481706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6491706SN/A
6505795Ssaidi@eecs.umich.edu    int fd;
6515795Ssaidi@eecs.umich.edu    if (!path.compare(0, 6, "/proc/") || !path.compare(0, 8, "/system/") ||
6525795Ssaidi@eecs.umich.edu        !path.compare(0, 10, "/platform/") || !path.compare(0, 5, "/sys/")) {
6535795Ssaidi@eecs.umich.edu        // It's a proc/sys entery and requires special handling
6545795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6555795Ssaidi@eecs.umich.edu        return (fd == -1) ? -1 : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
6565795Ssaidi@eecs.umich.edu     } else {
6575795Ssaidi@eecs.umich.edu        // open the file
6585795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6595795Ssaidi@eecs.umich.edu        return (fd == -1) ? -errno : process->alloc_fd(fd,path.c_str(),hostFlags,mode, false);
6605795Ssaidi@eecs.umich.edu     }
661360SN/A
662360SN/A}
663360SN/A
6646640Svince@csl.cornell.edu/// Target sysinfo() handler.
6656640Svince@csl.cornell.edutemplate <class OS>
6666640Svince@csl.cornell.eduSyscallReturn
6676640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6686640Svince@csl.cornell.edu         ThreadContext *tc)
6696640Svince@csl.cornell.edu{
6706640Svince@csl.cornell.edu
6716701Sgblack@eecs.umich.edu    int index = 0;
6726701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
6736701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
6746640Svince@csl.cornell.edu
6756701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
6766701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
6776640Svince@csl.cornell.edu
6788706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
6796640Svince@csl.cornell.edu
6806701Sgblack@eecs.umich.edu    return 0;
6816640Svince@csl.cornell.edu}
682360SN/A
6831999SN/A/// Target chmod() handler.
6841999SN/Atemplate <class OS>
6851999SN/ASyscallReturn
6863114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6872680Sktlim@umich.edu          ThreadContext *tc)
6881999SN/A{
6891999SN/A    std::string path;
6901999SN/A
6916701Sgblack@eecs.umich.edu    int index = 0;
6928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6936701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
6941999SN/A        return -EFAULT;
6956701Sgblack@eecs.umich.edu    }
6961999SN/A
6976701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
6981999SN/A    mode_t hostMode = 0;
6991999SN/A
7001999SN/A    // XXX translate mode flags via OS::something???
7011999SN/A    hostMode = mode;
7021999SN/A
7033669Sbinkertn@umich.edu    // Adjust path for current working directory
7043669Sbinkertn@umich.edu    path = process->fullPath(path);
7053669Sbinkertn@umich.edu
7061999SN/A    // do the chmod
7071999SN/A    int result = chmod(path.c_str(), hostMode);
7081999SN/A    if (result < 0)
7092218SN/A        return -errno;
7101999SN/A
7111999SN/A    return 0;
7121999SN/A}
7131999SN/A
7141999SN/A
7151999SN/A/// Target fchmod() handler.
7161999SN/Atemplate <class OS>
7171999SN/ASyscallReturn
7183114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7192680Sktlim@umich.edu           ThreadContext *tc)
7201999SN/A{
7216701Sgblack@eecs.umich.edu    int index = 0;
7226701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7231999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7241999SN/A        // doesn't map to any simulator fd: not a valid target fd
7251999SN/A        return -EBADF;
7261999SN/A    }
7271999SN/A
7286701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7291999SN/A    mode_t hostMode = 0;
7301999SN/A
7311999SN/A    // XXX translate mode flags via OS::someting???
7321999SN/A    hostMode = mode;
7331999SN/A
7341999SN/A    // do the fchmod
7351999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7361999SN/A    if (result < 0)
7372218SN/A        return -errno;
7381999SN/A
7391999SN/A    return 0;
7401999SN/A}
7411999SN/A
7425877Shsul@eecs.umich.edu/// Target mremap() handler.
7435877Shsul@eecs.umich.edutemplate <class OS>
7445877Shsul@eecs.umich.eduSyscallReturn
7455877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7465877Shsul@eecs.umich.edu{
7476701Sgblack@eecs.umich.edu    int index = 0;
7486701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7496701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7506701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7516701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
7525877Shsul@eecs.umich.edu
7535877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
7545877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
7555877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7565877Shsul@eecs.umich.edu        return -EINVAL;
7575877Shsul@eecs.umich.edu    }
7585877Shsul@eecs.umich.edu
7595877Shsul@eecs.umich.edu    if (new_length > old_length) {
7605877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
7615877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
7628601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
7635877Shsul@eecs.umich.edu            process->mmap_end += diff;
7645877Shsul@eecs.umich.edu            return start;
7655877Shsul@eecs.umich.edu        } else {
7665877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
7675877Shsul@eecs.umich.edu            if (!(flags & 1)) {
7685877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
7695877Shsul@eecs.umich.edu                return -ENOMEM;
7705877Shsul@eecs.umich.edu            } else {
7715877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
7725877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
7735877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
7745877Shsul@eecs.umich.edu                start = process->mmap_end;
7755877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
7768601Ssteve.reinhardt@amd.com                process->allocateMem(start + old_length,
7778601Ssteve.reinhardt@amd.com                                     new_length - old_length);
7785877Shsul@eecs.umich.edu                process->mmap_end += new_length;
7795877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
7805877Shsul@eecs.umich.edu                return start;
7815877Shsul@eecs.umich.edu            }
7825877Shsul@eecs.umich.edu        }
7835877Shsul@eecs.umich.edu    } else {
7848601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
7855877Shsul@eecs.umich.edu        return start;
7865877Shsul@eecs.umich.edu    }
7875877Shsul@eecs.umich.edu}
7881999SN/A
789378SN/A/// Target stat() handler.
790360SN/Atemplate <class OS>
7911450SN/ASyscallReturn
7923114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7932680Sktlim@umich.edu         ThreadContext *tc)
794360SN/A{
795360SN/A    std::string path;
796360SN/A
7976701Sgblack@eecs.umich.edu    int index = 0;
7988852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7996701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8006701Sgblack@eecs.umich.edu        return -EFAULT;
8016701Sgblack@eecs.umich.edu    }
8026701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
803360SN/A
8043669Sbinkertn@umich.edu    // Adjust path for current working directory
8053669Sbinkertn@umich.edu    path = process->fullPath(path);
8063669Sbinkertn@umich.edu
807360SN/A    struct stat hostBuf;
808360SN/A    int result = stat(path.c_str(), &hostBuf);
809360SN/A
810360SN/A    if (result < 0)
8112218SN/A        return -errno;
812360SN/A
8138706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
814360SN/A
8151458SN/A    return 0;
816360SN/A}
817360SN/A
818360SN/A
8195074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8205074Ssaidi@eecs.umich.edutemplate <class OS>
8215074Ssaidi@eecs.umich.eduSyscallReturn
8225074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8235074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8245074Ssaidi@eecs.umich.edu{
8255074Ssaidi@eecs.umich.edu    std::string path;
8265074Ssaidi@eecs.umich.edu
8276701Sgblack@eecs.umich.edu    int index = 0;
8288852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8296701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8305074Ssaidi@eecs.umich.edu        return -EFAULT;
8316701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8325074Ssaidi@eecs.umich.edu
8335074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8345074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8355074Ssaidi@eecs.umich.edu
8365208Ssaidi@eecs.umich.edu#if NO_STAT64
8375208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8385208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8395208Ssaidi@eecs.umich.edu#else
8405074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8415074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8425208Ssaidi@eecs.umich.edu#endif
8435074Ssaidi@eecs.umich.edu
8445074Ssaidi@eecs.umich.edu    if (result < 0)
8455074Ssaidi@eecs.umich.edu        return -errno;
8465074Ssaidi@eecs.umich.edu
8478706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
8485074Ssaidi@eecs.umich.edu
8495074Ssaidi@eecs.umich.edu    return 0;
8505074Ssaidi@eecs.umich.edu}
8515074Ssaidi@eecs.umich.edu
8525074Ssaidi@eecs.umich.edu
8531999SN/A/// Target fstat64() handler.
8541999SN/Atemplate <class OS>
8551999SN/ASyscallReturn
8563114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8572680Sktlim@umich.edu            ThreadContext *tc)
8581999SN/A{
8596701Sgblack@eecs.umich.edu    int index = 0;
8606701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
8616701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8621999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8631999SN/A        // doesn't map to any simulator fd: not a valid target fd
8641999SN/A        return -EBADF;
8651999SN/A    }
8661999SN/A
8672764Sstever@eecs.umich.edu#if NO_STAT64
8682064SN/A    struct stat  hostBuf;
8692064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
8702064SN/A#else
8712064SN/A    struct stat64  hostBuf;
8721999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
8732064SN/A#endif
8741999SN/A
8751999SN/A    if (result < 0)
8762218SN/A        return -errno;
8771999SN/A
8788706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
8791999SN/A
8801999SN/A    return 0;
8811999SN/A}
8821999SN/A
8831999SN/A
884378SN/A/// Target lstat() handler.
885360SN/Atemplate <class OS>
8861450SN/ASyscallReturn
8873114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8882680Sktlim@umich.edu          ThreadContext *tc)
889360SN/A{
890360SN/A    std::string path;
891360SN/A
8926701Sgblack@eecs.umich.edu    int index = 0;
8938852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8946701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8956701Sgblack@eecs.umich.edu        return -EFAULT;
8966701Sgblack@eecs.umich.edu    }
8976701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
898360SN/A
8993669Sbinkertn@umich.edu    // Adjust path for current working directory
9003669Sbinkertn@umich.edu    path = process->fullPath(path);
9013669Sbinkertn@umich.edu
902360SN/A    struct stat hostBuf;
903360SN/A    int result = lstat(path.c_str(), &hostBuf);
904360SN/A
905360SN/A    if (result < 0)
9061458SN/A        return -errno;
907360SN/A
9088706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
909360SN/A
9101458SN/A    return 0;
911360SN/A}
912360SN/A
9131999SN/A/// Target lstat64() handler.
9141999SN/Atemplate <class OS>
9151999SN/ASyscallReturn
9163114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9172680Sktlim@umich.edu            ThreadContext *tc)
9181999SN/A{
9191999SN/A    std::string path;
9201999SN/A
9216701Sgblack@eecs.umich.edu    int index = 0;
9228852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9236701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9246701Sgblack@eecs.umich.edu        return -EFAULT;
9256701Sgblack@eecs.umich.edu    }
9266701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9271999SN/A
9283669Sbinkertn@umich.edu    // Adjust path for current working directory
9293669Sbinkertn@umich.edu    path = process->fullPath(path);
9303669Sbinkertn@umich.edu
9312764Sstever@eecs.umich.edu#if NO_STAT64
9322064SN/A    struct stat hostBuf;
9332064SN/A    int result = lstat(path.c_str(), &hostBuf);
9342064SN/A#else
9351999SN/A    struct stat64 hostBuf;
9361999SN/A    int result = lstat64(path.c_str(), &hostBuf);
9372064SN/A#endif
9381999SN/A
9391999SN/A    if (result < 0)
9401999SN/A        return -errno;
9411999SN/A
9428706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9431999SN/A
9441999SN/A    return 0;
9451999SN/A}
9461999SN/A
947378SN/A/// Target fstat() handler.
948360SN/Atemplate <class OS>
9491450SN/ASyscallReturn
9503114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9512680Sktlim@umich.edu          ThreadContext *tc)
952360SN/A{
9536701Sgblack@eecs.umich.edu    int index = 0;
9546701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
9556701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
956360SN/A
9571969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
958360SN/A
959360SN/A    if (fd < 0)
9601458SN/A        return -EBADF;
961360SN/A
962360SN/A    struct stat hostBuf;
963360SN/A    int result = fstat(fd, &hostBuf);
964360SN/A
965360SN/A    if (result < 0)
9661458SN/A        return -errno;
967360SN/A
9688706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9692021SN/A
9701458SN/A    return 0;
971360SN/A}
972360SN/A
973360SN/A
9741706SN/A/// Target statfs() handler.
9751706SN/Atemplate <class OS>
9761706SN/ASyscallReturn
9773114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9782680Sktlim@umich.edu           ThreadContext *tc)
9791706SN/A{
9801706SN/A    std::string path;
9811706SN/A
9826701Sgblack@eecs.umich.edu    int index = 0;
9838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9846701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9856701Sgblack@eecs.umich.edu        return -EFAULT;
9866701Sgblack@eecs.umich.edu    }
9876701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9881706SN/A
9893669Sbinkertn@umich.edu    // Adjust path for current working directory
9903669Sbinkertn@umich.edu    path = process->fullPath(path);
9913669Sbinkertn@umich.edu
9921706SN/A    struct statfs hostBuf;
9931706SN/A    int result = statfs(path.c_str(), &hostBuf);
9941706SN/A
9951706SN/A    if (result < 0)
9962218SN/A        return -errno;
9971706SN/A
9988706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
9991706SN/A
10001706SN/A    return 0;
10011706SN/A}
10021706SN/A
10031706SN/A
10041706SN/A/// Target fstatfs() handler.
10051706SN/Atemplate <class OS>
10061706SN/ASyscallReturn
10073114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10082680Sktlim@umich.edu            ThreadContext *tc)
10091706SN/A{
10106701Sgblack@eecs.umich.edu    int index = 0;
10116701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10126701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10131706SN/A
10141706SN/A    if (fd < 0)
10151706SN/A        return -EBADF;
10161706SN/A
10171706SN/A    struct statfs hostBuf;
10181706SN/A    int result = fstatfs(fd, &hostBuf);
10191706SN/A
10201706SN/A    if (result < 0)
10212218SN/A        return -errno;
10221706SN/A
10238706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10241706SN/A
10251706SN/A    return 0;
10261706SN/A}
10271706SN/A
10281706SN/A
10291999SN/A/// Target writev() handler.
10301999SN/Atemplate <class OS>
10311999SN/ASyscallReturn
10323114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10332680Sktlim@umich.edu           ThreadContext *tc)
10341999SN/A{
10356701Sgblack@eecs.umich.edu    int index = 0;
10366701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
10371999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
10381999SN/A        // doesn't map to any simulator fd: not a valid target fd
10391999SN/A        return -EBADF;
10401999SN/A    }
10411999SN/A
10428852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
10436701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
10446701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
10451999SN/A    struct iovec hiov[count];
10466227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
10471999SN/A        typename OS::tgt_iovec tiov;
10482461SN/A
10498852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
10508852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
10518737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
10521999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
10538852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
10548852Sandreas.hansson@arm.com                   hiov[i].iov_len);
10551999SN/A    }
10561999SN/A
10571999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
10581999SN/A
10596227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
10601999SN/A        delete [] (char *)hiov[i].iov_base;
10611999SN/A
10621999SN/A    if (result < 0)
10632218SN/A        return -errno;
10641999SN/A
10651999SN/A    return 0;
10661999SN/A}
10671999SN/A
10681999SN/A
1069378SN/A/// Target mmap() handler.
1070378SN/A///
1071378SN/A/// We don't really handle mmap().  If the target is mmaping an
1072378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1073378SN/A/// nothing (since memory is initialized to zero and the simulator
10748324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
10758324Ssteve.reinhardt@amd.com///
1076360SN/Atemplate <class OS>
10771450SN/ASyscallReturn
10783114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1079360SN/A{
10806701Sgblack@eecs.umich.edu    int index = 0;
10816701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
10826701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
10836701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
10846701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
10858324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
10866701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1087360SN/A
10889008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
10899008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
10909008Sgblack@eecs.umich.edu
10918324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
10928324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
10938324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
10948324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
10958324Ssteve.reinhardt@amd.com            return -EBADF;
10968324Ssteve.reinhardt@amd.com        }
10978324Ssteve.reinhardt@amd.com
10988324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
10998324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
11008324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
11018324Ssteve.reinhardt@amd.com            // another name on some platform
11028324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11038324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11048324Ssteve.reinhardt@amd.com        }
11058324Ssteve.reinhardt@amd.com    }
11065877Shsul@eecs.umich.edu
11072544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
11082544SN/A        (length % TheISA::VMPageSize) != 0) {
11092544SN/A        warn("mmap failing: arguments not page-aligned: "
11102544SN/A             "start 0x%x length 0x%x",
11112544SN/A             start, length);
11122544SN/A        return -EINVAL;
1113360SN/A    }
1114360SN/A
11158600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
11168600Ssteve.reinhardt@amd.com    // true if the user has been warned.
11178600Ssteve.reinhardt@amd.com    bool clobber = false;
11188600Ssteve.reinhardt@amd.com
11198600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
11208600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
11218600Ssteve.reinhardt@amd.com
11228600Ssteve.reinhardt@amd.com    if (use_provided_address) {
11238600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
11248600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
11258600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
11268600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
11278600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
11288600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
11298600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
11308600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
11318600Ssteve.reinhardt@amd.com                     start);
11328600Ssteve.reinhardt@amd.com                clobber = true;
11338600Ssteve.reinhardt@amd.com            } else {
11348600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
11358600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
11368600Ssteve.reinhardt@amd.com                use_provided_address = false;
11378600Ssteve.reinhardt@amd.com            }
11388600Ssteve.reinhardt@amd.com        }
11392544SN/A    }
11402544SN/A
11418600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
11428600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
11438600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
11448600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
11458600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
11468600Ssteve.reinhardt@amd.com            p->mmap_end = start;
11478600Ssteve.reinhardt@amd.com        } else {
11488600Ssteve.reinhardt@amd.com            start = p->mmap_end;
11498600Ssteve.reinhardt@amd.com            p->mmap_end += length;
11508600Ssteve.reinhardt@amd.com        }
11516672Sgblack@eecs.umich.edu    }
11528600Ssteve.reinhardt@amd.com
11538601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
11542544SN/A
11551458SN/A    return start;
1156360SN/A}
1157360SN/A
1158378SN/A/// Target getrlimit() handler.
1159360SN/Atemplate <class OS>
11601450SN/ASyscallReturn
11613114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11622680Sktlim@umich.edu        ThreadContext *tc)
1163360SN/A{
11646701Sgblack@eecs.umich.edu    int index = 0;
11656701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
11666701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1167360SN/A
1168360SN/A    switch (resource) {
11692064SN/A        case OS::TGT_RLIMIT_STACK:
11705877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
11712064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
11728737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11738737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11742064SN/A            break;
1175360SN/A
11765877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
11775877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
11785877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
11798737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11808737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11815877Shsul@eecs.umich.edu            break;
11825877Shsul@eecs.umich.edu
11832064SN/A        default:
11842064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
11852064SN/A                << std::endl;
11862064SN/A            abort();
11872064SN/A            break;
1188360SN/A    }
1189360SN/A
11908706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
11911458SN/A    return 0;
1192360SN/A}
1193360SN/A
1194378SN/A/// Target gettimeofday() handler.
1195360SN/Atemplate <class OS>
11961450SN/ASyscallReturn
11973114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11982680Sktlim@umich.edu        ThreadContext *tc)
1199360SN/A{
12006701Sgblack@eecs.umich.edu    int index = 0;
12016701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1202360SN/A
1203360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1204360SN/A    tp->tv_sec += seconds_since_epoch;
12056109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12066109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1207360SN/A
12088706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1209360SN/A
12101458SN/A    return 0;
1211360SN/A}
1212360SN/A
1213360SN/A
12141999SN/A/// Target utimes() handler.
12151999SN/Atemplate <class OS>
12161999SN/ASyscallReturn
12173114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12182680Sktlim@umich.edu           ThreadContext *tc)
12191999SN/A{
12201999SN/A    std::string path;
12211999SN/A
12226701Sgblack@eecs.umich.edu    int index = 0;
12238852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12246701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12256701Sgblack@eecs.umich.edu        return -EFAULT;
12266701Sgblack@eecs.umich.edu    }
12271999SN/A
12286701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
12296701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
12308706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
12311999SN/A
12321999SN/A    struct timeval hostTimeval[2];
12331999SN/A    for (int i = 0; i < 2; ++i)
12341999SN/A    {
12358737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
12368737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
12371999SN/A    }
12383669Sbinkertn@umich.edu
12393669Sbinkertn@umich.edu    // Adjust path for current working directory
12403669Sbinkertn@umich.edu    path = process->fullPath(path);
12413669Sbinkertn@umich.edu
12421999SN/A    int result = utimes(path.c_str(), hostTimeval);
12431999SN/A
12441999SN/A    if (result < 0)
12451999SN/A        return -errno;
12461999SN/A
12471999SN/A    return 0;
12481999SN/A}
1249378SN/A/// Target getrusage() function.
1250360SN/Atemplate <class OS>
12511450SN/ASyscallReturn
12523114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12532680Sktlim@umich.edu              ThreadContext *tc)
1254360SN/A{
12556701Sgblack@eecs.umich.edu    int index = 0;
12566701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
12576701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1258360SN/A
12593670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
12603670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1261360SN/A    rup->ru_stime.tv_sec = 0;
1262360SN/A    rup->ru_stime.tv_usec = 0;
1263360SN/A    rup->ru_maxrss = 0;
1264360SN/A    rup->ru_ixrss = 0;
1265360SN/A    rup->ru_idrss = 0;
1266360SN/A    rup->ru_isrss = 0;
1267360SN/A    rup->ru_minflt = 0;
1268360SN/A    rup->ru_majflt = 0;
1269360SN/A    rup->ru_nswap = 0;
1270360SN/A    rup->ru_inblock = 0;
1271360SN/A    rup->ru_oublock = 0;
1272360SN/A    rup->ru_msgsnd = 0;
1273360SN/A    rup->ru_msgrcv = 0;
1274360SN/A    rup->ru_nsignals = 0;
1275360SN/A    rup->ru_nvcsw = 0;
1276360SN/A    rup->ru_nivcsw = 0;
1277360SN/A
12783670Sbinkertn@umich.edu    switch (who) {
12793670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
12803670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
12818737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
12828737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
12833670Sbinkertn@umich.edu        break;
12843670Sbinkertn@umich.edu
12853670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
12863670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
12873670Sbinkertn@umich.edu        break;
12883670Sbinkertn@umich.edu
12893670Sbinkertn@umich.edu      default:
12903670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
12913670Sbinkertn@umich.edu        // plow ahead
12923670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
12933670Sbinkertn@umich.edu             who);
12943670Sbinkertn@umich.edu    }
12953670Sbinkertn@umich.edu
12968706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1297360SN/A
12981458SN/A    return 0;
1299360SN/A}
1300360SN/A
13016683Stjones1@inf.ed.ac.uk/// Target times() function.
13026683Stjones1@inf.ed.ac.uktemplate <class OS>
13036683Stjones1@inf.ed.ac.ukSyscallReturn
13046683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13056683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13066683Stjones1@inf.ed.ac.uk{
13076701Sgblack@eecs.umich.edu    int index = 0;
13086701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
13096683Stjones1@inf.ed.ac.uk
13106683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
13117823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
13126683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
13136683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
13146683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
13156683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
13166683Stjones1@inf.ed.ac.uk
13176683Stjones1@inf.ed.ac.uk    // Convert to host endianness
13188737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
13196683Stjones1@inf.ed.ac.uk
13206683Stjones1@inf.ed.ac.uk    // Write back
13218706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
13226683Stjones1@inf.ed.ac.uk
13236683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
13246683Stjones1@inf.ed.ac.uk    return clocks;
13256683Stjones1@inf.ed.ac.uk}
13262553SN/A
13276684Stjones1@inf.ed.ac.uk/// Target time() function.
13286684Stjones1@inf.ed.ac.uktemplate <class OS>
13296684Stjones1@inf.ed.ac.ukSyscallReturn
13306684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13316684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13326684Stjones1@inf.ed.ac.uk{
13336684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
13346684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
13356684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
13366684Stjones1@inf.ed.ac.uk
13376701Sgblack@eecs.umich.edu    int index = 0;
13386701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
13396684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
13406684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
13418737Skoansin.tan@gmail.com        t = TheISA::htog(t);
13428852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
13438852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
13446684Stjones1@inf.ed.ac.uk    }
13456684Stjones1@inf.ed.ac.uk    return sec;
13466684Stjones1@inf.ed.ac.uk}
13472553SN/A
13482553SN/A
13491354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1350