syscall_emul.hh revision 11321
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
613113Sgblack@eecs.umich.edu#include <sys/stat.h>
628229Snate@binkert.org#include <sys/time.h>
638229Snate@binkert.org#include <sys/uio.h>
643113Sgblack@eecs.umich.edu#include <fcntl.h>
657075Snate@binkert.org
668229Snate@binkert.org#include <cerrno>
677075Snate@binkert.org#include <string>
68360SN/A
692474SN/A#include "base/chunk_generator.hh"
705543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
712462SN/A#include "base/misc.hh"
721354SN/A#include "base/trace.hh"
736216Snate@binkert.org#include "base/types.hh"
746658Snate@binkert.org#include "config/the_isa.hh"
752474SN/A#include "cpu/base.hh"
762680Sktlim@umich.edu#include "cpu/thread_context.hh"
778232Snate@binkert.org#include "debug/SyscallVerbose.hh"
788229Snate@binkert.org#include "mem/page_table.hh"
797678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
8010496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
818229Snate@binkert.org#include "sim/process.hh"
8210497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
838766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
846640Svince@csl.cornell.edu#include "sim/system.hh"
85360SN/A
86360SN/A///
87360SN/A/// System call descriptor.
88360SN/A///
89360SN/Aclass SyscallDesc {
90360SN/A
91360SN/A  public:
92360SN/A
93378SN/A    /// Typedef for target syscall handler functions.
941450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
953114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
96360SN/A
975543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
985543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
995543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
10010831Ssteve.reinhardt@amd.com    bool warned;        //!< Have we warned about unimplemented syscall?
101360SN/A
102360SN/A    /// Flag values for controlling syscall behavior.
103360SN/A    enum Flags {
104360SN/A        /// Don't set return regs according to funcPtr return value.
105360SN/A        /// Used for syscalls with non-standard return conventions
1062680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
107360SN/A        /// sigreturn).
10810831Ssteve.reinhardt@amd.com        SuppressReturnValue = 1,
10910831Ssteve.reinhardt@amd.com        WarnOnce = 2
110360SN/A    };
111360SN/A
112360SN/A    /// Constructor.
113360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
11410831Ssteve.reinhardt@amd.com        : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
115360SN/A    {
116360SN/A    }
117360SN/A
118360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1193114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
12010831Ssteve.reinhardt@amd.com
12110831Ssteve.reinhardt@amd.com    /// Is the WarnOnce flag set?
12210831Ssteve.reinhardt@amd.com    bool warnOnce() const {  return (flags & WarnOnce); }
123360SN/A};
124360SN/A
125360SN/A
126360SN/A//////////////////////////////////////////////////////////////////////
127360SN/A//
128360SN/A// The following emulation functions are generic enough that they
129360SN/A// don't need to be recompiled for different emulated OS's.  They are
130360SN/A// defined in sim/syscall_emul.cc.
131360SN/A//
132360SN/A//////////////////////////////////////////////////////////////////////
133360SN/A
134360SN/A
135378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1361706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1373114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
138378SN/A
139378SN/A/// Handler for unimplemented syscalls that we never intend to
140378SN/A/// implement (signal handling, etc.) and should not affect the correct
141378SN/A/// behavior of the program.  Print a warning only if the appropriate
142378SN/A/// trace flag is enabled.  Return success to the target program.
1431706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1443114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
145360SN/A
1466109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1471706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1483114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
149378SN/A
1506109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1516109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1526109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1536109Ssanchezd@stanford.edu
154378SN/A/// Target getpagesize() handler.
1551706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1563114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
157378SN/A
1585748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1595748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1605748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
161378SN/A
162378SN/A/// Target close() handler.
1631706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1643114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
165378SN/A
166378SN/A/// Target read() handler.
1671706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1683114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
169378SN/A
170378SN/A/// Target write() handler.
1711706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1723114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
173378SN/A
174378SN/A/// Target lseek() handler.
1751706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1763114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
177378SN/A
1784118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1794118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1804118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1814118Sgblack@eecs.umich.edu
182378SN/A/// Target munmap() handler.
1831706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
185378SN/A
186378SN/A/// Target gethostname() handler.
1871706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1883114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
189360SN/A
1905513SMichael.Adler@intel.com/// Target getcwd() handler.
1915513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
1925513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
1935513SMichael.Adler@intel.com
19410203SAli.Saidi@ARM.com/// Target readlink() handler.
19510203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19610203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
19710203SAli.Saidi@ARM.com                           int index = 0);
1985513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
1995513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2005513SMichael.Adler@intel.com
201511SN/A/// Target unlink() handler.
20210633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20310633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
20410633Smichaelupton@gmail.com                           int index);
2051706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2063114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
207511SN/A
2085513SMichael.Adler@intel.com/// Target mkdir() handler.
2095513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2105513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2115513SMichael.Adler@intel.com
212511SN/A/// Target rename() handler.
2131706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2143114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2151706SN/A
2161706SN/A
2171706SN/A/// Target truncate() handler.
2181706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2193114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2201706SN/A
2211706SN/A
2221706SN/A/// Target ftruncate() handler.
2231706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2243114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2251706SN/A
226511SN/A
2276703Svince@csl.cornell.edu/// Target truncate64() handler.
2286703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2296703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2306703Svince@csl.cornell.edu
2316685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2326685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2336685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2346685Stjones1@inf.ed.ac.uk
2356685Stjones1@inf.ed.ac.uk
2365513SMichael.Adler@intel.com/// Target umask() handler.
2375513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2385513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2395513SMichael.Adler@intel.com
2405513SMichael.Adler@intel.com
2411999SN/A/// Target chown() handler.
2421999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2433114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2441999SN/A
2451999SN/A
2461999SN/A/// Target fchown() handler.
2471999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2483114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2491999SN/A
2503079Sstever@eecs.umich.edu/// Target dup() handler.
2513079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2523114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2533079Sstever@eecs.umich.edu
2542093SN/A/// Target fnctl() handler.
2552093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2563114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2572093SN/A
2582687Sksewell@umich.edu/// Target fcntl64() handler.
2592687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2603114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2612687Sksewell@umich.edu
2622238SN/A/// Target setuid() handler.
2632238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2643114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2652238SN/A
2662238SN/A/// Target getpid() handler.
2672238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2683114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2692238SN/A
2702238SN/A/// Target getuid() handler.
2712238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2723114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2732238SN/A
2742238SN/A/// Target getgid() handler.
2752238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2763114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2772238SN/A
2782238SN/A/// Target getppid() handler.
2792238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2803114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2812238SN/A
2822238SN/A/// Target geteuid() handler.
2832238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2843114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2852238SN/A
2862238SN/A/// Target getegid() handler.
2872238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2883114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2892238SN/A
2906109Ssanchezd@stanford.edu/// Target clone() handler.
2916109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
2926109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
2932238SN/A
2949455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2959455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2969455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
29710203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
29810203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
29910203SAli.Saidi@ARM.com                               int index);
3009455Smitch.hayenga+gem5@gmail.com
3019112Smarc.orr@gmail.com/// Futex system call
3029112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3039112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3049112Smarc.orr@gmail.comtemplate <class OS>
3059112Smarc.orr@gmail.comSyscallReturn
3069112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3079112Smarc.orr@gmail.com          ThreadContext *tc)
3089112Smarc.orr@gmail.com{
3099112Smarc.orr@gmail.com    int index_uaddr = 0;
3109112Smarc.orr@gmail.com    int index_op = 1;
3119112Smarc.orr@gmail.com    int index_val = 2;
3129112Smarc.orr@gmail.com    int index_timeout = 3;
3139112Smarc.orr@gmail.com
3149112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3159112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3169112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3179112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3189112Smarc.orr@gmail.com
3199112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3209112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3219112Smarc.orr@gmail.com
3229112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3239112Smarc.orr@gmail.com            uaddr, op, val);
3249112Smarc.orr@gmail.com
3259238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3269112Smarc.orr@gmail.com
3279112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3289112Smarc.orr@gmail.com        if (timeout != 0) {
3299112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3309112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3319112Smarc.orr@gmail.com        }
3329112Smarc.orr@gmail.com
3339112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3349112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3359112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
3369112Smarc.orr@gmail.com        delete buf;
3379112Smarc.orr@gmail.com
33811321Ssteve.reinhardt@amd.com        if (val != mem_val) {
3399112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3409112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3419112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3429112Smarc.orr@gmail.com        }
3439112Smarc.orr@gmail.com
3449112Smarc.orr@gmail.com        // Queue the thread context
3459112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3469112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3479112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3489112Smarc.orr@gmail.com        } else {
3499112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3509112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3519112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3529112Smarc.orr@gmail.com        }
3539112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3549112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3559112Smarc.orr@gmail.com                                "thread context\n");
3569112Smarc.orr@gmail.com        tc->suspend();
3579112Smarc.orr@gmail.com        return 0;
3589112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3599112Smarc.orr@gmail.com        int wokenUp = 0;
3609112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3619112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3629112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3639112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3649112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3659112Smarc.orr@gmail.com                tcWaitList->pop_front();
3669112Smarc.orr@gmail.com                wokenUp++;
3679112Smarc.orr@gmail.com            }
36811321Ssteve.reinhardt@amd.com            if (tcWaitList->empty()) {
3699112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3709112Smarc.orr@gmail.com                delete tcWaitList;
3719112Smarc.orr@gmail.com            }
3729112Smarc.orr@gmail.com        }
3739112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3749112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3759112Smarc.orr@gmail.com        return wokenUp;
3769112Smarc.orr@gmail.com    } else {
3779238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3789112Smarc.orr@gmail.com        return 0;
3799112Smarc.orr@gmail.com    }
3809112Smarc.orr@gmail.com
3819112Smarc.orr@gmail.com}
3829112Smarc.orr@gmail.com
3832238SN/A
3842238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3852238SN/A/// returning a second value in a register other than the normal return register
3862238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3873114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3882238SN/A
3892238SN/A/// Target getpidPseudo() handler.
3902238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3913114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3922238SN/A
3932238SN/A/// Target getuidPseudo() handler.
3942238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3953114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3962238SN/A
3972238SN/A/// Target getgidPseudo() handler.
3982238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3993114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4002238SN/A
4012238SN/A
4021354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4031354SN/Aconst int one_million = 1000000;
40410796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
40510796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4061354SN/A
4071354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4081354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4091354SN/A/// real-world time) to keep simulations repeatable.
4101354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4111354SN/A
4121354SN/A/// Helper function to convert current elapsed time to seconds and
4131354SN/A/// microseconds.
4141354SN/Atemplate <class T1, class T2>
4151354SN/Avoid
41610796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4171354SN/A{
41810796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4191354SN/A    sec = elapsed_usecs / one_million;
4201354SN/A    usec = elapsed_usecs % one_million;
4211354SN/A}
4221354SN/A
42310796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
42410796Sbrandon.potter@amd.com/// nanoseconds.
42510796Sbrandon.potter@amd.comtemplate <class T1, class T2>
42610796Sbrandon.potter@amd.comvoid
42710796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
42810796Sbrandon.potter@amd.com{
42910796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
43010796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
43110796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
43210796Sbrandon.potter@amd.com}
43310796Sbrandon.potter@amd.com
434360SN/A//////////////////////////////////////////////////////////////////////
435360SN/A//
436360SN/A// The following emulation functions are generic, but need to be
437360SN/A// templated to account for differences in types, constants, etc.
438360SN/A//
439360SN/A//////////////////////////////////////////////////////////////////////
440360SN/A
4413113Sgblack@eecs.umich.edu#if NO_STAT64
4423113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4433113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4443113Sgblack@eecs.umich.edu#else
4453113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4463113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4473113Sgblack@eecs.umich.edu#endif
4483113Sgblack@eecs.umich.edu
4493113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4503113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4513113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4523113Sgblack@eecs.umich.edu
4533113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4543113Sgblack@eecs.umich.edustatic void
4553113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4563113Sgblack@eecs.umich.edu{
4574189Sgblack@eecs.umich.edu    using namespace TheISA;
4584189Sgblack@eecs.umich.edu
4593113Sgblack@eecs.umich.edu    if (fakeTTY)
4603113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4613113Sgblack@eecs.umich.edu    else
4623113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4638737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4643113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4658737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4663277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4675515SMichael.Adler@intel.com    if (fakeTTY) {
4685515SMichael.Adler@intel.com        // Claim to be a character device
4695515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4705515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4715515SMichael.Adler@intel.com    }
4728737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4733277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4748737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4753277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4768737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4773277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4788737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4793113Sgblack@eecs.umich.edu    if (fakeTTY)
4803113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4813113Sgblack@eecs.umich.edu    else
4823113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4838737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4843113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4858737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4863114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4878737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4883114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4898737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4903114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4918737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4924061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4934061Sgblack@eecs.umich.edu    // consistently across different hosts.
4944061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4958737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4963113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4978737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4983113Sgblack@eecs.umich.edu}
4993113Sgblack@eecs.umich.edu
5003113Sgblack@eecs.umich.edu// Same for stat64
5013113Sgblack@eecs.umich.edu
5023113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5033113Sgblack@eecs.umich.edustatic void
5043113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5053113Sgblack@eecs.umich.edu{
5064189Sgblack@eecs.umich.edu    using namespace TheISA;
5074189Sgblack@eecs.umich.edu
5083113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5093113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5103113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5118737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5123113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5138737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5143113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5158737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5163113Sgblack@eecs.umich.edu#else
5173113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5183113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5193113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5203113Sgblack@eecs.umich.edu#endif
5213113Sgblack@eecs.umich.edu}
5223113Sgblack@eecs.umich.edu
5233113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5243113Sgblack@eecs.umich.edutemplate<class OS>
5253113Sgblack@eecs.umich.edustatic void
5268852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5273113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5283113Sgblack@eecs.umich.edu{
5293113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5303113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5313113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5323113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5333113Sgblack@eecs.umich.edu}
5343113Sgblack@eecs.umich.edu
5353113Sgblack@eecs.umich.edutemplate<class OS>
5363113Sgblack@eecs.umich.edustatic void
5378852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5383113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5393113Sgblack@eecs.umich.edu{
5403113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5413113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5426686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5433113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5443113Sgblack@eecs.umich.edu}
5453113Sgblack@eecs.umich.edu
546378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
547378SN/A/// only to find out if their stdout is a tty, to determine whether to
5489141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5499141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
550360SN/Atemplate <class OS>
5511450SN/ASyscallReturn
5523114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5532680Sktlim@umich.edu          ThreadContext *tc)
554360SN/A{
5556701Sgblack@eecs.umich.edu    int index = 0;
55610930Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5576701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
558360SN/A
55910930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
560360SN/A
56110932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
56210496Ssteve.reinhardt@amd.com
56310930Sbrandon.potter@amd.com    if (fde == NULL) {
564360SN/A        // doesn't map to any simulator fd: not a valid target fd
5651458SN/A        return -EBADF;
566360SN/A    }
567360SN/A
56810930Sbrandon.potter@amd.com    if (fde->driver != NULL) {
56910930Sbrandon.potter@amd.com        return fde->driver->ioctl(process, tc, req);
57010496Ssteve.reinhardt@amd.com    }
57110496Ssteve.reinhardt@amd.com
5729141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5731458SN/A        return -ENOTTY;
5749141Smarc.orr@gmail.com    }
575360SN/A
5769141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
57710930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
5789141Smarc.orr@gmail.com    return -ENOTTY;
579360SN/A}
580360SN/A
581360SN/Atemplate <class OS>
58210027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5833114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
58410027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
585360SN/A{
586360SN/A    std::string path;
587360SN/A
5888852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5896701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5901458SN/A        return -EFAULT;
591360SN/A
5926701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5936701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
594360SN/A    int hostFlags = 0;
595360SN/A
596360SN/A    // translate open flags
597360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
598360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
599360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
600360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
601360SN/A        }
602360SN/A    }
603360SN/A
604360SN/A    // any target flags left?
605360SN/A    if (tgtFlags != 0)
6061706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
607360SN/A
608360SN/A#ifdef __CYGWIN32__
609360SN/A    hostFlags |= O_BINARY;
610360SN/A#endif
611360SN/A
6123669Sbinkertn@umich.edu    // Adjust path for current working directory
6133669Sbinkertn@umich.edu    path = process->fullPath(path);
6143669Sbinkertn@umich.edu
6151706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6161706SN/A
61710496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
61810496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
61910496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
62010496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
62110496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
62210496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
62310496Ssteve.reinhardt@amd.com            return -ENOENT;
62410496Ssteve.reinhardt@amd.com        }
62510496Ssteve.reinhardt@amd.com
62610496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
62710496Ssteve.reinhardt@amd.com        if (drv != NULL) {
62810496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
62910496Ssteve.reinhardt@amd.com            // process if necessary.
63010496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
63110496Ssteve.reinhardt@amd.com        }
63210496Ssteve.reinhardt@amd.com
63310496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
63410496Ssteve.reinhardt@amd.com        // /dev/zero
63510496Ssteve.reinhardt@amd.com    }
63610496Ssteve.reinhardt@amd.com
6375795Ssaidi@eecs.umich.edu    int fd;
6389143Ssteve.reinhardt@amd.com    int local_errno;
6399142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6409142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6419143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6425795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6439143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6445795Ssaidi@eecs.umich.edu     } else {
6455795Ssaidi@eecs.umich.edu        // open the file
6465795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6479143Ssteve.reinhardt@amd.com        local_errno = errno;
6485795Ssaidi@eecs.umich.edu     }
649360SN/A
6509143Ssteve.reinhardt@amd.com    if (fd == -1)
6519143Ssteve.reinhardt@amd.com        return -local_errno;
6529143Ssteve.reinhardt@amd.com
65310932Sbrandon.potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
654360SN/A}
655360SN/A
65610027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
65710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
65810027SChris.Adeniyi-Jones@arm.comSyscallReturn
65910027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
66110027SChris.Adeniyi-Jones@arm.com{
66210027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
66310027SChris.Adeniyi-Jones@arm.com}
66410027SChris.Adeniyi-Jones@arm.com
66510027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
66610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
66710027SChris.Adeniyi-Jones@arm.comSyscallReturn
66810027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66910027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67010027SChris.Adeniyi-Jones@arm.com{
67110027SChris.Adeniyi-Jones@arm.com    int index = 0;
67210027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
67310027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
67410027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
67510027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
67610027SChris.Adeniyi-Jones@arm.com}
67710027SChris.Adeniyi-Jones@arm.com
67810633Smichaelupton@gmail.com/// Target unlinkat() handler.
67910633Smichaelupton@gmail.comtemplate <class OS>
68010633Smichaelupton@gmail.comSyscallReturn
68110633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68210633Smichaelupton@gmail.com             ThreadContext *tc)
68310633Smichaelupton@gmail.com{
68410633Smichaelupton@gmail.com    int index = 0;
68510633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
68610633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
68710633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
68810633Smichaelupton@gmail.com
68910633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
69010633Smichaelupton@gmail.com}
69110633Smichaelupton@gmail.com
69210203SAli.Saidi@ARM.com/// Target facessat() handler
69310203SAli.Saidi@ARM.comtemplate <class OS>
69410203SAli.Saidi@ARM.comSyscallReturn
69510203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69610203SAli.Saidi@ARM.com        ThreadContext *tc)
69710203SAli.Saidi@ARM.com{
69810203SAli.Saidi@ARM.com    int index = 0;
69910203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
70010203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
70110203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
70210203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
70310203SAli.Saidi@ARM.com}
70410203SAli.Saidi@ARM.com
70510203SAli.Saidi@ARM.com/// Target readlinkat() handler
70610203SAli.Saidi@ARM.comtemplate <class OS>
70710203SAli.Saidi@ARM.comSyscallReturn
70810203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70910203SAli.Saidi@ARM.com        ThreadContext *tc)
71010203SAli.Saidi@ARM.com{
71110203SAli.Saidi@ARM.com    int index = 0;
71210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71410203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
71510203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
71610203SAli.Saidi@ARM.com}
71710203SAli.Saidi@ARM.com
71810850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
71910850SGiacomo.Gabrielli@arm.comtemplate <class OS>
72010850SGiacomo.Gabrielli@arm.comSyscallReturn
72110850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
72210850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
72310850SGiacomo.Gabrielli@arm.com{
72410850SGiacomo.Gabrielli@arm.com    int index = 0;
72510850SGiacomo.Gabrielli@arm.com
72610850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
72710850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
72810850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
72910850SGiacomo.Gabrielli@arm.com
73010850SGiacomo.Gabrielli@arm.com    std::string old_name;
73110850SGiacomo.Gabrielli@arm.com
73210850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
73310850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
73410850SGiacomo.Gabrielli@arm.com        return -EFAULT;
73510850SGiacomo.Gabrielli@arm.com
73610850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
73710850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
73810850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
73910850SGiacomo.Gabrielli@arm.com
74010850SGiacomo.Gabrielli@arm.com    std::string new_name;
74110850SGiacomo.Gabrielli@arm.com
74210850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
74310850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
74410850SGiacomo.Gabrielli@arm.com        return -EFAULT;
74510850SGiacomo.Gabrielli@arm.com
74610850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
74710850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
74810850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
74910850SGiacomo.Gabrielli@arm.com
75010850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
75110850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
75210850SGiacomo.Gabrielli@arm.com}
75310850SGiacomo.Gabrielli@arm.com
7546640Svince@csl.cornell.edu/// Target sysinfo() handler.
7556640Svince@csl.cornell.edutemplate <class OS>
7566640Svince@csl.cornell.eduSyscallReturn
7576640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7586640Svince@csl.cornell.edu         ThreadContext *tc)
7596640Svince@csl.cornell.edu{
7606640Svince@csl.cornell.edu
7616701Sgblack@eecs.umich.edu    int index = 0;
7626701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
76310793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7646640Svince@csl.cornell.edu
7656701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7666701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7676640Svince@csl.cornell.edu
7688706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7696640Svince@csl.cornell.edu
7706701Sgblack@eecs.umich.edu    return 0;
7716640Svince@csl.cornell.edu}
772360SN/A
7731999SN/A/// Target chmod() handler.
7741999SN/Atemplate <class OS>
7751999SN/ASyscallReturn
7763114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7772680Sktlim@umich.edu          ThreadContext *tc)
7781999SN/A{
7791999SN/A    std::string path;
7801999SN/A
7816701Sgblack@eecs.umich.edu    int index = 0;
7828852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7836701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7841999SN/A        return -EFAULT;
7856701Sgblack@eecs.umich.edu    }
7861999SN/A
7876701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7881999SN/A    mode_t hostMode = 0;
7891999SN/A
7901999SN/A    // XXX translate mode flags via OS::something???
7911999SN/A    hostMode = mode;
7921999SN/A
7933669Sbinkertn@umich.edu    // Adjust path for current working directory
7943669Sbinkertn@umich.edu    path = process->fullPath(path);
7953669Sbinkertn@umich.edu
7961999SN/A    // do the chmod
7971999SN/A    int result = chmod(path.c_str(), hostMode);
7981999SN/A    if (result < 0)
7992218SN/A        return -errno;
8001999SN/A
8011999SN/A    return 0;
8021999SN/A}
8031999SN/A
8041999SN/A
8051999SN/A/// Target fchmod() handler.
8061999SN/Atemplate <class OS>
8071999SN/ASyscallReturn
8083114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8092680Sktlim@umich.edu           ThreadContext *tc)
8101999SN/A{
8116701Sgblack@eecs.umich.edu    int index = 0;
81210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
81310931Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
81410931Sbrandon.potter@amd.com
81510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
81610931Sbrandon.potter@amd.com    if (sim_fd < 0)
8171999SN/A        return -EBADF;
8181999SN/A
8191999SN/A    mode_t hostMode = 0;
8201999SN/A
8211999SN/A    // XXX translate mode flags via OS::someting???
8221999SN/A    hostMode = mode;
8231999SN/A
8241999SN/A    // do the fchmod
82510931Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
8261999SN/A    if (result < 0)
8272218SN/A        return -errno;
8281999SN/A
8291999SN/A    return 0;
8301999SN/A}
8311999SN/A
8325877Shsul@eecs.umich.edu/// Target mremap() handler.
8335877Shsul@eecs.umich.edutemplate <class OS>
8345877Shsul@eecs.umich.eduSyscallReturn
8355877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8365877Shsul@eecs.umich.edu{
8376701Sgblack@eecs.umich.edu    int index = 0;
8386701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8396701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8406701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8416701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
84210027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
84310027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
84410027SChris.Adeniyi-Jones@arm.com
84510027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
84610027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8475877Shsul@eecs.umich.edu
84810318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
84910318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8505877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8515877Shsul@eecs.umich.edu        return -EINVAL;
8525877Shsul@eecs.umich.edu    }
8535877Shsul@eecs.umich.edu
85410486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
85510486Stjablin@gmail.com
8565877Shsul@eecs.umich.edu    if (new_length > old_length) {
85710027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
85810027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8595877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8608601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8615877Shsul@eecs.umich.edu            process->mmap_end += diff;
8625877Shsul@eecs.umich.edu            return start;
8635877Shsul@eecs.umich.edu        } else {
86410027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8655877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8665877Shsul@eecs.umich.edu                return -ENOMEM;
8675877Shsul@eecs.umich.edu            } else {
86810027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
86910027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
87010027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
87110027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
87210027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
87310027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8745877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
87510027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
87610027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
87710027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
87810027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
87910027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
88010027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
88110027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
88210027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
88310027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
88410027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
88510027SChris.Adeniyi-Jones@arm.com                }
88610027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
88710027SChris.Adeniyi-Jones@arm.com                return new_start;
8885877Shsul@eecs.umich.edu            }
8895877Shsul@eecs.umich.edu        }
8905877Shsul@eecs.umich.edu    } else {
89110027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
89210027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
8938601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
89410027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
8955877Shsul@eecs.umich.edu    }
8965877Shsul@eecs.umich.edu}
8971999SN/A
898378SN/A/// Target stat() handler.
899360SN/Atemplate <class OS>
9001450SN/ASyscallReturn
9013114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9022680Sktlim@umich.edu         ThreadContext *tc)
903360SN/A{
904360SN/A    std::string path;
905360SN/A
9066701Sgblack@eecs.umich.edu    int index = 0;
9078852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9086701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9096701Sgblack@eecs.umich.edu        return -EFAULT;
9106701Sgblack@eecs.umich.edu    }
9116701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
912360SN/A
9133669Sbinkertn@umich.edu    // Adjust path for current working directory
9143669Sbinkertn@umich.edu    path = process->fullPath(path);
9153669Sbinkertn@umich.edu
916360SN/A    struct stat hostBuf;
917360SN/A    int result = stat(path.c_str(), &hostBuf);
918360SN/A
919360SN/A    if (result < 0)
9202218SN/A        return -errno;
921360SN/A
9228706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
923360SN/A
9241458SN/A    return 0;
925360SN/A}
926360SN/A
927360SN/A
9285074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9295074Ssaidi@eecs.umich.edutemplate <class OS>
9305074Ssaidi@eecs.umich.eduSyscallReturn
9315074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9325074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9335074Ssaidi@eecs.umich.edu{
9345074Ssaidi@eecs.umich.edu    std::string path;
9355074Ssaidi@eecs.umich.edu
9366701Sgblack@eecs.umich.edu    int index = 0;
9378852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9386701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9395074Ssaidi@eecs.umich.edu        return -EFAULT;
9406701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9415074Ssaidi@eecs.umich.edu
9425074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9435074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9445074Ssaidi@eecs.umich.edu
9455208Ssaidi@eecs.umich.edu#if NO_STAT64
9465208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9475208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9485208Ssaidi@eecs.umich.edu#else
9495074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9505074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9515208Ssaidi@eecs.umich.edu#endif
9525074Ssaidi@eecs.umich.edu
9535074Ssaidi@eecs.umich.edu    if (result < 0)
9545074Ssaidi@eecs.umich.edu        return -errno;
9555074Ssaidi@eecs.umich.edu
9568706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9575074Ssaidi@eecs.umich.edu
9585074Ssaidi@eecs.umich.edu    return 0;
9595074Ssaidi@eecs.umich.edu}
9605074Ssaidi@eecs.umich.edu
9615074Ssaidi@eecs.umich.edu
96210027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
96310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
96410027SChris.Adeniyi-Jones@arm.comSyscallReturn
96510027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
96610027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
96710027SChris.Adeniyi-Jones@arm.com{
96810027SChris.Adeniyi-Jones@arm.com    int index = 0;
96910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
97010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
97110793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
97210027SChris.Adeniyi-Jones@arm.com
97310027SChris.Adeniyi-Jones@arm.com    std::string path;
97410027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
97510027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
97610027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
97710027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
97810027SChris.Adeniyi-Jones@arm.com
97910027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
98010027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
98110027SChris.Adeniyi-Jones@arm.com
98210027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
98310027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
98410027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
98510027SChris.Adeniyi-Jones@arm.com#else
98610027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
98710027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
98810027SChris.Adeniyi-Jones@arm.com#endif
98910027SChris.Adeniyi-Jones@arm.com
99010027SChris.Adeniyi-Jones@arm.com    if (result < 0)
99110027SChris.Adeniyi-Jones@arm.com        return -errno;
99210027SChris.Adeniyi-Jones@arm.com
99310027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
99410027SChris.Adeniyi-Jones@arm.com
99510027SChris.Adeniyi-Jones@arm.com    return 0;
99610027SChris.Adeniyi-Jones@arm.com}
99710027SChris.Adeniyi-Jones@arm.com
99810027SChris.Adeniyi-Jones@arm.com
9991999SN/A/// Target fstat64() handler.
10001999SN/Atemplate <class OS>
10011999SN/ASyscallReturn
10023114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10032680Sktlim@umich.edu            ThreadContext *tc)
10041999SN/A{
10056701Sgblack@eecs.umich.edu    int index = 0;
100610931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
10076701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
100810931Sbrandon.potter@amd.com
100910932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
101010931Sbrandon.potter@amd.com    if (sim_fd < 0)
10111999SN/A        return -EBADF;
10121999SN/A
10132764Sstever@eecs.umich.edu#if NO_STAT64
10142064SN/A    struct stat  hostBuf;
101510931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10162064SN/A#else
10172064SN/A    struct stat64  hostBuf;
101810931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10192064SN/A#endif
10201999SN/A
10211999SN/A    if (result < 0)
10222218SN/A        return -errno;
10231999SN/A
102410931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10251999SN/A
10261999SN/A    return 0;
10271999SN/A}
10281999SN/A
10291999SN/A
1030378SN/A/// Target lstat() handler.
1031360SN/Atemplate <class OS>
10321450SN/ASyscallReturn
10333114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10342680Sktlim@umich.edu          ThreadContext *tc)
1035360SN/A{
1036360SN/A    std::string path;
1037360SN/A
10386701Sgblack@eecs.umich.edu    int index = 0;
10398852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10406701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10416701Sgblack@eecs.umich.edu        return -EFAULT;
10426701Sgblack@eecs.umich.edu    }
10436701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1044360SN/A
10453669Sbinkertn@umich.edu    // Adjust path for current working directory
10463669Sbinkertn@umich.edu    path = process->fullPath(path);
10473669Sbinkertn@umich.edu
1048360SN/A    struct stat hostBuf;
1049360SN/A    int result = lstat(path.c_str(), &hostBuf);
1050360SN/A
1051360SN/A    if (result < 0)
10521458SN/A        return -errno;
1053360SN/A
10548706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1055360SN/A
10561458SN/A    return 0;
1057360SN/A}
1058360SN/A
10591999SN/A/// Target lstat64() handler.
10601999SN/Atemplate <class OS>
10611999SN/ASyscallReturn
10623114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10632680Sktlim@umich.edu            ThreadContext *tc)
10641999SN/A{
10651999SN/A    std::string path;
10661999SN/A
10676701Sgblack@eecs.umich.edu    int index = 0;
10688852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10696701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10706701Sgblack@eecs.umich.edu        return -EFAULT;
10716701Sgblack@eecs.umich.edu    }
10726701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10731999SN/A
10743669Sbinkertn@umich.edu    // Adjust path for current working directory
10753669Sbinkertn@umich.edu    path = process->fullPath(path);
10763669Sbinkertn@umich.edu
10772764Sstever@eecs.umich.edu#if NO_STAT64
10782064SN/A    struct stat hostBuf;
10792064SN/A    int result = lstat(path.c_str(), &hostBuf);
10802064SN/A#else
10811999SN/A    struct stat64 hostBuf;
10821999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10832064SN/A#endif
10841999SN/A
10851999SN/A    if (result < 0)
10861999SN/A        return -errno;
10871999SN/A
10888706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10891999SN/A
10901999SN/A    return 0;
10911999SN/A}
10921999SN/A
1093378SN/A/// Target fstat() handler.
1094360SN/Atemplate <class OS>
10951450SN/ASyscallReturn
10963114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10972680Sktlim@umich.edu          ThreadContext *tc)
1098360SN/A{
10996701Sgblack@eecs.umich.edu    int index = 0;
110010931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11016701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1102360SN/A
110310931Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "fstat(%d, ...)\n", tgt_fd);
1104360SN/A
110510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
110610931Sbrandon.potter@amd.com    if (sim_fd < 0)
11071458SN/A        return -EBADF;
1108360SN/A
1109360SN/A    struct stat hostBuf;
111010931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1111360SN/A
1112360SN/A    if (result < 0)
11131458SN/A        return -errno;
1114360SN/A
111510931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11162021SN/A
11171458SN/A    return 0;
1118360SN/A}
1119360SN/A
1120360SN/A
11211706SN/A/// Target statfs() handler.
11221706SN/Atemplate <class OS>
11231706SN/ASyscallReturn
11243114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11252680Sktlim@umich.edu           ThreadContext *tc)
11261706SN/A{
11271706SN/A    std::string path;
11281706SN/A
11296701Sgblack@eecs.umich.edu    int index = 0;
11308852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11316701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11326701Sgblack@eecs.umich.edu        return -EFAULT;
11336701Sgblack@eecs.umich.edu    }
11346701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11351706SN/A
11363669Sbinkertn@umich.edu    // Adjust path for current working directory
11373669Sbinkertn@umich.edu    path = process->fullPath(path);
11383669Sbinkertn@umich.edu
11391706SN/A    struct statfs hostBuf;
11401706SN/A    int result = statfs(path.c_str(), &hostBuf);
11411706SN/A
11421706SN/A    if (result < 0)
11432218SN/A        return -errno;
11441706SN/A
11458706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11461706SN/A
11471706SN/A    return 0;
11481706SN/A}
11491706SN/A
11501706SN/A
11511706SN/A/// Target fstatfs() handler.
11521706SN/Atemplate <class OS>
11531706SN/ASyscallReturn
11543114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11552680Sktlim@umich.edu            ThreadContext *tc)
11561706SN/A{
11576701Sgblack@eecs.umich.edu    int index = 0;
115810931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11596701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11601706SN/A
116110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
116210931Sbrandon.potter@amd.com    if (sim_fd < 0)
11631706SN/A        return -EBADF;
11641706SN/A
11651706SN/A    struct statfs hostBuf;
116610931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11671706SN/A
11681706SN/A    if (result < 0)
11692218SN/A        return -errno;
11701706SN/A
11718706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11721706SN/A
11731706SN/A    return 0;
11741706SN/A}
11751706SN/A
11761706SN/A
11771999SN/A/// Target writev() handler.
11781999SN/Atemplate <class OS>
11791999SN/ASyscallReturn
11803114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11812680Sktlim@umich.edu           ThreadContext *tc)
11821999SN/A{
11836701Sgblack@eecs.umich.edu    int index = 0;
118410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
118510931Sbrandon.potter@amd.com
118610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
118710931Sbrandon.potter@amd.com    if (sim_fd < 0)
11881999SN/A        return -EBADF;
11891999SN/A
11908852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
11916701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
11926701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
11931999SN/A    struct iovec hiov[count];
11946227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
11951999SN/A        typename OS::tgt_iovec tiov;
11962461SN/A
11978852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
11988852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
11998737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
12001999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
12018852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12028852Sandreas.hansson@arm.com                   hiov[i].iov_len);
12031999SN/A    }
12041999SN/A
120510931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
12061999SN/A
12076227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
12081999SN/A        delete [] (char *)hiov[i].iov_base;
12091999SN/A
12101999SN/A    if (result < 0)
12112218SN/A        return -errno;
12121999SN/A
121310629Sjthestness@gmail.com    return result;
12141999SN/A}
12151999SN/A
12161999SN/A
1217378SN/A/// Target mmap() handler.
1218378SN/A///
1219378SN/A/// We don't really handle mmap().  If the target is mmaping an
1220378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1221378SN/A/// nothing (since memory is initialized to zero and the simulator
12228324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
12238324Ssteve.reinhardt@amd.com///
1224360SN/Atemplate <class OS>
12251450SN/ASyscallReturn
12263114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1227360SN/A{
12286701Sgblack@eecs.umich.edu    int index = 0;
12296701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12306701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
12316701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
12326701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
12338324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
123410486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1235360SN/A
12369008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
12379008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12389008Sgblack@eecs.umich.edu
12398324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
124010932Sbrandon.potter@amd.com        FDEntry *fde = p->getFDEntry(tgt_fd);
124110930Sbrandon.potter@amd.com        if (!fde || fde->fd < 0) {
12428324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12438324Ssteve.reinhardt@amd.com            return -EBADF;
12448324Ssteve.reinhardt@amd.com        }
12458324Ssteve.reinhardt@amd.com
124610930Sbrandon.potter@amd.com        if (fde->filename != "/dev/zero") {
12478324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12488324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12498324Ssteve.reinhardt@amd.com            // another name on some platform
12508324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
125110930Sbrandon.potter@amd.com                 " other than /dev/zero\n", fde->filename);
12528324Ssteve.reinhardt@amd.com        }
12538324Ssteve.reinhardt@amd.com    }
12545877Shsul@eecs.umich.edu
125510486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
125610486Stjablin@gmail.com
125710318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
125810486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12592544SN/A        warn("mmap failing: arguments not page-aligned: "
126010486Stjablin@gmail.com             "start 0x%x offset 0x%x",
126110486Stjablin@gmail.com             start, offset);
12622544SN/A        return -EINVAL;
1263360SN/A    }
1264360SN/A
12658600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12668600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12678600Ssteve.reinhardt@amd.com    bool clobber = false;
12688600Ssteve.reinhardt@amd.com
12698600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12708600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12718600Ssteve.reinhardt@amd.com
12728600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12738600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12748600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12758600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12768600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12778600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12788600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
127910485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
128010485SMichael.Adler@intel.com                return -EINVAL;
12818600Ssteve.reinhardt@amd.com            } else {
12828600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12838600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12848600Ssteve.reinhardt@amd.com                use_provided_address = false;
12858600Ssteve.reinhardt@amd.com            }
12868600Ssteve.reinhardt@amd.com        }
12872544SN/A    }
12882544SN/A
12898600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12908600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
12918600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
12928600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
12938600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
12948600Ssteve.reinhardt@amd.com            p->mmap_end = start;
12958600Ssteve.reinhardt@amd.com        } else {
12968600Ssteve.reinhardt@amd.com            start = p->mmap_end;
12978600Ssteve.reinhardt@amd.com            p->mmap_end += length;
12988600Ssteve.reinhardt@amd.com        }
12996672Sgblack@eecs.umich.edu    }
13008600Ssteve.reinhardt@amd.com
13018601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
13022544SN/A
13031458SN/A    return start;
1304360SN/A}
1305360SN/A
1306378SN/A/// Target getrlimit() handler.
1307360SN/Atemplate <class OS>
13081450SN/ASyscallReturn
13093114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13102680Sktlim@umich.edu        ThreadContext *tc)
1311360SN/A{
13126701Sgblack@eecs.umich.edu    int index = 0;
13136701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
13146701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1315360SN/A
1316360SN/A    switch (resource) {
13172064SN/A        case OS::TGT_RLIMIT_STACK:
13185877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
13192064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
13208737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13218737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13222064SN/A            break;
1323360SN/A
13245877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
13255877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
13265877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
13278737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13288737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13295877Shsul@eecs.umich.edu            break;
13305877Shsul@eecs.umich.edu
13312064SN/A        default:
133210794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
133310794Sbrandon.potter@amd.com            return -EINVAL;
13342064SN/A            break;
1335360SN/A    }
1336360SN/A
13378706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13381458SN/A    return 0;
1339360SN/A}
1340360SN/A
134110796Sbrandon.potter@amd.com/// Target clock_gettime() function.
134210796Sbrandon.potter@amd.comtemplate <class OS>
134310796Sbrandon.potter@amd.comSyscallReturn
134410796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
134510796Sbrandon.potter@amd.com{
134610796Sbrandon.potter@amd.com    int index = 1;
134710796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
134810796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
134910796Sbrandon.potter@amd.com
135010796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
135110796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
135210796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
135310796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
135410796Sbrandon.potter@amd.com
135510796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
135610796Sbrandon.potter@amd.com
135710796Sbrandon.potter@amd.com    return 0;
135810796Sbrandon.potter@amd.com}
135910796Sbrandon.potter@amd.com
1360378SN/A/// Target gettimeofday() handler.
1361360SN/Atemplate <class OS>
13621450SN/ASyscallReturn
13633114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13642680Sktlim@umich.edu        ThreadContext *tc)
1365360SN/A{
13666701Sgblack@eecs.umich.edu    int index = 0;
13676701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1368360SN/A
136910796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1370360SN/A    tp->tv_sec += seconds_since_epoch;
13716109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13726109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1373360SN/A
13748706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1375360SN/A
13761458SN/A    return 0;
1377360SN/A}
1378360SN/A
1379360SN/A
13801999SN/A/// Target utimes() handler.
13811999SN/Atemplate <class OS>
13821999SN/ASyscallReturn
13833114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13842680Sktlim@umich.edu           ThreadContext *tc)
13851999SN/A{
13861999SN/A    std::string path;
13871999SN/A
13886701Sgblack@eecs.umich.edu    int index = 0;
13898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13906701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13916701Sgblack@eecs.umich.edu        return -EFAULT;
13926701Sgblack@eecs.umich.edu    }
13931999SN/A
13946701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
13956701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
13968706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
13971999SN/A
13981999SN/A    struct timeval hostTimeval[2];
13991999SN/A    for (int i = 0; i < 2; ++i)
14001999SN/A    {
14018737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
14028737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
14031999SN/A    }
14043669Sbinkertn@umich.edu
14053669Sbinkertn@umich.edu    // Adjust path for current working directory
14063669Sbinkertn@umich.edu    path = process->fullPath(path);
14073669Sbinkertn@umich.edu
14081999SN/A    int result = utimes(path.c_str(), hostTimeval);
14091999SN/A
14101999SN/A    if (result < 0)
14111999SN/A        return -errno;
14121999SN/A
14131999SN/A    return 0;
14141999SN/A}
1415378SN/A/// Target getrusage() function.
1416360SN/Atemplate <class OS>
14171450SN/ASyscallReturn
14183114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14192680Sktlim@umich.edu              ThreadContext *tc)
1420360SN/A{
14216701Sgblack@eecs.umich.edu    int index = 0;
14226701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
14236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1424360SN/A
14253670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
14263670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1427360SN/A    rup->ru_stime.tv_sec = 0;
1428360SN/A    rup->ru_stime.tv_usec = 0;
1429360SN/A    rup->ru_maxrss = 0;
1430360SN/A    rup->ru_ixrss = 0;
1431360SN/A    rup->ru_idrss = 0;
1432360SN/A    rup->ru_isrss = 0;
1433360SN/A    rup->ru_minflt = 0;
1434360SN/A    rup->ru_majflt = 0;
1435360SN/A    rup->ru_nswap = 0;
1436360SN/A    rup->ru_inblock = 0;
1437360SN/A    rup->ru_oublock = 0;
1438360SN/A    rup->ru_msgsnd = 0;
1439360SN/A    rup->ru_msgrcv = 0;
1440360SN/A    rup->ru_nsignals = 0;
1441360SN/A    rup->ru_nvcsw = 0;
1442360SN/A    rup->ru_nivcsw = 0;
1443360SN/A
14443670Sbinkertn@umich.edu    switch (who) {
14453670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
144610796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14478737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14488737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14493670Sbinkertn@umich.edu        break;
14503670Sbinkertn@umich.edu
14513670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14523670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14533670Sbinkertn@umich.edu        break;
14543670Sbinkertn@umich.edu
14553670Sbinkertn@umich.edu      default:
14563670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14573670Sbinkertn@umich.edu        // plow ahead
14583670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14593670Sbinkertn@umich.edu             who);
14603670Sbinkertn@umich.edu    }
14613670Sbinkertn@umich.edu
14628706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1463360SN/A
14641458SN/A    return 0;
1465360SN/A}
1466360SN/A
14676683Stjones1@inf.ed.ac.uk/// Target times() function.
14686683Stjones1@inf.ed.ac.uktemplate <class OS>
14696683Stjones1@inf.ed.ac.ukSyscallReturn
14706683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14716683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14726683Stjones1@inf.ed.ac.uk{
14736701Sgblack@eecs.umich.edu    int index = 0;
14746701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
14756683Stjones1@inf.ed.ac.uk
14766683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
14777823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
14786683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
14796683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
14806683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
14816683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
14826683Stjones1@inf.ed.ac.uk
14836683Stjones1@inf.ed.ac.uk    // Convert to host endianness
14848737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
14856683Stjones1@inf.ed.ac.uk
14866683Stjones1@inf.ed.ac.uk    // Write back
14878706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
14886683Stjones1@inf.ed.ac.uk
14896683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
14906683Stjones1@inf.ed.ac.uk    return clocks;
14916683Stjones1@inf.ed.ac.uk}
14922553SN/A
14936684Stjones1@inf.ed.ac.uk/// Target time() function.
14946684Stjones1@inf.ed.ac.uktemplate <class OS>
14956684Stjones1@inf.ed.ac.ukSyscallReturn
14966684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14976684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14986684Stjones1@inf.ed.ac.uk{
14996684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
150010796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
15016684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
15026684Stjones1@inf.ed.ac.uk
15036701Sgblack@eecs.umich.edu    int index = 0;
15046701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
150511321Ssteve.reinhardt@amd.com    if (taddr != 0) {
15066684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
15078737Skoansin.tan@gmail.com        t = TheISA::htog(t);
15088852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
15098852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
15106684Stjones1@inf.ed.ac.uk    }
15116684Stjones1@inf.ed.ac.uk    return sec;
15126684Stjones1@inf.ed.ac.uk}
15132553SN/A
15142553SN/A
15151354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1516