syscall_emul.hh revision 13534
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
5612018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
5712018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__NetBSD__))
5812018Sandreas.sandberg@arm.com#define NO_STATFS 1
5912018Sandreas.sandberg@arm.com#else
6012018Sandreas.sandberg@arm.com#define NO_STATFS 0
6112018Sandreas.sandberg@arm.com#endif
6211799Sbrandon.potter@amd.com
6312018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
6412018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__NetBSD__))
6512018Sandreas.sandberg@arm.com#define NO_FALLOCATE 1
6612018Sandreas.sandberg@arm.com#else
6712018Sandreas.sandberg@arm.com#define NO_FALLOCATE 0
6812018Sandreas.sandberg@arm.com#endif
6911799Sbrandon.potter@amd.com
70360SN/A///
71360SN/A/// @file syscall_emul.hh
72360SN/A///
73360SN/A/// This file defines objects used to emulate syscalls from the target
74360SN/A/// application on the host machine.
75360SN/A
761809SN/A#ifdef __CYGWIN32__
7711800Sbrandon.potter@amd.com#include <sys/fcntl.h>
7811392Sbrandon.potter@amd.com
791809SN/A#endif
8011392Sbrandon.potter@amd.com#include <fcntl.h>
8111383Sbrandon.potter@amd.com#include <sys/mman.h>
823113Sgblack@eecs.umich.edu#include <sys/stat.h>
8311799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
8411759Sbrandon.potter@amd.com#include <sys/statfs.h>
8511812Sbaz21@cam.ac.uk#else
8611812Sbaz21@cam.ac.uk#include <sys/mount.h>
8711799Sbrandon.potter@amd.com#endif
888229Snate@binkert.org#include <sys/time.h>
898229Snate@binkert.org#include <sys/uio.h>
9011594Santhony.gutierrez@amd.com#include <unistd.h>
917075Snate@binkert.org
928229Snate@binkert.org#include <cerrno>
9311856Sbrandon.potter@amd.com#include <memory>
947075Snate@binkert.org#include <string>
95360SN/A
9612461Sgabeblack@google.com#include "arch/generic/tlb.hh"
9711886Sbrandon.potter@amd.com#include "arch/utility.hh"
9811800Sbrandon.potter@amd.com#include "base/intmath.hh"
9911392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
10012334Sgabeblack@google.com#include "base/logging.hh"
1011354SN/A#include "base/trace.hh"
1026216Snate@binkert.org#include "base/types.hh"
1036658Snate@binkert.org#include "config/the_isa.hh"
1042474SN/A#include "cpu/base.hh"
1052680Sktlim@umich.edu#include "cpu/thread_context.hh"
1068229Snate@binkert.org#include "mem/page_table.hh"
10711886Sbrandon.potter@amd.com#include "params/Process.hh"
10810496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
10911911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1108229Snate@binkert.org#include "sim/process.hh"
11111794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
11211886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
11310497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
11411794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
115360SN/A
116360SN/A//////////////////////////////////////////////////////////////////////
117360SN/A//
118360SN/A// The following emulation functions are generic enough that they
119360SN/A// don't need to be recompiled for different emulated OS's.  They are
120360SN/A// defined in sim/syscall_emul.cc.
121360SN/A//
122360SN/A//////////////////////////////////////////////////////////////////////
123360SN/A
124360SN/A
125378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1261706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
12711851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
128378SN/A
129378SN/A/// Handler for unimplemented syscalls that we never intend to
130378SN/A/// implement (signal handling, etc.) and should not affect the correct
131378SN/A/// behavior of the program.  Print a warning only if the appropriate
132378SN/A/// trace flag is enabled.  Return success to the target program.
1331706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
13411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
135360SN/A
13611760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
13711760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
13811851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
13911760Sbrandon.potter@amd.com
1406109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1411706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14211851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
143378SN/A
1446109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1456109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
14611851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1476109Ssanchezd@stanford.edu
14811886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
14911886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
15011886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15111886Sbrandon.potter@amd.com
152378SN/A/// Target getpagesize() handler.
1531706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
15411851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
155378SN/A
1565748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1575748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
15811851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
159378SN/A
160378SN/A/// Target close() handler.
1611706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16211851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
163378SN/A
16411886Sbrandon.potter@amd.com// Target read() handler.
1651706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
16611851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
167378SN/A
168378SN/A/// Target write() handler.
1691706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
17011851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
171378SN/A
172378SN/A/// Target lseek() handler.
1731706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
17411851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
175378SN/A
1764118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1774118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17811851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1794118Sgblack@eecs.umich.edu
180378SN/A/// Target munmap() handler.
1811706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
18211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
183378SN/A
184378SN/A/// Target gethostname() handler.
1851706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
18611851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
187360SN/A
1885513SMichael.Adler@intel.com/// Target getcwd() handler.
1895513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
19011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1915513SMichael.Adler@intel.com
19210203SAli.Saidi@ARM.com/// Target readlink() handler.
19310203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19411851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
19510203SAli.Saidi@ARM.com                           int index = 0);
1965513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19711851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
1985513SMichael.Adler@intel.com
199511SN/A/// Target unlink() handler.
20010633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20111851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20210633Smichaelupton@gmail.com                           int index);
2031706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
205511SN/A
20612795Smattdsinclair@gmail.com/// Target link() handler
20712795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
20812795Smattdsinclair@gmail.com                       ThreadContext *tc);
20912795Smattdsinclair@gmail.com
21012796Smattdsinclair@gmail.com/// Target symlink() handler.
21112796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
21212796Smattdsinclair@gmail.com                          ThreadContext *tc);
21312796Smattdsinclair@gmail.com
2145513SMichael.Adler@intel.com/// Target mkdir() handler.
2155513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
21611851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2175513SMichael.Adler@intel.com
21813031Sbrandon.potter@amd.com/// Target mknod() handler.
21913031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
22013031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22113031Sbrandon.potter@amd.com
22213031Sbrandon.potter@amd.com/// Target chdir() handler.
22313031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
22413031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22513031Sbrandon.potter@amd.com
22613031Sbrandon.potter@amd.com// Target rmdir() handler.
22713031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
22813031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22913031Sbrandon.potter@amd.com
230511SN/A/// Target rename() handler.
2311706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
23211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2331706SN/A
2341706SN/A
2351706SN/A/// Target truncate() handler.
2361706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
23711851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2381706SN/A
2391706SN/A
2401706SN/A/// Target ftruncate() handler.
2411706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
24211851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2431706SN/A
244511SN/A
2456703Svince@csl.cornell.edu/// Target truncate64() handler.
2466703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
24711851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2486703Svince@csl.cornell.edu
2496685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2506685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
25111851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2526685Stjones1@inf.ed.ac.uk
2536685Stjones1@inf.ed.ac.uk
2545513SMichael.Adler@intel.com/// Target umask() handler.
2555513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
25611851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2575513SMichael.Adler@intel.com
25811885Sbrandon.potter@amd.com/// Target gettid() handler.
25911885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
26011885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2615513SMichael.Adler@intel.com
2621999SN/A/// Target chown() handler.
2631999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26411851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2651999SN/A
26611885Sbrandon.potter@amd.com/// Target setpgid() handler.
26711885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
26811885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2691999SN/A
2701999SN/A/// Target fchown() handler.
2711999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
27211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2731999SN/A
2743079Sstever@eecs.umich.edu/// Target dup() handler.
2753079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
27611851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2773079Sstever@eecs.umich.edu
27811908SBrandon.Potter@amd.com/// Target dup2() handler.
27911908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
28011908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
28111908SBrandon.Potter@amd.com
28211875Sbrandon.potter@amd.com/// Target fcntl() handler.
2832093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28411851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2852093SN/A
2862687Sksewell@umich.edu/// Target fcntl64() handler.
2872687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
28811851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2892687Sksewell@umich.edu
2902238SN/A/// Target setuid() handler.
2912238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
29211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2932238SN/A
29411908SBrandon.Potter@amd.com/// Target pipe() handler.
29511908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
29611908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
29711908SBrandon.Potter@amd.com
29811908SBrandon.Potter@amd.com/// Internal pipe() handler.
29911908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
30011908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
30111908SBrandon.Potter@amd.com
3022238SN/A/// Target getpid() handler.
3032238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3052238SN/A
30613448Sciro.santilli@arm.com#if defined(SYS_getdents)
30713031Sbrandon.potter@amd.com// Target getdents() handler.
30813031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
30913031Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
31013448Sciro.santilli@arm.com#endif
31113031Sbrandon.potter@amd.com
31213031Sbrandon.potter@amd.com// Target getuid() handler.
3132238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
31411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3152238SN/A
3162238SN/A/// Target getgid() handler.
3172238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
31811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3192238SN/A
3202238SN/A/// Target getppid() handler.
3212238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
32211851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3232238SN/A
3242238SN/A/// Target geteuid() handler.
3252238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
32611851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3272238SN/A
3282238SN/A/// Target getegid() handler.
3292238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
33011851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3312238SN/A
3329455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3339455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
33411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
33510203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
33611851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
33711851Sbrandon.potter@amd.com                         int index);
3389455Smitch.hayenga+gem5@gmail.com
3399112Smarc.orr@gmail.com/// Futex system call
34011906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
34111906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3429112Smarc.orr@gmail.comtemplate <class OS>
3439112Smarc.orr@gmail.comSyscallReturn
34411851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3459112Smarc.orr@gmail.com          ThreadContext *tc)
3469112Smarc.orr@gmail.com{
34711911SBrandon.Potter@amd.com    using namespace std;
3489112Smarc.orr@gmail.com
34911911SBrandon.Potter@amd.com    int index = 0;
35011911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
35111911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
35211911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
3539112Smarc.orr@gmail.com
35411911SBrandon.Potter@amd.com    /*
35511911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
35611911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
35711911SBrandon.Potter@amd.com     */
3589238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3599112Smarc.orr@gmail.com
36011911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
3619112Smarc.orr@gmail.com
36211911SBrandon.Potter@amd.com    if (OS::TGT_FUTEX_WAIT == op) {
36311911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
36411911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
36511911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
36611911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
3679112Smarc.orr@gmail.com
36811911SBrandon.Potter@amd.com        /*
36911911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
37011911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
37111911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
37211911SBrandon.Potter@amd.com         */
37311911SBrandon.Potter@amd.com        if (val != mem_val)
3749112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3759112Smarc.orr@gmail.com
37611911SBrandon.Potter@amd.com        futex_map.suspend(uaddr, process->tgid(), tc);
37711911SBrandon.Potter@amd.com
3789112Smarc.orr@gmail.com        return 0;
37911911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
38011911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
3819112Smarc.orr@gmail.com    }
3829112Smarc.orr@gmail.com
38311911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
38411911SBrandon.Potter@amd.com    return -ENOSYS;
3859112Smarc.orr@gmail.com}
3869112Smarc.orr@gmail.com
3872238SN/A
3882238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3892238SN/A/// returning a second value in a register other than the normal return register
3902238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
39111851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
3922238SN/A
3932238SN/A/// Target getpidPseudo() handler.
3942238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
39511851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3962238SN/A
3972238SN/A/// Target getuidPseudo() handler.
3982238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
39911851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4002238SN/A
4012238SN/A/// Target getgidPseudo() handler.
4022238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
40311851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4042238SN/A
4052238SN/A
4061354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4071354SN/Aconst int one_million = 1000000;
40810796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
40910796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4101354SN/A
4111354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4121354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4131354SN/A/// real-world time) to keep simulations repeatable.
4141354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4151354SN/A
4161354SN/A/// Helper function to convert current elapsed time to seconds and
4171354SN/A/// microseconds.
4181354SN/Atemplate <class T1, class T2>
4191354SN/Avoid
42010796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4211354SN/A{
42210796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4231354SN/A    sec = elapsed_usecs / one_million;
4241354SN/A    usec = elapsed_usecs % one_million;
4251354SN/A}
4261354SN/A
42710796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
42810796Sbrandon.potter@amd.com/// nanoseconds.
42910796Sbrandon.potter@amd.comtemplate <class T1, class T2>
43010796Sbrandon.potter@amd.comvoid
43110796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
43210796Sbrandon.potter@amd.com{
43310796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
43410796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
43510796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
43610796Sbrandon.potter@amd.com}
43710796Sbrandon.potter@amd.com
438360SN/A//////////////////////////////////////////////////////////////////////
439360SN/A//
440360SN/A// The following emulation functions are generic, but need to be
441360SN/A// templated to account for differences in types, constants, etc.
442360SN/A//
443360SN/A//////////////////////////////////////////////////////////////////////
444360SN/A
44511759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4463113Sgblack@eecs.umich.edu#if NO_STAT64
4473113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4483113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4493113Sgblack@eecs.umich.edu#else
4503113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4513113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4523113Sgblack@eecs.umich.edu#endif
4533113Sgblack@eecs.umich.edu
4543113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4553113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4563113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4573113Sgblack@eecs.umich.edu
4583113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
45912032Sandreas.sandberg@arm.comvoid
4603113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4613113Sgblack@eecs.umich.edu{
4624189Sgblack@eecs.umich.edu    using namespace TheISA;
4634189Sgblack@eecs.umich.edu
4643113Sgblack@eecs.umich.edu    if (fakeTTY)
4653113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4663113Sgblack@eecs.umich.edu    else
4673113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4688737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4693113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4708737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4713277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4725515SMichael.Adler@intel.com    if (fakeTTY) {
4735515SMichael.Adler@intel.com        // Claim to be a character device
4745515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4755515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4765515SMichael.Adler@intel.com    }
4778737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4783277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4798737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4803277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4818737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4823277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4838737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4843113Sgblack@eecs.umich.edu    if (fakeTTY)
4853113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4863113Sgblack@eecs.umich.edu    else
4873113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4888737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4893113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4908737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4913114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4928737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4933114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4948737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4953114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4968737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
49711906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
4984061Sgblack@eecs.umich.edu    // consistently across different hosts.
4994061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5008737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5013113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5028737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5033113Sgblack@eecs.umich.edu}
5043113Sgblack@eecs.umich.edu
5053113Sgblack@eecs.umich.edu// Same for stat64
5063113Sgblack@eecs.umich.edu
5073113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
50812032Sandreas.sandberg@arm.comvoid
5093113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5103113Sgblack@eecs.umich.edu{
5114189Sgblack@eecs.umich.edu    using namespace TheISA;
5124189Sgblack@eecs.umich.edu
5133113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5143113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5153113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5168737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5173113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5188737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5193113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5208737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5213113Sgblack@eecs.umich.edu#else
5223113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5233113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5243113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5253113Sgblack@eecs.umich.edu#endif
5263113Sgblack@eecs.umich.edu}
5273113Sgblack@eecs.umich.edu
52811906SBrandon.Potter@amd.com// Here are a couple of convenience functions
5293113Sgblack@eecs.umich.edutemplate<class OS>
53012032Sandreas.sandberg@arm.comvoid
5318852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
53211906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
5333113Sgblack@eecs.umich.edu{
5343113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5353113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5363113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5373113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5383113Sgblack@eecs.umich.edu}
5393113Sgblack@eecs.umich.edu
5403113Sgblack@eecs.umich.edutemplate<class OS>
54112032Sandreas.sandberg@arm.comvoid
5428852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
54311906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
5443113Sgblack@eecs.umich.edu{
5453113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5463113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5476686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5483113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5493113Sgblack@eecs.umich.edu}
5503113Sgblack@eecs.umich.edu
55111759Sbrandon.potter@amd.comtemplate <class OS>
55212032Sandreas.sandberg@arm.comvoid
55311759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
55411759Sbrandon.potter@amd.com                 hst_statfs *host)
55511759Sbrandon.potter@amd.com{
55611759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
55711759Sbrandon.potter@amd.com
55811812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
55911812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
56011812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
56111759Sbrandon.potter@amd.com#else
56211812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
56311759Sbrandon.potter@amd.com#endif
56411759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
56511759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
56611759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
56711759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
56811759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
56911759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
57011812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
57111812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
57211812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
57311812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
57411812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
57511812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
57611812Sbaz21@cam.ac.uk#else
57711759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
57811759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
57911812Sbaz21@cam.ac.uk#endif
58011812Sbaz21@cam.ac.uk#if defined(__linux__)
58111759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
58211812Sbaz21@cam.ac.uk#else
58311812Sbaz21@cam.ac.uk    /*
58411812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
58511812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
58611812Sbaz21@cam.ac.uk     */
58711812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
58811812Sbaz21@cam.ac.uk#endif
58911759Sbrandon.potter@amd.com
59011759Sbrandon.potter@amd.com    tgt.copyOut(mem);
59111759Sbrandon.potter@amd.com}
59211759Sbrandon.potter@amd.com
593378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
594378SN/A/// only to find out if their stdout is a tty, to determine whether to
5959141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5969141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
597360SN/Atemplate <class OS>
5981450SN/ASyscallReturn
59911856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
600360SN/A{
6016701Sgblack@eecs.umich.edu    int index = 0;
60211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
60311856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
604360SN/A
60510930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
606360SN/A
60711856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
60811856Sbrandon.potter@amd.com        return -ENOTTY;
60910496Ssteve.reinhardt@amd.com
61011856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
61111856Sbrandon.potter@amd.com    if (!dfdp)
6121458SN/A        return -EBADF;
613360SN/A
61411856Sbrandon.potter@amd.com    /**
61511856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
61611856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
61711856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
61811856Sbrandon.potter@amd.com     */
61911856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
62011856Sbrandon.potter@amd.com    if (emul_driver)
62111856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
62210496Ssteve.reinhardt@amd.com
62311856Sbrandon.potter@amd.com    /**
62411856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
62511856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
62611856Sbrandon.potter@amd.com     */
62711856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
62810930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
6299141Smarc.orr@gmail.com    return -ENOTTY;
630360SN/A}
631360SN/A
632360SN/Atemplate <class OS>
63311907SBrandon.Potter@amd.comSyscallReturn
63411907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
63511907SBrandon.Potter@amd.com         bool isopenat)
636360SN/A{
63711907SBrandon.Potter@amd.com    int index = 0;
63811907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
63911907SBrandon.Potter@amd.com
64011907SBrandon.Potter@amd.com    /**
64111907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
64211907SBrandon.Potter@amd.com     * descriptor from the simulated process.
64311907SBrandon.Potter@amd.com     */
64411907SBrandon.Potter@amd.com    if (isopenat)
64511907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
64611907SBrandon.Potter@amd.com
64711907SBrandon.Potter@amd.com    /**
64811907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
64911907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
65011907SBrandon.Potter@amd.com     */
651360SN/A    std::string path;
65211907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6531458SN/A        return -EFAULT;
654360SN/A
65511907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
65611907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
65711907SBrandon.Potter@amd.com#else
65811907SBrandon.Potter@amd.com    int host_flags = 0;
65911907SBrandon.Potter@amd.com#endif
66011907SBrandon.Potter@amd.com    /**
66111907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
66211907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
66311907SBrandon.Potter@amd.com     */
66411907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
665360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
66611907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
66711907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
66811907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
669360SN/A        }
670360SN/A    }
67111907SBrandon.Potter@amd.com    if (tgt_flags) {
67211907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
67311907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
67411907SBrandon.Potter@amd.com    }
675360SN/A#ifdef __CYGWIN32__
67611907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
677360SN/A#endif
678360SN/A
67911907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
6803669Sbinkertn@umich.edu
68111907SBrandon.Potter@amd.com    /**
68211907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
68311907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
68411907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
68511907SBrandon.Potter@amd.com     * create a full path.
68611907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
68711907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
68811907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
68911907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
69011907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
69111907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
69211907SBrandon.Potter@amd.com     */
69311907SBrandon.Potter@amd.com    if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) {
69411907SBrandon.Potter@amd.com        path = p->fullPath(path);
69511907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
69611907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
69711907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
69811907SBrandon.Potter@amd.com        if (!ffdp)
69911907SBrandon.Potter@amd.com            return -EBADF;
70013371Sciro.santilli@arm.com        path.insert(0, ffdp->getFileName() + "/");
70111907SBrandon.Potter@amd.com    }
7021706SN/A
70311907SBrandon.Potter@amd.com    /**
70411907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
70511907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
70611907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
70711907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
70811907SBrandon.Potter@amd.com     */
70910496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
71010496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
71111907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
71211907SBrandon.Potter@amd.com        if (drv) {
71311907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
71411907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
71511907SBrandon.Potter@amd.com                            isopenat ? "at" : "", path.c_str());
71611907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
71710496Ssteve.reinhardt@amd.com        }
71811907SBrandon.Potter@amd.com        /**
71911907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
72011907SBrandon.Potter@amd.com         * as /dev/zero
72111907SBrandon.Potter@amd.com         */
72210496Ssteve.reinhardt@amd.com    }
72310496Ssteve.reinhardt@amd.com
72411907SBrandon.Potter@amd.com    /**
72511907SBrandon.Potter@amd.com     * Some special paths and files cannot be called on the host and need
72611907SBrandon.Potter@amd.com     * to be handled as special cases inside the simulator.
72711907SBrandon.Potter@amd.com     * If the full path that was created above does not match any of the
72811907SBrandon.Potter@amd.com     * special cases, pass it through to the open call on the host to let
72911907SBrandon.Potter@amd.com     * the host open the file on our behalf.
73011907SBrandon.Potter@amd.com     * If the host cannot open the file, return the host's error code back
73111907SBrandon.Potter@amd.com     * through the system call to the simulated process.
73211907SBrandon.Potter@amd.com     */
73311907SBrandon.Potter@amd.com    int sim_fd = -1;
73411907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
73511907SBrandon.Potter@amd.com            { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" };
73611907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
73711907SBrandon.Potter@amd.com        if (startswith(path, entry))
73811907SBrandon.Potter@amd.com            sim_fd = OS::openSpecialFile(path, p, tc);
73911907SBrandon.Potter@amd.com    }
74011907SBrandon.Potter@amd.com    if (sim_fd == -1) {
74111907SBrandon.Potter@amd.com        sim_fd = open(path.c_str(), host_flags, mode);
74211907SBrandon.Potter@amd.com    }
74311907SBrandon.Potter@amd.com    if (sim_fd == -1) {
74411907SBrandon.Potter@amd.com        int local = -errno;
74511907SBrandon.Potter@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n",
74611907SBrandon.Potter@amd.com                        isopenat ? "at" : "", path.c_str());
74711907SBrandon.Potter@amd.com        return local;
74811907SBrandon.Potter@amd.com    }
749360SN/A
75011907SBrandon.Potter@amd.com    /**
75111907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
75211907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
75311907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
75411907SBrandon.Potter@amd.com     * file descriptor.
75511907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
75611907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
75711907SBrandon.Potter@amd.com     */
75811907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
75911907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
76011907SBrandon.Potter@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n",
76111907SBrandon.Potter@amd.com                    isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str());
76211907SBrandon.Potter@amd.com    return tgt_fd;
763360SN/A}
764360SN/A
76510027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
76610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
76710027SChris.Adeniyi-Jones@arm.comSyscallReturn
76811851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
76910027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
77010027SChris.Adeniyi-Jones@arm.com{
77111907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
77210027SChris.Adeniyi-Jones@arm.com}
77310027SChris.Adeniyi-Jones@arm.com
77410027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
77510027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
77610027SChris.Adeniyi-Jones@arm.comSyscallReturn
77711851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
77811851Sbrandon.potter@amd.com           ThreadContext *tc)
77910027SChris.Adeniyi-Jones@arm.com{
78011907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
78110027SChris.Adeniyi-Jones@arm.com}
78210027SChris.Adeniyi-Jones@arm.com
78310633Smichaelupton@gmail.com/// Target unlinkat() handler.
78410633Smichaelupton@gmail.comtemplate <class OS>
78510633Smichaelupton@gmail.comSyscallReturn
78611851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
78710633Smichaelupton@gmail.com             ThreadContext *tc)
78810633Smichaelupton@gmail.com{
78910633Smichaelupton@gmail.com    int index = 0;
79010633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
79110633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
79210633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
79310633Smichaelupton@gmail.com
79410633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
79510633Smichaelupton@gmail.com}
79610633Smichaelupton@gmail.com
79710203SAli.Saidi@ARM.com/// Target facessat() handler
79810203SAli.Saidi@ARM.comtemplate <class OS>
79910203SAli.Saidi@ARM.comSyscallReturn
80011851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
80111851Sbrandon.potter@amd.com              ThreadContext *tc)
80210203SAli.Saidi@ARM.com{
80310203SAli.Saidi@ARM.com    int index = 0;
80410203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
80510203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
80610203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
80710203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
80810203SAli.Saidi@ARM.com}
80910203SAli.Saidi@ARM.com
81010203SAli.Saidi@ARM.com/// Target readlinkat() handler
81110203SAli.Saidi@ARM.comtemplate <class OS>
81210203SAli.Saidi@ARM.comSyscallReturn
81311851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
81411851Sbrandon.potter@amd.com               ThreadContext *tc)
81510203SAli.Saidi@ARM.com{
81610203SAli.Saidi@ARM.com    int index = 0;
81710203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
81810203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
81910203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
82010203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
82110203SAli.Saidi@ARM.com}
82210203SAli.Saidi@ARM.com
82310850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
82410850SGiacomo.Gabrielli@arm.comtemplate <class OS>
82510850SGiacomo.Gabrielli@arm.comSyscallReturn
82611851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
82710850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
82810850SGiacomo.Gabrielli@arm.com{
82910850SGiacomo.Gabrielli@arm.com    int index = 0;
83010850SGiacomo.Gabrielli@arm.com
83110850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
83210850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
83310850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
83410850SGiacomo.Gabrielli@arm.com
83510850SGiacomo.Gabrielli@arm.com    std::string old_name;
83610850SGiacomo.Gabrielli@arm.com
83710850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
83810850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
83910850SGiacomo.Gabrielli@arm.com        return -EFAULT;
84010850SGiacomo.Gabrielli@arm.com
84110850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
84210850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
84310850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
84410850SGiacomo.Gabrielli@arm.com
84510850SGiacomo.Gabrielli@arm.com    std::string new_name;
84610850SGiacomo.Gabrielli@arm.com
84710850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
84810850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
84910850SGiacomo.Gabrielli@arm.com        return -EFAULT;
85010850SGiacomo.Gabrielli@arm.com
85110850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
85210850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
85310850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
85410850SGiacomo.Gabrielli@arm.com
85510850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
85610850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
85710850SGiacomo.Gabrielli@arm.com}
85810850SGiacomo.Gabrielli@arm.com
8596640Svince@csl.cornell.edu/// Target sysinfo() handler.
8606640Svince@csl.cornell.edutemplate <class OS>
8616640Svince@csl.cornell.eduSyscallReturn
86211851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
86311851Sbrandon.potter@amd.com            ThreadContext *tc)
8646640Svince@csl.cornell.edu{
8656640Svince@csl.cornell.edu
8666701Sgblack@eecs.umich.edu    int index = 0;
8676701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
86810793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
8696640Svince@csl.cornell.edu
87011758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
87111758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
87211758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
8736640Svince@csl.cornell.edu
8748706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
8756640Svince@csl.cornell.edu
8766701Sgblack@eecs.umich.edu    return 0;
8776640Svince@csl.cornell.edu}
878360SN/A
8791999SN/A/// Target chmod() handler.
8801999SN/Atemplate <class OS>
8811999SN/ASyscallReturn
88211851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
8832680Sktlim@umich.edu          ThreadContext *tc)
8841999SN/A{
8851999SN/A    std::string path;
8861999SN/A
8876701Sgblack@eecs.umich.edu    int index = 0;
8888852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8896701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8901999SN/A        return -EFAULT;
8916701Sgblack@eecs.umich.edu    }
8921999SN/A
8936701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8941999SN/A    mode_t hostMode = 0;
8951999SN/A
8961999SN/A    // XXX translate mode flags via OS::something???
8971999SN/A    hostMode = mode;
8981999SN/A
8993669Sbinkertn@umich.edu    // Adjust path for current working directory
9003669Sbinkertn@umich.edu    path = process->fullPath(path);
9013669Sbinkertn@umich.edu
9021999SN/A    // do the chmod
9031999SN/A    int result = chmod(path.c_str(), hostMode);
9041999SN/A    if (result < 0)
9052218SN/A        return -errno;
9061999SN/A
9071999SN/A    return 0;
9081999SN/A}
9091999SN/A
9101999SN/A
9111999SN/A/// Target fchmod() handler.
9121999SN/Atemplate <class OS>
9131999SN/ASyscallReturn
91411856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
9151999SN/A{
9166701Sgblack@eecs.umich.edu    int index = 0;
91711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
91811856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
91910931Sbrandon.potter@amd.com
92011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
92111856Sbrandon.potter@amd.com    if (!ffdp)
9221999SN/A        return -EBADF;
92311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
9241999SN/A
92511856Sbrandon.potter@amd.com    mode_t hostMode = mode;
9261999SN/A
92711856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
9281999SN/A
92911856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
9301999SN/A}
9311999SN/A
9325877Shsul@eecs.umich.edu/// Target mremap() handler.
9335877Shsul@eecs.umich.edutemplate <class OS>
9345877Shsul@eecs.umich.eduSyscallReturn
93511851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
9365877Shsul@eecs.umich.edu{
9376701Sgblack@eecs.umich.edu    int index = 0;
9386701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
9396701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
9406701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
9416701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
94210027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
94310027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
94410027SChris.Adeniyi-Jones@arm.com
94510027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
94610027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
9475877Shsul@eecs.umich.edu
94810318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
94910318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
9505877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
9515877Shsul@eecs.umich.edu        return -EINVAL;
9525877Shsul@eecs.umich.edu    }
9535877Shsul@eecs.umich.edu
95410486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
95510486Stjablin@gmail.com
9565877Shsul@eecs.umich.edu    if (new_length > old_length) {
95711905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
95811905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
95911905SBrandon.Potter@amd.com
96011905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
96110027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
96212206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
96312206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
9645877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
96511905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
96611905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
9675877Shsul@eecs.umich.edu            return start;
9685877Shsul@eecs.umich.edu        } else {
96910027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
9705877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
9715877Shsul@eecs.umich.edu                return -ENOMEM;
9725877Shsul@eecs.umich.edu            } else {
97312206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
97412206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
97512206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
97612206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
97712206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
97812206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
97912206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
98012206Srico.amslinger@informatik.uni-augsburg.de                }
98112206Srico.amslinger@informatik.uni-augsburg.de
98210027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
98310027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
98410027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
98510027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
9865877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
98710027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
98810027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
98910027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
99010027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
99112206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
99212206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
99312206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
99412206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
99510027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
99610027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
99710027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
99810027SChris.Adeniyi-Jones@arm.com                }
99910027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
100010027SChris.Adeniyi-Jones@arm.com                return new_start;
10015877Shsul@eecs.umich.edu            }
10025877Shsul@eecs.umich.edu        }
10035877Shsul@eecs.umich.edu    } else {
100410027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
100510027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
10068601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
100710027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
10085877Shsul@eecs.umich.edu    }
10095877Shsul@eecs.umich.edu}
10101999SN/A
1011378SN/A/// Target stat() handler.
1012360SN/Atemplate <class OS>
10131450SN/ASyscallReturn
101411851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
10152680Sktlim@umich.edu         ThreadContext *tc)
1016360SN/A{
1017360SN/A    std::string path;
1018360SN/A
10196701Sgblack@eecs.umich.edu    int index = 0;
10208852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10216701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10226701Sgblack@eecs.umich.edu        return -EFAULT;
10236701Sgblack@eecs.umich.edu    }
10246701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1025360SN/A
10263669Sbinkertn@umich.edu    // Adjust path for current working directory
10273669Sbinkertn@umich.edu    path = process->fullPath(path);
10283669Sbinkertn@umich.edu
1029360SN/A    struct stat hostBuf;
1030360SN/A    int result = stat(path.c_str(), &hostBuf);
1031360SN/A
1032360SN/A    if (result < 0)
10332218SN/A        return -errno;
1034360SN/A
10358706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1036360SN/A
10371458SN/A    return 0;
1038360SN/A}
1039360SN/A
1040360SN/A
10415074Ssaidi@eecs.umich.edu/// Target stat64() handler.
10425074Ssaidi@eecs.umich.edutemplate <class OS>
10435074Ssaidi@eecs.umich.eduSyscallReturn
104411851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
10455074Ssaidi@eecs.umich.edu           ThreadContext *tc)
10465074Ssaidi@eecs.umich.edu{
10475074Ssaidi@eecs.umich.edu    std::string path;
10485074Ssaidi@eecs.umich.edu
10496701Sgblack@eecs.umich.edu    int index = 0;
10508852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10516701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
10525074Ssaidi@eecs.umich.edu        return -EFAULT;
10536701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10545074Ssaidi@eecs.umich.edu
10555074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
10565074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
10575074Ssaidi@eecs.umich.edu
10585208Ssaidi@eecs.umich.edu#if NO_STAT64
10595208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
10605208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
10615208Ssaidi@eecs.umich.edu#else
10625074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
10635074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
10645208Ssaidi@eecs.umich.edu#endif
10655074Ssaidi@eecs.umich.edu
10665074Ssaidi@eecs.umich.edu    if (result < 0)
10675074Ssaidi@eecs.umich.edu        return -errno;
10685074Ssaidi@eecs.umich.edu
10698706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10705074Ssaidi@eecs.umich.edu
10715074Ssaidi@eecs.umich.edu    return 0;
10725074Ssaidi@eecs.umich.edu}
10735074Ssaidi@eecs.umich.edu
10745074Ssaidi@eecs.umich.edu
107510027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
107610027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
107710027SChris.Adeniyi-Jones@arm.comSyscallReturn
107811851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
107910027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
108010027SChris.Adeniyi-Jones@arm.com{
108110027SChris.Adeniyi-Jones@arm.com    int index = 0;
108210027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
108310027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
108410793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
108510027SChris.Adeniyi-Jones@arm.com
108610027SChris.Adeniyi-Jones@arm.com    std::string path;
108710027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
108810027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
108910027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
109010027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
109110027SChris.Adeniyi-Jones@arm.com
109210027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
109310027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
109410027SChris.Adeniyi-Jones@arm.com
109510027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
109610027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
109710027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
109810027SChris.Adeniyi-Jones@arm.com#else
109910027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
110010027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
110110027SChris.Adeniyi-Jones@arm.com#endif
110210027SChris.Adeniyi-Jones@arm.com
110310027SChris.Adeniyi-Jones@arm.com    if (result < 0)
110410027SChris.Adeniyi-Jones@arm.com        return -errno;
110510027SChris.Adeniyi-Jones@arm.com
110610027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
110710027SChris.Adeniyi-Jones@arm.com
110810027SChris.Adeniyi-Jones@arm.com    return 0;
110910027SChris.Adeniyi-Jones@arm.com}
111010027SChris.Adeniyi-Jones@arm.com
111110027SChris.Adeniyi-Jones@arm.com
11121999SN/A/// Target fstat64() handler.
11131999SN/Atemplate <class OS>
11141999SN/ASyscallReturn
111511856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
11161999SN/A{
11176701Sgblack@eecs.umich.edu    int index = 0;
111811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
111911856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
112010931Sbrandon.potter@amd.com
112111856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
112211856Sbrandon.potter@amd.com    if (!ffdp)
11231999SN/A        return -EBADF;
112411856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
11251999SN/A
11262764Sstever@eecs.umich.edu#if NO_STAT64
11272064SN/A    struct stat  hostBuf;
112810931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
11292064SN/A#else
11302064SN/A    struct stat64  hostBuf;
113110931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
11322064SN/A#endif
11331999SN/A
11341999SN/A    if (result < 0)
11352218SN/A        return -errno;
11361999SN/A
113710931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11381999SN/A
11391999SN/A    return 0;
11401999SN/A}
11411999SN/A
11421999SN/A
1143378SN/A/// Target lstat() handler.
1144360SN/Atemplate <class OS>
11451450SN/ASyscallReturn
114611851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
11472680Sktlim@umich.edu          ThreadContext *tc)
1148360SN/A{
1149360SN/A    std::string path;
1150360SN/A
11516701Sgblack@eecs.umich.edu    int index = 0;
11528852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11536701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11546701Sgblack@eecs.umich.edu        return -EFAULT;
11556701Sgblack@eecs.umich.edu    }
11566701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1157360SN/A
11583669Sbinkertn@umich.edu    // Adjust path for current working directory
11593669Sbinkertn@umich.edu    path = process->fullPath(path);
11603669Sbinkertn@umich.edu
1161360SN/A    struct stat hostBuf;
1162360SN/A    int result = lstat(path.c_str(), &hostBuf);
1163360SN/A
1164360SN/A    if (result < 0)
11651458SN/A        return -errno;
1166360SN/A
11678706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1168360SN/A
11691458SN/A    return 0;
1170360SN/A}
1171360SN/A
11721999SN/A/// Target lstat64() handler.
11731999SN/Atemplate <class OS>
11741999SN/ASyscallReturn
117511851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
11762680Sktlim@umich.edu            ThreadContext *tc)
11771999SN/A{
11781999SN/A    std::string path;
11791999SN/A
11806701Sgblack@eecs.umich.edu    int index = 0;
11818852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11826701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11836701Sgblack@eecs.umich.edu        return -EFAULT;
11846701Sgblack@eecs.umich.edu    }
11856701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11861999SN/A
11873669Sbinkertn@umich.edu    // Adjust path for current working directory
11883669Sbinkertn@umich.edu    path = process->fullPath(path);
11893669Sbinkertn@umich.edu
11902764Sstever@eecs.umich.edu#if NO_STAT64
11912064SN/A    struct stat hostBuf;
11922064SN/A    int result = lstat(path.c_str(), &hostBuf);
11932064SN/A#else
11941999SN/A    struct stat64 hostBuf;
11951999SN/A    int result = lstat64(path.c_str(), &hostBuf);
11962064SN/A#endif
11971999SN/A
11981999SN/A    if (result < 0)
11991999SN/A        return -errno;
12001999SN/A
12018706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
12021999SN/A
12031999SN/A    return 0;
12041999SN/A}
12051999SN/A
1206378SN/A/// Target fstat() handler.
1207360SN/Atemplate <class OS>
12081450SN/ASyscallReturn
120911856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1210360SN/A{
12116701Sgblack@eecs.umich.edu    int index = 0;
121211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
121311856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1214360SN/A
121511380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1216360SN/A
121711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
121811856Sbrandon.potter@amd.com    if (!ffdp)
12191458SN/A        return -EBADF;
122011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1221360SN/A
1222360SN/A    struct stat hostBuf;
122310931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1224360SN/A
1225360SN/A    if (result < 0)
12261458SN/A        return -errno;
1227360SN/A
122810931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
12292021SN/A
12301458SN/A    return 0;
1231360SN/A}
1232360SN/A
1233360SN/A
12341706SN/A/// Target statfs() handler.
12351706SN/Atemplate <class OS>
12361706SN/ASyscallReturn
123711851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
12382680Sktlim@umich.edu           ThreadContext *tc)
12391706SN/A{
124011799Sbrandon.potter@amd.com#if NO_STATFS
124111799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
124211799Sbrandon.potter@amd.com#else
12431706SN/A    std::string path;
12441706SN/A
12456701Sgblack@eecs.umich.edu    int index = 0;
12468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12476701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12486701Sgblack@eecs.umich.edu        return -EFAULT;
12496701Sgblack@eecs.umich.edu    }
12506701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12511706SN/A
12523669Sbinkertn@umich.edu    // Adjust path for current working directory
12533669Sbinkertn@umich.edu    path = process->fullPath(path);
12543669Sbinkertn@umich.edu
12551706SN/A    struct statfs hostBuf;
12561706SN/A    int result = statfs(path.c_str(), &hostBuf);
12571706SN/A
12581706SN/A    if (result < 0)
12592218SN/A        return -errno;
12601706SN/A
126111759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
126211799Sbrandon.potter@amd.com#endif
12631706SN/A    return 0;
12641706SN/A}
12651706SN/A
126611886Sbrandon.potter@amd.comtemplate <class OS>
126711886Sbrandon.potter@amd.comSyscallReturn
126811886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
126911886Sbrandon.potter@amd.com{
127011886Sbrandon.potter@amd.com    int index = 0;
127112426Sqtt2@cornell.edu
127211886Sbrandon.potter@amd.com    TheISA::IntReg flags = p->getSyscallArg(tc, index);
127311886Sbrandon.potter@amd.com    TheISA::IntReg newStack = p->getSyscallArg(tc, index);
127411886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
127512426Sqtt2@cornell.edu
127613534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
127712426Sqtt2@cornell.edu    /**
127813534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
127912426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
128012426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
128112426Sqtt2@cornell.edu     */
128212426Sqtt2@cornell.edu    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
128312426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
128412426Sqtt2@cornell.edu#else
128511886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
128611886Sbrandon.potter@amd.com    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
128712426Sqtt2@cornell.edu#endif
128811886Sbrandon.potter@amd.com
128911886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
129011886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
129111886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
129211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
129311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
129411886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
129511886Sbrandon.potter@amd.com        return -EINVAL;
129611886Sbrandon.potter@amd.com
129711886Sbrandon.potter@amd.com    ThreadContext *ctc;
129811886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
129911886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
130011886Sbrandon.potter@amd.com
130111886Sbrandon.potter@amd.com    /**
130211886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
130311886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
130411886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
130511886Sbrandon.potter@amd.com     * constructor.
130611886Sbrandon.potter@amd.com     */
130711886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
130811886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
130911886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
131011886Sbrandon.potter@amd.com    pp->system = p->system;
131111886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
131211886Sbrandon.potter@amd.com    pp->input.assign("stdin");
131311886Sbrandon.potter@amd.com    pp->output.assign("stdout");
131411886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
131511886Sbrandon.potter@amd.com    pp->uid = p->uid();
131611886Sbrandon.potter@amd.com    pp->euid = p->euid();
131711886Sbrandon.potter@amd.com    pp->gid = p->gid();
131811886Sbrandon.potter@amd.com    pp->egid = p->egid();
131911886Sbrandon.potter@amd.com
132011886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
132111886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
132211886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
132311886Sbrandon.potter@amd.com    do {
132411886Sbrandon.potter@amd.com        temp_pid++;
132511886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
132611886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
132711886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
132811886Sbrandon.potter@amd.com
132911886Sbrandon.potter@amd.com    pp->pid = temp_pid;
133011886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
133111886Sbrandon.potter@amd.com    Process *cp = pp->create();
133211886Sbrandon.potter@amd.com    delete pp;
133311886Sbrandon.potter@amd.com
133411886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
133511886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
133611886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
133711886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
133811886Sbrandon.potter@amd.com
133911886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
134011886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
134111886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
134211886Sbrandon.potter@amd.com        *ptid = cp->pid();
134311886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
134411886Sbrandon.potter@amd.com    }
134511886Sbrandon.potter@amd.com
134611886Sbrandon.potter@amd.com    cp->initState();
134711886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
134811886Sbrandon.potter@amd.com
134911911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
135011911SBrandon.Potter@amd.com        delete cp->sigchld;
135111911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
135211911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
135311911SBrandon.Potter@amd.com        *cp->sigchld = true;
135411911SBrandon.Potter@amd.com    }
135511911SBrandon.Potter@amd.com
135611886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
135711886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
135811886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
135911886Sbrandon.potter@amd.com        *ctid = cp->pid();
136011886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
136111886Sbrandon.potter@amd.com    }
136211886Sbrandon.potter@amd.com
136311886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
136411886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
136511886Sbrandon.potter@amd.com
136611886Sbrandon.potter@amd.com    ctc->clearArchRegs();
136711886Sbrandon.potter@amd.com
136811886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
136911886Sbrandon.potter@amd.com    TheISA::copyMiscRegs(tc, ctc);
137011886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
137111886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
137211886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0);
137311886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0);
137411886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2);
137511886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows);
137611886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_CWP, 0);
137711886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0);
137811886Sbrandon.potter@amd.com    ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0);
137911886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY);
138011886Sbrandon.potter@amd.com    for (int y = 8; y < 32; y++)
138111886Sbrandon.potter@amd.com        ctc->setIntReg(y, tc->readIntReg(y));
138212426Sqtt2@cornell.edu#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA or THE_ISA == RISCV_ISA
138311886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
138411886Sbrandon.potter@amd.com#endif
138511886Sbrandon.potter@amd.com
138611886Sbrandon.potter@amd.com#if THE_ISA == X86_ISA
138711886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_SETTLS) {
138811886Sbrandon.potter@amd.com        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr);
138911886Sbrandon.potter@amd.com        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr);
139011886Sbrandon.potter@amd.com    }
139111886Sbrandon.potter@amd.com#endif
139211886Sbrandon.potter@amd.com
139311886Sbrandon.potter@amd.com    if (newStack)
139411886Sbrandon.potter@amd.com        ctc->setIntReg(TheISA::StackPointerReg, newStack);
139511886Sbrandon.potter@amd.com
139611886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
139711886Sbrandon.potter@amd.com
139811886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
139911886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
140011886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
140111886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
140211886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
140311886Sbrandon.potter@amd.com#endif
140411886Sbrandon.potter@amd.com
140511886Sbrandon.potter@amd.com    ctc->pcState(tc->nextInstAddr());
140611886Sbrandon.potter@amd.com    ctc->activate();
140711886Sbrandon.potter@amd.com
140811886Sbrandon.potter@amd.com    return cp->pid();
140911886Sbrandon.potter@amd.com}
14101706SN/A
14111706SN/A/// Target fstatfs() handler.
14121706SN/Atemplate <class OS>
14131706SN/ASyscallReturn
141411856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14151706SN/A{
14166701Sgblack@eecs.umich.edu    int index = 0;
141711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
141811856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
14191706SN/A
142011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
142111856Sbrandon.potter@amd.com    if (!ffdp)
14221706SN/A        return -EBADF;
142311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
14241706SN/A
14251706SN/A    struct statfs hostBuf;
142610931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
14271706SN/A
14281706SN/A    if (result < 0)
14292218SN/A        return -errno;
14301706SN/A
143111759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14321706SN/A
14331706SN/A    return 0;
14341706SN/A}
14351706SN/A
14361706SN/A
14371999SN/A/// Target writev() handler.
14381999SN/Atemplate <class OS>
14391999SN/ASyscallReturn
144011856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14411999SN/A{
14426701Sgblack@eecs.umich.edu    int index = 0;
144311856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
144410931Sbrandon.potter@amd.com
144511856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
144611856Sbrandon.potter@amd.com    if (!hbfdp)
14471999SN/A        return -EBADF;
144811856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
14491999SN/A
145011856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
145111856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
145211856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
14531999SN/A    struct iovec hiov[count];
14546227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
14551999SN/A        typename OS::tgt_iovec tiov;
14562461SN/A
145711856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
145811856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
14598737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
14601999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
146111856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
146211856Sbrandon.potter@amd.com                      hiov[i].iov_len);
14631999SN/A    }
14641999SN/A
146510931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
14661999SN/A
14676227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
14681999SN/A        delete [] (char *)hiov[i].iov_base;
14691999SN/A
14701999SN/A    if (result < 0)
14712218SN/A        return -errno;
14721999SN/A
147310629Sjthestness@gmail.com    return result;
14741999SN/A}
14751999SN/A
147611385Sbrandon.potter@amd.com/// Real mmap handler.
1477360SN/Atemplate <class OS>
14781450SN/ASyscallReturn
147911851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
148011385Sbrandon.potter@amd.com         bool is_mmap2)
1481360SN/A{
14826701Sgblack@eecs.umich.edu    int index = 0;
14836701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
14846701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
148511383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
148611383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
14878324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
148810486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1489360SN/A
149011385Sbrandon.potter@amd.com    if (is_mmap2)
149111385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
14929008Sgblack@eecs.umich.edu
149311383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
149411383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
149511383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
149611383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
149711383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
149811383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
149911383Sbrandon.potter@amd.com        !length) {
150011383Sbrandon.potter@amd.com        return -EINVAL;
150111383Sbrandon.potter@amd.com    }
15028324Ssteve.reinhardt@amd.com
150311383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
150411383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
150511383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
150611383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
150711383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
150811383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
150911383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
151011383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
151111383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
151211383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
151311383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
151411383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
151511383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
151611383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
151711383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
151811383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
151911383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
152011383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
152111383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
152211383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
152311383Sbrandon.potter@amd.com        // than we currently maintain.
152411383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
152511383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
152611383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
15278324Ssteve.reinhardt@amd.com    }
15285877Shsul@eecs.umich.edu
152910486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
153010486Stjablin@gmail.com
153111383Sbrandon.potter@amd.com    int sim_fd = -1;
153211383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
153311383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
153411856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
153511624Smichael.lebeane@amd.com
153611856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
153711856Sbrandon.potter@amd.com        if (dfdp) {
153811856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
153911856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
154011624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
154111624Smichael.lebeane@amd.com        }
154211624Smichael.lebeane@amd.com
154311856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
154411856Sbrandon.potter@amd.com        if (!ffdp)
154511383Sbrandon.potter@amd.com            return -EBADF;
154611856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1547360SN/A
154811913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
154911383Sbrandon.potter@amd.com                                    sim_fd, offset);
15508600Ssteve.reinhardt@amd.com
155111383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
155211383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
155311383Sbrandon.potter@amd.com            return -errno;
15548600Ssteve.reinhardt@amd.com        }
15552544SN/A    }
15562544SN/A
155711383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
155811383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
155911383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
156011905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
156111905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
156211905SBrandon.Potter@amd.com
156311905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
156411905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
156511905SBrandon.Potter@amd.com
156611905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
156711383Sbrandon.potter@amd.com    }
156811383Sbrandon.potter@amd.com
156911383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
157011383Sbrandon.potter@amd.com                    start, start + length - 1);
157111383Sbrandon.potter@amd.com
157211383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
157311383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
157411383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
157511383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
157611383Sbrandon.potter@amd.com    if (clobber) {
157711383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
157811383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
157911383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
158011383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
158111383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
15828600Ssteve.reinhardt@amd.com        }
15836672Sgblack@eecs.umich.edu    }
15848600Ssteve.reinhardt@amd.com
158511383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
158611383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
158711383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
15888601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
15892544SN/A
159011383Sbrandon.potter@amd.com    // Transfer content into target address space.
159111383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
159211383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
159311383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
159411383Sbrandon.potter@amd.com        // with something like:
159511383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
159611383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
159711383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
159811383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
159911383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
160011383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
160111383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
160211383Sbrandon.potter@amd.com    } else {
160311383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
160411383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
160511383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
160611383Sbrandon.potter@amd.com        // the host into the target address space.
160711383Sbrandon.potter@amd.com        struct stat file_stat;
160811383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
160911383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
161011383Sbrandon.potter@amd.com
161111383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
161211383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
161311383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
161411383Sbrandon.potter@amd.com        // on the specified offset into the file.
161511383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
161611383Sbrandon.potter@amd.com                                 length);
161711383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
161811383Sbrandon.potter@amd.com
161911383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
162011383Sbrandon.potter@amd.com        munmap(pmap, length);
162111383Sbrandon.potter@amd.com
162211392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
162311392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
162411392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
162511392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
162611392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
162711392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
162811392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
162911392Sbrandon.potter@amd.com        if (interpreter) {
163011392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
163111392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
163211392Sbrandon.potter@amd.com
163311392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
163411392Sbrandon.potter@amd.com
163511392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
163611856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
163711856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
163811856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
163911392Sbrandon.potter@amd.com
164011392Sbrandon.potter@amd.com                if (lib) {
164111392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
164211392Sbrandon.potter@amd.com                                        lib->textBase(), start);
164311392Sbrandon.potter@amd.com                }
164411392Sbrandon.potter@amd.com            }
164511392Sbrandon.potter@amd.com        }
164611392Sbrandon.potter@amd.com
164711383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
164811383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
164911383Sbrandon.potter@amd.com        // execution (hopefully).
165011383Sbrandon.potter@amd.com    }
165111383Sbrandon.potter@amd.com
16521458SN/A    return start;
1653360SN/A}
1654360SN/A
165511593Santhony.gutierrez@amd.comtemplate <class OS>
165611593Santhony.gutierrez@amd.comSyscallReturn
165711851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
165811593Santhony.gutierrez@amd.com{
165911593Santhony.gutierrez@amd.com    int index = 0;
166011593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
166111593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
166211593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
166311593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
166411593Santhony.gutierrez@amd.com
166511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
166611856Sbrandon.potter@amd.com    if (!ffdp)
166711593Santhony.gutierrez@amd.com        return -EBADF;
166811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
166911593Santhony.gutierrez@amd.com
167011593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
167111593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
167211593Santhony.gutierrez@amd.com
167311594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
167411593Santhony.gutierrez@amd.com
167511593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
167611593Santhony.gutierrez@amd.com}
167711593Santhony.gutierrez@amd.com
167811385Sbrandon.potter@amd.com/// Target mmap() handler.
167911385Sbrandon.potter@amd.comtemplate <class OS>
168011385Sbrandon.potter@amd.comSyscallReturn
168111851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
168211385Sbrandon.potter@amd.com{
168311385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
168411385Sbrandon.potter@amd.com}
168511385Sbrandon.potter@amd.com
168611385Sbrandon.potter@amd.com/// Target mmap2() handler.
168711385Sbrandon.potter@amd.comtemplate <class OS>
168811385Sbrandon.potter@amd.comSyscallReturn
168911851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
169011385Sbrandon.potter@amd.com{
169111385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
169211385Sbrandon.potter@amd.com}
169311385Sbrandon.potter@amd.com
1694378SN/A/// Target getrlimit() handler.
1695360SN/Atemplate <class OS>
16961450SN/ASyscallReturn
169711851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
169811851Sbrandon.potter@amd.com              ThreadContext *tc)
1699360SN/A{
17006701Sgblack@eecs.umich.edu    int index = 0;
17016701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
17026701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1703360SN/A
1704360SN/A    switch (resource) {
170511906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
170611906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
170711906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
170811906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
170911906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
171011906SBrandon.Potter@amd.com        break;
1711360SN/A
171211906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
171311906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
171411906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
171511906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
171611906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
171711906SBrandon.Potter@amd.com        break;
17185877Shsul@eecs.umich.edu
171911906SBrandon.Potter@amd.com      default:
172011906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
172111906SBrandon.Potter@amd.com        return -EINVAL;
172211906SBrandon.Potter@amd.com        break;
1723360SN/A    }
1724360SN/A
17258706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
17261458SN/A    return 0;
1727360SN/A}
1728360SN/A
172912235Sar4jc@virginia.edutemplate <class OS>
173012235Sar4jc@virginia.eduSyscallReturn
173112235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
173212235Sar4jc@virginia.edu            ThreadContext *tc)
173312235Sar4jc@virginia.edu{
173412235Sar4jc@virginia.edu    int index = 0;
173512235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
173612235Sar4jc@virginia.edu    {
173712235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
173812235Sar4jc@virginia.edu        return -EPERM;
173912235Sar4jc@virginia.edu    }
174012235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
174112235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
174212235Sar4jc@virginia.edu    if (n != 0)
174312235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
174412235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
174512235Sar4jc@virginia.edu    if (o != 0)
174612235Sar4jc@virginia.edu    {
174712416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
174812235Sar4jc@virginia.edu        switch (resource) {
174912235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
175012235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
175112235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
175212235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
175312235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
175412235Sar4jc@virginia.edu            break;
175512235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
175612235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
175712235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
175812235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
175912235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
176012593Sjason@lowepower.com            break;
176112235Sar4jc@virginia.edu          default:
176212235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
176312235Sar4jc@virginia.edu            return -EINVAL;
176412235Sar4jc@virginia.edu            break;
176512235Sar4jc@virginia.edu        }
176612235Sar4jc@virginia.edu        rlp.copyOut(tc->getMemProxy());
176712235Sar4jc@virginia.edu    }
176812235Sar4jc@virginia.edu    return 0;
176912235Sar4jc@virginia.edu}
177012235Sar4jc@virginia.edu
177110796Sbrandon.potter@amd.com/// Target clock_gettime() function.
177210796Sbrandon.potter@amd.comtemplate <class OS>
177310796Sbrandon.potter@amd.comSyscallReturn
177411851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
177510796Sbrandon.potter@amd.com{
177610796Sbrandon.potter@amd.com    int index = 1;
177710796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
177810796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
177910796Sbrandon.potter@amd.com
178010796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
178110796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
178210796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
178310796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
178410796Sbrandon.potter@amd.com
178510796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
178610796Sbrandon.potter@amd.com
178710796Sbrandon.potter@amd.com    return 0;
178810796Sbrandon.potter@amd.com}
178910796Sbrandon.potter@amd.com
179011337SMichael.Lebeane@amd.com/// Target clock_getres() function.
179111337SMichael.Lebeane@amd.comtemplate <class OS>
179211337SMichael.Lebeane@amd.comSyscallReturn
179311851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
179411337SMichael.Lebeane@amd.com{
179511337SMichael.Lebeane@amd.com    int index = 1;
179611337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
179711337SMichael.Lebeane@amd.com
179811337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
179911337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
180011337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
180111337SMichael.Lebeane@amd.com
180211337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
180311337SMichael.Lebeane@amd.com
180411337SMichael.Lebeane@amd.com    return 0;
180511337SMichael.Lebeane@amd.com}
180611337SMichael.Lebeane@amd.com
1807378SN/A/// Target gettimeofday() handler.
1808360SN/Atemplate <class OS>
18091450SN/ASyscallReturn
181011851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
181111851Sbrandon.potter@amd.com                 ThreadContext *tc)
1812360SN/A{
18136701Sgblack@eecs.umich.edu    int index = 0;
18146701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1815360SN/A
181610796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1817360SN/A    tp->tv_sec += seconds_since_epoch;
18186109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
18196109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1820360SN/A
18218706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1822360SN/A
18231458SN/A    return 0;
1824360SN/A}
1825360SN/A
1826360SN/A
18271999SN/A/// Target utimes() handler.
18281999SN/Atemplate <class OS>
18291999SN/ASyscallReturn
183011851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
18312680Sktlim@umich.edu           ThreadContext *tc)
18321999SN/A{
18331999SN/A    std::string path;
18341999SN/A
18356701Sgblack@eecs.umich.edu    int index = 0;
18368852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
18376701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
18386701Sgblack@eecs.umich.edu        return -EFAULT;
18396701Sgblack@eecs.umich.edu    }
18401999SN/A
18416701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
18426701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
18438706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
18441999SN/A
18451999SN/A    struct timeval hostTimeval[2];
184611906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
18478737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
18488737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
18491999SN/A    }
18503669Sbinkertn@umich.edu
18513669Sbinkertn@umich.edu    // Adjust path for current working directory
18523669Sbinkertn@umich.edu    path = process->fullPath(path);
18533669Sbinkertn@umich.edu
18541999SN/A    int result = utimes(path.c_str(), hostTimeval);
18551999SN/A
18561999SN/A    if (result < 0)
18571999SN/A        return -errno;
18581999SN/A
18591999SN/A    return 0;
18601999SN/A}
186111886Sbrandon.potter@amd.com
186211886Sbrandon.potter@amd.comtemplate <class OS>
186311886Sbrandon.potter@amd.comSyscallReturn
186411886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
186511886Sbrandon.potter@amd.com{
186611886Sbrandon.potter@amd.com    desc->setFlags(0);
186711886Sbrandon.potter@amd.com
186811886Sbrandon.potter@amd.com    int index = 0;
186911886Sbrandon.potter@amd.com    std::string path;
187011886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
187111886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
187211886Sbrandon.potter@amd.com        return -EFAULT;
187311886Sbrandon.potter@amd.com
187411886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
187511886Sbrandon.potter@amd.com        return -EACCES;
187611886Sbrandon.potter@amd.com
187711886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
187811886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
187911886Sbrandon.potter@amd.com                      Addr mem_loc)
188011886Sbrandon.potter@amd.com    {
188111886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
188211886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
188311886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
188411886Sbrandon.potter@amd.com
188511886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
188611886Sbrandon.potter@amd.com                break;
188711886Sbrandon.potter@amd.com
188811886Sbrandon.potter@amd.com            vect.push_back(std::string());
188911886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
189011886Sbrandon.potter@amd.com        }
189111886Sbrandon.potter@amd.com    };
189211886Sbrandon.potter@amd.com
189311886Sbrandon.potter@amd.com    /**
189411886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
189511886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
189611886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
189711886Sbrandon.potter@amd.com     * constructor.
189811886Sbrandon.potter@amd.com     */
189911886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
190011886Sbrandon.potter@amd.com    pp->executable = path;
190111886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
190211886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
190311886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
190411886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
190511886Sbrandon.potter@amd.com    pp->uid = p->uid();
190611886Sbrandon.potter@amd.com    pp->egid = p->egid();
190711886Sbrandon.potter@amd.com    pp->euid = p->euid();
190811886Sbrandon.potter@amd.com    pp->gid = p->gid();
190911886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
191011886Sbrandon.potter@amd.com    pp->pid = p->pid();
191111886Sbrandon.potter@amd.com    pp->input.assign("cin");
191211886Sbrandon.potter@amd.com    pp->output.assign("cout");
191311886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
191411886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
191511886Sbrandon.potter@amd.com    pp->system = p->system;
191611886Sbrandon.potter@amd.com    /**
191711886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
191811886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
191911886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
192011886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
192111886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
192211886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
192311886Sbrandon.potter@amd.com     */
192411886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
192511886Sbrandon.potter@amd.com    Process *new_p = pp->create();
192611886Sbrandon.potter@amd.com    delete pp;
192711886Sbrandon.potter@amd.com
192811886Sbrandon.potter@amd.com    /**
192911886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
193011886Sbrandon.potter@amd.com     * close-on-exec.
193111886Sbrandon.potter@amd.com     */
193211886Sbrandon.potter@amd.com    new_p->fds = p->fds;
193311886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
193411886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
193511886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
193611886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
193711886Sbrandon.potter@amd.com    }
193811886Sbrandon.potter@amd.com
193911886Sbrandon.potter@amd.com    *new_p->sigchld = true;
194011886Sbrandon.potter@amd.com
194111886Sbrandon.potter@amd.com    delete p;
194211886Sbrandon.potter@amd.com    tc->clearArchRegs();
194311886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
194411886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
194511886Sbrandon.potter@amd.com    new_p->initState();
194611886Sbrandon.potter@amd.com    tc->activate();
194711886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
194811886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
194911886Sbrandon.potter@amd.com
195011886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
195111886Sbrandon.potter@amd.com    return 0;
195211886Sbrandon.potter@amd.com}
195311886Sbrandon.potter@amd.com
1954378SN/A/// Target getrusage() function.
1955360SN/Atemplate <class OS>
19561450SN/ASyscallReturn
195711851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
19582680Sktlim@umich.edu              ThreadContext *tc)
1959360SN/A{
19606701Sgblack@eecs.umich.edu    int index = 0;
19616701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
19626701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1963360SN/A
19643670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
19653670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1966360SN/A    rup->ru_stime.tv_sec = 0;
1967360SN/A    rup->ru_stime.tv_usec = 0;
1968360SN/A    rup->ru_maxrss = 0;
1969360SN/A    rup->ru_ixrss = 0;
1970360SN/A    rup->ru_idrss = 0;
1971360SN/A    rup->ru_isrss = 0;
1972360SN/A    rup->ru_minflt = 0;
1973360SN/A    rup->ru_majflt = 0;
1974360SN/A    rup->ru_nswap = 0;
1975360SN/A    rup->ru_inblock = 0;
1976360SN/A    rup->ru_oublock = 0;
1977360SN/A    rup->ru_msgsnd = 0;
1978360SN/A    rup->ru_msgrcv = 0;
1979360SN/A    rup->ru_nsignals = 0;
1980360SN/A    rup->ru_nvcsw = 0;
1981360SN/A    rup->ru_nivcsw = 0;
1982360SN/A
19833670Sbinkertn@umich.edu    switch (who) {
19843670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
198510796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
19868737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
19878737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
19883670Sbinkertn@umich.edu        break;
19893670Sbinkertn@umich.edu
19903670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
19913670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
19923670Sbinkertn@umich.edu        break;
19933670Sbinkertn@umich.edu
19943670Sbinkertn@umich.edu      default:
19953670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
19963670Sbinkertn@umich.edu        // plow ahead
19973670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
19983670Sbinkertn@umich.edu             who);
19993670Sbinkertn@umich.edu    }
20003670Sbinkertn@umich.edu
20018706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
2002360SN/A
20031458SN/A    return 0;
2004360SN/A}
2005360SN/A
20066683Stjones1@inf.ed.ac.uk/// Target times() function.
20076683Stjones1@inf.ed.ac.uktemplate <class OS>
20086683Stjones1@inf.ed.ac.ukSyscallReturn
200911851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
201011851Sbrandon.potter@amd.com          ThreadContext *tc)
20116683Stjones1@inf.ed.ac.uk{
20126701Sgblack@eecs.umich.edu    int index = 0;
20136701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
20146683Stjones1@inf.ed.ac.uk
20156683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
20167823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
20176683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
20186683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
20196683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
20206683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
20216683Stjones1@inf.ed.ac.uk
20226683Stjones1@inf.ed.ac.uk    // Convert to host endianness
20238737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
20246683Stjones1@inf.ed.ac.uk
20256683Stjones1@inf.ed.ac.uk    // Write back
20268706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
20276683Stjones1@inf.ed.ac.uk
20286683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
20296683Stjones1@inf.ed.ac.uk    return clocks;
20306683Stjones1@inf.ed.ac.uk}
20312553SN/A
20326684Stjones1@inf.ed.ac.uk/// Target time() function.
20336684Stjones1@inf.ed.ac.uktemplate <class OS>
20346684Stjones1@inf.ed.ac.ukSyscallReturn
203511851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
20366684Stjones1@inf.ed.ac.uk{
20376684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
203810796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
20396684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
20406684Stjones1@inf.ed.ac.uk
20416701Sgblack@eecs.umich.edu    int index = 0;
20426701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
204311321Ssteve.reinhardt@amd.com    if (taddr != 0) {
20446684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
20458737Skoansin.tan@gmail.com        t = TheISA::htog(t);
20468852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
20478852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
20486684Stjones1@inf.ed.ac.uk    }
20496684Stjones1@inf.ed.ac.uk    return sec;
20506684Stjones1@inf.ed.ac.uk}
20512553SN/A
205211910SBrandon.Potter@amd.comtemplate <class OS>
205311910SBrandon.Potter@amd.comSyscallReturn
205411910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
205511910SBrandon.Potter@amd.com{
205611910SBrandon.Potter@amd.com    int index = 0;
205711910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
205811910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
205911910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
206011910SBrandon.Potter@amd.com
206111910SBrandon.Potter@amd.com    /**
206211910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
206311910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
206411910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
206511910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
206611910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
206711910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
206811910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
206911910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
207011910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
207111910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
207211910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
207311910SBrandon.Potter@amd.com     * due to PID rollover.
207411910SBrandon.Potter@amd.com     */
207511910SBrandon.Potter@amd.com
207611910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
207711910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
207811910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
207911910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
208011910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
208111910SBrandon.Potter@amd.com            tgt_proc = temp;
208211910SBrandon.Potter@amd.com            break;
208311910SBrandon.Potter@amd.com        }
208411910SBrandon.Potter@amd.com    }
208511910SBrandon.Potter@amd.com
208611910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
208711910SBrandon.Potter@amd.com        return -EINVAL;
208811910SBrandon.Potter@amd.com
208911910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
209011910SBrandon.Potter@amd.com        return -ESRCH;
209111910SBrandon.Potter@amd.com
209211910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
209311910SBrandon.Potter@amd.com        return -ESRCH;
209411910SBrandon.Potter@amd.com
209511910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
209611910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
209711910SBrandon.Potter@amd.com
209811910SBrandon.Potter@amd.com    return 0;
209911910SBrandon.Potter@amd.com}
210011910SBrandon.Potter@amd.com
21012553SN/A
21021354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2103