syscall_emul.hh revision 11624
1360SN/A/*
210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited
310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc.
410027SChris.Adeniyi-Jones@arm.com * All rights reserved
510027SChris.Adeniyi-Jones@arm.com *
610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall
710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual
810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating
910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software
1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder.  You may use the software subject to the license
1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated
1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software,
1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form.
1410027SChris.Adeniyi-Jones@arm.com *
151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
16360SN/A * All rights reserved.
17360SN/A *
18360SN/A * Redistribution and use in source and binary forms, with or without
19360SN/A * modification, are permitted provided that the following conditions are
20360SN/A * met: redistributions of source code must retain the above copyright
21360SN/A * notice, this list of conditions and the following disclaimer;
22360SN/A * redistributions in binary form must reproduce the above copyright
23360SN/A * notice, this list of conditions and the following disclaimer in the
24360SN/A * documentation and/or other materials provided with the distribution;
25360SN/A * neither the name of the copyright holders nor the names of its
26360SN/A * contributors may be used to endorse or promote products derived from
27360SN/A * this software without specific prior written permission.
28360SN/A *
29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
402665Ssaidi@eecs.umich.edu *
412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
422665Ssaidi@eecs.umich.edu *          Kevin Lim
43360SN/A */
44360SN/A
451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__
461354SN/A#define __SIM_SYSCALL_EMUL_HH__
47360SN/A
4812018Sandreas.sandberg@arm.com#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \
4912018Sandreas.sandberg@arm.com  defined(__FreeBSD__) || defined(__CYGWIN__) || \
5012018Sandreas.sandberg@arm.com  defined(__NetBSD__))
5112018Sandreas.sandberg@arm.com
5212018Sandreas.sandberg@arm.com///
5312018Sandreas.sandberg@arm.com/// @file syscall_emul.hh
5412018Sandreas.sandberg@arm.com///
552064SN/A/// This file defines objects used to emulate syscalls from the target
5612018Sandreas.sandberg@arm.com/// application on the host machine.
5712018Sandreas.sandberg@arm.com
5812018Sandreas.sandberg@arm.com#ifdef __CYGWIN32__
5912018Sandreas.sandberg@arm.com#include <sys/fcntl.h>  // for O_BINARY
6012018Sandreas.sandberg@arm.com
6112018Sandreas.sandberg@arm.com#endif
6211799Sbrandon.potter@amd.com#include <fcntl.h>
6312018Sandreas.sandberg@arm.com#include <sys/mman.h>
6412018Sandreas.sandberg@arm.com#include <sys/stat.h>
6512018Sandreas.sandberg@arm.com#include <sys/time.h>
6612018Sandreas.sandberg@arm.com#include <sys/uio.h>
6712018Sandreas.sandberg@arm.com#include <unistd.h>
6812018Sandreas.sandberg@arm.com
6911799Sbrandon.potter@amd.com#include <cerrno>
70360SN/A#include <string>
71360SN/A
72360SN/A#include "base/chunk_generator.hh"
73360SN/A#include "base/intmath.hh"      // for RoundUp
74360SN/A#include "base/loader/object_file.hh"
75360SN/A#include "base/misc.hh"
761809SN/A#include "base/trace.hh"
7711800Sbrandon.potter@amd.com#include "base/types.hh"
7811392Sbrandon.potter@amd.com#include "config/the_isa.hh"
791809SN/A#include "cpu/base.hh"
8011392Sbrandon.potter@amd.com#include "cpu/thread_context.hh"
8111383Sbrandon.potter@amd.com#include "debug/SyscallBase.hh"
823113Sgblack@eecs.umich.edu#include "debug/SyscallVerbose.hh"
8311799Sbrandon.potter@amd.com#include "mem/page_table.hh"
8411759Sbrandon.potter@amd.com#include "sim/byteswap.hh"
8511812Sbaz21@cam.ac.uk#include "sim/emul_driver.hh"
8611812Sbaz21@cam.ac.uk#include "sim/process.hh"
8711799Sbrandon.potter@amd.com#include "sim/syscall_emul_buf.hh"
888229Snate@binkert.org#include "sim/syscallreturn.hh"
898229Snate@binkert.org#include "sim/system.hh"
9011594Santhony.gutierrez@amd.com
917075Snate@binkert.org// This wrapper macro helps out with readability a bit. FLAGEXT specifies
928229Snate@binkert.org// the verbosity and FMT is the message to be appended to the syscall
9311856Sbrandon.potter@amd.com// header information. The syscall header information contains the cpuid
947075Snate@binkert.org// and thread id.
95360SN/A#define DPRINTF_SYSCALL(FLAGEXT, FMT, ...)                                  \
9611886Sbrandon.potter@amd.com    DPRINTFS(Syscall##FLAGEXT, tc->getCpuPtr(), "T%d : syscall " FMT,       \
9711800Sbrandon.potter@amd.com             tc->threadId(), __VA_ARGS__)
9811392Sbrandon.potter@amd.com
992462SN/A///
1001354SN/A/// System call descriptor.
1016216Snate@binkert.org///
1026658Snate@binkert.orgclass SyscallDesc {
1032474SN/A
1042680Sktlim@umich.edu  public:
1058229Snate@binkert.org
10611886Sbrandon.potter@amd.com    /// Typedef for target syscall handler functions.
10710496Ssteve.reinhardt@amd.com    typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num,
10811911SBrandon.Potter@amd.com                           LiveProcess *, ThreadContext *);
1098229Snate@binkert.org
11011794Sbrandon.potter@amd.com    const char *name;   //!< Syscall name (e.g., "open").
11111886Sbrandon.potter@amd.com    FuncPtr funcPtr;    //!< Pointer to emulation function.
11210497Ssteve.reinhardt@amd.com    int flags;          //!< Flags (see Flags enum).
11311794Sbrandon.potter@amd.com    bool warned;        //!< Have we warned about unimplemented syscall?
114360SN/A
115360SN/A    /// Flag values for controlling syscall behavior.
116360SN/A    enum Flags {
117360SN/A        /// Don't set return regs according to funcPtr return value.
118360SN/A        /// Used for syscalls with non-standard return conventions
119360SN/A        /// that explicitly set the ThreadContext regs (e.g.,
120360SN/A        /// sigreturn).
121360SN/A        SuppressReturnValue = 1,
122360SN/A        WarnOnce = 2
123360SN/A    };
124378SN/A
1251706SN/A    /// Constructor.
12611851Sbrandon.potter@amd.com    SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0)
127378SN/A        : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false)
128378SN/A    {
129378SN/A    }
130378SN/A
131378SN/A    /// Emulate the syscall.  Public interface for calling through funcPtr.
1321706SN/A    void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc);
13311851Sbrandon.potter@amd.com
134360SN/A    /// Is the WarnOnce flag set?
13511760Sbrandon.potter@amd.com    bool warnOnce() const {  return (flags & WarnOnce); }
13611760Sbrandon.potter@amd.com};
13711851Sbrandon.potter@amd.com
13811760Sbrandon.potter@amd.com
1396109Ssanchezd@stanford.edu//////////////////////////////////////////////////////////////////////
1401706SN/A//
14111851Sbrandon.potter@amd.com// The following emulation functions are generic enough that they
142378SN/A// don't need to be recompiled for different emulated OS's.  They are
1436109Ssanchezd@stanford.edu// defined in sim/syscall_emul.cc.
1446109Ssanchezd@stanford.edu//
14511851Sbrandon.potter@amd.com//////////////////////////////////////////////////////////////////////
1466109Ssanchezd@stanford.edu
14711886Sbrandon.potter@amd.com
14811886Sbrandon.potter@amd.com/// Handler for unimplemented syscalls that we haven't thought about.
14911886Sbrandon.potter@amd.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
15011886Sbrandon.potter@amd.com                                LiveProcess *p, ThreadContext *tc);
151378SN/A
1521706SN/A/// Handler for unimplemented syscalls that we never intend to
15311851Sbrandon.potter@amd.com/// implement (signal handling, etc.) and should not affect the correct
154378SN/A/// behavior of the program.  Print a warning only if the appropriate
1555748SSteve.Reinhardt@amd.com/// trace flag is enabled.  Return success to the target program.
1565748SSteve.Reinhardt@amd.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num,
15711851Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
158378SN/A
159378SN/A/// Target exit() handler: terminate current context.
1601706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
16111851Sbrandon.potter@amd.com                       LiveProcess *p, ThreadContext *tc);
162378SN/A
16311886Sbrandon.potter@amd.com/// Target exit_group() handler: terminate simulation. (exit all threads)
1641706SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
16511851Sbrandon.potter@amd.com                       LiveProcess *p, ThreadContext *tc);
166378SN/A
167378SN/A/// Target getpagesize() handler.
1681706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
16911851Sbrandon.potter@amd.com                              LiveProcess *p, ThreadContext *tc);
170378SN/A
171378SN/A/// Target brk() handler: set brk address.
1721706SN/ASyscallReturn brkFunc(SyscallDesc *desc, int num,
17311851Sbrandon.potter@amd.com                      LiveProcess *p, ThreadContext *tc);
174378SN/A
1754118Sgblack@eecs.umich.edu/// Target close() handler.
1764118Sgblack@eecs.umich.eduSyscallReturn closeFunc(SyscallDesc *desc, int num,
17711851Sbrandon.potter@amd.com                        LiveProcess *p, ThreadContext *tc);
1784118Sgblack@eecs.umich.edu
179378SN/A/// Target read() handler.
1801706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num,
18111851Sbrandon.potter@amd.com                       LiveProcess *p, ThreadContext *tc);
182378SN/A
183378SN/A/// Target write() handler.
1841706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num,
18511851Sbrandon.potter@amd.com                        LiveProcess *p, ThreadContext *tc);
186360SN/A
1875513SMichael.Adler@intel.com/// Target lseek() handler.
1885513SMichael.Adler@intel.comSyscallReturn lseekFunc(SyscallDesc *desc, int num,
18911851Sbrandon.potter@amd.com                        LiveProcess *p, ThreadContext *tc);
1905513SMichael.Adler@intel.com
19110203SAli.Saidi@ARM.com/// Target _llseek() handler.
19210203SAli.Saidi@ARM.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
19311851Sbrandon.potter@amd.com                        LiveProcess *p, ThreadContext *tc);
19410203SAli.Saidi@ARM.com
1955513SMichael.Adler@intel.com/// Target munmap() handler.
19611851Sbrandon.potter@amd.comSyscallReturn munmapFunc(SyscallDesc *desc, int num,
1975513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
198511SN/A
19910633Smichaelupton@gmail.com/// Target gethostname() handler.
20011851Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
20110633Smichaelupton@gmail.com                              LiveProcess *p, ThreadContext *tc);
2021706SN/A
20311851Sbrandon.potter@amd.com/// Target getcwd() handler.
204511SN/ASyscallReturn getcwdFunc(SyscallDesc *desc, int num,
2055513SMichael.Adler@intel.com                         LiveProcess *p, ThreadContext *tc);
2065513SMichael.Adler@intel.com
20711851Sbrandon.potter@amd.com/// Target readlink() handler.
2085513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
209511SN/A                           LiveProcess *p, ThreadContext *tc,
2101706SN/A                           int index = 0);
21111851Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
2121706SN/A                           LiveProcess *p, ThreadContext *tc);
2131706SN/A
2141706SN/A/// Target unlink() handler.
2151706SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num,
21611851Sbrandon.potter@amd.com                           LiveProcess *p, ThreadContext *tc,
2171706SN/A                           int index);
2181706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
2191706SN/A                         LiveProcess *p, ThreadContext *tc);
2201706SN/A
22111851Sbrandon.potter@amd.com/// Target mkdir() handler.
2221706SN/ASyscallReturn mkdirFunc(SyscallDesc *desc, int num,
223511SN/A                        LiveProcess *p, ThreadContext *tc);
2246703Svince@csl.cornell.edu
2256703Svince@csl.cornell.edu/// Target rename() handler.
22611851Sbrandon.potter@amd.comSyscallReturn renameFunc(SyscallDesc *desc, int num,
2276703Svince@csl.cornell.edu                         LiveProcess *p, ThreadContext *tc);
2286685Stjones1@inf.ed.ac.uk
2296685Stjones1@inf.ed.ac.uk
23011851Sbrandon.potter@amd.com/// Target truncate() handler.
2316685Stjones1@inf.ed.ac.ukSyscallReturn truncateFunc(SyscallDesc *desc, int num,
2326685Stjones1@inf.ed.ac.uk                           LiveProcess *p, ThreadContext *tc);
2335513SMichael.Adler@intel.com
2345513SMichael.Adler@intel.com
23511851Sbrandon.potter@amd.com/// Target ftruncate() handler.
2365513SMichael.Adler@intel.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
23711885Sbrandon.potter@amd.com                            LiveProcess *p, ThreadContext *tc);
23811885Sbrandon.potter@amd.com
23911885Sbrandon.potter@amd.com
2405513SMichael.Adler@intel.com/// Target truncate64() handler.
2411999SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num,
2421999SN/A                             LiveProcess *p, ThreadContext *tc);
24311851Sbrandon.potter@amd.com
2441999SN/A/// Target ftruncate64() handler.
24511885Sbrandon.potter@amd.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
24611885Sbrandon.potter@amd.com                              LiveProcess *p, ThreadContext *tc);
24711885Sbrandon.potter@amd.com
2481999SN/A
2491999SN/A/// Target umask() handler.
2501999SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num,
25111851Sbrandon.potter@amd.com                        LiveProcess *p, ThreadContext *tc);
2521999SN/A
2533079Sstever@eecs.umich.edu
2543079Sstever@eecs.umich.edu/// Target chown() handler.
25511851Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num,
2563079Sstever@eecs.umich.edu                        LiveProcess *p, ThreadContext *tc);
25711908SBrandon.Potter@amd.com
25811908SBrandon.Potter@amd.com
25911908SBrandon.Potter@amd.com/// Target fchown() handler.
26011908SBrandon.Potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num,
26111875Sbrandon.potter@amd.com                         LiveProcess *p, ThreadContext *tc);
2622093SN/A
26311851Sbrandon.potter@amd.com/// Target dup() handler.
2642093SN/ASyscallReturn dupFunc(SyscallDesc *desc, int num,
2652687Sksewell@umich.edu                      LiveProcess *process, ThreadContext *tc);
2662687Sksewell@umich.edu
26711851Sbrandon.potter@amd.com/// Target fnctl() handler.
2682687Sksewell@umich.eduSyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2692238SN/A                        LiveProcess *process, ThreadContext *tc);
2702238SN/A
27111851Sbrandon.potter@amd.com/// Target fcntl64() handler.
2722238SN/ASyscallReturn fcntl64Func(SyscallDesc *desc, int num,
27311908SBrandon.Potter@amd.com                        LiveProcess *process, ThreadContext *tc);
27411908SBrandon.Potter@amd.com
27511908SBrandon.Potter@amd.com/// Target setuid() handler.
27611908SBrandon.Potter@amd.comSyscallReturn setuidFunc(SyscallDesc *desc, int num,
27711908SBrandon.Potter@amd.com                               LiveProcess *p, ThreadContext *tc);
27811908SBrandon.Potter@amd.com
27911908SBrandon.Potter@amd.com/// Target getpid() handler.
28011908SBrandon.Potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num,
2812238SN/A                               LiveProcess *p, ThreadContext *tc);
2822238SN/A
28311851Sbrandon.potter@amd.com/// Target getuid() handler.
2842238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2852238SN/A                               LiveProcess *p, ThreadContext *tc);
2862238SN/A
28711851Sbrandon.potter@amd.com/// Target getgid() handler.
2882238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num,
2892238SN/A                               LiveProcess *p, ThreadContext *tc);
2902238SN/A
29111851Sbrandon.potter@amd.com/// Target getppid() handler.
2922238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num,
2932238SN/A                               LiveProcess *p, ThreadContext *tc);
2942238SN/A
29511851Sbrandon.potter@amd.com/// Target geteuid() handler.
2962238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2972238SN/A                               LiveProcess *p, ThreadContext *tc);
2982238SN/A
29911851Sbrandon.potter@amd.com/// Target getegid() handler.
3002238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num,
3012238SN/A                               LiveProcess *p, ThreadContext *tc);
3022238SN/A
30311851Sbrandon.potter@amd.com/// Target clone() handler.
3042238SN/ASyscallReturn cloneFunc(SyscallDesc *desc, int num,
3059455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc);
3069455Smitch.hayenga+gem5@gmail.com
30711851Sbrandon.potter@amd.com/// Target access() handler
30810203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
30911851Sbrandon.potter@amd.com                               LiveProcess *p, ThreadContext *tc);
31011851Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
3119455Smitch.hayenga+gem5@gmail.com                               LiveProcess *p, ThreadContext *tc,
3129112Smarc.orr@gmail.com                               int index);
31311906SBrandon.Potter@amd.com
31411906SBrandon.Potter@amd.com/// Futex system call
3159112Smarc.orr@gmail.com///  Implemented by Daniel Sanchez
3169112Smarc.orr@gmail.com///  Used by printf's in multi-threaded apps
31711851Sbrandon.potter@amd.comtemplate <class OS>
3189112Smarc.orr@gmail.comSyscallReturn
3199112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
32011911SBrandon.Potter@amd.com          ThreadContext *tc)
3219112Smarc.orr@gmail.com{
32211911SBrandon.Potter@amd.com    int index_uaddr = 0;
32311911SBrandon.Potter@amd.com    int index_op = 1;
32411911SBrandon.Potter@amd.com    int index_val = 2;
32511911SBrandon.Potter@amd.com    int index_timeout = 3;
3269112Smarc.orr@gmail.com
32711911SBrandon.Potter@amd.com    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
32811911SBrandon.Potter@amd.com    int op = process->getSyscallArg(tc, index_op);
32911911SBrandon.Potter@amd.com    int val = process->getSyscallArg(tc, index_val);
33011911SBrandon.Potter@amd.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
3319238Slluc.alvarez@bsc.es
3329112Smarc.orr@gmail.com    std::map<uint64_t, std::list<ThreadContext *> * >
33311911SBrandon.Potter@amd.com        &futex_map = tc->getSystemPtr()->futexMap;
3349112Smarc.orr@gmail.com
33511911SBrandon.Potter@amd.com    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
33611911SBrandon.Potter@amd.com            uaddr, op, val);
33711911SBrandon.Potter@amd.com
33811911SBrandon.Potter@amd.com    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
33911911SBrandon.Potter@amd.com
3409112Smarc.orr@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
34111911SBrandon.Potter@amd.com        if (timeout != 0) {
34211911SBrandon.Potter@amd.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
34311911SBrandon.Potter@amd.com                 "we'll wait indefinitely");
34411911SBrandon.Potter@amd.com        }
34511911SBrandon.Potter@amd.com
34611911SBrandon.Potter@amd.com        uint8_t *buf = new uint8_t[sizeof(int)];
3479112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
3489112Smarc.orr@gmail.com        int mem_val = *((int *)buf);
34911911SBrandon.Potter@amd.com        delete[] buf;
35011911SBrandon.Potter@amd.com
3519112Smarc.orr@gmail.com        if (val != mem_val) {
35211911SBrandon.Potter@amd.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
35311911SBrandon.Potter@amd.com                                    "expected: %d\n", mem_val, val);
3549112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
3559112Smarc.orr@gmail.com        }
35611911SBrandon.Potter@amd.com
35711911SBrandon.Potter@amd.com        // Queue the thread context
3589112Smarc.orr@gmail.com        std::list<ThreadContext *> * tcWaitList;
3599112Smarc.orr@gmail.com        if (futex_map.count(uaddr)) {
3602238SN/A            tcWaitList = futex_map.find(uaddr)->second;
3612238SN/A        } else {
3622238SN/A            tcWaitList = new std::list<ThreadContext *>();
3632238SN/A            futex_map.insert(std::pair< uint64_t,
36411851Sbrandon.potter@amd.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
3652238SN/A        }
3662238SN/A        tcWaitList->push_back(tc);
3672238SN/A        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
36811851Sbrandon.potter@amd.com                                "thread context\n");
3692238SN/A        tc->suspend();
3702238SN/A        return 0;
3712238SN/A    } else if (op == OS::TGT_FUTEX_WAKE){
37211851Sbrandon.potter@amd.com        int wokenUp = 0;
3732238SN/A        std::list<ThreadContext *> * tcWaitList;
3742238SN/A        if (futex_map.count(uaddr)) {
3752238SN/A            tcWaitList = futex_map.find(uaddr)->second;
37611851Sbrandon.potter@amd.com            while (tcWaitList->size() > 0 && wokenUp < val) {
3772238SN/A                tcWaitList->front()->activate();
3782238SN/A                tcWaitList->pop_front();
3791354SN/A                wokenUp++;
3801354SN/A            }
38110796Sbrandon.potter@amd.com            if (tcWaitList->empty()) {
38210796Sbrandon.potter@amd.com                futex_map.erase(uaddr);
3831354SN/A                delete tcWaitList;
3841354SN/A            }
3851354SN/A        }
3861354SN/A        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
3871354SN/A                                "thread contexts\n", wokenUp);
3881354SN/A        return wokenUp;
3891354SN/A    } else {
3901354SN/A        warn("sys_futex: op %d is not implemented, just returning...", op);
3911354SN/A        return 0;
3921354SN/A    }
39310796Sbrandon.potter@amd.com
3941354SN/A}
39510796Sbrandon.potter@amd.com
3961354SN/A
3971354SN/A/// Pseudo Funcs  - These functions use a different return convension,
3981354SN/A/// returning a second value in a register other than the normal return register
3991354SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
40010796Sbrandon.potter@amd.com                             LiveProcess *process, ThreadContext *tc);
40110796Sbrandon.potter@amd.com
40210796Sbrandon.potter@amd.com/// Target getpidPseudo() handler.
40310796Sbrandon.potter@amd.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
40410796Sbrandon.potter@amd.com                               LiveProcess *p, ThreadContext *tc);
40510796Sbrandon.potter@amd.com
40610796Sbrandon.potter@amd.com/// Target getuidPseudo() handler.
40710796Sbrandon.potter@amd.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
40810796Sbrandon.potter@amd.com                               LiveProcess *p, ThreadContext *tc);
40910796Sbrandon.potter@amd.com
41010796Sbrandon.potter@amd.com/// Target getgidPseudo() handler.
411360SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
412360SN/A                               LiveProcess *p, ThreadContext *tc);
413360SN/A
414360SN/A
415360SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
416360SN/Aconst int one_million = 1000000;
417360SN/A/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
41811759Sbrandon.potter@amd.comconst int one_billion = 1000000000;
4193113Sgblack@eecs.umich.edu
4203113Sgblack@eecs.umich.edu/// Approximate seconds since the epoch (1/1/1970).  About a billion,
4213113Sgblack@eecs.umich.edu/// by my reckoning.  We want to keep this a constant (not use the
4223113Sgblack@eecs.umich.edu/// real-world time) to keep simulations repeatable.
4233113Sgblack@eecs.umich.educonst unsigned seconds_since_epoch = 1000000000;
4243113Sgblack@eecs.umich.edu
4253113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
4263113Sgblack@eecs.umich.edu/// microseconds.
4273113Sgblack@eecs.umich.edutemplate <class T1, class T2>
4283113Sgblack@eecs.umich.eduvoid
4293113Sgblack@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec)
4303113Sgblack@eecs.umich.edu{
4313113Sgblack@eecs.umich.edu    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
43212032Sandreas.sandberg@arm.com    sec = elapsed_usecs / one_million;
4333113Sgblack@eecs.umich.edu    usec = elapsed_usecs % one_million;
4343113Sgblack@eecs.umich.edu}
4354189Sgblack@eecs.umich.edu
4364189Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and
4373113Sgblack@eecs.umich.edu/// nanoseconds.
4383113Sgblack@eecs.umich.edutemplate <class T1, class T2>
4393113Sgblack@eecs.umich.eduvoid
4403113Sgblack@eecs.umich.edugetElapsedTimeNano(T1 &sec, T2 &nsec)
4418737Skoansin.tan@gmail.com{
4423113Sgblack@eecs.umich.edu    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
4438737Skoansin.tan@gmail.com    sec = elapsed_nsecs / one_billion;
4443277Sgblack@eecs.umich.edu    nsec = elapsed_nsecs % one_billion;
4455515SMichael.Adler@intel.com}
4465515SMichael.Adler@intel.com
4475515SMichael.Adler@intel.com//////////////////////////////////////////////////////////////////////
4485515SMichael.Adler@intel.com//
4495515SMichael.Adler@intel.com// The following emulation functions are generic, but need to be
4508737Skoansin.tan@gmail.com// templated to account for differences in types, constants, etc.
4513277Sgblack@eecs.umich.edu//
4528737Skoansin.tan@gmail.com//////////////////////////////////////////////////////////////////////
4533277Sgblack@eecs.umich.edu
4548737Skoansin.tan@gmail.com#if NO_STAT64
4553277Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4568737Skoansin.tan@gmail.com    typedef struct stat hst_stat64;
4573113Sgblack@eecs.umich.edu#else
4583113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4593113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4603113Sgblack@eecs.umich.edu#endif
4618737Skoansin.tan@gmail.com
4623113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4638737Skoansin.tan@gmail.com//// buffer.  Also copies the target buffer out to the simulated
4643114Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4658737Skoansin.tan@gmail.com
4663114Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat>
4678737Skoansin.tan@gmail.comstatic void
4683114Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4698737Skoansin.tan@gmail.com{
47011906SBrandon.Potter@amd.com    using namespace TheISA;
4714061Sgblack@eecs.umich.edu
4724061Sgblack@eecs.umich.edu    if (fakeTTY)
4738737Skoansin.tan@gmail.com        tgt->st_dev = 0xA;
4743113Sgblack@eecs.umich.edu    else
4758737Skoansin.tan@gmail.com        tgt->st_dev = host->st_dev;
4763113Sgblack@eecs.umich.edu    tgt->st_dev = TheISA::htog(tgt->st_dev);
4773113Sgblack@eecs.umich.edu    tgt->st_ino = host->st_ino;
4783113Sgblack@eecs.umich.edu    tgt->st_ino = TheISA::htog(tgt->st_ino);
4793113Sgblack@eecs.umich.edu    tgt->st_mode = host->st_mode;
4803113Sgblack@eecs.umich.edu    if (fakeTTY) {
48112032Sandreas.sandberg@arm.com        // Claim to be a character device
4823113Sgblack@eecs.umich.edu        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4833113Sgblack@eecs.umich.edu        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4844189Sgblack@eecs.umich.edu    }
4854189Sgblack@eecs.umich.edu    tgt->st_mode = TheISA::htog(tgt->st_mode);
4863113Sgblack@eecs.umich.edu    tgt->st_nlink = host->st_nlink;
4873113Sgblack@eecs.umich.edu    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4883113Sgblack@eecs.umich.edu    tgt->st_uid = host->st_uid;
4898737Skoansin.tan@gmail.com    tgt->st_uid = TheISA::htog(tgt->st_uid);
4903113Sgblack@eecs.umich.edu    tgt->st_gid = host->st_gid;
4918737Skoansin.tan@gmail.com    tgt->st_gid = TheISA::htog(tgt->st_gid);
4923113Sgblack@eecs.umich.edu    if (fakeTTY)
4938737Skoansin.tan@gmail.com        tgt->st_rdev = 0x880d;
4943113Sgblack@eecs.umich.edu    else
4953113Sgblack@eecs.umich.edu        tgt->st_rdev = host->st_rdev;
4963113Sgblack@eecs.umich.edu    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4973113Sgblack@eecs.umich.edu    tgt->st_size = host->st_size;
4983113Sgblack@eecs.umich.edu    tgt->st_size = TheISA::htog(tgt->st_size);
4993113Sgblack@eecs.umich.edu    tgt->st_atimeX = host->st_atime;
5003113Sgblack@eecs.umich.edu    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
50111906SBrandon.Potter@amd.com    tgt->st_mtimeX = host->st_mtime;
5023113Sgblack@eecs.umich.edu    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
50312032Sandreas.sandberg@arm.com    tgt->st_ctimeX = host->st_ctime;
5048852Sandreas.hansson@arm.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
50511906SBrandon.Potter@amd.com    // Force the block size to be 8k. This helps to ensure buffered io works
5063113Sgblack@eecs.umich.edu    // consistently across different hosts.
5073113Sgblack@eecs.umich.edu    tgt->st_blksize = 0x2000;
5083113Sgblack@eecs.umich.edu    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
5093113Sgblack@eecs.umich.edu    tgt->st_blocks = host->st_blocks;
5103113Sgblack@eecs.umich.edu    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
5113113Sgblack@eecs.umich.edu}
5123113Sgblack@eecs.umich.edu
5133113Sgblack@eecs.umich.edu// Same for stat64
51412032Sandreas.sandberg@arm.com
5158852Sandreas.hansson@arm.comtemplate <typename target_stat, typename host_stat64>
51611906SBrandon.Potter@amd.comstatic void
5173113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
5183113Sgblack@eecs.umich.edu{
5193113Sgblack@eecs.umich.edu    using namespace TheISA;
5206686Stjones1@inf.ed.ac.uk
5213113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
5223113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
5233113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = host->st_atime_nsec;
52411759Sbrandon.potter@amd.com    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
52512032Sandreas.sandberg@arm.com    tgt->st_mtime_nsec = host->st_mtime_nsec;
52611759Sbrandon.potter@amd.com    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
52711759Sbrandon.potter@amd.com    tgt->st_ctime_nsec = host->st_ctime_nsec;
52811759Sbrandon.potter@amd.com    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
52911759Sbrandon.potter@amd.com#else
53011759Sbrandon.potter@amd.com    tgt->st_atime_nsec = 0;
53111812Sbaz21@cam.ac.uk    tgt->st_mtime_nsec = 0;
53211812Sbaz21@cam.ac.uk    tgt->st_ctime_nsec = 0;
53311812Sbaz21@cam.ac.uk#endif
53411759Sbrandon.potter@amd.com}
53511812Sbaz21@cam.ac.uk
53611759Sbrandon.potter@amd.com//Here are a couple convenience functions
53711759Sbrandon.potter@amd.comtemplate<class OS>
53811759Sbrandon.potter@amd.comstatic void
53911759Sbrandon.potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
54011759Sbrandon.potter@amd.com        hst_stat *host, bool fakeTTY = false)
54111759Sbrandon.potter@amd.com{
54211759Sbrandon.potter@amd.com    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
54311812Sbaz21@cam.ac.uk    tgt_stat_buf tgt(addr);
54411812Sbaz21@cam.ac.uk    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
54511812Sbaz21@cam.ac.uk    tgt.copyOut(mem);
54611812Sbaz21@cam.ac.uk}
54711812Sbaz21@cam.ac.uk
54811812Sbaz21@cam.ac.uktemplate<class OS>
54911812Sbaz21@cam.ac.ukstatic void
55011759Sbrandon.potter@amd.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
55111759Sbrandon.potter@amd.com        hst_stat64 *host, bool fakeTTY = false)
55211812Sbaz21@cam.ac.uk{
55311812Sbaz21@cam.ac.uk    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
55411759Sbrandon.potter@amd.com    tgt_stat_buf tgt(addr);
55511812Sbaz21@cam.ac.uk    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
55611812Sbaz21@cam.ac.uk    tgt.copyOut(mem);
55711812Sbaz21@cam.ac.uk}
55811812Sbaz21@cam.ac.uk
55911812Sbaz21@cam.ac.uk/// Target ioctl() handler.  For the most part, programs call ioctl()
56011812Sbaz21@cam.ac.uk/// only to find out if their stdout is a tty, to determine whether to
56111812Sbaz21@cam.ac.uk/// do line or block buffering.  We always claim that output fds are
56211759Sbrandon.potter@amd.com/// not TTYs to provide repeatable results.
56311759Sbrandon.potter@amd.comtemplate <class OS>
56411759Sbrandon.potter@amd.comSyscallReturn
56511759Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
566378SN/A          ThreadContext *tc)
567378SN/A{
5689141Smarc.orr@gmail.com    int index = 0;
5699141Smarc.orr@gmail.com    int tgt_fd = process->getSyscallArg(tc, index);
570360SN/A    unsigned req = process->getSyscallArg(tc, index);
5711450SN/A
57211856Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
573360SN/A
5746701Sgblack@eecs.umich.edu    FDEntry *fde = process->getFDEntry(tgt_fd);
57511856Sbrandon.potter@amd.com
57611856Sbrandon.potter@amd.com    if (fde == NULL) {
577360SN/A        // doesn't map to any simulator fd: not a valid target fd
57810930Sbrandon.potter@amd.com        return -EBADF;
579360SN/A    }
58011856Sbrandon.potter@amd.com
58111856Sbrandon.potter@amd.com    if (fde->driver != NULL) {
58210496Ssteve.reinhardt@amd.com        return fde->driver->ioctl(process, tc, req);
58311856Sbrandon.potter@amd.com    }
58411856Sbrandon.potter@amd.com
5851458SN/A    if (OS::isTtyReq(req)) {
586360SN/A        return -ENOTTY;
58711856Sbrandon.potter@amd.com    }
58811856Sbrandon.potter@amd.com
58911856Sbrandon.potter@amd.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
59011856Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
59111856Sbrandon.potter@amd.com    return -ENOTTY;
59211856Sbrandon.potter@amd.com}
59311856Sbrandon.potter@amd.com
59411856Sbrandon.potter@amd.comtemplate <class OS>
59510496Ssteve.reinhardt@amd.comstatic SyscallReturn
59611856Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
59711856Sbrandon.potter@amd.com         ThreadContext *tc, int index)
59811856Sbrandon.potter@amd.com{
59911856Sbrandon.potter@amd.com    std::string path;
60011856Sbrandon.potter@amd.com
60110930Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
6029141Smarc.orr@gmail.com                process->getSyscallArg(tc, index)))
603360SN/A        return -EFAULT;
604360SN/A
605360SN/A    int tgtFlags = process->getSyscallArg(tc, index);
60611907SBrandon.Potter@amd.com    int mode = process->getSyscallArg(tc, index);
60711907SBrandon.Potter@amd.com    int hostFlags = 0;
60811907SBrandon.Potter@amd.com
609360SN/A    // translate open flags
61011907SBrandon.Potter@amd.com    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
61111907SBrandon.Potter@amd.com        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
61211907SBrandon.Potter@amd.com            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
61311907SBrandon.Potter@amd.com            hostFlags |= OS::openFlagTable[i].hostFlag;
61411907SBrandon.Potter@amd.com        }
61511907SBrandon.Potter@amd.com    }
61611907SBrandon.Potter@amd.com
61711907SBrandon.Potter@amd.com    // any target flags left?
61811907SBrandon.Potter@amd.com    if (tgtFlags != 0)
61911907SBrandon.Potter@amd.com        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
62011907SBrandon.Potter@amd.com
62111907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
62211907SBrandon.Potter@amd.com    hostFlags |= O_BINARY;
62311907SBrandon.Potter@amd.com#endif
624360SN/A
62511907SBrandon.Potter@amd.com    // Adjust path for current working directory
6261458SN/A    path = process->fullPath(path);
627360SN/A
62811907SBrandon.Potter@amd.com    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
62911907SBrandon.Potter@amd.com
63011907SBrandon.Potter@amd.com    if (startswith(path, "/dev/")) {
63111907SBrandon.Potter@amd.com        std::string filename = path.substr(strlen("/dev/"));
63211907SBrandon.Potter@amd.com        if (filename == "sysdev0") {
63311907SBrandon.Potter@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
63411907SBrandon.Potter@amd.com            // We don't support it, so just punt.
63511907SBrandon.Potter@amd.com            warn("Ignoring open(%s, ...)\n", path);
63611907SBrandon.Potter@amd.com            return -ENOENT;
63711907SBrandon.Potter@amd.com        }
638360SN/A
63911907SBrandon.Potter@amd.com        EmulatedDriver *drv = process->findDriver(filename);
64011907SBrandon.Potter@amd.com        if (drv != NULL) {
64111907SBrandon.Potter@amd.com            // the driver's open method will allocate a fd from the
642360SN/A            // process if necessary.
643360SN/A            return drv->open(process, tc, mode, hostFlags);
64411907SBrandon.Potter@amd.com        }
64511907SBrandon.Potter@amd.com
64611907SBrandon.Potter@amd.com        // fall through here for pass through to host devices, such as
64711907SBrandon.Potter@amd.com        // /dev/zero
648360SN/A    }
64911907SBrandon.Potter@amd.com
650360SN/A    int fd;
651360SN/A    int local_errno;
65211907SBrandon.Potter@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
6533669Sbinkertn@umich.edu        startswith(path, "/platform/") || startswith(path, "/sys/")) {
65411907SBrandon.Potter@amd.com        // It's a proc/sys entry and requires special handling
65511907SBrandon.Potter@amd.com        fd = OS::openSpecialFile(path, process, tc);
65611907SBrandon.Potter@amd.com        local_errno = ENOENT;
65711907SBrandon.Potter@amd.com     } else {
65811907SBrandon.Potter@amd.com        // open the file
65911907SBrandon.Potter@amd.com        fd = open(path.c_str(), hostFlags, mode);
66011907SBrandon.Potter@amd.com        local_errno = errno;
66111907SBrandon.Potter@amd.com     }
66211907SBrandon.Potter@amd.com
66311907SBrandon.Potter@amd.com    if (fd == -1)
66411907SBrandon.Potter@amd.com        return -local_errno;
66511907SBrandon.Potter@amd.com
66611907SBrandon.Potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
66711907SBrandon.Potter@amd.com}
66811907SBrandon.Potter@amd.com
66911907SBrandon.Potter@amd.com/// Target open() handler.
67011907SBrandon.Potter@amd.comtemplate <class OS>
67111907SBrandon.Potter@amd.comSyscallReturn
67211907SBrandon.Potter@amd.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
67311907SBrandon.Potter@amd.com         ThreadContext *tc)
67411907SBrandon.Potter@amd.com{
6751706SN/A    return openFunc<OS>(desc, callnum, process, tc, 0);
67611907SBrandon.Potter@amd.com}
67711907SBrandon.Potter@amd.com
67811907SBrandon.Potter@amd.com/// Target openat() handler.
67911907SBrandon.Potter@amd.comtemplate <class OS>
68011907SBrandon.Potter@amd.comSyscallReturn
68111907SBrandon.Potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
68210496Ssteve.reinhardt@amd.com         ThreadContext *tc)
68310496Ssteve.reinhardt@amd.com{
68411907SBrandon.Potter@amd.com    int index = 0;
68511907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
68611907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
68711907SBrandon.Potter@amd.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
68811907SBrandon.Potter@amd.com    return openFunc<OS>(desc, callnum, process, tc, 1);
68911907SBrandon.Potter@amd.com}
69010496Ssteve.reinhardt@amd.com
69111907SBrandon.Potter@amd.com/// Target unlinkat() handler.
69211907SBrandon.Potter@amd.comtemplate <class OS>
69311907SBrandon.Potter@amd.comSyscallReturn
69411907SBrandon.Potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
69510496Ssteve.reinhardt@amd.com             ThreadContext *tc)
69610496Ssteve.reinhardt@amd.com{
69711907SBrandon.Potter@amd.com    int index = 0;
69811907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
69911907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
70011907SBrandon.Potter@amd.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
70111907SBrandon.Potter@amd.com
70211907SBrandon.Potter@amd.com    return unlinkHelper(desc, callnum, process, tc, 1);
70311907SBrandon.Potter@amd.com}
70411907SBrandon.Potter@amd.com
70511907SBrandon.Potter@amd.com/// Target facessat() handler
70611907SBrandon.Potter@amd.comtemplate <class OS>
70711907SBrandon.Potter@amd.comSyscallReturn
70811907SBrandon.Potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
70911907SBrandon.Potter@amd.com        ThreadContext *tc)
71011907SBrandon.Potter@amd.com{
71111907SBrandon.Potter@amd.com    int index = 0;
71211907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
71311907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
71411907SBrandon.Potter@amd.com        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
71511907SBrandon.Potter@amd.com    return accessFunc(desc, callnum, process, tc, 1);
71611907SBrandon.Potter@amd.com}
71711907SBrandon.Potter@amd.com
71811907SBrandon.Potter@amd.com/// Target readlinkat() handler
71911907SBrandon.Potter@amd.comtemplate <class OS>
72011907SBrandon.Potter@amd.comSyscallReturn
72111907SBrandon.Potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
722360SN/A        ThreadContext *tc)
72311907SBrandon.Potter@amd.com{
72411907SBrandon.Potter@amd.com    int index = 0;
72511907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
72611907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
72711907SBrandon.Potter@amd.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
72811907SBrandon.Potter@amd.com    return readlinkFunc(desc, callnum, process, tc, 1);
72911907SBrandon.Potter@amd.com}
73011907SBrandon.Potter@amd.com
73111907SBrandon.Potter@amd.com/// Target renameat() handler.
73211907SBrandon.Potter@amd.comtemplate <class OS>
73311907SBrandon.Potter@amd.comSyscallReturn
73411907SBrandon.Potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
73511907SBrandon.Potter@amd.com             ThreadContext *tc)
736360SN/A{
737360SN/A    int index = 0;
73810027SChris.Adeniyi-Jones@arm.com
73910027SChris.Adeniyi-Jones@arm.com    int olddirfd = process->getSyscallArg(tc, index);
74010027SChris.Adeniyi-Jones@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
74111851Sbrandon.potter@amd.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
74210027SChris.Adeniyi-Jones@arm.com
74310027SChris.Adeniyi-Jones@arm.com    std::string old_name;
74411907SBrandon.Potter@amd.com
74510027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(old_name,
74610027SChris.Adeniyi-Jones@arm.com                                         process->getSyscallArg(tc, index)))
74710027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
74810027SChris.Adeniyi-Jones@arm.com
74910027SChris.Adeniyi-Jones@arm.com    int newdirfd = process->getSyscallArg(tc, index);
75011851Sbrandon.potter@amd.com    if (newdirfd != OS::TGT_AT_FDCWD)
75111851Sbrandon.potter@amd.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
75210027SChris.Adeniyi-Jones@arm.com
75311907SBrandon.Potter@amd.com    std::string new_name;
75410027SChris.Adeniyi-Jones@arm.com
75510027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(new_name,
75610633Smichaelupton@gmail.com                                         process->getSyscallArg(tc, index)))
75710633Smichaelupton@gmail.com        return -EFAULT;
75810633Smichaelupton@gmail.com
75911851Sbrandon.potter@amd.com    // Adjust path for current working directory
76010633Smichaelupton@gmail.com    old_name = process->fullPath(old_name);
76110633Smichaelupton@gmail.com    new_name = process->fullPath(new_name);
76210633Smichaelupton@gmail.com
76310633Smichaelupton@gmail.com    int result = rename(old_name.c_str(), new_name.c_str());
76410633Smichaelupton@gmail.com    return (result == -1) ? -errno : result;
76510633Smichaelupton@gmail.com}
76610633Smichaelupton@gmail.com
76710633Smichaelupton@gmail.com/// Target sysinfo() handler.
76810633Smichaelupton@gmail.comtemplate <class OS>
76910633Smichaelupton@gmail.comSyscallReturn
77010203SAli.Saidi@ARM.comsysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
77110203SAli.Saidi@ARM.com         ThreadContext *tc)
77210203SAli.Saidi@ARM.com{
77311851Sbrandon.potter@amd.com
77411851Sbrandon.potter@amd.com    int index = 0;
77510203SAli.Saidi@ARM.com    TypedBufferArg<typename OS::tgt_sysinfo>
77610203SAli.Saidi@ARM.com        sysinfo(process->getSyscallArg(tc, index));
77710203SAli.Saidi@ARM.com
77810203SAli.Saidi@ARM.com    sysinfo->uptime=seconds_since_epoch;
77910203SAli.Saidi@ARM.com    sysinfo->totalram=process->system->memSize();
78010203SAli.Saidi@ARM.com
78110203SAli.Saidi@ARM.com    sysinfo.copyOut(tc->getMemProxy());
78210203SAli.Saidi@ARM.com
78310203SAli.Saidi@ARM.com    return 0;
78410203SAli.Saidi@ARM.com}
78510203SAli.Saidi@ARM.com
78611851Sbrandon.potter@amd.com/// Target chmod() handler.
78711851Sbrandon.potter@amd.comtemplate <class OS>
78810203SAli.Saidi@ARM.comSyscallReturn
78910203SAli.Saidi@ARM.comchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
79010203SAli.Saidi@ARM.com          ThreadContext *tc)
79110203SAli.Saidi@ARM.com{
79210203SAli.Saidi@ARM.com    std::string path;
79310203SAli.Saidi@ARM.com
79410203SAli.Saidi@ARM.com    int index = 0;
79510203SAli.Saidi@ARM.com    if (!tc->getMemProxy().tryReadString(path,
79610850SGiacomo.Gabrielli@arm.com                process->getSyscallArg(tc, index))) {
79710850SGiacomo.Gabrielli@arm.com        return -EFAULT;
79810850SGiacomo.Gabrielli@arm.com    }
79911851Sbrandon.potter@amd.com
80010850SGiacomo.Gabrielli@arm.com    uint32_t mode = process->getSyscallArg(tc, index);
80110850SGiacomo.Gabrielli@arm.com    mode_t hostMode = 0;
80210850SGiacomo.Gabrielli@arm.com
80310850SGiacomo.Gabrielli@arm.com    // XXX translate mode flags via OS::something???
80410850SGiacomo.Gabrielli@arm.com    hostMode = mode;
80510850SGiacomo.Gabrielli@arm.com
80610850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
80710850SGiacomo.Gabrielli@arm.com    path = process->fullPath(path);
80810850SGiacomo.Gabrielli@arm.com
80910850SGiacomo.Gabrielli@arm.com    // do the chmod
81010850SGiacomo.Gabrielli@arm.com    int result = chmod(path.c_str(), hostMode);
81110850SGiacomo.Gabrielli@arm.com    if (result < 0)
81210850SGiacomo.Gabrielli@arm.com        return -errno;
81310850SGiacomo.Gabrielli@arm.com
81410850SGiacomo.Gabrielli@arm.com    return 0;
81510850SGiacomo.Gabrielli@arm.com}
81610850SGiacomo.Gabrielli@arm.com
81710850SGiacomo.Gabrielli@arm.com
81810850SGiacomo.Gabrielli@arm.com/// Target fchmod() handler.
81910850SGiacomo.Gabrielli@arm.comtemplate <class OS>
82010850SGiacomo.Gabrielli@arm.comSyscallReturn
82110850SGiacomo.Gabrielli@arm.comfchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
82210850SGiacomo.Gabrielli@arm.com           ThreadContext *tc)
82310850SGiacomo.Gabrielli@arm.com{
82410850SGiacomo.Gabrielli@arm.com    int index = 0;
82510850SGiacomo.Gabrielli@arm.com    int tgt_fd = process->getSyscallArg(tc, index);
82610850SGiacomo.Gabrielli@arm.com    uint32_t mode = process->getSyscallArg(tc, index);
82710850SGiacomo.Gabrielli@arm.com
82810850SGiacomo.Gabrielli@arm.com    int sim_fd = process->getSimFD(tgt_fd);
82910850SGiacomo.Gabrielli@arm.com    if (sim_fd < 0)
83010850SGiacomo.Gabrielli@arm.com        return -EBADF;
83110850SGiacomo.Gabrielli@arm.com
8326640Svince@csl.cornell.edu    mode_t hostMode = 0;
8336640Svince@csl.cornell.edu
8346640Svince@csl.cornell.edu    // XXX translate mode flags via OS::someting???
83511851Sbrandon.potter@amd.com    hostMode = mode;
83611851Sbrandon.potter@amd.com
8376640Svince@csl.cornell.edu    // do the fchmod
8386640Svince@csl.cornell.edu    int result = fchmod(sim_fd, hostMode);
8396701Sgblack@eecs.umich.edu    if (result < 0)
8406701Sgblack@eecs.umich.edu        return -errno;
84110793Sbrandon.potter@amd.com
8426640Svince@csl.cornell.edu    return 0;
84311758Sbrandon.potter@amd.com}
84411758Sbrandon.potter@amd.com
84511758Sbrandon.potter@amd.com/// Target mremap() handler.
8466640Svince@csl.cornell.edutemplate <class OS>
8478706Sandreas.hansson@arm.comSyscallReturn
8486640Svince@csl.cornell.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc)
8496701Sgblack@eecs.umich.edu{
8506640Svince@csl.cornell.edu    int index = 0;
851360SN/A    Addr start = process->getSyscallArg(tc, index);
8521999SN/A    uint64_t old_length = process->getSyscallArg(tc, index);
8531999SN/A    uint64_t new_length = process->getSyscallArg(tc, index);
8541999SN/A    uint64_t flags = process->getSyscallArg(tc, index);
85511851Sbrandon.potter@amd.com    uint64_t provided_address = 0;
8562680Sktlim@umich.edu    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
8571999SN/A
8581999SN/A    if (use_provided_address)
8591999SN/A        provided_address = process->getSyscallArg(tc, index);
8606701Sgblack@eecs.umich.edu
8618852Sandreas.hansson@arm.com    if ((start % TheISA::PageBytes != 0) ||
8626701Sgblack@eecs.umich.edu        (provided_address % TheISA::PageBytes != 0)) {
8631999SN/A        warn("mremap failing: arguments not page aligned");
8646701Sgblack@eecs.umich.edu        return -EINVAL;
8651999SN/A    }
8666701Sgblack@eecs.umich.edu
8671999SN/A    new_length = roundUp(new_length, TheISA::PageBytes);
8681999SN/A
8691999SN/A    if (new_length > old_length) {
8701999SN/A        if ((start + old_length) == process->mmap_end &&
8711999SN/A            (!use_provided_address || provided_address == start)) {
8723669Sbinkertn@umich.edu            uint64_t diff = new_length - old_length;
8733669Sbinkertn@umich.edu            process->allocateMem(process->mmap_end, diff);
8743669Sbinkertn@umich.edu            process->mmap_end += diff;
8751999SN/A            return start;
8761999SN/A        } else {
8771999SN/A            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
8782218SN/A                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8791999SN/A                return -ENOMEM;
8801999SN/A            } else {
8811999SN/A                uint64_t new_start = use_provided_address ?
8821999SN/A                    provided_address : process->mmap_end;
8831999SN/A                process->pTable->remap(start, old_length, new_start);
8841999SN/A                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
8851999SN/A                     new_start, new_start + new_length,
8861999SN/A                     new_length - old_length);
88711856Sbrandon.potter@amd.com                // add on the remaining unallocated pages
8881999SN/A                process->allocateMem(new_start + old_length,
8896701Sgblack@eecs.umich.edu                                     new_length - old_length,
89011856Sbrandon.potter@amd.com                                     use_provided_address /* clobber */);
89111856Sbrandon.potter@amd.com                if (!use_provided_address)
89210931Sbrandon.potter@amd.com                    process->mmap_end += new_length;
89311856Sbrandon.potter@amd.com                if (use_provided_address &&
89411856Sbrandon.potter@amd.com                    new_start + new_length > process->mmap_end) {
8951999SN/A                    // something fishy going on here, at least notify the user
89611856Sbrandon.potter@amd.com                    // @todo: increase mmap_end?
8971999SN/A                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
89811856Sbrandon.potter@amd.com                }
8991999SN/A                warn("returning %08p as start\n", new_start);
90011856Sbrandon.potter@amd.com                return new_start;
9011999SN/A            }
90211856Sbrandon.potter@amd.com        }
9031999SN/A    } else {
9041999SN/A        if (use_provided_address && provided_address != start)
9055877Shsul@eecs.umich.edu            process->pTable->remap(start, new_length, provided_address);
9065877Shsul@eecs.umich.edu        process->pTable->unmap(start + new_length, old_length - new_length);
9075877Shsul@eecs.umich.edu        return use_provided_address ? provided_address : start;
90811851Sbrandon.potter@amd.com    }
9095877Shsul@eecs.umich.edu}
9106701Sgblack@eecs.umich.edu
9116701Sgblack@eecs.umich.edu/// Target stat() handler.
9126701Sgblack@eecs.umich.edutemplate <class OS>
9136701Sgblack@eecs.umich.eduSyscallReturn
9146701Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
91510027SChris.Adeniyi-Jones@arm.com         ThreadContext *tc)
91610027SChris.Adeniyi-Jones@arm.com{
91710027SChris.Adeniyi-Jones@arm.com    std::string path;
91810027SChris.Adeniyi-Jones@arm.com
91910027SChris.Adeniyi-Jones@arm.com    int index = 0;
9205877Shsul@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
92110318Sandreas.hansson@arm.com                process->getSyscallArg(tc, index))) {
92210318Sandreas.hansson@arm.com        return -EFAULT;
9235877Shsul@eecs.umich.edu    }
9245877Shsul@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
9255877Shsul@eecs.umich.edu
9265877Shsul@eecs.umich.edu    // Adjust path for current working directory
92710486Stjablin@gmail.com    path = process->fullPath(path);
92810486Stjablin@gmail.com
9295877Shsul@eecs.umich.edu    struct stat hostBuf;
93011905SBrandon.Potter@amd.com    int result = stat(path.c_str(), &hostBuf);
93111905SBrandon.Potter@amd.com
93211905SBrandon.Potter@amd.com    if (result < 0)
93311905SBrandon.Potter@amd.com        return -errno;
93410027SChris.Adeniyi-Jones@arm.com
93512206Srico.amslinger@informatik.uni-augsburg.de    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
93612206Srico.amslinger@informatik.uni-augsburg.de
9375877Shsul@eecs.umich.edu    return 0;
93811905SBrandon.Potter@amd.com}
93911905SBrandon.Potter@amd.com
9405877Shsul@eecs.umich.edu
9415877Shsul@eecs.umich.edu/// Target stat64() handler.
94210027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
9435877Shsul@eecs.umich.eduSyscallReturn
9445877Shsul@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9455877Shsul@eecs.umich.edu           ThreadContext *tc)
94612206Srico.amslinger@informatik.uni-augsburg.de{
94712206Srico.amslinger@informatik.uni-augsburg.de    std::string path;
94812206Srico.amslinger@informatik.uni-augsburg.de
94912206Srico.amslinger@informatik.uni-augsburg.de    int index = 0;
95012206Srico.amslinger@informatik.uni-augsburg.de    if (!tc->getMemProxy().tryReadString(path,
95112206Srico.amslinger@informatik.uni-augsburg.de                process->getSyscallArg(tc, index)))
95212206Srico.amslinger@informatik.uni-augsburg.de        return -EFAULT;
95312206Srico.amslinger@informatik.uni-augsburg.de    Addr bufPtr = process->getSyscallArg(tc, index);
95412206Srico.amslinger@informatik.uni-augsburg.de
95510027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
95610027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
95710027SChris.Adeniyi-Jones@arm.com
95810027SChris.Adeniyi-Jones@arm.com#if NO_STAT64
9595877Shsul@eecs.umich.edu    struct stat  hostBuf;
96010027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
96110027SChris.Adeniyi-Jones@arm.com#else
96210027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
96310027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
96412206Srico.amslinger@informatik.uni-augsburg.de#endif
96512206Srico.amslinger@informatik.uni-augsburg.de
96612206Srico.amslinger@informatik.uni-augsburg.de    if (result < 0)
96712206Srico.amslinger@informatik.uni-augsburg.de        return -errno;
96810027SChris.Adeniyi-Jones@arm.com
96910027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
97010027SChris.Adeniyi-Jones@arm.com
97110027SChris.Adeniyi-Jones@arm.com    return 0;
97210027SChris.Adeniyi-Jones@arm.com}
97310027SChris.Adeniyi-Jones@arm.com
9745877Shsul@eecs.umich.edu
9755877Shsul@eecs.umich.edu/// Target fstatat64() handler.
9765877Shsul@eecs.umich.edutemplate <class OS>
97710027SChris.Adeniyi-Jones@arm.comSyscallReturn
97810027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
9798601Ssteve.reinhardt@amd.com              ThreadContext *tc)
98010027SChris.Adeniyi-Jones@arm.com{
9815877Shsul@eecs.umich.edu    int index = 0;
9825877Shsul@eecs.umich.edu    int dirfd = process->getSyscallArg(tc, index);
9831999SN/A    if (dirfd != OS::TGT_AT_FDCWD)
984378SN/A        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
985360SN/A
9861450SN/A    std::string path;
98711851Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
9882680Sktlim@umich.edu                process->getSyscallArg(tc, index)))
989360SN/A        return -EFAULT;
990360SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
991360SN/A
9926701Sgblack@eecs.umich.edu    // Adjust path for current working directory
9938852Sandreas.hansson@arm.com    path = process->fullPath(path);
9946701Sgblack@eecs.umich.edu
9956701Sgblack@eecs.umich.edu#if NO_STAT64
9966701Sgblack@eecs.umich.edu    struct stat  hostBuf;
9976701Sgblack@eecs.umich.edu    int result = stat(path.c_str(), &hostBuf);
998360SN/A#else
9993669Sbinkertn@umich.edu    struct stat64 hostBuf;
10003669Sbinkertn@umich.edu    int result = stat64(path.c_str(), &hostBuf);
10013669Sbinkertn@umich.edu#endif
1002360SN/A
1003360SN/A    if (result < 0)
1004360SN/A        return -errno;
1005360SN/A
10062218SN/A    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1007360SN/A
10088706Sandreas.hansson@arm.com    return 0;
1009360SN/A}
10101458SN/A
1011360SN/A
1012360SN/A/// Target fstat64() handler.
1013360SN/Atemplate <class OS>
10145074Ssaidi@eecs.umich.eduSyscallReturn
10155074Ssaidi@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
10165074Ssaidi@eecs.umich.edu            ThreadContext *tc)
101711851Sbrandon.potter@amd.com{
10185074Ssaidi@eecs.umich.edu    int index = 0;
10195074Ssaidi@eecs.umich.edu    int tgt_fd = process->getSyscallArg(tc, index);
10205074Ssaidi@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10215074Ssaidi@eecs.umich.edu
10226701Sgblack@eecs.umich.edu    int sim_fd = process->getSimFD(tgt_fd);
10238852Sandreas.hansson@arm.com    if (sim_fd < 0)
10246701Sgblack@eecs.umich.edu        return -EBADF;
10255074Ssaidi@eecs.umich.edu
10266701Sgblack@eecs.umich.edu#if NO_STAT64
10275074Ssaidi@eecs.umich.edu    struct stat  hostBuf;
10285074Ssaidi@eecs.umich.edu    int result = fstat(sim_fd, &hostBuf);
10295074Ssaidi@eecs.umich.edu#else
10305074Ssaidi@eecs.umich.edu    struct stat64  hostBuf;
10315208Ssaidi@eecs.umich.edu    int result = fstat64(sim_fd, &hostBuf);
10325208Ssaidi@eecs.umich.edu#endif
10335208Ssaidi@eecs.umich.edu
10345208Ssaidi@eecs.umich.edu    if (result < 0)
10355074Ssaidi@eecs.umich.edu        return -errno;
10365074Ssaidi@eecs.umich.edu
10375208Ssaidi@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10385074Ssaidi@eecs.umich.edu
10395074Ssaidi@eecs.umich.edu    return 0;
10405074Ssaidi@eecs.umich.edu}
10415074Ssaidi@eecs.umich.edu
10428706Sandreas.hansson@arm.com
10435074Ssaidi@eecs.umich.edu/// Target lstat() handler.
10445074Ssaidi@eecs.umich.edutemplate <class OS>
10455074Ssaidi@eecs.umich.eduSyscallReturn
10465074Ssaidi@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
10475074Ssaidi@eecs.umich.edu          ThreadContext *tc)
104810027SChris.Adeniyi-Jones@arm.com{
104910027SChris.Adeniyi-Jones@arm.com    std::string path;
105010027SChris.Adeniyi-Jones@arm.com
105111851Sbrandon.potter@amd.com    int index = 0;
105210027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
105310027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index))) {
105410027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
105510027SChris.Adeniyi-Jones@arm.com    }
105610027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
105710793Sbrandon.potter@amd.com
105810027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
105910027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
106010027SChris.Adeniyi-Jones@arm.com
106110027SChris.Adeniyi-Jones@arm.com    struct stat hostBuf;
106210027SChris.Adeniyi-Jones@arm.com    int result = lstat(path.c_str(), &hostBuf);
106310027SChris.Adeniyi-Jones@arm.com
106410027SChris.Adeniyi-Jones@arm.com    if (result < 0)
106510027SChris.Adeniyi-Jones@arm.com        return -errno;
106610027SChris.Adeniyi-Jones@arm.com
106710027SChris.Adeniyi-Jones@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
106810027SChris.Adeniyi-Jones@arm.com
106910027SChris.Adeniyi-Jones@arm.com    return 0;
107010027SChris.Adeniyi-Jones@arm.com}
107110027SChris.Adeniyi-Jones@arm.com
107210027SChris.Adeniyi-Jones@arm.com/// Target lstat64() handler.
107310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
107410027SChris.Adeniyi-Jones@arm.comSyscallReturn
107510027SChris.Adeniyi-Jones@arm.comlstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process,
107610027SChris.Adeniyi-Jones@arm.com            ThreadContext *tc)
107710027SChris.Adeniyi-Jones@arm.com{
107810027SChris.Adeniyi-Jones@arm.com    std::string path;
107910027SChris.Adeniyi-Jones@arm.com
108010027SChris.Adeniyi-Jones@arm.com    int index = 0;
108110027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
108210027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index))) {
108310027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
108410027SChris.Adeniyi-Jones@arm.com    }
10851999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
10861999SN/A
10871999SN/A    // Adjust path for current working directory
108811856Sbrandon.potter@amd.com    path = process->fullPath(path);
10891999SN/A
10906701Sgblack@eecs.umich.edu#if NO_STAT64
109111856Sbrandon.potter@amd.com    struct stat hostBuf;
109211856Sbrandon.potter@amd.com    int result = lstat(path.c_str(), &hostBuf);
109310931Sbrandon.potter@amd.com#else
109411856Sbrandon.potter@amd.com    struct stat64 hostBuf;
109511856Sbrandon.potter@amd.com    int result = lstat64(path.c_str(), &hostBuf);
10961999SN/A#endif
109711856Sbrandon.potter@amd.com
10981999SN/A    if (result < 0)
10992764Sstever@eecs.umich.edu        return -errno;
11002064SN/A
110110931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11022064SN/A
11032064SN/A    return 0;
110410931Sbrandon.potter@amd.com}
11052064SN/A
11061999SN/A/// Target fstat() handler.
11071999SN/Atemplate <class OS>
11082218SN/ASyscallReturn
11091999SN/AfstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
111010931Sbrandon.potter@amd.com          ThreadContext *tc)
11111999SN/A{
11121999SN/A    int index = 0;
11131999SN/A    int tgt_fd = process->getSyscallArg(tc, index);
11141999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
11151999SN/A
1116378SN/A    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
1117360SN/A
11181450SN/A    int sim_fd = process->getSimFD(tgt_fd);
111911851Sbrandon.potter@amd.com    if (sim_fd < 0)
11202680Sktlim@umich.edu        return -EBADF;
1121360SN/A
1122360SN/A    struct stat hostBuf;
1123360SN/A    int result = fstat(sim_fd, &hostBuf);
11246701Sgblack@eecs.umich.edu
11258852Sandreas.hansson@arm.com    if (result < 0)
11266701Sgblack@eecs.umich.edu        return -errno;
11276701Sgblack@eecs.umich.edu
11286701Sgblack@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11296701Sgblack@eecs.umich.edu
1130360SN/A    return 0;
11313669Sbinkertn@umich.edu}
11323669Sbinkertn@umich.edu
11333669Sbinkertn@umich.edu
1134360SN/A/// Target statfs() handler.
1135360SN/Atemplate <class OS>
1136360SN/ASyscallReturn
1137360SN/AstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11381458SN/A           ThreadContext *tc)
1139360SN/A{
11408706Sandreas.hansson@arm.com    std::string path;
1141360SN/A
11421458SN/A    int index = 0;
1143360SN/A    if (!tc->getMemProxy().tryReadString(path,
1144360SN/A                process->getSyscallArg(tc, index))) {
11451999SN/A        return -EFAULT;
11461999SN/A    }
11471999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
114811851Sbrandon.potter@amd.com
11492680Sktlim@umich.edu    // Adjust path for current working directory
11501999SN/A    path = process->fullPath(path);
11511999SN/A
11521999SN/A    struct statfs hostBuf;
11536701Sgblack@eecs.umich.edu    int result = statfs(path.c_str(), &hostBuf);
11548852Sandreas.hansson@arm.com
11556701Sgblack@eecs.umich.edu    if (result < 0)
11566701Sgblack@eecs.umich.edu        return -errno;
11576701Sgblack@eecs.umich.edu
11586701Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
11591999SN/A
11603669Sbinkertn@umich.edu    return 0;
11613669Sbinkertn@umich.edu}
11623669Sbinkertn@umich.edu
11632764Sstever@eecs.umich.edu
11642064SN/A/// Target fstatfs() handler.
11652064SN/Atemplate <class OS>
11662064SN/ASyscallReturn
11671999SN/AfstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
11681999SN/A            ThreadContext *tc)
11692064SN/A{
11701999SN/A    int index = 0;
11711999SN/A    int tgt_fd = process->getSyscallArg(tc, index);
11721999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
11731999SN/A
11748706Sandreas.hansson@arm.com    int sim_fd = process->getSimFD(tgt_fd);
11751999SN/A    if (sim_fd < 0)
11761999SN/A        return -EBADF;
11771999SN/A
11781999SN/A    struct statfs hostBuf;
1179378SN/A    int result = fstatfs(sim_fd, &hostBuf);
1180360SN/A
11811450SN/A    if (result < 0)
118211856Sbrandon.potter@amd.com        return -errno;
1183360SN/A
11846701Sgblack@eecs.umich.edu    OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf);
118511856Sbrandon.potter@amd.com
118611856Sbrandon.potter@amd.com    return 0;
1187360SN/A}
118811380Salexandru.dutu@amd.com
1189360SN/A
119011856Sbrandon.potter@amd.com/// Target writev() handler.
119111856Sbrandon.potter@amd.comtemplate <class OS>
11921458SN/ASyscallReturn
119311856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
1194360SN/A           ThreadContext *tc)
1195360SN/A{
119610931Sbrandon.potter@amd.com    int index = 0;
1197360SN/A    int tgt_fd = process->getSyscallArg(tc, index);
1198360SN/A
11991458SN/A    int sim_fd = process->getSimFD(tgt_fd);
1200360SN/A    if (sim_fd < 0)
120110931Sbrandon.potter@amd.com        return -EBADF;
12022021SN/A
12031458SN/A    SETranslatingPortProxy &p = tc->getMemProxy();
1204360SN/A    uint64_t tiov_base = process->getSyscallArg(tc, index);
1205360SN/A    size_t count = process->getSyscallArg(tc, index);
1206360SN/A    struct iovec hiov[count];
12071706SN/A    for (size_t i = 0; i < count; ++i) {
12081706SN/A        typename OS::tgt_iovec tiov;
12091706SN/A
121011851Sbrandon.potter@amd.com        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12112680Sktlim@umich.edu                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12121706SN/A        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
121311799Sbrandon.potter@amd.com        hiov[i].iov_base = new char [hiov[i].iov_len];
121411799Sbrandon.potter@amd.com        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
121511799Sbrandon.potter@amd.com                   hiov[i].iov_len);
12161706SN/A    }
12171706SN/A
12186701Sgblack@eecs.umich.edu    int result = writev(sim_fd, hiov, count);
12198852Sandreas.hansson@arm.com
12206701Sgblack@eecs.umich.edu    for (size_t i = 0; i < count; ++i)
12216701Sgblack@eecs.umich.edu        delete [] (char *)hiov[i].iov_base;
12226701Sgblack@eecs.umich.edu
12236701Sgblack@eecs.umich.edu    if (result < 0)
12241706SN/A        return -errno;
12253669Sbinkertn@umich.edu
12263669Sbinkertn@umich.edu    return result;
12273669Sbinkertn@umich.edu}
12281706SN/A
12291706SN/A/// Real mmap handler.
12301706SN/Atemplate <class OS>
12311706SN/ASyscallReturn
12322218SN/AmmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc,
12331706SN/A         bool is_mmap2)
123411759Sbrandon.potter@amd.com{
123511799Sbrandon.potter@amd.com    int index = 0;
12361706SN/A    Addr start = p->getSyscallArg(tc, index);
12371706SN/A    uint64_t length = p->getSyscallArg(tc, index);
12381706SN/A    int prot = p->getSyscallArg(tc, index);
123911886Sbrandon.potter@amd.com    int tgt_flags = p->getSyscallArg(tc, index);
124011886Sbrandon.potter@amd.com    int tgt_fd = p->getSyscallArg(tc, index);
124111886Sbrandon.potter@amd.com    int offset = p->getSyscallArg(tc, index);
124211886Sbrandon.potter@amd.com
124311886Sbrandon.potter@amd.com    if (is_mmap2)
124411886Sbrandon.potter@amd.com        offset *= TheISA::PageBytes;
124511886Sbrandon.potter@amd.com
124611886Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
124711886Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
124811886Sbrandon.potter@amd.com        (tgt_flags & OS::TGT_MAP_PRIVATE &&
124911886Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
125011886Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
125111886Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
125211886Sbrandon.potter@amd.com        !length) {
125311886Sbrandon.potter@amd.com        return -EINVAL;
125411886Sbrandon.potter@amd.com    }
125511886Sbrandon.potter@amd.com
125611886Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
125711886Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
125811886Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
125911886Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
126011886Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
126111886Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
126211886Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
126311886Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
126411886Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
126511886Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
126611886Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
126711886Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
126811886Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
126911886Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
127011886Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
127111886Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
127211886Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
127311886Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
127411886Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
127511886Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
127611886Sbrandon.potter@amd.com        // than we currently maintain.
127711886Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
127811886Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
127911886Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
128011886Sbrandon.potter@amd.com    }
128111886Sbrandon.potter@amd.com
128211886Sbrandon.potter@amd.com    length = roundUp(length, TheISA::PageBytes);
128311886Sbrandon.potter@amd.com
128411886Sbrandon.potter@amd.com    int sim_fd = -1;
128511886Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
128611886Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
128711886Sbrandon.potter@amd.com        // Check for EmulatedDriver mmap
128811886Sbrandon.potter@amd.com        FDEntry *fde = p->getFDEntry(tgt_fd);
128911886Sbrandon.potter@amd.com        if (fde == NULL)
129011886Sbrandon.potter@amd.com            return -EBADF;
129111886Sbrandon.potter@amd.com
129211886Sbrandon.potter@amd.com        if (fde->driver != NULL) {
129311886Sbrandon.potter@amd.com            return fde->driver->mmap(p, tc, start, length, prot,
129411886Sbrandon.potter@amd.com                                     tgt_flags, tgt_fd, offset);
129511886Sbrandon.potter@amd.com        }
129611886Sbrandon.potter@amd.com        sim_fd = fde->fd;
129711886Sbrandon.potter@amd.com
129811886Sbrandon.potter@amd.com        if (sim_fd < 0)
129911886Sbrandon.potter@amd.com            return -EBADF;
130011886Sbrandon.potter@amd.com
130111886Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
130211886Sbrandon.potter@amd.com                                    sim_fd, offset);
130311886Sbrandon.potter@amd.com
130411886Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
130511886Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
130611886Sbrandon.potter@amd.com            return -errno;
130711886Sbrandon.potter@amd.com        }
130811886Sbrandon.potter@amd.com    }
130911886Sbrandon.potter@amd.com
131011911SBrandon.Potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
131111911SBrandon.Potter@amd.com    // start address unless MAP_FIXED is specified.
131211911SBrandon.Potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
131311911SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end;
131411911SBrandon.Potter@amd.com        p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length;
131511911SBrandon.Potter@amd.com    }
131611911SBrandon.Potter@amd.com
131711886Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
131811886Sbrandon.potter@amd.com                    start, start + length - 1);
131911886Sbrandon.potter@amd.com
132011886Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
132111886Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
132211886Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
132311886Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
132411886Sbrandon.potter@amd.com    if (clobber) {
132511886Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
132611886Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
132711886Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
132811886Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
132911886Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
133011886Sbrandon.potter@amd.com        }
133111886Sbrandon.potter@amd.com    }
133211886Sbrandon.potter@amd.com
133311886Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
133411886Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
133511886Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
133611886Sbrandon.potter@amd.com    p->allocateMem(start, length, clobber);
133711886Sbrandon.potter@amd.com
133811886Sbrandon.potter@amd.com    // Transfer content into target address space.
133911886Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
134011886Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
134111886Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
134211886Sbrandon.potter@amd.com        // with something like:
134311886Sbrandon.potter@amd.com        //     tp.memsetBlob(start, 0, length);
134411886Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
134511886Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
134611886Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
134711886Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
134811886Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
134911886Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
135011886Sbrandon.potter@amd.com    } else {
135111886Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
135211886Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
135311886Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
135411886Sbrandon.potter@amd.com        // the host into the target address space.
135511886Sbrandon.potter@amd.com        struct stat file_stat;
135611886Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
135711886Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
135811886Sbrandon.potter@amd.com
135911886Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
136011886Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
136111886Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
136211886Sbrandon.potter@amd.com        // on the specified offset into the file.
136311886Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
136411886Sbrandon.potter@amd.com                                 length);
136511886Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
136611886Sbrandon.potter@amd.com
136711886Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
136811886Sbrandon.potter@amd.com        munmap(pmap, length);
136911886Sbrandon.potter@amd.com
137011886Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
13711706SN/A        // The loader will call mmap to map the images into its address
13721706SN/A        // space and we intercept that here. We can verify that we are
13731706SN/A        // executing inside the loader by checking the program counter value.
13741706SN/A        // XXX: with multiprogrammed workloads or multi-node configurations,
137511856Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
13761706SN/A        ObjectFile *interpreter = p->getInterpreter();
13776701Sgblack@eecs.umich.edu        if (interpreter) {
137811856Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
137911856Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
13801706SN/A
138111856Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
138211856Sbrandon.potter@amd.com
13831706SN/A            if (pc >= text_start && pc < text_end) {
138411856Sbrandon.potter@amd.com                FDEntry *fde = p->getFDEntry(tgt_fd);
13851706SN/A
13861706SN/A                ObjectFile *lib = createObjectFile(fde->filename);
138710931Sbrandon.potter@amd.com
13881706SN/A                if (lib) {
13891706SN/A                    lib->loadAllSymbols(debugSymbolTable,
13902218SN/A                                        lib->textBase(), start);
13911706SN/A                }
139211759Sbrandon.potter@amd.com            }
13931706SN/A        }
13941706SN/A
13951706SN/A        // Note that we do not zero out the remainder of the mapping. This
13961706SN/A        // is done by a real system, but it probably will not affect
13971706SN/A        // execution (hopefully).
13981999SN/A    }
13991999SN/A
14001999SN/A    return start;
140111856Sbrandon.potter@amd.com}
14021999SN/A
14036701Sgblack@eecs.umich.edutemplate <class OS>
140411856Sbrandon.potter@amd.comSyscallReturn
140510931Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
140611856Sbrandon.potter@amd.com{
140711856Sbrandon.potter@amd.com    int index = 0;
14081999SN/A    int tgt_fd = p->getSyscallArg(tc, index);
140911856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
14101999SN/A    int nbytes = p->getSyscallArg(tc, index);
141111856Sbrandon.potter@amd.com    int offset = p->getSyscallArg(tc, index);
141211856Sbrandon.potter@amd.com
141311856Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
14141999SN/A    if (sim_fd < 0)
14156227Snate@binkert.org        return -EBADF;
14161999SN/A
14172461SN/A    BufferArg bufArg(bufPtr, nbytes);
141811856Sbrandon.potter@amd.com    bufArg.copyIn(tc->getMemProxy());
141911856Sbrandon.potter@amd.com
14208737Skoansin.tan@gmail.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
14211999SN/A
142211856Sbrandon.potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
142311856Sbrandon.potter@amd.com}
14241999SN/A
14251999SN/A/// Target mmap() handler.
142610931Sbrandon.potter@amd.comtemplate <class OS>
14271999SN/ASyscallReturn
14286227Snate@binkert.orgmmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
14291999SN/A{
14301999SN/A    return mmapImpl<OS>(desc, num, p, tc, false);
14311999SN/A}
14322218SN/A
14331999SN/A/// Target mmap2() handler.
143410629Sjthestness@gmail.comtemplate <class OS>
14351999SN/ASyscallReturn
14361999SN/Ammap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
143711385Sbrandon.potter@amd.com{
1438360SN/A    return mmapImpl<OS>(desc, num, p, tc, true);
14391450SN/A}
144011851Sbrandon.potter@amd.com
144111385Sbrandon.potter@amd.com/// Target getrlimit() handler.
1442360SN/Atemplate <class OS>
14436701Sgblack@eecs.umich.eduSyscallReturn
14446701Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
14456701Sgblack@eecs.umich.edu        ThreadContext *tc)
144611383Sbrandon.potter@amd.com{
144711383Sbrandon.potter@amd.com    int index = 0;
14488324Ssteve.reinhardt@amd.com    unsigned resource = process->getSyscallArg(tc, index);
144910486Stjablin@gmail.com    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1450360SN/A
145111385Sbrandon.potter@amd.com    switch (resource) {
145211385Sbrandon.potter@amd.com        case OS::TGT_RLIMIT_STACK:
14539008Sgblack@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
145411383Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
145511383Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
145611383Sbrandon.potter@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
145711383Sbrandon.potter@amd.com            break;
145811383Sbrandon.potter@amd.com
145911383Sbrandon.potter@amd.com        case OS::TGT_RLIMIT_DATA:
146011383Sbrandon.potter@amd.com            // max data segment size in bytes: make up a number
146111383Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
146211383Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14638324Ssteve.reinhardt@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
146411383Sbrandon.potter@amd.com            break;
146511383Sbrandon.potter@amd.com
146611383Sbrandon.potter@amd.com        default:
146711383Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
146811383Sbrandon.potter@amd.com            return -EINVAL;
146911383Sbrandon.potter@amd.com            break;
147011383Sbrandon.potter@amd.com    }
147111383Sbrandon.potter@amd.com
147211383Sbrandon.potter@amd.com    rlp.copyOut(tc->getMemProxy());
147311383Sbrandon.potter@amd.com    return 0;
147411383Sbrandon.potter@amd.com}
147511383Sbrandon.potter@amd.com
147611383Sbrandon.potter@amd.com/// Target clock_gettime() function.
147711383Sbrandon.potter@amd.comtemplate <class OS>
147811383Sbrandon.potter@amd.comSyscallReturn
147911383Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
148011383Sbrandon.potter@amd.com{
148111383Sbrandon.potter@amd.com    int index = 1;
148211383Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
148311383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
148411383Sbrandon.potter@amd.com
148511383Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
148611383Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
148711383Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
14888324Ssteve.reinhardt@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
14895877Shsul@eecs.umich.edu
149010486Stjablin@gmail.com    tp.copyOut(tc->getMemProxy());
149110486Stjablin@gmail.com
149211383Sbrandon.potter@amd.com    return 0;
149311383Sbrandon.potter@amd.com}
149411383Sbrandon.potter@amd.com
149511856Sbrandon.potter@amd.com/// Target clock_getres() function.
149611624Smichael.lebeane@amd.comtemplate <class OS>
149711856Sbrandon.potter@amd.comSyscallReturn
149811856Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc)
149911856Sbrandon.potter@amd.com{
150011856Sbrandon.potter@amd.com    int index = 1;
150111624Smichael.lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
150211624Smichael.lebeane@amd.com
150311624Smichael.lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
150411856Sbrandon.potter@amd.com    tp->tv_sec = 0;
150511856Sbrandon.potter@amd.com    tp->tv_nsec = 1;
150611383Sbrandon.potter@amd.com
150711856Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
1508360SN/A
150911913SBrandon.Potter@amd.com    return 0;
151011383Sbrandon.potter@amd.com}
15118600Ssteve.reinhardt@amd.com
151211383Sbrandon.potter@amd.com/// Target gettimeofday() handler.
151311383Sbrandon.potter@amd.comtemplate <class OS>
151411383Sbrandon.potter@amd.comSyscallReturn
15158600Ssteve.reinhardt@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
15162544SN/A        ThreadContext *tc)
15172544SN/A{
151811383Sbrandon.potter@amd.com    int index = 0;
151911383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
152011383Sbrandon.potter@amd.com
152111905SBrandon.Potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
152211905SBrandon.Potter@amd.com    tp->tv_sec += seconds_since_epoch;
152311905SBrandon.Potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
152411905SBrandon.Potter@amd.com    tp->tv_usec = TheISA::htog(tp->tv_usec);
152511905SBrandon.Potter@amd.com
152611905SBrandon.Potter@amd.com    tp.copyOut(tc->getMemProxy());
152711905SBrandon.Potter@amd.com
152811383Sbrandon.potter@amd.com    return 0;
152911383Sbrandon.potter@amd.com}
153011383Sbrandon.potter@amd.com
153111383Sbrandon.potter@amd.com
153211383Sbrandon.potter@amd.com/// Target utimes() handler.
153311383Sbrandon.potter@amd.comtemplate <class OS>
153411383Sbrandon.potter@amd.comSyscallReturn
153511383Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
153611383Sbrandon.potter@amd.com           ThreadContext *tc)
153711383Sbrandon.potter@amd.com{
153811383Sbrandon.potter@amd.com    std::string path;
153911383Sbrandon.potter@amd.com
154011383Sbrandon.potter@amd.com    int index = 0;
154111383Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
154211383Sbrandon.potter@amd.com                process->getSyscallArg(tc, index))) {
15438600Ssteve.reinhardt@amd.com        return -EFAULT;
15446672Sgblack@eecs.umich.edu    }
15458600Ssteve.reinhardt@amd.com
154611383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval [2]>
154711383Sbrandon.potter@amd.com        tp(process->getSyscallArg(tc, index));
154811383Sbrandon.potter@amd.com    tp.copyIn(tc->getMemProxy());
15498601Ssteve.reinhardt@amd.com
15502544SN/A    struct timeval hostTimeval[2];
155111383Sbrandon.potter@amd.com    for (int i = 0; i < 2; ++i)
155211383Sbrandon.potter@amd.com    {
155311383Sbrandon.potter@amd.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
155411383Sbrandon.potter@amd.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
155511383Sbrandon.potter@amd.com    }
155611383Sbrandon.potter@amd.com
155711383Sbrandon.potter@amd.com    // Adjust path for current working directory
155811383Sbrandon.potter@amd.com    path = process->fullPath(path);
155911383Sbrandon.potter@amd.com
156011383Sbrandon.potter@amd.com    int result = utimes(path.c_str(), hostTimeval);
156111383Sbrandon.potter@amd.com
156211383Sbrandon.potter@amd.com    if (result < 0)
156311383Sbrandon.potter@amd.com        return -errno;
156411383Sbrandon.potter@amd.com
156511383Sbrandon.potter@amd.com    return 0;
156611383Sbrandon.potter@amd.com}
156711383Sbrandon.potter@amd.com/// Target getrusage() function.
156811383Sbrandon.potter@amd.comtemplate <class OS>
156911383Sbrandon.potter@amd.comSyscallReturn
157011383Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
157111383Sbrandon.potter@amd.com              ThreadContext *tc)
157211383Sbrandon.potter@amd.com{
157311383Sbrandon.potter@amd.com    int index = 0;
157411383Sbrandon.potter@amd.com    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
157511383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
157611383Sbrandon.potter@amd.com
157711383Sbrandon.potter@amd.com    rup->ru_utime.tv_sec = 0;
157811383Sbrandon.potter@amd.com    rup->ru_utime.tv_usec = 0;
157911383Sbrandon.potter@amd.com    rup->ru_stime.tv_sec = 0;
158011383Sbrandon.potter@amd.com    rup->ru_stime.tv_usec = 0;
158111383Sbrandon.potter@amd.com    rup->ru_maxrss = 0;
158211383Sbrandon.potter@amd.com    rup->ru_ixrss = 0;
158311392Sbrandon.potter@amd.com    rup->ru_idrss = 0;
158411392Sbrandon.potter@amd.com    rup->ru_isrss = 0;
158511392Sbrandon.potter@amd.com    rup->ru_minflt = 0;
158611392Sbrandon.potter@amd.com    rup->ru_majflt = 0;
158711392Sbrandon.potter@amd.com    rup->ru_nswap = 0;
158811392Sbrandon.potter@amd.com    rup->ru_inblock = 0;
158911392Sbrandon.potter@amd.com    rup->ru_oublock = 0;
159011392Sbrandon.potter@amd.com    rup->ru_msgsnd = 0;
159111392Sbrandon.potter@amd.com    rup->ru_msgrcv = 0;
159211392Sbrandon.potter@amd.com    rup->ru_nsignals = 0;
159311392Sbrandon.potter@amd.com    rup->ru_nvcsw = 0;
159411392Sbrandon.potter@amd.com    rup->ru_nivcsw = 0;
159511392Sbrandon.potter@amd.com
159611392Sbrandon.potter@amd.com    switch (who) {
159711856Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_SELF:
159811856Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
159911856Sbrandon.potter@amd.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
160011392Sbrandon.potter@amd.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
160111392Sbrandon.potter@amd.com        break;
160211392Sbrandon.potter@amd.com
160311392Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_CHILDREN:
160411392Sbrandon.potter@amd.com        // do nothing.  We have no child processes, so they take no time.
160511392Sbrandon.potter@amd.com        break;
160611392Sbrandon.potter@amd.com
160711392Sbrandon.potter@amd.com      default:
160811383Sbrandon.potter@amd.com        // don't really handle THREAD or CHILDREN, but just warn and
160911383Sbrandon.potter@amd.com        // plow ahead
161011383Sbrandon.potter@amd.com        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
161111383Sbrandon.potter@amd.com             who);
161211383Sbrandon.potter@amd.com    }
16131458SN/A
1614360SN/A    rup.copyOut(tc->getMemProxy());
1615360SN/A
161611593Santhony.gutierrez@amd.com    return 0;
161711593Santhony.gutierrez@amd.com}
161811851Sbrandon.potter@amd.com
161911593Santhony.gutierrez@amd.com/// Target times() function.
162011593Santhony.gutierrez@amd.comtemplate <class OS>
162111593Santhony.gutierrez@amd.comSyscallReturn
162211593Santhony.gutierrez@amd.comtimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
162311593Santhony.gutierrez@amd.com           ThreadContext *tc)
162411593Santhony.gutierrez@amd.com{
162511593Santhony.gutierrez@amd.com    int index = 0;
162611856Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
162711856Sbrandon.potter@amd.com
162811593Santhony.gutierrez@amd.com    // Fill in the time structure (in clocks)
162911856Sbrandon.potter@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
163011593Santhony.gutierrez@amd.com    bufp->tms_utime = clocks;
163111593Santhony.gutierrez@amd.com    bufp->tms_stime = 0;
163211593Santhony.gutierrez@amd.com    bufp->tms_cutime = 0;
163311593Santhony.gutierrez@amd.com    bufp->tms_cstime = 0;
163411594Santhony.gutierrez@amd.com
163511593Santhony.gutierrez@amd.com    // Convert to host endianness
163611593Santhony.gutierrez@amd.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
163711593Santhony.gutierrez@amd.com
163811593Santhony.gutierrez@amd.com    // Write back
163911385Sbrandon.potter@amd.com    bufp.copyOut(tc->getMemProxy());
164011385Sbrandon.potter@amd.com
164111385Sbrandon.potter@amd.com    // Return clock ticks since system boot
164211851Sbrandon.potter@amd.com    return clocks;
164311385Sbrandon.potter@amd.com}
164411385Sbrandon.potter@amd.com
164511385Sbrandon.potter@amd.com/// Target time() function.
164611385Sbrandon.potter@amd.comtemplate <class OS>
164711385Sbrandon.potter@amd.comSyscallReturn
164811385Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process,
164911385Sbrandon.potter@amd.com           ThreadContext *tc)
165011851Sbrandon.potter@amd.com{
165111385Sbrandon.potter@amd.com    typename OS::time_t sec, usec;
165211385Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
165311385Sbrandon.potter@amd.com    sec += seconds_since_epoch;
165411385Sbrandon.potter@amd.com
1655378SN/A    int index = 0;
1656360SN/A    Addr taddr = (Addr)process->getSyscallArg(tc, index);
16571450SN/A    if (taddr != 0) {
165811851Sbrandon.potter@amd.com        typename OS::time_t t = sec;
165911851Sbrandon.potter@amd.com        t = TheISA::htog(t);
1660360SN/A        SETranslatingPortProxy &p = tc->getMemProxy();
16616701Sgblack@eecs.umich.edu        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
16626701Sgblack@eecs.umich.edu    }
16636701Sgblack@eecs.umich.edu    return sec;
1664360SN/A}
1665360SN/A
166611906SBrandon.Potter@amd.com
166711906SBrandon.Potter@amd.com#endif // __SIM_SYSCALL_EMUL_HH__
166811906SBrandon.Potter@amd.com