syscall_emul.hh revision 12593
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#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 5712018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__NetBSD__)) 5812018Sandreas.sandberg@arm.com#define NO_STATFS 1 5912018Sandreas.sandberg@arm.com#else 6012018Sandreas.sandberg@arm.com#define NO_STATFS 0 6112018Sandreas.sandberg@arm.com#endif 6211799Sbrandon.potter@amd.com 6312018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 6412018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__NetBSD__)) 6512018Sandreas.sandberg@arm.com#define NO_FALLOCATE 1 6612018Sandreas.sandberg@arm.com#else 6712018Sandreas.sandberg@arm.com#define NO_FALLOCATE 0 6812018Sandreas.sandberg@arm.com#endif 6911799Sbrandon.potter@amd.com 70360SN/A/// 71360SN/A/// @file syscall_emul.hh 72360SN/A/// 73360SN/A/// This file defines objects used to emulate syscalls from the target 74360SN/A/// application on the host machine. 75360SN/A 761809SN/A#ifdef __CYGWIN32__ 7711800Sbrandon.potter@amd.com#include <sys/fcntl.h> 7811392Sbrandon.potter@amd.com 791809SN/A#endif 8011392Sbrandon.potter@amd.com#include <fcntl.h> 8111383Sbrandon.potter@amd.com#include <sys/mman.h> 823113Sgblack@eecs.umich.edu#include <sys/stat.h> 8311799Sbrandon.potter@amd.com#if (NO_STATFS == 0) 8411759Sbrandon.potter@amd.com#include <sys/statfs.h> 8511812Sbaz21@cam.ac.uk#else 8611812Sbaz21@cam.ac.uk#include <sys/mount.h> 8711799Sbrandon.potter@amd.com#endif 888229Snate@binkert.org#include <sys/time.h> 898229Snate@binkert.org#include <sys/uio.h> 9011594Santhony.gutierrez@amd.com#include <unistd.h> 917075Snate@binkert.org 928229Snate@binkert.org#include <cerrno> 9311856Sbrandon.potter@amd.com#include <memory> 947075Snate@binkert.org#include <string> 95360SN/A 9612461Sgabeblack@google.com#include "arch/generic/tlb.hh" 9711886Sbrandon.potter@amd.com#include "arch/utility.hh" 9811800Sbrandon.potter@amd.com#include "base/intmath.hh" 9911392Sbrandon.potter@amd.com#include "base/loader/object_file.hh" 10012334Sgabeblack@google.com#include "base/logging.hh" 1011354SN/A#include "base/trace.hh" 1026216Snate@binkert.org#include "base/types.hh" 1036658Snate@binkert.org#include "config/the_isa.hh" 1042474SN/A#include "cpu/base.hh" 1052680Sktlim@umich.edu#include "cpu/thread_context.hh" 1068229Snate@binkert.org#include "mem/page_table.hh" 10711886Sbrandon.potter@amd.com#include "params/Process.hh" 10810496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh" 10911911SBrandon.Potter@amd.com#include "sim/futex_map.hh" 1108229Snate@binkert.org#include "sim/process.hh" 11111794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh" 11211886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh" 11310497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh" 11411794Sbrandon.potter@amd.com#include "sim/syscall_return.hh" 115360SN/A 116360SN/A////////////////////////////////////////////////////////////////////// 117360SN/A// 118360SN/A// The following emulation functions are generic enough that they 119360SN/A// don't need to be recompiled for different emulated OS's. They are 120360SN/A// defined in sim/syscall_emul.cc. 121360SN/A// 122360SN/A////////////////////////////////////////////////////////////////////// 123360SN/A 124360SN/A 125378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1261706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 12711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 128378SN/A 129378SN/A/// Handler for unimplemented syscalls that we never intend to 130378SN/A/// implement (signal handling, etc.) and should not affect the correct 131378SN/A/// behavior of the program. Print a warning only if the appropriate 132378SN/A/// trace flag is enabled. Return success to the target program. 1331706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 13411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 135360SN/A 13611760Sbrandon.potter@amd.com// Target fallocateFunc() handler. 13711760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 13811851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 13911760Sbrandon.potter@amd.com 1406109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1411706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 14211851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 143378SN/A 1446109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1456109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 14611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1476109Ssanchezd@stanford.edu 14811886Sbrandon.potter@amd.com/// Target set_tid_address() handler. 14911886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 15011886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 15111886Sbrandon.potter@amd.com 152378SN/A/// Target getpagesize() handler. 1531706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 15411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 155378SN/A 1565748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 1575748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 15811851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 159378SN/A 160378SN/A/// Target close() handler. 1611706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 16211851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 163378SN/A 16411886Sbrandon.potter@amd.com// Target read() handler. 1651706SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 16611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 167378SN/A 168378SN/A/// Target write() handler. 1691706SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 17011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 171378SN/A 172378SN/A/// Target lseek() handler. 1731706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 17411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 175378SN/A 1764118Sgblack@eecs.umich.edu/// Target _llseek() handler. 1774118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 17811851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1794118Sgblack@eecs.umich.edu 180378SN/A/// Target munmap() handler. 1811706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 18211851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 183378SN/A 184378SN/A/// Target gethostname() handler. 1851706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 18611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 187360SN/A 1885513SMichael.Adler@intel.com/// Target getcwd() handler. 1895513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 19011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1915513SMichael.Adler@intel.com 19210203SAli.Saidi@ARM.com/// Target readlink() handler. 19310203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 19510203SAli.Saidi@ARM.com int index = 0); 1965513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1985513SMichael.Adler@intel.com 199511SN/A/// Target unlink() handler. 20010633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 20111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 20210633Smichaelupton@gmail.com int index); 2031706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 20411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 205511SN/A 20612795Smattdsinclair@gmail.com/// Target mkdir() handler. 20712795Smattdsinclair@gmail.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 20812795Smattdsinclair@gmail.com Process *p, ThreadContext *tc); 20912795Smattdsinclair@gmail.com 21012796Smattdsinclair@gmail.com/// Target rename() handler. 21112796Smattdsinclair@gmail.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 21212796Smattdsinclair@gmail.com Process *p, ThreadContext *tc); 21312796Smattdsinclair@gmail.com 2145513SMichael.Adler@intel.com 2155513SMichael.Adler@intel.com/// Target truncate() handler. 21611851Sbrandon.potter@amd.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2175513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 21813031Sbrandon.potter@amd.com 21913031Sbrandon.potter@amd.com 22013031Sbrandon.potter@amd.com/// Target ftruncate() handler. 22113031Sbrandon.potter@amd.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 22213031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22313031Sbrandon.potter@amd.com 22413031Sbrandon.potter@amd.com 22513031Sbrandon.potter@amd.com/// Target truncate64() handler. 22613031Sbrandon.potter@amd.comSyscallReturn truncate64Func(SyscallDesc *desc, int num, 22713031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22813031Sbrandon.potter@amd.com 22913031Sbrandon.potter@amd.com/// Target ftruncate64() handler. 230511SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2311706SN/A Process *p, ThreadContext *tc); 23211851Sbrandon.potter@amd.com 2331706SN/A 2341706SN/A/// Target umask() handler. 2351706SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num, 2361706SN/A Process *p, ThreadContext *tc); 23711851Sbrandon.potter@amd.com 2381706SN/A/// Target gettid() handler. 2391706SN/ASyscallReturn gettidFunc(SyscallDesc *desc, int num, 2401706SN/A Process *p, ThreadContext *tc); 2411706SN/A 24211851Sbrandon.potter@amd.com/// Target chown() handler. 2431706SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 244511SN/A Process *p, ThreadContext *tc); 2456703Svince@csl.cornell.edu 2466703Svince@csl.cornell.edu/// Target setpgid() handler. 24711851Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2486703Svince@csl.cornell.edu Process *p, ThreadContext *tc); 2496685Stjones1@inf.ed.ac.uk 2506685Stjones1@inf.ed.ac.uk/// Target fchown() handler. 25111851Sbrandon.potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2526685Stjones1@inf.ed.ac.uk Process *p, ThreadContext *tc); 2536685Stjones1@inf.ed.ac.uk 2545513SMichael.Adler@intel.com/// Target dup() handler. 2555513SMichael.Adler@intel.comSyscallReturn dupFunc(SyscallDesc *desc, int num, 25611851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2575513SMichael.Adler@intel.com 25811885Sbrandon.potter@amd.com/// Target dup2() handler. 25911885Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 26011885Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2615513SMichael.Adler@intel.com 2621999SN/A/// Target fcntl() handler. 2631999SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 26411851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2651999SN/A 26611885Sbrandon.potter@amd.com/// Target fcntl64() handler. 26711885Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 26811885Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2691999SN/A 2701999SN/A/// Target setuid() handler. 2711999SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 27211851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2731999SN/A 2743079Sstever@eecs.umich.edu/// Target pipe() handler. 2753079Sstever@eecs.umich.eduSyscallReturn pipeFunc(SyscallDesc *desc, int num, 27611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2773079Sstever@eecs.umich.edu 27811908SBrandon.Potter@amd.com/// Internal pipe() handler. 27911908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 28011908SBrandon.Potter@amd.com ThreadContext *tc, bool pseudoPipe); 28111908SBrandon.Potter@amd.com 28211875Sbrandon.potter@amd.com/// Target getpid() handler. 2832093SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 28411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2852093SN/A 2862687Sksewell@umich.edu/// Target getuid() handler. 2872687Sksewell@umich.eduSyscallReturn getuidFunc(SyscallDesc *desc, int num, 28811851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2892687Sksewell@umich.edu 2902238SN/A/// Target getgid() handler. 2912238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 29211851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2932238SN/A 29411908SBrandon.Potter@amd.com/// Target getppid() handler. 29511908SBrandon.Potter@amd.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, 29611908SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 29711908SBrandon.Potter@amd.com 29811908SBrandon.Potter@amd.com/// Target geteuid() handler. 29911908SBrandon.Potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 30011908SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 30111908SBrandon.Potter@amd.com 3022238SN/A/// Target getegid() handler. 3032238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 30411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3052238SN/A 30613031Sbrandon.potter@amd.com/// Target access() handler 30713031Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 30813031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 30913031Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 31013031Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 3112238SN/A int index); 31211851Sbrandon.potter@amd.com 3132238SN/A/// Futex system call 3142238SN/A/// Implemented by Daniel Sanchez 3152238SN/A/// Used by printf's in multi-threaded apps 31611851Sbrandon.potter@amd.comtemplate <class OS> 3172238SN/ASyscallReturn 3182238SN/AfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3192238SN/A ThreadContext *tc) 32011851Sbrandon.potter@amd.com{ 3212238SN/A using namespace std; 3222238SN/A 3232238SN/A int index = 0; 32411851Sbrandon.potter@amd.com Addr uaddr = process->getSyscallArg(tc, index); 3252238SN/A int op = process->getSyscallArg(tc, index); 3262238SN/A int val = process->getSyscallArg(tc, index); 3272238SN/A 32811851Sbrandon.potter@amd.com /* 3292238SN/A * Unsupported option that does not affect the correctness of the 3309455Smitch.hayenga+gem5@gmail.com * application. This is a performance optimization utilized by Linux. 3319455Smitch.hayenga+gem5@gmail.com */ 33211851Sbrandon.potter@amd.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 33310203SAli.Saidi@ARM.com 33411851Sbrandon.potter@amd.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 33511851Sbrandon.potter@amd.com 3369455Smitch.hayenga+gem5@gmail.com if (OS::TGT_FUTEX_WAIT == op) { 3379112Smarc.orr@gmail.com // Ensure futex system call accessed atomically. 33811906SBrandon.Potter@amd.com BufferArg buf(uaddr, sizeof(int)); 33911906SBrandon.Potter@amd.com buf.copyIn(tc->getMemProxy()); 3409112Smarc.orr@gmail.com int mem_val = *(int*)buf.bufferPtr(); 3419112Smarc.orr@gmail.com 34211851Sbrandon.potter@amd.com /* 3439112Smarc.orr@gmail.com * The value in memory at uaddr is not equal with the expected val 3449112Smarc.orr@gmail.com * (a different thread must have changed it before the system call was 34511911SBrandon.Potter@amd.com * invoked). In this case, we need to throw an error. 3469112Smarc.orr@gmail.com */ 34711911SBrandon.Potter@amd.com if (val != mem_val) 34811911SBrandon.Potter@amd.com return -OS::TGT_EWOULDBLOCK; 34911911SBrandon.Potter@amd.com 35011911SBrandon.Potter@amd.com futex_map.suspend(uaddr, process->tgid(), tc); 3519112Smarc.orr@gmail.com 35211911SBrandon.Potter@amd.com return 0; 35311911SBrandon.Potter@amd.com } else if (OS::TGT_FUTEX_WAKE == op) { 35411911SBrandon.Potter@amd.com return futex_map.wakeup(uaddr, process->tgid(), val); 35511911SBrandon.Potter@amd.com } 3569238Slluc.alvarez@bsc.es 3579112Smarc.orr@gmail.com warn("futex: op %d not implemented; ignoring.", op); 35811911SBrandon.Potter@amd.com return -ENOSYS; 3599112Smarc.orr@gmail.com} 36011911SBrandon.Potter@amd.com 36111911SBrandon.Potter@amd.com 36211911SBrandon.Potter@amd.com/// Pseudo Funcs - These functions use a different return convension, 36311911SBrandon.Potter@amd.com/// returning a second value in a register other than the normal return register 36411911SBrandon.Potter@amd.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3659112Smarc.orr@gmail.com Process *process, ThreadContext *tc); 36611911SBrandon.Potter@amd.com 36711911SBrandon.Potter@amd.com/// Target getpidPseudo() handler. 36811911SBrandon.Potter@amd.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 36911911SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 37011911SBrandon.Potter@amd.com 37111911SBrandon.Potter@amd.com/// Target getuidPseudo() handler. 3729112Smarc.orr@gmail.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3739112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 37411911SBrandon.Potter@amd.com 37511911SBrandon.Potter@amd.com/// Target getgidPseudo() handler. 3769112Smarc.orr@gmail.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 37711911SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 37811911SBrandon.Potter@amd.com 3799112Smarc.orr@gmail.com 3809112Smarc.orr@gmail.com/// A readable name for 1,000,000, for converting microseconds to seconds. 38111911SBrandon.Potter@amd.comconst int one_million = 1000000; 38211911SBrandon.Potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 3839112Smarc.orr@gmail.comconst int one_billion = 1000000000; 3849112Smarc.orr@gmail.com 3852238SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3862238SN/A/// by my reckoning. We want to keep this a constant (not use the 3872238SN/A/// real-world time) to keep simulations repeatable. 3882238SN/Aconst unsigned seconds_since_epoch = 1000000000; 38911851Sbrandon.potter@amd.com 3902238SN/A/// Helper function to convert current elapsed time to seconds and 3912238SN/A/// microseconds. 3922238SN/Atemplate <class T1, class T2> 39311851Sbrandon.potter@amd.comvoid 3942238SN/AgetElapsedTimeMicro(T1 &sec, T2 &usec) 3952238SN/A{ 3962238SN/A uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 39711851Sbrandon.potter@amd.com sec = elapsed_usecs / one_million; 3982238SN/A usec = elapsed_usecs % one_million; 3992238SN/A} 4002238SN/A 40111851Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and 4022238SN/A/// nanoseconds. 4032238SN/Atemplate <class T1, class T2> 4041354SN/Avoid 4051354SN/AgetElapsedTimeNano(T1 &sec, T2 &nsec) 40610796Sbrandon.potter@amd.com{ 40710796Sbrandon.potter@amd.com uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 4081354SN/A sec = elapsed_nsecs / one_billion; 4091354SN/A nsec = elapsed_nsecs % one_billion; 4101354SN/A} 4111354SN/A 4121354SN/A////////////////////////////////////////////////////////////////////// 4131354SN/A// 4141354SN/A// The following emulation functions are generic, but need to be 4151354SN/A// templated to account for differences in types, constants, etc. 4161354SN/A// 4171354SN/A////////////////////////////////////////////////////////////////////// 41810796Sbrandon.potter@amd.com 4191354SN/A typedef struct statfs hst_statfs; 42010796Sbrandon.potter@amd.com#if NO_STAT64 4211354SN/A typedef struct stat hst_stat; 4221354SN/A typedef struct stat hst_stat64; 4231354SN/A#else 4241354SN/A typedef struct stat hst_stat; 42510796Sbrandon.potter@amd.com typedef struct stat64 hst_stat64; 42610796Sbrandon.potter@amd.com#endif 42710796Sbrandon.potter@amd.com 42810796Sbrandon.potter@amd.com//// Helper function to convert a host stat buffer to a target stat 42910796Sbrandon.potter@amd.com//// buffer. Also copies the target buffer out to the simulated 43010796Sbrandon.potter@amd.com//// memory space. Used by stat(), fstat(), and lstat(). 43110796Sbrandon.potter@amd.com 43210796Sbrandon.potter@amd.comtemplate <typename target_stat, typename host_stat> 43310796Sbrandon.potter@amd.comvoid 43410796Sbrandon.potter@amd.comconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 43510796Sbrandon.potter@amd.com{ 436360SN/A using namespace TheISA; 437360SN/A 438360SN/A if (fakeTTY) 439360SN/A tgt->st_dev = 0xA; 440360SN/A else 441360SN/A tgt->st_dev = host->st_dev; 442360SN/A tgt->st_dev = TheISA::htog(tgt->st_dev); 44311759Sbrandon.potter@amd.com tgt->st_ino = host->st_ino; 4443113Sgblack@eecs.umich.edu tgt->st_ino = TheISA::htog(tgt->st_ino); 4453113Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4463113Sgblack@eecs.umich.edu if (fakeTTY) { 4473113Sgblack@eecs.umich.edu // Claim to be a character device 4483113Sgblack@eecs.umich.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4493113Sgblack@eecs.umich.edu tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4503113Sgblack@eecs.umich.edu } 4513113Sgblack@eecs.umich.edu tgt->st_mode = TheISA::htog(tgt->st_mode); 4523113Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4533113Sgblack@eecs.umich.edu tgt->st_nlink = TheISA::htog(tgt->st_nlink); 4543113Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4553113Sgblack@eecs.umich.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 4563113Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 45712032Sandreas.sandberg@arm.com tgt->st_gid = TheISA::htog(tgt->st_gid); 4583113Sgblack@eecs.umich.edu if (fakeTTY) 4593113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4604189Sgblack@eecs.umich.edu else 4614189Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4623113Sgblack@eecs.umich.edu tgt->st_rdev = TheISA::htog(tgt->st_rdev); 4633113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4643113Sgblack@eecs.umich.edu tgt->st_size = TheISA::htog(tgt->st_size); 4653113Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4668737Skoansin.tan@gmail.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4673113Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4688737Skoansin.tan@gmail.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4693277Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4705515SMichael.Adler@intel.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4715515SMichael.Adler@intel.com // Force the block size to be 8KB. This helps to ensure buffered io works 4725515SMichael.Adler@intel.com // consistently across different hosts. 4735515SMichael.Adler@intel.com tgt->st_blksize = 0x2000; 4745515SMichael.Adler@intel.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4758737Skoansin.tan@gmail.com tgt->st_blocks = host->st_blocks; 4763277Sgblack@eecs.umich.edu tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4778737Skoansin.tan@gmail.com} 4783277Sgblack@eecs.umich.edu 4798737Skoansin.tan@gmail.com// Same for stat64 4803277Sgblack@eecs.umich.edu 4818737Skoansin.tan@gmail.comtemplate <typename target_stat, typename host_stat64> 4823113Sgblack@eecs.umich.eduvoid 4833113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4843113Sgblack@eecs.umich.edu{ 4853113Sgblack@eecs.umich.edu using namespace TheISA; 4868737Skoansin.tan@gmail.com 4873113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4888737Skoansin.tan@gmail.com#if defined(STAT_HAVE_NSEC) 4893114Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4908737Skoansin.tan@gmail.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 4913114Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4928737Skoansin.tan@gmail.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 4933114Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4948737Skoansin.tan@gmail.com tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 49511906SBrandon.Potter@amd.com#else 4964061Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4974061Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4988737Skoansin.tan@gmail.com tgt->st_ctime_nsec = 0; 4993113Sgblack@eecs.umich.edu#endif 5008737Skoansin.tan@gmail.com} 5013113Sgblack@eecs.umich.edu 5023113Sgblack@eecs.umich.edu// Here are a couple of convenience functions 5033113Sgblack@eecs.umich.edutemplate<class OS> 5043113Sgblack@eecs.umich.eduvoid 5053113Sgblack@eecs.umich.educopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 50612032Sandreas.sandberg@arm.com hst_stat *host, bool fakeTTY = false) 5073113Sgblack@eecs.umich.edu{ 5083113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5094189Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5104189Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5113113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5123113Sgblack@eecs.umich.edu} 5133113Sgblack@eecs.umich.edu 5148737Skoansin.tan@gmail.comtemplate<class OS> 5153113Sgblack@eecs.umich.eduvoid 5168737Skoansin.tan@gmail.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5173113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5188737Skoansin.tan@gmail.com{ 5193113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5203113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5213113Sgblack@eecs.umich.edu convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5223113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5233113Sgblack@eecs.umich.edu} 5243113Sgblack@eecs.umich.edu 5253113Sgblack@eecs.umich.edutemplate <class OS> 52611906SBrandon.Potter@amd.comvoid 5273113Sgblack@eecs.umich.educopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 52812032Sandreas.sandberg@arm.com hst_statfs *host) 5298852Sandreas.hansson@arm.com{ 53011906SBrandon.Potter@amd.com TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 5313113Sgblack@eecs.umich.edu 5323113Sgblack@eecs.umich.edu tgt->f_type = TheISA::htog(host->f_type); 5333113Sgblack@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5343113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_iosize); 5353113Sgblack@eecs.umich.edu#else 5363113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_bsize); 5373113Sgblack@eecs.umich.edu#endif 5383113Sgblack@eecs.umich.edu tgt->f_blocks = TheISA::htog(host->f_blocks); 53912032Sandreas.sandberg@arm.com tgt->f_bfree = TheISA::htog(host->f_bfree); 5408852Sandreas.hansson@arm.com tgt->f_bavail = TheISA::htog(host->f_bavail); 54111906SBrandon.Potter@amd.com tgt->f_files = TheISA::htog(host->f_files); 5423113Sgblack@eecs.umich.edu tgt->f_ffree = TheISA::htog(host->f_ffree); 5433113Sgblack@eecs.umich.edu memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 5443113Sgblack@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5456686Stjones1@inf.ed.ac.uk tgt->f_namelen = TheISA::htog(host->f_namemax); 5463113Sgblack@eecs.umich.edu tgt->f_frsize = TheISA::htog(host->f_bsize); 5473113Sgblack@eecs.umich.edu#elif defined(__APPLE__) 5483113Sgblack@eecs.umich.edu tgt->f_namelen = 0; 54911759Sbrandon.potter@amd.com tgt->f_frsize = 0; 55012032Sandreas.sandberg@arm.com#else 55111759Sbrandon.potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 55211759Sbrandon.potter@amd.com tgt->f_frsize = TheISA::htog(host->f_frsize); 55311759Sbrandon.potter@amd.com#endif 55411759Sbrandon.potter@amd.com#if defined(__linux__) 55511759Sbrandon.potter@amd.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 55611812Sbaz21@cam.ac.uk#else 55711812Sbaz21@cam.ac.uk /* 55811812Sbaz21@cam.ac.uk * The fields are different sizes per OS. Don't bother with 55911759Sbrandon.potter@amd.com * f_spare or f_reserved on non-Linux for now. 56011812Sbaz21@cam.ac.uk */ 56111759Sbrandon.potter@amd.com memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 56211759Sbrandon.potter@amd.com#endif 56311759Sbrandon.potter@amd.com 56411759Sbrandon.potter@amd.com tgt.copyOut(mem); 56511759Sbrandon.potter@amd.com} 56611759Sbrandon.potter@amd.com 56711759Sbrandon.potter@amd.com/// Target ioctl() handler. For the most part, programs call ioctl() 56811812Sbaz21@cam.ac.uk/// only to find out if their stdout is a tty, to determine whether to 56911812Sbaz21@cam.ac.uk/// do line or block buffering. We always claim that output fds are 57011812Sbaz21@cam.ac.uk/// not TTYs to provide repeatable results. 57111812Sbaz21@cam.ac.uktemplate <class OS> 57211812Sbaz21@cam.ac.ukSyscallReturn 57311812Sbaz21@cam.ac.ukioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 57411812Sbaz21@cam.ac.uk{ 57511759Sbrandon.potter@amd.com int index = 0; 57611759Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 57711812Sbaz21@cam.ac.uk unsigned req = p->getSyscallArg(tc, index); 57811812Sbaz21@cam.ac.uk 57911759Sbrandon.potter@amd.com DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 58011812Sbaz21@cam.ac.uk 58111812Sbaz21@cam.ac.uk if (OS::isTtyReq(req)) 58211812Sbaz21@cam.ac.uk return -ENOTTY; 58311812Sbaz21@cam.ac.uk 58411812Sbaz21@cam.ac.uk auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 58511812Sbaz21@cam.ac.uk if (!dfdp) 58611812Sbaz21@cam.ac.uk return -EBADF; 58711759Sbrandon.potter@amd.com 58811759Sbrandon.potter@amd.com /** 58911759Sbrandon.potter@amd.com * If the driver is valid, issue the ioctl through it. Otherwise, 59011759Sbrandon.potter@amd.com * there's an implicit assumption that the device is a TTY type and we 591378SN/A * return that we do not have a valid TTY. 592378SN/A */ 5939141Smarc.orr@gmail.com EmulatedDriver *emul_driver = dfdp->getDriver(); 5949141Smarc.orr@gmail.com if (emul_driver) 595360SN/A return emul_driver->ioctl(p, tc, req); 5961450SN/A 59711856Sbrandon.potter@amd.com /** 598360SN/A * For lack of a better return code, return ENOTTY. Ideally, we should 5996701Sgblack@eecs.umich.edu * return something better here, but at least we issue the warning. 60011856Sbrandon.potter@amd.com */ 60111856Sbrandon.potter@amd.com warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 602360SN/A tgt_fd, req, tc->pcState()); 60310930Sbrandon.potter@amd.com return -ENOTTY; 604360SN/A} 60511856Sbrandon.potter@amd.com 60611856Sbrandon.potter@amd.comtemplate <class OS> 60710496Ssteve.reinhardt@amd.comSyscallReturn 60811856Sbrandon.potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 60911856Sbrandon.potter@amd.com bool isopenat) 6101458SN/A{ 611360SN/A int index = 0; 61211856Sbrandon.potter@amd.com int tgt_dirfd = -1; 61311856Sbrandon.potter@amd.com 61411856Sbrandon.potter@amd.com /** 61511856Sbrandon.potter@amd.com * If using the openat variant, read in the target directory file 61611856Sbrandon.potter@amd.com * descriptor from the simulated process. 61711856Sbrandon.potter@amd.com */ 61811856Sbrandon.potter@amd.com if (isopenat) 61911856Sbrandon.potter@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 62010496Ssteve.reinhardt@amd.com 62111856Sbrandon.potter@amd.com /** 62211856Sbrandon.potter@amd.com * Retrieve the simulated process' memory proxy and then read in the path 62311856Sbrandon.potter@amd.com * string from that memory space into the host's working memory space. 62411856Sbrandon.potter@amd.com */ 62511856Sbrandon.potter@amd.com std::string path; 62610930Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 6279141Smarc.orr@gmail.com return -EFAULT; 628360SN/A 629360SN/A#ifdef __CYGWIN32__ 630360SN/A int host_flags = O_BINARY; 63111907SBrandon.Potter@amd.com#else 63211907SBrandon.Potter@amd.com int host_flags = 0; 63311907SBrandon.Potter@amd.com#endif 634360SN/A /** 63511907SBrandon.Potter@amd.com * Translate target flags into host flags. Flags exist which are not 63611907SBrandon.Potter@amd.com * ported between architectures which can cause check failures. 63711907SBrandon.Potter@amd.com */ 63811907SBrandon.Potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 63911907SBrandon.Potter@amd.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 64011907SBrandon.Potter@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 64111907SBrandon.Potter@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 64211907SBrandon.Potter@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 64311907SBrandon.Potter@amd.com } 64411907SBrandon.Potter@amd.com } 64511907SBrandon.Potter@amd.com if (tgt_flags) { 64611907SBrandon.Potter@amd.com warn("open%s: cannot decode flags 0x%x", 64711907SBrandon.Potter@amd.com isopenat ? "at" : "", tgt_flags); 64811907SBrandon.Potter@amd.com } 649360SN/A#ifdef __CYGWIN32__ 65011907SBrandon.Potter@amd.com host_flags |= O_BINARY; 6511458SN/A#endif 652360SN/A 65311907SBrandon.Potter@amd.com int mode = p->getSyscallArg(tc, index); 65411907SBrandon.Potter@amd.com 65511907SBrandon.Potter@amd.com /** 65611907SBrandon.Potter@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 65711907SBrandon.Potter@amd.com * take the current working directory value which was passed into the 65811907SBrandon.Potter@amd.com * process class as a Python parameter and append the current path to 65911907SBrandon.Potter@amd.com * create a full path. 66011907SBrandon.Potter@amd.com * Otherwise, openat with a valid target directory file descriptor has 66111907SBrandon.Potter@amd.com * been called. If the path option, which was passed in as a parameter, 66211907SBrandon.Potter@amd.com * is not absolute, retrieve the directory file descriptor's path and 663360SN/A * prepend it to the path passed in as a parameter. 66411907SBrandon.Potter@amd.com * In every case, we should have a full path (which is relevant to the 66511907SBrandon.Potter@amd.com * host) to work with after this block has been passed. 66611907SBrandon.Potter@amd.com */ 667360SN/A if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 668360SN/A path = p->fullPath(path); 66911907SBrandon.Potter@amd.com } else if (!startswith(path, "/")) { 67011907SBrandon.Potter@amd.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 67111907SBrandon.Potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 67211907SBrandon.Potter@amd.com if (!ffdp) 673360SN/A return -EBADF; 67411907SBrandon.Potter@amd.com path.insert(0, ffdp->getFileName()); 675360SN/A } 676360SN/A 67711907SBrandon.Potter@amd.com /** 6783669Sbinkertn@umich.edu * Since this is an emulated environment, we create pseudo file 67911907SBrandon.Potter@amd.com * descriptors for device requests that have been registered with 68011907SBrandon.Potter@amd.com * the process class through Python; this allows us to create a file 68111907SBrandon.Potter@amd.com * descriptor for subsequent ioctl or mmap calls. 68211907SBrandon.Potter@amd.com */ 68311907SBrandon.Potter@amd.com if (startswith(path, "/dev/")) { 68411907SBrandon.Potter@amd.com std::string filename = path.substr(strlen("/dev/")); 68511907SBrandon.Potter@amd.com EmulatedDriver *drv = p->findDriver(filename); 68611907SBrandon.Potter@amd.com if (drv) { 68711907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 68811907SBrandon.Potter@amd.com "driver open with path[%s]\n", 68911907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 69011907SBrandon.Potter@amd.com return drv->open(p, tc, mode, host_flags); 69111907SBrandon.Potter@amd.com } 69211907SBrandon.Potter@amd.com /** 69311907SBrandon.Potter@amd.com * Fall through here for pass through to host devices, such 69411907SBrandon.Potter@amd.com * as /dev/zero 69511907SBrandon.Potter@amd.com */ 69611907SBrandon.Potter@amd.com } 69711907SBrandon.Potter@amd.com 69811907SBrandon.Potter@amd.com /** 69911907SBrandon.Potter@amd.com * Some special paths and files cannot be called on the host and need 7001706SN/A * to be handled as special cases inside the simulator. 70111907SBrandon.Potter@amd.com * If the full path that was created above does not match any of the 70211907SBrandon.Potter@amd.com * special cases, pass it through to the open call on the host to let 70311907SBrandon.Potter@amd.com * the host open the file on our behalf. 70411907SBrandon.Potter@amd.com * If the host cannot open the file, return the host's error code back 70511907SBrandon.Potter@amd.com * through the system call to the simulated process. 70611907SBrandon.Potter@amd.com */ 70710496Ssteve.reinhardt@amd.com int sim_fd = -1; 70810496Ssteve.reinhardt@amd.com std::vector<std::string> special_paths = 70911907SBrandon.Potter@amd.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 71011907SBrandon.Potter@amd.com for (auto entry : special_paths) { 71111907SBrandon.Potter@amd.com if (startswith(path, entry)) 71211907SBrandon.Potter@amd.com sim_fd = OS::openSpecialFile(path, p, tc); 71311907SBrandon.Potter@amd.com } 71411907SBrandon.Potter@amd.com if (sim_fd == -1) { 71510496Ssteve.reinhardt@amd.com sim_fd = open(path.c_str(), host_flags, mode); 71611907SBrandon.Potter@amd.com } 71711907SBrandon.Potter@amd.com if (sim_fd == -1) { 71811907SBrandon.Potter@amd.com int local = -errno; 71911907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 72010496Ssteve.reinhardt@amd.com isopenat ? "at" : "", path.c_str()); 72110496Ssteve.reinhardt@amd.com return local; 72211907SBrandon.Potter@amd.com } 72311907SBrandon.Potter@amd.com 72411907SBrandon.Potter@amd.com /** 72511907SBrandon.Potter@amd.com * The file was opened successfully and needs to be recorded in the 72611907SBrandon.Potter@amd.com * process' file descriptor array so that it can be retrieved later. 72711907SBrandon.Potter@amd.com * The target file descriptor that is chosen will be the lowest unused 72811907SBrandon.Potter@amd.com * file descriptor. 72911907SBrandon.Potter@amd.com * Return the indirect target file descriptor back to the simulated 73011907SBrandon.Potter@amd.com * process to act as a handle for the opened file. 73111907SBrandon.Potter@amd.com */ 73211907SBrandon.Potter@amd.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 73311907SBrandon.Potter@amd.com int tgt_fd = p->fds->allocFD(ffdp); 73411907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 73511907SBrandon.Potter@amd.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 73611907SBrandon.Potter@amd.com return tgt_fd; 73711907SBrandon.Potter@amd.com} 73811907SBrandon.Potter@amd.com 73911907SBrandon.Potter@amd.com/// Target open() handler. 74011907SBrandon.Potter@amd.comtemplate <class OS> 74111907SBrandon.Potter@amd.comSyscallReturn 74211907SBrandon.Potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 74311907SBrandon.Potter@amd.com ThreadContext *tc) 74411907SBrandon.Potter@amd.com{ 74511907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, false); 74611907SBrandon.Potter@amd.com} 747360SN/A 74811907SBrandon.Potter@amd.com/// Target openat() handler. 74911907SBrandon.Potter@amd.comtemplate <class OS> 75011907SBrandon.Potter@amd.comSyscallReturn 75111907SBrandon.Potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process, 75211907SBrandon.Potter@amd.com ThreadContext *tc) 75311907SBrandon.Potter@amd.com{ 75411907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, true); 75511907SBrandon.Potter@amd.com} 75611907SBrandon.Potter@amd.com 75711907SBrandon.Potter@amd.com/// Target unlinkat() handler. 75811907SBrandon.Potter@amd.comtemplate <class OS> 75911907SBrandon.Potter@amd.comSyscallReturn 76011907SBrandon.Potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 761360SN/A ThreadContext *tc) 762360SN/A{ 76310027SChris.Adeniyi-Jones@arm.com int index = 0; 76410027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 76510027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 76611851Sbrandon.potter@amd.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 76710027SChris.Adeniyi-Jones@arm.com 76810027SChris.Adeniyi-Jones@arm.com return unlinkHelper(desc, callnum, process, tc, 1); 76911907SBrandon.Potter@amd.com} 77010027SChris.Adeniyi-Jones@arm.com 77110027SChris.Adeniyi-Jones@arm.com/// Target facessat() handler 77210027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 77310027SChris.Adeniyi-Jones@arm.comSyscallReturn 77410027SChris.Adeniyi-Jones@arm.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 77511851Sbrandon.potter@amd.com ThreadContext *tc) 77611851Sbrandon.potter@amd.com{ 77710027SChris.Adeniyi-Jones@arm.com int index = 0; 77811907SBrandon.Potter@amd.com int dirfd = process->getSyscallArg(tc, index); 77910027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 78010027SChris.Adeniyi-Jones@arm.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 78110633Smichaelupton@gmail.com return accessFunc(desc, callnum, process, tc, 1); 78210633Smichaelupton@gmail.com} 78310633Smichaelupton@gmail.com 78411851Sbrandon.potter@amd.com/// Target readlinkat() handler 78510633Smichaelupton@gmail.comtemplate <class OS> 78610633Smichaelupton@gmail.comSyscallReturn 78710633Smichaelupton@gmail.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 78810633Smichaelupton@gmail.com ThreadContext *tc) 78910633Smichaelupton@gmail.com{ 79010633Smichaelupton@gmail.com int index = 0; 79110633Smichaelupton@gmail.com int dirfd = process->getSyscallArg(tc, index); 79210633Smichaelupton@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 79310633Smichaelupton@gmail.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 79410633Smichaelupton@gmail.com return readlinkFunc(desc, callnum, process, tc, 1); 79510203SAli.Saidi@ARM.com} 79610203SAli.Saidi@ARM.com 79710203SAli.Saidi@ARM.com/// Target renameat() handler. 79811851Sbrandon.potter@amd.comtemplate <class OS> 79911851Sbrandon.potter@amd.comSyscallReturn 80010203SAli.Saidi@ARM.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process, 80110203SAli.Saidi@ARM.com ThreadContext *tc) 80210203SAli.Saidi@ARM.com{ 80310203SAli.Saidi@ARM.com int index = 0; 80410203SAli.Saidi@ARM.com 80510203SAli.Saidi@ARM.com int olddirfd = process->getSyscallArg(tc, index); 80610203SAli.Saidi@ARM.com if (olddirfd != OS::TGT_AT_FDCWD) 80710203SAli.Saidi@ARM.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 80810203SAli.Saidi@ARM.com 80910203SAli.Saidi@ARM.com std::string old_name; 81010203SAli.Saidi@ARM.com 81111851Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(old_name, 81211851Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 81310203SAli.Saidi@ARM.com return -EFAULT; 81410203SAli.Saidi@ARM.com 81510203SAli.Saidi@ARM.com int newdirfd = process->getSyscallArg(tc, index); 81610203SAli.Saidi@ARM.com if (newdirfd != OS::TGT_AT_FDCWD) 81710203SAli.Saidi@ARM.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 81810203SAli.Saidi@ARM.com 81910203SAli.Saidi@ARM.com std::string new_name; 82010203SAli.Saidi@ARM.com 82110850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(new_name, 82210850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 82310850SGiacomo.Gabrielli@arm.com return -EFAULT; 82411851Sbrandon.potter@amd.com 82510850SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 82610850SGiacomo.Gabrielli@arm.com old_name = process->fullPath(old_name); 82710850SGiacomo.Gabrielli@arm.com new_name = process->fullPath(new_name); 82810850SGiacomo.Gabrielli@arm.com 82910850SGiacomo.Gabrielli@arm.com int result = rename(old_name.c_str(), new_name.c_str()); 83010850SGiacomo.Gabrielli@arm.com return (result == -1) ? -errno : result; 83110850SGiacomo.Gabrielli@arm.com} 83210850SGiacomo.Gabrielli@arm.com 83310850SGiacomo.Gabrielli@arm.com/// Target sysinfo() handler. 83410850SGiacomo.Gabrielli@arm.comtemplate <class OS> 83510850SGiacomo.Gabrielli@arm.comSyscallReturn 83610850SGiacomo.Gabrielli@arm.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 83710850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 83810850SGiacomo.Gabrielli@arm.com{ 83910850SGiacomo.Gabrielli@arm.com 84010850SGiacomo.Gabrielli@arm.com int index = 0; 84110850SGiacomo.Gabrielli@arm.com TypedBufferArg<typename OS::tgt_sysinfo> 84210850SGiacomo.Gabrielli@arm.com sysinfo(process->getSyscallArg(tc, index)); 84310850SGiacomo.Gabrielli@arm.com 84410850SGiacomo.Gabrielli@arm.com sysinfo->uptime = seconds_since_epoch; 84510850SGiacomo.Gabrielli@arm.com sysinfo->totalram = process->system->memSize(); 84610850SGiacomo.Gabrielli@arm.com sysinfo->mem_unit = 1; 84710850SGiacomo.Gabrielli@arm.com 84810850SGiacomo.Gabrielli@arm.com sysinfo.copyOut(tc->getMemProxy()); 84910850SGiacomo.Gabrielli@arm.com 85010850SGiacomo.Gabrielli@arm.com return 0; 85110850SGiacomo.Gabrielli@arm.com} 85210850SGiacomo.Gabrielli@arm.com 85310850SGiacomo.Gabrielli@arm.com/// Target chmod() handler. 85410850SGiacomo.Gabrielli@arm.comtemplate <class OS> 85510850SGiacomo.Gabrielli@arm.comSyscallReturn 85610850SGiacomo.Gabrielli@arm.comchmodFunc(SyscallDesc *desc, int callnum, Process *process, 8576640Svince@csl.cornell.edu ThreadContext *tc) 8586640Svince@csl.cornell.edu{ 8596640Svince@csl.cornell.edu std::string path; 86011851Sbrandon.potter@amd.com 86111851Sbrandon.potter@amd.com int index = 0; 8626640Svince@csl.cornell.edu if (!tc->getMemProxy().tryReadString(path, 8636640Svince@csl.cornell.edu process->getSyscallArg(tc, index))) { 8646701Sgblack@eecs.umich.edu return -EFAULT; 8656701Sgblack@eecs.umich.edu } 86610793Sbrandon.potter@amd.com 8676640Svince@csl.cornell.edu uint32_t mode = process->getSyscallArg(tc, index); 86811758Sbrandon.potter@amd.com mode_t hostMode = 0; 86911758Sbrandon.potter@amd.com 87011758Sbrandon.potter@amd.com // XXX translate mode flags via OS::something??? 8716640Svince@csl.cornell.edu hostMode = mode; 8728706Sandreas.hansson@arm.com 8736640Svince@csl.cornell.edu // Adjust path for current working directory 8746701Sgblack@eecs.umich.edu path = process->fullPath(path); 8756640Svince@csl.cornell.edu 876360SN/A // do the chmod 8771999SN/A int result = chmod(path.c_str(), hostMode); 8781999SN/A if (result < 0) 8791999SN/A return -errno; 88011851Sbrandon.potter@amd.com 8812680Sktlim@umich.edu return 0; 8821999SN/A} 8831999SN/A 8841999SN/A 8856701Sgblack@eecs.umich.edu/// Target fchmod() handler. 8868852Sandreas.hansson@arm.comtemplate <class OS> 8876701Sgblack@eecs.umich.eduSyscallReturn 8881999SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 8896701Sgblack@eecs.umich.edu{ 8901999SN/A int index = 0; 8916701Sgblack@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 8921999SN/A uint32_t mode = p->getSyscallArg(tc, index); 8931999SN/A 8941999SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 8951999SN/A if (!ffdp) 8961999SN/A return -EBADF; 8973669Sbinkertn@umich.edu int sim_fd = ffdp->getSimFD(); 8983669Sbinkertn@umich.edu 8993669Sbinkertn@umich.edu mode_t hostMode = mode; 9001999SN/A 9011999SN/A int result = fchmod(sim_fd, hostMode); 9021999SN/A 9032218SN/A return (result < 0) ? -errno : 0; 9041999SN/A} 9051999SN/A 9061999SN/A/// Target mremap() handler. 9071999SN/Atemplate <class OS> 9081999SN/ASyscallReturn 9091999SN/AmremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 9101999SN/A{ 9111999SN/A int index = 0; 91211856Sbrandon.potter@amd.com Addr start = process->getSyscallArg(tc, index); 9131999SN/A uint64_t old_length = process->getSyscallArg(tc, index); 9146701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 91511856Sbrandon.potter@amd.com uint64_t flags = process->getSyscallArg(tc, index); 91611856Sbrandon.potter@amd.com uint64_t provided_address = 0; 91710931Sbrandon.potter@amd.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 91811856Sbrandon.potter@amd.com 91911856Sbrandon.potter@amd.com if (use_provided_address) 9201999SN/A provided_address = process->getSyscallArg(tc, index); 92111856Sbrandon.potter@amd.com 9221999SN/A if ((start % TheISA::PageBytes != 0) || 92311856Sbrandon.potter@amd.com (provided_address % TheISA::PageBytes != 0)) { 9241999SN/A warn("mremap failing: arguments not page aligned"); 92511856Sbrandon.potter@amd.com return -EINVAL; 9261999SN/A } 92711856Sbrandon.potter@amd.com 9281999SN/A new_length = roundUp(new_length, TheISA::PageBytes); 9291999SN/A 9305877Shsul@eecs.umich.edu if (new_length > old_length) { 9315877Shsul@eecs.umich.edu std::shared_ptr<MemState> mem_state = process->memState; 9325877Shsul@eecs.umich.edu Addr mmap_end = mem_state->getMmapEnd(); 93311851Sbrandon.potter@amd.com 9345877Shsul@eecs.umich.edu if ((start + old_length) == mmap_end && 9356701Sgblack@eecs.umich.edu (!use_provided_address || provided_address == start)) { 9366701Sgblack@eecs.umich.edu // This case cannot occur when growing downward, as 9376701Sgblack@eecs.umich.edu // start is greater than or equal to mmap_end. 9386701Sgblack@eecs.umich.edu uint64_t diff = new_length - old_length; 9396701Sgblack@eecs.umich.edu process->allocateMem(mmap_end, diff); 94010027SChris.Adeniyi-Jones@arm.com mem_state->setMmapEnd(mmap_end + diff); 94110027SChris.Adeniyi-Jones@arm.com return start; 94210027SChris.Adeniyi-Jones@arm.com } else { 94310027SChris.Adeniyi-Jones@arm.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 94410027SChris.Adeniyi-Jones@arm.com warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 9455877Shsul@eecs.umich.edu return -ENOMEM; 94610318Sandreas.hansson@arm.com } else { 94710318Sandreas.hansson@arm.com uint64_t new_start = provided_address; 9485877Shsul@eecs.umich.edu if (!use_provided_address) { 9495877Shsul@eecs.umich.edu new_start = process->mmapGrowsDown() ? 9505877Shsul@eecs.umich.edu mmap_end - new_length : mmap_end; 9515877Shsul@eecs.umich.edu mmap_end = process->mmapGrowsDown() ? 95210486Stjablin@gmail.com new_start : mmap_end + new_length; 95310486Stjablin@gmail.com mem_state->setMmapEnd(mmap_end); 9545877Shsul@eecs.umich.edu } 95511905SBrandon.Potter@amd.com 95611905SBrandon.Potter@amd.com process->pTable->remap(start, old_length, new_start); 95711905SBrandon.Potter@amd.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 95811905SBrandon.Potter@amd.com new_start, new_start + new_length, 95910027SChris.Adeniyi-Jones@arm.com new_length - old_length); 96012206Srico.amslinger@informatik.uni-augsburg.de // add on the remaining unallocated pages 96112206Srico.amslinger@informatik.uni-augsburg.de process->allocateMem(new_start + old_length, 9625877Shsul@eecs.umich.edu new_length - old_length, 96311905SBrandon.Potter@amd.com use_provided_address /* clobber */); 96411905SBrandon.Potter@amd.com if (use_provided_address && 9655877Shsul@eecs.umich.edu ((new_start + new_length > mem_state->getMmapEnd() && 9665877Shsul@eecs.umich.edu !process->mmapGrowsDown()) || 96710027SChris.Adeniyi-Jones@arm.com (new_start < mem_state->getMmapEnd() && 9685877Shsul@eecs.umich.edu process->mmapGrowsDown()))) { 9695877Shsul@eecs.umich.edu // something fishy going on here, at least notify the user 9705877Shsul@eecs.umich.edu // @todo: increase mmap_end? 97112206Srico.amslinger@informatik.uni-augsburg.de warn("mmap region limit exceeded with MREMAP_FIXED\n"); 97212206Srico.amslinger@informatik.uni-augsburg.de } 97312206Srico.amslinger@informatik.uni-augsburg.de warn("returning %08p as start\n", new_start); 97412206Srico.amslinger@informatik.uni-augsburg.de return new_start; 97512206Srico.amslinger@informatik.uni-augsburg.de } 97612206Srico.amslinger@informatik.uni-augsburg.de } 97712206Srico.amslinger@informatik.uni-augsburg.de } else { 97812206Srico.amslinger@informatik.uni-augsburg.de if (use_provided_address && provided_address != start) 97912206Srico.amslinger@informatik.uni-augsburg.de process->pTable->remap(start, new_length, provided_address); 98010027SChris.Adeniyi-Jones@arm.com process->pTable->unmap(start + new_length, old_length - new_length); 98110027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 98210027SChris.Adeniyi-Jones@arm.com } 98310027SChris.Adeniyi-Jones@arm.com} 9845877Shsul@eecs.umich.edu 98510027SChris.Adeniyi-Jones@arm.com/// Target stat() handler. 98610027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 98710027SChris.Adeniyi-Jones@arm.comSyscallReturn 98810027SChris.Adeniyi-Jones@arm.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 98912206Srico.amslinger@informatik.uni-augsburg.de ThreadContext *tc) 99012206Srico.amslinger@informatik.uni-augsburg.de{ 99112206Srico.amslinger@informatik.uni-augsburg.de std::string path; 99212206Srico.amslinger@informatik.uni-augsburg.de 99310027SChris.Adeniyi-Jones@arm.com int index = 0; 99410027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 99510027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) { 99610027SChris.Adeniyi-Jones@arm.com return -EFAULT; 99710027SChris.Adeniyi-Jones@arm.com } 99810027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 9995877Shsul@eecs.umich.edu 10005877Shsul@eecs.umich.edu // Adjust path for current working directory 10015877Shsul@eecs.umich.edu path = process->fullPath(path); 100210027SChris.Adeniyi-Jones@arm.com 100310027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 10048601Ssteve.reinhardt@amd.com int result = stat(path.c_str(), &hostBuf); 100510027SChris.Adeniyi-Jones@arm.com 10065877Shsul@eecs.umich.edu if (result < 0) 10075877Shsul@eecs.umich.edu return -errno; 10081999SN/A 1009378SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1010360SN/A 10111450SN/A return 0; 101211851Sbrandon.potter@amd.com} 10132680Sktlim@umich.edu 1014360SN/A 1015360SN/A/// Target stat64() handler. 1016360SN/Atemplate <class OS> 10176701Sgblack@eecs.umich.eduSyscallReturn 10188852Sandreas.hansson@arm.comstat64Func(SyscallDesc *desc, int callnum, Process *process, 10196701Sgblack@eecs.umich.edu ThreadContext *tc) 10206701Sgblack@eecs.umich.edu{ 10216701Sgblack@eecs.umich.edu std::string path; 10226701Sgblack@eecs.umich.edu 1023360SN/A int index = 0; 10243669Sbinkertn@umich.edu if (!tc->getMemProxy().tryReadString(path, 10253669Sbinkertn@umich.edu process->getSyscallArg(tc, index))) 10263669Sbinkertn@umich.edu return -EFAULT; 1027360SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1028360SN/A 1029360SN/A // Adjust path for current working directory 1030360SN/A path = process->fullPath(path); 10312218SN/A 1032360SN/A#if NO_STAT64 10338706Sandreas.hansson@arm.com struct stat hostBuf; 1034360SN/A int result = stat(path.c_str(), &hostBuf); 10351458SN/A#else 1036360SN/A struct stat64 hostBuf; 1037360SN/A int result = stat64(path.c_str(), &hostBuf); 1038360SN/A#endif 10395074Ssaidi@eecs.umich.edu 10405074Ssaidi@eecs.umich.edu if (result < 0) 10415074Ssaidi@eecs.umich.edu return -errno; 104211851Sbrandon.potter@amd.com 10435074Ssaidi@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10445074Ssaidi@eecs.umich.edu 10455074Ssaidi@eecs.umich.edu return 0; 10465074Ssaidi@eecs.umich.edu} 10476701Sgblack@eecs.umich.edu 10488852Sandreas.hansson@arm.com 10496701Sgblack@eecs.umich.edu/// Target fstatat64() handler. 10505074Ssaidi@eecs.umich.edutemplate <class OS> 10516701Sgblack@eecs.umich.eduSyscallReturn 10525074Ssaidi@eecs.umich.edufstatat64Func(SyscallDesc *desc, int callnum, Process *process, 10535074Ssaidi@eecs.umich.edu ThreadContext *tc) 10545074Ssaidi@eecs.umich.edu{ 10555074Ssaidi@eecs.umich.edu int index = 0; 10565208Ssaidi@eecs.umich.edu int dirfd = process->getSyscallArg(tc, index); 10575208Ssaidi@eecs.umich.edu if (dirfd != OS::TGT_AT_FDCWD) 10585208Ssaidi@eecs.umich.edu warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 10595208Ssaidi@eecs.umich.edu 10605074Ssaidi@eecs.umich.edu std::string path; 10615074Ssaidi@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 10625208Ssaidi@eecs.umich.edu process->getSyscallArg(tc, index))) 10635074Ssaidi@eecs.umich.edu return -EFAULT; 10645074Ssaidi@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 10655074Ssaidi@eecs.umich.edu 10665074Ssaidi@eecs.umich.edu // Adjust path for current working directory 10678706Sandreas.hansson@arm.com path = process->fullPath(path); 10685074Ssaidi@eecs.umich.edu 10695074Ssaidi@eecs.umich.edu#if NO_STAT64 10705074Ssaidi@eecs.umich.edu struct stat hostBuf; 10715074Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 10725074Ssaidi@eecs.umich.edu#else 107310027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 107410027SChris.Adeniyi-Jones@arm.com int result = stat64(path.c_str(), &hostBuf); 107510027SChris.Adeniyi-Jones@arm.com#endif 107611851Sbrandon.potter@amd.com 107710027SChris.Adeniyi-Jones@arm.com if (result < 0) 107810027SChris.Adeniyi-Jones@arm.com return -errno; 107910027SChris.Adeniyi-Jones@arm.com 108010027SChris.Adeniyi-Jones@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 108110027SChris.Adeniyi-Jones@arm.com 108210793Sbrandon.potter@amd.com return 0; 108310027SChris.Adeniyi-Jones@arm.com} 108410027SChris.Adeniyi-Jones@arm.com 108510027SChris.Adeniyi-Jones@arm.com 108610027SChris.Adeniyi-Jones@arm.com/// Target fstat64() handler. 108710027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 108810027SChris.Adeniyi-Jones@arm.comSyscallReturn 108910027SChris.Adeniyi-Jones@arm.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 109010027SChris.Adeniyi-Jones@arm.com{ 109110027SChris.Adeniyi-Jones@arm.com int index = 0; 109210027SChris.Adeniyi-Jones@arm.com int tgt_fd = p->getSyscallArg(tc, index); 109310027SChris.Adeniyi-Jones@arm.com Addr bufPtr = p->getSyscallArg(tc, index); 109410027SChris.Adeniyi-Jones@arm.com 109510027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 109610027SChris.Adeniyi-Jones@arm.com if (!ffdp) 109710027SChris.Adeniyi-Jones@arm.com return -EBADF; 109810027SChris.Adeniyi-Jones@arm.com int sim_fd = ffdp->getSimFD(); 109910027SChris.Adeniyi-Jones@arm.com 110010027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 110110027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 110210027SChris.Adeniyi-Jones@arm.com int result = fstat(sim_fd, &hostBuf); 110310027SChris.Adeniyi-Jones@arm.com#else 110410027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 110510027SChris.Adeniyi-Jones@arm.com int result = fstat64(sim_fd, &hostBuf); 110610027SChris.Adeniyi-Jones@arm.com#endif 110710027SChris.Adeniyi-Jones@arm.com 110810027SChris.Adeniyi-Jones@arm.com if (result < 0) 110910027SChris.Adeniyi-Jones@arm.com return -errno; 11101999SN/A 11111999SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 11121999SN/A 111311856Sbrandon.potter@amd.com return 0; 11141999SN/A} 11156701Sgblack@eecs.umich.edu 111611856Sbrandon.potter@amd.com 111711856Sbrandon.potter@amd.com/// Target lstat() handler. 111810931Sbrandon.potter@amd.comtemplate <class OS> 111911856Sbrandon.potter@amd.comSyscallReturn 112011856Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 11211999SN/A ThreadContext *tc) 112211856Sbrandon.potter@amd.com{ 11231999SN/A std::string path; 11242764Sstever@eecs.umich.edu 11252064SN/A int index = 0; 112610931Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 11272064SN/A process->getSyscallArg(tc, index))) { 11282064SN/A return -EFAULT; 112910931Sbrandon.potter@amd.com } 11302064SN/A Addr bufPtr = process->getSyscallArg(tc, index); 11311999SN/A 11321999SN/A // Adjust path for current working directory 11332218SN/A path = process->fullPath(path); 11341999SN/A 113510931Sbrandon.potter@amd.com struct stat hostBuf; 11361999SN/A int result = lstat(path.c_str(), &hostBuf); 11371999SN/A 11381999SN/A if (result < 0) 11391999SN/A return -errno; 11401999SN/A 1141378SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1142360SN/A 11431450SN/A return 0; 114411851Sbrandon.potter@amd.com} 11452680Sktlim@umich.edu 1146360SN/A/// Target lstat64() handler. 1147360SN/Atemplate <class OS> 1148360SN/ASyscallReturn 11496701Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, Process *process, 11508852Sandreas.hansson@arm.com ThreadContext *tc) 11516701Sgblack@eecs.umich.edu{ 11526701Sgblack@eecs.umich.edu std::string path; 11536701Sgblack@eecs.umich.edu 11546701Sgblack@eecs.umich.edu int index = 0; 1155360SN/A if (!tc->getMemProxy().tryReadString(path, 11563669Sbinkertn@umich.edu process->getSyscallArg(tc, index))) { 11573669Sbinkertn@umich.edu return -EFAULT; 11583669Sbinkertn@umich.edu } 1159360SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1160360SN/A 1161360SN/A // Adjust path for current working directory 1162360SN/A path = process->fullPath(path); 11631458SN/A 1164360SN/A#if NO_STAT64 11658706Sandreas.hansson@arm.com struct stat hostBuf; 1166360SN/A int result = lstat(path.c_str(), &hostBuf); 11671458SN/A#else 1168360SN/A struct stat64 hostBuf; 1169360SN/A int result = lstat64(path.c_str(), &hostBuf); 11701999SN/A#endif 11711999SN/A 11721999SN/A if (result < 0) 117311851Sbrandon.potter@amd.com return -errno; 11742680Sktlim@umich.edu 11751999SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11761999SN/A 11771999SN/A return 0; 11786701Sgblack@eecs.umich.edu} 11798852Sandreas.hansson@arm.com 11806701Sgblack@eecs.umich.edu/// Target fstat() handler. 11816701Sgblack@eecs.umich.edutemplate <class OS> 11826701Sgblack@eecs.umich.eduSyscallReturn 11836701Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 11841999SN/A{ 11853669Sbinkertn@umich.edu int index = 0; 11863669Sbinkertn@umich.edu int tgt_fd = p->getSyscallArg(tc, index); 11873669Sbinkertn@umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 11882764Sstever@eecs.umich.edu 11892064SN/A DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 11902064SN/A 11912064SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 11921999SN/A if (!ffdp) 11931999SN/A return -EBADF; 11942064SN/A int sim_fd = ffdp->getSimFD(); 11951999SN/A 11961999SN/A struct stat hostBuf; 11971999SN/A int result = fstat(sim_fd, &hostBuf); 11981999SN/A 11998706Sandreas.hansson@arm.com if (result < 0) 12001999SN/A return -errno; 12011999SN/A 12021999SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12031999SN/A 1204378SN/A return 0; 1205360SN/A} 12061450SN/A 120711856Sbrandon.potter@amd.com 1208360SN/A/// Target statfs() handler. 12096701Sgblack@eecs.umich.edutemplate <class OS> 121011856Sbrandon.potter@amd.comSyscallReturn 121111856Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 1212360SN/A ThreadContext *tc) 121311380Salexandru.dutu@amd.com{ 1214360SN/A#if NO_STATFS 121511856Sbrandon.potter@amd.com warn("Host OS cannot support calls to statfs. Ignoring syscall"); 121611856Sbrandon.potter@amd.com#else 12171458SN/A std::string path; 121811856Sbrandon.potter@amd.com 1219360SN/A int index = 0; 1220360SN/A if (!tc->getMemProxy().tryReadString(path, 122110931Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 1222360SN/A return -EFAULT; 1223360SN/A } 12241458SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1225360SN/A 122610931Sbrandon.potter@amd.com // Adjust path for current working directory 12272021SN/A path = process->fullPath(path); 12281458SN/A 1229360SN/A struct statfs hostBuf; 1230360SN/A int result = statfs(path.c_str(), &hostBuf); 1231360SN/A 12321706SN/A if (result < 0) 12331706SN/A return -errno; 12341706SN/A 123511851Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12362680Sktlim@umich.edu#endif 12371706SN/A return 0; 123811799Sbrandon.potter@amd.com} 123911799Sbrandon.potter@amd.com 124011799Sbrandon.potter@amd.comtemplate <class OS> 12411706SN/ASyscallReturn 12421706SN/AcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 12436701Sgblack@eecs.umich.edu{ 12448852Sandreas.hansson@arm.com int index = 0; 12456701Sgblack@eecs.umich.edu 12466701Sgblack@eecs.umich.edu TheISA::IntReg flags = p->getSyscallArg(tc, index); 12476701Sgblack@eecs.umich.edu TheISA::IntReg newStack = p->getSyscallArg(tc, index); 12486701Sgblack@eecs.umich.edu Addr ptidPtr = p->getSyscallArg(tc, index); 12491706SN/A 12503669Sbinkertn@umich.edu#if THE_ISA == RISCV_ISA 12513669Sbinkertn@umich.edu /** 12523669Sbinkertn@umich.edu * Linux kernel 4.15 sets CLONE_BACKWARDS flag for RISC-V. 12531706SN/A * The flag defines the list of clone() arguments in the following 12541706SN/A * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 12551706SN/A */ 12561706SN/A Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 12572218SN/A Addr ctidPtr = p->getSyscallArg(tc, index); 12581706SN/A#else 125911759Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 126011799Sbrandon.potter@amd.com Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 12611706SN/A#endif 12621706SN/A 12631706SN/A if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 126411886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 126511886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 126611886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 126711886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 126811886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 126912426Sqtt2@cornell.edu return -EINVAL; 127011886Sbrandon.potter@amd.com 127111886Sbrandon.potter@amd.com ThreadContext *ctc; 127211886Sbrandon.potter@amd.com if (!(ctc = p->findFreeContext())) 127312426Sqtt2@cornell.edu fatal("clone: no spare thread context in system"); 127412426Sqtt2@cornell.edu 127512426Sqtt2@cornell.edu /** 127612426Sqtt2@cornell.edu * Note that ProcessParams is generated by swig and there are no other 127712426Sqtt2@cornell.edu * examples of how to create anything but this default constructor. The 127812426Sqtt2@cornell.edu * fields are manually initialized instead of passing parameters to the 127912426Sqtt2@cornell.edu * constructor. 128012426Sqtt2@cornell.edu */ 128112426Sqtt2@cornell.edu ProcessParams *pp = new ProcessParams(); 128212426Sqtt2@cornell.edu pp->executable.assign(*(new std::string(p->progName()))); 128311886Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 128411886Sbrandon.potter@amd.com pp->system = p->system; 128512426Sqtt2@cornell.edu pp->cwd.assign(p->getcwd()); 128611886Sbrandon.potter@amd.com pp->input.assign("stdin"); 128711886Sbrandon.potter@amd.com pp->output.assign("stdout"); 128811886Sbrandon.potter@amd.com pp->errout.assign("stderr"); 128911886Sbrandon.potter@amd.com pp->uid = p->uid(); 129011886Sbrandon.potter@amd.com pp->euid = p->euid(); 129111886Sbrandon.potter@amd.com pp->gid = p->gid(); 129211886Sbrandon.potter@amd.com pp->egid = p->egid(); 129311886Sbrandon.potter@amd.com 129411886Sbrandon.potter@amd.com /* Find the first free PID that's less than the maximum */ 129511886Sbrandon.potter@amd.com std::set<int> const& pids = p->system->PIDs; 129611886Sbrandon.potter@amd.com int temp_pid = *pids.begin(); 129711886Sbrandon.potter@amd.com do { 129811886Sbrandon.potter@amd.com temp_pid++; 129911886Sbrandon.potter@amd.com } while (pids.find(temp_pid) != pids.end()); 130011886Sbrandon.potter@amd.com if (temp_pid >= System::maxPID) 130111886Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 130211886Sbrandon.potter@amd.com 130311886Sbrandon.potter@amd.com pp->pid = temp_pid; 130411886Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 130511886Sbrandon.potter@amd.com Process *cp = pp->create(); 130611886Sbrandon.potter@amd.com delete pp; 130711886Sbrandon.potter@amd.com 130811886Sbrandon.potter@amd.com Process *owner = ctc->getProcessPtr(); 130911886Sbrandon.potter@amd.com ctc->setProcessPtr(cp); 131011886Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 131111886Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 131211886Sbrandon.potter@amd.com 131311886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 131411886Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 131511886Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 131611886Sbrandon.potter@amd.com *ptid = cp->pid(); 131711886Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 131811886Sbrandon.potter@amd.com } 131911886Sbrandon.potter@amd.com 132011886Sbrandon.potter@amd.com cp->initState(); 132111886Sbrandon.potter@amd.com p->clone(tc, ctc, cp, flags); 132211886Sbrandon.potter@amd.com 132311886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 132411886Sbrandon.potter@amd.com delete cp->sigchld; 132511886Sbrandon.potter@amd.com cp->sigchld = p->sigchld; 132611886Sbrandon.potter@amd.com } else if (flags & OS::TGT_SIGCHLD) { 132711886Sbrandon.potter@amd.com *cp->sigchld = true; 132811886Sbrandon.potter@amd.com } 132911886Sbrandon.potter@amd.com 133011886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 133111886Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 133211886Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 133311886Sbrandon.potter@amd.com *ctid = cp->pid(); 133411886Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 133511886Sbrandon.potter@amd.com } 133611886Sbrandon.potter@amd.com 133711886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 133811886Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 133911886Sbrandon.potter@amd.com 134011886Sbrandon.potter@amd.com ctc->clearArchRegs(); 134111886Sbrandon.potter@amd.com 134211886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 134311886Sbrandon.potter@amd.com TheISA::copyMiscRegs(tc, ctc); 134411886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 134511886Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 134611886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 134711911SBrandon.Potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 134811911SBrandon.Potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 134911911SBrandon.Potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 135011911SBrandon.Potter@amd.com ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 135111911SBrandon.Potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 135211911SBrandon.Potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 135311911SBrandon.Potter@amd.com ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 135411886Sbrandon.potter@amd.com for (int y = 8; y < 32; y++) 135511886Sbrandon.potter@amd.com ctc->setIntReg(y, tc->readIntReg(y)); 135611886Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA or THE_ISA == RISCV_ISA 135711886Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 135811886Sbrandon.potter@amd.com#endif 135911886Sbrandon.potter@amd.com 136011886Sbrandon.potter@amd.com#if THE_ISA == X86_ISA 136111886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_SETTLS) { 136211886Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 136311886Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 136411886Sbrandon.potter@amd.com } 136511886Sbrandon.potter@amd.com#endif 136611886Sbrandon.potter@amd.com 136711886Sbrandon.potter@amd.com if (newStack) 136811886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::StackPointerReg, newStack); 136911886Sbrandon.potter@amd.com 137011886Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 137111886Sbrandon.potter@amd.com 137211886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 137311886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 137411886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 137511886Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 137611886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 137711886Sbrandon.potter@amd.com#endif 137811886Sbrandon.potter@amd.com 137911886Sbrandon.potter@amd.com ctc->pcState(tc->nextInstAddr()); 138012426Sqtt2@cornell.edu ctc->activate(); 138111886Sbrandon.potter@amd.com 138211886Sbrandon.potter@amd.com return cp->pid(); 138311886Sbrandon.potter@amd.com} 138411886Sbrandon.potter@amd.com 138511886Sbrandon.potter@amd.com/// Target fstatfs() handler. 138611886Sbrandon.potter@amd.comtemplate <class OS> 138711886Sbrandon.potter@amd.comSyscallReturn 138811886Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 138911886Sbrandon.potter@amd.com{ 139011886Sbrandon.potter@amd.com int index = 0; 139111886Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 139211886Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 139311886Sbrandon.potter@amd.com 139411886Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 139511886Sbrandon.potter@amd.com if (!ffdp) 139611886Sbrandon.potter@amd.com return -EBADF; 139711886Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 139811886Sbrandon.potter@amd.com 139911886Sbrandon.potter@amd.com struct statfs hostBuf; 140011886Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 140111886Sbrandon.potter@amd.com 140211886Sbrandon.potter@amd.com if (result < 0) 140311886Sbrandon.potter@amd.com return -errno; 140411886Sbrandon.potter@amd.com 140511886Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 140611886Sbrandon.potter@amd.com 140711886Sbrandon.potter@amd.com return 0; 14081706SN/A} 14091706SN/A 14101706SN/A 14111706SN/A/// Target writev() handler. 141211856Sbrandon.potter@amd.comtemplate <class OS> 14131706SN/ASyscallReturn 14146701Sgblack@eecs.umich.eduwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 141511856Sbrandon.potter@amd.com{ 141611856Sbrandon.potter@amd.com int index = 0; 14171706SN/A int tgt_fd = p->getSyscallArg(tc, index); 141811856Sbrandon.potter@amd.com 141911856Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 14201706SN/A if (!hbfdp) 142111856Sbrandon.potter@amd.com return -EBADF; 14221706SN/A int sim_fd = hbfdp->getSimFD(); 14231706SN/A 142410931Sbrandon.potter@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 14251706SN/A uint64_t tiov_base = p->getSyscallArg(tc, index); 14261706SN/A size_t count = p->getSyscallArg(tc, index); 14272218SN/A struct iovec hiov[count]; 14281706SN/A for (size_t i = 0; i < count; ++i) { 142911759Sbrandon.potter@amd.com typename OS::tgt_iovec tiov; 14301706SN/A 14311706SN/A prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 14321706SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 14331706SN/A hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 14341706SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 14351999SN/A prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 14361999SN/A hiov[i].iov_len); 14371999SN/A } 143811856Sbrandon.potter@amd.com 14391999SN/A int result = writev(sim_fd, hiov, count); 14406701Sgblack@eecs.umich.edu 144111856Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) 144210931Sbrandon.potter@amd.com delete [] (char *)hiov[i].iov_base; 144311856Sbrandon.potter@amd.com 144411856Sbrandon.potter@amd.com if (result < 0) 14451999SN/A return -errno; 144611856Sbrandon.potter@amd.com 14471999SN/A return result; 144811856Sbrandon.potter@amd.com} 144911856Sbrandon.potter@amd.com 145011856Sbrandon.potter@amd.com/// Real mmap handler. 14511999SN/Atemplate <class OS> 14526227Snate@binkert.orgSyscallReturn 14531999SN/AmmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 14542461SN/A bool is_mmap2) 145511856Sbrandon.potter@amd.com{ 145611856Sbrandon.potter@amd.com int index = 0; 14578737Skoansin.tan@gmail.com Addr start = p->getSyscallArg(tc, index); 14581999SN/A uint64_t length = p->getSyscallArg(tc, index); 145911856Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 146011856Sbrandon.potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 14611999SN/A int tgt_fd = p->getSyscallArg(tc, index); 14621999SN/A int offset = p->getSyscallArg(tc, index); 146310931Sbrandon.potter@amd.com 14641999SN/A if (is_mmap2) 14656227Snate@binkert.org offset *= TheISA::PageBytes; 14661999SN/A 14671999SN/A if (start & (TheISA::PageBytes - 1) || 14681999SN/A offset & (TheISA::PageBytes - 1) || 14692218SN/A (tgt_flags & OS::TGT_MAP_PRIVATE && 14701999SN/A tgt_flags & OS::TGT_MAP_SHARED) || 147110629Sjthestness@gmail.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 14721999SN/A !(tgt_flags & OS::TGT_MAP_SHARED)) || 14731999SN/A !length) { 147411385Sbrandon.potter@amd.com return -EINVAL; 1475360SN/A } 14761450SN/A 147711851Sbrandon.potter@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 147811385Sbrandon.potter@amd.com // With shared mmaps, there are two cases to consider: 1479360SN/A // 1) anonymous: writes should modify the mapping and this should be 14806701Sgblack@eecs.umich.edu // visible to observers who share the mapping. Currently, it's 14816701Sgblack@eecs.umich.edu // difficult to update the shared mapping because there's no 14826701Sgblack@eecs.umich.edu // structure which maintains information about the which virtual 148311383Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 148411383Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 14858324Ssteve.reinhardt@amd.com // 2) file-backed: writes should modify the mapping and the file 148610486Stjablin@gmail.com // which is backed by the mapping. The shared mapping problem is the 1487360SN/A // same as what was mentioned about the anonymous mappings. For 148811385Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 148911385Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 14909008Sgblack@eecs.umich.edu // that resides on the host system. So, any write on a real system 149111383Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 149211383Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 149311383Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 149411383Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 149511383Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 149611383Sbrandon.potter@amd.com // a call to msync, but that again would require more information 149711383Sbrandon.potter@amd.com // than we currently maintain. 149811383Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 149911383Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 15008324Ssteve.reinhardt@amd.com "will not be propagated to the host or shared mappings"); 150111383Sbrandon.potter@amd.com } 150211383Sbrandon.potter@amd.com 150311383Sbrandon.potter@amd.com length = roundUp(length, TheISA::PageBytes); 150411383Sbrandon.potter@amd.com 150511383Sbrandon.potter@amd.com int sim_fd = -1; 150611383Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 150711383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 150811383Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 150911383Sbrandon.potter@amd.com 151011383Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 151111383Sbrandon.potter@amd.com if (dfdp) { 151211383Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 151311383Sbrandon.potter@amd.com return emul_driver->mmap(p, tc, start, length, prot, 151411383Sbrandon.potter@amd.com tgt_flags, tgt_fd, offset); 151511383Sbrandon.potter@amd.com } 151611383Sbrandon.potter@amd.com 151711383Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 151811383Sbrandon.potter@amd.com if (!ffdp) 151911383Sbrandon.potter@amd.com return -EBADF; 152011383Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 152111383Sbrandon.potter@amd.com 152211383Sbrandon.potter@amd.com pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 152311383Sbrandon.potter@amd.com sim_fd, offset); 152411383Sbrandon.potter@amd.com 15258324Ssteve.reinhardt@amd.com if (pmap == (decltype(pmap))-1) { 15265877Shsul@eecs.umich.edu warn("mmap: failed to map file into host address space"); 152710486Stjablin@gmail.com return -errno; 152810486Stjablin@gmail.com } 152911383Sbrandon.potter@amd.com } 153011383Sbrandon.potter@amd.com 153111383Sbrandon.potter@amd.com // Extend global mmap region if necessary. Note that we ignore the 153211856Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 153311624Smichael.lebeane@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 153411856Sbrandon.potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 153511856Sbrandon.potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 153611856Sbrandon.potter@amd.com 153711856Sbrandon.potter@amd.com start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 153811624Smichael.lebeane@amd.com mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 153911624Smichael.lebeane@amd.com 154011624Smichael.lebeane@amd.com mem_state->setMmapEnd(mmap_end); 154111856Sbrandon.potter@amd.com } 154211856Sbrandon.potter@amd.com 154311383Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 154411856Sbrandon.potter@amd.com start, start + length - 1); 1545360SN/A 154611913SBrandon.Potter@amd.com // We only allow mappings to overwrite existing mappings if 154711383Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 15488600Ssteve.reinhardt@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 154911383Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 155011383Sbrandon.potter@amd.com if (clobber) { 155111383Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 15528600Ssteve.reinhardt@amd.com // If we might be overwriting old mappings, we need to 15532544SN/A // invalidate potentially stale mappings out of the TLBs. 15542544SN/A tc->getDTBPtr()->flushAll(); 155511383Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 155611383Sbrandon.potter@amd.com } 155711383Sbrandon.potter@amd.com } 155811905SBrandon.Potter@amd.com 155911905SBrandon.Potter@amd.com // Allocate physical memory and map it in. If the page table is already 156011905SBrandon.Potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 156111905SBrandon.Potter@amd.com // fatal and bail out of the simulation. 156211905SBrandon.Potter@amd.com p->allocateMem(start, length, clobber); 156311905SBrandon.Potter@amd.com 156411905SBrandon.Potter@amd.com // Transfer content into target address space. 156511383Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 156611383Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 156711383Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 156811383Sbrandon.potter@amd.com // with something like: 156911383Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 157011383Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 157111383Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 157211383Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 157311383Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 157411383Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 157511383Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 157611383Sbrandon.potter@amd.com } else { 157711383Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 157811383Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 157911383Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 15808600Ssteve.reinhardt@amd.com // the host into the target address space. 15816672Sgblack@eecs.umich.edu struct stat file_stat; 15828600Ssteve.reinhardt@amd.com if (fstat(sim_fd, &file_stat) > 0) 158311383Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 158411383Sbrandon.potter@amd.com 158511383Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 15868601Ssteve.reinhardt@amd.com // checking both the mmap size and the filesize that we are 15872544SN/A // trying to mmap into this space; the mmap size also depends 158811383Sbrandon.potter@amd.com // on the specified offset into the file. 158911383Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 159011383Sbrandon.potter@amd.com length); 159111383Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 159211383Sbrandon.potter@amd.com 159311383Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 159411383Sbrandon.potter@amd.com munmap(pmap, length); 159511383Sbrandon.potter@amd.com 159611383Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 159711383Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 159811383Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 159911383Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 160011383Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 160111383Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 160211383Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 160311383Sbrandon.potter@amd.com if (interpreter) { 160411383Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 160511383Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 160611383Sbrandon.potter@amd.com 160711383Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 160811383Sbrandon.potter@amd.com 160911383Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 161011383Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 161111383Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 161211383Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 161311383Sbrandon.potter@amd.com 161411383Sbrandon.potter@amd.com if (lib) { 161511383Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 161611383Sbrandon.potter@amd.com lib->textBase(), start); 161711383Sbrandon.potter@amd.com } 161811383Sbrandon.potter@amd.com } 161911383Sbrandon.potter@amd.com } 162011392Sbrandon.potter@amd.com 162111392Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 162211392Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 162311392Sbrandon.potter@amd.com // execution (hopefully). 162411392Sbrandon.potter@amd.com } 162511392Sbrandon.potter@amd.com 162611392Sbrandon.potter@amd.com return start; 162711392Sbrandon.potter@amd.com} 162811392Sbrandon.potter@amd.com 162911392Sbrandon.potter@amd.comtemplate <class OS> 163011392Sbrandon.potter@amd.comSyscallReturn 163111392Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 163211392Sbrandon.potter@amd.com{ 163311392Sbrandon.potter@amd.com int index = 0; 163411856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 163511856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 163611856Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 163711392Sbrandon.potter@amd.com int offset = p->getSyscallArg(tc, index); 163811392Sbrandon.potter@amd.com 163911392Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 164011392Sbrandon.potter@amd.com if (!ffdp) 164111392Sbrandon.potter@amd.com return -EBADF; 164211392Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 164311392Sbrandon.potter@amd.com 164411392Sbrandon.potter@amd.com BufferArg bufArg(bufPtr, nbytes); 164511383Sbrandon.potter@amd.com bufArg.copyIn(tc->getMemProxy()); 164611383Sbrandon.potter@amd.com 164711383Sbrandon.potter@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 164811383Sbrandon.potter@amd.com 164911383Sbrandon.potter@amd.com return (bytes_written == -1) ? -errno : bytes_written; 16501458SN/A} 1651360SN/A 1652360SN/A/// Target mmap() handler. 165311593Santhony.gutierrez@amd.comtemplate <class OS> 165411593Santhony.gutierrez@amd.comSyscallReturn 165511851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 165611593Santhony.gutierrez@amd.com{ 165711593Santhony.gutierrez@amd.com return mmapImpl<OS>(desc, num, p, tc, false); 165811593Santhony.gutierrez@amd.com} 165911593Santhony.gutierrez@amd.com 166011593Santhony.gutierrez@amd.com/// Target mmap2() handler. 166111593Santhony.gutierrez@amd.comtemplate <class OS> 166211593Santhony.gutierrez@amd.comSyscallReturn 166311856Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 166411856Sbrandon.potter@amd.com{ 166511593Santhony.gutierrez@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 166611856Sbrandon.potter@amd.com} 166711593Santhony.gutierrez@amd.com 166811593Santhony.gutierrez@amd.com/// Target getrlimit() handler. 166911593Santhony.gutierrez@amd.comtemplate <class OS> 167011593Santhony.gutierrez@amd.comSyscallReturn 167111594Santhony.gutierrez@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 167211593Santhony.gutierrez@amd.com ThreadContext *tc) 167311593Santhony.gutierrez@amd.com{ 167411593Santhony.gutierrez@amd.com int index = 0; 167511593Santhony.gutierrez@amd.com unsigned resource = process->getSyscallArg(tc, index); 167611385Sbrandon.potter@amd.com TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 167711385Sbrandon.potter@amd.com 167811385Sbrandon.potter@amd.com switch (resource) { 167911851Sbrandon.potter@amd.com case OS::TGT_RLIMIT_STACK: 168011385Sbrandon.potter@amd.com // max stack size in bytes: make up a number (8MB for now) 168111385Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 168211385Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 168311385Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 168411385Sbrandon.potter@amd.com break; 168511385Sbrandon.potter@amd.com 168611385Sbrandon.potter@amd.com case OS::TGT_RLIMIT_DATA: 168711851Sbrandon.potter@amd.com // max data segment size in bytes: make up a number 168811385Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 168911385Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 169011385Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 169111385Sbrandon.potter@amd.com break; 1692378SN/A 1693360SN/A default: 16941450SN/A warn("getrlimit: unimplemented resource %d", resource); 169511851Sbrandon.potter@amd.com return -EINVAL; 169611851Sbrandon.potter@amd.com break; 1697360SN/A } 16986701Sgblack@eecs.umich.edu 16996701Sgblack@eecs.umich.edu rlp.copyOut(tc->getMemProxy()); 17006701Sgblack@eecs.umich.edu return 0; 1701360SN/A} 1702360SN/A 170311906SBrandon.Potter@amd.comtemplate <class OS> 170411906SBrandon.Potter@amd.comSyscallReturn 170511906SBrandon.Potter@amd.comprlimitFunc(SyscallDesc *desc, int callnum, Process *process, 170611906SBrandon.Potter@amd.com ThreadContext *tc) 170711906SBrandon.Potter@amd.com{ 170811906SBrandon.Potter@amd.com int index = 0; 1709360SN/A if (process->getSyscallArg(tc, index) != 0) 171011906SBrandon.Potter@amd.com { 171111906SBrandon.Potter@amd.com warn("prlimit: ignoring rlimits for nonzero pid"); 171211906SBrandon.Potter@amd.com return -EPERM; 171311906SBrandon.Potter@amd.com } 171411906SBrandon.Potter@amd.com int resource = process->getSyscallArg(tc, index); 171511906SBrandon.Potter@amd.com Addr n = process->getSyscallArg(tc, index); 17165877Shsul@eecs.umich.edu if (n != 0) 171711906SBrandon.Potter@amd.com warn("prlimit: ignoring new rlimit"); 171811906SBrandon.Potter@amd.com Addr o = process->getSyscallArg(tc, index); 171911906SBrandon.Potter@amd.com if (o != 0) 172011906SBrandon.Potter@amd.com { 1721360SN/A TypedBufferArg<typename OS::rlimit> rlp(o); 1722360SN/A switch (resource) { 17238706Sandreas.hansson@arm.com case OS::TGT_RLIMIT_STACK: 17241458SN/A // max stack size in bytes: make up a number (8MB for now) 1725360SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1726360SN/A rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 172712235Sar4jc@virginia.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 172812235Sar4jc@virginia.edu break; 172912235Sar4jc@virginia.edu case OS::TGT_RLIMIT_DATA: 173012235Sar4jc@virginia.edu // max data segment size in bytes: make up a number 173112235Sar4jc@virginia.edu rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 173212235Sar4jc@virginia.edu rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 173312235Sar4jc@virginia.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 173412235Sar4jc@virginia.edu break; 173512235Sar4jc@virginia.edu default: 173612235Sar4jc@virginia.edu warn("prlimit: unimplemented resource %d", resource); 173712235Sar4jc@virginia.edu return -EINVAL; 173812235Sar4jc@virginia.edu break; 173912235Sar4jc@virginia.edu } 174012235Sar4jc@virginia.edu rlp.copyOut(tc->getMemProxy()); 174112235Sar4jc@virginia.edu } 174212235Sar4jc@virginia.edu return 0; 174312235Sar4jc@virginia.edu} 174412235Sar4jc@virginia.edu 174512416Sqtt2@cornell.edu/// Target clock_gettime() function. 174612235Sar4jc@virginia.edutemplate <class OS> 174712235Sar4jc@virginia.eduSyscallReturn 174812235Sar4jc@virginia.educlock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 174912235Sar4jc@virginia.edu{ 175012235Sar4jc@virginia.edu int index = 1; 175112235Sar4jc@virginia.edu //int clk_id = p->getSyscallArg(tc, index); 175212235Sar4jc@virginia.edu TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 175312235Sar4jc@virginia.edu 175412235Sar4jc@virginia.edu getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 175512235Sar4jc@virginia.edu tp->tv_sec += seconds_since_epoch; 175612235Sar4jc@virginia.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 175712235Sar4jc@virginia.edu tp->tv_nsec = TheISA::htog(tp->tv_nsec); 175812593Sjason@lowepower.com 175912235Sar4jc@virginia.edu tp.copyOut(tc->getMemProxy()); 176012235Sar4jc@virginia.edu 176112235Sar4jc@virginia.edu return 0; 176212235Sar4jc@virginia.edu} 176312235Sar4jc@virginia.edu 176412235Sar4jc@virginia.edu/// Target clock_getres() function. 176512235Sar4jc@virginia.edutemplate <class OS> 176612235Sar4jc@virginia.eduSyscallReturn 176712235Sar4jc@virginia.educlock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 176812235Sar4jc@virginia.edu{ 176910796Sbrandon.potter@amd.com int index = 1; 177010796Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 177110796Sbrandon.potter@amd.com 177211851Sbrandon.potter@amd.com // Set resolution at ns, which is what clock_gettime() returns 177310796Sbrandon.potter@amd.com tp->tv_sec = 0; 177410796Sbrandon.potter@amd.com tp->tv_nsec = 1; 177510796Sbrandon.potter@amd.com 177610796Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 177710796Sbrandon.potter@amd.com 177810796Sbrandon.potter@amd.com return 0; 177910796Sbrandon.potter@amd.com} 178010796Sbrandon.potter@amd.com 178110796Sbrandon.potter@amd.com/// Target gettimeofday() handler. 178210796Sbrandon.potter@amd.comtemplate <class OS> 178310796Sbrandon.potter@amd.comSyscallReturn 178410796Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 178510796Sbrandon.potter@amd.com ThreadContext *tc) 178610796Sbrandon.potter@amd.com{ 178710796Sbrandon.potter@amd.com int index = 0; 178811337SMichael.Lebeane@amd.com TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 178911337SMichael.Lebeane@amd.com 179011337SMichael.Lebeane@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 179111851Sbrandon.potter@amd.com tp->tv_sec += seconds_since_epoch; 179211337SMichael.Lebeane@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 179311337SMichael.Lebeane@amd.com tp->tv_usec = TheISA::htog(tp->tv_usec); 179411337SMichael.Lebeane@amd.com 179511337SMichael.Lebeane@amd.com tp.copyOut(tc->getMemProxy()); 179611337SMichael.Lebeane@amd.com 179711337SMichael.Lebeane@amd.com return 0; 179811337SMichael.Lebeane@amd.com} 179911337SMichael.Lebeane@amd.com 180011337SMichael.Lebeane@amd.com 180111337SMichael.Lebeane@amd.com/// Target utimes() handler. 180211337SMichael.Lebeane@amd.comtemplate <class OS> 180311337SMichael.Lebeane@amd.comSyscallReturn 180411337SMichael.Lebeane@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process, 1805378SN/A ThreadContext *tc) 1806360SN/A{ 18071450SN/A std::string path; 180811851Sbrandon.potter@amd.com 180911851Sbrandon.potter@amd.com int index = 0; 1810360SN/A if (!tc->getMemProxy().tryReadString(path, 18116701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 18126701Sgblack@eecs.umich.edu return -EFAULT; 1813360SN/A } 181410796Sbrandon.potter@amd.com 1815360SN/A TypedBufferArg<typename OS::timeval [2]> 18166109Ssanchezd@stanford.edu tp(process->getSyscallArg(tc, index)); 18176109Ssanchezd@stanford.edu tp.copyIn(tc->getMemProxy()); 1818360SN/A 18198706Sandreas.hansson@arm.com struct timeval hostTimeval[2]; 1820360SN/A for (int i = 0; i < 2; ++i) { 18211458SN/A hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 1822360SN/A hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 1823360SN/A } 1824360SN/A 18251999SN/A // Adjust path for current working directory 18261999SN/A path = process->fullPath(path); 18271999SN/A 182811851Sbrandon.potter@amd.com int result = utimes(path.c_str(), hostTimeval); 18292680Sktlim@umich.edu 18301999SN/A if (result < 0) 18311999SN/A return -errno; 18321999SN/A 18336701Sgblack@eecs.umich.edu return 0; 18348852Sandreas.hansson@arm.com} 18356701Sgblack@eecs.umich.edu 18366701Sgblack@eecs.umich.edutemplate <class OS> 18376701Sgblack@eecs.umich.eduSyscallReturn 18381999SN/AexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 18396701Sgblack@eecs.umich.edu{ 18406701Sgblack@eecs.umich.edu desc->setFlags(0); 18418706Sandreas.hansson@arm.com 18421999SN/A int index = 0; 18431999SN/A std::string path; 184411906SBrandon.Potter@amd.com SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 18458737Skoansin.tan@gmail.com if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 18468737Skoansin.tan@gmail.com return -EFAULT; 18471999SN/A 18483669Sbinkertn@umich.edu if (access(path.c_str(), F_OK) == -1) 18493669Sbinkertn@umich.edu return -EACCES; 18503669Sbinkertn@umich.edu 18513669Sbinkertn@umich.edu auto read_in = [](std::vector<std::string> & vect, 18521999SN/A SETranslatingPortProxy & mem_proxy, 18531999SN/A Addr mem_loc) 18541999SN/A { 18551999SN/A for (int inc = 0; ; inc++) { 18561999SN/A BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 18571999SN/A b.copyIn(mem_proxy); 18581999SN/A 185911886Sbrandon.potter@amd.com if (!*(Addr*)b.bufferPtr()) 186011886Sbrandon.potter@amd.com break; 186111886Sbrandon.potter@amd.com 186211886Sbrandon.potter@amd.com vect.push_back(std::string()); 186311886Sbrandon.potter@amd.com mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 186411886Sbrandon.potter@amd.com } 186511886Sbrandon.potter@amd.com }; 186611886Sbrandon.potter@amd.com 186711886Sbrandon.potter@amd.com /** 186811886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 186911886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 187011886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 187111886Sbrandon.potter@amd.com * constructor. 187211886Sbrandon.potter@amd.com */ 187311886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 187411886Sbrandon.potter@amd.com pp->executable = path; 187511886Sbrandon.potter@amd.com Addr argv_mem_loc = p->getSyscallArg(tc, index); 187611886Sbrandon.potter@amd.com read_in(pp->cmd, mem_proxy, argv_mem_loc); 187711886Sbrandon.potter@amd.com Addr envp_mem_loc = p->getSyscallArg(tc, index); 187811886Sbrandon.potter@amd.com read_in(pp->env, mem_proxy, envp_mem_loc); 187911886Sbrandon.potter@amd.com pp->uid = p->uid(); 188011886Sbrandon.potter@amd.com pp->egid = p->egid(); 188111886Sbrandon.potter@amd.com pp->euid = p->euid(); 188211886Sbrandon.potter@amd.com pp->gid = p->gid(); 188311886Sbrandon.potter@amd.com pp->ppid = p->ppid(); 188411886Sbrandon.potter@amd.com pp->pid = p->pid(); 188511886Sbrandon.potter@amd.com pp->input.assign("cin"); 188611886Sbrandon.potter@amd.com pp->output.assign("cout"); 188711886Sbrandon.potter@amd.com pp->errout.assign("cerr"); 188811886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 188911886Sbrandon.potter@amd.com pp->system = p->system; 189011886Sbrandon.potter@amd.com /** 189111886Sbrandon.potter@amd.com * Prevent process object creation with identical PIDs (which will trip 189211886Sbrandon.potter@amd.com * a fatal check in Process constructor). The execve call is supposed to 189311886Sbrandon.potter@amd.com * take over the currently executing process' identity but replace 189411886Sbrandon.potter@amd.com * whatever it is doing with a new process image. Instead of hijacking 189511886Sbrandon.potter@amd.com * the process object in the simulator, we create a new process object 189611886Sbrandon.potter@amd.com * and bind to the previous process' thread below (hijacking the thread). 189711886Sbrandon.potter@amd.com */ 189811886Sbrandon.potter@amd.com p->system->PIDs.erase(p->pid()); 189911886Sbrandon.potter@amd.com Process *new_p = pp->create(); 190011886Sbrandon.potter@amd.com delete pp; 190111886Sbrandon.potter@amd.com 190211886Sbrandon.potter@amd.com /** 190311886Sbrandon.potter@amd.com * Work through the file descriptor array and close any files marked 190411886Sbrandon.potter@amd.com * close-on-exec. 190511886Sbrandon.potter@amd.com */ 190611886Sbrandon.potter@amd.com new_p->fds = p->fds; 190711886Sbrandon.potter@amd.com for (int i = 0; i < new_p->fds->getSize(); i++) { 190811886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 190911886Sbrandon.potter@amd.com if (fdep && fdep->getCOE()) 191011886Sbrandon.potter@amd.com new_p->fds->closeFDEntry(i); 191111886Sbrandon.potter@amd.com } 191211886Sbrandon.potter@amd.com 191311886Sbrandon.potter@amd.com *new_p->sigchld = true; 191411886Sbrandon.potter@amd.com 191511886Sbrandon.potter@amd.com delete p; 191611886Sbrandon.potter@amd.com tc->clearArchRegs(); 191711886Sbrandon.potter@amd.com tc->setProcessPtr(new_p); 191811886Sbrandon.potter@amd.com new_p->assignThreadContext(tc->contextId()); 191911886Sbrandon.potter@amd.com new_p->initState(); 192011886Sbrandon.potter@amd.com tc->activate(); 192111886Sbrandon.potter@amd.com TheISA::PCState pcState = tc->pcState(); 192211886Sbrandon.potter@amd.com tc->setNPC(pcState.instAddr()); 192311886Sbrandon.potter@amd.com 192411886Sbrandon.potter@amd.com desc->setFlags(SyscallDesc::SuppressReturnValue); 192511886Sbrandon.potter@amd.com return 0; 192611886Sbrandon.potter@amd.com} 192711886Sbrandon.potter@amd.com 192811886Sbrandon.potter@amd.com/// Target getrusage() function. 192911886Sbrandon.potter@amd.comtemplate <class OS> 193011886Sbrandon.potter@amd.comSyscallReturn 193111886Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 193211886Sbrandon.potter@amd.com ThreadContext *tc) 193311886Sbrandon.potter@amd.com{ 193411886Sbrandon.potter@amd.com int index = 0; 193511886Sbrandon.potter@amd.com int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 193611886Sbrandon.potter@amd.com TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 193711886Sbrandon.potter@amd.com 193811886Sbrandon.potter@amd.com rup->ru_utime.tv_sec = 0; 193911886Sbrandon.potter@amd.com rup->ru_utime.tv_usec = 0; 194011886Sbrandon.potter@amd.com rup->ru_stime.tv_sec = 0; 194111886Sbrandon.potter@amd.com rup->ru_stime.tv_usec = 0; 194211886Sbrandon.potter@amd.com rup->ru_maxrss = 0; 194311886Sbrandon.potter@amd.com rup->ru_ixrss = 0; 194411886Sbrandon.potter@amd.com rup->ru_idrss = 0; 194511886Sbrandon.potter@amd.com rup->ru_isrss = 0; 194611886Sbrandon.potter@amd.com rup->ru_minflt = 0; 194711886Sbrandon.potter@amd.com rup->ru_majflt = 0; 194811886Sbrandon.potter@amd.com rup->ru_nswap = 0; 194911886Sbrandon.potter@amd.com rup->ru_inblock = 0; 195011886Sbrandon.potter@amd.com rup->ru_oublock = 0; 195111886Sbrandon.potter@amd.com rup->ru_msgsnd = 0; 1952378SN/A rup->ru_msgrcv = 0; 1953360SN/A rup->ru_nsignals = 0; 19541450SN/A rup->ru_nvcsw = 0; 195511851Sbrandon.potter@amd.com rup->ru_nivcsw = 0; 19562680Sktlim@umich.edu 1957360SN/A switch (who) { 19586701Sgblack@eecs.umich.edu case OS::TGT_RUSAGE_SELF: 19596701Sgblack@eecs.umich.edu getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 19606701Sgblack@eecs.umich.edu rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 1961360SN/A rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 19623670Sbinkertn@umich.edu break; 19633670Sbinkertn@umich.edu 1964360SN/A case OS::TGT_RUSAGE_CHILDREN: 1965360SN/A // do nothing. We have no child processes, so they take no time. 1966360SN/A break; 1967360SN/A 1968360SN/A default: 1969360SN/A // don't really handle THREAD or CHILDREN, but just warn and 1970360SN/A // plow ahead 1971360SN/A warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1972360SN/A who); 1973360SN/A } 1974360SN/A 1975360SN/A rup.copyOut(tc->getMemProxy()); 1976360SN/A 1977360SN/A return 0; 1978360SN/A} 1979360SN/A 1980360SN/A/// Target times() function. 19813670Sbinkertn@umich.edutemplate <class OS> 19823670Sbinkertn@umich.eduSyscallReturn 198310796Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process, 19848737Skoansin.tan@gmail.com ThreadContext *tc) 19858737Skoansin.tan@gmail.com{ 19863670Sbinkertn@umich.edu int index = 0; 19873670Sbinkertn@umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 19883670Sbinkertn@umich.edu 19893670Sbinkertn@umich.edu // Fill in the time structure (in clocks) 19903670Sbinkertn@umich.edu int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 19913670Sbinkertn@umich.edu bufp->tms_utime = clocks; 19923670Sbinkertn@umich.edu bufp->tms_stime = 0; 19933670Sbinkertn@umich.edu bufp->tms_cutime = 0; 19943670Sbinkertn@umich.edu bufp->tms_cstime = 0; 19953670Sbinkertn@umich.edu 19963670Sbinkertn@umich.edu // Convert to host endianness 19973670Sbinkertn@umich.edu bufp->tms_utime = TheISA::htog(bufp->tms_utime); 19983670Sbinkertn@umich.edu 19998706Sandreas.hansson@arm.com // Write back 2000360SN/A bufp.copyOut(tc->getMemProxy()); 20011458SN/A 2002360SN/A // Return clock ticks since system boot 2003360SN/A return clocks; 20046683Stjones1@inf.ed.ac.uk} 20056683Stjones1@inf.ed.ac.uk 20066683Stjones1@inf.ed.ac.uk/// Target time() function. 200711851Sbrandon.potter@amd.comtemplate <class OS> 200811851Sbrandon.potter@amd.comSyscallReturn 20096683Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 20106701Sgblack@eecs.umich.edu{ 20116701Sgblack@eecs.umich.edu typename OS::time_t sec, usec; 20126683Stjones1@inf.ed.ac.uk getElapsedTimeMicro(sec, usec); 20136683Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 20147823Ssteve.reinhardt@amd.com 20156683Stjones1@inf.ed.ac.uk int index = 0; 20166683Stjones1@inf.ed.ac.uk Addr taddr = (Addr)process->getSyscallArg(tc, index); 20176683Stjones1@inf.ed.ac.uk if (taddr != 0) { 20186683Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 20196683Stjones1@inf.ed.ac.uk t = TheISA::htog(t); 20206683Stjones1@inf.ed.ac.uk SETranslatingPortProxy &p = tc->getMemProxy(); 20218737Skoansin.tan@gmail.com p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 20226683Stjones1@inf.ed.ac.uk } 20236683Stjones1@inf.ed.ac.uk return sec; 20248706Sandreas.hansson@arm.com} 20256683Stjones1@inf.ed.ac.uk 20266683Stjones1@inf.ed.ac.uktemplate <class OS> 20276683Stjones1@inf.ed.ac.ukSyscallReturn 20286683Stjones1@inf.ed.ac.uktgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 20292553SN/A{ 20306684Stjones1@inf.ed.ac.uk int index = 0; 20316684Stjones1@inf.ed.ac.uk int tgid = process->getSyscallArg(tc, index); 20326684Stjones1@inf.ed.ac.uk int tid = process->getSyscallArg(tc, index); 203311851Sbrandon.potter@amd.com int sig = process->getSyscallArg(tc, index); 20346684Stjones1@inf.ed.ac.uk 20356684Stjones1@inf.ed.ac.uk /** 203610796Sbrandon.potter@amd.com * This system call is intended to allow killing a specific thread 20376684Stjones1@inf.ed.ac.uk * within an arbitrary thread group if sanctioned with permission checks. 20386684Stjones1@inf.ed.ac.uk * It's usually true that threads share the termination signal as pointed 20396701Sgblack@eecs.umich.edu * out by the pthread_kill man page and this seems to be the intended 20406701Sgblack@eecs.umich.edu * usage. Due to this being an emulated environment, assume the following: 204111321Ssteve.reinhardt@amd.com * Threads are allowed to call tgkill because the EUID for all threads 20426684Stjones1@inf.ed.ac.uk * should be the same. There is no signal handling mechanism for kernel 20438737Skoansin.tan@gmail.com * registration of signal handlers since signals are poorly supported in 20448852Sandreas.hansson@arm.com * emulation mode. Since signal handlers cannot be registered, all 20458852Sandreas.hansson@arm.com * threads within in a thread group must share the termination signal. 20466684Stjones1@inf.ed.ac.uk * We never exhaust PIDs so there's no chance of finding the wrong one 20476684Stjones1@inf.ed.ac.uk * due to PID rollover. 20486684Stjones1@inf.ed.ac.uk */ 20492553SN/A 205011910SBrandon.Potter@amd.com System *sys = tc->getSystemPtr(); 205111910SBrandon.Potter@amd.com Process *tgt_proc = nullptr; 205211910SBrandon.Potter@amd.com for (int i = 0; i < sys->numContexts(); i++) { 205311910SBrandon.Potter@amd.com Process *temp = sys->threadContexts[i]->getProcessPtr(); 205411910SBrandon.Potter@amd.com if (temp->pid() == tid) { 205511910SBrandon.Potter@amd.com tgt_proc = temp; 205611910SBrandon.Potter@amd.com break; 205711910SBrandon.Potter@amd.com } 205811910SBrandon.Potter@amd.com } 205911910SBrandon.Potter@amd.com 206011910SBrandon.Potter@amd.com if (sig != 0 || sig != OS::TGT_SIGABRT) 206111910SBrandon.Potter@amd.com return -EINVAL; 206211910SBrandon.Potter@amd.com 206311910SBrandon.Potter@amd.com if (tgt_proc == nullptr) 206411910SBrandon.Potter@amd.com return -ESRCH; 206511910SBrandon.Potter@amd.com 206611910SBrandon.Potter@amd.com if (tgid != -1 && tgt_proc->tgid() != tgid) 206711910SBrandon.Potter@amd.com return -ESRCH; 206811910SBrandon.Potter@amd.com 206911910SBrandon.Potter@amd.com if (sig == OS::TGT_SIGABRT) 207011910SBrandon.Potter@amd.com exitGroupFunc(desc, 252, process, tc); 207111910SBrandon.Potter@amd.com 207211910SBrandon.Potter@amd.com return 0; 207311910SBrandon.Potter@amd.com} 207411910SBrandon.Potter@amd.com 207511910SBrandon.Potter@amd.com 207611910SBrandon.Potter@amd.com#endif // __SIM_SYSCALL_EMUL_HH__ 207711910SBrandon.Potter@amd.com