syscall_emul.hh revision 12018
1360SN/A/* 210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 410027SChris.Adeniyi-Jones@arm.com * All rights reserved 510027SChris.Adeniyi-Jones@arm.com * 610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall 710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual 810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating 910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software 1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder. You may use the software subject to the license 1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated 1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software, 1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form. 1410027SChris.Adeniyi-Jones@arm.com * 151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 16360SN/A * All rights reserved. 17360SN/A * 18360SN/A * Redistribution and use in source and binary forms, with or without 19360SN/A * modification, are permitted provided that the following conditions are 20360SN/A * met: redistributions of source code must retain the above copyright 21360SN/A * notice, this list of conditions and the following disclaimer; 22360SN/A * redistributions in binary form must reproduce the above copyright 23360SN/A * notice, this list of conditions and the following disclaimer in the 24360SN/A * documentation and/or other materials provided with the distribution; 25360SN/A * neither the name of the copyright holders nor the names of its 26360SN/A * contributors may be used to endorse or promote products derived from 27360SN/A * this software without specific prior written permission. 28360SN/A * 29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 422665Ssaidi@eecs.umich.edu * Kevin Lim 43360SN/A */ 44360SN/A 451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 461354SN/A#define __SIM_SYSCALL_EMUL_HH__ 47360SN/A 482764Sstever@eecs.umich.edu#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 499202Spalle@lyckegaard.dk defined(__FreeBSD__) || defined(__CYGWIN__) || \ 509202Spalle@lyckegaard.dk defined(__NetBSD__)) 512064SN/A#define NO_STAT64 1 5211799Sbrandon.potter@amd.com#else 5311799Sbrandon.potter@amd.com#define NO_STAT64 0 5411799Sbrandon.potter@amd.com#endif 5511799Sbrandon.potter@amd.com 5611799Sbrandon.potter@amd.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 5711799Sbrandon.potter@amd.com defined(__FreeBSD__) || defined(__NetBSD__)) 58360SN/A#define NO_STATFS 1 59360SN/A#else 60360SN/A#define NO_STATFS 0 61360SN/A#endif 62360SN/A 63360SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 641809SN/A defined(__FreeBSD__) || defined(__NetBSD__)) 6511800Sbrandon.potter@amd.com#define NO_FALLOCATE 1 6611392Sbrandon.potter@amd.com#else 671809SN/A#define NO_FALLOCATE 0 6811392Sbrandon.potter@amd.com#endif 6911383Sbrandon.potter@amd.com 703113Sgblack@eecs.umich.edu/// 7111799Sbrandon.potter@amd.com/// @file syscall_emul.hh 7211759Sbrandon.potter@amd.com/// 7311812Sbaz21@cam.ac.uk/// This file defines objects used to emulate syscalls from the target 7411812Sbaz21@cam.ac.uk/// application on the host machine. 7511799Sbrandon.potter@amd.com 768229Snate@binkert.org#ifdef __CYGWIN32__ 778229Snate@binkert.org#include <sys/fcntl.h> 7811594Santhony.gutierrez@amd.com 797075Snate@binkert.org#endif 808229Snate@binkert.org#include <fcntl.h> 8111856Sbrandon.potter@amd.com#include <sys/mman.h> 827075Snate@binkert.org#include <sys/stat.h> 83360SN/A#if (NO_STATFS == 0) 8411886Sbrandon.potter@amd.com#include <sys/statfs.h> 8511800Sbrandon.potter@amd.com#else 8611392Sbrandon.potter@amd.com#include <sys/mount.h> 872462SN/A#endif 881354SN/A#include <sys/time.h> 896216Snate@binkert.org#include <sys/uio.h> 906658Snate@binkert.org#include <unistd.h> 912474SN/A 922680Sktlim@umich.edu#include <cerrno> 938229Snate@binkert.org#include <memory> 9411886Sbrandon.potter@amd.com#include <string> 9510496Ssteve.reinhardt@amd.com 968229Snate@binkert.org#include "arch/utility.hh" 9711794Sbrandon.potter@amd.com#include "base/intmath.hh" 9811886Sbrandon.potter@amd.com#include "base/loader/object_file.hh" 9910497Ssteve.reinhardt@amd.com#include "base/misc.hh" 10011794Sbrandon.potter@amd.com#include "base/trace.hh" 101360SN/A#include "base/types.hh" 102360SN/A#include "config/the_isa.hh" 103360SN/A#include "cpu/base.hh" 104360SN/A#include "cpu/thread_context.hh" 105360SN/A#include "mem/page_table.hh" 106360SN/A#include "params/Process.hh" 107360SN/A#include "sim/emul_driver.hh" 108360SN/A#include "sim/futex_map.hh" 109360SN/A#include "sim/process.hh" 110360SN/A#include "sim/syscall_debug_macros.hh" 111378SN/A#include "sim/syscall_desc.hh" 1121706SN/A#include "sim/syscall_emul_buf.hh" 11311851Sbrandon.potter@amd.com#include "sim/syscall_return.hh" 114378SN/A 115378SN/A////////////////////////////////////////////////////////////////////// 116378SN/A// 117378SN/A// The following emulation functions are generic enough that they 118378SN/A// don't need to be recompiled for different emulated OS's. They are 1191706SN/A// defined in sim/syscall_emul.cc. 12011851Sbrandon.potter@amd.com// 121360SN/A////////////////////////////////////////////////////////////////////// 12211760Sbrandon.potter@amd.com 12311760Sbrandon.potter@amd.com 12411851Sbrandon.potter@amd.com/// Handler for unimplemented syscalls that we haven't thought about. 12511760Sbrandon.potter@amd.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1266109Ssanchezd@stanford.edu Process *p, ThreadContext *tc); 1271706SN/A 12811851Sbrandon.potter@amd.com/// Handler for unimplemented syscalls that we never intend to 129378SN/A/// implement (signal handling, etc.) and should not affect the correct 1306109Ssanchezd@stanford.edu/// behavior of the program. Print a warning only if the appropriate 1316109Ssanchezd@stanford.edu/// trace flag is enabled. Return success to the target program. 13211851Sbrandon.potter@amd.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1336109Ssanchezd@stanford.edu Process *p, ThreadContext *tc); 13411886Sbrandon.potter@amd.com 13511886Sbrandon.potter@amd.com// Target fallocateFunc() handler. 13611886Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 13711886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 138378SN/A 1391706SN/A/// Target exit() handler: terminate current context. 14011851Sbrandon.potter@amd.comSyscallReturn exitFunc(SyscallDesc *desc, int num, 141378SN/A Process *p, ThreadContext *tc); 1425748SSteve.Reinhardt@amd.com 1435748SSteve.Reinhardt@amd.com/// Target exit_group() handler: terminate simulation. (exit all threads) 14411851Sbrandon.potter@amd.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 145378SN/A Process *p, ThreadContext *tc); 146378SN/A 1471706SN/A/// Target set_tid_address() handler. 14811851Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 149378SN/A Process *p, ThreadContext *tc); 15011886Sbrandon.potter@amd.com 1511706SN/A/// Target getpagesize() handler. 15211851Sbrandon.potter@amd.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 153378SN/A Process *p, ThreadContext *tc); 154378SN/A 1551706SN/A/// Target brk() handler: set brk address. 15611851Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 157378SN/A Process *p, ThreadContext *tc); 158378SN/A 1591706SN/A/// Target close() handler. 16011851Sbrandon.potter@amd.comSyscallReturn closeFunc(SyscallDesc *desc, int num, 161378SN/A Process *p, ThreadContext *tc); 1624118Sgblack@eecs.umich.edu 1634118Sgblack@eecs.umich.edu// Target read() handler. 16411851Sbrandon.potter@amd.comSyscallReturn readFunc(SyscallDesc *desc, int num, 1654118Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 166378SN/A 1671706SN/A/// Target write() handler. 16811851Sbrandon.potter@amd.comSyscallReturn writeFunc(SyscallDesc *desc, int num, 169378SN/A Process *p, ThreadContext *tc); 170378SN/A 1711706SN/A/// Target lseek() handler. 17211851Sbrandon.potter@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 173360SN/A Process *p, ThreadContext *tc); 1745513SMichael.Adler@intel.com 1755513SMichael.Adler@intel.com/// Target _llseek() handler. 17611851Sbrandon.potter@amd.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1775513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 17810203SAli.Saidi@ARM.com 17910203SAli.Saidi@ARM.com/// Target munmap() handler. 18011851Sbrandon.potter@amd.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 18110203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1825513SMichael.Adler@intel.com 18311851Sbrandon.potter@amd.com/// Target gethostname() handler. 1845513SMichael.Adler@intel.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 185511SN/A Process *p, ThreadContext *tc); 18610633Smichaelupton@gmail.com 18711851Sbrandon.potter@amd.com/// Target getcwd() handler. 18810633Smichaelupton@gmail.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1891706SN/A Process *p, ThreadContext *tc); 19011851Sbrandon.potter@amd.com 191511SN/A/// Target readlink() handler. 1925513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1935513SMichael.Adler@intel.com Process *p, ThreadContext *tc, 19411851Sbrandon.potter@amd.com int index = 0); 1955513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 196511SN/A Process *p, ThreadContext *tc); 1971706SN/A 19811851Sbrandon.potter@amd.com/// Target unlink() handler. 1991706SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2001706SN/A Process *p, ThreadContext *tc, 2011706SN/A int index); 2021706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 20311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2041706SN/A 2051706SN/A/// Target mkdir() handler. 2061706SN/ASyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2071706SN/A Process *p, ThreadContext *tc); 20811851Sbrandon.potter@amd.com 2091706SN/A/// Target rename() handler. 210511SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 2116703Svince@csl.cornell.edu Process *p, ThreadContext *tc); 2126703Svince@csl.cornell.edu 21311851Sbrandon.potter@amd.com 2146703Svince@csl.cornell.edu/// Target truncate() handler. 2156685Stjones1@inf.ed.ac.ukSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2166685Stjones1@inf.ed.ac.uk Process *p, ThreadContext *tc); 21711851Sbrandon.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, 2215513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 22211851Sbrandon.potter@amd.com 2235513SMichael.Adler@intel.com 22411885Sbrandon.potter@amd.com/// Target truncate64() handler. 22511885Sbrandon.potter@amd.comSyscallReturn truncate64Func(SyscallDesc *desc, int num, 22611885Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2275513SMichael.Adler@intel.com 2281999SN/A/// Target ftruncate64() handler. 2291999SN/ASyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 23011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2311999SN/A 23211885Sbrandon.potter@amd.com 23311885Sbrandon.potter@amd.com/// Target umask() handler. 23411885Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2351999SN/A Process *p, ThreadContext *tc); 2361999SN/A 2371999SN/A/// Target gettid() handler. 23811851Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 2391999SN/A Process *p, ThreadContext *tc); 2403079Sstever@eecs.umich.edu 2413079Sstever@eecs.umich.edu/// Target chown() handler. 24211851Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num, 2433079Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 24411908SBrandon.Potter@amd.com 24511908SBrandon.Potter@amd.com/// Target setpgid() handler. 24611908SBrandon.Potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 24711908SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 24811875Sbrandon.potter@amd.com 2492093SN/A/// Target fchown() handler. 25011851Sbrandon.potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2512093SN/A Process *p, ThreadContext *tc); 2522687Sksewell@umich.edu 2532687Sksewell@umich.edu/// Target dup() handler. 25411851Sbrandon.potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num, 2552687Sksewell@umich.edu Process *process, ThreadContext *tc); 2562238SN/A 2572238SN/A/// Target dup2() handler. 25811851Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 2592238SN/A Process *process, ThreadContext *tc); 26011908SBrandon.Potter@amd.com 26111908SBrandon.Potter@amd.com/// Target fcntl() handler. 26211908SBrandon.Potter@amd.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 26311908SBrandon.Potter@amd.com Process *process, ThreadContext *tc); 26411908SBrandon.Potter@amd.com 26511908SBrandon.Potter@amd.com/// Target fcntl64() handler. 26611908SBrandon.Potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 26711908SBrandon.Potter@amd.com Process *process, ThreadContext *tc); 2682238SN/A 2692238SN/A/// Target setuid() handler. 27011851Sbrandon.potter@amd.comSyscallReturn setuidFunc(SyscallDesc *desc, int num, 2712238SN/A Process *p, ThreadContext *tc); 2722238SN/A 2732238SN/A/// Target pipe() handler. 27411851Sbrandon.potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 2752238SN/A Process *p, ThreadContext *tc); 2762238SN/A 2772238SN/A/// Internal pipe() handler. 27811851Sbrandon.potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 2792238SN/A ThreadContext *tc, bool pseudoPipe); 2802238SN/A 2812238SN/A/// Target getpid() handler. 28211851Sbrandon.potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num, 2832238SN/A Process *p, ThreadContext *tc); 2842238SN/A 2852238SN/A/// Target getuid() handler. 28611851Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, 2872238SN/A Process *p, ThreadContext *tc); 2882238SN/A 2892238SN/A/// Target getgid() handler. 29011851Sbrandon.potter@amd.comSyscallReturn getgidFunc(SyscallDesc *desc, int num, 2912238SN/A Process *p, ThreadContext *tc); 2929455Smitch.hayenga+gem5@gmail.com 2939455Smitch.hayenga+gem5@gmail.com/// Target getppid() handler. 29411851Sbrandon.potter@amd.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, 29510203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 29611851Sbrandon.potter@amd.com 29711851Sbrandon.potter@amd.com/// Target geteuid() handler. 2989455Smitch.hayenga+gem5@gmail.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2999112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 30011906SBrandon.Potter@amd.com 30111906SBrandon.Potter@amd.com/// Target getegid() handler. 3029112Smarc.orr@gmail.comSyscallReturn getegidFunc(SyscallDesc *desc, int num, 3039112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 30411851Sbrandon.potter@amd.com 3059112Smarc.orr@gmail.com/// Target access() handler 3069112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 3079112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3089112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 3099112Smarc.orr@gmail.com Process *p, ThreadContext *tc, 3109112Smarc.orr@gmail.com int index); 3119112Smarc.orr@gmail.com 3129112Smarc.orr@gmail.com/// Futex system call 3139112Smarc.orr@gmail.com/// Implemented by Daniel Sanchez 3149112Smarc.orr@gmail.com/// Used by printf's in multi-threaded apps 3159112Smarc.orr@gmail.comtemplate <class OS> 3169112Smarc.orr@gmail.comSyscallReturn 3179112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3189112Smarc.orr@gmail.com ThreadContext *tc) 3199112Smarc.orr@gmail.com{ 32011886Sbrandon.potter@amd.com using namespace std; 3219112Smarc.orr@gmail.com 3229112Smarc.orr@gmail.com int index = 0; 3239238Slluc.alvarez@bsc.es Addr uaddr = process->getSyscallArg(tc, index); 3249112Smarc.orr@gmail.com int op = process->getSyscallArg(tc, index); 3259112Smarc.orr@gmail.com int val = process->getSyscallArg(tc, index); 3269112Smarc.orr@gmail.com 32711886Sbrandon.potter@amd.com /* 3289112Smarc.orr@gmail.com * Unsupported option that does not affect the correctness of the 3299112Smarc.orr@gmail.com * application. This is a performance optimization utilized by Linux. 3309112Smarc.orr@gmail.com */ 3319112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3329112Smarc.orr@gmail.com 3339112Smarc.orr@gmail.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 33411367Sandreas.hansson@arm.com 3359112Smarc.orr@gmail.com if (OS::TGT_FUTEX_WAIT == op) { 33611321Ssteve.reinhardt@amd.com // Ensure futex system call accessed atomically. 33711886Sbrandon.potter@amd.com BufferArg buf(uaddr, sizeof(int)); 3389112Smarc.orr@gmail.com buf.copyIn(tc->getMemProxy()); 3399112Smarc.orr@gmail.com int mem_val = *(int*)buf.bufferPtr(); 3409112Smarc.orr@gmail.com 3419112Smarc.orr@gmail.com /* 3429112Smarc.orr@gmail.com * The value in memory at uaddr is not equal with the expected val 3439112Smarc.orr@gmail.com * (a different thread must have changed it before the system call was 3449112Smarc.orr@gmail.com * invoked). In this case, we need to throw an error. 3459112Smarc.orr@gmail.com */ 3469112Smarc.orr@gmail.com if (val != mem_val) 3479112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 3489112Smarc.orr@gmail.com 3499112Smarc.orr@gmail.com futex_map.suspend(uaddr, process->tgid(), tc); 3509112Smarc.orr@gmail.com 3519112Smarc.orr@gmail.com return 0; 35211886Sbrandon.potter@amd.com } else if (OS::TGT_FUTEX_WAKE == op) { 35311886Sbrandon.potter@amd.com return futex_map.wakeup(uaddr, process->tgid(), val); 3549112Smarc.orr@gmail.com } 3559112Smarc.orr@gmail.com 3569112Smarc.orr@gmail.com warn("futex: op %d not implemented; ignoring.", op); 3579112Smarc.orr@gmail.com return -ENOSYS; 3589112Smarc.orr@gmail.com} 3599112Smarc.orr@gmail.com 3609112Smarc.orr@gmail.com 3619112Smarc.orr@gmail.com/// Pseudo Funcs - These functions use a different return convension, 3629112Smarc.orr@gmail.com/// returning a second value in a register other than the normal return register 3639112Smarc.orr@gmail.comSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3649112Smarc.orr@gmail.com Process *process, ThreadContext *tc); 3659112Smarc.orr@gmail.com 36611321Ssteve.reinhardt@amd.com/// Target getpidPseudo() handler. 3679112Smarc.orr@gmail.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3689112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3699112Smarc.orr@gmail.com 3709112Smarc.orr@gmail.com/// Target getuidPseudo() handler. 37111886Sbrandon.potter@amd.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 37211886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 37311886Sbrandon.potter@amd.com 3749112Smarc.orr@gmail.com/// Target getgidPseudo() handler. 3759112Smarc.orr@gmail.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 37611886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3779112Smarc.orr@gmail.com 3789112Smarc.orr@gmail.com 3799112Smarc.orr@gmail.com/// A readable name for 1,000,000, for converting microseconds to seconds. 3809112Smarc.orr@gmail.comconst int one_million = 1000000; 3819112Smarc.orr@gmail.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 3822238SN/Aconst int one_billion = 1000000000; 3832238SN/A 3842238SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3852238SN/A/// by my reckoning. We want to keep this a constant (not use the 38611851Sbrandon.potter@amd.com/// real-world time) to keep simulations repeatable. 3872238SN/Aconst unsigned seconds_since_epoch = 1000000000; 3882238SN/A 3892238SN/A/// Helper function to convert current elapsed time to seconds and 39011851Sbrandon.potter@amd.com/// microseconds. 3912238SN/Atemplate <class T1, class T2> 3922238SN/Avoid 3932238SN/AgetElapsedTimeMicro(T1 &sec, T2 &usec) 39411851Sbrandon.potter@amd.com{ 3952238SN/A uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 3962238SN/A sec = elapsed_usecs / one_million; 3972238SN/A usec = elapsed_usecs % one_million; 39811851Sbrandon.potter@amd.com} 3992238SN/A 4002238SN/A/// Helper function to convert current elapsed time to seconds and 4011354SN/A/// nanoseconds. 4021354SN/Atemplate <class T1, class T2> 40310796Sbrandon.potter@amd.comvoid 40410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec) 4051354SN/A{ 4061354SN/A uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 4071354SN/A sec = elapsed_nsecs / one_billion; 4081354SN/A nsec = elapsed_nsecs % one_billion; 4091354SN/A} 4101354SN/A 4111354SN/A////////////////////////////////////////////////////////////////////// 4121354SN/A// 4131354SN/A// The following emulation functions are generic, but need to be 4141354SN/A// templated to account for differences in types, constants, etc. 41510796Sbrandon.potter@amd.com// 4161354SN/A////////////////////////////////////////////////////////////////////// 41710796Sbrandon.potter@amd.com 4181354SN/A typedef struct statfs hst_statfs; 4191354SN/A#if NO_STAT64 4201354SN/A typedef struct stat hst_stat; 4211354SN/A typedef struct stat hst_stat64; 42210796Sbrandon.potter@amd.com#else 42310796Sbrandon.potter@amd.com typedef struct stat hst_stat; 42410796Sbrandon.potter@amd.com typedef struct stat64 hst_stat64; 42510796Sbrandon.potter@amd.com#endif 42610796Sbrandon.potter@amd.com 42710796Sbrandon.potter@amd.com//// Helper function to convert a host stat buffer to a target stat 42810796Sbrandon.potter@amd.com//// buffer. Also copies the target buffer out to the simulated 42910796Sbrandon.potter@amd.com//// memory space. Used by stat(), fstat(), and lstat(). 43010796Sbrandon.potter@amd.com 43110796Sbrandon.potter@amd.comtemplate <typename target_stat, typename host_stat> 43210796Sbrandon.potter@amd.comstatic void 433360SN/AconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 434360SN/A{ 435360SN/A using namespace TheISA; 436360SN/A 437360SN/A if (fakeTTY) 438360SN/A tgt->st_dev = 0xA; 439360SN/A else 44011759Sbrandon.potter@amd.com tgt->st_dev = host->st_dev; 4413113Sgblack@eecs.umich.edu tgt->st_dev = TheISA::htog(tgt->st_dev); 4423113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 4433113Sgblack@eecs.umich.edu tgt->st_ino = TheISA::htog(tgt->st_ino); 4443113Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 4453113Sgblack@eecs.umich.edu if (fakeTTY) { 4463113Sgblack@eecs.umich.edu // Claim to be a character device 4473113Sgblack@eecs.umich.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4483113Sgblack@eecs.umich.edu tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4493113Sgblack@eecs.umich.edu } 4503113Sgblack@eecs.umich.edu tgt->st_mode = TheISA::htog(tgt->st_mode); 4513113Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4523113Sgblack@eecs.umich.edu tgt->st_nlink = TheISA::htog(tgt->st_nlink); 4533113Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4543113Sgblack@eecs.umich.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 4553113Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 4563113Sgblack@eecs.umich.edu tgt->st_gid = TheISA::htog(tgt->st_gid); 4574189Sgblack@eecs.umich.edu if (fakeTTY) 4584189Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4593113Sgblack@eecs.umich.edu else 4603113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4613113Sgblack@eecs.umich.edu tgt->st_rdev = TheISA::htog(tgt->st_rdev); 4623113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4638737Skoansin.tan@gmail.com tgt->st_size = TheISA::htog(tgt->st_size); 4643113Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4658737Skoansin.tan@gmail.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4663277Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4675515SMichael.Adler@intel.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4685515SMichael.Adler@intel.com tgt->st_ctimeX = host->st_ctime; 4695515SMichael.Adler@intel.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4705515SMichael.Adler@intel.com // Force the block size to be 8KB. This helps to ensure buffered io works 4715515SMichael.Adler@intel.com // consistently across different hosts. 4728737Skoansin.tan@gmail.com tgt->st_blksize = 0x2000; 4733277Sgblack@eecs.umich.edu tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4748737Skoansin.tan@gmail.com tgt->st_blocks = host->st_blocks; 4753277Sgblack@eecs.umich.edu tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4768737Skoansin.tan@gmail.com} 4773277Sgblack@eecs.umich.edu 4788737Skoansin.tan@gmail.com// Same for stat64 4793113Sgblack@eecs.umich.edu 4803113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4813113Sgblack@eecs.umich.edustatic void 4823113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4838737Skoansin.tan@gmail.com{ 4843113Sgblack@eecs.umich.edu using namespace TheISA; 4858737Skoansin.tan@gmail.com 4863114Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4878737Skoansin.tan@gmail.com#if defined(STAT_HAVE_NSEC) 4883114Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4898737Skoansin.tan@gmail.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 4903114Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4918737Skoansin.tan@gmail.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 49211906SBrandon.Potter@amd.com tgt->st_ctime_nsec = host->st_ctime_nsec; 4934061Sgblack@eecs.umich.edu tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 4944061Sgblack@eecs.umich.edu#else 4958737Skoansin.tan@gmail.com tgt->st_atime_nsec = 0; 4963113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4978737Skoansin.tan@gmail.com tgt->st_ctime_nsec = 0; 4983113Sgblack@eecs.umich.edu#endif 4993113Sgblack@eecs.umich.edu} 5003113Sgblack@eecs.umich.edu 5013113Sgblack@eecs.umich.edu// Here are a couple of convenience functions 5023113Sgblack@eecs.umich.edutemplate<class OS> 5033113Sgblack@eecs.umich.edustatic void 5043113Sgblack@eecs.umich.educopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 5053113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 5064189Sgblack@eecs.umich.edu{ 5074189Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5083113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5093113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5103113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5118737Skoansin.tan@gmail.com} 5123113Sgblack@eecs.umich.edu 5138737Skoansin.tan@gmail.comtemplate<class OS> 5143113Sgblack@eecs.umich.edustatic void 5158737Skoansin.tan@gmail.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5163113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5173113Sgblack@eecs.umich.edu{ 5183113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5193113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5203113Sgblack@eecs.umich.edu convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5213113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5223113Sgblack@eecs.umich.edu} 52311906SBrandon.Potter@amd.com 5243113Sgblack@eecs.umich.edutemplate <class OS> 5253113Sgblack@eecs.umich.edustatic void 5268852Sandreas.hansson@arm.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 52711906SBrandon.Potter@amd.com 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); 5343113Sgblack@eecs.umich.edu#else 5353113Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_bsize); 5363113Sgblack@eecs.umich.edu#endif 5378852Sandreas.hansson@arm.com tgt->f_blocks = TheISA::htog(host->f_blocks); 53811906SBrandon.Potter@amd.com 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); 5426686Stjones1@inf.ed.ac.uk memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 5433113Sgblack@eecs.umich.edu#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5443113Sgblack@eecs.umich.edu tgt->f_namelen = TheISA::htog(host->f_namemax); 5453113Sgblack@eecs.umich.edu tgt->f_frsize = TheISA::htog(host->f_bsize); 54611759Sbrandon.potter@amd.com#elif defined(__APPLE__) 54711759Sbrandon.potter@amd.com tgt->f_namelen = 0; 54811759Sbrandon.potter@amd.com tgt->f_frsize = 0; 54911759Sbrandon.potter@amd.com#else 55011759Sbrandon.potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 55111759Sbrandon.potter@amd.com tgt->f_frsize = TheISA::htog(host->f_frsize); 55211759Sbrandon.potter@amd.com#endif 55311812Sbaz21@cam.ac.uk#if defined(__linux__) 55411812Sbaz21@cam.ac.uk memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 55511812Sbaz21@cam.ac.uk#else 55611759Sbrandon.potter@amd.com /* 55711812Sbaz21@cam.ac.uk * The fields are different sizes per OS. Don't bother with 55811759Sbrandon.potter@amd.com * f_spare or f_reserved on non-Linux for now. 55911759Sbrandon.potter@amd.com */ 56011759Sbrandon.potter@amd.com memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 56111759Sbrandon.potter@amd.com#endif 56211759Sbrandon.potter@amd.com 56311759Sbrandon.potter@amd.com tgt.copyOut(mem); 56411759Sbrandon.potter@amd.com} 56511812Sbaz21@cam.ac.uk 56611812Sbaz21@cam.ac.uk/// Target ioctl() handler. For the most part, programs call ioctl() 56711812Sbaz21@cam.ac.uk/// only to find out if their stdout is a tty, to determine whether to 56811812Sbaz21@cam.ac.uk/// do line or block buffering. We always claim that output fds are 56911812Sbaz21@cam.ac.uk/// not TTYs to provide repeatable results. 57011812Sbaz21@cam.ac.uktemplate <class OS> 57111812Sbaz21@cam.ac.ukSyscallReturn 57211759Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 57311759Sbrandon.potter@amd.com{ 57411812Sbaz21@cam.ac.uk int index = 0; 57511812Sbaz21@cam.ac.uk int tgt_fd = p->getSyscallArg(tc, index); 57611759Sbrandon.potter@amd.com unsigned req = p->getSyscallArg(tc, index); 57711812Sbaz21@cam.ac.uk 57811812Sbaz21@cam.ac.uk DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 57911812Sbaz21@cam.ac.uk 58011812Sbaz21@cam.ac.uk if (OS::isTtyReq(req)) 58111812Sbaz21@cam.ac.uk return -ENOTTY; 58211812Sbaz21@cam.ac.uk 58311812Sbaz21@cam.ac.uk auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 58411759Sbrandon.potter@amd.com if (!dfdp) 58511759Sbrandon.potter@amd.com return -EBADF; 58611759Sbrandon.potter@amd.com 58711759Sbrandon.potter@amd.com /** 588378SN/A * If the driver is valid, issue the ioctl through it. Otherwise, 589378SN/A * there's an implicit assumption that the device is a TTY type and we 5909141Smarc.orr@gmail.com * return that we do not have a valid TTY. 5919141Smarc.orr@gmail.com */ 592360SN/A EmulatedDriver *emul_driver = dfdp->getDriver(); 5931450SN/A if (emul_driver) 59411856Sbrandon.potter@amd.com return emul_driver->ioctl(p, tc, req); 595360SN/A 5966701Sgblack@eecs.umich.edu /** 59711856Sbrandon.potter@amd.com * For lack of a better return code, return ENOTTY. Ideally, we should 59811856Sbrandon.potter@amd.com * return something better here, but at least we issue the warning. 599360SN/A */ 60010930Sbrandon.potter@amd.com warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 601360SN/A tgt_fd, req, tc->pcState()); 60211856Sbrandon.potter@amd.com return -ENOTTY; 60311856Sbrandon.potter@amd.com} 60410496Ssteve.reinhardt@amd.com 60511856Sbrandon.potter@amd.comtemplate <class OS> 60611856Sbrandon.potter@amd.comSyscallReturn 6071458SN/AopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 608360SN/A bool isopenat) 60911856Sbrandon.potter@amd.com{ 61011856Sbrandon.potter@amd.com int index = 0; 61111856Sbrandon.potter@amd.com int tgt_dirfd = -1; 61211856Sbrandon.potter@amd.com 61311856Sbrandon.potter@amd.com /** 61411856Sbrandon.potter@amd.com * If using the openat variant, read in the target directory file 61511856Sbrandon.potter@amd.com * descriptor from the simulated process. 61611856Sbrandon.potter@amd.com */ 61710496Ssteve.reinhardt@amd.com if (isopenat) 61811856Sbrandon.potter@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 61911856Sbrandon.potter@amd.com 62011856Sbrandon.potter@amd.com /** 62111856Sbrandon.potter@amd.com * Retrieve the simulated process' memory proxy and then read in the path 62211856Sbrandon.potter@amd.com * string from that memory space into the host's working memory space. 62310930Sbrandon.potter@amd.com */ 6249141Smarc.orr@gmail.com std::string path; 625360SN/A if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 626360SN/A return -EFAULT; 627360SN/A 62811907SBrandon.Potter@amd.com#ifdef __CYGWIN32__ 62911907SBrandon.Potter@amd.com int host_flags = O_BINARY; 63011907SBrandon.Potter@amd.com#else 631360SN/A int host_flags = 0; 63211907SBrandon.Potter@amd.com#endif 63311907SBrandon.Potter@amd.com /** 63411907SBrandon.Potter@amd.com * Translate target flags into host flags. Flags exist which are not 63511907SBrandon.Potter@amd.com * ported between architectures which can cause check failures. 63611907SBrandon.Potter@amd.com */ 63711907SBrandon.Potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 63811907SBrandon.Potter@amd.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 63911907SBrandon.Potter@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 64011907SBrandon.Potter@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 64111907SBrandon.Potter@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 64211907SBrandon.Potter@amd.com } 64311907SBrandon.Potter@amd.com } 64411907SBrandon.Potter@amd.com if (tgt_flags) { 64511907SBrandon.Potter@amd.com warn("open%s: cannot decode flags 0x%x", 646360SN/A isopenat ? "at" : "", tgt_flags); 64711907SBrandon.Potter@amd.com } 6481458SN/A#ifdef __CYGWIN32__ 649360SN/A host_flags |= O_BINARY; 65011907SBrandon.Potter@amd.com#endif 65111907SBrandon.Potter@amd.com 65211907SBrandon.Potter@amd.com int mode = p->getSyscallArg(tc, index); 65311907SBrandon.Potter@amd.com 65411907SBrandon.Potter@amd.com /** 65511907SBrandon.Potter@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 65611907SBrandon.Potter@amd.com * take the current working directory value which was passed into the 65711907SBrandon.Potter@amd.com * process class as a Python parameter and append the current path to 65811907SBrandon.Potter@amd.com * create a full path. 65911907SBrandon.Potter@amd.com * Otherwise, openat with a valid target directory file descriptor has 660360SN/A * been called. If the path option, which was passed in as a parameter, 66111907SBrandon.Potter@amd.com * is not absolute, retrieve the directory file descriptor's path and 66211907SBrandon.Potter@amd.com * prepend it to the path passed in as a parameter. 66311907SBrandon.Potter@amd.com * In every case, we should have a full path (which is relevant to the 664360SN/A * host) to work with after this block has been passed. 665360SN/A */ 66611907SBrandon.Potter@amd.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 66711907SBrandon.Potter@amd.com path = p->fullPath(path); 66811907SBrandon.Potter@amd.com } else if (!startswith(path, "/")) { 66911907SBrandon.Potter@amd.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 670360SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 67111907SBrandon.Potter@amd.com if (!ffdp) 672360SN/A return -EBADF; 673360SN/A path.insert(0, ffdp->getFileName()); 67411907SBrandon.Potter@amd.com } 6753669Sbinkertn@umich.edu 67611907SBrandon.Potter@amd.com /** 67711907SBrandon.Potter@amd.com * Since this is an emulated environment, we create pseudo file 67811907SBrandon.Potter@amd.com * descriptors for device requests that have been registered with 67911907SBrandon.Potter@amd.com * the process class through Python; this allows us to create a file 68011907SBrandon.Potter@amd.com * descriptor for subsequent ioctl or mmap calls. 68111907SBrandon.Potter@amd.com */ 68211907SBrandon.Potter@amd.com if (startswith(path, "/dev/")) { 68311907SBrandon.Potter@amd.com std::string filename = path.substr(strlen("/dev/")); 68411907SBrandon.Potter@amd.com EmulatedDriver *drv = p->findDriver(filename); 68511907SBrandon.Potter@amd.com if (drv) { 68611907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 68711907SBrandon.Potter@amd.com "driver open with path[%s]\n", 68811907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 68911907SBrandon.Potter@amd.com return drv->open(p, tc, mode, host_flags); 69011907SBrandon.Potter@amd.com } 69111907SBrandon.Potter@amd.com /** 69211907SBrandon.Potter@amd.com * Fall through here for pass through to host devices, such 69311907SBrandon.Potter@amd.com * as /dev/zero 69411907SBrandon.Potter@amd.com */ 69511907SBrandon.Potter@amd.com } 69611907SBrandon.Potter@amd.com 6971706SN/A /** 69811907SBrandon.Potter@amd.com * Some special paths and files cannot be called on the host and need 69911907SBrandon.Potter@amd.com * to be handled as special cases inside the simulator. 70011907SBrandon.Potter@amd.com * If the full path that was created above does not match any of the 70111907SBrandon.Potter@amd.com * special cases, pass it through to the open call on the host to let 70211907SBrandon.Potter@amd.com * the host open the file on our behalf. 70311907SBrandon.Potter@amd.com * If the host cannot open the file, return the host's error code back 70410496Ssteve.reinhardt@amd.com * through the system call to the simulated process. 70510496Ssteve.reinhardt@amd.com */ 70611907SBrandon.Potter@amd.com int sim_fd = -1; 70711907SBrandon.Potter@amd.com std::vector<std::string> special_paths = 70811907SBrandon.Potter@amd.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 70911907SBrandon.Potter@amd.com for (auto entry : special_paths) { 71011907SBrandon.Potter@amd.com if (startswith(path, entry)) 71111907SBrandon.Potter@amd.com sim_fd = OS::openSpecialFile(path, p, tc); 71210496Ssteve.reinhardt@amd.com } 71311907SBrandon.Potter@amd.com if (sim_fd == -1) { 71411907SBrandon.Potter@amd.com sim_fd = open(path.c_str(), host_flags, mode); 71511907SBrandon.Potter@amd.com } 71611907SBrandon.Potter@amd.com if (sim_fd == -1) { 71710496Ssteve.reinhardt@amd.com int local = -errno; 71810496Ssteve.reinhardt@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 71911907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 72011907SBrandon.Potter@amd.com return local; 72111907SBrandon.Potter@amd.com } 72211907SBrandon.Potter@amd.com 72311907SBrandon.Potter@amd.com /** 72411907SBrandon.Potter@amd.com * The file was opened successfully and needs to be recorded in the 72511907SBrandon.Potter@amd.com * process' file descriptor array so that it can be retrieved later. 72611907SBrandon.Potter@amd.com * The target file descriptor that is chosen will be the lowest unused 72711907SBrandon.Potter@amd.com * file descriptor. 72811907SBrandon.Potter@amd.com * Return the indirect target file descriptor back to the simulated 72911907SBrandon.Potter@amd.com * process to act as a handle for the opened file. 73011907SBrandon.Potter@amd.com */ 73111907SBrandon.Potter@amd.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 73211907SBrandon.Potter@amd.com int tgt_fd = p->fds->allocFD(ffdp); 73311907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 73411907SBrandon.Potter@amd.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 73511907SBrandon.Potter@amd.com return tgt_fd; 73611907SBrandon.Potter@amd.com} 73711907SBrandon.Potter@amd.com 73811907SBrandon.Potter@amd.com/// Target open() handler. 73911907SBrandon.Potter@amd.comtemplate <class OS> 74011907SBrandon.Potter@amd.comSyscallReturn 74111907SBrandon.Potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 74211907SBrandon.Potter@amd.com ThreadContext *tc) 74311907SBrandon.Potter@amd.com{ 744360SN/A return openImpl<OS>(desc, callnum, process, tc, false); 74511907SBrandon.Potter@amd.com} 74611907SBrandon.Potter@amd.com 74711907SBrandon.Potter@amd.com/// Target openat() handler. 74811907SBrandon.Potter@amd.comtemplate <class OS> 74911907SBrandon.Potter@amd.comSyscallReturn 75011907SBrandon.Potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process, 75111907SBrandon.Potter@amd.com ThreadContext *tc) 75211907SBrandon.Potter@amd.com{ 75311907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, true); 75411907SBrandon.Potter@amd.com} 75511907SBrandon.Potter@amd.com 75611907SBrandon.Potter@amd.com/// Target unlinkat() handler. 75711907SBrandon.Potter@amd.comtemplate <class OS> 758360SN/ASyscallReturn 759360SN/AunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 76010027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 76110027SChris.Adeniyi-Jones@arm.com{ 76210027SChris.Adeniyi-Jones@arm.com int index = 0; 76311851Sbrandon.potter@amd.com int dirfd = process->getSyscallArg(tc, index); 76410027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 76510027SChris.Adeniyi-Jones@arm.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 76611907SBrandon.Potter@amd.com 76710027SChris.Adeniyi-Jones@arm.com return unlinkHelper(desc, callnum, process, tc, 1); 76810027SChris.Adeniyi-Jones@arm.com} 76910027SChris.Adeniyi-Jones@arm.com 77010027SChris.Adeniyi-Jones@arm.com/// Target facessat() handler 77110027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 77211851Sbrandon.potter@amd.comSyscallReturn 77311851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 77410027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 77511907SBrandon.Potter@amd.com{ 77610027SChris.Adeniyi-Jones@arm.com int index = 0; 77710027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 77810633Smichaelupton@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 77910633Smichaelupton@gmail.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 78010633Smichaelupton@gmail.com return accessFunc(desc, callnum, process, tc, 1); 78111851Sbrandon.potter@amd.com} 78210633Smichaelupton@gmail.com 78310633Smichaelupton@gmail.com/// Target readlinkat() handler 78410633Smichaelupton@gmail.comtemplate <class OS> 78510633Smichaelupton@gmail.comSyscallReturn 78610633Smichaelupton@gmail.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 78710633Smichaelupton@gmail.com ThreadContext *tc) 78810633Smichaelupton@gmail.com{ 78910633Smichaelupton@gmail.com int index = 0; 79010633Smichaelupton@gmail.com int dirfd = process->getSyscallArg(tc, index); 79110633Smichaelupton@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 79210203SAli.Saidi@ARM.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 79310203SAli.Saidi@ARM.com return readlinkFunc(desc, callnum, process, tc, 1); 79410203SAli.Saidi@ARM.com} 79511851Sbrandon.potter@amd.com 79611851Sbrandon.potter@amd.com/// Target renameat() handler. 79710203SAli.Saidi@ARM.comtemplate <class OS> 79810203SAli.Saidi@ARM.comSyscallReturn 79910203SAli.Saidi@ARM.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process, 80010203SAli.Saidi@ARM.com ThreadContext *tc) 80110203SAli.Saidi@ARM.com{ 80210203SAli.Saidi@ARM.com int index = 0; 80310203SAli.Saidi@ARM.com 80410203SAli.Saidi@ARM.com int olddirfd = process->getSyscallArg(tc, index); 80510203SAli.Saidi@ARM.com if (olddirfd != OS::TGT_AT_FDCWD) 80610203SAli.Saidi@ARM.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 80710203SAli.Saidi@ARM.com 80811851Sbrandon.potter@amd.com std::string old_name; 80911851Sbrandon.potter@amd.com 81010203SAli.Saidi@ARM.com if (!tc->getMemProxy().tryReadString(old_name, 81110203SAli.Saidi@ARM.com process->getSyscallArg(tc, index))) 81210203SAli.Saidi@ARM.com return -EFAULT; 81310203SAli.Saidi@ARM.com 81410203SAli.Saidi@ARM.com int newdirfd = process->getSyscallArg(tc, index); 81510203SAli.Saidi@ARM.com if (newdirfd != OS::TGT_AT_FDCWD) 81610203SAli.Saidi@ARM.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 81710203SAli.Saidi@ARM.com 81810850SGiacomo.Gabrielli@arm.com std::string new_name; 81910850SGiacomo.Gabrielli@arm.com 82010850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(new_name, 82111851Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 82210850SGiacomo.Gabrielli@arm.com return -EFAULT; 82310850SGiacomo.Gabrielli@arm.com 82410850SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 82510850SGiacomo.Gabrielli@arm.com old_name = process->fullPath(old_name); 82610850SGiacomo.Gabrielli@arm.com new_name = process->fullPath(new_name); 82710850SGiacomo.Gabrielli@arm.com 82810850SGiacomo.Gabrielli@arm.com int result = rename(old_name.c_str(), new_name.c_str()); 82910850SGiacomo.Gabrielli@arm.com return (result == -1) ? -errno : result; 83010850SGiacomo.Gabrielli@arm.com} 83110850SGiacomo.Gabrielli@arm.com 83210850SGiacomo.Gabrielli@arm.com/// Target sysinfo() handler. 83310850SGiacomo.Gabrielli@arm.comtemplate <class OS> 83410850SGiacomo.Gabrielli@arm.comSyscallReturn 83510850SGiacomo.Gabrielli@arm.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 83610850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 83710850SGiacomo.Gabrielli@arm.com{ 83810850SGiacomo.Gabrielli@arm.com 83910850SGiacomo.Gabrielli@arm.com int index = 0; 84010850SGiacomo.Gabrielli@arm.com TypedBufferArg<typename OS::tgt_sysinfo> 84110850SGiacomo.Gabrielli@arm.com sysinfo(process->getSyscallArg(tc, index)); 84210850SGiacomo.Gabrielli@arm.com 84310850SGiacomo.Gabrielli@arm.com sysinfo->uptime = seconds_since_epoch; 84410850SGiacomo.Gabrielli@arm.com sysinfo->totalram = process->system->memSize(); 84510850SGiacomo.Gabrielli@arm.com sysinfo->mem_unit = 1; 84610850SGiacomo.Gabrielli@arm.com 84710850SGiacomo.Gabrielli@arm.com sysinfo.copyOut(tc->getMemProxy()); 84810850SGiacomo.Gabrielli@arm.com 84910850SGiacomo.Gabrielli@arm.com return 0; 85010850SGiacomo.Gabrielli@arm.com} 85110850SGiacomo.Gabrielli@arm.com 85210850SGiacomo.Gabrielli@arm.com/// Target chmod() handler. 85310850SGiacomo.Gabrielli@arm.comtemplate <class OS> 8546640Svince@csl.cornell.eduSyscallReturn 8556640Svince@csl.cornell.educhmodFunc(SyscallDesc *desc, int callnum, Process *process, 8566640Svince@csl.cornell.edu ThreadContext *tc) 85711851Sbrandon.potter@amd.com{ 85811851Sbrandon.potter@amd.com std::string path; 8596640Svince@csl.cornell.edu 8606640Svince@csl.cornell.edu int index = 0; 8616701Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 8626701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 86310793Sbrandon.potter@amd.com return -EFAULT; 8646640Svince@csl.cornell.edu } 86511758Sbrandon.potter@amd.com 86611758Sbrandon.potter@amd.com uint32_t mode = process->getSyscallArg(tc, index); 86711758Sbrandon.potter@amd.com mode_t hostMode = 0; 8686640Svince@csl.cornell.edu 8698706Sandreas.hansson@arm.com // XXX translate mode flags via OS::something??? 8706640Svince@csl.cornell.edu hostMode = mode; 8716701Sgblack@eecs.umich.edu 8726640Svince@csl.cornell.edu // Adjust path for current working directory 873360SN/A path = process->fullPath(path); 8741999SN/A 8751999SN/A // do the chmod 8761999SN/A int result = chmod(path.c_str(), hostMode); 87711851Sbrandon.potter@amd.com if (result < 0) 8782680Sktlim@umich.edu return -errno; 8791999SN/A 8801999SN/A return 0; 8811999SN/A} 8826701Sgblack@eecs.umich.edu 8838852Sandreas.hansson@arm.com 8846701Sgblack@eecs.umich.edu/// Target fchmod() handler. 8851999SN/Atemplate <class OS> 8866701Sgblack@eecs.umich.eduSyscallReturn 8871999SN/AfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 8886701Sgblack@eecs.umich.edu{ 8891999SN/A int index = 0; 8901999SN/A int tgt_fd = p->getSyscallArg(tc, index); 8911999SN/A uint32_t mode = p->getSyscallArg(tc, index); 8921999SN/A 8931999SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 8943669Sbinkertn@umich.edu if (!ffdp) 8953669Sbinkertn@umich.edu return -EBADF; 8963669Sbinkertn@umich.edu int sim_fd = ffdp->getSimFD(); 8971999SN/A 8981999SN/A mode_t hostMode = mode; 8991999SN/A 9002218SN/A int result = fchmod(sim_fd, hostMode); 9011999SN/A 9021999SN/A return (result < 0) ? -errno : 0; 9031999SN/A} 9041999SN/A 9051999SN/A/// Target mremap() handler. 9061999SN/Atemplate <class OS> 9071999SN/ASyscallReturn 9081999SN/AmremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 90911856Sbrandon.potter@amd.com{ 9101999SN/A int index = 0; 9116701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 91211856Sbrandon.potter@amd.com uint64_t old_length = process->getSyscallArg(tc, index); 91311856Sbrandon.potter@amd.com uint64_t new_length = process->getSyscallArg(tc, index); 91410931Sbrandon.potter@amd.com uint64_t flags = process->getSyscallArg(tc, index); 91511856Sbrandon.potter@amd.com uint64_t provided_address = 0; 91611856Sbrandon.potter@amd.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 9171999SN/A 91811856Sbrandon.potter@amd.com if (use_provided_address) 9191999SN/A provided_address = process->getSyscallArg(tc, index); 92011856Sbrandon.potter@amd.com 9211999SN/A if ((start % TheISA::PageBytes != 0) || 92211856Sbrandon.potter@amd.com (provided_address % TheISA::PageBytes != 0)) { 9231999SN/A warn("mremap failing: arguments not page aligned"); 92411856Sbrandon.potter@amd.com return -EINVAL; 9251999SN/A } 9261999SN/A 9275877Shsul@eecs.umich.edu new_length = roundUp(new_length, TheISA::PageBytes); 9285877Shsul@eecs.umich.edu 9295877Shsul@eecs.umich.edu if (new_length > old_length) { 93011851Sbrandon.potter@amd.com std::shared_ptr<MemState> mem_state = process->memState; 9315877Shsul@eecs.umich.edu Addr mmap_end = mem_state->getMmapEnd(); 9326701Sgblack@eecs.umich.edu 9336701Sgblack@eecs.umich.edu if ((start + old_length) == mmap_end && 9346701Sgblack@eecs.umich.edu (!use_provided_address || provided_address == start)) { 9356701Sgblack@eecs.umich.edu uint64_t diff = new_length - old_length; 9366701Sgblack@eecs.umich.edu process->allocateMem(mmap_end, diff); 93710027SChris.Adeniyi-Jones@arm.com mem_state->setMmapEnd(mmap_end + diff); 93810027SChris.Adeniyi-Jones@arm.com return start; 93910027SChris.Adeniyi-Jones@arm.com } else { 94010027SChris.Adeniyi-Jones@arm.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 94110027SChris.Adeniyi-Jones@arm.com warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 9425877Shsul@eecs.umich.edu return -ENOMEM; 94310318Sandreas.hansson@arm.com } else { 94410318Sandreas.hansson@arm.com uint64_t new_start = use_provided_address ? 9455877Shsul@eecs.umich.edu provided_address : mmap_end; 9465877Shsul@eecs.umich.edu process->pTable->remap(start, old_length, new_start); 9475877Shsul@eecs.umich.edu warn("mremapping to new vaddr %08p-%08p, adding %d\n", 9485877Shsul@eecs.umich.edu new_start, new_start + new_length, 94910486Stjablin@gmail.com new_length - old_length); 95010486Stjablin@gmail.com // add on the remaining unallocated pages 9515877Shsul@eecs.umich.edu process->allocateMem(new_start + old_length, 95211905SBrandon.Potter@amd.com new_length - old_length, 95311905SBrandon.Potter@amd.com use_provided_address /* clobber */); 95411905SBrandon.Potter@amd.com if (!use_provided_address) 95511905SBrandon.Potter@amd.com mem_state->setMmapEnd(mmap_end + new_length); 95610027SChris.Adeniyi-Jones@arm.com if (use_provided_address && 9575877Shsul@eecs.umich.edu new_start + new_length > mem_state->getMmapEnd()) { 95811905SBrandon.Potter@amd.com // something fishy going on here, at least notify the user 95911905SBrandon.Potter@amd.com // @todo: increase mmap_end? 9605877Shsul@eecs.umich.edu warn("mmap region limit exceeded with MREMAP_FIXED\n"); 9615877Shsul@eecs.umich.edu } 96210027SChris.Adeniyi-Jones@arm.com warn("returning %08p as start\n", new_start); 9635877Shsul@eecs.umich.edu return new_start; 9645877Shsul@eecs.umich.edu } 9655877Shsul@eecs.umich.edu } 96610027SChris.Adeniyi-Jones@arm.com } else { 96711905SBrandon.Potter@amd.com if (use_provided_address && provided_address != start) 96810027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 96910027SChris.Adeniyi-Jones@arm.com process->pTable->unmap(start + new_length, old_length - new_length); 97010027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 97110027SChris.Adeniyi-Jones@arm.com } 9725877Shsul@eecs.umich.edu} 97310027SChris.Adeniyi-Jones@arm.com 97410027SChris.Adeniyi-Jones@arm.com/// Target stat() handler. 97510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 97610027SChris.Adeniyi-Jones@arm.comSyscallReturn 97711905SBrandon.Potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 97810027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 97911905SBrandon.Potter@amd.com{ 98010027SChris.Adeniyi-Jones@arm.com std::string path; 98110027SChris.Adeniyi-Jones@arm.com 98210027SChris.Adeniyi-Jones@arm.com int index = 0; 98310027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 98410027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) { 98510027SChris.Adeniyi-Jones@arm.com return -EFAULT; 9865877Shsul@eecs.umich.edu } 9875877Shsul@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 9885877Shsul@eecs.umich.edu 98910027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 99010027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 9918601Ssteve.reinhardt@amd.com 99210027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 9935877Shsul@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 9945877Shsul@eecs.umich.edu 9951999SN/A if (result < 0) 996378SN/A return -errno; 997360SN/A 9981450SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 99911851Sbrandon.potter@amd.com 10002680Sktlim@umich.edu return 0; 1001360SN/A} 1002360SN/A 1003360SN/A 10046701Sgblack@eecs.umich.edu/// Target stat64() handler. 10058852Sandreas.hansson@arm.comtemplate <class OS> 10066701Sgblack@eecs.umich.eduSyscallReturn 10076701Sgblack@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, Process *process, 10086701Sgblack@eecs.umich.edu ThreadContext *tc) 10096701Sgblack@eecs.umich.edu{ 1010360SN/A std::string path; 10113669Sbinkertn@umich.edu 10123669Sbinkertn@umich.edu int index = 0; 10133669Sbinkertn@umich.edu if (!tc->getMemProxy().tryReadString(path, 1014360SN/A process->getSyscallArg(tc, index))) 1015360SN/A return -EFAULT; 1016360SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1017360SN/A 10182218SN/A // Adjust path for current working directory 1019360SN/A path = process->fullPath(path); 10208706Sandreas.hansson@arm.com 1021360SN/A#if NO_STAT64 10221458SN/A struct stat hostBuf; 1023360SN/A int result = stat(path.c_str(), &hostBuf); 1024360SN/A#else 1025360SN/A struct stat64 hostBuf; 10265074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 10275074Ssaidi@eecs.umich.edu#endif 10285074Ssaidi@eecs.umich.edu 102911851Sbrandon.potter@amd.com if (result < 0) 10305074Ssaidi@eecs.umich.edu return -errno; 10315074Ssaidi@eecs.umich.edu 10325074Ssaidi@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10335074Ssaidi@eecs.umich.edu 10346701Sgblack@eecs.umich.edu return 0; 10358852Sandreas.hansson@arm.com} 10366701Sgblack@eecs.umich.edu 10375074Ssaidi@eecs.umich.edu 10386701Sgblack@eecs.umich.edu/// Target fstatat64() handler. 10395074Ssaidi@eecs.umich.edutemplate <class OS> 10405074Ssaidi@eecs.umich.eduSyscallReturn 10415074Ssaidi@eecs.umich.edufstatat64Func(SyscallDesc *desc, int callnum, Process *process, 10425074Ssaidi@eecs.umich.edu ThreadContext *tc) 10435208Ssaidi@eecs.umich.edu{ 10445208Ssaidi@eecs.umich.edu int index = 0; 10455208Ssaidi@eecs.umich.edu int dirfd = process->getSyscallArg(tc, index); 10465208Ssaidi@eecs.umich.edu if (dirfd != OS::TGT_AT_FDCWD) 10475074Ssaidi@eecs.umich.edu warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 10485074Ssaidi@eecs.umich.edu 10495208Ssaidi@eecs.umich.edu std::string path; 10505074Ssaidi@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 10515074Ssaidi@eecs.umich.edu process->getSyscallArg(tc, index))) 10525074Ssaidi@eecs.umich.edu return -EFAULT; 10535074Ssaidi@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 10548706Sandreas.hansson@arm.com 10555074Ssaidi@eecs.umich.edu // Adjust path for current working directory 10565074Ssaidi@eecs.umich.edu path = process->fullPath(path); 10575074Ssaidi@eecs.umich.edu 10585074Ssaidi@eecs.umich.edu#if NO_STAT64 10595074Ssaidi@eecs.umich.edu struct stat hostBuf; 106010027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 106110027SChris.Adeniyi-Jones@arm.com#else 106210027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 106311851Sbrandon.potter@amd.com int result = stat64(path.c_str(), &hostBuf); 106410027SChris.Adeniyi-Jones@arm.com#endif 106510027SChris.Adeniyi-Jones@arm.com 106610027SChris.Adeniyi-Jones@arm.com if (result < 0) 106710027SChris.Adeniyi-Jones@arm.com return -errno; 106810027SChris.Adeniyi-Jones@arm.com 106910793Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 107010027SChris.Adeniyi-Jones@arm.com 107110027SChris.Adeniyi-Jones@arm.com return 0; 107210027SChris.Adeniyi-Jones@arm.com} 107310027SChris.Adeniyi-Jones@arm.com 107410027SChris.Adeniyi-Jones@arm.com 107510027SChris.Adeniyi-Jones@arm.com/// Target fstat64() handler. 107610027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 107710027SChris.Adeniyi-Jones@arm.comSyscallReturn 107810027SChris.Adeniyi-Jones@arm.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 107910027SChris.Adeniyi-Jones@arm.com{ 108010027SChris.Adeniyi-Jones@arm.com int index = 0; 108110027SChris.Adeniyi-Jones@arm.com int tgt_fd = p->getSyscallArg(tc, index); 108210027SChris.Adeniyi-Jones@arm.com Addr bufPtr = p->getSyscallArg(tc, index); 108310027SChris.Adeniyi-Jones@arm.com 108410027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 108510027SChris.Adeniyi-Jones@arm.com if (!ffdp) 108610027SChris.Adeniyi-Jones@arm.com return -EBADF; 108710027SChris.Adeniyi-Jones@arm.com int sim_fd = ffdp->getSimFD(); 108810027SChris.Adeniyi-Jones@arm.com 108910027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 109010027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 109110027SChris.Adeniyi-Jones@arm.com int result = fstat(sim_fd, &hostBuf); 109210027SChris.Adeniyi-Jones@arm.com#else 109310027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 109410027SChris.Adeniyi-Jones@arm.com int result = fstat64(sim_fd, &hostBuf); 109510027SChris.Adeniyi-Jones@arm.com#endif 109610027SChris.Adeniyi-Jones@arm.com 10971999SN/A if (result < 0) 10981999SN/A return -errno; 10991999SN/A 110011856Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 11011999SN/A 11026701Sgblack@eecs.umich.edu return 0; 110311856Sbrandon.potter@amd.com} 110411856Sbrandon.potter@amd.com 110510931Sbrandon.potter@amd.com 110611856Sbrandon.potter@amd.com/// Target lstat() handler. 110711856Sbrandon.potter@amd.comtemplate <class OS> 11081999SN/ASyscallReturn 110911856Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 11101999SN/A ThreadContext *tc) 11112764Sstever@eecs.umich.edu{ 11122064SN/A std::string path; 111310931Sbrandon.potter@amd.com 11142064SN/A int index = 0; 11152064SN/A if (!tc->getMemProxy().tryReadString(path, 111610931Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 11172064SN/A return -EFAULT; 11181999SN/A } 11191999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 11202218SN/A 11211999SN/A // Adjust path for current working directory 112210931Sbrandon.potter@amd.com path = process->fullPath(path); 11231999SN/A 11241999SN/A struct stat hostBuf; 11251999SN/A int result = lstat(path.c_str(), &hostBuf); 11261999SN/A 11271999SN/A if (result < 0) 1128378SN/A return -errno; 1129360SN/A 11301450SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 113111851Sbrandon.potter@amd.com 11322680Sktlim@umich.edu return 0; 1133360SN/A} 1134360SN/A 1135360SN/A/// Target lstat64() handler. 11366701Sgblack@eecs.umich.edutemplate <class OS> 11378852Sandreas.hansson@arm.comSyscallReturn 11386701Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, Process *process, 11396701Sgblack@eecs.umich.edu ThreadContext *tc) 11406701Sgblack@eecs.umich.edu{ 11416701Sgblack@eecs.umich.edu std::string path; 1142360SN/A 11433669Sbinkertn@umich.edu int index = 0; 11443669Sbinkertn@umich.edu if (!tc->getMemProxy().tryReadString(path, 11453669Sbinkertn@umich.edu process->getSyscallArg(tc, index))) { 1146360SN/A return -EFAULT; 1147360SN/A } 1148360SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1149360SN/A 11501458SN/A // Adjust path for current working directory 1151360SN/A path = process->fullPath(path); 11528706Sandreas.hansson@arm.com 1153360SN/A#if NO_STAT64 11541458SN/A struct stat hostBuf; 1155360SN/A int result = lstat(path.c_str(), &hostBuf); 1156360SN/A#else 11571999SN/A struct stat64 hostBuf; 11581999SN/A int result = lstat64(path.c_str(), &hostBuf); 11591999SN/A#endif 116011851Sbrandon.potter@amd.com 11612680Sktlim@umich.edu if (result < 0) 11621999SN/A return -errno; 11631999SN/A 11641999SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11656701Sgblack@eecs.umich.edu 11668852Sandreas.hansson@arm.com return 0; 11676701Sgblack@eecs.umich.edu} 11686701Sgblack@eecs.umich.edu 11696701Sgblack@eecs.umich.edu/// Target fstat() handler. 11706701Sgblack@eecs.umich.edutemplate <class OS> 11711999SN/ASyscallReturn 11723669Sbinkertn@umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 11733669Sbinkertn@umich.edu{ 11743669Sbinkertn@umich.edu int index = 0; 11752764Sstever@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 11762064SN/A Addr bufPtr = p->getSyscallArg(tc, index); 11772064SN/A 11782064SN/A DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 11791999SN/A 11801999SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 11812064SN/A if (!ffdp) 11821999SN/A return -EBADF; 11831999SN/A int sim_fd = ffdp->getSimFD(); 11841999SN/A 11851999SN/A struct stat hostBuf; 11868706Sandreas.hansson@arm.com int result = fstat(sim_fd, &hostBuf); 11871999SN/A 11881999SN/A if (result < 0) 11891999SN/A return -errno; 11901999SN/A 1191378SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1192360SN/A 11931450SN/A return 0; 119411856Sbrandon.potter@amd.com} 1195360SN/A 11966701Sgblack@eecs.umich.edu 119711856Sbrandon.potter@amd.com/// Target statfs() handler. 119811856Sbrandon.potter@amd.comtemplate <class OS> 1199360SN/ASyscallReturn 120011380Salexandru.dutu@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 1201360SN/A ThreadContext *tc) 120211856Sbrandon.potter@amd.com{ 120311856Sbrandon.potter@amd.com#if NO_STATFS 12041458SN/A warn("Host OS cannot support calls to statfs. Ignoring syscall"); 120511856Sbrandon.potter@amd.com#else 1206360SN/A std::string path; 1207360SN/A 120810931Sbrandon.potter@amd.com int index = 0; 1209360SN/A if (!tc->getMemProxy().tryReadString(path, 1210360SN/A process->getSyscallArg(tc, index))) { 12111458SN/A return -EFAULT; 1212360SN/A } 121310931Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 12142021SN/A 12151458SN/A // Adjust path for current working directory 1216360SN/A path = process->fullPath(path); 1217360SN/A 1218360SN/A struct statfs hostBuf; 12191706SN/A int result = statfs(path.c_str(), &hostBuf); 12201706SN/A 12211706SN/A if (result < 0) 122211851Sbrandon.potter@amd.com return -errno; 12232680Sktlim@umich.edu 12241706SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 122511799Sbrandon.potter@amd.com#endif 122611799Sbrandon.potter@amd.com return 0; 122711799Sbrandon.potter@amd.com} 12281706SN/A 12291706SN/Atemplate <class OS> 12306701Sgblack@eecs.umich.eduSyscallReturn 12318852Sandreas.hansson@arm.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 12326701Sgblack@eecs.umich.edu{ 12336701Sgblack@eecs.umich.edu int index = 0; 12346701Sgblack@eecs.umich.edu TheISA::IntReg flags = p->getSyscallArg(tc, index); 12356701Sgblack@eecs.umich.edu TheISA::IntReg newStack = p->getSyscallArg(tc, index); 12361706SN/A Addr ptidPtr = p->getSyscallArg(tc, index); 12373669Sbinkertn@umich.edu Addr ctidPtr = p->getSyscallArg(tc, index); 12383669Sbinkertn@umich.edu Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 12393669Sbinkertn@umich.edu 12401706SN/A if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 12411706SN/A ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 12421706SN/A ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 12431706SN/A ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 12442218SN/A ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 12451706SN/A ((flags & OS::TGT_CLONE_VM) && !(newStack))) 124611759Sbrandon.potter@amd.com return -EINVAL; 124711799Sbrandon.potter@amd.com 12481706SN/A ThreadContext *ctc; 12491706SN/A if (!(ctc = p->findFreeContext())) 12501706SN/A fatal("clone: no spare thread context in system"); 125111886Sbrandon.potter@amd.com 125211886Sbrandon.potter@amd.com /** 125311886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 125411886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 125511886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 125611886Sbrandon.potter@amd.com * constructor. 125711886Sbrandon.potter@amd.com */ 125811886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 125911886Sbrandon.potter@amd.com pp->executable.assign(*(new std::string(p->progName()))); 126011886Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 126111886Sbrandon.potter@amd.com pp->system = p->system; 126211886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 126311886Sbrandon.potter@amd.com pp->input.assign("stdin"); 126411886Sbrandon.potter@amd.com pp->output.assign("stdout"); 126511886Sbrandon.potter@amd.com pp->errout.assign("stderr"); 126611886Sbrandon.potter@amd.com pp->uid = p->uid(); 126711886Sbrandon.potter@amd.com pp->euid = p->euid(); 126811886Sbrandon.potter@amd.com pp->gid = p->gid(); 126911886Sbrandon.potter@amd.com pp->egid = p->egid(); 127011886Sbrandon.potter@amd.com 127111886Sbrandon.potter@amd.com /* Find the first free PID that's less than the maximum */ 127211886Sbrandon.potter@amd.com std::set<int> const& pids = p->system->PIDs; 127311886Sbrandon.potter@amd.com int temp_pid = *pids.begin(); 127411886Sbrandon.potter@amd.com do { 127511886Sbrandon.potter@amd.com temp_pid++; 127611886Sbrandon.potter@amd.com } while (pids.find(temp_pid) != pids.end()); 127711886Sbrandon.potter@amd.com if (temp_pid >= System::maxPID) 127811886Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 127911886Sbrandon.potter@amd.com 128011886Sbrandon.potter@amd.com pp->pid = temp_pid; 128111886Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 128211886Sbrandon.potter@amd.com Process *cp = pp->create(); 128311886Sbrandon.potter@amd.com delete pp; 128411886Sbrandon.potter@amd.com 128511886Sbrandon.potter@amd.com Process *owner = ctc->getProcessPtr(); 128611886Sbrandon.potter@amd.com ctc->setProcessPtr(cp); 128711886Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 128811886Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 128911886Sbrandon.potter@amd.com 129011886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 129111886Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 129211886Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 129311886Sbrandon.potter@amd.com *ptid = cp->pid(); 129411886Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 129511886Sbrandon.potter@amd.com } 129611886Sbrandon.potter@amd.com 129711886Sbrandon.potter@amd.com cp->initState(); 129811886Sbrandon.potter@amd.com p->clone(tc, ctc, cp, flags); 129911886Sbrandon.potter@amd.com 130011886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 130111886Sbrandon.potter@amd.com delete cp->sigchld; 130211886Sbrandon.potter@amd.com cp->sigchld = p->sigchld; 130311886Sbrandon.potter@amd.com } else if (flags & OS::TGT_SIGCHLD) { 130411886Sbrandon.potter@amd.com *cp->sigchld = true; 130511886Sbrandon.potter@amd.com } 130611886Sbrandon.potter@amd.com 130711886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 130811886Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 130911886Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 131011886Sbrandon.potter@amd.com *ctid = cp->pid(); 131111886Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 131211886Sbrandon.potter@amd.com } 131311886Sbrandon.potter@amd.com 131411886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 131511886Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 131611886Sbrandon.potter@amd.com 131711886Sbrandon.potter@amd.com ctc->clearArchRegs(); 131811886Sbrandon.potter@amd.com 131911886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 132011886Sbrandon.potter@amd.com TheISA::copyMiscRegs(tc, ctc); 132111886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 132211886Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 132311886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 132411886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 132511886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 132611886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 132711886Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 132811886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 132911886Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 133011886Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 133111886Sbrandon.potter@amd.com for (int y = 8; y < 32; y++) 133211886Sbrandon.potter@amd.com ctc->setIntReg(y, tc->readIntReg(y)); 133311886Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA 133411886Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 133511886Sbrandon.potter@amd.com#endif 133611886Sbrandon.potter@amd.com 133711886Sbrandon.potter@amd.com#if THE_ISA == X86_ISA 133811886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_SETTLS) { 133911886Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 134011886Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 134111886Sbrandon.potter@amd.com } 134211886Sbrandon.potter@amd.com#endif 134311886Sbrandon.potter@amd.com 134411886Sbrandon.potter@amd.com if (newStack) 134511886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::StackPointerReg, newStack); 134611886Sbrandon.potter@amd.com 134711886Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 134811886Sbrandon.potter@amd.com 134911886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 135011886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 135111886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 135211886Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 135311886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 135411886Sbrandon.potter@amd.com#endif 135511886Sbrandon.potter@amd.com 135611886Sbrandon.potter@amd.com ctc->pcState(tc->nextInstAddr()); 135711886Sbrandon.potter@amd.com ctc->activate(); 135811886Sbrandon.potter@amd.com 135911886Sbrandon.potter@amd.com return cp->pid(); 136011886Sbrandon.potter@amd.com} 136111886Sbrandon.potter@amd.com 136211886Sbrandon.potter@amd.com/// Target fstatfs() handler. 136311886Sbrandon.potter@amd.comtemplate <class OS> 136411886Sbrandon.potter@amd.comSyscallReturn 136511886Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 136611886Sbrandon.potter@amd.com{ 136711886Sbrandon.potter@amd.com int index = 0; 136811886Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 136911886Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 137011886Sbrandon.potter@amd.com 137111886Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 137211886Sbrandon.potter@amd.com if (!ffdp) 137311886Sbrandon.potter@amd.com return -EBADF; 137411886Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 137511886Sbrandon.potter@amd.com 13761706SN/A struct statfs hostBuf; 13771706SN/A int result = fstatfs(sim_fd, &hostBuf); 13781706SN/A 13791706SN/A if (result < 0) 138011856Sbrandon.potter@amd.com return -errno; 13811706SN/A 13826701Sgblack@eecs.umich.edu copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 138311856Sbrandon.potter@amd.com 138411856Sbrandon.potter@amd.com return 0; 13851706SN/A} 138611856Sbrandon.potter@amd.com 138711856Sbrandon.potter@amd.com 13881706SN/A/// Target writev() handler. 138911856Sbrandon.potter@amd.comtemplate <class OS> 13901706SN/ASyscallReturn 13911706SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 139210931Sbrandon.potter@amd.com{ 13931706SN/A int index = 0; 13941706SN/A int tgt_fd = p->getSyscallArg(tc, index); 13952218SN/A 13961706SN/A auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 139711759Sbrandon.potter@amd.com if (!hbfdp) 13981706SN/A return -EBADF; 13991706SN/A int sim_fd = hbfdp->getSimFD(); 14001706SN/A 14011706SN/A SETranslatingPortProxy &prox = tc->getMemProxy(); 14021706SN/A uint64_t tiov_base = p->getSyscallArg(tc, index); 14031999SN/A size_t count = p->getSyscallArg(tc, index); 14041999SN/A struct iovec hiov[count]; 14051999SN/A for (size_t i = 0; i < count; ++i) { 140611856Sbrandon.potter@amd.com typename OS::tgt_iovec tiov; 14071999SN/A 14086701Sgblack@eecs.umich.edu prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 140911856Sbrandon.potter@amd.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 141010931Sbrandon.potter@amd.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 141111856Sbrandon.potter@amd.com hiov[i].iov_base = new char [hiov[i].iov_len]; 141211856Sbrandon.potter@amd.com prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 14131999SN/A hiov[i].iov_len); 141411856Sbrandon.potter@amd.com } 14151999SN/A 141611856Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 141711856Sbrandon.potter@amd.com 141811856Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) 14191999SN/A delete [] (char *)hiov[i].iov_base; 14206227Snate@binkert.org 14211999SN/A if (result < 0) 14222461SN/A return -errno; 142311856Sbrandon.potter@amd.com 142411856Sbrandon.potter@amd.com return result; 14258737Skoansin.tan@gmail.com} 14261999SN/A 142711856Sbrandon.potter@amd.com/// Real mmap handler. 142811856Sbrandon.potter@amd.comtemplate <class OS> 14291999SN/ASyscallReturn 14301999SN/AmmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 143110931Sbrandon.potter@amd.com bool is_mmap2) 14321999SN/A{ 14336227Snate@binkert.org int index = 0; 14341999SN/A Addr start = p->getSyscallArg(tc, index); 14351999SN/A uint64_t length = p->getSyscallArg(tc, index); 14361999SN/A int prot = p->getSyscallArg(tc, index); 14372218SN/A int tgt_flags = p->getSyscallArg(tc, index); 14381999SN/A int tgt_fd = p->getSyscallArg(tc, index); 143910629Sjthestness@gmail.com int offset = p->getSyscallArg(tc, index); 14401999SN/A 14411999SN/A if (is_mmap2) 144211385Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 1443360SN/A 14441450SN/A if (start & (TheISA::PageBytes - 1) || 144511851Sbrandon.potter@amd.com offset & (TheISA::PageBytes - 1) || 144611385Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 1447360SN/A tgt_flags & OS::TGT_MAP_SHARED) || 14486701Sgblack@eecs.umich.edu (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 14496701Sgblack@eecs.umich.edu !(tgt_flags & OS::TGT_MAP_SHARED)) || 14506701Sgblack@eecs.umich.edu !length) { 145111383Sbrandon.potter@amd.com return -EINVAL; 145211383Sbrandon.potter@amd.com } 14538324Ssteve.reinhardt@amd.com 145410486Stjablin@gmail.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1455360SN/A // With shared mmaps, there are two cases to consider: 145611385Sbrandon.potter@amd.com // 1) anonymous: writes should modify the mapping and this should be 145711385Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 14589008Sgblack@eecs.umich.edu // difficult to update the shared mapping because there's no 145911383Sbrandon.potter@amd.com // structure which maintains information about the which virtual 146011383Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 146111383Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 146211383Sbrandon.potter@amd.com // 2) file-backed: writes should modify the mapping and the file 146311383Sbrandon.potter@amd.com // which is backed by the mapping. The shared mapping problem is the 146411383Sbrandon.potter@amd.com // same as what was mentioned about the anonymous mappings. For 146511383Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 146611383Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 146711383Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 14688324Ssteve.reinhardt@amd.com // would cause the change to be propagated to the file mapping at 146911383Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 147011383Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 147111383Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 147211383Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 147311383Sbrandon.potter@amd.com // a call to msync, but that again would require more information 147411383Sbrandon.potter@amd.com // than we currently maintain. 147511383Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 147611383Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 147711383Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 147811383Sbrandon.potter@amd.com } 147911383Sbrandon.potter@amd.com 148011383Sbrandon.potter@amd.com length = roundUp(length, TheISA::PageBytes); 148111383Sbrandon.potter@amd.com 148211383Sbrandon.potter@amd.com int sim_fd = -1; 148311383Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 148411383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 148511383Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 148611383Sbrandon.potter@amd.com 148711383Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 148811383Sbrandon.potter@amd.com if (dfdp) { 148911383Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 149011383Sbrandon.potter@amd.com return emul_driver->mmap(p, tc, start, length, prot, 149111383Sbrandon.potter@amd.com tgt_flags, tgt_fd, offset); 149211383Sbrandon.potter@amd.com } 14938324Ssteve.reinhardt@amd.com 14945877Shsul@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 149510486Stjablin@gmail.com if (!ffdp) 149610486Stjablin@gmail.com return -EBADF; 149711383Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 149811383Sbrandon.potter@amd.com 149911383Sbrandon.potter@amd.com pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 150011856Sbrandon.potter@amd.com sim_fd, offset); 150111624Smichael.lebeane@amd.com 150211856Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 150311856Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 150411856Sbrandon.potter@amd.com return -errno; 150511856Sbrandon.potter@amd.com } 150611624Smichael.lebeane@amd.com } 150711624Smichael.lebeane@amd.com 150811624Smichael.lebeane@amd.com // Extend global mmap region if necessary. Note that we ignore the 150911856Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 151011856Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 151111383Sbrandon.potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 151211856Sbrandon.potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 1513360SN/A 151411383Sbrandon.potter@amd.com start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 151511383Sbrandon.potter@amd.com mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 15168600Ssteve.reinhardt@amd.com 151711383Sbrandon.potter@amd.com mem_state->setMmapEnd(mmap_end); 151811383Sbrandon.potter@amd.com } 151911383Sbrandon.potter@amd.com 15208600Ssteve.reinhardt@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 15212544SN/A start, start + length - 1); 15222544SN/A 152311383Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 152411383Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 152511383Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 152611905SBrandon.Potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 152711905SBrandon.Potter@amd.com if (clobber) { 152811905SBrandon.Potter@amd.com for (auto tc : p->system->threadContexts) { 152911905SBrandon.Potter@amd.com // If we might be overwriting old mappings, we need to 153011905SBrandon.Potter@amd.com // invalidate potentially stale mappings out of the TLBs. 153111905SBrandon.Potter@amd.com tc->getDTBPtr()->flushAll(); 153211905SBrandon.Potter@amd.com tc->getITBPtr()->flushAll(); 153311383Sbrandon.potter@amd.com } 153411383Sbrandon.potter@amd.com } 153511383Sbrandon.potter@amd.com 153611383Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 153711383Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 153811383Sbrandon.potter@amd.com // fatal and bail out of the simulation. 153911383Sbrandon.potter@amd.com p->allocateMem(start, length, clobber); 154011383Sbrandon.potter@amd.com 154111383Sbrandon.potter@amd.com // Transfer content into target address space. 154211383Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 154311383Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 154411383Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 154511383Sbrandon.potter@amd.com // with something like: 154611383Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 154711383Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 15488600Ssteve.reinhardt@amd.com // some applications can map a couple of gigabytes of space 15496672Sgblack@eecs.umich.edu // (intending sparse usage), that can get painfully expensive. 15508600Ssteve.reinhardt@amd.com // Fortunately, since we don't properly implement munmap either, 155111383Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 155211383Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 155311383Sbrandon.potter@amd.com } else { 15548601Ssteve.reinhardt@amd.com // It is possible to mmap an area larger than a file, however 15552544SN/A // accessing unmapped portions the system triggers a "Bus error" 155611383Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 155711383Sbrandon.potter@amd.com // the host into the target address space. 155811383Sbrandon.potter@amd.com struct stat file_stat; 155911383Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 156011383Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 156111383Sbrandon.potter@amd.com 156211383Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 156311383Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 156411383Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 156511383Sbrandon.potter@amd.com // on the specified offset into the file. 156611383Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 156711383Sbrandon.potter@amd.com length); 156811383Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 156911383Sbrandon.potter@amd.com 157011383Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 157111383Sbrandon.potter@amd.com munmap(pmap, length); 157211383Sbrandon.potter@amd.com 157311383Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 157411383Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 157511383Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 157611383Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 157711383Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 157811383Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 157911383Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 158011383Sbrandon.potter@amd.com if (interpreter) { 158111383Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 158211383Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 158311383Sbrandon.potter@amd.com 158411383Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 158511383Sbrandon.potter@amd.com 158611383Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 158711383Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 158811392Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 158911392Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 159011392Sbrandon.potter@amd.com 159111392Sbrandon.potter@amd.com if (lib) { 159211392Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 159311392Sbrandon.potter@amd.com lib->textBase(), start); 159411392Sbrandon.potter@amd.com } 159511392Sbrandon.potter@amd.com } 159611392Sbrandon.potter@amd.com } 159711392Sbrandon.potter@amd.com 159811392Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 159911392Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 160011392Sbrandon.potter@amd.com // execution (hopefully). 160111392Sbrandon.potter@amd.com } 160211856Sbrandon.potter@amd.com 160311856Sbrandon.potter@amd.com return start; 160411856Sbrandon.potter@amd.com} 160511392Sbrandon.potter@amd.com 160611392Sbrandon.potter@amd.comtemplate <class OS> 160711392Sbrandon.potter@amd.comSyscallReturn 160811392Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 160911392Sbrandon.potter@amd.com{ 161011392Sbrandon.potter@amd.com int index = 0; 161111392Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 161211392Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 161311383Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 161411383Sbrandon.potter@amd.com int offset = p->getSyscallArg(tc, index); 161511383Sbrandon.potter@amd.com 161611383Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 161711383Sbrandon.potter@amd.com if (!ffdp) 16181458SN/A return -EBADF; 1619360SN/A int sim_fd = ffdp->getSimFD(); 1620360SN/A 162111593Santhony.gutierrez@amd.com BufferArg bufArg(bufPtr, nbytes); 162211593Santhony.gutierrez@amd.com bufArg.copyIn(tc->getMemProxy()); 162311851Sbrandon.potter@amd.com 162411593Santhony.gutierrez@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 162511593Santhony.gutierrez@amd.com 162611593Santhony.gutierrez@amd.com return (bytes_written == -1) ? -errno : bytes_written; 162711593Santhony.gutierrez@amd.com} 162811593Santhony.gutierrez@amd.com 162911593Santhony.gutierrez@amd.com/// Target mmap() handler. 163011593Santhony.gutierrez@amd.comtemplate <class OS> 163111856Sbrandon.potter@amd.comSyscallReturn 163211856Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 163311593Santhony.gutierrez@amd.com{ 163411856Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, false); 163511593Santhony.gutierrez@amd.com} 163611593Santhony.gutierrez@amd.com 163711593Santhony.gutierrez@amd.com/// Target mmap2() handler. 163811593Santhony.gutierrez@amd.comtemplate <class OS> 163911594Santhony.gutierrez@amd.comSyscallReturn 164011593Santhony.gutierrez@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 164111593Santhony.gutierrez@amd.com{ 164211593Santhony.gutierrez@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 164311593Santhony.gutierrez@amd.com} 164411385Sbrandon.potter@amd.com 164511385Sbrandon.potter@amd.com/// Target getrlimit() handler. 164611385Sbrandon.potter@amd.comtemplate <class OS> 164711851Sbrandon.potter@amd.comSyscallReturn 164811385Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 164911385Sbrandon.potter@amd.com ThreadContext *tc) 165011385Sbrandon.potter@amd.com{ 165111385Sbrandon.potter@amd.com int index = 0; 165211385Sbrandon.potter@amd.com unsigned resource = process->getSyscallArg(tc, index); 165311385Sbrandon.potter@amd.com TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 165411385Sbrandon.potter@amd.com 165511851Sbrandon.potter@amd.com switch (resource) { 165611385Sbrandon.potter@amd.com case OS::TGT_RLIMIT_STACK: 165711385Sbrandon.potter@amd.com // max stack size in bytes: make up a number (8MB for now) 165811385Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 165911385Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1660378SN/A rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1661360SN/A break; 16621450SN/A 166311851Sbrandon.potter@amd.com case OS::TGT_RLIMIT_DATA: 166411851Sbrandon.potter@amd.com // max data segment size in bytes: make up a number 1665360SN/A rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 16666701Sgblack@eecs.umich.edu rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 16676701Sgblack@eecs.umich.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 16686701Sgblack@eecs.umich.edu break; 1669360SN/A 1670360SN/A default: 167111906SBrandon.Potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 167211906SBrandon.Potter@amd.com return -EINVAL; 167311906SBrandon.Potter@amd.com break; 167411906SBrandon.Potter@amd.com } 167511906SBrandon.Potter@amd.com 167611906SBrandon.Potter@amd.com rlp.copyOut(tc->getMemProxy()); 1677360SN/A return 0; 167811906SBrandon.Potter@amd.com} 167911906SBrandon.Potter@amd.com 168011906SBrandon.Potter@amd.com/// Target clock_gettime() function. 168111906SBrandon.Potter@amd.comtemplate <class OS> 168211906SBrandon.Potter@amd.comSyscallReturn 168311906SBrandon.Potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16845877Shsul@eecs.umich.edu{ 168511906SBrandon.Potter@amd.com int index = 1; 168611906SBrandon.Potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 168711906SBrandon.Potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 168811906SBrandon.Potter@amd.com 1689360SN/A getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 1690360SN/A tp->tv_sec += seconds_since_epoch; 16918706Sandreas.hansson@arm.com tp->tv_sec = TheISA::htog(tp->tv_sec); 16921458SN/A tp->tv_nsec = TheISA::htog(tp->tv_nsec); 1693360SN/A 1694360SN/A tp.copyOut(tc->getMemProxy()); 169510796Sbrandon.potter@amd.com 169610796Sbrandon.potter@amd.com return 0; 169710796Sbrandon.potter@amd.com} 169811851Sbrandon.potter@amd.com 169910796Sbrandon.potter@amd.com/// Target clock_getres() function. 170010796Sbrandon.potter@amd.comtemplate <class OS> 170110796Sbrandon.potter@amd.comSyscallReturn 170210796Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 170310796Sbrandon.potter@amd.com{ 170410796Sbrandon.potter@amd.com int index = 1; 170510796Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 170610796Sbrandon.potter@amd.com 170710796Sbrandon.potter@amd.com // Set resolution at ns, which is what clock_gettime() returns 170810796Sbrandon.potter@amd.com tp->tv_sec = 0; 170910796Sbrandon.potter@amd.com tp->tv_nsec = 1; 171010796Sbrandon.potter@amd.com 171110796Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 171210796Sbrandon.potter@amd.com 171310796Sbrandon.potter@amd.com return 0; 171411337SMichael.Lebeane@amd.com} 171511337SMichael.Lebeane@amd.com 171611337SMichael.Lebeane@amd.com/// Target gettimeofday() handler. 171711851Sbrandon.potter@amd.comtemplate <class OS> 171811337SMichael.Lebeane@amd.comSyscallReturn 171911337SMichael.Lebeane@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 172011337SMichael.Lebeane@amd.com ThreadContext *tc) 172111337SMichael.Lebeane@amd.com{ 172211337SMichael.Lebeane@amd.com int index = 0; 172311337SMichael.Lebeane@amd.com TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 172411337SMichael.Lebeane@amd.com 172511337SMichael.Lebeane@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 172611337SMichael.Lebeane@amd.com tp->tv_sec += seconds_since_epoch; 172711337SMichael.Lebeane@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 172811337SMichael.Lebeane@amd.com tp->tv_usec = TheISA::htog(tp->tv_usec); 172911337SMichael.Lebeane@amd.com 173011337SMichael.Lebeane@amd.com tp.copyOut(tc->getMemProxy()); 1731378SN/A 1732360SN/A return 0; 17331450SN/A} 173411851Sbrandon.potter@amd.com 173511851Sbrandon.potter@amd.com 1736360SN/A/// Target utimes() handler. 17376701Sgblack@eecs.umich.edutemplate <class OS> 17386701Sgblack@eecs.umich.eduSyscallReturn 1739360SN/AutimesFunc(SyscallDesc *desc, int callnum, Process *process, 174010796Sbrandon.potter@amd.com ThreadContext *tc) 1741360SN/A{ 17426109Ssanchezd@stanford.edu std::string path; 17436109Ssanchezd@stanford.edu 1744360SN/A int index = 0; 17458706Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 1746360SN/A process->getSyscallArg(tc, index))) { 17471458SN/A return -EFAULT; 1748360SN/A } 1749360SN/A 1750360SN/A TypedBufferArg<typename OS::timeval [2]> 17511999SN/A tp(process->getSyscallArg(tc, index)); 17521999SN/A tp.copyIn(tc->getMemProxy()); 17531999SN/A 175411851Sbrandon.potter@amd.com struct timeval hostTimeval[2]; 17552680Sktlim@umich.edu for (int i = 0; i < 2; ++i) { 17561999SN/A hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 17571999SN/A hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 17581999SN/A } 17596701Sgblack@eecs.umich.edu 17608852Sandreas.hansson@arm.com // Adjust path for current working directory 17616701Sgblack@eecs.umich.edu path = process->fullPath(path); 17626701Sgblack@eecs.umich.edu 17636701Sgblack@eecs.umich.edu int result = utimes(path.c_str(), hostTimeval); 17641999SN/A 17656701Sgblack@eecs.umich.edu if (result < 0) 17666701Sgblack@eecs.umich.edu return -errno; 17678706Sandreas.hansson@arm.com 17681999SN/A return 0; 17691999SN/A} 177011906SBrandon.Potter@amd.com 17718737Skoansin.tan@gmail.comtemplate <class OS> 17728737Skoansin.tan@gmail.comSyscallReturn 17731999SN/AexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 17743669Sbinkertn@umich.edu{ 17753669Sbinkertn@umich.edu desc->setFlags(0); 17763669Sbinkertn@umich.edu 17773669Sbinkertn@umich.edu int index = 0; 17781999SN/A std::string path; 17791999SN/A SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 17801999SN/A if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 17811999SN/A return -EFAULT; 17821999SN/A 17831999SN/A if (access(path.c_str(), F_OK) == -1) 17841999SN/A return -EACCES; 178511886Sbrandon.potter@amd.com 178611886Sbrandon.potter@amd.com auto read_in = [](std::vector<std::string> & vect, 178711886Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy, 178811886Sbrandon.potter@amd.com Addr mem_loc) 178911886Sbrandon.potter@amd.com { 179011886Sbrandon.potter@amd.com for (int inc = 0; ; inc++) { 179111886Sbrandon.potter@amd.com BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 179211886Sbrandon.potter@amd.com b.copyIn(mem_proxy); 179311886Sbrandon.potter@amd.com 179411886Sbrandon.potter@amd.com if (!*(Addr*)b.bufferPtr()) 179511886Sbrandon.potter@amd.com break; 179611886Sbrandon.potter@amd.com 179711886Sbrandon.potter@amd.com vect.push_back(std::string()); 179811886Sbrandon.potter@amd.com mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 179911886Sbrandon.potter@amd.com } 180011886Sbrandon.potter@amd.com }; 180111886Sbrandon.potter@amd.com 180211886Sbrandon.potter@amd.com /** 180311886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 180411886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 180511886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 180611886Sbrandon.potter@amd.com * constructor. 180711886Sbrandon.potter@amd.com */ 180811886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 180911886Sbrandon.potter@amd.com pp->executable = path; 181011886Sbrandon.potter@amd.com Addr argv_mem_loc = p->getSyscallArg(tc, index); 181111886Sbrandon.potter@amd.com read_in(pp->cmd, mem_proxy, argv_mem_loc); 181211886Sbrandon.potter@amd.com Addr envp_mem_loc = p->getSyscallArg(tc, index); 181311886Sbrandon.potter@amd.com read_in(pp->env, mem_proxy, envp_mem_loc); 181411886Sbrandon.potter@amd.com pp->uid = p->uid(); 181511886Sbrandon.potter@amd.com pp->egid = p->egid(); 181611886Sbrandon.potter@amd.com pp->euid = p->euid(); 181711886Sbrandon.potter@amd.com pp->gid = p->gid(); 181811886Sbrandon.potter@amd.com pp->ppid = p->ppid(); 181911886Sbrandon.potter@amd.com pp->pid = p->pid(); 182011886Sbrandon.potter@amd.com pp->input.assign("cin"); 182111886Sbrandon.potter@amd.com pp->output.assign("cout"); 182211886Sbrandon.potter@amd.com pp->errout.assign("cerr"); 182311886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 182411886Sbrandon.potter@amd.com pp->system = p->system; 182511886Sbrandon.potter@amd.com /** 182611886Sbrandon.potter@amd.com * Prevent process object creation with identical PIDs (which will trip 182711886Sbrandon.potter@amd.com * a fatal check in Process constructor). The execve call is supposed to 182811886Sbrandon.potter@amd.com * take over the currently executing process' identity but replace 182911886Sbrandon.potter@amd.com * whatever it is doing with a new process image. Instead of hijacking 183011886Sbrandon.potter@amd.com * the process object in the simulator, we create a new process object 183111886Sbrandon.potter@amd.com * and bind to the previous process' thread below (hijacking the thread). 183211886Sbrandon.potter@amd.com */ 183311886Sbrandon.potter@amd.com p->system->PIDs.erase(p->pid()); 183411886Sbrandon.potter@amd.com Process *new_p = pp->create(); 183511886Sbrandon.potter@amd.com delete pp; 183611886Sbrandon.potter@amd.com 183711886Sbrandon.potter@amd.com /** 183811886Sbrandon.potter@amd.com * Work through the file descriptor array and close any files marked 183911886Sbrandon.potter@amd.com * close-on-exec. 184011886Sbrandon.potter@amd.com */ 184111886Sbrandon.potter@amd.com new_p->fds = p->fds; 184211886Sbrandon.potter@amd.com for (int i = 0; i < new_p->fds->getSize(); i++) { 184311886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 184411886Sbrandon.potter@amd.com if (fdep && fdep->getCOE()) 184511886Sbrandon.potter@amd.com new_p->fds->closeFDEntry(i); 184611886Sbrandon.potter@amd.com } 184711886Sbrandon.potter@amd.com 184811886Sbrandon.potter@amd.com *new_p->sigchld = true; 184911886Sbrandon.potter@amd.com 185011886Sbrandon.potter@amd.com delete p; 185111886Sbrandon.potter@amd.com tc->clearArchRegs(); 185211886Sbrandon.potter@amd.com tc->setProcessPtr(new_p); 185311886Sbrandon.potter@amd.com new_p->assignThreadContext(tc->contextId()); 185411886Sbrandon.potter@amd.com new_p->initState(); 185511886Sbrandon.potter@amd.com tc->activate(); 185611886Sbrandon.potter@amd.com TheISA::PCState pcState = tc->pcState(); 185711886Sbrandon.potter@amd.com tc->setNPC(pcState.instAddr()); 185811886Sbrandon.potter@amd.com 185911886Sbrandon.potter@amd.com desc->setFlags(SyscallDesc::SuppressReturnValue); 186011886Sbrandon.potter@amd.com return 0; 186111886Sbrandon.potter@amd.com} 186211886Sbrandon.potter@amd.com 186311886Sbrandon.potter@amd.com/// Target getrusage() function. 186411886Sbrandon.potter@amd.comtemplate <class OS> 186511886Sbrandon.potter@amd.comSyscallReturn 186611886Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 186711886Sbrandon.potter@amd.com ThreadContext *tc) 186811886Sbrandon.potter@amd.com{ 186911886Sbrandon.potter@amd.com int index = 0; 187011886Sbrandon.potter@amd.com int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 187111886Sbrandon.potter@amd.com TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 187211886Sbrandon.potter@amd.com 187311886Sbrandon.potter@amd.com rup->ru_utime.tv_sec = 0; 187411886Sbrandon.potter@amd.com rup->ru_utime.tv_usec = 0; 187511886Sbrandon.potter@amd.com rup->ru_stime.tv_sec = 0; 187611886Sbrandon.potter@amd.com rup->ru_stime.tv_usec = 0; 187711886Sbrandon.potter@amd.com rup->ru_maxrss = 0; 1878378SN/A rup->ru_ixrss = 0; 1879360SN/A rup->ru_idrss = 0; 18801450SN/A rup->ru_isrss = 0; 188111851Sbrandon.potter@amd.com rup->ru_minflt = 0; 18822680Sktlim@umich.edu rup->ru_majflt = 0; 1883360SN/A rup->ru_nswap = 0; 18846701Sgblack@eecs.umich.edu rup->ru_inblock = 0; 18856701Sgblack@eecs.umich.edu rup->ru_oublock = 0; 18866701Sgblack@eecs.umich.edu rup->ru_msgsnd = 0; 1887360SN/A rup->ru_msgrcv = 0; 18883670Sbinkertn@umich.edu rup->ru_nsignals = 0; 18893670Sbinkertn@umich.edu rup->ru_nvcsw = 0; 1890360SN/A rup->ru_nivcsw = 0; 1891360SN/A 1892360SN/A switch (who) { 1893360SN/A case OS::TGT_RUSAGE_SELF: 1894360SN/A getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1895360SN/A rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 1896360SN/A rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 1897360SN/A break; 1898360SN/A 1899360SN/A case OS::TGT_RUSAGE_CHILDREN: 1900360SN/A // do nothing. We have no child processes, so they take no time. 1901360SN/A break; 1902360SN/A 1903360SN/A default: 1904360SN/A // don't really handle THREAD or CHILDREN, but just warn and 1905360SN/A // plow ahead 1906360SN/A warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 19073670Sbinkertn@umich.edu who); 19083670Sbinkertn@umich.edu } 190910796Sbrandon.potter@amd.com 19108737Skoansin.tan@gmail.com rup.copyOut(tc->getMemProxy()); 19118737Skoansin.tan@gmail.com 19123670Sbinkertn@umich.edu return 0; 19133670Sbinkertn@umich.edu} 19143670Sbinkertn@umich.edu 19153670Sbinkertn@umich.edu/// Target times() function. 19163670Sbinkertn@umich.edutemplate <class OS> 19173670Sbinkertn@umich.eduSyscallReturn 19183670Sbinkertn@umich.edutimesFunc(SyscallDesc *desc, int callnum, Process *process, 19193670Sbinkertn@umich.edu ThreadContext *tc) 19203670Sbinkertn@umich.edu{ 19213670Sbinkertn@umich.edu int index = 0; 19223670Sbinkertn@umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 19233670Sbinkertn@umich.edu 19243670Sbinkertn@umich.edu // Fill in the time structure (in clocks) 19258706Sandreas.hansson@arm.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 1926360SN/A bufp->tms_utime = clocks; 19271458SN/A bufp->tms_stime = 0; 1928360SN/A bufp->tms_cutime = 0; 1929360SN/A bufp->tms_cstime = 0; 19306683Stjones1@inf.ed.ac.uk 19316683Stjones1@inf.ed.ac.uk // Convert to host endianness 19326683Stjones1@inf.ed.ac.uk bufp->tms_utime = TheISA::htog(bufp->tms_utime); 193311851Sbrandon.potter@amd.com 193411851Sbrandon.potter@amd.com // Write back 19356683Stjones1@inf.ed.ac.uk bufp.copyOut(tc->getMemProxy()); 19366701Sgblack@eecs.umich.edu 19376701Sgblack@eecs.umich.edu // Return clock ticks since system boot 19386683Stjones1@inf.ed.ac.uk return clocks; 19396683Stjones1@inf.ed.ac.uk} 19407823Ssteve.reinhardt@amd.com 19416683Stjones1@inf.ed.ac.uk/// Target time() function. 19426683Stjones1@inf.ed.ac.uktemplate <class OS> 19436683Stjones1@inf.ed.ac.ukSyscallReturn 19446683Stjones1@inf.ed.ac.uktimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 19456683Stjones1@inf.ed.ac.uk{ 19466683Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 19478737Skoansin.tan@gmail.com getElapsedTimeMicro(sec, usec); 19486683Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 19496683Stjones1@inf.ed.ac.uk 19508706Sandreas.hansson@arm.com int index = 0; 19516683Stjones1@inf.ed.ac.uk Addr taddr = (Addr)process->getSyscallArg(tc, index); 19526683Stjones1@inf.ed.ac.uk if (taddr != 0) { 19536683Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 19546683Stjones1@inf.ed.ac.uk t = TheISA::htog(t); 19552553SN/A SETranslatingPortProxy &p = tc->getMemProxy(); 19566684Stjones1@inf.ed.ac.uk p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 19576684Stjones1@inf.ed.ac.uk } 19586684Stjones1@inf.ed.ac.uk return sec; 195911851Sbrandon.potter@amd.com} 19606684Stjones1@inf.ed.ac.uk 19616684Stjones1@inf.ed.ac.uktemplate <class OS> 196210796Sbrandon.potter@amd.comSyscallReturn 19636684Stjones1@inf.ed.ac.uktgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 19646684Stjones1@inf.ed.ac.uk{ 19656701Sgblack@eecs.umich.edu int index = 0; 19666701Sgblack@eecs.umich.edu int tgid = process->getSyscallArg(tc, index); 196711321Ssteve.reinhardt@amd.com int tid = process->getSyscallArg(tc, index); 19686684Stjones1@inf.ed.ac.uk int sig = process->getSyscallArg(tc, index); 19698737Skoansin.tan@gmail.com 19708852Sandreas.hansson@arm.com /** 19718852Sandreas.hansson@arm.com * This system call is intended to allow killing a specific thread 19726684Stjones1@inf.ed.ac.uk * within an arbitrary thread group if sanctioned with permission checks. 19736684Stjones1@inf.ed.ac.uk * It's usually true that threads share the termination signal as pointed 19746684Stjones1@inf.ed.ac.uk * out by the pthread_kill man page and this seems to be the intended 19752553SN/A * usage. Due to this being an emulated environment, assume the following: 19762553SN/A * Threads are allowed to call tgkill because the EUID for all threads 19771354SN/A * should be the same. There is no signal handling mechanism for kernel 1978 * registration of signal handlers since signals are poorly supported in 1979 * emulation mode. Since signal handlers cannot be registered, all 1980 * threads within in a thread group must share the termination signal. 1981 * We never exhaust PIDs so there's no chance of finding the wrong one 1982 * due to PID rollover. 1983 */ 1984 1985 System *sys = tc->getSystemPtr(); 1986 Process *tgt_proc = nullptr; 1987 for (int i = 0; i < sys->numContexts(); i++) { 1988 Process *temp = sys->threadContexts[i]->getProcessPtr(); 1989 if (temp->pid() == tid) { 1990 tgt_proc = temp; 1991 break; 1992 } 1993 } 1994 1995 if (sig != 0 || sig != OS::TGT_SIGABRT) 1996 return -EINVAL; 1997 1998 if (tgt_proc == nullptr) 1999 return -ESRCH; 2000 2001 if (tgid != -1 && tgt_proc->tgid() != tgid) 2002 return -ESRCH; 2003 2004 if (sig == OS::TGT_SIGABRT) 2005 exitGroupFunc(desc, 252, process, tc); 2006 2007 return 0; 2008} 2009 2010 2011#endif // __SIM_SYSCALL_EMUL_HH__ 2012