syscall_emul.hh revision 10633
1360SN/A/*
210027SChris.Adeniyi-Jones@arm.com * Copyright (c) 2012-2013 ARM Limited
310027SChris.Adeniyi-Jones@arm.com * All rights reserved
410027SChris.Adeniyi-Jones@arm.com *
510027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
610027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
710027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
810027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
910027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1010027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1110027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1210027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1310027SChris.Adeniyi-Jones@arm.com *
141458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
15360SN/A * All rights reserved.
16360SN/A *
17360SN/A * Redistribution and use in source and binary forms, with or without
18360SN/A * modification, are permitted provided that the following conditions are
19360SN/A * met: redistributions of source code must retain the above copyright
20360SN/A * notice, this list of conditions and the following disclaimer;
21360SN/A * redistributions in binary form must reproduce the above copyright
22360SN/A * notice, this list of conditions and the following disclaimer in the
23360SN/A * documentation and/or other materials provided with the distribution;
24360SN/A * neither the name of the copyright holders nor the names of its
25360SN/A * contributors may be used to endorse or promote products derived from
26360SN/A * this software without specific prior written permission.
27360SN/A *
28360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
412665Ssaidi@eecs.umich.edu *          Kevin Lim
42360SN/A */
43360SN/A
441354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
451354SN/A#define __SIM_SYSCALL_EMUL_HH__
46360SN/A
472764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
489202Spalle@lyckegaard.dk  defined(__FreeBSD__) || defined(__CYGWIN__) || \
499202Spalle@lyckegaard.dk  defined(__NetBSD__))
502064SN/A
51360SN/A///
52360SN/A/// @file syscall_emul.hh
53360SN/A///
54360SN/A/// This file defines objects used to emulate syscalls from the target
55360SN/A/// application on the host machine.
56360SN/A
571809SN/A#ifdef __CYGWIN32__
585543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
591809SN/A#endif
603113Sgblack@eecs.umich.edu#include <sys/stat.h>
618229Snate@binkert.org#include <sys/time.h>
628229Snate@binkert.org#include <sys/uio.h>
633113Sgblack@eecs.umich.edu#include <fcntl.h>
647075Snate@binkert.org
658229Snate@binkert.org#include <cerrno>
667075Snate@binkert.org#include <string>
67360SN/A
682474SN/A#include "base/chunk_generator.hh"
695543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
702462SN/A#include "base/misc.hh"
711354SN/A#include "base/trace.hh"
726216Snate@binkert.org#include "base/types.hh"
736658Snate@binkert.org#include "config/the_isa.hh"
742474SN/A#include "cpu/base.hh"
752680Sktlim@umich.edu#include "cpu/thread_context.hh"
768232Snate@binkert.org#include "debug/SyscallVerbose.hh"
778229Snate@binkert.org#include "mem/page_table.hh"
787678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
7910496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
808229Snate@binkert.org#include "sim/process.hh"
8110497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
828766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
836640Svince@csl.cornell.edu#include "sim/system.hh"
84360SN/A
85360SN/A///
86360SN/A/// System call descriptor.
87360SN/A///
88360SN/Aclass SyscallDesc {
89360SN/A
90360SN/A  public:
91360SN/A
92378SN/A    /// Typedef for target syscall handler functions.
931450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
943114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
95360SN/A
965543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
975543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
985543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
99360SN/A
100360SN/A    /// Flag values for controlling syscall behavior.
101360SN/A    enum Flags {
102360SN/A        /// Don't set return regs according to funcPtr return value.
103360SN/A        /// Used for syscalls with non-standard return conventions
1042680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
105360SN/A        /// sigreturn).
106360SN/A        SuppressReturnValue = 1
107360SN/A    };
108360SN/A
109360SN/A    /// Constructor.
110360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
111360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
112360SN/A    {
113360SN/A    }
114360SN/A
115360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1163114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
117360SN/A};
118360SN/A
119360SN/A
120360SN/A//////////////////////////////////////////////////////////////////////
121360SN/A//
122360SN/A// The following emulation functions are generic enough that they
123360SN/A// don't need to be recompiled for different emulated OS's.  They are
124360SN/A// defined in sim/syscall_emul.cc.
125360SN/A//
126360SN/A//////////////////////////////////////////////////////////////////////
127360SN/A
128360SN/A
129378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1301706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1313114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
132378SN/A
133378SN/A/// Handler for unimplemented syscalls that we never intend to
134378SN/A/// implement (signal handling, etc.) and should not affect the correct
135378SN/A/// behavior of the program.  Print a warning only if the appropriate
136378SN/A/// trace flag is enabled.  Return success to the target program.
1371706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1383114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1398149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
1408149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
141360SN/A
1426109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1431706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1443114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
145378SN/A
1466109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1476109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1486109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1496109Ssanchezd@stanford.edu
150378SN/A/// Target getpagesize() handler.
1511706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1523114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
153378SN/A
1545748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1555748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1565748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
157378SN/A
158378SN/A/// Target close() handler.
1591706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1603114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
161378SN/A
162378SN/A/// Target read() handler.
1631706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1643114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
165378SN/A
166378SN/A/// Target write() handler.
1671706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1683114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
169378SN/A
170378SN/A/// Target lseek() handler.
1711706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1723114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
173378SN/A
1744118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1754118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1764118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1774118Sgblack@eecs.umich.edu
178378SN/A/// Target munmap() handler.
1791706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1803114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
181378SN/A
182378SN/A/// Target gethostname() handler.
1831706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
185360SN/A
1865513SMichael.Adler@intel.com/// Target getcwd() handler.
1875513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
1885513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
1895513SMichael.Adler@intel.com
19010203SAli.Saidi@ARM.com/// Target readlink() handler.
19110203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19210203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
19310203SAli.Saidi@ARM.com                           int index = 0);
1945513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
1955513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
1965513SMichael.Adler@intel.com
197511SN/A/// Target unlink() handler.
19810633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
19910633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
20010633Smichaelupton@gmail.com                           int index);
2011706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2023114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
203511SN/A
2045513SMichael.Adler@intel.com/// Target mkdir() handler.
2055513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2065513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2075513SMichael.Adler@intel.com
208511SN/A/// Target rename() handler.
2091706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2103114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2111706SN/A
2121706SN/A
2131706SN/A/// Target truncate() handler.
2141706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2153114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2161706SN/A
2171706SN/A
2181706SN/A/// Target ftruncate() handler.
2191706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2203114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2211706SN/A
222511SN/A
2236703Svince@csl.cornell.edu/// Target truncate64() handler.
2246703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2256703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2266703Svince@csl.cornell.edu
2276685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2286685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2296685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2306685Stjones1@inf.ed.ac.uk
2316685Stjones1@inf.ed.ac.uk
2325513SMichael.Adler@intel.com/// Target umask() handler.
2335513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2345513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2355513SMichael.Adler@intel.com
2365513SMichael.Adler@intel.com
2371999SN/A/// Target chown() handler.
2381999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2393114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2401999SN/A
2411999SN/A
2421999SN/A/// Target fchown() handler.
2431999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2443114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2451999SN/A
2463079Sstever@eecs.umich.edu/// Target dup() handler.
2473079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2483114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2493079Sstever@eecs.umich.edu
2502093SN/A/// Target fnctl() handler.
2512093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2523114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2532093SN/A
2542687Sksewell@umich.edu/// Target fcntl64() handler.
2552687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2563114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2572687Sksewell@umich.edu
2582238SN/A/// Target setuid() handler.
2592238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2603114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2612238SN/A
2622238SN/A/// Target getpid() handler.
2632238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2643114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2652238SN/A
2662238SN/A/// Target getuid() handler.
2672238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2692238SN/A
2702238SN/A/// Target getgid() handler.
2712238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2723114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2732238SN/A
2742238SN/A/// Target getppid() handler.
2752238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2763114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2772238SN/A
2782238SN/A/// Target geteuid() handler.
2792238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2812238SN/A
2822238SN/A/// Target getegid() handler.
2832238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2852238SN/A
2866109Ssanchezd@stanford.edu/// Target clone() handler.
2876109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
2886109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
2892238SN/A
2909455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2919455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2929455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
29310203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
29410203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
29510203SAli.Saidi@ARM.com                               int index);
2969455Smitch.hayenga+gem5@gmail.com
2979112Smarc.orr@gmail.com/// Futex system call
2989112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
2999112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3009112Smarc.orr@gmail.comtemplate <class OS>
3019112Smarc.orr@gmail.comSyscallReturn
3029112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3039112Smarc.orr@gmail.com          ThreadContext *tc)
3049112Smarc.orr@gmail.com{
3059112Smarc.orr@gmail.com    int index_uaddr = 0;
3069112Smarc.orr@gmail.com    int index_op = 1;
3079112Smarc.orr@gmail.com    int index_val = 2;
3089112Smarc.orr@gmail.com    int index_timeout = 3;
3099112Smarc.orr@gmail.com
3109112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3119112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3129112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3139112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3149112Smarc.orr@gmail.com
3159112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3169112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3179112Smarc.orr@gmail.com
3189112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3199112Smarc.orr@gmail.com            uaddr, op, val);
3209112Smarc.orr@gmail.com
3219238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3229112Smarc.orr@gmail.com
3239112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3249112Smarc.orr@gmail.com        if (timeout != 0) {
3259112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3269112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3279112Smarc.orr@gmail.com        }
3289112Smarc.orr@gmail.com
3299112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3309112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3319112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3329112Smarc.orr@gmail.com        delete buf;
3339112Smarc.orr@gmail.com
3349112Smarc.orr@gmail.com        if(val != mem_val) {
3359112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3369112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3379112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3389112Smarc.orr@gmail.com        }
3399112Smarc.orr@gmail.com
3409112Smarc.orr@gmail.com        // Queue the thread context
3419112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3429112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3439112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3449112Smarc.orr@gmail.com        } else {
3459112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3469112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3479112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3489112Smarc.orr@gmail.com        }
3499112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3509112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3519112Smarc.orr@gmail.com                                "thread context\n");
3529112Smarc.orr@gmail.com        tc->suspend();
3539112Smarc.orr@gmail.com        return 0;
3549112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3559112Smarc.orr@gmail.com        int wokenUp = 0;
3569112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3579112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3589112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3599112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3609112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3619112Smarc.orr@gmail.com                tcWaitList->pop_front();
3629112Smarc.orr@gmail.com                wokenUp++;
3639112Smarc.orr@gmail.com            }
3649112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
3659112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3669112Smarc.orr@gmail.com                delete tcWaitList;
3679112Smarc.orr@gmail.com            }
3689112Smarc.orr@gmail.com        }
3699112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3709112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3719112Smarc.orr@gmail.com        return wokenUp;
3729112Smarc.orr@gmail.com    } else {
3739238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3749112Smarc.orr@gmail.com        return 0;
3759112Smarc.orr@gmail.com    }
3769112Smarc.orr@gmail.com
3779112Smarc.orr@gmail.com}
3789112Smarc.orr@gmail.com
3792238SN/A
3802238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3812238SN/A/// returning a second value in a register other than the normal return register
3822238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3833114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3842238SN/A
3852238SN/A/// Target getpidPseudo() handler.
3862238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3873114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3882238SN/A
3892238SN/A/// Target getuidPseudo() handler.
3902238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3913114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3922238SN/A
3932238SN/A/// Target getgidPseudo() handler.
3942238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3953114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3962238SN/A
3972238SN/A
3981354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3991354SN/Aconst int one_million = 1000000;
4001354SN/A
4011354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4021354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4031354SN/A/// real-world time) to keep simulations repeatable.
4041354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4051354SN/A
4061354SN/A/// Helper function to convert current elapsed time to seconds and
4071354SN/A/// microseconds.
4081354SN/Atemplate <class T1, class T2>
4091354SN/Avoid
4101354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4111354SN/A{
4127823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4131354SN/A    sec = elapsed_usecs / one_million;
4141354SN/A    usec = elapsed_usecs % one_million;
4151354SN/A}
4161354SN/A
417360SN/A//////////////////////////////////////////////////////////////////////
418360SN/A//
419360SN/A// The following emulation functions are generic, but need to be
420360SN/A// templated to account for differences in types, constants, etc.
421360SN/A//
422360SN/A//////////////////////////////////////////////////////////////////////
423360SN/A
4243113Sgblack@eecs.umich.edu#if NO_STAT64
4253113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4263113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4273113Sgblack@eecs.umich.edu#else
4283113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4293113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4303113Sgblack@eecs.umich.edu#endif
4313113Sgblack@eecs.umich.edu
4323113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4333113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4343113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4353113Sgblack@eecs.umich.edu
4363113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4373113Sgblack@eecs.umich.edustatic void
4383113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4393113Sgblack@eecs.umich.edu{
4404189Sgblack@eecs.umich.edu    using namespace TheISA;
4414189Sgblack@eecs.umich.edu
4423113Sgblack@eecs.umich.edu    if (fakeTTY)
4433113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4443113Sgblack@eecs.umich.edu    else
4453113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4468737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4473113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4488737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4493277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4505515SMichael.Adler@intel.com    if (fakeTTY) {
4515515SMichael.Adler@intel.com        // Claim to be a character device
4525515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4535515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4545515SMichael.Adler@intel.com    }
4558737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4563277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4578737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4583277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4598737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4603277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4618737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4623113Sgblack@eecs.umich.edu    if (fakeTTY)
4633113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4643113Sgblack@eecs.umich.edu    else
4653113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4668737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4673113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4688737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4693114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4708737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4713114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4728737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4733114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4748737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4754061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4764061Sgblack@eecs.umich.edu    // consistently across different hosts.
4774061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4788737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4793113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4808737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4813113Sgblack@eecs.umich.edu}
4823113Sgblack@eecs.umich.edu
4833113Sgblack@eecs.umich.edu// Same for stat64
4843113Sgblack@eecs.umich.edu
4853113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4863113Sgblack@eecs.umich.edustatic void
4873113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4883113Sgblack@eecs.umich.edu{
4894189Sgblack@eecs.umich.edu    using namespace TheISA;
4904189Sgblack@eecs.umich.edu
4913113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4923113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4933113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4948737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
4953113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4968737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
4973113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4988737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
4993113Sgblack@eecs.umich.edu#else
5003113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5013113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5023113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5033113Sgblack@eecs.umich.edu#endif
5043113Sgblack@eecs.umich.edu}
5053113Sgblack@eecs.umich.edu
5063113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5073113Sgblack@eecs.umich.edutemplate<class OS>
5083113Sgblack@eecs.umich.edustatic void
5098852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5103113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5113113Sgblack@eecs.umich.edu{
5123113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5133113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5143113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5153113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5163113Sgblack@eecs.umich.edu}
5173113Sgblack@eecs.umich.edu
5183113Sgblack@eecs.umich.edutemplate<class OS>
5193113Sgblack@eecs.umich.edustatic void
5208852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5213113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5223113Sgblack@eecs.umich.edu{
5233113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5243113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5256686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5263113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5273113Sgblack@eecs.umich.edu}
5283113Sgblack@eecs.umich.edu
529378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
530378SN/A/// only to find out if their stdout is a tty, to determine whether to
5319141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5329141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
533360SN/Atemplate <class OS>
5341450SN/ASyscallReturn
5353114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5362680Sktlim@umich.edu          ThreadContext *tc)
537360SN/A{
5386701Sgblack@eecs.umich.edu    int index = 0;
5396701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5406701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
541360SN/A
5421969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
543360SN/A
54410496Ssteve.reinhardt@amd.com    Process::FdMap *fdObj = process->sim_fd_obj(fd);
54510496Ssteve.reinhardt@amd.com
54610496Ssteve.reinhardt@amd.com    if (fdObj == NULL) {
547360SN/A        // doesn't map to any simulator fd: not a valid target fd
5481458SN/A        return -EBADF;
549360SN/A    }
550360SN/A
55110496Ssteve.reinhardt@amd.com    if (fdObj->driver != NULL) {
55210496Ssteve.reinhardt@amd.com        return fdObj->driver->ioctl(process, tc, req);
55310496Ssteve.reinhardt@amd.com    }
55410496Ssteve.reinhardt@amd.com
5559141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5561458SN/A        return -ENOTTY;
5579141Smarc.orr@gmail.com    }
558360SN/A
5599141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5609141Smarc.orr@gmail.com         fd, req, tc->pcState());
5619141Smarc.orr@gmail.com    return -ENOTTY;
562360SN/A}
563360SN/A
564360SN/Atemplate <class OS>
56510027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5663114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
56710027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
568360SN/A{
569360SN/A    std::string path;
570360SN/A
5718852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5726701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5731458SN/A        return -EFAULT;
574360SN/A
5756701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5766701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
577360SN/A    int hostFlags = 0;
578360SN/A
579360SN/A    // translate open flags
580360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
581360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
582360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
583360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
584360SN/A        }
585360SN/A    }
586360SN/A
587360SN/A    // any target flags left?
588360SN/A    if (tgtFlags != 0)
5891706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
590360SN/A
591360SN/A#ifdef __CYGWIN32__
592360SN/A    hostFlags |= O_BINARY;
593360SN/A#endif
594360SN/A
5953669Sbinkertn@umich.edu    // Adjust path for current working directory
5963669Sbinkertn@umich.edu    path = process->fullPath(path);
5973669Sbinkertn@umich.edu
5981706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5991706SN/A
60010496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
60110496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
60210496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
60310496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
60410496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
60510496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
60610496Ssteve.reinhardt@amd.com            return -ENOENT;
60710496Ssteve.reinhardt@amd.com        }
60810496Ssteve.reinhardt@amd.com
60910496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
61010496Ssteve.reinhardt@amd.com        if (drv != NULL) {
61110496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
61210496Ssteve.reinhardt@amd.com            // process if necessary.
61310496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
61410496Ssteve.reinhardt@amd.com        }
61510496Ssteve.reinhardt@amd.com
61610496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
61710496Ssteve.reinhardt@amd.com        // /dev/zero
61810496Ssteve.reinhardt@amd.com    }
61910496Ssteve.reinhardt@amd.com
6205795Ssaidi@eecs.umich.edu    int fd;
6219143Ssteve.reinhardt@amd.com    int local_errno;
6229142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6239142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6249143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6255795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6269143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6275795Ssaidi@eecs.umich.edu     } else {
6285795Ssaidi@eecs.umich.edu        // open the file
6295795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6309143Ssteve.reinhardt@amd.com        local_errno = errno;
6315795Ssaidi@eecs.umich.edu     }
632360SN/A
6339143Ssteve.reinhardt@amd.com    if (fd == -1)
6349143Ssteve.reinhardt@amd.com        return -local_errno;
6359143Ssteve.reinhardt@amd.com
6369143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
637360SN/A}
638360SN/A
63910027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
64010027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
64110027SChris.Adeniyi-Jones@arm.comSyscallReturn
64210027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
64310027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
64410027SChris.Adeniyi-Jones@arm.com{
64510027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
64610027SChris.Adeniyi-Jones@arm.com}
64710027SChris.Adeniyi-Jones@arm.com
64810027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
64910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
65010027SChris.Adeniyi-Jones@arm.comSyscallReturn
65110027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
65210027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
65310027SChris.Adeniyi-Jones@arm.com{
65410027SChris.Adeniyi-Jones@arm.com    int index = 0;
65510027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
65610027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
65710027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
65810027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
65910027SChris.Adeniyi-Jones@arm.com}
66010027SChris.Adeniyi-Jones@arm.com
66110633Smichaelupton@gmail.com/// Target unlinkat() handler.
66210633Smichaelupton@gmail.comtemplate <class OS>
66310633Smichaelupton@gmail.comSyscallReturn
66410633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66510633Smichaelupton@gmail.com             ThreadContext *tc)
66610633Smichaelupton@gmail.com{
66710633Smichaelupton@gmail.com    int index = 0;
66810633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
66910633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
67010633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
67110633Smichaelupton@gmail.com
67210633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
67310633Smichaelupton@gmail.com}
67410633Smichaelupton@gmail.com
67510203SAli.Saidi@ARM.com/// Target facessat() handler
67610203SAli.Saidi@ARM.comtemplate <class OS>
67710203SAli.Saidi@ARM.comSyscallReturn
67810203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67910203SAli.Saidi@ARM.com        ThreadContext *tc)
68010203SAli.Saidi@ARM.com{
68110203SAli.Saidi@ARM.com    int index = 0;
68210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
68310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
68410203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
68510203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
68610203SAli.Saidi@ARM.com}
68710203SAli.Saidi@ARM.com
68810203SAli.Saidi@ARM.com/// Target readlinkat() handler
68910203SAli.Saidi@ARM.comtemplate <class OS>
69010203SAli.Saidi@ARM.comSyscallReturn
69110203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69210203SAli.Saidi@ARM.com        ThreadContext *tc)
69310203SAli.Saidi@ARM.com{
69410203SAli.Saidi@ARM.com    int index = 0;
69510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
69610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
69710203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
69810203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
69910203SAli.Saidi@ARM.com}
70010203SAli.Saidi@ARM.com
7016640Svince@csl.cornell.edu/// Target sysinfo() handler.
7026640Svince@csl.cornell.edutemplate <class OS>
7036640Svince@csl.cornell.eduSyscallReturn
7046640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7056640Svince@csl.cornell.edu         ThreadContext *tc)
7066640Svince@csl.cornell.edu{
7076640Svince@csl.cornell.edu
7086701Sgblack@eecs.umich.edu    int index = 0;
7096701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
7106701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
7116640Svince@csl.cornell.edu
7126701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7136701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7146640Svince@csl.cornell.edu
7158706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7166640Svince@csl.cornell.edu
7176701Sgblack@eecs.umich.edu    return 0;
7186640Svince@csl.cornell.edu}
719360SN/A
7201999SN/A/// Target chmod() handler.
7211999SN/Atemplate <class OS>
7221999SN/ASyscallReturn
7233114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7242680Sktlim@umich.edu          ThreadContext *tc)
7251999SN/A{
7261999SN/A    std::string path;
7271999SN/A
7286701Sgblack@eecs.umich.edu    int index = 0;
7298852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7306701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7311999SN/A        return -EFAULT;
7326701Sgblack@eecs.umich.edu    }
7331999SN/A
7346701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7351999SN/A    mode_t hostMode = 0;
7361999SN/A
7371999SN/A    // XXX translate mode flags via OS::something???
7381999SN/A    hostMode = mode;
7391999SN/A
7403669Sbinkertn@umich.edu    // Adjust path for current working directory
7413669Sbinkertn@umich.edu    path = process->fullPath(path);
7423669Sbinkertn@umich.edu
7431999SN/A    // do the chmod
7441999SN/A    int result = chmod(path.c_str(), hostMode);
7451999SN/A    if (result < 0)
7462218SN/A        return -errno;
7471999SN/A
7481999SN/A    return 0;
7491999SN/A}
7501999SN/A
7511999SN/A
7521999SN/A/// Target fchmod() handler.
7531999SN/Atemplate <class OS>
7541999SN/ASyscallReturn
7553114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7562680Sktlim@umich.edu           ThreadContext *tc)
7571999SN/A{
7586701Sgblack@eecs.umich.edu    int index = 0;
7596701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7601999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7611999SN/A        // doesn't map to any simulator fd: not a valid target fd
7621999SN/A        return -EBADF;
7631999SN/A    }
7641999SN/A
7656701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7661999SN/A    mode_t hostMode = 0;
7671999SN/A
7681999SN/A    // XXX translate mode flags via OS::someting???
7691999SN/A    hostMode = mode;
7701999SN/A
7711999SN/A    // do the fchmod
7721999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7731999SN/A    if (result < 0)
7742218SN/A        return -errno;
7751999SN/A
7761999SN/A    return 0;
7771999SN/A}
7781999SN/A
7795877Shsul@eecs.umich.edu/// Target mremap() handler.
7805877Shsul@eecs.umich.edutemplate <class OS>
7815877Shsul@eecs.umich.eduSyscallReturn
7825877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7835877Shsul@eecs.umich.edu{
7846701Sgblack@eecs.umich.edu    int index = 0;
7856701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7866701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7876701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7886701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
78910027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
79010027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
79110027SChris.Adeniyi-Jones@arm.com
79210027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
79310027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
7945877Shsul@eecs.umich.edu
79510318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
79610318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
7975877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7985877Shsul@eecs.umich.edu        return -EINVAL;
7995877Shsul@eecs.umich.edu    }
8005877Shsul@eecs.umich.edu
80110486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
80210486Stjablin@gmail.com
8035877Shsul@eecs.umich.edu    if (new_length > old_length) {
80410027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
80510027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8065877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8078601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8085877Shsul@eecs.umich.edu            process->mmap_end += diff;
8095877Shsul@eecs.umich.edu            return start;
8105877Shsul@eecs.umich.edu        } else {
81110027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8125877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8135877Shsul@eecs.umich.edu                return -ENOMEM;
8145877Shsul@eecs.umich.edu            } else {
81510027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
81610027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
81710027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
81810027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
81910027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
82010027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8215877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
82210027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
82310027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
82410027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
82510027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
82610027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
82710027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
82810027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
82910027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
83010027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
83110027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
83210027SChris.Adeniyi-Jones@arm.com                }
83310027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
83410027SChris.Adeniyi-Jones@arm.com                return new_start;
8355877Shsul@eecs.umich.edu            }
8365877Shsul@eecs.umich.edu        }
8375877Shsul@eecs.umich.edu    } else {
83810027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
83910027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8408601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
84110027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8425877Shsul@eecs.umich.edu    }
8435877Shsul@eecs.umich.edu}
8441999SN/A
845378SN/A/// Target stat() handler.
846360SN/Atemplate <class OS>
8471450SN/ASyscallReturn
8483114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8492680Sktlim@umich.edu         ThreadContext *tc)
850360SN/A{
851360SN/A    std::string path;
852360SN/A
8536701Sgblack@eecs.umich.edu    int index = 0;
8548852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8556701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8566701Sgblack@eecs.umich.edu        return -EFAULT;
8576701Sgblack@eecs.umich.edu    }
8586701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
859360SN/A
8603669Sbinkertn@umich.edu    // Adjust path for current working directory
8613669Sbinkertn@umich.edu    path = process->fullPath(path);
8623669Sbinkertn@umich.edu
863360SN/A    struct stat hostBuf;
864360SN/A    int result = stat(path.c_str(), &hostBuf);
865360SN/A
866360SN/A    if (result < 0)
8672218SN/A        return -errno;
868360SN/A
8698706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
870360SN/A
8711458SN/A    return 0;
872360SN/A}
873360SN/A
874360SN/A
8755074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8765074Ssaidi@eecs.umich.edutemplate <class OS>
8775074Ssaidi@eecs.umich.eduSyscallReturn
8785074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8795074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8805074Ssaidi@eecs.umich.edu{
8815074Ssaidi@eecs.umich.edu    std::string path;
8825074Ssaidi@eecs.umich.edu
8836701Sgblack@eecs.umich.edu    int index = 0;
8848852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8856701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8865074Ssaidi@eecs.umich.edu        return -EFAULT;
8876701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8885074Ssaidi@eecs.umich.edu
8895074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8905074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8915074Ssaidi@eecs.umich.edu
8925208Ssaidi@eecs.umich.edu#if NO_STAT64
8935208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8945208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8955208Ssaidi@eecs.umich.edu#else
8965074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8975074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8985208Ssaidi@eecs.umich.edu#endif
8995074Ssaidi@eecs.umich.edu
9005074Ssaidi@eecs.umich.edu    if (result < 0)
9015074Ssaidi@eecs.umich.edu        return -errno;
9025074Ssaidi@eecs.umich.edu
9038706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9045074Ssaidi@eecs.umich.edu
9055074Ssaidi@eecs.umich.edu    return 0;
9065074Ssaidi@eecs.umich.edu}
9075074Ssaidi@eecs.umich.edu
9085074Ssaidi@eecs.umich.edu
90910027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
91010027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
91110027SChris.Adeniyi-Jones@arm.comSyscallReturn
91210027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
91310027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
91410027SChris.Adeniyi-Jones@arm.com{
91510027SChris.Adeniyi-Jones@arm.com    int index = 0;
91610027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
91710027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
91810027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
91910027SChris.Adeniyi-Jones@arm.com
92010027SChris.Adeniyi-Jones@arm.com    std::string path;
92110027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
92210027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
92310027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
92410027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
92510027SChris.Adeniyi-Jones@arm.com
92610027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
92710027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
92810027SChris.Adeniyi-Jones@arm.com
92910027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
93010027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
93110027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
93210027SChris.Adeniyi-Jones@arm.com#else
93310027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
93410027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
93510027SChris.Adeniyi-Jones@arm.com#endif
93610027SChris.Adeniyi-Jones@arm.com
93710027SChris.Adeniyi-Jones@arm.com    if (result < 0)
93810027SChris.Adeniyi-Jones@arm.com        return -errno;
93910027SChris.Adeniyi-Jones@arm.com
94010027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
94110027SChris.Adeniyi-Jones@arm.com
94210027SChris.Adeniyi-Jones@arm.com    return 0;
94310027SChris.Adeniyi-Jones@arm.com}
94410027SChris.Adeniyi-Jones@arm.com
94510027SChris.Adeniyi-Jones@arm.com
9461999SN/A/// Target fstat64() handler.
9471999SN/Atemplate <class OS>
9481999SN/ASyscallReturn
9493114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9502680Sktlim@umich.edu            ThreadContext *tc)
9511999SN/A{
9526701Sgblack@eecs.umich.edu    int index = 0;
9536701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9546701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9551999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9561999SN/A        // doesn't map to any simulator fd: not a valid target fd
9571999SN/A        return -EBADF;
9581999SN/A    }
9591999SN/A
9602764Sstever@eecs.umich.edu#if NO_STAT64
9612064SN/A    struct stat  hostBuf;
9622064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
9632064SN/A#else
9642064SN/A    struct stat64  hostBuf;
9651999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
9662064SN/A#endif
9671999SN/A
9681999SN/A    if (result < 0)
9692218SN/A        return -errno;
9701999SN/A
9718706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9721999SN/A
9731999SN/A    return 0;
9741999SN/A}
9751999SN/A
9761999SN/A
977378SN/A/// Target lstat() handler.
978360SN/Atemplate <class OS>
9791450SN/ASyscallReturn
9803114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9812680Sktlim@umich.edu          ThreadContext *tc)
982360SN/A{
983360SN/A    std::string path;
984360SN/A
9856701Sgblack@eecs.umich.edu    int index = 0;
9868852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9876701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9886701Sgblack@eecs.umich.edu        return -EFAULT;
9896701Sgblack@eecs.umich.edu    }
9906701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
991360SN/A
9923669Sbinkertn@umich.edu    // Adjust path for current working directory
9933669Sbinkertn@umich.edu    path = process->fullPath(path);
9943669Sbinkertn@umich.edu
995360SN/A    struct stat hostBuf;
996360SN/A    int result = lstat(path.c_str(), &hostBuf);
997360SN/A
998360SN/A    if (result < 0)
9991458SN/A        return -errno;
1000360SN/A
10018706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1002360SN/A
10031458SN/A    return 0;
1004360SN/A}
1005360SN/A
10061999SN/A/// Target lstat64() handler.
10071999SN/Atemplate <class OS>
10081999SN/ASyscallReturn
10093114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10102680Sktlim@umich.edu            ThreadContext *tc)
10111999SN/A{
10121999SN/A    std::string path;
10131999SN/A
10146701Sgblack@eecs.umich.edu    int index = 0;
10158852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10166701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10176701Sgblack@eecs.umich.edu        return -EFAULT;
10186701Sgblack@eecs.umich.edu    }
10196701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10201999SN/A
10213669Sbinkertn@umich.edu    // Adjust path for current working directory
10223669Sbinkertn@umich.edu    path = process->fullPath(path);
10233669Sbinkertn@umich.edu
10242764Sstever@eecs.umich.edu#if NO_STAT64
10252064SN/A    struct stat hostBuf;
10262064SN/A    int result = lstat(path.c_str(), &hostBuf);
10272064SN/A#else
10281999SN/A    struct stat64 hostBuf;
10291999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10302064SN/A#endif
10311999SN/A
10321999SN/A    if (result < 0)
10331999SN/A        return -errno;
10341999SN/A
10358706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10361999SN/A
10371999SN/A    return 0;
10381999SN/A}
10391999SN/A
1040378SN/A/// Target fstat() handler.
1041360SN/Atemplate <class OS>
10421450SN/ASyscallReturn
10433114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10442680Sktlim@umich.edu          ThreadContext *tc)
1045360SN/A{
10466701Sgblack@eecs.umich.edu    int index = 0;
10476701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10486701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1049360SN/A
10501969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1051360SN/A
1052360SN/A    if (fd < 0)
10531458SN/A        return -EBADF;
1054360SN/A
1055360SN/A    struct stat hostBuf;
1056360SN/A    int result = fstat(fd, &hostBuf);
1057360SN/A
1058360SN/A    if (result < 0)
10591458SN/A        return -errno;
1060360SN/A
10618706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10622021SN/A
10631458SN/A    return 0;
1064360SN/A}
1065360SN/A
1066360SN/A
10671706SN/A/// Target statfs() handler.
10681706SN/Atemplate <class OS>
10691706SN/ASyscallReturn
10703114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10712680Sktlim@umich.edu           ThreadContext *tc)
10721706SN/A{
10731706SN/A    std::string path;
10741706SN/A
10756701Sgblack@eecs.umich.edu    int index = 0;
10768852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10776701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10786701Sgblack@eecs.umich.edu        return -EFAULT;
10796701Sgblack@eecs.umich.edu    }
10806701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10811706SN/A
10823669Sbinkertn@umich.edu    // Adjust path for current working directory
10833669Sbinkertn@umich.edu    path = process->fullPath(path);
10843669Sbinkertn@umich.edu
10851706SN/A    struct statfs hostBuf;
10861706SN/A    int result = statfs(path.c_str(), &hostBuf);
10871706SN/A
10881706SN/A    if (result < 0)
10892218SN/A        return -errno;
10901706SN/A
10918706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10921706SN/A
10931706SN/A    return 0;
10941706SN/A}
10951706SN/A
10961706SN/A
10971706SN/A/// Target fstatfs() handler.
10981706SN/Atemplate <class OS>
10991706SN/ASyscallReturn
11003114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11012680Sktlim@umich.edu            ThreadContext *tc)
11021706SN/A{
11036701Sgblack@eecs.umich.edu    int index = 0;
11046701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11056701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11061706SN/A
11071706SN/A    if (fd < 0)
11081706SN/A        return -EBADF;
11091706SN/A
11101706SN/A    struct statfs hostBuf;
11111706SN/A    int result = fstatfs(fd, &hostBuf);
11121706SN/A
11131706SN/A    if (result < 0)
11142218SN/A        return -errno;
11151706SN/A
11168706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11171706SN/A
11181706SN/A    return 0;
11191706SN/A}
11201706SN/A
11211706SN/A
11221999SN/A/// Target writev() handler.
11231999SN/Atemplate <class OS>
11241999SN/ASyscallReturn
11253114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11262680Sktlim@umich.edu           ThreadContext *tc)
11271999SN/A{
11286701Sgblack@eecs.umich.edu    int index = 0;
11296701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11301999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11311999SN/A        // doesn't map to any simulator fd: not a valid target fd
11321999SN/A        return -EBADF;
11331999SN/A    }
11341999SN/A
11358852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11366701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11376701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11381999SN/A    struct iovec hiov[count];
11396227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11401999SN/A        typename OS::tgt_iovec tiov;
11412461SN/A
11428852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11438852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11448737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11451999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11468852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11478852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11481999SN/A    }
11491999SN/A
11501999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11511999SN/A
11526227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11531999SN/A        delete [] (char *)hiov[i].iov_base;
11541999SN/A
11551999SN/A    if (result < 0)
11562218SN/A        return -errno;
11571999SN/A
115810629Sjthestness@gmail.com    return result;
11591999SN/A}
11601999SN/A
11611999SN/A
1162378SN/A/// Target mmap() handler.
1163378SN/A///
1164378SN/A/// We don't really handle mmap().  If the target is mmaping an
1165378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1166378SN/A/// nothing (since memory is initialized to zero and the simulator
11678324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
11688324Ssteve.reinhardt@amd.com///
1169360SN/Atemplate <class OS>
11701450SN/ASyscallReturn
11713114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1172360SN/A{
11736701Sgblack@eecs.umich.edu    int index = 0;
11746701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
11756701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
11766701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
11776701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
11788324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
117910486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1180360SN/A
11819008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
11829008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
11839008Sgblack@eecs.umich.edu
11848324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
11858324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
11868324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
11878324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
11888324Ssteve.reinhardt@amd.com            return -EBADF;
11898324Ssteve.reinhardt@amd.com        }
11908324Ssteve.reinhardt@amd.com
11918324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
11928324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
11938324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
11948324Ssteve.reinhardt@amd.com            // another name on some platform
11958324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11968324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11978324Ssteve.reinhardt@amd.com        }
11988324Ssteve.reinhardt@amd.com    }
11995877Shsul@eecs.umich.edu
120010486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
120110486Stjablin@gmail.com
120210318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
120310486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12042544SN/A        warn("mmap failing: arguments not page-aligned: "
120510486Stjablin@gmail.com             "start 0x%x offset 0x%x",
120610486Stjablin@gmail.com             start, offset);
12072544SN/A        return -EINVAL;
1208360SN/A    }
1209360SN/A
12108600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12118600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12128600Ssteve.reinhardt@amd.com    bool clobber = false;
12138600Ssteve.reinhardt@amd.com
12148600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12158600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12168600Ssteve.reinhardt@amd.com
12178600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12188600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12198600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12208600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12218600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12228600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12238600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
122410485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
122510485SMichael.Adler@intel.com                return -EINVAL;
12268600Ssteve.reinhardt@amd.com            } else {
12278600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12288600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12298600Ssteve.reinhardt@amd.com                use_provided_address = false;
12308600Ssteve.reinhardt@amd.com            }
12318600Ssteve.reinhardt@amd.com        }
12322544SN/A    }
12332544SN/A
12348600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12358600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12368600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12378600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12388600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12398600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12408600Ssteve.reinhardt@amd.com        } else {
12418600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12428600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12438600Ssteve.reinhardt@amd.com        }
12446672Sgblack@eecs.umich.edu    }
12458600Ssteve.reinhardt@amd.com
12468601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12472544SN/A
12481458SN/A    return start;
1249360SN/A}
1250360SN/A
1251378SN/A/// Target getrlimit() handler.
1252360SN/Atemplate <class OS>
12531450SN/ASyscallReturn
12543114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12552680Sktlim@umich.edu        ThreadContext *tc)
1256360SN/A{
12576701Sgblack@eecs.umich.edu    int index = 0;
12586701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12596701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1260360SN/A
1261360SN/A    switch (resource) {
12622064SN/A        case OS::TGT_RLIMIT_STACK:
12635877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
12642064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
12658737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12668737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12672064SN/A            break;
1268360SN/A
12695877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
12705877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
12715877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
12728737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12738737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12745877Shsul@eecs.umich.edu            break;
12755877Shsul@eecs.umich.edu
12762064SN/A        default:
12772064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
12782064SN/A                << std::endl;
12792064SN/A            abort();
12802064SN/A            break;
1281360SN/A    }
1282360SN/A
12838706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
12841458SN/A    return 0;
1285360SN/A}
1286360SN/A
1287378SN/A/// Target gettimeofday() handler.
1288360SN/Atemplate <class OS>
12891450SN/ASyscallReturn
12903114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12912680Sktlim@umich.edu        ThreadContext *tc)
1292360SN/A{
12936701Sgblack@eecs.umich.edu    int index = 0;
12946701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1295360SN/A
1296360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1297360SN/A    tp->tv_sec += seconds_since_epoch;
12986109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12996109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1300360SN/A
13018706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1302360SN/A
13031458SN/A    return 0;
1304360SN/A}
1305360SN/A
1306360SN/A
13071999SN/A/// Target utimes() handler.
13081999SN/Atemplate <class OS>
13091999SN/ASyscallReturn
13103114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13112680Sktlim@umich.edu           ThreadContext *tc)
13121999SN/A{
13131999SN/A    std::string path;
13141999SN/A
13156701Sgblack@eecs.umich.edu    int index = 0;
13168852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13176701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13186701Sgblack@eecs.umich.edu        return -EFAULT;
13196701Sgblack@eecs.umich.edu    }
13201999SN/A
13216701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13226701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13238706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13241999SN/A
13251999SN/A    struct timeval hostTimeval[2];
13261999SN/A    for (int i = 0; i < 2; ++i)
13271999SN/A    {
13288737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13298737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13301999SN/A    }
13313669Sbinkertn@umich.edu
13323669Sbinkertn@umich.edu    // Adjust path for current working directory
13333669Sbinkertn@umich.edu    path = process->fullPath(path);
13343669Sbinkertn@umich.edu
13351999SN/A    int result = utimes(path.c_str(), hostTimeval);
13361999SN/A
13371999SN/A    if (result < 0)
13381999SN/A        return -errno;
13391999SN/A
13401999SN/A    return 0;
13411999SN/A}
1342378SN/A/// Target getrusage() function.
1343360SN/Atemplate <class OS>
13441450SN/ASyscallReturn
13453114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13462680Sktlim@umich.edu              ThreadContext *tc)
1347360SN/A{
13486701Sgblack@eecs.umich.edu    int index = 0;
13496701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13506701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1351360SN/A
13523670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13533670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1354360SN/A    rup->ru_stime.tv_sec = 0;
1355360SN/A    rup->ru_stime.tv_usec = 0;
1356360SN/A    rup->ru_maxrss = 0;
1357360SN/A    rup->ru_ixrss = 0;
1358360SN/A    rup->ru_idrss = 0;
1359360SN/A    rup->ru_isrss = 0;
1360360SN/A    rup->ru_minflt = 0;
1361360SN/A    rup->ru_majflt = 0;
1362360SN/A    rup->ru_nswap = 0;
1363360SN/A    rup->ru_inblock = 0;
1364360SN/A    rup->ru_oublock = 0;
1365360SN/A    rup->ru_msgsnd = 0;
1366360SN/A    rup->ru_msgrcv = 0;
1367360SN/A    rup->ru_nsignals = 0;
1368360SN/A    rup->ru_nvcsw = 0;
1369360SN/A    rup->ru_nivcsw = 0;
1370360SN/A
13713670Sbinkertn@umich.edu    switch (who) {
13723670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
13733670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
13748737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
13758737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
13763670Sbinkertn@umich.edu        break;
13773670Sbinkertn@umich.edu
13783670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
13793670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
13803670Sbinkertn@umich.edu        break;
13813670Sbinkertn@umich.edu
13823670Sbinkertn@umich.edu      default:
13833670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
13843670Sbinkertn@umich.edu        // plow ahead
13853670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
13863670Sbinkertn@umich.edu             who);
13873670Sbinkertn@umich.edu    }
13883670Sbinkertn@umich.edu
13898706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1390360SN/A
13911458SN/A    return 0;
1392360SN/A}
1393360SN/A
13946683Stjones1@inf.ed.ac.uk/// Target times() function.
13956683Stjones1@inf.ed.ac.uktemplate <class OS>
13966683Stjones1@inf.ed.ac.ukSyscallReturn
13976683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13986683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13996683Stjones1@inf.ed.ac.uk{
14006701Sgblack@eecs.umich.edu    int index = 0;
14016701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14026683Stjones1@inf.ed.ac.uk
14036683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14047823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14056683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14066683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14076683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14086683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14096683Stjones1@inf.ed.ac.uk
14106683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14118737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14126683Stjones1@inf.ed.ac.uk
14136683Stjones1@inf.ed.ac.uk    // Write back
14148706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14156683Stjones1@inf.ed.ac.uk
14166683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14176683Stjones1@inf.ed.ac.uk    return clocks;
14186683Stjones1@inf.ed.ac.uk}
14192553SN/A
14206684Stjones1@inf.ed.ac.uk/// Target time() function.
14216684Stjones1@inf.ed.ac.uktemplate <class OS>
14226684Stjones1@inf.ed.ac.ukSyscallReturn
14236684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14246684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14256684Stjones1@inf.ed.ac.uk{
14266684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14276684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14286684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14296684Stjones1@inf.ed.ac.uk
14306701Sgblack@eecs.umich.edu    int index = 0;
14316701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14326684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14336684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14348737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14358852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14368852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14376684Stjones1@inf.ed.ac.uk    }
14386684Stjones1@inf.ed.ac.uk    return sec;
14396684Stjones1@inf.ed.ac.uk}
14402553SN/A
14412553SN/A
14421354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1443