syscall_emul.hh revision 10318
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
4422238SN/A
4432238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4442238SN/A/// returning a second value in a register other than the normal return register
4452238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4463114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4472238SN/A
4482238SN/A/// Target getpidPseudo() handler.
4492238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4503114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4512238SN/A
4522238SN/A/// Target getuidPseudo() handler.
4532238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4543114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4552238SN/A
4562238SN/A/// Target getgidPseudo() handler.
4572238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4583114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4592238SN/A
4602238SN/A
4611354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4621354SN/Aconst int one_million = 1000000;
4631354SN/A
4641354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4651354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4661354SN/A/// real-world time) to keep simulations repeatable.
4671354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4681354SN/A
4691354SN/A/// Helper function to convert current elapsed time to seconds and
4701354SN/A/// microseconds.
4711354SN/Atemplate <class T1, class T2>
4721354SN/Avoid
4731354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4741354SN/A{
4757823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4761354SN/A    sec = elapsed_usecs / one_million;
4771354SN/A    usec = elapsed_usecs % one_million;
4781354SN/A}
4791354SN/A
480360SN/A//////////////////////////////////////////////////////////////////////
481360SN/A//
482360SN/A// The following emulation functions are generic, but need to be
483360SN/A// templated to account for differences in types, constants, etc.
484360SN/A//
485360SN/A//////////////////////////////////////////////////////////////////////
486360SN/A
4873113Sgblack@eecs.umich.edu#if NO_STAT64
4883113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4893113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4903113Sgblack@eecs.umich.edu#else
4913113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4923113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4933113Sgblack@eecs.umich.edu#endif
4943113Sgblack@eecs.umich.edu
4953113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4963113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4973113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4983113Sgblack@eecs.umich.edu
4993113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
5003113Sgblack@eecs.umich.edustatic void
5013113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
5023113Sgblack@eecs.umich.edu{
5034189Sgblack@eecs.umich.edu    using namespace TheISA;
5044189Sgblack@eecs.umich.edu
5053113Sgblack@eecs.umich.edu    if (fakeTTY)
5063113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5073113Sgblack@eecs.umich.edu    else
5083113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5098737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5103113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5118737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5123277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5135515SMichael.Adler@intel.com    if (fakeTTY) {
5145515SMichael.Adler@intel.com        // Claim to be a character device
5155515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5165515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5175515SMichael.Adler@intel.com    }
5188737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5193277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5208737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5213277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5228737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5233277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5248737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5253113Sgblack@eecs.umich.edu    if (fakeTTY)
5263113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5273113Sgblack@eecs.umich.edu    else
5283113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5298737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5303113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5318737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5323114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5338737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5343114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5358737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5363114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5378737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5384061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5394061Sgblack@eecs.umich.edu    // consistently across different hosts.
5404061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5418737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5423113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5438737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5443113Sgblack@eecs.umich.edu}
5453113Sgblack@eecs.umich.edu
5463113Sgblack@eecs.umich.edu// Same for stat64
5473113Sgblack@eecs.umich.edu
5483113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5493113Sgblack@eecs.umich.edustatic void
5503113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5513113Sgblack@eecs.umich.edu{
5524189Sgblack@eecs.umich.edu    using namespace TheISA;
5534189Sgblack@eecs.umich.edu
5543113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5553113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5563113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5578737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5583113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5598737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5603113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5618737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5623113Sgblack@eecs.umich.edu#else
5633113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5643113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5653113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5663113Sgblack@eecs.umich.edu#endif
5673113Sgblack@eecs.umich.edu}
5683113Sgblack@eecs.umich.edu
5693113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5703113Sgblack@eecs.umich.edutemplate<class OS>
5713113Sgblack@eecs.umich.edustatic void
5728852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5733113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5743113Sgblack@eecs.umich.edu{
5753113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5763113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5773113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5783113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5793113Sgblack@eecs.umich.edu}
5803113Sgblack@eecs.umich.edu
5813113Sgblack@eecs.umich.edutemplate<class OS>
5823113Sgblack@eecs.umich.edustatic void
5838852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5843113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5853113Sgblack@eecs.umich.edu{
5863113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5873113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5886686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5893113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5903113Sgblack@eecs.umich.edu}
5913113Sgblack@eecs.umich.edu
592378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
593378SN/A/// only to find out if their stdout is a tty, to determine whether to
5949141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5959141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
596360SN/Atemplate <class OS>
5971450SN/ASyscallReturn
5983114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5992680Sktlim@umich.edu          ThreadContext *tc)
600360SN/A{
6016701Sgblack@eecs.umich.edu    int index = 0;
6026701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6036701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
604360SN/A
6051969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
606360SN/A
607360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
608360SN/A        // doesn't map to any simulator fd: not a valid target fd
6091458SN/A        return -EBADF;
610360SN/A    }
611360SN/A
6129141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
6131458SN/A        return -ENOTTY;
6149141Smarc.orr@gmail.com    }
615360SN/A
6169141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
6179141Smarc.orr@gmail.com         fd, req, tc->pcState());
6189141Smarc.orr@gmail.com    return -ENOTTY;
619360SN/A}
620360SN/A
621360SN/Atemplate <class OS>
62210027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
6233114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
62410027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
625360SN/A{
626360SN/A    std::string path;
627360SN/A
6288852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6296701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6301458SN/A        return -EFAULT;
631360SN/A
632360SN/A    if (path == "/dev/sysdev0") {
633360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
634360SN/A        // We don't support it, so just punt.
6351706SN/A        warn("Ignoring open(%s, ...)\n", path);
6361458SN/A        return -ENOENT;
637360SN/A    }
638360SN/A
6396701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6406701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
641360SN/A    int hostFlags = 0;
642360SN/A
643360SN/A    // translate open flags
644360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
645360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
646360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
647360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
648360SN/A        }
649360SN/A    }
650360SN/A
651360SN/A    // any target flags left?
652360SN/A    if (tgtFlags != 0)
6531706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
654360SN/A
655360SN/A#ifdef __CYGWIN32__
656360SN/A    hostFlags |= O_BINARY;
657360SN/A#endif
658360SN/A
6593669Sbinkertn@umich.edu    // Adjust path for current working directory
6603669Sbinkertn@umich.edu    path = process->fullPath(path);
6613669Sbinkertn@umich.edu
6621706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6631706SN/A
6645795Ssaidi@eecs.umich.edu    int fd;
6659143Ssteve.reinhardt@amd.com    int local_errno;
6669142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6679142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6689143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6695795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6709143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6715795Ssaidi@eecs.umich.edu     } else {
6725795Ssaidi@eecs.umich.edu        // open the file
6735795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6749143Ssteve.reinhardt@amd.com        local_errno = errno;
6755795Ssaidi@eecs.umich.edu     }
676360SN/A
6779143Ssteve.reinhardt@amd.com    if (fd == -1)
6789143Ssteve.reinhardt@amd.com        return -local_errno;
6799143Ssteve.reinhardt@amd.com
6809143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
681360SN/A}
682360SN/A
68310027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
68410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
68510027SChris.Adeniyi-Jones@arm.comSyscallReturn
68610027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68710027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
68810027SChris.Adeniyi-Jones@arm.com{
68910027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
69010027SChris.Adeniyi-Jones@arm.com}
69110027SChris.Adeniyi-Jones@arm.com
69210027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
69310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
69410027SChris.Adeniyi-Jones@arm.comSyscallReturn
69510027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69610027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
69710027SChris.Adeniyi-Jones@arm.com{
69810027SChris.Adeniyi-Jones@arm.com    int index = 0;
69910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
70010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
70110027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
70210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
70310027SChris.Adeniyi-Jones@arm.com}
70410027SChris.Adeniyi-Jones@arm.com
70510203SAli.Saidi@ARM.com/// Target facessat() handler
70610203SAli.Saidi@ARM.comtemplate <class OS>
70710203SAli.Saidi@ARM.comSyscallReturn
70810203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70910203SAli.Saidi@ARM.com        ThreadContext *tc)
71010203SAli.Saidi@ARM.com{
71110203SAli.Saidi@ARM.com    int index = 0;
71210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71410203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
71510203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
71610203SAli.Saidi@ARM.com}
71710203SAli.Saidi@ARM.com
71810203SAli.Saidi@ARM.com/// Target readlinkat() handler
71910203SAli.Saidi@ARM.comtemplate <class OS>
72010203SAli.Saidi@ARM.comSyscallReturn
72110203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
72210203SAli.Saidi@ARM.com        ThreadContext *tc)
72310203SAli.Saidi@ARM.com{
72410203SAli.Saidi@ARM.com    int index = 0;
72510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
72610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
72710203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72810203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
72910203SAli.Saidi@ARM.com}
73010203SAli.Saidi@ARM.com
7316640Svince@csl.cornell.edu/// Target sysinfo() handler.
7326640Svince@csl.cornell.edutemplate <class OS>
7336640Svince@csl.cornell.eduSyscallReturn
7346640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7356640Svince@csl.cornell.edu         ThreadContext *tc)
7366640Svince@csl.cornell.edu{
7376640Svince@csl.cornell.edu
7386701Sgblack@eecs.umich.edu    int index = 0;
7396701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
7406701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
7416640Svince@csl.cornell.edu
7426701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7436701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7446640Svince@csl.cornell.edu
7458706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7466640Svince@csl.cornell.edu
7476701Sgblack@eecs.umich.edu    return 0;
7486640Svince@csl.cornell.edu}
749360SN/A
7501999SN/A/// Target chmod() handler.
7511999SN/Atemplate <class OS>
7521999SN/ASyscallReturn
7533114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7542680Sktlim@umich.edu          ThreadContext *tc)
7551999SN/A{
7561999SN/A    std::string path;
7571999SN/A
7586701Sgblack@eecs.umich.edu    int index = 0;
7598852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7606701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7611999SN/A        return -EFAULT;
7626701Sgblack@eecs.umich.edu    }
7631999SN/A
7646701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7651999SN/A    mode_t hostMode = 0;
7661999SN/A
7671999SN/A    // XXX translate mode flags via OS::something???
7681999SN/A    hostMode = mode;
7691999SN/A
7703669Sbinkertn@umich.edu    // Adjust path for current working directory
7713669Sbinkertn@umich.edu    path = process->fullPath(path);
7723669Sbinkertn@umich.edu
7731999SN/A    // do the chmod
7741999SN/A    int result = chmod(path.c_str(), hostMode);
7751999SN/A    if (result < 0)
7762218SN/A        return -errno;
7771999SN/A
7781999SN/A    return 0;
7791999SN/A}
7801999SN/A
7811999SN/A
7821999SN/A/// Target fchmod() handler.
7831999SN/Atemplate <class OS>
7841999SN/ASyscallReturn
7853114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7862680Sktlim@umich.edu           ThreadContext *tc)
7871999SN/A{
7886701Sgblack@eecs.umich.edu    int index = 0;
7896701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7901999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7911999SN/A        // doesn't map to any simulator fd: not a valid target fd
7921999SN/A        return -EBADF;
7931999SN/A    }
7941999SN/A
7956701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7961999SN/A    mode_t hostMode = 0;
7971999SN/A
7981999SN/A    // XXX translate mode flags via OS::someting???
7991999SN/A    hostMode = mode;
8001999SN/A
8011999SN/A    // do the fchmod
8021999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
8031999SN/A    if (result < 0)
8042218SN/A        return -errno;
8051999SN/A
8061999SN/A    return 0;
8071999SN/A}
8081999SN/A
8095877Shsul@eecs.umich.edu/// Target mremap() handler.
8105877Shsul@eecs.umich.edutemplate <class OS>
8115877Shsul@eecs.umich.eduSyscallReturn
8125877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8135877Shsul@eecs.umich.edu{
8146701Sgblack@eecs.umich.edu    int index = 0;
8156701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8166701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8176701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8186701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
81910027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
82010027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
82110027SChris.Adeniyi-Jones@arm.com
82210027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
82310027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8245877Shsul@eecs.umich.edu
82510318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
82610318Sandreas.hansson@arm.com        (new_length % TheISA::PageBytes != 0) ||
82710318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8285877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8295877Shsul@eecs.umich.edu        return -EINVAL;
8305877Shsul@eecs.umich.edu    }
8315877Shsul@eecs.umich.edu
8325877Shsul@eecs.umich.edu    if (new_length > old_length) {
83310027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
83410027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8355877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8368601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8375877Shsul@eecs.umich.edu            process->mmap_end += diff;
8385877Shsul@eecs.umich.edu            return start;
8395877Shsul@eecs.umich.edu        } else {
84010027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8415877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8425877Shsul@eecs.umich.edu                return -ENOMEM;
8435877Shsul@eecs.umich.edu            } else {
84410027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
84510027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
84610027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
84710027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
84810027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
84910027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8505877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
85110027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
85210027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
85310027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
85410027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
85510027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
85610027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
85710027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
85810027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
85910027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
86010027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
86110027SChris.Adeniyi-Jones@arm.com                }
86210027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
86310027SChris.Adeniyi-Jones@arm.com                return new_start;
8645877Shsul@eecs.umich.edu            }
8655877Shsul@eecs.umich.edu        }
8665877Shsul@eecs.umich.edu    } else {
86710027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
86810027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8698601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
87010027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8715877Shsul@eecs.umich.edu    }
8725877Shsul@eecs.umich.edu}
8731999SN/A
874378SN/A/// Target stat() handler.
875360SN/Atemplate <class OS>
8761450SN/ASyscallReturn
8773114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8782680Sktlim@umich.edu         ThreadContext *tc)
879360SN/A{
880360SN/A    std::string path;
881360SN/A
8826701Sgblack@eecs.umich.edu    int index = 0;
8838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8846701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8856701Sgblack@eecs.umich.edu        return -EFAULT;
8866701Sgblack@eecs.umich.edu    }
8876701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
888360SN/A
8893669Sbinkertn@umich.edu    // Adjust path for current working directory
8903669Sbinkertn@umich.edu    path = process->fullPath(path);
8913669Sbinkertn@umich.edu
892360SN/A    struct stat hostBuf;
893360SN/A    int result = stat(path.c_str(), &hostBuf);
894360SN/A
895360SN/A    if (result < 0)
8962218SN/A        return -errno;
897360SN/A
8988706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
899360SN/A
9001458SN/A    return 0;
901360SN/A}
902360SN/A
903360SN/A
9045074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9055074Ssaidi@eecs.umich.edutemplate <class OS>
9065074Ssaidi@eecs.umich.eduSyscallReturn
9075074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9085074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9095074Ssaidi@eecs.umich.edu{
9105074Ssaidi@eecs.umich.edu    std::string path;
9115074Ssaidi@eecs.umich.edu
9126701Sgblack@eecs.umich.edu    int index = 0;
9138852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9146701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9155074Ssaidi@eecs.umich.edu        return -EFAULT;
9166701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9175074Ssaidi@eecs.umich.edu
9185074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9195074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9205074Ssaidi@eecs.umich.edu
9215208Ssaidi@eecs.umich.edu#if NO_STAT64
9225208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9235208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9245208Ssaidi@eecs.umich.edu#else
9255074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9265074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9275208Ssaidi@eecs.umich.edu#endif
9285074Ssaidi@eecs.umich.edu
9295074Ssaidi@eecs.umich.edu    if (result < 0)
9305074Ssaidi@eecs.umich.edu        return -errno;
9315074Ssaidi@eecs.umich.edu
9328706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9335074Ssaidi@eecs.umich.edu
9345074Ssaidi@eecs.umich.edu    return 0;
9355074Ssaidi@eecs.umich.edu}
9365074Ssaidi@eecs.umich.edu
9375074Ssaidi@eecs.umich.edu
93810027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
93910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
94010027SChris.Adeniyi-Jones@arm.comSyscallReturn
94110027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
94210027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
94310027SChris.Adeniyi-Jones@arm.com{
94410027SChris.Adeniyi-Jones@arm.com    int index = 0;
94510027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
94610027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
94710027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
94810027SChris.Adeniyi-Jones@arm.com
94910027SChris.Adeniyi-Jones@arm.com    std::string path;
95010027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
95110027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
95210027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
95310027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
95410027SChris.Adeniyi-Jones@arm.com
95510027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
95610027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
95710027SChris.Adeniyi-Jones@arm.com
95810027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
95910027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
96010027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
96110027SChris.Adeniyi-Jones@arm.com#else
96210027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
96310027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
96410027SChris.Adeniyi-Jones@arm.com#endif
96510027SChris.Adeniyi-Jones@arm.com
96610027SChris.Adeniyi-Jones@arm.com    if (result < 0)
96710027SChris.Adeniyi-Jones@arm.com        return -errno;
96810027SChris.Adeniyi-Jones@arm.com
96910027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
97010027SChris.Adeniyi-Jones@arm.com
97110027SChris.Adeniyi-Jones@arm.com    return 0;
97210027SChris.Adeniyi-Jones@arm.com}
97310027SChris.Adeniyi-Jones@arm.com
97410027SChris.Adeniyi-Jones@arm.com
9751999SN/A/// Target fstat64() handler.
9761999SN/Atemplate <class OS>
9771999SN/ASyscallReturn
9783114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9792680Sktlim@umich.edu            ThreadContext *tc)
9801999SN/A{
9816701Sgblack@eecs.umich.edu    int index = 0;
9826701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9836701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9841999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9851999SN/A        // doesn't map to any simulator fd: not a valid target fd
9861999SN/A        return -EBADF;
9871999SN/A    }
9881999SN/A
9892764Sstever@eecs.umich.edu#if NO_STAT64
9902064SN/A    struct stat  hostBuf;
9912064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
9922064SN/A#else
9932064SN/A    struct stat64  hostBuf;
9941999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
9952064SN/A#endif
9961999SN/A
9971999SN/A    if (result < 0)
9982218SN/A        return -errno;
9991999SN/A
10008706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10011999SN/A
10021999SN/A    return 0;
10031999SN/A}
10041999SN/A
10051999SN/A
1006378SN/A/// Target lstat() handler.
1007360SN/Atemplate <class OS>
10081450SN/ASyscallReturn
10093114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10102680Sktlim@umich.edu          ThreadContext *tc)
1011360SN/A{
1012360SN/A    std::string path;
1013360SN/A
10146701Sgblack@eecs.umich.edu    int index = 0;
10158852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10166701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10176701Sgblack@eecs.umich.edu        return -EFAULT;
10186701Sgblack@eecs.umich.edu    }
10196701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1020360SN/A
10213669Sbinkertn@umich.edu    // Adjust path for current working directory
10223669Sbinkertn@umich.edu    path = process->fullPath(path);
10233669Sbinkertn@umich.edu
1024360SN/A    struct stat hostBuf;
1025360SN/A    int result = lstat(path.c_str(), &hostBuf);
1026360SN/A
1027360SN/A    if (result < 0)
10281458SN/A        return -errno;
1029360SN/A
10308706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1031360SN/A
10321458SN/A    return 0;
1033360SN/A}
1034360SN/A
10351999SN/A/// Target lstat64() handler.
10361999SN/Atemplate <class OS>
10371999SN/ASyscallReturn
10383114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10392680Sktlim@umich.edu            ThreadContext *tc)
10401999SN/A{
10411999SN/A    std::string path;
10421999SN/A
10436701Sgblack@eecs.umich.edu    int index = 0;
10448852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10456701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10466701Sgblack@eecs.umich.edu        return -EFAULT;
10476701Sgblack@eecs.umich.edu    }
10486701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10491999SN/A
10503669Sbinkertn@umich.edu    // Adjust path for current working directory
10513669Sbinkertn@umich.edu    path = process->fullPath(path);
10523669Sbinkertn@umich.edu
10532764Sstever@eecs.umich.edu#if NO_STAT64
10542064SN/A    struct stat hostBuf;
10552064SN/A    int result = lstat(path.c_str(), &hostBuf);
10562064SN/A#else
10571999SN/A    struct stat64 hostBuf;
10581999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10592064SN/A#endif
10601999SN/A
10611999SN/A    if (result < 0)
10621999SN/A        return -errno;
10631999SN/A
10648706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10651999SN/A
10661999SN/A    return 0;
10671999SN/A}
10681999SN/A
1069378SN/A/// Target fstat() handler.
1070360SN/Atemplate <class OS>
10711450SN/ASyscallReturn
10723114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10732680Sktlim@umich.edu          ThreadContext *tc)
1074360SN/A{
10756701Sgblack@eecs.umich.edu    int index = 0;
10766701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10776701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1078360SN/A
10791969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1080360SN/A
1081360SN/A    if (fd < 0)
10821458SN/A        return -EBADF;
1083360SN/A
1084360SN/A    struct stat hostBuf;
1085360SN/A    int result = fstat(fd, &hostBuf);
1086360SN/A
1087360SN/A    if (result < 0)
10881458SN/A        return -errno;
1089360SN/A
10908706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10912021SN/A
10921458SN/A    return 0;
1093360SN/A}
1094360SN/A
1095360SN/A
10961706SN/A/// Target statfs() handler.
10971706SN/Atemplate <class OS>
10981706SN/ASyscallReturn
10993114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11002680Sktlim@umich.edu           ThreadContext *tc)
11011706SN/A{
11021706SN/A    std::string path;
11031706SN/A
11046701Sgblack@eecs.umich.edu    int index = 0;
11058852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11066701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11076701Sgblack@eecs.umich.edu        return -EFAULT;
11086701Sgblack@eecs.umich.edu    }
11096701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11101706SN/A
11113669Sbinkertn@umich.edu    // Adjust path for current working directory
11123669Sbinkertn@umich.edu    path = process->fullPath(path);
11133669Sbinkertn@umich.edu
11141706SN/A    struct statfs hostBuf;
11151706SN/A    int result = statfs(path.c_str(), &hostBuf);
11161706SN/A
11171706SN/A    if (result < 0)
11182218SN/A        return -errno;
11191706SN/A
11208706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11211706SN/A
11221706SN/A    return 0;
11231706SN/A}
11241706SN/A
11251706SN/A
11261706SN/A/// Target fstatfs() handler.
11271706SN/Atemplate <class OS>
11281706SN/ASyscallReturn
11293114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11302680Sktlim@umich.edu            ThreadContext *tc)
11311706SN/A{
11326701Sgblack@eecs.umich.edu    int index = 0;
11336701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11346701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11351706SN/A
11361706SN/A    if (fd < 0)
11371706SN/A        return -EBADF;
11381706SN/A
11391706SN/A    struct statfs hostBuf;
11401706SN/A    int result = fstatfs(fd, &hostBuf);
11411706SN/A
11421706SN/A    if (result < 0)
11432218SN/A        return -errno;
11441706SN/A
11458706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11461706SN/A
11471706SN/A    return 0;
11481706SN/A}
11491706SN/A
11501706SN/A
11511999SN/A/// Target writev() handler.
11521999SN/Atemplate <class OS>
11531999SN/ASyscallReturn
11543114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11552680Sktlim@umich.edu           ThreadContext *tc)
11561999SN/A{
11576701Sgblack@eecs.umich.edu    int index = 0;
11586701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11591999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11601999SN/A        // doesn't map to any simulator fd: not a valid target fd
11611999SN/A        return -EBADF;
11621999SN/A    }
11631999SN/A
11648852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11656701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11666701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11671999SN/A    struct iovec hiov[count];
11686227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11691999SN/A        typename OS::tgt_iovec tiov;
11702461SN/A
11718852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11728852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11738737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11741999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11758852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11768852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11771999SN/A    }
11781999SN/A
11791999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11801999SN/A
11816227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11821999SN/A        delete [] (char *)hiov[i].iov_base;
11831999SN/A
11841999SN/A    if (result < 0)
11852218SN/A        return -errno;
11861999SN/A
11871999SN/A    return 0;
11881999SN/A}
11891999SN/A
11901999SN/A
1191378SN/A/// Target mmap() handler.
1192378SN/A///
1193378SN/A/// We don't really handle mmap().  If the target is mmaping an
1194378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1195378SN/A/// nothing (since memory is initialized to zero and the simulator
11968324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
11978324Ssteve.reinhardt@amd.com///
1198360SN/Atemplate <class OS>
11991450SN/ASyscallReturn
12003114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1201360SN/A{
12026701Sgblack@eecs.umich.edu    int index = 0;
12036701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12046701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
12056701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
12066701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
12078324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
12086701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1209360SN/A
12109008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
12119008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12129008Sgblack@eecs.umich.edu
12138324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
12148324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
12158324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
12168324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12178324Ssteve.reinhardt@amd.com            return -EBADF;
12188324Ssteve.reinhardt@amd.com        }
12198324Ssteve.reinhardt@amd.com
12208324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
12218324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12228324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12238324Ssteve.reinhardt@amd.com            // another name on some platform
12248324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
12258324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
12268324Ssteve.reinhardt@amd.com        }
12278324Ssteve.reinhardt@amd.com    }
12285877Shsul@eecs.umich.edu
122910318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
123010318Sandreas.hansson@arm.com        (length % TheISA::PageBytes) != 0) {
12312544SN/A        warn("mmap failing: arguments not page-aligned: "
12322544SN/A             "start 0x%x length 0x%x",
12332544SN/A             start, length);
12342544SN/A        return -EINVAL;
1235360SN/A    }
1236360SN/A
12378600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12388600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12398600Ssteve.reinhardt@amd.com    bool clobber = false;
12408600Ssteve.reinhardt@amd.com
12418600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12428600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12438600Ssteve.reinhardt@amd.com
12448600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12458600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12468600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12478600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12488600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12498600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12508600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
12518600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
12528600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
12538600Ssteve.reinhardt@amd.com                     start);
12548600Ssteve.reinhardt@amd.com                clobber = true;
12558600Ssteve.reinhardt@amd.com            } else {
12568600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12578600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12588600Ssteve.reinhardt@amd.com                use_provided_address = false;
12598600Ssteve.reinhardt@amd.com            }
12608600Ssteve.reinhardt@amd.com        }
12612544SN/A    }
12622544SN/A
12638600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12648600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12658600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12668600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12678600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12688600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12698600Ssteve.reinhardt@amd.com        } else {
12708600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12718600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12728600Ssteve.reinhardt@amd.com        }
12736672Sgblack@eecs.umich.edu    }
12748600Ssteve.reinhardt@amd.com
12758601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12762544SN/A
12771458SN/A    return start;
1278360SN/A}
1279360SN/A
1280378SN/A/// Target getrlimit() handler.
1281360SN/Atemplate <class OS>
12821450SN/ASyscallReturn
12833114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12842680Sktlim@umich.edu        ThreadContext *tc)
1285360SN/A{
12866701Sgblack@eecs.umich.edu    int index = 0;
12876701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12886701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1289360SN/A
1290360SN/A    switch (resource) {
12912064SN/A        case OS::TGT_RLIMIT_STACK:
12925877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
12932064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
12948737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12958737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12962064SN/A            break;
1297360SN/A
12985877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
12995877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
13005877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
13018737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13028737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13035877Shsul@eecs.umich.edu            break;
13045877Shsul@eecs.umich.edu
13052064SN/A        default:
13062064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
13072064SN/A                << std::endl;
13082064SN/A            abort();
13092064SN/A            break;
1310360SN/A    }
1311360SN/A
13128706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13131458SN/A    return 0;
1314360SN/A}
1315360SN/A
1316378SN/A/// Target gettimeofday() handler.
1317360SN/Atemplate <class OS>
13181450SN/ASyscallReturn
13193114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13202680Sktlim@umich.edu        ThreadContext *tc)
1321360SN/A{
13226701Sgblack@eecs.umich.edu    int index = 0;
13236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1324360SN/A
1325360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1326360SN/A    tp->tv_sec += seconds_since_epoch;
13276109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13286109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1329360SN/A
13308706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1331360SN/A
13321458SN/A    return 0;
1333360SN/A}
1334360SN/A
1335360SN/A
13361999SN/A/// Target utimes() handler.
13371999SN/Atemplate <class OS>
13381999SN/ASyscallReturn
13393114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13402680Sktlim@umich.edu           ThreadContext *tc)
13411999SN/A{
13421999SN/A    std::string path;
13431999SN/A
13446701Sgblack@eecs.umich.edu    int index = 0;
13458852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13466701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13476701Sgblack@eecs.umich.edu        return -EFAULT;
13486701Sgblack@eecs.umich.edu    }
13491999SN/A
13506701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13516701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13528706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13531999SN/A
13541999SN/A    struct timeval hostTimeval[2];
13551999SN/A    for (int i = 0; i < 2; ++i)
13561999SN/A    {
13578737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13588737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13591999SN/A    }
13603669Sbinkertn@umich.edu
13613669Sbinkertn@umich.edu    // Adjust path for current working directory
13623669Sbinkertn@umich.edu    path = process->fullPath(path);
13633669Sbinkertn@umich.edu
13641999SN/A    int result = utimes(path.c_str(), hostTimeval);
13651999SN/A
13661999SN/A    if (result < 0)
13671999SN/A        return -errno;
13681999SN/A
13691999SN/A    return 0;
13701999SN/A}
1371378SN/A/// Target getrusage() function.
1372360SN/Atemplate <class OS>
13731450SN/ASyscallReturn
13743114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13752680Sktlim@umich.edu              ThreadContext *tc)
1376360SN/A{
13776701Sgblack@eecs.umich.edu    int index = 0;
13786701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13796701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1380360SN/A
13813670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13823670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1383360SN/A    rup->ru_stime.tv_sec = 0;
1384360SN/A    rup->ru_stime.tv_usec = 0;
1385360SN/A    rup->ru_maxrss = 0;
1386360SN/A    rup->ru_ixrss = 0;
1387360SN/A    rup->ru_idrss = 0;
1388360SN/A    rup->ru_isrss = 0;
1389360SN/A    rup->ru_minflt = 0;
1390360SN/A    rup->ru_majflt = 0;
1391360SN/A    rup->ru_nswap = 0;
1392360SN/A    rup->ru_inblock = 0;
1393360SN/A    rup->ru_oublock = 0;
1394360SN/A    rup->ru_msgsnd = 0;
1395360SN/A    rup->ru_msgrcv = 0;
1396360SN/A    rup->ru_nsignals = 0;
1397360SN/A    rup->ru_nvcsw = 0;
1398360SN/A    rup->ru_nivcsw = 0;
1399360SN/A
14003670Sbinkertn@umich.edu    switch (who) {
14013670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
14023670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14038737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14048737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14053670Sbinkertn@umich.edu        break;
14063670Sbinkertn@umich.edu
14073670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14083670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14093670Sbinkertn@umich.edu        break;
14103670Sbinkertn@umich.edu
14113670Sbinkertn@umich.edu      default:
14123670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14133670Sbinkertn@umich.edu        // plow ahead
14143670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14153670Sbinkertn@umich.edu             who);
14163670Sbinkertn@umich.edu    }
14173670Sbinkertn@umich.edu
14188706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1419360SN/A
14201458SN/A    return 0;
1421360SN/A}
1422360SN/A
14236683Stjones1@inf.ed.ac.uk/// Target times() function.
14246683Stjones1@inf.ed.ac.uktemplate <class OS>
14256683Stjones1@inf.ed.ac.ukSyscallReturn
14266683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14276683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14286683Stjones1@inf.ed.ac.uk{
14296701Sgblack@eecs.umich.edu    int index = 0;
14306701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14316683Stjones1@inf.ed.ac.uk
14326683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14337823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14346683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14356683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14366683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14376683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14386683Stjones1@inf.ed.ac.uk
14396683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14408737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14416683Stjones1@inf.ed.ac.uk
14426683Stjones1@inf.ed.ac.uk    // Write back
14438706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14446683Stjones1@inf.ed.ac.uk
14456683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14466683Stjones1@inf.ed.ac.uk    return clocks;
14476683Stjones1@inf.ed.ac.uk}
14482553SN/A
14496684Stjones1@inf.ed.ac.uk/// Target time() function.
14506684Stjones1@inf.ed.ac.uktemplate <class OS>
14516684Stjones1@inf.ed.ac.ukSyscallReturn
14526684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14536684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14546684Stjones1@inf.ed.ac.uk{
14556684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14566684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14576684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14586684Stjones1@inf.ed.ac.uk
14596701Sgblack@eecs.umich.edu    int index = 0;
14606701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14616684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14626684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14638737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14648852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14658852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14666684Stjones1@inf.ed.ac.uk    }
14676684Stjones1@inf.ed.ac.uk    return sec;
14686684Stjones1@inf.ed.ac.uk}
14692553SN/A
14702553SN/A
14711354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1472