syscall_emul.hh revision 12206
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#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 499202Spalle@lyckegaard.dk defined(__FreeBSD__) || defined(__CYGWIN__) || \ 509202Spalle@lyckegaard.dk defined(__NetBSD__)) 512064SN/A#define NO_STAT64 1 52360SN/A#else 53360SN/A#define NO_STAT64 0 54360SN/A#endif 55360SN/A 56360SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 57360SN/A defined(__FreeBSD__) || defined(__NetBSD__)) 581809SN/A#define NO_STATFS 1 595543Ssaidi@eecs.umich.edu#else 6011392Sbrandon.potter@amd.com#define NO_STATFS 0 611809SN/A#endif 6211392Sbrandon.potter@amd.com 6311383Sbrandon.potter@amd.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 643113Sgblack@eecs.umich.edu defined(__FreeBSD__) || defined(__NetBSD__)) 658229Snate@binkert.org#define NO_FALLOCATE 1 668229Snate@binkert.org#else 6711594Santhony.gutierrez@amd.com#define NO_FALLOCATE 0 687075Snate@binkert.org#endif 698229Snate@binkert.org 707075Snate@binkert.org/// 71360SN/A/// @file syscall_emul.hh 722474SN/A/// 735543Ssaidi@eecs.umich.edu/// This file defines objects used to emulate syscalls from the target 7411392Sbrandon.potter@amd.com/// application on the host machine. 752462SN/A 761354SN/A#ifdef __CYGWIN32__ 776216Snate@binkert.org#include <sys/fcntl.h> 786658Snate@binkert.org 792474SN/A#endif 802680Sktlim@umich.edu#include <fcntl.h> 8111380Salexandru.dutu@amd.com#include <sys/mman.h> 828232Snate@binkert.org#include <sys/stat.h> 838229Snate@binkert.org#if (NO_STATFS == 0) 847678Sgblack@eecs.umich.edu#include <sys/statfs.h> 8510496Ssteve.reinhardt@amd.com#else 868229Snate@binkert.org#include <sys/mount.h> 8710497Ssteve.reinhardt@amd.com#endif 888766Sgblack@eecs.umich.edu#include <sys/time.h> 896640Svince@csl.cornell.edu#include <sys/uio.h> 90360SN/A#include <unistd.h> 9111380Salexandru.dutu@amd.com 9211380Salexandru.dutu@amd.com#include <cerrno> 9311380Salexandru.dutu@amd.com#include <memory> 9411380Salexandru.dutu@amd.com#include <string> 9511380Salexandru.dutu@amd.com 9611380Salexandru.dutu@amd.com#include "arch/utility.hh" 9711380Salexandru.dutu@amd.com#include "base/intmath.hh" 9811380Salexandru.dutu@amd.com#include "base/loader/object_file.hh" 99360SN/A#include "base/misc.hh" 100360SN/A#include "base/trace.hh" 101360SN/A#include "base/types.hh" 102360SN/A#include "config/the_isa.hh" 103360SN/A#include "cpu/base.hh" 104360SN/A#include "cpu/thread_context.hh" 105360SN/A#include "mem/page_table.hh" 106378SN/A#include "params/Process.hh" 1071450SN/A#include "sim/emul_driver.hh" 1083114Sgblack@eecs.umich.edu#include "sim/futex_map.hh" 109360SN/A#include "sim/process.hh" 1105543Ssaidi@eecs.umich.edu#include "sim/syscall_debug_macros.hh" 1115543Ssaidi@eecs.umich.edu#include "sim/syscall_desc.hh" 1125543Ssaidi@eecs.umich.edu#include "sim/syscall_emul_buf.hh" 11310831Ssteve.reinhardt@amd.com#include "sim/syscall_return.hh" 114360SN/A 115360SN/A////////////////////////////////////////////////////////////////////// 116360SN/A// 117360SN/A// The following emulation functions are generic enough that they 118360SN/A// don't need to be recompiled for different emulated OS's. They are 1192680Sktlim@umich.edu// defined in sim/syscall_emul.cc. 120360SN/A// 12110831Ssteve.reinhardt@amd.com////////////////////////////////////////////////////////////////////// 12210831Ssteve.reinhardt@amd.com 123360SN/A 124360SN/A/// Handler for unimplemented syscalls that we haven't thought about. 125360SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 126360SN/A Process *p, ThreadContext *tc); 12710831Ssteve.reinhardt@amd.com 128360SN/A/// Handler for unimplemented syscalls that we never intend to 129360SN/A/// implement (signal handling, etc.) and should not affect the correct 130360SN/A/// behavior of the program. Print a warning only if the appropriate 131360SN/A/// trace flag is enabled. Return success to the target program. 1323114Sgblack@eecs.umich.eduSyscallReturn ignoreFunc(SyscallDesc *desc, int num, 13310831Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc); 13410831Ssteve.reinhardt@amd.com 13510831Ssteve.reinhardt@amd.com// Target fallocateFunc() handler. 136360SN/ASyscallReturn fallocateFunc(SyscallDesc *desc, int num, 137360SN/A Process *p, ThreadContext *tc); 138360SN/A 139360SN/A/// Target exit() handler: terminate current context. 140360SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 141360SN/A Process *p, ThreadContext *tc); 142360SN/A 143360SN/A/// Target exit_group() handler: terminate simulation. (exit all threads) 144360SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 145360SN/A Process *p, ThreadContext *tc); 146360SN/A 147360SN/A/// Target set_tid_address() handler. 148378SN/ASyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1491706SN/A Process *p, ThreadContext *tc); 1503114Sgblack@eecs.umich.edu 151378SN/A/// Target getpagesize() handler. 152378SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 153378SN/A Process *p, ThreadContext *tc); 154378SN/A 155378SN/A/// Target brk() handler: set brk address. 1561706SN/ASyscallReturn brkFunc(SyscallDesc *desc, int num, 1573114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 158360SN/A 1596109Ssanchezd@stanford.edu/// Target close() handler. 1601706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 1613114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 162378SN/A 1636109Ssanchezd@stanford.edu// Target read() handler. 1646109Ssanchezd@stanford.eduSyscallReturn readFunc(SyscallDesc *desc, int num, 1656109Ssanchezd@stanford.edu Process *p, ThreadContext *tc); 1666109Ssanchezd@stanford.edu 167378SN/A/// Target write() handler. 1681706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 1693114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 170378SN/A 1715748SSteve.Reinhardt@amd.com/// Target lseek() handler. 1725748SSteve.Reinhardt@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 1735748SSteve.Reinhardt@amd.com Process *p, ThreadContext *tc); 174378SN/A 175378SN/A/// Target _llseek() handler. 1761706SN/ASyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1773114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 178378SN/A 179378SN/A/// Target munmap() handler. 1801706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 1813114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 182378SN/A 183378SN/A/// Target gethostname() handler. 1841706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 1853114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 186378SN/A 187378SN/A/// Target getcwd() handler. 1881706SN/ASyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1893114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 190378SN/A 1914118Sgblack@eecs.umich.edu/// Target readlink() handler. 1924118Sgblack@eecs.umich.eduSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1934118Sgblack@eecs.umich.edu Process *p, ThreadContext *tc, 1944118Sgblack@eecs.umich.edu int index = 0); 195378SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1961706SN/A Process *p, ThreadContext *tc); 1973114Sgblack@eecs.umich.edu 198378SN/A/// Target unlink() handler. 199378SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2001706SN/A Process *p, ThreadContext *tc, 2013114Sgblack@eecs.umich.edu int index); 202360SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2035513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 2045513SMichael.Adler@intel.com 2055513SMichael.Adler@intel.com/// Target mkdir() handler. 2065513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 20710203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 20810203SAli.Saidi@ARM.com 20910203SAli.Saidi@ARM.com/// Target rename() handler. 21010203SAli.Saidi@ARM.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 2115513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 2125513SMichael.Adler@intel.com 2135513SMichael.Adler@intel.com 214511SN/A/// Target truncate() handler. 21510633Smichaelupton@gmail.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 21610633Smichaelupton@gmail.com Process *p, ThreadContext *tc); 21710633Smichaelupton@gmail.com 2181706SN/A 2193114Sgblack@eecs.umich.edu/// Target ftruncate() handler. 220511SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2215513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 2225513SMichael.Adler@intel.com 2235513SMichael.Adler@intel.com 2245513SMichael.Adler@intel.com/// Target truncate64() handler. 225511SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num, 2261706SN/A Process *p, ThreadContext *tc); 2273114Sgblack@eecs.umich.edu 2281706SN/A/// Target ftruncate64() handler. 2291706SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2301706SN/A Process *p, ThreadContext *tc); 2311706SN/A 2323114Sgblack@eecs.umich.edu 2331706SN/A/// Target umask() handler. 2341706SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num, 2351706SN/A Process *p, ThreadContext *tc); 2361706SN/A 2373114Sgblack@eecs.umich.edu/// Target gettid() handler. 2381706SN/ASyscallReturn gettidFunc(SyscallDesc *desc, int num, 239511SN/A Process *p, ThreadContext *tc); 2406703Svince@csl.cornell.edu 2416703Svince@csl.cornell.edu/// Target chown() handler. 2426703Svince@csl.cornell.eduSyscallReturn chownFunc(SyscallDesc *desc, int num, 2436703Svince@csl.cornell.edu Process *p, ThreadContext *tc); 2446685Stjones1@inf.ed.ac.uk 2456685Stjones1@inf.ed.ac.uk/// Target setpgid() handler. 2466685Stjones1@inf.ed.ac.ukSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2476685Stjones1@inf.ed.ac.uk Process *p, ThreadContext *tc); 2486685Stjones1@inf.ed.ac.uk 2495513SMichael.Adler@intel.com/// Target fchown() handler. 2505513SMichael.Adler@intel.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2515513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 2525513SMichael.Adler@intel.com 2535513SMichael.Adler@intel.com/// Target dup() handler. 2541999SN/ASyscallReturn dupFunc(SyscallDesc *desc, int num, 2551999SN/A Process *process, ThreadContext *tc); 2563114Sgblack@eecs.umich.edu 2571999SN/A/// Target dup2() handler. 2581999SN/ASyscallReturn dup2Func(SyscallDesc *desc, int num, 2591999SN/A Process *process, ThreadContext *tc); 2601999SN/A 2613114Sgblack@eecs.umich.edu/// Target fcntl() handler. 2621999SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2633079Sstever@eecs.umich.edu Process *process, ThreadContext *tc); 2643079Sstever@eecs.umich.edu 2653114Sgblack@eecs.umich.edu/// Target fcntl64() handler. 2663079Sstever@eecs.umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2672093SN/A Process *process, ThreadContext *tc); 2682093SN/A 2693114Sgblack@eecs.umich.edu/// Target setuid() handler. 2702093SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2712687Sksewell@umich.edu Process *p, ThreadContext *tc); 2722687Sksewell@umich.edu 2733114Sgblack@eecs.umich.edu/// Target pipe() handler. 2742687Sksewell@umich.eduSyscallReturn pipeFunc(SyscallDesc *desc, int num, 2752238SN/A Process *p, ThreadContext *tc); 2762238SN/A 2773114Sgblack@eecs.umich.edu/// Internal pipe() handler. 2782238SN/ASyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 2792238SN/A ThreadContext *tc, bool pseudoPipe); 2802238SN/A 2813114Sgblack@eecs.umich.edu/// Target getpid() handler. 2822238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 2832238SN/A Process *p, ThreadContext *tc); 2842238SN/A 2853114Sgblack@eecs.umich.edu/// Target getuid() handler. 2862238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 2872238SN/A Process *p, ThreadContext *tc); 2882238SN/A 2893114Sgblack@eecs.umich.edu/// Target getgid() handler. 2902238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 2912238SN/A Process *p, ThreadContext *tc); 2922238SN/A 2933114Sgblack@eecs.umich.edu/// Target getppid() handler. 2942238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 2952238SN/A Process *p, ThreadContext *tc); 2962238SN/A 2973114Sgblack@eecs.umich.edu/// Target geteuid() handler. 2982238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2992238SN/A Process *p, ThreadContext *tc); 3002238SN/A 3013114Sgblack@eecs.umich.edu/// Target getegid() handler. 3022238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3036109Ssanchezd@stanford.edu Process *p, ThreadContext *tc); 3046109Ssanchezd@stanford.edu 3056109Ssanchezd@stanford.edu/// Target access() handler 3062238SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num, 3079455Smitch.hayenga+gem5@gmail.com Process *p, ThreadContext *tc); 3089455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 3099455Smitch.hayenga+gem5@gmail.com Process *p, ThreadContext *tc, 31010203SAli.Saidi@ARM.com int index); 31110203SAli.Saidi@ARM.com 31210203SAli.Saidi@ARM.com/// Futex system call 3139455Smitch.hayenga+gem5@gmail.com/// Implemented by Daniel Sanchez 3149112Smarc.orr@gmail.com/// Used by printf's in multi-threaded apps 3159112Smarc.orr@gmail.comtemplate <class OS> 3169112Smarc.orr@gmail.comSyscallReturn 3179112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3189112Smarc.orr@gmail.com ThreadContext *tc) 3199112Smarc.orr@gmail.com{ 3209112Smarc.orr@gmail.com using namespace std; 3219112Smarc.orr@gmail.com 3229112Smarc.orr@gmail.com int index = 0; 3239112Smarc.orr@gmail.com Addr uaddr = process->getSyscallArg(tc, index); 3249112Smarc.orr@gmail.com int op = process->getSyscallArg(tc, index); 3259112Smarc.orr@gmail.com int val = process->getSyscallArg(tc, index); 3269112Smarc.orr@gmail.com 3279112Smarc.orr@gmail.com /* 3289112Smarc.orr@gmail.com * Unsupported option that does not affect the correctness of the 3299112Smarc.orr@gmail.com * application. This is a performance optimization utilized by Linux. 3309112Smarc.orr@gmail.com */ 3319112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3329112Smarc.orr@gmail.com 3339112Smarc.orr@gmail.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 3349112Smarc.orr@gmail.com 3359112Smarc.orr@gmail.com if (OS::TGT_FUTEX_WAIT == op) { 3369112Smarc.orr@gmail.com // Ensure futex system call accessed atomically. 3379112Smarc.orr@gmail.com BufferArg buf(uaddr, sizeof(int)); 3389238Slluc.alvarez@bsc.es buf.copyIn(tc->getMemProxy()); 3399112Smarc.orr@gmail.com int mem_val = *(int*)buf.bufferPtr(); 3409112Smarc.orr@gmail.com 3419112Smarc.orr@gmail.com /* 3429112Smarc.orr@gmail.com * The value in memory at uaddr is not equal with the expected val 3439112Smarc.orr@gmail.com * (a different thread must have changed it before the system call was 3449112Smarc.orr@gmail.com * invoked). In this case, we need to throw an error. 3459112Smarc.orr@gmail.com */ 3469112Smarc.orr@gmail.com if (val != mem_val) 3479112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 3489112Smarc.orr@gmail.com 34911367Sandreas.hansson@arm.com futex_map.suspend(uaddr, process->tgid(), tc); 3509112Smarc.orr@gmail.com 35111321Ssteve.reinhardt@amd.com return 0; 3529112Smarc.orr@gmail.com } else if (OS::TGT_FUTEX_WAKE == op) { 3539112Smarc.orr@gmail.com return futex_map.wakeup(uaddr, process->tgid(), val); 3549112Smarc.orr@gmail.com } 3559112Smarc.orr@gmail.com 3569112Smarc.orr@gmail.com warn("futex: op %d not implemented; ignoring.", op); 3579112Smarc.orr@gmail.com return -ENOSYS; 3589112Smarc.orr@gmail.com} 3599112Smarc.orr@gmail.com 3609112Smarc.orr@gmail.com 3619112Smarc.orr@gmail.com/// Pseudo Funcs - These functions use a different return convension, 3629112Smarc.orr@gmail.com/// returning a second value in a register other than the normal return register 3639112Smarc.orr@gmail.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3649112Smarc.orr@gmail.com Process *process, ThreadContext *tc); 3659112Smarc.orr@gmail.com 3669112Smarc.orr@gmail.com/// Target getpidPseudo() handler. 3679112Smarc.orr@gmail.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3689112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3699112Smarc.orr@gmail.com 3709112Smarc.orr@gmail.com/// Target getuidPseudo() handler. 3719112Smarc.orr@gmail.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3729112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3739112Smarc.orr@gmail.com 3749112Smarc.orr@gmail.com/// Target getgidPseudo() handler. 3759112Smarc.orr@gmail.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3769112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3779112Smarc.orr@gmail.com 3789112Smarc.orr@gmail.com 3799112Smarc.orr@gmail.com/// A readable name for 1,000,000, for converting microseconds to seconds. 3809112Smarc.orr@gmail.comconst int one_million = 1000000; 38111321Ssteve.reinhardt@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 3829112Smarc.orr@gmail.comconst int one_billion = 1000000000; 3839112Smarc.orr@gmail.com 3849112Smarc.orr@gmail.com/// Approximate seconds since the epoch (1/1/1970). About a billion, 3859112Smarc.orr@gmail.com/// by my reckoning. We want to keep this a constant (not use the 3869112Smarc.orr@gmail.com/// real-world time) to keep simulations repeatable. 3879112Smarc.orr@gmail.comconst unsigned seconds_since_epoch = 1000000000; 3889112Smarc.orr@gmail.com 3899112Smarc.orr@gmail.com/// Helper function to convert current elapsed time to seconds and 3909238Slluc.alvarez@bsc.es/// microseconds. 3919112Smarc.orr@gmail.comtemplate <class T1, class T2> 3929112Smarc.orr@gmail.comvoid 3939112Smarc.orr@gmail.comgetElapsedTimeMicro(T1 &sec, T2 &usec) 3949112Smarc.orr@gmail.com{ 3959112Smarc.orr@gmail.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 3962238SN/A sec = elapsed_usecs / one_million; 3972238SN/A usec = elapsed_usecs % one_million; 3982238SN/A} 3992238SN/A 4003114Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 4012238SN/A/// nanoseconds. 4022238SN/Atemplate <class T1, class T2> 4032238SN/Avoid 4043114Sgblack@eecs.umich.edugetElapsedTimeNano(T1 &sec, T2 &nsec) 4052238SN/A{ 4062238SN/A uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 4072238SN/A sec = elapsed_nsecs / one_billion; 4083114Sgblack@eecs.umich.edu nsec = elapsed_nsecs % one_billion; 4092238SN/A} 4102238SN/A 4112238SN/A////////////////////////////////////////////////////////////////////// 4123114Sgblack@eecs.umich.edu// 4132238SN/A// The following emulation functions are generic, but need to be 4142238SN/A// templated to account for differences in types, constants, etc. 4151354SN/A// 4161354SN/A////////////////////////////////////////////////////////////////////// 41710796Sbrandon.potter@amd.com 41810796Sbrandon.potter@amd.com typedef struct statfs hst_statfs; 4191354SN/A#if NO_STAT64 4201354SN/A typedef struct stat hst_stat; 4211354SN/A typedef struct stat hst_stat64; 4221354SN/A#else 4231354SN/A typedef struct stat hst_stat; 4241354SN/A typedef struct stat64 hst_stat64; 4251354SN/A#endif 4261354SN/A 4271354SN/A//// Helper function to convert a host stat buffer to a target stat 4281354SN/A//// buffer. Also copies the target buffer out to the simulated 42910796Sbrandon.potter@amd.com//// memory space. Used by stat(), fstat(), and lstat(). 4301354SN/A 43110796Sbrandon.potter@amd.comtemplate <typename target_stat, typename host_stat> 4321354SN/Avoid 4331354SN/AconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 4341354SN/A{ 4351354SN/A using namespace TheISA; 43610796Sbrandon.potter@amd.com 43710796Sbrandon.potter@amd.com if (fakeTTY) 43810796Sbrandon.potter@amd.com tgt->st_dev = 0xA; 43910796Sbrandon.potter@amd.com else 44010796Sbrandon.potter@amd.com tgt->st_dev = host->st_dev; 44110796Sbrandon.potter@amd.com tgt->st_dev = TheISA::htog(tgt->st_dev); 44210796Sbrandon.potter@amd.com tgt->st_ino = host->st_ino; 44310796Sbrandon.potter@amd.com tgt->st_ino = TheISA::htog(tgt->st_ino); 44410796Sbrandon.potter@amd.com tgt->st_mode = host->st_mode; 44510796Sbrandon.potter@amd.com if (fakeTTY) { 44610796Sbrandon.potter@amd.com // Claim to be a character device 447360SN/A tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 448360SN/A tgt->st_mode |= S_IFCHR; // Set S_IFCHR 449360SN/A } 450360SN/A tgt->st_mode = TheISA::htog(tgt->st_mode); 451360SN/A tgt->st_nlink = host->st_nlink; 452360SN/A tgt->st_nlink = TheISA::htog(tgt->st_nlink); 453360SN/A tgt->st_uid = host->st_uid; 4543113Sgblack@eecs.umich.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 4553113Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4563113Sgblack@eecs.umich.edu tgt->st_gid = TheISA::htog(tgt->st_gid); 4573113Sgblack@eecs.umich.edu if (fakeTTY) 4583113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4593113Sgblack@eecs.umich.edu else 4603113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4613113Sgblack@eecs.umich.edu tgt->st_rdev = TheISA::htog(tgt->st_rdev); 4623113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4633113Sgblack@eecs.umich.edu tgt->st_size = TheISA::htog(tgt->st_size); 4643113Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4653113Sgblack@eecs.umich.edu tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4663113Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4673113Sgblack@eecs.umich.edu tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4683113Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4693113Sgblack@eecs.umich.edu tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4704189Sgblack@eecs.umich.edu // Force the block size to be 8KB. This helps to ensure buffered io works 4714189Sgblack@eecs.umich.edu // consistently across different hosts. 4723113Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4733113Sgblack@eecs.umich.edu tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4743113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4753113Sgblack@eecs.umich.edu tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4768737Skoansin.tan@gmail.com} 4773113Sgblack@eecs.umich.edu 4788737Skoansin.tan@gmail.com// Same for stat64 4793277Sgblack@eecs.umich.edu 4805515SMichael.Adler@intel.comtemplate <typename target_stat, typename host_stat64> 4815515SMichael.Adler@intel.comvoid 4825515SMichael.Adler@intel.comconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4835515SMichael.Adler@intel.com{ 4845515SMichael.Adler@intel.com using namespace TheISA; 4858737Skoansin.tan@gmail.com 4863277Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4878737Skoansin.tan@gmail.com#if defined(STAT_HAVE_NSEC) 4883277Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4898737Skoansin.tan@gmail.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 4903277Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4918737Skoansin.tan@gmail.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 4923113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4933113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 4943113Sgblack@eecs.umich.edu#else 4953113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4968737Skoansin.tan@gmail.com tgt->st_mtime_nsec = 0; 4973113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 4988737Skoansin.tan@gmail.com#endif 4993114Sgblack@eecs.umich.edu} 5008737Skoansin.tan@gmail.com 5013114Sgblack@eecs.umich.edu// Here are a couple of convenience functions 5028737Skoansin.tan@gmail.comtemplate<class OS> 5033114Sgblack@eecs.umich.eduvoid 5048737Skoansin.tan@gmail.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 5054061Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 5064061Sgblack@eecs.umich.edu{ 5074061Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5088737Skoansin.tan@gmail.com tgt_stat_buf tgt(addr); 5093113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5108737Skoansin.tan@gmail.com tgt.copyOut(mem); 5113113Sgblack@eecs.umich.edu} 5123113Sgblack@eecs.umich.edu 5133113Sgblack@eecs.umich.edutemplate<class OS> 5143113Sgblack@eecs.umich.eduvoid 5153113Sgblack@eecs.umich.educopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5163113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5173113Sgblack@eecs.umich.edu{ 5183113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5194189Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5204189Sgblack@eecs.umich.edu convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5213113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5223113Sgblack@eecs.umich.edu} 5233113Sgblack@eecs.umich.edu 5248737Skoansin.tan@gmail.comtemplate <class OS> 5253113Sgblack@eecs.umich.eduvoid 5268737Skoansin.tan@gmail.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 5273113Sgblack@eecs.umich.edu hst_statfs *host) 5288737Skoansin.tan@gmail.com{ 5293113Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 5303113Sgblack@eecs.umich.edu 5313113Sgblack@eecs.umich.edu tgt->f_type = TheISA::htog(host->f_type); 5323113Sgblack@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5333113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_iosize); 5343113Sgblack@eecs.umich.edu#else 5353113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_bsize); 5363113Sgblack@eecs.umich.edu#endif 5373113Sgblack@eecs.umich.edu tgt->f_blocks = TheISA::htog(host->f_blocks); 5383113Sgblack@eecs.umich.edu tgt->f_bfree = TheISA::htog(host->f_bfree); 5398852Sandreas.hansson@arm.com tgt->f_bavail = TheISA::htog(host->f_bavail); 5403113Sgblack@eecs.umich.edu tgt->f_files = TheISA::htog(host->f_files); 5413113Sgblack@eecs.umich.edu tgt->f_ffree = TheISA::htog(host->f_ffree); 5423113Sgblack@eecs.umich.edu memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 5433113Sgblack@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5443113Sgblack@eecs.umich.edu tgt->f_namelen = TheISA::htog(host->f_namemax); 5453113Sgblack@eecs.umich.edu tgt->f_frsize = TheISA::htog(host->f_bsize); 5463113Sgblack@eecs.umich.edu#elif defined(__APPLE__) 5473113Sgblack@eecs.umich.edu tgt->f_namelen = 0; 5483113Sgblack@eecs.umich.edu tgt->f_frsize = 0; 5493113Sgblack@eecs.umich.edu#else 5508852Sandreas.hansson@arm.com tgt->f_namelen = TheISA::htog(host->f_namelen); 5513113Sgblack@eecs.umich.edu tgt->f_frsize = TheISA::htog(host->f_frsize); 5523113Sgblack@eecs.umich.edu#endif 5533113Sgblack@eecs.umich.edu#if defined(__linux__) 5543113Sgblack@eecs.umich.edu memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 5556686Stjones1@inf.ed.ac.uk#else 5563113Sgblack@eecs.umich.edu /* 5573113Sgblack@eecs.umich.edu * The fields are different sizes per OS. Don't bother with 5583113Sgblack@eecs.umich.edu * f_spare or f_reserved on non-Linux for now. 559378SN/A */ 560378SN/A memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 5619141Smarc.orr@gmail.com#endif 5629141Smarc.orr@gmail.com 563360SN/A tgt.copyOut(mem); 5641450SN/A} 5653114Sgblack@eecs.umich.edu 5662680Sktlim@umich.edu/// Target ioctl() handler. For the most part, programs call ioctl() 567360SN/A/// only to find out if their stdout is a tty, to determine whether to 5686701Sgblack@eecs.umich.edu/// do line or block buffering. We always claim that output fds are 56910930Sbrandon.potter@amd.com/// not TTYs to provide repeatable results. 5706701Sgblack@eecs.umich.edutemplate <class OS> 571360SN/ASyscallReturn 57210930Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 573360SN/A{ 57410932Sbrandon.potter@amd.com int index = 0; 57510496Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 57610930Sbrandon.potter@amd.com unsigned req = p->getSyscallArg(tc, index); 577360SN/A 5781458SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 579360SN/A 580360SN/A if (OS::isTtyReq(req)) 58110930Sbrandon.potter@amd.com return -ENOTTY; 58210930Sbrandon.potter@amd.com 58310496Ssteve.reinhardt@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 58410496Ssteve.reinhardt@amd.com if (!dfdp) 5859141Smarc.orr@gmail.com return -EBADF; 5861458SN/A 5879141Smarc.orr@gmail.com /** 588360SN/A * If the driver is valid, issue the ioctl through it. Otherwise, 5899141Smarc.orr@gmail.com * there's an implicit assumption that the device is a TTY type and we 59010930Sbrandon.potter@amd.com * return that we do not have a valid TTY. 5919141Smarc.orr@gmail.com */ 592360SN/A EmulatedDriver *emul_driver = dfdp->getDriver(); 593360SN/A if (emul_driver) 594360SN/A return emul_driver->ioctl(p, tc, req); 59510027SChris.Adeniyi-Jones@arm.com 5963114Sgblack@eecs.umich.edu /** 59710027SChris.Adeniyi-Jones@arm.com * For lack of a better return code, return ENOTTY. Ideally, we should 598360SN/A * return something better here, but at least we issue the warning. 599360SN/A */ 600360SN/A warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 6018852Sandreas.hansson@arm.com tgt_fd, req, tc->pcState()); 6026701Sgblack@eecs.umich.edu return -ENOTTY; 6031458SN/A} 604360SN/A 6056701Sgblack@eecs.umich.edutemplate <class OS> 6066701Sgblack@eecs.umich.eduSyscallReturn 607360SN/AopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 608360SN/A bool isopenat) 609360SN/A{ 610360SN/A int index = 0; 611360SN/A int tgt_dirfd = -1; 612360SN/A 613360SN/A /** 614360SN/A * If using the openat variant, read in the target directory file 615360SN/A * descriptor from the simulated process. 616360SN/A */ 617360SN/A if (isopenat) 618360SN/A tgt_dirfd = p->getSyscallArg(tc, index); 6191706SN/A 620360SN/A /** 621360SN/A * Retrieve the simulated process' memory proxy and then read in the path 622360SN/A * string from that memory space into the host's working memory space. 623360SN/A */ 624360SN/A std::string path; 6253669Sbinkertn@umich.edu if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 6263669Sbinkertn@umich.edu return -EFAULT; 6273669Sbinkertn@umich.edu 6281706SN/A#ifdef __CYGWIN32__ 6291706SN/A int host_flags = O_BINARY; 63010496Ssteve.reinhardt@amd.com#else 63110496Ssteve.reinhardt@amd.com int host_flags = 0; 63210496Ssteve.reinhardt@amd.com#endif 63310496Ssteve.reinhardt@amd.com /** 63410496Ssteve.reinhardt@amd.com * Translate target flags into host flags. Flags exist which are not 63510496Ssteve.reinhardt@amd.com * ported between architectures which can cause check failures. 63610496Ssteve.reinhardt@amd.com */ 63710496Ssteve.reinhardt@amd.com int tgt_flags = p->getSyscallArg(tc, index); 63810496Ssteve.reinhardt@amd.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 63910496Ssteve.reinhardt@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 64010496Ssteve.reinhardt@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 64110496Ssteve.reinhardt@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 64210496Ssteve.reinhardt@amd.com } 64310496Ssteve.reinhardt@amd.com } 64410496Ssteve.reinhardt@amd.com if (tgt_flags) { 64510496Ssteve.reinhardt@amd.com warn("open%s: cannot decode flags 0x%x", 64610496Ssteve.reinhardt@amd.com isopenat ? "at" : "", tgt_flags); 64710496Ssteve.reinhardt@amd.com } 64810496Ssteve.reinhardt@amd.com#ifdef __CYGWIN32__ 64910496Ssteve.reinhardt@amd.com host_flags |= O_BINARY; 6505795Ssaidi@eecs.umich.edu#endif 6519143Ssteve.reinhardt@amd.com 6529142Ssteve.reinhardt@amd.com int mode = p->getSyscallArg(tc, index); 6539142Ssteve.reinhardt@amd.com 6549143Ssteve.reinhardt@amd.com /** 6555795Ssaidi@eecs.umich.edu * If the simulated process called open or openat with AT_FDCWD specified, 6569143Ssteve.reinhardt@amd.com * take the current working directory value which was passed into the 6575795Ssaidi@eecs.umich.edu * process class as a Python parameter and append the current path to 6585795Ssaidi@eecs.umich.edu * create a full path. 6595795Ssaidi@eecs.umich.edu * Otherwise, openat with a valid target directory file descriptor has 6609143Ssteve.reinhardt@amd.com * been called. If the path option, which was passed in as a parameter, 6615795Ssaidi@eecs.umich.edu * is not absolute, retrieve the directory file descriptor's path and 662360SN/A * prepend it to the path passed in as a parameter. 6639143Ssteve.reinhardt@amd.com * In every case, we should have a full path (which is relevant to the 6649143Ssteve.reinhardt@amd.com * host) to work with after this block has been passed. 6659143Ssteve.reinhardt@amd.com */ 66610932Sbrandon.potter@amd.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 667360SN/A path = p->fullPath(path); 668360SN/A } else if (!startswith(path, "/")) { 66910027SChris.Adeniyi-Jones@arm.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 67010027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 67110027SChris.Adeniyi-Jones@arm.com if (!ffdp) 67210027SChris.Adeniyi-Jones@arm.com return -EBADF; 67310027SChris.Adeniyi-Jones@arm.com path.insert(0, ffdp->getFileName()); 67410027SChris.Adeniyi-Jones@arm.com } 67510027SChris.Adeniyi-Jones@arm.com 67610027SChris.Adeniyi-Jones@arm.com /** 67710027SChris.Adeniyi-Jones@arm.com * Since this is an emulated environment, we create pseudo file 67810027SChris.Adeniyi-Jones@arm.com * descriptors for device requests that have been registered with 67910027SChris.Adeniyi-Jones@arm.com * the process class through Python; this allows us to create a file 68010027SChris.Adeniyi-Jones@arm.com * descriptor for subsequent ioctl or mmap calls. 68110027SChris.Adeniyi-Jones@arm.com */ 68210027SChris.Adeniyi-Jones@arm.com if (startswith(path, "/dev/")) { 68310027SChris.Adeniyi-Jones@arm.com std::string filename = path.substr(strlen("/dev/")); 68410027SChris.Adeniyi-Jones@arm.com EmulatedDriver *drv = p->findDriver(filename); 68510027SChris.Adeniyi-Jones@arm.com if (drv) { 68610027SChris.Adeniyi-Jones@arm.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 68710027SChris.Adeniyi-Jones@arm.com "driver open with path[%s]\n", 68810027SChris.Adeniyi-Jones@arm.com isopenat ? "at" : "", path.c_str()); 68910027SChris.Adeniyi-Jones@arm.com return drv->open(p, tc, mode, host_flags); 69010027SChris.Adeniyi-Jones@arm.com } 69110633Smichaelupton@gmail.com /** 69210633Smichaelupton@gmail.com * Fall through here for pass through to host devices, such 69310633Smichaelupton@gmail.com * as /dev/zero 69410633Smichaelupton@gmail.com */ 69510633Smichaelupton@gmail.com } 69610633Smichaelupton@gmail.com 69710633Smichaelupton@gmail.com /** 69810633Smichaelupton@gmail.com * Some special paths and files cannot be called on the host and need 69910633Smichaelupton@gmail.com * to be handled as special cases inside the simulator. 70010633Smichaelupton@gmail.com * If the full path that was created above does not match any of the 70110633Smichaelupton@gmail.com * special cases, pass it through to the open call on the host to let 70210633Smichaelupton@gmail.com * the host open the file on our behalf. 70310633Smichaelupton@gmail.com * If the host cannot open the file, return the host's error code back 70410633Smichaelupton@gmail.com * through the system call to the simulated process. 70510203SAli.Saidi@ARM.com */ 70610203SAli.Saidi@ARM.com int sim_fd = -1; 70710203SAli.Saidi@ARM.com std::vector<std::string> special_paths = 70810203SAli.Saidi@ARM.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 70910203SAli.Saidi@ARM.com for (auto entry : special_paths) { 71010203SAli.Saidi@ARM.com if (startswith(path, entry)) 71110203SAli.Saidi@ARM.com sim_fd = OS::openSpecialFile(path, p, tc); 71210203SAli.Saidi@ARM.com } 71310203SAli.Saidi@ARM.com if (sim_fd == -1) { 71410203SAli.Saidi@ARM.com sim_fd = open(path.c_str(), host_flags, mode); 71510203SAli.Saidi@ARM.com } 71610203SAli.Saidi@ARM.com if (sim_fd == -1) { 71710203SAli.Saidi@ARM.com int local = -errno; 71810203SAli.Saidi@ARM.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 71910203SAli.Saidi@ARM.com isopenat ? "at" : "", path.c_str()); 72010203SAli.Saidi@ARM.com return local; 72110203SAli.Saidi@ARM.com } 72210203SAli.Saidi@ARM.com 72310203SAli.Saidi@ARM.com /** 72410203SAli.Saidi@ARM.com * The file was opened successfully and needs to be recorded in the 72510203SAli.Saidi@ARM.com * process' file descriptor array so that it can be retrieved later. 72610203SAli.Saidi@ARM.com * The target file descriptor that is chosen will be the lowest unused 72710203SAli.Saidi@ARM.com * file descriptor. 72810203SAli.Saidi@ARM.com * Return the indirect target file descriptor back to the simulated 72910203SAli.Saidi@ARM.com * process to act as a handle for the opened file. 73010203SAli.Saidi@ARM.com */ 73110850SGiacomo.Gabrielli@arm.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 73210850SGiacomo.Gabrielli@arm.com int tgt_fd = p->fds->allocFD(ffdp); 73310850SGiacomo.Gabrielli@arm.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 73410850SGiacomo.Gabrielli@arm.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 73510850SGiacomo.Gabrielli@arm.com return tgt_fd; 73610850SGiacomo.Gabrielli@arm.com} 73710850SGiacomo.Gabrielli@arm.com 73810850SGiacomo.Gabrielli@arm.com/// Target open() handler. 73910850SGiacomo.Gabrielli@arm.comtemplate <class OS> 74010850SGiacomo.Gabrielli@arm.comSyscallReturn 74110850SGiacomo.Gabrielli@arm.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 74210850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 74310850SGiacomo.Gabrielli@arm.com{ 74410850SGiacomo.Gabrielli@arm.com return openImpl<OS>(desc, callnum, process, tc, false); 74510850SGiacomo.Gabrielli@arm.com} 74610850SGiacomo.Gabrielli@arm.com 74710850SGiacomo.Gabrielli@arm.com/// Target openat() handler. 74810850SGiacomo.Gabrielli@arm.comtemplate <class OS> 74910850SGiacomo.Gabrielli@arm.comSyscallReturn 75010850SGiacomo.Gabrielli@arm.comopenatFunc(SyscallDesc *desc, int callnum, Process *process, 75110850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 75210850SGiacomo.Gabrielli@arm.com{ 75310850SGiacomo.Gabrielli@arm.com return openImpl<OS>(desc, callnum, process, tc, true); 75410850SGiacomo.Gabrielli@arm.com} 75510850SGiacomo.Gabrielli@arm.com 75610850SGiacomo.Gabrielli@arm.com/// Target unlinkat() handler. 75710850SGiacomo.Gabrielli@arm.comtemplate <class OS> 75810850SGiacomo.Gabrielli@arm.comSyscallReturn 75910850SGiacomo.Gabrielli@arm.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 76010850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 76110850SGiacomo.Gabrielli@arm.com{ 76210850SGiacomo.Gabrielli@arm.com int index = 0; 76310850SGiacomo.Gabrielli@arm.com int dirfd = process->getSyscallArg(tc, index); 76410850SGiacomo.Gabrielli@arm.com if (dirfd != OS::TGT_AT_FDCWD) 76510850SGiacomo.Gabrielli@arm.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 76610850SGiacomo.Gabrielli@arm.com 7676640Svince@csl.cornell.edu return unlinkHelper(desc, callnum, process, tc, 1); 7686640Svince@csl.cornell.edu} 7696640Svince@csl.cornell.edu 7706640Svince@csl.cornell.edu/// Target facessat() handler 7716640Svince@csl.cornell.edutemplate <class OS> 7726640Svince@csl.cornell.eduSyscallReturn 7736640Svince@csl.cornell.edufaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 7746701Sgblack@eecs.umich.edu ThreadContext *tc) 7756701Sgblack@eecs.umich.edu{ 77610793Sbrandon.potter@amd.com int index = 0; 7776640Svince@csl.cornell.edu int dirfd = process->getSyscallArg(tc, index); 7786701Sgblack@eecs.umich.edu if (dirfd != OS::TGT_AT_FDCWD) 7796701Sgblack@eecs.umich.edu warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 7806640Svince@csl.cornell.edu return accessFunc(desc, callnum, process, tc, 1); 7818706Sandreas.hansson@arm.com} 7826640Svince@csl.cornell.edu 7836701Sgblack@eecs.umich.edu/// Target readlinkat() handler 7846640Svince@csl.cornell.edutemplate <class OS> 785360SN/ASyscallReturn 7861999SN/AreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 7871999SN/A ThreadContext *tc) 7881999SN/A{ 7893114Sgblack@eecs.umich.edu int index = 0; 7902680Sktlim@umich.edu int dirfd = process->getSyscallArg(tc, index); 7911999SN/A if (dirfd != OS::TGT_AT_FDCWD) 7921999SN/A warn("openat: first argument not AT_FDCWD; unlikely to work"); 7931999SN/A return readlinkFunc(desc, callnum, process, tc, 1); 7946701Sgblack@eecs.umich.edu} 7958852Sandreas.hansson@arm.com 7966701Sgblack@eecs.umich.edu/// Target renameat() handler. 7971999SN/Atemplate <class OS> 7986701Sgblack@eecs.umich.eduSyscallReturn 7991999SN/ArenameatFunc(SyscallDesc *desc, int callnum, Process *process, 8006701Sgblack@eecs.umich.edu ThreadContext *tc) 8011999SN/A{ 8021999SN/A int index = 0; 8031999SN/A 8041999SN/A int olddirfd = process->getSyscallArg(tc, index); 8051999SN/A if (olddirfd != OS::TGT_AT_FDCWD) 8063669Sbinkertn@umich.edu warn("renameat: first argument not AT_FDCWD; unlikely to work"); 8073669Sbinkertn@umich.edu 8083669Sbinkertn@umich.edu std::string old_name; 8091999SN/A 8101999SN/A if (!tc->getMemProxy().tryReadString(old_name, 8111999SN/A process->getSyscallArg(tc, index))) 8122218SN/A return -EFAULT; 8131999SN/A 8141999SN/A int newdirfd = process->getSyscallArg(tc, index); 8151999SN/A if (newdirfd != OS::TGT_AT_FDCWD) 8161999SN/A warn("renameat: third argument not AT_FDCWD; unlikely to work"); 8171999SN/A 8181999SN/A std::string new_name; 8191999SN/A 8201999SN/A if (!tc->getMemProxy().tryReadString(new_name, 8213114Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 8222680Sktlim@umich.edu return -EFAULT; 8231999SN/A 8246701Sgblack@eecs.umich.edu // Adjust path for current working directory 82510931Sbrandon.potter@amd.com old_name = process->fullPath(old_name); 82610931Sbrandon.potter@amd.com new_name = process->fullPath(new_name); 82710931Sbrandon.potter@amd.com 82810932Sbrandon.potter@amd.com int result = rename(old_name.c_str(), new_name.c_str()); 82910931Sbrandon.potter@amd.com return (result == -1) ? -errno : result; 8301999SN/A} 8311999SN/A 8321999SN/A/// Target sysinfo() handler. 8331999SN/Atemplate <class OS> 8341999SN/ASyscallReturn 8351999SN/AsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 8361999SN/A ThreadContext *tc) 8371999SN/A{ 83810931Sbrandon.potter@amd.com 8391999SN/A int index = 0; 8402218SN/A TypedBufferArg<typename OS::tgt_sysinfo> 8411999SN/A sysinfo(process->getSyscallArg(tc, index)); 8421999SN/A 8431999SN/A sysinfo->uptime = seconds_since_epoch; 8441999SN/A sysinfo->totalram = process->system->memSize(); 8455877Shsul@eecs.umich.edu sysinfo->mem_unit = 1; 8465877Shsul@eecs.umich.edu 8475877Shsul@eecs.umich.edu sysinfo.copyOut(tc->getMemProxy()); 8485877Shsul@eecs.umich.edu 8495877Shsul@eecs.umich.edu return 0; 8506701Sgblack@eecs.umich.edu} 8516701Sgblack@eecs.umich.edu 8526701Sgblack@eecs.umich.edu/// Target chmod() handler. 8536701Sgblack@eecs.umich.edutemplate <class OS> 8546701Sgblack@eecs.umich.eduSyscallReturn 85510027SChris.Adeniyi-Jones@arm.comchmodFunc(SyscallDesc *desc, int callnum, Process *process, 85610027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 85710027SChris.Adeniyi-Jones@arm.com{ 85810027SChris.Adeniyi-Jones@arm.com std::string path; 85910027SChris.Adeniyi-Jones@arm.com 8605877Shsul@eecs.umich.edu int index = 0; 86110318Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 86210318Sandreas.hansson@arm.com process->getSyscallArg(tc, index))) { 8635877Shsul@eecs.umich.edu return -EFAULT; 8645877Shsul@eecs.umich.edu } 8655877Shsul@eecs.umich.edu 8665877Shsul@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 86710486Stjablin@gmail.com mode_t hostMode = 0; 86810486Stjablin@gmail.com 8695877Shsul@eecs.umich.edu // XXX translate mode flags via OS::something??? 87010027SChris.Adeniyi-Jones@arm.com hostMode = mode; 87110027SChris.Adeniyi-Jones@arm.com 8725877Shsul@eecs.umich.edu // Adjust path for current working directory 8738601Ssteve.reinhardt@amd.com path = process->fullPath(path); 8745877Shsul@eecs.umich.edu 8755877Shsul@eecs.umich.edu // do the chmod 8765877Shsul@eecs.umich.edu int result = chmod(path.c_str(), hostMode); 87710027SChris.Adeniyi-Jones@arm.com if (result < 0) 8785877Shsul@eecs.umich.edu return -errno; 8795877Shsul@eecs.umich.edu 8805877Shsul@eecs.umich.edu return 0; 88110027SChris.Adeniyi-Jones@arm.com} 88210027SChris.Adeniyi-Jones@arm.com 88310027SChris.Adeniyi-Jones@arm.com 88410027SChris.Adeniyi-Jones@arm.com/// Target fchmod() handler. 88510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 88610027SChris.Adeniyi-Jones@arm.comSyscallReturn 8875877Shsul@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 88810027SChris.Adeniyi-Jones@arm.com{ 88910027SChris.Adeniyi-Jones@arm.com int index = 0; 89010027SChris.Adeniyi-Jones@arm.com int tgt_fd = p->getSyscallArg(tc, index); 89110027SChris.Adeniyi-Jones@arm.com uint32_t mode = p->getSyscallArg(tc, index); 89210027SChris.Adeniyi-Jones@arm.com 89310027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 89410027SChris.Adeniyi-Jones@arm.com if (!ffdp) 89510027SChris.Adeniyi-Jones@arm.com return -EBADF; 89610027SChris.Adeniyi-Jones@arm.com int sim_fd = ffdp->getSimFD(); 89710027SChris.Adeniyi-Jones@arm.com 89810027SChris.Adeniyi-Jones@arm.com mode_t hostMode = mode; 89910027SChris.Adeniyi-Jones@arm.com 90010027SChris.Adeniyi-Jones@arm.com int result = fchmod(sim_fd, hostMode); 9015877Shsul@eecs.umich.edu 9025877Shsul@eecs.umich.edu return (result < 0) ? -errno : 0; 9035877Shsul@eecs.umich.edu} 90410027SChris.Adeniyi-Jones@arm.com 90510027SChris.Adeniyi-Jones@arm.com/// Target mremap() handler. 9068601Ssteve.reinhardt@amd.comtemplate <class OS> 90710027SChris.Adeniyi-Jones@arm.comSyscallReturn 9085877Shsul@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 9095877Shsul@eecs.umich.edu{ 9101999SN/A int index = 0; 911378SN/A Addr start = process->getSyscallArg(tc, index); 912360SN/A uint64_t old_length = process->getSyscallArg(tc, index); 9131450SN/A uint64_t new_length = process->getSyscallArg(tc, index); 9143114Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 9152680Sktlim@umich.edu uint64_t provided_address = 0; 916360SN/A bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 917360SN/A 918360SN/A if (use_provided_address) 9196701Sgblack@eecs.umich.edu provided_address = process->getSyscallArg(tc, index); 9208852Sandreas.hansson@arm.com 9216701Sgblack@eecs.umich.edu if ((start % TheISA::PageBytes != 0) || 9226701Sgblack@eecs.umich.edu (provided_address % TheISA::PageBytes != 0)) { 9236701Sgblack@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 9246701Sgblack@eecs.umich.edu return -EINVAL; 925360SN/A } 9263669Sbinkertn@umich.edu 9273669Sbinkertn@umich.edu new_length = roundUp(new_length, TheISA::PageBytes); 9283669Sbinkertn@umich.edu 929360SN/A if (new_length > old_length) { 930360SN/A std::shared_ptr<MemState> mem_state = process->memState; 931360SN/A Addr mmap_end = mem_state->getMmapEnd(); 932360SN/A 9332218SN/A if ((start + old_length) == mmap_end && 934360SN/A (!use_provided_address || provided_address == start)) { 9358706Sandreas.hansson@arm.com // This case cannot occur when growing downward, as 936360SN/A // start is greater than or equal to mmap_end. 9371458SN/A uint64_t diff = new_length - old_length; 938360SN/A process->allocateMem(mmap_end, diff); 939360SN/A mem_state->setMmapEnd(mmap_end + diff); 940360SN/A return start; 9415074Ssaidi@eecs.umich.edu } else { 9425074Ssaidi@eecs.umich.edu if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 9435074Ssaidi@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 9445074Ssaidi@eecs.umich.edu return -ENOMEM; 9455074Ssaidi@eecs.umich.edu } else { 9465074Ssaidi@eecs.umich.edu uint64_t new_start = provided_address; 9475074Ssaidi@eecs.umich.edu if (!use_provided_address) { 9485074Ssaidi@eecs.umich.edu new_start = process->mmapGrowsDown() ? 9496701Sgblack@eecs.umich.edu mmap_end - new_length : mmap_end; 9508852Sandreas.hansson@arm.com mmap_end = process->mmapGrowsDown() ? 9516701Sgblack@eecs.umich.edu new_start : mmap_end + new_length; 9525074Ssaidi@eecs.umich.edu mem_state->setMmapEnd(mmap_end); 9536701Sgblack@eecs.umich.edu } 9545074Ssaidi@eecs.umich.edu 9555074Ssaidi@eecs.umich.edu process->pTable->remap(start, old_length, new_start); 9565074Ssaidi@eecs.umich.edu warn("mremapping to new vaddr %08p-%08p, adding %d\n", 9575074Ssaidi@eecs.umich.edu new_start, new_start + new_length, 9585208Ssaidi@eecs.umich.edu new_length - old_length); 9595208Ssaidi@eecs.umich.edu // add on the remaining unallocated pages 9605208Ssaidi@eecs.umich.edu process->allocateMem(new_start + old_length, 9615208Ssaidi@eecs.umich.edu new_length - old_length, 9625074Ssaidi@eecs.umich.edu use_provided_address /* clobber */); 9635074Ssaidi@eecs.umich.edu if (use_provided_address && 9645208Ssaidi@eecs.umich.edu ((new_start + new_length > mem_state->getMmapEnd() && 9655074Ssaidi@eecs.umich.edu !process->mmapGrowsDown()) || 9665074Ssaidi@eecs.umich.edu (new_start < mem_state->getMmapEnd() && 9675074Ssaidi@eecs.umich.edu process->mmapGrowsDown()))) { 9685074Ssaidi@eecs.umich.edu // something fishy going on here, at least notify the user 9698706Sandreas.hansson@arm.com // @todo: increase mmap_end? 9705074Ssaidi@eecs.umich.edu warn("mmap region limit exceeded with MREMAP_FIXED\n"); 9715074Ssaidi@eecs.umich.edu } 9725074Ssaidi@eecs.umich.edu warn("returning %08p as start\n", new_start); 9735074Ssaidi@eecs.umich.edu return new_start; 9745074Ssaidi@eecs.umich.edu } 97510027SChris.Adeniyi-Jones@arm.com } 97610027SChris.Adeniyi-Jones@arm.com } else { 97710027SChris.Adeniyi-Jones@arm.com if (use_provided_address && provided_address != start) 97810027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 97910027SChris.Adeniyi-Jones@arm.com process->pTable->unmap(start + new_length, old_length - new_length); 98010027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 98110027SChris.Adeniyi-Jones@arm.com } 98210027SChris.Adeniyi-Jones@arm.com} 98310027SChris.Adeniyi-Jones@arm.com 98410793Sbrandon.potter@amd.com/// Target stat() handler. 98510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 98610027SChris.Adeniyi-Jones@arm.comSyscallReturn 98710027SChris.Adeniyi-Jones@arm.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 98810027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 98910027SChris.Adeniyi-Jones@arm.com{ 99010027SChris.Adeniyi-Jones@arm.com std::string path; 99110027SChris.Adeniyi-Jones@arm.com 99210027SChris.Adeniyi-Jones@arm.com int index = 0; 99310027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 99410027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) { 99510027SChris.Adeniyi-Jones@arm.com return -EFAULT; 99610027SChris.Adeniyi-Jones@arm.com } 99710027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 99810027SChris.Adeniyi-Jones@arm.com 99910027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 100010027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 100110027SChris.Adeniyi-Jones@arm.com 100210027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 100310027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 100410027SChris.Adeniyi-Jones@arm.com 100510027SChris.Adeniyi-Jones@arm.com if (result < 0) 100610027SChris.Adeniyi-Jones@arm.com return -errno; 100710027SChris.Adeniyi-Jones@arm.com 100810027SChris.Adeniyi-Jones@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 100910027SChris.Adeniyi-Jones@arm.com 101010027SChris.Adeniyi-Jones@arm.com return 0; 101110027SChris.Adeniyi-Jones@arm.com} 10121999SN/A 10131999SN/A 10141999SN/A/// Target stat64() handler. 10153114Sgblack@eecs.umich.edutemplate <class OS> 10162680Sktlim@umich.eduSyscallReturn 10171999SN/Astat64Func(SyscallDesc *desc, int callnum, Process *process, 10186701Sgblack@eecs.umich.edu ThreadContext *tc) 101910931Sbrandon.potter@amd.com{ 10206701Sgblack@eecs.umich.edu std::string path; 102110931Sbrandon.potter@amd.com 102210932Sbrandon.potter@amd.com int index = 0; 102310931Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 10241999SN/A process->getSyscallArg(tc, index))) 10251999SN/A return -EFAULT; 10262764Sstever@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 10272064SN/A 102810931Sbrandon.potter@amd.com // Adjust path for current working directory 10292064SN/A path = process->fullPath(path); 10302064SN/A 103110931Sbrandon.potter@amd.com#if NO_STAT64 10322064SN/A struct stat hostBuf; 10331999SN/A int result = stat(path.c_str(), &hostBuf); 10341999SN/A#else 10352218SN/A struct stat64 hostBuf; 10361999SN/A int result = stat64(path.c_str(), &hostBuf); 103710931Sbrandon.potter@amd.com#endif 10381999SN/A 10391999SN/A if (result < 0) 10401999SN/A return -errno; 10411999SN/A 10421999SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1043378SN/A 1044360SN/A return 0; 10451450SN/A} 10463114Sgblack@eecs.umich.edu 10472680Sktlim@umich.edu 1048360SN/A/// Target fstatat64() handler. 1049360SN/Atemplate <class OS> 1050360SN/ASyscallReturn 10516701Sgblack@eecs.umich.edufstatat64Func(SyscallDesc *desc, int callnum, Process *process, 10528852Sandreas.hansson@arm.com ThreadContext *tc) 10536701Sgblack@eecs.umich.edu{ 10546701Sgblack@eecs.umich.edu int index = 0; 10556701Sgblack@eecs.umich.edu int dirfd = process->getSyscallArg(tc, index); 10566701Sgblack@eecs.umich.edu if (dirfd != OS::TGT_AT_FDCWD) 1057360SN/A warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 10583669Sbinkertn@umich.edu 10593669Sbinkertn@umich.edu std::string path; 10603669Sbinkertn@umich.edu if (!tc->getMemProxy().tryReadString(path, 1061360SN/A process->getSyscallArg(tc, index))) 1062360SN/A return -EFAULT; 1063360SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1064360SN/A 10651458SN/A // Adjust path for current working directory 1066360SN/A path = process->fullPath(path); 10678706Sandreas.hansson@arm.com 1068360SN/A#if NO_STAT64 10691458SN/A struct stat hostBuf; 1070360SN/A int result = stat(path.c_str(), &hostBuf); 1071360SN/A#else 10721999SN/A struct stat64 hostBuf; 10731999SN/A int result = stat64(path.c_str(), &hostBuf); 10741999SN/A#endif 10753114Sgblack@eecs.umich.edu 10762680Sktlim@umich.edu if (result < 0) 10771999SN/A return -errno; 10781999SN/A 10791999SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10806701Sgblack@eecs.umich.edu 10818852Sandreas.hansson@arm.com return 0; 10826701Sgblack@eecs.umich.edu} 10836701Sgblack@eecs.umich.edu 10846701Sgblack@eecs.umich.edu 10856701Sgblack@eecs.umich.edu/// Target fstat64() handler. 10861999SN/Atemplate <class OS> 10873669Sbinkertn@umich.eduSyscallReturn 10883669Sbinkertn@umich.edufstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 10893669Sbinkertn@umich.edu{ 10902764Sstever@eecs.umich.edu int index = 0; 10912064SN/A int tgt_fd = p->getSyscallArg(tc, index); 10922064SN/A Addr bufPtr = p->getSyscallArg(tc, index); 10932064SN/A 10941999SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 10951999SN/A if (!ffdp) 10962064SN/A return -EBADF; 10971999SN/A int sim_fd = ffdp->getSimFD(); 10981999SN/A 10991999SN/A#if NO_STAT64 11001999SN/A struct stat hostBuf; 11018706Sandreas.hansson@arm.com int result = fstat(sim_fd, &hostBuf); 11021999SN/A#else 11031999SN/A struct stat64 hostBuf; 11041999SN/A int result = fstat64(sim_fd, &hostBuf); 11051999SN/A#endif 1106378SN/A 1107360SN/A if (result < 0) 11081450SN/A return -errno; 11093114Sgblack@eecs.umich.edu 11102680Sktlim@umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1111360SN/A 11126701Sgblack@eecs.umich.edu return 0; 111310931Sbrandon.potter@amd.com} 11146701Sgblack@eecs.umich.edu 1115360SN/A 111611380Salexandru.dutu@amd.com/// Target lstat() handler. 1117360SN/Atemplate <class OS> 111810932Sbrandon.potter@amd.comSyscallReturn 111910931Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 11201458SN/A ThreadContext *tc) 1121360SN/A{ 1122360SN/A std::string path; 112310931Sbrandon.potter@amd.com 1124360SN/A int index = 0; 1125360SN/A if (!tc->getMemProxy().tryReadString(path, 11261458SN/A process->getSyscallArg(tc, index))) { 1127360SN/A return -EFAULT; 112810931Sbrandon.potter@amd.com } 11292021SN/A Addr bufPtr = process->getSyscallArg(tc, index); 11301458SN/A 1131360SN/A // Adjust path for current working directory 1132360SN/A path = process->fullPath(path); 1133360SN/A 11341706SN/A struct stat hostBuf; 11351706SN/A int result = lstat(path.c_str(), &hostBuf); 11361706SN/A 11373114Sgblack@eecs.umich.edu if (result < 0) 11382680Sktlim@umich.edu return -errno; 11391706SN/A 11401706SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11411706SN/A 11426701Sgblack@eecs.umich.edu return 0; 11438852Sandreas.hansson@arm.com} 11446701Sgblack@eecs.umich.edu 11456701Sgblack@eecs.umich.edu/// Target lstat64() handler. 11466701Sgblack@eecs.umich.edutemplate <class OS> 11476701Sgblack@eecs.umich.eduSyscallReturn 11481706SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 11493669Sbinkertn@umich.edu ThreadContext *tc) 11503669Sbinkertn@umich.edu{ 11513669Sbinkertn@umich.edu std::string path; 11521706SN/A 11531706SN/A int index = 0; 11541706SN/A if (!tc->getMemProxy().tryReadString(path, 11551706SN/A process->getSyscallArg(tc, index))) { 11562218SN/A return -EFAULT; 11571706SN/A } 11588706Sandreas.hansson@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 11591706SN/A 11601706SN/A // Adjust path for current working directory 11611706SN/A path = process->fullPath(path); 11621706SN/A 11631706SN/A#if NO_STAT64 11641706SN/A struct stat hostBuf; 11651706SN/A int result = lstat(path.c_str(), &hostBuf); 11661706SN/A#else 11673114Sgblack@eecs.umich.edu struct stat64 hostBuf; 11682680Sktlim@umich.edu int result = lstat64(path.c_str(), &hostBuf); 11691706SN/A#endif 11706701Sgblack@eecs.umich.edu 117110931Sbrandon.potter@amd.com if (result < 0) 11726701Sgblack@eecs.umich.edu return -errno; 11731706SN/A 117410932Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 117510931Sbrandon.potter@amd.com 11761706SN/A return 0; 11771706SN/A} 11781706SN/A 117910931Sbrandon.potter@amd.com/// Target fstat() handler. 11801706SN/Atemplate <class OS> 11811706SN/ASyscallReturn 11822218SN/AfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 11831706SN/A{ 11848706Sandreas.hansson@arm.com int index = 0; 11851706SN/A int tgt_fd = p->getSyscallArg(tc, index); 11861706SN/A Addr bufPtr = p->getSyscallArg(tc, index); 11871706SN/A 11881706SN/A DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 11891706SN/A 11901999SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 11911999SN/A if (!ffdp) 11921999SN/A return -EBADF; 11933114Sgblack@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 11942680Sktlim@umich.edu 11951999SN/A struct stat hostBuf; 11966701Sgblack@eecs.umich.edu int result = fstat(sim_fd, &hostBuf); 119710931Sbrandon.potter@amd.com 119810931Sbrandon.potter@amd.com if (result < 0) 119910932Sbrandon.potter@amd.com return -errno; 120010931Sbrandon.potter@amd.com 12011999SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12021999SN/A 12038852Sandreas.hansson@arm.com return 0; 12046701Sgblack@eecs.umich.edu} 12056701Sgblack@eecs.umich.edu 12061999SN/A 12076227Snate@binkert.org/// Target statfs() handler. 12081999SN/Atemplate <class OS> 12092461SN/ASyscallReturn 12108852Sandreas.hansson@arm.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 12118852Sandreas.hansson@arm.com ThreadContext *tc) 12128737Skoansin.tan@gmail.com{ 12131999SN/A#if NO_STATFS 12148852Sandreas.hansson@arm.com warn("Host OS cannot support calls to statfs. Ignoring syscall"); 12158852Sandreas.hansson@arm.com#else 12161999SN/A std::string path; 12171999SN/A 121810931Sbrandon.potter@amd.com int index = 0; 12191999SN/A if (!tc->getMemProxy().tryReadString(path, 12206227Snate@binkert.org process->getSyscallArg(tc, index))) { 12211999SN/A return -EFAULT; 12221999SN/A } 12231999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 12242218SN/A 12251999SN/A // Adjust path for current working directory 122610629Sjthestness@gmail.com path = process->fullPath(path); 12271999SN/A 12281999SN/A struct statfs hostBuf; 122911385Sbrandon.potter@amd.com int result = statfs(path.c_str(), &hostBuf); 1230360SN/A 12311450SN/A if (result < 0) 123211385Sbrandon.potter@amd.com return -errno; 123311385Sbrandon.potter@amd.com 1234360SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12356701Sgblack@eecs.umich.edu#endif 12366701Sgblack@eecs.umich.edu return 0; 12376701Sgblack@eecs.umich.edu} 123811383Sbrandon.potter@amd.com 123911383Sbrandon.potter@amd.comtemplate <class OS> 12408324Ssteve.reinhardt@amd.comSyscallReturn 124110486Stjablin@gmail.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1242360SN/A{ 124311385Sbrandon.potter@amd.com int index = 0; 124411385Sbrandon.potter@amd.com TheISA::IntReg flags = p->getSyscallArg(tc, index); 12459008Sgblack@eecs.umich.edu TheISA::IntReg newStack = p->getSyscallArg(tc, index); 124611383Sbrandon.potter@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 124711383Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 124811383Sbrandon.potter@amd.com Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 124911383Sbrandon.potter@amd.com 125011383Sbrandon.potter@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 125111383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 125211383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 125311383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 125411383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 12558324Ssteve.reinhardt@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 125611383Sbrandon.potter@amd.com return -EINVAL; 125711383Sbrandon.potter@amd.com 125811383Sbrandon.potter@amd.com ThreadContext *ctc; 125911383Sbrandon.potter@amd.com if (!(ctc = p->findFreeContext())) 126011383Sbrandon.potter@amd.com fatal("clone: no spare thread context in system"); 126111383Sbrandon.potter@amd.com 126211383Sbrandon.potter@amd.com /** 126311383Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 126411383Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 126511383Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 126611383Sbrandon.potter@amd.com * constructor. 126711383Sbrandon.potter@amd.com */ 126811383Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 126911383Sbrandon.potter@amd.com pp->executable.assign(*(new std::string(p->progName()))); 127011383Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 127111383Sbrandon.potter@amd.com pp->system = p->system; 127211383Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 127311383Sbrandon.potter@amd.com pp->input.assign("stdin"); 127411383Sbrandon.potter@amd.com pp->output.assign("stdout"); 127511383Sbrandon.potter@amd.com pp->errout.assign("stderr"); 127611383Sbrandon.potter@amd.com pp->uid = p->uid(); 127711383Sbrandon.potter@amd.com pp->euid = p->euid(); 127811383Sbrandon.potter@amd.com pp->gid = p->gid(); 127911383Sbrandon.potter@amd.com pp->egid = p->egid(); 12808324Ssteve.reinhardt@amd.com 12815877Shsul@eecs.umich.edu /* Find the first free PID that's less than the maximum */ 128210486Stjablin@gmail.com std::set<int> const& pids = p->system->PIDs; 128310486Stjablin@gmail.com int temp_pid = *pids.begin(); 128411383Sbrandon.potter@amd.com do { 128511383Sbrandon.potter@amd.com temp_pid++; 128611383Sbrandon.potter@amd.com } while (pids.find(temp_pid) != pids.end()); 128711624Smichael.lebeane@amd.com if (temp_pid >= System::maxPID) 128811624Smichael.lebeane@amd.com fatal("temp_pid is too large: %d", temp_pid); 128911624Smichael.lebeane@amd.com 129011624Smichael.lebeane@amd.com pp->pid = temp_pid; 129111624Smichael.lebeane@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 129211624Smichael.lebeane@amd.com Process *cp = pp->create(); 129311624Smichael.lebeane@amd.com delete pp; 129411624Smichael.lebeane@amd.com 129511624Smichael.lebeane@amd.com Process *owner = ctc->getProcessPtr(); 129611624Smichael.lebeane@amd.com ctc->setProcessPtr(cp); 129711624Smichael.lebeane@amd.com cp->assignThreadContext(ctc->contextId()); 129811383Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 129911383Sbrandon.potter@amd.com 1300360SN/A if (flags & OS::TGT_CLONE_PARENT_SETTID) { 130111383Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 130211383Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 13038600Ssteve.reinhardt@amd.com *ptid = cp->pid(); 130411383Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 130511383Sbrandon.potter@amd.com } 130611383Sbrandon.potter@amd.com 13078600Ssteve.reinhardt@amd.com cp->initState(); 13082544SN/A p->clone(tc, ctc, cp, flags); 13092544SN/A 131011383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 131111383Sbrandon.potter@amd.com delete cp->sigchld; 131211383Sbrandon.potter@amd.com cp->sigchld = p->sigchld; 131311386Ssteve.reinhardt@amd.com } else if (flags & OS::TGT_SIGCHLD) { 131411386Ssteve.reinhardt@amd.com *cp->sigchld = true; 131511383Sbrandon.potter@amd.com } 131611383Sbrandon.potter@amd.com 131711383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 131811383Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 131911383Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 132011383Sbrandon.potter@amd.com *ctid = cp->pid(); 132111383Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 132211383Sbrandon.potter@amd.com } 132311383Sbrandon.potter@amd.com 132411383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 132511383Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 132611383Sbrandon.potter@amd.com 132711383Sbrandon.potter@amd.com ctc->clearArchRegs(); 132811383Sbrandon.potter@amd.com 132911383Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 13308600Ssteve.reinhardt@amd.com TheISA::copyMiscRegs(tc, ctc); 13316672Sgblack@eecs.umich.edu#elif THE_ISA == SPARC_ISA 13328600Ssteve.reinhardt@amd.com TheISA::copyRegs(tc, ctc); 133311383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 133411383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 133511383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 13368601Ssteve.reinhardt@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 13372544SN/A ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 133811383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 133911383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 134011383Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 134111383Sbrandon.potter@amd.com for (int y = 8; y < 32; y++) 134211383Sbrandon.potter@amd.com ctc->setIntReg(y, tc->readIntReg(y)); 134311383Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA 134411383Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 134511383Sbrandon.potter@amd.com#endif 134611383Sbrandon.potter@amd.com 134711383Sbrandon.potter@amd.com#if THE_ISA == X86_ISA 134811383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_SETTLS) { 134911383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 135011383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 135111383Sbrandon.potter@amd.com } 135211383Sbrandon.potter@amd.com#endif 135311383Sbrandon.potter@amd.com 135411383Sbrandon.potter@amd.com if (newStack) 135511383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::StackPointerReg, newStack); 135611383Sbrandon.potter@amd.com 135711383Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 135811383Sbrandon.potter@amd.com 135911383Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 136011383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 136111383Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 136211383Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 136311383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 136411383Sbrandon.potter@amd.com#endif 136511383Sbrandon.potter@amd.com 136611383Sbrandon.potter@amd.com ctc->pcState(tc->nextInstAddr()); 136711383Sbrandon.potter@amd.com ctc->activate(); 136811383Sbrandon.potter@amd.com 136911383Sbrandon.potter@amd.com return cp->pid(); 137011392Sbrandon.potter@amd.com} 137111392Sbrandon.potter@amd.com 137211392Sbrandon.potter@amd.com/// Target fstatfs() handler. 137311392Sbrandon.potter@amd.comtemplate <class OS> 137411392Sbrandon.potter@amd.comSyscallReturn 137511392Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 137611392Sbrandon.potter@amd.com{ 137711392Sbrandon.potter@amd.com int index = 0; 137811392Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 137911392Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 138011392Sbrandon.potter@amd.com 138111392Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 138211392Sbrandon.potter@amd.com if (!ffdp) 138311392Sbrandon.potter@amd.com return -EBADF; 138411392Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 138511392Sbrandon.potter@amd.com 138611392Sbrandon.potter@amd.com struct statfs hostBuf; 138711392Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 138811392Sbrandon.potter@amd.com 138911392Sbrandon.potter@amd.com if (result < 0) 139011392Sbrandon.potter@amd.com return -errno; 139111392Sbrandon.potter@amd.com 139211392Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 139311392Sbrandon.potter@amd.com 139411392Sbrandon.potter@amd.com return 0; 139511383Sbrandon.potter@amd.com} 139611383Sbrandon.potter@amd.com 139711383Sbrandon.potter@amd.com 139811383Sbrandon.potter@amd.com/// Target writev() handler. 139911383Sbrandon.potter@amd.comtemplate <class OS> 14001458SN/ASyscallReturn 1401360SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1402360SN/A{ 140311593Santhony.gutierrez@amd.com int index = 0; 140411593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 140511593Santhony.gutierrez@amd.com 140611593Santhony.gutierrez@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 140711593Santhony.gutierrez@amd.com if (!hbfdp) 140811593Santhony.gutierrez@amd.com return -EBADF; 140911593Santhony.gutierrez@amd.com int sim_fd = hbfdp->getSimFD(); 141011593Santhony.gutierrez@amd.com 141111593Santhony.gutierrez@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 141211593Santhony.gutierrez@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 141311593Santhony.gutierrez@amd.com size_t count = p->getSyscallArg(tc, index); 141411593Santhony.gutierrez@amd.com struct iovec hiov[count]; 141511593Santhony.gutierrez@amd.com for (size_t i = 0; i < count; ++i) { 141611593Santhony.gutierrez@amd.com typename OS::tgt_iovec tiov; 141711593Santhony.gutierrez@amd.com 141811593Santhony.gutierrez@amd.com prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 141911593Santhony.gutierrez@amd.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 142011594Santhony.gutierrez@amd.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 142111593Santhony.gutierrez@amd.com hiov[i].iov_base = new char [hiov[i].iov_len]; 142211593Santhony.gutierrez@amd.com prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 142311593Santhony.gutierrez@amd.com hiov[i].iov_len); 142411593Santhony.gutierrez@amd.com } 142511385Sbrandon.potter@amd.com 142611385Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 142711385Sbrandon.potter@amd.com 142811385Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) 142911385Sbrandon.potter@amd.com delete [] (char *)hiov[i].iov_base; 143011385Sbrandon.potter@amd.com 143111385Sbrandon.potter@amd.com if (result < 0) 143211385Sbrandon.potter@amd.com return -errno; 143311385Sbrandon.potter@amd.com 143411385Sbrandon.potter@amd.com return result; 143511385Sbrandon.potter@amd.com} 143611385Sbrandon.potter@amd.com 143711385Sbrandon.potter@amd.com/// Real mmap handler. 143811385Sbrandon.potter@amd.comtemplate <class OS> 143911385Sbrandon.potter@amd.comSyscallReturn 144011385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1441378SN/A bool is_mmap2) 1442360SN/A{ 14431450SN/A int index = 0; 14443114Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 14452680Sktlim@umich.edu uint64_t length = p->getSyscallArg(tc, index); 1446360SN/A int prot = p->getSyscallArg(tc, index); 14476701Sgblack@eecs.umich.edu int tgt_flags = p->getSyscallArg(tc, index); 14486701Sgblack@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 14496701Sgblack@eecs.umich.edu int offset = p->getSyscallArg(tc, index); 1450360SN/A 1451360SN/A if (is_mmap2) 14522064SN/A offset *= TheISA::PageBytes; 14535877Shsul@eecs.umich.edu 14542064SN/A if (start & (TheISA::PageBytes - 1) || 14558737Skoansin.tan@gmail.com offset & (TheISA::PageBytes - 1) || 14568737Skoansin.tan@gmail.com (tgt_flags & OS::TGT_MAP_PRIVATE && 14572064SN/A tgt_flags & OS::TGT_MAP_SHARED) || 1458360SN/A (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 14595877Shsul@eecs.umich.edu !(tgt_flags & OS::TGT_MAP_SHARED)) || 14605877Shsul@eecs.umich.edu !length) { 14615877Shsul@eecs.umich.edu return -EINVAL; 14628737Skoansin.tan@gmail.com } 14638737Skoansin.tan@gmail.com 14645877Shsul@eecs.umich.edu if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 14655877Shsul@eecs.umich.edu // With shared mmaps, there are two cases to consider: 14662064SN/A // 1) anonymous: writes should modify the mapping and this should be 146710794Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 146810794Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 14692064SN/A // structure which maintains information about the which virtual 1470360SN/A // memory areas are shared. If that structure existed, it would be 1471360SN/A // possible to make the translations point to the same frames. 14728706Sandreas.hansson@arm.com // 2) file-backed: writes should modify the mapping and the file 14731458SN/A // which is backed by the mapping. The shared mapping problem is the 1474360SN/A // same as what was mentioned about the anonymous mappings. For 1475360SN/A // file-backed mappings, the writes to the file are difficult 147610796Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 147710796Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 147810796Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 147910796Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 148010796Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 148110796Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 148210796Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 148310796Sbrandon.potter@amd.com // a call to msync, but that again would require more information 148410796Sbrandon.potter@amd.com // than we currently maintain. 148510796Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 148610796Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 148710796Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 148810796Sbrandon.potter@amd.com } 148910796Sbrandon.potter@amd.com 149010796Sbrandon.potter@amd.com length = roundUp(length, TheISA::PageBytes); 149110796Sbrandon.potter@amd.com 149210796Sbrandon.potter@amd.com int sim_fd = -1; 149310796Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 149410796Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 149511337SMichael.Lebeane@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 149611337SMichael.Lebeane@amd.com 149711337SMichael.Lebeane@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 149811337SMichael.Lebeane@amd.com if (dfdp) { 149911337SMichael.Lebeane@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 150011337SMichael.Lebeane@amd.com return emul_driver->mmap(p, tc, start, length, prot, 150111337SMichael.Lebeane@amd.com tgt_flags, tgt_fd, offset); 150211337SMichael.Lebeane@amd.com } 150311337SMichael.Lebeane@amd.com 150411337SMichael.Lebeane@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 150511337SMichael.Lebeane@amd.com if (!ffdp) 150611337SMichael.Lebeane@amd.com return -EBADF; 150711337SMichael.Lebeane@amd.com sim_fd = ffdp->getSimFD(); 150811337SMichael.Lebeane@amd.com 150911337SMichael.Lebeane@amd.com pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 151011337SMichael.Lebeane@amd.com sim_fd, offset); 151111337SMichael.Lebeane@amd.com 1512378SN/A if (pmap == (decltype(pmap))-1) { 1513360SN/A warn("mmap: failed to map file into host address space"); 15141450SN/A return -errno; 15153114Sgblack@eecs.umich.edu } 15162680Sktlim@umich.edu } 1517360SN/A 15186701Sgblack@eecs.umich.edu // Extend global mmap region if necessary. Note that we ignore the 15196701Sgblack@eecs.umich.edu // start address unless MAP_FIXED is specified. 1520360SN/A if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 152110796Sbrandon.potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 1522360SN/A Addr mmap_end = mem_state->getMmapEnd(); 15236109Ssanchezd@stanford.edu 15246109Ssanchezd@stanford.edu start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1525360SN/A mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 15268706Sandreas.hansson@arm.com 1527360SN/A mem_state->setMmapEnd(mmap_end); 15281458SN/A } 1529360SN/A 1530360SN/A DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1531360SN/A start, start + length - 1); 15321999SN/A 15331999SN/A // We only allow mappings to overwrite existing mappings if 15341999SN/A // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 15353114Sgblack@eecs.umich.edu // because we ignore the start hint if TGT_MAP_FIXED is not set. 15362680Sktlim@umich.edu int clobber = tgt_flags & OS::TGT_MAP_FIXED; 15371999SN/A if (clobber) { 15381999SN/A for (auto tc : p->system->threadContexts) { 15391999SN/A // If we might be overwriting old mappings, we need to 15406701Sgblack@eecs.umich.edu // invalidate potentially stale mappings out of the TLBs. 15418852Sandreas.hansson@arm.com tc->getDTBPtr()->flushAll(); 15426701Sgblack@eecs.umich.edu tc->getITBPtr()->flushAll(); 15436701Sgblack@eecs.umich.edu } 15446701Sgblack@eecs.umich.edu } 15451999SN/A 15466701Sgblack@eecs.umich.edu // Allocate physical memory and map it in. If the page table is already 15476701Sgblack@eecs.umich.edu // mapped and clobber is not set, the simulator will issue throw a 15488706Sandreas.hansson@arm.com // fatal and bail out of the simulation. 15491999SN/A p->allocateMem(start, length, clobber); 15501999SN/A 15511999SN/A // Transfer content into target address space. 15521999SN/A SETranslatingPortProxy &tp = tc->getMemProxy(); 15538737Skoansin.tan@gmail.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 15548737Skoansin.tan@gmail.com // In general, we should zero the mapped area for anonymous mappings, 15551999SN/A // with something like: 15563669Sbinkertn@umich.edu // tp.memsetBlob(start, 0, length); 15573669Sbinkertn@umich.edu // However, given that we don't support sparse mappings, and 15583669Sbinkertn@umich.edu // some applications can map a couple of gigabytes of space 15593669Sbinkertn@umich.edu // (intending sparse usage), that can get painfully expensive. 15601999SN/A // Fortunately, since we don't properly implement munmap either, 15611999SN/A // there's no danger of remapping used memory, so for now all 15621999SN/A // newly mapped memory should already be zeroed so we can skip it. 15631999SN/A } else { 15641999SN/A // It is possible to mmap an area larger than a file, however 15651999SN/A // accessing unmapped portions the system triggers a "Bus error" 15661999SN/A // on the host. We must know when to stop copying the file from 1567378SN/A // the host into the target address space. 1568360SN/A struct stat file_stat; 15691450SN/A if (fstat(sim_fd, &file_stat) > 0) 15703114Sgblack@eecs.umich.edu fatal("mmap: cannot stat file"); 15712680Sktlim@umich.edu 1572360SN/A // Copy the portion of the file that is resident. This requires 15736701Sgblack@eecs.umich.edu // checking both the mmap size and the filesize that we are 15746701Sgblack@eecs.umich.edu // trying to mmap into this space; the mmap size also depends 15756701Sgblack@eecs.umich.edu // on the specified offset into the file. 1576360SN/A uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 15773670Sbinkertn@umich.edu length); 15783670Sbinkertn@umich.edu tp.writeBlob(start, pmap, size); 1579360SN/A 1580360SN/A // Cleanup the mmap region before exiting this function. 1581360SN/A munmap(pmap, length); 1582360SN/A 1583360SN/A // Maintain the symbol table for dynamic executables. 1584360SN/A // The loader will call mmap to map the images into its address 1585360SN/A // space and we intercept that here. We can verify that we are 1586360SN/A // executing inside the loader by checking the program counter value. 1587360SN/A // XXX: with multiprogrammed workloads or multi-node configurations, 1588360SN/A // this will not work since there is a single global symbol table. 1589360SN/A ObjectFile *interpreter = p->getInterpreter(); 1590360SN/A if (interpreter) { 1591360SN/A Addr text_start = interpreter->textBase(); 1592360SN/A Addr text_end = text_start + interpreter->textSize(); 1593360SN/A 1594360SN/A Addr pc = tc->pcState().pc(); 1595360SN/A 15963670Sbinkertn@umich.edu if (pc >= text_start && pc < text_end) { 15973670Sbinkertn@umich.edu std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 159810796Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 15998737Skoansin.tan@gmail.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 16008737Skoansin.tan@gmail.com 16013670Sbinkertn@umich.edu if (lib) { 16023670Sbinkertn@umich.edu lib->loadAllSymbols(debugSymbolTable, 16033670Sbinkertn@umich.edu lib->textBase(), start); 16043670Sbinkertn@umich.edu } 16053670Sbinkertn@umich.edu } 16063670Sbinkertn@umich.edu } 16073670Sbinkertn@umich.edu 16083670Sbinkertn@umich.edu // Note that we do not zero out the remainder of the mapping. This 16093670Sbinkertn@umich.edu // is done by a real system, but it probably will not affect 16103670Sbinkertn@umich.edu // execution (hopefully). 16113670Sbinkertn@umich.edu } 16123670Sbinkertn@umich.edu 16133670Sbinkertn@umich.edu return start; 16148706Sandreas.hansson@arm.com} 1615360SN/A 16161458SN/Atemplate <class OS> 1617360SN/ASyscallReturn 1618360SN/Apwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16196683Stjones1@inf.ed.ac.uk{ 16206683Stjones1@inf.ed.ac.uk int index = 0; 16216683Stjones1@inf.ed.ac.uk int tgt_fd = p->getSyscallArg(tc, index); 16226683Stjones1@inf.ed.ac.uk Addr bufPtr = p->getSyscallArg(tc, index); 16236683Stjones1@inf.ed.ac.uk int nbytes = p->getSyscallArg(tc, index); 16246683Stjones1@inf.ed.ac.uk int offset = p->getSyscallArg(tc, index); 16256701Sgblack@eecs.umich.edu 16266701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16276683Stjones1@inf.ed.ac.uk if (!ffdp) 16286683Stjones1@inf.ed.ac.uk return -EBADF; 16297823Ssteve.reinhardt@amd.com int sim_fd = ffdp->getSimFD(); 16306683Stjones1@inf.ed.ac.uk 16316683Stjones1@inf.ed.ac.uk BufferArg bufArg(bufPtr, nbytes); 16326683Stjones1@inf.ed.ac.uk bufArg.copyIn(tc->getMemProxy()); 16336683Stjones1@inf.ed.ac.uk 16346683Stjones1@inf.ed.ac.uk int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 16356683Stjones1@inf.ed.ac.uk 16368737Skoansin.tan@gmail.com return (bytes_written == -1) ? -errno : bytes_written; 16376683Stjones1@inf.ed.ac.uk} 16386683Stjones1@inf.ed.ac.uk 16398706Sandreas.hansson@arm.com/// Target mmap() handler. 16406683Stjones1@inf.ed.ac.uktemplate <class OS> 16416683Stjones1@inf.ed.ac.ukSyscallReturn 16426683Stjones1@inf.ed.ac.ukmmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16436683Stjones1@inf.ed.ac.uk{ 16442553SN/A return mmapImpl<OS>(desc, num, p, tc, false); 16456684Stjones1@inf.ed.ac.uk} 16466684Stjones1@inf.ed.ac.uk 16476684Stjones1@inf.ed.ac.uk/// Target mmap2() handler. 16486684Stjones1@inf.ed.ac.uktemplate <class OS> 16496684Stjones1@inf.ed.ac.ukSyscallReturn 16506684Stjones1@inf.ed.ac.ukmmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16516684Stjones1@inf.ed.ac.uk{ 165210796Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 16536684Stjones1@inf.ed.ac.uk} 16546684Stjones1@inf.ed.ac.uk 16556701Sgblack@eecs.umich.edu/// Target getrlimit() handler. 16566701Sgblack@eecs.umich.edutemplate <class OS> 165711321Ssteve.reinhardt@amd.comSyscallReturn 16586684Stjones1@inf.ed.ac.ukgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 16598737Skoansin.tan@gmail.com ThreadContext *tc) 16608852Sandreas.hansson@arm.com{ 16618852Sandreas.hansson@arm.com int index = 0; 16626684Stjones1@inf.ed.ac.uk unsigned resource = process->getSyscallArg(tc, index); 16636684Stjones1@inf.ed.ac.uk TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 16646684Stjones1@inf.ed.ac.uk 16652553SN/A switch (resource) { 16662553SN/A case OS::TGT_RLIMIT_STACK: 16671354SN/A // max stack size in bytes: make up a number (8MB for now) 1668 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1669 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1670 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1671 break; 1672 1673 case OS::TGT_RLIMIT_DATA: 1674 // max data segment size in bytes: make up a number 1675 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1676 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1677 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1678 break; 1679 1680 default: 1681 warn("getrlimit: unimplemented resource %d", resource); 1682 return -EINVAL; 1683 break; 1684 } 1685 1686 rlp.copyOut(tc->getMemProxy()); 1687 return 0; 1688} 1689 1690/// Target clock_gettime() function. 1691template <class OS> 1692SyscallReturn 1693clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1694{ 1695 int index = 1; 1696 //int clk_id = p->getSyscallArg(tc, index); 1697 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1698 1699 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 1700 tp->tv_sec += seconds_since_epoch; 1701 tp->tv_sec = TheISA::htog(tp->tv_sec); 1702 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 1703 1704 tp.copyOut(tc->getMemProxy()); 1705 1706 return 0; 1707} 1708 1709/// Target clock_getres() function. 1710template <class OS> 1711SyscallReturn 1712clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1713{ 1714 int index = 1; 1715 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1716 1717 // Set resolution at ns, which is what clock_gettime() returns 1718 tp->tv_sec = 0; 1719 tp->tv_nsec = 1; 1720 1721 tp.copyOut(tc->getMemProxy()); 1722 1723 return 0; 1724} 1725 1726/// Target gettimeofday() handler. 1727template <class OS> 1728SyscallReturn 1729gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 1730 ThreadContext *tc) 1731{ 1732 int index = 0; 1733 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1734 1735 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1736 tp->tv_sec += seconds_since_epoch; 1737 tp->tv_sec = TheISA::htog(tp->tv_sec); 1738 tp->tv_usec = TheISA::htog(tp->tv_usec); 1739 1740 tp.copyOut(tc->getMemProxy()); 1741 1742 return 0; 1743} 1744 1745 1746/// Target utimes() handler. 1747template <class OS> 1748SyscallReturn 1749utimesFunc(SyscallDesc *desc, int callnum, Process *process, 1750 ThreadContext *tc) 1751{ 1752 std::string path; 1753 1754 int index = 0; 1755 if (!tc->getMemProxy().tryReadString(path, 1756 process->getSyscallArg(tc, index))) { 1757 return -EFAULT; 1758 } 1759 1760 TypedBufferArg<typename OS::timeval [2]> 1761 tp(process->getSyscallArg(tc, index)); 1762 tp.copyIn(tc->getMemProxy()); 1763 1764 struct timeval hostTimeval[2]; 1765 for (int i = 0; i < 2; ++i) { 1766 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 1767 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 1768 } 1769 1770 // Adjust path for current working directory 1771 path = process->fullPath(path); 1772 1773 int result = utimes(path.c_str(), hostTimeval); 1774 1775 if (result < 0) 1776 return -errno; 1777 1778 return 0; 1779} 1780 1781template <class OS> 1782SyscallReturn 1783execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1784{ 1785 desc->setFlags(0); 1786 1787 int index = 0; 1788 std::string path; 1789 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 1790 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 1791 return -EFAULT; 1792 1793 if (access(path.c_str(), F_OK) == -1) 1794 return -EACCES; 1795 1796 auto read_in = [](std::vector<std::string> & vect, 1797 SETranslatingPortProxy & mem_proxy, 1798 Addr mem_loc) 1799 { 1800 for (int inc = 0; ; inc++) { 1801 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 1802 b.copyIn(mem_proxy); 1803 1804 if (!*(Addr*)b.bufferPtr()) 1805 break; 1806 1807 vect.push_back(std::string()); 1808 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 1809 } 1810 }; 1811 1812 /** 1813 * Note that ProcessParams is generated by swig and there are no other 1814 * examples of how to create anything but this default constructor. The 1815 * fields are manually initialized instead of passing parameters to the 1816 * constructor. 1817 */ 1818 ProcessParams *pp = new ProcessParams(); 1819 pp->executable = path; 1820 Addr argv_mem_loc = p->getSyscallArg(tc, index); 1821 read_in(pp->cmd, mem_proxy, argv_mem_loc); 1822 Addr envp_mem_loc = p->getSyscallArg(tc, index); 1823 read_in(pp->env, mem_proxy, envp_mem_loc); 1824 pp->uid = p->uid(); 1825 pp->egid = p->egid(); 1826 pp->euid = p->euid(); 1827 pp->gid = p->gid(); 1828 pp->ppid = p->ppid(); 1829 pp->pid = p->pid(); 1830 pp->input.assign("cin"); 1831 pp->output.assign("cout"); 1832 pp->errout.assign("cerr"); 1833 pp->cwd.assign(p->getcwd()); 1834 pp->system = p->system; 1835 /** 1836 * Prevent process object creation with identical PIDs (which will trip 1837 * a fatal check in Process constructor). The execve call is supposed to 1838 * take over the currently executing process' identity but replace 1839 * whatever it is doing with a new process image. Instead of hijacking 1840 * the process object in the simulator, we create a new process object 1841 * and bind to the previous process' thread below (hijacking the thread). 1842 */ 1843 p->system->PIDs.erase(p->pid()); 1844 Process *new_p = pp->create(); 1845 delete pp; 1846 1847 /** 1848 * Work through the file descriptor array and close any files marked 1849 * close-on-exec. 1850 */ 1851 new_p->fds = p->fds; 1852 for (int i = 0; i < new_p->fds->getSize(); i++) { 1853 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 1854 if (fdep && fdep->getCOE()) 1855 new_p->fds->closeFDEntry(i); 1856 } 1857 1858 *new_p->sigchld = true; 1859 1860 delete p; 1861 tc->clearArchRegs(); 1862 tc->setProcessPtr(new_p); 1863 new_p->assignThreadContext(tc->contextId()); 1864 new_p->initState(); 1865 tc->activate(); 1866 TheISA::PCState pcState = tc->pcState(); 1867 tc->setNPC(pcState.instAddr()); 1868 1869 desc->setFlags(SyscallDesc::SuppressReturnValue); 1870 return 0; 1871} 1872 1873/// Target getrusage() function. 1874template <class OS> 1875SyscallReturn 1876getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 1877 ThreadContext *tc) 1878{ 1879 int index = 0; 1880 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 1881 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1882 1883 rup->ru_utime.tv_sec = 0; 1884 rup->ru_utime.tv_usec = 0; 1885 rup->ru_stime.tv_sec = 0; 1886 rup->ru_stime.tv_usec = 0; 1887 rup->ru_maxrss = 0; 1888 rup->ru_ixrss = 0; 1889 rup->ru_idrss = 0; 1890 rup->ru_isrss = 0; 1891 rup->ru_minflt = 0; 1892 rup->ru_majflt = 0; 1893 rup->ru_nswap = 0; 1894 rup->ru_inblock = 0; 1895 rup->ru_oublock = 0; 1896 rup->ru_msgsnd = 0; 1897 rup->ru_msgrcv = 0; 1898 rup->ru_nsignals = 0; 1899 rup->ru_nvcsw = 0; 1900 rup->ru_nivcsw = 0; 1901 1902 switch (who) { 1903 case OS::TGT_RUSAGE_SELF: 1904 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1905 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 1906 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 1907 break; 1908 1909 case OS::TGT_RUSAGE_CHILDREN: 1910 // do nothing. We have no child processes, so they take no time. 1911 break; 1912 1913 default: 1914 // don't really handle THREAD or CHILDREN, but just warn and 1915 // plow ahead 1916 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1917 who); 1918 } 1919 1920 rup.copyOut(tc->getMemProxy()); 1921 1922 return 0; 1923} 1924 1925/// Target times() function. 1926template <class OS> 1927SyscallReturn 1928timesFunc(SyscallDesc *desc, int callnum, Process *process, 1929 ThreadContext *tc) 1930{ 1931 int index = 0; 1932 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 1933 1934 // Fill in the time structure (in clocks) 1935 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 1936 bufp->tms_utime = clocks; 1937 bufp->tms_stime = 0; 1938 bufp->tms_cutime = 0; 1939 bufp->tms_cstime = 0; 1940 1941 // Convert to host endianness 1942 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 1943 1944 // Write back 1945 bufp.copyOut(tc->getMemProxy()); 1946 1947 // Return clock ticks since system boot 1948 return clocks; 1949} 1950 1951/// Target time() function. 1952template <class OS> 1953SyscallReturn 1954timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 1955{ 1956 typename OS::time_t sec, usec; 1957 getElapsedTimeMicro(sec, usec); 1958 sec += seconds_since_epoch; 1959 1960 int index = 0; 1961 Addr taddr = (Addr)process->getSyscallArg(tc, index); 1962 if (taddr != 0) { 1963 typename OS::time_t t = sec; 1964 t = TheISA::htog(t); 1965 SETranslatingPortProxy &p = tc->getMemProxy(); 1966 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 1967 } 1968 return sec; 1969} 1970 1971template <class OS> 1972SyscallReturn 1973tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 1974{ 1975 int index = 0; 1976 int tgid = process->getSyscallArg(tc, index); 1977 int tid = process->getSyscallArg(tc, index); 1978 int sig = process->getSyscallArg(tc, index); 1979 1980 /** 1981 * This system call is intended to allow killing a specific thread 1982 * within an arbitrary thread group if sanctioned with permission checks. 1983 * It's usually true that threads share the termination signal as pointed 1984 * out by the pthread_kill man page and this seems to be the intended 1985 * usage. Due to this being an emulated environment, assume the following: 1986 * Threads are allowed to call tgkill because the EUID for all threads 1987 * should be the same. There is no signal handling mechanism for kernel 1988 * registration of signal handlers since signals are poorly supported in 1989 * emulation mode. Since signal handlers cannot be registered, all 1990 * threads within in a thread group must share the termination signal. 1991 * We never exhaust PIDs so there's no chance of finding the wrong one 1992 * due to PID rollover. 1993 */ 1994 1995 System *sys = tc->getSystemPtr(); 1996 Process *tgt_proc = nullptr; 1997 for (int i = 0; i < sys->numContexts(); i++) { 1998 Process *temp = sys->threadContexts[i]->getProcessPtr(); 1999 if (temp->pid() == tid) { 2000 tgt_proc = temp; 2001 break; 2002 } 2003 } 2004 2005 if (sig != 0 || sig != OS::TGT_SIGABRT) 2006 return -EINVAL; 2007 2008 if (tgt_proc == nullptr) 2009 return -ESRCH; 2010 2011 if (tgid != -1 && tgt_proc->tgid() != tgid) 2012 return -ESRCH; 2013 2014 if (sig == OS::TGT_SIGABRT) 2015 exitGroupFunc(desc, 252, process, tc); 2016 2017 return 0; 2018} 2019 2020 2021#endif // __SIM_SYSCALL_EMUL_HH__ 2022