syscall_emul.hh revision 11593
1360SN/A/*
210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 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
5211799Sbrandon.potter@amd.com///
5311799Sbrandon.potter@amd.com/// @file syscall_emul.hh
5411799Sbrandon.potter@amd.com///
5511799Sbrandon.potter@amd.com/// This file defines objects used to emulate syscalls from the target
5611799Sbrandon.potter@amd.com/// application on the host machine.
5711799Sbrandon.potter@amd.com
58360SN/A#ifdef __CYGWIN32__
59360SN/A#include <sys/fcntl.h>  // for O_BINARY
60360SN/A
61360SN/A#endif
62360SN/A#include <fcntl.h>
63360SN/A#include <sys/mman.h>
641809SN/A#include <sys/stat.h>
6511800Sbrandon.potter@amd.com#include <sys/time.h>
6611392Sbrandon.potter@amd.com#include <sys/uio.h>
671809SN/A
6811392Sbrandon.potter@amd.com#include <cerrno>
6911383Sbrandon.potter@amd.com#include <string>
703113Sgblack@eecs.umich.edu
7111799Sbrandon.potter@amd.com#include "base/chunk_generator.hh"
7211759Sbrandon.potter@amd.com#include "base/intmath.hh"      // for RoundUp
7311799Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
748229Snate@binkert.org#include "base/misc.hh"
758229Snate@binkert.org#include "base/trace.hh"
7611594Santhony.gutierrez@amd.com#include "base/types.hh"
777075Snate@binkert.org#include "config/the_isa.hh"
788229Snate@binkert.org#include "cpu/base.hh"
797075Snate@binkert.org#include "cpu/thread_context.hh"
80360SN/A#include "debug/SyscallBase.hh"
8111800Sbrandon.potter@amd.com#include "debug/SyscallVerbose.hh"
8211392Sbrandon.potter@amd.com#include "mem/page_table.hh"
832462SN/A#include "sim/byteswap.hh"
841354SN/A#include "sim/emul_driver.hh"
856216Snate@binkert.org#include "sim/process.hh"
866658Snate@binkert.org#include "sim/syscall_emul_buf.hh"
872474SN/A#include "sim/syscallreturn.hh"
882680Sktlim@umich.edu#include "sim/system.hh"
898229Snate@binkert.org
9010496Ssteve.reinhardt@amd.com// This wrapper macro helps out with readability a bit. FLAGEXT specifies
918229Snate@binkert.org// the verbosity and FMT is the message to be appended to the syscall
9211794Sbrandon.potter@amd.com// header information. The syscall header information contains the cpuid
9310497Ssteve.reinhardt@amd.com// and thread id.
9411794Sbrandon.potter@amd.com#define DPRINTF_SYSCALL(FLAGEXT, FMT, ...)                                  \
95360SN/A    DPRINTFS(Syscall##FLAGEXT, tc->getCpuPtr(), "T%d : syscall " FMT,       \
9611794Sbrandon.potter@amd.com             tc->threadId(), __VA_ARGS__)
97360SN/A
98360SN/A///
99360SN/A/// System call descriptor.
100360SN/A///
101360SN/Aclass SyscallDesc {
102360SN/A
103360SN/A  public:
104360SN/A
105360SN/A    /// Typedef for target syscall handler functions.
106360SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
107378SN/A                           LiveProcess *, ThreadContext *);
1081706SN/A
1093114Sgblack@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
110378SN/A    FuncPtr funcPtr;    //!< Pointer to emulation function.
111378SN/A    int flags;          //!< Flags (see Flags enum).
112378SN/A    bool warned;        //!< Have we warned about unimplemented syscall?
113378SN/A
114378SN/A    /// Flag values for controlling syscall behavior.
1151706SN/A    enum Flags {
1163114Sgblack@eecs.umich.edu        /// Don't set return regs according to funcPtr return value.
117360SN/A        /// Used for syscalls with non-standard return conventions
11811760Sbrandon.potter@amd.com        /// that explicitly set the ThreadContext regs (e.g.,
11911760Sbrandon.potter@amd.com        /// sigreturn).
12011760Sbrandon.potter@amd.com        SuppressReturnValue = 1,
12111760Sbrandon.potter@amd.com        WarnOnce = 2
1226109Ssanchezd@stanford.edu    };
1231706SN/A
1243114Sgblack@eecs.umich.edu    /// Constructor.
125378SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
1266109Ssanchezd@stanford.edu        : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
1276109Ssanchezd@stanford.edu    {
1286109Ssanchezd@stanford.edu    }
1296109Ssanchezd@stanford.edu
130378SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1311706SN/A    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
1323114Sgblack@eecs.umich.edu
133378SN/A    /// Is the WarnOnce flag set?
1345748SSteve.Reinhardt@amd.com    bool warnOnce() const {  return (flags & WarnOnce); }
1355748SSteve.Reinhardt@amd.com};
1365748SSteve.Reinhardt@amd.com
137378SN/A
138378SN/A//////////////////////////////////////////////////////////////////////
1391706SN/A//
1403114Sgblack@eecs.umich.edu// The following emulation functions are generic enough that they
141378SN/A// don't need to be recompiled for different emulated OS's.  They are
142378SN/A// defined in sim/syscall_emul.cc.
1431706SN/A//
1443114Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////
145378SN/A
146378SN/A
1471706SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1483114Sgblack@eecs.umich.eduSyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
149378SN/A                                LiveProcess *p, ThreadContext *tc);
150378SN/A
1511706SN/A/// Handler for unimplemented syscalls that we never intend to
1523114Sgblack@eecs.umich.edu/// implement (signal handling, etc.) and should not affect the correct
153378SN/A/// behavior of the program.  Print a warning only if the appropriate
1544118Sgblack@eecs.umich.edu/// trace flag is enabled.  Return success to the target program.
1554118Sgblack@eecs.umich.eduSyscallReturn ignoreFunc(SyscallDesc *desc, int num,
15611800Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
1574118Sgblack@eecs.umich.edu
158378SN/A/// Target exit() handler: terminate current context.
1591706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1603114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
161378SN/A
162378SN/A/// Target exit_group() handler: terminate simulation. (exit all threads)
1631706SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1643114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
165360SN/A
1665513SMichael.Adler@intel.com/// Target getpagesize() handler.
1675513SMichael.Adler@intel.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1685513SMichael.Adler@intel.com                              LiveProcess *p, ThreadContext *tc);
1695513SMichael.Adler@intel.com
17010203SAli.Saidi@ARM.com/// Target brk() handler: set brk address.
17110203SAli.Saidi@ARM.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
17210203SAli.Saidi@ARM.com                      LiveProcess *p, ThreadContext *tc);
17310203SAli.Saidi@ARM.com
1745513SMichael.Adler@intel.com/// Target close() handler.
1755513SMichael.Adler@intel.comSyscallReturn closeFunc(SyscallDesc *desc, int num,
1765513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
177511SN/A
17810633Smichaelupton@gmail.com/// Target read() handler.
17910633Smichaelupton@gmail.comSyscallReturn readFunc(SyscallDesc *desc, int num,
18010633Smichaelupton@gmail.com                       LiveProcess *p, ThreadContext *tc);
1811706SN/A
1823114Sgblack@eecs.umich.edu/// Target write() handler.
183511SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1845513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
1855513SMichael.Adler@intel.com
1865513SMichael.Adler@intel.com/// Target lseek() handler.
1875513SMichael.Adler@intel.comSyscallReturn lseekFunc(SyscallDesc *desc, int num,
188511SN/A                        LiveProcess *p, ThreadContext *tc);
1891706SN/A
1903114Sgblack@eecs.umich.edu/// Target _llseek() handler.
1911706SN/ASyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1921706SN/A                        LiveProcess *p, ThreadContext *tc);
1931706SN/A
1941706SN/A/// Target munmap() handler.
1953114Sgblack@eecs.umich.eduSyscallReturn munmapFunc(SyscallDesc *desc, int num,
1961706SN/A                         LiveProcess *p, ThreadContext *tc);
1971706SN/A
1981706SN/A/// Target gethostname() handler.
1991706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
2003114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
2011706SN/A
202511SN/A/// Target getcwd() handler.
2036703Svince@csl.cornell.eduSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2046703Svince@csl.cornell.edu                         LiveProcess *p, ThreadContext *tc);
2056703Svince@csl.cornell.edu
2066703Svince@csl.cornell.edu/// Target readlink() handler.
2076685Stjones1@inf.ed.ac.ukSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2086685Stjones1@inf.ed.ac.uk                           LiveProcess *p, ThreadContext *tc,
2096685Stjones1@inf.ed.ac.uk                           int index = 0);
2106685Stjones1@inf.ed.ac.ukSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2116685Stjones1@inf.ed.ac.uk                           LiveProcess *p, ThreadContext *tc);
2125513SMichael.Adler@intel.com
2135513SMichael.Adler@intel.com/// Target unlink() handler.
2145513SMichael.Adler@intel.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
2155513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc,
2165513SMichael.Adler@intel.com                           int index);
2171999SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2181999SN/A                         LiveProcess *p, ThreadContext *tc);
2193114Sgblack@eecs.umich.edu
2201999SN/A/// Target mkdir() handler.
2211999SN/ASyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2221999SN/A                        LiveProcess *p, ThreadContext *tc);
2231999SN/A
2243114Sgblack@eecs.umich.edu/// Target rename() handler.
2251999SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2263079Sstever@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2273079Sstever@eecs.umich.edu
2283114Sgblack@eecs.umich.edu
2293079Sstever@eecs.umich.edu/// Target truncate() handler.
2302093SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2312093SN/A                           LiveProcess *p, ThreadContext *tc);
2323114Sgblack@eecs.umich.edu
2332093SN/A
2342687Sksewell@umich.edu/// Target ftruncate() handler.
2352687Sksewell@umich.eduSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
23611800Sbrandon.potter@amd.com                            LiveProcess *p, ThreadContext *tc);
2372687Sksewell@umich.edu
2382238SN/A
2392238SN/A/// Target truncate64() handler.
24011800Sbrandon.potter@amd.comSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2412238SN/A                             LiveProcess *p, ThreadContext *tc);
2422238SN/A
2432238SN/A/// Target ftruncate64() handler.
24411800Sbrandon.potter@amd.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2452238SN/A                              LiveProcess *p, ThreadContext *tc);
2462238SN/A
2472238SN/A
24811800Sbrandon.potter@amd.com/// Target umask() handler.
2492238SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num,
2502238SN/A                        LiveProcess *p, ThreadContext *tc);
2512238SN/A
25211800Sbrandon.potter@amd.com
2532238SN/A/// Target chown() handler.
2542238SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2552238SN/A                        LiveProcess *p, ThreadContext *tc);
25611800Sbrandon.potter@amd.com
2572238SN/A
2582238SN/A/// Target fchown() handler.
2592238SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
26011800Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
2612238SN/A
2622238SN/A/// Target dup() handler.
2632238SN/ASyscallReturn dupFunc(SyscallDesc *desc, int num,
26411800Sbrandon.potter@amd.com                      LiveProcess *process, ThreadContext *tc);
2652238SN/A
2666109Ssanchezd@stanford.edu/// Target fnctl() handler.
2676109Ssanchezd@stanford.eduSyscallReturn fcntlFunc(SyscallDesc *desc, int num,
26811800Sbrandon.potter@amd.com                        LiveProcess *process, ThreadContext *tc);
2692238SN/A
2709455Smitch.hayenga+gem5@gmail.com/// Target fcntl64() handler.
2719455Smitch.hayenga+gem5@gmail.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2729455Smitch.hayenga+gem5@gmail.com                        LiveProcess *process, ThreadContext *tc);
27310203SAli.Saidi@ARM.com
27410203SAli.Saidi@ARM.com/// Target setuid() handler.
27510203SAli.Saidi@ARM.comSyscallReturn setuidFunc(SyscallDesc *desc, int num,
2769455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
2779112Smarc.orr@gmail.com
2789112Smarc.orr@gmail.com/// Target getpid() handler.
2799112Smarc.orr@gmail.comSyscallReturn getpidFunc(SyscallDesc *desc, int num,
2809112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
2819112Smarc.orr@gmail.com
2829112Smarc.orr@gmail.com/// Target getuid() handler.
2839112Smarc.orr@gmail.comSyscallReturn getuidFunc(SyscallDesc *desc, int num,
2849112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
2859112Smarc.orr@gmail.com
2869112Smarc.orr@gmail.com/// Target getgid() handler.
2879112Smarc.orr@gmail.comSyscallReturn getgidFunc(SyscallDesc *desc, int num,
2889112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
2899112Smarc.orr@gmail.com
2909112Smarc.orr@gmail.com/// Target getppid() handler.
2919112Smarc.orr@gmail.comSyscallReturn getppidFunc(SyscallDesc *desc, int num,
2929112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
2939112Smarc.orr@gmail.com
2949112Smarc.orr@gmail.com/// Target geteuid() handler.
2959112Smarc.orr@gmail.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2969112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
2979112Smarc.orr@gmail.com
2989112Smarc.orr@gmail.com/// Target getegid() handler.
2999112Smarc.orr@gmail.comSyscallReturn getegidFunc(SyscallDesc *desc, int num,
3009112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
3019238Slluc.alvarez@bsc.es
3029112Smarc.orr@gmail.com/// Target clone() handler.
3039112Smarc.orr@gmail.comSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3049112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
3059112Smarc.orr@gmail.com
3069112Smarc.orr@gmail.com/// Target access() handler
3079112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3089112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc);
3099112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3109112Smarc.orr@gmail.com                               LiveProcess *p, ThreadContext *tc,
3119112Smarc.orr@gmail.com                               int index);
31211367Sandreas.hansson@arm.com
3139112Smarc.orr@gmail.com/// Futex system call
31411321Ssteve.reinhardt@amd.com///  Implemented by Daniel Sanchez
3159112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3169112Smarc.orr@gmail.comtemplate <class OS>
3179112Smarc.orr@gmail.comSyscallReturn
3189112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3199112Smarc.orr@gmail.com          ThreadContext *tc)
3209112Smarc.orr@gmail.com{
3219112Smarc.orr@gmail.com    int index_uaddr = 0;
3229112Smarc.orr@gmail.com    int index_op = 1;
3239112Smarc.orr@gmail.com    int index_val = 2;
3249112Smarc.orr@gmail.com    int index_timeout = 3;
3259112Smarc.orr@gmail.com
3269112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3279112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3289112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3299112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3309112Smarc.orr@gmail.com
3319112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3329112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3339112Smarc.orr@gmail.com
3349112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3359112Smarc.orr@gmail.com            uaddr, op, val);
3369112Smarc.orr@gmail.com
3379112Smarc.orr@gmail.com    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3389112Smarc.orr@gmail.com
3399112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3409112Smarc.orr@gmail.com        if (timeout != 0) {
3419112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3429112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3439112Smarc.orr@gmail.com        }
34411321Ssteve.reinhardt@amd.com
3459112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3469112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3479112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3489112Smarc.orr@gmail.com        delete[] buf;
3499112Smarc.orr@gmail.com
3509112Smarc.orr@gmail.com        if (val != mem_val) {
3519112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3529112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3539238Slluc.alvarez@bsc.es            return -OS::TGT_EWOULDBLOCK;
3549112Smarc.orr@gmail.com        }
3559112Smarc.orr@gmail.com
3569112Smarc.orr@gmail.com        // Queue the thread context
3579112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3589112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3592238SN/A            tcWaitList = futex_map.find(uaddr)->second;
3602238SN/A        } else {
3612238SN/A            tcWaitList = new std::list<ThreadContext *>();
3622238SN/A            futex_map.insert(std::pair< uint64_t,
3633114Sgblack@eecs.umich.edu                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3642238SN/A        }
3652238SN/A        tcWaitList->push_back(tc);
3662238SN/A        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3673114Sgblack@eecs.umich.edu                                "thread context\n");
3682238SN/A        tc->suspend();
3692238SN/A        return 0;
3702238SN/A    } else if (op == OS::TGT_FUTEX_WAKE){
3713114Sgblack@eecs.umich.edu        int wokenUp = 0;
3722238SN/A        std::list<ThreadContext *> * tcWaitList;
3732238SN/A        if (futex_map.count(uaddr)) {
3742238SN/A            tcWaitList = futex_map.find(uaddr)->second;
3753114Sgblack@eecs.umich.edu            while (tcWaitList->size() > 0 && wokenUp < val) {
3762238SN/A                tcWaitList->front()->activate();
3772238SN/A                tcWaitList->pop_front();
3781354SN/A                wokenUp++;
3791354SN/A            }
38010796Sbrandon.potter@amd.com            if (tcWaitList->empty()) {
38110796Sbrandon.potter@amd.com                futex_map.erase(uaddr);
3821354SN/A                delete tcWaitList;
3831354SN/A            }
3841354SN/A        }
3851354SN/A        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3861354SN/A                                "thread contexts\n", wokenUp);
3871354SN/A        return wokenUp;
3881354SN/A    } else {
3891354SN/A        warn("sys_futex: op %d is not implemented, just returning...", op);
3901354SN/A        return 0;
3911354SN/A    }
39210796Sbrandon.potter@amd.com
3931354SN/A}
39410796Sbrandon.potter@amd.com
3951354SN/A
3961354SN/A/// Pseudo Funcs  - These functions use a different return convension,
3971354SN/A/// returning a second value in a register other than the normal return register
3981354SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
39910796Sbrandon.potter@amd.com                             LiveProcess *process, ThreadContext *tc);
40010796Sbrandon.potter@amd.com
40110796Sbrandon.potter@amd.com/// Target getpidPseudo() handler.
40210796Sbrandon.potter@amd.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
40310796Sbrandon.potter@amd.com                               LiveProcess *p, ThreadContext *tc);
40410796Sbrandon.potter@amd.com
40510796Sbrandon.potter@amd.com/// Target getuidPseudo() handler.
40610796Sbrandon.potter@amd.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
40710796Sbrandon.potter@amd.com                               LiveProcess *p, ThreadContext *tc);
40810796Sbrandon.potter@amd.com
40910796Sbrandon.potter@amd.com/// Target getgidPseudo() handler.
410360SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
411360SN/A                               LiveProcess *p, ThreadContext *tc);
412360SN/A
413360SN/A
414360SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
415360SN/Aconst int one_million = 1000000;
416360SN/A/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
41711759Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4183113Sgblack@eecs.umich.edu
4193113Sgblack@eecs.umich.edu/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4203113Sgblack@eecs.umich.edu/// by my reckoning.  We want to keep this a constant (not use the
4213113Sgblack@eecs.umich.edu/// real-world time) to keep simulations repeatable.
4223113Sgblack@eecs.umich.educonst unsigned seconds_since_epoch = 1000000000;
4233113Sgblack@eecs.umich.edu
4243113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
4253113Sgblack@eecs.umich.edu/// microseconds.
4263113Sgblack@eecs.umich.edutemplate <class T1, class T2>
4273113Sgblack@eecs.umich.eduvoid
4283113Sgblack@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec)
4293113Sgblack@eecs.umich.edu{
4303113Sgblack@eecs.umich.edu    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4313113Sgblack@eecs.umich.edu    sec = elapsed_usecs / one_million;
4323113Sgblack@eecs.umich.edu    usec = elapsed_usecs % one_million;
4333113Sgblack@eecs.umich.edu}
4344189Sgblack@eecs.umich.edu
4354189Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
4363113Sgblack@eecs.umich.edu/// nanoseconds.
4373113Sgblack@eecs.umich.edutemplate <class T1, class T2>
4383113Sgblack@eecs.umich.eduvoid
4393113Sgblack@eecs.umich.edugetElapsedTimeNano(T1 &sec, T2 &nsec)
4408737Skoansin.tan@gmail.com{
4413113Sgblack@eecs.umich.edu    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
4428737Skoansin.tan@gmail.com    sec = elapsed_nsecs / one_billion;
4433277Sgblack@eecs.umich.edu    nsec = elapsed_nsecs % one_billion;
4445515SMichael.Adler@intel.com}
4455515SMichael.Adler@intel.com
4465515SMichael.Adler@intel.com//////////////////////////////////////////////////////////////////////
4475515SMichael.Adler@intel.com//
4485515SMichael.Adler@intel.com// The following emulation functions are generic, but need to be
4498737Skoansin.tan@gmail.com// templated to account for differences in types, constants, etc.
4503277Sgblack@eecs.umich.edu//
4518737Skoansin.tan@gmail.com//////////////////////////////////////////////////////////////////////
4523277Sgblack@eecs.umich.edu
4538737Skoansin.tan@gmail.com#if NO_STAT64
4543277Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4558737Skoansin.tan@gmail.com    typedef struct stat hst_stat64;
4563113Sgblack@eecs.umich.edu#else
4573113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4583113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4593113Sgblack@eecs.umich.edu#endif
4608737Skoansin.tan@gmail.com
4613113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4628737Skoansin.tan@gmail.com//// buffer.  Also copies the target buffer out to the simulated
4633114Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4648737Skoansin.tan@gmail.com
4653114Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4668737Skoansin.tan@gmail.comstatic void
4673114Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4688737Skoansin.tan@gmail.com{
4694061Sgblack@eecs.umich.edu    using namespace TheISA;
4704061Sgblack@eecs.umich.edu
4714061Sgblack@eecs.umich.edu    if (fakeTTY)
4728737Skoansin.tan@gmail.com        tgt->st_dev = 0xA;
4733113Sgblack@eecs.umich.edu    else
4748737Skoansin.tan@gmail.com        tgt->st_dev = host->st_dev;
4753113Sgblack@eecs.umich.edu    tgt->st_dev = TheISA::htog(tgt->st_dev);
4763113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4773113Sgblack@eecs.umich.edu    tgt->st_ino = TheISA::htog(tgt->st_ino);
4783113Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4793113Sgblack@eecs.umich.edu    if (fakeTTY) {
4803113Sgblack@eecs.umich.edu        // Claim to be a character device
4813113Sgblack@eecs.umich.edu        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4823113Sgblack@eecs.umich.edu        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4834189Sgblack@eecs.umich.edu    }
4844189Sgblack@eecs.umich.edu    tgt->st_mode = TheISA::htog(tgt->st_mode);
4853113Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4863113Sgblack@eecs.umich.edu    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4873113Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4888737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4893113Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4908737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4913113Sgblack@eecs.umich.edu    if (fakeTTY)
4928737Skoansin.tan@gmail.com        tgt->st_rdev = 0x880d;
4933113Sgblack@eecs.umich.edu    else
4943113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4953113Sgblack@eecs.umich.edu    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4963113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4973113Sgblack@eecs.umich.edu    tgt->st_size = TheISA::htog(tgt->st_size);
4983113Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4993113Sgblack@eecs.umich.edu    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5003113Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5013113Sgblack@eecs.umich.edu    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5023113Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5038852Sandreas.hansson@arm.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5043113Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5053113Sgblack@eecs.umich.edu    // consistently across different hosts.
5063113Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5073113Sgblack@eecs.umich.edu    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5083113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5093113Sgblack@eecs.umich.edu    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5103113Sgblack@eecs.umich.edu}
5113113Sgblack@eecs.umich.edu
5123113Sgblack@eecs.umich.edu// Same for stat64
5133113Sgblack@eecs.umich.edu
5148852Sandreas.hansson@arm.comtemplate <typename target_stat, typename host_stat64>
5153113Sgblack@eecs.umich.edustatic void
5163113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5173113Sgblack@eecs.umich.edu{
5183113Sgblack@eecs.umich.edu    using namespace TheISA;
5196686Stjones1@inf.ed.ac.uk
5203113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5213113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5223113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
52311759Sbrandon.potter@amd.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
52411759Sbrandon.potter@amd.com    tgt->st_mtime_nsec = host->st_mtime_nsec;
52511759Sbrandon.potter@amd.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
52611759Sbrandon.potter@amd.com    tgt->st_ctime_nsec = host->st_ctime_nsec;
52711759Sbrandon.potter@amd.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
52811759Sbrandon.potter@amd.com#else
52911759Sbrandon.potter@amd.com    tgt->st_atime_nsec = 0;
53011759Sbrandon.potter@amd.com    tgt->st_mtime_nsec = 0;
53111759Sbrandon.potter@amd.com    tgt->st_ctime_nsec = 0;
53211759Sbrandon.potter@amd.com#endif
53311759Sbrandon.potter@amd.com}
53411759Sbrandon.potter@amd.com
53511759Sbrandon.potter@amd.com//Here are a couple convenience functions
53611759Sbrandon.potter@amd.comtemplate<class OS>
53711759Sbrandon.potter@amd.comstatic void
53811759Sbrandon.potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
53911759Sbrandon.potter@amd.com        hst_stat *host, bool fakeTTY = false)
54011759Sbrandon.potter@amd.com{
54111759Sbrandon.potter@amd.com    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
54211759Sbrandon.potter@amd.com    tgt_stat_buf tgt(addr);
54311759Sbrandon.potter@amd.com    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
54411759Sbrandon.potter@amd.com    tgt.copyOut(mem);
54511759Sbrandon.potter@amd.com}
54611759Sbrandon.potter@amd.com
54711759Sbrandon.potter@amd.comtemplate<class OS>
54811759Sbrandon.potter@amd.comstatic void
549378SN/AcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
550378SN/A        hst_stat64 *host, bool fakeTTY = false)
5519141Smarc.orr@gmail.com{
5529141Smarc.orr@gmail.com    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
553360SN/A    tgt_stat_buf tgt(addr);
5541450SN/A    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5553114Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5562680Sktlim@umich.edu}
557360SN/A
5586701Sgblack@eecs.umich.edu/// Target ioctl() handler.  For the most part, programs call ioctl()
55910930Sbrandon.potter@amd.com/// only to find out if their stdout is a tty, to determine whether to
5606701Sgblack@eecs.umich.edu/// do line or block buffering.  We always claim that output fds are
561360SN/A/// not TTYs to provide repeatable results.
56210930Sbrandon.potter@amd.comtemplate <class OS>
563360SN/ASyscallReturn
56410932Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
56510496Ssteve.reinhardt@amd.com          ThreadContext *tc)
56610930Sbrandon.potter@amd.com{
567360SN/A    int index = 0;
5681458SN/A    int tgt_fd = process->getSyscallArg(tc, index);
569360SN/A    unsigned req = process->getSyscallArg(tc, index);
570360SN/A
57110930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
57210930Sbrandon.potter@amd.com
57310496Ssteve.reinhardt@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
57410496Ssteve.reinhardt@amd.com
5759141Smarc.orr@gmail.com    if (fde == NULL) {
5761458SN/A        // doesn't map to any simulator fd: not a valid target fd
5779141Smarc.orr@gmail.com        return -EBADF;
578360SN/A    }
5799141Smarc.orr@gmail.com
58010930Sbrandon.potter@amd.com    if (fde->driver != NULL) {
5819141Smarc.orr@gmail.com        return fde->driver->ioctl(process, tc, req);
582360SN/A    }
583360SN/A
584360SN/A    if (OS::isTtyReq(req)) {
58510027SChris.Adeniyi-Jones@arm.com        return -ENOTTY;
5863114Sgblack@eecs.umich.edu    }
58710027SChris.Adeniyi-Jones@arm.com
588360SN/A    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
589360SN/A         tgt_fd, req, tc->pcState());
590360SN/A    return -ENOTTY;
5918852Sandreas.hansson@arm.com}
5926701Sgblack@eecs.umich.edu
5931458SN/Atemplate <class OS>
594360SN/Astatic SyscallReturn
5956701Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5966701Sgblack@eecs.umich.edu         ThreadContext *tc, int index)
597360SN/A{
598360SN/A    std::string path;
599360SN/A
600360SN/A    if (!tc->getMemProxy().tryReadString(path,
601360SN/A                process->getSyscallArg(tc, index)))
602360SN/A        return -EFAULT;
603360SN/A
604360SN/A    int tgtFlags = process->getSyscallArg(tc, index);
605360SN/A    int mode = process->getSyscallArg(tc, index);
606360SN/A    int hostFlags = 0;
607360SN/A
608360SN/A    // translate open flags
6091706SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
610360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
611360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
612360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
613360SN/A        }
614360SN/A    }
6153669Sbinkertn@umich.edu
6163669Sbinkertn@umich.edu    // any target flags left?
6173669Sbinkertn@umich.edu    if (tgtFlags != 0)
6181706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
6191706SN/A
62010496Ssteve.reinhardt@amd.com#ifdef __CYGWIN32__
62110496Ssteve.reinhardt@amd.com    hostFlags |= O_BINARY;
62210496Ssteve.reinhardt@amd.com#endif
62310496Ssteve.reinhardt@amd.com
62410496Ssteve.reinhardt@amd.com    // Adjust path for current working directory
62510496Ssteve.reinhardt@amd.com    path = process->fullPath(path);
62610496Ssteve.reinhardt@amd.com
62710496Ssteve.reinhardt@amd.com    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
62810496Ssteve.reinhardt@amd.com
62910496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
63010496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
63110496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
63210496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
63310496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
63410496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
63510496Ssteve.reinhardt@amd.com            return -ENOENT;
63610496Ssteve.reinhardt@amd.com        }
63710496Ssteve.reinhardt@amd.com
63810496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
63910496Ssteve.reinhardt@amd.com        if (drv != NULL) {
6405795Ssaidi@eecs.umich.edu            // the driver's open method will allocate a fd from the
6419143Ssteve.reinhardt@amd.com            // process if necessary.
6429142Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
6439142Ssteve.reinhardt@amd.com        }
6449143Ssteve.reinhardt@amd.com
6455795Ssaidi@eecs.umich.edu        // fall through here for pass through to host devices, such as
6469143Ssteve.reinhardt@amd.com        // /dev/zero
6475795Ssaidi@eecs.umich.edu    }
6485795Ssaidi@eecs.umich.edu
6495795Ssaidi@eecs.umich.edu    int fd;
6509143Ssteve.reinhardt@amd.com    int local_errno;
6515795Ssaidi@eecs.umich.edu    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
652360SN/A        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6539143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6549143Ssteve.reinhardt@amd.com        fd = OS::openSpecialFile(path, process, tc);
6559143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
65610932Sbrandon.potter@amd.com     } else {
657360SN/A        // open the file
658360SN/A        fd = open(path.c_str(), hostFlags, mode);
65910027SChris.Adeniyi-Jones@arm.com        local_errno = errno;
66010027SChris.Adeniyi-Jones@arm.com     }
66110027SChris.Adeniyi-Jones@arm.com
66210027SChris.Adeniyi-Jones@arm.com    if (fd == -1)
66310027SChris.Adeniyi-Jones@arm.com        return -local_errno;
66410027SChris.Adeniyi-Jones@arm.com
66510027SChris.Adeniyi-Jones@arm.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
66610027SChris.Adeniyi-Jones@arm.com}
66710027SChris.Adeniyi-Jones@arm.com
66810027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
66910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67010027SChris.Adeniyi-Jones@arm.comSyscallReturn
67110027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67210027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67310027SChris.Adeniyi-Jones@arm.com{
67410027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
67510027SChris.Adeniyi-Jones@arm.com}
67610027SChris.Adeniyi-Jones@arm.com
67710027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
67810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67910027SChris.Adeniyi-Jones@arm.comSyscallReturn
68010027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68110633Smichaelupton@gmail.com         ThreadContext *tc)
68210633Smichaelupton@gmail.com{
68310633Smichaelupton@gmail.com    int index = 0;
68410633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
68510633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
68610633Smichaelupton@gmail.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
68710633Smichaelupton@gmail.com    return openFunc<OS>(desc, callnum, process, tc, 1);
68810633Smichaelupton@gmail.com}
68910633Smichaelupton@gmail.com
69010633Smichaelupton@gmail.com/// Target unlinkat() handler.
69110633Smichaelupton@gmail.comtemplate <class OS>
69210633Smichaelupton@gmail.comSyscallReturn
69310633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69410633Smichaelupton@gmail.com             ThreadContext *tc)
69510203SAli.Saidi@ARM.com{
69610203SAli.Saidi@ARM.com    int index = 0;
69710203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
69810203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
69910203SAli.Saidi@ARM.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
70010203SAli.Saidi@ARM.com
70110203SAli.Saidi@ARM.com    return unlinkHelper(desc, callnum, process, tc, 1);
70210203SAli.Saidi@ARM.com}
70310203SAli.Saidi@ARM.com
70410203SAli.Saidi@ARM.com/// Target facessat() handler
70510203SAli.Saidi@ARM.comtemplate <class OS>
70610203SAli.Saidi@ARM.comSyscallReturn
70710203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70810203SAli.Saidi@ARM.com        ThreadContext *tc)
70910203SAli.Saidi@ARM.com{
71010203SAli.Saidi@ARM.com    int index = 0;
71110203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71210203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71310203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
71410203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
71510203SAli.Saidi@ARM.com}
71610203SAli.Saidi@ARM.com
71710203SAli.Saidi@ARM.com/// Target readlinkat() handler
71810203SAli.Saidi@ARM.comtemplate <class OS>
71910203SAli.Saidi@ARM.comSyscallReturn
72010203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
72110850SGiacomo.Gabrielli@arm.com        ThreadContext *tc)
72210850SGiacomo.Gabrielli@arm.com{
72310850SGiacomo.Gabrielli@arm.com    int index = 0;
72410850SGiacomo.Gabrielli@arm.com    int dirfd = process->getSyscallArg(tc, index);
72510850SGiacomo.Gabrielli@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
72610850SGiacomo.Gabrielli@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72710850SGiacomo.Gabrielli@arm.com    return readlinkFunc(desc, callnum, process, tc, 1);
72810850SGiacomo.Gabrielli@arm.com}
72910850SGiacomo.Gabrielli@arm.com
73010850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
73110850SGiacomo.Gabrielli@arm.comtemplate <class OS>
73210850SGiacomo.Gabrielli@arm.comSyscallReturn
73310850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
73410850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
73510850SGiacomo.Gabrielli@arm.com{
73610850SGiacomo.Gabrielli@arm.com    int index = 0;
73710850SGiacomo.Gabrielli@arm.com
73810850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
73910850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
74010850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
74110850SGiacomo.Gabrielli@arm.com
74210850SGiacomo.Gabrielli@arm.com    std::string old_name;
74310850SGiacomo.Gabrielli@arm.com
74410850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
74510850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
74610850SGiacomo.Gabrielli@arm.com        return -EFAULT;
74710850SGiacomo.Gabrielli@arm.com
74810850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
74910850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
75010850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
75110850SGiacomo.Gabrielli@arm.com
75210850SGiacomo.Gabrielli@arm.com    std::string new_name;
75310850SGiacomo.Gabrielli@arm.com
75410850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
75510850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
75610850SGiacomo.Gabrielli@arm.com        return -EFAULT;
7576640Svince@csl.cornell.edu
7586640Svince@csl.cornell.edu    // Adjust path for current working directory
7596640Svince@csl.cornell.edu    old_name = process->fullPath(old_name);
7606640Svince@csl.cornell.edu    new_name = process->fullPath(new_name);
7616640Svince@csl.cornell.edu
7626640Svince@csl.cornell.edu    int result = rename(old_name.c_str(), new_name.c_str());
7636640Svince@csl.cornell.edu    return (result == -1) ? -errno : result;
7646701Sgblack@eecs.umich.edu}
7656701Sgblack@eecs.umich.edu
76610793Sbrandon.potter@amd.com/// Target sysinfo() handler.
7676640Svince@csl.cornell.edutemplate <class OS>
76811758Sbrandon.potter@amd.comSyscallReturn
76911758Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
77011758Sbrandon.potter@amd.com         ThreadContext *tc)
7716640Svince@csl.cornell.edu{
7728706Sandreas.hansson@arm.com
7736640Svince@csl.cornell.edu    int index = 0;
7746701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
7756640Svince@csl.cornell.edu        sysinfo(process->getSyscallArg(tc, index));
776360SN/A
7771999SN/A    sysinfo->uptime=seconds_since_epoch;
7781999SN/A    sysinfo->totalram=process->system->memSize();
7791999SN/A
7803114Sgblack@eecs.umich.edu    sysinfo.copyOut(tc->getMemProxy());
7812680Sktlim@umich.edu
7821999SN/A    return 0;
7831999SN/A}
7841999SN/A
7856701Sgblack@eecs.umich.edu/// Target chmod() handler.
7868852Sandreas.hansson@arm.comtemplate <class OS>
7876701Sgblack@eecs.umich.eduSyscallReturn
7881999SN/AchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7896701Sgblack@eecs.umich.edu          ThreadContext *tc)
7901999SN/A{
7916701Sgblack@eecs.umich.edu    std::string path;
7921999SN/A
7931999SN/A    int index = 0;
7941999SN/A    if (!tc->getMemProxy().tryReadString(path,
7951999SN/A                process->getSyscallArg(tc, index))) {
7961999SN/A        return -EFAULT;
7973669Sbinkertn@umich.edu    }
7983669Sbinkertn@umich.edu
7993669Sbinkertn@umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8001999SN/A    mode_t hostMode = 0;
8011999SN/A
8021999SN/A    // XXX translate mode flags via OS::something???
8032218SN/A    hostMode = mode;
8041999SN/A
8051999SN/A    // Adjust path for current working directory
8061999SN/A    path = process->fullPath(path);
8071999SN/A
8081999SN/A    // do the chmod
8091999SN/A    int result = chmod(path.c_str(), hostMode);
8101999SN/A    if (result < 0)
8111999SN/A        return -errno;
8123114Sgblack@eecs.umich.edu
8132680Sktlim@umich.edu    return 0;
8141999SN/A}
8156701Sgblack@eecs.umich.edu
81610931Sbrandon.potter@amd.com
81710931Sbrandon.potter@amd.com/// Target fchmod() handler.
81810931Sbrandon.potter@amd.comtemplate <class OS>
81910932Sbrandon.potter@amd.comSyscallReturn
82010931Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8211999SN/A           ThreadContext *tc)
8221999SN/A{
8231999SN/A    int index = 0;
8241999SN/A    int tgt_fd = process->getSyscallArg(tc, index);
8251999SN/A    uint32_t mode = process->getSyscallArg(tc, index);
8261999SN/A
8271999SN/A    int sim_fd = process->getSimFD(tgt_fd);
8281999SN/A    if (sim_fd < 0)
82910931Sbrandon.potter@amd.com        return -EBADF;
8301999SN/A
8312218SN/A    mode_t hostMode = 0;
8321999SN/A
8331999SN/A    // XXX translate mode flags via OS::someting???
8341999SN/A    hostMode = mode;
8351999SN/A
8365877Shsul@eecs.umich.edu    // do the fchmod
8375877Shsul@eecs.umich.edu    int result = fchmod(sim_fd, hostMode);
8385877Shsul@eecs.umich.edu    if (result < 0)
8395877Shsul@eecs.umich.edu        return -errno;
8405877Shsul@eecs.umich.edu
8416701Sgblack@eecs.umich.edu    return 0;
8426701Sgblack@eecs.umich.edu}
8436701Sgblack@eecs.umich.edu
8446701Sgblack@eecs.umich.edu/// Target mremap() handler.
8456701Sgblack@eecs.umich.edutemplate <class OS>
84610027SChris.Adeniyi-Jones@arm.comSyscallReturn
84710027SChris.Adeniyi-Jones@arm.commremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
84810027SChris.Adeniyi-Jones@arm.com{
84910027SChris.Adeniyi-Jones@arm.com    int index = 0;
85010027SChris.Adeniyi-Jones@arm.com    Addr start = process->getSyscallArg(tc, index);
8515877Shsul@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
85210318Sandreas.hansson@arm.com    uint64_t new_length = process->getSyscallArg(tc, index);
85310318Sandreas.hansson@arm.com    uint64_t flags = process->getSyscallArg(tc, index);
8545877Shsul@eecs.umich.edu    uint64_t provided_address = 0;
8555877Shsul@eecs.umich.edu    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
8565877Shsul@eecs.umich.edu
8575877Shsul@eecs.umich.edu    if (use_provided_address)
85810486Stjablin@gmail.com        provided_address = process->getSyscallArg(tc, index);
85910486Stjablin@gmail.com
8605877Shsul@eecs.umich.edu    if ((start % TheISA::PageBytes != 0) ||
86110027SChris.Adeniyi-Jones@arm.com        (provided_address % TheISA::PageBytes != 0)) {
86210027SChris.Adeniyi-Jones@arm.com        warn("mremap failing: arguments not page aligned");
8635877Shsul@eecs.umich.edu        return -EINVAL;
8648601Ssteve.reinhardt@amd.com    }
8655877Shsul@eecs.umich.edu
8665877Shsul@eecs.umich.edu    new_length = roundUp(new_length, TheISA::PageBytes);
8675877Shsul@eecs.umich.edu
86810027SChris.Adeniyi-Jones@arm.com    if (new_length > old_length) {
8695877Shsul@eecs.umich.edu        if ((start + old_length) == process->mmap_end &&
8705877Shsul@eecs.umich.edu            (!use_provided_address || provided_address == start)) {
8715877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
87210027SChris.Adeniyi-Jones@arm.com            process->allocateMem(process->mmap_end, diff);
87310027SChris.Adeniyi-Jones@arm.com            process->mmap_end += diff;
87410027SChris.Adeniyi-Jones@arm.com            return start;
87510027SChris.Adeniyi-Jones@arm.com        } else {
87610027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
87710027SChris.Adeniyi-Jones@arm.com                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8785877Shsul@eecs.umich.edu                return -ENOMEM;
87910027SChris.Adeniyi-Jones@arm.com            } else {
88010027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
88110027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
88210027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
88310027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
88410027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
88510027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
88610027SChris.Adeniyi-Jones@arm.com                // add on the remaining unallocated pages
88710027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
88810027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
88910027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
89010027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
89110027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
8925877Shsul@eecs.umich.edu                if (use_provided_address &&
8935877Shsul@eecs.umich.edu                    new_start + new_length > process->mmap_end) {
8945877Shsul@eecs.umich.edu                    // something fishy going on here, at least notify the user
89510027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
89610027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
8978601Ssteve.reinhardt@amd.com                }
89810027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
8995877Shsul@eecs.umich.edu                return new_start;
9005877Shsul@eecs.umich.edu            }
9011999SN/A        }
902378SN/A    } else {
903360SN/A        if (use_provided_address && provided_address != start)
9041450SN/A            process->pTable->remap(start, new_length, provided_address);
9053114Sgblack@eecs.umich.edu        process->pTable->unmap(start + new_length, old_length - new_length);
9062680Sktlim@umich.edu        return use_provided_address ? provided_address : start;
907360SN/A    }
908360SN/A}
909360SN/A
9106701Sgblack@eecs.umich.edu/// Target stat() handler.
9118852Sandreas.hansson@arm.comtemplate <class OS>
9126701Sgblack@eecs.umich.eduSyscallReturn
9136701Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9146701Sgblack@eecs.umich.edu         ThreadContext *tc)
9156701Sgblack@eecs.umich.edu{
916360SN/A    std::string path;
9173669Sbinkertn@umich.edu
9183669Sbinkertn@umich.edu    int index = 0;
9193669Sbinkertn@umich.edu    if (!tc->getMemProxy().tryReadString(path,
920360SN/A                process->getSyscallArg(tc, index))) {
921360SN/A        return -EFAULT;
922360SN/A    }
923360SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
9242218SN/A
925360SN/A    // Adjust path for current working directory
9268706Sandreas.hansson@arm.com    path = process->fullPath(path);
927360SN/A
9281458SN/A    struct stat hostBuf;
929360SN/A    int result = stat(path.c_str(), &hostBuf);
930360SN/A
931360SN/A    if (result < 0)
9325074Ssaidi@eecs.umich.edu        return -errno;
9335074Ssaidi@eecs.umich.edu
9345074Ssaidi@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9355074Ssaidi@eecs.umich.edu
9365074Ssaidi@eecs.umich.edu    return 0;
9375074Ssaidi@eecs.umich.edu}
9385074Ssaidi@eecs.umich.edu
9395074Ssaidi@eecs.umich.edu
9406701Sgblack@eecs.umich.edu/// Target stat64() handler.
9418852Sandreas.hansson@arm.comtemplate <class OS>
9426701Sgblack@eecs.umich.eduSyscallReturn
9435074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9446701Sgblack@eecs.umich.edu           ThreadContext *tc)
9455074Ssaidi@eecs.umich.edu{
9465074Ssaidi@eecs.umich.edu    std::string path;
9475074Ssaidi@eecs.umich.edu
9485074Ssaidi@eecs.umich.edu    int index = 0;
9495208Ssaidi@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
9505208Ssaidi@eecs.umich.edu                process->getSyscallArg(tc, index)))
9515208Ssaidi@eecs.umich.edu        return -EFAULT;
9525208Ssaidi@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9535074Ssaidi@eecs.umich.edu
9545074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9555208Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9565074Ssaidi@eecs.umich.edu
9575074Ssaidi@eecs.umich.edu#if NO_STAT64
9585074Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9595074Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9608706Sandreas.hansson@arm.com#else
9615074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9625074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9635074Ssaidi@eecs.umich.edu#endif
9645074Ssaidi@eecs.umich.edu
9655074Ssaidi@eecs.umich.edu    if (result < 0)
96610027SChris.Adeniyi-Jones@arm.com        return -errno;
96710027SChris.Adeniyi-Jones@arm.com
96810027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
96910027SChris.Adeniyi-Jones@arm.com
97010027SChris.Adeniyi-Jones@arm.com    return 0;
97110027SChris.Adeniyi-Jones@arm.com}
97210027SChris.Adeniyi-Jones@arm.com
97310027SChris.Adeniyi-Jones@arm.com
97410027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
97510793Sbrandon.potter@amd.comtemplate <class OS>
97610027SChris.Adeniyi-Jones@arm.comSyscallReturn
97710027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
97810027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
97910027SChris.Adeniyi-Jones@arm.com{
98010027SChris.Adeniyi-Jones@arm.com    int index = 0;
98110027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
98210027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
98310027SChris.Adeniyi-Jones@arm.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
98410027SChris.Adeniyi-Jones@arm.com
98510027SChris.Adeniyi-Jones@arm.com    std::string path;
98610027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
98710027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
98810027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
98910027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
99010027SChris.Adeniyi-Jones@arm.com
99110027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
99210027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
99310027SChris.Adeniyi-Jones@arm.com
99410027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
99510027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
99610027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
99710027SChris.Adeniyi-Jones@arm.com#else
99810027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
99910027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
100010027SChris.Adeniyi-Jones@arm.com#endif
100110027SChris.Adeniyi-Jones@arm.com
100210027SChris.Adeniyi-Jones@arm.com    if (result < 0)
10031999SN/A        return -errno;
10041999SN/A
10051999SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10063114Sgblack@eecs.umich.edu
10072680Sktlim@umich.edu    return 0;
10081999SN/A}
10096701Sgblack@eecs.umich.edu
101010931Sbrandon.potter@amd.com
10116701Sgblack@eecs.umich.edu/// Target fstat64() handler.
101210931Sbrandon.potter@amd.comtemplate <class OS>
101310932Sbrandon.potter@amd.comSyscallReturn
101410931Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10151999SN/A            ThreadContext *tc)
10161999SN/A{
10172764Sstever@eecs.umich.edu    int index = 0;
10182064SN/A    int tgt_fd = process->getSyscallArg(tc, index);
101910931Sbrandon.potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
10202064SN/A
10212064SN/A    int sim_fd = process->getSimFD(tgt_fd);
102210931Sbrandon.potter@amd.com    if (sim_fd < 0)
10232064SN/A        return -EBADF;
10241999SN/A
10251999SN/A#if NO_STAT64
10262218SN/A    struct stat  hostBuf;
10271999SN/A    int result = fstat(sim_fd, &hostBuf);
102810931Sbrandon.potter@amd.com#else
10291999SN/A    struct stat64  hostBuf;
10301999SN/A    int result = fstat64(sim_fd, &hostBuf);
10311999SN/A#endif
10321999SN/A
10331999SN/A    if (result < 0)
1034378SN/A        return -errno;
1035360SN/A
10361450SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10373114Sgblack@eecs.umich.edu
10382680Sktlim@umich.edu    return 0;
1039360SN/A}
1040360SN/A
1041360SN/A
10426701Sgblack@eecs.umich.edu/// Target lstat() handler.
10438852Sandreas.hansson@arm.comtemplate <class OS>
10446701Sgblack@eecs.umich.eduSyscallReturn
10456701Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10466701Sgblack@eecs.umich.edu          ThreadContext *tc)
10476701Sgblack@eecs.umich.edu{
1048360SN/A    std::string path;
10493669Sbinkertn@umich.edu
10503669Sbinkertn@umich.edu    int index = 0;
10513669Sbinkertn@umich.edu    if (!tc->getMemProxy().tryReadString(path,
1052360SN/A                process->getSyscallArg(tc, index))) {
1053360SN/A        return -EFAULT;
1054360SN/A    }
1055360SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
10561458SN/A
1057360SN/A    // Adjust path for current working directory
10588706Sandreas.hansson@arm.com    path = process->fullPath(path);
1059360SN/A
10601458SN/A    struct stat hostBuf;
1061360SN/A    int result = lstat(path.c_str(), &hostBuf);
1062360SN/A
10631999SN/A    if (result < 0)
10641999SN/A        return -errno;
10651999SN/A
10663114Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10672680Sktlim@umich.edu
10681999SN/A    return 0;
10691999SN/A}
10701999SN/A
10716701Sgblack@eecs.umich.edu/// Target lstat64() handler.
10728852Sandreas.hansson@arm.comtemplate <class OS>
10736701Sgblack@eecs.umich.eduSyscallReturn
10746701Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10756701Sgblack@eecs.umich.edu            ThreadContext *tc)
10766701Sgblack@eecs.umich.edu{
10771999SN/A    std::string path;
10783669Sbinkertn@umich.edu
10793669Sbinkertn@umich.edu    int index = 0;
10803669Sbinkertn@umich.edu    if (!tc->getMemProxy().tryReadString(path,
10812764Sstever@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10822064SN/A        return -EFAULT;
10832064SN/A    }
10842064SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
10851999SN/A
10861999SN/A    // Adjust path for current working directory
10872064SN/A    path = process->fullPath(path);
10881999SN/A
10891999SN/A#if NO_STAT64
10901999SN/A    struct stat hostBuf;
10911999SN/A    int result = lstat(path.c_str(), &hostBuf);
10928706Sandreas.hansson@arm.com#else
10931999SN/A    struct stat64 hostBuf;
10941999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10951999SN/A#endif
10961999SN/A
1097378SN/A    if (result < 0)
1098360SN/A        return -errno;
10991450SN/A
11003114Sgblack@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11012680Sktlim@umich.edu
1102360SN/A    return 0;
11036701Sgblack@eecs.umich.edu}
110410931Sbrandon.potter@amd.com
11056701Sgblack@eecs.umich.edu/// Target fstat() handler.
1106360SN/Atemplate <class OS>
110711380Salexandru.dutu@amd.comSyscallReturn
1108360SN/AfstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
110910932Sbrandon.potter@amd.com          ThreadContext *tc)
111010931Sbrandon.potter@amd.com{
11111458SN/A    int index = 0;
1112360SN/A    int tgt_fd = process->getSyscallArg(tc, index);
1113360SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
111410931Sbrandon.potter@amd.com
1115360SN/A    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1116360SN/A
11171458SN/A    int sim_fd = process->getSimFD(tgt_fd);
1118360SN/A    if (sim_fd < 0)
111910931Sbrandon.potter@amd.com        return -EBADF;
11202021SN/A
11211458SN/A    struct stat hostBuf;
1122360SN/A    int result = fstat(sim_fd, &hostBuf);
1123360SN/A
1124360SN/A    if (result < 0)
11251706SN/A        return -errno;
11261706SN/A
11271706SN/A    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11283114Sgblack@eecs.umich.edu
11292680Sktlim@umich.edu    return 0;
11301706SN/A}
113111799Sbrandon.potter@amd.com
113211799Sbrandon.potter@amd.com
113311799Sbrandon.potter@amd.com/// Target statfs() handler.
11341706SN/Atemplate <class OS>
11351706SN/ASyscallReturn
11366701Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11378852Sandreas.hansson@arm.com           ThreadContext *tc)
11386701Sgblack@eecs.umich.edu{
11396701Sgblack@eecs.umich.edu    std::string path;
11406701Sgblack@eecs.umich.edu
11416701Sgblack@eecs.umich.edu    int index = 0;
11421706SN/A    if (!tc->getMemProxy().tryReadString(path,
11433669Sbinkertn@umich.edu                process->getSyscallArg(tc, index))) {
11443669Sbinkertn@umich.edu        return -EFAULT;
11453669Sbinkertn@umich.edu    }
11461706SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
11471706SN/A
11481706SN/A    // Adjust path for current working directory
11491706SN/A    path = process->fullPath(path);
11502218SN/A
11511706SN/A    struct statfs hostBuf;
115211759Sbrandon.potter@amd.com    int result = statfs(path.c_str(), &hostBuf);
115311799Sbrandon.potter@amd.com
11541706SN/A    if (result < 0)
11551706SN/A        return -errno;
11561706SN/A
11571706SN/A    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11581706SN/A
11591706SN/A    return 0;
11601706SN/A}
11613114Sgblack@eecs.umich.edu
11622680Sktlim@umich.edu
11631706SN/A/// Target fstatfs() handler.
11646701Sgblack@eecs.umich.edutemplate <class OS>
116510931Sbrandon.potter@amd.comSyscallReturn
11666701Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11671706SN/A            ThreadContext *tc)
116810932Sbrandon.potter@amd.com{
116910931Sbrandon.potter@amd.com    int index = 0;
11701706SN/A    int tgt_fd = process->getSyscallArg(tc, index);
11711706SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
11721706SN/A
117310931Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
11741706SN/A    if (sim_fd < 0)
11751706SN/A        return -EBADF;
11762218SN/A
11771706SN/A    struct statfs hostBuf;
117811759Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11791706SN/A
11801706SN/A    if (result < 0)
11811706SN/A        return -errno;
11821706SN/A
11831706SN/A    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11841999SN/A
11851999SN/A    return 0;
11861999SN/A}
11873114Sgblack@eecs.umich.edu
11882680Sktlim@umich.edu
11891999SN/A/// Target writev() handler.
11906701Sgblack@eecs.umich.edutemplate <class OS>
119110931Sbrandon.potter@amd.comSyscallReturn
119210931Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
119310932Sbrandon.potter@amd.com           ThreadContext *tc)
119410931Sbrandon.potter@amd.com{
11951999SN/A    int index = 0;
11961999SN/A    int tgt_fd = process->getSyscallArg(tc, index);
11978852Sandreas.hansson@arm.com
11986701Sgblack@eecs.umich.edu    int sim_fd = process->getSimFD(tgt_fd);
11996701Sgblack@eecs.umich.edu    if (sim_fd < 0)
12001999SN/A        return -EBADF;
12016227Snate@binkert.org
12021999SN/A    SETranslatingPortProxy &p = tc->getMemProxy();
12032461SN/A    uint64_t tiov_base = process->getSyscallArg(tc, index);
12048852Sandreas.hansson@arm.com    size_t count = process->getSyscallArg(tc, index);
12058852Sandreas.hansson@arm.com    struct iovec hiov[count];
12068737Skoansin.tan@gmail.com    for (size_t i = 0; i < count; ++i) {
12071999SN/A        typename OS::tgt_iovec tiov;
12088852Sandreas.hansson@arm.com
12098852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12101999SN/A                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12111999SN/A        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
121210931Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
12131999SN/A        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12146227Snate@binkert.org                   hiov[i].iov_len);
12151999SN/A    }
12161999SN/A
12171999SN/A    int result = writev(sim_fd, hiov, count);
12182218SN/A
12191999SN/A    for (size_t i = 0; i < count; ++i)
122010629Sjthestness@gmail.com        delete [] (char *)hiov[i].iov_base;
12211999SN/A
12221999SN/A    if (result < 0)
122311385Sbrandon.potter@amd.com        return -errno;
1224360SN/A
12251450SN/A    return result;
122611385Sbrandon.potter@amd.com}
122711385Sbrandon.potter@amd.com
1228360SN/A/// Real mmap handler.
12296701Sgblack@eecs.umich.edutemplate <class OS>
12306701Sgblack@eecs.umich.eduSyscallReturn
12316701Sgblack@eecs.umich.edummapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
123211383Sbrandon.potter@amd.com         bool is_mmap2)
123311383Sbrandon.potter@amd.com{
12348324Ssteve.reinhardt@amd.com    int index = 0;
123510486Stjablin@gmail.com    Addr start = p->getSyscallArg(tc, index);
1236360SN/A    uint64_t length = p->getSyscallArg(tc, index);
123711385Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
123811385Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
12399008Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
124011383Sbrandon.potter@amd.com    int offset = p->getSyscallArg(tc, index);
124111383Sbrandon.potter@amd.com
124211383Sbrandon.potter@amd.com    if (is_mmap2)
124311383Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
124411383Sbrandon.potter@amd.com
124511383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
124611383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
124711383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
124811383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
12498324Ssteve.reinhardt@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
125011383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
125111383Sbrandon.potter@amd.com        !length) {
125211383Sbrandon.potter@amd.com        return -EINVAL;
125311383Sbrandon.potter@amd.com    }
125411383Sbrandon.potter@amd.com
125511383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
125611383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
125711383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
125811383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
125911383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
126011383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
126111383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
126211383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
126311383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
126411383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
126511383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
126611383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
126711383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
126811383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
126911383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
127011383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
127111383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
127211383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
127311383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
12748324Ssteve.reinhardt@amd.com        // a call to msync, but that again would require more information
12755877Shsul@eecs.umich.edu        // than we currently maintain.
127610486Stjablin@gmail.com        warn("mmap: writing to shared mmap region is currently "
127710486Stjablin@gmail.com             "unsupported. The write succeeds on the target, but it "
127811383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
127911383Sbrandon.potter@amd.com    }
128011383Sbrandon.potter@amd.com
128111624Smichael.lebeane@amd.com    length = roundUp(length, TheISA::PageBytes);
128211624Smichael.lebeane@amd.com
128311624Smichael.lebeane@amd.com    int sim_fd = -1;
128411624Smichael.lebeane@amd.com    uint8_t *pmap = nullptr;
128511624Smichael.lebeane@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
128611624Smichael.lebeane@amd.com        sim_fd = p->getSimFD(tgt_fd);
128711624Smichael.lebeane@amd.com        if (sim_fd < 0)
128811624Smichael.lebeane@amd.com            return -EBADF;
128911624Smichael.lebeane@amd.com
129011624Smichael.lebeane@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
129111624Smichael.lebeane@amd.com                                    sim_fd, offset);
129211383Sbrandon.potter@amd.com
129311383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
1294360SN/A            warn("mmap: failed to map file into host address space");
129511383Sbrandon.potter@amd.com            return -errno;
129611383Sbrandon.potter@amd.com        }
12978600Ssteve.reinhardt@amd.com    }
129811383Sbrandon.potter@amd.com
129911383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
130011383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
13018600Ssteve.reinhardt@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
13022544SN/A        start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end;
13032544SN/A        p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length;
130411383Sbrandon.potter@amd.com    }
130511383Sbrandon.potter@amd.com
130611383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
130711386Ssteve.reinhardt@amd.com                    start, start + length - 1);
130811386Ssteve.reinhardt@amd.com
130911383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
131011383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
131111383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
131211383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
131311383Sbrandon.potter@amd.com    if (clobber) {
131411383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
131511383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
131611383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
131711383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
131811383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
131911383Sbrandon.potter@amd.com        }
132011383Sbrandon.potter@amd.com    }
132111383Sbrandon.potter@amd.com
132211383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
132311383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
13248600Ssteve.reinhardt@amd.com    // fatal and bail out of the simulation.
13256672Sgblack@eecs.umich.edu    p->allocateMem(start, length, clobber);
13268600Ssteve.reinhardt@amd.com
132711383Sbrandon.potter@amd.com    // Transfer content into target address space.
132811383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
132911383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
13308601Ssteve.reinhardt@amd.com        // In general, we should zero the mapped area for anonymous mappings,
13312544SN/A        // with something like:
133211383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
133311383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
133411383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
133511383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
133611383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
133711383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
133811383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
133911383Sbrandon.potter@amd.com    } else {
134011383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
134111383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
134211383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
134311383Sbrandon.potter@amd.com        // the host into the target address space.
134411383Sbrandon.potter@amd.com        struct stat file_stat;
134511383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
134611383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
134711383Sbrandon.potter@amd.com
134811383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
134911383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
135011383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
135111383Sbrandon.potter@amd.com        // on the specified offset into the file.
135211383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
135311383Sbrandon.potter@amd.com                                 length);
135411383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
135511383Sbrandon.potter@amd.com
135611383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
135711383Sbrandon.potter@amd.com        munmap(pmap, length);
135811383Sbrandon.potter@amd.com
135911383Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
136011383Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
136111383Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
136211383Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
136311383Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
136411392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
136511392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
136611392Sbrandon.potter@amd.com        if (interpreter) {
136711392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
136811392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
136911392Sbrandon.potter@amd.com
137011392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
137111392Sbrandon.potter@amd.com
137211392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
137311392Sbrandon.potter@amd.com                FDEntry *fde = p->getFDEntry(tgt_fd);
137411392Sbrandon.potter@amd.com
137511392Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(fde->filename);
137611392Sbrandon.potter@amd.com
137711392Sbrandon.potter@amd.com                if (lib) {
137811392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
137911392Sbrandon.potter@amd.com                                        lib->textBase(), start);
138011392Sbrandon.potter@amd.com                }
138111392Sbrandon.potter@amd.com            }
138211392Sbrandon.potter@amd.com        }
138311392Sbrandon.potter@amd.com
138411392Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
138511392Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
138611392Sbrandon.potter@amd.com        // execution (hopefully).
138711392Sbrandon.potter@amd.com    }
138811392Sbrandon.potter@amd.com
138911383Sbrandon.potter@amd.com    return start;
139011383Sbrandon.potter@amd.com}
139111383Sbrandon.potter@amd.com
139211383Sbrandon.potter@amd.comtemplate <class OS>
139311383Sbrandon.potter@amd.comSyscallReturn
13941458SN/Apwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1395360SN/A{
1396360SN/A    int index = 0;
139711593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
139811593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
139911593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
140011593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
140111593Santhony.gutierrez@amd.com
140211593Santhony.gutierrez@amd.com    int sim_fd = p->getSimFD(tgt_fd);
140311593Santhony.gutierrez@amd.com    if (sim_fd < 0)
140411593Santhony.gutierrez@amd.com        return -EBADF;
140511593Santhony.gutierrez@amd.com
140611593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
140711593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
140811593Santhony.gutierrez@amd.com
140911593Santhony.gutierrez@amd.com    int bytes_written = pwrite64(sim_fd, bufArg.bufferPtr(), nbytes, offset);
141011593Santhony.gutierrez@amd.com
141111593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
141211593Santhony.gutierrez@amd.com}
141311593Santhony.gutierrez@amd.com
141411594Santhony.gutierrez@amd.com/// Target mmap() handler.
141511593Santhony.gutierrez@amd.comtemplate <class OS>
141611593Santhony.gutierrez@amd.comSyscallReturn
141711593Santhony.gutierrez@amd.commmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
141811593Santhony.gutierrez@amd.com{
141911385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
142011385Sbrandon.potter@amd.com}
142111385Sbrandon.potter@amd.com
142211385Sbrandon.potter@amd.com/// Target mmap2() handler.
142311385Sbrandon.potter@amd.comtemplate <class OS>
142411385Sbrandon.potter@amd.comSyscallReturn
142511385Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
142611385Sbrandon.potter@amd.com{
142711385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
142811385Sbrandon.potter@amd.com}
142911385Sbrandon.potter@amd.com
143011385Sbrandon.potter@amd.com/// Target getrlimit() handler.
143111385Sbrandon.potter@amd.comtemplate <class OS>
143211385Sbrandon.potter@amd.comSyscallReturn
143311385Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
143411385Sbrandon.potter@amd.com        ThreadContext *tc)
1435378SN/A{
1436360SN/A    int index = 0;
14371450SN/A    unsigned resource = process->getSyscallArg(tc, index);
14383114Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
14392680Sktlim@umich.edu
1440360SN/A    switch (resource) {
14416701Sgblack@eecs.umich.edu        case OS::TGT_RLIMIT_STACK:
14426701Sgblack@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
14436701Sgblack@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
1444360SN/A            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1445360SN/A            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14462064SN/A            break;
14475877Shsul@eecs.umich.edu
14482064SN/A        case OS::TGT_RLIMIT_DATA:
14498737Skoansin.tan@gmail.com            // max data segment size in bytes: make up a number
14508737Skoansin.tan@gmail.com            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
14512064SN/A            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
1452360SN/A            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14535877Shsul@eecs.umich.edu            break;
14545877Shsul@eecs.umich.edu
14555877Shsul@eecs.umich.edu        default:
14568737Skoansin.tan@gmail.com            warn("getrlimit: unimplemented resource %d", resource);
14578737Skoansin.tan@gmail.com            return -EINVAL;
14585877Shsul@eecs.umich.edu            break;
14595877Shsul@eecs.umich.edu    }
14602064SN/A
146110794Sbrandon.potter@amd.com    rlp.copyOut(tc->getMemProxy());
146210794Sbrandon.potter@amd.com    return 0;
14632064SN/A}
1464360SN/A
1465360SN/A/// Target clock_gettime() function.
14668706Sandreas.hansson@arm.comtemplate <class OS>
14671458SN/ASyscallReturn
1468360SN/Aclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1469360SN/A{
147010796Sbrandon.potter@amd.com    int index = 1;
147110796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
147210796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
147310796Sbrandon.potter@amd.com
147410796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
147510796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
147610796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
147710796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
147810796Sbrandon.potter@amd.com
147910796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
148010796Sbrandon.potter@amd.com
148110796Sbrandon.potter@amd.com    return 0;
148210796Sbrandon.potter@amd.com}
148310796Sbrandon.potter@amd.com
148410796Sbrandon.potter@amd.com/// Target clock_getres() function.
148510796Sbrandon.potter@amd.comtemplate <class OS>
148610796Sbrandon.potter@amd.comSyscallReturn
148710796Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
148810796Sbrandon.potter@amd.com{
148911337SMichael.Lebeane@amd.com    int index = 1;
149011337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
149111337SMichael.Lebeane@amd.com
149211337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
149311337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
149411337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
149511337SMichael.Lebeane@amd.com
149611337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
149711337SMichael.Lebeane@amd.com
149811337SMichael.Lebeane@amd.com    return 0;
149911337SMichael.Lebeane@amd.com}
150011337SMichael.Lebeane@amd.com
150111337SMichael.Lebeane@amd.com/// Target gettimeofday() handler.
150211337SMichael.Lebeane@amd.comtemplate <class OS>
150311337SMichael.Lebeane@amd.comSyscallReturn
150411337SMichael.Lebeane@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
150511337SMichael.Lebeane@amd.com        ThreadContext *tc)
1506378SN/A{
1507360SN/A    int index = 0;
15081450SN/A    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
15093114Sgblack@eecs.umich.edu
15102680Sktlim@umich.edu    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1511360SN/A    tp->tv_sec += seconds_since_epoch;
15126701Sgblack@eecs.umich.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
15136701Sgblack@eecs.umich.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1514360SN/A
151510796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
1516360SN/A
15176109Ssanchezd@stanford.edu    return 0;
15186109Ssanchezd@stanford.edu}
1519360SN/A
15208706Sandreas.hansson@arm.com
1521360SN/A/// Target utimes() handler.
15221458SN/Atemplate <class OS>
1523360SN/ASyscallReturn
1524360SN/AutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1525360SN/A           ThreadContext *tc)
15261999SN/A{
15271999SN/A    std::string path;
15281999SN/A
15293114Sgblack@eecs.umich.edu    int index = 0;
15302680Sktlim@umich.edu    if (!tc->getMemProxy().tryReadString(path,
15311999SN/A                process->getSyscallArg(tc, index))) {
15321999SN/A        return -EFAULT;
15331999SN/A    }
15346701Sgblack@eecs.umich.edu
15358852Sandreas.hansson@arm.com    TypedBufferArg<typename OS::timeval [2]>
15366701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
15376701Sgblack@eecs.umich.edu    tp.copyIn(tc->getMemProxy());
15386701Sgblack@eecs.umich.edu
15391999SN/A    struct timeval hostTimeval[2];
15406701Sgblack@eecs.umich.edu    for (int i = 0; i < 2; ++i)
15416701Sgblack@eecs.umich.edu    {
15428706Sandreas.hansson@arm.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
15431999SN/A        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
15441999SN/A    }
15451999SN/A
15461999SN/A    // Adjust path for current working directory
15478737Skoansin.tan@gmail.com    path = process->fullPath(path);
15488737Skoansin.tan@gmail.com
15491999SN/A    int result = utimes(path.c_str(), hostTimeval);
15503669Sbinkertn@umich.edu
15513669Sbinkertn@umich.edu    if (result < 0)
15523669Sbinkertn@umich.edu        return -errno;
15533669Sbinkertn@umich.edu
15541999SN/A    return 0;
15551999SN/A}
15561999SN/A/// Target getrusage() function.
15571999SN/Atemplate <class OS>
15581999SN/ASyscallReturn
15591999SN/AgetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15601999SN/A              ThreadContext *tc)
1561378SN/A{
1562360SN/A    int index = 0;
15631450SN/A    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
15643114Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
15652680Sktlim@umich.edu
1566360SN/A    rup->ru_utime.tv_sec = 0;
15676701Sgblack@eecs.umich.edu    rup->ru_utime.tv_usec = 0;
15686701Sgblack@eecs.umich.edu    rup->ru_stime.tv_sec = 0;
15696701Sgblack@eecs.umich.edu    rup->ru_stime.tv_usec = 0;
1570360SN/A    rup->ru_maxrss = 0;
15713670Sbinkertn@umich.edu    rup->ru_ixrss = 0;
15723670Sbinkertn@umich.edu    rup->ru_idrss = 0;
1573360SN/A    rup->ru_isrss = 0;
1574360SN/A    rup->ru_minflt = 0;
1575360SN/A    rup->ru_majflt = 0;
1576360SN/A    rup->ru_nswap = 0;
1577360SN/A    rup->ru_inblock = 0;
1578360SN/A    rup->ru_oublock = 0;
1579360SN/A    rup->ru_msgsnd = 0;
1580360SN/A    rup->ru_msgrcv = 0;
1581360SN/A    rup->ru_nsignals = 0;
1582360SN/A    rup->ru_nvcsw = 0;
1583360SN/A    rup->ru_nivcsw = 0;
1584360SN/A
1585360SN/A    switch (who) {
1586360SN/A      case OS::TGT_RUSAGE_SELF:
1587360SN/A        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
1588360SN/A        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
1589360SN/A        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
15903670Sbinkertn@umich.edu        break;
15913670Sbinkertn@umich.edu
159210796Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_CHILDREN:
15938737Skoansin.tan@gmail.com        // do nothing.  We have no child processes, so they take no time.
15948737Skoansin.tan@gmail.com        break;
15953670Sbinkertn@umich.edu
15963670Sbinkertn@umich.edu      default:
15973670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
15983670Sbinkertn@umich.edu        // plow ahead
15993670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
16003670Sbinkertn@umich.edu             who);
16013670Sbinkertn@umich.edu    }
16023670Sbinkertn@umich.edu
16033670Sbinkertn@umich.edu    rup.copyOut(tc->getMemProxy());
16043670Sbinkertn@umich.edu
16053670Sbinkertn@umich.edu    return 0;
16063670Sbinkertn@umich.edu}
16073670Sbinkertn@umich.edu
16088706Sandreas.hansson@arm.com/// Target times() function.
1609360SN/Atemplate <class OS>
16101458SN/ASyscallReturn
1611360SN/AtimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1612360SN/A           ThreadContext *tc)
16136683Stjones1@inf.ed.ac.uk{
16146683Stjones1@inf.ed.ac.uk    int index = 0;
16156683Stjones1@inf.ed.ac.uk    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
16166683Stjones1@inf.ed.ac.uk
16176683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
16186683Stjones1@inf.ed.ac.uk    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
16196701Sgblack@eecs.umich.edu    bufp->tms_utime = clocks;
16206701Sgblack@eecs.umich.edu    bufp->tms_stime = 0;
16216683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
16226683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
16237823Ssteve.reinhardt@amd.com
16246683Stjones1@inf.ed.ac.uk    // Convert to host endianness
16256683Stjones1@inf.ed.ac.uk    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
16266683Stjones1@inf.ed.ac.uk
16276683Stjones1@inf.ed.ac.uk    // Write back
16286683Stjones1@inf.ed.ac.uk    bufp.copyOut(tc->getMemProxy());
16296683Stjones1@inf.ed.ac.uk
16308737Skoansin.tan@gmail.com    // Return clock ticks since system boot
16316683Stjones1@inf.ed.ac.uk    return clocks;
16326683Stjones1@inf.ed.ac.uk}
16338706Sandreas.hansson@arm.com
16346683Stjones1@inf.ed.ac.uk/// Target time() function.
16356683Stjones1@inf.ed.ac.uktemplate <class OS>
16366683Stjones1@inf.ed.ac.ukSyscallReturn
16376683Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
16382553SN/A           ThreadContext *tc)
16396684Stjones1@inf.ed.ac.uk{
16406684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
16416684Stjones1@inf.ed.ac.uk    getElapsedTimeMicro(sec, usec);
16426684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
16436684Stjones1@inf.ed.ac.uk
16446684Stjones1@inf.ed.ac.uk    int index = 0;
16456684Stjones1@inf.ed.ac.uk    Addr taddr = (Addr)process->getSyscallArg(tc, index);
164610796Sbrandon.potter@amd.com    if (taddr != 0) {
16476684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
16486684Stjones1@inf.ed.ac.uk        t = TheISA::htog(t);
16496701Sgblack@eecs.umich.edu        SETranslatingPortProxy &p = tc->getMemProxy();
16506701Sgblack@eecs.umich.edu        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
165111321Ssteve.reinhardt@amd.com    }
16526684Stjones1@inf.ed.ac.uk    return sec;
16538737Skoansin.tan@gmail.com}
16548852Sandreas.hansson@arm.com
16558852Sandreas.hansson@arm.com
16566684Stjones1@inf.ed.ac.uk#endif // __SIM_SYSCALL_EMUL_HH__
16576684Stjones1@inf.ed.ac.uk