syscall_emul.hh revision 9143
1360SN/A/*
21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
3360SN/A * All rights reserved.
4360SN/A *
5360SN/A * Redistribution and use in source and binary forms, with or without
6360SN/A * modification, are permitted provided that the following conditions are
7360SN/A * met: redistributions of source code must retain the above copyright
8360SN/A * notice, this list of conditions and the following disclaimer;
9360SN/A * redistributions in binary form must reproduce the above copyright
10360SN/A * notice, this list of conditions and the following disclaimer in the
11360SN/A * documentation and/or other materials provided with the distribution;
12360SN/A * neither the name of the copyright holders nor the names of its
13360SN/A * contributors may be used to endorse or promote products derived from
14360SN/A * this software without specific prior written permission.
15360SN/A *
16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Kevin Lim
30360SN/A */
31360SN/A
321354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
331354SN/A#define __SIM_SYSCALL_EMUL_HH__
34360SN/A
352764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
362764Sstever@eecs.umich.edu                   defined(__FreeBSD__) || defined(__CYGWIN__))
372064SN/A
38360SN/A///
39360SN/A/// @file syscall_emul.hh
40360SN/A///
41360SN/A/// This file defines objects used to emulate syscalls from the target
42360SN/A/// application on the host machine.
43360SN/A
441809SN/A#ifdef __CYGWIN32__
455543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
461809SN/A#endif
473113Sgblack@eecs.umich.edu#include <sys/stat.h>
488229Snate@binkert.org#include <sys/time.h>
498229Snate@binkert.org#include <sys/uio.h>
503113Sgblack@eecs.umich.edu#include <fcntl.h>
517075Snate@binkert.org
528229Snate@binkert.org#include <cerrno>
537075Snate@binkert.org#include <string>
54360SN/A
552474SN/A#include "base/chunk_generator.hh"
565543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
572462SN/A#include "base/misc.hh"
581354SN/A#include "base/trace.hh"
596216Snate@binkert.org#include "base/types.hh"
606658Snate@binkert.org#include "config/the_isa.hh"
612474SN/A#include "cpu/base.hh"
622680Sktlim@umich.edu#include "cpu/thread_context.hh"
638232Snate@binkert.org#include "debug/SyscallVerbose.hh"
648229Snate@binkert.org#include "mem/page_table.hh"
658706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
667678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
678229Snate@binkert.org#include "sim/process.hh"
688766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
696640Svince@csl.cornell.edu#include "sim/system.hh"
70360SN/A
71360SN/A///
72360SN/A/// System call descriptor.
73360SN/A///
74360SN/Aclass SyscallDesc {
75360SN/A
76360SN/A  public:
77360SN/A
78378SN/A    /// Typedef for target syscall handler functions.
791450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
803114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
81360SN/A
825543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
835543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
845543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
85360SN/A
86360SN/A    /// Flag values for controlling syscall behavior.
87360SN/A    enum Flags {
88360SN/A        /// Don't set return regs according to funcPtr return value.
89360SN/A        /// Used for syscalls with non-standard return conventions
902680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
91360SN/A        /// sigreturn).
92360SN/A        SuppressReturnValue = 1
93360SN/A    };
94360SN/A
95360SN/A    /// Constructor.
96360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
97360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
98360SN/A    {
99360SN/A    }
100360SN/A
101360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1023114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
103360SN/A};
104360SN/A
105360SN/A
106360SN/Aclass BaseBufferArg {
107360SN/A
108360SN/A  public:
109360SN/A
110360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
111360SN/A    {
112360SN/A        bufPtr = new uint8_t[size];
113360SN/A        // clear out buffer: in case we only partially populate this,
114360SN/A        // and then do a copyOut(), we want to make sure we don't
115360SN/A        // introduce any random junk into the simulated address space
116360SN/A        memset(bufPtr, 0, size);
117360SN/A    }
118360SN/A
119360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
120360SN/A
121360SN/A    //
122360SN/A    // copy data into simulator space (read from target memory)
123360SN/A    //
1248852Sandreas.hansson@arm.com    virtual bool copyIn(SETranslatingPortProxy &memproxy)
125360SN/A    {
1268852Sandreas.hansson@arm.com        memproxy.readBlob(addr, bufPtr, size);
1275543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
128360SN/A    }
129360SN/A
130360SN/A    //
131360SN/A    // copy data out of simulator space (write to target memory)
132360SN/A    //
1338852Sandreas.hansson@arm.com    virtual bool copyOut(SETranslatingPortProxy &memproxy)
134360SN/A    {
1358852Sandreas.hansson@arm.com        memproxy.writeBlob(addr, bufPtr, size);
1365543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
137360SN/A    }
138360SN/A
139360SN/A  protected:
140360SN/A    Addr addr;
141360SN/A    int size;
142360SN/A    uint8_t *bufPtr;
143360SN/A};
144360SN/A
145360SN/A
146360SN/Aclass BufferArg : public BaseBufferArg
147360SN/A{
148360SN/A  public:
149360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1505543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
151360SN/A};
152360SN/A
153360SN/Atemplate <class T>
154360SN/Aclass TypedBufferArg : public BaseBufferArg
155360SN/A{
156360SN/A  public:
157360SN/A    // user can optionally specify a specific number of bytes to
158360SN/A    // allocate to deal with those structs that have variable-size
159360SN/A    // arrays at the end
160360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
161360SN/A        : BaseBufferArg(_addr, _size)
162360SN/A    { }
163360SN/A
164360SN/A    // type case
165360SN/A    operator T*() { return (T *)bufPtr; }
166360SN/A
167360SN/A    // dereference operators
1685543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1695543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
170502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
171360SN/A};
172360SN/A
173360SN/A//////////////////////////////////////////////////////////////////////
174360SN/A//
175360SN/A// The following emulation functions are generic enough that they
176360SN/A// don't need to be recompiled for different emulated OS's.  They are
177360SN/A// defined in sim/syscall_emul.cc.
178360SN/A//
179360SN/A//////////////////////////////////////////////////////////////////////
180360SN/A
181360SN/A
182378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1831706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
185378SN/A
186378SN/A/// Handler for unimplemented syscalls that we never intend to
187378SN/A/// implement (signal handling, etc.) and should not affect the correct
188378SN/A/// behavior of the program.  Print a warning only if the appropriate
189378SN/A/// trace flag is enabled.  Return success to the target program.
1901706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1913114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1928149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
1938149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
194360SN/A
1956109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1961706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1973114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
198378SN/A
1996109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
2006109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2016109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
2026109Ssanchezd@stanford.edu
203378SN/A/// Target getpagesize() handler.
2041706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
2053114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
206378SN/A
2075748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2085748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2095748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
210378SN/A
211378SN/A/// Target close() handler.
2121706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2133114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
214378SN/A
215378SN/A/// Target read() handler.
2161706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2173114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
218378SN/A
219378SN/A/// Target write() handler.
2201706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2213114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
222378SN/A
223378SN/A/// Target lseek() handler.
2241706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2253114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
226378SN/A
2274118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2284118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2294118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2304118Sgblack@eecs.umich.edu
231378SN/A/// Target munmap() handler.
2321706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2333114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
234378SN/A
235378SN/A/// Target gethostname() handler.
2361706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2373114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
238360SN/A
2395513SMichael.Adler@intel.com/// Target getcwd() handler.
2405513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2415513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2425513SMichael.Adler@intel.com
2435513SMichael.Adler@intel.com/// Target unlink() handler.
2445513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2455513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2465513SMichael.Adler@intel.com
247511SN/A/// Target unlink() handler.
2481706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2493114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
250511SN/A
2515513SMichael.Adler@intel.com/// Target mkdir() handler.
2525513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2535513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2545513SMichael.Adler@intel.com
255511SN/A/// Target rename() handler.
2561706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2573114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2581706SN/A
2591706SN/A
2601706SN/A/// Target truncate() handler.
2611706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2623114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2631706SN/A
2641706SN/A
2651706SN/A/// Target ftruncate() handler.
2661706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2673114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2681706SN/A
269511SN/A
2706703Svince@csl.cornell.edu/// Target truncate64() handler.
2716703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2726703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2736703Svince@csl.cornell.edu
2746685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2756685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2766685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2776685Stjones1@inf.ed.ac.uk
2786685Stjones1@inf.ed.ac.uk
2795513SMichael.Adler@intel.com/// Target umask() handler.
2805513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2815513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2825513SMichael.Adler@intel.com
2835513SMichael.Adler@intel.com
2841999SN/A/// Target chown() handler.
2851999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2863114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2871999SN/A
2881999SN/A
2891999SN/A/// Target fchown() handler.
2901999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2913114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2921999SN/A
2933079Sstever@eecs.umich.edu/// Target dup() handler.
2943079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2953114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2963079Sstever@eecs.umich.edu
2972093SN/A/// Target fnctl() handler.
2982093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2993114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3002093SN/A
3012687Sksewell@umich.edu/// Target fcntl64() handler.
3022687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3033114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3042687Sksewell@umich.edu
3052238SN/A/// Target setuid() handler.
3062238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3073114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3082238SN/A
3092238SN/A/// Target getpid() handler.
3102238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3113114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3122238SN/A
3132238SN/A/// Target getuid() handler.
3142238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3153114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3162238SN/A
3172238SN/A/// Target getgid() handler.
3182238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3193114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3202238SN/A
3212238SN/A/// Target getppid() handler.
3222238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3233114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3242238SN/A
3252238SN/A/// Target geteuid() handler.
3262238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3273114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3282238SN/A
3292238SN/A/// Target getegid() handler.
3302238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3313114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3322238SN/A
3336109Ssanchezd@stanford.edu/// Target clone() handler.
3346109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3356109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3362238SN/A
3379112Smarc.orr@gmail.com/// Futex system call
3389112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3399112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3409112Smarc.orr@gmail.comtemplate <class OS>
3419112Smarc.orr@gmail.comSyscallReturn
3429112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3439112Smarc.orr@gmail.com          ThreadContext *tc)
3449112Smarc.orr@gmail.com{
3459112Smarc.orr@gmail.com    int index_uaddr = 0;
3469112Smarc.orr@gmail.com    int index_op = 1;
3479112Smarc.orr@gmail.com    int index_val = 2;
3489112Smarc.orr@gmail.com    int index_timeout = 3;
3499112Smarc.orr@gmail.com
3509112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3519112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3529112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3539112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3549112Smarc.orr@gmail.com
3559112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3569112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3579112Smarc.orr@gmail.com
3589112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3599112Smarc.orr@gmail.com            uaddr, op, val);
3609112Smarc.orr@gmail.com
3619112Smarc.orr@gmail.com
3629112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3639112Smarc.orr@gmail.com        if (timeout != 0) {
3649112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3659112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3669112Smarc.orr@gmail.com        }
3679112Smarc.orr@gmail.com
3689112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3699112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3709112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3719112Smarc.orr@gmail.com        delete buf;
3729112Smarc.orr@gmail.com
3739112Smarc.orr@gmail.com        if(val != mem_val) {
3749112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3759112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3769112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3779112Smarc.orr@gmail.com        }
3789112Smarc.orr@gmail.com
3799112Smarc.orr@gmail.com        // Queue the thread context
3809112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3819112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3829112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3839112Smarc.orr@gmail.com        } else {
3849112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3859112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3869112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3879112Smarc.orr@gmail.com        }
3889112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3899112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3909112Smarc.orr@gmail.com                                "thread context\n");
3919112Smarc.orr@gmail.com        tc->suspend();
3929112Smarc.orr@gmail.com        return 0;
3939112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3949112Smarc.orr@gmail.com        int wokenUp = 0;
3959112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3969112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3979112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3989112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3999112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4009112Smarc.orr@gmail.com                tcWaitList->pop_front();
4019112Smarc.orr@gmail.com                wokenUp++;
4029112Smarc.orr@gmail.com            }
4039112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4049112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4059112Smarc.orr@gmail.com                delete tcWaitList;
4069112Smarc.orr@gmail.com            }
4079112Smarc.orr@gmail.com        }
4089112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4099112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4109112Smarc.orr@gmail.com        return wokenUp;
4119112Smarc.orr@gmail.com    } else {
4129112Smarc.orr@gmail.com        warn("sys_futex: op %d is not implemented, just returning...");
4139112Smarc.orr@gmail.com        return 0;
4149112Smarc.orr@gmail.com    }
4159112Smarc.orr@gmail.com
4169112Smarc.orr@gmail.com}
4179112Smarc.orr@gmail.com
4182238SN/A
4192238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4202238SN/A/// returning a second value in a register other than the normal return register
4212238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4223114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4232238SN/A
4242238SN/A/// Target getpidPseudo() handler.
4252238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4263114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4272238SN/A
4282238SN/A/// Target getuidPseudo() handler.
4292238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4303114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4312238SN/A
4322238SN/A/// Target getgidPseudo() handler.
4332238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4343114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4352238SN/A
4362238SN/A
4371354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4381354SN/Aconst int one_million = 1000000;
4391354SN/A
4401354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4411354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4421354SN/A/// real-world time) to keep simulations repeatable.
4431354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4441354SN/A
4451354SN/A/// Helper function to convert current elapsed time to seconds and
4461354SN/A/// microseconds.
4471354SN/Atemplate <class T1, class T2>
4481354SN/Avoid
4491354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4501354SN/A{
4517823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4521354SN/A    sec = elapsed_usecs / one_million;
4531354SN/A    usec = elapsed_usecs % one_million;
4541354SN/A}
4551354SN/A
456360SN/A//////////////////////////////////////////////////////////////////////
457360SN/A//
458360SN/A// The following emulation functions are generic, but need to be
459360SN/A// templated to account for differences in types, constants, etc.
460360SN/A//
461360SN/A//////////////////////////////////////////////////////////////////////
462360SN/A
4633113Sgblack@eecs.umich.edu#if NO_STAT64
4643113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4653113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4663113Sgblack@eecs.umich.edu#else
4673113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4683113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4693113Sgblack@eecs.umich.edu#endif
4703113Sgblack@eecs.umich.edu
4713113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4723113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4733113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4743113Sgblack@eecs.umich.edu
4753113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4763113Sgblack@eecs.umich.edustatic void
4773113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4783113Sgblack@eecs.umich.edu{
4794189Sgblack@eecs.umich.edu    using namespace TheISA;
4804189Sgblack@eecs.umich.edu
4813113Sgblack@eecs.umich.edu    if (fakeTTY)
4823113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4833113Sgblack@eecs.umich.edu    else
4843113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4858737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4863113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4878737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4883277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4895515SMichael.Adler@intel.com    if (fakeTTY) {
4905515SMichael.Adler@intel.com        // Claim to be a character device
4915515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4925515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4935515SMichael.Adler@intel.com    }
4948737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4953277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4968737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4973277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4988737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4993277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5008737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5013113Sgblack@eecs.umich.edu    if (fakeTTY)
5023113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5033113Sgblack@eecs.umich.edu    else
5043113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5058737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5063113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5078737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5083114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5098737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5103114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5118737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5123114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5138737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5144061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5154061Sgblack@eecs.umich.edu    // consistently across different hosts.
5164061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5178737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5183113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5198737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5203113Sgblack@eecs.umich.edu}
5213113Sgblack@eecs.umich.edu
5223113Sgblack@eecs.umich.edu// Same for stat64
5233113Sgblack@eecs.umich.edu
5243113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5253113Sgblack@eecs.umich.edustatic void
5263113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5273113Sgblack@eecs.umich.edu{
5284189Sgblack@eecs.umich.edu    using namespace TheISA;
5294189Sgblack@eecs.umich.edu
5303113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5313113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5323113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5338737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5343113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5358737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5363113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5378737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5383113Sgblack@eecs.umich.edu#else
5393113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5403113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5413113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5423113Sgblack@eecs.umich.edu#endif
5433113Sgblack@eecs.umich.edu}
5443113Sgblack@eecs.umich.edu
5453113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5463113Sgblack@eecs.umich.edutemplate<class OS>
5473113Sgblack@eecs.umich.edustatic void
5488852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5493113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5503113Sgblack@eecs.umich.edu{
5513113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5523113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5533113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5543113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5553113Sgblack@eecs.umich.edu}
5563113Sgblack@eecs.umich.edu
5573113Sgblack@eecs.umich.edutemplate<class OS>
5583113Sgblack@eecs.umich.edustatic void
5598852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5603113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5613113Sgblack@eecs.umich.edu{
5623113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5633113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5646686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5653113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5663113Sgblack@eecs.umich.edu}
5673113Sgblack@eecs.umich.edu
568378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
569378SN/A/// only to find out if their stdout is a tty, to determine whether to
5709141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5719141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
572360SN/Atemplate <class OS>
5731450SN/ASyscallReturn
5743114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5752680Sktlim@umich.edu          ThreadContext *tc)
576360SN/A{
5776701Sgblack@eecs.umich.edu    int index = 0;
5786701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5796701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
580360SN/A
5811969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
582360SN/A
583360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
584360SN/A        // doesn't map to any simulator fd: not a valid target fd
5851458SN/A        return -EBADF;
586360SN/A    }
587360SN/A
5889141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5891458SN/A        return -ENOTTY;
5909141Smarc.orr@gmail.com    }
591360SN/A
5929141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5939141Smarc.orr@gmail.com         fd, req, tc->pcState());
5949141Smarc.orr@gmail.com    return -ENOTTY;
595360SN/A}
596360SN/A
597378SN/A/// Target open() handler.
598360SN/Atemplate <class OS>
5991450SN/ASyscallReturn
6003114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6012680Sktlim@umich.edu         ThreadContext *tc)
602360SN/A{
603360SN/A    std::string path;
604360SN/A
6056701Sgblack@eecs.umich.edu    int index = 0;
6068852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6076701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6081458SN/A        return -EFAULT;
609360SN/A
610360SN/A    if (path == "/dev/sysdev0") {
611360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
612360SN/A        // We don't support it, so just punt.
6131706SN/A        warn("Ignoring open(%s, ...)\n", path);
6141458SN/A        return -ENOENT;
615360SN/A    }
616360SN/A
6176701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6186701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
619360SN/A    int hostFlags = 0;
620360SN/A
621360SN/A    // translate open flags
622360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
623360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
624360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
625360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
626360SN/A        }
627360SN/A    }
628360SN/A
629360SN/A    // any target flags left?
630360SN/A    if (tgtFlags != 0)
6311706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
632360SN/A
633360SN/A#ifdef __CYGWIN32__
634360SN/A    hostFlags |= O_BINARY;
635360SN/A#endif
636360SN/A
6373669Sbinkertn@umich.edu    // Adjust path for current working directory
6383669Sbinkertn@umich.edu    path = process->fullPath(path);
6393669Sbinkertn@umich.edu
6401706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6411706SN/A
6425795Ssaidi@eecs.umich.edu    int fd;
6439143Ssteve.reinhardt@amd.com    int local_errno;
6449142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6459142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6469143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6475795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6489143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6495795Ssaidi@eecs.umich.edu     } else {
6505795Ssaidi@eecs.umich.edu        // open the file
6515795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6529143Ssteve.reinhardt@amd.com        local_errno = errno;
6535795Ssaidi@eecs.umich.edu     }
654360SN/A
6559143Ssteve.reinhardt@amd.com    if (fd == -1)
6569143Ssteve.reinhardt@amd.com        return -local_errno;
6579143Ssteve.reinhardt@amd.com
6589143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
659360SN/A}
660360SN/A
6616640Svince@csl.cornell.edu/// Target sysinfo() handler.
6626640Svince@csl.cornell.edutemplate <class OS>
6636640Svince@csl.cornell.eduSyscallReturn
6646640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6656640Svince@csl.cornell.edu         ThreadContext *tc)
6666640Svince@csl.cornell.edu{
6676640Svince@csl.cornell.edu
6686701Sgblack@eecs.umich.edu    int index = 0;
6696701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
6706701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
6716640Svince@csl.cornell.edu
6726701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
6736701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
6746640Svince@csl.cornell.edu
6758706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
6766640Svince@csl.cornell.edu
6776701Sgblack@eecs.umich.edu    return 0;
6786640Svince@csl.cornell.edu}
679360SN/A
6801999SN/A/// Target chmod() handler.
6811999SN/Atemplate <class OS>
6821999SN/ASyscallReturn
6833114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6842680Sktlim@umich.edu          ThreadContext *tc)
6851999SN/A{
6861999SN/A    std::string path;
6871999SN/A
6886701Sgblack@eecs.umich.edu    int index = 0;
6898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6906701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
6911999SN/A        return -EFAULT;
6926701Sgblack@eecs.umich.edu    }
6931999SN/A
6946701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
6951999SN/A    mode_t hostMode = 0;
6961999SN/A
6971999SN/A    // XXX translate mode flags via OS::something???
6981999SN/A    hostMode = mode;
6991999SN/A
7003669Sbinkertn@umich.edu    // Adjust path for current working directory
7013669Sbinkertn@umich.edu    path = process->fullPath(path);
7023669Sbinkertn@umich.edu
7031999SN/A    // do the chmod
7041999SN/A    int result = chmod(path.c_str(), hostMode);
7051999SN/A    if (result < 0)
7062218SN/A        return -errno;
7071999SN/A
7081999SN/A    return 0;
7091999SN/A}
7101999SN/A
7111999SN/A
7121999SN/A/// Target fchmod() handler.
7131999SN/Atemplate <class OS>
7141999SN/ASyscallReturn
7153114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7162680Sktlim@umich.edu           ThreadContext *tc)
7171999SN/A{
7186701Sgblack@eecs.umich.edu    int index = 0;
7196701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7201999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7211999SN/A        // doesn't map to any simulator fd: not a valid target fd
7221999SN/A        return -EBADF;
7231999SN/A    }
7241999SN/A
7256701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7261999SN/A    mode_t hostMode = 0;
7271999SN/A
7281999SN/A    // XXX translate mode flags via OS::someting???
7291999SN/A    hostMode = mode;
7301999SN/A
7311999SN/A    // do the fchmod
7321999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7331999SN/A    if (result < 0)
7342218SN/A        return -errno;
7351999SN/A
7361999SN/A    return 0;
7371999SN/A}
7381999SN/A
7395877Shsul@eecs.umich.edu/// Target mremap() handler.
7405877Shsul@eecs.umich.edutemplate <class OS>
7415877Shsul@eecs.umich.eduSyscallReturn
7425877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7435877Shsul@eecs.umich.edu{
7446701Sgblack@eecs.umich.edu    int index = 0;
7456701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7466701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7476701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7486701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
7495877Shsul@eecs.umich.edu
7505877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
7515877Shsul@eecs.umich.edu            (new_length % TheISA::VMPageSize != 0)) {
7525877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7535877Shsul@eecs.umich.edu        return -EINVAL;
7545877Shsul@eecs.umich.edu    }
7555877Shsul@eecs.umich.edu
7565877Shsul@eecs.umich.edu    if (new_length > old_length) {
7575877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end) {
7585877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
7598601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
7605877Shsul@eecs.umich.edu            process->mmap_end += diff;
7615877Shsul@eecs.umich.edu            return start;
7625877Shsul@eecs.umich.edu        } else {
7635877Shsul@eecs.umich.edu            // sys/mman.h defined MREMAP_MAYMOVE
7645877Shsul@eecs.umich.edu            if (!(flags & 1)) {
7655877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
7665877Shsul@eecs.umich.edu                return -ENOMEM;
7675877Shsul@eecs.umich.edu            } else {
7685877Shsul@eecs.umich.edu                process->pTable->remap(start, old_length, process->mmap_end);
7695877Shsul@eecs.umich.edu                warn("mremapping to totally new vaddr %08p-%08p, adding %d\n",
7705877Shsul@eecs.umich.edu                        process->mmap_end, process->mmap_end + new_length, new_length);
7715877Shsul@eecs.umich.edu                start = process->mmap_end;
7725877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
7738601Ssteve.reinhardt@amd.com                process->allocateMem(start + old_length,
7748601Ssteve.reinhardt@amd.com                                     new_length - old_length);
7755877Shsul@eecs.umich.edu                process->mmap_end += new_length;
7765877Shsul@eecs.umich.edu                warn("returning %08p as start\n", start);
7775877Shsul@eecs.umich.edu                return start;
7785877Shsul@eecs.umich.edu            }
7795877Shsul@eecs.umich.edu        }
7805877Shsul@eecs.umich.edu    } else {
7818601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
7825877Shsul@eecs.umich.edu        return start;
7835877Shsul@eecs.umich.edu    }
7845877Shsul@eecs.umich.edu}
7851999SN/A
786378SN/A/// Target stat() handler.
787360SN/Atemplate <class OS>
7881450SN/ASyscallReturn
7893114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7902680Sktlim@umich.edu         ThreadContext *tc)
791360SN/A{
792360SN/A    std::string path;
793360SN/A
7946701Sgblack@eecs.umich.edu    int index = 0;
7958852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7966701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7976701Sgblack@eecs.umich.edu        return -EFAULT;
7986701Sgblack@eecs.umich.edu    }
7996701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
800360SN/A
8013669Sbinkertn@umich.edu    // Adjust path for current working directory
8023669Sbinkertn@umich.edu    path = process->fullPath(path);
8033669Sbinkertn@umich.edu
804360SN/A    struct stat hostBuf;
805360SN/A    int result = stat(path.c_str(), &hostBuf);
806360SN/A
807360SN/A    if (result < 0)
8082218SN/A        return -errno;
809360SN/A
8108706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
811360SN/A
8121458SN/A    return 0;
813360SN/A}
814360SN/A
815360SN/A
8165074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8175074Ssaidi@eecs.umich.edutemplate <class OS>
8185074Ssaidi@eecs.umich.eduSyscallReturn
8195074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8205074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8215074Ssaidi@eecs.umich.edu{
8225074Ssaidi@eecs.umich.edu    std::string path;
8235074Ssaidi@eecs.umich.edu
8246701Sgblack@eecs.umich.edu    int index = 0;
8258852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8266701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8275074Ssaidi@eecs.umich.edu        return -EFAULT;
8286701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8295074Ssaidi@eecs.umich.edu
8305074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8315074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8325074Ssaidi@eecs.umich.edu
8335208Ssaidi@eecs.umich.edu#if NO_STAT64
8345208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8355208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8365208Ssaidi@eecs.umich.edu#else
8375074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8385074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8395208Ssaidi@eecs.umich.edu#endif
8405074Ssaidi@eecs.umich.edu
8415074Ssaidi@eecs.umich.edu    if (result < 0)
8425074Ssaidi@eecs.umich.edu        return -errno;
8435074Ssaidi@eecs.umich.edu
8448706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
8455074Ssaidi@eecs.umich.edu
8465074Ssaidi@eecs.umich.edu    return 0;
8475074Ssaidi@eecs.umich.edu}
8485074Ssaidi@eecs.umich.edu
8495074Ssaidi@eecs.umich.edu
8501999SN/A/// Target fstat64() handler.
8511999SN/Atemplate <class OS>
8521999SN/ASyscallReturn
8533114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8542680Sktlim@umich.edu            ThreadContext *tc)
8551999SN/A{
8566701Sgblack@eecs.umich.edu    int index = 0;
8576701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
8586701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8591999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8601999SN/A        // doesn't map to any simulator fd: not a valid target fd
8611999SN/A        return -EBADF;
8621999SN/A    }
8631999SN/A
8642764Sstever@eecs.umich.edu#if NO_STAT64
8652064SN/A    struct stat  hostBuf;
8662064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
8672064SN/A#else
8682064SN/A    struct stat64  hostBuf;
8691999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
8702064SN/A#endif
8711999SN/A
8721999SN/A    if (result < 0)
8732218SN/A        return -errno;
8741999SN/A
8758706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
8761999SN/A
8771999SN/A    return 0;
8781999SN/A}
8791999SN/A
8801999SN/A
881378SN/A/// Target lstat() handler.
882360SN/Atemplate <class OS>
8831450SN/ASyscallReturn
8843114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8852680Sktlim@umich.edu          ThreadContext *tc)
886360SN/A{
887360SN/A    std::string path;
888360SN/A
8896701Sgblack@eecs.umich.edu    int index = 0;
8908852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8916701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8926701Sgblack@eecs.umich.edu        return -EFAULT;
8936701Sgblack@eecs.umich.edu    }
8946701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
895360SN/A
8963669Sbinkertn@umich.edu    // Adjust path for current working directory
8973669Sbinkertn@umich.edu    path = process->fullPath(path);
8983669Sbinkertn@umich.edu
899360SN/A    struct stat hostBuf;
900360SN/A    int result = lstat(path.c_str(), &hostBuf);
901360SN/A
902360SN/A    if (result < 0)
9031458SN/A        return -errno;
904360SN/A
9058706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
906360SN/A
9071458SN/A    return 0;
908360SN/A}
909360SN/A
9101999SN/A/// Target lstat64() handler.
9111999SN/Atemplate <class OS>
9121999SN/ASyscallReturn
9133114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9142680Sktlim@umich.edu            ThreadContext *tc)
9151999SN/A{
9161999SN/A    std::string path;
9171999SN/A
9186701Sgblack@eecs.umich.edu    int index = 0;
9198852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9206701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9216701Sgblack@eecs.umich.edu        return -EFAULT;
9226701Sgblack@eecs.umich.edu    }
9236701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9241999SN/A
9253669Sbinkertn@umich.edu    // Adjust path for current working directory
9263669Sbinkertn@umich.edu    path = process->fullPath(path);
9273669Sbinkertn@umich.edu
9282764Sstever@eecs.umich.edu#if NO_STAT64
9292064SN/A    struct stat hostBuf;
9302064SN/A    int result = lstat(path.c_str(), &hostBuf);
9312064SN/A#else
9321999SN/A    struct stat64 hostBuf;
9331999SN/A    int result = lstat64(path.c_str(), &hostBuf);
9342064SN/A#endif
9351999SN/A
9361999SN/A    if (result < 0)
9371999SN/A        return -errno;
9381999SN/A
9398706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9401999SN/A
9411999SN/A    return 0;
9421999SN/A}
9431999SN/A
944378SN/A/// Target fstat() handler.
945360SN/Atemplate <class OS>
9461450SN/ASyscallReturn
9473114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9482680Sktlim@umich.edu          ThreadContext *tc)
949360SN/A{
9506701Sgblack@eecs.umich.edu    int index = 0;
9516701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
9526701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
953360SN/A
9541969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
955360SN/A
956360SN/A    if (fd < 0)
9571458SN/A        return -EBADF;
958360SN/A
959360SN/A    struct stat hostBuf;
960360SN/A    int result = fstat(fd, &hostBuf);
961360SN/A
962360SN/A    if (result < 0)
9631458SN/A        return -errno;
964360SN/A
9658706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9662021SN/A
9671458SN/A    return 0;
968360SN/A}
969360SN/A
970360SN/A
9711706SN/A/// Target statfs() handler.
9721706SN/Atemplate <class OS>
9731706SN/ASyscallReturn
9743114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9752680Sktlim@umich.edu           ThreadContext *tc)
9761706SN/A{
9771706SN/A    std::string path;
9781706SN/A
9796701Sgblack@eecs.umich.edu    int index = 0;
9808852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9816701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9826701Sgblack@eecs.umich.edu        return -EFAULT;
9836701Sgblack@eecs.umich.edu    }
9846701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9851706SN/A
9863669Sbinkertn@umich.edu    // Adjust path for current working directory
9873669Sbinkertn@umich.edu    path = process->fullPath(path);
9883669Sbinkertn@umich.edu
9891706SN/A    struct statfs hostBuf;
9901706SN/A    int result = statfs(path.c_str(), &hostBuf);
9911706SN/A
9921706SN/A    if (result < 0)
9932218SN/A        return -errno;
9941706SN/A
9958706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
9961706SN/A
9971706SN/A    return 0;
9981706SN/A}
9991706SN/A
10001706SN/A
10011706SN/A/// Target fstatfs() handler.
10021706SN/Atemplate <class OS>
10031706SN/ASyscallReturn
10043114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10052680Sktlim@umich.edu            ThreadContext *tc)
10061706SN/A{
10076701Sgblack@eecs.umich.edu    int index = 0;
10086701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10096701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10101706SN/A
10111706SN/A    if (fd < 0)
10121706SN/A        return -EBADF;
10131706SN/A
10141706SN/A    struct statfs hostBuf;
10151706SN/A    int result = fstatfs(fd, &hostBuf);
10161706SN/A
10171706SN/A    if (result < 0)
10182218SN/A        return -errno;
10191706SN/A
10208706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10211706SN/A
10221706SN/A    return 0;
10231706SN/A}
10241706SN/A
10251706SN/A
10261999SN/A/// Target writev() handler.
10271999SN/Atemplate <class OS>
10281999SN/ASyscallReturn
10293114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10302680Sktlim@umich.edu           ThreadContext *tc)
10311999SN/A{
10326701Sgblack@eecs.umich.edu    int index = 0;
10336701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
10341999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
10351999SN/A        // doesn't map to any simulator fd: not a valid target fd
10361999SN/A        return -EBADF;
10371999SN/A    }
10381999SN/A
10398852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
10406701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
10416701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
10421999SN/A    struct iovec hiov[count];
10436227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
10441999SN/A        typename OS::tgt_iovec tiov;
10452461SN/A
10468852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
10478852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
10488737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
10491999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
10508852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
10518852Sandreas.hansson@arm.com                   hiov[i].iov_len);
10521999SN/A    }
10531999SN/A
10541999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
10551999SN/A
10566227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
10571999SN/A        delete [] (char *)hiov[i].iov_base;
10581999SN/A
10591999SN/A    if (result < 0)
10602218SN/A        return -errno;
10611999SN/A
10621999SN/A    return 0;
10631999SN/A}
10641999SN/A
10651999SN/A
1066378SN/A/// Target mmap() handler.
1067378SN/A///
1068378SN/A/// We don't really handle mmap().  If the target is mmaping an
1069378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1070378SN/A/// nothing (since memory is initialized to zero and the simulator
10718324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
10728324Ssteve.reinhardt@amd.com///
1073360SN/Atemplate <class OS>
10741450SN/ASyscallReturn
10753114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1076360SN/A{
10776701Sgblack@eecs.umich.edu    int index = 0;
10786701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
10796701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
10806701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
10816701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
10828324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
10836701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1084360SN/A
10859008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
10869008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
10879008Sgblack@eecs.umich.edu
10888324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
10898324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
10908324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
10918324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
10928324Ssteve.reinhardt@amd.com            return -EBADF;
10938324Ssteve.reinhardt@amd.com        }
10948324Ssteve.reinhardt@amd.com
10958324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
10968324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
10978324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
10988324Ssteve.reinhardt@amd.com            // another name on some platform
10998324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11008324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11018324Ssteve.reinhardt@amd.com        }
11028324Ssteve.reinhardt@amd.com    }
11035877Shsul@eecs.umich.edu
11042544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
11052544SN/A        (length % TheISA::VMPageSize) != 0) {
11062544SN/A        warn("mmap failing: arguments not page-aligned: "
11072544SN/A             "start 0x%x length 0x%x",
11082544SN/A             start, length);
11092544SN/A        return -EINVAL;
1110360SN/A    }
1111360SN/A
11128600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
11138600Ssteve.reinhardt@amd.com    // true if the user has been warned.
11148600Ssteve.reinhardt@amd.com    bool clobber = false;
11158600Ssteve.reinhardt@amd.com
11168600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
11178600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
11188600Ssteve.reinhardt@amd.com
11198600Ssteve.reinhardt@amd.com    if (use_provided_address) {
11208600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
11218600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
11228600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
11238600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
11248600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
11258600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
11268600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
11278600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
11288600Ssteve.reinhardt@amd.com                     start);
11298600Ssteve.reinhardt@amd.com                clobber = true;
11308600Ssteve.reinhardt@amd.com            } else {
11318600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
11328600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
11338600Ssteve.reinhardt@amd.com                use_provided_address = false;
11348600Ssteve.reinhardt@amd.com            }
11358600Ssteve.reinhardt@amd.com        }
11362544SN/A    }
11372544SN/A
11388600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
11398600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
11408600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
11418600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
11428600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
11438600Ssteve.reinhardt@amd.com            p->mmap_end = start;
11448600Ssteve.reinhardt@amd.com        } else {
11458600Ssteve.reinhardt@amd.com            start = p->mmap_end;
11468600Ssteve.reinhardt@amd.com            p->mmap_end += length;
11478600Ssteve.reinhardt@amd.com        }
11486672Sgblack@eecs.umich.edu    }
11498600Ssteve.reinhardt@amd.com
11508601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
11512544SN/A
11521458SN/A    return start;
1153360SN/A}
1154360SN/A
1155378SN/A/// Target getrlimit() handler.
1156360SN/Atemplate <class OS>
11571450SN/ASyscallReturn
11583114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11592680Sktlim@umich.edu        ThreadContext *tc)
1160360SN/A{
11616701Sgblack@eecs.umich.edu    int index = 0;
11626701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
11636701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1164360SN/A
1165360SN/A    switch (resource) {
11662064SN/A        case OS::TGT_RLIMIT_STACK:
11675877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
11682064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
11698737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11708737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11712064SN/A            break;
1172360SN/A
11735877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
11745877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
11755877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
11768737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
11778737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
11785877Shsul@eecs.umich.edu            break;
11795877Shsul@eecs.umich.edu
11802064SN/A        default:
11812064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
11822064SN/A                << std::endl;
11832064SN/A            abort();
11842064SN/A            break;
1185360SN/A    }
1186360SN/A
11878706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
11881458SN/A    return 0;
1189360SN/A}
1190360SN/A
1191378SN/A/// Target gettimeofday() handler.
1192360SN/Atemplate <class OS>
11931450SN/ASyscallReturn
11943114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11952680Sktlim@umich.edu        ThreadContext *tc)
1196360SN/A{
11976701Sgblack@eecs.umich.edu    int index = 0;
11986701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1199360SN/A
1200360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1201360SN/A    tp->tv_sec += seconds_since_epoch;
12026109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12036109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1204360SN/A
12058706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1206360SN/A
12071458SN/A    return 0;
1208360SN/A}
1209360SN/A
1210360SN/A
12111999SN/A/// Target utimes() handler.
12121999SN/Atemplate <class OS>
12131999SN/ASyscallReturn
12143114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12152680Sktlim@umich.edu           ThreadContext *tc)
12161999SN/A{
12171999SN/A    std::string path;
12181999SN/A
12196701Sgblack@eecs.umich.edu    int index = 0;
12208852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12216701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12226701Sgblack@eecs.umich.edu        return -EFAULT;
12236701Sgblack@eecs.umich.edu    }
12241999SN/A
12256701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
12266701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
12278706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
12281999SN/A
12291999SN/A    struct timeval hostTimeval[2];
12301999SN/A    for (int i = 0; i < 2; ++i)
12311999SN/A    {
12328737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
12338737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
12341999SN/A    }
12353669Sbinkertn@umich.edu
12363669Sbinkertn@umich.edu    // Adjust path for current working directory
12373669Sbinkertn@umich.edu    path = process->fullPath(path);
12383669Sbinkertn@umich.edu
12391999SN/A    int result = utimes(path.c_str(), hostTimeval);
12401999SN/A
12411999SN/A    if (result < 0)
12421999SN/A        return -errno;
12431999SN/A
12441999SN/A    return 0;
12451999SN/A}
1246378SN/A/// Target getrusage() function.
1247360SN/Atemplate <class OS>
12481450SN/ASyscallReturn
12493114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12502680Sktlim@umich.edu              ThreadContext *tc)
1251360SN/A{
12526701Sgblack@eecs.umich.edu    int index = 0;
12536701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
12546701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1255360SN/A
12563670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
12573670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1258360SN/A    rup->ru_stime.tv_sec = 0;
1259360SN/A    rup->ru_stime.tv_usec = 0;
1260360SN/A    rup->ru_maxrss = 0;
1261360SN/A    rup->ru_ixrss = 0;
1262360SN/A    rup->ru_idrss = 0;
1263360SN/A    rup->ru_isrss = 0;
1264360SN/A    rup->ru_minflt = 0;
1265360SN/A    rup->ru_majflt = 0;
1266360SN/A    rup->ru_nswap = 0;
1267360SN/A    rup->ru_inblock = 0;
1268360SN/A    rup->ru_oublock = 0;
1269360SN/A    rup->ru_msgsnd = 0;
1270360SN/A    rup->ru_msgrcv = 0;
1271360SN/A    rup->ru_nsignals = 0;
1272360SN/A    rup->ru_nvcsw = 0;
1273360SN/A    rup->ru_nivcsw = 0;
1274360SN/A
12753670Sbinkertn@umich.edu    switch (who) {
12763670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
12773670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
12788737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
12798737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
12803670Sbinkertn@umich.edu        break;
12813670Sbinkertn@umich.edu
12823670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
12833670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
12843670Sbinkertn@umich.edu        break;
12853670Sbinkertn@umich.edu
12863670Sbinkertn@umich.edu      default:
12873670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
12883670Sbinkertn@umich.edu        // plow ahead
12893670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
12903670Sbinkertn@umich.edu             who);
12913670Sbinkertn@umich.edu    }
12923670Sbinkertn@umich.edu
12938706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1294360SN/A
12951458SN/A    return 0;
1296360SN/A}
1297360SN/A
12986683Stjones1@inf.ed.ac.uk/// Target times() function.
12996683Stjones1@inf.ed.ac.uktemplate <class OS>
13006683Stjones1@inf.ed.ac.ukSyscallReturn
13016683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13026683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13036683Stjones1@inf.ed.ac.uk{
13046701Sgblack@eecs.umich.edu    int index = 0;
13056701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
13066683Stjones1@inf.ed.ac.uk
13076683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
13087823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
13096683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
13106683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
13116683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
13126683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
13136683Stjones1@inf.ed.ac.uk
13146683Stjones1@inf.ed.ac.uk    // Convert to host endianness
13158737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
13166683Stjones1@inf.ed.ac.uk
13176683Stjones1@inf.ed.ac.uk    // Write back
13188706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
13196683Stjones1@inf.ed.ac.uk
13206683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
13216683Stjones1@inf.ed.ac.uk    return clocks;
13226683Stjones1@inf.ed.ac.uk}
13232553SN/A
13246684Stjones1@inf.ed.ac.uk/// Target time() function.
13256684Stjones1@inf.ed.ac.uktemplate <class OS>
13266684Stjones1@inf.ed.ac.ukSyscallReturn
13276684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13286684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13296684Stjones1@inf.ed.ac.uk{
13306684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
13316684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
13326684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
13336684Stjones1@inf.ed.ac.uk
13346701Sgblack@eecs.umich.edu    int index = 0;
13356701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
13366684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
13376684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
13388737Skoansin.tan@gmail.com        t = TheISA::htog(t);
13398852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
13408852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
13416684Stjones1@inf.ed.ac.uk    }
13426684Stjones1@inf.ed.ac.uk    return sec;
13436684Stjones1@inf.ed.ac.uk}
13442553SN/A
13452553SN/A
13461354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1347