syscall_emul.hh revision 14024
1360SN/A/* 210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 410027SChris.Adeniyi-Jones@arm.com * All rights reserved 510027SChris.Adeniyi-Jones@arm.com * 610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall 710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual 810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating 910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software 1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder. You may use the software subject to the license 1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated 1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software, 1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form. 1410027SChris.Adeniyi-Jones@arm.com * 151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 16360SN/A * All rights reserved. 17360SN/A * 18360SN/A * Redistribution and use in source and binary forms, with or without 19360SN/A * modification, are permitted provided that the following conditions are 20360SN/A * met: redistributions of source code must retain the above copyright 21360SN/A * notice, this list of conditions and the following disclaimer; 22360SN/A * redistributions in binary form must reproduce the above copyright 23360SN/A * notice, this list of conditions and the following disclaimer in the 24360SN/A * documentation and/or other materials provided with the distribution; 25360SN/A * neither the name of the copyright holders nor the names of its 26360SN/A * contributors may be used to endorse or promote products derived from 27360SN/A * this software without specific prior written permission. 28360SN/A * 29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 422665Ssaidi@eecs.umich.edu * Kevin Lim 43360SN/A */ 44360SN/A 451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 461354SN/A#define __SIM_SYSCALL_EMUL_HH__ 47360SN/A 4812018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 4912018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__CYGWIN__) || \ 5012018Sandreas.sandberg@arm.com defined(__NetBSD__)) 5112018Sandreas.sandberg@arm.com#define NO_STAT64 1 5212018Sandreas.sandberg@arm.com#else 5312018Sandreas.sandberg@arm.com#define NO_STAT64 0 5412018Sandreas.sandberg@arm.com#endif 552064SN/A 5612018Sandreas.sandberg@arm.com/// 5712018Sandreas.sandberg@arm.com/// @file syscall_emul.hh 5812018Sandreas.sandberg@arm.com/// 5912018Sandreas.sandberg@arm.com/// This file defines objects used to emulate syscalls from the target 6012018Sandreas.sandberg@arm.com/// application on the host machine. 6112018Sandreas.sandberg@arm.com 6211799Sbrandon.potter@amd.com#if defined(__linux__) 6312018Sandreas.sandberg@arm.com#include <sys/eventfd.h> 6412018Sandreas.sandberg@arm.com#include <sys/statfs.h> 6512018Sandreas.sandberg@arm.com 6612018Sandreas.sandberg@arm.com#else 6712018Sandreas.sandberg@arm.com#include <sys/mount.h> 6812018Sandreas.sandberg@arm.com 6911799Sbrandon.potter@amd.com#endif 70360SN/A 71360SN/A#ifdef __CYGWIN32__ 72360SN/A#include <sys/fcntl.h> 73360SN/A 74360SN/A#endif 75360SN/A#include <fcntl.h> 761809SN/A#include <net/if.h> 7711800Sbrandon.potter@amd.com#include <poll.h> 7811392Sbrandon.potter@amd.com#include <sys/ioctl.h> 791809SN/A#include <sys/mman.h> 8011392Sbrandon.potter@amd.com#include <sys/socket.h> 8111383Sbrandon.potter@amd.com#include <sys/stat.h> 823113Sgblack@eecs.umich.edu#include <sys/time.h> 8311799Sbrandon.potter@amd.com#include <sys/types.h> 8411759Sbrandon.potter@amd.com#include <sys/uio.h> 8511812Sbaz21@cam.ac.uk#include <unistd.h> 8611812Sbaz21@cam.ac.uk 8711799Sbrandon.potter@amd.com#include <cerrno> 888229Snate@binkert.org#include <memory> 898229Snate@binkert.org#include <string> 9011594Santhony.gutierrez@amd.com 917075Snate@binkert.org#include "arch/generic/tlb.hh" 928229Snate@binkert.org#include "arch/utility.hh" 9311856Sbrandon.potter@amd.com#include "base/intmath.hh" 947075Snate@binkert.org#include "base/loader/object_file.hh" 95360SN/A#include "base/logging.hh" 9611886Sbrandon.potter@amd.com#include "base/trace.hh" 9711800Sbrandon.potter@amd.com#include "base/types.hh" 9811392Sbrandon.potter@amd.com#include "config/the_isa.hh" 992462SN/A#include "cpu/base.hh" 1001354SN/A#include "cpu/thread_context.hh" 1016216Snate@binkert.org#include "mem/page_table.hh" 1026658Snate@binkert.org#include "params/Process.hh" 1032474SN/A#include "sim/emul_driver.hh" 1042680Sktlim@umich.edu#include "sim/futex_map.hh" 1058229Snate@binkert.org#include "sim/process.hh" 10611886Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh" 10710496Ssteve.reinhardt@amd.com#include "sim/syscall_desc.hh" 10811911SBrandon.Potter@amd.com#include "sim/syscall_emul_buf.hh" 1098229Snate@binkert.org#include "sim/syscall_return.hh" 11011794Sbrandon.potter@amd.com 11111886Sbrandon.potter@amd.com#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN) 11210497Ssteve.reinhardt@amd.com#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) 11311794Sbrandon.potter@amd.com#endif 114360SN/A 115360SN/A////////////////////////////////////////////////////////////////////// 116360SN/A// 117360SN/A// The following emulation functions are generic enough that they 118360SN/A// don't need to be recompiled for different emulated OS's. They are 119360SN/A// defined in sim/syscall_emul.cc. 120360SN/A// 121360SN/A////////////////////////////////////////////////////////////////////// 122360SN/A 123360SN/Avoid warnUnsupportedOS(std::string syscall_name); 124378SN/A 1251706SN/A/// Handler for unimplemented syscalls that we haven't thought about. 12611851Sbrandon.potter@amd.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, ThreadContext *tc); 127378SN/A 128378SN/A/// Handler for unimplemented syscalls that we never intend to 129378SN/A/// implement (signal handling, etc.) and should not affect the correct 130378SN/A/// behavior of the program. Print a warning only if the appropriate 131378SN/A/// trace flag is enabled. Return success to the target program. 1321706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, ThreadContext *tc); 13311851Sbrandon.potter@amd.com 134360SN/A// Target fallocateFunc() handler. 13511760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, ThreadContext *tc); 13611760Sbrandon.potter@amd.com 13711851Sbrandon.potter@amd.com/// Target exit() handler: terminate current context. 13811760Sbrandon.potter@amd.comSyscallReturn exitFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1396109Ssanchezd@stanford.edu 1401706SN/A/// Target exit_group() handler: terminate simulation. (exit all threads) 14111851Sbrandon.potter@amd.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, ThreadContext *tc); 142378SN/A 1436109Ssanchezd@stanford.edu/// Target set_tid_address() handler. 1446109Ssanchezd@stanford.eduSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, ThreadContext *tc); 14511851Sbrandon.potter@amd.com 1466109Ssanchezd@stanford.edu/// Target getpagesize() handler. 14711886Sbrandon.potter@amd.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, ThreadContext *tc); 14811886Sbrandon.potter@amd.com 14911886Sbrandon.potter@amd.com/// Target brk() handler: set brk address. 15011886Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 151378SN/A 1521706SN/A/// Target close() handler. 15311851Sbrandon.potter@amd.comSyscallReturn closeFunc(SyscallDesc *desc, int num, ThreadContext *tc); 154378SN/A 1555748SSteve.Reinhardt@amd.com/// Target lseek() handler. 1565748SSteve.Reinhardt@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, ThreadContext *tc); 15711851Sbrandon.potter@amd.com 158378SN/A/// Target _llseek() handler. 159378SN/ASyscallReturn _llseekFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1601706SN/A 16111851Sbrandon.potter@amd.com/// Target munmap() handler. 162378SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, ThreadContext *tc); 16311886Sbrandon.potter@amd.com 1641706SN/A/// Target shutdown() handler. 16511851Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num, ThreadContext *tc); 166378SN/A 167378SN/A/// Target gethostname() handler. 1681706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 16911851Sbrandon.potter@amd.com 170378SN/A/// Target getcwd() handler. 171378SN/ASyscallReturn getcwdFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1721706SN/A 17311851Sbrandon.potter@amd.com/// Target readlink() handler. 174378SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc, 1754118Sgblack@eecs.umich.edu int index = 0); 1764118Sgblack@eecs.umich.eduSyscallReturn readlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 17711851Sbrandon.potter@amd.com 1784118Sgblack@eecs.umich.edu/// Target unlink() handler. 179378SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num, ThreadContext *tc, 1801706SN/A int index); 18111851Sbrandon.potter@amd.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 182378SN/A 183378SN/A/// Target link() handler 1841706SN/ASyscallReturn linkFunc(SyscallDesc *desc, int num, ThreadContext *tc); 18511851Sbrandon.potter@amd.com 186360SN/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); 19110203SAli.Saidi@ARM.com 19210203SAli.Saidi@ARM.com/// Target mknod() handler. 19311851Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, ThreadContext *tc); 19410203SAli.Saidi@ARM.com 1955513SMichael.Adler@intel.com/// Target chdir() handler. 19611851Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num, ThreadContext *tc); 1975513SMichael.Adler@intel.com 198511SN/A// Target rmdir() handler. 19910633Smichaelupton@gmail.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num, ThreadContext *tc); 20011851Sbrandon.potter@amd.com 20110633Smichaelupton@gmail.com/// Target rename() handler. 2021706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 20311851Sbrandon.potter@amd.com 204511SN/A 2055513SMichael.Adler@intel.com/// Target truncate() handler. 2065513SMichael.Adler@intel.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, ThreadContext *tc); 20711851Sbrandon.potter@amd.com 2085513SMichael.Adler@intel.com 209511SN/A/// Target ftruncate() handler. 2101706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, ThreadContext *tc); 21111851Sbrandon.potter@amd.com 2121706SN/A 2131706SN/A/// Target truncate64() handler. 2141706SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num, ThreadContext *tc); 2151706SN/A 21611851Sbrandon.potter@amd.com/// Target ftruncate64() handler. 2171706SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num, ThreadContext *tc); 2181706SN/A 2191706SN/A 2201706SN/A/// Target umask() handler. 22111851Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2221706SN/A 223511SN/A/// Target gettid() handler. 2246703Svince@csl.cornell.eduSyscallReturn gettidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2256703Svince@csl.cornell.edu 22611851Sbrandon.potter@amd.com/// Target chown() handler. 2276703Svince@csl.cornell.eduSyscallReturn chownFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2286685Stjones1@inf.ed.ac.uk 2296685Stjones1@inf.ed.ac.uk/// Target setpgid() handler. 23011851Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2316685Stjones1@inf.ed.ac.uk 2326685Stjones1@inf.ed.ac.uk/// Target fchown() handler. 2335513SMichael.Adler@intel.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2345513SMichael.Adler@intel.com 23511851Sbrandon.potter@amd.com/// Target dup() handler. 2365513SMichael.Adler@intel.comSyscallReturn dupFunc(SyscallDesc *desc, int num, ThreadContext *tc); 23711885Sbrandon.potter@amd.com 23811885Sbrandon.potter@amd.com/// Target dup2() handler. 23911885Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, ThreadContext *tc); 2405513SMichael.Adler@intel.com 2411999SN/A/// Target fcntl() handler. 2421999SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, ThreadContext *tc); 24311851Sbrandon.potter@amd.com 2441999SN/A/// Target fcntl64() handler. 24511885Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, ThreadContext *tc); 24611885Sbrandon.potter@amd.com 24711885Sbrandon.potter@amd.com/// Target setuid() handler. 2481999SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2491999SN/A 2501999SN/A/// Target pipe() handler. 25111851Sbrandon.potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2521999SN/A 2533079Sstever@eecs.umich.edu/// Internal pipe() handler. 2543079Sstever@eecs.umich.eduSyscallReturn pipeImpl(SyscallDesc *desc, int num, ThreadContext *tc, 25511851Sbrandon.potter@amd.com bool pseudoPipe); 2563079Sstever@eecs.umich.edu 25711908SBrandon.Potter@amd.com/// Target getpid() handler. 25811908SBrandon.Potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 25911908SBrandon.Potter@amd.com 26011908SBrandon.Potter@amd.com// Target getpeername() handler. 26111875Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2622093SN/A 26311851Sbrandon.potter@amd.com// Target bind() handler. 2642093SN/ASyscallReturn bindFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2652687Sksewell@umich.edu 2662687Sksewell@umich.edu// Target listen() handler. 26711851Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2682687Sksewell@umich.edu 2692238SN/A// Target connect() handler. 2702238SN/ASyscallReturn connectFunc(SyscallDesc *desc, int num, ThreadContext *tc); 27111851Sbrandon.potter@amd.com 2722238SN/A#if defined(SYS_getdents) 27311908SBrandon.Potter@amd.com// Target getdents() handler. 27411908SBrandon.Potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num, ThreadContext *tc); 27511908SBrandon.Potter@amd.com#endif 27611908SBrandon.Potter@amd.com 27711908SBrandon.Potter@amd.com#if defined(SYS_getdents64) 27811908SBrandon.Potter@amd.com// Target getdents() handler. 27911908SBrandon.Potter@amd.comSyscallReturn getdents64Func(SyscallDesc *desc, int num, ThreadContext *tc); 28011908SBrandon.Potter@amd.com#endif 2812238SN/A 2822238SN/A// Target sendto() handler. 28311851Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2842238SN/A 2852238SN/A// Target recvfrom() handler. 2862238SN/ASyscallReturn recvfromFunc(SyscallDesc *desc, int num, ThreadContext *tc); 28711851Sbrandon.potter@amd.com 2882238SN/A// Target recvmsg() handler. 2892238SN/ASyscallReturn recvmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2902238SN/A 29111851Sbrandon.potter@amd.com// Target sendmsg() handler. 2922238SN/ASyscallReturn sendmsgFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2932238SN/A 2942238SN/A// Target getuid() handler. 29511851Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 2962238SN/A 2972238SN/A/// Target getgid() handler. 2982238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 29911851Sbrandon.potter@amd.com 3002238SN/A/// Target getppid() handler. 3012238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3022238SN/A 30311851Sbrandon.potter@amd.com/// Target geteuid() handler. 3042238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3059455Smitch.hayenga+gem5@gmail.com 3069455Smitch.hayenga+gem5@gmail.com/// Target getegid() handler. 30711851Sbrandon.potter@amd.comSyscallReturn getegidFunc(SyscallDesc *desc, int num, ThreadContext *tc); 30810203SAli.Saidi@ARM.com 30911851Sbrandon.potter@amd.com/// Target access() handler 31011851Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3119455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, ThreadContext *tc, 3129112Smarc.orr@gmail.com int index); 31311906SBrandon.Potter@amd.com 31411906SBrandon.Potter@amd.com// Target getsockopt() handler. 3159112Smarc.orr@gmail.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3169112Smarc.orr@gmail.com 31711851Sbrandon.potter@amd.com// Target setsockopt() handler. 3189112Smarc.orr@gmail.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num, ThreadContext *tc); 3199112Smarc.orr@gmail.com 32011911SBrandon.Potter@amd.com// Target getsockname() handler. 3219112Smarc.orr@gmail.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num, ThreadContext *tc); 32211911SBrandon.Potter@amd.com 32311911SBrandon.Potter@amd.com/// Futex system call 32411911SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez 32511911SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps 3269112Smarc.orr@gmail.comtemplate <class OS> 32711911SBrandon.Potter@amd.comSyscallReturn 32811911SBrandon.Potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 32911911SBrandon.Potter@amd.com{ 33011911SBrandon.Potter@amd.com using namespace std; 3319238Slluc.alvarez@bsc.es 3329112Smarc.orr@gmail.com int index = 0; 33311911SBrandon.Potter@amd.com auto process = tc->getProcessPtr(); 3349112Smarc.orr@gmail.com 33511911SBrandon.Potter@amd.com Addr uaddr = process->getSyscallArg(tc, index); 33611911SBrandon.Potter@amd.com int op = process->getSyscallArg(tc, index); 33711911SBrandon.Potter@amd.com int val = process->getSyscallArg(tc, index); 33811911SBrandon.Potter@amd.com int timeout M5_VAR_USED = process->getSyscallArg(tc, index); 33911911SBrandon.Potter@amd.com Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index); 3409112Smarc.orr@gmail.com int val3 = process->getSyscallArg(tc, index); 34111911SBrandon.Potter@amd.com 34211911SBrandon.Potter@amd.com /* 34311911SBrandon.Potter@amd.com * Unsupported option that does not affect the correctness of the 34411911SBrandon.Potter@amd.com * application. This is a performance optimization utilized by Linux. 34511911SBrandon.Potter@amd.com */ 34611911SBrandon.Potter@amd.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3479112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG; 3489112Smarc.orr@gmail.com 34911911SBrandon.Potter@amd.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 35011911SBrandon.Potter@amd.com 3519112Smarc.orr@gmail.com if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) { 35211911SBrandon.Potter@amd.com // Ensure futex system call accessed atomically. 35311911SBrandon.Potter@amd.com BufferArg buf(uaddr, sizeof(int)); 3549112Smarc.orr@gmail.com buf.copyIn(tc->getVirtProxy()); 3559112Smarc.orr@gmail.com int mem_val = *(int*)buf.bufferPtr(); 35611911SBrandon.Potter@amd.com 35711911SBrandon.Potter@amd.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 3602238SN/A * invoked). In this case, we need to throw an error. 3612238SN/A */ 3622238SN/A if (val != mem_val) 3632238SN/A return -OS::TGT_EWOULDBLOCK; 36411851Sbrandon.potter@amd.com 3652238SN/A if (OS::TGT_FUTEX_WAIT) { 3662238SN/A futex_map.suspend(uaddr, process->tgid(), tc); 3672238SN/A } else { 36811851Sbrandon.potter@amd.com 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 3791354SN/A // Ensure futex system call accessed atomically. 3801354SN/A BufferArg buf(uaddr, sizeof(int)); 38110796Sbrandon.potter@amd.com buf.copyIn(tc->getVirtProxy()); 38210796Sbrandon.potter@amd.com int mem_val = *(int*)buf.bufferPtr(); 3831354SN/A /* 3841354SN/A * For CMP_REQUEUE, the whole operation is only started only if 3851354SN/A * val3 is still the value of the futex pointed to by uaddr. 3861354SN/A */ 3871354SN/A if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val) 3881354SN/A return -OS::TGT_EWOULDBLOCK; 3891354SN/A return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2); 3901354SN/A } else if (OS::TGT_FUTEX_WAKE_OP == op) { 3911354SN/A /* 3921354SN/A * The FUTEX_WAKE_OP operation is equivalent to executing the 39310796Sbrandon.potter@amd.com * following code atomically and totally ordered with respect to 3941354SN/A * other futex operations on any of the two supplied futex words: 39510796Sbrandon.potter@amd.com * 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) 40010796Sbrandon.potter@amd.com * futex(addr2, FUTEX_WAKE, val2, 0, 0, 0); 40110796Sbrandon.potter@amd.com * 40210796Sbrandon.potter@amd.com * (op, oparg, cmp, cmparg are encoded in val3) 40310796Sbrandon.potter@amd.com * 40410796Sbrandon.potter@amd.com * +---+---+-----------+-----------+ 40510796Sbrandon.potter@amd.com * |op |cmp| oparg | cmparg | 40610796Sbrandon.potter@amd.com * +---+---+-----------+-----------+ 40710796Sbrandon.potter@amd.com * 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 * 411360SN/A */ 412360SN/A // get value from simulated-space 413360SN/A BufferArg buf(uaddr2, sizeof(int)); 414360SN/A buf.copyIn(tc->getVirtProxy()); 415360SN/A int oldval = *(int*)buf.bufferPtr(); 416360SN/A int newval = oldval; 417360SN/A // extract op, oparg, cmp, cmparg from val3 41811759Sbrandon.potter@amd.com int wake_cmparg = val3 & 0xfff; 4193113Sgblack@eecs.umich.edu int wake_oparg = (val3 & 0xfff000) >> 12; 4203113Sgblack@eecs.umich.edu int wake_cmp = (val3 & 0xf000000) >> 24; 4213113Sgblack@eecs.umich.edu int wake_op = (val3 & 0xf0000000) >> 28; 4223113Sgblack@eecs.umich.edu if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1) 4233113Sgblack@eecs.umich.edu wake_oparg = (1 << wake_oparg); 4243113Sgblack@eecs.umich.edu wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT; 4253113Sgblack@eecs.umich.edu // perform operation on the value of the second futex 4263113Sgblack@eecs.umich.edu 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; 43212032Sandreas.sandberg@arm.com 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) 4354189Sgblack@eecs.umich.edu newval ^= wake_oparg; 4364189Sgblack@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->getVirtProxy()); 4393113Sgblack@eecs.umich.edu // perform the first wake-up 4403113Sgblack@eecs.umich.edu int woken1 = futex_map.wakeup(uaddr, process->tgid(), val); 4418737Skoansin.tan@gmail.com int woken2 = 0; 4423113Sgblack@eecs.umich.edu // calculate the condition of the second wake-up 4438737Skoansin.tan@gmail.com bool is_wake2 = false; 4443277Sgblack@eecs.umich.edu if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ) 4455515SMichael.Adler@intel.com is_wake2 = oldval == wake_cmparg; 4465515SMichael.Adler@intel.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE) 4475515SMichael.Adler@intel.com is_wake2 = oldval != wake_cmparg; 4485515SMichael.Adler@intel.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT) 4495515SMichael.Adler@intel.com is_wake2 = oldval < wake_cmparg; 4508737Skoansin.tan@gmail.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE) 4513277Sgblack@eecs.umich.edu is_wake2 = oldval <= wake_cmparg; 4528737Skoansin.tan@gmail.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT) 4533277Sgblack@eecs.umich.edu is_wake2 = oldval > wake_cmparg; 4548737Skoansin.tan@gmail.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE) 4553277Sgblack@eecs.umich.edu is_wake2 = oldval >= wake_cmparg; 4568737Skoansin.tan@gmail.com // perform the second wake-up 4573113Sgblack@eecs.umich.edu if (is_wake2) 4583113Sgblack@eecs.umich.edu woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout); 4593113Sgblack@eecs.umich.edu 4603113Sgblack@eecs.umich.edu return woken1 + woken2; 4618737Skoansin.tan@gmail.com } 4623113Sgblack@eecs.umich.edu warn("futex: op %d not implemented; ignoring.", op); 4638737Skoansin.tan@gmail.com return -ENOSYS; 4643114Sgblack@eecs.umich.edu} 4658737Skoansin.tan@gmail.com 4663114Sgblack@eecs.umich.edu 4678737Skoansin.tan@gmail.com/// Pseudo Funcs - These functions use a different return convension, 4683114Sgblack@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); 47011906SBrandon.Potter@amd.com 4714061Sgblack@eecs.umich.edu/// Target getpidPseudo() handler. 4724061Sgblack@eecs.umich.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4738737Skoansin.tan@gmail.com 4743113Sgblack@eecs.umich.edu/// Target getuidPseudo() handler. 4758737Skoansin.tan@gmail.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4763113Sgblack@eecs.umich.edu 4773113Sgblack@eecs.umich.edu/// Target getgidPseudo() handler. 4783113Sgblack@eecs.umich.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, ThreadContext *tc); 4793113Sgblack@eecs.umich.edu 4803113Sgblack@eecs.umich.edu 48112032Sandreas.sandberg@arm.com/// A readable name for 1,000,000, for converting microseconds to seconds. 4823113Sgblack@eecs.umich.educonst int one_million = 1000000; 4833113Sgblack@eecs.umich.edu/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 4844189Sgblack@eecs.umich.educonst int one_billion = 1000000000; 4854189Sgblack@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. 4898737Skoansin.tan@gmail.comconst unsigned seconds_since_epoch = 1000000000; 4903113Sgblack@eecs.umich.edu 4918737Skoansin.tan@gmail.com/// Helper function to convert current elapsed time to seconds and 4923113Sgblack@eecs.umich.edu/// microseconds. 4938737Skoansin.tan@gmail.comtemplate <class T1, class T2> 4943113Sgblack@eecs.umich.eduvoid 4953113Sgblack@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec) 4963113Sgblack@eecs.umich.edu{ 4973113Sgblack@eecs.umich.edu uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 4983113Sgblack@eecs.umich.edu sec = elapsed_usecs / one_million; 4993113Sgblack@eecs.umich.edu usec = elapsed_usecs % one_million; 5003113Sgblack@eecs.umich.edu} 50111906SBrandon.Potter@amd.com 5023113Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 50312032Sandreas.sandberg@arm.com/// nanoseconds. 5048852Sandreas.hansson@arm.comtemplate <class T1, class T2> 50511906SBrandon.Potter@amd.comvoid 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} 5123113Sgblack@eecs.umich.edu 5133113Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////// 51412032Sandreas.sandberg@arm.com// 5158852Sandreas.hansson@arm.com// The following emulation functions are generic, but need to be 51611906SBrandon.Potter@amd.com// templated to account for differences in types, constants, etc. 5173113Sgblack@eecs.umich.edu// 5183113Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////// 5193113Sgblack@eecs.umich.edu 5206686Stjones1@inf.ed.ac.uk typedef struct statfs hst_statfs; 5213113Sgblack@eecs.umich.edu#if NO_STAT64 5223113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 5233113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 52411759Sbrandon.potter@amd.com#else 52512032Sandreas.sandberg@arm.com typedef struct stat hst_stat; 52611759Sbrandon.potter@amd.com typedef struct stat64 hst_stat64; 52711759Sbrandon.potter@amd.com#endif 52811759Sbrandon.potter@amd.com 52911759Sbrandon.potter@amd.com//// Helper function to convert a host stat buffer to a target stat 53011759Sbrandon.potter@amd.com//// buffer. Also copies the target buffer out to the simulated 53111812Sbaz21@cam.ac.uk//// memory space. Used by stat(), fstat(), and lstat(). 53211812Sbaz21@cam.ac.uk 53311812Sbaz21@cam.ac.uktemplate <typename target_stat, typename host_stat> 53411759Sbrandon.potter@amd.comvoid 53511812Sbaz21@cam.ac.ukconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 53611759Sbrandon.potter@amd.com{ 53711759Sbrandon.potter@amd.com using namespace TheISA; 53811759Sbrandon.potter@amd.com 53911759Sbrandon.potter@amd.com if (fakeTTY) 54011759Sbrandon.potter@amd.com tgt->st_dev = 0xA; 54111759Sbrandon.potter@amd.com else 54211759Sbrandon.potter@amd.com tgt->st_dev = host->st_dev; 54311812Sbaz21@cam.ac.uk tgt->st_dev = TheISA::htog(tgt->st_dev); 54411812Sbaz21@cam.ac.uk tgt->st_ino = host->st_ino; 54511812Sbaz21@cam.ac.uk tgt->st_ino = TheISA::htog(tgt->st_ino); 54611812Sbaz21@cam.ac.uk tgt->st_mode = host->st_mode; 54711812Sbaz21@cam.ac.uk if (fakeTTY) { 54811812Sbaz21@cam.ac.uk // Claim to be a character device 54911812Sbaz21@cam.ac.uk tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 55011759Sbrandon.potter@amd.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 55111759Sbrandon.potter@amd.com } 55211812Sbaz21@cam.ac.uk tgt->st_mode = TheISA::htog(tgt->st_mode); 55311812Sbaz21@cam.ac.uk tgt->st_nlink = host->st_nlink; 55411759Sbrandon.potter@amd.com 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; 55811812Sbaz21@cam.ac.uk tgt->st_gid = TheISA::htog(tgt->st_gid); 55911812Sbaz21@cam.ac.uk 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; 56311759Sbrandon.potter@amd.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 56411759Sbrandon.potter@amd.com tgt->st_size = host->st_size; 56511759Sbrandon.potter@amd.com tgt->st_size = TheISA::htog(tgt->st_size); 566378SN/A tgt->st_atimeX = host->st_atime; 567378SN/A tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 5689141Smarc.orr@gmail.com tgt->st_mtimeX = host->st_mtime; 5699141Smarc.orr@gmail.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 570360SN/A tgt->st_ctimeX = host->st_ctime; 5711450SN/A tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 57211856Sbrandon.potter@amd.com // Force the block size to be 8KB. This helps to ensure buffered io works 573360SN/A // consistently across different hosts. 5746701Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 57511856Sbrandon.potter@amd.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 57611856Sbrandon.potter@amd.com tgt->st_blocks = host->st_blocks; 577360SN/A tgt->st_blocks = TheISA::htog(tgt->st_blocks); 57810930Sbrandon.potter@amd.com} 579360SN/A 58011856Sbrandon.potter@amd.com// Same for stat64 58111856Sbrandon.potter@amd.com 58210496Ssteve.reinhardt@amd.comtemplate <typename target_stat, typename host_stat64> 58311856Sbrandon.potter@amd.comvoid 58411856Sbrandon.potter@amd.comconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 5851458SN/A{ 586360SN/A using namespace TheISA; 58711856Sbrandon.potter@amd.com 58811856Sbrandon.potter@amd.com convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 58911856Sbrandon.potter@amd.com#if defined(STAT_HAVE_NSEC) 59011856Sbrandon.potter@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; 59311856Sbrandon.potter@amd.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 59411856Sbrandon.potter@amd.com tgt->st_ctime_nsec = host->st_ctime_nsec; 59510496Ssteve.reinhardt@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 60110930Sbrandon.potter@amd.com} 6029141Smarc.orr@gmail.com 603360SN/A// Here are a couple of convenience functions 604360SN/Atemplate<class OS> 605360SN/Avoid 60611907SBrandon.Potter@amd.comcopyOutStatBuf(PortProxy &mem, Addr addr, 60711907SBrandon.Potter@amd.com hst_stat *host, bool fakeTTY = false) 60811907SBrandon.Potter@amd.com{ 609360SN/A typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 61011907SBrandon.Potter@amd.com tgt_stat_buf tgt(addr); 61111907SBrandon.Potter@amd.com convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 61211907SBrandon.Potter@amd.com tgt.copyOut(mem); 61311907SBrandon.Potter@amd.com} 61411907SBrandon.Potter@amd.com 61511907SBrandon.Potter@amd.comtemplate<class OS> 61611907SBrandon.Potter@amd.comvoid 61711907SBrandon.Potter@amd.comcopyOutStat64Buf(PortProxy &mem, Addr addr, 61811907SBrandon.Potter@amd.com hst_stat64 *host, bool fakeTTY = false) 61911907SBrandon.Potter@amd.com{ 62011907SBrandon.Potter@amd.com typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 62111907SBrandon.Potter@amd.com tgt_stat_buf tgt(addr); 62211907SBrandon.Potter@amd.com convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 62311907SBrandon.Potter@amd.com tgt.copyOut(mem); 624360SN/A} 62511907SBrandon.Potter@amd.com 6261458SN/Atemplate <class OS> 627360SN/Avoid 62811907SBrandon.Potter@amd.comcopyOutStatfsBuf(PortProxy &mem, Addr addr, 62911907SBrandon.Potter@amd.com hst_statfs *host) 63011907SBrandon.Potter@amd.com{ 63111907SBrandon.Potter@amd.com TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 63211907SBrandon.Potter@amd.com 63311907SBrandon.Potter@amd.com tgt->f_type = TheISA::htog(host->f_type); 63411907SBrandon.Potter@amd.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 63511907SBrandon.Potter@amd.com tgt->f_bsize = TheISA::htog(host->f_iosize); 63611907SBrandon.Potter@amd.com#else 63711907SBrandon.Potter@amd.com tgt->f_bsize = TheISA::htog(host->f_bsize); 638360SN/A#endif 63911907SBrandon.Potter@amd.com tgt->f_blocks = TheISA::htog(host->f_blocks); 64011907SBrandon.Potter@amd.com tgt->f_bfree = TheISA::htog(host->f_bfree); 64111907SBrandon.Potter@amd.com 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); 64411907SBrandon.Potter@amd.com memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 64511907SBrandon.Potter@amd.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 64611907SBrandon.Potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namemax); 64711907SBrandon.Potter@amd.com tgt->f_frsize = TheISA::htog(host->f_bsize); 648360SN/A#elif defined(__APPLE__) 64911907SBrandon.Potter@amd.com tgt->f_namelen = 0; 650360SN/A tgt->f_frsize = 0; 651360SN/A#else 65211907SBrandon.Potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 6533669Sbinkertn@umich.edu tgt->f_frsize = TheISA::htog(host->f_frsize); 65411907SBrandon.Potter@amd.com#endif 65511907SBrandon.Potter@amd.com#if defined(__linux__) 65611907SBrandon.Potter@amd.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 65711907SBrandon.Potter@amd.com#else 65811907SBrandon.Potter@amd.com /* 65911907SBrandon.Potter@amd.com * The fields are different sizes per OS. Don't bother with 66011907SBrandon.Potter@amd.com * f_spare or f_reserved on non-Linux for now. 66111907SBrandon.Potter@amd.com */ 66211907SBrandon.Potter@amd.com memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 66311907SBrandon.Potter@amd.com#endif 66411907SBrandon.Potter@amd.com 66511907SBrandon.Potter@amd.com tgt.copyOut(mem); 66611907SBrandon.Potter@amd.com} 66711907SBrandon.Potter@amd.com 66811907SBrandon.Potter@amd.com/// Target ioctl() handler. For the most part, programs call ioctl() 66911907SBrandon.Potter@amd.com/// only to find out if their stdout is a tty, to determine whether to 67011907SBrandon.Potter@amd.com/// do line or block buffering. We always claim that output fds are 67111907SBrandon.Potter@amd.com/// not TTYs to provide repeatable results. 67211907SBrandon.Potter@amd.comtemplate <class OS> 67311907SBrandon.Potter@amd.comSyscallReturn 67411907SBrandon.Potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 6751706SN/A{ 67611907SBrandon.Potter@amd.com int index = 0; 67711907SBrandon.Potter@amd.com auto p = tc->getProcessPtr(); 67811907SBrandon.Potter@amd.com 67911907SBrandon.Potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 68011907SBrandon.Potter@amd.com unsigned req = p->getSyscallArg(tc, index); 68111907SBrandon.Potter@amd.com 68210496Ssteve.reinhardt@amd.com DPRINTF_SYSCALL(Verbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 68310496Ssteve.reinhardt@amd.com 68411907SBrandon.Potter@amd.com if (OS::isTtyReq(req)) 68511907SBrandon.Potter@amd.com return -ENOTTY; 68611907SBrandon.Potter@amd.com 68711907SBrandon.Potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 68811907SBrandon.Potter@amd.com if (dfdp) { 68911907SBrandon.Potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 69010496Ssteve.reinhardt@amd.com if (emul_driver) 69111907SBrandon.Potter@amd.com return emul_driver->ioctl(tc, req); 69211907SBrandon.Potter@amd.com } 69311907SBrandon.Potter@amd.com 69411907SBrandon.Potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 69510496Ssteve.reinhardt@amd.com if (sfdp) { 69610496Ssteve.reinhardt@amd.com int status; 69711907SBrandon.Potter@amd.com 69811907SBrandon.Potter@amd.com switch (req) { 69911907SBrandon.Potter@amd.com case SIOCGIFCONF: { 70011907SBrandon.Potter@amd.com Addr conf_addr = p->getSyscallArg(tc, index); 70111907SBrandon.Potter@amd.com BufferArg conf_arg(conf_addr, sizeof(ifconf)); 70211907SBrandon.Potter@amd.com conf_arg.copyIn(tc->getVirtProxy()); 70311907SBrandon.Potter@amd.com 70411907SBrandon.Potter@amd.com ifconf *conf = (ifconf*)conf_arg.bufferPtr(); 70511907SBrandon.Potter@amd.com Addr ifc_buf_addr = (Addr)conf->ifc_buf; 70611907SBrandon.Potter@amd.com BufferArg ifc_buf_arg(ifc_buf_addr, conf->ifc_len); 70711907SBrandon.Potter@amd.com ifc_buf_arg.copyIn(tc->getVirtProxy()); 70811907SBrandon.Potter@amd.com 70911907SBrandon.Potter@amd.com conf->ifc_buf = (char*)ifc_buf_arg.bufferPtr(); 71011907SBrandon.Potter@amd.com 71111907SBrandon.Potter@amd.com status = ioctl(sfdp->getSimFD(), req, conf_arg.bufferPtr()); 71211907SBrandon.Potter@amd.com if (status != -1) { 71311907SBrandon.Potter@amd.com conf->ifc_buf = (char*)ifc_buf_addr; 71411907SBrandon.Potter@amd.com ifc_buf_arg.copyOut(tc->getVirtProxy()); 71511907SBrandon.Potter@amd.com conf_arg.copyOut(tc->getVirtProxy()); 71611907SBrandon.Potter@amd.com } 71711907SBrandon.Potter@amd.com 71811907SBrandon.Potter@amd.com return status; 71911907SBrandon.Potter@amd.com } 72011907SBrandon.Potter@amd.com case SIOCGIFFLAGS: 72111907SBrandon.Potter@amd.com#if defined(__linux__) 722360SN/A case SIOCGIFINDEX: 72311907SBrandon.Potter@amd.com#endif 72411907SBrandon.Potter@amd.com case SIOCGIFNETMASK: 72511907SBrandon.Potter@amd.com case SIOCGIFADDR: 72611907SBrandon.Potter@amd.com#if defined(__linux__) 72711907SBrandon.Potter@amd.com case SIOCGIFHWADDR: 72811907SBrandon.Potter@amd.com#endif 72911907SBrandon.Potter@amd.com case SIOCGIFMTU: { 73011907SBrandon.Potter@amd.com Addr req_addr = p->getSyscallArg(tc, index); 73111907SBrandon.Potter@amd.com BufferArg req_arg(req_addr, sizeof(ifreq)); 73211907SBrandon.Potter@amd.com req_arg.copyIn(tc->getVirtProxy()); 73311907SBrandon.Potter@amd.com 73411907SBrandon.Potter@amd.com status = ioctl(sfdp->getSimFD(), req, req_arg.bufferPtr()); 73511907SBrandon.Potter@amd.com if (status != -1) 736360SN/A req_arg.copyOut(tc->getVirtProxy()); 737360SN/A return status; 73810027SChris.Adeniyi-Jones@arm.com } 73910027SChris.Adeniyi-Jones@arm.com } 74010027SChris.Adeniyi-Jones@arm.com } 74111851Sbrandon.potter@amd.com 74210027SChris.Adeniyi-Jones@arm.com /** 74310027SChris.Adeniyi-Jones@arm.com * For lack of a better return code, return ENOTTY. Ideally, we should 74411907SBrandon.Potter@amd.com * return something better here, but at least we issue the warning. 74510027SChris.Adeniyi-Jones@arm.com */ 74610027SChris.Adeniyi-Jones@arm.com warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 74710027SChris.Adeniyi-Jones@arm.com tgt_fd, req, tc->pcState()); 74810027SChris.Adeniyi-Jones@arm.com return -ENOTTY; 74910027SChris.Adeniyi-Jones@arm.com} 75011851Sbrandon.potter@amd.com 75111851Sbrandon.potter@amd.comtemplate <class OS> 75210027SChris.Adeniyi-Jones@arm.comSyscallReturn 75311907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, ThreadContext *tc, bool isopenat) 75410027SChris.Adeniyi-Jones@arm.com{ 75510027SChris.Adeniyi-Jones@arm.com int index = 0; 75610633Smichaelupton@gmail.com auto p = tc->getProcessPtr(); 75710633Smichaelupton@gmail.com int tgt_dirfd = -1; 75810633Smichaelupton@gmail.com 75911851Sbrandon.potter@amd.com /** 76010633Smichaelupton@gmail.com * If using the openat variant, read in the target directory file 76110633Smichaelupton@gmail.com * descriptor from the simulated process. 76210633Smichaelupton@gmail.com */ 76310633Smichaelupton@gmail.com if (isopenat) 76410633Smichaelupton@gmail.com tgt_dirfd = p->getSyscallArg(tc, index); 76510633Smichaelupton@gmail.com 76610633Smichaelupton@gmail.com /** 76710633Smichaelupton@gmail.com * Retrieve the simulated process' memory proxy and then read in the path 76810633Smichaelupton@gmail.com * string from that memory space into the host's working memory space. 76910633Smichaelupton@gmail.com */ 77010203SAli.Saidi@ARM.com std::string path; 77110203SAli.Saidi@ARM.com if (!tc->getVirtProxy().tryReadString(path, p->getSyscallArg(tc, index))) 77210203SAli.Saidi@ARM.com return -EFAULT; 77311851Sbrandon.potter@amd.com 77411851Sbrandon.potter@amd.com#ifdef __CYGWIN32__ 77510203SAli.Saidi@ARM.com int host_flags = O_BINARY; 77610203SAli.Saidi@ARM.com#else 77710203SAli.Saidi@ARM.com int host_flags = 0; 77810203SAli.Saidi@ARM.com#endif 77910203SAli.Saidi@ARM.com /** 78010203SAli.Saidi@ARM.com * Translate target flags into host flags. Flags exist which are not 78110203SAli.Saidi@ARM.com * ported between architectures which can cause check failures. 78210203SAli.Saidi@ARM.com */ 78310203SAli.Saidi@ARM.com int tgt_flags = p->getSyscallArg(tc, index); 78410203SAli.Saidi@ARM.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 78510203SAli.Saidi@ARM.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 78611851Sbrandon.potter@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 78711851Sbrandon.potter@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 78810203SAli.Saidi@ARM.com } 78910203SAli.Saidi@ARM.com } 79010203SAli.Saidi@ARM.com if (tgt_flags) { 79110203SAli.Saidi@ARM.com warn("open%s: cannot decode flags 0x%x", 79210203SAli.Saidi@ARM.com isopenat ? "at" : "", tgt_flags); 79310203SAli.Saidi@ARM.com } 79410203SAli.Saidi@ARM.com#ifdef __CYGWIN32__ 79510203SAli.Saidi@ARM.com host_flags |= O_BINARY; 79610850SGiacomo.Gabrielli@arm.com#endif 79710850SGiacomo.Gabrielli@arm.com 79810850SGiacomo.Gabrielli@arm.com int mode = p->getSyscallArg(tc, index); 79911851Sbrandon.potter@amd.com 80010850SGiacomo.Gabrielli@arm.com /** 80110850SGiacomo.Gabrielli@arm.com * If the simulated process called open or openat with AT_FDCWD specified, 80210850SGiacomo.Gabrielli@arm.com * take the current working directory value which was passed into the 80310850SGiacomo.Gabrielli@arm.com * process class as a Python parameter and append the current path to 80410850SGiacomo.Gabrielli@arm.com * create a full path. 80510850SGiacomo.Gabrielli@arm.com * Otherwise, openat with a valid target directory file descriptor has 80610850SGiacomo.Gabrielli@arm.com * been called. If the path option, which was passed in as a parameter, 80710850SGiacomo.Gabrielli@arm.com * is not absolute, retrieve the directory file descriptor's path and 80810850SGiacomo.Gabrielli@arm.com * prepend it to the path passed in as a parameter. 80910850SGiacomo.Gabrielli@arm.com * In every case, we should have a full path (which is relevant to the 81010850SGiacomo.Gabrielli@arm.com * host) to work with after this block has been passed. 81110850SGiacomo.Gabrielli@arm.com */ 81210850SGiacomo.Gabrielli@arm.com std::string redir_path = path; 81310850SGiacomo.Gabrielli@arm.com std::string abs_path = path; 81410850SGiacomo.Gabrielli@arm.com if (!isopenat || tgt_dirfd == OS::TGT_AT_FDCWD) { 81510850SGiacomo.Gabrielli@arm.com abs_path = p->absolutePath(path, true); 81610850SGiacomo.Gabrielli@arm.com redir_path = p->checkPathRedirect(path); 81710850SGiacomo.Gabrielli@arm.com } else if (!startswith(path, "/")) { 81810850SGiacomo.Gabrielli@arm.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 81910850SGiacomo.Gabrielli@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 82010850SGiacomo.Gabrielli@arm.com if (!ffdp) 82110850SGiacomo.Gabrielli@arm.com return -EBADF; 82210850SGiacomo.Gabrielli@arm.com abs_path = ffdp->getFileName() + path; 82310850SGiacomo.Gabrielli@arm.com redir_path = p->checkPathRedirect(abs_path); 82410850SGiacomo.Gabrielli@arm.com } 82510850SGiacomo.Gabrielli@arm.com 82610850SGiacomo.Gabrielli@arm.com /** 82710850SGiacomo.Gabrielli@arm.com * Since this is an emulated environment, we create pseudo file 82810850SGiacomo.Gabrielli@arm.com * descriptors for device requests that have been registered with 82910850SGiacomo.Gabrielli@arm.com * the process class through Python; this allows us to create a file 83010850SGiacomo.Gabrielli@arm.com * descriptor for subsequent ioctl or mmap calls. 83110850SGiacomo.Gabrielli@arm.com */ 8326640Svince@csl.cornell.edu if (startswith(abs_path, "/dev/")) { 8336640Svince@csl.cornell.edu std::string filename = abs_path.substr(strlen("/dev/")); 8346640Svince@csl.cornell.edu EmulatedDriver *drv = p->findDriver(filename); 83511851Sbrandon.potter@amd.com if (drv) { 83611851Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 8376640Svince@csl.cornell.edu "driver open with path[%s]\n", 8386640Svince@csl.cornell.edu isopenat ? "at" : "", abs_path.c_str()); 8396701Sgblack@eecs.umich.edu return drv->open(tc, mode, host_flags); 8406701Sgblack@eecs.umich.edu } 84110793Sbrandon.potter@amd.com /** 8426640Svince@csl.cornell.edu * Fall through here for pass through to host devices, such 84311758Sbrandon.potter@amd.com * as /dev/zero 84411758Sbrandon.potter@amd.com */ 84511758Sbrandon.potter@amd.com } 8466640Svince@csl.cornell.edu 8478706Sandreas.hansson@arm.com /** 8486640Svince@csl.cornell.edu * We make several attempts resolve a call to open. 8496701Sgblack@eecs.umich.edu * 8506640Svince@csl.cornell.edu * 1) Resolve any path redirection before hand. This will set the path 851360SN/A * up with variable 'redir_path' which may contain a modified path or 8521999SN/A * 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 8541999SN/A * and files cannot be called on the host and need to be handled as 85511851Sbrandon.potter@amd.com * special cases inside the simulator. These special_paths are handled by 8562680Sktlim@umich.edu * 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 8581999SN/A * 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 8606701Sgblack@eecs.umich.edu * USE_THE_HOST_FILESYSTEM_OPEN (with a possible redirection to the 8618852Sandreas.hansson@arm.com * faux-filesystem files). The faux-filesystem is dynamically created 8626701Sgblack@eecs.umich.edu * during simulator configuration using Python functions. 8631999SN/A * 4) If the host cannot open the file, the open attempt failed in "3)". 8646701Sgblack@eecs.umich.edu * Return the host's error code back through the system call to the 8651999SN/A * simulated process. If running a debug trace, also notify the user that 8666701Sgblack@eecs.umich.edu * the open call failed. 8671999SN/A * 8681999SN/A * Any success will set sim_fd to something other than -1 and skip the 8691999SN/A * next conditions effectively bypassing them. 8701999SN/A */ 8711999SN/A int sim_fd = -1; 8723669Sbinkertn@umich.edu std::string used_path; 8733669Sbinkertn@umich.edu std::vector<std::string> special_paths = 8743669Sbinkertn@umich.edu { "/proc/meminfo/", "/system/", "/platform/", "/etc/passwd" }; 8751999SN/A for (auto entry : special_paths) { 8761999SN/A if (startswith(path, entry)) { 8771999SN/A sim_fd = OS::openSpecialFile(abs_path, p, tc); 8782218SN/A used_path = abs_path; 8791999SN/A } 8801999SN/A } 8811999SN/A if (sim_fd == -1) { 8821999SN/A sim_fd = open(redir_path.c_str(), host_flags, mode); 8831999SN/A used_path = redir_path; 8841999SN/A } 8851999SN/A if (sim_fd == -1) { 8861999SN/A int local = -errno; 88711856Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s " 8881999SN/A "(inferred from:%s)\n", isopenat ? "at" : "", 8896701Sgblack@eecs.umich.edu used_path.c_str(), path.c_str()); 89011856Sbrandon.potter@amd.com return local; 89111856Sbrandon.potter@amd.com } 89210931Sbrandon.potter@amd.com 89311856Sbrandon.potter@amd.com /** 89411856Sbrandon.potter@amd.com * The file was opened successfully and needs to be recorded in the 8951999SN/A * process' file descriptor array so that it can be retrieved later. 89611856Sbrandon.potter@amd.com * The target file descriptor that is chosen will be the lowest unused 8971999SN/A * file descriptor. 89811856Sbrandon.potter@amd.com * Return the indirect target file descriptor back to the simulated 8991999SN/A * process to act as a handle for the opened file. 90011856Sbrandon.potter@amd.com */ 9011999SN/A auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 90211856Sbrandon.potter@amd.com int tgt_fd = p->fds->allocFD(ffdp); 9031999SN/A DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n" 9041999SN/A "(inferred from:%s)\n", isopenat ? "at" : "", 9055877Shsul@eecs.umich.edu sim_fd, tgt_fd, used_path.c_str(), path.c_str()); 9065877Shsul@eecs.umich.edu return tgt_fd; 9075877Shsul@eecs.umich.edu} 90811851Sbrandon.potter@amd.com 9095877Shsul@eecs.umich.edu/// Target open() handler. 9106701Sgblack@eecs.umich.edutemplate <class OS> 9116701Sgblack@eecs.umich.eduSyscallReturn 9126701Sgblack@eecs.umich.eduopenFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9136701Sgblack@eecs.umich.edu{ 9146701Sgblack@eecs.umich.edu return openImpl<OS>(desc, callnum, tc, false); 91510027SChris.Adeniyi-Jones@arm.com} 91610027SChris.Adeniyi-Jones@arm.com 91710027SChris.Adeniyi-Jones@arm.com/// Target openat() handler. 91810027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 91910027SChris.Adeniyi-Jones@arm.comSyscallReturn 9205877Shsul@eecs.umich.eduopenatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 92110318Sandreas.hansson@arm.com{ 92210318Sandreas.hansson@arm.com return openImpl<OS>(desc, callnum, tc, true); 9235877Shsul@eecs.umich.edu} 9245877Shsul@eecs.umich.edu 9255877Shsul@eecs.umich.edu/// Target unlinkat() handler. 9265877Shsul@eecs.umich.edutemplate <class OS> 92710486Stjablin@gmail.comSyscallReturn 92810486Stjablin@gmail.comunlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9295877Shsul@eecs.umich.edu{ 93011905SBrandon.Potter@amd.com int index = 0; 93111905SBrandon.Potter@amd.com auto process = tc->getProcessPtr(); 93211905SBrandon.Potter@amd.com int dirfd = process->getSyscallArg(tc, index); 93311905SBrandon.Potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 93410027SChris.Adeniyi-Jones@arm.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 93512206Srico.amslinger@informatik.uni-augsburg.de 93612206Srico.amslinger@informatik.uni-augsburg.de return unlinkHelper(desc, callnum, tc, 1); 9375877Shsul@eecs.umich.edu} 93811905SBrandon.Potter@amd.com 93911905SBrandon.Potter@amd.com/// Target facessat() handler 9405877Shsul@eecs.umich.edutemplate <class OS> 9415877Shsul@eecs.umich.eduSyscallReturn 94210027SChris.Adeniyi-Jones@arm.comfaccessatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 9435877Shsul@eecs.umich.edu{ 9445877Shsul@eecs.umich.edu int index = 0; 9455877Shsul@eecs.umich.edu auto process = tc->getProcessPtr(); 94612206Srico.amslinger@informatik.uni-augsburg.de int dirfd = process->getSyscallArg(tc, index); 94712206Srico.amslinger@informatik.uni-augsburg.de if (dirfd != OS::TGT_AT_FDCWD) 94812206Srico.amslinger@informatik.uni-augsburg.de warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 94912206Srico.amslinger@informatik.uni-augsburg.de return accessFunc(desc, callnum, tc, 1); 95012206Srico.amslinger@informatik.uni-augsburg.de} 95112206Srico.amslinger@informatik.uni-augsburg.de 95212206Srico.amslinger@informatik.uni-augsburg.de/// Target readlinkat() handler 95312206Srico.amslinger@informatik.uni-augsburg.detemplate <class OS> 95412206Srico.amslinger@informatik.uni-augsburg.deSyscallReturn 95510027SChris.Adeniyi-Jones@arm.comreadlinkatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 95610027SChris.Adeniyi-Jones@arm.com{ 95710027SChris.Adeniyi-Jones@arm.com int index = 0; 95810027SChris.Adeniyi-Jones@arm.com auto process = tc->getProcessPtr(); 9595877Shsul@eecs.umich.edu int dirfd = process->getSyscallArg(tc, index); 96010027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 96110027SChris.Adeniyi-Jones@arm.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 96210027SChris.Adeniyi-Jones@arm.com return readlinkFunc(desc, callnum, tc, 1); 96310027SChris.Adeniyi-Jones@arm.com} 96412206Srico.amslinger@informatik.uni-augsburg.de 96512206Srico.amslinger@informatik.uni-augsburg.de/// Target renameat() handler. 96612206Srico.amslinger@informatik.uni-augsburg.detemplate <class OS> 96712206Srico.amslinger@informatik.uni-augsburg.deSyscallReturn 96810027SChris.Adeniyi-Jones@arm.comrenameatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 96910027SChris.Adeniyi-Jones@arm.com{ 97010027SChris.Adeniyi-Jones@arm.com int index = 0; 97110027SChris.Adeniyi-Jones@arm.com auto process = tc->getProcessPtr(); 97210027SChris.Adeniyi-Jones@arm.com 97310027SChris.Adeniyi-Jones@arm.com int olddirfd = process->getSyscallArg(tc, index); 9745877Shsul@eecs.umich.edu if (olddirfd != OS::TGT_AT_FDCWD) 9755877Shsul@eecs.umich.edu warn("renameat: first argument not AT_FDCWD; unlikely to work"); 9765877Shsul@eecs.umich.edu 97710027SChris.Adeniyi-Jones@arm.com std::string old_name; 97810027SChris.Adeniyi-Jones@arm.com 9798601Ssteve.reinhardt@amd.com if (!tc->getVirtProxy().tryReadString(old_name, 98010027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) 9815877Shsul@eecs.umich.edu return -EFAULT; 9825877Shsul@eecs.umich.edu 9831999SN/A int newdirfd = process->getSyscallArg(tc, index); 984378SN/A if (newdirfd != OS::TGT_AT_FDCWD) 985360SN/A warn("renameat: third argument not AT_FDCWD; unlikely to work"); 9861450SN/A 98711851Sbrandon.potter@amd.com std::string new_name; 9882680Sktlim@umich.edu 989360SN/A if (!tc->getVirtProxy().tryReadString(new_name, 990360SN/A process->getSyscallArg(tc, index))) 991360SN/A return -EFAULT; 9926701Sgblack@eecs.umich.edu 9938852Sandreas.hansson@arm.com // Adjust path for cwd and redirection 9946701Sgblack@eecs.umich.edu old_name = process->checkPathRedirect(old_name); 9956701Sgblack@eecs.umich.edu new_name = process->checkPathRedirect(new_name); 9966701Sgblack@eecs.umich.edu 9976701Sgblack@eecs.umich.edu int result = rename(old_name.c_str(), new_name.c_str()); 998360SN/A return (result == -1) ? -errno : result; 9993669Sbinkertn@umich.edu} 10003669Sbinkertn@umich.edu 10013669Sbinkertn@umich.edu/// Target sysinfo() handler. 1002360SN/Atemplate <class OS> 1003360SN/ASyscallReturn 1004360SN/AsysinfoFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1005360SN/A{ 10062218SN/A int index = 0; 1007360SN/A auto process = tc->getProcessPtr(); 10088706Sandreas.hansson@arm.com 1009360SN/A TypedBufferArg<typename OS::tgt_sysinfo> 10101458SN/A sysinfo(process->getSyscallArg(tc, index)); 1011360SN/A 1012360SN/A sysinfo->uptime = seconds_since_epoch; 1013360SN/A sysinfo->totalram = process->system->memSize(); 10145074Ssaidi@eecs.umich.edu sysinfo->mem_unit = 1; 10155074Ssaidi@eecs.umich.edu 10165074Ssaidi@eecs.umich.edu sysinfo.copyOut(tc->getVirtProxy()); 101711851Sbrandon.potter@amd.com 10185074Ssaidi@eecs.umich.edu return 0; 10195074Ssaidi@eecs.umich.edu} 10205074Ssaidi@eecs.umich.edu 10215074Ssaidi@eecs.umich.edu/// Target chmod() handler. 10226701Sgblack@eecs.umich.edutemplate <class OS> 10238852Sandreas.hansson@arm.comSyscallReturn 10246701Sgblack@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 10255074Ssaidi@eecs.umich.edu{ 10266701Sgblack@eecs.umich.edu std::string path; 10275074Ssaidi@eecs.umich.edu auto process = tc->getProcessPtr(); 10285074Ssaidi@eecs.umich.edu 10295074Ssaidi@eecs.umich.edu int index = 0; 10305074Ssaidi@eecs.umich.edu if (!tc->getVirtProxy().tryReadString(path, 10315208Ssaidi@eecs.umich.edu process->getSyscallArg(tc, index))) { 10325208Ssaidi@eecs.umich.edu return -EFAULT; 10335208Ssaidi@eecs.umich.edu } 10345208Ssaidi@eecs.umich.edu 10355074Ssaidi@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 10365074Ssaidi@eecs.umich.edu mode_t hostMode = 0; 10375208Ssaidi@eecs.umich.edu 10385074Ssaidi@eecs.umich.edu // XXX translate mode flags via OS::something??? 10395074Ssaidi@eecs.umich.edu hostMode = mode; 10405074Ssaidi@eecs.umich.edu 10415074Ssaidi@eecs.umich.edu // Adjust path for cwd and redirection 10428706Sandreas.hansson@arm.com path = process->checkPathRedirect(path); 10435074Ssaidi@eecs.umich.edu 10445074Ssaidi@eecs.umich.edu // do the chmod 10455074Ssaidi@eecs.umich.edu int result = chmod(path.c_str(), hostMode); 10465074Ssaidi@eecs.umich.edu if (result < 0) 10475074Ssaidi@eecs.umich.edu return -errno; 104810027SChris.Adeniyi-Jones@arm.com 104910027SChris.Adeniyi-Jones@arm.com return 0; 105010027SChris.Adeniyi-Jones@arm.com} 105111851Sbrandon.potter@amd.com 105210027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 105310027SChris.Adeniyi-Jones@arm.comSyscallReturn 105410027SChris.Adeniyi-Jones@arm.compollFunc(SyscallDesc *desc, int num, ThreadContext *tc) 105510027SChris.Adeniyi-Jones@arm.com{ 105610027SChris.Adeniyi-Jones@arm.com int index = 0; 105710793Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 105810027SChris.Adeniyi-Jones@arm.com Addr fdsPtr = p->getSyscallArg(tc, index); 105910027SChris.Adeniyi-Jones@arm.com int nfds = p->getSyscallArg(tc, index); 106010027SChris.Adeniyi-Jones@arm.com int tmout = p->getSyscallArg(tc, index); 106110027SChris.Adeniyi-Jones@arm.com 106210027SChris.Adeniyi-Jones@arm.com BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 106310027SChris.Adeniyi-Jones@arm.com fdsBuf.copyIn(tc->getVirtProxy()); 106410027SChris.Adeniyi-Jones@arm.com 106510027SChris.Adeniyi-Jones@arm.com /** 106610027SChris.Adeniyi-Jones@arm.com * Record the target file descriptors in a local variable. We need to 106710027SChris.Adeniyi-Jones@arm.com * replace them with host file descriptors but we need a temporary copy 106810027SChris.Adeniyi-Jones@arm.com * for later. Afterwards, replace each target file descriptor in the 106910027SChris.Adeniyi-Jones@arm.com * poll_fd array with its host_fd. 107010027SChris.Adeniyi-Jones@arm.com */ 107110027SChris.Adeniyi-Jones@arm.com int temp_tgt_fds[nfds]; 107210027SChris.Adeniyi-Jones@arm.com for (index = 0; index < nfds; index++) { 107310027SChris.Adeniyi-Jones@arm.com temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 107410027SChris.Adeniyi-Jones@arm.com auto tgt_fd = temp_tgt_fds[index]; 107510027SChris.Adeniyi-Jones@arm.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 107610027SChris.Adeniyi-Jones@arm.com if (!hbfdp) 107710027SChris.Adeniyi-Jones@arm.com return -EBADF; 107810027SChris.Adeniyi-Jones@arm.com auto host_fd = hbfdp->getSimFD(); 107910027SChris.Adeniyi-Jones@arm.com ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 108010027SChris.Adeniyi-Jones@arm.com } 108110027SChris.Adeniyi-Jones@arm.com 108210027SChris.Adeniyi-Jones@arm.com /** 108310027SChris.Adeniyi-Jones@arm.com * We cannot allow an infinite poll to occur or it will inevitably cause 108410027SChris.Adeniyi-Jones@arm.com * a deadlock in the gem5 simulator with clone. We must pass in tmout with 10851999SN/A * a non-negative value, however it also makes no sense to poll on the 10861999SN/A * underlying host for any other time than tmout a zero timeout. 10871999SN/A */ 108811856Sbrandon.potter@amd.com int status; 10891999SN/A if (tmout < 0) { 10906701Sgblack@eecs.umich.edu status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 109111856Sbrandon.potter@amd.com if (status == 0) { 109211856Sbrandon.potter@amd.com /** 109310931Sbrandon.potter@amd.com * If blocking indefinitely, check the signal list to see if a 109411856Sbrandon.potter@amd.com * signal would break the poll out of the retry cycle and try 109511856Sbrandon.potter@amd.com * to return the signal interrupt instead. 10961999SN/A */ 109711856Sbrandon.potter@amd.com System *sysh = tc->getSystemPtr(); 10981999SN/A std::list<BasicSignal>::iterator it; 10992764Sstever@eecs.umich.edu for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 11002064SN/A if (it->receiver == p) 110110931Sbrandon.potter@amd.com return -EINTR; 11022064SN/A return SyscallReturn::retry(); 11032064SN/A } 110410931Sbrandon.potter@amd.com } else 11052064SN/A status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 11061999SN/A 11071999SN/A if (status == -1) 11082218SN/A return -errno; 11091999SN/A 111010931Sbrandon.potter@amd.com /** 11111999SN/A * Replace each host_fd in the returned poll_fd array with its original 11121999SN/A * target file descriptor. 11131999SN/A */ 11141999SN/A for (index = 0; index < nfds; index++) { 11151999SN/A auto tgt_fd = temp_tgt_fds[index]; 1116378SN/A ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 1117360SN/A } 11181450SN/A 111911851Sbrandon.potter@amd.com /** 11202680Sktlim@umich.edu * Copy out the pollfd struct because the host may have updated fields 1121360SN/A * in the structure. 1122360SN/A */ 1123360SN/A fdsBuf.copyOut(tc->getVirtProxy()); 11246701Sgblack@eecs.umich.edu 11258852Sandreas.hansson@arm.com return status; 11266701Sgblack@eecs.umich.edu} 11276701Sgblack@eecs.umich.edu 11286701Sgblack@eecs.umich.edu/// Target fchmod() handler. 11296701Sgblack@eecs.umich.edutemplate <class OS> 1130360SN/ASyscallReturn 11313669Sbinkertn@umich.edufchmodFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 11323669Sbinkertn@umich.edu{ 11333669Sbinkertn@umich.edu int index = 0; 1134360SN/A auto p = tc->getProcessPtr(); 1135360SN/A int tgt_fd = p->getSyscallArg(tc, index); 1136360SN/A uint32_t mode = p->getSyscallArg(tc, index); 1137360SN/A 11381458SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1139360SN/A if (!ffdp) 11408706Sandreas.hansson@arm.com return -EBADF; 1141360SN/A int sim_fd = ffdp->getSimFD(); 11421458SN/A 1143360SN/A mode_t hostMode = mode; 1144360SN/A 11451999SN/A int result = fchmod(sim_fd, hostMode); 11461999SN/A 11471999SN/A return (result < 0) ? -errno : 0; 114811851Sbrandon.potter@amd.com} 11492680Sktlim@umich.edu 11501999SN/A/// Target mremap() handler. 11511999SN/Atemplate <class OS> 11521999SN/ASyscallReturn 11536701Sgblack@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 11548852Sandreas.hansson@arm.com{ 11556701Sgblack@eecs.umich.edu int index = 0; 11566701Sgblack@eecs.umich.edu auto process = tc->getProcessPtr(); 11576701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 11586701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 11591999SN/A uint64_t new_length = process->getSyscallArg(tc, index); 11603669Sbinkertn@umich.edu uint64_t flags = process->getSyscallArg(tc, index); 11613669Sbinkertn@umich.edu uint64_t provided_address = 0; 11623669Sbinkertn@umich.edu bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 11632764Sstever@eecs.umich.edu 11642064SN/A if (use_provided_address) 11652064SN/A provided_address = process->getSyscallArg(tc, index); 11662064SN/A 11671999SN/A if ((start % TheISA::PageBytes != 0) || 11681999SN/A (provided_address % TheISA::PageBytes != 0)) { 11692064SN/A warn("mremap failing: arguments not page aligned"); 11701999SN/A return -EINVAL; 11711999SN/A } 11721999SN/A 11731999SN/A new_length = roundUp(new_length, TheISA::PageBytes); 11748706Sandreas.hansson@arm.com 11751999SN/A if (new_length > old_length) { 11761999SN/A std::shared_ptr<MemState> mem_state = process->memState; 11771999SN/A Addr mmap_end = mem_state->getMmapEnd(); 11781999SN/A 1179378SN/A if ((start + old_length) == mmap_end && 1180360SN/A (!use_provided_address || provided_address == start)) { 11811450SN/A // This case cannot occur when growing downward, as 118211856Sbrandon.potter@amd.com // start is greater than or equal to mmap_end. 1183360SN/A uint64_t diff = new_length - old_length; 11846701Sgblack@eecs.umich.edu process->allocateMem(mmap_end, diff); 118511856Sbrandon.potter@amd.com mem_state->setMmapEnd(mmap_end + diff); 118611856Sbrandon.potter@amd.com return start; 1187360SN/A } else { 118811380Salexandru.dutu@amd.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 1189360SN/A warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 119011856Sbrandon.potter@amd.com return -ENOMEM; 119111856Sbrandon.potter@amd.com } else { 11921458SN/A uint64_t new_start = provided_address; 119311856Sbrandon.potter@amd.com if (!use_provided_address) { 1194360SN/A new_start = process->mmapGrowsDown() ? 1195360SN/A mmap_end - new_length : mmap_end; 119610931Sbrandon.potter@amd.com mmap_end = process->mmapGrowsDown() ? 1197360SN/A new_start : mmap_end + new_length; 1198360SN/A mem_state->setMmapEnd(mmap_end); 11991458SN/A } 1200360SN/A 120110931Sbrandon.potter@amd.com process->pTable->remap(start, old_length, new_start); 12022021SN/A warn("mremapping to new vaddr %08p-%08p, adding %d\n", 12031458SN/A new_start, new_start + new_length, 1204360SN/A new_length - old_length); 1205360SN/A // add on the remaining unallocated pages 1206360SN/A process->allocateMem(new_start + old_length, 12071706SN/A new_length - old_length, 12081706SN/A use_provided_address /* clobber */); 12091706SN/A if (use_provided_address && 121011851Sbrandon.potter@amd.com ((new_start + new_length > mem_state->getMmapEnd() && 12112680Sktlim@umich.edu !process->mmapGrowsDown()) || 12121706SN/A (new_start < mem_state->getMmapEnd() && 121311799Sbrandon.potter@amd.com process->mmapGrowsDown()))) { 121411799Sbrandon.potter@amd.com // something fishy going on here, at least notify the user 121511799Sbrandon.potter@amd.com // @todo: increase mmap_end? 12161706SN/A warn("mmap region limit exceeded with MREMAP_FIXED\n"); 12171706SN/A } 12186701Sgblack@eecs.umich.edu warn("returning %08p as start\n", new_start); 12198852Sandreas.hansson@arm.com return new_start; 12206701Sgblack@eecs.umich.edu } 12216701Sgblack@eecs.umich.edu } 12226701Sgblack@eecs.umich.edu } else { 12236701Sgblack@eecs.umich.edu if (use_provided_address && provided_address != start) 12241706SN/A process->pTable->remap(start, new_length, provided_address); 12253669Sbinkertn@umich.edu process->pTable->unmap(start + new_length, old_length - new_length); 12263669Sbinkertn@umich.edu return use_provided_address ? provided_address : start; 12273669Sbinkertn@umich.edu } 12281706SN/A} 12291706SN/A 12301706SN/A/// Target stat() handler. 12311706SN/Atemplate <class OS> 12322218SN/ASyscallReturn 12331706SN/AstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 123411759Sbrandon.potter@amd.com{ 123511799Sbrandon.potter@amd.com std::string path; 12361706SN/A auto process = tc->getProcessPtr(); 12371706SN/A 12381706SN/A int index = 0; 123911886Sbrandon.potter@amd.com if (!tc->getVirtProxy().tryReadString(path, 124011886Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 124111886Sbrandon.potter@amd.com return -EFAULT; 124211886Sbrandon.potter@amd.com } 124311886Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 124411886Sbrandon.potter@amd.com 124511886Sbrandon.potter@amd.com // Adjust path for cwd and redirection 124611886Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 124711886Sbrandon.potter@amd.com 124811886Sbrandon.potter@amd.com struct stat hostBuf; 124911886Sbrandon.potter@amd.com int result = stat(path.c_str(), &hostBuf); 125011886Sbrandon.potter@amd.com 125111886Sbrandon.potter@amd.com if (result < 0) 125211886Sbrandon.potter@amd.com return -errno; 125311886Sbrandon.potter@amd.com 125411886Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 125511886Sbrandon.potter@amd.com 125611886Sbrandon.potter@amd.com return 0; 125711886Sbrandon.potter@amd.com} 125811886Sbrandon.potter@amd.com 125911886Sbrandon.potter@amd.com 126011886Sbrandon.potter@amd.com/// Target stat64() handler. 126111886Sbrandon.potter@amd.comtemplate <class OS> 126211886Sbrandon.potter@amd.comSyscallReturn 126311886Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 126411886Sbrandon.potter@amd.com{ 126511886Sbrandon.potter@amd.com std::string path; 126611886Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 126711886Sbrandon.potter@amd.com 126811886Sbrandon.potter@amd.com int index = 0; 126911886Sbrandon.potter@amd.com if (!tc->getVirtProxy().tryReadString(path, 127011886Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 127111886Sbrandon.potter@amd.com return -EFAULT; 127211886Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 127311886Sbrandon.potter@amd.com 127411886Sbrandon.potter@amd.com // Adjust path for cwd and redirection 127511886Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 127611886Sbrandon.potter@amd.com 127711886Sbrandon.potter@amd.com#if NO_STAT64 127811886Sbrandon.potter@amd.com struct stat hostBuf; 127911886Sbrandon.potter@amd.com int result = stat(path.c_str(), &hostBuf); 128011886Sbrandon.potter@amd.com#else 128111886Sbrandon.potter@amd.com struct stat64 hostBuf; 128211886Sbrandon.potter@amd.com int result = stat64(path.c_str(), &hostBuf); 128311886Sbrandon.potter@amd.com#endif 128411886Sbrandon.potter@amd.com 128511886Sbrandon.potter@amd.com if (result < 0) 128611886Sbrandon.potter@amd.com return -errno; 128711886Sbrandon.potter@amd.com 128811886Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 128911886Sbrandon.potter@amd.com 129011886Sbrandon.potter@amd.com return 0; 129111886Sbrandon.potter@amd.com} 129211886Sbrandon.potter@amd.com 129311886Sbrandon.potter@amd.com 129411886Sbrandon.potter@amd.com/// Target fstatat64() handler. 129511886Sbrandon.potter@amd.comtemplate <class OS> 129611886Sbrandon.potter@amd.comSyscallReturn 129711886Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 129811886Sbrandon.potter@amd.com{ 129911886Sbrandon.potter@amd.com int index = 0; 130011886Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 130111886Sbrandon.potter@amd.com int dirfd = process->getSyscallArg(tc, index); 130211886Sbrandon.potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 130311886Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 130411886Sbrandon.potter@amd.com 130511886Sbrandon.potter@amd.com std::string path; 130611886Sbrandon.potter@amd.com if (!tc->getVirtProxy().tryReadString(path, 130711886Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 130811886Sbrandon.potter@amd.com return -EFAULT; 130911886Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 131011911SBrandon.Potter@amd.com 131111911SBrandon.Potter@amd.com // Adjust path for cwd and redirection 131211911SBrandon.Potter@amd.com path = process->checkPathRedirect(path); 131311911SBrandon.Potter@amd.com 131411911SBrandon.Potter@amd.com#if NO_STAT64 131511911SBrandon.Potter@amd.com struct stat hostBuf; 131611911SBrandon.Potter@amd.com int result = stat(path.c_str(), &hostBuf); 131711886Sbrandon.potter@amd.com#else 131811886Sbrandon.potter@amd.com struct stat64 hostBuf; 131911886Sbrandon.potter@amd.com int result = stat64(path.c_str(), &hostBuf); 132011886Sbrandon.potter@amd.com#endif 132111886Sbrandon.potter@amd.com 132211886Sbrandon.potter@amd.com if (result < 0) 132311886Sbrandon.potter@amd.com return -errno; 132411886Sbrandon.potter@amd.com 132511886Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 132611886Sbrandon.potter@amd.com 132711886Sbrandon.potter@amd.com return 0; 132811886Sbrandon.potter@amd.com} 132911886Sbrandon.potter@amd.com 133011886Sbrandon.potter@amd.com 133111886Sbrandon.potter@amd.com/// Target fstat64() handler. 133211886Sbrandon.potter@amd.comtemplate <class OS> 133311886Sbrandon.potter@amd.comSyscallReturn 133411886Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 133511886Sbrandon.potter@amd.com{ 133611886Sbrandon.potter@amd.com int index = 0; 133711886Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 133811886Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 133911886Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 134011886Sbrandon.potter@amd.com 134111886Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 134211886Sbrandon.potter@amd.com if (!ffdp) 134311886Sbrandon.potter@amd.com return -EBADF; 134411886Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 134511886Sbrandon.potter@amd.com 134611886Sbrandon.potter@amd.com#if NO_STAT64 134711886Sbrandon.potter@amd.com struct stat hostBuf; 134811886Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 134911886Sbrandon.potter@amd.com#else 135011886Sbrandon.potter@amd.com struct stat64 hostBuf; 135111886Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 135211886Sbrandon.potter@amd.com#endif 135311886Sbrandon.potter@amd.com 135411886Sbrandon.potter@amd.com if (result < 0) 135511886Sbrandon.potter@amd.com return -errno; 135611886Sbrandon.potter@amd.com 135711886Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 135811886Sbrandon.potter@amd.com 135911886Sbrandon.potter@amd.com return 0; 136011886Sbrandon.potter@amd.com} 136111886Sbrandon.potter@amd.com 136211886Sbrandon.potter@amd.com 136311886Sbrandon.potter@amd.com/// Target lstat() handler. 136411886Sbrandon.potter@amd.comtemplate <class OS> 136511886Sbrandon.potter@amd.comSyscallReturn 136611886Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 136711886Sbrandon.potter@amd.com{ 136811886Sbrandon.potter@amd.com std::string path; 136911886Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 137011886Sbrandon.potter@amd.com 13711706SN/A int index = 0; 13721706SN/A if (!tc->getVirtProxy().tryReadString(path, 13731706SN/A process->getSyscallArg(tc, index))) { 13741706SN/A return -EFAULT; 137511856Sbrandon.potter@amd.com } 13761706SN/A Addr bufPtr = process->getSyscallArg(tc, index); 13776701Sgblack@eecs.umich.edu 137811856Sbrandon.potter@amd.com // Adjust path for cwd and redirection 137911856Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 13801706SN/A 138111856Sbrandon.potter@amd.com struct stat hostBuf; 138211856Sbrandon.potter@amd.com int result = lstat(path.c_str(), &hostBuf); 13831706SN/A 138411856Sbrandon.potter@amd.com if (result < 0) 13851706SN/A return -errno; 13861706SN/A 138710931Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 13881706SN/A 13891706SN/A return 0; 13902218SN/A} 13911706SN/A 139211759Sbrandon.potter@amd.com/// Target lstat64() handler. 13931706SN/Atemplate <class OS> 13941706SN/ASyscallReturn 13951706SN/Alstat64Func(SyscallDesc *desc, int callnum, ThreadContext *tc) 13961706SN/A{ 13971706SN/A std::string path; 13981999SN/A auto process = tc->getProcessPtr(); 13991999SN/A 14001999SN/A int index = 0; 140111856Sbrandon.potter@amd.com if (!tc->getVirtProxy().tryReadString(path, 14021999SN/A process->getSyscallArg(tc, index))) { 14036701Sgblack@eecs.umich.edu return -EFAULT; 140411856Sbrandon.potter@amd.com } 140510931Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 140611856Sbrandon.potter@amd.com 140711856Sbrandon.potter@amd.com // Adjust path for cwd and redirection 14081999SN/A path = process->checkPathRedirect(path); 140911856Sbrandon.potter@amd.com 14101999SN/A#if NO_STAT64 141111856Sbrandon.potter@amd.com struct stat hostBuf; 141211856Sbrandon.potter@amd.com int result = lstat(path.c_str(), &hostBuf); 141311856Sbrandon.potter@amd.com#else 14141999SN/A struct stat64 hostBuf; 14156227Snate@binkert.org int result = lstat64(path.c_str(), &hostBuf); 14161999SN/A#endif 14172461SN/A 141811856Sbrandon.potter@amd.com if (result < 0) 141911856Sbrandon.potter@amd.com return -errno; 14208737Skoansin.tan@gmail.com 14211999SN/A copyOutStat64Buf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 142211856Sbrandon.potter@amd.com 142311856Sbrandon.potter@amd.com return 0; 14241999SN/A} 14251999SN/A 142610931Sbrandon.potter@amd.com/// Target fstat() handler. 14271999SN/Atemplate <class OS> 14286227Snate@binkert.orgSyscallReturn 14291999SN/AfstatFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 14301999SN/A{ 14311999SN/A int index = 0; 14322218SN/A auto p = tc->getProcessPtr(); 14331999SN/A int tgt_fd = p->getSyscallArg(tc, index); 143410629Sjthestness@gmail.com Addr bufPtr = p->getSyscallArg(tc, index); 14351999SN/A 14361999SN/A DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 143711385Sbrandon.potter@amd.com 1438360SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 14391450SN/A if (!ffdp) 144011851Sbrandon.potter@amd.com return -EBADF; 144111385Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 1442360SN/A 14436701Sgblack@eecs.umich.edu struct stat hostBuf; 14446701Sgblack@eecs.umich.edu int result = fstat(sim_fd, &hostBuf); 14456701Sgblack@eecs.umich.edu 144611383Sbrandon.potter@amd.com if (result < 0) 144711383Sbrandon.potter@amd.com return -errno; 14488324Ssteve.reinhardt@amd.com 144910486Stjablin@gmail.com copyOutStatBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1450360SN/A 145111385Sbrandon.potter@amd.com return 0; 145211385Sbrandon.potter@amd.com} 14539008Sgblack@eecs.umich.edu 145411383Sbrandon.potter@amd.com/// Target statfs() handler. 145511383Sbrandon.potter@amd.comtemplate <class OS> 145611383Sbrandon.potter@amd.comSyscallReturn 145711383Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 145811383Sbrandon.potter@amd.com{ 145911383Sbrandon.potter@amd.com#if defined(__linux__) 146011383Sbrandon.potter@amd.com std::string path; 146111383Sbrandon.potter@amd.com auto process = tc->getProcessPtr(); 146211383Sbrandon.potter@amd.com 14638324Ssteve.reinhardt@amd.com int index = 0; 146411383Sbrandon.potter@amd.com if (!tc->getVirtProxy().tryReadString(path, 146511383Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 146611383Sbrandon.potter@amd.com return -EFAULT; 146711383Sbrandon.potter@amd.com } 146811383Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 146911383Sbrandon.potter@amd.com 147011383Sbrandon.potter@amd.com // Adjust path for cwd and redirection 147111383Sbrandon.potter@amd.com path = process->checkPathRedirect(path); 147211383Sbrandon.potter@amd.com 147311383Sbrandon.potter@amd.com struct statfs hostBuf; 147411383Sbrandon.potter@amd.com int result = statfs(path.c_str(), &hostBuf); 147511383Sbrandon.potter@amd.com 147611383Sbrandon.potter@amd.com if (result < 0) 147711383Sbrandon.potter@amd.com return -errno; 147811383Sbrandon.potter@amd.com 147911383Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 148011383Sbrandon.potter@amd.com return 0; 148111383Sbrandon.potter@amd.com#else 148211383Sbrandon.potter@amd.com warnUnsupportedOS("statfs"); 148311383Sbrandon.potter@amd.com return -1; 148411383Sbrandon.potter@amd.com#endif 148511383Sbrandon.potter@amd.com} 148611383Sbrandon.potter@amd.com 148711383Sbrandon.potter@amd.comtemplate <class OS> 14888324Ssteve.reinhardt@amd.comSyscallReturn 14895877Shsul@eecs.umich.educloneFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 149010486Stjablin@gmail.com{ 149110486Stjablin@gmail.com int index = 0; 149211383Sbrandon.potter@amd.com 149311383Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 149411383Sbrandon.potter@amd.com RegVal flags = p->getSyscallArg(tc, index); 149511856Sbrandon.potter@amd.com RegVal newStack = p->getSyscallArg(tc, index); 149611624Smichael.lebeane@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 149711856Sbrandon.potter@amd.com 149811856Sbrandon.potter@amd.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 149911856Sbrandon.potter@amd.com /** 150011856Sbrandon.potter@amd.com * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 150111624Smichael.lebeane@amd.com * The flag defines the list of clone() arguments in the following 150211624Smichael.lebeane@amd.com * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 150311624Smichael.lebeane@amd.com */ 150411856Sbrandon.potter@amd.com Addr tlsPtr = p->getSyscallArg(tc, index); 150511856Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 150611383Sbrandon.potter@amd.com#else 150711856Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 1508360SN/A Addr tlsPtr = p->getSyscallArg(tc, index); 150911913SBrandon.Potter@amd.com#endif 151011383Sbrandon.potter@amd.com 15118600Ssteve.reinhardt@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 151211383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 151311383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 151411383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 15158600Ssteve.reinhardt@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 15162544SN/A ((flags & OS::TGT_CLONE_VM) && !(newStack))) 15172544SN/A return -EINVAL; 151811383Sbrandon.potter@amd.com 151911383Sbrandon.potter@amd.com ThreadContext *ctc; 152011383Sbrandon.potter@amd.com if (!(ctc = p->findFreeContext())) { 152111905SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system" 152211905SBrandon.Potter@amd.com "[cpu %d, thread %d]", tc->cpuId(), tc->threadId()); 152311905SBrandon.Potter@amd.com return -EAGAIN; 152411905SBrandon.Potter@amd.com } 152511905SBrandon.Potter@amd.com 152611905SBrandon.Potter@amd.com /** 152711905SBrandon.Potter@amd.com * Note that ProcessParams is generated by swig and there are no other 152811383Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 152911383Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 153011383Sbrandon.potter@amd.com * constructor. 153111383Sbrandon.potter@amd.com */ 153211383Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 153311383Sbrandon.potter@amd.com pp->executable.assign(*(new std::string(p->progName()))); 153411383Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 153511383Sbrandon.potter@amd.com pp->system = p->system; 153611383Sbrandon.potter@amd.com pp->cwd.assign(p->tgtCwd); 153711383Sbrandon.potter@amd.com pp->input.assign("stdin"); 153811383Sbrandon.potter@amd.com pp->output.assign("stdout"); 153911383Sbrandon.potter@amd.com pp->errout.assign("stderr"); 154011383Sbrandon.potter@amd.com pp->uid = p->uid(); 154111383Sbrandon.potter@amd.com pp->euid = p->euid(); 154211383Sbrandon.potter@amd.com pp->gid = p->gid(); 15438600Ssteve.reinhardt@amd.com pp->egid = p->egid(); 15446672Sgblack@eecs.umich.edu 15458600Ssteve.reinhardt@amd.com /* Find the first free PID that's less than the maximum */ 154611383Sbrandon.potter@amd.com std::set<int> const& pids = p->system->PIDs; 154711383Sbrandon.potter@amd.com int temp_pid = *pids.begin(); 154811383Sbrandon.potter@amd.com do { 15498601Ssteve.reinhardt@amd.com temp_pid++; 15502544SN/A } while (pids.find(temp_pid) != pids.end()); 155111383Sbrandon.potter@amd.com if (temp_pid >= System::maxPID) 155211383Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 155311383Sbrandon.potter@amd.com 155411383Sbrandon.potter@amd.com pp->pid = temp_pid; 155511383Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 155611383Sbrandon.potter@amd.com pp->useArchPT = p->useArchPT; 155711383Sbrandon.potter@amd.com pp->kvmInSE = p->kvmInSE; 155811383Sbrandon.potter@amd.com Process *cp = pp->create(); 155911383Sbrandon.potter@amd.com delete pp; 156011383Sbrandon.potter@amd.com 156111383Sbrandon.potter@amd.com Process *owner = ctc->getProcessPtr(); 156211383Sbrandon.potter@amd.com ctc->setProcessPtr(cp); 156311383Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 156411383Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 156511383Sbrandon.potter@amd.com 156611383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 156711383Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 156811383Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 156911383Sbrandon.potter@amd.com *ptid = cp->pid(); 157011383Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getVirtProxy()); 157111383Sbrandon.potter@amd.com } 157211383Sbrandon.potter@amd.com 157311383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 157411383Sbrandon.potter@amd.com cp->pTable->shared = true; 157511383Sbrandon.potter@amd.com cp->useForClone = true; 157611383Sbrandon.potter@amd.com } 157711383Sbrandon.potter@amd.com cp->initState(); 157811383Sbrandon.potter@amd.com p->clone(tc, ctc, cp, flags); 157911383Sbrandon.potter@amd.com 158011383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 158111383Sbrandon.potter@amd.com delete cp->sigchld; 158211383Sbrandon.potter@amd.com cp->sigchld = p->sigchld; 158311392Sbrandon.potter@amd.com } else if (flags & OS::TGT_SIGCHLD) { 158411392Sbrandon.potter@amd.com *cp->sigchld = true; 158511392Sbrandon.potter@amd.com } 158611392Sbrandon.potter@amd.com 158711392Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 158811392Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 158911392Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 159011392Sbrandon.potter@amd.com *ctid = cp->pid(); 159111392Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getVirtProxy()); 159211392Sbrandon.potter@amd.com } 159311392Sbrandon.potter@amd.com 159411392Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 159511392Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 159611392Sbrandon.potter@amd.com 159711856Sbrandon.potter@amd.com ctc->clearArchRegs(); 159811856Sbrandon.potter@amd.com 159911856Sbrandon.potter@amd.com OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 160011392Sbrandon.potter@amd.com 160111392Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 160211392Sbrandon.potter@amd.com 160311392Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 160411392Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 160511392Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 160611392Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 160711392Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 160811383Sbrandon.potter@amd.com#endif 160911383Sbrandon.potter@amd.com 161011383Sbrandon.potter@amd.com if (p->kvmInSE) { 161111383Sbrandon.potter@amd.com#if THE_ISA == X86_ISA 161211383Sbrandon.potter@amd.com ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX)); 16131458SN/A#else 1614360SN/A panic("KVM CPU model is not supported for this ISA"); 1615360SN/A#endif 161611593Santhony.gutierrez@amd.com } else { 161711593Santhony.gutierrez@amd.com TheISA::PCState cpc = tc->pcState(); 161811851Sbrandon.potter@amd.com cpc.advance(); 161911593Santhony.gutierrez@amd.com ctc->pcState(cpc); 162011593Santhony.gutierrez@amd.com } 162111593Santhony.gutierrez@amd.com ctc->activate(); 162211593Santhony.gutierrez@amd.com 162311593Santhony.gutierrez@amd.com return cp->pid(); 162411593Santhony.gutierrez@amd.com} 162511593Santhony.gutierrez@amd.com 162611856Sbrandon.potter@amd.com/// Target fstatfs() handler. 162711856Sbrandon.potter@amd.comtemplate <class OS> 162811593Santhony.gutierrez@amd.comSyscallReturn 162911856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 163011593Santhony.gutierrez@amd.com{ 163111593Santhony.gutierrez@amd.com int index = 0; 163211593Santhony.gutierrez@amd.com auto p = tc->getProcessPtr(); 163311593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 163411594Santhony.gutierrez@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 163511593Santhony.gutierrez@amd.com 163611593Santhony.gutierrez@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 163711593Santhony.gutierrez@amd.com if (!ffdp) 163811593Santhony.gutierrez@amd.com return -EBADF; 163911385Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 164011385Sbrandon.potter@amd.com 164111385Sbrandon.potter@amd.com struct statfs hostBuf; 164211851Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 164311385Sbrandon.potter@amd.com 164411385Sbrandon.potter@amd.com if (result < 0) 164511385Sbrandon.potter@amd.com return -errno; 164611385Sbrandon.potter@amd.com 164711385Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getVirtProxy(), bufPtr, &hostBuf); 164811385Sbrandon.potter@amd.com 164911385Sbrandon.potter@amd.com return 0; 165011851Sbrandon.potter@amd.com} 165111385Sbrandon.potter@amd.com 165211385Sbrandon.potter@amd.com/// Target readv() handler. 165311385Sbrandon.potter@amd.comtemplate <class OS> 165411385Sbrandon.potter@amd.comSyscallReturn 1655378SN/AreadvFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 1656360SN/A{ 16571450SN/A int index = 0; 165811851Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 165911851Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 1660360SN/A 16616701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16626701Sgblack@eecs.umich.edu if (!ffdp) 16636701Sgblack@eecs.umich.edu return -EBADF; 1664360SN/A int sim_fd = ffdp->getSimFD(); 1665360SN/A 166611906SBrandon.Potter@amd.com PortProxy &prox = tc->getVirtProxy(); 166711906SBrandon.Potter@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 166811906SBrandon.Potter@amd.com size_t count = p->getSyscallArg(tc, index); 166911906SBrandon.Potter@amd.com typename OS::tgt_iovec tiov[count]; 167011906SBrandon.Potter@amd.com struct iovec hiov[count]; 167111906SBrandon.Potter@amd.com for (size_t i = 0; i < count; ++i) { 1672360SN/A prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)), 167311906SBrandon.Potter@amd.com &tiov[i], sizeof(typename OS::tgt_iovec)); 167411906SBrandon.Potter@amd.com hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len); 167511906SBrandon.Potter@amd.com hiov[i].iov_base = new char [hiov[i].iov_len]; 167611906SBrandon.Potter@amd.com } 167711906SBrandon.Potter@amd.com 167811906SBrandon.Potter@amd.com int result = readv(sim_fd, hiov, count); 16795877Shsul@eecs.umich.edu int local_errno = errno; 168011906SBrandon.Potter@amd.com 168111906SBrandon.Potter@amd.com for (size_t i = 0; i < count; ++i) { 168211906SBrandon.Potter@amd.com if (result != -1) { 168311906SBrandon.Potter@amd.com prox.writeBlob(TheISA::htog(tiov[i].iov_base), 1684360SN/A hiov[i].iov_base, hiov[i].iov_len); 1685360SN/A } 16868706Sandreas.hansson@arm.com delete [] (char *)hiov[i].iov_base; 16871458SN/A } 1688360SN/A 1689360SN/A return (result == -1) ? -local_errno : result; 169012235Sar4jc@virginia.edu} 169112235Sar4jc@virginia.edu 169212235Sar4jc@virginia.edu/// Target writev() handler. 169312235Sar4jc@virginia.edutemplate <class OS> 169412235Sar4jc@virginia.eduSyscallReturn 169512235Sar4jc@virginia.eduwritevFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 169612235Sar4jc@virginia.edu{ 169712235Sar4jc@virginia.edu int index = 0; 169812235Sar4jc@virginia.edu auto p = tc->getProcessPtr(); 169912235Sar4jc@virginia.edu int tgt_fd = p->getSyscallArg(tc, index); 170012235Sar4jc@virginia.edu 170112235Sar4jc@virginia.edu auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 170212235Sar4jc@virginia.edu if (!hbfdp) 170312235Sar4jc@virginia.edu return -EBADF; 170412235Sar4jc@virginia.edu int sim_fd = hbfdp->getSimFD(); 170512235Sar4jc@virginia.edu 170612235Sar4jc@virginia.edu PortProxy &prox = tc->getVirtProxy(); 170712235Sar4jc@virginia.edu uint64_t tiov_base = p->getSyscallArg(tc, index); 170812235Sar4jc@virginia.edu size_t count = p->getSyscallArg(tc, index); 170912235Sar4jc@virginia.edu struct iovec hiov[count]; 171012235Sar4jc@virginia.edu for (size_t i = 0; i < count; ++i) { 171112235Sar4jc@virginia.edu typename OS::tgt_iovec tiov; 171212235Sar4jc@virginia.edu 171312235Sar4jc@virginia.edu prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 171412235Sar4jc@virginia.edu &tiov, sizeof(typename OS::tgt_iovec)); 171512235Sar4jc@virginia.edu hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 171612235Sar4jc@virginia.edu hiov[i].iov_base = new char [hiov[i].iov_len]; 171712235Sar4jc@virginia.edu prox.readBlob(TheISA::gtoh(tiov.iov_base), hiov[i].iov_base, 171812235Sar4jc@virginia.edu hiov[i].iov_len); 171912235Sar4jc@virginia.edu } 172012235Sar4jc@virginia.edu 172112235Sar4jc@virginia.edu int result = writev(sim_fd, hiov, count); 172212235Sar4jc@virginia.edu 172312235Sar4jc@virginia.edu for (size_t i = 0; i < count; ++i) 172412235Sar4jc@virginia.edu delete [] (char *)hiov[i].iov_base; 172512235Sar4jc@virginia.edu 172612235Sar4jc@virginia.edu return (result == -1) ? -errno : result; 172712235Sar4jc@virginia.edu} 172812235Sar4jc@virginia.edu 172912235Sar4jc@virginia.edu/// Real mmap handler. 173012235Sar4jc@virginia.edutemplate <class OS> 173112235Sar4jc@virginia.eduSyscallReturn 173210796Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, ThreadContext *tc, bool is_mmap2) 173310796Sbrandon.potter@amd.com{ 173410796Sbrandon.potter@amd.com int index = 0; 173511851Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 173610796Sbrandon.potter@amd.com Addr start = p->getSyscallArg(tc, index); 173710796Sbrandon.potter@amd.com uint64_t length = p->getSyscallArg(tc, index); 173810796Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 173910796Sbrandon.potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 174010796Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 174110796Sbrandon.potter@amd.com int offset = p->getSyscallArg(tc, index); 174210796Sbrandon.potter@amd.com 174310796Sbrandon.potter@amd.com if (is_mmap2) 174410796Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 174510796Sbrandon.potter@amd.com 174610796Sbrandon.potter@amd.com if (start & (TheISA::PageBytes - 1) || 174710796Sbrandon.potter@amd.com offset & (TheISA::PageBytes - 1) || 174810796Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 174910796Sbrandon.potter@amd.com tgt_flags & OS::TGT_MAP_SHARED) || 175010796Sbrandon.potter@amd.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 175111337SMichael.Lebeane@amd.com !(tgt_flags & OS::TGT_MAP_SHARED)) || 175211337SMichael.Lebeane@amd.com !length) { 175311337SMichael.Lebeane@amd.com return -EINVAL; 175411851Sbrandon.potter@amd.com } 175511337SMichael.Lebeane@amd.com 175611337SMichael.Lebeane@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 175711337SMichael.Lebeane@amd.com // With shared mmaps, there are two cases to consider: 175811337SMichael.Lebeane@amd.com // 1) anonymous: writes should modify the mapping and this should be 175911337SMichael.Lebeane@amd.com // visible to observers who share the mapping. Currently, it's 176011337SMichael.Lebeane@amd.com // difficult to update the shared mapping because there's no 176111337SMichael.Lebeane@amd.com // structure which maintains information about the which virtual 176211337SMichael.Lebeane@amd.com // memory areas are shared. If that structure existed, it would be 176311337SMichael.Lebeane@amd.com // possible to make the translations point to the same frames. 176411337SMichael.Lebeane@amd.com // 2) file-backed: writes should modify the mapping and the file 176511337SMichael.Lebeane@amd.com // which is backed by the mapping. The shared mapping problem is the 176611337SMichael.Lebeane@amd.com // same as what was mentioned about the anonymous mappings. For 176711337SMichael.Lebeane@amd.com // file-backed mappings, the writes to the file are difficult 1768378SN/A // because it requires syncing what the mapping holds with the file 1769360SN/A // that resides on the host system. So, any write on a real system 17701450SN/A // would cause the change to be propagated to the file mapping at 177111851Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 177211851Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 1773360SN/A // works well enough. The guarantee is provided by the msync system 17746701Sgblack@eecs.umich.edu // call. We could force the change through with shared mappings with 17756701Sgblack@eecs.umich.edu // a call to msync, but that again would require more information 1776360SN/A // than we currently maintain. 177710796Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 1778360SN/A "unsupported. The write succeeds on the target, but it " 17796109Ssanchezd@stanford.edu "will not be propagated to the host or shared mappings"); 17806109Ssanchezd@stanford.edu } 1781360SN/A 17828706Sandreas.hansson@arm.com length = roundUp(length, TheISA::PageBytes); 1783360SN/A 17841458SN/A int sim_fd = -1; 1785360SN/A uint8_t *pmap = nullptr; 1786360SN/A if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1787360SN/A std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 17881999SN/A 17891999SN/A auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 17901999SN/A if (dfdp) { 179111851Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 17922680Sktlim@umich.edu return emul_driver->mmap(tc, start, length, prot, tgt_flags, 17931999SN/A tgt_fd, offset); 17941999SN/A } 17951999SN/A 17966701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 17978852Sandreas.hansson@arm.com if (!ffdp) 17986701Sgblack@eecs.umich.edu return -EBADF; 17996701Sgblack@eecs.umich.edu sim_fd = ffdp->getSimFD(); 18006701Sgblack@eecs.umich.edu 18011999SN/A pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 18026701Sgblack@eecs.umich.edu sim_fd, offset); 18036701Sgblack@eecs.umich.edu 18048706Sandreas.hansson@arm.com if (pmap == (decltype(pmap))-1) { 18051999SN/A warn("mmap: failed to map file into host address space"); 18061999SN/A return -errno; 180711906SBrandon.Potter@amd.com } 18088737Skoansin.tan@gmail.com } 18098737Skoansin.tan@gmail.com 18101999SN/A // Extend global mmap region if necessary. Note that we ignore the 18113669Sbinkertn@umich.edu // start address unless MAP_FIXED is specified. 18123669Sbinkertn@umich.edu if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 18133669Sbinkertn@umich.edu std::shared_ptr<MemState> mem_state = p->memState; 18143669Sbinkertn@umich.edu Addr mmap_end = mem_state->getMmapEnd(); 18151999SN/A 18161999SN/A start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 18171999SN/A mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 18181999SN/A 18191999SN/A mem_state->setMmapEnd(mmap_end); 18201999SN/A } 18211999SN/A 182211886Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 182311886Sbrandon.potter@amd.com start, start + length - 1); 182411886Sbrandon.potter@amd.com 182511886Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 182611886Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 182711886Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 182811886Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 182911886Sbrandon.potter@amd.com if (clobber) { 183011886Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 183111886Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 183211886Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 183311886Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 183411886Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 183511886Sbrandon.potter@amd.com } 183611886Sbrandon.potter@amd.com } 183711886Sbrandon.potter@amd.com 183811886Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 183911886Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 184011886Sbrandon.potter@amd.com // fatal and bail out of the simulation. 184111886Sbrandon.potter@amd.com p->allocateMem(start, length, clobber); 184211886Sbrandon.potter@amd.com 184311886Sbrandon.potter@amd.com // Transfer content into target address space. 184411886Sbrandon.potter@amd.com PortProxy &tp = tc->getVirtProxy(); 184511886Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 184611886Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 184711886Sbrandon.potter@amd.com // with something like: 184811886Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 184911886Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 185011886Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 185111886Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 185211886Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 185311886Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 185411886Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 185511886Sbrandon.potter@amd.com } else { 185611886Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 185711886Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 185811886Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 185911886Sbrandon.potter@amd.com // the host into the target address space. 186011886Sbrandon.potter@amd.com struct stat file_stat; 186111886Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 186211886Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 186311886Sbrandon.potter@amd.com 186411886Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 186511886Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 186611886Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 186711886Sbrandon.potter@amd.com // on the specified offset into the file. 186811886Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 186911886Sbrandon.potter@amd.com length); 187011886Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 187111886Sbrandon.potter@amd.com 187211886Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 187311886Sbrandon.potter@amd.com munmap(pmap, length); 187411886Sbrandon.potter@amd.com 187511886Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 187611886Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 187711886Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 187811886Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 187911886Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 188011886Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 188111886Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 188211886Sbrandon.potter@amd.com if (interpreter) { 188311886Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 188411886Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 188511886Sbrandon.potter@amd.com 188611886Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 188711886Sbrandon.potter@amd.com 188811886Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 188911886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 189011886Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 189111886Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 189211886Sbrandon.potter@amd.com 189311886Sbrandon.potter@amd.com if (lib) { 189411886Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 189511886Sbrandon.potter@amd.com lib->textBase(), start); 189611886Sbrandon.potter@amd.com } 189711886Sbrandon.potter@amd.com } 189811886Sbrandon.potter@amd.com } 189911886Sbrandon.potter@amd.com 190011886Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 190111886Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 190211886Sbrandon.potter@amd.com // execution (hopefully). 190311886Sbrandon.potter@amd.com } 190411886Sbrandon.potter@amd.com 190511886Sbrandon.potter@amd.com return start; 190611886Sbrandon.potter@amd.com} 190711886Sbrandon.potter@amd.com 190811886Sbrandon.potter@amd.comtemplate <class OS> 190911886Sbrandon.potter@amd.comSyscallReturn 191011886Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, ThreadContext *tc) 191111886Sbrandon.potter@amd.com{ 191211886Sbrandon.potter@amd.com int index = 0; 191311886Sbrandon.potter@amd.com auto p = tc->getProcessPtr(); 191411886Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 1915378SN/A Addr bufPtr = p->getSyscallArg(tc, index); 1916360SN/A int nbytes = p->getSyscallArg(tc, index); 19171450SN/A int offset = p->getSyscallArg(tc, index); 191811851Sbrandon.potter@amd.com 19192680Sktlim@umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1920360SN/A if (!ffdp) 19216701Sgblack@eecs.umich.edu return -EBADF; 19226701Sgblack@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 19236701Sgblack@eecs.umich.edu 1924360SN/A BufferArg bufArg(bufPtr, nbytes); 19253670Sbinkertn@umich.edu bufArg.copyIn(tc->getVirtProxy()); 19263670Sbinkertn@umich.edu 1927360SN/A int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1928360SN/A 1929360SN/A return (bytes_written == -1) ? -errno : bytes_written; 1930360SN/A} 1931360SN/A 1932360SN/A/// Target mmap() handler. 1933360SN/Atemplate <class OS> 1934360SN/ASyscallReturn 1935360SN/AmmapFunc(SyscallDesc *desc, int num, ThreadContext *tc) 1936360SN/A{ 1937360SN/A return mmapImpl<OS>(desc, num, tc, false); 1938360SN/A} 1939360SN/A 1940360SN/A/// Target mmap2() handler. 1941360SN/Atemplate <class OS> 1942360SN/ASyscallReturn 1943360SN/Ammap2Func(SyscallDesc *desc, int num, ThreadContext *tc) 19443670Sbinkertn@umich.edu{ 19453670Sbinkertn@umich.edu return mmapImpl<OS>(desc, num, tc, true); 194610796Sbrandon.potter@amd.com} 19478737Skoansin.tan@gmail.com 19488737Skoansin.tan@gmail.com/// Target getrlimit() handler. 19493670Sbinkertn@umich.edutemplate <class OS> 19503670Sbinkertn@umich.eduSyscallReturn 19513670Sbinkertn@umich.edugetrlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 19523670Sbinkertn@umich.edu{ 19533670Sbinkertn@umich.edu int index = 0; 19543670Sbinkertn@umich.edu auto process = tc->getProcessPtr(); 19553670Sbinkertn@umich.edu unsigned resource = process->getSyscallArg(tc, index); 19563670Sbinkertn@umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 19573670Sbinkertn@umich.edu 19583670Sbinkertn@umich.edu switch (resource) { 19593670Sbinkertn@umich.edu case OS::TGT_RLIMIT_STACK: 19603670Sbinkertn@umich.edu // max stack size in bytes: make up a number (8MB for now) 19613670Sbinkertn@umich.edu rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 19628706Sandreas.hansson@arm.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1963360SN/A rlp->rlim_max = TheISA::htog(rlp->rlim_max); 19641458SN/A break; 1965360SN/A 1966360SN/A case OS::TGT_RLIMIT_DATA: 19676683Stjones1@inf.ed.ac.uk // max data segment size in bytes: make up a number 19686683Stjones1@inf.ed.ac.uk rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 19696683Stjones1@inf.ed.ac.uk rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 197011851Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 197111851Sbrandon.potter@amd.com break; 19726683Stjones1@inf.ed.ac.uk 19736701Sgblack@eecs.umich.edu default: 19746701Sgblack@eecs.umich.edu warn("getrlimit: unimplemented resource %d", resource); 19756683Stjones1@inf.ed.ac.uk return -EINVAL; 19766683Stjones1@inf.ed.ac.uk break; 19777823Ssteve.reinhardt@amd.com } 19786683Stjones1@inf.ed.ac.uk 19796683Stjones1@inf.ed.ac.uk rlp.copyOut(tc->getVirtProxy()); 19806683Stjones1@inf.ed.ac.uk return 0; 19816683Stjones1@inf.ed.ac.uk} 19826683Stjones1@inf.ed.ac.uk 19836683Stjones1@inf.ed.ac.uktemplate <class OS> 19848737Skoansin.tan@gmail.comSyscallReturn 19856683Stjones1@inf.ed.ac.ukprlimitFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 19866683Stjones1@inf.ed.ac.uk{ 19878706Sandreas.hansson@arm.com int index = 0; 19886683Stjones1@inf.ed.ac.uk auto process = tc->getProcessPtr(); 19896683Stjones1@inf.ed.ac.uk if (process->getSyscallArg(tc, index) != 0) 19906683Stjones1@inf.ed.ac.uk { 19916683Stjones1@inf.ed.ac.uk warn("prlimit: ignoring rlimits for nonzero pid"); 19922553SN/A return -EPERM; 19936684Stjones1@inf.ed.ac.uk } 19946684Stjones1@inf.ed.ac.uk int resource = process->getSyscallArg(tc, index); 19956684Stjones1@inf.ed.ac.uk Addr n = process->getSyscallArg(tc, index); 199611851Sbrandon.potter@amd.com if (n != 0) 19976684Stjones1@inf.ed.ac.uk warn("prlimit: ignoring new rlimit"); 19986684Stjones1@inf.ed.ac.uk Addr o = process->getSyscallArg(tc, index); 199910796Sbrandon.potter@amd.com if (o != 0) 20006684Stjones1@inf.ed.ac.uk { 20016684Stjones1@inf.ed.ac.uk TypedBufferArg<typename OS::rlimit> rlp(o); 20026701Sgblack@eecs.umich.edu switch (resource) { 20036701Sgblack@eecs.umich.edu case OS::TGT_RLIMIT_STACK: 200411321Ssteve.reinhardt@amd.com // max stack size in bytes: make up a number (8MB for now) 20056684Stjones1@inf.ed.ac.uk rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 20068737Skoansin.tan@gmail.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 20078852Sandreas.hansson@arm.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 20088852Sandreas.hansson@arm.com break; 20096684Stjones1@inf.ed.ac.uk case OS::TGT_RLIMIT_DATA: 20106684Stjones1@inf.ed.ac.uk // max data segment size in bytes: make up a number 20116684Stjones1@inf.ed.ac.uk rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 20122553SN/A rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 201311910SBrandon.Potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 201411910SBrandon.Potter@amd.com break; 201511910SBrandon.Potter@amd.com default: 201611910SBrandon.Potter@amd.com warn("prlimit: unimplemented resource %d", resource); 201711910SBrandon.Potter@amd.com return -EINVAL; 201811910SBrandon.Potter@amd.com break; 201911910SBrandon.Potter@amd.com } 202011910SBrandon.Potter@amd.com rlp.copyOut(tc->getVirtProxy()); 202111910SBrandon.Potter@amd.com } 202211910SBrandon.Potter@amd.com return 0; 202311910SBrandon.Potter@amd.com} 202411910SBrandon.Potter@amd.com 202511910SBrandon.Potter@amd.com/// Target clock_gettime() function. 202611910SBrandon.Potter@amd.comtemplate <class OS> 202711910SBrandon.Potter@amd.comSyscallReturn 202811910SBrandon.Potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, ThreadContext *tc) 202911910SBrandon.Potter@amd.com{ 203011910SBrandon.Potter@amd.com int index = 1; 203111910SBrandon.Potter@amd.com auto p = tc->getProcessPtr(); 203211910SBrandon.Potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 203311910SBrandon.Potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 203411910SBrandon.Potter@amd.com 203511910SBrandon.Potter@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 203611910SBrandon.Potter@amd.com tp->tv_sec += seconds_since_epoch; 203711910SBrandon.Potter@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 203811910SBrandon.Potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 203911910SBrandon.Potter@amd.com 204011910SBrandon.Potter@amd.com tp.copyOut(tc->getVirtProxy()); 204111910SBrandon.Potter@amd.com 204211910SBrandon.Potter@amd.com return 0; 204311910SBrandon.Potter@amd.com} 204411910SBrandon.Potter@amd.com 204511910SBrandon.Potter@amd.com/// Target clock_getres() function. 204611910SBrandon.Potter@amd.comtemplate <class OS> 204711910SBrandon.Potter@amd.comSyscallReturn 204811910SBrandon.Potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, ThreadContext *tc) 204911910SBrandon.Potter@amd.com{ 205011910SBrandon.Potter@amd.com int index = 1; 205111910SBrandon.Potter@amd.com auto p = tc->getProcessPtr(); 205211910SBrandon.Potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 205311910SBrandon.Potter@amd.com 205411910SBrandon.Potter@amd.com // Set resolution at ns, which is what clock_gettime() returns 205511910SBrandon.Potter@amd.com tp->tv_sec = 0; 205611910SBrandon.Potter@amd.com tp->tv_nsec = 1; 205711910SBrandon.Potter@amd.com 205811910SBrandon.Potter@amd.com tp.copyOut(tc->getVirtProxy()); 205911910SBrandon.Potter@amd.com 206011910SBrandon.Potter@amd.com return 0; 206111910SBrandon.Potter@amd.com} 20622553SN/A 20631354SN/A/// 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->getVirtProxy()); 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->getVirtProxy().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->getVirtProxy()); 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 PortProxy & mem_proxy = tc->getVirtProxy(); 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 PortProxy &mem_proxy, Addr mem_loc) 2136 { 2137 for (int inc = 0; ; inc++) { 2138 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 2139 b.copyIn(mem_proxy); 2140 2141 if (!*(Addr*)b.bufferPtr()) 2142 break; 2143 2144 vect.push_back(std::string()); 2145 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 2146 } 2147 }; 2148 2149 /** 2150 * Note that ProcessParams is generated by swig and there are no other 2151 * examples of how to create anything but this default constructor. The 2152 * fields are manually initialized instead of passing parameters to the 2153 * constructor. 2154 */ 2155 ProcessParams *pp = new ProcessParams(); 2156 pp->executable = path; 2157 Addr argv_mem_loc = p->getSyscallArg(tc, index); 2158 read_in(pp->cmd, mem_proxy, argv_mem_loc); 2159 Addr envp_mem_loc = p->getSyscallArg(tc, index); 2160 read_in(pp->env, mem_proxy, envp_mem_loc); 2161 pp->uid = p->uid(); 2162 pp->egid = p->egid(); 2163 pp->euid = p->euid(); 2164 pp->gid = p->gid(); 2165 pp->ppid = p->ppid(); 2166 pp->pid = p->pid(); 2167 pp->input.assign("cin"); 2168 pp->output.assign("cout"); 2169 pp->errout.assign("cerr"); 2170 pp->cwd.assign(p->tgtCwd); 2171 pp->system = p->system; 2172 /** 2173 * Prevent process object creation with identical PIDs (which will trip 2174 * a fatal check in Process constructor). The execve call is supposed to 2175 * take over the currently executing process' identity but replace 2176 * whatever it is doing with a new process image. Instead of hijacking 2177 * the process object in the simulator, we create a new process object 2178 * and bind to the previous process' thread below (hijacking the thread). 2179 */ 2180 p->system->PIDs.erase(p->pid()); 2181 Process *new_p = pp->create(); 2182 delete pp; 2183 2184 /** 2185 * Work through the file descriptor array and close any files marked 2186 * close-on-exec. 2187 */ 2188 new_p->fds = p->fds; 2189 for (int i = 0; i < new_p->fds->getSize(); i++) { 2190 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 2191 if (fdep && fdep->getCOE()) 2192 new_p->fds->closeFDEntry(i); 2193 } 2194 2195 *new_p->sigchld = true; 2196 2197 delete p; 2198 tc->clearArchRegs(); 2199 tc->setProcessPtr(new_p); 2200 new_p->assignThreadContext(tc->contextId()); 2201 new_p->initState(); 2202 tc->activate(); 2203 TheISA::PCState pcState = tc->pcState(); 2204 tc->setNPC(pcState.instAddr()); 2205 2206 desc->setFlags(SyscallDesc::SuppressReturnValue); 2207 return 0; 2208} 2209 2210/// Target getrusage() function. 2211template <class OS> 2212SyscallReturn 2213getrusageFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2214{ 2215 int index = 0; 2216 auto process = tc->getProcessPtr(); 2217 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 2218 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2219 2220 rup->ru_utime.tv_sec = 0; 2221 rup->ru_utime.tv_usec = 0; 2222 rup->ru_stime.tv_sec = 0; 2223 rup->ru_stime.tv_usec = 0; 2224 rup->ru_maxrss = 0; 2225 rup->ru_ixrss = 0; 2226 rup->ru_idrss = 0; 2227 rup->ru_isrss = 0; 2228 rup->ru_minflt = 0; 2229 rup->ru_majflt = 0; 2230 rup->ru_nswap = 0; 2231 rup->ru_inblock = 0; 2232 rup->ru_oublock = 0; 2233 rup->ru_msgsnd = 0; 2234 rup->ru_msgrcv = 0; 2235 rup->ru_nsignals = 0; 2236 rup->ru_nvcsw = 0; 2237 rup->ru_nivcsw = 0; 2238 2239 switch (who) { 2240 case OS::TGT_RUSAGE_SELF: 2241 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 2242 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 2243 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 2244 break; 2245 2246 case OS::TGT_RUSAGE_CHILDREN: 2247 // do nothing. We have no child processes, so they take no time. 2248 break; 2249 2250 default: 2251 // don't really handle THREAD or CHILDREN, but just warn and 2252 // plow ahead 2253 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 2254 who); 2255 } 2256 2257 rup.copyOut(tc->getVirtProxy()); 2258 2259 return 0; 2260} 2261 2262/// Target times() function. 2263template <class OS> 2264SyscallReturn 2265timesFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2266{ 2267 int index = 0; 2268 auto process = tc->getProcessPtr(); 2269 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 2270 2271 // Fill in the time structure (in clocks) 2272 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 2273 bufp->tms_utime = clocks; 2274 bufp->tms_stime = 0; 2275 bufp->tms_cutime = 0; 2276 bufp->tms_cstime = 0; 2277 2278 // Convert to host endianness 2279 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 2280 2281 // Write back 2282 bufp.copyOut(tc->getVirtProxy()); 2283 2284 // Return clock ticks since system boot 2285 return clocks; 2286} 2287 2288/// Target time() function. 2289template <class OS> 2290SyscallReturn 2291timeFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2292{ 2293 typename OS::time_t sec, usec; 2294 getElapsedTimeMicro(sec, usec); 2295 sec += seconds_since_epoch; 2296 2297 int index = 0; 2298 auto process = tc->getProcessPtr(); 2299 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2300 if (taddr != 0) { 2301 typename OS::time_t t = sec; 2302 t = TheISA::htog(t); 2303 PortProxy &p = tc->getVirtProxy(); 2304 p.writeBlob(taddr, &t, (int)sizeof(typename OS::time_t)); 2305 } 2306 return sec; 2307} 2308 2309template <class OS> 2310SyscallReturn 2311tgkillFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2312{ 2313 int index = 0; 2314 auto process = tc->getProcessPtr(); 2315 int tgid = process->getSyscallArg(tc, index); 2316 int tid = process->getSyscallArg(tc, index); 2317 int sig = process->getSyscallArg(tc, index); 2318 2319 /** 2320 * This system call is intended to allow killing a specific thread 2321 * within an arbitrary thread group if sanctioned with permission checks. 2322 * It's usually true that threads share the termination signal as pointed 2323 * out by the pthread_kill man page and this seems to be the intended 2324 * usage. Due to this being an emulated environment, assume the following: 2325 * Threads are allowed to call tgkill because the EUID for all threads 2326 * should be the same. There is no signal handling mechanism for kernel 2327 * registration of signal handlers since signals are poorly supported in 2328 * emulation mode. Since signal handlers cannot be registered, all 2329 * threads within in a thread group must share the termination signal. 2330 * We never exhaust PIDs so there's no chance of finding the wrong one 2331 * due to PID rollover. 2332 */ 2333 2334 System *sys = tc->getSystemPtr(); 2335 Process *tgt_proc = nullptr; 2336 for (int i = 0; i < sys->numContexts(); i++) { 2337 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2338 if (temp->pid() == tid) { 2339 tgt_proc = temp; 2340 break; 2341 } 2342 } 2343 2344 if (sig != 0 || sig != OS::TGT_SIGABRT) 2345 return -EINVAL; 2346 2347 if (tgt_proc == nullptr) 2348 return -ESRCH; 2349 2350 if (tgid != -1 && tgt_proc->tgid() != tgid) 2351 return -ESRCH; 2352 2353 if (sig == OS::TGT_SIGABRT) 2354 exitGroupFunc(desc, 252, tc); 2355 2356 return 0; 2357} 2358 2359template <class OS> 2360SyscallReturn 2361socketFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2362{ 2363 int index = 0; 2364 auto p = tc->getProcessPtr(); 2365 int domain = p->getSyscallArg(tc, index); 2366 int type = p->getSyscallArg(tc, index); 2367 int prot = p->getSyscallArg(tc, index); 2368 2369 int sim_fd = socket(domain, type, prot); 2370 if (sim_fd == -1) 2371 return -errno; 2372 2373 auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 2374 int tgt_fd = p->fds->allocFD(sfdp); 2375 2376 return tgt_fd; 2377} 2378 2379template <class OS> 2380SyscallReturn 2381socketpairFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2382{ 2383 int index = 0; 2384 auto p = tc->getProcessPtr(); 2385 int domain = p->getSyscallArg(tc, index); 2386 int type = p->getSyscallArg(tc, index); 2387 int prot = p->getSyscallArg(tc, index); 2388 Addr svPtr = p->getSyscallArg(tc, index); 2389 2390 BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 2391 int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 2392 if (status == -1) 2393 return -errno; 2394 2395 int *fds = (int *)svBuf.bufferPtr(); 2396 2397 auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 2398 fds[0] = p->fds->allocFD(sfdp1); 2399 auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 2400 fds[1] = p->fds->allocFD(sfdp2); 2401 svBuf.copyOut(tc->getVirtProxy()); 2402 2403 return status; 2404} 2405 2406template <class OS> 2407SyscallReturn 2408selectFunc(SyscallDesc *desc, int callnum, ThreadContext *tc) 2409{ 2410 int retval; 2411 2412 int index = 0; 2413 auto p = tc->getProcessPtr(); 2414 int nfds_t = p->getSyscallArg(tc, index); 2415 Addr fds_read_ptr = p->getSyscallArg(tc, index); 2416 Addr fds_writ_ptr = p->getSyscallArg(tc, index); 2417 Addr fds_excp_ptr = p->getSyscallArg(tc, index); 2418 Addr time_val_ptr = p->getSyscallArg(tc, index); 2419 2420 TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 2421 TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 2422 TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 2423 TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 2424 2425 /** 2426 * Host fields. Notice that these use the definitions from the system 2427 * headers instead of the gem5 headers and libraries. If the host and 2428 * target have different header file definitions, this will not work. 2429 */ 2430 fd_set rd_h; 2431 FD_ZERO(&rd_h); 2432 fd_set wr_h; 2433 FD_ZERO(&wr_h); 2434 fd_set ex_h; 2435 FD_ZERO(&ex_h); 2436 2437 /** 2438 * Copy in the fd_set from the target. 2439 */ 2440 if (fds_read_ptr) 2441 rd_t.copyIn(tc->getVirtProxy()); 2442 if (fds_writ_ptr) 2443 wr_t.copyIn(tc->getVirtProxy()); 2444 if (fds_excp_ptr) 2445 ex_t.copyIn(tc->getVirtProxy()); 2446 2447 /** 2448 * We need to translate the target file descriptor set into a host file 2449 * descriptor set. This involves both our internal process fd array 2450 * and the fd_set defined in Linux header files. The nfds field also 2451 * needs to be updated as it will be only target specific after 2452 * retrieving it from the target; the nfds value is expected to be the 2453 * highest file descriptor that needs to be checked, so we need to extend 2454 * it out for nfds_h when we do the update. 2455 */ 2456 int nfds_h = 0; 2457 std::map<int, int> trans_map; 2458 auto try_add_host_set = [&](fd_set *tgt_set_entry, 2459 fd_set *hst_set_entry, 2460 int iter) -> bool 2461 { 2462 /** 2463 * By this point, we know that we are looking at a valid file 2464 * descriptor set on the target. We need to check if the target file 2465 * descriptor value passed in as iter is part of the set. 2466 */ 2467 if (FD_ISSET(iter, tgt_set_entry)) { 2468 /** 2469 * We know that the target file descriptor belongs to the set, 2470 * but we do not yet know if the file descriptor is valid or 2471 * that we have a host mapping. Check that now. 2472 */ 2473 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 2474 if (!hbfdp) 2475 return true; 2476 auto sim_fd = hbfdp->getSimFD(); 2477 2478 /** 2479 * Add the sim_fd to tgt_fd translation into trans_map for use 2480 * later when we need to zero the target fd_set structures and 2481 * then update them with hits returned from the host select call. 2482 */ 2483 trans_map[sim_fd] = iter; 2484 2485 /** 2486 * We know that the host file descriptor exists so now we check 2487 * if we need to update the max count for nfds_h before passing 2488 * the duplicated structure into the host. 2489 */ 2490 nfds_h = std::max(nfds_h - 1, sim_fd + 1); 2491 2492 /** 2493 * Add the host file descriptor to the set that we are going to 2494 * pass into the host. 2495 */ 2496 FD_SET(sim_fd, hst_set_entry); 2497 } 2498 return false; 2499 }; 2500 2501 for (int i = 0; i < nfds_t; i++) { 2502 if (fds_read_ptr) { 2503 bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 2504 if (ebadf) return -EBADF; 2505 } 2506 if (fds_writ_ptr) { 2507 bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 2508 if (ebadf) return -EBADF; 2509 } 2510 if (fds_excp_ptr) { 2511 bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 2512 if (ebadf) return -EBADF; 2513 } 2514 } 2515 2516 if (time_val_ptr) { 2517 /** 2518 * It might be possible to decrement the timeval based on some 2519 * derivation of wall clock determined from elapsed simulator ticks 2520 * but that seems like overkill. Rather, we just set the timeval with 2521 * zero timeout. (There is no reason to block during the simulation 2522 * as it only decreases simulator performance.) 2523 */ 2524 tp->tv_sec = 0; 2525 tp->tv_usec = 0; 2526 2527 retval = select(nfds_h, 2528 fds_read_ptr ? &rd_h : nullptr, 2529 fds_writ_ptr ? &wr_h : nullptr, 2530 fds_excp_ptr ? &ex_h : nullptr, 2531 (timeval*)&*tp); 2532 } else { 2533 /** 2534 * If the timeval pointer is null, setup a new timeval structure to 2535 * pass into the host select call. Unfortunately, we will need to 2536 * manually check the return value and throw a retry fault if the 2537 * return value is zero. Allowing the system call to block will 2538 * likely deadlock the event queue. 2539 */ 2540 struct timeval tv = { 0, 0 }; 2541 2542 retval = select(nfds_h, 2543 fds_read_ptr ? &rd_h : nullptr, 2544 fds_writ_ptr ? &wr_h : nullptr, 2545 fds_excp_ptr ? &ex_h : nullptr, 2546 &tv); 2547 2548 if (retval == 0) { 2549 /** 2550 * If blocking indefinitely, check the signal list to see if a 2551 * signal would break the poll out of the retry cycle and try to 2552 * return the signal interrupt instead. 2553 */ 2554 for (auto sig : tc->getSystemPtr()->signalList) 2555 if (sig.receiver == p) 2556 return -EINTR; 2557 return SyscallReturn::retry(); 2558 } 2559 } 2560 2561 if (retval == -1) 2562 return -errno; 2563 2564 FD_ZERO((fd_set*)&*rd_t); 2565 FD_ZERO((fd_set*)&*wr_t); 2566 FD_ZERO((fd_set*)&*ex_t); 2567 2568 /** 2569 * We need to translate the host file descriptor set into a target file 2570 * descriptor set. This involves both our internal process fd array 2571 * and the fd_set defined in header files. 2572 */ 2573 for (int i = 0; i < nfds_h; i++) { 2574 if (fds_read_ptr) { 2575 if (FD_ISSET(i, &rd_h)) 2576 FD_SET(trans_map[i], (fd_set*)&*rd_t); 2577 } 2578 2579 if (fds_writ_ptr) { 2580 if (FD_ISSET(i, &wr_h)) 2581 FD_SET(trans_map[i], (fd_set*)&*wr_t); 2582 } 2583 2584 if (fds_excp_ptr) { 2585 if (FD_ISSET(i, &ex_h)) 2586 FD_SET(trans_map[i], (fd_set*)&*ex_t); 2587 } 2588 } 2589 2590 if (fds_read_ptr) 2591 rd_t.copyOut(tc->getVirtProxy()); 2592 if (fds_writ_ptr) 2593 wr_t.copyOut(tc->getVirtProxy()); 2594 if (fds_excp_ptr) 2595 ex_t.copyOut(tc->getVirtProxy()); 2596 if (time_val_ptr) 2597 tp.copyOut(tc->getVirtProxy()); 2598 2599 return retval; 2600} 2601 2602template <class OS> 2603SyscallReturn 2604readFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2605{ 2606 int index = 0; 2607 auto p = tc->getProcessPtr(); 2608 int tgt_fd = p->getSyscallArg(tc, index); 2609 Addr buf_ptr = p->getSyscallArg(tc, index); 2610 int nbytes = p->getSyscallArg(tc, index); 2611 2612 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2613 if (!hbfdp) 2614 return -EBADF; 2615 int sim_fd = hbfdp->getSimFD(); 2616 2617 struct pollfd pfd; 2618 pfd.fd = sim_fd; 2619 pfd.events = POLLIN | POLLPRI; 2620 if ((poll(&pfd, 1, 0) == 0) 2621 && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2622 return SyscallReturn::retry(); 2623 2624 BufferArg buf_arg(buf_ptr, nbytes); 2625 int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 2626 2627 if (bytes_read > 0) 2628 buf_arg.copyOut(tc->getVirtProxy()); 2629 2630 return (bytes_read == -1) ? -errno : bytes_read; 2631} 2632 2633template <class OS> 2634SyscallReturn 2635writeFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2636{ 2637 int index = 0; 2638 auto p = tc->getProcessPtr(); 2639 int tgt_fd = p->getSyscallArg(tc, index); 2640 Addr buf_ptr = p->getSyscallArg(tc, index); 2641 int nbytes = p->getSyscallArg(tc, index); 2642 2643 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2644 if (!hbfdp) 2645 return -EBADF; 2646 int sim_fd = hbfdp->getSimFD(); 2647 2648 BufferArg buf_arg(buf_ptr, nbytes); 2649 buf_arg.copyIn(tc->getVirtProxy()); 2650 2651 struct pollfd pfd; 2652 pfd.fd = sim_fd; 2653 pfd.events = POLLOUT; 2654 2655 /** 2656 * We don't want to poll on /dev/random. The kernel will not enable the 2657 * file descriptor for writing unless the entropy in the system falls 2658 * below write_wakeup_threshold. This is not guaranteed to happen 2659 * depending on host settings. 2660 */ 2661 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 2662 if (ffdp && (ffdp->getFileName() != "/dev/random")) { 2663 if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 2664 return SyscallReturn::retry(); 2665 } 2666 2667 int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 2668 2669 if (bytes_written != -1) 2670 fsync(sim_fd); 2671 2672 return (bytes_written == -1) ? -errno : bytes_written; 2673} 2674 2675template <class OS> 2676SyscallReturn 2677wait4Func(SyscallDesc *desc, int num, ThreadContext *tc) 2678{ 2679 int index = 0; 2680 auto p = tc->getProcessPtr(); 2681 pid_t pid = p->getSyscallArg(tc, index); 2682 Addr statPtr = p->getSyscallArg(tc, index); 2683 int options = p->getSyscallArg(tc, index); 2684 Addr rusagePtr = p->getSyscallArg(tc, index); 2685 2686 if (rusagePtr) 2687 DPRINTF_SYSCALL(Verbose, "wait4: rusage pointer provided %lx, however " 2688 "functionality not supported. Ignoring rusage pointer.\n", 2689 rusagePtr); 2690 2691 /** 2692 * Currently, wait4 is only implemented so that it will wait for children 2693 * exit conditions which are denoted by a SIGCHLD signals posted into the 2694 * system signal list. We return no additional information via any of the 2695 * parameters supplied to wait4. If nothing is found in the system signal 2696 * list, we will wait indefinitely for SIGCHLD to post by retrying the 2697 * call. 2698 */ 2699 System *sysh = tc->getSystemPtr(); 2700 std::list<BasicSignal>::iterator iter; 2701 for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 2702 if (iter->receiver == p) { 2703 if (pid < -1) { 2704 if ((iter->sender->pgid() == -pid) 2705 && (iter->signalValue == OS::TGT_SIGCHLD)) 2706 goto success; 2707 } else if (pid == -1) { 2708 if (iter->signalValue == OS::TGT_SIGCHLD) 2709 goto success; 2710 } else if (pid == 0) { 2711 if ((iter->sender->pgid() == p->pgid()) 2712 && (iter->signalValue == OS::TGT_SIGCHLD)) 2713 goto success; 2714 } else { 2715 if ((iter->sender->pid() == pid) 2716 && (iter->signalValue == OS::TGT_SIGCHLD)) 2717 goto success; 2718 } 2719 } 2720 } 2721 2722 return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 2723 2724success: 2725 // Set status to EXITED for WIFEXITED evaluations. 2726 const int EXITED = 0; 2727 BufferArg statusBuf(statPtr, sizeof(int)); 2728 *(int *)statusBuf.bufferPtr() = EXITED; 2729 statusBuf.copyOut(tc->getVirtProxy()); 2730 2731 // Return the child PID. 2732 pid_t retval = iter->sender->pid(); 2733 sysh->signalList.erase(iter); 2734 return retval; 2735} 2736 2737template <class OS> 2738SyscallReturn 2739acceptFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2740{ 2741 struct sockaddr sa; 2742 socklen_t addrLen; 2743 int host_fd; 2744 int index = 0; 2745 auto p = tc->getProcessPtr(); 2746 int tgt_fd = p->getSyscallArg(tc, index); 2747 Addr addrPtr = p->getSyscallArg(tc, index); 2748 Addr lenPtr = p->getSyscallArg(tc, index); 2749 2750 BufferArg *lenBufPtr = nullptr; 2751 BufferArg *addrBufPtr = nullptr; 2752 2753 auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 2754 if (!sfdp) 2755 return -EBADF; 2756 int sim_fd = sfdp->getSimFD(); 2757 2758 /** 2759 * We poll the socket file descriptor first to guarantee that we do not 2760 * block on our accept call. The socket can be opened without the 2761 * non-blocking flag (it blocks). This will cause deadlocks between 2762 * communicating processes. 2763 */ 2764 struct pollfd pfd; 2765 pfd.fd = sim_fd; 2766 pfd.events = POLLIN | POLLPRI; 2767 if ((poll(&pfd, 1, 0) == 0) 2768 && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2769 return SyscallReturn::retry(); 2770 2771 if (lenPtr) { 2772 lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 2773 lenBufPtr->copyIn(tc->getVirtProxy()); 2774 memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 2775 sizeof(socklen_t)); 2776 } 2777 2778 if (addrPtr) { 2779 addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 2780 addrBufPtr->copyIn(tc->getVirtProxy()); 2781 memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 2782 sizeof(struct sockaddr)); 2783 } 2784 2785 host_fd = accept(sim_fd, &sa, &addrLen); 2786 2787 if (host_fd == -1) 2788 return -errno; 2789 2790 if (addrPtr) { 2791 memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 2792 addrBufPtr->copyOut(tc->getVirtProxy()); 2793 delete(addrBufPtr); 2794 } 2795 2796 if (lenPtr) { 2797 *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 2798 lenBufPtr->copyOut(tc->getVirtProxy()); 2799 delete(lenBufPtr); 2800 } 2801 2802 auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 2803 sfdp->_type, sfdp->_protocol); 2804 return p->fds->allocFD(afdp); 2805} 2806 2807/// Target eventfd() function. 2808template <class OS> 2809SyscallReturn 2810eventfdFunc(SyscallDesc *desc, int num, ThreadContext *tc) 2811{ 2812#if defined(__linux__) 2813 int index = 0; 2814 auto p = tc->getProcessPtr(); 2815 unsigned initval = p->getSyscallArg(tc, index); 2816 int in_flags = p->getSyscallArg(tc, index); 2817 2818 int sim_fd = eventfd(initval, in_flags); 2819 if (sim_fd == -1) 2820 return -errno; 2821 2822 bool cloexec = in_flags & OS::TGT_O_CLOEXEC; 2823 2824 int flags = cloexec ? OS::TGT_O_CLOEXEC : 0; 2825 flags |= (in_flags & OS::TGT_O_NONBLOCK) ? OS::TGT_O_NONBLOCK : 0; 2826 2827 auto hbfdp = std::make_shared<HBFDEntry>(flags, sim_fd, cloexec); 2828 int tgt_fd = p->fds->allocFD(hbfdp); 2829 return tgt_fd; 2830#else 2831 warnUnsupportedOS("eventfd"); 2832 return -1; 2833#endif 2834} 2835 2836#endif // __SIM_SYSCALL_EMUL_HH__ 2837