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