syscall_emul.hh revision 11851
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#define NO_STATFS (defined(__APPLE__) || defined(__OpenBSD__) || \
5312018Sandreas.sandberg@arm.com  defined(__FreeBSD__) || defined(__NetBSD__))
5412018Sandreas.sandberg@arm.com
552064SN/A#define NO_FALLOCATE (defined(__APPLE__) || defined(__OpenBSD__) || \
5612018Sandreas.sandberg@arm.com  defined(__FreeBSD__) || defined(__NetBSD__))
5712018Sandreas.sandberg@arm.com
5812018Sandreas.sandberg@arm.com///
5912018Sandreas.sandberg@arm.com/// @file syscall_emul.hh
6012018Sandreas.sandberg@arm.com///
6112018Sandreas.sandberg@arm.com/// This file defines objects used to emulate syscalls from the target
6211799Sbrandon.potter@amd.com/// application on the host machine.
6312018Sandreas.sandberg@arm.com
6412018Sandreas.sandberg@arm.com#ifdef __CYGWIN32__
6512018Sandreas.sandberg@arm.com#include <sys/fcntl.h>
6612018Sandreas.sandberg@arm.com
6712018Sandreas.sandberg@arm.com#endif
6812018Sandreas.sandberg@arm.com#include <fcntl.h>
6911799Sbrandon.potter@amd.com#include <sys/mman.h>
70360SN/A#include <sys/stat.h>
71360SN/A#if (NO_STATFS == 0)
72360SN/A#include <sys/statfs.h>
73360SN/A#else
74360SN/A#include <sys/mount.h>
75360SN/A#endif
761809SN/A#include <sys/time.h>
7711800Sbrandon.potter@amd.com#include <sys/uio.h>
7811392Sbrandon.potter@amd.com#include <unistd.h>
791809SN/A
8011392Sbrandon.potter@amd.com#include <cerrno>
8111383Sbrandon.potter@amd.com#include <string>
823113Sgblack@eecs.umich.edu
8311799Sbrandon.potter@amd.com#include "base/intmath.hh"
8411759Sbrandon.potter@amd.com#include "base/loader/object_file.hh"
8511812Sbaz21@cam.ac.uk#include "base/misc.hh"
8611812Sbaz21@cam.ac.uk#include "base/trace.hh"
8711799Sbrandon.potter@amd.com#include "base/types.hh"
888229Snate@binkert.org#include "config/the_isa.hh"
898229Snate@binkert.org#include "cpu/base.hh"
9011594Santhony.gutierrez@amd.com#include "cpu/thread_context.hh"
917075Snate@binkert.org#include "mem/page_table.hh"
928229Snate@binkert.org#include "sim/emul_driver.hh"
9311856Sbrandon.potter@amd.com#include "sim/process.hh"
947075Snate@binkert.org#include "sim/syscall_debug_macros.hh"
95360SN/A#include "sim/syscall_emul_buf.hh"
9611886Sbrandon.potter@amd.com#include "sim/syscall_return.hh"
9711800Sbrandon.potter@amd.com
9811392Sbrandon.potter@amd.comclass SyscallDesc;
9912334Sgabeblack@google.com
1001354SN/A//////////////////////////////////////////////////////////////////////
1016216Snate@binkert.org//
1026658Snate@binkert.org// The following emulation functions are generic enough that they
1032474SN/A// don't need to be recompiled for different emulated OS's.  They are
1042680Sktlim@umich.edu// defined in sim/syscall_emul.cc.
1058229Snate@binkert.org//
10611886Sbrandon.potter@amd.com//////////////////////////////////////////////////////////////////////
10710496Ssteve.reinhardt@amd.com
10811911SBrandon.Potter@amd.com
1098229Snate@binkert.org/// Handler for unimplemented syscalls that we haven't thought about.
11011794Sbrandon.potter@amd.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num,
11111886Sbrandon.potter@amd.com                                Process *p, ThreadContext *tc);
11210497Ssteve.reinhardt@amd.com
11311794Sbrandon.potter@amd.com/// Handler for unimplemented syscalls that we never intend to
114360SN/A/// implement (signal handling, etc.) and should not affect the correct
115360SN/A/// behavior of the program.  Print a warning only if the appropriate
116360SN/A/// trace flag is enabled.  Return success to the target program.
117360SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num,
118360SN/A                         Process *p, ThreadContext *tc);
119360SN/A
120360SN/A// Target fallocateFunc() handler.
121360SN/ASyscallReturn fallocateFunc(SyscallDesc *desc, int num,
122360SN/A                            Process *p, ThreadContext *tc);
123360SN/A
124378SN/A/// Target exit() handler: terminate current context.
1251706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num,
12611851Sbrandon.potter@amd.com                       Process *p, ThreadContext *tc);
127378SN/A
128378SN/A/// Target exit_group() handler: terminate simulation. (exit all threads)
129378SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num,
130378SN/A                       Process *p, ThreadContext *tc);
131378SN/A
1321706SN/A/// Target getpagesize() handler.
13311851Sbrandon.potter@amd.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num,
134360SN/A                              Process *p, ThreadContext *tc);
13511760Sbrandon.potter@amd.com
13611760Sbrandon.potter@amd.com/// Target brk() handler: set brk address.
13711851Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num,
13811760Sbrandon.potter@amd.com                      Process *p, ThreadContext *tc);
1396109Ssanchezd@stanford.edu
1401706SN/A/// Target close() handler.
14111851Sbrandon.potter@amd.comSyscallReturn closeFunc(SyscallDesc *desc, int num,
142378SN/A                        Process *p, ThreadContext *tc);
1436109Ssanchezd@stanford.edu
1446109Ssanchezd@stanford.edu/// Target read() handler.
14511851Sbrandon.potter@amd.comSyscallReturn readFunc(SyscallDesc *desc, int num,
1466109Ssanchezd@stanford.edu                       Process *p, ThreadContext *tc);
14711886Sbrandon.potter@amd.com
14811886Sbrandon.potter@amd.com/// Target write() handler.
14911886Sbrandon.potter@amd.comSyscallReturn writeFunc(SyscallDesc *desc, int num,
15011886Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
151378SN/A
1521706SN/A/// Target lseek() handler.
15311851Sbrandon.potter@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num,
154378SN/A                        Process *p, ThreadContext *tc);
1555748SSteve.Reinhardt@amd.com
1565748SSteve.Reinhardt@amd.com/// Target _llseek() handler.
15711851Sbrandon.potter@amd.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num,
158378SN/A                          Process *p, ThreadContext *tc);
159378SN/A
1601706SN/A/// Target munmap() handler.
16111851Sbrandon.potter@amd.comSyscallReturn munmapFunc(SyscallDesc *desc, int num,
162378SN/A                         Process *p, ThreadContext *tc);
16311886Sbrandon.potter@amd.com
1641706SN/A/// Target gethostname() handler.
16511851Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num,
166378SN/A                              Process *p, ThreadContext *tc);
167378SN/A
1681706SN/A/// Target getcwd() handler.
16911851Sbrandon.potter@amd.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num,
170378SN/A                         Process *p, ThreadContext *tc);
171378SN/A
1721706SN/A/// Target readlink() handler.
17311851Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
174378SN/A                           Process *p, ThreadContext *tc,
1754118Sgblack@eecs.umich.edu                           int index = 0);
1764118Sgblack@eecs.umich.eduSyscallReturn readlinkFunc(SyscallDesc *desc, int num,
17711851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc);
1784118Sgblack@eecs.umich.edu
179378SN/A/// Target unlink() handler.
1801706SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num,
18111851Sbrandon.potter@amd.com                           Process *p, ThreadContext *tc,
182378SN/A                           int index);
183378SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num,
1841706SN/A                         Process *p, ThreadContext *tc);
18511851Sbrandon.potter@amd.com
186360SN/A/// Target mkdir() handler.
1875513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num,
1885513SMichael.Adler@intel.com                        Process *p, ThreadContext *tc);
18911851Sbrandon.potter@amd.com
1905513SMichael.Adler@intel.com/// Target rename() handler.
19110203SAli.Saidi@ARM.comSyscallReturn renameFunc(SyscallDesc *desc, int num,
19210203SAli.Saidi@ARM.com                         Process *p, ThreadContext *tc);
19311851Sbrandon.potter@amd.com
19410203SAli.Saidi@ARM.com
1955513SMichael.Adler@intel.com/// Target truncate() handler.
19611851Sbrandon.potter@amd.comSyscallReturn truncateFunc(SyscallDesc *desc, int num,
1975513SMichael.Adler@intel.com                           Process *p, ThreadContext *tc);
198511SN/A
19910633Smichaelupton@gmail.com
20011851Sbrandon.potter@amd.com/// Target ftruncate() handler.
20110633Smichaelupton@gmail.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num,
2021706SN/A                            Process *p, ThreadContext *tc);
20311851Sbrandon.potter@amd.com
204511SN/A
2055513SMichael.Adler@intel.com/// Target truncate64() handler.
2065513SMichael.Adler@intel.comSyscallReturn truncate64Func(SyscallDesc *desc, int num,
20711851Sbrandon.potter@amd.com                             Process *p, ThreadContext *tc);
2085513SMichael.Adler@intel.com
209511SN/A/// Target ftruncate64() handler.
2101706SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num,
21111851Sbrandon.potter@amd.com                              Process *p, ThreadContext *tc);
2121706SN/A
2131706SN/A
2141706SN/A/// Target umask() handler.
2151706SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num,
21611851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2171706SN/A
2181706SN/A
2191706SN/A/// Target chown() handler.
2201706SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num,
22111851Sbrandon.potter@amd.com                        Process *p, ThreadContext *tc);
2221706SN/A
223511SN/A
2246703Svince@csl.cornell.edu/// Target fchown() handler.
2256703Svince@csl.cornell.eduSyscallReturn fchownFunc(SyscallDesc *desc, int num,
22611851Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
2276703Svince@csl.cornell.edu
2286685Stjones1@inf.ed.ac.uk/// Target dup() handler.
2296685Stjones1@inf.ed.ac.ukSyscallReturn dupFunc(SyscallDesc *desc, int num,
23011851Sbrandon.potter@amd.com                      Process *process, ThreadContext *tc);
2316685Stjones1@inf.ed.ac.uk
2326685Stjones1@inf.ed.ac.uk/// Target fnctl() handler.
2335513SMichael.Adler@intel.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num,
2345513SMichael.Adler@intel.com                        Process *process, ThreadContext *tc);
23511851Sbrandon.potter@amd.com
2365513SMichael.Adler@intel.com/// Target fcntl64() handler.
23711885Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num,
23811885Sbrandon.potter@amd.com                          Process *process, ThreadContext *tc);
23911885Sbrandon.potter@amd.com
2405513SMichael.Adler@intel.com/// Target setuid() handler.
2411999SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num,
2421999SN/A                         Process *p, ThreadContext *tc);
24311851Sbrandon.potter@amd.com
2441999SN/A/// Target getpid() handler.
24511885Sbrandon.potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num,
24611885Sbrandon.potter@amd.com                         Process *p, ThreadContext *tc);
24711885Sbrandon.potter@amd.com
2481999SN/A/// Target getuid() handler.
2491999SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num,
2501999SN/A                         Process *p, ThreadContext *tc);
25111851Sbrandon.potter@amd.com
2521999SN/A/// Target getgid() handler.
2533079Sstever@eecs.umich.eduSyscallReturn getgidFunc(SyscallDesc *desc, int num,
2543079Sstever@eecs.umich.edu                         Process *p, ThreadContext *tc);
25511851Sbrandon.potter@amd.com
2563079Sstever@eecs.umich.edu/// Target getppid() handler.
25711908SBrandon.Potter@amd.comSyscallReturn getppidFunc(SyscallDesc *desc, int num,
25811908SBrandon.Potter@amd.com                          Process *p, ThreadContext *tc);
25911908SBrandon.Potter@amd.com
26011908SBrandon.Potter@amd.com/// Target geteuid() handler.
26111875Sbrandon.potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num,
2622093SN/A                          Process *p, ThreadContext *tc);
26311851Sbrandon.potter@amd.com
2642093SN/A/// Target getegid() handler.
2652687Sksewell@umich.eduSyscallReturn getegidFunc(SyscallDesc *desc, int num,
2662687Sksewell@umich.edu                          Process *p, ThreadContext *tc);
26711851Sbrandon.potter@amd.com
2682687Sksewell@umich.edu/// Target clone() handler.
2692238SN/ASyscallReturn cloneFunc(SyscallDesc *desc, int num,
2702238SN/A                        Process *p, ThreadContext *tc);
27111851Sbrandon.potter@amd.com
2722238SN/A/// Target access() handler
27311908SBrandon.Potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
27411908SBrandon.Potter@amd.com                         Process *p, ThreadContext *tc);
27511908SBrandon.Potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num,
27611908SBrandon.Potter@amd.com                         Process *p, ThreadContext *tc,
27711908SBrandon.Potter@amd.com                         int index);
27811908SBrandon.Potter@amd.com
27911908SBrandon.Potter@amd.com/// Futex system call
28011908SBrandon.Potter@amd.com///  Implemented by Daniel Sanchez
2812238SN/A///  Used by printf's in multi-threaded apps
2822238SN/Atemplate <class OS>
28311851Sbrandon.potter@amd.comSyscallReturn
2842238SN/AfutexFunc(SyscallDesc *desc, int callnum, Process *process,
2852238SN/A          ThreadContext *tc)
2862238SN/A{
28711851Sbrandon.potter@amd.com    int index_uaddr = 0;
2882238SN/A    int index_op = 1;
2892238SN/A    int index_val = 2;
2902238SN/A    int index_timeout = 3;
29111851Sbrandon.potter@amd.com
2922238SN/A    uint64_t uaddr = process->getSyscallArg(tc, index_uaddr);
2932238SN/A    int op = process->getSyscallArg(tc, index_op);
2942238SN/A    int val = process->getSyscallArg(tc, index_val);
29511851Sbrandon.potter@amd.com    uint64_t timeout = process->getSyscallArg(tc, index_timeout);
2962238SN/A
2972238SN/A    std::map<uint64_t, std::list<ThreadContext *> * >
2982238SN/A        &futex_map = tc->getSystemPtr()->futexMap;
29911851Sbrandon.potter@amd.com
3002238SN/A    DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n",
3012238SN/A            uaddr, op, val);
3022238SN/A
30311851Sbrandon.potter@amd.com    op &= ~OS::TGT_FUTEX_PRIVATE_FLAG;
3042238SN/A
3059455Smitch.hayenga+gem5@gmail.com    if (op == OS::TGT_FUTEX_WAIT) {
3069455Smitch.hayenga+gem5@gmail.com        if (timeout != 0) {
30711851Sbrandon.potter@amd.com            warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;"
30810203SAli.Saidi@ARM.com                 "we'll wait indefinitely");
30911851Sbrandon.potter@amd.com        }
31011851Sbrandon.potter@amd.com
3119455Smitch.hayenga+gem5@gmail.com        uint8_t *buf = new uint8_t[sizeof(int)];
3129112Smarc.orr@gmail.com        tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int));
31311906SBrandon.Potter@amd.com        int mem_val = *((int *)buf);
31411906SBrandon.Potter@amd.com        delete[] buf;
3159112Smarc.orr@gmail.com
3169112Smarc.orr@gmail.com        if (val != mem_val) {
31711851Sbrandon.potter@amd.com            DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, "
3189112Smarc.orr@gmail.com                                    "expected: %d\n", mem_val, val);
3199112Smarc.orr@gmail.com            return -OS::TGT_EWOULDBLOCK;
32011911SBrandon.Potter@amd.com        }
3219112Smarc.orr@gmail.com
32211911SBrandon.Potter@amd.com        // Queue the thread context
32311911SBrandon.Potter@amd.com        std::list<ThreadContext *> * tcWaitList;
32411911SBrandon.Potter@amd.com        if (futex_map.count(uaddr)) {
32511911SBrandon.Potter@amd.com            tcWaitList = futex_map.find(uaddr)->second;
3269112Smarc.orr@gmail.com        } else {
32711911SBrandon.Potter@amd.com            tcWaitList = new std::list<ThreadContext *>();
32811911SBrandon.Potter@amd.com            futex_map.insert(std::pair< uint64_t,
32911911SBrandon.Potter@amd.com                            std::list<ThreadContext *> * >(uaddr, tcWaitList));
33011911SBrandon.Potter@amd.com        }
3319238Slluc.alvarez@bsc.es        tcWaitList->push_back(tc);
3329112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling "
33311911SBrandon.Potter@amd.com                                "thread context\n");
3349112Smarc.orr@gmail.com        tc->suspend();
33511911SBrandon.Potter@amd.com        return 0;
33611911SBrandon.Potter@amd.com    } else if (op == OS::TGT_FUTEX_WAKE){
33711911SBrandon.Potter@amd.com        int wokenUp = 0;
33811911SBrandon.Potter@amd.com        std::list<ThreadContext *> * tcWaitList;
33911911SBrandon.Potter@amd.com        if (futex_map.count(uaddr)) {
3409112Smarc.orr@gmail.com            tcWaitList = futex_map.find(uaddr)->second;
34111911SBrandon.Potter@amd.com            while (tcWaitList->size() > 0 && wokenUp < val) {
34211911SBrandon.Potter@amd.com                tcWaitList->front()->activate();
34311911SBrandon.Potter@amd.com                tcWaitList->pop_front();
34411911SBrandon.Potter@amd.com                wokenUp++;
34511911SBrandon.Potter@amd.com            }
34611911SBrandon.Potter@amd.com            if (tcWaitList->empty()) {
3479112Smarc.orr@gmail.com                futex_map.erase(uaddr);
3489112Smarc.orr@gmail.com                delete tcWaitList;
34911911SBrandon.Potter@amd.com            }
35011911SBrandon.Potter@amd.com        }
3519112Smarc.orr@gmail.com        DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting "
35211911SBrandon.Potter@amd.com                                "thread contexts\n", wokenUp);
35311911SBrandon.Potter@amd.com        return wokenUp;
3549112Smarc.orr@gmail.com    } else {
3559112Smarc.orr@gmail.com        warn("sys_futex: op %d is not implemented, just returning...", op);
35611911SBrandon.Potter@amd.com        return 0;
35711911SBrandon.Potter@amd.com    }
3589112Smarc.orr@gmail.com
3599112Smarc.orr@gmail.com}
3602238SN/A
3612238SN/A
3622238SN/A/// Pseudo Funcs  - These functions use a different return convension,
3632238SN/A/// returning a second value in a register other than the normal return register
36411851Sbrandon.potter@amd.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num,
3652238SN/A                             Process *process, ThreadContext *tc);
3662238SN/A
3672238SN/A/// Target getpidPseudo() handler.
36811851Sbrandon.potter@amd.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num,
3692238SN/A                               Process *p, ThreadContext *tc);
3702238SN/A
3712238SN/A/// Target getuidPseudo() handler.
37211851Sbrandon.potter@amd.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num,
3732238SN/A                               Process *p, ThreadContext *tc);
3742238SN/A
3752238SN/A/// Target getgidPseudo() handler.
37611851Sbrandon.potter@amd.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num,
3772238SN/A                               Process *p, ThreadContext *tc);
3782238SN/A
3791354SN/A
3801354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds.
38110796Sbrandon.potter@amd.comconst int one_million = 1000000;
38210796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds.
3831354SN/Aconst int one_billion = 1000000000;
3841354SN/A
3851354SN/A/// Approximate seconds since the epoch (1/1/1970).  About a billion,
3861354SN/A/// by my reckoning.  We want to keep this a constant (not use the
3871354SN/A/// real-world time) to keep simulations repeatable.
3881354SN/Aconst unsigned seconds_since_epoch = 1000000000;
3891354SN/A
3901354SN/A/// Helper function to convert current elapsed time to seconds and
3911354SN/A/// microseconds.
3921354SN/Atemplate <class T1, class T2>
39310796Sbrandon.potter@amd.comvoid
3941354SN/AgetElapsedTimeMicro(T1 &sec, T2 &usec)
39510796Sbrandon.potter@amd.com{
3961354SN/A    uint64_t elapsed_usecs = curTick() / SimClock::Int::us;
3971354SN/A    sec = elapsed_usecs / one_million;
3981354SN/A    usec = elapsed_usecs % one_million;
3991354SN/A}
40010796Sbrandon.potter@amd.com
40110796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and
40210796Sbrandon.potter@amd.com/// nanoseconds.
40310796Sbrandon.potter@amd.comtemplate <class T1, class T2>
40410796Sbrandon.potter@amd.comvoid
40510796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec)
40610796Sbrandon.potter@amd.com{
40710796Sbrandon.potter@amd.com    uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns;
40810796Sbrandon.potter@amd.com    sec = elapsed_nsecs / one_billion;
40910796Sbrandon.potter@amd.com    nsec = elapsed_nsecs % one_billion;
41010796Sbrandon.potter@amd.com}
411360SN/A
412360SN/A//////////////////////////////////////////////////////////////////////
413360SN/A//
414360SN/A// The following emulation functions are generic, but need to be
415360SN/A// templated to account for differences in types, constants, etc.
416360SN/A//
417360SN/A//////////////////////////////////////////////////////////////////////
41811759Sbrandon.potter@amd.com
4193113Sgblack@eecs.umich.edu    typedef struct statfs hst_statfs;
4203113Sgblack@eecs.umich.edu#if NO_STAT64
4213113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4223113Sgblack@eecs.umich.edu    typedef struct stat hst_stat64;
4233113Sgblack@eecs.umich.edu#else
4243113Sgblack@eecs.umich.edu    typedef struct stat hst_stat;
4253113Sgblack@eecs.umich.edu    typedef struct stat64 hst_stat64;
4263113Sgblack@eecs.umich.edu#endif
4273113Sgblack@eecs.umich.edu
4283113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat
4293113Sgblack@eecs.umich.edu//// buffer.  Also copies the target buffer out to the simulated
4303113Sgblack@eecs.umich.edu//// memory space.  Used by stat(), fstat(), and lstat().
4313113Sgblack@eecs.umich.edu
43212032Sandreas.sandberg@arm.comtemplate <typename target_stat, typename host_stat>
4333113Sgblack@eecs.umich.edustatic void
4343113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false)
4354189Sgblack@eecs.umich.edu{
4364189Sgblack@eecs.umich.edu    using namespace TheISA;
4373113Sgblack@eecs.umich.edu
4383113Sgblack@eecs.umich.edu    if (fakeTTY)
4393113Sgblack@eecs.umich.edu        tgt->st_dev = 0xA;
4403113Sgblack@eecs.umich.edu    else
4418737Skoansin.tan@gmail.com        tgt->st_dev = host->st_dev;
4423113Sgblack@eecs.umich.edu    tgt->st_dev = TheISA::htog(tgt->st_dev);
4438737Skoansin.tan@gmail.com    tgt->st_ino = host->st_ino;
4443277Sgblack@eecs.umich.edu    tgt->st_ino = TheISA::htog(tgt->st_ino);
4455515SMichael.Adler@intel.com    tgt->st_mode = host->st_mode;
4465515SMichael.Adler@intel.com    if (fakeTTY) {
4475515SMichael.Adler@intel.com        // Claim to be a character device
4485515SMichael.Adler@intel.com        tgt->st_mode &= ~S_IFMT;    // Clear S_IFMT
4495515SMichael.Adler@intel.com        tgt->st_mode |= S_IFCHR;    // Set S_IFCHR
4508737Skoansin.tan@gmail.com    }
4513277Sgblack@eecs.umich.edu    tgt->st_mode = TheISA::htog(tgt->st_mode);
4528737Skoansin.tan@gmail.com    tgt->st_nlink = host->st_nlink;
4533277Sgblack@eecs.umich.edu    tgt->st_nlink = TheISA::htog(tgt->st_nlink);
4548737Skoansin.tan@gmail.com    tgt->st_uid = host->st_uid;
4553277Sgblack@eecs.umich.edu    tgt->st_uid = TheISA::htog(tgt->st_uid);
4568737Skoansin.tan@gmail.com    tgt->st_gid = host->st_gid;
4573113Sgblack@eecs.umich.edu    tgt->st_gid = TheISA::htog(tgt->st_gid);
4583113Sgblack@eecs.umich.edu    if (fakeTTY)
4593113Sgblack@eecs.umich.edu        tgt->st_rdev = 0x880d;
4603113Sgblack@eecs.umich.edu    else
4618737Skoansin.tan@gmail.com        tgt->st_rdev = host->st_rdev;
4623113Sgblack@eecs.umich.edu    tgt->st_rdev = TheISA::htog(tgt->st_rdev);
4638737Skoansin.tan@gmail.com    tgt->st_size = host->st_size;
4643114Sgblack@eecs.umich.edu    tgt->st_size = TheISA::htog(tgt->st_size);
4658737Skoansin.tan@gmail.com    tgt->st_atimeX = host->st_atime;
4663114Sgblack@eecs.umich.edu    tgt->st_atimeX = TheISA::htog(tgt->st_atimeX);
4678737Skoansin.tan@gmail.com    tgt->st_mtimeX = host->st_mtime;
4683114Sgblack@eecs.umich.edu    tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX);
4698737Skoansin.tan@gmail.com    tgt->st_ctimeX = host->st_ctime;
47011906SBrandon.Potter@amd.com    tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX);
4714061Sgblack@eecs.umich.edu    // Force the block size to be 8k. This helps to ensure buffered io works
4724061Sgblack@eecs.umich.edu    // consistently across different hosts.
4738737Skoansin.tan@gmail.com    tgt->st_blksize = 0x2000;
4743113Sgblack@eecs.umich.edu    tgt->st_blksize = TheISA::htog(tgt->st_blksize);
4758737Skoansin.tan@gmail.com    tgt->st_blocks = host->st_blocks;
4763113Sgblack@eecs.umich.edu    tgt->st_blocks = TheISA::htog(tgt->st_blocks);
4773113Sgblack@eecs.umich.edu}
4783113Sgblack@eecs.umich.edu
4793113Sgblack@eecs.umich.edu// Same for stat64
4803113Sgblack@eecs.umich.edu
48112032Sandreas.sandberg@arm.comtemplate <typename target_stat, typename host_stat64>
4823113Sgblack@eecs.umich.edustatic void
4833113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false)
4844189Sgblack@eecs.umich.edu{
4854189Sgblack@eecs.umich.edu    using namespace TheISA;
4863113Sgblack@eecs.umich.edu
4873113Sgblack@eecs.umich.edu    convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY);
4883113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC)
4898737Skoansin.tan@gmail.com    tgt->st_atime_nsec = host->st_atime_nsec;
4903113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec);
4918737Skoansin.tan@gmail.com    tgt->st_mtime_nsec = host->st_mtime_nsec;
4923113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec);
4938737Skoansin.tan@gmail.com    tgt->st_ctime_nsec = host->st_ctime_nsec;
4943113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec);
4953113Sgblack@eecs.umich.edu#else
4963113Sgblack@eecs.umich.edu    tgt->st_atime_nsec = 0;
4973113Sgblack@eecs.umich.edu    tgt->st_mtime_nsec = 0;
4983113Sgblack@eecs.umich.edu    tgt->st_ctime_nsec = 0;
4993113Sgblack@eecs.umich.edu#endif
5003113Sgblack@eecs.umich.edu}
50111906SBrandon.Potter@amd.com
5023113Sgblack@eecs.umich.edu//Here are a couple convenience functions
50312032Sandreas.sandberg@arm.comtemplate<class OS>
5048852Sandreas.hansson@arm.comstatic void
50511906SBrandon.Potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr,
5063113Sgblack@eecs.umich.edu        hst_stat *host, bool fakeTTY = false)
5073113Sgblack@eecs.umich.edu{
5083113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf;
5093113Sgblack@eecs.umich.edu    tgt_stat_buf tgt(addr);
5103113Sgblack@eecs.umich.edu    convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY);
5113113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5123113Sgblack@eecs.umich.edu}
5133113Sgblack@eecs.umich.edu
51412032Sandreas.sandberg@arm.comtemplate<class OS>
5158852Sandreas.hansson@arm.comstatic void
51611906SBrandon.Potter@amd.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr,
5173113Sgblack@eecs.umich.edu        hst_stat64 *host, bool fakeTTY = false)
5183113Sgblack@eecs.umich.edu{
5193113Sgblack@eecs.umich.edu    typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf;
5206686Stjones1@inf.ed.ac.uk    tgt_stat_buf tgt(addr);
5213113Sgblack@eecs.umich.edu    convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY);
5223113Sgblack@eecs.umich.edu    tgt.copyOut(mem);
5233113Sgblack@eecs.umich.edu}
52411759Sbrandon.potter@amd.com
52512032Sandreas.sandberg@arm.comtemplate <class OS>
52611759Sbrandon.potter@amd.comstatic void
52711759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr,
52811759Sbrandon.potter@amd.com                 hst_statfs *host)
52911759Sbrandon.potter@amd.com{
53011759Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tgt_statfs> tgt(addr);
53111812Sbaz21@cam.ac.uk
53211812Sbaz21@cam.ac.uk    tgt->f_type = TheISA::htog(host->f_type);
53311812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
53411759Sbrandon.potter@amd.com    tgt->f_bsize = TheISA::htog(host->f_iosize);
53511812Sbaz21@cam.ac.uk#else
53611759Sbrandon.potter@amd.com    tgt->f_bsize = TheISA::htog(host->f_bsize);
53711759Sbrandon.potter@amd.com#endif
53811759Sbrandon.potter@amd.com    tgt->f_blocks = TheISA::htog(host->f_blocks);
53911759Sbrandon.potter@amd.com    tgt->f_bfree = TheISA::htog(host->f_bfree);
54011759Sbrandon.potter@amd.com    tgt->f_bavail = TheISA::htog(host->f_bavail);
54111759Sbrandon.potter@amd.com    tgt->f_files = TheISA::htog(host->f_files);
54211759Sbrandon.potter@amd.com    tgt->f_ffree = TheISA::htog(host->f_ffree);
54311812Sbaz21@cam.ac.uk    memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid));
54411812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
54511812Sbaz21@cam.ac.uk    tgt->f_namelen = TheISA::htog(host->f_namemax);
54611812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_bsize);
54711812Sbaz21@cam.ac.uk#elif defined(__APPLE__)
54811812Sbaz21@cam.ac.uk    tgt->f_namelen = 0;
54911812Sbaz21@cam.ac.uk    tgt->f_frsize = 0;
55011759Sbrandon.potter@amd.com#else
55111759Sbrandon.potter@amd.com    tgt->f_namelen = TheISA::htog(host->f_namelen);
55211812Sbaz21@cam.ac.uk    tgt->f_frsize = TheISA::htog(host->f_frsize);
55311812Sbaz21@cam.ac.uk#endif
55411759Sbrandon.potter@amd.com#if defined(__linux__)
55511812Sbaz21@cam.ac.uk    memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare));
55611812Sbaz21@cam.ac.uk#else
55711812Sbaz21@cam.ac.uk    /*
55811812Sbaz21@cam.ac.uk     * The fields are different sizes per OS. Don't bother with
55911812Sbaz21@cam.ac.uk     * f_spare or f_reserved on non-Linux for now.
56011812Sbaz21@cam.ac.uk     */
56111812Sbaz21@cam.ac.uk    memset(&tgt->f_spare, 0, sizeof(tgt->f_spare));
56211759Sbrandon.potter@amd.com#endif
56311759Sbrandon.potter@amd.com
56411759Sbrandon.potter@amd.com    tgt.copyOut(mem);
56511759Sbrandon.potter@amd.com}
566378SN/A
567378SN/A/// Target ioctl() handler.  For the most part, programs call ioctl()
5689141Smarc.orr@gmail.com/// only to find out if their stdout is a tty, to determine whether to
5699141Smarc.orr@gmail.com/// do line or block buffering.  We always claim that output fds are
570360SN/A/// not TTYs to provide repeatable results.
5711450SN/Atemplate <class OS>
57211856Sbrandon.potter@amd.comSyscallReturn
573360SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *process,
5746701Sgblack@eecs.umich.edu          ThreadContext *tc)
57511856Sbrandon.potter@amd.com{
57611856Sbrandon.potter@amd.com    int index = 0;
577360SN/A    int tgt_fd = process->getSyscallArg(tc, index);
57810930Sbrandon.potter@amd.com    unsigned req = process->getSyscallArg(tc, index);
579360SN/A
58011856Sbrandon.potter@amd.com    DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req);
58111856Sbrandon.potter@amd.com
58210496Ssteve.reinhardt@amd.com    FDEntry *fde = process->getFDEntry(tgt_fd);
58311856Sbrandon.potter@amd.com
58411856Sbrandon.potter@amd.com    if (fde == NULL) {
5851458SN/A        // doesn't map to any simulator fd: not a valid target fd
586360SN/A        return -EBADF;
58711856Sbrandon.potter@amd.com    }
58811856Sbrandon.potter@amd.com
58911856Sbrandon.potter@amd.com    if (fde->driver != NULL) {
59011856Sbrandon.potter@amd.com        return fde->driver->ioctl(process, tc, req);
59111856Sbrandon.potter@amd.com    }
59211856Sbrandon.potter@amd.com
59311856Sbrandon.potter@amd.com    if (OS::isTtyReq(req)) {
59411856Sbrandon.potter@amd.com        return -ENOTTY;
59510496Ssteve.reinhardt@amd.com    }
59611856Sbrandon.potter@amd.com
59711856Sbrandon.potter@amd.com    warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n",
59811856Sbrandon.potter@amd.com         tgt_fd, req, tc->pcState());
59911856Sbrandon.potter@amd.com    return -ENOTTY;
60011856Sbrandon.potter@amd.com}
60110930Sbrandon.potter@amd.com
6029141Smarc.orr@gmail.comtemplate <class OS>
603360SN/Astatic SyscallReturn
604360SN/AopenFunc(SyscallDesc *desc, int callnum, Process *process,
605360SN/A         ThreadContext *tc, int index)
60611907SBrandon.Potter@amd.com{
60711907SBrandon.Potter@amd.com    std::string path;
60811907SBrandon.Potter@amd.com
609360SN/A    if (!tc->getMemProxy().tryReadString(path,
61011907SBrandon.Potter@amd.com                process->getSyscallArg(tc, index)))
61111907SBrandon.Potter@amd.com        return -EFAULT;
61211907SBrandon.Potter@amd.com
61311907SBrandon.Potter@amd.com    int tgtFlags = process->getSyscallArg(tc, index);
61411907SBrandon.Potter@amd.com    int mode = process->getSyscallArg(tc, index);
61511907SBrandon.Potter@amd.com    int hostFlags = 0;
61611907SBrandon.Potter@amd.com
61711907SBrandon.Potter@amd.com    // translate open flags
61811907SBrandon.Potter@amd.com    for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) {
61911907SBrandon.Potter@amd.com        if (tgtFlags & OS::openFlagTable[i].tgtFlag) {
62011907SBrandon.Potter@amd.com            tgtFlags &= ~OS::openFlagTable[i].tgtFlag;
62111907SBrandon.Potter@amd.com            hostFlags |= OS::openFlagTable[i].hostFlag;
62211907SBrandon.Potter@amd.com        }
62311907SBrandon.Potter@amd.com    }
624360SN/A
62511907SBrandon.Potter@amd.com    // any target flags left?
6261458SN/A    if (tgtFlags != 0)
627360SN/A        warn("Syscall: open: cannot decode flags 0x%x", tgtFlags);
62811907SBrandon.Potter@amd.com
62911907SBrandon.Potter@amd.com#ifdef __CYGWIN32__
63011907SBrandon.Potter@amd.com    hostFlags |= O_BINARY;
63111907SBrandon.Potter@amd.com#endif
63211907SBrandon.Potter@amd.com
63311907SBrandon.Potter@amd.com    // Adjust path for current working directory
63411907SBrandon.Potter@amd.com    path = process->fullPath(path);
63511907SBrandon.Potter@amd.com
63611907SBrandon.Potter@amd.com    DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str());
63711907SBrandon.Potter@amd.com
638360SN/A    if (startswith(path, "/dev/")) {
63911907SBrandon.Potter@amd.com        std::string filename = path.substr(strlen("/dev/"));
64011907SBrandon.Potter@amd.com        if (filename == "sysdev0") {
64111907SBrandon.Potter@amd.com            // This is a memory-mapped high-resolution timer device on Alpha.
642360SN/A            // We don't support it, so just punt.
643360SN/A            warn("Ignoring open(%s, ...)\n", path);
64411907SBrandon.Potter@amd.com            return -ENOENT;
64511907SBrandon.Potter@amd.com        }
64611907SBrandon.Potter@amd.com
64711907SBrandon.Potter@amd.com        EmulatedDriver *drv = process->findDriver(filename);
648360SN/A        if (drv != NULL) {
64911907SBrandon.Potter@amd.com            // the driver's open method will allocate a fd from the
650360SN/A            // process if necessary.
651360SN/A            return drv->open(process, tc, mode, hostFlags);
65211907SBrandon.Potter@amd.com        }
6533669Sbinkertn@umich.edu
65411907SBrandon.Potter@amd.com        // fall through here for pass through to host devices, such as
65511907SBrandon.Potter@amd.com        // /dev/zero
65611907SBrandon.Potter@amd.com    }
65711907SBrandon.Potter@amd.com
65811907SBrandon.Potter@amd.com    int fd;
65911907SBrandon.Potter@amd.com    int local_errno;
66011907SBrandon.Potter@amd.com    if (startswith(path, "/proc/") || startswith(path, "/system/") ||
66111907SBrandon.Potter@amd.com        startswith(path, "/platform/") || startswith(path, "/sys/")) {
66211907SBrandon.Potter@amd.com        // It's a proc/sys entry and requires special handling
66311907SBrandon.Potter@amd.com        fd = OS::openSpecialFile(path, process, tc);
66411907SBrandon.Potter@amd.com        local_errno = ENOENT;
66511907SBrandon.Potter@amd.com     } else {
66611907SBrandon.Potter@amd.com        // open the file
66711907SBrandon.Potter@amd.com        fd = open(path.c_str(), hostFlags, mode);
66811907SBrandon.Potter@amd.com        local_errno = errno;
66911907SBrandon.Potter@amd.com     }
67011907SBrandon.Potter@amd.com
67111907SBrandon.Potter@amd.com    if (fd == -1)
67211907SBrandon.Potter@amd.com        return -local_errno;
67311907SBrandon.Potter@amd.com
67411907SBrandon.Potter@amd.com    return process->allocFD(fd, path.c_str(), hostFlags, mode, false);
6751706SN/A}
67611907SBrandon.Potter@amd.com
67711907SBrandon.Potter@amd.com/// Target open() handler.
67811907SBrandon.Potter@amd.comtemplate <class OS>
67911907SBrandon.Potter@amd.comSyscallReturn
68011907SBrandon.Potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process,
68111907SBrandon.Potter@amd.com         ThreadContext *tc)
68210496Ssteve.reinhardt@amd.com{
68310496Ssteve.reinhardt@amd.com    return openFunc<OS>(desc, callnum, process, tc, 0);
68411907SBrandon.Potter@amd.com}
68511907SBrandon.Potter@amd.com
68611907SBrandon.Potter@amd.com/// Target openat() handler.
68711907SBrandon.Potter@amd.comtemplate <class OS>
68811907SBrandon.Potter@amd.comSyscallReturn
68911907SBrandon.Potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process,
69010496Ssteve.reinhardt@amd.com           ThreadContext *tc)
69111907SBrandon.Potter@amd.com{
69211907SBrandon.Potter@amd.com    int index = 0;
69311907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
69411907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
69510496Ssteve.reinhardt@amd.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
69610496Ssteve.reinhardt@amd.com    return openFunc<OS>(desc, callnum, process, tc, 1);
69711907SBrandon.Potter@amd.com}
69811907SBrandon.Potter@amd.com
69911907SBrandon.Potter@amd.com/// Target unlinkat() handler.
70011907SBrandon.Potter@amd.comtemplate <class OS>
70111907SBrandon.Potter@amd.comSyscallReturn
70211907SBrandon.Potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
70311907SBrandon.Potter@amd.com             ThreadContext *tc)
70411907SBrandon.Potter@amd.com{
70511907SBrandon.Potter@amd.com    int index = 0;
70611907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
70711907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
70811907SBrandon.Potter@amd.com        warn("unlinkat: first argument not AT_FDCWD; unlikely to work");
70911907SBrandon.Potter@amd.com
71011907SBrandon.Potter@amd.com    return unlinkHelper(desc, callnum, process, tc, 1);
71111907SBrandon.Potter@amd.com}
71211907SBrandon.Potter@amd.com
71311907SBrandon.Potter@amd.com/// Target facessat() handler
71411907SBrandon.Potter@amd.comtemplate <class OS>
71511907SBrandon.Potter@amd.comSyscallReturn
71611907SBrandon.Potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process,
71711907SBrandon.Potter@amd.com              ThreadContext *tc)
71811907SBrandon.Potter@amd.com{
71911907SBrandon.Potter@amd.com    int index = 0;
72011907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
72111907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
722360SN/A        warn("faccessat: first argument not AT_FDCWD; unlikely to work");
72311907SBrandon.Potter@amd.com    return accessFunc(desc, callnum, process, tc, 1);
72411907SBrandon.Potter@amd.com}
72511907SBrandon.Potter@amd.com
72611907SBrandon.Potter@amd.com/// Target readlinkat() handler
72711907SBrandon.Potter@amd.comtemplate <class OS>
72811907SBrandon.Potter@amd.comSyscallReturn
72911907SBrandon.Potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process,
73011907SBrandon.Potter@amd.com               ThreadContext *tc)
73111907SBrandon.Potter@amd.com{
73211907SBrandon.Potter@amd.com    int index = 0;
73311907SBrandon.Potter@amd.com    int dirfd = process->getSyscallArg(tc, index);
73411907SBrandon.Potter@amd.com    if (dirfd != OS::TGT_AT_FDCWD)
73511907SBrandon.Potter@amd.com        warn("openat: first argument not AT_FDCWD; unlikely to work");
736360SN/A    return readlinkFunc(desc, callnum, process, tc, 1);
737360SN/A}
73810027SChris.Adeniyi-Jones@arm.com
73910027SChris.Adeniyi-Jones@arm.com/// Target renameat() handler.
74010027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
74111851Sbrandon.potter@amd.comSyscallReturn
74210027SChris.Adeniyi-Jones@arm.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process,
74310027SChris.Adeniyi-Jones@arm.com             ThreadContext *tc)
74411907SBrandon.Potter@amd.com{
74510027SChris.Adeniyi-Jones@arm.com    int index = 0;
74610027SChris.Adeniyi-Jones@arm.com
74710027SChris.Adeniyi-Jones@arm.com    int olddirfd = process->getSyscallArg(tc, index);
74810027SChris.Adeniyi-Jones@arm.com    if (olddirfd != OS::TGT_AT_FDCWD)
74910027SChris.Adeniyi-Jones@arm.com        warn("renameat: first argument not AT_FDCWD; unlikely to work");
75011851Sbrandon.potter@amd.com
75111851Sbrandon.potter@amd.com    std::string old_name;
75210027SChris.Adeniyi-Jones@arm.com
75311907SBrandon.Potter@amd.com    if (!tc->getMemProxy().tryReadString(old_name,
75410027SChris.Adeniyi-Jones@arm.com                                         process->getSyscallArg(tc, index)))
75510027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
75610633Smichaelupton@gmail.com
75710633Smichaelupton@gmail.com    int newdirfd = process->getSyscallArg(tc, index);
75810633Smichaelupton@gmail.com    if (newdirfd != OS::TGT_AT_FDCWD)
75911851Sbrandon.potter@amd.com        warn("renameat: third argument not AT_FDCWD; unlikely to work");
76010633Smichaelupton@gmail.com
76110633Smichaelupton@gmail.com    std::string new_name;
76210633Smichaelupton@gmail.com
76310633Smichaelupton@gmail.com    if (!tc->getMemProxy().tryReadString(new_name,
76410633Smichaelupton@gmail.com                                         process->getSyscallArg(tc, index)))
76510633Smichaelupton@gmail.com        return -EFAULT;
76610633Smichaelupton@gmail.com
76710633Smichaelupton@gmail.com    // Adjust path for current working directory
76810633Smichaelupton@gmail.com    old_name = process->fullPath(old_name);
76910633Smichaelupton@gmail.com    new_name = process->fullPath(new_name);
77010203SAli.Saidi@ARM.com
77110203SAli.Saidi@ARM.com    int result = rename(old_name.c_str(), new_name.c_str());
77210203SAli.Saidi@ARM.com    return (result == -1) ? -errno : result;
77311851Sbrandon.potter@amd.com}
77411851Sbrandon.potter@amd.com
77510203SAli.Saidi@ARM.com/// Target sysinfo() handler.
77610203SAli.Saidi@ARM.comtemplate <class OS>
77710203SAli.Saidi@ARM.comSyscallReturn
77810203SAli.Saidi@ARM.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process,
77910203SAli.Saidi@ARM.com            ThreadContext *tc)
78010203SAli.Saidi@ARM.com{
78110203SAli.Saidi@ARM.com
78210203SAli.Saidi@ARM.com    int index = 0;
78310203SAli.Saidi@ARM.com    TypedBufferArg<typename OS::tgt_sysinfo>
78410203SAli.Saidi@ARM.com        sysinfo(process->getSyscallArg(tc, index));
78510203SAli.Saidi@ARM.com
78611851Sbrandon.potter@amd.com    sysinfo->uptime = seconds_since_epoch;
78711851Sbrandon.potter@amd.com    sysinfo->totalram = process->system->memSize();
78810203SAli.Saidi@ARM.com    sysinfo->mem_unit = 1;
78910203SAli.Saidi@ARM.com
79010203SAli.Saidi@ARM.com    sysinfo.copyOut(tc->getMemProxy());
79110203SAli.Saidi@ARM.com
79210203SAli.Saidi@ARM.com    return 0;
79310203SAli.Saidi@ARM.com}
79410203SAli.Saidi@ARM.com
79510203SAli.Saidi@ARM.com/// Target chmod() handler.
79610850SGiacomo.Gabrielli@arm.comtemplate <class OS>
79710850SGiacomo.Gabrielli@arm.comSyscallReturn
79810850SGiacomo.Gabrielli@arm.comchmodFunc(SyscallDesc *desc, int callnum, Process *process,
79911851Sbrandon.potter@amd.com          ThreadContext *tc)
80010850SGiacomo.Gabrielli@arm.com{
80110850SGiacomo.Gabrielli@arm.com    std::string path;
80210850SGiacomo.Gabrielli@arm.com
80310850SGiacomo.Gabrielli@arm.com    int index = 0;
80410850SGiacomo.Gabrielli@arm.com    if (!tc->getMemProxy().tryReadString(path,
80510850SGiacomo.Gabrielli@arm.com                process->getSyscallArg(tc, index))) {
80610850SGiacomo.Gabrielli@arm.com        return -EFAULT;
80710850SGiacomo.Gabrielli@arm.com    }
80810850SGiacomo.Gabrielli@arm.com
80910850SGiacomo.Gabrielli@arm.com    uint32_t mode = process->getSyscallArg(tc, index);
81010850SGiacomo.Gabrielli@arm.com    mode_t hostMode = 0;
81110850SGiacomo.Gabrielli@arm.com
81210850SGiacomo.Gabrielli@arm.com    // XXX translate mode flags via OS::something???
81310850SGiacomo.Gabrielli@arm.com    hostMode = mode;
81410850SGiacomo.Gabrielli@arm.com
81510850SGiacomo.Gabrielli@arm.com    // Adjust path for current working directory
81610850SGiacomo.Gabrielli@arm.com    path = process->fullPath(path);
81710850SGiacomo.Gabrielli@arm.com
81810850SGiacomo.Gabrielli@arm.com    // do the chmod
81910850SGiacomo.Gabrielli@arm.com    int result = chmod(path.c_str(), hostMode);
82010850SGiacomo.Gabrielli@arm.com    if (result < 0)
82110850SGiacomo.Gabrielli@arm.com        return -errno;
82210850SGiacomo.Gabrielli@arm.com
82310850SGiacomo.Gabrielli@arm.com    return 0;
82410850SGiacomo.Gabrielli@arm.com}
82510850SGiacomo.Gabrielli@arm.com
82610850SGiacomo.Gabrielli@arm.com
82710850SGiacomo.Gabrielli@arm.com/// Target fchmod() handler.
82810850SGiacomo.Gabrielli@arm.comtemplate <class OS>
82910850SGiacomo.Gabrielli@arm.comSyscallReturn
83010850SGiacomo.Gabrielli@arm.comfchmodFunc(SyscallDesc *desc, int callnum, Process *process,
83110850SGiacomo.Gabrielli@arm.com           ThreadContext *tc)
8326640Svince@csl.cornell.edu{
8336640Svince@csl.cornell.edu    int index = 0;
8346640Svince@csl.cornell.edu    int tgt_fd = process->getSyscallArg(tc, index);
83511851Sbrandon.potter@amd.com    uint32_t mode = process->getSyscallArg(tc, index);
83611851Sbrandon.potter@amd.com
8376640Svince@csl.cornell.edu    int sim_fd = process->getSimFD(tgt_fd);
8386640Svince@csl.cornell.edu    if (sim_fd < 0)
8396701Sgblack@eecs.umich.edu        return -EBADF;
8406701Sgblack@eecs.umich.edu
84110793Sbrandon.potter@amd.com    mode_t hostMode = 0;
8426640Svince@csl.cornell.edu
84311758Sbrandon.potter@amd.com    // XXX translate mode flags via OS::someting???
84411758Sbrandon.potter@amd.com    hostMode = mode;
84511758Sbrandon.potter@amd.com
8466640Svince@csl.cornell.edu    // do the fchmod
8478706Sandreas.hansson@arm.com    int result = fchmod(sim_fd, hostMode);
8486640Svince@csl.cornell.edu    if (result < 0)
8496701Sgblack@eecs.umich.edu        return -errno;
8506640Svince@csl.cornell.edu
851360SN/A    return 0;
8521999SN/A}
8531999SN/A
8541999SN/A/// Target mremap() handler.
85511851Sbrandon.potter@amd.comtemplate <class OS>
8562680Sktlim@umich.eduSyscallReturn
8571999SN/AmremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
8581999SN/A{
8591999SN/A    int index = 0;
8606701Sgblack@eecs.umich.edu    Addr start = process->getSyscallArg(tc, index);
8618852Sandreas.hansson@arm.com    uint64_t old_length = process->getSyscallArg(tc, index);
8626701Sgblack@eecs.umich.edu    uint64_t new_length = process->getSyscallArg(tc, index);
8631999SN/A    uint64_t flags = process->getSyscallArg(tc, index);
8646701Sgblack@eecs.umich.edu    uint64_t provided_address = 0;
8651999SN/A    bool use_provided_address = flags & OS::TGT_MREMAP_FIXED;
8666701Sgblack@eecs.umich.edu
8671999SN/A    if (use_provided_address)
8681999SN/A        provided_address = process->getSyscallArg(tc, index);
8691999SN/A
8701999SN/A    if ((start % TheISA::PageBytes != 0) ||
8711999SN/A        (provided_address % TheISA::PageBytes != 0)) {
8723669Sbinkertn@umich.edu        warn("mremap failing: arguments not page aligned");
8733669Sbinkertn@umich.edu        return -EINVAL;
8743669Sbinkertn@umich.edu    }
8751999SN/A
8761999SN/A    new_length = roundUp(new_length, TheISA::PageBytes);
8771999SN/A
8782218SN/A    if (new_length > old_length) {
8791999SN/A        if ((start + old_length) == process->mmap_end &&
8801999SN/A            (!use_provided_address || provided_address == start)) {
8811999SN/A            uint64_t diff = new_length - old_length;
8821999SN/A            process->allocateMem(process->mmap_end, diff);
8831999SN/A            process->mmap_end += diff;
8841999SN/A            return start;
8851999SN/A        } else {
8861999SN/A            if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) {
88711856Sbrandon.potter@amd.com                warn("can't remap here and MREMAP_MAYMOVE flag not set\n");
8881999SN/A                return -ENOMEM;
8896701Sgblack@eecs.umich.edu            } else {
89011856Sbrandon.potter@amd.com                uint64_t new_start = use_provided_address ?
89111856Sbrandon.potter@amd.com                    provided_address : process->mmap_end;
89210931Sbrandon.potter@amd.com                process->pTable->remap(start, old_length, new_start);
89311856Sbrandon.potter@amd.com                warn("mremapping to new vaddr %08p-%08p, adding %d\n",
89411856Sbrandon.potter@amd.com                     new_start, new_start + new_length,
8951999SN/A                     new_length - old_length);
89611856Sbrandon.potter@amd.com                // add on the remaining unallocated pages
8971999SN/A                process->allocateMem(new_start + old_length,
89811856Sbrandon.potter@amd.com                                     new_length - old_length,
8991999SN/A                                     use_provided_address /* clobber */);
90011856Sbrandon.potter@amd.com                if (!use_provided_address)
9011999SN/A                    process->mmap_end += new_length;
90211856Sbrandon.potter@amd.com                if (use_provided_address &&
9031999SN/A                    new_start + new_length > process->mmap_end) {
9041999SN/A                    // something fishy going on here, at least notify the user
9055877Shsul@eecs.umich.edu                    // @todo: increase mmap_end?
9065877Shsul@eecs.umich.edu                    warn("mmap region limit exceeded with MREMAP_FIXED\n");
9075877Shsul@eecs.umich.edu                }
90811851Sbrandon.potter@amd.com                warn("returning %08p as start\n", new_start);
9095877Shsul@eecs.umich.edu                return new_start;
9106701Sgblack@eecs.umich.edu            }
9116701Sgblack@eecs.umich.edu        }
9126701Sgblack@eecs.umich.edu    } else {
9136701Sgblack@eecs.umich.edu        if (use_provided_address && provided_address != start)
9146701Sgblack@eecs.umich.edu            process->pTable->remap(start, new_length, provided_address);
91510027SChris.Adeniyi-Jones@arm.com        process->pTable->unmap(start + new_length, old_length - new_length);
91610027SChris.Adeniyi-Jones@arm.com        return use_provided_address ? provided_address : start;
91710027SChris.Adeniyi-Jones@arm.com    }
91810027SChris.Adeniyi-Jones@arm.com}
91910027SChris.Adeniyi-Jones@arm.com
9205877Shsul@eecs.umich.edu/// Target stat() handler.
92110318Sandreas.hansson@arm.comtemplate <class OS>
92210318Sandreas.hansson@arm.comSyscallReturn
9235877Shsul@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, Process *process,
9245877Shsul@eecs.umich.edu         ThreadContext *tc)
9255877Shsul@eecs.umich.edu{
9265877Shsul@eecs.umich.edu    std::string path;
92710486Stjablin@gmail.com
92810486Stjablin@gmail.com    int index = 0;
9295877Shsul@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
93011905SBrandon.Potter@amd.com                process->getSyscallArg(tc, index))) {
93111905SBrandon.Potter@amd.com        return -EFAULT;
93211905SBrandon.Potter@amd.com    }
93311905SBrandon.Potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
93410027SChris.Adeniyi-Jones@arm.com
93512206Srico.amslinger@informatik.uni-augsburg.de    // Adjust path for current working directory
93612206Srico.amslinger@informatik.uni-augsburg.de    path = process->fullPath(path);
9375877Shsul@eecs.umich.edu
93811905SBrandon.Potter@amd.com    struct stat hostBuf;
93911905SBrandon.Potter@amd.com    int result = stat(path.c_str(), &hostBuf);
9405877Shsul@eecs.umich.edu
9415877Shsul@eecs.umich.edu    if (result < 0)
94210027SChris.Adeniyi-Jones@arm.com        return -errno;
9435877Shsul@eecs.umich.edu
9445877Shsul@eecs.umich.edu    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9455877Shsul@eecs.umich.edu
94612206Srico.amslinger@informatik.uni-augsburg.de    return 0;
94712206Srico.amslinger@informatik.uni-augsburg.de}
94812206Srico.amslinger@informatik.uni-augsburg.de
94912206Srico.amslinger@informatik.uni-augsburg.de
95012206Srico.amslinger@informatik.uni-augsburg.de/// Target stat64() handler.
95112206Srico.amslinger@informatik.uni-augsburg.detemplate <class OS>
95212206Srico.amslinger@informatik.uni-augsburg.deSyscallReturn
95312206Srico.amslinger@informatik.uni-augsburg.destat64Func(SyscallDesc *desc, int callnum, Process *process,
95412206Srico.amslinger@informatik.uni-augsburg.de           ThreadContext *tc)
95510027SChris.Adeniyi-Jones@arm.com{
95610027SChris.Adeniyi-Jones@arm.com    std::string path;
95710027SChris.Adeniyi-Jones@arm.com
95810027SChris.Adeniyi-Jones@arm.com    int index = 0;
9595877Shsul@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
96010027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index)))
96110027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
96210027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
96310027SChris.Adeniyi-Jones@arm.com
96412206Srico.amslinger@informatik.uni-augsburg.de    // Adjust path for current working directory
96512206Srico.amslinger@informatik.uni-augsburg.de    path = process->fullPath(path);
96612206Srico.amslinger@informatik.uni-augsburg.de
96712206Srico.amslinger@informatik.uni-augsburg.de#if NO_STAT64
96810027SChris.Adeniyi-Jones@arm.com    struct stat  hostBuf;
96910027SChris.Adeniyi-Jones@arm.com    int result = stat(path.c_str(), &hostBuf);
97010027SChris.Adeniyi-Jones@arm.com#else
97110027SChris.Adeniyi-Jones@arm.com    struct stat64 hostBuf;
97210027SChris.Adeniyi-Jones@arm.com    int result = stat64(path.c_str(), &hostBuf);
97310027SChris.Adeniyi-Jones@arm.com#endif
9745877Shsul@eecs.umich.edu
9755877Shsul@eecs.umich.edu    if (result < 0)
9765877Shsul@eecs.umich.edu        return -errno;
97710027SChris.Adeniyi-Jones@arm.com
97810027SChris.Adeniyi-Jones@arm.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
9798601Ssteve.reinhardt@amd.com
98010027SChris.Adeniyi-Jones@arm.com    return 0;
9815877Shsul@eecs.umich.edu}
9825877Shsul@eecs.umich.edu
9831999SN/A
984378SN/A/// Target fstatat64() handler.
985360SN/Atemplate <class OS>
9861450SN/ASyscallReturn
98711851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process,
9882680Sktlim@umich.edu              ThreadContext *tc)
989360SN/A{
990360SN/A    int index = 0;
991360SN/A    int dirfd = process->getSyscallArg(tc, index);
9926701Sgblack@eecs.umich.edu    if (dirfd != OS::TGT_AT_FDCWD)
9938852Sandreas.hansson@arm.com        warn("fstatat64: first argument not AT_FDCWD; unlikely to work");
9946701Sgblack@eecs.umich.edu
9956701Sgblack@eecs.umich.edu    std::string path;
9966701Sgblack@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
9976701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index)))
998360SN/A        return -EFAULT;
9993669Sbinkertn@umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10003669Sbinkertn@umich.edu
10013669Sbinkertn@umich.edu    // Adjust path for current working directory
1002360SN/A    path = process->fullPath(path);
1003360SN/A
1004360SN/A#if NO_STAT64
1005360SN/A    struct stat  hostBuf;
10062218SN/A    int result = stat(path.c_str(), &hostBuf);
1007360SN/A#else
10088706Sandreas.hansson@arm.com    struct stat64 hostBuf;
1009360SN/A    int result = stat64(path.c_str(), &hostBuf);
10101458SN/A#endif
1011360SN/A
1012360SN/A    if (result < 0)
1013360SN/A        return -errno;
10145074Ssaidi@eecs.umich.edu
10155074Ssaidi@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
10165074Ssaidi@eecs.umich.edu
101711851Sbrandon.potter@amd.com    return 0;
10185074Ssaidi@eecs.umich.edu}
10195074Ssaidi@eecs.umich.edu
10205074Ssaidi@eecs.umich.edu
10215074Ssaidi@eecs.umich.edu/// Target fstat64() handler.
10226701Sgblack@eecs.umich.edutemplate <class OS>
10238852Sandreas.hansson@arm.comSyscallReturn
10246701Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, Process *process,
10255074Ssaidi@eecs.umich.edu            ThreadContext *tc)
10266701Sgblack@eecs.umich.edu{
10275074Ssaidi@eecs.umich.edu    int index = 0;
10285074Ssaidi@eecs.umich.edu    int tgt_fd = process->getSyscallArg(tc, index);
10295074Ssaidi@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
10305074Ssaidi@eecs.umich.edu
10315208Ssaidi@eecs.umich.edu    int sim_fd = process->getSimFD(tgt_fd);
10325208Ssaidi@eecs.umich.edu    if (sim_fd < 0)
10335208Ssaidi@eecs.umich.edu        return -EBADF;
10345208Ssaidi@eecs.umich.edu
10355074Ssaidi@eecs.umich.edu#if NO_STAT64
10365074Ssaidi@eecs.umich.edu    struct stat  hostBuf;
10375208Ssaidi@eecs.umich.edu    int result = fstat(sim_fd, &hostBuf);
10385074Ssaidi@eecs.umich.edu#else
10395074Ssaidi@eecs.umich.edu    struct stat64  hostBuf;
10405074Ssaidi@eecs.umich.edu    int result = fstat64(sim_fd, &hostBuf);
10415074Ssaidi@eecs.umich.edu#endif
10428706Sandreas.hansson@arm.com
10435074Ssaidi@eecs.umich.edu    if (result < 0)
10445074Ssaidi@eecs.umich.edu        return -errno;
10455074Ssaidi@eecs.umich.edu
10465074Ssaidi@eecs.umich.edu    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
10475074Ssaidi@eecs.umich.edu
104810027SChris.Adeniyi-Jones@arm.com    return 0;
104910027SChris.Adeniyi-Jones@arm.com}
105010027SChris.Adeniyi-Jones@arm.com
105111851Sbrandon.potter@amd.com
105210027SChris.Adeniyi-Jones@arm.com/// Target lstat() handler.
105310027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
105410027SChris.Adeniyi-Jones@arm.comSyscallReturn
105510027SChris.Adeniyi-Jones@arm.comlstatFunc(SyscallDesc *desc, int callnum, Process *process,
105610027SChris.Adeniyi-Jones@arm.com          ThreadContext *tc)
105710793Sbrandon.potter@amd.com{
105810027SChris.Adeniyi-Jones@arm.com    std::string path;
105910027SChris.Adeniyi-Jones@arm.com
106010027SChris.Adeniyi-Jones@arm.com    int index = 0;
106110027SChris.Adeniyi-Jones@arm.com    if (!tc->getMemProxy().tryReadString(path,
106210027SChris.Adeniyi-Jones@arm.com                process->getSyscallArg(tc, index))) {
106310027SChris.Adeniyi-Jones@arm.com        return -EFAULT;
106410027SChris.Adeniyi-Jones@arm.com    }
106510027SChris.Adeniyi-Jones@arm.com    Addr bufPtr = process->getSyscallArg(tc, index);
106610027SChris.Adeniyi-Jones@arm.com
106710027SChris.Adeniyi-Jones@arm.com    // Adjust path for current working directory
106810027SChris.Adeniyi-Jones@arm.com    path = process->fullPath(path);
106910027SChris.Adeniyi-Jones@arm.com
107010027SChris.Adeniyi-Jones@arm.com    struct stat hostBuf;
107110027SChris.Adeniyi-Jones@arm.com    int result = lstat(path.c_str(), &hostBuf);
107210027SChris.Adeniyi-Jones@arm.com
107310027SChris.Adeniyi-Jones@arm.com    if (result < 0)
107410027SChris.Adeniyi-Jones@arm.com        return -errno;
107510027SChris.Adeniyi-Jones@arm.com
107610027SChris.Adeniyi-Jones@arm.com    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
107710027SChris.Adeniyi-Jones@arm.com
107810027SChris.Adeniyi-Jones@arm.com    return 0;
107910027SChris.Adeniyi-Jones@arm.com}
108010027SChris.Adeniyi-Jones@arm.com
108110027SChris.Adeniyi-Jones@arm.com/// Target lstat64() handler.
108210027SChris.Adeniyi-Jones@arm.comtemplate <class OS>
108310027SChris.Adeniyi-Jones@arm.comSyscallReturn
108410027SChris.Adeniyi-Jones@arm.comlstat64Func(SyscallDesc *desc, int callnum, Process *process,
10851999SN/A            ThreadContext *tc)
10861999SN/A{
10871999SN/A    std::string path;
108811856Sbrandon.potter@amd.com
10891999SN/A    int index = 0;
10906701Sgblack@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
109111856Sbrandon.potter@amd.com                process->getSyscallArg(tc, index))) {
109211856Sbrandon.potter@amd.com        return -EFAULT;
109310931Sbrandon.potter@amd.com    }
109411856Sbrandon.potter@amd.com    Addr bufPtr = process->getSyscallArg(tc, index);
109511856Sbrandon.potter@amd.com
10961999SN/A    // Adjust path for current working directory
109711856Sbrandon.potter@amd.com    path = process->fullPath(path);
10981999SN/A
10992764Sstever@eecs.umich.edu#if NO_STAT64
11002064SN/A    struct stat hostBuf;
110110931Sbrandon.potter@amd.com    int result = lstat(path.c_str(), &hostBuf);
11022064SN/A#else
11032064SN/A    struct stat64 hostBuf;
110410931Sbrandon.potter@amd.com    int result = lstat64(path.c_str(), &hostBuf);
11052064SN/A#endif
11061999SN/A
11071999SN/A    if (result < 0)
11082218SN/A        return -errno;
11091999SN/A
111010931Sbrandon.potter@amd.com    copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11111999SN/A
11121999SN/A    return 0;
11131999SN/A}
11141999SN/A
11151999SN/A/// Target fstat() handler.
1116378SN/Atemplate <class OS>
1117360SN/ASyscallReturn
11181450SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *process,
111911851Sbrandon.potter@amd.com          ThreadContext *tc)
11202680Sktlim@umich.edu{
1121360SN/A    int index = 0;
1122360SN/A    int tgt_fd = process->getSyscallArg(tc, index);
1123360SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
11246701Sgblack@eecs.umich.edu
11258852Sandreas.hansson@arm.com    DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd);
11266701Sgblack@eecs.umich.edu
11276701Sgblack@eecs.umich.edu    int sim_fd = process->getSimFD(tgt_fd);
11286701Sgblack@eecs.umich.edu    if (sim_fd < 0)
11296701Sgblack@eecs.umich.edu        return -EBADF;
1130360SN/A
11313669Sbinkertn@umich.edu    struct stat hostBuf;
11323669Sbinkertn@umich.edu    int result = fstat(sim_fd, &hostBuf);
11333669Sbinkertn@umich.edu
1134360SN/A    if (result < 0)
1135360SN/A        return -errno;
1136360SN/A
1137360SN/A    copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1));
11381458SN/A
1139360SN/A    return 0;
11408706Sandreas.hansson@arm.com}
1141360SN/A
11421458SN/A
1143360SN/A/// Target statfs() handler.
1144360SN/Atemplate <class OS>
11451999SN/ASyscallReturn
11461999SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
11471999SN/A           ThreadContext *tc)
114811851Sbrandon.potter@amd.com{
11492680Sktlim@umich.edu#if NO_STATFS
11501999SN/A    warn("Host OS cannot support calls to statfs. Ignoring syscall");
11511999SN/A#else
11521999SN/A    std::string path;
11536701Sgblack@eecs.umich.edu
11548852Sandreas.hansson@arm.com    int index = 0;
11556701Sgblack@eecs.umich.edu    if (!tc->getMemProxy().tryReadString(path,
11566701Sgblack@eecs.umich.edu                process->getSyscallArg(tc, index))) {
11576701Sgblack@eecs.umich.edu        return -EFAULT;
11586701Sgblack@eecs.umich.edu    }
11591999SN/A    Addr bufPtr = process->getSyscallArg(tc, index);
11603669Sbinkertn@umich.edu
11613669Sbinkertn@umich.edu    // Adjust path for current working directory
11623669Sbinkertn@umich.edu    path = process->fullPath(path);
11632764Sstever@eecs.umich.edu
11642064SN/A    struct statfs hostBuf;
11652064SN/A    int result = statfs(path.c_str(), &hostBuf);
11662064SN/A
11671999SN/A    if (result < 0)
11681999SN/A        return -errno;
11692064SN/A
11701999SN/A    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
11711999SN/A#endif
11721999SN/A    return 0;
11731999SN/A}
11748706Sandreas.hansson@arm.com
11751999SN/A
11761999SN/A/// Target fstatfs() handler.
11771999SN/Atemplate <class OS>
11781999SN/ASyscallReturn
1179378SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *process,
1180360SN/A            ThreadContext *tc)
11811450SN/A{
118211856Sbrandon.potter@amd.com    int index = 0;
1183360SN/A    int tgt_fd = process->getSyscallArg(tc, index);
11846701Sgblack@eecs.umich.edu    Addr bufPtr = process->getSyscallArg(tc, index);
118511856Sbrandon.potter@amd.com
118611856Sbrandon.potter@amd.com    int sim_fd = process->getSimFD(tgt_fd);
1187360SN/A    if (sim_fd < 0)
118811380Salexandru.dutu@amd.com        return -EBADF;
1189360SN/A
119011856Sbrandon.potter@amd.com    struct statfs hostBuf;
119111856Sbrandon.potter@amd.com    int result = fstatfs(sim_fd, &hostBuf);
11921458SN/A
119311856Sbrandon.potter@amd.com    if (result < 0)
1194360SN/A        return -errno;
1195360SN/A
119610931Sbrandon.potter@amd.com    copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf);
1197360SN/A
1198360SN/A    return 0;
11991458SN/A}
1200360SN/A
120110931Sbrandon.potter@amd.com
12022021SN/A/// Target writev() handler.
12031458SN/Atemplate <class OS>
1204360SN/ASyscallReturn
1205360SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *process,
1206360SN/A           ThreadContext *tc)
12071706SN/A{
12081706SN/A    int index = 0;
12091706SN/A    int tgt_fd = process->getSyscallArg(tc, index);
121011851Sbrandon.potter@amd.com
12112680Sktlim@umich.edu    int sim_fd = process->getSimFD(tgt_fd);
12121706SN/A    if (sim_fd < 0)
121311799Sbrandon.potter@amd.com        return -EBADF;
121411799Sbrandon.potter@amd.com
121511799Sbrandon.potter@amd.com    SETranslatingPortProxy &p = tc->getMemProxy();
12161706SN/A    uint64_t tiov_base = process->getSyscallArg(tc, index);
12171706SN/A    size_t count = process->getSyscallArg(tc, index);
12186701Sgblack@eecs.umich.edu    struct iovec hiov[count];
12198852Sandreas.hansson@arm.com    for (size_t i = 0; i < count; ++i) {
12206701Sgblack@eecs.umich.edu        typename OS::tgt_iovec tiov;
12216701Sgblack@eecs.umich.edu
12226701Sgblack@eecs.umich.edu        p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec),
12236701Sgblack@eecs.umich.edu                   (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec));
12241706SN/A        hiov[i].iov_len = TheISA::gtoh(tiov.iov_len);
12253669Sbinkertn@umich.edu        hiov[i].iov_base = new char [hiov[i].iov_len];
12263669Sbinkertn@umich.edu        p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base,
12273669Sbinkertn@umich.edu                   hiov[i].iov_len);
12281706SN/A    }
12291706SN/A
12301706SN/A    int result = writev(sim_fd, hiov, count);
12311706SN/A
12322218SN/A    for (size_t i = 0; i < count; ++i)
12331706SN/A        delete [] (char *)hiov[i].iov_base;
123411759Sbrandon.potter@amd.com
123511799Sbrandon.potter@amd.com    if (result < 0)
12361706SN/A        return -errno;
12371706SN/A
12381706SN/A    return result;
123911886Sbrandon.potter@amd.com}
124011886Sbrandon.potter@amd.com
124111886Sbrandon.potter@amd.com/// Real mmap handler.
124211886Sbrandon.potter@amd.comtemplate <class OS>
124311886Sbrandon.potter@amd.comSyscallReturn
124412426Sqtt2@cornell.edummapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc,
124511886Sbrandon.potter@amd.com         bool is_mmap2)
124611886Sbrandon.potter@amd.com{
124711886Sbrandon.potter@amd.com    int index = 0;
124812426Sqtt2@cornell.edu    Addr start = p->getSyscallArg(tc, index);
124912426Sqtt2@cornell.edu    uint64_t length = p->getSyscallArg(tc, index);
125012426Sqtt2@cornell.edu    int prot = p->getSyscallArg(tc, index);
125112426Sqtt2@cornell.edu    int tgt_flags = p->getSyscallArg(tc, index);
125212426Sqtt2@cornell.edu    int tgt_fd = p->getSyscallArg(tc, index);
125312426Sqtt2@cornell.edu    int offset = p->getSyscallArg(tc, index);
125412426Sqtt2@cornell.edu
125512426Sqtt2@cornell.edu    if (is_mmap2)
125612426Sqtt2@cornell.edu        offset *= TheISA::PageBytes;
125712426Sqtt2@cornell.edu
125811886Sbrandon.potter@amd.com    if (start & (TheISA::PageBytes - 1) ||
125911886Sbrandon.potter@amd.com        offset & (TheISA::PageBytes - 1) ||
126012426Sqtt2@cornell.edu        (tgt_flags & OS::TGT_MAP_PRIVATE &&
126111886Sbrandon.potter@amd.com         tgt_flags & OS::TGT_MAP_SHARED) ||
126211886Sbrandon.potter@amd.com        (!(tgt_flags & OS::TGT_MAP_PRIVATE) &&
126311886Sbrandon.potter@amd.com         !(tgt_flags & OS::TGT_MAP_SHARED)) ||
126411886Sbrandon.potter@amd.com        !length) {
126511886Sbrandon.potter@amd.com        return -EINVAL;
126611886Sbrandon.potter@amd.com    }
126711886Sbrandon.potter@amd.com
126811886Sbrandon.potter@amd.com    if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) {
126911886Sbrandon.potter@amd.com        // With shared mmaps, there are two cases to consider:
127011886Sbrandon.potter@amd.com        // 1) anonymous: writes should modify the mapping and this should be
127111886Sbrandon.potter@amd.com        // visible to observers who share the mapping. Currently, it's
127211886Sbrandon.potter@amd.com        // difficult to update the shared mapping because there's no
127311886Sbrandon.potter@amd.com        // structure which maintains information about the which virtual
127411886Sbrandon.potter@amd.com        // memory areas are shared. If that structure existed, it would be
127511886Sbrandon.potter@amd.com        // possible to make the translations point to the same frames.
127611886Sbrandon.potter@amd.com        // 2) file-backed: writes should modify the mapping and the file
127711886Sbrandon.potter@amd.com        // which is backed by the mapping. The shared mapping problem is the
127811886Sbrandon.potter@amd.com        // same as what was mentioned about the anonymous mappings. For
127911886Sbrandon.potter@amd.com        // file-backed mappings, the writes to the file are difficult
128011886Sbrandon.potter@amd.com        // because it requires syncing what the mapping holds with the file
128111886Sbrandon.potter@amd.com        // that resides on the host system. So, any write on a real system
128211886Sbrandon.potter@amd.com        // would cause the change to be propagated to the file mapping at
128311886Sbrandon.potter@amd.com        // some point in the future (the inode is tracked along with the
128411886Sbrandon.potter@amd.com        // mapping). This isn't guaranteed to always happen, but it usually
128511886Sbrandon.potter@amd.com        // works well enough. The guarantee is provided by the msync system
128611886Sbrandon.potter@amd.com        // call. We could force the change through with shared mappings with
128711886Sbrandon.potter@amd.com        // a call to msync, but that again would require more information
128811886Sbrandon.potter@amd.com        // than we currently maintain.
128911886Sbrandon.potter@amd.com        warn("mmap: writing to shared mmap region is currently "
129011886Sbrandon.potter@amd.com             "unsupported. The write succeeds on the target, but it "
129111886Sbrandon.potter@amd.com             "will not be propagated to the host or shared mappings");
129211886Sbrandon.potter@amd.com    }
129311886Sbrandon.potter@amd.com
129411886Sbrandon.potter@amd.com    length = roundUp(length, TheISA::PageBytes);
129511886Sbrandon.potter@amd.com
129611886Sbrandon.potter@amd.com    int sim_fd = -1;
129711886Sbrandon.potter@amd.com    uint8_t *pmap = nullptr;
129811886Sbrandon.potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) {
129911886Sbrandon.potter@amd.com        // Check for EmulatedDriver mmap
130011886Sbrandon.potter@amd.com        FDEntry *fde = p->getFDEntry(tgt_fd);
130111886Sbrandon.potter@amd.com        if (fde == NULL)
130211886Sbrandon.potter@amd.com            return -EBADF;
130311886Sbrandon.potter@amd.com
130411886Sbrandon.potter@amd.com        if (fde->driver != NULL) {
130511886Sbrandon.potter@amd.com            return fde->driver->mmap(p, tc, start, length, prot,
130611886Sbrandon.potter@amd.com                                     tgt_flags, tgt_fd, offset);
130711886Sbrandon.potter@amd.com        }
130811886Sbrandon.potter@amd.com        sim_fd = fde->fd;
130911886Sbrandon.potter@amd.com
131011886Sbrandon.potter@amd.com        if (sim_fd < 0)
131111886Sbrandon.potter@amd.com            return -EBADF;
131211886Sbrandon.potter@amd.com
131311886Sbrandon.potter@amd.com        pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE,
131411886Sbrandon.potter@amd.com                                    sim_fd, offset);
131511886Sbrandon.potter@amd.com
131611886Sbrandon.potter@amd.com        if (pmap == (decltype(pmap))-1) {
131711886Sbrandon.potter@amd.com            warn("mmap: failed to map file into host address space");
131811886Sbrandon.potter@amd.com            return -errno;
131911886Sbrandon.potter@amd.com        }
132011886Sbrandon.potter@amd.com    }
132111886Sbrandon.potter@amd.com
132211911SBrandon.Potter@amd.com    // Extend global mmap region if necessary. Note that we ignore the
132311911SBrandon.Potter@amd.com    // start address unless MAP_FIXED is specified.
132411911SBrandon.Potter@amd.com    if (!(tgt_flags & OS::TGT_MAP_FIXED)) {
132511911SBrandon.Potter@amd.com        start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end;
132611911SBrandon.Potter@amd.com        p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length;
132711911SBrandon.Potter@amd.com    }
132811911SBrandon.Potter@amd.com
132911886Sbrandon.potter@amd.com    DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n",
133011886Sbrandon.potter@amd.com                    start, start + length - 1);
133111886Sbrandon.potter@amd.com
133211886Sbrandon.potter@amd.com    // We only allow mappings to overwrite existing mappings if
133311886Sbrandon.potter@amd.com    // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem
133411886Sbrandon.potter@amd.com    // because we ignore the start hint if TGT_MAP_FIXED is not set.
133511886Sbrandon.potter@amd.com    int clobber = tgt_flags & OS::TGT_MAP_FIXED;
133611886Sbrandon.potter@amd.com    if (clobber) {
133711886Sbrandon.potter@amd.com        for (auto tc : p->system->threadContexts) {
133811886Sbrandon.potter@amd.com            // If we might be overwriting old mappings, we need to
133911886Sbrandon.potter@amd.com            // invalidate potentially stale mappings out of the TLBs.
134011886Sbrandon.potter@amd.com            tc->getDTBPtr()->flushAll();
134111886Sbrandon.potter@amd.com            tc->getITBPtr()->flushAll();
134211886Sbrandon.potter@amd.com        }
134311886Sbrandon.potter@amd.com    }
134411886Sbrandon.potter@amd.com
134511886Sbrandon.potter@amd.com    // Allocate physical memory and map it in. If the page table is already
134611886Sbrandon.potter@amd.com    // mapped and clobber is not set, the simulator will issue throw a
134711886Sbrandon.potter@amd.com    // fatal and bail out of the simulation.
134811886Sbrandon.potter@amd.com    p->allocateMem(start, length, clobber);
134911886Sbrandon.potter@amd.com
135011886Sbrandon.potter@amd.com    // Transfer content into target address space.
135111886Sbrandon.potter@amd.com    SETranslatingPortProxy &tp = tc->getMemProxy();
135211886Sbrandon.potter@amd.com    if (tgt_flags & OS::TGT_MAP_ANONYMOUS) {
135311886Sbrandon.potter@amd.com        // In general, we should zero the mapped area for anonymous mappings,
135411886Sbrandon.potter@amd.com        // with something like:
135512426Sqtt2@cornell.edu        //     tp.memsetBlob(start, 0, length);
135611886Sbrandon.potter@amd.com        // However, given that we don't support sparse mappings, and
135711886Sbrandon.potter@amd.com        // some applications can map a couple of gigabytes of space
135811886Sbrandon.potter@amd.com        // (intending sparse usage), that can get painfully expensive.
135911886Sbrandon.potter@amd.com        // Fortunately, since we don't properly implement munmap either,
136011886Sbrandon.potter@amd.com        // there's no danger of remapping used memory, so for now all
136111886Sbrandon.potter@amd.com        // newly mapped memory should already be zeroed so we can skip it.
136211886Sbrandon.potter@amd.com    } else {
136311886Sbrandon.potter@amd.com        // It is possible to mmap an area larger than a file, however
136411886Sbrandon.potter@amd.com        // accessing unmapped portions the system triggers a "Bus error"
136511886Sbrandon.potter@amd.com        // on the host. We must know when to stop copying the file from
136611886Sbrandon.potter@amd.com        // the host into the target address space.
136711886Sbrandon.potter@amd.com        struct stat file_stat;
136811886Sbrandon.potter@amd.com        if (fstat(sim_fd, &file_stat) > 0)
136911886Sbrandon.potter@amd.com            fatal("mmap: cannot stat file");
137011886Sbrandon.potter@amd.com
137111886Sbrandon.potter@amd.com        // Copy the portion of the file that is resident. This requires
137211886Sbrandon.potter@amd.com        // checking both the mmap size and the filesize that we are
137311886Sbrandon.potter@amd.com        // trying to mmap into this space; the mmap size also depends
137411886Sbrandon.potter@amd.com        // on the specified offset into the file.
137511886Sbrandon.potter@amd.com        uint64_t size = std::min((uint64_t)file_stat.st_size - offset,
137611886Sbrandon.potter@amd.com                                 length);
137711886Sbrandon.potter@amd.com        tp.writeBlob(start, pmap, size);
137811886Sbrandon.potter@amd.com
137911886Sbrandon.potter@amd.com        // Cleanup the mmap region before exiting this function.
138011886Sbrandon.potter@amd.com        munmap(pmap, length);
138111886Sbrandon.potter@amd.com
138211886Sbrandon.potter@amd.com        // Maintain the symbol table for dynamic executables.
13831706SN/A        // The loader will call mmap to map the images into its address
13841706SN/A        // space and we intercept that here. We can verify that we are
13851706SN/A        // executing inside the loader by checking the program counter value.
13861706SN/A        // XXX: with multiprogrammed workloads or multi-node configurations,
138711856Sbrandon.potter@amd.com        // this will not work since there is a single global symbol table.
13881706SN/A        ObjectFile *interpreter = p->getInterpreter();
13896701Sgblack@eecs.umich.edu        if (interpreter) {
139011856Sbrandon.potter@amd.com            Addr text_start = interpreter->textBase();
139111856Sbrandon.potter@amd.com            Addr text_end = text_start + interpreter->textSize();
13921706SN/A
139311856Sbrandon.potter@amd.com            Addr pc = tc->pcState().pc();
139411856Sbrandon.potter@amd.com
13951706SN/A            if (pc >= text_start && pc < text_end) {
139611856Sbrandon.potter@amd.com                FDEntry *fde = p->getFDEntry(tgt_fd);
13971706SN/A
13981706SN/A                ObjectFile *lib = createObjectFile(fde->filename);
139910931Sbrandon.potter@amd.com
14001706SN/A                if (lib) {
14011706SN/A                    lib->loadAllSymbols(debugSymbolTable,
14022218SN/A                                        lib->textBase(), start);
14031706SN/A                }
140411759Sbrandon.potter@amd.com            }
14051706SN/A        }
14061706SN/A
14071706SN/A        // Note that we do not zero out the remainder of the mapping. This
14081706SN/A        // is done by a real system, but it probably will not affect
14091706SN/A        // execution (hopefully).
14101999SN/A    }
14111999SN/A
14121999SN/A    return start;
141311856Sbrandon.potter@amd.com}
14141999SN/A
14156701Sgblack@eecs.umich.edutemplate <class OS>
141611856Sbrandon.potter@amd.comSyscallReturn
141710931Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
141811856Sbrandon.potter@amd.com{
141911856Sbrandon.potter@amd.com    int index = 0;
14201999SN/A    int tgt_fd = p->getSyscallArg(tc, index);
142111856Sbrandon.potter@amd.com    Addr bufPtr = p->getSyscallArg(tc, index);
14221999SN/A    int nbytes = p->getSyscallArg(tc, index);
142311856Sbrandon.potter@amd.com    int offset = p->getSyscallArg(tc, index);
142411856Sbrandon.potter@amd.com
142511856Sbrandon.potter@amd.com    int sim_fd = p->getSimFD(tgt_fd);
14261999SN/A    if (sim_fd < 0)
14276227Snate@binkert.org        return -EBADF;
14281999SN/A
14292461SN/A    BufferArg bufArg(bufPtr, nbytes);
143011856Sbrandon.potter@amd.com    bufArg.copyIn(tc->getMemProxy());
143111856Sbrandon.potter@amd.com
14328737Skoansin.tan@gmail.com    int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset);
14331999SN/A
143411856Sbrandon.potter@amd.com    return (bytes_written == -1) ? -errno : bytes_written;
143511856Sbrandon.potter@amd.com}
14361999SN/A
14371999SN/A/// Target mmap() handler.
143810931Sbrandon.potter@amd.comtemplate <class OS>
14391999SN/ASyscallReturn
14406227Snate@binkert.orgmmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
14411999SN/A{
14421999SN/A    return mmapImpl<OS>(desc, num, p, tc, false);
14431999SN/A}
14442218SN/A
14451999SN/A/// Target mmap2() handler.
144610629Sjthestness@gmail.comtemplate <class OS>
14471999SN/ASyscallReturn
14481999SN/Ammap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
144911385Sbrandon.potter@amd.com{
1450360SN/A    return mmapImpl<OS>(desc, num, p, tc, true);
14511450SN/A}
145211851Sbrandon.potter@amd.com
145311385Sbrandon.potter@amd.com/// Target getrlimit() handler.
1454360SN/Atemplate <class OS>
14556701Sgblack@eecs.umich.eduSyscallReturn
14566701Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, Process *process,
14576701Sgblack@eecs.umich.edu              ThreadContext *tc)
145811383Sbrandon.potter@amd.com{
145911383Sbrandon.potter@amd.com    int index = 0;
14608324Ssteve.reinhardt@amd.com    unsigned resource = process->getSyscallArg(tc, index);
146110486Stjablin@gmail.com    TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index));
1462360SN/A
146311385Sbrandon.potter@amd.com    switch (resource) {
146411385Sbrandon.potter@amd.com        case OS::TGT_RLIMIT_STACK:
14659008Sgblack@eecs.umich.edu            // max stack size in bytes: make up a number (8MB for now)
146611383Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024;
146711383Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
146811383Sbrandon.potter@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
146911383Sbrandon.potter@amd.com            break;
147011383Sbrandon.potter@amd.com
147111383Sbrandon.potter@amd.com        case OS::TGT_RLIMIT_DATA:
147211383Sbrandon.potter@amd.com            // max data segment size in bytes: make up a number
147311383Sbrandon.potter@amd.com            rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024;
147411383Sbrandon.potter@amd.com            rlp->rlim_cur = TheISA::htog(rlp->rlim_cur);
14758324Ssteve.reinhardt@amd.com            rlp->rlim_max = TheISA::htog(rlp->rlim_max);
147611383Sbrandon.potter@amd.com            break;
147711383Sbrandon.potter@amd.com
147811383Sbrandon.potter@amd.com        default:
147911383Sbrandon.potter@amd.com            warn("getrlimit: unimplemented resource %d", resource);
148011383Sbrandon.potter@amd.com            return -EINVAL;
148111383Sbrandon.potter@amd.com            break;
148211383Sbrandon.potter@amd.com    }
148311383Sbrandon.potter@amd.com
148411383Sbrandon.potter@amd.com    rlp.copyOut(tc->getMemProxy());
148511383Sbrandon.potter@amd.com    return 0;
148611383Sbrandon.potter@amd.com}
148711383Sbrandon.potter@amd.com
148811383Sbrandon.potter@amd.com/// Target clock_gettime() function.
148911383Sbrandon.potter@amd.comtemplate <class OS>
149011383Sbrandon.potter@amd.comSyscallReturn
149111383Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
149211383Sbrandon.potter@amd.com{
149311383Sbrandon.potter@amd.com    int index = 1;
149411383Sbrandon.potter@amd.com    //int clk_id = p->getSyscallArg(tc, index);
149511383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
149611383Sbrandon.potter@amd.com
149711383Sbrandon.potter@amd.com    getElapsedTimeNano(tp->tv_sec, tp->tv_nsec);
149811383Sbrandon.potter@amd.com    tp->tv_sec += seconds_since_epoch;
149911383Sbrandon.potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
15008324Ssteve.reinhardt@amd.com    tp->tv_nsec = TheISA::htog(tp->tv_nsec);
15015877Shsul@eecs.umich.edu
150210486Stjablin@gmail.com    tp.copyOut(tc->getMemProxy());
150310486Stjablin@gmail.com
150411383Sbrandon.potter@amd.com    return 0;
150511383Sbrandon.potter@amd.com}
150611383Sbrandon.potter@amd.com
150711856Sbrandon.potter@amd.com/// Target clock_getres() function.
150811624Smichael.lebeane@amd.comtemplate <class OS>
150911856Sbrandon.potter@amd.comSyscallReturn
151011856Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc)
151111856Sbrandon.potter@amd.com{
151211856Sbrandon.potter@amd.com    int index = 1;
151311624Smichael.lebeane@amd.com    TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index));
151411624Smichael.lebeane@amd.com
151511624Smichael.lebeane@amd.com    // Set resolution at ns, which is what clock_gettime() returns
151611856Sbrandon.potter@amd.com    tp->tv_sec = 0;
151711856Sbrandon.potter@amd.com    tp->tv_nsec = 1;
151811383Sbrandon.potter@amd.com
151911856Sbrandon.potter@amd.com    tp.copyOut(tc->getMemProxy());
1520360SN/A
152111913SBrandon.Potter@amd.com    return 0;
152211383Sbrandon.potter@amd.com}
15238600Ssteve.reinhardt@amd.com
152411383Sbrandon.potter@amd.com/// Target gettimeofday() handler.
152511383Sbrandon.potter@amd.comtemplate <class OS>
152611383Sbrandon.potter@amd.comSyscallReturn
15278600Ssteve.reinhardt@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process,
15282544SN/A                 ThreadContext *tc)
15292544SN/A{
153011383Sbrandon.potter@amd.com    int index = 0;
153111383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index));
153211383Sbrandon.potter@amd.com
153311905SBrandon.Potter@amd.com    getElapsedTimeMicro(tp->tv_sec, tp->tv_usec);
153411905SBrandon.Potter@amd.com    tp->tv_sec += seconds_since_epoch;
153511905SBrandon.Potter@amd.com    tp->tv_sec = TheISA::htog(tp->tv_sec);
153611905SBrandon.Potter@amd.com    tp->tv_usec = TheISA::htog(tp->tv_usec);
153711905SBrandon.Potter@amd.com
153811905SBrandon.Potter@amd.com    tp.copyOut(tc->getMemProxy());
153911905SBrandon.Potter@amd.com
154011383Sbrandon.potter@amd.com    return 0;
154111383Sbrandon.potter@amd.com}
154211383Sbrandon.potter@amd.com
154311383Sbrandon.potter@amd.com
154411383Sbrandon.potter@amd.com/// Target utimes() handler.
154511383Sbrandon.potter@amd.comtemplate <class OS>
154611383Sbrandon.potter@amd.comSyscallReturn
154711383Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process,
154811383Sbrandon.potter@amd.com           ThreadContext *tc)
154911383Sbrandon.potter@amd.com{
155011383Sbrandon.potter@amd.com    std::string path;
155111383Sbrandon.potter@amd.com
155211383Sbrandon.potter@amd.com    int index = 0;
155311383Sbrandon.potter@amd.com    if (!tc->getMemProxy().tryReadString(path,
155411383Sbrandon.potter@amd.com                process->getSyscallArg(tc, index))) {
15558600Ssteve.reinhardt@amd.com        return -EFAULT;
15566672Sgblack@eecs.umich.edu    }
15578600Ssteve.reinhardt@amd.com
155811383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::timeval [2]>
155911383Sbrandon.potter@amd.com        tp(process->getSyscallArg(tc, index));
156011383Sbrandon.potter@amd.com    tp.copyIn(tc->getMemProxy());
15618601Ssteve.reinhardt@amd.com
15622544SN/A    struct timeval hostTimeval[2];
156311383Sbrandon.potter@amd.com    for (int i = 0; i < 2; ++i)
156411383Sbrandon.potter@amd.com    {
156511383Sbrandon.potter@amd.com        hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec);
156611383Sbrandon.potter@amd.com        hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec);
156711383Sbrandon.potter@amd.com    }
156811383Sbrandon.potter@amd.com
156911383Sbrandon.potter@amd.com    // Adjust path for current working directory
157011383Sbrandon.potter@amd.com    path = process->fullPath(path);
157111383Sbrandon.potter@amd.com
157211383Sbrandon.potter@amd.com    int result = utimes(path.c_str(), hostTimeval);
157311383Sbrandon.potter@amd.com
157411383Sbrandon.potter@amd.com    if (result < 0)
157511383Sbrandon.potter@amd.com        return -errno;
157611383Sbrandon.potter@amd.com
157711383Sbrandon.potter@amd.com    return 0;
157811383Sbrandon.potter@amd.com}
157911383Sbrandon.potter@amd.com/// Target getrusage() function.
158011383Sbrandon.potter@amd.comtemplate <class OS>
158111383Sbrandon.potter@amd.comSyscallReturn
158211383Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process,
158311383Sbrandon.potter@amd.com              ThreadContext *tc)
158411383Sbrandon.potter@amd.com{
158511383Sbrandon.potter@amd.com    int index = 0;
158611383Sbrandon.potter@amd.com    int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN
158711383Sbrandon.potter@amd.com    TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index));
158811383Sbrandon.potter@amd.com
158911383Sbrandon.potter@amd.com    rup->ru_utime.tv_sec = 0;
159011383Sbrandon.potter@amd.com    rup->ru_utime.tv_usec = 0;
159111383Sbrandon.potter@amd.com    rup->ru_stime.tv_sec = 0;
159211383Sbrandon.potter@amd.com    rup->ru_stime.tv_usec = 0;
159311383Sbrandon.potter@amd.com    rup->ru_maxrss = 0;
159411383Sbrandon.potter@amd.com    rup->ru_ixrss = 0;
159511392Sbrandon.potter@amd.com    rup->ru_idrss = 0;
159611392Sbrandon.potter@amd.com    rup->ru_isrss = 0;
159711392Sbrandon.potter@amd.com    rup->ru_minflt = 0;
159811392Sbrandon.potter@amd.com    rup->ru_majflt = 0;
159911392Sbrandon.potter@amd.com    rup->ru_nswap = 0;
160011392Sbrandon.potter@amd.com    rup->ru_inblock = 0;
160111392Sbrandon.potter@amd.com    rup->ru_oublock = 0;
160211392Sbrandon.potter@amd.com    rup->ru_msgsnd = 0;
160311392Sbrandon.potter@amd.com    rup->ru_msgrcv = 0;
160411392Sbrandon.potter@amd.com    rup->ru_nsignals = 0;
160511392Sbrandon.potter@amd.com    rup->ru_nvcsw = 0;
160611392Sbrandon.potter@amd.com    rup->ru_nivcsw = 0;
160711392Sbrandon.potter@amd.com
160811392Sbrandon.potter@amd.com    switch (who) {
160911856Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_SELF:
161011856Sbrandon.potter@amd.com        getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec);
161111856Sbrandon.potter@amd.com        rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec);
161211392Sbrandon.potter@amd.com        rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec);
161311392Sbrandon.potter@amd.com        break;
161411392Sbrandon.potter@amd.com
161511392Sbrandon.potter@amd.com      case OS::TGT_RUSAGE_CHILDREN:
161611392Sbrandon.potter@amd.com        // do nothing.  We have no child processes, so they take no time.
161711392Sbrandon.potter@amd.com        break;
161811392Sbrandon.potter@amd.com
161911392Sbrandon.potter@amd.com      default:
162011383Sbrandon.potter@amd.com        // don't really handle THREAD or CHILDREN, but just warn and
162111383Sbrandon.potter@amd.com        // plow ahead
162211383Sbrandon.potter@amd.com        warn("getrusage() only supports RUSAGE_SELF.  Parameter %d ignored.",
162311383Sbrandon.potter@amd.com             who);
162411383Sbrandon.potter@amd.com    }
16251458SN/A
1626360SN/A    rup.copyOut(tc->getMemProxy());
1627360SN/A
162811593Santhony.gutierrez@amd.com    return 0;
162911593Santhony.gutierrez@amd.com}
163011851Sbrandon.potter@amd.com
163111593Santhony.gutierrez@amd.com/// Target times() function.
163211593Santhony.gutierrez@amd.comtemplate <class OS>
163311593Santhony.gutierrez@amd.comSyscallReturn
163411593Santhony.gutierrez@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process,
163511593Santhony.gutierrez@amd.com          ThreadContext *tc)
163611593Santhony.gutierrez@amd.com{
163711593Santhony.gutierrez@amd.com    int index = 0;
163811856Sbrandon.potter@amd.com    TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index));
163911856Sbrandon.potter@amd.com
164011593Santhony.gutierrez@amd.com    // Fill in the time structure (in clocks)
164111856Sbrandon.potter@amd.com    int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s;
164211593Santhony.gutierrez@amd.com    bufp->tms_utime = clocks;
164311593Santhony.gutierrez@amd.com    bufp->tms_stime = 0;
164411593Santhony.gutierrez@amd.com    bufp->tms_cutime = 0;
164511593Santhony.gutierrez@amd.com    bufp->tms_cstime = 0;
164611594Santhony.gutierrez@amd.com
164711593Santhony.gutierrez@amd.com    // Convert to host endianness
164811593Santhony.gutierrez@amd.com    bufp->tms_utime = TheISA::htog(bufp->tms_utime);
164911593Santhony.gutierrez@amd.com
165011593Santhony.gutierrez@amd.com    // Write back
165111385Sbrandon.potter@amd.com    bufp.copyOut(tc->getMemProxy());
165211385Sbrandon.potter@amd.com
165311385Sbrandon.potter@amd.com    // Return clock ticks since system boot
165411851Sbrandon.potter@amd.com    return clocks;
165511385Sbrandon.potter@amd.com}
165611385Sbrandon.potter@amd.com
165711385Sbrandon.potter@amd.com/// Target time() function.
165811385Sbrandon.potter@amd.comtemplate <class OS>
165911385Sbrandon.potter@amd.comSyscallReturn
166011385Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc)
166111385Sbrandon.potter@amd.com{
166211851Sbrandon.potter@amd.com    typename OS::time_t sec, usec;
166311385Sbrandon.potter@amd.com    getElapsedTimeMicro(sec, usec);
166411385Sbrandon.potter@amd.com    sec += seconds_since_epoch;
166511385Sbrandon.potter@amd.com
166611385Sbrandon.potter@amd.com    int index = 0;
1667378SN/A    Addr taddr = (Addr)process->getSyscallArg(tc, index);
1668360SN/A    if (taddr != 0) {
16691450SN/A        typename OS::time_t t = sec;
167011851Sbrandon.potter@amd.com        t = TheISA::htog(t);
167111851Sbrandon.potter@amd.com        SETranslatingPortProxy &p = tc->getMemProxy();
1672360SN/A        p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t));
16736701Sgblack@eecs.umich.edu    }
16746701Sgblack@eecs.umich.edu    return sec;
16756701Sgblack@eecs.umich.edu}
1676360SN/A
1677360SN/A
167811906SBrandon.Potter@amd.com#endif // __SIM_SYSCALL_EMUL_HH__
167911906SBrandon.Potter@amd.com