syscall_emul.hh revision 11906
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
482764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
499202Spalle@lyckegaard.dk  defined(__FreeBSD__) || defined(__CYGWIN__) || \
509202Spalle@lyckegaard.dk  defined(__NetBSD__))
512064SN/A
5211799Sbrandon.potter@amd.com#define NO_STATFS (defined(__APPLE__) || defined(__OpenBSD__) || \
5311799Sbrandon.potter@amd.com  defined(__FreeBSD__) || defined(__NetBSD__))
5411799Sbrandon.potter@amd.com
5511799Sbrandon.potter@amd.com#define NO_FALLOCATE (defined(__APPLE__) || defined(__OpenBSD__) || \
5611799Sbrandon.potter@amd.com  defined(__FreeBSD__) || defined(__NetBSD__))
5711799Sbrandon.potter@amd.com
58360SN/A///
59360SN/A/// @file syscall_emul.hh
60360SN/A///
61360SN/A/// This file defines objects used to emulate syscalls from the target
62360SN/A/// application on the host machine.
63360SN/A
641809SN/A#ifdef __CYGWIN32__
6511800Sbrandon.potter@amd.com#include <sys/fcntl.h>
6611392Sbrandon.potter@amd.com
671809SN/A#endif
6811392Sbrandon.potter@amd.com#include <fcntl.h>
6911383Sbrandon.potter@amd.com#include <sys/mman.h>
703113Sgblack@eecs.umich.edu#include <sys/stat.h>
7111799Sbrandon.potter@amd.com#if (NO_STATFS == 0)
7211759Sbrandon.potter@amd.com#include <sys/statfs.h>
7311812Sbaz21@cam.ac.uk#else
7411812Sbaz21@cam.ac.uk#include <sys/mount.h>
7511799Sbrandon.potter@amd.com#endif
768229Snate@binkert.org#include <sys/time.h>
778229Snate@binkert.org#include <sys/uio.h>
7811594Santhony.gutierrez@amd.com#include <unistd.h>
797075Snate@binkert.org
808229Snate@binkert.org#include <cerrno>
8111856Sbrandon.potter@amd.com#include <memory>
827075Snate@binkert.org#include <string>
83360SN/A
8411886Sbrandon.potter@amd.com#include "arch/utility.hh"
8511800Sbrandon.potter@amd.com#include "base/intmath.hh"
8611392Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
872462SN/A#include "base/misc.hh"
881354SN/A#include "base/trace.hh"
896216Snate@binkert.org#include "base/types.hh"
906658Snate@binkert.org#include "config/the_isa.hh"
912474SN/A#include "cpu/base.hh"
922680Sktlim@umich.edu#include "cpu/thread_context.hh"
938229Snate@binkert.org#include "mem/page_table.hh"
9411886Sbrandon.potter@amd.com#include "params/Process.hh"
9510496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh"
968229Snate@binkert.org#include "sim/process.hh"
9711794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh"
9811886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
9910497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh"
10011794Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
101360SN/A
102360SN/A//////////////////////////////////////////////////////////////////////
103360SN/A//
104360SN/A// The following emulation functions are generic enough that they
105360SN/A// don't need to be recompiled for different emulated OS's.  They are
106360SN/A// defined in sim/syscall_emul.cc.
107360SN/A//
108360SN/A//////////////////////////////////////////////////////////////////////
109360SN/A
110360SN/A
111378SN/A/// Handler for unimplemented syscalls that we haven't thought about.
1121706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
11311851Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
114378SN/A
115378SN/A/// Handler for unimplemented syscalls that we never intend to
116378SN/A/// implement (signal handling, etc.) and should not affect the correct
117378SN/A/// behavior of the program.  Print a warning only if the appropriate
118378SN/A/// trace flag is enabled.  Return success to the target program.
1191706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
12011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
121360SN/A
12211760Sbrandon.potter@amd.com// Target fallocateFunc() handler.
12311760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num,
12411851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
12511760Sbrandon.potter@amd.com
1266109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context.
1271706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
12811851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
129378SN/A
1306109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads)
1316109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
13211851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
1336109Ssanchezd@stanford.edu
13411886Sbrandon.potter@amd.com/// Target set_tid_address() handler.
13511886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num,
13611886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
13711886Sbrandon.potter@amd.com
138378SN/A/// Target getpagesize() handler.
1391706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
14011851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
141378SN/A
1425748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address.
1435748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
14411851Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
145378SN/A
146378SN/A/// Target close() handler.
1471706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num,
14811851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
149378SN/A
15011886Sbrandon.potter@amd.com// Target read() handler.
1511706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
15211851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
153378SN/A
154378SN/A/// Target write() handler.
1551706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
15611851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
157378SN/A
158378SN/A/// Target lseek() handler.
1591706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num,
16011851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
161378SN/A
1624118Sgblack@eecs.umich.edu/// Target _llseek() handler.
1634118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
16411851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
1654118Sgblack@eecs.umich.edu
166378SN/A/// Target munmap() handler.
1671706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num,
16811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
169378SN/A
170378SN/A/// Target gethostname() handler.
1711706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
17211851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
173360SN/A
1745513SMichael.Adler@intel.com/// Target getcwd() handler.
1755513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
17611851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1775513SMichael.Adler@intel.com
17810203SAli.Saidi@ARM.com/// Target readlink() handler.
17910203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
18011851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
18110203SAli.Saidi@ARM.com                           int index = 0);
1825513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
18311851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
1845513SMichael.Adler@intel.com
185511SN/A/// Target unlink() handler.
18610633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num,
18711851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
18810633Smichaelupton@gmail.com                           int index);
1891706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
19011851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
191511SN/A
1925513SMichael.Adler@intel.com/// Target mkdir() handler.
1935513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
19411851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
1955513SMichael.Adler@intel.com
196511SN/A/// Target rename() handler.
1971706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num,
19811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
1991706SN/A
2001706SN/A
2011706SN/A/// Target truncate() handler.
2021706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num,
20311851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
2041706SN/A
2051706SN/A
2061706SN/A/// Target ftruncate() handler.
2071706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
20811851Sbrandon.potter@amd.com                            Process *p, ThreadContext *tc);
2091706SN/A
210511SN/A
2116703Svince@csl.cornell.edu/// Target truncate64() handler.
2126703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num,
21311851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2146703Svince@csl.cornell.edu
2156685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler.
2166685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
21711851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2186685Stjones1@inf.ed.ac.uk
2196685Stjones1@inf.ed.ac.uk
2205513SMichael.Adler@intel.com/// Target umask() handler.
2215513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num,
22211851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2235513SMichael.Adler@intel.com
22411885Sbrandon.potter@amd.com/// Target gettid() handler.
22511885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num,
22611885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2275513SMichael.Adler@intel.com
2281999SN/A/// Target chown() handler.
2291999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
23011851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2311999SN/A
23211885Sbrandon.potter@amd.com/// Target setpgid() handler.
23311885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num,
23411885Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2351999SN/A
2361999SN/A/// Target fchown() handler.
2371999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num,
23811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2391999SN/A
2403079Sstever@eecs.umich.edu/// Target dup() handler.
2413079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num,
24211851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2433079Sstever@eecs.umich.edu
24411875Sbrandon.potter@amd.com/// Target fcntl() handler.
2452093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num,
24611851Sbrandon.potter@amd.com                        Process *process, ThreadContext *tc);
2472093SN/A
2482687Sksewell@umich.edu/// Target fcntl64() handler.
2492687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
25011851Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
2512687Sksewell@umich.edu
2522238SN/A/// Target setuid() handler.
2532238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
25411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2552238SN/A
2562238SN/A/// Target getpid() handler.
2572238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num,
25811851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2592238SN/A
2602238SN/A/// Target getuid() handler.
2612238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
26211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2632238SN/A
2642238SN/A/// Target getgid() handler.
2652238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
26611851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2672238SN/A
2682238SN/A/// Target getppid() handler.
2692238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
27011851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2712238SN/A
2722238SN/A/// Target geteuid() handler.
2732238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
27411851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2752238SN/A
2762238SN/A/// Target getegid() handler.
2772238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
27811851Sbrandon.potter@amd.com                          Process *p, ThreadContext *tc);
2792238SN/A
2809455Smitch.hayenga+gem5@gmail.com/// Target access() handler
2819455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
28211851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
28310203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
28411851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc,
28511851Sbrandon.potter@amd.com                         int index);
2869455Smitch.hayenga+gem5@gmail.com
2879112Smarc.orr@gmail.com/// Futex system call
28811906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez
28911906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps
2909112Smarc.orr@gmail.comtemplate <class OS>
2919112Smarc.orr@gmail.comSyscallReturn
29211851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process,
2939112Smarc.orr@gmail.com          ThreadContext *tc)
2949112Smarc.orr@gmail.com{
2959112Smarc.orr@gmail.com    int index_uaddr = 0;
2969112Smarc.orr@gmail.com    int index_op = 1;
2979112Smarc.orr@gmail.com    int index_val = 2;
2989112Smarc.orr@gmail.com    int index_timeout = 3;
2999112Smarc.orr@gmail.com
3009112Smarc.orr@gmail.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
3019112Smarc.orr@gmail.com    int op = process->getSyscallArg(tc, index_op);
3029112Smarc.orr@gmail.com    int val = process->getSyscallArg(tc, index_val);
3039112Smarc.orr@gmail.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3049112Smarc.orr@gmail.com
3059112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
3069112Smarc.orr@gmail.com        &futex_map = tc->getSystemPtr()->futexMap;
3079112Smarc.orr@gmail.com
30811886Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "futex: Address=%llx, op=%d, val=%d\n",
3099112Smarc.orr@gmail.com            uaddr, op, val);
3109112Smarc.orr@gmail.com
3119238Slluc.alvarez@bsc.es    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3129112Smarc.orr@gmail.com
3139112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3149112Smarc.orr@gmail.com        if (timeout != 0) {
31511886Sbrandon.potter@amd.com            warn("futex: FUTEX_WAIT with non-null timeout unimplemented;"
3169112Smarc.orr@gmail.com                 "we'll wait indefinitely");
3179112Smarc.orr@gmail.com        }
3189112Smarc.orr@gmail.com
3199112Smarc.orr@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3209112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3219112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
32211367Sandreas.hansson@arm.com        delete[] buf;
3239112Smarc.orr@gmail.com
32411321Ssteve.reinhardt@amd.com        if (val != mem_val) {
32511886Sbrandon.potter@amd.com            DPRINTF(SyscallVerbose, "futex: FUTEX_WAKE, read: %d, "
3269112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3279112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3289112Smarc.orr@gmail.com        }
3299112Smarc.orr@gmail.com
3309112Smarc.orr@gmail.com        // Queue the thread context
3319112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3329112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3339112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3349112Smarc.orr@gmail.com        } else {
3359112Smarc.orr@gmail.com            tcWaitList = new std::list<ThreadContext *>();
3369112Smarc.orr@gmail.com            futex_map.insert(std::pair< uint64_t,
3379112Smarc.orr@gmail.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3389112Smarc.orr@gmail.com        }
3399112Smarc.orr@gmail.com        tcWaitList->push_back(tc);
34011886Sbrandon.potter@amd.com        DPRINTF(SyscallVerbose, "futex: FUTEX_WAIT, suspending calling thread "
34111886Sbrandon.potter@amd.com                                "context on address 0x%lx\n", uaddr);
3429112Smarc.orr@gmail.com        tc->suspend();
3439112Smarc.orr@gmail.com        return 0;
3449112Smarc.orr@gmail.com    } else if (op == OS::TGT_FUTEX_WAKE){
3459112Smarc.orr@gmail.com        int wokenUp = 0;
3469112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3479112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3489112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
3499112Smarc.orr@gmail.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3509112Smarc.orr@gmail.com                tcWaitList->front()->activate();
3519112Smarc.orr@gmail.com                tcWaitList->pop_front();
3529112Smarc.orr@gmail.com                wokenUp++;
3539112Smarc.orr@gmail.com            }
35411321Ssteve.reinhardt@amd.com            if (tcWaitList->empty()) {
3559112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3569112Smarc.orr@gmail.com                delete tcWaitList;
3579112Smarc.orr@gmail.com            }
3589112Smarc.orr@gmail.com        }
35911886Sbrandon.potter@amd.com        DPRINTF(SyscallVerbose, "futex: FUTEX_WAKE, activated %d waiting "
36011886Sbrandon.potter@amd.com                                "thread context on address 0x%lx\n",
36111886Sbrandon.potter@amd.com                                wokenUp, uaddr);
3629112Smarc.orr@gmail.com        return wokenUp;
3639112Smarc.orr@gmail.com    } else {
36411886Sbrandon.potter@amd.com        warn("futex: op %d is not implemented, just returning...", op);
3659112Smarc.orr@gmail.com        return 0;
3669112Smarc.orr@gmail.com    }
3679112Smarc.orr@gmail.com
3689112Smarc.orr@gmail.com}
3699112Smarc.orr@gmail.com
3702238SN/A
3712238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3722238SN/A/// returning a second value in a register other than the normal return register
3732238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
37411851Sbrandon.potter@amd.com                             Process *process, ThreadContext *tc);
3752238SN/A
3762238SN/A/// Target getpidPseudo() handler.
3772238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
37811851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3792238SN/A
3802238SN/A/// Target getuidPseudo() handler.
3812238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
38211851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3832238SN/A
3842238SN/A/// Target getgidPseudo() handler.
3852238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
38611851Sbrandon.potter@amd.com                               Process *p, ThreadContext *tc);
3872238SN/A
3882238SN/A
3891354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
3901354SN/Aconst int one_million = 1000000;
39110796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
39210796Sbrandon.potter@amd.comconst int one_billion = 1000000000;
3931354SN/A
3941354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3951354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3961354SN/A/// real-world time) to keep simulations repeatable.
3971354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3981354SN/A
3991354SN/A/// Helper function to convert current elapsed time to seconds and
4001354SN/A/// microseconds.
4011354SN/Atemplate <class T1, class T2>
4021354SN/Avoid
40310796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec)
4041354SN/A{
40510796Sbrandon.potter@amd.com    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
4061354SN/A    sec = elapsed_usecs / one_million;
4071354SN/A    usec = elapsed_usecs % one_million;
4081354SN/A}
4091354SN/A
41010796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
41110796Sbrandon.potter@amd.com/// nanoseconds.
41210796Sbrandon.potter@amd.comtemplate <class T1, class T2>
41310796Sbrandon.potter@amd.comvoid
41410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
41510796Sbrandon.potter@amd.com{
41610796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
41710796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
41810796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
41910796Sbrandon.potter@amd.com}
42010796Sbrandon.potter@amd.com
421360SN/A//////////////////////////////////////////////////////////////////////
422360SN/A//
423360SN/A// The following emulation functions are generic, but need to be
424360SN/A// templated to account for differences in types, constants, etc.
425360SN/A//
426360SN/A//////////////////////////////////////////////////////////////////////
427360SN/A
42811759Sbrandon.potter@amd.com    typedef struct statfs hst_statfs;
4293113Sgblack@eecs.umich.edu#if NO_STAT64
4303113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4313113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4323113Sgblack@eecs.umich.edu#else
4333113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4343113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4353113Sgblack@eecs.umich.edu#endif
4363113Sgblack@eecs.umich.edu
4373113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4383113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4393113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4403113Sgblack@eecs.umich.edu
4413113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4423113Sgblack@eecs.umich.edustatic void
4433113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4443113Sgblack@eecs.umich.edu{
4454189Sgblack@eecs.umich.edu    using namespace TheISA;
4464189Sgblack@eecs.umich.edu
4473113Sgblack@eecs.umich.edu    if (fakeTTY)
4483113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4493113Sgblack@eecs.umich.edu    else
4503113Sgblack@eecs.umich.edu        tgt->st_dev = host->st_dev;
4518737Skoansin.tan@gmail.com    tgt->st_dev = TheISA::htog(tgt->st_dev);
4523113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4538737Skoansin.tan@gmail.com    tgt->st_ino = TheISA::htog(tgt->st_ino);
4543277Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4555515SMichael.Adler@intel.com    if (fakeTTY) {
4565515SMichael.Adler@intel.com        // Claim to be a character device
4575515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4585515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4595515SMichael.Adler@intel.com    }
4608737Skoansin.tan@gmail.com    tgt->st_mode = TheISA::htog(tgt->st_mode);
4613277Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4628737Skoansin.tan@gmail.com    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4633277Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4648737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4653277Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4668737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4673113Sgblack@eecs.umich.edu    if (fakeTTY)
4683113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4693113Sgblack@eecs.umich.edu    else
4703113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4718737Skoansin.tan@gmail.com    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4723113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4738737Skoansin.tan@gmail.com    tgt->st_size = TheISA::htog(tgt->st_size);
4743114Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
4758737Skoansin.tan@gmail.com    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4763114Sgblack@eecs.umich.edu    tgt->st_mtimeX = host->st_mtime;
4778737Skoansin.tan@gmail.com    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4783114Sgblack@eecs.umich.edu    tgt->st_ctimeX = host->st_ctime;
4798737Skoansin.tan@gmail.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
48011906SBrandon.Potter@amd.com    // Force the block size to be 8KB. This helps to ensure buffered io works
4814061Sgblack@eecs.umich.edu    // consistently across different hosts.
4824061Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
4838737Skoansin.tan@gmail.com    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4843113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
4858737Skoansin.tan@gmail.com    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4863113Sgblack@eecs.umich.edu}
4873113Sgblack@eecs.umich.edu
4883113Sgblack@eecs.umich.edu// Same for stat64
4893113Sgblack@eecs.umich.edu
4903113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64>
4913113Sgblack@eecs.umich.edustatic void
4923113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4933113Sgblack@eecs.umich.edu{
4944189Sgblack@eecs.umich.edu    using namespace TheISA;
4954189Sgblack@eecs.umich.edu
4963113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4973113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4983113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
4998737Skoansin.tan@gmail.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
5003113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = host->st_mtime_nsec;
5018737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
5023113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = host->st_ctime_nsec;
5038737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
5043113Sgblack@eecs.umich.edu#else
5053113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
5063113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
5073113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
5083113Sgblack@eecs.umich.edu#endif
5093113Sgblack@eecs.umich.edu}
5103113Sgblack@eecs.umich.edu
51111906SBrandon.Potter@amd.com// Here are a couple of convenience functions
5123113Sgblack@eecs.umich.edutemplate<class OS>
5133113Sgblack@eecs.umich.edustatic void
5148852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
51511906SBrandon.Potter@amd.com               hst_stat *host, bool fakeTTY = false)
5163113Sgblack@eecs.umich.edu{
5173113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5183113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5193113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5203113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5213113Sgblack@eecs.umich.edu}
5223113Sgblack@eecs.umich.edu
5233113Sgblack@eecs.umich.edutemplate<class OS>
5243113Sgblack@eecs.umich.edustatic void
5258852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
52611906SBrandon.Potter@amd.com                 hst_stat64 *host, bool fakeTTY = false)
5273113Sgblack@eecs.umich.edu{
5283113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5293113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5306686Stjones1@inf.ed.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5313113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5323113Sgblack@eecs.umich.edu}
5333113Sgblack@eecs.umich.edu
53411759Sbrandon.potter@amd.comtemplate <class OS>
53511759Sbrandon.potter@amd.comstatic void
53611759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
53711759Sbrandon.potter@amd.com                 hst_statfs *host)
53811759Sbrandon.potter@amd.com{
53911759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
54011759Sbrandon.potter@amd.com
54111812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
54211812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
54311812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_iosize);
54411759Sbrandon.potter@amd.com#else
54511812Sbaz21@cam.ac.uk    tgt->f_bsize = TheISA::htog(host->f_bsize);
54611759Sbrandon.potter@amd.com#endif
54711759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
54811759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
54911759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
55011759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
55111759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
55211759Sbrandon.potter@amd.com    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
55311812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
55411812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
55511812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
55611812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
55711812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
55811812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
55911812Sbaz21@cam.ac.uk#else
56011759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
56111759Sbrandon.potter@amd.com    tgt->f_frsize = TheISA::htog(host->f_frsize);
56211812Sbaz21@cam.ac.uk#endif
56311812Sbaz21@cam.ac.uk#if defined(__linux__)
56411759Sbrandon.potter@amd.com    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
56511812Sbaz21@cam.ac.uk#else
56611812Sbaz21@cam.ac.uk    /*
56711812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
56811812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
56911812Sbaz21@cam.ac.uk     */
57011812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
57111812Sbaz21@cam.ac.uk#endif
57211759Sbrandon.potter@amd.com
57311759Sbrandon.potter@amd.com    tgt.copyOut(mem);
57411759Sbrandon.potter@amd.com}
57511759Sbrandon.potter@amd.com
576378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
577378SN/A/// only to find out if their stdout is a tty, to determine whether to
5789141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
5799141Smarc.orr@gmail.com/// not TTYs to provide repeatable results.
580360SN/Atemplate <class OS>
5811450SN/ASyscallReturn
58211856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
583360SN/A{
5846701Sgblack@eecs.umich.edu    int index = 0;
58511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
58611856Sbrandon.potter@amd.com    unsigned req = p->getSyscallArg(tc, index);
587360SN/A
58810930Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
589360SN/A
59011856Sbrandon.potter@amd.com    if (OS::isTtyReq(req))
59111856Sbrandon.potter@amd.com        return -ENOTTY;
59210496Ssteve.reinhardt@amd.com
59311856Sbrandon.potter@amd.com    auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]);
59411856Sbrandon.potter@amd.com    if (!dfdp)
5951458SN/A        return -EBADF;
596360SN/A
59711856Sbrandon.potter@amd.com    /**
59811856Sbrandon.potter@amd.com     * If the driver is valid, issue the ioctl through it. Otherwise,
59911856Sbrandon.potter@amd.com     * there's an implicit assumption that the device is a TTY type and we
60011856Sbrandon.potter@amd.com     * return that we do not have a valid TTY.
60111856Sbrandon.potter@amd.com     */
60211856Sbrandon.potter@amd.com    EmulatedDriver *emul_driver = dfdp->getDriver();
60311856Sbrandon.potter@amd.com    if (emul_driver)
60411856Sbrandon.potter@amd.com        return emul_driver->ioctl(p, tc, req);
60510496Ssteve.reinhardt@amd.com
60611856Sbrandon.potter@amd.com    /**
60711856Sbrandon.potter@amd.com     * For lack of a better return code, return ENOTTY. Ideally, we should
60811856Sbrandon.potter@amd.com     * return something better here, but at least we issue the warning.
60911856Sbrandon.potter@amd.com     */
61011856Sbrandon.potter@amd.com    warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n",
61110930Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
6129141Smarc.orr@gmail.com    return -ENOTTY;
613360SN/A}
614360SN/A
615360SN/Atemplate <class OS>
61610027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn
61711851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
61810027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc, int index)
619360SN/A{
620360SN/A    std::string path;
621360SN/A
6228852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
6236701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
6241458SN/A        return -EFAULT;
625360SN/A
6266701Sgblack@eecs.umich.edu    int tgtFlags = process->getSyscallArg(tc, index);
6276701Sgblack@eecs.umich.edu    int mode = process->getSyscallArg(tc, index);
628360SN/A    int hostFlags = 0;
629360SN/A
630360SN/A    // translate open flags
631360SN/A    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
632360SN/A        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
633360SN/A            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
634360SN/A            hostFlags |= OS::openFlagTable[i].hostFlag;
635360SN/A        }
636360SN/A    }
637360SN/A
638360SN/A    // any target flags left?
639360SN/A    if (tgtFlags != 0)
6401706SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
641360SN/A
642360SN/A#ifdef __CYGWIN32__
643360SN/A    hostFlags |= O_BINARY;
644360SN/A#endif
645360SN/A
6463669Sbinkertn@umich.edu    // Adjust path for current working directory
6473669Sbinkertn@umich.edu    path = process->fullPath(path);
6483669Sbinkertn@umich.edu
6491706SN/A    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
6501706SN/A
65110496Ssteve.reinhardt@amd.com    if (startswith(path, "/dev/")) {
65210496Ssteve.reinhardt@amd.com        std::string filename = path.substr(strlen("/dev/"));
65310496Ssteve.reinhardt@amd.com        if (filename == "sysdev0") {
65410496Ssteve.reinhardt@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
65510496Ssteve.reinhardt@amd.com            // We don't support it, so just punt.
65610496Ssteve.reinhardt@amd.com            warn("Ignoring open(%s, ...)\n", path);
65710496Ssteve.reinhardt@amd.com            return -ENOENT;
65810496Ssteve.reinhardt@amd.com        }
65910496Ssteve.reinhardt@amd.com
66010496Ssteve.reinhardt@amd.com        EmulatedDriver *drv = process->findDriver(filename);
66111856Sbrandon.potter@amd.com        if (drv) {
66210496Ssteve.reinhardt@amd.com            // the driver's open method will allocate a fd from the
66310496Ssteve.reinhardt@amd.com            // process if necessary.
66410496Ssteve.reinhardt@amd.com            return drv->open(process, tc, mode, hostFlags);
66510496Ssteve.reinhardt@amd.com        }
66610496Ssteve.reinhardt@amd.com
66710496Ssteve.reinhardt@amd.com        // fall through here for pass through to host devices, such as
66810496Ssteve.reinhardt@amd.com        // /dev/zero
66910496Ssteve.reinhardt@amd.com    }
67010496Ssteve.reinhardt@amd.com
6715795Ssaidi@eecs.umich.edu    int fd;
6729143Ssteve.reinhardt@amd.com    int local_errno;
6739142Ssteve.reinhardt@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6749142Ssteve.reinhardt@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
6759143Ssteve.reinhardt@amd.com        // It's a proc/sys entry and requires special handling
6765795Ssaidi@eecs.umich.edu        fd = OS::openSpecialFile(path, process, tc);
6779143Ssteve.reinhardt@amd.com        local_errno = ENOENT;
6785795Ssaidi@eecs.umich.edu     } else {
6795795Ssaidi@eecs.umich.edu        // open the file
6805795Ssaidi@eecs.umich.edu        fd = open(path.c_str(), hostFlags, mode);
6819143Ssteve.reinhardt@amd.com        local_errno = errno;
6825795Ssaidi@eecs.umich.edu     }
683360SN/A
6849143Ssteve.reinhardt@amd.com    if (fd == -1)
6859143Ssteve.reinhardt@amd.com        return -local_errno;
6869143Ssteve.reinhardt@amd.com
68711856Sbrandon.potter@amd.com    std::shared_ptr<FileFDEntry> ffdp =
68811856Sbrandon.potter@amd.com        std::make_shared<FileFDEntry>(fd, hostFlags, path.c_str(), false);
68911856Sbrandon.potter@amd.com    return process->fds->allocFD(ffdp);
690360SN/A}
691360SN/A
69210027SChris.Adeniyi-Jones@arm.com/// Target open() handler.
69310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
69410027SChris.Adeniyi-Jones@arm.comSyscallReturn
69511851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
69610027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
69710027SChris.Adeniyi-Jones@arm.com{
69810027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 0);
69910027SChris.Adeniyi-Jones@arm.com}
70010027SChris.Adeniyi-Jones@arm.com
70110027SChris.Adeniyi-Jones@arm.com/// Target openat() handler.
70210027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
70310027SChris.Adeniyi-Jones@arm.comSyscallReturn
70411851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
70511851Sbrandon.potter@amd.com           ThreadContext *tc)
70610027SChris.Adeniyi-Jones@arm.com{
70710027SChris.Adeniyi-Jones@arm.com    int index = 0;
70810027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
70910027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
71010027SChris.Adeniyi-Jones@arm.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
71110027SChris.Adeniyi-Jones@arm.com    return openFunc<OS>(desc, callnum, process, tc, 1);
71210027SChris.Adeniyi-Jones@arm.com}
71310027SChris.Adeniyi-Jones@arm.com
71410633Smichaelupton@gmail.com/// Target unlinkat() handler.
71510633Smichaelupton@gmail.comtemplate <class OS>
71610633Smichaelupton@gmail.comSyscallReturn
71711851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
71810633Smichaelupton@gmail.com             ThreadContext *tc)
71910633Smichaelupton@gmail.com{
72010633Smichaelupton@gmail.com    int index = 0;
72110633Smichaelupton@gmail.com    int dirfd = process->getSyscallArg(tc, index);
72210633Smichaelupton@gmail.com    if (dirfd != OS::TGT_AT_FDCWD)
72310633Smichaelupton@gmail.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
72410633Smichaelupton@gmail.com
72510633Smichaelupton@gmail.com    return unlinkHelper(desc, callnum, process, tc, 1);
72610633Smichaelupton@gmail.com}
72710633Smichaelupton@gmail.com
72810203SAli.Saidi@ARM.com/// Target facessat() handler
72910203SAli.Saidi@ARM.comtemplate <class OS>
73010203SAli.Saidi@ARM.comSyscallReturn
73111851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
73211851Sbrandon.potter@amd.com              ThreadContext *tc)
73310203SAli.Saidi@ARM.com{
73410203SAli.Saidi@ARM.com    int index = 0;
73510203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
73610203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
73710203SAli.Saidi@ARM.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
73810203SAli.Saidi@ARM.com    return accessFunc(desc, callnum, process, tc, 1);
73910203SAli.Saidi@ARM.com}
74010203SAli.Saidi@ARM.com
74110203SAli.Saidi@ARM.com/// Target readlinkat() handler
74210203SAli.Saidi@ARM.comtemplate <class OS>
74310203SAli.Saidi@ARM.comSyscallReturn
74411851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
74511851Sbrandon.potter@amd.com               ThreadContext *tc)
74610203SAli.Saidi@ARM.com{
74710203SAli.Saidi@ARM.com    int index = 0;
74810203SAli.Saidi@ARM.com    int dirfd = process->getSyscallArg(tc, index);
74910203SAli.Saidi@ARM.com    if (dirfd != OS::TGT_AT_FDCWD)
75010203SAli.Saidi@ARM.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
75110203SAli.Saidi@ARM.com    return readlinkFunc(desc, callnum, process, tc, 1);
75210203SAli.Saidi@ARM.com}
75310203SAli.Saidi@ARM.com
75410850SGiacomo.Gabrielli@arm.com/// Target renameat() handler.
75510850SGiacomo.Gabrielli@arm.comtemplate <class OS>
75610850SGiacomo.Gabrielli@arm.comSyscallReturn
75711851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
75810850SGiacomo.Gabrielli@arm.com             ThreadContext *tc)
75910850SGiacomo.Gabrielli@arm.com{
76010850SGiacomo.Gabrielli@arm.com    int index = 0;
76110850SGiacomo.Gabrielli@arm.com
76210850SGiacomo.Gabrielli@arm.com    int olddirfd = process->getSyscallArg(tc, index);
76310850SGiacomo.Gabrielli@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
76410850SGiacomo.Gabrielli@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
76510850SGiacomo.Gabrielli@arm.com
76610850SGiacomo.Gabrielli@arm.com    std::string old_name;
76710850SGiacomo.Gabrielli@arm.com
76810850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
76910850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
77010850SGiacomo.Gabrielli@arm.com        return -EFAULT;
77110850SGiacomo.Gabrielli@arm.com
77210850SGiacomo.Gabrielli@arm.com    int newdirfd = process->getSyscallArg(tc, index);
77310850SGiacomo.Gabrielli@arm.com    if (newdirfd != OS::TGT_AT_FDCWD)
77410850SGiacomo.Gabrielli@arm.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
77510850SGiacomo.Gabrielli@arm.com
77610850SGiacomo.Gabrielli@arm.com    std::string new_name;
77710850SGiacomo.Gabrielli@arm.com
77810850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
77910850SGiacomo.Gabrielli@arm.com                                         process->getSyscallArg(tc, index)))
78010850SGiacomo.Gabrielli@arm.com        return -EFAULT;
78110850SGiacomo.Gabrielli@arm.com
78210850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
78310850SGiacomo.Gabrielli@arm.com    old_name = process->fullPath(old_name);
78410850SGiacomo.Gabrielli@arm.com    new_name = process->fullPath(new_name);
78510850SGiacomo.Gabrielli@arm.com
78610850SGiacomo.Gabrielli@arm.com    int result = rename(old_name.c_str(), new_name.c_str());
78710850SGiacomo.Gabrielli@arm.com    return (result == -1) ? -errno : result;
78810850SGiacomo.Gabrielli@arm.com}
78910850SGiacomo.Gabrielli@arm.com
7906640Svince@csl.cornell.edu/// Target sysinfo() handler.
7916640Svince@csl.cornell.edutemplate <class OS>
7926640Svince@csl.cornell.eduSyscallReturn
79311851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
79411851Sbrandon.potter@amd.com            ThreadContext *tc)
7956640Svince@csl.cornell.edu{
7966640Svince@csl.cornell.edu
7976701Sgblack@eecs.umich.edu    int index = 0;
7986701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tgt_sysinfo>
79910793Sbrandon.potter@amd.com        sysinfo(process->getSyscallArg(tc, index));
8006640Svince@csl.cornell.edu
80111758Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
80211758Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
80311758Sbrandon.potter@amd.com    sysinfo->mem_unit = 1;
8046640Svince@csl.cornell.edu
8058706Sandreas.hansson@arm.com    sysinfo.copyOut(tc->getMemProxy());
8066640Svince@csl.cornell.edu
8076701Sgblack@eecs.umich.edu    return 0;
8086640Svince@csl.cornell.edu}
809360SN/A
8101999SN/A/// Target chmod() handler.
8111999SN/Atemplate <class OS>
8121999SN/ASyscallReturn
81311851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
8142680Sktlim@umich.edu          ThreadContext *tc)
8151999SN/A{
8161999SN/A    std::string path;
8171999SN/A
8186701Sgblack@eecs.umich.edu    int index = 0;
8198852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
8206701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
8211999SN/A        return -EFAULT;
8226701Sgblack@eecs.umich.edu    }
8231999SN/A
8246701Sgblack@eecs.umich.edu    uint32_t mode = process->getSyscallArg(tc, index);
8251999SN/A    mode_t hostMode = 0;
8261999SN/A
8271999SN/A    // XXX translate mode flags via OS::something???
8281999SN/A    hostMode = mode;
8291999SN/A
8303669Sbinkertn@umich.edu    // Adjust path for current working directory
8313669Sbinkertn@umich.edu    path = process->fullPath(path);
8323669Sbinkertn@umich.edu
8331999SN/A    // do the chmod
8341999SN/A    int result = chmod(path.c_str(), hostMode);
8351999SN/A    if (result < 0)
8362218SN/A        return -errno;
8371999SN/A
8381999SN/A    return 0;
8391999SN/A}
8401999SN/A
8411999SN/A
8421999SN/A/// Target fchmod() handler.
8431999SN/Atemplate <class OS>
8441999SN/ASyscallReturn
84511856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
8461999SN/A{
8476701Sgblack@eecs.umich.edu    int index = 0;
84811856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
84911856Sbrandon.potter@amd.com    uint32_t mode = p->getSyscallArg(tc, index);
85010931Sbrandon.potter@amd.com
85111856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
85211856Sbrandon.potter@amd.com    if (!ffdp)
8531999SN/A        return -EBADF;
85411856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
8551999SN/A
85611856Sbrandon.potter@amd.com    mode_t hostMode = mode;
8571999SN/A
85811856Sbrandon.potter@amd.com    int result = fchmod(sim_fd, hostMode);
8591999SN/A
86011856Sbrandon.potter@amd.com    return (result < 0) ? -errno : 0;
8611999SN/A}
8621999SN/A
8635877Shsul@eecs.umich.edu/// Target mremap() handler.
8645877Shsul@eecs.umich.edutemplate <class OS>
8655877Shsul@eecs.umich.eduSyscallReturn
86611851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
8675877Shsul@eecs.umich.edu{
8686701Sgblack@eecs.umich.edu    int index = 0;
8696701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8706701Sgblack@eecs.umich.edu    uint64_t old_length = process->getSyscallArg(tc, index);
8716701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8726701Sgblack@eecs.umich.edu    uint64_t flags = process->getSyscallArg(tc, index);
87310027SChris.Adeniyi-Jones@arm.com    uint64_t provided_address = 0;
87410027SChris.Adeniyi-Jones@arm.com    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
87510027SChris.Adeniyi-Jones@arm.com
87610027SChris.Adeniyi-Jones@arm.com    if (use_provided_address)
87710027SChris.Adeniyi-Jones@arm.com        provided_address = process->getSyscallArg(tc, index);
8785877Shsul@eecs.umich.edu
87910318Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
88010318Sandreas.hansson@arm.com        (provided_address % TheISA::PageBytes != 0)) {
8815877Shsul@eecs.umich.edu        warn("mremap failing: arguments not page aligned");
8825877Shsul@eecs.umich.edu        return -EINVAL;
8835877Shsul@eecs.umich.edu    }
8845877Shsul@eecs.umich.edu
88510486Stjablin@gmail.com    new_length = roundUp(new_length, TheISA::PageBytes);
88610486Stjablin@gmail.com
8875877Shsul@eecs.umich.edu    if (new_length > old_length) {
88811905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = process->memState;
88911905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
89011905SBrandon.Potter@amd.com
89111905SBrandon.Potter@amd.com        if ((start + old_length) == mmap_end &&
89210027SChris.Adeniyi-Jones@arm.com            (!use_provided_address || provided_address == start)) {
8935877Shsul@eecs.umich.edu            uint64_t diff = new_length - old_length;
89411905SBrandon.Potter@amd.com            process->allocateMem(mmap_end, diff);
89511905SBrandon.Potter@amd.com            mem_state->setMmapEnd(mmap_end + diff);
8965877Shsul@eecs.umich.edu            return start;
8975877Shsul@eecs.umich.edu        } else {
89810027SChris.Adeniyi-Jones@arm.com            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8995877Shsul@eecs.umich.edu                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
9005877Shsul@eecs.umich.edu                return -ENOMEM;
9015877Shsul@eecs.umich.edu            } else {
90210027SChris.Adeniyi-Jones@arm.com                uint64_t new_start = use_provided_address ?
90311905SBrandon.Potter@amd.com                    provided_address : mmap_end;
90410027SChris.Adeniyi-Jones@arm.com                process->pTable->remap(start, old_length, new_start);
90510027SChris.Adeniyi-Jones@arm.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
90610027SChris.Adeniyi-Jones@arm.com                     new_start, new_start + new_length,
90710027SChris.Adeniyi-Jones@arm.com                     new_length - old_length);
9085877Shsul@eecs.umich.edu                // add on the remaining unallocated pages
90910027SChris.Adeniyi-Jones@arm.com                process->allocateMem(new_start + old_length,
91010027SChris.Adeniyi-Jones@arm.com                                     new_length - old_length,
91110027SChris.Adeniyi-Jones@arm.com                                     use_provided_address /* clobber */);
91210027SChris.Adeniyi-Jones@arm.com                if (!use_provided_address)
91311905SBrandon.Potter@amd.com                    mem_state->setMmapEnd(mmap_end + new_length);
91410027SChris.Adeniyi-Jones@arm.com                if (use_provided_address &&
91511905SBrandon.Potter@amd.com                    new_start + new_length > mem_state->getMmapEnd()) {
91610027SChris.Adeniyi-Jones@arm.com                    // something fishy going on here, at least notify the user
91710027SChris.Adeniyi-Jones@arm.com                    // @todo: increase mmap_end?
91810027SChris.Adeniyi-Jones@arm.com                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
91910027SChris.Adeniyi-Jones@arm.com                }
92010027SChris.Adeniyi-Jones@arm.com                warn("returning %08p as start\n", new_start);
92110027SChris.Adeniyi-Jones@arm.com                return new_start;
9225877Shsul@eecs.umich.edu            }
9235877Shsul@eecs.umich.edu        }
9245877Shsul@eecs.umich.edu    } else {
92510027SChris.Adeniyi-Jones@arm.com        if (use_provided_address && provided_address != start)
92610027SChris.Adeniyi-Jones@arm.com            process->pTable->remap(start, new_length, provided_address);
9278601Ssteve.reinhardt@amd.com        process->pTable->unmap(start + new_length, old_length - new_length);
92810027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
9295877Shsul@eecs.umich.edu    }
9305877Shsul@eecs.umich.edu}
9311999SN/A
932378SN/A/// Target stat() handler.
933360SN/Atemplate <class OS>
9341450SN/ASyscallReturn
93511851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process,
9362680Sktlim@umich.edu         ThreadContext *tc)
937360SN/A{
938360SN/A    std::string path;
939360SN/A
9406701Sgblack@eecs.umich.edu    int index = 0;
9418852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9426701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
9436701Sgblack@eecs.umich.edu        return -EFAULT;
9446701Sgblack@eecs.umich.edu    }
9456701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
946360SN/A
9473669Sbinkertn@umich.edu    // Adjust path for current working directory
9483669Sbinkertn@umich.edu    path = process->fullPath(path);
9493669Sbinkertn@umich.edu
950360SN/A    struct stat hostBuf;
951360SN/A    int result = stat(path.c_str(), &hostBuf);
952360SN/A
953360SN/A    if (result < 0)
9542218SN/A        return -errno;
955360SN/A
9568706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
957360SN/A
9581458SN/A    return 0;
959360SN/A}
960360SN/A
961360SN/A
9625074Ssaidi@eecs.umich.edu/// Target stat64() handler.
9635074Ssaidi@eecs.umich.edutemplate <class OS>
9645074Ssaidi@eecs.umich.eduSyscallReturn
96511851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process,
9665074Ssaidi@eecs.umich.edu           ThreadContext *tc)
9675074Ssaidi@eecs.umich.edu{
9685074Ssaidi@eecs.umich.edu    std::string path;
9695074Ssaidi@eecs.umich.edu
9706701Sgblack@eecs.umich.edu    int index = 0;
9718852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
9726701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
9735074Ssaidi@eecs.umich.edu        return -EFAULT;
9746701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9755074Ssaidi@eecs.umich.edu
9765074Ssaidi@eecs.umich.edu    // Adjust path for current working directory
9775074Ssaidi@eecs.umich.edu    path = process->fullPath(path);
9785074Ssaidi@eecs.umich.edu
9795208Ssaidi@eecs.umich.edu#if NO_STAT64
9805208Ssaidi@eecs.umich.edu    struct stat  hostBuf;
9815208Ssaidi@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
9825208Ssaidi@eecs.umich.edu#else
9835074Ssaidi@eecs.umich.edu    struct stat64 hostBuf;
9845074Ssaidi@eecs.umich.edu    int result = stat64(path.c_str(), &hostBuf);
9855208Ssaidi@eecs.umich.edu#endif
9865074Ssaidi@eecs.umich.edu
9875074Ssaidi@eecs.umich.edu    if (result < 0)
9885074Ssaidi@eecs.umich.edu        return -errno;
9895074Ssaidi@eecs.umich.edu
9908706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9915074Ssaidi@eecs.umich.edu
9925074Ssaidi@eecs.umich.edu    return 0;
9935074Ssaidi@eecs.umich.edu}
9945074Ssaidi@eecs.umich.edu
9955074Ssaidi@eecs.umich.edu
99610027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler.
99710027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
99810027SChris.Adeniyi-Jones@arm.comSyscallReturn
99911851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
100010027SChris.Adeniyi-Jones@arm.com              ThreadContext *tc)
100110027SChris.Adeniyi-Jones@arm.com{
100210027SChris.Adeniyi-Jones@arm.com    int index = 0;
100310027SChris.Adeniyi-Jones@arm.com    int dirfd = process->getSyscallArg(tc, index);
100410027SChris.Adeniyi-Jones@arm.com    if (dirfd != OS::TGT_AT_FDCWD)
100510793Sbrandon.potter@amd.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
100610027SChris.Adeniyi-Jones@arm.com
100710027SChris.Adeniyi-Jones@arm.com    std::string path;
100810027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
100910027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
101010027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
101110027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
101210027SChris.Adeniyi-Jones@arm.com
101310027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
101410027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
101510027SChris.Adeniyi-Jones@arm.com
101610027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
101710027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
101810027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
101910027SChris.Adeniyi-Jones@arm.com#else
102010027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
102110027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
102210027SChris.Adeniyi-Jones@arm.com#endif
102310027SChris.Adeniyi-Jones@arm.com
102410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
102510027SChris.Adeniyi-Jones@arm.com        return -errno;
102610027SChris.Adeniyi-Jones@arm.com
102710027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
102810027SChris.Adeniyi-Jones@arm.com
102910027SChris.Adeniyi-Jones@arm.com    return 0;
103010027SChris.Adeniyi-Jones@arm.com}
103110027SChris.Adeniyi-Jones@arm.com
103210027SChris.Adeniyi-Jones@arm.com
10331999SN/A/// Target fstat64() handler.
10341999SN/Atemplate <class OS>
10351999SN/ASyscallReturn
103611856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
10371999SN/A{
10386701Sgblack@eecs.umich.edu    int index = 0;
103911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
104011856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
104110931Sbrandon.potter@amd.com
104211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
104311856Sbrandon.potter@amd.com    if (!ffdp)
10441999SN/A        return -EBADF;
104511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
10461999SN/A
10472764Sstever@eecs.umich.edu#if NO_STAT64
10482064SN/A    struct stat  hostBuf;
104910931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
10502064SN/A#else
10512064SN/A    struct stat64  hostBuf;
105210931Sbrandon.potter@amd.com    int result = fstat64(sim_fd, &hostBuf);
10532064SN/A#endif
10541999SN/A
10551999SN/A    if (result < 0)
10562218SN/A        return -errno;
10571999SN/A
105810931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10591999SN/A
10601999SN/A    return 0;
10611999SN/A}
10621999SN/A
10631999SN/A
1064378SN/A/// Target lstat() handler.
1065360SN/Atemplate <class OS>
10661450SN/ASyscallReturn
106711851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
10682680Sktlim@umich.edu          ThreadContext *tc)
1069360SN/A{
1070360SN/A    std::string path;
1071360SN/A
10726701Sgblack@eecs.umich.edu    int index = 0;
10738852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
10746701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
10756701Sgblack@eecs.umich.edu        return -EFAULT;
10766701Sgblack@eecs.umich.edu    }
10776701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
1078360SN/A
10793669Sbinkertn@umich.edu    // Adjust path for current working directory
10803669Sbinkertn@umich.edu    path = process->fullPath(path);
10813669Sbinkertn@umich.edu
1082360SN/A    struct stat hostBuf;
1083360SN/A    int result = lstat(path.c_str(), &hostBuf);
1084360SN/A
1085360SN/A    if (result < 0)
10861458SN/A        return -errno;
1087360SN/A
10888706Sandreas.hansson@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1089360SN/A
10901458SN/A    return 0;
1091360SN/A}
1092360SN/A
10931999SN/A/// Target lstat64() handler.
10941999SN/Atemplate <class OS>
10951999SN/ASyscallReturn
109611851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
10972680Sktlim@umich.edu            ThreadContext *tc)
10981999SN/A{
10991999SN/A    std::string path;
11001999SN/A
11016701Sgblack@eecs.umich.edu    int index = 0;
11028852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11036701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11046701Sgblack@eecs.umich.edu        return -EFAULT;
11056701Sgblack@eecs.umich.edu    }
11066701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11071999SN/A
11083669Sbinkertn@umich.edu    // Adjust path for current working directory
11093669Sbinkertn@umich.edu    path = process->fullPath(path);
11103669Sbinkertn@umich.edu
11112764Sstever@eecs.umich.edu#if NO_STAT64
11122064SN/A    struct stat hostBuf;
11132064SN/A    int result = lstat(path.c_str(), &hostBuf);
11142064SN/A#else
11151999SN/A    struct stat64 hostBuf;
11161999SN/A    int result = lstat64(path.c_str(), &hostBuf);
11172064SN/A#endif
11181999SN/A
11191999SN/A    if (result < 0)
11201999SN/A        return -errno;
11211999SN/A
11228706Sandreas.hansson@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11231999SN/A
11241999SN/A    return 0;
11251999SN/A}
11261999SN/A
1127378SN/A/// Target fstat() handler.
1128360SN/Atemplate <class OS>
11291450SN/ASyscallReturn
113011856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
1131360SN/A{
11326701Sgblack@eecs.umich.edu    int index = 0;
113311856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
113411856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
1135360SN/A
113611380Salexandru.dutu@amd.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1137360SN/A
113811856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
113911856Sbrandon.potter@amd.com    if (!ffdp)
11401458SN/A        return -EBADF;
114111856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
1142360SN/A
1143360SN/A    struct stat hostBuf;
114410931Sbrandon.potter@amd.com    int result = fstat(sim_fd, &hostBuf);
1145360SN/A
1146360SN/A    if (result < 0)
11471458SN/A        return -errno;
1148360SN/A
114910931Sbrandon.potter@amd.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11502021SN/A
11511458SN/A    return 0;
1152360SN/A}
1153360SN/A
1154360SN/A
11551706SN/A/// Target statfs() handler.
11561706SN/Atemplate <class OS>
11571706SN/ASyscallReturn
115811851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
11592680Sktlim@umich.edu           ThreadContext *tc)
11601706SN/A{
116111799Sbrandon.potter@amd.com#if NO_STATFS
116211799Sbrandon.potter@amd.com    warn("Host OS cannot support calls to statfs. Ignoring syscall");
116311799Sbrandon.potter@amd.com#else
11641706SN/A    std::string path;
11651706SN/A
11666701Sgblack@eecs.umich.edu    int index = 0;
11678852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
11686701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11696701Sgblack@eecs.umich.edu        return -EFAULT;
11706701Sgblack@eecs.umich.edu    }
11716701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
11721706SN/A
11733669Sbinkertn@umich.edu    // Adjust path for current working directory
11743669Sbinkertn@umich.edu    path = process->fullPath(path);
11753669Sbinkertn@umich.edu
11761706SN/A    struct statfs hostBuf;
11771706SN/A    int result = statfs(path.c_str(), &hostBuf);
11781706SN/A
11791706SN/A    if (result < 0)
11802218SN/A        return -errno;
11811706SN/A
118211759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
118311799Sbrandon.potter@amd.com#endif
11841706SN/A    return 0;
11851706SN/A}
11861706SN/A
118711886Sbrandon.potter@amd.comtemplate <class OS>
118811886Sbrandon.potter@amd.comSyscallReturn
118911886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
119011886Sbrandon.potter@amd.com{
119111886Sbrandon.potter@amd.com    int index = 0;
119211886Sbrandon.potter@amd.com    TheISA::IntReg flags = p->getSyscallArg(tc, index);
119311886Sbrandon.potter@amd.com    TheISA::IntReg newStack = p->getSyscallArg(tc, index);
119411886Sbrandon.potter@amd.com    Addr ptidPtr = p->getSyscallArg(tc, index);
119511886Sbrandon.potter@amd.com    Addr ctidPtr = p->getSyscallArg(tc, index);
119611886Sbrandon.potter@amd.com    Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index);
119711886Sbrandon.potter@amd.com
119811886Sbrandon.potter@amd.com    if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) ||
119911886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) ||
120011886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_FS)     &&  (flags & OS::TGT_CLONE_NEWNS)) ||
120111886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWIPC) &&  (flags & OS::TGT_CLONE_SYSVSEM)) ||
120211886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_NEWPID) &&  (flags & OS::TGT_CLONE_THREAD)) ||
120311886Sbrandon.potter@amd.com        ((flags & OS::TGT_CLONE_VM)     && !(newStack)))
120411886Sbrandon.potter@amd.com        return -EINVAL;
120511886Sbrandon.potter@amd.com
120611886Sbrandon.potter@amd.com    ThreadContext *ctc;
120711886Sbrandon.potter@amd.com    if (!(ctc = p->findFreeContext()))
120811886Sbrandon.potter@amd.com        fatal("clone: no spare thread context in system");
120911886Sbrandon.potter@amd.com
121011886Sbrandon.potter@amd.com    /**
121111886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
121211886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
121311886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
121411886Sbrandon.potter@amd.com     * constructor.
121511886Sbrandon.potter@amd.com     */
121611886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
121711886Sbrandon.potter@amd.com    pp->executable.assign(*(new std::string(p->progName())));
121811886Sbrandon.potter@amd.com    pp->cmd.push_back(*(new std::string(p->progName())));
121911886Sbrandon.potter@amd.com    pp->system = p->system;
122011886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
122111886Sbrandon.potter@amd.com    pp->input.assign("stdin");
122211886Sbrandon.potter@amd.com    pp->output.assign("stdout");
122311886Sbrandon.potter@amd.com    pp->errout.assign("stderr");
122411886Sbrandon.potter@amd.com    pp->uid = p->uid();
122511886Sbrandon.potter@amd.com    pp->euid = p->euid();
122611886Sbrandon.potter@amd.com    pp->gid = p->gid();
122711886Sbrandon.potter@amd.com    pp->egid = p->egid();
122811886Sbrandon.potter@amd.com
122911886Sbrandon.potter@amd.com    /* Find the first free PID that's less than the maximum */
123011886Sbrandon.potter@amd.com    std::set<int> const& pids = p->system->PIDs;
123111886Sbrandon.potter@amd.com    int temp_pid = *pids.begin();
123211886Sbrandon.potter@amd.com    do {
123311886Sbrandon.potter@amd.com        temp_pid++;
123411886Sbrandon.potter@amd.com    } while (pids.find(temp_pid) != pids.end());
123511886Sbrandon.potter@amd.com    if (temp_pid >= System::maxPID)
123611886Sbrandon.potter@amd.com        fatal("temp_pid is too large: %d", temp_pid);
123711886Sbrandon.potter@amd.com
123811886Sbrandon.potter@amd.com    pp->pid = temp_pid;
123911886Sbrandon.potter@amd.com    pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid();
124011886Sbrandon.potter@amd.com    Process *cp = pp->create();
124111886Sbrandon.potter@amd.com    delete pp;
124211886Sbrandon.potter@amd.com
124311886Sbrandon.potter@amd.com    Process *owner = ctc->getProcessPtr();
124411886Sbrandon.potter@amd.com    ctc->setProcessPtr(cp);
124511886Sbrandon.potter@amd.com    cp->assignThreadContext(ctc->contextId());
124611886Sbrandon.potter@amd.com    owner->revokeThreadContext(ctc->contextId());
124711886Sbrandon.potter@amd.com
124811886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_PARENT_SETTID) {
124911886Sbrandon.potter@amd.com        BufferArg ptidBuf(ptidPtr, sizeof(long));
125011886Sbrandon.potter@amd.com        long *ptid = (long *)ptidBuf.bufferPtr();
125111886Sbrandon.potter@amd.com        *ptid = cp->pid();
125211886Sbrandon.potter@amd.com        ptidBuf.copyOut(tc->getMemProxy());
125311886Sbrandon.potter@amd.com    }
125411886Sbrandon.potter@amd.com
125511886Sbrandon.potter@amd.com    cp->initState();
125611886Sbrandon.potter@amd.com    p->clone(tc, ctc, cp, flags);
125711886Sbrandon.potter@amd.com
125811886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_SETTID) {
125911886Sbrandon.potter@amd.com        BufferArg ctidBuf(ctidPtr, sizeof(long));
126011886Sbrandon.potter@amd.com        long *ctid = (long *)ctidBuf.bufferPtr();
126111886Sbrandon.potter@amd.com        *ctid = cp->pid();
126211886Sbrandon.potter@amd.com        ctidBuf.copyOut(ctc->getMemProxy());
126311886Sbrandon.potter@amd.com    }
126411886Sbrandon.potter@amd.com
126511886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_CHILD_CLEARTID)
126611886Sbrandon.potter@amd.com        cp->childClearTID = (uint64_t)ctidPtr;
126711886Sbrandon.potter@amd.com
126811886Sbrandon.potter@amd.com    ctc->clearArchRegs();
126911886Sbrandon.potter@amd.com
127011886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
127111886Sbrandon.potter@amd.com    TheISA::copyMiscRegs(tc, ctc);
127211886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
127311886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
127411886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0);
127511886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0);
127611886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2);
127711886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows);
127811886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_CWP, 0);
127911886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0);
128011886Sbrandon.potter@amd.com    ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0);
128111886Sbrandon.potter@amd.com    ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY);
128211886Sbrandon.potter@amd.com    for (int y = 8; y < 32; y++)
128311886Sbrandon.potter@amd.com        ctc->setIntReg(y, tc->readIntReg(y));
128411886Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA
128511886Sbrandon.potter@amd.com    TheISA::copyRegs(tc, ctc);
128611886Sbrandon.potter@amd.com#endif
128711886Sbrandon.potter@amd.com
128811886Sbrandon.potter@amd.com#if THE_ISA == X86_ISA
128911886Sbrandon.potter@amd.com    if (flags & OS::TGT_CLONE_SETTLS) {
129011886Sbrandon.potter@amd.com        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr);
129111886Sbrandon.potter@amd.com        ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr);
129211886Sbrandon.potter@amd.com    }
129311886Sbrandon.potter@amd.com#endif
129411886Sbrandon.potter@amd.com
129511886Sbrandon.potter@amd.com    if (newStack)
129611886Sbrandon.potter@amd.com        ctc->setIntReg(TheISA::StackPointerReg, newStack);
129711886Sbrandon.potter@amd.com
129811886Sbrandon.potter@amd.com    cp->setSyscallReturn(ctc, 0);
129911886Sbrandon.potter@amd.com
130011886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA
130111886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallSuccessReg, 0);
130211886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA
130311886Sbrandon.potter@amd.com    tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0);
130411886Sbrandon.potter@amd.com    ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1);
130511886Sbrandon.potter@amd.com#endif
130611886Sbrandon.potter@amd.com
130711886Sbrandon.potter@amd.com    ctc->pcState(tc->nextInstAddr());
130811886Sbrandon.potter@amd.com    ctc->activate();
130911886Sbrandon.potter@amd.com
131011886Sbrandon.potter@amd.com    return cp->pid();
131111886Sbrandon.potter@amd.com}
13121706SN/A
13131706SN/A/// Target fstatfs() handler.
13141706SN/Atemplate <class OS>
13151706SN/ASyscallReturn
131611856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
13171706SN/A{
13186701Sgblack@eecs.umich.edu    int index = 0;
131911856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
132011856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
13211706SN/A
132211856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
132311856Sbrandon.potter@amd.com    if (!ffdp)
13241706SN/A        return -EBADF;
132511856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
13261706SN/A
13271706SN/A    struct statfs hostBuf;
132810931Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
13291706SN/A
13301706SN/A    if (result < 0)
13312218SN/A        return -errno;
13321706SN/A
133311759Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
13341706SN/A
13351706SN/A    return 0;
13361706SN/A}
13371706SN/A
13381706SN/A
13391999SN/A/// Target writev() handler.
13401999SN/Atemplate <class OS>
13411999SN/ASyscallReturn
134211856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
13431999SN/A{
13446701Sgblack@eecs.umich.edu    int index = 0;
134511856Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
134610931Sbrandon.potter@amd.com
134711856Sbrandon.potter@amd.com    auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]);
134811856Sbrandon.potter@amd.com    if (!hbfdp)
13491999SN/A        return -EBADF;
135011856Sbrandon.potter@amd.com    int sim_fd = hbfdp->getSimFD();
13511999SN/A
135211856Sbrandon.potter@amd.com    SETranslatingPortProxy &prox = tc->getMemProxy();
135311856Sbrandon.potter@amd.com    uint64_t tiov_base = p->getSyscallArg(tc, index);
135411856Sbrandon.potter@amd.com    size_t count = p->getSyscallArg(tc, index);
13551999SN/A    struct iovec hiov[count];
13566227Snate@binkert.org    for (size_t i = 0; i < count; ++i) {
13571999SN/A        typename OS::tgt_iovec tiov;
13582461SN/A
135911856Sbrandon.potter@amd.com        prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
136011856Sbrandon.potter@amd.com                      (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
13618737Skoansin.tan@gmail.com        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
13621999SN/A        hiov[i].iov_base = new char [hiov[i].iov_len];
136311856Sbrandon.potter@amd.com        prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
136411856Sbrandon.potter@amd.com                      hiov[i].iov_len);
13651999SN/A    }
13661999SN/A
136710931Sbrandon.potter@amd.com    int result = writev(sim_fd, hiov, count);
13681999SN/A
13696227Snate@binkert.org    for (size_t i = 0; i < count; ++i)
13701999SN/A        delete [] (char *)hiov[i].iov_base;
13711999SN/A
13721999SN/A    if (result < 0)
13732218SN/A        return -errno;
13741999SN/A
137510629Sjthestness@gmail.com    return result;
13761999SN/A}
13771999SN/A
137811385Sbrandon.potter@amd.com/// Real mmap handler.
1379360SN/Atemplate <class OS>
13801450SN/ASyscallReturn
138111851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
138211385Sbrandon.potter@amd.com         bool is_mmap2)
1383360SN/A{
13846701Sgblack@eecs.umich.edu    int index = 0;
13856701Sgblack@eecs.umich.edu    Addr start = p->getSyscallArg(tc, index);
13866701Sgblack@eecs.umich.edu    uint64_t length = p->getSyscallArg(tc, index);
138711383Sbrandon.potter@amd.com    int prot = p->getSyscallArg(tc, index);
138811383Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
13898324Ssteve.reinhardt@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
139010486Stjablin@gmail.com    int offset = p->getSyscallArg(tc, index);
1391360SN/A
139211385Sbrandon.potter@amd.com    if (is_mmap2)
139311385Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
13949008Sgblack@eecs.umich.edu
139511383Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
139611383Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
139711383Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
139811383Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
139911383Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
140011383Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
140111383Sbrandon.potter@amd.com        !length) {
140211383Sbrandon.potter@amd.com        return -EINVAL;
140311383Sbrandon.potter@amd.com    }
14048324Ssteve.reinhardt@amd.com
140511383Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
140611383Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
140711383Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
140811383Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
140911383Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
141011383Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
141111383Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
141211383Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
141311383Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
141411383Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
141511383Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
141611383Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
141711383Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
141811383Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
141911383Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
142011383Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
142111383Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
142211383Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
142311383Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
142411383Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
142511383Sbrandon.potter@amd.com        // than we currently maintain.
142611383Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
142711383Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
142811383Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
14298324Ssteve.reinhardt@amd.com    }
14305877Shsul@eecs.umich.edu
143110486Stjablin@gmail.com    length = roundUp(length, TheISA::PageBytes);
143210486Stjablin@gmail.com
143311383Sbrandon.potter@amd.com    int sim_fd = -1;
143411383Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
143511383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
143611856Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
143711624Smichael.lebeane@amd.com
143811856Sbrandon.potter@amd.com        auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep);
143911856Sbrandon.potter@amd.com        if (dfdp) {
144011856Sbrandon.potter@amd.com            EmulatedDriver *emul_driver = dfdp->getDriver();
144111856Sbrandon.potter@amd.com            return emul_driver->mmap(p, tc, start, length, prot,
144211624Smichael.lebeane@amd.com                                     tgt_flags, tgt_fd, offset);
144311624Smichael.lebeane@amd.com        }
144411624Smichael.lebeane@amd.com
144511856Sbrandon.potter@amd.com        auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
144611856Sbrandon.potter@amd.com        if (!ffdp)
144711383Sbrandon.potter@amd.com            return -EBADF;
144811856Sbrandon.potter@amd.com        sim_fd = ffdp->getSimFD();
1449360SN/A
145011383Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
145111383Sbrandon.potter@amd.com                                    sim_fd, offset);
14528600Ssteve.reinhardt@amd.com
145311383Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
145411383Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
145511383Sbrandon.potter@amd.com            return -errno;
14568600Ssteve.reinhardt@amd.com        }
14572544SN/A    }
14582544SN/A
145911383Sbrandon.potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
146011383Sbrandon.potter@amd.com    // start address unless MAP_FIXED is specified.
146111383Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
146211905SBrandon.Potter@amd.com        std::shared_ptr<MemState> mem_state = p->memState;
146311905SBrandon.Potter@amd.com        Addr mmap_end = mem_state->getMmapEnd();
146411905SBrandon.Potter@amd.com
146511905SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? mmap_end - length : mmap_end;
146611905SBrandon.Potter@amd.com        mmap_end = p->mmapGrowsDown() ? start : mmap_end + length;
146711905SBrandon.Potter@amd.com
146811905SBrandon.Potter@amd.com        mem_state->setMmapEnd(mmap_end);
146911383Sbrandon.potter@amd.com    }
147011383Sbrandon.potter@amd.com
147111383Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
147211383Sbrandon.potter@amd.com                    start, start + length - 1);
147311383Sbrandon.potter@amd.com
147411383Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
147511383Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
147611383Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
147711383Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
147811383Sbrandon.potter@amd.com    if (clobber) {
147911383Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
148011383Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
148111383Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
148211383Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
148311383Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
14848600Ssteve.reinhardt@amd.com        }
14856672Sgblack@eecs.umich.edu    }
14868600Ssteve.reinhardt@amd.com
148711383Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
148811383Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
148911383Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
14908601Ssteve.reinhardt@amd.com    p->allocateMem(start, length, clobber);
14912544SN/A
149211383Sbrandon.potter@amd.com    // Transfer content into target address space.
149311383Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
149411383Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
149511383Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
149611383Sbrandon.potter@amd.com        // with something like:
149711383Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
149811383Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
149911383Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
150011383Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
150111383Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
150211383Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
150311383Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
150411383Sbrandon.potter@amd.com    } else {
150511383Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
150611383Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
150711383Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
150811383Sbrandon.potter@amd.com        // the host into the target address space.
150911383Sbrandon.potter@amd.com        struct stat file_stat;
151011383Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
151111383Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
151211383Sbrandon.potter@amd.com
151311383Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
151411383Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
151511383Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
151611383Sbrandon.potter@amd.com        // on the specified offset into the file.
151711383Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
151811383Sbrandon.potter@amd.com                                 length);
151911383Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
152011383Sbrandon.potter@amd.com
152111383Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
152211383Sbrandon.potter@amd.com        munmap(pmap, length);
152311383Sbrandon.potter@amd.com
152411392Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
152511392Sbrandon.potter@amd.com        // The loader will call mmap to map the images into its address
152611392Sbrandon.potter@amd.com        // space and we intercept that here. We can verify that we are
152711392Sbrandon.potter@amd.com        // executing inside the loader by checking the program counter value.
152811392Sbrandon.potter@amd.com        // XXX: with multiprogrammed workloads or multi-node configurations,
152911392Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
153011392Sbrandon.potter@amd.com        ObjectFile *interpreter = p->getInterpreter();
153111392Sbrandon.potter@amd.com        if (interpreter) {
153211392Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
153311392Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
153411392Sbrandon.potter@amd.com
153511392Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
153611392Sbrandon.potter@amd.com
153711392Sbrandon.potter@amd.com            if (pc >= text_start && pc < text_end) {
153811856Sbrandon.potter@amd.com                std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd];
153911856Sbrandon.potter@amd.com                auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep);
154011856Sbrandon.potter@amd.com                ObjectFile *lib = createObjectFile(ffdp->getFileName());
154111392Sbrandon.potter@amd.com
154211392Sbrandon.potter@amd.com                if (lib) {
154311392Sbrandon.potter@amd.com                    lib->loadAllSymbols(debugSymbolTable,
154411392Sbrandon.potter@amd.com                                        lib->textBase(), start);
154511392Sbrandon.potter@amd.com                }
154611392Sbrandon.potter@amd.com            }
154711392Sbrandon.potter@amd.com        }
154811392Sbrandon.potter@amd.com
154911383Sbrandon.potter@amd.com        // Note that we do not zero out the remainder of the mapping. This
155011383Sbrandon.potter@amd.com        // is done by a real system, but it probably will not affect
155111383Sbrandon.potter@amd.com        // execution (hopefully).
155211383Sbrandon.potter@amd.com    }
155311383Sbrandon.potter@amd.com
15541458SN/A    return start;
1555360SN/A}
1556360SN/A
155711593Santhony.gutierrez@amd.comtemplate <class OS>
155811593Santhony.gutierrez@amd.comSyscallReturn
155911851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
156011593Santhony.gutierrez@amd.com{
156111593Santhony.gutierrez@amd.com    int index = 0;
156211593Santhony.gutierrez@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
156311593Santhony.gutierrez@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
156411593Santhony.gutierrez@amd.com    int nbytes = p->getSyscallArg(tc, index);
156511593Santhony.gutierrez@amd.com    int offset = p->getSyscallArg(tc, index);
156611593Santhony.gutierrez@amd.com
156711856Sbrandon.potter@amd.com    auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]);
156811856Sbrandon.potter@amd.com    if (!ffdp)
156911593Santhony.gutierrez@amd.com        return -EBADF;
157011856Sbrandon.potter@amd.com    int sim_fd = ffdp->getSimFD();
157111593Santhony.gutierrez@amd.com
157211593Santhony.gutierrez@amd.com    BufferArg bufArg(bufPtr, nbytes);
157311593Santhony.gutierrez@amd.com    bufArg.copyIn(tc->getMemProxy());
157411593Santhony.gutierrez@amd.com
157511594Santhony.gutierrez@amd.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
157611593Santhony.gutierrez@amd.com
157711593Santhony.gutierrez@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
157811593Santhony.gutierrez@amd.com}
157911593Santhony.gutierrez@amd.com
158011385Sbrandon.potter@amd.com/// Target mmap() handler.
158111385Sbrandon.potter@amd.comtemplate <class OS>
158211385Sbrandon.potter@amd.comSyscallReturn
158311851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
158411385Sbrandon.potter@amd.com{
158511385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, false);
158611385Sbrandon.potter@amd.com}
158711385Sbrandon.potter@amd.com
158811385Sbrandon.potter@amd.com/// Target mmap2() handler.
158911385Sbrandon.potter@amd.comtemplate <class OS>
159011385Sbrandon.potter@amd.comSyscallReturn
159111851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
159211385Sbrandon.potter@amd.com{
159311385Sbrandon.potter@amd.com    return mmapImpl<OS>(desc, num, p, tc, true);
159411385Sbrandon.potter@amd.com}
159511385Sbrandon.potter@amd.com
1596378SN/A/// Target getrlimit() handler.
1597360SN/Atemplate <class OS>
15981450SN/ASyscallReturn
159911851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
160011851Sbrandon.potter@amd.com              ThreadContext *tc)
1601360SN/A{
16026701Sgblack@eecs.umich.edu    int index = 0;
16036701Sgblack@eecs.umich.edu    unsigned resource = process->getSyscallArg(tc, index);
16046701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1605360SN/A
1606360SN/A    switch (resource) {
160711906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_STACK:
160811906SBrandon.Potter@amd.com        // max stack size in bytes: make up a number (8MB for now)
160911906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
161011906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
161111906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
161211906SBrandon.Potter@amd.com        break;
1613360SN/A
161411906SBrandon.Potter@amd.com      case OS::TGT_RLIMIT_DATA:
161511906SBrandon.Potter@amd.com        // max data segment size in bytes: make up a number
161611906SBrandon.Potter@amd.com        rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
161711906SBrandon.Potter@amd.com        rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
161811906SBrandon.Potter@amd.com        rlp->rlim_max = TheISA::htog(rlp->rlim_max);
161911906SBrandon.Potter@amd.com        break;
16205877Shsul@eecs.umich.edu
162111906SBrandon.Potter@amd.com      default:
162211906SBrandon.Potter@amd.com        warn("getrlimit: unimplemented resource %d", resource);
162311906SBrandon.Potter@amd.com        return -EINVAL;
162411906SBrandon.Potter@amd.com        break;
1625360SN/A    }
1626360SN/A
16278706Sandreas.hansson@arm.com    rlp.copyOut(tc->getMemProxy());
16281458SN/A    return 0;
1629360SN/A}
1630360SN/A
163110796Sbrandon.potter@amd.com/// Target clock_gettime() function.
163210796Sbrandon.potter@amd.comtemplate <class OS>
163310796Sbrandon.potter@amd.comSyscallReturn
163411851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
163510796Sbrandon.potter@amd.com{
163610796Sbrandon.potter@amd.com    int index = 1;
163710796Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
163810796Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
163910796Sbrandon.potter@amd.com
164010796Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
164110796Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
164210796Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
164310796Sbrandon.potter@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
164410796Sbrandon.potter@amd.com
164510796Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
164610796Sbrandon.potter@amd.com
164710796Sbrandon.potter@amd.com    return 0;
164810796Sbrandon.potter@amd.com}
164910796Sbrandon.potter@amd.com
165011337SMichael.Lebeane@amd.com/// Target clock_getres() function.
165111337SMichael.Lebeane@amd.comtemplate <class OS>
165211337SMichael.Lebeane@amd.comSyscallReturn
165311851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
165411337SMichael.Lebeane@amd.com{
165511337SMichael.Lebeane@amd.com    int index = 1;
165611337SMichael.Lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
165711337SMichael.Lebeane@amd.com
165811337SMichael.Lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
165911337SMichael.Lebeane@amd.com    tp->tv_sec = 0;
166011337SMichael.Lebeane@amd.com    tp->tv_nsec = 1;
166111337SMichael.Lebeane@amd.com
166211337SMichael.Lebeane@amd.com    tp.copyOut(tc->getMemProxy());
166311337SMichael.Lebeane@amd.com
166411337SMichael.Lebeane@amd.com    return 0;
166511337SMichael.Lebeane@amd.com}
166611337SMichael.Lebeane@amd.com
1667378SN/A/// Target gettimeofday() handler.
1668360SN/Atemplate <class OS>
16691450SN/ASyscallReturn
167011851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
167111851Sbrandon.potter@amd.com                 ThreadContext *tc)
1672360SN/A{
16736701Sgblack@eecs.umich.edu    int index = 0;
16746701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
1675360SN/A
167610796Sbrandon.potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
1677360SN/A    tp->tv_sec += seconds_since_epoch;
16786109Ssanchezd@stanford.edu    tp->tv_sec = TheISA::htog(tp->tv_sec);
16796109Ssanchezd@stanford.edu    tp->tv_usec = TheISA::htog(tp->tv_usec);
1680360SN/A
16818706Sandreas.hansson@arm.com    tp.copyOut(tc->getMemProxy());
1682360SN/A
16831458SN/A    return 0;
1684360SN/A}
1685360SN/A
1686360SN/A
16871999SN/A/// Target utimes() handler.
16881999SN/Atemplate <class OS>
16891999SN/ASyscallReturn
169011851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
16912680Sktlim@umich.edu           ThreadContext *tc)
16921999SN/A{
16931999SN/A    std::string path;
16941999SN/A
16956701Sgblack@eecs.umich.edu    int index = 0;
16968852Sandreas.hansson@arm.com    if (!tc->getMemProxy().tryReadString(path,
16976701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
16986701Sgblack@eecs.umich.edu        return -EFAULT;
16996701Sgblack@eecs.umich.edu    }
17001999SN/A
17016701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::timeval [2]>
17026701Sgblack@eecs.umich.edu        tp(process->getSyscallArg(tc, index));
17038706Sandreas.hansson@arm.com    tp.copyIn(tc->getMemProxy());
17041999SN/A
17051999SN/A    struct timeval hostTimeval[2];
170611906SBrandon.Potter@amd.com    for (int i = 0; i < 2; ++i) {
17078737Skoansin.tan@gmail.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
17088737Skoansin.tan@gmail.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
17091999SN/A    }
17103669Sbinkertn@umich.edu
17113669Sbinkertn@umich.edu    // Adjust path for current working directory
17123669Sbinkertn@umich.edu    path = process->fullPath(path);
17133669Sbinkertn@umich.edu
17141999SN/A    int result = utimes(path.c_str(), hostTimeval);
17151999SN/A
17161999SN/A    if (result < 0)
17171999SN/A        return -errno;
17181999SN/A
17191999SN/A    return 0;
17201999SN/A}
172111886Sbrandon.potter@amd.com
172211886Sbrandon.potter@amd.comtemplate <class OS>
172311886Sbrandon.potter@amd.comSyscallReturn
172411886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc)
172511886Sbrandon.potter@amd.com{
172611886Sbrandon.potter@amd.com    desc->setFlags(0);
172711886Sbrandon.potter@amd.com
172811886Sbrandon.potter@amd.com    int index = 0;
172911886Sbrandon.potter@amd.com    std::string path;
173011886Sbrandon.potter@amd.com    SETranslatingPortProxy & mem_proxy = tc->getMemProxy();
173111886Sbrandon.potter@amd.com    if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index)))
173211886Sbrandon.potter@amd.com        return -EFAULT;
173311886Sbrandon.potter@amd.com
173411886Sbrandon.potter@amd.com    if (access(path.c_str(), F_OK) == -1)
173511886Sbrandon.potter@amd.com        return -EACCES;
173611886Sbrandon.potter@amd.com
173711886Sbrandon.potter@amd.com    auto read_in = [](std::vector<std::string> & vect,
173811886Sbrandon.potter@amd.com                      SETranslatingPortProxy & mem_proxy,
173911886Sbrandon.potter@amd.com                      Addr mem_loc)
174011886Sbrandon.potter@amd.com    {
174111886Sbrandon.potter@amd.com        for (int inc = 0; ; inc++) {
174211886Sbrandon.potter@amd.com            BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr));
174311886Sbrandon.potter@amd.com            b.copyIn(mem_proxy);
174411886Sbrandon.potter@amd.com
174511886Sbrandon.potter@amd.com            if (!*(Addr*)b.bufferPtr())
174611886Sbrandon.potter@amd.com                break;
174711886Sbrandon.potter@amd.com
174811886Sbrandon.potter@amd.com            vect.push_back(std::string());
174911886Sbrandon.potter@amd.com            mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr());
175011886Sbrandon.potter@amd.com        }
175111886Sbrandon.potter@amd.com    };
175211886Sbrandon.potter@amd.com
175311886Sbrandon.potter@amd.com    /**
175411886Sbrandon.potter@amd.com     * Note that ProcessParams is generated by swig and there are no other
175511886Sbrandon.potter@amd.com     * examples of how to create anything but this default constructor. The
175611886Sbrandon.potter@amd.com     * fields are manually initialized instead of passing parameters to the
175711886Sbrandon.potter@amd.com     * constructor.
175811886Sbrandon.potter@amd.com     */
175911886Sbrandon.potter@amd.com    ProcessParams *pp = new ProcessParams();
176011886Sbrandon.potter@amd.com    pp->executable = path;
176111886Sbrandon.potter@amd.com    Addr argv_mem_loc = p->getSyscallArg(tc, index);
176211886Sbrandon.potter@amd.com    read_in(pp->cmd, mem_proxy, argv_mem_loc);
176311886Sbrandon.potter@amd.com    Addr envp_mem_loc = p->getSyscallArg(tc, index);
176411886Sbrandon.potter@amd.com    read_in(pp->env, mem_proxy, envp_mem_loc);
176511886Sbrandon.potter@amd.com    pp->uid = p->uid();
176611886Sbrandon.potter@amd.com    pp->egid = p->egid();
176711886Sbrandon.potter@amd.com    pp->euid = p->euid();
176811886Sbrandon.potter@amd.com    pp->gid = p->gid();
176911886Sbrandon.potter@amd.com    pp->ppid = p->ppid();
177011886Sbrandon.potter@amd.com    pp->pid = p->pid();
177111886Sbrandon.potter@amd.com    pp->input.assign("cin");
177211886Sbrandon.potter@amd.com    pp->output.assign("cout");
177311886Sbrandon.potter@amd.com    pp->errout.assign("cerr");
177411886Sbrandon.potter@amd.com    pp->cwd.assign(p->getcwd());
177511886Sbrandon.potter@amd.com    pp->system = p->system;
177611886Sbrandon.potter@amd.com    /**
177711886Sbrandon.potter@amd.com     * Prevent process object creation with identical PIDs (which will trip
177811886Sbrandon.potter@amd.com     * a fatal check in Process constructor). The execve call is supposed to
177911886Sbrandon.potter@amd.com     * take over the currently executing process' identity but replace
178011886Sbrandon.potter@amd.com     * whatever it is doing with a new process image. Instead of hijacking
178111886Sbrandon.potter@amd.com     * the process object in the simulator, we create a new process object
178211886Sbrandon.potter@amd.com     * and bind to the previous process' thread below (hijacking the thread).
178311886Sbrandon.potter@amd.com     */
178411886Sbrandon.potter@amd.com    p->system->PIDs.erase(p->pid());
178511886Sbrandon.potter@amd.com    Process *new_p = pp->create();
178611886Sbrandon.potter@amd.com    delete pp;
178711886Sbrandon.potter@amd.com
178811886Sbrandon.potter@amd.com    /**
178911886Sbrandon.potter@amd.com     * Work through the file descriptor array and close any files marked
179011886Sbrandon.potter@amd.com     * close-on-exec.
179111886Sbrandon.potter@amd.com     */
179211886Sbrandon.potter@amd.com    new_p->fds = p->fds;
179311886Sbrandon.potter@amd.com    for (int i = 0; i < new_p->fds->getSize(); i++) {
179411886Sbrandon.potter@amd.com        std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i];
179511886Sbrandon.potter@amd.com        if (fdep && fdep->getCOE())
179611886Sbrandon.potter@amd.com            new_p->fds->closeFDEntry(i);
179711886Sbrandon.potter@amd.com    }
179811886Sbrandon.potter@amd.com
179911886Sbrandon.potter@amd.com    *new_p->sigchld = true;
180011886Sbrandon.potter@amd.com
180111886Sbrandon.potter@amd.com    delete p;
180211886Sbrandon.potter@amd.com    tc->clearArchRegs();
180311886Sbrandon.potter@amd.com    tc->setProcessPtr(new_p);
180411886Sbrandon.potter@amd.com    new_p->assignThreadContext(tc->contextId());
180511886Sbrandon.potter@amd.com    new_p->initState();
180611886Sbrandon.potter@amd.com    tc->activate();
180711886Sbrandon.potter@amd.com    TheISA::PCState pcState = tc->pcState();
180811886Sbrandon.potter@amd.com    tc->setNPC(pcState.instAddr());
180911886Sbrandon.potter@amd.com
181011886Sbrandon.potter@amd.com    desc->setFlags(SyscallDesc::SuppressReturnValue);
181111886Sbrandon.potter@amd.com    return 0;
181211886Sbrandon.potter@amd.com}
181311886Sbrandon.potter@amd.com
1814378SN/A/// Target getrusage() function.
1815360SN/Atemplate <class OS>
18161450SN/ASyscallReturn
181711851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
18182680Sktlim@umich.edu              ThreadContext *tc)
1819360SN/A{
18206701Sgblack@eecs.umich.edu    int index = 0;
18216701Sgblack@eecs.umich.edu    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
18226701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
1823360SN/A
18243670Sbinkertn@umich.edu    rup->ru_utime.tv_sec = 0;
18253670Sbinkertn@umich.edu    rup->ru_utime.tv_usec = 0;
1826360SN/A    rup->ru_stime.tv_sec = 0;
1827360SN/A    rup->ru_stime.tv_usec = 0;
1828360SN/A    rup->ru_maxrss = 0;
1829360SN/A    rup->ru_ixrss = 0;
1830360SN/A    rup->ru_idrss = 0;
1831360SN/A    rup->ru_isrss = 0;
1832360SN/A    rup->ru_minflt = 0;
1833360SN/A    rup->ru_majflt = 0;
1834360SN/A    rup->ru_nswap = 0;
1835360SN/A    rup->ru_inblock = 0;
1836360SN/A    rup->ru_oublock = 0;
1837360SN/A    rup->ru_msgsnd = 0;
1838360SN/A    rup->ru_msgrcv = 0;
1839360SN/A    rup->ru_nsignals = 0;
1840360SN/A    rup->ru_nvcsw = 0;
1841360SN/A    rup->ru_nivcsw = 0;
1842360SN/A
18433670Sbinkertn@umich.edu    switch (who) {
18443670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_SELF:
184510796Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
18468737Skoansin.tan@gmail.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
18478737Skoansin.tan@gmail.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
18483670Sbinkertn@umich.edu        break;
18493670Sbinkertn@umich.edu
18503670Sbinkertn@umich.edu      case OS::TGT_RUSAGE_CHILDREN:
18513670Sbinkertn@umich.edu        // do nothing.  We have no child processes, so they take no time.
18523670Sbinkertn@umich.edu        break;
18533670Sbinkertn@umich.edu
18543670Sbinkertn@umich.edu      default:
18553670Sbinkertn@umich.edu        // don't really handle THREAD or CHILDREN, but just warn and
18563670Sbinkertn@umich.edu        // plow ahead
18573670Sbinkertn@umich.edu        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
18583670Sbinkertn@umich.edu             who);
18593670Sbinkertn@umich.edu    }
18603670Sbinkertn@umich.edu
18618706Sandreas.hansson@arm.com    rup.copyOut(tc->getMemProxy());
1862360SN/A
18631458SN/A    return 0;
1864360SN/A}
1865360SN/A
18666683Stjones1@inf.ed.ac.uk/// Target times() function.
18676683Stjones1@inf.ed.ac.uktemplate <class OS>
18686683Stjones1@inf.ed.ac.ukSyscallReturn
186911851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
187011851Sbrandon.potter@amd.com          ThreadContext *tc)
18716683Stjones1@inf.ed.ac.uk{
18726701Sgblack@eecs.umich.edu    int index = 0;
18736701Sgblack@eecs.umich.edu    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
18746683Stjones1@inf.ed.ac.uk
18756683Stjones1@inf.ed.ac.uk    // Fill in the time structure (in clocks)
18767823Ssteve.reinhardt@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
18776683Stjones1@inf.ed.ac.uk    bufp->tms_utime = clocks;
18786683Stjones1@inf.ed.ac.uk    bufp->tms_stime = 0;
18796683Stjones1@inf.ed.ac.uk    bufp->tms_cutime = 0;
18806683Stjones1@inf.ed.ac.uk    bufp->tms_cstime = 0;
18816683Stjones1@inf.ed.ac.uk
18826683Stjones1@inf.ed.ac.uk    // Convert to host endianness
18838737Skoansin.tan@gmail.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
18846683Stjones1@inf.ed.ac.uk
18856683Stjones1@inf.ed.ac.uk    // Write back
18868706Sandreas.hansson@arm.com    bufp.copyOut(tc->getMemProxy());
18876683Stjones1@inf.ed.ac.uk
18886683Stjones1@inf.ed.ac.uk    // Return clock ticks since system boot
18896683Stjones1@inf.ed.ac.uk    return clocks;
18906683Stjones1@inf.ed.ac.uk}
18912553SN/A
18926684Stjones1@inf.ed.ac.uk/// Target time() function.
18936684Stjones1@inf.ed.ac.uktemplate <class OS>
18946684Stjones1@inf.ed.ac.ukSyscallReturn
189511851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
18966684Stjones1@inf.ed.ac.uk{
18976684Stjones1@inf.ed.ac.uk    typename OS::time_t sec, usec;
189810796Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
18996684Stjones1@inf.ed.ac.uk    sec += seconds_since_epoch;
19006684Stjones1@inf.ed.ac.uk
19016701Sgblack@eecs.umich.edu    int index = 0;
19026701Sgblack@eecs.umich.edu    Addr taddr = (Addr)process->getSyscallArg(tc, index);
190311321Ssteve.reinhardt@amd.com    if (taddr != 0) {
19046684Stjones1@inf.ed.ac.uk        typename OS::time_t t = sec;
19058737Skoansin.tan@gmail.com        t = TheISA::htog(t);
19068852Sandreas.hansson@arm.com        SETranslatingPortProxy &p = tc->getMemProxy();
19078852Sandreas.hansson@arm.com        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
19086684Stjones1@inf.ed.ac.uk    }
19096684Stjones1@inf.ed.ac.uk    return sec;
19106684Stjones1@inf.ed.ac.uk}
19112553SN/A
19122553SN/A
19131354SN/A#endif // __SIM_SYSCALL_EMUL_HH__
1914