syscall_emul.hh revision 10497
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.
1981706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
1993114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
200511SN/A
2015513SMichael.Adler@intel.com/// Target mkdir() handler.
2025513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2035513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2045513SMichael.Adler@intel.com
205511SN/A/// Target rename() handler.
2061706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2073114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2081706SN/A
2091706SN/A
2101706SN/A/// Target truncate() handler.
2111706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2123114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2131706SN/A
2141706SN/A
2151706SN/A/// Target ftruncate() handler.
2161706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2173114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2181706SN/A
219511SN/A
2206703Svince@csl.cornell.edu/// Target truncate64() handler.
2216703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2226703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2236703Svince@csl.cornell.edu
2246685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2256685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2266685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2276685Stjones1@inf.ed.ac.uk
2286685Stjones1@inf.ed.ac.uk
2295513SMichael.Adler@intel.com/// Target umask() handler.
2305513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2315513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2325513SMichael.Adler@intel.com
2335513SMichael.Adler@intel.com
2341999SN/A/// Target chown() handler.
2351999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2363114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2371999SN/A
2381999SN/A
2391999SN/A/// Target fchown() handler.
2401999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2413114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2421999SN/A
2433079Sstever@eecs.umich.edu/// Target dup() handler.
2443079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2453114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2463079Sstever@eecs.umich.edu
2472093SN/A/// Target fnctl() handler.
2482093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2493114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2502093SN/A
2512687Sksewell@umich.edu/// Target fcntl64() handler.
2522687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2533114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2542687Sksewell@umich.edu
2552238SN/A/// Target setuid() handler.
2562238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2573114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2582238SN/A
2592238SN/A/// Target getpid() handler.
2602238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2613114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2622238SN/A
2632238SN/A/// Target getuid() handler.
2642238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2653114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2662238SN/A
2672238SN/A/// Target getgid() handler.
2682238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2693114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2702238SN/A
2712238SN/A/// Target getppid() handler.
2722238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2742238SN/A
2752238SN/A/// Target geteuid() handler.
2762238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2773114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2782238SN/A
2792238SN/A/// Target getegid() handler.
2802238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2813114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2822238SN/A
2836109Ssanchezd@stanford.edu/// Target clone() handler.
2846109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
2856109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
2862238SN/A
2879455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2889455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2899455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
29010203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
29110203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
29210203SAli.Saidi@ARM.com                               int index);
2939455Smitch.hayenga+gem5@gmail.com
2949112Smarc.orr@gmail.com/// Futex system call
2959112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
2969112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
2979112Smarc.orr@gmail.comtemplate <class OS>
2989112Smarc.orr@gmail.comSyscallReturn
2999112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3009112Smarc.orr@gmail.com          ThreadContext *tc)
3019112Smarc.orr@gmail.com{
3029112Smarc.orr@gmail.com    int index_uaddr = 0;
3039112Smarc.orr@gmail.com    int index_op = 1;
3049112Smarc.orr@gmail.com    int index_val = 2;
3059112Smarc.orr@gmail.com    int index_timeout = 3;
3069112Smarc.orr@gmail.com
3079112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3089112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3099112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3109112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3119112Smarc.orr@gmail.com
3129112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3139112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3149112Smarc.orr@gmail.com
3159112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3169112Smarc.orr@gmail.com            uaddr, op, val);
3179112Smarc.orr@gmail.com
3189238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3199112Smarc.orr@gmail.com
3209112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3219112Smarc.orr@gmail.com        if (timeout != 0) {
3229112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3239112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3249112Smarc.orr@gmail.com        }
3259112Smarc.orr@gmail.com
3269112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3279112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3289112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3299112Smarc.orr@gmail.com        delete buf;
3309112Smarc.orr@gmail.com
3319112Smarc.orr@gmail.com        if(val != mem_val) {
3329112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3339112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3349112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3359112Smarc.orr@gmail.com        }
3369112Smarc.orr@gmail.com
3379112Smarc.orr@gmail.com        // Queue the thread context
3389112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3399112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3409112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3419112Smarc.orr@gmail.com        } else {
3429112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3439112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3449112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3459112Smarc.orr@gmail.com        }
3469112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3479112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3489112Smarc.orr@gmail.com                                "thread context\n");
3499112Smarc.orr@gmail.com        tc->suspend();
3509112Smarc.orr@gmail.com        return 0;
3519112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3529112Smarc.orr@gmail.com        int wokenUp = 0;
3539112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3549112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3559112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3569112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3579112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3589112Smarc.orr@gmail.com                tcWaitList->pop_front();
3599112Smarc.orr@gmail.com                wokenUp++;
3609112Smarc.orr@gmail.com            }
3619112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
3629112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3639112Smarc.orr@gmail.com                delete tcWaitList;
3649112Smarc.orr@gmail.com            }
3659112Smarc.orr@gmail.com        }
3669112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3679112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3689112Smarc.orr@gmail.com        return wokenUp;
3699112Smarc.orr@gmail.com    } else {
3709238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3719112Smarc.orr@gmail.com        return 0;
3729112Smarc.orr@gmail.com    }
3739112Smarc.orr@gmail.com
3749112Smarc.orr@gmail.com}
3759112Smarc.orr@gmail.com
3762238SN/A
3772238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3782238SN/A/// returning a second value in a register other than the normal return register
3792238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3803114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3812238SN/A
3822238SN/A/// Target getpidPseudo() handler.
3832238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3843114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3852238SN/A
3862238SN/A/// Target getuidPseudo() handler.
3872238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3883114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3892238SN/A
3902238SN/A/// Target getgidPseudo() handler.
3912238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3923114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3932238SN/A
3942238SN/A
3951354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3961354SN/Aconst int one_million = 1000000;
3971354SN/A
3981354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3991354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4001354SN/A/// real-world time) to keep simulations repeatable.
4011354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4021354SN/A
4031354SN/A/// Helper function to convert current elapsed time to seconds and
4041354SN/A/// microseconds.
4051354SN/Atemplate <class T1, class T2>
4061354SN/Avoid
4071354SN/AgetElapsedTime(T1 &sec, T2 &usec)
4081354SN/A{
4097823Ssteve.reinhardt@amd.com    int elapsed_usecs = curTick() / SimClock::Int::us;
4101354SN/A    sec = elapsed_usecs / one_million;
4111354SN/A    usec = elapsed_usecs % one_million;
4121354SN/A}
4131354SN/A
414360SN/A//////////////////////////////////////////////////////////////////////
415360SN/A//
416360SN/A// The following emulation functions are generic, but need to be
417360SN/A// templated to account for differences in types, constants, etc.
418360SN/A//
419360SN/A//////////////////////////////////////////////////////////////////////
420360SN/A
4213113Sgblack@eecs.umich.edu#if NO_STAT64
4223113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4233113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4243113Sgblack@eecs.umich.edu#else
4253113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4263113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4273113Sgblack@eecs.umich.edu#endif
4283113Sgblack@eecs.umich.edu
4293113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4303113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4313113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4323113Sgblack@eecs.umich.edu
4333113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4343113Sgblack@eecs.umich.edustatic void
4353113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4363113Sgblack@eecs.umich.edu{
4374189Sgblack@eecs.umich.edu    using namespace TheISA;
4384189Sgblack@eecs.umich.edu
4393113Sgblack@eecs.umich.edu    if (fakeTTY)
4403113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4413113Sgblack@eecs.umich.edu    else
4423113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4438737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4443113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4458737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4463277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4475515SMichael.Adler@intel.com    if (fakeTTY) {
4485515SMichael.Adler@intel.com        // Claim to be a character device
4495515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4505515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4515515SMichael.Adler@intel.com    }
4528737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4533277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4548737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4553277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4568737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4573277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4588737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4593113Sgblack@eecs.umich.edu    if (fakeTTY)
4603113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4613113Sgblack@eecs.umich.edu    else
4623113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4638737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4643113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4658737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4663114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4678737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4683114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4698737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4703114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4718737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4724061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4734061Sgblack@eecs.umich.edu    // consistently across different hosts.
4744061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4758737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4763113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4778737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4783113Sgblack@eecs.umich.edu}
4793113Sgblack@eecs.umich.edu
4803113Sgblack@eecs.umich.edu// Same for stat64
4813113Sgblack@eecs.umich.edu
4823113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4833113Sgblack@eecs.umich.edustatic void
4843113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4853113Sgblack@eecs.umich.edu{
4864189Sgblack@eecs.umich.edu    using namespace TheISA;
4874189Sgblack@eecs.umich.edu
4883113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4893113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4903113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4918737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
4923113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4938737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
4943113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4958737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
4963113Sgblack@eecs.umich.edu#else
4973113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4983113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4993113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5003113Sgblack@eecs.umich.edu#endif
5013113Sgblack@eecs.umich.edu}
5023113Sgblack@eecs.umich.edu
5033113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5043113Sgblack@eecs.umich.edutemplate<class OS>
5053113Sgblack@eecs.umich.edustatic void
5068852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5073113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5083113Sgblack@eecs.umich.edu{
5093113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5103113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5113113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5123113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5133113Sgblack@eecs.umich.edu}
5143113Sgblack@eecs.umich.edu
5153113Sgblack@eecs.umich.edutemplate<class OS>
5163113Sgblack@eecs.umich.edustatic void
5178852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5183113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5193113Sgblack@eecs.umich.edu{
5203113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5213113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5226686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5233113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5243113Sgblack@eecs.umich.edu}
5253113Sgblack@eecs.umich.edu
526378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
527378SN/A/// only to find out if their stdout is a tty, to determine whether to
5289141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5299141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
530360SN/Atemplate <class OS>
5311450SN/ASyscallReturn
5323114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5332680Sktlim@umich.edu          ThreadContext *tc)
534360SN/A{
5356701Sgblack@eecs.umich.edu    int index = 0;
5366701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5376701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
538360SN/A
5391969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
540360SN/A
54110496Ssteve.reinhardt@amd.com    Process::FdMap *fdObj = process->sim_fd_obj(fd);
54210496Ssteve.reinhardt@amd.com
54310496Ssteve.reinhardt@amd.com    if (fdObj == NULL) {
544360SN/A        // doesn't map to any simulator fd: not a valid target fd
5451458SN/A        return -EBADF;
546360SN/A    }
547360SN/A
54810496Ssteve.reinhardt@amd.com    if (fdObj->driver != NULL) {
54910496Ssteve.reinhardt@amd.com        return fdObj->driver->ioctl(process, tc, req);
55010496Ssteve.reinhardt@amd.com    }
55110496Ssteve.reinhardt@amd.com
5529141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5531458SN/A        return -ENOTTY;
5549141Smarc.orr@gmail.com    }
555360SN/A
5569141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5579141Smarc.orr@gmail.com         fd, req, tc->pcState());
5589141Smarc.orr@gmail.com    return -ENOTTY;
559360SN/A}
560360SN/A
561360SN/Atemplate <class OS>
56210027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5633114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
56410027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
565360SN/A{
566360SN/A    std::string path;
567360SN/A
5688852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5696701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5701458SN/A        return -EFAULT;
571360SN/A
5726701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5736701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
574360SN/A    int hostFlags = 0;
575360SN/A
576360SN/A    // translate open flags
577360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
578360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
579360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
580360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
581360SN/A        }
582360SN/A    }
583360SN/A
584360SN/A    // any target flags left?
585360SN/A    if (tgtFlags != 0)
5861706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
587360SN/A
588360SN/A#ifdef __CYGWIN32__
589360SN/A    hostFlags |= O_BINARY;
590360SN/A#endif
591360SN/A
5923669Sbinkertn@umich.edu    // Adjust path for current working directory
5933669Sbinkertn@umich.edu    path = process->fullPath(path);
5943669Sbinkertn@umich.edu
5951706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
5961706SN/A
59710496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
59810496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
59910496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
60010496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
60110496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
60210496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
60310496Ssteve.reinhardt@amd.com            return -ENOENT;
60410496Ssteve.reinhardt@amd.com        }
60510496Ssteve.reinhardt@amd.com
60610496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
60710496Ssteve.reinhardt@amd.com        if (drv != NULL) {
60810496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
60910496Ssteve.reinhardt@amd.com            // process if necessary.
61010496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
61110496Ssteve.reinhardt@amd.com        }
61210496Ssteve.reinhardt@amd.com
61310496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
61410496Ssteve.reinhardt@amd.com        // /dev/zero
61510496Ssteve.reinhardt@amd.com    }
61610496Ssteve.reinhardt@amd.com
6175795Ssaidi@eecs.umich.edu    int fd;
6189143Ssteve.reinhardt@amd.com    int local_errno;
6199142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6209142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6219143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6225795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6239143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6245795Ssaidi@eecs.umich.edu     } else {
6255795Ssaidi@eecs.umich.edu        // open the file
6265795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6279143Ssteve.reinhardt@amd.com        local_errno = errno;
6285795Ssaidi@eecs.umich.edu     }
629360SN/A
6309143Ssteve.reinhardt@amd.com    if (fd == -1)
6319143Ssteve.reinhardt@amd.com        return -local_errno;
6329143Ssteve.reinhardt@amd.com
6339143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
634360SN/A}
635360SN/A
63610027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
63710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
63810027SChris.Adeniyi-Jones@arm.comSyscallReturn
63910027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
64010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
64110027SChris.Adeniyi-Jones@arm.com{
64210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
64310027SChris.Adeniyi-Jones@arm.com}
64410027SChris.Adeniyi-Jones@arm.com
64510027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
64610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
64710027SChris.Adeniyi-Jones@arm.comSyscallReturn
64810027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
64910027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
65010027SChris.Adeniyi-Jones@arm.com{
65110027SChris.Adeniyi-Jones@arm.com    int index = 0;
65210027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
65310027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
65410027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
65510027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
65610027SChris.Adeniyi-Jones@arm.com}
65710027SChris.Adeniyi-Jones@arm.com
65810203SAli.Saidi@ARM.com/// Target facessat() handler
65910203SAli.Saidi@ARM.comtemplate <class OS>
66010203SAli.Saidi@ARM.comSyscallReturn
66110203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66210203SAli.Saidi@ARM.com        ThreadContext *tc)
66310203SAli.Saidi@ARM.com{
66410203SAli.Saidi@ARM.com    int index = 0;
66510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
66610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
66710203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
66810203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
66910203SAli.Saidi@ARM.com}
67010203SAli.Saidi@ARM.com
67110203SAli.Saidi@ARM.com/// Target readlinkat() handler
67210203SAli.Saidi@ARM.comtemplate <class OS>
67310203SAli.Saidi@ARM.comSyscallReturn
67410203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67510203SAli.Saidi@ARM.com        ThreadContext *tc)
67610203SAli.Saidi@ARM.com{
67710203SAli.Saidi@ARM.com    int index = 0;
67810203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
67910203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
68010203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
68110203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
68210203SAli.Saidi@ARM.com}
68310203SAli.Saidi@ARM.com
6846640Svince@csl.cornell.edu/// Target sysinfo() handler.
6856640Svince@csl.cornell.edutemplate <class OS>
6866640Svince@csl.cornell.eduSyscallReturn
6876640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
6886640Svince@csl.cornell.edu         ThreadContext *tc)
6896640Svince@csl.cornell.edu{
6906640Svince@csl.cornell.edu
6916701Sgblack@eecs.umich.edu    int index = 0;
6926701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
6936701Sgblack@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
6946640Svince@csl.cornell.edu
6956701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
6966701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
6976640Svince@csl.cornell.edu
6988706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
6996640Svince@csl.cornell.edu
7006701Sgblack@eecs.umich.edu    return 0;
7016640Svince@csl.cornell.edu}
702360SN/A
7031999SN/A/// Target chmod() handler.
7041999SN/Atemplate <class OS>
7051999SN/ASyscallReturn
7063114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7072680Sktlim@umich.edu          ThreadContext *tc)
7081999SN/A{
7091999SN/A    std::string path;
7101999SN/A
7116701Sgblack@eecs.umich.edu    int index = 0;
7128852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7136701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7141999SN/A        return -EFAULT;
7156701Sgblack@eecs.umich.edu    }
7161999SN/A
7176701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7181999SN/A    mode_t hostMode = 0;
7191999SN/A
7201999SN/A    // XXX translate mode flags via OS::something???
7211999SN/A    hostMode = mode;
7221999SN/A
7233669Sbinkertn@umich.edu    // Adjust path for current working directory
7243669Sbinkertn@umich.edu    path = process->fullPath(path);
7253669Sbinkertn@umich.edu
7261999SN/A    // do the chmod
7271999SN/A    int result = chmod(path.c_str(), hostMode);
7281999SN/A    if (result < 0)
7292218SN/A        return -errno;
7301999SN/A
7311999SN/A    return 0;
7321999SN/A}
7331999SN/A
7341999SN/A
7351999SN/A/// Target fchmod() handler.
7361999SN/Atemplate <class OS>
7371999SN/ASyscallReturn
7383114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7392680Sktlim@umich.edu           ThreadContext *tc)
7401999SN/A{
7416701Sgblack@eecs.umich.edu    int index = 0;
7426701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7431999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7441999SN/A        // doesn't map to any simulator fd: not a valid target fd
7451999SN/A        return -EBADF;
7461999SN/A    }
7471999SN/A
7486701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7491999SN/A    mode_t hostMode = 0;
7501999SN/A
7511999SN/A    // XXX translate mode flags via OS::someting???
7521999SN/A    hostMode = mode;
7531999SN/A
7541999SN/A    // do the fchmod
7551999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7561999SN/A    if (result < 0)
7572218SN/A        return -errno;
7581999SN/A
7591999SN/A    return 0;
7601999SN/A}
7611999SN/A
7625877Shsul@eecs.umich.edu/// Target mremap() handler.
7635877Shsul@eecs.umich.edutemplate <class OS>
7645877Shsul@eecs.umich.eduSyscallReturn
7655877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7665877Shsul@eecs.umich.edu{
7676701Sgblack@eecs.umich.edu    int index = 0;
7686701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
7696701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
7706701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
7716701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
77210027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
77310027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
77410027SChris.Adeniyi-Jones@arm.com
77510027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
77610027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
7775877Shsul@eecs.umich.edu
77810318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
77910318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
7805877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
7815877Shsul@eecs.umich.edu        return -EINVAL;
7825877Shsul@eecs.umich.edu    }
7835877Shsul@eecs.umich.edu
78410486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
78510486Stjablin@gmail.com
7865877Shsul@eecs.umich.edu    if (new_length > old_length) {
78710027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
78810027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
7895877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
7908601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
7915877Shsul@eecs.umich.edu            process->mmap_end += diff;
7925877Shsul@eecs.umich.edu            return start;
7935877Shsul@eecs.umich.edu        } else {
79410027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
7955877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
7965877Shsul@eecs.umich.edu                return -ENOMEM;
7975877Shsul@eecs.umich.edu            } else {
79810027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
79910027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
80010027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
80110027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
80210027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
80310027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8045877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
80510027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
80610027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
80710027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
80810027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
80910027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
81010027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
81110027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
81210027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
81310027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
81410027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
81510027SChris.Adeniyi-Jones@arm.com                }
81610027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
81710027SChris.Adeniyi-Jones@arm.com                return new_start;
8185877Shsul@eecs.umich.edu            }
8195877Shsul@eecs.umich.edu        }
8205877Shsul@eecs.umich.edu    } else {
82110027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
82210027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8238601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
82410027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8255877Shsul@eecs.umich.edu    }
8265877Shsul@eecs.umich.edu}
8271999SN/A
828378SN/A/// Target stat() handler.
829360SN/Atemplate <class OS>
8301450SN/ASyscallReturn
8313114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8322680Sktlim@umich.edu         ThreadContext *tc)
833360SN/A{
834360SN/A    std::string path;
835360SN/A
8366701Sgblack@eecs.umich.edu    int index = 0;
8378852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8386701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8396701Sgblack@eecs.umich.edu        return -EFAULT;
8406701Sgblack@eecs.umich.edu    }
8416701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
842360SN/A
8433669Sbinkertn@umich.edu    // Adjust path for current working directory
8443669Sbinkertn@umich.edu    path = process->fullPath(path);
8453669Sbinkertn@umich.edu
846360SN/A    struct stat hostBuf;
847360SN/A    int result = stat(path.c_str(), &hostBuf);
848360SN/A
849360SN/A    if (result < 0)
8502218SN/A        return -errno;
851360SN/A
8528706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
853360SN/A
8541458SN/A    return 0;
855360SN/A}
856360SN/A
857360SN/A
8585074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8595074Ssaidi@eecs.umich.edutemplate <class OS>
8605074Ssaidi@eecs.umich.eduSyscallReturn
8615074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8625074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8635074Ssaidi@eecs.umich.edu{
8645074Ssaidi@eecs.umich.edu    std::string path;
8655074Ssaidi@eecs.umich.edu
8666701Sgblack@eecs.umich.edu    int index = 0;
8678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8686701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
8695074Ssaidi@eecs.umich.edu        return -EFAULT;
8706701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
8715074Ssaidi@eecs.umich.edu
8725074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
8735074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
8745074Ssaidi@eecs.umich.edu
8755208Ssaidi@eecs.umich.edu#if NO_STAT64
8765208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
8775208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
8785208Ssaidi@eecs.umich.edu#else
8795074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
8805074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
8815208Ssaidi@eecs.umich.edu#endif
8825074Ssaidi@eecs.umich.edu
8835074Ssaidi@eecs.umich.edu    if (result < 0)
8845074Ssaidi@eecs.umich.edu        return -errno;
8855074Ssaidi@eecs.umich.edu
8868706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
8875074Ssaidi@eecs.umich.edu
8885074Ssaidi@eecs.umich.edu    return 0;
8895074Ssaidi@eecs.umich.edu}
8905074Ssaidi@eecs.umich.edu
8915074Ssaidi@eecs.umich.edu
89210027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
89310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
89410027SChris.Adeniyi-Jones@arm.comSyscallReturn
89510027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
89610027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
89710027SChris.Adeniyi-Jones@arm.com{
89810027SChris.Adeniyi-Jones@arm.com    int index = 0;
89910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
90010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
90110027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
90210027SChris.Adeniyi-Jones@arm.com
90310027SChris.Adeniyi-Jones@arm.com    std::string path;
90410027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
90510027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
90610027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
90710027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
90810027SChris.Adeniyi-Jones@arm.com
90910027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
91010027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
91110027SChris.Adeniyi-Jones@arm.com
91210027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
91310027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
91410027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
91510027SChris.Adeniyi-Jones@arm.com#else
91610027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
91710027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
91810027SChris.Adeniyi-Jones@arm.com#endif
91910027SChris.Adeniyi-Jones@arm.com
92010027SChris.Adeniyi-Jones@arm.com    if (result < 0)
92110027SChris.Adeniyi-Jones@arm.com        return -errno;
92210027SChris.Adeniyi-Jones@arm.com
92310027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
92410027SChris.Adeniyi-Jones@arm.com
92510027SChris.Adeniyi-Jones@arm.com    return 0;
92610027SChris.Adeniyi-Jones@arm.com}
92710027SChris.Adeniyi-Jones@arm.com
92810027SChris.Adeniyi-Jones@arm.com
9291999SN/A/// Target fstat64() handler.
9301999SN/Atemplate <class OS>
9311999SN/ASyscallReturn
9323114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9332680Sktlim@umich.edu            ThreadContext *tc)
9341999SN/A{
9356701Sgblack@eecs.umich.edu    int index = 0;
9366701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9376701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9381999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9391999SN/A        // doesn't map to any simulator fd: not a valid target fd
9401999SN/A        return -EBADF;
9411999SN/A    }
9421999SN/A
9432764Sstever@eecs.umich.edu#if NO_STAT64
9442064SN/A    struct stat  hostBuf;
9452064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
9462064SN/A#else
9472064SN/A    struct stat64  hostBuf;
9481999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
9492064SN/A#endif
9501999SN/A
9511999SN/A    if (result < 0)
9522218SN/A        return -errno;
9531999SN/A
9548706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9551999SN/A
9561999SN/A    return 0;
9571999SN/A}
9581999SN/A
9591999SN/A
960378SN/A/// Target lstat() handler.
961360SN/Atemplate <class OS>
9621450SN/ASyscallReturn
9633114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9642680Sktlim@umich.edu          ThreadContext *tc)
965360SN/A{
966360SN/A    std::string path;
967360SN/A
9686701Sgblack@eecs.umich.edu    int index = 0;
9698852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9706701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9716701Sgblack@eecs.umich.edu        return -EFAULT;
9726701Sgblack@eecs.umich.edu    }
9736701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
974360SN/A
9753669Sbinkertn@umich.edu    // Adjust path for current working directory
9763669Sbinkertn@umich.edu    path = process->fullPath(path);
9773669Sbinkertn@umich.edu
978360SN/A    struct stat hostBuf;
979360SN/A    int result = lstat(path.c_str(), &hostBuf);
980360SN/A
981360SN/A    if (result < 0)
9821458SN/A        return -errno;
983360SN/A
9848706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
985360SN/A
9861458SN/A    return 0;
987360SN/A}
988360SN/A
9891999SN/A/// Target lstat64() handler.
9901999SN/Atemplate <class OS>
9911999SN/ASyscallReturn
9923114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9932680Sktlim@umich.edu            ThreadContext *tc)
9941999SN/A{
9951999SN/A    std::string path;
9961999SN/A
9976701Sgblack@eecs.umich.edu    int index = 0;
9988852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9996701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10006701Sgblack@eecs.umich.edu        return -EFAULT;
10016701Sgblack@eecs.umich.edu    }
10026701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10031999SN/A
10043669Sbinkertn@umich.edu    // Adjust path for current working directory
10053669Sbinkertn@umich.edu    path = process->fullPath(path);
10063669Sbinkertn@umich.edu
10072764Sstever@eecs.umich.edu#if NO_STAT64
10082064SN/A    struct stat hostBuf;
10092064SN/A    int result = lstat(path.c_str(), &hostBuf);
10102064SN/A#else
10111999SN/A    struct stat64 hostBuf;
10121999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10132064SN/A#endif
10141999SN/A
10151999SN/A    if (result < 0)
10161999SN/A        return -errno;
10171999SN/A
10188706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10191999SN/A
10201999SN/A    return 0;
10211999SN/A}
10221999SN/A
1023378SN/A/// Target fstat() handler.
1024360SN/Atemplate <class OS>
10251450SN/ASyscallReturn
10263114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10272680Sktlim@umich.edu          ThreadContext *tc)
1028360SN/A{
10296701Sgblack@eecs.umich.edu    int index = 0;
10306701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10316701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1032360SN/A
10331969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1034360SN/A
1035360SN/A    if (fd < 0)
10361458SN/A        return -EBADF;
1037360SN/A
1038360SN/A    struct stat hostBuf;
1039360SN/A    int result = fstat(fd, &hostBuf);
1040360SN/A
1041360SN/A    if (result < 0)
10421458SN/A        return -errno;
1043360SN/A
10448706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10452021SN/A
10461458SN/A    return 0;
1047360SN/A}
1048360SN/A
1049360SN/A
10501706SN/A/// Target statfs() handler.
10511706SN/Atemplate <class OS>
10521706SN/ASyscallReturn
10533114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10542680Sktlim@umich.edu           ThreadContext *tc)
10551706SN/A{
10561706SN/A    std::string path;
10571706SN/A
10586701Sgblack@eecs.umich.edu    int index = 0;
10598852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10606701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10616701Sgblack@eecs.umich.edu        return -EFAULT;
10626701Sgblack@eecs.umich.edu    }
10636701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10641706SN/A
10653669Sbinkertn@umich.edu    // Adjust path for current working directory
10663669Sbinkertn@umich.edu    path = process->fullPath(path);
10673669Sbinkertn@umich.edu
10681706SN/A    struct statfs hostBuf;
10691706SN/A    int result = statfs(path.c_str(), &hostBuf);
10701706SN/A
10711706SN/A    if (result < 0)
10722218SN/A        return -errno;
10731706SN/A
10748706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
10751706SN/A
10761706SN/A    return 0;
10771706SN/A}
10781706SN/A
10791706SN/A
10801706SN/A/// Target fstatfs() handler.
10811706SN/Atemplate <class OS>
10821706SN/ASyscallReturn
10833114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10842680Sktlim@umich.edu            ThreadContext *tc)
10851706SN/A{
10866701Sgblack@eecs.umich.edu    int index = 0;
10876701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10886701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10891706SN/A
10901706SN/A    if (fd < 0)
10911706SN/A        return -EBADF;
10921706SN/A
10931706SN/A    struct statfs hostBuf;
10941706SN/A    int result = fstatfs(fd, &hostBuf);
10951706SN/A
10961706SN/A    if (result < 0)
10972218SN/A        return -errno;
10981706SN/A
10998706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11001706SN/A
11011706SN/A    return 0;
11021706SN/A}
11031706SN/A
11041706SN/A
11051999SN/A/// Target writev() handler.
11061999SN/Atemplate <class OS>
11071999SN/ASyscallReturn
11083114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11092680Sktlim@umich.edu           ThreadContext *tc)
11101999SN/A{
11116701Sgblack@eecs.umich.edu    int index = 0;
11126701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11131999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11141999SN/A        // doesn't map to any simulator fd: not a valid target fd
11151999SN/A        return -EBADF;
11161999SN/A    }
11171999SN/A
11188852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11196701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11206701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11211999SN/A    struct iovec hiov[count];
11226227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11231999SN/A        typename OS::tgt_iovec tiov;
11242461SN/A
11258852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11268852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11278737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11281999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11298852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11308852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11311999SN/A    }
11321999SN/A
11331999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11341999SN/A
11356227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11361999SN/A        delete [] (char *)hiov[i].iov_base;
11371999SN/A
11381999SN/A    if (result < 0)
11392218SN/A        return -errno;
11401999SN/A
11411999SN/A    return 0;
11421999SN/A}
11431999SN/A
11441999SN/A
1145378SN/A/// Target mmap() handler.
1146378SN/A///
1147378SN/A/// We don't really handle mmap().  If the target is mmaping an
1148378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1149378SN/A/// nothing (since memory is initialized to zero and the simulator
11508324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
11518324Ssteve.reinhardt@amd.com///
1152360SN/Atemplate <class OS>
11531450SN/ASyscallReturn
11543114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1155360SN/A{
11566701Sgblack@eecs.umich.edu    int index = 0;
11576701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
11586701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
11596701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
11606701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
11618324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
116210486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1163360SN/A
11649008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
11659008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
11669008Sgblack@eecs.umich.edu
11678324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
11688324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
11698324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
11708324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
11718324Ssteve.reinhardt@amd.com            return -EBADF;
11728324Ssteve.reinhardt@amd.com        }
11738324Ssteve.reinhardt@amd.com
11748324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
11758324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
11768324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
11778324Ssteve.reinhardt@amd.com            // another name on some platform
11788324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
11798324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
11808324Ssteve.reinhardt@amd.com        }
11818324Ssteve.reinhardt@amd.com    }
11825877Shsul@eecs.umich.edu
118310486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
118410486Stjablin@gmail.com
118510318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
118610486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
11872544SN/A        warn("mmap failing: arguments not page-aligned: "
118810486Stjablin@gmail.com             "start 0x%x offset 0x%x",
118910486Stjablin@gmail.com             start, offset);
11902544SN/A        return -EINVAL;
1191360SN/A    }
1192360SN/A
11938600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
11948600Ssteve.reinhardt@amd.com    // true if the user has been warned.
11958600Ssteve.reinhardt@amd.com    bool clobber = false;
11968600Ssteve.reinhardt@amd.com
11978600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
11988600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
11998600Ssteve.reinhardt@amd.com
12008600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12018600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12028600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12038600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12048600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12058600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12068600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
120710485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
120810485SMichael.Adler@intel.com                return -EINVAL;
12098600Ssteve.reinhardt@amd.com            } else {
12108600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12118600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12128600Ssteve.reinhardt@amd.com                use_provided_address = false;
12138600Ssteve.reinhardt@amd.com            }
12148600Ssteve.reinhardt@amd.com        }
12152544SN/A    }
12162544SN/A
12178600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12188600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12198600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12208600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12218600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12228600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12238600Ssteve.reinhardt@amd.com        } else {
12248600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12258600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12268600Ssteve.reinhardt@amd.com        }
12276672Sgblack@eecs.umich.edu    }
12288600Ssteve.reinhardt@amd.com
12298601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12302544SN/A
12311458SN/A    return start;
1232360SN/A}
1233360SN/A
1234378SN/A/// Target getrlimit() handler.
1235360SN/Atemplate <class OS>
12361450SN/ASyscallReturn
12373114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12382680Sktlim@umich.edu        ThreadContext *tc)
1239360SN/A{
12406701Sgblack@eecs.umich.edu    int index = 0;
12416701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12426701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1243360SN/A
1244360SN/A    switch (resource) {
12452064SN/A        case OS::TGT_RLIMIT_STACK:
12465877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
12472064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
12488737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12498737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12502064SN/A            break;
1251360SN/A
12525877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
12535877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
12545877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
12558737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12568737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12575877Shsul@eecs.umich.edu            break;
12585877Shsul@eecs.umich.edu
12592064SN/A        default:
12602064SN/A            std::cerr << "getrlimitFunc: unimplemented resource " << resource
12612064SN/A                << std::endl;
12622064SN/A            abort();
12632064SN/A            break;
1264360SN/A    }
1265360SN/A
12668706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
12671458SN/A    return 0;
1268360SN/A}
1269360SN/A
1270378SN/A/// Target gettimeofday() handler.
1271360SN/Atemplate <class OS>
12721450SN/ASyscallReturn
12733114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12742680Sktlim@umich.edu        ThreadContext *tc)
1275360SN/A{
12766701Sgblack@eecs.umich.edu    int index = 0;
12776701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1278360SN/A
1279360SN/A    getElapsedTime(tp->tv_sec, tp->tv_usec);
1280360SN/A    tp->tv_sec += seconds_since_epoch;
12816109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
12826109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1283360SN/A
12848706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1285360SN/A
12861458SN/A    return 0;
1287360SN/A}
1288360SN/A
1289360SN/A
12901999SN/A/// Target utimes() handler.
12911999SN/Atemplate <class OS>
12921999SN/ASyscallReturn
12933114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12942680Sktlim@umich.edu           ThreadContext *tc)
12951999SN/A{
12961999SN/A    std::string path;
12971999SN/A
12986701Sgblack@eecs.umich.edu    int index = 0;
12998852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13006701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13016701Sgblack@eecs.umich.edu        return -EFAULT;
13026701Sgblack@eecs.umich.edu    }
13031999SN/A
13046701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13056701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13068706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13071999SN/A
13081999SN/A    struct timeval hostTimeval[2];
13091999SN/A    for (int i = 0; i < 2; ++i)
13101999SN/A    {
13118737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13128737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13131999SN/A    }
13143669Sbinkertn@umich.edu
13153669Sbinkertn@umich.edu    // Adjust path for current working directory
13163669Sbinkertn@umich.edu    path = process->fullPath(path);
13173669Sbinkertn@umich.edu
13181999SN/A    int result = utimes(path.c_str(), hostTimeval);
13191999SN/A
13201999SN/A    if (result < 0)
13211999SN/A        return -errno;
13221999SN/A
13231999SN/A    return 0;
13241999SN/A}
1325378SN/A/// Target getrusage() function.
1326360SN/Atemplate <class OS>
13271450SN/ASyscallReturn
13283114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13292680Sktlim@umich.edu              ThreadContext *tc)
1330360SN/A{
13316701Sgblack@eecs.umich.edu    int index = 0;
13326701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13336701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1334360SN/A
13353670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13363670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1337360SN/A    rup->ru_stime.tv_sec = 0;
1338360SN/A    rup->ru_stime.tv_usec = 0;
1339360SN/A    rup->ru_maxrss = 0;
1340360SN/A    rup->ru_ixrss = 0;
1341360SN/A    rup->ru_idrss = 0;
1342360SN/A    rup->ru_isrss = 0;
1343360SN/A    rup->ru_minflt = 0;
1344360SN/A    rup->ru_majflt = 0;
1345360SN/A    rup->ru_nswap = 0;
1346360SN/A    rup->ru_inblock = 0;
1347360SN/A    rup->ru_oublock = 0;
1348360SN/A    rup->ru_msgsnd = 0;
1349360SN/A    rup->ru_msgrcv = 0;
1350360SN/A    rup->ru_nsignals = 0;
1351360SN/A    rup->ru_nvcsw = 0;
1352360SN/A    rup->ru_nivcsw = 0;
1353360SN/A
13543670Sbinkertn@umich.edu    switch (who) {
13553670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
13563670Sbinkertn@umich.edu        getElapsedTime(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
13578737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
13588737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
13593670Sbinkertn@umich.edu        break;
13603670Sbinkertn@umich.edu
13613670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
13623670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
13633670Sbinkertn@umich.edu        break;
13643670Sbinkertn@umich.edu
13653670Sbinkertn@umich.edu      default:
13663670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
13673670Sbinkertn@umich.edu        // plow ahead
13683670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
13693670Sbinkertn@umich.edu             who);
13703670Sbinkertn@umich.edu    }
13713670Sbinkertn@umich.edu
13728706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1373360SN/A
13741458SN/A    return 0;
1375360SN/A}
1376360SN/A
13776683Stjones1@inf.ed.ac.uk/// Target times() function.
13786683Stjones1@inf.ed.ac.uktemplate <class OS>
13796683Stjones1@inf.ed.ac.ukSyscallReturn
13806683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13816683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
13826683Stjones1@inf.ed.ac.uk{
13836701Sgblack@eecs.umich.edu    int index = 0;
13846701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
13856683Stjones1@inf.ed.ac.uk
13866683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
13877823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
13886683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
13896683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
13906683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
13916683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
13926683Stjones1@inf.ed.ac.uk
13936683Stjones1@inf.ed.ac.uk    // Convert to host endianness
13948737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
13956683Stjones1@inf.ed.ac.uk
13966683Stjones1@inf.ed.ac.uk    // Write back
13978706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
13986683Stjones1@inf.ed.ac.uk
13996683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14006683Stjones1@inf.ed.ac.uk    return clocks;
14016683Stjones1@inf.ed.ac.uk}
14022553SN/A
14036684Stjones1@inf.ed.ac.uk/// Target time() function.
14046684Stjones1@inf.ed.ac.uktemplate <class OS>
14056684Stjones1@inf.ed.ac.ukSyscallReturn
14066684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14076684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14086684Stjones1@inf.ed.ac.uk{
14096684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
14106684Stjones1@inf.ed.ac.uk    getElapsedTime(sec, usec);
14116684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14126684Stjones1@inf.ed.ac.uk
14136701Sgblack@eecs.umich.edu    int index = 0;
14146701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14156684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14166684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14178737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14188852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14198852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14206684Stjones1@inf.ed.ac.uk    }
14216684Stjones1@inf.ed.ac.uk    return sec;
14226684Stjones1@inf.ed.ac.uk}
14232553SN/A
14242553SN/A
14251354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1426