syscall_emul.hh revision 2707
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
302707Sksewell@umich.edu *          Korey Sewell
31360SN/A */
32360SN/A
331354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
341354SN/A#define __SIM_SYSCALL_EMUL_HH__
35360SN/A
362064SN/A#define BSD_HOST (defined(__APPLE__) || defined(__OpenBSD__) || \
372064SN/A                  defined(__FreeBSD__))
382064SN/A
39360SN/A///
40360SN/A/// @file syscall_emul.hh
41360SN/A///
42360SN/A/// This file defines objects used to emulate syscalls from the target
43360SN/A/// application on the host machine.
44360SN/A
451354SN/A#include <errno.h>
46360SN/A#include <string>
471809SN/A#ifdef __CYGWIN32__
481809SN/A#include <sys/fcntl.h>	// for O_BINARY
491809SN/A#endif
501999SN/A#include <sys/uio.h>
51360SN/A
522474SN/A#include "arch/isa_traits.hh"	// for Addr
532474SN/A#include "base/chunk_generator.hh"
54360SN/A#include "base/intmath.hh"	// for RoundUp
552462SN/A#include "base/misc.hh"
561354SN/A#include "base/trace.hh"
572474SN/A#include "cpu/base.hh"
582680Sktlim@umich.edu#include "cpu/thread_context.hh"
592474SN/A#include "mem/translating_port.hh"
602474SN/A#include "mem/page_table.hh"
611354SN/A#include "sim/process.hh"
62360SN/A
63360SN/A///
64360SN/A/// System call descriptor.
65360SN/A///
66360SN/Aclass SyscallDesc {
67360SN/A
68360SN/A  public:
69360SN/A
70378SN/A    /// Typedef for target syscall handler functions.
711450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
722680Sktlim@umich.edu                           Process *, ThreadContext *);
73360SN/A
74360SN/A    const char *name;	//!< Syscall name (e.g., "open").
75360SN/A    FuncPtr funcPtr;	//!< Pointer to emulation function.
76360SN/A    int flags;		//!< Flags (see Flags enum).
77360SN/A
78360SN/A    /// Flag values for controlling syscall behavior.
79360SN/A    enum Flags {
80360SN/A        /// Don't set return regs according to funcPtr return value.
81360SN/A        /// Used for syscalls with non-standard return conventions
822680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
83360SN/A        /// sigreturn).
84360SN/A        SuppressReturnValue = 1
85360SN/A    };
86360SN/A
87360SN/A    /// Constructor.
88360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
89360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
90360SN/A    {
91360SN/A    }
92360SN/A
93360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
942680Sktlim@umich.edu    void doSyscall(int callnum, Process *proc, ThreadContext *tc);
95360SN/A};
96360SN/A
97360SN/A
98360SN/Aclass BaseBufferArg {
99360SN/A
100360SN/A  public:
101360SN/A
102360SN/A    BaseBufferArg(Addr _addr, int _size) : addr(_addr), size(_size)
103360SN/A    {
104360SN/A        bufPtr = new uint8_t[size];
105360SN/A        // clear out buffer: in case we only partially populate this,
106360SN/A        // and then do a copyOut(), we want to make sure we don't
107360SN/A        // introduce any random junk into the simulated address space
108360SN/A        memset(bufPtr, 0, size);
109360SN/A    }
110360SN/A
111360SN/A    virtual ~BaseBufferArg() { delete [] bufPtr; }
112360SN/A
113360SN/A    //
114360SN/A    // copy data into simulator space (read from target memory)
115360SN/A    //
1162400SN/A    virtual bool copyIn(TranslatingPort *memport)
117360SN/A    {
1182461SN/A        memport->readBlob(addr, bufPtr, size);
119360SN/A        return true;	// no EFAULT detection for now
120360SN/A    }
121360SN/A
122360SN/A    //
123360SN/A    // copy data out of simulator space (write to target memory)
124360SN/A    //
1252400SN/A    virtual bool copyOut(TranslatingPort *memport)
126360SN/A    {
1272461SN/A        memport->writeBlob(addr, bufPtr, size);
128360SN/A        return true;	// no EFAULT detection for now
129360SN/A    }
130360SN/A
131360SN/A  protected:
132360SN/A    Addr addr;
133360SN/A    int size;
134360SN/A    uint8_t *bufPtr;
135360SN/A};
136360SN/A
137360SN/A
138360SN/Aclass BufferArg : public BaseBufferArg
139360SN/A{
140360SN/A  public:
141360SN/A    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
142360SN/A    void *bufferPtr()	{ return bufPtr; }
143360SN/A};
144360SN/A
145360SN/Atemplate <class T>
146360SN/Aclass TypedBufferArg : public BaseBufferArg
147360SN/A{
148360SN/A  public:
149360SN/A    // user can optionally specify a specific number of bytes to
150360SN/A    // allocate to deal with those structs that have variable-size
151360SN/A    // arrays at the end
152360SN/A    TypedBufferArg(Addr _addr, int _size = sizeof(T))
153360SN/A        : BaseBufferArg(_addr, _size)
154360SN/A    { }
155360SN/A
156360SN/A    // type case
157360SN/A    operator T*() { return (T *)bufPtr; }
158360SN/A
159360SN/A    // dereference operators
160502SN/A    T &operator*()	 { return *((T *)bufPtr); }
161360SN/A    T* operator->()	 { return (T *)bufPtr; }
162502SN/A    T &operator[](int i) { return ((T *)bufPtr)[i]; }
163360SN/A};
164360SN/A
165360SN/A//////////////////////////////////////////////////////////////////////
166360SN/A//
167360SN/A// The following emulation functions are generic enough that they
168360SN/A// don't need to be recompiled for different emulated OS's.  They are
169360SN/A// defined in sim/syscall_emul.cc.
170360SN/A//
171360SN/A//////////////////////////////////////////////////////////////////////
172360SN/A
173360SN/A
174378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1751706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1762680Sktlim@umich.edu                                Process *p, ThreadContext *tc);
177378SN/A
178378SN/A/// Handler for unimplemented syscalls that we never intend to
179378SN/A/// implement (signal handling, etc.) and should not affect the correct
180378SN/A/// behavior of the program.  Print a warning only if the appropriate
181378SN/A/// trace flag is enabled.  Return success to the target program.
1821706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1832680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
184360SN/A
185378SN/A/// Target exit() handler: terminate simulation.
1861706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1872680Sktlim@umich.edu                       Process *p, ThreadContext *tc);
188378SN/A
189378SN/A/// Target getpagesize() handler.
1901706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1912680Sktlim@umich.edu                              Process *p, ThreadContext *tc);
192378SN/A
193378SN/A/// Target obreak() handler: set brk address.
1941706SN/ASyscallReturn obreakFunc(SyscallDesc *desc, int num,
1952680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
196378SN/A
197378SN/A/// Target close() handler.
1981706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1992680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
200378SN/A
201378SN/A/// Target read() handler.
2021706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
2032680Sktlim@umich.edu                       Process *p, ThreadContext *tc);
204378SN/A
205378SN/A/// Target write() handler.
2061706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
2072680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
208378SN/A
209378SN/A/// Target lseek() handler.
2101706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
2112680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
212378SN/A
213378SN/A/// Target munmap() handler.
2141706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
2152680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
216378SN/A
217378SN/A/// Target gethostname() handler.
2181706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2192680Sktlim@umich.edu                              Process *p, ThreadContext *tc);
220360SN/A
221511SN/A/// Target unlink() handler.
2221706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2232680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
224511SN/A
225511SN/A/// Target rename() handler.
2261706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2272680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
2281706SN/A
2291706SN/A
2301706SN/A/// Target truncate() handler.
2311706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2322680Sktlim@umich.edu                           Process *p, ThreadContext *tc);
2331706SN/A
2341706SN/A
2351706SN/A/// Target ftruncate() handler.
2361706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2372680Sktlim@umich.edu                            Process *p, ThreadContext *tc);
2381706SN/A
239511SN/A
2401999SN/A/// Target chown() handler.
2411999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2422680Sktlim@umich.edu                        Process *p, ThreadContext *tc);
2431999SN/A
2441999SN/A
2451999SN/A/// Target fchown() handler.
2461999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2472680Sktlim@umich.edu                         Process *p, ThreadContext *tc);
2481999SN/A
2492093SN/A/// Target fnctl() handler.
2502093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2512680Sktlim@umich.edu                        Process *process, ThreadContext *tc);
2522093SN/A
2532687Sksewell@umich.edu/// Target fcntl64() handler.
2542687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2552687Sksewell@umich.edu                        Process *process, ThreadContext *tc);
2562687Sksewell@umich.edu
2572238SN/A/// Target setuid() handler.
2582238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2592680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2602238SN/A
2612238SN/A/// Target getpid() handler.
2622238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2632680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2642238SN/A
2652238SN/A/// Target getuid() handler.
2662238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2672680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2682238SN/A
2692238SN/A/// Target getgid() handler.
2702238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2712680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2722238SN/A
2732238SN/A/// Target getppid() handler.
2742238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2752680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2762238SN/A
2772238SN/A/// Target geteuid() handler.
2782238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2792680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2802238SN/A
2812238SN/A/// Target getegid() handler.
2822238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2832680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2842238SN/A
2852238SN/A
2862238SN/A
2872238SN/A/// Pseudo Funcs  - These functions use a different return convension,
2882238SN/A/// returning a second value in a register other than the normal return register
2892238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
2902680Sktlim@umich.edu                             Process *process, ThreadContext *tc);
2912238SN/A
2922238SN/A/// Target getpidPseudo() handler.
2932238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
2942680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2952238SN/A
2962238SN/A/// Target getuidPseudo() handler.
2972238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
2982680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
2992238SN/A
3002238SN/A/// Target getgidPseudo() handler.
3012238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3022680Sktlim@umich.edu                               Process *p, ThreadContext *tc);
3032238SN/A
3042238SN/A
3051354SN/A/// This struct is used to build an target-OS-dependent table that
3061354SN/A/// maps the target's open() flags to the host open() flags.
3071354SN/Astruct OpenFlagTransTable {
3081354SN/A    int tgtFlag;	//!< Target system flag value.
3091354SN/A    int hostFlag;	//!< Corresponding host system flag value.
3101354SN/A};
3111354SN/A
3121354SN/A
3131354SN/A
3141354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3151354SN/Aconst int one_million = 1000000;
3161354SN/A
3171354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3181354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3191354SN/A/// real-world time) to keep simulations repeatable.
3201354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3211354SN/A
3221354SN/A/// Helper function to convert current elapsed time to seconds and
3231354SN/A/// microseconds.
3241354SN/Atemplate <class T1, class T2>
3251354SN/Avoid
3261354SN/AgetElapsedTime(T1 &sec, T2 &usec)
3271354SN/A{
3281609SN/A    int elapsed_usecs = curTick / Clock::Int::us;
3291354SN/A    sec = elapsed_usecs / one_million;
3301354SN/A    usec = elapsed_usecs % one_million;
3311354SN/A}
3321354SN/A
333360SN/A//////////////////////////////////////////////////////////////////////
334360SN/A//
335360SN/A// The following emulation functions are generic, but need to be
336360SN/A// templated to account for differences in types, constants, etc.
337360SN/A//
338360SN/A//////////////////////////////////////////////////////////////////////
339360SN/A
340378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
341378SN/A/// only to find out if their stdout is a tty, to determine whether to
342378SN/A/// do line or block buffering.
343360SN/Atemplate <class OS>
3441450SN/ASyscallReturn
345360SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process,
3462680Sktlim@umich.edu          ThreadContext *tc)
347360SN/A{
3482680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
3492680Sktlim@umich.edu    unsigned req = tc->getSyscallArg(1);
350360SN/A
3511969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
352360SN/A
353360SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
354360SN/A        // doesn't map to any simulator fd: not a valid target fd
3551458SN/A        return -EBADF;
356360SN/A    }
357360SN/A
358360SN/A    switch (req) {
3592553SN/A      case OS::TIOCISATTY:
3602553SN/A      case OS::TIOCGETP:
3612553SN/A      case OS::TIOCSETP:
3622553SN/A      case OS::TIOCSETN:
3632553SN/A      case OS::TIOCSETC:
3642553SN/A      case OS::TIOCGETC:
3652553SN/A      case OS::TIOCGETS:
3662553SN/A      case OS::TIOCGETA:
3671458SN/A        return -ENOTTY;
368360SN/A
369360SN/A      default:
3701706SN/A        fatal("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ 0x%llx\n",
3712680Sktlim@umich.edu              fd, req, tc->readPC());
372360SN/A    }
373360SN/A}
374360SN/A
375378SN/A/// Target open() handler.
376360SN/Atemplate <class OS>
3771450SN/ASyscallReturn
378360SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process,
3792680Sktlim@umich.edu         ThreadContext *tc)
380360SN/A{
381360SN/A    std::string path;
382360SN/A
3832680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
3841458SN/A        return -EFAULT;
385360SN/A
386360SN/A    if (path == "/dev/sysdev0") {
387360SN/A        // This is a memory-mapped high-resolution timer device on Alpha.
388360SN/A        // We don't support it, so just punt.
3891706SN/A        warn("Ignoring open(%s, ...)\n", path);
3901458SN/A        return -ENOENT;
391360SN/A    }
392360SN/A
3932680Sktlim@umich.edu    int tgtFlags = tc->getSyscallArg(1);
3942680Sktlim@umich.edu    int mode = tc->getSyscallArg(2);
395360SN/A    int hostFlags = 0;
396360SN/A
397360SN/A    // translate open flags
398360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
399360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
400360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
401360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
402360SN/A        }
403360SN/A    }
404360SN/A
405360SN/A    // any target flags left?
406360SN/A    if (tgtFlags != 0)
4071706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
408360SN/A
409360SN/A#ifdef __CYGWIN32__
410360SN/A    hostFlags |= O_BINARY;
411360SN/A#endif
412360SN/A
4131706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
4141706SN/A
415360SN/A    // open the file
416360SN/A    int fd = open(path.c_str(), hostFlags, mode);
417360SN/A
4181970SN/A    return (fd == -1) ? -errno : process->alloc_fd(fd);
419360SN/A}
420360SN/A
421360SN/A
4221999SN/A/// Target chmod() handler.
4231999SN/Atemplate <class OS>
4241999SN/ASyscallReturn
4251999SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process,
4262680Sktlim@umich.edu          ThreadContext *tc)
4271999SN/A{
4281999SN/A    std::string path;
4291999SN/A
4302680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4311999SN/A        return -EFAULT;
4321999SN/A
4332680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
4341999SN/A    mode_t hostMode = 0;
4351999SN/A
4361999SN/A    // XXX translate mode flags via OS::something???
4371999SN/A    hostMode = mode;
4381999SN/A
4391999SN/A    // do the chmod
4401999SN/A    int result = chmod(path.c_str(), hostMode);
4411999SN/A    if (result < 0)
4422218SN/A        return -errno;
4431999SN/A
4441999SN/A    return 0;
4451999SN/A}
4461999SN/A
4471999SN/A
4481999SN/A/// Target fchmod() handler.
4491999SN/Atemplate <class OS>
4501999SN/ASyscallReturn
4511999SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *process,
4522680Sktlim@umich.edu           ThreadContext *tc)
4531999SN/A{
4542680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
4551999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
4561999SN/A        // doesn't map to any simulator fd: not a valid target fd
4571999SN/A        return -EBADF;
4581999SN/A    }
4591999SN/A
4602680Sktlim@umich.edu    uint32_t mode = tc->getSyscallArg(1);
4611999SN/A    mode_t hostMode = 0;
4621999SN/A
4631999SN/A    // XXX translate mode flags via OS::someting???
4641999SN/A    hostMode = mode;
4651999SN/A
4661999SN/A    // do the fchmod
4671999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
4681999SN/A    if (result < 0)
4692218SN/A        return -errno;
4701999SN/A
4711999SN/A    return 0;
4721999SN/A}
4731999SN/A
4741999SN/A
475378SN/A/// Target stat() handler.
476360SN/Atemplate <class OS>
4771450SN/ASyscallReturn
478360SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process,
4792680Sktlim@umich.edu         ThreadContext *tc)
480360SN/A{
481360SN/A    std::string path;
482360SN/A
4832680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
4842400SN/A    return -EFAULT;
485360SN/A
486360SN/A    struct stat hostBuf;
487360SN/A    int result = stat(path.c_str(), &hostBuf);
488360SN/A
489360SN/A    if (result < 0)
4902218SN/A        return -errno;
491360SN/A
4922680Sktlim@umich.edu    OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
493360SN/A
4941458SN/A    return 0;
495360SN/A}
496360SN/A
497360SN/A
4981999SN/A/// Target fstat64() handler.
4991999SN/Atemplate <class OS>
5001999SN/ASyscallReturn
5011999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *process,
5022680Sktlim@umich.edu            ThreadContext *tc)
5031999SN/A{
5042680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
5051999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
5061999SN/A        // doesn't map to any simulator fd: not a valid target fd
5071999SN/A        return -EBADF;
5081999SN/A    }
5091999SN/A
5102067SN/A#if BSD_HOST
5112064SN/A    struct stat  hostBuf;
5122064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
5132064SN/A#else
5142064SN/A    struct stat64  hostBuf;
5151999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
5162064SN/A#endif
5171999SN/A
5181999SN/A    if (result < 0)
5192218SN/A        return -errno;
5201999SN/A
5212680Sktlim@umich.edu    OS::copyOutStat64Buf(tc->getMemPort(), fd, tc->getSyscallArg(1), &hostBuf);
5221999SN/A
5231999SN/A    return 0;
5241999SN/A}
5251999SN/A
5261999SN/A
527378SN/A/// Target lstat() handler.
528360SN/Atemplate <class OS>
5291450SN/ASyscallReturn
530360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process,
5312680Sktlim@umich.edu          ThreadContext *tc)
532360SN/A{
533360SN/A    std::string path;
534360SN/A
5352680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5362400SN/A      return -EFAULT;
537360SN/A
538360SN/A    struct stat hostBuf;
539360SN/A    int result = lstat(path.c_str(), &hostBuf);
540360SN/A
541360SN/A    if (result < 0)
5421458SN/A        return -errno;
543360SN/A
5442680Sktlim@umich.edu    OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
545360SN/A
5461458SN/A    return 0;
547360SN/A}
548360SN/A
5491999SN/A/// Target lstat64() handler.
5501999SN/Atemplate <class OS>
5511999SN/ASyscallReturn
5521999SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process,
5532680Sktlim@umich.edu            ThreadContext *tc)
5541999SN/A{
5551999SN/A    std::string path;
5561999SN/A
5572680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
5582400SN/A      return -EFAULT;
5591999SN/A
5602067SN/A#if BSD_HOST
5612064SN/A    struct stat hostBuf;
5622064SN/A    int result = lstat(path.c_str(), &hostBuf);
5632064SN/A#else
5641999SN/A    struct stat64 hostBuf;
5651999SN/A    int result = lstat64(path.c_str(), &hostBuf);
5662064SN/A#endif
5671999SN/A
5681999SN/A    if (result < 0)
5691999SN/A        return -errno;
5701999SN/A
5712680Sktlim@umich.edu    OS::copyOutStat64Buf(tc->getMemPort(), -1, tc->getSyscallArg(1), &hostBuf);
5721999SN/A
5731999SN/A    return 0;
5741999SN/A}
5751999SN/A
576378SN/A/// Target fstat() handler.
577360SN/Atemplate <class OS>
5781450SN/ASyscallReturn
579360SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *process,
5802680Sktlim@umich.edu          ThreadContext *tc)
581360SN/A{
5822680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
583360SN/A
5841969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
585360SN/A
586360SN/A    if (fd < 0)
5871458SN/A        return -EBADF;
588360SN/A
589360SN/A    struct stat hostBuf;
590360SN/A    int result = fstat(fd, &hostBuf);
591360SN/A
592360SN/A    if (result < 0)
5931458SN/A        return -errno;
594360SN/A
5952680Sktlim@umich.edu    OS::copyOutStatBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
5962021SN/A
5971458SN/A    return 0;
598360SN/A}
599360SN/A
600360SN/A
6011706SN/A/// Target statfs() handler.
6021706SN/Atemplate <class OS>
6031706SN/ASyscallReturn
6041706SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
6052680Sktlim@umich.edu           ThreadContext *tc)
6061706SN/A{
6071706SN/A    std::string path;
6081706SN/A
6092680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
6102400SN/A      return -EFAULT;
6111706SN/A
6121706SN/A    struct statfs hostBuf;
6131706SN/A    int result = statfs(path.c_str(), &hostBuf);
6141706SN/A
6151706SN/A    if (result < 0)
6162218SN/A        return -errno;
6171706SN/A
6182680Sktlim@umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6191706SN/A
6201706SN/A    return 0;
6211706SN/A}
6221706SN/A
6231706SN/A
6241706SN/A/// Target fstatfs() handler.
6251706SN/Atemplate <class OS>
6261706SN/ASyscallReturn
6271706SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
6282680Sktlim@umich.edu            ThreadContext *tc)
6291706SN/A{
6302680Sktlim@umich.edu    int fd = process->sim_fd(tc->getSyscallArg(0));
6311706SN/A
6321706SN/A    if (fd < 0)
6331706SN/A        return -EBADF;
6341706SN/A
6351706SN/A    struct statfs hostBuf;
6361706SN/A    int result = fstatfs(fd, &hostBuf);
6371706SN/A
6381706SN/A    if (result < 0)
6392218SN/A        return -errno;
6401706SN/A
6412680Sktlim@umich.edu    OS::copyOutStatfsBuf(tc->getMemPort(), tc->getSyscallArg(1), &hostBuf);
6421706SN/A
6431706SN/A    return 0;
6441706SN/A}
6451706SN/A
6461706SN/A
6471999SN/A/// Target writev() handler.
6481999SN/Atemplate <class OS>
6491999SN/ASyscallReturn
6501999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process,
6512680Sktlim@umich.edu           ThreadContext *tc)
6521999SN/A{
6532680Sktlim@umich.edu    int fd = tc->getSyscallArg(0);
6541999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
6551999SN/A        // doesn't map to any simulator fd: not a valid target fd
6561999SN/A        return -EBADF;
6571999SN/A    }
6581999SN/A
6592680Sktlim@umich.edu    TranslatingPort *p = tc->getMemPort();
6602680Sktlim@umich.edu    uint64_t tiov_base = tc->getSyscallArg(1);
6612680Sktlim@umich.edu    size_t count = tc->getSyscallArg(2);
6621999SN/A    struct iovec hiov[count];
6631999SN/A    for (int i = 0; i < count; ++i)
6641999SN/A    {
6651999SN/A        typename OS::tgt_iovec tiov;
6662461SN/A
6672461SN/A        p->readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
6682461SN/A                    (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
6692091SN/A        hiov[i].iov_len = gtoh(tiov.iov_len);
6701999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
6712461SN/A        p->readBlob(gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
6722461SN/A                    hiov[i].iov_len);
6731999SN/A    }
6741999SN/A
6751999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
6761999SN/A
6771999SN/A    for (int i = 0; i < count; ++i)
6781999SN/A    {
6791999SN/A        delete [] (char *)hiov[i].iov_base;
6801999SN/A    }
6811999SN/A
6821999SN/A    if (result < 0)
6832218SN/A        return -errno;
6841999SN/A
6851999SN/A    return 0;
6861999SN/A}
6871999SN/A
6881999SN/A
689378SN/A/// Target mmap() handler.
690378SN/A///
691378SN/A/// We don't really handle mmap().  If the target is mmaping an
692378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
693378SN/A/// nothing (since memory is initialized to zero and the simulator
694378SN/A/// doesn't really check addresses anyway).  Always print a warning,
695378SN/A/// since this could be seriously broken if we're not mapping
696378SN/A/// /dev/zero.
697360SN/A//
698378SN/A/// Someday we should explicitly check for /dev/zero in open, flag the
699378SN/A/// file descriptor, and fail (or implement!) a non-anonymous mmap to
700378SN/A/// anything else.
701360SN/Atemplate <class OS>
7021450SN/ASyscallReturn
7032680Sktlim@umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
704360SN/A{
7052680Sktlim@umich.edu    Addr start = tc->getSyscallArg(0);
7062680Sktlim@umich.edu    uint64_t length = tc->getSyscallArg(1);
7072680Sktlim@umich.edu    // int prot = tc->getSyscallArg(2);
7082680Sktlim@umich.edu    int flags = tc->getSyscallArg(3);
7092680Sktlim@umich.edu    // int fd = p->sim_fd(tc->getSyscallArg(4));
7102680Sktlim@umich.edu    // int offset = tc->getSyscallArg(5);
711360SN/A
7122544SN/A    if ((start  % TheISA::VMPageSize) != 0 ||
7132544SN/A        (length % TheISA::VMPageSize) != 0) {
7142544SN/A        warn("mmap failing: arguments not page-aligned: "
7152544SN/A             "start 0x%x length 0x%x",
7162544SN/A             start, length);
7172544SN/A        return -EINVAL;
718360SN/A    }
719360SN/A
7202544SN/A    if (start != 0) {
7212544SN/A        warn("mmap: ignoring suggested map address 0x%x, using 0x%x",
7222544SN/A             start, p->mmap_end);
7232544SN/A    }
7242544SN/A
7252544SN/A    // pick next address from our "mmap region"
7262544SN/A    start = p->mmap_end;
7272544SN/A    p->pTable->allocate(start, length);
7282544SN/A    p->mmap_end += length;
7292544SN/A
7302553SN/A    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
7311969SN/A        warn("allowing mmap of file @ fd %d. "
7322680Sktlim@umich.edu             "This will break if not /dev/zero.", tc->getSyscallArg(4));
733360SN/A    }
734360SN/A
7351458SN/A    return start;
736360SN/A}
737360SN/A
738378SN/A/// Target getrlimit() handler.
739360SN/Atemplate <class OS>
7401450SN/ASyscallReturn
741360SN/AgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
7422680Sktlim@umich.edu        ThreadContext *tc)
743360SN/A{
7442680Sktlim@umich.edu    unsigned resource = tc->getSyscallArg(0);
7452680Sktlim@umich.edu    TypedBufferArg<typename OS::rlimit> rlp(tc->getSyscallArg(1));
746360SN/A
747360SN/A    switch (resource) {
7482064SN/A        case OS::TGT_RLIMIT_STACK:
7492064SN/A            // max stack size in bytes: make up a number (2MB for now)
7502064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
7512091SN/A            rlp->rlim_cur = htog(rlp->rlim_cur);
7522091SN/A            rlp->rlim_max = htog(rlp->rlim_max);
7532064SN/A            break;
754360SN/A
7552064SN/A        default:
7562064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
7572064SN/A                << std::endl;
7582064SN/A            abort();
7592064SN/A            break;
760360SN/A    }
761360SN/A
7622680Sktlim@umich.edu    rlp.copyOut(tc->getMemPort());
7631458SN/A    return 0;
764360SN/A}
765360SN/A
766378SN/A/// Target gettimeofday() handler.
767360SN/Atemplate <class OS>
7681450SN/ASyscallReturn
769360SN/AgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
7702680Sktlim@umich.edu        ThreadContext *tc)
771360SN/A{
7722680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval> tp(tc->getSyscallArg(0));
773360SN/A
774360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
775360SN/A    tp->tv_sec += seconds_since_epoch;
7762091SN/A    tp->tv_sec = htog(tp->tv_sec);
7772091SN/A    tp->tv_usec = htog(tp->tv_usec);
778360SN/A
7792680Sktlim@umich.edu    tp.copyOut(tc->getMemPort());
780360SN/A
7811458SN/A    return 0;
782360SN/A}
783360SN/A
784360SN/A
7851999SN/A/// Target utimes() handler.
7861999SN/Atemplate <class OS>
7871999SN/ASyscallReturn
7881999SN/AutimesFunc(SyscallDesc *desc, int callnum, Process *process,
7892680Sktlim@umich.edu           ThreadContext *tc)
7901999SN/A{
7911999SN/A    std::string path;
7921999SN/A
7932680Sktlim@umich.edu    if (!tc->getMemPort()->tryReadString(path, tc->getSyscallArg(0)))
7942400SN/A      return -EFAULT;
7951999SN/A
7962680Sktlim@umich.edu    TypedBufferArg<typename OS::timeval [2]> tp(tc->getSyscallArg(1));
7972680Sktlim@umich.edu    tp.copyIn(tc->getMemPort());
7981999SN/A
7991999SN/A    struct timeval hostTimeval[2];
8001999SN/A    for (int i = 0; i < 2; ++i)
8011999SN/A    {
8022091SN/A        hostTimeval[i].tv_sec = gtoh((*tp)[i].tv_sec);
8032091SN/A        hostTimeval[i].tv_usec = gtoh((*tp)[i].tv_usec);
8041999SN/A    }
8051999SN/A    int result = utimes(path.c_str(), hostTimeval);
8061999SN/A
8071999SN/A    if (result < 0)
8081999SN/A        return -errno;
8091999SN/A
8101999SN/A    return 0;
8111999SN/A}
812378SN/A/// Target getrusage() function.
813360SN/Atemplate <class OS>
8141450SN/ASyscallReturn
815360SN/AgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
8162680Sktlim@umich.edu              ThreadContext *tc)
817360SN/A{
8182680Sktlim@umich.edu    int who = tc->getSyscallArg(0);	// THREAD, SELF, or CHILDREN
8192680Sktlim@umich.edu    TypedBufferArg<typename OS::rusage> rup(tc->getSyscallArg(1));
820360SN/A
8212553SN/A    if (who != OS::TGT_RUSAGE_SELF) {
822360SN/A        // don't really handle THREAD or CHILDREN, but just warn and
823360SN/A        // plow ahead
8241969SN/A        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
8251969SN/A             who);
826360SN/A    }
827360SN/A
828360SN/A    getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
8292091SN/A    rup->ru_utime.tv_sec = htog(rup->ru_utime.tv_sec);
8302091SN/A    rup->ru_utime.tv_usec = htog(rup->ru_utime.tv_usec);
8312091SN/A
832360SN/A    rup->ru_stime.tv_sec = 0;
833360SN/A    rup->ru_stime.tv_usec = 0;
834360SN/A    rup->ru_maxrss = 0;
835360SN/A    rup->ru_ixrss = 0;
836360SN/A    rup->ru_idrss = 0;
837360SN/A    rup->ru_isrss = 0;
838360SN/A    rup->ru_minflt = 0;
839360SN/A    rup->ru_majflt = 0;
840360SN/A    rup->ru_nswap = 0;
841360SN/A    rup->ru_inblock = 0;
842360SN/A    rup->ru_oublock = 0;
843360SN/A    rup->ru_msgsnd = 0;
844360SN/A    rup->ru_msgrcv = 0;
845360SN/A    rup->ru_nsignals = 0;
846360SN/A    rup->ru_nvcsw = 0;
847360SN/A    rup->ru_nivcsw = 0;
848360SN/A
8492680Sktlim@umich.edu    rup.copyOut(tc->getMemPort());
850360SN/A
8511458SN/A    return 0;
852360SN/A}
853360SN/A
8542553SN/A
8552553SN/A
8562553SN/A
8571354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
858