syscall_emul.hh revision 10027
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
2565513SMichael.Adler@intel.com/// Target unlink() handler.
2575513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2585513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2595513SMichael.Adler@intel.com
260511SN/A/// Target unlink() handler.
2611706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2623114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
263511SN/A
2645513SMichael.Adler@intel.com/// Target mkdir() handler.
2655513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2665513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2675513SMichael.Adler@intel.com
268511SN/A/// Target rename() handler.
2691706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2703114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2711706SN/A
2721706SN/A
2731706SN/A/// Target truncate() handler.
2741706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2753114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2761706SN/A
2771706SN/A
2781706SN/A/// Target ftruncate() handler.
2791706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2811706SN/A
282511SN/A
2836703Svince@csl.cornell.edu/// Target truncate64() handler.
2846703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2856703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2866703Svince@csl.cornell.edu
2876685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2886685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2896685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2906685Stjones1@inf.ed.ac.uk
2916685Stjones1@inf.ed.ac.uk
2925513SMichael.Adler@intel.com/// Target umask() handler.
2935513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2945513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2955513SMichael.Adler@intel.com
2965513SMichael.Adler@intel.com
2971999SN/A/// Target chown() handler.
2981999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2993114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
3001999SN/A
3011999SN/A
3021999SN/A/// Target fchown() handler.
3031999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
3043114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
3051999SN/A
3063079Sstever@eecs.umich.edu/// Target dup() handler.
3073079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
3083114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
3093079Sstever@eecs.umich.edu
3102093SN/A/// Target fnctl() handler.
3112093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
3123114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3132093SN/A
3142687Sksewell@umich.edu/// Target fcntl64() handler.
3152687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
3163114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
3172687Sksewell@umich.edu
3182238SN/A/// Target setuid() handler.
3192238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
3203114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3212238SN/A
3222238SN/A/// Target getpid() handler.
3232238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
3243114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3252238SN/A
3262238SN/A/// Target getuid() handler.
3272238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
3283114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3292238SN/A
3302238SN/A/// Target getgid() handler.
3312238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
3323114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3332238SN/A
3342238SN/A/// Target getppid() handler.
3352238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
3363114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3372238SN/A
3382238SN/A/// Target geteuid() handler.
3392238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3403114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3412238SN/A
3422238SN/A/// Target getegid() handler.
3432238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3443114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3452238SN/A
3466109Ssanchezd@stanford.edu/// Target clone() handler.
3476109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3486109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3492238SN/A
3509455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3519455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3529455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
3539455Smitch.hayenga+gem5@gmail.com
3549112Smarc.orr@gmail.com/// Futex system call
3559112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3569112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3579112Smarc.orr@gmail.comtemplate <class OS>
3589112Smarc.orr@gmail.comSyscallReturn
3599112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3609112Smarc.orr@gmail.com          ThreadContext *tc)
3619112Smarc.orr@gmail.com{
3629112Smarc.orr@gmail.com    int index_uaddr = 0;
3639112Smarc.orr@gmail.com    int index_op = 1;
3649112Smarc.orr@gmail.com    int index_val = 2;
3659112Smarc.orr@gmail.com    int index_timeout = 3;
3669112Smarc.orr@gmail.com
3679112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3689112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3699112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3709112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3719112Smarc.orr@gmail.com
3729112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3739112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3749112Smarc.orr@gmail.com
3759112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3769112Smarc.orr@gmail.com            uaddr, op, val);
3779112Smarc.orr@gmail.com
3789238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3799112Smarc.orr@gmail.com
3809112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3819112Smarc.orr@gmail.com        if (timeout != 0) {
3829112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3839112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3849112Smarc.orr@gmail.com        }
3859112Smarc.orr@gmail.com
3869112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3879112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3889112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3899112Smarc.orr@gmail.com        delete buf;
3909112Smarc.orr@gmail.com
3919112Smarc.orr@gmail.com        if(val != mem_val) {
3929112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3939112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3949112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3959112Smarc.orr@gmail.com        }
3969112Smarc.orr@gmail.com
3979112Smarc.orr@gmail.com        // Queue the thread context
3989112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3999112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
4009112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4019112Smarc.orr@gmail.com        } else {
4029112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
4039112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
4049112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
4059112Smarc.orr@gmail.com        }
4069112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
4079112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
4089112Smarc.orr@gmail.com                                "thread context\n");
4099112Smarc.orr@gmail.com        tc->suspend();
4109112Smarc.orr@gmail.com        return 0;
4119112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
4129112Smarc.orr@gmail.com        int wokenUp = 0;
4139112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
4149112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
4159112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
4169112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
4179112Smarc.orr@gmail.com                tcWaitList->front()->activate();
4189112Smarc.orr@gmail.com                tcWaitList->pop_front();
4199112Smarc.orr@gmail.com                wokenUp++;
4209112Smarc.orr@gmail.com            }
4219112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
4229112Smarc.orr@gmail.com                futex_map.erase(uaddr);
4239112Smarc.orr@gmail.com                delete tcWaitList;
4249112Smarc.orr@gmail.com            }
4259112Smarc.orr@gmail.com        }
4269112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
4279112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
4289112Smarc.orr@gmail.com        return wokenUp;
4299112Smarc.orr@gmail.com    } else {
4309238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
4319112Smarc.orr@gmail.com        return 0;
4329112Smarc.orr@gmail.com    }
4339112Smarc.orr@gmail.com
4349112Smarc.orr@gmail.com}
4359112Smarc.orr@gmail.com
4362238SN/A
4372238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4382238SN/A/// returning a second value in a register other than the normal return register
4392238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
4403114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
4412238SN/A
4422238SN/A/// Target getpidPseudo() handler.
4432238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4443114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4452238SN/A
4462238SN/A/// Target getuidPseudo() handler.
4472238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4483114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4492238SN/A
4502238SN/A/// Target getgidPseudo() handler.
4512238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4523114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4532238SN/A
4542238SN/A
4551354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4561354SN/Aconst int one_million = 1000000;
4571354SN/A
4581354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4591354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4601354SN/A/// real-world time) to keep simulations repeatable.
4611354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4621354SN/A
4631354SN/A/// Helper function to convert current elapsed time to seconds and
4641354SN/A/// microseconds.
4651354SN/Atemplate <class T1, class T2>
4661354SN/Avoid
4671354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4681354SN/A{
4697823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4701354SN/A    sec = elapsed_usecs / one_million;
4711354SN/A    usec = elapsed_usecs % one_million;
4721354SN/A}
4731354SN/A
474360SN/A//////////////////////////////////////////////////////////////////////
475360SN/A//
476360SN/A// The following emulation functions are generic, but need to be
477360SN/A// templated to account for differences in types, constants, etc.
478360SN/A//
479360SN/A//////////////////////////////////////////////////////////////////////
480360SN/A
4813113Sgblack@eecs.umich.edu#if NO_STAT64
4823113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4833113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4843113Sgblack@eecs.umich.edu#else
4853113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4863113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4873113Sgblack@eecs.umich.edu#endif
4883113Sgblack@eecs.umich.edu
4893113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4903113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4913113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4923113Sgblack@eecs.umich.edu
4933113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4943113Sgblack@eecs.umich.edustatic void
4953113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4963113Sgblack@eecs.umich.edu{
4974189Sgblack@eecs.umich.edu    using namespace TheISA;
4984189Sgblack@eecs.umich.edu
4993113Sgblack@eecs.umich.edu    if (fakeTTY)
5003113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5013113Sgblack@eecs.umich.edu    else
5023113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5038737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5043113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5058737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5063277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5075515SMichael.Adler@intel.com    if (fakeTTY) {
5085515SMichael.Adler@intel.com        // Claim to be a character device
5095515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5105515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5115515SMichael.Adler@intel.com    }
5128737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5133277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5148737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5153277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5168737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5173277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5188737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5193113Sgblack@eecs.umich.edu    if (fakeTTY)
5203113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5213113Sgblack@eecs.umich.edu    else
5223113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5238737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5243113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5258737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5263114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5278737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5283114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5298737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5303114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5318737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5324061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5334061Sgblack@eecs.umich.edu    // consistently across different hosts.
5344061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5358737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5363113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5378737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5383113Sgblack@eecs.umich.edu}
5393113Sgblack@eecs.umich.edu
5403113Sgblack@eecs.umich.edu// Same for stat64
5413113Sgblack@eecs.umich.edu
5423113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5433113Sgblack@eecs.umich.edustatic void
5443113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5453113Sgblack@eecs.umich.edu{
5464189Sgblack@eecs.umich.edu    using namespace TheISA;
5474189Sgblack@eecs.umich.edu
5483113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5493113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5503113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5518737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5523113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5538737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5543113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5558737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5563113Sgblack@eecs.umich.edu#else
5573113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5583113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5593113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5603113Sgblack@eecs.umich.edu#endif
5613113Sgblack@eecs.umich.edu}
5623113Sgblack@eecs.umich.edu
5633113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5643113Sgblack@eecs.umich.edutemplate<class OS>
5653113Sgblack@eecs.umich.edustatic void
5668852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5673113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5683113Sgblack@eecs.umich.edu{
5693113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5703113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5713113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5723113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5733113Sgblack@eecs.umich.edu}
5743113Sgblack@eecs.umich.edu
5753113Sgblack@eecs.umich.edutemplate<class OS>
5763113Sgblack@eecs.umich.edustatic void
5778852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5783113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5793113Sgblack@eecs.umich.edu{
5803113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5813113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5826686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5833113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5843113Sgblack@eecs.umich.edu}
5853113Sgblack@eecs.umich.edu
586378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
587378SN/A/// only to find out if their stdout is a tty, to determine whether to
5889141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5899141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
590360SN/Atemplate <class OS>
5911450SN/ASyscallReturn
5923114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5932680Sktlim@umich.edu          ThreadContext *tc)
594360SN/A{
5956701Sgblack@eecs.umich.edu    int index = 0;
5966701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5976701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
598360SN/A
5991969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
600360SN/A
601360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
602360SN/A        // doesn't map to any simulator fd: not a valid target fd
6031458SN/A        return -EBADF;
604360SN/A    }
605360SN/A
6069141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
6071458SN/A        return -ENOTTY;
6089141Smarc.orr@gmail.com    }
609360SN/A
6109141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
6119141Smarc.orr@gmail.com         fd, req, tc->pcState());
6129141Smarc.orr@gmail.com    return -ENOTTY;
613360SN/A}
614360SN/A
615360SN/Atemplate <class OS>
61610027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
6173114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
61810027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
619360SN/A{
620360SN/A    std::string path;
621360SN/A
6228852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6236701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6241458SN/A        return -EFAULT;
625360SN/A
626360SN/A    if (path == "/dev/sysdev0") {
627360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
628360SN/A        // We don't support it, so just punt.
6291706SN/A        warn("Ignoring open(%s, ...)\n", path);
6301458SN/A        return -ENOENT;
631360SN/A    }
632360SN/A
6336701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6346701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
635360SN/A    int hostFlags = 0;
636360SN/A
637360SN/A    // translate open flags
638360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
639360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
640360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
641360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
642360SN/A        }
643360SN/A    }
644360SN/A
645360SN/A    // any target flags left?
646360SN/A    if (tgtFlags != 0)
6471706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
648360SN/A
649360SN/A#ifdef __CYGWIN32__
650360SN/A    hostFlags |= O_BINARY;
651360SN/A#endif
652360SN/A
6533669Sbinkertn@umich.edu    // Adjust path for current working directory
6543669Sbinkertn@umich.edu    path = process->fullPath(path);
6553669Sbinkertn@umich.edu
6561706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6571706SN/A
6585795Ssaidi@eecs.umich.edu    int fd;
6599143Ssteve.reinhardt@amd.com    int local_errno;
6609142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6619142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6629143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6635795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6649143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6655795Ssaidi@eecs.umich.edu     } else {
6665795Ssaidi@eecs.umich.edu        // open the file
6675795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6689143Ssteve.reinhardt@amd.com        local_errno = errno;
6695795Ssaidi@eecs.umich.edu     }
670360SN/A
6719143Ssteve.reinhardt@amd.com    if (fd == -1)
6729143Ssteve.reinhardt@amd.com        return -local_errno;
6739143Ssteve.reinhardt@amd.com
6749143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
675360SN/A}
676360SN/A
67710027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
67810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67910027SChris.Adeniyi-Jones@arm.comSyscallReturn
68010027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68110027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
68210027SChris.Adeniyi-Jones@arm.com{
68310027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
68410027SChris.Adeniyi-Jones@arm.com}
68510027SChris.Adeniyi-Jones@arm.com
68610027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
68710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
68810027SChris.Adeniyi-Jones@arm.comSyscallReturn
68910027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
69110027SChris.Adeniyi-Jones@arm.com{
69210027SChris.Adeniyi-Jones@arm.com    int index = 0;
69310027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
69410027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
69510027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
69610027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
69710027SChris.Adeniyi-Jones@arm.com}
69810027SChris.Adeniyi-Jones@arm.com
6996640Svince@csl.cornell.edu/// Target sysinfo() handler.
7006640Svince@csl.cornell.edutemplate <class OS>
7016640Svince@csl.cornell.eduSyscallReturn
7026640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7036640Svince@csl.cornell.edu         ThreadContext *tc)
7046640Svince@csl.cornell.edu{
7056640Svince@csl.cornell.edu
7066701Sgblack@eecs.umich.edu    int index = 0;
7076701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
7086701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
7096640Svince@csl.cornell.edu
7106701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7116701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7126640Svince@csl.cornell.edu
7138706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7146640Svince@csl.cornell.edu
7156701Sgblack@eecs.umich.edu    return 0;
7166640Svince@csl.cornell.edu}
717360SN/A
7181999SN/A/// Target chmod() handler.
7191999SN/Atemplate <class OS>
7201999SN/ASyscallReturn
7213114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7222680Sktlim@umich.edu          ThreadContext *tc)
7231999SN/A{
7241999SN/A    std::string path;
7251999SN/A
7266701Sgblack@eecs.umich.edu    int index = 0;
7278852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7286701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7291999SN/A        return -EFAULT;
7306701Sgblack@eecs.umich.edu    }
7311999SN/A
7326701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7331999SN/A    mode_t hostMode = 0;
7341999SN/A
7351999SN/A    // XXX translate mode flags via OS::something???
7361999SN/A    hostMode = mode;
7371999SN/A
7383669Sbinkertn@umich.edu    // Adjust path for current working directory
7393669Sbinkertn@umich.edu    path = process->fullPath(path);
7403669Sbinkertn@umich.edu
7411999SN/A    // do the chmod
7421999SN/A    int result = chmod(path.c_str(), hostMode);
7431999SN/A    if (result < 0)
7442218SN/A        return -errno;
7451999SN/A
7461999SN/A    return 0;
7471999SN/A}
7481999SN/A
7491999SN/A
7501999SN/A/// Target fchmod() handler.
7511999SN/Atemplate <class OS>
7521999SN/ASyscallReturn
7533114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7542680Sktlim@umich.edu           ThreadContext *tc)
7551999SN/A{
7566701Sgblack@eecs.umich.edu    int index = 0;
7576701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7581999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7591999SN/A        // doesn't map to any simulator fd: not a valid target fd
7601999SN/A        return -EBADF;
7611999SN/A    }
7621999SN/A
7636701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7641999SN/A    mode_t hostMode = 0;
7651999SN/A
7661999SN/A    // XXX translate mode flags via OS::someting???
7671999SN/A    hostMode = mode;
7681999SN/A
7691999SN/A    // do the fchmod
7701999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7711999SN/A    if (result < 0)
7722218SN/A        return -errno;
7731999SN/A
7741999SN/A    return 0;
7751999SN/A}
7761999SN/A
7775877Shsul@eecs.umich.edu/// Target mremap() handler.
7785877Shsul@eecs.umich.edutemplate <class OS>
7795877Shsul@eecs.umich.eduSyscallReturn
7805877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7815877Shsul@eecs.umich.edu{
7826701Sgblack@eecs.umich.edu    int index = 0;
7836701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7846701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7856701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7866701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
78710027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
78810027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
78910027SChris.Adeniyi-Jones@arm.com
79010027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
79110027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
7925877Shsul@eecs.umich.edu
7935877Shsul@eecs.umich.edu    if ((start % TheISA::VMPageSize != 0) ||
79410027SChris.Adeniyi-Jones@arm.com        (new_length % TheISA::VMPageSize != 0) ||
79510027SChris.Adeniyi-Jones@arm.com        (provided_address % TheISA::VMPageSize != 0)) {
7965877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7975877Shsul@eecs.umich.edu        return -EINVAL;
7985877Shsul@eecs.umich.edu    }
7995877Shsul@eecs.umich.edu
8005877Shsul@eecs.umich.edu    if (new_length > old_length) {
80110027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
80210027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8035877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8048601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8055877Shsul@eecs.umich.edu            process->mmap_end += diff;
8065877Shsul@eecs.umich.edu            return start;
8075877Shsul@eecs.umich.edu        } else {
80810027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8095877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8105877Shsul@eecs.umich.edu                return -ENOMEM;
8115877Shsul@eecs.umich.edu            } else {
81210027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
81310027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
81410027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
81510027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
81610027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
81710027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8185877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
81910027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
82010027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
82110027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
82210027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
82310027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
82410027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
82510027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
82610027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
82710027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
82810027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
82910027SChris.Adeniyi-Jones@arm.com                }
83010027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
83110027SChris.Adeniyi-Jones@arm.com                return new_start;
8325877Shsul@eecs.umich.edu            }
8335877Shsul@eecs.umich.edu        }
8345877Shsul@eecs.umich.edu    } else {
83510027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
83610027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8378601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
83810027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8395877Shsul@eecs.umich.edu    }
8405877Shsul@eecs.umich.edu}
8411999SN/A
842378SN/A/// Target stat() handler.
843360SN/Atemplate <class OS>
8441450SN/ASyscallReturn
8453114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8462680Sktlim@umich.edu         ThreadContext *tc)
847360SN/A{
848360SN/A    std::string path;
849360SN/A
8506701Sgblack@eecs.umich.edu    int index = 0;
8518852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8526701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8536701Sgblack@eecs.umich.edu        return -EFAULT;
8546701Sgblack@eecs.umich.edu    }
8556701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
856360SN/A
8573669Sbinkertn@umich.edu    // Adjust path for current working directory
8583669Sbinkertn@umich.edu    path = process->fullPath(path);
8593669Sbinkertn@umich.edu
860360SN/A    struct stat hostBuf;
861360SN/A    int result = stat(path.c_str(), &hostBuf);
862360SN/A
863360SN/A    if (result < 0)
8642218SN/A        return -errno;
865360SN/A
8668706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
867360SN/A
8681458SN/A    return 0;
869360SN/A}
870360SN/A
871360SN/A
8725074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8735074Ssaidi@eecs.umich.edutemplate <class OS>
8745074Ssaidi@eecs.umich.eduSyscallReturn
8755074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8765074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8775074Ssaidi@eecs.umich.edu{
8785074Ssaidi@eecs.umich.edu    std::string path;
8795074Ssaidi@eecs.umich.edu
8806701Sgblack@eecs.umich.edu    int index = 0;
8818852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8826701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8835074Ssaidi@eecs.umich.edu        return -EFAULT;
8846701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8855074Ssaidi@eecs.umich.edu
8865074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8875074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8885074Ssaidi@eecs.umich.edu
8895208Ssaidi@eecs.umich.edu#if NO_STAT64
8905208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8915208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8925208Ssaidi@eecs.umich.edu#else
8935074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8945074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8955208Ssaidi@eecs.umich.edu#endif
8965074Ssaidi@eecs.umich.edu
8975074Ssaidi@eecs.umich.edu    if (result < 0)
8985074Ssaidi@eecs.umich.edu        return -errno;
8995074Ssaidi@eecs.umich.edu
9008706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9015074Ssaidi@eecs.umich.edu
9025074Ssaidi@eecs.umich.edu    return 0;
9035074Ssaidi@eecs.umich.edu}
9045074Ssaidi@eecs.umich.edu
9055074Ssaidi@eecs.umich.edu
90610027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
90710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
90810027SChris.Adeniyi-Jones@arm.comSyscallReturn
90910027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
91010027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
91110027SChris.Adeniyi-Jones@arm.com{
91210027SChris.Adeniyi-Jones@arm.com    int index = 0;
91310027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
91410027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
91510027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
91610027SChris.Adeniyi-Jones@arm.com
91710027SChris.Adeniyi-Jones@arm.com    std::string path;
91810027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
91910027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
92010027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
92110027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
92210027SChris.Adeniyi-Jones@arm.com
92310027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
92410027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
92510027SChris.Adeniyi-Jones@arm.com
92610027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
92710027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
92810027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
92910027SChris.Adeniyi-Jones@arm.com#else
93010027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
93110027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
93210027SChris.Adeniyi-Jones@arm.com#endif
93310027SChris.Adeniyi-Jones@arm.com
93410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
93510027SChris.Adeniyi-Jones@arm.com        return -errno;
93610027SChris.Adeniyi-Jones@arm.com
93710027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
93810027SChris.Adeniyi-Jones@arm.com
93910027SChris.Adeniyi-Jones@arm.com    return 0;
94010027SChris.Adeniyi-Jones@arm.com}
94110027SChris.Adeniyi-Jones@arm.com
94210027SChris.Adeniyi-Jones@arm.com
9431999SN/A/// Target fstat64() handler.
9441999SN/Atemplate <class OS>
9451999SN/ASyscallReturn
9463114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9472680Sktlim@umich.edu            ThreadContext *tc)
9481999SN/A{
9496701Sgblack@eecs.umich.edu    int index = 0;
9506701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9516701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9521999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9531999SN/A        // doesn't map to any simulator fd: not a valid target fd
9541999SN/A        return -EBADF;
9551999SN/A    }
9561999SN/A
9572764Sstever@eecs.umich.edu#if NO_STAT64
9582064SN/A    struct stat  hostBuf;
9592064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
9602064SN/A#else
9612064SN/A    struct stat64  hostBuf;
9621999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
9632064SN/A#endif
9641999SN/A
9651999SN/A    if (result < 0)
9662218SN/A        return -errno;
9671999SN/A
9688706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9691999SN/A
9701999SN/A    return 0;
9711999SN/A}
9721999SN/A
9731999SN/A
974378SN/A/// Target lstat() handler.
975360SN/Atemplate <class OS>
9761450SN/ASyscallReturn
9773114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9782680Sktlim@umich.edu          ThreadContext *tc)
979360SN/A{
980360SN/A    std::string path;
981360SN/A
9826701Sgblack@eecs.umich.edu    int index = 0;
9838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9846701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9856701Sgblack@eecs.umich.edu        return -EFAULT;
9866701Sgblack@eecs.umich.edu    }
9876701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
988360SN/A
9893669Sbinkertn@umich.edu    // Adjust path for current working directory
9903669Sbinkertn@umich.edu    path = process->fullPath(path);
9913669Sbinkertn@umich.edu
992360SN/A    struct stat hostBuf;
993360SN/A    int result = lstat(path.c_str(), &hostBuf);
994360SN/A
995360SN/A    if (result < 0)
9961458SN/A        return -errno;
997360SN/A
9988706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
999360SN/A
10001458SN/A    return 0;
1001360SN/A}
1002360SN/A
10031999SN/A/// Target lstat64() handler.
10041999SN/Atemplate <class OS>
10051999SN/ASyscallReturn
10063114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10072680Sktlim@umich.edu            ThreadContext *tc)
10081999SN/A{
10091999SN/A    std::string path;
10101999SN/A
10116701Sgblack@eecs.umich.edu    int index = 0;
10128852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10136701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10146701Sgblack@eecs.umich.edu        return -EFAULT;
10156701Sgblack@eecs.umich.edu    }
10166701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10171999SN/A
10183669Sbinkertn@umich.edu    // Adjust path for current working directory
10193669Sbinkertn@umich.edu    path = process->fullPath(path);
10203669Sbinkertn@umich.edu
10212764Sstever@eecs.umich.edu#if NO_STAT64
10222064SN/A    struct stat hostBuf;
10232064SN/A    int result = lstat(path.c_str(), &hostBuf);
10242064SN/A#else
10251999SN/A    struct stat64 hostBuf;
10261999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10272064SN/A#endif
10281999SN/A
10291999SN/A    if (result < 0)
10301999SN/A        return -errno;
10311999SN/A
10328706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10331999SN/A
10341999SN/A    return 0;
10351999SN/A}
10361999SN/A
1037378SN/A/// Target fstat() handler.
1038360SN/Atemplate <class OS>
10391450SN/ASyscallReturn
10403114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10412680Sktlim@umich.edu          ThreadContext *tc)
1042360SN/A{
10436701Sgblack@eecs.umich.edu    int index = 0;
10446701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10456701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1046360SN/A
10471969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1048360SN/A
1049360SN/A    if (fd < 0)
10501458SN/A        return -EBADF;
1051360SN/A
1052360SN/A    struct stat hostBuf;
1053360SN/A    int result = fstat(fd, &hostBuf);
1054360SN/A
1055360SN/A    if (result < 0)
10561458SN/A        return -errno;
1057360SN/A
10588706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10592021SN/A
10601458SN/A    return 0;
1061360SN/A}
1062360SN/A
1063360SN/A
10641706SN/A/// Target statfs() handler.
10651706SN/Atemplate <class OS>
10661706SN/ASyscallReturn
10673114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10682680Sktlim@umich.edu           ThreadContext *tc)
10691706SN/A{
10701706SN/A    std::string path;
10711706SN/A
10726701Sgblack@eecs.umich.edu    int index = 0;
10738852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10746701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10756701Sgblack@eecs.umich.edu        return -EFAULT;
10766701Sgblack@eecs.umich.edu    }
10776701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10781706SN/A
10793669Sbinkertn@umich.edu    // Adjust path for current working directory
10803669Sbinkertn@umich.edu    path = process->fullPath(path);
10813669Sbinkertn@umich.edu
10821706SN/A    struct statfs hostBuf;
10831706SN/A    int result = statfs(path.c_str(), &hostBuf);
10841706SN/A
10851706SN/A    if (result < 0)
10862218SN/A        return -errno;
10871706SN/A
10888706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10891706SN/A
10901706SN/A    return 0;
10911706SN/A}
10921706SN/A
10931706SN/A
10941706SN/A/// Target fstatfs() handler.
10951706SN/Atemplate <class OS>
10961706SN/ASyscallReturn
10973114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10982680Sktlim@umich.edu            ThreadContext *tc)
10991706SN/A{
11006701Sgblack@eecs.umich.edu    int index = 0;
11016701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11026701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11031706SN/A
11041706SN/A    if (fd < 0)
11051706SN/A        return -EBADF;
11061706SN/A
11071706SN/A    struct statfs hostBuf;
11081706SN/A    int result = fstatfs(fd, &hostBuf);
11091706SN/A
11101706SN/A    if (result < 0)
11112218SN/A        return -errno;
11121706SN/A
11138706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11141706SN/A
11151706SN/A    return 0;
11161706SN/A}
11171706SN/A
11181706SN/A
11191999SN/A/// Target writev() handler.
11201999SN/Atemplate <class OS>
11211999SN/ASyscallReturn
11223114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11232680Sktlim@umich.edu           ThreadContext *tc)
11241999SN/A{
11256701Sgblack@eecs.umich.edu    int index = 0;
11266701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11271999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11281999SN/A        // doesn't map to any simulator fd: not a valid target fd
11291999SN/A        return -EBADF;
11301999SN/A    }
11311999SN/A
11328852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11336701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11346701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11351999SN/A    struct iovec hiov[count];
11366227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11371999SN/A        typename OS::tgt_iovec tiov;
11382461SN/A
11398852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11408852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11418737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11421999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11438852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11448852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11451999SN/A    }
11461999SN/A
11471999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11481999SN/A
11496227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11501999SN/A        delete [] (char *)hiov[i].iov_base;
11511999SN/A
11521999SN/A    if (result < 0)
11532218SN/A        return -errno;
11541999SN/A
11551999SN/A    return 0;
11561999SN/A}
11571999SN/A
11581999SN/A
1159378SN/A/// Target mmap() handler.
1160378SN/A///
1161378SN/A/// We don't really handle mmap().  If the target is mmaping an
1162378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1163378SN/A/// nothing (since memory is initialized to zero and the simulator
11648324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
11658324Ssteve.reinhardt@amd.com///
1166360SN/Atemplate <class OS>
11671450SN/ASyscallReturn
11683114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1169360SN/A{
11706701Sgblack@eecs.umich.edu    int index = 0;
11716701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
11726701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
11736701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
11746701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
11758324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
11766701Sgblack@eecs.umich.edu    // int offset = p->getSyscallArg(tc, index);
1177360SN/A
11789008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
11799008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
11809008Sgblack@eecs.umich.edu
11818324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
11828324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
11838324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
11848324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
11858324Ssteve.reinhardt@amd.com            return -EBADF;
11868324Ssteve.reinhardt@amd.com        }
11878324Ssteve.reinhardt@amd.com
11888324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
11898324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
11908324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
11918324Ssteve.reinhardt@amd.com            // another name on some platform
11928324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11938324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11948324Ssteve.reinhardt@amd.com        }
11958324Ssteve.reinhardt@amd.com    }
11965877Shsul@eecs.umich.edu
11972544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
11982544SN/A        (length % TheISA::VMPageSize) != 0) {
11992544SN/A        warn("mmap failing: arguments not page-aligned: "
12002544SN/A             "start 0x%x length 0x%x",
12012544SN/A             start, length);
12022544SN/A        return -EINVAL;
1203360SN/A    }
1204360SN/A
12058600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12068600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12078600Ssteve.reinhardt@amd.com    bool clobber = false;
12088600Ssteve.reinhardt@amd.com
12098600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12108600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12118600Ssteve.reinhardt@amd.com
12128600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12138600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12148600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12158600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12168600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12178600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12188600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
12198600Ssteve.reinhardt@amd.com                // MAP_FIXED specified: clobber existing mappings
12208600Ssteve.reinhardt@amd.com                warn("mmap: MAP_FIXED at 0x%x overwrites existing mappings\n",
12218600Ssteve.reinhardt@amd.com                     start);
12228600Ssteve.reinhardt@amd.com                clobber = true;
12238600Ssteve.reinhardt@amd.com            } else {
12248600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12258600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12268600Ssteve.reinhardt@amd.com                use_provided_address = false;
12278600Ssteve.reinhardt@amd.com            }
12288600Ssteve.reinhardt@amd.com        }
12292544SN/A    }
12302544SN/A
12318600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12328600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12338600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12348600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12358600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12368600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12378600Ssteve.reinhardt@amd.com        } else {
12388600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12398600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12408600Ssteve.reinhardt@amd.com        }
12416672Sgblack@eecs.umich.edu    }
12428600Ssteve.reinhardt@amd.com
12438601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12442544SN/A
12451458SN/A    return start;
1246360SN/A}
1247360SN/A
1248378SN/A/// Target getrlimit() handler.
1249360SN/Atemplate <class OS>
12501450SN/ASyscallReturn
12513114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12522680Sktlim@umich.edu        ThreadContext *tc)
1253360SN/A{
12546701Sgblack@eecs.umich.edu    int index = 0;
12556701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12566701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1257360SN/A
1258360SN/A    switch (resource) {
12592064SN/A        case OS::TGT_RLIMIT_STACK:
12605877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
12612064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
12628737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12638737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12642064SN/A            break;
1265360SN/A
12665877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
12675877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
12685877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
12698737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12708737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12715877Shsul@eecs.umich.edu            break;
12725877Shsul@eecs.umich.edu
12732064SN/A        default:
12742064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
12752064SN/A                << std::endl;
12762064SN/A            abort();
12772064SN/A            break;
1278360SN/A    }
1279360SN/A
12808706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
12811458SN/A    return 0;
1282360SN/A}
1283360SN/A
1284378SN/A/// Target gettimeofday() handler.
1285360SN/Atemplate <class OS>
12861450SN/ASyscallReturn
12873114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12882680Sktlim@umich.edu        ThreadContext *tc)
1289360SN/A{
12906701Sgblack@eecs.umich.edu    int index = 0;
12916701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1292360SN/A
1293360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1294360SN/A    tp->tv_sec += seconds_since_epoch;
12956109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12966109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1297360SN/A
12988706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1299360SN/A
13001458SN/A    return 0;
1301360SN/A}
1302360SN/A
1303360SN/A
13041999SN/A/// Target utimes() handler.
13051999SN/Atemplate <class OS>
13061999SN/ASyscallReturn
13073114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13082680Sktlim@umich.edu           ThreadContext *tc)
13091999SN/A{
13101999SN/A    std::string path;
13111999SN/A
13126701Sgblack@eecs.umich.edu    int index = 0;
13138852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13146701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13156701Sgblack@eecs.umich.edu        return -EFAULT;
13166701Sgblack@eecs.umich.edu    }
13171999SN/A
13186701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13196701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13208706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13211999SN/A
13221999SN/A    struct timeval hostTimeval[2];
13231999SN/A    for (int i = 0; i < 2; ++i)
13241999SN/A    {
13258737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13268737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13271999SN/A    }
13283669Sbinkertn@umich.edu
13293669Sbinkertn@umich.edu    // Adjust path for current working directory
13303669Sbinkertn@umich.edu    path = process->fullPath(path);
13313669Sbinkertn@umich.edu
13321999SN/A    int result = utimes(path.c_str(), hostTimeval);
13331999SN/A
13341999SN/A    if (result < 0)
13351999SN/A        return -errno;
13361999SN/A
13371999SN/A    return 0;
13381999SN/A}
1339378SN/A/// Target getrusage() function.
1340360SN/Atemplate <class OS>
13411450SN/ASyscallReturn
13423114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13432680Sktlim@umich.edu              ThreadContext *tc)
1344360SN/A{
13456701Sgblack@eecs.umich.edu    int index = 0;
13466701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13476701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1348360SN/A
13493670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13503670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1351360SN/A    rup->ru_stime.tv_sec = 0;
1352360SN/A    rup->ru_stime.tv_usec = 0;
1353360SN/A    rup->ru_maxrss = 0;
1354360SN/A    rup->ru_ixrss = 0;
1355360SN/A    rup->ru_idrss = 0;
1356360SN/A    rup->ru_isrss = 0;
1357360SN/A    rup->ru_minflt = 0;
1358360SN/A    rup->ru_majflt = 0;
1359360SN/A    rup->ru_nswap = 0;
1360360SN/A    rup->ru_inblock = 0;
1361360SN/A    rup->ru_oublock = 0;
1362360SN/A    rup->ru_msgsnd = 0;
1363360SN/A    rup->ru_msgrcv = 0;
1364360SN/A    rup->ru_nsignals = 0;
1365360SN/A    rup->ru_nvcsw = 0;
1366360SN/A    rup->ru_nivcsw = 0;
1367360SN/A
13683670Sbinkertn@umich.edu    switch (who) {
13693670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
13703670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
13718737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
13728737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
13733670Sbinkertn@umich.edu        break;
13743670Sbinkertn@umich.edu
13753670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
13763670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
13773670Sbinkertn@umich.edu        break;
13783670Sbinkertn@umich.edu
13793670Sbinkertn@umich.edu      default:
13803670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
13813670Sbinkertn@umich.edu        // plow ahead
13823670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
13833670Sbinkertn@umich.edu             who);
13843670Sbinkertn@umich.edu    }
13853670Sbinkertn@umich.edu
13868706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1387360SN/A
13881458SN/A    return 0;
1389360SN/A}
1390360SN/A
13916683Stjones1@inf.ed.ac.uk/// Target times() function.
13926683Stjones1@inf.ed.ac.uktemplate <class OS>
13936683Stjones1@inf.ed.ac.ukSyscallReturn
13946683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13956683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13966683Stjones1@inf.ed.ac.uk{
13976701Sgblack@eecs.umich.edu    int index = 0;
13986701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
13996683Stjones1@inf.ed.ac.uk
14006683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14017823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14026683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14036683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14046683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14056683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14066683Stjones1@inf.ed.ac.uk
14076683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14088737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14096683Stjones1@inf.ed.ac.uk
14106683Stjones1@inf.ed.ac.uk    // Write back
14118706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14126683Stjones1@inf.ed.ac.uk
14136683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14146683Stjones1@inf.ed.ac.uk    return clocks;
14156683Stjones1@inf.ed.ac.uk}
14162553SN/A
14176684Stjones1@inf.ed.ac.uk/// Target time() function.
14186684Stjones1@inf.ed.ac.uktemplate <class OS>
14196684Stjones1@inf.ed.ac.ukSyscallReturn
14206684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14216684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14226684Stjones1@inf.ed.ac.uk{
14236684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14246684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14256684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14266684Stjones1@inf.ed.ac.uk
14276701Sgblack@eecs.umich.edu    int index = 0;
14286701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14296684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14306684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14318737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14328852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14338852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14346684Stjones1@inf.ed.ac.uk    }
14356684Stjones1@inf.ed.ac.uk    return sec;
14366684Stjones1@inf.ed.ac.uk}
14372553SN/A
14382553SN/A
14391354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1440