syscall_emul.hh revision 12032
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>
823113Sgblack@eecs.umich.edu#include <sys/stat.h>
8311799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
8411759Sbrandon.potter@amd.com#include <sys/statfs.h>
8511812Sbaz21@cam.ac.uk#else
8611812Sbaz21@cam.ac.uk#include <sys/mount.h>
8711799Sbrandon.potter@amd.com#endif
888229Snate@binkert.org#include <sys/time.h>
898229Snate@binkert.org#include <sys/uio.h>
9011594Santhony.gutierrez@amd.com#include <unistd.h>
917075Snate@binkert.org
928229Snate@binkert.org#include <cerrno>
9311856Sbrandon.potter@amd.com#include <memory>
947075Snate@binkert.org#include <string>
95360SN/A
9611886Sbrandon.potter@amd.com#include "arch/utility.hh"
9711800Sbrandon.potter@amd.com#include "base/intmath.hh"
9811392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
992462SN/A#include "base/misc.hh"
1001354SN/A#include "base/trace.hh"
1016216Snate@binkert.org#include "base/types.hh"
1026658Snate@binkert.org#include "config/the_isa.hh"
1032474SN/A#include "cpu/base.hh"
1042680Sktlim@umich.edu#include "cpu/thread_context.hh"
1058229Snate@binkert.org#include "mem/page_table.hh"
10611886Sbrandon.potter@amd.com#include "params/Process.hh"
10710496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
10811911SBrandon.Potter@amd.com#include "sim/futex_map.hh"
1098229Snate@binkert.org#include "sim/process.hh"
11011794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
11111886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
11210497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
11311794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
114360SN/A
115360SN/A//////////////////////////////////////////////////////////////////////
116360SN/A//
117360SN/A// The following emulation functions are generic enough that they
118360SN/A// don't need to be recompiled for different emulated OS's.  They are
119360SN/A// defined in sim/syscall_emul.cc.
120360SN/A//
121360SN/A//////////////////////////////////////////////////////////////////////
122360SN/A
123360SN/A
124378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1251706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
12611851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
127378SN/A
128378SN/A/// Handler for unimplemented syscalls that we never intend to
129378SN/A/// implement (signal handling, etc.) and should not affect the correct
130378SN/A/// behavior of the program.  Print a warning only if the appropriate
131378SN/A/// trace flag is enabled.  Return success to the target program.
1321706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
13311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
134360SN/A
13511760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
13611760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
13711851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
13811760Sbrandon.potter@amd.com
1396109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1401706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
14111851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
142378SN/A
1436109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1446109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
14511851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1466109Ssanchezd@stanford.edu
14711886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
14811886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
14911886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
15011886Sbrandon.potter@amd.com
151378SN/A/// Target getpagesize() handler.
1521706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
15311851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
154378SN/A
1555748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1565748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
15711851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
158378SN/A
159378SN/A/// Target close() handler.
1601706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
16111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
162378SN/A
16311886Sbrandon.potter@amd.com// Target read() handler.
1641706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
16511851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
166378SN/A
167378SN/A/// Target write() handler.
1681706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
16911851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
170378SN/A
171378SN/A/// Target lseek() handler.
1721706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
17311851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
174378SN/A
1754118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1764118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
17711851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1784118Sgblack@eecs.umich.edu
179378SN/A/// Target munmap() handler.
1801706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
18111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
182378SN/A
183378SN/A/// Target gethostname() handler.
1841706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
18511851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
186360SN/A
1875513SMichael.Adler@intel.com/// Target getcwd() handler.
1885513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
18911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1905513SMichael.Adler@intel.com
19110203SAli.Saidi@ARM.com/// Target readlink() handler.
19210203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19311851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
19410203SAli.Saidi@ARM.com                           int index = 0);
1955513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
19611851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
1975513SMichael.Adler@intel.com
198511SN/A/// Target unlink() handler.
19910633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
20011851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
20110633Smichaelupton@gmail.com                           int index);
2021706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
20311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
204511SN/A
2055513SMichael.Adler@intel.com/// Target mkdir() handler.
2065513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
20711851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2085513SMichael.Adler@intel.com
209511SN/A/// Target rename() handler.
2101706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
21111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2121706SN/A
2131706SN/A
2141706SN/A/// Target truncate() handler.
2151706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
21611851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2171706SN/A
2181706SN/A
2191706SN/A/// Target ftruncate() handler.
2201706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
22111851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2221706SN/A
223511SN/A
2246703Svince@csl.cornell.edu/// Target truncate64() handler.
2256703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
22611851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2276703Svince@csl.cornell.edu
2286685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2296685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
23011851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2316685Stjones1@inf.ed.ac.uk
2326685Stjones1@inf.ed.ac.uk
2335513SMichael.Adler@intel.com/// Target umask() handler.
2345513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
23511851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2365513SMichael.Adler@intel.com
23711885Sbrandon.potter@amd.com/// Target gettid() handler.
23811885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
23911885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2405513SMichael.Adler@intel.com
2411999SN/A/// Target chown() handler.
2421999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
24311851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2441999SN/A
24511885Sbrandon.potter@amd.com/// Target setpgid() handler.
24611885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
24711885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2481999SN/A
2491999SN/A/// Target fchown() handler.
2501999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
25111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2521999SN/A
2533079Sstever@eecs.umich.edu/// Target dup() handler.
2543079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
25511851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2563079Sstever@eecs.umich.edu
25711908SBrandon.Potter@amd.com/// Target dup2() handler.
25811908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num,
25911908SBrandon.Potter@amd.com                       Process *process, ThreadContext *tc);
26011908SBrandon.Potter@amd.com
26111875Sbrandon.potter@amd.com/// Target fcntl() handler.
2622093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
26311851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2642093SN/A
2652687Sksewell@umich.edu/// Target fcntl64() handler.
2662687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
26711851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2682687Sksewell@umich.edu
2692238SN/A/// Target setuid() handler.
2702238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
27111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2722238SN/A
27311908SBrandon.Potter@amd.com/// Target pipe() handler.
27411908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num,
27511908SBrandon.Potter@amd.com                       Process *p, ThreadContext *tc);
27611908SBrandon.Potter@amd.com
27711908SBrandon.Potter@amd.com/// Internal pipe() handler.
27811908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p,
27911908SBrandon.Potter@amd.com                       ThreadContext *tc, bool pseudoPipe);
28011908SBrandon.Potter@amd.com
2812238SN/A/// Target getpid() handler.
2822238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
28311851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2842238SN/A
2852238SN/A/// Target getuid() handler.
2862238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
28711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2882238SN/A
2892238SN/A/// Target getgid() handler.
2902238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
29111851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2922238SN/A
2932238SN/A/// Target getppid() handler.
2942238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
29511851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2962238SN/A
2972238SN/A/// Target geteuid() handler.
2982238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
29911851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3002238SN/A
3012238SN/A/// Target getegid() handler.
3022238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
30311851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
3042238SN/A
3059455Smitch.hayenga+gem5@gmail.com/// Target access() handler
3069455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
30711851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
30810203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
30911851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
31011851Sbrandon.potter@amd.com                         int index);
3119455Smitch.hayenga+gem5@gmail.com
3129112Smarc.orr@gmail.com/// Futex system call
31311906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
31411906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
3159112Smarc.orr@gmail.comtemplate <class OS>
3169112Smarc.orr@gmail.comSyscallReturn
31711851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
3189112Smarc.orr@gmail.com          ThreadContext *tc)
3199112Smarc.orr@gmail.com{
32011911SBrandon.Potter@amd.com    using namespace std;
3219112Smarc.orr@gmail.com
32211911SBrandon.Potter@amd.com    int index = 0;
32311911SBrandon.Potter@amd.com    Addr uaddr = process->getSyscallArg(tc, index);
32411911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index);
32511911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index);
3269112Smarc.orr@gmail.com
32711911SBrandon.Potter@amd.com    /*
32811911SBrandon.Potter@amd.com     * Unsupported option that does not affect the correctness of the
32911911SBrandon.Potter@amd.com     * application. This is a performance optimization utilized by Linux.
33011911SBrandon.Potter@amd.com     */
3319238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3329112Smarc.orr@gmail.com
33311911SBrandon.Potter@amd.com    FutexMap &futex_map = tc->getSystemPtr()->futexMap;
3349112Smarc.orr@gmail.com
33511911SBrandon.Potter@amd.com    if (OS::TGT_FUTEX_WAIT == op) {
33611911SBrandon.Potter@amd.com        // Ensure futex system call accessed atomically.
33711911SBrandon.Potter@amd.com        BufferArg buf(uaddr, sizeof(int));
33811911SBrandon.Potter@amd.com        buf.copyIn(tc->getMemProxy());
33911911SBrandon.Potter@amd.com        int mem_val = *(int*)buf.bufferPtr();
3409112Smarc.orr@gmail.com
34111911SBrandon.Potter@amd.com        /*
34211911SBrandon.Potter@amd.com         * The value in memory at uaddr is not equal with the expected val
34311911SBrandon.Potter@amd.com         * (a different thread must have changed it before the system call was
34411911SBrandon.Potter@amd.com         * invoked). In this case, we need to throw an error.
34511911SBrandon.Potter@amd.com         */
34611911SBrandon.Potter@amd.com        if (val != mem_val)
3479112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3489112Smarc.orr@gmail.com
34911911SBrandon.Potter@amd.com        futex_map.suspend(uaddr, process->tgid(), tc);
35011911SBrandon.Potter@amd.com
3519112Smarc.orr@gmail.com        return 0;
35211911SBrandon.Potter@amd.com    } else if (OS::TGT_FUTEX_WAKE == op) {
35311911SBrandon.Potter@amd.com        return futex_map.wakeup(uaddr, process->tgid(), val);
3549112Smarc.orr@gmail.com    }
3559112Smarc.orr@gmail.com
35611911SBrandon.Potter@amd.com    warn("futex: op %d not implemented; ignoring.", op);
35711911SBrandon.Potter@amd.com    return -ENOSYS;
3589112Smarc.orr@gmail.com}
3599112Smarc.orr@gmail.com
3602238SN/A
3612238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3622238SN/A/// returning a second value in a register other than the normal return register
3632238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
36411851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
3652238SN/A
3662238SN/A/// Target getpidPseudo() handler.
3672238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
36811851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3692238SN/A
3702238SN/A/// Target getuidPseudo() handler.
3712238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
37211851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3732238SN/A
3742238SN/A/// Target getgidPseudo() handler.
3752238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
37611851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3772238SN/A
3782238SN/A
3791354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3801354SN/Aconst int one_million = 1000000;
38110796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
38210796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
3831354SN/A
3841354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3851354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3861354SN/A/// real-world time) to keep simulations repeatable.
3871354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3881354SN/A
3891354SN/A/// Helper function to convert current elapsed time to seconds and
3901354SN/A/// microseconds.
3911354SN/Atemplate <class T1, class T2>
3921354SN/Avoid
39310796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
3941354SN/A{
39510796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
3961354SN/A    sec = elapsed_usecs / one_million;
3971354SN/A    usec = elapsed_usecs % one_million;
3981354SN/A}
3991354SN/A
40010796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
40110796Sbrandon.potter@amd.com/// nanoseconds.
40210796Sbrandon.potter@amd.comtemplate <class T1, class T2>
40310796Sbrandon.potter@amd.comvoid
40410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
40510796Sbrandon.potter@amd.com{
40610796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
40710796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
40810796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
40910796Sbrandon.potter@amd.com}
41010796Sbrandon.potter@amd.com
411360SN/A//////////////////////////////////////////////////////////////////////
412360SN/A//
413360SN/A// The following emulation functions are generic, but need to be
414360SN/A// templated to account for differences in types, constants, etc.
415360SN/A//
416360SN/A//////////////////////////////////////////////////////////////////////
417360SN/A
41811759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4193113Sgblack@eecs.umich.edu#if NO_STAT64
4203113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4213113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4223113Sgblack@eecs.umich.edu#else
4233113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4243113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4253113Sgblack@eecs.umich.edu#endif
4263113Sgblack@eecs.umich.edu
4273113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4283113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4293113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4303113Sgblack@eecs.umich.edu
4313113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
43212032Sandreas.sandberg@arm.comvoid
4333113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4343113Sgblack@eecs.umich.edu{
4354189Sgblack@eecs.umich.edu    using namespace TheISA;
4364189Sgblack@eecs.umich.edu
4373113Sgblack@eecs.umich.edu    if (fakeTTY)
4383113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4393113Sgblack@eecs.umich.edu    else
4403113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4418737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4423113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4438737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4443277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4455515SMichael.Adler@intel.com    if (fakeTTY) {
4465515SMichael.Adler@intel.com        // Claim to be a character device
4475515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4485515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4495515SMichael.Adler@intel.com    }
4508737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4513277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4528737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4533277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4548737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4553277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4568737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4573113Sgblack@eecs.umich.edu    if (fakeTTY)
4583113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4593113Sgblack@eecs.umich.edu    else
4603113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4618737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4623113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4638737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4643114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4658737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4663114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4678737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4683114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4698737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
47011906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
4714061Sgblack@eecs.umich.edu    // consistently across different hosts.
4724061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4738737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4743113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4758737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4763113Sgblack@eecs.umich.edu}
4773113Sgblack@eecs.umich.edu
4783113Sgblack@eecs.umich.edu// Same for stat64
4793113Sgblack@eecs.umich.edu
4803113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
48112032Sandreas.sandberg@arm.comvoid
4823113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4833113Sgblack@eecs.umich.edu{
4844189Sgblack@eecs.umich.edu    using namespace TheISA;
4854189Sgblack@eecs.umich.edu
4863113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4873113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4883113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4898737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
4903113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
4918737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
4923113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
4938737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
4943113Sgblack@eecs.umich.edu#else
4953113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4963113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4973113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4983113Sgblack@eecs.umich.edu#endif
4993113Sgblack@eecs.umich.edu}
5003113Sgblack@eecs.umich.edu
50111906SBrandon.Potter@amd.com// Here are a couple of convenience functions
5023113Sgblack@eecs.umich.edutemplate<class OS>
50312032Sandreas.sandberg@arm.comvoid
5048852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
50511906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
5063113Sgblack@eecs.umich.edu{
5073113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5083113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5093113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5103113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5113113Sgblack@eecs.umich.edu}
5123113Sgblack@eecs.umich.edu
5133113Sgblack@eecs.umich.edutemplate<class OS>
51412032Sandreas.sandberg@arm.comvoid
5158852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
51611906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
5173113Sgblack@eecs.umich.edu{
5183113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5193113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5206686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5213113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5223113Sgblack@eecs.umich.edu}
5233113Sgblack@eecs.umich.edu
52411759Sbrandon.potter@amd.comtemplate <class OS>
52512032Sandreas.sandberg@arm.comvoid
52611759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
52711759Sbrandon.potter@amd.com                 hst_statfs *host)
52811759Sbrandon.potter@amd.com{
52911759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
53011759Sbrandon.potter@amd.com
53111812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
53211812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
53311812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
53411759Sbrandon.potter@amd.com#else
53511812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
53611759Sbrandon.potter@amd.com#endif
53711759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
53811759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
53911759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
54011759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
54111759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
54211759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
54311812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
54411812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
54511812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
54611812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
54711812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
54811812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
54911812Sbaz21@cam.ac.uk#else
55011759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
55111759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
55211812Sbaz21@cam.ac.uk#endif
55311812Sbaz21@cam.ac.uk#if defined(__linux__)
55411759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
55511812Sbaz21@cam.ac.uk#else
55611812Sbaz21@cam.ac.uk    /*
55711812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
55811812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
55911812Sbaz21@cam.ac.uk     */
56011812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
56111812Sbaz21@cam.ac.uk#endif
56211759Sbrandon.potter@amd.com
56311759Sbrandon.potter@amd.com    tgt.copyOut(mem);
56411759Sbrandon.potter@amd.com}
56511759Sbrandon.potter@amd.com
566378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
567378SN/A/// only to find out if their stdout is a tty, to determine whether to
5689141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5699141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
570360SN/Atemplate <class OS>
5711450SN/ASyscallReturn
57211856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
573360SN/A{
5746701Sgblack@eecs.umich.edu    int index = 0;
57511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
57611856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
577360SN/A
57810930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
579360SN/A
58011856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
58111856Sbrandon.potter@amd.com        return -ENOTTY;
58210496Ssteve.reinhardt@amd.com
58311856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
58411856Sbrandon.potter@amd.com    if (!dfdp)
5851458SN/A        return -EBADF;
586360SN/A
58711856Sbrandon.potter@amd.com    /**
58811856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
58911856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
59011856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
59111856Sbrandon.potter@amd.com     */
59211856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
59311856Sbrandon.potter@amd.com    if (emul_driver)
59411856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
59510496Ssteve.reinhardt@amd.com
59611856Sbrandon.potter@amd.com    /**
59711856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
59811856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
59911856Sbrandon.potter@amd.com     */
60011856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
60110930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
6029141Smarc.orr@gmail.com    return -ENOTTY;
603360SN/A}
604360SN/A
605360SN/Atemplate <class OS>
60611907SBrandon.Potter@amd.comSyscallReturn
60711907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc,
60811907SBrandon.Potter@amd.com         bool isopenat)
609360SN/A{
61011907SBrandon.Potter@amd.com    int index = 0;
61111907SBrandon.Potter@amd.com    int tgt_dirfd = -1;
61211907SBrandon.Potter@amd.com
61311907SBrandon.Potter@amd.com    /**
61411907SBrandon.Potter@amd.com     * If using the openat variant, read in the target directory file
61511907SBrandon.Potter@amd.com     * descriptor from the simulated process.
61611907SBrandon.Potter@amd.com     */
61711907SBrandon.Potter@amd.com    if (isopenat)
61811907SBrandon.Potter@amd.com        tgt_dirfd = p->getSyscallArg(tc, index);
61911907SBrandon.Potter@amd.com
62011907SBrandon.Potter@amd.com    /**
62111907SBrandon.Potter@amd.com     * Retrieve the simulated process' memory proxy and then read in the path
62211907SBrandon.Potter@amd.com     * string from that memory space into the host's working memory space.
62311907SBrandon.Potter@amd.com     */
624360SN/A    std::string path;
62511907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index)))
6261458SN/A        return -EFAULT;
627360SN/A
62811907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
62911907SBrandon.Potter@amd.com    int host_flags = O_BINARY;
63011907SBrandon.Potter@amd.com#else
63111907SBrandon.Potter@amd.com    int host_flags = 0;
63211907SBrandon.Potter@amd.com#endif
63311907SBrandon.Potter@amd.com    /**
63411907SBrandon.Potter@amd.com     * Translate target flags into host flags. Flags exist which are not
63511907SBrandon.Potter@amd.com     * ported between architectures which can cause check failures.
63611907SBrandon.Potter@amd.com     */
63711907SBrandon.Potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
638360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
63911907SBrandon.Potter@amd.com        if (tgt_flags & OS::openFlagTable[i].tgtFlag) {
64011907SBrandon.Potter@amd.com            tgt_flags &= ~OS::openFlagTable[i].tgtFlag;
64111907SBrandon.Potter@amd.com            host_flags |= OS::openFlagTable[i].hostFlag;
642360SN/A        }
643360SN/A    }
64411907SBrandon.Potter@amd.com    if (tgt_flags) {
64511907SBrandon.Potter@amd.com        warn("open%s: cannot decode flags 0x%x",
64611907SBrandon.Potter@amd.com             isopenat ? "at" : "", tgt_flags);
64711907SBrandon.Potter@amd.com    }
648360SN/A#ifdef __CYGWIN32__
64911907SBrandon.Potter@amd.com    host_flags |= O_BINARY;
650360SN/A#endif
651360SN/A
65211907SBrandon.Potter@amd.com    int mode = p->getSyscallArg(tc, index);
6533669Sbinkertn@umich.edu
65411907SBrandon.Potter@amd.com    /**
65511907SBrandon.Potter@amd.com     * If the simulated process called open or openat with AT_FDCWD specified,
65611907SBrandon.Potter@amd.com     * take the current working directory value which was passed into the
65711907SBrandon.Potter@amd.com     * process class as a Python parameter and append the current path to
65811907SBrandon.Potter@amd.com     * create a full path.
65911907SBrandon.Potter@amd.com     * Otherwise, openat with a valid target directory file descriptor has
66011907SBrandon.Potter@amd.com     * been called. If the path option, which was passed in as a parameter,
66111907SBrandon.Potter@amd.com     * is not absolute, retrieve the directory file descriptor's path and
66211907SBrandon.Potter@amd.com     * prepend it to the path passed in as a parameter.
66311907SBrandon.Potter@amd.com     * In every case, we should have a full path (which is relevant to the
66411907SBrandon.Potter@amd.com     * host) to work with after this block has been passed.
66511907SBrandon.Potter@amd.com     */
66611907SBrandon.Potter@amd.com    if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) {
66711907SBrandon.Potter@amd.com        path = p->fullPath(path);
66811907SBrandon.Potter@amd.com    } else if (!startswith(path, "/")) {
66911907SBrandon.Potter@amd.com        std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]);
67011907SBrandon.Potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
67111907SBrandon.Potter@amd.com        if (!ffdp)
67211907SBrandon.Potter@amd.com            return -EBADF;
67311907SBrandon.Potter@amd.com        path.insert(0, ffdp->getFileName());
67411907SBrandon.Potter@amd.com    }
6751706SN/A
67611907SBrandon.Potter@amd.com    /**
67711907SBrandon.Potter@amd.com     * Since this is an emulated environment, we create pseudo file
67811907SBrandon.Potter@amd.com     * descriptors for device requests that have been registered with
67911907SBrandon.Potter@amd.com     * the process class through Python; this allows us to create a file
68011907SBrandon.Potter@amd.com     * descriptor for subsequent ioctl or mmap calls.
68111907SBrandon.Potter@amd.com     */
68210496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
68310496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
68411907SBrandon.Potter@amd.com        EmulatedDriver *drv = p->findDriver(filename);
68511907SBrandon.Potter@amd.com        if (drv) {
68611907SBrandon.Potter@amd.com            DPRINTF_SYSCALL(Verbose, "open%s: passing call to "
68711907SBrandon.Potter@amd.com                            "driver open with path[%s]\n",
68811907SBrandon.Potter@amd.com                            isopenat ? "at" : "", path.c_str());
68911907SBrandon.Potter@amd.com            return drv->open(p, tc, mode, host_flags);
69010496Ssteve.reinhardt@amd.com        }
69111907SBrandon.Potter@amd.com        /**
69211907SBrandon.Potter@amd.com         * Fall through here for pass through to host devices, such
69311907SBrandon.Potter@amd.com         * as /dev/zero
69411907SBrandon.Potter@amd.com         */
69510496Ssteve.reinhardt@amd.com    }
69610496Ssteve.reinhardt@amd.com
69711907SBrandon.Potter@amd.com    /**
69811907SBrandon.Potter@amd.com     * Some special paths and files cannot be called on the host and need
69911907SBrandon.Potter@amd.com     * to be handled as special cases inside the simulator.
70011907SBrandon.Potter@amd.com     * If the full path that was created above does not match any of the
70111907SBrandon.Potter@amd.com     * special cases, pass it through to the open call on the host to let
70211907SBrandon.Potter@amd.com     * the host open the file on our behalf.
70311907SBrandon.Potter@amd.com     * If the host cannot open the file, return the host's error code back
70411907SBrandon.Potter@amd.com     * through the system call to the simulated process.
70511907SBrandon.Potter@amd.com     */
70611907SBrandon.Potter@amd.com    int sim_fd = -1;
70711907SBrandon.Potter@amd.com    std::vector<std::string> special_paths =
70811907SBrandon.Potter@amd.com            { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" };
70911907SBrandon.Potter@amd.com    for (auto entry : special_paths) {
71011907SBrandon.Potter@amd.com        if (startswith(path, entry))
71111907SBrandon.Potter@amd.com            sim_fd = OS::openSpecialFile(path, p, tc);
71211907SBrandon.Potter@amd.com    }
71311907SBrandon.Potter@amd.com    if (sim_fd == -1) {
71411907SBrandon.Potter@amd.com        sim_fd = open(path.c_str(), host_flags, mode);
71511907SBrandon.Potter@amd.com    }
71611907SBrandon.Potter@amd.com    if (sim_fd == -1) {
71711907SBrandon.Potter@amd.com        int local = -errno;
71811907SBrandon.Potter@amd.com        DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n",
71911907SBrandon.Potter@amd.com                        isopenat ? "at" : "", path.c_str());
72011907SBrandon.Potter@amd.com        return local;
72111907SBrandon.Potter@amd.com    }
722360SN/A
72311907SBrandon.Potter@amd.com    /**
72411907SBrandon.Potter@amd.com     * The file was opened successfully and needs to be recorded in the
72511907SBrandon.Potter@amd.com     * process' file descriptor array so that it can be retrieved later.
72611907SBrandon.Potter@amd.com     * The target file descriptor that is chosen will be the lowest unused
72711907SBrandon.Potter@amd.com     * file descriptor.
72811907SBrandon.Potter@amd.com     * Return the indirect target file descriptor back to the simulated
72911907SBrandon.Potter@amd.com     * process to act as a handle for the opened file.
73011907SBrandon.Potter@amd.com     */
73111907SBrandon.Potter@amd.com    auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0);
73211907SBrandon.Potter@amd.com    int tgt_fd = p->fds->allocFD(ffdp);
73311907SBrandon.Potter@amd.com    DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n",
73411907SBrandon.Potter@amd.com                    isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str());
73511907SBrandon.Potter@amd.com    return tgt_fd;
736360SN/A}
737360SN/A
73810027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
73910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
74010027SChris.Adeniyi-Jones@arm.comSyscallReturn
74111851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
74210027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
74310027SChris.Adeniyi-Jones@arm.com{
74411907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, false);
74510027SChris.Adeniyi-Jones@arm.com}
74610027SChris.Adeniyi-Jones@arm.com
74710027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
74810027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
74910027SChris.Adeniyi-Jones@arm.comSyscallReturn
75011851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
75111851Sbrandon.potter@amd.com           ThreadContext *tc)
75210027SChris.Adeniyi-Jones@arm.com{
75311907SBrandon.Potter@amd.com    return openImpl<OS>(desc, callnum, process, tc, true);
75410027SChris.Adeniyi-Jones@arm.com}
75510027SChris.Adeniyi-Jones@arm.com
75610633Smichaelupton@gmail.com/// Target unlinkat() handler.
75710633Smichaelupton@gmail.comtemplate <class OS>
75810633Smichaelupton@gmail.comSyscallReturn
75911851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
76010633Smichaelupton@gmail.com             ThreadContext *tc)
76110633Smichaelupton@gmail.com{
76210633Smichaelupton@gmail.com    int index = 0;
76310633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
76410633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
76510633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
76610633Smichaelupton@gmail.com
76710633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
76810633Smichaelupton@gmail.com}
76910633Smichaelupton@gmail.com
77010203SAli.Saidi@ARM.com/// Target facessat() handler
77110203SAli.Saidi@ARM.comtemplate <class OS>
77210203SAli.Saidi@ARM.comSyscallReturn
77311851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
77411851Sbrandon.potter@amd.com              ThreadContext *tc)
77510203SAli.Saidi@ARM.com{
77610203SAli.Saidi@ARM.com    int index = 0;
77710203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
77810203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
77910203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
78010203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
78110203SAli.Saidi@ARM.com}
78210203SAli.Saidi@ARM.com
78310203SAli.Saidi@ARM.com/// Target readlinkat() handler
78410203SAli.Saidi@ARM.comtemplate <class OS>
78510203SAli.Saidi@ARM.comSyscallReturn
78611851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
78711851Sbrandon.potter@amd.com               ThreadContext *tc)
78810203SAli.Saidi@ARM.com{
78910203SAli.Saidi@ARM.com    int index = 0;
79010203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
79110203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
79210203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
79310203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
79410203SAli.Saidi@ARM.com}
79510203SAli.Saidi@ARM.com
79610850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
79710850SGiacomo.Gabrielli@arm.comtemplate <class OS>
79810850SGiacomo.Gabrielli@arm.comSyscallReturn
79911851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
80010850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
80110850SGiacomo.Gabrielli@arm.com{
80210850SGiacomo.Gabrielli@arm.com    int index = 0;
80310850SGiacomo.Gabrielli@arm.com
80410850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
80510850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
80610850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
80710850SGiacomo.Gabrielli@arm.com
80810850SGiacomo.Gabrielli@arm.com    std::string old_name;
80910850SGiacomo.Gabrielli@arm.com
81010850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
81110850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
81210850SGiacomo.Gabrielli@arm.com        return -EFAULT;
81310850SGiacomo.Gabrielli@arm.com
81410850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
81510850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
81610850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
81710850SGiacomo.Gabrielli@arm.com
81810850SGiacomo.Gabrielli@arm.com    std::string new_name;
81910850SGiacomo.Gabrielli@arm.com
82010850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
82110850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
82210850SGiacomo.Gabrielli@arm.com        return -EFAULT;
82310850SGiacomo.Gabrielli@arm.com
82410850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
82510850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
82610850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
82710850SGiacomo.Gabrielli@arm.com
82810850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
82910850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
83010850SGiacomo.Gabrielli@arm.com}
83110850SGiacomo.Gabrielli@arm.com
8326640Svince@csl.cornell.edu/// Target sysinfo() handler.
8336640Svince@csl.cornell.edutemplate <class OS>
8346640Svince@csl.cornell.eduSyscallReturn
83511851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
83611851Sbrandon.potter@amd.com            ThreadContext *tc)
8376640Svince@csl.cornell.edu{
8386640Svince@csl.cornell.edu
8396701Sgblack@eecs.umich.edu    int index = 0;
8406701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
84110793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
8426640Svince@csl.cornell.edu
84311758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
84411758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
84511758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
8466640Svince@csl.cornell.edu
8478706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
8486640Svince@csl.cornell.edu
8496701Sgblack@eecs.umich.edu    return 0;
8506640Svince@csl.cornell.edu}
851360SN/A
8521999SN/A/// Target chmod() handler.
8531999SN/Atemplate <class OS>
8541999SN/ASyscallReturn
85511851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
8562680Sktlim@umich.edu          ThreadContext *tc)
8571999SN/A{
8581999SN/A    std::string path;
8591999SN/A
8606701Sgblack@eecs.umich.edu    int index = 0;
8618852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8626701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8631999SN/A        return -EFAULT;
8646701Sgblack@eecs.umich.edu    }
8651999SN/A
8666701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8671999SN/A    mode_t hostMode = 0;
8681999SN/A
8691999SN/A    // XXX translate mode flags via OS::something???
8701999SN/A    hostMode = mode;
8711999SN/A
8723669Sbinkertn@umich.edu    // Adjust path for current working directory
8733669Sbinkertn@umich.edu    path = process->fullPath(path);
8743669Sbinkertn@umich.edu
8751999SN/A    // do the chmod
8761999SN/A    int result = chmod(path.c_str(), hostMode);
8771999SN/A    if (result < 0)
8782218SN/A        return -errno;
8791999SN/A
8801999SN/A    return 0;
8811999SN/A}
8821999SN/A
8831999SN/A
8841999SN/A/// Target fchmod() handler.
8851999SN/Atemplate <class OS>
8861999SN/ASyscallReturn
88711856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
8881999SN/A{
8896701Sgblack@eecs.umich.edu    int index = 0;
89011856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
89111856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
89210931Sbrandon.potter@amd.com
89311856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
89411856Sbrandon.potter@amd.com    if (!ffdp)
8951999SN/A        return -EBADF;
89611856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
8971999SN/A
89811856Sbrandon.potter@amd.com    mode_t hostMode = mode;
8991999SN/A
90011856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
9011999SN/A
90211856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
9031999SN/A}
9041999SN/A
9055877Shsul@eecs.umich.edu/// Target mremap() handler.
9065877Shsul@eecs.umich.edutemplate <class OS>
9075877Shsul@eecs.umich.eduSyscallReturn
90811851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
9095877Shsul@eecs.umich.edu{
9106701Sgblack@eecs.umich.edu    int index = 0;
9116701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
9126701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
9136701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
9146701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
91510027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
91610027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
91710027SChris.Adeniyi-Jones@arm.com
91810027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
91910027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
9205877Shsul@eecs.umich.edu
92110318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
92210318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
9235877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
9245877Shsul@eecs.umich.edu        return -EINVAL;
9255877Shsul@eecs.umich.edu    }
9265877Shsul@eecs.umich.edu
92710486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
92810486Stjablin@gmail.com
9295877Shsul@eecs.umich.edu    if (new_length > old_length) {
93011905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
93111905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
93211905SBrandon.Potter@amd.com
93311905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
93410027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
9355877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
93611905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
93711905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
9385877Shsul@eecs.umich.edu            return start;
9395877Shsul@eecs.umich.edu        } else {
94010027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
9415877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
9425877Shsul@eecs.umich.edu                return -ENOMEM;
9435877Shsul@eecs.umich.edu            } else {
94410027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
94511905SBrandon.Potter@amd.com                    provided_address : mmap_end;
94610027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
94710027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
94810027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
94910027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
9505877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
95110027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
95210027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
95310027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
95410027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
95511905SBrandon.Potter@amd.com                    mem_state->setMmapEnd(mmap_end + new_length);
95610027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
95711905SBrandon.Potter@amd.com                    new_start + new_length > mem_state->getMmapEnd()) {
95810027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
95910027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
96010027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
96110027SChris.Adeniyi-Jones@arm.com                }
96210027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
96310027SChris.Adeniyi-Jones@arm.com                return new_start;
9645877Shsul@eecs.umich.edu            }
9655877Shsul@eecs.umich.edu        }
9665877Shsul@eecs.umich.edu    } else {
96710027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
96810027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
9698601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
97010027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
9715877Shsul@eecs.umich.edu    }
9725877Shsul@eecs.umich.edu}
9731999SN/A
974378SN/A/// Target stat() handler.
975360SN/Atemplate <class OS>
9761450SN/ASyscallReturn
97711851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
9782680Sktlim@umich.edu         ThreadContext *tc)
979360SN/A{
980360SN/A    std::string path;
981360SN/A
9826701Sgblack@eecs.umich.edu    int index = 0;
9838852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9846701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9856701Sgblack@eecs.umich.edu        return -EFAULT;
9866701Sgblack@eecs.umich.edu    }
9876701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
988360SN/A
9893669Sbinkertn@umich.edu    // Adjust path for current working directory
9903669Sbinkertn@umich.edu    path = process->fullPath(path);
9913669Sbinkertn@umich.edu
992360SN/A    struct stat hostBuf;
993360SN/A    int result = stat(path.c_str(), &hostBuf);
994360SN/A
995360SN/A    if (result < 0)
9962218SN/A        return -errno;
997360SN/A
9988706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
999360SN/A
10001458SN/A    return 0;
1001360SN/A}
1002360SN/A
1003360SN/A
10045074Ssaidi@eecs.umich.edu/// Target stat64() handler.
10055074Ssaidi@eecs.umich.edutemplate <class OS>
10065074Ssaidi@eecs.umich.eduSyscallReturn
100711851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
10085074Ssaidi@eecs.umich.edu           ThreadContext *tc)
10095074Ssaidi@eecs.umich.edu{
10105074Ssaidi@eecs.umich.edu    std::string path;
10115074Ssaidi@eecs.umich.edu
10126701Sgblack@eecs.umich.edu    int index = 0;
10138852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10146701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
10155074Ssaidi@eecs.umich.edu        return -EFAULT;
10166701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10175074Ssaidi@eecs.umich.edu
10185074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
10195074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
10205074Ssaidi@eecs.umich.edu
10215208Ssaidi@eecs.umich.edu#if NO_STAT64
10225208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
10235208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
10245208Ssaidi@eecs.umich.edu#else
10255074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
10265074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
10275208Ssaidi@eecs.umich.edu#endif
10285074Ssaidi@eecs.umich.edu
10295074Ssaidi@eecs.umich.edu    if (result < 0)
10305074Ssaidi@eecs.umich.edu        return -errno;
10315074Ssaidi@eecs.umich.edu
10328706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10335074Ssaidi@eecs.umich.edu
10345074Ssaidi@eecs.umich.edu    return 0;
10355074Ssaidi@eecs.umich.edu}
10365074Ssaidi@eecs.umich.edu
10375074Ssaidi@eecs.umich.edu
103810027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
103910027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
104010027SChris.Adeniyi-Jones@arm.comSyscallReturn
104111851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
104210027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
104310027SChris.Adeniyi-Jones@arm.com{
104410027SChris.Adeniyi-Jones@arm.com    int index = 0;
104510027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
104610027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
104710793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
104810027SChris.Adeniyi-Jones@arm.com
104910027SChris.Adeniyi-Jones@arm.com    std::string path;
105010027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
105110027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
105210027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
105310027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
105410027SChris.Adeniyi-Jones@arm.com
105510027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
105610027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
105710027SChris.Adeniyi-Jones@arm.com
105810027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
105910027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
106010027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
106110027SChris.Adeniyi-Jones@arm.com#else
106210027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
106310027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
106410027SChris.Adeniyi-Jones@arm.com#endif
106510027SChris.Adeniyi-Jones@arm.com
106610027SChris.Adeniyi-Jones@arm.com    if (result < 0)
106710027SChris.Adeniyi-Jones@arm.com        return -errno;
106810027SChris.Adeniyi-Jones@arm.com
106910027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
107010027SChris.Adeniyi-Jones@arm.com
107110027SChris.Adeniyi-Jones@arm.com    return 0;
107210027SChris.Adeniyi-Jones@arm.com}
107310027SChris.Adeniyi-Jones@arm.com
107410027SChris.Adeniyi-Jones@arm.com
10751999SN/A/// Target fstat64() handler.
10761999SN/Atemplate <class OS>
10771999SN/ASyscallReturn
107811856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
10791999SN/A{
10806701Sgblack@eecs.umich.edu    int index = 0;
108111856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
108211856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
108310931Sbrandon.potter@amd.com
108411856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
108511856Sbrandon.potter@amd.com    if (!ffdp)
10861999SN/A        return -EBADF;
108711856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
10881999SN/A
10892764Sstever@eecs.umich.edu#if NO_STAT64
10902064SN/A    struct stat  hostBuf;
109110931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10922064SN/A#else
10932064SN/A    struct stat64  hostBuf;
109410931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10952064SN/A#endif
10961999SN/A
10971999SN/A    if (result < 0)
10982218SN/A        return -errno;
10991999SN/A
110010931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11011999SN/A
11021999SN/A    return 0;
11031999SN/A}
11041999SN/A
11051999SN/A
1106378SN/A/// Target lstat() handler.
1107360SN/Atemplate <class OS>
11081450SN/ASyscallReturn
110911851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
11102680Sktlim@umich.edu          ThreadContext *tc)
1111360SN/A{
1112360SN/A    std::string path;
1113360SN/A
11146701Sgblack@eecs.umich.edu    int index = 0;
11158852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11166701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11176701Sgblack@eecs.umich.edu        return -EFAULT;
11186701Sgblack@eecs.umich.edu    }
11196701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1120360SN/A
11213669Sbinkertn@umich.edu    // Adjust path for current working directory
11223669Sbinkertn@umich.edu    path = process->fullPath(path);
11233669Sbinkertn@umich.edu
1124360SN/A    struct stat hostBuf;
1125360SN/A    int result = lstat(path.c_str(), &hostBuf);
1126360SN/A
1127360SN/A    if (result < 0)
11281458SN/A        return -errno;
1129360SN/A
11308706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1131360SN/A
11321458SN/A    return 0;
1133360SN/A}
1134360SN/A
11351999SN/A/// Target lstat64() handler.
11361999SN/Atemplate <class OS>
11371999SN/ASyscallReturn
113811851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
11392680Sktlim@umich.edu            ThreadContext *tc)
11401999SN/A{
11411999SN/A    std::string path;
11421999SN/A
11436701Sgblack@eecs.umich.edu    int index = 0;
11448852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11456701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11466701Sgblack@eecs.umich.edu        return -EFAULT;
11476701Sgblack@eecs.umich.edu    }
11486701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11491999SN/A
11503669Sbinkertn@umich.edu    // Adjust path for current working directory
11513669Sbinkertn@umich.edu    path = process->fullPath(path);
11523669Sbinkertn@umich.edu
11532764Sstever@eecs.umich.edu#if NO_STAT64
11542064SN/A    struct stat hostBuf;
11552064SN/A    int result = lstat(path.c_str(), &hostBuf);
11562064SN/A#else
11571999SN/A    struct stat64 hostBuf;
11581999SN/A    int result = lstat64(path.c_str(), &hostBuf);
11592064SN/A#endif
11601999SN/A
11611999SN/A    if (result < 0)
11621999SN/A        return -errno;
11631999SN/A
11648706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11651999SN/A
11661999SN/A    return 0;
11671999SN/A}
11681999SN/A
1169378SN/A/// Target fstat() handler.
1170360SN/Atemplate <class OS>
11711450SN/ASyscallReturn
117211856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1173360SN/A{
11746701Sgblack@eecs.umich.edu    int index = 0;
117511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
117611856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1177360SN/A
117811380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1179360SN/A
118011856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
118111856Sbrandon.potter@amd.com    if (!ffdp)
11821458SN/A        return -EBADF;
118311856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1184360SN/A
1185360SN/A    struct stat hostBuf;
118610931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1187360SN/A
1188360SN/A    if (result < 0)
11891458SN/A        return -errno;
1190360SN/A
119110931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11922021SN/A
11931458SN/A    return 0;
1194360SN/A}
1195360SN/A
1196360SN/A
11971706SN/A/// Target statfs() handler.
11981706SN/Atemplate <class OS>
11991706SN/ASyscallReturn
120011851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
12012680Sktlim@umich.edu           ThreadContext *tc)
12021706SN/A{
120311799Sbrandon.potter@amd.com#if NO_STATFS
120411799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
120511799Sbrandon.potter@amd.com#else
12061706SN/A    std::string path;
12071706SN/A
12086701Sgblack@eecs.umich.edu    int index = 0;
12098852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
12106701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
12116701Sgblack@eecs.umich.edu        return -EFAULT;
12126701Sgblack@eecs.umich.edu    }
12136701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
12141706SN/A
12153669Sbinkertn@umich.edu    // Adjust path for current working directory
12163669Sbinkertn@umich.edu    path = process->fullPath(path);
12173669Sbinkertn@umich.edu
12181706SN/A    struct statfs hostBuf;
12191706SN/A    int result = statfs(path.c_str(), &hostBuf);
12201706SN/A
12211706SN/A    if (result < 0)
12222218SN/A        return -errno;
12231706SN/A
122411759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
122511799Sbrandon.potter@amd.com#endif
12261706SN/A    return 0;
12271706SN/A}
12281706SN/A
122911886Sbrandon.potter@amd.comtemplate <class OS>
123011886Sbrandon.potter@amd.comSyscallReturn
123111886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
123211886Sbrandon.potter@amd.com{
123311886Sbrandon.potter@amd.com    int index = 0;
123411886Sbrandon.potter@amd.com    TheISA::IntReg flags = p->getSyscallArg(tc, index);
123511886Sbrandon.potter@amd.com    TheISA::IntReg newStack = p->getSyscallArg(tc, index);
123611886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
123711886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
123811886Sbrandon.potter@amd.com    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
123911886Sbrandon.potter@amd.com
124011886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
124111886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
124211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
124311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
124411886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
124511886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
124611886Sbrandon.potter@amd.com        return -EINVAL;
124711886Sbrandon.potter@amd.com
124811886Sbrandon.potter@amd.com    ThreadContext *ctc;
124911886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
125011886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
125111886Sbrandon.potter@amd.com
125211886Sbrandon.potter@amd.com    /**
125311886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
125411886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
125511886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
125611886Sbrandon.potter@amd.com     * constructor.
125711886Sbrandon.potter@amd.com     */
125811886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
125911886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
126011886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
126111886Sbrandon.potter@amd.com    pp->system = p->system;
126211886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
126311886Sbrandon.potter@amd.com    pp->input.assign("stdin");
126411886Sbrandon.potter@amd.com    pp->output.assign("stdout");
126511886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
126611886Sbrandon.potter@amd.com    pp->uid = p->uid();
126711886Sbrandon.potter@amd.com    pp->euid = p->euid();
126811886Sbrandon.potter@amd.com    pp->gid = p->gid();
126911886Sbrandon.potter@amd.com    pp->egid = p->egid();
127011886Sbrandon.potter@amd.com
127111886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
127211886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
127311886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
127411886Sbrandon.potter@amd.com    do {
127511886Sbrandon.potter@amd.com        temp_pid++;
127611886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
127711886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
127811886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
127911886Sbrandon.potter@amd.com
128011886Sbrandon.potter@amd.com    pp->pid = temp_pid;
128111886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
128211886Sbrandon.potter@amd.com    Process *cp = pp->create();
128311886Sbrandon.potter@amd.com    delete pp;
128411886Sbrandon.potter@amd.com
128511886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
128611886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
128711886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
128811886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
128911886Sbrandon.potter@amd.com
129011886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
129111886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
129211886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
129311886Sbrandon.potter@amd.com        *ptid = cp->pid();
129411886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
129511886Sbrandon.potter@amd.com    }
129611886Sbrandon.potter@amd.com
129711886Sbrandon.potter@amd.com    cp->initState();
129811886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
129911886Sbrandon.potter@amd.com
130011911SBrandon.Potter@amd.com    if (flags & OS::TGT_CLONE_THREAD) {
130111911SBrandon.Potter@amd.com        delete cp->sigchld;
130211911SBrandon.Potter@amd.com        cp->sigchld = p->sigchld;
130311911SBrandon.Potter@amd.com    } else if (flags & OS::TGT_SIGCHLD) {
130411911SBrandon.Potter@amd.com        *cp->sigchld = true;
130511911SBrandon.Potter@amd.com    }
130611911SBrandon.Potter@amd.com
130711886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
130811886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
130911886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
131011886Sbrandon.potter@amd.com        *ctid = cp->pid();
131111886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
131211886Sbrandon.potter@amd.com    }
131311886Sbrandon.potter@amd.com
131411886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
131511886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
131611886Sbrandon.potter@amd.com
131711886Sbrandon.potter@amd.com    ctc->clearArchRegs();
131811886Sbrandon.potter@amd.com
131911886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
132011886Sbrandon.potter@amd.com    TheISA::copyMiscRegs(tc, ctc);
132111886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
132211886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
132311886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0);
132411886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0);
132511886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2);
132611886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows);
132711886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_CWP, 0);
132811886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0);
132911886Sbrandon.potter@amd.com    ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0);
133011886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY);
133111886Sbrandon.potter@amd.com    for (int y = 8; y < 32; y++)
133211886Sbrandon.potter@amd.com        ctc->setIntReg(y, tc->readIntReg(y));
133311886Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA
133411886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
133511886Sbrandon.potter@amd.com#endif
133611886Sbrandon.potter@amd.com
133711886Sbrandon.potter@amd.com#if THE_ISA == X86_ISA
133811886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_SETTLS) {
133911886Sbrandon.potter@amd.com        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr);
134011886Sbrandon.potter@amd.com        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr);
134111886Sbrandon.potter@amd.com    }
134211886Sbrandon.potter@amd.com#endif
134311886Sbrandon.potter@amd.com
134411886Sbrandon.potter@amd.com    if (newStack)
134511886Sbrandon.potter@amd.com        ctc->setIntReg(TheISA::StackPointerReg, newStack);
134611886Sbrandon.potter@amd.com
134711886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
134811886Sbrandon.potter@amd.com
134911886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
135011886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
135111886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
135211886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
135311886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
135411886Sbrandon.potter@amd.com#endif
135511886Sbrandon.potter@amd.com
135611886Sbrandon.potter@amd.com    ctc->pcState(tc->nextInstAddr());
135711886Sbrandon.potter@amd.com    ctc->activate();
135811886Sbrandon.potter@amd.com
135911886Sbrandon.potter@amd.com    return cp->pid();
136011886Sbrandon.potter@amd.com}
13611706SN/A
13621706SN/A/// Target fstatfs() handler.
13631706SN/Atemplate <class OS>
13641706SN/ASyscallReturn
136511856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
13661706SN/A{
13676701Sgblack@eecs.umich.edu    int index = 0;
136811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
136911856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
13701706SN/A
137111856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
137211856Sbrandon.potter@amd.com    if (!ffdp)
13731706SN/A        return -EBADF;
137411856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
13751706SN/A
13761706SN/A    struct statfs hostBuf;
137710931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
13781706SN/A
13791706SN/A    if (result < 0)
13802218SN/A        return -errno;
13811706SN/A
138211759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
13831706SN/A
13841706SN/A    return 0;
13851706SN/A}
13861706SN/A
13871706SN/A
13881999SN/A/// Target writev() handler.
13891999SN/Atemplate <class OS>
13901999SN/ASyscallReturn
139111856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
13921999SN/A{
13936701Sgblack@eecs.umich.edu    int index = 0;
139411856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
139510931Sbrandon.potter@amd.com
139611856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
139711856Sbrandon.potter@amd.com    if (!hbfdp)
13981999SN/A        return -EBADF;
139911856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
14001999SN/A
140111856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
140211856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
140311856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
14041999SN/A    struct iovec hiov[count];
14056227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
14061999SN/A        typename OS::tgt_iovec tiov;
14072461SN/A
140811856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
140911856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
14108737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
14111999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
141211856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
141311856Sbrandon.potter@amd.com                      hiov[i].iov_len);
14141999SN/A    }
14151999SN/A
141610931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
14171999SN/A
14186227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
14191999SN/A        delete [] (char *)hiov[i].iov_base;
14201999SN/A
14211999SN/A    if (result < 0)
14222218SN/A        return -errno;
14231999SN/A
142410629Sjthestness@gmail.com    return result;
14251999SN/A}
14261999SN/A
142711385Sbrandon.potter@amd.com/// Real mmap handler.
1428360SN/Atemplate <class OS>
14291450SN/ASyscallReturn
143011851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
143111385Sbrandon.potter@amd.com         bool is_mmap2)
1432360SN/A{
14336701Sgblack@eecs.umich.edu    int index = 0;
14346701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
14356701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
143611383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
143711383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
14388324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
143910486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1440360SN/A
144111385Sbrandon.potter@amd.com    if (is_mmap2)
144211385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
14439008Sgblack@eecs.umich.edu
144411383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
144511383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
144611383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
144711383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
144811383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
144911383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
145011383Sbrandon.potter@amd.com        !length) {
145111383Sbrandon.potter@amd.com        return -EINVAL;
145211383Sbrandon.potter@amd.com    }
14538324Ssteve.reinhardt@amd.com
145411383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
145511383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
145611383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
145711383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
145811383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
145911383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
146011383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
146111383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
146211383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
146311383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
146411383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
146511383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
146611383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
146711383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
146811383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
146911383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
147011383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
147111383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
147211383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
147311383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
147411383Sbrandon.potter@amd.com        // than we currently maintain.
147511383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
147611383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
147711383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
14788324Ssteve.reinhardt@amd.com    }
14795877Shsul@eecs.umich.edu
148010486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
148110486Stjablin@gmail.com
148211383Sbrandon.potter@amd.com    int sim_fd = -1;
148311383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
148411383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
148511856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
148611624Smichael.lebeane@amd.com
148711856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
148811856Sbrandon.potter@amd.com        if (dfdp) {
148911856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
149011856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
149111624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
149211624Smichael.lebeane@amd.com        }
149311624Smichael.lebeane@amd.com
149411856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
149511856Sbrandon.potter@amd.com        if (!ffdp)
149611383Sbrandon.potter@amd.com            return -EBADF;
149711856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1498360SN/A
149911913SBrandon.Potter@amd.com        pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE,
150011383Sbrandon.potter@amd.com                                    sim_fd, offset);
15018600Ssteve.reinhardt@amd.com
150211383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
150311383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
150411383Sbrandon.potter@amd.com            return -errno;
15058600Ssteve.reinhardt@amd.com        }
15062544SN/A    }
15072544SN/A
150811383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
150911383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
151011383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
151111905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
151211905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
151311905SBrandon.Potter@amd.com
151411905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
151511905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
151611905SBrandon.Potter@amd.com
151711905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
151811383Sbrandon.potter@amd.com    }
151911383Sbrandon.potter@amd.com
152011383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
152111383Sbrandon.potter@amd.com                    start, start + length - 1);
152211383Sbrandon.potter@amd.com
152311383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
152411383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
152511383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
152611383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
152711383Sbrandon.potter@amd.com    if (clobber) {
152811383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
152911383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
153011383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
153111383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
153211383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
15338600Ssteve.reinhardt@amd.com        }
15346672Sgblack@eecs.umich.edu    }
15358600Ssteve.reinhardt@amd.com
153611383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
153711383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
153811383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
15398601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
15402544SN/A
154111383Sbrandon.potter@amd.com    // Transfer content into target address space.
154211383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
154311383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
154411383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
154511383Sbrandon.potter@amd.com        // with something like:
154611383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
154711383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
154811383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
154911383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
155011383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
155111383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
155211383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
155311383Sbrandon.potter@amd.com    } else {
155411383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
155511383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
155611383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
155711383Sbrandon.potter@amd.com        // the host into the target address space.
155811383Sbrandon.potter@amd.com        struct stat file_stat;
155911383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
156011383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
156111383Sbrandon.potter@amd.com
156211383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
156311383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
156411383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
156511383Sbrandon.potter@amd.com        // on the specified offset into the file.
156611383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
156711383Sbrandon.potter@amd.com                                 length);
156811383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
156911383Sbrandon.potter@amd.com
157011383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
157111383Sbrandon.potter@amd.com        munmap(pmap, length);
157211383Sbrandon.potter@amd.com
157311392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
157411392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
157511392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
157611392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
157711392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
157811392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
157911392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
158011392Sbrandon.potter@amd.com        if (interpreter) {
158111392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
158211392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
158311392Sbrandon.potter@amd.com
158411392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
158511392Sbrandon.potter@amd.com
158611392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
158711856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
158811856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
158911856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
159011392Sbrandon.potter@amd.com
159111392Sbrandon.potter@amd.com                if (lib) {
159211392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
159311392Sbrandon.potter@amd.com                                        lib->textBase(), start);
159411392Sbrandon.potter@amd.com                }
159511392Sbrandon.potter@amd.com            }
159611392Sbrandon.potter@amd.com        }
159711392Sbrandon.potter@amd.com
159811383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
159911383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
160011383Sbrandon.potter@amd.com        // execution (hopefully).
160111383Sbrandon.potter@amd.com    }
160211383Sbrandon.potter@amd.com
16031458SN/A    return start;
1604360SN/A}
1605360SN/A
160611593Santhony.gutierrez@amd.comtemplate <class OS>
160711593Santhony.gutierrez@amd.comSyscallReturn
160811851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
160911593Santhony.gutierrez@amd.com{
161011593Santhony.gutierrez@amd.com    int index = 0;
161111593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
161211593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
161311593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
161411593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
161511593Santhony.gutierrez@amd.com
161611856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
161711856Sbrandon.potter@amd.com    if (!ffdp)
161811593Santhony.gutierrez@amd.com        return -EBADF;
161911856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
162011593Santhony.gutierrez@amd.com
162111593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
162211593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
162311593Santhony.gutierrez@amd.com
162411594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
162511593Santhony.gutierrez@amd.com
162611593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
162711593Santhony.gutierrez@amd.com}
162811593Santhony.gutierrez@amd.com
162911385Sbrandon.potter@amd.com/// Target mmap() handler.
163011385Sbrandon.potter@amd.comtemplate <class OS>
163111385Sbrandon.potter@amd.comSyscallReturn
163211851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
163311385Sbrandon.potter@amd.com{
163411385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
163511385Sbrandon.potter@amd.com}
163611385Sbrandon.potter@amd.com
163711385Sbrandon.potter@amd.com/// Target mmap2() handler.
163811385Sbrandon.potter@amd.comtemplate <class OS>
163911385Sbrandon.potter@amd.comSyscallReturn
164011851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
164111385Sbrandon.potter@amd.com{
164211385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
164311385Sbrandon.potter@amd.com}
164411385Sbrandon.potter@amd.com
1645378SN/A/// Target getrlimit() handler.
1646360SN/Atemplate <class OS>
16471450SN/ASyscallReturn
164811851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
164911851Sbrandon.potter@amd.com              ThreadContext *tc)
1650360SN/A{
16516701Sgblack@eecs.umich.edu    int index = 0;
16526701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
16536701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1654360SN/A
1655360SN/A    switch (resource) {
165611906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
165711906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
165811906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
165911906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
166011906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
166111906SBrandon.Potter@amd.com        break;
1662360SN/A
166311906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
166411906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
166511906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
166611906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
166711906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
166811906SBrandon.Potter@amd.com        break;
16695877Shsul@eecs.umich.edu
167011906SBrandon.Potter@amd.com      default:
167111906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
167211906SBrandon.Potter@amd.com        return -EINVAL;
167311906SBrandon.Potter@amd.com        break;
1674360SN/A    }
1675360SN/A
16768706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
16771458SN/A    return 0;
1678360SN/A}
1679360SN/A
168010796Sbrandon.potter@amd.com/// Target clock_gettime() function.
168110796Sbrandon.potter@amd.comtemplate <class OS>
168210796Sbrandon.potter@amd.comSyscallReturn
168311851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
168410796Sbrandon.potter@amd.com{
168510796Sbrandon.potter@amd.com    int index = 1;
168610796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
168710796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
168810796Sbrandon.potter@amd.com
168910796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
169010796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
169110796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
169210796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
169310796Sbrandon.potter@amd.com
169410796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
169510796Sbrandon.potter@amd.com
169610796Sbrandon.potter@amd.com    return 0;
169710796Sbrandon.potter@amd.com}
169810796Sbrandon.potter@amd.com
169911337SMichael.Lebeane@amd.com/// Target clock_getres() function.
170011337SMichael.Lebeane@amd.comtemplate <class OS>
170111337SMichael.Lebeane@amd.comSyscallReturn
170211851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
170311337SMichael.Lebeane@amd.com{
170411337SMichael.Lebeane@amd.com    int index = 1;
170511337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
170611337SMichael.Lebeane@amd.com
170711337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
170811337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
170911337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
171011337SMichael.Lebeane@amd.com
171111337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
171211337SMichael.Lebeane@amd.com
171311337SMichael.Lebeane@amd.com    return 0;
171411337SMichael.Lebeane@amd.com}
171511337SMichael.Lebeane@amd.com
1716378SN/A/// Target gettimeofday() handler.
1717360SN/Atemplate <class OS>
17181450SN/ASyscallReturn
171911851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
172011851Sbrandon.potter@amd.com                 ThreadContext *tc)
1721360SN/A{
17226701Sgblack@eecs.umich.edu    int index = 0;
17236701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1724360SN/A
172510796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1726360SN/A    tp->tv_sec += seconds_since_epoch;
17276109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
17286109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1729360SN/A
17308706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1731360SN/A
17321458SN/A    return 0;
1733360SN/A}
1734360SN/A
1735360SN/A
17361999SN/A/// Target utimes() handler.
17371999SN/Atemplate <class OS>
17381999SN/ASyscallReturn
173911851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
17402680Sktlim@umich.edu           ThreadContext *tc)
17411999SN/A{
17421999SN/A    std::string path;
17431999SN/A
17446701Sgblack@eecs.umich.edu    int index = 0;
17458852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
17466701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
17476701Sgblack@eecs.umich.edu        return -EFAULT;
17486701Sgblack@eecs.umich.edu    }
17491999SN/A
17506701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
17516701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
17528706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
17531999SN/A
17541999SN/A    struct timeval hostTimeval[2];
175511906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
17568737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
17578737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
17581999SN/A    }
17593669Sbinkertn@umich.edu
17603669Sbinkertn@umich.edu    // Adjust path for current working directory
17613669Sbinkertn@umich.edu    path = process->fullPath(path);
17623669Sbinkertn@umich.edu
17631999SN/A    int result = utimes(path.c_str(), hostTimeval);
17641999SN/A
17651999SN/A    if (result < 0)
17661999SN/A        return -errno;
17671999SN/A
17681999SN/A    return 0;
17691999SN/A}
177011886Sbrandon.potter@amd.com
177111886Sbrandon.potter@amd.comtemplate <class OS>
177211886Sbrandon.potter@amd.comSyscallReturn
177311886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
177411886Sbrandon.potter@amd.com{
177511886Sbrandon.potter@amd.com    desc->setFlags(0);
177611886Sbrandon.potter@amd.com
177711886Sbrandon.potter@amd.com    int index = 0;
177811886Sbrandon.potter@amd.com    std::string path;
177911886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
178011886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
178111886Sbrandon.potter@amd.com        return -EFAULT;
178211886Sbrandon.potter@amd.com
178311886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
178411886Sbrandon.potter@amd.com        return -EACCES;
178511886Sbrandon.potter@amd.com
178611886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
178711886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
178811886Sbrandon.potter@amd.com                      Addr mem_loc)
178911886Sbrandon.potter@amd.com    {
179011886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
179111886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
179211886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
179311886Sbrandon.potter@amd.com
179411886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
179511886Sbrandon.potter@amd.com                break;
179611886Sbrandon.potter@amd.com
179711886Sbrandon.potter@amd.com            vect.push_back(std::string());
179811886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
179911886Sbrandon.potter@amd.com        }
180011886Sbrandon.potter@amd.com    };
180111886Sbrandon.potter@amd.com
180211886Sbrandon.potter@amd.com    /**
180311886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
180411886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
180511886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
180611886Sbrandon.potter@amd.com     * constructor.
180711886Sbrandon.potter@amd.com     */
180811886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
180911886Sbrandon.potter@amd.com    pp->executable = path;
181011886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
181111886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
181211886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
181311886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
181411886Sbrandon.potter@amd.com    pp->uid = p->uid();
181511886Sbrandon.potter@amd.com    pp->egid = p->egid();
181611886Sbrandon.potter@amd.com    pp->euid = p->euid();
181711886Sbrandon.potter@amd.com    pp->gid = p->gid();
181811886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
181911886Sbrandon.potter@amd.com    pp->pid = p->pid();
182011886Sbrandon.potter@amd.com    pp->input.assign("cin");
182111886Sbrandon.potter@amd.com    pp->output.assign("cout");
182211886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
182311886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
182411886Sbrandon.potter@amd.com    pp->system = p->system;
182511886Sbrandon.potter@amd.com    /**
182611886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
182711886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
182811886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
182911886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
183011886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
183111886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
183211886Sbrandon.potter@amd.com     */
183311886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
183411886Sbrandon.potter@amd.com    Process *new_p = pp->create();
183511886Sbrandon.potter@amd.com    delete pp;
183611886Sbrandon.potter@amd.com
183711886Sbrandon.potter@amd.com    /**
183811886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
183911886Sbrandon.potter@amd.com     * close-on-exec.
184011886Sbrandon.potter@amd.com     */
184111886Sbrandon.potter@amd.com    new_p->fds = p->fds;
184211886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
184311886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
184411886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
184511886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
184611886Sbrandon.potter@amd.com    }
184711886Sbrandon.potter@amd.com
184811886Sbrandon.potter@amd.com    *new_p->sigchld = true;
184911886Sbrandon.potter@amd.com
185011886Sbrandon.potter@amd.com    delete p;
185111886Sbrandon.potter@amd.com    tc->clearArchRegs();
185211886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
185311886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
185411886Sbrandon.potter@amd.com    new_p->initState();
185511886Sbrandon.potter@amd.com    tc->activate();
185611886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
185711886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
185811886Sbrandon.potter@amd.com
185911886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
186011886Sbrandon.potter@amd.com    return 0;
186111886Sbrandon.potter@amd.com}
186211886Sbrandon.potter@amd.com
1863378SN/A/// Target getrusage() function.
1864360SN/Atemplate <class OS>
18651450SN/ASyscallReturn
186611851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
18672680Sktlim@umich.edu              ThreadContext *tc)
1868360SN/A{
18696701Sgblack@eecs.umich.edu    int index = 0;
18706701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
18716701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1872360SN/A
18733670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
18743670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1875360SN/A    rup->ru_stime.tv_sec = 0;
1876360SN/A    rup->ru_stime.tv_usec = 0;
1877360SN/A    rup->ru_maxrss = 0;
1878360SN/A    rup->ru_ixrss = 0;
1879360SN/A    rup->ru_idrss = 0;
1880360SN/A    rup->ru_isrss = 0;
1881360SN/A    rup->ru_minflt = 0;
1882360SN/A    rup->ru_majflt = 0;
1883360SN/A    rup->ru_nswap = 0;
1884360SN/A    rup->ru_inblock = 0;
1885360SN/A    rup->ru_oublock = 0;
1886360SN/A    rup->ru_msgsnd = 0;
1887360SN/A    rup->ru_msgrcv = 0;
1888360SN/A    rup->ru_nsignals = 0;
1889360SN/A    rup->ru_nvcsw = 0;
1890360SN/A    rup->ru_nivcsw = 0;
1891360SN/A
18923670Sbinkertn@umich.edu    switch (who) {
18933670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
189410796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
18958737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
18968737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
18973670Sbinkertn@umich.edu        break;
18983670Sbinkertn@umich.edu
18993670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
19003670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
19013670Sbinkertn@umich.edu        break;
19023670Sbinkertn@umich.edu
19033670Sbinkertn@umich.edu      default:
19043670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
19053670Sbinkertn@umich.edu        // plow ahead
19063670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
19073670Sbinkertn@umich.edu             who);
19083670Sbinkertn@umich.edu    }
19093670Sbinkertn@umich.edu
19108706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1911360SN/A
19121458SN/A    return 0;
1913360SN/A}
1914360SN/A
19156683Stjones1@inf.ed.ac.uk/// Target times() function.
19166683Stjones1@inf.ed.ac.uktemplate <class OS>
19176683Stjones1@inf.ed.ac.ukSyscallReturn
191811851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
191911851Sbrandon.potter@amd.com          ThreadContext *tc)
19206683Stjones1@inf.ed.ac.uk{
19216701Sgblack@eecs.umich.edu    int index = 0;
19226701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
19236683Stjones1@inf.ed.ac.uk
19246683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
19257823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
19266683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
19276683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
19286683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
19296683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
19306683Stjones1@inf.ed.ac.uk
19316683Stjones1@inf.ed.ac.uk    // Convert to host endianness
19328737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
19336683Stjones1@inf.ed.ac.uk
19346683Stjones1@inf.ed.ac.uk    // Write back
19358706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
19366683Stjones1@inf.ed.ac.uk
19376683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
19386683Stjones1@inf.ed.ac.uk    return clocks;
19396683Stjones1@inf.ed.ac.uk}
19402553SN/A
19416684Stjones1@inf.ed.ac.uk/// Target time() function.
19426684Stjones1@inf.ed.ac.uktemplate <class OS>
19436684Stjones1@inf.ed.ac.ukSyscallReturn
194411851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
19456684Stjones1@inf.ed.ac.uk{
19466684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
194710796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
19486684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
19496684Stjones1@inf.ed.ac.uk
19506701Sgblack@eecs.umich.edu    int index = 0;
19516701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
195211321Ssteve.reinhardt@amd.com    if (taddr != 0) {
19536684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
19548737Skoansin.tan@gmail.com        t = TheISA::htog(t);
19558852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
19568852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
19576684Stjones1@inf.ed.ac.uk    }
19586684Stjones1@inf.ed.ac.uk    return sec;
19596684Stjones1@inf.ed.ac.uk}
19602553SN/A
196111910SBrandon.Potter@amd.comtemplate <class OS>
196211910SBrandon.Potter@amd.comSyscallReturn
196311910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc)
196411910SBrandon.Potter@amd.com{
196511910SBrandon.Potter@amd.com    int index = 0;
196611910SBrandon.Potter@amd.com    int tgid = process->getSyscallArg(tc, index);
196711910SBrandon.Potter@amd.com    int tid = process->getSyscallArg(tc, index);
196811910SBrandon.Potter@amd.com    int sig = process->getSyscallArg(tc, index);
196911910SBrandon.Potter@amd.com
197011910SBrandon.Potter@amd.com    /**
197111910SBrandon.Potter@amd.com     * This system call is intended to allow killing a specific thread
197211910SBrandon.Potter@amd.com     * within an arbitrary thread group if sanctioned with permission checks.
197311910SBrandon.Potter@amd.com     * It's usually true that threads share the termination signal as pointed
197411910SBrandon.Potter@amd.com     * out by the pthread_kill man page and this seems to be the intended
197511910SBrandon.Potter@amd.com     * usage. Due to this being an emulated environment, assume the following:
197611910SBrandon.Potter@amd.com     * Threads are allowed to call tgkill because the EUID for all threads
197711910SBrandon.Potter@amd.com     * should be the same. There is no signal handling mechanism for kernel
197811910SBrandon.Potter@amd.com     * registration of signal handlers since signals are poorly supported in
197911910SBrandon.Potter@amd.com     * emulation mode. Since signal handlers cannot be registered, all
198011910SBrandon.Potter@amd.com     * threads within in a thread group must share the termination signal.
198111910SBrandon.Potter@amd.com     * We never exhaust PIDs so there's no chance of finding the wrong one
198211910SBrandon.Potter@amd.com     * due to PID rollover.
198311910SBrandon.Potter@amd.com     */
198411910SBrandon.Potter@amd.com
198511910SBrandon.Potter@amd.com    System *sys = tc->getSystemPtr();
198611910SBrandon.Potter@amd.com    Process *tgt_proc = nullptr;
198711910SBrandon.Potter@amd.com    for (int i = 0; i < sys->numContexts(); i++) {
198811910SBrandon.Potter@amd.com        Process *temp = sys->threadContexts[i]->getProcessPtr();
198911910SBrandon.Potter@amd.com        if (temp->pid() == tid) {
199011910SBrandon.Potter@amd.com            tgt_proc = temp;
199111910SBrandon.Potter@amd.com            break;
199211910SBrandon.Potter@amd.com        }
199311910SBrandon.Potter@amd.com    }
199411910SBrandon.Potter@amd.com
199511910SBrandon.Potter@amd.com    if (sig != 0 || sig != OS::TGT_SIGABRT)
199611910SBrandon.Potter@amd.com        return -EINVAL;
199711910SBrandon.Potter@amd.com
199811910SBrandon.Potter@amd.com    if (tgt_proc == nullptr)
199911910SBrandon.Potter@amd.com        return -ESRCH;
200011910SBrandon.Potter@amd.com
200111910SBrandon.Potter@amd.com    if (tgid != -1 && tgt_proc->tgid() != tgid)
200211910SBrandon.Potter@amd.com        return -ESRCH;
200311910SBrandon.Potter@amd.com
200411910SBrandon.Potter@amd.com    if (sig == OS::TGT_SIGABRT)
200511910SBrandon.Potter@amd.com        exitGroupFunc(desc, 252, process, tc);
200611910SBrandon.Potter@amd.com
200711910SBrandon.Potter@amd.com    return 0;
200811910SBrandon.Potter@amd.com}
200911910SBrandon.Potter@amd.com
20102553SN/A
20111354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
2012