syscall_emul.hh revision 13031
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
2065513SMichael.Adler@intel.com/// Target link() handler
2075513SMichael.Adler@intel.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
20811851Sbrandon.potter@amd.com                       ThreadContext *tc);
2095513SMichael.Adler@intel.com
210511SN/A/// Target symlink() handler.
2111706SN/ASyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
21211851Sbrandon.potter@amd.com                          ThreadContext *tc);
2131706SN/A
2141706SN/A/// Target mkdir() handler.
2151706SN/ASyscallReturn mkdirFunc(SyscallDesc *desc, int num,
2161706SN/A                        Process *p, ThreadContext *tc);
21711851Sbrandon.potter@amd.com
2181706SN/A/// Target mknod() handler.
2191706SN/ASyscallReturn mknodFunc(SyscallDesc *desc, int num,
2201706SN/A                        Process *p, ThreadContext *tc);
2211706SN/A
22211851Sbrandon.potter@amd.com/// Target chdir() handler.
2231706SN/ASyscallReturn chdirFunc(SyscallDesc *desc, int num,
224511SN/A                        Process *p, ThreadContext *tc);
2256703Svince@csl.cornell.edu
2266703Svince@csl.cornell.edu// Target rmdir() handler.
22711851Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
2286703Svince@csl.cornell.edu                        Process *p, ThreadContext *tc);
2296685Stjones1@inf.ed.ac.uk
2306685Stjones1@inf.ed.ac.uk/// Target rename() handler.
23111851Sbrandon.potter@amd.comSyscallReturn renameFunc(SyscallDesc *desc, int num,
2326685Stjones1@inf.ed.ac.uk                         Process *p, ThreadContext *tc);
2336685Stjones1@inf.ed.ac.uk
2345513SMichael.Adler@intel.com
2355513SMichael.Adler@intel.com/// Target truncate() handler.
23611851Sbrandon.potter@amd.comSyscallReturn truncateFunc(SyscallDesc *desc, int num,
2375513SMichael.Adler@intel.com                           Process *p, ThreadContext *tc);
23811885Sbrandon.potter@amd.com
23911885Sbrandon.potter@amd.com
24011885Sbrandon.potter@amd.com/// Target ftruncate() handler.
2415513SMichael.Adler@intel.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2421999SN/A                            Process *p, ThreadContext *tc);
2431999SN/A
24411851Sbrandon.potter@amd.com
2451999SN/A/// Target truncate64() handler.
24611885Sbrandon.potter@amd.comSyscallReturn truncate64Func(SyscallDesc *desc, int num,
24711885Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
24811885Sbrandon.potter@amd.com
2491999SN/A/// Target ftruncate64() handler.
2501999SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
2511999SN/A                              Process *p, ThreadContext *tc);
25211851Sbrandon.potter@amd.com
2531999SN/A
2543079Sstever@eecs.umich.edu/// Target umask() handler.
2553079Sstever@eecs.umich.eduSyscallReturn umaskFunc(SyscallDesc *desc, int num,
25611851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2573079Sstever@eecs.umich.edu
25811908SBrandon.Potter@amd.com/// Target gettid() handler.
25911908SBrandon.Potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
26011908SBrandon.Potter@amd.com                         Process *p, ThreadContext *tc);
26111908SBrandon.Potter@amd.com
26211875Sbrandon.potter@amd.com/// Target chown() handler.
2632093SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26411851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2652093SN/A
2662687Sksewell@umich.edu/// Target setpgid() handler.
2672687Sksewell@umich.eduSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
26811851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2692687Sksewell@umich.edu
2702238SN/A/// Target fchown() handler.
2712238SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
27211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2732238SN/A
27411908SBrandon.Potter@amd.com/// Target dup() handler.
27511908SBrandon.Potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num,
27611908SBrandon.Potter@amd.com                      Process *process, ThreadContext *tc);
27711908SBrandon.Potter@amd.com
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
2822238SN/A/// Target fcntl() handler.
2832238SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28411851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2852238SN/A
2862238SN/A/// Target fcntl64() handler.
2872238SN/ASyscallReturn fcntl64Func(SyscallDesc *desc, int num,
28811851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2892238SN/A
2902238SN/A/// Target setuid() handler.
2912238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
29211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2932238SN/A
2942238SN/A/// Target pipe() handler.
2952238SN/ASyscallReturn pipeFunc(SyscallDesc *desc, int num,
29611851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
2972238SN/A
2982238SN/A/// Internal pipe() handler.
2992238SN/ASyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
30011851Sbrandon.potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
3012238SN/A
3022238SN/A/// Target getpid() handler.
3032238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3052238SN/A
3069455Smitch.hayenga+gem5@gmail.com// Target getdents() handler.
3079455Smitch.hayenga+gem5@gmail.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
30811851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
30910203SAli.Saidi@ARM.com
31011851Sbrandon.potter@amd.com// Target getuid() handler.
31111851Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num,
3129455Smitch.hayenga+gem5@gmail.com                         Process *p, ThreadContext *tc);
3139112Smarc.orr@gmail.com
31411906SBrandon.Potter@amd.com/// Target getgid() handler.
31511906SBrandon.Potter@amd.comSyscallReturn getgidFunc(SyscallDesc *desc, int num,
3169112Smarc.orr@gmail.com                         Process *p, ThreadContext *tc);
3179112Smarc.orr@gmail.com
31811851Sbrandon.potter@amd.com/// Target getppid() handler.
3199112Smarc.orr@gmail.comSyscallReturn getppidFunc(SyscallDesc *desc, int num,
3209112Smarc.orr@gmail.com                          Process *p, ThreadContext *tc);
32111911SBrandon.Potter@amd.com
3229112Smarc.orr@gmail.com/// Target geteuid() handler.
32311911SBrandon.Potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num,
32411911SBrandon.Potter@amd.com                          Process *p, ThreadContext *tc);
32511911SBrandon.Potter@amd.com
32611911SBrandon.Potter@amd.com/// Target getegid() handler.
3279112Smarc.orr@gmail.comSyscallReturn getegidFunc(SyscallDesc *desc, int num,
32811911SBrandon.Potter@amd.com                          Process *p, ThreadContext *tc);
32911911SBrandon.Potter@amd.com
33011911SBrandon.Potter@amd.com/// Target access() handler
33111911SBrandon.Potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3329238Slluc.alvarez@bsc.es                         Process *p, ThreadContext *tc);
3339112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
33411911SBrandon.Potter@amd.com                         Process *p, ThreadContext *tc,
3359112Smarc.orr@gmail.com                         int index);
33611911SBrandon.Potter@amd.com
33711911SBrandon.Potter@amd.com/// Futex system call
33811911SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
33911911SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
34011911SBrandon.Potter@amd.comtemplate <class OS>
3419112Smarc.orr@gmail.comSyscallReturn
34211911SBrandon.Potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
34311911SBrandon.Potter@amd.com          ThreadContext *tc)
34411911SBrandon.Potter@amd.com{
34511911SBrandon.Potter@amd.com    using namespace std;
34611911SBrandon.Potter@amd.com
34711911SBrandon.Potter@amd.com    int index = 0;
3489112Smarc.orr@gmail.com    Addr uaddr = process->getSyscallArg(tc, index);
3499112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index);
35011911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
35111911SBrandon.Potter@amd.com
3529112Smarc.orr@gmail.com    /*
35311911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
35411911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
3559112Smarc.orr@gmail.com     */
3569112Smarc.orr@gmail.com    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
35711911SBrandon.Potter@amd.com
35811911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
3599112Smarc.orr@gmail.com
3609112Smarc.orr@gmail.com    if (OS::TGT_FUTEX_WAIT == op) {
3612238SN/A        // Ensure futex system call accessed atomically.
3622238SN/A        BufferArg buf(uaddr, sizeof(int));
3632238SN/A        buf.copyIn(tc->getMemProxy());
3642238SN/A        int mem_val = *(int*)buf.bufferPtr();
36511851Sbrandon.potter@amd.com
3662238SN/A        /*
3672238SN/A         * The value in memory at uaddr is not equal with the expected val
3682238SN/A         * (a different thread must have changed it before the system call was
36911851Sbrandon.potter@amd.com         * invoked). In this case, we need to throw an error.
3702238SN/A         */
3712238SN/A        if (val != mem_val)
3722238SN/A            return -OS::TGT_EWOULDBLOCK;
37311851Sbrandon.potter@amd.com
3742238SN/A        futex_map.suspend(uaddr, process->tgid(), tc);
3752238SN/A
3762238SN/A        return 0;
37711851Sbrandon.potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
3782238SN/A        return futex_map.wakeup(uaddr, process->tgid(), val);
3792238SN/A    }
3801354SN/A
3811354SN/A    warn("futex: op %d not implemented; ignoring.", op);
38210796Sbrandon.potter@amd.com    return -ENOSYS;
38310796Sbrandon.potter@amd.com}
3841354SN/A
3851354SN/A
3861354SN/A/// Pseudo Funcs  - These functions use a different return convension,
3871354SN/A/// returning a second value in a register other than the normal return register
3881354SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3891354SN/A                             Process *process, ThreadContext *tc);
3901354SN/A
3911354SN/A/// Target getpidPseudo() handler.
3921354SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3931354SN/A                               Process *p, ThreadContext *tc);
39410796Sbrandon.potter@amd.com
3951354SN/A/// Target getuidPseudo() handler.
39610796Sbrandon.potter@amd.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3971354SN/A                               Process *p, ThreadContext *tc);
3981354SN/A
3991354SN/A/// Target getgidPseudo() handler.
4001354SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
40110796Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
40210796Sbrandon.potter@amd.com
40310796Sbrandon.potter@amd.com
40410796Sbrandon.potter@amd.com/// A readable name for 1,000,000, for converting microseconds to seconds.
40510796Sbrandon.potter@amd.comconst int one_million = 1000000;
40610796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
40710796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
40810796Sbrandon.potter@amd.com
40910796Sbrandon.potter@amd.com/// Approximate seconds since the epoch (1/1/1970).  About a billion,
41010796Sbrandon.potter@amd.com/// by my reckoning.  We want to keep this a constant (not use the
41110796Sbrandon.potter@amd.com/// real-world time) to keep simulations repeatable.
412360SN/Aconst unsigned seconds_since_epoch = 1000000000;
413360SN/A
414360SN/A/// Helper function to convert current elapsed time to seconds and
415360SN/A/// microseconds.
416360SN/Atemplate <class T1, class T2>
417360SN/Avoid
418360SN/AgetElapsedTimeMicro(T1 &sec, T2 &usec)
41911759Sbrandon.potter@amd.com{
4203113Sgblack@eecs.umich.edu    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4213113Sgblack@eecs.umich.edu    sec = elapsed_usecs / one_million;
4223113Sgblack@eecs.umich.edu    usec = elapsed_usecs % one_million;
4233113Sgblack@eecs.umich.edu}
4243113Sgblack@eecs.umich.edu
4253113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
4263113Sgblack@eecs.umich.edu/// nanoseconds.
4273113Sgblack@eecs.umich.edutemplate <class T1, class T2>
4283113Sgblack@eecs.umich.eduvoid
4293113Sgblack@eecs.umich.edugetElapsedTimeNano(T1 &sec, T2 &nsec)
4303113Sgblack@eecs.umich.edu{
4313113Sgblack@eecs.umich.edu    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
4323113Sgblack@eecs.umich.edu    sec = elapsed_nsecs / one_billion;
43312032Sandreas.sandberg@arm.com    nsec = elapsed_nsecs % one_billion;
4343113Sgblack@eecs.umich.edu}
4353113Sgblack@eecs.umich.edu
4364189Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////
4374189Sgblack@eecs.umich.edu//
4383113Sgblack@eecs.umich.edu// The following emulation functions are generic, but need to be
4393113Sgblack@eecs.umich.edu// templated to account for differences in types, constants, etc.
4403113Sgblack@eecs.umich.edu//
4413113Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////
4428737Skoansin.tan@gmail.com
4433113Sgblack@eecs.umich.edu    typedef struct statfs hst_statfs;
4448737Skoansin.tan@gmail.com#if NO_STAT64
4453277Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4465515SMichael.Adler@intel.com    typedef struct stat hst_stat64;
4475515SMichael.Adler@intel.com#else
4485515SMichael.Adler@intel.com    typedef struct stat hst_stat;
4495515SMichael.Adler@intel.com    typedef struct stat64 hst_stat64;
4505515SMichael.Adler@intel.com#endif
4518737Skoansin.tan@gmail.com
4523277Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4538737Skoansin.tan@gmail.com//// buffer.  Also copies the target buffer out to the simulated
4543277Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4558737Skoansin.tan@gmail.com
4563277Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4578737Skoansin.tan@gmail.comvoid
4583113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4593113Sgblack@eecs.umich.edu{
4603113Sgblack@eecs.umich.edu    using namespace TheISA;
4613113Sgblack@eecs.umich.edu
4628737Skoansin.tan@gmail.com    if (fakeTTY)
4633113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4648737Skoansin.tan@gmail.com    else
4653114Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4668737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4673114Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4688737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4693114Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4708737Skoansin.tan@gmail.com    if (fakeTTY) {
47111906SBrandon.Potter@amd.com        // Claim to be a character device
4724061Sgblack@eecs.umich.edu        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4734061Sgblack@eecs.umich.edu        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4748737Skoansin.tan@gmail.com    }
4753113Sgblack@eecs.umich.edu    tgt->st_mode = TheISA::htog(tgt->st_mode);
4768737Skoansin.tan@gmail.com    tgt->st_nlink = host->st_nlink;
4773113Sgblack@eecs.umich.edu    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4783113Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4793113Sgblack@eecs.umich.edu    tgt->st_uid = TheISA::htog(tgt->st_uid);
4803113Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4813113Sgblack@eecs.umich.edu    tgt->st_gid = TheISA::htog(tgt->st_gid);
48212032Sandreas.sandberg@arm.com    if (fakeTTY)
4833113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4843113Sgblack@eecs.umich.edu    else
4854189Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4864189Sgblack@eecs.umich.edu    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4873113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4883113Sgblack@eecs.umich.edu    tgt->st_size = TheISA::htog(tgt->st_size);
4893113Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4908737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4913113Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4928737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4933113Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4948737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4953113Sgblack@eecs.umich.edu    // Force the block size to be 8KB. This helps to ensure buffered io works
4963113Sgblack@eecs.umich.edu    // consistently across different hosts.
4973113Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4983113Sgblack@eecs.umich.edu    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4993113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5003113Sgblack@eecs.umich.edu    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5013113Sgblack@eecs.umich.edu}
50211906SBrandon.Potter@amd.com
5033113Sgblack@eecs.umich.edu// Same for stat64
50412032Sandreas.sandberg@arm.com
5058852Sandreas.hansson@arm.comtemplate <typename target_stat, typename host_stat64>
50611906SBrandon.Potter@amd.comvoid
5073113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5083113Sgblack@eecs.umich.edu{
5093113Sgblack@eecs.umich.edu    using namespace TheISA;
5103113Sgblack@eecs.umich.edu
5113113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5123113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5133113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5143113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
51512032Sandreas.sandberg@arm.com    tgt->st_mtime_nsec = host->st_mtime_nsec;
5168852Sandreas.hansson@arm.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
51711906SBrandon.Potter@amd.com    tgt->st_ctime_nsec = host->st_ctime_nsec;
5183113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5193113Sgblack@eecs.umich.edu#else
5203113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5216686Stjones1@inf.ed.ac.uk    tgt->st_mtime_nsec = 0;
5223113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5233113Sgblack@eecs.umich.edu#endif
5243113Sgblack@eecs.umich.edu}
52511759Sbrandon.potter@amd.com
52612032Sandreas.sandberg@arm.com// Here are a couple of convenience functions
52711759Sbrandon.potter@amd.comtemplate<class OS>
52811759Sbrandon.potter@amd.comvoid
52911759Sbrandon.potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
53011759Sbrandon.potter@amd.com               hst_stat *host, bool fakeTTY = false)
53111759Sbrandon.potter@amd.com{
53211812Sbaz21@cam.ac.uk    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
53311812Sbaz21@cam.ac.uk    tgt_stat_buf tgt(addr);
53411812Sbaz21@cam.ac.uk    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
53511759Sbrandon.potter@amd.com    tgt.copyOut(mem);
53611812Sbaz21@cam.ac.uk}
53711759Sbrandon.potter@amd.com
53811759Sbrandon.potter@amd.comtemplate<class OS>
53911759Sbrandon.potter@amd.comvoid
54011759Sbrandon.potter@amd.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
54111759Sbrandon.potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
54211759Sbrandon.potter@amd.com{
54311759Sbrandon.potter@amd.com    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
54411812Sbaz21@cam.ac.uk    tgt_stat_buf tgt(addr);
54511812Sbaz21@cam.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
54611812Sbaz21@cam.ac.uk    tgt.copyOut(mem);
54711812Sbaz21@cam.ac.uk}
54811812Sbaz21@cam.ac.uk
54911812Sbaz21@cam.ac.uktemplate <class OS>
55011812Sbaz21@cam.ac.ukvoid
55111759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
55211759Sbrandon.potter@amd.com                 hst_statfs *host)
55311812Sbaz21@cam.ac.uk{
55411812Sbaz21@cam.ac.uk    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
55511759Sbrandon.potter@amd.com
55611812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
55711812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
55811812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
55911812Sbaz21@cam.ac.uk#else
56011812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
56111812Sbaz21@cam.ac.uk#endif
56211812Sbaz21@cam.ac.uk    tgt->f_blocks = TheISA::htog(host->f_blocks);
56311759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
56411759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
56511759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
56611759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
567378SN/A    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
568378SN/A#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
5699141Smarc.orr@gmail.com    tgt->f_namelen = TheISA::htog(host->f_namemax);
5709141Smarc.orr@gmail.com    tgt->f_frsize = TheISA::htog(host->f_bsize);
571360SN/A#elif defined(__APPLE__)
5721450SN/A    tgt->f_namelen = 0;
57311856Sbrandon.potter@amd.com    tgt->f_frsize = 0;
574360SN/A#else
5756701Sgblack@eecs.umich.edu    tgt->f_namelen = TheISA::htog(host->f_namelen);
57611856Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
57711856Sbrandon.potter@amd.com#endif
578360SN/A#if defined(__linux__)
57910930Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
580360SN/A#else
58111856Sbrandon.potter@amd.com    /*
58211856Sbrandon.potter@amd.com     * The fields are different sizes per OS. Don't bother with
58310496Ssteve.reinhardt@amd.com     * f_spare or f_reserved on non-Linux for now.
58411856Sbrandon.potter@amd.com     */
58511856Sbrandon.potter@amd.com    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
5861458SN/A#endif
587360SN/A
58811856Sbrandon.potter@amd.com    tgt.copyOut(mem);
58911856Sbrandon.potter@amd.com}
59011856Sbrandon.potter@amd.com
59111856Sbrandon.potter@amd.com/// Target ioctl() handler.  For the most part, programs call ioctl()
59211856Sbrandon.potter@amd.com/// only to find out if their stdout is a tty, to determine whether to
59311856Sbrandon.potter@amd.com/// do line or block buffering.  We always claim that output fds are
59411856Sbrandon.potter@amd.com/// not TTYs to provide repeatable results.
59511856Sbrandon.potter@amd.comtemplate <class OS>
59610496Ssteve.reinhardt@amd.comSyscallReturn
59711856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
59811856Sbrandon.potter@amd.com{
59911856Sbrandon.potter@amd.com    int index = 0;
60011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
60111856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
60210930Sbrandon.potter@amd.com
6039141Smarc.orr@gmail.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
604360SN/A
605360SN/A    if (OS::isTtyReq(req))
606360SN/A        return -ENOTTY;
60711907SBrandon.Potter@amd.com
60811907SBrandon.Potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
60911907SBrandon.Potter@amd.com    if (!dfdp)
610360SN/A        return -EBADF;
61111907SBrandon.Potter@amd.com
61211907SBrandon.Potter@amd.com    /**
61311907SBrandon.Potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
61411907SBrandon.Potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
61511907SBrandon.Potter@amd.com     * return that we do not have a valid TTY.
61611907SBrandon.Potter@amd.com     */
61711907SBrandon.Potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
61811907SBrandon.Potter@amd.com    if (emul_driver)
61911907SBrandon.Potter@amd.com        return emul_driver->ioctl(p, tc, req);
62011907SBrandon.Potter@amd.com
62111907SBrandon.Potter@amd.com    /**
62211907SBrandon.Potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
62311907SBrandon.Potter@amd.com     * return something better here, but at least we issue the warning.
62411907SBrandon.Potter@amd.com     */
625360SN/A    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
62611907SBrandon.Potter@amd.com         tgt_fd, req, tc->pcState());
6271458SN/A    return -ENOTTY;
628360SN/A}
62911907SBrandon.Potter@amd.com
63011907SBrandon.Potter@amd.comtemplate <class OS>
63111907SBrandon.Potter@amd.comSyscallReturn
63211907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
63311907SBrandon.Potter@amd.com         bool isopenat)
63411907SBrandon.Potter@amd.com{
63511907SBrandon.Potter@amd.com    int index = 0;
63611907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
63711907SBrandon.Potter@amd.com
63811907SBrandon.Potter@amd.com    /**
639360SN/A     * If using the openat variant, read in the target directory file
64011907SBrandon.Potter@amd.com     * descriptor from the simulated process.
64111907SBrandon.Potter@amd.com     */
64211907SBrandon.Potter@amd.com    if (isopenat)
643360SN/A        tgt_dirfd = p->getSyscallArg(tc, index);
644360SN/A
64511907SBrandon.Potter@amd.com    /**
64611907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
64711907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
64811907SBrandon.Potter@amd.com     */
649360SN/A    std::string path;
65011907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
651360SN/A        return -EFAULT;
652360SN/A
65311907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
6543669Sbinkertn@umich.edu    int host_flags = O_BINARY;
65511907SBrandon.Potter@amd.com#else
65611907SBrandon.Potter@amd.com    int host_flags = 0;
65711907SBrandon.Potter@amd.com#endif
65811907SBrandon.Potter@amd.com    /**
65911907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
66011907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
66111907SBrandon.Potter@amd.com     */
66211907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
66311907SBrandon.Potter@amd.com    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
66411907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
66511907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
66611907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
66711907SBrandon.Potter@amd.com        }
66811907SBrandon.Potter@amd.com    }
66911907SBrandon.Potter@amd.com    if (tgt_flags) {
67011907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
67111907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
67211907SBrandon.Potter@amd.com    }
67311907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
67411907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
67511907SBrandon.Potter@amd.com#endif
6761706SN/A
67711907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
67811907SBrandon.Potter@amd.com
67911907SBrandon.Potter@amd.com    /**
68011907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
68111907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
68211907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
68310496Ssteve.reinhardt@amd.com     * create a full path.
68410496Ssteve.reinhardt@amd.com     * Otherwise, openat with a valid target directory file descriptor has
68511907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
68611907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
68711907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
68811907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
68911907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
69011907SBrandon.Potter@amd.com     */
69110496Ssteve.reinhardt@amd.com    if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) {
69211907SBrandon.Potter@amd.com        path = p->fullPath(path);
69311907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
69411907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
69511907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
69610496Ssteve.reinhardt@amd.com        if (!ffdp)
69710496Ssteve.reinhardt@amd.com            return -EBADF;
69811907SBrandon.Potter@amd.com        path.insert(0, ffdp->getFileName());
69911907SBrandon.Potter@amd.com    }
70011907SBrandon.Potter@amd.com
70111907SBrandon.Potter@amd.com    /**
70211907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
70311907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
70411907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
70511907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
70611907SBrandon.Potter@amd.com     */
70711907SBrandon.Potter@amd.com    if (startswith(path, "/dev/")) {
70811907SBrandon.Potter@amd.com        std::string filename = path.substr(strlen("/dev/"));
70911907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
71011907SBrandon.Potter@amd.com        if (drv) {
71111907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
71211907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
71311907SBrandon.Potter@amd.com                            isopenat ? "at" : "", path.c_str());
71411907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
71511907SBrandon.Potter@amd.com        }
71611907SBrandon.Potter@amd.com        /**
71711907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
71811907SBrandon.Potter@amd.com         * as /dev/zero
71911907SBrandon.Potter@amd.com         */
72011907SBrandon.Potter@amd.com    }
72111907SBrandon.Potter@amd.com
72211907SBrandon.Potter@amd.com    /**
723360SN/A     * Some special paths and files cannot be called on the host and need
72411907SBrandon.Potter@amd.com     * to be handled as special cases inside the simulator.
72511907SBrandon.Potter@amd.com     * If the full path that was created above does not match any of the
72611907SBrandon.Potter@amd.com     * special cases, pass it through to the open call on the host to let
72711907SBrandon.Potter@amd.com     * the host open the file on our behalf.
72811907SBrandon.Potter@amd.com     * If the host cannot open the file, return the host's error code back
72911907SBrandon.Potter@amd.com     * through the system call to the simulated process.
73011907SBrandon.Potter@amd.com     */
73111907SBrandon.Potter@amd.com    int sim_fd = -1;
73211907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
73311907SBrandon.Potter@amd.com            { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" };
73411907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
73511907SBrandon.Potter@amd.com        if (startswith(path, entry))
73611907SBrandon.Potter@amd.com            sim_fd = OS::openSpecialFile(path, p, tc);
737360SN/A    }
738360SN/A    if (sim_fd == -1) {
73910027SChris.Adeniyi-Jones@arm.com        sim_fd = open(path.c_str(), host_flags, mode);
74010027SChris.Adeniyi-Jones@arm.com    }
74110027SChris.Adeniyi-Jones@arm.com    if (sim_fd == -1) {
74211851Sbrandon.potter@amd.com        int local = -errno;
74310027SChris.Adeniyi-Jones@arm.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n",
74410027SChris.Adeniyi-Jones@arm.com                        isopenat ? "at" : "", path.c_str());
74511907SBrandon.Potter@amd.com        return local;
74610027SChris.Adeniyi-Jones@arm.com    }
74710027SChris.Adeniyi-Jones@arm.com
74810027SChris.Adeniyi-Jones@arm.com    /**
74910027SChris.Adeniyi-Jones@arm.com     * The file was opened successfully and needs to be recorded in the
75010027SChris.Adeniyi-Jones@arm.com     * process' file descriptor array so that it can be retrieved later.
75111851Sbrandon.potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
75211851Sbrandon.potter@amd.com     * file descriptor.
75310027SChris.Adeniyi-Jones@arm.com     * Return the indirect target file descriptor back to the simulated
75411907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
75510027SChris.Adeniyi-Jones@arm.com     */
75610027SChris.Adeniyi-Jones@arm.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
75710633Smichaelupton@gmail.com    int tgt_fd = p->fds->allocFD(ffdp);
75810633Smichaelupton@gmail.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n",
75910633Smichaelupton@gmail.com                    isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str());
76011851Sbrandon.potter@amd.com    return tgt_fd;
76110633Smichaelupton@gmail.com}
76210633Smichaelupton@gmail.com
76310633Smichaelupton@gmail.com/// Target open() handler.
76410633Smichaelupton@gmail.comtemplate <class OS>
76510633Smichaelupton@gmail.comSyscallReturn
76610633Smichaelupton@gmail.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
76710633Smichaelupton@gmail.com         ThreadContext *tc)
76810633Smichaelupton@gmail.com{
76910633Smichaelupton@gmail.com    return openImpl<OS>(desc, callnum, process, tc, false);
77010633Smichaelupton@gmail.com}
77110203SAli.Saidi@ARM.com
77210203SAli.Saidi@ARM.com/// Target openat() handler.
77310203SAli.Saidi@ARM.comtemplate <class OS>
77411851Sbrandon.potter@amd.comSyscallReturn
77511851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
77610203SAli.Saidi@ARM.com           ThreadContext *tc)
77710203SAli.Saidi@ARM.com{
77810203SAli.Saidi@ARM.com    return openImpl<OS>(desc, callnum, process, tc, true);
77910203SAli.Saidi@ARM.com}
78010203SAli.Saidi@ARM.com
78110203SAli.Saidi@ARM.com/// Target unlinkat() handler.
78210203SAli.Saidi@ARM.comtemplate <class OS>
78310203SAli.Saidi@ARM.comSyscallReturn
78410203SAli.Saidi@ARM.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
78510203SAli.Saidi@ARM.com             ThreadContext *tc)
78610203SAli.Saidi@ARM.com{
78711851Sbrandon.potter@amd.com    int index = 0;
78811851Sbrandon.potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
78910203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
79010203SAli.Saidi@ARM.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
79110203SAli.Saidi@ARM.com
79210203SAli.Saidi@ARM.com    return unlinkHelper(desc, callnum, process, tc, 1);
79310203SAli.Saidi@ARM.com}
79410203SAli.Saidi@ARM.com
79510203SAli.Saidi@ARM.com/// Target facessat() handler
79610203SAli.Saidi@ARM.comtemplate <class OS>
79710850SGiacomo.Gabrielli@arm.comSyscallReturn
79810850SGiacomo.Gabrielli@arm.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
79910850SGiacomo.Gabrielli@arm.com              ThreadContext *tc)
80011851Sbrandon.potter@amd.com{
80110850SGiacomo.Gabrielli@arm.com    int index = 0;
80210850SGiacomo.Gabrielli@arm.com    int dirfd = process->getSyscallArg(tc, index);
80310850SGiacomo.Gabrielli@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
80410850SGiacomo.Gabrielli@arm.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
80510850SGiacomo.Gabrielli@arm.com    return accessFunc(desc, callnum, process, tc, 1);
80610850SGiacomo.Gabrielli@arm.com}
80710850SGiacomo.Gabrielli@arm.com
80810850SGiacomo.Gabrielli@arm.com/// Target readlinkat() handler
80910850SGiacomo.Gabrielli@arm.comtemplate <class OS>
81010850SGiacomo.Gabrielli@arm.comSyscallReturn
81110850SGiacomo.Gabrielli@arm.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
81210850SGiacomo.Gabrielli@arm.com               ThreadContext *tc)
81310850SGiacomo.Gabrielli@arm.com{
81410850SGiacomo.Gabrielli@arm.com    int index = 0;
81510850SGiacomo.Gabrielli@arm.com    int dirfd = process->getSyscallArg(tc, index);
81610850SGiacomo.Gabrielli@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
81710850SGiacomo.Gabrielli@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
81810850SGiacomo.Gabrielli@arm.com    return readlinkFunc(desc, callnum, process, tc, 1);
81910850SGiacomo.Gabrielli@arm.com}
82010850SGiacomo.Gabrielli@arm.com
82110850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
82210850SGiacomo.Gabrielli@arm.comtemplate <class OS>
82310850SGiacomo.Gabrielli@arm.comSyscallReturn
82410850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
82510850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
82610850SGiacomo.Gabrielli@arm.com{
82710850SGiacomo.Gabrielli@arm.com    int index = 0;
82810850SGiacomo.Gabrielli@arm.com
82910850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
83010850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
83110850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
83210850SGiacomo.Gabrielli@arm.com
8336640Svince@csl.cornell.edu    std::string old_name;
8346640Svince@csl.cornell.edu
8356640Svince@csl.cornell.edu    if (!tc->getMemProxy().tryReadString(old_name,
83611851Sbrandon.potter@amd.com                                         process->getSyscallArg(tc, index)))
83711851Sbrandon.potter@amd.com        return -EFAULT;
8386640Svince@csl.cornell.edu
8396640Svince@csl.cornell.edu    int newdirfd = process->getSyscallArg(tc, index);
8406701Sgblack@eecs.umich.edu    if (newdirfd != OS::TGT_AT_FDCWD)
8416701Sgblack@eecs.umich.edu        warn("renameat: third argument not AT_FDCWD; unlikely to work");
84210793Sbrandon.potter@amd.com
8436640Svince@csl.cornell.edu    std::string new_name;
84411758Sbrandon.potter@amd.com
84511758Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(new_name,
84611758Sbrandon.potter@amd.com                                         process->getSyscallArg(tc, index)))
8476640Svince@csl.cornell.edu        return -EFAULT;
8488706Sandreas.hansson@arm.com
8496640Svince@csl.cornell.edu    // Adjust path for current working directory
8506701Sgblack@eecs.umich.edu    old_name = process->fullPath(old_name);
8516640Svince@csl.cornell.edu    new_name = process->fullPath(new_name);
852360SN/A
8531999SN/A    int result = rename(old_name.c_str(), new_name.c_str());
8541999SN/A    return (result == -1) ? -errno : result;
8551999SN/A}
85611851Sbrandon.potter@amd.com
8572680Sktlim@umich.edu/// Target sysinfo() handler.
8581999SN/Atemplate <class OS>
8591999SN/ASyscallReturn
8601999SN/AsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
8616701Sgblack@eecs.umich.edu            ThreadContext *tc)
8628852Sandreas.hansson@arm.com{
8636701Sgblack@eecs.umich.edu
8641999SN/A    int index = 0;
8656701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
8661999SN/A        sysinfo(process->getSyscallArg(tc, index));
8676701Sgblack@eecs.umich.edu
8681999SN/A    sysinfo->uptime = seconds_since_epoch;
8691999SN/A    sysinfo->totalram = process->system->memSize();
8701999SN/A    sysinfo->mem_unit = 1;
8711999SN/A
8721999SN/A    sysinfo.copyOut(tc->getMemProxy());
8733669Sbinkertn@umich.edu
8743669Sbinkertn@umich.edu    return 0;
8753669Sbinkertn@umich.edu}
8761999SN/A
8771999SN/A/// Target chmod() handler.
8781999SN/Atemplate <class OS>
8792218SN/ASyscallReturn
8801999SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process,
8811999SN/A          ThreadContext *tc)
8821999SN/A{
8831999SN/A    std::string path;
8841999SN/A
8851999SN/A    int index = 0;
8861999SN/A    if (!tc->getMemProxy().tryReadString(path,
8871999SN/A                process->getSyscallArg(tc, index))) {
88811856Sbrandon.potter@amd.com        return -EFAULT;
8891999SN/A    }
8906701Sgblack@eecs.umich.edu
89111856Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
89211856Sbrandon.potter@amd.com    mode_t hostMode = 0;
89310931Sbrandon.potter@amd.com
89411856Sbrandon.potter@amd.com    // XXX translate mode flags via OS::something???
89511856Sbrandon.potter@amd.com    hostMode = mode;
8961999SN/A
89711856Sbrandon.potter@amd.com    // Adjust path for current working directory
8981999SN/A    path = process->fullPath(path);
89911856Sbrandon.potter@amd.com
9001999SN/A    // do the chmod
90111856Sbrandon.potter@amd.com    int result = chmod(path.c_str(), hostMode);
9021999SN/A    if (result < 0)
90311856Sbrandon.potter@amd.com        return -errno;
9041999SN/A
9051999SN/A    return 0;
9065877Shsul@eecs.umich.edu}
9075877Shsul@eecs.umich.edu
9085877Shsul@eecs.umich.edu
90911851Sbrandon.potter@amd.com/// Target fchmod() handler.
9105877Shsul@eecs.umich.edutemplate <class OS>
9116701Sgblack@eecs.umich.eduSyscallReturn
9126701Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
9136701Sgblack@eecs.umich.edu{
9146701Sgblack@eecs.umich.edu    int index = 0;
9156701Sgblack@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
91610027SChris.Adeniyi-Jones@arm.com    uint32_t mode = p->getSyscallArg(tc, index);
91710027SChris.Adeniyi-Jones@arm.com
91810027SChris.Adeniyi-Jones@arm.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
91910027SChris.Adeniyi-Jones@arm.com    if (!ffdp)
92010027SChris.Adeniyi-Jones@arm.com        return -EBADF;
9215877Shsul@eecs.umich.edu    int sim_fd = ffdp->getSimFD();
92210318Sandreas.hansson@arm.com
92310318Sandreas.hansson@arm.com    mode_t hostMode = mode;
9245877Shsul@eecs.umich.edu
9255877Shsul@eecs.umich.edu    int result = fchmod(sim_fd, hostMode);
9265877Shsul@eecs.umich.edu
9275877Shsul@eecs.umich.edu    return (result < 0) ? -errno : 0;
92810486Stjablin@gmail.com}
92910486Stjablin@gmail.com
9305877Shsul@eecs.umich.edu/// Target mremap() handler.
93111905SBrandon.Potter@amd.comtemplate <class OS>
93211905SBrandon.Potter@amd.comSyscallReturn
93311905SBrandon.Potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
93411905SBrandon.Potter@amd.com{
93510027SChris.Adeniyi-Jones@arm.com    int index = 0;
93612206Srico.amslinger@informatik.uni-augsburg.de    Addr start = process->getSyscallArg(tc, index);
93712206Srico.amslinger@informatik.uni-augsburg.de    uint64_t old_length = process->getSyscallArg(tc, index);
9385877Shsul@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
93911905SBrandon.Potter@amd.com    uint64_t flags = process->getSyscallArg(tc, index);
94011905SBrandon.Potter@amd.com    uint64_t provided_address = 0;
9415877Shsul@eecs.umich.edu    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
9425877Shsul@eecs.umich.edu
94310027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
9445877Shsul@eecs.umich.edu        provided_address = process->getSyscallArg(tc, index);
9455877Shsul@eecs.umich.edu
9465877Shsul@eecs.umich.edu    if ((start % TheISA::PageBytes != 0) ||
94712206Srico.amslinger@informatik.uni-augsburg.de        (provided_address % TheISA::PageBytes != 0)) {
94812206Srico.amslinger@informatik.uni-augsburg.de        warn("mremap failing: arguments not page aligned");
94912206Srico.amslinger@informatik.uni-augsburg.de        return -EINVAL;
95012206Srico.amslinger@informatik.uni-augsburg.de    }
95112206Srico.amslinger@informatik.uni-augsburg.de
95212206Srico.amslinger@informatik.uni-augsburg.de    new_length = roundUp(new_length, TheISA::PageBytes);
95312206Srico.amslinger@informatik.uni-augsburg.de
95412206Srico.amslinger@informatik.uni-augsburg.de    if (new_length > old_length) {
95512206Srico.amslinger@informatik.uni-augsburg.de        std::shared_ptr<MemState> mem_state = process->memState;
95610027SChris.Adeniyi-Jones@arm.com        Addr mmap_end = mem_state->getMmapEnd();
95710027SChris.Adeniyi-Jones@arm.com
95810027SChris.Adeniyi-Jones@arm.com        if ((start + old_length) == mmap_end &&
95910027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
9605877Shsul@eecs.umich.edu            // This case cannot occur when growing downward, as
96110027SChris.Adeniyi-Jones@arm.com            // start is greater than or equal to mmap_end.
96210027SChris.Adeniyi-Jones@arm.com            uint64_t diff = new_length - old_length;
96310027SChris.Adeniyi-Jones@arm.com            process->allocateMem(mmap_end, diff);
96410027SChris.Adeniyi-Jones@arm.com            mem_state->setMmapEnd(mmap_end + diff);
96512206Srico.amslinger@informatik.uni-augsburg.de            return start;
96612206Srico.amslinger@informatik.uni-augsburg.de        } else {
96712206Srico.amslinger@informatik.uni-augsburg.de            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
96812206Srico.amslinger@informatik.uni-augsburg.de                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
96910027SChris.Adeniyi-Jones@arm.com                return -ENOMEM;
97010027SChris.Adeniyi-Jones@arm.com            } else {
97110027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = provided_address;
97210027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address) {
97310027SChris.Adeniyi-Jones@arm.com                    new_start = process->mmapGrowsDown() ?
97410027SChris.Adeniyi-Jones@arm.com                                mmap_end - new_length : mmap_end;
9755877Shsul@eecs.umich.edu                    mmap_end = process->mmapGrowsDown() ?
9765877Shsul@eecs.umich.edu                               new_start : mmap_end + new_length;
9775877Shsul@eecs.umich.edu                    mem_state->setMmapEnd(mmap_end);
97810027SChris.Adeniyi-Jones@arm.com                }
97910027SChris.Adeniyi-Jones@arm.com
9808601Ssteve.reinhardt@amd.com                process->pTable->remap(start, old_length, new_start);
98110027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
9825877Shsul@eecs.umich.edu                     new_start, new_start + new_length,
9835877Shsul@eecs.umich.edu                     new_length - old_length);
9841999SN/A                // add on the remaining unallocated pages
985378SN/A                process->allocateMem(new_start + old_length,
986360SN/A                                     new_length - old_length,
9871450SN/A                                     use_provided_address /* clobber */);
98811851Sbrandon.potter@amd.com                if (use_provided_address &&
9892680Sktlim@umich.edu                    ((new_start + new_length > mem_state->getMmapEnd() &&
990360SN/A                      !process->mmapGrowsDown()) ||
991360SN/A                    (new_start < mem_state->getMmapEnd() &&
992360SN/A                      process->mmapGrowsDown()))) {
9936701Sgblack@eecs.umich.edu                    // something fishy going on here, at least notify the user
9948852Sandreas.hansson@arm.com                    // @todo: increase mmap_end?
9956701Sgblack@eecs.umich.edu                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
9966701Sgblack@eecs.umich.edu                }
9976701Sgblack@eecs.umich.edu                warn("returning %08p as start\n", new_start);
9986701Sgblack@eecs.umich.edu                return new_start;
999360SN/A            }
10003669Sbinkertn@umich.edu        }
10013669Sbinkertn@umich.edu    } else {
10023669Sbinkertn@umich.edu        if (use_provided_address && provided_address != start)
1003360SN/A            process->pTable->remap(start, new_length, provided_address);
1004360SN/A        process->pTable->unmap(start + new_length, old_length - new_length);
1005360SN/A        return use_provided_address ? provided_address : start;
1006360SN/A    }
10072218SN/A}
1008360SN/A
10098706Sandreas.hansson@arm.com/// Target stat() handler.
1010360SN/Atemplate <class OS>
10111458SN/ASyscallReturn
1012360SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process,
1013360SN/A         ThreadContext *tc)
1014360SN/A{
10155074Ssaidi@eecs.umich.edu    std::string path;
10165074Ssaidi@eecs.umich.edu
10175074Ssaidi@eecs.umich.edu    int index = 0;
101811851Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
10195074Ssaidi@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10205074Ssaidi@eecs.umich.edu        return -EFAULT;
10215074Ssaidi@eecs.umich.edu    }
10225074Ssaidi@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10236701Sgblack@eecs.umich.edu
10248852Sandreas.hansson@arm.com    // Adjust path for current working directory
10256701Sgblack@eecs.umich.edu    path = process->fullPath(path);
10265074Ssaidi@eecs.umich.edu
10276701Sgblack@eecs.umich.edu    struct stat hostBuf;
10285074Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
10295074Ssaidi@eecs.umich.edu
10305074Ssaidi@eecs.umich.edu    if (result < 0)
10315074Ssaidi@eecs.umich.edu        return -errno;
10325208Ssaidi@eecs.umich.edu
10335208Ssaidi@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10345208Ssaidi@eecs.umich.edu
10355208Ssaidi@eecs.umich.edu    return 0;
10365074Ssaidi@eecs.umich.edu}
10375074Ssaidi@eecs.umich.edu
10385208Ssaidi@eecs.umich.edu
10395074Ssaidi@eecs.umich.edu/// Target stat64() handler.
10405074Ssaidi@eecs.umich.edutemplate <class OS>
10415074Ssaidi@eecs.umich.eduSyscallReturn
10425074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, Process *process,
10438706Sandreas.hansson@arm.com           ThreadContext *tc)
10445074Ssaidi@eecs.umich.edu{
10455074Ssaidi@eecs.umich.edu    std::string path;
10465074Ssaidi@eecs.umich.edu
10475074Ssaidi@eecs.umich.edu    int index = 0;
10485074Ssaidi@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
104910027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
105010027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
105110027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
105211851Sbrandon.potter@amd.com
105310027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
105410027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
105510027SChris.Adeniyi-Jones@arm.com
105610027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
105710027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
105810793Sbrandon.potter@amd.com    int result = stat(path.c_str(), &hostBuf);
105910027SChris.Adeniyi-Jones@arm.com#else
106010027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
106110027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
106210027SChris.Adeniyi-Jones@arm.com#endif
106310027SChris.Adeniyi-Jones@arm.com
106410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
106510027SChris.Adeniyi-Jones@arm.com        return -errno;
106610027SChris.Adeniyi-Jones@arm.com
106710027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
106810027SChris.Adeniyi-Jones@arm.com
106910027SChris.Adeniyi-Jones@arm.com    return 0;
107010027SChris.Adeniyi-Jones@arm.com}
107110027SChris.Adeniyi-Jones@arm.com
107210027SChris.Adeniyi-Jones@arm.com
107310027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
107410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
107510027SChris.Adeniyi-Jones@arm.comSyscallReturn
107610027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
107710027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
107810027SChris.Adeniyi-Jones@arm.com{
107910027SChris.Adeniyi-Jones@arm.com    int index = 0;
108010027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
108110027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
108210027SChris.Adeniyi-Jones@arm.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
108310027SChris.Adeniyi-Jones@arm.com
108410027SChris.Adeniyi-Jones@arm.com    std::string path;
108510027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
10861999SN/A                process->getSyscallArg(tc, index)))
10871999SN/A        return -EFAULT;
10881999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
108911856Sbrandon.potter@amd.com
10901999SN/A    // Adjust path for current working directory
10916701Sgblack@eecs.umich.edu    path = process->fullPath(path);
109211856Sbrandon.potter@amd.com
109311856Sbrandon.potter@amd.com#if NO_STAT64
109410931Sbrandon.potter@amd.com    struct stat  hostBuf;
109511856Sbrandon.potter@amd.com    int result = stat(path.c_str(), &hostBuf);
109611856Sbrandon.potter@amd.com#else
10971999SN/A    struct stat64 hostBuf;
109811856Sbrandon.potter@amd.com    int result = stat64(path.c_str(), &hostBuf);
10991999SN/A#endif
11002764Sstever@eecs.umich.edu
11012064SN/A    if (result < 0)
110210931Sbrandon.potter@amd.com        return -errno;
11032064SN/A
11042064SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
110510931Sbrandon.potter@amd.com
11062064SN/A    return 0;
11071999SN/A}
11081999SN/A
11092218SN/A
11101999SN/A/// Target fstat64() handler.
111110931Sbrandon.potter@amd.comtemplate <class OS>
11121999SN/ASyscallReturn
11131999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
11141999SN/A{
11151999SN/A    int index = 0;
11161999SN/A    int tgt_fd = p->getSyscallArg(tc, index);
1117378SN/A    Addr bufPtr = p->getSyscallArg(tc, index);
1118360SN/A
11191450SN/A    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
112011851Sbrandon.potter@amd.com    if (!ffdp)
11212680Sktlim@umich.edu        return -EBADF;
1122360SN/A    int sim_fd = ffdp->getSimFD();
1123360SN/A
1124360SN/A#if NO_STAT64
11256701Sgblack@eecs.umich.edu    struct stat  hostBuf;
11268852Sandreas.hansson@arm.com    int result = fstat(sim_fd, &hostBuf);
11276701Sgblack@eecs.umich.edu#else
11286701Sgblack@eecs.umich.edu    struct stat64  hostBuf;
11296701Sgblack@eecs.umich.edu    int result = fstat64(sim_fd, &hostBuf);
11306701Sgblack@eecs.umich.edu#endif
1131360SN/A
11323669Sbinkertn@umich.edu    if (result < 0)
11333669Sbinkertn@umich.edu        return -errno;
11343669Sbinkertn@umich.edu
1135360SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
1136360SN/A
1137360SN/A    return 0;
1138360SN/A}
11391458SN/A
1140360SN/A
11418706Sandreas.hansson@arm.com/// Target lstat() handler.
1142360SN/Atemplate <class OS>
11431458SN/ASyscallReturn
1144360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process,
1145360SN/A          ThreadContext *tc)
11461999SN/A{
11471999SN/A    std::string path;
11481999SN/A
114911851Sbrandon.potter@amd.com    int index = 0;
11502680Sktlim@umich.edu    if (!tc->getMemProxy().tryReadString(path,
11511999SN/A                process->getSyscallArg(tc, index))) {
11521999SN/A        return -EFAULT;
11531999SN/A    }
11546701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11558852Sandreas.hansson@arm.com
11566701Sgblack@eecs.umich.edu    // Adjust path for current working directory
11576701Sgblack@eecs.umich.edu    path = process->fullPath(path);
11586701Sgblack@eecs.umich.edu
11596701Sgblack@eecs.umich.edu    struct stat hostBuf;
11601999SN/A    int result = lstat(path.c_str(), &hostBuf);
11613669Sbinkertn@umich.edu
11623669Sbinkertn@umich.edu    if (result < 0)
11633669Sbinkertn@umich.edu        return -errno;
11642764Sstever@eecs.umich.edu
11652064SN/A    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11662064SN/A
11672064SN/A    return 0;
11681999SN/A}
11691999SN/A
11702064SN/A/// Target lstat64() handler.
11711999SN/Atemplate <class OS>
11721999SN/ASyscallReturn
11731999SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process,
11741999SN/A            ThreadContext *tc)
11758706Sandreas.hansson@arm.com{
11761999SN/A    std::string path;
11771999SN/A
11781999SN/A    int index = 0;
11791999SN/A    if (!tc->getMemProxy().tryReadString(path,
1180378SN/A                process->getSyscallArg(tc, index))) {
1181360SN/A        return -EFAULT;
11821450SN/A    }
118311856Sbrandon.potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
1184360SN/A
11856701Sgblack@eecs.umich.edu    // Adjust path for current working directory
118611856Sbrandon.potter@amd.com    path = process->fullPath(path);
118711856Sbrandon.potter@amd.com
1188360SN/A#if NO_STAT64
118911380Salexandru.dutu@amd.com    struct stat hostBuf;
1190360SN/A    int result = lstat(path.c_str(), &hostBuf);
119111856Sbrandon.potter@amd.com#else
119211856Sbrandon.potter@amd.com    struct stat64 hostBuf;
11931458SN/A    int result = lstat64(path.c_str(), &hostBuf);
119411856Sbrandon.potter@amd.com#endif
1195360SN/A
1196360SN/A    if (result < 0)
119710931Sbrandon.potter@amd.com        return -errno;
1198360SN/A
1199360SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
12001458SN/A
1201360SN/A    return 0;
120210931Sbrandon.potter@amd.com}
12032021SN/A
12041458SN/A/// Target fstat() handler.
1205360SN/Atemplate <class OS>
1206360SN/ASyscallReturn
1207360SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
12081706SN/A{
12091706SN/A    int index = 0;
12101706SN/A    int tgt_fd = p->getSyscallArg(tc, index);
121111851Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
12122680Sktlim@umich.edu
12131706SN/A    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
121411799Sbrandon.potter@amd.com
121511799Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
121611799Sbrandon.potter@amd.com    if (!ffdp)
12171706SN/A        return -EBADF;
12181706SN/A    int sim_fd = ffdp->getSimFD();
12196701Sgblack@eecs.umich.edu
12208852Sandreas.hansson@arm.com    struct stat hostBuf;
12216701Sgblack@eecs.umich.edu    int result = fstat(sim_fd, &hostBuf);
12226701Sgblack@eecs.umich.edu
12236701Sgblack@eecs.umich.edu    if (result < 0)
12246701Sgblack@eecs.umich.edu        return -errno;
12251706SN/A
12263669Sbinkertn@umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
12273669Sbinkertn@umich.edu
12283669Sbinkertn@umich.edu    return 0;
12291706SN/A}
12301706SN/A
12311706SN/A
12321706SN/A/// Target statfs() handler.
12332218SN/Atemplate <class OS>
12341706SN/ASyscallReturn
123511759Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
123611799Sbrandon.potter@amd.com           ThreadContext *tc)
12371706SN/A{
12381706SN/A#if NO_STATFS
12391706SN/A    warn("Host OS cannot support calls to statfs. Ignoring syscall");
124011886Sbrandon.potter@amd.com#else
124111886Sbrandon.potter@amd.com    std::string path;
124211886Sbrandon.potter@amd.com
124311886Sbrandon.potter@amd.com    int index = 0;
124411886Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
124512426Sqtt2@cornell.edu                process->getSyscallArg(tc, index))) {
124611886Sbrandon.potter@amd.com        return -EFAULT;
124711886Sbrandon.potter@amd.com    }
124811886Sbrandon.potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
124912426Sqtt2@cornell.edu
125012426Sqtt2@cornell.edu    // Adjust path for current working directory
125112426Sqtt2@cornell.edu    path = process->fullPath(path);
125212426Sqtt2@cornell.edu
125312426Sqtt2@cornell.edu    struct statfs hostBuf;
125412426Sqtt2@cornell.edu    int result = statfs(path.c_str(), &hostBuf);
125512426Sqtt2@cornell.edu
125612426Sqtt2@cornell.edu    if (result < 0)
125712426Sqtt2@cornell.edu        return -errno;
125812426Sqtt2@cornell.edu
125911886Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
126011886Sbrandon.potter@amd.com#endif
126112426Sqtt2@cornell.edu    return 0;
126211886Sbrandon.potter@amd.com}
126311886Sbrandon.potter@amd.com
126411886Sbrandon.potter@amd.comtemplate <class OS>
126511886Sbrandon.potter@amd.comSyscallReturn
126611886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
126711886Sbrandon.potter@amd.com{
126811886Sbrandon.potter@amd.com    int index = 0;
126911886Sbrandon.potter@amd.com
127011886Sbrandon.potter@amd.com    TheISA::IntReg flags = p->getSyscallArg(tc, index);
127111886Sbrandon.potter@amd.com    TheISA::IntReg newStack = p->getSyscallArg(tc, index);
127211886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
127311886Sbrandon.potter@amd.com
127411886Sbrandon.potter@amd.com#if THE_ISA == RISCV_ISA
127511886Sbrandon.potter@amd.com    /**
127611886Sbrandon.potter@amd.com     * Linux kernel 4.15 sets CLONE_BACKWARDS flag for RISC-V.
127711886Sbrandon.potter@amd.com     * The flag defines the list of clone() arguments in the following
127811886Sbrandon.potter@amd.com     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
127911886Sbrandon.potter@amd.com     */
128011886Sbrandon.potter@amd.com    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
128111886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
128211886Sbrandon.potter@amd.com#else
128311886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
128411886Sbrandon.potter@amd.com    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
128511886Sbrandon.potter@amd.com#endif
128611886Sbrandon.potter@amd.com
128711886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
128811886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
128911886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
129011886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
129111886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
129211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
129311886Sbrandon.potter@amd.com        return -EINVAL;
129411886Sbrandon.potter@amd.com
129511886Sbrandon.potter@amd.com    ThreadContext *ctc;
129611886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
129711886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
129811886Sbrandon.potter@amd.com
129911886Sbrandon.potter@amd.com    /**
130011886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
130111886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
130211886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
130311886Sbrandon.potter@amd.com     * constructor.
130411886Sbrandon.potter@amd.com     */
130511886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
130611886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
130711886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
130811886Sbrandon.potter@amd.com    pp->system = p->system;
130911886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
131011886Sbrandon.potter@amd.com    pp->input.assign("stdin");
131111886Sbrandon.potter@amd.com    pp->output.assign("stdout");
131211886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
131311886Sbrandon.potter@amd.com    pp->uid = p->uid();
131411886Sbrandon.potter@amd.com    pp->euid = p->euid();
131511886Sbrandon.potter@amd.com    pp->gid = p->gid();
131611886Sbrandon.potter@amd.com    pp->egid = p->egid();
131711886Sbrandon.potter@amd.com
131811886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
131911886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
132011886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
132111886Sbrandon.potter@amd.com    do {
132211886Sbrandon.potter@amd.com        temp_pid++;
132311911SBrandon.Potter@amd.com    } while (pids.find(temp_pid) != pids.end());
132411911SBrandon.Potter@amd.com    if (temp_pid >= System::maxPID)
132511911SBrandon.Potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
132611911SBrandon.Potter@amd.com
132711911SBrandon.Potter@amd.com    pp->pid = temp_pid;
132811911SBrandon.Potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
132911911SBrandon.Potter@amd.com    Process *cp = pp->create();
133011886Sbrandon.potter@amd.com    delete pp;
133111886Sbrandon.potter@amd.com
133211886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
133311886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
133411886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
133511886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
133611886Sbrandon.potter@amd.com
133711886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
133811886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
133911886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
134011886Sbrandon.potter@amd.com        *ptid = cp->pid();
134111886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
134211886Sbrandon.potter@amd.com    }
134311886Sbrandon.potter@amd.com
134411886Sbrandon.potter@amd.com    cp->initState();
134511886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
134611886Sbrandon.potter@amd.com
134711886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
134811886Sbrandon.potter@amd.com        delete cp->sigchld;
134911886Sbrandon.potter@amd.com        cp->sigchld = p->sigchld;
135011886Sbrandon.potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
135111886Sbrandon.potter@amd.com        *cp->sigchld = true;
135211886Sbrandon.potter@amd.com    }
135311886Sbrandon.potter@amd.com
135411886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
135511886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
135612426Sqtt2@cornell.edu        long *ctid = (long *)ctidBuf.bufferPtr();
135711886Sbrandon.potter@amd.com        *ctid = cp->pid();
135811886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
135911886Sbrandon.potter@amd.com    }
136011886Sbrandon.potter@amd.com
136111886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
136211886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
136311886Sbrandon.potter@amd.com
136411886Sbrandon.potter@amd.com    ctc->clearArchRegs();
136511886Sbrandon.potter@amd.com
136611886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
136711886Sbrandon.potter@amd.com    TheISA::copyMiscRegs(tc, ctc);
136811886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
136911886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
137011886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0);
137111886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0);
137211886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2);
137311886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows);
137411886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_CWP, 0);
137511886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0);
137611886Sbrandon.potter@amd.com    ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0);
137711886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY);
137811886Sbrandon.potter@amd.com    for (int y = 8; y < 32; y++)
137911886Sbrandon.potter@amd.com        ctc->setIntReg(y, tc->readIntReg(y));
138011886Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA or THE_ISA == RISCV_ISA
138111886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
138211886Sbrandon.potter@amd.com#endif
138311886Sbrandon.potter@amd.com
13841706SN/A#if THE_ISA == X86_ISA
13851706SN/A    if (flags & OS::TGT_CLONE_SETTLS) {
13861706SN/A        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr);
13871706SN/A        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr);
138811856Sbrandon.potter@amd.com    }
13891706SN/A#endif
13906701Sgblack@eecs.umich.edu
139111856Sbrandon.potter@amd.com    if (newStack)
139211856Sbrandon.potter@amd.com        ctc->setIntReg(TheISA::StackPointerReg, newStack);
13931706SN/A
139411856Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
139511856Sbrandon.potter@amd.com
13961706SN/A#if THE_ISA == ALPHA_ISA
139711856Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
13981706SN/A#elif THE_ISA == SPARC_ISA
13991706SN/A    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
140010931Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
14011706SN/A#endif
14021706SN/A
14032218SN/A    ctc->pcState(tc->nextInstAddr());
14041706SN/A    ctc->activate();
140511759Sbrandon.potter@amd.com
14061706SN/A    return cp->pid();
14071706SN/A}
14081706SN/A
14091706SN/A/// Target fstatfs() handler.
14101706SN/Atemplate <class OS>
14111999SN/ASyscallReturn
14121999SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14131999SN/A{
141411856Sbrandon.potter@amd.com    int index = 0;
14151999SN/A    int tgt_fd = p->getSyscallArg(tc, index);
14166701Sgblack@eecs.umich.edu    Addr bufPtr = p->getSyscallArg(tc, index);
141711856Sbrandon.potter@amd.com
141810931Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
141911856Sbrandon.potter@amd.com    if (!ffdp)
142011856Sbrandon.potter@amd.com        return -EBADF;
14211999SN/A    int sim_fd = ffdp->getSimFD();
142211856Sbrandon.potter@amd.com
14231999SN/A    struct statfs hostBuf;
142411856Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
142511856Sbrandon.potter@amd.com
142611856Sbrandon.potter@amd.com    if (result < 0)
14271999SN/A        return -errno;
14286227Snate@binkert.org
14291999SN/A    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14302461SN/A
143111856Sbrandon.potter@amd.com    return 0;
143211856Sbrandon.potter@amd.com}
14338737Skoansin.tan@gmail.com
14341999SN/A
143511856Sbrandon.potter@amd.com/// Target writev() handler.
143611856Sbrandon.potter@amd.comtemplate <class OS>
14371999SN/ASyscallReturn
14381999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
143910931Sbrandon.potter@amd.com{
14401999SN/A    int index = 0;
14416227Snate@binkert.org    int tgt_fd = p->getSyscallArg(tc, index);
14421999SN/A
14431999SN/A    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
14441999SN/A    if (!hbfdp)
14452218SN/A        return -EBADF;
14461999SN/A    int sim_fd = hbfdp->getSimFD();
144710629Sjthestness@gmail.com
14481999SN/A    SETranslatingPortProxy &prox = tc->getMemProxy();
14491999SN/A    uint64_t tiov_base = p->getSyscallArg(tc, index);
145011385Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
1451360SN/A    struct iovec hiov[count];
14521450SN/A    for (size_t i = 0; i < count; ++i) {
145311851Sbrandon.potter@amd.com        typename OS::tgt_iovec tiov;
145411385Sbrandon.potter@amd.com
1455360SN/A        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
14566701Sgblack@eecs.umich.edu                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
14576701Sgblack@eecs.umich.edu        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
14586701Sgblack@eecs.umich.edu        hiov[i].iov_base = new char [hiov[i].iov_len];
145911383Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
146011383Sbrandon.potter@amd.com                      hiov[i].iov_len);
14618324Ssteve.reinhardt@amd.com    }
146210486Stjablin@gmail.com
1463360SN/A    int result = writev(sim_fd, hiov, count);
146411385Sbrandon.potter@amd.com
146511385Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i)
14669008Sgblack@eecs.umich.edu        delete [] (char *)hiov[i].iov_base;
146711383Sbrandon.potter@amd.com
146811383Sbrandon.potter@amd.com    if (result < 0)
146911383Sbrandon.potter@amd.com        return -errno;
147011383Sbrandon.potter@amd.com
147111383Sbrandon.potter@amd.com    return result;
147211383Sbrandon.potter@amd.com}
147311383Sbrandon.potter@amd.com
147411383Sbrandon.potter@amd.com/// Real mmap handler.
147511383Sbrandon.potter@amd.comtemplate <class OS>
14768324Ssteve.reinhardt@amd.comSyscallReturn
147711383Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
147811383Sbrandon.potter@amd.com         bool is_mmap2)
147911383Sbrandon.potter@amd.com{
148011383Sbrandon.potter@amd.com    int index = 0;
148111383Sbrandon.potter@amd.com    Addr start = p->getSyscallArg(tc, index);
148211383Sbrandon.potter@amd.com    uint64_t length = p->getSyscallArg(tc, index);
148311383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
148411383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
148511383Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
148611383Sbrandon.potter@amd.com    int offset = p->getSyscallArg(tc, index);
148711383Sbrandon.potter@amd.com
148811383Sbrandon.potter@amd.com    if (is_mmap2)
148911383Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
149011383Sbrandon.potter@amd.com
149111383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
149211383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
149311383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
149411383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
149511383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
149611383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
149711383Sbrandon.potter@amd.com        !length) {
149811383Sbrandon.potter@amd.com        return -EINVAL;
149911383Sbrandon.potter@amd.com    }
150011383Sbrandon.potter@amd.com
15018324Ssteve.reinhardt@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
15025877Shsul@eecs.umich.edu        // With shared mmaps, there are two cases to consider:
150310486Stjablin@gmail.com        // 1) anonymous: writes should modify the mapping and this should be
150410486Stjablin@gmail.com        // visible to observers who share the mapping. Currently, it's
150511383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
150611383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
150711383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
150811856Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
150911624Smichael.lebeane@amd.com        // 2) file-backed: writes should modify the mapping and the file
151011856Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
151111856Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
151211856Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
151311856Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
151411624Smichael.lebeane@amd.com        // that resides on the host system. So, any write on a real system
151511624Smichael.lebeane@amd.com        // would cause the change to be propagated to the file mapping at
151611624Smichael.lebeane@amd.com        // some point in the future (the inode is tracked along with the
151711856Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
151811856Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
151911383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
152011856Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
1521360SN/A        // than we currently maintain.
152211913SBrandon.Potter@amd.com        warn("mmap: writing to shared mmap region is currently "
152311383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
15248600Ssteve.reinhardt@amd.com             "will not be propagated to the host or shared mappings");
152511383Sbrandon.potter@amd.com    }
152611383Sbrandon.potter@amd.com
152711383Sbrandon.potter@amd.com    length = roundUp(length, TheISA::PageBytes);
15288600Ssteve.reinhardt@amd.com
15292544SN/A    int sim_fd = -1;
15302544SN/A    uint8_t *pmap = nullptr;
153111383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
153211383Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
153311383Sbrandon.potter@amd.com
153411905SBrandon.Potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
153511905SBrandon.Potter@amd.com        if (dfdp) {
153611905SBrandon.Potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
153711905SBrandon.Potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
153811905SBrandon.Potter@amd.com                                     tgt_flags, tgt_fd, offset);
153911905SBrandon.Potter@amd.com        }
154011905SBrandon.Potter@amd.com
154111383Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
154211383Sbrandon.potter@amd.com        if (!ffdp)
154311383Sbrandon.potter@amd.com            return -EBADF;
154411383Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
154511383Sbrandon.potter@amd.com
154611383Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
154711383Sbrandon.potter@amd.com                                    sim_fd, offset);
154811383Sbrandon.potter@amd.com
154911383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
155011383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
155111383Sbrandon.potter@amd.com            return -errno;
155211383Sbrandon.potter@amd.com        }
155311383Sbrandon.potter@amd.com    }
155411383Sbrandon.potter@amd.com
155511383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
15568600Ssteve.reinhardt@amd.com    // start address unless MAP_FIXED is specified.
15576672Sgblack@eecs.umich.edu    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
15588600Ssteve.reinhardt@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
155911383Sbrandon.potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
156011383Sbrandon.potter@amd.com
156111383Sbrandon.potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
15628601Ssteve.reinhardt@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
15632544SN/A
156411383Sbrandon.potter@amd.com        mem_state->setMmapEnd(mmap_end);
156511383Sbrandon.potter@amd.com    }
156611383Sbrandon.potter@amd.com
156711383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
156811383Sbrandon.potter@amd.com                    start, start + length - 1);
156911383Sbrandon.potter@amd.com
157011383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
157111383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
157211383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
157311383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
157411383Sbrandon.potter@amd.com    if (clobber) {
157511383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
157611383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
157711383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
157811383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
157911383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
158011383Sbrandon.potter@amd.com        }
158111383Sbrandon.potter@amd.com    }
158211383Sbrandon.potter@amd.com
158311383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
158411383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
158511383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
158611383Sbrandon.potter@amd.com    p->allocateMem(start, length, clobber);
158711383Sbrandon.potter@amd.com
158811383Sbrandon.potter@amd.com    // Transfer content into target address space.
158911383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
159011383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
159111383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
159211383Sbrandon.potter@amd.com        // with something like:
159311383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
159411383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
159511383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
159611392Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
159711392Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
159811392Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
159911392Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
160011392Sbrandon.potter@amd.com    } else {
160111392Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
160211392Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
160311392Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
160411392Sbrandon.potter@amd.com        // the host into the target address space.
160511392Sbrandon.potter@amd.com        struct stat file_stat;
160611392Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
160711392Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
160811392Sbrandon.potter@amd.com
160911392Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
161011856Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
161111856Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
161211856Sbrandon.potter@amd.com        // on the specified offset into the file.
161311392Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
161411392Sbrandon.potter@amd.com                                 length);
161511392Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
161611392Sbrandon.potter@amd.com
161711392Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
161811392Sbrandon.potter@amd.com        munmap(pmap, length);
161911392Sbrandon.potter@amd.com
162011392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
162111383Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
162211383Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
162311383Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
162411383Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
162511383Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
16261458SN/A        ObjectFile *interpreter = p->getInterpreter();
1627360SN/A        if (interpreter) {
1628360SN/A            Addr text_start = interpreter->textBase();
162911593Santhony.gutierrez@amd.com            Addr text_end = text_start + interpreter->textSize();
163011593Santhony.gutierrez@amd.com
163111851Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
163211593Santhony.gutierrez@amd.com
163311593Santhony.gutierrez@amd.com            if (pc >= text_start && pc < text_end) {
163411593Santhony.gutierrez@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
163511593Santhony.gutierrez@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
163611593Santhony.gutierrez@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
163711593Santhony.gutierrez@amd.com
163811593Santhony.gutierrez@amd.com                if (lib) {
163911856Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
164011856Sbrandon.potter@amd.com                                        lib->textBase(), start);
164111593Santhony.gutierrez@amd.com                }
164211856Sbrandon.potter@amd.com            }
164311593Santhony.gutierrez@amd.com        }
164411593Santhony.gutierrez@amd.com
164511593Santhony.gutierrez@amd.com        // Note that we do not zero out the remainder of the mapping. This
164611593Santhony.gutierrez@amd.com        // is done by a real system, but it probably will not affect
164711594Santhony.gutierrez@amd.com        // execution (hopefully).
164811593Santhony.gutierrez@amd.com    }
164911593Santhony.gutierrez@amd.com
165011593Santhony.gutierrez@amd.com    return start;
165111593Santhony.gutierrez@amd.com}
165211385Sbrandon.potter@amd.com
165311385Sbrandon.potter@amd.comtemplate <class OS>
165411385Sbrandon.potter@amd.comSyscallReturn
165511851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
165611385Sbrandon.potter@amd.com{
165711385Sbrandon.potter@amd.com    int index = 0;
165811385Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
165911385Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
166011385Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
166111385Sbrandon.potter@amd.com    int offset = p->getSyscallArg(tc, index);
166211385Sbrandon.potter@amd.com
166311851Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
166411385Sbrandon.potter@amd.com    if (!ffdp)
166511385Sbrandon.potter@amd.com        return -EBADF;
166611385Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
166711385Sbrandon.potter@amd.com
1668378SN/A    BufferArg bufArg(bufPtr, nbytes);
1669360SN/A    bufArg.copyIn(tc->getMemProxy());
16701450SN/A
167111851Sbrandon.potter@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
167211851Sbrandon.potter@amd.com
1673360SN/A    return (bytes_written == -1) ? -errno : bytes_written;
16746701Sgblack@eecs.umich.edu}
16756701Sgblack@eecs.umich.edu
16766701Sgblack@eecs.umich.edu/// Target mmap() handler.
1677360SN/Atemplate <class OS>
1678360SN/ASyscallReturn
167911906SBrandon.Potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
168011906SBrandon.Potter@amd.com{
168111906SBrandon.Potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
168211906SBrandon.Potter@amd.com}
168311906SBrandon.Potter@amd.com
168411906SBrandon.Potter@amd.com/// Target mmap2() handler.
1685360SN/Atemplate <class OS>
168611906SBrandon.Potter@amd.comSyscallReturn
168711906SBrandon.Potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
168811906SBrandon.Potter@amd.com{
168911906SBrandon.Potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
169011906SBrandon.Potter@amd.com}
169111906SBrandon.Potter@amd.com
16925877Shsul@eecs.umich.edu/// Target getrlimit() handler.
169311906SBrandon.Potter@amd.comtemplate <class OS>
169411906SBrandon.Potter@amd.comSyscallReturn
169511906SBrandon.Potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
169611906SBrandon.Potter@amd.com              ThreadContext *tc)
1697360SN/A{
1698360SN/A    int index = 0;
16998706Sandreas.hansson@arm.com    unsigned resource = process->getSyscallArg(tc, index);
17001458SN/A    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1701360SN/A
1702360SN/A    switch (resource) {
170312235Sar4jc@virginia.edu      case OS::TGT_RLIMIT_STACK:
170412235Sar4jc@virginia.edu        // max stack size in bytes: make up a number (8MB for now)
170512235Sar4jc@virginia.edu        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
170612235Sar4jc@virginia.edu        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
170712235Sar4jc@virginia.edu        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
170812235Sar4jc@virginia.edu        break;
170912235Sar4jc@virginia.edu
171012235Sar4jc@virginia.edu      case OS::TGT_RLIMIT_DATA:
171112235Sar4jc@virginia.edu        // max data segment size in bytes: make up a number
171212235Sar4jc@virginia.edu        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
171312235Sar4jc@virginia.edu        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
171412235Sar4jc@virginia.edu        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
171512235Sar4jc@virginia.edu        break;
171612235Sar4jc@virginia.edu
171712235Sar4jc@virginia.edu      default:
171812235Sar4jc@virginia.edu        warn("getrlimit: unimplemented resource %d", resource);
171912235Sar4jc@virginia.edu        return -EINVAL;
172012235Sar4jc@virginia.edu        break;
172112416Sqtt2@cornell.edu    }
172212235Sar4jc@virginia.edu
172312235Sar4jc@virginia.edu    rlp.copyOut(tc->getMemProxy());
172412235Sar4jc@virginia.edu    return 0;
172512235Sar4jc@virginia.edu}
172612235Sar4jc@virginia.edu
172712235Sar4jc@virginia.edutemplate <class OS>
172812235Sar4jc@virginia.eduSyscallReturn
172912235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
173012235Sar4jc@virginia.edu            ThreadContext *tc)
173112235Sar4jc@virginia.edu{
173212235Sar4jc@virginia.edu    int index = 0;
173312235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
173412593Sjason@lowepower.com    {
173512235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
173612235Sar4jc@virginia.edu        return -EPERM;
173712235Sar4jc@virginia.edu    }
173812235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
173912235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
174012235Sar4jc@virginia.edu    if (n != 0)
174112235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
174212235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
174312235Sar4jc@virginia.edu    if (o != 0)
174412235Sar4jc@virginia.edu    {
174510796Sbrandon.potter@amd.com        TypedBufferArg<typename OS::rlimit> rlp(o);
174610796Sbrandon.potter@amd.com        switch (resource) {
174710796Sbrandon.potter@amd.com          case OS::TGT_RLIMIT_STACK:
174811851Sbrandon.potter@amd.com            // max stack size in bytes: make up a number (8MB for now)
174910796Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
175010796Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
175110796Sbrandon.potter@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
175210796Sbrandon.potter@amd.com            break;
175310796Sbrandon.potter@amd.com          case OS::TGT_RLIMIT_DATA:
175410796Sbrandon.potter@amd.com            // max data segment size in bytes: make up a number
175510796Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
175610796Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
175710796Sbrandon.potter@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
175810796Sbrandon.potter@amd.com            break;
175910796Sbrandon.potter@amd.com          default:
176010796Sbrandon.potter@amd.com            warn("prlimit: unimplemented resource %d", resource);
176110796Sbrandon.potter@amd.com            return -EINVAL;
176210796Sbrandon.potter@amd.com            break;
176310796Sbrandon.potter@amd.com        }
176411337SMichael.Lebeane@amd.com        rlp.copyOut(tc->getMemProxy());
176511337SMichael.Lebeane@amd.com    }
176611337SMichael.Lebeane@amd.com    return 0;
176711851Sbrandon.potter@amd.com}
176811337SMichael.Lebeane@amd.com
176911337SMichael.Lebeane@amd.com/// Target clock_gettime() function.
177011337SMichael.Lebeane@amd.comtemplate <class OS>
177111337SMichael.Lebeane@amd.comSyscallReturn
177211337SMichael.Lebeane@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
177311337SMichael.Lebeane@amd.com{
177411337SMichael.Lebeane@amd.com    int index = 1;
177511337SMichael.Lebeane@amd.com    //int clk_id = p->getSyscallArg(tc, index);
177611337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
177711337SMichael.Lebeane@amd.com
177811337SMichael.Lebeane@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
177911337SMichael.Lebeane@amd.com    tp->tv_sec += seconds_since_epoch;
178011337SMichael.Lebeane@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
1781378SN/A    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
1782360SN/A
17831450SN/A    tp.copyOut(tc->getMemProxy());
178411851Sbrandon.potter@amd.com
178511851Sbrandon.potter@amd.com    return 0;
1786360SN/A}
17876701Sgblack@eecs.umich.edu
17886701Sgblack@eecs.umich.edu/// Target clock_getres() function.
1789360SN/Atemplate <class OS>
179010796Sbrandon.potter@amd.comSyscallReturn
1791360SN/Aclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
17926109Ssanchezd@stanford.edu{
17936109Ssanchezd@stanford.edu    int index = 1;
1794360SN/A    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
17958706Sandreas.hansson@arm.com
1796360SN/A    // Set resolution at ns, which is what clock_gettime() returns
17971458SN/A    tp->tv_sec = 0;
1798360SN/A    tp->tv_nsec = 1;
1799360SN/A
1800360SN/A    tp.copyOut(tc->getMemProxy());
18011999SN/A
18021999SN/A    return 0;
18031999SN/A}
180411851Sbrandon.potter@amd.com
18052680Sktlim@umich.edu/// Target gettimeofday() handler.
18061999SN/Atemplate <class OS>
18071999SN/ASyscallReturn
18081999SN/AgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
18096701Sgblack@eecs.umich.edu                 ThreadContext *tc)
18108852Sandreas.hansson@arm.com{
18116701Sgblack@eecs.umich.edu    int index = 0;
18126701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
18136701Sgblack@eecs.umich.edu
18141999SN/A    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
18156701Sgblack@eecs.umich.edu    tp->tv_sec += seconds_since_epoch;
18166701Sgblack@eecs.umich.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
18178706Sandreas.hansson@arm.com    tp->tv_usec = TheISA::htog(tp->tv_usec);
18181999SN/A
18191999SN/A    tp.copyOut(tc->getMemProxy());
182011906SBrandon.Potter@amd.com
18218737Skoansin.tan@gmail.com    return 0;
18228737Skoansin.tan@gmail.com}
18231999SN/A
18243669Sbinkertn@umich.edu
18253669Sbinkertn@umich.edu/// Target utimes() handler.
18263669Sbinkertn@umich.edutemplate <class OS>
18273669Sbinkertn@umich.eduSyscallReturn
18281999SN/AutimesFunc(SyscallDesc *desc, int callnum, Process *process,
18291999SN/A           ThreadContext *tc)
18301999SN/A{
18311999SN/A    std::string path;
18321999SN/A
18331999SN/A    int index = 0;
18341999SN/A    if (!tc->getMemProxy().tryReadString(path,
183511886Sbrandon.potter@amd.com                process->getSyscallArg(tc, index))) {
183611886Sbrandon.potter@amd.com        return -EFAULT;
183711886Sbrandon.potter@amd.com    }
183811886Sbrandon.potter@amd.com
183911886Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval [2]>
184011886Sbrandon.potter@amd.com        tp(process->getSyscallArg(tc, index));
184111886Sbrandon.potter@amd.com    tp.copyIn(tc->getMemProxy());
184211886Sbrandon.potter@amd.com
184311886Sbrandon.potter@amd.com    struct timeval hostTimeval[2];
184411886Sbrandon.potter@amd.com    for (int i = 0; i < 2; ++i) {
184511886Sbrandon.potter@amd.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
184611886Sbrandon.potter@amd.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
184711886Sbrandon.potter@amd.com    }
184811886Sbrandon.potter@amd.com
184911886Sbrandon.potter@amd.com    // Adjust path for current working directory
185011886Sbrandon.potter@amd.com    path = process->fullPath(path);
185111886Sbrandon.potter@amd.com
185211886Sbrandon.potter@amd.com    int result = utimes(path.c_str(), hostTimeval);
185311886Sbrandon.potter@amd.com
185411886Sbrandon.potter@amd.com    if (result < 0)
185511886Sbrandon.potter@amd.com        return -errno;
185611886Sbrandon.potter@amd.com
185711886Sbrandon.potter@amd.com    return 0;
185811886Sbrandon.potter@amd.com}
185911886Sbrandon.potter@amd.com
186011886Sbrandon.potter@amd.comtemplate <class OS>
186111886Sbrandon.potter@amd.comSyscallReturn
186211886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
186311886Sbrandon.potter@amd.com{
186411886Sbrandon.potter@amd.com    desc->setFlags(0);
186511886Sbrandon.potter@amd.com
186611886Sbrandon.potter@amd.com    int index = 0;
186711886Sbrandon.potter@amd.com    std::string path;
186811886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
186911886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
187011886Sbrandon.potter@amd.com        return -EFAULT;
187111886Sbrandon.potter@amd.com
187211886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
187311886Sbrandon.potter@amd.com        return -EACCES;
187411886Sbrandon.potter@amd.com
187511886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
187611886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
187711886Sbrandon.potter@amd.com                      Addr mem_loc)
187811886Sbrandon.potter@amd.com    {
187911886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
188011886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
188111886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
188211886Sbrandon.potter@amd.com
188311886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
188411886Sbrandon.potter@amd.com                break;
188511886Sbrandon.potter@amd.com
188611886Sbrandon.potter@amd.com            vect.push_back(std::string());
188711886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
188811886Sbrandon.potter@amd.com        }
188911886Sbrandon.potter@amd.com    };
189011886Sbrandon.potter@amd.com
189111886Sbrandon.potter@amd.com    /**
189211886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
189311886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
189411886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
189511886Sbrandon.potter@amd.com     * constructor.
189611886Sbrandon.potter@amd.com     */
189711886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
189811886Sbrandon.potter@amd.com    pp->executable = path;
189911886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
190011886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
190111886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
190211886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
190311886Sbrandon.potter@amd.com    pp->uid = p->uid();
190411886Sbrandon.potter@amd.com    pp->egid = p->egid();
190511886Sbrandon.potter@amd.com    pp->euid = p->euid();
190611886Sbrandon.potter@amd.com    pp->gid = p->gid();
190711886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
190811886Sbrandon.potter@amd.com    pp->pid = p->pid();
190911886Sbrandon.potter@amd.com    pp->input.assign("cin");
191011886Sbrandon.potter@amd.com    pp->output.assign("cout");
191111886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
191211886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
191311886Sbrandon.potter@amd.com    pp->system = p->system;
191411886Sbrandon.potter@amd.com    /**
191511886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
191611886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
191711886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
191811886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
191911886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
192011886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
192111886Sbrandon.potter@amd.com     */
192211886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
192311886Sbrandon.potter@amd.com    Process *new_p = pp->create();
192411886Sbrandon.potter@amd.com    delete pp;
192511886Sbrandon.potter@amd.com
192611886Sbrandon.potter@amd.com    /**
192711886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
1928378SN/A     * close-on-exec.
1929360SN/A     */
19301450SN/A    new_p->fds = p->fds;
193111851Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
19322680Sktlim@umich.edu        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
1933360SN/A        if (fdep && fdep->getCOE())
19346701Sgblack@eecs.umich.edu            new_p->fds->closeFDEntry(i);
19356701Sgblack@eecs.umich.edu    }
19366701Sgblack@eecs.umich.edu
1937360SN/A    *new_p->sigchld = true;
19383670Sbinkertn@umich.edu
19393670Sbinkertn@umich.edu    delete p;
1940360SN/A    tc->clearArchRegs();
1941360SN/A    tc->setProcessPtr(new_p);
1942360SN/A    new_p->assignThreadContext(tc->contextId());
1943360SN/A    new_p->initState();
1944360SN/A    tc->activate();
1945360SN/A    TheISA::PCState pcState = tc->pcState();
1946360SN/A    tc->setNPC(pcState.instAddr());
1947360SN/A
1948360SN/A    desc->setFlags(SyscallDesc::SuppressReturnValue);
1949360SN/A    return 0;
1950360SN/A}
1951360SN/A
1952360SN/A/// Target getrusage() function.
1953360SN/Atemplate <class OS>
1954360SN/ASyscallReturn
1955360SN/AgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
1956360SN/A              ThreadContext *tc)
19573670Sbinkertn@umich.edu{
19583670Sbinkertn@umich.edu    int index = 0;
195910796Sbrandon.potter@amd.com    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
19608737Skoansin.tan@gmail.com    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
19618737Skoansin.tan@gmail.com
19623670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
19633670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
19643670Sbinkertn@umich.edu    rup->ru_stime.tv_sec = 0;
19653670Sbinkertn@umich.edu    rup->ru_stime.tv_usec = 0;
19663670Sbinkertn@umich.edu    rup->ru_maxrss = 0;
19673670Sbinkertn@umich.edu    rup->ru_ixrss = 0;
19683670Sbinkertn@umich.edu    rup->ru_idrss = 0;
19693670Sbinkertn@umich.edu    rup->ru_isrss = 0;
19703670Sbinkertn@umich.edu    rup->ru_minflt = 0;
19713670Sbinkertn@umich.edu    rup->ru_majflt = 0;
19723670Sbinkertn@umich.edu    rup->ru_nswap = 0;
19733670Sbinkertn@umich.edu    rup->ru_inblock = 0;
19743670Sbinkertn@umich.edu    rup->ru_oublock = 0;
19758706Sandreas.hansson@arm.com    rup->ru_msgsnd = 0;
1976360SN/A    rup->ru_msgrcv = 0;
19771458SN/A    rup->ru_nsignals = 0;
1978360SN/A    rup->ru_nvcsw = 0;
1979360SN/A    rup->ru_nivcsw = 0;
19806683Stjones1@inf.ed.ac.uk
19816683Stjones1@inf.ed.ac.uk    switch (who) {
19826683Stjones1@inf.ed.ac.uk      case OS::TGT_RUSAGE_SELF:
198311851Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
198411851Sbrandon.potter@amd.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
19856683Stjones1@inf.ed.ac.uk        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
19866701Sgblack@eecs.umich.edu        break;
19876701Sgblack@eecs.umich.edu
19886683Stjones1@inf.ed.ac.uk      case OS::TGT_RUSAGE_CHILDREN:
19896683Stjones1@inf.ed.ac.uk        // do nothing.  We have no child processes, so they take no time.
19907823Ssteve.reinhardt@amd.com        break;
19916683Stjones1@inf.ed.ac.uk
19926683Stjones1@inf.ed.ac.uk      default:
19936683Stjones1@inf.ed.ac.uk        // don't really handle THREAD or CHILDREN, but just warn and
19946683Stjones1@inf.ed.ac.uk        // plow ahead
19956683Stjones1@inf.ed.ac.uk        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
19966683Stjones1@inf.ed.ac.uk             who);
19978737Skoansin.tan@gmail.com    }
19986683Stjones1@inf.ed.ac.uk
19996683Stjones1@inf.ed.ac.uk    rup.copyOut(tc->getMemProxy());
20008706Sandreas.hansson@arm.com
20016683Stjones1@inf.ed.ac.uk    return 0;
20026683Stjones1@inf.ed.ac.uk}
20036683Stjones1@inf.ed.ac.uk
20046683Stjones1@inf.ed.ac.uk/// Target times() function.
20052553SN/Atemplate <class OS>
20066684Stjones1@inf.ed.ac.ukSyscallReturn
20076684Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, Process *process,
20086684Stjones1@inf.ed.ac.uk          ThreadContext *tc)
200911851Sbrandon.potter@amd.com{
20106684Stjones1@inf.ed.ac.uk    int index = 0;
20116684Stjones1@inf.ed.ac.uk    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
201210796Sbrandon.potter@amd.com
20136684Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
20146684Stjones1@inf.ed.ac.uk    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
20156701Sgblack@eecs.umich.edu    bufp->tms_utime = clocks;
20166701Sgblack@eecs.umich.edu    bufp->tms_stime = 0;
201711321Ssteve.reinhardt@amd.com    bufp->tms_cutime = 0;
20186684Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
20198737Skoansin.tan@gmail.com
20208852Sandreas.hansson@arm.com    // Convert to host endianness
20218852Sandreas.hansson@arm.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
20226684Stjones1@inf.ed.ac.uk
20236684Stjones1@inf.ed.ac.uk    // Write back
20246684Stjones1@inf.ed.ac.uk    bufp.copyOut(tc->getMemProxy());
20252553SN/A
202611910SBrandon.Potter@amd.com    // Return clock ticks since system boot
202711910SBrandon.Potter@amd.com    return clocks;
202811910SBrandon.Potter@amd.com}
202911910SBrandon.Potter@amd.com
203011910SBrandon.Potter@amd.com/// Target time() function.
203111910SBrandon.Potter@amd.comtemplate <class OS>
203211910SBrandon.Potter@amd.comSyscallReturn
203311910SBrandon.Potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
203411910SBrandon.Potter@amd.com{
203511910SBrandon.Potter@amd.com    typename OS::time_t sec, usec;
203611910SBrandon.Potter@amd.com    getElapsedTimeMicro(sec, usec);
203711910SBrandon.Potter@amd.com    sec += seconds_since_epoch;
203811910SBrandon.Potter@amd.com
203911910SBrandon.Potter@amd.com    int index = 0;
204011910SBrandon.Potter@amd.com    Addr taddr = (Addr)process->getSyscallArg(tc, index);
204111910SBrandon.Potter@amd.com    if (taddr != 0) {
204211910SBrandon.Potter@amd.com        typename OS::time_t t = sec;
204311910SBrandon.Potter@amd.com        t = TheISA::htog(t);
204411910SBrandon.Potter@amd.com        SETranslatingPortProxy &p = tc->getMemProxy();
204511910SBrandon.Potter@amd.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
204611910SBrandon.Potter@amd.com    }
204711910SBrandon.Potter@amd.com    return sec;
204811910SBrandon.Potter@amd.com}
204911910SBrandon.Potter@amd.com
205011910SBrandon.Potter@amd.comtemplate <class OS>
205111910SBrandon.Potter@amd.comSyscallReturn
205211910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
205311910SBrandon.Potter@amd.com{
205411910SBrandon.Potter@amd.com    int index = 0;
205511910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
205611910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
205711910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
205811910SBrandon.Potter@amd.com
205911910SBrandon.Potter@amd.com    /**
206011910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
206111910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
206211910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
206311910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
206411910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
206511910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
206611910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
206711910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
206811910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
206911910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
207011910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
207111910SBrandon.Potter@amd.com     * due to PID rollover.
207211910SBrandon.Potter@amd.com     */
207311910SBrandon.Potter@amd.com
207411910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
20752553SN/A    Process *tgt_proc = nullptr;
20761354SN/A    for (int i = 0; i < sys->numContexts(); i++) {
2077        Process *temp = sys->threadContexts[i]->getProcessPtr();
2078        if (temp->pid() == tid) {
2079            tgt_proc = temp;
2080            break;
2081        }
2082    }
2083
2084    if (sig != 0 || sig != OS::TGT_SIGABRT)
2085        return -EINVAL;
2086
2087    if (tgt_proc == nullptr)
2088        return -ESRCH;
2089
2090    if (tgid != -1 && tgt_proc->tgid() != tgid)
2091        return -ESRCH;
2092
2093    if (sig == OS::TGT_SIGABRT)
2094        exitGroupFunc(desc, 252, process, tc);
2095
2096    return 0;
2097}
2098
2099
2100#endif // __SIM_SYSCALL_EMUL_HH__
2101