syscall_emul.hh revision 12416
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 56360SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 57360SN/A defined(__FreeBSD__) || defined(__NetBSD__)) 58360SN/A#define NO_STATFS 1 59360SN/A#else 60360SN/A#define NO_STATFS 0 61360SN/A#endif 6213936SAndrea.Mondelli@ucf.edu 6313933Sbrandon.potter@amd.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 6413933Sbrandon.potter@amd.com defined(__FreeBSD__) || defined(__NetBSD__)) 6513933Sbrandon.potter@amd.com#define NO_FALLOCATE 1 6613936SAndrea.Mondelli@ucf.edu#else 6713936SAndrea.Mondelli@ucf.edu#define NO_FALLOCATE 0 6813936SAndrea.Mondelli@ucf.edu#endif 6913933Sbrandon.potter@amd.com 7013933Sbrandon.potter@amd.com/// 711809SN/A/// @file syscall_emul.hh 7211800Sbrandon.potter@amd.com/// 7311392Sbrandon.potter@amd.com/// This file defines objects used to emulate syscalls from the target 741809SN/A/// application on the host machine. 7511392Sbrandon.potter@amd.com 7613902Sbrandon.potter@amd.com#ifdef __CYGWIN32__ 7713570Sbrandon.potter@amd.com#include <sys/fcntl.h> 7813902Sbrandon.potter@amd.com 7911383Sbrandon.potter@amd.com#endif 8013568Sbrandon.potter@amd.com#include <fcntl.h> 813113Sgblack@eecs.umich.edu#include <sys/mman.h> 828229Snate@binkert.org#include <sys/stat.h> 8313570Sbrandon.potter@amd.com#if (NO_STATFS == 0) 848229Snate@binkert.org#include <sys/statfs.h> 8511594Santhony.gutierrez@amd.com#else 867075Snate@binkert.org#include <sys/mount.h> 878229Snate@binkert.org#endif 8811856Sbrandon.potter@amd.com#include <sys/time.h> 897075Snate@binkert.org#include <sys/uio.h> 90360SN/A#include <unistd.h> 9112461Sgabeblack@google.com 9211886Sbrandon.potter@amd.com#include <cerrno> 9311800Sbrandon.potter@amd.com#include <memory> 9411392Sbrandon.potter@amd.com#include <string> 9512334Sgabeblack@google.com 961354SN/A#include "arch/utility.hh" 976216Snate@binkert.org#include "base/intmath.hh" 986658Snate@binkert.org#include "base/loader/object_file.hh" 992474SN/A#include "base/logging.hh" 1002680Sktlim@umich.edu#include "base/trace.hh" 1018229Snate@binkert.org#include "base/types.hh" 10211886Sbrandon.potter@amd.com#include "config/the_isa.hh" 10310496Ssteve.reinhardt@amd.com#include "cpu/base.hh" 10411911SBrandon.Potter@amd.com#include "cpu/thread_context.hh" 1058229Snate@binkert.org#include "mem/page_table.hh" 10611794Sbrandon.potter@amd.com#include "params/Process.hh" 10711886Sbrandon.potter@amd.com#include "sim/emul_driver.hh" 10810497Ssteve.reinhardt@amd.com#include "sim/futex_map.hh" 10911794Sbrandon.potter@amd.com#include "sim/process.hh" 110360SN/A#include "sim/syscall_debug_macros.hh" 11113629SAndrea.Mondelli@ucf.edu#include "sim/syscall_desc.hh" 11213629SAndrea.Mondelli@ucf.edu#include "sim/syscall_emul_buf.hh" 11313629SAndrea.Mondelli@ucf.edu#include "sim/syscall_return.hh" 11413629SAndrea.Mondelli@ucf.edu 115360SN/A////////////////////////////////////////////////////////////////////// 116360SN/A// 117360SN/A// The following emulation functions are generic enough that they 118360SN/A// don't need to be recompiled for different emulated OS's. They are 119360SN/A// defined in sim/syscall_emul.cc. 120360SN/A// 121360SN/A////////////////////////////////////////////////////////////////////// 122360SN/A 12313933Sbrandon.potter@amd.com 124360SN/A/// Handler for unimplemented syscalls that we haven't thought about. 125378SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 12613995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 127378SN/A 128378SN/A/// Handler for unimplemented syscalls that we never intend to 129378SN/A/// implement (signal handling, etc.) and should not affect the correct 130378SN/A/// behavior of the program. Print a warning only if the appropriate 131378SN/A/// trace flag is enabled. Return success to the target program. 13213995Sbrandon.potter@amd.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num, 133360SN/A Process *p, ThreadContext *tc); 13411760Sbrandon.potter@amd.com 13513995Sbrandon.potter@amd.com// Target fallocateFunc() handler. 13611760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 1376109Ssanchezd@stanford.edu Process *p, ThreadContext *tc); 13813995Sbrandon.potter@amd.com 139378SN/A/// Target exit() handler: terminate current context. 1406109Ssanchezd@stanford.eduSyscallReturn exitFunc(SyscallDesc *desc, int num, 14113995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1426109Ssanchezd@stanford.edu 14311886Sbrandon.potter@amd.com/// Target exit_group() handler: terminate simulation. (exit all threads) 14413995Sbrandon.potter@amd.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 14511886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 146378SN/A 14713995Sbrandon.potter@amd.com/// Target set_tid_address() handler. 148378SN/ASyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1495748SSteve.Reinhardt@amd.com Process *p, ThreadContext *tc); 15013995Sbrandon.potter@amd.com 151378SN/A/// Target getpagesize() handler. 152378SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 15313995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 154378SN/A 155378SN/A/// Target brk() handler: set brk address. 15613995Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 157378SN/A Process *p, ThreadContext *tc); 1584118Sgblack@eecs.umich.edu 15913995Sbrandon.potter@amd.com/// Target close() handler. 1604118Sgblack@eecs.umich.eduSyscallReturn closeFunc(SyscallDesc *desc, int num, 161378SN/A Process *p, ThreadContext *tc); 16213995Sbrandon.potter@amd.com 163378SN/A// Target read() handler. 16413568Sbrandon.potter@amd.comSyscallReturn readFunc(SyscallDesc *desc, int num, 16513995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 16613568Sbrandon.potter@amd.com 167378SN/A/// Target write() handler. 16813995Sbrandon.potter@amd.comSyscallReturn writeFunc(SyscallDesc *desc, int num, 169360SN/A Process *p, ThreadContext *tc); 1705513SMichael.Adler@intel.com 17113995Sbrandon.potter@amd.com/// Target lseek() handler. 1725513SMichael.Adler@intel.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 17310203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 17413995Sbrandon.potter@amd.com 17510203SAli.Saidi@ARM.com/// Target _llseek() handler. 17613995Sbrandon.potter@amd.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1775513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 178511SN/A 17913995Sbrandon.potter@amd.com/// Target munmap() handler. 18010633Smichaelupton@gmail.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 18113995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 182511SN/A 18312795Smattdsinclair@gmail.com/// Target gethostname() handler. 18413995Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 18512795Smattdsinclair@gmail.com Process *p, ThreadContext *tc); 18612796Smattdsinclair@gmail.com 18713995Sbrandon.potter@amd.com/// Target getcwd() handler. 18812796Smattdsinclair@gmail.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1895513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 19013995Sbrandon.potter@amd.com 1915513SMichael.Adler@intel.com/// Target readlink() handler. 19213031Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19313995Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 19413031Sbrandon.potter@amd.com int index = 0); 19513031Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19613995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 19713031Sbrandon.potter@amd.com 19813031Sbrandon.potter@amd.com/// Target unlink() handler. 19913995Sbrandon.potter@amd.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 20013031Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 201511SN/A int index); 20213995Sbrandon.potter@amd.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2031706SN/A Process *p, ThreadContext *tc); 2041706SN/A 2051706SN/A/// Target mkdir() handler. 20613995Sbrandon.potter@amd.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2071706SN/A Process *p, ThreadContext *tc); 2081706SN/A 2091706SN/A/// Target rename() handler. 21013995Sbrandon.potter@amd.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 2111706SN/A Process *p, ThreadContext *tc); 212511SN/A 2136703Svince@csl.cornell.edu 21413995Sbrandon.potter@amd.com/// Target truncate() handler. 2156703Svince@csl.cornell.eduSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2166685Stjones1@inf.ed.ac.uk Process *p, ThreadContext *tc); 21713995Sbrandon.potter@amd.com 2186685Stjones1@inf.ed.ac.uk 2196685Stjones1@inf.ed.ac.uk/// Target ftruncate() handler. 2205513SMichael.Adler@intel.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 22113995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2225513SMichael.Adler@intel.com 22311885Sbrandon.potter@amd.com 22413995Sbrandon.potter@amd.com/// Target truncate64() handler. 2255513SMichael.Adler@intel.comSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2261999SN/A Process *p, ThreadContext *tc); 22713995Sbrandon.potter@amd.com 2281999SN/A/// Target ftruncate64() handler. 22911885Sbrandon.potter@amd.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 23013995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2311999SN/A 2321999SN/A 23313995Sbrandon.potter@amd.com/// Target umask() handler. 2341999SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num, 2353079Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 23613995Sbrandon.potter@amd.com 2373079Sstever@eecs.umich.edu/// Target gettid() handler. 23811908SBrandon.Potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 23913995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 24011908SBrandon.Potter@amd.com 24111875Sbrandon.potter@amd.com/// Target chown() handler. 24213995Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num, 2432093SN/A Process *p, ThreadContext *tc); 2442687Sksewell@umich.edu 24513995Sbrandon.potter@amd.com/// Target setpgid() handler. 2462687Sksewell@umich.eduSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2472238SN/A Process *p, ThreadContext *tc); 24813995Sbrandon.potter@amd.com 2492238SN/A/// Target fchown() handler. 25011908SBrandon.Potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 25113995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 25211908SBrandon.Potter@amd.com 25311908SBrandon.Potter@amd.com/// Target dup() handler. 25413995Sbrandon.potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num, 25513995Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 25611908SBrandon.Potter@amd.com 2572238SN/A/// Target dup2() handler. 25813995Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 2592238SN/A Process *process, ThreadContext *tc); 26013571Sbrandon.potter@amd.com 26113995Sbrandon.potter@amd.com/// Target fcntl() handler. 26213571Sbrandon.potter@amd.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 26313568Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 26413995Sbrandon.potter@amd.com 26513568Sbrandon.potter@amd.com/// Target fcntl64() handler. 26613568Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 26713995Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 26813568Sbrandon.potter@amd.com 26913568Sbrandon.potter@amd.com/// Target setuid() handler. 27013995Sbrandon.potter@amd.comSyscallReturn setuidFunc(SyscallDesc *desc, int num, 27113568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 27213448Sciro.santilli@arm.com 27313031Sbrandon.potter@amd.com/// Target pipe() handler. 27413995Sbrandon.potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 27513448Sciro.santilli@arm.com Process *p, ThreadContext *tc); 27613031Sbrandon.potter@amd.com 27713539Sjavier.setoain@arm.com/// Internal pipe() handler. 27813539Sjavier.setoain@arm.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 27913995Sbrandon.potter@amd.com ThreadContext *tc, bool pseudoPipe); 28013539Sjavier.setoain@arm.com 28113539Sjavier.setoain@arm.com/// Target getpid() handler. 28213569Sbrandon.potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num, 28313995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 28413569Sbrandon.potter@amd.com 28513569Sbrandon.potter@amd.com/// Target getuid() handler. 28613995Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, 28713569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 28813569Sbrandon.potter@amd.com 28913995Sbrandon.potter@amd.com/// Target getgid() handler. 29013569Sbrandon.potter@amd.comSyscallReturn getgidFunc(SyscallDesc *desc, int num, 29113569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 29213995Sbrandon.potter@amd.com 29313569Sbrandon.potter@amd.com/// Target getppid() handler. 29413031Sbrandon.potter@amd.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, 29513995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2962238SN/A 2972238SN/A/// Target geteuid() handler. 29813995Sbrandon.potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2992238SN/A Process *p, ThreadContext *tc); 3002238SN/A 30113995Sbrandon.potter@amd.com/// Target getegid() handler. 3022238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 3032238SN/A Process *p, ThreadContext *tc); 30413995Sbrandon.potter@amd.com 3052238SN/A/// Target access() handler 3062238SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num, 30713995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3082238SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num, 3099455Smitch.hayenga+gem5@gmail.com Process *p, ThreadContext *tc, 31013995Sbrandon.potter@amd.com int index); 31113995Sbrandon.potter@amd.com 31211851Sbrandon.potter@amd.com/// Futex system call 3139455Smitch.hayenga+gem5@gmail.com/// Implemented by Daniel Sanchez 31413571Sbrandon.potter@amd.com/// Used by printf's in multi-threaded apps 31513995Sbrandon.potter@amd.comtemplate <class OS> 31613571Sbrandon.potter@amd.comSyscallReturn 31713571Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 31813995Sbrandon.potter@amd.com ThreadContext *tc) 31913571Sbrandon.potter@amd.com{ 32013571Sbrandon.potter@amd.com using namespace std; 32113995Sbrandon.potter@amd.com 32213571Sbrandon.potter@amd.com int index = 0; 3239112Smarc.orr@gmail.com Addr uaddr = process->getSyscallArg(tc, index); 32411906SBrandon.Potter@amd.com int op = process->getSyscallArg(tc, index); 32511906SBrandon.Potter@amd.com int val = process->getSyscallArg(tc, index); 3269112Smarc.orr@gmail.com 3279112Smarc.orr@gmail.com /* 32813995Sbrandon.potter@amd.com * Unsupported option that does not affect the correctness of the 3299112Smarc.orr@gmail.com * application. This is a performance optimization utilized by Linux. 33011911SBrandon.Potter@amd.com */ 3319112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 33211911SBrandon.Potter@amd.com 33313995Sbrandon.potter@amd.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 33413995Sbrandon.potter@amd.com 33511911SBrandon.Potter@amd.com if (OS::TGT_FUTEX_WAIT == op) { 33611911SBrandon.Potter@amd.com // Ensure futex system call accessed atomically. 33711911SBrandon.Potter@amd.com BufferArg buf(uaddr, sizeof(int)); 33813642Sqtt2@cornell.edu buf.copyIn(tc->getMemProxy()); 33913642Sqtt2@cornell.edu int mem_val = *(int*)buf.bufferPtr(); 34013642Sqtt2@cornell.edu 3419112Smarc.orr@gmail.com /* 34211911SBrandon.Potter@amd.com * The value in memory at uaddr is not equal with the expected val 34311911SBrandon.Potter@amd.com * (a different thread must have changed it before the system call was 34411911SBrandon.Potter@amd.com * invoked). In this case, we need to throw an error. 34511911SBrandon.Potter@amd.com */ 3469238Slluc.alvarez@bsc.es if (val != mem_val) 34713642Sqtt2@cornell.edu return -OS::TGT_EWOULDBLOCK; 3489112Smarc.orr@gmail.com 34911911SBrandon.Potter@amd.com futex_map.suspend(uaddr, process->tgid(), tc); 3509112Smarc.orr@gmail.com 35113642Sqtt2@cornell.edu return 0; 35211911SBrandon.Potter@amd.com } else if (OS::TGT_FUTEX_WAKE == op) { 35311911SBrandon.Potter@amd.com return futex_map.wakeup(uaddr, process->tgid(), val); 35411911SBrandon.Potter@amd.com } 35511911SBrandon.Potter@amd.com 3569112Smarc.orr@gmail.com warn("futex: op %d not implemented; ignoring.", op); 35711911SBrandon.Potter@amd.com return -ENOSYS; 35811911SBrandon.Potter@amd.com} 35911911SBrandon.Potter@amd.com 36011911SBrandon.Potter@amd.com 36111911SBrandon.Potter@amd.com/// Pseudo Funcs - These functions use a different return convension, 36211911SBrandon.Potter@amd.com/// returning a second value in a register other than the normal return register 3639112Smarc.orr@gmail.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3649112Smarc.orr@gmail.com Process *process, ThreadContext *tc); 36513642Sqtt2@cornell.edu 36613642Sqtt2@cornell.edu/// Target getpidPseudo() handler. 36713642Sqtt2@cornell.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 36813642Sqtt2@cornell.edu Process *p, ThreadContext *tc); 36913642Sqtt2@cornell.edu 37011911SBrandon.Potter@amd.com/// Target getuidPseudo() handler. 3719112Smarc.orr@gmail.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 37211911SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 37311911SBrandon.Potter@amd.com 37413642Sqtt2@cornell.edu/// Target getgidPseudo() handler. 37513642Sqtt2@cornell.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 37613650Smw828@cornell.edu Process *p, ThreadContext *tc); 37713650Smw828@cornell.edu 37813650Smw828@cornell.edu 37913650Smw828@cornell.edu/// A readable name for 1,000,000, for converting microseconds to seconds. 38013650Smw828@cornell.educonst int one_million = 1000000; 38113650Smw828@cornell.edu/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 38213650Smw828@cornell.educonst int one_billion = 1000000000; 38313650Smw828@cornell.edu 38413650Smw828@cornell.edu/// Approximate seconds since the epoch (1/1/1970). About a billion, 38513650Smw828@cornell.edu/// by my reckoning. We want to keep this a constant (not use the 38613650Smw828@cornell.edu/// real-world time) to keep simulations repeatable. 38713650Smw828@cornell.educonst unsigned seconds_since_epoch = 1000000000; 38813650Smw828@cornell.edu 38913650Smw828@cornell.edu/// Helper function to convert current elapsed time to seconds and 39013651Smw828@cornell.edu/// microseconds. 39113651Smw828@cornell.edutemplate <class T1, class T2> 39213651Smw828@cornell.eduvoid 39313651Smw828@cornell.edugetElapsedTimeMicro(T1 &sec, T2 &usec) 39413651Smw828@cornell.edu{ 39513651Smw828@cornell.edu uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 39613651Smw828@cornell.edu sec = elapsed_usecs / one_million; 39713651Smw828@cornell.edu usec = elapsed_usecs % one_million; 39813651Smw828@cornell.edu} 39913651Smw828@cornell.edu 40013651Smw828@cornell.edu/// Helper function to convert current elapsed time to seconds and 40113651Smw828@cornell.edu/// nanoseconds. 40213651Smw828@cornell.edutemplate <class T1, class T2> 40313651Smw828@cornell.eduvoid 40413651Smw828@cornell.edugetElapsedTimeNano(T1 &sec, T2 &nsec) 40513651Smw828@cornell.edu{ 40613651Smw828@cornell.edu uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 40713651Smw828@cornell.edu sec = elapsed_nsecs / one_billion; 40813651Smw828@cornell.edu nsec = elapsed_nsecs % one_billion; 40913651Smw828@cornell.edu} 41013651Smw828@cornell.edu 41113651Smw828@cornell.edu////////////////////////////////////////////////////////////////////// 41213651Smw828@cornell.edu// 41313651Smw828@cornell.edu// The following emulation functions are generic, but need to be 41413651Smw828@cornell.edu// templated to account for differences in types, constants, etc. 41513651Smw828@cornell.edu// 41613651Smw828@cornell.edu////////////////////////////////////////////////////////////////////// 41713651Smw828@cornell.edu 41813651Smw828@cornell.edu typedef struct statfs hst_statfs; 41913651Smw828@cornell.edu#if NO_STAT64 42013651Smw828@cornell.edu typedef struct stat hst_stat; 42113651Smw828@cornell.edu typedef struct stat hst_stat64; 42213651Smw828@cornell.edu#else 42313651Smw828@cornell.edu typedef struct stat hst_stat; 42413651Smw828@cornell.edu typedef struct stat64 hst_stat64; 42513651Smw828@cornell.edu#endif 42613651Smw828@cornell.edu 42713651Smw828@cornell.edu//// Helper function to convert a host stat buffer to a target stat 42813651Smw828@cornell.edu//// buffer. Also copies the target buffer out to the simulated 42913651Smw828@cornell.edu//// memory space. Used by stat(), fstat(), and lstat(). 43013651Smw828@cornell.edu 43113651Smw828@cornell.edutemplate <typename target_stat, typename host_stat> 43213651Smw828@cornell.eduvoid 43313651Smw828@cornell.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 43413651Smw828@cornell.edu{ 43513651Smw828@cornell.edu using namespace TheISA; 43613651Smw828@cornell.edu 43713651Smw828@cornell.edu if (fakeTTY) 43813651Smw828@cornell.edu tgt->st_dev = 0xA; 43913651Smw828@cornell.edu else 44013651Smw828@cornell.edu tgt->st_dev = host->st_dev; 44113651Smw828@cornell.edu tgt->st_dev = TheISA::htog(tgt->st_dev); 44213651Smw828@cornell.edu tgt->st_ino = host->st_ino; 44313651Smw828@cornell.edu tgt->st_ino = TheISA::htog(tgt->st_ino); 44413651Smw828@cornell.edu tgt->st_mode = host->st_mode; 44513651Smw828@cornell.edu if (fakeTTY) { 44613651Smw828@cornell.edu // Claim to be a character device 44713651Smw828@cornell.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 44813651Smw828@cornell.edu tgt->st_mode |= S_IFCHR; // Set S_IFCHR 44913651Smw828@cornell.edu } 45013651Smw828@cornell.edu tgt->st_mode = TheISA::htog(tgt->st_mode); 45113651Smw828@cornell.edu tgt->st_nlink = host->st_nlink; 45213651Smw828@cornell.edu tgt->st_nlink = TheISA::htog(tgt->st_nlink); 45313651Smw828@cornell.edu tgt->st_uid = host->st_uid; 45413651Smw828@cornell.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 45513651Smw828@cornell.edu tgt->st_gid = host->st_gid; 45613651Smw828@cornell.edu tgt->st_gid = TheISA::htog(tgt->st_gid); 45713651Smw828@cornell.edu if (fakeTTY) 45813651Smw828@cornell.edu tgt->st_rdev = 0x880d; 45913651Smw828@cornell.edu else 46013651Smw828@cornell.edu tgt->st_rdev = host->st_rdev; 4619112Smarc.orr@gmail.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 46211911SBrandon.Potter@amd.com tgt->st_size = host->st_size; 46311911SBrandon.Potter@amd.com tgt->st_size = TheISA::htog(tgt->st_size); 4649112Smarc.orr@gmail.com tgt->st_atimeX = host->st_atime; 4659112Smarc.orr@gmail.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4662238SN/A tgt->st_mtimeX = host->st_mtime; 4672238SN/A tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4682238SN/A tgt->st_ctimeX = host->st_ctime; 46913995Sbrandon.potter@amd.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4702238SN/A // Force the block size to be 8KB. This helps to ensure buffered io works 4712238SN/A // consistently across different hosts. 47213995Sbrandon.potter@amd.com tgt->st_blksize = 0x2000; 4732238SN/A tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4742238SN/A tgt->st_blocks = host->st_blocks; 47513995Sbrandon.potter@amd.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4762238SN/A} 4772238SN/A 47813995Sbrandon.potter@amd.com// Same for stat64 4792238SN/A 4802238SN/Atemplate <typename target_stat, typename host_stat64> 4811354SN/Avoid 4821354SN/AconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 48310796Sbrandon.potter@amd.com{ 48410796Sbrandon.potter@amd.com using namespace TheISA; 4851354SN/A 4861354SN/A convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4871354SN/A#if defined(STAT_HAVE_NSEC) 4881354SN/A tgt->st_atime_nsec = host->st_atime_nsec; 4891354SN/A tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 4901354SN/A tgt->st_mtime_nsec = host->st_mtime_nsec; 4911354SN/A tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 4921354SN/A tgt->st_ctime_nsec = host->st_ctime_nsec; 4931354SN/A tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 4941354SN/A#else 49510796Sbrandon.potter@amd.com tgt->st_atime_nsec = 0; 4961354SN/A tgt->st_mtime_nsec = 0; 49710796Sbrandon.potter@amd.com tgt->st_ctime_nsec = 0; 4981354SN/A#endif 4991354SN/A} 5001354SN/A 5011354SN/A// Here are a couple of convenience functions 50210796Sbrandon.potter@amd.comtemplate<class OS> 50310796Sbrandon.potter@amd.comvoid 50410796Sbrandon.potter@amd.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 50510796Sbrandon.potter@amd.com hst_stat *host, bool fakeTTY = false) 50610796Sbrandon.potter@amd.com{ 50710796Sbrandon.potter@amd.com typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 50810796Sbrandon.potter@amd.com tgt_stat_buf tgt(addr); 50910796Sbrandon.potter@amd.com convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 51010796Sbrandon.potter@amd.com tgt.copyOut(mem); 51110796Sbrandon.potter@amd.com} 51210796Sbrandon.potter@amd.com 513360SN/Atemplate<class OS> 514360SN/Avoid 515360SN/AcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 516360SN/A hst_stat64 *host, bool fakeTTY = false) 517360SN/A{ 518360SN/A typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 519360SN/A tgt_stat_buf tgt(addr); 52011759Sbrandon.potter@amd.com convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5213113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5223113Sgblack@eecs.umich.edu} 5233113Sgblack@eecs.umich.edu 5243113Sgblack@eecs.umich.edutemplate <class OS> 5253113Sgblack@eecs.umich.eduvoid 5263113Sgblack@eecs.umich.educopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 5273113Sgblack@eecs.umich.edu hst_statfs *host) 5283113Sgblack@eecs.umich.edu{ 5293113Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 5303113Sgblack@eecs.umich.edu 5313113Sgblack@eecs.umich.edu tgt->f_type = TheISA::htog(host->f_type); 5323113Sgblack@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5333113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_iosize); 53412032Sandreas.sandberg@arm.com#else 5353113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_bsize); 5363113Sgblack@eecs.umich.edu#endif 5374189Sgblack@eecs.umich.edu tgt->f_blocks = TheISA::htog(host->f_blocks); 5384189Sgblack@eecs.umich.edu tgt->f_bfree = TheISA::htog(host->f_bfree); 5393113Sgblack@eecs.umich.edu tgt->f_bavail = TheISA::htog(host->f_bavail); 5403113Sgblack@eecs.umich.edu tgt->f_files = TheISA::htog(host->f_files); 5413113Sgblack@eecs.umich.edu tgt->f_ffree = TheISA::htog(host->f_ffree); 5423113Sgblack@eecs.umich.edu memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 5438737Skoansin.tan@gmail.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5443113Sgblack@eecs.umich.edu tgt->f_namelen = TheISA::htog(host->f_namemax); 5458737Skoansin.tan@gmail.com tgt->f_frsize = TheISA::htog(host->f_bsize); 5463277Sgblack@eecs.umich.edu#elif defined(__APPLE__) 5475515SMichael.Adler@intel.com tgt->f_namelen = 0; 5485515SMichael.Adler@intel.com tgt->f_frsize = 0; 5495515SMichael.Adler@intel.com#else 5505515SMichael.Adler@intel.com tgt->f_namelen = TheISA::htog(host->f_namelen); 5515515SMichael.Adler@intel.com tgt->f_frsize = TheISA::htog(host->f_frsize); 5528737Skoansin.tan@gmail.com#endif 5533277Sgblack@eecs.umich.edu#if defined(__linux__) 5548737Skoansin.tan@gmail.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 5553277Sgblack@eecs.umich.edu#else 5568737Skoansin.tan@gmail.com /* 5573277Sgblack@eecs.umich.edu * The fields are different sizes per OS. Don't bother with 5588737Skoansin.tan@gmail.com * f_spare or f_reserved on non-Linux for now. 5593113Sgblack@eecs.umich.edu */ 5603113Sgblack@eecs.umich.edu memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 5613113Sgblack@eecs.umich.edu#endif 5623113Sgblack@eecs.umich.edu 5638737Skoansin.tan@gmail.com tgt.copyOut(mem); 5643113Sgblack@eecs.umich.edu} 5658737Skoansin.tan@gmail.com 5663114Sgblack@eecs.umich.edu/// Target ioctl() handler. For the most part, programs call ioctl() 5678737Skoansin.tan@gmail.com/// only to find out if their stdout is a tty, to determine whether to 5683114Sgblack@eecs.umich.edu/// do line or block buffering. We always claim that output fds are 5698737Skoansin.tan@gmail.com/// not TTYs to provide repeatable results. 5703114Sgblack@eecs.umich.edutemplate <class OS> 5718737Skoansin.tan@gmail.comSyscallReturn 57211906SBrandon.Potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 5734061Sgblack@eecs.umich.edu{ 5744061Sgblack@eecs.umich.edu int index = 0; 5758737Skoansin.tan@gmail.com int tgt_fd = p->getSyscallArg(tc, index); 5763113Sgblack@eecs.umich.edu unsigned req = p->getSyscallArg(tc, index); 5778737Skoansin.tan@gmail.com 5783113Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 5793113Sgblack@eecs.umich.edu 5803113Sgblack@eecs.umich.edu if (OS::isTtyReq(req)) 5813113Sgblack@eecs.umich.edu return -ENOTTY; 5823113Sgblack@eecs.umich.edu 58312032Sandreas.sandberg@arm.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 5843113Sgblack@eecs.umich.edu if (!dfdp) 5853113Sgblack@eecs.umich.edu return -EBADF; 5864189Sgblack@eecs.umich.edu 5874189Sgblack@eecs.umich.edu /** 5883113Sgblack@eecs.umich.edu * If the driver is valid, issue the ioctl through it. Otherwise, 5893113Sgblack@eecs.umich.edu * there's an implicit assumption that the device is a TTY type and we 5903113Sgblack@eecs.umich.edu * return that we do not have a valid TTY. 5918737Skoansin.tan@gmail.com */ 5923113Sgblack@eecs.umich.edu EmulatedDriver *emul_driver = dfdp->getDriver(); 5938737Skoansin.tan@gmail.com if (emul_driver) 5943113Sgblack@eecs.umich.edu return emul_driver->ioctl(p, tc, req); 5958737Skoansin.tan@gmail.com 5963113Sgblack@eecs.umich.edu /** 5973113Sgblack@eecs.umich.edu * For lack of a better return code, return ENOTTY. Ideally, we should 5983113Sgblack@eecs.umich.edu * return something better here, but at least we issue the warning. 5993113Sgblack@eecs.umich.edu */ 6003113Sgblack@eecs.umich.edu warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 6013113Sgblack@eecs.umich.edu tgt_fd, req, tc->pcState()); 6023113Sgblack@eecs.umich.edu return -ENOTTY; 60311906SBrandon.Potter@amd.com} 6043113Sgblack@eecs.umich.edu 60512032Sandreas.sandberg@arm.comtemplate <class OS> 60614020Sgabeblack@google.comSyscallReturn 60711906SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 6083113Sgblack@eecs.umich.edu bool isopenat) 6093113Sgblack@eecs.umich.edu{ 6103113Sgblack@eecs.umich.edu int index = 0; 6113113Sgblack@eecs.umich.edu int tgt_dirfd = -1; 6123113Sgblack@eecs.umich.edu 6133113Sgblack@eecs.umich.edu /** 6143113Sgblack@eecs.umich.edu * If using the openat variant, read in the target directory file 6153113Sgblack@eecs.umich.edu * descriptor from the simulated process. 61612032Sandreas.sandberg@arm.com */ 61714020Sgabeblack@google.com if (isopenat) 61811906SBrandon.Potter@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 6193113Sgblack@eecs.umich.edu 6203113Sgblack@eecs.umich.edu /** 6213113Sgblack@eecs.umich.edu * Retrieve the simulated process' memory proxy and then read in the path 6226686Stjones1@inf.ed.ac.uk * string from that memory space into the host's working memory space. 6233113Sgblack@eecs.umich.edu */ 6243113Sgblack@eecs.umich.edu std::string path; 6253113Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 62611759Sbrandon.potter@amd.com return -EFAULT; 62712032Sandreas.sandberg@arm.com 62814020Sgabeblack@google.com#ifdef __CYGWIN32__ 62911759Sbrandon.potter@amd.com int host_flags = O_BINARY; 63011759Sbrandon.potter@amd.com#else 63111759Sbrandon.potter@amd.com int host_flags = 0; 63211759Sbrandon.potter@amd.com#endif 63311812Sbaz21@cam.ac.uk /** 63411812Sbaz21@cam.ac.uk * Translate target flags into host flags. Flags exist which are not 63511812Sbaz21@cam.ac.uk * ported between architectures which can cause check failures. 63611759Sbrandon.potter@amd.com */ 63711812Sbaz21@cam.ac.uk int tgt_flags = p->getSyscallArg(tc, index); 63811759Sbrandon.potter@amd.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 63911759Sbrandon.potter@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 64011759Sbrandon.potter@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 64111759Sbrandon.potter@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 64211759Sbrandon.potter@amd.com } 64311759Sbrandon.potter@amd.com } 64411759Sbrandon.potter@amd.com if (tgt_flags) { 64511812Sbaz21@cam.ac.uk warn("open%s: cannot decode flags 0x%x", 64611812Sbaz21@cam.ac.uk isopenat ? "at" : "", tgt_flags); 64711812Sbaz21@cam.ac.uk } 64811812Sbaz21@cam.ac.uk#ifdef __CYGWIN32__ 64911812Sbaz21@cam.ac.uk host_flags |= O_BINARY; 65011812Sbaz21@cam.ac.uk#endif 65111812Sbaz21@cam.ac.uk 65211759Sbrandon.potter@amd.com int mode = p->getSyscallArg(tc, index); 65311759Sbrandon.potter@amd.com 65411812Sbaz21@cam.ac.uk /** 65511812Sbaz21@cam.ac.uk * If the simulated process called open or openat with AT_FDCWD specified, 65611759Sbrandon.potter@amd.com * take the current working directory value which was passed into the 65711812Sbaz21@cam.ac.uk * process class as a Python parameter and append the current path to 65811812Sbaz21@cam.ac.uk * create a full path. 65911812Sbaz21@cam.ac.uk * Otherwise, openat with a valid target directory file descriptor has 66011812Sbaz21@cam.ac.uk * been called. If the path option, which was passed in as a parameter, 66111812Sbaz21@cam.ac.uk * is not absolute, retrieve the directory file descriptor's path and 66211812Sbaz21@cam.ac.uk * prepend it to the path passed in as a parameter. 66311812Sbaz21@cam.ac.uk * In every case, we should have a full path (which is relevant to the 66411759Sbrandon.potter@amd.com * host) to work with after this block has been passed. 66511759Sbrandon.potter@amd.com */ 66611759Sbrandon.potter@amd.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 66711759Sbrandon.potter@amd.com path = p->fullPath(path); 668378SN/A } else if (!startswith(path, "/")) { 669378SN/A std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 6709141Smarc.orr@gmail.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 6719141Smarc.orr@gmail.com if (!ffdp) 672360SN/A return -EBADF; 6731450SN/A path.insert(0, ffdp->getFileName()); 67413995Sbrandon.potter@amd.com } 675360SN/A 6766701Sgblack@eecs.umich.edu /** 67713995Sbrandon.potter@amd.com * Since this is an emulated environment, we create pseudo file 67813995Sbrandon.potter@amd.com * descriptors for device requests that have been registered with 67911856Sbrandon.potter@amd.com * the process class through Python; this allows us to create a file 68011856Sbrandon.potter@amd.com * descriptor for subsequent ioctl or mmap calls. 681360SN/A */ 68213907Salexandru.dutu@amd.com if (startswith(path, "/dev/")) { 683360SN/A std::string filename = path.substr(strlen("/dev/")); 68411856Sbrandon.potter@amd.com EmulatedDriver *drv = p->findDriver(filename); 68511856Sbrandon.potter@amd.com if (drv) { 68610496Ssteve.reinhardt@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 68711856Sbrandon.potter@amd.com "driver open with path[%s]\n", 68813902Sbrandon.potter@amd.com isopenat ? "at" : "", path.c_str()); 68913902Sbrandon.potter@amd.com return drv->open(p, tc, mode, host_flags); 69013902Sbrandon.potter@amd.com } 69113995Sbrandon.potter@amd.com /** 69213902Sbrandon.potter@amd.com * Fall through here for pass through to host devices, such 69313902Sbrandon.potter@amd.com * as /dev/zero 69413902Sbrandon.potter@amd.com */ 69513902Sbrandon.potter@amd.com } 69613902Sbrandon.potter@amd.com 69713902Sbrandon.potter@amd.com /** 69813902Sbrandon.potter@amd.com * Some special paths and files cannot be called on the host and need 69913902Sbrandon.potter@amd.com * to be handled as special cases inside the simulator. 70013902Sbrandon.potter@amd.com * If the full path that was created above does not match any of the 70113902Sbrandon.potter@amd.com * special cases, pass it through to the open call on the host to let 70213902Sbrandon.potter@amd.com * the host open the file on our behalf. 70313902Sbrandon.potter@amd.com * If the host cannot open the file, return the host's error code back 70413902Sbrandon.potter@amd.com * through the system call to the simulated process. 70513902Sbrandon.potter@amd.com */ 70613902Sbrandon.potter@amd.com int sim_fd = -1; 70713902Sbrandon.potter@amd.com std::vector<std::string> special_paths = 70813902Sbrandon.potter@amd.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 70913902Sbrandon.potter@amd.com for (auto entry : special_paths) { 71013902Sbrandon.potter@amd.com if (startswith(path, entry)) 71113902Sbrandon.potter@amd.com sim_fd = OS::openSpecialFile(path, p, tc); 71213902Sbrandon.potter@amd.com } 71313902Sbrandon.potter@amd.com if (sim_fd == -1) { 71413902Sbrandon.potter@amd.com sim_fd = open(path.c_str(), host_flags, mode); 71513902Sbrandon.potter@amd.com } 71613902Sbrandon.potter@amd.com if (sim_fd == -1) { 71713902Sbrandon.potter@amd.com int local = -errno; 71813902Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 71913902Sbrandon.potter@amd.com isopenat ? "at" : "", path.c_str()); 72013902Sbrandon.potter@amd.com return local; 72113936SAndrea.Mondelli@ucf.edu } 72213902Sbrandon.potter@amd.com 72313902Sbrandon.potter@amd.com /** 72413902Sbrandon.potter@amd.com * The file was opened successfully and needs to be recorded in the 72513902Sbrandon.potter@amd.com * process' file descriptor array so that it can be retrieved later. 72613936SAndrea.Mondelli@ucf.edu * The target file descriptor that is chosen will be the lowest unused 72713902Sbrandon.potter@amd.com * file descriptor. 72813902Sbrandon.potter@amd.com * Return the indirect target file descriptor back to the simulated 72913902Sbrandon.potter@amd.com * process to act as a handle for the opened file. 73013902Sbrandon.potter@amd.com */ 73113902Sbrandon.potter@amd.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 73213902Sbrandon.potter@amd.com int tgt_fd = p->fds->allocFD(ffdp); 73313902Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 73413902Sbrandon.potter@amd.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 73513902Sbrandon.potter@amd.com return tgt_fd; 73613902Sbrandon.potter@amd.com} 73713902Sbrandon.potter@amd.com 73813902Sbrandon.potter@amd.com/// Target open() handler. 73913902Sbrandon.potter@amd.comtemplate <class OS> 74013902Sbrandon.potter@amd.comSyscallReturn 74110496Ssteve.reinhardt@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 74211856Sbrandon.potter@amd.com ThreadContext *tc) 74311856Sbrandon.potter@amd.com{ 74411856Sbrandon.potter@amd.com return openImpl<OS>(desc, callnum, process, tc, false); 74511856Sbrandon.potter@amd.com} 74611856Sbrandon.potter@amd.com 74710930Sbrandon.potter@amd.com/// Target openat() handler. 7489141Smarc.orr@gmail.comtemplate <class OS> 749360SN/ASyscallReturn 750360SN/AopenatFunc(SyscallDesc *desc, int callnum, Process *process, 751360SN/A ThreadContext *tc) 75211907SBrandon.Potter@amd.com{ 75313995Sbrandon.potter@amd.com return openImpl<OS>(desc, callnum, process, tc, true); 754360SN/A} 75511907SBrandon.Potter@amd.com 75613995Sbrandon.potter@amd.com/// Target unlinkat() handler. 75711907SBrandon.Potter@amd.comtemplate <class OS> 75811907SBrandon.Potter@amd.comSyscallReturn 75911907SBrandon.Potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 76011907SBrandon.Potter@amd.com ThreadContext *tc) 76111907SBrandon.Potter@amd.com{ 76211907SBrandon.Potter@amd.com int index = 0; 76311907SBrandon.Potter@amd.com int dirfd = process->getSyscallArg(tc, index); 76411907SBrandon.Potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 76511907SBrandon.Potter@amd.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 76611907SBrandon.Potter@amd.com 76711907SBrandon.Potter@amd.com return unlinkHelper(desc, callnum, process, tc, 1); 76811907SBrandon.Potter@amd.com} 76911907SBrandon.Potter@amd.com 770360SN/A/// Target facessat() handler 77111907SBrandon.Potter@amd.comtemplate <class OS> 7721458SN/ASyscallReturn 773360SN/AfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 77411907SBrandon.Potter@amd.com ThreadContext *tc) 77511907SBrandon.Potter@amd.com{ 77611907SBrandon.Potter@amd.com int index = 0; 77711907SBrandon.Potter@amd.com int dirfd = process->getSyscallArg(tc, index); 77811907SBrandon.Potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 77911907SBrandon.Potter@amd.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 78011907SBrandon.Potter@amd.com return accessFunc(desc, callnum, process, tc, 1); 78111907SBrandon.Potter@amd.com} 78211907SBrandon.Potter@amd.com 78311907SBrandon.Potter@amd.com/// Target readlinkat() handler 784360SN/Atemplate <class OS> 78511907SBrandon.Potter@amd.comSyscallReturn 78611907SBrandon.Potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 78711907SBrandon.Potter@amd.com ThreadContext *tc) 788360SN/A{ 789360SN/A int index = 0; 79011907SBrandon.Potter@amd.com int dirfd = process->getSyscallArg(tc, index); 79111907SBrandon.Potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 79211907SBrandon.Potter@amd.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 79311907SBrandon.Potter@amd.com return readlinkFunc(desc, callnum, process, tc, 1); 794360SN/A} 79511907SBrandon.Potter@amd.com 796360SN/A/// Target renameat() handler. 797360SN/Atemplate <class OS> 79811907SBrandon.Potter@amd.comSyscallReturn 7993669Sbinkertn@umich.edurenameatFunc(SyscallDesc *desc, int callnum, Process *process, 80011907SBrandon.Potter@amd.com ThreadContext *tc) 80111907SBrandon.Potter@amd.com{ 80211907SBrandon.Potter@amd.com int index = 0; 80311907SBrandon.Potter@amd.com 80411907SBrandon.Potter@amd.com int olddirfd = process->getSyscallArg(tc, index); 80511907SBrandon.Potter@amd.com if (olddirfd != OS::TGT_AT_FDCWD) 80611907SBrandon.Potter@amd.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 80711907SBrandon.Potter@amd.com 80811907SBrandon.Potter@amd.com std::string old_name; 80911907SBrandon.Potter@amd.com 81011907SBrandon.Potter@amd.com if (!tc->getMemProxy().tryReadString(old_name, 81111907SBrandon.Potter@amd.com process->getSyscallArg(tc, index))) 81213883Sdavid.hashe@amd.com return -EFAULT; 81313883Sdavid.hashe@amd.com 81413883Sdavid.hashe@amd.com int newdirfd = process->getSyscallArg(tc, index); 81513883Sdavid.hashe@amd.com if (newdirfd != OS::TGT_AT_FDCWD) 81613883Sdavid.hashe@amd.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 81711907SBrandon.Potter@amd.com 81811907SBrandon.Potter@amd.com std::string new_name; 81911907SBrandon.Potter@amd.com 82011907SBrandon.Potter@amd.com if (!tc->getMemProxy().tryReadString(new_name, 82111907SBrandon.Potter@amd.com process->getSyscallArg(tc, index))) 82213883Sdavid.hashe@amd.com return -EFAULT; 82313883Sdavid.hashe@amd.com 82411907SBrandon.Potter@amd.com // Adjust path for current working directory 8251706SN/A old_name = process->fullPath(old_name); 82611907SBrandon.Potter@amd.com new_name = process->fullPath(new_name); 82711907SBrandon.Potter@amd.com 82811907SBrandon.Potter@amd.com int result = rename(old_name.c_str(), new_name.c_str()); 82911907SBrandon.Potter@amd.com return (result == -1) ? -errno : result; 83011907SBrandon.Potter@amd.com} 83111907SBrandon.Potter@amd.com 83213883Sdavid.hashe@amd.com/// Target sysinfo() handler. 83313883Sdavid.hashe@amd.comtemplate <class OS> 83411907SBrandon.Potter@amd.comSyscallReturn 83511907SBrandon.Potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 83611907SBrandon.Potter@amd.com ThreadContext *tc) 83711907SBrandon.Potter@amd.com{ 83813883Sdavid.hashe@amd.com 83913995Sbrandon.potter@amd.com int index = 0; 84010496Ssteve.reinhardt@amd.com TypedBufferArg<typename OS::tgt_sysinfo> 84111907SBrandon.Potter@amd.com sysinfo(process->getSyscallArg(tc, index)); 84211907SBrandon.Potter@amd.com 84311907SBrandon.Potter@amd.com sysinfo->uptime = seconds_since_epoch; 84411907SBrandon.Potter@amd.com sysinfo->totalram = process->system->memSize(); 84510496Ssteve.reinhardt@amd.com sysinfo->mem_unit = 1; 84610496Ssteve.reinhardt@amd.com 84711907SBrandon.Potter@amd.com sysinfo.copyOut(tc->getMemProxy()); 84813883Sdavid.hashe@amd.com 84913883Sdavid.hashe@amd.com return 0; 85013883Sdavid.hashe@amd.com} 85113883Sdavid.hashe@amd.com 85213883Sdavid.hashe@amd.com/// Target chmod() handler. 85313883Sdavid.hashe@amd.comtemplate <class OS> 85413883Sdavid.hashe@amd.comSyscallReturn 85513883Sdavid.hashe@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process, 85613883Sdavid.hashe@amd.com ThreadContext *tc) 85713883Sdavid.hashe@amd.com{ 85813883Sdavid.hashe@amd.com std::string path; 85913883Sdavid.hashe@amd.com 86013883Sdavid.hashe@amd.com int index = 0; 86113883Sdavid.hashe@amd.com if (!tc->getMemProxy().tryReadString(path, 86213883Sdavid.hashe@amd.com process->getSyscallArg(tc, index))) { 86313883Sdavid.hashe@amd.com return -EFAULT; 86413883Sdavid.hashe@amd.com } 86513883Sdavid.hashe@amd.com 86613883Sdavid.hashe@amd.com uint32_t mode = process->getSyscallArg(tc, index); 86713883Sdavid.hashe@amd.com mode_t hostMode = 0; 86813883Sdavid.hashe@amd.com 86913883Sdavid.hashe@amd.com // XXX translate mode flags via OS::something??? 87011907SBrandon.Potter@amd.com hostMode = mode; 87111907SBrandon.Potter@amd.com 87213883Sdavid.hashe@amd.com // Adjust path for current working directory 87311907SBrandon.Potter@amd.com path = process->fullPath(path); 87413994Santhony.gutierrez@amd.com 87511907SBrandon.Potter@amd.com // do the chmod 87613883Sdavid.hashe@amd.com int result = chmod(path.c_str(), hostMode); 87713883Sdavid.hashe@amd.com if (result < 0) 87813883Sdavid.hashe@amd.com return -errno; 87913883Sdavid.hashe@amd.com 88011907SBrandon.Potter@amd.com return 0; 88111907SBrandon.Potter@amd.com} 88213883Sdavid.hashe@amd.com 88313883Sdavid.hashe@amd.com 88411907SBrandon.Potter@amd.com/// Target fchmod() handler. 88511907SBrandon.Potter@amd.comtemplate <class OS> 88611907SBrandon.Potter@amd.comSyscallReturn 88713883Sdavid.hashe@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 88813883Sdavid.hashe@amd.com{ 88913883Sdavid.hashe@amd.com int index = 0; 89011907SBrandon.Potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 89111907SBrandon.Potter@amd.com uint32_t mode = p->getSyscallArg(tc, index); 892360SN/A 89311907SBrandon.Potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 89411907SBrandon.Potter@amd.com if (!ffdp) 89511907SBrandon.Potter@amd.com return -EBADF; 89611907SBrandon.Potter@amd.com int sim_fd = ffdp->getSimFD(); 89711907SBrandon.Potter@amd.com 89811907SBrandon.Potter@amd.com mode_t hostMode = mode; 89911907SBrandon.Potter@amd.com 90011907SBrandon.Potter@amd.com int result = fchmod(sim_fd, hostMode); 90111907SBrandon.Potter@amd.com 90211907SBrandon.Potter@amd.com return (result < 0) ? -errno : 0; 90313883Sdavid.hashe@amd.com} 90413883Sdavid.hashe@amd.com 90513883Sdavid.hashe@amd.com/// Target mremap() handler. 90611907SBrandon.Potter@amd.comtemplate <class OS> 907360SN/ASyscallReturn 908360SN/AmremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 90910027SChris.Adeniyi-Jones@arm.com{ 91010027SChris.Adeniyi-Jones@arm.com int index = 0; 91110027SChris.Adeniyi-Jones@arm.com Addr start = process->getSyscallArg(tc, index); 91213995Sbrandon.potter@amd.com uint64_t old_length = process->getSyscallArg(tc, index); 91310027SChris.Adeniyi-Jones@arm.com uint64_t new_length = process->getSyscallArg(tc, index); 91413995Sbrandon.potter@amd.com uint64_t flags = process->getSyscallArg(tc, index); 91510027SChris.Adeniyi-Jones@arm.com uint64_t provided_address = 0; 91610027SChris.Adeniyi-Jones@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 91710027SChris.Adeniyi-Jones@arm.com 91810027SChris.Adeniyi-Jones@arm.com if (use_provided_address) 91910027SChris.Adeniyi-Jones@arm.com provided_address = process->getSyscallArg(tc, index); 92013995Sbrandon.potter@amd.com 92110027SChris.Adeniyi-Jones@arm.com if ((start % TheISA::PageBytes != 0) || 92213995Sbrandon.potter@amd.com (provided_address % TheISA::PageBytes != 0)) { 92310027SChris.Adeniyi-Jones@arm.com warn("mremap failing: arguments not page aligned"); 92410027SChris.Adeniyi-Jones@arm.com return -EINVAL; 92510633Smichaelupton@gmail.com } 92610633Smichaelupton@gmail.com 92710633Smichaelupton@gmail.com new_length = roundUp(new_length, TheISA::PageBytes); 92813995Sbrandon.potter@amd.com 92910633Smichaelupton@gmail.com if (new_length > old_length) { 93010633Smichaelupton@gmail.com std::shared_ptr<MemState> mem_state = process->memState; 93113995Sbrandon.potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 93210633Smichaelupton@gmail.com 93310633Smichaelupton@gmail.com if ((start + old_length) == mmap_end && 93410633Smichaelupton@gmail.com (!use_provided_address || provided_address == start)) { 93510633Smichaelupton@gmail.com // This case cannot occur when growing downward, as 93613995Sbrandon.potter@amd.com // start is greater than or equal to mmap_end. 93710633Smichaelupton@gmail.com uint64_t diff = new_length - old_length; 93810633Smichaelupton@gmail.com process->allocateMem(mmap_end, diff); 93910203SAli.Saidi@ARM.com mem_state->setMmapEnd(mmap_end + diff); 94010203SAli.Saidi@ARM.com return start; 94110203SAli.Saidi@ARM.com } else { 94213995Sbrandon.potter@amd.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 94310203SAli.Saidi@ARM.com warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 94410203SAli.Saidi@ARM.com return -ENOMEM; 94513995Sbrandon.potter@amd.com } else { 94610203SAli.Saidi@ARM.com uint64_t new_start = provided_address; 94710203SAli.Saidi@ARM.com if (!use_provided_address) { 94810203SAli.Saidi@ARM.com new_start = process->mmapGrowsDown() ? 94913995Sbrandon.potter@amd.com mmap_end - new_length : mmap_end; 95010203SAli.Saidi@ARM.com mmap_end = process->mmapGrowsDown() ? 95110203SAli.Saidi@ARM.com new_start : mmap_end + new_length; 95210203SAli.Saidi@ARM.com mem_state->setMmapEnd(mmap_end); 95310203SAli.Saidi@ARM.com } 95410203SAli.Saidi@ARM.com 95513995Sbrandon.potter@amd.com process->pTable->remap(start, old_length, new_start); 95610203SAli.Saidi@ARM.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 95710203SAli.Saidi@ARM.com new_start, new_start + new_length, 95813995Sbrandon.potter@amd.com new_length - old_length); 95910203SAli.Saidi@ARM.com // add on the remaining unallocated pages 96010203SAli.Saidi@ARM.com process->allocateMem(new_start + old_length, 96110203SAli.Saidi@ARM.com new_length - old_length, 96213995Sbrandon.potter@amd.com use_provided_address /* clobber */); 96310203SAli.Saidi@ARM.com if (use_provided_address && 96410203SAli.Saidi@ARM.com ((new_start + new_length > mem_state->getMmapEnd() && 96510850SGiacomo.Gabrielli@arm.com !process->mmapGrowsDown()) || 96610850SGiacomo.Gabrielli@arm.com (new_start < mem_state->getMmapEnd() && 96710850SGiacomo.Gabrielli@arm.com process->mmapGrowsDown()))) { 96813995Sbrandon.potter@amd.com // something fishy going on here, at least notify the user 96910850SGiacomo.Gabrielli@arm.com // @todo: increase mmap_end? 97010850SGiacomo.Gabrielli@arm.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 97113995Sbrandon.potter@amd.com } 97210850SGiacomo.Gabrielli@arm.com warn("returning %08p as start\n", new_start); 97310850SGiacomo.Gabrielli@arm.com return new_start; 97410850SGiacomo.Gabrielli@arm.com } 97510850SGiacomo.Gabrielli@arm.com } 97610850SGiacomo.Gabrielli@arm.com } else { 97710850SGiacomo.Gabrielli@arm.com if (use_provided_address && provided_address != start) 97810850SGiacomo.Gabrielli@arm.com process->pTable->remap(start, new_length, provided_address); 97910850SGiacomo.Gabrielli@arm.com process->pTable->unmap(start + new_length, old_length - new_length); 98010850SGiacomo.Gabrielli@arm.com return use_provided_address ? provided_address : start; 98110850SGiacomo.Gabrielli@arm.com } 98210850SGiacomo.Gabrielli@arm.com} 98310850SGiacomo.Gabrielli@arm.com 98410850SGiacomo.Gabrielli@arm.com/// Target stat() handler. 98510850SGiacomo.Gabrielli@arm.comtemplate <class OS> 98610850SGiacomo.Gabrielli@arm.comSyscallReturn 98710850SGiacomo.Gabrielli@arm.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 98810850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 98910850SGiacomo.Gabrielli@arm.com{ 99010850SGiacomo.Gabrielli@arm.com std::string path; 99110850SGiacomo.Gabrielli@arm.com 99210850SGiacomo.Gabrielli@arm.com int index = 0; 99313883Sdavid.hashe@amd.com if (!tc->getMemProxy().tryReadString(path, 99413883Sdavid.hashe@amd.com process->getSyscallArg(tc, index))) { 99513883Sdavid.hashe@amd.com return -EFAULT; 99610850SGiacomo.Gabrielli@arm.com } 99710850SGiacomo.Gabrielli@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 99810850SGiacomo.Gabrielli@arm.com 99910850SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 100010850SGiacomo.Gabrielli@arm.com path = process->fullPath(path); 10016640Svince@csl.cornell.edu 10026640Svince@csl.cornell.edu struct stat hostBuf; 10036640Svince@csl.cornell.edu int result = stat(path.c_str(), &hostBuf); 100413995Sbrandon.potter@amd.com 10056640Svince@csl.cornell.edu if (result < 0) 10066701Sgblack@eecs.umich.edu return -errno; 100713995Sbrandon.potter@amd.com 100813995Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10096701Sgblack@eecs.umich.edu 101010793Sbrandon.potter@amd.com return 0; 10116640Svince@csl.cornell.edu} 101211758Sbrandon.potter@amd.com 101311758Sbrandon.potter@amd.com 101411758Sbrandon.potter@amd.com/// Target stat64() handler. 10156640Svince@csl.cornell.edutemplate <class OS> 10168706Sandreas.hansson@arm.comSyscallReturn 10176640Svince@csl.cornell.edustat64Func(SyscallDesc *desc, int callnum, Process *process, 10186701Sgblack@eecs.umich.edu ThreadContext *tc) 10196640Svince@csl.cornell.edu{ 1020360SN/A std::string path; 10211999SN/A 10221999SN/A int index = 0; 10231999SN/A if (!tc->getMemProxy().tryReadString(path, 102413995Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 10251999SN/A return -EFAULT; 10261999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 102713995Sbrandon.potter@amd.com 10281999SN/A // Adjust path for current working directory 10296701Sgblack@eecs.umich.edu path = process->fullPath(path); 10308852Sandreas.hansson@arm.com 10316701Sgblack@eecs.umich.edu#if NO_STAT64 10321999SN/A struct stat hostBuf; 10336701Sgblack@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 10341999SN/A#else 10356701Sgblack@eecs.umich.edu struct stat64 hostBuf; 10361999SN/A int result = stat64(path.c_str(), &hostBuf); 10371999SN/A#endif 10381999SN/A 10391999SN/A if (result < 0) 10401999SN/A return -errno; 104113883Sdavid.hashe@amd.com 104213883Sdavid.hashe@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10433669Sbinkertn@umich.edu 10441999SN/A return 0; 10451999SN/A} 10461999SN/A 10472218SN/A 10481999SN/A/// Target fstatat64() handler. 10491999SN/Atemplate <class OS> 10501999SN/ASyscallReturn 10511999SN/Afstatat64Func(SyscallDesc *desc, int callnum, Process *process, 105213570Sbrandon.potter@amd.com ThreadContext *tc) 105313570Sbrandon.potter@amd.com{ 105413995Sbrandon.potter@amd.com int index = 0; 105513570Sbrandon.potter@amd.com int dirfd = process->getSyscallArg(tc, index); 105613570Sbrandon.potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 105713995Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 105813570Sbrandon.potter@amd.com 105913570Sbrandon.potter@amd.com std::string path; 106013570Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 106113570Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 106213570Sbrandon.potter@amd.com return -EFAULT; 106313570Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 106413570Sbrandon.potter@amd.com 106513570Sbrandon.potter@amd.com // Adjust path for current working directory 106613570Sbrandon.potter@amd.com path = process->fullPath(path); 106713570Sbrandon.potter@amd.com 106813570Sbrandon.potter@amd.com#if NO_STAT64 106913570Sbrandon.potter@amd.com struct stat hostBuf; 107013570Sbrandon.potter@amd.com int result = stat(path.c_str(), &hostBuf); 107113570Sbrandon.potter@amd.com#else 107213570Sbrandon.potter@amd.com struct stat64 hostBuf; 107313570Sbrandon.potter@amd.com int result = stat64(path.c_str(), &hostBuf); 107413570Sbrandon.potter@amd.com#endif 107513570Sbrandon.potter@amd.com 107613570Sbrandon.potter@amd.com if (result < 0) 107713570Sbrandon.potter@amd.com return -errno; 107813570Sbrandon.potter@amd.com 107913570Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 108013570Sbrandon.potter@amd.com 108113570Sbrandon.potter@amd.com return 0; 108213570Sbrandon.potter@amd.com} 108313570Sbrandon.potter@amd.com 108413570Sbrandon.potter@amd.com 108513570Sbrandon.potter@amd.com/// Target fstat64() handler. 108613570Sbrandon.potter@amd.comtemplate <class OS> 108713570Sbrandon.potter@amd.comSyscallReturn 108813570Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 108913570Sbrandon.potter@amd.com{ 109013570Sbrandon.potter@amd.com int index = 0; 109113570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 109213570Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 109313570Sbrandon.potter@amd.com 109413570Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 109513570Sbrandon.potter@amd.com if (!ffdp) 109613570Sbrandon.potter@amd.com return -EBADF; 109713570Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 109813570Sbrandon.potter@amd.com 109913570Sbrandon.potter@amd.com#if NO_STAT64 110013570Sbrandon.potter@amd.com struct stat hostBuf; 110113570Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 110213570Sbrandon.potter@amd.com#else 110313570Sbrandon.potter@amd.com struct stat64 hostBuf; 110413570Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 110513570Sbrandon.potter@amd.com#endif 110613570Sbrandon.potter@amd.com 110713570Sbrandon.potter@amd.com if (result < 0) 110813570Sbrandon.potter@amd.com return -errno; 110913570Sbrandon.potter@amd.com 111013570Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 111113570Sbrandon.potter@amd.com 111213570Sbrandon.potter@amd.com return 0; 111313570Sbrandon.potter@amd.com} 111413570Sbrandon.potter@amd.com 111513570Sbrandon.potter@amd.com 111613570Sbrandon.potter@amd.com/// Target lstat() handler. 111713570Sbrandon.potter@amd.comtemplate <class OS> 111813570Sbrandon.potter@amd.comSyscallReturn 111913570Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 112013570Sbrandon.potter@amd.com ThreadContext *tc) 112113570Sbrandon.potter@amd.com{ 112213570Sbrandon.potter@amd.com std::string path; 112313570Sbrandon.potter@amd.com 112413570Sbrandon.potter@amd.com int index = 0; 112513570Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 112613570Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 11271999SN/A return -EFAULT; 11281999SN/A } 11291999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 11301999SN/A 113113995Sbrandon.potter@amd.com // Adjust path for current working directory 11321999SN/A path = process->fullPath(path); 11336701Sgblack@eecs.umich.edu 113413995Sbrandon.potter@amd.com struct stat hostBuf; 113511856Sbrandon.potter@amd.com int result = lstat(path.c_str(), &hostBuf); 113611856Sbrandon.potter@amd.com 113710931Sbrandon.potter@amd.com if (result < 0) 113811856Sbrandon.potter@amd.com return -errno; 113911856Sbrandon.potter@amd.com 11401999SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 114111856Sbrandon.potter@amd.com 11421999SN/A return 0; 114311856Sbrandon.potter@amd.com} 11441999SN/A 114511856Sbrandon.potter@amd.com/// Target lstat64() handler. 11461999SN/Atemplate <class OS> 114711856Sbrandon.potter@amd.comSyscallReturn 11481999SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 11491999SN/A ThreadContext *tc) 11505877Shsul@eecs.umich.edu{ 11515877Shsul@eecs.umich.edu std::string path; 11525877Shsul@eecs.umich.edu 115313995Sbrandon.potter@amd.com int index = 0; 11545877Shsul@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 11556701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 115613995Sbrandon.potter@amd.com return -EFAULT; 11576701Sgblack@eecs.umich.edu } 11586701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11596701Sgblack@eecs.umich.edu 11606701Sgblack@eecs.umich.edu // Adjust path for current working directory 116110027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 116210027SChris.Adeniyi-Jones@arm.com 116310027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 116410027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 116510027SChris.Adeniyi-Jones@arm.com int result = lstat(path.c_str(), &hostBuf); 11665877Shsul@eecs.umich.edu#else 116710318Sandreas.hansson@arm.com struct stat64 hostBuf; 116810318Sandreas.hansson@arm.com int result = lstat64(path.c_str(), &hostBuf); 11695877Shsul@eecs.umich.edu#endif 11705877Shsul@eecs.umich.edu 11715877Shsul@eecs.umich.edu if (result < 0) 11725877Shsul@eecs.umich.edu return -errno; 117310486Stjablin@gmail.com 117410486Stjablin@gmail.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11755877Shsul@eecs.umich.edu 117611905SBrandon.Potter@amd.com return 0; 117711905SBrandon.Potter@amd.com} 117811905SBrandon.Potter@amd.com 117911905SBrandon.Potter@amd.com/// Target fstat() handler. 118010027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 118112206Srico.amslinger@informatik.uni-augsburg.deSyscallReturn 118212206Srico.amslinger@informatik.uni-augsburg.defstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 11835877Shsul@eecs.umich.edu{ 118411905SBrandon.Potter@amd.com int index = 0; 118511905SBrandon.Potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 11865877Shsul@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 11875877Shsul@eecs.umich.edu 118810027SChris.Adeniyi-Jones@arm.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 11895877Shsul@eecs.umich.edu 11905877Shsul@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 11915877Shsul@eecs.umich.edu if (!ffdp) 119212206Srico.amslinger@informatik.uni-augsburg.de return -EBADF; 119312206Srico.amslinger@informatik.uni-augsburg.de int sim_fd = ffdp->getSimFD(); 119412206Srico.amslinger@informatik.uni-augsburg.de 119512206Srico.amslinger@informatik.uni-augsburg.de struct stat hostBuf; 119612206Srico.amslinger@informatik.uni-augsburg.de int result = fstat(sim_fd, &hostBuf); 119712206Srico.amslinger@informatik.uni-augsburg.de 119812206Srico.amslinger@informatik.uni-augsburg.de if (result < 0) 119912206Srico.amslinger@informatik.uni-augsburg.de return -errno; 120012206Srico.amslinger@informatik.uni-augsburg.de 120110027SChris.Adeniyi-Jones@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 120210027SChris.Adeniyi-Jones@arm.com 120310027SChris.Adeniyi-Jones@arm.com return 0; 120410027SChris.Adeniyi-Jones@arm.com} 12055877Shsul@eecs.umich.edu 120610027SChris.Adeniyi-Jones@arm.com 120710027SChris.Adeniyi-Jones@arm.com/// Target statfs() handler. 120810027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 120910027SChris.Adeniyi-Jones@arm.comSyscallReturn 121012206Srico.amslinger@informatik.uni-augsburg.destatfsFunc(SyscallDesc *desc, int callnum, Process *process, 121112206Srico.amslinger@informatik.uni-augsburg.de ThreadContext *tc) 121212206Srico.amslinger@informatik.uni-augsburg.de{ 121312206Srico.amslinger@informatik.uni-augsburg.de#if NO_STATFS 121410027SChris.Adeniyi-Jones@arm.com warn("Host OS cannot support calls to statfs. Ignoring syscall"); 121510027SChris.Adeniyi-Jones@arm.com#else 121610027SChris.Adeniyi-Jones@arm.com std::string path; 121710027SChris.Adeniyi-Jones@arm.com 121810027SChris.Adeniyi-Jones@arm.com int index = 0; 121910027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 12205877Shsul@eecs.umich.edu process->getSyscallArg(tc, index))) { 12215877Shsul@eecs.umich.edu return -EFAULT; 12225877Shsul@eecs.umich.edu } 122310027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 122410027SChris.Adeniyi-Jones@arm.com 12258601Ssteve.reinhardt@amd.com // Adjust path for current working directory 122610027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 12275877Shsul@eecs.umich.edu 12285877Shsul@eecs.umich.edu struct statfs hostBuf; 12291999SN/A int result = statfs(path.c_str(), &hostBuf); 1230378SN/A 1231360SN/A if (result < 0) 12321450SN/A return -errno; 123313995Sbrandon.potter@amd.com 1234360SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1235360SN/A#endif 123613995Sbrandon.potter@amd.com return 0; 1237360SN/A} 12386701Sgblack@eecs.umich.edu 12398852Sandreas.hansson@arm.comtemplate <class OS> 12406701Sgblack@eecs.umich.eduSyscallReturn 12416701Sgblack@eecs.umich.educloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 12426701Sgblack@eecs.umich.edu{ 12436701Sgblack@eecs.umich.edu int index = 0; 1244360SN/A TheISA::IntReg flags = p->getSyscallArg(tc, index); 124513883Sdavid.hashe@amd.com TheISA::IntReg newStack = p->getSyscallArg(tc, index); 124613883Sdavid.hashe@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 12473669Sbinkertn@umich.edu Addr ctidPtr = p->getSyscallArg(tc, index); 1248360SN/A Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 1249360SN/A 1250360SN/A if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 1251360SN/A ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 12522218SN/A ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 1253360SN/A ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 12548706Sandreas.hansson@arm.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 1255360SN/A ((flags & OS::TGT_CLONE_VM) && !(newStack))) 12561458SN/A return -EINVAL; 1257360SN/A 1258360SN/A ThreadContext *ctc; 1259360SN/A if (!(ctc = p->findFreeContext())) 12605074Ssaidi@eecs.umich.edu fatal("clone: no spare thread context in system"); 12615074Ssaidi@eecs.umich.edu 12625074Ssaidi@eecs.umich.edu /** 126313995Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 12645074Ssaidi@eecs.umich.edu * examples of how to create anything but this default constructor. The 12655074Ssaidi@eecs.umich.edu * fields are manually initialized instead of passing parameters to the 126613995Sbrandon.potter@amd.com * constructor. 12675074Ssaidi@eecs.umich.edu */ 12686701Sgblack@eecs.umich.edu ProcessParams *pp = new ProcessParams(); 12698852Sandreas.hansson@arm.com pp->executable.assign(*(new std::string(p->progName()))); 12706701Sgblack@eecs.umich.edu pp->cmd.push_back(*(new std::string(p->progName()))); 12715074Ssaidi@eecs.umich.edu pp->system = p->system; 12726701Sgblack@eecs.umich.edu pp->cwd.assign(p->getcwd()); 12735074Ssaidi@eecs.umich.edu pp->input.assign("stdin"); 127413883Sdavid.hashe@amd.com pp->output.assign("stdout"); 127513883Sdavid.hashe@amd.com pp->errout.assign("stderr"); 12765074Ssaidi@eecs.umich.edu pp->uid = p->uid(); 12775208Ssaidi@eecs.umich.edu pp->euid = p->euid(); 12785208Ssaidi@eecs.umich.edu pp->gid = p->gid(); 12795208Ssaidi@eecs.umich.edu pp->egid = p->egid(); 12805208Ssaidi@eecs.umich.edu 12815074Ssaidi@eecs.umich.edu /* Find the first free PID that's less than the maximum */ 12825074Ssaidi@eecs.umich.edu std::set<int> const& pids = p->system->PIDs; 12835208Ssaidi@eecs.umich.edu int temp_pid = *pids.begin(); 12845074Ssaidi@eecs.umich.edu do { 12855074Ssaidi@eecs.umich.edu temp_pid++; 12865074Ssaidi@eecs.umich.edu } while (pids.find(temp_pid) != pids.end()); 12875074Ssaidi@eecs.umich.edu if (temp_pid >= System::maxPID) 12888706Sandreas.hansson@arm.com fatal("temp_pid is too large: %d", temp_pid); 12895074Ssaidi@eecs.umich.edu 12905074Ssaidi@eecs.umich.edu pp->pid = temp_pid; 12915074Ssaidi@eecs.umich.edu pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 12925074Ssaidi@eecs.umich.edu Process *cp = pp->create(); 12935074Ssaidi@eecs.umich.edu delete pp; 129410027SChris.Adeniyi-Jones@arm.com 129510027SChris.Adeniyi-Jones@arm.com Process *owner = ctc->getProcessPtr(); 129610027SChris.Adeniyi-Jones@arm.com ctc->setProcessPtr(cp); 129713995Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 129810027SChris.Adeniyi-Jones@arm.com owner->revokeThreadContext(ctc->contextId()); 129910027SChris.Adeniyi-Jones@arm.com 130013995Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 130110027SChris.Adeniyi-Jones@arm.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 130210027SChris.Adeniyi-Jones@arm.com long *ptid = (long *)ptidBuf.bufferPtr(); 130310793Sbrandon.potter@amd.com *ptid = cp->pid(); 130410027SChris.Adeniyi-Jones@arm.com ptidBuf.copyOut(tc->getMemProxy()); 130510027SChris.Adeniyi-Jones@arm.com } 130610027SChris.Adeniyi-Jones@arm.com 130710027SChris.Adeniyi-Jones@arm.com cp->initState(); 130810027SChris.Adeniyi-Jones@arm.com p->clone(tc, ctc, cp, flags); 130910027SChris.Adeniyi-Jones@arm.com 131010027SChris.Adeniyi-Jones@arm.com if (flags & OS::TGT_CLONE_THREAD) { 131113883Sdavid.hashe@amd.com delete cp->sigchld; 131213883Sdavid.hashe@amd.com cp->sigchld = p->sigchld; 131310027SChris.Adeniyi-Jones@arm.com } else if (flags & OS::TGT_SIGCHLD) { 131410027SChris.Adeniyi-Jones@arm.com *cp->sigchld = true; 131510027SChris.Adeniyi-Jones@arm.com } 131610027SChris.Adeniyi-Jones@arm.com 131710027SChris.Adeniyi-Jones@arm.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 131810027SChris.Adeniyi-Jones@arm.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 131910027SChris.Adeniyi-Jones@arm.com long *ctid = (long *)ctidBuf.bufferPtr(); 132010027SChris.Adeniyi-Jones@arm.com *ctid = cp->pid(); 132110027SChris.Adeniyi-Jones@arm.com ctidBuf.copyOut(ctc->getMemProxy()); 132210027SChris.Adeniyi-Jones@arm.com } 132310027SChris.Adeniyi-Jones@arm.com 132410027SChris.Adeniyi-Jones@arm.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 132510027SChris.Adeniyi-Jones@arm.com cp->childClearTID = (uint64_t)ctidPtr; 132610027SChris.Adeniyi-Jones@arm.com 132710027SChris.Adeniyi-Jones@arm.com ctc->clearArchRegs(); 132810027SChris.Adeniyi-Jones@arm.com 132910027SChris.Adeniyi-Jones@arm.com#if THE_ISA == ALPHA_ISA 133010027SChris.Adeniyi-Jones@arm.com TheISA::copyMiscRegs(tc, ctc); 13311999SN/A#elif THE_ISA == SPARC_ISA 13321999SN/A TheISA::copyRegs(tc, ctc); 13331999SN/A ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 133413995Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 13351999SN/A ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 13366701Sgblack@eecs.umich.edu ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 133713995Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 133811856Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 133911856Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 134010931Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 134111856Sbrandon.potter@amd.com for (int y = 8; y < 32; y++) 134211856Sbrandon.potter@amd.com ctc->setIntReg(y, tc->readIntReg(y)); 13431999SN/A#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA 134411856Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 13451999SN/A#endif 13462764Sstever@eecs.umich.edu 13472064SN/A#if THE_ISA == X86_ISA 134810931Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_SETTLS) { 13492064SN/A ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 13502064SN/A ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 135110931Sbrandon.potter@amd.com } 13522064SN/A#endif 13531999SN/A 13541999SN/A if (newStack) 13552218SN/A ctc->setIntReg(TheISA::StackPointerReg, newStack); 13561999SN/A 135710931Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 13581999SN/A 13591999SN/A#if THE_ISA == ALPHA_ISA 13601999SN/A ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 13611999SN/A#elif THE_ISA == SPARC_ISA 13621999SN/A tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1363378SN/A ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 1364360SN/A#endif 13651450SN/A 136613995Sbrandon.potter@amd.com ctc->pcState(tc->nextInstAddr()); 1367360SN/A ctc->activate(); 1368360SN/A 136913995Sbrandon.potter@amd.com return cp->pid(); 1370360SN/A} 13716701Sgblack@eecs.umich.edu 13728852Sandreas.hansson@arm.com/// Target fstatfs() handler. 13736701Sgblack@eecs.umich.edutemplate <class OS> 13746701Sgblack@eecs.umich.eduSyscallReturn 13756701Sgblack@eecs.umich.edufstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 13766701Sgblack@eecs.umich.edu{ 1377360SN/A int index = 0; 137813883Sdavid.hashe@amd.com int tgt_fd = p->getSyscallArg(tc, index); 137913883Sdavid.hashe@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 13803669Sbinkertn@umich.edu 1381360SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1382360SN/A if (!ffdp) 1383360SN/A return -EBADF; 1384360SN/A int sim_fd = ffdp->getSimFD(); 13851458SN/A 1386360SN/A struct statfs hostBuf; 13878706Sandreas.hansson@arm.com int result = fstatfs(sim_fd, &hostBuf); 1388360SN/A 13891458SN/A if (result < 0) 1390360SN/A return -errno; 1391360SN/A 13921999SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13931999SN/A 13941999SN/A return 0; 139513995Sbrandon.potter@amd.com} 13961999SN/A 13971999SN/A 139813995Sbrandon.potter@amd.com/// Target writev() handler. 13991999SN/Atemplate <class OS> 14006701Sgblack@eecs.umich.eduSyscallReturn 14018852Sandreas.hansson@arm.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 14026701Sgblack@eecs.umich.edu{ 14036701Sgblack@eecs.umich.edu int index = 0; 14046701Sgblack@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 14056701Sgblack@eecs.umich.edu 14061999SN/A auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 140713883Sdavid.hashe@amd.com if (!hbfdp) 140813883Sdavid.hashe@amd.com return -EBADF; 14093669Sbinkertn@umich.edu int sim_fd = hbfdp->getSimFD(); 14102764Sstever@eecs.umich.edu 14112064SN/A SETranslatingPortProxy &prox = tc->getMemProxy(); 14122064SN/A uint64_t tiov_base = p->getSyscallArg(tc, index); 14132064SN/A size_t count = p->getSyscallArg(tc, index); 14141999SN/A struct iovec hiov[count]; 14151999SN/A for (size_t i = 0; i < count; ++i) { 14162064SN/A typename OS::tgt_iovec tiov; 14171999SN/A 14181999SN/A prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 14191999SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 14201999SN/A hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 14218706Sandreas.hansson@arm.com hiov[i].iov_base = new char [hiov[i].iov_len]; 14221999SN/A prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 14231999SN/A hiov[i].iov_len); 14241999SN/A } 14251999SN/A 1426378SN/A int result = writev(sim_fd, hiov, count); 1427360SN/A 14281450SN/A for (size_t i = 0; i < count; ++i) 142913995Sbrandon.potter@amd.com delete [] (char *)hiov[i].iov_base; 1430360SN/A 14316701Sgblack@eecs.umich.edu if (result < 0) 143213995Sbrandon.potter@amd.com return -errno; 143311856Sbrandon.potter@amd.com 143411856Sbrandon.potter@amd.com return result; 1435360SN/A} 143611380Salexandru.dutu@amd.com 1437360SN/A/// Real mmap handler. 143811856Sbrandon.potter@amd.comtemplate <class OS> 143911856Sbrandon.potter@amd.comSyscallReturn 14401458SN/AmmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 144111856Sbrandon.potter@amd.com bool is_mmap2) 1442360SN/A{ 1443360SN/A int index = 0; 144410931Sbrandon.potter@amd.com Addr start = p->getSyscallArg(tc, index); 1445360SN/A uint64_t length = p->getSyscallArg(tc, index); 1446360SN/A int prot = p->getSyscallArg(tc, index); 14471458SN/A int tgt_flags = p->getSyscallArg(tc, index); 1448360SN/A int tgt_fd = p->getSyscallArg(tc, index); 144910931Sbrandon.potter@amd.com int offset = p->getSyscallArg(tc, index); 14502021SN/A 14511458SN/A if (is_mmap2) 1452360SN/A offset *= TheISA::PageBytes; 1453360SN/A 14541706SN/A if (start & (TheISA::PageBytes - 1) || 14551706SN/A offset & (TheISA::PageBytes - 1) || 14561706SN/A (tgt_flags & OS::TGT_MAP_PRIVATE && 145713995Sbrandon.potter@amd.com tgt_flags & OS::TGT_MAP_SHARED) || 14581706SN/A (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 145913936SAndrea.Mondelli@ucf.edu !(tgt_flags & OS::TGT_MAP_SHARED)) || 14601706SN/A !length) { 146113995Sbrandon.potter@amd.com return -EINVAL; 14621706SN/A } 14636701Sgblack@eecs.umich.edu 14648852Sandreas.hansson@arm.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 14656701Sgblack@eecs.umich.edu // With shared mmaps, there are two cases to consider: 14666701Sgblack@eecs.umich.edu // 1) anonymous: writes should modify the mapping and this should be 14676701Sgblack@eecs.umich.edu // visible to observers who share the mapping. Currently, it's 14686701Sgblack@eecs.umich.edu // difficult to update the shared mapping because there's no 14691706SN/A // structure which maintains information about the which virtual 147013883Sdavid.hashe@amd.com // memory areas are shared. If that structure existed, it would be 147113883Sdavid.hashe@amd.com // possible to make the translations point to the same frames. 14723669Sbinkertn@umich.edu // 2) file-backed: writes should modify the mapping and the file 14731706SN/A // which is backed by the mapping. The shared mapping problem is the 14741706SN/A // same as what was mentioned about the anonymous mappings. For 14751706SN/A // file-backed mappings, the writes to the file are difficult 14761706SN/A // because it requires syncing what the mapping holds with the file 14772218SN/A // that resides on the host system. So, any write on a real system 14781706SN/A // would cause the change to be propagated to the file mapping at 147911759Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 148013933Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 148113933Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 148213933Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 148313933Sbrandon.potter@amd.com // a call to msync, but that again would require more information 148411799Sbrandon.potter@amd.com // than we currently maintain. 14851706SN/A warn("mmap: writing to shared mmap region is currently " 14861706SN/A "unsupported. The write succeeds on the target, but it " 148711886Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 148811886Sbrandon.potter@amd.com } 148913995Sbrandon.potter@amd.com 149011886Sbrandon.potter@amd.com length = roundUp(length, TheISA::PageBytes); 149111886Sbrandon.potter@amd.com 149212426Sqtt2@cornell.edu int sim_fd = -1; 149313995Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 149413557Sgabeblack@google.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 149513557Sgabeblack@google.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 149611886Sbrandon.potter@amd.com 149712426Sqtt2@cornell.edu auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 149813534Sandreas.sandberg@arm.com if (dfdp) { 149912426Sqtt2@cornell.edu EmulatedDriver *emul_driver = dfdp->getDriver(); 150013534Sandreas.sandberg@arm.com return emul_driver->mmap(p, tc, start, length, prot, 150112426Sqtt2@cornell.edu tgt_flags, tgt_fd, offset); 150212426Sqtt2@cornell.edu } 150312426Sqtt2@cornell.edu 150413536Sandreas.sandberg@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 150512426Sqtt2@cornell.edu if (!ffdp) 150612426Sqtt2@cornell.edu return -EBADF; 150711886Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 150813536Sandreas.sandberg@arm.com 150912426Sqtt2@cornell.edu pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 151011886Sbrandon.potter@amd.com sim_fd, offset); 151111886Sbrandon.potter@amd.com 151211886Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 151311886Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 151411886Sbrandon.potter@amd.com return -errno; 151511886Sbrandon.potter@amd.com } 151611886Sbrandon.potter@amd.com } 151711886Sbrandon.potter@amd.com 151811886Sbrandon.potter@amd.com // Extend global mmap region if necessary. Note that we ignore the 151911886Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 152013649Sqtt2@cornell.edu if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 152113649Sqtt2@cornell.edu std::shared_ptr<MemState> mem_state = p->memState; 152213649Sqtt2@cornell.edu Addr mmap_end = mem_state->getMmapEnd(); 152313649Sqtt2@cornell.edu 152413649Sqtt2@cornell.edu start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 152511886Sbrandon.potter@amd.com mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 152611886Sbrandon.potter@amd.com 152711886Sbrandon.potter@amd.com mem_state->setMmapEnd(mmap_end); 152811886Sbrandon.potter@amd.com } 152911886Sbrandon.potter@amd.com 153011886Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 153111886Sbrandon.potter@amd.com start, start + length - 1); 153211886Sbrandon.potter@amd.com 153311886Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 153411886Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 153511886Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 153613883Sdavid.hashe@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 153711886Sbrandon.potter@amd.com if (clobber) { 153811886Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 153911886Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 154011886Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 154111886Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 154211886Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 154311886Sbrandon.potter@amd.com } 154411886Sbrandon.potter@amd.com } 154511886Sbrandon.potter@amd.com 154611886Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 154711886Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 154811886Sbrandon.potter@amd.com // fatal and bail out of the simulation. 154911886Sbrandon.potter@amd.com p->allocateMem(start, length, clobber); 155011886Sbrandon.potter@amd.com 155111886Sbrandon.potter@amd.com // Transfer content into target address space. 155211886Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 155311886Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 155411886Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 155511886Sbrandon.potter@amd.com // with something like: 155613867Salexandru.dutu@amd.com // tp.memsetBlob(start, 0, length); 155713867Salexandru.dutu@amd.com // However, given that we don't support sparse mappings, and 155811886Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 155911886Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 156011886Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 156111886Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 156211886Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 156311886Sbrandon.potter@amd.com } else { 156411886Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 156511886Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 156611886Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 156711886Sbrandon.potter@amd.com // the host into the target address space. 156811886Sbrandon.potter@amd.com struct stat file_stat; 156911886Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 157011886Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 157111886Sbrandon.potter@amd.com 157211886Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 157313867Salexandru.dutu@amd.com // checking both the mmap size and the filesize that we are 157413867Salexandru.dutu@amd.com // trying to mmap into this space; the mmap size also depends 157513867Salexandru.dutu@amd.com // on the specified offset into the file. 157613867Salexandru.dutu@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 157711886Sbrandon.potter@amd.com length); 157811886Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 157911886Sbrandon.potter@amd.com 158011911SBrandon.Potter@amd.com // Cleanup the mmap region before exiting this function. 158111911SBrandon.Potter@amd.com munmap(pmap, length); 158211911SBrandon.Potter@amd.com 158311911SBrandon.Potter@amd.com // Maintain the symbol table for dynamic executables. 158411911SBrandon.Potter@amd.com // The loader will call mmap to map the images into its address 158511911SBrandon.Potter@amd.com // space and we intercept that here. We can verify that we are 158611911SBrandon.Potter@amd.com // executing inside the loader by checking the program counter value. 158711886Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 158811886Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 158911886Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 159011886Sbrandon.potter@amd.com if (interpreter) { 159111886Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 159211886Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 159311886Sbrandon.potter@amd.com 159411886Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 159511886Sbrandon.potter@amd.com 159611886Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 159711886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 159811886Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 159913536Sandreas.sandberg@arm.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 160011886Sbrandon.potter@amd.com 160111886Sbrandon.potter@amd.com if (lib) { 160211886Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 160311886Sbrandon.potter@amd.com lib->textBase(), start); 160411886Sbrandon.potter@amd.com } 160511886Sbrandon.potter@amd.com } 160611886Sbrandon.potter@amd.com } 160711886Sbrandon.potter@amd.com 160811886Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 160911886Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 161013867Salexandru.dutu@amd.com // execution (hopefully). 161113867Salexandru.dutu@amd.com } 161213867Salexandru.dutu@amd.com 161313867Salexandru.dutu@amd.com return start; 161413867Salexandru.dutu@amd.com} 161513867Salexandru.dutu@amd.com 161613867Salexandru.dutu@amd.comtemplate <class OS> 161713867Salexandru.dutu@amd.comSyscallReturn 161813867Salexandru.dutu@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 161913867Salexandru.dutu@amd.com{ 162013867Salexandru.dutu@amd.com int index = 0; 162111886Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 162211886Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 162311886Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 162411886Sbrandon.potter@amd.com int offset = p->getSyscallArg(tc, index); 16251706SN/A 16261706SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16271706SN/A if (!ffdp) 16281706SN/A return -EBADF; 162913995Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 16301706SN/A 16316701Sgblack@eecs.umich.edu BufferArg bufArg(bufPtr, nbytes); 163213995Sbrandon.potter@amd.com bufArg.copyIn(tc->getMemProxy()); 163311856Sbrandon.potter@amd.com 163411856Sbrandon.potter@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 16351706SN/A 163611856Sbrandon.potter@amd.com return (bytes_written == -1) ? -errno : bytes_written; 163711856Sbrandon.potter@amd.com} 16381706SN/A 163911856Sbrandon.potter@amd.com/// Target mmap() handler. 16401706SN/Atemplate <class OS> 16411706SN/ASyscallReturn 164210931Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16431706SN/A{ 16441706SN/A return mmapImpl<OS>(desc, num, p, tc, false); 16452218SN/A} 16461706SN/A 164711759Sbrandon.potter@amd.com/// Target mmap2() handler. 16481706SN/Atemplate <class OS> 16491706SN/ASyscallReturn 16501706SN/Ammap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16511706SN/A{ 165213572Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 165313572Sbrandon.potter@amd.com} 165413572Sbrandon.potter@amd.com 165513995Sbrandon.potter@amd.com/// Target getrlimit() handler. 165613572Sbrandon.potter@amd.comtemplate <class OS> 165713572Sbrandon.potter@amd.comSyscallReturn 165813995Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 165913572Sbrandon.potter@amd.com ThreadContext *tc) 166013572Sbrandon.potter@amd.com{ 166113572Sbrandon.potter@amd.com int index = 0; 166213572Sbrandon.potter@amd.com unsigned resource = process->getSyscallArg(tc, index); 166313572Sbrandon.potter@amd.com TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 166413572Sbrandon.potter@amd.com 166513572Sbrandon.potter@amd.com switch (resource) { 166614020Sgabeblack@google.com case OS::TGT_RLIMIT_STACK: 166713572Sbrandon.potter@amd.com // max stack size in bytes: make up a number (8MB for now) 166813572Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 166913572Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 167013572Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 167113572Sbrandon.potter@amd.com break; 167213572Sbrandon.potter@amd.com 167314010Sgabeblack@google.com case OS::TGT_RLIMIT_DATA: 167413572Sbrandon.potter@amd.com // max data segment size in bytes: make up a number 167513572Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 167613572Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 167713572Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 167813572Sbrandon.potter@amd.com break; 167913572Sbrandon.potter@amd.com 168013572Sbrandon.potter@amd.com default: 168113572Sbrandon.potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 168213572Sbrandon.potter@amd.com return -EINVAL; 168313572Sbrandon.potter@amd.com break; 168414010Sgabeblack@google.com } 168513572Sbrandon.potter@amd.com 168613572Sbrandon.potter@amd.com rlp.copyOut(tc->getMemProxy()); 168713572Sbrandon.potter@amd.com return 0; 168813572Sbrandon.potter@amd.com} 168913572Sbrandon.potter@amd.com 169013572Sbrandon.potter@amd.comtemplate <class OS> 16911706SN/ASyscallReturn 16921999SN/AprlimitFunc(SyscallDesc *desc, int callnum, Process *process, 16931999SN/A ThreadContext *tc) 16941999SN/A{ 169513995Sbrandon.potter@amd.com int index = 0; 16961999SN/A if (process->getSyscallArg(tc, index) != 0) 16976701Sgblack@eecs.umich.edu { 169813995Sbrandon.potter@amd.com warn("prlimit: ignoring rlimits for nonzero pid"); 169911856Sbrandon.potter@amd.com return -EPERM; 170010931Sbrandon.potter@amd.com } 170111856Sbrandon.potter@amd.com int resource = process->getSyscallArg(tc, index); 170211856Sbrandon.potter@amd.com Addr n = process->getSyscallArg(tc, index); 17031999SN/A if (n != 0) 170411856Sbrandon.potter@amd.com warn("prlimit: ignoring new rlimit"); 17051999SN/A Addr o = process->getSyscallArg(tc, index); 170614020Sgabeblack@google.com if (o != 0) 170711856Sbrandon.potter@amd.com { 170811856Sbrandon.potter@amd.com TypedBufferArg<typename OS::rlimit> rlp(o); 17091999SN/A switch (resource) { 17106227Snate@binkert.org case OS::TGT_RLIMIT_STACK: 17111999SN/A // max stack size in bytes: make up a number (8MB for now) 17122461SN/A rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 171311856Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 171414010Sgabeblack@google.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 17158737Skoansin.tan@gmail.com break; 17161999SN/A case OS::TGT_RLIMIT_DATA: 171714010Sgabeblack@google.com // max data segment size in bytes: make up a number 171811856Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 17191999SN/A rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 17201999SN/A rlp->rlim_max = TheISA::htog(rlp->rlim_max); 172110931Sbrandon.potter@amd.com default: 17221999SN/A warn("prlimit: unimplemented resource %d", resource); 17236227Snate@binkert.org return -EINVAL; 17241999SN/A break; 17251999SN/A } 172613572Sbrandon.potter@amd.com rlp.copyOut(tc->getMemProxy()); 17271999SN/A } 17281999SN/A return 0; 172911385Sbrandon.potter@amd.com} 1730360SN/A 17311450SN/A/// Target clock_gettime() function. 173213995Sbrandon.potter@amd.comtemplate <class OS> 1733360SN/ASyscallReturn 17346701Sgblack@eecs.umich.educlock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 173513995Sbrandon.potter@amd.com{ 17366701Sgblack@eecs.umich.edu int index = 1; 17376701Sgblack@eecs.umich.edu //int clk_id = p->getSyscallArg(tc, index); 173811383Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 173911383Sbrandon.potter@amd.com 17408324Ssteve.reinhardt@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 174110486Stjablin@gmail.com tp->tv_sec += seconds_since_epoch; 1742360SN/A tp->tv_sec = TheISA::htog(tp->tv_sec); 174311385Sbrandon.potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 174411385Sbrandon.potter@amd.com 17459008Sgblack@eecs.umich.edu tp.copyOut(tc->getMemProxy()); 174611383Sbrandon.potter@amd.com 174711383Sbrandon.potter@amd.com return 0; 174811383Sbrandon.potter@amd.com} 174911383Sbrandon.potter@amd.com 175011383Sbrandon.potter@amd.com/// Target clock_getres() function. 175111383Sbrandon.potter@amd.comtemplate <class OS> 175211383Sbrandon.potter@amd.comSyscallReturn 175311383Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 175411383Sbrandon.potter@amd.com{ 17558324Ssteve.reinhardt@amd.com int index = 1; 175611383Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 175711383Sbrandon.potter@amd.com 175811383Sbrandon.potter@amd.com // Set resolution at ns, which is what clock_gettime() returns 175911383Sbrandon.potter@amd.com tp->tv_sec = 0; 176011383Sbrandon.potter@amd.com tp->tv_nsec = 1; 176111383Sbrandon.potter@amd.com 176211383Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 176311383Sbrandon.potter@amd.com 176411383Sbrandon.potter@amd.com return 0; 176511383Sbrandon.potter@amd.com} 176611383Sbrandon.potter@amd.com 176711383Sbrandon.potter@amd.com/// Target gettimeofday() handler. 176811383Sbrandon.potter@amd.comtemplate <class OS> 176911383Sbrandon.potter@amd.comSyscallReturn 177011383Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 177111383Sbrandon.potter@amd.com ThreadContext *tc) 177211383Sbrandon.potter@amd.com{ 177311383Sbrandon.potter@amd.com int index = 0; 177411383Sbrandon.potter@amd.com TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 177511383Sbrandon.potter@amd.com 177611383Sbrandon.potter@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 177711383Sbrandon.potter@amd.com tp->tv_sec += seconds_since_epoch; 177811383Sbrandon.potter@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 177911383Sbrandon.potter@amd.com tp->tv_usec = TheISA::htog(tp->tv_usec); 17808324Ssteve.reinhardt@amd.com 17815877Shsul@eecs.umich.edu tp.copyOut(tc->getMemProxy()); 178210486Stjablin@gmail.com 178310486Stjablin@gmail.com return 0; 178411383Sbrandon.potter@amd.com} 178511383Sbrandon.potter@amd.com 178611383Sbrandon.potter@amd.com 178711856Sbrandon.potter@amd.com/// Target utimes() handler. 178811624Smichael.lebeane@amd.comtemplate <class OS> 178911856Sbrandon.potter@amd.comSyscallReturn 179011856Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process, 179111856Sbrandon.potter@amd.com ThreadContext *tc) 179213995Sbrandon.potter@amd.com{ 179313995Sbrandon.potter@amd.com std::string path; 179411624Smichael.lebeane@amd.com 179511624Smichael.lebeane@amd.com int index = 0; 179611856Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 179711856Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 179811383Sbrandon.potter@amd.com return -EFAULT; 179911856Sbrandon.potter@amd.com } 1800360SN/A 180111913SBrandon.Potter@amd.com TypedBufferArg<typename OS::timeval [2]> 180211383Sbrandon.potter@amd.com tp(process->getSyscallArg(tc, index)); 18038600Ssteve.reinhardt@amd.com tp.copyIn(tc->getMemProxy()); 180411383Sbrandon.potter@amd.com 180511383Sbrandon.potter@amd.com struct timeval hostTimeval[2]; 180611383Sbrandon.potter@amd.com for (int i = 0; i < 2; ++i) { 18078600Ssteve.reinhardt@amd.com hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 18082544SN/A hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 18092544SN/A } 181011383Sbrandon.potter@amd.com 181111383Sbrandon.potter@amd.com // Adjust path for current working directory 181211383Sbrandon.potter@amd.com path = process->fullPath(path); 181311905SBrandon.Potter@amd.com 181411905SBrandon.Potter@amd.com int result = utimes(path.c_str(), hostTimeval); 181511905SBrandon.Potter@amd.com 181611905SBrandon.Potter@amd.com if (result < 0) 181711905SBrandon.Potter@amd.com return -errno; 181811905SBrandon.Potter@amd.com 181911905SBrandon.Potter@amd.com return 0; 182011383Sbrandon.potter@amd.com} 182111383Sbrandon.potter@amd.com 182211383Sbrandon.potter@amd.comtemplate <class OS> 182311383Sbrandon.potter@amd.comSyscallReturn 182411383Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 182511383Sbrandon.potter@amd.com{ 182611383Sbrandon.potter@amd.com desc->setFlags(0); 182711383Sbrandon.potter@amd.com 182811383Sbrandon.potter@amd.com int index = 0; 182911383Sbrandon.potter@amd.com std::string path; 183011383Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 183111383Sbrandon.potter@amd.com if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 183211383Sbrandon.potter@amd.com return -EFAULT; 183311383Sbrandon.potter@amd.com 183411383Sbrandon.potter@amd.com if (access(path.c_str(), F_OK) == -1) 18358600Ssteve.reinhardt@amd.com return -EACCES; 18366672Sgblack@eecs.umich.edu 18378600Ssteve.reinhardt@amd.com auto read_in = [](std::vector<std::string> & vect, 183811383Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy, 183911383Sbrandon.potter@amd.com Addr mem_loc) 184011383Sbrandon.potter@amd.com { 18418601Ssteve.reinhardt@amd.com for (int inc = 0; ; inc++) { 18422544SN/A BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 184311383Sbrandon.potter@amd.com b.copyIn(mem_proxy); 184414020Sgabeblack@google.com 184511383Sbrandon.potter@amd.com if (!*(Addr*)b.bufferPtr()) 184611383Sbrandon.potter@amd.com break; 184711383Sbrandon.potter@amd.com 184811383Sbrandon.potter@amd.com vect.push_back(std::string()); 184911383Sbrandon.potter@amd.com mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 185011383Sbrandon.potter@amd.com } 185111383Sbrandon.potter@amd.com }; 185211383Sbrandon.potter@amd.com 185311383Sbrandon.potter@amd.com /** 185411383Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 185511383Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 185611383Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 185711383Sbrandon.potter@amd.com * constructor. 185811383Sbrandon.potter@amd.com */ 185911383Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 186011383Sbrandon.potter@amd.com pp->executable = path; 186111383Sbrandon.potter@amd.com Addr argv_mem_loc = p->getSyscallArg(tc, index); 186211383Sbrandon.potter@amd.com read_in(pp->cmd, mem_proxy, argv_mem_loc); 186311383Sbrandon.potter@amd.com Addr envp_mem_loc = p->getSyscallArg(tc, index); 186411383Sbrandon.potter@amd.com read_in(pp->env, mem_proxy, envp_mem_loc); 186511383Sbrandon.potter@amd.com pp->uid = p->uid(); 186611383Sbrandon.potter@amd.com pp->egid = p->egid(); 186711383Sbrandon.potter@amd.com pp->euid = p->euid(); 186811383Sbrandon.potter@amd.com pp->gid = p->gid(); 186911383Sbrandon.potter@amd.com pp->ppid = p->ppid(); 187011383Sbrandon.potter@amd.com pp->pid = p->pid(); 187111383Sbrandon.potter@amd.com pp->input.assign("cin"); 187211383Sbrandon.potter@amd.com pp->output.assign("cout"); 187311383Sbrandon.potter@amd.com pp->errout.assign("cerr"); 187411383Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 187511392Sbrandon.potter@amd.com pp->system = p->system; 187611392Sbrandon.potter@amd.com /** 187711392Sbrandon.potter@amd.com * Prevent process object creation with identical PIDs (which will trip 187811392Sbrandon.potter@amd.com * a fatal check in Process constructor). The execve call is supposed to 187911392Sbrandon.potter@amd.com * take over the currently executing process' identity but replace 188011392Sbrandon.potter@amd.com * whatever it is doing with a new process image. Instead of hijacking 188111392Sbrandon.potter@amd.com * the process object in the simulator, we create a new process object 188211392Sbrandon.potter@amd.com * and bind to the previous process' thread below (hijacking the thread). 188311392Sbrandon.potter@amd.com */ 188411392Sbrandon.potter@amd.com p->system->PIDs.erase(p->pid()); 188511392Sbrandon.potter@amd.com Process *new_p = pp->create(); 188611392Sbrandon.potter@amd.com delete pp; 188711392Sbrandon.potter@amd.com 188811392Sbrandon.potter@amd.com /** 188911856Sbrandon.potter@amd.com * Work through the file descriptor array and close any files marked 189011856Sbrandon.potter@amd.com * close-on-exec. 189111856Sbrandon.potter@amd.com */ 189211392Sbrandon.potter@amd.com new_p->fds = p->fds; 189311392Sbrandon.potter@amd.com for (int i = 0; i < new_p->fds->getSize(); i++) { 189411392Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 189511392Sbrandon.potter@amd.com if (fdep && fdep->getCOE()) 189611392Sbrandon.potter@amd.com new_p->fds->closeFDEntry(i); 189711392Sbrandon.potter@amd.com } 189811392Sbrandon.potter@amd.com 189911392Sbrandon.potter@amd.com *new_p->sigchld = true; 190011383Sbrandon.potter@amd.com 190111383Sbrandon.potter@amd.com delete p; 190211383Sbrandon.potter@amd.com tc->clearArchRegs(); 190311383Sbrandon.potter@amd.com tc->setProcessPtr(new_p); 190411383Sbrandon.potter@amd.com new_p->assignThreadContext(tc->contextId()); 19051458SN/A new_p->initState(); 1906360SN/A tc->activate(); 1907360SN/A TheISA::PCState pcState = tc->pcState(); 190811593Santhony.gutierrez@amd.com tc->setNPC(pcState.instAddr()); 190911593Santhony.gutierrez@amd.com 191013995Sbrandon.potter@amd.com desc->setFlags(SyscallDesc::SuppressReturnValue); 191111593Santhony.gutierrez@amd.com return 0; 191211593Santhony.gutierrez@amd.com} 191313995Sbrandon.potter@amd.com 191411593Santhony.gutierrez@amd.com/// Target getrusage() function. 191511593Santhony.gutierrez@amd.comtemplate <class OS> 191611593Santhony.gutierrez@amd.comSyscallReturn 191711593Santhony.gutierrez@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 191811593Santhony.gutierrez@amd.com ThreadContext *tc) 191911856Sbrandon.potter@amd.com{ 192011856Sbrandon.potter@amd.com int index = 0; 192111593Santhony.gutierrez@amd.com int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 192211856Sbrandon.potter@amd.com TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 192311593Santhony.gutierrez@amd.com 192411593Santhony.gutierrez@amd.com rup->ru_utime.tv_sec = 0; 192511593Santhony.gutierrez@amd.com rup->ru_utime.tv_usec = 0; 192611593Santhony.gutierrez@amd.com rup->ru_stime.tv_sec = 0; 192711594Santhony.gutierrez@amd.com rup->ru_stime.tv_usec = 0; 192811593Santhony.gutierrez@amd.com rup->ru_maxrss = 0; 192911593Santhony.gutierrez@amd.com rup->ru_ixrss = 0; 193011593Santhony.gutierrez@amd.com rup->ru_idrss = 0; 193111593Santhony.gutierrez@amd.com rup->ru_isrss = 0; 193211385Sbrandon.potter@amd.com rup->ru_minflt = 0; 193311385Sbrandon.potter@amd.com rup->ru_majflt = 0; 193411385Sbrandon.potter@amd.com rup->ru_nswap = 0; 193513995Sbrandon.potter@amd.com rup->ru_inblock = 0; 193611385Sbrandon.potter@amd.com rup->ru_oublock = 0; 193713995Sbrandon.potter@amd.com rup->ru_msgsnd = 0; 193811385Sbrandon.potter@amd.com rup->ru_msgrcv = 0; 193911385Sbrandon.potter@amd.com rup->ru_nsignals = 0; 194011385Sbrandon.potter@amd.com rup->ru_nvcsw = 0; 194111385Sbrandon.potter@amd.com rup->ru_nivcsw = 0; 194211385Sbrandon.potter@amd.com 194313995Sbrandon.potter@amd.com switch (who) { 194411385Sbrandon.potter@amd.com case OS::TGT_RUSAGE_SELF: 194513995Sbrandon.potter@amd.com getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 194611385Sbrandon.potter@amd.com rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 194711385Sbrandon.potter@amd.com rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 1948378SN/A break; 1949360SN/A 19501450SN/A case OS::TGT_RUSAGE_CHILDREN: 195113995Sbrandon.potter@amd.com // do nothing. We have no child processes, so they take no time. 1952360SN/A break; 19536701Sgblack@eecs.umich.edu 195413995Sbrandon.potter@amd.com default: 19556701Sgblack@eecs.umich.edu // don't really handle THREAD or CHILDREN, but just warn and 19566701Sgblack@eecs.umich.edu // plow ahead 1957360SN/A warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1958360SN/A who); 195911906SBrandon.Potter@amd.com } 196011906SBrandon.Potter@amd.com 196111906SBrandon.Potter@amd.com rup.copyOut(tc->getMemProxy()); 196211906SBrandon.Potter@amd.com 196311906SBrandon.Potter@amd.com return 0; 196411906SBrandon.Potter@amd.com} 1965360SN/A 196611906SBrandon.Potter@amd.com/// Target times() function. 196711906SBrandon.Potter@amd.comtemplate <class OS> 196811906SBrandon.Potter@amd.comSyscallReturn 196911906SBrandon.Potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process, 197011906SBrandon.Potter@amd.com ThreadContext *tc) 197111906SBrandon.Potter@amd.com{ 19725877Shsul@eecs.umich.edu int index = 0; 197311906SBrandon.Potter@amd.com TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 197411906SBrandon.Potter@amd.com 197511906SBrandon.Potter@amd.com // Fill in the time structure (in clocks) 197611906SBrandon.Potter@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 1977360SN/A bufp->tms_utime = clocks; 1978360SN/A bufp->tms_stime = 0; 19798706Sandreas.hansson@arm.com bufp->tms_cutime = 0; 19801458SN/A bufp->tms_cstime = 0; 1981360SN/A 1982360SN/A // Convert to host endianness 198312235Sar4jc@virginia.edu bufp->tms_utime = TheISA::htog(bufp->tms_utime); 198412235Sar4jc@virginia.edu 198513995Sbrandon.potter@amd.com // Write back 198612235Sar4jc@virginia.edu bufp.copyOut(tc->getMemProxy()); 198712235Sar4jc@virginia.edu 198813995Sbrandon.potter@amd.com // Return clock ticks since system boot 198912235Sar4jc@virginia.edu return clocks; 199012235Sar4jc@virginia.edu} 199112235Sar4jc@virginia.edu 199212235Sar4jc@virginia.edu/// Target time() function. 199312235Sar4jc@virginia.edutemplate <class OS> 199412235Sar4jc@virginia.eduSyscallReturn 199512235Sar4jc@virginia.edutimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 199612235Sar4jc@virginia.edu{ 199712235Sar4jc@virginia.edu typename OS::time_t sec, usec; 199812235Sar4jc@virginia.edu getElapsedTimeMicro(sec, usec); 199912235Sar4jc@virginia.edu sec += seconds_since_epoch; 200012235Sar4jc@virginia.edu 200112416Sqtt2@cornell.edu int index = 0; 200212235Sar4jc@virginia.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 200312235Sar4jc@virginia.edu if (taddr != 0) { 200412235Sar4jc@virginia.edu typename OS::time_t t = sec; 200512235Sar4jc@virginia.edu t = TheISA::htog(t); 200612235Sar4jc@virginia.edu SETranslatingPortProxy &p = tc->getMemProxy(); 200712235Sar4jc@virginia.edu p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 200812235Sar4jc@virginia.edu } 200912235Sar4jc@virginia.edu return sec; 201012235Sar4jc@virginia.edu} 201112235Sar4jc@virginia.edu 201212235Sar4jc@virginia.edutemplate <class OS> 201312235Sar4jc@virginia.eduSyscallReturn 201412593Sjason@lowepower.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 201512235Sar4jc@virginia.edu{ 201612235Sar4jc@virginia.edu int index = 0; 201712235Sar4jc@virginia.edu int tgid = process->getSyscallArg(tc, index); 201812235Sar4jc@virginia.edu int tid = process->getSyscallArg(tc, index); 201912235Sar4jc@virginia.edu int sig = process->getSyscallArg(tc, index); 202012235Sar4jc@virginia.edu 202112235Sar4jc@virginia.edu /** 202212235Sar4jc@virginia.edu * This system call is intended to allow killing a specific thread 202312235Sar4jc@virginia.edu * within an arbitrary thread group if sanctioned with permission checks. 202412235Sar4jc@virginia.edu * It's usually true that threads share the termination signal as pointed 202510796Sbrandon.potter@amd.com * out by the pthread_kill man page and this seems to be the intended 202610796Sbrandon.potter@amd.com * usage. Due to this being an emulated environment, assume the following: 202710796Sbrandon.potter@amd.com * Threads are allowed to call tgkill because the EUID for all threads 202813995Sbrandon.potter@amd.com * should be the same. There is no signal handling mechanism for kernel 202910796Sbrandon.potter@amd.com * registration of signal handlers since signals are poorly supported in 203010796Sbrandon.potter@amd.com * emulation mode. Since signal handlers cannot be registered, all 203113995Sbrandon.potter@amd.com * threads within in a thread group must share the termination signal. 203210796Sbrandon.potter@amd.com * We never exhaust PIDs so there's no chance of finding the wrong one 203310796Sbrandon.potter@amd.com * due to PID rollover. 203410796Sbrandon.potter@amd.com */ 203510796Sbrandon.potter@amd.com 203610796Sbrandon.potter@amd.com System *sys = tc->getSystemPtr(); 203710796Sbrandon.potter@amd.com Process *tgt_proc = nullptr; 203810796Sbrandon.potter@amd.com for (int i = 0; i < sys->numContexts(); i++) { 203910796Sbrandon.potter@amd.com Process *temp = sys->threadContexts[i]->getProcessPtr(); 204010796Sbrandon.potter@amd.com if (temp->pid() == tid) { 204110796Sbrandon.potter@amd.com tgt_proc = temp; 204210796Sbrandon.potter@amd.com break; 204310796Sbrandon.potter@amd.com } 204410796Sbrandon.potter@amd.com } 204511337SMichael.Lebeane@amd.com 204611337SMichael.Lebeane@amd.com if (sig != 0 || sig != OS::TGT_SIGABRT) 204711337SMichael.Lebeane@amd.com return -EINVAL; 204813995Sbrandon.potter@amd.com 204911337SMichael.Lebeane@amd.com if (tgt_proc == nullptr) 205011337SMichael.Lebeane@amd.com return -ESRCH; 205113995Sbrandon.potter@amd.com 205211337SMichael.Lebeane@amd.com if (tgid != -1 && tgt_proc->tgid() != tgid) 205311337SMichael.Lebeane@amd.com return -ESRCH; 205411337SMichael.Lebeane@amd.com 205511337SMichael.Lebeane@amd.com if (sig == OS::TGT_SIGABRT) 205611337SMichael.Lebeane@amd.com exitGroupFunc(desc, 252, process, tc); 205711337SMichael.Lebeane@amd.com 205811337SMichael.Lebeane@amd.com return 0; 205911337SMichael.Lebeane@amd.com} 206011337SMichael.Lebeane@amd.com 206111337SMichael.Lebeane@amd.com 206211337SMichael.Lebeane@amd.com#endif // __SIM_SYSCALL_EMUL_HH__ 2063378SN/A