syscall_emul.hh revision 10486
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        (provided_address % TheISA::PageBytes != 0)) {
8355877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8365877Shsul@eecs.umich.edu        return -EINVAL;
8375877Shsul@eecs.umich.edu    }
8385877Shsul@eecs.umich.edu
83910486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
84010486Stjablin@gmail.com
8415877Shsul@eecs.umich.edu    if (new_length > old_length) {
84210027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
84310027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8445877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8458601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8465877Shsul@eecs.umich.edu            process->mmap_end += diff;
8475877Shsul@eecs.umich.edu            return start;
8485877Shsul@eecs.umich.edu        } else {
84910027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8505877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8515877Shsul@eecs.umich.edu                return -ENOMEM;
8525877Shsul@eecs.umich.edu            } else {
85310027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
85410027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
85510027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
85610027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
85710027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
85810027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8595877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
86010027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
86110027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
86210027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
86310027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
86410027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
86510027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
86610027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
86710027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
86810027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
86910027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
87010027SChris.Adeniyi-Jones@arm.com                }
87110027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
87210027SChris.Adeniyi-Jones@arm.com                return new_start;
8735877Shsul@eecs.umich.edu            }
8745877Shsul@eecs.umich.edu        }
8755877Shsul@eecs.umich.edu    } else {
87610027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
87710027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8788601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
87910027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8805877Shsul@eecs.umich.edu    }
8815877Shsul@eecs.umich.edu}
8821999SN/A
883378SN/A/// Target stat() handler.
884360SN/Atemplate <class OS>
8851450SN/ASyscallReturn
8863114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8872680Sktlim@umich.edu         ThreadContext *tc)
888360SN/A{
889360SN/A    std::string path;
890360SN/A
8916701Sgblack@eecs.umich.edu    int index = 0;
8928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8936701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8946701Sgblack@eecs.umich.edu        return -EFAULT;
8956701Sgblack@eecs.umich.edu    }
8966701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
897360SN/A
8983669Sbinkertn@umich.edu    // Adjust path for current working directory
8993669Sbinkertn@umich.edu    path = process->fullPath(path);
9003669Sbinkertn@umich.edu
901360SN/A    struct stat hostBuf;
902360SN/A    int result = stat(path.c_str(), &hostBuf);
903360SN/A
904360SN/A    if (result < 0)
9052218SN/A        return -errno;
906360SN/A
9078706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
908360SN/A
9091458SN/A    return 0;
910360SN/A}
911360SN/A
912360SN/A
9135074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9145074Ssaidi@eecs.umich.edutemplate <class OS>
9155074Ssaidi@eecs.umich.eduSyscallReturn
9165074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9175074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9185074Ssaidi@eecs.umich.edu{
9195074Ssaidi@eecs.umich.edu    std::string path;
9205074Ssaidi@eecs.umich.edu
9216701Sgblack@eecs.umich.edu    int index = 0;
9228852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9236701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9245074Ssaidi@eecs.umich.edu        return -EFAULT;
9256701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9265074Ssaidi@eecs.umich.edu
9275074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9285074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9295074Ssaidi@eecs.umich.edu
9305208Ssaidi@eecs.umich.edu#if NO_STAT64
9315208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9325208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9335208Ssaidi@eecs.umich.edu#else
9345074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9355074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9365208Ssaidi@eecs.umich.edu#endif
9375074Ssaidi@eecs.umich.edu
9385074Ssaidi@eecs.umich.edu    if (result < 0)
9395074Ssaidi@eecs.umich.edu        return -errno;
9405074Ssaidi@eecs.umich.edu
9418706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9425074Ssaidi@eecs.umich.edu
9435074Ssaidi@eecs.umich.edu    return 0;
9445074Ssaidi@eecs.umich.edu}
9455074Ssaidi@eecs.umich.edu
9465074Ssaidi@eecs.umich.edu
94710027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
94810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
94910027SChris.Adeniyi-Jones@arm.comSyscallReturn
95010027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
95110027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
95210027SChris.Adeniyi-Jones@arm.com{
95310027SChris.Adeniyi-Jones@arm.com    int index = 0;
95410027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
95510027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
95610027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
95710027SChris.Adeniyi-Jones@arm.com
95810027SChris.Adeniyi-Jones@arm.com    std::string path;
95910027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
96010027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
96110027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
96210027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
96310027SChris.Adeniyi-Jones@arm.com
96410027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
96510027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
96610027SChris.Adeniyi-Jones@arm.com
96710027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
96810027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
96910027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
97010027SChris.Adeniyi-Jones@arm.com#else
97110027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
97210027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
97310027SChris.Adeniyi-Jones@arm.com#endif
97410027SChris.Adeniyi-Jones@arm.com
97510027SChris.Adeniyi-Jones@arm.com    if (result < 0)
97610027SChris.Adeniyi-Jones@arm.com        return -errno;
97710027SChris.Adeniyi-Jones@arm.com
97810027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
97910027SChris.Adeniyi-Jones@arm.com
98010027SChris.Adeniyi-Jones@arm.com    return 0;
98110027SChris.Adeniyi-Jones@arm.com}
98210027SChris.Adeniyi-Jones@arm.com
98310027SChris.Adeniyi-Jones@arm.com
9841999SN/A/// Target fstat64() handler.
9851999SN/Atemplate <class OS>
9861999SN/ASyscallReturn
9873114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9882680Sktlim@umich.edu            ThreadContext *tc)
9891999SN/A{
9906701Sgblack@eecs.umich.edu    int index = 0;
9916701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9926701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9931999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9941999SN/A        // doesn't map to any simulator fd: not a valid target fd
9951999SN/A        return -EBADF;
9961999SN/A    }
9971999SN/A
9982764Sstever@eecs.umich.edu#if NO_STAT64
9992064SN/A    struct stat  hostBuf;
10002064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
10012064SN/A#else
10022064SN/A    struct stat64  hostBuf;
10031999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
10042064SN/A#endif
10051999SN/A
10061999SN/A    if (result < 0)
10072218SN/A        return -errno;
10081999SN/A
10098706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10101999SN/A
10111999SN/A    return 0;
10121999SN/A}
10131999SN/A
10141999SN/A
1015378SN/A/// Target lstat() handler.
1016360SN/Atemplate <class OS>
10171450SN/ASyscallReturn
10183114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10192680Sktlim@umich.edu          ThreadContext *tc)
1020360SN/A{
1021360SN/A    std::string path;
1022360SN/A
10236701Sgblack@eecs.umich.edu    int index = 0;
10248852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10256701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10266701Sgblack@eecs.umich.edu        return -EFAULT;
10276701Sgblack@eecs.umich.edu    }
10286701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1029360SN/A
10303669Sbinkertn@umich.edu    // Adjust path for current working directory
10313669Sbinkertn@umich.edu    path = process->fullPath(path);
10323669Sbinkertn@umich.edu
1033360SN/A    struct stat hostBuf;
1034360SN/A    int result = lstat(path.c_str(), &hostBuf);
1035360SN/A
1036360SN/A    if (result < 0)
10371458SN/A        return -errno;
1038360SN/A
10398706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1040360SN/A
10411458SN/A    return 0;
1042360SN/A}
1043360SN/A
10441999SN/A/// Target lstat64() handler.
10451999SN/Atemplate <class OS>
10461999SN/ASyscallReturn
10473114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10482680Sktlim@umich.edu            ThreadContext *tc)
10491999SN/A{
10501999SN/A    std::string path;
10511999SN/A
10526701Sgblack@eecs.umich.edu    int index = 0;
10538852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10546701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10556701Sgblack@eecs.umich.edu        return -EFAULT;
10566701Sgblack@eecs.umich.edu    }
10576701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10581999SN/A
10593669Sbinkertn@umich.edu    // Adjust path for current working directory
10603669Sbinkertn@umich.edu    path = process->fullPath(path);
10613669Sbinkertn@umich.edu
10622764Sstever@eecs.umich.edu#if NO_STAT64
10632064SN/A    struct stat hostBuf;
10642064SN/A    int result = lstat(path.c_str(), &hostBuf);
10652064SN/A#else
10661999SN/A    struct stat64 hostBuf;
10671999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10682064SN/A#endif
10691999SN/A
10701999SN/A    if (result < 0)
10711999SN/A        return -errno;
10721999SN/A
10738706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10741999SN/A
10751999SN/A    return 0;
10761999SN/A}
10771999SN/A
1078378SN/A/// Target fstat() handler.
1079360SN/Atemplate <class OS>
10801450SN/ASyscallReturn
10813114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10822680Sktlim@umich.edu          ThreadContext *tc)
1083360SN/A{
10846701Sgblack@eecs.umich.edu    int index = 0;
10856701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10866701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1087360SN/A
10881969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1089360SN/A
1090360SN/A    if (fd < 0)
10911458SN/A        return -EBADF;
1092360SN/A
1093360SN/A    struct stat hostBuf;
1094360SN/A    int result = fstat(fd, &hostBuf);
1095360SN/A
1096360SN/A    if (result < 0)
10971458SN/A        return -errno;
1098360SN/A
10998706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
11002021SN/A
11011458SN/A    return 0;
1102360SN/A}
1103360SN/A
1104360SN/A
11051706SN/A/// Target statfs() handler.
11061706SN/Atemplate <class OS>
11071706SN/ASyscallReturn
11083114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11092680Sktlim@umich.edu           ThreadContext *tc)
11101706SN/A{
11111706SN/A    std::string path;
11121706SN/A
11136701Sgblack@eecs.umich.edu    int index = 0;
11148852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11156701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11166701Sgblack@eecs.umich.edu        return -EFAULT;
11176701Sgblack@eecs.umich.edu    }
11186701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11191706SN/A
11203669Sbinkertn@umich.edu    // Adjust path for current working directory
11213669Sbinkertn@umich.edu    path = process->fullPath(path);
11223669Sbinkertn@umich.edu
11231706SN/A    struct statfs hostBuf;
11241706SN/A    int result = statfs(path.c_str(), &hostBuf);
11251706SN/A
11261706SN/A    if (result < 0)
11272218SN/A        return -errno;
11281706SN/A
11298706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11301706SN/A
11311706SN/A    return 0;
11321706SN/A}
11331706SN/A
11341706SN/A
11351706SN/A/// Target fstatfs() handler.
11361706SN/Atemplate <class OS>
11371706SN/ASyscallReturn
11383114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11392680Sktlim@umich.edu            ThreadContext *tc)
11401706SN/A{
11416701Sgblack@eecs.umich.edu    int index = 0;
11426701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11436701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11441706SN/A
11451706SN/A    if (fd < 0)
11461706SN/A        return -EBADF;
11471706SN/A
11481706SN/A    struct statfs hostBuf;
11491706SN/A    int result = fstatfs(fd, &hostBuf);
11501706SN/A
11511706SN/A    if (result < 0)
11522218SN/A        return -errno;
11531706SN/A
11548706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11551706SN/A
11561706SN/A    return 0;
11571706SN/A}
11581706SN/A
11591706SN/A
11601999SN/A/// Target writev() handler.
11611999SN/Atemplate <class OS>
11621999SN/ASyscallReturn
11633114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11642680Sktlim@umich.edu           ThreadContext *tc)
11651999SN/A{
11666701Sgblack@eecs.umich.edu    int index = 0;
11676701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11681999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11691999SN/A        // doesn't map to any simulator fd: not a valid target fd
11701999SN/A        return -EBADF;
11711999SN/A    }
11721999SN/A
11738852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11746701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11756701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11761999SN/A    struct iovec hiov[count];
11776227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11781999SN/A        typename OS::tgt_iovec tiov;
11792461SN/A
11808852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11818852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11828737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11831999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11848852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11858852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11861999SN/A    }
11871999SN/A
11881999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11891999SN/A
11906227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11911999SN/A        delete [] (char *)hiov[i].iov_base;
11921999SN/A
11931999SN/A    if (result < 0)
11942218SN/A        return -errno;
11951999SN/A
11961999SN/A    return 0;
11971999SN/A}
11981999SN/A
11991999SN/A
1200378SN/A/// Target mmap() handler.
1201378SN/A///
1202378SN/A/// We don't really handle mmap().  If the target is mmaping an
1203378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1204378SN/A/// nothing (since memory is initialized to zero and the simulator
12058324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
12068324Ssteve.reinhardt@amd.com///
1207360SN/Atemplate <class OS>
12081450SN/ASyscallReturn
12093114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1210360SN/A{
12116701Sgblack@eecs.umich.edu    int index = 0;
12126701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12136701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
12146701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
12156701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
12168324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
121710486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1218360SN/A
12199008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
12209008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12219008Sgblack@eecs.umich.edu
12228324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
12238324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
12248324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
12258324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12268324Ssteve.reinhardt@amd.com            return -EBADF;
12278324Ssteve.reinhardt@amd.com        }
12288324Ssteve.reinhardt@amd.com
12298324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
12308324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12318324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12328324Ssteve.reinhardt@amd.com            // another name on some platform
12338324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
12348324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
12358324Ssteve.reinhardt@amd.com        }
12368324Ssteve.reinhardt@amd.com    }
12375877Shsul@eecs.umich.edu
123810486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
123910486Stjablin@gmail.com
124010318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
124110486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12422544SN/A        warn("mmap failing: arguments not page-aligned: "
124310486Stjablin@gmail.com             "start 0x%x offset 0x%x",
124410486Stjablin@gmail.com             start, offset);
12452544SN/A        return -EINVAL;
1246360SN/A    }
1247360SN/A
12488600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12498600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12508600Ssteve.reinhardt@amd.com    bool clobber = false;
12518600Ssteve.reinhardt@amd.com
12528600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12538600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12548600Ssteve.reinhardt@amd.com
12558600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12568600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12578600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12588600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12598600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12608600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12618600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
126210485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
126310485SMichael.Adler@intel.com                return -EINVAL;
12648600Ssteve.reinhardt@amd.com            } else {
12658600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12668600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12678600Ssteve.reinhardt@amd.com                use_provided_address = false;
12688600Ssteve.reinhardt@amd.com            }
12698600Ssteve.reinhardt@amd.com        }
12702544SN/A    }
12712544SN/A
12728600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12738600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12748600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12758600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12768600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12778600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12788600Ssteve.reinhardt@amd.com        } else {
12798600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12808600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12818600Ssteve.reinhardt@amd.com        }
12826672Sgblack@eecs.umich.edu    }
12838600Ssteve.reinhardt@amd.com
12848601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12852544SN/A
12861458SN/A    return start;
1287360SN/A}
1288360SN/A
1289378SN/A/// Target getrlimit() handler.
1290360SN/Atemplate <class OS>
12911450SN/ASyscallReturn
12923114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12932680Sktlim@umich.edu        ThreadContext *tc)
1294360SN/A{
12956701Sgblack@eecs.umich.edu    int index = 0;
12966701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12976701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1298360SN/A
1299360SN/A    switch (resource) {
13002064SN/A        case OS::TGT_RLIMIT_STACK:
13015877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
13022064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
13038737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13048737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13052064SN/A            break;
1306360SN/A
13075877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
13085877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
13095877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
13108737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13118737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13125877Shsul@eecs.umich.edu            break;
13135877Shsul@eecs.umich.edu
13142064SN/A        default:
13152064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
13162064SN/A                << std::endl;
13172064SN/A            abort();
13182064SN/A            break;
1319360SN/A    }
1320360SN/A
13218706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13221458SN/A    return 0;
1323360SN/A}
1324360SN/A
1325378SN/A/// Target gettimeofday() handler.
1326360SN/Atemplate <class OS>
13271450SN/ASyscallReturn
13283114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13292680Sktlim@umich.edu        ThreadContext *tc)
1330360SN/A{
13316701Sgblack@eecs.umich.edu    int index = 0;
13326701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1333360SN/A
1334360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1335360SN/A    tp->tv_sec += seconds_since_epoch;
13366109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13376109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1338360SN/A
13398706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1340360SN/A
13411458SN/A    return 0;
1342360SN/A}
1343360SN/A
1344360SN/A
13451999SN/A/// Target utimes() handler.
13461999SN/Atemplate <class OS>
13471999SN/ASyscallReturn
13483114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13492680Sktlim@umich.edu           ThreadContext *tc)
13501999SN/A{
13511999SN/A    std::string path;
13521999SN/A
13536701Sgblack@eecs.umich.edu    int index = 0;
13548852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13556701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13566701Sgblack@eecs.umich.edu        return -EFAULT;
13576701Sgblack@eecs.umich.edu    }
13581999SN/A
13596701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13606701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13618706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13621999SN/A
13631999SN/A    struct timeval hostTimeval[2];
13641999SN/A    for (int i = 0; i < 2; ++i)
13651999SN/A    {
13668737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13678737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13681999SN/A    }
13693669Sbinkertn@umich.edu
13703669Sbinkertn@umich.edu    // Adjust path for current working directory
13713669Sbinkertn@umich.edu    path = process->fullPath(path);
13723669Sbinkertn@umich.edu
13731999SN/A    int result = utimes(path.c_str(), hostTimeval);
13741999SN/A
13751999SN/A    if (result < 0)
13761999SN/A        return -errno;
13771999SN/A
13781999SN/A    return 0;
13791999SN/A}
1380378SN/A/// Target getrusage() function.
1381360SN/Atemplate <class OS>
13821450SN/ASyscallReturn
13833114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13842680Sktlim@umich.edu              ThreadContext *tc)
1385360SN/A{
13866701Sgblack@eecs.umich.edu    int index = 0;
13876701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13886701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1389360SN/A
13903670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13913670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1392360SN/A    rup->ru_stime.tv_sec = 0;
1393360SN/A    rup->ru_stime.tv_usec = 0;
1394360SN/A    rup->ru_maxrss = 0;
1395360SN/A    rup->ru_ixrss = 0;
1396360SN/A    rup->ru_idrss = 0;
1397360SN/A    rup->ru_isrss = 0;
1398360SN/A    rup->ru_minflt = 0;
1399360SN/A    rup->ru_majflt = 0;
1400360SN/A    rup->ru_nswap = 0;
1401360SN/A    rup->ru_inblock = 0;
1402360SN/A    rup->ru_oublock = 0;
1403360SN/A    rup->ru_msgsnd = 0;
1404360SN/A    rup->ru_msgrcv = 0;
1405360SN/A    rup->ru_nsignals = 0;
1406360SN/A    rup->ru_nvcsw = 0;
1407360SN/A    rup->ru_nivcsw = 0;
1408360SN/A
14093670Sbinkertn@umich.edu    switch (who) {
14103670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
14113670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14128737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14138737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14143670Sbinkertn@umich.edu        break;
14153670Sbinkertn@umich.edu
14163670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14173670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14183670Sbinkertn@umich.edu        break;
14193670Sbinkertn@umich.edu
14203670Sbinkertn@umich.edu      default:
14213670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14223670Sbinkertn@umich.edu        // plow ahead
14233670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14243670Sbinkertn@umich.edu             who);
14253670Sbinkertn@umich.edu    }
14263670Sbinkertn@umich.edu
14278706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1428360SN/A
14291458SN/A    return 0;
1430360SN/A}
1431360SN/A
14326683Stjones1@inf.ed.ac.uk/// Target times() function.
14336683Stjones1@inf.ed.ac.uktemplate <class OS>
14346683Stjones1@inf.ed.ac.ukSyscallReturn
14356683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14366683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14376683Stjones1@inf.ed.ac.uk{
14386701Sgblack@eecs.umich.edu    int index = 0;
14396701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14406683Stjones1@inf.ed.ac.uk
14416683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14427823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14436683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14446683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14456683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14466683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14476683Stjones1@inf.ed.ac.uk
14486683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14498737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14506683Stjones1@inf.ed.ac.uk
14516683Stjones1@inf.ed.ac.uk    // Write back
14528706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14536683Stjones1@inf.ed.ac.uk
14546683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14556683Stjones1@inf.ed.ac.uk    return clocks;
14566683Stjones1@inf.ed.ac.uk}
14572553SN/A
14586684Stjones1@inf.ed.ac.uk/// Target time() function.
14596684Stjones1@inf.ed.ac.uktemplate <class OS>
14606684Stjones1@inf.ed.ac.ukSyscallReturn
14616684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14626684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14636684Stjones1@inf.ed.ac.uk{
14646684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14656684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14666684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14676684Stjones1@inf.ed.ac.uk
14686701Sgblack@eecs.umich.edu    int index = 0;
14696701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14706684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14716684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14728737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14738852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14748852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14756684Stjones1@inf.ed.ac.uk    }
14766684Stjones1@inf.ed.ac.uk    return sec;
14776684Stjones1@inf.ed.ac.uk}
14782553SN/A
14792553SN/A
14801354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1481