syscall_emul.hh revision 13883
1360SN/A/*
210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited
310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc.
410027SChris.Adeniyi-Jones@arm.com * All rights reserved
510027SChris.Adeniyi-Jones@arm.com *
610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1410027SChris.Adeniyi-Jones@arm.com *
151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
16360SN/A * All rights reserved.
17360SN/A *
18360SN/A * Redistribution and use in source and binary forms, with or without
19360SN/A * modification, are permitted provided that the following conditions are
20360SN/A * met: redistributions of source code must retain the above copyright
21360SN/A * notice, this list of conditions and the following disclaimer;
22360SN/A * redistributions in binary form must reproduce the above copyright
23360SN/A * notice, this list of conditions and the following disclaimer in the
24360SN/A * documentation and/or other materials provided with the distribution;
25360SN/A * neither the name of the copyright holders nor the names of its
26360SN/A * contributors may be used to endorse or promote products derived from
27360SN/A * this software without specific prior written permission.
28360SN/A *
29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
422665Ssaidi@eecs.umich.edu *          Kevin Lim
43360SN/A */
44360SN/A
451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
461354SN/A#define __SIM_SYSCALL_EMUL_HH__
47360SN/A
4812018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
4912018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__CYGWIN__) ||     \
5012018Sandreas.sandberg@arm.com     defined(__NetBSD__))
5112018Sandreas.sandberg@arm.com#define NO_STAT64 1
5212018Sandreas.sandberg@arm.com#else
5312018Sandreas.sandberg@arm.com#define NO_STAT64 0
5412018Sandreas.sandberg@arm.com#endif
552064SN/A
5612018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
5712018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__NetBSD__))
5812018Sandreas.sandberg@arm.com#define NO_STATFS 1
5912018Sandreas.sandberg@arm.com#else
6012018Sandreas.sandberg@arm.com#define NO_STATFS 0
6112018Sandreas.sandberg@arm.com#endif
6211799Sbrandon.potter@amd.com
6312018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
6412018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__NetBSD__))
6512018Sandreas.sandberg@arm.com#define NO_FALLOCATE 1
6612018Sandreas.sandberg@arm.com#else
6712018Sandreas.sandberg@arm.com#define NO_FALLOCATE 0
6812018Sandreas.sandberg@arm.com#endif
6911799Sbrandon.potter@amd.com
70360SN/A///
71360SN/A/// @file syscall_emul.hh
72360SN/A///
73360SN/A/// This file defines objects used to emulate syscalls from the target
74360SN/A/// application on the host machine.
75360SN/A
761809SN/A#ifdef __CYGWIN32__
7711800Sbrandon.potter@amd.com#include <sys/fcntl.h>
7811392Sbrandon.potter@amd.com
791809SN/A#endif
8011392Sbrandon.potter@amd.com#include <fcntl.h>
8113570Sbrandon.potter@amd.com#include <poll.h>
8211383Sbrandon.potter@amd.com#include <sys/mman.h>
8313568Sbrandon.potter@amd.com#include <sys/socket.h>
843113Sgblack@eecs.umich.edu#include <sys/stat.h>
8511799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
8611759Sbrandon.potter@amd.com#include <sys/statfs.h>
8711812Sbaz21@cam.ac.uk#else
8811812Sbaz21@cam.ac.uk#include <sys/mount.h>
8911799Sbrandon.potter@amd.com#endif
908229Snate@binkert.org#include <sys/time.h>
9113570Sbrandon.potter@amd.com#include <sys/types.h>
928229Snate@binkert.org#include <sys/uio.h>
9311594Santhony.gutierrez@amd.com#include <unistd.h>
947075Snate@binkert.org
958229Snate@binkert.org#include <cerrno>
9611856Sbrandon.potter@amd.com#include <memory>
977075Snate@binkert.org#include <string>
98360SN/A
9912461Sgabeblack@google.com#include "arch/generic/tlb.hh"
10011886Sbrandon.potter@amd.com#include "arch/utility.hh"
10111800Sbrandon.potter@amd.com#include "base/intmath.hh"
10211392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
10312334Sgabeblack@google.com#include "base/logging.hh"
1041354SN/A#include "base/trace.hh"
1056216Snate@binkert.org#include "base/types.hh"
1066658Snate@binkert.org#include "config/the_isa.hh"
1072474SN/A#include "cpu/base.hh"
1082680Sktlim@umich.edu#include "cpu/thread_context.hh"
1098229Snate@binkert.org#include "mem/page_table.hh"
11011886Sbrandon.potter@amd.com#include "params/Process.hh"
11110496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
11211911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1138229Snate@binkert.org#include "sim/process.hh"
11411794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
11511886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
11610497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
11711794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
118360SN/A
11913629SAndrea.Mondelli@ucf.edu#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN)
12013629SAndrea.Mondelli@ucf.edu#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
12113629SAndrea.Mondelli@ucf.edu#endif
12213629SAndrea.Mondelli@ucf.edu
123360SN/A//////////////////////////////////////////////////////////////////////
124360SN/A//
125360SN/A// The following emulation functions are generic enough that they
126360SN/A// don't need to be recompiled for different emulated OS's.  They are
127360SN/A// defined in sim/syscall_emul.cc.
128360SN/A//
129360SN/A//////////////////////////////////////////////////////////////////////
130360SN/A
131360SN/A
132378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1331706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
13411851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
135378SN/A
136378SN/A/// Handler for unimplemented syscalls that we never intend to
137378SN/A/// implement (signal handling, etc.) and should not affect the correct
138378SN/A/// behavior of the program.  Print a warning only if the appropriate
139378SN/A/// trace flag is enabled.  Return success to the target program.
1401706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
14111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
142360SN/A
14311760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
14411760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
14511851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
14611760Sbrandon.potter@amd.com
1476109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1481706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14911851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
150378SN/A
1516109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1526109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
15311851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1546109Ssanchezd@stanford.edu
15511886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
15611886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
15711886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15811886Sbrandon.potter@amd.com
159378SN/A/// Target getpagesize() handler.
1601706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
16111851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
162378SN/A
1635748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1645748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
16511851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
166378SN/A
167378SN/A/// Target close() handler.
1681706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
170378SN/A
171378SN/A/// Target lseek() handler.
1721706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
17311851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
174378SN/A
1754118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1764118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17711851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1784118Sgblack@eecs.umich.edu
179378SN/A/// Target munmap() handler.
1801706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
18111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
182378SN/A
18313568Sbrandon.potter@amd.com/// Target shutdown() handler.
18413568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num,
18513568Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
18613568Sbrandon.potter@amd.com
187378SN/A/// Target gethostname() handler.
1881706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
18911851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
190360SN/A
1915513SMichael.Adler@intel.com/// Target getcwd() handler.
1925513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
19311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1945513SMichael.Adler@intel.com
19510203SAli.Saidi@ARM.com/// Target readlink() handler.
19610203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19711851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
19810203SAli.Saidi@ARM.com                           int index = 0);
1995513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
20011851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2015513SMichael.Adler@intel.com
202511SN/A/// Target unlink() handler.
20310633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20411851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20510633Smichaelupton@gmail.com                           int index);
2061706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
208511SN/A
20912795Smattdsinclair@gmail.com/// Target link() handler
21012795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
21112795Smattdsinclair@gmail.com                       ThreadContext *tc);
21212795Smattdsinclair@gmail.com
21312796Smattdsinclair@gmail.com/// Target symlink() handler.
21412796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
21512796Smattdsinclair@gmail.com                          ThreadContext *tc);
21612796Smattdsinclair@gmail.com
2175513SMichael.Adler@intel.com/// Target mkdir() handler.
2185513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
21911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2205513SMichael.Adler@intel.com
22113031Sbrandon.potter@amd.com/// Target mknod() handler.
22213031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
22313031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22413031Sbrandon.potter@amd.com
22513031Sbrandon.potter@amd.com/// Target chdir() handler.
22613031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
22713031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22813031Sbrandon.potter@amd.com
22913031Sbrandon.potter@amd.com// Target rmdir() handler.
23013031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
23113031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
23213031Sbrandon.potter@amd.com
233511SN/A/// Target rename() handler.
2341706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
23511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2361706SN/A
2371706SN/A
2381706SN/A/// Target truncate() handler.
2391706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
24011851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2411706SN/A
2421706SN/A
2431706SN/A/// Target ftruncate() handler.
2441706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
24511851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2461706SN/A
247511SN/A
2486703Svince@csl.cornell.edu/// Target truncate64() handler.
2496703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
25011851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2516703Svince@csl.cornell.edu
2526685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2536685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
25411851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2556685Stjones1@inf.ed.ac.uk
2566685Stjones1@inf.ed.ac.uk
2575513SMichael.Adler@intel.com/// Target umask() handler.
2585513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
25911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2605513SMichael.Adler@intel.com
26111885Sbrandon.potter@amd.com/// Target gettid() handler.
26211885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
26311885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2645513SMichael.Adler@intel.com
2651999SN/A/// Target chown() handler.
2661999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26711851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2681999SN/A
26911885Sbrandon.potter@amd.com/// Target setpgid() handler.
27011885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
27111885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2721999SN/A
2731999SN/A/// Target fchown() handler.
2741999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
27511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2761999SN/A
2773079Sstever@eecs.umich.edu/// Target dup() handler.
2783079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
27911851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2803079Sstever@eecs.umich.edu
28111908SBrandon.Potter@amd.com/// Target dup2() handler.
28211908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
28311908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
28411908SBrandon.Potter@amd.com
28511875Sbrandon.potter@amd.com/// Target fcntl() handler.
2862093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28711851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2882093SN/A
2892687Sksewell@umich.edu/// Target fcntl64() handler.
2902687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
29111851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2922687Sksewell@umich.edu
2932238SN/A/// Target setuid() handler.
2942238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
29511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2962238SN/A
29711908SBrandon.Potter@amd.com/// Target pipe() handler.
29811908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
29911908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
30011908SBrandon.Potter@amd.com
30111908SBrandon.Potter@amd.com/// Internal pipe() handler.
30211908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
30311908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
30411908SBrandon.Potter@amd.com
3052238SN/A/// Target getpid() handler.
3062238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3082238SN/A
30913571Sbrandon.potter@amd.com// Target getpeername() handler.
31013571Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num,
31113571Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
31213571Sbrandon.potter@amd.com
31313568Sbrandon.potter@amd.com// Target bind() handler.
31413568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num,
31513568Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
31613568Sbrandon.potter@amd.com
31713568Sbrandon.potter@amd.com// Target listen() handler.
31813568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num,
31913568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
32013568Sbrandon.potter@amd.com
32113568Sbrandon.potter@amd.com// Target connect() handler.
32213568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num,
32313568Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
32413568Sbrandon.potter@amd.com
32513448Sciro.santilli@arm.com#if defined(SYS_getdents)
32613031Sbrandon.potter@amd.com// Target getdents() handler.
32713031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
32813031Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
32913448Sciro.santilli@arm.com#endif
33013031Sbrandon.potter@amd.com
33113539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
33213539Sjavier.setoain@arm.com// Target getdents() handler.
33313539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
33413539Sjavier.setoain@arm.com                           Process *p, ThreadContext *tc);
33513539Sjavier.setoain@arm.com#endif
33613539Sjavier.setoain@arm.com
33713569Sbrandon.potter@amd.com// Target sendto() handler.
33813569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num,
33913569Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
34013569Sbrandon.potter@amd.com
34113569Sbrandon.potter@amd.com// Target recvfrom() handler.
34213569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num,
34313569Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
34413569Sbrandon.potter@amd.com
34513569Sbrandon.potter@amd.com// Target recvmsg() handler.
34613569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num,
34713569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
34813569Sbrandon.potter@amd.com
34913569Sbrandon.potter@amd.com// Target sendmsg() handler.
35013569Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num,
35113569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
35213569Sbrandon.potter@amd.com
35313031Sbrandon.potter@amd.com// Target getuid() handler.
3542238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
35511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3562238SN/A
3572238SN/A/// Target getgid() handler.
3582238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
35911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3602238SN/A
3612238SN/A/// Target getppid() handler.
3622238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
36311851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3642238SN/A
3652238SN/A/// Target geteuid() handler.
3662238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
36711851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3682238SN/A
3692238SN/A/// Target getegid() handler.
3702238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
37111851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3722238SN/A
3739455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3749455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
37610203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
37811851Sbrandon.potter@amd.com                         int index);
3799455Smitch.hayenga+gem5@gmail.com
38013571Sbrandon.potter@amd.com// Target getsockopt() handler.
38113571Sbrandon.potter@amd.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num,
38213571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
38313571Sbrandon.potter@amd.com
38413571Sbrandon.potter@amd.com// Target setsockopt() handler.
38513571Sbrandon.potter@amd.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num,
38613571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
38713571Sbrandon.potter@amd.com
38813571Sbrandon.potter@amd.com// Target getsockname() handler.
38913571Sbrandon.potter@amd.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num,
39013571Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
39113571Sbrandon.potter@amd.com
3929112Smarc.orr@gmail.com/// Futex system call
39311906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
39411906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3959112Smarc.orr@gmail.comtemplate <class OS>
3969112Smarc.orr@gmail.comSyscallReturn
39711851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3989112Smarc.orr@gmail.com          ThreadContext *tc)
3999112Smarc.orr@gmail.com{
40011911SBrandon.Potter@amd.com    using namespace std;
4019112Smarc.orr@gmail.com
40211911SBrandon.Potter@amd.com    int index = 0;
40311911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
40411911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
40511911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
40613642Sqtt2@cornell.edu    int timeout M5_VAR_USED = process->getSyscallArg(tc, index);
40713642Sqtt2@cornell.edu    Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index);
40813642Sqtt2@cornell.edu    int val3 = process->getSyscallArg(tc, index);
4099112Smarc.orr@gmail.com
41011911SBrandon.Potter@amd.com    /*
41111911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
41211911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
41311911SBrandon.Potter@amd.com     */
4149238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
41513642Sqtt2@cornell.edu    op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG;
4169112Smarc.orr@gmail.com
41711911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
4189112Smarc.orr@gmail.com
41913642Sqtt2@cornell.edu    if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) {
42011911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
42111911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
42211911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
42311911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
4249112Smarc.orr@gmail.com
42511911SBrandon.Potter@amd.com        /*
42611911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
42711911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
42811911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
42911911SBrandon.Potter@amd.com         */
43011911SBrandon.Potter@amd.com        if (val != mem_val)
4319112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
4329112Smarc.orr@gmail.com
43313642Sqtt2@cornell.edu        if (OS::TGT_FUTEX_WAIT) {
43413642Sqtt2@cornell.edu            futex_map.suspend(uaddr, process->tgid(), tc);
43513642Sqtt2@cornell.edu        } else {
43613642Sqtt2@cornell.edu            futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3);
43713642Sqtt2@cornell.edu        }
43811911SBrandon.Potter@amd.com
4399112Smarc.orr@gmail.com        return 0;
44011911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
44111911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
44213642Sqtt2@cornell.edu    } else if (OS::TGT_FUTEX_WAKE_BITSET == op) {
44313642Sqtt2@cornell.edu        return futex_map.wakeup_bitset(uaddr, process->tgid(), val3);
44413650Smw828@cornell.edu    } else if (OS::TGT_FUTEX_REQUEUE == op ||
44513650Smw828@cornell.edu               OS::TGT_FUTEX_CMP_REQUEUE == op) {
44613650Smw828@cornell.edu
44713650Smw828@cornell.edu        // Ensure futex system call accessed atomically.
44813650Smw828@cornell.edu        BufferArg buf(uaddr, sizeof(int));
44913650Smw828@cornell.edu        buf.copyIn(tc->getMemProxy());
45013650Smw828@cornell.edu        int mem_val = *(int*)buf.bufferPtr();
45113650Smw828@cornell.edu        /*
45213650Smw828@cornell.edu         * For CMP_REQUEUE, the whole operation is only started only if
45313650Smw828@cornell.edu         * val3 is still the value of the futex pointed to by uaddr.
45413650Smw828@cornell.edu         */
45513650Smw828@cornell.edu        if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val)
45613650Smw828@cornell.edu            return -OS::TGT_EWOULDBLOCK;
45713650Smw828@cornell.edu        return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2);
45813651Smw828@cornell.edu    } else if (OS::TGT_FUTEX_WAKE_OP == op) {
45913651Smw828@cornell.edu        /*
46013651Smw828@cornell.edu         * The FUTEX_WAKE_OP operation is equivalent to executing the
46113651Smw828@cornell.edu         * following code atomically and totally ordered with respect to
46213651Smw828@cornell.edu         * other futex operations on any of the two supplied futex words:
46313651Smw828@cornell.edu         *
46413651Smw828@cornell.edu         *   int oldval = *(int *) addr2;
46513651Smw828@cornell.edu         *   *(int *) addr2 = oldval op oparg;
46613651Smw828@cornell.edu         *   futex(addr1, FUTEX_WAKE, val, 0, 0, 0);
46713651Smw828@cornell.edu         *   if (oldval cmp cmparg)
46813651Smw828@cornell.edu         *        futex(addr2, FUTEX_WAKE, val2, 0, 0, 0);
46913651Smw828@cornell.edu         *
47013651Smw828@cornell.edu         * (op, oparg, cmp, cmparg are encoded in val3)
47113651Smw828@cornell.edu         *
47213651Smw828@cornell.edu         * +---+---+-----------+-----------+
47313651Smw828@cornell.edu         * |op |cmp|   oparg   |  cmparg   |
47413651Smw828@cornell.edu         * +---+---+-----------+-----------+
47513651Smw828@cornell.edu         *   4   4       12          12    <== # of bits
47613651Smw828@cornell.edu         *
47713651Smw828@cornell.edu         * reference: http://man7.org/linux/man-pages/man2/futex.2.html
47813651Smw828@cornell.edu         *
47913651Smw828@cornell.edu         */
48013651Smw828@cornell.edu        // get value from simulated-space
48113651Smw828@cornell.edu        BufferArg buf(uaddr2, sizeof(int));
48213651Smw828@cornell.edu        buf.copyIn(tc->getMemProxy());
48313651Smw828@cornell.edu        int oldval = *(int*)buf.bufferPtr();
48413651Smw828@cornell.edu        int newval = oldval;
48513651Smw828@cornell.edu        // extract op, oparg, cmp, cmparg from val3
48613651Smw828@cornell.edu        int wake_cmparg =  val3 & 0xfff;
48713651Smw828@cornell.edu        int wake_oparg  = (val3 & 0xfff000)   >> 12;
48813651Smw828@cornell.edu        int wake_cmp    = (val3 & 0xf000000)  >> 24;
48913651Smw828@cornell.edu        int wake_op     = (val3 & 0xf0000000) >> 28;
49013651Smw828@cornell.edu        if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1)
49113651Smw828@cornell.edu            wake_oparg = (1 << wake_oparg);
49213651Smw828@cornell.edu        wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT;
49313651Smw828@cornell.edu        // perform operation on the value of the second futex
49413651Smw828@cornell.edu        if (wake_op == OS::TGT_FUTEX_OP_SET)
49513651Smw828@cornell.edu            newval = wake_oparg;
49613651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_ADD)
49713651Smw828@cornell.edu            newval += wake_oparg;
49813651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_OR)
49913651Smw828@cornell.edu            newval |= wake_oparg;
50013651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_ANDN)
50113651Smw828@cornell.edu            newval &= ~wake_oparg;
50213651Smw828@cornell.edu        else if (wake_op == OS::TGT_FUTEX_OP_XOR)
50313651Smw828@cornell.edu            newval ^= wake_oparg;
50413651Smw828@cornell.edu        // copy updated value back to simulated-space
50513651Smw828@cornell.edu        *(int*)buf.bufferPtr() = newval;
50613651Smw828@cornell.edu        buf.copyOut(tc->getMemProxy());
50713651Smw828@cornell.edu        // perform the first wake-up
50813651Smw828@cornell.edu        int woken1 = futex_map.wakeup(uaddr, process->tgid(), val);
50913651Smw828@cornell.edu        int woken2 = 0;
51013651Smw828@cornell.edu        // calculate the condition of the second wake-up
51113651Smw828@cornell.edu        bool is_wake2 = false;
51213651Smw828@cornell.edu        if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ)
51313651Smw828@cornell.edu            is_wake2 = oldval == wake_cmparg;
51413651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE)
51513651Smw828@cornell.edu            is_wake2 = oldval != wake_cmparg;
51613651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT)
51713651Smw828@cornell.edu            is_wake2 = oldval < wake_cmparg;
51813651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE)
51913651Smw828@cornell.edu            is_wake2 = oldval <= wake_cmparg;
52013651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT)
52113651Smw828@cornell.edu            is_wake2 = oldval > wake_cmparg;
52213651Smw828@cornell.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE)
52313651Smw828@cornell.edu            is_wake2 = oldval >= wake_cmparg;
52413651Smw828@cornell.edu        // perform the second wake-up
52513651Smw828@cornell.edu        if (is_wake2)
52613651Smw828@cornell.edu            woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout);
52713651Smw828@cornell.edu
52813651Smw828@cornell.edu        return woken1 + woken2;
5299112Smarc.orr@gmail.com    }
53011911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
53111911SBrandon.Potter@amd.com    return -ENOSYS;
5329112Smarc.orr@gmail.com}
5339112Smarc.orr@gmail.com
5342238SN/A
5352238SN/A/// Pseudo Funcs  - These functions use a different return convension,
5362238SN/A/// returning a second value in a register other than the normal return register
5372238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
53811851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
5392238SN/A
5402238SN/A/// Target getpidPseudo() handler.
5412238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
54211851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
5432238SN/A
5442238SN/A/// Target getuidPseudo() handler.
5452238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
54611851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
5472238SN/A
5482238SN/A/// Target getgidPseudo() handler.
5492238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
55011851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
5512238SN/A
5522238SN/A
5531354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
5541354SN/Aconst int one_million = 1000000;
55510796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
55610796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
5571354SN/A
5581354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
5591354SN/A/// by my reckoning.  We want to keep this a constant (not use the
5601354SN/A/// real-world time) to keep simulations repeatable.
5611354SN/Aconst unsigned seconds_since_epoch = 1000000000;
5621354SN/A
5631354SN/A/// Helper function to convert current elapsed time to seconds and
5641354SN/A/// microseconds.
5651354SN/Atemplate <class T1, class T2>
5661354SN/Avoid
56710796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
5681354SN/A{
56910796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
5701354SN/A    sec = elapsed_usecs / one_million;
5711354SN/A    usec = elapsed_usecs % one_million;
5721354SN/A}
5731354SN/A
57410796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
57510796Sbrandon.potter@amd.com/// nanoseconds.
57610796Sbrandon.potter@amd.comtemplate <class T1, class T2>
57710796Sbrandon.potter@amd.comvoid
57810796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
57910796Sbrandon.potter@amd.com{
58010796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
58110796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
58210796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
58310796Sbrandon.potter@amd.com}
58410796Sbrandon.potter@amd.com
585360SN/A//////////////////////////////////////////////////////////////////////
586360SN/A//
587360SN/A// The following emulation functions are generic, but need to be
588360SN/A// templated to account for differences in types, constants, etc.
589360SN/A//
590360SN/A//////////////////////////////////////////////////////////////////////
591360SN/A
59211759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
5933113Sgblack@eecs.umich.edu#if NO_STAT64
5943113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5953113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
5963113Sgblack@eecs.umich.edu#else
5973113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5983113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
5993113Sgblack@eecs.umich.edu#endif
6003113Sgblack@eecs.umich.edu
6013113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
6023113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
6033113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
6043113Sgblack@eecs.umich.edu
6053113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
60612032Sandreas.sandberg@arm.comvoid
6073113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
6083113Sgblack@eecs.umich.edu{
6094189Sgblack@eecs.umich.edu    using namespace TheISA;
6104189Sgblack@eecs.umich.edu
6113113Sgblack@eecs.umich.edu    if (fakeTTY)
6123113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
6133113Sgblack@eecs.umich.edu    else
6143113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
6158737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
6163113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
6178737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
6183277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
6195515SMichael.Adler@intel.com    if (fakeTTY) {
6205515SMichael.Adler@intel.com        // Claim to be a character device
6215515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
6225515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
6235515SMichael.Adler@intel.com    }
6248737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
6253277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
6268737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
6273277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
6288737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
6293277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
6308737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
6313113Sgblack@eecs.umich.edu    if (fakeTTY)
6323113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
6333113Sgblack@eecs.umich.edu    else
6343113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
6358737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
6363113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
6378737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
6383114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
6398737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
6403114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
6418737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
6423114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
6438737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
64411906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
6454061Sgblack@eecs.umich.edu    // consistently across different hosts.
6464061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
6478737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
6483113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
6498737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
6503113Sgblack@eecs.umich.edu}
6513113Sgblack@eecs.umich.edu
6523113Sgblack@eecs.umich.edu// Same for stat64
6533113Sgblack@eecs.umich.edu
6543113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
65512032Sandreas.sandberg@arm.comvoid
6563113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
6573113Sgblack@eecs.umich.edu{
6584189Sgblack@eecs.umich.edu    using namespace TheISA;
6594189Sgblack@eecs.umich.edu
6603113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
6613113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
6623113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
6638737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
6643113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
6658737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
6663113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
6678737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
6683113Sgblack@eecs.umich.edu#else
6693113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
6703113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
6713113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
6723113Sgblack@eecs.umich.edu#endif
6733113Sgblack@eecs.umich.edu}
6743113Sgblack@eecs.umich.edu
67511906SBrandon.Potter@amd.com// Here are a couple of convenience functions
6763113Sgblack@eecs.umich.edutemplate<class OS>
67712032Sandreas.sandberg@arm.comvoid
6788852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
67911906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
6803113Sgblack@eecs.umich.edu{
6813113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
6823113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
6833113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
6843113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
6853113Sgblack@eecs.umich.edu}
6863113Sgblack@eecs.umich.edu
6873113Sgblack@eecs.umich.edutemplate<class OS>
68812032Sandreas.sandberg@arm.comvoid
6898852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
69011906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
6913113Sgblack@eecs.umich.edu{
6923113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
6933113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
6946686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
6953113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
6963113Sgblack@eecs.umich.edu}
6973113Sgblack@eecs.umich.edu
69811759Sbrandon.potter@amd.comtemplate <class OS>
69912032Sandreas.sandberg@arm.comvoid
70011759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
70111759Sbrandon.potter@amd.com                 hst_statfs *host)
70211759Sbrandon.potter@amd.com{
70311759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
70411759Sbrandon.potter@amd.com
70511812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
70611812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
70711812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
70811759Sbrandon.potter@amd.com#else
70911812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
71011759Sbrandon.potter@amd.com#endif
71111759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
71211759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
71311759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
71411759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
71511759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
71611759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
71711812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
71811812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
71911812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
72011812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
72111812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
72211812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
72311812Sbaz21@cam.ac.uk#else
72411759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
72511759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
72611812Sbaz21@cam.ac.uk#endif
72711812Sbaz21@cam.ac.uk#if defined(__linux__)
72811759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
72911812Sbaz21@cam.ac.uk#else
73011812Sbaz21@cam.ac.uk    /*
73111812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
73211812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
73311812Sbaz21@cam.ac.uk     */
73411812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
73511812Sbaz21@cam.ac.uk#endif
73611759Sbrandon.potter@amd.com
73711759Sbrandon.potter@amd.com    tgt.copyOut(mem);
73811759Sbrandon.potter@amd.com}
73911759Sbrandon.potter@amd.com
740378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
741378SN/A/// only to find out if their stdout is a tty, to determine whether to
7429141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
7439141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
744360SN/Atemplate <class OS>
7451450SN/ASyscallReturn
74611856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
747360SN/A{
7486701Sgblack@eecs.umich.edu    int index = 0;
74911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
75011856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
751360SN/A
75210930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
753360SN/A
75411856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
75511856Sbrandon.potter@amd.com        return -ENOTTY;
75610496Ssteve.reinhardt@amd.com
75711856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
75811856Sbrandon.potter@amd.com    if (!dfdp)
7591458SN/A        return -EBADF;
760360SN/A
76111856Sbrandon.potter@amd.com    /**
76211856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
76311856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
76411856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
76511856Sbrandon.potter@amd.com     */
76611856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
76711856Sbrandon.potter@amd.com    if (emul_driver)
76811856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
76910496Ssteve.reinhardt@amd.com
77011856Sbrandon.potter@amd.com    /**
77111856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
77211856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
77311856Sbrandon.potter@amd.com     */
77411856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
77510930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
7769141Smarc.orr@gmail.com    return -ENOTTY;
777360SN/A}
778360SN/A
779360SN/Atemplate <class OS>
78011907SBrandon.Potter@amd.comSyscallReturn
78111907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
78211907SBrandon.Potter@amd.com         bool isopenat)
783360SN/A{
78411907SBrandon.Potter@amd.com    int index = 0;
78511907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
78611907SBrandon.Potter@amd.com
78711907SBrandon.Potter@amd.com    /**
78811907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
78911907SBrandon.Potter@amd.com     * descriptor from the simulated process.
79011907SBrandon.Potter@amd.com     */
79111907SBrandon.Potter@amd.com    if (isopenat)
79211907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
79311907SBrandon.Potter@amd.com
79411907SBrandon.Potter@amd.com    /**
79511907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
79611907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
79711907SBrandon.Potter@amd.com     */
798360SN/A    std::string path;
79911907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
8001458SN/A        return -EFAULT;
801360SN/A
80211907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
80311907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
80411907SBrandon.Potter@amd.com#else
80511907SBrandon.Potter@amd.com    int host_flags = 0;
80611907SBrandon.Potter@amd.com#endif
80711907SBrandon.Potter@amd.com    /**
80811907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
80911907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
81011907SBrandon.Potter@amd.com     */
81111907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
812360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
81311907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
81411907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
81511907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
816360SN/A        }
817360SN/A    }
81811907SBrandon.Potter@amd.com    if (tgt_flags) {
81911907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
82011907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
82111907SBrandon.Potter@amd.com    }
822360SN/A#ifdef __CYGWIN32__
82311907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
824360SN/A#endif
825360SN/A
82611907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
8273669Sbinkertn@umich.edu
82811907SBrandon.Potter@amd.com    /**
82911907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
83011907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
83111907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
83211907SBrandon.Potter@amd.com     * create a full path.
83311907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
83411907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
83511907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
83611907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
83711907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
83811907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
83911907SBrandon.Potter@amd.com     */
84013883Sdavid.hashe@amd.com    std::string redir_path = path;
84113883Sdavid.hashe@amd.com    std::string abs_path = path;
84213883Sdavid.hashe@amd.com    if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) {
84313883Sdavid.hashe@amd.com        abs_path = p->absolutePath(path, true);
84413883Sdavid.hashe@amd.com        redir_path = p->checkPathRedirect(path);
84511907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
84611907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
84711907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
84811907SBrandon.Potter@amd.com        if (!ffdp)
84911907SBrandon.Potter@amd.com            return -EBADF;
85013883Sdavid.hashe@amd.com        abs_path = ffdp->getFileName() + path;
85113883Sdavid.hashe@amd.com        redir_path = p->checkPathRedirect(abs_path);
85211907SBrandon.Potter@amd.com    }
8531706SN/A
85411907SBrandon.Potter@amd.com    /**
85511907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
85611907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
85711907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
85811907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
85911907SBrandon.Potter@amd.com     */
86013883Sdavid.hashe@amd.com    if (startswith(abs_path, "/dev/")) {
86113883Sdavid.hashe@amd.com        std::string filename = abs_path.substr(strlen("/dev/"));
86211907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
86311907SBrandon.Potter@amd.com        if (drv) {
86411907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
86511907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
86613883Sdavid.hashe@amd.com                            isopenat ? "at" : "", abs_path.c_str());
86711907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
86810496Ssteve.reinhardt@amd.com        }
86911907SBrandon.Potter@amd.com        /**
87011907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
87111907SBrandon.Potter@amd.com         * as /dev/zero
87211907SBrandon.Potter@amd.com         */
87310496Ssteve.reinhardt@amd.com    }
87410496Ssteve.reinhardt@amd.com
87511907SBrandon.Potter@amd.com    /**
87613883Sdavid.hashe@amd.com     * We make several attempts resolve a call to open.
87713883Sdavid.hashe@amd.com     *
87813883Sdavid.hashe@amd.com     * 1) Resolve any path redirection before hand. This will set the path
87913883Sdavid.hashe@amd.com     * up with variable 'redir_path' which may contain a modified path or
88013883Sdavid.hashe@amd.com     * the original path value. This should already be done in prior code.
88113883Sdavid.hashe@amd.com     * 2) Try to handle the access using 'special_paths'. Some special_paths
88213883Sdavid.hashe@amd.com     * and files cannot be called on the host and need to be handled as
88313883Sdavid.hashe@amd.com     * special cases inside the simulator. These special_paths are handled by
88413883Sdavid.hashe@amd.com     * C++ routines to provide output back to userspace.
88513883Sdavid.hashe@amd.com     * 3) If the full path that was created above does not match any of the
88613883Sdavid.hashe@amd.com     * special cases, pass it through to the open call on the __HOST__ to let
88713883Sdavid.hashe@amd.com     * the host open the file on our behalf. Again, the openImpl tries to
88813883Sdavid.hashe@amd.com     * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the
88913883Sdavid.hashe@amd.com     * faux-filesystem files). The faux-filesystem is dynamically created
89013883Sdavid.hashe@amd.com     * during simulator configuration using Python functions.
89113883Sdavid.hashe@amd.com     * 4) If the host cannot open the file, the open attempt failed in "3)".
89213883Sdavid.hashe@amd.com     * Return the host's error code back through the system call to the
89313883Sdavid.hashe@amd.com     * simulated process. If running a debug trace, also notify the user that
89413883Sdavid.hashe@amd.com     * the open call failed.
89513883Sdavid.hashe@amd.com     *
89613883Sdavid.hashe@amd.com     * Any success will set sim_fd to something other than -1 and skip the
89713883Sdavid.hashe@amd.com     * next conditions effectively bypassing them.
89811907SBrandon.Potter@amd.com     */
89911907SBrandon.Potter@amd.com    int sim_fd = -1;
90013883Sdavid.hashe@amd.com    std::string used_path;
90111907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
90213883Sdavid.hashe@amd.com            { "/proc/meminfo/", "/system/", "/sys/", "/platform/",
90313883Sdavid.hashe@amd.com              "/etc/passwd" };
90411907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
90513883Sdavid.hashe@amd.com        if (startswith(path, entry)) {
90613883Sdavid.hashe@amd.com            sim_fd = OS::openSpecialFile(abs_path, p, tc);
90713883Sdavid.hashe@amd.com            used_path = abs_path;
90813883Sdavid.hashe@amd.com        }
90911907SBrandon.Potter@amd.com    }
91011907SBrandon.Potter@amd.com    if (sim_fd == -1) {
91113883Sdavid.hashe@amd.com        sim_fd = open(redir_path.c_str(), host_flags, mode);
91213883Sdavid.hashe@amd.com        used_path = redir_path;
91311907SBrandon.Potter@amd.com    }
91411907SBrandon.Potter@amd.com    if (sim_fd == -1) {
91511907SBrandon.Potter@amd.com        int local = -errno;
91613883Sdavid.hashe@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s "
91713883Sdavid.hashe@amd.com                        "(inferred from:%s)\n", isopenat ? "at" : "",
91813883Sdavid.hashe@amd.com                        used_path.c_str(), path.c_str());
91911907SBrandon.Potter@amd.com        return local;
92011907SBrandon.Potter@amd.com    }
921360SN/A
92211907SBrandon.Potter@amd.com    /**
92311907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
92411907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
92511907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
92611907SBrandon.Potter@amd.com     * file descriptor.
92711907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
92811907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
92911907SBrandon.Potter@amd.com     */
93011907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
93111907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
93213883Sdavid.hashe@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n"
93313883Sdavid.hashe@amd.com                    "(inferred from:%s)\n", isopenat ? "at" : "",
93413883Sdavid.hashe@amd.com                    sim_fd, tgt_fd, used_path.c_str(), path.c_str());
93511907SBrandon.Potter@amd.com    return tgt_fd;
936360SN/A}
937360SN/A
93810027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
93910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
94010027SChris.Adeniyi-Jones@arm.comSyscallReturn
94111851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
94210027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
94310027SChris.Adeniyi-Jones@arm.com{
94411907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
94510027SChris.Adeniyi-Jones@arm.com}
94610027SChris.Adeniyi-Jones@arm.com
94710027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
94810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
94910027SChris.Adeniyi-Jones@arm.comSyscallReturn
95011851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
95111851Sbrandon.potter@amd.com           ThreadContext *tc)
95210027SChris.Adeniyi-Jones@arm.com{
95311907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
95410027SChris.Adeniyi-Jones@arm.com}
95510027SChris.Adeniyi-Jones@arm.com
95610633Smichaelupton@gmail.com/// Target unlinkat() handler.
95710633Smichaelupton@gmail.comtemplate <class OS>
95810633Smichaelupton@gmail.comSyscallReturn
95911851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
96010633Smichaelupton@gmail.com             ThreadContext *tc)
96110633Smichaelupton@gmail.com{
96210633Smichaelupton@gmail.com    int index = 0;
96310633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
96410633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
96510633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
96610633Smichaelupton@gmail.com
96710633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
96810633Smichaelupton@gmail.com}
96910633Smichaelupton@gmail.com
97010203SAli.Saidi@ARM.com/// Target facessat() handler
97110203SAli.Saidi@ARM.comtemplate <class OS>
97210203SAli.Saidi@ARM.comSyscallReturn
97311851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
97411851Sbrandon.potter@amd.com              ThreadContext *tc)
97510203SAli.Saidi@ARM.com{
97610203SAli.Saidi@ARM.com    int index = 0;
97710203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
97810203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
97910203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
98010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
98110203SAli.Saidi@ARM.com}
98210203SAli.Saidi@ARM.com
98310203SAli.Saidi@ARM.com/// Target readlinkat() handler
98410203SAli.Saidi@ARM.comtemplate <class OS>
98510203SAli.Saidi@ARM.comSyscallReturn
98611851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
98711851Sbrandon.potter@amd.com               ThreadContext *tc)
98810203SAli.Saidi@ARM.com{
98910203SAli.Saidi@ARM.com    int index = 0;
99010203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
99110203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
99210203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
99310203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
99410203SAli.Saidi@ARM.com}
99510203SAli.Saidi@ARM.com
99610850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
99710850SGiacomo.Gabrielli@arm.comtemplate <class OS>
99810850SGiacomo.Gabrielli@arm.comSyscallReturn
99911851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
100010850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
100110850SGiacomo.Gabrielli@arm.com{
100210850SGiacomo.Gabrielli@arm.com    int index = 0;
100310850SGiacomo.Gabrielli@arm.com
100410850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
100510850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
100610850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
100710850SGiacomo.Gabrielli@arm.com
100810850SGiacomo.Gabrielli@arm.com    std::string old_name;
100910850SGiacomo.Gabrielli@arm.com
101010850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
101110850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
101210850SGiacomo.Gabrielli@arm.com        return -EFAULT;
101310850SGiacomo.Gabrielli@arm.com
101410850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
101510850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
101610850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
101710850SGiacomo.Gabrielli@arm.com
101810850SGiacomo.Gabrielli@arm.com    std::string new_name;
101910850SGiacomo.Gabrielli@arm.com
102010850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
102110850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
102210850SGiacomo.Gabrielli@arm.com        return -EFAULT;
102310850SGiacomo.Gabrielli@arm.com
102413883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
102513883Sdavid.hashe@amd.com    old_name = process->checkPathRedirect(old_name);
102613883Sdavid.hashe@amd.com    new_name = process->checkPathRedirect(new_name);
102710850SGiacomo.Gabrielli@arm.com
102810850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
102910850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
103010850SGiacomo.Gabrielli@arm.com}
103110850SGiacomo.Gabrielli@arm.com
10326640Svince@csl.cornell.edu/// Target sysinfo() handler.
10336640Svince@csl.cornell.edutemplate <class OS>
10346640Svince@csl.cornell.eduSyscallReturn
103511851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
103611851Sbrandon.potter@amd.com            ThreadContext *tc)
10376640Svince@csl.cornell.edu{
10386640Svince@csl.cornell.edu
10396701Sgblack@eecs.umich.edu    int index = 0;
10406701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
104110793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
10426640Svince@csl.cornell.edu
104311758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
104411758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
104511758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
10466640Svince@csl.cornell.edu
10478706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
10486640Svince@csl.cornell.edu
10496701Sgblack@eecs.umich.edu    return 0;
10506640Svince@csl.cornell.edu}
1051360SN/A
10521999SN/A/// Target chmod() handler.
10531999SN/Atemplate <class OS>
10541999SN/ASyscallReturn
105511851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
10562680Sktlim@umich.edu          ThreadContext *tc)
10571999SN/A{
10581999SN/A    std::string path;
10591999SN/A
10606701Sgblack@eecs.umich.edu    int index = 0;
10618852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10626701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10631999SN/A        return -EFAULT;
10646701Sgblack@eecs.umich.edu    }
10651999SN/A
10666701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
10671999SN/A    mode_t hostMode = 0;
10681999SN/A
10691999SN/A    // XXX translate mode flags via OS::something???
10701999SN/A    hostMode = mode;
10711999SN/A
107213883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
107313883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
10743669Sbinkertn@umich.edu
10751999SN/A    // do the chmod
10761999SN/A    int result = chmod(path.c_str(), hostMode);
10771999SN/A    if (result < 0)
10782218SN/A        return -errno;
10791999SN/A
10801999SN/A    return 0;
10811999SN/A}
10821999SN/A
108313570Sbrandon.potter@amd.comtemplate <class OS>
108413570Sbrandon.potter@amd.comSyscallReturn
108513570Sbrandon.potter@amd.compollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
108613570Sbrandon.potter@amd.com{
108713570Sbrandon.potter@amd.com    int index = 0;
108813570Sbrandon.potter@amd.com    Addr fdsPtr = p->getSyscallArg(tc, index);
108913570Sbrandon.potter@amd.com    int nfds = p->getSyscallArg(tc, index);
109013570Sbrandon.potter@amd.com    int tmout = p->getSyscallArg(tc, index);
109113570Sbrandon.potter@amd.com
109213570Sbrandon.potter@amd.com    BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds);
109313570Sbrandon.potter@amd.com    fdsBuf.copyIn(tc->getMemProxy());
109413570Sbrandon.potter@amd.com
109513570Sbrandon.potter@amd.com    /**
109613570Sbrandon.potter@amd.com     * Record the target file descriptors in a local variable. We need to
109713570Sbrandon.potter@amd.com     * replace them with host file descriptors but we need a temporary copy
109813570Sbrandon.potter@amd.com     * for later. Afterwards, replace each target file descriptor in the
109913570Sbrandon.potter@amd.com     * poll_fd array with its host_fd.
110013570Sbrandon.potter@amd.com     */
110113570Sbrandon.potter@amd.com    int temp_tgt_fds[nfds];
110213570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
110313570Sbrandon.potter@amd.com        temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd;
110413570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
110513570Sbrandon.potter@amd.com        auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
110613570Sbrandon.potter@amd.com        if (!hbfdp)
110713570Sbrandon.potter@amd.com            return -EBADF;
110813570Sbrandon.potter@amd.com        auto host_fd = hbfdp->getSimFD();
110913570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd;
111013570Sbrandon.potter@amd.com    }
111113570Sbrandon.potter@amd.com
111213570Sbrandon.potter@amd.com    /**
111313570Sbrandon.potter@amd.com     * We cannot allow an infinite poll to occur or it will inevitably cause
111413570Sbrandon.potter@amd.com     * a deadlock in the gem5 simulator with clone. We must pass in tmout with
111513570Sbrandon.potter@amd.com     * a non-negative value, however it also makes no sense to poll on the
111613570Sbrandon.potter@amd.com     * underlying host for any other time than tmout a zero timeout.
111713570Sbrandon.potter@amd.com     */
111813570Sbrandon.potter@amd.com    int status;
111913570Sbrandon.potter@amd.com    if (tmout < 0) {
112013570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
112113570Sbrandon.potter@amd.com        if (status == 0) {
112213570Sbrandon.potter@amd.com            /**
112313570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
112413570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try
112513570Sbrandon.potter@amd.com             * to return the signal interrupt instead.
112613570Sbrandon.potter@amd.com             */
112713570Sbrandon.potter@amd.com            System *sysh = tc->getSystemPtr();
112813570Sbrandon.potter@amd.com            std::list<BasicSignal>::iterator it;
112913570Sbrandon.potter@amd.com            for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++)
113013570Sbrandon.potter@amd.com                if (it->receiver == p)
113113570Sbrandon.potter@amd.com                    return -EINTR;
113213570Sbrandon.potter@amd.com            return SyscallReturn::retry();
113313570Sbrandon.potter@amd.com        }
113413570Sbrandon.potter@amd.com    } else
113513570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
113613570Sbrandon.potter@amd.com
113713570Sbrandon.potter@amd.com    if (status == -1)
113813570Sbrandon.potter@amd.com        return -errno;
113913570Sbrandon.potter@amd.com
114013570Sbrandon.potter@amd.com    /**
114113570Sbrandon.potter@amd.com     * Replace each host_fd in the returned poll_fd array with its original
114213570Sbrandon.potter@amd.com     * target file descriptor.
114313570Sbrandon.potter@amd.com     */
114413570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
114513570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
114613570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd;
114713570Sbrandon.potter@amd.com    }
114813570Sbrandon.potter@amd.com
114913570Sbrandon.potter@amd.com    /**
115013570Sbrandon.potter@amd.com     * Copy out the pollfd struct because the host may have updated fields
115113570Sbrandon.potter@amd.com     * in the structure.
115213570Sbrandon.potter@amd.com     */
115313570Sbrandon.potter@amd.com    fdsBuf.copyOut(tc->getMemProxy());
115413570Sbrandon.potter@amd.com
115513570Sbrandon.potter@amd.com    return status;
115613570Sbrandon.potter@amd.com}
11571999SN/A
11581999SN/A/// Target fchmod() handler.
11591999SN/Atemplate <class OS>
11601999SN/ASyscallReturn
116111856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
11621999SN/A{
11636701Sgblack@eecs.umich.edu    int index = 0;
116411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
116511856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
116610931Sbrandon.potter@amd.com
116711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
116811856Sbrandon.potter@amd.com    if (!ffdp)
11691999SN/A        return -EBADF;
117011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
11711999SN/A
117211856Sbrandon.potter@amd.com    mode_t hostMode = mode;
11731999SN/A
117411856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
11751999SN/A
117611856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
11771999SN/A}
11781999SN/A
11795877Shsul@eecs.umich.edu/// Target mremap() handler.
11805877Shsul@eecs.umich.edutemplate <class OS>
11815877Shsul@eecs.umich.eduSyscallReturn
118211851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
11835877Shsul@eecs.umich.edu{
11846701Sgblack@eecs.umich.edu    int index = 0;
11856701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
11866701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
11876701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
11886701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
118910027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
119010027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
119110027SChris.Adeniyi-Jones@arm.com
119210027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
119310027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
11945877Shsul@eecs.umich.edu
119510318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
119610318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
11975877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
11985877Shsul@eecs.umich.edu        return -EINVAL;
11995877Shsul@eecs.umich.edu    }
12005877Shsul@eecs.umich.edu
120110486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
120210486Stjablin@gmail.com
12035877Shsul@eecs.umich.edu    if (new_length > old_length) {
120411905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
120511905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
120611905SBrandon.Potter@amd.com
120711905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
120810027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
120912206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
121012206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
12115877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
121211905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
121311905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
12145877Shsul@eecs.umich.edu            return start;
12155877Shsul@eecs.umich.edu        } else {
121610027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
12175877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
12185877Shsul@eecs.umich.edu                return -ENOMEM;
12195877Shsul@eecs.umich.edu            } else {
122012206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
122112206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
122212206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
122312206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
122412206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
122512206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
122612206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
122712206Srico.amslinger@informatik.uni-augsburg.de                }
122812206Srico.amslinger@informatik.uni-augsburg.de
122910027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
123010027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
123110027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
123210027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
12335877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
123410027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
123510027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
123610027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
123710027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
123812206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
123912206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
124012206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
124112206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
124210027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
124310027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
124410027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
124510027SChris.Adeniyi-Jones@arm.com                }
124610027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
124710027SChris.Adeniyi-Jones@arm.com                return new_start;
12485877Shsul@eecs.umich.edu            }
12495877Shsul@eecs.umich.edu        }
12505877Shsul@eecs.umich.edu    } else {
125110027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
125210027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
12538601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
125410027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
12555877Shsul@eecs.umich.edu    }
12565877Shsul@eecs.umich.edu}
12571999SN/A
1258378SN/A/// Target stat() handler.
1259360SN/Atemplate <class OS>
12601450SN/ASyscallReturn
126111851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
12622680Sktlim@umich.edu         ThreadContext *tc)
1263360SN/A{
1264360SN/A    std::string path;
1265360SN/A
12666701Sgblack@eecs.umich.edu    int index = 0;
12678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12686701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12696701Sgblack@eecs.umich.edu        return -EFAULT;
12706701Sgblack@eecs.umich.edu    }
12716701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1272360SN/A
127313883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
127413883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
12753669Sbinkertn@umich.edu
1276360SN/A    struct stat hostBuf;
1277360SN/A    int result = stat(path.c_str(), &hostBuf);
1278360SN/A
1279360SN/A    if (result < 0)
12802218SN/A        return -errno;
1281360SN/A
12828706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1283360SN/A
12841458SN/A    return 0;
1285360SN/A}
1286360SN/A
1287360SN/A
12885074Ssaidi@eecs.umich.edu/// Target stat64() handler.
12895074Ssaidi@eecs.umich.edutemplate <class OS>
12905074Ssaidi@eecs.umich.eduSyscallReturn
129111851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
12925074Ssaidi@eecs.umich.edu           ThreadContext *tc)
12935074Ssaidi@eecs.umich.edu{
12945074Ssaidi@eecs.umich.edu    std::string path;
12955074Ssaidi@eecs.umich.edu
12966701Sgblack@eecs.umich.edu    int index = 0;
12978852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12986701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
12995074Ssaidi@eecs.umich.edu        return -EFAULT;
13006701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
13015074Ssaidi@eecs.umich.edu
130213883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
130313883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
13045074Ssaidi@eecs.umich.edu
13055208Ssaidi@eecs.umich.edu#if NO_STAT64
13065208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
13075208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
13085208Ssaidi@eecs.umich.edu#else
13095074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
13105074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
13115208Ssaidi@eecs.umich.edu#endif
13125074Ssaidi@eecs.umich.edu
13135074Ssaidi@eecs.umich.edu    if (result < 0)
13145074Ssaidi@eecs.umich.edu        return -errno;
13155074Ssaidi@eecs.umich.edu
13168706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
13175074Ssaidi@eecs.umich.edu
13185074Ssaidi@eecs.umich.edu    return 0;
13195074Ssaidi@eecs.umich.edu}
13205074Ssaidi@eecs.umich.edu
13215074Ssaidi@eecs.umich.edu
132210027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
132310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
132410027SChris.Adeniyi-Jones@arm.comSyscallReturn
132511851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
132610027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
132710027SChris.Adeniyi-Jones@arm.com{
132810027SChris.Adeniyi-Jones@arm.com    int index = 0;
132910027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
133010027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
133110793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
133210027SChris.Adeniyi-Jones@arm.com
133310027SChris.Adeniyi-Jones@arm.com    std::string path;
133410027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
133510027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
133610027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
133710027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
133810027SChris.Adeniyi-Jones@arm.com
133913883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
134013883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
134110027SChris.Adeniyi-Jones@arm.com
134210027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
134310027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
134410027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
134510027SChris.Adeniyi-Jones@arm.com#else
134610027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
134710027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
134810027SChris.Adeniyi-Jones@arm.com#endif
134910027SChris.Adeniyi-Jones@arm.com
135010027SChris.Adeniyi-Jones@arm.com    if (result < 0)
135110027SChris.Adeniyi-Jones@arm.com        return -errno;
135210027SChris.Adeniyi-Jones@arm.com
135310027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
135410027SChris.Adeniyi-Jones@arm.com
135510027SChris.Adeniyi-Jones@arm.com    return 0;
135610027SChris.Adeniyi-Jones@arm.com}
135710027SChris.Adeniyi-Jones@arm.com
135810027SChris.Adeniyi-Jones@arm.com
13591999SN/A/// Target fstat64() handler.
13601999SN/Atemplate <class OS>
13611999SN/ASyscallReturn
136211856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
13631999SN/A{
13646701Sgblack@eecs.umich.edu    int index = 0;
136511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
136611856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
136710931Sbrandon.potter@amd.com
136811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
136911856Sbrandon.potter@amd.com    if (!ffdp)
13701999SN/A        return -EBADF;
137111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
13721999SN/A
13732764Sstever@eecs.umich.edu#if NO_STAT64
13742064SN/A    struct stat  hostBuf;
137510931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
13762064SN/A#else
13772064SN/A    struct stat64  hostBuf;
137810931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
13792064SN/A#endif
13801999SN/A
13811999SN/A    if (result < 0)
13822218SN/A        return -errno;
13831999SN/A
138410931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
13851999SN/A
13861999SN/A    return 0;
13871999SN/A}
13881999SN/A
13891999SN/A
1390378SN/A/// Target lstat() handler.
1391360SN/Atemplate <class OS>
13921450SN/ASyscallReturn
139311851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
13942680Sktlim@umich.edu          ThreadContext *tc)
1395360SN/A{
1396360SN/A    std::string path;
1397360SN/A
13986701Sgblack@eecs.umich.edu    int index = 0;
13998852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14006701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14016701Sgblack@eecs.umich.edu        return -EFAULT;
14026701Sgblack@eecs.umich.edu    }
14036701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1404360SN/A
140513883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
140613883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
14073669Sbinkertn@umich.edu
1408360SN/A    struct stat hostBuf;
1409360SN/A    int result = lstat(path.c_str(), &hostBuf);
1410360SN/A
1411360SN/A    if (result < 0)
14121458SN/A        return -errno;
1413360SN/A
14148706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1415360SN/A
14161458SN/A    return 0;
1417360SN/A}
1418360SN/A
14191999SN/A/// Target lstat64() handler.
14201999SN/Atemplate <class OS>
14211999SN/ASyscallReturn
142211851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
14232680Sktlim@umich.edu            ThreadContext *tc)
14241999SN/A{
14251999SN/A    std::string path;
14261999SN/A
14276701Sgblack@eecs.umich.edu    int index = 0;
14288852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14296701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14306701Sgblack@eecs.umich.edu        return -EFAULT;
14316701Sgblack@eecs.umich.edu    }
14326701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
14331999SN/A
143413883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
143513883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
14363669Sbinkertn@umich.edu
14372764Sstever@eecs.umich.edu#if NO_STAT64
14382064SN/A    struct stat hostBuf;
14392064SN/A    int result = lstat(path.c_str(), &hostBuf);
14402064SN/A#else
14411999SN/A    struct stat64 hostBuf;
14421999SN/A    int result = lstat64(path.c_str(), &hostBuf);
14432064SN/A#endif
14441999SN/A
14451999SN/A    if (result < 0)
14461999SN/A        return -errno;
14471999SN/A
14488706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14491999SN/A
14501999SN/A    return 0;
14511999SN/A}
14521999SN/A
1453378SN/A/// Target fstat() handler.
1454360SN/Atemplate <class OS>
14551450SN/ASyscallReturn
145611856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1457360SN/A{
14586701Sgblack@eecs.umich.edu    int index = 0;
145911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
146011856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1461360SN/A
146211380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1463360SN/A
146411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
146511856Sbrandon.potter@amd.com    if (!ffdp)
14661458SN/A        return -EBADF;
146711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1468360SN/A
1469360SN/A    struct stat hostBuf;
147010931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1471360SN/A
1472360SN/A    if (result < 0)
14731458SN/A        return -errno;
1474360SN/A
147510931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
14762021SN/A
14771458SN/A    return 0;
1478360SN/A}
1479360SN/A
14801706SN/A/// Target statfs() handler.
14811706SN/Atemplate <class OS>
14821706SN/ASyscallReturn
148311851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
14842680Sktlim@umich.edu           ThreadContext *tc)
14851706SN/A{
148611799Sbrandon.potter@amd.com#if NO_STATFS
148711799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
148811799Sbrandon.potter@amd.com#else
14891706SN/A    std::string path;
14901706SN/A
14916701Sgblack@eecs.umich.edu    int index = 0;
14928852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
14936701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
14946701Sgblack@eecs.umich.edu        return -EFAULT;
14956701Sgblack@eecs.umich.edu    }
14966701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
14971706SN/A
149813883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
149913883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
15003669Sbinkertn@umich.edu
15011706SN/A    struct statfs hostBuf;
15021706SN/A    int result = statfs(path.c_str(), &hostBuf);
15031706SN/A
15041706SN/A    if (result < 0)
15052218SN/A        return -errno;
15061706SN/A
150711759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
150811799Sbrandon.potter@amd.com#endif
15091706SN/A    return 0;
15101706SN/A}
15111706SN/A
151211886Sbrandon.potter@amd.comtemplate <class OS>
151311886Sbrandon.potter@amd.comSyscallReturn
151411886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
151511886Sbrandon.potter@amd.com{
151611886Sbrandon.potter@amd.com    int index = 0;
151712426Sqtt2@cornell.edu
151813557Sgabeblack@google.com    RegVal flags = p->getSyscallArg(tc, index);
151913557Sgabeblack@google.com    RegVal newStack = p->getSyscallArg(tc, index);
152011886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
152112426Sqtt2@cornell.edu
152213534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
152312426Sqtt2@cornell.edu    /**
152413534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
152512426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
152612426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
152712426Sqtt2@cornell.edu     */
152813536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
152912426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
153012426Sqtt2@cornell.edu#else
153111886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
153213536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
153312426Sqtt2@cornell.edu#endif
153411886Sbrandon.potter@amd.com
153511886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
153611886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
153711886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
153811886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
153911886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
154011886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
154111886Sbrandon.potter@amd.com        return -EINVAL;
154211886Sbrandon.potter@amd.com
154311886Sbrandon.potter@amd.com    ThreadContext *ctc;
154413649Sqtt2@cornell.edu    if (!(ctc = p->findFreeContext())) {
154513649Sqtt2@cornell.edu        DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system"
154613649Sqtt2@cornell.edu                        "[cpu %d, thread %d]", tc->cpuId(), tc->threadId());
154713649Sqtt2@cornell.edu        return -EAGAIN;
154813649Sqtt2@cornell.edu    }
154911886Sbrandon.potter@amd.com
155011886Sbrandon.potter@amd.com    /**
155111886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
155211886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
155311886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
155411886Sbrandon.potter@amd.com     * constructor.
155511886Sbrandon.potter@amd.com     */
155611886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
155711886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
155811886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
155911886Sbrandon.potter@amd.com    pp->system = p->system;
156013883Sdavid.hashe@amd.com    pp->cwd.assign(p->tgtCwd);
156111886Sbrandon.potter@amd.com    pp->input.assign("stdin");
156211886Sbrandon.potter@amd.com    pp->output.assign("stdout");
156311886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
156411886Sbrandon.potter@amd.com    pp->uid = p->uid();
156511886Sbrandon.potter@amd.com    pp->euid = p->euid();
156611886Sbrandon.potter@amd.com    pp->gid = p->gid();
156711886Sbrandon.potter@amd.com    pp->egid = p->egid();
156811886Sbrandon.potter@amd.com
156911886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
157011886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
157111886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
157211886Sbrandon.potter@amd.com    do {
157311886Sbrandon.potter@amd.com        temp_pid++;
157411886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
157511886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
157611886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
157711886Sbrandon.potter@amd.com
157811886Sbrandon.potter@amd.com    pp->pid = temp_pid;
157911886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
158013867Salexandru.dutu@amd.com    pp->useArchPT = p->useArchPT;
158113867Salexandru.dutu@amd.com    pp->kvmInSE = p->kvmInSE;
158211886Sbrandon.potter@amd.com    Process *cp = pp->create();
158311886Sbrandon.potter@amd.com    delete pp;
158411886Sbrandon.potter@amd.com
158511886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
158611886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
158711886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
158811886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
158911886Sbrandon.potter@amd.com
159011886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
159111886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
159211886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
159311886Sbrandon.potter@amd.com        *ptid = cp->pid();
159411886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
159511886Sbrandon.potter@amd.com    }
159611886Sbrandon.potter@amd.com
159713867Salexandru.dutu@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
159813867Salexandru.dutu@amd.com        cp->pTable->shared = true;
159913867Salexandru.dutu@amd.com        cp->useForClone = true;
160013867Salexandru.dutu@amd.com    }
160111886Sbrandon.potter@amd.com    cp->initState();
160211886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
160311886Sbrandon.potter@amd.com
160411911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
160511911SBrandon.Potter@amd.com        delete cp->sigchld;
160611911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
160711911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
160811911SBrandon.Potter@amd.com        *cp->sigchld = true;
160911911SBrandon.Potter@amd.com    }
161011911SBrandon.Potter@amd.com
161111886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
161211886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
161311886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
161411886Sbrandon.potter@amd.com        *ctid = cp->pid();
161511886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
161611886Sbrandon.potter@amd.com    }
161711886Sbrandon.potter@amd.com
161811886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
161911886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
162011886Sbrandon.potter@amd.com
162111886Sbrandon.potter@amd.com    ctc->clearArchRegs();
162211886Sbrandon.potter@amd.com
162313536Sandreas.sandberg@arm.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
162411886Sbrandon.potter@amd.com
162511886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
162611886Sbrandon.potter@amd.com
162711886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
162811886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
162911886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
163011886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
163111886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
163211886Sbrandon.potter@amd.com#endif
163311886Sbrandon.potter@amd.com
163413867Salexandru.dutu@amd.com    if (p->kvmInSE) {
163513867Salexandru.dutu@amd.com#if THE_ISA == X86_ISA
163613867Salexandru.dutu@amd.com        ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX));
163713867Salexandru.dutu@amd.com#else
163813867Salexandru.dutu@amd.com        panic("KVM CPU model is not supported for this ISA");
163913867Salexandru.dutu@amd.com#endif
164013867Salexandru.dutu@amd.com    } else {
164113867Salexandru.dutu@amd.com        TheISA::PCState cpc = tc->pcState();
164213867Salexandru.dutu@amd.com        cpc.advance();
164313867Salexandru.dutu@amd.com        ctc->pcState(cpc);
164413867Salexandru.dutu@amd.com    }
164511886Sbrandon.potter@amd.com    ctc->activate();
164611886Sbrandon.potter@amd.com
164711886Sbrandon.potter@amd.com    return cp->pid();
164811886Sbrandon.potter@amd.com}
16491706SN/A
16501706SN/A/// Target fstatfs() handler.
16511706SN/Atemplate <class OS>
16521706SN/ASyscallReturn
165311856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
16541706SN/A{
16556701Sgblack@eecs.umich.edu    int index = 0;
165611856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
165711856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
16581706SN/A
165911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
166011856Sbrandon.potter@amd.com    if (!ffdp)
16611706SN/A        return -EBADF;
166211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
16631706SN/A
16641706SN/A    struct statfs hostBuf;
166510931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
16661706SN/A
16671706SN/A    if (result < 0)
16682218SN/A        return -errno;
16691706SN/A
167011759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
16711706SN/A
16721706SN/A    return 0;
16731706SN/A}
16741706SN/A
167513572Sbrandon.potter@amd.com/// Target readv() handler.
167613572Sbrandon.potter@amd.comtemplate <class OS>
167713572Sbrandon.potter@amd.comSyscallReturn
167813572Sbrandon.potter@amd.comreadvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
167913572Sbrandon.potter@amd.com{
168013572Sbrandon.potter@amd.com    int index = 0;
168113572Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
168213572Sbrandon.potter@amd.com
168313572Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
168413572Sbrandon.potter@amd.com    if (!ffdp)
168513572Sbrandon.potter@amd.com        return -EBADF;
168613572Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
168713572Sbrandon.potter@amd.com
168813572Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
168913572Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
169013572Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
169113572Sbrandon.potter@amd.com    typename OS::tgt_iovec tiov[count];
169213572Sbrandon.potter@amd.com    struct iovec hiov[count];
169313572Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
169413572Sbrandon.potter@amd.com        prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)),
169513572Sbrandon.potter@amd.com                      (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec));
169613572Sbrandon.potter@amd.com        hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len);
169713572Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
169813572Sbrandon.potter@amd.com    }
169913572Sbrandon.potter@amd.com
170013572Sbrandon.potter@amd.com    int result = readv(sim_fd, hiov, count);
170113572Sbrandon.potter@amd.com    int local_errno = errno;
170213572Sbrandon.potter@amd.com
170313572Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
170413572Sbrandon.potter@amd.com        if (result != -1) {
170513572Sbrandon.potter@amd.com            prox.writeBlob(TheISA::htog(tiov[i].iov_base),
170613572Sbrandon.potter@amd.com                           (uint8_t*)hiov[i].iov_base, hiov[i].iov_len);
170713572Sbrandon.potter@amd.com        }
170813572Sbrandon.potter@amd.com        delete [] (char *)hiov[i].iov_base;
170913572Sbrandon.potter@amd.com    }
171013572Sbrandon.potter@amd.com
171113572Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : result;
171213572Sbrandon.potter@amd.com}
17131706SN/A
17141999SN/A/// Target writev() handler.
17151999SN/Atemplate <class OS>
17161999SN/ASyscallReturn
171711856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
17181999SN/A{
17196701Sgblack@eecs.umich.edu    int index = 0;
172011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
172110931Sbrandon.potter@amd.com
172211856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
172311856Sbrandon.potter@amd.com    if (!hbfdp)
17241999SN/A        return -EBADF;
172511856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
17261999SN/A
172711856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
172811856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
172911856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
17301999SN/A    struct iovec hiov[count];
17316227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
17321999SN/A        typename OS::tgt_iovec tiov;
17332461SN/A
173411856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
173511856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
17368737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
17371999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
173811856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
173911856Sbrandon.potter@amd.com                      hiov[i].iov_len);
17401999SN/A    }
17411999SN/A
174210931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
17431999SN/A
17446227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
17451999SN/A        delete [] (char *)hiov[i].iov_base;
17461999SN/A
174713572Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
17481999SN/A}
17491999SN/A
175011385Sbrandon.potter@amd.com/// Real mmap handler.
1751360SN/Atemplate <class OS>
17521450SN/ASyscallReturn
175311851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
175411385Sbrandon.potter@amd.com         bool is_mmap2)
1755360SN/A{
17566701Sgblack@eecs.umich.edu    int index = 0;
17576701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
17586701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
175911383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
176011383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
17618324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
176210486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1763360SN/A
176411385Sbrandon.potter@amd.com    if (is_mmap2)
176511385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
17669008Sgblack@eecs.umich.edu
176711383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
176811383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
176911383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
177011383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
177111383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
177211383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
177311383Sbrandon.potter@amd.com        !length) {
177411383Sbrandon.potter@amd.com        return -EINVAL;
177511383Sbrandon.potter@amd.com    }
17768324Ssteve.reinhardt@amd.com
177711383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
177811383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
177911383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
178011383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
178111383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
178211383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
178311383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
178411383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
178511383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
178611383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
178711383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
178811383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
178911383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
179011383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
179111383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
179211383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
179311383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
179411383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
179511383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
179611383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
179711383Sbrandon.potter@amd.com        // than we currently maintain.
179811383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
179911383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
180011383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
18018324Ssteve.reinhardt@amd.com    }
18025877Shsul@eecs.umich.edu
180310486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
180410486Stjablin@gmail.com
180511383Sbrandon.potter@amd.com    int sim_fd = -1;
180611383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
180711383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
180811856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
180911624Smichael.lebeane@amd.com
181011856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
181111856Sbrandon.potter@amd.com        if (dfdp) {
181211856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
181311856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
181411624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
181511624Smichael.lebeane@amd.com        }
181611624Smichael.lebeane@amd.com
181711856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
181811856Sbrandon.potter@amd.com        if (!ffdp)
181911383Sbrandon.potter@amd.com            return -EBADF;
182011856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1821360SN/A
182211913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
182311383Sbrandon.potter@amd.com                                    sim_fd, offset);
18248600Ssteve.reinhardt@amd.com
182511383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
182611383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
182711383Sbrandon.potter@amd.com            return -errno;
18288600Ssteve.reinhardt@amd.com        }
18292544SN/A    }
18302544SN/A
183111383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
183211383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
183311383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
183411905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
183511905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
183611905SBrandon.Potter@amd.com
183711905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
183811905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
183911905SBrandon.Potter@amd.com
184011905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
184111383Sbrandon.potter@amd.com    }
184211383Sbrandon.potter@amd.com
184311383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
184411383Sbrandon.potter@amd.com                    start, start + length - 1);
184511383Sbrandon.potter@amd.com
184611383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
184711383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
184811383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
184911383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
185011383Sbrandon.potter@amd.com    if (clobber) {
185111383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
185211383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
185311383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
185411383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
185511383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
18568600Ssteve.reinhardt@amd.com        }
18576672Sgblack@eecs.umich.edu    }
18588600Ssteve.reinhardt@amd.com
185911383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
186011383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
186111383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
18628601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
18632544SN/A
186411383Sbrandon.potter@amd.com    // Transfer content into target address space.
186511383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
186611383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
186711383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
186811383Sbrandon.potter@amd.com        // with something like:
186911383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
187011383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
187111383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
187211383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
187311383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
187411383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
187511383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
187611383Sbrandon.potter@amd.com    } else {
187711383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
187811383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
187911383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
188011383Sbrandon.potter@amd.com        // the host into the target address space.
188111383Sbrandon.potter@amd.com        struct stat file_stat;
188211383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
188311383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
188411383Sbrandon.potter@amd.com
188511383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
188611383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
188711383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
188811383Sbrandon.potter@amd.com        // on the specified offset into the file.
188911383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
189011383Sbrandon.potter@amd.com                                 length);
189111383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
189211383Sbrandon.potter@amd.com
189311383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
189411383Sbrandon.potter@amd.com        munmap(pmap, length);
189511383Sbrandon.potter@amd.com
189611392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
189711392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
189811392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
189911392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
190011392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
190111392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
190211392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
190311392Sbrandon.potter@amd.com        if (interpreter) {
190411392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
190511392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
190611392Sbrandon.potter@amd.com
190711392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
190811392Sbrandon.potter@amd.com
190911392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
191011856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
191111856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
191211856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
191311392Sbrandon.potter@amd.com
191411392Sbrandon.potter@amd.com                if (lib) {
191511392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
191611392Sbrandon.potter@amd.com                                        lib->textBase(), start);
191711392Sbrandon.potter@amd.com                }
191811392Sbrandon.potter@amd.com            }
191911392Sbrandon.potter@amd.com        }
192011392Sbrandon.potter@amd.com
192111383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
192211383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
192311383Sbrandon.potter@amd.com        // execution (hopefully).
192411383Sbrandon.potter@amd.com    }
192511383Sbrandon.potter@amd.com
19261458SN/A    return start;
1927360SN/A}
1928360SN/A
192911593Santhony.gutierrez@amd.comtemplate <class OS>
193011593Santhony.gutierrez@amd.comSyscallReturn
193111851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
193211593Santhony.gutierrez@amd.com{
193311593Santhony.gutierrez@amd.com    int index = 0;
193411593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
193511593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
193611593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
193711593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
193811593Santhony.gutierrez@amd.com
193911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
194011856Sbrandon.potter@amd.com    if (!ffdp)
194111593Santhony.gutierrez@amd.com        return -EBADF;
194211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
194311593Santhony.gutierrez@amd.com
194411593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
194511593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
194611593Santhony.gutierrez@amd.com
194711594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
194811593Santhony.gutierrez@amd.com
194911593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
195011593Santhony.gutierrez@amd.com}
195111593Santhony.gutierrez@amd.com
195211385Sbrandon.potter@amd.com/// Target mmap() handler.
195311385Sbrandon.potter@amd.comtemplate <class OS>
195411385Sbrandon.potter@amd.comSyscallReturn
195511851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
195611385Sbrandon.potter@amd.com{
195711385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
195811385Sbrandon.potter@amd.com}
195911385Sbrandon.potter@amd.com
196011385Sbrandon.potter@amd.com/// Target mmap2() handler.
196111385Sbrandon.potter@amd.comtemplate <class OS>
196211385Sbrandon.potter@amd.comSyscallReturn
196311851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
196411385Sbrandon.potter@amd.com{
196511385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
196611385Sbrandon.potter@amd.com}
196711385Sbrandon.potter@amd.com
1968378SN/A/// Target getrlimit() handler.
1969360SN/Atemplate <class OS>
19701450SN/ASyscallReturn
197111851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
197211851Sbrandon.potter@amd.com              ThreadContext *tc)
1973360SN/A{
19746701Sgblack@eecs.umich.edu    int index = 0;
19756701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
19766701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1977360SN/A
1978360SN/A    switch (resource) {
197911906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
198011906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
198111906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
198211906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
198311906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
198411906SBrandon.Potter@amd.com        break;
1985360SN/A
198611906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
198711906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
198811906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
198911906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
199011906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
199111906SBrandon.Potter@amd.com        break;
19925877Shsul@eecs.umich.edu
199311906SBrandon.Potter@amd.com      default:
199411906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
199511906SBrandon.Potter@amd.com        return -EINVAL;
199611906SBrandon.Potter@amd.com        break;
1997360SN/A    }
1998360SN/A
19998706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
20001458SN/A    return 0;
2001360SN/A}
2002360SN/A
200312235Sar4jc@virginia.edutemplate <class OS>
200412235Sar4jc@virginia.eduSyscallReturn
200512235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
200612235Sar4jc@virginia.edu            ThreadContext *tc)
200712235Sar4jc@virginia.edu{
200812235Sar4jc@virginia.edu    int index = 0;
200912235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
201012235Sar4jc@virginia.edu    {
201112235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
201212235Sar4jc@virginia.edu        return -EPERM;
201312235Sar4jc@virginia.edu    }
201412235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
201512235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
201612235Sar4jc@virginia.edu    if (n != 0)
201712235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
201812235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
201912235Sar4jc@virginia.edu    if (o != 0)
202012235Sar4jc@virginia.edu    {
202112416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
202212235Sar4jc@virginia.edu        switch (resource) {
202312235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
202412235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
202512235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
202612235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
202712235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
202812235Sar4jc@virginia.edu            break;
202912235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
203012235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
203112235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
203212235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
203312235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
203412593Sjason@lowepower.com            break;
203512235Sar4jc@virginia.edu          default:
203612235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
203712235Sar4jc@virginia.edu            return -EINVAL;
203812235Sar4jc@virginia.edu            break;
203912235Sar4jc@virginia.edu        }
204012235Sar4jc@virginia.edu        rlp.copyOut(tc->getMemProxy());
204112235Sar4jc@virginia.edu    }
204212235Sar4jc@virginia.edu    return 0;
204312235Sar4jc@virginia.edu}
204412235Sar4jc@virginia.edu
204510796Sbrandon.potter@amd.com/// Target clock_gettime() function.
204610796Sbrandon.potter@amd.comtemplate <class OS>
204710796Sbrandon.potter@amd.comSyscallReturn
204811851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
204910796Sbrandon.potter@amd.com{
205010796Sbrandon.potter@amd.com    int index = 1;
205110796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
205210796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
205310796Sbrandon.potter@amd.com
205410796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
205510796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
205610796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
205710796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
205810796Sbrandon.potter@amd.com
205910796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
206010796Sbrandon.potter@amd.com
206110796Sbrandon.potter@amd.com    return 0;
206210796Sbrandon.potter@amd.com}
206310796Sbrandon.potter@amd.com
206411337SMichael.Lebeane@amd.com/// Target clock_getres() function.
206511337SMichael.Lebeane@amd.comtemplate <class OS>
206611337SMichael.Lebeane@amd.comSyscallReturn
206711851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
206811337SMichael.Lebeane@amd.com{
206911337SMichael.Lebeane@amd.com    int index = 1;
207011337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
207111337SMichael.Lebeane@amd.com
207211337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
207311337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
207411337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
207511337SMichael.Lebeane@amd.com
207611337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
207711337SMichael.Lebeane@amd.com
207811337SMichael.Lebeane@amd.com    return 0;
207911337SMichael.Lebeane@amd.com}
208011337SMichael.Lebeane@amd.com
2081378SN/A/// Target gettimeofday() handler.
2082360SN/Atemplate <class OS>
20831450SN/ASyscallReturn
208411851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
208511851Sbrandon.potter@amd.com                 ThreadContext *tc)
2086360SN/A{
20876701Sgblack@eecs.umich.edu    int index = 0;
20886701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
2089360SN/A
209010796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
2091360SN/A    tp->tv_sec += seconds_since_epoch;
20926109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
20936109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
2094360SN/A
20958706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
2096360SN/A
20971458SN/A    return 0;
2098360SN/A}
2099360SN/A
2100360SN/A
21011999SN/A/// Target utimes() handler.
21021999SN/Atemplate <class OS>
21031999SN/ASyscallReturn
210411851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
21052680Sktlim@umich.edu           ThreadContext *tc)
21061999SN/A{
21071999SN/A    std::string path;
21081999SN/A
21096701Sgblack@eecs.umich.edu    int index = 0;
21108852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
21116701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
21126701Sgblack@eecs.umich.edu        return -EFAULT;
21136701Sgblack@eecs.umich.edu    }
21141999SN/A
21156701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
21166701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
21178706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
21181999SN/A
21191999SN/A    struct timeval hostTimeval[2];
212011906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
21218737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
21228737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
21231999SN/A    }
21243669Sbinkertn@umich.edu
212513883Sdavid.hashe@amd.com    // Adjust path for cwd and redirection
212613883Sdavid.hashe@amd.com    path = process->checkPathRedirect(path);
21273669Sbinkertn@umich.edu
21281999SN/A    int result = utimes(path.c_str(), hostTimeval);
21291999SN/A
21301999SN/A    if (result < 0)
21311999SN/A        return -errno;
21321999SN/A
21331999SN/A    return 0;
21341999SN/A}
213511886Sbrandon.potter@amd.com
213611886Sbrandon.potter@amd.comtemplate <class OS>
213711886Sbrandon.potter@amd.comSyscallReturn
213811886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
213911886Sbrandon.potter@amd.com{
214011886Sbrandon.potter@amd.com    desc->setFlags(0);
214111886Sbrandon.potter@amd.com
214211886Sbrandon.potter@amd.com    int index = 0;
214311886Sbrandon.potter@amd.com    std::string path;
214411886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
214511886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
214611886Sbrandon.potter@amd.com        return -EFAULT;
214711886Sbrandon.potter@amd.com
214811886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
214911886Sbrandon.potter@amd.com        return -EACCES;
215011886Sbrandon.potter@amd.com
215111886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
215211886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
215311886Sbrandon.potter@amd.com                      Addr mem_loc)
215411886Sbrandon.potter@amd.com    {
215511886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
215611886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
215711886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
215811886Sbrandon.potter@amd.com
215911886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
216011886Sbrandon.potter@amd.com                break;
216111886Sbrandon.potter@amd.com
216211886Sbrandon.potter@amd.com            vect.push_back(std::string());
216311886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
216411886Sbrandon.potter@amd.com        }
216511886Sbrandon.potter@amd.com    };
216611886Sbrandon.potter@amd.com
216711886Sbrandon.potter@amd.com    /**
216811886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
216911886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
217011886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
217111886Sbrandon.potter@amd.com     * constructor.
217211886Sbrandon.potter@amd.com     */
217311886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
217411886Sbrandon.potter@amd.com    pp->executable = path;
217511886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
217611886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
217711886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
217811886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
217911886Sbrandon.potter@amd.com    pp->uid = p->uid();
218011886Sbrandon.potter@amd.com    pp->egid = p->egid();
218111886Sbrandon.potter@amd.com    pp->euid = p->euid();
218211886Sbrandon.potter@amd.com    pp->gid = p->gid();
218311886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
218411886Sbrandon.potter@amd.com    pp->pid = p->pid();
218511886Sbrandon.potter@amd.com    pp->input.assign("cin");
218611886Sbrandon.potter@amd.com    pp->output.assign("cout");
218711886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
218813883Sdavid.hashe@amd.com    pp->cwd.assign(p->tgtCwd);
218911886Sbrandon.potter@amd.com    pp->system = p->system;
219011886Sbrandon.potter@amd.com    /**
219111886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
219211886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
219311886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
219411886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
219511886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
219611886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
219711886Sbrandon.potter@amd.com     */
219811886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
219911886Sbrandon.potter@amd.com    Process *new_p = pp->create();
220011886Sbrandon.potter@amd.com    delete pp;
220111886Sbrandon.potter@amd.com
220211886Sbrandon.potter@amd.com    /**
220311886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
220411886Sbrandon.potter@amd.com     * close-on-exec.
220511886Sbrandon.potter@amd.com     */
220611886Sbrandon.potter@amd.com    new_p->fds = p->fds;
220711886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
220811886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
220911886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
221011886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
221111886Sbrandon.potter@amd.com    }
221211886Sbrandon.potter@amd.com
221311886Sbrandon.potter@amd.com    *new_p->sigchld = true;
221411886Sbrandon.potter@amd.com
221511886Sbrandon.potter@amd.com    delete p;
221611886Sbrandon.potter@amd.com    tc->clearArchRegs();
221711886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
221811886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
221911886Sbrandon.potter@amd.com    new_p->initState();
222011886Sbrandon.potter@amd.com    tc->activate();
222111886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
222211886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
222311886Sbrandon.potter@amd.com
222411886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
222511886Sbrandon.potter@amd.com    return 0;
222611886Sbrandon.potter@amd.com}
222711886Sbrandon.potter@amd.com
2228378SN/A/// Target getrusage() function.
2229360SN/Atemplate <class OS>
22301450SN/ASyscallReturn
223111851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
22322680Sktlim@umich.edu              ThreadContext *tc)
2233360SN/A{
22346701Sgblack@eecs.umich.edu    int index = 0;
22356701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
22366701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
2237360SN/A
22383670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
22393670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
2240360SN/A    rup->ru_stime.tv_sec = 0;
2241360SN/A    rup->ru_stime.tv_usec = 0;
2242360SN/A    rup->ru_maxrss = 0;
2243360SN/A    rup->ru_ixrss = 0;
2244360SN/A    rup->ru_idrss = 0;
2245360SN/A    rup->ru_isrss = 0;
2246360SN/A    rup->ru_minflt = 0;
2247360SN/A    rup->ru_majflt = 0;
2248360SN/A    rup->ru_nswap = 0;
2249360SN/A    rup->ru_inblock = 0;
2250360SN/A    rup->ru_oublock = 0;
2251360SN/A    rup->ru_msgsnd = 0;
2252360SN/A    rup->ru_msgrcv = 0;
2253360SN/A    rup->ru_nsignals = 0;
2254360SN/A    rup->ru_nvcsw = 0;
2255360SN/A    rup->ru_nivcsw = 0;
2256360SN/A
22573670Sbinkertn@umich.edu    switch (who) {
22583670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
225910796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
22608737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
22618737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
22623670Sbinkertn@umich.edu        break;
22633670Sbinkertn@umich.edu
22643670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
22653670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
22663670Sbinkertn@umich.edu        break;
22673670Sbinkertn@umich.edu
22683670Sbinkertn@umich.edu      default:
22693670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
22703670Sbinkertn@umich.edu        // plow ahead
22713670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
22723670Sbinkertn@umich.edu             who);
22733670Sbinkertn@umich.edu    }
22743670Sbinkertn@umich.edu
22758706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
2276360SN/A
22771458SN/A    return 0;
2278360SN/A}
2279360SN/A
22806683Stjones1@inf.ed.ac.uk/// Target times() function.
22816683Stjones1@inf.ed.ac.uktemplate <class OS>
22826683Stjones1@inf.ed.ac.ukSyscallReturn
228311851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
228411851Sbrandon.potter@amd.com          ThreadContext *tc)
22856683Stjones1@inf.ed.ac.uk{
22866701Sgblack@eecs.umich.edu    int index = 0;
22876701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
22886683Stjones1@inf.ed.ac.uk
22896683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
22907823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
22916683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
22926683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
22936683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
22946683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
22956683Stjones1@inf.ed.ac.uk
22966683Stjones1@inf.ed.ac.uk    // Convert to host endianness
22978737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
22986683Stjones1@inf.ed.ac.uk
22996683Stjones1@inf.ed.ac.uk    // Write back
23008706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
23016683Stjones1@inf.ed.ac.uk
23026683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
23036683Stjones1@inf.ed.ac.uk    return clocks;
23046683Stjones1@inf.ed.ac.uk}
23052553SN/A
23066684Stjones1@inf.ed.ac.uk/// Target time() function.
23076684Stjones1@inf.ed.ac.uktemplate <class OS>
23086684Stjones1@inf.ed.ac.ukSyscallReturn
230911851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
23106684Stjones1@inf.ed.ac.uk{
23116684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
231210796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
23136684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
23146684Stjones1@inf.ed.ac.uk
23156701Sgblack@eecs.umich.edu    int index = 0;
23166701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
231711321Ssteve.reinhardt@amd.com    if (taddr != 0) {
23186684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
23198737Skoansin.tan@gmail.com        t = TheISA::htog(t);
23208852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
23218852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
23226684Stjones1@inf.ed.ac.uk    }
23236684Stjones1@inf.ed.ac.uk    return sec;
23246684Stjones1@inf.ed.ac.uk}
23252553SN/A
232611910SBrandon.Potter@amd.comtemplate <class OS>
232711910SBrandon.Potter@amd.comSyscallReturn
232811910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
232911910SBrandon.Potter@amd.com{
233011910SBrandon.Potter@amd.com    int index = 0;
233111910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
233211910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
233311910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
233411910SBrandon.Potter@amd.com
233511910SBrandon.Potter@amd.com    /**
233611910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
233711910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
233811910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
233911910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
234011910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
234111910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
234211910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
234311910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
234411910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
234511910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
234611910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
234711910SBrandon.Potter@amd.com     * due to PID rollover.
234811910SBrandon.Potter@amd.com     */
234911910SBrandon.Potter@amd.com
235011910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
235111910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
235211910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
235311910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
235411910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
235511910SBrandon.Potter@amd.com            tgt_proc = temp;
235611910SBrandon.Potter@amd.com            break;
235711910SBrandon.Potter@amd.com        }
235811910SBrandon.Potter@amd.com    }
235911910SBrandon.Potter@amd.com
236011910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
236111910SBrandon.Potter@amd.com        return -EINVAL;
236211910SBrandon.Potter@amd.com
236311910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
236411910SBrandon.Potter@amd.com        return -ESRCH;
236511910SBrandon.Potter@amd.com
236611910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
236711910SBrandon.Potter@amd.com        return -ESRCH;
236811910SBrandon.Potter@amd.com
236911910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
237011910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
237111910SBrandon.Potter@amd.com
237211910SBrandon.Potter@amd.com    return 0;
237311910SBrandon.Potter@amd.com}
237411910SBrandon.Potter@amd.com
237513568Sbrandon.potter@amd.comtemplate <class OS>
237613568Sbrandon.potter@amd.comSyscallReturn
237713568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
237813568Sbrandon.potter@amd.com{
237913568Sbrandon.potter@amd.com    int index = 0;
238013568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
238113568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
238213568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
238313568Sbrandon.potter@amd.com
238413568Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
238513568Sbrandon.potter@amd.com    if (sim_fd == -1)
238613568Sbrandon.potter@amd.com        return -errno;
238713568Sbrandon.potter@amd.com
238813568Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
238913568Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
239013568Sbrandon.potter@amd.com
239113568Sbrandon.potter@amd.com    return tgt_fd;
239213568Sbrandon.potter@amd.com}
239313568Sbrandon.potter@amd.com
239413568Sbrandon.potter@amd.comtemplate <class OS>
239513568Sbrandon.potter@amd.comSyscallReturn
239613568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
239713568Sbrandon.potter@amd.com{
239813568Sbrandon.potter@amd.com    int index = 0;
239913568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
240013568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
240113568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
240213568Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
240313568Sbrandon.potter@amd.com
240413568Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
240513568Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
240613568Sbrandon.potter@amd.com    if (status == -1)
240713568Sbrandon.potter@amd.com        return -errno;
240813568Sbrandon.potter@amd.com
240913568Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
241013568Sbrandon.potter@amd.com
241113568Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
241213568Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
241313568Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
241413568Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
241513568Sbrandon.potter@amd.com    svBuf.copyOut(tc->getMemProxy());
241613568Sbrandon.potter@amd.com
241713568Sbrandon.potter@amd.com    return status;
241813568Sbrandon.potter@amd.com}
24192553SN/A
242013570Sbrandon.potter@amd.comtemplate <class OS>
242113570Sbrandon.potter@amd.comSyscallReturn
242213570Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
242313570Sbrandon.potter@amd.com{
242413570Sbrandon.potter@amd.com    int retval;
242513570Sbrandon.potter@amd.com
242613570Sbrandon.potter@amd.com    int index = 0;
242713570Sbrandon.potter@amd.com    int nfds_t = p->getSyscallArg(tc, index);
242813570Sbrandon.potter@amd.com    Addr fds_read_ptr = p->getSyscallArg(tc, index);
242913570Sbrandon.potter@amd.com    Addr fds_writ_ptr = p->getSyscallArg(tc, index);
243013570Sbrandon.potter@amd.com    Addr fds_excp_ptr = p->getSyscallArg(tc, index);
243113570Sbrandon.potter@amd.com    Addr time_val_ptr = p->getSyscallArg(tc, index);
243213570Sbrandon.potter@amd.com
243313570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr);
243413570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr);
243513570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr);
243613570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(time_val_ptr);
243713570Sbrandon.potter@amd.com
243813570Sbrandon.potter@amd.com    /**
243913570Sbrandon.potter@amd.com     * Host fields. Notice that these use the definitions from the system
244013570Sbrandon.potter@amd.com     * headers instead of the gem5 headers and libraries. If the host and
244113570Sbrandon.potter@amd.com     * target have different header file definitions, this will not work.
244213570Sbrandon.potter@amd.com     */
244313570Sbrandon.potter@amd.com    fd_set rd_h;
244413570Sbrandon.potter@amd.com    FD_ZERO(&rd_h);
244513570Sbrandon.potter@amd.com    fd_set wr_h;
244613570Sbrandon.potter@amd.com    FD_ZERO(&wr_h);
244713570Sbrandon.potter@amd.com    fd_set ex_h;
244813570Sbrandon.potter@amd.com    FD_ZERO(&ex_h);
244913570Sbrandon.potter@amd.com
245013570Sbrandon.potter@amd.com    /**
245113570Sbrandon.potter@amd.com     * Copy in the fd_set from the target.
245213570Sbrandon.potter@amd.com     */
245313570Sbrandon.potter@amd.com    if (fds_read_ptr)
245413570Sbrandon.potter@amd.com        rd_t.copyIn(tc->getMemProxy());
245513570Sbrandon.potter@amd.com    if (fds_writ_ptr)
245613570Sbrandon.potter@amd.com        wr_t.copyIn(tc->getMemProxy());
245713570Sbrandon.potter@amd.com    if (fds_excp_ptr)
245813570Sbrandon.potter@amd.com        ex_t.copyIn(tc->getMemProxy());
245913570Sbrandon.potter@amd.com
246013570Sbrandon.potter@amd.com    /**
246113570Sbrandon.potter@amd.com     * We need to translate the target file descriptor set into a host file
246213570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
246313570Sbrandon.potter@amd.com     * and the fd_set defined in Linux header files. The nfds field also
246413570Sbrandon.potter@amd.com     * needs to be updated as it will be only target specific after
246513570Sbrandon.potter@amd.com     * retrieving it from the target; the nfds value is expected to be the
246613570Sbrandon.potter@amd.com     * highest file descriptor that needs to be checked, so we need to extend
246713570Sbrandon.potter@amd.com     * it out for nfds_h when we do the update.
246813570Sbrandon.potter@amd.com     */
246913570Sbrandon.potter@amd.com    int nfds_h = 0;
247013570Sbrandon.potter@amd.com    std::map<int, int> trans_map;
247113570Sbrandon.potter@amd.com    auto try_add_host_set = [&](fd_set *tgt_set_entry,
247213570Sbrandon.potter@amd.com                                fd_set *hst_set_entry,
247313570Sbrandon.potter@amd.com                                int iter) -> bool
247413570Sbrandon.potter@amd.com    {
247513570Sbrandon.potter@amd.com        /**
247613570Sbrandon.potter@amd.com         * By this point, we know that we are looking at a valid file
247713570Sbrandon.potter@amd.com         * descriptor set on the target. We need to check if the target file
247813570Sbrandon.potter@amd.com         * descriptor value passed in as iter is part of the set.
247913570Sbrandon.potter@amd.com         */
248013570Sbrandon.potter@amd.com        if (FD_ISSET(iter, tgt_set_entry)) {
248113570Sbrandon.potter@amd.com            /**
248213570Sbrandon.potter@amd.com             * We know that the target file descriptor belongs to the set,
248313570Sbrandon.potter@amd.com             * but we do not yet know if the file descriptor is valid or
248413570Sbrandon.potter@amd.com             * that we have a host mapping. Check that now.
248513570Sbrandon.potter@amd.com             */
248613570Sbrandon.potter@amd.com            auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]);
248713570Sbrandon.potter@amd.com            if (!hbfdp)
248813570Sbrandon.potter@amd.com                return true;
248913570Sbrandon.potter@amd.com            auto sim_fd = hbfdp->getSimFD();
249013570Sbrandon.potter@amd.com
249113570Sbrandon.potter@amd.com            /**
249213570Sbrandon.potter@amd.com             * Add the sim_fd to tgt_fd translation into trans_map for use
249313570Sbrandon.potter@amd.com             * later when we need to zero the target fd_set structures and
249413570Sbrandon.potter@amd.com             * then update them with hits returned from the host select call.
249513570Sbrandon.potter@amd.com             */
249613570Sbrandon.potter@amd.com            trans_map[sim_fd] = iter;
249713570Sbrandon.potter@amd.com
249813570Sbrandon.potter@amd.com            /**
249913570Sbrandon.potter@amd.com             * We know that the host file descriptor exists so now we check
250013570Sbrandon.potter@amd.com             * if we need to update the max count for nfds_h before passing
250113570Sbrandon.potter@amd.com             * the duplicated structure into the host.
250213570Sbrandon.potter@amd.com             */
250313570Sbrandon.potter@amd.com            nfds_h = std::max(nfds_h - 1, sim_fd + 1);
250413570Sbrandon.potter@amd.com
250513570Sbrandon.potter@amd.com            /**
250613570Sbrandon.potter@amd.com             * Add the host file descriptor to the set that we are going to
250713570Sbrandon.potter@amd.com             * pass into the host.
250813570Sbrandon.potter@amd.com             */
250913570Sbrandon.potter@amd.com            FD_SET(sim_fd, hst_set_entry);
251013570Sbrandon.potter@amd.com        }
251113570Sbrandon.potter@amd.com        return false;
251213570Sbrandon.potter@amd.com    };
251313570Sbrandon.potter@amd.com
251413570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_t; i++) {
251513570Sbrandon.potter@amd.com        if (fds_read_ptr) {
251613570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i);
251713570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
251813570Sbrandon.potter@amd.com        }
251913570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
252013570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i);
252113570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
252213570Sbrandon.potter@amd.com        }
252313570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
252413570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i);
252513570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
252613570Sbrandon.potter@amd.com        }
252713570Sbrandon.potter@amd.com    }
252813570Sbrandon.potter@amd.com
252913570Sbrandon.potter@amd.com    if (time_val_ptr) {
253013570Sbrandon.potter@amd.com        /**
253113570Sbrandon.potter@amd.com         * It might be possible to decrement the timeval based on some
253213570Sbrandon.potter@amd.com         * derivation of wall clock determined from elapsed simulator ticks
253313570Sbrandon.potter@amd.com         * but that seems like overkill. Rather, we just set the timeval with
253413570Sbrandon.potter@amd.com         * zero timeout. (There is no reason to block during the simulation
253513570Sbrandon.potter@amd.com         * as it only decreases simulator performance.)
253613570Sbrandon.potter@amd.com         */
253713570Sbrandon.potter@amd.com        tp->tv_sec = 0;
253813570Sbrandon.potter@amd.com        tp->tv_usec = 0;
253913570Sbrandon.potter@amd.com
254013570Sbrandon.potter@amd.com        retval = select(nfds_h,
254113570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
254213570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
254313570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
254413570Sbrandon.potter@amd.com                        (timeval*)&*tp);
254513570Sbrandon.potter@amd.com    } else {
254613570Sbrandon.potter@amd.com        /**
254713570Sbrandon.potter@amd.com         * If the timeval pointer is null, setup a new timeval structure to
254813570Sbrandon.potter@amd.com         * pass into the host select call. Unfortunately, we will need to
254913570Sbrandon.potter@amd.com         * manually check the return value and throw a retry fault if the
255013570Sbrandon.potter@amd.com         * return value is zero. Allowing the system call to block will
255113570Sbrandon.potter@amd.com         * likely deadlock the event queue.
255213570Sbrandon.potter@amd.com         */
255313570Sbrandon.potter@amd.com        struct timeval tv = { 0, 0 };
255413570Sbrandon.potter@amd.com
255513570Sbrandon.potter@amd.com        retval = select(nfds_h,
255613570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
255713570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
255813570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
255913570Sbrandon.potter@amd.com                        &tv);
256013570Sbrandon.potter@amd.com
256113570Sbrandon.potter@amd.com        if (retval == 0) {
256213570Sbrandon.potter@amd.com            /**
256313570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
256413570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try to
256513570Sbrandon.potter@amd.com             * return the signal interrupt instead.
256613570Sbrandon.potter@amd.com             */
256713570Sbrandon.potter@amd.com            for (auto sig : tc->getSystemPtr()->signalList)
256813570Sbrandon.potter@amd.com                if (sig.receiver == p)
256913570Sbrandon.potter@amd.com                    return -EINTR;
257013570Sbrandon.potter@amd.com            return SyscallReturn::retry();
257113570Sbrandon.potter@amd.com        }
257213570Sbrandon.potter@amd.com    }
257313570Sbrandon.potter@amd.com
257413570Sbrandon.potter@amd.com    if (retval == -1)
257513570Sbrandon.potter@amd.com        return -errno;
257613570Sbrandon.potter@amd.com
257713570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*rd_t);
257813570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*wr_t);
257913570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*ex_t);
258013570Sbrandon.potter@amd.com
258113570Sbrandon.potter@amd.com    /**
258213570Sbrandon.potter@amd.com     * We need to translate the host file descriptor set into a target file
258313570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
258413570Sbrandon.potter@amd.com     * and the fd_set defined in header files.
258513570Sbrandon.potter@amd.com     */
258613570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_h; i++) {
258713570Sbrandon.potter@amd.com        if (fds_read_ptr) {
258813570Sbrandon.potter@amd.com            if (FD_ISSET(i, &rd_h))
258913570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*rd_t);
259013570Sbrandon.potter@amd.com        }
259113570Sbrandon.potter@amd.com
259213570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
259313570Sbrandon.potter@amd.com            if (FD_ISSET(i, &wr_h))
259413570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*wr_t);
259513570Sbrandon.potter@amd.com        }
259613570Sbrandon.potter@amd.com
259713570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
259813570Sbrandon.potter@amd.com            if (FD_ISSET(i, &ex_h))
259913570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*ex_t);
260013570Sbrandon.potter@amd.com        }
260113570Sbrandon.potter@amd.com    }
260213570Sbrandon.potter@amd.com
260313570Sbrandon.potter@amd.com    if (fds_read_ptr)
260413570Sbrandon.potter@amd.com        rd_t.copyOut(tc->getMemProxy());
260513570Sbrandon.potter@amd.com    if (fds_writ_ptr)
260613570Sbrandon.potter@amd.com        wr_t.copyOut(tc->getMemProxy());
260713570Sbrandon.potter@amd.com    if (fds_excp_ptr)
260813570Sbrandon.potter@amd.com        ex_t.copyOut(tc->getMemProxy());
260913570Sbrandon.potter@amd.com    if (time_val_ptr)
261013570Sbrandon.potter@amd.com        tp.copyOut(tc->getMemProxy());
261113570Sbrandon.potter@amd.com
261213570Sbrandon.potter@amd.com    return retval;
261313570Sbrandon.potter@amd.com}
261413570Sbrandon.potter@amd.com
261513570Sbrandon.potter@amd.comtemplate <class OS>
261613570Sbrandon.potter@amd.comSyscallReturn
261713570Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
261813570Sbrandon.potter@amd.com{
261913570Sbrandon.potter@amd.com    int index = 0;
262013570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
262113570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
262213570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
262313570Sbrandon.potter@amd.com
262413570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
262513570Sbrandon.potter@amd.com    if (!hbfdp)
262613570Sbrandon.potter@amd.com        return -EBADF;
262713570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
262813570Sbrandon.potter@amd.com
262913570Sbrandon.potter@amd.com    struct pollfd pfd;
263013570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
263113570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
263213570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
263313570Sbrandon.potter@amd.com        && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK))
263413570Sbrandon.potter@amd.com        return SyscallReturn::retry();
263513570Sbrandon.potter@amd.com
263613570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
263713570Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes);
263813570Sbrandon.potter@amd.com
263913570Sbrandon.potter@amd.com    if (bytes_read > 0)
264013570Sbrandon.potter@amd.com        buf_arg.copyOut(tc->getMemProxy());
264113570Sbrandon.potter@amd.com
264213570Sbrandon.potter@amd.com    return (bytes_read == -1) ? -errno : bytes_read;
264313570Sbrandon.potter@amd.com}
264413570Sbrandon.potter@amd.com
264513570Sbrandon.potter@amd.comtemplate <class OS>
264613570Sbrandon.potter@amd.comSyscallReturn
264713570Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
264813570Sbrandon.potter@amd.com{
264913570Sbrandon.potter@amd.com    int index = 0;
265013570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
265113570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
265213570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
265313570Sbrandon.potter@amd.com
265413570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
265513570Sbrandon.potter@amd.com    if (!hbfdp)
265613570Sbrandon.potter@amd.com        return -EBADF;
265713570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
265813570Sbrandon.potter@amd.com
265913570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
266013570Sbrandon.potter@amd.com    buf_arg.copyIn(tc->getMemProxy());
266113570Sbrandon.potter@amd.com
266213570Sbrandon.potter@amd.com    struct pollfd pfd;
266313570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
266413570Sbrandon.potter@amd.com    pfd.events = POLLOUT;
266513570Sbrandon.potter@amd.com
266613570Sbrandon.potter@amd.com    /**
266713570Sbrandon.potter@amd.com     * We don't want to poll on /dev/random. The kernel will not enable the
266813570Sbrandon.potter@amd.com     * file descriptor for writing unless the entropy in the system falls
266913570Sbrandon.potter@amd.com     * below write_wakeup_threshold. This is not guaranteed to happen
267013570Sbrandon.potter@amd.com     * depending on host settings.
267113570Sbrandon.potter@amd.com     */
267213570Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp);
267313570Sbrandon.potter@amd.com    if (ffdp && (ffdp->getFileName() != "/dev/random")) {
267413570Sbrandon.potter@amd.com        if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK))
267513570Sbrandon.potter@amd.com            return SyscallReturn::retry();
267613570Sbrandon.potter@amd.com    }
267713570Sbrandon.potter@amd.com
267813570Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes);
267913570Sbrandon.potter@amd.com
268013570Sbrandon.potter@amd.com    if (bytes_written != -1)
268113570Sbrandon.potter@amd.com        fsync(sim_fd);
268213570Sbrandon.potter@amd.com
268313570Sbrandon.potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
268413570Sbrandon.potter@amd.com}
268513570Sbrandon.potter@amd.com
268613570Sbrandon.potter@amd.comtemplate <class OS>
268713570Sbrandon.potter@amd.comSyscallReturn
268813570Sbrandon.potter@amd.comwait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
268913570Sbrandon.potter@amd.com{
269013570Sbrandon.potter@amd.com    int index = 0;
269113570Sbrandon.potter@amd.com    pid_t pid = p->getSyscallArg(tc, index);
269213570Sbrandon.potter@amd.com    Addr statPtr = p->getSyscallArg(tc, index);
269313570Sbrandon.potter@amd.com    int options = p->getSyscallArg(tc, index);
269413570Sbrandon.potter@amd.com    Addr rusagePtr = p->getSyscallArg(tc, index);
269513570Sbrandon.potter@amd.com
269613570Sbrandon.potter@amd.com    if (rusagePtr)
269713570Sbrandon.potter@amd.com        DPRINTFR(SyscallVerbose,
269813570Sbrandon.potter@amd.com                 "%d: %s: syscall wait4: rusage pointer provided however "
269913570Sbrandon.potter@amd.com                 "functionality not supported. Ignoring rusage pointer.\n",
270013570Sbrandon.potter@amd.com                 curTick(), tc->getCpuPtr()->name());
270113570Sbrandon.potter@amd.com
270213570Sbrandon.potter@amd.com    /**
270313570Sbrandon.potter@amd.com     * Currently, wait4 is only implemented so that it will wait for children
270413570Sbrandon.potter@amd.com     * exit conditions which are denoted by a SIGCHLD signals posted into the
270513570Sbrandon.potter@amd.com     * system signal list. We return no additional information via any of the
270613570Sbrandon.potter@amd.com     * parameters supplied to wait4. If nothing is found in the system signal
270713570Sbrandon.potter@amd.com     * list, we will wait indefinitely for SIGCHLD to post by retrying the
270813570Sbrandon.potter@amd.com     * call.
270913570Sbrandon.potter@amd.com     */
271013570Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
271113570Sbrandon.potter@amd.com    std::list<BasicSignal>::iterator iter;
271213570Sbrandon.potter@amd.com    for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) {
271313570Sbrandon.potter@amd.com        if (iter->receiver == p) {
271413570Sbrandon.potter@amd.com            if (pid < -1) {
271513570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == -pid)
271613570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
271713570Sbrandon.potter@amd.com                    goto success;
271813570Sbrandon.potter@amd.com            } else if (pid == -1) {
271913570Sbrandon.potter@amd.com                if (iter->signalValue == OS::TGT_SIGCHLD)
272013570Sbrandon.potter@amd.com                    goto success;
272113570Sbrandon.potter@amd.com            } else if (pid == 0) {
272213570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == p->pgid())
272313570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
272413570Sbrandon.potter@amd.com                    goto success;
272513570Sbrandon.potter@amd.com            } else {
272613570Sbrandon.potter@amd.com                if ((iter->sender->pid() == pid)
272713570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
272813570Sbrandon.potter@amd.com                    goto success;
272913570Sbrandon.potter@amd.com            }
273013570Sbrandon.potter@amd.com        }
273113570Sbrandon.potter@amd.com    }
273213570Sbrandon.potter@amd.com
273313570Sbrandon.potter@amd.com    return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry();
273413570Sbrandon.potter@amd.com
273513570Sbrandon.potter@amd.comsuccess:
273613570Sbrandon.potter@amd.com    // Set status to EXITED for WIFEXITED evaluations.
273713570Sbrandon.potter@amd.com    const int EXITED = 0;
273813570Sbrandon.potter@amd.com    BufferArg statusBuf(statPtr, sizeof(int));
273913570Sbrandon.potter@amd.com    *(int *)statusBuf.bufferPtr() = EXITED;
274013570Sbrandon.potter@amd.com    statusBuf.copyOut(tc->getMemProxy());
274113570Sbrandon.potter@amd.com
274213570Sbrandon.potter@amd.com    // Return the child PID.
274313570Sbrandon.potter@amd.com    pid_t retval = iter->sender->pid();
274413570Sbrandon.potter@amd.com    sysh->signalList.erase(iter);
274513570Sbrandon.potter@amd.com    return retval;
274613570Sbrandon.potter@amd.com}
274713570Sbrandon.potter@amd.com
274813570Sbrandon.potter@amd.comtemplate <class OS>
274913570Sbrandon.potter@amd.comSyscallReturn
275013570Sbrandon.potter@amd.comacceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
275113570Sbrandon.potter@amd.com{
275213570Sbrandon.potter@amd.com    struct sockaddr sa;
275313570Sbrandon.potter@amd.com    socklen_t addrLen;
275413570Sbrandon.potter@amd.com    int host_fd;
275513570Sbrandon.potter@amd.com    int index = 0;
275613570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
275713570Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
275813570Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
275913570Sbrandon.potter@amd.com
276013570Sbrandon.potter@amd.com    BufferArg *lenBufPtr = nullptr;
276113570Sbrandon.potter@amd.com    BufferArg *addrBufPtr = nullptr;
276213570Sbrandon.potter@amd.com
276313570Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
276413570Sbrandon.potter@amd.com    if (!sfdp)
276513570Sbrandon.potter@amd.com        return -EBADF;
276613570Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
276713570Sbrandon.potter@amd.com
276813570Sbrandon.potter@amd.com    /**
276913570Sbrandon.potter@amd.com     * We poll the socket file descriptor first to guarantee that we do not
277013570Sbrandon.potter@amd.com     * block on our accept call. The socket can be opened without the
277113570Sbrandon.potter@amd.com     * non-blocking flag (it blocks). This will cause deadlocks between
277213570Sbrandon.potter@amd.com     * communicating processes.
277313570Sbrandon.potter@amd.com     */
277413570Sbrandon.potter@amd.com    struct pollfd pfd;
277513570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
277613570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
277713570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
277813570Sbrandon.potter@amd.com        && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK))
277913570Sbrandon.potter@amd.com        return SyscallReturn::retry();
278013570Sbrandon.potter@amd.com
278113570Sbrandon.potter@amd.com    if (lenPtr) {
278213570Sbrandon.potter@amd.com        lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t));
278313570Sbrandon.potter@amd.com        lenBufPtr->copyIn(tc->getMemProxy());
278413570Sbrandon.potter@amd.com        memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(),
278513570Sbrandon.potter@amd.com               sizeof(socklen_t));
278613570Sbrandon.potter@amd.com    }
278713570Sbrandon.potter@amd.com
278813570Sbrandon.potter@amd.com    if (addrPtr) {
278913570Sbrandon.potter@amd.com        addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr));
279013570Sbrandon.potter@amd.com        addrBufPtr->copyIn(tc->getMemProxy());
279113570Sbrandon.potter@amd.com        memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(),
279213570Sbrandon.potter@amd.com               sizeof(struct sockaddr));
279313570Sbrandon.potter@amd.com    }
279413570Sbrandon.potter@amd.com
279513570Sbrandon.potter@amd.com    host_fd = accept(sim_fd, &sa, &addrLen);
279613570Sbrandon.potter@amd.com
279713570Sbrandon.potter@amd.com    if (host_fd == -1)
279813570Sbrandon.potter@amd.com        return -errno;
279913570Sbrandon.potter@amd.com
280013570Sbrandon.potter@amd.com    if (addrPtr) {
280113570Sbrandon.potter@amd.com        memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa));
280213570Sbrandon.potter@amd.com        addrBufPtr->copyOut(tc->getMemProxy());
280313570Sbrandon.potter@amd.com        delete(addrBufPtr);
280413570Sbrandon.potter@amd.com    }
280513570Sbrandon.potter@amd.com
280613570Sbrandon.potter@amd.com    if (lenPtr) {
280713570Sbrandon.potter@amd.com        *(socklen_t *)lenBufPtr->bufferPtr() = addrLen;
280813570Sbrandon.potter@amd.com        lenBufPtr->copyOut(tc->getMemProxy());
280913570Sbrandon.potter@amd.com        delete(lenBufPtr);
281013570Sbrandon.potter@amd.com    }
281113570Sbrandon.potter@amd.com
281213570Sbrandon.potter@amd.com    auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain,
281313570Sbrandon.potter@amd.com                                                sfdp->_type, sfdp->_protocol);
281413570Sbrandon.potter@amd.com    return p->fds->allocFD(afdp);
281513570Sbrandon.potter@amd.com}
281613570Sbrandon.potter@amd.com
28171354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2818