syscall_emul.hh revision 13995
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 5211799Sbrandon.potter@amd.com#else 5311799Sbrandon.potter@amd.com#define NO_STAT64 0 5411799Sbrandon.potter@amd.com#endif 5511799Sbrandon.potter@amd.com 5611799Sbrandon.potter@amd.com/// 5711799Sbrandon.potter@amd.com/// @file syscall_emul.hh 58360SN/A/// 59360SN/A/// This file defines objects used to emulate syscalls from the target 60360SN/A/// application on the host machine. 61360SN/A 62360SN/A#if defined(__linux__) 63360SN/A#include <sys/eventfd.h> 641809SN/A#include <sys/statfs.h> 6511800Sbrandon.potter@amd.com 6611392Sbrandon.potter@amd.com#else 671809SN/A#include <sys/mount.h> 6811392Sbrandon.potter@amd.com 6911383Sbrandon.potter@amd.com#endif 703113Sgblack@eecs.umich.edu 7111799Sbrandon.potter@amd.com#ifdef __CYGWIN32__ 7211759Sbrandon.potter@amd.com#include <sys/fcntl.h> 7311812Sbaz21@cam.ac.uk 7411812Sbaz21@cam.ac.uk#endif 7511799Sbrandon.potter@amd.com#include <fcntl.h> 768229Snate@binkert.org#include <net/if.h> 778229Snate@binkert.org#include <poll.h> 7811594Santhony.gutierrez@amd.com#include <sys/ioctl.h> 797075Snate@binkert.org#include <sys/mman.h> 808229Snate@binkert.org#include <sys/socket.h> 8111856Sbrandon.potter@amd.com#include <sys/stat.h> 827075Snate@binkert.org#include <sys/time.h> 83360SN/A#include <sys/types.h> 8411800Sbrandon.potter@amd.com#include <sys/uio.h> 8511392Sbrandon.potter@amd.com#include <unistd.h> 862462SN/A 871354SN/A#include <cerrno> 886216Snate@binkert.org#include <memory> 896658Snate@binkert.org#include <string> 902474SN/A 912680Sktlim@umich.edu#include "arch/generic/tlb.hh" 928229Snate@binkert.org#include "arch/utility.hh" 9310496Ssteve.reinhardt@amd.com#include "base/intmath.hh" 948229Snate@binkert.org#include "base/loader/object_file.hh" 9511794Sbrandon.potter@amd.com#include "base/logging.hh" 9610497Ssteve.reinhardt@amd.com#include "base/trace.hh" 9711794Sbrandon.potter@amd.com#include "base/types.hh" 98360SN/A#include "config/the_isa.hh" 9911794Sbrandon.potter@amd.com#include "cpu/base.hh" 100360SN/A#include "cpu/thread_context.hh" 101360SN/A#include "mem/page_table.hh" 102360SN/A#include "params/Process.hh" 103360SN/A#include "sim/emul_driver.hh" 104360SN/A#include "sim/futex_map.hh" 105360SN/A#include "sim/process.hh" 106360SN/A#include "sim/syscall_debug_macros.hh" 107360SN/A#include "sim/syscall_desc.hh" 108360SN/A#include "sim/syscall_emul_buf.hh" 109360SN/A#include "sim/syscall_return.hh" 110378SN/A 1111706SN/A#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN) 11211851Sbrandon.potter@amd.com#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) 113378SN/A#endif 114378SN/A 115378SN/A////////////////////////////////////////////////////////////////////// 116378SN/A// 117378SN/A// The following emulation functions are generic enough that they 1181706SN/A// don't need to be recompiled for different emulated OS's. They are 11911851Sbrandon.potter@amd.com// defined in sim/syscall_emul.cc. 120360SN/A// 12111760Sbrandon.potter@amd.com////////////////////////////////////////////////////////////////////// 12211760Sbrandon.potter@amd.com 12311851Sbrandon.potter@amd.comvoid warnUnsupportedOS(std::string syscall_name); 12411760Sbrandon.potter@amd.com 1256109Ssanchezd@stanford.edu/// Handler for unimplemented syscalls that we haven't thought about. 1261706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, ThreadContext *tc); 12711851Sbrandon.potter@amd.com 128378SN/A/// Handler for unimplemented syscalls that we never intend to 1296109Ssanchezd@stanford.edu/// implement (signal handling, etc.) and should not affect the correct 1306109Ssanchezd@stanford.edu/// behavior of the program. Print a warning only if the appropriate 13111851Sbrandon.potter@amd.com/// trace flag is enabled. Return success to the target program. 1326109Ssanchezd@stanford.eduSyscallReturn ignoreFunc(SyscallDesc *desc, int num, ThreadContext *tc); 133378SN/A 1341706SN/A// Target fallocateFunc() handler. 13511851Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, ThreadContext *tc); 136378SN/A 1375748SSteve.Reinhardt@amd.com/// Target exit() handler: terminate current context. 1385748SSteve.Reinhardt@amd.comSyscallReturn exitFunc(SyscallDesc *desc, int num, ThreadContext *tc); 13911851Sbrandon.potter@amd.com 140378SN/A/// Target exit_group() handler: terminate simulation. (exit all threads) 141378SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1421706SN/A 14311851Sbrandon.potter@amd.com/// Target set_tid_address() handler. 144378SN/ASyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, ThreadContext *tc); 145378SN/A 1461706SN/A/// Target getpagesize() handler. 14711851Sbrandon.potter@amd.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, ThreadContext *tc); 148378SN/A 149378SN/A/// Target brk() handler: set brk address. 1501706SN/ASyscallReturn brkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 15111851Sbrandon.potter@amd.com 152378SN/A/// Target close() handler. 153378SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1541706SN/A 15511851Sbrandon.potter@amd.com/// Target lseek() handler. 156378SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1574118Sgblack@eecs.umich.edu 1584118Sgblack@eecs.umich.edu/// Target _llseek() handler. 15911851Sbrandon.potter@amd.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1604118Sgblack@eecs.umich.edu 161378SN/A/// Target munmap() handler. 1621706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, ThreadContext *tc); 16311851Sbrandon.potter@amd.com 164378SN/A/// Target shutdown() handler. 165378SN/ASyscallReturn shutdownFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1661706SN/A 16711851Sbrandon.potter@amd.com/// Target gethostname() handler. 168360SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1695513SMichael.Adler@intel.com 1705513SMichael.Adler@intel.com/// Target getcwd() handler. 17111851Sbrandon.potter@amd.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1725513SMichael.Adler@intel.com 17310203SAli.Saidi@ARM.com/// Target readlink() handler. 17410203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc, 17511851Sbrandon.potter@amd.com int index = 0); 17610203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1775513SMichael.Adler@intel.com 17811851Sbrandon.potter@amd.com/// Target unlink() handler. 1795513SMichael.Adler@intel.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, ThreadContext *tc, 180511SN/A int index); 18110633Smichaelupton@gmail.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 18211851Sbrandon.potter@amd.com 18310633Smichaelupton@gmail.com/// Target link() handler 1841706SN/ASyscallReturn linkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 18511851Sbrandon.potter@amd.com 186511SN/A/// Target symlink() handler. 1875513SMichael.Adler@intel.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1885513SMichael.Adler@intel.com 18911851Sbrandon.potter@amd.com/// Target mkdir() handler. 1905513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, ThreadContext *tc); 191511SN/A 1921706SN/A/// Target mknod() handler. 19311851Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1941706SN/A 1951706SN/A/// Target chdir() handler. 1961706SN/ASyscallReturn chdirFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1971706SN/A 19811851Sbrandon.potter@amd.com// Target rmdir() handler. 1991706SN/ASyscallReturn rmdirFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2001706SN/A 2011706SN/A/// Target rename() handler. 2021706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 20311851Sbrandon.potter@amd.com 2041706SN/A 205511SN/A/// Target truncate() handler. 2066703Svince@csl.cornell.eduSyscallReturn truncateFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2076703Svince@csl.cornell.edu 20811851Sbrandon.potter@amd.com 2096703Svince@csl.cornell.edu/// Target ftruncate() handler. 2106685Stjones1@inf.ed.ac.ukSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2116685Stjones1@inf.ed.ac.uk 21211851Sbrandon.potter@amd.com 2136685Stjones1@inf.ed.ac.uk/// Target truncate64() handler. 2146685Stjones1@inf.ed.ac.ukSyscallReturn truncate64Func(SyscallDesc *desc, int num, ThreadContext *tc); 2155513SMichael.Adler@intel.com 2165513SMichael.Adler@intel.com/// Target ftruncate64() handler. 21711851Sbrandon.potter@amd.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc); 2185513SMichael.Adler@intel.com 21911885Sbrandon.potter@amd.com 22011885Sbrandon.potter@amd.com/// Target umask() handler. 22111885Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2225513SMichael.Adler@intel.com 2231999SN/A/// Target gettid() handler. 2241999SN/ASyscallReturn gettidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 22511851Sbrandon.potter@amd.com 2261999SN/A/// Target chown() handler. 22711885Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num, ThreadContext *tc); 22811885Sbrandon.potter@amd.com 22911885Sbrandon.potter@amd.com/// Target setpgid() handler. 2301999SN/ASyscallReturn setpgidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2311999SN/A 2321999SN/A/// Target fchown() handler. 23311851Sbrandon.potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2341999SN/A 2353079Sstever@eecs.umich.edu/// Target dup() handler. 2363079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, ThreadContext *tc); 23711851Sbrandon.potter@amd.com 2383079Sstever@eecs.umich.edu/// Target dup2() handler. 23911875Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, ThreadContext *tc); 2402093SN/A 24111851Sbrandon.potter@amd.com/// Target fcntl() handler. 2422093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2432687Sksewell@umich.edu 2442687Sksewell@umich.edu/// Target fcntl64() handler. 24511851Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, ThreadContext *tc); 2462687Sksewell@umich.edu 2472238SN/A/// Target setuid() handler. 2482238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 24911851Sbrandon.potter@amd.com 2502238SN/A/// Target pipe() handler. 2512238SN/ASyscallReturn pipeFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2522238SN/A 25311851Sbrandon.potter@amd.com/// Internal pipe() handler. 2542238SN/ASyscallReturn pipeImpl(SyscallDesc *desc, int num, ThreadContext *tc, 2552238SN/A bool pseudoPipe); 2562238SN/A 25711851Sbrandon.potter@amd.com/// Target getpid() handler. 2582238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2592238SN/A 2602238SN/A// Target getpeername() handler. 26111851Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2622238SN/A 2632238SN/A// Target bind() handler. 2642238SN/ASyscallReturn bindFunc(SyscallDesc *desc, int num, ThreadContext *tc); 26511851Sbrandon.potter@amd.com 2662238SN/A// Target listen() handler. 2672238SN/ASyscallReturn listenFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2682238SN/A 26911851Sbrandon.potter@amd.com// Target connect() handler. 2702238SN/ASyscallReturn connectFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2712238SN/A 2722238SN/A#if defined(SYS_getdents) 27311851Sbrandon.potter@amd.com// Target getdents() handler. 2742238SN/ASyscallReturn getdentsFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2756109Ssanchezd@stanford.edu#endif 2766109Ssanchezd@stanford.edu 27711851Sbrandon.potter@amd.com#if defined(SYS_getdents64) 2782238SN/A// Target getdents() handler. 2799455Smitch.hayenga+gem5@gmail.comSyscallReturn getdents64Func(SyscallDesc *desc, int num, ThreadContext *tc); 2809455Smitch.hayenga+gem5@gmail.com#endif 28111851Sbrandon.potter@amd.com 28210203SAli.Saidi@ARM.com// Target sendto() handler. 28311851Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 28411851Sbrandon.potter@amd.com 2859455Smitch.hayenga+gem5@gmail.com// Target recvfrom() handler. 2869112Smarc.orr@gmail.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2879112Smarc.orr@gmail.com 2889112Smarc.orr@gmail.com// Target recvmsg() handler. 2899112Smarc.orr@gmail.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2909112Smarc.orr@gmail.com 29111851Sbrandon.potter@amd.com// Target sendmsg() handler. 2929112Smarc.orr@gmail.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2939112Smarc.orr@gmail.com 2949112Smarc.orr@gmail.com// Target getuid() handler. 2959112Smarc.orr@gmail.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2969112Smarc.orr@gmail.com 2979112Smarc.orr@gmail.com/// Target getgid() handler. 2989112Smarc.orr@gmail.comSyscallReturn getgidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2999112Smarc.orr@gmail.com 3009112Smarc.orr@gmail.com/// Target getppid() handler. 3019112Smarc.orr@gmail.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3029112Smarc.orr@gmail.com 3039112Smarc.orr@gmail.com/// Target geteuid() handler. 3049112Smarc.orr@gmail.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3059112Smarc.orr@gmail.com 3069112Smarc.orr@gmail.com/// Target getegid() handler. 3079112Smarc.orr@gmail.comSyscallReturn getegidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3089112Smarc.orr@gmail.com 3099112Smarc.orr@gmail.com/// Target access() handler 3109238Slluc.alvarez@bsc.esSyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3119112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc, 3129112Smarc.orr@gmail.com int index); 3139112Smarc.orr@gmail.com 3149112Smarc.orr@gmail.com// Target getsockopt() handler. 3159112Smarc.orr@gmail.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3169112Smarc.orr@gmail.com 3179112Smarc.orr@gmail.com// Target setsockopt() handler. 3189112Smarc.orr@gmail.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3199112Smarc.orr@gmail.com 3209112Smarc.orr@gmail.com// Target getsockname() handler. 32111367Sandreas.hansson@arm.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3229112Smarc.orr@gmail.com 32311321Ssteve.reinhardt@amd.com/// Futex system call 3249112Smarc.orr@gmail.com/// Implemented by Daniel Sanchez 3259112Smarc.orr@gmail.com/// Used by printf's in multi-threaded apps 3269112Smarc.orr@gmail.comtemplate <class OS> 3279112Smarc.orr@gmail.comSyscallReturn 3289112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 3299112Smarc.orr@gmail.com{ 3309112Smarc.orr@gmail.com using namespace std; 3319112Smarc.orr@gmail.com 3329112Smarc.orr@gmail.com int index = 0; 3339112Smarc.orr@gmail.com auto process = tc->getProcessPtr(); 3349112Smarc.orr@gmail.com 3359112Smarc.orr@gmail.com Addr uaddr = process->getSyscallArg(tc, index); 3369112Smarc.orr@gmail.com int op = process->getSyscallArg(tc, index); 3379112Smarc.orr@gmail.com int val = process->getSyscallArg(tc, index); 3389112Smarc.orr@gmail.com int timeout M5_VAR_USED = process->getSyscallArg(tc, index); 3399112Smarc.orr@gmail.com Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index); 3409112Smarc.orr@gmail.com int val3 = process->getSyscallArg(tc, index); 3419112Smarc.orr@gmail.com 3429112Smarc.orr@gmail.com /* 3439112Smarc.orr@gmail.com * Unsupported option that does not affect the correctness of the 3449112Smarc.orr@gmail.com * application. This is a performance optimization utilized by Linux. 3459112Smarc.orr@gmail.com */ 3469112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3479112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG; 3489112Smarc.orr@gmail.com 3499112Smarc.orr@gmail.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 3509112Smarc.orr@gmail.com 3519112Smarc.orr@gmail.com if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) { 3529112Smarc.orr@gmail.com // Ensure futex system call accessed atomically. 35311321Ssteve.reinhardt@amd.com BufferArg buf(uaddr, sizeof(int)); 3549112Smarc.orr@gmail.com buf.copyIn(tc->getMemProxy()); 3559112Smarc.orr@gmail.com int mem_val = *(int*)buf.bufferPtr(); 3569112Smarc.orr@gmail.com 3579112Smarc.orr@gmail.com /* 3589112Smarc.orr@gmail.com * The value in memory at uaddr is not equal with the expected val 3599112Smarc.orr@gmail.com * (a different thread must have changed it before the system call was 3609112Smarc.orr@gmail.com * invoked). In this case, we need to throw an error. 3619112Smarc.orr@gmail.com */ 3629238Slluc.alvarez@bsc.es if (val != mem_val) 3639112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 3649112Smarc.orr@gmail.com 3659112Smarc.orr@gmail.com if (OS::TGT_FUTEX_WAIT) { 3669112Smarc.orr@gmail.com futex_map.suspend(uaddr, process->tgid(), tc); 3679112Smarc.orr@gmail.com } else { 3682238SN/A futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3); 3692238SN/A } 3702238SN/A 3712238SN/A return 0; 37211851Sbrandon.potter@amd.com } else if (OS::TGT_FUTEX_WAKE == op) { 3732238SN/A return futex_map.wakeup(uaddr, process->tgid(), val); 3742238SN/A } else if (OS::TGT_FUTEX_WAKE_BITSET == op) { 3752238SN/A return futex_map.wakeup_bitset(uaddr, process->tgid(), val3); 37611851Sbrandon.potter@amd.com } else if (OS::TGT_FUTEX_REQUEUE == op || 3772238SN/A OS::TGT_FUTEX_CMP_REQUEUE == op) { 3782238SN/A 3792238SN/A // Ensure futex system call accessed atomically. 38011851Sbrandon.potter@amd.com BufferArg buf(uaddr, sizeof(int)); 3812238SN/A buf.copyIn(tc->getMemProxy()); 3822238SN/A int mem_val = *(int*)buf.bufferPtr(); 3832238SN/A /* 38411851Sbrandon.potter@amd.com * For CMP_REQUEUE, the whole operation is only started only if 3852238SN/A * val3 is still the value of the futex pointed to by uaddr. 3862238SN/A */ 3871354SN/A if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val) 3881354SN/A return -OS::TGT_EWOULDBLOCK; 38910796Sbrandon.potter@amd.com return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2); 39010796Sbrandon.potter@amd.com } else if (OS::TGT_FUTEX_WAKE_OP == op) { 3911354SN/A /* 3921354SN/A * The FUTEX_WAKE_OP operation is equivalent to executing the 3931354SN/A * following code atomically and totally ordered with respect to 3941354SN/A * other futex operations on any of the two supplied futex words: 3951354SN/A * 3961354SN/A * int oldval = *(int *) addr2; 3971354SN/A * *(int *) addr2 = oldval op oparg; 3981354SN/A * futex(addr1, FUTEX_WAKE, val, 0, 0, 0); 3991354SN/A * if (oldval cmp cmparg) 4001354SN/A * futex(addr2, FUTEX_WAKE, val2, 0, 0, 0); 40110796Sbrandon.potter@amd.com * 4021354SN/A * (op, oparg, cmp, cmparg are encoded in val3) 40310796Sbrandon.potter@amd.com * 4041354SN/A * +---+---+-----------+-----------+ 4051354SN/A * |op |cmp| oparg | cmparg | 4061354SN/A * +---+---+-----------+-----------+ 4071354SN/A * 4 4 12 12 <== # of bits 40810796Sbrandon.potter@amd.com * 40910796Sbrandon.potter@amd.com * reference: http://man7.org/linux/man-pages/man2/futex.2.html 41010796Sbrandon.potter@amd.com * 41110796Sbrandon.potter@amd.com */ 41210796Sbrandon.potter@amd.com // get value from simulated-space 41310796Sbrandon.potter@amd.com BufferArg buf(uaddr2, sizeof(int)); 41410796Sbrandon.potter@amd.com buf.copyIn(tc->getMemProxy()); 41510796Sbrandon.potter@amd.com int oldval = *(int*)buf.bufferPtr(); 41610796Sbrandon.potter@amd.com int newval = oldval; 41710796Sbrandon.potter@amd.com // extract op, oparg, cmp, cmparg from val3 41810796Sbrandon.potter@amd.com int wake_cmparg = val3 & 0xfff; 419360SN/A int wake_oparg = (val3 & 0xfff000) >> 12; 420360SN/A int wake_cmp = (val3 & 0xf000000) >> 24; 421360SN/A int wake_op = (val3 & 0xf0000000) >> 28; 422360SN/A if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1) 423360SN/A wake_oparg = (1 << wake_oparg); 424360SN/A wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT; 425360SN/A // perform operation on the value of the second futex 42611759Sbrandon.potter@amd.com if (wake_op == OS::TGT_FUTEX_OP_SET) 4273113Sgblack@eecs.umich.edu newval = wake_oparg; 4283113Sgblack@eecs.umich.edu else if (wake_op == OS::TGT_FUTEX_OP_ADD) 4293113Sgblack@eecs.umich.edu newval += wake_oparg; 4303113Sgblack@eecs.umich.edu else if (wake_op == OS::TGT_FUTEX_OP_OR) 4313113Sgblack@eecs.umich.edu newval |= wake_oparg; 4323113Sgblack@eecs.umich.edu else if (wake_op == OS::TGT_FUTEX_OP_ANDN) 4333113Sgblack@eecs.umich.edu newval &= ~wake_oparg; 4343113Sgblack@eecs.umich.edu else if (wake_op == OS::TGT_FUTEX_OP_XOR) 4353113Sgblack@eecs.umich.edu newval ^= wake_oparg; 4363113Sgblack@eecs.umich.edu // copy updated value back to simulated-space 4373113Sgblack@eecs.umich.edu *(int*)buf.bufferPtr() = newval; 4383113Sgblack@eecs.umich.edu buf.copyOut(tc->getMemProxy()); 4393113Sgblack@eecs.umich.edu // perform the first wake-up 4403113Sgblack@eecs.umich.edu int woken1 = futex_map.wakeup(uaddr, process->tgid(), val); 4413113Sgblack@eecs.umich.edu int woken2 = 0; 4423113Sgblack@eecs.umich.edu // calculate the condition of the second wake-up 4434189Sgblack@eecs.umich.edu bool is_wake2 = false; 4444189Sgblack@eecs.umich.edu if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ) 4453113Sgblack@eecs.umich.edu is_wake2 = oldval == wake_cmparg; 4463113Sgblack@eecs.umich.edu else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE) 4473113Sgblack@eecs.umich.edu is_wake2 = oldval != wake_cmparg; 4483113Sgblack@eecs.umich.edu else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT) 4498737Skoansin.tan@gmail.com is_wake2 = oldval < wake_cmparg; 4503113Sgblack@eecs.umich.edu else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE) 4518737Skoansin.tan@gmail.com is_wake2 = oldval <= wake_cmparg; 4523277Sgblack@eecs.umich.edu else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT) 4535515SMichael.Adler@intel.com is_wake2 = oldval > wake_cmparg; 4545515SMichael.Adler@intel.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE) 4555515SMichael.Adler@intel.com is_wake2 = oldval >= wake_cmparg; 4565515SMichael.Adler@intel.com // perform the second wake-up 4575515SMichael.Adler@intel.com if (is_wake2) 4588737Skoansin.tan@gmail.com woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout); 4593277Sgblack@eecs.umich.edu 4608737Skoansin.tan@gmail.com return woken1 + woken2; 4613277Sgblack@eecs.umich.edu } 4628737Skoansin.tan@gmail.com warn("futex: op %d not implemented; ignoring.", op); 4633277Sgblack@eecs.umich.edu return -ENOSYS; 4648737Skoansin.tan@gmail.com} 4653113Sgblack@eecs.umich.edu 4663113Sgblack@eecs.umich.edu 4673113Sgblack@eecs.umich.edu/// Pseudo Funcs - These functions use a different return convension, 4683113Sgblack@eecs.umich.edu/// returning a second value in a register other than the normal return register 4698737Skoansin.tan@gmail.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4703113Sgblack@eecs.umich.edu 4718737Skoansin.tan@gmail.com/// Target getpidPseudo() handler. 4723114Sgblack@eecs.umich.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4738737Skoansin.tan@gmail.com 4743114Sgblack@eecs.umich.edu/// Target getuidPseudo() handler. 4758737Skoansin.tan@gmail.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4763114Sgblack@eecs.umich.edu 4778737Skoansin.tan@gmail.com/// Target getgidPseudo() handler. 4784061Sgblack@eecs.umich.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4794061Sgblack@eecs.umich.edu 4804061Sgblack@eecs.umich.edu 4818737Skoansin.tan@gmail.com/// A readable name for 1,000,000, for converting microseconds to seconds. 4823113Sgblack@eecs.umich.educonst int one_million = 1000000; 4838737Skoansin.tan@gmail.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 4843113Sgblack@eecs.umich.educonst int one_billion = 1000000000; 4853113Sgblack@eecs.umich.edu 4863113Sgblack@eecs.umich.edu/// Approximate seconds since the epoch (1/1/1970). About a billion, 4873113Sgblack@eecs.umich.edu/// by my reckoning. We want to keep this a constant (not use the 4883113Sgblack@eecs.umich.edu/// real-world time) to keep simulations repeatable. 4893113Sgblack@eecs.umich.educonst unsigned seconds_since_epoch = 1000000000; 4903113Sgblack@eecs.umich.edu 4913113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 4924189Sgblack@eecs.umich.edu/// microseconds. 4934189Sgblack@eecs.umich.edutemplate <class T1, class T2> 4943113Sgblack@eecs.umich.eduvoid 4953113Sgblack@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec) 4963113Sgblack@eecs.umich.edu{ 4978737Skoansin.tan@gmail.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 4983113Sgblack@eecs.umich.edu sec = elapsed_usecs / one_million; 4998737Skoansin.tan@gmail.com usec = elapsed_usecs % one_million; 5003113Sgblack@eecs.umich.edu} 5018737Skoansin.tan@gmail.com 5023113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 5033113Sgblack@eecs.umich.edu/// nanoseconds. 5043113Sgblack@eecs.umich.edutemplate <class T1, class T2> 5053113Sgblack@eecs.umich.eduvoid 5063113Sgblack@eecs.umich.edugetElapsedTimeNano(T1 &sec, T2 &nsec) 5073113Sgblack@eecs.umich.edu{ 5083113Sgblack@eecs.umich.edu uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 5093113Sgblack@eecs.umich.edu sec = elapsed_nsecs / one_billion; 5103113Sgblack@eecs.umich.edu nsec = elapsed_nsecs % one_billion; 5113113Sgblack@eecs.umich.edu} 5128852Sandreas.hansson@arm.com 5133113Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////// 5143113Sgblack@eecs.umich.edu// 5153113Sgblack@eecs.umich.edu// The following emulation functions are generic, but need to be 5163113Sgblack@eecs.umich.edu// templated to account for differences in types, constants, etc. 5173113Sgblack@eecs.umich.edu// 5183113Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////// 5193113Sgblack@eecs.umich.edu 5203113Sgblack@eecs.umich.edu typedef struct statfs hst_statfs; 5213113Sgblack@eecs.umich.edu#if NO_STAT64 5223113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 5238852Sandreas.hansson@arm.com typedef struct stat hst_stat64; 5243113Sgblack@eecs.umich.edu#else 5253113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 5263113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 5273113Sgblack@eecs.umich.edu#endif 5286686Stjones1@inf.ed.ac.uk 5293113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 5303113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 5313113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 53211759Sbrandon.potter@amd.com 53311759Sbrandon.potter@amd.comtemplate <typename target_stat, typename host_stat> 53411759Sbrandon.potter@amd.comvoid 53511759Sbrandon.potter@amd.comconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 53611759Sbrandon.potter@amd.com{ 53711759Sbrandon.potter@amd.com using namespace TheISA; 53811759Sbrandon.potter@amd.com 53911812Sbaz21@cam.ac.uk if (fakeTTY) 54011812Sbaz21@cam.ac.uk tgt->st_dev = 0xA; 54111812Sbaz21@cam.ac.uk else 54211759Sbrandon.potter@amd.com tgt->st_dev = host->st_dev; 54311812Sbaz21@cam.ac.uk tgt->st_dev = TheISA::htog(tgt->st_dev); 54411759Sbrandon.potter@amd.com tgt->st_ino = host->st_ino; 54511759Sbrandon.potter@amd.com tgt->st_ino = TheISA::htog(tgt->st_ino); 54611759Sbrandon.potter@amd.com tgt->st_mode = host->st_mode; 54711759Sbrandon.potter@amd.com if (fakeTTY) { 54811759Sbrandon.potter@amd.com // Claim to be a character device 54911759Sbrandon.potter@amd.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 55011759Sbrandon.potter@amd.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 55111812Sbaz21@cam.ac.uk } 55211812Sbaz21@cam.ac.uk tgt->st_mode = TheISA::htog(tgt->st_mode); 55311812Sbaz21@cam.ac.uk tgt->st_nlink = host->st_nlink; 55411812Sbaz21@cam.ac.uk tgt->st_nlink = TheISA::htog(tgt->st_nlink); 55511812Sbaz21@cam.ac.uk tgt->st_uid = host->st_uid; 55611812Sbaz21@cam.ac.uk tgt->st_uid = TheISA::htog(tgt->st_uid); 55711812Sbaz21@cam.ac.uk tgt->st_gid = host->st_gid; 55811759Sbrandon.potter@amd.com tgt->st_gid = TheISA::htog(tgt->st_gid); 55911759Sbrandon.potter@amd.com if (fakeTTY) 56011812Sbaz21@cam.ac.uk tgt->st_rdev = 0x880d; 56111812Sbaz21@cam.ac.uk else 56211759Sbrandon.potter@amd.com tgt->st_rdev = host->st_rdev; 56311812Sbaz21@cam.ac.uk tgt->st_rdev = TheISA::htog(tgt->st_rdev); 56411812Sbaz21@cam.ac.uk tgt->st_size = host->st_size; 56511812Sbaz21@cam.ac.uk tgt->st_size = TheISA::htog(tgt->st_size); 56611812Sbaz21@cam.ac.uk tgt->st_atimeX = host->st_atime; 56711812Sbaz21@cam.ac.uk tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 56811812Sbaz21@cam.ac.uk tgt->st_mtimeX = host->st_mtime; 56911812Sbaz21@cam.ac.uk tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 57011759Sbrandon.potter@amd.com tgt->st_ctimeX = host->st_ctime; 57111759Sbrandon.potter@amd.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 57211759Sbrandon.potter@amd.com // Force the block size to be 8KB. This helps to ensure buffered io works 57311759Sbrandon.potter@amd.com // consistently across different hosts. 574378SN/A tgt->st_blksize = 0x2000; 575378SN/A tgt->st_blksize = TheISA::htog(tgt->st_blksize); 5769141Smarc.orr@gmail.com tgt->st_blocks = host->st_blocks; 5779141Smarc.orr@gmail.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 578360SN/A} 5791450SN/A 58011856Sbrandon.potter@amd.com// Same for stat64 581360SN/A 5826701Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 58311856Sbrandon.potter@amd.comvoid 58411856Sbrandon.potter@amd.comconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 585360SN/A{ 58610930Sbrandon.potter@amd.com using namespace TheISA; 587360SN/A 58811856Sbrandon.potter@amd.com convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 58911856Sbrandon.potter@amd.com#if defined(STAT_HAVE_NSEC) 59010496Ssteve.reinhardt@amd.com tgt->st_atime_nsec = host->st_atime_nsec; 59111856Sbrandon.potter@amd.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 59211856Sbrandon.potter@amd.com tgt->st_mtime_nsec = host->st_mtime_nsec; 5931458SN/A tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 594360SN/A tgt->st_ctime_nsec = host->st_ctime_nsec; 59511856Sbrandon.potter@amd.com tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 59611856Sbrandon.potter@amd.com#else 59711856Sbrandon.potter@amd.com tgt->st_atime_nsec = 0; 59811856Sbrandon.potter@amd.com tgt->st_mtime_nsec = 0; 59911856Sbrandon.potter@amd.com tgt->st_ctime_nsec = 0; 60011856Sbrandon.potter@amd.com#endif 60111856Sbrandon.potter@amd.com} 60211856Sbrandon.potter@amd.com 60310496Ssteve.reinhardt@amd.com// Here are a couple of convenience functions 60411856Sbrandon.potter@amd.comtemplate<class OS> 60511856Sbrandon.potter@amd.comvoid 60611856Sbrandon.potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 60711856Sbrandon.potter@amd.com hst_stat *host, bool fakeTTY = false) 60811856Sbrandon.potter@amd.com{ 60910930Sbrandon.potter@amd.com typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 6109141Smarc.orr@gmail.com tgt_stat_buf tgt(addr); 611360SN/A convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 612360SN/A tgt.copyOut(mem); 613360SN/A} 61410027SChris.Adeniyi-Jones@arm.com 61511851Sbrandon.potter@amd.comtemplate<class OS> 61610027SChris.Adeniyi-Jones@arm.comvoid 617360SN/AcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 618360SN/A hst_stat64 *host, bool fakeTTY = false) 619360SN/A{ 6208852Sandreas.hansson@arm.com typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 6216701Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 6221458SN/A convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 623360SN/A tgt.copyOut(mem); 6246701Sgblack@eecs.umich.edu} 6256701Sgblack@eecs.umich.edu 626360SN/Atemplate <class OS> 627360SN/Avoid 628360SN/AcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 629360SN/A hst_statfs *host) 630360SN/A{ 631360SN/A TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 632360SN/A 633360SN/A tgt->f_type = TheISA::htog(host->f_type); 634360SN/A#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 635360SN/A tgt->f_bsize = TheISA::htog(host->f_iosize); 636360SN/A#else 637360SN/A tgt->f_bsize = TheISA::htog(host->f_bsize); 6381706SN/A#endif 639360SN/A tgt->f_blocks = TheISA::htog(host->f_blocks); 640360SN/A tgt->f_bfree = TheISA::htog(host->f_bfree); 641360SN/A tgt->f_bavail = TheISA::htog(host->f_bavail); 642360SN/A tgt->f_files = TheISA::htog(host->f_files); 643360SN/A tgt->f_ffree = TheISA::htog(host->f_ffree); 6443669Sbinkertn@umich.edu memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 6453669Sbinkertn@umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 6463669Sbinkertn@umich.edu tgt->f_namelen = TheISA::htog(host->f_namemax); 6471706SN/A tgt->f_frsize = TheISA::htog(host->f_bsize); 6481706SN/A#elif defined(__APPLE__) 64910496Ssteve.reinhardt@amd.com tgt->f_namelen = 0; 65010496Ssteve.reinhardt@amd.com tgt->f_frsize = 0; 65110496Ssteve.reinhardt@amd.com#else 65210496Ssteve.reinhardt@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 65310496Ssteve.reinhardt@amd.com tgt->f_frsize = TheISA::htog(host->f_frsize); 65410496Ssteve.reinhardt@amd.com#endif 65510496Ssteve.reinhardt@amd.com#if defined(__linux__) 65610496Ssteve.reinhardt@amd.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 65710496Ssteve.reinhardt@amd.com#else 65810496Ssteve.reinhardt@amd.com /* 65911856Sbrandon.potter@amd.com * The fields are different sizes per OS. Don't bother with 66010496Ssteve.reinhardt@amd.com * f_spare or f_reserved on non-Linux for now. 66110496Ssteve.reinhardt@amd.com */ 66210496Ssteve.reinhardt@amd.com memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 66310496Ssteve.reinhardt@amd.com#endif 66410496Ssteve.reinhardt@amd.com 66510496Ssteve.reinhardt@amd.com tgt.copyOut(mem); 66610496Ssteve.reinhardt@amd.com} 66710496Ssteve.reinhardt@amd.com 66810496Ssteve.reinhardt@amd.com/// Target ioctl() handler. For the most part, programs call ioctl() 6695795Ssaidi@eecs.umich.edu/// only to find out if their stdout is a tty, to determine whether to 6709143Ssteve.reinhardt@amd.com/// do line or block buffering. We always claim that output fds are 6719142Ssteve.reinhardt@amd.com/// not TTYs to provide repeatable results. 6729142Ssteve.reinhardt@amd.comtemplate <class OS> 6739143Ssteve.reinhardt@amd.comSyscallReturn 6745795Ssaidi@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 6759143Ssteve.reinhardt@amd.com{ 6765795Ssaidi@eecs.umich.edu int index = 0; 6775795Ssaidi@eecs.umich.edu auto p = tc->getProcessPtr(); 6785795Ssaidi@eecs.umich.edu 6799143Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 6805795Ssaidi@eecs.umich.edu unsigned req = p->getSyscallArg(tc, index); 681360SN/A 6829143Ssteve.reinhardt@amd.com DPRINTF_SYSCALL(Verbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 6839143Ssteve.reinhardt@amd.com 6849143Ssteve.reinhardt@amd.com if (OS::isTtyReq(req)) 68511856Sbrandon.potter@amd.com return -ENOTTY; 68611856Sbrandon.potter@amd.com 68711856Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 688360SN/A if (dfdp) { 689360SN/A EmulatedDriver *emul_driver = dfdp->getDriver(); 69010027SChris.Adeniyi-Jones@arm.com if (emul_driver) 69110027SChris.Adeniyi-Jones@arm.com return emul_driver->ioctl(tc, req); 69210027SChris.Adeniyi-Jones@arm.com } 69311851Sbrandon.potter@amd.com 69410027SChris.Adeniyi-Jones@arm.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 69510027SChris.Adeniyi-Jones@arm.com if (sfdp) { 69610027SChris.Adeniyi-Jones@arm.com int status; 69710027SChris.Adeniyi-Jones@arm.com 69810027SChris.Adeniyi-Jones@arm.com switch (req) { 69910027SChris.Adeniyi-Jones@arm.com case SIOCGIFCONF: { 70010027SChris.Adeniyi-Jones@arm.com Addr conf_addr = p->getSyscallArg(tc, index); 70110027SChris.Adeniyi-Jones@arm.com BufferArg conf_arg(conf_addr, sizeof(ifconf)); 70211851Sbrandon.potter@amd.com conf_arg.copyIn(tc->getMemProxy()); 70311851Sbrandon.potter@amd.com 70410027SChris.Adeniyi-Jones@arm.com ifconf *conf = (ifconf*)conf_arg.bufferPtr(); 70510027SChris.Adeniyi-Jones@arm.com Addr ifc_buf_addr = (Addr)conf->ifc_buf; 70610027SChris.Adeniyi-Jones@arm.com BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len); 70710027SChris.Adeniyi-Jones@arm.com ifc_buf_arg.copyIn(tc->getMemProxy()); 70810027SChris.Adeniyi-Jones@arm.com 70910027SChris.Adeniyi-Jones@arm.com conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr(); 71010027SChris.Adeniyi-Jones@arm.com 71110027SChris.Adeniyi-Jones@arm.com status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr()); 71210633Smichaelupton@gmail.com if (status != -1) { 71310633Smichaelupton@gmail.com conf->ifc_buf = (char*)ifc_buf_addr; 71410633Smichaelupton@gmail.com ifc_buf_arg.copyOut(tc->getMemProxy()); 71511851Sbrandon.potter@amd.com conf_arg.copyOut(tc->getMemProxy()); 71610633Smichaelupton@gmail.com } 71710633Smichaelupton@gmail.com 71810633Smichaelupton@gmail.com return status; 71910633Smichaelupton@gmail.com } 72010633Smichaelupton@gmail.com case SIOCGIFFLAGS: 72110633Smichaelupton@gmail.com#if defined(__linux__) 72210633Smichaelupton@gmail.com case SIOCGIFINDEX: 72310633Smichaelupton@gmail.com#endif 72410633Smichaelupton@gmail.com case SIOCGIFNETMASK: 72510633Smichaelupton@gmail.com case SIOCGIFADDR: 72610203SAli.Saidi@ARM.com#if defined(__linux__) 72710203SAli.Saidi@ARM.com case SIOCGIFHWADDR: 72810203SAli.Saidi@ARM.com#endif 72911851Sbrandon.potter@amd.com case SIOCGIFMTU: { 73011851Sbrandon.potter@amd.com Addr req_addr = p->getSyscallArg(tc, index); 73110203SAli.Saidi@ARM.com BufferArg req_arg(req_addr, sizeof(ifreq)); 73210203SAli.Saidi@ARM.com req_arg.copyIn(tc->getMemProxy()); 73310203SAli.Saidi@ARM.com 73410203SAli.Saidi@ARM.com status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr()); 73510203SAli.Saidi@ARM.com if (status != -1) 73610203SAli.Saidi@ARM.com req_arg.copyOut(tc->getMemProxy()); 73710203SAli.Saidi@ARM.com return status; 73810203SAli.Saidi@ARM.com } 73910203SAli.Saidi@ARM.com } 74010203SAli.Saidi@ARM.com } 74110203SAli.Saidi@ARM.com 74211851Sbrandon.potter@amd.com /** 74311851Sbrandon.potter@amd.com * For lack of a better return code, return ENOTTY. Ideally, we should 74410203SAli.Saidi@ARM.com * return something better here, but at least we issue the warning. 74510203SAli.Saidi@ARM.com */ 74610203SAli.Saidi@ARM.com warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 74710203SAli.Saidi@ARM.com tgt_fd, req, tc->pcState()); 74810203SAli.Saidi@ARM.com return -ENOTTY; 74910203SAli.Saidi@ARM.com} 75010203SAli.Saidi@ARM.com 75110203SAli.Saidi@ARM.comtemplate <class OS> 75210850SGiacomo.Gabrielli@arm.comSyscallReturn 75310850SGiacomo.Gabrielli@arm.comopenImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool isopenat) 75410850SGiacomo.Gabrielli@arm.com{ 75511851Sbrandon.potter@amd.com int index = 0; 75610850SGiacomo.Gabrielli@arm.com auto p = tc->getProcessPtr(); 75710850SGiacomo.Gabrielli@arm.com int tgt_dirfd = -1; 75810850SGiacomo.Gabrielli@arm.com 75910850SGiacomo.Gabrielli@arm.com /** 76010850SGiacomo.Gabrielli@arm.com * If using the openat variant, read in the target directory file 76110850SGiacomo.Gabrielli@arm.com * descriptor from the simulated process. 76210850SGiacomo.Gabrielli@arm.com */ 76310850SGiacomo.Gabrielli@arm.com if (isopenat) 76410850SGiacomo.Gabrielli@arm.com tgt_dirfd = p->getSyscallArg(tc, index); 76510850SGiacomo.Gabrielli@arm.com 76610850SGiacomo.Gabrielli@arm.com /** 76710850SGiacomo.Gabrielli@arm.com * Retrieve the simulated process' memory proxy and then read in the path 76810850SGiacomo.Gabrielli@arm.com * string from that memory space into the host's working memory space. 76910850SGiacomo.Gabrielli@arm.com */ 77010850SGiacomo.Gabrielli@arm.com std::string path; 77110850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 77210850SGiacomo.Gabrielli@arm.com return -EFAULT; 77310850SGiacomo.Gabrielli@arm.com 77410850SGiacomo.Gabrielli@arm.com#ifdef __CYGWIN32__ 77510850SGiacomo.Gabrielli@arm.com int host_flags = O_BINARY; 77610850SGiacomo.Gabrielli@arm.com#else 77710850SGiacomo.Gabrielli@arm.com int host_flags = 0; 77810850SGiacomo.Gabrielli@arm.com#endif 77910850SGiacomo.Gabrielli@arm.com /** 78010850SGiacomo.Gabrielli@arm.com * Translate target flags into host flags. Flags exist which are not 78110850SGiacomo.Gabrielli@arm.com * ported between architectures which can cause check failures. 78210850SGiacomo.Gabrielli@arm.com */ 78310850SGiacomo.Gabrielli@arm.com int tgt_flags = p->getSyscallArg(tc, index); 78410850SGiacomo.Gabrielli@arm.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 78510850SGiacomo.Gabrielli@arm.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 78610850SGiacomo.Gabrielli@arm.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 78710850SGiacomo.Gabrielli@arm.com host_flags |= OS::openFlagTable[i].hostFlag; 7886640Svince@csl.cornell.edu } 7896640Svince@csl.cornell.edu } 7906640Svince@csl.cornell.edu if (tgt_flags) { 79111851Sbrandon.potter@amd.com warn("open%s: cannot decode flags 0x%x", 79211851Sbrandon.potter@amd.com isopenat ? "at" : "", tgt_flags); 7936640Svince@csl.cornell.edu } 7946640Svince@csl.cornell.edu#ifdef __CYGWIN32__ 7956701Sgblack@eecs.umich.edu host_flags |= O_BINARY; 7966701Sgblack@eecs.umich.edu#endif 79710793Sbrandon.potter@amd.com 7986640Svince@csl.cornell.edu int mode = p->getSyscallArg(tc, index); 79911758Sbrandon.potter@amd.com 80011758Sbrandon.potter@amd.com /** 80111758Sbrandon.potter@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 8026640Svince@csl.cornell.edu * take the current working directory value which was passed into the 8038706Sandreas.hansson@arm.com * process class as a Python parameter and append the current path to 8046640Svince@csl.cornell.edu * create a full path. 8056701Sgblack@eecs.umich.edu * Otherwise, openat with a valid target directory file descriptor has 8066640Svince@csl.cornell.edu * been called. If the path option, which was passed in as a parameter, 807360SN/A * is not absolute, retrieve the directory file descriptor's path and 8081999SN/A * prepend it to the path passed in as a parameter. 8091999SN/A * In every case, we should have a full path (which is relevant to the 8101999SN/A * host) to work with after this block has been passed. 81111851Sbrandon.potter@amd.com */ 8122680Sktlim@umich.edu std::string redir_path = path; 8131999SN/A std::string abs_path = path; 8141999SN/A if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) { 8151999SN/A abs_path = p->absolutePath(path, true); 8166701Sgblack@eecs.umich.edu redir_path = p->checkPathRedirect(path); 8178852Sandreas.hansson@arm.com } else if (!startswith(path, "/")) { 8186701Sgblack@eecs.umich.edu std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 8191999SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 8206701Sgblack@eecs.umich.edu if (!ffdp) 8211999SN/A return -EBADF; 8226701Sgblack@eecs.umich.edu abs_path = ffdp->getFileName() + path; 8231999SN/A redir_path = p->checkPathRedirect(abs_path); 8241999SN/A } 8251999SN/A 8261999SN/A /** 8271999SN/A * Since this is an emulated environment, we create pseudo file 8283669Sbinkertn@umich.edu * descriptors for device requests that have been registered with 8293669Sbinkertn@umich.edu * the process class through Python; this allows us to create a file 8303669Sbinkertn@umich.edu * descriptor for subsequent ioctl or mmap calls. 8311999SN/A */ 8321999SN/A if (startswith(abs_path, "/dev/")) { 8331999SN/A std::string filename = abs_path.substr(strlen("/dev/")); 8342218SN/A EmulatedDriver *drv = p->findDriver(filename); 8351999SN/A if (drv) { 8361999SN/A DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 8371999SN/A "driver open with path[%s]\n", 8381999SN/A isopenat ? "at" : "", abs_path.c_str()); 8391999SN/A return drv->open(tc, mode, host_flags); 8401999SN/A } 8411999SN/A /** 8421999SN/A * Fall through here for pass through to host devices, such 84311856Sbrandon.potter@amd.com * as /dev/zero 8441999SN/A */ 8456701Sgblack@eecs.umich.edu } 84611856Sbrandon.potter@amd.com 84711856Sbrandon.potter@amd.com /** 84810931Sbrandon.potter@amd.com * We make several attempts resolve a call to open. 84911856Sbrandon.potter@amd.com * 85011856Sbrandon.potter@amd.com * 1) Resolve any path redirection before hand. This will set the path 8511999SN/A * up with variable 'redir_path' which may contain a modified path or 85211856Sbrandon.potter@amd.com * the original path value. This should already be done in prior code. 8531999SN/A * 2) Try to handle the access using 'special_paths'. Some special_paths 85411856Sbrandon.potter@amd.com * and files cannot be called on the host and need to be handled as 8551999SN/A * special cases inside the simulator. These special_paths are handled by 85611856Sbrandon.potter@amd.com * C++ routines to provide output back to userspace. 8571999SN/A * 3) If the full path that was created above does not match any of the 85811856Sbrandon.potter@amd.com * special cases, pass it through to the open call on the __HOST__ to let 8591999SN/A * the host open the file on our behalf. Again, the openImpl tries to 8601999SN/A * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the 8615877Shsul@eecs.umich.edu * faux-filesystem files). The faux-filesystem is dynamically created 8625877Shsul@eecs.umich.edu * during simulator configuration using Python functions. 8635877Shsul@eecs.umich.edu * 4) If the host cannot open the file, the open attempt failed in "3)". 86411851Sbrandon.potter@amd.com * Return the host's error code back through the system call to the 8655877Shsul@eecs.umich.edu * simulated process. If running a debug trace, also notify the user that 8666701Sgblack@eecs.umich.edu * the open call failed. 8676701Sgblack@eecs.umich.edu * 8686701Sgblack@eecs.umich.edu * Any success will set sim_fd to something other than -1 and skip the 8696701Sgblack@eecs.umich.edu * next conditions effectively bypassing them. 8706701Sgblack@eecs.umich.edu */ 87110027SChris.Adeniyi-Jones@arm.com int sim_fd = -1; 87210027SChris.Adeniyi-Jones@arm.com std::string used_path; 87310027SChris.Adeniyi-Jones@arm.com std::vector<std::string> special_paths = 87410027SChris.Adeniyi-Jones@arm.com { "/proc/meminfo/", "/system/", "/platform/", "/etc/passwd" }; 87510027SChris.Adeniyi-Jones@arm.com for (auto entry : special_paths) { 8765877Shsul@eecs.umich.edu if (startswith(path, entry)) { 87710318Sandreas.hansson@arm.com sim_fd = OS::openSpecialFile(abs_path, p, tc); 87810318Sandreas.hansson@arm.com used_path = abs_path; 8795877Shsul@eecs.umich.edu } 8805877Shsul@eecs.umich.edu } 8815877Shsul@eecs.umich.edu if (sim_fd == -1) { 8825877Shsul@eecs.umich.edu sim_fd = open(redir_path.c_str(), host_flags, mode); 88310486Stjablin@gmail.com used_path = redir_path; 88410486Stjablin@gmail.com } 8855877Shsul@eecs.umich.edu if (sim_fd == -1) { 88610027SChris.Adeniyi-Jones@arm.com int local = -errno; 88710027SChris.Adeniyi-Jones@arm.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s " 8885877Shsul@eecs.umich.edu "(inferred from:%s)\n", isopenat ? "at" : "", 8898601Ssteve.reinhardt@amd.com used_path.c_str(), path.c_str()); 8905877Shsul@eecs.umich.edu return local; 8915877Shsul@eecs.umich.edu } 8925877Shsul@eecs.umich.edu 89310027SChris.Adeniyi-Jones@arm.com /** 8945877Shsul@eecs.umich.edu * The file was opened successfully and needs to be recorded in the 8955877Shsul@eecs.umich.edu * process' file descriptor array so that it can be retrieved later. 8965877Shsul@eecs.umich.edu * The target file descriptor that is chosen will be the lowest unused 89710027SChris.Adeniyi-Jones@arm.com * file descriptor. 89810027SChris.Adeniyi-Jones@arm.com * Return the indirect target file descriptor back to the simulated 89910027SChris.Adeniyi-Jones@arm.com * process to act as a handle for the opened file. 90010027SChris.Adeniyi-Jones@arm.com */ 90110027SChris.Adeniyi-Jones@arm.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 90210027SChris.Adeniyi-Jones@arm.com int tgt_fd = p->fds->allocFD(ffdp); 9035877Shsul@eecs.umich.edu DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n" 90410027SChris.Adeniyi-Jones@arm.com "(inferred from:%s)\n", isopenat ? "at" : "", 90510027SChris.Adeniyi-Jones@arm.com sim_fd, tgt_fd, used_path.c_str(), path.c_str()); 90610027SChris.Adeniyi-Jones@arm.com return tgt_fd; 90710027SChris.Adeniyi-Jones@arm.com} 90810027SChris.Adeniyi-Jones@arm.com 90910027SChris.Adeniyi-Jones@arm.com/// Target open() handler. 91010027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 91110027SChris.Adeniyi-Jones@arm.comSyscallReturn 91210027SChris.Adeniyi-Jones@arm.comopenFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 91310027SChris.Adeniyi-Jones@arm.com{ 91410027SChris.Adeniyi-Jones@arm.com return openImpl<OS>(desc, callnum, tc, false); 91510027SChris.Adeniyi-Jones@arm.com} 91610027SChris.Adeniyi-Jones@arm.com 9175877Shsul@eecs.umich.edu/// Target openat() handler. 9185877Shsul@eecs.umich.edutemplate <class OS> 9195877Shsul@eecs.umich.eduSyscallReturn 92010027SChris.Adeniyi-Jones@arm.comopenatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 92110027SChris.Adeniyi-Jones@arm.com{ 9228601Ssteve.reinhardt@amd.com return openImpl<OS>(desc, callnum, tc, true); 92310027SChris.Adeniyi-Jones@arm.com} 9245877Shsul@eecs.umich.edu 9255877Shsul@eecs.umich.edu/// Target unlinkat() handler. 9261999SN/Atemplate <class OS> 927378SN/ASyscallReturn 928360SN/AunlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9291450SN/A{ 93011851Sbrandon.potter@amd.com int index = 0; 9312680Sktlim@umich.edu auto process = tc->getProcessPtr(); 932360SN/A int dirfd = process->getSyscallArg(tc, index); 933360SN/A if (dirfd != OS::TGT_AT_FDCWD) 934360SN/A warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 9356701Sgblack@eecs.umich.edu 9368852Sandreas.hansson@arm.com return unlinkHelper(desc, callnum, tc, 1); 9376701Sgblack@eecs.umich.edu} 9386701Sgblack@eecs.umich.edu 9396701Sgblack@eecs.umich.edu/// Target facessat() handler 9406701Sgblack@eecs.umich.edutemplate <class OS> 941360SN/ASyscallReturn 9423669Sbinkertn@umich.edufaccessatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9433669Sbinkertn@umich.edu{ 9443669Sbinkertn@umich.edu int index = 0; 945360SN/A auto process = tc->getProcessPtr(); 946360SN/A int dirfd = process->getSyscallArg(tc, index); 947360SN/A if (dirfd != OS::TGT_AT_FDCWD) 948360SN/A warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 9492218SN/A return accessFunc(desc, callnum, tc, 1); 950360SN/A} 9518706Sandreas.hansson@arm.com 952360SN/A/// Target readlinkat() handler 9531458SN/Atemplate <class OS> 954360SN/ASyscallReturn 955360SN/AreadlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 956360SN/A{ 9575074Ssaidi@eecs.umich.edu int index = 0; 9585074Ssaidi@eecs.umich.edu auto process = tc->getProcessPtr(); 9595074Ssaidi@eecs.umich.edu int dirfd = process->getSyscallArg(tc, index); 96011851Sbrandon.potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 9615074Ssaidi@eecs.umich.edu warn("openat: first argument not AT_FDCWD; unlikely to work"); 9625074Ssaidi@eecs.umich.edu return readlinkFunc(desc, callnum, tc, 1); 9635074Ssaidi@eecs.umich.edu} 9645074Ssaidi@eecs.umich.edu 9656701Sgblack@eecs.umich.edu/// Target renameat() handler. 9668852Sandreas.hansson@arm.comtemplate <class OS> 9676701Sgblack@eecs.umich.eduSyscallReturn 9685074Ssaidi@eecs.umich.edurenameatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9696701Sgblack@eecs.umich.edu{ 9705074Ssaidi@eecs.umich.edu int index = 0; 9715074Ssaidi@eecs.umich.edu auto process = tc->getProcessPtr(); 9725074Ssaidi@eecs.umich.edu 9735074Ssaidi@eecs.umich.edu int olddirfd = process->getSyscallArg(tc, index); 9745208Ssaidi@eecs.umich.edu if (olddirfd != OS::TGT_AT_FDCWD) 9755208Ssaidi@eecs.umich.edu warn("renameat: first argument not AT_FDCWD; unlikely to work"); 9765208Ssaidi@eecs.umich.edu 9775208Ssaidi@eecs.umich.edu std::string old_name; 9785074Ssaidi@eecs.umich.edu 9795074Ssaidi@eecs.umich.edu if (!tc->getMemProxy().tryReadString(old_name, 9805208Ssaidi@eecs.umich.edu process->getSyscallArg(tc, index))) 9815074Ssaidi@eecs.umich.edu return -EFAULT; 9825074Ssaidi@eecs.umich.edu 9835074Ssaidi@eecs.umich.edu int newdirfd = process->getSyscallArg(tc, index); 9845074Ssaidi@eecs.umich.edu if (newdirfd != OS::TGT_AT_FDCWD) 9858706Sandreas.hansson@arm.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 9865074Ssaidi@eecs.umich.edu 9875074Ssaidi@eecs.umich.edu std::string new_name; 9885074Ssaidi@eecs.umich.edu 9895074Ssaidi@eecs.umich.edu if (!tc->getMemProxy().tryReadString(new_name, 9905074Ssaidi@eecs.umich.edu process->getSyscallArg(tc, index))) 99110027SChris.Adeniyi-Jones@arm.com return -EFAULT; 99210027SChris.Adeniyi-Jones@arm.com 99310027SChris.Adeniyi-Jones@arm.com // Adjust path for cwd and redirection 99411851Sbrandon.potter@amd.com old_name = process->checkPathRedirect(old_name); 99510027SChris.Adeniyi-Jones@arm.com new_name = process->checkPathRedirect(new_name); 99610027SChris.Adeniyi-Jones@arm.com 99710027SChris.Adeniyi-Jones@arm.com int result = rename(old_name.c_str(), new_name.c_str()); 99810027SChris.Adeniyi-Jones@arm.com return (result == -1) ? -errno : result; 99910027SChris.Adeniyi-Jones@arm.com} 100010793Sbrandon.potter@amd.com 100110027SChris.Adeniyi-Jones@arm.com/// Target sysinfo() handler. 100210027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 100310027SChris.Adeniyi-Jones@arm.comSyscallReturn 100410027SChris.Adeniyi-Jones@arm.comsysinfoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 100510027SChris.Adeniyi-Jones@arm.com{ 100610027SChris.Adeniyi-Jones@arm.com int index = 0; 100710027SChris.Adeniyi-Jones@arm.com auto process = tc->getProcessPtr(); 100810027SChris.Adeniyi-Jones@arm.com 100910027SChris.Adeniyi-Jones@arm.com TypedBufferArg<typename OS::tgt_sysinfo> 101010027SChris.Adeniyi-Jones@arm.com sysinfo(process->getSyscallArg(tc, index)); 101110027SChris.Adeniyi-Jones@arm.com 101210027SChris.Adeniyi-Jones@arm.com sysinfo->uptime = seconds_since_epoch; 101310027SChris.Adeniyi-Jones@arm.com sysinfo->totalram = process->system->memSize(); 101410027SChris.Adeniyi-Jones@arm.com sysinfo->mem_unit = 1; 101510027SChris.Adeniyi-Jones@arm.com 101610027SChris.Adeniyi-Jones@arm.com sysinfo.copyOut(tc->getMemProxy()); 101710027SChris.Adeniyi-Jones@arm.com 101810027SChris.Adeniyi-Jones@arm.com return 0; 101910027SChris.Adeniyi-Jones@arm.com} 102010027SChris.Adeniyi-Jones@arm.com 102110027SChris.Adeniyi-Jones@arm.com/// Target chmod() handler. 102210027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 102310027SChris.Adeniyi-Jones@arm.comSyscallReturn 102410027SChris.Adeniyi-Jones@arm.comchmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 102510027SChris.Adeniyi-Jones@arm.com{ 102610027SChris.Adeniyi-Jones@arm.com std::string path; 102710027SChris.Adeniyi-Jones@arm.com auto process = tc->getProcessPtr(); 10281999SN/A 10291999SN/A int index = 0; 10301999SN/A if (!tc->getMemProxy().tryReadString(path, 103111856Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 10321999SN/A return -EFAULT; 10336701Sgblack@eecs.umich.edu } 103411856Sbrandon.potter@amd.com 103511856Sbrandon.potter@amd.com uint32_t mode = process->getSyscallArg(tc, index); 103610931Sbrandon.potter@amd.com mode_t hostMode = 0; 103711856Sbrandon.potter@amd.com 103811856Sbrandon.potter@amd.com // XXX translate mode flags via OS::something??? 10391999SN/A hostMode = mode; 104011856Sbrandon.potter@amd.com 10411999SN/A // Adjust path for cwd and redirection 10422764Sstever@eecs.umich.edu path = process->checkPathRedirect(path); 10432064SN/A 104410931Sbrandon.potter@amd.com // do the chmod 10452064SN/A int result = chmod(path.c_str(), hostMode); 10462064SN/A if (result < 0) 104710931Sbrandon.potter@amd.com return -errno; 10482064SN/A 10491999SN/A return 0; 10501999SN/A} 10512218SN/A 10521999SN/Atemplate <class OS> 105310931Sbrandon.potter@amd.comSyscallReturn 10541999SN/ApollFunc(SyscallDesc *desc, int num, ThreadContext *tc) 10551999SN/A{ 10561999SN/A int index = 0; 10571999SN/A auto p = tc->getProcessPtr(); 10581999SN/A Addr fdsPtr = p->getSyscallArg(tc, index); 1059378SN/A int nfds = p->getSyscallArg(tc, index); 1060360SN/A int tmout = p->getSyscallArg(tc, index); 10611450SN/A 106211851Sbrandon.potter@amd.com BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 10632680Sktlim@umich.edu fdsBuf.copyIn(tc->getMemProxy()); 1064360SN/A 1065360SN/A /** 1066360SN/A * Record the target file descriptors in a local variable. We need to 10676701Sgblack@eecs.umich.edu * replace them with host file descriptors but we need a temporary copy 10688852Sandreas.hansson@arm.com * for later. Afterwards, replace each target file descriptor in the 10696701Sgblack@eecs.umich.edu * poll_fd array with its host_fd. 10706701Sgblack@eecs.umich.edu */ 10716701Sgblack@eecs.umich.edu int temp_tgt_fds[nfds]; 10726701Sgblack@eecs.umich.edu for (index = 0; index < nfds; index++) { 1073360SN/A temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 10743669Sbinkertn@umich.edu auto tgt_fd = temp_tgt_fds[index]; 10753669Sbinkertn@umich.edu auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 10763669Sbinkertn@umich.edu if (!hbfdp) 1077360SN/A return -EBADF; 1078360SN/A auto host_fd = hbfdp->getSimFD(); 1079360SN/A ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 1080360SN/A } 10811458SN/A 1082360SN/A /** 10838706Sandreas.hansson@arm.com * We cannot allow an infinite poll to occur or it will inevitably cause 1084360SN/A * a deadlock in the gem5 simulator with clone. We must pass in tmout with 10851458SN/A * a non-negative value, however it also makes no sense to poll on the 1086360SN/A * underlying host for any other time than tmout a zero timeout. 1087360SN/A */ 10881999SN/A int status; 10891999SN/A if (tmout < 0) { 10901999SN/A status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 109111851Sbrandon.potter@amd.com if (status == 0) { 10922680Sktlim@umich.edu /** 10931999SN/A * If blocking indefinitely, check the signal list to see if a 10941999SN/A * signal would break the poll out of the retry cycle and try 10951999SN/A * to return the signal interrupt instead. 10966701Sgblack@eecs.umich.edu */ 10978852Sandreas.hansson@arm.com System *sysh = tc->getSystemPtr(); 10986701Sgblack@eecs.umich.edu std::list<BasicSignal>::iterator it; 10996701Sgblack@eecs.umich.edu for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 11006701Sgblack@eecs.umich.edu if (it->receiver == p) 11016701Sgblack@eecs.umich.edu return -EINTR; 11021999SN/A return SyscallReturn::retry(); 11033669Sbinkertn@umich.edu } 11043669Sbinkertn@umich.edu } else 11053669Sbinkertn@umich.edu status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 11062764Sstever@eecs.umich.edu 11072064SN/A if (status == -1) 11082064SN/A return -errno; 11092064SN/A 11101999SN/A /** 11111999SN/A * Replace each host_fd in the returned poll_fd array with its original 11122064SN/A * target file descriptor. 11131999SN/A */ 11141999SN/A for (index = 0; index < nfds; index++) { 11151999SN/A auto tgt_fd = temp_tgt_fds[index]; 11161999SN/A ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 11178706Sandreas.hansson@arm.com } 11181999SN/A 11191999SN/A /** 11201999SN/A * Copy out the pollfd struct because the host may have updated fields 11211999SN/A * in the structure. 1122378SN/A */ 1123360SN/A fdsBuf.copyOut(tc->getMemProxy()); 11241450SN/A 112511856Sbrandon.potter@amd.com return status; 1126360SN/A} 11276701Sgblack@eecs.umich.edu 112811856Sbrandon.potter@amd.com/// Target fchmod() handler. 112911856Sbrandon.potter@amd.comtemplate <class OS> 1130360SN/ASyscallReturn 113111380Salexandru.dutu@amd.comfchmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1132360SN/A{ 113311856Sbrandon.potter@amd.com int index = 0; 113411856Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 11351458SN/A int tgt_fd = p->getSyscallArg(tc, index); 113611856Sbrandon.potter@amd.com uint32_t mode = p->getSyscallArg(tc, index); 1137360SN/A 1138360SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 113910931Sbrandon.potter@amd.com if (!ffdp) 1140360SN/A return -EBADF; 1141360SN/A int sim_fd = ffdp->getSimFD(); 11421458SN/A 1143360SN/A mode_t hostMode = mode; 114410931Sbrandon.potter@amd.com 11452021SN/A int result = fchmod(sim_fd, hostMode); 11461458SN/A 1147360SN/A return (result < 0) ? -errno : 0; 1148360SN/A} 1149360SN/A 11501706SN/A/// Target mremap() handler. 11511706SN/Atemplate <class OS> 11521706SN/ASyscallReturn 115311851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 11542680Sktlim@umich.edu{ 11551706SN/A int index = 0; 115611799Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 115711799Sbrandon.potter@amd.com Addr start = process->getSyscallArg(tc, index); 115811799Sbrandon.potter@amd.com uint64_t old_length = process->getSyscallArg(tc, index); 11591706SN/A uint64_t new_length = process->getSyscallArg(tc, index); 11601706SN/A uint64_t flags = process->getSyscallArg(tc, index); 11616701Sgblack@eecs.umich.edu uint64_t provided_address = 0; 11628852Sandreas.hansson@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 11636701Sgblack@eecs.umich.edu 11646701Sgblack@eecs.umich.edu if (use_provided_address) 11656701Sgblack@eecs.umich.edu provided_address = process->getSyscallArg(tc, index); 11666701Sgblack@eecs.umich.edu 11671706SN/A if ((start % TheISA::PageBytes != 0) || 11683669Sbinkertn@umich.edu (provided_address % TheISA::PageBytes != 0)) { 11693669Sbinkertn@umich.edu warn("mremap failing: arguments not page aligned"); 11703669Sbinkertn@umich.edu return -EINVAL; 11711706SN/A } 11721706SN/A 11731706SN/A new_length = roundUp(new_length, TheISA::PageBytes); 11741706SN/A 11752218SN/A if (new_length > old_length) { 11761706SN/A std::shared_ptr<MemState> mem_state = process->memState; 117711759Sbrandon.potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 117811799Sbrandon.potter@amd.com 11791706SN/A if ((start + old_length) == mmap_end && 11801706SN/A (!use_provided_address || provided_address == start)) { 11811706SN/A // This case cannot occur when growing downward, as 11821706SN/A // start is greater than or equal to mmap_end. 11831706SN/A uint64_t diff = new_length - old_length; 11841706SN/A process->allocateMem(mmap_end, diff); 11851706SN/A mem_state->setMmapEnd(mmap_end + diff); 118611856Sbrandon.potter@amd.com return start; 11871706SN/A } else { 11886701Sgblack@eecs.umich.edu if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 118911856Sbrandon.potter@amd.com warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 119011856Sbrandon.potter@amd.com return -ENOMEM; 11911706SN/A } else { 119211856Sbrandon.potter@amd.com uint64_t new_start = provided_address; 119311856Sbrandon.potter@amd.com if (!use_provided_address) { 11941706SN/A new_start = process->mmapGrowsDown() ? 119511856Sbrandon.potter@amd.com mmap_end - new_length : mmap_end; 11961706SN/A mmap_end = process->mmapGrowsDown() ? 11971706SN/A new_start : mmap_end + new_length; 119810931Sbrandon.potter@amd.com mem_state->setMmapEnd(mmap_end); 11991706SN/A } 12001706SN/A 12012218SN/A process->pTable->remap(start, old_length, new_start); 12021706SN/A warn("mremapping to new vaddr %08p-%08p, adding %d\n", 120311759Sbrandon.potter@amd.com new_start, new_start + new_length, 12041706SN/A new_length - old_length); 12051706SN/A // add on the remaining unallocated pages 12061706SN/A process->allocateMem(new_start + old_length, 12071706SN/A new_length - old_length, 12081706SN/A use_provided_address /* clobber */); 12091999SN/A if (use_provided_address && 12101999SN/A ((new_start + new_length > mem_state->getMmapEnd() && 12111999SN/A !process->mmapGrowsDown()) || 121211856Sbrandon.potter@amd.com (new_start < mem_state->getMmapEnd() && 12131999SN/A process->mmapGrowsDown()))) { 12146701Sgblack@eecs.umich.edu // something fishy going on here, at least notify the user 121511856Sbrandon.potter@amd.com // @todo: increase mmap_end? 121610931Sbrandon.potter@amd.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 121711856Sbrandon.potter@amd.com } 121811856Sbrandon.potter@amd.com warn("returning %08p as start\n", new_start); 12191999SN/A return new_start; 122011856Sbrandon.potter@amd.com } 12211999SN/A } 122211856Sbrandon.potter@amd.com } else { 122311856Sbrandon.potter@amd.com if (use_provided_address && provided_address != start) 122411856Sbrandon.potter@amd.com process->pTable->remap(start, new_length, provided_address); 12251999SN/A process->pTable->unmap(start + new_length, old_length - new_length); 12266227Snate@binkert.org return use_provided_address ? provided_address : start; 12271999SN/A } 12282461SN/A} 122911856Sbrandon.potter@amd.com 123011856Sbrandon.potter@amd.com/// Target stat() handler. 12318737Skoansin.tan@gmail.comtemplate <class OS> 12321999SN/ASyscallReturn 123311856Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 123411856Sbrandon.potter@amd.com{ 12351999SN/A std::string path; 12361999SN/A auto process = tc->getProcessPtr(); 123710931Sbrandon.potter@amd.com 12381999SN/A int index = 0; 12396227Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, 12401999SN/A process->getSyscallArg(tc, index))) { 12411999SN/A return -EFAULT; 12421999SN/A } 12432218SN/A Addr bufPtr = process->getSyscallArg(tc, index); 12441999SN/A 124510629Sjthestness@gmail.com // Adjust path for cwd and redirection 12461999SN/A path = process->checkPathRedirect(path); 12471999SN/A 124811385Sbrandon.potter@amd.com struct stat hostBuf; 1249360SN/A int result = stat(path.c_str(), &hostBuf); 12501450SN/A 125111851Sbrandon.potter@amd.com if (result < 0) 125211385Sbrandon.potter@amd.com return -errno; 1253360SN/A 12546701Sgblack@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12556701Sgblack@eecs.umich.edu 12566701Sgblack@eecs.umich.edu return 0; 125711383Sbrandon.potter@amd.com} 125811383Sbrandon.potter@amd.com 12598324Ssteve.reinhardt@amd.com 126010486Stjablin@gmail.com/// Target stat64() handler. 1261360SN/Atemplate <class OS> 126211385Sbrandon.potter@amd.comSyscallReturn 126311385Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 12649008Sgblack@eecs.umich.edu{ 126511383Sbrandon.potter@amd.com std::string path; 126611383Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 126711383Sbrandon.potter@amd.com 126811383Sbrandon.potter@amd.com int index = 0; 126911383Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 127011383Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 127111383Sbrandon.potter@amd.com return -EFAULT; 127211383Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 127311383Sbrandon.potter@amd.com 12748324Ssteve.reinhardt@amd.com // Adjust path for cwd and redirection 127511383Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 127611383Sbrandon.potter@amd.com 127711383Sbrandon.potter@amd.com#if NO_STAT64 127811383Sbrandon.potter@amd.com struct stat hostBuf; 127911383Sbrandon.potter@amd.com int result = stat(path.c_str(), &hostBuf); 128011383Sbrandon.potter@amd.com#else 128111383Sbrandon.potter@amd.com struct stat64 hostBuf; 128211383Sbrandon.potter@amd.com int result = stat64(path.c_str(), &hostBuf); 128311383Sbrandon.potter@amd.com#endif 128411383Sbrandon.potter@amd.com 128511383Sbrandon.potter@amd.com if (result < 0) 128611383Sbrandon.potter@amd.com return -errno; 128711383Sbrandon.potter@amd.com 128811383Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 128911383Sbrandon.potter@amd.com 129011383Sbrandon.potter@amd.com return 0; 129111383Sbrandon.potter@amd.com} 129211383Sbrandon.potter@amd.com 129311383Sbrandon.potter@amd.com 129411383Sbrandon.potter@amd.com/// Target fstatat64() handler. 129511383Sbrandon.potter@amd.comtemplate <class OS> 129611383Sbrandon.potter@amd.comSyscallReturn 129711383Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 129811383Sbrandon.potter@amd.com{ 12998324Ssteve.reinhardt@amd.com int index = 0; 13005877Shsul@eecs.umich.edu auto process = tc->getProcessPtr(); 130110486Stjablin@gmail.com int dirfd = process->getSyscallArg(tc, index); 130210486Stjablin@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 130311383Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 130411383Sbrandon.potter@amd.com 130511383Sbrandon.potter@amd.com std::string path; 130611856Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 130711624Smichael.lebeane@amd.com process->getSyscallArg(tc, index))) 130811856Sbrandon.potter@amd.com return -EFAULT; 130911856Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 131011856Sbrandon.potter@amd.com 131111856Sbrandon.potter@amd.com // Adjust path for cwd and redirection 131211624Smichael.lebeane@amd.com path = process->checkPathRedirect(path); 131311624Smichael.lebeane@amd.com 131411624Smichael.lebeane@amd.com#if NO_STAT64 131511856Sbrandon.potter@amd.com struct stat hostBuf; 131611856Sbrandon.potter@amd.com int result = stat(path.c_str(), &hostBuf); 131711383Sbrandon.potter@amd.com#else 131811856Sbrandon.potter@amd.com struct stat64 hostBuf; 1319360SN/A int result = stat64(path.c_str(), &hostBuf); 132011383Sbrandon.potter@amd.com#endif 132111383Sbrandon.potter@amd.com 13228600Ssteve.reinhardt@amd.com if (result < 0) 132311383Sbrandon.potter@amd.com return -errno; 132411383Sbrandon.potter@amd.com 132511383Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13268600Ssteve.reinhardt@amd.com 13272544SN/A return 0; 13282544SN/A} 132911383Sbrandon.potter@amd.com 133011383Sbrandon.potter@amd.com 133111383Sbrandon.potter@amd.com/// Target fstat64() handler. 133211386Ssteve.reinhardt@amd.comtemplate <class OS> 133311386Ssteve.reinhardt@amd.comSyscallReturn 133411383Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 133511383Sbrandon.potter@amd.com{ 133611383Sbrandon.potter@amd.com int index = 0; 133711383Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 133811383Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 133911383Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 134011383Sbrandon.potter@amd.com 134111383Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 134211383Sbrandon.potter@amd.com if (!ffdp) 134311383Sbrandon.potter@amd.com return -EBADF; 134411383Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 134511383Sbrandon.potter@amd.com 134611383Sbrandon.potter@amd.com#if NO_STAT64 134711383Sbrandon.potter@amd.com struct stat hostBuf; 134811383Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 13498600Ssteve.reinhardt@amd.com#else 13506672Sgblack@eecs.umich.edu struct stat64 hostBuf; 13518600Ssteve.reinhardt@amd.com int result = fstat64(sim_fd, &hostBuf); 135211383Sbrandon.potter@amd.com#endif 135311383Sbrandon.potter@amd.com 135411383Sbrandon.potter@amd.com if (result < 0) 13558601Ssteve.reinhardt@amd.com return -errno; 13562544SN/A 135711383Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 135811383Sbrandon.potter@amd.com 135911383Sbrandon.potter@amd.com return 0; 136011383Sbrandon.potter@amd.com} 136111383Sbrandon.potter@amd.com 136211383Sbrandon.potter@amd.com 136311383Sbrandon.potter@amd.com/// Target lstat() handler. 136411383Sbrandon.potter@amd.comtemplate <class OS> 136511383Sbrandon.potter@amd.comSyscallReturn 136611383Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 136711383Sbrandon.potter@amd.com{ 136811383Sbrandon.potter@amd.com std::string path; 136911383Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 137011383Sbrandon.potter@amd.com 137111383Sbrandon.potter@amd.com int index = 0; 137211383Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 137311383Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 137411383Sbrandon.potter@amd.com return -EFAULT; 137511383Sbrandon.potter@amd.com } 137611383Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 137711383Sbrandon.potter@amd.com 137811383Sbrandon.potter@amd.com // Adjust path for cwd and redirection 137911383Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 138011383Sbrandon.potter@amd.com 138111383Sbrandon.potter@amd.com struct stat hostBuf; 138211383Sbrandon.potter@amd.com int result = lstat(path.c_str(), &hostBuf); 138311383Sbrandon.potter@amd.com 138411383Sbrandon.potter@amd.com if (result < 0) 138511383Sbrandon.potter@amd.com return -errno; 138611383Sbrandon.potter@amd.com 138711383Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 138811383Sbrandon.potter@amd.com 138911392Sbrandon.potter@amd.com return 0; 139011392Sbrandon.potter@amd.com} 139111392Sbrandon.potter@amd.com 139211392Sbrandon.potter@amd.com/// Target lstat64() handler. 139311392Sbrandon.potter@amd.comtemplate <class OS> 139411392Sbrandon.potter@amd.comSyscallReturn 139511392Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 139611392Sbrandon.potter@amd.com{ 139711392Sbrandon.potter@amd.com std::string path; 139811392Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 139911392Sbrandon.potter@amd.com 140011392Sbrandon.potter@amd.com int index = 0; 140111392Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 140211392Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 140311856Sbrandon.potter@amd.com return -EFAULT; 140411856Sbrandon.potter@amd.com } 140511856Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 140611392Sbrandon.potter@amd.com 140711392Sbrandon.potter@amd.com // Adjust path for cwd and redirection 140811392Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 140911392Sbrandon.potter@amd.com 141011392Sbrandon.potter@amd.com#if NO_STAT64 141111392Sbrandon.potter@amd.com struct stat hostBuf; 141211392Sbrandon.potter@amd.com int result = lstat(path.c_str(), &hostBuf); 141311392Sbrandon.potter@amd.com#else 141411383Sbrandon.potter@amd.com struct stat64 hostBuf; 141511383Sbrandon.potter@amd.com int result = lstat64(path.c_str(), &hostBuf); 141611383Sbrandon.potter@amd.com#endif 141711383Sbrandon.potter@amd.com 141811383Sbrandon.potter@amd.com if (result < 0) 14191458SN/A return -errno; 1420360SN/A 1421360SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 142211593Santhony.gutierrez@amd.com 142311593Santhony.gutierrez@amd.com return 0; 142411851Sbrandon.potter@amd.com} 142511593Santhony.gutierrez@amd.com 142611593Santhony.gutierrez@amd.com/// Target fstat() handler. 142711593Santhony.gutierrez@amd.comtemplate <class OS> 142811593Santhony.gutierrez@amd.comSyscallReturn 142911593Santhony.gutierrez@amd.comfstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 143011593Santhony.gutierrez@amd.com{ 143111593Santhony.gutierrez@amd.com int index = 0; 143211856Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 143311856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 143411593Santhony.gutierrez@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 143511856Sbrandon.potter@amd.com 143611593Santhony.gutierrez@amd.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 143711593Santhony.gutierrez@amd.com 143811593Santhony.gutierrez@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 143911593Santhony.gutierrez@amd.com if (!ffdp) 144011594Santhony.gutierrez@amd.com return -EBADF; 144111593Santhony.gutierrez@amd.com int sim_fd = ffdp->getSimFD(); 144211593Santhony.gutierrez@amd.com 144311593Santhony.gutierrez@amd.com struct stat hostBuf; 144411593Santhony.gutierrez@amd.com int result = fstat(sim_fd, &hostBuf); 144511385Sbrandon.potter@amd.com 144611385Sbrandon.potter@amd.com if (result < 0) 144711385Sbrandon.potter@amd.com return -errno; 144811851Sbrandon.potter@amd.com 144911385Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 145011385Sbrandon.potter@amd.com 145111385Sbrandon.potter@amd.com return 0; 145211385Sbrandon.potter@amd.com} 145311385Sbrandon.potter@amd.com 145411385Sbrandon.potter@amd.com/// Target statfs() handler. 145511385Sbrandon.potter@amd.comtemplate <class OS> 145611851Sbrandon.potter@amd.comSyscallReturn 145711385Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 145811385Sbrandon.potter@amd.com{ 145911385Sbrandon.potter@amd.com#if defined(__linux__) 146011385Sbrandon.potter@amd.com std::string path; 1461378SN/A auto process = tc->getProcessPtr(); 1462360SN/A 14631450SN/A int index = 0; 146411851Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 146511851Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 1466360SN/A return -EFAULT; 14676701Sgblack@eecs.umich.edu } 14686701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 14696701Sgblack@eecs.umich.edu 1470360SN/A // Adjust path for cwd and redirection 1471360SN/A path = process->checkPathRedirect(path); 14722064SN/A 14735877Shsul@eecs.umich.edu struct statfs hostBuf; 14742064SN/A int result = statfs(path.c_str(), &hostBuf); 14758737Skoansin.tan@gmail.com 14768737Skoansin.tan@gmail.com if (result < 0) 14772064SN/A return -errno; 1478360SN/A 14795877Shsul@eecs.umich.edu copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 14805877Shsul@eecs.umich.edu return 0; 14815877Shsul@eecs.umich.edu#else 14828737Skoansin.tan@gmail.com warnUnsupportedOS("statfs"); 14838737Skoansin.tan@gmail.com return -1; 14845877Shsul@eecs.umich.edu#endif 14855877Shsul@eecs.umich.edu} 14862064SN/A 148710794Sbrandon.potter@amd.comtemplate <class OS> 148810794Sbrandon.potter@amd.comSyscallReturn 14892064SN/AcloneFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1490360SN/A{ 1491360SN/A int index = 0; 14928706Sandreas.hansson@arm.com 14931458SN/A auto p = tc->getProcessPtr(); 1494360SN/A RegVal flags = p->getSyscallArg(tc, index); 1495360SN/A RegVal newStack = p->getSyscallArg(tc, index); 149610796Sbrandon.potter@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 149710796Sbrandon.potter@amd.com 149810796Sbrandon.potter@amd.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 149911851Sbrandon.potter@amd.com /** 150010796Sbrandon.potter@amd.com * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 150110796Sbrandon.potter@amd.com * The flag defines the list of clone() arguments in the following 150210796Sbrandon.potter@amd.com * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 150310796Sbrandon.potter@amd.com */ 150410796Sbrandon.potter@amd.com Addr tlsPtr = p->getSyscallArg(tc, index); 150510796Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 150610796Sbrandon.potter@amd.com#else 150710796Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 150810796Sbrandon.potter@amd.com Addr tlsPtr = p->getSyscallArg(tc, index); 150910796Sbrandon.potter@amd.com#endif 151010796Sbrandon.potter@amd.com 151110796Sbrandon.potter@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 151210796Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 151310796Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 151410796Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 151511337SMichael.Lebeane@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 151611337SMichael.Lebeane@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 151711337SMichael.Lebeane@amd.com return -EINVAL; 151811851Sbrandon.potter@amd.com 151911337SMichael.Lebeane@amd.com ThreadContext *ctc; 152011337SMichael.Lebeane@amd.com if (!(ctc = p->findFreeContext())) { 152111337SMichael.Lebeane@amd.com DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system" 152211337SMichael.Lebeane@amd.com "[cpu %d, thread %d]", tc->cpuId(), tc->threadId()); 152311337SMichael.Lebeane@amd.com return -EAGAIN; 152411337SMichael.Lebeane@amd.com } 152511337SMichael.Lebeane@amd.com 152611337SMichael.Lebeane@amd.com /** 152711337SMichael.Lebeane@amd.com * Note that ProcessParams is generated by swig and there are no other 152811337SMichael.Lebeane@amd.com * examples of how to create anything but this default constructor. The 152911337SMichael.Lebeane@amd.com * fields are manually initialized instead of passing parameters to the 153011337SMichael.Lebeane@amd.com * constructor. 153111337SMichael.Lebeane@amd.com */ 1532378SN/A ProcessParams *pp = new ProcessParams(); 1533360SN/A pp->executable.assign(*(new std::string(p->progName()))); 15341450SN/A pp->cmd.push_back(*(new std::string(p->progName()))); 153511851Sbrandon.potter@amd.com pp->system = p->system; 153611851Sbrandon.potter@amd.com pp->cwd.assign(p->tgtCwd); 1537360SN/A pp->input.assign("stdin"); 15386701Sgblack@eecs.umich.edu pp->output.assign("stdout"); 15396701Sgblack@eecs.umich.edu pp->errout.assign("stderr"); 1540360SN/A pp->uid = p->uid(); 154110796Sbrandon.potter@amd.com pp->euid = p->euid(); 1542360SN/A pp->gid = p->gid(); 15436109Ssanchezd@stanford.edu pp->egid = p->egid(); 15446109Ssanchezd@stanford.edu 1545360SN/A /* Find the first free PID that's less than the maximum */ 15468706Sandreas.hansson@arm.com std::set<int> const& pids = p->system->PIDs; 1547360SN/A int temp_pid = *pids.begin(); 15481458SN/A do { 1549360SN/A temp_pid++; 1550360SN/A } while (pids.find(temp_pid) != pids.end()); 1551360SN/A if (temp_pid >= System::maxPID) 15521999SN/A fatal("temp_pid is too large: %d", temp_pid); 15531999SN/A 15541999SN/A pp->pid = temp_pid; 155511851Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 15562680Sktlim@umich.edu pp->useArchPT = p->useArchPT; 15571999SN/A pp->kvmInSE = p->kvmInSE; 15581999SN/A Process *cp = pp->create(); 15591999SN/A delete pp; 15606701Sgblack@eecs.umich.edu 15618852Sandreas.hansson@arm.com Process *owner = ctc->getProcessPtr(); 15626701Sgblack@eecs.umich.edu ctc->setProcessPtr(cp); 15636701Sgblack@eecs.umich.edu cp->assignThreadContext(ctc->contextId()); 15646701Sgblack@eecs.umich.edu owner->revokeThreadContext(ctc->contextId()); 15651999SN/A 15666701Sgblack@eecs.umich.edu if (flags & OS::TGT_CLONE_PARENT_SETTID) { 15676701Sgblack@eecs.umich.edu BufferArg ptidBuf(ptidPtr, sizeof(long)); 15688706Sandreas.hansson@arm.com long *ptid = (long *)ptidBuf.bufferPtr(); 15691999SN/A *ptid = cp->pid(); 15701999SN/A ptidBuf.copyOut(tc->getMemProxy()); 15711999SN/A } 15721999SN/A 15738737Skoansin.tan@gmail.com if (flags & OS::TGT_CLONE_THREAD) { 15748737Skoansin.tan@gmail.com cp->pTable->shared = true; 15751999SN/A cp->useForClone = true; 15763669Sbinkertn@umich.edu } 15773669Sbinkertn@umich.edu cp->initState(); 15783669Sbinkertn@umich.edu p->clone(tc, ctc, cp, flags); 15793669Sbinkertn@umich.edu 15801999SN/A if (flags & OS::TGT_CLONE_THREAD) { 15811999SN/A delete cp->sigchld; 15821999SN/A cp->sigchld = p->sigchld; 15831999SN/A } else if (flags & OS::TGT_SIGCHLD) { 15841999SN/A *cp->sigchld = true; 15851999SN/A } 15861999SN/A 1587378SN/A if (flags & OS::TGT_CLONE_CHILD_SETTID) { 1588360SN/A BufferArg ctidBuf(ctidPtr, sizeof(long)); 15891450SN/A long *ctid = (long *)ctidBuf.bufferPtr(); 159011851Sbrandon.potter@amd.com *ctid = cp->pid(); 15912680Sktlim@umich.edu ctidBuf.copyOut(ctc->getMemProxy()); 1592360SN/A } 15936701Sgblack@eecs.umich.edu 15946701Sgblack@eecs.umich.edu if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 15956701Sgblack@eecs.umich.edu cp->childClearTID = (uint64_t)ctidPtr; 1596360SN/A 15973670Sbinkertn@umich.edu ctc->clearArchRegs(); 15983670Sbinkertn@umich.edu 1599360SN/A OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 1600360SN/A 1601360SN/A cp->setSyscallReturn(ctc, 0); 1602360SN/A 1603360SN/A#if THE_ISA == ALPHA_ISA 1604360SN/A ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 1605360SN/A#elif THE_ISA == SPARC_ISA 1606360SN/A tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1607360SN/A ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 1608360SN/A#endif 1609360SN/A 1610360SN/A if (p->kvmInSE) { 1611360SN/A#if THE_ISA == X86_ISA 1612360SN/A ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX)); 1613360SN/A#else 1614360SN/A panic("KVM CPU model is not supported for this ISA"); 1615360SN/A#endif 16163670Sbinkertn@umich.edu } else { 16173670Sbinkertn@umich.edu TheISA::PCState cpc = tc->pcState(); 161810796Sbrandon.potter@amd.com cpc.advance(); 16198737Skoansin.tan@gmail.com ctc->pcState(cpc); 16208737Skoansin.tan@gmail.com } 16213670Sbinkertn@umich.edu ctc->activate(); 16223670Sbinkertn@umich.edu 16233670Sbinkertn@umich.edu return cp->pid(); 16243670Sbinkertn@umich.edu} 16253670Sbinkertn@umich.edu 16263670Sbinkertn@umich.edu/// Target fstatfs() handler. 16273670Sbinkertn@umich.edutemplate <class OS> 16283670Sbinkertn@umich.eduSyscallReturn 16293670Sbinkertn@umich.edufstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 16303670Sbinkertn@umich.edu{ 16313670Sbinkertn@umich.edu int index = 0; 16323670Sbinkertn@umich.edu auto p = tc->getProcessPtr(); 16333670Sbinkertn@umich.edu int tgt_fd = p->getSyscallArg(tc, index); 16348706Sandreas.hansson@arm.com Addr bufPtr = p->getSyscallArg(tc, index); 1635360SN/A 16361458SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1637360SN/A if (!ffdp) 1638360SN/A return -EBADF; 16396683Stjones1@inf.ed.ac.uk int sim_fd = ffdp->getSimFD(); 16406683Stjones1@inf.ed.ac.uk 16416683Stjones1@inf.ed.ac.uk struct statfs hostBuf; 164211851Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 164311851Sbrandon.potter@amd.com 16446683Stjones1@inf.ed.ac.uk if (result < 0) 16456701Sgblack@eecs.umich.edu return -errno; 16466701Sgblack@eecs.umich.edu 16476683Stjones1@inf.ed.ac.uk copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 16486683Stjones1@inf.ed.ac.uk 16497823Ssteve.reinhardt@amd.com return 0; 16506683Stjones1@inf.ed.ac.uk} 16516683Stjones1@inf.ed.ac.uk 16526683Stjones1@inf.ed.ac.uk/// Target readv() handler. 16536683Stjones1@inf.ed.ac.uktemplate <class OS> 16546683Stjones1@inf.ed.ac.ukSyscallReturn 16556683Stjones1@inf.ed.ac.ukreadvFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 16568737Skoansin.tan@gmail.com{ 16576683Stjones1@inf.ed.ac.uk int index = 0; 16586683Stjones1@inf.ed.ac.uk auto p = tc->getProcessPtr(); 16598706Sandreas.hansson@arm.com int tgt_fd = p->getSyscallArg(tc, index); 16606683Stjones1@inf.ed.ac.uk 16616683Stjones1@inf.ed.ac.uk auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16626683Stjones1@inf.ed.ac.uk if (!ffdp) 16636683Stjones1@inf.ed.ac.uk return -EBADF; 16642553SN/A int sim_fd = ffdp->getSimFD(); 16656684Stjones1@inf.ed.ac.uk 16666684Stjones1@inf.ed.ac.uk SETranslatingPortProxy &prox = tc->getMemProxy(); 16676684Stjones1@inf.ed.ac.uk uint64_t tiov_base = p->getSyscallArg(tc, index); 166811851Sbrandon.potter@amd.com size_t count = p->getSyscallArg(tc, index); 16696684Stjones1@inf.ed.ac.uk typename OS::tgt_iovec tiov[count]; 16706684Stjones1@inf.ed.ac.uk struct iovec hiov[count]; 167110796Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) { 16726684Stjones1@inf.ed.ac.uk prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)), 16736684Stjones1@inf.ed.ac.uk (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec)); 16746701Sgblack@eecs.umich.edu hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len); 16756701Sgblack@eecs.umich.edu hiov[i].iov_base = new char [hiov[i].iov_len]; 167611321Ssteve.reinhardt@amd.com } 16776684Stjones1@inf.ed.ac.uk 16788737Skoansin.tan@gmail.com int result = readv(sim_fd, hiov, count); 16798852Sandreas.hansson@arm.com int local_errno = errno; 16808852Sandreas.hansson@arm.com 16816684Stjones1@inf.ed.ac.uk for (size_t i = 0; i < count; ++i) { 16826684Stjones1@inf.ed.ac.uk if (result != -1) { 16836684Stjones1@inf.ed.ac.uk prox.writeBlob(TheISA::htog(tiov[i].iov_base), 16842553SN/A (uint8_t*)hiov[i].iov_base, hiov[i].iov_len); 16852553SN/A } 16861354SN/A delete [] (char *)hiov[i].iov_base; 1687 } 1688 1689 return (result == -1) ? -local_errno : result; 1690} 1691 1692/// Target writev() handler. 1693template <class OS> 1694SyscallReturn 1695writevFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1696{ 1697 int index = 0; 1698 auto p = tc->getProcessPtr(); 1699 int tgt_fd = p->getSyscallArg(tc, index); 1700 1701 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 1702 if (!hbfdp) 1703 return -EBADF; 1704 int sim_fd = hbfdp->getSimFD(); 1705 1706 SETranslatingPortProxy &prox = tc->getMemProxy(); 1707 uint64_t tiov_base = p->getSyscallArg(tc, index); 1708 size_t count = p->getSyscallArg(tc, index); 1709 struct iovec hiov[count]; 1710 for (size_t i = 0; i < count; ++i) { 1711 typename OS::tgt_iovec tiov; 1712 1713 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 1714 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 1715 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1716 hiov[i].iov_base = new char [hiov[i].iov_len]; 1717 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 1718 hiov[i].iov_len); 1719 } 1720 1721 int result = writev(sim_fd, hiov, count); 1722 1723 for (size_t i = 0; i < count; ++i) 1724 delete [] (char *)hiov[i].iov_base; 1725 1726 return (result == -1) ? -errno : result; 1727} 1728 1729/// Real mmap handler. 1730template <class OS> 1731SyscallReturn 1732mmapImpl(SyscallDesc *desc, int num, ThreadContext *tc, bool is_mmap2) 1733{ 1734 int index = 0; 1735 auto p = tc->getProcessPtr(); 1736 Addr start = p->getSyscallArg(tc, index); 1737 uint64_t length = p->getSyscallArg(tc, index); 1738 int prot = p->getSyscallArg(tc, index); 1739 int tgt_flags = p->getSyscallArg(tc, index); 1740 int tgt_fd = p->getSyscallArg(tc, index); 1741 int offset = p->getSyscallArg(tc, index); 1742 1743 if (is_mmap2) 1744 offset *= TheISA::PageBytes; 1745 1746 if (start & (TheISA::PageBytes - 1) || 1747 offset & (TheISA::PageBytes - 1) || 1748 (tgt_flags & OS::TGT_MAP_PRIVATE && 1749 tgt_flags & OS::TGT_MAP_SHARED) || 1750 (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 1751 !(tgt_flags & OS::TGT_MAP_SHARED)) || 1752 !length) { 1753 return -EINVAL; 1754 } 1755 1756 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1757 // With shared mmaps, there are two cases to consider: 1758 // 1) anonymous: writes should modify the mapping and this should be 1759 // visible to observers who share the mapping. Currently, it's 1760 // difficult to update the shared mapping because there's no 1761 // structure which maintains information about the which virtual 1762 // memory areas are shared. If that structure existed, it would be 1763 // possible to make the translations point to the same frames. 1764 // 2) file-backed: writes should modify the mapping and the file 1765 // which is backed by the mapping. The shared mapping problem is the 1766 // same as what was mentioned about the anonymous mappings. For 1767 // file-backed mappings, the writes to the file are difficult 1768 // because it requires syncing what the mapping holds with the file 1769 // that resides on the host system. So, any write on a real system 1770 // would cause the change to be propagated to the file mapping at 1771 // some point in the future (the inode is tracked along with the 1772 // mapping). This isn't guaranteed to always happen, but it usually 1773 // works well enough. The guarantee is provided by the msync system 1774 // call. We could force the change through with shared mappings with 1775 // a call to msync, but that again would require more information 1776 // than we currently maintain. 1777 warn("mmap: writing to shared mmap region is currently " 1778 "unsupported. The write succeeds on the target, but it " 1779 "will not be propagated to the host or shared mappings"); 1780 } 1781 1782 length = roundUp(length, TheISA::PageBytes); 1783 1784 int sim_fd = -1; 1785 uint8_t *pmap = nullptr; 1786 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1787 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1788 1789 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1790 if (dfdp) { 1791 EmulatedDriver *emul_driver = dfdp->getDriver(); 1792 return emul_driver->mmap(tc, start, length, prot, tgt_flags, 1793 tgt_fd, offset); 1794 } 1795 1796 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1797 if (!ffdp) 1798 return -EBADF; 1799 sim_fd = ffdp->getSimFD(); 1800 1801 pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 1802 sim_fd, offset); 1803 1804 if (pmap == (decltype(pmap))-1) { 1805 warn("mmap: failed to map file into host address space"); 1806 return -errno; 1807 } 1808 } 1809 1810 // Extend global mmap region if necessary. Note that we ignore the 1811 // start address unless MAP_FIXED is specified. 1812 if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 1813 std::shared_ptr<MemState> mem_state = p->memState; 1814 Addr mmap_end = mem_state->getMmapEnd(); 1815 1816 start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1817 mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 1818 1819 mem_state->setMmapEnd(mmap_end); 1820 } 1821 1822 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1823 start, start + length - 1); 1824 1825 // We only allow mappings to overwrite existing mappings if 1826 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 1827 // because we ignore the start hint if TGT_MAP_FIXED is not set. 1828 int clobber = tgt_flags & OS::TGT_MAP_FIXED; 1829 if (clobber) { 1830 for (auto tc : p->system->threadContexts) { 1831 // If we might be overwriting old mappings, we need to 1832 // invalidate potentially stale mappings out of the TLBs. 1833 tc->getDTBPtr()->flushAll(); 1834 tc->getITBPtr()->flushAll(); 1835 } 1836 } 1837 1838 // Allocate physical memory and map it in. If the page table is already 1839 // mapped and clobber is not set, the simulator will issue throw a 1840 // fatal and bail out of the simulation. 1841 p->allocateMem(start, length, clobber); 1842 1843 // Transfer content into target address space. 1844 SETranslatingPortProxy &tp = tc->getMemProxy(); 1845 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1846 // In general, we should zero the mapped area for anonymous mappings, 1847 // with something like: 1848 // tp.memsetBlob(start, 0, length); 1849 // However, given that we don't support sparse mappings, and 1850 // some applications can map a couple of gigabytes of space 1851 // (intending sparse usage), that can get painfully expensive. 1852 // Fortunately, since we don't properly implement munmap either, 1853 // there's no danger of remapping used memory, so for now all 1854 // newly mapped memory should already be zeroed so we can skip it. 1855 } else { 1856 // It is possible to mmap an area larger than a file, however 1857 // accessing unmapped portions the system triggers a "Bus error" 1858 // on the host. We must know when to stop copying the file from 1859 // the host into the target address space. 1860 struct stat file_stat; 1861 if (fstat(sim_fd, &file_stat) > 0) 1862 fatal("mmap: cannot stat file"); 1863 1864 // Copy the portion of the file that is resident. This requires 1865 // checking both the mmap size and the filesize that we are 1866 // trying to mmap into this space; the mmap size also depends 1867 // on the specified offset into the file. 1868 uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1869 length); 1870 tp.writeBlob(start, pmap, size); 1871 1872 // Cleanup the mmap region before exiting this function. 1873 munmap(pmap, length); 1874 1875 // Maintain the symbol table for dynamic executables. 1876 // The loader will call mmap to map the images into its address 1877 // space and we intercept that here. We can verify that we are 1878 // executing inside the loader by checking the program counter value. 1879 // XXX: with multiprogrammed workloads or multi-node configurations, 1880 // this will not work since there is a single global symbol table. 1881 ObjectFile *interpreter = p->getInterpreter(); 1882 if (interpreter) { 1883 Addr text_start = interpreter->textBase(); 1884 Addr text_end = text_start + interpreter->textSize(); 1885 1886 Addr pc = tc->pcState().pc(); 1887 1888 if (pc >= text_start && pc < text_end) { 1889 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1890 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1891 ObjectFile *lib = createObjectFile(ffdp->getFileName()); 1892 1893 if (lib) { 1894 lib->loadAllSymbols(debugSymbolTable, 1895 lib->textBase(), start); 1896 } 1897 } 1898 } 1899 1900 // Note that we do not zero out the remainder of the mapping. This 1901 // is done by a real system, but it probably will not affect 1902 // execution (hopefully). 1903 } 1904 1905 return start; 1906} 1907 1908template <class OS> 1909SyscallReturn 1910pwrite64Func(SyscallDesc *desc, int num, ThreadContext *tc) 1911{ 1912 int index = 0; 1913 auto p = tc->getProcessPtr(); 1914 int tgt_fd = p->getSyscallArg(tc, index); 1915 Addr bufPtr = p->getSyscallArg(tc, index); 1916 int nbytes = p->getSyscallArg(tc, index); 1917 int offset = p->getSyscallArg(tc, index); 1918 1919 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1920 if (!ffdp) 1921 return -EBADF; 1922 int sim_fd = ffdp->getSimFD(); 1923 1924 BufferArg bufArg(bufPtr, nbytes); 1925 bufArg.copyIn(tc->getMemProxy()); 1926 1927 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1928 1929 return (bytes_written == -1) ? -errno : bytes_written; 1930} 1931 1932/// Target mmap() handler. 1933template <class OS> 1934SyscallReturn 1935mmapFunc(SyscallDesc *desc, int num, ThreadContext *tc) 1936{ 1937 return mmapImpl<OS>(desc, num, tc, false); 1938} 1939 1940/// Target mmap2() handler. 1941template <class OS> 1942SyscallReturn 1943mmap2Func(SyscallDesc *desc, int num, ThreadContext *tc) 1944{ 1945 return mmapImpl<OS>(desc, num, tc, true); 1946} 1947 1948/// Target getrlimit() handler. 1949template <class OS> 1950SyscallReturn 1951getrlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1952{ 1953 int index = 0; 1954 auto process = tc->getProcessPtr(); 1955 unsigned resource = process->getSyscallArg(tc, index); 1956 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1957 1958 switch (resource) { 1959 case OS::TGT_RLIMIT_STACK: 1960 // max stack size in bytes: make up a number (8MB for now) 1961 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1962 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1963 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1964 break; 1965 1966 case OS::TGT_RLIMIT_DATA: 1967 // max data segment size in bytes: make up a number 1968 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1969 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1970 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1971 break; 1972 1973 default: 1974 warn("getrlimit: unimplemented resource %d", resource); 1975 return -EINVAL; 1976 break; 1977 } 1978 1979 rlp.copyOut(tc->getMemProxy()); 1980 return 0; 1981} 1982 1983template <class OS> 1984SyscallReturn 1985prlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1986{ 1987 int index = 0; 1988 auto process = tc->getProcessPtr(); 1989 if (process->getSyscallArg(tc, index) != 0) 1990 { 1991 warn("prlimit: ignoring rlimits for nonzero pid"); 1992 return -EPERM; 1993 } 1994 int resource = process->getSyscallArg(tc, index); 1995 Addr n = process->getSyscallArg(tc, index); 1996 if (n != 0) 1997 warn("prlimit: ignoring new rlimit"); 1998 Addr o = process->getSyscallArg(tc, index); 1999 if (o != 0) 2000 { 2001 TypedBufferArg<typename OS::rlimit> rlp(o); 2002 switch (resource) { 2003 case OS::TGT_RLIMIT_STACK: 2004 // max stack size in bytes: make up a number (8MB for now) 2005 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 2006 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2007 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2008 break; 2009 case OS::TGT_RLIMIT_DATA: 2010 // max data segment size in bytes: make up a number 2011 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 2012 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2013 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2014 break; 2015 default: 2016 warn("prlimit: unimplemented resource %d", resource); 2017 return -EINVAL; 2018 break; 2019 } 2020 rlp.copyOut(tc->getMemProxy()); 2021 } 2022 return 0; 2023} 2024 2025/// Target clock_gettime() function. 2026template <class OS> 2027SyscallReturn 2028clock_gettimeFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2029{ 2030 int index = 1; 2031 auto p = tc->getProcessPtr(); 2032 //int clk_id = p->getSyscallArg(tc, index); 2033 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 2034 2035 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 2036 tp->tv_sec += seconds_since_epoch; 2037 tp->tv_sec = TheISA::htog(tp->tv_sec); 2038 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 2039 2040 tp.copyOut(tc->getMemProxy()); 2041 2042 return 0; 2043} 2044 2045/// Target clock_getres() function. 2046template <class OS> 2047SyscallReturn 2048clock_getresFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2049{ 2050 int index = 1; 2051 auto p = tc->getProcessPtr(); 2052 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 2053 2054 // Set resolution at ns, which is what clock_gettime() returns 2055 tp->tv_sec = 0; 2056 tp->tv_nsec = 1; 2057 2058 tp.copyOut(tc->getMemProxy()); 2059 2060 return 0; 2061} 2062 2063/// Target gettimeofday() handler. 2064template <class OS> 2065SyscallReturn 2066gettimeofdayFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2067{ 2068 int index = 0; 2069 auto process = tc->getProcessPtr(); 2070 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 2071 2072 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 2073 tp->tv_sec += seconds_since_epoch; 2074 tp->tv_sec = TheISA::htog(tp->tv_sec); 2075 tp->tv_usec = TheISA::htog(tp->tv_usec); 2076 2077 tp.copyOut(tc->getMemProxy()); 2078 2079 return 0; 2080} 2081 2082 2083/// Target utimes() handler. 2084template <class OS> 2085SyscallReturn 2086utimesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2087{ 2088 std::string path; 2089 auto process = tc->getProcessPtr(); 2090 2091 int index = 0; 2092 if (!tc->getMemProxy().tryReadString(path, 2093 process->getSyscallArg(tc, index))) { 2094 return -EFAULT; 2095 } 2096 2097 TypedBufferArg<typename OS::timeval [2]> 2098 tp(process->getSyscallArg(tc, index)); 2099 tp.copyIn(tc->getMemProxy()); 2100 2101 struct timeval hostTimeval[2]; 2102 for (int i = 0; i < 2; ++i) { 2103 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 2104 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 2105 } 2106 2107 // Adjust path for cwd and redirection 2108 path = process->checkPathRedirect(path); 2109 2110 int result = utimes(path.c_str(), hostTimeval); 2111 2112 if (result < 0) 2113 return -errno; 2114 2115 return 0; 2116} 2117 2118template <class OS> 2119SyscallReturn 2120execveFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2121{ 2122 desc->setFlags(0); 2123 auto p = tc->getProcessPtr(); 2124 2125 int index = 0; 2126 std::string path; 2127 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 2128 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 2129 return -EFAULT; 2130 2131 if (access(path.c_str(), F_OK) == -1) 2132 return -EACCES; 2133 2134 auto read_in = [](std::vector<std::string> & vect, 2135 SETranslatingPortProxy & mem_proxy, 2136 Addr mem_loc) 2137 { 2138 for (int inc = 0; ; inc++) { 2139 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 2140 b.copyIn(mem_proxy); 2141 2142 if (!*(Addr*)b.bufferPtr()) 2143 break; 2144 2145 vect.push_back(std::string()); 2146 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 2147 } 2148 }; 2149 2150 /** 2151 * Note that ProcessParams is generated by swig and there are no other 2152 * examples of how to create anything but this default constructor. The 2153 * fields are manually initialized instead of passing parameters to the 2154 * constructor. 2155 */ 2156 ProcessParams *pp = new ProcessParams(); 2157 pp->executable = path; 2158 Addr argv_mem_loc = p->getSyscallArg(tc, index); 2159 read_in(pp->cmd, mem_proxy, argv_mem_loc); 2160 Addr envp_mem_loc = p->getSyscallArg(tc, index); 2161 read_in(pp->env, mem_proxy, envp_mem_loc); 2162 pp->uid = p->uid(); 2163 pp->egid = p->egid(); 2164 pp->euid = p->euid(); 2165 pp->gid = p->gid(); 2166 pp->ppid = p->ppid(); 2167 pp->pid = p->pid(); 2168 pp->input.assign("cin"); 2169 pp->output.assign("cout"); 2170 pp->errout.assign("cerr"); 2171 pp->cwd.assign(p->tgtCwd); 2172 pp->system = p->system; 2173 /** 2174 * Prevent process object creation with identical PIDs (which will trip 2175 * a fatal check in Process constructor). The execve call is supposed to 2176 * take over the currently executing process' identity but replace 2177 * whatever it is doing with a new process image. Instead of hijacking 2178 * the process object in the simulator, we create a new process object 2179 * and bind to the previous process' thread below (hijacking the thread). 2180 */ 2181 p->system->PIDs.erase(p->pid()); 2182 Process *new_p = pp->create(); 2183 delete pp; 2184 2185 /** 2186 * Work through the file descriptor array and close any files marked 2187 * close-on-exec. 2188 */ 2189 new_p->fds = p->fds; 2190 for (int i = 0; i < new_p->fds->getSize(); i++) { 2191 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 2192 if (fdep && fdep->getCOE()) 2193 new_p->fds->closeFDEntry(i); 2194 } 2195 2196 *new_p->sigchld = true; 2197 2198 delete p; 2199 tc->clearArchRegs(); 2200 tc->setProcessPtr(new_p); 2201 new_p->assignThreadContext(tc->contextId()); 2202 new_p->initState(); 2203 tc->activate(); 2204 TheISA::PCState pcState = tc->pcState(); 2205 tc->setNPC(pcState.instAddr()); 2206 2207 desc->setFlags(SyscallDesc::SuppressReturnValue); 2208 return 0; 2209} 2210 2211/// Target getrusage() function. 2212template <class OS> 2213SyscallReturn 2214getrusageFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2215{ 2216 int index = 0; 2217 auto process = tc->getProcessPtr(); 2218 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 2219 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2220 2221 rup->ru_utime.tv_sec = 0; 2222 rup->ru_utime.tv_usec = 0; 2223 rup->ru_stime.tv_sec = 0; 2224 rup->ru_stime.tv_usec = 0; 2225 rup->ru_maxrss = 0; 2226 rup->ru_ixrss = 0; 2227 rup->ru_idrss = 0; 2228 rup->ru_isrss = 0; 2229 rup->ru_minflt = 0; 2230 rup->ru_majflt = 0; 2231 rup->ru_nswap = 0; 2232 rup->ru_inblock = 0; 2233 rup->ru_oublock = 0; 2234 rup->ru_msgsnd = 0; 2235 rup->ru_msgrcv = 0; 2236 rup->ru_nsignals = 0; 2237 rup->ru_nvcsw = 0; 2238 rup->ru_nivcsw = 0; 2239 2240 switch (who) { 2241 case OS::TGT_RUSAGE_SELF: 2242 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 2243 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 2244 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 2245 break; 2246 2247 case OS::TGT_RUSAGE_CHILDREN: 2248 // do nothing. We have no child processes, so they take no time. 2249 break; 2250 2251 default: 2252 // don't really handle THREAD or CHILDREN, but just warn and 2253 // plow ahead 2254 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 2255 who); 2256 } 2257 2258 rup.copyOut(tc->getMemProxy()); 2259 2260 return 0; 2261} 2262 2263/// Target times() function. 2264template <class OS> 2265SyscallReturn 2266timesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2267{ 2268 int index = 0; 2269 auto process = tc->getProcessPtr(); 2270 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 2271 2272 // Fill in the time structure (in clocks) 2273 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 2274 bufp->tms_utime = clocks; 2275 bufp->tms_stime = 0; 2276 bufp->tms_cutime = 0; 2277 bufp->tms_cstime = 0; 2278 2279 // Convert to host endianness 2280 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 2281 2282 // Write back 2283 bufp.copyOut(tc->getMemProxy()); 2284 2285 // Return clock ticks since system boot 2286 return clocks; 2287} 2288 2289/// Target time() function. 2290template <class OS> 2291SyscallReturn 2292timeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2293{ 2294 typename OS::time_t sec, usec; 2295 getElapsedTimeMicro(sec, usec); 2296 sec += seconds_since_epoch; 2297 2298 int index = 0; 2299 auto process = tc->getProcessPtr(); 2300 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2301 if (taddr != 0) { 2302 typename OS::time_t t = sec; 2303 t = TheISA::htog(t); 2304 SETranslatingPortProxy &p = tc->getMemProxy(); 2305 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2306 } 2307 return sec; 2308} 2309 2310template <class OS> 2311SyscallReturn 2312tgkillFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2313{ 2314 int index = 0; 2315 auto process = tc->getProcessPtr(); 2316 int tgid = process->getSyscallArg(tc, index); 2317 int tid = process->getSyscallArg(tc, index); 2318 int sig = process->getSyscallArg(tc, index); 2319 2320 /** 2321 * This system call is intended to allow killing a specific thread 2322 * within an arbitrary thread group if sanctioned with permission checks. 2323 * It's usually true that threads share the termination signal as pointed 2324 * out by the pthread_kill man page and this seems to be the intended 2325 * usage. Due to this being an emulated environment, assume the following: 2326 * Threads are allowed to call tgkill because the EUID for all threads 2327 * should be the same. There is no signal handling mechanism for kernel 2328 * registration of signal handlers since signals are poorly supported in 2329 * emulation mode. Since signal handlers cannot be registered, all 2330 * threads within in a thread group must share the termination signal. 2331 * We never exhaust PIDs so there's no chance of finding the wrong one 2332 * due to PID rollover. 2333 */ 2334 2335 System *sys = tc->getSystemPtr(); 2336 Process *tgt_proc = nullptr; 2337 for (int i = 0; i < sys->numContexts(); i++) { 2338 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2339 if (temp->pid() == tid) { 2340 tgt_proc = temp; 2341 break; 2342 } 2343 } 2344 2345 if (sig != 0 || sig != OS::TGT_SIGABRT) 2346 return -EINVAL; 2347 2348 if (tgt_proc == nullptr) 2349 return -ESRCH; 2350 2351 if (tgid != -1 && tgt_proc->tgid() != tgid) 2352 return -ESRCH; 2353 2354 if (sig == OS::TGT_SIGABRT) 2355 exitGroupFunc(desc, 252, tc); 2356 2357 return 0; 2358} 2359 2360template <class OS> 2361SyscallReturn 2362socketFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2363{ 2364 int index = 0; 2365 auto p = tc->getProcessPtr(); 2366 int domain = p->getSyscallArg(tc, index); 2367 int type = p->getSyscallArg(tc, index); 2368 int prot = p->getSyscallArg(tc, index); 2369 2370 int sim_fd = socket(domain, type, prot); 2371 if (sim_fd == -1) 2372 return -errno; 2373 2374 auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 2375 int tgt_fd = p->fds->allocFD(sfdp); 2376 2377 return tgt_fd; 2378} 2379 2380template <class OS> 2381SyscallReturn 2382socketpairFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2383{ 2384 int index = 0; 2385 auto p = tc->getProcessPtr(); 2386 int domain = p->getSyscallArg(tc, index); 2387 int type = p->getSyscallArg(tc, index); 2388 int prot = p->getSyscallArg(tc, index); 2389 Addr svPtr = p->getSyscallArg(tc, index); 2390 2391 BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 2392 int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 2393 if (status == -1) 2394 return -errno; 2395 2396 int *fds = (int *)svBuf.bufferPtr(); 2397 2398 auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 2399 fds[0] = p->fds->allocFD(sfdp1); 2400 auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 2401 fds[1] = p->fds->allocFD(sfdp2); 2402 svBuf.copyOut(tc->getMemProxy()); 2403 2404 return status; 2405} 2406 2407template <class OS> 2408SyscallReturn 2409selectFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2410{ 2411 int retval; 2412 2413 int index = 0; 2414 auto p = tc->getProcessPtr(); 2415 int nfds_t = p->getSyscallArg(tc, index); 2416 Addr fds_read_ptr = p->getSyscallArg(tc, index); 2417 Addr fds_writ_ptr = p->getSyscallArg(tc, index); 2418 Addr fds_excp_ptr = p->getSyscallArg(tc, index); 2419 Addr time_val_ptr = p->getSyscallArg(tc, index); 2420 2421 TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 2422 TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 2423 TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 2424 TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 2425 2426 /** 2427 * Host fields. Notice that these use the definitions from the system 2428 * headers instead of the gem5 headers and libraries. If the host and 2429 * target have different header file definitions, this will not work. 2430 */ 2431 fd_set rd_h; 2432 FD_ZERO(&rd_h); 2433 fd_set wr_h; 2434 FD_ZERO(&wr_h); 2435 fd_set ex_h; 2436 FD_ZERO(&ex_h); 2437 2438 /** 2439 * Copy in the fd_set from the target. 2440 */ 2441 if (fds_read_ptr) 2442 rd_t.copyIn(tc->getMemProxy()); 2443 if (fds_writ_ptr) 2444 wr_t.copyIn(tc->getMemProxy()); 2445 if (fds_excp_ptr) 2446 ex_t.copyIn(tc->getMemProxy()); 2447 2448 /** 2449 * We need to translate the target file descriptor set into a host file 2450 * descriptor set. This involves both our internal process fd array 2451 * and the fd_set defined in Linux header files. The nfds field also 2452 * needs to be updated as it will be only target specific after 2453 * retrieving it from the target; the nfds value is expected to be the 2454 * highest file descriptor that needs to be checked, so we need to extend 2455 * it out for nfds_h when we do the update. 2456 */ 2457 int nfds_h = 0; 2458 std::map<int, int> trans_map; 2459 auto try_add_host_set = [&](fd_set *tgt_set_entry, 2460 fd_set *hst_set_entry, 2461 int iter) -> bool 2462 { 2463 /** 2464 * By this point, we know that we are looking at a valid file 2465 * descriptor set on the target. We need to check if the target file 2466 * descriptor value passed in as iter is part of the set. 2467 */ 2468 if (FD_ISSET(iter, tgt_set_entry)) { 2469 /** 2470 * We know that the target file descriptor belongs to the set, 2471 * but we do not yet know if the file descriptor is valid or 2472 * that we have a host mapping. Check that now. 2473 */ 2474 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 2475 if (!hbfdp) 2476 return true; 2477 auto sim_fd = hbfdp->getSimFD(); 2478 2479 /** 2480 * Add the sim_fd to tgt_fd translation into trans_map for use 2481 * later when we need to zero the target fd_set structures and 2482 * then update them with hits returned from the host select call. 2483 */ 2484 trans_map[sim_fd] = iter; 2485 2486 /** 2487 * We know that the host file descriptor exists so now we check 2488 * if we need to update the max count for nfds_h before passing 2489 * the duplicated structure into the host. 2490 */ 2491 nfds_h = std::max(nfds_h - 1, sim_fd + 1); 2492 2493 /** 2494 * Add the host file descriptor to the set that we are going to 2495 * pass into the host. 2496 */ 2497 FD_SET(sim_fd, hst_set_entry); 2498 } 2499 return false; 2500 }; 2501 2502 for (int i = 0; i < nfds_t; i++) { 2503 if (fds_read_ptr) { 2504 bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 2505 if (ebadf) return -EBADF; 2506 } 2507 if (fds_writ_ptr) { 2508 bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 2509 if (ebadf) return -EBADF; 2510 } 2511 if (fds_excp_ptr) { 2512 bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 2513 if (ebadf) return -EBADF; 2514 } 2515 } 2516 2517 if (time_val_ptr) { 2518 /** 2519 * It might be possible to decrement the timeval based on some 2520 * derivation of wall clock determined from elapsed simulator ticks 2521 * but that seems like overkill. Rather, we just set the timeval with 2522 * zero timeout. (There is no reason to block during the simulation 2523 * as it only decreases simulator performance.) 2524 */ 2525 tp->tv_sec = 0; 2526 tp->tv_usec = 0; 2527 2528 retval = select(nfds_h, 2529 fds_read_ptr ? &rd_h : nullptr, 2530 fds_writ_ptr ? &wr_h : nullptr, 2531 fds_excp_ptr ? &ex_h : nullptr, 2532 (timeval*)&*tp); 2533 } else { 2534 /** 2535 * If the timeval pointer is null, setup a new timeval structure to 2536 * pass into the host select call. Unfortunately, we will need to 2537 * manually check the return value and throw a retry fault if the 2538 * return value is zero. Allowing the system call to block will 2539 * likely deadlock the event queue. 2540 */ 2541 struct timeval tv = { 0, 0 }; 2542 2543 retval = select(nfds_h, 2544 fds_read_ptr ? &rd_h : nullptr, 2545 fds_writ_ptr ? &wr_h : nullptr, 2546 fds_excp_ptr ? &ex_h : nullptr, 2547 &tv); 2548 2549 if (retval == 0) { 2550 /** 2551 * If blocking indefinitely, check the signal list to see if a 2552 * signal would break the poll out of the retry cycle and try to 2553 * return the signal interrupt instead. 2554 */ 2555 for (auto sig : tc->getSystemPtr()->signalList) 2556 if (sig.receiver == p) 2557 return -EINTR; 2558 return SyscallReturn::retry(); 2559 } 2560 } 2561 2562 if (retval == -1) 2563 return -errno; 2564 2565 FD_ZERO((fd_set*)&*rd_t); 2566 FD_ZERO((fd_set*)&*wr_t); 2567 FD_ZERO((fd_set*)&*ex_t); 2568 2569 /** 2570 * We need to translate the host file descriptor set into a target file 2571 * descriptor set. This involves both our internal process fd array 2572 * and the fd_set defined in header files. 2573 */ 2574 for (int i = 0; i < nfds_h; i++) { 2575 if (fds_read_ptr) { 2576 if (FD_ISSET(i, &rd_h)) 2577 FD_SET(trans_map[i], (fd_set*)&*rd_t); 2578 } 2579 2580 if (fds_writ_ptr) { 2581 if (FD_ISSET(i, &wr_h)) 2582 FD_SET(trans_map[i], (fd_set*)&*wr_t); 2583 } 2584 2585 if (fds_excp_ptr) { 2586 if (FD_ISSET(i, &ex_h)) 2587 FD_SET(trans_map[i], (fd_set*)&*ex_t); 2588 } 2589 } 2590 2591 if (fds_read_ptr) 2592 rd_t.copyOut(tc->getMemProxy()); 2593 if (fds_writ_ptr) 2594 wr_t.copyOut(tc->getMemProxy()); 2595 if (fds_excp_ptr) 2596 ex_t.copyOut(tc->getMemProxy()); 2597 if (time_val_ptr) 2598 tp.copyOut(tc->getMemProxy()); 2599 2600 return retval; 2601} 2602 2603template <class OS> 2604SyscallReturn 2605readFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2606{ 2607 int index = 0; 2608 auto p = tc->getProcessPtr(); 2609 int tgt_fd = p->getSyscallArg(tc, index); 2610 Addr buf_ptr = p->getSyscallArg(tc, index); 2611 int nbytes = p->getSyscallArg(tc, index); 2612 2613 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2614 if (!hbfdp) 2615 return -EBADF; 2616 int sim_fd = hbfdp->getSimFD(); 2617 2618 struct pollfd pfd; 2619 pfd.fd = sim_fd; 2620 pfd.events = POLLIN | POLLPRI; 2621 if ((poll(&pfd, 1, 0) == 0) 2622 && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2623 return SyscallReturn::retry(); 2624 2625 BufferArg buf_arg(buf_ptr, nbytes); 2626 int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 2627 2628 if (bytes_read > 0) 2629 buf_arg.copyOut(tc->getMemProxy()); 2630 2631 return (bytes_read == -1) ? -errno : bytes_read; 2632} 2633 2634template <class OS> 2635SyscallReturn 2636writeFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2637{ 2638 int index = 0; 2639 auto p = tc->getProcessPtr(); 2640 int tgt_fd = p->getSyscallArg(tc, index); 2641 Addr buf_ptr = p->getSyscallArg(tc, index); 2642 int nbytes = p->getSyscallArg(tc, index); 2643 2644 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2645 if (!hbfdp) 2646 return -EBADF; 2647 int sim_fd = hbfdp->getSimFD(); 2648 2649 BufferArg buf_arg(buf_ptr, nbytes); 2650 buf_arg.copyIn(tc->getMemProxy()); 2651 2652 struct pollfd pfd; 2653 pfd.fd = sim_fd; 2654 pfd.events = POLLOUT; 2655 2656 /** 2657 * We don't want to poll on /dev/random. The kernel will not enable the 2658 * file descriptor for writing unless the entropy in the system falls 2659 * below write_wakeup_threshold. This is not guaranteed to happen 2660 * depending on host settings. 2661 */ 2662 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 2663 if (ffdp && (ffdp->getFileName() != "/dev/random")) { 2664 if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 2665 return SyscallReturn::retry(); 2666 } 2667 2668 int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 2669 2670 if (bytes_written != -1) 2671 fsync(sim_fd); 2672 2673 return (bytes_written == -1) ? -errno : bytes_written; 2674} 2675 2676template <class OS> 2677SyscallReturn 2678wait4Func(SyscallDesc *desc, int num, ThreadContext *tc) 2679{ 2680 int index = 0; 2681 auto p = tc->getProcessPtr(); 2682 pid_t pid = p->getSyscallArg(tc, index); 2683 Addr statPtr = p->getSyscallArg(tc, index); 2684 int options = p->getSyscallArg(tc, index); 2685 Addr rusagePtr = p->getSyscallArg(tc, index); 2686 2687 if (rusagePtr) 2688 DPRINTF_SYSCALL(Verbose, "wait4: rusage pointer provided %lx, however " 2689 "functionality not supported. Ignoring rusage pointer.\n", 2690 rusagePtr); 2691 2692 /** 2693 * Currently, wait4 is only implemented so that it will wait for children 2694 * exit conditions which are denoted by a SIGCHLD signals posted into the 2695 * system signal list. We return no additional information via any of the 2696 * parameters supplied to wait4. If nothing is found in the system signal 2697 * list, we will wait indefinitely for SIGCHLD to post by retrying the 2698 * call. 2699 */ 2700 System *sysh = tc->getSystemPtr(); 2701 std::list<BasicSignal>::iterator iter; 2702 for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 2703 if (iter->receiver == p) { 2704 if (pid < -1) { 2705 if ((iter->sender->pgid() == -pid) 2706 && (iter->signalValue == OS::TGT_SIGCHLD)) 2707 goto success; 2708 } else if (pid == -1) { 2709 if (iter->signalValue == OS::TGT_SIGCHLD) 2710 goto success; 2711 } else if (pid == 0) { 2712 if ((iter->sender->pgid() == p->pgid()) 2713 && (iter->signalValue == OS::TGT_SIGCHLD)) 2714 goto success; 2715 } else { 2716 if ((iter->sender->pid() == pid) 2717 && (iter->signalValue == OS::TGT_SIGCHLD)) 2718 goto success; 2719 } 2720 } 2721 } 2722 2723 return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 2724 2725success: 2726 // Set status to EXITED for WIFEXITED evaluations. 2727 const int EXITED = 0; 2728 BufferArg statusBuf(statPtr, sizeof(int)); 2729 *(int *)statusBuf.bufferPtr() = EXITED; 2730 statusBuf.copyOut(tc->getMemProxy()); 2731 2732 // Return the child PID. 2733 pid_t retval = iter->sender->pid(); 2734 sysh->signalList.erase(iter); 2735 return retval; 2736} 2737 2738template <class OS> 2739SyscallReturn 2740acceptFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2741{ 2742 struct sockaddr sa; 2743 socklen_t addrLen; 2744 int host_fd; 2745 int index = 0; 2746 auto p = tc->getProcessPtr(); 2747 int tgt_fd = p->getSyscallArg(tc, index); 2748 Addr addrPtr = p->getSyscallArg(tc, index); 2749 Addr lenPtr = p->getSyscallArg(tc, index); 2750 2751 BufferArg *lenBufPtr = nullptr; 2752 BufferArg *addrBufPtr = nullptr; 2753 2754 auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 2755 if (!sfdp) 2756 return -EBADF; 2757 int sim_fd = sfdp->getSimFD(); 2758 2759 /** 2760 * We poll the socket file descriptor first to guarantee that we do not 2761 * block on our accept call. The socket can be opened without the 2762 * non-blocking flag (it blocks). This will cause deadlocks between 2763 * communicating processes. 2764 */ 2765 struct pollfd pfd; 2766 pfd.fd = sim_fd; 2767 pfd.events = POLLIN | POLLPRI; 2768 if ((poll(&pfd, 1, 0) == 0) 2769 && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2770 return SyscallReturn::retry(); 2771 2772 if (lenPtr) { 2773 lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 2774 lenBufPtr->copyIn(tc->getMemProxy()); 2775 memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 2776 sizeof(socklen_t)); 2777 } 2778 2779 if (addrPtr) { 2780 addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 2781 addrBufPtr->copyIn(tc->getMemProxy()); 2782 memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 2783 sizeof(struct sockaddr)); 2784 } 2785 2786 host_fd = accept(sim_fd, &sa, &addrLen); 2787 2788 if (host_fd == -1) 2789 return -errno; 2790 2791 if (addrPtr) { 2792 memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 2793 addrBufPtr->copyOut(tc->getMemProxy()); 2794 delete(addrBufPtr); 2795 } 2796 2797 if (lenPtr) { 2798 *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 2799 lenBufPtr->copyOut(tc->getMemProxy()); 2800 delete(lenBufPtr); 2801 } 2802 2803 auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 2804 sfdp->_type, sfdp->_protocol); 2805 return p->fds->allocFD(afdp); 2806} 2807 2808/// Target eventfd() function. 2809template <class OS> 2810SyscallReturn 2811eventfdFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2812{ 2813#if defined(__linux__) 2814 int index = 0; 2815 auto p = tc->getProcessPtr(); 2816 unsigned initval = p->getSyscallArg(tc, index); 2817 int in_flags = p->getSyscallArg(tc, index); 2818 2819 int sim_fd = eventfd(initval, in_flags); 2820 if (sim_fd == -1) 2821 return -errno; 2822 2823 bool cloexec = in_flags & OS::TGT_O_CLOEXEC; 2824 2825 int flags = cloexec ? OS::TGT_O_CLOEXEC : 0; 2826 flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0; 2827 2828 auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec); 2829 int tgt_fd = p->fds->allocFD(hbfdp); 2830 return tgt_fd; 2831#else 2832 warnUnsupportedOS("eventfd"); 2833 return -1; 2834#endif 2835} 2836 2837#endif // __SIM_SYSCALL_EMUL_HH__ 2838