syscall_emul.hh revision 11758
1360SN/A/* 210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 410027SChris.Adeniyi-Jones@arm.com * All rights reserved 510027SChris.Adeniyi-Jones@arm.com * 610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall 710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual 810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating 910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software 1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder. You may use the software subject to the license 1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated 1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software, 1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form. 1410027SChris.Adeniyi-Jones@arm.com * 151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 16360SN/A * All rights reserved. 17360SN/A * 18360SN/A * Redistribution and use in source and binary forms, with or without 19360SN/A * modification, are permitted provided that the following conditions are 20360SN/A * met: redistributions of source code must retain the above copyright 21360SN/A * notice, this list of conditions and the following disclaimer; 22360SN/A * redistributions in binary form must reproduce the above copyright 23360SN/A * notice, this list of conditions and the following disclaimer in the 24360SN/A * documentation and/or other materials provided with the distribution; 25360SN/A * neither the name of the copyright holders nor the names of its 26360SN/A * contributors may be used to endorse or promote products derived from 27360SN/A * this software without specific prior written permission. 28360SN/A * 29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 422665Ssaidi@eecs.umich.edu * Kevin Lim 43360SN/A */ 44360SN/A 451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 461354SN/A#define __SIM_SYSCALL_EMUL_HH__ 47360SN/A 482764Sstever@eecs.umich.edu#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \ 499202Spalle@lyckegaard.dk defined(__FreeBSD__) || defined(__CYGWIN__) || \ 509202Spalle@lyckegaard.dk defined(__NetBSD__)) 512064SN/A 52360SN/A/// 53360SN/A/// @file syscall_emul.hh 54360SN/A/// 55360SN/A/// This file defines objects used to emulate syscalls from the target 56360SN/A/// application on the host machine. 57360SN/A 581809SN/A#ifdef __CYGWIN32__ 595543Ssaidi@eecs.umich.edu#include <sys/fcntl.h> // for O_BINARY 6011392Sbrandon.potter@amd.com 611809SN/A#endif 6211392Sbrandon.potter@amd.com#include <fcntl.h> 6311383Sbrandon.potter@amd.com#include <sys/mman.h> 643113Sgblack@eecs.umich.edu#include <sys/stat.h> 658229Snate@binkert.org#include <sys/time.h> 668229Snate@binkert.org#include <sys/uio.h> 6711594Santhony.gutierrez@amd.com#include <unistd.h> 687075Snate@binkert.org 698229Snate@binkert.org#include <cerrno> 707075Snate@binkert.org#include <string> 71360SN/A 722474SN/A#include "base/chunk_generator.hh" 735543Ssaidi@eecs.umich.edu#include "base/intmath.hh" // for RoundUp 7411392Sbrandon.potter@amd.com#include "base/loader/object_file.hh" 752462SN/A#include "base/misc.hh" 761354SN/A#include "base/trace.hh" 776216Snate@binkert.org#include "base/types.hh" 786658Snate@binkert.org#include "config/the_isa.hh" 792474SN/A#include "cpu/base.hh" 802680Sktlim@umich.edu#include "cpu/thread_context.hh" 8111380Salexandru.dutu@amd.com#include "debug/SyscallBase.hh" 828232Snate@binkert.org#include "debug/SyscallVerbose.hh" 838229Snate@binkert.org#include "mem/page_table.hh" 847678Sgblack@eecs.umich.edu#include "sim/byteswap.hh" 8510496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh" 868229Snate@binkert.org#include "sim/process.hh" 8710497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh" 888766Sgblack@eecs.umich.edu#include "sim/syscallreturn.hh" 896640Svince@csl.cornell.edu#include "sim/system.hh" 90360SN/A 9111380Salexandru.dutu@amd.com// This wrapper macro helps out with readability a bit. FLAGEXT specifies 9211380Salexandru.dutu@amd.com// the verbosity and FMT is the message to be appended to the syscall 9311380Salexandru.dutu@amd.com// header information. The syscall header information contains the cpuid 9411380Salexandru.dutu@amd.com// and thread id. 9511380Salexandru.dutu@amd.com#define DPRINTF_SYSCALL(FLAGEXT, FMT, ...) \ 9611380Salexandru.dutu@amd.com DPRINTFS(Syscall##FLAGEXT, tc->getCpuPtr(), "T%d : syscall " FMT, \ 9711380Salexandru.dutu@amd.com tc->threadId(), __VA_ARGS__) 9811380Salexandru.dutu@amd.com 99360SN/A/// 100360SN/A/// System call descriptor. 101360SN/A/// 102360SN/Aclass SyscallDesc { 103360SN/A 104360SN/A public: 105360SN/A 106378SN/A /// Typedef for target syscall handler functions. 1071450SN/A typedef SyscallReturn (*FuncPtr)(SyscallDesc *, int num, 1083114Sgblack@eecs.umich.edu LiveProcess *, ThreadContext *); 109360SN/A 1105543Ssaidi@eecs.umich.edu const char *name; //!< Syscall name (e.g., "open"). 1115543Ssaidi@eecs.umich.edu FuncPtr funcPtr; //!< Pointer to emulation function. 1125543Ssaidi@eecs.umich.edu int flags; //!< Flags (see Flags enum). 11310831Ssteve.reinhardt@amd.com bool warned; //!< Have we warned about unimplemented syscall? 114360SN/A 115360SN/A /// Flag values for controlling syscall behavior. 116360SN/A enum Flags { 117360SN/A /// Don't set return regs according to funcPtr return value. 118360SN/A /// Used for syscalls with non-standard return conventions 1192680Sktlim@umich.edu /// that explicitly set the ThreadContext regs (e.g., 120360SN/A /// sigreturn). 12110831Ssteve.reinhardt@amd.com SuppressReturnValue = 1, 12210831Ssteve.reinhardt@amd.com WarnOnce = 2 123360SN/A }; 124360SN/A 125360SN/A /// Constructor. 126360SN/A SyscallDesc(const char *_name, FuncPtr _funcPtr, int _flags = 0) 12710831Ssteve.reinhardt@amd.com : name(_name), funcPtr(_funcPtr), flags(_flags), warned(false) 128360SN/A { 129360SN/A } 130360SN/A 131360SN/A /// Emulate the syscall. Public interface for calling through funcPtr. 1323114Sgblack@eecs.umich.edu void doSyscall(int callnum, LiveProcess *proc, ThreadContext *tc); 13310831Ssteve.reinhardt@amd.com 13410831Ssteve.reinhardt@amd.com /// Is the WarnOnce flag set? 13510831Ssteve.reinhardt@amd.com bool warnOnce() const { return (flags & WarnOnce); } 136360SN/A}; 137360SN/A 138360SN/A 139360SN/A////////////////////////////////////////////////////////////////////// 140360SN/A// 141360SN/A// The following emulation functions are generic enough that they 142360SN/A// don't need to be recompiled for different emulated OS's. They are 143360SN/A// defined in sim/syscall_emul.cc. 144360SN/A// 145360SN/A////////////////////////////////////////////////////////////////////// 146360SN/A 147360SN/A 148378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1491706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1503114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 151378SN/A 152378SN/A/// Handler for unimplemented syscalls that we never intend to 153378SN/A/// implement (signal handling, etc.) and should not affect the correct 154378SN/A/// behavior of the program. Print a warning only if the appropriate 155378SN/A/// trace flag is enabled. Return success to the target program. 1561706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1573114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 158360SN/A 1596109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1601706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1613114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 162378SN/A 1636109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1646109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1656109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 1666109Ssanchezd@stanford.edu 167378SN/A/// Target getpagesize() handler. 1681706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1693114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 170378SN/A 1715748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 1725748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 1735748SSteve.Reinhardt@amd.com LiveProcess *p, ThreadContext *tc); 174378SN/A 175378SN/A/// Target close() handler. 1761706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 1773114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 178378SN/A 179378SN/A/// Target read() handler. 1801706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 1813114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 182378SN/A 183378SN/A/// Target write() handler. 1841706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 1853114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 186378SN/A 187378SN/A/// Target lseek() handler. 1881706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 1893114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 190378SN/A 1914118Sgblack@eecs.umich.edu/// Target _llseek() handler. 1924118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1934118Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 1944118Sgblack@eecs.umich.edu 195378SN/A/// Target munmap() handler. 1961706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 1973114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 198378SN/A 199378SN/A/// Target gethostname() handler. 2001706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 2013114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 202360SN/A 2035513SMichael.Adler@intel.com/// Target getcwd() handler. 2045513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 2055513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2065513SMichael.Adler@intel.com 20710203SAli.Saidi@ARM.com/// Target readlink() handler. 20810203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 20910203SAli.Saidi@ARM.com LiveProcess *p, ThreadContext *tc, 21010203SAli.Saidi@ARM.com int index = 0); 2115513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2125513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2135513SMichael.Adler@intel.com 214511SN/A/// Target unlink() handler. 21510633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 21610633Smichaelupton@gmail.com LiveProcess *p, ThreadContext *tc, 21710633Smichaelupton@gmail.com int index); 2181706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2193114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 220511SN/A 2215513SMichael.Adler@intel.com/// Target mkdir() handler. 2225513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2235513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2245513SMichael.Adler@intel.com 225511SN/A/// Target rename() handler. 2261706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2273114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2281706SN/A 2291706SN/A 2301706SN/A/// Target truncate() handler. 2311706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2323114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2331706SN/A 2341706SN/A 2351706SN/A/// Target ftruncate() handler. 2361706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2373114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2381706SN/A 239511SN/A 2406703Svince@csl.cornell.edu/// Target truncate64() handler. 2416703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2426703Svince@csl.cornell.edu LiveProcess *p, ThreadContext *tc); 2436703Svince@csl.cornell.edu 2446685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2456685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2466685Stjones1@inf.ed.ac.uk LiveProcess *p, ThreadContext *tc); 2476685Stjones1@inf.ed.ac.uk 2486685Stjones1@inf.ed.ac.uk 2495513SMichael.Adler@intel.com/// Target umask() handler. 2505513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2515513SMichael.Adler@intel.com LiveProcess *p, ThreadContext *tc); 2525513SMichael.Adler@intel.com 2535513SMichael.Adler@intel.com 2541999SN/A/// Target chown() handler. 2551999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2563114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2571999SN/A 2581999SN/A 2591999SN/A/// Target fchown() handler. 2601999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2613114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2621999SN/A 2633079Sstever@eecs.umich.edu/// Target dup() handler. 2643079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 2653114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2663079Sstever@eecs.umich.edu 2672093SN/A/// Target fnctl() handler. 2682093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2693114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2702093SN/A 2712687Sksewell@umich.edu/// Target fcntl64() handler. 2722687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2733114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 2742687Sksewell@umich.edu 2752238SN/A/// Target setuid() handler. 2762238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2773114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2782238SN/A 2792238SN/A/// Target getpid() handler. 2802238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 2813114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2822238SN/A 2832238SN/A/// Target getuid() handler. 2842238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 2853114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2862238SN/A 2872238SN/A/// Target getgid() handler. 2882238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 2893114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2902238SN/A 2912238SN/A/// Target getppid() handler. 2922238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 2933114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2942238SN/A 2952238SN/A/// Target geteuid() handler. 2962238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2973114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 2982238SN/A 2992238SN/A/// Target getegid() handler. 3002238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3013114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 3022238SN/A 3036109Ssanchezd@stanford.edu/// Target clone() handler. 3046109Ssanchezd@stanford.eduSyscallReturn cloneFunc(SyscallDesc *desc, int num, 3056109Ssanchezd@stanford.edu LiveProcess *p, ThreadContext *tc); 3062238SN/A 3079455Smitch.hayenga+gem5@gmail.com/// Target access() handler 3089455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 3099455Smitch.hayenga+gem5@gmail.com LiveProcess *p, ThreadContext *tc); 31010203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 31110203SAli.Saidi@ARM.com LiveProcess *p, ThreadContext *tc, 31210203SAli.Saidi@ARM.com int index); 3139455Smitch.hayenga+gem5@gmail.com 3149112Smarc.orr@gmail.com/// Futex system call 3159112Smarc.orr@gmail.com/// Implemented by Daniel Sanchez 3169112Smarc.orr@gmail.com/// Used by printf's in multi-threaded apps 3179112Smarc.orr@gmail.comtemplate <class OS> 3189112Smarc.orr@gmail.comSyscallReturn 3199112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 3209112Smarc.orr@gmail.com ThreadContext *tc) 3219112Smarc.orr@gmail.com{ 3229112Smarc.orr@gmail.com int index_uaddr = 0; 3239112Smarc.orr@gmail.com int index_op = 1; 3249112Smarc.orr@gmail.com int index_val = 2; 3259112Smarc.orr@gmail.com int index_timeout = 3; 3269112Smarc.orr@gmail.com 3279112Smarc.orr@gmail.com uint64_t uaddr = process->getSyscallArg(tc, index_uaddr); 3289112Smarc.orr@gmail.com int op = process->getSyscallArg(tc, index_op); 3299112Smarc.orr@gmail.com int val = process->getSyscallArg(tc, index_val); 3309112Smarc.orr@gmail.com uint64_t timeout = process->getSyscallArg(tc, index_timeout); 3319112Smarc.orr@gmail.com 3329112Smarc.orr@gmail.com std::map<uint64_t, std::list<ThreadContext *> * > 3339112Smarc.orr@gmail.com &futex_map = tc->getSystemPtr()->futexMap; 3349112Smarc.orr@gmail.com 3359112Smarc.orr@gmail.com DPRINTF(SyscallVerbose, "In sys_futex: Address=%llx, op=%d, val=%d\n", 3369112Smarc.orr@gmail.com uaddr, op, val); 3379112Smarc.orr@gmail.com 3389238Slluc.alvarez@bsc.es op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3399112Smarc.orr@gmail.com 3409112Smarc.orr@gmail.com if (op == OS::TGT_FUTEX_WAIT) { 3419112Smarc.orr@gmail.com if (timeout != 0) { 3429112Smarc.orr@gmail.com warn("sys_futex: FUTEX_WAIT with non-null timeout unimplemented;" 3439112Smarc.orr@gmail.com "we'll wait indefinitely"); 3449112Smarc.orr@gmail.com } 3459112Smarc.orr@gmail.com 3469112Smarc.orr@gmail.com uint8_t *buf = new uint8_t[sizeof(int)]; 3479112Smarc.orr@gmail.com tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int)); 3489112Smarc.orr@gmail.com int mem_val = *((int *)buf); 34911367Sandreas.hansson@arm.com delete[] buf; 3509112Smarc.orr@gmail.com 35111321Ssteve.reinhardt@amd.com if (val != mem_val) { 3529112Smarc.orr@gmail.com DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, read: %d, " 3539112Smarc.orr@gmail.com "expected: %d\n", mem_val, val); 3549112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 3559112Smarc.orr@gmail.com } 3569112Smarc.orr@gmail.com 3579112Smarc.orr@gmail.com // Queue the thread context 3589112Smarc.orr@gmail.com std::list<ThreadContext *> * tcWaitList; 3599112Smarc.orr@gmail.com if (futex_map.count(uaddr)) { 3609112Smarc.orr@gmail.com tcWaitList = futex_map.find(uaddr)->second; 3619112Smarc.orr@gmail.com } else { 3629112Smarc.orr@gmail.com tcWaitList = new std::list<ThreadContext *>(); 3639112Smarc.orr@gmail.com futex_map.insert(std::pair< uint64_t, 3649112Smarc.orr@gmail.com std::list<ThreadContext *> * >(uaddr, tcWaitList)); 3659112Smarc.orr@gmail.com } 3669112Smarc.orr@gmail.com tcWaitList->push_back(tc); 3679112Smarc.orr@gmail.com DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAIT, suspending calling " 3689112Smarc.orr@gmail.com "thread context\n"); 3699112Smarc.orr@gmail.com tc->suspend(); 3709112Smarc.orr@gmail.com return 0; 3719112Smarc.orr@gmail.com } else if (op == OS::TGT_FUTEX_WAKE){ 3729112Smarc.orr@gmail.com int wokenUp = 0; 3739112Smarc.orr@gmail.com std::list<ThreadContext *> * tcWaitList; 3749112Smarc.orr@gmail.com if (futex_map.count(uaddr)) { 3759112Smarc.orr@gmail.com tcWaitList = futex_map.find(uaddr)->second; 3769112Smarc.orr@gmail.com while (tcWaitList->size() > 0 && wokenUp < val) { 3779112Smarc.orr@gmail.com tcWaitList->front()->activate(); 3789112Smarc.orr@gmail.com tcWaitList->pop_front(); 3799112Smarc.orr@gmail.com wokenUp++; 3809112Smarc.orr@gmail.com } 38111321Ssteve.reinhardt@amd.com if (tcWaitList->empty()) { 3829112Smarc.orr@gmail.com futex_map.erase(uaddr); 3839112Smarc.orr@gmail.com delete tcWaitList; 3849112Smarc.orr@gmail.com } 3859112Smarc.orr@gmail.com } 3869112Smarc.orr@gmail.com DPRINTF(SyscallVerbose, "sys_futex: FUTEX_WAKE, activated %d waiting " 3879112Smarc.orr@gmail.com "thread contexts\n", wokenUp); 3889112Smarc.orr@gmail.com return wokenUp; 3899112Smarc.orr@gmail.com } else { 3909238Slluc.alvarez@bsc.es warn("sys_futex: op %d is not implemented, just returning...", op); 3919112Smarc.orr@gmail.com return 0; 3929112Smarc.orr@gmail.com } 3939112Smarc.orr@gmail.com 3949112Smarc.orr@gmail.com} 3959112Smarc.orr@gmail.com 3962238SN/A 3972238SN/A/// Pseudo Funcs - These functions use a different return convension, 3982238SN/A/// returning a second value in a register other than the normal return register 3992238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 4003114Sgblack@eecs.umich.edu LiveProcess *process, ThreadContext *tc); 4012238SN/A 4022238SN/A/// Target getpidPseudo() handler. 4032238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 4043114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 4052238SN/A 4062238SN/A/// Target getuidPseudo() handler. 4072238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 4083114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 4092238SN/A 4102238SN/A/// Target getgidPseudo() handler. 4112238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 4123114Sgblack@eecs.umich.edu LiveProcess *p, ThreadContext *tc); 4132238SN/A 4142238SN/A 4151354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 4161354SN/Aconst int one_million = 1000000; 41710796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 41810796Sbrandon.potter@amd.comconst int one_billion = 1000000000; 4191354SN/A 4201354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 4211354SN/A/// by my reckoning. We want to keep this a constant (not use the 4221354SN/A/// real-world time) to keep simulations repeatable. 4231354SN/Aconst unsigned seconds_since_epoch = 1000000000; 4241354SN/A 4251354SN/A/// Helper function to convert current elapsed time to seconds and 4261354SN/A/// microseconds. 4271354SN/Atemplate <class T1, class T2> 4281354SN/Avoid 42910796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec) 4301354SN/A{ 43110796Sbrandon.potter@amd.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 4321354SN/A sec = elapsed_usecs / one_million; 4331354SN/A usec = elapsed_usecs % one_million; 4341354SN/A} 4351354SN/A 43610796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and 43710796Sbrandon.potter@amd.com/// nanoseconds. 43810796Sbrandon.potter@amd.comtemplate <class T1, class T2> 43910796Sbrandon.potter@amd.comvoid 44010796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec) 44110796Sbrandon.potter@amd.com{ 44210796Sbrandon.potter@amd.com uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 44310796Sbrandon.potter@amd.com sec = elapsed_nsecs / one_billion; 44410796Sbrandon.potter@amd.com nsec = elapsed_nsecs % one_billion; 44510796Sbrandon.potter@amd.com} 44610796Sbrandon.potter@amd.com 447360SN/A////////////////////////////////////////////////////////////////////// 448360SN/A// 449360SN/A// The following emulation functions are generic, but need to be 450360SN/A// templated to account for differences in types, constants, etc. 451360SN/A// 452360SN/A////////////////////////////////////////////////////////////////////// 453360SN/A 4543113Sgblack@eecs.umich.edu#if NO_STAT64 4553113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 4563113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 4573113Sgblack@eecs.umich.edu#else 4583113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 4593113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 4603113Sgblack@eecs.umich.edu#endif 4613113Sgblack@eecs.umich.edu 4623113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 4633113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 4643113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 4653113Sgblack@eecs.umich.edu 4663113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 4673113Sgblack@eecs.umich.edustatic void 4683113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 4693113Sgblack@eecs.umich.edu{ 4704189Sgblack@eecs.umich.edu using namespace TheISA; 4714189Sgblack@eecs.umich.edu 4723113Sgblack@eecs.umich.edu if (fakeTTY) 4733113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 4743113Sgblack@eecs.umich.edu else 4753113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 4768737Skoansin.tan@gmail.com tgt->st_dev = TheISA::htog(tgt->st_dev); 4773113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 4788737Skoansin.tan@gmail.com tgt->st_ino = TheISA::htog(tgt->st_ino); 4793277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4805515SMichael.Adler@intel.com if (fakeTTY) { 4815515SMichael.Adler@intel.com // Claim to be a character device 4825515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4835515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4845515SMichael.Adler@intel.com } 4858737Skoansin.tan@gmail.com tgt->st_mode = TheISA::htog(tgt->st_mode); 4863277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4878737Skoansin.tan@gmail.com tgt->st_nlink = TheISA::htog(tgt->st_nlink); 4883277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4898737Skoansin.tan@gmail.com tgt->st_uid = TheISA::htog(tgt->st_uid); 4903277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4918737Skoansin.tan@gmail.com tgt->st_gid = TheISA::htog(tgt->st_gid); 4923113Sgblack@eecs.umich.edu if (fakeTTY) 4933113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4943113Sgblack@eecs.umich.edu else 4953113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4968737Skoansin.tan@gmail.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 4973113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4988737Skoansin.tan@gmail.com tgt->st_size = TheISA::htog(tgt->st_size); 4993114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 5008737Skoansin.tan@gmail.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 5013114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 5028737Skoansin.tan@gmail.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 5033114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 5048737Skoansin.tan@gmail.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 5054061Sgblack@eecs.umich.edu // Force the block size to be 8k. This helps to ensure buffered io works 5064061Sgblack@eecs.umich.edu // consistently across different hosts. 5074061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 5088737Skoansin.tan@gmail.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 5093113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 5108737Skoansin.tan@gmail.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 5113113Sgblack@eecs.umich.edu} 5123113Sgblack@eecs.umich.edu 5133113Sgblack@eecs.umich.edu// Same for stat64 5143113Sgblack@eecs.umich.edu 5153113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 5163113Sgblack@eecs.umich.edustatic void 5173113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 5183113Sgblack@eecs.umich.edu{ 5194189Sgblack@eecs.umich.edu using namespace TheISA; 5204189Sgblack@eecs.umich.edu 5213113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 5223113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 5233113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 5248737Skoansin.tan@gmail.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 5253113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 5268737Skoansin.tan@gmail.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 5273113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 5288737Skoansin.tan@gmail.com tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 5293113Sgblack@eecs.umich.edu#else 5303113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 5313113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 5323113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 5333113Sgblack@eecs.umich.edu#endif 5343113Sgblack@eecs.umich.edu} 5353113Sgblack@eecs.umich.edu 5363113Sgblack@eecs.umich.edu//Here are a couple convenience functions 5373113Sgblack@eecs.umich.edutemplate<class OS> 5383113Sgblack@eecs.umich.edustatic void 5398852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 5403113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 5413113Sgblack@eecs.umich.edu{ 5423113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5433113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5443113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5453113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5463113Sgblack@eecs.umich.edu} 5473113Sgblack@eecs.umich.edu 5483113Sgblack@eecs.umich.edutemplate<class OS> 5493113Sgblack@eecs.umich.edustatic void 5508852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5513113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5523113Sgblack@eecs.umich.edu{ 5533113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5543113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5556686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5563113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5573113Sgblack@eecs.umich.edu} 5583113Sgblack@eecs.umich.edu 559378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 560378SN/A/// only to find out if their stdout is a tty, to determine whether to 5619141Smarc.orr@gmail.com/// do line or block buffering. We always claim that output fds are 5629141Smarc.orr@gmail.com/// not TTYs to provide repeatable results. 563360SN/Atemplate <class OS> 5641450SN/ASyscallReturn 5653114Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 5662680Sktlim@umich.edu ThreadContext *tc) 567360SN/A{ 5686701Sgblack@eecs.umich.edu int index = 0; 56910930Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 5706701Sgblack@eecs.umich.edu unsigned req = process->getSyscallArg(tc, index); 571360SN/A 57210930Sbrandon.potter@amd.com DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 573360SN/A 57410932Sbrandon.potter@amd.com FDEntry *fde = process->getFDEntry(tgt_fd); 57510496Ssteve.reinhardt@amd.com 57610930Sbrandon.potter@amd.com if (fde == NULL) { 577360SN/A // doesn't map to any simulator fd: not a valid target fd 5781458SN/A return -EBADF; 579360SN/A } 580360SN/A 58110930Sbrandon.potter@amd.com if (fde->driver != NULL) { 58210930Sbrandon.potter@amd.com return fde->driver->ioctl(process, tc, req); 58310496Ssteve.reinhardt@amd.com } 58410496Ssteve.reinhardt@amd.com 5859141Smarc.orr@gmail.com if (OS::isTtyReq(req)) { 5861458SN/A return -ENOTTY; 5879141Smarc.orr@gmail.com } 588360SN/A 5899141Smarc.orr@gmail.com warn("Unsupported ioctl call: ioctl(%d, 0x%x, ...) @ \n", 59010930Sbrandon.potter@amd.com tgt_fd, req, tc->pcState()); 5919141Smarc.orr@gmail.com return -ENOTTY; 592360SN/A} 593360SN/A 594360SN/Atemplate <class OS> 59510027SChris.Adeniyi-Jones@arm.comstatic SyscallReturn 5963114Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 59710027SChris.Adeniyi-Jones@arm.com ThreadContext *tc, int index) 598360SN/A{ 599360SN/A std::string path; 600360SN/A 6018852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 6026701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 6031458SN/A return -EFAULT; 604360SN/A 6056701Sgblack@eecs.umich.edu int tgtFlags = process->getSyscallArg(tc, index); 6066701Sgblack@eecs.umich.edu int mode = process->getSyscallArg(tc, index); 607360SN/A int hostFlags = 0; 608360SN/A 609360SN/A // translate open flags 610360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 611360SN/A if (tgtFlags & OS::openFlagTable[i].tgtFlag) { 612360SN/A tgtFlags &= ~OS::openFlagTable[i].tgtFlag; 613360SN/A hostFlags |= OS::openFlagTable[i].hostFlag; 614360SN/A } 615360SN/A } 616360SN/A 617360SN/A // any target flags left? 618360SN/A if (tgtFlags != 0) 6191706SN/A warn("Syscall: open: cannot decode flags 0x%x", tgtFlags); 620360SN/A 621360SN/A#ifdef __CYGWIN32__ 622360SN/A hostFlags |= O_BINARY; 623360SN/A#endif 624360SN/A 6253669Sbinkertn@umich.edu // Adjust path for current working directory 6263669Sbinkertn@umich.edu path = process->fullPath(path); 6273669Sbinkertn@umich.edu 6281706SN/A DPRINTF(SyscallVerbose, "opening file %s\n", path.c_str()); 6291706SN/A 63010496Ssteve.reinhardt@amd.com if (startswith(path, "/dev/")) { 63110496Ssteve.reinhardt@amd.com std::string filename = path.substr(strlen("/dev/")); 63210496Ssteve.reinhardt@amd.com if (filename == "sysdev0") { 63310496Ssteve.reinhardt@amd.com // This is a memory-mapped high-resolution timer device on Alpha. 63410496Ssteve.reinhardt@amd.com // We don't support it, so just punt. 63510496Ssteve.reinhardt@amd.com warn("Ignoring open(%s, ...)\n", path); 63610496Ssteve.reinhardt@amd.com return -ENOENT; 63710496Ssteve.reinhardt@amd.com } 63810496Ssteve.reinhardt@amd.com 63910496Ssteve.reinhardt@amd.com EmulatedDriver *drv = process->findDriver(filename); 64010496Ssteve.reinhardt@amd.com if (drv != NULL) { 64110496Ssteve.reinhardt@amd.com // the driver's open method will allocate a fd from the 64210496Ssteve.reinhardt@amd.com // process if necessary. 64310496Ssteve.reinhardt@amd.com return drv->open(process, tc, mode, hostFlags); 64410496Ssteve.reinhardt@amd.com } 64510496Ssteve.reinhardt@amd.com 64610496Ssteve.reinhardt@amd.com // fall through here for pass through to host devices, such as 64710496Ssteve.reinhardt@amd.com // /dev/zero 64810496Ssteve.reinhardt@amd.com } 64910496Ssteve.reinhardt@amd.com 6505795Ssaidi@eecs.umich.edu int fd; 6519143Ssteve.reinhardt@amd.com int local_errno; 6529142Ssteve.reinhardt@amd.com if (startswith(path, "/proc/") || startswith(path, "/system/") || 6539142Ssteve.reinhardt@amd.com startswith(path, "/platform/") || startswith(path, "/sys/")) { 6549143Ssteve.reinhardt@amd.com // It's a proc/sys entry and requires special handling 6555795Ssaidi@eecs.umich.edu fd = OS::openSpecialFile(path, process, tc); 6569143Ssteve.reinhardt@amd.com local_errno = ENOENT; 6575795Ssaidi@eecs.umich.edu } else { 6585795Ssaidi@eecs.umich.edu // open the file 6595795Ssaidi@eecs.umich.edu fd = open(path.c_str(), hostFlags, mode); 6609143Ssteve.reinhardt@amd.com local_errno = errno; 6615795Ssaidi@eecs.umich.edu } 662360SN/A 6639143Ssteve.reinhardt@amd.com if (fd == -1) 6649143Ssteve.reinhardt@amd.com return -local_errno; 6659143Ssteve.reinhardt@amd.com 66610932Sbrandon.potter@amd.com return process->allocFD(fd, path.c_str(), hostFlags, mode, false); 667360SN/A} 668360SN/A 66910027SChris.Adeniyi-Jones@arm.com/// Target open() handler. 67010027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 67110027SChris.Adeniyi-Jones@arm.comSyscallReturn 67210027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 67310027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 67410027SChris.Adeniyi-Jones@arm.com{ 67510027SChris.Adeniyi-Jones@arm.com return openFunc<OS>(desc, callnum, process, tc, 0); 67610027SChris.Adeniyi-Jones@arm.com} 67710027SChris.Adeniyi-Jones@arm.com 67810027SChris.Adeniyi-Jones@arm.com/// Target openat() handler. 67910027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 68010027SChris.Adeniyi-Jones@arm.comSyscallReturn 68110027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 68210027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 68310027SChris.Adeniyi-Jones@arm.com{ 68410027SChris.Adeniyi-Jones@arm.com int index = 0; 68510027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 68610027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 68710027SChris.Adeniyi-Jones@arm.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 68810027SChris.Adeniyi-Jones@arm.com return openFunc<OS>(desc, callnum, process, tc, 1); 68910027SChris.Adeniyi-Jones@arm.com} 69010027SChris.Adeniyi-Jones@arm.com 69110633Smichaelupton@gmail.com/// Target unlinkat() handler. 69210633Smichaelupton@gmail.comtemplate <class OS> 69310633Smichaelupton@gmail.comSyscallReturn 69410633Smichaelupton@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 69510633Smichaelupton@gmail.com ThreadContext *tc) 69610633Smichaelupton@gmail.com{ 69710633Smichaelupton@gmail.com int index = 0; 69810633Smichaelupton@gmail.com int dirfd = process->getSyscallArg(tc, index); 69910633Smichaelupton@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 70010633Smichaelupton@gmail.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 70110633Smichaelupton@gmail.com 70210633Smichaelupton@gmail.com return unlinkHelper(desc, callnum, process, tc, 1); 70310633Smichaelupton@gmail.com} 70410633Smichaelupton@gmail.com 70510203SAli.Saidi@ARM.com/// Target facessat() handler 70610203SAli.Saidi@ARM.comtemplate <class OS> 70710203SAli.Saidi@ARM.comSyscallReturn 70810203SAli.Saidi@ARM.comfaccessatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 70910203SAli.Saidi@ARM.com ThreadContext *tc) 71010203SAli.Saidi@ARM.com{ 71110203SAli.Saidi@ARM.com int index = 0; 71210203SAli.Saidi@ARM.com int dirfd = process->getSyscallArg(tc, index); 71310203SAli.Saidi@ARM.com if (dirfd != OS::TGT_AT_FDCWD) 71410203SAli.Saidi@ARM.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 71510203SAli.Saidi@ARM.com return accessFunc(desc, callnum, process, tc, 1); 71610203SAli.Saidi@ARM.com} 71710203SAli.Saidi@ARM.com 71810203SAli.Saidi@ARM.com/// Target readlinkat() handler 71910203SAli.Saidi@ARM.comtemplate <class OS> 72010203SAli.Saidi@ARM.comSyscallReturn 72110203SAli.Saidi@ARM.comreadlinkatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 72210203SAli.Saidi@ARM.com ThreadContext *tc) 72310203SAli.Saidi@ARM.com{ 72410203SAli.Saidi@ARM.com int index = 0; 72510203SAli.Saidi@ARM.com int dirfd = process->getSyscallArg(tc, index); 72610203SAli.Saidi@ARM.com if (dirfd != OS::TGT_AT_FDCWD) 72710203SAli.Saidi@ARM.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 72810203SAli.Saidi@ARM.com return readlinkFunc(desc, callnum, process, tc, 1); 72910203SAli.Saidi@ARM.com} 73010203SAli.Saidi@ARM.com 73110850SGiacomo.Gabrielli@arm.com/// Target renameat() handler. 73210850SGiacomo.Gabrielli@arm.comtemplate <class OS> 73310850SGiacomo.Gabrielli@arm.comSyscallReturn 73410850SGiacomo.Gabrielli@arm.comrenameatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 73510850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 73610850SGiacomo.Gabrielli@arm.com{ 73710850SGiacomo.Gabrielli@arm.com int index = 0; 73810850SGiacomo.Gabrielli@arm.com 73910850SGiacomo.Gabrielli@arm.com int olddirfd = process->getSyscallArg(tc, index); 74010850SGiacomo.Gabrielli@arm.com if (olddirfd != OS::TGT_AT_FDCWD) 74110850SGiacomo.Gabrielli@arm.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 74210850SGiacomo.Gabrielli@arm.com 74310850SGiacomo.Gabrielli@arm.com std::string old_name; 74410850SGiacomo.Gabrielli@arm.com 74510850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(old_name, 74610850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 74710850SGiacomo.Gabrielli@arm.com return -EFAULT; 74810850SGiacomo.Gabrielli@arm.com 74910850SGiacomo.Gabrielli@arm.com int newdirfd = process->getSyscallArg(tc, index); 75010850SGiacomo.Gabrielli@arm.com if (newdirfd != OS::TGT_AT_FDCWD) 75110850SGiacomo.Gabrielli@arm.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 75210850SGiacomo.Gabrielli@arm.com 75310850SGiacomo.Gabrielli@arm.com std::string new_name; 75410850SGiacomo.Gabrielli@arm.com 75510850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(new_name, 75610850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 75710850SGiacomo.Gabrielli@arm.com return -EFAULT; 75810850SGiacomo.Gabrielli@arm.com 75910850SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 76010850SGiacomo.Gabrielli@arm.com old_name = process->fullPath(old_name); 76110850SGiacomo.Gabrielli@arm.com new_name = process->fullPath(new_name); 76210850SGiacomo.Gabrielli@arm.com 76310850SGiacomo.Gabrielli@arm.com int result = rename(old_name.c_str(), new_name.c_str()); 76410850SGiacomo.Gabrielli@arm.com return (result == -1) ? -errno : result; 76510850SGiacomo.Gabrielli@arm.com} 76610850SGiacomo.Gabrielli@arm.com 7676640Svince@csl.cornell.edu/// Target sysinfo() handler. 7686640Svince@csl.cornell.edutemplate <class OS> 7696640Svince@csl.cornell.eduSyscallReturn 7706640Svince@csl.cornell.edusysinfoFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7716640Svince@csl.cornell.edu ThreadContext *tc) 7726640Svince@csl.cornell.edu{ 7736640Svince@csl.cornell.edu 7746701Sgblack@eecs.umich.edu int index = 0; 7756701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 77610793Sbrandon.potter@amd.com sysinfo(process->getSyscallArg(tc, index)); 7776640Svince@csl.cornell.edu 77811758Sbrandon.potter@amd.com sysinfo->uptime = seconds_since_epoch; 77911758Sbrandon.potter@amd.com sysinfo->totalram = process->system->memSize(); 78011758Sbrandon.potter@amd.com sysinfo->mem_unit = 1; 7816640Svince@csl.cornell.edu 7828706Sandreas.hansson@arm.com sysinfo.copyOut(tc->getMemProxy()); 7836640Svince@csl.cornell.edu 7846701Sgblack@eecs.umich.edu return 0; 7856640Svince@csl.cornell.edu} 786360SN/A 7871999SN/A/// Target chmod() handler. 7881999SN/Atemplate <class OS> 7891999SN/ASyscallReturn 7903114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7912680Sktlim@umich.edu ThreadContext *tc) 7921999SN/A{ 7931999SN/A std::string path; 7941999SN/A 7956701Sgblack@eecs.umich.edu int index = 0; 7968852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 7976701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7981999SN/A return -EFAULT; 7996701Sgblack@eecs.umich.edu } 8001999SN/A 8016701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 8021999SN/A mode_t hostMode = 0; 8031999SN/A 8041999SN/A // XXX translate mode flags via OS::something??? 8051999SN/A hostMode = mode; 8061999SN/A 8073669Sbinkertn@umich.edu // Adjust path for current working directory 8083669Sbinkertn@umich.edu path = process->fullPath(path); 8093669Sbinkertn@umich.edu 8101999SN/A // do the chmod 8111999SN/A int result = chmod(path.c_str(), hostMode); 8121999SN/A if (result < 0) 8132218SN/A return -errno; 8141999SN/A 8151999SN/A return 0; 8161999SN/A} 8171999SN/A 8181999SN/A 8191999SN/A/// Target fchmod() handler. 8201999SN/Atemplate <class OS> 8211999SN/ASyscallReturn 8223114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8232680Sktlim@umich.edu ThreadContext *tc) 8241999SN/A{ 8256701Sgblack@eecs.umich.edu int index = 0; 82610931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 82710931Sbrandon.potter@amd.com uint32_t mode = process->getSyscallArg(tc, index); 82810931Sbrandon.potter@amd.com 82910932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 83010931Sbrandon.potter@amd.com if (sim_fd < 0) 8311999SN/A return -EBADF; 8321999SN/A 8331999SN/A mode_t hostMode = 0; 8341999SN/A 8351999SN/A // XXX translate mode flags via OS::someting??? 8361999SN/A hostMode = mode; 8371999SN/A 8381999SN/A // do the fchmod 83910931Sbrandon.potter@amd.com int result = fchmod(sim_fd, hostMode); 8401999SN/A if (result < 0) 8412218SN/A return -errno; 8421999SN/A 8431999SN/A return 0; 8441999SN/A} 8451999SN/A 8465877Shsul@eecs.umich.edu/// Target mremap() handler. 8475877Shsul@eecs.umich.edutemplate <class OS> 8485877Shsul@eecs.umich.eduSyscallReturn 8495877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 8505877Shsul@eecs.umich.edu{ 8516701Sgblack@eecs.umich.edu int index = 0; 8526701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 8536701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 8546701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 8556701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 85610027SChris.Adeniyi-Jones@arm.com uint64_t provided_address = 0; 85710027SChris.Adeniyi-Jones@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 85810027SChris.Adeniyi-Jones@arm.com 85910027SChris.Adeniyi-Jones@arm.com if (use_provided_address) 86010027SChris.Adeniyi-Jones@arm.com provided_address = process->getSyscallArg(tc, index); 8615877Shsul@eecs.umich.edu 86210318Sandreas.hansson@arm.com if ((start % TheISA::PageBytes != 0) || 86310318Sandreas.hansson@arm.com (provided_address % TheISA::PageBytes != 0)) { 8645877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 8655877Shsul@eecs.umich.edu return -EINVAL; 8665877Shsul@eecs.umich.edu } 8675877Shsul@eecs.umich.edu 86810486Stjablin@gmail.com new_length = roundUp(new_length, TheISA::PageBytes); 86910486Stjablin@gmail.com 8705877Shsul@eecs.umich.edu if (new_length > old_length) { 87110027SChris.Adeniyi-Jones@arm.com if ((start + old_length) == process->mmap_end && 87210027SChris.Adeniyi-Jones@arm.com (!use_provided_address || provided_address == start)) { 8735877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 8748601Ssteve.reinhardt@amd.com process->allocateMem(process->mmap_end, diff); 8755877Shsul@eecs.umich.edu process->mmap_end += diff; 8765877Shsul@eecs.umich.edu return start; 8775877Shsul@eecs.umich.edu } else { 87810027SChris.Adeniyi-Jones@arm.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 8795877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 8805877Shsul@eecs.umich.edu return -ENOMEM; 8815877Shsul@eecs.umich.edu } else { 88210027SChris.Adeniyi-Jones@arm.com uint64_t new_start = use_provided_address ? 88310027SChris.Adeniyi-Jones@arm.com provided_address : process->mmap_end; 88410027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, old_length, new_start); 88510027SChris.Adeniyi-Jones@arm.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 88610027SChris.Adeniyi-Jones@arm.com new_start, new_start + new_length, 88710027SChris.Adeniyi-Jones@arm.com new_length - old_length); 8885877Shsul@eecs.umich.edu // add on the remaining unallocated pages 88910027SChris.Adeniyi-Jones@arm.com process->allocateMem(new_start + old_length, 89010027SChris.Adeniyi-Jones@arm.com new_length - old_length, 89110027SChris.Adeniyi-Jones@arm.com use_provided_address /* clobber */); 89210027SChris.Adeniyi-Jones@arm.com if (!use_provided_address) 89310027SChris.Adeniyi-Jones@arm.com process->mmap_end += new_length; 89410027SChris.Adeniyi-Jones@arm.com if (use_provided_address && 89510027SChris.Adeniyi-Jones@arm.com new_start + new_length > process->mmap_end) { 89610027SChris.Adeniyi-Jones@arm.com // something fishy going on here, at least notify the user 89710027SChris.Adeniyi-Jones@arm.com // @todo: increase mmap_end? 89810027SChris.Adeniyi-Jones@arm.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 89910027SChris.Adeniyi-Jones@arm.com } 90010027SChris.Adeniyi-Jones@arm.com warn("returning %08p as start\n", new_start); 90110027SChris.Adeniyi-Jones@arm.com return new_start; 9025877Shsul@eecs.umich.edu } 9035877Shsul@eecs.umich.edu } 9045877Shsul@eecs.umich.edu } else { 90510027SChris.Adeniyi-Jones@arm.com if (use_provided_address && provided_address != start) 90610027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 9078601Ssteve.reinhardt@amd.com process->pTable->unmap(start + new_length, old_length - new_length); 90810027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 9095877Shsul@eecs.umich.edu } 9105877Shsul@eecs.umich.edu} 9111999SN/A 912378SN/A/// Target stat() handler. 913360SN/Atemplate <class OS> 9141450SN/ASyscallReturn 9153114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9162680Sktlim@umich.edu ThreadContext *tc) 917360SN/A{ 918360SN/A std::string path; 919360SN/A 9206701Sgblack@eecs.umich.edu int index = 0; 9218852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 9226701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9236701Sgblack@eecs.umich.edu return -EFAULT; 9246701Sgblack@eecs.umich.edu } 9256701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 926360SN/A 9273669Sbinkertn@umich.edu // Adjust path for current working directory 9283669Sbinkertn@umich.edu path = process->fullPath(path); 9293669Sbinkertn@umich.edu 930360SN/A struct stat hostBuf; 931360SN/A int result = stat(path.c_str(), &hostBuf); 932360SN/A 933360SN/A if (result < 0) 9342218SN/A return -errno; 935360SN/A 9368706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 937360SN/A 9381458SN/A return 0; 939360SN/A} 940360SN/A 941360SN/A 9425074Ssaidi@eecs.umich.edu/// Target stat64() handler. 9435074Ssaidi@eecs.umich.edutemplate <class OS> 9445074Ssaidi@eecs.umich.eduSyscallReturn 9455074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 9465074Ssaidi@eecs.umich.edu ThreadContext *tc) 9475074Ssaidi@eecs.umich.edu{ 9485074Ssaidi@eecs.umich.edu std::string path; 9495074Ssaidi@eecs.umich.edu 9506701Sgblack@eecs.umich.edu int index = 0; 9518852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 9526701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 9535074Ssaidi@eecs.umich.edu return -EFAULT; 9546701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9555074Ssaidi@eecs.umich.edu 9565074Ssaidi@eecs.umich.edu // Adjust path for current working directory 9575074Ssaidi@eecs.umich.edu path = process->fullPath(path); 9585074Ssaidi@eecs.umich.edu 9595208Ssaidi@eecs.umich.edu#if NO_STAT64 9605208Ssaidi@eecs.umich.edu struct stat hostBuf; 9615208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 9625208Ssaidi@eecs.umich.edu#else 9635074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 9645074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 9655208Ssaidi@eecs.umich.edu#endif 9665074Ssaidi@eecs.umich.edu 9675074Ssaidi@eecs.umich.edu if (result < 0) 9685074Ssaidi@eecs.umich.edu return -errno; 9695074Ssaidi@eecs.umich.edu 9708706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 9715074Ssaidi@eecs.umich.edu 9725074Ssaidi@eecs.umich.edu return 0; 9735074Ssaidi@eecs.umich.edu} 9745074Ssaidi@eecs.umich.edu 9755074Ssaidi@eecs.umich.edu 97610027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler. 97710027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 97810027SChris.Adeniyi-Jones@arm.comSyscallReturn 97910027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 98010027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 98110027SChris.Adeniyi-Jones@arm.com{ 98210027SChris.Adeniyi-Jones@arm.com int index = 0; 98310027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 98410027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 98510793Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 98610027SChris.Adeniyi-Jones@arm.com 98710027SChris.Adeniyi-Jones@arm.com std::string path; 98810027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 98910027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) 99010027SChris.Adeniyi-Jones@arm.com return -EFAULT; 99110027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 99210027SChris.Adeniyi-Jones@arm.com 99310027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 99410027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 99510027SChris.Adeniyi-Jones@arm.com 99610027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 99710027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 99810027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 99910027SChris.Adeniyi-Jones@arm.com#else 100010027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 100110027SChris.Adeniyi-Jones@arm.com int result = stat64(path.c_str(), &hostBuf); 100210027SChris.Adeniyi-Jones@arm.com#endif 100310027SChris.Adeniyi-Jones@arm.com 100410027SChris.Adeniyi-Jones@arm.com if (result < 0) 100510027SChris.Adeniyi-Jones@arm.com return -errno; 100610027SChris.Adeniyi-Jones@arm.com 100710027SChris.Adeniyi-Jones@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 100810027SChris.Adeniyi-Jones@arm.com 100910027SChris.Adeniyi-Jones@arm.com return 0; 101010027SChris.Adeniyi-Jones@arm.com} 101110027SChris.Adeniyi-Jones@arm.com 101210027SChris.Adeniyi-Jones@arm.com 10131999SN/A/// Target fstat64() handler. 10141999SN/Atemplate <class OS> 10151999SN/ASyscallReturn 10163114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 10172680Sktlim@umich.edu ThreadContext *tc) 10181999SN/A{ 10196701Sgblack@eecs.umich.edu int index = 0; 102010931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 10216701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 102210931Sbrandon.potter@amd.com 102310932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 102410931Sbrandon.potter@amd.com if (sim_fd < 0) 10251999SN/A return -EBADF; 10261999SN/A 10272764Sstever@eecs.umich.edu#if NO_STAT64 10282064SN/A struct stat hostBuf; 102910931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 10302064SN/A#else 10312064SN/A struct stat64 hostBuf; 103210931Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 10332064SN/A#endif 10341999SN/A 10351999SN/A if (result < 0) 10362218SN/A return -errno; 10371999SN/A 103810931Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 10391999SN/A 10401999SN/A return 0; 10411999SN/A} 10421999SN/A 10431999SN/A 1044378SN/A/// Target lstat() handler. 1045360SN/Atemplate <class OS> 10461450SN/ASyscallReturn 10473114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10482680Sktlim@umich.edu ThreadContext *tc) 1049360SN/A{ 1050360SN/A std::string path; 1051360SN/A 10526701Sgblack@eecs.umich.edu int index = 0; 10538852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 10546701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 10556701Sgblack@eecs.umich.edu return -EFAULT; 10566701Sgblack@eecs.umich.edu } 10576701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1058360SN/A 10593669Sbinkertn@umich.edu // Adjust path for current working directory 10603669Sbinkertn@umich.edu path = process->fullPath(path); 10613669Sbinkertn@umich.edu 1062360SN/A struct stat hostBuf; 1063360SN/A int result = lstat(path.c_str(), &hostBuf); 1064360SN/A 1065360SN/A if (result < 0) 10661458SN/A return -errno; 1067360SN/A 10688706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1069360SN/A 10701458SN/A return 0; 1071360SN/A} 1072360SN/A 10731999SN/A/// Target lstat64() handler. 10741999SN/Atemplate <class OS> 10751999SN/ASyscallReturn 10763114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 10772680Sktlim@umich.edu ThreadContext *tc) 10781999SN/A{ 10791999SN/A std::string path; 10801999SN/A 10816701Sgblack@eecs.umich.edu int index = 0; 10828852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 10836701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 10846701Sgblack@eecs.umich.edu return -EFAULT; 10856701Sgblack@eecs.umich.edu } 10866701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 10871999SN/A 10883669Sbinkertn@umich.edu // Adjust path for current working directory 10893669Sbinkertn@umich.edu path = process->fullPath(path); 10903669Sbinkertn@umich.edu 10912764Sstever@eecs.umich.edu#if NO_STAT64 10922064SN/A struct stat hostBuf; 10932064SN/A int result = lstat(path.c_str(), &hostBuf); 10942064SN/A#else 10951999SN/A struct stat64 hostBuf; 10961999SN/A int result = lstat64(path.c_str(), &hostBuf); 10972064SN/A#endif 10981999SN/A 10991999SN/A if (result < 0) 11001999SN/A return -errno; 11011999SN/A 11028706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11031999SN/A 11041999SN/A return 0; 11051999SN/A} 11061999SN/A 1107378SN/A/// Target fstat() handler. 1108360SN/Atemplate <class OS> 11091450SN/ASyscallReturn 11103114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11112680Sktlim@umich.edu ThreadContext *tc) 1112360SN/A{ 11136701Sgblack@eecs.umich.edu int index = 0; 111410931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 11156701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1116360SN/A 111711380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1118360SN/A 111910932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 112010931Sbrandon.potter@amd.com if (sim_fd < 0) 11211458SN/A return -EBADF; 1122360SN/A 1123360SN/A struct stat hostBuf; 112410931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 1125360SN/A 1126360SN/A if (result < 0) 11271458SN/A return -errno; 1128360SN/A 112910931Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 11302021SN/A 11311458SN/A return 0; 1132360SN/A} 1133360SN/A 1134360SN/A 11351706SN/A/// Target statfs() handler. 11361706SN/Atemplate <class OS> 11371706SN/ASyscallReturn 11383114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11392680Sktlim@umich.edu ThreadContext *tc) 11401706SN/A{ 11411706SN/A std::string path; 11421706SN/A 11436701Sgblack@eecs.umich.edu int index = 0; 11448852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 11456701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11466701Sgblack@eecs.umich.edu return -EFAULT; 11476701Sgblack@eecs.umich.edu } 11486701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11491706SN/A 11503669Sbinkertn@umich.edu // Adjust path for current working directory 11513669Sbinkertn@umich.edu path = process->fullPath(path); 11523669Sbinkertn@umich.edu 11531706SN/A struct statfs hostBuf; 11541706SN/A int result = statfs(path.c_str(), &hostBuf); 11551706SN/A 11561706SN/A if (result < 0) 11572218SN/A return -errno; 11581706SN/A 11598706Sandreas.hansson@arm.com OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf); 11601706SN/A 11611706SN/A return 0; 11621706SN/A} 11631706SN/A 11641706SN/A 11651706SN/A/// Target fstatfs() handler. 11661706SN/Atemplate <class OS> 11671706SN/ASyscallReturn 11683114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11692680Sktlim@umich.edu ThreadContext *tc) 11701706SN/A{ 11716701Sgblack@eecs.umich.edu int index = 0; 117210931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 11736701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11741706SN/A 117510932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 117610931Sbrandon.potter@amd.com if (sim_fd < 0) 11771706SN/A return -EBADF; 11781706SN/A 11791706SN/A struct statfs hostBuf; 118010931Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 11811706SN/A 11821706SN/A if (result < 0) 11832218SN/A return -errno; 11841706SN/A 11858706Sandreas.hansson@arm.com OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf); 11861706SN/A 11871706SN/A return 0; 11881706SN/A} 11891706SN/A 11901706SN/A 11911999SN/A/// Target writev() handler. 11921999SN/Atemplate <class OS> 11931999SN/ASyscallReturn 11943114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11952680Sktlim@umich.edu ThreadContext *tc) 11961999SN/A{ 11976701Sgblack@eecs.umich.edu int index = 0; 119810931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 119910931Sbrandon.potter@amd.com 120010932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 120110931Sbrandon.potter@amd.com if (sim_fd < 0) 12021999SN/A return -EBADF; 12031999SN/A 12048852Sandreas.hansson@arm.com SETranslatingPortProxy &p = tc->getMemProxy(); 12056701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 12066701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 12071999SN/A struct iovec hiov[count]; 12086227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 12091999SN/A typename OS::tgt_iovec tiov; 12102461SN/A 12118852Sandreas.hansson@arm.com p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 12128852Sandreas.hansson@arm.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 12138737Skoansin.tan@gmail.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 12141999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 12158852Sandreas.hansson@arm.com p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 12168852Sandreas.hansson@arm.com hiov[i].iov_len); 12171999SN/A } 12181999SN/A 121910931Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 12201999SN/A 12216227Snate@binkert.org for (size_t i = 0; i < count; ++i) 12221999SN/A delete [] (char *)hiov[i].iov_base; 12231999SN/A 12241999SN/A if (result < 0) 12252218SN/A return -errno; 12261999SN/A 122710629Sjthestness@gmail.com return result; 12281999SN/A} 12291999SN/A 123011385Sbrandon.potter@amd.com/// Real mmap handler. 1231360SN/Atemplate <class OS> 12321450SN/ASyscallReturn 123311385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc, 123411385Sbrandon.potter@amd.com bool is_mmap2) 1235360SN/A{ 12366701Sgblack@eecs.umich.edu int index = 0; 12376701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 12386701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 123911383Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 124011383Sbrandon.potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 12418324Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 124210486Stjablin@gmail.com int offset = p->getSyscallArg(tc, index); 1243360SN/A 124411385Sbrandon.potter@amd.com if (is_mmap2) 124511385Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 12469008Sgblack@eecs.umich.edu 124711383Sbrandon.potter@amd.com if (start & (TheISA::PageBytes - 1) || 124811383Sbrandon.potter@amd.com offset & (TheISA::PageBytes - 1) || 124911383Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 125011383Sbrandon.potter@amd.com tgt_flags & OS::TGT_MAP_SHARED) || 125111383Sbrandon.potter@amd.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 125211383Sbrandon.potter@amd.com !(tgt_flags & OS::TGT_MAP_SHARED)) || 125311383Sbrandon.potter@amd.com !length) { 125411383Sbrandon.potter@amd.com return -EINVAL; 125511383Sbrandon.potter@amd.com } 12568324Ssteve.reinhardt@amd.com 125711383Sbrandon.potter@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 125811383Sbrandon.potter@amd.com // With shared mmaps, there are two cases to consider: 125911383Sbrandon.potter@amd.com // 1) anonymous: writes should modify the mapping and this should be 126011383Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 126111383Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 126211383Sbrandon.potter@amd.com // structure which maintains information about the which virtual 126311383Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 126411383Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 126511383Sbrandon.potter@amd.com // 2) file-backed: writes should modify the mapping and the file 126611383Sbrandon.potter@amd.com // which is backed by the mapping. The shared mapping problem is the 126711383Sbrandon.potter@amd.com // same as what was mentioned about the anonymous mappings. For 126811383Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 126911383Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 127011383Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 127111383Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 127211383Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 127311383Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 127411383Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 127511383Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 127611383Sbrandon.potter@amd.com // a call to msync, but that again would require more information 127711383Sbrandon.potter@amd.com // than we currently maintain. 127811383Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 127911383Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 128011383Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 12818324Ssteve.reinhardt@amd.com } 12825877Shsul@eecs.umich.edu 128310486Stjablin@gmail.com length = roundUp(length, TheISA::PageBytes); 128410486Stjablin@gmail.com 128511383Sbrandon.potter@amd.com int sim_fd = -1; 128611383Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 128711383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 128811624Smichael.lebeane@amd.com // Check for EmulatedDriver mmap 128911624Smichael.lebeane@amd.com FDEntry *fde = p->getFDEntry(tgt_fd); 129011624Smichael.lebeane@amd.com if (fde == NULL) 129111624Smichael.lebeane@amd.com return -EBADF; 129211624Smichael.lebeane@amd.com 129311624Smichael.lebeane@amd.com if (fde->driver != NULL) { 129411624Smichael.lebeane@amd.com return fde->driver->mmap(p, tc, start, length, prot, 129511624Smichael.lebeane@amd.com tgt_flags, tgt_fd, offset); 129611624Smichael.lebeane@amd.com } 129711624Smichael.lebeane@amd.com sim_fd = fde->fd; 129811624Smichael.lebeane@amd.com 129911383Sbrandon.potter@amd.com if (sim_fd < 0) 130011383Sbrandon.potter@amd.com return -EBADF; 1301360SN/A 130211383Sbrandon.potter@amd.com pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE, 130311383Sbrandon.potter@amd.com sim_fd, offset); 13048600Ssteve.reinhardt@amd.com 130511383Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 130611383Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 130711383Sbrandon.potter@amd.com return -errno; 13088600Ssteve.reinhardt@amd.com } 13092544SN/A } 13102544SN/A 131111383Sbrandon.potter@amd.com // Extend global mmap region if necessary. Note that we ignore the 131211383Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 131311383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 131411386Ssteve.reinhardt@amd.com start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end; 131511386Ssteve.reinhardt@amd.com p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length; 131611383Sbrandon.potter@amd.com } 131711383Sbrandon.potter@amd.com 131811383Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 131911383Sbrandon.potter@amd.com start, start + length - 1); 132011383Sbrandon.potter@amd.com 132111383Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 132211383Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 132311383Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 132411383Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 132511383Sbrandon.potter@amd.com if (clobber) { 132611383Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 132711383Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 132811383Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 132911383Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 133011383Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 13318600Ssteve.reinhardt@amd.com } 13326672Sgblack@eecs.umich.edu } 13338600Ssteve.reinhardt@amd.com 133411383Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 133511383Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 133611383Sbrandon.potter@amd.com // fatal and bail out of the simulation. 13378601Ssteve.reinhardt@amd.com p->allocateMem(start, length, clobber); 13382544SN/A 133911383Sbrandon.potter@amd.com // Transfer content into target address space. 134011383Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 134111383Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 134211383Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 134311383Sbrandon.potter@amd.com // with something like: 134411383Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 134511383Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 134611383Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 134711383Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 134811383Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 134911383Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 135011383Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 135111383Sbrandon.potter@amd.com } else { 135211383Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 135311383Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 135411383Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 135511383Sbrandon.potter@amd.com // the host into the target address space. 135611383Sbrandon.potter@amd.com struct stat file_stat; 135711383Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 135811383Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 135911383Sbrandon.potter@amd.com 136011383Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 136111383Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 136211383Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 136311383Sbrandon.potter@amd.com // on the specified offset into the file. 136411383Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 136511383Sbrandon.potter@amd.com length); 136611383Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 136711383Sbrandon.potter@amd.com 136811383Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 136911383Sbrandon.potter@amd.com munmap(pmap, length); 137011383Sbrandon.potter@amd.com 137111392Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 137211392Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 137311392Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 137411392Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 137511392Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 137611392Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 137711392Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 137811392Sbrandon.potter@amd.com if (interpreter) { 137911392Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 138011392Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 138111392Sbrandon.potter@amd.com 138211392Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 138311392Sbrandon.potter@amd.com 138411392Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 138511392Sbrandon.potter@amd.com FDEntry *fde = p->getFDEntry(tgt_fd); 138611392Sbrandon.potter@amd.com 138711392Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(fde->filename); 138811392Sbrandon.potter@amd.com 138911392Sbrandon.potter@amd.com if (lib) { 139011392Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 139111392Sbrandon.potter@amd.com lib->textBase(), start); 139211392Sbrandon.potter@amd.com } 139311392Sbrandon.potter@amd.com } 139411392Sbrandon.potter@amd.com } 139511392Sbrandon.potter@amd.com 139611383Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 139711383Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 139811383Sbrandon.potter@amd.com // execution (hopefully). 139911383Sbrandon.potter@amd.com } 140011383Sbrandon.potter@amd.com 14011458SN/A return start; 1402360SN/A} 1403360SN/A 140411593Santhony.gutierrez@amd.comtemplate <class OS> 140511593Santhony.gutierrez@amd.comSyscallReturn 140611593Santhony.gutierrez@amd.compwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 140711593Santhony.gutierrez@amd.com{ 140811593Santhony.gutierrez@amd.com int index = 0; 140911593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 141011593Santhony.gutierrez@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 141111593Santhony.gutierrez@amd.com int nbytes = p->getSyscallArg(tc, index); 141211593Santhony.gutierrez@amd.com int offset = p->getSyscallArg(tc, index); 141311593Santhony.gutierrez@amd.com 141411593Santhony.gutierrez@amd.com int sim_fd = p->getSimFD(tgt_fd); 141511593Santhony.gutierrez@amd.com if (sim_fd < 0) 141611593Santhony.gutierrez@amd.com return -EBADF; 141711593Santhony.gutierrez@amd.com 141811593Santhony.gutierrez@amd.com BufferArg bufArg(bufPtr, nbytes); 141911593Santhony.gutierrez@amd.com bufArg.copyIn(tc->getMemProxy()); 142011593Santhony.gutierrez@amd.com 142111594Santhony.gutierrez@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 142211593Santhony.gutierrez@amd.com 142311593Santhony.gutierrez@amd.com return (bytes_written == -1) ? -errno : bytes_written; 142411593Santhony.gutierrez@amd.com} 142511593Santhony.gutierrez@amd.com 142611385Sbrandon.potter@amd.com/// Target mmap() handler. 142711385Sbrandon.potter@amd.comtemplate <class OS> 142811385Sbrandon.potter@amd.comSyscallReturn 142911385Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 143011385Sbrandon.potter@amd.com{ 143111385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, false); 143211385Sbrandon.potter@amd.com} 143311385Sbrandon.potter@amd.com 143411385Sbrandon.potter@amd.com/// Target mmap2() handler. 143511385Sbrandon.potter@amd.comtemplate <class OS> 143611385Sbrandon.potter@amd.comSyscallReturn 143711385Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 143811385Sbrandon.potter@amd.com{ 143911385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 144011385Sbrandon.potter@amd.com} 144111385Sbrandon.potter@amd.com 1442378SN/A/// Target getrlimit() handler. 1443360SN/Atemplate <class OS> 14441450SN/ASyscallReturn 14453114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 14462680Sktlim@umich.edu ThreadContext *tc) 1447360SN/A{ 14486701Sgblack@eecs.umich.edu int index = 0; 14496701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 14506701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1451360SN/A 1452360SN/A switch (resource) { 14532064SN/A case OS::TGT_RLIMIT_STACK: 14545877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 14552064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 14568737Skoansin.tan@gmail.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 14578737Skoansin.tan@gmail.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 14582064SN/A break; 1459360SN/A 14605877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 14615877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 14625877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 14638737Skoansin.tan@gmail.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 14648737Skoansin.tan@gmail.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 14655877Shsul@eecs.umich.edu break; 14665877Shsul@eecs.umich.edu 14672064SN/A default: 146810794Sbrandon.potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 146910794Sbrandon.potter@amd.com return -EINVAL; 14702064SN/A break; 1471360SN/A } 1472360SN/A 14738706Sandreas.hansson@arm.com rlp.copyOut(tc->getMemProxy()); 14741458SN/A return 0; 1475360SN/A} 1476360SN/A 147710796Sbrandon.potter@amd.com/// Target clock_gettime() function. 147810796Sbrandon.potter@amd.comtemplate <class OS> 147910796Sbrandon.potter@amd.comSyscallReturn 148010796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 148110796Sbrandon.potter@amd.com{ 148210796Sbrandon.potter@amd.com int index = 1; 148310796Sbrandon.potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 148410796Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 148510796Sbrandon.potter@amd.com 148610796Sbrandon.potter@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 148710796Sbrandon.potter@amd.com tp->tv_sec += seconds_since_epoch; 148810796Sbrandon.potter@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 148910796Sbrandon.potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 149010796Sbrandon.potter@amd.com 149110796Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 149210796Sbrandon.potter@amd.com 149310796Sbrandon.potter@amd.com return 0; 149410796Sbrandon.potter@amd.com} 149510796Sbrandon.potter@amd.com 149611337SMichael.Lebeane@amd.com/// Target clock_getres() function. 149711337SMichael.Lebeane@amd.comtemplate <class OS> 149811337SMichael.Lebeane@amd.comSyscallReturn 149911337SMichael.Lebeane@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 150011337SMichael.Lebeane@amd.com{ 150111337SMichael.Lebeane@amd.com int index = 1; 150211337SMichael.Lebeane@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 150311337SMichael.Lebeane@amd.com 150411337SMichael.Lebeane@amd.com // Set resolution at ns, which is what clock_gettime() returns 150511337SMichael.Lebeane@amd.com tp->tv_sec = 0; 150611337SMichael.Lebeane@amd.com tp->tv_nsec = 1; 150711337SMichael.Lebeane@amd.com 150811337SMichael.Lebeane@amd.com tp.copyOut(tc->getMemProxy()); 150911337SMichael.Lebeane@amd.com 151011337SMichael.Lebeane@amd.com return 0; 151111337SMichael.Lebeane@amd.com} 151211337SMichael.Lebeane@amd.com 1513378SN/A/// Target gettimeofday() handler. 1514360SN/Atemplate <class OS> 15151450SN/ASyscallReturn 15163114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 15172680Sktlim@umich.edu ThreadContext *tc) 1518360SN/A{ 15196701Sgblack@eecs.umich.edu int index = 0; 15206701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1521360SN/A 152210796Sbrandon.potter@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1523360SN/A tp->tv_sec += seconds_since_epoch; 15246109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 15256109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1526360SN/A 15278706Sandreas.hansson@arm.com tp.copyOut(tc->getMemProxy()); 1528360SN/A 15291458SN/A return 0; 1530360SN/A} 1531360SN/A 1532360SN/A 15331999SN/A/// Target utimes() handler. 15341999SN/Atemplate <class OS> 15351999SN/ASyscallReturn 15363114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 15372680Sktlim@umich.edu ThreadContext *tc) 15381999SN/A{ 15391999SN/A std::string path; 15401999SN/A 15416701Sgblack@eecs.umich.edu int index = 0; 15428852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 15436701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 15446701Sgblack@eecs.umich.edu return -EFAULT; 15456701Sgblack@eecs.umich.edu } 15461999SN/A 15476701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 15486701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 15498706Sandreas.hansson@arm.com tp.copyIn(tc->getMemProxy()); 15501999SN/A 15511999SN/A struct timeval hostTimeval[2]; 15521999SN/A for (int i = 0; i < 2; ++i) 15531999SN/A { 15548737Skoansin.tan@gmail.com hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 15558737Skoansin.tan@gmail.com hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 15561999SN/A } 15573669Sbinkertn@umich.edu 15583669Sbinkertn@umich.edu // Adjust path for current working directory 15593669Sbinkertn@umich.edu path = process->fullPath(path); 15603669Sbinkertn@umich.edu 15611999SN/A int result = utimes(path.c_str(), hostTimeval); 15621999SN/A 15631999SN/A if (result < 0) 15641999SN/A return -errno; 15651999SN/A 15661999SN/A return 0; 15671999SN/A} 1568378SN/A/// Target getrusage() function. 1569360SN/Atemplate <class OS> 15701450SN/ASyscallReturn 15713114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 15722680Sktlim@umich.edu ThreadContext *tc) 1573360SN/A{ 15746701Sgblack@eecs.umich.edu int index = 0; 15756701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 15766701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1577360SN/A 15783670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 15793670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1580360SN/A rup->ru_stime.tv_sec = 0; 1581360SN/A rup->ru_stime.tv_usec = 0; 1582360SN/A rup->ru_maxrss = 0; 1583360SN/A rup->ru_ixrss = 0; 1584360SN/A rup->ru_idrss = 0; 1585360SN/A rup->ru_isrss = 0; 1586360SN/A rup->ru_minflt = 0; 1587360SN/A rup->ru_majflt = 0; 1588360SN/A rup->ru_nswap = 0; 1589360SN/A rup->ru_inblock = 0; 1590360SN/A rup->ru_oublock = 0; 1591360SN/A rup->ru_msgsnd = 0; 1592360SN/A rup->ru_msgrcv = 0; 1593360SN/A rup->ru_nsignals = 0; 1594360SN/A rup->ru_nvcsw = 0; 1595360SN/A rup->ru_nivcsw = 0; 1596360SN/A 15973670Sbinkertn@umich.edu switch (who) { 15983670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 159910796Sbrandon.potter@amd.com getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 16008737Skoansin.tan@gmail.com rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 16018737Skoansin.tan@gmail.com rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 16023670Sbinkertn@umich.edu break; 16033670Sbinkertn@umich.edu 16043670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 16053670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 16063670Sbinkertn@umich.edu break; 16073670Sbinkertn@umich.edu 16083670Sbinkertn@umich.edu default: 16093670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 16103670Sbinkertn@umich.edu // plow ahead 16113670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 16123670Sbinkertn@umich.edu who); 16133670Sbinkertn@umich.edu } 16143670Sbinkertn@umich.edu 16158706Sandreas.hansson@arm.com rup.copyOut(tc->getMemProxy()); 1616360SN/A 16171458SN/A return 0; 1618360SN/A} 1619360SN/A 16206683Stjones1@inf.ed.ac.uk/// Target times() function. 16216683Stjones1@inf.ed.ac.uktemplate <class OS> 16226683Stjones1@inf.ed.ac.ukSyscallReturn 16236683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 16246683Stjones1@inf.ed.ac.uk ThreadContext *tc) 16256683Stjones1@inf.ed.ac.uk{ 16266701Sgblack@eecs.umich.edu int index = 0; 16276701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 16286683Stjones1@inf.ed.ac.uk 16296683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 16307823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 16316683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 16326683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 16336683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 16346683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 16356683Stjones1@inf.ed.ac.uk 16366683Stjones1@inf.ed.ac.uk // Convert to host endianness 16378737Skoansin.tan@gmail.com bufp->tms_utime = TheISA::htog(bufp->tms_utime); 16386683Stjones1@inf.ed.ac.uk 16396683Stjones1@inf.ed.ac.uk // Write back 16408706Sandreas.hansson@arm.com bufp.copyOut(tc->getMemProxy()); 16416683Stjones1@inf.ed.ac.uk 16426683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 16436683Stjones1@inf.ed.ac.uk return clocks; 16446683Stjones1@inf.ed.ac.uk} 16452553SN/A 16466684Stjones1@inf.ed.ac.uk/// Target time() function. 16476684Stjones1@inf.ed.ac.uktemplate <class OS> 16486684Stjones1@inf.ed.ac.ukSyscallReturn 16496684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 16506684Stjones1@inf.ed.ac.uk ThreadContext *tc) 16516684Stjones1@inf.ed.ac.uk{ 16526684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 165310796Sbrandon.potter@amd.com getElapsedTimeMicro(sec, usec); 16546684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 16556684Stjones1@inf.ed.ac.uk 16566701Sgblack@eecs.umich.edu int index = 0; 16576701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 165811321Ssteve.reinhardt@amd.com if (taddr != 0) { 16596684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 16608737Skoansin.tan@gmail.com t = TheISA::htog(t); 16618852Sandreas.hansson@arm.com SETranslatingPortProxy &p = tc->getMemProxy(); 16628852Sandreas.hansson@arm.com p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 16636684Stjones1@inf.ed.ac.uk } 16646684Stjones1@inf.ed.ac.uk return sec; 16656684Stjones1@inf.ed.ac.uk} 16662553SN/A 16672553SN/A 16681354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1669