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
4812018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
4912018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__CYGWIN__) ||     \
5012018Sandreas.sandberg@arm.com     defined(__NetBSD__))
5112018Sandreas.sandberg@arm.com#define NO_STAT64 1
5212018Sandreas.sandberg@arm.com#else
5312018Sandreas.sandberg@arm.com#define NO_STAT64 0
5412018Sandreas.sandberg@arm.com#endif
552064SN/A
56360SN/A///
57360SN/A/// @file syscall_emul.hh
58360SN/A///
59360SN/A/// This file defines objects used to emulate syscalls from the target
60360SN/A/// application on the host machine.
61360SN/A
6213936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
6313933Sbrandon.potter@amd.com#include <sys/eventfd.h>
6413933Sbrandon.potter@amd.com#include <sys/statfs.h>
6513933Sbrandon.potter@amd.com
6613936SAndrea.Mondelli@ucf.edu#else
6713936SAndrea.Mondelli@ucf.edu#include <sys/mount.h>
6813936SAndrea.Mondelli@ucf.edu
6913933Sbrandon.potter@amd.com#endif
7013933Sbrandon.potter@amd.com
711809SN/A#ifdef __CYGWIN32__
7211800Sbrandon.potter@amd.com#include <sys/fcntl.h>
7311392Sbrandon.potter@amd.com
741809SN/A#endif
7511392Sbrandon.potter@amd.com#include <fcntl.h>
7613902Sbrandon.potter@amd.com#include <net/if.h>
7713570Sbrandon.potter@amd.com#include <poll.h>
7813902Sbrandon.potter@amd.com#include <sys/ioctl.h>
7911383Sbrandon.potter@amd.com#include <sys/mman.h>
8013568Sbrandon.potter@amd.com#include <sys/socket.h>
813113Sgblack@eecs.umich.edu#include <sys/stat.h>
828229Snate@binkert.org#include <sys/time.h>
8313570Sbrandon.potter@amd.com#include <sys/types.h>
848229Snate@binkert.org#include <sys/uio.h>
8511594Santhony.gutierrez@amd.com#include <unistd.h>
867075Snate@binkert.org
878229Snate@binkert.org#include <cerrno>
8811856Sbrandon.potter@amd.com#include <memory>
897075Snate@binkert.org#include <string>
90360SN/A
9112461Sgabeblack@google.com#include "arch/generic/tlb.hh"
9211886Sbrandon.potter@amd.com#include "arch/utility.hh"
9311800Sbrandon.potter@amd.com#include "base/intmath.hh"
9411392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
9512334Sgabeblack@google.com#include "base/logging.hh"
961354SN/A#include "base/trace.hh"
976216Snate@binkert.org#include "base/types.hh"
986658Snate@binkert.org#include "config/the_isa.hh"
992474SN/A#include "cpu/base.hh"
1002680Sktlim@umich.edu#include "cpu/thread_context.hh"
1018229Snate@binkert.org#include "mem/page_table.hh"
10211886Sbrandon.potter@amd.com#include "params/Process.hh"
10310496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
10411911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1058229Snate@binkert.org#include "sim/process.hh"
10611794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
10711886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
10810497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
10911794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
110360SN/A
11113629SAndrea.Mondelli@ucf.edu#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN)
11213629SAndrea.Mondelli@ucf.edu#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
11313629SAndrea.Mondelli@ucf.edu#endif
11413629SAndrea.Mondelli@ucf.edu
115360SN/A//////////////////////////////////////////////////////////////////////
116360SN/A//
117360SN/A// The following emulation functions are generic enough that they
118360SN/A// don't need to be recompiled for different emulated OS's.  They are
119360SN/A// defined in sim/syscall_emul.cc.
120360SN/A//
121360SN/A//////////////////////////////////////////////////////////////////////
122360SN/A
12313933Sbrandon.potter@amd.comvoid warnUnsupportedOS(std::string syscall_name);
124360SN/A
125378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
12613995Sbrandon.potter@amd.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, ThreadContext *tc);
127378SN/A
128378SN/A/// Handler for unimplemented syscalls that we never intend to
129378SN/A/// implement (signal handling, etc.) and should not affect the correct
130378SN/A/// behavior of the program.  Print a warning only if the appropriate
131378SN/A/// trace flag is enabled.  Return success to the target program.
13213995Sbrandon.potter@amd.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num, ThreadContext *tc);
133360SN/A
13411760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
13513995Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, ThreadContext *tc);
13611760Sbrandon.potter@amd.com
1376109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
13813995Sbrandon.potter@amd.comSyscallReturn exitFunc(SyscallDesc *desc, int num, ThreadContext *tc);
139378SN/A
1406109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
14113995Sbrandon.potter@amd.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, ThreadContext *tc);
1426109Ssanchezd@stanford.edu
14311886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
14413995Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, ThreadContext *tc);
14511886Sbrandon.potter@amd.com
146378SN/A/// Target getpagesize() handler.
14713995Sbrandon.potter@amd.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, ThreadContext *tc);
148378SN/A
1495748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
15013995Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
151378SN/A
152378SN/A/// Target close() handler.
15313995Sbrandon.potter@amd.comSyscallReturn closeFunc(SyscallDesc *desc, int num, ThreadContext *tc);
154378SN/A
155378SN/A/// Target lseek() handler.
15613995Sbrandon.potter@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, ThreadContext *tc);
157378SN/A
1584118Sgblack@eecs.umich.edu/// Target _llseek() handler.
15913995Sbrandon.potter@amd.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc);
1604118Sgblack@eecs.umich.edu
161378SN/A/// Target munmap() handler.
16213995Sbrandon.potter@amd.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, ThreadContext *tc);
163378SN/A
16413568Sbrandon.potter@amd.com/// Target shutdown() handler.
16513995Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num, ThreadContext *tc);
16613568Sbrandon.potter@amd.com
167378SN/A/// Target gethostname() handler.
16813995Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
169360SN/A
1705513SMichael.Adler@intel.com/// Target getcwd() handler.
17113995Sbrandon.potter@amd.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, ThreadContext *tc);
1725513SMichael.Adler@intel.com
17310203SAli.Saidi@ARM.com/// Target readlink() handler.
17413995Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc,
17510203SAli.Saidi@ARM.com                           int index = 0);
17613995Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
1775513SMichael.Adler@intel.com
178511SN/A/// Target unlink() handler.
17913995Sbrandon.potter@amd.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, ThreadContext *tc,
18010633Smichaelupton@gmail.com                           int index);
18113995Sbrandon.potter@amd.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
182511SN/A
18312795Smattdsinclair@gmail.com/// Target link() handler
18413995Sbrandon.potter@amd.comSyscallReturn linkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
18512795Smattdsinclair@gmail.com
18612796Smattdsinclair@gmail.com/// Target symlink() handler.
18713995Sbrandon.potter@amd.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc);
18812796Smattdsinclair@gmail.com
1895513SMichael.Adler@intel.com/// Target mkdir() handler.
19013995Sbrandon.potter@amd.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, ThreadContext *tc);
1915513SMichael.Adler@intel.com
19213031Sbrandon.potter@amd.com/// Target mknod() handler.
19313995Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, ThreadContext *tc);
19413031Sbrandon.potter@amd.com
19513031Sbrandon.potter@amd.com/// Target chdir() handler.
19613995Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num, ThreadContext *tc);
19713031Sbrandon.potter@amd.com
19813031Sbrandon.potter@amd.com// Target rmdir() handler.
19913995Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num, ThreadContext *tc);
20013031Sbrandon.potter@amd.com
201511SN/A/// Target rename() handler.
20213995Sbrandon.potter@amd.comSyscallReturn renameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2031706SN/A
2041706SN/A
2051706SN/A/// Target truncate() handler.
20613995Sbrandon.potter@amd.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2071706SN/A
2081706SN/A
2091706SN/A/// Target ftruncate() handler.
21013995Sbrandon.potter@amd.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2111706SN/A
212511SN/A
2136703Svince@csl.cornell.edu/// Target truncate64() handler.
21413995Sbrandon.potter@amd.comSyscallReturn truncate64Func(SyscallDesc *desc, int num, ThreadContext *tc);
2156703Svince@csl.cornell.edu
2166685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
21713995Sbrandon.potter@amd.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc);
2186685Stjones1@inf.ed.ac.uk
2196685Stjones1@inf.ed.ac.uk
2205513SMichael.Adler@intel.com/// Target umask() handler.
22113995Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2225513SMichael.Adler@intel.com
22311885Sbrandon.potter@amd.com/// Target gettid() handler.
22413995Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2255513SMichael.Adler@intel.com
2261999SN/A/// Target chown() handler.
22713995Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2281999SN/A
22914130Sbrandon.potter@amd.com/// Target getpgrpFunc() handler.
23014130Sbrandon.potter@amd.comSyscallReturn getpgrpFunc(SyscallDesc *desc, int num, ThreadContext *tc);
23114130Sbrandon.potter@amd.com
23211885Sbrandon.potter@amd.com/// Target setpgid() handler.
23313995Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2341999SN/A
2351999SN/A/// Target fchown() handler.
23613995Sbrandon.potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2371999SN/A
2383079Sstever@eecs.umich.edu/// Target dup() handler.
23913995Sbrandon.potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2403079Sstever@eecs.umich.edu
24111908SBrandon.Potter@amd.com/// Target dup2() handler.
24213995Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, ThreadContext *tc);
24311908SBrandon.Potter@amd.com
24411875Sbrandon.potter@amd.com/// Target fcntl() handler.
24513995Sbrandon.potter@amd.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2462093SN/A
2472687Sksewell@umich.edu/// Target fcntl64() handler.
24813995Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, ThreadContext *tc);
2492687Sksewell@umich.edu
2502238SN/A/// Target setuid() handler.
25113995Sbrandon.potter@amd.comSyscallReturn setuidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2522238SN/A
25311908SBrandon.Potter@amd.com/// Target pipe() handler.
25413995Sbrandon.potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, ThreadContext *tc);
25511908SBrandon.Potter@amd.com
25611908SBrandon.Potter@amd.com/// Internal pipe() handler.
25713995Sbrandon.potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, ThreadContext *tc,
25814129Smatthew.sinclair@amd.com                       bool pseudo_pipe, bool is_pipe2=false);
25914129Smatthew.sinclair@amd.com
26014129Smatthew.sinclair@amd.com/// Target pipe() handler.
26114129Smatthew.sinclair@amd.comSyscallReturn pipe2Func(SyscallDesc *desc, int num, ThreadContext *tc);
26211908SBrandon.Potter@amd.com
2632238SN/A/// Target getpid() handler.
26413995Sbrandon.potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
2652238SN/A
26613571Sbrandon.potter@amd.com// Target getpeername() handler.
26713995Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
26813571Sbrandon.potter@amd.com
26913568Sbrandon.potter@amd.com// Target bind() handler.
27013995Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num, ThreadContext *tc);
27113568Sbrandon.potter@amd.com
27213568Sbrandon.potter@amd.com// Target listen() handler.
27313995Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num, ThreadContext *tc);
27413568Sbrandon.potter@amd.com
27513568Sbrandon.potter@amd.com// Target connect() handler.
27613995Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num, ThreadContext *tc);
27713568Sbrandon.potter@amd.com
27813448Sciro.santilli@arm.com#if defined(SYS_getdents)
27913031Sbrandon.potter@amd.com// Target getdents() handler.
28013995Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num, ThreadContext *tc);
28113448Sciro.santilli@arm.com#endif
28213031Sbrandon.potter@amd.com
28313539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
28413539Sjavier.setoain@arm.com// Target getdents() handler.
28513995Sbrandon.potter@amd.comSyscallReturn getdents64Func(SyscallDesc *desc, int num, ThreadContext *tc);
28613539Sjavier.setoain@arm.com#endif
28713539Sjavier.setoain@arm.com
28813569Sbrandon.potter@amd.com// Target sendto() handler.
28913995Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
29013569Sbrandon.potter@amd.com
29113569Sbrandon.potter@amd.com// Target recvfrom() handler.
29213995Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num, ThreadContext *tc);
29313569Sbrandon.potter@amd.com
29413569Sbrandon.potter@amd.com// Target recvmsg() handler.
29513995Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc);
29613569Sbrandon.potter@amd.com
29713569Sbrandon.potter@amd.com// Target sendmsg() handler.
29813995Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc);
29913569Sbrandon.potter@amd.com
30013031Sbrandon.potter@amd.com// Target getuid() handler.
30113995Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
3022238SN/A
3032238SN/A/// Target getgid() handler.
30413995Sbrandon.potter@amd.comSyscallReturn getgidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
3052238SN/A
3062238SN/A/// Target getppid() handler.
30713995Sbrandon.potter@amd.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
3082238SN/A
3092238SN/A/// Target geteuid() handler.
31013995Sbrandon.potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
3112238SN/A
3122238SN/A/// Target getegid() handler.
31313995Sbrandon.potter@amd.comSyscallReturn getegidFunc(SyscallDesc *desc, int num, ThreadContext *tc);
3142238SN/A
3159455Smitch.hayenga+gem5@gmail.com/// Target access() handler
31613995Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc);
31713995Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc,
31811851Sbrandon.potter@amd.com                         int index);
3199455Smitch.hayenga+gem5@gmail.com
32013571Sbrandon.potter@amd.com// Target getsockopt() handler.
32113995Sbrandon.potter@amd.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc);
32213571Sbrandon.potter@amd.com
32313571Sbrandon.potter@amd.com// Target setsockopt() handler.
32413995Sbrandon.potter@amd.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc);
32513571Sbrandon.potter@amd.com
32613571Sbrandon.potter@amd.com// Target getsockname() handler.
32713995Sbrandon.potter@amd.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num, ThreadContext *tc);
32813571Sbrandon.potter@amd.com
3299112Smarc.orr@gmail.com/// Futex system call
33011906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
33111906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3329112Smarc.orr@gmail.comtemplate <class OS>
3339112Smarc.orr@gmail.comSyscallReturn
33413995Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
3359112Smarc.orr@gmail.com{
33611911SBrandon.Potter@amd.com    using namespace std;
3379112Smarc.orr@gmail.com
33811911SBrandon.Potter@amd.com    int index = 0;
33913995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
34013995Sbrandon.potter@amd.com
34111911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
34211911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
34311911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
34413642Sqtt2@cornell.edu    int timeout M5_VAR_USED = process->getSyscallArg(tc, index);
34513642Sqtt2@cornell.edu    Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index);
34613642Sqtt2@cornell.edu    int val3 = process->getSyscallArg(tc, index);
3479112Smarc.orr@gmail.com
34811911SBrandon.Potter@amd.com    /*
34911911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
35011911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
35111911SBrandon.Potter@amd.com     */
3529238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
35313642Sqtt2@cornell.edu    op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG;
3549112Smarc.orr@gmail.com
35511911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
3569112Smarc.orr@gmail.com
35713642Sqtt2@cornell.edu    if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) {
35811911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
35911911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
36014024Sgabeblack@google.com        buf.copyIn(tc->getVirtProxy());
36111911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
3629112Smarc.orr@gmail.com
36311911SBrandon.Potter@amd.com        /*
36411911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
36511911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
36611911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
36711911SBrandon.Potter@amd.com         */
36811911SBrandon.Potter@amd.com        if (val != mem_val)
3699112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3709112Smarc.orr@gmail.com
37113642Sqtt2@cornell.edu        if (OS::TGT_FUTEX_WAIT) {
37213642Sqtt2@cornell.edu            futex_map.suspend(uaddr, process->tgid(), tc);
37313642Sqtt2@cornell.edu        } else {
37413642Sqtt2@cornell.edu            futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3);
37513642Sqtt2@cornell.edu        }
37611911SBrandon.Potter@amd.com
3779112Smarc.orr@gmail.com        return 0;
37811911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
37911911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
38013642Sqtt2@cornell.edu    } else if (OS::TGT_FUTEX_WAKE_BITSET == op) {
38113642Sqtt2@cornell.edu        return futex_map.wakeup_bitset(uaddr, process->tgid(), val3);
38213650Smw828@cornell.edu    } else if (OS::TGT_FUTEX_REQUEUE == op ||
38313650Smw828@cornell.edu               OS::TGT_FUTEX_CMP_REQUEUE == op) {
38413650Smw828@cornell.edu
38513650Smw828@cornell.edu        // Ensure futex system call accessed atomically.
38613650Smw828@cornell.edu        BufferArg buf(uaddr, sizeof(int));
38714024Sgabeblack@google.com        buf.copyIn(tc->getVirtProxy());
38813650Smw828@cornell.edu        int mem_val = *(int*)buf.bufferPtr();
38913650Smw828@cornell.edu        /*
39013650Smw828@cornell.edu         * For CMP_REQUEUE, the whole operation is only started only if
39113650Smw828@cornell.edu         * val3 is still the value of the futex pointed to by uaddr.
39213650Smw828@cornell.edu         */
39313650Smw828@cornell.edu        if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val)
39413650Smw828@cornell.edu            return -OS::TGT_EWOULDBLOCK;
39513650Smw828@cornell.edu        return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2);
39613651Smw828@cornell.edu    } else if (OS::TGT_FUTEX_WAKE_OP == op) {
39713651Smw828@cornell.edu        /*
39813651Smw828@cornell.edu         * The FUTEX_WAKE_OP operation is equivalent to executing the
39913651Smw828@cornell.edu         * following code atomically and totally ordered with respect to
40013651Smw828@cornell.edu         * other futex operations on any of the two supplied futex words:
40113651Smw828@cornell.edu         *
40213651Smw828@cornell.edu         *   int oldval = *(int *) addr2;
40313651Smw828@cornell.edu         *   *(int *) addr2 = oldval op oparg;
40413651Smw828@cornell.edu         *   futex(addr1, FUTEX_WAKE, val, 0, 0, 0);
40513651Smw828@cornell.edu         *   if (oldval cmp cmparg)
40613651Smw828@cornell.edu         *        futex(addr2, FUTEX_WAKE, val2, 0, 0, 0);
40713651Smw828@cornell.edu         *
40813651Smw828@cornell.edu         * (op, oparg, cmp, cmparg are encoded in val3)
40913651Smw828@cornell.edu         *
41013651Smw828@cornell.edu         * +---+---+-----------+-----------+
41113651Smw828@cornell.edu         * |op |cmp|   oparg   |  cmparg   |
41213651Smw828@cornell.edu         * +---+---+-----------+-----------+
41313651Smw828@cornell.edu         *   4   4       12          12    <== # of bits
41413651Smw828@cornell.edu         *
41513651Smw828@cornell.edu         * reference: http://man7.org/linux/man-pages/man2/futex.2.html
41613651Smw828@cornell.edu         *
41713651Smw828@cornell.edu         */
41813651Smw828@cornell.edu        // get value from simulated-space
41913651Smw828@cornell.edu        BufferArg buf(uaddr2, sizeof(int));
42014024Sgabeblack@google.com        buf.copyIn(tc->getVirtProxy());
42113651Smw828@cornell.edu        int oldval = *(int*)buf.bufferPtr();
42213651Smw828@cornell.edu        int newval = oldval;
42313651Smw828@cornell.edu        // extract op, oparg, cmp, cmparg from val3
42413651Smw828@cornell.edu        int wake_cmparg =  val3 & 0xfff;
42513651Smw828@cornell.edu        int wake_oparg  = (val3 & 0xfff000)   >> 12;
42613651Smw828@cornell.edu        int wake_cmp    = (val3 & 0xf000000)  >> 24;
42713651Smw828@cornell.edu        int wake_op     = (val3 & 0xf0000000) >> 28;
42813651Smw828@cornell.edu        if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1)
42913651Smw828@cornell.edu            wake_oparg = (1 << wake_oparg);
43013651Smw828@cornell.edu        wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT;
43113651Smw828@cornell.edu        // perform operation on the value of the second futex
43213651Smw828@cornell.edu        if (wake_op == OS::TGT_FUTEX_OP_SET)
43313651Smw828@cornell.edu            newval = wake_oparg;
43413651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_ADD)
43513651Smw828@cornell.edu            newval += wake_oparg;
43613651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_OR)
43713651Smw828@cornell.edu            newval |= wake_oparg;
43813651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_ANDN)
43913651Smw828@cornell.edu            newval &= ~wake_oparg;
44013651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_XOR)
44113651Smw828@cornell.edu            newval ^= wake_oparg;
44213651Smw828@cornell.edu        // copy updated value back to simulated-space
44313651Smw828@cornell.edu        *(int*)buf.bufferPtr() = newval;
44414024Sgabeblack@google.com        buf.copyOut(tc->getVirtProxy());
44513651Smw828@cornell.edu        // perform the first wake-up
44613651Smw828@cornell.edu        int woken1 = futex_map.wakeup(uaddr, process->tgid(), val);
44713651Smw828@cornell.edu        int woken2 = 0;
44813651Smw828@cornell.edu        // calculate the condition of the second wake-up
44913651Smw828@cornell.edu        bool is_wake2 = false;
45013651Smw828@cornell.edu        if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ)
45113651Smw828@cornell.edu            is_wake2 = oldval == wake_cmparg;
45213651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE)
45313651Smw828@cornell.edu            is_wake2 = oldval != wake_cmparg;
45413651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT)
45513651Smw828@cornell.edu            is_wake2 = oldval < wake_cmparg;
45613651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE)
45713651Smw828@cornell.edu            is_wake2 = oldval <= wake_cmparg;
45813651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT)
45913651Smw828@cornell.edu            is_wake2 = oldval > wake_cmparg;
46013651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE)
46113651Smw828@cornell.edu            is_wake2 = oldval >= wake_cmparg;
46213651Smw828@cornell.edu        // perform the second wake-up
46313651Smw828@cornell.edu        if (is_wake2)
46413651Smw828@cornell.edu            woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout);
46513651Smw828@cornell.edu
46613651Smw828@cornell.edu        return woken1 + woken2;
4679112Smarc.orr@gmail.com    }
46811911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
46911911SBrandon.Potter@amd.com    return -ENOSYS;
4709112Smarc.orr@gmail.com}
4719112Smarc.orr@gmail.com
4722238SN/A
4732238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4742238SN/A/// returning a second value in a register other than the normal return register
47513995Sbrandon.potter@amd.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
4762238SN/A
4772238SN/A/// Target getpidPseudo() handler.
47813995Sbrandon.potter@amd.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
4792238SN/A
4802238SN/A/// Target getuidPseudo() handler.
48113995Sbrandon.potter@amd.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
4822238SN/A
4832238SN/A/// Target getgidPseudo() handler.
48413995Sbrandon.potter@amd.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc);
4852238SN/A
4862238SN/A
4871354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4881354SN/Aconst int one_million = 1000000;
48910796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
49010796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4911354SN/A
4921354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4931354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4941354SN/A/// real-world time) to keep simulations repeatable.
4951354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4961354SN/A
4971354SN/A/// Helper function to convert current elapsed time to seconds and
4981354SN/A/// microseconds.
4991354SN/Atemplate <class T1, class T2>
5001354SN/Avoid
50110796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
5021354SN/A{
50310796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
5041354SN/A    sec = elapsed_usecs / one_million;
5051354SN/A    usec = elapsed_usecs % one_million;
5061354SN/A}
5071354SN/A
50810796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
50910796Sbrandon.potter@amd.com/// nanoseconds.
51010796Sbrandon.potter@amd.comtemplate <class T1, class T2>
51110796Sbrandon.potter@amd.comvoid
51210796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
51310796Sbrandon.potter@amd.com{
51410796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
51510796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
51610796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
51710796Sbrandon.potter@amd.com}
51810796Sbrandon.potter@amd.com
519360SN/A//////////////////////////////////////////////////////////////////////
520360SN/A//
521360SN/A// The following emulation functions are generic, but need to be
522360SN/A// templated to account for differences in types, constants, etc.
523360SN/A//
524360SN/A//////////////////////////////////////////////////////////////////////
525360SN/A
52611759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
5273113Sgblack@eecs.umich.edu#if NO_STAT64
5283113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5293113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
5303113Sgblack@eecs.umich.edu#else
5313113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5323113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
5333113Sgblack@eecs.umich.edu#endif
5343113Sgblack@eecs.umich.edu
5353113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
5363113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
5373113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
5383113Sgblack@eecs.umich.edu
5393113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
54012032Sandreas.sandberg@arm.comvoid
5413113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
5423113Sgblack@eecs.umich.edu{
5434189Sgblack@eecs.umich.edu    using namespace TheISA;
5444189Sgblack@eecs.umich.edu
5453113Sgblack@eecs.umich.edu    if (fakeTTY)
5463113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5473113Sgblack@eecs.umich.edu    else
5483113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5498737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5503113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5518737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5523277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5535515SMichael.Adler@intel.com    if (fakeTTY) {
5545515SMichael.Adler@intel.com        // Claim to be a character device
5555515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5565515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5575515SMichael.Adler@intel.com    }
5588737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5593277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5608737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5613277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5628737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5633277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5648737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5653113Sgblack@eecs.umich.edu    if (fakeTTY)
5663113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5673113Sgblack@eecs.umich.edu    else
5683113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5698737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5703113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5718737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5723114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5738737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5743114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5758737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5763114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5778737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
57811906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
5794061Sgblack@eecs.umich.edu    // consistently across different hosts.
5804061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5818737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5823113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5838737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5843113Sgblack@eecs.umich.edu}
5853113Sgblack@eecs.umich.edu
5863113Sgblack@eecs.umich.edu// Same for stat64
5873113Sgblack@eecs.umich.edu
5883113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
58912032Sandreas.sandberg@arm.comvoid
5903113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5913113Sgblack@eecs.umich.edu{
5924189Sgblack@eecs.umich.edu    using namespace TheISA;
5934189Sgblack@eecs.umich.edu
5943113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5953113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5963113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5978737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5983113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5998737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
6003113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
6018737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
6023113Sgblack@eecs.umich.edu#else
6033113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
6043113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
6053113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
6063113Sgblack@eecs.umich.edu#endif
6073113Sgblack@eecs.umich.edu}
6083113Sgblack@eecs.umich.edu
60911906SBrandon.Potter@amd.com// Here are a couple of convenience functions
6103113Sgblack@eecs.umich.edutemplate<class OS>
61112032Sandreas.sandberg@arm.comvoid
61214020Sgabeblack@google.comcopyOutStatBuf(PortProxy &mem, Addr addr,
61311906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
6143113Sgblack@eecs.umich.edu{
6153113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
6163113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
6173113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
6183113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
6193113Sgblack@eecs.umich.edu}
6203113Sgblack@eecs.umich.edu
6213113Sgblack@eecs.umich.edutemplate<class OS>
62212032Sandreas.sandberg@arm.comvoid
62314020Sgabeblack@google.comcopyOutStat64Buf(PortProxy &mem, Addr addr,
62411906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
6253113Sgblack@eecs.umich.edu{
6263113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
6273113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
6286686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
6293113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
6303113Sgblack@eecs.umich.edu}
6313113Sgblack@eecs.umich.edu
63211759Sbrandon.potter@amd.comtemplate <class OS>
63312032Sandreas.sandberg@arm.comvoid
63414020Sgabeblack@google.comcopyOutStatfsBuf(PortProxy &mem, Addr addr,
63511759Sbrandon.potter@amd.com                 hst_statfs *host)
63611759Sbrandon.potter@amd.com{
63711759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
63811759Sbrandon.potter@amd.com
63911812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
64011812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
64111812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
64211759Sbrandon.potter@amd.com#else
64311812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
64411759Sbrandon.potter@amd.com#endif
64511759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
64611759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
64711759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
64811759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
64911759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
65011759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
65111812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
65211812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
65311812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
65411812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
65511812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
65611812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
65711812Sbaz21@cam.ac.uk#else
65811759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
65911759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
66011812Sbaz21@cam.ac.uk#endif
66111812Sbaz21@cam.ac.uk#if defined(__linux__)
66211759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
66311812Sbaz21@cam.ac.uk#else
66411812Sbaz21@cam.ac.uk    /*
66511812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
66611812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
66711812Sbaz21@cam.ac.uk     */
66811812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
66911812Sbaz21@cam.ac.uk#endif
67011759Sbrandon.potter@amd.com
67111759Sbrandon.potter@amd.com    tgt.copyOut(mem);
67211759Sbrandon.potter@amd.com}
67311759Sbrandon.potter@amd.com
674378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
675378SN/A/// only to find out if their stdout is a tty, to determine whether to
6769141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
6779141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
678360SN/Atemplate <class OS>
6791450SN/ASyscallReturn
68013995Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
681360SN/A{
6826701Sgblack@eecs.umich.edu    int index = 0;
68313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
68413995Sbrandon.potter@amd.com
68511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
68611856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
687360SN/A
68813907Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
689360SN/A
69011856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
69111856Sbrandon.potter@amd.com        return -ENOTTY;
69210496Ssteve.reinhardt@amd.com
69311856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
69413902Sbrandon.potter@amd.com    if (dfdp) {
69513902Sbrandon.potter@amd.com        EmulatedDriver *emul_driver = dfdp->getDriver();
69613902Sbrandon.potter@amd.com        if (emul_driver)
69713995Sbrandon.potter@amd.com            return emul_driver->ioctl(tc, req);
69813902Sbrandon.potter@amd.com    }
69913902Sbrandon.potter@amd.com
70013902Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
70113902Sbrandon.potter@amd.com    if (sfdp) {
70213902Sbrandon.potter@amd.com        int status;
70313902Sbrandon.potter@amd.com
70413902Sbrandon.potter@amd.com        switch (req) {
70513902Sbrandon.potter@amd.com          case SIOCGIFCONF: {
70613902Sbrandon.potter@amd.com            Addr conf_addr = p->getSyscallArg(tc, index);
70713902Sbrandon.potter@amd.com            BufferArg conf_arg(conf_addr, sizeof(ifconf));
70814024Sgabeblack@google.com            conf_arg.copyIn(tc->getVirtProxy());
70913902Sbrandon.potter@amd.com
71013902Sbrandon.potter@amd.com            ifconf *conf = (ifconf*)conf_arg.bufferPtr();
71113902Sbrandon.potter@amd.com            Addr ifc_buf_addr = (Addr)conf->ifc_buf;
71213902Sbrandon.potter@amd.com            BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len);
71314024Sgabeblack@google.com            ifc_buf_arg.copyIn(tc->getVirtProxy());
71413902Sbrandon.potter@amd.com
71513902Sbrandon.potter@amd.com            conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr();
71613902Sbrandon.potter@amd.com
71713902Sbrandon.potter@amd.com            status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr());
71813902Sbrandon.potter@amd.com            if (status != -1) {
71913902Sbrandon.potter@amd.com                conf->ifc_buf = (char*)ifc_buf_addr;
72014024Sgabeblack@google.com                ifc_buf_arg.copyOut(tc->getVirtProxy());
72114024Sgabeblack@google.com                conf_arg.copyOut(tc->getVirtProxy());
72213902Sbrandon.potter@amd.com            }
72313902Sbrandon.potter@amd.com
72413902Sbrandon.potter@amd.com            return status;
72513902Sbrandon.potter@amd.com          }
72613902Sbrandon.potter@amd.com          case SIOCGIFFLAGS:
72713936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
72813902Sbrandon.potter@amd.com          case SIOCGIFINDEX:
72913902Sbrandon.potter@amd.com#endif
73013902Sbrandon.potter@amd.com          case SIOCGIFNETMASK:
73113902Sbrandon.potter@amd.com          case SIOCGIFADDR:
73213936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
73313902Sbrandon.potter@amd.com          case SIOCGIFHWADDR:
73413902Sbrandon.potter@amd.com#endif
73513902Sbrandon.potter@amd.com          case SIOCGIFMTU: {
73613902Sbrandon.potter@amd.com            Addr req_addr = p->getSyscallArg(tc, index);
73713902Sbrandon.potter@amd.com            BufferArg req_arg(req_addr, sizeof(ifreq));
73814024Sgabeblack@google.com            req_arg.copyIn(tc->getVirtProxy());
73913902Sbrandon.potter@amd.com
74013902Sbrandon.potter@amd.com            status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr());
74113902Sbrandon.potter@amd.com            if (status != -1)
74214024Sgabeblack@google.com                req_arg.copyOut(tc->getVirtProxy());
74313902Sbrandon.potter@amd.com            return status;
74413902Sbrandon.potter@amd.com          }
74513902Sbrandon.potter@amd.com        }
74613902Sbrandon.potter@amd.com    }
74710496Ssteve.reinhardt@amd.com
74811856Sbrandon.potter@amd.com    /**
74911856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
75011856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
75111856Sbrandon.potter@amd.com     */
75211856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
75310930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
7549141Smarc.orr@gmail.com    return -ENOTTY;
755360SN/A}
756360SN/A
757360SN/Atemplate <class OS>
75811907SBrandon.Potter@amd.comSyscallReturn
75913995Sbrandon.potter@amd.comopenImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool isopenat)
760360SN/A{
76111907SBrandon.Potter@amd.com    int index = 0;
76213995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
76311907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
76411907SBrandon.Potter@amd.com
76511907SBrandon.Potter@amd.com    /**
76611907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
76711907SBrandon.Potter@amd.com     * descriptor from the simulated process.
76811907SBrandon.Potter@amd.com     */
76911907SBrandon.Potter@amd.com    if (isopenat)
77011907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
77111907SBrandon.Potter@amd.com
77211907SBrandon.Potter@amd.com    /**
77311907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
77411907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
77511907SBrandon.Potter@amd.com     */
776360SN/A    std::string path;
77714024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index)))
7781458SN/A        return -EFAULT;
779360SN/A
78011907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
78111907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
78211907SBrandon.Potter@amd.com#else
78311907SBrandon.Potter@amd.com    int host_flags = 0;
78411907SBrandon.Potter@amd.com#endif
78511907SBrandon.Potter@amd.com    /**
78611907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
78711907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
78811907SBrandon.Potter@amd.com     */
78911907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
790360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
79111907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
79211907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
79311907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
794360SN/A        }
795360SN/A    }
79611907SBrandon.Potter@amd.com    if (tgt_flags) {
79711907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
79811907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
79911907SBrandon.Potter@amd.com    }
800360SN/A#ifdef __CYGWIN32__
80111907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
802360SN/A#endif
803360SN/A
80411907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
8053669Sbinkertn@umich.edu
80611907SBrandon.Potter@amd.com    /**
80711907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
80811907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
80911907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
81011907SBrandon.Potter@amd.com     * create a full path.
81111907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
81211907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
81311907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
81411907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
81511907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
81611907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
81711907SBrandon.Potter@amd.com     */
81813883Sdavid.hashe@amd.com    std::string redir_path = path;
81913883Sdavid.hashe@amd.com    std::string abs_path = path;
82013883Sdavid.hashe@amd.com    if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) {
82113883Sdavid.hashe@amd.com        abs_path = p->absolutePath(path, true);
82213883Sdavid.hashe@amd.com        redir_path = p->checkPathRedirect(path);
82311907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
82411907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
82511907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
82611907SBrandon.Potter@amd.com        if (!ffdp)
82711907SBrandon.Potter@amd.com            return -EBADF;
82813883Sdavid.hashe@amd.com        abs_path = ffdp->getFileName() + path;
82913883Sdavid.hashe@amd.com        redir_path = p->checkPathRedirect(abs_path);
83011907SBrandon.Potter@amd.com    }
8311706SN/A
83211907SBrandon.Potter@amd.com    /**
83311907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
83411907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
83511907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
83611907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
83711907SBrandon.Potter@amd.com     */
83813883Sdavid.hashe@amd.com    if (startswith(abs_path, "/dev/")) {
83913883Sdavid.hashe@amd.com        std::string filename = abs_path.substr(strlen("/dev/"));
84011907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
84111907SBrandon.Potter@amd.com        if (drv) {
84211907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
84311907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
84413883Sdavid.hashe@amd.com                            isopenat ? "at" : "", abs_path.c_str());
84513995Sbrandon.potter@amd.com            return drv->open(tc, mode, host_flags);
84610496Ssteve.reinhardt@amd.com        }
84711907SBrandon.Potter@amd.com        /**
84811907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
84911907SBrandon.Potter@amd.com         * as /dev/zero
85011907SBrandon.Potter@amd.com         */
85110496Ssteve.reinhardt@amd.com    }
85210496Ssteve.reinhardt@amd.com
85311907SBrandon.Potter@amd.com    /**
85413883Sdavid.hashe@amd.com     * We make several attempts resolve a call to open.
85513883Sdavid.hashe@amd.com     *
85613883Sdavid.hashe@amd.com     * 1) Resolve any path redirection before hand. This will set the path
85713883Sdavid.hashe@amd.com     * up with variable 'redir_path' which may contain a modified path or
85813883Sdavid.hashe@amd.com     * the original path value. This should already be done in prior code.
85913883Sdavid.hashe@amd.com     * 2) Try to handle the access using 'special_paths'. Some special_paths
86013883Sdavid.hashe@amd.com     * and files cannot be called on the host and need to be handled as
86113883Sdavid.hashe@amd.com     * special cases inside the simulator. These special_paths are handled by
86213883Sdavid.hashe@amd.com     * C++ routines to provide output back to userspace.
86313883Sdavid.hashe@amd.com     * 3) If the full path that was created above does not match any of the
86413883Sdavid.hashe@amd.com     * special cases, pass it through to the open call on the __HOST__ to let
86513883Sdavid.hashe@amd.com     * the host open the file on our behalf. Again, the openImpl tries to
86613883Sdavid.hashe@amd.com     * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the
86713883Sdavid.hashe@amd.com     * faux-filesystem files). The faux-filesystem is dynamically created
86813883Sdavid.hashe@amd.com     * during simulator configuration using Python functions.
86913883Sdavid.hashe@amd.com     * 4) If the host cannot open the file, the open attempt failed in "3)".
87013883Sdavid.hashe@amd.com     * Return the host's error code back through the system call to the
87113883Sdavid.hashe@amd.com     * simulated process. If running a debug trace, also notify the user that
87213883Sdavid.hashe@amd.com     * the open call failed.
87313883Sdavid.hashe@amd.com     *
87413883Sdavid.hashe@amd.com     * Any success will set sim_fd to something other than -1 and skip the
87513883Sdavid.hashe@amd.com     * next conditions effectively bypassing them.
87611907SBrandon.Potter@amd.com     */
87711907SBrandon.Potter@amd.com    int sim_fd = -1;
87813883Sdavid.hashe@amd.com    std::string used_path;
87911907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
88013994Santhony.gutierrez@amd.com            { "/proc/meminfo/", "/system/", "/platform/", "/etc/passwd" };
88111907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
88213883Sdavid.hashe@amd.com        if (startswith(path, entry)) {
88313883Sdavid.hashe@amd.com            sim_fd = OS::openSpecialFile(abs_path, p, tc);
88413883Sdavid.hashe@amd.com            used_path = abs_path;
88513883Sdavid.hashe@amd.com        }
88611907SBrandon.Potter@amd.com    }
88711907SBrandon.Potter@amd.com    if (sim_fd == -1) {
88813883Sdavid.hashe@amd.com        sim_fd = open(redir_path.c_str(), host_flags, mode);
88913883Sdavid.hashe@amd.com        used_path = redir_path;
89011907SBrandon.Potter@amd.com    }
89111907SBrandon.Potter@amd.com    if (sim_fd == -1) {
89211907SBrandon.Potter@amd.com        int local = -errno;
89313883Sdavid.hashe@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s "
89413883Sdavid.hashe@amd.com                        "(inferred from:%s)\n", isopenat ? "at" : "",
89513883Sdavid.hashe@amd.com                        used_path.c_str(), path.c_str());
89611907SBrandon.Potter@amd.com        return local;
89711907SBrandon.Potter@amd.com    }
898360SN/A
89911907SBrandon.Potter@amd.com    /**
90011907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
90111907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
90211907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
90311907SBrandon.Potter@amd.com     * file descriptor.
90411907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
90511907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
90611907SBrandon.Potter@amd.com     */
90711907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
90811907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
90913883Sdavid.hashe@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n"
91013883Sdavid.hashe@amd.com                    "(inferred from:%s)\n", isopenat ? "at" : "",
91113883Sdavid.hashe@amd.com                    sim_fd, tgt_fd, used_path.c_str(), path.c_str());
91211907SBrandon.Potter@amd.com    return tgt_fd;
913360SN/A}
914360SN/A
91510027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
91610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
91710027SChris.Adeniyi-Jones@arm.comSyscallReturn
91813995Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
91910027SChris.Adeniyi-Jones@arm.com{
92013995Sbrandon.potter@amd.com    return openImpl<OS>(desc, callnum, tc, false);
92110027SChris.Adeniyi-Jones@arm.com}
92210027SChris.Adeniyi-Jones@arm.com
92310027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
92410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
92510027SChris.Adeniyi-Jones@arm.comSyscallReturn
92613995Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
92710027SChris.Adeniyi-Jones@arm.com{
92813995Sbrandon.potter@amd.com    return openImpl<OS>(desc, callnum, tc, true);
92910027SChris.Adeniyi-Jones@arm.com}
93010027SChris.Adeniyi-Jones@arm.com
93110633Smichaelupton@gmail.com/// Target unlinkat() handler.
93210633Smichaelupton@gmail.comtemplate <class OS>
93310633Smichaelupton@gmail.comSyscallReturn
93413995Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
93510633Smichaelupton@gmail.com{
93610633Smichaelupton@gmail.com    int index = 0;
93713995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
93810633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
93910633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
94010633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
94110633Smichaelupton@gmail.com
94213995Sbrandon.potter@amd.com    return unlinkHelper(desc, callnum, tc, 1);
94310633Smichaelupton@gmail.com}
94410633Smichaelupton@gmail.com
94510203SAli.Saidi@ARM.com/// Target facessat() handler
94610203SAli.Saidi@ARM.comtemplate <class OS>
94710203SAli.Saidi@ARM.comSyscallReturn
94813995Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
94910203SAli.Saidi@ARM.com{
95010203SAli.Saidi@ARM.com    int index = 0;
95113995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
95210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
95310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
95410203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
95513995Sbrandon.potter@amd.com    return accessFunc(desc, callnum, tc, 1);
95610203SAli.Saidi@ARM.com}
95710203SAli.Saidi@ARM.com
95810203SAli.Saidi@ARM.com/// Target readlinkat() handler
95910203SAli.Saidi@ARM.comtemplate <class OS>
96010203SAli.Saidi@ARM.comSyscallReturn
96113995Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
96210203SAli.Saidi@ARM.com{
96310203SAli.Saidi@ARM.com    int index = 0;
96413995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
96510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
96610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
96710203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
96813995Sbrandon.potter@amd.com    return readlinkFunc(desc, callnum, tc, 1);
96910203SAli.Saidi@ARM.com}
97010203SAli.Saidi@ARM.com
97110850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
97210850SGiacomo.Gabrielli@arm.comtemplate <class OS>
97310850SGiacomo.Gabrielli@arm.comSyscallReturn
97413995Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
97510850SGiacomo.Gabrielli@arm.com{
97610850SGiacomo.Gabrielli@arm.com    int index = 0;
97713995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
97810850SGiacomo.Gabrielli@arm.com
97910850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
98010850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
98110850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
98210850SGiacomo.Gabrielli@arm.com
98310850SGiacomo.Gabrielli@arm.com    std::string old_name;
98410850SGiacomo.Gabrielli@arm.com
98514024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(old_name,
98610850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
98710850SGiacomo.Gabrielli@arm.com        return -EFAULT;
98810850SGiacomo.Gabrielli@arm.com
98910850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
99010850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
99110850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
99210850SGiacomo.Gabrielli@arm.com
99310850SGiacomo.Gabrielli@arm.com    std::string new_name;
99410850SGiacomo.Gabrielli@arm.com
99514024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(new_name,
99610850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
99710850SGiacomo.Gabrielli@arm.com        return -EFAULT;
99810850SGiacomo.Gabrielli@arm.com
99913883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
100013883Sdavid.hashe@amd.com    old_name = process->checkPathRedirect(old_name);
100113883Sdavid.hashe@amd.com    new_name = process->checkPathRedirect(new_name);
100210850SGiacomo.Gabrielli@arm.com
100310850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
100410850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
100510850SGiacomo.Gabrielli@arm.com}
100610850SGiacomo.Gabrielli@arm.com
10076640Svince@csl.cornell.edu/// Target sysinfo() handler.
10086640Svince@csl.cornell.edutemplate <class OS>
10096640Svince@csl.cornell.eduSyscallReturn
101013995Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10116640Svince@csl.cornell.edu{
10126701Sgblack@eecs.umich.edu    int index = 0;
101313995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
101413995Sbrandon.potter@amd.com
10156701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
101610793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
10176640Svince@csl.cornell.edu
101811758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
101911758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
102011758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
10216640Svince@csl.cornell.edu
102214024Sgabeblack@google.com    sysinfo.copyOut(tc->getVirtProxy());
10236640Svince@csl.cornell.edu
10246701Sgblack@eecs.umich.edu    return 0;
10256640Svince@csl.cornell.edu}
1026360SN/A
10271999SN/A/// Target chmod() handler.
10281999SN/Atemplate <class OS>
10291999SN/ASyscallReturn
103013995Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
10311999SN/A{
10321999SN/A    std::string path;
103313995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
10341999SN/A
10356701Sgblack@eecs.umich.edu    int index = 0;
103614024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
10376701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10381999SN/A        return -EFAULT;
10396701Sgblack@eecs.umich.edu    }
10401999SN/A
10416701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
10421999SN/A    mode_t hostMode = 0;
10431999SN/A
10441999SN/A    // XXX translate mode flags via OS::something???
10451999SN/A    hostMode = mode;
10461999SN/A
104713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
104813883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
10493669Sbinkertn@umich.edu
10501999SN/A    // do the chmod
10511999SN/A    int result = chmod(path.c_str(), hostMode);
10521999SN/A    if (result < 0)
10532218SN/A        return -errno;
10541999SN/A
10551999SN/A    return 0;
10561999SN/A}
10571999SN/A
105813570Sbrandon.potter@amd.comtemplate <class OS>
105913570Sbrandon.potter@amd.comSyscallReturn
106013995Sbrandon.potter@amd.compollFunc(SyscallDesc *desc, int num, ThreadContext *tc)
106113570Sbrandon.potter@amd.com{
106213570Sbrandon.potter@amd.com    int index = 0;
106313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
106413570Sbrandon.potter@amd.com    Addr fdsPtr = p->getSyscallArg(tc, index);
106513570Sbrandon.potter@amd.com    int nfds = p->getSyscallArg(tc, index);
106613570Sbrandon.potter@amd.com    int tmout = p->getSyscallArg(tc, index);
106713570Sbrandon.potter@amd.com
106813570Sbrandon.potter@amd.com    BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds);
106914024Sgabeblack@google.com    fdsBuf.copyIn(tc->getVirtProxy());
107013570Sbrandon.potter@amd.com
107113570Sbrandon.potter@amd.com    /**
107213570Sbrandon.potter@amd.com     * Record the target file descriptors in a local variable. We need to
107313570Sbrandon.potter@amd.com     * replace them with host file descriptors but we need a temporary copy
107413570Sbrandon.potter@amd.com     * for later. Afterwards, replace each target file descriptor in the
107513570Sbrandon.potter@amd.com     * poll_fd array with its host_fd.
107613570Sbrandon.potter@amd.com     */
107713570Sbrandon.potter@amd.com    int temp_tgt_fds[nfds];
107813570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
107913570Sbrandon.potter@amd.com        temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd;
108013570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
108113570Sbrandon.potter@amd.com        auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
108213570Sbrandon.potter@amd.com        if (!hbfdp)
108313570Sbrandon.potter@amd.com            return -EBADF;
108413570Sbrandon.potter@amd.com        auto host_fd = hbfdp->getSimFD();
108513570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd;
108613570Sbrandon.potter@amd.com    }
108713570Sbrandon.potter@amd.com
108813570Sbrandon.potter@amd.com    /**
108913570Sbrandon.potter@amd.com     * We cannot allow an infinite poll to occur or it will inevitably cause
109013570Sbrandon.potter@amd.com     * a deadlock in the gem5 simulator with clone. We must pass in tmout with
109113570Sbrandon.potter@amd.com     * a non-negative value, however it also makes no sense to poll on the
109213570Sbrandon.potter@amd.com     * underlying host for any other time than tmout a zero timeout.
109313570Sbrandon.potter@amd.com     */
109413570Sbrandon.potter@amd.com    int status;
109513570Sbrandon.potter@amd.com    if (tmout < 0) {
109613570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
109713570Sbrandon.potter@amd.com        if (status == 0) {
109813570Sbrandon.potter@amd.com            /**
109913570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
110013570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try
110113570Sbrandon.potter@amd.com             * to return the signal interrupt instead.
110213570Sbrandon.potter@amd.com             */
110313570Sbrandon.potter@amd.com            System *sysh = tc->getSystemPtr();
110413570Sbrandon.potter@amd.com            std::list<BasicSignal>::iterator it;
110513570Sbrandon.potter@amd.com            for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++)
110613570Sbrandon.potter@amd.com                if (it->receiver == p)
110713570Sbrandon.potter@amd.com                    return -EINTR;
110813570Sbrandon.potter@amd.com            return SyscallReturn::retry();
110913570Sbrandon.potter@amd.com        }
111013570Sbrandon.potter@amd.com    } else
111113570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
111213570Sbrandon.potter@amd.com
111313570Sbrandon.potter@amd.com    if (status == -1)
111413570Sbrandon.potter@amd.com        return -errno;
111513570Sbrandon.potter@amd.com
111613570Sbrandon.potter@amd.com    /**
111713570Sbrandon.potter@amd.com     * Replace each host_fd in the returned poll_fd array with its original
111813570Sbrandon.potter@amd.com     * target file descriptor.
111913570Sbrandon.potter@amd.com     */
112013570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
112113570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
112213570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd;
112313570Sbrandon.potter@amd.com    }
112413570Sbrandon.potter@amd.com
112513570Sbrandon.potter@amd.com    /**
112613570Sbrandon.potter@amd.com     * Copy out the pollfd struct because the host may have updated fields
112713570Sbrandon.potter@amd.com     * in the structure.
112813570Sbrandon.potter@amd.com     */
112914024Sgabeblack@google.com    fdsBuf.copyOut(tc->getVirtProxy());
113013570Sbrandon.potter@amd.com
113113570Sbrandon.potter@amd.com    return status;
113213570Sbrandon.potter@amd.com}
11331999SN/A
11341999SN/A/// Target fchmod() handler.
11351999SN/Atemplate <class OS>
11361999SN/ASyscallReturn
113713995Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
11381999SN/A{
11396701Sgblack@eecs.umich.edu    int index = 0;
114013995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
114111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
114211856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
114310931Sbrandon.potter@amd.com
114411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
114511856Sbrandon.potter@amd.com    if (!ffdp)
11461999SN/A        return -EBADF;
114711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
11481999SN/A
114911856Sbrandon.potter@amd.com    mode_t hostMode = mode;
11501999SN/A
115111856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
11521999SN/A
115311856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
11541999SN/A}
11551999SN/A
11565877Shsul@eecs.umich.edu/// Target mremap() handler.
11575877Shsul@eecs.umich.edutemplate <class OS>
11585877Shsul@eecs.umich.eduSyscallReturn
115913995Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
11605877Shsul@eecs.umich.edu{
11616701Sgblack@eecs.umich.edu    int index = 0;
116213995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
11636701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
11646701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
11656701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
11666701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
116710027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
116810027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
116910027SChris.Adeniyi-Jones@arm.com
117010027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
117110027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
11725877Shsul@eecs.umich.edu
117310318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
117410318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
11755877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
11765877Shsul@eecs.umich.edu        return -EINVAL;
11775877Shsul@eecs.umich.edu    }
11785877Shsul@eecs.umich.edu
117910486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
118010486Stjablin@gmail.com
11815877Shsul@eecs.umich.edu    if (new_length > old_length) {
118211905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
118311905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
118411905SBrandon.Potter@amd.com
118511905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
118610027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
118712206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
118812206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
11895877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
119011905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
119111905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
11925877Shsul@eecs.umich.edu            return start;
11935877Shsul@eecs.umich.edu        } else {
119410027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
11955877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
11965877Shsul@eecs.umich.edu                return -ENOMEM;
11975877Shsul@eecs.umich.edu            } else {
119812206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
119912206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
120012206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
120112206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
120212206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
120312206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
120412206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
120512206Srico.amslinger@informatik.uni-augsburg.de                }
120612206Srico.amslinger@informatik.uni-augsburg.de
120710027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
120810027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
120910027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
121010027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
12115877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
121210027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
121310027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
121410027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
121510027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
121612206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
121712206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
121812206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
121912206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
122010027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
122110027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
122210027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
122310027SChris.Adeniyi-Jones@arm.com                }
122410027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
122510027SChris.Adeniyi-Jones@arm.com                return new_start;
12265877Shsul@eecs.umich.edu            }
12275877Shsul@eecs.umich.edu        }
12285877Shsul@eecs.umich.edu    } else {
122910027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
123010027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
12318601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
123210027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
12335877Shsul@eecs.umich.edu    }
12345877Shsul@eecs.umich.edu}
12351999SN/A
1236378SN/A/// Target stat() handler.
1237360SN/Atemplate <class OS>
12381450SN/ASyscallReturn
123913995Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1240360SN/A{
1241360SN/A    std::string path;
124213995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
1243360SN/A
12446701Sgblack@eecs.umich.edu    int index = 0;
124514024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
12466701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12476701Sgblack@eecs.umich.edu        return -EFAULT;
12486701Sgblack@eecs.umich.edu    }
12496701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1250360SN/A
125113883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
125213883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
12533669Sbinkertn@umich.edu
1254360SN/A    struct stat hostBuf;
1255360SN/A    int result = stat(path.c_str(), &hostBuf);
1256360SN/A
1257360SN/A    if (result < 0)
12582218SN/A        return -errno;
1259360SN/A
126014024Sgabeblack@google.com    copyOutStatBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
1261360SN/A
12621458SN/A    return 0;
1263360SN/A}
1264360SN/A
1265360SN/A
12665074Ssaidi@eecs.umich.edu/// Target stat64() handler.
12675074Ssaidi@eecs.umich.edutemplate <class OS>
12685074Ssaidi@eecs.umich.eduSyscallReturn
126913995Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
12705074Ssaidi@eecs.umich.edu{
12715074Ssaidi@eecs.umich.edu    std::string path;
127213995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
12735074Ssaidi@eecs.umich.edu
12746701Sgblack@eecs.umich.edu    int index = 0;
127514024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
12766701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
12775074Ssaidi@eecs.umich.edu        return -EFAULT;
12786701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12795074Ssaidi@eecs.umich.edu
128013883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
128113883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
12825074Ssaidi@eecs.umich.edu
12835208Ssaidi@eecs.umich.edu#if NO_STAT64
12845208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
12855208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
12865208Ssaidi@eecs.umich.edu#else
12875074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
12885074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
12895208Ssaidi@eecs.umich.edu#endif
12905074Ssaidi@eecs.umich.edu
12915074Ssaidi@eecs.umich.edu    if (result < 0)
12925074Ssaidi@eecs.umich.edu        return -errno;
12935074Ssaidi@eecs.umich.edu
129414024Sgabeblack@google.com    copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
12955074Ssaidi@eecs.umich.edu
12965074Ssaidi@eecs.umich.edu    return 0;
12975074Ssaidi@eecs.umich.edu}
12985074Ssaidi@eecs.umich.edu
12995074Ssaidi@eecs.umich.edu
130010027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
130110027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
130210027SChris.Adeniyi-Jones@arm.comSyscallReturn
130313995Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
130410027SChris.Adeniyi-Jones@arm.com{
130510027SChris.Adeniyi-Jones@arm.com    int index = 0;
130613995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
130710027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
130810027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
130910793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
131010027SChris.Adeniyi-Jones@arm.com
131110027SChris.Adeniyi-Jones@arm.com    std::string path;
131214024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
131310027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
131410027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
131510027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
131610027SChris.Adeniyi-Jones@arm.com
131713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
131813883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
131910027SChris.Adeniyi-Jones@arm.com
132010027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
132110027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
132210027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
132310027SChris.Adeniyi-Jones@arm.com#else
132410027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
132510027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
132610027SChris.Adeniyi-Jones@arm.com#endif
132710027SChris.Adeniyi-Jones@arm.com
132810027SChris.Adeniyi-Jones@arm.com    if (result < 0)
132910027SChris.Adeniyi-Jones@arm.com        return -errno;
133010027SChris.Adeniyi-Jones@arm.com
133114024Sgabeblack@google.com    copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
133210027SChris.Adeniyi-Jones@arm.com
133310027SChris.Adeniyi-Jones@arm.com    return 0;
133410027SChris.Adeniyi-Jones@arm.com}
133510027SChris.Adeniyi-Jones@arm.com
133610027SChris.Adeniyi-Jones@arm.com
13371999SN/A/// Target fstat64() handler.
13381999SN/Atemplate <class OS>
13391999SN/ASyscallReturn
134013995Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
13411999SN/A{
13426701Sgblack@eecs.umich.edu    int index = 0;
134313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
134411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
134511856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
134610931Sbrandon.potter@amd.com
134714120Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
134811856Sbrandon.potter@amd.com    if (!ffdp)
13491999SN/A        return -EBADF;
135011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
13511999SN/A
13522764Sstever@eecs.umich.edu#if NO_STAT64
13532064SN/A    struct stat  hostBuf;
135410931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
13552064SN/A#else
13562064SN/A    struct stat64  hostBuf;
135710931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
13582064SN/A#endif
13591999SN/A
13601999SN/A    if (result < 0)
13612218SN/A        return -errno;
13621999SN/A
136314024Sgabeblack@google.com    copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf, (sim_fd == 1));
13641999SN/A
13651999SN/A    return 0;
13661999SN/A}
13671999SN/A
13681999SN/A
1369378SN/A/// Target lstat() handler.
1370360SN/Atemplate <class OS>
13711450SN/ASyscallReturn
137213995Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1373360SN/A{
1374360SN/A    std::string path;
137513995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
1376360SN/A
13776701Sgblack@eecs.umich.edu    int index = 0;
137814024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
13796701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13806701Sgblack@eecs.umich.edu        return -EFAULT;
13816701Sgblack@eecs.umich.edu    }
13826701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1383360SN/A
138413883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
138513883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
13863669Sbinkertn@umich.edu
1387360SN/A    struct stat hostBuf;
1388360SN/A    int result = lstat(path.c_str(), &hostBuf);
1389360SN/A
1390360SN/A    if (result < 0)
13911458SN/A        return -errno;
1392360SN/A
139314024Sgabeblack@google.com    copyOutStatBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
1394360SN/A
13951458SN/A    return 0;
1396360SN/A}
1397360SN/A
13981999SN/A/// Target lstat64() handler.
13991999SN/Atemplate <class OS>
14001999SN/ASyscallReturn
140113995Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc)
14021999SN/A{
14031999SN/A    std::string path;
140413995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
14051999SN/A
14066701Sgblack@eecs.umich.edu    int index = 0;
140714024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
14086701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14096701Sgblack@eecs.umich.edu        return -EFAULT;
14106701Sgblack@eecs.umich.edu    }
14116701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
14121999SN/A
141313883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
141413883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
14153669Sbinkertn@umich.edu
14162764Sstever@eecs.umich.edu#if NO_STAT64
14172064SN/A    struct stat hostBuf;
14182064SN/A    int result = lstat(path.c_str(), &hostBuf);
14192064SN/A#else
14201999SN/A    struct stat64 hostBuf;
14211999SN/A    int result = lstat64(path.c_str(), &hostBuf);
14222064SN/A#endif
14231999SN/A
14241999SN/A    if (result < 0)
14251999SN/A        return -errno;
14261999SN/A
142714024Sgabeblack@google.com    copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
14281999SN/A
14291999SN/A    return 0;
14301999SN/A}
14311999SN/A
1432378SN/A/// Target fstat() handler.
1433360SN/Atemplate <class OS>
14341450SN/ASyscallReturn
143513995Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1436360SN/A{
14376701Sgblack@eecs.umich.edu    int index = 0;
143813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
143911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
144011856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1441360SN/A
144211380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1443360SN/A
144411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
144511856Sbrandon.potter@amd.com    if (!ffdp)
14461458SN/A        return -EBADF;
144711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1448360SN/A
1449360SN/A    struct stat hostBuf;
145010931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1451360SN/A
1452360SN/A    if (result < 0)
14531458SN/A        return -errno;
1454360SN/A
145514024Sgabeblack@google.com    copyOutStatBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf, (sim_fd == 1));
14562021SN/A
14571458SN/A    return 0;
1458360SN/A}
1459360SN/A
14601706SN/A/// Target statfs() handler.
14611706SN/Atemplate <class OS>
14621706SN/ASyscallReturn
146313995Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
14641706SN/A{
146513936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
14661706SN/A    std::string path;
146713995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
14681706SN/A
14696701Sgblack@eecs.umich.edu    int index = 0;
147014024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
14716701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14726701Sgblack@eecs.umich.edu        return -EFAULT;
14736701Sgblack@eecs.umich.edu    }
14746701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
14751706SN/A
147613883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
147713883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
14783669Sbinkertn@umich.edu
14791706SN/A    struct statfs hostBuf;
14801706SN/A    int result = statfs(path.c_str(), &hostBuf);
14811706SN/A
14821706SN/A    if (result < 0)
14832218SN/A        return -errno;
14841706SN/A
148514024Sgabeblack@google.com    copyOutStatfsBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
148613933Sbrandon.potter@amd.com    return 0;
148713933Sbrandon.potter@amd.com#else
148813933Sbrandon.potter@amd.com    warnUnsupportedOS("statfs");
148913933Sbrandon.potter@amd.com    return -1;
149011799Sbrandon.potter@amd.com#endif
14911706SN/A}
14921706SN/A
149311886Sbrandon.potter@amd.comtemplate <class OS>
149411886Sbrandon.potter@amd.comSyscallReturn
149513995Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
149611886Sbrandon.potter@amd.com{
149711886Sbrandon.potter@amd.com    int index = 0;
149812426Sqtt2@cornell.edu
149913995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
150013557Sgabeblack@google.com    RegVal flags = p->getSyscallArg(tc, index);
150113557Sgabeblack@google.com    RegVal newStack = p->getSyscallArg(tc, index);
150211886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
150312426Sqtt2@cornell.edu
150413534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
150512426Sqtt2@cornell.edu    /**
150613534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
150712426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
150812426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
150912426Sqtt2@cornell.edu     */
151013536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
151112426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
151212426Sqtt2@cornell.edu#else
151311886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
151413536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
151512426Sqtt2@cornell.edu#endif
151611886Sbrandon.potter@amd.com
151711886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
151811886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
151911886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
152011886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
152111886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
152211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
152311886Sbrandon.potter@amd.com        return -EINVAL;
152411886Sbrandon.potter@amd.com
152511886Sbrandon.potter@amd.com    ThreadContext *ctc;
152613649Sqtt2@cornell.edu    if (!(ctc = p->findFreeContext())) {
152713649Sqtt2@cornell.edu        DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system"
152813649Sqtt2@cornell.edu                        "[cpu %d, thread %d]", tc->cpuId(), tc->threadId());
152913649Sqtt2@cornell.edu        return -EAGAIN;
153013649Sqtt2@cornell.edu    }
153111886Sbrandon.potter@amd.com
153211886Sbrandon.potter@amd.com    /**
153311886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
153411886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
153511886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
153611886Sbrandon.potter@amd.com     * constructor.
153711886Sbrandon.potter@amd.com     */
153811886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
153911886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
154011886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
154111886Sbrandon.potter@amd.com    pp->system = p->system;
154213883Sdavid.hashe@amd.com    pp->cwd.assign(p->tgtCwd);
154311886Sbrandon.potter@amd.com    pp->input.assign("stdin");
154411886Sbrandon.potter@amd.com    pp->output.assign("stdout");
154511886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
154611886Sbrandon.potter@amd.com    pp->uid = p->uid();
154711886Sbrandon.potter@amd.com    pp->euid = p->euid();
154811886Sbrandon.potter@amd.com    pp->gid = p->gid();
154911886Sbrandon.potter@amd.com    pp->egid = p->egid();
155011886Sbrandon.potter@amd.com
155111886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
155211886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
155311886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
155411886Sbrandon.potter@amd.com    do {
155511886Sbrandon.potter@amd.com        temp_pid++;
155611886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
155711886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
155811886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
155911886Sbrandon.potter@amd.com
156011886Sbrandon.potter@amd.com    pp->pid = temp_pid;
156111886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
156213867Salexandru.dutu@amd.com    pp->useArchPT = p->useArchPT;
156313867Salexandru.dutu@amd.com    pp->kvmInSE = p->kvmInSE;
156411886Sbrandon.potter@amd.com    Process *cp = pp->create();
156511886Sbrandon.potter@amd.com    delete pp;
156611886Sbrandon.potter@amd.com
156711886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
156811886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
156911886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
157011886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
157111886Sbrandon.potter@amd.com
157211886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
157311886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
157411886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
157511886Sbrandon.potter@amd.com        *ptid = cp->pid();
157614024Sgabeblack@google.com        ptidBuf.copyOut(tc->getVirtProxy());
157711886Sbrandon.potter@amd.com    }
157811886Sbrandon.potter@amd.com
157913867Salexandru.dutu@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
158013867Salexandru.dutu@amd.com        cp->pTable->shared = true;
158113867Salexandru.dutu@amd.com        cp->useForClone = true;
158213867Salexandru.dutu@amd.com    }
158311886Sbrandon.potter@amd.com    cp->initState();
158411886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
158511886Sbrandon.potter@amd.com
158611911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
158711911SBrandon.Potter@amd.com        delete cp->sigchld;
158811911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
158911911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
159011911SBrandon.Potter@amd.com        *cp->sigchld = true;
159111911SBrandon.Potter@amd.com    }
159211911SBrandon.Potter@amd.com
159311886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
159411886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
159511886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
159611886Sbrandon.potter@amd.com        *ctid = cp->pid();
159714024Sgabeblack@google.com        ctidBuf.copyOut(ctc->getVirtProxy());
159811886Sbrandon.potter@amd.com    }
159911886Sbrandon.potter@amd.com
160011886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
160111886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
160211886Sbrandon.potter@amd.com
160311886Sbrandon.potter@amd.com    ctc->clearArchRegs();
160411886Sbrandon.potter@amd.com
160513536Sandreas.sandberg@arm.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
160611886Sbrandon.potter@amd.com
160711886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
160811886Sbrandon.potter@amd.com
160911886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
161011886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
161111886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
161211886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
161311886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
161411886Sbrandon.potter@amd.com#endif
161511886Sbrandon.potter@amd.com
161613867Salexandru.dutu@amd.com    if (p->kvmInSE) {
161713867Salexandru.dutu@amd.com#if THE_ISA == X86_ISA
161813867Salexandru.dutu@amd.com        ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX));
161913867Salexandru.dutu@amd.com#else
162013867Salexandru.dutu@amd.com        panic("KVM CPU model is not supported for this ISA");
162113867Salexandru.dutu@amd.com#endif
162213867Salexandru.dutu@amd.com    } else {
162313867Salexandru.dutu@amd.com        TheISA::PCState cpc = tc->pcState();
162413867Salexandru.dutu@amd.com        cpc.advance();
162513867Salexandru.dutu@amd.com        ctc->pcState(cpc);
162613867Salexandru.dutu@amd.com    }
162711886Sbrandon.potter@amd.com    ctc->activate();
162811886Sbrandon.potter@amd.com
162911886Sbrandon.potter@amd.com    return cp->pid();
163011886Sbrandon.potter@amd.com}
16311706SN/A
16321706SN/A/// Target fstatfs() handler.
16331706SN/Atemplate <class OS>
16341706SN/ASyscallReturn
163513995Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
16361706SN/A{
16376701Sgblack@eecs.umich.edu    int index = 0;
163813995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
163911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
164011856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
16411706SN/A
164211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
164311856Sbrandon.potter@amd.com    if (!ffdp)
16441706SN/A        return -EBADF;
164511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
16461706SN/A
16471706SN/A    struct statfs hostBuf;
164810931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
16491706SN/A
16501706SN/A    if (result < 0)
16512218SN/A        return -errno;
16521706SN/A
165314024Sgabeblack@google.com    copyOutStatfsBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf);
16541706SN/A
16551706SN/A    return 0;
16561706SN/A}
16571706SN/A
165813572Sbrandon.potter@amd.com/// Target readv() handler.
165913572Sbrandon.potter@amd.comtemplate <class OS>
166013572Sbrandon.potter@amd.comSyscallReturn
166113995Sbrandon.potter@amd.comreadvFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
166213572Sbrandon.potter@amd.com{
166313572Sbrandon.potter@amd.com    int index = 0;
166413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
166513572Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
166613572Sbrandon.potter@amd.com
166713572Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
166813572Sbrandon.potter@amd.com    if (!ffdp)
166913572Sbrandon.potter@amd.com        return -EBADF;
167013572Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
167113572Sbrandon.potter@amd.com
167214024Sgabeblack@google.com    PortProxy &prox = tc->getVirtProxy();
167313572Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
167413572Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
167513572Sbrandon.potter@amd.com    typename OS::tgt_iovec tiov[count];
167613572Sbrandon.potter@amd.com    struct iovec hiov[count];
167713572Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
167813572Sbrandon.potter@amd.com        prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)),
167914010Sgabeblack@google.com                      &tiov[i], sizeof(typename OS::tgt_iovec));
168013572Sbrandon.potter@amd.com        hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len);
168113572Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
168213572Sbrandon.potter@amd.com    }
168313572Sbrandon.potter@amd.com
168413572Sbrandon.potter@amd.com    int result = readv(sim_fd, hiov, count);
168513572Sbrandon.potter@amd.com    int local_errno = errno;
168613572Sbrandon.potter@amd.com
168713572Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
168813572Sbrandon.potter@amd.com        if (result != -1) {
168913572Sbrandon.potter@amd.com            prox.writeBlob(TheISA::htog(tiov[i].iov_base),
169014010Sgabeblack@google.com                           hiov[i].iov_base, hiov[i].iov_len);
169113572Sbrandon.potter@amd.com        }
169213572Sbrandon.potter@amd.com        delete [] (char *)hiov[i].iov_base;
169313572Sbrandon.potter@amd.com    }
169413572Sbrandon.potter@amd.com
169513572Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : result;
169613572Sbrandon.potter@amd.com}
16971706SN/A
16981999SN/A/// Target writev() handler.
16991999SN/Atemplate <class OS>
17001999SN/ASyscallReturn
170113995Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
17021999SN/A{
17036701Sgblack@eecs.umich.edu    int index = 0;
170413995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
170511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
170610931Sbrandon.potter@amd.com
170711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
170811856Sbrandon.potter@amd.com    if (!hbfdp)
17091999SN/A        return -EBADF;
171011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
17111999SN/A
171214024Sgabeblack@google.com    PortProxy &prox = tc->getVirtProxy();
171311856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
171411856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
17151999SN/A    struct iovec hiov[count];
17166227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
17171999SN/A        typename OS::tgt_iovec tiov;
17182461SN/A
171911856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
172014010Sgabeblack@google.com                      &tiov, sizeof(typename OS::tgt_iovec));
17218737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
17221999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
172314010Sgabeblack@google.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), hiov[i].iov_base,
172411856Sbrandon.potter@amd.com                      hiov[i].iov_len);
17251999SN/A    }
17261999SN/A
172710931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
17281999SN/A
17296227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
17301999SN/A        delete [] (char *)hiov[i].iov_base;
17311999SN/A
173213572Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
17331999SN/A}
17341999SN/A
173511385Sbrandon.potter@amd.com/// Real mmap handler.
1736360SN/Atemplate <class OS>
17371450SN/ASyscallReturn
173813995Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, ThreadContext *tc, bool is_mmap2)
1739360SN/A{
17406701Sgblack@eecs.umich.edu    int index = 0;
174113995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
17426701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
17436701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
174411383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
174511383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
17468324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
174710486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1748360SN/A
174911385Sbrandon.potter@amd.com    if (is_mmap2)
175011385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
17519008Sgblack@eecs.umich.edu
175211383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
175311383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
175411383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
175511383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
175611383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
175711383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
175811383Sbrandon.potter@amd.com        !length) {
175911383Sbrandon.potter@amd.com        return -EINVAL;
176011383Sbrandon.potter@amd.com    }
17618324Ssteve.reinhardt@amd.com
176211383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
176311383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
176411383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
176511383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
176611383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
176711383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
176811383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
176911383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
177011383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
177111383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
177211383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
177311383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
177411383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
177511383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
177611383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
177711383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
177811383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
177911383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
178011383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
178111383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
178211383Sbrandon.potter@amd.com        // than we currently maintain.
178311383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
178411383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
178511383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
17868324Ssteve.reinhardt@amd.com    }
17875877Shsul@eecs.umich.edu
178810486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
178910486Stjablin@gmail.com
179011383Sbrandon.potter@amd.com    int sim_fd = -1;
179111383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
179211383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
179311856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
179411624Smichael.lebeane@amd.com
179511856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
179611856Sbrandon.potter@amd.com        if (dfdp) {
179711856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
179813995Sbrandon.potter@amd.com            return emul_driver->mmap(tc, start, length, prot, tgt_flags,
179913995Sbrandon.potter@amd.com                                     tgt_fd, offset);
180011624Smichael.lebeane@amd.com        }
180111624Smichael.lebeane@amd.com
180211856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
180311856Sbrandon.potter@amd.com        if (!ffdp)
180411383Sbrandon.potter@amd.com            return -EBADF;
180511856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1806360SN/A
180711913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
180811383Sbrandon.potter@amd.com                                    sim_fd, offset);
18098600Ssteve.reinhardt@amd.com
181011383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
181111383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
181211383Sbrandon.potter@amd.com            return -errno;
18138600Ssteve.reinhardt@amd.com        }
18142544SN/A    }
18152544SN/A
181611383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
181711383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
181811383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
181911905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
182011905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
182111905SBrandon.Potter@amd.com
182211905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
182311905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
182411905SBrandon.Potter@amd.com
182511905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
182611383Sbrandon.potter@amd.com    }
182711383Sbrandon.potter@amd.com
182811383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
182911383Sbrandon.potter@amd.com                    start, start + length - 1);
183011383Sbrandon.potter@amd.com
183111383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
183211383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
183311383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
183411383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
183511383Sbrandon.potter@amd.com    if (clobber) {
183611383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
183711383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
183811383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
183911383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
184011383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
18418600Ssteve.reinhardt@amd.com        }
18426672Sgblack@eecs.umich.edu    }
18438600Ssteve.reinhardt@amd.com
184411383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
184511383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
184611383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
18478601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
18482544SN/A
184911383Sbrandon.potter@amd.com    // Transfer content into target address space.
185014024Sgabeblack@google.com    PortProxy &tp = tc->getVirtProxy();
185111383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
185211383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
185311383Sbrandon.potter@amd.com        // with something like:
185411383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
185511383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
185611383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
185711383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
185811383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
185911383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
186011383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
186111383Sbrandon.potter@amd.com    } else {
186211383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
186311383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
186411383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
186511383Sbrandon.potter@amd.com        // the host into the target address space.
186611383Sbrandon.potter@amd.com        struct stat file_stat;
186711383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
186811383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
186911383Sbrandon.potter@amd.com
187011383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
187111383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
187211383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
187311383Sbrandon.potter@amd.com        // on the specified offset into the file.
187411383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
187511383Sbrandon.potter@amd.com                                 length);
187611383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
187711383Sbrandon.potter@amd.com
187811383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
187911383Sbrandon.potter@amd.com        munmap(pmap, length);
188011383Sbrandon.potter@amd.com
188111392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
188211392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
188311392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
188411392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
188511392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
188611392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
188711392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
188811392Sbrandon.potter@amd.com        if (interpreter) {
188911392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
189011392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
189111392Sbrandon.potter@amd.com
189211392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
189311392Sbrandon.potter@amd.com
189411392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
189511856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
189611856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
189711856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
189811392Sbrandon.potter@amd.com
189911392Sbrandon.potter@amd.com                if (lib) {
190011392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
190111392Sbrandon.potter@amd.com                                        lib->textBase(), start);
190211392Sbrandon.potter@amd.com                }
190311392Sbrandon.potter@amd.com            }
190411392Sbrandon.potter@amd.com        }
190511392Sbrandon.potter@amd.com
190611383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
190711383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
190811383Sbrandon.potter@amd.com        // execution (hopefully).
190911383Sbrandon.potter@amd.com    }
191011383Sbrandon.potter@amd.com
19111458SN/A    return start;
1912360SN/A}
1913360SN/A
191411593Santhony.gutierrez@amd.comtemplate <class OS>
191511593Santhony.gutierrez@amd.comSyscallReturn
191613995Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, ThreadContext *tc)
191711593Santhony.gutierrez@amd.com{
191811593Santhony.gutierrez@amd.com    int index = 0;
191913995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
192011593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
192111593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
192211593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
192311593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
192411593Santhony.gutierrez@amd.com
192511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
192611856Sbrandon.potter@amd.com    if (!ffdp)
192711593Santhony.gutierrez@amd.com        return -EBADF;
192811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
192911593Santhony.gutierrez@amd.com
193011593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
193114024Sgabeblack@google.com    bufArg.copyIn(tc->getVirtProxy());
193211593Santhony.gutierrez@amd.com
193311594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
193411593Santhony.gutierrez@amd.com
193511593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
193611593Santhony.gutierrez@amd.com}
193711593Santhony.gutierrez@amd.com
193811385Sbrandon.potter@amd.com/// Target mmap() handler.
193911385Sbrandon.potter@amd.comtemplate <class OS>
194011385Sbrandon.potter@amd.comSyscallReturn
194113995Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, ThreadContext *tc)
194211385Sbrandon.potter@amd.com{
194313995Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, tc, false);
194411385Sbrandon.potter@amd.com}
194511385Sbrandon.potter@amd.com
194611385Sbrandon.potter@amd.com/// Target mmap2() handler.
194711385Sbrandon.potter@amd.comtemplate <class OS>
194811385Sbrandon.potter@amd.comSyscallReturn
194913995Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, ThreadContext *tc)
195011385Sbrandon.potter@amd.com{
195113995Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, tc, true);
195211385Sbrandon.potter@amd.com}
195311385Sbrandon.potter@amd.com
1954378SN/A/// Target getrlimit() handler.
1955360SN/Atemplate <class OS>
19561450SN/ASyscallReturn
195713995Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
1958360SN/A{
19596701Sgblack@eecs.umich.edu    int index = 0;
196013995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
19616701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
19626701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1963360SN/A
1964360SN/A    switch (resource) {
196511906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
196611906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
196711906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
196811906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
196911906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
197011906SBrandon.Potter@amd.com        break;
1971360SN/A
197211906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
197311906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
197411906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
197511906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
197611906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
197711906SBrandon.Potter@amd.com        break;
19785877Shsul@eecs.umich.edu
197914119Sbrandon.potter@amd.com      case OS::TGT_RLIMIT_NPROC:
198014119Sbrandon.potter@amd.com        rlp->rlim_cur = rlp->rlim_max = tc->getSystemPtr()->numContexts();
198114119Sbrandon.potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
198214119Sbrandon.potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
198314119Sbrandon.potter@amd.com        break;
198414119Sbrandon.potter@amd.com
198511906SBrandon.Potter@amd.com      default:
198611906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
198711906SBrandon.Potter@amd.com        return -EINVAL;
198811906SBrandon.Potter@amd.com        break;
1989360SN/A    }
1990360SN/A
199114024Sgabeblack@google.com    rlp.copyOut(tc->getVirtProxy());
19921458SN/A    return 0;
1993360SN/A}
1994360SN/A
199512235Sar4jc@virginia.edutemplate <class OS>
199612235Sar4jc@virginia.eduSyscallReturn
199713995Sbrandon.potter@amd.comprlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
199812235Sar4jc@virginia.edu{
199912235Sar4jc@virginia.edu    int index = 0;
200013995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
200112235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
200212235Sar4jc@virginia.edu    {
200312235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
200412235Sar4jc@virginia.edu        return -EPERM;
200512235Sar4jc@virginia.edu    }
200612235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
200712235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
200812235Sar4jc@virginia.edu    if (n != 0)
200912235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
201012235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
201112235Sar4jc@virginia.edu    if (o != 0)
201212235Sar4jc@virginia.edu    {
201312416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
201412235Sar4jc@virginia.edu        switch (resource) {
201512235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
201612235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
201712235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
201812235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
201912235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
202012235Sar4jc@virginia.edu            break;
202112235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
202212235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
202312235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
202412235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
202512235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
202612593Sjason@lowepower.com            break;
202712235Sar4jc@virginia.edu          default:
202812235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
202912235Sar4jc@virginia.edu            return -EINVAL;
203012235Sar4jc@virginia.edu            break;
203112235Sar4jc@virginia.edu        }
203214024Sgabeblack@google.com        rlp.copyOut(tc->getVirtProxy());
203312235Sar4jc@virginia.edu    }
203412235Sar4jc@virginia.edu    return 0;
203512235Sar4jc@virginia.edu}
203612235Sar4jc@virginia.edu
203710796Sbrandon.potter@amd.com/// Target clock_gettime() function.
203810796Sbrandon.potter@amd.comtemplate <class OS>
203910796Sbrandon.potter@amd.comSyscallReturn
204013995Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, ThreadContext *tc)
204110796Sbrandon.potter@amd.com{
204210796Sbrandon.potter@amd.com    int index = 1;
204313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
204410796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
204510796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
204610796Sbrandon.potter@amd.com
204710796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
204810796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
204910796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
205010796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
205110796Sbrandon.potter@amd.com
205214024Sgabeblack@google.com    tp.copyOut(tc->getVirtProxy());
205310796Sbrandon.potter@amd.com
205410796Sbrandon.potter@amd.com    return 0;
205510796Sbrandon.potter@amd.com}
205610796Sbrandon.potter@amd.com
205711337SMichael.Lebeane@amd.com/// Target clock_getres() function.
205811337SMichael.Lebeane@amd.comtemplate <class OS>
205911337SMichael.Lebeane@amd.comSyscallReturn
206013995Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, ThreadContext *tc)
206111337SMichael.Lebeane@amd.com{
206211337SMichael.Lebeane@amd.com    int index = 1;
206313995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
206411337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
206511337SMichael.Lebeane@amd.com
206611337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
206711337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
206811337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
206911337SMichael.Lebeane@amd.com
207014024Sgabeblack@google.com    tp.copyOut(tc->getVirtProxy());
207111337SMichael.Lebeane@amd.com
207211337SMichael.Lebeane@amd.com    return 0;
207311337SMichael.Lebeane@amd.com}
207411337SMichael.Lebeane@amd.com
2075378SN/A/// Target gettimeofday() handler.
2076360SN/Atemplate <class OS>
20771450SN/ASyscallReturn
207813995Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2079360SN/A{
20806701Sgblack@eecs.umich.edu    int index = 0;
208113995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
20826701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
2083360SN/A
208410796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
2085360SN/A    tp->tv_sec += seconds_since_epoch;
20866109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
20876109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
2088360SN/A
208914024Sgabeblack@google.com    tp.copyOut(tc->getVirtProxy());
2090360SN/A
20911458SN/A    return 0;
2092360SN/A}
2093360SN/A
2094360SN/A
20951999SN/A/// Target utimes() handler.
20961999SN/Atemplate <class OS>
20971999SN/ASyscallReturn
209813995Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
20991999SN/A{
21001999SN/A    std::string path;
210113995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
21021999SN/A
21036701Sgblack@eecs.umich.edu    int index = 0;
210414024Sgabeblack@google.com    if (!tc->getVirtProxy().tryReadString(path,
21056701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
21066701Sgblack@eecs.umich.edu        return -EFAULT;
21076701Sgblack@eecs.umich.edu    }
21081999SN/A
21096701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
21106701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
211114024Sgabeblack@google.com    tp.copyIn(tc->getVirtProxy());
21121999SN/A
21131999SN/A    struct timeval hostTimeval[2];
211411906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
21158737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
21168737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
21171999SN/A    }
21183669Sbinkertn@umich.edu
211913883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
212013883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
21213669Sbinkertn@umich.edu
21221999SN/A    int result = utimes(path.c_str(), hostTimeval);
21231999SN/A
21241999SN/A    if (result < 0)
21251999SN/A        return -errno;
21261999SN/A
21271999SN/A    return 0;
21281999SN/A}
212911886Sbrandon.potter@amd.com
213011886Sbrandon.potter@amd.comtemplate <class OS>
213111886Sbrandon.potter@amd.comSyscallReturn
213213995Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
213311886Sbrandon.potter@amd.com{
213411886Sbrandon.potter@amd.com    desc->setFlags(0);
213513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
213611886Sbrandon.potter@amd.com
213711886Sbrandon.potter@amd.com    int index = 0;
213811886Sbrandon.potter@amd.com    std::string path;
213914024Sgabeblack@google.com    PortProxy & mem_proxy = tc->getVirtProxy();
214011886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
214111886Sbrandon.potter@amd.com        return -EFAULT;
214211886Sbrandon.potter@amd.com
214311886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
214411886Sbrandon.potter@amd.com        return -EACCES;
214511886Sbrandon.potter@amd.com
214614020Sgabeblack@google.com    auto read_in = [](std::vector<std::string> &vect,
214714020Sgabeblack@google.com                      PortProxy &mem_proxy, Addr mem_loc)
214811886Sbrandon.potter@amd.com    {
214911886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
215011886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
215111886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
215211886Sbrandon.potter@amd.com
215311886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
215411886Sbrandon.potter@amd.com                break;
215511886Sbrandon.potter@amd.com
215611886Sbrandon.potter@amd.com            vect.push_back(std::string());
215711886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
215811886Sbrandon.potter@amd.com        }
215911886Sbrandon.potter@amd.com    };
216011886Sbrandon.potter@amd.com
216111886Sbrandon.potter@amd.com    /**
216211886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
216311886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
216411886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
216511886Sbrandon.potter@amd.com     * constructor.
216611886Sbrandon.potter@amd.com     */
216711886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
216811886Sbrandon.potter@amd.com    pp->executable = path;
216911886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
217011886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
217111886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
217211886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
217311886Sbrandon.potter@amd.com    pp->uid = p->uid();
217411886Sbrandon.potter@amd.com    pp->egid = p->egid();
217511886Sbrandon.potter@amd.com    pp->euid = p->euid();
217611886Sbrandon.potter@amd.com    pp->gid = p->gid();
217711886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
217811886Sbrandon.potter@amd.com    pp->pid = p->pid();
217911886Sbrandon.potter@amd.com    pp->input.assign("cin");
218011886Sbrandon.potter@amd.com    pp->output.assign("cout");
218111886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
218213883Sdavid.hashe@amd.com    pp->cwd.assign(p->tgtCwd);
218311886Sbrandon.potter@amd.com    pp->system = p->system;
218411886Sbrandon.potter@amd.com    /**
218511886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
218611886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
218711886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
218811886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
218911886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
219011886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
219111886Sbrandon.potter@amd.com     */
219211886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
219311886Sbrandon.potter@amd.com    Process *new_p = pp->create();
219411886Sbrandon.potter@amd.com    delete pp;
219511886Sbrandon.potter@amd.com
219611886Sbrandon.potter@amd.com    /**
219711886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
219811886Sbrandon.potter@amd.com     * close-on-exec.
219911886Sbrandon.potter@amd.com     */
220011886Sbrandon.potter@amd.com    new_p->fds = p->fds;
220111886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
220211886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
220311886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
220411886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
220511886Sbrandon.potter@amd.com    }
220611886Sbrandon.potter@amd.com
220711886Sbrandon.potter@amd.com    *new_p->sigchld = true;
220811886Sbrandon.potter@amd.com
220911886Sbrandon.potter@amd.com    delete p;
221011886Sbrandon.potter@amd.com    tc->clearArchRegs();
221111886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
221211886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
221311886Sbrandon.potter@amd.com    new_p->initState();
221411886Sbrandon.potter@amd.com    tc->activate();
221511886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
221611886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
221711886Sbrandon.potter@amd.com
221811886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
221911886Sbrandon.potter@amd.com    return 0;
222011886Sbrandon.potter@amd.com}
222111886Sbrandon.potter@amd.com
2222378SN/A/// Target getrusage() function.
2223360SN/Atemplate <class OS>
22241450SN/ASyscallReturn
222513995Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
2226360SN/A{
22276701Sgblack@eecs.umich.edu    int index = 0;
222813995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
22296701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
22306701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
2231360SN/A
22323670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
22333670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
2234360SN/A    rup->ru_stime.tv_sec = 0;
2235360SN/A    rup->ru_stime.tv_usec = 0;
2236360SN/A    rup->ru_maxrss = 0;
2237360SN/A    rup->ru_ixrss = 0;
2238360SN/A    rup->ru_idrss = 0;
2239360SN/A    rup->ru_isrss = 0;
2240360SN/A    rup->ru_minflt = 0;
2241360SN/A    rup->ru_majflt = 0;
2242360SN/A    rup->ru_nswap = 0;
2243360SN/A    rup->ru_inblock = 0;
2244360SN/A    rup->ru_oublock = 0;
2245360SN/A    rup->ru_msgsnd = 0;
2246360SN/A    rup->ru_msgrcv = 0;
2247360SN/A    rup->ru_nsignals = 0;
2248360SN/A    rup->ru_nvcsw = 0;
2249360SN/A    rup->ru_nivcsw = 0;
2250360SN/A
22513670Sbinkertn@umich.edu    switch (who) {
22523670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
225310796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
22548737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
22558737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
22563670Sbinkertn@umich.edu        break;
22573670Sbinkertn@umich.edu
22583670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
22593670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
22603670Sbinkertn@umich.edu        break;
22613670Sbinkertn@umich.edu
22623670Sbinkertn@umich.edu      default:
22633670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
22643670Sbinkertn@umich.edu        // plow ahead
22653670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
22663670Sbinkertn@umich.edu             who);
22673670Sbinkertn@umich.edu    }
22683670Sbinkertn@umich.edu
226914024Sgabeblack@google.com    rup.copyOut(tc->getVirtProxy());
2270360SN/A
22711458SN/A    return 0;
2272360SN/A}
2273360SN/A
22746683Stjones1@inf.ed.ac.uk/// Target times() function.
22756683Stjones1@inf.ed.ac.uktemplate <class OS>
22766683Stjones1@inf.ed.ac.ukSyscallReturn
227713995Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
22786683Stjones1@inf.ed.ac.uk{
22796701Sgblack@eecs.umich.edu    int index = 0;
228013995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
22816701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
22826683Stjones1@inf.ed.ac.uk
22836683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
22847823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
22856683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
22866683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
22876683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
22886683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
22896683Stjones1@inf.ed.ac.uk
22906683Stjones1@inf.ed.ac.uk    // Convert to host endianness
22918737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
22926683Stjones1@inf.ed.ac.uk
22936683Stjones1@inf.ed.ac.uk    // Write back
229414024Sgabeblack@google.com    bufp.copyOut(tc->getVirtProxy());
22956683Stjones1@inf.ed.ac.uk
22966683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
22976683Stjones1@inf.ed.ac.uk    return clocks;
22986683Stjones1@inf.ed.ac.uk}
22992553SN/A
23006684Stjones1@inf.ed.ac.uk/// Target time() function.
23016684Stjones1@inf.ed.ac.uktemplate <class OS>
23026684Stjones1@inf.ed.ac.ukSyscallReturn
230313995Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
23046684Stjones1@inf.ed.ac.uk{
23056684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
230610796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
23076684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
23086684Stjones1@inf.ed.ac.uk
23096701Sgblack@eecs.umich.edu    int index = 0;
231013995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
23116701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
231211321Ssteve.reinhardt@amd.com    if (taddr != 0) {
23136684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
23148737Skoansin.tan@gmail.com        t = TheISA::htog(t);
231514024Sgabeblack@google.com        PortProxy &p = tc->getVirtProxy();
231614010Sgabeblack@google.com        p.writeBlob(taddr, &t, (int)sizeof(typename OS::time_t));
23176684Stjones1@inf.ed.ac.uk    }
23186684Stjones1@inf.ed.ac.uk    return sec;
23196684Stjones1@inf.ed.ac.uk}
23202553SN/A
232111910SBrandon.Potter@amd.comtemplate <class OS>
232211910SBrandon.Potter@amd.comSyscallReturn
232313995Sbrandon.potter@amd.comtgkillFunc(SyscallDesc *desc, int num, ThreadContext *tc)
232411910SBrandon.Potter@amd.com{
232511910SBrandon.Potter@amd.com    int index = 0;
232613995Sbrandon.potter@amd.com    auto process = tc->getProcessPtr();
232711910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
232811910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
232911910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
233011910SBrandon.Potter@amd.com
233111910SBrandon.Potter@amd.com    /**
233211910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
233311910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
233411910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
233511910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
233611910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
233711910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
233811910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
233911910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
234011910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
234111910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
234211910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
234311910SBrandon.Potter@amd.com     * due to PID rollover.
234411910SBrandon.Potter@amd.com     */
234511910SBrandon.Potter@amd.com
234611910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
234711910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
234811910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
234911910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
235011910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
235111910SBrandon.Potter@amd.com            tgt_proc = temp;
235211910SBrandon.Potter@amd.com            break;
235311910SBrandon.Potter@amd.com        }
235411910SBrandon.Potter@amd.com    }
235511910SBrandon.Potter@amd.com
235611910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
235711910SBrandon.Potter@amd.com        return -EINVAL;
235811910SBrandon.Potter@amd.com
235911910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
236011910SBrandon.Potter@amd.com        return -ESRCH;
236111910SBrandon.Potter@amd.com
236211910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
236311910SBrandon.Potter@amd.com        return -ESRCH;
236411910SBrandon.Potter@amd.com
236511910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
236613995Sbrandon.potter@amd.com        exitGroupFunc(desc, 252, tc);
236711910SBrandon.Potter@amd.com
236811910SBrandon.Potter@amd.com    return 0;
236911910SBrandon.Potter@amd.com}
237011910SBrandon.Potter@amd.com
237113568Sbrandon.potter@amd.comtemplate <class OS>
237213568Sbrandon.potter@amd.comSyscallReturn
237313995Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, ThreadContext *tc)
237413568Sbrandon.potter@amd.com{
237513568Sbrandon.potter@amd.com    int index = 0;
237613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
237713568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
237813568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
237913568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
238013568Sbrandon.potter@amd.com
238113568Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
238213568Sbrandon.potter@amd.com    if (sim_fd == -1)
238313568Sbrandon.potter@amd.com        return -errno;
238413568Sbrandon.potter@amd.com
238513568Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
238613568Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
238713568Sbrandon.potter@amd.com
238813568Sbrandon.potter@amd.com    return tgt_fd;
238913568Sbrandon.potter@amd.com}
239013568Sbrandon.potter@amd.com
239113568Sbrandon.potter@amd.comtemplate <class OS>
239213568Sbrandon.potter@amd.comSyscallReturn
239313995Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, ThreadContext *tc)
239413568Sbrandon.potter@amd.com{
239513568Sbrandon.potter@amd.com    int index = 0;
239613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
239713568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
239813568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
239913568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
240013568Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
240113568Sbrandon.potter@amd.com
240213568Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
240313568Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
240413568Sbrandon.potter@amd.com    if (status == -1)
240513568Sbrandon.potter@amd.com        return -errno;
240613568Sbrandon.potter@amd.com
240713568Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
240813568Sbrandon.potter@amd.com
240913568Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
241013568Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
241113568Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
241213568Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
241314024Sgabeblack@google.com    svBuf.copyOut(tc->getVirtProxy());
241413568Sbrandon.potter@amd.com
241513568Sbrandon.potter@amd.com    return status;
241613568Sbrandon.potter@amd.com}
24172553SN/A
241813570Sbrandon.potter@amd.comtemplate <class OS>
241913570Sbrandon.potter@amd.comSyscallReturn
242013995Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, ThreadContext *tc)
242113570Sbrandon.potter@amd.com{
242213570Sbrandon.potter@amd.com    int retval;
242313570Sbrandon.potter@amd.com
242413570Sbrandon.potter@amd.com    int index = 0;
242513995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
242613570Sbrandon.potter@amd.com    int nfds_t = p->getSyscallArg(tc, index);
242713570Sbrandon.potter@amd.com    Addr fds_read_ptr = p->getSyscallArg(tc, index);
242813570Sbrandon.potter@amd.com    Addr fds_writ_ptr = p->getSyscallArg(tc, index);
242913570Sbrandon.potter@amd.com    Addr fds_excp_ptr = p->getSyscallArg(tc, index);
243013570Sbrandon.potter@amd.com    Addr time_val_ptr = p->getSyscallArg(tc, index);
243113570Sbrandon.potter@amd.com
243213570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr);
243313570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr);
243413570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr);
243513570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(time_val_ptr);
243613570Sbrandon.potter@amd.com
243713570Sbrandon.potter@amd.com    /**
243813570Sbrandon.potter@amd.com     * Host fields. Notice that these use the definitions from the system
243913570Sbrandon.potter@amd.com     * headers instead of the gem5 headers and libraries. If the host and
244013570Sbrandon.potter@amd.com     * target have different header file definitions, this will not work.
244113570Sbrandon.potter@amd.com     */
244213570Sbrandon.potter@amd.com    fd_set rd_h;
244313570Sbrandon.potter@amd.com    FD_ZERO(&rd_h);
244413570Sbrandon.potter@amd.com    fd_set wr_h;
244513570Sbrandon.potter@amd.com    FD_ZERO(&wr_h);
244613570Sbrandon.potter@amd.com    fd_set ex_h;
244713570Sbrandon.potter@amd.com    FD_ZERO(&ex_h);
244813570Sbrandon.potter@amd.com
244913570Sbrandon.potter@amd.com    /**
245013570Sbrandon.potter@amd.com     * Copy in the fd_set from the target.
245113570Sbrandon.potter@amd.com     */
245213570Sbrandon.potter@amd.com    if (fds_read_ptr)
245314024Sgabeblack@google.com        rd_t.copyIn(tc->getVirtProxy());
245413570Sbrandon.potter@amd.com    if (fds_writ_ptr)
245514024Sgabeblack@google.com        wr_t.copyIn(tc->getVirtProxy());
245613570Sbrandon.potter@amd.com    if (fds_excp_ptr)
245714024Sgabeblack@google.com        ex_t.copyIn(tc->getVirtProxy());
245813570Sbrandon.potter@amd.com
245913570Sbrandon.potter@amd.com    /**
246013570Sbrandon.potter@amd.com     * We need to translate the target file descriptor set into a host file
246113570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
246213570Sbrandon.potter@amd.com     * and the fd_set defined in Linux header files. The nfds field also
246313570Sbrandon.potter@amd.com     * needs to be updated as it will be only target specific after
246413570Sbrandon.potter@amd.com     * retrieving it from the target; the nfds value is expected to be the
246513570Sbrandon.potter@amd.com     * highest file descriptor that needs to be checked, so we need to extend
246613570Sbrandon.potter@amd.com     * it out for nfds_h when we do the update.
246713570Sbrandon.potter@amd.com     */
246813570Sbrandon.potter@amd.com    int nfds_h = 0;
246913570Sbrandon.potter@amd.com    std::map<int, int> trans_map;
247013570Sbrandon.potter@amd.com    auto try_add_host_set = [&](fd_set *tgt_set_entry,
247113570Sbrandon.potter@amd.com                                fd_set *hst_set_entry,
247213570Sbrandon.potter@amd.com                                int iter) -> bool
247313570Sbrandon.potter@amd.com    {
247413570Sbrandon.potter@amd.com        /**
247513570Sbrandon.potter@amd.com         * By this point, we know that we are looking at a valid file
247613570Sbrandon.potter@amd.com         * descriptor set on the target. We need to check if the target file
247713570Sbrandon.potter@amd.com         * descriptor value passed in as iter is part of the set.
247813570Sbrandon.potter@amd.com         */
247913570Sbrandon.potter@amd.com        if (FD_ISSET(iter, tgt_set_entry)) {
248013570Sbrandon.potter@amd.com            /**
248113570Sbrandon.potter@amd.com             * We know that the target file descriptor belongs to the set,
248213570Sbrandon.potter@amd.com             * but we do not yet know if the file descriptor is valid or
248313570Sbrandon.potter@amd.com             * that we have a host mapping. Check that now.
248413570Sbrandon.potter@amd.com             */
248513570Sbrandon.potter@amd.com            auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]);
248613570Sbrandon.potter@amd.com            if (!hbfdp)
248713570Sbrandon.potter@amd.com                return true;
248813570Sbrandon.potter@amd.com            auto sim_fd = hbfdp->getSimFD();
248913570Sbrandon.potter@amd.com
249013570Sbrandon.potter@amd.com            /**
249113570Sbrandon.potter@amd.com             * Add the sim_fd to tgt_fd translation into trans_map for use
249213570Sbrandon.potter@amd.com             * later when we need to zero the target fd_set structures and
249313570Sbrandon.potter@amd.com             * then update them with hits returned from the host select call.
249413570Sbrandon.potter@amd.com             */
249513570Sbrandon.potter@amd.com            trans_map[sim_fd] = iter;
249613570Sbrandon.potter@amd.com
249713570Sbrandon.potter@amd.com            /**
249813570Sbrandon.potter@amd.com             * We know that the host file descriptor exists so now we check
249913570Sbrandon.potter@amd.com             * if we need to update the max count for nfds_h before passing
250013570Sbrandon.potter@amd.com             * the duplicated structure into the host.
250113570Sbrandon.potter@amd.com             */
250213570Sbrandon.potter@amd.com            nfds_h = std::max(nfds_h - 1, sim_fd + 1);
250313570Sbrandon.potter@amd.com
250413570Sbrandon.potter@amd.com            /**
250513570Sbrandon.potter@amd.com             * Add the host file descriptor to the set that we are going to
250613570Sbrandon.potter@amd.com             * pass into the host.
250713570Sbrandon.potter@amd.com             */
250813570Sbrandon.potter@amd.com            FD_SET(sim_fd, hst_set_entry);
250913570Sbrandon.potter@amd.com        }
251013570Sbrandon.potter@amd.com        return false;
251113570Sbrandon.potter@amd.com    };
251213570Sbrandon.potter@amd.com
251313570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_t; i++) {
251413570Sbrandon.potter@amd.com        if (fds_read_ptr) {
251513570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i);
251613570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
251713570Sbrandon.potter@amd.com        }
251813570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
251913570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i);
252013570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
252113570Sbrandon.potter@amd.com        }
252213570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
252313570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i);
252413570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
252513570Sbrandon.potter@amd.com        }
252613570Sbrandon.potter@amd.com    }
252713570Sbrandon.potter@amd.com
252813570Sbrandon.potter@amd.com    if (time_val_ptr) {
252913570Sbrandon.potter@amd.com        /**
253013570Sbrandon.potter@amd.com         * It might be possible to decrement the timeval based on some
253113570Sbrandon.potter@amd.com         * derivation of wall clock determined from elapsed simulator ticks
253213570Sbrandon.potter@amd.com         * but that seems like overkill. Rather, we just set the timeval with
253313570Sbrandon.potter@amd.com         * zero timeout. (There is no reason to block during the simulation
253413570Sbrandon.potter@amd.com         * as it only decreases simulator performance.)
253513570Sbrandon.potter@amd.com         */
253613570Sbrandon.potter@amd.com        tp->tv_sec = 0;
253713570Sbrandon.potter@amd.com        tp->tv_usec = 0;
253813570Sbrandon.potter@amd.com
253913570Sbrandon.potter@amd.com        retval = select(nfds_h,
254013570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
254113570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
254213570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
254313570Sbrandon.potter@amd.com                        (timeval*)&*tp);
254413570Sbrandon.potter@amd.com    } else {
254513570Sbrandon.potter@amd.com        /**
254613570Sbrandon.potter@amd.com         * If the timeval pointer is null, setup a new timeval structure to
254713570Sbrandon.potter@amd.com         * pass into the host select call. Unfortunately, we will need to
254813570Sbrandon.potter@amd.com         * manually check the return value and throw a retry fault if the
254913570Sbrandon.potter@amd.com         * return value is zero. Allowing the system call to block will
255013570Sbrandon.potter@amd.com         * likely deadlock the event queue.
255113570Sbrandon.potter@amd.com         */
255213570Sbrandon.potter@amd.com        struct timeval tv = { 0, 0 };
255313570Sbrandon.potter@amd.com
255413570Sbrandon.potter@amd.com        retval = select(nfds_h,
255513570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
255613570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
255713570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
255813570Sbrandon.potter@amd.com                        &tv);
255913570Sbrandon.potter@amd.com
256013570Sbrandon.potter@amd.com        if (retval == 0) {
256113570Sbrandon.potter@amd.com            /**
256213570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
256313570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try to
256413570Sbrandon.potter@amd.com             * return the signal interrupt instead.
256513570Sbrandon.potter@amd.com             */
256613570Sbrandon.potter@amd.com            for (auto sig : tc->getSystemPtr()->signalList)
256713570Sbrandon.potter@amd.com                if (sig.receiver == p)
256813570Sbrandon.potter@amd.com                    return -EINTR;
256913570Sbrandon.potter@amd.com            return SyscallReturn::retry();
257013570Sbrandon.potter@amd.com        }
257113570Sbrandon.potter@amd.com    }
257213570Sbrandon.potter@amd.com
257313570Sbrandon.potter@amd.com    if (retval == -1)
257413570Sbrandon.potter@amd.com        return -errno;
257513570Sbrandon.potter@amd.com
257613570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*rd_t);
257713570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*wr_t);
257813570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*ex_t);
257913570Sbrandon.potter@amd.com
258013570Sbrandon.potter@amd.com    /**
258113570Sbrandon.potter@amd.com     * We need to translate the host file descriptor set into a target file
258213570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
258313570Sbrandon.potter@amd.com     * and the fd_set defined in header files.
258413570Sbrandon.potter@amd.com     */
258513570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_h; i++) {
258613570Sbrandon.potter@amd.com        if (fds_read_ptr) {
258713570Sbrandon.potter@amd.com            if (FD_ISSET(i, &rd_h))
258813570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*rd_t);
258913570Sbrandon.potter@amd.com        }
259013570Sbrandon.potter@amd.com
259113570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
259213570Sbrandon.potter@amd.com            if (FD_ISSET(i, &wr_h))
259313570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*wr_t);
259413570Sbrandon.potter@amd.com        }
259513570Sbrandon.potter@amd.com
259613570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
259713570Sbrandon.potter@amd.com            if (FD_ISSET(i, &ex_h))
259813570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*ex_t);
259913570Sbrandon.potter@amd.com        }
260013570Sbrandon.potter@amd.com    }
260113570Sbrandon.potter@amd.com
260213570Sbrandon.potter@amd.com    if (fds_read_ptr)
260314024Sgabeblack@google.com        rd_t.copyOut(tc->getVirtProxy());
260413570Sbrandon.potter@amd.com    if (fds_writ_ptr)
260514024Sgabeblack@google.com        wr_t.copyOut(tc->getVirtProxy());
260613570Sbrandon.potter@amd.com    if (fds_excp_ptr)
260714024Sgabeblack@google.com        ex_t.copyOut(tc->getVirtProxy());
260813570Sbrandon.potter@amd.com    if (time_val_ptr)
260914024Sgabeblack@google.com        tp.copyOut(tc->getVirtProxy());
261013570Sbrandon.potter@amd.com
261113570Sbrandon.potter@amd.com    return retval;
261213570Sbrandon.potter@amd.com}
261313570Sbrandon.potter@amd.com
261413570Sbrandon.potter@amd.comtemplate <class OS>
261513570Sbrandon.potter@amd.comSyscallReturn
261613995Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, ThreadContext *tc)
261713570Sbrandon.potter@amd.com{
261813570Sbrandon.potter@amd.com    int index = 0;
261913995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
262013570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
262113570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
262213570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
262313570Sbrandon.potter@amd.com
262413570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
262513570Sbrandon.potter@amd.com    if (!hbfdp)
262613570Sbrandon.potter@amd.com        return -EBADF;
262713570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
262813570Sbrandon.potter@amd.com
262913570Sbrandon.potter@amd.com    struct pollfd pfd;
263013570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
263113570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
263213570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
263313570Sbrandon.potter@amd.com        && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK))
263413570Sbrandon.potter@amd.com        return SyscallReturn::retry();
263513570Sbrandon.potter@amd.com
263613570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
263713570Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes);
263813570Sbrandon.potter@amd.com
263913570Sbrandon.potter@amd.com    if (bytes_read > 0)
264014024Sgabeblack@google.com        buf_arg.copyOut(tc->getVirtProxy());
264113570Sbrandon.potter@amd.com
264213570Sbrandon.potter@amd.com    return (bytes_read == -1) ? -errno : bytes_read;
264313570Sbrandon.potter@amd.com}
264413570Sbrandon.potter@amd.com
264513570Sbrandon.potter@amd.comtemplate <class OS>
264613570Sbrandon.potter@amd.comSyscallReturn
264713995Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, ThreadContext *tc)
264813570Sbrandon.potter@amd.com{
264913570Sbrandon.potter@amd.com    int index = 0;
265013995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
265113570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
265213570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
265313570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
265413570Sbrandon.potter@amd.com
265513570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
265613570Sbrandon.potter@amd.com    if (!hbfdp)
265713570Sbrandon.potter@amd.com        return -EBADF;
265813570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
265913570Sbrandon.potter@amd.com
266013570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
266114024Sgabeblack@google.com    buf_arg.copyIn(tc->getVirtProxy());
266213570Sbrandon.potter@amd.com
266313570Sbrandon.potter@amd.com    struct pollfd pfd;
266413570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
266513570Sbrandon.potter@amd.com    pfd.events = POLLOUT;
266613570Sbrandon.potter@amd.com
266713570Sbrandon.potter@amd.com    /**
266813570Sbrandon.potter@amd.com     * We don't want to poll on /dev/random. The kernel will not enable the
266913570Sbrandon.potter@amd.com     * file descriptor for writing unless the entropy in the system falls
267013570Sbrandon.potter@amd.com     * below write_wakeup_threshold. This is not guaranteed to happen
267113570Sbrandon.potter@amd.com     * depending on host settings.
267213570Sbrandon.potter@amd.com     */
267313570Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp);
267413570Sbrandon.potter@amd.com    if (ffdp && (ffdp->getFileName() != "/dev/random")) {
267513570Sbrandon.potter@amd.com        if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK))
267613570Sbrandon.potter@amd.com            return SyscallReturn::retry();
267713570Sbrandon.potter@amd.com    }
267813570Sbrandon.potter@amd.com
267913570Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes);
268013570Sbrandon.potter@amd.com
268113570Sbrandon.potter@amd.com    if (bytes_written != -1)
268213570Sbrandon.potter@amd.com        fsync(sim_fd);
268313570Sbrandon.potter@amd.com
268413570Sbrandon.potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
268513570Sbrandon.potter@amd.com}
268613570Sbrandon.potter@amd.com
268713570Sbrandon.potter@amd.comtemplate <class OS>
268813570Sbrandon.potter@amd.comSyscallReturn
268913995Sbrandon.potter@amd.comwait4Func(SyscallDesc *desc, int num, ThreadContext *tc)
269013570Sbrandon.potter@amd.com{
269113570Sbrandon.potter@amd.com    int index = 0;
269213995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
269313570Sbrandon.potter@amd.com    pid_t pid = p->getSyscallArg(tc, index);
269413570Sbrandon.potter@amd.com    Addr statPtr = p->getSyscallArg(tc, index);
269513570Sbrandon.potter@amd.com    int options = p->getSyscallArg(tc, index);
269613570Sbrandon.potter@amd.com    Addr rusagePtr = p->getSyscallArg(tc, index);
269713570Sbrandon.potter@amd.com
269813570Sbrandon.potter@amd.com    if (rusagePtr)
269913907Salexandru.dutu@amd.com        DPRINTF_SYSCALL(Verbose, "wait4: rusage pointer provided %lx, however "
270013570Sbrandon.potter@amd.com                 "functionality not supported. Ignoring rusage pointer.\n",
270113907Salexandru.dutu@amd.com                 rusagePtr);
270213570Sbrandon.potter@amd.com
270313570Sbrandon.potter@amd.com    /**
270413570Sbrandon.potter@amd.com     * Currently, wait4 is only implemented so that it will wait for children
270513570Sbrandon.potter@amd.com     * exit conditions which are denoted by a SIGCHLD signals posted into the
270613570Sbrandon.potter@amd.com     * system signal list. We return no additional information via any of the
270713570Sbrandon.potter@amd.com     * parameters supplied to wait4. If nothing is found in the system signal
270813570Sbrandon.potter@amd.com     * list, we will wait indefinitely for SIGCHLD to post by retrying the
270913570Sbrandon.potter@amd.com     * call.
271013570Sbrandon.potter@amd.com     */
271113570Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
271213570Sbrandon.potter@amd.com    std::list<BasicSignal>::iterator iter;
271313570Sbrandon.potter@amd.com    for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) {
271413570Sbrandon.potter@amd.com        if (iter->receiver == p) {
271513570Sbrandon.potter@amd.com            if (pid < -1) {
271613570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == -pid)
271713570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
271813570Sbrandon.potter@amd.com                    goto success;
271913570Sbrandon.potter@amd.com            } else if (pid == -1) {
272013570Sbrandon.potter@amd.com                if (iter->signalValue == OS::TGT_SIGCHLD)
272113570Sbrandon.potter@amd.com                    goto success;
272213570Sbrandon.potter@amd.com            } else if (pid == 0) {
272313570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == p->pgid())
272413570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
272513570Sbrandon.potter@amd.com                    goto success;
272613570Sbrandon.potter@amd.com            } else {
272713570Sbrandon.potter@amd.com                if ((iter->sender->pid() == pid)
272813570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
272913570Sbrandon.potter@amd.com                    goto success;
273013570Sbrandon.potter@amd.com            }
273113570Sbrandon.potter@amd.com        }
273213570Sbrandon.potter@amd.com    }
273313570Sbrandon.potter@amd.com
273413570Sbrandon.potter@amd.com    return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry();
273513570Sbrandon.potter@amd.com
273613570Sbrandon.potter@amd.comsuccess:
273713570Sbrandon.potter@amd.com    // Set status to EXITED for WIFEXITED evaluations.
273813570Sbrandon.potter@amd.com    const int EXITED = 0;
273913570Sbrandon.potter@amd.com    BufferArg statusBuf(statPtr, sizeof(int));
274013570Sbrandon.potter@amd.com    *(int *)statusBuf.bufferPtr() = EXITED;
274114024Sgabeblack@google.com    statusBuf.copyOut(tc->getVirtProxy());
274213570Sbrandon.potter@amd.com
274313570Sbrandon.potter@amd.com    // Return the child PID.
274413570Sbrandon.potter@amd.com    pid_t retval = iter->sender->pid();
274513570Sbrandon.potter@amd.com    sysh->signalList.erase(iter);
274613570Sbrandon.potter@amd.com    return retval;
274713570Sbrandon.potter@amd.com}
274813570Sbrandon.potter@amd.com
274913570Sbrandon.potter@amd.comtemplate <class OS>
275013570Sbrandon.potter@amd.comSyscallReturn
275113995Sbrandon.potter@amd.comacceptFunc(SyscallDesc *desc, int num, ThreadContext *tc)
275213570Sbrandon.potter@amd.com{
275313570Sbrandon.potter@amd.com    struct sockaddr sa;
275413570Sbrandon.potter@amd.com    socklen_t addrLen;
275513570Sbrandon.potter@amd.com    int host_fd;
275613570Sbrandon.potter@amd.com    int index = 0;
275713995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
275813570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
275913570Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
276013570Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
276113570Sbrandon.potter@amd.com
276213570Sbrandon.potter@amd.com    BufferArg *lenBufPtr = nullptr;
276313570Sbrandon.potter@amd.com    BufferArg *addrBufPtr = nullptr;
276413570Sbrandon.potter@amd.com
276513570Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
276613570Sbrandon.potter@amd.com    if (!sfdp)
276713570Sbrandon.potter@amd.com        return -EBADF;
276813570Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
276913570Sbrandon.potter@amd.com
277013570Sbrandon.potter@amd.com    /**
277113570Sbrandon.potter@amd.com     * We poll the socket file descriptor first to guarantee that we do not
277213570Sbrandon.potter@amd.com     * block on our accept call. The socket can be opened without the
277313570Sbrandon.potter@amd.com     * non-blocking flag (it blocks). This will cause deadlocks between
277413570Sbrandon.potter@amd.com     * communicating processes.
277513570Sbrandon.potter@amd.com     */
277613570Sbrandon.potter@amd.com    struct pollfd pfd;
277713570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
277813570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
277913570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
278013570Sbrandon.potter@amd.com        && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK))
278113570Sbrandon.potter@amd.com        return SyscallReturn::retry();
278213570Sbrandon.potter@amd.com
278313570Sbrandon.potter@amd.com    if (lenPtr) {
278413570Sbrandon.potter@amd.com        lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t));
278514024Sgabeblack@google.com        lenBufPtr->copyIn(tc->getVirtProxy());
278613570Sbrandon.potter@amd.com        memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(),
278713570Sbrandon.potter@amd.com               sizeof(socklen_t));
278813570Sbrandon.potter@amd.com    }
278913570Sbrandon.potter@amd.com
279013570Sbrandon.potter@amd.com    if (addrPtr) {
279113570Sbrandon.potter@amd.com        addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr));
279214024Sgabeblack@google.com        addrBufPtr->copyIn(tc->getVirtProxy());
279313570Sbrandon.potter@amd.com        memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(),
279413570Sbrandon.potter@amd.com               sizeof(struct sockaddr));
279513570Sbrandon.potter@amd.com    }
279613570Sbrandon.potter@amd.com
279713570Sbrandon.potter@amd.com    host_fd = accept(sim_fd, &sa, &addrLen);
279813570Sbrandon.potter@amd.com
279913570Sbrandon.potter@amd.com    if (host_fd == -1)
280013570Sbrandon.potter@amd.com        return -errno;
280113570Sbrandon.potter@amd.com
280213570Sbrandon.potter@amd.com    if (addrPtr) {
280313570Sbrandon.potter@amd.com        memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa));
280414024Sgabeblack@google.com        addrBufPtr->copyOut(tc->getVirtProxy());
280513570Sbrandon.potter@amd.com        delete(addrBufPtr);
280613570Sbrandon.potter@amd.com    }
280713570Sbrandon.potter@amd.com
280813570Sbrandon.potter@amd.com    if (lenPtr) {
280913570Sbrandon.potter@amd.com        *(socklen_t *)lenBufPtr->bufferPtr() = addrLen;
281014024Sgabeblack@google.com        lenBufPtr->copyOut(tc->getVirtProxy());
281113570Sbrandon.potter@amd.com        delete(lenBufPtr);
281213570Sbrandon.potter@amd.com    }
281313570Sbrandon.potter@amd.com
281413570Sbrandon.potter@amd.com    auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain,
281513570Sbrandon.potter@amd.com                                                sfdp->_type, sfdp->_protocol);
281613570Sbrandon.potter@amd.com    return p->fds->allocFD(afdp);
281713570Sbrandon.potter@amd.com}
281813570Sbrandon.potter@amd.com
281913933Sbrandon.potter@amd.com/// Target eventfd() function.
282013933Sbrandon.potter@amd.comtemplate <class OS>
282113933Sbrandon.potter@amd.comSyscallReturn
282213995Sbrandon.potter@amd.comeventfdFunc(SyscallDesc *desc, int num, ThreadContext *tc)
282313933Sbrandon.potter@amd.com{
282413936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
282513933Sbrandon.potter@amd.com    int index = 0;
282613995Sbrandon.potter@amd.com    auto p = tc->getProcessPtr();
282713933Sbrandon.potter@amd.com    unsigned initval = p->getSyscallArg(tc, index);
282813933Sbrandon.potter@amd.com    int in_flags = p->getSyscallArg(tc, index);
282913933Sbrandon.potter@amd.com
283013933Sbrandon.potter@amd.com    int sim_fd = eventfd(initval, in_flags);
283113933Sbrandon.potter@amd.com    if (sim_fd == -1)
283213933Sbrandon.potter@amd.com        return -errno;
283313933Sbrandon.potter@amd.com
283413933Sbrandon.potter@amd.com    bool cloexec = in_flags & OS::TGT_O_CLOEXEC;
283513933Sbrandon.potter@amd.com
283613933Sbrandon.potter@amd.com    int flags = cloexec ? OS::TGT_O_CLOEXEC : 0;
283713933Sbrandon.potter@amd.com    flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0;
283813933Sbrandon.potter@amd.com
283913933Sbrandon.potter@amd.com    auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec);
284013933Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(hbfdp);
284113933Sbrandon.potter@amd.com    return tgt_fd;
284213933Sbrandon.potter@amd.com#else
284313933Sbrandon.potter@amd.com    warnUnsupportedOS("eventfd");
284413933Sbrandon.potter@amd.com    return -1;
284513933Sbrandon.potter@amd.com#endif
284613933Sbrandon.potter@amd.com}
284713933Sbrandon.potter@amd.com
28481354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2849