syscall_emul.hh revision 13936
1360SN/A/*
210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited
310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc.
410027SChris.Adeniyi-Jones@arm.com * All rights reserved
510027SChris.Adeniyi-Jones@arm.com *
610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1410027SChris.Adeniyi-Jones@arm.com *
151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
16360SN/A * All rights reserved.
17360SN/A *
18360SN/A * Redistribution and use in source and binary forms, with or without
19360SN/A * modification, are permitted provided that the following conditions are
20360SN/A * met: redistributions of source code must retain the above copyright
21360SN/A * notice, this list of conditions and the following disclaimer;
22360SN/A * redistributions in binary form must reproduce the above copyright
23360SN/A * notice, this list of conditions and the following disclaimer in the
24360SN/A * documentation and/or other materials provided with the distribution;
25360SN/A * neither the name of the copyright holders nor the names of its
26360SN/A * contributors may be used to endorse or promote products derived from
27360SN/A * this software without specific prior written permission.
28360SN/A *
29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
422665Ssaidi@eecs.umich.edu *          Kevin Lim
43360SN/A */
44360SN/A
451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
461354SN/A#define __SIM_SYSCALL_EMUL_HH__
47360SN/A
4812018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
4912018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__CYGWIN__) ||     \
5012018Sandreas.sandberg@arm.com     defined(__NetBSD__))
5112018Sandreas.sandberg@arm.com#define NO_STAT64 1
5212018Sandreas.sandberg@arm.com#else
5312018Sandreas.sandberg@arm.com#define NO_STAT64 0
5412018Sandreas.sandberg@arm.com#endif
552064SN/A
56360SN/A///
57360SN/A/// @file syscall_emul.hh
58360SN/A///
59360SN/A/// This file defines objects used to emulate syscalls from the target
60360SN/A/// application on the host machine.
61360SN/A
6213936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
6313933Sbrandon.potter@amd.com#include <sys/eventfd.h>
6413933Sbrandon.potter@amd.com#include <sys/statfs.h>
6513933Sbrandon.potter@amd.com
6613936SAndrea.Mondelli@ucf.edu#else
6713936SAndrea.Mondelli@ucf.edu#include <sys/mount.h>
6813936SAndrea.Mondelli@ucf.edu
6913933Sbrandon.potter@amd.com#endif
7013933Sbrandon.potter@amd.com
711809SN/A#ifdef __CYGWIN32__
7211800Sbrandon.potter@amd.com#include <sys/fcntl.h>
7311392Sbrandon.potter@amd.com
741809SN/A#endif
7511392Sbrandon.potter@amd.com#include <fcntl.h>
7613902Sbrandon.potter@amd.com#include <net/if.h>
7713570Sbrandon.potter@amd.com#include <poll.h>
7813902Sbrandon.potter@amd.com#include <sys/ioctl.h>
7911383Sbrandon.potter@amd.com#include <sys/mman.h>
8013568Sbrandon.potter@amd.com#include <sys/socket.h>
813113Sgblack@eecs.umich.edu#include <sys/stat.h>
828229Snate@binkert.org#include <sys/time.h>
8313570Sbrandon.potter@amd.com#include <sys/types.h>
848229Snate@binkert.org#include <sys/uio.h>
8511594Santhony.gutierrez@amd.com#include <unistd.h>
867075Snate@binkert.org
878229Snate@binkert.org#include <cerrno>
8811856Sbrandon.potter@amd.com#include <memory>
897075Snate@binkert.org#include <string>
90360SN/A
9112461Sgabeblack@google.com#include "arch/generic/tlb.hh"
9211886Sbrandon.potter@amd.com#include "arch/utility.hh"
9311800Sbrandon.potter@amd.com#include "base/intmath.hh"
9411392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
9512334Sgabeblack@google.com#include "base/logging.hh"
961354SN/A#include "base/trace.hh"
976216Snate@binkert.org#include "base/types.hh"
986658Snate@binkert.org#include "config/the_isa.hh"
992474SN/A#include "cpu/base.hh"
1002680Sktlim@umich.edu#include "cpu/thread_context.hh"
1018229Snate@binkert.org#include "mem/page_table.hh"
10211886Sbrandon.potter@amd.com#include "params/Process.hh"
10310496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
10411911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1058229Snate@binkert.org#include "sim/process.hh"
10611794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
10711886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
10810497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
10911794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
110360SN/A
11113629SAndrea.Mondelli@ucf.edu#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN)
11213629SAndrea.Mondelli@ucf.edu#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
11313629SAndrea.Mondelli@ucf.edu#endif
11413629SAndrea.Mondelli@ucf.edu
115360SN/A//////////////////////////////////////////////////////////////////////
116360SN/A//
117360SN/A// The following emulation functions are generic enough that they
118360SN/A// don't need to be recompiled for different emulated OS's.  They are
119360SN/A// defined in sim/syscall_emul.cc.
120360SN/A//
121360SN/A//////////////////////////////////////////////////////////////////////
122360SN/A
12313933Sbrandon.potter@amd.comvoid warnUnsupportedOS(std::string syscall_name);
124360SN/A
125378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1261706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
12711851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
128378SN/A
129378SN/A/// Handler for unimplemented syscalls that we never intend to
130378SN/A/// implement (signal handling, etc.) and should not affect the correct
131378SN/A/// behavior of the program.  Print a warning only if the appropriate
132378SN/A/// trace flag is enabled.  Return success to the target program.
1331706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
13411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
135360SN/A
13611760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
13711760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
13811851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
13911760Sbrandon.potter@amd.com
1406109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1411706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14211851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
143378SN/A
1446109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1456109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
14611851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1476109Ssanchezd@stanford.edu
14811886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
14911886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
15011886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15111886Sbrandon.potter@amd.com
152378SN/A/// Target getpagesize() handler.
1531706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
15411851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
155378SN/A
1565748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1575748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
15811851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
159378SN/A
160378SN/A/// Target close() handler.
1611706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16211851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
163378SN/A
164378SN/A/// Target lseek() handler.
1651706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
16611851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
167378SN/A
1684118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1694118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17011851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1714118Sgblack@eecs.umich.edu
172378SN/A/// Target munmap() handler.
1731706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
17411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
175378SN/A
17613568Sbrandon.potter@amd.com/// Target shutdown() handler.
17713568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num,
17813568Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
17913568Sbrandon.potter@amd.com
180378SN/A/// Target gethostname() handler.
1811706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
18211851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
183360SN/A
1845513SMichael.Adler@intel.com/// Target getcwd() handler.
1855513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
18611851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1875513SMichael.Adler@intel.com
18810203SAli.Saidi@ARM.com/// Target readlink() handler.
18910203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19011851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
19110203SAli.Saidi@ARM.com                           int index = 0);
1925513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19311851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
1945513SMichael.Adler@intel.com
195511SN/A/// Target unlink() handler.
19610633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
19711851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
19810633Smichaelupton@gmail.com                           int index);
1991706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
201511SN/A
20212795Smattdsinclair@gmail.com/// Target link() handler
20312795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
20412795Smattdsinclair@gmail.com                       ThreadContext *tc);
20512795Smattdsinclair@gmail.com
20612796Smattdsinclair@gmail.com/// Target symlink() handler.
20712796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
20812796Smattdsinclair@gmail.com                          ThreadContext *tc);
20912796Smattdsinclair@gmail.com
2105513SMichael.Adler@intel.com/// Target mkdir() handler.
2115513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
21211851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2135513SMichael.Adler@intel.com
21413031Sbrandon.potter@amd.com/// Target mknod() handler.
21513031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
21613031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
21713031Sbrandon.potter@amd.com
21813031Sbrandon.potter@amd.com/// Target chdir() handler.
21913031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
22013031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22113031Sbrandon.potter@amd.com
22213031Sbrandon.potter@amd.com// Target rmdir() handler.
22313031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
22413031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22513031Sbrandon.potter@amd.com
226511SN/A/// Target rename() handler.
2271706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
22811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2291706SN/A
2301706SN/A
2311706SN/A/// Target truncate() handler.
2321706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
23311851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2341706SN/A
2351706SN/A
2361706SN/A/// Target ftruncate() handler.
2371706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
23811851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2391706SN/A
240511SN/A
2416703Svince@csl.cornell.edu/// Target truncate64() handler.
2426703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
24311851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2446703Svince@csl.cornell.edu
2456685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2466685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
24711851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2486685Stjones1@inf.ed.ac.uk
2496685Stjones1@inf.ed.ac.uk
2505513SMichael.Adler@intel.com/// Target umask() handler.
2515513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
25211851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2535513SMichael.Adler@intel.com
25411885Sbrandon.potter@amd.com/// Target gettid() handler.
25511885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
25611885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2575513SMichael.Adler@intel.com
2581999SN/A/// Target chown() handler.
2591999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26011851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2611999SN/A
26211885Sbrandon.potter@amd.com/// Target setpgid() handler.
26311885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
26411885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2651999SN/A
2661999SN/A/// Target fchown() handler.
2671999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
26811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2691999SN/A
2703079Sstever@eecs.umich.edu/// Target dup() handler.
2713079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
27211851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2733079Sstever@eecs.umich.edu
27411908SBrandon.Potter@amd.com/// Target dup2() handler.
27511908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
27611908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
27711908SBrandon.Potter@amd.com
27811875Sbrandon.potter@amd.com/// Target fcntl() handler.
2792093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28011851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2812093SN/A
2822687Sksewell@umich.edu/// Target fcntl64() handler.
2832687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
28411851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2852687Sksewell@umich.edu
2862238SN/A/// Target setuid() handler.
2872238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
28811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2892238SN/A
29011908SBrandon.Potter@amd.com/// Target pipe() handler.
29111908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
29211908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
29311908SBrandon.Potter@amd.com
29411908SBrandon.Potter@amd.com/// Internal pipe() handler.
29511908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
29611908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
29711908SBrandon.Potter@amd.com
2982238SN/A/// Target getpid() handler.
2992238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3012238SN/A
30213571Sbrandon.potter@amd.com// Target getpeername() handler.
30313571Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num,
30413571Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
30513571Sbrandon.potter@amd.com
30613568Sbrandon.potter@amd.com// Target bind() handler.
30713568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num,
30813568Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
30913568Sbrandon.potter@amd.com
31013568Sbrandon.potter@amd.com// Target listen() handler.
31113568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num,
31213568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
31313568Sbrandon.potter@amd.com
31413568Sbrandon.potter@amd.com// Target connect() handler.
31513568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num,
31613568Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
31713568Sbrandon.potter@amd.com
31813448Sciro.santilli@arm.com#if defined(SYS_getdents)
31913031Sbrandon.potter@amd.com// Target getdents() handler.
32013031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
32113031Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
32213448Sciro.santilli@arm.com#endif
32313031Sbrandon.potter@amd.com
32413539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
32513539Sjavier.setoain@arm.com// Target getdents() handler.
32613539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
32713539Sjavier.setoain@arm.com                           Process *p, ThreadContext *tc);
32813539Sjavier.setoain@arm.com#endif
32913539Sjavier.setoain@arm.com
33013569Sbrandon.potter@amd.com// Target sendto() handler.
33113569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num,
33213569Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
33313569Sbrandon.potter@amd.com
33413569Sbrandon.potter@amd.com// Target recvfrom() handler.
33513569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num,
33613569Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
33713569Sbrandon.potter@amd.com
33813569Sbrandon.potter@amd.com// Target recvmsg() handler.
33913569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num,
34013569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
34113569Sbrandon.potter@amd.com
34213569Sbrandon.potter@amd.com// Target sendmsg() handler.
34313569Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num,
34413569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
34513569Sbrandon.potter@amd.com
34613031Sbrandon.potter@amd.com// Target getuid() handler.
3472238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
34811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3492238SN/A
3502238SN/A/// Target getgid() handler.
3512238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
35211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3532238SN/A
3542238SN/A/// Target getppid() handler.
3552238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
35611851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3572238SN/A
3582238SN/A/// Target geteuid() handler.
3592238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
36011851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3612238SN/A
3622238SN/A/// Target getegid() handler.
3632238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
36411851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3652238SN/A
3669455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3679455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
36811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
36910203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
37111851Sbrandon.potter@amd.com                         int index);
3729455Smitch.hayenga+gem5@gmail.com
37313571Sbrandon.potter@amd.com// Target getsockopt() handler.
37413571Sbrandon.potter@amd.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num,
37513571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
37613571Sbrandon.potter@amd.com
37713571Sbrandon.potter@amd.com// Target setsockopt() handler.
37813571Sbrandon.potter@amd.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num,
37913571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
38013571Sbrandon.potter@amd.com
38113571Sbrandon.potter@amd.com// Target getsockname() handler.
38213571Sbrandon.potter@amd.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num,
38313571Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
38413571Sbrandon.potter@amd.com
3859112Smarc.orr@gmail.com/// Futex system call
38611906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
38711906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3889112Smarc.orr@gmail.comtemplate <class OS>
3899112Smarc.orr@gmail.comSyscallReturn
39011851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3919112Smarc.orr@gmail.com          ThreadContext *tc)
3929112Smarc.orr@gmail.com{
39311911SBrandon.Potter@amd.com    using namespace std;
3949112Smarc.orr@gmail.com
39511911SBrandon.Potter@amd.com    int index = 0;
39611911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
39711911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
39811911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
39913642Sqtt2@cornell.edu    int timeout M5_VAR_USED = process->getSyscallArg(tc, index);
40013642Sqtt2@cornell.edu    Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index);
40113642Sqtt2@cornell.edu    int val3 = process->getSyscallArg(tc, index);
4029112Smarc.orr@gmail.com
40311911SBrandon.Potter@amd.com    /*
40411911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
40511911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
40611911SBrandon.Potter@amd.com     */
4079238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
40813642Sqtt2@cornell.edu    op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG;
4099112Smarc.orr@gmail.com
41011911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
4119112Smarc.orr@gmail.com
41213642Sqtt2@cornell.edu    if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) {
41311911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
41411911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
41511911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
41611911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
4179112Smarc.orr@gmail.com
41811911SBrandon.Potter@amd.com        /*
41911911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
42011911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
42111911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
42211911SBrandon.Potter@amd.com         */
42311911SBrandon.Potter@amd.com        if (val != mem_val)
4249112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
4259112Smarc.orr@gmail.com
42613642Sqtt2@cornell.edu        if (OS::TGT_FUTEX_WAIT) {
42713642Sqtt2@cornell.edu            futex_map.suspend(uaddr, process->tgid(), tc);
42813642Sqtt2@cornell.edu        } else {
42913642Sqtt2@cornell.edu            futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3);
43013642Sqtt2@cornell.edu        }
43111911SBrandon.Potter@amd.com
4329112Smarc.orr@gmail.com        return 0;
43311911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
43411911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
43513642Sqtt2@cornell.edu    } else if (OS::TGT_FUTEX_WAKE_BITSET == op) {
43613642Sqtt2@cornell.edu        return futex_map.wakeup_bitset(uaddr, process->tgid(), val3);
43713650Smw828@cornell.edu    } else if (OS::TGT_FUTEX_REQUEUE == op ||
43813650Smw828@cornell.edu               OS::TGT_FUTEX_CMP_REQUEUE == op) {
43913650Smw828@cornell.edu
44013650Smw828@cornell.edu        // Ensure futex system call accessed atomically.
44113650Smw828@cornell.edu        BufferArg buf(uaddr, sizeof(int));
44213650Smw828@cornell.edu        buf.copyIn(tc->getMemProxy());
44313650Smw828@cornell.edu        int mem_val = *(int*)buf.bufferPtr();
44413650Smw828@cornell.edu        /*
44513650Smw828@cornell.edu         * For CMP_REQUEUE, the whole operation is only started only if
44613650Smw828@cornell.edu         * val3 is still the value of the futex pointed to by uaddr.
44713650Smw828@cornell.edu         */
44813650Smw828@cornell.edu        if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val)
44913650Smw828@cornell.edu            return -OS::TGT_EWOULDBLOCK;
45013650Smw828@cornell.edu        return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2);
45113651Smw828@cornell.edu    } else if (OS::TGT_FUTEX_WAKE_OP == op) {
45213651Smw828@cornell.edu        /*
45313651Smw828@cornell.edu         * The FUTEX_WAKE_OP operation is equivalent to executing the
45413651Smw828@cornell.edu         * following code atomically and totally ordered with respect to
45513651Smw828@cornell.edu         * other futex operations on any of the two supplied futex words:
45613651Smw828@cornell.edu         *
45713651Smw828@cornell.edu         *   int oldval = *(int *) addr2;
45813651Smw828@cornell.edu         *   *(int *) addr2 = oldval op oparg;
45913651Smw828@cornell.edu         *   futex(addr1, FUTEX_WAKE, val, 0, 0, 0);
46013651Smw828@cornell.edu         *   if (oldval cmp cmparg)
46113651Smw828@cornell.edu         *        futex(addr2, FUTEX_WAKE, val2, 0, 0, 0);
46213651Smw828@cornell.edu         *
46313651Smw828@cornell.edu         * (op, oparg, cmp, cmparg are encoded in val3)
46413651Smw828@cornell.edu         *
46513651Smw828@cornell.edu         * +---+---+-----------+-----------+
46613651Smw828@cornell.edu         * |op |cmp|   oparg   |  cmparg   |
46713651Smw828@cornell.edu         * +---+---+-----------+-----------+
46813651Smw828@cornell.edu         *   4   4       12          12    <== # of bits
46913651Smw828@cornell.edu         *
47013651Smw828@cornell.edu         * reference: http://man7.org/linux/man-pages/man2/futex.2.html
47113651Smw828@cornell.edu         *
47213651Smw828@cornell.edu         */
47313651Smw828@cornell.edu        // get value from simulated-space
47413651Smw828@cornell.edu        BufferArg buf(uaddr2, sizeof(int));
47513651Smw828@cornell.edu        buf.copyIn(tc->getMemProxy());
47613651Smw828@cornell.edu        int oldval = *(int*)buf.bufferPtr();
47713651Smw828@cornell.edu        int newval = oldval;
47813651Smw828@cornell.edu        // extract op, oparg, cmp, cmparg from val3
47913651Smw828@cornell.edu        int wake_cmparg =  val3 & 0xfff;
48013651Smw828@cornell.edu        int wake_oparg  = (val3 & 0xfff000)   >> 12;
48113651Smw828@cornell.edu        int wake_cmp    = (val3 & 0xf000000)  >> 24;
48213651Smw828@cornell.edu        int wake_op     = (val3 & 0xf0000000) >> 28;
48313651Smw828@cornell.edu        if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1)
48413651Smw828@cornell.edu            wake_oparg = (1 << wake_oparg);
48513651Smw828@cornell.edu        wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT;
48613651Smw828@cornell.edu        // perform operation on the value of the second futex
48713651Smw828@cornell.edu        if (wake_op == OS::TGT_FUTEX_OP_SET)
48813651Smw828@cornell.edu            newval = wake_oparg;
48913651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_ADD)
49013651Smw828@cornell.edu            newval += wake_oparg;
49113651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_OR)
49213651Smw828@cornell.edu            newval |= wake_oparg;
49313651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_ANDN)
49413651Smw828@cornell.edu            newval &= ~wake_oparg;
49513651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_XOR)
49613651Smw828@cornell.edu            newval ^= wake_oparg;
49713651Smw828@cornell.edu        // copy updated value back to simulated-space
49813651Smw828@cornell.edu        *(int*)buf.bufferPtr() = newval;
49913651Smw828@cornell.edu        buf.copyOut(tc->getMemProxy());
50013651Smw828@cornell.edu        // perform the first wake-up
50113651Smw828@cornell.edu        int woken1 = futex_map.wakeup(uaddr, process->tgid(), val);
50213651Smw828@cornell.edu        int woken2 = 0;
50313651Smw828@cornell.edu        // calculate the condition of the second wake-up
50413651Smw828@cornell.edu        bool is_wake2 = false;
50513651Smw828@cornell.edu        if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ)
50613651Smw828@cornell.edu            is_wake2 = oldval == wake_cmparg;
50713651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE)
50813651Smw828@cornell.edu            is_wake2 = oldval != wake_cmparg;
50913651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT)
51013651Smw828@cornell.edu            is_wake2 = oldval < wake_cmparg;
51113651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE)
51213651Smw828@cornell.edu            is_wake2 = oldval <= wake_cmparg;
51313651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT)
51413651Smw828@cornell.edu            is_wake2 = oldval > wake_cmparg;
51513651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE)
51613651Smw828@cornell.edu            is_wake2 = oldval >= wake_cmparg;
51713651Smw828@cornell.edu        // perform the second wake-up
51813651Smw828@cornell.edu        if (is_wake2)
51913651Smw828@cornell.edu            woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout);
52013651Smw828@cornell.edu
52113651Smw828@cornell.edu        return woken1 + woken2;
5229112Smarc.orr@gmail.com    }
52311911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
52411911SBrandon.Potter@amd.com    return -ENOSYS;
5259112Smarc.orr@gmail.com}
5269112Smarc.orr@gmail.com
5272238SN/A
5282238SN/A/// Pseudo Funcs  - These functions use a different return convension,
5292238SN/A/// returning a second value in a register other than the normal return register
5302238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
53111851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
5322238SN/A
5332238SN/A/// Target getpidPseudo() handler.
5342238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
53511851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
5362238SN/A
5372238SN/A/// Target getuidPseudo() handler.
5382238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
53911851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
5402238SN/A
5412238SN/A/// Target getgidPseudo() handler.
5422238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
54311851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
5442238SN/A
5452238SN/A
5461354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
5471354SN/Aconst int one_million = 1000000;
54810796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
54910796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
5501354SN/A
5511354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
5521354SN/A/// by my reckoning.  We want to keep this a constant (not use the
5531354SN/A/// real-world time) to keep simulations repeatable.
5541354SN/Aconst unsigned seconds_since_epoch = 1000000000;
5551354SN/A
5561354SN/A/// Helper function to convert current elapsed time to seconds and
5571354SN/A/// microseconds.
5581354SN/Atemplate <class T1, class T2>
5591354SN/Avoid
56010796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
5611354SN/A{
56210796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
5631354SN/A    sec = elapsed_usecs / one_million;
5641354SN/A    usec = elapsed_usecs % one_million;
5651354SN/A}
5661354SN/A
56710796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
56810796Sbrandon.potter@amd.com/// nanoseconds.
56910796Sbrandon.potter@amd.comtemplate <class T1, class T2>
57010796Sbrandon.potter@amd.comvoid
57110796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
57210796Sbrandon.potter@amd.com{
57310796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
57410796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
57510796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
57610796Sbrandon.potter@amd.com}
57710796Sbrandon.potter@amd.com
578360SN/A//////////////////////////////////////////////////////////////////////
579360SN/A//
580360SN/A// The following emulation functions are generic, but need to be
581360SN/A// templated to account for differences in types, constants, etc.
582360SN/A//
583360SN/A//////////////////////////////////////////////////////////////////////
584360SN/A
58511759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
5863113Sgblack@eecs.umich.edu#if NO_STAT64
5873113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5883113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
5893113Sgblack@eecs.umich.edu#else
5903113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5913113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
5923113Sgblack@eecs.umich.edu#endif
5933113Sgblack@eecs.umich.edu
5943113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
5953113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
5963113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
5973113Sgblack@eecs.umich.edu
5983113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
59912032Sandreas.sandberg@arm.comvoid
6003113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
6013113Sgblack@eecs.umich.edu{
6024189Sgblack@eecs.umich.edu    using namespace TheISA;
6034189Sgblack@eecs.umich.edu
6043113Sgblack@eecs.umich.edu    if (fakeTTY)
6053113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
6063113Sgblack@eecs.umich.edu    else
6073113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
6088737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
6093113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
6108737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
6113277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
6125515SMichael.Adler@intel.com    if (fakeTTY) {
6135515SMichael.Adler@intel.com        // Claim to be a character device
6145515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
6155515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
6165515SMichael.Adler@intel.com    }
6178737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
6183277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
6198737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
6203277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
6218737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
6223277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
6238737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
6243113Sgblack@eecs.umich.edu    if (fakeTTY)
6253113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
6263113Sgblack@eecs.umich.edu    else
6273113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
6288737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
6293113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
6308737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
6313114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
6328737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
6333114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
6348737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
6353114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
6368737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
63711906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
6384061Sgblack@eecs.umich.edu    // consistently across different hosts.
6394061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
6408737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
6413113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
6428737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
6433113Sgblack@eecs.umich.edu}
6443113Sgblack@eecs.umich.edu
6453113Sgblack@eecs.umich.edu// Same for stat64
6463113Sgblack@eecs.umich.edu
6473113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
64812032Sandreas.sandberg@arm.comvoid
6493113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
6503113Sgblack@eecs.umich.edu{
6514189Sgblack@eecs.umich.edu    using namespace TheISA;
6524189Sgblack@eecs.umich.edu
6533113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
6543113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
6553113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
6568737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
6573113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
6588737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
6593113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
6608737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
6613113Sgblack@eecs.umich.edu#else
6623113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
6633113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
6643113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
6653113Sgblack@eecs.umich.edu#endif
6663113Sgblack@eecs.umich.edu}
6673113Sgblack@eecs.umich.edu
66811906SBrandon.Potter@amd.com// Here are a couple of convenience functions
6693113Sgblack@eecs.umich.edutemplate<class OS>
67012032Sandreas.sandberg@arm.comvoid
6718852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
67211906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
6733113Sgblack@eecs.umich.edu{
6743113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
6753113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
6763113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
6773113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
6783113Sgblack@eecs.umich.edu}
6793113Sgblack@eecs.umich.edu
6803113Sgblack@eecs.umich.edutemplate<class OS>
68112032Sandreas.sandberg@arm.comvoid
6828852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
68311906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
6843113Sgblack@eecs.umich.edu{
6853113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
6863113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
6876686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
6883113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
6893113Sgblack@eecs.umich.edu}
6903113Sgblack@eecs.umich.edu
69111759Sbrandon.potter@amd.comtemplate <class OS>
69212032Sandreas.sandberg@arm.comvoid
69311759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
69411759Sbrandon.potter@amd.com                 hst_statfs *host)
69511759Sbrandon.potter@amd.com{
69611759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
69711759Sbrandon.potter@amd.com
69811812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
69911812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
70011812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
70111759Sbrandon.potter@amd.com#else
70211812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
70311759Sbrandon.potter@amd.com#endif
70411759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
70511759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
70611759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
70711759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
70811759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
70911759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
71011812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
71111812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
71211812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
71311812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
71411812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
71511812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
71611812Sbaz21@cam.ac.uk#else
71711759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
71811759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
71911812Sbaz21@cam.ac.uk#endif
72011812Sbaz21@cam.ac.uk#if defined(__linux__)
72111759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
72211812Sbaz21@cam.ac.uk#else
72311812Sbaz21@cam.ac.uk    /*
72411812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
72511812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
72611812Sbaz21@cam.ac.uk     */
72711812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
72811812Sbaz21@cam.ac.uk#endif
72911759Sbrandon.potter@amd.com
73011759Sbrandon.potter@amd.com    tgt.copyOut(mem);
73111759Sbrandon.potter@amd.com}
73211759Sbrandon.potter@amd.com
733378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
734378SN/A/// only to find out if their stdout is a tty, to determine whether to
7359141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
7369141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
737360SN/Atemplate <class OS>
7381450SN/ASyscallReturn
73911856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
740360SN/A{
7416701Sgblack@eecs.umich.edu    int index = 0;
74211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
74311856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
744360SN/A
74513907Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
746360SN/A
74711856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
74811856Sbrandon.potter@amd.com        return -ENOTTY;
74910496Ssteve.reinhardt@amd.com
75011856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
75113902Sbrandon.potter@amd.com    if (dfdp) {
75213902Sbrandon.potter@amd.com        EmulatedDriver *emul_driver = dfdp->getDriver();
75313902Sbrandon.potter@amd.com        if (emul_driver)
75413902Sbrandon.potter@amd.com            return emul_driver->ioctl(p, tc, req);
75513902Sbrandon.potter@amd.com    }
75613902Sbrandon.potter@amd.com
75713902Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
75813902Sbrandon.potter@amd.com    if (sfdp) {
75913902Sbrandon.potter@amd.com        int status;
76013902Sbrandon.potter@amd.com
76113902Sbrandon.potter@amd.com        switch (req) {
76213902Sbrandon.potter@amd.com          case SIOCGIFCONF: {
76313902Sbrandon.potter@amd.com            Addr conf_addr = p->getSyscallArg(tc, index);
76413902Sbrandon.potter@amd.com            BufferArg conf_arg(conf_addr, sizeof(ifconf));
76513902Sbrandon.potter@amd.com            conf_arg.copyIn(tc->getMemProxy());
76613902Sbrandon.potter@amd.com
76713902Sbrandon.potter@amd.com            ifconf *conf = (ifconf*)conf_arg.bufferPtr();
76813902Sbrandon.potter@amd.com            Addr ifc_buf_addr = (Addr)conf->ifc_buf;
76913902Sbrandon.potter@amd.com            BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len);
77013902Sbrandon.potter@amd.com            ifc_buf_arg.copyIn(tc->getMemProxy());
77113902Sbrandon.potter@amd.com
77213902Sbrandon.potter@amd.com            conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr();
77313902Sbrandon.potter@amd.com
77413902Sbrandon.potter@amd.com            status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr());
77513902Sbrandon.potter@amd.com            if (status != -1) {
77613902Sbrandon.potter@amd.com                conf->ifc_buf = (char*)ifc_buf_addr;
77713902Sbrandon.potter@amd.com                ifc_buf_arg.copyOut(tc->getMemProxy());
77813902Sbrandon.potter@amd.com                conf_arg.copyOut(tc->getMemProxy());
77913902Sbrandon.potter@amd.com            }
78013902Sbrandon.potter@amd.com
78113902Sbrandon.potter@amd.com            return status;
78213902Sbrandon.potter@amd.com          }
78313902Sbrandon.potter@amd.com          case SIOCGIFFLAGS:
78413936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
78513902Sbrandon.potter@amd.com          case SIOCGIFINDEX:
78613902Sbrandon.potter@amd.com#endif
78713902Sbrandon.potter@amd.com          case SIOCGIFNETMASK:
78813902Sbrandon.potter@amd.com          case SIOCGIFADDR:
78913936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
79013902Sbrandon.potter@amd.com          case SIOCGIFHWADDR:
79113902Sbrandon.potter@amd.com#endif
79213902Sbrandon.potter@amd.com          case SIOCGIFMTU: {
79313902Sbrandon.potter@amd.com            Addr req_addr = p->getSyscallArg(tc, index);
79413902Sbrandon.potter@amd.com            BufferArg req_arg(req_addr, sizeof(ifreq));
79513902Sbrandon.potter@amd.com            req_arg.copyIn(tc->getMemProxy());
79613902Sbrandon.potter@amd.com
79713902Sbrandon.potter@amd.com            status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr());
79813902Sbrandon.potter@amd.com            if (status != -1)
79913902Sbrandon.potter@amd.com                req_arg.copyOut(tc->getMemProxy());
80013902Sbrandon.potter@amd.com            return status;
80113902Sbrandon.potter@amd.com          }
80213902Sbrandon.potter@amd.com        }
80313902Sbrandon.potter@amd.com    }
80410496Ssteve.reinhardt@amd.com
80511856Sbrandon.potter@amd.com    /**
80611856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
80711856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
80811856Sbrandon.potter@amd.com     */
80911856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
81010930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
8119141Smarc.orr@gmail.com    return -ENOTTY;
812360SN/A}
813360SN/A
814360SN/Atemplate <class OS>
81511907SBrandon.Potter@amd.comSyscallReturn
81611907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
81711907SBrandon.Potter@amd.com         bool isopenat)
818360SN/A{
81911907SBrandon.Potter@amd.com    int index = 0;
82011907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
82111907SBrandon.Potter@amd.com
82211907SBrandon.Potter@amd.com    /**
82311907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
82411907SBrandon.Potter@amd.com     * descriptor from the simulated process.
82511907SBrandon.Potter@amd.com     */
82611907SBrandon.Potter@amd.com    if (isopenat)
82711907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
82811907SBrandon.Potter@amd.com
82911907SBrandon.Potter@amd.com    /**
83011907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
83111907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
83211907SBrandon.Potter@amd.com     */
833360SN/A    std::string path;
83411907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
8351458SN/A        return -EFAULT;
836360SN/A
83711907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
83811907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
83911907SBrandon.Potter@amd.com#else
84011907SBrandon.Potter@amd.com    int host_flags = 0;
84111907SBrandon.Potter@amd.com#endif
84211907SBrandon.Potter@amd.com    /**
84311907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
84411907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
84511907SBrandon.Potter@amd.com     */
84611907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
847360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
84811907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
84911907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
85011907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
851360SN/A        }
852360SN/A    }
85311907SBrandon.Potter@amd.com    if (tgt_flags) {
85411907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
85511907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
85611907SBrandon.Potter@amd.com    }
857360SN/A#ifdef __CYGWIN32__
85811907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
859360SN/A#endif
860360SN/A
86111907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
8623669Sbinkertn@umich.edu
86311907SBrandon.Potter@amd.com    /**
86411907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
86511907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
86611907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
86711907SBrandon.Potter@amd.com     * create a full path.
86811907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
86911907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
87011907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
87111907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
87211907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
87311907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
87411907SBrandon.Potter@amd.com     */
87513883Sdavid.hashe@amd.com    std::string redir_path = path;
87613883Sdavid.hashe@amd.com    std::string abs_path = path;
87713883Sdavid.hashe@amd.com    if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) {
87813883Sdavid.hashe@amd.com        abs_path = p->absolutePath(path, true);
87913883Sdavid.hashe@amd.com        redir_path = p->checkPathRedirect(path);
88011907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
88111907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
88211907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
88311907SBrandon.Potter@amd.com        if (!ffdp)
88411907SBrandon.Potter@amd.com            return -EBADF;
88513883Sdavid.hashe@amd.com        abs_path = ffdp->getFileName() + path;
88613883Sdavid.hashe@amd.com        redir_path = p->checkPathRedirect(abs_path);
88711907SBrandon.Potter@amd.com    }
8881706SN/A
88911907SBrandon.Potter@amd.com    /**
89011907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
89111907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
89211907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
89311907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
89411907SBrandon.Potter@amd.com     */
89513883Sdavid.hashe@amd.com    if (startswith(abs_path, "/dev/")) {
89613883Sdavid.hashe@amd.com        std::string filename = abs_path.substr(strlen("/dev/"));
89711907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
89811907SBrandon.Potter@amd.com        if (drv) {
89911907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
90011907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
90113883Sdavid.hashe@amd.com                            isopenat ? "at" : "", abs_path.c_str());
90211907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
90310496Ssteve.reinhardt@amd.com        }
90411907SBrandon.Potter@amd.com        /**
90511907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
90611907SBrandon.Potter@amd.com         * as /dev/zero
90711907SBrandon.Potter@amd.com         */
90810496Ssteve.reinhardt@amd.com    }
90910496Ssteve.reinhardt@amd.com
91011907SBrandon.Potter@amd.com    /**
91113883Sdavid.hashe@amd.com     * We make several attempts resolve a call to open.
91213883Sdavid.hashe@amd.com     *
91313883Sdavid.hashe@amd.com     * 1) Resolve any path redirection before hand. This will set the path
91413883Sdavid.hashe@amd.com     * up with variable 'redir_path' which may contain a modified path or
91513883Sdavid.hashe@amd.com     * the original path value. This should already be done in prior code.
91613883Sdavid.hashe@amd.com     * 2) Try to handle the access using 'special_paths'. Some special_paths
91713883Sdavid.hashe@amd.com     * and files cannot be called on the host and need to be handled as
91813883Sdavid.hashe@amd.com     * special cases inside the simulator. These special_paths are handled by
91913883Sdavid.hashe@amd.com     * C++ routines to provide output back to userspace.
92013883Sdavid.hashe@amd.com     * 3) If the full path that was created above does not match any of the
92113883Sdavid.hashe@amd.com     * special cases, pass it through to the open call on the __HOST__ to let
92213883Sdavid.hashe@amd.com     * the host open the file on our behalf. Again, the openImpl tries to
92313883Sdavid.hashe@amd.com     * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the
92413883Sdavid.hashe@amd.com     * faux-filesystem files). The faux-filesystem is dynamically created
92513883Sdavid.hashe@amd.com     * during simulator configuration using Python functions.
92613883Sdavid.hashe@amd.com     * 4) If the host cannot open the file, the open attempt failed in "3)".
92713883Sdavid.hashe@amd.com     * Return the host's error code back through the system call to the
92813883Sdavid.hashe@amd.com     * simulated process. If running a debug trace, also notify the user that
92913883Sdavid.hashe@amd.com     * the open call failed.
93013883Sdavid.hashe@amd.com     *
93113883Sdavid.hashe@amd.com     * Any success will set sim_fd to something other than -1 and skip the
93213883Sdavid.hashe@amd.com     * next conditions effectively bypassing them.
93311907SBrandon.Potter@amd.com     */
93411907SBrandon.Potter@amd.com    int sim_fd = -1;
93513883Sdavid.hashe@amd.com    std::string used_path;
93611907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
93713883Sdavid.hashe@amd.com            { "/proc/meminfo/", "/system/", "/sys/", "/platform/",
93813883Sdavid.hashe@amd.com              "/etc/passwd" };
93911907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
94013883Sdavid.hashe@amd.com        if (startswith(path, entry)) {
94113883Sdavid.hashe@amd.com            sim_fd = OS::openSpecialFile(abs_path, p, tc);
94213883Sdavid.hashe@amd.com            used_path = abs_path;
94313883Sdavid.hashe@amd.com        }
94411907SBrandon.Potter@amd.com    }
94511907SBrandon.Potter@amd.com    if (sim_fd == -1) {
94613883Sdavid.hashe@amd.com        sim_fd = open(redir_path.c_str(), host_flags, mode);
94713883Sdavid.hashe@amd.com        used_path = redir_path;
94811907SBrandon.Potter@amd.com    }
94911907SBrandon.Potter@amd.com    if (sim_fd == -1) {
95011907SBrandon.Potter@amd.com        int local = -errno;
95113883Sdavid.hashe@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s "
95213883Sdavid.hashe@amd.com                        "(inferred from:%s)\n", isopenat ? "at" : "",
95313883Sdavid.hashe@amd.com                        used_path.c_str(), path.c_str());
95411907SBrandon.Potter@amd.com        return local;
95511907SBrandon.Potter@amd.com    }
956360SN/A
95711907SBrandon.Potter@amd.com    /**
95811907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
95911907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
96011907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
96111907SBrandon.Potter@amd.com     * file descriptor.
96211907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
96311907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
96411907SBrandon.Potter@amd.com     */
96511907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
96611907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
96713883Sdavid.hashe@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n"
96813883Sdavid.hashe@amd.com                    "(inferred from:%s)\n", isopenat ? "at" : "",
96913883Sdavid.hashe@amd.com                    sim_fd, tgt_fd, used_path.c_str(), path.c_str());
97011907SBrandon.Potter@amd.com    return tgt_fd;
971360SN/A}
972360SN/A
97310027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
97410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
97510027SChris.Adeniyi-Jones@arm.comSyscallReturn
97611851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
97710027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
97810027SChris.Adeniyi-Jones@arm.com{
97911907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
98010027SChris.Adeniyi-Jones@arm.com}
98110027SChris.Adeniyi-Jones@arm.com
98210027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
98310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
98410027SChris.Adeniyi-Jones@arm.comSyscallReturn
98511851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
98611851Sbrandon.potter@amd.com           ThreadContext *tc)
98710027SChris.Adeniyi-Jones@arm.com{
98811907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
98910027SChris.Adeniyi-Jones@arm.com}
99010027SChris.Adeniyi-Jones@arm.com
99110633Smichaelupton@gmail.com/// Target unlinkat() handler.
99210633Smichaelupton@gmail.comtemplate <class OS>
99310633Smichaelupton@gmail.comSyscallReturn
99411851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
99510633Smichaelupton@gmail.com             ThreadContext *tc)
99610633Smichaelupton@gmail.com{
99710633Smichaelupton@gmail.com    int index = 0;
99810633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
99910633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
100010633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
100110633Smichaelupton@gmail.com
100210633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
100310633Smichaelupton@gmail.com}
100410633Smichaelupton@gmail.com
100510203SAli.Saidi@ARM.com/// Target facessat() handler
100610203SAli.Saidi@ARM.comtemplate <class OS>
100710203SAli.Saidi@ARM.comSyscallReturn
100811851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
100911851Sbrandon.potter@amd.com              ThreadContext *tc)
101010203SAli.Saidi@ARM.com{
101110203SAli.Saidi@ARM.com    int index = 0;
101210203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
101310203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
101410203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
101510203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
101610203SAli.Saidi@ARM.com}
101710203SAli.Saidi@ARM.com
101810203SAli.Saidi@ARM.com/// Target readlinkat() handler
101910203SAli.Saidi@ARM.comtemplate <class OS>
102010203SAli.Saidi@ARM.comSyscallReturn
102111851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
102211851Sbrandon.potter@amd.com               ThreadContext *tc)
102310203SAli.Saidi@ARM.com{
102410203SAli.Saidi@ARM.com    int index = 0;
102510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
102610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
102710203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
102810203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
102910203SAli.Saidi@ARM.com}
103010203SAli.Saidi@ARM.com
103110850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
103210850SGiacomo.Gabrielli@arm.comtemplate <class OS>
103310850SGiacomo.Gabrielli@arm.comSyscallReturn
103411851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
103510850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
103610850SGiacomo.Gabrielli@arm.com{
103710850SGiacomo.Gabrielli@arm.com    int index = 0;
103810850SGiacomo.Gabrielli@arm.com
103910850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
104010850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
104110850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
104210850SGiacomo.Gabrielli@arm.com
104310850SGiacomo.Gabrielli@arm.com    std::string old_name;
104410850SGiacomo.Gabrielli@arm.com
104510850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
104610850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
104710850SGiacomo.Gabrielli@arm.com        return -EFAULT;
104810850SGiacomo.Gabrielli@arm.com
104910850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
105010850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
105110850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
105210850SGiacomo.Gabrielli@arm.com
105310850SGiacomo.Gabrielli@arm.com    std::string new_name;
105410850SGiacomo.Gabrielli@arm.com
105510850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
105610850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
105710850SGiacomo.Gabrielli@arm.com        return -EFAULT;
105810850SGiacomo.Gabrielli@arm.com
105913883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
106013883Sdavid.hashe@amd.com    old_name = process->checkPathRedirect(old_name);
106113883Sdavid.hashe@amd.com    new_name = process->checkPathRedirect(new_name);
106210850SGiacomo.Gabrielli@arm.com
106310850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
106410850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
106510850SGiacomo.Gabrielli@arm.com}
106610850SGiacomo.Gabrielli@arm.com
10676640Svince@csl.cornell.edu/// Target sysinfo() handler.
10686640Svince@csl.cornell.edutemplate <class OS>
10696640Svince@csl.cornell.eduSyscallReturn
107011851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
107111851Sbrandon.potter@amd.com            ThreadContext *tc)
10726640Svince@csl.cornell.edu{
10736640Svince@csl.cornell.edu
10746701Sgblack@eecs.umich.edu    int index = 0;
10756701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
107610793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
10776640Svince@csl.cornell.edu
107811758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
107911758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
108011758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
10816640Svince@csl.cornell.edu
10828706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
10836640Svince@csl.cornell.edu
10846701Sgblack@eecs.umich.edu    return 0;
10856640Svince@csl.cornell.edu}
1086360SN/A
10871999SN/A/// Target chmod() handler.
10881999SN/Atemplate <class OS>
10891999SN/ASyscallReturn
109011851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
10912680Sktlim@umich.edu          ThreadContext *tc)
10921999SN/A{
10931999SN/A    std::string path;
10941999SN/A
10956701Sgblack@eecs.umich.edu    int index = 0;
10968852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10976701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10981999SN/A        return -EFAULT;
10996701Sgblack@eecs.umich.edu    }
11001999SN/A
11016701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
11021999SN/A    mode_t hostMode = 0;
11031999SN/A
11041999SN/A    // XXX translate mode flags via OS::something???
11051999SN/A    hostMode = mode;
11061999SN/A
110713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
110813883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
11093669Sbinkertn@umich.edu
11101999SN/A    // do the chmod
11111999SN/A    int result = chmod(path.c_str(), hostMode);
11121999SN/A    if (result < 0)
11132218SN/A        return -errno;
11141999SN/A
11151999SN/A    return 0;
11161999SN/A}
11171999SN/A
111813570Sbrandon.potter@amd.comtemplate <class OS>
111913570Sbrandon.potter@amd.comSyscallReturn
112013570Sbrandon.potter@amd.compollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
112113570Sbrandon.potter@amd.com{
112213570Sbrandon.potter@amd.com    int index = 0;
112313570Sbrandon.potter@amd.com    Addr fdsPtr = p->getSyscallArg(tc, index);
112413570Sbrandon.potter@amd.com    int nfds = p->getSyscallArg(tc, index);
112513570Sbrandon.potter@amd.com    int tmout = p->getSyscallArg(tc, index);
112613570Sbrandon.potter@amd.com
112713570Sbrandon.potter@amd.com    BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds);
112813570Sbrandon.potter@amd.com    fdsBuf.copyIn(tc->getMemProxy());
112913570Sbrandon.potter@amd.com
113013570Sbrandon.potter@amd.com    /**
113113570Sbrandon.potter@amd.com     * Record the target file descriptors in a local variable. We need to
113213570Sbrandon.potter@amd.com     * replace them with host file descriptors but we need a temporary copy
113313570Sbrandon.potter@amd.com     * for later. Afterwards, replace each target file descriptor in the
113413570Sbrandon.potter@amd.com     * poll_fd array with its host_fd.
113513570Sbrandon.potter@amd.com     */
113613570Sbrandon.potter@amd.com    int temp_tgt_fds[nfds];
113713570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
113813570Sbrandon.potter@amd.com        temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd;
113913570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
114013570Sbrandon.potter@amd.com        auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
114113570Sbrandon.potter@amd.com        if (!hbfdp)
114213570Sbrandon.potter@amd.com            return -EBADF;
114313570Sbrandon.potter@amd.com        auto host_fd = hbfdp->getSimFD();
114413570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd;
114513570Sbrandon.potter@amd.com    }
114613570Sbrandon.potter@amd.com
114713570Sbrandon.potter@amd.com    /**
114813570Sbrandon.potter@amd.com     * We cannot allow an infinite poll to occur or it will inevitably cause
114913570Sbrandon.potter@amd.com     * a deadlock in the gem5 simulator with clone. We must pass in tmout with
115013570Sbrandon.potter@amd.com     * a non-negative value, however it also makes no sense to poll on the
115113570Sbrandon.potter@amd.com     * underlying host for any other time than tmout a zero timeout.
115213570Sbrandon.potter@amd.com     */
115313570Sbrandon.potter@amd.com    int status;
115413570Sbrandon.potter@amd.com    if (tmout < 0) {
115513570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
115613570Sbrandon.potter@amd.com        if (status == 0) {
115713570Sbrandon.potter@amd.com            /**
115813570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
115913570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try
116013570Sbrandon.potter@amd.com             * to return the signal interrupt instead.
116113570Sbrandon.potter@amd.com             */
116213570Sbrandon.potter@amd.com            System *sysh = tc->getSystemPtr();
116313570Sbrandon.potter@amd.com            std::list<BasicSignal>::iterator it;
116413570Sbrandon.potter@amd.com            for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++)
116513570Sbrandon.potter@amd.com                if (it->receiver == p)
116613570Sbrandon.potter@amd.com                    return -EINTR;
116713570Sbrandon.potter@amd.com            return SyscallReturn::retry();
116813570Sbrandon.potter@amd.com        }
116913570Sbrandon.potter@amd.com    } else
117013570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
117113570Sbrandon.potter@amd.com
117213570Sbrandon.potter@amd.com    if (status == -1)
117313570Sbrandon.potter@amd.com        return -errno;
117413570Sbrandon.potter@amd.com
117513570Sbrandon.potter@amd.com    /**
117613570Sbrandon.potter@amd.com     * Replace each host_fd in the returned poll_fd array with its original
117713570Sbrandon.potter@amd.com     * target file descriptor.
117813570Sbrandon.potter@amd.com     */
117913570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
118013570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
118113570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd;
118213570Sbrandon.potter@amd.com    }
118313570Sbrandon.potter@amd.com
118413570Sbrandon.potter@amd.com    /**
118513570Sbrandon.potter@amd.com     * Copy out the pollfd struct because the host may have updated fields
118613570Sbrandon.potter@amd.com     * in the structure.
118713570Sbrandon.potter@amd.com     */
118813570Sbrandon.potter@amd.com    fdsBuf.copyOut(tc->getMemProxy());
118913570Sbrandon.potter@amd.com
119013570Sbrandon.potter@amd.com    return status;
119113570Sbrandon.potter@amd.com}
11921999SN/A
11931999SN/A/// Target fchmod() handler.
11941999SN/Atemplate <class OS>
11951999SN/ASyscallReturn
119611856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
11971999SN/A{
11986701Sgblack@eecs.umich.edu    int index = 0;
119911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
120011856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
120110931Sbrandon.potter@amd.com
120211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
120311856Sbrandon.potter@amd.com    if (!ffdp)
12041999SN/A        return -EBADF;
120511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
12061999SN/A
120711856Sbrandon.potter@amd.com    mode_t hostMode = mode;
12081999SN/A
120911856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
12101999SN/A
121111856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
12121999SN/A}
12131999SN/A
12145877Shsul@eecs.umich.edu/// Target mremap() handler.
12155877Shsul@eecs.umich.edutemplate <class OS>
12165877Shsul@eecs.umich.eduSyscallReturn
121711851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
12185877Shsul@eecs.umich.edu{
12196701Sgblack@eecs.umich.edu    int index = 0;
12206701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
12216701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
12226701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
12236701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
122410027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
122510027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
122610027SChris.Adeniyi-Jones@arm.com
122710027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
122810027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
12295877Shsul@eecs.umich.edu
123010318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
123110318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
12325877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
12335877Shsul@eecs.umich.edu        return -EINVAL;
12345877Shsul@eecs.umich.edu    }
12355877Shsul@eecs.umich.edu
123610486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
123710486Stjablin@gmail.com
12385877Shsul@eecs.umich.edu    if (new_length > old_length) {
123911905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
124011905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
124111905SBrandon.Potter@amd.com
124211905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
124310027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
124412206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
124512206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
12465877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
124711905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
124811905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
12495877Shsul@eecs.umich.edu            return start;
12505877Shsul@eecs.umich.edu        } else {
125110027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
12525877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
12535877Shsul@eecs.umich.edu                return -ENOMEM;
12545877Shsul@eecs.umich.edu            } else {
125512206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
125612206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
125712206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
125812206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
125912206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
126012206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
126112206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
126212206Srico.amslinger@informatik.uni-augsburg.de                }
126312206Srico.amslinger@informatik.uni-augsburg.de
126410027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
126510027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
126610027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
126710027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
12685877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
126910027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
127010027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
127110027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
127210027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
127312206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
127412206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
127512206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
127612206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
127710027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
127810027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
127910027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
128010027SChris.Adeniyi-Jones@arm.com                }
128110027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
128210027SChris.Adeniyi-Jones@arm.com                return new_start;
12835877Shsul@eecs.umich.edu            }
12845877Shsul@eecs.umich.edu        }
12855877Shsul@eecs.umich.edu    } else {
128610027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
128710027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
12888601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
128910027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
12905877Shsul@eecs.umich.edu    }
12915877Shsul@eecs.umich.edu}
12921999SN/A
1293378SN/A/// Target stat() handler.
1294360SN/Atemplate <class OS>
12951450SN/ASyscallReturn
129611851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
12972680Sktlim@umich.edu         ThreadContext *tc)
1298360SN/A{
1299360SN/A    std::string path;
1300360SN/A
13016701Sgblack@eecs.umich.edu    int index = 0;
13028852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13036701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13046701Sgblack@eecs.umich.edu        return -EFAULT;
13056701Sgblack@eecs.umich.edu    }
13066701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1307360SN/A
130813883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
130913883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
13103669Sbinkertn@umich.edu
1311360SN/A    struct stat hostBuf;
1312360SN/A    int result = stat(path.c_str(), &hostBuf);
1313360SN/A
1314360SN/A    if (result < 0)
13152218SN/A        return -errno;
1316360SN/A
13178706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1318360SN/A
13191458SN/A    return 0;
1320360SN/A}
1321360SN/A
1322360SN/A
13235074Ssaidi@eecs.umich.edu/// Target stat64() handler.
13245074Ssaidi@eecs.umich.edutemplate <class OS>
13255074Ssaidi@eecs.umich.eduSyscallReturn
132611851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
13275074Ssaidi@eecs.umich.edu           ThreadContext *tc)
13285074Ssaidi@eecs.umich.edu{
13295074Ssaidi@eecs.umich.edu    std::string path;
13305074Ssaidi@eecs.umich.edu
13316701Sgblack@eecs.umich.edu    int index = 0;
13328852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13336701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
13345074Ssaidi@eecs.umich.edu        return -EFAULT;
13356701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
13365074Ssaidi@eecs.umich.edu
133713883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
133813883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
13395074Ssaidi@eecs.umich.edu
13405208Ssaidi@eecs.umich.edu#if NO_STAT64
13415208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
13425208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
13435208Ssaidi@eecs.umich.edu#else
13445074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
13455074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
13465208Ssaidi@eecs.umich.edu#endif
13475074Ssaidi@eecs.umich.edu
13485074Ssaidi@eecs.umich.edu    if (result < 0)
13495074Ssaidi@eecs.umich.edu        return -errno;
13505074Ssaidi@eecs.umich.edu
13518706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
13525074Ssaidi@eecs.umich.edu
13535074Ssaidi@eecs.umich.edu    return 0;
13545074Ssaidi@eecs.umich.edu}
13555074Ssaidi@eecs.umich.edu
13565074Ssaidi@eecs.umich.edu
135710027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
135810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
135910027SChris.Adeniyi-Jones@arm.comSyscallReturn
136011851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
136110027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
136210027SChris.Adeniyi-Jones@arm.com{
136310027SChris.Adeniyi-Jones@arm.com    int index = 0;
136410027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
136510027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
136610793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
136710027SChris.Adeniyi-Jones@arm.com
136810027SChris.Adeniyi-Jones@arm.com    std::string path;
136910027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
137010027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
137110027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
137210027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
137310027SChris.Adeniyi-Jones@arm.com
137413883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
137513883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
137610027SChris.Adeniyi-Jones@arm.com
137710027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
137810027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
137910027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
138010027SChris.Adeniyi-Jones@arm.com#else
138110027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
138210027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
138310027SChris.Adeniyi-Jones@arm.com#endif
138410027SChris.Adeniyi-Jones@arm.com
138510027SChris.Adeniyi-Jones@arm.com    if (result < 0)
138610027SChris.Adeniyi-Jones@arm.com        return -errno;
138710027SChris.Adeniyi-Jones@arm.com
138810027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
138910027SChris.Adeniyi-Jones@arm.com
139010027SChris.Adeniyi-Jones@arm.com    return 0;
139110027SChris.Adeniyi-Jones@arm.com}
139210027SChris.Adeniyi-Jones@arm.com
139310027SChris.Adeniyi-Jones@arm.com
13941999SN/A/// Target fstat64() handler.
13951999SN/Atemplate <class OS>
13961999SN/ASyscallReturn
139711856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
13981999SN/A{
13996701Sgblack@eecs.umich.edu    int index = 0;
140011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
140111856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
140210931Sbrandon.potter@amd.com
140311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
140411856Sbrandon.potter@amd.com    if (!ffdp)
14051999SN/A        return -EBADF;
140611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
14071999SN/A
14082764Sstever@eecs.umich.edu#if NO_STAT64
14092064SN/A    struct stat  hostBuf;
141010931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
14112064SN/A#else
14122064SN/A    struct stat64  hostBuf;
141310931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
14142064SN/A#endif
14151999SN/A
14161999SN/A    if (result < 0)
14172218SN/A        return -errno;
14181999SN/A
141910931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
14201999SN/A
14211999SN/A    return 0;
14221999SN/A}
14231999SN/A
14241999SN/A
1425378SN/A/// Target lstat() handler.
1426360SN/Atemplate <class OS>
14271450SN/ASyscallReturn
142811851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
14292680Sktlim@umich.edu          ThreadContext *tc)
1430360SN/A{
1431360SN/A    std::string path;
1432360SN/A
14336701Sgblack@eecs.umich.edu    int index = 0;
14348852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14356701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14366701Sgblack@eecs.umich.edu        return -EFAULT;
14376701Sgblack@eecs.umich.edu    }
14386701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1439360SN/A
144013883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
144113883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
14423669Sbinkertn@umich.edu
1443360SN/A    struct stat hostBuf;
1444360SN/A    int result = lstat(path.c_str(), &hostBuf);
1445360SN/A
1446360SN/A    if (result < 0)
14471458SN/A        return -errno;
1448360SN/A
14498706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1450360SN/A
14511458SN/A    return 0;
1452360SN/A}
1453360SN/A
14541999SN/A/// Target lstat64() handler.
14551999SN/Atemplate <class OS>
14561999SN/ASyscallReturn
145711851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
14582680Sktlim@umich.edu            ThreadContext *tc)
14591999SN/A{
14601999SN/A    std::string path;
14611999SN/A
14626701Sgblack@eecs.umich.edu    int index = 0;
14638852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14646701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14656701Sgblack@eecs.umich.edu        return -EFAULT;
14666701Sgblack@eecs.umich.edu    }
14676701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
14681999SN/A
146913883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
147013883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
14713669Sbinkertn@umich.edu
14722764Sstever@eecs.umich.edu#if NO_STAT64
14732064SN/A    struct stat hostBuf;
14742064SN/A    int result = lstat(path.c_str(), &hostBuf);
14752064SN/A#else
14761999SN/A    struct stat64 hostBuf;
14771999SN/A    int result = lstat64(path.c_str(), &hostBuf);
14782064SN/A#endif
14791999SN/A
14801999SN/A    if (result < 0)
14811999SN/A        return -errno;
14821999SN/A
14838706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14841999SN/A
14851999SN/A    return 0;
14861999SN/A}
14871999SN/A
1488378SN/A/// Target fstat() handler.
1489360SN/Atemplate <class OS>
14901450SN/ASyscallReturn
149111856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1492360SN/A{
14936701Sgblack@eecs.umich.edu    int index = 0;
149411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
149511856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1496360SN/A
149711380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1498360SN/A
149911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
150011856Sbrandon.potter@amd.com    if (!ffdp)
15011458SN/A        return -EBADF;
150211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1503360SN/A
1504360SN/A    struct stat hostBuf;
150510931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1506360SN/A
1507360SN/A    if (result < 0)
15081458SN/A        return -errno;
1509360SN/A
151010931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
15112021SN/A
15121458SN/A    return 0;
1513360SN/A}
1514360SN/A
15151706SN/A/// Target statfs() handler.
15161706SN/Atemplate <class OS>
15171706SN/ASyscallReturn
151811851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
15192680Sktlim@umich.edu           ThreadContext *tc)
15201706SN/A{
152113936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
15221706SN/A    std::string path;
15231706SN/A
15246701Sgblack@eecs.umich.edu    int index = 0;
15258852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
15266701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
15276701Sgblack@eecs.umich.edu        return -EFAULT;
15286701Sgblack@eecs.umich.edu    }
15296701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
15301706SN/A
153113883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
153213883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
15333669Sbinkertn@umich.edu
15341706SN/A    struct statfs hostBuf;
15351706SN/A    int result = statfs(path.c_str(), &hostBuf);
15361706SN/A
15371706SN/A    if (result < 0)
15382218SN/A        return -errno;
15391706SN/A
154011759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
154113933Sbrandon.potter@amd.com    return 0;
154213933Sbrandon.potter@amd.com#else
154313933Sbrandon.potter@amd.com    warnUnsupportedOS("statfs");
154413933Sbrandon.potter@amd.com    return -1;
154511799Sbrandon.potter@amd.com#endif
15461706SN/A}
15471706SN/A
154811886Sbrandon.potter@amd.comtemplate <class OS>
154911886Sbrandon.potter@amd.comSyscallReturn
155011886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
155111886Sbrandon.potter@amd.com{
155211886Sbrandon.potter@amd.com    int index = 0;
155312426Sqtt2@cornell.edu
155413557Sgabeblack@google.com    RegVal flags = p->getSyscallArg(tc, index);
155513557Sgabeblack@google.com    RegVal newStack = p->getSyscallArg(tc, index);
155611886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
155712426Sqtt2@cornell.edu
155813534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
155912426Sqtt2@cornell.edu    /**
156013534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
156112426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
156212426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
156312426Sqtt2@cornell.edu     */
156413536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
156512426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
156612426Sqtt2@cornell.edu#else
156711886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
156813536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
156912426Sqtt2@cornell.edu#endif
157011886Sbrandon.potter@amd.com
157111886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
157211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
157311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
157411886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
157511886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
157611886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
157711886Sbrandon.potter@amd.com        return -EINVAL;
157811886Sbrandon.potter@amd.com
157911886Sbrandon.potter@amd.com    ThreadContext *ctc;
158013649Sqtt2@cornell.edu    if (!(ctc = p->findFreeContext())) {
158113649Sqtt2@cornell.edu        DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system"
158213649Sqtt2@cornell.edu                        "[cpu %d, thread %d]", tc->cpuId(), tc->threadId());
158313649Sqtt2@cornell.edu        return -EAGAIN;
158413649Sqtt2@cornell.edu    }
158511886Sbrandon.potter@amd.com
158611886Sbrandon.potter@amd.com    /**
158711886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
158811886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
158911886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
159011886Sbrandon.potter@amd.com     * constructor.
159111886Sbrandon.potter@amd.com     */
159211886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
159311886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
159411886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
159511886Sbrandon.potter@amd.com    pp->system = p->system;
159613883Sdavid.hashe@amd.com    pp->cwd.assign(p->tgtCwd);
159711886Sbrandon.potter@amd.com    pp->input.assign("stdin");
159811886Sbrandon.potter@amd.com    pp->output.assign("stdout");
159911886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
160011886Sbrandon.potter@amd.com    pp->uid = p->uid();
160111886Sbrandon.potter@amd.com    pp->euid = p->euid();
160211886Sbrandon.potter@amd.com    pp->gid = p->gid();
160311886Sbrandon.potter@amd.com    pp->egid = p->egid();
160411886Sbrandon.potter@amd.com
160511886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
160611886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
160711886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
160811886Sbrandon.potter@amd.com    do {
160911886Sbrandon.potter@amd.com        temp_pid++;
161011886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
161111886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
161211886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
161311886Sbrandon.potter@amd.com
161411886Sbrandon.potter@amd.com    pp->pid = temp_pid;
161511886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
161613867Salexandru.dutu@amd.com    pp->useArchPT = p->useArchPT;
161713867Salexandru.dutu@amd.com    pp->kvmInSE = p->kvmInSE;
161811886Sbrandon.potter@amd.com    Process *cp = pp->create();
161911886Sbrandon.potter@amd.com    delete pp;
162011886Sbrandon.potter@amd.com
162111886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
162211886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
162311886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
162411886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
162511886Sbrandon.potter@amd.com
162611886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
162711886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
162811886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
162911886Sbrandon.potter@amd.com        *ptid = cp->pid();
163011886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
163111886Sbrandon.potter@amd.com    }
163211886Sbrandon.potter@amd.com
163313867Salexandru.dutu@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
163413867Salexandru.dutu@amd.com        cp->pTable->shared = true;
163513867Salexandru.dutu@amd.com        cp->useForClone = true;
163613867Salexandru.dutu@amd.com    }
163711886Sbrandon.potter@amd.com    cp->initState();
163811886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
163911886Sbrandon.potter@amd.com
164011911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
164111911SBrandon.Potter@amd.com        delete cp->sigchld;
164211911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
164311911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
164411911SBrandon.Potter@amd.com        *cp->sigchld = true;
164511911SBrandon.Potter@amd.com    }
164611911SBrandon.Potter@amd.com
164711886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
164811886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
164911886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
165011886Sbrandon.potter@amd.com        *ctid = cp->pid();
165111886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
165211886Sbrandon.potter@amd.com    }
165311886Sbrandon.potter@amd.com
165411886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
165511886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
165611886Sbrandon.potter@amd.com
165711886Sbrandon.potter@amd.com    ctc->clearArchRegs();
165811886Sbrandon.potter@amd.com
165913536Sandreas.sandberg@arm.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
166011886Sbrandon.potter@amd.com
166111886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
166211886Sbrandon.potter@amd.com
166311886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
166411886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
166511886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
166611886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
166711886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
166811886Sbrandon.potter@amd.com#endif
166911886Sbrandon.potter@amd.com
167013867Salexandru.dutu@amd.com    if (p->kvmInSE) {
167113867Salexandru.dutu@amd.com#if THE_ISA == X86_ISA
167213867Salexandru.dutu@amd.com        ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX));
167313867Salexandru.dutu@amd.com#else
167413867Salexandru.dutu@amd.com        panic("KVM CPU model is not supported for this ISA");
167513867Salexandru.dutu@amd.com#endif
167613867Salexandru.dutu@amd.com    } else {
167713867Salexandru.dutu@amd.com        TheISA::PCState cpc = tc->pcState();
167813867Salexandru.dutu@amd.com        cpc.advance();
167913867Salexandru.dutu@amd.com        ctc->pcState(cpc);
168013867Salexandru.dutu@amd.com    }
168111886Sbrandon.potter@amd.com    ctc->activate();
168211886Sbrandon.potter@amd.com
168311886Sbrandon.potter@amd.com    return cp->pid();
168411886Sbrandon.potter@amd.com}
16851706SN/A
16861706SN/A/// Target fstatfs() handler.
16871706SN/Atemplate <class OS>
16881706SN/ASyscallReturn
168911856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
16901706SN/A{
16916701Sgblack@eecs.umich.edu    int index = 0;
169211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
169311856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
16941706SN/A
169511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
169611856Sbrandon.potter@amd.com    if (!ffdp)
16971706SN/A        return -EBADF;
169811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
16991706SN/A
17001706SN/A    struct statfs hostBuf;
170110931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
17021706SN/A
17031706SN/A    if (result < 0)
17042218SN/A        return -errno;
17051706SN/A
170611759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
17071706SN/A
17081706SN/A    return 0;
17091706SN/A}
17101706SN/A
171113572Sbrandon.potter@amd.com/// Target readv() handler.
171213572Sbrandon.potter@amd.comtemplate <class OS>
171313572Sbrandon.potter@amd.comSyscallReturn
171413572Sbrandon.potter@amd.comreadvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
171513572Sbrandon.potter@amd.com{
171613572Sbrandon.potter@amd.com    int index = 0;
171713572Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
171813572Sbrandon.potter@amd.com
171913572Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
172013572Sbrandon.potter@amd.com    if (!ffdp)
172113572Sbrandon.potter@amd.com        return -EBADF;
172213572Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
172313572Sbrandon.potter@amd.com
172413572Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
172513572Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
172613572Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
172713572Sbrandon.potter@amd.com    typename OS::tgt_iovec tiov[count];
172813572Sbrandon.potter@amd.com    struct iovec hiov[count];
172913572Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
173013572Sbrandon.potter@amd.com        prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)),
173113572Sbrandon.potter@amd.com                      (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec));
173213572Sbrandon.potter@amd.com        hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len);
173313572Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
173413572Sbrandon.potter@amd.com    }
173513572Sbrandon.potter@amd.com
173613572Sbrandon.potter@amd.com    int result = readv(sim_fd, hiov, count);
173713572Sbrandon.potter@amd.com    int local_errno = errno;
173813572Sbrandon.potter@amd.com
173913572Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
174013572Sbrandon.potter@amd.com        if (result != -1) {
174113572Sbrandon.potter@amd.com            prox.writeBlob(TheISA::htog(tiov[i].iov_base),
174213572Sbrandon.potter@amd.com                           (uint8_t*)hiov[i].iov_base, hiov[i].iov_len);
174313572Sbrandon.potter@amd.com        }
174413572Sbrandon.potter@amd.com        delete [] (char *)hiov[i].iov_base;
174513572Sbrandon.potter@amd.com    }
174613572Sbrandon.potter@amd.com
174713572Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : result;
174813572Sbrandon.potter@amd.com}
17491706SN/A
17501999SN/A/// Target writev() handler.
17511999SN/Atemplate <class OS>
17521999SN/ASyscallReturn
175311856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
17541999SN/A{
17556701Sgblack@eecs.umich.edu    int index = 0;
175611856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
175710931Sbrandon.potter@amd.com
175811856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
175911856Sbrandon.potter@amd.com    if (!hbfdp)
17601999SN/A        return -EBADF;
176111856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
17621999SN/A
176311856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
176411856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
176511856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
17661999SN/A    struct iovec hiov[count];
17676227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
17681999SN/A        typename OS::tgt_iovec tiov;
17692461SN/A
177011856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
177111856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
17728737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
17731999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
177411856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
177511856Sbrandon.potter@amd.com                      hiov[i].iov_len);
17761999SN/A    }
17771999SN/A
177810931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
17791999SN/A
17806227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
17811999SN/A        delete [] (char *)hiov[i].iov_base;
17821999SN/A
178313572Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
17841999SN/A}
17851999SN/A
178611385Sbrandon.potter@amd.com/// Real mmap handler.
1787360SN/Atemplate <class OS>
17881450SN/ASyscallReturn
178911851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
179011385Sbrandon.potter@amd.com         bool is_mmap2)
1791360SN/A{
17926701Sgblack@eecs.umich.edu    int index = 0;
17936701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
17946701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
179511383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
179611383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
17978324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
179810486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1799360SN/A
180011385Sbrandon.potter@amd.com    if (is_mmap2)
180111385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
18029008Sgblack@eecs.umich.edu
180311383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
180411383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
180511383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
180611383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
180711383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
180811383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
180911383Sbrandon.potter@amd.com        !length) {
181011383Sbrandon.potter@amd.com        return -EINVAL;
181111383Sbrandon.potter@amd.com    }
18128324Ssteve.reinhardt@amd.com
181311383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
181411383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
181511383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
181611383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
181711383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
181811383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
181911383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
182011383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
182111383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
182211383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
182311383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
182411383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
182511383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
182611383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
182711383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
182811383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
182911383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
183011383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
183111383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
183211383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
183311383Sbrandon.potter@amd.com        // than we currently maintain.
183411383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
183511383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
183611383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
18378324Ssteve.reinhardt@amd.com    }
18385877Shsul@eecs.umich.edu
183910486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
184010486Stjablin@gmail.com
184111383Sbrandon.potter@amd.com    int sim_fd = -1;
184211383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
184311383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
184411856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
184511624Smichael.lebeane@amd.com
184611856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
184711856Sbrandon.potter@amd.com        if (dfdp) {
184811856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
184911856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
185011624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
185111624Smichael.lebeane@amd.com        }
185211624Smichael.lebeane@amd.com
185311856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
185411856Sbrandon.potter@amd.com        if (!ffdp)
185511383Sbrandon.potter@amd.com            return -EBADF;
185611856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1857360SN/A
185811913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
185911383Sbrandon.potter@amd.com                                    sim_fd, offset);
18608600Ssteve.reinhardt@amd.com
186111383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
186211383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
186311383Sbrandon.potter@amd.com            return -errno;
18648600Ssteve.reinhardt@amd.com        }
18652544SN/A    }
18662544SN/A
186711383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
186811383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
186911383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
187011905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
187111905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
187211905SBrandon.Potter@amd.com
187311905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
187411905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
187511905SBrandon.Potter@amd.com
187611905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
187711383Sbrandon.potter@amd.com    }
187811383Sbrandon.potter@amd.com
187911383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
188011383Sbrandon.potter@amd.com                    start, start + length - 1);
188111383Sbrandon.potter@amd.com
188211383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
188311383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
188411383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
188511383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
188611383Sbrandon.potter@amd.com    if (clobber) {
188711383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
188811383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
188911383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
189011383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
189111383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
18928600Ssteve.reinhardt@amd.com        }
18936672Sgblack@eecs.umich.edu    }
18948600Ssteve.reinhardt@amd.com
189511383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
189611383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
189711383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
18988601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
18992544SN/A
190011383Sbrandon.potter@amd.com    // Transfer content into target address space.
190111383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
190211383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
190311383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
190411383Sbrandon.potter@amd.com        // with something like:
190511383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
190611383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
190711383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
190811383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
190911383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
191011383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
191111383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
191211383Sbrandon.potter@amd.com    } else {
191311383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
191411383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
191511383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
191611383Sbrandon.potter@amd.com        // the host into the target address space.
191711383Sbrandon.potter@amd.com        struct stat file_stat;
191811383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
191911383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
192011383Sbrandon.potter@amd.com
192111383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
192211383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
192311383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
192411383Sbrandon.potter@amd.com        // on the specified offset into the file.
192511383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
192611383Sbrandon.potter@amd.com                                 length);
192711383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
192811383Sbrandon.potter@amd.com
192911383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
193011383Sbrandon.potter@amd.com        munmap(pmap, length);
193111383Sbrandon.potter@amd.com
193211392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
193311392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
193411392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
193511392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
193611392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
193711392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
193811392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
193911392Sbrandon.potter@amd.com        if (interpreter) {
194011392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
194111392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
194211392Sbrandon.potter@amd.com
194311392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
194411392Sbrandon.potter@amd.com
194511392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
194611856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
194711856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
194811856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
194911392Sbrandon.potter@amd.com
195011392Sbrandon.potter@amd.com                if (lib) {
195111392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
195211392Sbrandon.potter@amd.com                                        lib->textBase(), start);
195311392Sbrandon.potter@amd.com                }
195411392Sbrandon.potter@amd.com            }
195511392Sbrandon.potter@amd.com        }
195611392Sbrandon.potter@amd.com
195711383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
195811383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
195911383Sbrandon.potter@amd.com        // execution (hopefully).
196011383Sbrandon.potter@amd.com    }
196111383Sbrandon.potter@amd.com
19621458SN/A    return start;
1963360SN/A}
1964360SN/A
196511593Santhony.gutierrez@amd.comtemplate <class OS>
196611593Santhony.gutierrez@amd.comSyscallReturn
196711851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
196811593Santhony.gutierrez@amd.com{
196911593Santhony.gutierrez@amd.com    int index = 0;
197011593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
197111593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
197211593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
197311593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
197411593Santhony.gutierrez@amd.com
197511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
197611856Sbrandon.potter@amd.com    if (!ffdp)
197711593Santhony.gutierrez@amd.com        return -EBADF;
197811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
197911593Santhony.gutierrez@amd.com
198011593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
198111593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
198211593Santhony.gutierrez@amd.com
198311594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
198411593Santhony.gutierrez@amd.com
198511593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
198611593Santhony.gutierrez@amd.com}
198711593Santhony.gutierrez@amd.com
198811385Sbrandon.potter@amd.com/// Target mmap() handler.
198911385Sbrandon.potter@amd.comtemplate <class OS>
199011385Sbrandon.potter@amd.comSyscallReturn
199111851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
199211385Sbrandon.potter@amd.com{
199311385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
199411385Sbrandon.potter@amd.com}
199511385Sbrandon.potter@amd.com
199611385Sbrandon.potter@amd.com/// Target mmap2() handler.
199711385Sbrandon.potter@amd.comtemplate <class OS>
199811385Sbrandon.potter@amd.comSyscallReturn
199911851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
200011385Sbrandon.potter@amd.com{
200111385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
200211385Sbrandon.potter@amd.com}
200311385Sbrandon.potter@amd.com
2004378SN/A/// Target getrlimit() handler.
2005360SN/Atemplate <class OS>
20061450SN/ASyscallReturn
200711851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
200811851Sbrandon.potter@amd.com              ThreadContext *tc)
2009360SN/A{
20106701Sgblack@eecs.umich.edu    int index = 0;
20116701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
20126701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
2013360SN/A
2014360SN/A    switch (resource) {
201511906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
201611906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
201711906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
201811906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
201911906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
202011906SBrandon.Potter@amd.com        break;
2021360SN/A
202211906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
202311906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
202411906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
202511906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
202611906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
202711906SBrandon.Potter@amd.com        break;
20285877Shsul@eecs.umich.edu
202911906SBrandon.Potter@amd.com      default:
203011906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
203111906SBrandon.Potter@amd.com        return -EINVAL;
203211906SBrandon.Potter@amd.com        break;
2033360SN/A    }
2034360SN/A
20358706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
20361458SN/A    return 0;
2037360SN/A}
2038360SN/A
203912235Sar4jc@virginia.edutemplate <class OS>
204012235Sar4jc@virginia.eduSyscallReturn
204112235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
204212235Sar4jc@virginia.edu            ThreadContext *tc)
204312235Sar4jc@virginia.edu{
204412235Sar4jc@virginia.edu    int index = 0;
204512235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
204612235Sar4jc@virginia.edu    {
204712235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
204812235Sar4jc@virginia.edu        return -EPERM;
204912235Sar4jc@virginia.edu    }
205012235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
205112235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
205212235Sar4jc@virginia.edu    if (n != 0)
205312235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
205412235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
205512235Sar4jc@virginia.edu    if (o != 0)
205612235Sar4jc@virginia.edu    {
205712416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
205812235Sar4jc@virginia.edu        switch (resource) {
205912235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
206012235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
206112235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
206212235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
206312235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
206412235Sar4jc@virginia.edu            break;
206512235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
206612235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
206712235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
206812235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
206912235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
207012593Sjason@lowepower.com            break;
207112235Sar4jc@virginia.edu          default:
207212235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
207312235Sar4jc@virginia.edu            return -EINVAL;
207412235Sar4jc@virginia.edu            break;
207512235Sar4jc@virginia.edu        }
207612235Sar4jc@virginia.edu        rlp.copyOut(tc->getMemProxy());
207712235Sar4jc@virginia.edu    }
207812235Sar4jc@virginia.edu    return 0;
207912235Sar4jc@virginia.edu}
208012235Sar4jc@virginia.edu
208110796Sbrandon.potter@amd.com/// Target clock_gettime() function.
208210796Sbrandon.potter@amd.comtemplate <class OS>
208310796Sbrandon.potter@amd.comSyscallReturn
208411851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
208510796Sbrandon.potter@amd.com{
208610796Sbrandon.potter@amd.com    int index = 1;
208710796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
208810796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
208910796Sbrandon.potter@amd.com
209010796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
209110796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
209210796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
209310796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
209410796Sbrandon.potter@amd.com
209510796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
209610796Sbrandon.potter@amd.com
209710796Sbrandon.potter@amd.com    return 0;
209810796Sbrandon.potter@amd.com}
209910796Sbrandon.potter@amd.com
210011337SMichael.Lebeane@amd.com/// Target clock_getres() function.
210111337SMichael.Lebeane@amd.comtemplate <class OS>
210211337SMichael.Lebeane@amd.comSyscallReturn
210311851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
210411337SMichael.Lebeane@amd.com{
210511337SMichael.Lebeane@amd.com    int index = 1;
210611337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
210711337SMichael.Lebeane@amd.com
210811337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
210911337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
211011337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
211111337SMichael.Lebeane@amd.com
211211337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
211311337SMichael.Lebeane@amd.com
211411337SMichael.Lebeane@amd.com    return 0;
211511337SMichael.Lebeane@amd.com}
211611337SMichael.Lebeane@amd.com
2117378SN/A/// Target gettimeofday() handler.
2118360SN/Atemplate <class OS>
21191450SN/ASyscallReturn
212011851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
212111851Sbrandon.potter@amd.com                 ThreadContext *tc)
2122360SN/A{
21236701Sgblack@eecs.umich.edu    int index = 0;
21246701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
2125360SN/A
212610796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
2127360SN/A    tp->tv_sec += seconds_since_epoch;
21286109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
21296109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
2130360SN/A
21318706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
2132360SN/A
21331458SN/A    return 0;
2134360SN/A}
2135360SN/A
2136360SN/A
21371999SN/A/// Target utimes() handler.
21381999SN/Atemplate <class OS>
21391999SN/ASyscallReturn
214011851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
21412680Sktlim@umich.edu           ThreadContext *tc)
21421999SN/A{
21431999SN/A    std::string path;
21441999SN/A
21456701Sgblack@eecs.umich.edu    int index = 0;
21468852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
21476701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
21486701Sgblack@eecs.umich.edu        return -EFAULT;
21496701Sgblack@eecs.umich.edu    }
21501999SN/A
21516701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
21526701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
21538706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
21541999SN/A
21551999SN/A    struct timeval hostTimeval[2];
215611906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
21578737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
21588737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
21591999SN/A    }
21603669Sbinkertn@umich.edu
216113883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
216213883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
21633669Sbinkertn@umich.edu
21641999SN/A    int result = utimes(path.c_str(), hostTimeval);
21651999SN/A
21661999SN/A    if (result < 0)
21671999SN/A        return -errno;
21681999SN/A
21691999SN/A    return 0;
21701999SN/A}
217111886Sbrandon.potter@amd.com
217211886Sbrandon.potter@amd.comtemplate <class OS>
217311886Sbrandon.potter@amd.comSyscallReturn
217411886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
217511886Sbrandon.potter@amd.com{
217611886Sbrandon.potter@amd.com    desc->setFlags(0);
217711886Sbrandon.potter@amd.com
217811886Sbrandon.potter@amd.com    int index = 0;
217911886Sbrandon.potter@amd.com    std::string path;
218011886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
218111886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
218211886Sbrandon.potter@amd.com        return -EFAULT;
218311886Sbrandon.potter@amd.com
218411886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
218511886Sbrandon.potter@amd.com        return -EACCES;
218611886Sbrandon.potter@amd.com
218711886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
218811886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
218911886Sbrandon.potter@amd.com                      Addr mem_loc)
219011886Sbrandon.potter@amd.com    {
219111886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
219211886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
219311886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
219411886Sbrandon.potter@amd.com
219511886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
219611886Sbrandon.potter@amd.com                break;
219711886Sbrandon.potter@amd.com
219811886Sbrandon.potter@amd.com            vect.push_back(std::string());
219911886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
220011886Sbrandon.potter@amd.com        }
220111886Sbrandon.potter@amd.com    };
220211886Sbrandon.potter@amd.com
220311886Sbrandon.potter@amd.com    /**
220411886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
220511886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
220611886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
220711886Sbrandon.potter@amd.com     * constructor.
220811886Sbrandon.potter@amd.com     */
220911886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
221011886Sbrandon.potter@amd.com    pp->executable = path;
221111886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
221211886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
221311886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
221411886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
221511886Sbrandon.potter@amd.com    pp->uid = p->uid();
221611886Sbrandon.potter@amd.com    pp->egid = p->egid();
221711886Sbrandon.potter@amd.com    pp->euid = p->euid();
221811886Sbrandon.potter@amd.com    pp->gid = p->gid();
221911886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
222011886Sbrandon.potter@amd.com    pp->pid = p->pid();
222111886Sbrandon.potter@amd.com    pp->input.assign("cin");
222211886Sbrandon.potter@amd.com    pp->output.assign("cout");
222311886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
222413883Sdavid.hashe@amd.com    pp->cwd.assign(p->tgtCwd);
222511886Sbrandon.potter@amd.com    pp->system = p->system;
222611886Sbrandon.potter@amd.com    /**
222711886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
222811886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
222911886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
223011886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
223111886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
223211886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
223311886Sbrandon.potter@amd.com     */
223411886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
223511886Sbrandon.potter@amd.com    Process *new_p = pp->create();
223611886Sbrandon.potter@amd.com    delete pp;
223711886Sbrandon.potter@amd.com
223811886Sbrandon.potter@amd.com    /**
223911886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
224011886Sbrandon.potter@amd.com     * close-on-exec.
224111886Sbrandon.potter@amd.com     */
224211886Sbrandon.potter@amd.com    new_p->fds = p->fds;
224311886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
224411886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
224511886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
224611886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
224711886Sbrandon.potter@amd.com    }
224811886Sbrandon.potter@amd.com
224911886Sbrandon.potter@amd.com    *new_p->sigchld = true;
225011886Sbrandon.potter@amd.com
225111886Sbrandon.potter@amd.com    delete p;
225211886Sbrandon.potter@amd.com    tc->clearArchRegs();
225311886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
225411886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
225511886Sbrandon.potter@amd.com    new_p->initState();
225611886Sbrandon.potter@amd.com    tc->activate();
225711886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
225811886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
225911886Sbrandon.potter@amd.com
226011886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
226111886Sbrandon.potter@amd.com    return 0;
226211886Sbrandon.potter@amd.com}
226311886Sbrandon.potter@amd.com
2264378SN/A/// Target getrusage() function.
2265360SN/Atemplate <class OS>
22661450SN/ASyscallReturn
226711851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
22682680Sktlim@umich.edu              ThreadContext *tc)
2269360SN/A{
22706701Sgblack@eecs.umich.edu    int index = 0;
22716701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
22726701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
2273360SN/A
22743670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
22753670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
2276360SN/A    rup->ru_stime.tv_sec = 0;
2277360SN/A    rup->ru_stime.tv_usec = 0;
2278360SN/A    rup->ru_maxrss = 0;
2279360SN/A    rup->ru_ixrss = 0;
2280360SN/A    rup->ru_idrss = 0;
2281360SN/A    rup->ru_isrss = 0;
2282360SN/A    rup->ru_minflt = 0;
2283360SN/A    rup->ru_majflt = 0;
2284360SN/A    rup->ru_nswap = 0;
2285360SN/A    rup->ru_inblock = 0;
2286360SN/A    rup->ru_oublock = 0;
2287360SN/A    rup->ru_msgsnd = 0;
2288360SN/A    rup->ru_msgrcv = 0;
2289360SN/A    rup->ru_nsignals = 0;
2290360SN/A    rup->ru_nvcsw = 0;
2291360SN/A    rup->ru_nivcsw = 0;
2292360SN/A
22933670Sbinkertn@umich.edu    switch (who) {
22943670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
229510796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
22968737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
22978737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
22983670Sbinkertn@umich.edu        break;
22993670Sbinkertn@umich.edu
23003670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
23013670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
23023670Sbinkertn@umich.edu        break;
23033670Sbinkertn@umich.edu
23043670Sbinkertn@umich.edu      default:
23053670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
23063670Sbinkertn@umich.edu        // plow ahead
23073670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
23083670Sbinkertn@umich.edu             who);
23093670Sbinkertn@umich.edu    }
23103670Sbinkertn@umich.edu
23118706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
2312360SN/A
23131458SN/A    return 0;
2314360SN/A}
2315360SN/A
23166683Stjones1@inf.ed.ac.uk/// Target times() function.
23176683Stjones1@inf.ed.ac.uktemplate <class OS>
23186683Stjones1@inf.ed.ac.ukSyscallReturn
231911851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
232011851Sbrandon.potter@amd.com          ThreadContext *tc)
23216683Stjones1@inf.ed.ac.uk{
23226701Sgblack@eecs.umich.edu    int index = 0;
23236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
23246683Stjones1@inf.ed.ac.uk
23256683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
23267823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
23276683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
23286683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
23296683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
23306683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
23316683Stjones1@inf.ed.ac.uk
23326683Stjones1@inf.ed.ac.uk    // Convert to host endianness
23338737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
23346683Stjones1@inf.ed.ac.uk
23356683Stjones1@inf.ed.ac.uk    // Write back
23368706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
23376683Stjones1@inf.ed.ac.uk
23386683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
23396683Stjones1@inf.ed.ac.uk    return clocks;
23406683Stjones1@inf.ed.ac.uk}
23412553SN/A
23426684Stjones1@inf.ed.ac.uk/// Target time() function.
23436684Stjones1@inf.ed.ac.uktemplate <class OS>
23446684Stjones1@inf.ed.ac.ukSyscallReturn
234511851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
23466684Stjones1@inf.ed.ac.uk{
23476684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
234810796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
23496684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
23506684Stjones1@inf.ed.ac.uk
23516701Sgblack@eecs.umich.edu    int index = 0;
23526701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
235311321Ssteve.reinhardt@amd.com    if (taddr != 0) {
23546684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
23558737Skoansin.tan@gmail.com        t = TheISA::htog(t);
23568852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
23578852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
23586684Stjones1@inf.ed.ac.uk    }
23596684Stjones1@inf.ed.ac.uk    return sec;
23606684Stjones1@inf.ed.ac.uk}
23612553SN/A
236211910SBrandon.Potter@amd.comtemplate <class OS>
236311910SBrandon.Potter@amd.comSyscallReturn
236411910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
236511910SBrandon.Potter@amd.com{
236611910SBrandon.Potter@amd.com    int index = 0;
236711910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
236811910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
236911910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
237011910SBrandon.Potter@amd.com
237111910SBrandon.Potter@amd.com    /**
237211910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
237311910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
237411910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
237511910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
237611910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
237711910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
237811910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
237911910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
238011910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
238111910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
238211910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
238311910SBrandon.Potter@amd.com     * due to PID rollover.
238411910SBrandon.Potter@amd.com     */
238511910SBrandon.Potter@amd.com
238611910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
238711910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
238811910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
238911910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
239011910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
239111910SBrandon.Potter@amd.com            tgt_proc = temp;
239211910SBrandon.Potter@amd.com            break;
239311910SBrandon.Potter@amd.com        }
239411910SBrandon.Potter@amd.com    }
239511910SBrandon.Potter@amd.com
239611910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
239711910SBrandon.Potter@amd.com        return -EINVAL;
239811910SBrandon.Potter@amd.com
239911910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
240011910SBrandon.Potter@amd.com        return -ESRCH;
240111910SBrandon.Potter@amd.com
240211910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
240311910SBrandon.Potter@amd.com        return -ESRCH;
240411910SBrandon.Potter@amd.com
240511910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
240611910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
240711910SBrandon.Potter@amd.com
240811910SBrandon.Potter@amd.com    return 0;
240911910SBrandon.Potter@amd.com}
241011910SBrandon.Potter@amd.com
241113568Sbrandon.potter@amd.comtemplate <class OS>
241213568Sbrandon.potter@amd.comSyscallReturn
241313568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
241413568Sbrandon.potter@amd.com{
241513568Sbrandon.potter@amd.com    int index = 0;
241613568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
241713568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
241813568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
241913568Sbrandon.potter@amd.com
242013568Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
242113568Sbrandon.potter@amd.com    if (sim_fd == -1)
242213568Sbrandon.potter@amd.com        return -errno;
242313568Sbrandon.potter@amd.com
242413568Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
242513568Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
242613568Sbrandon.potter@amd.com
242713568Sbrandon.potter@amd.com    return tgt_fd;
242813568Sbrandon.potter@amd.com}
242913568Sbrandon.potter@amd.com
243013568Sbrandon.potter@amd.comtemplate <class OS>
243113568Sbrandon.potter@amd.comSyscallReturn
243213568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
243313568Sbrandon.potter@amd.com{
243413568Sbrandon.potter@amd.com    int index = 0;
243513568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
243613568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
243713568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
243813568Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
243913568Sbrandon.potter@amd.com
244013568Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
244113568Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
244213568Sbrandon.potter@amd.com    if (status == -1)
244313568Sbrandon.potter@amd.com        return -errno;
244413568Sbrandon.potter@amd.com
244513568Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
244613568Sbrandon.potter@amd.com
244713568Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
244813568Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
244913568Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
245013568Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
245113568Sbrandon.potter@amd.com    svBuf.copyOut(tc->getMemProxy());
245213568Sbrandon.potter@amd.com
245313568Sbrandon.potter@amd.com    return status;
245413568Sbrandon.potter@amd.com}
24552553SN/A
245613570Sbrandon.potter@amd.comtemplate <class OS>
245713570Sbrandon.potter@amd.comSyscallReturn
245813570Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
245913570Sbrandon.potter@amd.com{
246013570Sbrandon.potter@amd.com    int retval;
246113570Sbrandon.potter@amd.com
246213570Sbrandon.potter@amd.com    int index = 0;
246313570Sbrandon.potter@amd.com    int nfds_t = p->getSyscallArg(tc, index);
246413570Sbrandon.potter@amd.com    Addr fds_read_ptr = p->getSyscallArg(tc, index);
246513570Sbrandon.potter@amd.com    Addr fds_writ_ptr = p->getSyscallArg(tc, index);
246613570Sbrandon.potter@amd.com    Addr fds_excp_ptr = p->getSyscallArg(tc, index);
246713570Sbrandon.potter@amd.com    Addr time_val_ptr = p->getSyscallArg(tc, index);
246813570Sbrandon.potter@amd.com
246913570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr);
247013570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr);
247113570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr);
247213570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(time_val_ptr);
247313570Sbrandon.potter@amd.com
247413570Sbrandon.potter@amd.com    /**
247513570Sbrandon.potter@amd.com     * Host fields. Notice that these use the definitions from the system
247613570Sbrandon.potter@amd.com     * headers instead of the gem5 headers and libraries. If the host and
247713570Sbrandon.potter@amd.com     * target have different header file definitions, this will not work.
247813570Sbrandon.potter@amd.com     */
247913570Sbrandon.potter@amd.com    fd_set rd_h;
248013570Sbrandon.potter@amd.com    FD_ZERO(&rd_h);
248113570Sbrandon.potter@amd.com    fd_set wr_h;
248213570Sbrandon.potter@amd.com    FD_ZERO(&wr_h);
248313570Sbrandon.potter@amd.com    fd_set ex_h;
248413570Sbrandon.potter@amd.com    FD_ZERO(&ex_h);
248513570Sbrandon.potter@amd.com
248613570Sbrandon.potter@amd.com    /**
248713570Sbrandon.potter@amd.com     * Copy in the fd_set from the target.
248813570Sbrandon.potter@amd.com     */
248913570Sbrandon.potter@amd.com    if (fds_read_ptr)
249013570Sbrandon.potter@amd.com        rd_t.copyIn(tc->getMemProxy());
249113570Sbrandon.potter@amd.com    if (fds_writ_ptr)
249213570Sbrandon.potter@amd.com        wr_t.copyIn(tc->getMemProxy());
249313570Sbrandon.potter@amd.com    if (fds_excp_ptr)
249413570Sbrandon.potter@amd.com        ex_t.copyIn(tc->getMemProxy());
249513570Sbrandon.potter@amd.com
249613570Sbrandon.potter@amd.com    /**
249713570Sbrandon.potter@amd.com     * We need to translate the target file descriptor set into a host file
249813570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
249913570Sbrandon.potter@amd.com     * and the fd_set defined in Linux header files. The nfds field also
250013570Sbrandon.potter@amd.com     * needs to be updated as it will be only target specific after
250113570Sbrandon.potter@amd.com     * retrieving it from the target; the nfds value is expected to be the
250213570Sbrandon.potter@amd.com     * highest file descriptor that needs to be checked, so we need to extend
250313570Sbrandon.potter@amd.com     * it out for nfds_h when we do the update.
250413570Sbrandon.potter@amd.com     */
250513570Sbrandon.potter@amd.com    int nfds_h = 0;
250613570Sbrandon.potter@amd.com    std::map<int, int> trans_map;
250713570Sbrandon.potter@amd.com    auto try_add_host_set = [&](fd_set *tgt_set_entry,
250813570Sbrandon.potter@amd.com                                fd_set *hst_set_entry,
250913570Sbrandon.potter@amd.com                                int iter) -> bool
251013570Sbrandon.potter@amd.com    {
251113570Sbrandon.potter@amd.com        /**
251213570Sbrandon.potter@amd.com         * By this point, we know that we are looking at a valid file
251313570Sbrandon.potter@amd.com         * descriptor set on the target. We need to check if the target file
251413570Sbrandon.potter@amd.com         * descriptor value passed in as iter is part of the set.
251513570Sbrandon.potter@amd.com         */
251613570Sbrandon.potter@amd.com        if (FD_ISSET(iter, tgt_set_entry)) {
251713570Sbrandon.potter@amd.com            /**
251813570Sbrandon.potter@amd.com             * We know that the target file descriptor belongs to the set,
251913570Sbrandon.potter@amd.com             * but we do not yet know if the file descriptor is valid or
252013570Sbrandon.potter@amd.com             * that we have a host mapping. Check that now.
252113570Sbrandon.potter@amd.com             */
252213570Sbrandon.potter@amd.com            auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]);
252313570Sbrandon.potter@amd.com            if (!hbfdp)
252413570Sbrandon.potter@amd.com                return true;
252513570Sbrandon.potter@amd.com            auto sim_fd = hbfdp->getSimFD();
252613570Sbrandon.potter@amd.com
252713570Sbrandon.potter@amd.com            /**
252813570Sbrandon.potter@amd.com             * Add the sim_fd to tgt_fd translation into trans_map for use
252913570Sbrandon.potter@amd.com             * later when we need to zero the target fd_set structures and
253013570Sbrandon.potter@amd.com             * then update them with hits returned from the host select call.
253113570Sbrandon.potter@amd.com             */
253213570Sbrandon.potter@amd.com            trans_map[sim_fd] = iter;
253313570Sbrandon.potter@amd.com
253413570Sbrandon.potter@amd.com            /**
253513570Sbrandon.potter@amd.com             * We know that the host file descriptor exists so now we check
253613570Sbrandon.potter@amd.com             * if we need to update the max count for nfds_h before passing
253713570Sbrandon.potter@amd.com             * the duplicated structure into the host.
253813570Sbrandon.potter@amd.com             */
253913570Sbrandon.potter@amd.com            nfds_h = std::max(nfds_h - 1, sim_fd + 1);
254013570Sbrandon.potter@amd.com
254113570Sbrandon.potter@amd.com            /**
254213570Sbrandon.potter@amd.com             * Add the host file descriptor to the set that we are going to
254313570Sbrandon.potter@amd.com             * pass into the host.
254413570Sbrandon.potter@amd.com             */
254513570Sbrandon.potter@amd.com            FD_SET(sim_fd, hst_set_entry);
254613570Sbrandon.potter@amd.com        }
254713570Sbrandon.potter@amd.com        return false;
254813570Sbrandon.potter@amd.com    };
254913570Sbrandon.potter@amd.com
255013570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_t; i++) {
255113570Sbrandon.potter@amd.com        if (fds_read_ptr) {
255213570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i);
255313570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
255413570Sbrandon.potter@amd.com        }
255513570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
255613570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i);
255713570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
255813570Sbrandon.potter@amd.com        }
255913570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
256013570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i);
256113570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
256213570Sbrandon.potter@amd.com        }
256313570Sbrandon.potter@amd.com    }
256413570Sbrandon.potter@amd.com
256513570Sbrandon.potter@amd.com    if (time_val_ptr) {
256613570Sbrandon.potter@amd.com        /**
256713570Sbrandon.potter@amd.com         * It might be possible to decrement the timeval based on some
256813570Sbrandon.potter@amd.com         * derivation of wall clock determined from elapsed simulator ticks
256913570Sbrandon.potter@amd.com         * but that seems like overkill. Rather, we just set the timeval with
257013570Sbrandon.potter@amd.com         * zero timeout. (There is no reason to block during the simulation
257113570Sbrandon.potter@amd.com         * as it only decreases simulator performance.)
257213570Sbrandon.potter@amd.com         */
257313570Sbrandon.potter@amd.com        tp->tv_sec = 0;
257413570Sbrandon.potter@amd.com        tp->tv_usec = 0;
257513570Sbrandon.potter@amd.com
257613570Sbrandon.potter@amd.com        retval = select(nfds_h,
257713570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
257813570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
257913570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
258013570Sbrandon.potter@amd.com                        (timeval*)&*tp);
258113570Sbrandon.potter@amd.com    } else {
258213570Sbrandon.potter@amd.com        /**
258313570Sbrandon.potter@amd.com         * If the timeval pointer is null, setup a new timeval structure to
258413570Sbrandon.potter@amd.com         * pass into the host select call. Unfortunately, we will need to
258513570Sbrandon.potter@amd.com         * manually check the return value and throw a retry fault if the
258613570Sbrandon.potter@amd.com         * return value is zero. Allowing the system call to block will
258713570Sbrandon.potter@amd.com         * likely deadlock the event queue.
258813570Sbrandon.potter@amd.com         */
258913570Sbrandon.potter@amd.com        struct timeval tv = { 0, 0 };
259013570Sbrandon.potter@amd.com
259113570Sbrandon.potter@amd.com        retval = select(nfds_h,
259213570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
259313570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
259413570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
259513570Sbrandon.potter@amd.com                        &tv);
259613570Sbrandon.potter@amd.com
259713570Sbrandon.potter@amd.com        if (retval == 0) {
259813570Sbrandon.potter@amd.com            /**
259913570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
260013570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try to
260113570Sbrandon.potter@amd.com             * return the signal interrupt instead.
260213570Sbrandon.potter@amd.com             */
260313570Sbrandon.potter@amd.com            for (auto sig : tc->getSystemPtr()->signalList)
260413570Sbrandon.potter@amd.com                if (sig.receiver == p)
260513570Sbrandon.potter@amd.com                    return -EINTR;
260613570Sbrandon.potter@amd.com            return SyscallReturn::retry();
260713570Sbrandon.potter@amd.com        }
260813570Sbrandon.potter@amd.com    }
260913570Sbrandon.potter@amd.com
261013570Sbrandon.potter@amd.com    if (retval == -1)
261113570Sbrandon.potter@amd.com        return -errno;
261213570Sbrandon.potter@amd.com
261313570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*rd_t);
261413570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*wr_t);
261513570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*ex_t);
261613570Sbrandon.potter@amd.com
261713570Sbrandon.potter@amd.com    /**
261813570Sbrandon.potter@amd.com     * We need to translate the host file descriptor set into a target file
261913570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
262013570Sbrandon.potter@amd.com     * and the fd_set defined in header files.
262113570Sbrandon.potter@amd.com     */
262213570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_h; i++) {
262313570Sbrandon.potter@amd.com        if (fds_read_ptr) {
262413570Sbrandon.potter@amd.com            if (FD_ISSET(i, &rd_h))
262513570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*rd_t);
262613570Sbrandon.potter@amd.com        }
262713570Sbrandon.potter@amd.com
262813570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
262913570Sbrandon.potter@amd.com            if (FD_ISSET(i, &wr_h))
263013570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*wr_t);
263113570Sbrandon.potter@amd.com        }
263213570Sbrandon.potter@amd.com
263313570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
263413570Sbrandon.potter@amd.com            if (FD_ISSET(i, &ex_h))
263513570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*ex_t);
263613570Sbrandon.potter@amd.com        }
263713570Sbrandon.potter@amd.com    }
263813570Sbrandon.potter@amd.com
263913570Sbrandon.potter@amd.com    if (fds_read_ptr)
264013570Sbrandon.potter@amd.com        rd_t.copyOut(tc->getMemProxy());
264113570Sbrandon.potter@amd.com    if (fds_writ_ptr)
264213570Sbrandon.potter@amd.com        wr_t.copyOut(tc->getMemProxy());
264313570Sbrandon.potter@amd.com    if (fds_excp_ptr)
264413570Sbrandon.potter@amd.com        ex_t.copyOut(tc->getMemProxy());
264513570Sbrandon.potter@amd.com    if (time_val_ptr)
264613570Sbrandon.potter@amd.com        tp.copyOut(tc->getMemProxy());
264713570Sbrandon.potter@amd.com
264813570Sbrandon.potter@amd.com    return retval;
264913570Sbrandon.potter@amd.com}
265013570Sbrandon.potter@amd.com
265113570Sbrandon.potter@amd.comtemplate <class OS>
265213570Sbrandon.potter@amd.comSyscallReturn
265313570Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
265413570Sbrandon.potter@amd.com{
265513570Sbrandon.potter@amd.com    int index = 0;
265613570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
265713570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
265813570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
265913570Sbrandon.potter@amd.com
266013570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
266113570Sbrandon.potter@amd.com    if (!hbfdp)
266213570Sbrandon.potter@amd.com        return -EBADF;
266313570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
266413570Sbrandon.potter@amd.com
266513570Sbrandon.potter@amd.com    struct pollfd pfd;
266613570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
266713570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
266813570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
266913570Sbrandon.potter@amd.com        && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK))
267013570Sbrandon.potter@amd.com        return SyscallReturn::retry();
267113570Sbrandon.potter@amd.com
267213570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
267313570Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes);
267413570Sbrandon.potter@amd.com
267513570Sbrandon.potter@amd.com    if (bytes_read > 0)
267613570Sbrandon.potter@amd.com        buf_arg.copyOut(tc->getMemProxy());
267713570Sbrandon.potter@amd.com
267813570Sbrandon.potter@amd.com    return (bytes_read == -1) ? -errno : bytes_read;
267913570Sbrandon.potter@amd.com}
268013570Sbrandon.potter@amd.com
268113570Sbrandon.potter@amd.comtemplate <class OS>
268213570Sbrandon.potter@amd.comSyscallReturn
268313570Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
268413570Sbrandon.potter@amd.com{
268513570Sbrandon.potter@amd.com    int index = 0;
268613570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
268713570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
268813570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
268913570Sbrandon.potter@amd.com
269013570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
269113570Sbrandon.potter@amd.com    if (!hbfdp)
269213570Sbrandon.potter@amd.com        return -EBADF;
269313570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
269413570Sbrandon.potter@amd.com
269513570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
269613570Sbrandon.potter@amd.com    buf_arg.copyIn(tc->getMemProxy());
269713570Sbrandon.potter@amd.com
269813570Sbrandon.potter@amd.com    struct pollfd pfd;
269913570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
270013570Sbrandon.potter@amd.com    pfd.events = POLLOUT;
270113570Sbrandon.potter@amd.com
270213570Sbrandon.potter@amd.com    /**
270313570Sbrandon.potter@amd.com     * We don't want to poll on /dev/random. The kernel will not enable the
270413570Sbrandon.potter@amd.com     * file descriptor for writing unless the entropy in the system falls
270513570Sbrandon.potter@amd.com     * below write_wakeup_threshold. This is not guaranteed to happen
270613570Sbrandon.potter@amd.com     * depending on host settings.
270713570Sbrandon.potter@amd.com     */
270813570Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp);
270913570Sbrandon.potter@amd.com    if (ffdp && (ffdp->getFileName() != "/dev/random")) {
271013570Sbrandon.potter@amd.com        if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK))
271113570Sbrandon.potter@amd.com            return SyscallReturn::retry();
271213570Sbrandon.potter@amd.com    }
271313570Sbrandon.potter@amd.com
271413570Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes);
271513570Sbrandon.potter@amd.com
271613570Sbrandon.potter@amd.com    if (bytes_written != -1)
271713570Sbrandon.potter@amd.com        fsync(sim_fd);
271813570Sbrandon.potter@amd.com
271913570Sbrandon.potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
272013570Sbrandon.potter@amd.com}
272113570Sbrandon.potter@amd.com
272213570Sbrandon.potter@amd.comtemplate <class OS>
272313570Sbrandon.potter@amd.comSyscallReturn
272413570Sbrandon.potter@amd.comwait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
272513570Sbrandon.potter@amd.com{
272613570Sbrandon.potter@amd.com    int index = 0;
272713570Sbrandon.potter@amd.com    pid_t pid = p->getSyscallArg(tc, index);
272813570Sbrandon.potter@amd.com    Addr statPtr = p->getSyscallArg(tc, index);
272913570Sbrandon.potter@amd.com    int options = p->getSyscallArg(tc, index);
273013570Sbrandon.potter@amd.com    Addr rusagePtr = p->getSyscallArg(tc, index);
273113570Sbrandon.potter@amd.com
273213570Sbrandon.potter@amd.com    if (rusagePtr)
273313907Salexandru.dutu@amd.com        DPRINTF_SYSCALL(Verbose, "wait4: rusage pointer provided %lx, however "
273413570Sbrandon.potter@amd.com                 "functionality not supported. Ignoring rusage pointer.\n",
273513907Salexandru.dutu@amd.com                 rusagePtr);
273613570Sbrandon.potter@amd.com
273713570Sbrandon.potter@amd.com    /**
273813570Sbrandon.potter@amd.com     * Currently, wait4 is only implemented so that it will wait for children
273913570Sbrandon.potter@amd.com     * exit conditions which are denoted by a SIGCHLD signals posted into the
274013570Sbrandon.potter@amd.com     * system signal list. We return no additional information via any of the
274113570Sbrandon.potter@amd.com     * parameters supplied to wait4. If nothing is found in the system signal
274213570Sbrandon.potter@amd.com     * list, we will wait indefinitely for SIGCHLD to post by retrying the
274313570Sbrandon.potter@amd.com     * call.
274413570Sbrandon.potter@amd.com     */
274513570Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
274613570Sbrandon.potter@amd.com    std::list<BasicSignal>::iterator iter;
274713570Sbrandon.potter@amd.com    for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) {
274813570Sbrandon.potter@amd.com        if (iter->receiver == p) {
274913570Sbrandon.potter@amd.com            if (pid < -1) {
275013570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == -pid)
275113570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
275213570Sbrandon.potter@amd.com                    goto success;
275313570Sbrandon.potter@amd.com            } else if (pid == -1) {
275413570Sbrandon.potter@amd.com                if (iter->signalValue == OS::TGT_SIGCHLD)
275513570Sbrandon.potter@amd.com                    goto success;
275613570Sbrandon.potter@amd.com            } else if (pid == 0) {
275713570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == p->pgid())
275813570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
275913570Sbrandon.potter@amd.com                    goto success;
276013570Sbrandon.potter@amd.com            } else {
276113570Sbrandon.potter@amd.com                if ((iter->sender->pid() == pid)
276213570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
276313570Sbrandon.potter@amd.com                    goto success;
276413570Sbrandon.potter@amd.com            }
276513570Sbrandon.potter@amd.com        }
276613570Sbrandon.potter@amd.com    }
276713570Sbrandon.potter@amd.com
276813570Sbrandon.potter@amd.com    return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry();
276913570Sbrandon.potter@amd.com
277013570Sbrandon.potter@amd.comsuccess:
277113570Sbrandon.potter@amd.com    // Set status to EXITED for WIFEXITED evaluations.
277213570Sbrandon.potter@amd.com    const int EXITED = 0;
277313570Sbrandon.potter@amd.com    BufferArg statusBuf(statPtr, sizeof(int));
277413570Sbrandon.potter@amd.com    *(int *)statusBuf.bufferPtr() = EXITED;
277513570Sbrandon.potter@amd.com    statusBuf.copyOut(tc->getMemProxy());
277613570Sbrandon.potter@amd.com
277713570Sbrandon.potter@amd.com    // Return the child PID.
277813570Sbrandon.potter@amd.com    pid_t retval = iter->sender->pid();
277913570Sbrandon.potter@amd.com    sysh->signalList.erase(iter);
278013570Sbrandon.potter@amd.com    return retval;
278113570Sbrandon.potter@amd.com}
278213570Sbrandon.potter@amd.com
278313570Sbrandon.potter@amd.comtemplate <class OS>
278413570Sbrandon.potter@amd.comSyscallReturn
278513570Sbrandon.potter@amd.comacceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
278613570Sbrandon.potter@amd.com{
278713570Sbrandon.potter@amd.com    struct sockaddr sa;
278813570Sbrandon.potter@amd.com    socklen_t addrLen;
278913570Sbrandon.potter@amd.com    int host_fd;
279013570Sbrandon.potter@amd.com    int index = 0;
279113570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
279213570Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
279313570Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
279413570Sbrandon.potter@amd.com
279513570Sbrandon.potter@amd.com    BufferArg *lenBufPtr = nullptr;
279613570Sbrandon.potter@amd.com    BufferArg *addrBufPtr = nullptr;
279713570Sbrandon.potter@amd.com
279813570Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
279913570Sbrandon.potter@amd.com    if (!sfdp)
280013570Sbrandon.potter@amd.com        return -EBADF;
280113570Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
280213570Sbrandon.potter@amd.com
280313570Sbrandon.potter@amd.com    /**
280413570Sbrandon.potter@amd.com     * We poll the socket file descriptor first to guarantee that we do not
280513570Sbrandon.potter@amd.com     * block on our accept call. The socket can be opened without the
280613570Sbrandon.potter@amd.com     * non-blocking flag (it blocks). This will cause deadlocks between
280713570Sbrandon.potter@amd.com     * communicating processes.
280813570Sbrandon.potter@amd.com     */
280913570Sbrandon.potter@amd.com    struct pollfd pfd;
281013570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
281113570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
281213570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
281313570Sbrandon.potter@amd.com        && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK))
281413570Sbrandon.potter@amd.com        return SyscallReturn::retry();
281513570Sbrandon.potter@amd.com
281613570Sbrandon.potter@amd.com    if (lenPtr) {
281713570Sbrandon.potter@amd.com        lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t));
281813570Sbrandon.potter@amd.com        lenBufPtr->copyIn(tc->getMemProxy());
281913570Sbrandon.potter@amd.com        memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(),
282013570Sbrandon.potter@amd.com               sizeof(socklen_t));
282113570Sbrandon.potter@amd.com    }
282213570Sbrandon.potter@amd.com
282313570Sbrandon.potter@amd.com    if (addrPtr) {
282413570Sbrandon.potter@amd.com        addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr));
282513570Sbrandon.potter@amd.com        addrBufPtr->copyIn(tc->getMemProxy());
282613570Sbrandon.potter@amd.com        memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(),
282713570Sbrandon.potter@amd.com               sizeof(struct sockaddr));
282813570Sbrandon.potter@amd.com    }
282913570Sbrandon.potter@amd.com
283013570Sbrandon.potter@amd.com    host_fd = accept(sim_fd, &sa, &addrLen);
283113570Sbrandon.potter@amd.com
283213570Sbrandon.potter@amd.com    if (host_fd == -1)
283313570Sbrandon.potter@amd.com        return -errno;
283413570Sbrandon.potter@amd.com
283513570Sbrandon.potter@amd.com    if (addrPtr) {
283613570Sbrandon.potter@amd.com        memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa));
283713570Sbrandon.potter@amd.com        addrBufPtr->copyOut(tc->getMemProxy());
283813570Sbrandon.potter@amd.com        delete(addrBufPtr);
283913570Sbrandon.potter@amd.com    }
284013570Sbrandon.potter@amd.com
284113570Sbrandon.potter@amd.com    if (lenPtr) {
284213570Sbrandon.potter@amd.com        *(socklen_t *)lenBufPtr->bufferPtr() = addrLen;
284313570Sbrandon.potter@amd.com        lenBufPtr->copyOut(tc->getMemProxy());
284413570Sbrandon.potter@amd.com        delete(lenBufPtr);
284513570Sbrandon.potter@amd.com    }
284613570Sbrandon.potter@amd.com
284713570Sbrandon.potter@amd.com    auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain,
284813570Sbrandon.potter@amd.com                                                sfdp->_type, sfdp->_protocol);
284913570Sbrandon.potter@amd.com    return p->fds->allocFD(afdp);
285013570Sbrandon.potter@amd.com}
285113570Sbrandon.potter@amd.com
285213933Sbrandon.potter@amd.com/// Target eventfd() function.
285313933Sbrandon.potter@amd.comtemplate <class OS>
285413933Sbrandon.potter@amd.comSyscallReturn
285513933Sbrandon.potter@amd.comeventfdFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
285613933Sbrandon.potter@amd.com{
285713936SAndrea.Mondelli@ucf.edu#if defined(__linux__)
285813933Sbrandon.potter@amd.com    int index = 0;
285913933Sbrandon.potter@amd.com    unsigned initval = p->getSyscallArg(tc, index);
286013933Sbrandon.potter@amd.com    int in_flags = p->getSyscallArg(tc, index);
286113933Sbrandon.potter@amd.com
286213933Sbrandon.potter@amd.com    int sim_fd = eventfd(initval, in_flags);
286313933Sbrandon.potter@amd.com    if (sim_fd == -1)
286413933Sbrandon.potter@amd.com        return -errno;
286513933Sbrandon.potter@amd.com
286613933Sbrandon.potter@amd.com    bool cloexec = in_flags & OS::TGT_O_CLOEXEC;
286713933Sbrandon.potter@amd.com
286813933Sbrandon.potter@amd.com    int flags = cloexec ? OS::TGT_O_CLOEXEC : 0;
286913933Sbrandon.potter@amd.com    flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0;
287013933Sbrandon.potter@amd.com
287113933Sbrandon.potter@amd.com    auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec);
287213933Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(hbfdp);
287313933Sbrandon.potter@amd.com    return tgt_fd;
287413933Sbrandon.potter@amd.com#else
287513933Sbrandon.potter@amd.com    warnUnsupportedOS("eventfd");
287613933Sbrandon.potter@amd.com    return -1;
287713933Sbrandon.potter@amd.com#endif
287813933Sbrandon.potter@amd.com}
287913933Sbrandon.potter@amd.com
28801354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2881