syscall_emul.hh revision 10484
1360SN/A/*
210027SChris.Adeniyi-Jones@arm.com * Copyright (c) 2012-2013 ARM Limited
310027SChris.Adeniyi-Jones@arm.com * All rights reserved
410027SChris.Adeniyi-Jones@arm.com *
510027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
610027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
710027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
810027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
910027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1010027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1110027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1210027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1310027SChris.Adeniyi-Jones@arm.com *
141458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
15360SN/A * All rights reserved.
16360SN/A *
17360SN/A * Redistribution and use in source and binary forms, with or without
18360SN/A * modification, are permitted provided that the following conditions are
19360SN/A * met: redistributions of source code must retain the above copyright
20360SN/A * notice, this list of conditions and the following disclaimer;
21360SN/A * redistributions in binary form must reproduce the above copyright
22360SN/A * notice, this list of conditions and the following disclaimer in the
23360SN/A * documentation and/or other materials provided with the distribution;
24360SN/A * neither the name of the copyright holders nor the names of its
25360SN/A * contributors may be used to endorse or promote products derived from
26360SN/A * this software without specific prior written permission.
27360SN/A *
28360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
412665Ssaidi@eecs.umich.edu *          Kevin Lim
42360SN/A */
43360SN/A
441354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
451354SN/A#define __SIM_SYSCALL_EMUL_HH__
46360SN/A
472764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
489202Spalle@lyckegaard.dk  defined(__FreeBSD__) || defined(__CYGWIN__) || \
499202Spalle@lyckegaard.dk  defined(__NetBSD__))
502064SN/A
51360SN/A///
52360SN/A/// @file syscall_emul.hh
53360SN/A///
54360SN/A/// This file defines objects used to emulate syscalls from the target
55360SN/A/// application on the host machine.
56360SN/A
571809SN/A#ifdef __CYGWIN32__
585543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
591809SN/A#endif
603113Sgblack@eecs.umich.edu#include <sys/stat.h>
618229Snate@binkert.org#include <sys/time.h>
628229Snate@binkert.org#include <sys/uio.h>
633113Sgblack@eecs.umich.edu#include <fcntl.h>
647075Snate@binkert.org
658229Snate@binkert.org#include <cerrno>
667075Snate@binkert.org#include <string>
67360SN/A
682474SN/A#include "base/chunk_generator.hh"
695543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
702462SN/A#include "base/misc.hh"
711354SN/A#include "base/trace.hh"
726216Snate@binkert.org#include "base/types.hh"
736658Snate@binkert.org#include "config/the_isa.hh"
742474SN/A#include "cpu/base.hh"
752680Sktlim@umich.edu#include "cpu/thread_context.hh"
768232Snate@binkert.org#include "debug/SyscallVerbose.hh"
778229Snate@binkert.org#include "mem/page_table.hh"
788706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
797678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
808229Snate@binkert.org#include "sim/process.hh"
818766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
826640Svince@csl.cornell.edu#include "sim/system.hh"
83360SN/A
84360SN/A///
85360SN/A/// System call descriptor.
86360SN/A///
87360SN/Aclass SyscallDesc {
88360SN/A
89360SN/A  public:
90360SN/A
91378SN/A    /// Typedef for target syscall handler functions.
921450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
933114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
94360SN/A
955543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
965543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
975543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
98360SN/A
99360SN/A    /// Flag values for controlling syscall behavior.
100360SN/A    enum Flags {
101360SN/A        /// Don't set return regs according to funcPtr return value.
102360SN/A        /// Used for syscalls with non-standard return conventions
1032680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
104360SN/A        /// sigreturn).
105360SN/A        SuppressReturnValue = 1
106360SN/A    };
107360SN/A
108360SN/A    /// Constructor.
109360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
110360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
111360SN/A    {
112360SN/A    }
113360SN/A
114360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1153114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
116360SN/A};
117360SN/A
118360SN/A
119360SN/Aclass BaseBufferArg {
120360SN/A
121360SN/A  public:
122360SN/A
123360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
124360SN/A    {
125360SN/A        bufPtr = new uint8_t[size];
126360SN/A        // clear out buffer: in case we only partially populate this,
127360SN/A        // and then do a copyOut(), we want to make sure we don't
128360SN/A        // introduce any random junk into the simulated address space
129360SN/A        memset(bufPtr, 0, size);
130360SN/A    }
131360SN/A
132360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
133360SN/A
134360SN/A    //
135360SN/A    // copy data into simulator space (read from target memory)
136360SN/A    //
1378852Sandreas.hansson@arm.com    virtual bool copyIn(SETranslatingPortProxy &memproxy)
138360SN/A    {
1398852Sandreas.hansson@arm.com        memproxy.readBlob(addr, bufPtr, size);
1405543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
141360SN/A    }
142360SN/A
143360SN/A    //
144360SN/A    // copy data out of simulator space (write to target memory)
145360SN/A    //
1468852Sandreas.hansson@arm.com    virtual bool copyOut(SETranslatingPortProxy &memproxy)
147360SN/A    {
1488852Sandreas.hansson@arm.com        memproxy.writeBlob(addr, bufPtr, size);
1495543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
150360SN/A    }
151360SN/A
152360SN/A  protected:
153360SN/A    Addr addr;
154360SN/A    int size;
155360SN/A    uint8_t *bufPtr;
156360SN/A};
157360SN/A
158360SN/A
159360SN/Aclass BufferArg : public BaseBufferArg
160360SN/A{
161360SN/A  public:
162360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1635543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
164360SN/A};
165360SN/A
166360SN/Atemplate <class T>
167360SN/Aclass TypedBufferArg : public BaseBufferArg
168360SN/A{
169360SN/A  public:
170360SN/A    // user can optionally specify a specific number of bytes to
171360SN/A    // allocate to deal with those structs that have variable-size
172360SN/A    // arrays at the end
173360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
174360SN/A        : BaseBufferArg(_addr, _size)
175360SN/A    { }
176360SN/A
177360SN/A    // type case
178360SN/A    operator T*() { return (T *)bufPtr; }
179360SN/A
180360SN/A    // dereference operators
1815543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1825543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
183502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
184360SN/A};
185360SN/A
186360SN/A//////////////////////////////////////////////////////////////////////
187360SN/A//
188360SN/A// The following emulation functions are generic enough that they
189360SN/A// don't need to be recompiled for different emulated OS's.  They are
190360SN/A// defined in sim/syscall_emul.cc.
191360SN/A//
192360SN/A//////////////////////////////////////////////////////////////////////
193360SN/A
194360SN/A
195378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1961706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1973114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
198378SN/A
199378SN/A/// Handler for unimplemented syscalls that we never intend to
200378SN/A/// implement (signal handling, etc.) and should not affect the correct
201378SN/A/// behavior of the program.  Print a warning only if the appropriate
202378SN/A/// trace flag is enabled.  Return success to the target program.
2031706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
2043114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2058149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
2068149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
207360SN/A
2086109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
2091706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
2103114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
211378SN/A
2126109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
2136109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2146109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
2156109Ssanchezd@stanford.edu
216378SN/A/// Target getpagesize() handler.
2171706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
2183114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
219378SN/A
2205748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2215748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2225748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
223378SN/A
224378SN/A/// Target close() handler.
2251706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2263114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
227378SN/A
228378SN/A/// Target read() handler.
2291706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2303114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
231378SN/A
232378SN/A/// Target write() handler.
2331706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2343114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
235378SN/A
236378SN/A/// Target lseek() handler.
2371706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2383114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
239378SN/A
2404118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2414118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2424118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2434118Sgblack@eecs.umich.edu
244378SN/A/// Target munmap() handler.
2451706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2463114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
247378SN/A
248378SN/A/// Target gethostname() handler.
2491706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2503114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
251360SN/A
2525513SMichael.Adler@intel.com/// Target getcwd() handler.
2535513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2545513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2555513SMichael.Adler@intel.com
25610203SAli.Saidi@ARM.com/// Target readlink() handler.
25710203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
25810203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
25910203SAli.Saidi@ARM.com                           int index = 0);
2605513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2615513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2625513SMichael.Adler@intel.com
263511SN/A/// Target unlink() handler.
2641706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2653114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
266511SN/A
2675513SMichael.Adler@intel.com/// Target mkdir() handler.
2685513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2695513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2705513SMichael.Adler@intel.com
271511SN/A/// Target rename() handler.
2721706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2741706SN/A
2751706SN/A
2761706SN/A/// Target truncate() handler.
2771706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2783114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2791706SN/A
2801706SN/A
2811706SN/A/// Target ftruncate() handler.
2821706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2833114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2841706SN/A
285511SN/A
2866703Svince@csl.cornell.edu/// Target truncate64() handler.
2876703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2886703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2896703Svince@csl.cornell.edu
2906685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2916685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2926685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2936685Stjones1@inf.ed.ac.uk
2946685Stjones1@inf.ed.ac.uk
2955513SMichael.Adler@intel.com/// Target umask() handler.
2965513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2975513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2985513SMichael.Adler@intel.com
2995513SMichael.Adler@intel.com
3001999SN/A/// Target chown() handler.
3011999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
3023114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
3031999SN/A
3041999SN/A
3051999SN/A/// Target fchown() handler.
3061999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
3073114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
3081999SN/A
3093079Sstever@eecs.umich.edu/// Target dup() handler.
3103079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
3113114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
3123079Sstever@eecs.umich.edu
3132093SN/A/// Target fnctl() handler.
3142093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
3153114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3162093SN/A
3172687Sksewell@umich.edu/// Target fcntl64() handler.
3182687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3193114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3202687Sksewell@umich.edu
3212238SN/A/// Target setuid() handler.
3222238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3233114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3242238SN/A
3252238SN/A/// Target getpid() handler.
3262238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3273114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3282238SN/A
3292238SN/A/// Target getuid() handler.
3302238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3313114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3322238SN/A
3332238SN/A/// Target getgid() handler.
3342238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3353114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3362238SN/A
3372238SN/A/// Target getppid() handler.
3382238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3393114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3402238SN/A
3412238SN/A/// Target geteuid() handler.
3422238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3433114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3442238SN/A
3452238SN/A/// Target getegid() handler.
3462238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3473114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3482238SN/A
3496109Ssanchezd@stanford.edu/// Target clone() handler.
3506109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3516109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3522238SN/A
3539455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3549455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3559455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
35610203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
35710203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
35810203SAli.Saidi@ARM.com                               int index);
3599455Smitch.hayenga+gem5@gmail.com
3609112Smarc.orr@gmail.com/// Futex system call
3619112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3629112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3639112Smarc.orr@gmail.comtemplate <class OS>
3649112Smarc.orr@gmail.comSyscallReturn
3659112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3669112Smarc.orr@gmail.com          ThreadContext *tc)
3679112Smarc.orr@gmail.com{
3689112Smarc.orr@gmail.com    int index_uaddr = 0;
3699112Smarc.orr@gmail.com    int index_op = 1;
3709112Smarc.orr@gmail.com    int index_val = 2;
3719112Smarc.orr@gmail.com    int index_timeout = 3;
3729112Smarc.orr@gmail.com
3739112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3749112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3759112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3769112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3779112Smarc.orr@gmail.com
3789112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3799112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3809112Smarc.orr@gmail.com
3819112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3829112Smarc.orr@gmail.com            uaddr, op, val);
3839112Smarc.orr@gmail.com
3849238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3859112Smarc.orr@gmail.com
3869112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3879112Smarc.orr@gmail.com        if (timeout != 0) {
3889112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3899112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3909112Smarc.orr@gmail.com        }
3919112Smarc.orr@gmail.com
3929112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3939112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3949112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3959112Smarc.orr@gmail.com        delete buf;
3969112Smarc.orr@gmail.com
3979112Smarc.orr@gmail.com        if(val != mem_val) {
3989112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3999112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
4009112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
4019112Smarc.orr@gmail.com        }
4029112Smarc.orr@gmail.com
4039112Smarc.orr@gmail.com        // Queue the thread context
4049112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
4059112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
4069112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4079112Smarc.orr@gmail.com        } else {
4089112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
4099112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
4109112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
4119112Smarc.orr@gmail.com        }
4129112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
4139112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
4149112Smarc.orr@gmail.com                                "thread context\n");
4159112Smarc.orr@gmail.com        tc->suspend();
4169112Smarc.orr@gmail.com        return 0;
4179112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
4189112Smarc.orr@gmail.com        int wokenUp = 0;
4199112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
4209112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
4219112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4229112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
4239112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4249112Smarc.orr@gmail.com                tcWaitList->pop_front();
4259112Smarc.orr@gmail.com                wokenUp++;
4269112Smarc.orr@gmail.com            }
4279112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4289112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4299112Smarc.orr@gmail.com                delete tcWaitList;
4309112Smarc.orr@gmail.com            }
4319112Smarc.orr@gmail.com        }
4329112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4339112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4349112Smarc.orr@gmail.com        return wokenUp;
4359112Smarc.orr@gmail.com    } else {
4369238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
4379112Smarc.orr@gmail.com        return 0;
4389112Smarc.orr@gmail.com    }
4399112Smarc.orr@gmail.com
4409112Smarc.orr@gmail.com}
4419112Smarc.orr@gmail.com
44210484SMichael.Adler@intel.com/// Target getdents() handler.
44310484SMichael.Adler@intel.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
44410484SMichael.Adler@intel.com                           LiveProcess *process, ThreadContext *tc);
44510484SMichael.Adler@intel.com
44610484SMichael.Adler@intel.com/// Target getdents64() handler.
44710484SMichael.Adler@intel.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
44810484SMichael.Adler@intel.com                             LiveProcess *process, ThreadContext *tc);
44910484SMichael.Adler@intel.com
4502238SN/A
4512238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4522238SN/A/// returning a second value in a register other than the normal return register
4532238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4543114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4552238SN/A
4562238SN/A/// Target getpidPseudo() handler.
4572238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4583114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4592238SN/A
4602238SN/A/// Target getuidPseudo() handler.
4612238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4623114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4632238SN/A
4642238SN/A/// Target getgidPseudo() handler.
4652238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4663114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4672238SN/A
4682238SN/A
4691354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4701354SN/Aconst int one_million = 1000000;
4711354SN/A
4721354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4731354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4741354SN/A/// real-world time) to keep simulations repeatable.
4751354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4761354SN/A
4771354SN/A/// Helper function to convert current elapsed time to seconds and
4781354SN/A/// microseconds.
4791354SN/Atemplate <class T1, class T2>
4801354SN/Avoid
4811354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4821354SN/A{
4837823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4841354SN/A    sec = elapsed_usecs / one_million;
4851354SN/A    usec = elapsed_usecs % one_million;
4861354SN/A}
4871354SN/A
488360SN/A//////////////////////////////////////////////////////////////////////
489360SN/A//
490360SN/A// The following emulation functions are generic, but need to be
491360SN/A// templated to account for differences in types, constants, etc.
492360SN/A//
493360SN/A//////////////////////////////////////////////////////////////////////
494360SN/A
4953113Sgblack@eecs.umich.edu#if NO_STAT64
4963113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4973113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4983113Sgblack@eecs.umich.edu#else
4993113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5003113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
5013113Sgblack@eecs.umich.edu#endif
5023113Sgblack@eecs.umich.edu
5033113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
5043113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
5053113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
5063113Sgblack@eecs.umich.edu
5073113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
5083113Sgblack@eecs.umich.edustatic void
5093113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
5103113Sgblack@eecs.umich.edu{
5114189Sgblack@eecs.umich.edu    using namespace TheISA;
5124189Sgblack@eecs.umich.edu
5133113Sgblack@eecs.umich.edu    if (fakeTTY)
5143113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5153113Sgblack@eecs.umich.edu    else
5163113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5178737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5183113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5198737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5203277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5215515SMichael.Adler@intel.com    if (fakeTTY) {
5225515SMichael.Adler@intel.com        // Claim to be a character device
5235515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5245515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5255515SMichael.Adler@intel.com    }
5268737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5273277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5288737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5293277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5308737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5313277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5328737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5333113Sgblack@eecs.umich.edu    if (fakeTTY)
5343113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5353113Sgblack@eecs.umich.edu    else
5363113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5378737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5383113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5398737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5403114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5418737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5423114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5438737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5443114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5458737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5464061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5474061Sgblack@eecs.umich.edu    // consistently across different hosts.
5484061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5498737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5503113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5518737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5523113Sgblack@eecs.umich.edu}
5533113Sgblack@eecs.umich.edu
5543113Sgblack@eecs.umich.edu// Same for stat64
5553113Sgblack@eecs.umich.edu
5563113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5573113Sgblack@eecs.umich.edustatic void
5583113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5593113Sgblack@eecs.umich.edu{
5604189Sgblack@eecs.umich.edu    using namespace TheISA;
5614189Sgblack@eecs.umich.edu
5623113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5633113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5643113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5658737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5663113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5678737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5683113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5698737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5703113Sgblack@eecs.umich.edu#else
5713113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5723113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5733113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5743113Sgblack@eecs.umich.edu#endif
5753113Sgblack@eecs.umich.edu}
5763113Sgblack@eecs.umich.edu
5773113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5783113Sgblack@eecs.umich.edutemplate<class OS>
5793113Sgblack@eecs.umich.edustatic void
5808852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5813113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5823113Sgblack@eecs.umich.edu{
5833113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5843113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5853113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5863113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5873113Sgblack@eecs.umich.edu}
5883113Sgblack@eecs.umich.edu
5893113Sgblack@eecs.umich.edutemplate<class OS>
5903113Sgblack@eecs.umich.edustatic void
5918852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5923113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5933113Sgblack@eecs.umich.edu{
5943113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5953113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5966686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5973113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5983113Sgblack@eecs.umich.edu}
5993113Sgblack@eecs.umich.edu
600378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
601378SN/A/// only to find out if their stdout is a tty, to determine whether to
6029141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
6039141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
604360SN/Atemplate <class OS>
6051450SN/ASyscallReturn
6063114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6072680Sktlim@umich.edu          ThreadContext *tc)
608360SN/A{
6096701Sgblack@eecs.umich.edu    int index = 0;
6106701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6116701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
612360SN/A
6131969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
614360SN/A
615360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
616360SN/A        // doesn't map to any simulator fd: not a valid target fd
6171458SN/A        return -EBADF;
618360SN/A    }
619360SN/A
6209141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
6211458SN/A        return -ENOTTY;
6229141Smarc.orr@gmail.com    }
623360SN/A
6249141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
6259141Smarc.orr@gmail.com         fd, req, tc->pcState());
6269141Smarc.orr@gmail.com    return -ENOTTY;
627360SN/A}
628360SN/A
629360SN/Atemplate <class OS>
63010027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
6313114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
63210027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
633360SN/A{
634360SN/A    std::string path;
635360SN/A
6368852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6376701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6381458SN/A        return -EFAULT;
639360SN/A
640360SN/A    if (path == "/dev/sysdev0") {
641360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
642360SN/A        // We don't support it, so just punt.
6431706SN/A        warn("Ignoring open(%s, ...)\n", path);
6441458SN/A        return -ENOENT;
645360SN/A    }
646360SN/A
6476701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6486701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
649360SN/A    int hostFlags = 0;
650360SN/A
651360SN/A    // translate open flags
652360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
653360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
654360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
655360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
656360SN/A        }
657360SN/A    }
658360SN/A
659360SN/A    // any target flags left?
660360SN/A    if (tgtFlags != 0)
6611706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
662360SN/A
663360SN/A#ifdef __CYGWIN32__
664360SN/A    hostFlags |= O_BINARY;
665360SN/A#endif
666360SN/A
6673669Sbinkertn@umich.edu    // Adjust path for current working directory
6683669Sbinkertn@umich.edu    path = process->fullPath(path);
6693669Sbinkertn@umich.edu
6701706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6711706SN/A
6725795Ssaidi@eecs.umich.edu    int fd;
6739143Ssteve.reinhardt@amd.com    int local_errno;
6749142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6759142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6769143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6775795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6789143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6795795Ssaidi@eecs.umich.edu     } else {
6805795Ssaidi@eecs.umich.edu        // open the file
6815795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6829143Ssteve.reinhardt@amd.com        local_errno = errno;
6835795Ssaidi@eecs.umich.edu     }
684360SN/A
6859143Ssteve.reinhardt@amd.com    if (fd == -1)
6869143Ssteve.reinhardt@amd.com        return -local_errno;
6879143Ssteve.reinhardt@amd.com
6889143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
689360SN/A}
690360SN/A
69110027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
69210027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
69310027SChris.Adeniyi-Jones@arm.comSyscallReturn
69410027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69510027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
69610027SChris.Adeniyi-Jones@arm.com{
69710027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
69810027SChris.Adeniyi-Jones@arm.com}
69910027SChris.Adeniyi-Jones@arm.com
70010027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
70110027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
70210027SChris.Adeniyi-Jones@arm.comSyscallReturn
70310027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70410027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
70510027SChris.Adeniyi-Jones@arm.com{
70610027SChris.Adeniyi-Jones@arm.com    int index = 0;
70710027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
70810027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
70910027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
71010027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
71110027SChris.Adeniyi-Jones@arm.com}
71210027SChris.Adeniyi-Jones@arm.com
71310203SAli.Saidi@ARM.com/// Target facessat() handler
71410203SAli.Saidi@ARM.comtemplate <class OS>
71510203SAli.Saidi@ARM.comSyscallReturn
71610203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71710203SAli.Saidi@ARM.com        ThreadContext *tc)
71810203SAli.Saidi@ARM.com{
71910203SAli.Saidi@ARM.com    int index = 0;
72010203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
72110203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
72210203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
72310203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
72410203SAli.Saidi@ARM.com}
72510203SAli.Saidi@ARM.com
72610203SAli.Saidi@ARM.com/// Target readlinkat() handler
72710203SAli.Saidi@ARM.comtemplate <class OS>
72810203SAli.Saidi@ARM.comSyscallReturn
72910203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
73010203SAli.Saidi@ARM.com        ThreadContext *tc)
73110203SAli.Saidi@ARM.com{
73210203SAli.Saidi@ARM.com    int index = 0;
73310203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
73410203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
73510203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
73610203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
73710203SAli.Saidi@ARM.com}
73810203SAli.Saidi@ARM.com
7396640Svince@csl.cornell.edu/// Target sysinfo() handler.
7406640Svince@csl.cornell.edutemplate <class OS>
7416640Svince@csl.cornell.eduSyscallReturn
7426640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7436640Svince@csl.cornell.edu         ThreadContext *tc)
7446640Svince@csl.cornell.edu{
7456640Svince@csl.cornell.edu
7466701Sgblack@eecs.umich.edu    int index = 0;
7476701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
7486701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
7496640Svince@csl.cornell.edu
7506701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7516701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7526640Svince@csl.cornell.edu
7538706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7546640Svince@csl.cornell.edu
7556701Sgblack@eecs.umich.edu    return 0;
7566640Svince@csl.cornell.edu}
757360SN/A
7581999SN/A/// Target chmod() handler.
7591999SN/Atemplate <class OS>
7601999SN/ASyscallReturn
7613114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7622680Sktlim@umich.edu          ThreadContext *tc)
7631999SN/A{
7641999SN/A    std::string path;
7651999SN/A
7666701Sgblack@eecs.umich.edu    int index = 0;
7678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7686701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7691999SN/A        return -EFAULT;
7706701Sgblack@eecs.umich.edu    }
7711999SN/A
7726701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7731999SN/A    mode_t hostMode = 0;
7741999SN/A
7751999SN/A    // XXX translate mode flags via OS::something???
7761999SN/A    hostMode = mode;
7771999SN/A
7783669Sbinkertn@umich.edu    // Adjust path for current working directory
7793669Sbinkertn@umich.edu    path = process->fullPath(path);
7803669Sbinkertn@umich.edu
7811999SN/A    // do the chmod
7821999SN/A    int result = chmod(path.c_str(), hostMode);
7831999SN/A    if (result < 0)
7842218SN/A        return -errno;
7851999SN/A
7861999SN/A    return 0;
7871999SN/A}
7881999SN/A
7891999SN/A
7901999SN/A/// Target fchmod() handler.
7911999SN/Atemplate <class OS>
7921999SN/ASyscallReturn
7933114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7942680Sktlim@umich.edu           ThreadContext *tc)
7951999SN/A{
7966701Sgblack@eecs.umich.edu    int index = 0;
7976701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7981999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7991999SN/A        // doesn't map to any simulator fd: not a valid target fd
8001999SN/A        return -EBADF;
8011999SN/A    }
8021999SN/A
8036701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8041999SN/A    mode_t hostMode = 0;
8051999SN/A
8061999SN/A    // XXX translate mode flags via OS::someting???
8071999SN/A    hostMode = mode;
8081999SN/A
8091999SN/A    // do the fchmod
8101999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
8111999SN/A    if (result < 0)
8122218SN/A        return -errno;
8131999SN/A
8141999SN/A    return 0;
8151999SN/A}
8161999SN/A
8175877Shsul@eecs.umich.edu/// Target mremap() handler.
8185877Shsul@eecs.umich.edutemplate <class OS>
8195877Shsul@eecs.umich.eduSyscallReturn
8205877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8215877Shsul@eecs.umich.edu{
8226701Sgblack@eecs.umich.edu    int index = 0;
8236701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8246701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8256701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8266701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
82710027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
82810027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
82910027SChris.Adeniyi-Jones@arm.com
83010027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
83110027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8325877Shsul@eecs.umich.edu
83310318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
83410318Sandreas.hansson@arm.com        (new_length % TheISA::PageBytes != 0) ||
83510318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8365877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8375877Shsul@eecs.umich.edu        return -EINVAL;
8385877Shsul@eecs.umich.edu    }
8395877Shsul@eecs.umich.edu
8405877Shsul@eecs.umich.edu    if (new_length > old_length) {
84110027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
84210027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8435877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8448601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8455877Shsul@eecs.umich.edu            process->mmap_end += diff;
8465877Shsul@eecs.umich.edu            return start;
8475877Shsul@eecs.umich.edu        } else {
84810027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8495877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8505877Shsul@eecs.umich.edu                return -ENOMEM;
8515877Shsul@eecs.umich.edu            } else {
85210027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
85310027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
85410027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
85510027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
85610027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
85710027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8585877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
85910027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
86010027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
86110027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
86210027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
86310027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
86410027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
86510027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
86610027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
86710027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
86810027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
86910027SChris.Adeniyi-Jones@arm.com                }
87010027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
87110027SChris.Adeniyi-Jones@arm.com                return new_start;
8725877Shsul@eecs.umich.edu            }
8735877Shsul@eecs.umich.edu        }
8745877Shsul@eecs.umich.edu    } else {
87510027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
87610027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8778601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
87810027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8795877Shsul@eecs.umich.edu    }
8805877Shsul@eecs.umich.edu}
8811999SN/A
882378SN/A/// Target stat() handler.
883360SN/Atemplate <class OS>
8841450SN/ASyscallReturn
8853114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8862680Sktlim@umich.edu         ThreadContext *tc)
887360SN/A{
888360SN/A    std::string path;
889360SN/A
8906701Sgblack@eecs.umich.edu    int index = 0;
8918852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8926701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8936701Sgblack@eecs.umich.edu        return -EFAULT;
8946701Sgblack@eecs.umich.edu    }
8956701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
896360SN/A
8973669Sbinkertn@umich.edu    // Adjust path for current working directory
8983669Sbinkertn@umich.edu    path = process->fullPath(path);
8993669Sbinkertn@umich.edu
900360SN/A    struct stat hostBuf;
901360SN/A    int result = stat(path.c_str(), &hostBuf);
902360SN/A
903360SN/A    if (result < 0)
9042218SN/A        return -errno;
905360SN/A
9068706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
907360SN/A
9081458SN/A    return 0;
909360SN/A}
910360SN/A
911360SN/A
9125074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9135074Ssaidi@eecs.umich.edutemplate <class OS>
9145074Ssaidi@eecs.umich.eduSyscallReturn
9155074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9165074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9175074Ssaidi@eecs.umich.edu{
9185074Ssaidi@eecs.umich.edu    std::string path;
9195074Ssaidi@eecs.umich.edu
9206701Sgblack@eecs.umich.edu    int index = 0;
9218852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9226701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9235074Ssaidi@eecs.umich.edu        return -EFAULT;
9246701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9255074Ssaidi@eecs.umich.edu
9265074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9275074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9285074Ssaidi@eecs.umich.edu
9295208Ssaidi@eecs.umich.edu#if NO_STAT64
9305208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9315208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9325208Ssaidi@eecs.umich.edu#else
9335074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9345074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9355208Ssaidi@eecs.umich.edu#endif
9365074Ssaidi@eecs.umich.edu
9375074Ssaidi@eecs.umich.edu    if (result < 0)
9385074Ssaidi@eecs.umich.edu        return -errno;
9395074Ssaidi@eecs.umich.edu
9408706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9415074Ssaidi@eecs.umich.edu
9425074Ssaidi@eecs.umich.edu    return 0;
9435074Ssaidi@eecs.umich.edu}
9445074Ssaidi@eecs.umich.edu
9455074Ssaidi@eecs.umich.edu
94610027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
94710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
94810027SChris.Adeniyi-Jones@arm.comSyscallReturn
94910027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
95010027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
95110027SChris.Adeniyi-Jones@arm.com{
95210027SChris.Adeniyi-Jones@arm.com    int index = 0;
95310027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
95410027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
95510027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
95610027SChris.Adeniyi-Jones@arm.com
95710027SChris.Adeniyi-Jones@arm.com    std::string path;
95810027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
95910027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
96010027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
96110027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
96210027SChris.Adeniyi-Jones@arm.com
96310027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
96410027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
96510027SChris.Adeniyi-Jones@arm.com
96610027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
96710027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
96810027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
96910027SChris.Adeniyi-Jones@arm.com#else
97010027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
97110027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
97210027SChris.Adeniyi-Jones@arm.com#endif
97310027SChris.Adeniyi-Jones@arm.com
97410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
97510027SChris.Adeniyi-Jones@arm.com        return -errno;
97610027SChris.Adeniyi-Jones@arm.com
97710027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
97810027SChris.Adeniyi-Jones@arm.com
97910027SChris.Adeniyi-Jones@arm.com    return 0;
98010027SChris.Adeniyi-Jones@arm.com}
98110027SChris.Adeniyi-Jones@arm.com
98210027SChris.Adeniyi-Jones@arm.com
9831999SN/A/// Target fstat64() handler.
9841999SN/Atemplate <class OS>
9851999SN/ASyscallReturn
9863114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9872680Sktlim@umich.edu            ThreadContext *tc)
9881999SN/A{
9896701Sgblack@eecs.umich.edu    int index = 0;
9906701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9916701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9921999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9931999SN/A        // doesn't map to any simulator fd: not a valid target fd
9941999SN/A        return -EBADF;
9951999SN/A    }
9961999SN/A
9972764Sstever@eecs.umich.edu#if NO_STAT64
9982064SN/A    struct stat  hostBuf;
9992064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
10002064SN/A#else
10012064SN/A    struct stat64  hostBuf;
10021999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
10032064SN/A#endif
10041999SN/A
10051999SN/A    if (result < 0)
10062218SN/A        return -errno;
10071999SN/A
10088706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10091999SN/A
10101999SN/A    return 0;
10111999SN/A}
10121999SN/A
10131999SN/A
1014378SN/A/// Target lstat() handler.
1015360SN/Atemplate <class OS>
10161450SN/ASyscallReturn
10173114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10182680Sktlim@umich.edu          ThreadContext *tc)
1019360SN/A{
1020360SN/A    std::string path;
1021360SN/A
10226701Sgblack@eecs.umich.edu    int index = 0;
10238852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10246701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10256701Sgblack@eecs.umich.edu        return -EFAULT;
10266701Sgblack@eecs.umich.edu    }
10276701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1028360SN/A
10293669Sbinkertn@umich.edu    // Adjust path for current working directory
10303669Sbinkertn@umich.edu    path = process->fullPath(path);
10313669Sbinkertn@umich.edu
1032360SN/A    struct stat hostBuf;
1033360SN/A    int result = lstat(path.c_str(), &hostBuf);
1034360SN/A
1035360SN/A    if (result < 0)
10361458SN/A        return -errno;
1037360SN/A
10388706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1039360SN/A
10401458SN/A    return 0;
1041360SN/A}
1042360SN/A
10431999SN/A/// Target lstat64() handler.
10441999SN/Atemplate <class OS>
10451999SN/ASyscallReturn
10463114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10472680Sktlim@umich.edu            ThreadContext *tc)
10481999SN/A{
10491999SN/A    std::string path;
10501999SN/A
10516701Sgblack@eecs.umich.edu    int index = 0;
10528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10536701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10546701Sgblack@eecs.umich.edu        return -EFAULT;
10556701Sgblack@eecs.umich.edu    }
10566701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10571999SN/A
10583669Sbinkertn@umich.edu    // Adjust path for current working directory
10593669Sbinkertn@umich.edu    path = process->fullPath(path);
10603669Sbinkertn@umich.edu
10612764Sstever@eecs.umich.edu#if NO_STAT64
10622064SN/A    struct stat hostBuf;
10632064SN/A    int result = lstat(path.c_str(), &hostBuf);
10642064SN/A#else
10651999SN/A    struct stat64 hostBuf;
10661999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10672064SN/A#endif
10681999SN/A
10691999SN/A    if (result < 0)
10701999SN/A        return -errno;
10711999SN/A
10728706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10731999SN/A
10741999SN/A    return 0;
10751999SN/A}
10761999SN/A
1077378SN/A/// Target fstat() handler.
1078360SN/Atemplate <class OS>
10791450SN/ASyscallReturn
10803114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10812680Sktlim@umich.edu          ThreadContext *tc)
1082360SN/A{
10836701Sgblack@eecs.umich.edu    int index = 0;
10846701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10856701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1086360SN/A
10871969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1088360SN/A
1089360SN/A    if (fd < 0)
10901458SN/A        return -EBADF;
1091360SN/A
1092360SN/A    struct stat hostBuf;
1093360SN/A    int result = fstat(fd, &hostBuf);
1094360SN/A
1095360SN/A    if (result < 0)
10961458SN/A        return -errno;
1097360SN/A
10988706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10992021SN/A
11001458SN/A    return 0;
1101360SN/A}
1102360SN/A
1103360SN/A
11041706SN/A/// Target statfs() handler.
11051706SN/Atemplate <class OS>
11061706SN/ASyscallReturn
11073114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11082680Sktlim@umich.edu           ThreadContext *tc)
11091706SN/A{
11101706SN/A    std::string path;
11111706SN/A
11126701Sgblack@eecs.umich.edu    int index = 0;
11138852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11146701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11156701Sgblack@eecs.umich.edu        return -EFAULT;
11166701Sgblack@eecs.umich.edu    }
11176701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11181706SN/A
11193669Sbinkertn@umich.edu    // Adjust path for current working directory
11203669Sbinkertn@umich.edu    path = process->fullPath(path);
11213669Sbinkertn@umich.edu
11221706SN/A    struct statfs hostBuf;
11231706SN/A    int result = statfs(path.c_str(), &hostBuf);
11241706SN/A
11251706SN/A    if (result < 0)
11262218SN/A        return -errno;
11271706SN/A
11288706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11291706SN/A
11301706SN/A    return 0;
11311706SN/A}
11321706SN/A
11331706SN/A
11341706SN/A/// Target fstatfs() handler.
11351706SN/Atemplate <class OS>
11361706SN/ASyscallReturn
11373114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11382680Sktlim@umich.edu            ThreadContext *tc)
11391706SN/A{
11406701Sgblack@eecs.umich.edu    int index = 0;
11416701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11426701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11431706SN/A
11441706SN/A    if (fd < 0)
11451706SN/A        return -EBADF;
11461706SN/A
11471706SN/A    struct statfs hostBuf;
11481706SN/A    int result = fstatfs(fd, &hostBuf);
11491706SN/A
11501706SN/A    if (result < 0)
11512218SN/A        return -errno;
11521706SN/A
11538706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11541706SN/A
11551706SN/A    return 0;
11561706SN/A}
11571706SN/A
11581706SN/A
11591999SN/A/// Target writev() handler.
11601999SN/Atemplate <class OS>
11611999SN/ASyscallReturn
11623114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11632680Sktlim@umich.edu           ThreadContext *tc)
11641999SN/A{
11656701Sgblack@eecs.umich.edu    int index = 0;
11666701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11671999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11681999SN/A        // doesn't map to any simulator fd: not a valid target fd
11691999SN/A        return -EBADF;
11701999SN/A    }
11711999SN/A
11728852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11736701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11746701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11751999SN/A    struct iovec hiov[count];
11766227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11771999SN/A        typename OS::tgt_iovec tiov;
11782461SN/A
11798852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11808852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11818737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11821999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11838852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11848852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11851999SN/A    }
11861999SN/A
11871999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11881999SN/A
11896227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11901999SN/A        delete [] (char *)hiov[i].iov_base;
11911999SN/A
11921999SN/A    if (result < 0)
11932218SN/A        return -errno;
11941999SN/A
11951999SN/A    return 0;
11961999SN/A}
11971999SN/A
11981999SN/A
1199378SN/A/// Target mmap() handler.
1200378SN/A///
1201378SN/A/// We don't really handle mmap().  If the target is mmaping an
1202378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1203378SN/A/// nothing (since memory is initialized to zero and the simulator
12048324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
12058324Ssteve.reinhardt@amd.com///
1206360SN/Atemplate <class OS>
12071450SN/ASyscallReturn
12083114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1209360SN/A{
12106701Sgblack@eecs.umich.edu    int index = 0;
12116701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12126701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
12136701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
12146701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
12158324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
12166701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1217360SN/A
12189008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
12199008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12209008Sgblack@eecs.umich.edu
12218324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
12228324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
12238324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
12248324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12258324Ssteve.reinhardt@amd.com            return -EBADF;
12268324Ssteve.reinhardt@amd.com        }
12278324Ssteve.reinhardt@amd.com
12288324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
12298324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12308324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12318324Ssteve.reinhardt@amd.com            // another name on some platform
12328324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
12338324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
12348324Ssteve.reinhardt@amd.com        }
12358324Ssteve.reinhardt@amd.com    }
12365877Shsul@eecs.umich.edu
123710318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
123810318Sandreas.hansson@arm.com        (length % TheISA::PageBytes) != 0) {
12392544SN/A        warn("mmap failing: arguments not page-aligned: "
12402544SN/A             "start 0x%x length 0x%x",
12412544SN/A             start, length);
12422544SN/A        return -EINVAL;
1243360SN/A    }
1244360SN/A
12458600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12468600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12478600Ssteve.reinhardt@amd.com    bool clobber = false;
12488600Ssteve.reinhardt@amd.com
12498600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12508600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12518600Ssteve.reinhardt@amd.com
12528600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12538600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12548600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12558600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12568600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12578600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12588600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
12598600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
12608600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
12618600Ssteve.reinhardt@amd.com                     start);
12628600Ssteve.reinhardt@amd.com                clobber = true;
12638600Ssteve.reinhardt@amd.com            } else {
12648600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12658600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12668600Ssteve.reinhardt@amd.com                use_provided_address = false;
12678600Ssteve.reinhardt@amd.com            }
12688600Ssteve.reinhardt@amd.com        }
12692544SN/A    }
12702544SN/A
12718600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12728600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12738600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12748600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12758600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12768600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12778600Ssteve.reinhardt@amd.com        } else {
12788600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12798600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12808600Ssteve.reinhardt@amd.com        }
12816672Sgblack@eecs.umich.edu    }
12828600Ssteve.reinhardt@amd.com
12838601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12842544SN/A
12851458SN/A    return start;
1286360SN/A}
1287360SN/A
1288378SN/A/// Target getrlimit() handler.
1289360SN/Atemplate <class OS>
12901450SN/ASyscallReturn
12913114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12922680Sktlim@umich.edu        ThreadContext *tc)
1293360SN/A{
12946701Sgblack@eecs.umich.edu    int index = 0;
12956701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12966701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1297360SN/A
1298360SN/A    switch (resource) {
12992064SN/A        case OS::TGT_RLIMIT_STACK:
13005877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
13012064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
13028737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13038737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13042064SN/A            break;
1305360SN/A
13065877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
13075877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
13085877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
13098737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13108737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13115877Shsul@eecs.umich.edu            break;
13125877Shsul@eecs.umich.edu
13132064SN/A        default:
13142064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
13152064SN/A                << std::endl;
13162064SN/A            abort();
13172064SN/A            break;
1318360SN/A    }
1319360SN/A
13208706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13211458SN/A    return 0;
1322360SN/A}
1323360SN/A
1324378SN/A/// Target gettimeofday() handler.
1325360SN/Atemplate <class OS>
13261450SN/ASyscallReturn
13273114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13282680Sktlim@umich.edu        ThreadContext *tc)
1329360SN/A{
13306701Sgblack@eecs.umich.edu    int index = 0;
13316701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1332360SN/A
1333360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1334360SN/A    tp->tv_sec += seconds_since_epoch;
13356109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13366109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1337360SN/A
13388706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1339360SN/A
13401458SN/A    return 0;
1341360SN/A}
1342360SN/A
1343360SN/A
13441999SN/A/// Target utimes() handler.
13451999SN/Atemplate <class OS>
13461999SN/ASyscallReturn
13473114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13482680Sktlim@umich.edu           ThreadContext *tc)
13491999SN/A{
13501999SN/A    std::string path;
13511999SN/A
13526701Sgblack@eecs.umich.edu    int index = 0;
13538852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13546701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13556701Sgblack@eecs.umich.edu        return -EFAULT;
13566701Sgblack@eecs.umich.edu    }
13571999SN/A
13586701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13596701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13608706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13611999SN/A
13621999SN/A    struct timeval hostTimeval[2];
13631999SN/A    for (int i = 0; i < 2; ++i)
13641999SN/A    {
13658737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13668737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13671999SN/A    }
13683669Sbinkertn@umich.edu
13693669Sbinkertn@umich.edu    // Adjust path for current working directory
13703669Sbinkertn@umich.edu    path = process->fullPath(path);
13713669Sbinkertn@umich.edu
13721999SN/A    int result = utimes(path.c_str(), hostTimeval);
13731999SN/A
13741999SN/A    if (result < 0)
13751999SN/A        return -errno;
13761999SN/A
13771999SN/A    return 0;
13781999SN/A}
1379378SN/A/// Target getrusage() function.
1380360SN/Atemplate <class OS>
13811450SN/ASyscallReturn
13823114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13832680Sktlim@umich.edu              ThreadContext *tc)
1384360SN/A{
13856701Sgblack@eecs.umich.edu    int index = 0;
13866701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13876701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1388360SN/A
13893670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13903670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1391360SN/A    rup->ru_stime.tv_sec = 0;
1392360SN/A    rup->ru_stime.tv_usec = 0;
1393360SN/A    rup->ru_maxrss = 0;
1394360SN/A    rup->ru_ixrss = 0;
1395360SN/A    rup->ru_idrss = 0;
1396360SN/A    rup->ru_isrss = 0;
1397360SN/A    rup->ru_minflt = 0;
1398360SN/A    rup->ru_majflt = 0;
1399360SN/A    rup->ru_nswap = 0;
1400360SN/A    rup->ru_inblock = 0;
1401360SN/A    rup->ru_oublock = 0;
1402360SN/A    rup->ru_msgsnd = 0;
1403360SN/A    rup->ru_msgrcv = 0;
1404360SN/A    rup->ru_nsignals = 0;
1405360SN/A    rup->ru_nvcsw = 0;
1406360SN/A    rup->ru_nivcsw = 0;
1407360SN/A
14083670Sbinkertn@umich.edu    switch (who) {
14093670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
14103670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14118737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14128737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14133670Sbinkertn@umich.edu        break;
14143670Sbinkertn@umich.edu
14153670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14163670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14173670Sbinkertn@umich.edu        break;
14183670Sbinkertn@umich.edu
14193670Sbinkertn@umich.edu      default:
14203670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14213670Sbinkertn@umich.edu        // plow ahead
14223670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14233670Sbinkertn@umich.edu             who);
14243670Sbinkertn@umich.edu    }
14253670Sbinkertn@umich.edu
14268706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1427360SN/A
14281458SN/A    return 0;
1429360SN/A}
1430360SN/A
14316683Stjones1@inf.ed.ac.uk/// Target times() function.
14326683Stjones1@inf.ed.ac.uktemplate <class OS>
14336683Stjones1@inf.ed.ac.ukSyscallReturn
14346683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14356683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14366683Stjones1@inf.ed.ac.uk{
14376701Sgblack@eecs.umich.edu    int index = 0;
14386701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14396683Stjones1@inf.ed.ac.uk
14406683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14417823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14426683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14436683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14446683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14456683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14466683Stjones1@inf.ed.ac.uk
14476683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14488737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14496683Stjones1@inf.ed.ac.uk
14506683Stjones1@inf.ed.ac.uk    // Write back
14518706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14526683Stjones1@inf.ed.ac.uk
14536683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14546683Stjones1@inf.ed.ac.uk    return clocks;
14556683Stjones1@inf.ed.ac.uk}
14562553SN/A
14576684Stjones1@inf.ed.ac.uk/// Target time() function.
14586684Stjones1@inf.ed.ac.uktemplate <class OS>
14596684Stjones1@inf.ed.ac.ukSyscallReturn
14606684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14616684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14626684Stjones1@inf.ed.ac.uk{
14636684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14646684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14656684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14666684Stjones1@inf.ed.ac.uk
14676701Sgblack@eecs.umich.edu    int index = 0;
14686701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14696684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14706684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14718737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14728852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14738852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14746684Stjones1@inf.ed.ac.uk    }
14756684Stjones1@inf.ed.ac.uk    return sec;
14766684Stjones1@inf.ed.ac.uk}
14772553SN/A
14782553SN/A
14791354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1480