syscall_emul.hh revision 9202
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__) || \
369202Spalle@lyckegaard.dk  defined(__FreeBSD__) || defined(__CYGWIN__) || \
379202Spalle@lyckegaard.dk  defined(__NetBSD__))
382064SN/A
39360SN/A///
40360SN/A/// @file syscall_emul.hh
41360SN/A///
42360SN/A/// This file defines objects used to emulate syscalls from the target
43360SN/A/// application on the host machine.
44360SN/A
451809SN/A#ifdef __CYGWIN32__
465543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
471809SN/A#endif
483113Sgblack@eecs.umich.edu#include <sys/stat.h>
498229Snate@binkert.org#include <sys/time.h>
508229Snate@binkert.org#include <sys/uio.h>
513113Sgblack@eecs.umich.edu#include <fcntl.h>
527075Snate@binkert.org
538229Snate@binkert.org#include <cerrno>
547075Snate@binkert.org#include <string>
55360SN/A
562474SN/A#include "base/chunk_generator.hh"
575543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
582462SN/A#include "base/misc.hh"
591354SN/A#include "base/trace.hh"
606216Snate@binkert.org#include "base/types.hh"
616658Snate@binkert.org#include "config/the_isa.hh"
622474SN/A#include "cpu/base.hh"
632680Sktlim@umich.edu#include "cpu/thread_context.hh"
648232Snate@binkert.org#include "debug/SyscallVerbose.hh"
658229Snate@binkert.org#include "mem/page_table.hh"
668706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
677678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
688229Snate@binkert.org#include "sim/process.hh"
698766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
706640Svince@csl.cornell.edu#include "sim/system.hh"
71360SN/A
72360SN/A///
73360SN/A/// System call descriptor.
74360SN/A///
75360SN/Aclass SyscallDesc {
76360SN/A
77360SN/A  public:
78360SN/A
79378SN/A    /// Typedef for target syscall handler functions.
801450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
813114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
82360SN/A
835543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
845543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
855543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
86360SN/A
87360SN/A    /// Flag values for controlling syscall behavior.
88360SN/A    enum Flags {
89360SN/A        /// Don't set return regs according to funcPtr return value.
90360SN/A        /// Used for syscalls with non-standard return conventions
912680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
92360SN/A        /// sigreturn).
93360SN/A        SuppressReturnValue = 1
94360SN/A    };
95360SN/A
96360SN/A    /// Constructor.
97360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
98360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
99360SN/A    {
100360SN/A    }
101360SN/A
102360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1033114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
104360SN/A};
105360SN/A
106360SN/A
107360SN/Aclass BaseBufferArg {
108360SN/A
109360SN/A  public:
110360SN/A
111360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
112360SN/A    {
113360SN/A        bufPtr = new uint8_t[size];
114360SN/A        // clear out buffer: in case we only partially populate this,
115360SN/A        // and then do a copyOut(), we want to make sure we don't
116360SN/A        // introduce any random junk into the simulated address space
117360SN/A        memset(bufPtr, 0, size);
118360SN/A    }
119360SN/A
120360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
121360SN/A
122360SN/A    //
123360SN/A    // copy data into simulator space (read from target memory)
124360SN/A    //
1258852Sandreas.hansson@arm.com    virtual bool copyIn(SETranslatingPortProxy &memproxy)
126360SN/A    {
1278852Sandreas.hansson@arm.com        memproxy.readBlob(addr, bufPtr, size);
1285543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
129360SN/A    }
130360SN/A
131360SN/A    //
132360SN/A    // copy data out of simulator space (write to target memory)
133360SN/A    //
1348852Sandreas.hansson@arm.com    virtual bool copyOut(SETranslatingPortProxy &memproxy)
135360SN/A    {
1368852Sandreas.hansson@arm.com        memproxy.writeBlob(addr, bufPtr, size);
1375543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
138360SN/A    }
139360SN/A
140360SN/A  protected:
141360SN/A    Addr addr;
142360SN/A    int size;
143360SN/A    uint8_t *bufPtr;
144360SN/A};
145360SN/A
146360SN/A
147360SN/Aclass BufferArg : public BaseBufferArg
148360SN/A{
149360SN/A  public:
150360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1515543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
152360SN/A};
153360SN/A
154360SN/Atemplate <class T>
155360SN/Aclass TypedBufferArg : public BaseBufferArg
156360SN/A{
157360SN/A  public:
158360SN/A    // user can optionally specify a specific number of bytes to
159360SN/A    // allocate to deal with those structs that have variable-size
160360SN/A    // arrays at the end
161360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
162360SN/A        : BaseBufferArg(_addr, _size)
163360SN/A    { }
164360SN/A
165360SN/A    // type case
166360SN/A    operator T*() { return (T *)bufPtr; }
167360SN/A
168360SN/A    // dereference operators
1695543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1705543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
171502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
172360SN/A};
173360SN/A
174360SN/A//////////////////////////////////////////////////////////////////////
175360SN/A//
176360SN/A// The following emulation functions are generic enough that they
177360SN/A// don't need to be recompiled for different emulated OS's.  They are
178360SN/A// defined in sim/syscall_emul.cc.
179360SN/A//
180360SN/A//////////////////////////////////////////////////////////////////////
181360SN/A
182360SN/A
183378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1841706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1853114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
186378SN/A
187378SN/A/// Handler for unimplemented syscalls that we never intend to
188378SN/A/// implement (signal handling, etc.) and should not affect the correct
189378SN/A/// behavior of the program.  Print a warning only if the appropriate
190378SN/A/// trace flag is enabled.  Return success to the target program.
1911706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1923114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1938149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
1948149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
195360SN/A
1966109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1971706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1983114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
199378SN/A
2006109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
2016109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2026109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
2036109Ssanchezd@stanford.edu
204378SN/A/// Target getpagesize() handler.
2051706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
2063114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
207378SN/A
2085748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2095748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2105748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
211378SN/A
212378SN/A/// Target close() handler.
2131706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2143114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
215378SN/A
216378SN/A/// Target read() handler.
2171706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2183114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
219378SN/A
220378SN/A/// Target write() handler.
2211706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2223114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
223378SN/A
224378SN/A/// Target lseek() handler.
2251706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2263114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
227378SN/A
2284118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2294118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2304118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2314118Sgblack@eecs.umich.edu
232378SN/A/// Target munmap() handler.
2331706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2343114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
235378SN/A
236378SN/A/// Target gethostname() handler.
2371706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2383114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
239360SN/A
2405513SMichael.Adler@intel.com/// Target getcwd() handler.
2415513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2425513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2435513SMichael.Adler@intel.com
2445513SMichael.Adler@intel.com/// Target unlink() handler.
2455513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2465513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2475513SMichael.Adler@intel.com
248511SN/A/// Target unlink() handler.
2491706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2503114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
251511SN/A
2525513SMichael.Adler@intel.com/// Target mkdir() handler.
2535513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2545513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2555513SMichael.Adler@intel.com
256511SN/A/// Target rename() handler.
2571706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2583114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2591706SN/A
2601706SN/A
2611706SN/A/// Target truncate() handler.
2621706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2633114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2641706SN/A
2651706SN/A
2661706SN/A/// Target ftruncate() handler.
2671706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2691706SN/A
270511SN/A
2716703Svince@csl.cornell.edu/// Target truncate64() handler.
2726703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2736703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2746703Svince@csl.cornell.edu
2756685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2766685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2776685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2786685Stjones1@inf.ed.ac.uk
2796685Stjones1@inf.ed.ac.uk
2805513SMichael.Adler@intel.com/// Target umask() handler.
2815513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2825513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2835513SMichael.Adler@intel.com
2845513SMichael.Adler@intel.com
2851999SN/A/// Target chown() handler.
2861999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2873114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2881999SN/A
2891999SN/A
2901999SN/A/// Target fchown() handler.
2911999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2923114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2931999SN/A
2943079Sstever@eecs.umich.edu/// Target dup() handler.
2953079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2963114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2973079Sstever@eecs.umich.edu
2982093SN/A/// Target fnctl() handler.
2992093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
3003114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3012093SN/A
3022687Sksewell@umich.edu/// Target fcntl64() handler.
3032687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3043114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3052687Sksewell@umich.edu
3062238SN/A/// Target setuid() handler.
3072238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3083114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3092238SN/A
3102238SN/A/// Target getpid() handler.
3112238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3123114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3132238SN/A
3142238SN/A/// Target getuid() handler.
3152238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3163114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3172238SN/A
3182238SN/A/// Target getgid() handler.
3192238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3203114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3212238SN/A
3222238SN/A/// Target getppid() handler.
3232238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3243114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3252238SN/A
3262238SN/A/// Target geteuid() handler.
3272238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3283114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3292238SN/A
3302238SN/A/// Target getegid() handler.
3312238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3323114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3332238SN/A
3346109Ssanchezd@stanford.edu/// Target clone() handler.
3356109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3366109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3372238SN/A
3389112Smarc.orr@gmail.com/// Futex system call
3399112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3409112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3419112Smarc.orr@gmail.comtemplate <class OS>
3429112Smarc.orr@gmail.comSyscallReturn
3439112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3449112Smarc.orr@gmail.com          ThreadContext *tc)
3459112Smarc.orr@gmail.com{
3469112Smarc.orr@gmail.com    int index_uaddr = 0;
3479112Smarc.orr@gmail.com    int index_op = 1;
3489112Smarc.orr@gmail.com    int index_val = 2;
3499112Smarc.orr@gmail.com    int index_timeout = 3;
3509112Smarc.orr@gmail.com
3519112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3529112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3539112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3549112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3559112Smarc.orr@gmail.com
3569112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3579112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3589112Smarc.orr@gmail.com
3599112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3609112Smarc.orr@gmail.com            uaddr, op, val);
3619112Smarc.orr@gmail.com
3629112Smarc.orr@gmail.com
3639112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3649112Smarc.orr@gmail.com        if (timeout != 0) {
3659112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3669112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3679112Smarc.orr@gmail.com        }
3689112Smarc.orr@gmail.com
3699112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3709112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3719112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3729112Smarc.orr@gmail.com        delete buf;
3739112Smarc.orr@gmail.com
3749112Smarc.orr@gmail.com        if(val != mem_val) {
3759112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3769112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3779112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3789112Smarc.orr@gmail.com        }
3799112Smarc.orr@gmail.com
3809112Smarc.orr@gmail.com        // Queue the thread context
3819112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3829112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3839112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3849112Smarc.orr@gmail.com        } else {
3859112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3869112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3879112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3889112Smarc.orr@gmail.com        }
3899112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3909112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3919112Smarc.orr@gmail.com                                "thread context\n");
3929112Smarc.orr@gmail.com        tc->suspend();
3939112Smarc.orr@gmail.com        return 0;
3949112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3959112Smarc.orr@gmail.com        int wokenUp = 0;
3969112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3979112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3989112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3999112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
4009112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4019112Smarc.orr@gmail.com                tcWaitList->pop_front();
4029112Smarc.orr@gmail.com                wokenUp++;
4039112Smarc.orr@gmail.com            }
4049112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4059112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4069112Smarc.orr@gmail.com                delete tcWaitList;
4079112Smarc.orr@gmail.com            }
4089112Smarc.orr@gmail.com        }
4099112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4109112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4119112Smarc.orr@gmail.com        return wokenUp;
4129112Smarc.orr@gmail.com    } else {
4139112Smarc.orr@gmail.com        warn("sys_futex: op %d is not implemented, just returning...");
4149112Smarc.orr@gmail.com        return 0;
4159112Smarc.orr@gmail.com    }
4169112Smarc.orr@gmail.com
4179112Smarc.orr@gmail.com}
4189112Smarc.orr@gmail.com
4192238SN/A
4202238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4212238SN/A/// returning a second value in a register other than the normal return register
4222238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4233114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4242238SN/A
4252238SN/A/// Target getpidPseudo() handler.
4262238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4273114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4282238SN/A
4292238SN/A/// Target getuidPseudo() handler.
4302238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4313114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4322238SN/A
4332238SN/A/// Target getgidPseudo() handler.
4342238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4353114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4362238SN/A
4372238SN/A
4381354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4391354SN/Aconst int one_million = 1000000;
4401354SN/A
4411354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4421354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4431354SN/A/// real-world time) to keep simulations repeatable.
4441354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4451354SN/A
4461354SN/A/// Helper function to convert current elapsed time to seconds and
4471354SN/A/// microseconds.
4481354SN/Atemplate <class T1, class T2>
4491354SN/Avoid
4501354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4511354SN/A{
4527823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4531354SN/A    sec = elapsed_usecs / one_million;
4541354SN/A    usec = elapsed_usecs % one_million;
4551354SN/A}
4561354SN/A
457360SN/A//////////////////////////////////////////////////////////////////////
458360SN/A//
459360SN/A// The following emulation functions are generic, but need to be
460360SN/A// templated to account for differences in types, constants, etc.
461360SN/A//
462360SN/A//////////////////////////////////////////////////////////////////////
463360SN/A
4643113Sgblack@eecs.umich.edu#if NO_STAT64
4653113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4663113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4673113Sgblack@eecs.umich.edu#else
4683113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4693113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4703113Sgblack@eecs.umich.edu#endif
4713113Sgblack@eecs.umich.edu
4723113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4733113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4743113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4753113Sgblack@eecs.umich.edu
4763113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4773113Sgblack@eecs.umich.edustatic void
4783113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4793113Sgblack@eecs.umich.edu{
4804189Sgblack@eecs.umich.edu    using namespace TheISA;
4814189Sgblack@eecs.umich.edu
4823113Sgblack@eecs.umich.edu    if (fakeTTY)
4833113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4843113Sgblack@eecs.umich.edu    else
4853113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4868737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4873113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4888737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4893277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4905515SMichael.Adler@intel.com    if (fakeTTY) {
4915515SMichael.Adler@intel.com        // Claim to be a character device
4925515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4935515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4945515SMichael.Adler@intel.com    }
4958737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4963277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4978737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4983277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4998737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5003277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5018737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5023113Sgblack@eecs.umich.edu    if (fakeTTY)
5033113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5043113Sgblack@eecs.umich.edu    else
5053113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5068737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5073113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5088737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5093114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5108737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5113114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5128737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5133114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5148737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5154061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5164061Sgblack@eecs.umich.edu    // consistently across different hosts.
5174061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5188737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5193113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5208737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5213113Sgblack@eecs.umich.edu}
5223113Sgblack@eecs.umich.edu
5233113Sgblack@eecs.umich.edu// Same for stat64
5243113Sgblack@eecs.umich.edu
5253113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5263113Sgblack@eecs.umich.edustatic void
5273113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5283113Sgblack@eecs.umich.edu{
5294189Sgblack@eecs.umich.edu    using namespace TheISA;
5304189Sgblack@eecs.umich.edu
5313113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5323113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5333113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5348737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5353113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5368737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5373113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5388737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5393113Sgblack@eecs.umich.edu#else
5403113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5413113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5423113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5433113Sgblack@eecs.umich.edu#endif
5443113Sgblack@eecs.umich.edu}
5453113Sgblack@eecs.umich.edu
5463113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5473113Sgblack@eecs.umich.edutemplate<class OS>
5483113Sgblack@eecs.umich.edustatic void
5498852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5503113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5513113Sgblack@eecs.umich.edu{
5523113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5533113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5543113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5553113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5563113Sgblack@eecs.umich.edu}
5573113Sgblack@eecs.umich.edu
5583113Sgblack@eecs.umich.edutemplate<class OS>
5593113Sgblack@eecs.umich.edustatic void
5608852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5613113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5623113Sgblack@eecs.umich.edu{
5633113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5643113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5656686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5663113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5673113Sgblack@eecs.umich.edu}
5683113Sgblack@eecs.umich.edu
569378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
570378SN/A/// only to find out if their stdout is a tty, to determine whether to
5719141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5729141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
573360SN/Atemplate <class OS>
5741450SN/ASyscallReturn
5753114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5762680Sktlim@umich.edu          ThreadContext *tc)
577360SN/A{
5786701Sgblack@eecs.umich.edu    int index = 0;
5796701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5806701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
581360SN/A
5821969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
583360SN/A
584360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
585360SN/A        // doesn't map to any simulator fd: not a valid target fd
5861458SN/A        return -EBADF;
587360SN/A    }
588360SN/A
5899141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5901458SN/A        return -ENOTTY;
5919141Smarc.orr@gmail.com    }
592360SN/A
5939141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5949141Smarc.orr@gmail.com         fd, req, tc->pcState());
5959141Smarc.orr@gmail.com    return -ENOTTY;
596360SN/A}
597360SN/A
598378SN/A/// Target open() handler.
599360SN/Atemplate <class OS>
6001450SN/ASyscallReturn
6013114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6022680Sktlim@umich.edu         ThreadContext *tc)
603360SN/A{
604360SN/A    std::string path;
605360SN/A
6066701Sgblack@eecs.umich.edu    int index = 0;
6078852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6086701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6091458SN/A        return -EFAULT;
610360SN/A
611360SN/A    if (path == "/dev/sysdev0") {
612360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
613360SN/A        // We don't support it, so just punt.
6141706SN/A        warn("Ignoring open(%s, ...)\n", path);
6151458SN/A        return -ENOENT;
616360SN/A    }
617360SN/A
6186701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6196701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
620360SN/A    int hostFlags = 0;
621360SN/A
622360SN/A    // translate open flags
623360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
624360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
625360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
626360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
627360SN/A        }
628360SN/A    }
629360SN/A
630360SN/A    // any target flags left?
631360SN/A    if (tgtFlags != 0)
6321706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
633360SN/A
634360SN/A#ifdef __CYGWIN32__
635360SN/A    hostFlags |= O_BINARY;
636360SN/A#endif
637360SN/A
6383669Sbinkertn@umich.edu    // Adjust path for current working directory
6393669Sbinkertn@umich.edu    path = process->fullPath(path);
6403669Sbinkertn@umich.edu
6411706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6421706SN/A
6435795Ssaidi@eecs.umich.edu    int fd;
6449143Ssteve.reinhardt@amd.com    int local_errno;
6459142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6469142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6479143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6485795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6499143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6505795Ssaidi@eecs.umich.edu     } else {
6515795Ssaidi@eecs.umich.edu        // open the file
6525795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6539143Ssteve.reinhardt@amd.com        local_errno = errno;
6545795Ssaidi@eecs.umich.edu     }
655360SN/A
6569143Ssteve.reinhardt@amd.com    if (fd == -1)
6579143Ssteve.reinhardt@amd.com        return -local_errno;
6589143Ssteve.reinhardt@amd.com
6599143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
660360SN/A}
661360SN/A
6626640Svince@csl.cornell.edu/// Target sysinfo() handler.
6636640Svince@csl.cornell.edutemplate <class OS>
6646640Svince@csl.cornell.eduSyscallReturn
6656640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6666640Svince@csl.cornell.edu         ThreadContext *tc)
6676640Svince@csl.cornell.edu{
6686640Svince@csl.cornell.edu
6696701Sgblack@eecs.umich.edu    int index = 0;
6706701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
6716701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
6726640Svince@csl.cornell.edu
6736701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
6746701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
6756640Svince@csl.cornell.edu
6768706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
6776640Svince@csl.cornell.edu
6786701Sgblack@eecs.umich.edu    return 0;
6796640Svince@csl.cornell.edu}
680360SN/A
6811999SN/A/// Target chmod() handler.
6821999SN/Atemplate <class OS>
6831999SN/ASyscallReturn
6843114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6852680Sktlim@umich.edu          ThreadContext *tc)
6861999SN/A{
6871999SN/A    std::string path;
6881999SN/A
6896701Sgblack@eecs.umich.edu    int index = 0;
6908852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6916701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
6921999SN/A        return -EFAULT;
6936701Sgblack@eecs.umich.edu    }
6941999SN/A
6956701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
6961999SN/A    mode_t hostMode = 0;
6971999SN/A
6981999SN/A    // XXX translate mode flags via OS::something???
6991999SN/A    hostMode = mode;
7001999SN/A
7013669Sbinkertn@umich.edu    // Adjust path for current working directory
7023669Sbinkertn@umich.edu    path = process->fullPath(path);
7033669Sbinkertn@umich.edu
7041999SN/A    // do the chmod
7051999SN/A    int result = chmod(path.c_str(), hostMode);
7061999SN/A    if (result < 0)
7072218SN/A        return -errno;
7081999SN/A
7091999SN/A    return 0;
7101999SN/A}
7111999SN/A
7121999SN/A
7131999SN/A/// Target fchmod() handler.
7141999SN/Atemplate <class OS>
7151999SN/ASyscallReturn
7163114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7172680Sktlim@umich.edu           ThreadContext *tc)
7181999SN/A{
7196701Sgblack@eecs.umich.edu    int index = 0;
7206701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7211999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7221999SN/A        // doesn't map to any simulator fd: not a valid target fd
7231999SN/A        return -EBADF;
7241999SN/A    }
7251999SN/A
7266701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7271999SN/A    mode_t hostMode = 0;
7281999SN/A
7291999SN/A    // XXX translate mode flags via OS::someting???
7301999SN/A    hostMode = mode;
7311999SN/A
7321999SN/A    // do the fchmod
7331999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7341999SN/A    if (result < 0)
7352218SN/A        return -errno;
7361999SN/A
7371999SN/A    return 0;
7381999SN/A}
7391999SN/A
7405877Shsul@eecs.umich.edu/// Target mremap() handler.
7415877Shsul@eecs.umich.edutemplate <class OS>
7425877Shsul@eecs.umich.eduSyscallReturn
7435877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7445877Shsul@eecs.umich.edu{
7456701Sgblack@eecs.umich.edu    int index = 0;
7466701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7476701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7486701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7496701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
7505877Shsul@eecs.umich.edu
7515877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
7525877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
7535877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7545877Shsul@eecs.umich.edu        return -EINVAL;
7555877Shsul@eecs.umich.edu    }
7565877Shsul@eecs.umich.edu
7575877Shsul@eecs.umich.edu    if (new_length > old_length) {
7585877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
7595877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
7608601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
7615877Shsul@eecs.umich.edu            process->mmap_end += diff;
7625877Shsul@eecs.umich.edu            return start;
7635877Shsul@eecs.umich.edu        } else {
7645877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
7655877Shsul@eecs.umich.edu            if (!(flags & 1)) {
7665877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
7675877Shsul@eecs.umich.edu                return -ENOMEM;
7685877Shsul@eecs.umich.edu            } else {
7695877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
7705877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
7715877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
7725877Shsul@eecs.umich.edu                start = process->mmap_end;
7735877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
7748601Ssteve.reinhardt@amd.com                process->allocateMem(start + old_length,
7758601Ssteve.reinhardt@amd.com                                     new_length - old_length);
7765877Shsul@eecs.umich.edu                process->mmap_end += new_length;
7775877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
7785877Shsul@eecs.umich.edu                return start;
7795877Shsul@eecs.umich.edu            }
7805877Shsul@eecs.umich.edu        }
7815877Shsul@eecs.umich.edu    } else {
7828601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
7835877Shsul@eecs.umich.edu        return start;
7845877Shsul@eecs.umich.edu    }
7855877Shsul@eecs.umich.edu}
7861999SN/A
787378SN/A/// Target stat() handler.
788360SN/Atemplate <class OS>
7891450SN/ASyscallReturn
7903114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7912680Sktlim@umich.edu         ThreadContext *tc)
792360SN/A{
793360SN/A    std::string path;
794360SN/A
7956701Sgblack@eecs.umich.edu    int index = 0;
7968852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7976701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7986701Sgblack@eecs.umich.edu        return -EFAULT;
7996701Sgblack@eecs.umich.edu    }
8006701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
801360SN/A
8023669Sbinkertn@umich.edu    // Adjust path for current working directory
8033669Sbinkertn@umich.edu    path = process->fullPath(path);
8043669Sbinkertn@umich.edu
805360SN/A    struct stat hostBuf;
806360SN/A    int result = stat(path.c_str(), &hostBuf);
807360SN/A
808360SN/A    if (result < 0)
8092218SN/A        return -errno;
810360SN/A
8118706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
812360SN/A
8131458SN/A    return 0;
814360SN/A}
815360SN/A
816360SN/A
8175074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8185074Ssaidi@eecs.umich.edutemplate <class OS>
8195074Ssaidi@eecs.umich.eduSyscallReturn
8205074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8215074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8225074Ssaidi@eecs.umich.edu{
8235074Ssaidi@eecs.umich.edu    std::string path;
8245074Ssaidi@eecs.umich.edu
8256701Sgblack@eecs.umich.edu    int index = 0;
8268852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8276701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8285074Ssaidi@eecs.umich.edu        return -EFAULT;
8296701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8305074Ssaidi@eecs.umich.edu
8315074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8325074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8335074Ssaidi@eecs.umich.edu
8345208Ssaidi@eecs.umich.edu#if NO_STAT64
8355208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8365208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8375208Ssaidi@eecs.umich.edu#else
8385074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8395074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8405208Ssaidi@eecs.umich.edu#endif
8415074Ssaidi@eecs.umich.edu
8425074Ssaidi@eecs.umich.edu    if (result < 0)
8435074Ssaidi@eecs.umich.edu        return -errno;
8445074Ssaidi@eecs.umich.edu
8458706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
8465074Ssaidi@eecs.umich.edu
8475074Ssaidi@eecs.umich.edu    return 0;
8485074Ssaidi@eecs.umich.edu}
8495074Ssaidi@eecs.umich.edu
8505074Ssaidi@eecs.umich.edu
8511999SN/A/// Target fstat64() handler.
8521999SN/Atemplate <class OS>
8531999SN/ASyscallReturn
8543114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8552680Sktlim@umich.edu            ThreadContext *tc)
8561999SN/A{
8576701Sgblack@eecs.umich.edu    int index = 0;
8586701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
8596701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8601999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8611999SN/A        // doesn't map to any simulator fd: not a valid target fd
8621999SN/A        return -EBADF;
8631999SN/A    }
8641999SN/A
8652764Sstever@eecs.umich.edu#if NO_STAT64
8662064SN/A    struct stat  hostBuf;
8672064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
8682064SN/A#else
8692064SN/A    struct stat64  hostBuf;
8701999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
8712064SN/A#endif
8721999SN/A
8731999SN/A    if (result < 0)
8742218SN/A        return -errno;
8751999SN/A
8768706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
8771999SN/A
8781999SN/A    return 0;
8791999SN/A}
8801999SN/A
8811999SN/A
882378SN/A/// Target lstat() handler.
883360SN/Atemplate <class OS>
8841450SN/ASyscallReturn
8853114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8862680Sktlim@umich.edu          ThreadContext *tc)
887360SN/A{
888360SN/A    std::string path;
889360SN/A
8906701Sgblack@eecs.umich.edu    int index = 0;
8918852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8926701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8936701Sgblack@eecs.umich.edu        return -EFAULT;
8946701Sgblack@eecs.umich.edu    }
8956701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
896360SN/A
8973669Sbinkertn@umich.edu    // Adjust path for current working directory
8983669Sbinkertn@umich.edu    path = process->fullPath(path);
8993669Sbinkertn@umich.edu
900360SN/A    struct stat hostBuf;
901360SN/A    int result = lstat(path.c_str(), &hostBuf);
902360SN/A
903360SN/A    if (result < 0)
9041458SN/A        return -errno;
905360SN/A
9068706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
907360SN/A
9081458SN/A    return 0;
909360SN/A}
910360SN/A
9111999SN/A/// Target lstat64() handler.
9121999SN/Atemplate <class OS>
9131999SN/ASyscallReturn
9143114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9152680Sktlim@umich.edu            ThreadContext *tc)
9161999SN/A{
9171999SN/A    std::string path;
9181999SN/A
9196701Sgblack@eecs.umich.edu    int index = 0;
9208852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9216701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9226701Sgblack@eecs.umich.edu        return -EFAULT;
9236701Sgblack@eecs.umich.edu    }
9246701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9251999SN/A
9263669Sbinkertn@umich.edu    // Adjust path for current working directory
9273669Sbinkertn@umich.edu    path = process->fullPath(path);
9283669Sbinkertn@umich.edu
9292764Sstever@eecs.umich.edu#if NO_STAT64
9302064SN/A    struct stat hostBuf;
9312064SN/A    int result = lstat(path.c_str(), &hostBuf);
9322064SN/A#else
9331999SN/A    struct stat64 hostBuf;
9341999SN/A    int result = lstat64(path.c_str(), &hostBuf);
9352064SN/A#endif
9361999SN/A
9371999SN/A    if (result < 0)
9381999SN/A        return -errno;
9391999SN/A
9408706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9411999SN/A
9421999SN/A    return 0;
9431999SN/A}
9441999SN/A
945378SN/A/// Target fstat() handler.
946360SN/Atemplate <class OS>
9471450SN/ASyscallReturn
9483114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9492680Sktlim@umich.edu          ThreadContext *tc)
950360SN/A{
9516701Sgblack@eecs.umich.edu    int index = 0;
9526701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
9536701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
954360SN/A
9551969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
956360SN/A
957360SN/A    if (fd < 0)
9581458SN/A        return -EBADF;
959360SN/A
960360SN/A    struct stat hostBuf;
961360SN/A    int result = fstat(fd, &hostBuf);
962360SN/A
963360SN/A    if (result < 0)
9641458SN/A        return -errno;
965360SN/A
9668706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9672021SN/A
9681458SN/A    return 0;
969360SN/A}
970360SN/A
971360SN/A
9721706SN/A/// Target statfs() handler.
9731706SN/Atemplate <class OS>
9741706SN/ASyscallReturn
9753114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9762680Sktlim@umich.edu           ThreadContext *tc)
9771706SN/A{
9781706SN/A    std::string path;
9791706SN/A
9806701Sgblack@eecs.umich.edu    int index = 0;
9818852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9826701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9836701Sgblack@eecs.umich.edu        return -EFAULT;
9846701Sgblack@eecs.umich.edu    }
9856701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9861706SN/A
9873669Sbinkertn@umich.edu    // Adjust path for current working directory
9883669Sbinkertn@umich.edu    path = process->fullPath(path);
9893669Sbinkertn@umich.edu
9901706SN/A    struct statfs hostBuf;
9911706SN/A    int result = statfs(path.c_str(), &hostBuf);
9921706SN/A
9931706SN/A    if (result < 0)
9942218SN/A        return -errno;
9951706SN/A
9968706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
9971706SN/A
9981706SN/A    return 0;
9991706SN/A}
10001706SN/A
10011706SN/A
10021706SN/A/// Target fstatfs() handler.
10031706SN/Atemplate <class OS>
10041706SN/ASyscallReturn
10053114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10062680Sktlim@umich.edu            ThreadContext *tc)
10071706SN/A{
10086701Sgblack@eecs.umich.edu    int index = 0;
10096701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10106701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10111706SN/A
10121706SN/A    if (fd < 0)
10131706SN/A        return -EBADF;
10141706SN/A
10151706SN/A    struct statfs hostBuf;
10161706SN/A    int result = fstatfs(fd, &hostBuf);
10171706SN/A
10181706SN/A    if (result < 0)
10192218SN/A        return -errno;
10201706SN/A
10218706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10221706SN/A
10231706SN/A    return 0;
10241706SN/A}
10251706SN/A
10261706SN/A
10271999SN/A/// Target writev() handler.
10281999SN/Atemplate <class OS>
10291999SN/ASyscallReturn
10303114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10312680Sktlim@umich.edu           ThreadContext *tc)
10321999SN/A{
10336701Sgblack@eecs.umich.edu    int index = 0;
10346701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
10351999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
10361999SN/A        // doesn't map to any simulator fd: not a valid target fd
10371999SN/A        return -EBADF;
10381999SN/A    }
10391999SN/A
10408852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
10416701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
10426701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
10431999SN/A    struct iovec hiov[count];
10446227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
10451999SN/A        typename OS::tgt_iovec tiov;
10462461SN/A
10478852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
10488852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
10498737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
10501999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
10518852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
10528852Sandreas.hansson@arm.com                   hiov[i].iov_len);
10531999SN/A    }
10541999SN/A
10551999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
10561999SN/A
10576227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
10581999SN/A        delete [] (char *)hiov[i].iov_base;
10591999SN/A
10601999SN/A    if (result < 0)
10612218SN/A        return -errno;
10621999SN/A
10631999SN/A    return 0;
10641999SN/A}
10651999SN/A
10661999SN/A
1067378SN/A/// Target mmap() handler.
1068378SN/A///
1069378SN/A/// We don't really handle mmap().  If the target is mmaping an
1070378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1071378SN/A/// nothing (since memory is initialized to zero and the simulator
10728324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
10738324Ssteve.reinhardt@amd.com///
1074360SN/Atemplate <class OS>
10751450SN/ASyscallReturn
10763114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1077360SN/A{
10786701Sgblack@eecs.umich.edu    int index = 0;
10796701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
10806701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
10816701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
10826701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
10838324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
10846701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1085360SN/A
10869008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
10879008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
10889008Sgblack@eecs.umich.edu
10898324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
10908324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
10918324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
10928324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
10938324Ssteve.reinhardt@amd.com            return -EBADF;
10948324Ssteve.reinhardt@amd.com        }
10958324Ssteve.reinhardt@amd.com
10968324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
10978324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
10988324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
10998324Ssteve.reinhardt@amd.com            // another name on some platform
11008324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11018324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11028324Ssteve.reinhardt@amd.com        }
11038324Ssteve.reinhardt@amd.com    }
11045877Shsul@eecs.umich.edu
11052544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
11062544SN/A        (length % TheISA::VMPageSize) != 0) {
11072544SN/A        warn("mmap failing: arguments not page-aligned: "
11082544SN/A             "start 0x%x length 0x%x",
11092544SN/A             start, length);
11102544SN/A        return -EINVAL;
1111360SN/A    }
1112360SN/A
11138600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
11148600Ssteve.reinhardt@amd.com    // true if the user has been warned.
11158600Ssteve.reinhardt@amd.com    bool clobber = false;
11168600Ssteve.reinhardt@amd.com
11178600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
11188600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
11198600Ssteve.reinhardt@amd.com
11208600Ssteve.reinhardt@amd.com    if (use_provided_address) {
11218600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
11228600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
11238600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
11248600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
11258600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
11268600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
11278600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
11288600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
11298600Ssteve.reinhardt@amd.com                     start);
11308600Ssteve.reinhardt@amd.com                clobber = true;
11318600Ssteve.reinhardt@amd.com            } else {
11328600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
11338600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
11348600Ssteve.reinhardt@amd.com                use_provided_address = false;
11358600Ssteve.reinhardt@amd.com            }
11368600Ssteve.reinhardt@amd.com        }
11372544SN/A    }
11382544SN/A
11398600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
11408600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
11418600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
11428600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
11438600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
11448600Ssteve.reinhardt@amd.com            p->mmap_end = start;
11458600Ssteve.reinhardt@amd.com        } else {
11468600Ssteve.reinhardt@amd.com            start = p->mmap_end;
11478600Ssteve.reinhardt@amd.com            p->mmap_end += length;
11488600Ssteve.reinhardt@amd.com        }
11496672Sgblack@eecs.umich.edu    }
11508600Ssteve.reinhardt@amd.com
11518601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
11522544SN/A
11531458SN/A    return start;
1154360SN/A}
1155360SN/A
1156378SN/A/// Target getrlimit() handler.
1157360SN/Atemplate <class OS>
11581450SN/ASyscallReturn
11593114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11602680Sktlim@umich.edu        ThreadContext *tc)
1161360SN/A{
11626701Sgblack@eecs.umich.edu    int index = 0;
11636701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
11646701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1165360SN/A
1166360SN/A    switch (resource) {
11672064SN/A        case OS::TGT_RLIMIT_STACK:
11685877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
11692064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
11708737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11718737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11722064SN/A            break;
1173360SN/A
11745877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
11755877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
11765877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
11778737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11788737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11795877Shsul@eecs.umich.edu            break;
11805877Shsul@eecs.umich.edu
11812064SN/A        default:
11822064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
11832064SN/A                << std::endl;
11842064SN/A            abort();
11852064SN/A            break;
1186360SN/A    }
1187360SN/A
11888706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
11891458SN/A    return 0;
1190360SN/A}
1191360SN/A
1192378SN/A/// Target gettimeofday() handler.
1193360SN/Atemplate <class OS>
11941450SN/ASyscallReturn
11953114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11962680Sktlim@umich.edu        ThreadContext *tc)
1197360SN/A{
11986701Sgblack@eecs.umich.edu    int index = 0;
11996701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1200360SN/A
1201360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1202360SN/A    tp->tv_sec += seconds_since_epoch;
12036109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12046109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1205360SN/A
12068706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1207360SN/A
12081458SN/A    return 0;
1209360SN/A}
1210360SN/A
1211360SN/A
12121999SN/A/// Target utimes() handler.
12131999SN/Atemplate <class OS>
12141999SN/ASyscallReturn
12153114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12162680Sktlim@umich.edu           ThreadContext *tc)
12171999SN/A{
12181999SN/A    std::string path;
12191999SN/A
12206701Sgblack@eecs.umich.edu    int index = 0;
12218852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12226701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12236701Sgblack@eecs.umich.edu        return -EFAULT;
12246701Sgblack@eecs.umich.edu    }
12251999SN/A
12266701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
12276701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
12288706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
12291999SN/A
12301999SN/A    struct timeval hostTimeval[2];
12311999SN/A    for (int i = 0; i < 2; ++i)
12321999SN/A    {
12338737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
12348737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
12351999SN/A    }
12363669Sbinkertn@umich.edu
12373669Sbinkertn@umich.edu    // Adjust path for current working directory
12383669Sbinkertn@umich.edu    path = process->fullPath(path);
12393669Sbinkertn@umich.edu
12401999SN/A    int result = utimes(path.c_str(), hostTimeval);
12411999SN/A
12421999SN/A    if (result < 0)
12431999SN/A        return -errno;
12441999SN/A
12451999SN/A    return 0;
12461999SN/A}
1247378SN/A/// Target getrusage() function.
1248360SN/Atemplate <class OS>
12491450SN/ASyscallReturn
12503114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12512680Sktlim@umich.edu              ThreadContext *tc)
1252360SN/A{
12536701Sgblack@eecs.umich.edu    int index = 0;
12546701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
12556701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1256360SN/A
12573670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
12583670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1259360SN/A    rup->ru_stime.tv_sec = 0;
1260360SN/A    rup->ru_stime.tv_usec = 0;
1261360SN/A    rup->ru_maxrss = 0;
1262360SN/A    rup->ru_ixrss = 0;
1263360SN/A    rup->ru_idrss = 0;
1264360SN/A    rup->ru_isrss = 0;
1265360SN/A    rup->ru_minflt = 0;
1266360SN/A    rup->ru_majflt = 0;
1267360SN/A    rup->ru_nswap = 0;
1268360SN/A    rup->ru_inblock = 0;
1269360SN/A    rup->ru_oublock = 0;
1270360SN/A    rup->ru_msgsnd = 0;
1271360SN/A    rup->ru_msgrcv = 0;
1272360SN/A    rup->ru_nsignals = 0;
1273360SN/A    rup->ru_nvcsw = 0;
1274360SN/A    rup->ru_nivcsw = 0;
1275360SN/A
12763670Sbinkertn@umich.edu    switch (who) {
12773670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
12783670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
12798737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
12808737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
12813670Sbinkertn@umich.edu        break;
12823670Sbinkertn@umich.edu
12833670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
12843670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
12853670Sbinkertn@umich.edu        break;
12863670Sbinkertn@umich.edu
12873670Sbinkertn@umich.edu      default:
12883670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
12893670Sbinkertn@umich.edu        // plow ahead
12903670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
12913670Sbinkertn@umich.edu             who);
12923670Sbinkertn@umich.edu    }
12933670Sbinkertn@umich.edu
12948706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1295360SN/A
12961458SN/A    return 0;
1297360SN/A}
1298360SN/A
12996683Stjones1@inf.ed.ac.uk/// Target times() function.
13006683Stjones1@inf.ed.ac.uktemplate <class OS>
13016683Stjones1@inf.ed.ac.ukSyscallReturn
13026683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13036683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13046683Stjones1@inf.ed.ac.uk{
13056701Sgblack@eecs.umich.edu    int index = 0;
13066701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
13076683Stjones1@inf.ed.ac.uk
13086683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
13097823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
13106683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
13116683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
13126683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
13136683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
13146683Stjones1@inf.ed.ac.uk
13156683Stjones1@inf.ed.ac.uk    // Convert to host endianness
13168737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
13176683Stjones1@inf.ed.ac.uk
13186683Stjones1@inf.ed.ac.uk    // Write back
13198706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
13206683Stjones1@inf.ed.ac.uk
13216683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
13226683Stjones1@inf.ed.ac.uk    return clocks;
13236683Stjones1@inf.ed.ac.uk}
13242553SN/A
13256684Stjones1@inf.ed.ac.uk/// Target time() function.
13266684Stjones1@inf.ed.ac.uktemplate <class OS>
13276684Stjones1@inf.ed.ac.ukSyscallReturn
13286684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13296684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13306684Stjones1@inf.ed.ac.uk{
13316684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
13326684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
13336684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
13346684Stjones1@inf.ed.ac.uk
13356701Sgblack@eecs.umich.edu    int index = 0;
13366701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
13376684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
13386684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
13398737Skoansin.tan@gmail.com        t = TheISA::htog(t);
13408852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
13418852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
13426684Stjones1@inf.ed.ac.uk    }
13436684Stjones1@inf.ed.ac.uk    return sec;
13446684Stjones1@inf.ed.ac.uk}
13452553SN/A
13462553SN/A
13471354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1348