syscall_emul.hh revision 13568
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>
8213568Sbrandon.potter@amd.com#include <sys/socket.h>
833113Sgblack@eecs.umich.edu#include <sys/stat.h>
8411799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
8511759Sbrandon.potter@amd.com#include <sys/statfs.h>
8611812Sbaz21@cam.ac.uk#else
8711812Sbaz21@cam.ac.uk#include <sys/mount.h>
8811799Sbrandon.potter@amd.com#endif
898229Snate@binkert.org#include <sys/time.h>
908229Snate@binkert.org#include <sys/uio.h>
9111594Santhony.gutierrez@amd.com#include <unistd.h>
927075Snate@binkert.org
938229Snate@binkert.org#include <cerrno>
9411856Sbrandon.potter@amd.com#include <memory>
957075Snate@binkert.org#include <string>
96360SN/A
9712461Sgabeblack@google.com#include "arch/generic/tlb.hh"
9811886Sbrandon.potter@amd.com#include "arch/utility.hh"
9911800Sbrandon.potter@amd.com#include "base/intmath.hh"
10011392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
10112334Sgabeblack@google.com#include "base/logging.hh"
1021354SN/A#include "base/trace.hh"
1036216Snate@binkert.org#include "base/types.hh"
1046658Snate@binkert.org#include "config/the_isa.hh"
1052474SN/A#include "cpu/base.hh"
1062680Sktlim@umich.edu#include "cpu/thread_context.hh"
1078229Snate@binkert.org#include "mem/page_table.hh"
10811886Sbrandon.potter@amd.com#include "params/Process.hh"
10910496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
11011911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1118229Snate@binkert.org#include "sim/process.hh"
11211794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
11311886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
11410497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
11511794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
116360SN/A
117360SN/A//////////////////////////////////////////////////////////////////////
118360SN/A//
119360SN/A// The following emulation functions are generic enough that they
120360SN/A// don't need to be recompiled for different emulated OS's.  They are
121360SN/A// defined in sim/syscall_emul.cc.
122360SN/A//
123360SN/A//////////////////////////////////////////////////////////////////////
124360SN/A
125360SN/A
126378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1271706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
12811851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
129378SN/A
130378SN/A/// Handler for unimplemented syscalls that we never intend to
131378SN/A/// implement (signal handling, etc.) and should not affect the correct
132378SN/A/// behavior of the program.  Print a warning only if the appropriate
133378SN/A/// trace flag is enabled.  Return success to the target program.
1341706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
13511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
136360SN/A
13711760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
13811760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
13911851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
14011760Sbrandon.potter@amd.com
1416109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1421706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14311851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
144378SN/A
1456109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1466109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
14711851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1486109Ssanchezd@stanford.edu
14911886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
15011886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
15111886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15211886Sbrandon.potter@amd.com
153378SN/A/// Target getpagesize() handler.
1541706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
15511851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
156378SN/A
1575748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1585748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
15911851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
160378SN/A
161378SN/A/// Target close() handler.
1621706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16311851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
164378SN/A
16511886Sbrandon.potter@amd.com// Target read() handler.
1661706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
16711851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
168378SN/A
169378SN/A/// Target write() handler.
1701706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
17111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
172378SN/A
173378SN/A/// Target lseek() handler.
1741706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
17511851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
176378SN/A
1774118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1784118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17911851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1804118Sgblack@eecs.umich.edu
181378SN/A/// Target munmap() handler.
1821706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
18311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
184378SN/A
18513568Sbrandon.potter@amd.com/// Target shutdown() handler.
18613568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num,
18713568Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
18813568Sbrandon.potter@amd.com
189378SN/A/// Target gethostname() handler.
1901706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
19111851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
192360SN/A
1935513SMichael.Adler@intel.com/// Target getcwd() handler.
1945513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
19511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1965513SMichael.Adler@intel.com
19710203SAli.Saidi@ARM.com/// Target readlink() handler.
19810203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19911851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20010203SAli.Saidi@ARM.com                           int index = 0);
2015513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
20211851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2035513SMichael.Adler@intel.com
204511SN/A/// Target unlink() handler.
20510633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20611851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20710633Smichaelupton@gmail.com                           int index);
2081706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
210511SN/A
21112795Smattdsinclair@gmail.com/// Target link() handler
21212795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
21312795Smattdsinclair@gmail.com                       ThreadContext *tc);
21412795Smattdsinclair@gmail.com
21512796Smattdsinclair@gmail.com/// Target symlink() handler.
21612796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
21712796Smattdsinclair@gmail.com                          ThreadContext *tc);
21812796Smattdsinclair@gmail.com
2195513SMichael.Adler@intel.com/// Target mkdir() handler.
2205513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
22111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2225513SMichael.Adler@intel.com
22313031Sbrandon.potter@amd.com/// Target mknod() handler.
22413031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
22513031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22613031Sbrandon.potter@amd.com
22713031Sbrandon.potter@amd.com/// Target chdir() handler.
22813031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
22913031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
23013031Sbrandon.potter@amd.com
23113031Sbrandon.potter@amd.com// Target rmdir() handler.
23213031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
23313031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
23413031Sbrandon.potter@amd.com
235511SN/A/// Target rename() handler.
2361706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
23711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2381706SN/A
2391706SN/A
2401706SN/A/// Target truncate() handler.
2411706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
24211851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2431706SN/A
2441706SN/A
2451706SN/A/// Target ftruncate() handler.
2461706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
24711851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2481706SN/A
249511SN/A
2506703Svince@csl.cornell.edu/// Target truncate64() handler.
2516703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
25211851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2536703Svince@csl.cornell.edu
2546685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2556685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
25611851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2576685Stjones1@inf.ed.ac.uk
2586685Stjones1@inf.ed.ac.uk
2595513SMichael.Adler@intel.com/// Target umask() handler.
2605513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
26111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2625513SMichael.Adler@intel.com
26311885Sbrandon.potter@amd.com/// Target gettid() handler.
26411885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
26511885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2665513SMichael.Adler@intel.com
2671999SN/A/// Target chown() handler.
2681999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2701999SN/A
27111885Sbrandon.potter@amd.com/// Target setpgid() handler.
27211885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
27311885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2741999SN/A
2751999SN/A/// Target fchown() handler.
2761999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
27711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2781999SN/A
2793079Sstever@eecs.umich.edu/// Target dup() handler.
2803079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
28111851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2823079Sstever@eecs.umich.edu
28311908SBrandon.Potter@amd.com/// Target dup2() handler.
28411908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
28511908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
28611908SBrandon.Potter@amd.com
28711875Sbrandon.potter@amd.com/// Target fcntl() handler.
2882093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28911851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2902093SN/A
2912687Sksewell@umich.edu/// Target fcntl64() handler.
2922687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
29311851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2942687Sksewell@umich.edu
2952238SN/A/// Target setuid() handler.
2962238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
29711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2982238SN/A
29911908SBrandon.Potter@amd.com/// Target pipe() handler.
30011908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
30111908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
30211908SBrandon.Potter@amd.com
30311908SBrandon.Potter@amd.com/// Internal pipe() handler.
30411908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
30511908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
30611908SBrandon.Potter@amd.com
3072238SN/A/// Target getpid() handler.
3082238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3102238SN/A
31113568Sbrandon.potter@amd.com// Target bind() handler.
31213568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num,
31313568Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
31413568Sbrandon.potter@amd.com
31513568Sbrandon.potter@amd.com// Target listen() handler.
31613568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num,
31713568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
31813568Sbrandon.potter@amd.com
31913568Sbrandon.potter@amd.com// Target connect() handler.
32013568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num,
32113568Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
32213568Sbrandon.potter@amd.com
32313448Sciro.santilli@arm.com#if defined(SYS_getdents)
32413031Sbrandon.potter@amd.com// Target getdents() handler.
32513031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
32613031Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
32713448Sciro.santilli@arm.com#endif
32813031Sbrandon.potter@amd.com
32913539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
33013539Sjavier.setoain@arm.com// Target getdents() handler.
33113539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
33213539Sjavier.setoain@arm.com                           Process *p, ThreadContext *tc);
33313539Sjavier.setoain@arm.com#endif
33413539Sjavier.setoain@arm.com
33513031Sbrandon.potter@amd.com// Target getuid() handler.
3362238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
33711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3382238SN/A
3392238SN/A/// Target getgid() handler.
3402238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
34111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3422238SN/A
3432238SN/A/// Target getppid() handler.
3442238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
34511851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3462238SN/A
3472238SN/A/// Target geteuid() handler.
3482238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
34911851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3502238SN/A
3512238SN/A/// Target getegid() handler.
3522238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
35311851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3542238SN/A
3559455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3569455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
35711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
35810203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
35911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
36011851Sbrandon.potter@amd.com                         int index);
3619455Smitch.hayenga+gem5@gmail.com
3629112Smarc.orr@gmail.com/// Futex system call
36311906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
36411906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3659112Smarc.orr@gmail.comtemplate <class OS>
3669112Smarc.orr@gmail.comSyscallReturn
36711851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3689112Smarc.orr@gmail.com          ThreadContext *tc)
3699112Smarc.orr@gmail.com{
37011911SBrandon.Potter@amd.com    using namespace std;
3719112Smarc.orr@gmail.com
37211911SBrandon.Potter@amd.com    int index = 0;
37311911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
37411911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
37511911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
3769112Smarc.orr@gmail.com
37711911SBrandon.Potter@amd.com    /*
37811911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
37911911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
38011911SBrandon.Potter@amd.com     */
3819238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3829112Smarc.orr@gmail.com
38311911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
3849112Smarc.orr@gmail.com
38511911SBrandon.Potter@amd.com    if (OS::TGT_FUTEX_WAIT == op) {
38611911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
38711911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
38811911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
38911911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
3909112Smarc.orr@gmail.com
39111911SBrandon.Potter@amd.com        /*
39211911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
39311911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
39411911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
39511911SBrandon.Potter@amd.com         */
39611911SBrandon.Potter@amd.com        if (val != mem_val)
3979112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3989112Smarc.orr@gmail.com
39911911SBrandon.Potter@amd.com        futex_map.suspend(uaddr, process->tgid(), tc);
40011911SBrandon.Potter@amd.com
4019112Smarc.orr@gmail.com        return 0;
40211911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
40311911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
4049112Smarc.orr@gmail.com    }
4059112Smarc.orr@gmail.com
40611911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
40711911SBrandon.Potter@amd.com    return -ENOSYS;
4089112Smarc.orr@gmail.com}
4099112Smarc.orr@gmail.com
4102238SN/A
4112238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4122238SN/A/// returning a second value in a register other than the normal return register
4132238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
41411851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
4152238SN/A
4162238SN/A/// Target getpidPseudo() handler.
4172238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
41811851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4192238SN/A
4202238SN/A/// Target getuidPseudo() handler.
4212238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
42211851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4232238SN/A
4242238SN/A/// Target getgidPseudo() handler.
4252238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
42611851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4272238SN/A
4282238SN/A
4291354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4301354SN/Aconst int one_million = 1000000;
43110796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
43210796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4331354SN/A
4341354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4351354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4361354SN/A/// real-world time) to keep simulations repeatable.
4371354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4381354SN/A
4391354SN/A/// Helper function to convert current elapsed time to seconds and
4401354SN/A/// microseconds.
4411354SN/Atemplate <class T1, class T2>
4421354SN/Avoid
44310796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4441354SN/A{
44510796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4461354SN/A    sec = elapsed_usecs / one_million;
4471354SN/A    usec = elapsed_usecs % one_million;
4481354SN/A}
4491354SN/A
45010796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
45110796Sbrandon.potter@amd.com/// nanoseconds.
45210796Sbrandon.potter@amd.comtemplate <class T1, class T2>
45310796Sbrandon.potter@amd.comvoid
45410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
45510796Sbrandon.potter@amd.com{
45610796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
45710796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
45810796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
45910796Sbrandon.potter@amd.com}
46010796Sbrandon.potter@amd.com
461360SN/A//////////////////////////////////////////////////////////////////////
462360SN/A//
463360SN/A// The following emulation functions are generic, but need to be
464360SN/A// templated to account for differences in types, constants, etc.
465360SN/A//
466360SN/A//////////////////////////////////////////////////////////////////////
467360SN/A
46811759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4693113Sgblack@eecs.umich.edu#if NO_STAT64
4703113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4713113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4723113Sgblack@eecs.umich.edu#else
4733113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4743113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4753113Sgblack@eecs.umich.edu#endif
4763113Sgblack@eecs.umich.edu
4773113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4783113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4793113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4803113Sgblack@eecs.umich.edu
4813113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
48212032Sandreas.sandberg@arm.comvoid
4833113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4843113Sgblack@eecs.umich.edu{
4854189Sgblack@eecs.umich.edu    using namespace TheISA;
4864189Sgblack@eecs.umich.edu
4873113Sgblack@eecs.umich.edu    if (fakeTTY)
4883113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4893113Sgblack@eecs.umich.edu    else
4903113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4918737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4923113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4938737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4943277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4955515SMichael.Adler@intel.com    if (fakeTTY) {
4965515SMichael.Adler@intel.com        // Claim to be a character device
4975515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4985515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4995515SMichael.Adler@intel.com    }
5008737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5013277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5028737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5033277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5048737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5053277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5068737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5073113Sgblack@eecs.umich.edu    if (fakeTTY)
5083113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5093113Sgblack@eecs.umich.edu    else
5103113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5118737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5123113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5138737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5143114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5158737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5163114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5178737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5183114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5198737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
52011906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
5214061Sgblack@eecs.umich.edu    // consistently across different hosts.
5224061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5238737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5243113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5258737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5263113Sgblack@eecs.umich.edu}
5273113Sgblack@eecs.umich.edu
5283113Sgblack@eecs.umich.edu// Same for stat64
5293113Sgblack@eecs.umich.edu
5303113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
53112032Sandreas.sandberg@arm.comvoid
5323113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5333113Sgblack@eecs.umich.edu{
5344189Sgblack@eecs.umich.edu    using namespace TheISA;
5354189Sgblack@eecs.umich.edu
5363113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5373113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5383113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5398737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5403113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5418737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5423113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5438737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5443113Sgblack@eecs.umich.edu#else
5453113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5463113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5473113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5483113Sgblack@eecs.umich.edu#endif
5493113Sgblack@eecs.umich.edu}
5503113Sgblack@eecs.umich.edu
55111906SBrandon.Potter@amd.com// Here are a couple of convenience functions
5523113Sgblack@eecs.umich.edutemplate<class OS>
55312032Sandreas.sandberg@arm.comvoid
5548852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
55511906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
5563113Sgblack@eecs.umich.edu{
5573113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5583113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5593113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5603113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5613113Sgblack@eecs.umich.edu}
5623113Sgblack@eecs.umich.edu
5633113Sgblack@eecs.umich.edutemplate<class OS>
56412032Sandreas.sandberg@arm.comvoid
5658852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
56611906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
5673113Sgblack@eecs.umich.edu{
5683113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5693113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5706686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5713113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5723113Sgblack@eecs.umich.edu}
5733113Sgblack@eecs.umich.edu
57411759Sbrandon.potter@amd.comtemplate <class OS>
57512032Sandreas.sandberg@arm.comvoid
57611759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
57711759Sbrandon.potter@amd.com                 hst_statfs *host)
57811759Sbrandon.potter@amd.com{
57911759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
58011759Sbrandon.potter@amd.com
58111812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
58211812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
58311812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
58411759Sbrandon.potter@amd.com#else
58511812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
58611759Sbrandon.potter@amd.com#endif
58711759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
58811759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
58911759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
59011759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
59111759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
59211759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
59311812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
59411812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
59511812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
59611812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
59711812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
59811812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
59911812Sbaz21@cam.ac.uk#else
60011759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
60111759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
60211812Sbaz21@cam.ac.uk#endif
60311812Sbaz21@cam.ac.uk#if defined(__linux__)
60411759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
60511812Sbaz21@cam.ac.uk#else
60611812Sbaz21@cam.ac.uk    /*
60711812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
60811812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
60911812Sbaz21@cam.ac.uk     */
61011812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
61111812Sbaz21@cam.ac.uk#endif
61211759Sbrandon.potter@amd.com
61311759Sbrandon.potter@amd.com    tgt.copyOut(mem);
61411759Sbrandon.potter@amd.com}
61511759Sbrandon.potter@amd.com
616378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
617378SN/A/// only to find out if their stdout is a tty, to determine whether to
6189141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
6199141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
620360SN/Atemplate <class OS>
6211450SN/ASyscallReturn
62211856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
623360SN/A{
6246701Sgblack@eecs.umich.edu    int index = 0;
62511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
62611856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
627360SN/A
62810930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
629360SN/A
63011856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
63111856Sbrandon.potter@amd.com        return -ENOTTY;
63210496Ssteve.reinhardt@amd.com
63311856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
63411856Sbrandon.potter@amd.com    if (!dfdp)
6351458SN/A        return -EBADF;
636360SN/A
63711856Sbrandon.potter@amd.com    /**
63811856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
63911856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
64011856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
64111856Sbrandon.potter@amd.com     */
64211856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
64311856Sbrandon.potter@amd.com    if (emul_driver)
64411856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
64510496Ssteve.reinhardt@amd.com
64611856Sbrandon.potter@amd.com    /**
64711856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
64811856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
64911856Sbrandon.potter@amd.com     */
65011856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
65110930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
6529141Smarc.orr@gmail.com    return -ENOTTY;
653360SN/A}
654360SN/A
655360SN/Atemplate <class OS>
65611907SBrandon.Potter@amd.comSyscallReturn
65711907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
65811907SBrandon.Potter@amd.com         bool isopenat)
659360SN/A{
66011907SBrandon.Potter@amd.com    int index = 0;
66111907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
66211907SBrandon.Potter@amd.com
66311907SBrandon.Potter@amd.com    /**
66411907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
66511907SBrandon.Potter@amd.com     * descriptor from the simulated process.
66611907SBrandon.Potter@amd.com     */
66711907SBrandon.Potter@amd.com    if (isopenat)
66811907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
66911907SBrandon.Potter@amd.com
67011907SBrandon.Potter@amd.com    /**
67111907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
67211907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
67311907SBrandon.Potter@amd.com     */
674360SN/A    std::string path;
67511907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6761458SN/A        return -EFAULT;
677360SN/A
67811907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
67911907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
68011907SBrandon.Potter@amd.com#else
68111907SBrandon.Potter@amd.com    int host_flags = 0;
68211907SBrandon.Potter@amd.com#endif
68311907SBrandon.Potter@amd.com    /**
68411907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
68511907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
68611907SBrandon.Potter@amd.com     */
68711907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
688360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
68911907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
69011907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
69111907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
692360SN/A        }
693360SN/A    }
69411907SBrandon.Potter@amd.com    if (tgt_flags) {
69511907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
69611907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
69711907SBrandon.Potter@amd.com    }
698360SN/A#ifdef __CYGWIN32__
69911907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
700360SN/A#endif
701360SN/A
70211907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
7033669Sbinkertn@umich.edu
70411907SBrandon.Potter@amd.com    /**
70511907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
70611907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
70711907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
70811907SBrandon.Potter@amd.com     * create a full path.
70911907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
71011907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
71111907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
71211907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
71311907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
71411907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
71511907SBrandon.Potter@amd.com     */
71611907SBrandon.Potter@amd.com    if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) {
71711907SBrandon.Potter@amd.com        path = p->fullPath(path);
71811907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
71911907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
72011907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
72111907SBrandon.Potter@amd.com        if (!ffdp)
72211907SBrandon.Potter@amd.com            return -EBADF;
72313371Sciro.santilli@arm.com        path.insert(0, ffdp->getFileName() + "/");
72411907SBrandon.Potter@amd.com    }
7251706SN/A
72611907SBrandon.Potter@amd.com    /**
72711907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
72811907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
72911907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
73011907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
73111907SBrandon.Potter@amd.com     */
73210496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
73310496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
73411907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
73511907SBrandon.Potter@amd.com        if (drv) {
73611907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
73711907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
73811907SBrandon.Potter@amd.com                            isopenat ? "at" : "", path.c_str());
73911907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
74010496Ssteve.reinhardt@amd.com        }
74111907SBrandon.Potter@amd.com        /**
74211907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
74311907SBrandon.Potter@amd.com         * as /dev/zero
74411907SBrandon.Potter@amd.com         */
74510496Ssteve.reinhardt@amd.com    }
74610496Ssteve.reinhardt@amd.com
74711907SBrandon.Potter@amd.com    /**
74811907SBrandon.Potter@amd.com     * Some special paths and files cannot be called on the host and need
74911907SBrandon.Potter@amd.com     * to be handled as special cases inside the simulator.
75011907SBrandon.Potter@amd.com     * If the full path that was created above does not match any of the
75111907SBrandon.Potter@amd.com     * special cases, pass it through to the open call on the host to let
75211907SBrandon.Potter@amd.com     * the host open the file on our behalf.
75311907SBrandon.Potter@amd.com     * If the host cannot open the file, return the host's error code back
75411907SBrandon.Potter@amd.com     * through the system call to the simulated process.
75511907SBrandon.Potter@amd.com     */
75611907SBrandon.Potter@amd.com    int sim_fd = -1;
75711907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
75811907SBrandon.Potter@amd.com            { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" };
75911907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
76011907SBrandon.Potter@amd.com        if (startswith(path, entry))
76111907SBrandon.Potter@amd.com            sim_fd = OS::openSpecialFile(path, p, tc);
76211907SBrandon.Potter@amd.com    }
76311907SBrandon.Potter@amd.com    if (sim_fd == -1) {
76411907SBrandon.Potter@amd.com        sim_fd = open(path.c_str(), host_flags, mode);
76511907SBrandon.Potter@amd.com    }
76611907SBrandon.Potter@amd.com    if (sim_fd == -1) {
76711907SBrandon.Potter@amd.com        int local = -errno;
76811907SBrandon.Potter@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n",
76911907SBrandon.Potter@amd.com                        isopenat ? "at" : "", path.c_str());
77011907SBrandon.Potter@amd.com        return local;
77111907SBrandon.Potter@amd.com    }
772360SN/A
77311907SBrandon.Potter@amd.com    /**
77411907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
77511907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
77611907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
77711907SBrandon.Potter@amd.com     * file descriptor.
77811907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
77911907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
78011907SBrandon.Potter@amd.com     */
78111907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
78211907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
78311907SBrandon.Potter@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n",
78411907SBrandon.Potter@amd.com                    isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str());
78511907SBrandon.Potter@amd.com    return tgt_fd;
786360SN/A}
787360SN/A
78810027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
78910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
79010027SChris.Adeniyi-Jones@arm.comSyscallReturn
79111851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
79210027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
79310027SChris.Adeniyi-Jones@arm.com{
79411907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
79510027SChris.Adeniyi-Jones@arm.com}
79610027SChris.Adeniyi-Jones@arm.com
79710027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
79810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
79910027SChris.Adeniyi-Jones@arm.comSyscallReturn
80011851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
80111851Sbrandon.potter@amd.com           ThreadContext *tc)
80210027SChris.Adeniyi-Jones@arm.com{
80311907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
80410027SChris.Adeniyi-Jones@arm.com}
80510027SChris.Adeniyi-Jones@arm.com
80610633Smichaelupton@gmail.com/// Target unlinkat() handler.
80710633Smichaelupton@gmail.comtemplate <class OS>
80810633Smichaelupton@gmail.comSyscallReturn
80911851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
81010633Smichaelupton@gmail.com             ThreadContext *tc)
81110633Smichaelupton@gmail.com{
81210633Smichaelupton@gmail.com    int index = 0;
81310633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
81410633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
81510633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
81610633Smichaelupton@gmail.com
81710633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
81810633Smichaelupton@gmail.com}
81910633Smichaelupton@gmail.com
82010203SAli.Saidi@ARM.com/// Target facessat() handler
82110203SAli.Saidi@ARM.comtemplate <class OS>
82210203SAli.Saidi@ARM.comSyscallReturn
82311851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
82411851Sbrandon.potter@amd.com              ThreadContext *tc)
82510203SAli.Saidi@ARM.com{
82610203SAli.Saidi@ARM.com    int index = 0;
82710203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
82810203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
82910203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
83010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
83110203SAli.Saidi@ARM.com}
83210203SAli.Saidi@ARM.com
83310203SAli.Saidi@ARM.com/// Target readlinkat() handler
83410203SAli.Saidi@ARM.comtemplate <class OS>
83510203SAli.Saidi@ARM.comSyscallReturn
83611851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
83711851Sbrandon.potter@amd.com               ThreadContext *tc)
83810203SAli.Saidi@ARM.com{
83910203SAli.Saidi@ARM.com    int index = 0;
84010203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
84110203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
84210203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
84310203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
84410203SAli.Saidi@ARM.com}
84510203SAli.Saidi@ARM.com
84610850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
84710850SGiacomo.Gabrielli@arm.comtemplate <class OS>
84810850SGiacomo.Gabrielli@arm.comSyscallReturn
84911851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
85010850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
85110850SGiacomo.Gabrielli@arm.com{
85210850SGiacomo.Gabrielli@arm.com    int index = 0;
85310850SGiacomo.Gabrielli@arm.com
85410850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
85510850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
85610850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
85710850SGiacomo.Gabrielli@arm.com
85810850SGiacomo.Gabrielli@arm.com    std::string old_name;
85910850SGiacomo.Gabrielli@arm.com
86010850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
86110850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
86210850SGiacomo.Gabrielli@arm.com        return -EFAULT;
86310850SGiacomo.Gabrielli@arm.com
86410850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
86510850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
86610850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
86710850SGiacomo.Gabrielli@arm.com
86810850SGiacomo.Gabrielli@arm.com    std::string new_name;
86910850SGiacomo.Gabrielli@arm.com
87010850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
87110850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
87210850SGiacomo.Gabrielli@arm.com        return -EFAULT;
87310850SGiacomo.Gabrielli@arm.com
87410850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
87510850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
87610850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
87710850SGiacomo.Gabrielli@arm.com
87810850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
87910850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
88010850SGiacomo.Gabrielli@arm.com}
88110850SGiacomo.Gabrielli@arm.com
8826640Svince@csl.cornell.edu/// Target sysinfo() handler.
8836640Svince@csl.cornell.edutemplate <class OS>
8846640Svince@csl.cornell.eduSyscallReturn
88511851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
88611851Sbrandon.potter@amd.com            ThreadContext *tc)
8876640Svince@csl.cornell.edu{
8886640Svince@csl.cornell.edu
8896701Sgblack@eecs.umich.edu    int index = 0;
8906701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
89110793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
8926640Svince@csl.cornell.edu
89311758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
89411758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
89511758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
8966640Svince@csl.cornell.edu
8978706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
8986640Svince@csl.cornell.edu
8996701Sgblack@eecs.umich.edu    return 0;
9006640Svince@csl.cornell.edu}
901360SN/A
9021999SN/A/// Target chmod() handler.
9031999SN/Atemplate <class OS>
9041999SN/ASyscallReturn
90511851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
9062680Sktlim@umich.edu          ThreadContext *tc)
9071999SN/A{
9081999SN/A    std::string path;
9091999SN/A
9106701Sgblack@eecs.umich.edu    int index = 0;
9118852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9126701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9131999SN/A        return -EFAULT;
9146701Sgblack@eecs.umich.edu    }
9151999SN/A
9166701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
9171999SN/A    mode_t hostMode = 0;
9181999SN/A
9191999SN/A    // XXX translate mode flags via OS::something???
9201999SN/A    hostMode = mode;
9211999SN/A
9223669Sbinkertn@umich.edu    // Adjust path for current working directory
9233669Sbinkertn@umich.edu    path = process->fullPath(path);
9243669Sbinkertn@umich.edu
9251999SN/A    // do the chmod
9261999SN/A    int result = chmod(path.c_str(), hostMode);
9271999SN/A    if (result < 0)
9282218SN/A        return -errno;
9291999SN/A
9301999SN/A    return 0;
9311999SN/A}
9321999SN/A
9331999SN/A
9341999SN/A/// Target fchmod() handler.
9351999SN/Atemplate <class OS>
9361999SN/ASyscallReturn
93711856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
9381999SN/A{
9396701Sgblack@eecs.umich.edu    int index = 0;
94011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
94111856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
94210931Sbrandon.potter@amd.com
94311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
94411856Sbrandon.potter@amd.com    if (!ffdp)
9451999SN/A        return -EBADF;
94611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
9471999SN/A
94811856Sbrandon.potter@amd.com    mode_t hostMode = mode;
9491999SN/A
95011856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
9511999SN/A
95211856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
9531999SN/A}
9541999SN/A
9555877Shsul@eecs.umich.edu/// Target mremap() handler.
9565877Shsul@eecs.umich.edutemplate <class OS>
9575877Shsul@eecs.umich.eduSyscallReturn
95811851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
9595877Shsul@eecs.umich.edu{
9606701Sgblack@eecs.umich.edu    int index = 0;
9616701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
9626701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
9636701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
9646701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
96510027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
96610027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
96710027SChris.Adeniyi-Jones@arm.com
96810027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
96910027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
9705877Shsul@eecs.umich.edu
97110318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
97210318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
9735877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
9745877Shsul@eecs.umich.edu        return -EINVAL;
9755877Shsul@eecs.umich.edu    }
9765877Shsul@eecs.umich.edu
97710486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
97810486Stjablin@gmail.com
9795877Shsul@eecs.umich.edu    if (new_length > old_length) {
98011905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
98111905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
98211905SBrandon.Potter@amd.com
98311905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
98410027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
98512206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
98612206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
9875877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
98811905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
98911905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
9905877Shsul@eecs.umich.edu            return start;
9915877Shsul@eecs.umich.edu        } else {
99210027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
9935877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
9945877Shsul@eecs.umich.edu                return -ENOMEM;
9955877Shsul@eecs.umich.edu            } else {
99612206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
99712206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
99812206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
99912206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
100012206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
100112206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
100212206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
100312206Srico.amslinger@informatik.uni-augsburg.de                }
100412206Srico.amslinger@informatik.uni-augsburg.de
100510027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
100610027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
100710027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
100810027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
10095877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
101010027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
101110027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
101210027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
101310027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
101412206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
101512206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
101612206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
101712206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
101810027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
101910027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
102010027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
102110027SChris.Adeniyi-Jones@arm.com                }
102210027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
102310027SChris.Adeniyi-Jones@arm.com                return new_start;
10245877Shsul@eecs.umich.edu            }
10255877Shsul@eecs.umich.edu        }
10265877Shsul@eecs.umich.edu    } else {
102710027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
102810027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
10298601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
103010027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
10315877Shsul@eecs.umich.edu    }
10325877Shsul@eecs.umich.edu}
10331999SN/A
1034378SN/A/// Target stat() handler.
1035360SN/Atemplate <class OS>
10361450SN/ASyscallReturn
103711851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
10382680Sktlim@umich.edu         ThreadContext *tc)
1039360SN/A{
1040360SN/A    std::string path;
1041360SN/A
10426701Sgblack@eecs.umich.edu    int index = 0;
10438852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10446701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10456701Sgblack@eecs.umich.edu        return -EFAULT;
10466701Sgblack@eecs.umich.edu    }
10476701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1048360SN/A
10493669Sbinkertn@umich.edu    // Adjust path for current working directory
10503669Sbinkertn@umich.edu    path = process->fullPath(path);
10513669Sbinkertn@umich.edu
1052360SN/A    struct stat hostBuf;
1053360SN/A    int result = stat(path.c_str(), &hostBuf);
1054360SN/A
1055360SN/A    if (result < 0)
10562218SN/A        return -errno;
1057360SN/A
10588706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1059360SN/A
10601458SN/A    return 0;
1061360SN/A}
1062360SN/A
1063360SN/A
10645074Ssaidi@eecs.umich.edu/// Target stat64() handler.
10655074Ssaidi@eecs.umich.edutemplate <class OS>
10665074Ssaidi@eecs.umich.eduSyscallReturn
106711851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
10685074Ssaidi@eecs.umich.edu           ThreadContext *tc)
10695074Ssaidi@eecs.umich.edu{
10705074Ssaidi@eecs.umich.edu    std::string path;
10715074Ssaidi@eecs.umich.edu
10726701Sgblack@eecs.umich.edu    int index = 0;
10738852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10746701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
10755074Ssaidi@eecs.umich.edu        return -EFAULT;
10766701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10775074Ssaidi@eecs.umich.edu
10785074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
10795074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
10805074Ssaidi@eecs.umich.edu
10815208Ssaidi@eecs.umich.edu#if NO_STAT64
10825208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
10835208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
10845208Ssaidi@eecs.umich.edu#else
10855074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
10865074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
10875208Ssaidi@eecs.umich.edu#endif
10885074Ssaidi@eecs.umich.edu
10895074Ssaidi@eecs.umich.edu    if (result < 0)
10905074Ssaidi@eecs.umich.edu        return -errno;
10915074Ssaidi@eecs.umich.edu
10928706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10935074Ssaidi@eecs.umich.edu
10945074Ssaidi@eecs.umich.edu    return 0;
10955074Ssaidi@eecs.umich.edu}
10965074Ssaidi@eecs.umich.edu
10975074Ssaidi@eecs.umich.edu
109810027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
109910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
110010027SChris.Adeniyi-Jones@arm.comSyscallReturn
110111851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
110210027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
110310027SChris.Adeniyi-Jones@arm.com{
110410027SChris.Adeniyi-Jones@arm.com    int index = 0;
110510027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
110610027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
110710793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
110810027SChris.Adeniyi-Jones@arm.com
110910027SChris.Adeniyi-Jones@arm.com    std::string path;
111010027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
111110027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
111210027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
111310027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
111410027SChris.Adeniyi-Jones@arm.com
111510027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
111610027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
111710027SChris.Adeniyi-Jones@arm.com
111810027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
111910027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
112010027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
112110027SChris.Adeniyi-Jones@arm.com#else
112210027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
112310027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
112410027SChris.Adeniyi-Jones@arm.com#endif
112510027SChris.Adeniyi-Jones@arm.com
112610027SChris.Adeniyi-Jones@arm.com    if (result < 0)
112710027SChris.Adeniyi-Jones@arm.com        return -errno;
112810027SChris.Adeniyi-Jones@arm.com
112910027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
113010027SChris.Adeniyi-Jones@arm.com
113110027SChris.Adeniyi-Jones@arm.com    return 0;
113210027SChris.Adeniyi-Jones@arm.com}
113310027SChris.Adeniyi-Jones@arm.com
113410027SChris.Adeniyi-Jones@arm.com
11351999SN/A/// Target fstat64() handler.
11361999SN/Atemplate <class OS>
11371999SN/ASyscallReturn
113811856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
11391999SN/A{
11406701Sgblack@eecs.umich.edu    int index = 0;
114111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
114211856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
114310931Sbrandon.potter@amd.com
114411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
114511856Sbrandon.potter@amd.com    if (!ffdp)
11461999SN/A        return -EBADF;
114711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
11481999SN/A
11492764Sstever@eecs.umich.edu#if NO_STAT64
11502064SN/A    struct stat  hostBuf;
115110931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
11522064SN/A#else
11532064SN/A    struct stat64  hostBuf;
115410931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
11552064SN/A#endif
11561999SN/A
11571999SN/A    if (result < 0)
11582218SN/A        return -errno;
11591999SN/A
116010931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11611999SN/A
11621999SN/A    return 0;
11631999SN/A}
11641999SN/A
11651999SN/A
1166378SN/A/// Target lstat() handler.
1167360SN/Atemplate <class OS>
11681450SN/ASyscallReturn
116911851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
11702680Sktlim@umich.edu          ThreadContext *tc)
1171360SN/A{
1172360SN/A    std::string path;
1173360SN/A
11746701Sgblack@eecs.umich.edu    int index = 0;
11758852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11766701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11776701Sgblack@eecs.umich.edu        return -EFAULT;
11786701Sgblack@eecs.umich.edu    }
11796701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1180360SN/A
11813669Sbinkertn@umich.edu    // Adjust path for current working directory
11823669Sbinkertn@umich.edu    path = process->fullPath(path);
11833669Sbinkertn@umich.edu
1184360SN/A    struct stat hostBuf;
1185360SN/A    int result = lstat(path.c_str(), &hostBuf);
1186360SN/A
1187360SN/A    if (result < 0)
11881458SN/A        return -errno;
1189360SN/A
11908706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1191360SN/A
11921458SN/A    return 0;
1193360SN/A}
1194360SN/A
11951999SN/A/// Target lstat64() handler.
11961999SN/Atemplate <class OS>
11971999SN/ASyscallReturn
119811851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
11992680Sktlim@umich.edu            ThreadContext *tc)
12001999SN/A{
12011999SN/A    std::string path;
12021999SN/A
12036701Sgblack@eecs.umich.edu    int index = 0;
12048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12056701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12066701Sgblack@eecs.umich.edu        return -EFAULT;
12076701Sgblack@eecs.umich.edu    }
12086701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12091999SN/A
12103669Sbinkertn@umich.edu    // Adjust path for current working directory
12113669Sbinkertn@umich.edu    path = process->fullPath(path);
12123669Sbinkertn@umich.edu
12132764Sstever@eecs.umich.edu#if NO_STAT64
12142064SN/A    struct stat hostBuf;
12152064SN/A    int result = lstat(path.c_str(), &hostBuf);
12162064SN/A#else
12171999SN/A    struct stat64 hostBuf;
12181999SN/A    int result = lstat64(path.c_str(), &hostBuf);
12192064SN/A#endif
12201999SN/A
12211999SN/A    if (result < 0)
12221999SN/A        return -errno;
12231999SN/A
12248706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
12251999SN/A
12261999SN/A    return 0;
12271999SN/A}
12281999SN/A
1229378SN/A/// Target fstat() handler.
1230360SN/Atemplate <class OS>
12311450SN/ASyscallReturn
123211856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1233360SN/A{
12346701Sgblack@eecs.umich.edu    int index = 0;
123511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
123611856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1237360SN/A
123811380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1239360SN/A
124011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
124111856Sbrandon.potter@amd.com    if (!ffdp)
12421458SN/A        return -EBADF;
124311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1244360SN/A
1245360SN/A    struct stat hostBuf;
124610931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1247360SN/A
1248360SN/A    if (result < 0)
12491458SN/A        return -errno;
1250360SN/A
125110931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
12522021SN/A
12531458SN/A    return 0;
1254360SN/A}
1255360SN/A
1256360SN/A
12571706SN/A/// Target statfs() handler.
12581706SN/Atemplate <class OS>
12591706SN/ASyscallReturn
126011851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
12612680Sktlim@umich.edu           ThreadContext *tc)
12621706SN/A{
126311799Sbrandon.potter@amd.com#if NO_STATFS
126411799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
126511799Sbrandon.potter@amd.com#else
12661706SN/A    std::string path;
12671706SN/A
12686701Sgblack@eecs.umich.edu    int index = 0;
12698852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12706701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12716701Sgblack@eecs.umich.edu        return -EFAULT;
12726701Sgblack@eecs.umich.edu    }
12736701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12741706SN/A
12753669Sbinkertn@umich.edu    // Adjust path for current working directory
12763669Sbinkertn@umich.edu    path = process->fullPath(path);
12773669Sbinkertn@umich.edu
12781706SN/A    struct statfs hostBuf;
12791706SN/A    int result = statfs(path.c_str(), &hostBuf);
12801706SN/A
12811706SN/A    if (result < 0)
12822218SN/A        return -errno;
12831706SN/A
128411759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
128511799Sbrandon.potter@amd.com#endif
12861706SN/A    return 0;
12871706SN/A}
12881706SN/A
128911886Sbrandon.potter@amd.comtemplate <class OS>
129011886Sbrandon.potter@amd.comSyscallReturn
129111886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
129211886Sbrandon.potter@amd.com{
129311886Sbrandon.potter@amd.com    int index = 0;
129412426Sqtt2@cornell.edu
129513557Sgabeblack@google.com    RegVal flags = p->getSyscallArg(tc, index);
129613557Sgabeblack@google.com    RegVal newStack = p->getSyscallArg(tc, index);
129711886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
129812426Sqtt2@cornell.edu
129913534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
130012426Sqtt2@cornell.edu    /**
130113534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
130212426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
130312426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
130412426Sqtt2@cornell.edu     */
130513536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
130612426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
130712426Sqtt2@cornell.edu#else
130811886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
130913536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
131012426Sqtt2@cornell.edu#endif
131111886Sbrandon.potter@amd.com
131211886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
131311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
131411886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
131511886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
131611886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
131711886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
131811886Sbrandon.potter@amd.com        return -EINVAL;
131911886Sbrandon.potter@amd.com
132011886Sbrandon.potter@amd.com    ThreadContext *ctc;
132111886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
132211886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
132311886Sbrandon.potter@amd.com
132411886Sbrandon.potter@amd.com    /**
132511886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
132611886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
132711886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
132811886Sbrandon.potter@amd.com     * constructor.
132911886Sbrandon.potter@amd.com     */
133011886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
133111886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
133211886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
133311886Sbrandon.potter@amd.com    pp->system = p->system;
133411886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
133511886Sbrandon.potter@amd.com    pp->input.assign("stdin");
133611886Sbrandon.potter@amd.com    pp->output.assign("stdout");
133711886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
133811886Sbrandon.potter@amd.com    pp->uid = p->uid();
133911886Sbrandon.potter@amd.com    pp->euid = p->euid();
134011886Sbrandon.potter@amd.com    pp->gid = p->gid();
134111886Sbrandon.potter@amd.com    pp->egid = p->egid();
134211886Sbrandon.potter@amd.com
134311886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
134411886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
134511886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
134611886Sbrandon.potter@amd.com    do {
134711886Sbrandon.potter@amd.com        temp_pid++;
134811886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
134911886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
135011886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
135111886Sbrandon.potter@amd.com
135211886Sbrandon.potter@amd.com    pp->pid = temp_pid;
135311886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
135411886Sbrandon.potter@amd.com    Process *cp = pp->create();
135511886Sbrandon.potter@amd.com    delete pp;
135611886Sbrandon.potter@amd.com
135711886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
135811886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
135911886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
136011886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
136111886Sbrandon.potter@amd.com
136211886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
136311886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
136411886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
136511886Sbrandon.potter@amd.com        *ptid = cp->pid();
136611886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
136711886Sbrandon.potter@amd.com    }
136811886Sbrandon.potter@amd.com
136911886Sbrandon.potter@amd.com    cp->initState();
137011886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
137111886Sbrandon.potter@amd.com
137211911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
137311911SBrandon.Potter@amd.com        delete cp->sigchld;
137411911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
137511911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
137611911SBrandon.Potter@amd.com        *cp->sigchld = true;
137711911SBrandon.Potter@amd.com    }
137811911SBrandon.Potter@amd.com
137911886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
138011886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
138111886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
138211886Sbrandon.potter@amd.com        *ctid = cp->pid();
138311886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
138411886Sbrandon.potter@amd.com    }
138511886Sbrandon.potter@amd.com
138611886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
138711886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
138811886Sbrandon.potter@amd.com
138911886Sbrandon.potter@amd.com    ctc->clearArchRegs();
139011886Sbrandon.potter@amd.com
139113536Sandreas.sandberg@arm.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
139211886Sbrandon.potter@amd.com
139311886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
139411886Sbrandon.potter@amd.com
139511886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
139611886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
139711886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
139811886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
139911886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
140011886Sbrandon.potter@amd.com#endif
140111886Sbrandon.potter@amd.com
140213535Sandreas.sandberg@arm.com    TheISA::PCState cpc = tc->pcState();
140313535Sandreas.sandberg@arm.com    cpc.advance();
140413535Sandreas.sandberg@arm.com    ctc->pcState(cpc);
140511886Sbrandon.potter@amd.com    ctc->activate();
140611886Sbrandon.potter@amd.com
140711886Sbrandon.potter@amd.com    return cp->pid();
140811886Sbrandon.potter@amd.com}
14091706SN/A
14101706SN/A/// Target fstatfs() handler.
14111706SN/Atemplate <class OS>
14121706SN/ASyscallReturn
141311856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14141706SN/A{
14156701Sgblack@eecs.umich.edu    int index = 0;
141611856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
141711856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
14181706SN/A
141911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
142011856Sbrandon.potter@amd.com    if (!ffdp)
14211706SN/A        return -EBADF;
142211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
14231706SN/A
14241706SN/A    struct statfs hostBuf;
142510931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
14261706SN/A
14271706SN/A    if (result < 0)
14282218SN/A        return -errno;
14291706SN/A
143011759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14311706SN/A
14321706SN/A    return 0;
14331706SN/A}
14341706SN/A
14351706SN/A
14361999SN/A/// Target writev() handler.
14371999SN/Atemplate <class OS>
14381999SN/ASyscallReturn
143911856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14401999SN/A{
14416701Sgblack@eecs.umich.edu    int index = 0;
144211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
144310931Sbrandon.potter@amd.com
144411856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
144511856Sbrandon.potter@amd.com    if (!hbfdp)
14461999SN/A        return -EBADF;
144711856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
14481999SN/A
144911856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
145011856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
145111856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
14521999SN/A    struct iovec hiov[count];
14536227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
14541999SN/A        typename OS::tgt_iovec tiov;
14552461SN/A
145611856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
145711856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
14588737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
14591999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
146011856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
146111856Sbrandon.potter@amd.com                      hiov[i].iov_len);
14621999SN/A    }
14631999SN/A
146410931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
14651999SN/A
14666227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
14671999SN/A        delete [] (char *)hiov[i].iov_base;
14681999SN/A
14691999SN/A    if (result < 0)
14702218SN/A        return -errno;
14711999SN/A
147210629Sjthestness@gmail.com    return result;
14731999SN/A}
14741999SN/A
147511385Sbrandon.potter@amd.com/// Real mmap handler.
1476360SN/Atemplate <class OS>
14771450SN/ASyscallReturn
147811851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
147911385Sbrandon.potter@amd.com         bool is_mmap2)
1480360SN/A{
14816701Sgblack@eecs.umich.edu    int index = 0;
14826701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
14836701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
148411383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
148511383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
14868324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
148710486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1488360SN/A
148911385Sbrandon.potter@amd.com    if (is_mmap2)
149011385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
14919008Sgblack@eecs.umich.edu
149211383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
149311383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
149411383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
149511383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
149611383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
149711383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
149811383Sbrandon.potter@amd.com        !length) {
149911383Sbrandon.potter@amd.com        return -EINVAL;
150011383Sbrandon.potter@amd.com    }
15018324Ssteve.reinhardt@amd.com
150211383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
150311383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
150411383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
150511383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
150611383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
150711383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
150811383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
150911383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
151011383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
151111383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
151211383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
151311383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
151411383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
151511383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
151611383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
151711383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
151811383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
151911383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
152011383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
152111383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
152211383Sbrandon.potter@amd.com        // than we currently maintain.
152311383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
152411383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
152511383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
15268324Ssteve.reinhardt@amd.com    }
15275877Shsul@eecs.umich.edu
152810486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
152910486Stjablin@gmail.com
153011383Sbrandon.potter@amd.com    int sim_fd = -1;
153111383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
153211383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
153311856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
153411624Smichael.lebeane@amd.com
153511856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
153611856Sbrandon.potter@amd.com        if (dfdp) {
153711856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
153811856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
153911624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
154011624Smichael.lebeane@amd.com        }
154111624Smichael.lebeane@amd.com
154211856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
154311856Sbrandon.potter@amd.com        if (!ffdp)
154411383Sbrandon.potter@amd.com            return -EBADF;
154511856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1546360SN/A
154711913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
154811383Sbrandon.potter@amd.com                                    sim_fd, offset);
15498600Ssteve.reinhardt@amd.com
155011383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
155111383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
155211383Sbrandon.potter@amd.com            return -errno;
15538600Ssteve.reinhardt@amd.com        }
15542544SN/A    }
15552544SN/A
155611383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
155711383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
155811383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
155911905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
156011905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
156111905SBrandon.Potter@amd.com
156211905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
156311905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
156411905SBrandon.Potter@amd.com
156511905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
156611383Sbrandon.potter@amd.com    }
156711383Sbrandon.potter@amd.com
156811383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
156911383Sbrandon.potter@amd.com                    start, start + length - 1);
157011383Sbrandon.potter@amd.com
157111383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
157211383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
157311383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
157411383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
157511383Sbrandon.potter@amd.com    if (clobber) {
157611383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
157711383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
157811383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
157911383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
158011383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
15818600Ssteve.reinhardt@amd.com        }
15826672Sgblack@eecs.umich.edu    }
15838600Ssteve.reinhardt@amd.com
158411383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
158511383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
158611383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
15878601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
15882544SN/A
158911383Sbrandon.potter@amd.com    // Transfer content into target address space.
159011383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
159111383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
159211383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
159311383Sbrandon.potter@amd.com        // with something like:
159411383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
159511383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
159611383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
159711383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
159811383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
159911383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
160011383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
160111383Sbrandon.potter@amd.com    } else {
160211383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
160311383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
160411383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
160511383Sbrandon.potter@amd.com        // the host into the target address space.
160611383Sbrandon.potter@amd.com        struct stat file_stat;
160711383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
160811383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
160911383Sbrandon.potter@amd.com
161011383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
161111383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
161211383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
161311383Sbrandon.potter@amd.com        // on the specified offset into the file.
161411383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
161511383Sbrandon.potter@amd.com                                 length);
161611383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
161711383Sbrandon.potter@amd.com
161811383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
161911383Sbrandon.potter@amd.com        munmap(pmap, length);
162011383Sbrandon.potter@amd.com
162111392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
162211392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
162311392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
162411392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
162511392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
162611392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
162711392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
162811392Sbrandon.potter@amd.com        if (interpreter) {
162911392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
163011392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
163111392Sbrandon.potter@amd.com
163211392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
163311392Sbrandon.potter@amd.com
163411392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
163511856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
163611856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
163711856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
163811392Sbrandon.potter@amd.com
163911392Sbrandon.potter@amd.com                if (lib) {
164011392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
164111392Sbrandon.potter@amd.com                                        lib->textBase(), start);
164211392Sbrandon.potter@amd.com                }
164311392Sbrandon.potter@amd.com            }
164411392Sbrandon.potter@amd.com        }
164511392Sbrandon.potter@amd.com
164611383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
164711383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
164811383Sbrandon.potter@amd.com        // execution (hopefully).
164911383Sbrandon.potter@amd.com    }
165011383Sbrandon.potter@amd.com
16511458SN/A    return start;
1652360SN/A}
1653360SN/A
165411593Santhony.gutierrez@amd.comtemplate <class OS>
165511593Santhony.gutierrez@amd.comSyscallReturn
165611851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
165711593Santhony.gutierrez@amd.com{
165811593Santhony.gutierrez@amd.com    int index = 0;
165911593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
166011593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
166111593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
166211593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
166311593Santhony.gutierrez@amd.com
166411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
166511856Sbrandon.potter@amd.com    if (!ffdp)
166611593Santhony.gutierrez@amd.com        return -EBADF;
166711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
166811593Santhony.gutierrez@amd.com
166911593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
167011593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
167111593Santhony.gutierrez@amd.com
167211594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
167311593Santhony.gutierrez@amd.com
167411593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
167511593Santhony.gutierrez@amd.com}
167611593Santhony.gutierrez@amd.com
167711385Sbrandon.potter@amd.com/// Target mmap() handler.
167811385Sbrandon.potter@amd.comtemplate <class OS>
167911385Sbrandon.potter@amd.comSyscallReturn
168011851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
168111385Sbrandon.potter@amd.com{
168211385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
168311385Sbrandon.potter@amd.com}
168411385Sbrandon.potter@amd.com
168511385Sbrandon.potter@amd.com/// Target mmap2() handler.
168611385Sbrandon.potter@amd.comtemplate <class OS>
168711385Sbrandon.potter@amd.comSyscallReturn
168811851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
168911385Sbrandon.potter@amd.com{
169011385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
169111385Sbrandon.potter@amd.com}
169211385Sbrandon.potter@amd.com
1693378SN/A/// Target getrlimit() handler.
1694360SN/Atemplate <class OS>
16951450SN/ASyscallReturn
169611851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
169711851Sbrandon.potter@amd.com              ThreadContext *tc)
1698360SN/A{
16996701Sgblack@eecs.umich.edu    int index = 0;
17006701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
17016701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1702360SN/A
1703360SN/A    switch (resource) {
170411906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
170511906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
170611906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
170711906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
170811906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
170911906SBrandon.Potter@amd.com        break;
1710360SN/A
171111906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
171211906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
171311906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
171411906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
171511906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
171611906SBrandon.Potter@amd.com        break;
17175877Shsul@eecs.umich.edu
171811906SBrandon.Potter@amd.com      default:
171911906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
172011906SBrandon.Potter@amd.com        return -EINVAL;
172111906SBrandon.Potter@amd.com        break;
1722360SN/A    }
1723360SN/A
17248706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
17251458SN/A    return 0;
1726360SN/A}
1727360SN/A
172812235Sar4jc@virginia.edutemplate <class OS>
172912235Sar4jc@virginia.eduSyscallReturn
173012235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
173112235Sar4jc@virginia.edu            ThreadContext *tc)
173212235Sar4jc@virginia.edu{
173312235Sar4jc@virginia.edu    int index = 0;
173412235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
173512235Sar4jc@virginia.edu    {
173612235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
173712235Sar4jc@virginia.edu        return -EPERM;
173812235Sar4jc@virginia.edu    }
173912235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
174012235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
174112235Sar4jc@virginia.edu    if (n != 0)
174212235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
174312235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
174412235Sar4jc@virginia.edu    if (o != 0)
174512235Sar4jc@virginia.edu    {
174612416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
174712235Sar4jc@virginia.edu        switch (resource) {
174812235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
174912235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
175012235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
175112235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
175212235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
175312235Sar4jc@virginia.edu            break;
175412235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
175512235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
175612235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
175712235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
175812235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
175912593Sjason@lowepower.com            break;
176012235Sar4jc@virginia.edu          default:
176112235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
176212235Sar4jc@virginia.edu            return -EINVAL;
176312235Sar4jc@virginia.edu            break;
176412235Sar4jc@virginia.edu        }
176512235Sar4jc@virginia.edu        rlp.copyOut(tc->getMemProxy());
176612235Sar4jc@virginia.edu    }
176712235Sar4jc@virginia.edu    return 0;
176812235Sar4jc@virginia.edu}
176912235Sar4jc@virginia.edu
177010796Sbrandon.potter@amd.com/// Target clock_gettime() function.
177110796Sbrandon.potter@amd.comtemplate <class OS>
177210796Sbrandon.potter@amd.comSyscallReturn
177311851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
177410796Sbrandon.potter@amd.com{
177510796Sbrandon.potter@amd.com    int index = 1;
177610796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
177710796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
177810796Sbrandon.potter@amd.com
177910796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
178010796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
178110796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
178210796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
178310796Sbrandon.potter@amd.com
178410796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
178510796Sbrandon.potter@amd.com
178610796Sbrandon.potter@amd.com    return 0;
178710796Sbrandon.potter@amd.com}
178810796Sbrandon.potter@amd.com
178911337SMichael.Lebeane@amd.com/// Target clock_getres() function.
179011337SMichael.Lebeane@amd.comtemplate <class OS>
179111337SMichael.Lebeane@amd.comSyscallReturn
179211851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
179311337SMichael.Lebeane@amd.com{
179411337SMichael.Lebeane@amd.com    int index = 1;
179511337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
179611337SMichael.Lebeane@amd.com
179711337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
179811337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
179911337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
180011337SMichael.Lebeane@amd.com
180111337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
180211337SMichael.Lebeane@amd.com
180311337SMichael.Lebeane@amd.com    return 0;
180411337SMichael.Lebeane@amd.com}
180511337SMichael.Lebeane@amd.com
1806378SN/A/// Target gettimeofday() handler.
1807360SN/Atemplate <class OS>
18081450SN/ASyscallReturn
180911851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
181011851Sbrandon.potter@amd.com                 ThreadContext *tc)
1811360SN/A{
18126701Sgblack@eecs.umich.edu    int index = 0;
18136701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1814360SN/A
181510796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1816360SN/A    tp->tv_sec += seconds_since_epoch;
18176109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
18186109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1819360SN/A
18208706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1821360SN/A
18221458SN/A    return 0;
1823360SN/A}
1824360SN/A
1825360SN/A
18261999SN/A/// Target utimes() handler.
18271999SN/Atemplate <class OS>
18281999SN/ASyscallReturn
182911851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
18302680Sktlim@umich.edu           ThreadContext *tc)
18311999SN/A{
18321999SN/A    std::string path;
18331999SN/A
18346701Sgblack@eecs.umich.edu    int index = 0;
18358852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
18366701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
18376701Sgblack@eecs.umich.edu        return -EFAULT;
18386701Sgblack@eecs.umich.edu    }
18391999SN/A
18406701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
18416701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
18428706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
18431999SN/A
18441999SN/A    struct timeval hostTimeval[2];
184511906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
18468737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
18478737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
18481999SN/A    }
18493669Sbinkertn@umich.edu
18503669Sbinkertn@umich.edu    // Adjust path for current working directory
18513669Sbinkertn@umich.edu    path = process->fullPath(path);
18523669Sbinkertn@umich.edu
18531999SN/A    int result = utimes(path.c_str(), hostTimeval);
18541999SN/A
18551999SN/A    if (result < 0)
18561999SN/A        return -errno;
18571999SN/A
18581999SN/A    return 0;
18591999SN/A}
186011886Sbrandon.potter@amd.com
186111886Sbrandon.potter@amd.comtemplate <class OS>
186211886Sbrandon.potter@amd.comSyscallReturn
186311886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
186411886Sbrandon.potter@amd.com{
186511886Sbrandon.potter@amd.com    desc->setFlags(0);
186611886Sbrandon.potter@amd.com
186711886Sbrandon.potter@amd.com    int index = 0;
186811886Sbrandon.potter@amd.com    std::string path;
186911886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
187011886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
187111886Sbrandon.potter@amd.com        return -EFAULT;
187211886Sbrandon.potter@amd.com
187311886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
187411886Sbrandon.potter@amd.com        return -EACCES;
187511886Sbrandon.potter@amd.com
187611886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
187711886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
187811886Sbrandon.potter@amd.com                      Addr mem_loc)
187911886Sbrandon.potter@amd.com    {
188011886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
188111886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
188211886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
188311886Sbrandon.potter@amd.com
188411886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
188511886Sbrandon.potter@amd.com                break;
188611886Sbrandon.potter@amd.com
188711886Sbrandon.potter@amd.com            vect.push_back(std::string());
188811886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
188911886Sbrandon.potter@amd.com        }
189011886Sbrandon.potter@amd.com    };
189111886Sbrandon.potter@amd.com
189211886Sbrandon.potter@amd.com    /**
189311886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
189411886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
189511886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
189611886Sbrandon.potter@amd.com     * constructor.
189711886Sbrandon.potter@amd.com     */
189811886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
189911886Sbrandon.potter@amd.com    pp->executable = path;
190011886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
190111886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
190211886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
190311886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
190411886Sbrandon.potter@amd.com    pp->uid = p->uid();
190511886Sbrandon.potter@amd.com    pp->egid = p->egid();
190611886Sbrandon.potter@amd.com    pp->euid = p->euid();
190711886Sbrandon.potter@amd.com    pp->gid = p->gid();
190811886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
190911886Sbrandon.potter@amd.com    pp->pid = p->pid();
191011886Sbrandon.potter@amd.com    pp->input.assign("cin");
191111886Sbrandon.potter@amd.com    pp->output.assign("cout");
191211886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
191311886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
191411886Sbrandon.potter@amd.com    pp->system = p->system;
191511886Sbrandon.potter@amd.com    /**
191611886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
191711886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
191811886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
191911886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
192011886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
192111886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
192211886Sbrandon.potter@amd.com     */
192311886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
192411886Sbrandon.potter@amd.com    Process *new_p = pp->create();
192511886Sbrandon.potter@amd.com    delete pp;
192611886Sbrandon.potter@amd.com
192711886Sbrandon.potter@amd.com    /**
192811886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
192911886Sbrandon.potter@amd.com     * close-on-exec.
193011886Sbrandon.potter@amd.com     */
193111886Sbrandon.potter@amd.com    new_p->fds = p->fds;
193211886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
193311886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
193411886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
193511886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
193611886Sbrandon.potter@amd.com    }
193711886Sbrandon.potter@amd.com
193811886Sbrandon.potter@amd.com    *new_p->sigchld = true;
193911886Sbrandon.potter@amd.com
194011886Sbrandon.potter@amd.com    delete p;
194111886Sbrandon.potter@amd.com    tc->clearArchRegs();
194211886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
194311886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
194411886Sbrandon.potter@amd.com    new_p->initState();
194511886Sbrandon.potter@amd.com    tc->activate();
194611886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
194711886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
194811886Sbrandon.potter@amd.com
194911886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
195011886Sbrandon.potter@amd.com    return 0;
195111886Sbrandon.potter@amd.com}
195211886Sbrandon.potter@amd.com
1953378SN/A/// Target getrusage() function.
1954360SN/Atemplate <class OS>
19551450SN/ASyscallReturn
195611851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
19572680Sktlim@umich.edu              ThreadContext *tc)
1958360SN/A{
19596701Sgblack@eecs.umich.edu    int index = 0;
19606701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
19616701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1962360SN/A
19633670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
19643670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1965360SN/A    rup->ru_stime.tv_sec = 0;
1966360SN/A    rup->ru_stime.tv_usec = 0;
1967360SN/A    rup->ru_maxrss = 0;
1968360SN/A    rup->ru_ixrss = 0;
1969360SN/A    rup->ru_idrss = 0;
1970360SN/A    rup->ru_isrss = 0;
1971360SN/A    rup->ru_minflt = 0;
1972360SN/A    rup->ru_majflt = 0;
1973360SN/A    rup->ru_nswap = 0;
1974360SN/A    rup->ru_inblock = 0;
1975360SN/A    rup->ru_oublock = 0;
1976360SN/A    rup->ru_msgsnd = 0;
1977360SN/A    rup->ru_msgrcv = 0;
1978360SN/A    rup->ru_nsignals = 0;
1979360SN/A    rup->ru_nvcsw = 0;
1980360SN/A    rup->ru_nivcsw = 0;
1981360SN/A
19823670Sbinkertn@umich.edu    switch (who) {
19833670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
198410796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
19858737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
19868737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
19873670Sbinkertn@umich.edu        break;
19883670Sbinkertn@umich.edu
19893670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
19903670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
19913670Sbinkertn@umich.edu        break;
19923670Sbinkertn@umich.edu
19933670Sbinkertn@umich.edu      default:
19943670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
19953670Sbinkertn@umich.edu        // plow ahead
19963670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
19973670Sbinkertn@umich.edu             who);
19983670Sbinkertn@umich.edu    }
19993670Sbinkertn@umich.edu
20008706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
2001360SN/A
20021458SN/A    return 0;
2003360SN/A}
2004360SN/A
20056683Stjones1@inf.ed.ac.uk/// Target times() function.
20066683Stjones1@inf.ed.ac.uktemplate <class OS>
20076683Stjones1@inf.ed.ac.ukSyscallReturn
200811851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
200911851Sbrandon.potter@amd.com          ThreadContext *tc)
20106683Stjones1@inf.ed.ac.uk{
20116701Sgblack@eecs.umich.edu    int index = 0;
20126701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
20136683Stjones1@inf.ed.ac.uk
20146683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
20157823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
20166683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
20176683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
20186683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
20196683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
20206683Stjones1@inf.ed.ac.uk
20216683Stjones1@inf.ed.ac.uk    // Convert to host endianness
20228737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
20236683Stjones1@inf.ed.ac.uk
20246683Stjones1@inf.ed.ac.uk    // Write back
20258706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
20266683Stjones1@inf.ed.ac.uk
20276683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
20286683Stjones1@inf.ed.ac.uk    return clocks;
20296683Stjones1@inf.ed.ac.uk}
20302553SN/A
20316684Stjones1@inf.ed.ac.uk/// Target time() function.
20326684Stjones1@inf.ed.ac.uktemplate <class OS>
20336684Stjones1@inf.ed.ac.ukSyscallReturn
203411851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
20356684Stjones1@inf.ed.ac.uk{
20366684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
203710796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
20386684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
20396684Stjones1@inf.ed.ac.uk
20406701Sgblack@eecs.umich.edu    int index = 0;
20416701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
204211321Ssteve.reinhardt@amd.com    if (taddr != 0) {
20436684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
20448737Skoansin.tan@gmail.com        t = TheISA::htog(t);
20458852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
20468852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
20476684Stjones1@inf.ed.ac.uk    }
20486684Stjones1@inf.ed.ac.uk    return sec;
20496684Stjones1@inf.ed.ac.uk}
20502553SN/A
205111910SBrandon.Potter@amd.comtemplate <class OS>
205211910SBrandon.Potter@amd.comSyscallReturn
205311910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
205411910SBrandon.Potter@amd.com{
205511910SBrandon.Potter@amd.com    int index = 0;
205611910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
205711910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
205811910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
205911910SBrandon.Potter@amd.com
206011910SBrandon.Potter@amd.com    /**
206111910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
206211910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
206311910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
206411910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
206511910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
206611910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
206711910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
206811910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
206911910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
207011910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
207111910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
207211910SBrandon.Potter@amd.com     * due to PID rollover.
207311910SBrandon.Potter@amd.com     */
207411910SBrandon.Potter@amd.com
207511910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
207611910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
207711910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
207811910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
207911910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
208011910SBrandon.Potter@amd.com            tgt_proc = temp;
208111910SBrandon.Potter@amd.com            break;
208211910SBrandon.Potter@amd.com        }
208311910SBrandon.Potter@amd.com    }
208411910SBrandon.Potter@amd.com
208511910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
208611910SBrandon.Potter@amd.com        return -EINVAL;
208711910SBrandon.Potter@amd.com
208811910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
208911910SBrandon.Potter@amd.com        return -ESRCH;
209011910SBrandon.Potter@amd.com
209111910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
209211910SBrandon.Potter@amd.com        return -ESRCH;
209311910SBrandon.Potter@amd.com
209411910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
209511910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
209611910SBrandon.Potter@amd.com
209711910SBrandon.Potter@amd.com    return 0;
209811910SBrandon.Potter@amd.com}
209911910SBrandon.Potter@amd.com
210013568Sbrandon.potter@amd.comtemplate <class OS>
210113568Sbrandon.potter@amd.comSyscallReturn
210213568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
210313568Sbrandon.potter@amd.com{
210413568Sbrandon.potter@amd.com    int index = 0;
210513568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
210613568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
210713568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
210813568Sbrandon.potter@amd.com
210913568Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
211013568Sbrandon.potter@amd.com    if (sim_fd == -1)
211113568Sbrandon.potter@amd.com        return -errno;
211213568Sbrandon.potter@amd.com
211313568Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
211413568Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
211513568Sbrandon.potter@amd.com
211613568Sbrandon.potter@amd.com    return tgt_fd;
211713568Sbrandon.potter@amd.com}
211813568Sbrandon.potter@amd.com
211913568Sbrandon.potter@amd.comtemplate <class OS>
212013568Sbrandon.potter@amd.comSyscallReturn
212113568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
212213568Sbrandon.potter@amd.com{
212313568Sbrandon.potter@amd.com    int index = 0;
212413568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
212513568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
212613568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
212713568Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
212813568Sbrandon.potter@amd.com
212913568Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
213013568Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
213113568Sbrandon.potter@amd.com    if (status == -1)
213213568Sbrandon.potter@amd.com        return -errno;
213313568Sbrandon.potter@amd.com
213413568Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
213513568Sbrandon.potter@amd.com
213613568Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
213713568Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
213813568Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
213913568Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
214013568Sbrandon.potter@amd.com    svBuf.copyOut(tc->getMemProxy());
214113568Sbrandon.potter@amd.com
214213568Sbrandon.potter@amd.com    return status;
214313568Sbrandon.potter@amd.com}
21442553SN/A
21451354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2146