syscall_emul.hh revision 11812
1360SN/A/*
210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited
310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc.
410027SChris.Adeniyi-Jones@arm.com * All rights reserved
510027SChris.Adeniyi-Jones@arm.com *
610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1410027SChris.Adeniyi-Jones@arm.com *
151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
16360SN/A * All rights reserved.
17360SN/A *
18360SN/A * Redistribution and use in source and binary forms, with or without
19360SN/A * modification, are permitted provided that the following conditions are
20360SN/A * met: redistributions of source code must retain the above copyright
21360SN/A * notice, this list of conditions and the following disclaimer;
22360SN/A * redistributions in binary form must reproduce the above copyright
23360SN/A * notice, this list of conditions and the following disclaimer in the
24360SN/A * documentation and/or other materials provided with the distribution;
25360SN/A * neither the name of the copyright holders nor the names of its
26360SN/A * contributors may be used to endorse or promote products derived from
27360SN/A * this software without specific prior written permission.
28360SN/A *
29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
422665Ssaidi@eecs.umich.edu *          Kevin Lim
43360SN/A */
44360SN/A
451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
461354SN/A#define __SIM_SYSCALL_EMUL_HH__
47360SN/A
482764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
499202Spalle@lyckegaard.dk  defined(__FreeBSD__) || defined(__CYGWIN__) || \
509202Spalle@lyckegaard.dk  defined(__NetBSD__))
512064SN/A
5211799Sbrandon.potter@amd.com#define NO_STATFS (defined(__APPLE__) || defined(__OpenBSD__) || \
5311799Sbrandon.potter@amd.com  defined(__FreeBSD__) || defined(__NetBSD__))
5411799Sbrandon.potter@amd.com
5511799Sbrandon.potter@amd.com#define NO_FALLOCATE (defined(__APPLE__) || defined(__OpenBSD__) || \
5611799Sbrandon.potter@amd.com  defined(__FreeBSD__) || defined(__NetBSD__))
5711799Sbrandon.potter@amd.com
58360SN/A///
59360SN/A/// @file syscall_emul.hh
60360SN/A///
61360SN/A/// This file defines objects used to emulate syscalls from the target
62360SN/A/// application on the host machine.
63360SN/A
641809SN/A#ifdef __CYGWIN32__
6511800Sbrandon.potter@amd.com#include <sys/fcntl.h>
6611392Sbrandon.potter@amd.com
671809SN/A#endif
6811392Sbrandon.potter@amd.com#include <fcntl.h>
6911383Sbrandon.potter@amd.com#include <sys/mman.h>
703113Sgblack@eecs.umich.edu#include <sys/stat.h>
7111799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
7211759Sbrandon.potter@amd.com#include <sys/statfs.h>
7311812Sbaz21@cam.ac.uk#else
7411812Sbaz21@cam.ac.uk#include <sys/mount.h>
7511799Sbrandon.potter@amd.com#endif
768229Snate@binkert.org#include <sys/time.h>
778229Snate@binkert.org#include <sys/uio.h>
7811594Santhony.gutierrez@amd.com#include <unistd.h>
797075Snate@binkert.org
808229Snate@binkert.org#include <cerrno>
817075Snate@binkert.org#include <string>
82360SN/A
8311800Sbrandon.potter@amd.com#include "base/intmath.hh"
8411392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
852462SN/A#include "base/misc.hh"
861354SN/A#include "base/trace.hh"
876216Snate@binkert.org#include "base/types.hh"
886658Snate@binkert.org#include "config/the_isa.hh"
892474SN/A#include "cpu/base.hh"
902680Sktlim@umich.edu#include "cpu/thread_context.hh"
918229Snate@binkert.org#include "mem/page_table.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"
97360SN/A
9811794Sbrandon.potter@amd.comclass SyscallDesc;
99360SN/A
100360SN/A//////////////////////////////////////////////////////////////////////
101360SN/A//
102360SN/A// The following emulation functions are generic enough that they
103360SN/A// don't need to be recompiled for different emulated OS's.  They are
104360SN/A// defined in sim/syscall_emul.cc.
105360SN/A//
106360SN/A//////////////////////////////////////////////////////////////////////
107360SN/A
108360SN/A
109378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1101706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
1113114Sgblack@eecs.umich.edu                                LiveProcess *p, ThreadContext *tc);
112378SN/A
113378SN/A/// Handler for unimplemented syscalls that we never intend to
114378SN/A/// implement (signal handling, etc.) and should not affect the correct
115378SN/A/// behavior of the program.  Print a warning only if the appropriate
116378SN/A/// trace flag is enabled.  Return success to the target program.
1171706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
1183114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
119360SN/A
12011760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
12111760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
12211760Sbrandon.potter@amd.com                            LiveProcess *p, ThreadContext *tc);
12311760Sbrandon.potter@amd.com
1246109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1251706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
1263114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
127378SN/A
1286109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1296109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
1306109Ssanchezd@stanford.edu                       LiveProcess *p, ThreadContext *tc);
1316109Ssanchezd@stanford.edu
132378SN/A/// Target getpagesize() handler.
1331706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
1343114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
135378SN/A
1365748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1375748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
1385748SSteve.Reinhardt@amd.com                      LiveProcess *p, ThreadContext *tc);
139378SN/A
140378SN/A/// Target close() handler.
1411706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
1423114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
143378SN/A
144378SN/A/// Target read() handler.
1451706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
1463114Sgblack@eecs.umich.edu                       LiveProcess *p, ThreadContext *tc);
147378SN/A
148378SN/A/// Target write() handler.
1491706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
1503114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
151378SN/A
152378SN/A/// Target lseek() handler.
1531706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
1543114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
155378SN/A
1564118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1574118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
15811800Sbrandon.potter@amd.com                          LiveProcess *p, ThreadContext *tc);
1594118Sgblack@eecs.umich.edu
160378SN/A/// Target munmap() handler.
1611706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
1623114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
163378SN/A
164378SN/A/// Target gethostname() handler.
1651706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1663114Sgblack@eecs.umich.edu                              LiveProcess *p, ThreadContext *tc);
167360SN/A
1685513SMichael.Adler@intel.com/// Target getcwd() handler.
1695513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
1705513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
1715513SMichael.Adler@intel.com
17210203SAli.Saidi@ARM.com/// Target readlink() handler.
17310203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
17410203SAli.Saidi@ARM.com                           LiveProcess *p, ThreadContext *tc,
17510203SAli.Saidi@ARM.com                           int index = 0);
1765513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
1775513SMichael.Adler@intel.com                           LiveProcess *p, ThreadContext *tc);
1785513SMichael.Adler@intel.com
179511SN/A/// Target unlink() handler.
18010633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
18110633Smichaelupton@gmail.com                           LiveProcess *p, ThreadContext *tc,
18210633Smichaelupton@gmail.com                           int index);
1831706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
1843114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
185511SN/A
1865513SMichael.Adler@intel.com/// Target mkdir() handler.
1875513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
1885513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
1895513SMichael.Adler@intel.com
190511SN/A/// Target rename() handler.
1911706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
1923114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
1931706SN/A
1941706SN/A
1951706SN/A/// Target truncate() handler.
1961706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
1973114Sgblack@eecs.umich.edu                           LiveProcess *p, ThreadContext *tc);
1981706SN/A
1991706SN/A
2001706SN/A/// Target ftruncate() handler.
2011706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2023114Sgblack@eecs.umich.edu                            LiveProcess *p, ThreadContext *tc);
2031706SN/A
204511SN/A
2056703Svince@csl.cornell.edu/// Target truncate64() handler.
2066703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2076703Svince@csl.cornell.edu                             LiveProcess *p, ThreadContext *tc);
2086703Svince@csl.cornell.edu
2096685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2106685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2116685Stjones1@inf.ed.ac.uk                              LiveProcess *p, ThreadContext *tc);
2126685Stjones1@inf.ed.ac.uk
2136685Stjones1@inf.ed.ac.uk
2145513SMichael.Adler@intel.com/// Target umask() handler.
2155513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2165513SMichael.Adler@intel.com                        LiveProcess *p, ThreadContext *tc);
2175513SMichael.Adler@intel.com
2185513SMichael.Adler@intel.com
2191999SN/A/// Target chown() handler.
2201999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
2213114Sgblack@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
2221999SN/A
2231999SN/A
2241999SN/A/// Target fchown() handler.
2251999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
2263114Sgblack@eecs.umich.edu                         LiveProcess *p, ThreadContext *tc);
2271999SN/A
2283079Sstever@eecs.umich.edu/// Target dup() handler.
2293079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
2303114Sgblack@eecs.umich.edu                      LiveProcess *process, ThreadContext *tc);
2313079Sstever@eecs.umich.edu
2322093SN/A/// Target fnctl() handler.
2332093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2343114Sgblack@eecs.umich.edu                        LiveProcess *process, ThreadContext *tc);
2352093SN/A
2362687Sksewell@umich.edu/// Target fcntl64() handler.
2372687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
23811800Sbrandon.potter@amd.com                          LiveProcess *process, ThreadContext *tc);
2392687Sksewell@umich.edu
2402238SN/A/// Target setuid() handler.
2412238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
24211800Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
2432238SN/A
2442238SN/A/// Target getpid() handler.
2452238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
24611800Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
2472238SN/A
2482238SN/A/// Target getuid() handler.
2492238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
25011800Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
2512238SN/A
2522238SN/A/// Target getgid() handler.
2532238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
25411800Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
2552238SN/A
2562238SN/A/// Target getppid() handler.
2572238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
25811800Sbrandon.potter@amd.com                          LiveProcess *p, ThreadContext *tc);
2592238SN/A
2602238SN/A/// Target geteuid() handler.
2612238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
26211800Sbrandon.potter@amd.com                          LiveProcess *p, ThreadContext *tc);
2632238SN/A
2642238SN/A/// Target getegid() handler.
2652238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
26611800Sbrandon.potter@amd.com                          LiveProcess *p, ThreadContext *tc);
2672238SN/A
2686109Ssanchezd@stanford.edu/// Target clone() handler.
2696109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num,
27011800Sbrandon.potter@amd.com                        LiveProcess *p, ThreadContext *tc);
2712238SN/A
2729455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2739455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
2749455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
27510203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
27610203SAli.Saidi@ARM.com                               LiveProcess *p, ThreadContext *tc,
27710203SAli.Saidi@ARM.com                               int index);
2789455Smitch.hayenga+gem5@gmail.com
2799112Smarc.orr@gmail.com/// Futex system call
2809112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
2819112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
2829112Smarc.orr@gmail.comtemplate <class OS>
2839112Smarc.orr@gmail.comSyscallReturn
2849112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
2859112Smarc.orr@gmail.com          ThreadContext *tc)
2869112Smarc.orr@gmail.com{
2879112Smarc.orr@gmail.com    int index_uaddr = 0;
2889112Smarc.orr@gmail.com    int index_op = 1;
2899112Smarc.orr@gmail.com    int index_val = 2;
2909112Smarc.orr@gmail.com    int index_timeout = 3;
2919112Smarc.orr@gmail.com
2929112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
2939112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
2949112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
2959112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
2969112Smarc.orr@gmail.com
2979112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
2989112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
2999112Smarc.orr@gmail.com
3009112Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3019112Smarc.orr@gmail.com            uaddr, op, val);
3029112Smarc.orr@gmail.com
3039238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3049112Smarc.orr@gmail.com
3059112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3069112Smarc.orr@gmail.com        if (timeout != 0) {
3079112Smarc.orr@gmail.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
3089112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3099112Smarc.orr@gmail.com        }
3109112Smarc.orr@gmail.com
3119112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3129112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3139112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
31411367Sandreas.hansson@arm.com        delete[] buf;
3159112Smarc.orr@gmail.com
31611321Ssteve.reinhardt@amd.com        if (val != mem_val) {
3179112Smarc.orr@gmail.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3189112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3199112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3209112Smarc.orr@gmail.com        }
3219112Smarc.orr@gmail.com
3229112Smarc.orr@gmail.com        // Queue the thread context
3239112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3249112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3259112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3269112Smarc.orr@gmail.com        } else {
3279112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3289112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3299112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3309112Smarc.orr@gmail.com        }
3319112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
3329112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
3339112Smarc.orr@gmail.com                                "thread context\n");
3349112Smarc.orr@gmail.com        tc->suspend();
3359112Smarc.orr@gmail.com        return 0;
3369112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3379112Smarc.orr@gmail.com        int wokenUp = 0;
3389112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3399112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3409112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3419112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3429112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3439112Smarc.orr@gmail.com                tcWaitList->pop_front();
3449112Smarc.orr@gmail.com                wokenUp++;
3459112Smarc.orr@gmail.com            }
34611321Ssteve.reinhardt@amd.com            if (tcWaitList->empty()) {
3479112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3489112Smarc.orr@gmail.com                delete tcWaitList;
3499112Smarc.orr@gmail.com            }
3509112Smarc.orr@gmail.com        }
3519112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3529112Smarc.orr@gmail.com                                "thread contexts\n", wokenUp);
3539112Smarc.orr@gmail.com        return wokenUp;
3549112Smarc.orr@gmail.com    } else {
3559238Slluc.alvarez@bsc.es        warn("sys_futex: op %d is not implemented, just returning...", op);
3569112Smarc.orr@gmail.com        return 0;
3579112Smarc.orr@gmail.com    }
3589112Smarc.orr@gmail.com
3599112Smarc.orr@gmail.com}
3609112Smarc.orr@gmail.com
3612238SN/A
3622238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3632238SN/A/// returning a second value in a register other than the normal return register
3642238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3653114Sgblack@eecs.umich.edu                             LiveProcess *process, ThreadContext *tc);
3662238SN/A
3672238SN/A/// Target getpidPseudo() handler.
3682238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3693114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3702238SN/A
3712238SN/A/// Target getuidPseudo() handler.
3722238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3733114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3742238SN/A
3752238SN/A/// Target getgidPseudo() handler.
3762238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3773114Sgblack@eecs.umich.edu                               LiveProcess *p, ThreadContext *tc);
3782238SN/A
3792238SN/A
3801354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3811354SN/Aconst int one_million = 1000000;
38210796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
38310796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
3841354SN/A
3851354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3861354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3871354SN/A/// real-world time) to keep simulations repeatable.
3881354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3891354SN/A
3901354SN/A/// Helper function to convert current elapsed time to seconds and
3911354SN/A/// microseconds.
3921354SN/Atemplate <class T1, class T2>
3931354SN/Avoid
39410796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
3951354SN/A{
39610796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
3971354SN/A    sec = elapsed_usecs / one_million;
3981354SN/A    usec = elapsed_usecs % one_million;
3991354SN/A}
4001354SN/A
40110796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
40210796Sbrandon.potter@amd.com/// nanoseconds.
40310796Sbrandon.potter@amd.comtemplate <class T1, class T2>
40410796Sbrandon.potter@amd.comvoid
40510796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
40610796Sbrandon.potter@amd.com{
40710796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
40810796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
40910796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
41010796Sbrandon.potter@amd.com}
41110796Sbrandon.potter@amd.com
412360SN/A//////////////////////////////////////////////////////////////////////
413360SN/A//
414360SN/A// The following emulation functions are generic, but need to be
415360SN/A// templated to account for differences in types, constants, etc.
416360SN/A//
417360SN/A//////////////////////////////////////////////////////////////////////
418360SN/A
41911759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4203113Sgblack@eecs.umich.edu#if NO_STAT64
4213113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4223113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4233113Sgblack@eecs.umich.edu#else
4243113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4253113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4263113Sgblack@eecs.umich.edu#endif
4273113Sgblack@eecs.umich.edu
4283113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4293113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4303113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4313113Sgblack@eecs.umich.edu
4323113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4333113Sgblack@eecs.umich.edustatic void
4343113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4353113Sgblack@eecs.umich.edu{
4364189Sgblack@eecs.umich.edu    using namespace TheISA;
4374189Sgblack@eecs.umich.edu
4383113Sgblack@eecs.umich.edu    if (fakeTTY)
4393113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4403113Sgblack@eecs.umich.edu    else
4413113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4428737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4433113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4448737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4453277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4465515SMichael.Adler@intel.com    if (fakeTTY) {
4475515SMichael.Adler@intel.com        // Claim to be a character device
4485515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4495515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4505515SMichael.Adler@intel.com    }
4518737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4523277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4538737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4543277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4558737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4563277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4578737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4583113Sgblack@eecs.umich.edu    if (fakeTTY)
4593113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4603113Sgblack@eecs.umich.edu    else
4613113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4628737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4633113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4648737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4653114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4668737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4673114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4688737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4693114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4708737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4714061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4724061Sgblack@eecs.umich.edu    // consistently across different hosts.
4734061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4748737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4753113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4768737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4773113Sgblack@eecs.umich.edu}
4783113Sgblack@eecs.umich.edu
4793113Sgblack@eecs.umich.edu// Same for stat64
4803113Sgblack@eecs.umich.edu
4813113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4823113Sgblack@eecs.umich.edustatic void
4833113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4843113Sgblack@eecs.umich.edu{
4854189Sgblack@eecs.umich.edu    using namespace TheISA;
4864189Sgblack@eecs.umich.edu
4873113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4883113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4893113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4908737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
4913113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4928737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
4933113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4948737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
4953113Sgblack@eecs.umich.edu#else
4963113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4973113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4983113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4993113Sgblack@eecs.umich.edu#endif
5003113Sgblack@eecs.umich.edu}
5013113Sgblack@eecs.umich.edu
5023113Sgblack@eecs.umich.edu//Here are a couple convenience functions
5033113Sgblack@eecs.umich.edutemplate<class OS>
5043113Sgblack@eecs.umich.edustatic void
5058852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5063113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5073113Sgblack@eecs.umich.edu{
5083113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5093113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5103113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5113113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5123113Sgblack@eecs.umich.edu}
5133113Sgblack@eecs.umich.edu
5143113Sgblack@eecs.umich.edutemplate<class OS>
5153113Sgblack@eecs.umich.edustatic void
5168852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5173113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5183113Sgblack@eecs.umich.edu{
5193113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5203113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5216686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5223113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5233113Sgblack@eecs.umich.edu}
5243113Sgblack@eecs.umich.edu
52511759Sbrandon.potter@amd.comtemplate <class OS>
52611759Sbrandon.potter@amd.comstatic void
52711759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
52811759Sbrandon.potter@amd.com                 hst_statfs *host)
52911759Sbrandon.potter@amd.com{
53011759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
53111759Sbrandon.potter@amd.com
53211812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
53311812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
53411812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
53511759Sbrandon.potter@amd.com#else
53611812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
53711759Sbrandon.potter@amd.com#endif
53811759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
53911759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
54011759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
54111759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
54211759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
54311759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
54411812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
54511812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
54611812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
54711812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
54811812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
54911812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
55011812Sbaz21@cam.ac.uk#else
55111759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
55211759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
55311812Sbaz21@cam.ac.uk#endif
55411812Sbaz21@cam.ac.uk#if defined(__linux__)
55511759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
55611812Sbaz21@cam.ac.uk#else
55711812Sbaz21@cam.ac.uk    /*
55811812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
55911812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
56011812Sbaz21@cam.ac.uk     */
56111812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
56211812Sbaz21@cam.ac.uk#endif
56311759Sbrandon.potter@amd.com
56411759Sbrandon.potter@amd.com    tgt.copyOut(mem);
56511759Sbrandon.potter@amd.com}
56611759Sbrandon.potter@amd.com
567378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
568378SN/A/// only to find out if their stdout is a tty, to determine whether to
5699141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5709141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
571360SN/Atemplate <class OS>
5721450SN/ASyscallReturn
5733114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
5742680Sktlim@umich.edu          ThreadContext *tc)
575360SN/A{
5766701Sgblack@eecs.umich.edu    int index = 0;
57710930Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
5786701Sgblack@eecs.umich.edu    unsigned req = process->getSyscallArg(tc, index);
579360SN/A
58010930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
581360SN/A
58210932Sbrandon.potter@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
58310496Ssteve.reinhardt@amd.com
58410930Sbrandon.potter@amd.com    if (fde == NULL) {
585360SN/A        // doesn't map to any simulator fd: not a valid target fd
5861458SN/A        return -EBADF;
587360SN/A    }
588360SN/A
58910930Sbrandon.potter@amd.com    if (fde->driver != NULL) {
59010930Sbrandon.potter@amd.com        return fde->driver->ioctl(process, tc, req);
59110496Ssteve.reinhardt@amd.com    }
59210496Ssteve.reinhardt@amd.com
5939141Smarc.orr@gmail.com    if (OS::isTtyReq(req)) {
5941458SN/A        return -ENOTTY;
5959141Smarc.orr@gmail.com    }
596360SN/A
5979141Smarc.orr@gmail.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
59810930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
5999141Smarc.orr@gmail.com    return -ENOTTY;
600360SN/A}
601360SN/A
602360SN/Atemplate <class OS>
60310027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
6043114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
60510027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
606360SN/A{
607360SN/A    std::string path;
608360SN/A
6098852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6106701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6111458SN/A        return -EFAULT;
612360SN/A
6136701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6146701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
615360SN/A    int hostFlags = 0;
616360SN/A
617360SN/A    // translate open flags
618360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
619360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
620360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
621360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
622360SN/A        }
623360SN/A    }
624360SN/A
625360SN/A    // any target flags left?
626360SN/A    if (tgtFlags != 0)
6271706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
628360SN/A
629360SN/A#ifdef __CYGWIN32__
630360SN/A    hostFlags |= O_BINARY;
631360SN/A#endif
632360SN/A
6333669Sbinkertn@umich.edu    // Adjust path for current working directory
6343669Sbinkertn@umich.edu    path = process->fullPath(path);
6353669Sbinkertn@umich.edu
6361706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6371706SN/A
63810496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
63910496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
64010496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
64110496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
64210496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
64310496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
64410496Ssteve.reinhardt@amd.com            return -ENOENT;
64510496Ssteve.reinhardt@amd.com        }
64610496Ssteve.reinhardt@amd.com
64710496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
64810496Ssteve.reinhardt@amd.com        if (drv != NULL) {
64910496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
65010496Ssteve.reinhardt@amd.com            // process if necessary.
65110496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
65210496Ssteve.reinhardt@amd.com        }
65310496Ssteve.reinhardt@amd.com
65410496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
65510496Ssteve.reinhardt@amd.com        // /dev/zero
65610496Ssteve.reinhardt@amd.com    }
65710496Ssteve.reinhardt@amd.com
6585795Ssaidi@eecs.umich.edu    int fd;
6599143Ssteve.reinhardt@amd.com    int local_errno;
6609142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6619142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6629143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6635795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6649143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6655795Ssaidi@eecs.umich.edu     } else {
6665795Ssaidi@eecs.umich.edu        // open the file
6675795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6689143Ssteve.reinhardt@amd.com        local_errno = errno;
6695795Ssaidi@eecs.umich.edu     }
670360SN/A
6719143Ssteve.reinhardt@amd.com    if (fd == -1)
6729143Ssteve.reinhardt@amd.com        return -local_errno;
6739143Ssteve.reinhardt@amd.com
67410932Sbrandon.potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
675360SN/A}
676360SN/A
67710027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
67810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
67910027SChris.Adeniyi-Jones@arm.comSyscallReturn
68010027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68110027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
68210027SChris.Adeniyi-Jones@arm.com{
68310027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
68410027SChris.Adeniyi-Jones@arm.com}
68510027SChris.Adeniyi-Jones@arm.com
68610027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
68710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
68810027SChris.Adeniyi-Jones@arm.comSyscallReturn
68910027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69010027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
69110027SChris.Adeniyi-Jones@arm.com{
69210027SChris.Adeniyi-Jones@arm.com    int index = 0;
69310027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
69410027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
69510027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
69610027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
69710027SChris.Adeniyi-Jones@arm.com}
69810027SChris.Adeniyi-Jones@arm.com
69910633Smichaelupton@gmail.com/// Target unlinkat() handler.
70010633Smichaelupton@gmail.comtemplate <class OS>
70110633Smichaelupton@gmail.comSyscallReturn
70210633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70310633Smichaelupton@gmail.com             ThreadContext *tc)
70410633Smichaelupton@gmail.com{
70510633Smichaelupton@gmail.com    int index = 0;
70610633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
70710633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
70810633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
70910633Smichaelupton@gmail.com
71010633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
71110633Smichaelupton@gmail.com}
71210633Smichaelupton@gmail.com
71310203SAli.Saidi@ARM.com/// Target facessat() handler
71410203SAli.Saidi@ARM.comtemplate <class OS>
71510203SAli.Saidi@ARM.comSyscallReturn
71610203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
71710203SAli.Saidi@ARM.com        ThreadContext *tc)
71810203SAli.Saidi@ARM.com{
71910203SAli.Saidi@ARM.com    int index = 0;
72010203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
72110203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
72210203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
72310203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
72410203SAli.Saidi@ARM.com}
72510203SAli.Saidi@ARM.com
72610203SAli.Saidi@ARM.com/// Target readlinkat() handler
72710203SAli.Saidi@ARM.comtemplate <class OS>
72810203SAli.Saidi@ARM.comSyscallReturn
72910203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
73010203SAli.Saidi@ARM.com        ThreadContext *tc)
73110203SAli.Saidi@ARM.com{
73210203SAli.Saidi@ARM.com    int index = 0;
73310203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
73410203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
73510203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
73610203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
73710203SAli.Saidi@ARM.com}
73810203SAli.Saidi@ARM.com
73910850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
74010850SGiacomo.Gabrielli@arm.comtemplate <class OS>
74110850SGiacomo.Gabrielli@arm.comSyscallReturn
74210850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
74310850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
74410850SGiacomo.Gabrielli@arm.com{
74510850SGiacomo.Gabrielli@arm.com    int index = 0;
74610850SGiacomo.Gabrielli@arm.com
74710850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
74810850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
74910850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
75010850SGiacomo.Gabrielli@arm.com
75110850SGiacomo.Gabrielli@arm.com    std::string old_name;
75210850SGiacomo.Gabrielli@arm.com
75310850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
75410850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
75510850SGiacomo.Gabrielli@arm.com        return -EFAULT;
75610850SGiacomo.Gabrielli@arm.com
75710850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
75810850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
75910850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
76010850SGiacomo.Gabrielli@arm.com
76110850SGiacomo.Gabrielli@arm.com    std::string new_name;
76210850SGiacomo.Gabrielli@arm.com
76310850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
76410850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
76510850SGiacomo.Gabrielli@arm.com        return -EFAULT;
76610850SGiacomo.Gabrielli@arm.com
76710850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
76810850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
76910850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
77010850SGiacomo.Gabrielli@arm.com
77110850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
77210850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
77310850SGiacomo.Gabrielli@arm.com}
77410850SGiacomo.Gabrielli@arm.com
7756640Svince@csl.cornell.edu/// Target sysinfo() handler.
7766640Svince@csl.cornell.edutemplate <class OS>
7776640Svince@csl.cornell.eduSyscallReturn
7786640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7796640Svince@csl.cornell.edu         ThreadContext *tc)
7806640Svince@csl.cornell.edu{
7816640Svince@csl.cornell.edu
7826701Sgblack@eecs.umich.edu    int index = 0;
7836701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
78410793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
7856640Svince@csl.cornell.edu
78611758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
78711758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
78811758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
7896640Svince@csl.cornell.edu
7908706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
7916640Svince@csl.cornell.edu
7926701Sgblack@eecs.umich.edu    return 0;
7936640Svince@csl.cornell.edu}
794360SN/A
7951999SN/A/// Target chmod() handler.
7961999SN/Atemplate <class OS>
7971999SN/ASyscallReturn
7983114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
7992680Sktlim@umich.edu          ThreadContext *tc)
8001999SN/A{
8011999SN/A    std::string path;
8021999SN/A
8036701Sgblack@eecs.umich.edu    int index = 0;
8048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8056701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8061999SN/A        return -EFAULT;
8076701Sgblack@eecs.umich.edu    }
8081999SN/A
8096701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8101999SN/A    mode_t hostMode = 0;
8111999SN/A
8121999SN/A    // XXX translate mode flags via OS::something???
8131999SN/A    hostMode = mode;
8141999SN/A
8153669Sbinkertn@umich.edu    // Adjust path for current working directory
8163669Sbinkertn@umich.edu    path = process->fullPath(path);
8173669Sbinkertn@umich.edu
8181999SN/A    // do the chmod
8191999SN/A    int result = chmod(path.c_str(), hostMode);
8201999SN/A    if (result < 0)
8212218SN/A        return -errno;
8221999SN/A
8231999SN/A    return 0;
8241999SN/A}
8251999SN/A
8261999SN/A
8271999SN/A/// Target fchmod() handler.
8281999SN/Atemplate <class OS>
8291999SN/ASyscallReturn
8303114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
8312680Sktlim@umich.edu           ThreadContext *tc)
8321999SN/A{
8336701Sgblack@eecs.umich.edu    int index = 0;
83410931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
83510931Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
83610931Sbrandon.potter@amd.com
83710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
83810931Sbrandon.potter@amd.com    if (sim_fd < 0)
8391999SN/A        return -EBADF;
8401999SN/A
8411999SN/A    mode_t hostMode = 0;
8421999SN/A
8431999SN/A    // XXX translate mode flags via OS::someting???
8441999SN/A    hostMode = mode;
8451999SN/A
8461999SN/A    // do the fchmod
84710931Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
8481999SN/A    if (result < 0)
8492218SN/A        return -errno;
8501999SN/A
8511999SN/A    return 0;
8521999SN/A}
8531999SN/A
8545877Shsul@eecs.umich.edu/// Target mremap() handler.
8555877Shsul@eecs.umich.edutemplate <class OS>
8565877Shsul@eecs.umich.eduSyscallReturn
8575877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8585877Shsul@eecs.umich.edu{
8596701Sgblack@eecs.umich.edu    int index = 0;
8606701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8616701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8626701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8636701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
86410027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
86510027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
86610027SChris.Adeniyi-Jones@arm.com
86710027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
86810027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8695877Shsul@eecs.umich.edu
87010318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
87110318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8725877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8735877Shsul@eecs.umich.edu        return -EINVAL;
8745877Shsul@eecs.umich.edu    }
8755877Shsul@eecs.umich.edu
87610486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
87710486Stjablin@gmail.com
8785877Shsul@eecs.umich.edu    if (new_length > old_length) {
87910027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == process->mmap_end &&
88010027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8815877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
8828601Ssteve.reinhardt@amd.com            process->allocateMem(process->mmap_end, diff);
8835877Shsul@eecs.umich.edu            process->mmap_end += diff;
8845877Shsul@eecs.umich.edu            return start;
8855877Shsul@eecs.umich.edu        } else {
88610027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8875877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8885877Shsul@eecs.umich.edu                return -ENOMEM;
8895877Shsul@eecs.umich.edu            } else {
89010027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
89110027SChris.Adeniyi-Jones@arm.com                    provided_address : process->mmap_end;
89210027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
89310027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
89410027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
89510027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
8965877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
89710027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
89810027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
89910027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
90010027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
90110027SChris.Adeniyi-Jones@arm.com                    process->mmap_end += new_length;
90210027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
90310027SChris.Adeniyi-Jones@arm.com                    new_start + new_length > process->mmap_end) {
90410027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
90510027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
90610027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
90710027SChris.Adeniyi-Jones@arm.com                }
90810027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
90910027SChris.Adeniyi-Jones@arm.com                return new_start;
9105877Shsul@eecs.umich.edu            }
9115877Shsul@eecs.umich.edu        }
9125877Shsul@eecs.umich.edu    } else {
91310027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
91410027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
9158601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
91610027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
9175877Shsul@eecs.umich.edu    }
9185877Shsul@eecs.umich.edu}
9191999SN/A
920378SN/A/// Target stat() handler.
921360SN/Atemplate <class OS>
9221450SN/ASyscallReturn
9233114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
9242680Sktlim@umich.edu         ThreadContext *tc)
925360SN/A{
926360SN/A    std::string path;
927360SN/A
9286701Sgblack@eecs.umich.edu    int index = 0;
9298852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9306701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9316701Sgblack@eecs.umich.edu        return -EFAULT;
9326701Sgblack@eecs.umich.edu    }
9336701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
934360SN/A
9353669Sbinkertn@umich.edu    // Adjust path for current working directory
9363669Sbinkertn@umich.edu    path = process->fullPath(path);
9373669Sbinkertn@umich.edu
938360SN/A    struct stat hostBuf;
939360SN/A    int result = stat(path.c_str(), &hostBuf);
940360SN/A
941360SN/A    if (result < 0)
9422218SN/A        return -errno;
943360SN/A
9448706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
945360SN/A
9461458SN/A    return 0;
947360SN/A}
948360SN/A
949360SN/A
9505074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9515074Ssaidi@eecs.umich.edutemplate <class OS>
9525074Ssaidi@eecs.umich.eduSyscallReturn
9535074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9545074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9555074Ssaidi@eecs.umich.edu{
9565074Ssaidi@eecs.umich.edu    std::string path;
9575074Ssaidi@eecs.umich.edu
9586701Sgblack@eecs.umich.edu    int index = 0;
9598852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9606701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9615074Ssaidi@eecs.umich.edu        return -EFAULT;
9626701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9635074Ssaidi@eecs.umich.edu
9645074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9655074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9665074Ssaidi@eecs.umich.edu
9675208Ssaidi@eecs.umich.edu#if NO_STAT64
9685208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9695208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9705208Ssaidi@eecs.umich.edu#else
9715074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9725074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9735208Ssaidi@eecs.umich.edu#endif
9745074Ssaidi@eecs.umich.edu
9755074Ssaidi@eecs.umich.edu    if (result < 0)
9765074Ssaidi@eecs.umich.edu        return -errno;
9775074Ssaidi@eecs.umich.edu
9788706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9795074Ssaidi@eecs.umich.edu
9805074Ssaidi@eecs.umich.edu    return 0;
9815074Ssaidi@eecs.umich.edu}
9825074Ssaidi@eecs.umich.edu
9835074Ssaidi@eecs.umich.edu
98410027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
98510027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
98610027SChris.Adeniyi-Jones@arm.comSyscallReturn
98710027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
98810027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
98910027SChris.Adeniyi-Jones@arm.com{
99010027SChris.Adeniyi-Jones@arm.com    int index = 0;
99110027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
99210027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
99310793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
99410027SChris.Adeniyi-Jones@arm.com
99510027SChris.Adeniyi-Jones@arm.com    std::string path;
99610027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
99710027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
99810027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
99910027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
100010027SChris.Adeniyi-Jones@arm.com
100110027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
100210027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
100310027SChris.Adeniyi-Jones@arm.com
100410027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
100510027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
100610027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
100710027SChris.Adeniyi-Jones@arm.com#else
100810027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
100910027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
101010027SChris.Adeniyi-Jones@arm.com#endif
101110027SChris.Adeniyi-Jones@arm.com
101210027SChris.Adeniyi-Jones@arm.com    if (result < 0)
101310027SChris.Adeniyi-Jones@arm.com        return -errno;
101410027SChris.Adeniyi-Jones@arm.com
101510027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
101610027SChris.Adeniyi-Jones@arm.com
101710027SChris.Adeniyi-Jones@arm.com    return 0;
101810027SChris.Adeniyi-Jones@arm.com}
101910027SChris.Adeniyi-Jones@arm.com
102010027SChris.Adeniyi-Jones@arm.com
10211999SN/A/// Target fstat64() handler.
10221999SN/Atemplate <class OS>
10231999SN/ASyscallReturn
10243114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10252680Sktlim@umich.edu            ThreadContext *tc)
10261999SN/A{
10276701Sgblack@eecs.umich.edu    int index = 0;
102810931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
10296701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
103010931Sbrandon.potter@amd.com
103110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
103210931Sbrandon.potter@amd.com    if (sim_fd < 0)
10331999SN/A        return -EBADF;
10341999SN/A
10352764Sstever@eecs.umich.edu#if NO_STAT64
10362064SN/A    struct stat  hostBuf;
103710931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10382064SN/A#else
10392064SN/A    struct stat64  hostBuf;
104010931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10412064SN/A#endif
10421999SN/A
10431999SN/A    if (result < 0)
10442218SN/A        return -errno;
10451999SN/A
104610931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10471999SN/A
10481999SN/A    return 0;
10491999SN/A}
10501999SN/A
10511999SN/A
1052378SN/A/// Target lstat() handler.
1053360SN/Atemplate <class OS>
10541450SN/ASyscallReturn
10553114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10562680Sktlim@umich.edu          ThreadContext *tc)
1057360SN/A{
1058360SN/A    std::string path;
1059360SN/A
10606701Sgblack@eecs.umich.edu    int index = 0;
10618852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10626701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10636701Sgblack@eecs.umich.edu        return -EFAULT;
10646701Sgblack@eecs.umich.edu    }
10656701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1066360SN/A
10673669Sbinkertn@umich.edu    // Adjust path for current working directory
10683669Sbinkertn@umich.edu    path = process->fullPath(path);
10693669Sbinkertn@umich.edu
1070360SN/A    struct stat hostBuf;
1071360SN/A    int result = lstat(path.c_str(), &hostBuf);
1072360SN/A
1073360SN/A    if (result < 0)
10741458SN/A        return -errno;
1075360SN/A
10768706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1077360SN/A
10781458SN/A    return 0;
1079360SN/A}
1080360SN/A
10811999SN/A/// Target lstat64() handler.
10821999SN/Atemplate <class OS>
10831999SN/ASyscallReturn
10843114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10852680Sktlim@umich.edu            ThreadContext *tc)
10861999SN/A{
10871999SN/A    std::string path;
10881999SN/A
10896701Sgblack@eecs.umich.edu    int index = 0;
10908852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10916701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10926701Sgblack@eecs.umich.edu        return -EFAULT;
10936701Sgblack@eecs.umich.edu    }
10946701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10951999SN/A
10963669Sbinkertn@umich.edu    // Adjust path for current working directory
10973669Sbinkertn@umich.edu    path = process->fullPath(path);
10983669Sbinkertn@umich.edu
10992764Sstever@eecs.umich.edu#if NO_STAT64
11002064SN/A    struct stat hostBuf;
11012064SN/A    int result = lstat(path.c_str(), &hostBuf);
11022064SN/A#else
11031999SN/A    struct stat64 hostBuf;
11041999SN/A    int result = lstat64(path.c_str(), &hostBuf);
11052064SN/A#endif
11061999SN/A
11071999SN/A    if (result < 0)
11081999SN/A        return -errno;
11091999SN/A
11108706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11111999SN/A
11121999SN/A    return 0;
11131999SN/A}
11141999SN/A
1115378SN/A/// Target fstat() handler.
1116360SN/Atemplate <class OS>
11171450SN/ASyscallReturn
11183114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11192680Sktlim@umich.edu          ThreadContext *tc)
1120360SN/A{
11216701Sgblack@eecs.umich.edu    int index = 0;
112210931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11236701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1124360SN/A
112511380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1126360SN/A
112710932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
112810931Sbrandon.potter@amd.com    if (sim_fd < 0)
11291458SN/A        return -EBADF;
1130360SN/A
1131360SN/A    struct stat hostBuf;
113210931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1133360SN/A
1134360SN/A    if (result < 0)
11351458SN/A        return -errno;
1136360SN/A
113710931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11382021SN/A
11391458SN/A    return 0;
1140360SN/A}
1141360SN/A
1142360SN/A
11431706SN/A/// Target statfs() handler.
11441706SN/Atemplate <class OS>
11451706SN/ASyscallReturn
11463114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11472680Sktlim@umich.edu           ThreadContext *tc)
11481706SN/A{
114911799Sbrandon.potter@amd.com#if NO_STATFS
115011799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
115111799Sbrandon.potter@amd.com#else
11521706SN/A    std::string path;
11531706SN/A
11546701Sgblack@eecs.umich.edu    int index = 0;
11558852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11566701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11576701Sgblack@eecs.umich.edu        return -EFAULT;
11586701Sgblack@eecs.umich.edu    }
11596701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11601706SN/A
11613669Sbinkertn@umich.edu    // Adjust path for current working directory
11623669Sbinkertn@umich.edu    path = process->fullPath(path);
11633669Sbinkertn@umich.edu
11641706SN/A    struct statfs hostBuf;
11651706SN/A    int result = statfs(path.c_str(), &hostBuf);
11661706SN/A
11671706SN/A    if (result < 0)
11682218SN/A        return -errno;
11691706SN/A
117011759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
117111799Sbrandon.potter@amd.com#endif
11721706SN/A    return 0;
11731706SN/A}
11741706SN/A
11751706SN/A
11761706SN/A/// Target fstatfs() handler.
11771706SN/Atemplate <class OS>
11781706SN/ASyscallReturn
11793114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11802680Sktlim@umich.edu            ThreadContext *tc)
11811706SN/A{
11826701Sgblack@eecs.umich.edu    int index = 0;
118310931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
11846701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11851706SN/A
118610932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
118710931Sbrandon.potter@amd.com    if (sim_fd < 0)
11881706SN/A        return -EBADF;
11891706SN/A
11901706SN/A    struct statfs hostBuf;
119110931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11921706SN/A
11931706SN/A    if (result < 0)
11942218SN/A        return -errno;
11951706SN/A
119611759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11971706SN/A
11981706SN/A    return 0;
11991706SN/A}
12001706SN/A
12011706SN/A
12021999SN/A/// Target writev() handler.
12031999SN/Atemplate <class OS>
12041999SN/ASyscallReturn
12053114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
12062680Sktlim@umich.edu           ThreadContext *tc)
12071999SN/A{
12086701Sgblack@eecs.umich.edu    int index = 0;
120910931Sbrandon.potter@amd.com    int tgt_fd = process->getSyscallArg(tc, index);
121010931Sbrandon.potter@amd.com
121110932Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
121210931Sbrandon.potter@amd.com    if (sim_fd < 0)
12131999SN/A        return -EBADF;
12141999SN/A
12158852Sandreas.hansson@arm.com    SETranslatingPortProxy &p = tc->getMemProxy();
12166701Sgblack@eecs.umich.edu    uint64_t tiov_base = process->getSyscallArg(tc, index);
12176701Sgblack@eecs.umich.edu    size_t count = process->getSyscallArg(tc, index);
12181999SN/A    struct iovec hiov[count];
12196227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
12201999SN/A        typename OS::tgt_iovec tiov;
12212461SN/A
12228852Sandreas.hansson@arm.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12238852Sandreas.hansson@arm.com                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12248737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
12251999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
12268852Sandreas.hansson@arm.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12278852Sandreas.hansson@arm.com                   hiov[i].iov_len);
12281999SN/A    }
12291999SN/A
123010931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
12311999SN/A
12326227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
12331999SN/A        delete [] (char *)hiov[i].iov_base;
12341999SN/A
12351999SN/A    if (result < 0)
12362218SN/A        return -errno;
12371999SN/A
123810629Sjthestness@gmail.com    return result;
12391999SN/A}
12401999SN/A
124111385Sbrandon.potter@amd.com/// Real mmap handler.
1242360SN/Atemplate <class OS>
12431450SN/ASyscallReturn
124411385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
124511385Sbrandon.potter@amd.com         bool is_mmap2)
1246360SN/A{
12476701Sgblack@eecs.umich.edu    int index = 0;
12486701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
12496701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
125011383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
125111383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
12528324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
125310486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1254360SN/A
125511385Sbrandon.potter@amd.com    if (is_mmap2)
125611385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
12579008Sgblack@eecs.umich.edu
125811383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
125911383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
126011383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
126111383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
126211383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
126311383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
126411383Sbrandon.potter@amd.com        !length) {
126511383Sbrandon.potter@amd.com        return -EINVAL;
126611383Sbrandon.potter@amd.com    }
12678324Ssteve.reinhardt@amd.com
126811383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
126911383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
127011383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
127111383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
127211383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
127311383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
127411383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
127511383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
127611383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
127711383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
127811383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
127911383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
128011383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
128111383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
128211383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
128311383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
128411383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
128511383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
128611383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
128711383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
128811383Sbrandon.potter@amd.com        // than we currently maintain.
128911383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
129011383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
129111383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
12928324Ssteve.reinhardt@amd.com    }
12935877Shsul@eecs.umich.edu
129410486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
129510486Stjablin@gmail.com
129611383Sbrandon.potter@amd.com    int sim_fd = -1;
129711383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
129811383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
129911624Smichael.lebeane@amd.com        // Check for EmulatedDriver mmap
130011624Smichael.lebeane@amd.com        FDEntry *fde = p->getFDEntry(tgt_fd);
130111624Smichael.lebeane@amd.com        if (fde == NULL)
130211624Smichael.lebeane@amd.com            return -EBADF;
130311624Smichael.lebeane@amd.com
130411624Smichael.lebeane@amd.com        if (fde->driver != NULL) {
130511624Smichael.lebeane@amd.com            return fde->driver->mmap(p, tc, start, length, prot,
130611624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
130711624Smichael.lebeane@amd.com        }
130811624Smichael.lebeane@amd.com        sim_fd = fde->fd;
130911624Smichael.lebeane@amd.com
131011383Sbrandon.potter@amd.com        if (sim_fd < 0)
131111383Sbrandon.potter@amd.com            return -EBADF;
1312360SN/A
131311383Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
131411383Sbrandon.potter@amd.com                                    sim_fd, offset);
13158600Ssteve.reinhardt@amd.com
131611383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
131711383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
131811383Sbrandon.potter@amd.com            return -errno;
13198600Ssteve.reinhardt@amd.com        }
13202544SN/A    }
13212544SN/A
132211383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
132311383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
132411383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
132511386Ssteve.reinhardt@amd.com        start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end;
132611386Ssteve.reinhardt@amd.com        p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length;
132711383Sbrandon.potter@amd.com    }
132811383Sbrandon.potter@amd.com
132911383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
133011383Sbrandon.potter@amd.com                    start, start + length - 1);
133111383Sbrandon.potter@amd.com
133211383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
133311383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
133411383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
133511383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
133611383Sbrandon.potter@amd.com    if (clobber) {
133711383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
133811383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
133911383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
134011383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
134111383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
13428600Ssteve.reinhardt@amd.com        }
13436672Sgblack@eecs.umich.edu    }
13448600Ssteve.reinhardt@amd.com
134511383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
134611383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
134711383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
13488601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
13492544SN/A
135011383Sbrandon.potter@amd.com    // Transfer content into target address space.
135111383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
135211383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
135311383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
135411383Sbrandon.potter@amd.com        // with something like:
135511383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
135611383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
135711383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
135811383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
135911383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
136011383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
136111383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
136211383Sbrandon.potter@amd.com    } else {
136311383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
136411383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
136511383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
136611383Sbrandon.potter@amd.com        // the host into the target address space.
136711383Sbrandon.potter@amd.com        struct stat file_stat;
136811383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
136911383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
137011383Sbrandon.potter@amd.com
137111383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
137211383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
137311383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
137411383Sbrandon.potter@amd.com        // on the specified offset into the file.
137511383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
137611383Sbrandon.potter@amd.com                                 length);
137711383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
137811383Sbrandon.potter@amd.com
137911383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
138011383Sbrandon.potter@amd.com        munmap(pmap, length);
138111383Sbrandon.potter@amd.com
138211392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
138311392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
138411392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
138511392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
138611392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
138711392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
138811392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
138911392Sbrandon.potter@amd.com        if (interpreter) {
139011392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
139111392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
139211392Sbrandon.potter@amd.com
139311392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
139411392Sbrandon.potter@amd.com
139511392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
139611392Sbrandon.potter@amd.com                FDEntry *fde = p->getFDEntry(tgt_fd);
139711392Sbrandon.potter@amd.com
139811392Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(fde->filename);
139911392Sbrandon.potter@amd.com
140011392Sbrandon.potter@amd.com                if (lib) {
140111392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
140211392Sbrandon.potter@amd.com                                        lib->textBase(), start);
140311392Sbrandon.potter@amd.com                }
140411392Sbrandon.potter@amd.com            }
140511392Sbrandon.potter@amd.com        }
140611392Sbrandon.potter@amd.com
140711383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
140811383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
140911383Sbrandon.potter@amd.com        // execution (hopefully).
141011383Sbrandon.potter@amd.com    }
141111383Sbrandon.potter@amd.com
14121458SN/A    return start;
1413360SN/A}
1414360SN/A
141511593Santhony.gutierrez@amd.comtemplate <class OS>
141611593Santhony.gutierrez@amd.comSyscallReturn
141711593Santhony.gutierrez@amd.compwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
141811593Santhony.gutierrez@amd.com{
141911593Santhony.gutierrez@amd.com    int index = 0;
142011593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
142111593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
142211593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
142311593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
142411593Santhony.gutierrez@amd.com
142511593Santhony.gutierrez@amd.com    int sim_fd = p->getSimFD(tgt_fd);
142611593Santhony.gutierrez@amd.com    if (sim_fd < 0)
142711593Santhony.gutierrez@amd.com        return -EBADF;
142811593Santhony.gutierrez@amd.com
142911593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
143011593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
143111593Santhony.gutierrez@amd.com
143211594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
143311593Santhony.gutierrez@amd.com
143411593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
143511593Santhony.gutierrez@amd.com}
143611593Santhony.gutierrez@amd.com
143711385Sbrandon.potter@amd.com/// Target mmap() handler.
143811385Sbrandon.potter@amd.comtemplate <class OS>
143911385Sbrandon.potter@amd.comSyscallReturn
144011385Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
144111385Sbrandon.potter@amd.com{
144211385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
144311385Sbrandon.potter@amd.com}
144411385Sbrandon.potter@amd.com
144511385Sbrandon.potter@amd.com/// Target mmap2() handler.
144611385Sbrandon.potter@amd.comtemplate <class OS>
144711385Sbrandon.potter@amd.comSyscallReturn
144811385Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
144911385Sbrandon.potter@amd.com{
145011385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
145111385Sbrandon.potter@amd.com}
145211385Sbrandon.potter@amd.com
1453378SN/A/// Target getrlimit() handler.
1454360SN/Atemplate <class OS>
14551450SN/ASyscallReturn
14563114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14572680Sktlim@umich.edu        ThreadContext *tc)
1458360SN/A{
14596701Sgblack@eecs.umich.edu    int index = 0;
14606701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
14616701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1462360SN/A
1463360SN/A    switch (resource) {
14642064SN/A        case OS::TGT_RLIMIT_STACK:
14655877Shsul@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
14662064SN/A            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
14678737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14688737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14692064SN/A            break;
1470360SN/A
14715877Shsul@eecs.umich.edu        case OS::TGT_RLIMIT_DATA:
14725877Shsul@eecs.umich.edu            // max data segment size in bytes: make up a number
14735877Shsul@eecs.umich.edu            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
14748737Skoansin.tan@gmail.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14758737Skoansin.tan@gmail.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
14765877Shsul@eecs.umich.edu            break;
14775877Shsul@eecs.umich.edu
14782064SN/A        default:
147910794Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
148010794Sbrandon.potter@amd.com            return -EINVAL;
14812064SN/A            break;
1482360SN/A    }
1483360SN/A
14848706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
14851458SN/A    return 0;
1486360SN/A}
1487360SN/A
148810796Sbrandon.potter@amd.com/// Target clock_gettime() function.
148910796Sbrandon.potter@amd.comtemplate <class OS>
149010796Sbrandon.potter@amd.comSyscallReturn
149110796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
149210796Sbrandon.potter@amd.com{
149310796Sbrandon.potter@amd.com    int index = 1;
149410796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
149510796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
149610796Sbrandon.potter@amd.com
149710796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
149810796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
149910796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
150010796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
150110796Sbrandon.potter@amd.com
150210796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
150310796Sbrandon.potter@amd.com
150410796Sbrandon.potter@amd.com    return 0;
150510796Sbrandon.potter@amd.com}
150610796Sbrandon.potter@amd.com
150711337SMichael.Lebeane@amd.com/// Target clock_getres() function.
150811337SMichael.Lebeane@amd.comtemplate <class OS>
150911337SMichael.Lebeane@amd.comSyscallReturn
151011337SMichael.Lebeane@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
151111337SMichael.Lebeane@amd.com{
151211337SMichael.Lebeane@amd.com    int index = 1;
151311337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
151411337SMichael.Lebeane@amd.com
151511337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
151611337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
151711337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
151811337SMichael.Lebeane@amd.com
151911337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
152011337SMichael.Lebeane@amd.com
152111337SMichael.Lebeane@amd.com    return 0;
152211337SMichael.Lebeane@amd.com}
152311337SMichael.Lebeane@amd.com
1524378SN/A/// Target gettimeofday() handler.
1525360SN/Atemplate <class OS>
15261450SN/ASyscallReturn
15273114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15282680Sktlim@umich.edu        ThreadContext *tc)
1529360SN/A{
15306701Sgblack@eecs.umich.edu    int index = 0;
15316701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1532360SN/A
153310796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1534360SN/A    tp->tv_sec += seconds_since_epoch;
15356109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
15366109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1537360SN/A
15388706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1539360SN/A
15401458SN/A    return 0;
1541360SN/A}
1542360SN/A
1543360SN/A
15441999SN/A/// Target utimes() handler.
15451999SN/Atemplate <class OS>
15461999SN/ASyscallReturn
15473114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15482680Sktlim@umich.edu           ThreadContext *tc)
15491999SN/A{
15501999SN/A    std::string path;
15511999SN/A
15526701Sgblack@eecs.umich.edu    int index = 0;
15538852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
15546701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
15556701Sgblack@eecs.umich.edu        return -EFAULT;
15566701Sgblack@eecs.umich.edu    }
15571999SN/A
15586701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
15596701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
15608706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
15611999SN/A
15621999SN/A    struct timeval hostTimeval[2];
15631999SN/A    for (int i = 0; i < 2; ++i)
15641999SN/A    {
15658737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
15668737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
15671999SN/A    }
15683669Sbinkertn@umich.edu
15693669Sbinkertn@umich.edu    // Adjust path for current working directory
15703669Sbinkertn@umich.edu    path = process->fullPath(path);
15713669Sbinkertn@umich.edu
15721999SN/A    int result = utimes(path.c_str(), hostTimeval);
15731999SN/A
15741999SN/A    if (result < 0)
15751999SN/A        return -errno;
15761999SN/A
15771999SN/A    return 0;
15781999SN/A}
1579378SN/A/// Target getrusage() function.
1580360SN/Atemplate <class OS>
15811450SN/ASyscallReturn
15823114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15832680Sktlim@umich.edu              ThreadContext *tc)
1584360SN/A{
15856701Sgblack@eecs.umich.edu    int index = 0;
15866701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
15876701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1588360SN/A
15893670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
15903670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1591360SN/A    rup->ru_stime.tv_sec = 0;
1592360SN/A    rup->ru_stime.tv_usec = 0;
1593360SN/A    rup->ru_maxrss = 0;
1594360SN/A    rup->ru_ixrss = 0;
1595360SN/A    rup->ru_idrss = 0;
1596360SN/A    rup->ru_isrss = 0;
1597360SN/A    rup->ru_minflt = 0;
1598360SN/A    rup->ru_majflt = 0;
1599360SN/A    rup->ru_nswap = 0;
1600360SN/A    rup->ru_inblock = 0;
1601360SN/A    rup->ru_oublock = 0;
1602360SN/A    rup->ru_msgsnd = 0;
1603360SN/A    rup->ru_msgrcv = 0;
1604360SN/A    rup->ru_nsignals = 0;
1605360SN/A    rup->ru_nvcsw = 0;
1606360SN/A    rup->ru_nivcsw = 0;
1607360SN/A
16083670Sbinkertn@umich.edu    switch (who) {
16093670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
161010796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
16118737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
16128737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
16133670Sbinkertn@umich.edu        break;
16143670Sbinkertn@umich.edu
16153670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
16163670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
16173670Sbinkertn@umich.edu        break;
16183670Sbinkertn@umich.edu
16193670Sbinkertn@umich.edu      default:
16203670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
16213670Sbinkertn@umich.edu        // plow ahead
16223670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
16233670Sbinkertn@umich.edu             who);
16243670Sbinkertn@umich.edu    }
16253670Sbinkertn@umich.edu
16268706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1627360SN/A
16281458SN/A    return 0;
1629360SN/A}
1630360SN/A
16316683Stjones1@inf.ed.ac.uk/// Target times() function.
16326683Stjones1@inf.ed.ac.uktemplate <class OS>
16336683Stjones1@inf.ed.ac.ukSyscallReturn
16346683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
16356683Stjones1@inf.ed.ac.uk           ThreadContext *tc)
16366683Stjones1@inf.ed.ac.uk{
16376701Sgblack@eecs.umich.edu    int index = 0;
16386701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
16396683Stjones1@inf.ed.ac.uk
16406683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
16417823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
16426683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
16436683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
16446683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
16456683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
16466683Stjones1@inf.ed.ac.uk
16476683Stjones1@inf.ed.ac.uk    // Convert to host endianness
16488737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
16496683Stjones1@inf.ed.ac.uk
16506683Stjones1@inf.ed.ac.uk    // Write back
16518706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
16526683Stjones1@inf.ed.ac.uk
16536683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
16546683Stjones1@inf.ed.ac.uk    return clocks;
16556683Stjones1@inf.ed.ac.uk}
16562553SN/A
16576684Stjones1@inf.ed.ac.uk/// Target time() function.
16586684Stjones1@inf.ed.ac.uktemplate <class OS>
16596684Stjones1@inf.ed.ac.ukSyscallReturn
16606684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
16616684Stjones1@inf.ed.ac.uk           ThreadContext *tc)
16626684Stjones1@inf.ed.ac.uk{
16636684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
166410796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
16656684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
16666684Stjones1@inf.ed.ac.uk
16676701Sgblack@eecs.umich.edu    int index = 0;
16686701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
166911321Ssteve.reinhardt@amd.com    if (taddr != 0) {
16706684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
16718737Skoansin.tan@gmail.com        t = TheISA::htog(t);
16728852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
16738852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
16746684Stjones1@inf.ed.ac.uk    }
16756684Stjones1@inf.ed.ac.uk    return sec;
16766684Stjones1@inf.ed.ac.uk}
16772553SN/A
16782553SN/A
16791354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1680