syscall_emul.hh revision 13571
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
119360SN/A//////////////////////////////////////////////////////////////////////
120360SN/A//
121360SN/A// The following emulation functions are generic enough that they
122360SN/A// don't need to be recompiled for different emulated OS's.  They are
123360SN/A// defined in sim/syscall_emul.cc.
124360SN/A//
125360SN/A//////////////////////////////////////////////////////////////////////
126360SN/A
127360SN/A
128378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1291706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
13011851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
131378SN/A
132378SN/A/// Handler for unimplemented syscalls that we never intend to
133378SN/A/// implement (signal handling, etc.) and should not affect the correct
134378SN/A/// behavior of the program.  Print a warning only if the appropriate
135378SN/A/// trace flag is enabled.  Return success to the target program.
1361706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
13711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
138360SN/A
13911760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
14011760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
14111851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
14211760Sbrandon.potter@amd.com
1436109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1441706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14511851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
146378SN/A
1476109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1486109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
14911851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1506109Ssanchezd@stanford.edu
15111886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
15211886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
15311886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15411886Sbrandon.potter@amd.com
155378SN/A/// Target getpagesize() handler.
1561706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
15711851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
158378SN/A
1595748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1605748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
16111851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
162378SN/A
163378SN/A/// Target close() handler.
1641706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16511851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
166378SN/A
167378SN/A/// Target lseek() handler.
1681706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
16911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
170378SN/A
1714118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1724118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17311851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1744118Sgblack@eecs.umich.edu
175378SN/A/// Target munmap() handler.
1761706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
17711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
178378SN/A
17913568Sbrandon.potter@amd.com/// Target shutdown() handler.
18013568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num,
18113568Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
18213568Sbrandon.potter@amd.com
183378SN/A/// Target gethostname() handler.
1841706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
18511851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
186360SN/A
1875513SMichael.Adler@intel.com/// Target getcwd() handler.
1885513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
18911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1905513SMichael.Adler@intel.com
19110203SAli.Saidi@ARM.com/// Target readlink() handler.
19210203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19311851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
19410203SAli.Saidi@ARM.com                           int index = 0);
1955513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19611851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
1975513SMichael.Adler@intel.com
198511SN/A/// Target unlink() handler.
19910633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20011851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20110633Smichaelupton@gmail.com                           int index);
2021706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
204511SN/A
20512795Smattdsinclair@gmail.com/// Target link() handler
20612795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
20712795Smattdsinclair@gmail.com                       ThreadContext *tc);
20812795Smattdsinclair@gmail.com
20912796Smattdsinclair@gmail.com/// Target symlink() handler.
21012796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
21112796Smattdsinclair@gmail.com                          ThreadContext *tc);
21212796Smattdsinclair@gmail.com
2135513SMichael.Adler@intel.com/// Target mkdir() handler.
2145513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
21511851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2165513SMichael.Adler@intel.com
21713031Sbrandon.potter@amd.com/// Target mknod() handler.
21813031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
21913031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22013031Sbrandon.potter@amd.com
22113031Sbrandon.potter@amd.com/// Target chdir() handler.
22213031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
22313031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22413031Sbrandon.potter@amd.com
22513031Sbrandon.potter@amd.com// Target rmdir() handler.
22613031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
22713031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22813031Sbrandon.potter@amd.com
229511SN/A/// Target rename() handler.
2301706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
23111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2321706SN/A
2331706SN/A
2341706SN/A/// Target truncate() handler.
2351706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
23611851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2371706SN/A
2381706SN/A
2391706SN/A/// Target ftruncate() handler.
2401706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
24111851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2421706SN/A
243511SN/A
2446703Svince@csl.cornell.edu/// Target truncate64() handler.
2456703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
24611851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2476703Svince@csl.cornell.edu
2486685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2496685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
25011851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2516685Stjones1@inf.ed.ac.uk
2526685Stjones1@inf.ed.ac.uk
2535513SMichael.Adler@intel.com/// Target umask() handler.
2545513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
25511851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2565513SMichael.Adler@intel.com
25711885Sbrandon.potter@amd.com/// Target gettid() handler.
25811885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
25911885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2605513SMichael.Adler@intel.com
2611999SN/A/// Target chown() handler.
2621999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26311851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2641999SN/A
26511885Sbrandon.potter@amd.com/// Target setpgid() handler.
26611885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
26711885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2681999SN/A
2691999SN/A/// Target fchown() handler.
2701999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
27111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2721999SN/A
2733079Sstever@eecs.umich.edu/// Target dup() handler.
2743079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
27511851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2763079Sstever@eecs.umich.edu
27711908SBrandon.Potter@amd.com/// Target dup2() handler.
27811908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
27911908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
28011908SBrandon.Potter@amd.com
28111875Sbrandon.potter@amd.com/// Target fcntl() handler.
2822093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28311851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2842093SN/A
2852687Sksewell@umich.edu/// Target fcntl64() handler.
2862687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
28711851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2882687Sksewell@umich.edu
2892238SN/A/// Target setuid() handler.
2902238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
29111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2922238SN/A
29311908SBrandon.Potter@amd.com/// Target pipe() handler.
29411908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
29511908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
29611908SBrandon.Potter@amd.com
29711908SBrandon.Potter@amd.com/// Internal pipe() handler.
29811908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
29911908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
30011908SBrandon.Potter@amd.com
3012238SN/A/// Target getpid() handler.
3022238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3042238SN/A
30513571Sbrandon.potter@amd.com// Target getpeername() handler.
30613571Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num,
30713571Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
30813571Sbrandon.potter@amd.com
30913568Sbrandon.potter@amd.com// Target bind() handler.
31013568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num,
31113568Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
31213568Sbrandon.potter@amd.com
31313568Sbrandon.potter@amd.com// Target listen() handler.
31413568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num,
31513568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
31613568Sbrandon.potter@amd.com
31713568Sbrandon.potter@amd.com// Target connect() handler.
31813568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num,
31913568Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
32013568Sbrandon.potter@amd.com
32113448Sciro.santilli@arm.com#if defined(SYS_getdents)
32213031Sbrandon.potter@amd.com// Target getdents() handler.
32313031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
32413031Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
32513448Sciro.santilli@arm.com#endif
32613031Sbrandon.potter@amd.com
32713539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
32813539Sjavier.setoain@arm.com// Target getdents() handler.
32913539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
33013539Sjavier.setoain@arm.com                           Process *p, ThreadContext *tc);
33113539Sjavier.setoain@arm.com#endif
33213539Sjavier.setoain@arm.com
33313569Sbrandon.potter@amd.com// Target sendto() handler.
33413569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num,
33513569Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
33613569Sbrandon.potter@amd.com
33713569Sbrandon.potter@amd.com// Target recvfrom() handler.
33813569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num,
33913569Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
34013569Sbrandon.potter@amd.com
34113569Sbrandon.potter@amd.com// Target recvmsg() handler.
34213569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num,
34313569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
34413569Sbrandon.potter@amd.com
34513569Sbrandon.potter@amd.com// Target sendmsg() handler.
34613569Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num,
34713569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
34813569Sbrandon.potter@amd.com
34913031Sbrandon.potter@amd.com// Target getuid() handler.
3502238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
35111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3522238SN/A
3532238SN/A/// Target getgid() handler.
3542238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
35511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3562238SN/A
3572238SN/A/// Target getppid() handler.
3582238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
35911851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3602238SN/A
3612238SN/A/// Target geteuid() handler.
3622238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
36311851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3642238SN/A
3652238SN/A/// Target getegid() handler.
3662238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
36711851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3682238SN/A
3699455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3709455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
37210203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
37411851Sbrandon.potter@amd.com                         int index);
3759455Smitch.hayenga+gem5@gmail.com
37613571Sbrandon.potter@amd.com// Target getsockopt() handler.
37713571Sbrandon.potter@amd.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num,
37813571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
37913571Sbrandon.potter@amd.com
38013571Sbrandon.potter@amd.com// Target setsockopt() handler.
38113571Sbrandon.potter@amd.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num,
38213571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
38313571Sbrandon.potter@amd.com
38413571Sbrandon.potter@amd.com// Target getsockname() handler.
38513571Sbrandon.potter@amd.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num,
38613571Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
38713571Sbrandon.potter@amd.com
3889112Smarc.orr@gmail.com/// Futex system call
38911906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
39011906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3919112Smarc.orr@gmail.comtemplate <class OS>
3929112Smarc.orr@gmail.comSyscallReturn
39311851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3949112Smarc.orr@gmail.com          ThreadContext *tc)
3959112Smarc.orr@gmail.com{
39611911SBrandon.Potter@amd.com    using namespace std;
3979112Smarc.orr@gmail.com
39811911SBrandon.Potter@amd.com    int index = 0;
39911911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
40011911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
40111911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
4029112Smarc.orr@gmail.com
40311911SBrandon.Potter@amd.com    /*
40411911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
40511911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
40611911SBrandon.Potter@amd.com     */
4079238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
4089112Smarc.orr@gmail.com
40911911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
4109112Smarc.orr@gmail.com
41111911SBrandon.Potter@amd.com    if (OS::TGT_FUTEX_WAIT == op) {
41211911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
41311911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
41411911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
41511911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
4169112Smarc.orr@gmail.com
41711911SBrandon.Potter@amd.com        /*
41811911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
41911911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
42011911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
42111911SBrandon.Potter@amd.com         */
42211911SBrandon.Potter@amd.com        if (val != mem_val)
4239112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
4249112Smarc.orr@gmail.com
42511911SBrandon.Potter@amd.com        futex_map.suspend(uaddr, process->tgid(), tc);
42611911SBrandon.Potter@amd.com
4279112Smarc.orr@gmail.com        return 0;
42811911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
42911911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
4309112Smarc.orr@gmail.com    }
4319112Smarc.orr@gmail.com
43211911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
43311911SBrandon.Potter@amd.com    return -ENOSYS;
4349112Smarc.orr@gmail.com}
4359112Smarc.orr@gmail.com
4362238SN/A
4372238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4382238SN/A/// returning a second value in a register other than the normal return register
4392238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
44011851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
4412238SN/A
4422238SN/A/// Target getpidPseudo() handler.
4432238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
44411851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4452238SN/A
4462238SN/A/// Target getuidPseudo() handler.
4472238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
44811851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4492238SN/A
4502238SN/A/// Target getgidPseudo() handler.
4512238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
45211851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4532238SN/A
4542238SN/A
4551354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4561354SN/Aconst int one_million = 1000000;
45710796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
45810796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4591354SN/A
4601354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4611354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4621354SN/A/// real-world time) to keep simulations repeatable.
4631354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4641354SN/A
4651354SN/A/// Helper function to convert current elapsed time to seconds and
4661354SN/A/// microseconds.
4671354SN/Atemplate <class T1, class T2>
4681354SN/Avoid
46910796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4701354SN/A{
47110796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4721354SN/A    sec = elapsed_usecs / one_million;
4731354SN/A    usec = elapsed_usecs % one_million;
4741354SN/A}
4751354SN/A
47610796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
47710796Sbrandon.potter@amd.com/// nanoseconds.
47810796Sbrandon.potter@amd.comtemplate <class T1, class T2>
47910796Sbrandon.potter@amd.comvoid
48010796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
48110796Sbrandon.potter@amd.com{
48210796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
48310796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
48410796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
48510796Sbrandon.potter@amd.com}
48610796Sbrandon.potter@amd.com
487360SN/A//////////////////////////////////////////////////////////////////////
488360SN/A//
489360SN/A// The following emulation functions are generic, but need to be
490360SN/A// templated to account for differences in types, constants, etc.
491360SN/A//
492360SN/A//////////////////////////////////////////////////////////////////////
493360SN/A
49411759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4953113Sgblack@eecs.umich.edu#if NO_STAT64
4963113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4973113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4983113Sgblack@eecs.umich.edu#else
4993113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
5003113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
5013113Sgblack@eecs.umich.edu#endif
5023113Sgblack@eecs.umich.edu
5033113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
5043113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
5053113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
5063113Sgblack@eecs.umich.edu
5073113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
50812032Sandreas.sandberg@arm.comvoid
5093113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
5103113Sgblack@eecs.umich.edu{
5114189Sgblack@eecs.umich.edu    using namespace TheISA;
5124189Sgblack@eecs.umich.edu
5133113Sgblack@eecs.umich.edu    if (fakeTTY)
5143113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5153113Sgblack@eecs.umich.edu    else
5163113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5178737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5183113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5198737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5203277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5215515SMichael.Adler@intel.com    if (fakeTTY) {
5225515SMichael.Adler@intel.com        // Claim to be a character device
5235515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5245515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5255515SMichael.Adler@intel.com    }
5268737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5273277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5288737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5293277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5308737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5313277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5328737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5333113Sgblack@eecs.umich.edu    if (fakeTTY)
5343113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5353113Sgblack@eecs.umich.edu    else
5363113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5378737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5383113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5398737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5403114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5418737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5423114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5438737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5443114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5458737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
54611906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
5474061Sgblack@eecs.umich.edu    // consistently across different hosts.
5484061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5498737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5503113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5518737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5523113Sgblack@eecs.umich.edu}
5533113Sgblack@eecs.umich.edu
5543113Sgblack@eecs.umich.edu// Same for stat64
5553113Sgblack@eecs.umich.edu
5563113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
55712032Sandreas.sandberg@arm.comvoid
5583113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5593113Sgblack@eecs.umich.edu{
5604189Sgblack@eecs.umich.edu    using namespace TheISA;
5614189Sgblack@eecs.umich.edu
5623113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5633113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5643113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5658737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5663113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5678737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5683113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5698737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5703113Sgblack@eecs.umich.edu#else
5713113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5723113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5733113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5743113Sgblack@eecs.umich.edu#endif
5753113Sgblack@eecs.umich.edu}
5763113Sgblack@eecs.umich.edu
57711906SBrandon.Potter@amd.com// Here are a couple of convenience functions
5783113Sgblack@eecs.umich.edutemplate<class OS>
57912032Sandreas.sandberg@arm.comvoid
5808852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
58111906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
5823113Sgblack@eecs.umich.edu{
5833113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5843113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5853113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5863113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5873113Sgblack@eecs.umich.edu}
5883113Sgblack@eecs.umich.edu
5893113Sgblack@eecs.umich.edutemplate<class OS>
59012032Sandreas.sandberg@arm.comvoid
5918852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
59211906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
5933113Sgblack@eecs.umich.edu{
5943113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5953113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5966686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5973113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5983113Sgblack@eecs.umich.edu}
5993113Sgblack@eecs.umich.edu
60011759Sbrandon.potter@amd.comtemplate <class OS>
60112032Sandreas.sandberg@arm.comvoid
60211759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
60311759Sbrandon.potter@amd.com                 hst_statfs *host)
60411759Sbrandon.potter@amd.com{
60511759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
60611759Sbrandon.potter@amd.com
60711812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
60811812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
60911812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
61011759Sbrandon.potter@amd.com#else
61111812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
61211759Sbrandon.potter@amd.com#endif
61311759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
61411759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
61511759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
61611759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
61711759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
61811759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
61911812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
62011812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
62111812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
62211812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
62311812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
62411812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
62511812Sbaz21@cam.ac.uk#else
62611759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
62711759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
62811812Sbaz21@cam.ac.uk#endif
62911812Sbaz21@cam.ac.uk#if defined(__linux__)
63011759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
63111812Sbaz21@cam.ac.uk#else
63211812Sbaz21@cam.ac.uk    /*
63311812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
63411812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
63511812Sbaz21@cam.ac.uk     */
63611812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
63711812Sbaz21@cam.ac.uk#endif
63811759Sbrandon.potter@amd.com
63911759Sbrandon.potter@amd.com    tgt.copyOut(mem);
64011759Sbrandon.potter@amd.com}
64111759Sbrandon.potter@amd.com
642378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
643378SN/A/// only to find out if their stdout is a tty, to determine whether to
6449141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
6459141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
646360SN/Atemplate <class OS>
6471450SN/ASyscallReturn
64811856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
649360SN/A{
6506701Sgblack@eecs.umich.edu    int index = 0;
65111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
65211856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
653360SN/A
65410930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
655360SN/A
65611856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
65711856Sbrandon.potter@amd.com        return -ENOTTY;
65810496Ssteve.reinhardt@amd.com
65911856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
66011856Sbrandon.potter@amd.com    if (!dfdp)
6611458SN/A        return -EBADF;
662360SN/A
66311856Sbrandon.potter@amd.com    /**
66411856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
66511856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
66611856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
66711856Sbrandon.potter@amd.com     */
66811856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
66911856Sbrandon.potter@amd.com    if (emul_driver)
67011856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
67110496Ssteve.reinhardt@amd.com
67211856Sbrandon.potter@amd.com    /**
67311856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
67411856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
67511856Sbrandon.potter@amd.com     */
67611856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
67710930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
6789141Smarc.orr@gmail.com    return -ENOTTY;
679360SN/A}
680360SN/A
681360SN/Atemplate <class OS>
68211907SBrandon.Potter@amd.comSyscallReturn
68311907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
68411907SBrandon.Potter@amd.com         bool isopenat)
685360SN/A{
68611907SBrandon.Potter@amd.com    int index = 0;
68711907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
68811907SBrandon.Potter@amd.com
68911907SBrandon.Potter@amd.com    /**
69011907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
69111907SBrandon.Potter@amd.com     * descriptor from the simulated process.
69211907SBrandon.Potter@amd.com     */
69311907SBrandon.Potter@amd.com    if (isopenat)
69411907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
69511907SBrandon.Potter@amd.com
69611907SBrandon.Potter@amd.com    /**
69711907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
69811907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
69911907SBrandon.Potter@amd.com     */
700360SN/A    std::string path;
70111907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
7021458SN/A        return -EFAULT;
703360SN/A
70411907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
70511907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
70611907SBrandon.Potter@amd.com#else
70711907SBrandon.Potter@amd.com    int host_flags = 0;
70811907SBrandon.Potter@amd.com#endif
70911907SBrandon.Potter@amd.com    /**
71011907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
71111907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
71211907SBrandon.Potter@amd.com     */
71311907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
714360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
71511907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
71611907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
71711907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
718360SN/A        }
719360SN/A    }
72011907SBrandon.Potter@amd.com    if (tgt_flags) {
72111907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
72211907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
72311907SBrandon.Potter@amd.com    }
724360SN/A#ifdef __CYGWIN32__
72511907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
726360SN/A#endif
727360SN/A
72811907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
7293669Sbinkertn@umich.edu
73011907SBrandon.Potter@amd.com    /**
73111907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
73211907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
73311907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
73411907SBrandon.Potter@amd.com     * create a full path.
73511907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
73611907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
73711907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
73811907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
73911907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
74011907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
74111907SBrandon.Potter@amd.com     */
74211907SBrandon.Potter@amd.com    if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) {
74311907SBrandon.Potter@amd.com        path = p->fullPath(path);
74411907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
74511907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
74611907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
74711907SBrandon.Potter@amd.com        if (!ffdp)
74811907SBrandon.Potter@amd.com            return -EBADF;
74913371Sciro.santilli@arm.com        path.insert(0, ffdp->getFileName() + "/");
75011907SBrandon.Potter@amd.com    }
7511706SN/A
75211907SBrandon.Potter@amd.com    /**
75311907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
75411907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
75511907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
75611907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
75711907SBrandon.Potter@amd.com     */
75810496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
75910496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
76011907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
76111907SBrandon.Potter@amd.com        if (drv) {
76211907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
76311907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
76411907SBrandon.Potter@amd.com                            isopenat ? "at" : "", path.c_str());
76511907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
76610496Ssteve.reinhardt@amd.com        }
76711907SBrandon.Potter@amd.com        /**
76811907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
76911907SBrandon.Potter@amd.com         * as /dev/zero
77011907SBrandon.Potter@amd.com         */
77110496Ssteve.reinhardt@amd.com    }
77210496Ssteve.reinhardt@amd.com
77311907SBrandon.Potter@amd.com    /**
77411907SBrandon.Potter@amd.com     * Some special paths and files cannot be called on the host and need
77511907SBrandon.Potter@amd.com     * to be handled as special cases inside the simulator.
77611907SBrandon.Potter@amd.com     * If the full path that was created above does not match any of the
77711907SBrandon.Potter@amd.com     * special cases, pass it through to the open call on the host to let
77811907SBrandon.Potter@amd.com     * the host open the file on our behalf.
77911907SBrandon.Potter@amd.com     * If the host cannot open the file, return the host's error code back
78011907SBrandon.Potter@amd.com     * through the system call to the simulated process.
78111907SBrandon.Potter@amd.com     */
78211907SBrandon.Potter@amd.com    int sim_fd = -1;
78311907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
78411907SBrandon.Potter@amd.com            { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" };
78511907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
78611907SBrandon.Potter@amd.com        if (startswith(path, entry))
78711907SBrandon.Potter@amd.com            sim_fd = OS::openSpecialFile(path, p, tc);
78811907SBrandon.Potter@amd.com    }
78911907SBrandon.Potter@amd.com    if (sim_fd == -1) {
79011907SBrandon.Potter@amd.com        sim_fd = open(path.c_str(), host_flags, mode);
79111907SBrandon.Potter@amd.com    }
79211907SBrandon.Potter@amd.com    if (sim_fd == -1) {
79311907SBrandon.Potter@amd.com        int local = -errno;
79411907SBrandon.Potter@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n",
79511907SBrandon.Potter@amd.com                        isopenat ? "at" : "", path.c_str());
79611907SBrandon.Potter@amd.com        return local;
79711907SBrandon.Potter@amd.com    }
798360SN/A
79911907SBrandon.Potter@amd.com    /**
80011907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
80111907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
80211907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
80311907SBrandon.Potter@amd.com     * file descriptor.
80411907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
80511907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
80611907SBrandon.Potter@amd.com     */
80711907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
80811907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
80911907SBrandon.Potter@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n",
81011907SBrandon.Potter@amd.com                    isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str());
81111907SBrandon.Potter@amd.com    return tgt_fd;
812360SN/A}
813360SN/A
81410027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
81510027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
81610027SChris.Adeniyi-Jones@arm.comSyscallReturn
81711851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
81810027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
81910027SChris.Adeniyi-Jones@arm.com{
82011907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
82110027SChris.Adeniyi-Jones@arm.com}
82210027SChris.Adeniyi-Jones@arm.com
82310027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
82410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
82510027SChris.Adeniyi-Jones@arm.comSyscallReturn
82611851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
82711851Sbrandon.potter@amd.com           ThreadContext *tc)
82810027SChris.Adeniyi-Jones@arm.com{
82911907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
83010027SChris.Adeniyi-Jones@arm.com}
83110027SChris.Adeniyi-Jones@arm.com
83210633Smichaelupton@gmail.com/// Target unlinkat() handler.
83310633Smichaelupton@gmail.comtemplate <class OS>
83410633Smichaelupton@gmail.comSyscallReturn
83511851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
83610633Smichaelupton@gmail.com             ThreadContext *tc)
83710633Smichaelupton@gmail.com{
83810633Smichaelupton@gmail.com    int index = 0;
83910633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
84010633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
84110633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
84210633Smichaelupton@gmail.com
84310633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
84410633Smichaelupton@gmail.com}
84510633Smichaelupton@gmail.com
84610203SAli.Saidi@ARM.com/// Target facessat() handler
84710203SAli.Saidi@ARM.comtemplate <class OS>
84810203SAli.Saidi@ARM.comSyscallReturn
84911851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
85011851Sbrandon.potter@amd.com              ThreadContext *tc)
85110203SAli.Saidi@ARM.com{
85210203SAli.Saidi@ARM.com    int index = 0;
85310203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
85410203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
85510203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
85610203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
85710203SAli.Saidi@ARM.com}
85810203SAli.Saidi@ARM.com
85910203SAli.Saidi@ARM.com/// Target readlinkat() handler
86010203SAli.Saidi@ARM.comtemplate <class OS>
86110203SAli.Saidi@ARM.comSyscallReturn
86211851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
86311851Sbrandon.potter@amd.com               ThreadContext *tc)
86410203SAli.Saidi@ARM.com{
86510203SAli.Saidi@ARM.com    int index = 0;
86610203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
86710203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
86810203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
86910203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
87010203SAli.Saidi@ARM.com}
87110203SAli.Saidi@ARM.com
87210850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
87310850SGiacomo.Gabrielli@arm.comtemplate <class OS>
87410850SGiacomo.Gabrielli@arm.comSyscallReturn
87511851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
87610850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
87710850SGiacomo.Gabrielli@arm.com{
87810850SGiacomo.Gabrielli@arm.com    int index = 0;
87910850SGiacomo.Gabrielli@arm.com
88010850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
88110850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
88210850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
88310850SGiacomo.Gabrielli@arm.com
88410850SGiacomo.Gabrielli@arm.com    std::string old_name;
88510850SGiacomo.Gabrielli@arm.com
88610850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
88710850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
88810850SGiacomo.Gabrielli@arm.com        return -EFAULT;
88910850SGiacomo.Gabrielli@arm.com
89010850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
89110850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
89210850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
89310850SGiacomo.Gabrielli@arm.com
89410850SGiacomo.Gabrielli@arm.com    std::string new_name;
89510850SGiacomo.Gabrielli@arm.com
89610850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
89710850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
89810850SGiacomo.Gabrielli@arm.com        return -EFAULT;
89910850SGiacomo.Gabrielli@arm.com
90010850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
90110850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
90210850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
90310850SGiacomo.Gabrielli@arm.com
90410850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
90510850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
90610850SGiacomo.Gabrielli@arm.com}
90710850SGiacomo.Gabrielli@arm.com
9086640Svince@csl.cornell.edu/// Target sysinfo() handler.
9096640Svince@csl.cornell.edutemplate <class OS>
9106640Svince@csl.cornell.eduSyscallReturn
91111851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
91211851Sbrandon.potter@amd.com            ThreadContext *tc)
9136640Svince@csl.cornell.edu{
9146640Svince@csl.cornell.edu
9156701Sgblack@eecs.umich.edu    int index = 0;
9166701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
91710793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
9186640Svince@csl.cornell.edu
91911758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
92011758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
92111758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
9226640Svince@csl.cornell.edu
9238706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
9246640Svince@csl.cornell.edu
9256701Sgblack@eecs.umich.edu    return 0;
9266640Svince@csl.cornell.edu}
927360SN/A
9281999SN/A/// Target chmod() handler.
9291999SN/Atemplate <class OS>
9301999SN/ASyscallReturn
93111851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
9322680Sktlim@umich.edu          ThreadContext *tc)
9331999SN/A{
9341999SN/A    std::string path;
9351999SN/A
9366701Sgblack@eecs.umich.edu    int index = 0;
9378852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9386701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9391999SN/A        return -EFAULT;
9406701Sgblack@eecs.umich.edu    }
9411999SN/A
9426701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
9431999SN/A    mode_t hostMode = 0;
9441999SN/A
9451999SN/A    // XXX translate mode flags via OS::something???
9461999SN/A    hostMode = mode;
9471999SN/A
9483669Sbinkertn@umich.edu    // Adjust path for current working directory
9493669Sbinkertn@umich.edu    path = process->fullPath(path);
9503669Sbinkertn@umich.edu
9511999SN/A    // do the chmod
9521999SN/A    int result = chmod(path.c_str(), hostMode);
9531999SN/A    if (result < 0)
9542218SN/A        return -errno;
9551999SN/A
9561999SN/A    return 0;
9571999SN/A}
9581999SN/A
95913570Sbrandon.potter@amd.comtemplate <class OS>
96013570Sbrandon.potter@amd.comSyscallReturn
96113570Sbrandon.potter@amd.compollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
96213570Sbrandon.potter@amd.com{
96313570Sbrandon.potter@amd.com    int index = 0;
96413570Sbrandon.potter@amd.com    Addr fdsPtr = p->getSyscallArg(tc, index);
96513570Sbrandon.potter@amd.com    int nfds = p->getSyscallArg(tc, index);
96613570Sbrandon.potter@amd.com    int tmout = p->getSyscallArg(tc, index);
96713570Sbrandon.potter@amd.com
96813570Sbrandon.potter@amd.com    BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds);
96913570Sbrandon.potter@amd.com    fdsBuf.copyIn(tc->getMemProxy());
97013570Sbrandon.potter@amd.com
97113570Sbrandon.potter@amd.com    /**
97213570Sbrandon.potter@amd.com     * Record the target file descriptors in a local variable. We need to
97313570Sbrandon.potter@amd.com     * replace them with host file descriptors but we need a temporary copy
97413570Sbrandon.potter@amd.com     * for later. Afterwards, replace each target file descriptor in the
97513570Sbrandon.potter@amd.com     * poll_fd array with its host_fd.
97613570Sbrandon.potter@amd.com     */
97713570Sbrandon.potter@amd.com    int temp_tgt_fds[nfds];
97813570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
97913570Sbrandon.potter@amd.com        temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd;
98013570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
98113570Sbrandon.potter@amd.com        auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
98213570Sbrandon.potter@amd.com        if (!hbfdp)
98313570Sbrandon.potter@amd.com            return -EBADF;
98413570Sbrandon.potter@amd.com        auto host_fd = hbfdp->getSimFD();
98513570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd;
98613570Sbrandon.potter@amd.com    }
98713570Sbrandon.potter@amd.com
98813570Sbrandon.potter@amd.com    /**
98913570Sbrandon.potter@amd.com     * We cannot allow an infinite poll to occur or it will inevitably cause
99013570Sbrandon.potter@amd.com     * a deadlock in the gem5 simulator with clone. We must pass in tmout with
99113570Sbrandon.potter@amd.com     * a non-negative value, however it also makes no sense to poll on the
99213570Sbrandon.potter@amd.com     * underlying host for any other time than tmout a zero timeout.
99313570Sbrandon.potter@amd.com     */
99413570Sbrandon.potter@amd.com    int status;
99513570Sbrandon.potter@amd.com    if (tmout < 0) {
99613570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
99713570Sbrandon.potter@amd.com        if (status == 0) {
99813570Sbrandon.potter@amd.com            /**
99913570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
100013570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try
100113570Sbrandon.potter@amd.com             * to return the signal interrupt instead.
100213570Sbrandon.potter@amd.com             */
100313570Sbrandon.potter@amd.com            System *sysh = tc->getSystemPtr();
100413570Sbrandon.potter@amd.com            std::list<BasicSignal>::iterator it;
100513570Sbrandon.potter@amd.com            for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++)
100613570Sbrandon.potter@amd.com                if (it->receiver == p)
100713570Sbrandon.potter@amd.com                    return -EINTR;
100813570Sbrandon.potter@amd.com            return SyscallReturn::retry();
100913570Sbrandon.potter@amd.com        }
101013570Sbrandon.potter@amd.com    } else
101113570Sbrandon.potter@amd.com        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
101213570Sbrandon.potter@amd.com
101313570Sbrandon.potter@amd.com    if (status == -1)
101413570Sbrandon.potter@amd.com        return -errno;
101513570Sbrandon.potter@amd.com
101613570Sbrandon.potter@amd.com    /**
101713570Sbrandon.potter@amd.com     * Replace each host_fd in the returned poll_fd array with its original
101813570Sbrandon.potter@amd.com     * target file descriptor.
101913570Sbrandon.potter@amd.com     */
102013570Sbrandon.potter@amd.com    for (index = 0; index < nfds; index++) {
102113570Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
102213570Sbrandon.potter@amd.com        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd;
102313570Sbrandon.potter@amd.com    }
102413570Sbrandon.potter@amd.com
102513570Sbrandon.potter@amd.com    /**
102613570Sbrandon.potter@amd.com     * Copy out the pollfd struct because the host may have updated fields
102713570Sbrandon.potter@amd.com     * in the structure.
102813570Sbrandon.potter@amd.com     */
102913570Sbrandon.potter@amd.com    fdsBuf.copyOut(tc->getMemProxy());
103013570Sbrandon.potter@amd.com
103113570Sbrandon.potter@amd.com    return status;
103213570Sbrandon.potter@amd.com}
10331999SN/A
10341999SN/A/// Target fchmod() handler.
10351999SN/Atemplate <class OS>
10361999SN/ASyscallReturn
103711856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
10381999SN/A{
10396701Sgblack@eecs.umich.edu    int index = 0;
104011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
104111856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
104210931Sbrandon.potter@amd.com
104311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
104411856Sbrandon.potter@amd.com    if (!ffdp)
10451999SN/A        return -EBADF;
104611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
10471999SN/A
104811856Sbrandon.potter@amd.com    mode_t hostMode = mode;
10491999SN/A
105011856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
10511999SN/A
105211856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
10531999SN/A}
10541999SN/A
10555877Shsul@eecs.umich.edu/// Target mremap() handler.
10565877Shsul@eecs.umich.edutemplate <class OS>
10575877Shsul@eecs.umich.eduSyscallReturn
105811851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
10595877Shsul@eecs.umich.edu{
10606701Sgblack@eecs.umich.edu    int index = 0;
10616701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
10626701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
10636701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
10646701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
106510027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
106610027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
106710027SChris.Adeniyi-Jones@arm.com
106810027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
106910027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
10705877Shsul@eecs.umich.edu
107110318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
107210318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
10735877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
10745877Shsul@eecs.umich.edu        return -EINVAL;
10755877Shsul@eecs.umich.edu    }
10765877Shsul@eecs.umich.edu
107710486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
107810486Stjablin@gmail.com
10795877Shsul@eecs.umich.edu    if (new_length > old_length) {
108011905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
108111905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
108211905SBrandon.Potter@amd.com
108311905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
108410027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
108512206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
108612206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
10875877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
108811905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
108911905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
10905877Shsul@eecs.umich.edu            return start;
10915877Shsul@eecs.umich.edu        } else {
109210027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
10935877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
10945877Shsul@eecs.umich.edu                return -ENOMEM;
10955877Shsul@eecs.umich.edu            } else {
109612206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
109712206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
109812206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
109912206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
110012206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
110112206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
110212206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
110312206Srico.amslinger@informatik.uni-augsburg.de                }
110412206Srico.amslinger@informatik.uni-augsburg.de
110510027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
110610027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
110710027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
110810027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
11095877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
111010027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
111110027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
111210027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
111310027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
111412206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
111512206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
111612206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
111712206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
111810027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
111910027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
112010027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
112110027SChris.Adeniyi-Jones@arm.com                }
112210027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
112310027SChris.Adeniyi-Jones@arm.com                return new_start;
11245877Shsul@eecs.umich.edu            }
11255877Shsul@eecs.umich.edu        }
11265877Shsul@eecs.umich.edu    } else {
112710027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
112810027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
11298601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
113010027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
11315877Shsul@eecs.umich.edu    }
11325877Shsul@eecs.umich.edu}
11331999SN/A
1134378SN/A/// Target stat() handler.
1135360SN/Atemplate <class OS>
11361450SN/ASyscallReturn
113711851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
11382680Sktlim@umich.edu         ThreadContext *tc)
1139360SN/A{
1140360SN/A    std::string path;
1141360SN/A
11426701Sgblack@eecs.umich.edu    int index = 0;
11438852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11446701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11456701Sgblack@eecs.umich.edu        return -EFAULT;
11466701Sgblack@eecs.umich.edu    }
11476701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1148360SN/A
11493669Sbinkertn@umich.edu    // Adjust path for current working directory
11503669Sbinkertn@umich.edu    path = process->fullPath(path);
11513669Sbinkertn@umich.edu
1152360SN/A    struct stat hostBuf;
1153360SN/A    int result = stat(path.c_str(), &hostBuf);
1154360SN/A
1155360SN/A    if (result < 0)
11562218SN/A        return -errno;
1157360SN/A
11588706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1159360SN/A
11601458SN/A    return 0;
1161360SN/A}
1162360SN/A
1163360SN/A
11645074Ssaidi@eecs.umich.edu/// Target stat64() handler.
11655074Ssaidi@eecs.umich.edutemplate <class OS>
11665074Ssaidi@eecs.umich.eduSyscallReturn
116711851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
11685074Ssaidi@eecs.umich.edu           ThreadContext *tc)
11695074Ssaidi@eecs.umich.edu{
11705074Ssaidi@eecs.umich.edu    std::string path;
11715074Ssaidi@eecs.umich.edu
11726701Sgblack@eecs.umich.edu    int index = 0;
11738852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11746701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
11755074Ssaidi@eecs.umich.edu        return -EFAULT;
11766701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11775074Ssaidi@eecs.umich.edu
11785074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
11795074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
11805074Ssaidi@eecs.umich.edu
11815208Ssaidi@eecs.umich.edu#if NO_STAT64
11825208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
11835208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
11845208Ssaidi@eecs.umich.edu#else
11855074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
11865074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
11875208Ssaidi@eecs.umich.edu#endif
11885074Ssaidi@eecs.umich.edu
11895074Ssaidi@eecs.umich.edu    if (result < 0)
11905074Ssaidi@eecs.umich.edu        return -errno;
11915074Ssaidi@eecs.umich.edu
11928706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11935074Ssaidi@eecs.umich.edu
11945074Ssaidi@eecs.umich.edu    return 0;
11955074Ssaidi@eecs.umich.edu}
11965074Ssaidi@eecs.umich.edu
11975074Ssaidi@eecs.umich.edu
119810027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
119910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
120010027SChris.Adeniyi-Jones@arm.comSyscallReturn
120111851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
120210027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
120310027SChris.Adeniyi-Jones@arm.com{
120410027SChris.Adeniyi-Jones@arm.com    int index = 0;
120510027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
120610027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
120710793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
120810027SChris.Adeniyi-Jones@arm.com
120910027SChris.Adeniyi-Jones@arm.com    std::string path;
121010027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
121110027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
121210027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
121310027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
121410027SChris.Adeniyi-Jones@arm.com
121510027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
121610027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
121710027SChris.Adeniyi-Jones@arm.com
121810027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
121910027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
122010027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
122110027SChris.Adeniyi-Jones@arm.com#else
122210027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
122310027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
122410027SChris.Adeniyi-Jones@arm.com#endif
122510027SChris.Adeniyi-Jones@arm.com
122610027SChris.Adeniyi-Jones@arm.com    if (result < 0)
122710027SChris.Adeniyi-Jones@arm.com        return -errno;
122810027SChris.Adeniyi-Jones@arm.com
122910027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
123010027SChris.Adeniyi-Jones@arm.com
123110027SChris.Adeniyi-Jones@arm.com    return 0;
123210027SChris.Adeniyi-Jones@arm.com}
123310027SChris.Adeniyi-Jones@arm.com
123410027SChris.Adeniyi-Jones@arm.com
12351999SN/A/// Target fstat64() handler.
12361999SN/Atemplate <class OS>
12371999SN/ASyscallReturn
123811856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
12391999SN/A{
12406701Sgblack@eecs.umich.edu    int index = 0;
124111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
124211856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
124310931Sbrandon.potter@amd.com
124411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
124511856Sbrandon.potter@amd.com    if (!ffdp)
12461999SN/A        return -EBADF;
124711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
12481999SN/A
12492764Sstever@eecs.umich.edu#if NO_STAT64
12502064SN/A    struct stat  hostBuf;
125110931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
12522064SN/A#else
12532064SN/A    struct stat64  hostBuf;
125410931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
12552064SN/A#endif
12561999SN/A
12571999SN/A    if (result < 0)
12582218SN/A        return -errno;
12591999SN/A
126010931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
12611999SN/A
12621999SN/A    return 0;
12631999SN/A}
12641999SN/A
12651999SN/A
1266378SN/A/// Target lstat() handler.
1267360SN/Atemplate <class OS>
12681450SN/ASyscallReturn
126911851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
12702680Sktlim@umich.edu          ThreadContext *tc)
1271360SN/A{
1272360SN/A    std::string path;
1273360SN/A
12746701Sgblack@eecs.umich.edu    int index = 0;
12758852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12766701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12776701Sgblack@eecs.umich.edu        return -EFAULT;
12786701Sgblack@eecs.umich.edu    }
12796701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1280360SN/A
12813669Sbinkertn@umich.edu    // Adjust path for current working directory
12823669Sbinkertn@umich.edu    path = process->fullPath(path);
12833669Sbinkertn@umich.edu
1284360SN/A    struct stat hostBuf;
1285360SN/A    int result = lstat(path.c_str(), &hostBuf);
1286360SN/A
1287360SN/A    if (result < 0)
12881458SN/A        return -errno;
1289360SN/A
12908706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1291360SN/A
12921458SN/A    return 0;
1293360SN/A}
1294360SN/A
12951999SN/A/// Target lstat64() handler.
12961999SN/Atemplate <class OS>
12971999SN/ASyscallReturn
129811851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
12992680Sktlim@umich.edu            ThreadContext *tc)
13001999SN/A{
13011999SN/A    std::string path;
13021999SN/A
13036701Sgblack@eecs.umich.edu    int index = 0;
13048852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13056701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13066701Sgblack@eecs.umich.edu        return -EFAULT;
13076701Sgblack@eecs.umich.edu    }
13086701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
13091999SN/A
13103669Sbinkertn@umich.edu    // Adjust path for current working directory
13113669Sbinkertn@umich.edu    path = process->fullPath(path);
13123669Sbinkertn@umich.edu
13132764Sstever@eecs.umich.edu#if NO_STAT64
13142064SN/A    struct stat hostBuf;
13152064SN/A    int result = lstat(path.c_str(), &hostBuf);
13162064SN/A#else
13171999SN/A    struct stat64 hostBuf;
13181999SN/A    int result = lstat64(path.c_str(), &hostBuf);
13192064SN/A#endif
13201999SN/A
13211999SN/A    if (result < 0)
13221999SN/A        return -errno;
13231999SN/A
13248706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
13251999SN/A
13261999SN/A    return 0;
13271999SN/A}
13281999SN/A
1329378SN/A/// Target fstat() handler.
1330360SN/Atemplate <class OS>
13311450SN/ASyscallReturn
133211856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1333360SN/A{
13346701Sgblack@eecs.umich.edu    int index = 0;
133511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
133611856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1337360SN/A
133811380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1339360SN/A
134011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
134111856Sbrandon.potter@amd.com    if (!ffdp)
13421458SN/A        return -EBADF;
134311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1344360SN/A
1345360SN/A    struct stat hostBuf;
134610931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1347360SN/A
1348360SN/A    if (result < 0)
13491458SN/A        return -errno;
1350360SN/A
135110931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
13522021SN/A
13531458SN/A    return 0;
1354360SN/A}
1355360SN/A
13561706SN/A/// Target statfs() handler.
13571706SN/Atemplate <class OS>
13581706SN/ASyscallReturn
135911851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
13602680Sktlim@umich.edu           ThreadContext *tc)
13611706SN/A{
136211799Sbrandon.potter@amd.com#if NO_STATFS
136311799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
136411799Sbrandon.potter@amd.com#else
13651706SN/A    std::string path;
13661706SN/A
13676701Sgblack@eecs.umich.edu    int index = 0;
13688852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
13696701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
13706701Sgblack@eecs.umich.edu        return -EFAULT;
13716701Sgblack@eecs.umich.edu    }
13726701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
13731706SN/A
13743669Sbinkertn@umich.edu    // Adjust path for current working directory
13753669Sbinkertn@umich.edu    path = process->fullPath(path);
13763669Sbinkertn@umich.edu
13771706SN/A    struct statfs hostBuf;
13781706SN/A    int result = statfs(path.c_str(), &hostBuf);
13791706SN/A
13801706SN/A    if (result < 0)
13812218SN/A        return -errno;
13821706SN/A
138311759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
138411799Sbrandon.potter@amd.com#endif
13851706SN/A    return 0;
13861706SN/A}
13871706SN/A
138811886Sbrandon.potter@amd.comtemplate <class OS>
138911886Sbrandon.potter@amd.comSyscallReturn
139011886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
139111886Sbrandon.potter@amd.com{
139211886Sbrandon.potter@amd.com    int index = 0;
139312426Sqtt2@cornell.edu
139413557Sgabeblack@google.com    RegVal flags = p->getSyscallArg(tc, index);
139513557Sgabeblack@google.com    RegVal newStack = p->getSyscallArg(tc, index);
139611886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
139712426Sqtt2@cornell.edu
139813534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
139912426Sqtt2@cornell.edu    /**
140013534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
140112426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
140212426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
140312426Sqtt2@cornell.edu     */
140413536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
140512426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
140612426Sqtt2@cornell.edu#else
140711886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
140813536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
140912426Sqtt2@cornell.edu#endif
141011886Sbrandon.potter@amd.com
141111886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
141211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
141311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
141411886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
141511886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
141611886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
141711886Sbrandon.potter@amd.com        return -EINVAL;
141811886Sbrandon.potter@amd.com
141911886Sbrandon.potter@amd.com    ThreadContext *ctc;
142011886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
142111886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
142211886Sbrandon.potter@amd.com
142311886Sbrandon.potter@amd.com    /**
142411886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
142511886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
142611886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
142711886Sbrandon.potter@amd.com     * constructor.
142811886Sbrandon.potter@amd.com     */
142911886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
143011886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
143111886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
143211886Sbrandon.potter@amd.com    pp->system = p->system;
143311886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
143411886Sbrandon.potter@amd.com    pp->input.assign("stdin");
143511886Sbrandon.potter@amd.com    pp->output.assign("stdout");
143611886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
143711886Sbrandon.potter@amd.com    pp->uid = p->uid();
143811886Sbrandon.potter@amd.com    pp->euid = p->euid();
143911886Sbrandon.potter@amd.com    pp->gid = p->gid();
144011886Sbrandon.potter@amd.com    pp->egid = p->egid();
144111886Sbrandon.potter@amd.com
144211886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
144311886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
144411886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
144511886Sbrandon.potter@amd.com    do {
144611886Sbrandon.potter@amd.com        temp_pid++;
144711886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
144811886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
144911886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
145011886Sbrandon.potter@amd.com
145111886Sbrandon.potter@amd.com    pp->pid = temp_pid;
145211886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
145311886Sbrandon.potter@amd.com    Process *cp = pp->create();
145411886Sbrandon.potter@amd.com    delete pp;
145511886Sbrandon.potter@amd.com
145611886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
145711886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
145811886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
145911886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
146011886Sbrandon.potter@amd.com
146111886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
146211886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
146311886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
146411886Sbrandon.potter@amd.com        *ptid = cp->pid();
146511886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
146611886Sbrandon.potter@amd.com    }
146711886Sbrandon.potter@amd.com
146811886Sbrandon.potter@amd.com    cp->initState();
146911886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
147011886Sbrandon.potter@amd.com
147111911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
147211911SBrandon.Potter@amd.com        delete cp->sigchld;
147311911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
147411911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
147511911SBrandon.Potter@amd.com        *cp->sigchld = true;
147611911SBrandon.Potter@amd.com    }
147711911SBrandon.Potter@amd.com
147811886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
147911886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
148011886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
148111886Sbrandon.potter@amd.com        *ctid = cp->pid();
148211886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
148311886Sbrandon.potter@amd.com    }
148411886Sbrandon.potter@amd.com
148511886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
148611886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
148711886Sbrandon.potter@amd.com
148811886Sbrandon.potter@amd.com    ctc->clearArchRegs();
148911886Sbrandon.potter@amd.com
149013536Sandreas.sandberg@arm.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
149111886Sbrandon.potter@amd.com
149211886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
149311886Sbrandon.potter@amd.com
149411886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
149511886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
149611886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
149711886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
149811886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
149911886Sbrandon.potter@amd.com#endif
150011886Sbrandon.potter@amd.com
150113535Sandreas.sandberg@arm.com    TheISA::PCState cpc = tc->pcState();
150213535Sandreas.sandberg@arm.com    cpc.advance();
150313535Sandreas.sandberg@arm.com    ctc->pcState(cpc);
150411886Sbrandon.potter@amd.com    ctc->activate();
150511886Sbrandon.potter@amd.com
150611886Sbrandon.potter@amd.com    return cp->pid();
150711886Sbrandon.potter@amd.com}
15081706SN/A
15091706SN/A/// Target fstatfs() handler.
15101706SN/Atemplate <class OS>
15111706SN/ASyscallReturn
151211856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
15131706SN/A{
15146701Sgblack@eecs.umich.edu    int index = 0;
151511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
151611856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
15171706SN/A
151811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
151911856Sbrandon.potter@amd.com    if (!ffdp)
15201706SN/A        return -EBADF;
152111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
15221706SN/A
15231706SN/A    struct statfs hostBuf;
152410931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
15251706SN/A
15261706SN/A    if (result < 0)
15272218SN/A        return -errno;
15281706SN/A
152911759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
15301706SN/A
15311706SN/A    return 0;
15321706SN/A}
15331706SN/A
15341706SN/A
15351999SN/A/// Target writev() handler.
15361999SN/Atemplate <class OS>
15371999SN/ASyscallReturn
153811856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
15391999SN/A{
15406701Sgblack@eecs.umich.edu    int index = 0;
154111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
154210931Sbrandon.potter@amd.com
154311856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
154411856Sbrandon.potter@amd.com    if (!hbfdp)
15451999SN/A        return -EBADF;
154611856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
15471999SN/A
154811856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
154911856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
155011856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
15511999SN/A    struct iovec hiov[count];
15526227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
15531999SN/A        typename OS::tgt_iovec tiov;
15542461SN/A
155511856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
155611856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
15578737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
15581999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
155911856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
156011856Sbrandon.potter@amd.com                      hiov[i].iov_len);
15611999SN/A    }
15621999SN/A
156310931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
15641999SN/A
15656227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
15661999SN/A        delete [] (char *)hiov[i].iov_base;
15671999SN/A
15681999SN/A    if (result < 0)
15692218SN/A        return -errno;
15701999SN/A
157110629Sjthestness@gmail.com    return result;
15721999SN/A}
15731999SN/A
157411385Sbrandon.potter@amd.com/// Real mmap handler.
1575360SN/Atemplate <class OS>
15761450SN/ASyscallReturn
157711851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
157811385Sbrandon.potter@amd.com         bool is_mmap2)
1579360SN/A{
15806701Sgblack@eecs.umich.edu    int index = 0;
15816701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
15826701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
158311383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
158411383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
15858324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
158610486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1587360SN/A
158811385Sbrandon.potter@amd.com    if (is_mmap2)
158911385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
15909008Sgblack@eecs.umich.edu
159111383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
159211383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
159311383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
159411383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
159511383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
159611383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
159711383Sbrandon.potter@amd.com        !length) {
159811383Sbrandon.potter@amd.com        return -EINVAL;
159911383Sbrandon.potter@amd.com    }
16008324Ssteve.reinhardt@amd.com
160111383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
160211383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
160311383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
160411383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
160511383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
160611383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
160711383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
160811383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
160911383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
161011383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
161111383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
161211383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
161311383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
161411383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
161511383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
161611383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
161711383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
161811383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
161911383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
162011383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
162111383Sbrandon.potter@amd.com        // than we currently maintain.
162211383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
162311383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
162411383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
16258324Ssteve.reinhardt@amd.com    }
16265877Shsul@eecs.umich.edu
162710486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
162810486Stjablin@gmail.com
162911383Sbrandon.potter@amd.com    int sim_fd = -1;
163011383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
163111383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
163211856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
163311624Smichael.lebeane@amd.com
163411856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
163511856Sbrandon.potter@amd.com        if (dfdp) {
163611856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
163711856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
163811624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
163911624Smichael.lebeane@amd.com        }
164011624Smichael.lebeane@amd.com
164111856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
164211856Sbrandon.potter@amd.com        if (!ffdp)
164311383Sbrandon.potter@amd.com            return -EBADF;
164411856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1645360SN/A
164611913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
164711383Sbrandon.potter@amd.com                                    sim_fd, offset);
16488600Ssteve.reinhardt@amd.com
164911383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
165011383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
165111383Sbrandon.potter@amd.com            return -errno;
16528600Ssteve.reinhardt@amd.com        }
16532544SN/A    }
16542544SN/A
165511383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
165611383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
165711383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
165811905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
165911905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
166011905SBrandon.Potter@amd.com
166111905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
166211905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
166311905SBrandon.Potter@amd.com
166411905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
166511383Sbrandon.potter@amd.com    }
166611383Sbrandon.potter@amd.com
166711383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
166811383Sbrandon.potter@amd.com                    start, start + length - 1);
166911383Sbrandon.potter@amd.com
167011383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
167111383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
167211383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
167311383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
167411383Sbrandon.potter@amd.com    if (clobber) {
167511383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
167611383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
167711383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
167811383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
167911383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
16808600Ssteve.reinhardt@amd.com        }
16816672Sgblack@eecs.umich.edu    }
16828600Ssteve.reinhardt@amd.com
168311383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
168411383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
168511383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
16868601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
16872544SN/A
168811383Sbrandon.potter@amd.com    // Transfer content into target address space.
168911383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
169011383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
169111383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
169211383Sbrandon.potter@amd.com        // with something like:
169311383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
169411383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
169511383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
169611383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
169711383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
169811383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
169911383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
170011383Sbrandon.potter@amd.com    } else {
170111383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
170211383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
170311383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
170411383Sbrandon.potter@amd.com        // the host into the target address space.
170511383Sbrandon.potter@amd.com        struct stat file_stat;
170611383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
170711383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
170811383Sbrandon.potter@amd.com
170911383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
171011383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
171111383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
171211383Sbrandon.potter@amd.com        // on the specified offset into the file.
171311383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
171411383Sbrandon.potter@amd.com                                 length);
171511383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
171611383Sbrandon.potter@amd.com
171711383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
171811383Sbrandon.potter@amd.com        munmap(pmap, length);
171911383Sbrandon.potter@amd.com
172011392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
172111392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
172211392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
172311392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
172411392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
172511392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
172611392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
172711392Sbrandon.potter@amd.com        if (interpreter) {
172811392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
172911392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
173011392Sbrandon.potter@amd.com
173111392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
173211392Sbrandon.potter@amd.com
173311392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
173411856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
173511856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
173611856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
173711392Sbrandon.potter@amd.com
173811392Sbrandon.potter@amd.com                if (lib) {
173911392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
174011392Sbrandon.potter@amd.com                                        lib->textBase(), start);
174111392Sbrandon.potter@amd.com                }
174211392Sbrandon.potter@amd.com            }
174311392Sbrandon.potter@amd.com        }
174411392Sbrandon.potter@amd.com
174511383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
174611383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
174711383Sbrandon.potter@amd.com        // execution (hopefully).
174811383Sbrandon.potter@amd.com    }
174911383Sbrandon.potter@amd.com
17501458SN/A    return start;
1751360SN/A}
1752360SN/A
175311593Santhony.gutierrez@amd.comtemplate <class OS>
175411593Santhony.gutierrez@amd.comSyscallReturn
175511851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
175611593Santhony.gutierrez@amd.com{
175711593Santhony.gutierrez@amd.com    int index = 0;
175811593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
175911593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
176011593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
176111593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
176211593Santhony.gutierrez@amd.com
176311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
176411856Sbrandon.potter@amd.com    if (!ffdp)
176511593Santhony.gutierrez@amd.com        return -EBADF;
176611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
176711593Santhony.gutierrez@amd.com
176811593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
176911593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
177011593Santhony.gutierrez@amd.com
177111594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
177211593Santhony.gutierrez@amd.com
177311593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
177411593Santhony.gutierrez@amd.com}
177511593Santhony.gutierrez@amd.com
177611385Sbrandon.potter@amd.com/// Target mmap() handler.
177711385Sbrandon.potter@amd.comtemplate <class OS>
177811385Sbrandon.potter@amd.comSyscallReturn
177911851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
178011385Sbrandon.potter@amd.com{
178111385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
178211385Sbrandon.potter@amd.com}
178311385Sbrandon.potter@amd.com
178411385Sbrandon.potter@amd.com/// Target mmap2() handler.
178511385Sbrandon.potter@amd.comtemplate <class OS>
178611385Sbrandon.potter@amd.comSyscallReturn
178711851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
178811385Sbrandon.potter@amd.com{
178911385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
179011385Sbrandon.potter@amd.com}
179111385Sbrandon.potter@amd.com
1792378SN/A/// Target getrlimit() handler.
1793360SN/Atemplate <class OS>
17941450SN/ASyscallReturn
179511851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
179611851Sbrandon.potter@amd.com              ThreadContext *tc)
1797360SN/A{
17986701Sgblack@eecs.umich.edu    int index = 0;
17996701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
18006701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1801360SN/A
1802360SN/A    switch (resource) {
180311906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
180411906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
180511906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
180611906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
180711906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
180811906SBrandon.Potter@amd.com        break;
1809360SN/A
181011906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
181111906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
181211906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
181311906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
181411906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
181511906SBrandon.Potter@amd.com        break;
18165877Shsul@eecs.umich.edu
181711906SBrandon.Potter@amd.com      default:
181811906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
181911906SBrandon.Potter@amd.com        return -EINVAL;
182011906SBrandon.Potter@amd.com        break;
1821360SN/A    }
1822360SN/A
18238706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
18241458SN/A    return 0;
1825360SN/A}
1826360SN/A
182712235Sar4jc@virginia.edutemplate <class OS>
182812235Sar4jc@virginia.eduSyscallReturn
182912235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
183012235Sar4jc@virginia.edu            ThreadContext *tc)
183112235Sar4jc@virginia.edu{
183212235Sar4jc@virginia.edu    int index = 0;
183312235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
183412235Sar4jc@virginia.edu    {
183512235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
183612235Sar4jc@virginia.edu        return -EPERM;
183712235Sar4jc@virginia.edu    }
183812235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
183912235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
184012235Sar4jc@virginia.edu    if (n != 0)
184112235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
184212235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
184312235Sar4jc@virginia.edu    if (o != 0)
184412235Sar4jc@virginia.edu    {
184512416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
184612235Sar4jc@virginia.edu        switch (resource) {
184712235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
184812235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
184912235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
185012235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
185112235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
185212235Sar4jc@virginia.edu            break;
185312235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
185412235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
185512235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
185612235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
185712235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
185812593Sjason@lowepower.com            break;
185912235Sar4jc@virginia.edu          default:
186012235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
186112235Sar4jc@virginia.edu            return -EINVAL;
186212235Sar4jc@virginia.edu            break;
186312235Sar4jc@virginia.edu        }
186412235Sar4jc@virginia.edu        rlp.copyOut(tc->getMemProxy());
186512235Sar4jc@virginia.edu    }
186612235Sar4jc@virginia.edu    return 0;
186712235Sar4jc@virginia.edu}
186812235Sar4jc@virginia.edu
186910796Sbrandon.potter@amd.com/// Target clock_gettime() function.
187010796Sbrandon.potter@amd.comtemplate <class OS>
187110796Sbrandon.potter@amd.comSyscallReturn
187211851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
187310796Sbrandon.potter@amd.com{
187410796Sbrandon.potter@amd.com    int index = 1;
187510796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
187610796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
187710796Sbrandon.potter@amd.com
187810796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
187910796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
188010796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
188110796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
188210796Sbrandon.potter@amd.com
188310796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
188410796Sbrandon.potter@amd.com
188510796Sbrandon.potter@amd.com    return 0;
188610796Sbrandon.potter@amd.com}
188710796Sbrandon.potter@amd.com
188811337SMichael.Lebeane@amd.com/// Target clock_getres() function.
188911337SMichael.Lebeane@amd.comtemplate <class OS>
189011337SMichael.Lebeane@amd.comSyscallReturn
189111851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
189211337SMichael.Lebeane@amd.com{
189311337SMichael.Lebeane@amd.com    int index = 1;
189411337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
189511337SMichael.Lebeane@amd.com
189611337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
189711337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
189811337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
189911337SMichael.Lebeane@amd.com
190011337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
190111337SMichael.Lebeane@amd.com
190211337SMichael.Lebeane@amd.com    return 0;
190311337SMichael.Lebeane@amd.com}
190411337SMichael.Lebeane@amd.com
1905378SN/A/// Target gettimeofday() handler.
1906360SN/Atemplate <class OS>
19071450SN/ASyscallReturn
190811851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
190911851Sbrandon.potter@amd.com                 ThreadContext *tc)
1910360SN/A{
19116701Sgblack@eecs.umich.edu    int index = 0;
19126701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1913360SN/A
191410796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1915360SN/A    tp->tv_sec += seconds_since_epoch;
19166109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
19176109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1918360SN/A
19198706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1920360SN/A
19211458SN/A    return 0;
1922360SN/A}
1923360SN/A
1924360SN/A
19251999SN/A/// Target utimes() handler.
19261999SN/Atemplate <class OS>
19271999SN/ASyscallReturn
192811851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
19292680Sktlim@umich.edu           ThreadContext *tc)
19301999SN/A{
19311999SN/A    std::string path;
19321999SN/A
19336701Sgblack@eecs.umich.edu    int index = 0;
19348852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
19356701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
19366701Sgblack@eecs.umich.edu        return -EFAULT;
19376701Sgblack@eecs.umich.edu    }
19381999SN/A
19396701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
19406701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
19418706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
19421999SN/A
19431999SN/A    struct timeval hostTimeval[2];
194411906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
19458737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
19468737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
19471999SN/A    }
19483669Sbinkertn@umich.edu
19493669Sbinkertn@umich.edu    // Adjust path for current working directory
19503669Sbinkertn@umich.edu    path = process->fullPath(path);
19513669Sbinkertn@umich.edu
19521999SN/A    int result = utimes(path.c_str(), hostTimeval);
19531999SN/A
19541999SN/A    if (result < 0)
19551999SN/A        return -errno;
19561999SN/A
19571999SN/A    return 0;
19581999SN/A}
195911886Sbrandon.potter@amd.com
196011886Sbrandon.potter@amd.comtemplate <class OS>
196111886Sbrandon.potter@amd.comSyscallReturn
196211886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
196311886Sbrandon.potter@amd.com{
196411886Sbrandon.potter@amd.com    desc->setFlags(0);
196511886Sbrandon.potter@amd.com
196611886Sbrandon.potter@amd.com    int index = 0;
196711886Sbrandon.potter@amd.com    std::string path;
196811886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
196911886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
197011886Sbrandon.potter@amd.com        return -EFAULT;
197111886Sbrandon.potter@amd.com
197211886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
197311886Sbrandon.potter@amd.com        return -EACCES;
197411886Sbrandon.potter@amd.com
197511886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
197611886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
197711886Sbrandon.potter@amd.com                      Addr mem_loc)
197811886Sbrandon.potter@amd.com    {
197911886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
198011886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
198111886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
198211886Sbrandon.potter@amd.com
198311886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
198411886Sbrandon.potter@amd.com                break;
198511886Sbrandon.potter@amd.com
198611886Sbrandon.potter@amd.com            vect.push_back(std::string());
198711886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
198811886Sbrandon.potter@amd.com        }
198911886Sbrandon.potter@amd.com    };
199011886Sbrandon.potter@amd.com
199111886Sbrandon.potter@amd.com    /**
199211886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
199311886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
199411886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
199511886Sbrandon.potter@amd.com     * constructor.
199611886Sbrandon.potter@amd.com     */
199711886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
199811886Sbrandon.potter@amd.com    pp->executable = path;
199911886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
200011886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
200111886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
200211886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
200311886Sbrandon.potter@amd.com    pp->uid = p->uid();
200411886Sbrandon.potter@amd.com    pp->egid = p->egid();
200511886Sbrandon.potter@amd.com    pp->euid = p->euid();
200611886Sbrandon.potter@amd.com    pp->gid = p->gid();
200711886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
200811886Sbrandon.potter@amd.com    pp->pid = p->pid();
200911886Sbrandon.potter@amd.com    pp->input.assign("cin");
201011886Sbrandon.potter@amd.com    pp->output.assign("cout");
201111886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
201211886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
201311886Sbrandon.potter@amd.com    pp->system = p->system;
201411886Sbrandon.potter@amd.com    /**
201511886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
201611886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
201711886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
201811886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
201911886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
202011886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
202111886Sbrandon.potter@amd.com     */
202211886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
202311886Sbrandon.potter@amd.com    Process *new_p = pp->create();
202411886Sbrandon.potter@amd.com    delete pp;
202511886Sbrandon.potter@amd.com
202611886Sbrandon.potter@amd.com    /**
202711886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
202811886Sbrandon.potter@amd.com     * close-on-exec.
202911886Sbrandon.potter@amd.com     */
203011886Sbrandon.potter@amd.com    new_p->fds = p->fds;
203111886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
203211886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
203311886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
203411886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
203511886Sbrandon.potter@amd.com    }
203611886Sbrandon.potter@amd.com
203711886Sbrandon.potter@amd.com    *new_p->sigchld = true;
203811886Sbrandon.potter@amd.com
203911886Sbrandon.potter@amd.com    delete p;
204011886Sbrandon.potter@amd.com    tc->clearArchRegs();
204111886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
204211886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
204311886Sbrandon.potter@amd.com    new_p->initState();
204411886Sbrandon.potter@amd.com    tc->activate();
204511886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
204611886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
204711886Sbrandon.potter@amd.com
204811886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
204911886Sbrandon.potter@amd.com    return 0;
205011886Sbrandon.potter@amd.com}
205111886Sbrandon.potter@amd.com
2052378SN/A/// Target getrusage() function.
2053360SN/Atemplate <class OS>
20541450SN/ASyscallReturn
205511851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
20562680Sktlim@umich.edu              ThreadContext *tc)
2057360SN/A{
20586701Sgblack@eecs.umich.edu    int index = 0;
20596701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
20606701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
2061360SN/A
20623670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
20633670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
2064360SN/A    rup->ru_stime.tv_sec = 0;
2065360SN/A    rup->ru_stime.tv_usec = 0;
2066360SN/A    rup->ru_maxrss = 0;
2067360SN/A    rup->ru_ixrss = 0;
2068360SN/A    rup->ru_idrss = 0;
2069360SN/A    rup->ru_isrss = 0;
2070360SN/A    rup->ru_minflt = 0;
2071360SN/A    rup->ru_majflt = 0;
2072360SN/A    rup->ru_nswap = 0;
2073360SN/A    rup->ru_inblock = 0;
2074360SN/A    rup->ru_oublock = 0;
2075360SN/A    rup->ru_msgsnd = 0;
2076360SN/A    rup->ru_msgrcv = 0;
2077360SN/A    rup->ru_nsignals = 0;
2078360SN/A    rup->ru_nvcsw = 0;
2079360SN/A    rup->ru_nivcsw = 0;
2080360SN/A
20813670Sbinkertn@umich.edu    switch (who) {
20823670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
208310796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
20848737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
20858737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
20863670Sbinkertn@umich.edu        break;
20873670Sbinkertn@umich.edu
20883670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
20893670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
20903670Sbinkertn@umich.edu        break;
20913670Sbinkertn@umich.edu
20923670Sbinkertn@umich.edu      default:
20933670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
20943670Sbinkertn@umich.edu        // plow ahead
20953670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
20963670Sbinkertn@umich.edu             who);
20973670Sbinkertn@umich.edu    }
20983670Sbinkertn@umich.edu
20998706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
2100360SN/A
21011458SN/A    return 0;
2102360SN/A}
2103360SN/A
21046683Stjones1@inf.ed.ac.uk/// Target times() function.
21056683Stjones1@inf.ed.ac.uktemplate <class OS>
21066683Stjones1@inf.ed.ac.ukSyscallReturn
210711851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
210811851Sbrandon.potter@amd.com          ThreadContext *tc)
21096683Stjones1@inf.ed.ac.uk{
21106701Sgblack@eecs.umich.edu    int index = 0;
21116701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
21126683Stjones1@inf.ed.ac.uk
21136683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
21147823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
21156683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
21166683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
21176683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
21186683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
21196683Stjones1@inf.ed.ac.uk
21206683Stjones1@inf.ed.ac.uk    // Convert to host endianness
21218737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
21226683Stjones1@inf.ed.ac.uk
21236683Stjones1@inf.ed.ac.uk    // Write back
21248706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
21256683Stjones1@inf.ed.ac.uk
21266683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
21276683Stjones1@inf.ed.ac.uk    return clocks;
21286683Stjones1@inf.ed.ac.uk}
21292553SN/A
21306684Stjones1@inf.ed.ac.uk/// Target time() function.
21316684Stjones1@inf.ed.ac.uktemplate <class OS>
21326684Stjones1@inf.ed.ac.ukSyscallReturn
213311851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
21346684Stjones1@inf.ed.ac.uk{
21356684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
213610796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
21376684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
21386684Stjones1@inf.ed.ac.uk
21396701Sgblack@eecs.umich.edu    int index = 0;
21406701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
214111321Ssteve.reinhardt@amd.com    if (taddr != 0) {
21426684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
21438737Skoansin.tan@gmail.com        t = TheISA::htog(t);
21448852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
21458852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
21466684Stjones1@inf.ed.ac.uk    }
21476684Stjones1@inf.ed.ac.uk    return sec;
21486684Stjones1@inf.ed.ac.uk}
21492553SN/A
215011910SBrandon.Potter@amd.comtemplate <class OS>
215111910SBrandon.Potter@amd.comSyscallReturn
215211910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
215311910SBrandon.Potter@amd.com{
215411910SBrandon.Potter@amd.com    int index = 0;
215511910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
215611910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
215711910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
215811910SBrandon.Potter@amd.com
215911910SBrandon.Potter@amd.com    /**
216011910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
216111910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
216211910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
216311910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
216411910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
216511910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
216611910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
216711910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
216811910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
216911910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
217011910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
217111910SBrandon.Potter@amd.com     * due to PID rollover.
217211910SBrandon.Potter@amd.com     */
217311910SBrandon.Potter@amd.com
217411910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
217511910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
217611910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
217711910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
217811910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
217911910SBrandon.Potter@amd.com            tgt_proc = temp;
218011910SBrandon.Potter@amd.com            break;
218111910SBrandon.Potter@amd.com        }
218211910SBrandon.Potter@amd.com    }
218311910SBrandon.Potter@amd.com
218411910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
218511910SBrandon.Potter@amd.com        return -EINVAL;
218611910SBrandon.Potter@amd.com
218711910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
218811910SBrandon.Potter@amd.com        return -ESRCH;
218911910SBrandon.Potter@amd.com
219011910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
219111910SBrandon.Potter@amd.com        return -ESRCH;
219211910SBrandon.Potter@amd.com
219311910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
219411910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
219511910SBrandon.Potter@amd.com
219611910SBrandon.Potter@amd.com    return 0;
219711910SBrandon.Potter@amd.com}
219811910SBrandon.Potter@amd.com
219913568Sbrandon.potter@amd.comtemplate <class OS>
220013568Sbrandon.potter@amd.comSyscallReturn
220113568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
220213568Sbrandon.potter@amd.com{
220313568Sbrandon.potter@amd.com    int index = 0;
220413568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
220513568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
220613568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
220713568Sbrandon.potter@amd.com
220813568Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
220913568Sbrandon.potter@amd.com    if (sim_fd == -1)
221013568Sbrandon.potter@amd.com        return -errno;
221113568Sbrandon.potter@amd.com
221213568Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
221313568Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
221413568Sbrandon.potter@amd.com
221513568Sbrandon.potter@amd.com    return tgt_fd;
221613568Sbrandon.potter@amd.com}
221713568Sbrandon.potter@amd.com
221813568Sbrandon.potter@amd.comtemplate <class OS>
221913568Sbrandon.potter@amd.comSyscallReturn
222013568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
222113568Sbrandon.potter@amd.com{
222213568Sbrandon.potter@amd.com    int index = 0;
222313568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
222413568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
222513568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
222613568Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
222713568Sbrandon.potter@amd.com
222813568Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
222913568Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
223013568Sbrandon.potter@amd.com    if (status == -1)
223113568Sbrandon.potter@amd.com        return -errno;
223213568Sbrandon.potter@amd.com
223313568Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
223413568Sbrandon.potter@amd.com
223513568Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
223613568Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
223713568Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
223813568Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
223913568Sbrandon.potter@amd.com    svBuf.copyOut(tc->getMemProxy());
224013568Sbrandon.potter@amd.com
224113568Sbrandon.potter@amd.com    return status;
224213568Sbrandon.potter@amd.com}
22432553SN/A
224413570Sbrandon.potter@amd.comtemplate <class OS>
224513570Sbrandon.potter@amd.comSyscallReturn
224613570Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
224713570Sbrandon.potter@amd.com{
224813570Sbrandon.potter@amd.com    int retval;
224913570Sbrandon.potter@amd.com
225013570Sbrandon.potter@amd.com    int index = 0;
225113570Sbrandon.potter@amd.com    int nfds_t = p->getSyscallArg(tc, index);
225213570Sbrandon.potter@amd.com    Addr fds_read_ptr = p->getSyscallArg(tc, index);
225313570Sbrandon.potter@amd.com    Addr fds_writ_ptr = p->getSyscallArg(tc, index);
225413570Sbrandon.potter@amd.com    Addr fds_excp_ptr = p->getSyscallArg(tc, index);
225513570Sbrandon.potter@amd.com    Addr time_val_ptr = p->getSyscallArg(tc, index);
225613570Sbrandon.potter@amd.com
225713570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr);
225813570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr);
225913570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr);
226013570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(time_val_ptr);
226113570Sbrandon.potter@amd.com
226213570Sbrandon.potter@amd.com    /**
226313570Sbrandon.potter@amd.com     * Host fields. Notice that these use the definitions from the system
226413570Sbrandon.potter@amd.com     * headers instead of the gem5 headers and libraries. If the host and
226513570Sbrandon.potter@amd.com     * target have different header file definitions, this will not work.
226613570Sbrandon.potter@amd.com     */
226713570Sbrandon.potter@amd.com    fd_set rd_h;
226813570Sbrandon.potter@amd.com    FD_ZERO(&rd_h);
226913570Sbrandon.potter@amd.com    fd_set wr_h;
227013570Sbrandon.potter@amd.com    FD_ZERO(&wr_h);
227113570Sbrandon.potter@amd.com    fd_set ex_h;
227213570Sbrandon.potter@amd.com    FD_ZERO(&ex_h);
227313570Sbrandon.potter@amd.com
227413570Sbrandon.potter@amd.com    /**
227513570Sbrandon.potter@amd.com     * Copy in the fd_set from the target.
227613570Sbrandon.potter@amd.com     */
227713570Sbrandon.potter@amd.com    if (fds_read_ptr)
227813570Sbrandon.potter@amd.com        rd_t.copyIn(tc->getMemProxy());
227913570Sbrandon.potter@amd.com    if (fds_writ_ptr)
228013570Sbrandon.potter@amd.com        wr_t.copyIn(tc->getMemProxy());
228113570Sbrandon.potter@amd.com    if (fds_excp_ptr)
228213570Sbrandon.potter@amd.com        ex_t.copyIn(tc->getMemProxy());
228313570Sbrandon.potter@amd.com
228413570Sbrandon.potter@amd.com    /**
228513570Sbrandon.potter@amd.com     * We need to translate the target file descriptor set into a host file
228613570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
228713570Sbrandon.potter@amd.com     * and the fd_set defined in Linux header files. The nfds field also
228813570Sbrandon.potter@amd.com     * needs to be updated as it will be only target specific after
228913570Sbrandon.potter@amd.com     * retrieving it from the target; the nfds value is expected to be the
229013570Sbrandon.potter@amd.com     * highest file descriptor that needs to be checked, so we need to extend
229113570Sbrandon.potter@amd.com     * it out for nfds_h when we do the update.
229213570Sbrandon.potter@amd.com     */
229313570Sbrandon.potter@amd.com    int nfds_h = 0;
229413570Sbrandon.potter@amd.com    std::map<int, int> trans_map;
229513570Sbrandon.potter@amd.com    auto try_add_host_set = [&](fd_set *tgt_set_entry,
229613570Sbrandon.potter@amd.com                                fd_set *hst_set_entry,
229713570Sbrandon.potter@amd.com                                int iter) -> bool
229813570Sbrandon.potter@amd.com    {
229913570Sbrandon.potter@amd.com        /**
230013570Sbrandon.potter@amd.com         * By this point, we know that we are looking at a valid file
230113570Sbrandon.potter@amd.com         * descriptor set on the target. We need to check if the target file
230213570Sbrandon.potter@amd.com         * descriptor value passed in as iter is part of the set.
230313570Sbrandon.potter@amd.com         */
230413570Sbrandon.potter@amd.com        if (FD_ISSET(iter, tgt_set_entry)) {
230513570Sbrandon.potter@amd.com            /**
230613570Sbrandon.potter@amd.com             * We know that the target file descriptor belongs to the set,
230713570Sbrandon.potter@amd.com             * but we do not yet know if the file descriptor is valid or
230813570Sbrandon.potter@amd.com             * that we have a host mapping. Check that now.
230913570Sbrandon.potter@amd.com             */
231013570Sbrandon.potter@amd.com            auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]);
231113570Sbrandon.potter@amd.com            if (!hbfdp)
231213570Sbrandon.potter@amd.com                return true;
231313570Sbrandon.potter@amd.com            auto sim_fd = hbfdp->getSimFD();
231413570Sbrandon.potter@amd.com
231513570Sbrandon.potter@amd.com            /**
231613570Sbrandon.potter@amd.com             * Add the sim_fd to tgt_fd translation into trans_map for use
231713570Sbrandon.potter@amd.com             * later when we need to zero the target fd_set structures and
231813570Sbrandon.potter@amd.com             * then update them with hits returned from the host select call.
231913570Sbrandon.potter@amd.com             */
232013570Sbrandon.potter@amd.com            trans_map[sim_fd] = iter;
232113570Sbrandon.potter@amd.com
232213570Sbrandon.potter@amd.com            /**
232313570Sbrandon.potter@amd.com             * We know that the host file descriptor exists so now we check
232413570Sbrandon.potter@amd.com             * if we need to update the max count for nfds_h before passing
232513570Sbrandon.potter@amd.com             * the duplicated structure into the host.
232613570Sbrandon.potter@amd.com             */
232713570Sbrandon.potter@amd.com            nfds_h = std::max(nfds_h - 1, sim_fd + 1);
232813570Sbrandon.potter@amd.com
232913570Sbrandon.potter@amd.com            /**
233013570Sbrandon.potter@amd.com             * Add the host file descriptor to the set that we are going to
233113570Sbrandon.potter@amd.com             * pass into the host.
233213570Sbrandon.potter@amd.com             */
233313570Sbrandon.potter@amd.com            FD_SET(sim_fd, hst_set_entry);
233413570Sbrandon.potter@amd.com        }
233513570Sbrandon.potter@amd.com        return false;
233613570Sbrandon.potter@amd.com    };
233713570Sbrandon.potter@amd.com
233813570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_t; i++) {
233913570Sbrandon.potter@amd.com        if (fds_read_ptr) {
234013570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i);
234113570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
234213570Sbrandon.potter@amd.com        }
234313570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
234413570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i);
234513570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
234613570Sbrandon.potter@amd.com        }
234713570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
234813570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i);
234913570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
235013570Sbrandon.potter@amd.com        }
235113570Sbrandon.potter@amd.com    }
235213570Sbrandon.potter@amd.com
235313570Sbrandon.potter@amd.com    if (time_val_ptr) {
235413570Sbrandon.potter@amd.com        /**
235513570Sbrandon.potter@amd.com         * It might be possible to decrement the timeval based on some
235613570Sbrandon.potter@amd.com         * derivation of wall clock determined from elapsed simulator ticks
235713570Sbrandon.potter@amd.com         * but that seems like overkill. Rather, we just set the timeval with
235813570Sbrandon.potter@amd.com         * zero timeout. (There is no reason to block during the simulation
235913570Sbrandon.potter@amd.com         * as it only decreases simulator performance.)
236013570Sbrandon.potter@amd.com         */
236113570Sbrandon.potter@amd.com        tp->tv_sec = 0;
236213570Sbrandon.potter@amd.com        tp->tv_usec = 0;
236313570Sbrandon.potter@amd.com
236413570Sbrandon.potter@amd.com        retval = select(nfds_h,
236513570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
236613570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
236713570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
236813570Sbrandon.potter@amd.com                        (timeval*)&*tp);
236913570Sbrandon.potter@amd.com    } else {
237013570Sbrandon.potter@amd.com        /**
237113570Sbrandon.potter@amd.com         * If the timeval pointer is null, setup a new timeval structure to
237213570Sbrandon.potter@amd.com         * pass into the host select call. Unfortunately, we will need to
237313570Sbrandon.potter@amd.com         * manually check the return value and throw a retry fault if the
237413570Sbrandon.potter@amd.com         * return value is zero. Allowing the system call to block will
237513570Sbrandon.potter@amd.com         * likely deadlock the event queue.
237613570Sbrandon.potter@amd.com         */
237713570Sbrandon.potter@amd.com        struct timeval tv = { 0, 0 };
237813570Sbrandon.potter@amd.com
237913570Sbrandon.potter@amd.com        retval = select(nfds_h,
238013570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
238113570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
238213570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
238313570Sbrandon.potter@amd.com                        &tv);
238413570Sbrandon.potter@amd.com
238513570Sbrandon.potter@amd.com        if (retval == 0) {
238613570Sbrandon.potter@amd.com            /**
238713570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
238813570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try to
238913570Sbrandon.potter@amd.com             * return the signal interrupt instead.
239013570Sbrandon.potter@amd.com             */
239113570Sbrandon.potter@amd.com            for (auto sig : tc->getSystemPtr()->signalList)
239213570Sbrandon.potter@amd.com                if (sig.receiver == p)
239313570Sbrandon.potter@amd.com                    return -EINTR;
239413570Sbrandon.potter@amd.com            return SyscallReturn::retry();
239513570Sbrandon.potter@amd.com        }
239613570Sbrandon.potter@amd.com    }
239713570Sbrandon.potter@amd.com
239813570Sbrandon.potter@amd.com    if (retval == -1)
239913570Sbrandon.potter@amd.com        return -errno;
240013570Sbrandon.potter@amd.com
240113570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*rd_t);
240213570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*wr_t);
240313570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*ex_t);
240413570Sbrandon.potter@amd.com
240513570Sbrandon.potter@amd.com    /**
240613570Sbrandon.potter@amd.com     * We need to translate the host file descriptor set into a target file
240713570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
240813570Sbrandon.potter@amd.com     * and the fd_set defined in header files.
240913570Sbrandon.potter@amd.com     */
241013570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_h; i++) {
241113570Sbrandon.potter@amd.com        if (fds_read_ptr) {
241213570Sbrandon.potter@amd.com            if (FD_ISSET(i, &rd_h))
241313570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*rd_t);
241413570Sbrandon.potter@amd.com        }
241513570Sbrandon.potter@amd.com
241613570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
241713570Sbrandon.potter@amd.com            if (FD_ISSET(i, &wr_h))
241813570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*wr_t);
241913570Sbrandon.potter@amd.com        }
242013570Sbrandon.potter@amd.com
242113570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
242213570Sbrandon.potter@amd.com            if (FD_ISSET(i, &ex_h))
242313570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*ex_t);
242413570Sbrandon.potter@amd.com        }
242513570Sbrandon.potter@amd.com    }
242613570Sbrandon.potter@amd.com
242713570Sbrandon.potter@amd.com    if (fds_read_ptr)
242813570Sbrandon.potter@amd.com        rd_t.copyOut(tc->getMemProxy());
242913570Sbrandon.potter@amd.com    if (fds_writ_ptr)
243013570Sbrandon.potter@amd.com        wr_t.copyOut(tc->getMemProxy());
243113570Sbrandon.potter@amd.com    if (fds_excp_ptr)
243213570Sbrandon.potter@amd.com        ex_t.copyOut(tc->getMemProxy());
243313570Sbrandon.potter@amd.com    if (time_val_ptr)
243413570Sbrandon.potter@amd.com        tp.copyOut(tc->getMemProxy());
243513570Sbrandon.potter@amd.com
243613570Sbrandon.potter@amd.com    return retval;
243713570Sbrandon.potter@amd.com}
243813570Sbrandon.potter@amd.com
243913570Sbrandon.potter@amd.comtemplate <class OS>
244013570Sbrandon.potter@amd.comSyscallReturn
244113570Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
244213570Sbrandon.potter@amd.com{
244313570Sbrandon.potter@amd.com    int index = 0;
244413570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
244513570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
244613570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
244713570Sbrandon.potter@amd.com
244813570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
244913570Sbrandon.potter@amd.com    if (!hbfdp)
245013570Sbrandon.potter@amd.com        return -EBADF;
245113570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
245213570Sbrandon.potter@amd.com
245313570Sbrandon.potter@amd.com    struct pollfd pfd;
245413570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
245513570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
245613570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
245713570Sbrandon.potter@amd.com        && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK))
245813570Sbrandon.potter@amd.com        return SyscallReturn::retry();
245913570Sbrandon.potter@amd.com
246013570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
246113570Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes);
246213570Sbrandon.potter@amd.com
246313570Sbrandon.potter@amd.com    if (bytes_read > 0)
246413570Sbrandon.potter@amd.com        buf_arg.copyOut(tc->getMemProxy());
246513570Sbrandon.potter@amd.com
246613570Sbrandon.potter@amd.com    return (bytes_read == -1) ? -errno : bytes_read;
246713570Sbrandon.potter@amd.com}
246813570Sbrandon.potter@amd.com
246913570Sbrandon.potter@amd.comtemplate <class OS>
247013570Sbrandon.potter@amd.comSyscallReturn
247113570Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
247213570Sbrandon.potter@amd.com{
247313570Sbrandon.potter@amd.com    int index = 0;
247413570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
247513570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
247613570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
247713570Sbrandon.potter@amd.com
247813570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
247913570Sbrandon.potter@amd.com    if (!hbfdp)
248013570Sbrandon.potter@amd.com        return -EBADF;
248113570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
248213570Sbrandon.potter@amd.com
248313570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
248413570Sbrandon.potter@amd.com    buf_arg.copyIn(tc->getMemProxy());
248513570Sbrandon.potter@amd.com
248613570Sbrandon.potter@amd.com    struct pollfd pfd;
248713570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
248813570Sbrandon.potter@amd.com    pfd.events = POLLOUT;
248913570Sbrandon.potter@amd.com
249013570Sbrandon.potter@amd.com    /**
249113570Sbrandon.potter@amd.com     * We don't want to poll on /dev/random. The kernel will not enable the
249213570Sbrandon.potter@amd.com     * file descriptor for writing unless the entropy in the system falls
249313570Sbrandon.potter@amd.com     * below write_wakeup_threshold. This is not guaranteed to happen
249413570Sbrandon.potter@amd.com     * depending on host settings.
249513570Sbrandon.potter@amd.com     */
249613570Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp);
249713570Sbrandon.potter@amd.com    if (ffdp && (ffdp->getFileName() != "/dev/random")) {
249813570Sbrandon.potter@amd.com        if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK))
249913570Sbrandon.potter@amd.com            return SyscallReturn::retry();
250013570Sbrandon.potter@amd.com    }
250113570Sbrandon.potter@amd.com
250213570Sbrandon.potter@amd.com    int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes);
250313570Sbrandon.potter@amd.com
250413570Sbrandon.potter@amd.com    if (bytes_written != -1)
250513570Sbrandon.potter@amd.com        fsync(sim_fd);
250613570Sbrandon.potter@amd.com
250713570Sbrandon.potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
250813570Sbrandon.potter@amd.com}
250913570Sbrandon.potter@amd.com
251013570Sbrandon.potter@amd.comtemplate <class OS>
251113570Sbrandon.potter@amd.comSyscallReturn
251213570Sbrandon.potter@amd.comwait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
251313570Sbrandon.potter@amd.com{
251413570Sbrandon.potter@amd.com    int index = 0;
251513570Sbrandon.potter@amd.com    pid_t pid = p->getSyscallArg(tc, index);
251613570Sbrandon.potter@amd.com    Addr statPtr = p->getSyscallArg(tc, index);
251713570Sbrandon.potter@amd.com    int options = p->getSyscallArg(tc, index);
251813570Sbrandon.potter@amd.com    Addr rusagePtr = p->getSyscallArg(tc, index);
251913570Sbrandon.potter@amd.com
252013570Sbrandon.potter@amd.com    if (rusagePtr)
252113570Sbrandon.potter@amd.com        DPRINTFR(SyscallVerbose,
252213570Sbrandon.potter@amd.com                 "%d: %s: syscall wait4: rusage pointer provided however "
252313570Sbrandon.potter@amd.com                 "functionality not supported. Ignoring rusage pointer.\n",
252413570Sbrandon.potter@amd.com                 curTick(), tc->getCpuPtr()->name());
252513570Sbrandon.potter@amd.com
252613570Sbrandon.potter@amd.com    /**
252713570Sbrandon.potter@amd.com     * Currently, wait4 is only implemented so that it will wait for children
252813570Sbrandon.potter@amd.com     * exit conditions which are denoted by a SIGCHLD signals posted into the
252913570Sbrandon.potter@amd.com     * system signal list. We return no additional information via any of the
253013570Sbrandon.potter@amd.com     * parameters supplied to wait4. If nothing is found in the system signal
253113570Sbrandon.potter@amd.com     * list, we will wait indefinitely for SIGCHLD to post by retrying the
253213570Sbrandon.potter@amd.com     * call.
253313570Sbrandon.potter@amd.com     */
253413570Sbrandon.potter@amd.com    System *sysh = tc->getSystemPtr();
253513570Sbrandon.potter@amd.com    std::list<BasicSignal>::iterator iter;
253613570Sbrandon.potter@amd.com    for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) {
253713570Sbrandon.potter@amd.com        if (iter->receiver == p) {
253813570Sbrandon.potter@amd.com            if (pid < -1) {
253913570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == -pid)
254013570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
254113570Sbrandon.potter@amd.com                    goto success;
254213570Sbrandon.potter@amd.com            } else if (pid == -1) {
254313570Sbrandon.potter@amd.com                if (iter->signalValue == OS::TGT_SIGCHLD)
254413570Sbrandon.potter@amd.com                    goto success;
254513570Sbrandon.potter@amd.com            } else if (pid == 0) {
254613570Sbrandon.potter@amd.com                if ((iter->sender->pgid() == p->pgid())
254713570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
254813570Sbrandon.potter@amd.com                    goto success;
254913570Sbrandon.potter@amd.com            } else {
255013570Sbrandon.potter@amd.com                if ((iter->sender->pid() == pid)
255113570Sbrandon.potter@amd.com                    && (iter->signalValue == OS::TGT_SIGCHLD))
255213570Sbrandon.potter@amd.com                    goto success;
255313570Sbrandon.potter@amd.com            }
255413570Sbrandon.potter@amd.com        }
255513570Sbrandon.potter@amd.com    }
255613570Sbrandon.potter@amd.com
255713570Sbrandon.potter@amd.com    return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry();
255813570Sbrandon.potter@amd.com
255913570Sbrandon.potter@amd.comsuccess:
256013570Sbrandon.potter@amd.com    // Set status to EXITED for WIFEXITED evaluations.
256113570Sbrandon.potter@amd.com    const int EXITED = 0;
256213570Sbrandon.potter@amd.com    BufferArg statusBuf(statPtr, sizeof(int));
256313570Sbrandon.potter@amd.com    *(int *)statusBuf.bufferPtr() = EXITED;
256413570Sbrandon.potter@amd.com    statusBuf.copyOut(tc->getMemProxy());
256513570Sbrandon.potter@amd.com
256613570Sbrandon.potter@amd.com    // Return the child PID.
256713570Sbrandon.potter@amd.com    pid_t retval = iter->sender->pid();
256813570Sbrandon.potter@amd.com    sysh->signalList.erase(iter);
256913570Sbrandon.potter@amd.com    return retval;
257013570Sbrandon.potter@amd.com}
257113570Sbrandon.potter@amd.com
257213570Sbrandon.potter@amd.comtemplate <class OS>
257313570Sbrandon.potter@amd.comSyscallReturn
257413570Sbrandon.potter@amd.comacceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
257513570Sbrandon.potter@amd.com{
257613570Sbrandon.potter@amd.com    struct sockaddr sa;
257713570Sbrandon.potter@amd.com    socklen_t addrLen;
257813570Sbrandon.potter@amd.com    int host_fd;
257913570Sbrandon.potter@amd.com    int index = 0;
258013570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
258113570Sbrandon.potter@amd.com    Addr addrPtr = p->getSyscallArg(tc, index);
258213570Sbrandon.potter@amd.com    Addr lenPtr = p->getSyscallArg(tc, index);
258313570Sbrandon.potter@amd.com
258413570Sbrandon.potter@amd.com    BufferArg *lenBufPtr = nullptr;
258513570Sbrandon.potter@amd.com    BufferArg *addrBufPtr = nullptr;
258613570Sbrandon.potter@amd.com
258713570Sbrandon.potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
258813570Sbrandon.potter@amd.com    if (!sfdp)
258913570Sbrandon.potter@amd.com        return -EBADF;
259013570Sbrandon.potter@amd.com    int sim_fd = sfdp->getSimFD();
259113570Sbrandon.potter@amd.com
259213570Sbrandon.potter@amd.com    /**
259313570Sbrandon.potter@amd.com     * We poll the socket file descriptor first to guarantee that we do not
259413570Sbrandon.potter@amd.com     * block on our accept call. The socket can be opened without the
259513570Sbrandon.potter@amd.com     * non-blocking flag (it blocks). This will cause deadlocks between
259613570Sbrandon.potter@amd.com     * communicating processes.
259713570Sbrandon.potter@amd.com     */
259813570Sbrandon.potter@amd.com    struct pollfd pfd;
259913570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
260013570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
260113570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
260213570Sbrandon.potter@amd.com        && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK))
260313570Sbrandon.potter@amd.com        return SyscallReturn::retry();
260413570Sbrandon.potter@amd.com
260513570Sbrandon.potter@amd.com    if (lenPtr) {
260613570Sbrandon.potter@amd.com        lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t));
260713570Sbrandon.potter@amd.com        lenBufPtr->copyIn(tc->getMemProxy());
260813570Sbrandon.potter@amd.com        memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(),
260913570Sbrandon.potter@amd.com               sizeof(socklen_t));
261013570Sbrandon.potter@amd.com    }
261113570Sbrandon.potter@amd.com
261213570Sbrandon.potter@amd.com    if (addrPtr) {
261313570Sbrandon.potter@amd.com        addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr));
261413570Sbrandon.potter@amd.com        addrBufPtr->copyIn(tc->getMemProxy());
261513570Sbrandon.potter@amd.com        memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(),
261613570Sbrandon.potter@amd.com               sizeof(struct sockaddr));
261713570Sbrandon.potter@amd.com    }
261813570Sbrandon.potter@amd.com
261913570Sbrandon.potter@amd.com    host_fd = accept(sim_fd, &sa, &addrLen);
262013570Sbrandon.potter@amd.com
262113570Sbrandon.potter@amd.com    if (host_fd == -1)
262213570Sbrandon.potter@amd.com        return -errno;
262313570Sbrandon.potter@amd.com
262413570Sbrandon.potter@amd.com    if (addrPtr) {
262513570Sbrandon.potter@amd.com        memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa));
262613570Sbrandon.potter@amd.com        addrBufPtr->copyOut(tc->getMemProxy());
262713570Sbrandon.potter@amd.com        delete(addrBufPtr);
262813570Sbrandon.potter@amd.com    }
262913570Sbrandon.potter@amd.com
263013570Sbrandon.potter@amd.com    if (lenPtr) {
263113570Sbrandon.potter@amd.com        *(socklen_t *)lenBufPtr->bufferPtr() = addrLen;
263213570Sbrandon.potter@amd.com        lenBufPtr->copyOut(tc->getMemProxy());
263313570Sbrandon.potter@amd.com        delete(lenBufPtr);
263413570Sbrandon.potter@amd.com    }
263513570Sbrandon.potter@amd.com
263613570Sbrandon.potter@amd.com    auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain,
263713570Sbrandon.potter@amd.com                                                sfdp->_type, sfdp->_protocol);
263813570Sbrandon.potter@amd.com    return p->fds->allocFD(afdp);
263913570Sbrandon.potter@amd.com}
264013570Sbrandon.potter@amd.com
26411354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2642