syscall_emul.hh revision 13569
1360SN/A/*
210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited
310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc.
410027SChris.Adeniyi-Jones@arm.com * All rights reserved
510027SChris.Adeniyi-Jones@arm.com *
610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1410027SChris.Adeniyi-Jones@arm.com *
151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
16360SN/A * All rights reserved.
17360SN/A *
18360SN/A * Redistribution and use in source and binary forms, with or without
19360SN/A * modification, are permitted provided that the following conditions are
20360SN/A * met: redistributions of source code must retain the above copyright
21360SN/A * notice, this list of conditions and the following disclaimer;
22360SN/A * redistributions in binary form must reproduce the above copyright
23360SN/A * notice, this list of conditions and the following disclaimer in the
24360SN/A * documentation and/or other materials provided with the distribution;
25360SN/A * neither the name of the copyright holders nor the names of its
26360SN/A * contributors may be used to endorse or promote products derived from
27360SN/A * this software without specific prior written permission.
28360SN/A *
29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
422665Ssaidi@eecs.umich.edu *          Kevin Lim
43360SN/A */
44360SN/A
451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
461354SN/A#define __SIM_SYSCALL_EMUL_HH__
47360SN/A
4812018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
4912018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__CYGWIN__) ||     \
5012018Sandreas.sandberg@arm.com     defined(__NetBSD__))
5112018Sandreas.sandberg@arm.com#define NO_STAT64 1
5212018Sandreas.sandberg@arm.com#else
5312018Sandreas.sandberg@arm.com#define NO_STAT64 0
5412018Sandreas.sandberg@arm.com#endif
552064SN/A
5612018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
5712018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__NetBSD__))
5812018Sandreas.sandberg@arm.com#define NO_STATFS 1
5912018Sandreas.sandberg@arm.com#else
6012018Sandreas.sandberg@arm.com#define NO_STATFS 0
6112018Sandreas.sandberg@arm.com#endif
6211799Sbrandon.potter@amd.com
6312018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) ||      \
6412018Sandreas.sandberg@arm.com     defined(__FreeBSD__) || defined(__NetBSD__))
6512018Sandreas.sandberg@arm.com#define NO_FALLOCATE 1
6612018Sandreas.sandberg@arm.com#else
6712018Sandreas.sandberg@arm.com#define NO_FALLOCATE 0
6812018Sandreas.sandberg@arm.com#endif
6911799Sbrandon.potter@amd.com
70360SN/A///
71360SN/A/// @file syscall_emul.hh
72360SN/A///
73360SN/A/// This file defines objects used to emulate syscalls from the target
74360SN/A/// application on the host machine.
75360SN/A
761809SN/A#ifdef __CYGWIN32__
7711800Sbrandon.potter@amd.com#include <sys/fcntl.h>
7811392Sbrandon.potter@amd.com
791809SN/A#endif
8011392Sbrandon.potter@amd.com#include <fcntl.h>
8111383Sbrandon.potter@amd.com#include <sys/mman.h>
8213568Sbrandon.potter@amd.com#include <sys/socket.h>
833113Sgblack@eecs.umich.edu#include <sys/stat.h>
8411799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
8511759Sbrandon.potter@amd.com#include <sys/statfs.h>
8611812Sbaz21@cam.ac.uk#else
8711812Sbaz21@cam.ac.uk#include <sys/mount.h>
8811799Sbrandon.potter@amd.com#endif
898229Snate@binkert.org#include <sys/time.h>
908229Snate@binkert.org#include <sys/uio.h>
9111594Santhony.gutierrez@amd.com#include <unistd.h>
927075Snate@binkert.org
938229Snate@binkert.org#include <cerrno>
9411856Sbrandon.potter@amd.com#include <memory>
957075Snate@binkert.org#include <string>
96360SN/A
9712461Sgabeblack@google.com#include "arch/generic/tlb.hh"
9811886Sbrandon.potter@amd.com#include "arch/utility.hh"
9911800Sbrandon.potter@amd.com#include "base/intmath.hh"
10011392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
10112334Sgabeblack@google.com#include "base/logging.hh"
1021354SN/A#include "base/trace.hh"
1036216Snate@binkert.org#include "base/types.hh"
1046658Snate@binkert.org#include "config/the_isa.hh"
1052474SN/A#include "cpu/base.hh"
1062680Sktlim@umich.edu#include "cpu/thread_context.hh"
1078229Snate@binkert.org#include "mem/page_table.hh"
10811886Sbrandon.potter@amd.com#include "params/Process.hh"
10910496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
11011911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1118229Snate@binkert.org#include "sim/process.hh"
11211794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
11311886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
11410497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
11511794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
116360SN/A
117360SN/A//////////////////////////////////////////////////////////////////////
118360SN/A//
119360SN/A// The following emulation functions are generic enough that they
120360SN/A// don't need to be recompiled for different emulated OS's.  They are
121360SN/A// defined in sim/syscall_emul.cc.
122360SN/A//
123360SN/A//////////////////////////////////////////////////////////////////////
124360SN/A
125360SN/A
126378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1271706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
12811851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
129378SN/A
130378SN/A/// Handler for unimplemented syscalls that we never intend to
131378SN/A/// implement (signal handling, etc.) and should not affect the correct
132378SN/A/// behavior of the program.  Print a warning only if the appropriate
133378SN/A/// trace flag is enabled.  Return success to the target program.
1341706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
13511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
136360SN/A
13711760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
13811760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
13911851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
14011760Sbrandon.potter@amd.com
1416109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1421706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14311851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
144378SN/A
1456109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1466109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
14711851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1486109Ssanchezd@stanford.edu
14911886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
15011886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
15111886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15211886Sbrandon.potter@amd.com
153378SN/A/// Target getpagesize() handler.
1541706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
15511851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
156378SN/A
1575748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1585748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
15911851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
160378SN/A
161378SN/A/// Target close() handler.
1621706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16311851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
164378SN/A
16511886Sbrandon.potter@amd.com// Target read() handler.
1661706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
16711851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
168378SN/A
169378SN/A/// Target write() handler.
1701706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
17111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
172378SN/A
173378SN/A/// Target lseek() handler.
1741706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
17511851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
176378SN/A
1774118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1784118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17911851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1804118Sgblack@eecs.umich.edu
181378SN/A/// Target munmap() handler.
1821706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
18311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
184378SN/A
18513568Sbrandon.potter@amd.com/// Target shutdown() handler.
18613568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num,
18713568Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
18813568Sbrandon.potter@amd.com
189378SN/A/// Target gethostname() handler.
1901706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
19111851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
192360SN/A
1935513SMichael.Adler@intel.com/// Target getcwd() handler.
1945513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
19511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1965513SMichael.Adler@intel.com
19710203SAli.Saidi@ARM.com/// Target readlink() handler.
19810203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19911851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20010203SAli.Saidi@ARM.com                           int index = 0);
2015513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
20211851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2035513SMichael.Adler@intel.com
204511SN/A/// Target unlink() handler.
20510633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20611851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20710633Smichaelupton@gmail.com                           int index);
2081706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
210511SN/A
21112795Smattdsinclair@gmail.com/// Target link() handler
21212795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p,
21312795Smattdsinclair@gmail.com                       ThreadContext *tc);
21412795Smattdsinclair@gmail.com
21512796Smattdsinclair@gmail.com/// Target symlink() handler.
21612796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p,
21712796Smattdsinclair@gmail.com                          ThreadContext *tc);
21812796Smattdsinclair@gmail.com
2195513SMichael.Adler@intel.com/// Target mkdir() handler.
2205513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
22111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2225513SMichael.Adler@intel.com
22313031Sbrandon.potter@amd.com/// Target mknod() handler.
22413031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num,
22513031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
22613031Sbrandon.potter@amd.com
22713031Sbrandon.potter@amd.com/// Target chdir() handler.
22813031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num,
22913031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
23013031Sbrandon.potter@amd.com
23113031Sbrandon.potter@amd.com// Target rmdir() handler.
23213031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num,
23313031Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
23413031Sbrandon.potter@amd.com
235511SN/A/// Target rename() handler.
2361706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
23711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2381706SN/A
2391706SN/A
2401706SN/A/// Target truncate() handler.
2411706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
24211851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2431706SN/A
2441706SN/A
2451706SN/A/// Target ftruncate() handler.
2461706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
24711851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2481706SN/A
249511SN/A
2506703Svince@csl.cornell.edu/// Target truncate64() handler.
2516703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
25211851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2536703Svince@csl.cornell.edu
2546685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2556685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
25611851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2576685Stjones1@inf.ed.ac.uk
2586685Stjones1@inf.ed.ac.uk
2595513SMichael.Adler@intel.com/// Target umask() handler.
2605513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
26111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2625513SMichael.Adler@intel.com
26311885Sbrandon.potter@amd.com/// Target gettid() handler.
26411885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
26511885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2665513SMichael.Adler@intel.com
2671999SN/A/// Target chown() handler.
2681999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
26911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2701999SN/A
27111885Sbrandon.potter@amd.com/// Target setpgid() handler.
27211885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
27311885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2741999SN/A
2751999SN/A/// Target fchown() handler.
2761999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
27711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2781999SN/A
2793079Sstever@eecs.umich.edu/// Target dup() handler.
2803079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
28111851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2823079Sstever@eecs.umich.edu
28311908SBrandon.Potter@amd.com/// Target dup2() handler.
28411908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
28511908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
28611908SBrandon.Potter@amd.com
28711875Sbrandon.potter@amd.com/// Target fcntl() handler.
2882093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
28911851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2902093SN/A
2912687Sksewell@umich.edu/// Target fcntl64() handler.
2922687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
29311851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2942687Sksewell@umich.edu
2952238SN/A/// Target setuid() handler.
2962238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
29711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2982238SN/A
29911908SBrandon.Potter@amd.com/// Target pipe() handler.
30011908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
30111908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
30211908SBrandon.Potter@amd.com
30311908SBrandon.Potter@amd.com/// Internal pipe() handler.
30411908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
30511908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
30611908SBrandon.Potter@amd.com
3072238SN/A/// Target getpid() handler.
3082238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
30911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3102238SN/A
31113568Sbrandon.potter@amd.com// Target bind() handler.
31213568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num,
31313568Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
31413568Sbrandon.potter@amd.com
31513568Sbrandon.potter@amd.com// Target listen() handler.
31613568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num,
31713568Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
31813568Sbrandon.potter@amd.com
31913568Sbrandon.potter@amd.com// Target connect() handler.
32013568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num,
32113568Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
32213568Sbrandon.potter@amd.com
32313448Sciro.santilli@arm.com#if defined(SYS_getdents)
32413031Sbrandon.potter@amd.com// Target getdents() handler.
32513031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num,
32613031Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
32713448Sciro.santilli@arm.com#endif
32813031Sbrandon.potter@amd.com
32913539Sjavier.setoain@arm.com#if defined(SYS_getdents64)
33013539Sjavier.setoain@arm.com// Target getdents() handler.
33113539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num,
33213539Sjavier.setoain@arm.com                           Process *p, ThreadContext *tc);
33313539Sjavier.setoain@arm.com#endif
33413539Sjavier.setoain@arm.com
33513569Sbrandon.potter@amd.com// Target sendto() handler.
33613569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num,
33713569Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
33813569Sbrandon.potter@amd.com
33913569Sbrandon.potter@amd.com// Target recvfrom() handler.
34013569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num,
34113569Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
34213569Sbrandon.potter@amd.com
34313569Sbrandon.potter@amd.com// Target recvmsg() handler.
34413569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num,
34513569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
34613569Sbrandon.potter@amd.com
34713569Sbrandon.potter@amd.com// Target sendmsg() handler.
34813569Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num,
34913569Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
35013569Sbrandon.potter@amd.com
35113031Sbrandon.potter@amd.com// Target getuid() handler.
3522238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
35311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3542238SN/A
3552238SN/A/// Target getgid() handler.
3562238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
35711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
3582238SN/A
3592238SN/A/// Target getppid() handler.
3602238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
36111851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3622238SN/A
3632238SN/A/// Target geteuid() handler.
3642238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
36511851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3662238SN/A
3672238SN/A/// Target getegid() handler.
3682238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
36911851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3702238SN/A
3719455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3729455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
37410203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
37511851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
37611851Sbrandon.potter@amd.com                         int index);
3779455Smitch.hayenga+gem5@gmail.com
3789112Smarc.orr@gmail.com/// Futex system call
37911906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
38011906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3819112Smarc.orr@gmail.comtemplate <class OS>
3829112Smarc.orr@gmail.comSyscallReturn
38311851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3849112Smarc.orr@gmail.com          ThreadContext *tc)
3859112Smarc.orr@gmail.com{
38611911SBrandon.Potter@amd.com    using namespace std;
3879112Smarc.orr@gmail.com
38811911SBrandon.Potter@amd.com    int index = 0;
38911911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
39011911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
39111911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
3929112Smarc.orr@gmail.com
39311911SBrandon.Potter@amd.com    /*
39411911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
39511911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
39611911SBrandon.Potter@amd.com     */
3979238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3989112Smarc.orr@gmail.com
39911911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
4009112Smarc.orr@gmail.com
40111911SBrandon.Potter@amd.com    if (OS::TGT_FUTEX_WAIT == op) {
40211911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
40311911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
40411911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
40511911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
4069112Smarc.orr@gmail.com
40711911SBrandon.Potter@amd.com        /*
40811911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
40911911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
41011911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
41111911SBrandon.Potter@amd.com         */
41211911SBrandon.Potter@amd.com        if (val != mem_val)
4139112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
4149112Smarc.orr@gmail.com
41511911SBrandon.Potter@amd.com        futex_map.suspend(uaddr, process->tgid(), tc);
41611911SBrandon.Potter@amd.com
4179112Smarc.orr@gmail.com        return 0;
41811911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
41911911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
4209112Smarc.orr@gmail.com    }
4219112Smarc.orr@gmail.com
42211911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
42311911SBrandon.Potter@amd.com    return -ENOSYS;
4249112Smarc.orr@gmail.com}
4259112Smarc.orr@gmail.com
4262238SN/A
4272238SN/A/// Pseudo Funcs  - These functions use a different return convension,
4282238SN/A/// returning a second value in a register other than the normal return register
4292238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
43011851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
4312238SN/A
4322238SN/A/// Target getpidPseudo() handler.
4332238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
43411851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4352238SN/A
4362238SN/A/// Target getuidPseudo() handler.
4372238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
43811851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4392238SN/A
4402238SN/A/// Target getgidPseudo() handler.
4412238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
44211851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
4432238SN/A
4442238SN/A
4451354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
4461354SN/Aconst int one_million = 1000000;
44710796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
44810796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4491354SN/A
4501354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4511354SN/A/// by my reckoning.  We want to keep this a constant (not use the
4521354SN/A/// real-world time) to keep simulations repeatable.
4531354SN/Aconst unsigned seconds_since_epoch = 1000000000;
4541354SN/A
4551354SN/A/// Helper function to convert current elapsed time to seconds and
4561354SN/A/// microseconds.
4571354SN/Atemplate <class T1, class T2>
4581354SN/Avoid
45910796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4601354SN/A{
46110796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4621354SN/A    sec = elapsed_usecs / one_million;
4631354SN/A    usec = elapsed_usecs % one_million;
4641354SN/A}
4651354SN/A
46610796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
46710796Sbrandon.potter@amd.com/// nanoseconds.
46810796Sbrandon.potter@amd.comtemplate <class T1, class T2>
46910796Sbrandon.potter@amd.comvoid
47010796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
47110796Sbrandon.potter@amd.com{
47210796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
47310796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
47410796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
47510796Sbrandon.potter@amd.com}
47610796Sbrandon.potter@amd.com
477360SN/A//////////////////////////////////////////////////////////////////////
478360SN/A//
479360SN/A// The following emulation functions are generic, but need to be
480360SN/A// templated to account for differences in types, constants, etc.
481360SN/A//
482360SN/A//////////////////////////////////////////////////////////////////////
483360SN/A
48411759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4853113Sgblack@eecs.umich.edu#if NO_STAT64
4863113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4873113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4883113Sgblack@eecs.umich.edu#else
4893113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4903113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4913113Sgblack@eecs.umich.edu#endif
4923113Sgblack@eecs.umich.edu
4933113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4943113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4953113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4963113Sgblack@eecs.umich.edu
4973113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
49812032Sandreas.sandberg@arm.comvoid
4993113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
5003113Sgblack@eecs.umich.edu{
5014189Sgblack@eecs.umich.edu    using namespace TheISA;
5024189Sgblack@eecs.umich.edu
5033113Sgblack@eecs.umich.edu    if (fakeTTY)
5043113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
5053113Sgblack@eecs.umich.edu    else
5063113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
5078737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
5083113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
5098737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
5103277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
5115515SMichael.Adler@intel.com    if (fakeTTY) {
5125515SMichael.Adler@intel.com        // Claim to be a character device
5135515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
5145515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
5155515SMichael.Adler@intel.com    }
5168737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
5173277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
5188737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
5193277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
5208737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
5213277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
5228737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
5233113Sgblack@eecs.umich.edu    if (fakeTTY)
5243113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
5253113Sgblack@eecs.umich.edu    else
5263113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
5278737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
5283113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
5298737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
5303114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5318737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
5323114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
5338737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
5343114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
5358737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
53611906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
5374061Sgblack@eecs.umich.edu    // consistently across different hosts.
5384061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5398737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5403113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5418737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5423113Sgblack@eecs.umich.edu}
5433113Sgblack@eecs.umich.edu
5443113Sgblack@eecs.umich.edu// Same for stat64
5453113Sgblack@eecs.umich.edu
5463113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
54712032Sandreas.sandberg@arm.comvoid
5483113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5493113Sgblack@eecs.umich.edu{
5504189Sgblack@eecs.umich.edu    using namespace TheISA;
5514189Sgblack@eecs.umich.edu
5523113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5533113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5543113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
5558737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5563113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5578737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5583113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5598737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5603113Sgblack@eecs.umich.edu#else
5613113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5623113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5633113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5643113Sgblack@eecs.umich.edu#endif
5653113Sgblack@eecs.umich.edu}
5663113Sgblack@eecs.umich.edu
56711906SBrandon.Potter@amd.com// Here are a couple of convenience functions
5683113Sgblack@eecs.umich.edutemplate<class OS>
56912032Sandreas.sandberg@arm.comvoid
5708852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
57111906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
5723113Sgblack@eecs.umich.edu{
5733113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5743113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5753113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5763113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5773113Sgblack@eecs.umich.edu}
5783113Sgblack@eecs.umich.edu
5793113Sgblack@eecs.umich.edutemplate<class OS>
58012032Sandreas.sandberg@arm.comvoid
5818852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
58211906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
5833113Sgblack@eecs.umich.edu{
5843113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5853113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5866686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5873113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5883113Sgblack@eecs.umich.edu}
5893113Sgblack@eecs.umich.edu
59011759Sbrandon.potter@amd.comtemplate <class OS>
59112032Sandreas.sandberg@arm.comvoid
59211759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
59311759Sbrandon.potter@amd.com                 hst_statfs *host)
59411759Sbrandon.potter@amd.com{
59511759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
59611759Sbrandon.potter@amd.com
59711812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
59811812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
59911812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
60011759Sbrandon.potter@amd.com#else
60111812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
60211759Sbrandon.potter@amd.com#endif
60311759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
60411759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
60511759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
60611759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
60711759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
60811759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
60911812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
61011812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
61111812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
61211812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
61311812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
61411812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
61511812Sbaz21@cam.ac.uk#else
61611759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
61711759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
61811812Sbaz21@cam.ac.uk#endif
61911812Sbaz21@cam.ac.uk#if defined(__linux__)
62011759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
62111812Sbaz21@cam.ac.uk#else
62211812Sbaz21@cam.ac.uk    /*
62311812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
62411812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
62511812Sbaz21@cam.ac.uk     */
62611812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
62711812Sbaz21@cam.ac.uk#endif
62811759Sbrandon.potter@amd.com
62911759Sbrandon.potter@amd.com    tgt.copyOut(mem);
63011759Sbrandon.potter@amd.com}
63111759Sbrandon.potter@amd.com
632378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
633378SN/A/// only to find out if their stdout is a tty, to determine whether to
6349141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
6359141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
636360SN/Atemplate <class OS>
6371450SN/ASyscallReturn
63811856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
639360SN/A{
6406701Sgblack@eecs.umich.edu    int index = 0;
64111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
64211856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
643360SN/A
64410930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
645360SN/A
64611856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
64711856Sbrandon.potter@amd.com        return -ENOTTY;
64810496Ssteve.reinhardt@amd.com
64911856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
65011856Sbrandon.potter@amd.com    if (!dfdp)
6511458SN/A        return -EBADF;
652360SN/A
65311856Sbrandon.potter@amd.com    /**
65411856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
65511856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
65611856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
65711856Sbrandon.potter@amd.com     */
65811856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
65911856Sbrandon.potter@amd.com    if (emul_driver)
66011856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
66110496Ssteve.reinhardt@amd.com
66211856Sbrandon.potter@amd.com    /**
66311856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
66411856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
66511856Sbrandon.potter@amd.com     */
66611856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
66710930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
6689141Smarc.orr@gmail.com    return -ENOTTY;
669360SN/A}
670360SN/A
671360SN/Atemplate <class OS>
67211907SBrandon.Potter@amd.comSyscallReturn
67311907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
67411907SBrandon.Potter@amd.com         bool isopenat)
675360SN/A{
67611907SBrandon.Potter@amd.com    int index = 0;
67711907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
67811907SBrandon.Potter@amd.com
67911907SBrandon.Potter@amd.com    /**
68011907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
68111907SBrandon.Potter@amd.com     * descriptor from the simulated process.
68211907SBrandon.Potter@amd.com     */
68311907SBrandon.Potter@amd.com    if (isopenat)
68411907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
68511907SBrandon.Potter@amd.com
68611907SBrandon.Potter@amd.com    /**
68711907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
68811907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
68911907SBrandon.Potter@amd.com     */
690360SN/A    std::string path;
69111907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6921458SN/A        return -EFAULT;
693360SN/A
69411907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
69511907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
69611907SBrandon.Potter@amd.com#else
69711907SBrandon.Potter@amd.com    int host_flags = 0;
69811907SBrandon.Potter@amd.com#endif
69911907SBrandon.Potter@amd.com    /**
70011907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
70111907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
70211907SBrandon.Potter@amd.com     */
70311907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
704360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
70511907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
70611907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
70711907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
708360SN/A        }
709360SN/A    }
71011907SBrandon.Potter@amd.com    if (tgt_flags) {
71111907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
71211907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
71311907SBrandon.Potter@amd.com    }
714360SN/A#ifdef __CYGWIN32__
71511907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
716360SN/A#endif
717360SN/A
71811907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
7193669Sbinkertn@umich.edu
72011907SBrandon.Potter@amd.com    /**
72111907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
72211907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
72311907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
72411907SBrandon.Potter@amd.com     * create a full path.
72511907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
72611907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
72711907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
72811907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
72911907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
73011907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
73111907SBrandon.Potter@amd.com     */
73211907SBrandon.Potter@amd.com    if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) {
73311907SBrandon.Potter@amd.com        path = p->fullPath(path);
73411907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
73511907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
73611907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
73711907SBrandon.Potter@amd.com        if (!ffdp)
73811907SBrandon.Potter@amd.com            return -EBADF;
73913371Sciro.santilli@arm.com        path.insert(0, ffdp->getFileName() + "/");
74011907SBrandon.Potter@amd.com    }
7411706SN/A
74211907SBrandon.Potter@amd.com    /**
74311907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
74411907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
74511907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
74611907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
74711907SBrandon.Potter@amd.com     */
74810496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
74910496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
75011907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
75111907SBrandon.Potter@amd.com        if (drv) {
75211907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
75311907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
75411907SBrandon.Potter@amd.com                            isopenat ? "at" : "", path.c_str());
75511907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
75610496Ssteve.reinhardt@amd.com        }
75711907SBrandon.Potter@amd.com        /**
75811907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
75911907SBrandon.Potter@amd.com         * as /dev/zero
76011907SBrandon.Potter@amd.com         */
76110496Ssteve.reinhardt@amd.com    }
76210496Ssteve.reinhardt@amd.com
76311907SBrandon.Potter@amd.com    /**
76411907SBrandon.Potter@amd.com     * Some special paths and files cannot be called on the host and need
76511907SBrandon.Potter@amd.com     * to be handled as special cases inside the simulator.
76611907SBrandon.Potter@amd.com     * If the full path that was created above does not match any of the
76711907SBrandon.Potter@amd.com     * special cases, pass it through to the open call on the host to let
76811907SBrandon.Potter@amd.com     * the host open the file on our behalf.
76911907SBrandon.Potter@amd.com     * If the host cannot open the file, return the host's error code back
77011907SBrandon.Potter@amd.com     * through the system call to the simulated process.
77111907SBrandon.Potter@amd.com     */
77211907SBrandon.Potter@amd.com    int sim_fd = -1;
77311907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
77411907SBrandon.Potter@amd.com            { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" };
77511907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
77611907SBrandon.Potter@amd.com        if (startswith(path, entry))
77711907SBrandon.Potter@amd.com            sim_fd = OS::openSpecialFile(path, p, tc);
77811907SBrandon.Potter@amd.com    }
77911907SBrandon.Potter@amd.com    if (sim_fd == -1) {
78011907SBrandon.Potter@amd.com        sim_fd = open(path.c_str(), host_flags, mode);
78111907SBrandon.Potter@amd.com    }
78211907SBrandon.Potter@amd.com    if (sim_fd == -1) {
78311907SBrandon.Potter@amd.com        int local = -errno;
78411907SBrandon.Potter@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n",
78511907SBrandon.Potter@amd.com                        isopenat ? "at" : "", path.c_str());
78611907SBrandon.Potter@amd.com        return local;
78711907SBrandon.Potter@amd.com    }
788360SN/A
78911907SBrandon.Potter@amd.com    /**
79011907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
79111907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
79211907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
79311907SBrandon.Potter@amd.com     * file descriptor.
79411907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
79511907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
79611907SBrandon.Potter@amd.com     */
79711907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
79811907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
79911907SBrandon.Potter@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n",
80011907SBrandon.Potter@amd.com                    isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str());
80111907SBrandon.Potter@amd.com    return tgt_fd;
802360SN/A}
803360SN/A
80410027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
80510027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
80610027SChris.Adeniyi-Jones@arm.comSyscallReturn
80711851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
80810027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
80910027SChris.Adeniyi-Jones@arm.com{
81011907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
81110027SChris.Adeniyi-Jones@arm.com}
81210027SChris.Adeniyi-Jones@arm.com
81310027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
81410027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
81510027SChris.Adeniyi-Jones@arm.comSyscallReturn
81611851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
81711851Sbrandon.potter@amd.com           ThreadContext *tc)
81810027SChris.Adeniyi-Jones@arm.com{
81911907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
82010027SChris.Adeniyi-Jones@arm.com}
82110027SChris.Adeniyi-Jones@arm.com
82210633Smichaelupton@gmail.com/// Target unlinkat() handler.
82310633Smichaelupton@gmail.comtemplate <class OS>
82410633Smichaelupton@gmail.comSyscallReturn
82511851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
82610633Smichaelupton@gmail.com             ThreadContext *tc)
82710633Smichaelupton@gmail.com{
82810633Smichaelupton@gmail.com    int index = 0;
82910633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
83010633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
83110633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
83210633Smichaelupton@gmail.com
83310633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
83410633Smichaelupton@gmail.com}
83510633Smichaelupton@gmail.com
83610203SAli.Saidi@ARM.com/// Target facessat() handler
83710203SAli.Saidi@ARM.comtemplate <class OS>
83810203SAli.Saidi@ARM.comSyscallReturn
83911851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
84011851Sbrandon.potter@amd.com              ThreadContext *tc)
84110203SAli.Saidi@ARM.com{
84210203SAli.Saidi@ARM.com    int index = 0;
84310203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
84410203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
84510203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
84610203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
84710203SAli.Saidi@ARM.com}
84810203SAli.Saidi@ARM.com
84910203SAli.Saidi@ARM.com/// Target readlinkat() handler
85010203SAli.Saidi@ARM.comtemplate <class OS>
85110203SAli.Saidi@ARM.comSyscallReturn
85211851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
85311851Sbrandon.potter@amd.com               ThreadContext *tc)
85410203SAli.Saidi@ARM.com{
85510203SAli.Saidi@ARM.com    int index = 0;
85610203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
85710203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
85810203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
85910203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
86010203SAli.Saidi@ARM.com}
86110203SAli.Saidi@ARM.com
86210850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
86310850SGiacomo.Gabrielli@arm.comtemplate <class OS>
86410850SGiacomo.Gabrielli@arm.comSyscallReturn
86511851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
86610850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
86710850SGiacomo.Gabrielli@arm.com{
86810850SGiacomo.Gabrielli@arm.com    int index = 0;
86910850SGiacomo.Gabrielli@arm.com
87010850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
87110850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
87210850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
87310850SGiacomo.Gabrielli@arm.com
87410850SGiacomo.Gabrielli@arm.com    std::string old_name;
87510850SGiacomo.Gabrielli@arm.com
87610850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
87710850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
87810850SGiacomo.Gabrielli@arm.com        return -EFAULT;
87910850SGiacomo.Gabrielli@arm.com
88010850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
88110850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
88210850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
88310850SGiacomo.Gabrielli@arm.com
88410850SGiacomo.Gabrielli@arm.com    std::string new_name;
88510850SGiacomo.Gabrielli@arm.com
88610850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
88710850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
88810850SGiacomo.Gabrielli@arm.com        return -EFAULT;
88910850SGiacomo.Gabrielli@arm.com
89010850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
89110850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
89210850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
89310850SGiacomo.Gabrielli@arm.com
89410850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
89510850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
89610850SGiacomo.Gabrielli@arm.com}
89710850SGiacomo.Gabrielli@arm.com
8986640Svince@csl.cornell.edu/// Target sysinfo() handler.
8996640Svince@csl.cornell.edutemplate <class OS>
9006640Svince@csl.cornell.eduSyscallReturn
90111851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
90211851Sbrandon.potter@amd.com            ThreadContext *tc)
9036640Svince@csl.cornell.edu{
9046640Svince@csl.cornell.edu
9056701Sgblack@eecs.umich.edu    int index = 0;
9066701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
90710793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
9086640Svince@csl.cornell.edu
90911758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
91011758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
91111758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
9126640Svince@csl.cornell.edu
9138706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
9146640Svince@csl.cornell.edu
9156701Sgblack@eecs.umich.edu    return 0;
9166640Svince@csl.cornell.edu}
917360SN/A
9181999SN/A/// Target chmod() handler.
9191999SN/Atemplate <class OS>
9201999SN/ASyscallReturn
92111851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
9222680Sktlim@umich.edu          ThreadContext *tc)
9231999SN/A{
9241999SN/A    std::string path;
9251999SN/A
9266701Sgblack@eecs.umich.edu    int index = 0;
9278852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9286701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9291999SN/A        return -EFAULT;
9306701Sgblack@eecs.umich.edu    }
9311999SN/A
9326701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
9331999SN/A    mode_t hostMode = 0;
9341999SN/A
9351999SN/A    // XXX translate mode flags via OS::something???
9361999SN/A    hostMode = mode;
9371999SN/A
9383669Sbinkertn@umich.edu    // Adjust path for current working directory
9393669Sbinkertn@umich.edu    path = process->fullPath(path);
9403669Sbinkertn@umich.edu
9411999SN/A    // do the chmod
9421999SN/A    int result = chmod(path.c_str(), hostMode);
9431999SN/A    if (result < 0)
9442218SN/A        return -errno;
9451999SN/A
9461999SN/A    return 0;
9471999SN/A}
9481999SN/A
9491999SN/A
9501999SN/A/// Target fchmod() handler.
9511999SN/Atemplate <class OS>
9521999SN/ASyscallReturn
95311856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
9541999SN/A{
9556701Sgblack@eecs.umich.edu    int index = 0;
95611856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
95711856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
95810931Sbrandon.potter@amd.com
95911856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
96011856Sbrandon.potter@amd.com    if (!ffdp)
9611999SN/A        return -EBADF;
96211856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
9631999SN/A
96411856Sbrandon.potter@amd.com    mode_t hostMode = mode;
9651999SN/A
96611856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
9671999SN/A
96811856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
9691999SN/A}
9701999SN/A
9715877Shsul@eecs.umich.edu/// Target mremap() handler.
9725877Shsul@eecs.umich.edutemplate <class OS>
9735877Shsul@eecs.umich.eduSyscallReturn
97411851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
9755877Shsul@eecs.umich.edu{
9766701Sgblack@eecs.umich.edu    int index = 0;
9776701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
9786701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
9796701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
9806701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
98110027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
98210027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
98310027SChris.Adeniyi-Jones@arm.com
98410027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
98510027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
9865877Shsul@eecs.umich.edu
98710318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
98810318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
9895877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
9905877Shsul@eecs.umich.edu        return -EINVAL;
9915877Shsul@eecs.umich.edu    }
9925877Shsul@eecs.umich.edu
99310486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
99410486Stjablin@gmail.com
9955877Shsul@eecs.umich.edu    if (new_length > old_length) {
99611905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
99711905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
99811905SBrandon.Potter@amd.com
99911905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
100010027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
100112206Srico.amslinger@informatik.uni-augsburg.de            // This case cannot occur when growing downward, as
100212206Srico.amslinger@informatik.uni-augsburg.de            // start is greater than or equal to mmap_end.
10035877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
100411905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
100511905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
10065877Shsul@eecs.umich.edu            return start;
10075877Shsul@eecs.umich.edu        } else {
100810027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
10095877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
10105877Shsul@eecs.umich.edu                return -ENOMEM;
10115877Shsul@eecs.umich.edu            } else {
101212206Srico.amslinger@informatik.uni-augsburg.de                uint64_t new_start = provided_address;
101312206Srico.amslinger@informatik.uni-augsburg.de                if (!use_provided_address) {
101412206Srico.amslinger@informatik.uni-augsburg.de                    new_start = process->mmapGrowsDown() ?
101512206Srico.amslinger@informatik.uni-augsburg.de                                mmap_end - new_length : mmap_end;
101612206Srico.amslinger@informatik.uni-augsburg.de                    mmap_end = process->mmapGrowsDown() ?
101712206Srico.amslinger@informatik.uni-augsburg.de                               new_start : mmap_end + new_length;
101812206Srico.amslinger@informatik.uni-augsburg.de                    mem_state->setMmapEnd(mmap_end);
101912206Srico.amslinger@informatik.uni-augsburg.de                }
102012206Srico.amslinger@informatik.uni-augsburg.de
102110027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
102210027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
102310027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
102410027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
10255877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
102610027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
102710027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
102810027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
102910027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
103012206Srico.amslinger@informatik.uni-augsburg.de                    ((new_start + new_length > mem_state->getMmapEnd() &&
103112206Srico.amslinger@informatik.uni-augsburg.de                      !process->mmapGrowsDown()) ||
103212206Srico.amslinger@informatik.uni-augsburg.de                    (new_start < mem_state->getMmapEnd() &&
103312206Srico.amslinger@informatik.uni-augsburg.de                      process->mmapGrowsDown()))) {
103410027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
103510027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
103610027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
103710027SChris.Adeniyi-Jones@arm.com                }
103810027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
103910027SChris.Adeniyi-Jones@arm.com                return new_start;
10405877Shsul@eecs.umich.edu            }
10415877Shsul@eecs.umich.edu        }
10425877Shsul@eecs.umich.edu    } else {
104310027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
104410027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
10458601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
104610027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
10475877Shsul@eecs.umich.edu    }
10485877Shsul@eecs.umich.edu}
10491999SN/A
1050378SN/A/// Target stat() handler.
1051360SN/Atemplate <class OS>
10521450SN/ASyscallReturn
105311851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
10542680Sktlim@umich.edu         ThreadContext *tc)
1055360SN/A{
1056360SN/A    std::string path;
1057360SN/A
10586701Sgblack@eecs.umich.edu    int index = 0;
10598852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10606701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10616701Sgblack@eecs.umich.edu        return -EFAULT;
10626701Sgblack@eecs.umich.edu    }
10636701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1064360SN/A
10653669Sbinkertn@umich.edu    // Adjust path for current working directory
10663669Sbinkertn@umich.edu    path = process->fullPath(path);
10673669Sbinkertn@umich.edu
1068360SN/A    struct stat hostBuf;
1069360SN/A    int result = stat(path.c_str(), &hostBuf);
1070360SN/A
1071360SN/A    if (result < 0)
10722218SN/A        return -errno;
1073360SN/A
10748706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1075360SN/A
10761458SN/A    return 0;
1077360SN/A}
1078360SN/A
1079360SN/A
10805074Ssaidi@eecs.umich.edu/// Target stat64() handler.
10815074Ssaidi@eecs.umich.edutemplate <class OS>
10825074Ssaidi@eecs.umich.eduSyscallReturn
108311851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
10845074Ssaidi@eecs.umich.edu           ThreadContext *tc)
10855074Ssaidi@eecs.umich.edu{
10865074Ssaidi@eecs.umich.edu    std::string path;
10875074Ssaidi@eecs.umich.edu
10886701Sgblack@eecs.umich.edu    int index = 0;
10898852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10906701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
10915074Ssaidi@eecs.umich.edu        return -EFAULT;
10926701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10935074Ssaidi@eecs.umich.edu
10945074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
10955074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
10965074Ssaidi@eecs.umich.edu
10975208Ssaidi@eecs.umich.edu#if NO_STAT64
10985208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
10995208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
11005208Ssaidi@eecs.umich.edu#else
11015074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
11025074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
11035208Ssaidi@eecs.umich.edu#endif
11045074Ssaidi@eecs.umich.edu
11055074Ssaidi@eecs.umich.edu    if (result < 0)
11065074Ssaidi@eecs.umich.edu        return -errno;
11075074Ssaidi@eecs.umich.edu
11088706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11095074Ssaidi@eecs.umich.edu
11105074Ssaidi@eecs.umich.edu    return 0;
11115074Ssaidi@eecs.umich.edu}
11125074Ssaidi@eecs.umich.edu
11135074Ssaidi@eecs.umich.edu
111410027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
111510027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
111610027SChris.Adeniyi-Jones@arm.comSyscallReturn
111711851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
111810027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
111910027SChris.Adeniyi-Jones@arm.com{
112010027SChris.Adeniyi-Jones@arm.com    int index = 0;
112110027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
112210027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
112310793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
112410027SChris.Adeniyi-Jones@arm.com
112510027SChris.Adeniyi-Jones@arm.com    std::string path;
112610027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
112710027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
112810027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
112910027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
113010027SChris.Adeniyi-Jones@arm.com
113110027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
113210027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
113310027SChris.Adeniyi-Jones@arm.com
113410027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
113510027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
113610027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
113710027SChris.Adeniyi-Jones@arm.com#else
113810027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
113910027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
114010027SChris.Adeniyi-Jones@arm.com#endif
114110027SChris.Adeniyi-Jones@arm.com
114210027SChris.Adeniyi-Jones@arm.com    if (result < 0)
114310027SChris.Adeniyi-Jones@arm.com        return -errno;
114410027SChris.Adeniyi-Jones@arm.com
114510027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
114610027SChris.Adeniyi-Jones@arm.com
114710027SChris.Adeniyi-Jones@arm.com    return 0;
114810027SChris.Adeniyi-Jones@arm.com}
114910027SChris.Adeniyi-Jones@arm.com
115010027SChris.Adeniyi-Jones@arm.com
11511999SN/A/// Target fstat64() handler.
11521999SN/Atemplate <class OS>
11531999SN/ASyscallReturn
115411856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
11551999SN/A{
11566701Sgblack@eecs.umich.edu    int index = 0;
115711856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
115811856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
115910931Sbrandon.potter@amd.com
116011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
116111856Sbrandon.potter@amd.com    if (!ffdp)
11621999SN/A        return -EBADF;
116311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
11641999SN/A
11652764Sstever@eecs.umich.edu#if NO_STAT64
11662064SN/A    struct stat  hostBuf;
116710931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
11682064SN/A#else
11692064SN/A    struct stat64  hostBuf;
117010931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
11712064SN/A#endif
11721999SN/A
11731999SN/A    if (result < 0)
11742218SN/A        return -errno;
11751999SN/A
117610931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11771999SN/A
11781999SN/A    return 0;
11791999SN/A}
11801999SN/A
11811999SN/A
1182378SN/A/// Target lstat() handler.
1183360SN/Atemplate <class OS>
11841450SN/ASyscallReturn
118511851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
11862680Sktlim@umich.edu          ThreadContext *tc)
1187360SN/A{
1188360SN/A    std::string path;
1189360SN/A
11906701Sgblack@eecs.umich.edu    int index = 0;
11918852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11926701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11936701Sgblack@eecs.umich.edu        return -EFAULT;
11946701Sgblack@eecs.umich.edu    }
11956701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1196360SN/A
11973669Sbinkertn@umich.edu    // Adjust path for current working directory
11983669Sbinkertn@umich.edu    path = process->fullPath(path);
11993669Sbinkertn@umich.edu
1200360SN/A    struct stat hostBuf;
1201360SN/A    int result = lstat(path.c_str(), &hostBuf);
1202360SN/A
1203360SN/A    if (result < 0)
12041458SN/A        return -errno;
1205360SN/A
12068706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1207360SN/A
12081458SN/A    return 0;
1209360SN/A}
1210360SN/A
12111999SN/A/// Target lstat64() handler.
12121999SN/Atemplate <class OS>
12131999SN/ASyscallReturn
121411851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
12152680Sktlim@umich.edu            ThreadContext *tc)
12161999SN/A{
12171999SN/A    std::string path;
12181999SN/A
12196701Sgblack@eecs.umich.edu    int index = 0;
12208852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12216701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12226701Sgblack@eecs.umich.edu        return -EFAULT;
12236701Sgblack@eecs.umich.edu    }
12246701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12251999SN/A
12263669Sbinkertn@umich.edu    // Adjust path for current working directory
12273669Sbinkertn@umich.edu    path = process->fullPath(path);
12283669Sbinkertn@umich.edu
12292764Sstever@eecs.umich.edu#if NO_STAT64
12302064SN/A    struct stat hostBuf;
12312064SN/A    int result = lstat(path.c_str(), &hostBuf);
12322064SN/A#else
12331999SN/A    struct stat64 hostBuf;
12341999SN/A    int result = lstat64(path.c_str(), &hostBuf);
12352064SN/A#endif
12361999SN/A
12371999SN/A    if (result < 0)
12381999SN/A        return -errno;
12391999SN/A
12408706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
12411999SN/A
12421999SN/A    return 0;
12431999SN/A}
12441999SN/A
1245378SN/A/// Target fstat() handler.
1246360SN/Atemplate <class OS>
12471450SN/ASyscallReturn
124811856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1249360SN/A{
12506701Sgblack@eecs.umich.edu    int index = 0;
125111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
125211856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1253360SN/A
125411380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1255360SN/A
125611856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
125711856Sbrandon.potter@amd.com    if (!ffdp)
12581458SN/A        return -EBADF;
125911856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1260360SN/A
1261360SN/A    struct stat hostBuf;
126210931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1263360SN/A
1264360SN/A    if (result < 0)
12651458SN/A        return -errno;
1266360SN/A
126710931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
12682021SN/A
12691458SN/A    return 0;
1270360SN/A}
1271360SN/A
1272360SN/A
12731706SN/A/// Target statfs() handler.
12741706SN/Atemplate <class OS>
12751706SN/ASyscallReturn
127611851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
12772680Sktlim@umich.edu           ThreadContext *tc)
12781706SN/A{
127911799Sbrandon.potter@amd.com#if NO_STATFS
128011799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
128111799Sbrandon.potter@amd.com#else
12821706SN/A    std::string path;
12831706SN/A
12846701Sgblack@eecs.umich.edu    int index = 0;
12858852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12866701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12876701Sgblack@eecs.umich.edu        return -EFAULT;
12886701Sgblack@eecs.umich.edu    }
12896701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12901706SN/A
12913669Sbinkertn@umich.edu    // Adjust path for current working directory
12923669Sbinkertn@umich.edu    path = process->fullPath(path);
12933669Sbinkertn@umich.edu
12941706SN/A    struct statfs hostBuf;
12951706SN/A    int result = statfs(path.c_str(), &hostBuf);
12961706SN/A
12971706SN/A    if (result < 0)
12982218SN/A        return -errno;
12991706SN/A
130011759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
130111799Sbrandon.potter@amd.com#endif
13021706SN/A    return 0;
13031706SN/A}
13041706SN/A
130511886Sbrandon.potter@amd.comtemplate <class OS>
130611886Sbrandon.potter@amd.comSyscallReturn
130711886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
130811886Sbrandon.potter@amd.com{
130911886Sbrandon.potter@amd.com    int index = 0;
131012426Sqtt2@cornell.edu
131113557Sgabeblack@google.com    RegVal flags = p->getSyscallArg(tc, index);
131213557Sgabeblack@google.com    RegVal newStack = p->getSyscallArg(tc, index);
131311886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
131412426Sqtt2@cornell.edu
131513534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA
131612426Sqtt2@cornell.edu    /**
131713534Sandreas.sandberg@arm.com     * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm.
131812426Sqtt2@cornell.edu     * The flag defines the list of clone() arguments in the following
131912426Sqtt2@cornell.edu     * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr
132012426Sqtt2@cornell.edu     */
132113536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
132212426Sqtt2@cornell.edu    Addr ctidPtr = p->getSyscallArg(tc, index);
132312426Sqtt2@cornell.edu#else
132411886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
132513536Sandreas.sandberg@arm.com    Addr tlsPtr = p->getSyscallArg(tc, index);
132612426Sqtt2@cornell.edu#endif
132711886Sbrandon.potter@amd.com
132811886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
132911886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
133011886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
133111886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
133211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
133311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
133411886Sbrandon.potter@amd.com        return -EINVAL;
133511886Sbrandon.potter@amd.com
133611886Sbrandon.potter@amd.com    ThreadContext *ctc;
133711886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
133811886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
133911886Sbrandon.potter@amd.com
134011886Sbrandon.potter@amd.com    /**
134111886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
134211886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
134311886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
134411886Sbrandon.potter@amd.com     * constructor.
134511886Sbrandon.potter@amd.com     */
134611886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
134711886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
134811886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
134911886Sbrandon.potter@amd.com    pp->system = p->system;
135011886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
135111886Sbrandon.potter@amd.com    pp->input.assign("stdin");
135211886Sbrandon.potter@amd.com    pp->output.assign("stdout");
135311886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
135411886Sbrandon.potter@amd.com    pp->uid = p->uid();
135511886Sbrandon.potter@amd.com    pp->euid = p->euid();
135611886Sbrandon.potter@amd.com    pp->gid = p->gid();
135711886Sbrandon.potter@amd.com    pp->egid = p->egid();
135811886Sbrandon.potter@amd.com
135911886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
136011886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
136111886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
136211886Sbrandon.potter@amd.com    do {
136311886Sbrandon.potter@amd.com        temp_pid++;
136411886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
136511886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
136611886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
136711886Sbrandon.potter@amd.com
136811886Sbrandon.potter@amd.com    pp->pid = temp_pid;
136911886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
137011886Sbrandon.potter@amd.com    Process *cp = pp->create();
137111886Sbrandon.potter@amd.com    delete pp;
137211886Sbrandon.potter@amd.com
137311886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
137411886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
137511886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
137611886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
137711886Sbrandon.potter@amd.com
137811886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
137911886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
138011886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
138111886Sbrandon.potter@amd.com        *ptid = cp->pid();
138211886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
138311886Sbrandon.potter@amd.com    }
138411886Sbrandon.potter@amd.com
138511886Sbrandon.potter@amd.com    cp->initState();
138611886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
138711886Sbrandon.potter@amd.com
138811911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
138911911SBrandon.Potter@amd.com        delete cp->sigchld;
139011911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
139111911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
139211911SBrandon.Potter@amd.com        *cp->sigchld = true;
139311911SBrandon.Potter@amd.com    }
139411911SBrandon.Potter@amd.com
139511886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
139611886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
139711886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
139811886Sbrandon.potter@amd.com        *ctid = cp->pid();
139911886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
140011886Sbrandon.potter@amd.com    }
140111886Sbrandon.potter@amd.com
140211886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
140311886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
140411886Sbrandon.potter@amd.com
140511886Sbrandon.potter@amd.com    ctc->clearArchRegs();
140611886Sbrandon.potter@amd.com
140713536Sandreas.sandberg@arm.com    OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr);
140811886Sbrandon.potter@amd.com
140911886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
141011886Sbrandon.potter@amd.com
141111886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
141211886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
141311886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
141411886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
141511886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
141611886Sbrandon.potter@amd.com#endif
141711886Sbrandon.potter@amd.com
141813535Sandreas.sandberg@arm.com    TheISA::PCState cpc = tc->pcState();
141913535Sandreas.sandberg@arm.com    cpc.advance();
142013535Sandreas.sandberg@arm.com    ctc->pcState(cpc);
142111886Sbrandon.potter@amd.com    ctc->activate();
142211886Sbrandon.potter@amd.com
142311886Sbrandon.potter@amd.com    return cp->pid();
142411886Sbrandon.potter@amd.com}
14251706SN/A
14261706SN/A/// Target fstatfs() handler.
14271706SN/Atemplate <class OS>
14281706SN/ASyscallReturn
142911856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14301706SN/A{
14316701Sgblack@eecs.umich.edu    int index = 0;
143211856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
143311856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
14341706SN/A
143511856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
143611856Sbrandon.potter@amd.com    if (!ffdp)
14371706SN/A        return -EBADF;
143811856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
14391706SN/A
14401706SN/A    struct statfs hostBuf;
144110931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
14421706SN/A
14431706SN/A    if (result < 0)
14442218SN/A        return -errno;
14451706SN/A
144611759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
14471706SN/A
14481706SN/A    return 0;
14491706SN/A}
14501706SN/A
14511706SN/A
14521999SN/A/// Target writev() handler.
14531999SN/Atemplate <class OS>
14541999SN/ASyscallReturn
145511856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
14561999SN/A{
14576701Sgblack@eecs.umich.edu    int index = 0;
145811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
145910931Sbrandon.potter@amd.com
146011856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
146111856Sbrandon.potter@amd.com    if (!hbfdp)
14621999SN/A        return -EBADF;
146311856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
14641999SN/A
146511856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
146611856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
146711856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
14681999SN/A    struct iovec hiov[count];
14696227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
14701999SN/A        typename OS::tgt_iovec tiov;
14712461SN/A
147211856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
147311856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
14748737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
14751999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
147611856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
147711856Sbrandon.potter@amd.com                      hiov[i].iov_len);
14781999SN/A    }
14791999SN/A
148010931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
14811999SN/A
14826227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
14831999SN/A        delete [] (char *)hiov[i].iov_base;
14841999SN/A
14851999SN/A    if (result < 0)
14862218SN/A        return -errno;
14871999SN/A
148810629Sjthestness@gmail.com    return result;
14891999SN/A}
14901999SN/A
149111385Sbrandon.potter@amd.com/// Real mmap handler.
1492360SN/Atemplate <class OS>
14931450SN/ASyscallReturn
149411851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
149511385Sbrandon.potter@amd.com         bool is_mmap2)
1496360SN/A{
14976701Sgblack@eecs.umich.edu    int index = 0;
14986701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
14996701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
150011383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
150111383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
15028324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
150310486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1504360SN/A
150511385Sbrandon.potter@amd.com    if (is_mmap2)
150611385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
15079008Sgblack@eecs.umich.edu
150811383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
150911383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
151011383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
151111383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
151211383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
151311383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
151411383Sbrandon.potter@amd.com        !length) {
151511383Sbrandon.potter@amd.com        return -EINVAL;
151611383Sbrandon.potter@amd.com    }
15178324Ssteve.reinhardt@amd.com
151811383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
151911383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
152011383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
152111383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
152211383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
152311383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
152411383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
152511383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
152611383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
152711383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
152811383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
152911383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
153011383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
153111383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
153211383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
153311383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
153411383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
153511383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
153611383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
153711383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
153811383Sbrandon.potter@amd.com        // than we currently maintain.
153911383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
154011383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
154111383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
15428324Ssteve.reinhardt@amd.com    }
15435877Shsul@eecs.umich.edu
154410486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
154510486Stjablin@gmail.com
154611383Sbrandon.potter@amd.com    int sim_fd = -1;
154711383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
154811383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
154911856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
155011624Smichael.lebeane@amd.com
155111856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
155211856Sbrandon.potter@amd.com        if (dfdp) {
155311856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
155411856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
155511624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
155611624Smichael.lebeane@amd.com        }
155711624Smichael.lebeane@amd.com
155811856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
155911856Sbrandon.potter@amd.com        if (!ffdp)
156011383Sbrandon.potter@amd.com            return -EBADF;
156111856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1562360SN/A
156311913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
156411383Sbrandon.potter@amd.com                                    sim_fd, offset);
15658600Ssteve.reinhardt@amd.com
156611383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
156711383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
156811383Sbrandon.potter@amd.com            return -errno;
15698600Ssteve.reinhardt@amd.com        }
15702544SN/A    }
15712544SN/A
157211383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
157311383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
157411383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
157511905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
157611905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
157711905SBrandon.Potter@amd.com
157811905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
157911905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
158011905SBrandon.Potter@amd.com
158111905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
158211383Sbrandon.potter@amd.com    }
158311383Sbrandon.potter@amd.com
158411383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
158511383Sbrandon.potter@amd.com                    start, start + length - 1);
158611383Sbrandon.potter@amd.com
158711383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
158811383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
158911383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
159011383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
159111383Sbrandon.potter@amd.com    if (clobber) {
159211383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
159311383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
159411383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
159511383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
159611383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
15978600Ssteve.reinhardt@amd.com        }
15986672Sgblack@eecs.umich.edu    }
15998600Ssteve.reinhardt@amd.com
160011383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
160111383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
160211383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
16038601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
16042544SN/A
160511383Sbrandon.potter@amd.com    // Transfer content into target address space.
160611383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
160711383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
160811383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
160911383Sbrandon.potter@amd.com        // with something like:
161011383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
161111383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
161211383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
161311383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
161411383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
161511383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
161611383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
161711383Sbrandon.potter@amd.com    } else {
161811383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
161911383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
162011383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
162111383Sbrandon.potter@amd.com        // the host into the target address space.
162211383Sbrandon.potter@amd.com        struct stat file_stat;
162311383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
162411383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
162511383Sbrandon.potter@amd.com
162611383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
162711383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
162811383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
162911383Sbrandon.potter@amd.com        // on the specified offset into the file.
163011383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
163111383Sbrandon.potter@amd.com                                 length);
163211383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
163311383Sbrandon.potter@amd.com
163411383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
163511383Sbrandon.potter@amd.com        munmap(pmap, length);
163611383Sbrandon.potter@amd.com
163711392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
163811392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
163911392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
164011392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
164111392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
164211392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
164311392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
164411392Sbrandon.potter@amd.com        if (interpreter) {
164511392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
164611392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
164711392Sbrandon.potter@amd.com
164811392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
164911392Sbrandon.potter@amd.com
165011392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
165111856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
165211856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
165311856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
165411392Sbrandon.potter@amd.com
165511392Sbrandon.potter@amd.com                if (lib) {
165611392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
165711392Sbrandon.potter@amd.com                                        lib->textBase(), start);
165811392Sbrandon.potter@amd.com                }
165911392Sbrandon.potter@amd.com            }
166011392Sbrandon.potter@amd.com        }
166111392Sbrandon.potter@amd.com
166211383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
166311383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
166411383Sbrandon.potter@amd.com        // execution (hopefully).
166511383Sbrandon.potter@amd.com    }
166611383Sbrandon.potter@amd.com
16671458SN/A    return start;
1668360SN/A}
1669360SN/A
167011593Santhony.gutierrez@amd.comtemplate <class OS>
167111593Santhony.gutierrez@amd.comSyscallReturn
167211851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
167311593Santhony.gutierrez@amd.com{
167411593Santhony.gutierrez@amd.com    int index = 0;
167511593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
167611593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
167711593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
167811593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
167911593Santhony.gutierrez@amd.com
168011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
168111856Sbrandon.potter@amd.com    if (!ffdp)
168211593Santhony.gutierrez@amd.com        return -EBADF;
168311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
168411593Santhony.gutierrez@amd.com
168511593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
168611593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
168711593Santhony.gutierrez@amd.com
168811594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
168911593Santhony.gutierrez@amd.com
169011593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
169111593Santhony.gutierrez@amd.com}
169211593Santhony.gutierrez@amd.com
169311385Sbrandon.potter@amd.com/// Target mmap() handler.
169411385Sbrandon.potter@amd.comtemplate <class OS>
169511385Sbrandon.potter@amd.comSyscallReturn
169611851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
169711385Sbrandon.potter@amd.com{
169811385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
169911385Sbrandon.potter@amd.com}
170011385Sbrandon.potter@amd.com
170111385Sbrandon.potter@amd.com/// Target mmap2() handler.
170211385Sbrandon.potter@amd.comtemplate <class OS>
170311385Sbrandon.potter@amd.comSyscallReturn
170411851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
170511385Sbrandon.potter@amd.com{
170611385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
170711385Sbrandon.potter@amd.com}
170811385Sbrandon.potter@amd.com
1709378SN/A/// Target getrlimit() handler.
1710360SN/Atemplate <class OS>
17111450SN/ASyscallReturn
171211851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
171311851Sbrandon.potter@amd.com              ThreadContext *tc)
1714360SN/A{
17156701Sgblack@eecs.umich.edu    int index = 0;
17166701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
17176701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1718360SN/A
1719360SN/A    switch (resource) {
172011906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
172111906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
172211906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
172311906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
172411906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
172511906SBrandon.Potter@amd.com        break;
1726360SN/A
172711906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
172811906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
172911906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
173011906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
173111906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
173211906SBrandon.Potter@amd.com        break;
17335877Shsul@eecs.umich.edu
173411906SBrandon.Potter@amd.com      default:
173511906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
173611906SBrandon.Potter@amd.com        return -EINVAL;
173711906SBrandon.Potter@amd.com        break;
1738360SN/A    }
1739360SN/A
17408706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
17411458SN/A    return 0;
1742360SN/A}
1743360SN/A
174412235Sar4jc@virginia.edutemplate <class OS>
174512235Sar4jc@virginia.eduSyscallReturn
174612235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process,
174712235Sar4jc@virginia.edu            ThreadContext *tc)
174812235Sar4jc@virginia.edu{
174912235Sar4jc@virginia.edu    int index = 0;
175012235Sar4jc@virginia.edu    if (process->getSyscallArg(tc, index) != 0)
175112235Sar4jc@virginia.edu    {
175212235Sar4jc@virginia.edu        warn("prlimit: ignoring rlimits for nonzero pid");
175312235Sar4jc@virginia.edu        return -EPERM;
175412235Sar4jc@virginia.edu    }
175512235Sar4jc@virginia.edu    int resource = process->getSyscallArg(tc, index);
175612235Sar4jc@virginia.edu    Addr n = process->getSyscallArg(tc, index);
175712235Sar4jc@virginia.edu    if (n != 0)
175812235Sar4jc@virginia.edu        warn("prlimit: ignoring new rlimit");
175912235Sar4jc@virginia.edu    Addr o = process->getSyscallArg(tc, index);
176012235Sar4jc@virginia.edu    if (o != 0)
176112235Sar4jc@virginia.edu    {
176212416Sqtt2@cornell.edu        TypedBufferArg<typename OS::rlimit> rlp(o);
176312235Sar4jc@virginia.edu        switch (resource) {
176412235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_STACK:
176512235Sar4jc@virginia.edu            // max stack size in bytes: make up a number (8MB for now)
176612235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
176712235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
176812235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
176912235Sar4jc@virginia.edu            break;
177012235Sar4jc@virginia.edu          case OS::TGT_RLIMIT_DATA:
177112235Sar4jc@virginia.edu            // max data segment size in bytes: make up a number
177212235Sar4jc@virginia.edu            rlp->rlim_cur = rlp->rlim_max = 256*1024*1024;
177312235Sar4jc@virginia.edu            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
177412235Sar4jc@virginia.edu            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
177512593Sjason@lowepower.com            break;
177612235Sar4jc@virginia.edu          default:
177712235Sar4jc@virginia.edu            warn("prlimit: unimplemented resource %d", resource);
177812235Sar4jc@virginia.edu            return -EINVAL;
177912235Sar4jc@virginia.edu            break;
178012235Sar4jc@virginia.edu        }
178112235Sar4jc@virginia.edu        rlp.copyOut(tc->getMemProxy());
178212235Sar4jc@virginia.edu    }
178312235Sar4jc@virginia.edu    return 0;
178412235Sar4jc@virginia.edu}
178512235Sar4jc@virginia.edu
178610796Sbrandon.potter@amd.com/// Target clock_gettime() function.
178710796Sbrandon.potter@amd.comtemplate <class OS>
178810796Sbrandon.potter@amd.comSyscallReturn
178911851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
179010796Sbrandon.potter@amd.com{
179110796Sbrandon.potter@amd.com    int index = 1;
179210796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
179310796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
179410796Sbrandon.potter@amd.com
179510796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
179610796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
179710796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
179810796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
179910796Sbrandon.potter@amd.com
180010796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
180110796Sbrandon.potter@amd.com
180210796Sbrandon.potter@amd.com    return 0;
180310796Sbrandon.potter@amd.com}
180410796Sbrandon.potter@amd.com
180511337SMichael.Lebeane@amd.com/// Target clock_getres() function.
180611337SMichael.Lebeane@amd.comtemplate <class OS>
180711337SMichael.Lebeane@amd.comSyscallReturn
180811851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
180911337SMichael.Lebeane@amd.com{
181011337SMichael.Lebeane@amd.com    int index = 1;
181111337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
181211337SMichael.Lebeane@amd.com
181311337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
181411337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
181511337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
181611337SMichael.Lebeane@amd.com
181711337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
181811337SMichael.Lebeane@amd.com
181911337SMichael.Lebeane@amd.com    return 0;
182011337SMichael.Lebeane@amd.com}
182111337SMichael.Lebeane@amd.com
1822378SN/A/// Target gettimeofday() handler.
1823360SN/Atemplate <class OS>
18241450SN/ASyscallReturn
182511851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
182611851Sbrandon.potter@amd.com                 ThreadContext *tc)
1827360SN/A{
18286701Sgblack@eecs.umich.edu    int index = 0;
18296701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1830360SN/A
183110796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1832360SN/A    tp->tv_sec += seconds_since_epoch;
18336109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
18346109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1835360SN/A
18368706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1837360SN/A
18381458SN/A    return 0;
1839360SN/A}
1840360SN/A
1841360SN/A
18421999SN/A/// Target utimes() handler.
18431999SN/Atemplate <class OS>
18441999SN/ASyscallReturn
184511851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
18462680Sktlim@umich.edu           ThreadContext *tc)
18471999SN/A{
18481999SN/A    std::string path;
18491999SN/A
18506701Sgblack@eecs.umich.edu    int index = 0;
18518852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
18526701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
18536701Sgblack@eecs.umich.edu        return -EFAULT;
18546701Sgblack@eecs.umich.edu    }
18551999SN/A
18566701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
18576701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
18588706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
18591999SN/A
18601999SN/A    struct timeval hostTimeval[2];
186111906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
18628737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
18638737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
18641999SN/A    }
18653669Sbinkertn@umich.edu
18663669Sbinkertn@umich.edu    // Adjust path for current working directory
18673669Sbinkertn@umich.edu    path = process->fullPath(path);
18683669Sbinkertn@umich.edu
18691999SN/A    int result = utimes(path.c_str(), hostTimeval);
18701999SN/A
18711999SN/A    if (result < 0)
18721999SN/A        return -errno;
18731999SN/A
18741999SN/A    return 0;
18751999SN/A}
187611886Sbrandon.potter@amd.com
187711886Sbrandon.potter@amd.comtemplate <class OS>
187811886Sbrandon.potter@amd.comSyscallReturn
187911886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
188011886Sbrandon.potter@amd.com{
188111886Sbrandon.potter@amd.com    desc->setFlags(0);
188211886Sbrandon.potter@amd.com
188311886Sbrandon.potter@amd.com    int index = 0;
188411886Sbrandon.potter@amd.com    std::string path;
188511886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
188611886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
188711886Sbrandon.potter@amd.com        return -EFAULT;
188811886Sbrandon.potter@amd.com
188911886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
189011886Sbrandon.potter@amd.com        return -EACCES;
189111886Sbrandon.potter@amd.com
189211886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
189311886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
189411886Sbrandon.potter@amd.com                      Addr mem_loc)
189511886Sbrandon.potter@amd.com    {
189611886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
189711886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
189811886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
189911886Sbrandon.potter@amd.com
190011886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
190111886Sbrandon.potter@amd.com                break;
190211886Sbrandon.potter@amd.com
190311886Sbrandon.potter@amd.com            vect.push_back(std::string());
190411886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
190511886Sbrandon.potter@amd.com        }
190611886Sbrandon.potter@amd.com    };
190711886Sbrandon.potter@amd.com
190811886Sbrandon.potter@amd.com    /**
190911886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
191011886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
191111886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
191211886Sbrandon.potter@amd.com     * constructor.
191311886Sbrandon.potter@amd.com     */
191411886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
191511886Sbrandon.potter@amd.com    pp->executable = path;
191611886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
191711886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
191811886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
191911886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
192011886Sbrandon.potter@amd.com    pp->uid = p->uid();
192111886Sbrandon.potter@amd.com    pp->egid = p->egid();
192211886Sbrandon.potter@amd.com    pp->euid = p->euid();
192311886Sbrandon.potter@amd.com    pp->gid = p->gid();
192411886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
192511886Sbrandon.potter@amd.com    pp->pid = p->pid();
192611886Sbrandon.potter@amd.com    pp->input.assign("cin");
192711886Sbrandon.potter@amd.com    pp->output.assign("cout");
192811886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
192911886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
193011886Sbrandon.potter@amd.com    pp->system = p->system;
193111886Sbrandon.potter@amd.com    /**
193211886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
193311886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
193411886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
193511886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
193611886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
193711886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
193811886Sbrandon.potter@amd.com     */
193911886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
194011886Sbrandon.potter@amd.com    Process *new_p = pp->create();
194111886Sbrandon.potter@amd.com    delete pp;
194211886Sbrandon.potter@amd.com
194311886Sbrandon.potter@amd.com    /**
194411886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
194511886Sbrandon.potter@amd.com     * close-on-exec.
194611886Sbrandon.potter@amd.com     */
194711886Sbrandon.potter@amd.com    new_p->fds = p->fds;
194811886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
194911886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
195011886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
195111886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
195211886Sbrandon.potter@amd.com    }
195311886Sbrandon.potter@amd.com
195411886Sbrandon.potter@amd.com    *new_p->sigchld = true;
195511886Sbrandon.potter@amd.com
195611886Sbrandon.potter@amd.com    delete p;
195711886Sbrandon.potter@amd.com    tc->clearArchRegs();
195811886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
195911886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
196011886Sbrandon.potter@amd.com    new_p->initState();
196111886Sbrandon.potter@amd.com    tc->activate();
196211886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
196311886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
196411886Sbrandon.potter@amd.com
196511886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
196611886Sbrandon.potter@amd.com    return 0;
196711886Sbrandon.potter@amd.com}
196811886Sbrandon.potter@amd.com
1969378SN/A/// Target getrusage() function.
1970360SN/Atemplate <class OS>
19711450SN/ASyscallReturn
197211851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
19732680Sktlim@umich.edu              ThreadContext *tc)
1974360SN/A{
19756701Sgblack@eecs.umich.edu    int index = 0;
19766701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
19776701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1978360SN/A
19793670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
19803670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1981360SN/A    rup->ru_stime.tv_sec = 0;
1982360SN/A    rup->ru_stime.tv_usec = 0;
1983360SN/A    rup->ru_maxrss = 0;
1984360SN/A    rup->ru_ixrss = 0;
1985360SN/A    rup->ru_idrss = 0;
1986360SN/A    rup->ru_isrss = 0;
1987360SN/A    rup->ru_minflt = 0;
1988360SN/A    rup->ru_majflt = 0;
1989360SN/A    rup->ru_nswap = 0;
1990360SN/A    rup->ru_inblock = 0;
1991360SN/A    rup->ru_oublock = 0;
1992360SN/A    rup->ru_msgsnd = 0;
1993360SN/A    rup->ru_msgrcv = 0;
1994360SN/A    rup->ru_nsignals = 0;
1995360SN/A    rup->ru_nvcsw = 0;
1996360SN/A    rup->ru_nivcsw = 0;
1997360SN/A
19983670Sbinkertn@umich.edu    switch (who) {
19993670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
200010796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
20018737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
20028737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
20033670Sbinkertn@umich.edu        break;
20043670Sbinkertn@umich.edu
20053670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
20063670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
20073670Sbinkertn@umich.edu        break;
20083670Sbinkertn@umich.edu
20093670Sbinkertn@umich.edu      default:
20103670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
20113670Sbinkertn@umich.edu        // plow ahead
20123670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
20133670Sbinkertn@umich.edu             who);
20143670Sbinkertn@umich.edu    }
20153670Sbinkertn@umich.edu
20168706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
2017360SN/A
20181458SN/A    return 0;
2019360SN/A}
2020360SN/A
20216683Stjones1@inf.ed.ac.uk/// Target times() function.
20226683Stjones1@inf.ed.ac.uktemplate <class OS>
20236683Stjones1@inf.ed.ac.ukSyscallReturn
202411851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
202511851Sbrandon.potter@amd.com          ThreadContext *tc)
20266683Stjones1@inf.ed.ac.uk{
20276701Sgblack@eecs.umich.edu    int index = 0;
20286701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
20296683Stjones1@inf.ed.ac.uk
20306683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
20317823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
20326683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
20336683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
20346683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
20356683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
20366683Stjones1@inf.ed.ac.uk
20376683Stjones1@inf.ed.ac.uk    // Convert to host endianness
20388737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
20396683Stjones1@inf.ed.ac.uk
20406683Stjones1@inf.ed.ac.uk    // Write back
20418706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
20426683Stjones1@inf.ed.ac.uk
20436683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
20446683Stjones1@inf.ed.ac.uk    return clocks;
20456683Stjones1@inf.ed.ac.uk}
20462553SN/A
20476684Stjones1@inf.ed.ac.uk/// Target time() function.
20486684Stjones1@inf.ed.ac.uktemplate <class OS>
20496684Stjones1@inf.ed.ac.ukSyscallReturn
205011851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
20516684Stjones1@inf.ed.ac.uk{
20526684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
205310796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
20546684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
20556684Stjones1@inf.ed.ac.uk
20566701Sgblack@eecs.umich.edu    int index = 0;
20576701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
205811321Ssteve.reinhardt@amd.com    if (taddr != 0) {
20596684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
20608737Skoansin.tan@gmail.com        t = TheISA::htog(t);
20618852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
20628852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
20636684Stjones1@inf.ed.ac.uk    }
20646684Stjones1@inf.ed.ac.uk    return sec;
20656684Stjones1@inf.ed.ac.uk}
20662553SN/A
206711910SBrandon.Potter@amd.comtemplate <class OS>
206811910SBrandon.Potter@amd.comSyscallReturn
206911910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
207011910SBrandon.Potter@amd.com{
207111910SBrandon.Potter@amd.com    int index = 0;
207211910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
207311910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
207411910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
207511910SBrandon.Potter@amd.com
207611910SBrandon.Potter@amd.com    /**
207711910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
207811910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
207911910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
208011910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
208111910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
208211910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
208311910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
208411910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
208511910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
208611910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
208711910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
208811910SBrandon.Potter@amd.com     * due to PID rollover.
208911910SBrandon.Potter@amd.com     */
209011910SBrandon.Potter@amd.com
209111910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
209211910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
209311910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
209411910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
209511910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
209611910SBrandon.Potter@amd.com            tgt_proc = temp;
209711910SBrandon.Potter@amd.com            break;
209811910SBrandon.Potter@amd.com        }
209911910SBrandon.Potter@amd.com    }
210011910SBrandon.Potter@amd.com
210111910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
210211910SBrandon.Potter@amd.com        return -EINVAL;
210311910SBrandon.Potter@amd.com
210411910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
210511910SBrandon.Potter@amd.com        return -ESRCH;
210611910SBrandon.Potter@amd.com
210711910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
210811910SBrandon.Potter@amd.com        return -ESRCH;
210911910SBrandon.Potter@amd.com
211011910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
211111910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
211211910SBrandon.Potter@amd.com
211311910SBrandon.Potter@amd.com    return 0;
211411910SBrandon.Potter@amd.com}
211511910SBrandon.Potter@amd.com
211613568Sbrandon.potter@amd.comtemplate <class OS>
211713568Sbrandon.potter@amd.comSyscallReturn
211813568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
211913568Sbrandon.potter@amd.com{
212013568Sbrandon.potter@amd.com    int index = 0;
212113568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
212213568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
212313568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
212413568Sbrandon.potter@amd.com
212513568Sbrandon.potter@amd.com    int sim_fd = socket(domain, type, prot);
212613568Sbrandon.potter@amd.com    if (sim_fd == -1)
212713568Sbrandon.potter@amd.com        return -errno;
212813568Sbrandon.potter@amd.com
212913568Sbrandon.potter@amd.com    auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot);
213013568Sbrandon.potter@amd.com    int tgt_fd = p->fds->allocFD(sfdp);
213113568Sbrandon.potter@amd.com
213213568Sbrandon.potter@amd.com    return tgt_fd;
213313568Sbrandon.potter@amd.com}
213413568Sbrandon.potter@amd.com
213513568Sbrandon.potter@amd.comtemplate <class OS>
213613568Sbrandon.potter@amd.comSyscallReturn
213713568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
213813568Sbrandon.potter@amd.com{
213913568Sbrandon.potter@amd.com    int index = 0;
214013568Sbrandon.potter@amd.com    int domain = p->getSyscallArg(tc, index);
214113568Sbrandon.potter@amd.com    int type = p->getSyscallArg(tc, index);
214213568Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
214313568Sbrandon.potter@amd.com    Addr svPtr = p->getSyscallArg(tc, index);
214413568Sbrandon.potter@amd.com
214513568Sbrandon.potter@amd.com    BufferArg svBuf((Addr)svPtr, 2 * sizeof(int));
214613568Sbrandon.potter@amd.com    int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr());
214713568Sbrandon.potter@amd.com    if (status == -1)
214813568Sbrandon.potter@amd.com        return -errno;
214913568Sbrandon.potter@amd.com
215013568Sbrandon.potter@amd.com    int *fds = (int *)svBuf.bufferPtr();
215113568Sbrandon.potter@amd.com
215213568Sbrandon.potter@amd.com    auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot);
215313568Sbrandon.potter@amd.com    fds[0] = p->fds->allocFD(sfdp1);
215413568Sbrandon.potter@amd.com    auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot);
215513568Sbrandon.potter@amd.com    fds[1] = p->fds->allocFD(sfdp2);
215613568Sbrandon.potter@amd.com    svBuf.copyOut(tc->getMemProxy());
215713568Sbrandon.potter@amd.com
215813568Sbrandon.potter@amd.com    return status;
215913568Sbrandon.potter@amd.com}
21602553SN/A
21611354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2162