syscall_emul.hh revision 10831
1360SN/A/*
210027SChris.Adeniyi-Jones@arm.com * Copyright (c) 2012-2013 ARM Limited
310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc.
410027SChris.Adeniyi-Jones@arm.com * All rights reserved
510027SChris.Adeniyi-Jones@arm.com *
610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1410027SChris.Adeniyi-Jones@arm.com *
151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
16360SN/A * All rights reserved.
17360SN/A *
18360SN/A * Redistribution and use in source and binary forms, with or without
19360SN/A * modification, are permitted provided that the following conditions are
20360SN/A * met: redistributions of source code must retain the above copyright
21360SN/A * notice, this list of conditions and the following disclaimer;
22360SN/A * redistributions in binary form must reproduce the above copyright
23360SN/A * notice, this list of conditions and the following disclaimer in the
24360SN/A * documentation and/or other materials provided with the distribution;
25360SN/A * neither the name of the copyright holders nor the names of its
26360SN/A * contributors may be used to endorse or promote products derived from
27360SN/A * this software without specific prior written permission.
28360SN/A *
29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
422665Ssaidi@eecs.umich.edu *          Kevin Lim
43360SN/A */
44360SN/A
451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
461354SN/A#define __SIM_SYSCALL_EMUL_HH__
47360SN/A
482764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
499202Spalle@lyckegaard.dk  defined(__FreeBSD__) || defined(__CYGWIN__) || \
509202Spalle@lyckegaard.dk  defined(__NetBSD__))
512064SN/A
52360SN/A///
53360SN/A/// @file syscall_emul.hh
54360SN/A///
55360SN/A/// This file defines objects used to emulate syscalls from the target
56360SN/A/// application on the host machine.
57360SN/A
581809SN/A#ifdef __CYGWIN32__
595543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
601809SN/A#endif
613113Sgblack@eecs.umich.edu#include <sys/stat.h>
628229Snate@binkert.org#include <sys/time.h>
638229Snate@binkert.org#include <sys/uio.h>
643113Sgblack@eecs.umich.edu#include <fcntl.h>
657075Snate@binkert.org
668229Snate@binkert.org#include <cerrno>
677075Snate@binkert.org#include <string>
68360SN/A
692474SN/A#include "base/chunk_generator.hh"
705543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
712462SN/A#include "base/misc.hh"
721354SN/A#include "base/trace.hh"
736216Snate@binkert.org#include "base/types.hh"
746658Snate@binkert.org#include "config/the_isa.hh"
752474SN/A#include "cpu/base.hh"
762680Sktlim@umich.edu#include "cpu/thread_context.hh"
778232Snate@binkert.org#include "debug/SyscallVerbose.hh"
788229Snate@binkert.org#include "mem/page_table.hh"
797678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
8010496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
818229Snate@binkert.org#include "sim/process.hh"
8210497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
838766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
846640Svince@csl.cornell.edu#include "sim/system.hh"
85360SN/A
86360SN/A///
87360SN/A/// System call descriptor.
88360SN/A///
89360SN/Aclass SyscallDesc {
90360SN/A
91360SN/A  public:
92360SN/A
93378SN/A    /// Typedef for target syscall handler functions.
941450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
953114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
96360SN/A
975543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
985543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
995543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
10010831Ssteve.reinhardt@amd.com    bool warned;        //!< Have we warned about unimplemented syscall?
101360SN/A
102360SN/A    /// Flag values for controlling syscall behavior.
103360SN/A    enum Flags {
104360SN/A        /// Don't set return regs according to funcPtr return value.
105360SN/A        /// Used for syscalls with non-standard return conventions
1062680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
107360SN/A        /// sigreturn).
10810831Ssteve.reinhardt@amd.com        SuppressReturnValue = 1,
10910831Ssteve.reinhardt@amd.com        WarnOnce = 2
110360SN/A    };
111360SN/A
112360SN/A    /// Constructor.
113360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
11410831Ssteve.reinhardt@amd.com        : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
115360SN/A    {
116360SN/A    }
117360SN/A
118360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1193114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
12010831Ssteve.reinhardt@amd.com
12110831Ssteve.reinhardt@amd.com    /// Is the WarnOnce flag set?
12210831Ssteve.reinhardt@amd.com    bool warnOnce() const {  return (flags & WarnOnce); }
123360SN/A};
124360SN/A
125360SN/A
126360SN/A//////////////////////////////////////////////////////////////////////
127360SN/A//
128360SN/A// The following emulation functions are generic enough that they
129360SN/A// don't need to be recompiled for different emulated OS's.  They are
130360SN/A// defined in sim/syscall_emul.cc.
131360SN/A//
132360SN/A//////////////////////////////////////////////////////////////////////
133360SN/A
134360SN/A
135378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1361706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1373114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
138378SN/A
139378SN/A/// Handler for unimplemented syscalls that we never intend to
140378SN/A/// implement (signal handling, etc.) and should not affect the correct
141378SN/A/// behavior of the program.  Print a warning only if the appropriate
142378SN/A/// trace flag is enabled.  Return success to the target program.
1431706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1443114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
145360SN/A
1466109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1471706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1483114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
149378SN/A
1506109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1516109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1526109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1536109Ssanchezd@stanford.edu
154378SN/A/// Target getpagesize() handler.
1551706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1563114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
157378SN/A
1585748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1595748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1605748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
161378SN/A
162378SN/A/// Target close() handler.
1631706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1643114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
165378SN/A
166378SN/A/// Target read() handler.
1671706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1683114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
169378SN/A
170378SN/A/// Target write() handler.
1711706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1723114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
173378SN/A
174378SN/A/// Target lseek() handler.
1751706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1763114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
177378SN/A
1784118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1794118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1804118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1814118Sgblack@eecs.umich.edu
182378SN/A/// Target munmap() handler.
1831706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
185378SN/A
186378SN/A/// Target gethostname() handler.
1871706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1883114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
189360SN/A
1905513SMichael.Adler@intel.com/// Target getcwd() handler.
1915513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
1925513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
1935513SMichael.Adler@intel.com
19410203SAli.Saidi@ARM.com/// Target readlink() handler.
19510203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19610203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
19710203SAli.Saidi@ARM.com                           int index = 0);
1985513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
1995513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2005513SMichael.Adler@intel.com
201511SN/A/// Target unlink() handler.
20210633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20310633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
20410633Smichaelupton@gmail.com                           int index);
2051706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2063114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
207511SN/A
2085513SMichael.Adler@intel.com/// Target mkdir() handler.
2095513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2105513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2115513SMichael.Adler@intel.com
212511SN/A/// Target rename() handler.
2131706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2143114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2151706SN/A
2161706SN/A
2171706SN/A/// Target truncate() handler.
2181706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2193114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2201706SN/A
2211706SN/A
2221706SN/A/// Target ftruncate() handler.
2231706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2251706SN/A
226511SN/A
2276703Svince@csl.cornell.edu/// Target truncate64() handler.
2286703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2296703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2306703Svince@csl.cornell.edu
2316685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2326685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2336685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2346685Stjones1@inf.ed.ac.uk
2356685Stjones1@inf.ed.ac.uk
2365513SMichael.Adler@intel.com/// Target umask() handler.
2375513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2385513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2395513SMichael.Adler@intel.com
2405513SMichael.Adler@intel.com
2411999SN/A/// Target chown() handler.
2421999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2433114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2441999SN/A
2451999SN/A
2461999SN/A/// Target fchown() handler.
2471999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2483114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2491999SN/A
2503079Sstever@eecs.umich.edu/// Target dup() handler.
2513079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2523114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2533079Sstever@eecs.umich.edu
2542093SN/A/// Target fnctl() handler.
2552093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2563114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2572093SN/A
2582687Sksewell@umich.edu/// Target fcntl64() handler.
2592687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2603114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2612687Sksewell@umich.edu
2622238SN/A/// Target setuid() handler.
2632238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2643114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2652238SN/A
2662238SN/A/// Target getpid() handler.
2672238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2692238SN/A
2702238SN/A/// Target getuid() handler.
2712238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2723114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2732238SN/A
2742238SN/A/// Target getgid() handler.
2752238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2763114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2772238SN/A
2782238SN/A/// Target getppid() handler.
2792238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2812238SN/A
2822238SN/A/// Target geteuid() handler.
2832238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2852238SN/A
2862238SN/A/// Target getegid() handler.
2872238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2883114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2892238SN/A
2906109Ssanchezd@stanford.edu/// Target clone() handler.
2916109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
2926109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
2932238SN/A
2949455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2959455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2969455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
29710203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
29810203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
29910203SAli.Saidi@ARM.com                               int index);
3009455Smitch.hayenga+gem5@gmail.com
3019112Smarc.orr@gmail.com/// Futex system call
3029112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3039112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3049112Smarc.orr@gmail.comtemplate <class OS>
3059112Smarc.orr@gmail.comSyscallReturn
3069112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3079112Smarc.orr@gmail.com          ThreadContext *tc)
3089112Smarc.orr@gmail.com{
3099112Smarc.orr@gmail.com    int index_uaddr = 0;
3109112Smarc.orr@gmail.com    int index_op = 1;
3119112Smarc.orr@gmail.com    int index_val = 2;
3129112Smarc.orr@gmail.com    int index_timeout = 3;
3139112Smarc.orr@gmail.com
3149112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3159112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3169112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3179112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3189112Smarc.orr@gmail.com
3199112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3209112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3219112Smarc.orr@gmail.com
3229112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3239112Smarc.orr@gmail.com            uaddr, op, val);
3249112Smarc.orr@gmail.com
3259238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3269112Smarc.orr@gmail.com
3279112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3289112Smarc.orr@gmail.com        if (timeout != 0) {
3299112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3309112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3319112Smarc.orr@gmail.com        }
3329112Smarc.orr@gmail.com
3339112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3349112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3359112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3369112Smarc.orr@gmail.com        delete buf;
3379112Smarc.orr@gmail.com
3389112Smarc.orr@gmail.com        if(val != mem_val) {
3399112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3409112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3419112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3429112Smarc.orr@gmail.com        }
3439112Smarc.orr@gmail.com
3449112Smarc.orr@gmail.com        // Queue the thread context
3459112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3469112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3479112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3489112Smarc.orr@gmail.com        } else {
3499112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3509112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3519112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3529112Smarc.orr@gmail.com        }
3539112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3549112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3559112Smarc.orr@gmail.com                                "thread context\n");
3569112Smarc.orr@gmail.com        tc->suspend();
3579112Smarc.orr@gmail.com        return 0;
3589112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3599112Smarc.orr@gmail.com        int wokenUp = 0;
3609112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3619112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3629112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3639112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3649112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3659112Smarc.orr@gmail.com                tcWaitList->pop_front();
3669112Smarc.orr@gmail.com                wokenUp++;
3679112Smarc.orr@gmail.com            }
3689112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
3699112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3709112Smarc.orr@gmail.com                delete tcWaitList;
3719112Smarc.orr@gmail.com            }
3729112Smarc.orr@gmail.com        }
3739112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3749112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3759112Smarc.orr@gmail.com        return wokenUp;
3769112Smarc.orr@gmail.com    } else {
3779238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3789112Smarc.orr@gmail.com        return 0;
3799112Smarc.orr@gmail.com    }
3809112Smarc.orr@gmail.com
3819112Smarc.orr@gmail.com}
3829112Smarc.orr@gmail.com
3832238SN/A
3842238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3852238SN/A/// returning a second value in a register other than the normal return register
3862238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3873114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3882238SN/A
3892238SN/A/// Target getpidPseudo() handler.
3902238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3913114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3922238SN/A
3932238SN/A/// Target getuidPseudo() handler.
3942238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3953114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3962238SN/A
3972238SN/A/// Target getgidPseudo() handler.
3982238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3993114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4002238SN/A
4012238SN/A
4021354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4031354SN/Aconst int one_million = 1000000;
40410796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
40510796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4061354SN/A
4071354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4081354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4091354SN/A/// real-world time) to keep simulations repeatable.
4101354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4111354SN/A
4121354SN/A/// Helper function to convert current elapsed time to seconds and
4131354SN/A/// microseconds.
4141354SN/Atemplate <class T1, class T2>
4151354SN/Avoid
41610796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4171354SN/A{
41810796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4191354SN/A    sec = elapsed_usecs / one_million;
4201354SN/A    usec = elapsed_usecs % one_million;
4211354SN/A}
4221354SN/A
42310796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
42410796Sbrandon.potter@amd.com/// nanoseconds.
42510796Sbrandon.potter@amd.comtemplate <class T1, class T2>
42610796Sbrandon.potter@amd.comvoid
42710796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
42810796Sbrandon.potter@amd.com{
42910796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
43010796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
43110796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
43210796Sbrandon.potter@amd.com}
43310796Sbrandon.potter@amd.com
434360SN/A//////////////////////////////////////////////////////////////////////
435360SN/A//
436360SN/A// The following emulation functions are generic, but need to be
437360SN/A// templated to account for differences in types, constants, etc.
438360SN/A//
439360SN/A//////////////////////////////////////////////////////////////////////
440360SN/A
4413113Sgblack@eecs.umich.edu#if NO_STAT64
4423113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4433113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4443113Sgblack@eecs.umich.edu#else
4453113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4463113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4473113Sgblack@eecs.umich.edu#endif
4483113Sgblack@eecs.umich.edu
4493113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4503113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4513113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4523113Sgblack@eecs.umich.edu
4533113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4543113Sgblack@eecs.umich.edustatic void
4553113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4563113Sgblack@eecs.umich.edu{
4574189Sgblack@eecs.umich.edu    using namespace TheISA;
4584189Sgblack@eecs.umich.edu
4593113Sgblack@eecs.umich.edu    if (fakeTTY)
4603113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4613113Sgblack@eecs.umich.edu    else
4623113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4638737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4643113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4658737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4663277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4675515SMichael.Adler@intel.com    if (fakeTTY) {
4685515SMichael.Adler@intel.com        // Claim to be a character device
4695515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4705515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4715515SMichael.Adler@intel.com    }
4728737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4733277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4748737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4753277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4768737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4773277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4788737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4793113Sgblack@eecs.umich.edu    if (fakeTTY)
4803113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4813113Sgblack@eecs.umich.edu    else
4823113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4838737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4843113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4858737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4863114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4878737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4883114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4898737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4903114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4918737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4924061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4934061Sgblack@eecs.umich.edu    // consistently across different hosts.
4944061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4958737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4963113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4978737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4983113Sgblack@eecs.umich.edu}
4993113Sgblack@eecs.umich.edu
5003113Sgblack@eecs.umich.edu// Same for stat64
5013113Sgblack@eecs.umich.edu
5023113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5033113Sgblack@eecs.umich.edustatic void
5043113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5053113Sgblack@eecs.umich.edu{
5064189Sgblack@eecs.umich.edu    using namespace TheISA;
5074189Sgblack@eecs.umich.edu
5083113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5093113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5103113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5118737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5123113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5138737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5143113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5158737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5163113Sgblack@eecs.umich.edu#else
5173113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5183113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5193113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5203113Sgblack@eecs.umich.edu#endif
5213113Sgblack@eecs.umich.edu}
5223113Sgblack@eecs.umich.edu
5233113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5243113Sgblack@eecs.umich.edutemplate<class OS>
5253113Sgblack@eecs.umich.edustatic void
5268852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5273113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5283113Sgblack@eecs.umich.edu{
5293113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5303113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5313113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5323113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5333113Sgblack@eecs.umich.edu}
5343113Sgblack@eecs.umich.edu
5353113Sgblack@eecs.umich.edutemplate<class OS>
5363113Sgblack@eecs.umich.edustatic void
5378852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5383113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5393113Sgblack@eecs.umich.edu{
5403113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5413113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5426686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5433113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5443113Sgblack@eecs.umich.edu}
5453113Sgblack@eecs.umich.edu
546378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
547378SN/A/// only to find out if their stdout is a tty, to determine whether to
5489141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5499141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
550360SN/Atemplate <class OS>
5511450SN/ASyscallReturn
5523114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5532680Sktlim@umich.edu          ThreadContext *tc)
554360SN/A{
5556701Sgblack@eecs.umich.edu    int index = 0;
5566701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5576701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
558360SN/A
5591969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
560360SN/A
56110496Ssteve.reinhardt@amd.com    Process::FdMap *fdObj = process->sim_fd_obj(fd);
56210496Ssteve.reinhardt@amd.com
56310496Ssteve.reinhardt@amd.com    if (fdObj == NULL) {
564360SN/A        // doesn't map to any simulator fd: not a valid target fd
5651458SN/A        return -EBADF;
566360SN/A    }
567360SN/A
56810496Ssteve.reinhardt@amd.com    if (fdObj->driver != NULL) {
56910496Ssteve.reinhardt@amd.com        return fdObj->driver->ioctl(process, tc, req);
57010496Ssteve.reinhardt@amd.com    }
57110496Ssteve.reinhardt@amd.com
5729141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5731458SN/A        return -ENOTTY;
5749141Smarc.orr@gmail.com    }
575360SN/A
5769141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5779141Smarc.orr@gmail.com         fd, req, tc->pcState());
5789141Smarc.orr@gmail.com    return -ENOTTY;
579360SN/A}
580360SN/A
581360SN/Atemplate <class OS>
58210027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5833114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
58410027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
585360SN/A{
586360SN/A    std::string path;
587360SN/A
5888852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5896701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5901458SN/A        return -EFAULT;
591360SN/A
5926701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5936701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
594360SN/A    int hostFlags = 0;
595360SN/A
596360SN/A    // translate open flags
597360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
598360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
599360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
600360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
601360SN/A        }
602360SN/A    }
603360SN/A
604360SN/A    // any target flags left?
605360SN/A    if (tgtFlags != 0)
6061706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
607360SN/A
608360SN/A#ifdef __CYGWIN32__
609360SN/A    hostFlags |= O_BINARY;
610360SN/A#endif
611360SN/A
6123669Sbinkertn@umich.edu    // Adjust path for current working directory
6133669Sbinkertn@umich.edu    path = process->fullPath(path);
6143669Sbinkertn@umich.edu
6151706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6161706SN/A
61710496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
61810496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
61910496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
62010496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
62110496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
62210496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
62310496Ssteve.reinhardt@amd.com            return -ENOENT;
62410496Ssteve.reinhardt@amd.com        }
62510496Ssteve.reinhardt@amd.com
62610496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
62710496Ssteve.reinhardt@amd.com        if (drv != NULL) {
62810496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
62910496Ssteve.reinhardt@amd.com            // process if necessary.
63010496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
63110496Ssteve.reinhardt@amd.com        }
63210496Ssteve.reinhardt@amd.com
63310496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
63410496Ssteve.reinhardt@amd.com        // /dev/zero
63510496Ssteve.reinhardt@amd.com    }
63610496Ssteve.reinhardt@amd.com
6375795Ssaidi@eecs.umich.edu    int fd;
6389143Ssteve.reinhardt@amd.com    int local_errno;
6399142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6409142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6419143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6425795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6439143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6445795Ssaidi@eecs.umich.edu     } else {
6455795Ssaidi@eecs.umich.edu        // open the file
6465795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6479143Ssteve.reinhardt@amd.com        local_errno = errno;
6485795Ssaidi@eecs.umich.edu     }
649360SN/A
6509143Ssteve.reinhardt@amd.com    if (fd == -1)
6519143Ssteve.reinhardt@amd.com        return -local_errno;
6529143Ssteve.reinhardt@amd.com
6539143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
654360SN/A}
655360SN/A
65610027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
65710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
65810027SChris.Adeniyi-Jones@arm.comSyscallReturn
65910027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
66110027SChris.Adeniyi-Jones@arm.com{
66210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
66310027SChris.Adeniyi-Jones@arm.com}
66410027SChris.Adeniyi-Jones@arm.com
66510027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
66610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
66710027SChris.Adeniyi-Jones@arm.comSyscallReturn
66810027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66910027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67010027SChris.Adeniyi-Jones@arm.com{
67110027SChris.Adeniyi-Jones@arm.com    int index = 0;
67210027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
67310027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
67410027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
67510027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
67610027SChris.Adeniyi-Jones@arm.com}
67710027SChris.Adeniyi-Jones@arm.com
67810633Smichaelupton@gmail.com/// Target unlinkat() handler.
67910633Smichaelupton@gmail.comtemplate <class OS>
68010633Smichaelupton@gmail.comSyscallReturn
68110633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68210633Smichaelupton@gmail.com             ThreadContext *tc)
68310633Smichaelupton@gmail.com{
68410633Smichaelupton@gmail.com    int index = 0;
68510633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
68610633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
68710633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
68810633Smichaelupton@gmail.com
68910633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
69010633Smichaelupton@gmail.com}
69110633Smichaelupton@gmail.com
69210203SAli.Saidi@ARM.com/// Target facessat() handler
69310203SAli.Saidi@ARM.comtemplate <class OS>
69410203SAli.Saidi@ARM.comSyscallReturn
69510203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69610203SAli.Saidi@ARM.com        ThreadContext *tc)
69710203SAli.Saidi@ARM.com{
69810203SAli.Saidi@ARM.com    int index = 0;
69910203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
70010203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
70110203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
70210203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
70310203SAli.Saidi@ARM.com}
70410203SAli.Saidi@ARM.com
70510203SAli.Saidi@ARM.com/// Target readlinkat() handler
70610203SAli.Saidi@ARM.comtemplate <class OS>
70710203SAli.Saidi@ARM.comSyscallReturn
70810203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70910203SAli.Saidi@ARM.com        ThreadContext *tc)
71010203SAli.Saidi@ARM.com{
71110203SAli.Saidi@ARM.com    int index = 0;
71210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71410203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
71510203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
71610203SAli.Saidi@ARM.com}
71710203SAli.Saidi@ARM.com
7186640Svince@csl.cornell.edu/// Target sysinfo() handler.
7196640Svince@csl.cornell.edutemplate <class OS>
7206640Svince@csl.cornell.eduSyscallReturn
7216640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7226640Svince@csl.cornell.edu         ThreadContext *tc)
7236640Svince@csl.cornell.edu{
7246640Svince@csl.cornell.edu
7256701Sgblack@eecs.umich.edu    int index = 0;
7266701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
72710793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7286640Svince@csl.cornell.edu
7296701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7306701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7316640Svince@csl.cornell.edu
7328706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7336640Svince@csl.cornell.edu
7346701Sgblack@eecs.umich.edu    return 0;
7356640Svince@csl.cornell.edu}
736360SN/A
7371999SN/A/// Target chmod() handler.
7381999SN/Atemplate <class OS>
7391999SN/ASyscallReturn
7403114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7412680Sktlim@umich.edu          ThreadContext *tc)
7421999SN/A{
7431999SN/A    std::string path;
7441999SN/A
7456701Sgblack@eecs.umich.edu    int index = 0;
7468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7476701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7481999SN/A        return -EFAULT;
7496701Sgblack@eecs.umich.edu    }
7501999SN/A
7516701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7521999SN/A    mode_t hostMode = 0;
7531999SN/A
7541999SN/A    // XXX translate mode flags via OS::something???
7551999SN/A    hostMode = mode;
7561999SN/A
7573669Sbinkertn@umich.edu    // Adjust path for current working directory
7583669Sbinkertn@umich.edu    path = process->fullPath(path);
7593669Sbinkertn@umich.edu
7601999SN/A    // do the chmod
7611999SN/A    int result = chmod(path.c_str(), hostMode);
7621999SN/A    if (result < 0)
7632218SN/A        return -errno;
7641999SN/A
7651999SN/A    return 0;
7661999SN/A}
7671999SN/A
7681999SN/A
7691999SN/A/// Target fchmod() handler.
7701999SN/Atemplate <class OS>
7711999SN/ASyscallReturn
7723114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7732680Sktlim@umich.edu           ThreadContext *tc)
7741999SN/A{
7756701Sgblack@eecs.umich.edu    int index = 0;
7766701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7771999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7781999SN/A        // doesn't map to any simulator fd: not a valid target fd
7791999SN/A        return -EBADF;
7801999SN/A    }
7811999SN/A
7826701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7831999SN/A    mode_t hostMode = 0;
7841999SN/A
7851999SN/A    // XXX translate mode flags via OS::someting???
7861999SN/A    hostMode = mode;
7871999SN/A
7881999SN/A    // do the fchmod
7891999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7901999SN/A    if (result < 0)
7912218SN/A        return -errno;
7921999SN/A
7931999SN/A    return 0;
7941999SN/A}
7951999SN/A
7965877Shsul@eecs.umich.edu/// Target mremap() handler.
7975877Shsul@eecs.umich.edutemplate <class OS>
7985877Shsul@eecs.umich.eduSyscallReturn
7995877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8005877Shsul@eecs.umich.edu{
8016701Sgblack@eecs.umich.edu    int index = 0;
8026701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8036701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8046701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8056701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
80610027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
80710027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
80810027SChris.Adeniyi-Jones@arm.com
80910027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
81010027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8115877Shsul@eecs.umich.edu
81210318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
81310318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8145877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8155877Shsul@eecs.umich.edu        return -EINVAL;
8165877Shsul@eecs.umich.edu    }
8175877Shsul@eecs.umich.edu
81810486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
81910486Stjablin@gmail.com
8205877Shsul@eecs.umich.edu    if (new_length > old_length) {
82110027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
82210027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8235877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8248601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8255877Shsul@eecs.umich.edu            process->mmap_end += diff;
8265877Shsul@eecs.umich.edu            return start;
8275877Shsul@eecs.umich.edu        } else {
82810027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8295877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8305877Shsul@eecs.umich.edu                return -ENOMEM;
8315877Shsul@eecs.umich.edu            } else {
83210027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
83310027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
83410027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
83510027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
83610027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
83710027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8385877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
83910027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
84010027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
84110027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
84210027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
84310027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
84410027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
84510027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
84610027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
84710027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
84810027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
84910027SChris.Adeniyi-Jones@arm.com                }
85010027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
85110027SChris.Adeniyi-Jones@arm.com                return new_start;
8525877Shsul@eecs.umich.edu            }
8535877Shsul@eecs.umich.edu        }
8545877Shsul@eecs.umich.edu    } else {
85510027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
85610027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8578601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
85810027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8595877Shsul@eecs.umich.edu    }
8605877Shsul@eecs.umich.edu}
8611999SN/A
862378SN/A/// Target stat() handler.
863360SN/Atemplate <class OS>
8641450SN/ASyscallReturn
8653114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8662680Sktlim@umich.edu         ThreadContext *tc)
867360SN/A{
868360SN/A    std::string path;
869360SN/A
8706701Sgblack@eecs.umich.edu    int index = 0;
8718852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8726701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8736701Sgblack@eecs.umich.edu        return -EFAULT;
8746701Sgblack@eecs.umich.edu    }
8756701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
876360SN/A
8773669Sbinkertn@umich.edu    // Adjust path for current working directory
8783669Sbinkertn@umich.edu    path = process->fullPath(path);
8793669Sbinkertn@umich.edu
880360SN/A    struct stat hostBuf;
881360SN/A    int result = stat(path.c_str(), &hostBuf);
882360SN/A
883360SN/A    if (result < 0)
8842218SN/A        return -errno;
885360SN/A
8868706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
887360SN/A
8881458SN/A    return 0;
889360SN/A}
890360SN/A
891360SN/A
8925074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8935074Ssaidi@eecs.umich.edutemplate <class OS>
8945074Ssaidi@eecs.umich.eduSyscallReturn
8955074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8965074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8975074Ssaidi@eecs.umich.edu{
8985074Ssaidi@eecs.umich.edu    std::string path;
8995074Ssaidi@eecs.umich.edu
9006701Sgblack@eecs.umich.edu    int index = 0;
9018852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9026701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9035074Ssaidi@eecs.umich.edu        return -EFAULT;
9046701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9055074Ssaidi@eecs.umich.edu
9065074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9075074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9085074Ssaidi@eecs.umich.edu
9095208Ssaidi@eecs.umich.edu#if NO_STAT64
9105208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9115208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9125208Ssaidi@eecs.umich.edu#else
9135074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9145074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9155208Ssaidi@eecs.umich.edu#endif
9165074Ssaidi@eecs.umich.edu
9175074Ssaidi@eecs.umich.edu    if (result < 0)
9185074Ssaidi@eecs.umich.edu        return -errno;
9195074Ssaidi@eecs.umich.edu
9208706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9215074Ssaidi@eecs.umich.edu
9225074Ssaidi@eecs.umich.edu    return 0;
9235074Ssaidi@eecs.umich.edu}
9245074Ssaidi@eecs.umich.edu
9255074Ssaidi@eecs.umich.edu
92610027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
92710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
92810027SChris.Adeniyi-Jones@arm.comSyscallReturn
92910027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
93010027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
93110027SChris.Adeniyi-Jones@arm.com{
93210027SChris.Adeniyi-Jones@arm.com    int index = 0;
93310027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
93410027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
93510793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
93610027SChris.Adeniyi-Jones@arm.com
93710027SChris.Adeniyi-Jones@arm.com    std::string path;
93810027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
93910027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
94010027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
94110027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
94210027SChris.Adeniyi-Jones@arm.com
94310027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
94410027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
94510027SChris.Adeniyi-Jones@arm.com
94610027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
94710027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
94810027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
94910027SChris.Adeniyi-Jones@arm.com#else
95010027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
95110027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
95210027SChris.Adeniyi-Jones@arm.com#endif
95310027SChris.Adeniyi-Jones@arm.com
95410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
95510027SChris.Adeniyi-Jones@arm.com        return -errno;
95610027SChris.Adeniyi-Jones@arm.com
95710027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
95810027SChris.Adeniyi-Jones@arm.com
95910027SChris.Adeniyi-Jones@arm.com    return 0;
96010027SChris.Adeniyi-Jones@arm.com}
96110027SChris.Adeniyi-Jones@arm.com
96210027SChris.Adeniyi-Jones@arm.com
9631999SN/A/// Target fstat64() handler.
9641999SN/Atemplate <class OS>
9651999SN/ASyscallReturn
9663114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9672680Sktlim@umich.edu            ThreadContext *tc)
9681999SN/A{
9696701Sgblack@eecs.umich.edu    int index = 0;
9706701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9716701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9721999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9731999SN/A        // doesn't map to any simulator fd: not a valid target fd
9741999SN/A        return -EBADF;
9751999SN/A    }
9761999SN/A
9772764Sstever@eecs.umich.edu#if NO_STAT64
9782064SN/A    struct stat  hostBuf;
9792064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
9802064SN/A#else
9812064SN/A    struct stat64  hostBuf;
9821999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
9832064SN/A#endif
9841999SN/A
9851999SN/A    if (result < 0)
9862218SN/A        return -errno;
9871999SN/A
9888706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9891999SN/A
9901999SN/A    return 0;
9911999SN/A}
9921999SN/A
9931999SN/A
994378SN/A/// Target lstat() handler.
995360SN/Atemplate <class OS>
9961450SN/ASyscallReturn
9973114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9982680Sktlim@umich.edu          ThreadContext *tc)
999360SN/A{
1000360SN/A    std::string path;
1001360SN/A
10026701Sgblack@eecs.umich.edu    int index = 0;
10038852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10046701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10056701Sgblack@eecs.umich.edu        return -EFAULT;
10066701Sgblack@eecs.umich.edu    }
10076701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1008360SN/A
10093669Sbinkertn@umich.edu    // Adjust path for current working directory
10103669Sbinkertn@umich.edu    path = process->fullPath(path);
10113669Sbinkertn@umich.edu
1012360SN/A    struct stat hostBuf;
1013360SN/A    int result = lstat(path.c_str(), &hostBuf);
1014360SN/A
1015360SN/A    if (result < 0)
10161458SN/A        return -errno;
1017360SN/A
10188706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1019360SN/A
10201458SN/A    return 0;
1021360SN/A}
1022360SN/A
10231999SN/A/// Target lstat64() handler.
10241999SN/Atemplate <class OS>
10251999SN/ASyscallReturn
10263114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10272680Sktlim@umich.edu            ThreadContext *tc)
10281999SN/A{
10291999SN/A    std::string path;
10301999SN/A
10316701Sgblack@eecs.umich.edu    int index = 0;
10328852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10336701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10346701Sgblack@eecs.umich.edu        return -EFAULT;
10356701Sgblack@eecs.umich.edu    }
10366701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10371999SN/A
10383669Sbinkertn@umich.edu    // Adjust path for current working directory
10393669Sbinkertn@umich.edu    path = process->fullPath(path);
10403669Sbinkertn@umich.edu
10412764Sstever@eecs.umich.edu#if NO_STAT64
10422064SN/A    struct stat hostBuf;
10432064SN/A    int result = lstat(path.c_str(), &hostBuf);
10442064SN/A#else
10451999SN/A    struct stat64 hostBuf;
10461999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10472064SN/A#endif
10481999SN/A
10491999SN/A    if (result < 0)
10501999SN/A        return -errno;
10511999SN/A
10528706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10531999SN/A
10541999SN/A    return 0;
10551999SN/A}
10561999SN/A
1057378SN/A/// Target fstat() handler.
1058360SN/Atemplate <class OS>
10591450SN/ASyscallReturn
10603114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10612680Sktlim@umich.edu          ThreadContext *tc)
1062360SN/A{
10636701Sgblack@eecs.umich.edu    int index = 0;
10646701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10656701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1066360SN/A
10671969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1068360SN/A
1069360SN/A    if (fd < 0)
10701458SN/A        return -EBADF;
1071360SN/A
1072360SN/A    struct stat hostBuf;
1073360SN/A    int result = fstat(fd, &hostBuf);
1074360SN/A
1075360SN/A    if (result < 0)
10761458SN/A        return -errno;
1077360SN/A
10788706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10792021SN/A
10801458SN/A    return 0;
1081360SN/A}
1082360SN/A
1083360SN/A
10841706SN/A/// Target statfs() handler.
10851706SN/Atemplate <class OS>
10861706SN/ASyscallReturn
10873114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10882680Sktlim@umich.edu           ThreadContext *tc)
10891706SN/A{
10901706SN/A    std::string path;
10911706SN/A
10926701Sgblack@eecs.umich.edu    int index = 0;
10938852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10946701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10956701Sgblack@eecs.umich.edu        return -EFAULT;
10966701Sgblack@eecs.umich.edu    }
10976701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10981706SN/A
10993669Sbinkertn@umich.edu    // Adjust path for current working directory
11003669Sbinkertn@umich.edu    path = process->fullPath(path);
11013669Sbinkertn@umich.edu
11021706SN/A    struct statfs hostBuf;
11031706SN/A    int result = statfs(path.c_str(), &hostBuf);
11041706SN/A
11051706SN/A    if (result < 0)
11062218SN/A        return -errno;
11071706SN/A
11088706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11091706SN/A
11101706SN/A    return 0;
11111706SN/A}
11121706SN/A
11131706SN/A
11141706SN/A/// Target fstatfs() handler.
11151706SN/Atemplate <class OS>
11161706SN/ASyscallReturn
11173114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11182680Sktlim@umich.edu            ThreadContext *tc)
11191706SN/A{
11206701Sgblack@eecs.umich.edu    int index = 0;
11216701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11226701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11231706SN/A
11241706SN/A    if (fd < 0)
11251706SN/A        return -EBADF;
11261706SN/A
11271706SN/A    struct statfs hostBuf;
11281706SN/A    int result = fstatfs(fd, &hostBuf);
11291706SN/A
11301706SN/A    if (result < 0)
11312218SN/A        return -errno;
11321706SN/A
11338706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11341706SN/A
11351706SN/A    return 0;
11361706SN/A}
11371706SN/A
11381706SN/A
11391999SN/A/// Target writev() handler.
11401999SN/Atemplate <class OS>
11411999SN/ASyscallReturn
11423114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11432680Sktlim@umich.edu           ThreadContext *tc)
11441999SN/A{
11456701Sgblack@eecs.umich.edu    int index = 0;
11466701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11471999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11481999SN/A        // doesn't map to any simulator fd: not a valid target fd
11491999SN/A        return -EBADF;
11501999SN/A    }
11511999SN/A
11528852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11536701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11546701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11551999SN/A    struct iovec hiov[count];
11566227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11571999SN/A        typename OS::tgt_iovec tiov;
11582461SN/A
11598852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11608852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11618737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11621999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11638852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11648852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11651999SN/A    }
11661999SN/A
11671999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11681999SN/A
11696227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11701999SN/A        delete [] (char *)hiov[i].iov_base;
11711999SN/A
11721999SN/A    if (result < 0)
11732218SN/A        return -errno;
11741999SN/A
117510629Sjthestness@gmail.com    return result;
11761999SN/A}
11771999SN/A
11781999SN/A
1179378SN/A/// Target mmap() handler.
1180378SN/A///
1181378SN/A/// We don't really handle mmap().  If the target is mmaping an
1182378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1183378SN/A/// nothing (since memory is initialized to zero and the simulator
11848324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
11858324Ssteve.reinhardt@amd.com///
1186360SN/Atemplate <class OS>
11871450SN/ASyscallReturn
11883114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1189360SN/A{
11906701Sgblack@eecs.umich.edu    int index = 0;
11916701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
11926701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
11936701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
11946701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
11958324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
119610486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1197360SN/A
11989008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
11999008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12009008Sgblack@eecs.umich.edu
12018324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
12028324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
12038324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
12048324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12058324Ssteve.reinhardt@amd.com            return -EBADF;
12068324Ssteve.reinhardt@amd.com        }
12078324Ssteve.reinhardt@amd.com
12088324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
12098324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12108324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12118324Ssteve.reinhardt@amd.com            // another name on some platform
12128324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
12138324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
12148324Ssteve.reinhardt@amd.com        }
12158324Ssteve.reinhardt@amd.com    }
12165877Shsul@eecs.umich.edu
121710486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
121810486Stjablin@gmail.com
121910318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
122010486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12212544SN/A        warn("mmap failing: arguments not page-aligned: "
122210486Stjablin@gmail.com             "start 0x%x offset 0x%x",
122310486Stjablin@gmail.com             start, offset);
12242544SN/A        return -EINVAL;
1225360SN/A    }
1226360SN/A
12278600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12288600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12298600Ssteve.reinhardt@amd.com    bool clobber = false;
12308600Ssteve.reinhardt@amd.com
12318600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12328600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12338600Ssteve.reinhardt@amd.com
12348600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12358600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12368600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12378600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12388600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12398600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12408600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
124110485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
124210485SMichael.Adler@intel.com                return -EINVAL;
12438600Ssteve.reinhardt@amd.com            } else {
12448600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12458600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12468600Ssteve.reinhardt@amd.com                use_provided_address = false;
12478600Ssteve.reinhardt@amd.com            }
12488600Ssteve.reinhardt@amd.com        }
12492544SN/A    }
12502544SN/A
12518600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12528600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12538600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12548600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12558600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12568600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12578600Ssteve.reinhardt@amd.com        } else {
12588600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12598600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12608600Ssteve.reinhardt@amd.com        }
12616672Sgblack@eecs.umich.edu    }
12628600Ssteve.reinhardt@amd.com
12638601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12642544SN/A
12651458SN/A    return start;
1266360SN/A}
1267360SN/A
1268378SN/A/// Target getrlimit() handler.
1269360SN/Atemplate <class OS>
12701450SN/ASyscallReturn
12713114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12722680Sktlim@umich.edu        ThreadContext *tc)
1273360SN/A{
12746701Sgblack@eecs.umich.edu    int index = 0;
12756701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12766701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1277360SN/A
1278360SN/A    switch (resource) {
12792064SN/A        case OS::TGT_RLIMIT_STACK:
12805877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
12812064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
12828737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12838737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12842064SN/A            break;
1285360SN/A
12865877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
12875877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
12885877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
12898737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12908737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12915877Shsul@eecs.umich.edu            break;
12925877Shsul@eecs.umich.edu
12932064SN/A        default:
129410794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
129510794Sbrandon.potter@amd.com            return -EINVAL;
12962064SN/A            break;
1297360SN/A    }
1298360SN/A
12998706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13001458SN/A    return 0;
1301360SN/A}
1302360SN/A
130310796Sbrandon.potter@amd.com/// Target clock_gettime() function.
130410796Sbrandon.potter@amd.comtemplate <class OS>
130510796Sbrandon.potter@amd.comSyscallReturn
130610796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
130710796Sbrandon.potter@amd.com{
130810796Sbrandon.potter@amd.com    int index = 1;
130910796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
131010796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
131110796Sbrandon.potter@amd.com
131210796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
131310796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
131410796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
131510796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
131610796Sbrandon.potter@amd.com
131710796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
131810796Sbrandon.potter@amd.com
131910796Sbrandon.potter@amd.com    return 0;
132010796Sbrandon.potter@amd.com}
132110796Sbrandon.potter@amd.com
1322378SN/A/// Target gettimeofday() handler.
1323360SN/Atemplate <class OS>
13241450SN/ASyscallReturn
13253114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13262680Sktlim@umich.edu        ThreadContext *tc)
1327360SN/A{
13286701Sgblack@eecs.umich.edu    int index = 0;
13296701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1330360SN/A
133110796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1332360SN/A    tp->tv_sec += seconds_since_epoch;
13336109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13346109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1335360SN/A
13368706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1337360SN/A
13381458SN/A    return 0;
1339360SN/A}
1340360SN/A
1341360SN/A
13421999SN/A/// Target utimes() handler.
13431999SN/Atemplate <class OS>
13441999SN/ASyscallReturn
13453114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13462680Sktlim@umich.edu           ThreadContext *tc)
13471999SN/A{
13481999SN/A    std::string path;
13491999SN/A
13506701Sgblack@eecs.umich.edu    int index = 0;
13518852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13526701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13536701Sgblack@eecs.umich.edu        return -EFAULT;
13546701Sgblack@eecs.umich.edu    }
13551999SN/A
13566701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13576701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13588706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13591999SN/A
13601999SN/A    struct timeval hostTimeval[2];
13611999SN/A    for (int i = 0; i < 2; ++i)
13621999SN/A    {
13638737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13648737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13651999SN/A    }
13663669Sbinkertn@umich.edu
13673669Sbinkertn@umich.edu    // Adjust path for current working directory
13683669Sbinkertn@umich.edu    path = process->fullPath(path);
13693669Sbinkertn@umich.edu
13701999SN/A    int result = utimes(path.c_str(), hostTimeval);
13711999SN/A
13721999SN/A    if (result < 0)
13731999SN/A        return -errno;
13741999SN/A
13751999SN/A    return 0;
13761999SN/A}
1377378SN/A/// Target getrusage() function.
1378360SN/Atemplate <class OS>
13791450SN/ASyscallReturn
13803114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13812680Sktlim@umich.edu              ThreadContext *tc)
1382360SN/A{
13836701Sgblack@eecs.umich.edu    int index = 0;
13846701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13856701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1386360SN/A
13873670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13883670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1389360SN/A    rup->ru_stime.tv_sec = 0;
1390360SN/A    rup->ru_stime.tv_usec = 0;
1391360SN/A    rup->ru_maxrss = 0;
1392360SN/A    rup->ru_ixrss = 0;
1393360SN/A    rup->ru_idrss = 0;
1394360SN/A    rup->ru_isrss = 0;
1395360SN/A    rup->ru_minflt = 0;
1396360SN/A    rup->ru_majflt = 0;
1397360SN/A    rup->ru_nswap = 0;
1398360SN/A    rup->ru_inblock = 0;
1399360SN/A    rup->ru_oublock = 0;
1400360SN/A    rup->ru_msgsnd = 0;
1401360SN/A    rup->ru_msgrcv = 0;
1402360SN/A    rup->ru_nsignals = 0;
1403360SN/A    rup->ru_nvcsw = 0;
1404360SN/A    rup->ru_nivcsw = 0;
1405360SN/A
14063670Sbinkertn@umich.edu    switch (who) {
14073670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
140810796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14098737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14108737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14113670Sbinkertn@umich.edu        break;
14123670Sbinkertn@umich.edu
14133670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14143670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14153670Sbinkertn@umich.edu        break;
14163670Sbinkertn@umich.edu
14173670Sbinkertn@umich.edu      default:
14183670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14193670Sbinkertn@umich.edu        // plow ahead
14203670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14213670Sbinkertn@umich.edu             who);
14223670Sbinkertn@umich.edu    }
14233670Sbinkertn@umich.edu
14248706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1425360SN/A
14261458SN/A    return 0;
1427360SN/A}
1428360SN/A
14296683Stjones1@inf.ed.ac.uk/// Target times() function.
14306683Stjones1@inf.ed.ac.uktemplate <class OS>
14316683Stjones1@inf.ed.ac.ukSyscallReturn
14326683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14336683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14346683Stjones1@inf.ed.ac.uk{
14356701Sgblack@eecs.umich.edu    int index = 0;
14366701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14376683Stjones1@inf.ed.ac.uk
14386683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14397823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14406683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14416683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14426683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14436683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14446683Stjones1@inf.ed.ac.uk
14456683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14468737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14476683Stjones1@inf.ed.ac.uk
14486683Stjones1@inf.ed.ac.uk    // Write back
14498706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14506683Stjones1@inf.ed.ac.uk
14516683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14526683Stjones1@inf.ed.ac.uk    return clocks;
14536683Stjones1@inf.ed.ac.uk}
14542553SN/A
14556684Stjones1@inf.ed.ac.uk/// Target time() function.
14566684Stjones1@inf.ed.ac.uktemplate <class OS>
14576684Stjones1@inf.ed.ac.ukSyscallReturn
14586684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14596684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14606684Stjones1@inf.ed.ac.uk{
14616684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
146210796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
14636684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14646684Stjones1@inf.ed.ac.uk
14656701Sgblack@eecs.umich.edu    int index = 0;
14666701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14676684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14686684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14698737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14708852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14718852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14726684Stjones1@inf.ed.ac.uk    }
14736684Stjones1@inf.ed.ac.uk    return sec;
14746684Stjones1@inf.ed.ac.uk}
14752553SN/A
14762553SN/A
14771354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1478