syscall_emul.hh revision 9238
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
3629238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3639112Smarc.orr@gmail.com
3649112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3659112Smarc.orr@gmail.com        if (timeout != 0) {
3669112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3679112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3689112Smarc.orr@gmail.com        }
3699112Smarc.orr@gmail.com
3709112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3719112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3729112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3739112Smarc.orr@gmail.com        delete buf;
3749112Smarc.orr@gmail.com
3759112Smarc.orr@gmail.com        if(val != mem_val) {
3769112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3779112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3789112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3799112Smarc.orr@gmail.com        }
3809112Smarc.orr@gmail.com
3819112Smarc.orr@gmail.com        // Queue the thread context
3829112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3839112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3849112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3859112Smarc.orr@gmail.com        } else {
3869112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3879112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3889112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3899112Smarc.orr@gmail.com        }
3909112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3919112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3929112Smarc.orr@gmail.com                                "thread context\n");
3939112Smarc.orr@gmail.com        tc->suspend();
3949112Smarc.orr@gmail.com        return 0;
3959112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3969112Smarc.orr@gmail.com        int wokenUp = 0;
3979112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3989112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3999112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4009112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
4019112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4029112Smarc.orr@gmail.com                tcWaitList->pop_front();
4039112Smarc.orr@gmail.com                wokenUp++;
4049112Smarc.orr@gmail.com            }
4059112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4069112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4079112Smarc.orr@gmail.com                delete tcWaitList;
4089112Smarc.orr@gmail.com            }
4099112Smarc.orr@gmail.com        }
4109112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4119112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4129112Smarc.orr@gmail.com        return wokenUp;
4139112Smarc.orr@gmail.com    } else {
4149238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
4159112Smarc.orr@gmail.com        return 0;
4169112Smarc.orr@gmail.com    }
4179112Smarc.orr@gmail.com
4189112Smarc.orr@gmail.com}
4199112Smarc.orr@gmail.com
4202238SN/A
4212238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4222238SN/A/// returning a second value in a register other than the normal return register
4232238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4243114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4252238SN/A
4262238SN/A/// Target getpidPseudo() handler.
4272238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4283114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4292238SN/A
4302238SN/A/// Target getuidPseudo() handler.
4312238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4323114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4332238SN/A
4342238SN/A/// Target getgidPseudo() handler.
4352238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4363114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4372238SN/A
4382238SN/A
4391354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4401354SN/Aconst int one_million = 1000000;
4411354SN/A
4421354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4431354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4441354SN/A/// real-world time) to keep simulations repeatable.
4451354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4461354SN/A
4471354SN/A/// Helper function to convert current elapsed time to seconds and
4481354SN/A/// microseconds.
4491354SN/Atemplate <class T1, class T2>
4501354SN/Avoid
4511354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4521354SN/A{
4537823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4541354SN/A    sec = elapsed_usecs / one_million;
4551354SN/A    usec = elapsed_usecs % one_million;
4561354SN/A}
4571354SN/A
458360SN/A//////////////////////////////////////////////////////////////////////
459360SN/A//
460360SN/A// The following emulation functions are generic, but need to be
461360SN/A// templated to account for differences in types, constants, etc.
462360SN/A//
463360SN/A//////////////////////////////////////////////////////////////////////
464360SN/A
4653113Sgblack@eecs.umich.edu#if NO_STAT64
4663113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4673113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4683113Sgblack@eecs.umich.edu#else
4693113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4703113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4713113Sgblack@eecs.umich.edu#endif
4723113Sgblack@eecs.umich.edu
4733113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4743113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4753113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4763113Sgblack@eecs.umich.edu
4773113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4783113Sgblack@eecs.umich.edustatic void
4793113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4803113Sgblack@eecs.umich.edu{
4814189Sgblack@eecs.umich.edu    using namespace TheISA;
4824189Sgblack@eecs.umich.edu
4833113Sgblack@eecs.umich.edu    if (fakeTTY)
4843113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4853113Sgblack@eecs.umich.edu    else
4863113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4878737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4883113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4898737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4903277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4915515SMichael.Adler@intel.com    if (fakeTTY) {
4925515SMichael.Adler@intel.com        // Claim to be a character device
4935515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4945515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4955515SMichael.Adler@intel.com    }
4968737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4973277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4988737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4993277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5008737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5013277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5028737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5033113Sgblack@eecs.umich.edu    if (fakeTTY)
5043113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5053113Sgblack@eecs.umich.edu    else
5063113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5078737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5083113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5098737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5103114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5118737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5123114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5138737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5143114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5158737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5164061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5174061Sgblack@eecs.umich.edu    // consistently across different hosts.
5184061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5198737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5203113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5218737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5223113Sgblack@eecs.umich.edu}
5233113Sgblack@eecs.umich.edu
5243113Sgblack@eecs.umich.edu// Same for stat64
5253113Sgblack@eecs.umich.edu
5263113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5273113Sgblack@eecs.umich.edustatic void
5283113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5293113Sgblack@eecs.umich.edu{
5304189Sgblack@eecs.umich.edu    using namespace TheISA;
5314189Sgblack@eecs.umich.edu
5323113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5333113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5343113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5358737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5363113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5378737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5383113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5398737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5403113Sgblack@eecs.umich.edu#else
5413113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5423113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5433113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5443113Sgblack@eecs.umich.edu#endif
5453113Sgblack@eecs.umich.edu}
5463113Sgblack@eecs.umich.edu
5473113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5483113Sgblack@eecs.umich.edutemplate<class OS>
5493113Sgblack@eecs.umich.edustatic void
5508852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5513113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5523113Sgblack@eecs.umich.edu{
5533113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5543113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5553113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5563113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5573113Sgblack@eecs.umich.edu}
5583113Sgblack@eecs.umich.edu
5593113Sgblack@eecs.umich.edutemplate<class OS>
5603113Sgblack@eecs.umich.edustatic void
5618852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5623113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5633113Sgblack@eecs.umich.edu{
5643113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5653113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5666686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5673113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5683113Sgblack@eecs.umich.edu}
5693113Sgblack@eecs.umich.edu
570378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
571378SN/A/// only to find out if their stdout is a tty, to determine whether to
5729141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5739141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
574360SN/Atemplate <class OS>
5751450SN/ASyscallReturn
5763114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5772680Sktlim@umich.edu          ThreadContext *tc)
578360SN/A{
5796701Sgblack@eecs.umich.edu    int index = 0;
5806701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5816701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
582360SN/A
5831969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
584360SN/A
585360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
586360SN/A        // doesn't map to any simulator fd: not a valid target fd
5871458SN/A        return -EBADF;
588360SN/A    }
589360SN/A
5909141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5911458SN/A        return -ENOTTY;
5929141Smarc.orr@gmail.com    }
593360SN/A
5949141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5959141Smarc.orr@gmail.com         fd, req, tc->pcState());
5969141Smarc.orr@gmail.com    return -ENOTTY;
597360SN/A}
598360SN/A
599378SN/A/// Target open() handler.
600360SN/Atemplate <class OS>
6011450SN/ASyscallReturn
6023114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6032680Sktlim@umich.edu         ThreadContext *tc)
604360SN/A{
605360SN/A    std::string path;
606360SN/A
6076701Sgblack@eecs.umich.edu    int index = 0;
6088852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6096701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6101458SN/A        return -EFAULT;
611360SN/A
612360SN/A    if (path == "/dev/sysdev0") {
613360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
614360SN/A        // We don't support it, so just punt.
6151706SN/A        warn("Ignoring open(%s, ...)\n", path);
6161458SN/A        return -ENOENT;
617360SN/A    }
618360SN/A
6196701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6206701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
621360SN/A    int hostFlags = 0;
622360SN/A
623360SN/A    // translate open flags
624360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
625360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
626360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
627360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
628360SN/A        }
629360SN/A    }
630360SN/A
631360SN/A    // any target flags left?
632360SN/A    if (tgtFlags != 0)
6331706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
634360SN/A
635360SN/A#ifdef __CYGWIN32__
636360SN/A    hostFlags |= O_BINARY;
637360SN/A#endif
638360SN/A
6393669Sbinkertn@umich.edu    // Adjust path for current working directory
6403669Sbinkertn@umich.edu    path = process->fullPath(path);
6413669Sbinkertn@umich.edu
6421706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6431706SN/A
6445795Ssaidi@eecs.umich.edu    int fd;
6459143Ssteve.reinhardt@amd.com    int local_errno;
6469142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6479142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6489143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6495795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6509143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6515795Ssaidi@eecs.umich.edu     } else {
6525795Ssaidi@eecs.umich.edu        // open the file
6535795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6549143Ssteve.reinhardt@amd.com        local_errno = errno;
6555795Ssaidi@eecs.umich.edu     }
656360SN/A
6579143Ssteve.reinhardt@amd.com    if (fd == -1)
6589143Ssteve.reinhardt@amd.com        return -local_errno;
6599143Ssteve.reinhardt@amd.com
6609143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
661360SN/A}
662360SN/A
6636640Svince@csl.cornell.edu/// Target sysinfo() handler.
6646640Svince@csl.cornell.edutemplate <class OS>
6656640Svince@csl.cornell.eduSyscallReturn
6666640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6676640Svince@csl.cornell.edu         ThreadContext *tc)
6686640Svince@csl.cornell.edu{
6696640Svince@csl.cornell.edu
6706701Sgblack@eecs.umich.edu    int index = 0;
6716701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
6726701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
6736640Svince@csl.cornell.edu
6746701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
6756701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
6766640Svince@csl.cornell.edu
6778706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
6786640Svince@csl.cornell.edu
6796701Sgblack@eecs.umich.edu    return 0;
6806640Svince@csl.cornell.edu}
681360SN/A
6821999SN/A/// Target chmod() handler.
6831999SN/Atemplate <class OS>
6841999SN/ASyscallReturn
6853114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6862680Sktlim@umich.edu          ThreadContext *tc)
6871999SN/A{
6881999SN/A    std::string path;
6891999SN/A
6906701Sgblack@eecs.umich.edu    int index = 0;
6918852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6926701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
6931999SN/A        return -EFAULT;
6946701Sgblack@eecs.umich.edu    }
6951999SN/A
6966701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
6971999SN/A    mode_t hostMode = 0;
6981999SN/A
6991999SN/A    // XXX translate mode flags via OS::something???
7001999SN/A    hostMode = mode;
7011999SN/A
7023669Sbinkertn@umich.edu    // Adjust path for current working directory
7033669Sbinkertn@umich.edu    path = process->fullPath(path);
7043669Sbinkertn@umich.edu
7051999SN/A    // do the chmod
7061999SN/A    int result = chmod(path.c_str(), hostMode);
7071999SN/A    if (result < 0)
7082218SN/A        return -errno;
7091999SN/A
7101999SN/A    return 0;
7111999SN/A}
7121999SN/A
7131999SN/A
7141999SN/A/// Target fchmod() handler.
7151999SN/Atemplate <class OS>
7161999SN/ASyscallReturn
7173114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7182680Sktlim@umich.edu           ThreadContext *tc)
7191999SN/A{
7206701Sgblack@eecs.umich.edu    int index = 0;
7216701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7221999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7231999SN/A        // doesn't map to any simulator fd: not a valid target fd
7241999SN/A        return -EBADF;
7251999SN/A    }
7261999SN/A
7276701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7281999SN/A    mode_t hostMode = 0;
7291999SN/A
7301999SN/A    // XXX translate mode flags via OS::someting???
7311999SN/A    hostMode = mode;
7321999SN/A
7331999SN/A    // do the fchmod
7341999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7351999SN/A    if (result < 0)
7362218SN/A        return -errno;
7371999SN/A
7381999SN/A    return 0;
7391999SN/A}
7401999SN/A
7415877Shsul@eecs.umich.edu/// Target mremap() handler.
7425877Shsul@eecs.umich.edutemplate <class OS>
7435877Shsul@eecs.umich.eduSyscallReturn
7445877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7455877Shsul@eecs.umich.edu{
7466701Sgblack@eecs.umich.edu    int index = 0;
7476701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7486701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7496701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7506701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
7515877Shsul@eecs.umich.edu
7525877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
7535877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
7545877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7555877Shsul@eecs.umich.edu        return -EINVAL;
7565877Shsul@eecs.umich.edu    }
7575877Shsul@eecs.umich.edu
7585877Shsul@eecs.umich.edu    if (new_length > old_length) {
7595877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
7605877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
7618601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
7625877Shsul@eecs.umich.edu            process->mmap_end += diff;
7635877Shsul@eecs.umich.edu            return start;
7645877Shsul@eecs.umich.edu        } else {
7655877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
7665877Shsul@eecs.umich.edu            if (!(flags & 1)) {
7675877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
7685877Shsul@eecs.umich.edu                return -ENOMEM;
7695877Shsul@eecs.umich.edu            } else {
7705877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
7715877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
7725877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
7735877Shsul@eecs.umich.edu                start = process->mmap_end;
7745877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
7758601Ssteve.reinhardt@amd.com                process->allocateMem(start + old_length,
7768601Ssteve.reinhardt@amd.com                                     new_length - old_length);
7775877Shsul@eecs.umich.edu                process->mmap_end += new_length;
7785877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
7795877Shsul@eecs.umich.edu                return start;
7805877Shsul@eecs.umich.edu            }
7815877Shsul@eecs.umich.edu        }
7825877Shsul@eecs.umich.edu    } else {
7838601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
7845877Shsul@eecs.umich.edu        return start;
7855877Shsul@eecs.umich.edu    }
7865877Shsul@eecs.umich.edu}
7871999SN/A
788378SN/A/// Target stat() handler.
789360SN/Atemplate <class OS>
7901450SN/ASyscallReturn
7913114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7922680Sktlim@umich.edu         ThreadContext *tc)
793360SN/A{
794360SN/A    std::string path;
795360SN/A
7966701Sgblack@eecs.umich.edu    int index = 0;
7978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7986701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7996701Sgblack@eecs.umich.edu        return -EFAULT;
8006701Sgblack@eecs.umich.edu    }
8016701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
802360SN/A
8033669Sbinkertn@umich.edu    // Adjust path for current working directory
8043669Sbinkertn@umich.edu    path = process->fullPath(path);
8053669Sbinkertn@umich.edu
806360SN/A    struct stat hostBuf;
807360SN/A    int result = stat(path.c_str(), &hostBuf);
808360SN/A
809360SN/A    if (result < 0)
8102218SN/A        return -errno;
811360SN/A
8128706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
813360SN/A
8141458SN/A    return 0;
815360SN/A}
816360SN/A
817360SN/A
8185074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8195074Ssaidi@eecs.umich.edutemplate <class OS>
8205074Ssaidi@eecs.umich.eduSyscallReturn
8215074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8225074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8235074Ssaidi@eecs.umich.edu{
8245074Ssaidi@eecs.umich.edu    std::string path;
8255074Ssaidi@eecs.umich.edu
8266701Sgblack@eecs.umich.edu    int index = 0;
8278852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8286701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8295074Ssaidi@eecs.umich.edu        return -EFAULT;
8306701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8315074Ssaidi@eecs.umich.edu
8325074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8335074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8345074Ssaidi@eecs.umich.edu
8355208Ssaidi@eecs.umich.edu#if NO_STAT64
8365208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8375208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8385208Ssaidi@eecs.umich.edu#else
8395074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8405074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8415208Ssaidi@eecs.umich.edu#endif
8425074Ssaidi@eecs.umich.edu
8435074Ssaidi@eecs.umich.edu    if (result < 0)
8445074Ssaidi@eecs.umich.edu        return -errno;
8455074Ssaidi@eecs.umich.edu
8468706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
8475074Ssaidi@eecs.umich.edu
8485074Ssaidi@eecs.umich.edu    return 0;
8495074Ssaidi@eecs.umich.edu}
8505074Ssaidi@eecs.umich.edu
8515074Ssaidi@eecs.umich.edu
8521999SN/A/// Target fstat64() handler.
8531999SN/Atemplate <class OS>
8541999SN/ASyscallReturn
8553114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8562680Sktlim@umich.edu            ThreadContext *tc)
8571999SN/A{
8586701Sgblack@eecs.umich.edu    int index = 0;
8596701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
8606701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8611999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8621999SN/A        // doesn't map to any simulator fd: not a valid target fd
8631999SN/A        return -EBADF;
8641999SN/A    }
8651999SN/A
8662764Sstever@eecs.umich.edu#if NO_STAT64
8672064SN/A    struct stat  hostBuf;
8682064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
8692064SN/A#else
8702064SN/A    struct stat64  hostBuf;
8711999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
8722064SN/A#endif
8731999SN/A
8741999SN/A    if (result < 0)
8752218SN/A        return -errno;
8761999SN/A
8778706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
8781999SN/A
8791999SN/A    return 0;
8801999SN/A}
8811999SN/A
8821999SN/A
883378SN/A/// Target lstat() handler.
884360SN/Atemplate <class OS>
8851450SN/ASyscallReturn
8863114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8872680Sktlim@umich.edu          ThreadContext *tc)
888360SN/A{
889360SN/A    std::string path;
890360SN/A
8916701Sgblack@eecs.umich.edu    int index = 0;
8928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8936701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8946701Sgblack@eecs.umich.edu        return -EFAULT;
8956701Sgblack@eecs.umich.edu    }
8966701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
897360SN/A
8983669Sbinkertn@umich.edu    // Adjust path for current working directory
8993669Sbinkertn@umich.edu    path = process->fullPath(path);
9003669Sbinkertn@umich.edu
901360SN/A    struct stat hostBuf;
902360SN/A    int result = lstat(path.c_str(), &hostBuf);
903360SN/A
904360SN/A    if (result < 0)
9051458SN/A        return -errno;
906360SN/A
9078706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
908360SN/A
9091458SN/A    return 0;
910360SN/A}
911360SN/A
9121999SN/A/// Target lstat64() handler.
9131999SN/Atemplate <class OS>
9141999SN/ASyscallReturn
9153114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9162680Sktlim@umich.edu            ThreadContext *tc)
9171999SN/A{
9181999SN/A    std::string path;
9191999SN/A
9206701Sgblack@eecs.umich.edu    int index = 0;
9218852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9226701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9236701Sgblack@eecs.umich.edu        return -EFAULT;
9246701Sgblack@eecs.umich.edu    }
9256701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9261999SN/A
9273669Sbinkertn@umich.edu    // Adjust path for current working directory
9283669Sbinkertn@umich.edu    path = process->fullPath(path);
9293669Sbinkertn@umich.edu
9302764Sstever@eecs.umich.edu#if NO_STAT64
9312064SN/A    struct stat hostBuf;
9322064SN/A    int result = lstat(path.c_str(), &hostBuf);
9332064SN/A#else
9341999SN/A    struct stat64 hostBuf;
9351999SN/A    int result = lstat64(path.c_str(), &hostBuf);
9362064SN/A#endif
9371999SN/A
9381999SN/A    if (result < 0)
9391999SN/A        return -errno;
9401999SN/A
9418706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9421999SN/A
9431999SN/A    return 0;
9441999SN/A}
9451999SN/A
946378SN/A/// Target fstat() handler.
947360SN/Atemplate <class OS>
9481450SN/ASyscallReturn
9493114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9502680Sktlim@umich.edu          ThreadContext *tc)
951360SN/A{
9526701Sgblack@eecs.umich.edu    int index = 0;
9536701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
9546701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
955360SN/A
9561969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
957360SN/A
958360SN/A    if (fd < 0)
9591458SN/A        return -EBADF;
960360SN/A
961360SN/A    struct stat hostBuf;
962360SN/A    int result = fstat(fd, &hostBuf);
963360SN/A
964360SN/A    if (result < 0)
9651458SN/A        return -errno;
966360SN/A
9678706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9682021SN/A
9691458SN/A    return 0;
970360SN/A}
971360SN/A
972360SN/A
9731706SN/A/// Target statfs() handler.
9741706SN/Atemplate <class OS>
9751706SN/ASyscallReturn
9763114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9772680Sktlim@umich.edu           ThreadContext *tc)
9781706SN/A{
9791706SN/A    std::string path;
9801706SN/A
9816701Sgblack@eecs.umich.edu    int index = 0;
9828852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9836701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9846701Sgblack@eecs.umich.edu        return -EFAULT;
9856701Sgblack@eecs.umich.edu    }
9866701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9871706SN/A
9883669Sbinkertn@umich.edu    // Adjust path for current working directory
9893669Sbinkertn@umich.edu    path = process->fullPath(path);
9903669Sbinkertn@umich.edu
9911706SN/A    struct statfs hostBuf;
9921706SN/A    int result = statfs(path.c_str(), &hostBuf);
9931706SN/A
9941706SN/A    if (result < 0)
9952218SN/A        return -errno;
9961706SN/A
9978706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
9981706SN/A
9991706SN/A    return 0;
10001706SN/A}
10011706SN/A
10021706SN/A
10031706SN/A/// Target fstatfs() handler.
10041706SN/Atemplate <class OS>
10051706SN/ASyscallReturn
10063114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10072680Sktlim@umich.edu            ThreadContext *tc)
10081706SN/A{
10096701Sgblack@eecs.umich.edu    int index = 0;
10106701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10116701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10121706SN/A
10131706SN/A    if (fd < 0)
10141706SN/A        return -EBADF;
10151706SN/A
10161706SN/A    struct statfs hostBuf;
10171706SN/A    int result = fstatfs(fd, &hostBuf);
10181706SN/A
10191706SN/A    if (result < 0)
10202218SN/A        return -errno;
10211706SN/A
10228706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10231706SN/A
10241706SN/A    return 0;
10251706SN/A}
10261706SN/A
10271706SN/A
10281999SN/A/// Target writev() handler.
10291999SN/Atemplate <class OS>
10301999SN/ASyscallReturn
10313114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10322680Sktlim@umich.edu           ThreadContext *tc)
10331999SN/A{
10346701Sgblack@eecs.umich.edu    int index = 0;
10356701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
10361999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
10371999SN/A        // doesn't map to any simulator fd: not a valid target fd
10381999SN/A        return -EBADF;
10391999SN/A    }
10401999SN/A
10418852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
10426701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
10436701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
10441999SN/A    struct iovec hiov[count];
10456227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
10461999SN/A        typename OS::tgt_iovec tiov;
10472461SN/A
10488852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
10498852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
10508737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
10511999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
10528852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
10538852Sandreas.hansson@arm.com                   hiov[i].iov_len);
10541999SN/A    }
10551999SN/A
10561999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
10571999SN/A
10586227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
10591999SN/A        delete [] (char *)hiov[i].iov_base;
10601999SN/A
10611999SN/A    if (result < 0)
10622218SN/A        return -errno;
10631999SN/A
10641999SN/A    return 0;
10651999SN/A}
10661999SN/A
10671999SN/A
1068378SN/A/// Target mmap() handler.
1069378SN/A///
1070378SN/A/// We don't really handle mmap().  If the target is mmaping an
1071378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1072378SN/A/// nothing (since memory is initialized to zero and the simulator
10738324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
10748324Ssteve.reinhardt@amd.com///
1075360SN/Atemplate <class OS>
10761450SN/ASyscallReturn
10773114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1078360SN/A{
10796701Sgblack@eecs.umich.edu    int index = 0;
10806701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
10816701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
10826701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
10836701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
10848324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
10856701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1086360SN/A
10879008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
10889008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
10899008Sgblack@eecs.umich.edu
10908324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
10918324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
10928324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
10938324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
10948324Ssteve.reinhardt@amd.com            return -EBADF;
10958324Ssteve.reinhardt@amd.com        }
10968324Ssteve.reinhardt@amd.com
10978324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
10988324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
10998324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
11008324Ssteve.reinhardt@amd.com            // another name on some platform
11018324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11028324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11038324Ssteve.reinhardt@amd.com        }
11048324Ssteve.reinhardt@amd.com    }
11055877Shsul@eecs.umich.edu
11062544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
11072544SN/A        (length % TheISA::VMPageSize) != 0) {
11082544SN/A        warn("mmap failing: arguments not page-aligned: "
11092544SN/A             "start 0x%x length 0x%x",
11102544SN/A             start, length);
11112544SN/A        return -EINVAL;
1112360SN/A    }
1113360SN/A
11148600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
11158600Ssteve.reinhardt@amd.com    // true if the user has been warned.
11168600Ssteve.reinhardt@amd.com    bool clobber = false;
11178600Ssteve.reinhardt@amd.com
11188600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
11198600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
11208600Ssteve.reinhardt@amd.com
11218600Ssteve.reinhardt@amd.com    if (use_provided_address) {
11228600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
11238600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
11248600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
11258600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
11268600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
11278600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
11288600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
11298600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
11308600Ssteve.reinhardt@amd.com                     start);
11318600Ssteve.reinhardt@amd.com                clobber = true;
11328600Ssteve.reinhardt@amd.com            } else {
11338600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
11348600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
11358600Ssteve.reinhardt@amd.com                use_provided_address = false;
11368600Ssteve.reinhardt@amd.com            }
11378600Ssteve.reinhardt@amd.com        }
11382544SN/A    }
11392544SN/A
11408600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
11418600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
11428600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
11438600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
11448600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
11458600Ssteve.reinhardt@amd.com            p->mmap_end = start;
11468600Ssteve.reinhardt@amd.com        } else {
11478600Ssteve.reinhardt@amd.com            start = p->mmap_end;
11488600Ssteve.reinhardt@amd.com            p->mmap_end += length;
11498600Ssteve.reinhardt@amd.com        }
11506672Sgblack@eecs.umich.edu    }
11518600Ssteve.reinhardt@amd.com
11528601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
11532544SN/A
11541458SN/A    return start;
1155360SN/A}
1156360SN/A
1157378SN/A/// Target getrlimit() handler.
1158360SN/Atemplate <class OS>
11591450SN/ASyscallReturn
11603114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11612680Sktlim@umich.edu        ThreadContext *tc)
1162360SN/A{
11636701Sgblack@eecs.umich.edu    int index = 0;
11646701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
11656701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1166360SN/A
1167360SN/A    switch (resource) {
11682064SN/A        case OS::TGT_RLIMIT_STACK:
11695877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
11702064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
11718737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11728737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11732064SN/A            break;
1174360SN/A
11755877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
11765877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
11775877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
11788737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11798737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11805877Shsul@eecs.umich.edu            break;
11815877Shsul@eecs.umich.edu
11822064SN/A        default:
11832064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
11842064SN/A                << std::endl;
11852064SN/A            abort();
11862064SN/A            break;
1187360SN/A    }
1188360SN/A
11898706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
11901458SN/A    return 0;
1191360SN/A}
1192360SN/A
1193378SN/A/// Target gettimeofday() handler.
1194360SN/Atemplate <class OS>
11951450SN/ASyscallReturn
11963114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11972680Sktlim@umich.edu        ThreadContext *tc)
1198360SN/A{
11996701Sgblack@eecs.umich.edu    int index = 0;
12006701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1201360SN/A
1202360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1203360SN/A    tp->tv_sec += seconds_since_epoch;
12046109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12056109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1206360SN/A
12078706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1208360SN/A
12091458SN/A    return 0;
1210360SN/A}
1211360SN/A
1212360SN/A
12131999SN/A/// Target utimes() handler.
12141999SN/Atemplate <class OS>
12151999SN/ASyscallReturn
12163114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12172680Sktlim@umich.edu           ThreadContext *tc)
12181999SN/A{
12191999SN/A    std::string path;
12201999SN/A
12216701Sgblack@eecs.umich.edu    int index = 0;
12228852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12236701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12246701Sgblack@eecs.umich.edu        return -EFAULT;
12256701Sgblack@eecs.umich.edu    }
12261999SN/A
12276701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
12286701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
12298706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
12301999SN/A
12311999SN/A    struct timeval hostTimeval[2];
12321999SN/A    for (int i = 0; i < 2; ++i)
12331999SN/A    {
12348737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
12358737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
12361999SN/A    }
12373669Sbinkertn@umich.edu
12383669Sbinkertn@umich.edu    // Adjust path for current working directory
12393669Sbinkertn@umich.edu    path = process->fullPath(path);
12403669Sbinkertn@umich.edu
12411999SN/A    int result = utimes(path.c_str(), hostTimeval);
12421999SN/A
12431999SN/A    if (result < 0)
12441999SN/A        return -errno;
12451999SN/A
12461999SN/A    return 0;
12471999SN/A}
1248378SN/A/// Target getrusage() function.
1249360SN/Atemplate <class OS>
12501450SN/ASyscallReturn
12513114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12522680Sktlim@umich.edu              ThreadContext *tc)
1253360SN/A{
12546701Sgblack@eecs.umich.edu    int index = 0;
12556701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
12566701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1257360SN/A
12583670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
12593670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1260360SN/A    rup->ru_stime.tv_sec = 0;
1261360SN/A    rup->ru_stime.tv_usec = 0;
1262360SN/A    rup->ru_maxrss = 0;
1263360SN/A    rup->ru_ixrss = 0;
1264360SN/A    rup->ru_idrss = 0;
1265360SN/A    rup->ru_isrss = 0;
1266360SN/A    rup->ru_minflt = 0;
1267360SN/A    rup->ru_majflt = 0;
1268360SN/A    rup->ru_nswap = 0;
1269360SN/A    rup->ru_inblock = 0;
1270360SN/A    rup->ru_oublock = 0;
1271360SN/A    rup->ru_msgsnd = 0;
1272360SN/A    rup->ru_msgrcv = 0;
1273360SN/A    rup->ru_nsignals = 0;
1274360SN/A    rup->ru_nvcsw = 0;
1275360SN/A    rup->ru_nivcsw = 0;
1276360SN/A
12773670Sbinkertn@umich.edu    switch (who) {
12783670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
12793670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
12808737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
12818737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
12823670Sbinkertn@umich.edu        break;
12833670Sbinkertn@umich.edu
12843670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
12853670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
12863670Sbinkertn@umich.edu        break;
12873670Sbinkertn@umich.edu
12883670Sbinkertn@umich.edu      default:
12893670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
12903670Sbinkertn@umich.edu        // plow ahead
12913670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
12923670Sbinkertn@umich.edu             who);
12933670Sbinkertn@umich.edu    }
12943670Sbinkertn@umich.edu
12958706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1296360SN/A
12971458SN/A    return 0;
1298360SN/A}
1299360SN/A
13006683Stjones1@inf.ed.ac.uk/// Target times() function.
13016683Stjones1@inf.ed.ac.uktemplate <class OS>
13026683Stjones1@inf.ed.ac.ukSyscallReturn
13036683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13046683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13056683Stjones1@inf.ed.ac.uk{
13066701Sgblack@eecs.umich.edu    int index = 0;
13076701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
13086683Stjones1@inf.ed.ac.uk
13096683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
13107823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
13116683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
13126683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
13136683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
13146683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
13156683Stjones1@inf.ed.ac.uk
13166683Stjones1@inf.ed.ac.uk    // Convert to host endianness
13178737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
13186683Stjones1@inf.ed.ac.uk
13196683Stjones1@inf.ed.ac.uk    // Write back
13208706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
13216683Stjones1@inf.ed.ac.uk
13226683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
13236683Stjones1@inf.ed.ac.uk    return clocks;
13246683Stjones1@inf.ed.ac.uk}
13252553SN/A
13266684Stjones1@inf.ed.ac.uk/// Target time() function.
13276684Stjones1@inf.ed.ac.uktemplate <class OS>
13286684Stjones1@inf.ed.ac.ukSyscallReturn
13296684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13306684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13316684Stjones1@inf.ed.ac.uk{
13326684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
13336684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
13346684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
13356684Stjones1@inf.ed.ac.uk
13366701Sgblack@eecs.umich.edu    int index = 0;
13376701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
13386684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
13396684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
13408737Skoansin.tan@gmail.com        t = TheISA::htog(t);
13418852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
13428852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
13436684Stjones1@inf.ed.ac.uk    }
13446684Stjones1@inf.ed.ac.uk    return sec;
13456684Stjones1@inf.ed.ac.uk}
13462553SN/A
13472553SN/A
13481354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1349