syscall_emul.hh revision 3079
1360SN/A/*
21458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
3360SN/A * All rights reserved.
4360SN/A *
5360SN/A * Redistribution and use in source and binary forms, with or without
6360SN/A * modification, are permitted provided that the following conditions are
7360SN/A * met: redistributions of source code must retain the above copyright
8360SN/A * notice, this list of conditions and the following disclaimer;
9360SN/A * redistributions in binary form must reproduce the above copyright
10360SN/A * notice, this list of conditions and the following disclaimer in the
11360SN/A * documentation and/or other materials provided with the distribution;
12360SN/A * neither the name of the copyright holders nor the names of its
13360SN/A * contributors may be used to endorse or promote products derived from
14360SN/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.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Kevin Lim
30360SN/A */
31360SN/A
321354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
331354SN/A#define __SIM_SYSCALL_EMUL_HH__
34360SN/A
352764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
362764Sstever@eecs.umich.edu                   defined(__FreeBSD__) || defined(__CYGWIN__))
372064SN/A
38360SN/A///
39360SN/A/// @file syscall_emul.hh
40360SN/A///
41360SN/A/// This file defines objects used to emulate syscalls from the target
42360SN/A/// application on the host machine.
43360SN/A
441354SN/A#include <errno.h>
45360SN/A#include <string>
461809SN/A#ifdef __CYGWIN32__
471809SN/A#include <sys/fcntl.h>	// for O_BINARY
481809SN/A#endif
491999SN/A#include <sys/uio.h>
50360SN/A
512474SN/A#include "arch/isa_traits.hh"	// for Addr
522474SN/A#include "base/chunk_generator.hh"
53360SN/A#include "base/intmath.hh"	// for RoundUp
542462SN/A#include "base/misc.hh"
551354SN/A#include "base/trace.hh"
562474SN/A#include "cpu/base.hh"
572680Sktlim@umich.edu#include "cpu/thread_context.hh"
582474SN/A#include "mem/translating_port.hh"
592474SN/A#include "mem/page_table.hh"
601354SN/A#include "sim/process.hh"
61360SN/A
62360SN/A///
63360SN/A/// System call descriptor.
64360SN/A///
65360SN/Aclass SyscallDesc {
66360SN/A
67360SN/A  public:
68360SN/A
69378SN/A    /// Typedef for target syscall handler functions.
701450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
712680Sktlim@umich.edu                           Process *, ThreadContext *);
72360SN/A
73360SN/A    const char *name;	//!< Syscall name (e.g., "open").
74360SN/A    FuncPtr funcPtr;	//!< Pointer to emulation function.
75360SN/A    int flags;		//!< Flags (see Flags enum).
76360SN/A
77360SN/A    /// Flag values for controlling syscall behavior.
78360SN/A    enum Flags {
79360SN/A        /// Don't set return regs according to funcPtr return value.
80360SN/A        /// Used for syscalls with non-standard return conventions
812680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
82360SN/A        /// sigreturn).
83360SN/A        SuppressReturnValue = 1
84360SN/A    };
85360SN/A
86360SN/A    /// Constructor.
87360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
88360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
89360SN/A    {
90360SN/A    }
91360SN/A
92360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
932680Sktlim@umich.edu    void doSyscall(int callnum, Process *proc, ThreadContext *tc);
94360SN/A};
95360SN/A
96360SN/A
97360SN/Aclass BaseBufferArg {
98360SN/A
99360SN/A  public:
100360SN/A
101360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
102360SN/A    {
103360SN/A        bufPtr = new uint8_t[size];
104360SN/A        // clear out buffer: in case we only partially populate this,
105360SN/A        // and then do a copyOut(), we want to make sure we don't
106360SN/A        // introduce any random junk into the simulated address space
107360SN/A        memset(bufPtr, 0, size);
108360SN/A    }
109360SN/A
110360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
111360SN/A
112360SN/A    //
113360SN/A    // copy data into simulator space (read from target memory)
114360SN/A    //
1152400SN/A    virtual bool copyIn(TranslatingPort *memport)
116360SN/A    {
1172461SN/A        memport->readBlob(addr, bufPtr, size);
118360SN/A        return true;	// no EFAULT detection for now
119360SN/A    }
120360SN/A
121360SN/A    //
122360SN/A    // copy data out of simulator space (write to target memory)
123360SN/A    //
1242400SN/A    virtual bool copyOut(TranslatingPort *memport)
125360SN/A    {
1262461SN/A        memport->writeBlob(addr, bufPtr, size);
127360SN/A        return true;	// no EFAULT detection for now
128360SN/A    }
129360SN/A
130360SN/A  protected:
131360SN/A    Addr addr;
132360SN/A    int size;
133360SN/A    uint8_t *bufPtr;
134360SN/A};
135360SN/A
136360SN/A
137360SN/Aclass BufferArg : public BaseBufferArg
138360SN/A{
139360SN/A  public:
140360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
141360SN/A    void *bufferPtr()	{ return bufPtr; }
142360SN/A};
143360SN/A
144360SN/Atemplate <class T>
145360SN/Aclass TypedBufferArg : public BaseBufferArg
146360SN/A{
147360SN/A  public:
148360SN/A    // user can optionally specify a specific number of bytes to
149360SN/A    // allocate to deal with those structs that have variable-size
150360SN/A    // arrays at the end
151360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
152360SN/A        : BaseBufferArg(_addr, _size)
153360SN/A    { }
154360SN/A
155360SN/A    // type case
156360SN/A    operator T*() { return (T *)bufPtr; }
157360SN/A
158360SN/A    // dereference operators
159502SN/A    T &operator*()	 { return *((T *)bufPtr); }
160360SN/A    T* operator->()	 { return (T *)bufPtr; }
161502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
162360SN/A};
163360SN/A
164360SN/A//////////////////////////////////////////////////////////////////////
165360SN/A//
166360SN/A// The following emulation functions are generic enough that they
167360SN/A// don't need to be recompiled for different emulated OS's.  They are
168360SN/A// defined in sim/syscall_emul.cc.
169360SN/A//
170360SN/A//////////////////////////////////////////////////////////////////////
171360SN/A
172360SN/A
173378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1741706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1752680Sktlim@umich.edu                                Process *p, ThreadContext *tc);
176378SN/A
177378SN/A/// Handler for unimplemented syscalls that we never intend to
178378SN/A/// implement (signal handling, etc.) and should not affect the correct
179378SN/A/// behavior of the program.  Print a warning only if the appropriate
180378SN/A/// trace flag is enabled.  Return success to the target program.
1811706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1822680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
183360SN/A
184378SN/A/// Target exit() handler: terminate simulation.
1851706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1862680Sktlim@umich.edu                       Process *p, ThreadContext *tc);
187378SN/A
188378SN/A/// Target getpagesize() handler.
1891706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1902680Sktlim@umich.edu                              Process *p, ThreadContext *tc);
191378SN/A
192378SN/A/// Target obreak() handler: set brk address.
1931706SN/ASyscallReturn obreakFunc(SyscallDesc *desc, int num,
1942680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
195378SN/A
196378SN/A/// Target close() handler.
1971706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1982680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
199378SN/A
200378SN/A/// Target read() handler.
2011706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2022680Sktlim@umich.edu                       Process *p, ThreadContext *tc);
203378SN/A
204378SN/A/// Target write() handler.
2051706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2062680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
207378SN/A
208378SN/A/// Target lseek() handler.
2091706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2102680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
211378SN/A
212378SN/A/// Target munmap() handler.
2131706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2142680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
215378SN/A
216378SN/A/// Target gethostname() handler.
2171706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2182680Sktlim@umich.edu                              Process *p, ThreadContext *tc);
219360SN/A
220511SN/A/// Target unlink() handler.
2211706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2222680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
223511SN/A
224511SN/A/// Target rename() handler.
2251706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2262680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
2271706SN/A
2281706SN/A
2291706SN/A/// Target truncate() handler.
2301706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2312680Sktlim@umich.edu                           Process *p, ThreadContext *tc);
2321706SN/A
2331706SN/A
2341706SN/A/// Target ftruncate() handler.
2351706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2362680Sktlim@umich.edu                            Process *p, ThreadContext *tc);
2371706SN/A
238511SN/A
2391999SN/A/// Target chown() handler.
2401999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2412680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
2421999SN/A
2431999SN/A
2441999SN/A/// Target fchown() handler.
2451999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2462680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
2471999SN/A
2483079Sstever@eecs.umich.edu/// Target dup() handler.
2493079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2503079Sstever@eecs.umich.edu                      Process *process, ThreadContext *tc);
2513079Sstever@eecs.umich.edu
2522093SN/A/// Target fnctl() handler.
2532093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2542680Sktlim@umich.edu                        Process *process, ThreadContext *tc);
2552093SN/A
2562687Sksewell@umich.edu/// Target fcntl64() handler.
2572687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2582687Sksewell@umich.edu                        Process *process, ThreadContext *tc);
2592687Sksewell@umich.edu
2602238SN/A/// Target setuid() handler.
2612238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2622680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2632238SN/A
2642238SN/A/// Target getpid() handler.
2652238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2662680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2672238SN/A
2682238SN/A/// Target getuid() handler.
2692238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2702680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2712238SN/A
2722238SN/A/// Target getgid() handler.
2732238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2742680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2752238SN/A
2762238SN/A/// Target getppid() handler.
2772238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2782680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2792238SN/A
2802238SN/A/// Target geteuid() handler.
2812238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2822680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2832238SN/A
2842238SN/A/// Target getegid() handler.
2852238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2862680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2872238SN/A
2882238SN/A
2892238SN/A
2902238SN/A/// Pseudo Funcs  - These functions use a different return convension,
2912238SN/A/// returning a second value in a register other than the normal return register
2922238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
2932680Sktlim@umich.edu                             Process *process, ThreadContext *tc);
2942238SN/A
2952238SN/A/// Target getpidPseudo() handler.
2962238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
2972680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2982238SN/A
2992238SN/A/// Target getuidPseudo() handler.
3002238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3012680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
3022238SN/A
3032238SN/A/// Target getgidPseudo() handler.
3042238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3052680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
3062238SN/A
3072238SN/A
3081354SN/A/// This struct is used to build an target-OS-dependent table that
3091354SN/A/// maps the target's open() flags to the host open() flags.
3101354SN/Astruct OpenFlagTransTable {
3111354SN/A    int tgtFlag;	//!< Target system flag value.
3121354SN/A    int hostFlag;	//!< Corresponding host system flag value.
3131354SN/A};
3141354SN/A
3151354SN/A
3161354SN/A
3171354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3181354SN/Aconst int one_million = 1000000;
3191354SN/A
3201354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3211354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3221354SN/A/// real-world time) to keep simulations repeatable.
3231354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3241354SN/A
3251354SN/A/// Helper function to convert current elapsed time to seconds and
3261354SN/A/// microseconds.
3271354SN/Atemplate <class T1, class T2>
3281354SN/Avoid
3291354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3301354SN/A{
3311609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3321354SN/A    sec = elapsed_usecs / one_million;
3331354SN/A    usec = elapsed_usecs % one_million;
3341354SN/A}
3351354SN/A
336360SN/A//////////////////////////////////////////////////////////////////////
337360SN/A//
338360SN/A// The following emulation functions are generic, but need to be
339360SN/A// templated to account for differences in types, constants, etc.
340360SN/A//
341360SN/A//////////////////////////////////////////////////////////////////////
342360SN/A
343378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
344378SN/A/// only to find out if their stdout is a tty, to determine whether to
345378SN/A/// do line or block buffering.
346360SN/Atemplate <class OS>
3471450SN/ASyscallReturn
348360SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process,
3492680Sktlim@umich.edu          ThreadContext *tc)
350360SN/A{
3512680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
3522680Sktlim@umich.edu    unsigned req = tc->getSyscallArg(1);
353360SN/A
3541969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
355360SN/A
356360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
357360SN/A        // doesn't map to any simulator fd: not a valid target fd
3581458SN/A        return -EBADF;
359360SN/A    }
360360SN/A
361360SN/A    switch (req) {
3622553SN/A      case OS::TIOCISATTY:
3632553SN/A      case OS::TIOCGETP:
3642553SN/A      case OS::TIOCSETP:
3652553SN/A      case OS::TIOCSETN:
3662553SN/A      case OS::TIOCSETC:
3672553SN/A      case OS::TIOCGETC:
3682553SN/A      case OS::TIOCGETS:
3692553SN/A      case OS::TIOCGETA:
3701458SN/A        return -ENOTTY;
371360SN/A
372360SN/A      default:
3731706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
3742680Sktlim@umich.edu              fd, req, tc->readPC());
375360SN/A    }
376360SN/A}
377360SN/A
378378SN/A/// Target open() handler.
379360SN/Atemplate <class OS>
3801450SN/ASyscallReturn
381360SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process,
3822680Sktlim@umich.edu         ThreadContext *tc)
383360SN/A{
384360SN/A    std::string path;
385360SN/A
3862680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
3871458SN/A        return -EFAULT;
388360SN/A
389360SN/A    if (path == "/dev/sysdev0") {
390360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
391360SN/A        // We don't support it, so just punt.
3921706SN/A        warn("Ignoring open(%s, ...)\n", path);
3931458SN/A        return -ENOENT;
394360SN/A    }
395360SN/A
3962680Sktlim@umich.edu    int tgtFlags = tc->getSyscallArg(1);
3972680Sktlim@umich.edu    int mode = tc->getSyscallArg(2);
398360SN/A    int hostFlags = 0;
399360SN/A
400360SN/A    // translate open flags
401360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
402360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
403360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
404360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
405360SN/A        }
406360SN/A    }
407360SN/A
408360SN/A    // any target flags left?
409360SN/A    if (tgtFlags != 0)
4101706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
411360SN/A
412360SN/A#ifdef __CYGWIN32__
413360SN/A    hostFlags |= O_BINARY;
414360SN/A#endif
415360SN/A
4161706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
4171706SN/A
418360SN/A    // open the file
419360SN/A    int fd = open(path.c_str(), hostFlags, mode);
420360SN/A
4211970SN/A    return (fd == -1) ? -errno : process->alloc_fd(fd);
422360SN/A}
423360SN/A
424360SN/A
4251999SN/A/// Target chmod() handler.
4261999SN/Atemplate <class OS>
4271999SN/ASyscallReturn
4281999SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process,
4292680Sktlim@umich.edu          ThreadContext *tc)
4301999SN/A{
4311999SN/A    std::string path;
4321999SN/A
4332680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4341999SN/A        return -EFAULT;
4351999SN/A
4362680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
4371999SN/A    mode_t hostMode = 0;
4381999SN/A
4391999SN/A    // XXX translate mode flags via OS::something???
4401999SN/A    hostMode = mode;
4411999SN/A
4421999SN/A    // do the chmod
4431999SN/A    int result = chmod(path.c_str(), hostMode);
4441999SN/A    if (result < 0)
4452218SN/A        return -errno;
4461999SN/A
4471999SN/A    return 0;
4481999SN/A}
4491999SN/A
4501999SN/A
4511999SN/A/// Target fchmod() handler.
4521999SN/Atemplate <class OS>
4531999SN/ASyscallReturn
4541999SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *process,
4552680Sktlim@umich.edu           ThreadContext *tc)
4561999SN/A{
4572680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4581999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
4591999SN/A        // doesn't map to any simulator fd: not a valid target fd
4601999SN/A        return -EBADF;
4611999SN/A    }
4621999SN/A
4632680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
4641999SN/A    mode_t hostMode = 0;
4651999SN/A
4661999SN/A    // XXX translate mode flags via OS::someting???
4671999SN/A    hostMode = mode;
4681999SN/A
4691999SN/A    // do the fchmod
4701999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
4711999SN/A    if (result < 0)
4722218SN/A        return -errno;
4731999SN/A
4741999SN/A    return 0;
4751999SN/A}
4761999SN/A
4771999SN/A
478378SN/A/// Target stat() handler.
479360SN/Atemplate <class OS>
4801450SN/ASyscallReturn
481360SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process,
4822680Sktlim@umich.edu         ThreadContext *tc)
483360SN/A{
484360SN/A    std::string path;
485360SN/A
4862680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4872400SN/A    return -EFAULT;
488360SN/A
489360SN/A    struct stat hostBuf;
490360SN/A    int result = stat(path.c_str(), &hostBuf);
491360SN/A
492360SN/A    if (result < 0)
4932218SN/A        return -errno;
494360SN/A
4952680Sktlim@umich.edu    OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
496360SN/A
4971458SN/A    return 0;
498360SN/A}
499360SN/A
500360SN/A
5011999SN/A/// Target fstat64() handler.
5021999SN/Atemplate <class OS>
5031999SN/ASyscallReturn
5041999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *process,
5052680Sktlim@umich.edu            ThreadContext *tc)
5061999SN/A{
5072680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5081999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5091999SN/A        // doesn't map to any simulator fd: not a valid target fd
5101999SN/A        return -EBADF;
5111999SN/A    }
5121999SN/A
5132764Sstever@eecs.umich.edu#if NO_STAT64
5142064SN/A    struct stat  hostBuf;
5152064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
5162064SN/A#else
5172064SN/A    struct stat64  hostBuf;
5181999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
5192064SN/A#endif
5201999SN/A
5211999SN/A    if (result < 0)
5222218SN/A        return -errno;
5231999SN/A
5242680Sktlim@umich.edu    OS::copyOutStat64Buf(tc->getMemPort(), fd, tc->getSyscallArg(1), &hostBuf);
5251999SN/A
5261999SN/A    return 0;
5271999SN/A}
5281999SN/A
5291999SN/A
530378SN/A/// Target lstat() handler.
531360SN/Atemplate <class OS>
5321450SN/ASyscallReturn
533360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process,
5342680Sktlim@umich.edu          ThreadContext *tc)
535360SN/A{
536360SN/A    std::string path;
537360SN/A
5382680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5392400SN/A      return -EFAULT;
540360SN/A
541360SN/A    struct stat hostBuf;
542360SN/A    int result = lstat(path.c_str(), &hostBuf);
543360SN/A
544360SN/A    if (result < 0)
5451458SN/A        return -errno;
546360SN/A
5472680Sktlim@umich.edu    OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
548360SN/A
5491458SN/A    return 0;
550360SN/A}
551360SN/A
5521999SN/A/// Target lstat64() handler.
5531999SN/Atemplate <class OS>
5541999SN/ASyscallReturn
5551999SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process,
5562680Sktlim@umich.edu            ThreadContext *tc)
5571999SN/A{
5581999SN/A    std::string path;
5591999SN/A
5602680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5612400SN/A      return -EFAULT;
5621999SN/A
5632764Sstever@eecs.umich.edu#if NO_STAT64
5642064SN/A    struct stat hostBuf;
5652064SN/A    int result = lstat(path.c_str(), &hostBuf);
5662064SN/A#else
5671999SN/A    struct stat64 hostBuf;
5681999SN/A    int result = lstat64(path.c_str(), &hostBuf);
5692064SN/A#endif
5701999SN/A
5711999SN/A    if (result < 0)
5721999SN/A        return -errno;
5731999SN/A
5742680Sktlim@umich.edu    OS::copyOutStat64Buf(tc->getMemPort(), -1, tc->getSyscallArg(1), &hostBuf);
5751999SN/A
5761999SN/A    return 0;
5771999SN/A}
5781999SN/A
579378SN/A/// Target fstat() handler.
580360SN/Atemplate <class OS>
5811450SN/ASyscallReturn
582360SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *process,
5832680Sktlim@umich.edu          ThreadContext *tc)
584360SN/A{
5852680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
586360SN/A
5871969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
588360SN/A
589360SN/A    if (fd < 0)
5901458SN/A        return -EBADF;
591360SN/A
592360SN/A    struct stat hostBuf;
593360SN/A    int result = fstat(fd, &hostBuf);
594360SN/A
595360SN/A    if (result < 0)
5961458SN/A        return -errno;
597360SN/A
5982680Sktlim@umich.edu    OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
5992021SN/A
6001458SN/A    return 0;
601360SN/A}
602360SN/A
603360SN/A
6041706SN/A/// Target statfs() handler.
6051706SN/Atemplate <class OS>
6061706SN/ASyscallReturn
6071706SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
6082680Sktlim@umich.edu           ThreadContext *tc)
6091706SN/A{
6101706SN/A    std::string path;
6111706SN/A
6122680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6132400SN/A      return -EFAULT;
6141706SN/A
6151706SN/A    struct statfs hostBuf;
6161706SN/A    int result = statfs(path.c_str(), &hostBuf);
6171706SN/A
6181706SN/A    if (result < 0)
6192218SN/A        return -errno;
6201706SN/A
6212680Sktlim@umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6221706SN/A
6231706SN/A    return 0;
6241706SN/A}
6251706SN/A
6261706SN/A
6271706SN/A/// Target fstatfs() handler.
6281706SN/Atemplate <class OS>
6291706SN/ASyscallReturn
6301706SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
6312680Sktlim@umich.edu            ThreadContext *tc)
6321706SN/A{
6332680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
6341706SN/A
6351706SN/A    if (fd < 0)
6361706SN/A        return -EBADF;
6371706SN/A
6381706SN/A    struct statfs hostBuf;
6391706SN/A    int result = fstatfs(fd, &hostBuf);
6401706SN/A
6411706SN/A    if (result < 0)
6422218SN/A        return -errno;
6431706SN/A
6442680Sktlim@umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6451706SN/A
6461706SN/A    return 0;
6471706SN/A}
6481706SN/A
6491706SN/A
6501999SN/A/// Target writev() handler.
6511999SN/Atemplate <class OS>
6521999SN/ASyscallReturn
6531999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process,
6542680Sktlim@umich.edu           ThreadContext *tc)
6551999SN/A{
6562680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
6571999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6581999SN/A        // doesn't map to any simulator fd: not a valid target fd
6591999SN/A        return -EBADF;
6601999SN/A    }
6611999SN/A
6622680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
6632680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
6642680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
6651999SN/A    struct iovec hiov[count];
6661999SN/A    for (int i = 0; i < count; ++i)
6671999SN/A    {
6681999SN/A        typename OS::tgt_iovec tiov;
6692461SN/A
6702461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
6712461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
6722091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
6731999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
6742461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
6752461SN/A                    hiov[i].iov_len);
6761999SN/A    }
6771999SN/A
6781999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
6791999SN/A
6801999SN/A    for (int i = 0; i < count; ++i)
6811999SN/A    {
6821999SN/A        delete [] (char *)hiov[i].iov_base;
6831999SN/A    }
6841999SN/A
6851999SN/A    if (result < 0)
6862218SN/A        return -errno;
6871999SN/A
6881999SN/A    return 0;
6891999SN/A}
6901999SN/A
6911999SN/A
692378SN/A/// Target mmap() handler.
693378SN/A///
694378SN/A/// We don't really handle mmap().  If the target is mmaping an
695378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
696378SN/A/// nothing (since memory is initialized to zero and the simulator
697378SN/A/// doesn't really check addresses anyway).  Always print a warning,
698378SN/A/// since this could be seriously broken if we're not mapping
699378SN/A/// /dev/zero.
700360SN/A//
701378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
702378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
703378SN/A/// anything else.
704360SN/Atemplate <class OS>
7051450SN/ASyscallReturn
7062680Sktlim@umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
707360SN/A{
7082680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
7092680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
7102680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
7112680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
7122680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
7132680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
714360SN/A
7152544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
7162544SN/A        (length % TheISA::VMPageSize) != 0) {
7172544SN/A        warn("mmap failing: arguments not page-aligned: "
7182544SN/A             "start 0x%x length 0x%x",
7192544SN/A             start, length);
7202544SN/A        return -EINVAL;
721360SN/A    }
722360SN/A
7232544SN/A    if (start != 0) {
7242544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
7252544SN/A             start, p->mmap_end);
7262544SN/A    }
7272544SN/A
7282544SN/A    // pick next address from our "mmap region"
7292544SN/A    start = p->mmap_end;
7302544SN/A    p->pTable->allocate(start, length);
7312544SN/A    p->mmap_end += length;
7322544SN/A
7332553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
7341969SN/A        warn("allowing mmap of file @ fd %d. "
7352680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
736360SN/A    }
737360SN/A
7381458SN/A    return start;
739360SN/A}
740360SN/A
741378SN/A/// Target getrlimit() handler.
742360SN/Atemplate <class OS>
7431450SN/ASyscallReturn
744360SN/AgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
7452680Sktlim@umich.edu        ThreadContext *tc)
746360SN/A{
7472680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
7482680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
749360SN/A
750360SN/A    switch (resource) {
7512064SN/A        case OS::TGT_RLIMIT_STACK:
7522064SN/A            // max stack size in bytes: make up a number (2MB for now)
7532064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
7542091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
7552091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
7562064SN/A            break;
757360SN/A
7582064SN/A        default:
7592064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
7602064SN/A                << std::endl;
7612064SN/A            abort();
7622064SN/A            break;
763360SN/A    }
764360SN/A
7652680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
7661458SN/A    return 0;
767360SN/A}
768360SN/A
769378SN/A/// Target gettimeofday() handler.
770360SN/Atemplate <class OS>
7711450SN/ASyscallReturn
772360SN/AgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
7732680Sktlim@umich.edu        ThreadContext *tc)
774360SN/A{
7752680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
776360SN/A
777360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
778360SN/A    tp->tv_sec += seconds_since_epoch;
7792091SN/A    tp->tv_sec = htog(tp->tv_sec);
7802091SN/A    tp->tv_usec = htog(tp->tv_usec);
781360SN/A
7822680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
783360SN/A
7841458SN/A    return 0;
785360SN/A}
786360SN/A
787360SN/A
7881999SN/A/// Target utimes() handler.
7891999SN/Atemplate <class OS>
7901999SN/ASyscallReturn
7911999SN/AutimesFunc(SyscallDesc *desc, int callnum, Process *process,
7922680Sktlim@umich.edu           ThreadContext *tc)
7931999SN/A{
7941999SN/A    std::string path;
7951999SN/A
7962680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7972400SN/A      return -EFAULT;
7981999SN/A
7992680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
8002680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
8011999SN/A
8021999SN/A    struct timeval hostTimeval[2];
8031999SN/A    for (int i = 0; i < 2; ++i)
8041999SN/A    {
8052091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
8062091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
8071999SN/A    }
8081999SN/A    int result = utimes(path.c_str(), hostTimeval);
8091999SN/A
8101999SN/A    if (result < 0)
8111999SN/A        return -errno;
8121999SN/A
8131999SN/A    return 0;
8141999SN/A}
815378SN/A/// Target getrusage() function.
816360SN/Atemplate <class OS>
8171450SN/ASyscallReturn
818360SN/AgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
8192680Sktlim@umich.edu              ThreadContext *tc)
820360SN/A{
8212680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
8222680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
823360SN/A
8242553SN/A    if (who != OS::TGT_RUSAGE_SELF) {
825360SN/A        // don't really handle THREAD or CHILDREN, but just warn and
826360SN/A        // plow ahead
8271969SN/A        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
8281969SN/A             who);
829360SN/A    }
830360SN/A
831360SN/A    getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
8322091SN/A    rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
8332091SN/A    rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
8342091SN/A
835360SN/A    rup->ru_stime.tv_sec = 0;
836360SN/A    rup->ru_stime.tv_usec = 0;
837360SN/A    rup->ru_maxrss = 0;
838360SN/A    rup->ru_ixrss = 0;
839360SN/A    rup->ru_idrss = 0;
840360SN/A    rup->ru_isrss = 0;
841360SN/A    rup->ru_minflt = 0;
842360SN/A    rup->ru_majflt = 0;
843360SN/A    rup->ru_nswap = 0;
844360SN/A    rup->ru_inblock = 0;
845360SN/A    rup->ru_oublock = 0;
846360SN/A    rup->ru_msgsnd = 0;
847360SN/A    rup->ru_msgrcv = 0;
848360SN/A    rup->ru_nsignals = 0;
849360SN/A    rup->ru_nvcsw = 0;
850360SN/A    rup->ru_nivcsw = 0;
851360SN/A
8522680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
853360SN/A
8541458SN/A    return 0;
855360SN/A}
856360SN/A
8572553SN/A
8582553SN/A
8592553SN/A
8601354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
861