syscall_emul.hh revision 11385
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
52360SN/A///
53360SN/A/// @file syscall_emul.hh
54360SN/A///
55360SN/A/// This file defines objects used to emulate syscalls from the target
56360SN/A/// application on the host machine.
57360SN/A
581809SN/A#ifdef __CYGWIN32__
595543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
601809SN/A#endif
6111383Sbrandon.potter@amd.com#include <sys/mman.h>
623113Sgblack@eecs.umich.edu#include <sys/stat.h>
638229Snate@binkert.org#include <sys/time.h>
648229Snate@binkert.org#include <sys/uio.h>
653113Sgblack@eecs.umich.edu#include <fcntl.h>
667075Snate@binkert.org
678229Snate@binkert.org#include <cerrno>
687075Snate@binkert.org#include <string>
69360SN/A
702474SN/A#include "base/chunk_generator.hh"
715543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
722462SN/A#include "base/misc.hh"
731354SN/A#include "base/trace.hh"
746216Snate@binkert.org#include "base/types.hh"
756658Snate@binkert.org#include "config/the_isa.hh"
762474SN/A#include "cpu/base.hh"
772680Sktlim@umich.edu#include "cpu/thread_context.hh"
7811380Salexandru.dutu@amd.com#include "debug/SyscallBase.hh"
798232Snate@binkert.org#include "debug/SyscallVerbose.hh"
808229Snate@binkert.org#include "mem/page_table.hh"
817678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
8210496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
838229Snate@binkert.org#include "sim/process.hh"
8410497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
858766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
866640Svince@csl.cornell.edu#include "sim/system.hh"
87360SN/A
8811380Salexandru.dutu@amd.com// This wrapper macro helps out with readability a bit. FLAGEXT specifies
8911380Salexandru.dutu@amd.com// the verbosity and FMT is the message to be appended to the syscall
9011380Salexandru.dutu@amd.com// header information. The syscall header information contains the cpuid
9111380Salexandru.dutu@amd.com// and thread id.
9211380Salexandru.dutu@amd.com#define DPRINTF_SYSCALL(FLAGEXT, FMT, ...)                                  \
9311380Salexandru.dutu@amd.com    DPRINTFS(Syscall##FLAGEXT, tc->getCpuPtr(), "T%d : syscall " FMT,       \
9411380Salexandru.dutu@amd.com             tc->threadId(), __VA_ARGS__)
9511380Salexandru.dutu@amd.com
96360SN/A///
97360SN/A/// System call descriptor.
98360SN/A///
99360SN/Aclass SyscallDesc {
100360SN/A
101360SN/A  public:
102360SN/A
103378SN/A    /// Typedef for target syscall handler functions.
1041450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
1053114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
106360SN/A
1075543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
1085543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
1095543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
11010831Ssteve.reinhardt@amd.com    bool warned;        //!< Have we warned about unimplemented syscall?
111360SN/A
112360SN/A    /// Flag values for controlling syscall behavior.
113360SN/A    enum Flags {
114360SN/A        /// Don't set return regs according to funcPtr return value.
115360SN/A        /// Used for syscalls with non-standard return conventions
1162680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
117360SN/A        /// sigreturn).
11810831Ssteve.reinhardt@amd.com        SuppressReturnValue = 1,
11910831Ssteve.reinhardt@amd.com        WarnOnce = 2
120360SN/A    };
121360SN/A
122360SN/A    /// Constructor.
123360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
12410831Ssteve.reinhardt@amd.com        : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
125360SN/A    {
126360SN/A    }
127360SN/A
128360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1293114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
13010831Ssteve.reinhardt@amd.com
13110831Ssteve.reinhardt@amd.com    /// Is the WarnOnce flag set?
13210831Ssteve.reinhardt@amd.com    bool warnOnce() const {  return (flags & WarnOnce); }
133360SN/A};
134360SN/A
135360SN/A
136360SN/A//////////////////////////////////////////////////////////////////////
137360SN/A//
138360SN/A// The following emulation functions are generic enough that they
139360SN/A// don't need to be recompiled for different emulated OS's.  They are
140360SN/A// defined in sim/syscall_emul.cc.
141360SN/A//
142360SN/A//////////////////////////////////////////////////////////////////////
143360SN/A
144360SN/A
145378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1461706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1473114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
148378SN/A
149378SN/A/// Handler for unimplemented syscalls that we never intend to
150378SN/A/// implement (signal handling, etc.) and should not affect the correct
151378SN/A/// behavior of the program.  Print a warning only if the appropriate
152378SN/A/// trace flag is enabled.  Return success to the target program.
1531706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1543114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
155360SN/A
1566109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1571706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1583114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
159378SN/A
1606109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1616109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1626109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1636109Ssanchezd@stanford.edu
164378SN/A/// Target getpagesize() handler.
1651706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1663114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
167378SN/A
1685748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1695748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1705748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
171378SN/A
172378SN/A/// Target close() handler.
1731706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1743114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
175378SN/A
176378SN/A/// Target read() handler.
1771706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1783114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
179378SN/A
180378SN/A/// Target write() handler.
1811706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1823114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
183378SN/A
184378SN/A/// Target lseek() handler.
1851706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1863114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
187378SN/A
1884118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1894118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1904118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1914118Sgblack@eecs.umich.edu
192378SN/A/// Target munmap() handler.
1931706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1943114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
195378SN/A
196378SN/A/// Target gethostname() handler.
1971706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1983114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
199360SN/A
2005513SMichael.Adler@intel.com/// Target getcwd() handler.
2015513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2025513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2035513SMichael.Adler@intel.com
20410203SAli.Saidi@ARM.com/// Target readlink() handler.
20510203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
20610203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
20710203SAli.Saidi@ARM.com                           int index = 0);
2085513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2095513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2105513SMichael.Adler@intel.com
211511SN/A/// Target unlink() handler.
21210633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
21310633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
21410633Smichaelupton@gmail.com                           int index);
2151706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2163114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
217511SN/A
2185513SMichael.Adler@intel.com/// Target mkdir() handler.
2195513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2205513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2215513SMichael.Adler@intel.com
222511SN/A/// Target rename() handler.
2231706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2251706SN/A
2261706SN/A
2271706SN/A/// Target truncate() handler.
2281706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2293114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2301706SN/A
2311706SN/A
2321706SN/A/// Target ftruncate() handler.
2331706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2343114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2351706SN/A
236511SN/A
2376703Svince@csl.cornell.edu/// Target truncate64() handler.
2386703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2396703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2406703Svince@csl.cornell.edu
2416685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2426685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2436685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2446685Stjones1@inf.ed.ac.uk
2456685Stjones1@inf.ed.ac.uk
2465513SMichael.Adler@intel.com/// Target umask() handler.
2475513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2485513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2495513SMichael.Adler@intel.com
2505513SMichael.Adler@intel.com
2511999SN/A/// Target chown() handler.
2521999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2533114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2541999SN/A
2551999SN/A
2561999SN/A/// Target fchown() handler.
2571999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2583114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2591999SN/A
2603079Sstever@eecs.umich.edu/// Target dup() handler.
2613079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2623114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2633079Sstever@eecs.umich.edu
2642093SN/A/// Target fnctl() handler.
2652093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2663114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2672093SN/A
2682687Sksewell@umich.edu/// Target fcntl64() handler.
2692687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2703114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2712687Sksewell@umich.edu
2722238SN/A/// Target setuid() handler.
2732238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2743114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2752238SN/A
2762238SN/A/// Target getpid() handler.
2772238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2783114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2792238SN/A
2802238SN/A/// Target getuid() handler.
2812238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2823114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2832238SN/A
2842238SN/A/// Target getgid() handler.
2852238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2863114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2872238SN/A
2882238SN/A/// Target getppid() handler.
2892238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2903114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2912238SN/A
2922238SN/A/// Target geteuid() handler.
2932238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2943114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2952238SN/A
2962238SN/A/// Target getegid() handler.
2972238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2983114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2992238SN/A
3006109Ssanchezd@stanford.edu/// Target clone() handler.
3016109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3026109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3032238SN/A
3049455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3059455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3069455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
30710203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
30810203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
30910203SAli.Saidi@ARM.com                               int index);
3109455Smitch.hayenga+gem5@gmail.com
3119112Smarc.orr@gmail.com/// Futex system call
3129112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3139112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3149112Smarc.orr@gmail.comtemplate <class OS>
3159112Smarc.orr@gmail.comSyscallReturn
3169112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3179112Smarc.orr@gmail.com          ThreadContext *tc)
3189112Smarc.orr@gmail.com{
3199112Smarc.orr@gmail.com    int index_uaddr = 0;
3209112Smarc.orr@gmail.com    int index_op = 1;
3219112Smarc.orr@gmail.com    int index_val = 2;
3229112Smarc.orr@gmail.com    int index_timeout = 3;
3239112Smarc.orr@gmail.com
3249112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3259112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3269112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3279112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3289112Smarc.orr@gmail.com
3299112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3309112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3319112Smarc.orr@gmail.com
3329112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3339112Smarc.orr@gmail.com            uaddr, op, val);
3349112Smarc.orr@gmail.com
3359238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3369112Smarc.orr@gmail.com
3379112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3389112Smarc.orr@gmail.com        if (timeout != 0) {
3399112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3409112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3419112Smarc.orr@gmail.com        }
3429112Smarc.orr@gmail.com
3439112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3449112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3459112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
34611367Sandreas.hansson@arm.com        delete[] buf;
3479112Smarc.orr@gmail.com
34811321Ssteve.reinhardt@amd.com        if (val != mem_val) {
3499112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3509112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3519112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3529112Smarc.orr@gmail.com        }
3539112Smarc.orr@gmail.com
3549112Smarc.orr@gmail.com        // Queue the thread context
3559112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3569112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3579112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3589112Smarc.orr@gmail.com        } else {
3599112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3609112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3619112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3629112Smarc.orr@gmail.com        }
3639112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3649112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3659112Smarc.orr@gmail.com                                "thread context\n");
3669112Smarc.orr@gmail.com        tc->suspend();
3679112Smarc.orr@gmail.com        return 0;
3689112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3699112Smarc.orr@gmail.com        int wokenUp = 0;
3709112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3719112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3729112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3739112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3749112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3759112Smarc.orr@gmail.com                tcWaitList->pop_front();
3769112Smarc.orr@gmail.com                wokenUp++;
3779112Smarc.orr@gmail.com            }
37811321Ssteve.reinhardt@amd.com            if (tcWaitList->empty()) {
3799112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3809112Smarc.orr@gmail.com                delete tcWaitList;
3819112Smarc.orr@gmail.com            }
3829112Smarc.orr@gmail.com        }
3839112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3849112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3859112Smarc.orr@gmail.com        return wokenUp;
3869112Smarc.orr@gmail.com    } else {
3879238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3889112Smarc.orr@gmail.com        return 0;
3899112Smarc.orr@gmail.com    }
3909112Smarc.orr@gmail.com
3919112Smarc.orr@gmail.com}
3929112Smarc.orr@gmail.com
3932238SN/A
3942238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3952238SN/A/// returning a second value in a register other than the normal return register
3962238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3973114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3982238SN/A
3992238SN/A/// Target getpidPseudo() handler.
4002238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4013114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4022238SN/A
4032238SN/A/// Target getuidPseudo() handler.
4042238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4053114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4062238SN/A
4072238SN/A/// Target getgidPseudo() handler.
4082238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4093114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4102238SN/A
4112238SN/A
4121354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4131354SN/Aconst int one_million = 1000000;
41410796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
41510796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4161354SN/A
4171354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4181354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4191354SN/A/// real-world time) to keep simulations repeatable.
4201354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4211354SN/A
4221354SN/A/// Helper function to convert current elapsed time to seconds and
4231354SN/A/// microseconds.
4241354SN/Atemplate <class T1, class T2>
4251354SN/Avoid
42610796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4271354SN/A{
42810796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4291354SN/A    sec = elapsed_usecs / one_million;
4301354SN/A    usec = elapsed_usecs % one_million;
4311354SN/A}
4321354SN/A
43310796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
43410796Sbrandon.potter@amd.com/// nanoseconds.
43510796Sbrandon.potter@amd.comtemplate <class T1, class T2>
43610796Sbrandon.potter@amd.comvoid
43710796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
43810796Sbrandon.potter@amd.com{
43910796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
44010796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
44110796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
44210796Sbrandon.potter@amd.com}
44310796Sbrandon.potter@amd.com
444360SN/A//////////////////////////////////////////////////////////////////////
445360SN/A//
446360SN/A// The following emulation functions are generic, but need to be
447360SN/A// templated to account for differences in types, constants, etc.
448360SN/A//
449360SN/A//////////////////////////////////////////////////////////////////////
450360SN/A
4513113Sgblack@eecs.umich.edu#if NO_STAT64
4523113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4533113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4543113Sgblack@eecs.umich.edu#else
4553113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4563113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4573113Sgblack@eecs.umich.edu#endif
4583113Sgblack@eecs.umich.edu
4593113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4603113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4613113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4623113Sgblack@eecs.umich.edu
4633113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4643113Sgblack@eecs.umich.edustatic void
4653113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4663113Sgblack@eecs.umich.edu{
4674189Sgblack@eecs.umich.edu    using namespace TheISA;
4684189Sgblack@eecs.umich.edu
4693113Sgblack@eecs.umich.edu    if (fakeTTY)
4703113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4713113Sgblack@eecs.umich.edu    else
4723113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4738737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4743113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4758737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4763277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4775515SMichael.Adler@intel.com    if (fakeTTY) {
4785515SMichael.Adler@intel.com        // Claim to be a character device
4795515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4805515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4815515SMichael.Adler@intel.com    }
4828737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4833277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4848737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4853277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4868737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4873277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4888737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4893113Sgblack@eecs.umich.edu    if (fakeTTY)
4903113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4913113Sgblack@eecs.umich.edu    else
4923113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4938737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4943113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4958737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4963114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4978737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4983114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4998737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5003114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5018737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5024061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5034061Sgblack@eecs.umich.edu    // consistently across different hosts.
5044061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5058737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5063113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5078737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5083113Sgblack@eecs.umich.edu}
5093113Sgblack@eecs.umich.edu
5103113Sgblack@eecs.umich.edu// Same for stat64
5113113Sgblack@eecs.umich.edu
5123113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5133113Sgblack@eecs.umich.edustatic void
5143113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5153113Sgblack@eecs.umich.edu{
5164189Sgblack@eecs.umich.edu    using namespace TheISA;
5174189Sgblack@eecs.umich.edu
5183113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5193113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5203113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5218737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5223113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5238737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5243113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5258737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5263113Sgblack@eecs.umich.edu#else
5273113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5283113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5293113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5303113Sgblack@eecs.umich.edu#endif
5313113Sgblack@eecs.umich.edu}
5323113Sgblack@eecs.umich.edu
5333113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5343113Sgblack@eecs.umich.edutemplate<class OS>
5353113Sgblack@eecs.umich.edustatic void
5368852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5373113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5383113Sgblack@eecs.umich.edu{
5393113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5403113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5413113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5423113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5433113Sgblack@eecs.umich.edu}
5443113Sgblack@eecs.umich.edu
5453113Sgblack@eecs.umich.edutemplate<class OS>
5463113Sgblack@eecs.umich.edustatic void
5478852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5483113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5493113Sgblack@eecs.umich.edu{
5503113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5513113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5526686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5533113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5543113Sgblack@eecs.umich.edu}
5553113Sgblack@eecs.umich.edu
556378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
557378SN/A/// only to find out if their stdout is a tty, to determine whether to
5589141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5599141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
560360SN/Atemplate <class OS>
5611450SN/ASyscallReturn
5623114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5632680Sktlim@umich.edu          ThreadContext *tc)
564360SN/A{
5656701Sgblack@eecs.umich.edu    int index = 0;
56610930Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5676701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
568360SN/A
56910930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
570360SN/A
57110932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
57210496Ssteve.reinhardt@amd.com
57310930Sbrandon.potter@amd.com    if (fde == NULL) {
574360SN/A        // doesn't map to any simulator fd: not a valid target fd
5751458SN/A        return -EBADF;
576360SN/A    }
577360SN/A
57810930Sbrandon.potter@amd.com    if (fde->driver != NULL) {
57910930Sbrandon.potter@amd.com        return fde->driver->ioctl(process, tc, req);
58010496Ssteve.reinhardt@amd.com    }
58110496Ssteve.reinhardt@amd.com
5829141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5831458SN/A        return -ENOTTY;
5849141Smarc.orr@gmail.com    }
585360SN/A
5869141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
58710930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
5889141Smarc.orr@gmail.com    return -ENOTTY;
589360SN/A}
590360SN/A
591360SN/Atemplate <class OS>
59210027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5933114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
59410027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
595360SN/A{
596360SN/A    std::string path;
597360SN/A
5988852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5996701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6001458SN/A        return -EFAULT;
601360SN/A
6026701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6036701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
604360SN/A    int hostFlags = 0;
605360SN/A
606360SN/A    // translate open flags
607360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
608360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
609360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
610360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
611360SN/A        }
612360SN/A    }
613360SN/A
614360SN/A    // any target flags left?
615360SN/A    if (tgtFlags != 0)
6161706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
617360SN/A
618360SN/A#ifdef __CYGWIN32__
619360SN/A    hostFlags |= O_BINARY;
620360SN/A#endif
621360SN/A
6223669Sbinkertn@umich.edu    // Adjust path for current working directory
6233669Sbinkertn@umich.edu    path = process->fullPath(path);
6243669Sbinkertn@umich.edu
6251706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6261706SN/A
62710496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
62810496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
62910496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
63010496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
63110496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
63210496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
63310496Ssteve.reinhardt@amd.com            return -ENOENT;
63410496Ssteve.reinhardt@amd.com        }
63510496Ssteve.reinhardt@amd.com
63610496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
63710496Ssteve.reinhardt@amd.com        if (drv != NULL) {
63810496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
63910496Ssteve.reinhardt@amd.com            // process if necessary.
64010496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
64110496Ssteve.reinhardt@amd.com        }
64210496Ssteve.reinhardt@amd.com
64310496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
64410496Ssteve.reinhardt@amd.com        // /dev/zero
64510496Ssteve.reinhardt@amd.com    }
64610496Ssteve.reinhardt@amd.com
6475795Ssaidi@eecs.umich.edu    int fd;
6489143Ssteve.reinhardt@amd.com    int local_errno;
6499142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6509142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6519143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6525795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6539143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6545795Ssaidi@eecs.umich.edu     } else {
6555795Ssaidi@eecs.umich.edu        // open the file
6565795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6579143Ssteve.reinhardt@amd.com        local_errno = errno;
6585795Ssaidi@eecs.umich.edu     }
659360SN/A
6609143Ssteve.reinhardt@amd.com    if (fd == -1)
6619143Ssteve.reinhardt@amd.com        return -local_errno;
6629143Ssteve.reinhardt@amd.com
66310932Sbrandon.potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
664360SN/A}
665360SN/A
66610027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
66710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
66810027SChris.Adeniyi-Jones@arm.comSyscallReturn
66910027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67110027SChris.Adeniyi-Jones@arm.com{
67210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
67310027SChris.Adeniyi-Jones@arm.com}
67410027SChris.Adeniyi-Jones@arm.com
67510027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
67610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67710027SChris.Adeniyi-Jones@arm.comSyscallReturn
67810027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67910027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
68010027SChris.Adeniyi-Jones@arm.com{
68110027SChris.Adeniyi-Jones@arm.com    int index = 0;
68210027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
68310027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
68410027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
68510027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
68610027SChris.Adeniyi-Jones@arm.com}
68710027SChris.Adeniyi-Jones@arm.com
68810633Smichaelupton@gmail.com/// Target unlinkat() handler.
68910633Smichaelupton@gmail.comtemplate <class OS>
69010633Smichaelupton@gmail.comSyscallReturn
69110633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69210633Smichaelupton@gmail.com             ThreadContext *tc)
69310633Smichaelupton@gmail.com{
69410633Smichaelupton@gmail.com    int index = 0;
69510633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
69610633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
69710633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
69810633Smichaelupton@gmail.com
69910633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
70010633Smichaelupton@gmail.com}
70110633Smichaelupton@gmail.com
70210203SAli.Saidi@ARM.com/// Target facessat() handler
70310203SAli.Saidi@ARM.comtemplate <class OS>
70410203SAli.Saidi@ARM.comSyscallReturn
70510203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70610203SAli.Saidi@ARM.com        ThreadContext *tc)
70710203SAli.Saidi@ARM.com{
70810203SAli.Saidi@ARM.com    int index = 0;
70910203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71010203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71110203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
71210203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
71310203SAli.Saidi@ARM.com}
71410203SAli.Saidi@ARM.com
71510203SAli.Saidi@ARM.com/// Target readlinkat() handler
71610203SAli.Saidi@ARM.comtemplate <class OS>
71710203SAli.Saidi@ARM.comSyscallReturn
71810203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71910203SAli.Saidi@ARM.com        ThreadContext *tc)
72010203SAli.Saidi@ARM.com{
72110203SAli.Saidi@ARM.com    int index = 0;
72210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
72310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
72410203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72510203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
72610203SAli.Saidi@ARM.com}
72710203SAli.Saidi@ARM.com
72810850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
72910850SGiacomo.Gabrielli@arm.comtemplate <class OS>
73010850SGiacomo.Gabrielli@arm.comSyscallReturn
73110850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
73210850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
73310850SGiacomo.Gabrielli@arm.com{
73410850SGiacomo.Gabrielli@arm.com    int index = 0;
73510850SGiacomo.Gabrielli@arm.com
73610850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
73710850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
73810850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
73910850SGiacomo.Gabrielli@arm.com
74010850SGiacomo.Gabrielli@arm.com    std::string old_name;
74110850SGiacomo.Gabrielli@arm.com
74210850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
74310850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
74410850SGiacomo.Gabrielli@arm.com        return -EFAULT;
74510850SGiacomo.Gabrielli@arm.com
74610850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
74710850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
74810850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
74910850SGiacomo.Gabrielli@arm.com
75010850SGiacomo.Gabrielli@arm.com    std::string new_name;
75110850SGiacomo.Gabrielli@arm.com
75210850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
75310850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
75410850SGiacomo.Gabrielli@arm.com        return -EFAULT;
75510850SGiacomo.Gabrielli@arm.com
75610850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
75710850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
75810850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
75910850SGiacomo.Gabrielli@arm.com
76010850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
76110850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
76210850SGiacomo.Gabrielli@arm.com}
76310850SGiacomo.Gabrielli@arm.com
7646640Svince@csl.cornell.edu/// Target sysinfo() handler.
7656640Svince@csl.cornell.edutemplate <class OS>
7666640Svince@csl.cornell.eduSyscallReturn
7676640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7686640Svince@csl.cornell.edu         ThreadContext *tc)
7696640Svince@csl.cornell.edu{
7706640Svince@csl.cornell.edu
7716701Sgblack@eecs.umich.edu    int index = 0;
7726701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
77310793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7746640Svince@csl.cornell.edu
7756701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7766701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7776640Svince@csl.cornell.edu
7788706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7796640Svince@csl.cornell.edu
7806701Sgblack@eecs.umich.edu    return 0;
7816640Svince@csl.cornell.edu}
782360SN/A
7831999SN/A/// Target chmod() handler.
7841999SN/Atemplate <class OS>
7851999SN/ASyscallReturn
7863114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7872680Sktlim@umich.edu          ThreadContext *tc)
7881999SN/A{
7891999SN/A    std::string path;
7901999SN/A
7916701Sgblack@eecs.umich.edu    int index = 0;
7928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7936701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7941999SN/A        return -EFAULT;
7956701Sgblack@eecs.umich.edu    }
7961999SN/A
7976701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7981999SN/A    mode_t hostMode = 0;
7991999SN/A
8001999SN/A    // XXX translate mode flags via OS::something???
8011999SN/A    hostMode = mode;
8021999SN/A
8033669Sbinkertn@umich.edu    // Adjust path for current working directory
8043669Sbinkertn@umich.edu    path = process->fullPath(path);
8053669Sbinkertn@umich.edu
8061999SN/A    // do the chmod
8071999SN/A    int result = chmod(path.c_str(), hostMode);
8081999SN/A    if (result < 0)
8092218SN/A        return -errno;
8101999SN/A
8111999SN/A    return 0;
8121999SN/A}
8131999SN/A
8141999SN/A
8151999SN/A/// Target fchmod() handler.
8161999SN/Atemplate <class OS>
8171999SN/ASyscallReturn
8183114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8192680Sktlim@umich.edu           ThreadContext *tc)
8201999SN/A{
8216701Sgblack@eecs.umich.edu    int index = 0;
82210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
82310931Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
82410931Sbrandon.potter@amd.com
82510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
82610931Sbrandon.potter@amd.com    if (sim_fd < 0)
8271999SN/A        return -EBADF;
8281999SN/A
8291999SN/A    mode_t hostMode = 0;
8301999SN/A
8311999SN/A    // XXX translate mode flags via OS::someting???
8321999SN/A    hostMode = mode;
8331999SN/A
8341999SN/A    // do the fchmod
83510931Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
8361999SN/A    if (result < 0)
8372218SN/A        return -errno;
8381999SN/A
8391999SN/A    return 0;
8401999SN/A}
8411999SN/A
8425877Shsul@eecs.umich.edu/// Target mremap() handler.
8435877Shsul@eecs.umich.edutemplate <class OS>
8445877Shsul@eecs.umich.eduSyscallReturn
8455877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8465877Shsul@eecs.umich.edu{
8476701Sgblack@eecs.umich.edu    int index = 0;
8486701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8496701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8506701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8516701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
85210027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
85310027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
85410027SChris.Adeniyi-Jones@arm.com
85510027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
85610027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8575877Shsul@eecs.umich.edu
85810318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
85910318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8605877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8615877Shsul@eecs.umich.edu        return -EINVAL;
8625877Shsul@eecs.umich.edu    }
8635877Shsul@eecs.umich.edu
86410486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
86510486Stjablin@gmail.com
8665877Shsul@eecs.umich.edu    if (new_length > old_length) {
86710027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
86810027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8695877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8708601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8715877Shsul@eecs.umich.edu            process->mmap_end += diff;
8725877Shsul@eecs.umich.edu            return start;
8735877Shsul@eecs.umich.edu        } else {
87410027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8755877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8765877Shsul@eecs.umich.edu                return -ENOMEM;
8775877Shsul@eecs.umich.edu            } else {
87810027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
87910027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
88010027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
88110027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
88210027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
88310027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8845877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
88510027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
88610027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
88710027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
88810027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
88910027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
89010027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
89110027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
89210027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
89310027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
89410027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
89510027SChris.Adeniyi-Jones@arm.com                }
89610027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
89710027SChris.Adeniyi-Jones@arm.com                return new_start;
8985877Shsul@eecs.umich.edu            }
8995877Shsul@eecs.umich.edu        }
9005877Shsul@eecs.umich.edu    } else {
90110027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
90210027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
9038601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
90410027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
9055877Shsul@eecs.umich.edu    }
9065877Shsul@eecs.umich.edu}
9071999SN/A
908378SN/A/// Target stat() handler.
909360SN/Atemplate <class OS>
9101450SN/ASyscallReturn
9113114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9122680Sktlim@umich.edu         ThreadContext *tc)
913360SN/A{
914360SN/A    std::string path;
915360SN/A
9166701Sgblack@eecs.umich.edu    int index = 0;
9178852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9186701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9196701Sgblack@eecs.umich.edu        return -EFAULT;
9206701Sgblack@eecs.umich.edu    }
9216701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
922360SN/A
9233669Sbinkertn@umich.edu    // Adjust path for current working directory
9243669Sbinkertn@umich.edu    path = process->fullPath(path);
9253669Sbinkertn@umich.edu
926360SN/A    struct stat hostBuf;
927360SN/A    int result = stat(path.c_str(), &hostBuf);
928360SN/A
929360SN/A    if (result < 0)
9302218SN/A        return -errno;
931360SN/A
9328706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
933360SN/A
9341458SN/A    return 0;
935360SN/A}
936360SN/A
937360SN/A
9385074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9395074Ssaidi@eecs.umich.edutemplate <class OS>
9405074Ssaidi@eecs.umich.eduSyscallReturn
9415074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9425074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9435074Ssaidi@eecs.umich.edu{
9445074Ssaidi@eecs.umich.edu    std::string path;
9455074Ssaidi@eecs.umich.edu
9466701Sgblack@eecs.umich.edu    int index = 0;
9478852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9486701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9495074Ssaidi@eecs.umich.edu        return -EFAULT;
9506701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9515074Ssaidi@eecs.umich.edu
9525074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9535074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9545074Ssaidi@eecs.umich.edu
9555208Ssaidi@eecs.umich.edu#if NO_STAT64
9565208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9575208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9585208Ssaidi@eecs.umich.edu#else
9595074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9605074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9615208Ssaidi@eecs.umich.edu#endif
9625074Ssaidi@eecs.umich.edu
9635074Ssaidi@eecs.umich.edu    if (result < 0)
9645074Ssaidi@eecs.umich.edu        return -errno;
9655074Ssaidi@eecs.umich.edu
9668706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9675074Ssaidi@eecs.umich.edu
9685074Ssaidi@eecs.umich.edu    return 0;
9695074Ssaidi@eecs.umich.edu}
9705074Ssaidi@eecs.umich.edu
9715074Ssaidi@eecs.umich.edu
97210027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
97310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
97410027SChris.Adeniyi-Jones@arm.comSyscallReturn
97510027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
97610027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
97710027SChris.Adeniyi-Jones@arm.com{
97810027SChris.Adeniyi-Jones@arm.com    int index = 0;
97910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
98010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
98110793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
98210027SChris.Adeniyi-Jones@arm.com
98310027SChris.Adeniyi-Jones@arm.com    std::string path;
98410027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
98510027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
98610027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
98710027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
98810027SChris.Adeniyi-Jones@arm.com
98910027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
99010027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
99110027SChris.Adeniyi-Jones@arm.com
99210027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
99310027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
99410027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
99510027SChris.Adeniyi-Jones@arm.com#else
99610027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
99710027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
99810027SChris.Adeniyi-Jones@arm.com#endif
99910027SChris.Adeniyi-Jones@arm.com
100010027SChris.Adeniyi-Jones@arm.com    if (result < 0)
100110027SChris.Adeniyi-Jones@arm.com        return -errno;
100210027SChris.Adeniyi-Jones@arm.com
100310027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
100410027SChris.Adeniyi-Jones@arm.com
100510027SChris.Adeniyi-Jones@arm.com    return 0;
100610027SChris.Adeniyi-Jones@arm.com}
100710027SChris.Adeniyi-Jones@arm.com
100810027SChris.Adeniyi-Jones@arm.com
10091999SN/A/// Target fstat64() handler.
10101999SN/Atemplate <class OS>
10111999SN/ASyscallReturn
10123114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10132680Sktlim@umich.edu            ThreadContext *tc)
10141999SN/A{
10156701Sgblack@eecs.umich.edu    int index = 0;
101610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
10176701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
101810931Sbrandon.potter@amd.com
101910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
102010931Sbrandon.potter@amd.com    if (sim_fd < 0)
10211999SN/A        return -EBADF;
10221999SN/A
10232764Sstever@eecs.umich.edu#if NO_STAT64
10242064SN/A    struct stat  hostBuf;
102510931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10262064SN/A#else
10272064SN/A    struct stat64  hostBuf;
102810931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10292064SN/A#endif
10301999SN/A
10311999SN/A    if (result < 0)
10322218SN/A        return -errno;
10331999SN/A
103410931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10351999SN/A
10361999SN/A    return 0;
10371999SN/A}
10381999SN/A
10391999SN/A
1040378SN/A/// Target lstat() handler.
1041360SN/Atemplate <class OS>
10421450SN/ASyscallReturn
10433114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10442680Sktlim@umich.edu          ThreadContext *tc)
1045360SN/A{
1046360SN/A    std::string path;
1047360SN/A
10486701Sgblack@eecs.umich.edu    int index = 0;
10498852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10506701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10516701Sgblack@eecs.umich.edu        return -EFAULT;
10526701Sgblack@eecs.umich.edu    }
10536701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1054360SN/A
10553669Sbinkertn@umich.edu    // Adjust path for current working directory
10563669Sbinkertn@umich.edu    path = process->fullPath(path);
10573669Sbinkertn@umich.edu
1058360SN/A    struct stat hostBuf;
1059360SN/A    int result = lstat(path.c_str(), &hostBuf);
1060360SN/A
1061360SN/A    if (result < 0)
10621458SN/A        return -errno;
1063360SN/A
10648706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1065360SN/A
10661458SN/A    return 0;
1067360SN/A}
1068360SN/A
10691999SN/A/// Target lstat64() handler.
10701999SN/Atemplate <class OS>
10711999SN/ASyscallReturn
10723114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10732680Sktlim@umich.edu            ThreadContext *tc)
10741999SN/A{
10751999SN/A    std::string path;
10761999SN/A
10776701Sgblack@eecs.umich.edu    int index = 0;
10788852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10796701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10806701Sgblack@eecs.umich.edu        return -EFAULT;
10816701Sgblack@eecs.umich.edu    }
10826701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10831999SN/A
10843669Sbinkertn@umich.edu    // Adjust path for current working directory
10853669Sbinkertn@umich.edu    path = process->fullPath(path);
10863669Sbinkertn@umich.edu
10872764Sstever@eecs.umich.edu#if NO_STAT64
10882064SN/A    struct stat hostBuf;
10892064SN/A    int result = lstat(path.c_str(), &hostBuf);
10902064SN/A#else
10911999SN/A    struct stat64 hostBuf;
10921999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10932064SN/A#endif
10941999SN/A
10951999SN/A    if (result < 0)
10961999SN/A        return -errno;
10971999SN/A
10988706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10991999SN/A
11001999SN/A    return 0;
11011999SN/A}
11021999SN/A
1103378SN/A/// Target fstat() handler.
1104360SN/Atemplate <class OS>
11051450SN/ASyscallReturn
11063114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11072680Sktlim@umich.edu          ThreadContext *tc)
1108360SN/A{
11096701Sgblack@eecs.umich.edu    int index = 0;
111010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11116701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1112360SN/A
111311380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1114360SN/A
111510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
111610931Sbrandon.potter@amd.com    if (sim_fd < 0)
11171458SN/A        return -EBADF;
1118360SN/A
1119360SN/A    struct stat hostBuf;
112010931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1121360SN/A
1122360SN/A    if (result < 0)
11231458SN/A        return -errno;
1124360SN/A
112510931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11262021SN/A
11271458SN/A    return 0;
1128360SN/A}
1129360SN/A
1130360SN/A
11311706SN/A/// Target statfs() handler.
11321706SN/Atemplate <class OS>
11331706SN/ASyscallReturn
11343114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11352680Sktlim@umich.edu           ThreadContext *tc)
11361706SN/A{
11371706SN/A    std::string path;
11381706SN/A
11396701Sgblack@eecs.umich.edu    int index = 0;
11408852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11416701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11426701Sgblack@eecs.umich.edu        return -EFAULT;
11436701Sgblack@eecs.umich.edu    }
11446701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11451706SN/A
11463669Sbinkertn@umich.edu    // Adjust path for current working directory
11473669Sbinkertn@umich.edu    path = process->fullPath(path);
11483669Sbinkertn@umich.edu
11491706SN/A    struct statfs hostBuf;
11501706SN/A    int result = statfs(path.c_str(), &hostBuf);
11511706SN/A
11521706SN/A    if (result < 0)
11532218SN/A        return -errno;
11541706SN/A
11558706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11561706SN/A
11571706SN/A    return 0;
11581706SN/A}
11591706SN/A
11601706SN/A
11611706SN/A/// Target fstatfs() handler.
11621706SN/Atemplate <class OS>
11631706SN/ASyscallReturn
11643114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11652680Sktlim@umich.edu            ThreadContext *tc)
11661706SN/A{
11676701Sgblack@eecs.umich.edu    int index = 0;
116810931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11696701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11701706SN/A
117110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
117210931Sbrandon.potter@amd.com    if (sim_fd < 0)
11731706SN/A        return -EBADF;
11741706SN/A
11751706SN/A    struct statfs hostBuf;
117610931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11771706SN/A
11781706SN/A    if (result < 0)
11792218SN/A        return -errno;
11801706SN/A
11818706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11821706SN/A
11831706SN/A    return 0;
11841706SN/A}
11851706SN/A
11861706SN/A
11871999SN/A/// Target writev() handler.
11881999SN/Atemplate <class OS>
11891999SN/ASyscallReturn
11903114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11912680Sktlim@umich.edu           ThreadContext *tc)
11921999SN/A{
11936701Sgblack@eecs.umich.edu    int index = 0;
119410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
119510931Sbrandon.potter@amd.com
119610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
119710931Sbrandon.potter@amd.com    if (sim_fd < 0)
11981999SN/A        return -EBADF;
11991999SN/A
12008852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
12016701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
12026701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
12031999SN/A    struct iovec hiov[count];
12046227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
12051999SN/A        typename OS::tgt_iovec tiov;
12062461SN/A
12078852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12088852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12098737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
12101999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
12118852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12128852Sandreas.hansson@arm.com                   hiov[i].iov_len);
12131999SN/A    }
12141999SN/A
121510931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
12161999SN/A
12176227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
12181999SN/A        delete [] (char *)hiov[i].iov_base;
12191999SN/A
12201999SN/A    if (result < 0)
12212218SN/A        return -errno;
12221999SN/A
122310629Sjthestness@gmail.com    return result;
12241999SN/A}
12251999SN/A
122611385Sbrandon.potter@amd.com/// Real mmap handler.
1227360SN/Atemplate <class OS>
12281450SN/ASyscallReturn
122911385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
123011385Sbrandon.potter@amd.com         bool is_mmap2)
1231360SN/A{
12326701Sgblack@eecs.umich.edu    int index = 0;
12336701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12346701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
123511383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
123611383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
12378324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
123810486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1239360SN/A
124011385Sbrandon.potter@amd.com    if (is_mmap2)
124111385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
12429008Sgblack@eecs.umich.edu
124311383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
124411383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
124511383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
124611383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
124711383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
124811383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
124911383Sbrandon.potter@amd.com        !length) {
125011383Sbrandon.potter@amd.com        return -EINVAL;
125111383Sbrandon.potter@amd.com    }
12528324Ssteve.reinhardt@amd.com
125311383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
125411383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
125511383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
125611383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
125711383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
125811383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
125911383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
126011383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
126111383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
126211383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
126311383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
126411383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
126511383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
126611383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
126711383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
126811383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
126911383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
127011383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
127111383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
127211383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
127311383Sbrandon.potter@amd.com        // than we currently maintain.
127411383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
127511383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
127611383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
12778324Ssteve.reinhardt@amd.com    }
12785877Shsul@eecs.umich.edu
127910486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
128010486Stjablin@gmail.com
128111383Sbrandon.potter@amd.com    int sim_fd = -1;
128211383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
128311383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
128411383Sbrandon.potter@amd.com        sim_fd = p->getSimFD(tgt_fd);
128511383Sbrandon.potter@amd.com        if (sim_fd < 0)
128611383Sbrandon.potter@amd.com            return -EBADF;
1287360SN/A
128811383Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
128911383Sbrandon.potter@amd.com                                    sim_fd, offset);
12908600Ssteve.reinhardt@amd.com
129111383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
129211383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
129311383Sbrandon.potter@amd.com            return -errno;
12948600Ssteve.reinhardt@amd.com        }
12952544SN/A    }
12962544SN/A
129711383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
129811383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
129911383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
130011383Sbrandon.potter@amd.com        start = (OS::mmapGrowsDown()) ? p->mmap_end - length : p->mmap_end;
130111383Sbrandon.potter@amd.com        p->mmap_end = (OS::mmapGrowsDown()) ? start : p->mmap_end + length;
130211383Sbrandon.potter@amd.com    }
130311383Sbrandon.potter@amd.com
130411383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
130511383Sbrandon.potter@amd.com                    start, start + length - 1);
130611383Sbrandon.potter@amd.com
130711383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
130811383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
130911383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
131011383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
131111383Sbrandon.potter@amd.com    if (clobber) {
131211383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
131311383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
131411383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
131511383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
131611383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
13178600Ssteve.reinhardt@amd.com        }
13186672Sgblack@eecs.umich.edu    }
13198600Ssteve.reinhardt@amd.com
132011383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
132111383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
132211383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
13238601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
13242544SN/A
132511383Sbrandon.potter@amd.com    // Transfer content into target address space.
132611383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
132711383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
132811383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
132911383Sbrandon.potter@amd.com        // with something like:
133011383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
133111383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
133211383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
133311383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
133411383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
133511383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
133611383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
133711383Sbrandon.potter@amd.com    } else {
133811383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
133911383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
134011383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
134111383Sbrandon.potter@amd.com        // the host into the target address space.
134211383Sbrandon.potter@amd.com        struct stat file_stat;
134311383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
134411383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
134511383Sbrandon.potter@amd.com
134611383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
134711383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
134811383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
134911383Sbrandon.potter@amd.com        // on the specified offset into the file.
135011383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
135111383Sbrandon.potter@amd.com                                 length);
135211383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
135311383Sbrandon.potter@amd.com
135411383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
135511383Sbrandon.potter@amd.com        munmap(pmap, length);
135611383Sbrandon.potter@amd.com
135711383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
135811383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
135911383Sbrandon.potter@amd.com        // execution (hopefully).
136011383Sbrandon.potter@amd.com    }
136111383Sbrandon.potter@amd.com
13621458SN/A    return start;
1363360SN/A}
1364360SN/A
136511385Sbrandon.potter@amd.com/// Target mmap() handler.
136611385Sbrandon.potter@amd.comtemplate <class OS>
136711385Sbrandon.potter@amd.comSyscallReturn
136811385Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
136911385Sbrandon.potter@amd.com{
137011385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
137111385Sbrandon.potter@amd.com}
137211385Sbrandon.potter@amd.com
137311385Sbrandon.potter@amd.com/// Target mmap2() handler.
137411385Sbrandon.potter@amd.comtemplate <class OS>
137511385Sbrandon.potter@amd.comSyscallReturn
137611385Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
137711385Sbrandon.potter@amd.com{
137811385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
137911385Sbrandon.potter@amd.com}
138011385Sbrandon.potter@amd.com
1381378SN/A/// Target getrlimit() handler.
1382360SN/Atemplate <class OS>
13831450SN/ASyscallReturn
13843114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13852680Sktlim@umich.edu        ThreadContext *tc)
1386360SN/A{
13876701Sgblack@eecs.umich.edu    int index = 0;
13886701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
13896701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1390360SN/A
1391360SN/A    switch (resource) {
13922064SN/A        case OS::TGT_RLIMIT_STACK:
13935877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
13942064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
13958737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13968737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13972064SN/A            break;
1398360SN/A
13995877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
14005877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
14015877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
14028737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14038737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14045877Shsul@eecs.umich.edu            break;
14055877Shsul@eecs.umich.edu
14062064SN/A        default:
140710794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
140810794Sbrandon.potter@amd.com            return -EINVAL;
14092064SN/A            break;
1410360SN/A    }
1411360SN/A
14128706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
14131458SN/A    return 0;
1414360SN/A}
1415360SN/A
141610796Sbrandon.potter@amd.com/// Target clock_gettime() function.
141710796Sbrandon.potter@amd.comtemplate <class OS>
141810796Sbrandon.potter@amd.comSyscallReturn
141910796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
142010796Sbrandon.potter@amd.com{
142110796Sbrandon.potter@amd.com    int index = 1;
142210796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
142310796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
142410796Sbrandon.potter@amd.com
142510796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
142610796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
142710796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
142810796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
142910796Sbrandon.potter@amd.com
143010796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
143110796Sbrandon.potter@amd.com
143210796Sbrandon.potter@amd.com    return 0;
143310796Sbrandon.potter@amd.com}
143410796Sbrandon.potter@amd.com
143511337SMichael.Lebeane@amd.com/// Target clock_getres() function.
143611337SMichael.Lebeane@amd.comtemplate <class OS>
143711337SMichael.Lebeane@amd.comSyscallReturn
143811337SMichael.Lebeane@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
143911337SMichael.Lebeane@amd.com{
144011337SMichael.Lebeane@amd.com    int index = 1;
144111337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
144211337SMichael.Lebeane@amd.com
144311337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
144411337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
144511337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
144611337SMichael.Lebeane@amd.com
144711337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
144811337SMichael.Lebeane@amd.com
144911337SMichael.Lebeane@amd.com    return 0;
145011337SMichael.Lebeane@amd.com}
145111337SMichael.Lebeane@amd.com
1452378SN/A/// Target gettimeofday() handler.
1453360SN/Atemplate <class OS>
14541450SN/ASyscallReturn
14553114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14562680Sktlim@umich.edu        ThreadContext *tc)
1457360SN/A{
14586701Sgblack@eecs.umich.edu    int index = 0;
14596701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1460360SN/A
146110796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1462360SN/A    tp->tv_sec += seconds_since_epoch;
14636109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
14646109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1465360SN/A
14668706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1467360SN/A
14681458SN/A    return 0;
1469360SN/A}
1470360SN/A
1471360SN/A
14721999SN/A/// Target utimes() handler.
14731999SN/Atemplate <class OS>
14741999SN/ASyscallReturn
14753114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14762680Sktlim@umich.edu           ThreadContext *tc)
14771999SN/A{
14781999SN/A    std::string path;
14791999SN/A
14806701Sgblack@eecs.umich.edu    int index = 0;
14818852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14826701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14836701Sgblack@eecs.umich.edu        return -EFAULT;
14846701Sgblack@eecs.umich.edu    }
14851999SN/A
14866701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
14876701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
14888706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
14891999SN/A
14901999SN/A    struct timeval hostTimeval[2];
14911999SN/A    for (int i = 0; i < 2; ++i)
14921999SN/A    {
14938737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
14948737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
14951999SN/A    }
14963669Sbinkertn@umich.edu
14973669Sbinkertn@umich.edu    // Adjust path for current working directory
14983669Sbinkertn@umich.edu    path = process->fullPath(path);
14993669Sbinkertn@umich.edu
15001999SN/A    int result = utimes(path.c_str(), hostTimeval);
15011999SN/A
15021999SN/A    if (result < 0)
15031999SN/A        return -errno;
15041999SN/A
15051999SN/A    return 0;
15061999SN/A}
1507378SN/A/// Target getrusage() function.
1508360SN/Atemplate <class OS>
15091450SN/ASyscallReturn
15103114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15112680Sktlim@umich.edu              ThreadContext *tc)
1512360SN/A{
15136701Sgblack@eecs.umich.edu    int index = 0;
15146701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
15156701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1516360SN/A
15173670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
15183670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1519360SN/A    rup->ru_stime.tv_sec = 0;
1520360SN/A    rup->ru_stime.tv_usec = 0;
1521360SN/A    rup->ru_maxrss = 0;
1522360SN/A    rup->ru_ixrss = 0;
1523360SN/A    rup->ru_idrss = 0;
1524360SN/A    rup->ru_isrss = 0;
1525360SN/A    rup->ru_minflt = 0;
1526360SN/A    rup->ru_majflt = 0;
1527360SN/A    rup->ru_nswap = 0;
1528360SN/A    rup->ru_inblock = 0;
1529360SN/A    rup->ru_oublock = 0;
1530360SN/A    rup->ru_msgsnd = 0;
1531360SN/A    rup->ru_msgrcv = 0;
1532360SN/A    rup->ru_nsignals = 0;
1533360SN/A    rup->ru_nvcsw = 0;
1534360SN/A    rup->ru_nivcsw = 0;
1535360SN/A
15363670Sbinkertn@umich.edu    switch (who) {
15373670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
153810796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
15398737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
15408737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
15413670Sbinkertn@umich.edu        break;
15423670Sbinkertn@umich.edu
15433670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
15443670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
15453670Sbinkertn@umich.edu        break;
15463670Sbinkertn@umich.edu
15473670Sbinkertn@umich.edu      default:
15483670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
15493670Sbinkertn@umich.edu        // plow ahead
15503670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
15513670Sbinkertn@umich.edu             who);
15523670Sbinkertn@umich.edu    }
15533670Sbinkertn@umich.edu
15548706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1555360SN/A
15561458SN/A    return 0;
1557360SN/A}
1558360SN/A
15596683Stjones1@inf.ed.ac.uk/// Target times() function.
15606683Stjones1@inf.ed.ac.uktemplate <class OS>
15616683Stjones1@inf.ed.ac.ukSyscallReturn
15626683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15636683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
15646683Stjones1@inf.ed.ac.uk{
15656701Sgblack@eecs.umich.edu    int index = 0;
15666701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
15676683Stjones1@inf.ed.ac.uk
15686683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
15697823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
15706683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
15716683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
15726683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
15736683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
15746683Stjones1@inf.ed.ac.uk
15756683Stjones1@inf.ed.ac.uk    // Convert to host endianness
15768737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
15776683Stjones1@inf.ed.ac.uk
15786683Stjones1@inf.ed.ac.uk    // Write back
15798706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
15806683Stjones1@inf.ed.ac.uk
15816683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
15826683Stjones1@inf.ed.ac.uk    return clocks;
15836683Stjones1@inf.ed.ac.uk}
15842553SN/A
15856684Stjones1@inf.ed.ac.uk/// Target time() function.
15866684Stjones1@inf.ed.ac.uktemplate <class OS>
15876684Stjones1@inf.ed.ac.ukSyscallReturn
15886684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15896684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
15906684Stjones1@inf.ed.ac.uk{
15916684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
159210796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
15936684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
15946684Stjones1@inf.ed.ac.uk
15956701Sgblack@eecs.umich.edu    int index = 0;
15966701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
159711321Ssteve.reinhardt@amd.com    if (taddr != 0) {
15986684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
15998737Skoansin.tan@gmail.com        t = TheISA::htog(t);
16008852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
16018852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
16026684Stjones1@inf.ed.ac.uk    }
16036684Stjones1@inf.ed.ac.uk    return sec;
16046684Stjones1@inf.ed.ac.uk}
16052553SN/A
16062553SN/A
16071354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1608