syscall_emul.hh revision 13902
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 <net/if.h>
8211383Sbrandon.potter@amd.com#include <poll.h>
8313568Sbrandon.potter@amd.com#include <sys/ioctl.h>
843113Sgblack@eecs.umich.edu#include <sys/mman.h>
8511799Sbrandon.potter@amd.com#include <sys/socket.h>
8611759Sbrandon.potter@amd.com#include <sys/stat.h>
8711812Sbaz21@cam.ac.uk
8811812Sbaz21@cam.ac.uk#if (NO_STATFS == 0)
8911799Sbrandon.potter@amd.com#include <sys/statfs.h>
908229Snate@binkert.org
9113570Sbrandon.potter@amd.com#else
928229Snate@binkert.org#include <sys/mount.h>
9311594Santhony.gutierrez@amd.com
947075Snate@binkert.org#endif
958229Snate@binkert.org#include <sys/time.h>
9611856Sbrandon.potter@amd.com#include <sys/types.h>
977075Snate@binkert.org#include <sys/uio.h>
98360SN/A#include <unistd.h>
9912461Sgabeblack@google.com
10011886Sbrandon.potter@amd.com#include <cerrno>
10111800Sbrandon.potter@amd.com#include <memory>
10211392Sbrandon.potter@amd.com#include <string>
10312334Sgabeblack@google.com
1041354SN/A#include "arch/generic/tlb.hh"
1056216Snate@binkert.org#include "arch/utility.hh"
1066658Snate@binkert.org#include "base/intmath.hh"
1072474SN/A#include "base/loader/object_file.hh"
1082680Sktlim@umich.edu#include "base/logging.hh"
1098229Snate@binkert.org#include "base/trace.hh"
11011886Sbrandon.potter@amd.com#include "base/types.hh"
11110496Ssteve.reinhardt@amd.com#include "config/the_isa.hh"
11211911SBrandon.Potter@amd.com#include "cpu/base.hh"
1138229Snate@binkert.org#include "cpu/thread_context.hh"
11411794Sbrandon.potter@amd.com#include "mem/page_table.hh"
11511886Sbrandon.potter@amd.com#include "params/Process.hh"
11610497Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
11711794Sbrandon.potter@amd.com#include "sim/futex_map.hh"
118360SN/A#include "sim/process.hh"
11913629SAndrea.Mondelli@ucf.edu#include "sim/syscall_debug_macros.hh"
12013629SAndrea.Mondelli@ucf.edu#include "sim/syscall_desc.hh"
12113629SAndrea.Mondelli@ucf.edu#include "sim/syscall_emul_buf.hh"
12213629SAndrea.Mondelli@ucf.edu#include "sim/syscall_return.hh"
123360SN/A
124360SN/A#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN)
125360SN/A#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1))
126360SN/A#endif
127360SN/A
128360SN/A//////////////////////////////////////////////////////////////////////
129360SN/A//
130360SN/A// The following emulation functions are generic enough that they
131360SN/A// don't need to be recompiled for different emulated OS's.  They are
132378SN/A// defined in sim/syscall_emul.cc.
1331706SN/A//
13411851Sbrandon.potter@amd.com//////////////////////////////////////////////////////////////////////
135378SN/A
136378SN/A
137378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
138378SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
139378SN/A                                Process *p, ThreadContext *tc);
1401706SN/A
14111851Sbrandon.potter@amd.com/// Handler for unimplemented syscalls that we never intend to
142360SN/A/// implement (signal handling, etc.) and should not affect the correct
14311760Sbrandon.potter@amd.com/// behavior of the program.  Print a warning only if the appropriate
14411760Sbrandon.potter@amd.com/// trace flag is enabled.  Return success to the target program.
14511851Sbrandon.potter@amd.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num,
14611760Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1476109Ssanchezd@stanford.edu
1481706SN/A// Target fallocateFunc() handler.
14911851Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
150378SN/A                            Process *p, ThreadContext *tc);
1516109Ssanchezd@stanford.edu
1526109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
15311851Sbrandon.potter@amd.comSyscallReturn exitFunc(SyscallDesc *desc, int num,
1546109Ssanchezd@stanford.edu                       Process *p, ThreadContext *tc);
15511886Sbrandon.potter@amd.com
15611886Sbrandon.potter@amd.com/// Target exit_group() handler: terminate simulation. (exit all threads)
15711886Sbrandon.potter@amd.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
15811886Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
159378SN/A
1601706SN/A/// Target set_tid_address() handler.
16111851Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
162378SN/A                                Process *p, ThreadContext *tc);
1635748SSteve.Reinhardt@amd.com
1645748SSteve.Reinhardt@amd.com/// Target getpagesize() handler.
16511851Sbrandon.potter@amd.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
166378SN/A                              Process *p, ThreadContext *tc);
167378SN/A
1681706SN/A/// Target brk() handler: set brk address.
16911851Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
170378SN/A                      Process *p, ThreadContext *tc);
171378SN/A
1721706SN/A/// Target close() handler.
17311851Sbrandon.potter@amd.comSyscallReturn closeFunc(SyscallDesc *desc, int num,
174378SN/A                        Process *p, ThreadContext *tc);
1754118Sgblack@eecs.umich.edu
1764118Sgblack@eecs.umich.edu/// Target lseek() handler.
17711851Sbrandon.potter@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num,
1784118Sgblack@eecs.umich.edu                        Process *p, ThreadContext *tc);
179378SN/A
1801706SN/A/// Target _llseek() handler.
18111851Sbrandon.potter@amd.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
182378SN/A                          Process *p, ThreadContext *tc);
18313568Sbrandon.potter@amd.com
18413568Sbrandon.potter@amd.com/// Target munmap() handler.
18513568Sbrandon.potter@amd.comSyscallReturn munmapFunc(SyscallDesc *desc, int num,
18613568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
187378SN/A
1881706SN/A/// Target shutdown() handler.
18911851Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num,
190360SN/A                           Process *p, ThreadContext *tc);
1915513SMichael.Adler@intel.com
1925513SMichael.Adler@intel.com/// Target gethostname() handler.
19311851Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
1945513SMichael.Adler@intel.com                              Process *p, ThreadContext *tc);
19510203SAli.Saidi@ARM.com
19610203SAli.Saidi@ARM.com/// Target getcwd() handler.
19711851Sbrandon.potter@amd.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
19810203SAli.Saidi@ARM.com                         Process *p, ThreadContext *tc);
1995513SMichael.Adler@intel.com
20011851Sbrandon.potter@amd.com/// Target readlink() handler.
2015513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
202511SN/A                           Process *p, ThreadContext *tc,
20310633Smichaelupton@gmail.com                           int index = 0);
20411851Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
20510633Smichaelupton@gmail.com                           Process *p, ThreadContext *tc);
2061706SN/A
20711851Sbrandon.potter@amd.com/// Target unlink() handler.
208511SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20912795Smattdsinclair@gmail.com                           Process *p, ThreadContext *tc,
21012795Smattdsinclair@gmail.com                           int index);
21112795Smattdsinclair@gmail.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num,
21212795Smattdsinclair@gmail.com                         Process *p, ThreadContext *tc);
21312796Smattdsinclair@gmail.com
21412796Smattdsinclair@gmail.com/// Target link() handler
21512796Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
21612796Smattdsinclair@gmail.com                       ThreadContext *tc);
2175513SMichael.Adler@intel.com
2185513SMichael.Adler@intel.com/// Target symlink() handler.
21911851Sbrandon.potter@amd.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
2205513SMichael.Adler@intel.com                          ThreadContext *tc);
22113031Sbrandon.potter@amd.com
22213031Sbrandon.potter@amd.com/// Target mkdir() handler.
22313031Sbrandon.potter@amd.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
22413031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22513031Sbrandon.potter@amd.com
22613031Sbrandon.potter@amd.com/// Target mknod() handler.
22713031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
22813031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22913031Sbrandon.potter@amd.com
23013031Sbrandon.potter@amd.com/// Target chdir() handler.
23113031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
23213031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
233511SN/A
2341706SN/A// Target rmdir() handler.
23511851Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
2361706SN/A                        Process *p, ThreadContext *tc);
2371706SN/A
2381706SN/A/// Target rename() handler.
2391706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
24011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2411706SN/A
2421706SN/A
2431706SN/A/// Target truncate() handler.
2441706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
24511851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2461706SN/A
247511SN/A
2486703Svince@csl.cornell.edu/// Target ftruncate() handler.
2496703Svince@csl.cornell.eduSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
25011851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2516703Svince@csl.cornell.edu
2526685Stjones1@inf.ed.ac.uk
2536685Stjones1@inf.ed.ac.uk/// Target truncate64() handler.
25411851Sbrandon.potter@amd.comSyscallReturn truncate64Func(SyscallDesc *desc, int num,
2556685Stjones1@inf.ed.ac.uk                             Process *p, ThreadContext *tc);
2566685Stjones1@inf.ed.ac.uk
2575513SMichael.Adler@intel.com/// Target ftruncate64() handler.
2585513SMichael.Adler@intel.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
25911851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2605513SMichael.Adler@intel.com
26111885Sbrandon.potter@amd.com
26211885Sbrandon.potter@amd.com/// Target umask() handler.
26311885Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
2645513SMichael.Adler@intel.com                        Process *p, ThreadContext *tc);
2651999SN/A
2661999SN/A/// Target gettid() handler.
26711851Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
2681999SN/A                         Process *p, ThreadContext *tc);
26911885Sbrandon.potter@amd.com
27011885Sbrandon.potter@amd.com/// Target chown() handler.
27111885Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num,
2721999SN/A                        Process *p, ThreadContext *tc);
2731999SN/A
2741999SN/A/// Target setpgid() handler.
27511851Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
2761999SN/A                          Process *p, ThreadContext *tc);
2773079Sstever@eecs.umich.edu
2783079Sstever@eecs.umich.edu/// Target fchown() handler.
27911851Sbrandon.potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num,
2803079Sstever@eecs.umich.edu                         Process *p, ThreadContext *tc);
28111908SBrandon.Potter@amd.com
28211908SBrandon.Potter@amd.com/// Target dup() handler.
28311908SBrandon.Potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num,
28411908SBrandon.Potter@amd.com                      Process *process, ThreadContext *tc);
28511875Sbrandon.potter@amd.com
2862093SN/A/// Target dup2() handler.
28711851Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
2882093SN/A                       Process *process, ThreadContext *tc);
2892687Sksewell@umich.edu
2902687Sksewell@umich.edu/// Target fcntl() handler.
29111851Sbrandon.potter@amd.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2922687Sksewell@umich.edu                        Process *process, ThreadContext *tc);
2932238SN/A
2942238SN/A/// Target fcntl64() handler.
29511851Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
2962238SN/A                          Process *process, ThreadContext *tc);
29711908SBrandon.Potter@amd.com
29811908SBrandon.Potter@amd.com/// Target setuid() handler.
29911908SBrandon.Potter@amd.comSyscallReturn setuidFunc(SyscallDesc *desc, int num,
30011908SBrandon.Potter@amd.com                         Process *p, ThreadContext *tc);
30111908SBrandon.Potter@amd.com
30211908SBrandon.Potter@amd.com/// Target pipe() handler.
30311908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
30411908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
3052238SN/A
3062238SN/A/// Internal pipe() handler.
30711851Sbrandon.potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
3082238SN/A                       ThreadContext *tc, bool pseudoPipe);
30913571Sbrandon.potter@amd.com
31013571Sbrandon.potter@amd.com/// Target getpid() handler.
31113571Sbrandon.potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num,
31213571Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
31313568Sbrandon.potter@amd.com
31413568Sbrandon.potter@amd.com// Target getpeername() handler.
31513568Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num,
31613568Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
31713568Sbrandon.potter@amd.com
31813568Sbrandon.potter@amd.com// Target bind() handler.
31913568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num,
32013568Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
32113568Sbrandon.potter@amd.com
32213568Sbrandon.potter@amd.com// Target listen() handler.
32313568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num,
32413568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
32513448Sciro.santilli@arm.com
32613031Sbrandon.potter@amd.com// Target connect() handler.
32713031Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num,
32813031Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
32913448Sciro.santilli@arm.com
33013031Sbrandon.potter@amd.com#if defined(SYS_getdents)
33113539Sjavier.setoain@arm.com// Target getdents() handler.
33213539Sjavier.setoain@arm.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
33313539Sjavier.setoain@arm.com                           Process *p, ThreadContext *tc);
33413539Sjavier.setoain@arm.com#endif
33513539Sjavier.setoain@arm.com
33613539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
33713569Sbrandon.potter@amd.com// Target getdents() handler.
33813569Sbrandon.potter@amd.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
33913569Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
34013569Sbrandon.potter@amd.com#endif
34113569Sbrandon.potter@amd.com
34213569Sbrandon.potter@amd.com// Target sendto() handler.
34313569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num,
34413569Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
34513569Sbrandon.potter@amd.com
34613569Sbrandon.potter@amd.com// Target recvfrom() handler.
34713569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num,
34813569Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
34913569Sbrandon.potter@amd.com
35013569Sbrandon.potter@amd.com// Target recvmsg() handler.
35113569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num,
35213569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
35313031Sbrandon.potter@amd.com
3542238SN/A// Target sendmsg() handler.
35511851Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num,
3562238SN/A                          Process *p, ThreadContext *tc);
3572238SN/A
3582238SN/A// Target getuid() handler.
35911851Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num,
3602238SN/A                         Process *p, ThreadContext *tc);
3612238SN/A
3622238SN/A/// Target getgid() handler.
36311851Sbrandon.potter@amd.comSyscallReturn getgidFunc(SyscallDesc *desc, int num,
3642238SN/A                         Process *p, ThreadContext *tc);
3652238SN/A
3662238SN/A/// Target getppid() handler.
36711851Sbrandon.potter@amd.comSyscallReturn getppidFunc(SyscallDesc *desc, int num,
3682238SN/A                          Process *p, ThreadContext *tc);
3692238SN/A
3702238SN/A/// Target geteuid() handler.
37111851Sbrandon.potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num,
3722238SN/A                          Process *p, ThreadContext *tc);
3739455Smitch.hayenga+gem5@gmail.com
3749455Smitch.hayenga+gem5@gmail.com/// Target getegid() handler.
37511851Sbrandon.potter@amd.comSyscallReturn getegidFunc(SyscallDesc *desc, int num,
37610203SAli.Saidi@ARM.com                          Process *p, ThreadContext *tc);
37711851Sbrandon.potter@amd.com
37811851Sbrandon.potter@amd.com/// Target access() handler
3799455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
38013571Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
38113571Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
38213571Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
38313571Sbrandon.potter@amd.com                         int index);
38413571Sbrandon.potter@amd.com
38513571Sbrandon.potter@amd.com// Target getsockopt() handler.
38613571Sbrandon.potter@amd.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num,
38713571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
38813571Sbrandon.potter@amd.com
38913571Sbrandon.potter@amd.com// Target setsockopt() handler.
39013571Sbrandon.potter@amd.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num,
39113571Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
3929112Smarc.orr@gmail.com
39311906SBrandon.Potter@amd.com// Target getsockname() handler.
39411906SBrandon.Potter@amd.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num,
3959112Smarc.orr@gmail.com                              Process *p, ThreadContext *tc);
3969112Smarc.orr@gmail.com
39711851Sbrandon.potter@amd.com/// Futex system call
3989112Smarc.orr@gmail.com/// Implemented by Daniel Sanchez
3999112Smarc.orr@gmail.com/// Used by printf's in multi-threaded apps
40011911SBrandon.Potter@amd.comtemplate <class OS>
4019112Smarc.orr@gmail.comSyscallReturn
40211911SBrandon.Potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
40311911SBrandon.Potter@amd.com          ThreadContext *tc)
40411911SBrandon.Potter@amd.com{
40511911SBrandon.Potter@amd.com    using namespace std;
40613642Sqtt2@cornell.edu
40713642Sqtt2@cornell.edu    int index = 0;
40813642Sqtt2@cornell.edu    Addr uaddr = process->getSyscallArg(tc, index);
4099112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index);
41011911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
41111911SBrandon.Potter@amd.com    int timeout M5_VAR_USED = process->getSyscallArg(tc, index);
41211911SBrandon.Potter@amd.com    Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index);
41311911SBrandon.Potter@amd.com    int val3 = process->getSyscallArg(tc, index);
4149238Slluc.alvarez@bsc.es
41513642Sqtt2@cornell.edu    /*
4169112Smarc.orr@gmail.com     * Unsupported option that does not affect the correctness of the
41711911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
4189112Smarc.orr@gmail.com     */
41913642Sqtt2@cornell.edu    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
42011911SBrandon.Potter@amd.com    op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG;
42111911SBrandon.Potter@amd.com
42211911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
42311911SBrandon.Potter@amd.com
4249112Smarc.orr@gmail.com    if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) {
42511911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
42611911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
42711911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
42811911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
42911911SBrandon.Potter@amd.com
43011911SBrandon.Potter@amd.com        /*
4319112Smarc.orr@gmail.com         * The value in memory at uaddr is not equal with the expected val
4329112Smarc.orr@gmail.com         * (a different thread must have changed it before the system call was
43313642Sqtt2@cornell.edu         * invoked). In this case, we need to throw an error.
43413642Sqtt2@cornell.edu         */
43513642Sqtt2@cornell.edu        if (val != mem_val)
43613642Sqtt2@cornell.edu            return -OS::TGT_EWOULDBLOCK;
43713642Sqtt2@cornell.edu
43811911SBrandon.Potter@amd.com        if (OS::TGT_FUTEX_WAIT) {
4399112Smarc.orr@gmail.com            futex_map.suspend(uaddr, process->tgid(), tc);
44011911SBrandon.Potter@amd.com        } else {
44111911SBrandon.Potter@amd.com            futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3);
44213642Sqtt2@cornell.edu        }
44313642Sqtt2@cornell.edu
4449112Smarc.orr@gmail.com        return 0;
4459112Smarc.orr@gmail.com    } else if (OS::TGT_FUTEX_WAKE == op) {
44611911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
44711911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE_BITSET == op) {
4489112Smarc.orr@gmail.com        return futex_map.wakeup_bitset(uaddr, process->tgid(), val3);
4499112Smarc.orr@gmail.com    } else if (OS::TGT_FUTEX_REQUEUE == op ||
4502238SN/A               OS::TGT_FUTEX_CMP_REQUEUE == op) {
4512238SN/A
4522238SN/A        // Ensure futex system call accessed atomically.
4532238SN/A        BufferArg buf(uaddr, sizeof(int));
45411851Sbrandon.potter@amd.com        buf.copyIn(tc->getMemProxy());
4552238SN/A        int mem_val = *(int*)buf.bufferPtr();
4562238SN/A        /*
4572238SN/A         * For CMP_REQUEUE, the whole operation is only started only if
45811851Sbrandon.potter@amd.com         * val3 is still the value of the futex pointed to by uaddr.
4592238SN/A         */
4602238SN/A        if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val)
4612238SN/A            return -OS::TGT_EWOULDBLOCK;
46211851Sbrandon.potter@amd.com        return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2);
4632238SN/A    } else if (OS::TGT_FUTEX_WAKE_OP == op) {
4642238SN/A        /*
4652238SN/A         * The FUTEX_WAKE_OP operation is equivalent to executing the
46611851Sbrandon.potter@amd.com         * following code atomically and totally ordered with respect to
4672238SN/A         * other futex operations on any of the two supplied futex words:
4682238SN/A         *
4691354SN/A         *   int oldval = *(int *) addr2;
4701354SN/A         *   *(int *) addr2 = oldval op oparg;
47110796Sbrandon.potter@amd.com         *   futex(addr1, FUTEX_WAKE, val, 0, 0, 0);
47210796Sbrandon.potter@amd.com         *   if (oldval cmp cmparg)
4731354SN/A         *        futex(addr2, FUTEX_WAKE, val2, 0, 0, 0);
4741354SN/A         *
4751354SN/A         * (op, oparg, cmp, cmparg are encoded in val3)
4761354SN/A         *
4771354SN/A         * +---+---+-----------+-----------+
4781354SN/A         * |op |cmp|   oparg   |  cmparg   |
4791354SN/A         * +---+---+-----------+-----------+
4801354SN/A         *   4   4       12          12    <== # of bits
4811354SN/A         *
4821354SN/A         * reference: http://man7.org/linux/man-pages/man2/futex.2.html
48310796Sbrandon.potter@amd.com         *
4841354SN/A         */
48510796Sbrandon.potter@amd.com        // get value from simulated-space
4861354SN/A        BufferArg buf(uaddr2, sizeof(int));
4871354SN/A        buf.copyIn(tc->getMemProxy());
4881354SN/A        int oldval = *(int*)buf.bufferPtr();
4891354SN/A        int newval = oldval;
49010796Sbrandon.potter@amd.com        // extract op, oparg, cmp, cmparg from val3
49110796Sbrandon.potter@amd.com        int wake_cmparg =  val3 & 0xfff;
49210796Sbrandon.potter@amd.com        int wake_oparg  = (val3 & 0xfff000)   >> 12;
49310796Sbrandon.potter@amd.com        int wake_cmp    = (val3 & 0xf000000)  >> 24;
49410796Sbrandon.potter@amd.com        int wake_op     = (val3 & 0xf0000000) >> 28;
49510796Sbrandon.potter@amd.com        if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1)
49610796Sbrandon.potter@amd.com            wake_oparg = (1 << wake_oparg);
49710796Sbrandon.potter@amd.com        wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT;
49810796Sbrandon.potter@amd.com        // perform operation on the value of the second futex
49910796Sbrandon.potter@amd.com        if (wake_op == OS::TGT_FUTEX_OP_SET)
50010796Sbrandon.potter@amd.com            newval = wake_oparg;
501360SN/A        else if (wake_op == OS::TGT_FUTEX_OP_ADD)
502360SN/A            newval += wake_oparg;
503360SN/A        else if (wake_op == OS::TGT_FUTEX_OP_OR)
504360SN/A            newval |= wake_oparg;
505360SN/A        else if (wake_op == OS::TGT_FUTEX_OP_ANDN)
506360SN/A            newval &= ~wake_oparg;
507360SN/A        else if (wake_op == OS::TGT_FUTEX_OP_XOR)
50811759Sbrandon.potter@amd.com            newval ^= wake_oparg;
5093113Sgblack@eecs.umich.edu        // copy updated value back to simulated-space
5103113Sgblack@eecs.umich.edu        *(int*)buf.bufferPtr() = newval;
5113113Sgblack@eecs.umich.edu        buf.copyOut(tc->getMemProxy());
5123113Sgblack@eecs.umich.edu        // perform the first wake-up
5133113Sgblack@eecs.umich.edu        int woken1 = futex_map.wakeup(uaddr, process->tgid(), val);
5143113Sgblack@eecs.umich.edu        int woken2 = 0;
5153113Sgblack@eecs.umich.edu        // calculate the condition of the second wake-up
5163113Sgblack@eecs.umich.edu        bool is_wake2 = false;
5173113Sgblack@eecs.umich.edu        if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ)
5183113Sgblack@eecs.umich.edu            is_wake2 = oldval == wake_cmparg;
5193113Sgblack@eecs.umich.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE)
5203113Sgblack@eecs.umich.edu            is_wake2 = oldval != wake_cmparg;
5213113Sgblack@eecs.umich.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT)
52212032Sandreas.sandberg@arm.com            is_wake2 = oldval < wake_cmparg;
5233113Sgblack@eecs.umich.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE)
5243113Sgblack@eecs.umich.edu            is_wake2 = oldval <= wake_cmparg;
5254189Sgblack@eecs.umich.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT)
5264189Sgblack@eecs.umich.edu            is_wake2 = oldval > wake_cmparg;
5273113Sgblack@eecs.umich.edu        else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE)
5283113Sgblack@eecs.umich.edu            is_wake2 = oldval >= wake_cmparg;
5293113Sgblack@eecs.umich.edu        // perform the second wake-up
5303113Sgblack@eecs.umich.edu        if (is_wake2)
5318737Skoansin.tan@gmail.com            woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout);
5323113Sgblack@eecs.umich.edu
5338737Skoansin.tan@gmail.com        return woken1 + woken2;
5343277Sgblack@eecs.umich.edu    }
5355515SMichael.Adler@intel.com    warn("futex: op %d not implemented; ignoring.", op);
5365515SMichael.Adler@intel.com    return -ENOSYS;
5375515SMichael.Adler@intel.com}
5385515SMichael.Adler@intel.com
5395515SMichael.Adler@intel.com
5408737Skoansin.tan@gmail.com/// Pseudo Funcs  - These functions use a different return convension,
5413277Sgblack@eecs.umich.edu/// returning a second value in a register other than the normal return register
5428737Skoansin.tan@gmail.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
5433277Sgblack@eecs.umich.edu                             Process *process, ThreadContext *tc);
5448737Skoansin.tan@gmail.com
5453277Sgblack@eecs.umich.edu/// Target getpidPseudo() handler.
5468737Skoansin.tan@gmail.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
5473113Sgblack@eecs.umich.edu                               Process *p, ThreadContext *tc);
5483113Sgblack@eecs.umich.edu
5493113Sgblack@eecs.umich.edu/// Target getuidPseudo() handler.
5503113Sgblack@eecs.umich.eduSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
5518737Skoansin.tan@gmail.com                               Process *p, ThreadContext *tc);
5523113Sgblack@eecs.umich.edu
5538737Skoansin.tan@gmail.com/// Target getgidPseudo() handler.
5543114Sgblack@eecs.umich.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
5558737Skoansin.tan@gmail.com                               Process *p, ThreadContext *tc);
5563114Sgblack@eecs.umich.edu
5578737Skoansin.tan@gmail.com
5583114Sgblack@eecs.umich.edu/// A readable name for 1,000,000, for converting microseconds to seconds.
5598737Skoansin.tan@gmail.comconst int one_million = 1000000;
56011906SBrandon.Potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
5614061Sgblack@eecs.umich.educonst int one_billion = 1000000000;
5624061Sgblack@eecs.umich.edu
5638737Skoansin.tan@gmail.com/// Approximate seconds since the epoch (1/1/1970).  About a billion,
5643113Sgblack@eecs.umich.edu/// by my reckoning.  We want to keep this a constant (not use the
5658737Skoansin.tan@gmail.com/// real-world time) to keep simulations repeatable.
5663113Sgblack@eecs.umich.educonst unsigned seconds_since_epoch = 1000000000;
5673113Sgblack@eecs.umich.edu
5683113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
5693113Sgblack@eecs.umich.edu/// microseconds.
5703113Sgblack@eecs.umich.edutemplate <class T1, class T2>
57112032Sandreas.sandberg@arm.comvoid
5723113Sgblack@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec)
5733113Sgblack@eecs.umich.edu{
5744189Sgblack@eecs.umich.edu    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
5754189Sgblack@eecs.umich.edu    sec = elapsed_usecs / one_million;
5763113Sgblack@eecs.umich.edu    usec = elapsed_usecs % one_million;
5773113Sgblack@eecs.umich.edu}
5783113Sgblack@eecs.umich.edu
5798737Skoansin.tan@gmail.com/// Helper function to convert current elapsed time to seconds and
5803113Sgblack@eecs.umich.edu/// nanoseconds.
5818737Skoansin.tan@gmail.comtemplate <class T1, class T2>
5823113Sgblack@eecs.umich.eduvoid
5838737Skoansin.tan@gmail.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
5843113Sgblack@eecs.umich.edu{
5853113Sgblack@eecs.umich.edu    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
5863113Sgblack@eecs.umich.edu    sec = elapsed_nsecs / one_billion;
5873113Sgblack@eecs.umich.edu    nsec = elapsed_nsecs % one_billion;
5883113Sgblack@eecs.umich.edu}
5893113Sgblack@eecs.umich.edu
5903113Sgblack@eecs.umich.edu//////////////////////////////////////////////////////////////////////
59111906SBrandon.Potter@amd.com//
5923113Sgblack@eecs.umich.edu// The following emulation functions are generic, but need to be
59312032Sandreas.sandberg@arm.com// templated to account for differences in types, constants, etc.
5948852Sandreas.hansson@arm.com//
59511906SBrandon.Potter@amd.com//////////////////////////////////////////////////////////////////////
5963113Sgblack@eecs.umich.edu
5973113Sgblack@eecs.umich.edu    typedef struct statfs hst_statfs;
5983113Sgblack@eecs.umich.edu#if NO_STAT64
5993113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
6003113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
6013113Sgblack@eecs.umich.edu#else
6023113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
6033113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
60412032Sandreas.sandberg@arm.com#endif
6058852Sandreas.hansson@arm.com
60611906SBrandon.Potter@amd.com//// Helper function to convert a host stat buffer to a target stat
6073113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
6083113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
6093113Sgblack@eecs.umich.edu
6106686Stjones1@inf.ed.ac.uktemplate <typename target_stat, typename host_stat>
6113113Sgblack@eecs.umich.eduvoid
6123113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
6133113Sgblack@eecs.umich.edu{
61411759Sbrandon.potter@amd.com    using namespace TheISA;
61512032Sandreas.sandberg@arm.com
61611759Sbrandon.potter@amd.com    if (fakeTTY)
61711759Sbrandon.potter@amd.com        tgt->st_dev = 0xA;
61811759Sbrandon.potter@amd.com    else
61911759Sbrandon.potter@amd.com        tgt->st_dev = host->st_dev;
62011759Sbrandon.potter@amd.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
62111812Sbaz21@cam.ac.uk    tgt->st_ino = host->st_ino;
62211812Sbaz21@cam.ac.uk    tgt->st_ino = TheISA::htog(tgt->st_ino);
62311812Sbaz21@cam.ac.uk    tgt->st_mode = host->st_mode;
62411759Sbrandon.potter@amd.com    if (fakeTTY) {
62511812Sbaz21@cam.ac.uk        // Claim to be a character device
62611759Sbrandon.potter@amd.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
62711759Sbrandon.potter@amd.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
62811759Sbrandon.potter@amd.com    }
62911759Sbrandon.potter@amd.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
63011759Sbrandon.potter@amd.com    tgt->st_nlink = host->st_nlink;
63111759Sbrandon.potter@amd.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
63211759Sbrandon.potter@amd.com    tgt->st_uid = host->st_uid;
63311812Sbaz21@cam.ac.uk    tgt->st_uid = TheISA::htog(tgt->st_uid);
63411812Sbaz21@cam.ac.uk    tgt->st_gid = host->st_gid;
63511812Sbaz21@cam.ac.uk    tgt->st_gid = TheISA::htog(tgt->st_gid);
63611812Sbaz21@cam.ac.uk    if (fakeTTY)
63711812Sbaz21@cam.ac.uk        tgt->st_rdev = 0x880d;
63811812Sbaz21@cam.ac.uk    else
63911812Sbaz21@cam.ac.uk        tgt->st_rdev = host->st_rdev;
64011759Sbrandon.potter@amd.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
64111759Sbrandon.potter@amd.com    tgt->st_size = host->st_size;
64211812Sbaz21@cam.ac.uk    tgt->st_size = TheISA::htog(tgt->st_size);
64311812Sbaz21@cam.ac.uk    tgt->st_atimeX = host->st_atime;
64411759Sbrandon.potter@amd.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
64511812Sbaz21@cam.ac.uk    tgt->st_mtimeX = host->st_mtime;
64611812Sbaz21@cam.ac.uk    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
64711812Sbaz21@cam.ac.uk    tgt->st_ctimeX = host->st_ctime;
64811812Sbaz21@cam.ac.uk    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
64911812Sbaz21@cam.ac.uk    // Force the block size to be 8KB. This helps to ensure buffered io works
65011812Sbaz21@cam.ac.uk    // consistently across different hosts.
65111812Sbaz21@cam.ac.uk    tgt->st_blksize = 0x2000;
65211759Sbrandon.potter@amd.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
65311759Sbrandon.potter@amd.com    tgt->st_blocks = host->st_blocks;
65411759Sbrandon.potter@amd.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
65511759Sbrandon.potter@amd.com}
656378SN/A
657378SN/A// Same for stat64
6589141Smarc.orr@gmail.com
6599141Smarc.orr@gmail.comtemplate <typename target_stat, typename host_stat64>
660360SN/Avoid
6611450SN/AconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
66211856Sbrandon.potter@amd.com{
663360SN/A    using namespace TheISA;
6646701Sgblack@eecs.umich.edu
66511856Sbrandon.potter@amd.com    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
66611856Sbrandon.potter@amd.com#if defined(STAT_HAVE_NSEC)
667360SN/A    tgt->st_atime_nsec = host->st_atime_nsec;
66810930Sbrandon.potter@amd.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
669360SN/A    tgt->st_mtime_nsec = host->st_mtime_nsec;
67011856Sbrandon.potter@amd.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
67111856Sbrandon.potter@amd.com    tgt->st_ctime_nsec = host->st_ctime_nsec;
67210496Ssteve.reinhardt@amd.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
67311856Sbrandon.potter@amd.com#else
67411856Sbrandon.potter@amd.com    tgt->st_atime_nsec = 0;
6751458SN/A    tgt->st_mtime_nsec = 0;
676360SN/A    tgt->st_ctime_nsec = 0;
67711856Sbrandon.potter@amd.com#endif
67811856Sbrandon.potter@amd.com}
67911856Sbrandon.potter@amd.com
68011856Sbrandon.potter@amd.com// Here are a couple of convenience functions
68111856Sbrandon.potter@amd.comtemplate<class OS>
68211856Sbrandon.potter@amd.comvoid
68311856Sbrandon.potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
68411856Sbrandon.potter@amd.com               hst_stat *host, bool fakeTTY = false)
68510496Ssteve.reinhardt@amd.com{
68611856Sbrandon.potter@amd.com    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
68711856Sbrandon.potter@amd.com    tgt_stat_buf tgt(addr);
68811856Sbrandon.potter@amd.com    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
68911856Sbrandon.potter@amd.com    tgt.copyOut(mem);
69011856Sbrandon.potter@amd.com}
69110930Sbrandon.potter@amd.com
6929141Smarc.orr@gmail.comtemplate<class OS>
693360SN/Avoid
694360SN/AcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
695360SN/A                 hst_stat64 *host, bool fakeTTY = false)
69611907SBrandon.Potter@amd.com{
69711907SBrandon.Potter@amd.com    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
69811907SBrandon.Potter@amd.com    tgt_stat_buf tgt(addr);
699360SN/A    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
70011907SBrandon.Potter@amd.com    tgt.copyOut(mem);
70111907SBrandon.Potter@amd.com}
70211907SBrandon.Potter@amd.com
70311907SBrandon.Potter@amd.comtemplate <class OS>
70411907SBrandon.Potter@amd.comvoid
70511907SBrandon.Potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
70611907SBrandon.Potter@amd.com                 hst_statfs *host)
70711907SBrandon.Potter@amd.com{
70811907SBrandon.Potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
70911907SBrandon.Potter@amd.com
71011907SBrandon.Potter@amd.com    tgt->f_type = TheISA::htog(host->f_type);
71111907SBrandon.Potter@amd.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
71211907SBrandon.Potter@amd.com    tgt->f_bsize = TheISA::htog(host->f_iosize);
71311907SBrandon.Potter@amd.com#else
714360SN/A    tgt->f_bsize = TheISA::htog(host->f_bsize);
71511907SBrandon.Potter@amd.com#endif
7161458SN/A    tgt->f_blocks = TheISA::htog(host->f_blocks);
717360SN/A    tgt->f_bfree = TheISA::htog(host->f_bfree);
71811907SBrandon.Potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
71911907SBrandon.Potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
72011907SBrandon.Potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
72111907SBrandon.Potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
72211907SBrandon.Potter@amd.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
72311907SBrandon.Potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namemax);
72411907SBrandon.Potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_bsize);
72511907SBrandon.Potter@amd.com#elif defined(__APPLE__)
72611907SBrandon.Potter@amd.com    tgt->f_namelen = 0;
72711907SBrandon.Potter@amd.com    tgt->f_frsize = 0;
728360SN/A#else
72911907SBrandon.Potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
73011907SBrandon.Potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
73111907SBrandon.Potter@amd.com#endif
732360SN/A#if defined(__linux__)
733360SN/A    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
73411907SBrandon.Potter@amd.com#else
73511907SBrandon.Potter@amd.com    /*
73611907SBrandon.Potter@amd.com     * The fields are different sizes per OS. Don't bother with
73711907SBrandon.Potter@amd.com     * f_spare or f_reserved on non-Linux for now.
738360SN/A     */
73911907SBrandon.Potter@amd.com    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
740360SN/A#endif
741360SN/A
74211907SBrandon.Potter@amd.com    tgt.copyOut(mem);
7433669Sbinkertn@umich.edu}
74411907SBrandon.Potter@amd.com
74511907SBrandon.Potter@amd.com/// Target ioctl() handler.  For the most part, programs call ioctl()
74611907SBrandon.Potter@amd.com/// only to find out if their stdout is a tty, to determine whether to
74711907SBrandon.Potter@amd.com/// do line or block buffering.  We always claim that output fds are
74811907SBrandon.Potter@amd.com/// not TTYs to provide repeatable results.
74911907SBrandon.Potter@amd.comtemplate <class OS>
75011907SBrandon.Potter@amd.comSyscallReturn
75111907SBrandon.Potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
75211907SBrandon.Potter@amd.com{
75311907SBrandon.Potter@amd.com    int index = 0;
75411907SBrandon.Potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
75511907SBrandon.Potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
75611907SBrandon.Potter@amd.com
75711907SBrandon.Potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
75811907SBrandon.Potter@amd.com
75911907SBrandon.Potter@amd.com    if (OS::isTtyReq(req))
76011907SBrandon.Potter@amd.com        return -ENOTTY;
76111907SBrandon.Potter@amd.com
76211907SBrandon.Potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
76313371Sciro.santilli@arm.com    if (dfdp) {
76411907SBrandon.Potter@amd.com        EmulatedDriver *emul_driver = dfdp->getDriver();
7651706SN/A        if (emul_driver)
76611907SBrandon.Potter@amd.com            return emul_driver->ioctl(p, tc, req);
76711907SBrandon.Potter@amd.com    }
76811907SBrandon.Potter@amd.com
76911907SBrandon.Potter@amd.com    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
77011907SBrandon.Potter@amd.com    if (sfdp) {
77111907SBrandon.Potter@amd.com        int status;
77210496Ssteve.reinhardt@amd.com
77310496Ssteve.reinhardt@amd.com        switch (req) {
77411907SBrandon.Potter@amd.com          case SIOCGIFCONF: {
77511907SBrandon.Potter@amd.com            Addr conf_addr = p->getSyscallArg(tc, index);
77611907SBrandon.Potter@amd.com            BufferArg conf_arg(conf_addr, sizeof(ifconf));
77711907SBrandon.Potter@amd.com            conf_arg.copyIn(tc->getMemProxy());
77811907SBrandon.Potter@amd.com
77911907SBrandon.Potter@amd.com            ifconf *conf = (ifconf*)conf_arg.bufferPtr();
78010496Ssteve.reinhardt@amd.com            Addr ifc_buf_addr = (Addr)conf->ifc_buf;
78111907SBrandon.Potter@amd.com            BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len);
78211907SBrandon.Potter@amd.com            ifc_buf_arg.copyIn(tc->getMemProxy());
78311907SBrandon.Potter@amd.com
78411907SBrandon.Potter@amd.com            conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr();
78510496Ssteve.reinhardt@amd.com
78610496Ssteve.reinhardt@amd.com            status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr());
78711907SBrandon.Potter@amd.com            if (status != -1) {
78811907SBrandon.Potter@amd.com                conf->ifc_buf = (char*)ifc_buf_addr;
78911907SBrandon.Potter@amd.com                ifc_buf_arg.copyOut(tc->getMemProxy());
79011907SBrandon.Potter@amd.com                conf_arg.copyOut(tc->getMemProxy());
79111907SBrandon.Potter@amd.com            }
79211907SBrandon.Potter@amd.com
79311907SBrandon.Potter@amd.com            return status;
79411907SBrandon.Potter@amd.com          }
79511907SBrandon.Potter@amd.com          case SIOCGIFFLAGS:
79611907SBrandon.Potter@amd.com#ifdef __linux__
79711907SBrandon.Potter@amd.com          case SIOCGIFINDEX:
79811907SBrandon.Potter@amd.com#endif
79911907SBrandon.Potter@amd.com          case SIOCGIFNETMASK:
80011907SBrandon.Potter@amd.com          case SIOCGIFADDR:
80111907SBrandon.Potter@amd.com#ifdef __linux__
80211907SBrandon.Potter@amd.com          case SIOCGIFHWADDR:
80311907SBrandon.Potter@amd.com#endif
80411907SBrandon.Potter@amd.com          case SIOCGIFMTU: {
80511907SBrandon.Potter@amd.com            Addr req_addr = p->getSyscallArg(tc, index);
80611907SBrandon.Potter@amd.com            BufferArg req_arg(req_addr, sizeof(ifreq));
80711907SBrandon.Potter@amd.com            req_arg.copyIn(tc->getMemProxy());
80811907SBrandon.Potter@amd.com
80911907SBrandon.Potter@amd.com            status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr());
81011907SBrandon.Potter@amd.com            if (status != -1)
81111907SBrandon.Potter@amd.com                req_arg.copyOut(tc->getMemProxy());
812360SN/A            return status;
81311907SBrandon.Potter@amd.com          }
81411907SBrandon.Potter@amd.com        }
81511907SBrandon.Potter@amd.com    }
81611907SBrandon.Potter@amd.com
81711907SBrandon.Potter@amd.com    /**
81811907SBrandon.Potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
81911907SBrandon.Potter@amd.com     * return something better here, but at least we issue the warning.
82011907SBrandon.Potter@amd.com     */
82111907SBrandon.Potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
82211907SBrandon.Potter@amd.com         tgt_fd, req, tc->pcState());
82311907SBrandon.Potter@amd.com    return -ENOTTY;
82411907SBrandon.Potter@amd.com}
82511907SBrandon.Potter@amd.com
826360SN/Atemplate <class OS>
827360SN/ASyscallReturn
82810027SChris.Adeniyi-Jones@arm.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
82910027SChris.Adeniyi-Jones@arm.com         bool isopenat)
83010027SChris.Adeniyi-Jones@arm.com{
83111851Sbrandon.potter@amd.com    int index = 0;
83210027SChris.Adeniyi-Jones@arm.com    int tgt_dirfd = -1;
83310027SChris.Adeniyi-Jones@arm.com
83411907SBrandon.Potter@amd.com    /**
83510027SChris.Adeniyi-Jones@arm.com     * If using the openat variant, read in the target directory file
83610027SChris.Adeniyi-Jones@arm.com     * descriptor from the simulated process.
83710027SChris.Adeniyi-Jones@arm.com     */
83810027SChris.Adeniyi-Jones@arm.com    if (isopenat)
83910027SChris.Adeniyi-Jones@arm.com        tgt_dirfd = p->getSyscallArg(tc, index);
84011851Sbrandon.potter@amd.com
84111851Sbrandon.potter@amd.com    /**
84210027SChris.Adeniyi-Jones@arm.com     * Retrieve the simulated process' memory proxy and then read in the path
84311907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
84410027SChris.Adeniyi-Jones@arm.com     */
84510027SChris.Adeniyi-Jones@arm.com    std::string path;
84610633Smichaelupton@gmail.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
84710633Smichaelupton@gmail.com        return -EFAULT;
84810633Smichaelupton@gmail.com
84911851Sbrandon.potter@amd.com#ifdef __CYGWIN32__
85010633Smichaelupton@gmail.com    int host_flags = O_BINARY;
85110633Smichaelupton@gmail.com#else
85210633Smichaelupton@gmail.com    int host_flags = 0;
85310633Smichaelupton@gmail.com#endif
85410633Smichaelupton@gmail.com    /**
85510633Smichaelupton@gmail.com     * Translate target flags into host flags. Flags exist which are not
85610633Smichaelupton@gmail.com     * ported between architectures which can cause check failures.
85710633Smichaelupton@gmail.com     */
85810633Smichaelupton@gmail.com    int tgt_flags = p->getSyscallArg(tc, index);
85910633Smichaelupton@gmail.com    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
86010203SAli.Saidi@ARM.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
86110203SAli.Saidi@ARM.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
86210203SAli.Saidi@ARM.com            host_flags |= OS::openFlagTable[i].hostFlag;
86311851Sbrandon.potter@amd.com        }
86411851Sbrandon.potter@amd.com    }
86510203SAli.Saidi@ARM.com    if (tgt_flags) {
86610203SAli.Saidi@ARM.com        warn("open%s: cannot decode flags 0x%x",
86710203SAli.Saidi@ARM.com             isopenat ? "at" : "", tgt_flags);
86810203SAli.Saidi@ARM.com    }
86910203SAli.Saidi@ARM.com#ifdef __CYGWIN32__
87010203SAli.Saidi@ARM.com    host_flags |= O_BINARY;
87110203SAli.Saidi@ARM.com#endif
87210203SAli.Saidi@ARM.com
87310203SAli.Saidi@ARM.com    int mode = p->getSyscallArg(tc, index);
87410203SAli.Saidi@ARM.com
87510203SAli.Saidi@ARM.com    /**
87611851Sbrandon.potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
87711851Sbrandon.potter@amd.com     * take the current working directory value which was passed into the
87810203SAli.Saidi@ARM.com     * process class as a Python parameter and append the current path to
87910203SAli.Saidi@ARM.com     * create a full path.
88010203SAli.Saidi@ARM.com     * Otherwise, openat with a valid target directory file descriptor has
88110203SAli.Saidi@ARM.com     * been called. If the path option, which was passed in as a parameter,
88210203SAli.Saidi@ARM.com     * is not absolute, retrieve the directory file descriptor's path and
88310203SAli.Saidi@ARM.com     * prepend it to the path passed in as a parameter.
88410203SAli.Saidi@ARM.com     * In every case, we should have a full path (which is relevant to the
88510203SAli.Saidi@ARM.com     * host) to work with after this block has been passed.
88610850SGiacomo.Gabrielli@arm.com     */
88710850SGiacomo.Gabrielli@arm.com    std::string redir_path = path;
88810850SGiacomo.Gabrielli@arm.com    std::string abs_path = path;
88911851Sbrandon.potter@amd.com    if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) {
89010850SGiacomo.Gabrielli@arm.com        abs_path = p->absolutePath(path, true);
89110850SGiacomo.Gabrielli@arm.com        redir_path = p->checkPathRedirect(path);
89210850SGiacomo.Gabrielli@arm.com    } else if (!startswith(path, "/")) {
89310850SGiacomo.Gabrielli@arm.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
89410850SGiacomo.Gabrielli@arm.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
89510850SGiacomo.Gabrielli@arm.com        if (!ffdp)
89610850SGiacomo.Gabrielli@arm.com            return -EBADF;
89710850SGiacomo.Gabrielli@arm.com        abs_path = ffdp->getFileName() + path;
89810850SGiacomo.Gabrielli@arm.com        redir_path = p->checkPathRedirect(abs_path);
89910850SGiacomo.Gabrielli@arm.com    }
90010850SGiacomo.Gabrielli@arm.com
90110850SGiacomo.Gabrielli@arm.com    /**
90210850SGiacomo.Gabrielli@arm.com     * Since this is an emulated environment, we create pseudo file
90310850SGiacomo.Gabrielli@arm.com     * descriptors for device requests that have been registered with
90410850SGiacomo.Gabrielli@arm.com     * the process class through Python; this allows us to create a file
90510850SGiacomo.Gabrielli@arm.com     * descriptor for subsequent ioctl or mmap calls.
90610850SGiacomo.Gabrielli@arm.com     */
90710850SGiacomo.Gabrielli@arm.com    if (startswith(abs_path, "/dev/")) {
90810850SGiacomo.Gabrielli@arm.com        std::string filename = abs_path.substr(strlen("/dev/"));
90910850SGiacomo.Gabrielli@arm.com        EmulatedDriver *drv = p->findDriver(filename);
91010850SGiacomo.Gabrielli@arm.com        if (drv) {
91110850SGiacomo.Gabrielli@arm.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
91210850SGiacomo.Gabrielli@arm.com                            "driver open with path[%s]\n",
91310850SGiacomo.Gabrielli@arm.com                            isopenat ? "at" : "", abs_path.c_str());
91410850SGiacomo.Gabrielli@arm.com            return drv->open(p, tc, mode, host_flags);
91510850SGiacomo.Gabrielli@arm.com        }
91610850SGiacomo.Gabrielli@arm.com        /**
91710850SGiacomo.Gabrielli@arm.com         * Fall through here for pass through to host devices, such
91810850SGiacomo.Gabrielli@arm.com         * as /dev/zero
91910850SGiacomo.Gabrielli@arm.com         */
92010850SGiacomo.Gabrielli@arm.com    }
92110850SGiacomo.Gabrielli@arm.com
9226640Svince@csl.cornell.edu    /**
9236640Svince@csl.cornell.edu     * We make several attempts resolve a call to open.
9246640Svince@csl.cornell.edu     *
92511851Sbrandon.potter@amd.com     * 1) Resolve any path redirection before hand. This will set the path
92611851Sbrandon.potter@amd.com     * up with variable 'redir_path' which may contain a modified path or
9276640Svince@csl.cornell.edu     * the original path value. This should already be done in prior code.
9286640Svince@csl.cornell.edu     * 2) Try to handle the access using 'special_paths'. Some special_paths
9296701Sgblack@eecs.umich.edu     * and files cannot be called on the host and need to be handled as
9306701Sgblack@eecs.umich.edu     * special cases inside the simulator. These special_paths are handled by
93110793Sbrandon.potter@amd.com     * C++ routines to provide output back to userspace.
9326640Svince@csl.cornell.edu     * 3) If the full path that was created above does not match any of the
93311758Sbrandon.potter@amd.com     * special cases, pass it through to the open call on the __HOST__ to let
93411758Sbrandon.potter@amd.com     * the host open the file on our behalf. Again, the openImpl tries to
93511758Sbrandon.potter@amd.com     * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the
9366640Svince@csl.cornell.edu     * faux-filesystem files). The faux-filesystem is dynamically created
9378706Sandreas.hansson@arm.com     * during simulator configuration using Python functions.
9386640Svince@csl.cornell.edu     * 4) If the host cannot open the file, the open attempt failed in "3)".
9396701Sgblack@eecs.umich.edu     * Return the host's error code back through the system call to the
9406640Svince@csl.cornell.edu     * simulated process. If running a debug trace, also notify the user that
941360SN/A     * the open call failed.
9421999SN/A     *
9431999SN/A     * Any success will set sim_fd to something other than -1 and skip the
9441999SN/A     * next conditions effectively bypassing them.
94511851Sbrandon.potter@amd.com     */
9462680Sktlim@umich.edu    int sim_fd = -1;
9471999SN/A    std::string used_path;
9481999SN/A    std::vector<std::string> special_paths =
9491999SN/A            { "/proc/meminfo/", "/system/", "/sys/", "/platform/",
9506701Sgblack@eecs.umich.edu              "/etc/passwd" };
9518852Sandreas.hansson@arm.com    for (auto entry : special_paths) {
9526701Sgblack@eecs.umich.edu        if (startswith(path, entry)) {
9531999SN/A            sim_fd = OS::openSpecialFile(abs_path, p, tc);
9546701Sgblack@eecs.umich.edu            used_path = abs_path;
9551999SN/A        }
9566701Sgblack@eecs.umich.edu    }
9571999SN/A    if (sim_fd == -1) {
9581999SN/A        sim_fd = open(redir_path.c_str(), host_flags, mode);
9591999SN/A        used_path = redir_path;
9601999SN/A    }
9611999SN/A    if (sim_fd == -1) {
9623669Sbinkertn@umich.edu        int local = -errno;
9633669Sbinkertn@umich.edu        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s "
9643669Sbinkertn@umich.edu                        "(inferred from:%s)\n", isopenat ? "at" : "",
9651999SN/A                        used_path.c_str(), path.c_str());
9661999SN/A        return local;
9671999SN/A    }
9682218SN/A
9691999SN/A    /**
9701999SN/A     * The file was opened successfully and needs to be recorded in the
9711999SN/A     * process' file descriptor array so that it can be retrieved later.
9721999SN/A     * The target file descriptor that is chosen will be the lowest unused
97313570Sbrandon.potter@amd.com     * file descriptor.
97413570Sbrandon.potter@amd.com     * Return the indirect target file descriptor back to the simulated
97513570Sbrandon.potter@amd.com     * process to act as a handle for the opened file.
97613570Sbrandon.potter@amd.com     */
97713570Sbrandon.potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
97813570Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
97913570Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n"
98013570Sbrandon.potter@amd.com                    "(inferred from:%s)\n", isopenat ? "at" : "",
98113570Sbrandon.potter@amd.com                    sim_fd, tgt_fd, used_path.c_str(), path.c_str());
98213570Sbrandon.potter@amd.com    return tgt_fd;
98313570Sbrandon.potter@amd.com}
98413570Sbrandon.potter@amd.com
98513570Sbrandon.potter@amd.com/// Target open() handler.
98613570Sbrandon.potter@amd.comtemplate <class OS>
98713570Sbrandon.potter@amd.comSyscallReturn
98813570Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
98913570Sbrandon.potter@amd.com         ThreadContext *tc)
99013570Sbrandon.potter@amd.com{
99113570Sbrandon.potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
99213570Sbrandon.potter@amd.com}
99313570Sbrandon.potter@amd.com
99413570Sbrandon.potter@amd.com/// Target openat() handler.
99513570Sbrandon.potter@amd.comtemplate <class OS>
99613570Sbrandon.potter@amd.comSyscallReturn
99713570Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
99813570Sbrandon.potter@amd.com           ThreadContext *tc)
99913570Sbrandon.potter@amd.com{
100013570Sbrandon.potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
100113570Sbrandon.potter@amd.com}
100213570Sbrandon.potter@amd.com
100313570Sbrandon.potter@amd.com/// Target unlinkat() handler.
100413570Sbrandon.potter@amd.comtemplate <class OS>
100513570Sbrandon.potter@amd.comSyscallReturn
100613570Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
100713570Sbrandon.potter@amd.com             ThreadContext *tc)
100813570Sbrandon.potter@amd.com{
100913570Sbrandon.potter@amd.com    int index = 0;
101013570Sbrandon.potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
101113570Sbrandon.potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
101213570Sbrandon.potter@amd.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
101313570Sbrandon.potter@amd.com
101413570Sbrandon.potter@amd.com    return unlinkHelper(desc, callnum, process, tc, 1);
101513570Sbrandon.potter@amd.com}
101613570Sbrandon.potter@amd.com
101713570Sbrandon.potter@amd.com/// Target facessat() handler
101813570Sbrandon.potter@amd.comtemplate <class OS>
101913570Sbrandon.potter@amd.comSyscallReturn
102013570Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
102113570Sbrandon.potter@amd.com              ThreadContext *tc)
102213570Sbrandon.potter@amd.com{
102313570Sbrandon.potter@amd.com    int index = 0;
102413570Sbrandon.potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
102513570Sbrandon.potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
102613570Sbrandon.potter@amd.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
102713570Sbrandon.potter@amd.com    return accessFunc(desc, callnum, process, tc, 1);
102813570Sbrandon.potter@amd.com}
102913570Sbrandon.potter@amd.com
103013570Sbrandon.potter@amd.com/// Target readlinkat() handler
103113570Sbrandon.potter@amd.comtemplate <class OS>
103213570Sbrandon.potter@amd.comSyscallReturn
103313570Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
103413570Sbrandon.potter@amd.com               ThreadContext *tc)
103513570Sbrandon.potter@amd.com{
103613570Sbrandon.potter@amd.com    int index = 0;
103713570Sbrandon.potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
103813570Sbrandon.potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
103913570Sbrandon.potter@amd.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
104013570Sbrandon.potter@amd.com    return readlinkFunc(desc, callnum, process, tc, 1);
104113570Sbrandon.potter@amd.com}
104213570Sbrandon.potter@amd.com
104313570Sbrandon.potter@amd.com/// Target renameat() handler.
104413570Sbrandon.potter@amd.comtemplate <class OS>
104513570Sbrandon.potter@amd.comSyscallReturn
104613570Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
10471999SN/A             ThreadContext *tc)
10481999SN/A{
10491999SN/A    int index = 0;
10501999SN/A
105111856Sbrandon.potter@amd.com    int olddirfd = process->getSyscallArg(tc, index);
10521999SN/A    if (olddirfd != OS::TGT_AT_FDCWD)
10536701Sgblack@eecs.umich.edu        warn("renameat: first argument not AT_FDCWD; unlikely to work");
105411856Sbrandon.potter@amd.com
105511856Sbrandon.potter@amd.com    std::string old_name;
105610931Sbrandon.potter@amd.com
105711856Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(old_name,
105811856Sbrandon.potter@amd.com                                         process->getSyscallArg(tc, index)))
10591999SN/A        return -EFAULT;
106011856Sbrandon.potter@amd.com
10611999SN/A    int newdirfd = process->getSyscallArg(tc, index);
106211856Sbrandon.potter@amd.com    if (newdirfd != OS::TGT_AT_FDCWD)
10631999SN/A        warn("renameat: third argument not AT_FDCWD; unlikely to work");
106411856Sbrandon.potter@amd.com
10651999SN/A    std::string new_name;
106611856Sbrandon.potter@amd.com
10671999SN/A    if (!tc->getMemProxy().tryReadString(new_name,
10681999SN/A                                         process->getSyscallArg(tc, index)))
10695877Shsul@eecs.umich.edu        return -EFAULT;
10705877Shsul@eecs.umich.edu
10715877Shsul@eecs.umich.edu    // Adjust path for cwd and redirection
107211851Sbrandon.potter@amd.com    old_name = process->checkPathRedirect(old_name);
10735877Shsul@eecs.umich.edu    new_name = process->checkPathRedirect(new_name);
10746701Sgblack@eecs.umich.edu
10756701Sgblack@eecs.umich.edu    int result = rename(old_name.c_str(), new_name.c_str());
10766701Sgblack@eecs.umich.edu    return (result == -1) ? -errno : result;
10776701Sgblack@eecs.umich.edu}
10786701Sgblack@eecs.umich.edu
107910027SChris.Adeniyi-Jones@arm.com/// Target sysinfo() handler.
108010027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
108110027SChris.Adeniyi-Jones@arm.comSyscallReturn
108210027SChris.Adeniyi-Jones@arm.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
108310027SChris.Adeniyi-Jones@arm.com            ThreadContext *tc)
10845877Shsul@eecs.umich.edu{
108510318Sandreas.hansson@arm.com
108610318Sandreas.hansson@arm.com    int index = 0;
10875877Shsul@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
10885877Shsul@eecs.umich.edu        sysinfo(process->getSyscallArg(tc, index));
10895877Shsul@eecs.umich.edu
10905877Shsul@eecs.umich.edu    sysinfo->uptime = seconds_since_epoch;
109110486Stjablin@gmail.com    sysinfo->totalram = process->system->memSize();
109210486Stjablin@gmail.com    sysinfo->mem_unit = 1;
10935877Shsul@eecs.umich.edu
109411905SBrandon.Potter@amd.com    sysinfo.copyOut(tc->getMemProxy());
109511905SBrandon.Potter@amd.com
109611905SBrandon.Potter@amd.com    return 0;
109711905SBrandon.Potter@amd.com}
109810027SChris.Adeniyi-Jones@arm.com
109912206Srico.amslinger@informatik.uni-augsburg.de/// Target chmod() handler.
110012206Srico.amslinger@informatik.uni-augsburg.detemplate <class OS>
11015877Shsul@eecs.umich.eduSyscallReturn
110211905SBrandon.Potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
110311905SBrandon.Potter@amd.com          ThreadContext *tc)
11045877Shsul@eecs.umich.edu{
11055877Shsul@eecs.umich.edu    std::string path;
110610027SChris.Adeniyi-Jones@arm.com
11075877Shsul@eecs.umich.edu    int index = 0;
11085877Shsul@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
11095877Shsul@eecs.umich.edu                process->getSyscallArg(tc, index))) {
111012206Srico.amslinger@informatik.uni-augsburg.de        return -EFAULT;
111112206Srico.amslinger@informatik.uni-augsburg.de    }
111212206Srico.amslinger@informatik.uni-augsburg.de
111312206Srico.amslinger@informatik.uni-augsburg.de    uint32_t mode = process->getSyscallArg(tc, index);
111412206Srico.amslinger@informatik.uni-augsburg.de    mode_t hostMode = 0;
111512206Srico.amslinger@informatik.uni-augsburg.de
111612206Srico.amslinger@informatik.uni-augsburg.de    // XXX translate mode flags via OS::something???
111712206Srico.amslinger@informatik.uni-augsburg.de    hostMode = mode;
111812206Srico.amslinger@informatik.uni-augsburg.de
111910027SChris.Adeniyi-Jones@arm.com    // Adjust path for cwd and redirection
112010027SChris.Adeniyi-Jones@arm.com    path = process->checkPathRedirect(path);
112110027SChris.Adeniyi-Jones@arm.com
112210027SChris.Adeniyi-Jones@arm.com    // do the chmod
11235877Shsul@eecs.umich.edu    int result = chmod(path.c_str(), hostMode);
112410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
112510027SChris.Adeniyi-Jones@arm.com        return -errno;
112610027SChris.Adeniyi-Jones@arm.com
112710027SChris.Adeniyi-Jones@arm.com    return 0;
112812206Srico.amslinger@informatik.uni-augsburg.de}
112912206Srico.amslinger@informatik.uni-augsburg.de
113012206Srico.amslinger@informatik.uni-augsburg.detemplate <class OS>
113112206Srico.amslinger@informatik.uni-augsburg.deSyscallReturn
113210027SChris.Adeniyi-Jones@arm.compollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
113310027SChris.Adeniyi-Jones@arm.com{
113410027SChris.Adeniyi-Jones@arm.com    int index = 0;
113510027SChris.Adeniyi-Jones@arm.com    Addr fdsPtr = p->getSyscallArg(tc, index);
113610027SChris.Adeniyi-Jones@arm.com    int nfds = p->getSyscallArg(tc, index);
113710027SChris.Adeniyi-Jones@arm.com    int tmout = p->getSyscallArg(tc, index);
11385877Shsul@eecs.umich.edu
11395877Shsul@eecs.umich.edu    BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds);
11405877Shsul@eecs.umich.edu    fdsBuf.copyIn(tc->getMemProxy());
114110027SChris.Adeniyi-Jones@arm.com
114210027SChris.Adeniyi-Jones@arm.com    /**
11438601Ssteve.reinhardt@amd.com     * Record the target file descriptors in a local variable. We need to
114410027SChris.Adeniyi-Jones@arm.com     * replace them with host file descriptors but we need a temporary copy
11455877Shsul@eecs.umich.edu     * for later. Afterwards, replace each target file descriptor in the
11465877Shsul@eecs.umich.edu     * poll_fd array with its host_fd.
11471999SN/A     */
1148378SN/A    int temp_tgt_fds[nfds];
1149360SN/A    for (index = 0; index < nfds; index++) {
11501450SN/A        temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd;
115111851Sbrandon.potter@amd.com        auto tgt_fd = temp_tgt_fds[index];
11522680Sktlim@umich.edu        auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
1153360SN/A        if (!hbfdp)
1154360SN/A            return -EBADF;
1155360SN/A        auto host_fd = hbfdp->getSimFD();
11566701Sgblack@eecs.umich.edu        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd;
11578852Sandreas.hansson@arm.com    }
11586701Sgblack@eecs.umich.edu
11596701Sgblack@eecs.umich.edu    /**
11606701Sgblack@eecs.umich.edu     * We cannot allow an infinite poll to occur or it will inevitably cause
11616701Sgblack@eecs.umich.edu     * a deadlock in the gem5 simulator with clone. We must pass in tmout with
1162360SN/A     * a non-negative value, however it also makes no sense to poll on the
11633669Sbinkertn@umich.edu     * underlying host for any other time than tmout a zero timeout.
11643669Sbinkertn@umich.edu     */
11653669Sbinkertn@umich.edu    int status;
1166360SN/A    if (tmout < 0) {
1167360SN/A        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
1168360SN/A        if (status == 0) {
1169360SN/A            /**
11702218SN/A             * If blocking indefinitely, check the signal list to see if a
1171360SN/A             * signal would break the poll out of the retry cycle and try
11728706Sandreas.hansson@arm.com             * to return the signal interrupt instead.
1173360SN/A             */
11741458SN/A            System *sysh = tc->getSystemPtr();
1175360SN/A            std::list<BasicSignal>::iterator it;
1176360SN/A            for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++)
1177360SN/A                if (it->receiver == p)
11785074Ssaidi@eecs.umich.edu                    return -EINTR;
11795074Ssaidi@eecs.umich.edu            return SyscallReturn::retry();
11805074Ssaidi@eecs.umich.edu        }
118111851Sbrandon.potter@amd.com    } else
11825074Ssaidi@eecs.umich.edu        status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0);
11835074Ssaidi@eecs.umich.edu
11845074Ssaidi@eecs.umich.edu    if (status == -1)
11855074Ssaidi@eecs.umich.edu        return -errno;
11866701Sgblack@eecs.umich.edu
11878852Sandreas.hansson@arm.com    /**
11886701Sgblack@eecs.umich.edu     * Replace each host_fd in the returned poll_fd array with its original
11895074Ssaidi@eecs.umich.edu     * target file descriptor.
11906701Sgblack@eecs.umich.edu     */
11915074Ssaidi@eecs.umich.edu    for (index = 0; index < nfds; index++) {
11925074Ssaidi@eecs.umich.edu        auto tgt_fd = temp_tgt_fds[index];
11935074Ssaidi@eecs.umich.edu        ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd;
11945074Ssaidi@eecs.umich.edu    }
11955208Ssaidi@eecs.umich.edu
11965208Ssaidi@eecs.umich.edu    /**
11975208Ssaidi@eecs.umich.edu     * Copy out the pollfd struct because the host may have updated fields
11985208Ssaidi@eecs.umich.edu     * in the structure.
11995074Ssaidi@eecs.umich.edu     */
12005074Ssaidi@eecs.umich.edu    fdsBuf.copyOut(tc->getMemProxy());
12015208Ssaidi@eecs.umich.edu
12025074Ssaidi@eecs.umich.edu    return status;
12035074Ssaidi@eecs.umich.edu}
12045074Ssaidi@eecs.umich.edu
12055074Ssaidi@eecs.umich.edu/// Target fchmod() handler.
12068706Sandreas.hansson@arm.comtemplate <class OS>
12075074Ssaidi@eecs.umich.eduSyscallReturn
12085074Ssaidi@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
12095074Ssaidi@eecs.umich.edu{
12105074Ssaidi@eecs.umich.edu    int index = 0;
12115074Ssaidi@eecs.umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
121210027SChris.Adeniyi-Jones@arm.com    uint32_t mode = p->getSyscallArg(tc, index);
121310027SChris.Adeniyi-Jones@arm.com
121410027SChris.Adeniyi-Jones@arm.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
121511851Sbrandon.potter@amd.com    if (!ffdp)
121610027SChris.Adeniyi-Jones@arm.com        return -EBADF;
121710027SChris.Adeniyi-Jones@arm.com    int sim_fd = ffdp->getSimFD();
121810027SChris.Adeniyi-Jones@arm.com
121910027SChris.Adeniyi-Jones@arm.com    mode_t hostMode = mode;
122010027SChris.Adeniyi-Jones@arm.com
122110793Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
122210027SChris.Adeniyi-Jones@arm.com
122310027SChris.Adeniyi-Jones@arm.com    return (result < 0) ? -errno : 0;
122410027SChris.Adeniyi-Jones@arm.com}
122510027SChris.Adeniyi-Jones@arm.com
122610027SChris.Adeniyi-Jones@arm.com/// Target mremap() handler.
122710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
122810027SChris.Adeniyi-Jones@arm.comSyscallReturn
122910027SChris.Adeniyi-Jones@arm.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
123010027SChris.Adeniyi-Jones@arm.com{
123110027SChris.Adeniyi-Jones@arm.com    int index = 0;
123210027SChris.Adeniyi-Jones@arm.com    Addr start = process->getSyscallArg(tc, index);
123310027SChris.Adeniyi-Jones@arm.com    uint64_t old_length = process->getSyscallArg(tc, index);
123410027SChris.Adeniyi-Jones@arm.com    uint64_t new_length = process->getSyscallArg(tc, index);
123510027SChris.Adeniyi-Jones@arm.com    uint64_t flags = process->getSyscallArg(tc, index);
123610027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
123710027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
123810027SChris.Adeniyi-Jones@arm.com
123910027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
124010027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
124110027SChris.Adeniyi-Jones@arm.com
124210027SChris.Adeniyi-Jones@arm.com    if ((start % TheISA::PageBytes != 0) ||
124310027SChris.Adeniyi-Jones@arm.com        (provided_address % TheISA::PageBytes != 0)) {
124410027SChris.Adeniyi-Jones@arm.com        warn("mremap failing: arguments not page aligned");
124510027SChris.Adeniyi-Jones@arm.com        return -EINVAL;
124610027SChris.Adeniyi-Jones@arm.com    }
124710027SChris.Adeniyi-Jones@arm.com
124810027SChris.Adeniyi-Jones@arm.com    new_length = roundUp(new_length, TheISA::PageBytes);
12491999SN/A
12501999SN/A    if (new_length > old_length) {
12511999SN/A        std::shared_ptr<MemState> mem_state = process->memState;
125211856Sbrandon.potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
12531999SN/A
12546701Sgblack@eecs.umich.edu        if ((start + old_length) == mmap_end &&
125511856Sbrandon.potter@amd.com            (!use_provided_address || provided_address == start)) {
125611856Sbrandon.potter@amd.com            // This case cannot occur when growing downward, as
125710931Sbrandon.potter@amd.com            // start is greater than or equal to mmap_end.
125811856Sbrandon.potter@amd.com            uint64_t diff = new_length - old_length;
125911856Sbrandon.potter@amd.com            process->allocateMem(mmap_end, diff);
12601999SN/A            mem_state->setMmapEnd(mmap_end + diff);
126111856Sbrandon.potter@amd.com            return start;
12621999SN/A        } else {
12632764Sstever@eecs.umich.edu            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
12642064SN/A                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
126510931Sbrandon.potter@amd.com                return -ENOMEM;
12662064SN/A            } else {
12672064SN/A                uint64_t new_start = provided_address;
126810931Sbrandon.potter@amd.com                if (!use_provided_address) {
12692064SN/A                    new_start = process->mmapGrowsDown() ?
12701999SN/A                                mmap_end - new_length : mmap_end;
12711999SN/A                    mmap_end = process->mmapGrowsDown() ?
12722218SN/A                               new_start : mmap_end + new_length;
12731999SN/A                    mem_state->setMmapEnd(mmap_end);
127410931Sbrandon.potter@amd.com                }
12751999SN/A
12761999SN/A                process->pTable->remap(start, old_length, new_start);
12771999SN/A                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
12781999SN/A                     new_start, new_start + new_length,
12791999SN/A                     new_length - old_length);
1280378SN/A                // add on the remaining unallocated pages
1281360SN/A                process->allocateMem(new_start + old_length,
12821450SN/A                                     new_length - old_length,
128311851Sbrandon.potter@amd.com                                     use_provided_address /* clobber */);
12842680Sktlim@umich.edu                if (use_provided_address &&
1285360SN/A                    ((new_start + new_length > mem_state->getMmapEnd() &&
1286360SN/A                      !process->mmapGrowsDown()) ||
1287360SN/A                    (new_start < mem_state->getMmapEnd() &&
12886701Sgblack@eecs.umich.edu                      process->mmapGrowsDown()))) {
12898852Sandreas.hansson@arm.com                    // something fishy going on here, at least notify the user
12906701Sgblack@eecs.umich.edu                    // @todo: increase mmap_end?
12916701Sgblack@eecs.umich.edu                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
12926701Sgblack@eecs.umich.edu                }
12936701Sgblack@eecs.umich.edu                warn("returning %08p as start\n", new_start);
1294360SN/A                return new_start;
12953669Sbinkertn@umich.edu            }
12963669Sbinkertn@umich.edu        }
12973669Sbinkertn@umich.edu    } else {
1298360SN/A        if (use_provided_address && provided_address != start)
1299360SN/A            process->pTable->remap(start, new_length, provided_address);
1300360SN/A        process->pTable->unmap(start + new_length, old_length - new_length);
1301360SN/A        return use_provided_address ? provided_address : start;
13021458SN/A    }
1303360SN/A}
13048706Sandreas.hansson@arm.com
1305360SN/A/// Target stat() handler.
13061458SN/Atemplate <class OS>
1307360SN/ASyscallReturn
1308360SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process,
13091999SN/A         ThreadContext *tc)
13101999SN/A{
13111999SN/A    std::string path;
131211851Sbrandon.potter@amd.com
13132680Sktlim@umich.edu    int index = 0;
13141999SN/A    if (!tc->getMemProxy().tryReadString(path,
13151999SN/A                process->getSyscallArg(tc, index))) {
13161999SN/A        return -EFAULT;
13176701Sgblack@eecs.umich.edu    }
13188852Sandreas.hansson@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
13196701Sgblack@eecs.umich.edu
13206701Sgblack@eecs.umich.edu    // Adjust path for cwd and redirection
13216701Sgblack@eecs.umich.edu    path = process->checkPathRedirect(path);
13226701Sgblack@eecs.umich.edu
13231999SN/A    struct stat hostBuf;
13243669Sbinkertn@umich.edu    int result = stat(path.c_str(), &hostBuf);
13253669Sbinkertn@umich.edu
13263669Sbinkertn@umich.edu    if (result < 0)
13272764Sstever@eecs.umich.edu        return -errno;
13282064SN/A
13292064SN/A    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
13302064SN/A
13311999SN/A    return 0;
13321999SN/A}
13332064SN/A
13341999SN/A
13351999SN/A/// Target stat64() handler.
13361999SN/Atemplate <class OS>
13371999SN/ASyscallReturn
13388706Sandreas.hansson@arm.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
13391999SN/A           ThreadContext *tc)
13401999SN/A{
13411999SN/A    std::string path;
13421999SN/A
1343378SN/A    int index = 0;
1344360SN/A    if (!tc->getMemProxy().tryReadString(path,
13451450SN/A                process->getSyscallArg(tc, index)))
134611856Sbrandon.potter@amd.com        return -EFAULT;
1347360SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
13486701Sgblack@eecs.umich.edu
134911856Sbrandon.potter@amd.com    // Adjust path for cwd and redirection
135011856Sbrandon.potter@amd.com    path = process->checkPathRedirect(path);
1351360SN/A
135211380Salexandru.dutu@amd.com#if NO_STAT64
1353360SN/A    struct stat  hostBuf;
135411856Sbrandon.potter@amd.com    int result = stat(path.c_str(), &hostBuf);
135511856Sbrandon.potter@amd.com#else
13561458SN/A    struct stat64 hostBuf;
135711856Sbrandon.potter@amd.com    int result = stat64(path.c_str(), &hostBuf);
1358360SN/A#endif
1359360SN/A
136010931Sbrandon.potter@amd.com    if (result < 0)
1361360SN/A        return -errno;
1362360SN/A
13631458SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1364360SN/A
136510931Sbrandon.potter@amd.com    return 0;
13662021SN/A}
13671458SN/A
1368360SN/A
1369360SN/A/// Target fstatat64() handler.
13701706SN/Atemplate <class OS>
13711706SN/ASyscallReturn
13721706SN/Afstatat64Func(SyscallDesc *desc, int callnum, Process *process,
137311851Sbrandon.potter@amd.com              ThreadContext *tc)
13742680Sktlim@umich.edu{
13751706SN/A    int index = 0;
137611799Sbrandon.potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
137711799Sbrandon.potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
137811799Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
13791706SN/A
13801706SN/A    std::string path;
13816701Sgblack@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
13828852Sandreas.hansson@arm.com                process->getSyscallArg(tc, index)))
13836701Sgblack@eecs.umich.edu        return -EFAULT;
13846701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
13856701Sgblack@eecs.umich.edu
13866701Sgblack@eecs.umich.edu    // Adjust path for cwd and redirection
13871706SN/A    path = process->checkPathRedirect(path);
13883669Sbinkertn@umich.edu
13893669Sbinkertn@umich.edu#if NO_STAT64
13903669Sbinkertn@umich.edu    struct stat  hostBuf;
13911706SN/A    int result = stat(path.c_str(), &hostBuf);
13921706SN/A#else
13931706SN/A    struct stat64 hostBuf;
13941706SN/A    int result = stat64(path.c_str(), &hostBuf);
13952218SN/A#endif
13961706SN/A
139711759Sbrandon.potter@amd.com    if (result < 0)
139811799Sbrandon.potter@amd.com        return -errno;
13991706SN/A
14001706SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14011706SN/A
140211886Sbrandon.potter@amd.com    return 0;
140311886Sbrandon.potter@amd.com}
140411886Sbrandon.potter@amd.com
140511886Sbrandon.potter@amd.com
140611886Sbrandon.potter@amd.com/// Target fstat64() handler.
140712426Sqtt2@cornell.edutemplate <class OS>
140813557Sgabeblack@google.comSyscallReturn
140913557Sgabeblack@google.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
141011886Sbrandon.potter@amd.com{
141112426Sqtt2@cornell.edu    int index = 0;
141213534Sandreas.sandberg@arm.com    int tgt_fd = p->getSyscallArg(tc, index);
141312426Sqtt2@cornell.edu    Addr bufPtr = p->getSyscallArg(tc, index);
141413534Sandreas.sandberg@arm.com
141512426Sqtt2@cornell.edu    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
141612426Sqtt2@cornell.edu    if (!ffdp)
141712426Sqtt2@cornell.edu        return -EBADF;
141813536Sandreas.sandberg@arm.com    int sim_fd = ffdp->getSimFD();
141912426Sqtt2@cornell.edu
142012426Sqtt2@cornell.edu#if NO_STAT64
142111886Sbrandon.potter@amd.com    struct stat  hostBuf;
142213536Sandreas.sandberg@arm.com    int result = fstat(sim_fd, &hostBuf);
142312426Sqtt2@cornell.edu#else
142411886Sbrandon.potter@amd.com    struct stat64  hostBuf;
142511886Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
142611886Sbrandon.potter@amd.com#endif
142711886Sbrandon.potter@amd.com
142811886Sbrandon.potter@amd.com    if (result < 0)
142911886Sbrandon.potter@amd.com        return -errno;
143011886Sbrandon.potter@amd.com
143111886Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
143211886Sbrandon.potter@amd.com
143311886Sbrandon.potter@amd.com    return 0;
143413649Sqtt2@cornell.edu}
143513649Sqtt2@cornell.edu
143613649Sqtt2@cornell.edu
143713649Sqtt2@cornell.edu/// Target lstat() handler.
143813649Sqtt2@cornell.edutemplate <class OS>
143911886Sbrandon.potter@amd.comSyscallReturn
144011886Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
144111886Sbrandon.potter@amd.com          ThreadContext *tc)
144211886Sbrandon.potter@amd.com{
144311886Sbrandon.potter@amd.com    std::string path;
144411886Sbrandon.potter@amd.com
144511886Sbrandon.potter@amd.com    int index = 0;
144611886Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
144711886Sbrandon.potter@amd.com                process->getSyscallArg(tc, index))) {
144811886Sbrandon.potter@amd.com        return -EFAULT;
144911886Sbrandon.potter@amd.com    }
145011886Sbrandon.potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
145111886Sbrandon.potter@amd.com
145211886Sbrandon.potter@amd.com    // Adjust path for cwd and redirection
145311886Sbrandon.potter@amd.com    path = process->checkPathRedirect(path);
145411886Sbrandon.potter@amd.com
145511886Sbrandon.potter@amd.com    struct stat hostBuf;
145611886Sbrandon.potter@amd.com    int result = lstat(path.c_str(), &hostBuf);
145711886Sbrandon.potter@amd.com
145811886Sbrandon.potter@amd.com    if (result < 0)
145911886Sbrandon.potter@amd.com        return -errno;
146011886Sbrandon.potter@amd.com
146111886Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
146211886Sbrandon.potter@amd.com
146311886Sbrandon.potter@amd.com    return 0;
146411886Sbrandon.potter@amd.com}
146511886Sbrandon.potter@amd.com
146611886Sbrandon.potter@amd.com/// Target lstat64() handler.
146711886Sbrandon.potter@amd.comtemplate <class OS>
146811886Sbrandon.potter@amd.comSyscallReturn
146911886Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
147011886Sbrandon.potter@amd.com            ThreadContext *tc)
147111886Sbrandon.potter@amd.com{
147211886Sbrandon.potter@amd.com    std::string path;
147311886Sbrandon.potter@amd.com
147411886Sbrandon.potter@amd.com    int index = 0;
147511886Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
147611886Sbrandon.potter@amd.com                process->getSyscallArg(tc, index))) {
147711886Sbrandon.potter@amd.com        return -EFAULT;
147811886Sbrandon.potter@amd.com    }
147911886Sbrandon.potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
148011886Sbrandon.potter@amd.com
148111886Sbrandon.potter@amd.com    // Adjust path for cwd and redirection
148211886Sbrandon.potter@amd.com    path = process->checkPathRedirect(path);
148311886Sbrandon.potter@amd.com
148411886Sbrandon.potter@amd.com#if NO_STAT64
148511886Sbrandon.potter@amd.com    struct stat hostBuf;
148611886Sbrandon.potter@amd.com    int result = lstat(path.c_str(), &hostBuf);
148711886Sbrandon.potter@amd.com#else
148811911SBrandon.Potter@amd.com    struct stat64 hostBuf;
148911911SBrandon.Potter@amd.com    int result = lstat64(path.c_str(), &hostBuf);
149011911SBrandon.Potter@amd.com#endif
149111911SBrandon.Potter@amd.com
149211911SBrandon.Potter@amd.com    if (result < 0)
149311911SBrandon.Potter@amd.com        return -errno;
149411911SBrandon.Potter@amd.com
149511886Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
149611886Sbrandon.potter@amd.com
149711886Sbrandon.potter@amd.com    return 0;
149811886Sbrandon.potter@amd.com}
149911886Sbrandon.potter@amd.com
150011886Sbrandon.potter@amd.com/// Target fstat() handler.
150111886Sbrandon.potter@amd.comtemplate <class OS>
150211886Sbrandon.potter@amd.comSyscallReturn
150311886Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
150411886Sbrandon.potter@amd.com{
150511886Sbrandon.potter@amd.com    int index = 0;
150611886Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
150713536Sandreas.sandberg@arm.com    Addr bufPtr = p->getSyscallArg(tc, index);
150811886Sbrandon.potter@amd.com
150911886Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
151011886Sbrandon.potter@amd.com
151111886Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
151211886Sbrandon.potter@amd.com    if (!ffdp)
151311886Sbrandon.potter@amd.com        return -EBADF;
151411886Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
151511886Sbrandon.potter@amd.com
151611886Sbrandon.potter@amd.com    struct stat hostBuf;
151711886Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
151813535Sandreas.sandberg@arm.com
151913535Sandreas.sandberg@arm.com    if (result < 0)
152013535Sandreas.sandberg@arm.com        return -errno;
152111886Sbrandon.potter@amd.com
152211886Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
152311886Sbrandon.potter@amd.com
152411886Sbrandon.potter@amd.com    return 0;
15251706SN/A}
15261706SN/A
15271706SN/A/// Target statfs() handler.
15281706SN/Atemplate <class OS>
152911856Sbrandon.potter@amd.comSyscallReturn
15301706SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
15316701Sgblack@eecs.umich.edu           ThreadContext *tc)
153211856Sbrandon.potter@amd.com{
153311856Sbrandon.potter@amd.com#if NO_STATFS
15341706SN/A    warn("Host OS cannot support calls to statfs. Ignoring syscall");
153511856Sbrandon.potter@amd.com#else
153611856Sbrandon.potter@amd.com    std::string path;
15371706SN/A
153811856Sbrandon.potter@amd.com    int index = 0;
15391706SN/A    if (!tc->getMemProxy().tryReadString(path,
15401706SN/A                process->getSyscallArg(tc, index))) {
154110931Sbrandon.potter@amd.com        return -EFAULT;
15421706SN/A    }
15431706SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
15442218SN/A
15451706SN/A    // Adjust path for cwd and redirection
154611759Sbrandon.potter@amd.com    path = process->checkPathRedirect(path);
15471706SN/A
15481706SN/A    struct statfs hostBuf;
15491706SN/A    int result = statfs(path.c_str(), &hostBuf);
15501706SN/A
155113572Sbrandon.potter@amd.com    if (result < 0)
155213572Sbrandon.potter@amd.com        return -errno;
155313572Sbrandon.potter@amd.com
155413572Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
155513572Sbrandon.potter@amd.com#endif
155613572Sbrandon.potter@amd.com    return 0;
155713572Sbrandon.potter@amd.com}
155813572Sbrandon.potter@amd.com
155913572Sbrandon.potter@amd.comtemplate <class OS>
156013572Sbrandon.potter@amd.comSyscallReturn
156113572Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
156213572Sbrandon.potter@amd.com{
156313572Sbrandon.potter@amd.com    int index = 0;
156413572Sbrandon.potter@amd.com
156513572Sbrandon.potter@amd.com    RegVal flags = p->getSyscallArg(tc, index);
156613572Sbrandon.potter@amd.com    RegVal newStack = p->getSyscallArg(tc, index);
156713572Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
156813572Sbrandon.potter@amd.com
156913572Sbrandon.potter@amd.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
157013572Sbrandon.potter@amd.com    /**
157113572Sbrandon.potter@amd.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
157213572Sbrandon.potter@amd.com     * The flag defines the list of clone() arguments in the following
157313572Sbrandon.potter@amd.com     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
157413572Sbrandon.potter@amd.com     */
157513572Sbrandon.potter@amd.com    Addr tlsPtr = p->getSyscallArg(tc, index);
157613572Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
157713572Sbrandon.potter@amd.com#else
157813572Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
157913572Sbrandon.potter@amd.com    Addr tlsPtr = p->getSyscallArg(tc, index);
158013572Sbrandon.potter@amd.com#endif
158113572Sbrandon.potter@amd.com
158213572Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
158313572Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
158413572Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
158513572Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
158613572Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
158713572Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
158813572Sbrandon.potter@amd.com        return -EINVAL;
15891706SN/A
15901999SN/A    ThreadContext *ctc;
15911999SN/A    if (!(ctc = p->findFreeContext())) {
15921999SN/A        DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system"
159311856Sbrandon.potter@amd.com                        "[cpu %d, thread %d]", tc->cpuId(), tc->threadId());
15941999SN/A        return -EAGAIN;
15956701Sgblack@eecs.umich.edu    }
159611856Sbrandon.potter@amd.com
159710931Sbrandon.potter@amd.com    /**
159811856Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
159911856Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
16001999SN/A     * fields are manually initialized instead of passing parameters to the
160111856Sbrandon.potter@amd.com     * constructor.
16021999SN/A     */
160311856Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
160411856Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
160511856Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
16061999SN/A    pp->system = p->system;
16076227Snate@binkert.org    pp->cwd.assign(p->tgtCwd);
16081999SN/A    pp->input.assign("stdin");
16092461SN/A    pp->output.assign("stdout");
161011856Sbrandon.potter@amd.com    pp->errout.assign("stderr");
161111856Sbrandon.potter@amd.com    pp->uid = p->uid();
16128737Skoansin.tan@gmail.com    pp->euid = p->euid();
16131999SN/A    pp->gid = p->gid();
161411856Sbrandon.potter@amd.com    pp->egid = p->egid();
161511856Sbrandon.potter@amd.com
16161999SN/A    /* Find the first free PID that's less than the maximum */
16171999SN/A    std::set<int> const& pids = p->system->PIDs;
161810931Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
16191999SN/A    do {
16206227Snate@binkert.org        temp_pid++;
16211999SN/A    } while (pids.find(temp_pid) != pids.end());
16221999SN/A    if (temp_pid >= System::maxPID)
162313572Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
16241999SN/A
16251999SN/A    pp->pid = temp_pid;
162611385Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
1627360SN/A    pp->useArchPT = p->useArchPT;
16281450SN/A    pp->kvmInSE = p->kvmInSE;
162911851Sbrandon.potter@amd.com    Process *cp = pp->create();
163011385Sbrandon.potter@amd.com    delete pp;
1631360SN/A
16326701Sgblack@eecs.umich.edu    Process *owner = ctc->getProcessPtr();
16336701Sgblack@eecs.umich.edu    ctc->setProcessPtr(cp);
16346701Sgblack@eecs.umich.edu    cp->assignThreadContext(ctc->contextId());
163511383Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
163611383Sbrandon.potter@amd.com
16378324Ssteve.reinhardt@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
163810486Stjablin@gmail.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
1639360SN/A        long *ptid = (long *)ptidBuf.bufferPtr();
164011385Sbrandon.potter@amd.com        *ptid = cp->pid();
164111385Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
16429008Sgblack@eecs.umich.edu    }
164311383Sbrandon.potter@amd.com
164411383Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
164511383Sbrandon.potter@amd.com        cp->pTable->shared = true;
164611383Sbrandon.potter@amd.com        cp->useForClone = true;
164711383Sbrandon.potter@amd.com    }
164811383Sbrandon.potter@amd.com    cp->initState();
164911383Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
165011383Sbrandon.potter@amd.com
165111383Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
16528324Ssteve.reinhardt@amd.com        delete cp->sigchld;
165311383Sbrandon.potter@amd.com        cp->sigchld = p->sigchld;
165411383Sbrandon.potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
165511383Sbrandon.potter@amd.com        *cp->sigchld = true;
165611383Sbrandon.potter@amd.com    }
165711383Sbrandon.potter@amd.com
165811383Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
165911383Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
166011383Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
166111383Sbrandon.potter@amd.com        *ctid = cp->pid();
166211383Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
166311383Sbrandon.potter@amd.com    }
166411383Sbrandon.potter@amd.com
166511383Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
166611383Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
166711383Sbrandon.potter@amd.com
166811383Sbrandon.potter@amd.com    ctc->clearArchRegs();
166911383Sbrandon.potter@amd.com
167011383Sbrandon.potter@amd.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
167111383Sbrandon.potter@amd.com
167211383Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
167311383Sbrandon.potter@amd.com
167411383Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
167511383Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
167611383Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
16778324Ssteve.reinhardt@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
16785877Shsul@eecs.umich.edu    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
167910486Stjablin@gmail.com#endif
168010486Stjablin@gmail.com
168111383Sbrandon.potter@amd.com    if (p->kvmInSE) {
168211383Sbrandon.potter@amd.com#if THE_ISA == X86_ISA
168311383Sbrandon.potter@amd.com        ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX));
168411856Sbrandon.potter@amd.com#else
168511624Smichael.lebeane@amd.com        panic("KVM CPU model is not supported for this ISA");
168611856Sbrandon.potter@amd.com#endif
168711856Sbrandon.potter@amd.com    } else {
168811856Sbrandon.potter@amd.com        TheISA::PCState cpc = tc->pcState();
168911856Sbrandon.potter@amd.com        cpc.advance();
169011624Smichael.lebeane@amd.com        ctc->pcState(cpc);
169111624Smichael.lebeane@amd.com    }
169211624Smichael.lebeane@amd.com    ctc->activate();
169311856Sbrandon.potter@amd.com
169411856Sbrandon.potter@amd.com    return cp->pid();
169511383Sbrandon.potter@amd.com}
169611856Sbrandon.potter@amd.com
1697360SN/A/// Target fstatfs() handler.
169811913SBrandon.Potter@amd.comtemplate <class OS>
169911383Sbrandon.potter@amd.comSyscallReturn
17008600Ssteve.reinhardt@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
170111383Sbrandon.potter@amd.com{
170211383Sbrandon.potter@amd.com    int index = 0;
170311383Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
17048600Ssteve.reinhardt@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
17052544SN/A
17062544SN/A    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
170711383Sbrandon.potter@amd.com    if (!ffdp)
170811383Sbrandon.potter@amd.com        return -EBADF;
170911383Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
171011905SBrandon.Potter@amd.com
171111905SBrandon.Potter@amd.com    struct statfs hostBuf;
171211905SBrandon.Potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
171311905SBrandon.Potter@amd.com
171411905SBrandon.Potter@amd.com    if (result < 0)
171511905SBrandon.Potter@amd.com        return -errno;
171611905SBrandon.Potter@amd.com
171711383Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
171811383Sbrandon.potter@amd.com
171911383Sbrandon.potter@amd.com    return 0;
172011383Sbrandon.potter@amd.com}
172111383Sbrandon.potter@amd.com
172211383Sbrandon.potter@amd.com/// Target readv() handler.
172311383Sbrandon.potter@amd.comtemplate <class OS>
172411383Sbrandon.potter@amd.comSyscallReturn
172511383Sbrandon.potter@amd.comreadvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
172611383Sbrandon.potter@amd.com{
172711383Sbrandon.potter@amd.com    int index = 0;
172811383Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
172911383Sbrandon.potter@amd.com
173011383Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
173111383Sbrandon.potter@amd.com    if (!ffdp)
17328600Ssteve.reinhardt@amd.com        return -EBADF;
17336672Sgblack@eecs.umich.edu    int sim_fd = ffdp->getSimFD();
17348600Ssteve.reinhardt@amd.com
173511383Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
173611383Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
173711383Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
17388601Ssteve.reinhardt@amd.com    typename OS::tgt_iovec tiov[count];
17392544SN/A    struct iovec hiov[count];
174011383Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
174111383Sbrandon.potter@amd.com        prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)),
174211383Sbrandon.potter@amd.com                      (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec));
174311383Sbrandon.potter@amd.com        hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len);
174411383Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
174511383Sbrandon.potter@amd.com    }
174611383Sbrandon.potter@amd.com
174711383Sbrandon.potter@amd.com    int result = readv(sim_fd, hiov, count);
174811383Sbrandon.potter@amd.com    int local_errno = errno;
174911383Sbrandon.potter@amd.com
175011383Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
175111383Sbrandon.potter@amd.com        if (result != -1) {
175211383Sbrandon.potter@amd.com            prox.writeBlob(TheISA::htog(tiov[i].iov_base),
175311383Sbrandon.potter@amd.com                           (uint8_t*)hiov[i].iov_base, hiov[i].iov_len);
175411383Sbrandon.potter@amd.com        }
175511383Sbrandon.potter@amd.com        delete [] (char *)hiov[i].iov_base;
175611383Sbrandon.potter@amd.com    }
175711383Sbrandon.potter@amd.com
175811383Sbrandon.potter@amd.com    return (result == -1) ? -local_errno : result;
175911383Sbrandon.potter@amd.com}
176011383Sbrandon.potter@amd.com
176111383Sbrandon.potter@amd.com/// Target writev() handler.
176211383Sbrandon.potter@amd.comtemplate <class OS>
176311383Sbrandon.potter@amd.comSyscallReturn
176411383Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
176511383Sbrandon.potter@amd.com{
176611383Sbrandon.potter@amd.com    int index = 0;
176711383Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
176811383Sbrandon.potter@amd.com
176911383Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
177011383Sbrandon.potter@amd.com    if (!hbfdp)
177111383Sbrandon.potter@amd.com        return -EBADF;
177211392Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
177311392Sbrandon.potter@amd.com
177411392Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
177511392Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
177611392Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
177711392Sbrandon.potter@amd.com    struct iovec hiov[count];
177811392Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i) {
177911392Sbrandon.potter@amd.com        typename OS::tgt_iovec tiov;
178011392Sbrandon.potter@amd.com
178111392Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
178211392Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
178311392Sbrandon.potter@amd.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
178411392Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
178511392Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
178611856Sbrandon.potter@amd.com                      hiov[i].iov_len);
178711856Sbrandon.potter@amd.com    }
178811856Sbrandon.potter@amd.com
178911392Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
179011392Sbrandon.potter@amd.com
179111392Sbrandon.potter@amd.com    for (size_t i = 0; i < count; ++i)
179211392Sbrandon.potter@amd.com        delete [] (char *)hiov[i].iov_base;
179311392Sbrandon.potter@amd.com
179411392Sbrandon.potter@amd.com    return (result == -1) ? -errno : result;
179511392Sbrandon.potter@amd.com}
179611392Sbrandon.potter@amd.com
179711383Sbrandon.potter@amd.com/// Real mmap handler.
179811383Sbrandon.potter@amd.comtemplate <class OS>
179911383Sbrandon.potter@amd.comSyscallReturn
180011383Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
180111383Sbrandon.potter@amd.com         bool is_mmap2)
18021458SN/A{
1803360SN/A    int index = 0;
1804360SN/A    Addr start = p->getSyscallArg(tc, index);
180511593Santhony.gutierrez@amd.com    uint64_t length = p->getSyscallArg(tc, index);
180611593Santhony.gutierrez@amd.com    int prot = p->getSyscallArg(tc, index);
180711851Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
180811593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
180911593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
181011593Santhony.gutierrez@amd.com
181111593Santhony.gutierrez@amd.com    if (is_mmap2)
181211593Santhony.gutierrez@amd.com        offset *= TheISA::PageBytes;
181311593Santhony.gutierrez@amd.com
181411593Santhony.gutierrez@amd.com    if (start & (TheISA::PageBytes - 1) ||
181511856Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
181611856Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
181711593Santhony.gutierrez@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
181811856Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
181911593Santhony.gutierrez@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
182011593Santhony.gutierrez@amd.com        !length) {
182111593Santhony.gutierrez@amd.com        return -EINVAL;
182211593Santhony.gutierrez@amd.com    }
182311594Santhony.gutierrez@amd.com
182411593Santhony.gutierrez@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
182511593Santhony.gutierrez@amd.com        // With shared mmaps, there are two cases to consider:
182611593Santhony.gutierrez@amd.com        // 1) anonymous: writes should modify the mapping and this should be
182711593Santhony.gutierrez@amd.com        // visible to observers who share the mapping. Currently, it's
182811385Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
182911385Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
183011385Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
183111851Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
183211385Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
183311385Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
183411385Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
183511385Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
183611385Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
183711385Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
183811385Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
183911851Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
184011385Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
184111385Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
184211385Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
184311385Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
1844378SN/A        // than we currently maintain.
1845360SN/A        warn("mmap: writing to shared mmap region is currently "
18461450SN/A             "unsupported. The write succeeds on the target, but it "
184711851Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
184811851Sbrandon.potter@amd.com    }
1849360SN/A
18506701Sgblack@eecs.umich.edu    length = roundUp(length, TheISA::PageBytes);
18516701Sgblack@eecs.umich.edu
18526701Sgblack@eecs.umich.edu    int sim_fd = -1;
1853360SN/A    uint8_t *pmap = nullptr;
1854360SN/A    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
185511906SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
185611906SBrandon.Potter@amd.com
185711906SBrandon.Potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
185811906SBrandon.Potter@amd.com        if (dfdp) {
185911906SBrandon.Potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
186011906SBrandon.Potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
1861360SN/A                                     tgt_flags, tgt_fd, offset);
186211906SBrandon.Potter@amd.com        }
186311906SBrandon.Potter@amd.com
186411906SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
186511906SBrandon.Potter@amd.com        if (!ffdp)
186611906SBrandon.Potter@amd.com            return -EBADF;
186711906SBrandon.Potter@amd.com        sim_fd = ffdp->getSimFD();
18685877Shsul@eecs.umich.edu
186911906SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
187011906SBrandon.Potter@amd.com                                    sim_fd, offset);
187111906SBrandon.Potter@amd.com
187211906SBrandon.Potter@amd.com        if (pmap == (decltype(pmap))-1) {
1873360SN/A            warn("mmap: failed to map file into host address space");
1874360SN/A            return -errno;
18758706Sandreas.hansson@arm.com        }
18761458SN/A    }
1877360SN/A
1878360SN/A    // Extend global mmap region if necessary. Note that we ignore the
187912235Sar4jc@virginia.edu    // start address unless MAP_FIXED is specified.
188012235Sar4jc@virginia.edu    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
188112235Sar4jc@virginia.edu        std::shared_ptr<MemState> mem_state = p->memState;
188212235Sar4jc@virginia.edu        Addr mmap_end = mem_state->getMmapEnd();
188312235Sar4jc@virginia.edu
188412235Sar4jc@virginia.edu        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
188512235Sar4jc@virginia.edu        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
188612235Sar4jc@virginia.edu
188712235Sar4jc@virginia.edu        mem_state->setMmapEnd(mmap_end);
188812235Sar4jc@virginia.edu    }
188912235Sar4jc@virginia.edu
189012235Sar4jc@virginia.edu    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
189112235Sar4jc@virginia.edu                    start, start + length - 1);
189212235Sar4jc@virginia.edu
189312235Sar4jc@virginia.edu    // We only allow mappings to overwrite existing mappings if
189412235Sar4jc@virginia.edu    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
189512235Sar4jc@virginia.edu    // because we ignore the start hint if TGT_MAP_FIXED is not set.
189612235Sar4jc@virginia.edu    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
189712416Sqtt2@cornell.edu    if (clobber) {
189812235Sar4jc@virginia.edu        for (auto tc : p->system->threadContexts) {
189912235Sar4jc@virginia.edu            // If we might be overwriting old mappings, we need to
190012235Sar4jc@virginia.edu            // invalidate potentially stale mappings out of the TLBs.
190112235Sar4jc@virginia.edu            tc->getDTBPtr()->flushAll();
190212235Sar4jc@virginia.edu            tc->getITBPtr()->flushAll();
190312235Sar4jc@virginia.edu        }
190412235Sar4jc@virginia.edu    }
190512235Sar4jc@virginia.edu
190612235Sar4jc@virginia.edu    // Allocate physical memory and map it in. If the page table is already
190712235Sar4jc@virginia.edu    // mapped and clobber is not set, the simulator will issue throw a
190812235Sar4jc@virginia.edu    // fatal and bail out of the simulation.
190912235Sar4jc@virginia.edu    p->allocateMem(start, length, clobber);
191012593Sjason@lowepower.com
191112235Sar4jc@virginia.edu    // Transfer content into target address space.
191212235Sar4jc@virginia.edu    SETranslatingPortProxy &tp = tc->getMemProxy();
191312235Sar4jc@virginia.edu    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
191412235Sar4jc@virginia.edu        // In general, we should zero the mapped area for anonymous mappings,
191512235Sar4jc@virginia.edu        // with something like:
191612235Sar4jc@virginia.edu        //     tp.memsetBlob(start, 0, length);
191712235Sar4jc@virginia.edu        // However, given that we don't support sparse mappings, and
191812235Sar4jc@virginia.edu        // some applications can map a couple of gigabytes of space
191912235Sar4jc@virginia.edu        // (intending sparse usage), that can get painfully expensive.
192012235Sar4jc@virginia.edu        // Fortunately, since we don't properly implement munmap either,
192110796Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
192210796Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
192310796Sbrandon.potter@amd.com    } else {
192411851Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
192510796Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
192610796Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
192710796Sbrandon.potter@amd.com        // the host into the target address space.
192810796Sbrandon.potter@amd.com        struct stat file_stat;
192910796Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
193010796Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
193110796Sbrandon.potter@amd.com
193210796Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
193310796Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
193410796Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
193510796Sbrandon.potter@amd.com        // on the specified offset into the file.
193610796Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
193710796Sbrandon.potter@amd.com                                 length);
193810796Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
193910796Sbrandon.potter@amd.com
194011337SMichael.Lebeane@amd.com        // Cleanup the mmap region before exiting this function.
194111337SMichael.Lebeane@amd.com        munmap(pmap, length);
194211337SMichael.Lebeane@amd.com
194311851Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
194411337SMichael.Lebeane@amd.com        // The loader will call mmap to map the images into its address
194511337SMichael.Lebeane@amd.com        // space and we intercept that here. We can verify that we are
194611337SMichael.Lebeane@amd.com        // executing inside the loader by checking the program counter value.
194711337SMichael.Lebeane@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
194811337SMichael.Lebeane@amd.com        // this will not work since there is a single global symbol table.
194911337SMichael.Lebeane@amd.com        ObjectFile *interpreter = p->getInterpreter();
195011337SMichael.Lebeane@amd.com        if (interpreter) {
195111337SMichael.Lebeane@amd.com            Addr text_start = interpreter->textBase();
195211337SMichael.Lebeane@amd.com            Addr text_end = text_start + interpreter->textSize();
195311337SMichael.Lebeane@amd.com
195411337SMichael.Lebeane@amd.com            Addr pc = tc->pcState().pc();
195511337SMichael.Lebeane@amd.com
195611337SMichael.Lebeane@amd.com            if (pc >= text_start && pc < text_end) {
1957378SN/A                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
1958360SN/A                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
19591450SN/A                ObjectFile *lib = createObjectFile(ffdp->getFileName());
196011851Sbrandon.potter@amd.com
196111851Sbrandon.potter@amd.com                if (lib) {
1962360SN/A                    lib->loadAllSymbols(debugSymbolTable,
19636701Sgblack@eecs.umich.edu                                        lib->textBase(), start);
19646701Sgblack@eecs.umich.edu                }
1965360SN/A            }
196610796Sbrandon.potter@amd.com        }
1967360SN/A
19686109Ssanchezd@stanford.edu        // Note that we do not zero out the remainder of the mapping. This
19696109Ssanchezd@stanford.edu        // is done by a real system, but it probably will not affect
1970360SN/A        // execution (hopefully).
19718706Sandreas.hansson@arm.com    }
1972360SN/A
19731458SN/A    return start;
1974360SN/A}
1975360SN/A
1976360SN/Atemplate <class OS>
19771999SN/ASyscallReturn
19781999SN/Apwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
19791999SN/A{
198011851Sbrandon.potter@amd.com    int index = 0;
19812680Sktlim@umich.edu    int tgt_fd = p->getSyscallArg(tc, index);
19821999SN/A    Addr bufPtr = p->getSyscallArg(tc, index);
19831999SN/A    int nbytes = p->getSyscallArg(tc, index);
19841999SN/A    int offset = p->getSyscallArg(tc, index);
19856701Sgblack@eecs.umich.edu
19868852Sandreas.hansson@arm.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
19876701Sgblack@eecs.umich.edu    if (!ffdp)
19886701Sgblack@eecs.umich.edu        return -EBADF;
19896701Sgblack@eecs.umich.edu    int sim_fd = ffdp->getSimFD();
19901999SN/A
19916701Sgblack@eecs.umich.edu    BufferArg bufArg(bufPtr, nbytes);
19926701Sgblack@eecs.umich.edu    bufArg.copyIn(tc->getMemProxy());
19938706Sandreas.hansson@arm.com
19941999SN/A    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
19951999SN/A
199611906SBrandon.Potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
19978737Skoansin.tan@gmail.com}
19988737Skoansin.tan@gmail.com
19991999SN/A/// Target mmap() handler.
20003669Sbinkertn@umich.edutemplate <class OS>
20013669Sbinkertn@umich.eduSyscallReturn
20023669Sbinkertn@umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
20033669Sbinkertn@umich.edu{
20041999SN/A    return mmapImpl<OS>(desc, num, p, tc, false);
20051999SN/A}
20061999SN/A
20071999SN/A/// Target mmap2() handler.
20081999SN/Atemplate <class OS>
20091999SN/ASyscallReturn
20101999SN/Ammap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
201111886Sbrandon.potter@amd.com{
201211886Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
201311886Sbrandon.potter@amd.com}
201411886Sbrandon.potter@amd.com
201511886Sbrandon.potter@amd.com/// Target getrlimit() handler.
201611886Sbrandon.potter@amd.comtemplate <class OS>
201711886Sbrandon.potter@amd.comSyscallReturn
201811886Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
201911886Sbrandon.potter@amd.com              ThreadContext *tc)
202011886Sbrandon.potter@amd.com{
202111886Sbrandon.potter@amd.com    int index = 0;
202211886Sbrandon.potter@amd.com    unsigned resource = process->getSyscallArg(tc, index);
202311886Sbrandon.potter@amd.com    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
202411886Sbrandon.potter@amd.com
202511886Sbrandon.potter@amd.com    switch (resource) {
202611886Sbrandon.potter@amd.com      case OS::TGT_RLIMIT_STACK:
202711886Sbrandon.potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
202811886Sbrandon.potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
202911886Sbrandon.potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
203011886Sbrandon.potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
203111886Sbrandon.potter@amd.com        break;
203211886Sbrandon.potter@amd.com
203311886Sbrandon.potter@amd.com      case OS::TGT_RLIMIT_DATA:
203411886Sbrandon.potter@amd.com        // max data segment size in bytes: make up a number
203511886Sbrandon.potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
203611886Sbrandon.potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
203711886Sbrandon.potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
203811886Sbrandon.potter@amd.com        break;
203911886Sbrandon.potter@amd.com
204011886Sbrandon.potter@amd.com      default:
204111886Sbrandon.potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
204211886Sbrandon.potter@amd.com        return -EINVAL;
204311886Sbrandon.potter@amd.com        break;
204411886Sbrandon.potter@amd.com    }
204511886Sbrandon.potter@amd.com
204611886Sbrandon.potter@amd.com    rlp.copyOut(tc->getMemProxy());
204711886Sbrandon.potter@amd.com    return 0;
204811886Sbrandon.potter@amd.com}
204911886Sbrandon.potter@amd.com
205011886Sbrandon.potter@amd.comtemplate <class OS>
205111886Sbrandon.potter@amd.comSyscallReturn
205211886Sbrandon.potter@amd.comprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
205311886Sbrandon.potter@amd.com            ThreadContext *tc)
205411886Sbrandon.potter@amd.com{
205511886Sbrandon.potter@amd.com    int index = 0;
205611886Sbrandon.potter@amd.com    if (process->getSyscallArg(tc, index) != 0)
205711886Sbrandon.potter@amd.com    {
205811886Sbrandon.potter@amd.com        warn("prlimit: ignoring rlimits for nonzero pid");
205911886Sbrandon.potter@amd.com        return -EPERM;
206011886Sbrandon.potter@amd.com    }
206111886Sbrandon.potter@amd.com    int resource = process->getSyscallArg(tc, index);
206211886Sbrandon.potter@amd.com    Addr n = process->getSyscallArg(tc, index);
206311886Sbrandon.potter@amd.com    if (n != 0)
206411886Sbrandon.potter@amd.com        warn("prlimit: ignoring new rlimit");
206511886Sbrandon.potter@amd.com    Addr o = process->getSyscallArg(tc, index);
206611886Sbrandon.potter@amd.com    if (o != 0)
206711886Sbrandon.potter@amd.com    {
206811886Sbrandon.potter@amd.com        TypedBufferArg<typename OS::rlimit> rlp(o);
206911886Sbrandon.potter@amd.com        switch (resource) {
207011886Sbrandon.potter@amd.com          case OS::TGT_RLIMIT_STACK:
207111886Sbrandon.potter@amd.com            // max stack size in bytes: make up a number (8MB for now)
207211886Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
207311886Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
207411886Sbrandon.potter@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
207511886Sbrandon.potter@amd.com            break;
207611886Sbrandon.potter@amd.com          case OS::TGT_RLIMIT_DATA:
207711886Sbrandon.potter@amd.com            // max data segment size in bytes: make up a number
207811886Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
207911886Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
208011886Sbrandon.potter@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
208111886Sbrandon.potter@amd.com            break;
208211886Sbrandon.potter@amd.com          default:
208311886Sbrandon.potter@amd.com            warn("prlimit: unimplemented resource %d", resource);
208411886Sbrandon.potter@amd.com            return -EINVAL;
208511886Sbrandon.potter@amd.com            break;
208611886Sbrandon.potter@amd.com        }
208711886Sbrandon.potter@amd.com        rlp.copyOut(tc->getMemProxy());
208811886Sbrandon.potter@amd.com    }
208911886Sbrandon.potter@amd.com    return 0;
209011886Sbrandon.potter@amd.com}
209111886Sbrandon.potter@amd.com
209211886Sbrandon.potter@amd.com/// Target clock_gettime() function.
209311886Sbrandon.potter@amd.comtemplate <class OS>
209411886Sbrandon.potter@amd.comSyscallReturn
209511886Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
209611886Sbrandon.potter@amd.com{
209711886Sbrandon.potter@amd.com    int index = 1;
209811886Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
209911886Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
210011886Sbrandon.potter@amd.com
210111886Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
210211886Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
210311886Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
2104378SN/A    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
2105360SN/A
21061450SN/A    tp.copyOut(tc->getMemProxy());
210711851Sbrandon.potter@amd.com
21082680Sktlim@umich.edu    return 0;
2109360SN/A}
21106701Sgblack@eecs.umich.edu
21116701Sgblack@eecs.umich.edu/// Target clock_getres() function.
21126701Sgblack@eecs.umich.edutemplate <class OS>
2113360SN/ASyscallReturn
21143670Sbinkertn@umich.educlock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
21153670Sbinkertn@umich.edu{
2116360SN/A    int index = 1;
2117360SN/A    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
2118360SN/A
2119360SN/A    // Set resolution at ns, which is what clock_gettime() returns
2120360SN/A    tp->tv_sec = 0;
2121360SN/A    tp->tv_nsec = 1;
2122360SN/A
2123360SN/A    tp.copyOut(tc->getMemProxy());
2124360SN/A
2125360SN/A    return 0;
2126360SN/A}
2127360SN/A
2128360SN/A/// Target gettimeofday() handler.
2129360SN/Atemplate <class OS>
2130360SN/ASyscallReturn
2131360SN/AgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
2132360SN/A                 ThreadContext *tc)
21333670Sbinkertn@umich.edu{
21343670Sbinkertn@umich.edu    int index = 0;
213510796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
21368737Skoansin.tan@gmail.com
21378737Skoansin.tan@gmail.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
21383670Sbinkertn@umich.edu    tp->tv_sec += seconds_since_epoch;
21393670Sbinkertn@umich.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
21403670Sbinkertn@umich.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
21413670Sbinkertn@umich.edu
21423670Sbinkertn@umich.edu    tp.copyOut(tc->getMemProxy());
21433670Sbinkertn@umich.edu
21443670Sbinkertn@umich.edu    return 0;
21453670Sbinkertn@umich.edu}
21463670Sbinkertn@umich.edu
21473670Sbinkertn@umich.edu
21483670Sbinkertn@umich.edu/// Target utimes() handler.
21493670Sbinkertn@umich.edutemplate <class OS>
21503670Sbinkertn@umich.eduSyscallReturn
21518706Sandreas.hansson@arm.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
2152360SN/A           ThreadContext *tc)
21531458SN/A{
2154360SN/A    std::string path;
2155360SN/A
21566683Stjones1@inf.ed.ac.uk    int index = 0;
21576683Stjones1@inf.ed.ac.uk    if (!tc->getMemProxy().tryReadString(path,
21586683Stjones1@inf.ed.ac.uk                process->getSyscallArg(tc, index))) {
215911851Sbrandon.potter@amd.com        return -EFAULT;
216011851Sbrandon.potter@amd.com    }
21616683Stjones1@inf.ed.ac.uk
21626701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
21636701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
21646683Stjones1@inf.ed.ac.uk    tp.copyIn(tc->getMemProxy());
21656683Stjones1@inf.ed.ac.uk
21667823Ssteve.reinhardt@amd.com    struct timeval hostTimeval[2];
21676683Stjones1@inf.ed.ac.uk    for (int i = 0; i < 2; ++i) {
21686683Stjones1@inf.ed.ac.uk        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
21696683Stjones1@inf.ed.ac.uk        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
21706683Stjones1@inf.ed.ac.uk    }
21716683Stjones1@inf.ed.ac.uk
21726683Stjones1@inf.ed.ac.uk    // Adjust path for cwd and redirection
21738737Skoansin.tan@gmail.com    path = process->checkPathRedirect(path);
21746683Stjones1@inf.ed.ac.uk
21756683Stjones1@inf.ed.ac.uk    int result = utimes(path.c_str(), hostTimeval);
21768706Sandreas.hansson@arm.com
21776683Stjones1@inf.ed.ac.uk    if (result < 0)
21786683Stjones1@inf.ed.ac.uk        return -errno;
21796683Stjones1@inf.ed.ac.uk
21806683Stjones1@inf.ed.ac.uk    return 0;
21812553SN/A}
21826684Stjones1@inf.ed.ac.uk
21836684Stjones1@inf.ed.ac.uktemplate <class OS>
21846684Stjones1@inf.ed.ac.ukSyscallReturn
218511851Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
21866684Stjones1@inf.ed.ac.uk{
21876684Stjones1@inf.ed.ac.uk    desc->setFlags(0);
218810796Sbrandon.potter@amd.com
21896684Stjones1@inf.ed.ac.uk    int index = 0;
21906684Stjones1@inf.ed.ac.uk    std::string path;
21916701Sgblack@eecs.umich.edu    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
21926701Sgblack@eecs.umich.edu    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
219311321Ssteve.reinhardt@amd.com        return -EFAULT;
21946684Stjones1@inf.ed.ac.uk
21958737Skoansin.tan@gmail.com    if (access(path.c_str(), F_OK) == -1)
21968852Sandreas.hansson@arm.com        return -EACCES;
21978852Sandreas.hansson@arm.com
21986684Stjones1@inf.ed.ac.uk    auto read_in = [](std::vector<std::string> & vect,
21996684Stjones1@inf.ed.ac.uk                      SETranslatingPortProxy & mem_proxy,
22006684Stjones1@inf.ed.ac.uk                      Addr mem_loc)
22012553SN/A    {
220211910SBrandon.Potter@amd.com        for (int inc = 0; ; inc++) {
220311910SBrandon.Potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
220411910SBrandon.Potter@amd.com            b.copyIn(mem_proxy);
220511910SBrandon.Potter@amd.com
220611910SBrandon.Potter@amd.com            if (!*(Addr*)b.bufferPtr())
220711910SBrandon.Potter@amd.com                break;
220811910SBrandon.Potter@amd.com
220911910SBrandon.Potter@amd.com            vect.push_back(std::string());
221011910SBrandon.Potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
221111910SBrandon.Potter@amd.com        }
221211910SBrandon.Potter@amd.com    };
221311910SBrandon.Potter@amd.com
221411910SBrandon.Potter@amd.com    /**
221511910SBrandon.Potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
221611910SBrandon.Potter@amd.com     * examples of how to create anything but this default constructor. The
221711910SBrandon.Potter@amd.com     * fields are manually initialized instead of passing parameters to the
221811910SBrandon.Potter@amd.com     * constructor.
221911910SBrandon.Potter@amd.com     */
222011910SBrandon.Potter@amd.com    ProcessParams *pp = new ProcessParams();
222111910SBrandon.Potter@amd.com    pp->executable = path;
222211910SBrandon.Potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
222311910SBrandon.Potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
222411910SBrandon.Potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
222511910SBrandon.Potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
222611910SBrandon.Potter@amd.com    pp->uid = p->uid();
222711910SBrandon.Potter@amd.com    pp->egid = p->egid();
222811910SBrandon.Potter@amd.com    pp->euid = p->euid();
222911910SBrandon.Potter@amd.com    pp->gid = p->gid();
223011910SBrandon.Potter@amd.com    pp->ppid = p->ppid();
223111910SBrandon.Potter@amd.com    pp->pid = p->pid();
223211910SBrandon.Potter@amd.com    pp->input.assign("cin");
223311910SBrandon.Potter@amd.com    pp->output.assign("cout");
223411910SBrandon.Potter@amd.com    pp->errout.assign("cerr");
223511910SBrandon.Potter@amd.com    pp->cwd.assign(p->tgtCwd);
223611910SBrandon.Potter@amd.com    pp->system = p->system;
223711910SBrandon.Potter@amd.com    /**
223811910SBrandon.Potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
223911910SBrandon.Potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
224011910SBrandon.Potter@amd.com     * take over the currently executing process' identity but replace
224111910SBrandon.Potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
224211910SBrandon.Potter@amd.com     * the process object in the simulator, we create a new process object
224311910SBrandon.Potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
224411910SBrandon.Potter@amd.com     */
224511910SBrandon.Potter@amd.com    p->system->PIDs.erase(p->pid());
224611910SBrandon.Potter@amd.com    Process *new_p = pp->create();
224711910SBrandon.Potter@amd.com    delete pp;
224811910SBrandon.Potter@amd.com
224911910SBrandon.Potter@amd.com    /**
225011910SBrandon.Potter@amd.com     * Work through the file descriptor array and close any files marked
225113568Sbrandon.potter@amd.com     * close-on-exec.
225213568Sbrandon.potter@amd.com     */
225313568Sbrandon.potter@amd.com    new_p->fds = p->fds;
225413568Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
225513568Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
225613568Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
225713568Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
225813568Sbrandon.potter@amd.com    }
225913568Sbrandon.potter@amd.com
226013568Sbrandon.potter@amd.com    *new_p->sigchld = true;
226113568Sbrandon.potter@amd.com
226213568Sbrandon.potter@amd.com    delete p;
226313568Sbrandon.potter@amd.com    tc->clearArchRegs();
226413568Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
226513568Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
226613568Sbrandon.potter@amd.com    new_p->initState();
226713568Sbrandon.potter@amd.com    tc->activate();
226813568Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
226913568Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
227013568Sbrandon.potter@amd.com
227113568Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
227213568Sbrandon.potter@amd.com    return 0;
227313568Sbrandon.potter@amd.com}
227413568Sbrandon.potter@amd.com
227513568Sbrandon.potter@amd.com/// Target getrusage() function.
227613568Sbrandon.potter@amd.comtemplate <class OS>
227713568Sbrandon.potter@amd.comSyscallReturn
227813568Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
227913568Sbrandon.potter@amd.com              ThreadContext *tc)
228013568Sbrandon.potter@amd.com{
228113568Sbrandon.potter@amd.com    int index = 0;
228213568Sbrandon.potter@amd.com    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
228313568Sbrandon.potter@amd.com    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
228413568Sbrandon.potter@amd.com
228513568Sbrandon.potter@amd.com    rup->ru_utime.tv_sec = 0;
228613568Sbrandon.potter@amd.com    rup->ru_utime.tv_usec = 0;
228713568Sbrandon.potter@amd.com    rup->ru_stime.tv_sec = 0;
228813568Sbrandon.potter@amd.com    rup->ru_stime.tv_usec = 0;
228913568Sbrandon.potter@amd.com    rup->ru_maxrss = 0;
229013568Sbrandon.potter@amd.com    rup->ru_ixrss = 0;
229113568Sbrandon.potter@amd.com    rup->ru_idrss = 0;
229213568Sbrandon.potter@amd.com    rup->ru_isrss = 0;
229313568Sbrandon.potter@amd.com    rup->ru_minflt = 0;
229413568Sbrandon.potter@amd.com    rup->ru_majflt = 0;
22952553SN/A    rup->ru_nswap = 0;
229613570Sbrandon.potter@amd.com    rup->ru_inblock = 0;
229713570Sbrandon.potter@amd.com    rup->ru_oublock = 0;
229813570Sbrandon.potter@amd.com    rup->ru_msgsnd = 0;
229913570Sbrandon.potter@amd.com    rup->ru_msgrcv = 0;
230013570Sbrandon.potter@amd.com    rup->ru_nsignals = 0;
230113570Sbrandon.potter@amd.com    rup->ru_nvcsw = 0;
230213570Sbrandon.potter@amd.com    rup->ru_nivcsw = 0;
230313570Sbrandon.potter@amd.com
230413570Sbrandon.potter@amd.com    switch (who) {
230513570Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_SELF:
230613570Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
230713570Sbrandon.potter@amd.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
230813570Sbrandon.potter@amd.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
230913570Sbrandon.potter@amd.com        break;
231013570Sbrandon.potter@amd.com
231113570Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_CHILDREN:
231213570Sbrandon.potter@amd.com        // do nothing.  We have no child processes, so they take no time.
231313570Sbrandon.potter@amd.com        break;
231413570Sbrandon.potter@amd.com
231513570Sbrandon.potter@amd.com      default:
231613570Sbrandon.potter@amd.com        // don't really handle THREAD or CHILDREN, but just warn and
231713570Sbrandon.potter@amd.com        // plow ahead
231813570Sbrandon.potter@amd.com        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
231913570Sbrandon.potter@amd.com             who);
232013570Sbrandon.potter@amd.com    }
232113570Sbrandon.potter@amd.com
232213570Sbrandon.potter@amd.com    rup.copyOut(tc->getMemProxy());
232313570Sbrandon.potter@amd.com
232413570Sbrandon.potter@amd.com    return 0;
232513570Sbrandon.potter@amd.com}
232613570Sbrandon.potter@amd.com
232713570Sbrandon.potter@amd.com/// Target times() function.
232813570Sbrandon.potter@amd.comtemplate <class OS>
232913570Sbrandon.potter@amd.comSyscallReturn
233013570Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
233113570Sbrandon.potter@amd.com          ThreadContext *tc)
233213570Sbrandon.potter@amd.com{
233313570Sbrandon.potter@amd.com    int index = 0;
233413570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
233513570Sbrandon.potter@amd.com
233613570Sbrandon.potter@amd.com    // Fill in the time structure (in clocks)
233713570Sbrandon.potter@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
233813570Sbrandon.potter@amd.com    bufp->tms_utime = clocks;
233913570Sbrandon.potter@amd.com    bufp->tms_stime = 0;
234013570Sbrandon.potter@amd.com    bufp->tms_cutime = 0;
234113570Sbrandon.potter@amd.com    bufp->tms_cstime = 0;
234213570Sbrandon.potter@amd.com
234313570Sbrandon.potter@amd.com    // Convert to host endianness
234413570Sbrandon.potter@amd.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
234513570Sbrandon.potter@amd.com
234613570Sbrandon.potter@amd.com    // Write back
234713570Sbrandon.potter@amd.com    bufp.copyOut(tc->getMemProxy());
234813570Sbrandon.potter@amd.com
234913570Sbrandon.potter@amd.com    // Return clock ticks since system boot
235013570Sbrandon.potter@amd.com    return clocks;
235113570Sbrandon.potter@amd.com}
235213570Sbrandon.potter@amd.com
235313570Sbrandon.potter@amd.com/// Target time() function.
235413570Sbrandon.potter@amd.comtemplate <class OS>
235513570Sbrandon.potter@amd.comSyscallReturn
235613570Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
235713570Sbrandon.potter@amd.com{
235813570Sbrandon.potter@amd.com    typename OS::time_t sec, usec;
235913570Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
236013570Sbrandon.potter@amd.com    sec += seconds_since_epoch;
236113570Sbrandon.potter@amd.com
236213570Sbrandon.potter@amd.com    int index = 0;
236313570Sbrandon.potter@amd.com    Addr taddr = (Addr)process->getSyscallArg(tc, index);
236413570Sbrandon.potter@amd.com    if (taddr != 0) {
236513570Sbrandon.potter@amd.com        typename OS::time_t t = sec;
236613570Sbrandon.potter@amd.com        t = TheISA::htog(t);
236713570Sbrandon.potter@amd.com        SETranslatingPortProxy &p = tc->getMemProxy();
236813570Sbrandon.potter@amd.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
236913570Sbrandon.potter@amd.com    }
237013570Sbrandon.potter@amd.com    return sec;
237113570Sbrandon.potter@amd.com}
237213570Sbrandon.potter@amd.com
237313570Sbrandon.potter@amd.comtemplate <class OS>
237413570Sbrandon.potter@amd.comSyscallReturn
237513570Sbrandon.potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
237613570Sbrandon.potter@amd.com{
237713570Sbrandon.potter@amd.com    int index = 0;
237813570Sbrandon.potter@amd.com    int tgid = process->getSyscallArg(tc, index);
237913570Sbrandon.potter@amd.com    int tid = process->getSyscallArg(tc, index);
238013570Sbrandon.potter@amd.com    int sig = process->getSyscallArg(tc, index);
238113570Sbrandon.potter@amd.com
238213570Sbrandon.potter@amd.com    /**
238313570Sbrandon.potter@amd.com     * This system call is intended to allow killing a specific thread
238413570Sbrandon.potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
238513570Sbrandon.potter@amd.com     * It's usually true that threads share the termination signal as pointed
238613570Sbrandon.potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
238713570Sbrandon.potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
238813570Sbrandon.potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
238913570Sbrandon.potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
239013570Sbrandon.potter@amd.com     * registration of signal handlers since signals are poorly supported in
239113570Sbrandon.potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
239213570Sbrandon.potter@amd.com     * threads within in a thread group must share the termination signal.
239313570Sbrandon.potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
239413570Sbrandon.potter@amd.com     * due to PID rollover.
239513570Sbrandon.potter@amd.com     */
239613570Sbrandon.potter@amd.com
239713570Sbrandon.potter@amd.com    System *sys = tc->getSystemPtr();
239813570Sbrandon.potter@amd.com    Process *tgt_proc = nullptr;
239913570Sbrandon.potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
240013570Sbrandon.potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
240113570Sbrandon.potter@amd.com        if (temp->pid() == tid) {
240213570Sbrandon.potter@amd.com            tgt_proc = temp;
240313570Sbrandon.potter@amd.com            break;
240413570Sbrandon.potter@amd.com        }
240513570Sbrandon.potter@amd.com    }
240613570Sbrandon.potter@amd.com
240713570Sbrandon.potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
240813570Sbrandon.potter@amd.com        return -EINVAL;
240913570Sbrandon.potter@amd.com
241013570Sbrandon.potter@amd.com    if (tgt_proc == nullptr)
241113570Sbrandon.potter@amd.com        return -ESRCH;
241213570Sbrandon.potter@amd.com
241313570Sbrandon.potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
241413570Sbrandon.potter@amd.com        return -ESRCH;
241513570Sbrandon.potter@amd.com
241613570Sbrandon.potter@amd.com    if (sig == OS::TGT_SIGABRT)
241713570Sbrandon.potter@amd.com        exitGroupFunc(desc, 252, process, tc);
241813570Sbrandon.potter@amd.com
241913570Sbrandon.potter@amd.com    return 0;
242013570Sbrandon.potter@amd.com}
242113570Sbrandon.potter@amd.com
242213570Sbrandon.potter@amd.comtemplate <class OS>
242313570Sbrandon.potter@amd.comSyscallReturn
242413570Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
242513570Sbrandon.potter@amd.com{
242613570Sbrandon.potter@amd.com    int index = 0;
242713570Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
242813570Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
242913570Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
243013570Sbrandon.potter@amd.com
243113570Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
243213570Sbrandon.potter@amd.com    if (sim_fd == -1)
243313570Sbrandon.potter@amd.com        return -errno;
243413570Sbrandon.potter@amd.com
243513570Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
243613570Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
243713570Sbrandon.potter@amd.com
243813570Sbrandon.potter@amd.com    return tgt_fd;
243913570Sbrandon.potter@amd.com}
244013570Sbrandon.potter@amd.com
244113570Sbrandon.potter@amd.comtemplate <class OS>
244213570Sbrandon.potter@amd.comSyscallReturn
244313570Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
244413570Sbrandon.potter@amd.com{
244513570Sbrandon.potter@amd.com    int index = 0;
244613570Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
244713570Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
244813570Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
244913570Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
245013570Sbrandon.potter@amd.com
245113570Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
245213570Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
245313570Sbrandon.potter@amd.com    if (status == -1)
245413570Sbrandon.potter@amd.com        return -errno;
245513570Sbrandon.potter@amd.com
245613570Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
245713570Sbrandon.potter@amd.com
245813570Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
245913570Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
246013570Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
246113570Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
246213570Sbrandon.potter@amd.com    svBuf.copyOut(tc->getMemProxy());
246313570Sbrandon.potter@amd.com
246413570Sbrandon.potter@amd.com    return status;
246513570Sbrandon.potter@amd.com}
246613570Sbrandon.potter@amd.com
246713570Sbrandon.potter@amd.comtemplate <class OS>
246813570Sbrandon.potter@amd.comSyscallReturn
246913570Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
247013570Sbrandon.potter@amd.com{
247113570Sbrandon.potter@amd.com    int retval;
247213570Sbrandon.potter@amd.com
247313570Sbrandon.potter@amd.com    int index = 0;
247413570Sbrandon.potter@amd.com    int nfds_t = p->getSyscallArg(tc, index);
247513570Sbrandon.potter@amd.com    Addr fds_read_ptr = p->getSyscallArg(tc, index);
247613570Sbrandon.potter@amd.com    Addr fds_writ_ptr = p->getSyscallArg(tc, index);
247713570Sbrandon.potter@amd.com    Addr fds_excp_ptr = p->getSyscallArg(tc, index);
247813570Sbrandon.potter@amd.com    Addr time_val_ptr = p->getSyscallArg(tc, index);
247913570Sbrandon.potter@amd.com
248013570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr);
248113570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr);
248213570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr);
248313570Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(time_val_ptr);
248413570Sbrandon.potter@amd.com
248513570Sbrandon.potter@amd.com    /**
248613570Sbrandon.potter@amd.com     * Host fields. Notice that these use the definitions from the system
248713570Sbrandon.potter@amd.com     * headers instead of the gem5 headers and libraries. If the host and
248813570Sbrandon.potter@amd.com     * target have different header file definitions, this will not work.
248913570Sbrandon.potter@amd.com     */
249013570Sbrandon.potter@amd.com    fd_set rd_h;
249113570Sbrandon.potter@amd.com    FD_ZERO(&rd_h);
249213570Sbrandon.potter@amd.com    fd_set wr_h;
249313570Sbrandon.potter@amd.com    FD_ZERO(&wr_h);
249413570Sbrandon.potter@amd.com    fd_set ex_h;
249513570Sbrandon.potter@amd.com    FD_ZERO(&ex_h);
249613570Sbrandon.potter@amd.com
249713570Sbrandon.potter@amd.com    /**
249813570Sbrandon.potter@amd.com     * Copy in the fd_set from the target.
249913570Sbrandon.potter@amd.com     */
250013570Sbrandon.potter@amd.com    if (fds_read_ptr)
250113570Sbrandon.potter@amd.com        rd_t.copyIn(tc->getMemProxy());
250213570Sbrandon.potter@amd.com    if (fds_writ_ptr)
250313570Sbrandon.potter@amd.com        wr_t.copyIn(tc->getMemProxy());
250413570Sbrandon.potter@amd.com    if (fds_excp_ptr)
250513570Sbrandon.potter@amd.com        ex_t.copyIn(tc->getMemProxy());
250613570Sbrandon.potter@amd.com
250713570Sbrandon.potter@amd.com    /**
250813570Sbrandon.potter@amd.com     * We need to translate the target file descriptor set into a host file
250913570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
251013570Sbrandon.potter@amd.com     * and the fd_set defined in Linux header files. The nfds field also
251113570Sbrandon.potter@amd.com     * needs to be updated as it will be only target specific after
251213570Sbrandon.potter@amd.com     * retrieving it from the target; the nfds value is expected to be the
251313570Sbrandon.potter@amd.com     * highest file descriptor that needs to be checked, so we need to extend
251413570Sbrandon.potter@amd.com     * it out for nfds_h when we do the update.
251513570Sbrandon.potter@amd.com     */
251613570Sbrandon.potter@amd.com    int nfds_h = 0;
251713570Sbrandon.potter@amd.com    std::map<int, int> trans_map;
251813570Sbrandon.potter@amd.com    auto try_add_host_set = [&](fd_set *tgt_set_entry,
251913570Sbrandon.potter@amd.com                                fd_set *hst_set_entry,
252013570Sbrandon.potter@amd.com                                int iter) -> bool
252113570Sbrandon.potter@amd.com    {
252213570Sbrandon.potter@amd.com        /**
252313570Sbrandon.potter@amd.com         * By this point, we know that we are looking at a valid file
252413570Sbrandon.potter@amd.com         * descriptor set on the target. We need to check if the target file
252513570Sbrandon.potter@amd.com         * descriptor value passed in as iter is part of the set.
252613570Sbrandon.potter@amd.com         */
252713570Sbrandon.potter@amd.com        if (FD_ISSET(iter, tgt_set_entry)) {
252813570Sbrandon.potter@amd.com            /**
252913570Sbrandon.potter@amd.com             * We know that the target file descriptor belongs to the set,
253013570Sbrandon.potter@amd.com             * but we do not yet know if the file descriptor is valid or
253113570Sbrandon.potter@amd.com             * that we have a host mapping. Check that now.
253213570Sbrandon.potter@amd.com             */
253313570Sbrandon.potter@amd.com            auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]);
253413570Sbrandon.potter@amd.com            if (!hbfdp)
253513570Sbrandon.potter@amd.com                return true;
253613570Sbrandon.potter@amd.com            auto sim_fd = hbfdp->getSimFD();
253713570Sbrandon.potter@amd.com
253813570Sbrandon.potter@amd.com            /**
253913570Sbrandon.potter@amd.com             * Add the sim_fd to tgt_fd translation into trans_map for use
254013570Sbrandon.potter@amd.com             * later when we need to zero the target fd_set structures and
254113570Sbrandon.potter@amd.com             * then update them with hits returned from the host select call.
254213570Sbrandon.potter@amd.com             */
254313570Sbrandon.potter@amd.com            trans_map[sim_fd] = iter;
254413570Sbrandon.potter@amd.com
254513570Sbrandon.potter@amd.com            /**
254613570Sbrandon.potter@amd.com             * We know that the host file descriptor exists so now we check
254713570Sbrandon.potter@amd.com             * if we need to update the max count for nfds_h before passing
254813570Sbrandon.potter@amd.com             * the duplicated structure into the host.
254913570Sbrandon.potter@amd.com             */
255013570Sbrandon.potter@amd.com            nfds_h = std::max(nfds_h - 1, sim_fd + 1);
255113570Sbrandon.potter@amd.com
255213570Sbrandon.potter@amd.com            /**
255313570Sbrandon.potter@amd.com             * Add the host file descriptor to the set that we are going to
255413570Sbrandon.potter@amd.com             * pass into the host.
255513570Sbrandon.potter@amd.com             */
255613570Sbrandon.potter@amd.com            FD_SET(sim_fd, hst_set_entry);
255713570Sbrandon.potter@amd.com        }
255813570Sbrandon.potter@amd.com        return false;
255913570Sbrandon.potter@amd.com    };
256013570Sbrandon.potter@amd.com
256113570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_t; i++) {
256213570Sbrandon.potter@amd.com        if (fds_read_ptr) {
256313570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i);
256413570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
256513570Sbrandon.potter@amd.com        }
256613570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
256713570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i);
256813570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
256913570Sbrandon.potter@amd.com        }
257013570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
257113570Sbrandon.potter@amd.com            bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i);
257213570Sbrandon.potter@amd.com            if (ebadf) return -EBADF;
257313570Sbrandon.potter@amd.com        }
257413570Sbrandon.potter@amd.com    }
257513570Sbrandon.potter@amd.com
257613570Sbrandon.potter@amd.com    if (time_val_ptr) {
257713570Sbrandon.potter@amd.com        /**
257813570Sbrandon.potter@amd.com         * It might be possible to decrement the timeval based on some
257913570Sbrandon.potter@amd.com         * derivation of wall clock determined from elapsed simulator ticks
258013570Sbrandon.potter@amd.com         * but that seems like overkill. Rather, we just set the timeval with
258113570Sbrandon.potter@amd.com         * zero timeout. (There is no reason to block during the simulation
258213570Sbrandon.potter@amd.com         * as it only decreases simulator performance.)
258313570Sbrandon.potter@amd.com         */
258413570Sbrandon.potter@amd.com        tp->tv_sec = 0;
258513570Sbrandon.potter@amd.com        tp->tv_usec = 0;
258613570Sbrandon.potter@amd.com
258713570Sbrandon.potter@amd.com        retval = select(nfds_h,
258813570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
258913570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
259013570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
259113570Sbrandon.potter@amd.com                        (timeval*)&*tp);
259213570Sbrandon.potter@amd.com    } else {
259313570Sbrandon.potter@amd.com        /**
259413570Sbrandon.potter@amd.com         * If the timeval pointer is null, setup a new timeval structure to
259513570Sbrandon.potter@amd.com         * pass into the host select call. Unfortunately, we will need to
259613570Sbrandon.potter@amd.com         * manually check the return value and throw a retry fault if the
259713570Sbrandon.potter@amd.com         * return value is zero. Allowing the system call to block will
259813570Sbrandon.potter@amd.com         * likely deadlock the event queue.
259913570Sbrandon.potter@amd.com         */
260013570Sbrandon.potter@amd.com        struct timeval tv = { 0, 0 };
260113570Sbrandon.potter@amd.com
260213570Sbrandon.potter@amd.com        retval = select(nfds_h,
260313570Sbrandon.potter@amd.com                        fds_read_ptr ? &rd_h : nullptr,
260413570Sbrandon.potter@amd.com                        fds_writ_ptr ? &wr_h : nullptr,
260513570Sbrandon.potter@amd.com                        fds_excp_ptr ? &ex_h : nullptr,
260613570Sbrandon.potter@amd.com                        &tv);
260713570Sbrandon.potter@amd.com
260813570Sbrandon.potter@amd.com        if (retval == 0) {
260913570Sbrandon.potter@amd.com            /**
261013570Sbrandon.potter@amd.com             * If blocking indefinitely, check the signal list to see if a
261113570Sbrandon.potter@amd.com             * signal would break the poll out of the retry cycle and try to
261213570Sbrandon.potter@amd.com             * return the signal interrupt instead.
261313570Sbrandon.potter@amd.com             */
261413570Sbrandon.potter@amd.com            for (auto sig : tc->getSystemPtr()->signalList)
261513570Sbrandon.potter@amd.com                if (sig.receiver == p)
261613570Sbrandon.potter@amd.com                    return -EINTR;
261713570Sbrandon.potter@amd.com            return SyscallReturn::retry();
261813570Sbrandon.potter@amd.com        }
261913570Sbrandon.potter@amd.com    }
262013570Sbrandon.potter@amd.com
262113570Sbrandon.potter@amd.com    if (retval == -1)
262213570Sbrandon.potter@amd.com        return -errno;
262313570Sbrandon.potter@amd.com
262413570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*rd_t);
262513570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*wr_t);
262613570Sbrandon.potter@amd.com    FD_ZERO((fd_set*)&*ex_t);
262713570Sbrandon.potter@amd.com
262813570Sbrandon.potter@amd.com    /**
262913570Sbrandon.potter@amd.com     * We need to translate the host file descriptor set into a target file
263013570Sbrandon.potter@amd.com     * descriptor set. This involves both our internal process fd array
263113570Sbrandon.potter@amd.com     * and the fd_set defined in header files.
263213570Sbrandon.potter@amd.com     */
263313570Sbrandon.potter@amd.com    for (int i = 0; i < nfds_h; i++) {
263413570Sbrandon.potter@amd.com        if (fds_read_ptr) {
263513570Sbrandon.potter@amd.com            if (FD_ISSET(i, &rd_h))
263613570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*rd_t);
263713570Sbrandon.potter@amd.com        }
263813570Sbrandon.potter@amd.com
263913570Sbrandon.potter@amd.com        if (fds_writ_ptr) {
264013570Sbrandon.potter@amd.com            if (FD_ISSET(i, &wr_h))
264113570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*wr_t);
264213570Sbrandon.potter@amd.com        }
264313570Sbrandon.potter@amd.com
264413570Sbrandon.potter@amd.com        if (fds_excp_ptr) {
264513570Sbrandon.potter@amd.com            if (FD_ISSET(i, &ex_h))
264613570Sbrandon.potter@amd.com                FD_SET(trans_map[i], (fd_set*)&*ex_t);
264713570Sbrandon.potter@amd.com        }
264813570Sbrandon.potter@amd.com    }
264913570Sbrandon.potter@amd.com
265013570Sbrandon.potter@amd.com    if (fds_read_ptr)
265113570Sbrandon.potter@amd.com        rd_t.copyOut(tc->getMemProxy());
265213570Sbrandon.potter@amd.com    if (fds_writ_ptr)
265313570Sbrandon.potter@amd.com        wr_t.copyOut(tc->getMemProxy());
265413570Sbrandon.potter@amd.com    if (fds_excp_ptr)
265513570Sbrandon.potter@amd.com        ex_t.copyOut(tc->getMemProxy());
265613570Sbrandon.potter@amd.com    if (time_val_ptr)
265713570Sbrandon.potter@amd.com        tp.copyOut(tc->getMemProxy());
265813570Sbrandon.potter@amd.com
265913570Sbrandon.potter@amd.com    return retval;
266013570Sbrandon.potter@amd.com}
266113570Sbrandon.potter@amd.com
266213570Sbrandon.potter@amd.comtemplate <class OS>
266313570Sbrandon.potter@amd.comSyscallReturn
266413570Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
266513570Sbrandon.potter@amd.com{
266613570Sbrandon.potter@amd.com    int index = 0;
266713570Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
266813570Sbrandon.potter@amd.com    Addr buf_ptr = p->getSyscallArg(tc, index);
266913570Sbrandon.potter@amd.com    int nbytes = p->getSyscallArg(tc, index);
267013570Sbrandon.potter@amd.com
267113570Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
267213570Sbrandon.potter@amd.com    if (!hbfdp)
267313570Sbrandon.potter@amd.com        return -EBADF;
267413570Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
267513570Sbrandon.potter@amd.com
267613570Sbrandon.potter@amd.com    struct pollfd pfd;
267713570Sbrandon.potter@amd.com    pfd.fd = sim_fd;
267813570Sbrandon.potter@amd.com    pfd.events = POLLIN | POLLPRI;
267913570Sbrandon.potter@amd.com    if ((poll(&pfd, 1, 0) == 0)
268013570Sbrandon.potter@amd.com        && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK))
268113570Sbrandon.potter@amd.com        return SyscallReturn::retry();
268213570Sbrandon.potter@amd.com
268313570Sbrandon.potter@amd.com    BufferArg buf_arg(buf_ptr, nbytes);
268413570Sbrandon.potter@amd.com    int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes);
268513570Sbrandon.potter@amd.com
268613570Sbrandon.potter@amd.com    if (bytes_read > 0)
268713570Sbrandon.potter@amd.com        buf_arg.copyOut(tc->getMemProxy());
268813570Sbrandon.potter@amd.com
268913570Sbrandon.potter@amd.com    return (bytes_read == -1) ? -errno : bytes_read;
269013570Sbrandon.potter@amd.com}
269113570Sbrandon.potter@amd.com
269213570Sbrandon.potter@amd.comtemplate <class OS>
26931354SN/ASyscallReturn
2694writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2695{
2696    int index = 0;
2697    int tgt_fd = p->getSyscallArg(tc, index);
2698    Addr buf_ptr = p->getSyscallArg(tc, index);
2699    int nbytes = p->getSyscallArg(tc, index);
2700
2701    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
2702    if (!hbfdp)
2703        return -EBADF;
2704    int sim_fd = hbfdp->getSimFD();
2705
2706    BufferArg buf_arg(buf_ptr, nbytes);
2707    buf_arg.copyIn(tc->getMemProxy());
2708
2709    struct pollfd pfd;
2710    pfd.fd = sim_fd;
2711    pfd.events = POLLOUT;
2712
2713    /**
2714     * We don't want to poll on /dev/random. The kernel will not enable the
2715     * file descriptor for writing unless the entropy in the system falls
2716     * below write_wakeup_threshold. This is not guaranteed to happen
2717     * depending on host settings.
2718     */
2719    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp);
2720    if (ffdp && (ffdp->getFileName() != "/dev/random")) {
2721        if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK))
2722            return SyscallReturn::retry();
2723    }
2724
2725    int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes);
2726
2727    if (bytes_written != -1)
2728        fsync(sim_fd);
2729
2730    return (bytes_written == -1) ? -errno : bytes_written;
2731}
2732
2733template <class OS>
2734SyscallReturn
2735wait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2736{
2737    int index = 0;
2738    pid_t pid = p->getSyscallArg(tc, index);
2739    Addr statPtr = p->getSyscallArg(tc, index);
2740    int options = p->getSyscallArg(tc, index);
2741    Addr rusagePtr = p->getSyscallArg(tc, index);
2742
2743    if (rusagePtr)
2744        DPRINTFR(SyscallVerbose,
2745                 "%d: %s: syscall wait4: rusage pointer provided however "
2746                 "functionality not supported. Ignoring rusage pointer.\n",
2747                 curTick(), tc->getCpuPtr()->name());
2748
2749    /**
2750     * Currently, wait4 is only implemented so that it will wait for children
2751     * exit conditions which are denoted by a SIGCHLD signals posted into the
2752     * system signal list. We return no additional information via any of the
2753     * parameters supplied to wait4. If nothing is found in the system signal
2754     * list, we will wait indefinitely for SIGCHLD to post by retrying the
2755     * call.
2756     */
2757    System *sysh = tc->getSystemPtr();
2758    std::list<BasicSignal>::iterator iter;
2759    for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) {
2760        if (iter->receiver == p) {
2761            if (pid < -1) {
2762                if ((iter->sender->pgid() == -pid)
2763                    && (iter->signalValue == OS::TGT_SIGCHLD))
2764                    goto success;
2765            } else if (pid == -1) {
2766                if (iter->signalValue == OS::TGT_SIGCHLD)
2767                    goto success;
2768            } else if (pid == 0) {
2769                if ((iter->sender->pgid() == p->pgid())
2770                    && (iter->signalValue == OS::TGT_SIGCHLD))
2771                    goto success;
2772            } else {
2773                if ((iter->sender->pid() == pid)
2774                    && (iter->signalValue == OS::TGT_SIGCHLD))
2775                    goto success;
2776            }
2777        }
2778    }
2779
2780    return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry();
2781
2782success:
2783    // Set status to EXITED for WIFEXITED evaluations.
2784    const int EXITED = 0;
2785    BufferArg statusBuf(statPtr, sizeof(int));
2786    *(int *)statusBuf.bufferPtr() = EXITED;
2787    statusBuf.copyOut(tc->getMemProxy());
2788
2789    // Return the child PID.
2790    pid_t retval = iter->sender->pid();
2791    sysh->signalList.erase(iter);
2792    return retval;
2793}
2794
2795template <class OS>
2796SyscallReturn
2797acceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
2798{
2799    struct sockaddr sa;
2800    socklen_t addrLen;
2801    int host_fd;
2802    int index = 0;
2803    int tgt_fd = p->getSyscallArg(tc, index);
2804    Addr addrPtr = p->getSyscallArg(tc, index);
2805    Addr lenPtr = p->getSyscallArg(tc, index);
2806
2807    BufferArg *lenBufPtr = nullptr;
2808    BufferArg *addrBufPtr = nullptr;
2809
2810    auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]);
2811    if (!sfdp)
2812        return -EBADF;
2813    int sim_fd = sfdp->getSimFD();
2814
2815    /**
2816     * We poll the socket file descriptor first to guarantee that we do not
2817     * block on our accept call. The socket can be opened without the
2818     * non-blocking flag (it blocks). This will cause deadlocks between
2819     * communicating processes.
2820     */
2821    struct pollfd pfd;
2822    pfd.fd = sim_fd;
2823    pfd.events = POLLIN | POLLPRI;
2824    if ((poll(&pfd, 1, 0) == 0)
2825        && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK))
2826        return SyscallReturn::retry();
2827
2828    if (lenPtr) {
2829        lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t));
2830        lenBufPtr->copyIn(tc->getMemProxy());
2831        memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(),
2832               sizeof(socklen_t));
2833    }
2834
2835    if (addrPtr) {
2836        addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr));
2837        addrBufPtr->copyIn(tc->getMemProxy());
2838        memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(),
2839               sizeof(struct sockaddr));
2840    }
2841
2842    host_fd = accept(sim_fd, &sa, &addrLen);
2843
2844    if (host_fd == -1)
2845        return -errno;
2846
2847    if (addrPtr) {
2848        memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa));
2849        addrBufPtr->copyOut(tc->getMemProxy());
2850        delete(addrBufPtr);
2851    }
2852
2853    if (lenPtr) {
2854        *(socklen_t *)lenBufPtr->bufferPtr() = addrLen;
2855        lenBufPtr->copyOut(tc->getMemProxy());
2856        delete(lenBufPtr);
2857    }
2858
2859    auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain,
2860                                                sfdp->_type, sfdp->_protocol);
2861    return p->fds->allocFD(afdp);
2862}
2863
2864#endif // __SIM_SYSCALL_EMUL_HH__
2865