syscall_emul.hh revision 2021
1360SN/A/*
210027SChris.Adeniyi-Jones@arm.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
310027SChris.Adeniyi-Jones@arm.com * All rights reserved.
410027SChris.Adeniyi-Jones@arm.com *
510027SChris.Adeniyi-Jones@arm.com * Redistribution and use in source and binary forms, with or without
610027SChris.Adeniyi-Jones@arm.com * modification, are permitted provided that the following conditions are
710027SChris.Adeniyi-Jones@arm.com * met: redistributions of source code must retain the above copyright
810027SChris.Adeniyi-Jones@arm.com * notice, this list of conditions and the following disclaimer;
910027SChris.Adeniyi-Jones@arm.com * redistributions in binary form must reproduce the above copyright
1010027SChris.Adeniyi-Jones@arm.com * notice, this list of conditions and the following disclaimer in the
1110027SChris.Adeniyi-Jones@arm.com * documentation and/or other materials provided with the distribution;
1210027SChris.Adeniyi-Jones@arm.com * neither the name of the copyright holders nor the names of its
1310027SChris.Adeniyi-Jones@arm.com * contributors may be used to endorse or promote products derived from
141458SN/A * this software without specific prior written permission.
15360SN/A *
16360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27360SN/A */
28360SN/A
29360SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
30360SN/A#define __SIM_SYSCALL_EMUL_HH__
31360SN/A
32360SN/A///
33360SN/A/// @file syscall_emul.hh
34360SN/A///
35360SN/A/// This file defines objects used to emulate syscalls from the target
36360SN/A/// application on the host machine.
37360SN/A
38360SN/A#include <errno.h>
392665Ssaidi@eecs.umich.edu#include <string>
402665Ssaidi@eecs.umich.edu#ifdef __CYGWIN32__
412665Ssaidi@eecs.umich.edu#include <sys/fcntl.h>	// for O_BINARY
42360SN/A#endif
43360SN/A#include <sys/uio.h>
441354SN/A
451354SN/A#include "base/intmath.hh"	// for RoundUp
46360SN/A#include "mem/functional/functional.hh"
472764Sstever@eecs.umich.edu#include "targetarch/isa_traits.hh"	// for Addr
489202Spalle@lyckegaard.dk
499202Spalle@lyckegaard.dk#include "base/trace.hh"
502064SN/A#include "cpu/exec_context.hh"
51360SN/A#include "sim/process.hh"
52360SN/A
53360SN/A///
54360SN/A/// System call descriptor.
55360SN/A///
56360SN/Aclass SyscallDesc {
571809SN/A
585543Ssaidi@eecs.umich.edu  public:
591809SN/A
603113Sgblack@eecs.umich.edu    /// Typedef for target syscall handler functions.
618229Snate@binkert.org    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
628229Snate@binkert.org                           Process *, ExecContext *);
633113Sgblack@eecs.umich.edu
647075Snate@binkert.org    const char *name;	//!< Syscall name (e.g., "open").
658229Snate@binkert.org    FuncPtr funcPtr;	//!< Pointer to emulation function.
667075Snate@binkert.org    int flags;		//!< Flags (see Flags enum).
67360SN/A
682474SN/A    /// Flag values for controlling syscall behavior.
695543Ssaidi@eecs.umich.edu    enum Flags {
702462SN/A        /// Don't set return regs according to funcPtr return value.
711354SN/A        /// Used for syscalls with non-standard return conventions
726216Snate@binkert.org        /// that explicitly set the ExecContext regs (e.g.,
736658Snate@binkert.org        /// sigreturn).
742474SN/A        SuppressReturnValue = 1
752680Sktlim@umich.edu    };
768232Snate@binkert.org
778229Snate@binkert.org    /// Constructor.
787678Sgblack@eecs.umich.edu    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
7910496Ssteve.reinhardt@amd.com        : name(_name), funcPtr(_funcPtr), flags(_flags)
808229Snate@binkert.org    {
8110497Ssteve.reinhardt@amd.com    }
828766Sgblack@eecs.umich.edu
836640Svince@csl.cornell.edu    /// Emulate the syscall.  Public interface for calling through funcPtr.
84360SN/A    void doSyscall(int callnum, Process *proc, ExecContext *xc);
85360SN/A};
86360SN/A
87360SN/A
88360SN/Aclass BaseBufferArg {
89360SN/A
90360SN/A  public:
91360SN/A
92378SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
931450SN/A    {
943114Sgblack@eecs.umich.edu        bufPtr = new uint8_t[size];
95360SN/A        // clear out buffer: in case we only partially populate this,
965543Ssaidi@eecs.umich.edu        // and then do a copyOut(), we want to make sure we don't
975543Ssaidi@eecs.umich.edu        // introduce any random junk into the simulated address space
985543Ssaidi@eecs.umich.edu        memset(bufPtr, 0, size);
99360SN/A    }
100360SN/A
101360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
102360SN/A
103360SN/A    //
1042680Sktlim@umich.edu    // copy data into simulator space (read from target memory)
105360SN/A    //
106360SN/A    virtual bool copyIn(FunctionalMemory *mem)
107360SN/A    {
108360SN/A        mem->access(Read, addr, bufPtr, size);
109360SN/A        return true;	// no EFAULT detection for now
110360SN/A    }
111360SN/A
112360SN/A    //
113360SN/A    // copy data out of simulator space (write to target memory)
114360SN/A    //
115360SN/A    virtual bool copyOut(FunctionalMemory *mem)
1163114Sgblack@eecs.umich.edu    {
117360SN/A        mem->access(Write, addr, bufPtr, size);
118360SN/A        return true;	// no EFAULT detection for now
119360SN/A    }
120360SN/A
121360SN/A  protected:
122360SN/A    Addr addr;
123360SN/A    int size;
124360SN/A    uint8_t *bufPtr;
125360SN/A};
126360SN/A
127360SN/A
128360SN/Aclass BufferArg : public BaseBufferArg
129378SN/A{
1301706SN/A  public:
1313114Sgblack@eecs.umich.edu    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
132378SN/A    void *bufferPtr()	{ return bufPtr; }
133378SN/A};
134378SN/A
135378SN/Atemplate <class T>
136378SN/Aclass TypedBufferArg : public BaseBufferArg
1371706SN/A{
1383114Sgblack@eecs.umich.edu  public:
1398149SChris.Emmons@ARM.com    // user can optionally specify a specific number of bytes to
1408149SChris.Emmons@ARM.com    // allocate to deal with those structs that have variable-size
141360SN/A    // arrays at the end
1426109Ssanchezd@stanford.edu    TypedBufferArg(Addr _addr, int _size = sizeof(T))
1431706SN/A        : BaseBufferArg(_addr, _size)
1443114Sgblack@eecs.umich.edu    { }
145378SN/A
1466109Ssanchezd@stanford.edu    // type case
1476109Ssanchezd@stanford.edu    operator T*() { return (T *)bufPtr; }
1486109Ssanchezd@stanford.edu
1496109Ssanchezd@stanford.edu    // dereference operators
150378SN/A    T &operator*()	 { return *((T *)bufPtr); }
1511706SN/A    T* operator->()	 { return (T *)bufPtr; }
1523114Sgblack@eecs.umich.edu    T &operator[](int i) { return ((T *)bufPtr)[i]; }
153378SN/A};
1545748SSteve.Reinhardt@amd.com
1555748SSteve.Reinhardt@amd.com//////////////////////////////////////////////////////////////////////
1565748SSteve.Reinhardt@amd.com//
157378SN/A// The following emulation functions are generic enough that they
158378SN/A// don't need to be recompiled for different emulated OS's.  They are
1591706SN/A// defined in sim/syscall_emul.cc.
1603114Sgblack@eecs.umich.edu//
161378SN/A//////////////////////////////////////////////////////////////////////
162378SN/A
1631706SN/A
1643114Sgblack@eecs.umich.edu/// Handler for unimplemented syscalls that we haven't thought about.
165378SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
166378SN/A                                Process *p, ExecContext *xc);
1671706SN/A
1683114Sgblack@eecs.umich.edu/// Handler for unimplemented syscalls that we never intend to
169378SN/A/// implement (signal handling, etc.) and should not affect the correct
170378SN/A/// behavior of the program.  Print a warning only if the appropriate
1711706SN/A/// trace flag is enabled.  Return success to the target program.
1723114Sgblack@eecs.umich.eduSyscallReturn ignoreFunc(SyscallDesc *desc, int num,
173378SN/A                         Process *p, ExecContext *xc);
1744118Sgblack@eecs.umich.edu
1754118Sgblack@eecs.umich.edu/// Target exit() handler: terminate simulation.
1764118Sgblack@eecs.umich.eduSyscallReturn exitFunc(SyscallDesc *desc, int num,
1774118Sgblack@eecs.umich.edu                       Process *p, ExecContext *xc);
178378SN/A
1791706SN/A/// Target getpagesize() handler.
1803114Sgblack@eecs.umich.eduSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
181378SN/A                              Process *p, ExecContext *xc);
182378SN/A
1831706SN/A/// Target obreak() handler: set brk address.
1843114Sgblack@eecs.umich.eduSyscallReturn obreakFunc(SyscallDesc *desc, int num,
185360SN/A                         Process *p, ExecContext *xc);
1865513SMichael.Adler@intel.com
1875513SMichael.Adler@intel.com/// Target close() handler.
1885513SMichael.Adler@intel.comSyscallReturn closeFunc(SyscallDesc *desc, int num,
1895513SMichael.Adler@intel.com                        Process *p, ExecContext *xc);
19010203SAli.Saidi@ARM.com
19110203SAli.Saidi@ARM.com/// Target read() handler.
19210203SAli.Saidi@ARM.comSyscallReturn readFunc(SyscallDesc *desc, int num,
19310203SAli.Saidi@ARM.com                       Process *p, ExecContext *xc);
1945513SMichael.Adler@intel.com
1955513SMichael.Adler@intel.com/// Target write() handler.
1965513SMichael.Adler@intel.comSyscallReturn writeFunc(SyscallDesc *desc, int num,
197511SN/A                        Process *p, ExecContext *xc);
19810633Smichaelupton@gmail.com
19910633Smichaelupton@gmail.com/// Target lseek() handler.
20010633Smichaelupton@gmail.comSyscallReturn lseekFunc(SyscallDesc *desc, int num,
2011706SN/A                        Process *p, ExecContext *xc);
2023114Sgblack@eecs.umich.edu
203511SN/A/// Target munmap() handler.
2045513SMichael.Adler@intel.comSyscallReturn munmapFunc(SyscallDesc *desc, int num,
2055513SMichael.Adler@intel.com                         Process *p, ExecContext *xc);
2065513SMichael.Adler@intel.com
2075513SMichael.Adler@intel.com/// Target gethostname() handler.
208511SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2091706SN/A                              Process *p, ExecContext *xc);
2103114Sgblack@eecs.umich.edu
2111706SN/A/// Target unlink() handler.
2121706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2131706SN/A                         Process *p, ExecContext *xc);
2141706SN/A
2153114Sgblack@eecs.umich.edu/// Target rename() handler.
2161706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2171706SN/A                         Process *p, ExecContext *xc);
2181706SN/A
2191706SN/A
2203114Sgblack@eecs.umich.edu/// Target truncate() handler.
2211706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
222511SN/A                           Process *p, ExecContext *xc);
2236703Svince@csl.cornell.edu
2246703Svince@csl.cornell.edu
2256703Svince@csl.cornell.edu/// Target ftruncate() handler.
2266703Svince@csl.cornell.eduSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2276685Stjones1@inf.ed.ac.uk                            Process *p, ExecContext *xc);
2286685Stjones1@inf.ed.ac.uk
2296685Stjones1@inf.ed.ac.uk
2306685Stjones1@inf.ed.ac.uk/// Target chown() handler.
2316685Stjones1@inf.ed.ac.ukSyscallReturn chownFunc(SyscallDesc *desc, int num,
2325513SMichael.Adler@intel.com                        Process *p, ExecContext *xc);
2335513SMichael.Adler@intel.com
2345513SMichael.Adler@intel.com
2355513SMichael.Adler@intel.com/// Target fchown() handler.
2365513SMichael.Adler@intel.comSyscallReturn fchownFunc(SyscallDesc *desc, int num,
2371999SN/A                         Process *p, ExecContext *xc);
2381999SN/A
2393114Sgblack@eecs.umich.edu/// This struct is used to build an target-OS-dependent table that
2401999SN/A/// maps the target's open() flags to the host open() flags.
2411999SN/Astruct OpenFlagTransTable {
2421999SN/A    int tgtFlag;	//!< Target system flag value.
2431999SN/A    int hostFlag;	//!< Corresponding host system flag value.
2443114Sgblack@eecs.umich.edu};
2451999SN/A
2463079Sstever@eecs.umich.edu
2473079Sstever@eecs.umich.edu
2483114Sgblack@eecs.umich.edu/// A readable name for 1,000,000, for converting microseconds to seconds.
2493079Sstever@eecs.umich.educonst int one_million = 1000000;
2502093SN/A
2512093SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
2523114Sgblack@eecs.umich.edu/// by my reckoning.  We want to keep this a constant (not use the
2532093SN/A/// real-world time) to keep simulations repeatable.
2542687Sksewell@umich.educonst unsigned seconds_since_epoch = 1000000000;
2552687Sksewell@umich.edu
2563114Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
2572687Sksewell@umich.edu/// microseconds.
2582238SN/Atemplate <class T1, class T2>
2592238SN/Avoid
2603114Sgblack@eecs.umich.edugetElapsedTime(T1 &sec, T2 &usec)
2612238SN/A{
2622238SN/A    int elapsed_usecs = curTick / Clock::Int::us;
2632238SN/A    sec = elapsed_usecs / one_million;
2643114Sgblack@eecs.umich.edu    usec = elapsed_usecs % one_million;
2652238SN/A}
2662238SN/A
2672238SN/A//////////////////////////////////////////////////////////////////////
2683114Sgblack@eecs.umich.edu//
2692238SN/A// The following emulation functions are generic, but need to be
2702238SN/A// templated to account for differences in types, constants, etc.
2712238SN/A//
2723114Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////
2732238SN/A
2742238SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
2752238SN/A/// only to find out if their stdout is a tty, to determine whether to
2763114Sgblack@eecs.umich.edu/// do line or block buffering.
2772238SN/Atemplate <class OS>
2782238SN/ASyscallReturn
2792238SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process,
2803114Sgblack@eecs.umich.edu          ExecContext *xc)
2812238SN/A{
2822238SN/A    int fd = xc->getSyscallArg(0);
2832238SN/A    unsigned req = xc->getSyscallArg(1);
2843114Sgblack@eecs.umich.edu
2852238SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
2866109Ssanchezd@stanford.edu
2876109Ssanchezd@stanford.edu    if (fd < 0 || process->sim_fd(fd) < 0) {
2886109Ssanchezd@stanford.edu        // doesn't map to any simulator fd: not a valid target fd
2892238SN/A        return -EBADF;
2909455Smitch.hayenga+gem5@gmail.com    }
2919455Smitch.hayenga+gem5@gmail.com
2929455Smitch.hayenga+gem5@gmail.com    switch (req) {
29310203SAli.Saidi@ARM.com      case OS::TIOCISATTY:
29410203SAli.Saidi@ARM.com      case OS::TIOCGETP:
29510203SAli.Saidi@ARM.com      case OS::TIOCSETP:
2969455Smitch.hayenga+gem5@gmail.com      case OS::TIOCSETN:
2979112Smarc.orr@gmail.com      case OS::TIOCSETC:
2989112Smarc.orr@gmail.com      case OS::TIOCGETC:
2999112Smarc.orr@gmail.com      case OS::TIOCGETS:
3009112Smarc.orr@gmail.com      case OS::TIOCGETA:
3019112Smarc.orr@gmail.com        return -ENOTTY;
3029112Smarc.orr@gmail.com
3039112Smarc.orr@gmail.com      default:
3049112Smarc.orr@gmail.com        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
3059112Smarc.orr@gmail.com              fd, req, xc->readPC());
3069112Smarc.orr@gmail.com    }
3079112Smarc.orr@gmail.com}
3089112Smarc.orr@gmail.com
3099112Smarc.orr@gmail.com/// Target open() handler.
3109112Smarc.orr@gmail.comtemplate <class OS>
3119112Smarc.orr@gmail.comSyscallReturn
3129112Smarc.orr@gmail.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
3139112Smarc.orr@gmail.com         ExecContext *xc)
3149112Smarc.orr@gmail.com{
3159112Smarc.orr@gmail.com    std::string path;
3169112Smarc.orr@gmail.com
3179112Smarc.orr@gmail.com    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
3189112Smarc.orr@gmail.com        return -EFAULT;
3199112Smarc.orr@gmail.com
3209112Smarc.orr@gmail.com    if (path == "/dev/sysdev0") {
3219238Slluc.alvarez@bsc.es        // This is a memory-mapped high-resolution timer device on Alpha.
3229112Smarc.orr@gmail.com        // We don't support it, so just punt.
3239112Smarc.orr@gmail.com        warn("Ignoring open(%s, ...)\n", path);
3249112Smarc.orr@gmail.com        return -ENOENT;
3259112Smarc.orr@gmail.com    }
3269112Smarc.orr@gmail.com
3279112Smarc.orr@gmail.com    int tgtFlags = xc->getSyscallArg(1);
3289112Smarc.orr@gmail.com    int mode = xc->getSyscallArg(2);
3299112Smarc.orr@gmail.com    int hostFlags = 0;
3309112Smarc.orr@gmail.com
3319112Smarc.orr@gmail.com    // translate open flags
3329112Smarc.orr@gmail.com    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
3339112Smarc.orr@gmail.com        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
3349112Smarc.orr@gmail.com            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
3359112Smarc.orr@gmail.com            hostFlags |= OS::openFlagTable[i].hostFlag;
3369112Smarc.orr@gmail.com        }
3379112Smarc.orr@gmail.com    }
3389112Smarc.orr@gmail.com
3399112Smarc.orr@gmail.com    // any target flags left?
3409112Smarc.orr@gmail.com    if (tgtFlags != 0)
3419112Smarc.orr@gmail.com        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
3429112Smarc.orr@gmail.com
3439112Smarc.orr@gmail.com#ifdef __CYGWIN32__
3449112Smarc.orr@gmail.com    hostFlags |= O_BINARY;
3459112Smarc.orr@gmail.com#endif
3469112Smarc.orr@gmail.com
3479112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
3489112Smarc.orr@gmail.com
3499112Smarc.orr@gmail.com    // open the file
3509112Smarc.orr@gmail.com    int fd = open(path.c_str(), hostFlags, mode);
3519112Smarc.orr@gmail.com
3529112Smarc.orr@gmail.com    return (fd == -1) ? -errno : process->alloc_fd(fd);
3539112Smarc.orr@gmail.com}
3549112Smarc.orr@gmail.com
3559112Smarc.orr@gmail.com
3569112Smarc.orr@gmail.com/// Target chmod() handler.
3579112Smarc.orr@gmail.comtemplate <class OS>
3589112Smarc.orr@gmail.comSyscallReturn
3599112Smarc.orr@gmail.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
3609112Smarc.orr@gmail.com          ExecContext *xc)
3619112Smarc.orr@gmail.com{
3629112Smarc.orr@gmail.com    std::string path;
3639112Smarc.orr@gmail.com
3649112Smarc.orr@gmail.com    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
3659112Smarc.orr@gmail.com        return -EFAULT;
3669112Smarc.orr@gmail.com
3679112Smarc.orr@gmail.com    uint32_t mode = xc->getSyscallArg(1);
3689112Smarc.orr@gmail.com    mode_t hostMode = 0;
3699112Smarc.orr@gmail.com
3709112Smarc.orr@gmail.com    // XXX translate mode flags via OS::something???
3719112Smarc.orr@gmail.com    hostMode = mode;
3729112Smarc.orr@gmail.com
3739238Slluc.alvarez@bsc.es    // do the chmod
3749112Smarc.orr@gmail.com    int result = chmod(path.c_str(), hostMode);
3759112Smarc.orr@gmail.com    if (result < 0)
3769112Smarc.orr@gmail.com        return errno;
3779112Smarc.orr@gmail.com
3789112Smarc.orr@gmail.com    return 0;
3792238SN/A}
3802238SN/A
3812238SN/A
3822238SN/A/// Target fchmod() handler.
3833114Sgblack@eecs.umich.edutemplate <class OS>
3842238SN/ASyscallReturn
3852238SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *process,
3862238SN/A           ExecContext *xc)
3873114Sgblack@eecs.umich.edu{
3882238SN/A    int fd = xc->getSyscallArg(0);
3892238SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
3902238SN/A        // doesn't map to any simulator fd: not a valid target fd
3913114Sgblack@eecs.umich.edu        return -EBADF;
3922238SN/A    }
3932238SN/A
3942238SN/A    uint32_t mode = xc->getSyscallArg(1);
3953114Sgblack@eecs.umich.edu    mode_t hostMode = 0;
3962238SN/A
3972238SN/A    // XXX translate mode flags via OS::someting???
3981354SN/A    hostMode = mode;
3991354SN/A
4001354SN/A    // do the fchmod
4011354SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
4021354SN/A    if (result < 0)
4031354SN/A        return errno;
4041354SN/A
4051354SN/A    return 0;
4061354SN/A}
4071354SN/A
4081354SN/A
4091354SN/A/// Target stat() handler.
4101354SN/Atemplate <class OS>
4111354SN/ASyscallReturn
4127823Ssteve.reinhardt@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
4131354SN/A         ExecContext *xc)
4141354SN/A{
4151354SN/A    std::string path;
4161354SN/A
417360SN/A    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
418360SN/A        return -EFAULT;
419360SN/A
420360SN/A    struct stat hostBuf;
421360SN/A    int result = stat(path.c_str(), &hostBuf);
422360SN/A
423360SN/A    if (result < 0)
4243113Sgblack@eecs.umich.edu        return errno;
4253113Sgblack@eecs.umich.edu
4263113Sgblack@eecs.umich.edu    OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
4273113Sgblack@eecs.umich.edu
4283113Sgblack@eecs.umich.edu    return 0;
4293113Sgblack@eecs.umich.edu}
4303113Sgblack@eecs.umich.edu
4313113Sgblack@eecs.umich.edu
4323113Sgblack@eecs.umich.edu/// Target fstat64() handler.
4333113Sgblack@eecs.umich.edutemplate <class OS>
4343113Sgblack@eecs.umich.eduSyscallReturn
4353113Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, Process *process,
4363113Sgblack@eecs.umich.edu            ExecContext *xc)
4373113Sgblack@eecs.umich.edu{
4383113Sgblack@eecs.umich.edu    int fd = xc->getSyscallArg(0);
4393113Sgblack@eecs.umich.edu    if (fd < 0 || process->sim_fd(fd) < 0) {
4404189Sgblack@eecs.umich.edu        // doesn't map to any simulator fd: not a valid target fd
4414189Sgblack@eecs.umich.edu        return -EBADF;
4423113Sgblack@eecs.umich.edu    }
4433113Sgblack@eecs.umich.edu
4443113Sgblack@eecs.umich.edu    struct stat64 hostBuf;
4453113Sgblack@eecs.umich.edu    int result = fstat64(process->sim_fd(fd), &hostBuf);
4468737Skoansin.tan@gmail.com
4473113Sgblack@eecs.umich.edu    if (result < 0)
4488737Skoansin.tan@gmail.com        return errno;
4493277Sgblack@eecs.umich.edu
4505515SMichael.Adler@intel.com    OS::copyOutStat64Buf(xc->mem, xc->getSyscallArg(1), &hostBuf);
4515515SMichael.Adler@intel.com
4525515SMichael.Adler@intel.com    return 0;
4535515SMichael.Adler@intel.com}
4545515SMichael.Adler@intel.com
4558737Skoansin.tan@gmail.com
4563277Sgblack@eecs.umich.edu/// Target lstat() handler.
4578737Skoansin.tan@gmail.comtemplate <class OS>
4583277Sgblack@eecs.umich.eduSyscallReturn
4598737Skoansin.tan@gmail.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
4603277Sgblack@eecs.umich.edu          ExecContext *xc)
4618737Skoansin.tan@gmail.com{
4623113Sgblack@eecs.umich.edu    std::string path;
4633113Sgblack@eecs.umich.edu
4643113Sgblack@eecs.umich.edu    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
4653113Sgblack@eecs.umich.edu        return -EFAULT;
4668737Skoansin.tan@gmail.com
4673113Sgblack@eecs.umich.edu    struct stat hostBuf;
4688737Skoansin.tan@gmail.com    int result = lstat(path.c_str(), &hostBuf);
4693114Sgblack@eecs.umich.edu
4708737Skoansin.tan@gmail.com    if (result < 0)
4713114Sgblack@eecs.umich.edu        return -errno;
4728737Skoansin.tan@gmail.com
4733114Sgblack@eecs.umich.edu    OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
4748737Skoansin.tan@gmail.com
4754061Sgblack@eecs.umich.edu    return 0;
4764061Sgblack@eecs.umich.edu}
4774061Sgblack@eecs.umich.edu
4788737Skoansin.tan@gmail.com/// Target lstat64() handler.
4793113Sgblack@eecs.umich.edutemplate <class OS>
4808737Skoansin.tan@gmail.comSyscallReturn
4813113Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, Process *process,
4823113Sgblack@eecs.umich.edu            ExecContext *xc)
4833113Sgblack@eecs.umich.edu{
4843113Sgblack@eecs.umich.edu    std::string path;
4853113Sgblack@eecs.umich.edu
4863113Sgblack@eecs.umich.edu    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
4873113Sgblack@eecs.umich.edu        return -EFAULT;
4883113Sgblack@eecs.umich.edu
4894189Sgblack@eecs.umich.edu    struct stat64 hostBuf;
4904189Sgblack@eecs.umich.edu    int result = lstat64(path.c_str(), &hostBuf);
4913113Sgblack@eecs.umich.edu
4923113Sgblack@eecs.umich.edu    if (result < 0)
4933113Sgblack@eecs.umich.edu        return -errno;
4948737Skoansin.tan@gmail.com
4953113Sgblack@eecs.umich.edu    OS::copyOutStat64Buf(xc->mem, xc->getSyscallArg(1), &hostBuf);
4968737Skoansin.tan@gmail.com
4973113Sgblack@eecs.umich.edu    return 0;
4988737Skoansin.tan@gmail.com}
4993113Sgblack@eecs.umich.edu
5003113Sgblack@eecs.umich.edu/// Target fstat() handler.
5013113Sgblack@eecs.umich.edutemplate <class OS>
5023113Sgblack@eecs.umich.eduSyscallReturn
5033113Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *process,
5043113Sgblack@eecs.umich.edu          ExecContext *xc)
5053113Sgblack@eecs.umich.edu{
5063113Sgblack@eecs.umich.edu    int fd = process->sim_fd(xc->getSyscallArg(0));
5073113Sgblack@eecs.umich.edu
5083113Sgblack@eecs.umich.edu    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
5098852Sandreas.hansson@arm.com
5103113Sgblack@eecs.umich.edu    if (fd < 0)
5113113Sgblack@eecs.umich.edu        return -EBADF;
5123113Sgblack@eecs.umich.edu
5133113Sgblack@eecs.umich.edu    struct stat hostBuf;
5143113Sgblack@eecs.umich.edu    int result = fstat(fd, &hostBuf);
5153113Sgblack@eecs.umich.edu
5163113Sgblack@eecs.umich.edu    if (result < 0)
5173113Sgblack@eecs.umich.edu        return -errno;
5183113Sgblack@eecs.umich.edu
5193113Sgblack@eecs.umich.edu    OS::copyOutStatBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
5208852Sandreas.hansson@arm.com
5213113Sgblack@eecs.umich.edu    return 0;
5223113Sgblack@eecs.umich.edu}
5233113Sgblack@eecs.umich.edu
5243113Sgblack@eecs.umich.edu
5256686Stjones1@inf.ed.ac.uk/// Target statfs() handler.
5263113Sgblack@eecs.umich.edutemplate <class OS>
5273113Sgblack@eecs.umich.eduSyscallReturn
5283113Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, Process *process,
529378SN/A           ExecContext *xc)
530378SN/A{
5319141Smarc.orr@gmail.com    std::string path;
5329141Smarc.orr@gmail.com
533360SN/A    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
5341450SN/A        return -EFAULT;
5353114Sgblack@eecs.umich.edu
5362680Sktlim@umich.edu    struct statfs hostBuf;
537360SN/A    int result = statfs(path.c_str(), &hostBuf);
5386701Sgblack@eecs.umich.edu
5396701Sgblack@eecs.umich.edu    if (result < 0)
5406701Sgblack@eecs.umich.edu        return errno;
541360SN/A
5421969SN/A    OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
543360SN/A
54410496Ssteve.reinhardt@amd.com    return 0;
54510496Ssteve.reinhardt@amd.com}
54610496Ssteve.reinhardt@amd.com
547360SN/A
5481458SN/A/// Target fstatfs() handler.
549360SN/Atemplate <class OS>
550360SN/ASyscallReturn
55110496Ssteve.reinhardt@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
55210496Ssteve.reinhardt@amd.com            ExecContext *xc)
55310496Ssteve.reinhardt@amd.com{
55410496Ssteve.reinhardt@amd.com    int fd = process->sim_fd(xc->getSyscallArg(0));
5559141Smarc.orr@gmail.com
5561458SN/A    if (fd < 0)
5579141Smarc.orr@gmail.com        return -EBADF;
558360SN/A
5599141Smarc.orr@gmail.com    struct statfs hostBuf;
5609141Smarc.orr@gmail.com    int result = fstatfs(fd, &hostBuf);
5619141Smarc.orr@gmail.com
562360SN/A    if (result < 0)
563360SN/A        return errno;
564360SN/A
56510027SChris.Adeniyi-Jones@arm.com    OS::copyOutStatfsBuf(xc->mem, xc->getSyscallArg(1), &hostBuf);
5663114Sgblack@eecs.umich.edu
56710027SChris.Adeniyi-Jones@arm.com    return 0;
568360SN/A}
569360SN/A
570360SN/A
5718852Sandreas.hansson@arm.com/// Target writev() handler.
5726701Sgblack@eecs.umich.edutemplate <class OS>
5731458SN/ASyscallReturn
574360SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process,
5756701Sgblack@eecs.umich.edu           ExecContext *xc)
5766701Sgblack@eecs.umich.edu{
577360SN/A    int fd = xc->getSyscallArg(0);
578360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
579360SN/A        // doesn't map to any simulator fd: not a valid target fd
580360SN/A        return -EBADF;
581360SN/A    }
582360SN/A
583360SN/A    uint64_t tiov_base = xc->getSyscallArg(1);
584360SN/A    size_t count = xc->getSyscallArg(2);
585360SN/A    struct iovec hiov[count];
586360SN/A    for (int i = 0; i < count; ++i)
587360SN/A    {
588360SN/A        typename OS::tgt_iovec tiov;
5891706SN/A        xc->mem->access(Read, tiov_base + i*sizeof(typename OS::tgt_iovec),
590360SN/A                        &tiov, sizeof(typename OS::tgt_iovec));
591360SN/A        hiov[i].iov_len = tiov.iov_len;
592360SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
593360SN/A        xc->mem->access(Read, tiov.iov_base,
594360SN/A                        hiov[i].iov_base, hiov[i].iov_len);
5953669Sbinkertn@umich.edu    }
5963669Sbinkertn@umich.edu
5973669Sbinkertn@umich.edu    int result = writev(process->sim_fd(fd), hiov, count);
5981706SN/A
5991706SN/A    for (int i = 0; i < count; ++i)
60010496Ssteve.reinhardt@amd.com    {
60110496Ssteve.reinhardt@amd.com        delete [] (char *)hiov[i].iov_base;
60210496Ssteve.reinhardt@amd.com    }
60310496Ssteve.reinhardt@amd.com
60410496Ssteve.reinhardt@amd.com    if (result < 0)
60510496Ssteve.reinhardt@amd.com        return errno;
60610496Ssteve.reinhardt@amd.com
60710496Ssteve.reinhardt@amd.com    return 0;
60810496Ssteve.reinhardt@amd.com}
60910496Ssteve.reinhardt@amd.com
61010496Ssteve.reinhardt@amd.com
61110496Ssteve.reinhardt@amd.com/// Target mmap() handler.
61210496Ssteve.reinhardt@amd.com///
61310496Ssteve.reinhardt@amd.com/// We don't really handle mmap().  If the target is mmaping an
61410496Ssteve.reinhardt@amd.com/// anonymous region or /dev/zero, we can get away with doing basically
61510496Ssteve.reinhardt@amd.com/// nothing (since memory is initialized to zero and the simulator
61610496Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).  Always print a warning,
61710496Ssteve.reinhardt@amd.com/// since this could be seriously broken if we're not mapping
61810496Ssteve.reinhardt@amd.com/// /dev/zero.
61910496Ssteve.reinhardt@amd.com//
6205795Ssaidi@eecs.umich.edu/// Someday we should explicitly check for /dev/zero in open, flag the
6219143Ssteve.reinhardt@amd.com/// file descriptor, and fail (or implement!) a non-anonymous mmap to
6229142Ssteve.reinhardt@amd.com/// anything else.
6239142Ssteve.reinhardt@amd.comtemplate <class OS>
6249143Ssteve.reinhardt@amd.comSyscallReturn
6255795Ssaidi@eecs.umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ExecContext *xc)
6269143Ssteve.reinhardt@amd.com{
6275795Ssaidi@eecs.umich.edu    Addr start = xc->getSyscallArg(0);
6285795Ssaidi@eecs.umich.edu    uint64_t length = xc->getSyscallArg(1);
6295795Ssaidi@eecs.umich.edu    // int prot = xc->getSyscallArg(2);
6309143Ssteve.reinhardt@amd.com    int flags = xc->getSyscallArg(3);
6315795Ssaidi@eecs.umich.edu    // int fd = p->sim_fd(xc->getSyscallArg(4));
632360SN/A    // int offset = xc->getSyscallArg(5);
6339143Ssteve.reinhardt@amd.com
6349143Ssteve.reinhardt@amd.com    if (start == 0) {
6359143Ssteve.reinhardt@amd.com        // user didn't give an address... pick one from our "mmap region"
6369143Ssteve.reinhardt@amd.com        start = p->mmap_end;
637360SN/A        p->mmap_end += roundUp(length, VMPageSize);
638360SN/A        if (p->nxm_start != 0) {
63910027SChris.Adeniyi-Jones@arm.com            //If we have an nxm space, make sure we haven't colided
64010027SChris.Adeniyi-Jones@arm.com            assert(p->mmap_end < p->nxm_start);
64110027SChris.Adeniyi-Jones@arm.com        }
64210027SChris.Adeniyi-Jones@arm.com    }
64310027SChris.Adeniyi-Jones@arm.com
64410027SChris.Adeniyi-Jones@arm.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
64510027SChris.Adeniyi-Jones@arm.com        warn("allowing mmap of file @ fd %d. "
64610027SChris.Adeniyi-Jones@arm.com             "This will break if not /dev/zero.", xc->getSyscallArg(4));
64710027SChris.Adeniyi-Jones@arm.com    }
64810027SChris.Adeniyi-Jones@arm.com
64910027SChris.Adeniyi-Jones@arm.com    return start;
65010027SChris.Adeniyi-Jones@arm.com}
65110027SChris.Adeniyi-Jones@arm.com
65210027SChris.Adeniyi-Jones@arm.com/// Target getrlimit() handler.
65310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
65410027SChris.Adeniyi-Jones@arm.comSyscallReturn
65510027SChris.Adeniyi-Jones@arm.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
65610027SChris.Adeniyi-Jones@arm.com              ExecContext *xc)
65710027SChris.Adeniyi-Jones@arm.com{
65810027SChris.Adeniyi-Jones@arm.com    unsigned resource = xc->getSyscallArg(0);
65910027SChris.Adeniyi-Jones@arm.com    TypedBufferArg<typename OS::rlimit> rlp(xc->getSyscallArg(1));
66010027SChris.Adeniyi-Jones@arm.com
66110633Smichaelupton@gmail.com    switch (resource) {
66210633Smichaelupton@gmail.com      case OS::RLIMIT_STACK:
66310633Smichaelupton@gmail.com        // max stack size in bytes: make up a number (2MB for now)
66410633Smichaelupton@gmail.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
66510633Smichaelupton@gmail.com        break;
66610633Smichaelupton@gmail.com
66710633Smichaelupton@gmail.com      default:
66810633Smichaelupton@gmail.com        std::cerr << "getrlimitFunc: unimplemented resource " << resource
66910633Smichaelupton@gmail.com                  << std::endl;
67010633Smichaelupton@gmail.com        abort();
67110633Smichaelupton@gmail.com        break;
67210633Smichaelupton@gmail.com    }
67310633Smichaelupton@gmail.com
67410633Smichaelupton@gmail.com    rlp.copyOut(xc->mem);
67510203SAli.Saidi@ARM.com    return 0;
67610203SAli.Saidi@ARM.com}
67710203SAli.Saidi@ARM.com
67810203SAli.Saidi@ARM.com/// Target gettimeofday() handler.
67910203SAli.Saidi@ARM.comtemplate <class OS>
68010203SAli.Saidi@ARM.comSyscallReturn
68110203SAli.Saidi@ARM.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
68210203SAli.Saidi@ARM.com                 ExecContext *xc)
68310203SAli.Saidi@ARM.com{
68410203SAli.Saidi@ARM.com    TypedBufferArg<typename OS::timeval> tp(xc->getSyscallArg(0));
68510203SAli.Saidi@ARM.com
68610203SAli.Saidi@ARM.com    getElapsedTime(tp->tv_sec, tp->tv_usec);
68710203SAli.Saidi@ARM.com    tp->tv_sec += seconds_since_epoch;
68810203SAli.Saidi@ARM.com
68910203SAli.Saidi@ARM.com    tp.copyOut(xc->mem);
69010203SAli.Saidi@ARM.com
69110203SAli.Saidi@ARM.com    return 0;
69210203SAli.Saidi@ARM.com}
69310203SAli.Saidi@ARM.com
69410203SAli.Saidi@ARM.com
69510203SAli.Saidi@ARM.com/// Target utimes() handler.
69610203SAli.Saidi@ARM.comtemplate <class OS>
69710203SAli.Saidi@ARM.comSyscallReturn
69810203SAli.Saidi@ARM.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
69910203SAli.Saidi@ARM.com           ExecContext *xc)
70010203SAli.Saidi@ARM.com{
7016640Svince@csl.cornell.edu    std::string path;
7026640Svince@csl.cornell.edu
7036640Svince@csl.cornell.edu    if (xc->mem->readString(path, xc->getSyscallArg(0)) != No_Fault)
7046640Svince@csl.cornell.edu        return -EFAULT;
7056640Svince@csl.cornell.edu
7066640Svince@csl.cornell.edu    TypedBufferArg<typename OS::timeval [2]> tp(xc->getSyscallArg(1));
7076640Svince@csl.cornell.edu    tp.copyIn(xc->mem);
7086701Sgblack@eecs.umich.edu
7096701Sgblack@eecs.umich.edu    struct timeval hostTimeval[2];
71010793Sbrandon.potter@amd.com    for (int i = 0; i < 2; ++i)
7116640Svince@csl.cornell.edu    {
7126701Sgblack@eecs.umich.edu        hostTimeval[i].tv_sec = (*tp)[i].tv_sec;
7136701Sgblack@eecs.umich.edu        hostTimeval[i].tv_usec = (*tp)[i].tv_usec;
7146640Svince@csl.cornell.edu    }
7158706Sandreas.hansson@arm.com    int result = utimes(path.c_str(), hostTimeval);
7166640Svince@csl.cornell.edu
7176701Sgblack@eecs.umich.edu    if (result < 0)
7186640Svince@csl.cornell.edu        return -errno;
719360SN/A
7201999SN/A    return 0;
7211999SN/A}
7221999SN/A
7233114Sgblack@eecs.umich.edu/// Target getrusage() function.
7242680Sktlim@umich.edutemplate <class OS>
7251999SN/ASyscallReturn
7261999SN/AgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
7271999SN/A              ExecContext *xc)
7286701Sgblack@eecs.umich.edu{
7298852Sandreas.hansson@arm.com    int who = xc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
7306701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(xc->getSyscallArg(1));
7311999SN/A
7326701Sgblack@eecs.umich.edu    if (who != OS::RUSAGE_SELF) {
7331999SN/A        // don't really handle THREAD or CHILDREN, but just warn and
7346701Sgblack@eecs.umich.edu        // plow ahead
7351999SN/A        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
7361999SN/A             who);
7371999SN/A    }
7381999SN/A
7391999SN/A    getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
7403669Sbinkertn@umich.edu    rup->ru_stime.tv_sec = 0;
7413669Sbinkertn@umich.edu    rup->ru_stime.tv_usec = 0;
7423669Sbinkertn@umich.edu    rup->ru_maxrss = 0;
7431999SN/A    rup->ru_ixrss = 0;
7441999SN/A    rup->ru_idrss = 0;
7451999SN/A    rup->ru_isrss = 0;
7462218SN/A    rup->ru_minflt = 0;
7471999SN/A    rup->ru_majflt = 0;
7481999SN/A    rup->ru_nswap = 0;
7491999SN/A    rup->ru_inblock = 0;
7501999SN/A    rup->ru_oublock = 0;
7511999SN/A    rup->ru_msgsnd = 0;
7521999SN/A    rup->ru_msgrcv = 0;
7531999SN/A    rup->ru_nsignals = 0;
7541999SN/A    rup->ru_nvcsw = 0;
7553114Sgblack@eecs.umich.edu    rup->ru_nivcsw = 0;
7562680Sktlim@umich.edu
7571999SN/A    rup.copyOut(xc->mem);
7586701Sgblack@eecs.umich.edu
7596701Sgblack@eecs.umich.edu    return 0;
7601999SN/A}
7611999SN/A
7621999SN/A#endif // __SIM_SYSCALL_EMUL_HH__
7631999SN/A