syscall_emul.hh revision 10496
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"
8010496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
818229Snate@binkert.org#include "sim/process.hh"
828766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
836640Svince@csl.cornell.edu#include "sim/system.hh"
84360SN/A
85360SN/A///
86360SN/A/// System call descriptor.
87360SN/A///
88360SN/Aclass SyscallDesc {
89360SN/A
90360SN/A  public:
91360SN/A
92378SN/A    /// Typedef for target syscall handler functions.
931450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
943114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
95360SN/A
965543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
975543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
985543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
99360SN/A
100360SN/A    /// Flag values for controlling syscall behavior.
101360SN/A    enum Flags {
102360SN/A        /// Don't set return regs according to funcPtr return value.
103360SN/A        /// Used for syscalls with non-standard return conventions
1042680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
105360SN/A        /// sigreturn).
106360SN/A        SuppressReturnValue = 1
107360SN/A    };
108360SN/A
109360SN/A    /// Constructor.
110360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
111360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
112360SN/A    {
113360SN/A    }
114360SN/A
115360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1163114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
117360SN/A};
118360SN/A
119360SN/A
120360SN/Aclass BaseBufferArg {
121360SN/A
122360SN/A  public:
123360SN/A
124360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
125360SN/A    {
126360SN/A        bufPtr = new uint8_t[size];
127360SN/A        // clear out buffer: in case we only partially populate this,
128360SN/A        // and then do a copyOut(), we want to make sure we don't
129360SN/A        // introduce any random junk into the simulated address space
130360SN/A        memset(bufPtr, 0, size);
131360SN/A    }
132360SN/A
133360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
134360SN/A
135360SN/A    //
136360SN/A    // copy data into simulator space (read from target memory)
137360SN/A    //
1388852Sandreas.hansson@arm.com    virtual bool copyIn(SETranslatingPortProxy &memproxy)
139360SN/A    {
1408852Sandreas.hansson@arm.com        memproxy.readBlob(addr, bufPtr, size);
1415543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
142360SN/A    }
143360SN/A
144360SN/A    //
145360SN/A    // copy data out of simulator space (write to target memory)
146360SN/A    //
1478852Sandreas.hansson@arm.com    virtual bool copyOut(SETranslatingPortProxy &memproxy)
148360SN/A    {
1498852Sandreas.hansson@arm.com        memproxy.writeBlob(addr, bufPtr, size);
1505543Ssaidi@eecs.umich.edu        return true;    // no EFAULT detection for now
151360SN/A    }
152360SN/A
153360SN/A  protected:
154360SN/A    Addr addr;
155360SN/A    int size;
156360SN/A    uint8_t *bufPtr;
157360SN/A};
158360SN/A
159360SN/A
160360SN/Aclass BufferArg : public BaseBufferArg
161360SN/A{
162360SN/A  public:
163360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
1645543Ssaidi@eecs.umich.edu    void *bufferPtr()   { return bufPtr; }
165360SN/A};
166360SN/A
167360SN/Atemplate <class T>
168360SN/Aclass TypedBufferArg : public BaseBufferArg
169360SN/A{
170360SN/A  public:
171360SN/A    // user can optionally specify a specific number of bytes to
172360SN/A    // allocate to deal with those structs that have variable-size
173360SN/A    // arrays at the end
174360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
175360SN/A        : BaseBufferArg(_addr, _size)
176360SN/A    { }
177360SN/A
178360SN/A    // type case
179360SN/A    operator T*() { return (T *)bufPtr; }
180360SN/A
181360SN/A    // dereference operators
1825543Ssaidi@eecs.umich.edu    T &operator*()       { return *((T *)bufPtr); }
1835543Ssaidi@eecs.umich.edu    T* operator->()      { return (T *)bufPtr; }
184502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
185360SN/A};
186360SN/A
187360SN/A//////////////////////////////////////////////////////////////////////
188360SN/A//
189360SN/A// The following emulation functions are generic enough that they
190360SN/A// don't need to be recompiled for different emulated OS's.  They are
191360SN/A// defined in sim/syscall_emul.cc.
192360SN/A//
193360SN/A//////////////////////////////////////////////////////////////////////
194360SN/A
195360SN/A
196378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1971706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1983114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
199378SN/A
200378SN/A/// Handler for unimplemented syscalls that we never intend to
201378SN/A/// implement (signal handling, etc.) and should not affect the correct
202378SN/A/// behavior of the program.  Print a warning only if the appropriate
203378SN/A/// trace flag is enabled.  Return success to the target program.
2041706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
2053114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2068149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
2078149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
208360SN/A
2096109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
2101706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
2113114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
212378SN/A
2136109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
2146109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
2156109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
2166109Ssanchezd@stanford.edu
217378SN/A/// Target getpagesize() handler.
2181706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
2193114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
220378SN/A
2215748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
2225748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
2235748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
224378SN/A
225378SN/A/// Target close() handler.
2261706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
2273114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
228378SN/A
229378SN/A/// Target read() handler.
2301706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2313114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
232378SN/A
233378SN/A/// Target write() handler.
2341706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2353114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
236378SN/A
237378SN/A/// Target lseek() handler.
2381706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2393114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
240378SN/A
2414118Sgblack@eecs.umich.edu/// Target _llseek() handler.
2424118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
2434118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2444118Sgblack@eecs.umich.edu
245378SN/A/// Target munmap() handler.
2461706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2473114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
248378SN/A
249378SN/A/// Target gethostname() handler.
2501706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2513114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
252360SN/A
2535513SMichael.Adler@intel.com/// Target getcwd() handler.
2545513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2555513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2565513SMichael.Adler@intel.com
25710203SAli.Saidi@ARM.com/// Target readlink() handler.
25810203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
25910203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
26010203SAli.Saidi@ARM.com                           int index = 0);
2615513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2625513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2635513SMichael.Adler@intel.com
264511SN/A/// Target unlink() handler.
2651706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2663114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
267511SN/A
2685513SMichael.Adler@intel.com/// Target mkdir() handler.
2695513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2705513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2715513SMichael.Adler@intel.com
272511SN/A/// Target rename() handler.
2731706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2743114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2751706SN/A
2761706SN/A
2771706SN/A/// Target truncate() handler.
2781706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2793114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2801706SN/A
2811706SN/A
2821706SN/A/// Target ftruncate() handler.
2831706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2851706SN/A
286511SN/A
2876703Svince@csl.cornell.edu/// Target truncate64() handler.
2886703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2896703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2906703Svince@csl.cornell.edu
2916685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2926685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2936685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2946685Stjones1@inf.ed.ac.uk
2956685Stjones1@inf.ed.ac.uk
2965513SMichael.Adler@intel.com/// Target umask() handler.
2975513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2985513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2995513SMichael.Adler@intel.com
3005513SMichael.Adler@intel.com
3011999SN/A/// Target chown() handler.
3021999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
3033114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
3041999SN/A
3051999SN/A
3061999SN/A/// Target fchown() handler.
3071999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
3083114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
3091999SN/A
3103079Sstever@eecs.umich.edu/// Target dup() handler.
3113079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
3123114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
3133079Sstever@eecs.umich.edu
3142093SN/A/// Target fnctl() handler.
3152093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
3163114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3172093SN/A
3182687Sksewell@umich.edu/// Target fcntl64() handler.
3192687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3203114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3212687Sksewell@umich.edu
3222238SN/A/// Target setuid() handler.
3232238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3243114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3252238SN/A
3262238SN/A/// Target getpid() handler.
3272238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3283114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3292238SN/A
3302238SN/A/// Target getuid() handler.
3312238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3323114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3332238SN/A
3342238SN/A/// Target getgid() handler.
3352238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3363114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3372238SN/A
3382238SN/A/// Target getppid() handler.
3392238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3403114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3412238SN/A
3422238SN/A/// Target geteuid() handler.
3432238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3443114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3452238SN/A
3462238SN/A/// Target getegid() handler.
3472238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3483114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3492238SN/A
3506109Ssanchezd@stanford.edu/// Target clone() handler.
3516109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3526109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3532238SN/A
3549455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3559455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3569455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
35710203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
35810203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
35910203SAli.Saidi@ARM.com                               int index);
3609455Smitch.hayenga+gem5@gmail.com
3619112Smarc.orr@gmail.com/// Futex system call
3629112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3639112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3649112Smarc.orr@gmail.comtemplate <class OS>
3659112Smarc.orr@gmail.comSyscallReturn
3669112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3679112Smarc.orr@gmail.com          ThreadContext *tc)
3689112Smarc.orr@gmail.com{
3699112Smarc.orr@gmail.com    int index_uaddr = 0;
3709112Smarc.orr@gmail.com    int index_op = 1;
3719112Smarc.orr@gmail.com    int index_val = 2;
3729112Smarc.orr@gmail.com    int index_timeout = 3;
3739112Smarc.orr@gmail.com
3749112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3759112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3769112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3779112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3789112Smarc.orr@gmail.com
3799112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3809112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3819112Smarc.orr@gmail.com
3829112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3839112Smarc.orr@gmail.com            uaddr, op, val);
3849112Smarc.orr@gmail.com
3859238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3869112Smarc.orr@gmail.com
3879112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3889112Smarc.orr@gmail.com        if (timeout != 0) {
3899112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3909112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3919112Smarc.orr@gmail.com        }
3929112Smarc.orr@gmail.com
3939112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3949112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3959112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3969112Smarc.orr@gmail.com        delete buf;
3979112Smarc.orr@gmail.com
3989112Smarc.orr@gmail.com        if(val != mem_val) {
3999112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
4009112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
4019112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
4029112Smarc.orr@gmail.com        }
4039112Smarc.orr@gmail.com
4049112Smarc.orr@gmail.com        // Queue the thread context
4059112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
4069112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
4079112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4089112Smarc.orr@gmail.com        } else {
4099112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
4109112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
4119112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
4129112Smarc.orr@gmail.com        }
4139112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
4149112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
4159112Smarc.orr@gmail.com                                "thread context\n");
4169112Smarc.orr@gmail.com        tc->suspend();
4179112Smarc.orr@gmail.com        return 0;
4189112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
4199112Smarc.orr@gmail.com        int wokenUp = 0;
4209112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
4219112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
4229112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4239112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
4249112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4259112Smarc.orr@gmail.com                tcWaitList->pop_front();
4269112Smarc.orr@gmail.com                wokenUp++;
4279112Smarc.orr@gmail.com            }
4289112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4299112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4309112Smarc.orr@gmail.com                delete tcWaitList;
4319112Smarc.orr@gmail.com            }
4329112Smarc.orr@gmail.com        }
4339112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4349112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4359112Smarc.orr@gmail.com        return wokenUp;
4369112Smarc.orr@gmail.com    } else {
4379238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
4389112Smarc.orr@gmail.com        return 0;
4399112Smarc.orr@gmail.com    }
4409112Smarc.orr@gmail.com
4419112Smarc.orr@gmail.com}
4429112Smarc.orr@gmail.com
4432238SN/A
4442238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4452238SN/A/// returning a second value in a register other than the normal return register
4462238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4473114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4482238SN/A
4492238SN/A/// Target getpidPseudo() handler.
4502238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4513114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4522238SN/A
4532238SN/A/// Target getuidPseudo() handler.
4542238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4553114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4562238SN/A
4572238SN/A/// Target getgidPseudo() handler.
4582238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4593114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4602238SN/A
4612238SN/A
4621354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4631354SN/Aconst int one_million = 1000000;
4641354SN/A
4651354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4661354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4671354SN/A/// real-world time) to keep simulations repeatable.
4681354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4691354SN/A
4701354SN/A/// Helper function to convert current elapsed time to seconds and
4711354SN/A/// microseconds.
4721354SN/Atemplate <class T1, class T2>
4731354SN/Avoid
4741354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4751354SN/A{
4767823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4771354SN/A    sec = elapsed_usecs / one_million;
4781354SN/A    usec = elapsed_usecs % one_million;
4791354SN/A}
4801354SN/A
481360SN/A//////////////////////////////////////////////////////////////////////
482360SN/A//
483360SN/A// The following emulation functions are generic, but need to be
484360SN/A// templated to account for differences in types, constants, etc.
485360SN/A//
486360SN/A//////////////////////////////////////////////////////////////////////
487360SN/A
4883113Sgblack@eecs.umich.edu#if NO_STAT64
4893113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4903113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4913113Sgblack@eecs.umich.edu#else
4923113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4933113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4943113Sgblack@eecs.umich.edu#endif
4953113Sgblack@eecs.umich.edu
4963113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4973113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4983113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4993113Sgblack@eecs.umich.edu
5003113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
5013113Sgblack@eecs.umich.edustatic void
5023113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
5033113Sgblack@eecs.umich.edu{
5044189Sgblack@eecs.umich.edu    using namespace TheISA;
5054189Sgblack@eecs.umich.edu
5063113Sgblack@eecs.umich.edu    if (fakeTTY)
5073113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5083113Sgblack@eecs.umich.edu    else
5093113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5108737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5113113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5128737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5133277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5145515SMichael.Adler@intel.com    if (fakeTTY) {
5155515SMichael.Adler@intel.com        // Claim to be a character device
5165515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5175515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5185515SMichael.Adler@intel.com    }
5198737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5203277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5218737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5223277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5238737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5243277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5258737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5263113Sgblack@eecs.umich.edu    if (fakeTTY)
5273113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5283113Sgblack@eecs.umich.edu    else
5293113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5308737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5313113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5328737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5333114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5348737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5353114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5368737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5373114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5388737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5394061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5404061Sgblack@eecs.umich.edu    // consistently across different hosts.
5414061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5428737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5433113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5448737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5453113Sgblack@eecs.umich.edu}
5463113Sgblack@eecs.umich.edu
5473113Sgblack@eecs.umich.edu// Same for stat64
5483113Sgblack@eecs.umich.edu
5493113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5503113Sgblack@eecs.umich.edustatic void
5513113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5523113Sgblack@eecs.umich.edu{
5534189Sgblack@eecs.umich.edu    using namespace TheISA;
5544189Sgblack@eecs.umich.edu
5553113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5563113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5573113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5588737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5593113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5608737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5613113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5628737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5633113Sgblack@eecs.umich.edu#else
5643113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5653113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5663113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5673113Sgblack@eecs.umich.edu#endif
5683113Sgblack@eecs.umich.edu}
5693113Sgblack@eecs.umich.edu
5703113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5713113Sgblack@eecs.umich.edutemplate<class OS>
5723113Sgblack@eecs.umich.edustatic void
5738852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5743113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5753113Sgblack@eecs.umich.edu{
5763113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5773113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5783113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5793113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5803113Sgblack@eecs.umich.edu}
5813113Sgblack@eecs.umich.edu
5823113Sgblack@eecs.umich.edutemplate<class OS>
5833113Sgblack@eecs.umich.edustatic void
5848852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5853113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5863113Sgblack@eecs.umich.edu{
5873113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5883113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5896686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5903113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5913113Sgblack@eecs.umich.edu}
5923113Sgblack@eecs.umich.edu
593378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
594378SN/A/// only to find out if their stdout is a tty, to determine whether to
5959141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5969141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
597360SN/Atemplate <class OS>
5981450SN/ASyscallReturn
5993114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6002680Sktlim@umich.edu          ThreadContext *tc)
601360SN/A{
6026701Sgblack@eecs.umich.edu    int index = 0;
6036701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
6046701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
605360SN/A
6061969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
607360SN/A
60810496Ssteve.reinhardt@amd.com    Process::FdMap *fdObj = process->sim_fd_obj(fd);
60910496Ssteve.reinhardt@amd.com
61010496Ssteve.reinhardt@amd.com    if (fdObj == NULL) {
611360SN/A        // doesn't map to any simulator fd: not a valid target fd
6121458SN/A        return -EBADF;
613360SN/A    }
614360SN/A
61510496Ssteve.reinhardt@amd.com    if (fdObj->driver != NULL) {
61610496Ssteve.reinhardt@amd.com        return fdObj->driver->ioctl(process, tc, req);
61710496Ssteve.reinhardt@amd.com    }
61810496Ssteve.reinhardt@amd.com
6199141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
6201458SN/A        return -ENOTTY;
6219141Smarc.orr@gmail.com    }
622360SN/A
6239141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
6249141Smarc.orr@gmail.com         fd, req, tc->pcState());
6259141Smarc.orr@gmail.com    return -ENOTTY;
626360SN/A}
627360SN/A
628360SN/Atemplate <class OS>
62910027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
6303114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
63110027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
632360SN/A{
633360SN/A    std::string path;
634360SN/A
6358852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6366701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6371458SN/A        return -EFAULT;
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
66410496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
66510496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
66610496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
66710496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
66810496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
66910496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
67010496Ssteve.reinhardt@amd.com            return -ENOENT;
67110496Ssteve.reinhardt@amd.com        }
67210496Ssteve.reinhardt@amd.com
67310496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
67410496Ssteve.reinhardt@amd.com        if (drv != NULL) {
67510496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
67610496Ssteve.reinhardt@amd.com            // process if necessary.
67710496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
67810496Ssteve.reinhardt@amd.com        }
67910496Ssteve.reinhardt@amd.com
68010496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
68110496Ssteve.reinhardt@amd.com        // /dev/zero
68210496Ssteve.reinhardt@amd.com    }
68310496Ssteve.reinhardt@amd.com
6845795Ssaidi@eecs.umich.edu    int fd;
6859143Ssteve.reinhardt@amd.com    int local_errno;
6869142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6879142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6889143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6895795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6909143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6915795Ssaidi@eecs.umich.edu     } else {
6925795Ssaidi@eecs.umich.edu        // open the file
6935795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6949143Ssteve.reinhardt@amd.com        local_errno = errno;
6955795Ssaidi@eecs.umich.edu     }
696360SN/A
6979143Ssteve.reinhardt@amd.com    if (fd == -1)
6989143Ssteve.reinhardt@amd.com        return -local_errno;
6999143Ssteve.reinhardt@amd.com
7009143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
701360SN/A}
702360SN/A
70310027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
70410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
70510027SChris.Adeniyi-Jones@arm.comSyscallReturn
70610027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70710027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
70810027SChris.Adeniyi-Jones@arm.com{
70910027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
71010027SChris.Adeniyi-Jones@arm.com}
71110027SChris.Adeniyi-Jones@arm.com
71210027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
71310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
71410027SChris.Adeniyi-Jones@arm.comSyscallReturn
71510027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71610027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
71710027SChris.Adeniyi-Jones@arm.com{
71810027SChris.Adeniyi-Jones@arm.com    int index = 0;
71910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
72010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
72110027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
72310027SChris.Adeniyi-Jones@arm.com}
72410027SChris.Adeniyi-Jones@arm.com
72510203SAli.Saidi@ARM.com/// Target facessat() handler
72610203SAli.Saidi@ARM.comtemplate <class OS>
72710203SAli.Saidi@ARM.comSyscallReturn
72810203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
72910203SAli.Saidi@ARM.com        ThreadContext *tc)
73010203SAli.Saidi@ARM.com{
73110203SAli.Saidi@ARM.com    int index = 0;
73210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
73310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
73410203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
73510203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
73610203SAli.Saidi@ARM.com}
73710203SAli.Saidi@ARM.com
73810203SAli.Saidi@ARM.com/// Target readlinkat() handler
73910203SAli.Saidi@ARM.comtemplate <class OS>
74010203SAli.Saidi@ARM.comSyscallReturn
74110203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
74210203SAli.Saidi@ARM.com        ThreadContext *tc)
74310203SAli.Saidi@ARM.com{
74410203SAli.Saidi@ARM.com    int index = 0;
74510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
74610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
74710203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
74810203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
74910203SAli.Saidi@ARM.com}
75010203SAli.Saidi@ARM.com
7516640Svince@csl.cornell.edu/// Target sysinfo() handler.
7526640Svince@csl.cornell.edutemplate <class OS>
7536640Svince@csl.cornell.eduSyscallReturn
7546640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7556640Svince@csl.cornell.edu         ThreadContext *tc)
7566640Svince@csl.cornell.edu{
7576640Svince@csl.cornell.edu
7586701Sgblack@eecs.umich.edu    int index = 0;
7596701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
7606701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
7616640Svince@csl.cornell.edu
7626701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7636701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7646640Svince@csl.cornell.edu
7658706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7666640Svince@csl.cornell.edu
7676701Sgblack@eecs.umich.edu    return 0;
7686640Svince@csl.cornell.edu}
769360SN/A
7701999SN/A/// Target chmod() handler.
7711999SN/Atemplate <class OS>
7721999SN/ASyscallReturn
7733114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7742680Sktlim@umich.edu          ThreadContext *tc)
7751999SN/A{
7761999SN/A    std::string path;
7771999SN/A
7786701Sgblack@eecs.umich.edu    int index = 0;
7798852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7806701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7811999SN/A        return -EFAULT;
7826701Sgblack@eecs.umich.edu    }
7831999SN/A
7846701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7851999SN/A    mode_t hostMode = 0;
7861999SN/A
7871999SN/A    // XXX translate mode flags via OS::something???
7881999SN/A    hostMode = mode;
7891999SN/A
7903669Sbinkertn@umich.edu    // Adjust path for current working directory
7913669Sbinkertn@umich.edu    path = process->fullPath(path);
7923669Sbinkertn@umich.edu
7931999SN/A    // do the chmod
7941999SN/A    int result = chmod(path.c_str(), hostMode);
7951999SN/A    if (result < 0)
7962218SN/A        return -errno;
7971999SN/A
7981999SN/A    return 0;
7991999SN/A}
8001999SN/A
8011999SN/A
8021999SN/A/// Target fchmod() handler.
8031999SN/Atemplate <class OS>
8041999SN/ASyscallReturn
8053114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8062680Sktlim@umich.edu           ThreadContext *tc)
8071999SN/A{
8086701Sgblack@eecs.umich.edu    int index = 0;
8096701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
8101999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
8111999SN/A        // doesn't map to any simulator fd: not a valid target fd
8121999SN/A        return -EBADF;
8131999SN/A    }
8141999SN/A
8156701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8161999SN/A    mode_t hostMode = 0;
8171999SN/A
8181999SN/A    // XXX translate mode flags via OS::someting???
8191999SN/A    hostMode = mode;
8201999SN/A
8211999SN/A    // do the fchmod
8221999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
8231999SN/A    if (result < 0)
8242218SN/A        return -errno;
8251999SN/A
8261999SN/A    return 0;
8271999SN/A}
8281999SN/A
8295877Shsul@eecs.umich.edu/// Target mremap() handler.
8305877Shsul@eecs.umich.edutemplate <class OS>
8315877Shsul@eecs.umich.eduSyscallReturn
8325877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8335877Shsul@eecs.umich.edu{
8346701Sgblack@eecs.umich.edu    int index = 0;
8356701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8366701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8376701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8386701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
83910027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
84010027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
84110027SChris.Adeniyi-Jones@arm.com
84210027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
84310027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8445877Shsul@eecs.umich.edu
84510318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
84610318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8475877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8485877Shsul@eecs.umich.edu        return -EINVAL;
8495877Shsul@eecs.umich.edu    }
8505877Shsul@eecs.umich.edu
85110486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
85210486Stjablin@gmail.com
8535877Shsul@eecs.umich.edu    if (new_length > old_length) {
85410027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
85510027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8565877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8578601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8585877Shsul@eecs.umich.edu            process->mmap_end += diff;
8595877Shsul@eecs.umich.edu            return start;
8605877Shsul@eecs.umich.edu        } else {
86110027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8625877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8635877Shsul@eecs.umich.edu                return -ENOMEM;
8645877Shsul@eecs.umich.edu            } else {
86510027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
86610027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
86710027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
86810027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
86910027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
87010027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8715877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
87210027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
87310027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
87410027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
87510027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
87610027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
87710027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
87810027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
87910027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
88010027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
88110027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
88210027SChris.Adeniyi-Jones@arm.com                }
88310027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
88410027SChris.Adeniyi-Jones@arm.com                return new_start;
8855877Shsul@eecs.umich.edu            }
8865877Shsul@eecs.umich.edu        }
8875877Shsul@eecs.umich.edu    } else {
88810027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
88910027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8908601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
89110027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8925877Shsul@eecs.umich.edu    }
8935877Shsul@eecs.umich.edu}
8941999SN/A
895378SN/A/// Target stat() handler.
896360SN/Atemplate <class OS>
8971450SN/ASyscallReturn
8983114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8992680Sktlim@umich.edu         ThreadContext *tc)
900360SN/A{
901360SN/A    std::string path;
902360SN/A
9036701Sgblack@eecs.umich.edu    int index = 0;
9048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9056701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9066701Sgblack@eecs.umich.edu        return -EFAULT;
9076701Sgblack@eecs.umich.edu    }
9086701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
909360SN/A
9103669Sbinkertn@umich.edu    // Adjust path for current working directory
9113669Sbinkertn@umich.edu    path = process->fullPath(path);
9123669Sbinkertn@umich.edu
913360SN/A    struct stat hostBuf;
914360SN/A    int result = stat(path.c_str(), &hostBuf);
915360SN/A
916360SN/A    if (result < 0)
9172218SN/A        return -errno;
918360SN/A
9198706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
920360SN/A
9211458SN/A    return 0;
922360SN/A}
923360SN/A
924360SN/A
9255074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9265074Ssaidi@eecs.umich.edutemplate <class OS>
9275074Ssaidi@eecs.umich.eduSyscallReturn
9285074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9295074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9305074Ssaidi@eecs.umich.edu{
9315074Ssaidi@eecs.umich.edu    std::string path;
9325074Ssaidi@eecs.umich.edu
9336701Sgblack@eecs.umich.edu    int index = 0;
9348852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9356701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9365074Ssaidi@eecs.umich.edu        return -EFAULT;
9376701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9385074Ssaidi@eecs.umich.edu
9395074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9405074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9415074Ssaidi@eecs.umich.edu
9425208Ssaidi@eecs.umich.edu#if NO_STAT64
9435208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9445208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9455208Ssaidi@eecs.umich.edu#else
9465074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9475074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9485208Ssaidi@eecs.umich.edu#endif
9495074Ssaidi@eecs.umich.edu
9505074Ssaidi@eecs.umich.edu    if (result < 0)
9515074Ssaidi@eecs.umich.edu        return -errno;
9525074Ssaidi@eecs.umich.edu
9538706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9545074Ssaidi@eecs.umich.edu
9555074Ssaidi@eecs.umich.edu    return 0;
9565074Ssaidi@eecs.umich.edu}
9575074Ssaidi@eecs.umich.edu
9585074Ssaidi@eecs.umich.edu
95910027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
96010027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
96110027SChris.Adeniyi-Jones@arm.comSyscallReturn
96210027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
96310027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
96410027SChris.Adeniyi-Jones@arm.com{
96510027SChris.Adeniyi-Jones@arm.com    int index = 0;
96610027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
96710027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
96810027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
96910027SChris.Adeniyi-Jones@arm.com
97010027SChris.Adeniyi-Jones@arm.com    std::string path;
97110027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
97210027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
97310027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
97410027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
97510027SChris.Adeniyi-Jones@arm.com
97610027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
97710027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
97810027SChris.Adeniyi-Jones@arm.com
97910027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
98010027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
98110027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
98210027SChris.Adeniyi-Jones@arm.com#else
98310027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
98410027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
98510027SChris.Adeniyi-Jones@arm.com#endif
98610027SChris.Adeniyi-Jones@arm.com
98710027SChris.Adeniyi-Jones@arm.com    if (result < 0)
98810027SChris.Adeniyi-Jones@arm.com        return -errno;
98910027SChris.Adeniyi-Jones@arm.com
99010027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
99110027SChris.Adeniyi-Jones@arm.com
99210027SChris.Adeniyi-Jones@arm.com    return 0;
99310027SChris.Adeniyi-Jones@arm.com}
99410027SChris.Adeniyi-Jones@arm.com
99510027SChris.Adeniyi-Jones@arm.com
9961999SN/A/// Target fstat64() handler.
9971999SN/Atemplate <class OS>
9981999SN/ASyscallReturn
9993114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10002680Sktlim@umich.edu            ThreadContext *tc)
10011999SN/A{
10026701Sgblack@eecs.umich.edu    int index = 0;
10036701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
10046701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10051999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
10061999SN/A        // doesn't map to any simulator fd: not a valid target fd
10071999SN/A        return -EBADF;
10081999SN/A    }
10091999SN/A
10102764Sstever@eecs.umich.edu#if NO_STAT64
10112064SN/A    struct stat  hostBuf;
10122064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
10132064SN/A#else
10142064SN/A    struct stat64  hostBuf;
10151999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
10162064SN/A#endif
10171999SN/A
10181999SN/A    if (result < 0)
10192218SN/A        return -errno;
10201999SN/A
10218706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10221999SN/A
10231999SN/A    return 0;
10241999SN/A}
10251999SN/A
10261999SN/A
1027378SN/A/// Target lstat() handler.
1028360SN/Atemplate <class OS>
10291450SN/ASyscallReturn
10303114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10312680Sktlim@umich.edu          ThreadContext *tc)
1032360SN/A{
1033360SN/A    std::string path;
1034360SN/A
10356701Sgblack@eecs.umich.edu    int index = 0;
10368852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10376701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10386701Sgblack@eecs.umich.edu        return -EFAULT;
10396701Sgblack@eecs.umich.edu    }
10406701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1041360SN/A
10423669Sbinkertn@umich.edu    // Adjust path for current working directory
10433669Sbinkertn@umich.edu    path = process->fullPath(path);
10443669Sbinkertn@umich.edu
1045360SN/A    struct stat hostBuf;
1046360SN/A    int result = lstat(path.c_str(), &hostBuf);
1047360SN/A
1048360SN/A    if (result < 0)
10491458SN/A        return -errno;
1050360SN/A
10518706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1052360SN/A
10531458SN/A    return 0;
1054360SN/A}
1055360SN/A
10561999SN/A/// Target lstat64() handler.
10571999SN/Atemplate <class OS>
10581999SN/ASyscallReturn
10593114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10602680Sktlim@umich.edu            ThreadContext *tc)
10611999SN/A{
10621999SN/A    std::string path;
10631999SN/A
10646701Sgblack@eecs.umich.edu    int index = 0;
10658852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10666701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10676701Sgblack@eecs.umich.edu        return -EFAULT;
10686701Sgblack@eecs.umich.edu    }
10696701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10701999SN/A
10713669Sbinkertn@umich.edu    // Adjust path for current working directory
10723669Sbinkertn@umich.edu    path = process->fullPath(path);
10733669Sbinkertn@umich.edu
10742764Sstever@eecs.umich.edu#if NO_STAT64
10752064SN/A    struct stat hostBuf;
10762064SN/A    int result = lstat(path.c_str(), &hostBuf);
10772064SN/A#else
10781999SN/A    struct stat64 hostBuf;
10791999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10802064SN/A#endif
10811999SN/A
10821999SN/A    if (result < 0)
10831999SN/A        return -errno;
10841999SN/A
10858706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10861999SN/A
10871999SN/A    return 0;
10881999SN/A}
10891999SN/A
1090378SN/A/// Target fstat() handler.
1091360SN/Atemplate <class OS>
10921450SN/ASyscallReturn
10933114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10942680Sktlim@umich.edu          ThreadContext *tc)
1095360SN/A{
10966701Sgblack@eecs.umich.edu    int index = 0;
10976701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10986701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1099360SN/A
11001969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1101360SN/A
1102360SN/A    if (fd < 0)
11031458SN/A        return -EBADF;
1104360SN/A
1105360SN/A    struct stat hostBuf;
1106360SN/A    int result = fstat(fd, &hostBuf);
1107360SN/A
1108360SN/A    if (result < 0)
11091458SN/A        return -errno;
1110360SN/A
11118706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
11122021SN/A
11131458SN/A    return 0;
1114360SN/A}
1115360SN/A
1116360SN/A
11171706SN/A/// Target statfs() handler.
11181706SN/Atemplate <class OS>
11191706SN/ASyscallReturn
11203114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11212680Sktlim@umich.edu           ThreadContext *tc)
11221706SN/A{
11231706SN/A    std::string path;
11241706SN/A
11256701Sgblack@eecs.umich.edu    int index = 0;
11268852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11276701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11286701Sgblack@eecs.umich.edu        return -EFAULT;
11296701Sgblack@eecs.umich.edu    }
11306701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11311706SN/A
11323669Sbinkertn@umich.edu    // Adjust path for current working directory
11333669Sbinkertn@umich.edu    path = process->fullPath(path);
11343669Sbinkertn@umich.edu
11351706SN/A    struct statfs hostBuf;
11361706SN/A    int result = statfs(path.c_str(), &hostBuf);
11371706SN/A
11381706SN/A    if (result < 0)
11392218SN/A        return -errno;
11401706SN/A
11418706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11421706SN/A
11431706SN/A    return 0;
11441706SN/A}
11451706SN/A
11461706SN/A
11471706SN/A/// Target fstatfs() handler.
11481706SN/Atemplate <class OS>
11491706SN/ASyscallReturn
11503114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11512680Sktlim@umich.edu            ThreadContext *tc)
11521706SN/A{
11536701Sgblack@eecs.umich.edu    int index = 0;
11546701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11556701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11561706SN/A
11571706SN/A    if (fd < 0)
11581706SN/A        return -EBADF;
11591706SN/A
11601706SN/A    struct statfs hostBuf;
11611706SN/A    int result = fstatfs(fd, &hostBuf);
11621706SN/A
11631706SN/A    if (result < 0)
11642218SN/A        return -errno;
11651706SN/A
11668706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11671706SN/A
11681706SN/A    return 0;
11691706SN/A}
11701706SN/A
11711706SN/A
11721999SN/A/// Target writev() handler.
11731999SN/Atemplate <class OS>
11741999SN/ASyscallReturn
11753114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11762680Sktlim@umich.edu           ThreadContext *tc)
11771999SN/A{
11786701Sgblack@eecs.umich.edu    int index = 0;
11796701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11801999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11811999SN/A        // doesn't map to any simulator fd: not a valid target fd
11821999SN/A        return -EBADF;
11831999SN/A    }
11841999SN/A
11858852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11866701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11876701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11881999SN/A    struct iovec hiov[count];
11896227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11901999SN/A        typename OS::tgt_iovec tiov;
11912461SN/A
11928852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11938852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11948737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11951999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11968852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11978852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11981999SN/A    }
11991999SN/A
12001999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
12011999SN/A
12026227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
12031999SN/A        delete [] (char *)hiov[i].iov_base;
12041999SN/A
12051999SN/A    if (result < 0)
12062218SN/A        return -errno;
12071999SN/A
12081999SN/A    return 0;
12091999SN/A}
12101999SN/A
12111999SN/A
1212378SN/A/// Target mmap() handler.
1213378SN/A///
1214378SN/A/// We don't really handle mmap().  If the target is mmaping an
1215378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1216378SN/A/// nothing (since memory is initialized to zero and the simulator
12178324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
12188324Ssteve.reinhardt@amd.com///
1219360SN/Atemplate <class OS>
12201450SN/ASyscallReturn
12213114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1222360SN/A{
12236701Sgblack@eecs.umich.edu    int index = 0;
12246701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12256701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
12266701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
12276701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
12288324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
122910486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1230360SN/A
12319008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
12329008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12339008Sgblack@eecs.umich.edu
12348324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
12358324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
12368324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
12378324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12388324Ssteve.reinhardt@amd.com            return -EBADF;
12398324Ssteve.reinhardt@amd.com        }
12408324Ssteve.reinhardt@amd.com
12418324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
12428324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12438324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12448324Ssteve.reinhardt@amd.com            // another name on some platform
12458324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
12468324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
12478324Ssteve.reinhardt@amd.com        }
12488324Ssteve.reinhardt@amd.com    }
12495877Shsul@eecs.umich.edu
125010486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
125110486Stjablin@gmail.com
125210318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
125310486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12542544SN/A        warn("mmap failing: arguments not page-aligned: "
125510486Stjablin@gmail.com             "start 0x%x offset 0x%x",
125610486Stjablin@gmail.com             start, offset);
12572544SN/A        return -EINVAL;
1258360SN/A    }
1259360SN/A
12608600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12618600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12628600Ssteve.reinhardt@amd.com    bool clobber = false;
12638600Ssteve.reinhardt@amd.com
12648600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12658600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12668600Ssteve.reinhardt@amd.com
12678600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12688600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12698600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12708600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12718600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12728600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12738600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
127410485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
127510485SMichael.Adler@intel.com                return -EINVAL;
12768600Ssteve.reinhardt@amd.com            } else {
12778600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12788600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12798600Ssteve.reinhardt@amd.com                use_provided_address = false;
12808600Ssteve.reinhardt@amd.com            }
12818600Ssteve.reinhardt@amd.com        }
12822544SN/A    }
12832544SN/A
12848600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12858600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12868600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12878600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12888600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12898600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12908600Ssteve.reinhardt@amd.com        } else {
12918600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12928600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12938600Ssteve.reinhardt@amd.com        }
12946672Sgblack@eecs.umich.edu    }
12958600Ssteve.reinhardt@amd.com
12968601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12972544SN/A
12981458SN/A    return start;
1299360SN/A}
1300360SN/A
1301378SN/A/// Target getrlimit() handler.
1302360SN/Atemplate <class OS>
13031450SN/ASyscallReturn
13043114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13052680Sktlim@umich.edu        ThreadContext *tc)
1306360SN/A{
13076701Sgblack@eecs.umich.edu    int index = 0;
13086701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
13096701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1310360SN/A
1311360SN/A    switch (resource) {
13122064SN/A        case OS::TGT_RLIMIT_STACK:
13135877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
13142064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
13158737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13168737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13172064SN/A            break;
1318360SN/A
13195877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
13205877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
13215877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
13228737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13238737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13245877Shsul@eecs.umich.edu            break;
13255877Shsul@eecs.umich.edu
13262064SN/A        default:
13272064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
13282064SN/A                << std::endl;
13292064SN/A            abort();
13302064SN/A            break;
1331360SN/A    }
1332360SN/A
13338706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13341458SN/A    return 0;
1335360SN/A}
1336360SN/A
1337378SN/A/// Target gettimeofday() handler.
1338360SN/Atemplate <class OS>
13391450SN/ASyscallReturn
13403114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13412680Sktlim@umich.edu        ThreadContext *tc)
1342360SN/A{
13436701Sgblack@eecs.umich.edu    int index = 0;
13446701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1345360SN/A
1346360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1347360SN/A    tp->tv_sec += seconds_since_epoch;
13486109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13496109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1350360SN/A
13518706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1352360SN/A
13531458SN/A    return 0;
1354360SN/A}
1355360SN/A
1356360SN/A
13571999SN/A/// Target utimes() handler.
13581999SN/Atemplate <class OS>
13591999SN/ASyscallReturn
13603114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13612680Sktlim@umich.edu           ThreadContext *tc)
13621999SN/A{
13631999SN/A    std::string path;
13641999SN/A
13656701Sgblack@eecs.umich.edu    int index = 0;
13668852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13676701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13686701Sgblack@eecs.umich.edu        return -EFAULT;
13696701Sgblack@eecs.umich.edu    }
13701999SN/A
13716701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13726701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13738706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13741999SN/A
13751999SN/A    struct timeval hostTimeval[2];
13761999SN/A    for (int i = 0; i < 2; ++i)
13771999SN/A    {
13788737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13798737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13801999SN/A    }
13813669Sbinkertn@umich.edu
13823669Sbinkertn@umich.edu    // Adjust path for current working directory
13833669Sbinkertn@umich.edu    path = process->fullPath(path);
13843669Sbinkertn@umich.edu
13851999SN/A    int result = utimes(path.c_str(), hostTimeval);
13861999SN/A
13871999SN/A    if (result < 0)
13881999SN/A        return -errno;
13891999SN/A
13901999SN/A    return 0;
13911999SN/A}
1392378SN/A/// Target getrusage() function.
1393360SN/Atemplate <class OS>
13941450SN/ASyscallReturn
13953114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13962680Sktlim@umich.edu              ThreadContext *tc)
1397360SN/A{
13986701Sgblack@eecs.umich.edu    int index = 0;
13996701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
14006701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1401360SN/A
14023670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
14033670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1404360SN/A    rup->ru_stime.tv_sec = 0;
1405360SN/A    rup->ru_stime.tv_usec = 0;
1406360SN/A    rup->ru_maxrss = 0;
1407360SN/A    rup->ru_ixrss = 0;
1408360SN/A    rup->ru_idrss = 0;
1409360SN/A    rup->ru_isrss = 0;
1410360SN/A    rup->ru_minflt = 0;
1411360SN/A    rup->ru_majflt = 0;
1412360SN/A    rup->ru_nswap = 0;
1413360SN/A    rup->ru_inblock = 0;
1414360SN/A    rup->ru_oublock = 0;
1415360SN/A    rup->ru_msgsnd = 0;
1416360SN/A    rup->ru_msgrcv = 0;
1417360SN/A    rup->ru_nsignals = 0;
1418360SN/A    rup->ru_nvcsw = 0;
1419360SN/A    rup->ru_nivcsw = 0;
1420360SN/A
14213670Sbinkertn@umich.edu    switch (who) {
14223670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
14233670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14248737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14258737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14263670Sbinkertn@umich.edu        break;
14273670Sbinkertn@umich.edu
14283670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14293670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14303670Sbinkertn@umich.edu        break;
14313670Sbinkertn@umich.edu
14323670Sbinkertn@umich.edu      default:
14333670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14343670Sbinkertn@umich.edu        // plow ahead
14353670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14363670Sbinkertn@umich.edu             who);
14373670Sbinkertn@umich.edu    }
14383670Sbinkertn@umich.edu
14398706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1440360SN/A
14411458SN/A    return 0;
1442360SN/A}
1443360SN/A
14446683Stjones1@inf.ed.ac.uk/// Target times() function.
14456683Stjones1@inf.ed.ac.uktemplate <class OS>
14466683Stjones1@inf.ed.ac.ukSyscallReturn
14476683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14486683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14496683Stjones1@inf.ed.ac.uk{
14506701Sgblack@eecs.umich.edu    int index = 0;
14516701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14526683Stjones1@inf.ed.ac.uk
14536683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14547823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14556683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14566683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14576683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14586683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14596683Stjones1@inf.ed.ac.uk
14606683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14618737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14626683Stjones1@inf.ed.ac.uk
14636683Stjones1@inf.ed.ac.uk    // Write back
14648706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14656683Stjones1@inf.ed.ac.uk
14666683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14676683Stjones1@inf.ed.ac.uk    return clocks;
14686683Stjones1@inf.ed.ac.uk}
14692553SN/A
14706684Stjones1@inf.ed.ac.uk/// Target time() function.
14716684Stjones1@inf.ed.ac.uktemplate <class OS>
14726684Stjones1@inf.ed.ac.ukSyscallReturn
14736684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14746684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14756684Stjones1@inf.ed.ac.uk{
14766684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14776684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14786684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14796684Stjones1@inf.ed.ac.uk
14806701Sgblack@eecs.umich.edu    int index = 0;
14816701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14826684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14836684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14848737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14858852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14868852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14876684Stjones1@inf.ed.ac.uk    }
14886684Stjones1@inf.ed.ac.uk    return sec;
14896684Stjones1@inf.ed.ac.uk}
14902553SN/A
14912553SN/A
14921354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1493