syscall_emul.hh revision 11380
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"
7711380Salexandru.dutu@amd.com#include "debug/SyscallBase.hh"
788232Snate@binkert.org#include "debug/SyscallVerbose.hh"
798229Snate@binkert.org#include "mem/page_table.hh"
807678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
8110496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
828229Snate@binkert.org#include "sim/process.hh"
8310497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
848766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh"
856640Svince@csl.cornell.edu#include "sim/system.hh"
86360SN/A
8711380Salexandru.dutu@amd.com// This wrapper macro helps out with readability a bit. FLAGEXT specifies
8811380Salexandru.dutu@amd.com// the verbosity and FMT is the message to be appended to the syscall
8911380Salexandru.dutu@amd.com// header information. The syscall header information contains the cpuid
9011380Salexandru.dutu@amd.com// and thread id.
9111380Salexandru.dutu@amd.com#define DPRINTF_SYSCALL(FLAGEXT, FMT, ...)                                  \
9211380Salexandru.dutu@amd.com    DPRINTFS(Syscall##FLAGEXT, tc->getCpuPtr(), "T%d : syscall " FMT,       \
9311380Salexandru.dutu@amd.com             tc->threadId(), __VA_ARGS__)
9411380Salexandru.dutu@amd.com
95360SN/A///
96360SN/A/// System call descriptor.
97360SN/A///
98360SN/Aclass SyscallDesc {
99360SN/A
100360SN/A  public:
101360SN/A
102378SN/A    /// Typedef for target syscall handler functions.
1031450SN/A    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
1043114Sgblack@eecs.umich.edu                           LiveProcess *, ThreadContext *);
105360SN/A
1065543Ssaidi@eecs.umich.edu    const char *name;   //!< Syscall name (e.g., "open").
1075543Ssaidi@eecs.umich.edu    FuncPtr funcPtr;    //!< Pointer to emulation function.
1085543Ssaidi@eecs.umich.edu    int flags;          //!< Flags (see Flags enum).
10910831Ssteve.reinhardt@amd.com    bool warned;        //!< Have we warned about unimplemented syscall?
110360SN/A
111360SN/A    /// Flag values for controlling syscall behavior.
112360SN/A    enum Flags {
113360SN/A        /// Don't set return regs according to funcPtr return value.
114360SN/A        /// Used for syscalls with non-standard return conventions
1152680Sktlim@umich.edu        /// that explicitly set the ThreadContext regs (e.g.,
116360SN/A        /// sigreturn).
11710831Ssteve.reinhardt@amd.com        SuppressReturnValue = 1,
11810831Ssteve.reinhardt@amd.com        WarnOnce = 2
119360SN/A    };
120360SN/A
121360SN/A    /// Constructor.
122360SN/A    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
12310831Ssteve.reinhardt@amd.com        : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
124360SN/A    {
125360SN/A    }
126360SN/A
127360SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1283114Sgblack@eecs.umich.edu    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
12910831Ssteve.reinhardt@amd.com
13010831Ssteve.reinhardt@amd.com    /// Is the WarnOnce flag set?
13110831Ssteve.reinhardt@amd.com    bool warnOnce() const {  return (flags & WarnOnce); }
132360SN/A};
133360SN/A
134360SN/A
135360SN/A//////////////////////////////////////////////////////////////////////
136360SN/A//
137360SN/A// The following emulation functions are generic enough that they
138360SN/A// don't need to be recompiled for different emulated OS's.  They are
139360SN/A// defined in sim/syscall_emul.cc.
140360SN/A//
141360SN/A//////////////////////////////////////////////////////////////////////
142360SN/A
143360SN/A
144378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1451706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1463114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
147378SN/A
148378SN/A/// Handler for unimplemented syscalls that we never intend to
149378SN/A/// implement (signal handling, etc.) and should not affect the correct
150378SN/A/// behavior of the program.  Print a warning only if the appropriate
151378SN/A/// trace flag is enabled.  Return success to the target program.
1521706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1533114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
154360SN/A
1556109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1561706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1573114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
158378SN/A
1596109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1606109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1616109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1626109Ssanchezd@stanford.edu
163378SN/A/// Target getpagesize() handler.
1641706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1653114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
166378SN/A
1675748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1685748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1695748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
170378SN/A
171378SN/A/// Target close() handler.
1721706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1733114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
174378SN/A
175378SN/A/// Target read() handler.
1761706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1773114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
178378SN/A
179378SN/A/// Target write() handler.
1801706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1813114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
182378SN/A
183378SN/A/// Target lseek() handler.
1841706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1853114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
186378SN/A
1874118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1884118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1894118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1904118Sgblack@eecs.umich.edu
191378SN/A/// Target munmap() handler.
1921706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1933114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
194378SN/A
195378SN/A/// Target gethostname() handler.
1961706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1973114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
198360SN/A
1995513SMichael.Adler@intel.com/// Target getcwd() handler.
2005513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2015513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2025513SMichael.Adler@intel.com
20310203SAli.Saidi@ARM.com/// Target readlink() handler.
20410203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
20510203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
20610203SAli.Saidi@ARM.com                           int index = 0);
2075513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2085513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
2095513SMichael.Adler@intel.com
210511SN/A/// Target unlink() handler.
21110633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
21210633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
21310633Smichaelupton@gmail.com                           int index);
2141706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2153114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
216511SN/A
2175513SMichael.Adler@intel.com/// Target mkdir() handler.
2185513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2195513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2205513SMichael.Adler@intel.com
221511SN/A/// Target rename() handler.
2221706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
2233114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2241706SN/A
2251706SN/A
2261706SN/A/// Target truncate() handler.
2271706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
2283114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
2291706SN/A
2301706SN/A
2311706SN/A/// Target ftruncate() handler.
2321706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2333114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2341706SN/A
235511SN/A
2366703Svince@csl.cornell.edu/// Target truncate64() handler.
2376703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2386703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2396703Svince@csl.cornell.edu
2406685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2416685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2426685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2436685Stjones1@inf.ed.ac.uk
2446685Stjones1@inf.ed.ac.uk
2455513SMichael.Adler@intel.com/// Target umask() handler.
2465513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2475513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2485513SMichael.Adler@intel.com
2495513SMichael.Adler@intel.com
2501999SN/A/// Target chown() handler.
2511999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2523114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2531999SN/A
2541999SN/A
2551999SN/A/// Target fchown() handler.
2561999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2573114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2581999SN/A
2593079Sstever@eecs.umich.edu/// Target dup() handler.
2603079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2613114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2623079Sstever@eecs.umich.edu
2632093SN/A/// Target fnctl() handler.
2642093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2653114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2662093SN/A
2672687Sksewell@umich.edu/// Target fcntl64() handler.
2682687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2693114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2702687Sksewell@umich.edu
2712238SN/A/// Target setuid() handler.
2722238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2733114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2742238SN/A
2752238SN/A/// Target getpid() handler.
2762238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2773114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2782238SN/A
2792238SN/A/// Target getuid() handler.
2802238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2813114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2822238SN/A
2832238SN/A/// Target getgid() handler.
2842238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2853114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2862238SN/A
2872238SN/A/// Target getppid() handler.
2882238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2893114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2902238SN/A
2912238SN/A/// Target geteuid() handler.
2922238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2933114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2942238SN/A
2952238SN/A/// Target getegid() handler.
2962238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2973114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2982238SN/A
2996109Ssanchezd@stanford.edu/// Target clone() handler.
3006109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
3016109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
3022238SN/A
3039455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3049455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3059455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
30610203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
30710203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
30810203SAli.Saidi@ARM.com                               int index);
3099455Smitch.hayenga+gem5@gmail.com
3109112Smarc.orr@gmail.com/// Futex system call
3119112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3129112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
3139112Smarc.orr@gmail.comtemplate <class OS>
3149112Smarc.orr@gmail.comSyscallReturn
3159112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
3169112Smarc.orr@gmail.com          ThreadContext *tc)
3179112Smarc.orr@gmail.com{
3189112Smarc.orr@gmail.com    int index_uaddr = 0;
3199112Smarc.orr@gmail.com    int index_op = 1;
3209112Smarc.orr@gmail.com    int index_val = 2;
3219112Smarc.orr@gmail.com    int index_timeout = 3;
3229112Smarc.orr@gmail.com
3239112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3249112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3259112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3269112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3279112Smarc.orr@gmail.com
3289112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3299112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3309112Smarc.orr@gmail.com
3319112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3329112Smarc.orr@gmail.com            uaddr, op, val);
3339112Smarc.orr@gmail.com
3349238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3359112Smarc.orr@gmail.com
3369112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3379112Smarc.orr@gmail.com        if (timeout != 0) {
3389112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3399112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3409112Smarc.orr@gmail.com        }
3419112Smarc.orr@gmail.com
3429112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3439112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3449112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
34511367Sandreas.hansson@arm.com        delete[] buf;
3469112Smarc.orr@gmail.com
34711321Ssteve.reinhardt@amd.com        if (val != mem_val) {
3489112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3499112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3509112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3519112Smarc.orr@gmail.com        }
3529112Smarc.orr@gmail.com
3539112Smarc.orr@gmail.com        // Queue the thread context
3549112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3559112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3569112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3579112Smarc.orr@gmail.com        } else {
3589112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3599112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3609112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3619112Smarc.orr@gmail.com        }
3629112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3639112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3649112Smarc.orr@gmail.com                                "thread context\n");
3659112Smarc.orr@gmail.com        tc->suspend();
3669112Smarc.orr@gmail.com        return 0;
3679112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3689112Smarc.orr@gmail.com        int wokenUp = 0;
3699112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3709112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3719112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3729112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3739112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3749112Smarc.orr@gmail.com                tcWaitList->pop_front();
3759112Smarc.orr@gmail.com                wokenUp++;
3769112Smarc.orr@gmail.com            }
37711321Ssteve.reinhardt@amd.com            if (tcWaitList->empty()) {
3789112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3799112Smarc.orr@gmail.com                delete tcWaitList;
3809112Smarc.orr@gmail.com            }
3819112Smarc.orr@gmail.com        }
3829112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3839112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3849112Smarc.orr@gmail.com        return wokenUp;
3859112Smarc.orr@gmail.com    } else {
3869238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3879112Smarc.orr@gmail.com        return 0;
3889112Smarc.orr@gmail.com    }
3899112Smarc.orr@gmail.com
3909112Smarc.orr@gmail.com}
3919112Smarc.orr@gmail.com
3922238SN/A
3932238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3942238SN/A/// returning a second value in a register other than the normal return register
3952238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3963114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3972238SN/A
3982238SN/A/// Target getpidPseudo() handler.
3992238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
4003114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4012238SN/A
4022238SN/A/// Target getuidPseudo() handler.
4032238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
4043114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4052238SN/A
4062238SN/A/// Target getgidPseudo() handler.
4072238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
4083114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
4092238SN/A
4102238SN/A
4111354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4121354SN/Aconst int one_million = 1000000;
41310796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
41410796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4151354SN/A
4161354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4171354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4181354SN/A/// real-world time) to keep simulations repeatable.
4191354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4201354SN/A
4211354SN/A/// Helper function to convert current elapsed time to seconds and
4221354SN/A/// microseconds.
4231354SN/Atemplate <class T1, class T2>
4241354SN/Avoid
42510796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4261354SN/A{
42710796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4281354SN/A    sec = elapsed_usecs / one_million;
4291354SN/A    usec = elapsed_usecs % one_million;
4301354SN/A}
4311354SN/A
43210796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
43310796Sbrandon.potter@amd.com/// nanoseconds.
43410796Sbrandon.potter@amd.comtemplate <class T1, class T2>
43510796Sbrandon.potter@amd.comvoid
43610796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
43710796Sbrandon.potter@amd.com{
43810796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
43910796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
44010796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
44110796Sbrandon.potter@amd.com}
44210796Sbrandon.potter@amd.com
443360SN/A//////////////////////////////////////////////////////////////////////
444360SN/A//
445360SN/A// The following emulation functions are generic, but need to be
446360SN/A// templated to account for differences in types, constants, etc.
447360SN/A//
448360SN/A//////////////////////////////////////////////////////////////////////
449360SN/A
4503113Sgblack@eecs.umich.edu#if NO_STAT64
4513113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4523113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4533113Sgblack@eecs.umich.edu#else
4543113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4553113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4563113Sgblack@eecs.umich.edu#endif
4573113Sgblack@eecs.umich.edu
4583113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4593113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4603113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4613113Sgblack@eecs.umich.edu
4623113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4633113Sgblack@eecs.umich.edustatic void
4643113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4653113Sgblack@eecs.umich.edu{
4664189Sgblack@eecs.umich.edu    using namespace TheISA;
4674189Sgblack@eecs.umich.edu
4683113Sgblack@eecs.umich.edu    if (fakeTTY)
4693113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4703113Sgblack@eecs.umich.edu    else
4713113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4728737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4733113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4748737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4753277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4765515SMichael.Adler@intel.com    if (fakeTTY) {
4775515SMichael.Adler@intel.com        // Claim to be a character device
4785515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4795515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4805515SMichael.Adler@intel.com    }
4818737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4823277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4838737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4843277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4858737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4863277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4878737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4883113Sgblack@eecs.umich.edu    if (fakeTTY)
4893113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4903113Sgblack@eecs.umich.edu    else
4913113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4928737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4933113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4948737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4953114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4968737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4973114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4988737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4993114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5008737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
5014061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
5024061Sgblack@eecs.umich.edu    // consistently across different hosts.
5034061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5048737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5053113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5068737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5073113Sgblack@eecs.umich.edu}
5083113Sgblack@eecs.umich.edu
5093113Sgblack@eecs.umich.edu// Same for stat64
5103113Sgblack@eecs.umich.edu
5113113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
5123113Sgblack@eecs.umich.edustatic void
5133113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5143113Sgblack@eecs.umich.edu{
5154189Sgblack@eecs.umich.edu    using namespace TheISA;
5164189Sgblack@eecs.umich.edu
5173113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5183113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5193113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5208737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5213113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5228737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5233113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5248737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5253113Sgblack@eecs.umich.edu#else
5263113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5273113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5283113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5293113Sgblack@eecs.umich.edu#endif
5303113Sgblack@eecs.umich.edu}
5313113Sgblack@eecs.umich.edu
5323113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5333113Sgblack@eecs.umich.edutemplate<class OS>
5343113Sgblack@eecs.umich.edustatic void
5358852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5363113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5373113Sgblack@eecs.umich.edu{
5383113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5393113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5403113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5413113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5423113Sgblack@eecs.umich.edu}
5433113Sgblack@eecs.umich.edu
5443113Sgblack@eecs.umich.edutemplate<class OS>
5453113Sgblack@eecs.umich.edustatic void
5468852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5473113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5483113Sgblack@eecs.umich.edu{
5493113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5503113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5516686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5523113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5533113Sgblack@eecs.umich.edu}
5543113Sgblack@eecs.umich.edu
555378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
556378SN/A/// only to find out if their stdout is a tty, to determine whether to
5579141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5589141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
559360SN/Atemplate <class OS>
5601450SN/ASyscallReturn
5613114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5622680Sktlim@umich.edu          ThreadContext *tc)
563360SN/A{
5646701Sgblack@eecs.umich.edu    int index = 0;
56510930Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5666701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
567360SN/A
56810930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
569360SN/A
57010932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
57110496Ssteve.reinhardt@amd.com
57210930Sbrandon.potter@amd.com    if (fde == NULL) {
573360SN/A        // doesn't map to any simulator fd: not a valid target fd
5741458SN/A        return -EBADF;
575360SN/A    }
576360SN/A
57710930Sbrandon.potter@amd.com    if (fde->driver != NULL) {
57810930Sbrandon.potter@amd.com        return fde->driver->ioctl(process, tc, req);
57910496Ssteve.reinhardt@amd.com    }
58010496Ssteve.reinhardt@amd.com
5819141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5821458SN/A        return -ENOTTY;
5839141Smarc.orr@gmail.com    }
584360SN/A
5859141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
58610930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
5879141Smarc.orr@gmail.com    return -ENOTTY;
588360SN/A}
589360SN/A
590360SN/Atemplate <class OS>
59110027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5923114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
59310027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
594360SN/A{
595360SN/A    std::string path;
596360SN/A
5978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5986701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5991458SN/A        return -EFAULT;
600360SN/A
6016701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6026701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
603360SN/A    int hostFlags = 0;
604360SN/A
605360SN/A    // translate open flags
606360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
607360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
608360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
609360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
610360SN/A        }
611360SN/A    }
612360SN/A
613360SN/A    // any target flags left?
614360SN/A    if (tgtFlags != 0)
6151706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
616360SN/A
617360SN/A#ifdef __CYGWIN32__
618360SN/A    hostFlags |= O_BINARY;
619360SN/A#endif
620360SN/A
6213669Sbinkertn@umich.edu    // Adjust path for current working directory
6223669Sbinkertn@umich.edu    path = process->fullPath(path);
6233669Sbinkertn@umich.edu
6241706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6251706SN/A
62610496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
62710496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
62810496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
62910496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
63010496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
63110496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
63210496Ssteve.reinhardt@amd.com            return -ENOENT;
63310496Ssteve.reinhardt@amd.com        }
63410496Ssteve.reinhardt@amd.com
63510496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
63610496Ssteve.reinhardt@amd.com        if (drv != NULL) {
63710496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
63810496Ssteve.reinhardt@amd.com            // process if necessary.
63910496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
64010496Ssteve.reinhardt@amd.com        }
64110496Ssteve.reinhardt@amd.com
64210496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
64310496Ssteve.reinhardt@amd.com        // /dev/zero
64410496Ssteve.reinhardt@amd.com    }
64510496Ssteve.reinhardt@amd.com
6465795Ssaidi@eecs.umich.edu    int fd;
6479143Ssteve.reinhardt@amd.com    int local_errno;
6489142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6499142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6509143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6515795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6529143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6535795Ssaidi@eecs.umich.edu     } else {
6545795Ssaidi@eecs.umich.edu        // open the file
6555795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6569143Ssteve.reinhardt@amd.com        local_errno = errno;
6575795Ssaidi@eecs.umich.edu     }
658360SN/A
6599143Ssteve.reinhardt@amd.com    if (fd == -1)
6609143Ssteve.reinhardt@amd.com        return -local_errno;
6619143Ssteve.reinhardt@amd.com
66210932Sbrandon.potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
663360SN/A}
664360SN/A
66510027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
66610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
66710027SChris.Adeniyi-Jones@arm.comSyscallReturn
66810027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66910027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67010027SChris.Adeniyi-Jones@arm.com{
67110027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
67210027SChris.Adeniyi-Jones@arm.com}
67310027SChris.Adeniyi-Jones@arm.com
67410027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
67510027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67610027SChris.Adeniyi-Jones@arm.comSyscallReturn
67710027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67810027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67910027SChris.Adeniyi-Jones@arm.com{
68010027SChris.Adeniyi-Jones@arm.com    int index = 0;
68110027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
68210027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
68310027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
68410027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
68510027SChris.Adeniyi-Jones@arm.com}
68610027SChris.Adeniyi-Jones@arm.com
68710633Smichaelupton@gmail.com/// Target unlinkat() handler.
68810633Smichaelupton@gmail.comtemplate <class OS>
68910633Smichaelupton@gmail.comSyscallReturn
69010633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69110633Smichaelupton@gmail.com             ThreadContext *tc)
69210633Smichaelupton@gmail.com{
69310633Smichaelupton@gmail.com    int index = 0;
69410633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
69510633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
69610633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
69710633Smichaelupton@gmail.com
69810633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
69910633Smichaelupton@gmail.com}
70010633Smichaelupton@gmail.com
70110203SAli.Saidi@ARM.com/// Target facessat() handler
70210203SAli.Saidi@ARM.comtemplate <class OS>
70310203SAli.Saidi@ARM.comSyscallReturn
70410203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70510203SAli.Saidi@ARM.com        ThreadContext *tc)
70610203SAli.Saidi@ARM.com{
70710203SAli.Saidi@ARM.com    int index = 0;
70810203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
70910203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
71010203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
71110203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
71210203SAli.Saidi@ARM.com}
71310203SAli.Saidi@ARM.com
71410203SAli.Saidi@ARM.com/// Target readlinkat() handler
71510203SAli.Saidi@ARM.comtemplate <class OS>
71610203SAli.Saidi@ARM.comSyscallReturn
71710203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71810203SAli.Saidi@ARM.com        ThreadContext *tc)
71910203SAli.Saidi@ARM.com{
72010203SAli.Saidi@ARM.com    int index = 0;
72110203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
72210203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
72310203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72410203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
72510203SAli.Saidi@ARM.com}
72610203SAli.Saidi@ARM.com
72710850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
72810850SGiacomo.Gabrielli@arm.comtemplate <class OS>
72910850SGiacomo.Gabrielli@arm.comSyscallReturn
73010850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
73110850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
73210850SGiacomo.Gabrielli@arm.com{
73310850SGiacomo.Gabrielli@arm.com    int index = 0;
73410850SGiacomo.Gabrielli@arm.com
73510850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
73610850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
73710850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
73810850SGiacomo.Gabrielli@arm.com
73910850SGiacomo.Gabrielli@arm.com    std::string old_name;
74010850SGiacomo.Gabrielli@arm.com
74110850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
74210850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
74310850SGiacomo.Gabrielli@arm.com        return -EFAULT;
74410850SGiacomo.Gabrielli@arm.com
74510850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
74610850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
74710850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
74810850SGiacomo.Gabrielli@arm.com
74910850SGiacomo.Gabrielli@arm.com    std::string new_name;
75010850SGiacomo.Gabrielli@arm.com
75110850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
75210850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
75310850SGiacomo.Gabrielli@arm.com        return -EFAULT;
75410850SGiacomo.Gabrielli@arm.com
75510850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
75610850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
75710850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
75810850SGiacomo.Gabrielli@arm.com
75910850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
76010850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
76110850SGiacomo.Gabrielli@arm.com}
76210850SGiacomo.Gabrielli@arm.com
7636640Svince@csl.cornell.edu/// Target sysinfo() handler.
7646640Svince@csl.cornell.edutemplate <class OS>
7656640Svince@csl.cornell.eduSyscallReturn
7666640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7676640Svince@csl.cornell.edu         ThreadContext *tc)
7686640Svince@csl.cornell.edu{
7696640Svince@csl.cornell.edu
7706701Sgblack@eecs.umich.edu    int index = 0;
7716701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
77210793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7736640Svince@csl.cornell.edu
7746701Sgblack@eecs.umich.edu    sysinfo->uptime=seconds_since_epoch;
7756701Sgblack@eecs.umich.edu    sysinfo->totalram=process->system->memSize();
7766640Svince@csl.cornell.edu
7778706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7786640Svince@csl.cornell.edu
7796701Sgblack@eecs.umich.edu    return 0;
7806640Svince@csl.cornell.edu}
781360SN/A
7821999SN/A/// Target chmod() handler.
7831999SN/Atemplate <class OS>
7841999SN/ASyscallReturn
7853114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7862680Sktlim@umich.edu          ThreadContext *tc)
7871999SN/A{
7881999SN/A    std::string path;
7891999SN/A
7906701Sgblack@eecs.umich.edu    int index = 0;
7918852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7926701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7931999SN/A        return -EFAULT;
7946701Sgblack@eecs.umich.edu    }
7951999SN/A
7966701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7971999SN/A    mode_t hostMode = 0;
7981999SN/A
7991999SN/A    // XXX translate mode flags via OS::something???
8001999SN/A    hostMode = mode;
8011999SN/A
8023669Sbinkertn@umich.edu    // Adjust path for current working directory
8033669Sbinkertn@umich.edu    path = process->fullPath(path);
8043669Sbinkertn@umich.edu
8051999SN/A    // do the chmod
8061999SN/A    int result = chmod(path.c_str(), hostMode);
8071999SN/A    if (result < 0)
8082218SN/A        return -errno;
8091999SN/A
8101999SN/A    return 0;
8111999SN/A}
8121999SN/A
8131999SN/A
8141999SN/A/// Target fchmod() handler.
8151999SN/Atemplate <class OS>
8161999SN/ASyscallReturn
8173114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8182680Sktlim@umich.edu           ThreadContext *tc)
8191999SN/A{
8206701Sgblack@eecs.umich.edu    int index = 0;
82110931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
82210931Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
82310931Sbrandon.potter@amd.com
82410932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
82510931Sbrandon.potter@amd.com    if (sim_fd < 0)
8261999SN/A        return -EBADF;
8271999SN/A
8281999SN/A    mode_t hostMode = 0;
8291999SN/A
8301999SN/A    // XXX translate mode flags via OS::someting???
8311999SN/A    hostMode = mode;
8321999SN/A
8331999SN/A    // do the fchmod
83410931Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
8351999SN/A    if (result < 0)
8362218SN/A        return -errno;
8371999SN/A
8381999SN/A    return 0;
8391999SN/A}
8401999SN/A
8415877Shsul@eecs.umich.edu/// Target mremap() handler.
8425877Shsul@eecs.umich.edutemplate <class OS>
8435877Shsul@eecs.umich.eduSyscallReturn
8445877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8455877Shsul@eecs.umich.edu{
8466701Sgblack@eecs.umich.edu    int index = 0;
8476701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8486701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8496701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8506701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
85110027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
85210027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
85310027SChris.Adeniyi-Jones@arm.com
85410027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
85510027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8565877Shsul@eecs.umich.edu
85710318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
85810318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8595877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8605877Shsul@eecs.umich.edu        return -EINVAL;
8615877Shsul@eecs.umich.edu    }
8625877Shsul@eecs.umich.edu
86310486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
86410486Stjablin@gmail.com
8655877Shsul@eecs.umich.edu    if (new_length > old_length) {
86610027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
86710027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8685877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8698601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8705877Shsul@eecs.umich.edu            process->mmap_end += diff;
8715877Shsul@eecs.umich.edu            return start;
8725877Shsul@eecs.umich.edu        } else {
87310027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8745877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8755877Shsul@eecs.umich.edu                return -ENOMEM;
8765877Shsul@eecs.umich.edu            } else {
87710027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
87810027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
87910027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
88010027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
88110027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
88210027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8835877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
88410027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
88510027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
88610027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
88710027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
88810027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
88910027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
89010027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
89110027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
89210027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
89310027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
89410027SChris.Adeniyi-Jones@arm.com                }
89510027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
89610027SChris.Adeniyi-Jones@arm.com                return new_start;
8975877Shsul@eecs.umich.edu            }
8985877Shsul@eecs.umich.edu        }
8995877Shsul@eecs.umich.edu    } else {
90010027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
90110027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
9028601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
90310027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
9045877Shsul@eecs.umich.edu    }
9055877Shsul@eecs.umich.edu}
9061999SN/A
907378SN/A/// Target stat() handler.
908360SN/Atemplate <class OS>
9091450SN/ASyscallReturn
9103114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9112680Sktlim@umich.edu         ThreadContext *tc)
912360SN/A{
913360SN/A    std::string path;
914360SN/A
9156701Sgblack@eecs.umich.edu    int index = 0;
9168852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9176701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9186701Sgblack@eecs.umich.edu        return -EFAULT;
9196701Sgblack@eecs.umich.edu    }
9206701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
921360SN/A
9223669Sbinkertn@umich.edu    // Adjust path for current working directory
9233669Sbinkertn@umich.edu    path = process->fullPath(path);
9243669Sbinkertn@umich.edu
925360SN/A    struct stat hostBuf;
926360SN/A    int result = stat(path.c_str(), &hostBuf);
927360SN/A
928360SN/A    if (result < 0)
9292218SN/A        return -errno;
930360SN/A
9318706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
932360SN/A
9331458SN/A    return 0;
934360SN/A}
935360SN/A
936360SN/A
9375074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9385074Ssaidi@eecs.umich.edutemplate <class OS>
9395074Ssaidi@eecs.umich.eduSyscallReturn
9405074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9415074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9425074Ssaidi@eecs.umich.edu{
9435074Ssaidi@eecs.umich.edu    std::string path;
9445074Ssaidi@eecs.umich.edu
9456701Sgblack@eecs.umich.edu    int index = 0;
9468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9476701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9485074Ssaidi@eecs.umich.edu        return -EFAULT;
9496701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9505074Ssaidi@eecs.umich.edu
9515074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9525074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9535074Ssaidi@eecs.umich.edu
9545208Ssaidi@eecs.umich.edu#if NO_STAT64
9555208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9565208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9575208Ssaidi@eecs.umich.edu#else
9585074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9595074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9605208Ssaidi@eecs.umich.edu#endif
9615074Ssaidi@eecs.umich.edu
9625074Ssaidi@eecs.umich.edu    if (result < 0)
9635074Ssaidi@eecs.umich.edu        return -errno;
9645074Ssaidi@eecs.umich.edu
9658706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9665074Ssaidi@eecs.umich.edu
9675074Ssaidi@eecs.umich.edu    return 0;
9685074Ssaidi@eecs.umich.edu}
9695074Ssaidi@eecs.umich.edu
9705074Ssaidi@eecs.umich.edu
97110027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
97210027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
97310027SChris.Adeniyi-Jones@arm.comSyscallReturn
97410027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
97510027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
97610027SChris.Adeniyi-Jones@arm.com{
97710027SChris.Adeniyi-Jones@arm.com    int index = 0;
97810027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
97910027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
98010793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
98110027SChris.Adeniyi-Jones@arm.com
98210027SChris.Adeniyi-Jones@arm.com    std::string path;
98310027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
98410027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
98510027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
98610027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
98710027SChris.Adeniyi-Jones@arm.com
98810027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
98910027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
99010027SChris.Adeniyi-Jones@arm.com
99110027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
99210027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
99310027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
99410027SChris.Adeniyi-Jones@arm.com#else
99510027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
99610027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
99710027SChris.Adeniyi-Jones@arm.com#endif
99810027SChris.Adeniyi-Jones@arm.com
99910027SChris.Adeniyi-Jones@arm.com    if (result < 0)
100010027SChris.Adeniyi-Jones@arm.com        return -errno;
100110027SChris.Adeniyi-Jones@arm.com
100210027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
100310027SChris.Adeniyi-Jones@arm.com
100410027SChris.Adeniyi-Jones@arm.com    return 0;
100510027SChris.Adeniyi-Jones@arm.com}
100610027SChris.Adeniyi-Jones@arm.com
100710027SChris.Adeniyi-Jones@arm.com
10081999SN/A/// Target fstat64() handler.
10091999SN/Atemplate <class OS>
10101999SN/ASyscallReturn
10113114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10122680Sktlim@umich.edu            ThreadContext *tc)
10131999SN/A{
10146701Sgblack@eecs.umich.edu    int index = 0;
101510931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
10166701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
101710931Sbrandon.potter@amd.com
101810932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
101910931Sbrandon.potter@amd.com    if (sim_fd < 0)
10201999SN/A        return -EBADF;
10211999SN/A
10222764Sstever@eecs.umich.edu#if NO_STAT64
10232064SN/A    struct stat  hostBuf;
102410931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10252064SN/A#else
10262064SN/A    struct stat64  hostBuf;
102710931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10282064SN/A#endif
10291999SN/A
10301999SN/A    if (result < 0)
10312218SN/A        return -errno;
10321999SN/A
103310931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10341999SN/A
10351999SN/A    return 0;
10361999SN/A}
10371999SN/A
10381999SN/A
1039378SN/A/// Target lstat() handler.
1040360SN/Atemplate <class OS>
10411450SN/ASyscallReturn
10423114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10432680Sktlim@umich.edu          ThreadContext *tc)
1044360SN/A{
1045360SN/A    std::string path;
1046360SN/A
10476701Sgblack@eecs.umich.edu    int index = 0;
10488852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10496701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10506701Sgblack@eecs.umich.edu        return -EFAULT;
10516701Sgblack@eecs.umich.edu    }
10526701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1053360SN/A
10543669Sbinkertn@umich.edu    // Adjust path for current working directory
10553669Sbinkertn@umich.edu    path = process->fullPath(path);
10563669Sbinkertn@umich.edu
1057360SN/A    struct stat hostBuf;
1058360SN/A    int result = lstat(path.c_str(), &hostBuf);
1059360SN/A
1060360SN/A    if (result < 0)
10611458SN/A        return -errno;
1062360SN/A
10638706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1064360SN/A
10651458SN/A    return 0;
1066360SN/A}
1067360SN/A
10681999SN/A/// Target lstat64() handler.
10691999SN/Atemplate <class OS>
10701999SN/ASyscallReturn
10713114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10722680Sktlim@umich.edu            ThreadContext *tc)
10731999SN/A{
10741999SN/A    std::string path;
10751999SN/A
10766701Sgblack@eecs.umich.edu    int index = 0;
10778852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10786701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10796701Sgblack@eecs.umich.edu        return -EFAULT;
10806701Sgblack@eecs.umich.edu    }
10816701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10821999SN/A
10833669Sbinkertn@umich.edu    // Adjust path for current working directory
10843669Sbinkertn@umich.edu    path = process->fullPath(path);
10853669Sbinkertn@umich.edu
10862764Sstever@eecs.umich.edu#if NO_STAT64
10872064SN/A    struct stat hostBuf;
10882064SN/A    int result = lstat(path.c_str(), &hostBuf);
10892064SN/A#else
10901999SN/A    struct stat64 hostBuf;
10911999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10922064SN/A#endif
10931999SN/A
10941999SN/A    if (result < 0)
10951999SN/A        return -errno;
10961999SN/A
10978706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10981999SN/A
10991999SN/A    return 0;
11001999SN/A}
11011999SN/A
1102378SN/A/// Target fstat() handler.
1103360SN/Atemplate <class OS>
11041450SN/ASyscallReturn
11053114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11062680Sktlim@umich.edu          ThreadContext *tc)
1107360SN/A{
11086701Sgblack@eecs.umich.edu    int index = 0;
110910931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11106701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1111360SN/A
111211380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1113360SN/A
111410932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
111510931Sbrandon.potter@amd.com    if (sim_fd < 0)
11161458SN/A        return -EBADF;
1117360SN/A
1118360SN/A    struct stat hostBuf;
111910931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1120360SN/A
1121360SN/A    if (result < 0)
11221458SN/A        return -errno;
1123360SN/A
112410931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11252021SN/A
11261458SN/A    return 0;
1127360SN/A}
1128360SN/A
1129360SN/A
11301706SN/A/// Target statfs() handler.
11311706SN/Atemplate <class OS>
11321706SN/ASyscallReturn
11333114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11342680Sktlim@umich.edu           ThreadContext *tc)
11351706SN/A{
11361706SN/A    std::string path;
11371706SN/A
11386701Sgblack@eecs.umich.edu    int index = 0;
11398852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11406701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11416701Sgblack@eecs.umich.edu        return -EFAULT;
11426701Sgblack@eecs.umich.edu    }
11436701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11441706SN/A
11453669Sbinkertn@umich.edu    // Adjust path for current working directory
11463669Sbinkertn@umich.edu    path = process->fullPath(path);
11473669Sbinkertn@umich.edu
11481706SN/A    struct statfs hostBuf;
11491706SN/A    int result = statfs(path.c_str(), &hostBuf);
11501706SN/A
11511706SN/A    if (result < 0)
11522218SN/A        return -errno;
11531706SN/A
11548706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11551706SN/A
11561706SN/A    return 0;
11571706SN/A}
11581706SN/A
11591706SN/A
11601706SN/A/// Target fstatfs() handler.
11611706SN/Atemplate <class OS>
11621706SN/ASyscallReturn
11633114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11642680Sktlim@umich.edu            ThreadContext *tc)
11651706SN/A{
11666701Sgblack@eecs.umich.edu    int index = 0;
116710931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11686701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11691706SN/A
117010932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
117110931Sbrandon.potter@amd.com    if (sim_fd < 0)
11721706SN/A        return -EBADF;
11731706SN/A
11741706SN/A    struct statfs hostBuf;
117510931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11761706SN/A
11771706SN/A    if (result < 0)
11782218SN/A        return -errno;
11791706SN/A
11808706Sandreas.hansson@arm.com    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11811706SN/A
11821706SN/A    return 0;
11831706SN/A}
11841706SN/A
11851706SN/A
11861999SN/A/// Target writev() handler.
11871999SN/Atemplate <class OS>
11881999SN/ASyscallReturn
11893114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11902680Sktlim@umich.edu           ThreadContext *tc)
11911999SN/A{
11926701Sgblack@eecs.umich.edu    int index = 0;
119310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
119410931Sbrandon.potter@amd.com
119510932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
119610931Sbrandon.potter@amd.com    if (sim_fd < 0)
11971999SN/A        return -EBADF;
11981999SN/A
11998852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
12006701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
12016701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
12021999SN/A    struct iovec hiov[count];
12036227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
12041999SN/A        typename OS::tgt_iovec tiov;
12052461SN/A
12068852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12078852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12088737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
12091999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
12108852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12118852Sandreas.hansson@arm.com                   hiov[i].iov_len);
12121999SN/A    }
12131999SN/A
121410931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
12151999SN/A
12166227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
12171999SN/A        delete [] (char *)hiov[i].iov_base;
12181999SN/A
12191999SN/A    if (result < 0)
12202218SN/A        return -errno;
12211999SN/A
122210629Sjthestness@gmail.com    return result;
12231999SN/A}
12241999SN/A
12251999SN/A
1226378SN/A/// Target mmap() handler.
1227378SN/A///
1228378SN/A/// We don't really handle mmap().  If the target is mmaping an
1229378SN/A/// anonymous region or /dev/zero, we can get away with doing basically
1230378SN/A/// nothing (since memory is initialized to zero and the simulator
12318324Ssteve.reinhardt@amd.com/// doesn't really check addresses anyway).
12328324Ssteve.reinhardt@amd.com///
1233360SN/Atemplate <class OS>
12341450SN/ASyscallReturn
12353114Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
1236360SN/A{
12376701Sgblack@eecs.umich.edu    int index = 0;
12386701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12396701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
12406701Sgblack@eecs.umich.edu    index++; // int prot = p->getSyscallArg(tc, index);
12416701Sgblack@eecs.umich.edu    int flags = p->getSyscallArg(tc, index);
12428324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
124310486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1244360SN/A
12459008Sgblack@eecs.umich.edu    if (length > 0x100000000ULL)
12469008Sgblack@eecs.umich.edu        warn("mmap length argument %#x is unreasonably large.\n", length);
12479008Sgblack@eecs.umich.edu
12488324Ssteve.reinhardt@amd.com    if (!(flags & OS::TGT_MAP_ANONYMOUS)) {
124910932Sbrandon.potter@amd.com        FDEntry *fde = p->getFDEntry(tgt_fd);
125010930Sbrandon.potter@amd.com        if (!fde || fde->fd < 0) {
12518324Ssteve.reinhardt@amd.com            warn("mmap failing: target fd %d is not valid\n", tgt_fd);
12528324Ssteve.reinhardt@amd.com            return -EBADF;
12538324Ssteve.reinhardt@amd.com        }
12548324Ssteve.reinhardt@amd.com
125510930Sbrandon.potter@amd.com        if (fde->filename != "/dev/zero") {
12568324Ssteve.reinhardt@amd.com            // This is very likely broken, but leave a warning here
12578324Ssteve.reinhardt@amd.com            // (rather than panic) in case /dev/zero is known by
12588324Ssteve.reinhardt@amd.com            // another name on some platform
12598324Ssteve.reinhardt@amd.com            warn("allowing mmap of file %s; mmap not supported on files"
126010930Sbrandon.potter@amd.com                 " other than /dev/zero\n", fde->filename);
12618324Ssteve.reinhardt@amd.com        }
12628324Ssteve.reinhardt@amd.com    }
12635877Shsul@eecs.umich.edu
126410486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
126510486Stjablin@gmail.com
126610318Sandreas.hansson@arm.com    if ((start  % TheISA::PageBytes) != 0 ||
126710486Stjablin@gmail.com        (offset % TheISA::PageBytes) != 0) {
12682544SN/A        warn("mmap failing: arguments not page-aligned: "
126910486Stjablin@gmail.com             "start 0x%x offset 0x%x",
127010486Stjablin@gmail.com             start, offset);
12712544SN/A        return -EINVAL;
1272360SN/A    }
1273360SN/A
12748600Ssteve.reinhardt@amd.com    // are we ok with clobbering existing mappings?  only set this to
12758600Ssteve.reinhardt@amd.com    // true if the user has been warned.
12768600Ssteve.reinhardt@amd.com    bool clobber = false;
12778600Ssteve.reinhardt@amd.com
12788600Ssteve.reinhardt@amd.com    // try to use the caller-provided address if there is one
12798600Ssteve.reinhardt@amd.com    bool use_provided_address = (start != 0);
12808600Ssteve.reinhardt@amd.com
12818600Ssteve.reinhardt@amd.com    if (use_provided_address) {
12828600Ssteve.reinhardt@amd.com        // check to see if the desired address is already in use
12838600Ssteve.reinhardt@amd.com        if (!p->pTable->isUnmapped(start, length)) {
12848600Ssteve.reinhardt@amd.com            // there are existing mappings in the desired range
12858600Ssteve.reinhardt@amd.com            // whether we clobber them or not depends on whether the caller
12868600Ssteve.reinhardt@amd.com            // specified MAP_FIXED
12878600Ssteve.reinhardt@amd.com            if (flags & OS::TGT_MAP_FIXED) {
128810485SMichael.Adler@intel.com                // MAP_FIXED specified: map attempt fails
128910485SMichael.Adler@intel.com                return -EINVAL;
12908600Ssteve.reinhardt@amd.com            } else {
12918600Ssteve.reinhardt@amd.com                // MAP_FIXED not specified: ignore suggested start address
12928600Ssteve.reinhardt@amd.com                warn("mmap: ignoring suggested map address 0x%x\n", start);
12938600Ssteve.reinhardt@amd.com                use_provided_address = false;
12948600Ssteve.reinhardt@amd.com            }
12958600Ssteve.reinhardt@amd.com        }
12962544SN/A    }
12972544SN/A
12988600Ssteve.reinhardt@amd.com    if (!use_provided_address) {
12998600Ssteve.reinhardt@amd.com        // no address provided, or provided address unusable:
13008600Ssteve.reinhardt@amd.com        // pick next address from our "mmap region"
13018600Ssteve.reinhardt@amd.com        if (OS::mmapGrowsDown()) {
13028600Ssteve.reinhardt@amd.com            start = p->mmap_end - length;
13038600Ssteve.reinhardt@amd.com            p->mmap_end = start;
13048600Ssteve.reinhardt@amd.com        } else {
13058600Ssteve.reinhardt@amd.com            start = p->mmap_end;
13068600Ssteve.reinhardt@amd.com            p->mmap_end += length;
13078600Ssteve.reinhardt@amd.com        }
13086672Sgblack@eecs.umich.edu    }
13098600Ssteve.reinhardt@amd.com
13108601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
13112544SN/A
13121458SN/A    return start;
1313360SN/A}
1314360SN/A
1315378SN/A/// Target getrlimit() handler.
1316360SN/Atemplate <class OS>
13171450SN/ASyscallReturn
13183114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13192680Sktlim@umich.edu        ThreadContext *tc)
1320360SN/A{
13216701Sgblack@eecs.umich.edu    int index = 0;
13226701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
13236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1324360SN/A
1325360SN/A    switch (resource) {
13262064SN/A        case OS::TGT_RLIMIT_STACK:
13275877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
13282064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
13298737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13308737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13312064SN/A            break;
1332360SN/A
13335877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
13345877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
13355877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
13368737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
13378737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
13385877Shsul@eecs.umich.edu            break;
13395877Shsul@eecs.umich.edu
13402064SN/A        default:
134110794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
134210794Sbrandon.potter@amd.com            return -EINVAL;
13432064SN/A            break;
1344360SN/A    }
1345360SN/A
13468706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
13471458SN/A    return 0;
1348360SN/A}
1349360SN/A
135010796Sbrandon.potter@amd.com/// Target clock_gettime() function.
135110796Sbrandon.potter@amd.comtemplate <class OS>
135210796Sbrandon.potter@amd.comSyscallReturn
135310796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
135410796Sbrandon.potter@amd.com{
135510796Sbrandon.potter@amd.com    int index = 1;
135610796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
135710796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
135810796Sbrandon.potter@amd.com
135910796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
136010796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
136110796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
136210796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
136310796Sbrandon.potter@amd.com
136410796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
136510796Sbrandon.potter@amd.com
136610796Sbrandon.potter@amd.com    return 0;
136710796Sbrandon.potter@amd.com}
136810796Sbrandon.potter@amd.com
136911337SMichael.Lebeane@amd.com/// Target clock_getres() function.
137011337SMichael.Lebeane@amd.comtemplate <class OS>
137111337SMichael.Lebeane@amd.comSyscallReturn
137211337SMichael.Lebeane@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
137311337SMichael.Lebeane@amd.com{
137411337SMichael.Lebeane@amd.com    int index = 1;
137511337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
137611337SMichael.Lebeane@amd.com
137711337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
137811337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
137911337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
138011337SMichael.Lebeane@amd.com
138111337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
138211337SMichael.Lebeane@amd.com
138311337SMichael.Lebeane@amd.com    return 0;
138411337SMichael.Lebeane@amd.com}
138511337SMichael.Lebeane@amd.com
1386378SN/A/// Target gettimeofday() handler.
1387360SN/Atemplate <class OS>
13881450SN/ASyscallReturn
13893114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
13902680Sktlim@umich.edu        ThreadContext *tc)
1391360SN/A{
13926701Sgblack@eecs.umich.edu    int index = 0;
13936701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1394360SN/A
139510796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1396360SN/A    tp->tv_sec += seconds_since_epoch;
13976109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
13986109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1399360SN/A
14008706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1401360SN/A
14021458SN/A    return 0;
1403360SN/A}
1404360SN/A
1405360SN/A
14061999SN/A/// Target utimes() handler.
14071999SN/Atemplate <class OS>
14081999SN/ASyscallReturn
14093114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14102680Sktlim@umich.edu           ThreadContext *tc)
14111999SN/A{
14121999SN/A    std::string path;
14131999SN/A
14146701Sgblack@eecs.umich.edu    int index = 0;
14158852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14166701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14176701Sgblack@eecs.umich.edu        return -EFAULT;
14186701Sgblack@eecs.umich.edu    }
14191999SN/A
14206701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
14216701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
14228706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
14231999SN/A
14241999SN/A    struct timeval hostTimeval[2];
14251999SN/A    for (int i = 0; i < 2; ++i)
14261999SN/A    {
14278737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
14288737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
14291999SN/A    }
14303669Sbinkertn@umich.edu
14313669Sbinkertn@umich.edu    // Adjust path for current working directory
14323669Sbinkertn@umich.edu    path = process->fullPath(path);
14333669Sbinkertn@umich.edu
14341999SN/A    int result = utimes(path.c_str(), hostTimeval);
14351999SN/A
14361999SN/A    if (result < 0)
14371999SN/A        return -errno;
14381999SN/A
14391999SN/A    return 0;
14401999SN/A}
1441378SN/A/// Target getrusage() function.
1442360SN/Atemplate <class OS>
14431450SN/ASyscallReturn
14443114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14452680Sktlim@umich.edu              ThreadContext *tc)
1446360SN/A{
14476701Sgblack@eecs.umich.edu    int index = 0;
14486701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
14496701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1450360SN/A
14513670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
14523670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1453360SN/A    rup->ru_stime.tv_sec = 0;
1454360SN/A    rup->ru_stime.tv_usec = 0;
1455360SN/A    rup->ru_maxrss = 0;
1456360SN/A    rup->ru_ixrss = 0;
1457360SN/A    rup->ru_idrss = 0;
1458360SN/A    rup->ru_isrss = 0;
1459360SN/A    rup->ru_minflt = 0;
1460360SN/A    rup->ru_majflt = 0;
1461360SN/A    rup->ru_nswap = 0;
1462360SN/A    rup->ru_inblock = 0;
1463360SN/A    rup->ru_oublock = 0;
1464360SN/A    rup->ru_msgsnd = 0;
1465360SN/A    rup->ru_msgrcv = 0;
1466360SN/A    rup->ru_nsignals = 0;
1467360SN/A    rup->ru_nvcsw = 0;
1468360SN/A    rup->ru_nivcsw = 0;
1469360SN/A
14703670Sbinkertn@umich.edu    switch (who) {
14713670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
147210796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
14738737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
14748737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
14753670Sbinkertn@umich.edu        break;
14763670Sbinkertn@umich.edu
14773670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
14783670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
14793670Sbinkertn@umich.edu        break;
14803670Sbinkertn@umich.edu
14813670Sbinkertn@umich.edu      default:
14823670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
14833670Sbinkertn@umich.edu        // plow ahead
14843670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
14853670Sbinkertn@umich.edu             who);
14863670Sbinkertn@umich.edu    }
14873670Sbinkertn@umich.edu
14888706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1489360SN/A
14901458SN/A    return 0;
1491360SN/A}
1492360SN/A
14936683Stjones1@inf.ed.ac.uk/// Target times() function.
14946683Stjones1@inf.ed.ac.uktemplate <class OS>
14956683Stjones1@inf.ed.ac.ukSyscallReturn
14966683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14976683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
14986683Stjones1@inf.ed.ac.uk{
14996701Sgblack@eecs.umich.edu    int index = 0;
15006701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
15016683Stjones1@inf.ed.ac.uk
15026683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
15037823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
15046683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
15056683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
15066683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
15076683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
15086683Stjones1@inf.ed.ac.uk
15096683Stjones1@inf.ed.ac.uk    // Convert to host endianness
15108737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
15116683Stjones1@inf.ed.ac.uk
15126683Stjones1@inf.ed.ac.uk    // Write back
15138706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
15146683Stjones1@inf.ed.ac.uk
15156683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
15166683Stjones1@inf.ed.ac.uk    return clocks;
15176683Stjones1@inf.ed.ac.uk}
15182553SN/A
15196684Stjones1@inf.ed.ac.uk/// Target time() function.
15206684Stjones1@inf.ed.ac.uktemplate <class OS>
15216684Stjones1@inf.ed.ac.ukSyscallReturn
15226684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15236684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
15246684Stjones1@inf.ed.ac.uk{
15256684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
152610796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
15276684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
15286684Stjones1@inf.ed.ac.uk
15296701Sgblack@eecs.umich.edu    int index = 0;
15306701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
153111321Ssteve.reinhardt@amd.com    if (taddr != 0) {
15326684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
15338737Skoansin.tan@gmail.com        t = TheISA::htog(t);
15348852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
15358852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
15366684Stjones1@inf.ed.ac.uk    }
15376684Stjones1@inf.ed.ac.uk    return sec;
15386684Stjones1@inf.ed.ac.uk}
15392553SN/A
15402553SN/A
15411354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1542