syscall_emul.hh revision 11624
1360SN/A/* 210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 410027SChris.Adeniyi-Jones@arm.com * All rights reserved 510027SChris.Adeniyi-Jones@arm.com * 610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall 710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual 810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating 910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software 1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder. You may use the software subject to the license 1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated 1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software, 1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form. 1410027SChris.Adeniyi-Jones@arm.com * 151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 16360SN/A * All rights reserved. 17360SN/A * 18360SN/A * Redistribution and use in source and binary forms, with or without 19360SN/A * modification, are permitted provided that the following conditions are 20360SN/A * met: redistributions of source code must retain the above copyright 21360SN/A * notice, this list of conditions and the following disclaimer; 22360SN/A * redistributions in binary form must reproduce the above copyright 23360SN/A * notice, this list of conditions and the following disclaimer in the 24360SN/A * documentation and/or other materials provided with the distribution; 25360SN/A * neither the name of the copyright holders nor the names of its 26360SN/A * contributors may be used to endorse or promote products derived from 27360SN/A * this software without specific prior written permission. 28360SN/A * 29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 422665Ssaidi@eecs.umich.edu * Kevin Lim 43360SN/A */ 44360SN/A 451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 461354SN/A#define __SIM_SYSCALL_EMUL_HH__ 47360SN/A 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 7786701Sgblack@eecs.umich.edu sysinfo->uptime=seconds_since_epoch; 7796701Sgblack@eecs.umich.edu sysinfo->totalram=process->system->memSize(); 7806640Svince@csl.cornell.edu 7818706Sandreas.hansson@arm.com sysinfo.copyOut(tc->getMemProxy()); 7826640Svince@csl.cornell.edu 7836701Sgblack@eecs.umich.edu return 0; 7846640Svince@csl.cornell.edu} 785360SN/A 7861999SN/A/// Target chmod() handler. 7871999SN/Atemplate <class OS> 7881999SN/ASyscallReturn 7893114Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 7902680Sktlim@umich.edu ThreadContext *tc) 7911999SN/A{ 7921999SN/A std::string path; 7931999SN/A 7946701Sgblack@eecs.umich.edu int index = 0; 7958852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 7966701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 7971999SN/A return -EFAULT; 7986701Sgblack@eecs.umich.edu } 7991999SN/A 8006701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 8011999SN/A mode_t hostMode = 0; 8021999SN/A 8031999SN/A // XXX translate mode flags via OS::something??? 8041999SN/A hostMode = mode; 8051999SN/A 8063669Sbinkertn@umich.edu // Adjust path for current working directory 8073669Sbinkertn@umich.edu path = process->fullPath(path); 8083669Sbinkertn@umich.edu 8091999SN/A // do the chmod 8101999SN/A int result = chmod(path.c_str(), hostMode); 8111999SN/A if (result < 0) 8122218SN/A return -errno; 8131999SN/A 8141999SN/A return 0; 8151999SN/A} 8161999SN/A 8171999SN/A 8181999SN/A/// Target fchmod() handler. 8191999SN/Atemplate <class OS> 8201999SN/ASyscallReturn 8213114Sgblack@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 8222680Sktlim@umich.edu ThreadContext *tc) 8231999SN/A{ 8246701Sgblack@eecs.umich.edu int index = 0; 82510931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 82610931Sbrandon.potter@amd.com uint32_t mode = process->getSyscallArg(tc, index); 82710931Sbrandon.potter@amd.com 82810932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 82910931Sbrandon.potter@amd.com if (sim_fd < 0) 8301999SN/A return -EBADF; 8311999SN/A 8321999SN/A mode_t hostMode = 0; 8331999SN/A 8341999SN/A // XXX translate mode flags via OS::someting??? 8351999SN/A hostMode = mode; 8361999SN/A 8371999SN/A // do the fchmod 83810931Sbrandon.potter@amd.com int result = fchmod(sim_fd, hostMode); 8391999SN/A if (result < 0) 8402218SN/A return -errno; 8411999SN/A 8421999SN/A return 0; 8431999SN/A} 8441999SN/A 8455877Shsul@eecs.umich.edu/// Target mremap() handler. 8465877Shsul@eecs.umich.edutemplate <class OS> 8475877Shsul@eecs.umich.eduSyscallReturn 8485877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, LiveProcess *process, ThreadContext *tc) 8495877Shsul@eecs.umich.edu{ 8506701Sgblack@eecs.umich.edu int index = 0; 8516701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 8526701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 8536701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 8546701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 85510027SChris.Adeniyi-Jones@arm.com uint64_t provided_address = 0; 85610027SChris.Adeniyi-Jones@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 85710027SChris.Adeniyi-Jones@arm.com 85810027SChris.Adeniyi-Jones@arm.com if (use_provided_address) 85910027SChris.Adeniyi-Jones@arm.com provided_address = process->getSyscallArg(tc, index); 8605877Shsul@eecs.umich.edu 86110318Sandreas.hansson@arm.com if ((start % TheISA::PageBytes != 0) || 86210318Sandreas.hansson@arm.com (provided_address % TheISA::PageBytes != 0)) { 8635877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 8645877Shsul@eecs.umich.edu return -EINVAL; 8655877Shsul@eecs.umich.edu } 8665877Shsul@eecs.umich.edu 86710486Stjablin@gmail.com new_length = roundUp(new_length, TheISA::PageBytes); 86810486Stjablin@gmail.com 8695877Shsul@eecs.umich.edu if (new_length > old_length) { 87010027SChris.Adeniyi-Jones@arm.com if ((start + old_length) == process->mmap_end && 87110027SChris.Adeniyi-Jones@arm.com (!use_provided_address || provided_address == start)) { 8725877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 8738601Ssteve.reinhardt@amd.com process->allocateMem(process->mmap_end, diff); 8745877Shsul@eecs.umich.edu process->mmap_end += diff; 8755877Shsul@eecs.umich.edu return start; 8765877Shsul@eecs.umich.edu } else { 87710027SChris.Adeniyi-Jones@arm.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 8785877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 8795877Shsul@eecs.umich.edu return -ENOMEM; 8805877Shsul@eecs.umich.edu } else { 88110027SChris.Adeniyi-Jones@arm.com uint64_t new_start = use_provided_address ? 88210027SChris.Adeniyi-Jones@arm.com provided_address : process->mmap_end; 88310027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, old_length, new_start); 88410027SChris.Adeniyi-Jones@arm.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 88510027SChris.Adeniyi-Jones@arm.com new_start, new_start + new_length, 88610027SChris.Adeniyi-Jones@arm.com new_length - old_length); 8875877Shsul@eecs.umich.edu // add on the remaining unallocated pages 88810027SChris.Adeniyi-Jones@arm.com process->allocateMem(new_start + old_length, 88910027SChris.Adeniyi-Jones@arm.com new_length - old_length, 89010027SChris.Adeniyi-Jones@arm.com use_provided_address /* clobber */); 89110027SChris.Adeniyi-Jones@arm.com if (!use_provided_address) 89210027SChris.Adeniyi-Jones@arm.com process->mmap_end += new_length; 89310027SChris.Adeniyi-Jones@arm.com if (use_provided_address && 89410027SChris.Adeniyi-Jones@arm.com new_start + new_length > process->mmap_end) { 89510027SChris.Adeniyi-Jones@arm.com // something fishy going on here, at least notify the user 89610027SChris.Adeniyi-Jones@arm.com // @todo: increase mmap_end? 89710027SChris.Adeniyi-Jones@arm.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 89810027SChris.Adeniyi-Jones@arm.com } 89910027SChris.Adeniyi-Jones@arm.com warn("returning %08p as start\n", new_start); 90010027SChris.Adeniyi-Jones@arm.com return new_start; 9015877Shsul@eecs.umich.edu } 9025877Shsul@eecs.umich.edu } 9035877Shsul@eecs.umich.edu } else { 90410027SChris.Adeniyi-Jones@arm.com if (use_provided_address && provided_address != start) 90510027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 9068601Ssteve.reinhardt@amd.com process->pTable->unmap(start + new_length, old_length - new_length); 90710027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 9085877Shsul@eecs.umich.edu } 9095877Shsul@eecs.umich.edu} 9101999SN/A 911378SN/A/// Target stat() handler. 912360SN/Atemplate <class OS> 9131450SN/ASyscallReturn 9143114Sgblack@eecs.umich.edustatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 9152680Sktlim@umich.edu ThreadContext *tc) 916360SN/A{ 917360SN/A std::string path; 918360SN/A 9196701Sgblack@eecs.umich.edu int index = 0; 9208852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 9216701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9226701Sgblack@eecs.umich.edu return -EFAULT; 9236701Sgblack@eecs.umich.edu } 9246701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 925360SN/A 9263669Sbinkertn@umich.edu // Adjust path for current working directory 9273669Sbinkertn@umich.edu path = process->fullPath(path); 9283669Sbinkertn@umich.edu 929360SN/A struct stat hostBuf; 930360SN/A int result = stat(path.c_str(), &hostBuf); 931360SN/A 932360SN/A if (result < 0) 9332218SN/A return -errno; 934360SN/A 9358706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 936360SN/A 9371458SN/A return 0; 938360SN/A} 939360SN/A 940360SN/A 9415074Ssaidi@eecs.umich.edu/// Target stat64() handler. 9425074Ssaidi@eecs.umich.edutemplate <class OS> 9435074Ssaidi@eecs.umich.eduSyscallReturn 9445074Ssaidi@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 9455074Ssaidi@eecs.umich.edu ThreadContext *tc) 9465074Ssaidi@eecs.umich.edu{ 9475074Ssaidi@eecs.umich.edu std::string path; 9485074Ssaidi@eecs.umich.edu 9496701Sgblack@eecs.umich.edu int index = 0; 9508852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 9516701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 9525074Ssaidi@eecs.umich.edu return -EFAULT; 9536701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9545074Ssaidi@eecs.umich.edu 9555074Ssaidi@eecs.umich.edu // Adjust path for current working directory 9565074Ssaidi@eecs.umich.edu path = process->fullPath(path); 9575074Ssaidi@eecs.umich.edu 9585208Ssaidi@eecs.umich.edu#if NO_STAT64 9595208Ssaidi@eecs.umich.edu struct stat hostBuf; 9605208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 9615208Ssaidi@eecs.umich.edu#else 9625074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 9635074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 9645208Ssaidi@eecs.umich.edu#endif 9655074Ssaidi@eecs.umich.edu 9665074Ssaidi@eecs.umich.edu if (result < 0) 9675074Ssaidi@eecs.umich.edu return -errno; 9685074Ssaidi@eecs.umich.edu 9698706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 9705074Ssaidi@eecs.umich.edu 9715074Ssaidi@eecs.umich.edu return 0; 9725074Ssaidi@eecs.umich.edu} 9735074Ssaidi@eecs.umich.edu 9745074Ssaidi@eecs.umich.edu 97510027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler. 97610027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 97710027SChris.Adeniyi-Jones@arm.comSyscallReturn 97810027SChris.Adeniyi-Jones@arm.comfstatat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 97910027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 98010027SChris.Adeniyi-Jones@arm.com{ 98110027SChris.Adeniyi-Jones@arm.com int index = 0; 98210027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 98310027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 98410793Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 98510027SChris.Adeniyi-Jones@arm.com 98610027SChris.Adeniyi-Jones@arm.com std::string path; 98710027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 98810027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) 98910027SChris.Adeniyi-Jones@arm.com return -EFAULT; 99010027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 99110027SChris.Adeniyi-Jones@arm.com 99210027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 99310027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 99410027SChris.Adeniyi-Jones@arm.com 99510027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 99610027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 99710027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 99810027SChris.Adeniyi-Jones@arm.com#else 99910027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 100010027SChris.Adeniyi-Jones@arm.com int result = stat64(path.c_str(), &hostBuf); 100110027SChris.Adeniyi-Jones@arm.com#endif 100210027SChris.Adeniyi-Jones@arm.com 100310027SChris.Adeniyi-Jones@arm.com if (result < 0) 100410027SChris.Adeniyi-Jones@arm.com return -errno; 100510027SChris.Adeniyi-Jones@arm.com 100610027SChris.Adeniyi-Jones@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 100710027SChris.Adeniyi-Jones@arm.com 100810027SChris.Adeniyi-Jones@arm.com return 0; 100910027SChris.Adeniyi-Jones@arm.com} 101010027SChris.Adeniyi-Jones@arm.com 101110027SChris.Adeniyi-Jones@arm.com 10121999SN/A/// Target fstat64() handler. 10131999SN/Atemplate <class OS> 10141999SN/ASyscallReturn 10153114Sgblack@eecs.umich.edufstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 10162680Sktlim@umich.edu ThreadContext *tc) 10171999SN/A{ 10186701Sgblack@eecs.umich.edu int index = 0; 101910931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 10206701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 102110931Sbrandon.potter@amd.com 102210932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 102310931Sbrandon.potter@amd.com if (sim_fd < 0) 10241999SN/A return -EBADF; 10251999SN/A 10262764Sstever@eecs.umich.edu#if NO_STAT64 10272064SN/A struct stat hostBuf; 102810931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 10292064SN/A#else 10302064SN/A struct stat64 hostBuf; 103110931Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 10322064SN/A#endif 10331999SN/A 10341999SN/A if (result < 0) 10352218SN/A return -errno; 10361999SN/A 103710931Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 10381999SN/A 10391999SN/A return 0; 10401999SN/A} 10411999SN/A 10421999SN/A 1043378SN/A/// Target lstat() handler. 1044360SN/Atemplate <class OS> 10451450SN/ASyscallReturn 10463114Sgblack@eecs.umich.edulstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 10472680Sktlim@umich.edu ThreadContext *tc) 1048360SN/A{ 1049360SN/A std::string path; 1050360SN/A 10516701Sgblack@eecs.umich.edu int index = 0; 10528852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 10536701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 10546701Sgblack@eecs.umich.edu return -EFAULT; 10556701Sgblack@eecs.umich.edu } 10566701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1057360SN/A 10583669Sbinkertn@umich.edu // Adjust path for current working directory 10593669Sbinkertn@umich.edu path = process->fullPath(path); 10603669Sbinkertn@umich.edu 1061360SN/A struct stat hostBuf; 1062360SN/A int result = lstat(path.c_str(), &hostBuf); 1063360SN/A 1064360SN/A if (result < 0) 10651458SN/A return -errno; 1066360SN/A 10678706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1068360SN/A 10691458SN/A return 0; 1070360SN/A} 1071360SN/A 10721999SN/A/// Target lstat64() handler. 10731999SN/Atemplate <class OS> 10741999SN/ASyscallReturn 10753114Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, LiveProcess *process, 10762680Sktlim@umich.edu ThreadContext *tc) 10771999SN/A{ 10781999SN/A std::string path; 10791999SN/A 10806701Sgblack@eecs.umich.edu int index = 0; 10818852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 10826701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 10836701Sgblack@eecs.umich.edu return -EFAULT; 10846701Sgblack@eecs.umich.edu } 10856701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 10861999SN/A 10873669Sbinkertn@umich.edu // Adjust path for current working directory 10883669Sbinkertn@umich.edu path = process->fullPath(path); 10893669Sbinkertn@umich.edu 10902764Sstever@eecs.umich.edu#if NO_STAT64 10912064SN/A struct stat hostBuf; 10922064SN/A int result = lstat(path.c_str(), &hostBuf); 10932064SN/A#else 10941999SN/A struct stat64 hostBuf; 10951999SN/A int result = lstat64(path.c_str(), &hostBuf); 10962064SN/A#endif 10971999SN/A 10981999SN/A if (result < 0) 10991999SN/A return -errno; 11001999SN/A 11018706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11021999SN/A 11031999SN/A return 0; 11041999SN/A} 11051999SN/A 1106378SN/A/// Target fstat() handler. 1107360SN/Atemplate <class OS> 11081450SN/ASyscallReturn 11093114Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11102680Sktlim@umich.edu ThreadContext *tc) 1111360SN/A{ 11126701Sgblack@eecs.umich.edu int index = 0; 111310931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 11146701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1115360SN/A 111611380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1117360SN/A 111810932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 111910931Sbrandon.potter@amd.com if (sim_fd < 0) 11201458SN/A return -EBADF; 1121360SN/A 1122360SN/A struct stat hostBuf; 112310931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 1124360SN/A 1125360SN/A if (result < 0) 11261458SN/A return -errno; 1127360SN/A 112810931Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 11292021SN/A 11301458SN/A return 0; 1131360SN/A} 1132360SN/A 1133360SN/A 11341706SN/A/// Target statfs() handler. 11351706SN/Atemplate <class OS> 11361706SN/ASyscallReturn 11373114Sgblack@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11382680Sktlim@umich.edu ThreadContext *tc) 11391706SN/A{ 11401706SN/A std::string path; 11411706SN/A 11426701Sgblack@eecs.umich.edu int index = 0; 11438852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 11446701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11456701Sgblack@eecs.umich.edu return -EFAULT; 11466701Sgblack@eecs.umich.edu } 11476701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11481706SN/A 11493669Sbinkertn@umich.edu // Adjust path for current working directory 11503669Sbinkertn@umich.edu path = process->fullPath(path); 11513669Sbinkertn@umich.edu 11521706SN/A struct statfs hostBuf; 11531706SN/A int result = statfs(path.c_str(), &hostBuf); 11541706SN/A 11551706SN/A if (result < 0) 11562218SN/A return -errno; 11571706SN/A 11588706Sandreas.hansson@arm.com OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf); 11591706SN/A 11601706SN/A return 0; 11611706SN/A} 11621706SN/A 11631706SN/A 11641706SN/A/// Target fstatfs() handler. 11651706SN/Atemplate <class OS> 11661706SN/ASyscallReturn 11673114Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11682680Sktlim@umich.edu ThreadContext *tc) 11691706SN/A{ 11706701Sgblack@eecs.umich.edu int index = 0; 117110931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 11726701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11731706SN/A 117410932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 117510931Sbrandon.potter@amd.com if (sim_fd < 0) 11761706SN/A return -EBADF; 11771706SN/A 11781706SN/A struct statfs hostBuf; 117910931Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 11801706SN/A 11811706SN/A if (result < 0) 11822218SN/A return -errno; 11831706SN/A 11848706Sandreas.hansson@arm.com OS::copyOutStatfsBuf(tc->getMemProxy(), bufPtr, &hostBuf); 11851706SN/A 11861706SN/A return 0; 11871706SN/A} 11881706SN/A 11891706SN/A 11901999SN/A/// Target writev() handler. 11911999SN/Atemplate <class OS> 11921999SN/ASyscallReturn 11933114Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 11942680Sktlim@umich.edu ThreadContext *tc) 11951999SN/A{ 11966701Sgblack@eecs.umich.edu int index = 0; 119710931Sbrandon.potter@amd.com int tgt_fd = process->getSyscallArg(tc, index); 119810931Sbrandon.potter@amd.com 119910932Sbrandon.potter@amd.com int sim_fd = process->getSimFD(tgt_fd); 120010931Sbrandon.potter@amd.com if (sim_fd < 0) 12011999SN/A return -EBADF; 12021999SN/A 12038852Sandreas.hansson@arm.com SETranslatingPortProxy &p = tc->getMemProxy(); 12046701Sgblack@eecs.umich.edu uint64_t tiov_base = process->getSyscallArg(tc, index); 12056701Sgblack@eecs.umich.edu size_t count = process->getSyscallArg(tc, index); 12061999SN/A struct iovec hiov[count]; 12076227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 12081999SN/A typename OS::tgt_iovec tiov; 12092461SN/A 12108852Sandreas.hansson@arm.com p.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 12118852Sandreas.hansson@arm.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 12128737Skoansin.tan@gmail.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 12131999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 12148852Sandreas.hansson@arm.com p.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 12158852Sandreas.hansson@arm.com hiov[i].iov_len); 12161999SN/A } 12171999SN/A 121810931Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 12191999SN/A 12206227Snate@binkert.org for (size_t i = 0; i < count; ++i) 12211999SN/A delete [] (char *)hiov[i].iov_base; 12221999SN/A 12231999SN/A if (result < 0) 12242218SN/A return -errno; 12251999SN/A 122610629Sjthestness@gmail.com return result; 12271999SN/A} 12281999SN/A 122911385Sbrandon.potter@amd.com/// Real mmap handler. 1230360SN/Atemplate <class OS> 12311450SN/ASyscallReturn 123211385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc, 123311385Sbrandon.potter@amd.com bool is_mmap2) 1234360SN/A{ 12356701Sgblack@eecs.umich.edu int index = 0; 12366701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 12376701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 123811383Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 123911383Sbrandon.potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 12408324Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 124110486Stjablin@gmail.com int offset = p->getSyscallArg(tc, index); 1242360SN/A 124311385Sbrandon.potter@amd.com if (is_mmap2) 124411385Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 12459008Sgblack@eecs.umich.edu 124611383Sbrandon.potter@amd.com if (start & (TheISA::PageBytes - 1) || 124711383Sbrandon.potter@amd.com offset & (TheISA::PageBytes - 1) || 124811383Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 124911383Sbrandon.potter@amd.com tgt_flags & OS::TGT_MAP_SHARED) || 125011383Sbrandon.potter@amd.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 125111383Sbrandon.potter@amd.com !(tgt_flags & OS::TGT_MAP_SHARED)) || 125211383Sbrandon.potter@amd.com !length) { 125311383Sbrandon.potter@amd.com return -EINVAL; 125411383Sbrandon.potter@amd.com } 12558324Ssteve.reinhardt@amd.com 125611383Sbrandon.potter@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 125711383Sbrandon.potter@amd.com // With shared mmaps, there are two cases to consider: 125811383Sbrandon.potter@amd.com // 1) anonymous: writes should modify the mapping and this should be 125911383Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 126011383Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 126111383Sbrandon.potter@amd.com // structure which maintains information about the which virtual 126211383Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 126311383Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 126411383Sbrandon.potter@amd.com // 2) file-backed: writes should modify the mapping and the file 126511383Sbrandon.potter@amd.com // which is backed by the mapping. The shared mapping problem is the 126611383Sbrandon.potter@amd.com // same as what was mentioned about the anonymous mappings. For 126711383Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 126811383Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 126911383Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 127011383Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 127111383Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 127211383Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 127311383Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 127411383Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 127511383Sbrandon.potter@amd.com // a call to msync, but that again would require more information 127611383Sbrandon.potter@amd.com // than we currently maintain. 127711383Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 127811383Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 127911383Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 12808324Ssteve.reinhardt@amd.com } 12815877Shsul@eecs.umich.edu 128210486Stjablin@gmail.com length = roundUp(length, TheISA::PageBytes); 128310486Stjablin@gmail.com 128411383Sbrandon.potter@amd.com int sim_fd = -1; 128511383Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 128611383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 128711624Smichael.lebeane@amd.com // Check for EmulatedDriver mmap 128811624Smichael.lebeane@amd.com FDEntry *fde = p->getFDEntry(tgt_fd); 128911624Smichael.lebeane@amd.com if (fde == NULL) 129011624Smichael.lebeane@amd.com return -EBADF; 129111624Smichael.lebeane@amd.com 129211624Smichael.lebeane@amd.com if (fde->driver != NULL) { 129311624Smichael.lebeane@amd.com return fde->driver->mmap(p, tc, start, length, prot, 129411624Smichael.lebeane@amd.com tgt_flags, tgt_fd, offset); 129511624Smichael.lebeane@amd.com } 129611624Smichael.lebeane@amd.com sim_fd = fde->fd; 129711624Smichael.lebeane@amd.com 129811383Sbrandon.potter@amd.com if (sim_fd < 0) 129911383Sbrandon.potter@amd.com return -EBADF; 1300360SN/A 130111383Sbrandon.potter@amd.com pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE, 130211383Sbrandon.potter@amd.com sim_fd, offset); 13038600Ssteve.reinhardt@amd.com 130411383Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 130511383Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 130611383Sbrandon.potter@amd.com return -errno; 13078600Ssteve.reinhardt@amd.com } 13082544SN/A } 13092544SN/A 131011383Sbrandon.potter@amd.com // Extend global mmap region if necessary. Note that we ignore the 131111383Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 131211383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 131311386Ssteve.reinhardt@amd.com start = p->mmapGrowsDown() ? p->mmap_end - length : p->mmap_end; 131411386Ssteve.reinhardt@amd.com p->mmap_end = p->mmapGrowsDown() ? start : p->mmap_end + length; 131511383Sbrandon.potter@amd.com } 131611383Sbrandon.potter@amd.com 131711383Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 131811383Sbrandon.potter@amd.com start, start + length - 1); 131911383Sbrandon.potter@amd.com 132011383Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 132111383Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 132211383Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 132311383Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 132411383Sbrandon.potter@amd.com if (clobber) { 132511383Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 132611383Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 132711383Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 132811383Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 132911383Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 13308600Ssteve.reinhardt@amd.com } 13316672Sgblack@eecs.umich.edu } 13328600Ssteve.reinhardt@amd.com 133311383Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 133411383Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 133511383Sbrandon.potter@amd.com // fatal and bail out of the simulation. 13368601Ssteve.reinhardt@amd.com p->allocateMem(start, length, clobber); 13372544SN/A 133811383Sbrandon.potter@amd.com // Transfer content into target address space. 133911383Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 134011383Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 134111383Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 134211383Sbrandon.potter@amd.com // with something like: 134311383Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 134411383Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 134511383Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 134611383Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 134711383Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 134811383Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 134911383Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 135011383Sbrandon.potter@amd.com } else { 135111383Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 135211383Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 135311383Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 135411383Sbrandon.potter@amd.com // the host into the target address space. 135511383Sbrandon.potter@amd.com struct stat file_stat; 135611383Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 135711383Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 135811383Sbrandon.potter@amd.com 135911383Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 136011383Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 136111383Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 136211383Sbrandon.potter@amd.com // on the specified offset into the file. 136311383Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 136411383Sbrandon.potter@amd.com length); 136511383Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 136611383Sbrandon.potter@amd.com 136711383Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 136811383Sbrandon.potter@amd.com munmap(pmap, length); 136911383Sbrandon.potter@amd.com 137011392Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 137111392Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 137211392Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 137311392Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 137411392Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 137511392Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 137611392Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 137711392Sbrandon.potter@amd.com if (interpreter) { 137811392Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 137911392Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 138011392Sbrandon.potter@amd.com 138111392Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 138211392Sbrandon.potter@amd.com 138311392Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 138411392Sbrandon.potter@amd.com FDEntry *fde = p->getFDEntry(tgt_fd); 138511392Sbrandon.potter@amd.com 138611392Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(fde->filename); 138711392Sbrandon.potter@amd.com 138811392Sbrandon.potter@amd.com if (lib) { 138911392Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 139011392Sbrandon.potter@amd.com lib->textBase(), start); 139111392Sbrandon.potter@amd.com } 139211392Sbrandon.potter@amd.com } 139311392Sbrandon.potter@amd.com } 139411392Sbrandon.potter@amd.com 139511383Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 139611383Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 139711383Sbrandon.potter@amd.com // execution (hopefully). 139811383Sbrandon.potter@amd.com } 139911383Sbrandon.potter@amd.com 14001458SN/A return start; 1401360SN/A} 1402360SN/A 140311593Santhony.gutierrez@amd.comtemplate <class OS> 140411593Santhony.gutierrez@amd.comSyscallReturn 140511593Santhony.gutierrez@amd.compwrite64Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 140611593Santhony.gutierrez@amd.com{ 140711593Santhony.gutierrez@amd.com int index = 0; 140811593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 140911593Santhony.gutierrez@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 141011593Santhony.gutierrez@amd.com int nbytes = p->getSyscallArg(tc, index); 141111593Santhony.gutierrez@amd.com int offset = p->getSyscallArg(tc, index); 141211593Santhony.gutierrez@amd.com 141311593Santhony.gutierrez@amd.com int sim_fd = p->getSimFD(tgt_fd); 141411593Santhony.gutierrez@amd.com if (sim_fd < 0) 141511593Santhony.gutierrez@amd.com return -EBADF; 141611593Santhony.gutierrez@amd.com 141711593Santhony.gutierrez@amd.com BufferArg bufArg(bufPtr, nbytes); 141811593Santhony.gutierrez@amd.com bufArg.copyIn(tc->getMemProxy()); 141911593Santhony.gutierrez@amd.com 142011594Santhony.gutierrez@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 142111593Santhony.gutierrez@amd.com 142211593Santhony.gutierrez@amd.com return (bytes_written == -1) ? -errno : bytes_written; 142311593Santhony.gutierrez@amd.com} 142411593Santhony.gutierrez@amd.com 142511385Sbrandon.potter@amd.com/// Target mmap() handler. 142611385Sbrandon.potter@amd.comtemplate <class OS> 142711385Sbrandon.potter@amd.comSyscallReturn 142811385Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 142911385Sbrandon.potter@amd.com{ 143011385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, false); 143111385Sbrandon.potter@amd.com} 143211385Sbrandon.potter@amd.com 143311385Sbrandon.potter@amd.com/// Target mmap2() handler. 143411385Sbrandon.potter@amd.comtemplate <class OS> 143511385Sbrandon.potter@amd.comSyscallReturn 143611385Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 143711385Sbrandon.potter@amd.com{ 143811385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 143911385Sbrandon.potter@amd.com} 144011385Sbrandon.potter@amd.com 1441378SN/A/// Target getrlimit() handler. 1442360SN/Atemplate <class OS> 14431450SN/ASyscallReturn 14443114Sgblack@eecs.umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 14452680Sktlim@umich.edu ThreadContext *tc) 1446360SN/A{ 14476701Sgblack@eecs.umich.edu int index = 0; 14486701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 14496701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1450360SN/A 1451360SN/A switch (resource) { 14522064SN/A case OS::TGT_RLIMIT_STACK: 14535877Shsul@eecs.umich.edu // max stack size in bytes: make up a number (8MB for now) 14542064SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 14558737Skoansin.tan@gmail.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 14568737Skoansin.tan@gmail.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 14572064SN/A break; 1458360SN/A 14595877Shsul@eecs.umich.edu case OS::TGT_RLIMIT_DATA: 14605877Shsul@eecs.umich.edu // max data segment size in bytes: make up a number 14615877Shsul@eecs.umich.edu rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 14628737Skoansin.tan@gmail.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 14638737Skoansin.tan@gmail.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 14645877Shsul@eecs.umich.edu break; 14655877Shsul@eecs.umich.edu 14662064SN/A default: 146710794Sbrandon.potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 146810794Sbrandon.potter@amd.com return -EINVAL; 14692064SN/A break; 1470360SN/A } 1471360SN/A 14728706Sandreas.hansson@arm.com rlp.copyOut(tc->getMemProxy()); 14731458SN/A return 0; 1474360SN/A} 1475360SN/A 147610796Sbrandon.potter@amd.com/// Target clock_gettime() function. 147710796Sbrandon.potter@amd.comtemplate <class OS> 147810796Sbrandon.potter@amd.comSyscallReturn 147910796Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 148010796Sbrandon.potter@amd.com{ 148110796Sbrandon.potter@amd.com int index = 1; 148210796Sbrandon.potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 148310796Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 148410796Sbrandon.potter@amd.com 148510796Sbrandon.potter@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 148610796Sbrandon.potter@amd.com tp->tv_sec += seconds_since_epoch; 148710796Sbrandon.potter@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 148810796Sbrandon.potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 148910796Sbrandon.potter@amd.com 149010796Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 149110796Sbrandon.potter@amd.com 149210796Sbrandon.potter@amd.com return 0; 149310796Sbrandon.potter@amd.com} 149410796Sbrandon.potter@amd.com 149511337SMichael.Lebeane@amd.com/// Target clock_getres() function. 149611337SMichael.Lebeane@amd.comtemplate <class OS> 149711337SMichael.Lebeane@amd.comSyscallReturn 149811337SMichael.Lebeane@amd.comclock_getresFunc(SyscallDesc *desc, int num, LiveProcess *p, ThreadContext *tc) 149911337SMichael.Lebeane@amd.com{ 150011337SMichael.Lebeane@amd.com int index = 1; 150111337SMichael.Lebeane@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 150211337SMichael.Lebeane@amd.com 150311337SMichael.Lebeane@amd.com // Set resolution at ns, which is what clock_gettime() returns 150411337SMichael.Lebeane@amd.com tp->tv_sec = 0; 150511337SMichael.Lebeane@amd.com tp->tv_nsec = 1; 150611337SMichael.Lebeane@amd.com 150711337SMichael.Lebeane@amd.com tp.copyOut(tc->getMemProxy()); 150811337SMichael.Lebeane@amd.com 150911337SMichael.Lebeane@amd.com return 0; 151011337SMichael.Lebeane@amd.com} 151111337SMichael.Lebeane@amd.com 1512378SN/A/// Target gettimeofday() handler. 1513360SN/Atemplate <class OS> 15141450SN/ASyscallReturn 15153114Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 15162680Sktlim@umich.edu ThreadContext *tc) 1517360SN/A{ 15186701Sgblack@eecs.umich.edu int index = 0; 15196701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1520360SN/A 152110796Sbrandon.potter@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1522360SN/A tp->tv_sec += seconds_since_epoch; 15236109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 15246109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1525360SN/A 15268706Sandreas.hansson@arm.com tp.copyOut(tc->getMemProxy()); 1527360SN/A 15281458SN/A return 0; 1529360SN/A} 1530360SN/A 1531360SN/A 15321999SN/A/// Target utimes() handler. 15331999SN/Atemplate <class OS> 15341999SN/ASyscallReturn 15353114Sgblack@eecs.umich.eduutimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 15362680Sktlim@umich.edu ThreadContext *tc) 15371999SN/A{ 15381999SN/A std::string path; 15391999SN/A 15406701Sgblack@eecs.umich.edu int index = 0; 15418852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 15426701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 15436701Sgblack@eecs.umich.edu return -EFAULT; 15446701Sgblack@eecs.umich.edu } 15451999SN/A 15466701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 15476701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 15488706Sandreas.hansson@arm.com tp.copyIn(tc->getMemProxy()); 15491999SN/A 15501999SN/A struct timeval hostTimeval[2]; 15511999SN/A for (int i = 0; i < 2; ++i) 15521999SN/A { 15538737Skoansin.tan@gmail.com hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 15548737Skoansin.tan@gmail.com hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 15551999SN/A } 15563669Sbinkertn@umich.edu 15573669Sbinkertn@umich.edu // Adjust path for current working directory 15583669Sbinkertn@umich.edu path = process->fullPath(path); 15593669Sbinkertn@umich.edu 15601999SN/A int result = utimes(path.c_str(), hostTimeval); 15611999SN/A 15621999SN/A if (result < 0) 15631999SN/A return -errno; 15641999SN/A 15651999SN/A return 0; 15661999SN/A} 1567378SN/A/// Target getrusage() function. 1568360SN/Atemplate <class OS> 15691450SN/ASyscallReturn 15703114Sgblack@eecs.umich.edugetrusageFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 15712680Sktlim@umich.edu ThreadContext *tc) 1572360SN/A{ 15736701Sgblack@eecs.umich.edu int index = 0; 15746701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 15756701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1576360SN/A 15773670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 15783670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 1579360SN/A rup->ru_stime.tv_sec = 0; 1580360SN/A rup->ru_stime.tv_usec = 0; 1581360SN/A rup->ru_maxrss = 0; 1582360SN/A rup->ru_ixrss = 0; 1583360SN/A rup->ru_idrss = 0; 1584360SN/A rup->ru_isrss = 0; 1585360SN/A rup->ru_minflt = 0; 1586360SN/A rup->ru_majflt = 0; 1587360SN/A rup->ru_nswap = 0; 1588360SN/A rup->ru_inblock = 0; 1589360SN/A rup->ru_oublock = 0; 1590360SN/A rup->ru_msgsnd = 0; 1591360SN/A rup->ru_msgrcv = 0; 1592360SN/A rup->ru_nsignals = 0; 1593360SN/A rup->ru_nvcsw = 0; 1594360SN/A rup->ru_nivcsw = 0; 1595360SN/A 15963670Sbinkertn@umich.edu switch (who) { 15973670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 159810796Sbrandon.potter@amd.com getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 15998737Skoansin.tan@gmail.com rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 16008737Skoansin.tan@gmail.com rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 16013670Sbinkertn@umich.edu break; 16023670Sbinkertn@umich.edu 16033670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 16043670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 16053670Sbinkertn@umich.edu break; 16063670Sbinkertn@umich.edu 16073670Sbinkertn@umich.edu default: 16083670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 16093670Sbinkertn@umich.edu // plow ahead 16103670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 16113670Sbinkertn@umich.edu who); 16123670Sbinkertn@umich.edu } 16133670Sbinkertn@umich.edu 16148706Sandreas.hansson@arm.com rup.copyOut(tc->getMemProxy()); 1615360SN/A 16161458SN/A return 0; 1617360SN/A} 1618360SN/A 16196683Stjones1@inf.ed.ac.uk/// Target times() function. 16206683Stjones1@inf.ed.ac.uktemplate <class OS> 16216683Stjones1@inf.ed.ac.ukSyscallReturn 16226683Stjones1@inf.ed.ac.uktimesFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 16236683Stjones1@inf.ed.ac.uk ThreadContext *tc) 16246683Stjones1@inf.ed.ac.uk{ 16256701Sgblack@eecs.umich.edu int index = 0; 16266701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 16276683Stjones1@inf.ed.ac.uk 16286683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 16297823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 16306683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 16316683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 16326683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 16336683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 16346683Stjones1@inf.ed.ac.uk 16356683Stjones1@inf.ed.ac.uk // Convert to host endianness 16368737Skoansin.tan@gmail.com bufp->tms_utime = TheISA::htog(bufp->tms_utime); 16376683Stjones1@inf.ed.ac.uk 16386683Stjones1@inf.ed.ac.uk // Write back 16398706Sandreas.hansson@arm.com bufp.copyOut(tc->getMemProxy()); 16406683Stjones1@inf.ed.ac.uk 16416683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 16426683Stjones1@inf.ed.ac.uk return clocks; 16436683Stjones1@inf.ed.ac.uk} 16442553SN/A 16456684Stjones1@inf.ed.ac.uk/// Target time() function. 16466684Stjones1@inf.ed.ac.uktemplate <class OS> 16476684Stjones1@inf.ed.ac.ukSyscallReturn 16486684Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, LiveProcess *process, 16496684Stjones1@inf.ed.ac.uk ThreadContext *tc) 16506684Stjones1@inf.ed.ac.uk{ 16516684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 165210796Sbrandon.potter@amd.com getElapsedTimeMicro(sec, usec); 16536684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 16546684Stjones1@inf.ed.ac.uk 16556701Sgblack@eecs.umich.edu int index = 0; 16566701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 165711321Ssteve.reinhardt@amd.com if (taddr != 0) { 16586684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 16598737Skoansin.tan@gmail.com t = TheISA::htog(t); 16608852Sandreas.hansson@arm.com SETranslatingPortProxy &p = tc->getMemProxy(); 16618852Sandreas.hansson@arm.com p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 16626684Stjones1@inf.ed.ac.uk } 16636684Stjones1@inf.ed.ac.uk return sec; 16646684Stjones1@inf.ed.ac.uk} 16652553SN/A 16662553SN/A 16671354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1668