syscall_emul.hh revision 11799
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__
655543Ssaidi@eecs.umich.edu#include <sys/fcntl.h>  // for O_BINARY
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
812474SN/A#include "base/chunk_generator.hh"
825543Ssaidi@eecs.umich.edu#include "base/intmath.hh"      // for RoundUp
8311392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
842462SN/A#include "base/misc.hh"
851354SN/A#include "base/trace.hh"
866216Snate@binkert.org#include "base/types.hh"
876658Snate@binkert.org#include "config/the_isa.hh"
882474SN/A#include "cpu/base.hh"
892680Sktlim@umich.edu#include "cpu/thread_context.hh"
908229Snate@binkert.org#include "mem/page_table.hh"
917678Sgblack@eecs.umich.edu#include "sim/byteswap.hh"
9210496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
938229Snate@binkert.org#include "sim/process.hh"
9411794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
9510497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
9611794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
976640Svince@csl.cornell.edu#include "sim/system.hh"
98360SN/A
9911794Sbrandon.potter@amd.comclass SyscallDesc;
100360SN/A
101360SN/A//////////////////////////////////////////////////////////////////////
102360SN/A//
103360SN/A// The following emulation functions are generic enough that they
104360SN/A// don't need to be recompiled for different emulated OS's.  They are
105360SN/A// defined in sim/syscall_emul.cc.
106360SN/A//
107360SN/A//////////////////////////////////////////////////////////////////////
108360SN/A
109360SN/A
110378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1111706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1123114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
113378SN/A
114378SN/A/// Handler for unimplemented syscalls that we never intend to
115378SN/A/// implement (signal handling, etc.) and should not affect the correct
116378SN/A/// behavior of the program.  Print a warning only if the appropriate
117378SN/A/// trace flag is enabled.  Return success to the target program.
1181706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1193114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
120360SN/A
12111760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
12211760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
12311760Sbrandon.potter@amd.com                            LiveProcess *p, ThreadContext *tc);
12411760Sbrandon.potter@amd.com
1256109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1261706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1273114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
128378SN/A
1296109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1306109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1316109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1326109Ssanchezd@stanford.edu
133378SN/A/// Target getpagesize() handler.
1341706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1353114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
136378SN/A
1375748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1385748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1395748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
140378SN/A
141378SN/A/// Target close() handler.
1421706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1433114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
144378SN/A
145378SN/A/// Target read() handler.
1461706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1473114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
148378SN/A
149378SN/A/// Target write() handler.
1501706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1513114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
152378SN/A
153378SN/A/// Target lseek() handler.
1541706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1553114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
156378SN/A
1574118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1584118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
1594118Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
1604118Sgblack@eecs.umich.edu
161378SN/A/// Target munmap() handler.
1621706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1633114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
164378SN/A
165378SN/A/// Target gethostname() handler.
1661706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1673114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
168360SN/A
1695513SMichael.Adler@intel.com/// Target getcwd() handler.
1705513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
1715513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
1725513SMichael.Adler@intel.com
17310203SAli.Saidi@ARM.com/// Target readlink() handler.
17410203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
17510203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
17610203SAli.Saidi@ARM.com                           int index = 0);
1775513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
1785513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
1795513SMichael.Adler@intel.com
180511SN/A/// Target unlink() handler.
18110633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
18210633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
18310633Smichaelupton@gmail.com                           int index);
1841706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
1853114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
186511SN/A
1875513SMichael.Adler@intel.com/// Target mkdir() handler.
1885513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
1895513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
1905513SMichael.Adler@intel.com
191511SN/A/// Target rename() handler.
1921706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
1933114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1941706SN/A
1951706SN/A
1961706SN/A/// Target truncate() handler.
1971706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
1983114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
1991706SN/A
2001706SN/A
2011706SN/A/// Target ftruncate() handler.
2021706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2033114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2041706SN/A
205511SN/A
2066703Svince@csl.cornell.edu/// Target truncate64() handler.
2076703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2086703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2096703Svince@csl.cornell.edu
2106685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2116685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2126685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2136685Stjones1@inf.ed.ac.uk
2146685Stjones1@inf.ed.ac.uk
2155513SMichael.Adler@intel.com/// Target umask() handler.
2165513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2175513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2185513SMichael.Adler@intel.com
2195513SMichael.Adler@intel.com
2201999SN/A/// Target chown() handler.
2211999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2223114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2231999SN/A
2241999SN/A
2251999SN/A/// Target fchown() handler.
2261999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2273114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2281999SN/A
2293079Sstever@eecs.umich.edu/// Target dup() handler.
2303079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2313114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2323079Sstever@eecs.umich.edu
2332093SN/A/// Target fnctl() handler.
2342093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2353114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2362093SN/A
2372687Sksewell@umich.edu/// Target fcntl64() handler.
2382687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2393114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2402687Sksewell@umich.edu
2412238SN/A/// Target setuid() handler.
2422238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2433114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2442238SN/A
2452238SN/A/// Target getpid() handler.
2462238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
2473114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2482238SN/A
2492238SN/A/// Target getuid() handler.
2502238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2513114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2522238SN/A
2532238SN/A/// Target getgid() handler.
2542238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2553114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2562238SN/A
2572238SN/A/// Target getppid() handler.
2582238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2593114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2602238SN/A
2612238SN/A/// Target geteuid() handler.
2622238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2633114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2642238SN/A
2652238SN/A/// Target getegid() handler.
2662238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
2673114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
2682238SN/A
2696109Ssanchezd@stanford.edu/// Target clone() handler.
2706109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
2716109Ssanchezd@stanford.edu                               LiveProcess *p, ThreadContext *tc);
2722238SN/A
2739455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2749455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2759455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
27610203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
27710203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
27810203SAli.Saidi@ARM.com                               int index);
2799455Smitch.hayenga+gem5@gmail.com
2809112Smarc.orr@gmail.com/// Futex system call
2819112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
2829112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
2839112Smarc.orr@gmail.comtemplate <class OS>
2849112Smarc.orr@gmail.comSyscallReturn
2859112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
2869112Smarc.orr@gmail.com          ThreadContext *tc)
2879112Smarc.orr@gmail.com{
2889112Smarc.orr@gmail.com    int index_uaddr = 0;
2899112Smarc.orr@gmail.com    int index_op = 1;
2909112Smarc.orr@gmail.com    int index_val = 2;
2919112Smarc.orr@gmail.com    int index_timeout = 3;
2929112Smarc.orr@gmail.com
2939112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
2949112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
2959112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
2969112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
2979112Smarc.orr@gmail.com
2989112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
2999112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3009112Smarc.orr@gmail.com
3019112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3029112Smarc.orr@gmail.com            uaddr, op, val);
3039112Smarc.orr@gmail.com
3049238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3059112Smarc.orr@gmail.com
3069112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3079112Smarc.orr@gmail.com        if (timeout != 0) {
3089112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3099112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3109112Smarc.orr@gmail.com        }
3119112Smarc.orr@gmail.com
3129112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3139112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3149112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
31511367Sandreas.hansson@arm.com        delete[] buf;
3169112Smarc.orr@gmail.com
31711321Ssteve.reinhardt@amd.com        if (val != mem_val) {
3189112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3199112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3209112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3219112Smarc.orr@gmail.com        }
3229112Smarc.orr@gmail.com
3239112Smarc.orr@gmail.com        // Queue the thread context
3249112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3259112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3269112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3279112Smarc.orr@gmail.com        } else {
3289112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3299112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3309112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3319112Smarc.orr@gmail.com        }
3329112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3339112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3349112Smarc.orr@gmail.com                                "thread context\n");
3359112Smarc.orr@gmail.com        tc->suspend();
3369112Smarc.orr@gmail.com        return 0;
3379112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3389112Smarc.orr@gmail.com        int wokenUp = 0;
3399112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3409112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3419112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3429112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3439112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3449112Smarc.orr@gmail.com                tcWaitList->pop_front();
3459112Smarc.orr@gmail.com                wokenUp++;
3469112Smarc.orr@gmail.com            }
34711321Ssteve.reinhardt@amd.com            if (tcWaitList->empty()) {
3489112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3499112Smarc.orr@gmail.com                delete tcWaitList;
3509112Smarc.orr@gmail.com            }
3519112Smarc.orr@gmail.com        }
3529112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3539112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3549112Smarc.orr@gmail.com        return wokenUp;
3559112Smarc.orr@gmail.com    } else {
3569238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3579112Smarc.orr@gmail.com        return 0;
3589112Smarc.orr@gmail.com    }
3599112Smarc.orr@gmail.com
3609112Smarc.orr@gmail.com}
3619112Smarc.orr@gmail.com
3622238SN/A
3632238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3642238SN/A/// returning a second value in a register other than the normal return register
3652238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3663114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3672238SN/A
3682238SN/A/// Target getpidPseudo() handler.
3692238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3703114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3712238SN/A
3722238SN/A/// Target getuidPseudo() handler.
3732238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3743114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3752238SN/A
3762238SN/A/// Target getgidPseudo() handler.
3772238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3783114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3792238SN/A
3802238SN/A
3811354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3821354SN/Aconst int one_million = 1000000;
38310796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
38410796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
3851354SN/A
3861354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3871354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3881354SN/A/// real-world time) to keep simulations repeatable.
3891354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3901354SN/A
3911354SN/A/// Helper function to convert current elapsed time to seconds and
3921354SN/A/// microseconds.
3931354SN/Atemplate <class T1, class T2>
3941354SN/Avoid
39510796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
3961354SN/A{
39710796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
3981354SN/A    sec = elapsed_usecs / one_million;
3991354SN/A    usec = elapsed_usecs % one_million;
4001354SN/A}
4011354SN/A
40210796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
40310796Sbrandon.potter@amd.com/// nanoseconds.
40410796Sbrandon.potter@amd.comtemplate <class T1, class T2>
40510796Sbrandon.potter@amd.comvoid
40610796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
40710796Sbrandon.potter@amd.com{
40810796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
40910796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
41010796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
41110796Sbrandon.potter@amd.com}
41210796Sbrandon.potter@amd.com
413360SN/A//////////////////////////////////////////////////////////////////////
414360SN/A//
415360SN/A// The following emulation functions are generic, but need to be
416360SN/A// templated to account for differences in types, constants, etc.
417360SN/A//
418360SN/A//////////////////////////////////////////////////////////////////////
419360SN/A
42011759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4213113Sgblack@eecs.umich.edu#if NO_STAT64
4223113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4233113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4243113Sgblack@eecs.umich.edu#else
4253113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4263113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4273113Sgblack@eecs.umich.edu#endif
4283113Sgblack@eecs.umich.edu
4293113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4303113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4313113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4323113Sgblack@eecs.umich.edu
4333113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4343113Sgblack@eecs.umich.edustatic void
4353113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4363113Sgblack@eecs.umich.edu{
4374189Sgblack@eecs.umich.edu    using namespace TheISA;
4384189Sgblack@eecs.umich.edu
4393113Sgblack@eecs.umich.edu    if (fakeTTY)
4403113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4413113Sgblack@eecs.umich.edu    else
4423113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4438737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4443113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4458737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4463277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4475515SMichael.Adler@intel.com    if (fakeTTY) {
4485515SMichael.Adler@intel.com        // Claim to be a character device
4495515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4505515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4515515SMichael.Adler@intel.com    }
4528737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4533277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4548737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4553277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4568737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4573277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4588737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4593113Sgblack@eecs.umich.edu    if (fakeTTY)
4603113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4613113Sgblack@eecs.umich.edu    else
4623113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4638737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4643113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4658737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4663114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4678737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4683114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4698737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4703114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4718737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4724061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4734061Sgblack@eecs.umich.edu    // consistently across different hosts.
4744061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4758737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4763113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4778737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4783113Sgblack@eecs.umich.edu}
4793113Sgblack@eecs.umich.edu
4803113Sgblack@eecs.umich.edu// Same for stat64
4813113Sgblack@eecs.umich.edu
4823113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4833113Sgblack@eecs.umich.edustatic void
4843113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4853113Sgblack@eecs.umich.edu{
4864189Sgblack@eecs.umich.edu    using namespace TheISA;
4874189Sgblack@eecs.umich.edu
4883113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4893113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4903113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4918737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
4923113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4938737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
4943113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4958737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
4963113Sgblack@eecs.umich.edu#else
4973113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4983113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4993113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5003113Sgblack@eecs.umich.edu#endif
5013113Sgblack@eecs.umich.edu}
5023113Sgblack@eecs.umich.edu
5033113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5043113Sgblack@eecs.umich.edutemplate<class OS>
5053113Sgblack@eecs.umich.edustatic void
5068852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5073113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5083113Sgblack@eecs.umich.edu{
5093113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5103113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5113113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5123113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5133113Sgblack@eecs.umich.edu}
5143113Sgblack@eecs.umich.edu
5153113Sgblack@eecs.umich.edutemplate<class OS>
5163113Sgblack@eecs.umich.edustatic void
5178852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5183113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5193113Sgblack@eecs.umich.edu{
5203113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5213113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5226686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5233113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5243113Sgblack@eecs.umich.edu}
5253113Sgblack@eecs.umich.edu
52611759Sbrandon.potter@amd.comtemplate <class OS>
52711759Sbrandon.potter@amd.comstatic void
52811759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
52911759Sbrandon.potter@amd.com                 hst_statfs *host)
53011759Sbrandon.potter@amd.com{
53111759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
53211759Sbrandon.potter@amd.com
53311759Sbrandon.potter@amd.com#if defined(__OpenBSD__) || defined(__APPLE__) || defined(__FreeBSD__)
53411759Sbrandon.potter@amd.com    tgt->f_type = 0;
53511759Sbrandon.potter@amd.com#else
53611759Sbrandon.potter@amd.com    tgt->f_type = TheISA::htog(host->f_type);
53711759Sbrandon.potter@amd.com#endif
53811759Sbrandon.potter@amd.com    tgt->f_bsize = TheISA::htog(host->f_bsize);
53911759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
54011759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
54111759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
54211759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
54311759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
54411759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
54511759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
54611759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
54711759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
54811759Sbrandon.potter@amd.com
54911759Sbrandon.potter@amd.com    tgt.copyOut(mem);
55011759Sbrandon.potter@amd.com}
55111759Sbrandon.potter@amd.com
552378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
553378SN/A/// only to find out if their stdout is a tty, to determine whether to
5549141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5559141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
556360SN/Atemplate <class OS>
5571450SN/ASyscallReturn
5583114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5592680Sktlim@umich.edu          ThreadContext *tc)
560360SN/A{
5616701Sgblack@eecs.umich.edu    int index = 0;
56210930Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5636701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
564360SN/A
56510930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
566360SN/A
56710932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
56810496Ssteve.reinhardt@amd.com
56910930Sbrandon.potter@amd.com    if (fde == NULL) {
570360SN/A        // doesn't map to any simulator fd: not a valid target fd
5711458SN/A        return -EBADF;
572360SN/A    }
573360SN/A
57410930Sbrandon.potter@amd.com    if (fde->driver != NULL) {
57510930Sbrandon.potter@amd.com        return fde->driver->ioctl(process, tc, req);
57610496Ssteve.reinhardt@amd.com    }
57710496Ssteve.reinhardt@amd.com
5789141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5791458SN/A        return -ENOTTY;
5809141Smarc.orr@gmail.com    }
581360SN/A
5829141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
58310930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
5849141Smarc.orr@gmail.com    return -ENOTTY;
585360SN/A}
586360SN/A
587360SN/Atemplate <class OS>
58810027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
5893114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
59010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
591360SN/A{
592360SN/A    std::string path;
593360SN/A
5948852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
5956701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
5961458SN/A        return -EFAULT;
597360SN/A
5986701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
5996701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
600360SN/A    int hostFlags = 0;
601360SN/A
602360SN/A    // translate open flags
603360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
604360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
605360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
606360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
607360SN/A        }
608360SN/A    }
609360SN/A
610360SN/A    // any target flags left?
611360SN/A    if (tgtFlags != 0)
6121706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
613360SN/A
614360SN/A#ifdef __CYGWIN32__
615360SN/A    hostFlags |= O_BINARY;
616360SN/A#endif
617360SN/A
6183669Sbinkertn@umich.edu    // Adjust path for current working directory
6193669Sbinkertn@umich.edu    path = process->fullPath(path);
6203669Sbinkertn@umich.edu
6211706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6221706SN/A
62310496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
62410496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
62510496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
62610496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
62710496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
62810496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
62910496Ssteve.reinhardt@amd.com            return -ENOENT;
63010496Ssteve.reinhardt@amd.com        }
63110496Ssteve.reinhardt@amd.com
63210496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
63310496Ssteve.reinhardt@amd.com        if (drv != NULL) {
63410496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
63510496Ssteve.reinhardt@amd.com            // process if necessary.
63610496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
63710496Ssteve.reinhardt@amd.com        }
63810496Ssteve.reinhardt@amd.com
63910496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
64010496Ssteve.reinhardt@amd.com        // /dev/zero
64110496Ssteve.reinhardt@amd.com    }
64210496Ssteve.reinhardt@amd.com
6435795Ssaidi@eecs.umich.edu    int fd;
6449143Ssteve.reinhardt@amd.com    int local_errno;
6459142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6469142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6479143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6485795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6499143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6505795Ssaidi@eecs.umich.edu     } else {
6515795Ssaidi@eecs.umich.edu        // open the file
6525795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6539143Ssteve.reinhardt@amd.com        local_errno = errno;
6545795Ssaidi@eecs.umich.edu     }
655360SN/A
6569143Ssteve.reinhardt@amd.com    if (fd == -1)
6579143Ssteve.reinhardt@amd.com        return -local_errno;
6589143Ssteve.reinhardt@amd.com
65910932Sbrandon.potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
660360SN/A}
661360SN/A
66210027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
66310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
66410027SChris.Adeniyi-Jones@arm.comSyscallReturn
66510027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
66610027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
66710027SChris.Adeniyi-Jones@arm.com{
66810027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
66910027SChris.Adeniyi-Jones@arm.com}
67010027SChris.Adeniyi-Jones@arm.com
67110027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
67210027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67310027SChris.Adeniyi-Jones@arm.comSyscallReturn
67410027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67510027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
67610027SChris.Adeniyi-Jones@arm.com{
67710027SChris.Adeniyi-Jones@arm.com    int index = 0;
67810027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
67910027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
68010027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
68110027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
68210027SChris.Adeniyi-Jones@arm.com}
68310027SChris.Adeniyi-Jones@arm.com
68410633Smichaelupton@gmail.com/// Target unlinkat() handler.
68510633Smichaelupton@gmail.comtemplate <class OS>
68610633Smichaelupton@gmail.comSyscallReturn
68710633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68810633Smichaelupton@gmail.com             ThreadContext *tc)
68910633Smichaelupton@gmail.com{
69010633Smichaelupton@gmail.com    int index = 0;
69110633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
69210633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
69310633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
69410633Smichaelupton@gmail.com
69510633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
69610633Smichaelupton@gmail.com}
69710633Smichaelupton@gmail.com
69810203SAli.Saidi@ARM.com/// Target facessat() handler
69910203SAli.Saidi@ARM.comtemplate <class OS>
70010203SAli.Saidi@ARM.comSyscallReturn
70110203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70210203SAli.Saidi@ARM.com        ThreadContext *tc)
70310203SAli.Saidi@ARM.com{
70410203SAli.Saidi@ARM.com    int index = 0;
70510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
70610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
70710203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
70810203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
70910203SAli.Saidi@ARM.com}
71010203SAli.Saidi@ARM.com
71110203SAli.Saidi@ARM.com/// Target readlinkat() handler
71210203SAli.Saidi@ARM.comtemplate <class OS>
71310203SAli.Saidi@ARM.comSyscallReturn
71410203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71510203SAli.Saidi@ARM.com        ThreadContext *tc)
71610203SAli.Saidi@ARM.com{
71710203SAli.Saidi@ARM.com    int index = 0;
71810203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
71910203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
72010203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72110203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
72210203SAli.Saidi@ARM.com}
72310203SAli.Saidi@ARM.com
72410850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
72510850SGiacomo.Gabrielli@arm.comtemplate <class OS>
72610850SGiacomo.Gabrielli@arm.comSyscallReturn
72710850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
72810850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
72910850SGiacomo.Gabrielli@arm.com{
73010850SGiacomo.Gabrielli@arm.com    int index = 0;
73110850SGiacomo.Gabrielli@arm.com
73210850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
73310850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
73410850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
73510850SGiacomo.Gabrielli@arm.com
73610850SGiacomo.Gabrielli@arm.com    std::string old_name;
73710850SGiacomo.Gabrielli@arm.com
73810850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
73910850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
74010850SGiacomo.Gabrielli@arm.com        return -EFAULT;
74110850SGiacomo.Gabrielli@arm.com
74210850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
74310850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
74410850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
74510850SGiacomo.Gabrielli@arm.com
74610850SGiacomo.Gabrielli@arm.com    std::string new_name;
74710850SGiacomo.Gabrielli@arm.com
74810850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
74910850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
75010850SGiacomo.Gabrielli@arm.com        return -EFAULT;
75110850SGiacomo.Gabrielli@arm.com
75210850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
75310850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
75410850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
75510850SGiacomo.Gabrielli@arm.com
75610850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
75710850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
75810850SGiacomo.Gabrielli@arm.com}
75910850SGiacomo.Gabrielli@arm.com
7606640Svince@csl.cornell.edu/// Target sysinfo() handler.
7616640Svince@csl.cornell.edutemplate <class OS>
7626640Svince@csl.cornell.eduSyscallReturn
7636640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7646640Svince@csl.cornell.edu         ThreadContext *tc)
7656640Svince@csl.cornell.edu{
7666640Svince@csl.cornell.edu
7676701Sgblack@eecs.umich.edu    int index = 0;
7686701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
76910793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7706640Svince@csl.cornell.edu
77111758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
77211758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
77311758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
7746640Svince@csl.cornell.edu
7758706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7766640Svince@csl.cornell.edu
7776701Sgblack@eecs.umich.edu    return 0;
7786640Svince@csl.cornell.edu}
779360SN/A
7801999SN/A/// Target chmod() handler.
7811999SN/Atemplate <class OS>
7821999SN/ASyscallReturn
7833114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7842680Sktlim@umich.edu          ThreadContext *tc)
7851999SN/A{
7861999SN/A    std::string path;
7871999SN/A
7886701Sgblack@eecs.umich.edu    int index = 0;
7898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
7906701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
7911999SN/A        return -EFAULT;
7926701Sgblack@eecs.umich.edu    }
7931999SN/A
7946701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
7951999SN/A    mode_t hostMode = 0;
7961999SN/A
7971999SN/A    // XXX translate mode flags via OS::something???
7981999SN/A    hostMode = mode;
7991999SN/A
8003669Sbinkertn@umich.edu    // Adjust path for current working directory
8013669Sbinkertn@umich.edu    path = process->fullPath(path);
8023669Sbinkertn@umich.edu
8031999SN/A    // do the chmod
8041999SN/A    int result = chmod(path.c_str(), hostMode);
8051999SN/A    if (result < 0)
8062218SN/A        return -errno;
8071999SN/A
8081999SN/A    return 0;
8091999SN/A}
8101999SN/A
8111999SN/A
8121999SN/A/// Target fchmod() handler.
8131999SN/Atemplate <class OS>
8141999SN/ASyscallReturn
8153114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8162680Sktlim@umich.edu           ThreadContext *tc)
8171999SN/A{
8186701Sgblack@eecs.umich.edu    int index = 0;
81910931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
82010931Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
82110931Sbrandon.potter@amd.com
82210932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
82310931Sbrandon.potter@amd.com    if (sim_fd < 0)
8241999SN/A        return -EBADF;
8251999SN/A
8261999SN/A    mode_t hostMode = 0;
8271999SN/A
8281999SN/A    // XXX translate mode flags via OS::someting???
8291999SN/A    hostMode = mode;
8301999SN/A
8311999SN/A    // do the fchmod
83210931Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
8331999SN/A    if (result < 0)
8342218SN/A        return -errno;
8351999SN/A
8361999SN/A    return 0;
8371999SN/A}
8381999SN/A
8395877Shsul@eecs.umich.edu/// Target mremap() handler.
8405877Shsul@eecs.umich.edutemplate <class OS>
8415877Shsul@eecs.umich.eduSyscallReturn
8425877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8435877Shsul@eecs.umich.edu{
8446701Sgblack@eecs.umich.edu    int index = 0;
8456701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8466701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8476701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8486701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
84910027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
85010027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
85110027SChris.Adeniyi-Jones@arm.com
85210027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
85310027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8545877Shsul@eecs.umich.edu
85510318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
85610318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8575877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8585877Shsul@eecs.umich.edu        return -EINVAL;
8595877Shsul@eecs.umich.edu    }
8605877Shsul@eecs.umich.edu
86110486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
86210486Stjablin@gmail.com
8635877Shsul@eecs.umich.edu    if (new_length > old_length) {
86410027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
86510027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8665877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8678601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8685877Shsul@eecs.umich.edu            process->mmap_end += diff;
8695877Shsul@eecs.umich.edu            return start;
8705877Shsul@eecs.umich.edu        } else {
87110027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8725877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8735877Shsul@eecs.umich.edu                return -ENOMEM;
8745877Shsul@eecs.umich.edu            } else {
87510027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
87610027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
87710027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
87810027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
87910027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
88010027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8815877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
88210027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
88310027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
88410027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
88510027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
88610027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
88710027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
88810027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
88910027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
89010027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
89110027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
89210027SChris.Adeniyi-Jones@arm.com                }
89310027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
89410027SChris.Adeniyi-Jones@arm.com                return new_start;
8955877Shsul@eecs.umich.edu            }
8965877Shsul@eecs.umich.edu        }
8975877Shsul@eecs.umich.edu    } else {
89810027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
89910027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
9008601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
90110027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
9025877Shsul@eecs.umich.edu    }
9035877Shsul@eecs.umich.edu}
9041999SN/A
905378SN/A/// Target stat() handler.
906360SN/Atemplate <class OS>
9071450SN/ASyscallReturn
9083114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9092680Sktlim@umich.edu         ThreadContext *tc)
910360SN/A{
911360SN/A    std::string path;
912360SN/A
9136701Sgblack@eecs.umich.edu    int index = 0;
9148852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9156701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9166701Sgblack@eecs.umich.edu        return -EFAULT;
9176701Sgblack@eecs.umich.edu    }
9186701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
919360SN/A
9203669Sbinkertn@umich.edu    // Adjust path for current working directory
9213669Sbinkertn@umich.edu    path = process->fullPath(path);
9223669Sbinkertn@umich.edu
923360SN/A    struct stat hostBuf;
924360SN/A    int result = stat(path.c_str(), &hostBuf);
925360SN/A
926360SN/A    if (result < 0)
9272218SN/A        return -errno;
928360SN/A
9298706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
930360SN/A
9311458SN/A    return 0;
932360SN/A}
933360SN/A
934360SN/A
9355074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9365074Ssaidi@eecs.umich.edutemplate <class OS>
9375074Ssaidi@eecs.umich.eduSyscallReturn
9385074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9395074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9405074Ssaidi@eecs.umich.edu{
9415074Ssaidi@eecs.umich.edu    std::string path;
9425074Ssaidi@eecs.umich.edu
9436701Sgblack@eecs.umich.edu    int index = 0;
9448852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9456701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9465074Ssaidi@eecs.umich.edu        return -EFAULT;
9476701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9485074Ssaidi@eecs.umich.edu
9495074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9505074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9515074Ssaidi@eecs.umich.edu
9525208Ssaidi@eecs.umich.edu#if NO_STAT64
9535208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9545208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9555208Ssaidi@eecs.umich.edu#else
9565074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9575074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9585208Ssaidi@eecs.umich.edu#endif
9595074Ssaidi@eecs.umich.edu
9605074Ssaidi@eecs.umich.edu    if (result < 0)
9615074Ssaidi@eecs.umich.edu        return -errno;
9625074Ssaidi@eecs.umich.edu
9638706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9645074Ssaidi@eecs.umich.edu
9655074Ssaidi@eecs.umich.edu    return 0;
9665074Ssaidi@eecs.umich.edu}
9675074Ssaidi@eecs.umich.edu
9685074Ssaidi@eecs.umich.edu
96910027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
97010027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
97110027SChris.Adeniyi-Jones@arm.comSyscallReturn
97210027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
97310027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
97410027SChris.Adeniyi-Jones@arm.com{
97510027SChris.Adeniyi-Jones@arm.com    int index = 0;
97610027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
97710027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
97810793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
97910027SChris.Adeniyi-Jones@arm.com
98010027SChris.Adeniyi-Jones@arm.com    std::string path;
98110027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
98210027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
98310027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
98410027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
98510027SChris.Adeniyi-Jones@arm.com
98610027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
98710027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
98810027SChris.Adeniyi-Jones@arm.com
98910027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
99010027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
99110027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
99210027SChris.Adeniyi-Jones@arm.com#else
99310027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
99410027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
99510027SChris.Adeniyi-Jones@arm.com#endif
99610027SChris.Adeniyi-Jones@arm.com
99710027SChris.Adeniyi-Jones@arm.com    if (result < 0)
99810027SChris.Adeniyi-Jones@arm.com        return -errno;
99910027SChris.Adeniyi-Jones@arm.com
100010027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
100110027SChris.Adeniyi-Jones@arm.com
100210027SChris.Adeniyi-Jones@arm.com    return 0;
100310027SChris.Adeniyi-Jones@arm.com}
100410027SChris.Adeniyi-Jones@arm.com
100510027SChris.Adeniyi-Jones@arm.com
10061999SN/A/// Target fstat64() handler.
10071999SN/Atemplate <class OS>
10081999SN/ASyscallReturn
10093114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10102680Sktlim@umich.edu            ThreadContext *tc)
10111999SN/A{
10126701Sgblack@eecs.umich.edu    int index = 0;
101310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
10146701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
101510931Sbrandon.potter@amd.com
101610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
101710931Sbrandon.potter@amd.com    if (sim_fd < 0)
10181999SN/A        return -EBADF;
10191999SN/A
10202764Sstever@eecs.umich.edu#if NO_STAT64
10212064SN/A    struct stat  hostBuf;
102210931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10232064SN/A#else
10242064SN/A    struct stat64  hostBuf;
102510931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10262064SN/A#endif
10271999SN/A
10281999SN/A    if (result < 0)
10292218SN/A        return -errno;
10301999SN/A
103110931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10321999SN/A
10331999SN/A    return 0;
10341999SN/A}
10351999SN/A
10361999SN/A
1037378SN/A/// Target lstat() handler.
1038360SN/Atemplate <class OS>
10391450SN/ASyscallReturn
10403114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10412680Sktlim@umich.edu          ThreadContext *tc)
1042360SN/A{
1043360SN/A    std::string path;
1044360SN/A
10456701Sgblack@eecs.umich.edu    int index = 0;
10468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10476701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10486701Sgblack@eecs.umich.edu        return -EFAULT;
10496701Sgblack@eecs.umich.edu    }
10506701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1051360SN/A
10523669Sbinkertn@umich.edu    // Adjust path for current working directory
10533669Sbinkertn@umich.edu    path = process->fullPath(path);
10543669Sbinkertn@umich.edu
1055360SN/A    struct stat hostBuf;
1056360SN/A    int result = lstat(path.c_str(), &hostBuf);
1057360SN/A
1058360SN/A    if (result < 0)
10591458SN/A        return -errno;
1060360SN/A
10618706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1062360SN/A
10631458SN/A    return 0;
1064360SN/A}
1065360SN/A
10661999SN/A/// Target lstat64() handler.
10671999SN/Atemplate <class OS>
10681999SN/ASyscallReturn
10693114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10702680Sktlim@umich.edu            ThreadContext *tc)
10711999SN/A{
10721999SN/A    std::string path;
10731999SN/A
10746701Sgblack@eecs.umich.edu    int index = 0;
10758852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10766701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10776701Sgblack@eecs.umich.edu        return -EFAULT;
10786701Sgblack@eecs.umich.edu    }
10796701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10801999SN/A
10813669Sbinkertn@umich.edu    // Adjust path for current working directory
10823669Sbinkertn@umich.edu    path = process->fullPath(path);
10833669Sbinkertn@umich.edu
10842764Sstever@eecs.umich.edu#if NO_STAT64
10852064SN/A    struct stat hostBuf;
10862064SN/A    int result = lstat(path.c_str(), &hostBuf);
10872064SN/A#else
10881999SN/A    struct stat64 hostBuf;
10891999SN/A    int result = lstat64(path.c_str(), &hostBuf);
10902064SN/A#endif
10911999SN/A
10921999SN/A    if (result < 0)
10931999SN/A        return -errno;
10941999SN/A
10958706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10961999SN/A
10971999SN/A    return 0;
10981999SN/A}
10991999SN/A
1100378SN/A/// Target fstat() handler.
1101360SN/Atemplate <class OS>
11021450SN/ASyscallReturn
11033114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11042680Sktlim@umich.edu          ThreadContext *tc)
1105360SN/A{
11066701Sgblack@eecs.umich.edu    int index = 0;
110710931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11086701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1109360SN/A
111011380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1111360SN/A
111210932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
111310931Sbrandon.potter@amd.com    if (sim_fd < 0)
11141458SN/A        return -EBADF;
1115360SN/A
1116360SN/A    struct stat hostBuf;
111710931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1118360SN/A
1119360SN/A    if (result < 0)
11201458SN/A        return -errno;
1121360SN/A
112210931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11232021SN/A
11241458SN/A    return 0;
1125360SN/A}
1126360SN/A
1127360SN/A
11281706SN/A/// Target statfs() handler.
11291706SN/Atemplate <class OS>
11301706SN/ASyscallReturn
11313114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11322680Sktlim@umich.edu           ThreadContext *tc)
11331706SN/A{
113411799Sbrandon.potter@amd.com#if NO_STATFS
113511799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
113611799Sbrandon.potter@amd.com#else
11371706SN/A    std::string path;
11381706SN/A
11396701Sgblack@eecs.umich.edu    int index = 0;
11408852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11416701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11426701Sgblack@eecs.umich.edu        return -EFAULT;
11436701Sgblack@eecs.umich.edu    }
11446701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11451706SN/A
11463669Sbinkertn@umich.edu    // Adjust path for current working directory
11473669Sbinkertn@umich.edu    path = process->fullPath(path);
11483669Sbinkertn@umich.edu
11491706SN/A    struct statfs hostBuf;
11501706SN/A    int result = statfs(path.c_str(), &hostBuf);
11511706SN/A
11521706SN/A    if (result < 0)
11532218SN/A        return -errno;
11541706SN/A
115511759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
115611799Sbrandon.potter@amd.com#endif
11571706SN/A    return 0;
11581706SN/A}
11591706SN/A
11601706SN/A
11611706SN/A/// Target fstatfs() handler.
11621706SN/Atemplate <class OS>
11631706SN/ASyscallReturn
11643114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11652680Sktlim@umich.edu            ThreadContext *tc)
11661706SN/A{
11676701Sgblack@eecs.umich.edu    int index = 0;
116810931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11696701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11701706SN/A
117110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
117210931Sbrandon.potter@amd.com    if (sim_fd < 0)
11731706SN/A        return -EBADF;
11741706SN/A
11751706SN/A    struct statfs hostBuf;
117610931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11771706SN/A
11781706SN/A    if (result < 0)
11792218SN/A        return -errno;
11801706SN/A
118111759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11821706SN/A
11831706SN/A    return 0;
11841706SN/A}
11851706SN/A
11861706SN/A
11871999SN/A/// Target writev() handler.
11881999SN/Atemplate <class OS>
11891999SN/ASyscallReturn
11903114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11912680Sktlim@umich.edu           ThreadContext *tc)
11921999SN/A{
11936701Sgblack@eecs.umich.edu    int index = 0;
119410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
119510931Sbrandon.potter@amd.com
119610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
119710931Sbrandon.potter@amd.com    if (sim_fd < 0)
11981999SN/A        return -EBADF;
11991999SN/A
12008852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
12016701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
12026701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
12031999SN/A    struct iovec hiov[count];
12046227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
12051999SN/A        typename OS::tgt_iovec tiov;
12062461SN/A
12078852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12088852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12098737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
12101999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
12118852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12128852Sandreas.hansson@arm.com                   hiov[i].iov_len);
12131999SN/A    }
12141999SN/A
121510931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
12161999SN/A
12176227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
12181999SN/A        delete [] (char *)hiov[i].iov_base;
12191999SN/A
12201999SN/A    if (result < 0)
12212218SN/A        return -errno;
12221999SN/A
122310629Sjthestness@gmail.com    return result;
12241999SN/A}
12251999SN/A
122611385Sbrandon.potter@amd.com/// Real mmap handler.
1227360SN/Atemplate <class OS>
12281450SN/ASyscallReturn
122911385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
123011385Sbrandon.potter@amd.com         bool is_mmap2)
1231360SN/A{
12326701Sgblack@eecs.umich.edu    int index = 0;
12336701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12346701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
123511383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
123611383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
12378324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
123810486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1239360SN/A
124011385Sbrandon.potter@amd.com    if (is_mmap2)
124111385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
12429008Sgblack@eecs.umich.edu
124311383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
124411383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
124511383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
124611383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
124711383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
124811383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
124911383Sbrandon.potter@amd.com        !length) {
125011383Sbrandon.potter@amd.com        return -EINVAL;
125111383Sbrandon.potter@amd.com    }
12528324Ssteve.reinhardt@amd.com
125311383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
125411383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
125511383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
125611383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
125711383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
125811383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
125911383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
126011383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
126111383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
126211383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
126311383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
126411383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
126511383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
126611383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
126711383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
126811383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
126911383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
127011383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
127111383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
127211383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
127311383Sbrandon.potter@amd.com        // than we currently maintain.
127411383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
127511383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
127611383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
12778324Ssteve.reinhardt@amd.com    }
12785877Shsul@eecs.umich.edu
127910486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
128010486Stjablin@gmail.com
128111383Sbrandon.potter@amd.com    int sim_fd = -1;
128211383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
128311383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
128411624Smichael.lebeane@amd.com        // Check for EmulatedDriver mmap
128511624Smichael.lebeane@amd.com        FDEntry *fde = p->getFDEntry(tgt_fd);
128611624Smichael.lebeane@amd.com        if (fde == NULL)
128711624Smichael.lebeane@amd.com            return -EBADF;
128811624Smichael.lebeane@amd.com
128911624Smichael.lebeane@amd.com        if (fde->driver != NULL) {
129011624Smichael.lebeane@amd.com            return fde->driver->mmap(p, tc, start, length, prot,
129111624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
129211624Smichael.lebeane@amd.com        }
129311624Smichael.lebeane@amd.com        sim_fd = fde->fd;
129411624Smichael.lebeane@amd.com
129511383Sbrandon.potter@amd.com        if (sim_fd < 0)
129611383Sbrandon.potter@amd.com            return -EBADF;
1297360SN/A
129811383Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
129911383Sbrandon.potter@amd.com                                    sim_fd, offset);
13008600Ssteve.reinhardt@amd.com
130111383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
130211383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
130311383Sbrandon.potter@amd.com            return -errno;
13048600Ssteve.reinhardt@amd.com        }
13052544SN/A    }
13062544SN/A
130711383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
130811383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
130911383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
131011386Ssteve.reinhardt@amd.com        start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end;
131111386Ssteve.reinhardt@amd.com        p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length;
131211383Sbrandon.potter@amd.com    }
131311383Sbrandon.potter@amd.com
131411383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
131511383Sbrandon.potter@amd.com                    start, start + length - 1);
131611383Sbrandon.potter@amd.com
131711383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
131811383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
131911383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
132011383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
132111383Sbrandon.potter@amd.com    if (clobber) {
132211383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
132311383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
132411383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
132511383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
132611383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
13278600Ssteve.reinhardt@amd.com        }
13286672Sgblack@eecs.umich.edu    }
13298600Ssteve.reinhardt@amd.com
133011383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
133111383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
133211383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
13338601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
13342544SN/A
133511383Sbrandon.potter@amd.com    // Transfer content into target address space.
133611383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
133711383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
133811383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
133911383Sbrandon.potter@amd.com        // with something like:
134011383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
134111383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
134211383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
134311383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
134411383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
134511383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
134611383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
134711383Sbrandon.potter@amd.com    } else {
134811383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
134911383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
135011383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
135111383Sbrandon.potter@amd.com        // the host into the target address space.
135211383Sbrandon.potter@amd.com        struct stat file_stat;
135311383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
135411383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
135511383Sbrandon.potter@amd.com
135611383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
135711383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
135811383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
135911383Sbrandon.potter@amd.com        // on the specified offset into the file.
136011383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
136111383Sbrandon.potter@amd.com                                 length);
136211383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
136311383Sbrandon.potter@amd.com
136411383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
136511383Sbrandon.potter@amd.com        munmap(pmap, length);
136611383Sbrandon.potter@amd.com
136711392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
136811392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
136911392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
137011392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
137111392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
137211392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
137311392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
137411392Sbrandon.potter@amd.com        if (interpreter) {
137511392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
137611392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
137711392Sbrandon.potter@amd.com
137811392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
137911392Sbrandon.potter@amd.com
138011392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
138111392Sbrandon.potter@amd.com                FDEntry *fde = p->getFDEntry(tgt_fd);
138211392Sbrandon.potter@amd.com
138311392Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(fde->filename);
138411392Sbrandon.potter@amd.com
138511392Sbrandon.potter@amd.com                if (lib) {
138611392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
138711392Sbrandon.potter@amd.com                                        lib->textBase(), start);
138811392Sbrandon.potter@amd.com                }
138911392Sbrandon.potter@amd.com            }
139011392Sbrandon.potter@amd.com        }
139111392Sbrandon.potter@amd.com
139211383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
139311383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
139411383Sbrandon.potter@amd.com        // execution (hopefully).
139511383Sbrandon.potter@amd.com    }
139611383Sbrandon.potter@amd.com
13971458SN/A    return start;
1398360SN/A}
1399360SN/A
140011593Santhony.gutierrez@amd.comtemplate <class OS>
140111593Santhony.gutierrez@amd.comSyscallReturn
140211593Santhony.gutierrez@amd.compwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
140311593Santhony.gutierrez@amd.com{
140411593Santhony.gutierrez@amd.com    int index = 0;
140511593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
140611593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
140711593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
140811593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
140911593Santhony.gutierrez@amd.com
141011593Santhony.gutierrez@amd.com    int sim_fd = p->getSimFD(tgt_fd);
141111593Santhony.gutierrez@amd.com    if (sim_fd < 0)
141211593Santhony.gutierrez@amd.com        return -EBADF;
141311593Santhony.gutierrez@amd.com
141411593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
141511593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
141611593Santhony.gutierrez@amd.com
141711594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
141811593Santhony.gutierrez@amd.com
141911593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
142011593Santhony.gutierrez@amd.com}
142111593Santhony.gutierrez@amd.com
142211385Sbrandon.potter@amd.com/// Target mmap() handler.
142311385Sbrandon.potter@amd.comtemplate <class OS>
142411385Sbrandon.potter@amd.comSyscallReturn
142511385Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
142611385Sbrandon.potter@amd.com{
142711385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
142811385Sbrandon.potter@amd.com}
142911385Sbrandon.potter@amd.com
143011385Sbrandon.potter@amd.com/// Target mmap2() handler.
143111385Sbrandon.potter@amd.comtemplate <class OS>
143211385Sbrandon.potter@amd.comSyscallReturn
143311385Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
143411385Sbrandon.potter@amd.com{
143511385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
143611385Sbrandon.potter@amd.com}
143711385Sbrandon.potter@amd.com
1438378SN/A/// Target getrlimit() handler.
1439360SN/Atemplate <class OS>
14401450SN/ASyscallReturn
14413114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14422680Sktlim@umich.edu        ThreadContext *tc)
1443360SN/A{
14446701Sgblack@eecs.umich.edu    int index = 0;
14456701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
14466701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1447360SN/A
1448360SN/A    switch (resource) {
14492064SN/A        case OS::TGT_RLIMIT_STACK:
14505877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
14512064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
14528737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14538737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14542064SN/A            break;
1455360SN/A
14565877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
14575877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
14585877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
14598737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14608737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14615877Shsul@eecs.umich.edu            break;
14625877Shsul@eecs.umich.edu
14632064SN/A        default:
146410794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
146510794Sbrandon.potter@amd.com            return -EINVAL;
14662064SN/A            break;
1467360SN/A    }
1468360SN/A
14698706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
14701458SN/A    return 0;
1471360SN/A}
1472360SN/A
147310796Sbrandon.potter@amd.com/// Target clock_gettime() function.
147410796Sbrandon.potter@amd.comtemplate <class OS>
147510796Sbrandon.potter@amd.comSyscallReturn
147610796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
147710796Sbrandon.potter@amd.com{
147810796Sbrandon.potter@amd.com    int index = 1;
147910796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
148010796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
148110796Sbrandon.potter@amd.com
148210796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
148310796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
148410796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
148510796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
148610796Sbrandon.potter@amd.com
148710796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
148810796Sbrandon.potter@amd.com
148910796Sbrandon.potter@amd.com    return 0;
149010796Sbrandon.potter@amd.com}
149110796Sbrandon.potter@amd.com
149211337SMichael.Lebeane@amd.com/// Target clock_getres() function.
149311337SMichael.Lebeane@amd.comtemplate <class OS>
149411337SMichael.Lebeane@amd.comSyscallReturn
149511337SMichael.Lebeane@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
149611337SMichael.Lebeane@amd.com{
149711337SMichael.Lebeane@amd.com    int index = 1;
149811337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
149911337SMichael.Lebeane@amd.com
150011337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
150111337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
150211337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
150311337SMichael.Lebeane@amd.com
150411337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
150511337SMichael.Lebeane@amd.com
150611337SMichael.Lebeane@amd.com    return 0;
150711337SMichael.Lebeane@amd.com}
150811337SMichael.Lebeane@amd.com
1509378SN/A/// Target gettimeofday() handler.
1510360SN/Atemplate <class OS>
15111450SN/ASyscallReturn
15123114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15132680Sktlim@umich.edu        ThreadContext *tc)
1514360SN/A{
15156701Sgblack@eecs.umich.edu    int index = 0;
15166701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1517360SN/A
151810796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1519360SN/A    tp->tv_sec += seconds_since_epoch;
15206109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
15216109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1522360SN/A
15238706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1524360SN/A
15251458SN/A    return 0;
1526360SN/A}
1527360SN/A
1528360SN/A
15291999SN/A/// Target utimes() handler.
15301999SN/Atemplate <class OS>
15311999SN/ASyscallReturn
15323114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15332680Sktlim@umich.edu           ThreadContext *tc)
15341999SN/A{
15351999SN/A    std::string path;
15361999SN/A
15376701Sgblack@eecs.umich.edu    int index = 0;
15388852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
15396701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
15406701Sgblack@eecs.umich.edu        return -EFAULT;
15416701Sgblack@eecs.umich.edu    }
15421999SN/A
15436701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
15446701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
15458706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
15461999SN/A
15471999SN/A    struct timeval hostTimeval[2];
15481999SN/A    for (int i = 0; i < 2; ++i)
15491999SN/A    {
15508737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
15518737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
15521999SN/A    }
15533669Sbinkertn@umich.edu
15543669Sbinkertn@umich.edu    // Adjust path for current working directory
15553669Sbinkertn@umich.edu    path = process->fullPath(path);
15563669Sbinkertn@umich.edu
15571999SN/A    int result = utimes(path.c_str(), hostTimeval);
15581999SN/A
15591999SN/A    if (result < 0)
15601999SN/A        return -errno;
15611999SN/A
15621999SN/A    return 0;
15631999SN/A}
1564378SN/A/// Target getrusage() function.
1565360SN/Atemplate <class OS>
15661450SN/ASyscallReturn
15673114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15682680Sktlim@umich.edu              ThreadContext *tc)
1569360SN/A{
15706701Sgblack@eecs.umich.edu    int index = 0;
15716701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
15726701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1573360SN/A
15743670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
15753670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1576360SN/A    rup->ru_stime.tv_sec = 0;
1577360SN/A    rup->ru_stime.tv_usec = 0;
1578360SN/A    rup->ru_maxrss = 0;
1579360SN/A    rup->ru_ixrss = 0;
1580360SN/A    rup->ru_idrss = 0;
1581360SN/A    rup->ru_isrss = 0;
1582360SN/A    rup->ru_minflt = 0;
1583360SN/A    rup->ru_majflt = 0;
1584360SN/A    rup->ru_nswap = 0;
1585360SN/A    rup->ru_inblock = 0;
1586360SN/A    rup->ru_oublock = 0;
1587360SN/A    rup->ru_msgsnd = 0;
1588360SN/A    rup->ru_msgrcv = 0;
1589360SN/A    rup->ru_nsignals = 0;
1590360SN/A    rup->ru_nvcsw = 0;
1591360SN/A    rup->ru_nivcsw = 0;
1592360SN/A
15933670Sbinkertn@umich.edu    switch (who) {
15943670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
159510796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
15968737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
15978737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
15983670Sbinkertn@umich.edu        break;
15993670Sbinkertn@umich.edu
16003670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
16013670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
16023670Sbinkertn@umich.edu        break;
16033670Sbinkertn@umich.edu
16043670Sbinkertn@umich.edu      default:
16053670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
16063670Sbinkertn@umich.edu        // plow ahead
16073670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
16083670Sbinkertn@umich.edu             who);
16093670Sbinkertn@umich.edu    }
16103670Sbinkertn@umich.edu
16118706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1612360SN/A
16131458SN/A    return 0;
1614360SN/A}
1615360SN/A
16166683Stjones1@inf.ed.ac.uk/// Target times() function.
16176683Stjones1@inf.ed.ac.uktemplate <class OS>
16186683Stjones1@inf.ed.ac.ukSyscallReturn
16196683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
16206683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
16216683Stjones1@inf.ed.ac.uk{
16226701Sgblack@eecs.umich.edu    int index = 0;
16236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
16246683Stjones1@inf.ed.ac.uk
16256683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
16267823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
16276683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
16286683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
16296683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
16306683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
16316683Stjones1@inf.ed.ac.uk
16326683Stjones1@inf.ed.ac.uk    // Convert to host endianness
16338737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
16346683Stjones1@inf.ed.ac.uk
16356683Stjones1@inf.ed.ac.uk    // Write back
16368706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
16376683Stjones1@inf.ed.ac.uk
16386683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
16396683Stjones1@inf.ed.ac.uk    return clocks;
16406683Stjones1@inf.ed.ac.uk}
16412553SN/A
16426684Stjones1@inf.ed.ac.uk/// Target time() function.
16436684Stjones1@inf.ed.ac.uktemplate <class OS>
16446684Stjones1@inf.ed.ac.ukSyscallReturn
16456684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
16466684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
16476684Stjones1@inf.ed.ac.uk{
16486684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
164910796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
16506684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
16516684Stjones1@inf.ed.ac.uk
16526701Sgblack@eecs.umich.edu    int index = 0;
16536701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
165411321Ssteve.reinhardt@amd.com    if (taddr != 0) {
16556684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
16568737Skoansin.tan@gmail.com        t = TheISA::htog(t);
16578852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
16588852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
16596684Stjones1@inf.ed.ac.uk    }
16606684Stjones1@inf.ed.ac.uk    return sec;
16616684Stjones1@inf.ed.ac.uk}
16622553SN/A
16632553SN/A
16641354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1665