syscall_emul.hh revision 10796
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).
100360SN/A
101360SN/A    /// Flag values for controlling syscall behavior.
102360SN/A    enum Flags {
103360SN/A        /// Don't set return regs according to funcPtr return value.
104360SN/A        /// Used for syscalls with non-standard return conventions
1052680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
106360SN/A        /// sigreturn).
107360SN/A        SuppressReturnValue = 1
108360SN/A    };
109360SN/A
110360SN/A    /// Constructor.
111360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
112360SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags)
113360SN/A    {
114360SN/A    }
115360SN/A
116360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1173114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
118360SN/A};
119360SN/A
120360SN/A
121360SN/A//////////////////////////////////////////////////////////////////////
122360SN/A//
123360SN/A// The following emulation functions are generic enough that they
124360SN/A// don't need to be recompiled for different emulated OS's.  They are
125360SN/A// defined in sim/syscall_emul.cc.
126360SN/A//
127360SN/A//////////////////////////////////////////////////////////////////////
128360SN/A
129360SN/A
130378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1311706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1323114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
133378SN/A
134378SN/A/// Handler for unimplemented syscalls that we never intend to
135378SN/A/// implement (signal handling, etc.) and should not affect the correct
136378SN/A/// behavior of the program.  Print a warning only if the appropriate
137378SN/A/// trace flag is enabled.  Return success to the target program.
1381706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1393114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1408149SChris.Emmons@ARM.comSyscallReturn ignoreWarnOnceFunc(SyscallDesc *desc, int num,
1418149SChris.Emmons@ARM.com                         LiveProcess *p, ThreadContext *tc);
142360SN/A
1436109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1441706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1453114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
146378SN/A
1476109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1486109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1496109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1506109Ssanchezd@stanford.edu
151378SN/A/// Target getpagesize() handler.
1521706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1533114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
154378SN/A
1555748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1565748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1575748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
158378SN/A
159378SN/A/// Target close() handler.
1601706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1613114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
162378SN/A
163378SN/A/// Target read() handler.
1641706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1653114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
166378SN/A
167378SN/A/// Target write() handler.
1681706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1693114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
170378SN/A
171378SN/A/// Target lseek() handler.
1721706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1733114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
174378SN/A
1754118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1764118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1774118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1784118Sgblack@eecs.umich.edu
179378SN/A/// Target munmap() handler.
1801706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1813114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
182378SN/A
183378SN/A/// Target gethostname() handler.
1841706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1853114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
186360SN/A
1875513SMichael.Adler@intel.com/// Target getcwd() handler.
1885513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
1895513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
1905513SMichael.Adler@intel.com
19110203SAli.Saidi@ARM.com/// Target readlink() handler.
19210203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19310203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
19410203SAli.Saidi@ARM.com                           int index = 0);
1955513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
1965513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
1975513SMichael.Adler@intel.com
198511SN/A/// Target unlink() handler.
19910633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20010633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
20110633Smichaelupton@gmail.com                           int index);
2021706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2033114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
204511SN/A
2055513SMichael.Adler@intel.com/// Target mkdir() handler.
2065513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2075513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2085513SMichael.Adler@intel.com
209511SN/A/// Target rename() handler.
2101706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2113114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2121706SN/A
2131706SN/A
2141706SN/A/// Target truncate() handler.
2151706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2163114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2171706SN/A
2181706SN/A
2191706SN/A/// Target ftruncate() handler.
2201706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2213114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2221706SN/A
223511SN/A
2246703Svince@csl.cornell.edu/// Target truncate64() handler.
2256703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2266703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2276703Svince@csl.cornell.edu
2286685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2296685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2306685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2316685Stjones1@inf.ed.ac.uk
2326685Stjones1@inf.ed.ac.uk
2335513SMichael.Adler@intel.com/// Target umask() handler.
2345513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2355513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2365513SMichael.Adler@intel.com
2375513SMichael.Adler@intel.com
2381999SN/A/// Target chown() handler.
2391999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2403114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2411999SN/A
2421999SN/A
2431999SN/A/// Target fchown() handler.
2441999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2453114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2461999SN/A
2473079Sstever@eecs.umich.edu/// Target dup() handler.
2483079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2493114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2503079Sstever@eecs.umich.edu
2512093SN/A/// Target fnctl() handler.
2522093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2533114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2542093SN/A
2552687Sksewell@umich.edu/// Target fcntl64() handler.
2562687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2573114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2582687Sksewell@umich.edu
2592238SN/A/// Target setuid() handler.
2602238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2613114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2622238SN/A
2632238SN/A/// Target getpid() handler.
2642238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2653114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2662238SN/A
2672238SN/A/// Target getuid() handler.
2682238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2693114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2702238SN/A
2712238SN/A/// Target getgid() handler.
2722238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2742238SN/A
2752238SN/A/// Target getppid() handler.
2762238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2773114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2782238SN/A
2792238SN/A/// Target geteuid() handler.
2802238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2813114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2822238SN/A
2832238SN/A/// Target getegid() handler.
2842238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2853114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2862238SN/A
2876109Ssanchezd@stanford.edu/// Target clone() handler.
2886109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
2896109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
2902238SN/A
2919455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2929455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2939455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
29410203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
29510203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
29610203SAli.Saidi@ARM.com                               int index);
2979455Smitch.hayenga+gem5@gmail.com
2989112Smarc.orr@gmail.com/// Futex system call
2999112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3009112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3019112Smarc.orr@gmail.comtemplate <class OS>
3029112Smarc.orr@gmail.comSyscallReturn
3039112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3049112Smarc.orr@gmail.com          ThreadContext *tc)
3059112Smarc.orr@gmail.com{
3069112Smarc.orr@gmail.com    int index_uaddr = 0;
3079112Smarc.orr@gmail.com    int index_op = 1;
3089112Smarc.orr@gmail.com    int index_val = 2;
3099112Smarc.orr@gmail.com    int index_timeout = 3;
3109112Smarc.orr@gmail.com
3119112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3129112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3139112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3149112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3159112Smarc.orr@gmail.com
3169112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3179112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3189112Smarc.orr@gmail.com
3199112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3209112Smarc.orr@gmail.com            uaddr, op, val);
3219112Smarc.orr@gmail.com
3229238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3239112Smarc.orr@gmail.com
3249112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3259112Smarc.orr@gmail.com        if (timeout != 0) {
3269112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3279112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3289112Smarc.orr@gmail.com        }
3299112Smarc.orr@gmail.com
3309112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3319112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3329112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3339112Smarc.orr@gmail.com        delete buf;
3349112Smarc.orr@gmail.com
3359112Smarc.orr@gmail.com        if(val != mem_val) {
3369112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3379112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3389112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3399112Smarc.orr@gmail.com        }
3409112Smarc.orr@gmail.com
3419112Smarc.orr@gmail.com        // Queue the thread context
3429112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3439112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3449112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3459112Smarc.orr@gmail.com        } else {
3469112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3479112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3489112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3499112Smarc.orr@gmail.com        }
3509112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3519112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3529112Smarc.orr@gmail.com                                "thread context\n");
3539112Smarc.orr@gmail.com        tc->suspend();
3549112Smarc.orr@gmail.com        return 0;
3559112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3569112Smarc.orr@gmail.com        int wokenUp = 0;
3579112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3589112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3599112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3609112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3619112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3629112Smarc.orr@gmail.com                tcWaitList->pop_front();
3639112Smarc.orr@gmail.com                wokenUp++;
3649112Smarc.orr@gmail.com            }
3659112Smarc.orr@gmail.com            if(tcWaitList->empty()) {
3669112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3679112Smarc.orr@gmail.com                delete tcWaitList;
3689112Smarc.orr@gmail.com            }
3699112Smarc.orr@gmail.com        }
3709112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3719112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3729112Smarc.orr@gmail.com        return wokenUp;
3739112Smarc.orr@gmail.com    } else {
3749238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3759112Smarc.orr@gmail.com        return 0;
3769112Smarc.orr@gmail.com    }
3779112Smarc.orr@gmail.com
3789112Smarc.orr@gmail.com}
3799112Smarc.orr@gmail.com
3802238SN/A
3812238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3822238SN/A/// returning a second value in a register other than the normal return register
3832238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3843114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3852238SN/A
3862238SN/A/// Target getpidPseudo() handler.
3872238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3883114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3892238SN/A
3902238SN/A/// Target getuidPseudo() handler.
3912238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3923114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3932238SN/A
3942238SN/A/// Target getgidPseudo() handler.
3952238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3963114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3972238SN/A
3982238SN/A
3991354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4001354SN/Aconst int one_million = 1000000;
40110796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
40210796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4031354SN/A
4041354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4051354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4061354SN/A/// real-world time) to keep simulations repeatable.
4071354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4081354SN/A
4091354SN/A/// Helper function to convert current elapsed time to seconds and
4101354SN/A/// microseconds.
4111354SN/Atemplate <class T1, class T2>
4121354SN/Avoid
41310796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4141354SN/A{
41510796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4161354SN/A    sec = elapsed_usecs / one_million;
4171354SN/A    usec = elapsed_usecs % one_million;
4181354SN/A}
4191354SN/A
42010796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
42110796Sbrandon.potter@amd.com/// nanoseconds.
42210796Sbrandon.potter@amd.comtemplate <class T1, class T2>
42310796Sbrandon.potter@amd.comvoid
42410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
42510796Sbrandon.potter@amd.com{
42610796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
42710796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
42810796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
42910796Sbrandon.potter@amd.com}
43010796Sbrandon.potter@amd.com
431360SN/A//////////////////////////////////////////////////////////////////////
432360SN/A//
433360SN/A// The following emulation functions are generic, but need to be
434360SN/A// templated to account for differences in types, constants, etc.
435360SN/A//
436360SN/A//////////////////////////////////////////////////////////////////////
437360SN/A
4383113Sgblack@eecs.umich.edu#if NO_STAT64
4393113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4403113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4413113Sgblack@eecs.umich.edu#else
4423113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4433113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4443113Sgblack@eecs.umich.edu#endif
4453113Sgblack@eecs.umich.edu
4463113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4473113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4483113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4493113Sgblack@eecs.umich.edu
4503113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4513113Sgblack@eecs.umich.edustatic void
4523113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4533113Sgblack@eecs.umich.edu{
4544189Sgblack@eecs.umich.edu    using namespace TheISA;
4554189Sgblack@eecs.umich.edu
4563113Sgblack@eecs.umich.edu    if (fakeTTY)
4573113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4583113Sgblack@eecs.umich.edu    else
4593113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4608737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4613113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4628737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4633277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4645515SMichael.Adler@intel.com    if (fakeTTY) {
4655515SMichael.Adler@intel.com        // Claim to be a character device
4665515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4675515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4685515SMichael.Adler@intel.com    }
4698737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4703277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4718737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4723277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4738737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4743277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4758737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4763113Sgblack@eecs.umich.edu    if (fakeTTY)
4773113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4783113Sgblack@eecs.umich.edu    else
4793113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4808737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4813113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4828737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4833114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4848737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4853114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4868737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4873114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4888737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4894061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4904061Sgblack@eecs.umich.edu    // consistently across different hosts.
4914061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4928737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4933113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4948737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4953113Sgblack@eecs.umich.edu}
4963113Sgblack@eecs.umich.edu
4973113Sgblack@eecs.umich.edu// Same for stat64
4983113Sgblack@eecs.umich.edu
4993113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5003113Sgblack@eecs.umich.edustatic void
5013113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5023113Sgblack@eecs.umich.edu{
5034189Sgblack@eecs.umich.edu    using namespace TheISA;
5044189Sgblack@eecs.umich.edu
5053113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5063113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5073113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5088737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5093113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5108737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5113113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5128737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5133113Sgblack@eecs.umich.edu#else
5143113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5153113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5163113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5173113Sgblack@eecs.umich.edu#endif
5183113Sgblack@eecs.umich.edu}
5193113Sgblack@eecs.umich.edu
5203113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5213113Sgblack@eecs.umich.edutemplate<class OS>
5223113Sgblack@eecs.umich.edustatic void
5238852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5243113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5253113Sgblack@eecs.umich.edu{
5263113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5273113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5283113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5293113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5303113Sgblack@eecs.umich.edu}
5313113Sgblack@eecs.umich.edu
5323113Sgblack@eecs.umich.edutemplate<class OS>
5333113Sgblack@eecs.umich.edustatic void
5348852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5353113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5363113Sgblack@eecs.umich.edu{
5373113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5383113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5396686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5403113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5413113Sgblack@eecs.umich.edu}
5423113Sgblack@eecs.umich.edu
543378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
544378SN/A/// only to find out if their stdout is a tty, to determine whether to
5459141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5469141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
547360SN/Atemplate <class OS>
5481450SN/ASyscallReturn
5493114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5502680Sktlim@umich.edu          ThreadContext *tc)
551360SN/A{
5526701Sgblack@eecs.umich.edu    int index = 0;
5536701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
5546701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
555360SN/A
5561969SN/A    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", fd, req);
557360SN/A
55810496Ssteve.reinhardt@amd.com    Process::FdMap *fdObj = process->sim_fd_obj(fd);
55910496Ssteve.reinhardt@amd.com
56010496Ssteve.reinhardt@amd.com    if (fdObj == NULL) {
561360SN/A        // doesn't map to any simulator fd: not a valid target fd
5621458SN/A        return -EBADF;
563360SN/A    }
564360SN/A
56510496Ssteve.reinhardt@amd.com    if (fdObj->driver != NULL) {
56610496Ssteve.reinhardt@amd.com        return fdObj->driver->ioctl(process, tc, req);
56710496Ssteve.reinhardt@amd.com    }
56810496Ssteve.reinhardt@amd.com
5699141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5701458SN/A        return -ENOTTY;
5719141Smarc.orr@gmail.com    }
572360SN/A
5739141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
5749141Smarc.orr@gmail.com         fd, req, tc->pcState());
5759141Smarc.orr@gmail.com    return -ENOTTY;
576360SN/A}
577360SN/A
578360SN/Atemplate <class OS>
57910027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5803114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
58110027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
582360SN/A{
583360SN/A    std::string path;
584360SN/A
5858852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5866701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5871458SN/A        return -EFAULT;
588360SN/A
5896701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5906701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
591360SN/A    int hostFlags = 0;
592360SN/A
593360SN/A    // translate open flags
594360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
595360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
596360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
597360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
598360SN/A        }
599360SN/A    }
600360SN/A
601360SN/A    // any target flags left?
602360SN/A    if (tgtFlags != 0)
6031706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
604360SN/A
605360SN/A#ifdef __CYGWIN32__
606360SN/A    hostFlags |= O_BINARY;
607360SN/A#endif
608360SN/A
6093669Sbinkertn@umich.edu    // Adjust path for current working directory
6103669Sbinkertn@umich.edu    path = process->fullPath(path);
6113669Sbinkertn@umich.edu
6121706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6131706SN/A
61410496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
61510496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
61610496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
61710496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
61810496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
61910496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
62010496Ssteve.reinhardt@amd.com            return -ENOENT;
62110496Ssteve.reinhardt@amd.com        }
62210496Ssteve.reinhardt@amd.com
62310496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
62410496Ssteve.reinhardt@amd.com        if (drv != NULL) {
62510496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
62610496Ssteve.reinhardt@amd.com            // process if necessary.
62710496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
62810496Ssteve.reinhardt@amd.com        }
62910496Ssteve.reinhardt@amd.com
63010496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
63110496Ssteve.reinhardt@amd.com        // /dev/zero
63210496Ssteve.reinhardt@amd.com    }
63310496Ssteve.reinhardt@amd.com
6345795Ssaidi@eecs.umich.edu    int fd;
6359143Ssteve.reinhardt@amd.com    int local_errno;
6369142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6379142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6389143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6395795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6409143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6415795Ssaidi@eecs.umich.edu     } else {
6425795Ssaidi@eecs.umich.edu        // open the file
6435795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6449143Ssteve.reinhardt@amd.com        local_errno = errno;
6455795Ssaidi@eecs.umich.edu     }
646360SN/A
6479143Ssteve.reinhardt@amd.com    if (fd == -1)
6489143Ssteve.reinhardt@amd.com        return -local_errno;
6499143Ssteve.reinhardt@amd.com
6509143Ssteve.reinhardt@amd.com    return process->alloc_fd(fd, path.c_str(), hostFlags, mode, false);
651360SN/A}
652360SN/A
65310027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
65410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
65510027SChris.Adeniyi-Jones@arm.comSyscallReturn
65610027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
65710027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
65810027SChris.Adeniyi-Jones@arm.com{
65910027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
66010027SChris.Adeniyi-Jones@arm.com}
66110027SChris.Adeniyi-Jones@arm.com
66210027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
66310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
66410027SChris.Adeniyi-Jones@arm.comSyscallReturn
66510027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66610027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
66710027SChris.Adeniyi-Jones@arm.com{
66810027SChris.Adeniyi-Jones@arm.com    int index = 0;
66910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
67010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
67110027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
67210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
67310027SChris.Adeniyi-Jones@arm.com}
67410027SChris.Adeniyi-Jones@arm.com
67510633Smichaelupton@gmail.com/// Target unlinkat() handler.
67610633Smichaelupton@gmail.comtemplate <class OS>
67710633Smichaelupton@gmail.comSyscallReturn
67810633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67910633Smichaelupton@gmail.com             ThreadContext *tc)
68010633Smichaelupton@gmail.com{
68110633Smichaelupton@gmail.com    int index = 0;
68210633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
68310633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
68410633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
68510633Smichaelupton@gmail.com
68610633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
68710633Smichaelupton@gmail.com}
68810633Smichaelupton@gmail.com
68910203SAli.Saidi@ARM.com/// Target facessat() handler
69010203SAli.Saidi@ARM.comtemplate <class OS>
69110203SAli.Saidi@ARM.comSyscallReturn
69210203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69310203SAli.Saidi@ARM.com        ThreadContext *tc)
69410203SAli.Saidi@ARM.com{
69510203SAli.Saidi@ARM.com    int index = 0;
69610203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
69710203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
69810203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
69910203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
70010203SAli.Saidi@ARM.com}
70110203SAli.Saidi@ARM.com
70210203SAli.Saidi@ARM.com/// Target readlinkat() handler
70310203SAli.Saidi@ARM.comtemplate <class OS>
70410203SAli.Saidi@ARM.comSyscallReturn
70510203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70610203SAli.Saidi@ARM.com        ThreadContext *tc)
70710203SAli.Saidi@ARM.com{
70810203SAli.Saidi@ARM.com    int index = 0;
70910203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71010203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71110203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
71210203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
71310203SAli.Saidi@ARM.com}
71410203SAli.Saidi@ARM.com
7156640Svince@csl.cornell.edu/// Target sysinfo() handler.
7166640Svince@csl.cornell.edutemplate <class OS>
7176640Svince@csl.cornell.eduSyscallReturn
7186640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7196640Svince@csl.cornell.edu         ThreadContext *tc)
7206640Svince@csl.cornell.edu{
7216640Svince@csl.cornell.edu
7226701Sgblack@eecs.umich.edu    int index = 0;
7236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
72410793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7256640Svince@csl.cornell.edu
7266701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7276701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7286640Svince@csl.cornell.edu
7298706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7306640Svince@csl.cornell.edu
7316701Sgblack@eecs.umich.edu    return 0;
7326640Svince@csl.cornell.edu}
733360SN/A
7341999SN/A/// Target chmod() handler.
7351999SN/Atemplate <class OS>
7361999SN/ASyscallReturn
7373114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7382680Sktlim@umich.edu          ThreadContext *tc)
7391999SN/A{
7401999SN/A    std::string path;
7411999SN/A
7426701Sgblack@eecs.umich.edu    int index = 0;
7438852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7446701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7451999SN/A        return -EFAULT;
7466701Sgblack@eecs.umich.edu    }
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::something???
7521999SN/A    hostMode = mode;
7531999SN/A
7543669Sbinkertn@umich.edu    // Adjust path for current working directory
7553669Sbinkertn@umich.edu    path = process->fullPath(path);
7563669Sbinkertn@umich.edu
7571999SN/A    // do the chmod
7581999SN/A    int result = chmod(path.c_str(), hostMode);
7591999SN/A    if (result < 0)
7602218SN/A        return -errno;
7611999SN/A
7621999SN/A    return 0;
7631999SN/A}
7641999SN/A
7651999SN/A
7661999SN/A/// Target fchmod() handler.
7671999SN/Atemplate <class OS>
7681999SN/ASyscallReturn
7693114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7702680Sktlim@umich.edu           ThreadContext *tc)
7711999SN/A{
7726701Sgblack@eecs.umich.edu    int index = 0;
7736701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
7741999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
7751999SN/A        // doesn't map to any simulator fd: not a valid target fd
7761999SN/A        return -EBADF;
7771999SN/A    }
7781999SN/A
7796701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7801999SN/A    mode_t hostMode = 0;
7811999SN/A
7821999SN/A    // XXX translate mode flags via OS::someting???
7831999SN/A    hostMode = mode;
7841999SN/A
7851999SN/A    // do the fchmod
7861999SN/A    int result = fchmod(process->sim_fd(fd), hostMode);
7871999SN/A    if (result < 0)
7882218SN/A        return -errno;
7891999SN/A
7901999SN/A    return 0;
7911999SN/A}
7921999SN/A
7935877Shsul@eecs.umich.edu/// Target mremap() handler.
7945877Shsul@eecs.umich.edutemplate <class OS>
7955877Shsul@eecs.umich.eduSyscallReturn
7965877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
7975877Shsul@eecs.umich.edu{
7986701Sgblack@eecs.umich.edu    int index = 0;
7996701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8006701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8016701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8026701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
80310027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
80410027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
80510027SChris.Adeniyi-Jones@arm.com
80610027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
80710027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8085877Shsul@eecs.umich.edu
80910318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
81010318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8115877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8125877Shsul@eecs.umich.edu        return -EINVAL;
8135877Shsul@eecs.umich.edu    }
8145877Shsul@eecs.umich.edu
81510486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
81610486Stjablin@gmail.com
8175877Shsul@eecs.umich.edu    if (new_length > old_length) {
81810027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
81910027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8205877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8218601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8225877Shsul@eecs.umich.edu            process->mmap_end += diff;
8235877Shsul@eecs.umich.edu            return start;
8245877Shsul@eecs.umich.edu        } else {
82510027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8265877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8275877Shsul@eecs.umich.edu                return -ENOMEM;
8285877Shsul@eecs.umich.edu            } else {
82910027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
83010027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
83110027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
83210027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
83310027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
83410027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8355877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
83610027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
83710027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
83810027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
83910027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
84010027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
84110027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
84210027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
84310027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
84410027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
84510027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
84610027SChris.Adeniyi-Jones@arm.com                }
84710027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
84810027SChris.Adeniyi-Jones@arm.com                return new_start;
8495877Shsul@eecs.umich.edu            }
8505877Shsul@eecs.umich.edu        }
8515877Shsul@eecs.umich.edu    } else {
85210027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
85310027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8548601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
85510027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8565877Shsul@eecs.umich.edu    }
8575877Shsul@eecs.umich.edu}
8581999SN/A
859378SN/A/// Target stat() handler.
860360SN/Atemplate <class OS>
8611450SN/ASyscallReturn
8623114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8632680Sktlim@umich.edu         ThreadContext *tc)
864360SN/A{
865360SN/A    std::string path;
866360SN/A
8676701Sgblack@eecs.umich.edu    int index = 0;
8688852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8696701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8706701Sgblack@eecs.umich.edu        return -EFAULT;
8716701Sgblack@eecs.umich.edu    }
8726701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
873360SN/A
8743669Sbinkertn@umich.edu    // Adjust path for current working directory
8753669Sbinkertn@umich.edu    path = process->fullPath(path);
8763669Sbinkertn@umich.edu
877360SN/A    struct stat hostBuf;
878360SN/A    int result = stat(path.c_str(), &hostBuf);
879360SN/A
880360SN/A    if (result < 0)
8812218SN/A        return -errno;
882360SN/A
8838706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
884360SN/A
8851458SN/A    return 0;
886360SN/A}
887360SN/A
888360SN/A
8895074Ssaidi@eecs.umich.edu/// Target stat64() handler.
8905074Ssaidi@eecs.umich.edutemplate <class OS>
8915074Ssaidi@eecs.umich.eduSyscallReturn
8925074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
8935074Ssaidi@eecs.umich.edu           ThreadContext *tc)
8945074Ssaidi@eecs.umich.edu{
8955074Ssaidi@eecs.umich.edu    std::string path;
8965074Ssaidi@eecs.umich.edu
8976701Sgblack@eecs.umich.edu    int index = 0;
8988852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8996701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9005074Ssaidi@eecs.umich.edu        return -EFAULT;
9016701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9025074Ssaidi@eecs.umich.edu
9035074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9045074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9055074Ssaidi@eecs.umich.edu
9065208Ssaidi@eecs.umich.edu#if NO_STAT64
9075208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9085208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9095208Ssaidi@eecs.umich.edu#else
9105074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9115074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9125208Ssaidi@eecs.umich.edu#endif
9135074Ssaidi@eecs.umich.edu
9145074Ssaidi@eecs.umich.edu    if (result < 0)
9155074Ssaidi@eecs.umich.edu        return -errno;
9165074Ssaidi@eecs.umich.edu
9178706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9185074Ssaidi@eecs.umich.edu
9195074Ssaidi@eecs.umich.edu    return 0;
9205074Ssaidi@eecs.umich.edu}
9215074Ssaidi@eecs.umich.edu
9225074Ssaidi@eecs.umich.edu
92310027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
92410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
92510027SChris.Adeniyi-Jones@arm.comSyscallReturn
92610027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
92710027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
92810027SChris.Adeniyi-Jones@arm.com{
92910027SChris.Adeniyi-Jones@arm.com    int index = 0;
93010027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
93110027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
93210793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
93310027SChris.Adeniyi-Jones@arm.com
93410027SChris.Adeniyi-Jones@arm.com    std::string path;
93510027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
93610027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
93710027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
93810027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
93910027SChris.Adeniyi-Jones@arm.com
94010027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
94110027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
94210027SChris.Adeniyi-Jones@arm.com
94310027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
94410027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
94510027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
94610027SChris.Adeniyi-Jones@arm.com#else
94710027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
94810027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
94910027SChris.Adeniyi-Jones@arm.com#endif
95010027SChris.Adeniyi-Jones@arm.com
95110027SChris.Adeniyi-Jones@arm.com    if (result < 0)
95210027SChris.Adeniyi-Jones@arm.com        return -errno;
95310027SChris.Adeniyi-Jones@arm.com
95410027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
95510027SChris.Adeniyi-Jones@arm.com
95610027SChris.Adeniyi-Jones@arm.com    return 0;
95710027SChris.Adeniyi-Jones@arm.com}
95810027SChris.Adeniyi-Jones@arm.com
95910027SChris.Adeniyi-Jones@arm.com
9601999SN/A/// Target fstat64() handler.
9611999SN/Atemplate <class OS>
9621999SN/ASyscallReturn
9633114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9642680Sktlim@umich.edu            ThreadContext *tc)
9651999SN/A{
9666701Sgblack@eecs.umich.edu    int index = 0;
9676701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
9686701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9691999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
9701999SN/A        // doesn't map to any simulator fd: not a valid target fd
9711999SN/A        return -EBADF;
9721999SN/A    }
9731999SN/A
9742764Sstever@eecs.umich.edu#if NO_STAT64
9752064SN/A    struct stat  hostBuf;
9762064SN/A    int result = fstat(process->sim_fd(fd), &hostBuf);
9772064SN/A#else
9782064SN/A    struct stat64  hostBuf;
9791999SN/A    int result = fstat64(process->sim_fd(fd), &hostBuf);
9802064SN/A#endif
9811999SN/A
9821999SN/A    if (result < 0)
9832218SN/A        return -errno;
9841999SN/A
9858706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
9861999SN/A
9871999SN/A    return 0;
9881999SN/A}
9891999SN/A
9901999SN/A
991378SN/A/// Target lstat() handler.
992360SN/Atemplate <class OS>
9931450SN/ASyscallReturn
9943114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9952680Sktlim@umich.edu          ThreadContext *tc)
996360SN/A{
997360SN/A    std::string path;
998360SN/A
9996701Sgblack@eecs.umich.edu    int index = 0;
10008852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10016701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10026701Sgblack@eecs.umich.edu        return -EFAULT;
10036701Sgblack@eecs.umich.edu    }
10046701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1005360SN/A
10063669Sbinkertn@umich.edu    // Adjust path for current working directory
10073669Sbinkertn@umich.edu    path = process->fullPath(path);
10083669Sbinkertn@umich.edu
1009360SN/A    struct stat hostBuf;
1010360SN/A    int result = lstat(path.c_str(), &hostBuf);
1011360SN/A
1012360SN/A    if (result < 0)
10131458SN/A        return -errno;
1014360SN/A
10158706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1016360SN/A
10171458SN/A    return 0;
1018360SN/A}
1019360SN/A
10201999SN/A/// Target lstat64() handler.
10211999SN/Atemplate <class OS>
10221999SN/ASyscallReturn
10233114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10242680Sktlim@umich.edu            ThreadContext *tc)
10251999SN/A{
10261999SN/A    std::string path;
10271999SN/A
10286701Sgblack@eecs.umich.edu    int index = 0;
10298852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10306701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10316701Sgblack@eecs.umich.edu        return -EFAULT;
10326701Sgblack@eecs.umich.edu    }
10336701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10341999SN/A
10353669Sbinkertn@umich.edu    // Adjust path for current working directory
10363669Sbinkertn@umich.edu    path = process->fullPath(path);
10373669Sbinkertn@umich.edu
10382764Sstever@eecs.umich.edu#if NO_STAT64
10392064SN/A    struct stat hostBuf;
10402064SN/A    int result = lstat(path.c_str(), &hostBuf);
10412064SN/A#else
10421999SN/A    struct stat64 hostBuf;
10431999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10442064SN/A#endif
10451999SN/A
10461999SN/A    if (result < 0)
10471999SN/A        return -errno;
10481999SN/A
10498706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10501999SN/A
10511999SN/A    return 0;
10521999SN/A}
10531999SN/A
1054378SN/A/// Target fstat() handler.
1055360SN/Atemplate <class OS>
10561450SN/ASyscallReturn
10573114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10582680Sktlim@umich.edu          ThreadContext *tc)
1059360SN/A{
10606701Sgblack@eecs.umich.edu    int index = 0;
10616701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
10626701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1063360SN/A
10641969SN/A    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", fd);
1065360SN/A
1066360SN/A    if (fd < 0)
10671458SN/A        return -EBADF;
1068360SN/A
1069360SN/A    struct stat hostBuf;
1070360SN/A    int result = fstat(fd, &hostBuf);
1071360SN/A
1072360SN/A    if (result < 0)
10731458SN/A        return -errno;
1074360SN/A
10758706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (fd == 1));
10762021SN/A
10771458SN/A    return 0;
1078360SN/A}
1079360SN/A
1080360SN/A
10811706SN/A/// Target statfs() handler.
10821706SN/Atemplate <class OS>
10831706SN/ASyscallReturn
10843114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10852680Sktlim@umich.edu           ThreadContext *tc)
10861706SN/A{
10871706SN/A    std::string path;
10881706SN/A
10896701Sgblack@eecs.umich.edu    int index = 0;
10908852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10916701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10926701Sgblack@eecs.umich.edu        return -EFAULT;
10936701Sgblack@eecs.umich.edu    }
10946701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10951706SN/A
10963669Sbinkertn@umich.edu    // Adjust path for current working directory
10973669Sbinkertn@umich.edu    path = process->fullPath(path);
10983669Sbinkertn@umich.edu
10991706SN/A    struct statfs hostBuf;
11001706SN/A    int result = statfs(path.c_str(), &hostBuf);
11011706SN/A
11021706SN/A    if (result < 0)
11032218SN/A        return -errno;
11041706SN/A
11058706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11061706SN/A
11071706SN/A    return 0;
11081706SN/A}
11091706SN/A
11101706SN/A
11111706SN/A/// Target fstatfs() handler.
11121706SN/Atemplate <class OS>
11131706SN/ASyscallReturn
11143114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11152680Sktlim@umich.edu            ThreadContext *tc)
11161706SN/A{
11176701Sgblack@eecs.umich.edu    int index = 0;
11186701Sgblack@eecs.umich.edu    int fd = process->sim_fd(process->getSyscallArg(tc, index));
11196701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11201706SN/A
11211706SN/A    if (fd < 0)
11221706SN/A        return -EBADF;
11231706SN/A
11241706SN/A    struct statfs hostBuf;
11251706SN/A    int result = fstatfs(fd, &hostBuf);
11261706SN/A
11271706SN/A    if (result < 0)
11282218SN/A        return -errno;
11291706SN/A
11308706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11311706SN/A
11321706SN/A    return 0;
11331706SN/A}
11341706SN/A
11351706SN/A
11361999SN/A/// Target writev() handler.
11371999SN/Atemplate <class OS>
11381999SN/ASyscallReturn
11393114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11402680Sktlim@umich.edu           ThreadContext *tc)
11411999SN/A{
11426701Sgblack@eecs.umich.edu    int index = 0;
11436701Sgblack@eecs.umich.edu    int fd = process->getSyscallArg(tc, index);
11441999SN/A    if (fd < 0 || process->sim_fd(fd) < 0) {
11451999SN/A        // doesn't map to any simulator fd: not a valid target fd
11461999SN/A        return -EBADF;
11471999SN/A    }
11481999SN/A
11498852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11506701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11516701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11521999SN/A    struct iovec hiov[count];
11536227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11541999SN/A        typename OS::tgt_iovec tiov;
11552461SN/A
11568852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11578852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11588737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
11591999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
11608852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
11618852Sandreas.hansson@arm.com                   hiov[i].iov_len);
11621999SN/A    }
11631999SN/A
11641999SN/A    int result = writev(process->sim_fd(fd), hiov, count);
11651999SN/A
11666227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
11671999SN/A        delete [] (char *)hiov[i].iov_base;
11681999SN/A
11691999SN/A    if (result < 0)
11702218SN/A        return -errno;
11711999SN/A
117210629Sjthestness@gmail.com    return result;
11731999SN/A}
11741999SN/A
11751999SN/A
1176378SN/A/// Target mmap() handler.
1177378SN/A///
1178378SN/A/// We don't really handle mmap().  If the target is mmaping an
1179378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1180378SN/A/// nothing (since memory is initialized to zero and the simulator
11818324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
11828324Ssteve.reinhardt@amd.com///
1183360SN/Atemplate <class OS>
11841450SN/ASyscallReturn
11853114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1186360SN/A{
11876701Sgblack@eecs.umich.edu    int index = 0;
11886701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
11896701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
11906701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
11916701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
11928324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
119310486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1194360SN/A
11959008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
11969008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
11979008Sgblack@eecs.umich.edu
11988324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
11998324Ssteve.reinhardt@amd.com        Process::FdMap *fd_map = p->sim_fd_obj(tgt_fd);
12008324Ssteve.reinhardt@amd.com        if (!fd_map || fd_map->fd < 0) {
12018324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12028324Ssteve.reinhardt@amd.com            return -EBADF;
12038324Ssteve.reinhardt@amd.com        }
12048324Ssteve.reinhardt@amd.com
12058324Ssteve.reinhardt@amd.com        if (fd_map->filename != "/dev/zero") {
12068324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12078324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12088324Ssteve.reinhardt@amd.com            // another name on some platform
12098324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
12108324Ssteve.reinhardt@amd.com                 " other than /dev/zero\n", fd_map->filename);
12118324Ssteve.reinhardt@amd.com        }
12128324Ssteve.reinhardt@amd.com    }
12135877Shsul@eecs.umich.edu
121410486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
121510486Stjablin@gmail.com
121610318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
121710486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12182544SN/A        warn("mmap failing: arguments not page-aligned: "
121910486Stjablin@gmail.com             "start 0x%x offset 0x%x",
122010486Stjablin@gmail.com             start, offset);
12212544SN/A        return -EINVAL;
1222360SN/A    }
1223360SN/A
12248600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12258600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12268600Ssteve.reinhardt@amd.com    bool clobber = false;
12278600Ssteve.reinhardt@amd.com
12288600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12298600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12308600Ssteve.reinhardt@amd.com
12318600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12328600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12338600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12348600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12358600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12368600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12378600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
123810485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
123910485SMichael.Adler@intel.com                return -EINVAL;
12408600Ssteve.reinhardt@amd.com            } else {
12418600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12428600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12438600Ssteve.reinhardt@amd.com                use_provided_address = false;
12448600Ssteve.reinhardt@amd.com            }
12458600Ssteve.reinhardt@amd.com        }
12462544SN/A    }
12472544SN/A
12488600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12498600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12508600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12518600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12528600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12538600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12548600Ssteve.reinhardt@amd.com        } else {
12558600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12568600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12578600Ssteve.reinhardt@amd.com        }
12586672Sgblack@eecs.umich.edu    }
12598600Ssteve.reinhardt@amd.com
12608601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
12612544SN/A
12621458SN/A    return start;
1263360SN/A}
1264360SN/A
1265378SN/A/// Target getrlimit() handler.
1266360SN/Atemplate <class OS>
12671450SN/ASyscallReturn
12683114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12692680Sktlim@umich.edu        ThreadContext *tc)
1270360SN/A{
12716701Sgblack@eecs.umich.edu    int index = 0;
12726701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
12736701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1274360SN/A
1275360SN/A    switch (resource) {
12762064SN/A        case OS::TGT_RLIMIT_STACK:
12775877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
12782064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
12798737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12808737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12812064SN/A            break;
1282360SN/A
12835877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
12845877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
12855877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
12868737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
12878737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
12885877Shsul@eecs.umich.edu            break;
12895877Shsul@eecs.umich.edu
12902064SN/A        default:
129110794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
129210794Sbrandon.potter@amd.com            return -EINVAL;
12932064SN/A            break;
1294360SN/A    }
1295360SN/A
12968706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
12971458SN/A    return 0;
1298360SN/A}
1299360SN/A
130010796Sbrandon.potter@amd.com/// Target clock_gettime() function.
130110796Sbrandon.potter@amd.comtemplate <class OS>
130210796Sbrandon.potter@amd.comSyscallReturn
130310796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
130410796Sbrandon.potter@amd.com{
130510796Sbrandon.potter@amd.com    int index = 1;
130610796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
130710796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
130810796Sbrandon.potter@amd.com
130910796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
131010796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
131110796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
131210796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
131310796Sbrandon.potter@amd.com
131410796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
131510796Sbrandon.potter@amd.com
131610796Sbrandon.potter@amd.com    return 0;
131710796Sbrandon.potter@amd.com}
131810796Sbrandon.potter@amd.com
1319378SN/A/// Target gettimeofday() handler.
1320360SN/Atemplate <class OS>
13211450SN/ASyscallReturn
13223114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13232680Sktlim@umich.edu        ThreadContext *tc)
1324360SN/A{
13256701Sgblack@eecs.umich.edu    int index = 0;
13266701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1327360SN/A
132810796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1329360SN/A    tp->tv_sec += seconds_since_epoch;
13306109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13316109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1332360SN/A
13338706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1334360SN/A
13351458SN/A    return 0;
1336360SN/A}
1337360SN/A
1338360SN/A
13391999SN/A/// Target utimes() handler.
13401999SN/Atemplate <class OS>
13411999SN/ASyscallReturn
13423114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13432680Sktlim@umich.edu           ThreadContext *tc)
13441999SN/A{
13451999SN/A    std::string path;
13461999SN/A
13476701Sgblack@eecs.umich.edu    int index = 0;
13488852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13496701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13506701Sgblack@eecs.umich.edu        return -EFAULT;
13516701Sgblack@eecs.umich.edu    }
13521999SN/A
13536701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13546701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13558706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13561999SN/A
13571999SN/A    struct timeval hostTimeval[2];
13581999SN/A    for (int i = 0; i < 2; ++i)
13591999SN/A    {
13608737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
13618737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
13621999SN/A    }
13633669Sbinkertn@umich.edu
13643669Sbinkertn@umich.edu    // Adjust path for current working directory
13653669Sbinkertn@umich.edu    path = process->fullPath(path);
13663669Sbinkertn@umich.edu
13671999SN/A    int result = utimes(path.c_str(), hostTimeval);
13681999SN/A
13691999SN/A    if (result < 0)
13701999SN/A        return -errno;
13711999SN/A
13721999SN/A    return 0;
13731999SN/A}
1374378SN/A/// Target getrusage() function.
1375360SN/Atemplate <class OS>
13761450SN/ASyscallReturn
13773114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13782680Sktlim@umich.edu              ThreadContext *tc)
1379360SN/A{
13806701Sgblack@eecs.umich.edu    int index = 0;
13816701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
13826701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1383360SN/A
13843670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
13853670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1386360SN/A    rup->ru_stime.tv_sec = 0;
1387360SN/A    rup->ru_stime.tv_usec = 0;
1388360SN/A    rup->ru_maxrss = 0;
1389360SN/A    rup->ru_ixrss = 0;
1390360SN/A    rup->ru_idrss = 0;
1391360SN/A    rup->ru_isrss = 0;
1392360SN/A    rup->ru_minflt = 0;
1393360SN/A    rup->ru_majflt = 0;
1394360SN/A    rup->ru_nswap = 0;
1395360SN/A    rup->ru_inblock = 0;
1396360SN/A    rup->ru_oublock = 0;
1397360SN/A    rup->ru_msgsnd = 0;
1398360SN/A    rup->ru_msgrcv = 0;
1399360SN/A    rup->ru_nsignals = 0;
1400360SN/A    rup->ru_nvcsw = 0;
1401360SN/A    rup->ru_nivcsw = 0;
1402360SN/A
14033670Sbinkertn@umich.edu    switch (who) {
14043670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
140510796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14068737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14078737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14083670Sbinkertn@umich.edu        break;
14093670Sbinkertn@umich.edu
14103670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14113670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14123670Sbinkertn@umich.edu        break;
14133670Sbinkertn@umich.edu
14143670Sbinkertn@umich.edu      default:
14153670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14163670Sbinkertn@umich.edu        // plow ahead
14173670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14183670Sbinkertn@umich.edu             who);
14193670Sbinkertn@umich.edu    }
14203670Sbinkertn@umich.edu
14218706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1422360SN/A
14231458SN/A    return 0;
1424360SN/A}
1425360SN/A
14266683Stjones1@inf.ed.ac.uk/// Target times() function.
14276683Stjones1@inf.ed.ac.uktemplate <class OS>
14286683Stjones1@inf.ed.ac.ukSyscallReturn
14296683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14306683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14316683Stjones1@inf.ed.ac.uk{
14326701Sgblack@eecs.umich.edu    int index = 0;
14336701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14346683Stjones1@inf.ed.ac.uk
14356683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14367823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14376683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14386683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14396683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14406683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14416683Stjones1@inf.ed.ac.uk
14426683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14438737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14446683Stjones1@inf.ed.ac.uk
14456683Stjones1@inf.ed.ac.uk    // Write back
14468706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14476683Stjones1@inf.ed.ac.uk
14486683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14496683Stjones1@inf.ed.ac.uk    return clocks;
14506683Stjones1@inf.ed.ac.uk}
14512553SN/A
14526684Stjones1@inf.ed.ac.uk/// Target time() function.
14536684Stjones1@inf.ed.ac.uktemplate <class OS>
14546684Stjones1@inf.ed.ac.ukSyscallReturn
14556684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14566684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14576684Stjones1@inf.ed.ac.uk{
14586684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
145910796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
14606684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
14616684Stjones1@inf.ed.ac.uk
14626701Sgblack@eecs.umich.edu    int index = 0;
14636701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
14646684Stjones1@inf.ed.ac.uk    if(taddr != 0) {
14656684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
14668737Skoansin.tan@gmail.com        t = TheISA::htog(t);
14678852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
14688852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
14696684Stjones1@inf.ed.ac.uk    }
14706684Stjones1@inf.ed.ac.uk    return sec;
14716684Stjones1@inf.ed.ac.uk}
14722553SN/A
14732553SN/A
14741354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1475