syscall_emul.hh revision 12426
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> 817075Snate@binkert.org#include <sys/mman.h> 82360SN/A#include <sys/stat.h> 8311800Sbrandon.potter@amd.com#if (NO_STATFS == 0) 8411392Sbrandon.potter@amd.com#include <sys/statfs.h> 852462SN/A#else 861354SN/A#include <sys/mount.h> 876216Snate@binkert.org#endif 886658Snate@binkert.org#include <sys/time.h> 892474SN/A#include <sys/uio.h> 902680Sktlim@umich.edu#include <unistd.h> 918229Snate@binkert.org 9210496Ssteve.reinhardt@amd.com#include <cerrno> 938229Snate@binkert.org#include <memory> 9411794Sbrandon.potter@amd.com#include <string> 9510497Ssteve.reinhardt@amd.com 9611794Sbrandon.potter@amd.com#include "arch/utility.hh" 97360SN/A#include "base/intmath.hh" 9811794Sbrandon.potter@amd.com#include "base/loader/object_file.hh" 99360SN/A#include "base/logging.hh" 100360SN/A#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" 109378SN/A#include "sim/process.hh" 1101706SN/A#include "sim/syscall_debug_macros.hh" 11111851Sbrandon.potter@amd.com#include "sim/syscall_desc.hh" 112378SN/A#include "sim/syscall_emul_buf.hh" 113378SN/A#include "sim/syscall_return.hh" 114378SN/A 115378SN/A////////////////////////////////////////////////////////////////////// 116378SN/A// 1171706SN/A// The following emulation functions are generic enough that they 11811851Sbrandon.potter@amd.com// don't need to be recompiled for different emulated OS's. They are 119360SN/A// defined in sim/syscall_emul.cc. 12011760Sbrandon.potter@amd.com// 12111760Sbrandon.potter@amd.com////////////////////////////////////////////////////////////////////// 12211851Sbrandon.potter@amd.com 12311760Sbrandon.potter@amd.com 1246109Ssanchezd@stanford.edu/// Handler for unimplemented syscalls that we haven't thought about. 1251706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 12611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 127378SN/A 1286109Ssanchezd@stanford.edu/// Handler for unimplemented syscalls that we never intend to 1296109Ssanchezd@stanford.edu/// implement (signal handling, etc.) and should not affect the correct 13011851Sbrandon.potter@amd.com/// behavior of the program. Print a warning only if the appropriate 1316109Ssanchezd@stanford.edu/// trace flag is enabled. Return success to the target program. 132378SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1331706SN/A Process *p, ThreadContext *tc); 13411851Sbrandon.potter@amd.com 135378SN/A// Target fallocateFunc() handler. 1365748SSteve.Reinhardt@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 1375748SSteve.Reinhardt@amd.com Process *p, ThreadContext *tc); 13811851Sbrandon.potter@amd.com 139378SN/A/// Target exit() handler: terminate current context. 140378SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1411706SN/A Process *p, ThreadContext *tc); 14211851Sbrandon.potter@amd.com 143378SN/A/// Target exit_group() handler: terminate simulation. (exit all threads) 144378SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1451706SN/A Process *p, ThreadContext *tc); 14611851Sbrandon.potter@amd.com 147378SN/A/// Target set_tid_address() handler. 148378SN/ASyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1491706SN/A Process *p, ThreadContext *tc); 15011851Sbrandon.potter@amd.com 151378SN/A/// Target getpagesize() handler. 152378SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1531706SN/A Process *p, ThreadContext *tc); 15411851Sbrandon.potter@amd.com 155378SN/A/// Target brk() handler: set brk address. 1564118Sgblack@eecs.umich.eduSyscallReturn brkFunc(SyscallDesc *desc, int num, 1574118Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 15811851Sbrandon.potter@amd.com 1594118Sgblack@eecs.umich.edu/// Target close() handler. 160378SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 1611706SN/A Process *p, ThreadContext *tc); 16211851Sbrandon.potter@amd.com 163378SN/A// Target read() handler. 164378SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 1651706SN/A Process *p, ThreadContext *tc); 16611851Sbrandon.potter@amd.com 167360SN/A/// Target write() handler. 1685513SMichael.Adler@intel.comSyscallReturn writeFunc(SyscallDesc *desc, int num, 1695513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 17011851Sbrandon.potter@amd.com 1715513SMichael.Adler@intel.com/// Target lseek() handler. 17210203SAli.Saidi@ARM.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 17310203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 17411851Sbrandon.potter@amd.com 17510203SAli.Saidi@ARM.com/// Target _llseek() handler. 1765513SMichael.Adler@intel.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 17711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1785513SMichael.Adler@intel.com 179511SN/A/// Target munmap() handler. 18010633Smichaelupton@gmail.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 18111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 18210633Smichaelupton@gmail.com 1831706SN/A/// Target gethostname() handler. 18411851Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 185511SN/A Process *p, ThreadContext *tc); 1865513SMichael.Adler@intel.com 1875513SMichael.Adler@intel.com/// Target getcwd() handler. 18811851Sbrandon.potter@amd.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1895513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 190511SN/A 1911706SN/A/// Target readlink() handler. 19211851Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1931706SN/A Process *p, ThreadContext *tc, 1941706SN/A int index = 0); 1951706SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1961706SN/A Process *p, ThreadContext *tc); 19711851Sbrandon.potter@amd.com 1981706SN/A/// Target unlink() handler. 1991706SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2001706SN/A Process *p, ThreadContext *tc, 2011706SN/A int index); 20211851Sbrandon.potter@amd.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2031706SN/A Process *p, ThreadContext *tc); 204511SN/A 2056703Svince@csl.cornell.edu/// Target mkdir() handler. 2066703Svince@csl.cornell.eduSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 20711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2086703Svince@csl.cornell.edu 2096685Stjones1@inf.ed.ac.uk/// Target rename() handler. 2106685Stjones1@inf.ed.ac.ukSyscallReturn renameFunc(SyscallDesc *desc, int num, 21111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2126685Stjones1@inf.ed.ac.uk 2136685Stjones1@inf.ed.ac.uk 2145513SMichael.Adler@intel.com/// Target truncate() handler. 2155513SMichael.Adler@intel.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 21611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2175513SMichael.Adler@intel.com 2185513SMichael.Adler@intel.com 2191999SN/A/// Target ftruncate() handler. 2201999SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 22111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2221999SN/A 2231999SN/A 2241999SN/A/// Target truncate64() handler. 2251999SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num, 22611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2271999SN/A 2283079Sstever@eecs.umich.edu/// Target ftruncate64() handler. 2293079Sstever@eecs.umich.eduSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 23011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2313079Sstever@eecs.umich.edu 2322093SN/A 2332093SN/A/// Target umask() handler. 23411851Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2352093SN/A Process *p, ThreadContext *tc); 2362687Sksewell@umich.edu 2372687Sksewell@umich.edu/// Target gettid() handler. 23811851Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 2392687Sksewell@umich.edu Process *p, ThreadContext *tc); 2402238SN/A 2412238SN/A/// Target chown() handler. 24211851Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num, 2432238SN/A Process *p, ThreadContext *tc); 2442238SN/A 2452238SN/A/// Target setpgid() handler. 24611851Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2472238SN/A Process *p, ThreadContext *tc); 2482238SN/A 2492238SN/A/// Target fchown() handler. 25011851Sbrandon.potter@amd.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2512238SN/A Process *p, ThreadContext *tc); 2522238SN/A 2532238SN/A/// Target dup() handler. 25411851Sbrandon.potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num, 2552238SN/A 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); 2602238SN/A 2612238SN/A/// Target fcntl() handler. 26211851Sbrandon.potter@amd.comSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2632238SN/A Process *process, ThreadContext *tc); 2642238SN/A 2652238SN/A/// Target fcntl64() handler. 26611851Sbrandon.potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2672238SN/A Process *process, ThreadContext *tc); 2686109Ssanchezd@stanford.edu 2696109Ssanchezd@stanford.edu/// Target setuid() handler. 27011851Sbrandon.potter@amd.comSyscallReturn setuidFunc(SyscallDesc *desc, int num, 2712238SN/A Process *p, ThreadContext *tc); 2729455Smitch.hayenga+gem5@gmail.com 2739455Smitch.hayenga+gem5@gmail.com/// Target pipe() handler. 27411851Sbrandon.potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 27510203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 27611851Sbrandon.potter@amd.com 27711851Sbrandon.potter@amd.com/// Internal pipe() handler. 2789455Smitch.hayenga+gem5@gmail.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 2799112Smarc.orr@gmail.com ThreadContext *tc, bool pseudoPipe); 2809112Smarc.orr@gmail.com 2819112Smarc.orr@gmail.com/// Target getpid() handler. 2829112Smarc.orr@gmail.comSyscallReturn getpidFunc(SyscallDesc *desc, int num, 2839112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 28411851Sbrandon.potter@amd.com 2859112Smarc.orr@gmail.com/// Target getuid() handler. 2869112Smarc.orr@gmail.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, 2879112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 2889112Smarc.orr@gmail.com 2899112Smarc.orr@gmail.com/// Target getgid() handler. 2909112Smarc.orr@gmail.comSyscallReturn getgidFunc(SyscallDesc *desc, int num, 2919112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 2929112Smarc.orr@gmail.com 2939112Smarc.orr@gmail.com/// Target getppid() handler. 2949112Smarc.orr@gmail.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, 2959112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 2969112Smarc.orr@gmail.com 2979112Smarc.orr@gmail.com/// Target geteuid() handler. 2989112Smarc.orr@gmail.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2999112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3009112Smarc.orr@gmail.com 3019112Smarc.orr@gmail.com/// Target getegid() handler. 3029112Smarc.orr@gmail.comSyscallReturn getegidFunc(SyscallDesc *desc, int num, 3039238Slluc.alvarez@bsc.es Process *p, ThreadContext *tc); 3049112Smarc.orr@gmail.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 31411367Sandreas.hansson@arm.com/// Used by printf's in multi-threaded apps 3159112Smarc.orr@gmail.comtemplate <class OS> 31611321Ssteve.reinhardt@amd.comSyscallReturn 3179112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3189112Smarc.orr@gmail.com ThreadContext *tc) 3199112Smarc.orr@gmail.com{ 3209112Smarc.orr@gmail.com using namespace std; 3219112Smarc.orr@gmail.com 3229112Smarc.orr@gmail.com int index = 0; 3239112Smarc.orr@gmail.com 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 3279112Smarc.orr@gmail.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; 3349112Smarc.orr@gmail.com 3359112Smarc.orr@gmail.com if (OS::TGT_FUTEX_WAIT == op) { 3369112Smarc.orr@gmail.com // Ensure futex system call accessed atomically. 3379112Smarc.orr@gmail.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 */ 34611321Ssteve.reinhardt@amd.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; 3529112Smarc.orr@gmail.com } else if (OS::TGT_FUTEX_WAKE == op) { 3539112Smarc.orr@gmail.com return futex_map.wakeup(uaddr, process->tgid(), val); 3549112Smarc.orr@gmail.com } 3559238Slluc.alvarez@bsc.es 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 3612238SN/A/// Pseudo Funcs - These functions use a different return convension, 3622238SN/A/// returning a second value in a register other than the normal return register 3632238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3642238SN/A Process *process, ThreadContext *tc); 36511851Sbrandon.potter@amd.com 3662238SN/A/// Target getpidPseudo() handler. 3672238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3682238SN/A Process *p, ThreadContext *tc); 36911851Sbrandon.potter@amd.com 3702238SN/A/// Target getuidPseudo() handler. 3712238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3722238SN/A Process *p, ThreadContext *tc); 37311851Sbrandon.potter@amd.com 3742238SN/A/// Target getgidPseudo() handler. 3752238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3762238SN/A Process *p, ThreadContext *tc); 37711851Sbrandon.potter@amd.com 3782238SN/A 3792238SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 3801354SN/Aconst int one_million = 1000000; 3811354SN/A/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 38210796Sbrandon.potter@amd.comconst int one_billion = 1000000000; 38310796Sbrandon.potter@amd.com 3841354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 3851354SN/A/// by my reckoning. We want to keep this a constant (not use the 3861354SN/A/// real-world time) to keep simulations repeatable. 3871354SN/Aconst unsigned seconds_since_epoch = 1000000000; 3881354SN/A 3891354SN/A/// Helper function to convert current elapsed time to seconds and 3901354SN/A/// microseconds. 3911354SN/Atemplate <class T1, class T2> 3921354SN/Avoid 3931354SN/AgetElapsedTimeMicro(T1 &sec, T2 &usec) 39410796Sbrandon.potter@amd.com{ 3951354SN/A uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 39610796Sbrandon.potter@amd.com sec = elapsed_usecs / one_million; 3971354SN/A usec = elapsed_usecs % one_million; 3981354SN/A} 3991354SN/A 4001354SN/A/// Helper function to convert current elapsed time to seconds and 40110796Sbrandon.potter@amd.com/// nanoseconds. 40210796Sbrandon.potter@amd.comtemplate <class T1, class T2> 40310796Sbrandon.potter@amd.comvoid 40410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec) 40510796Sbrandon.potter@amd.com{ 40610796Sbrandon.potter@amd.com uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 40710796Sbrandon.potter@amd.com sec = elapsed_nsecs / one_billion; 40810796Sbrandon.potter@amd.com nsec = elapsed_nsecs % one_billion; 40910796Sbrandon.potter@amd.com} 41010796Sbrandon.potter@amd.com 41110796Sbrandon.potter@amd.com////////////////////////////////////////////////////////////////////// 412360SN/A// 413360SN/A// The following emulation functions are generic, but need to be 414360SN/A// templated to account for differences in types, constants, etc. 415360SN/A// 416360SN/A////////////////////////////////////////////////////////////////////// 417360SN/A 418360SN/A typedef struct statfs hst_statfs; 41911759Sbrandon.potter@amd.com#if NO_STAT64 4203113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 4213113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 4223113Sgblack@eecs.umich.edu#else 4233113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 4243113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 4253113Sgblack@eecs.umich.edu#endif 4263113Sgblack@eecs.umich.edu 4273113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 4283113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 4293113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 4303113Sgblack@eecs.umich.edu 4313113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 4323113Sgblack@eecs.umich.eduvoid 4333113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 4343113Sgblack@eecs.umich.edu{ 4353113Sgblack@eecs.umich.edu using namespace TheISA; 4364189Sgblack@eecs.umich.edu 4374189Sgblack@eecs.umich.edu if (fakeTTY) 4383113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 4393113Sgblack@eecs.umich.edu else 4403113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 4413113Sgblack@eecs.umich.edu tgt->st_dev = TheISA::htog(tgt->st_dev); 4428737Skoansin.tan@gmail.com tgt->st_ino = host->st_ino; 4433113Sgblack@eecs.umich.edu tgt->st_ino = TheISA::htog(tgt->st_ino); 4448737Skoansin.tan@gmail.com tgt->st_mode = host->st_mode; 4453277Sgblack@eecs.umich.edu if (fakeTTY) { 4465515SMichael.Adler@intel.com // Claim to be a character device 4475515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4485515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4495515SMichael.Adler@intel.com } 4505515SMichael.Adler@intel.com tgt->st_mode = TheISA::htog(tgt->st_mode); 4518737Skoansin.tan@gmail.com tgt->st_nlink = host->st_nlink; 4523277Sgblack@eecs.umich.edu tgt->st_nlink = TheISA::htog(tgt->st_nlink); 4538737Skoansin.tan@gmail.com tgt->st_uid = host->st_uid; 4543277Sgblack@eecs.umich.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 4558737Skoansin.tan@gmail.com tgt->st_gid = host->st_gid; 4563277Sgblack@eecs.umich.edu tgt->st_gid = TheISA::htog(tgt->st_gid); 4578737Skoansin.tan@gmail.com if (fakeTTY) 4583113Sgblack@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); 4628737Skoansin.tan@gmail.com tgt->st_size = host->st_size; 4633113Sgblack@eecs.umich.edu tgt->st_size = TheISA::htog(tgt->st_size); 4648737Skoansin.tan@gmail.com tgt->st_atimeX = host->st_atime; 4653114Sgblack@eecs.umich.edu tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4668737Skoansin.tan@gmail.com tgt->st_mtimeX = host->st_mtime; 4673114Sgblack@eecs.umich.edu tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4688737Skoansin.tan@gmail.com tgt->st_ctimeX = host->st_ctime; 4693114Sgblack@eecs.umich.edu tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4708737Skoansin.tan@gmail.com // Force the block size to be 8KB. This helps to ensure buffered io works 4714061Sgblack@eecs.umich.edu // consistently across different hosts. 4724061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4734061Sgblack@eecs.umich.edu tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4748737Skoansin.tan@gmail.com tgt->st_blocks = host->st_blocks; 4753113Sgblack@eecs.umich.edu tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4768737Skoansin.tan@gmail.com} 4773113Sgblack@eecs.umich.edu 4783113Sgblack@eecs.umich.edu// Same for stat64 4793113Sgblack@eecs.umich.edu 4803113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 4813113Sgblack@eecs.umich.eduvoid 4823113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4833113Sgblack@eecs.umich.edu{ 4843113Sgblack@eecs.umich.edu using namespace TheISA; 4854189Sgblack@eecs.umich.edu 4864189Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4873113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 4883113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 4893113Sgblack@eecs.umich.edu tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 4908737Skoansin.tan@gmail.com tgt->st_mtime_nsec = host->st_mtime_nsec; 4913113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 4928737Skoansin.tan@gmail.com tgt->st_ctime_nsec = host->st_ctime_nsec; 4933113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 4948737Skoansin.tan@gmail.com#else 4953113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 4963113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 4973113Sgblack@eecs.umich.edu 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.eduvoid 5043113Sgblack@eecs.umich.educopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 5058852Sandreas.hansson@arm.com hst_stat *host, bool fakeTTY = false) 5063113Sgblack@eecs.umich.edu{ 5073113Sgblack@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); 5113113Sgblack@eecs.umich.edu} 5123113Sgblack@eecs.umich.edu 5133113Sgblack@eecs.umich.edutemplate<class OS> 5143113Sgblack@eecs.umich.eduvoid 5153113Sgblack@eecs.umich.educopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5168852Sandreas.hansson@arm.com 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); 5216686Stjones1@inf.ed.ac.uk tgt.copyOut(mem); 5223113Sgblack@eecs.umich.edu} 5233113Sgblack@eecs.umich.edu 5243113Sgblack@eecs.umich.edutemplate <class OS> 52511759Sbrandon.potter@amd.comvoid 52611759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 52711759Sbrandon.potter@amd.com hst_statfs *host) 52811759Sbrandon.potter@amd.com{ 52911759Sbrandon.potter@amd.com TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 53011759Sbrandon.potter@amd.com 53111759Sbrandon.potter@amd.com tgt->f_type = TheISA::htog(host->f_type); 53211812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 53311812Sbaz21@cam.ac.uk tgt->f_bsize = TheISA::htog(host->f_iosize); 53411812Sbaz21@cam.ac.uk#else 53511759Sbrandon.potter@amd.com tgt->f_bsize = TheISA::htog(host->f_bsize); 53611812Sbaz21@cam.ac.uk#endif 53711759Sbrandon.potter@amd.com tgt->f_blocks = TheISA::htog(host->f_blocks); 53811759Sbrandon.potter@amd.com tgt->f_bfree = TheISA::htog(host->f_bfree); 53911759Sbrandon.potter@amd.com tgt->f_bavail = TheISA::htog(host->f_bavail); 54011759Sbrandon.potter@amd.com tgt->f_files = TheISA::htog(host->f_files); 54111759Sbrandon.potter@amd.com tgt->f_ffree = TheISA::htog(host->f_ffree); 54211759Sbrandon.potter@amd.com memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 54311759Sbrandon.potter@amd.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 54411812Sbaz21@cam.ac.uk tgt->f_namelen = TheISA::htog(host->f_namemax); 54511812Sbaz21@cam.ac.uk tgt->f_frsize = TheISA::htog(host->f_bsize); 54611812Sbaz21@cam.ac.uk#elif defined(__APPLE__) 54711812Sbaz21@cam.ac.uk tgt->f_namelen = 0; 54811812Sbaz21@cam.ac.uk tgt->f_frsize = 0; 54911812Sbaz21@cam.ac.uk#else 55011812Sbaz21@cam.ac.uk 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)); 55511759Sbrandon.potter@amd.com#else 55611812Sbaz21@cam.ac.uk /* 55711812Sbaz21@cam.ac.uk * The fields are different sizes per OS. Don't bother with 55811812Sbaz21@cam.ac.uk * f_spare or f_reserved on non-Linux for now. 55911812Sbaz21@cam.ac.uk */ 56011812Sbaz21@cam.ac.uk memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 56111812Sbaz21@cam.ac.uk#endif 56211812Sbaz21@cam.ac.uk 56311759Sbrandon.potter@amd.com tgt.copyOut(mem); 56411759Sbrandon.potter@amd.com} 56511759Sbrandon.potter@amd.com 56611759Sbrandon.potter@amd.com/// Target ioctl() handler. For the most part, programs call ioctl() 567378SN/A/// only to find out if their stdout is a tty, to determine whether to 568378SN/A/// do line or block buffering. We always claim that output fds are 5699141Smarc.orr@gmail.com/// not TTYs to provide repeatable results. 5709141Smarc.orr@gmail.comtemplate <class OS> 571360SN/ASyscallReturn 5721450SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 57311851Sbrandon.potter@amd.com{ 5742680Sktlim@umich.edu int index = 0; 575360SN/A int tgt_fd = p->getSyscallArg(tc, index); 5766701Sgblack@eecs.umich.edu unsigned req = p->getSyscallArg(tc, index); 57710930Sbrandon.potter@amd.com 5786701Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 579360SN/A 58010930Sbrandon.potter@amd.com if (OS::isTtyReq(req)) 581360SN/A return -ENOTTY; 58210932Sbrandon.potter@amd.com 58310496Ssteve.reinhardt@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 58410930Sbrandon.potter@amd.com if (!dfdp) 585360SN/A return -EBADF; 5861458SN/A 587360SN/A /** 588360SN/A * If the driver is valid, issue the ioctl through it. Otherwise, 58910930Sbrandon.potter@amd.com * there's an implicit assumption that the device is a TTY type and we 59010930Sbrandon.potter@amd.com * return that we do not have a valid TTY. 59110496Ssteve.reinhardt@amd.com */ 59210496Ssteve.reinhardt@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 5939141Smarc.orr@gmail.com if (emul_driver) 5941458SN/A return emul_driver->ioctl(p, tc, req); 5959141Smarc.orr@gmail.com 596360SN/A /** 5979141Smarc.orr@gmail.com * For lack of a better return code, return ENOTTY. Ideally, we should 59810930Sbrandon.potter@amd.com * return something better here, but at least we issue the warning. 5999141Smarc.orr@gmail.com */ 600360SN/A warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 601360SN/A tgt_fd, req, tc->pcState()); 602360SN/A return -ENOTTY; 60310027SChris.Adeniyi-Jones@arm.com} 60411851Sbrandon.potter@amd.com 60510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 606360SN/ASyscallReturn 607360SN/AopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 608360SN/A bool isopenat) 6098852Sandreas.hansson@arm.com{ 6106701Sgblack@eecs.umich.edu int index = 0; 6111458SN/A int tgt_dirfd = -1; 612360SN/A 6136701Sgblack@eecs.umich.edu /** 6146701Sgblack@eecs.umich.edu * If using the openat variant, read in the target directory file 615360SN/A * descriptor from the simulated process. 616360SN/A */ 617360SN/A if (isopenat) 618360SN/A tgt_dirfd = p->getSyscallArg(tc, index); 619360SN/A 620360SN/A /** 621360SN/A * Retrieve the simulated process' memory proxy and then read in the path 622360SN/A * string from that memory space into the host's working memory space. 623360SN/A */ 624360SN/A std::string path; 625360SN/A if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 626360SN/A return -EFAULT; 6271706SN/A 628360SN/A#ifdef __CYGWIN32__ 629360SN/A int host_flags = O_BINARY; 630360SN/A#else 631360SN/A int host_flags = 0; 632360SN/A#endif 6333669Sbinkertn@umich.edu /** 6343669Sbinkertn@umich.edu * Translate target flags into host flags. Flags exist which are not 6353669Sbinkertn@umich.edu * ported between architectures which can cause check failures. 6361706SN/A */ 6371706SN/A int tgt_flags = p->getSyscallArg(tc, index); 63810496Ssteve.reinhardt@amd.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 63910496Ssteve.reinhardt@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 64010496Ssteve.reinhardt@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 64110496Ssteve.reinhardt@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 64210496Ssteve.reinhardt@amd.com } 64310496Ssteve.reinhardt@amd.com } 64410496Ssteve.reinhardt@amd.com if (tgt_flags) { 64510496Ssteve.reinhardt@amd.com warn("open%s: cannot decode flags 0x%x", 64610496Ssteve.reinhardt@amd.com isopenat ? "at" : "", tgt_flags); 64710496Ssteve.reinhardt@amd.com } 64810496Ssteve.reinhardt@amd.com#ifdef __CYGWIN32__ 64910496Ssteve.reinhardt@amd.com host_flags |= O_BINARY; 65010496Ssteve.reinhardt@amd.com#endif 65110496Ssteve.reinhardt@amd.com 65210496Ssteve.reinhardt@amd.com int mode = p->getSyscallArg(tc, index); 65310496Ssteve.reinhardt@amd.com 65410496Ssteve.reinhardt@amd.com /** 65510496Ssteve.reinhardt@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 65610496Ssteve.reinhardt@amd.com * take the current working directory value which was passed into the 65710496Ssteve.reinhardt@amd.com * process class as a Python parameter and append the current path to 6585795Ssaidi@eecs.umich.edu * create a full path. 6599143Ssteve.reinhardt@amd.com * Otherwise, openat with a valid target directory file descriptor has 6609142Ssteve.reinhardt@amd.com * been called. If the path option, which was passed in as a parameter, 6619142Ssteve.reinhardt@amd.com * is not absolute, retrieve the directory file descriptor's path and 6629143Ssteve.reinhardt@amd.com * prepend it to the path passed in as a parameter. 6635795Ssaidi@eecs.umich.edu * In every case, we should have a full path (which is relevant to the 6649143Ssteve.reinhardt@amd.com * host) to work with after this block has been passed. 6655795Ssaidi@eecs.umich.edu */ 6665795Ssaidi@eecs.umich.edu if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 6675795Ssaidi@eecs.umich.edu path = p->fullPath(path); 6689143Ssteve.reinhardt@amd.com } else if (!startswith(path, "/")) { 6695795Ssaidi@eecs.umich.edu std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 670360SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 6719143Ssteve.reinhardt@amd.com if (!ffdp) 6729143Ssteve.reinhardt@amd.com return -EBADF; 6739143Ssteve.reinhardt@amd.com path.insert(0, ffdp->getFileName()); 67410932Sbrandon.potter@amd.com } 675360SN/A 676360SN/A /** 67710027SChris.Adeniyi-Jones@arm.com * Since this is an emulated environment, we create pseudo file 67810027SChris.Adeniyi-Jones@arm.com * descriptors for device requests that have been registered with 67910027SChris.Adeniyi-Jones@arm.com * the process class through Python; this allows us to create a file 68011851Sbrandon.potter@amd.com * descriptor for subsequent ioctl or mmap calls. 68110027SChris.Adeniyi-Jones@arm.com */ 68210027SChris.Adeniyi-Jones@arm.com if (startswith(path, "/dev/")) { 68310027SChris.Adeniyi-Jones@arm.com std::string filename = path.substr(strlen("/dev/")); 68410027SChris.Adeniyi-Jones@arm.com EmulatedDriver *drv = p->findDriver(filename); 68510027SChris.Adeniyi-Jones@arm.com if (drv) { 68610027SChris.Adeniyi-Jones@arm.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 68710027SChris.Adeniyi-Jones@arm.com "driver open with path[%s]\n", 68810027SChris.Adeniyi-Jones@arm.com isopenat ? "at" : "", path.c_str()); 68911851Sbrandon.potter@amd.com return drv->open(p, tc, mode, host_flags); 69011851Sbrandon.potter@amd.com } 69110027SChris.Adeniyi-Jones@arm.com /** 69210027SChris.Adeniyi-Jones@arm.com * Fall through here for pass through to host devices, such 69310027SChris.Adeniyi-Jones@arm.com * as /dev/zero 69410027SChris.Adeniyi-Jones@arm.com */ 69510027SChris.Adeniyi-Jones@arm.com } 69610027SChris.Adeniyi-Jones@arm.com 69710027SChris.Adeniyi-Jones@arm.com /** 69810027SChris.Adeniyi-Jones@arm.com * Some special paths and files cannot be called on the host and need 69910633Smichaelupton@gmail.com * to be handled as special cases inside the simulator. 70010633Smichaelupton@gmail.com * If the full path that was created above does not match any of the 70110633Smichaelupton@gmail.com * special cases, pass it through to the open call on the host to let 70211851Sbrandon.potter@amd.com * the host open the file on our behalf. 70310633Smichaelupton@gmail.com * If the host cannot open the file, return the host's error code back 70410633Smichaelupton@gmail.com * through the system call to the simulated process. 70510633Smichaelupton@gmail.com */ 70610633Smichaelupton@gmail.com int sim_fd = -1; 70710633Smichaelupton@gmail.com std::vector<std::string> special_paths = 70810633Smichaelupton@gmail.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 70910633Smichaelupton@gmail.com for (auto entry : special_paths) { 71010633Smichaelupton@gmail.com if (startswith(path, entry)) 71110633Smichaelupton@gmail.com sim_fd = OS::openSpecialFile(path, p, tc); 71210633Smichaelupton@gmail.com } 71310203SAli.Saidi@ARM.com if (sim_fd == -1) { 71410203SAli.Saidi@ARM.com sim_fd = open(path.c_str(), host_flags, mode); 71510203SAli.Saidi@ARM.com } 71611851Sbrandon.potter@amd.com if (sim_fd == -1) { 71711851Sbrandon.potter@amd.com int local = -errno; 71810203SAli.Saidi@ARM.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 71910203SAli.Saidi@ARM.com isopenat ? "at" : "", path.c_str()); 72010203SAli.Saidi@ARM.com return local; 72110203SAli.Saidi@ARM.com } 72210203SAli.Saidi@ARM.com 72310203SAli.Saidi@ARM.com /** 72410203SAli.Saidi@ARM.com * The file was opened successfully and needs to be recorded in the 72510203SAli.Saidi@ARM.com * process' file descriptor array so that it can be retrieved later. 72610203SAli.Saidi@ARM.com * The target file descriptor that is chosen will be the lowest unused 72710203SAli.Saidi@ARM.com * file descriptor. 72810203SAli.Saidi@ARM.com * Return the indirect target file descriptor back to the simulated 72911851Sbrandon.potter@amd.com * process to act as a handle for the opened file. 73011851Sbrandon.potter@amd.com */ 73110203SAli.Saidi@ARM.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 73210203SAli.Saidi@ARM.com int tgt_fd = p->fds->allocFD(ffdp); 73310203SAli.Saidi@ARM.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 73410203SAli.Saidi@ARM.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 73510203SAli.Saidi@ARM.com return tgt_fd; 73610203SAli.Saidi@ARM.com} 73710203SAli.Saidi@ARM.com 73810203SAli.Saidi@ARM.com/// Target open() handler. 73910850SGiacomo.Gabrielli@arm.comtemplate <class OS> 74010850SGiacomo.Gabrielli@arm.comSyscallReturn 74110850SGiacomo.Gabrielli@arm.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 74211851Sbrandon.potter@amd.com ThreadContext *tc) 74310850SGiacomo.Gabrielli@arm.com{ 74410850SGiacomo.Gabrielli@arm.com return openImpl<OS>(desc, callnum, process, tc, false); 74510850SGiacomo.Gabrielli@arm.com} 74610850SGiacomo.Gabrielli@arm.com 74710850SGiacomo.Gabrielli@arm.com/// Target openat() handler. 74810850SGiacomo.Gabrielli@arm.comtemplate <class OS> 74910850SGiacomo.Gabrielli@arm.comSyscallReturn 75010850SGiacomo.Gabrielli@arm.comopenatFunc(SyscallDesc *desc, int callnum, Process *process, 75110850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 75210850SGiacomo.Gabrielli@arm.com{ 75310850SGiacomo.Gabrielli@arm.com return openImpl<OS>(desc, callnum, process, tc, true); 75410850SGiacomo.Gabrielli@arm.com} 75510850SGiacomo.Gabrielli@arm.com 75610850SGiacomo.Gabrielli@arm.com/// Target unlinkat() handler. 75710850SGiacomo.Gabrielli@arm.comtemplate <class OS> 75810850SGiacomo.Gabrielli@arm.comSyscallReturn 75910850SGiacomo.Gabrielli@arm.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 76010850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 76110850SGiacomo.Gabrielli@arm.com{ 76210850SGiacomo.Gabrielli@arm.com int index = 0; 76310850SGiacomo.Gabrielli@arm.com int dirfd = process->getSyscallArg(tc, index); 76410850SGiacomo.Gabrielli@arm.com if (dirfd != OS::TGT_AT_FDCWD) 76510850SGiacomo.Gabrielli@arm.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 76610850SGiacomo.Gabrielli@arm.com 76710850SGiacomo.Gabrielli@arm.com return unlinkHelper(desc, callnum, process, tc, 1); 76810850SGiacomo.Gabrielli@arm.com} 76910850SGiacomo.Gabrielli@arm.com 77010850SGiacomo.Gabrielli@arm.com/// Target facessat() handler 77110850SGiacomo.Gabrielli@arm.comtemplate <class OS> 77210850SGiacomo.Gabrielli@arm.comSyscallReturn 77310850SGiacomo.Gabrielli@arm.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 77410850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 7756640Svince@csl.cornell.edu{ 7766640Svince@csl.cornell.edu int index = 0; 7776640Svince@csl.cornell.edu int dirfd = process->getSyscallArg(tc, index); 77811851Sbrandon.potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 77911851Sbrandon.potter@amd.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 7806640Svince@csl.cornell.edu return accessFunc(desc, callnum, process, tc, 1); 7816640Svince@csl.cornell.edu} 7826701Sgblack@eecs.umich.edu 7836701Sgblack@eecs.umich.edu/// Target readlinkat() handler 78410793Sbrandon.potter@amd.comtemplate <class OS> 7856640Svince@csl.cornell.eduSyscallReturn 78611758Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 78711758Sbrandon.potter@amd.com ThreadContext *tc) 78811758Sbrandon.potter@amd.com{ 7896640Svince@csl.cornell.edu int index = 0; 7908706Sandreas.hansson@arm.com int dirfd = process->getSyscallArg(tc, index); 7916640Svince@csl.cornell.edu if (dirfd != OS::TGT_AT_FDCWD) 7926701Sgblack@eecs.umich.edu warn("openat: first argument not AT_FDCWD; unlikely to work"); 7936640Svince@csl.cornell.edu return readlinkFunc(desc, callnum, process, tc, 1); 794360SN/A} 7951999SN/A 7961999SN/A/// Target renameat() handler. 7971999SN/Atemplate <class OS> 79811851Sbrandon.potter@amd.comSyscallReturn 7992680Sktlim@umich.edurenameatFunc(SyscallDesc *desc, int callnum, Process *process, 8001999SN/A ThreadContext *tc) 8011999SN/A{ 8021999SN/A int index = 0; 8036701Sgblack@eecs.umich.edu 8048852Sandreas.hansson@arm.com int olddirfd = process->getSyscallArg(tc, index); 8056701Sgblack@eecs.umich.edu if (olddirfd != OS::TGT_AT_FDCWD) 8061999SN/A warn("renameat: first argument not AT_FDCWD; unlikely to work"); 8076701Sgblack@eecs.umich.edu 8081999SN/A std::string old_name; 8096701Sgblack@eecs.umich.edu 8101999SN/A if (!tc->getMemProxy().tryReadString(old_name, 8111999SN/A process->getSyscallArg(tc, index))) 8121999SN/A return -EFAULT; 8131999SN/A 8141999SN/A int newdirfd = process->getSyscallArg(tc, index); 8153669Sbinkertn@umich.edu if (newdirfd != OS::TGT_AT_FDCWD) 8163669Sbinkertn@umich.edu warn("renameat: third argument not AT_FDCWD; unlikely to work"); 8173669Sbinkertn@umich.edu 8181999SN/A std::string new_name; 8191999SN/A 8201999SN/A if (!tc->getMemProxy().tryReadString(new_name, 8212218SN/A process->getSyscallArg(tc, index))) 8221999SN/A return -EFAULT; 8231999SN/A 8241999SN/A // Adjust path for current working directory 8251999SN/A old_name = process->fullPath(old_name); 8261999SN/A new_name = process->fullPath(new_name); 8271999SN/A 8281999SN/A int result = rename(old_name.c_str(), new_name.c_str()); 8291999SN/A return (result == -1) ? -errno : result; 83011851Sbrandon.potter@amd.com} 8312680Sktlim@umich.edu 8321999SN/A/// Target sysinfo() handler. 8336701Sgblack@eecs.umich.edutemplate <class OS> 83410931Sbrandon.potter@amd.comSyscallReturn 83510931Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 83610931Sbrandon.potter@amd.com ThreadContext *tc) 83710932Sbrandon.potter@amd.com{ 83810931Sbrandon.potter@amd.com 8391999SN/A int index = 0; 8401999SN/A TypedBufferArg<typename OS::tgt_sysinfo> 8411999SN/A sysinfo(process->getSyscallArg(tc, index)); 8421999SN/A 8431999SN/A sysinfo->uptime = seconds_since_epoch; 8441999SN/A sysinfo->totalram = process->system->memSize(); 8451999SN/A sysinfo->mem_unit = 1; 8461999SN/A 84710931Sbrandon.potter@amd.com sysinfo.copyOut(tc->getMemProxy()); 8481999SN/A 8492218SN/A return 0; 8501999SN/A} 8511999SN/A 8521999SN/A/// Target chmod() handler. 8531999SN/Atemplate <class OS> 8545877Shsul@eecs.umich.eduSyscallReturn 8555877Shsul@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, Process *process, 8565877Shsul@eecs.umich.edu ThreadContext *tc) 85711851Sbrandon.potter@amd.com{ 8585877Shsul@eecs.umich.edu std::string path; 8596701Sgblack@eecs.umich.edu 8606701Sgblack@eecs.umich.edu int index = 0; 8616701Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 8626701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 8636701Sgblack@eecs.umich.edu return -EFAULT; 86410027SChris.Adeniyi-Jones@arm.com } 86510027SChris.Adeniyi-Jones@arm.com 86610027SChris.Adeniyi-Jones@arm.com uint32_t mode = process->getSyscallArg(tc, index); 86710027SChris.Adeniyi-Jones@arm.com mode_t hostMode = 0; 86810027SChris.Adeniyi-Jones@arm.com 8695877Shsul@eecs.umich.edu // XXX translate mode flags via OS::something??? 87010318Sandreas.hansson@arm.com hostMode = mode; 87110318Sandreas.hansson@arm.com 8725877Shsul@eecs.umich.edu // Adjust path for current working directory 8735877Shsul@eecs.umich.edu path = process->fullPath(path); 8745877Shsul@eecs.umich.edu 8755877Shsul@eecs.umich.edu // do the chmod 87610486Stjablin@gmail.com int result = chmod(path.c_str(), hostMode); 87710486Stjablin@gmail.com if (result < 0) 8785877Shsul@eecs.umich.edu return -errno; 87910027SChris.Adeniyi-Jones@arm.com 88010027SChris.Adeniyi-Jones@arm.com return 0; 8815877Shsul@eecs.umich.edu} 8828601Ssteve.reinhardt@amd.com 8835877Shsul@eecs.umich.edu 8845877Shsul@eecs.umich.edu/// Target fchmod() handler. 8855877Shsul@eecs.umich.edutemplate <class OS> 88610027SChris.Adeniyi-Jones@arm.comSyscallReturn 8875877Shsul@eecs.umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 8885877Shsul@eecs.umich.edu{ 8895877Shsul@eecs.umich.edu int index = 0; 89010027SChris.Adeniyi-Jones@arm.com int tgt_fd = p->getSyscallArg(tc, index); 89110027SChris.Adeniyi-Jones@arm.com uint32_t mode = p->getSyscallArg(tc, index); 89210027SChris.Adeniyi-Jones@arm.com 89310027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 89410027SChris.Adeniyi-Jones@arm.com if (!ffdp) 89510027SChris.Adeniyi-Jones@arm.com return -EBADF; 8965877Shsul@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 89710027SChris.Adeniyi-Jones@arm.com 89810027SChris.Adeniyi-Jones@arm.com mode_t hostMode = mode; 89910027SChris.Adeniyi-Jones@arm.com 90010027SChris.Adeniyi-Jones@arm.com int result = fchmod(sim_fd, hostMode); 90110027SChris.Adeniyi-Jones@arm.com 90210027SChris.Adeniyi-Jones@arm.com return (result < 0) ? -errno : 0; 90310027SChris.Adeniyi-Jones@arm.com} 90410027SChris.Adeniyi-Jones@arm.com 90510027SChris.Adeniyi-Jones@arm.com/// Target mremap() handler. 90610027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 90710027SChris.Adeniyi-Jones@arm.comSyscallReturn 90810027SChris.Adeniyi-Jones@arm.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 90910027SChris.Adeniyi-Jones@arm.com{ 9105877Shsul@eecs.umich.edu int index = 0; 9115877Shsul@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 9125877Shsul@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 91310027SChris.Adeniyi-Jones@arm.com uint64_t new_length = process->getSyscallArg(tc, index); 91410027SChris.Adeniyi-Jones@arm.com uint64_t flags = process->getSyscallArg(tc, index); 9158601Ssteve.reinhardt@amd.com uint64_t provided_address = 0; 91610027SChris.Adeniyi-Jones@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 9175877Shsul@eecs.umich.edu 9185877Shsul@eecs.umich.edu if (use_provided_address) 9191999SN/A provided_address = process->getSyscallArg(tc, index); 920378SN/A 921360SN/A if ((start % TheISA::PageBytes != 0) || 9221450SN/A (provided_address % TheISA::PageBytes != 0)) { 92311851Sbrandon.potter@amd.com warn("mremap failing: arguments not page aligned"); 9242680Sktlim@umich.edu return -EINVAL; 925360SN/A } 926360SN/A 927360SN/A new_length = roundUp(new_length, TheISA::PageBytes); 9286701Sgblack@eecs.umich.edu 9298852Sandreas.hansson@arm.com if (new_length > old_length) { 9306701Sgblack@eecs.umich.edu std::shared_ptr<MemState> mem_state = process->memState; 9316701Sgblack@eecs.umich.edu Addr mmap_end = mem_state->getMmapEnd(); 9326701Sgblack@eecs.umich.edu 9336701Sgblack@eecs.umich.edu if ((start + old_length) == mmap_end && 934360SN/A (!use_provided_address || provided_address == start)) { 9353669Sbinkertn@umich.edu // This case cannot occur when growing downward, as 9363669Sbinkertn@umich.edu // start is greater than or equal to mmap_end. 9373669Sbinkertn@umich.edu uint64_t diff = new_length - old_length; 938360SN/A process->allocateMem(mmap_end, diff); 939360SN/A mem_state->setMmapEnd(mmap_end + diff); 940360SN/A return start; 941360SN/A } else { 9422218SN/A if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 943360SN/A warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 9448706Sandreas.hansson@arm.com return -ENOMEM; 945360SN/A } else { 9461458SN/A uint64_t new_start = provided_address; 947360SN/A if (!use_provided_address) { 948360SN/A new_start = process->mmapGrowsDown() ? 949360SN/A mmap_end - new_length : mmap_end; 9505074Ssaidi@eecs.umich.edu mmap_end = process->mmapGrowsDown() ? 9515074Ssaidi@eecs.umich.edu new_start : mmap_end + new_length; 9525074Ssaidi@eecs.umich.edu mem_state->setMmapEnd(mmap_end); 95311851Sbrandon.potter@amd.com } 9545074Ssaidi@eecs.umich.edu 9555074Ssaidi@eecs.umich.edu process->pTable->remap(start, old_length, new_start); 9565074Ssaidi@eecs.umich.edu warn("mremapping to new vaddr %08p-%08p, adding %d\n", 9575074Ssaidi@eecs.umich.edu new_start, new_start + new_length, 9586701Sgblack@eecs.umich.edu new_length - old_length); 9598852Sandreas.hansson@arm.com // add on the remaining unallocated pages 9606701Sgblack@eecs.umich.edu process->allocateMem(new_start + old_length, 9615074Ssaidi@eecs.umich.edu new_length - old_length, 9626701Sgblack@eecs.umich.edu use_provided_address /* clobber */); 9635074Ssaidi@eecs.umich.edu if (use_provided_address && 9645074Ssaidi@eecs.umich.edu ((new_start + new_length > mem_state->getMmapEnd() && 9655074Ssaidi@eecs.umich.edu !process->mmapGrowsDown()) || 9665074Ssaidi@eecs.umich.edu (new_start < mem_state->getMmapEnd() && 9675208Ssaidi@eecs.umich.edu process->mmapGrowsDown()))) { 9685208Ssaidi@eecs.umich.edu // something fishy going on here, at least notify the user 9695208Ssaidi@eecs.umich.edu // @todo: increase mmap_end? 9705208Ssaidi@eecs.umich.edu warn("mmap region limit exceeded with MREMAP_FIXED\n"); 9715074Ssaidi@eecs.umich.edu } 9725074Ssaidi@eecs.umich.edu warn("returning %08p as start\n", new_start); 9735208Ssaidi@eecs.umich.edu return new_start; 9745074Ssaidi@eecs.umich.edu } 9755074Ssaidi@eecs.umich.edu } 9765074Ssaidi@eecs.umich.edu } else { 9775074Ssaidi@eecs.umich.edu if (use_provided_address && provided_address != start) 9788706Sandreas.hansson@arm.com process->pTable->remap(start, new_length, provided_address); 9795074Ssaidi@eecs.umich.edu process->pTable->unmap(start + new_length, old_length - new_length); 9805074Ssaidi@eecs.umich.edu return use_provided_address ? provided_address : start; 9815074Ssaidi@eecs.umich.edu } 9825074Ssaidi@eecs.umich.edu} 9835074Ssaidi@eecs.umich.edu 98410027SChris.Adeniyi-Jones@arm.com/// Target stat() handler. 98510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 98610027SChris.Adeniyi-Jones@arm.comSyscallReturn 98711851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 98810027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 98910027SChris.Adeniyi-Jones@arm.com{ 99010027SChris.Adeniyi-Jones@arm.com std::string path; 99110027SChris.Adeniyi-Jones@arm.com 99210027SChris.Adeniyi-Jones@arm.com int index = 0; 99310793Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 99410027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) { 99510027SChris.Adeniyi-Jones@arm.com return -EFAULT; 99610027SChris.Adeniyi-Jones@arm.com } 99710027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 99810027SChris.Adeniyi-Jones@arm.com 99910027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 100010027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 100110027SChris.Adeniyi-Jones@arm.com 100210027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 100310027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 100410027SChris.Adeniyi-Jones@arm.com 100510027SChris.Adeniyi-Jones@arm.com if (result < 0) 100610027SChris.Adeniyi-Jones@arm.com return -errno; 100710027SChris.Adeniyi-Jones@arm.com 100810027SChris.Adeniyi-Jones@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 100910027SChris.Adeniyi-Jones@arm.com 101010027SChris.Adeniyi-Jones@arm.com return 0; 101110027SChris.Adeniyi-Jones@arm.com} 101210027SChris.Adeniyi-Jones@arm.com 101310027SChris.Adeniyi-Jones@arm.com 101410027SChris.Adeniyi-Jones@arm.com/// Target stat64() handler. 101510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 101610027SChris.Adeniyi-Jones@arm.comSyscallReturn 101710027SChris.Adeniyi-Jones@arm.comstat64Func(SyscallDesc *desc, int callnum, Process *process, 101810027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 101910027SChris.Adeniyi-Jones@arm.com{ 102010027SChris.Adeniyi-Jones@arm.com std::string path; 10211999SN/A 10221999SN/A int index = 0; 10231999SN/A if (!tc->getMemProxy().tryReadString(path, 102411851Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 10252680Sktlim@umich.edu return -EFAULT; 10261999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 10276701Sgblack@eecs.umich.edu 102810931Sbrandon.potter@amd.com // Adjust path for current working directory 10296701Sgblack@eecs.umich.edu path = process->fullPath(path); 103010931Sbrandon.potter@amd.com 103110932Sbrandon.potter@amd.com#if NO_STAT64 103210931Sbrandon.potter@amd.com struct stat hostBuf; 10331999SN/A int result = stat(path.c_str(), &hostBuf); 10341999SN/A#else 10352764Sstever@eecs.umich.edu struct stat64 hostBuf; 10362064SN/A int result = stat64(path.c_str(), &hostBuf); 103710931Sbrandon.potter@amd.com#endif 10382064SN/A 10392064SN/A if (result < 0) 104010931Sbrandon.potter@amd.com return -errno; 10412064SN/A 10421999SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10431999SN/A 10442218SN/A return 0; 10451999SN/A} 104610931Sbrandon.potter@amd.com 10471999SN/A 10481999SN/A/// Target fstatat64() handler. 10491999SN/Atemplate <class OS> 10501999SN/ASyscallReturn 10511999SN/Afstatat64Func(SyscallDesc *desc, int callnum, Process *process, 1052378SN/A ThreadContext *tc) 1053360SN/A{ 10541450SN/A int index = 0; 105511851Sbrandon.potter@amd.com int dirfd = process->getSyscallArg(tc, index); 10562680Sktlim@umich.edu if (dirfd != OS::TGT_AT_FDCWD) 1057360SN/A warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 1058360SN/A 1059360SN/A std::string path; 10606701Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 10618852Sandreas.hansson@arm.com process->getSyscallArg(tc, index))) 10626701Sgblack@eecs.umich.edu return -EFAULT; 10636701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 10646701Sgblack@eecs.umich.edu 10656701Sgblack@eecs.umich.edu // Adjust path for current working directory 1066360SN/A path = process->fullPath(path); 10673669Sbinkertn@umich.edu 10683669Sbinkertn@umich.edu#if NO_STAT64 10693669Sbinkertn@umich.edu struct stat hostBuf; 1070360SN/A int result = stat(path.c_str(), &hostBuf); 1071360SN/A#else 1072360SN/A struct stat64 hostBuf; 1073360SN/A int result = stat64(path.c_str(), &hostBuf); 10741458SN/A#endif 1075360SN/A 10768706Sandreas.hansson@arm.com if (result < 0) 1077360SN/A return -errno; 10781458SN/A 1079360SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1080360SN/A 10811999SN/A return 0; 10821999SN/A} 10831999SN/A 108411851Sbrandon.potter@amd.com 10852680Sktlim@umich.edu/// Target fstat64() handler. 10861999SN/Atemplate <class OS> 10871999SN/ASyscallReturn 10881999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 10896701Sgblack@eecs.umich.edu{ 10908852Sandreas.hansson@arm.com int index = 0; 10916701Sgblack@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 10926701Sgblack@eecs.umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 10936701Sgblack@eecs.umich.edu 10946701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 10951999SN/A if (!ffdp) 10963669Sbinkertn@umich.edu return -EBADF; 10973669Sbinkertn@umich.edu int sim_fd = ffdp->getSimFD(); 10983669Sbinkertn@umich.edu 10992764Sstever@eecs.umich.edu#if NO_STAT64 11002064SN/A struct stat hostBuf; 11012064SN/A int result = fstat(sim_fd, &hostBuf); 11022064SN/A#else 11031999SN/A struct stat64 hostBuf; 11041999SN/A int result = fstat64(sim_fd, &hostBuf); 11052064SN/A#endif 11061999SN/A 11071999SN/A if (result < 0) 11081999SN/A return -errno; 11091999SN/A 11108706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 11111999SN/A 11121999SN/A return 0; 11131999SN/A} 11141999SN/A 1115378SN/A 1116360SN/A/// Target lstat() handler. 11171450SN/Atemplate <class OS> 111811851Sbrandon.potter@amd.comSyscallReturn 11192680Sktlim@umich.edulstatFunc(SyscallDesc *desc, int callnum, Process *process, 1120360SN/A ThreadContext *tc) 11216701Sgblack@eecs.umich.edu{ 112210931Sbrandon.potter@amd.com std::string path; 11236701Sgblack@eecs.umich.edu 1124360SN/A int index = 0; 112511380Salexandru.dutu@amd.com if (!tc->getMemProxy().tryReadString(path, 1126360SN/A process->getSyscallArg(tc, index))) { 112710932Sbrandon.potter@amd.com return -EFAULT; 112810931Sbrandon.potter@amd.com } 11291458SN/A Addr bufPtr = process->getSyscallArg(tc, index); 1130360SN/A 1131360SN/A // Adjust path for current working directory 113210931Sbrandon.potter@amd.com path = process->fullPath(path); 1133360SN/A 1134360SN/A struct stat hostBuf; 11351458SN/A int result = lstat(path.c_str(), &hostBuf); 1136360SN/A 113710931Sbrandon.potter@amd.com if (result < 0) 11382021SN/A return -errno; 11391458SN/A 1140360SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1141360SN/A 1142360SN/A return 0; 11431706SN/A} 11441706SN/A 11451706SN/A/// Target lstat64() handler. 114611851Sbrandon.potter@amd.comtemplate <class OS> 11472680Sktlim@umich.eduSyscallReturn 11481706SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 114911799Sbrandon.potter@amd.com ThreadContext *tc) 115011799Sbrandon.potter@amd.com{ 115111799Sbrandon.potter@amd.com std::string path; 11521706SN/A 11531706SN/A int index = 0; 11546701Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 11558852Sandreas.hansson@arm.com process->getSyscallArg(tc, index))) { 11566701Sgblack@eecs.umich.edu return -EFAULT; 11576701Sgblack@eecs.umich.edu } 11586701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11596701Sgblack@eecs.umich.edu 11601706SN/A // Adjust path for current working directory 11613669Sbinkertn@umich.edu path = process->fullPath(path); 11623669Sbinkertn@umich.edu 11633669Sbinkertn@umich.edu#if NO_STAT64 11641706SN/A struct stat hostBuf; 11651706SN/A int result = lstat(path.c_str(), &hostBuf); 11661706SN/A#else 11671706SN/A struct stat64 hostBuf; 11682218SN/A int result = lstat64(path.c_str(), &hostBuf); 11691706SN/A#endif 117011759Sbrandon.potter@amd.com 117111799Sbrandon.potter@amd.com if (result < 0) 11721706SN/A return -errno; 11731706SN/A 11741706SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11751706SN/A 11761706SN/A return 0; 11771706SN/A} 11781706SN/A 117911851Sbrandon.potter@amd.com/// Target fstat() handler. 11802680Sktlim@umich.edutemplate <class OS> 11811706SN/ASyscallReturn 11826701Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 118310931Sbrandon.potter@amd.com{ 11846701Sgblack@eecs.umich.edu int index = 0; 11851706SN/A int tgt_fd = p->getSyscallArg(tc, index); 118610932Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 118710931Sbrandon.potter@amd.com 11881706SN/A DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 11891706SN/A 11901706SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 119110931Sbrandon.potter@amd.com if (!ffdp) 11921706SN/A return -EBADF; 11931706SN/A int sim_fd = ffdp->getSimFD(); 11942218SN/A 11951706SN/A struct stat hostBuf; 119611759Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 11971706SN/A 11981706SN/A if (result < 0) 11991706SN/A return -errno; 12001706SN/A 12011706SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12021999SN/A 12031999SN/A return 0; 12041999SN/A} 120511851Sbrandon.potter@amd.com 12062680Sktlim@umich.edu 12071999SN/A/// Target statfs() handler. 12086701Sgblack@eecs.umich.edutemplate <class OS> 120910931Sbrandon.potter@amd.comSyscallReturn 121010931Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 121110932Sbrandon.potter@amd.com ThreadContext *tc) 121210931Sbrandon.potter@amd.com{ 12131999SN/A#if NO_STATFS 12141999SN/A warn("Host OS cannot support calls to statfs. Ignoring syscall"); 12158852Sandreas.hansson@arm.com#else 12166701Sgblack@eecs.umich.edu std::string path; 12176701Sgblack@eecs.umich.edu 12181999SN/A int index = 0; 12196227Snate@binkert.org if (!tc->getMemProxy().tryReadString(path, 12201999SN/A process->getSyscallArg(tc, index))) { 12212461SN/A return -EFAULT; 12228852Sandreas.hansson@arm.com } 12238852Sandreas.hansson@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 12248737Skoansin.tan@gmail.com 12251999SN/A // Adjust path for current working directory 12268852Sandreas.hansson@arm.com path = process->fullPath(path); 12278852Sandreas.hansson@arm.com 12281999SN/A struct statfs hostBuf; 12291999SN/A int result = statfs(path.c_str(), &hostBuf); 123010931Sbrandon.potter@amd.com 12311999SN/A if (result < 0) 12326227Snate@binkert.org return -errno; 12331999SN/A 12341999SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12351999SN/A#endif 12362218SN/A return 0; 12371999SN/A} 123810629Sjthestness@gmail.com 12391999SN/Atemplate <class OS> 12401999SN/ASyscallReturn 124111385Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1242360SN/A{ 12431450SN/A int index = 0; 124411851Sbrandon.potter@amd.com 124511385Sbrandon.potter@amd.com TheISA::IntReg flags = p->getSyscallArg(tc, index); 1246360SN/A TheISA::IntReg newStack = p->getSyscallArg(tc, index); 12476701Sgblack@eecs.umich.edu Addr ptidPtr = p->getSyscallArg(tc, index); 12486701Sgblack@eecs.umich.edu 12496701Sgblack@eecs.umich.edu#if THE_ISA == RISCV_ISA 125011383Sbrandon.potter@amd.com /** 125111383Sbrandon.potter@amd.com * Linux kernel 4.15 sets CLONE_BACKWARDS flag for RISC-V. 12528324Ssteve.reinhardt@amd.com * The flag defines the list of clone() arguments in the following 125310486Stjablin@gmail.com * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 1254360SN/A */ 125511385Sbrandon.potter@amd.com Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 125611385Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 12579008Sgblack@eecs.umich.edu#else 125811383Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 125911383Sbrandon.potter@amd.com Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 126011383Sbrandon.potter@amd.com#endif 126111383Sbrandon.potter@amd.com 126211383Sbrandon.potter@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 126311383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 126411383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 126511383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 126611383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 12678324Ssteve.reinhardt@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 126811383Sbrandon.potter@amd.com return -EINVAL; 126911383Sbrandon.potter@amd.com 127011383Sbrandon.potter@amd.com ThreadContext *ctc; 127111383Sbrandon.potter@amd.com if (!(ctc = p->findFreeContext())) 127211383Sbrandon.potter@amd.com fatal("clone: no spare thread context in system"); 127311383Sbrandon.potter@amd.com 127411383Sbrandon.potter@amd.com /** 127511383Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 127611383Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 127711383Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 127811383Sbrandon.potter@amd.com * constructor. 127911383Sbrandon.potter@amd.com */ 128011383Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 128111383Sbrandon.potter@amd.com pp->executable.assign(*(new std::string(p->progName()))); 128211383Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 128311383Sbrandon.potter@amd.com pp->system = p->system; 128411383Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 128511383Sbrandon.potter@amd.com pp->input.assign("stdin"); 128611383Sbrandon.potter@amd.com pp->output.assign("stdout"); 128711383Sbrandon.potter@amd.com pp->errout.assign("stderr"); 128811383Sbrandon.potter@amd.com pp->uid = p->uid(); 128911383Sbrandon.potter@amd.com pp->euid = p->euid(); 129011383Sbrandon.potter@amd.com pp->gid = p->gid(); 129111383Sbrandon.potter@amd.com pp->egid = p->egid(); 12928324Ssteve.reinhardt@amd.com 12935877Shsul@eecs.umich.edu /* Find the first free PID that's less than the maximum */ 129410486Stjablin@gmail.com std::set<int> const& pids = p->system->PIDs; 129510486Stjablin@gmail.com int temp_pid = *pids.begin(); 129611383Sbrandon.potter@amd.com do { 129711383Sbrandon.potter@amd.com temp_pid++; 129811383Sbrandon.potter@amd.com } while (pids.find(temp_pid) != pids.end()); 129911624Smichael.lebeane@amd.com if (temp_pid >= System::maxPID) 130011624Smichael.lebeane@amd.com fatal("temp_pid is too large: %d", temp_pid); 130111624Smichael.lebeane@amd.com 130211624Smichael.lebeane@amd.com pp->pid = temp_pid; 130311624Smichael.lebeane@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 130411624Smichael.lebeane@amd.com Process *cp = pp->create(); 130511624Smichael.lebeane@amd.com delete pp; 130611624Smichael.lebeane@amd.com 130711624Smichael.lebeane@amd.com Process *owner = ctc->getProcessPtr(); 130811624Smichael.lebeane@amd.com ctc->setProcessPtr(cp); 130911624Smichael.lebeane@amd.com cp->assignThreadContext(ctc->contextId()); 131011383Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 131111383Sbrandon.potter@amd.com 1312360SN/A if (flags & OS::TGT_CLONE_PARENT_SETTID) { 131311383Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 131411383Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 13158600Ssteve.reinhardt@amd.com *ptid = cp->pid(); 131611383Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 131711383Sbrandon.potter@amd.com } 131811383Sbrandon.potter@amd.com 13198600Ssteve.reinhardt@amd.com cp->initState(); 13202544SN/A p->clone(tc, ctc, cp, flags); 13212544SN/A 132211383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 132311383Sbrandon.potter@amd.com delete cp->sigchld; 132411383Sbrandon.potter@amd.com cp->sigchld = p->sigchld; 132511386Ssteve.reinhardt@amd.com } else if (flags & OS::TGT_SIGCHLD) { 132611386Ssteve.reinhardt@amd.com *cp->sigchld = true; 132711383Sbrandon.potter@amd.com } 132811383Sbrandon.potter@amd.com 132911383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 133011383Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 133111383Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 133211383Sbrandon.potter@amd.com *ctid = cp->pid(); 133311383Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 133411383Sbrandon.potter@amd.com } 133511383Sbrandon.potter@amd.com 133611383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 133711383Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 133811383Sbrandon.potter@amd.com 133911383Sbrandon.potter@amd.com ctc->clearArchRegs(); 134011383Sbrandon.potter@amd.com 134111383Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 13428600Ssteve.reinhardt@amd.com TheISA::copyMiscRegs(tc, ctc); 13436672Sgblack@eecs.umich.edu#elif THE_ISA == SPARC_ISA 13448600Ssteve.reinhardt@amd.com TheISA::copyRegs(tc, ctc); 134511383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 134611383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 134711383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 13488601Ssteve.reinhardt@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 13492544SN/A ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 135011383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 135111383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 135211383Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 135311383Sbrandon.potter@amd.com for (int y = 8; y < 32; y++) 135411383Sbrandon.potter@amd.com ctc->setIntReg(y, tc->readIntReg(y)); 135511383Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA or THE_ISA == RISCV_ISA 135611383Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 135711383Sbrandon.potter@amd.com#endif 135811383Sbrandon.potter@amd.com 135911383Sbrandon.potter@amd.com#if THE_ISA == X86_ISA 136011383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_SETTLS) { 136111383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 136211383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 136311383Sbrandon.potter@amd.com } 136411383Sbrandon.potter@amd.com#endif 136511383Sbrandon.potter@amd.com 136611383Sbrandon.potter@amd.com if (newStack) 136711383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::StackPointerReg, newStack); 136811383Sbrandon.potter@amd.com 136911383Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 137011383Sbrandon.potter@amd.com 137111383Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 137211383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 137311383Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 137411383Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 137511383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 137611383Sbrandon.potter@amd.com#endif 137711383Sbrandon.potter@amd.com 137811383Sbrandon.potter@amd.com ctc->pcState(tc->nextInstAddr()); 137911383Sbrandon.potter@amd.com ctc->activate(); 138011383Sbrandon.potter@amd.com 138111383Sbrandon.potter@amd.com return cp->pid(); 138211392Sbrandon.potter@amd.com} 138311392Sbrandon.potter@amd.com 138411392Sbrandon.potter@amd.com/// Target fstatfs() handler. 138511392Sbrandon.potter@amd.comtemplate <class OS> 138611392Sbrandon.potter@amd.comSyscallReturn 138711392Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 138811392Sbrandon.potter@amd.com{ 138911392Sbrandon.potter@amd.com int index = 0; 139011392Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 139111392Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 139211392Sbrandon.potter@amd.com 139311392Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 139411392Sbrandon.potter@amd.com if (!ffdp) 139511392Sbrandon.potter@amd.com return -EBADF; 139611392Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 139711392Sbrandon.potter@amd.com 139811392Sbrandon.potter@amd.com struct statfs hostBuf; 139911392Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 140011392Sbrandon.potter@amd.com 140111392Sbrandon.potter@amd.com if (result < 0) 140211392Sbrandon.potter@amd.com return -errno; 140311392Sbrandon.potter@amd.com 140411392Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 140511392Sbrandon.potter@amd.com 140611392Sbrandon.potter@amd.com return 0; 140711383Sbrandon.potter@amd.com} 140811383Sbrandon.potter@amd.com 140911383Sbrandon.potter@amd.com 141011383Sbrandon.potter@amd.com/// Target writev() handler. 141111383Sbrandon.potter@amd.comtemplate <class OS> 14121458SN/ASyscallReturn 1413360SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1414360SN/A{ 141511593Santhony.gutierrez@amd.com int index = 0; 141611593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 141711851Sbrandon.potter@amd.com 141811593Santhony.gutierrez@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 141911593Santhony.gutierrez@amd.com if (!hbfdp) 142011593Santhony.gutierrez@amd.com return -EBADF; 142111593Santhony.gutierrez@amd.com int sim_fd = hbfdp->getSimFD(); 142211593Santhony.gutierrez@amd.com 142311593Santhony.gutierrez@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 142411593Santhony.gutierrez@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 142511593Santhony.gutierrez@amd.com size_t count = p->getSyscallArg(tc, index); 142611593Santhony.gutierrez@amd.com struct iovec hiov[count]; 142711593Santhony.gutierrez@amd.com for (size_t i = 0; i < count; ++i) { 142811593Santhony.gutierrez@amd.com typename OS::tgt_iovec tiov; 142911593Santhony.gutierrez@amd.com 143011593Santhony.gutierrez@amd.com prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 143111593Santhony.gutierrez@amd.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 143211594Santhony.gutierrez@amd.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 143311593Santhony.gutierrez@amd.com hiov[i].iov_base = new char [hiov[i].iov_len]; 143411593Santhony.gutierrez@amd.com prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 143511593Santhony.gutierrez@amd.com hiov[i].iov_len); 143611593Santhony.gutierrez@amd.com } 143711385Sbrandon.potter@amd.com 143811385Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 143911385Sbrandon.potter@amd.com 144011851Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) 144111385Sbrandon.potter@amd.com delete [] (char *)hiov[i].iov_base; 144211385Sbrandon.potter@amd.com 144311385Sbrandon.potter@amd.com if (result < 0) 144411385Sbrandon.potter@amd.com return -errno; 144511385Sbrandon.potter@amd.com 144611385Sbrandon.potter@amd.com return result; 144711385Sbrandon.potter@amd.com} 144811851Sbrandon.potter@amd.com 144911385Sbrandon.potter@amd.com/// Real mmap handler. 145011385Sbrandon.potter@amd.comtemplate <class OS> 145111385Sbrandon.potter@amd.comSyscallReturn 145211385Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1453378SN/A bool is_mmap2) 1454360SN/A{ 14551450SN/A int index = 0; 145611851Sbrandon.potter@amd.com Addr start = p->getSyscallArg(tc, index); 145711851Sbrandon.potter@amd.com uint64_t length = p->getSyscallArg(tc, index); 1458360SN/A int prot = p->getSyscallArg(tc, index); 14596701Sgblack@eecs.umich.edu int tgt_flags = p->getSyscallArg(tc, index); 14606701Sgblack@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 14616701Sgblack@eecs.umich.edu int offset = p->getSyscallArg(tc, index); 1462360SN/A 1463360SN/A if (is_mmap2) 14642064SN/A offset *= TheISA::PageBytes; 14655877Shsul@eecs.umich.edu 14662064SN/A if (start & (TheISA::PageBytes - 1) || 14678737Skoansin.tan@gmail.com offset & (TheISA::PageBytes - 1) || 14688737Skoansin.tan@gmail.com (tgt_flags & OS::TGT_MAP_PRIVATE && 14692064SN/A tgt_flags & OS::TGT_MAP_SHARED) || 1470360SN/A (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 14715877Shsul@eecs.umich.edu !(tgt_flags & OS::TGT_MAP_SHARED)) || 14725877Shsul@eecs.umich.edu !length) { 14735877Shsul@eecs.umich.edu return -EINVAL; 14748737Skoansin.tan@gmail.com } 14758737Skoansin.tan@gmail.com 14765877Shsul@eecs.umich.edu if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 14775877Shsul@eecs.umich.edu // With shared mmaps, there are two cases to consider: 14782064SN/A // 1) anonymous: writes should modify the mapping and this should be 147910794Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 148010794Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 14812064SN/A // structure which maintains information about the which virtual 1482360SN/A // memory areas are shared. If that structure existed, it would be 1483360SN/A // possible to make the translations point to the same frames. 14848706Sandreas.hansson@arm.com // 2) file-backed: writes should modify the mapping and the file 14851458SN/A // which is backed by the mapping. The shared mapping problem is the 1486360SN/A // same as what was mentioned about the anonymous mappings. For 1487360SN/A // file-backed mappings, the writes to the file are difficult 148810796Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 148910796Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 149010796Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 149111851Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 149210796Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 149310796Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 149410796Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 149510796Sbrandon.potter@amd.com // a call to msync, but that again would require more information 149610796Sbrandon.potter@amd.com // than we currently maintain. 149710796Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 149810796Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 149910796Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 150010796Sbrandon.potter@amd.com } 150110796Sbrandon.potter@amd.com 150210796Sbrandon.potter@amd.com length = roundUp(length, TheISA::PageBytes); 150310796Sbrandon.potter@amd.com 150410796Sbrandon.potter@amd.com int sim_fd = -1; 150510796Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 150610796Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 150711337SMichael.Lebeane@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 150811337SMichael.Lebeane@amd.com 150911337SMichael.Lebeane@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 151011851Sbrandon.potter@amd.com if (dfdp) { 151111337SMichael.Lebeane@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 151211337SMichael.Lebeane@amd.com return emul_driver->mmap(p, tc, start, length, prot, 151311337SMichael.Lebeane@amd.com tgt_flags, tgt_fd, offset); 151411337SMichael.Lebeane@amd.com } 151511337SMichael.Lebeane@amd.com 151611337SMichael.Lebeane@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 151711337SMichael.Lebeane@amd.com if (!ffdp) 151811337SMichael.Lebeane@amd.com return -EBADF; 151911337SMichael.Lebeane@amd.com sim_fd = ffdp->getSimFD(); 152011337SMichael.Lebeane@amd.com 152111337SMichael.Lebeane@amd.com pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 152211337SMichael.Lebeane@amd.com sim_fd, offset); 152311337SMichael.Lebeane@amd.com 1524378SN/A if (pmap == (decltype(pmap))-1) { 1525360SN/A warn("mmap: failed to map file into host address space"); 15261450SN/A return -errno; 152711851Sbrandon.potter@amd.com } 152811851Sbrandon.potter@amd.com } 1529360SN/A 15306701Sgblack@eecs.umich.edu // Extend global mmap region if necessary. Note that we ignore the 15316701Sgblack@eecs.umich.edu // start address unless MAP_FIXED is specified. 1532360SN/A if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 153310796Sbrandon.potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 1534360SN/A Addr mmap_end = mem_state->getMmapEnd(); 15356109Ssanchezd@stanford.edu 15366109Ssanchezd@stanford.edu start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1537360SN/A mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 15388706Sandreas.hansson@arm.com 1539360SN/A mem_state->setMmapEnd(mmap_end); 15401458SN/A } 1541360SN/A 1542360SN/A DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1543360SN/A start, start + length - 1); 15441999SN/A 15451999SN/A // We only allow mappings to overwrite existing mappings if 15461999SN/A // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 154711851Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 15482680Sktlim@umich.edu int clobber = tgt_flags & OS::TGT_MAP_FIXED; 15491999SN/A if (clobber) { 15501999SN/A for (auto tc : p->system->threadContexts) { 15511999SN/A // If we might be overwriting old mappings, we need to 15526701Sgblack@eecs.umich.edu // invalidate potentially stale mappings out of the TLBs. 15538852Sandreas.hansson@arm.com tc->getDTBPtr()->flushAll(); 15546701Sgblack@eecs.umich.edu tc->getITBPtr()->flushAll(); 15556701Sgblack@eecs.umich.edu } 15566701Sgblack@eecs.umich.edu } 15571999SN/A 15586701Sgblack@eecs.umich.edu // Allocate physical memory and map it in. If the page table is already 15596701Sgblack@eecs.umich.edu // mapped and clobber is not set, the simulator will issue throw a 15608706Sandreas.hansson@arm.com // fatal and bail out of the simulation. 15611999SN/A p->allocateMem(start, length, clobber); 15621999SN/A 15631999SN/A // Transfer content into target address space. 15641999SN/A SETranslatingPortProxy &tp = tc->getMemProxy(); 15658737Skoansin.tan@gmail.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 15668737Skoansin.tan@gmail.com // In general, we should zero the mapped area for anonymous mappings, 15671999SN/A // with something like: 15683669Sbinkertn@umich.edu // tp.memsetBlob(start, 0, length); 15693669Sbinkertn@umich.edu // However, given that we don't support sparse mappings, and 15703669Sbinkertn@umich.edu // some applications can map a couple of gigabytes of space 15713669Sbinkertn@umich.edu // (intending sparse usage), that can get painfully expensive. 15721999SN/A // Fortunately, since we don't properly implement munmap either, 15731999SN/A // there's no danger of remapping used memory, so for now all 15741999SN/A // newly mapped memory should already be zeroed so we can skip it. 15751999SN/A } else { 15761999SN/A // It is possible to mmap an area larger than a file, however 15771999SN/A // accessing unmapped portions the system triggers a "Bus error" 15781999SN/A // on the host. We must know when to stop copying the file from 1579378SN/A // the host into the target address space. 1580360SN/A struct stat file_stat; 15811450SN/A if (fstat(sim_fd, &file_stat) > 0) 158211851Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 15832680Sktlim@umich.edu 1584360SN/A // Copy the portion of the file that is resident. This requires 15856701Sgblack@eecs.umich.edu // checking both the mmap size and the filesize that we are 15866701Sgblack@eecs.umich.edu // trying to mmap into this space; the mmap size also depends 15876701Sgblack@eecs.umich.edu // on the specified offset into the file. 1588360SN/A uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 15893670Sbinkertn@umich.edu length); 15903670Sbinkertn@umich.edu tp.writeBlob(start, pmap, size); 1591360SN/A 1592360SN/A // Cleanup the mmap region before exiting this function. 1593360SN/A munmap(pmap, length); 1594360SN/A 1595360SN/A // Maintain the symbol table for dynamic executables. 1596360SN/A // The loader will call mmap to map the images into its address 1597360SN/A // space and we intercept that here. We can verify that we are 1598360SN/A // executing inside the loader by checking the program counter value. 1599360SN/A // XXX: with multiprogrammed workloads or multi-node configurations, 1600360SN/A // this will not work since there is a single global symbol table. 1601360SN/A ObjectFile *interpreter = p->getInterpreter(); 1602360SN/A if (interpreter) { 1603360SN/A Addr text_start = interpreter->textBase(); 1604360SN/A Addr text_end = text_start + interpreter->textSize(); 1605360SN/A 1606360SN/A Addr pc = tc->pcState().pc(); 1607360SN/A 16083670Sbinkertn@umich.edu if (pc >= text_start && pc < text_end) { 16093670Sbinkertn@umich.edu std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 161010796Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 16118737Skoansin.tan@gmail.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 16128737Skoansin.tan@gmail.com 16133670Sbinkertn@umich.edu if (lib) { 16143670Sbinkertn@umich.edu lib->loadAllSymbols(debugSymbolTable, 16153670Sbinkertn@umich.edu lib->textBase(), start); 16163670Sbinkertn@umich.edu } 16173670Sbinkertn@umich.edu } 16183670Sbinkertn@umich.edu } 16193670Sbinkertn@umich.edu 16203670Sbinkertn@umich.edu // Note that we do not zero out the remainder of the mapping. This 16213670Sbinkertn@umich.edu // is done by a real system, but it probably will not affect 16223670Sbinkertn@umich.edu // execution (hopefully). 16233670Sbinkertn@umich.edu } 16243670Sbinkertn@umich.edu 16253670Sbinkertn@umich.edu return start; 16268706Sandreas.hansson@arm.com} 1627360SN/A 16281458SN/Atemplate <class OS> 1629360SN/ASyscallReturn 1630360SN/Apwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16316683Stjones1@inf.ed.ac.uk{ 16326683Stjones1@inf.ed.ac.uk int index = 0; 16336683Stjones1@inf.ed.ac.uk int tgt_fd = p->getSyscallArg(tc, index); 163411851Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 163511851Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 16366683Stjones1@inf.ed.ac.uk int offset = p->getSyscallArg(tc, index); 16376701Sgblack@eecs.umich.edu 16386701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16396683Stjones1@inf.ed.ac.uk if (!ffdp) 16406683Stjones1@inf.ed.ac.uk return -EBADF; 16417823Ssteve.reinhardt@amd.com int sim_fd = ffdp->getSimFD(); 16426683Stjones1@inf.ed.ac.uk 16436683Stjones1@inf.ed.ac.uk BufferArg bufArg(bufPtr, nbytes); 16446683Stjones1@inf.ed.ac.uk bufArg.copyIn(tc->getMemProxy()); 16456683Stjones1@inf.ed.ac.uk 16466683Stjones1@inf.ed.ac.uk int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 16476683Stjones1@inf.ed.ac.uk 16488737Skoansin.tan@gmail.com return (bytes_written == -1) ? -errno : bytes_written; 16496683Stjones1@inf.ed.ac.uk} 16506683Stjones1@inf.ed.ac.uk 16518706Sandreas.hansson@arm.com/// Target mmap() handler. 16526683Stjones1@inf.ed.ac.uktemplate <class OS> 16536683Stjones1@inf.ed.ac.ukSyscallReturn 16546683Stjones1@inf.ed.ac.ukmmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16556683Stjones1@inf.ed.ac.uk{ 16562553SN/A return mmapImpl<OS>(desc, num, p, tc, false); 16576684Stjones1@inf.ed.ac.uk} 16586684Stjones1@inf.ed.ac.uk 16596684Stjones1@inf.ed.ac.uk/// Target mmap2() handler. 166011851Sbrandon.potter@amd.comtemplate <class OS> 16616684Stjones1@inf.ed.ac.ukSyscallReturn 16626684Stjones1@inf.ed.ac.ukmmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 166310796Sbrandon.potter@amd.com{ 16646684Stjones1@inf.ed.ac.uk return mmapImpl<OS>(desc, num, p, tc, true); 16656684Stjones1@inf.ed.ac.uk} 16666701Sgblack@eecs.umich.edu 16676701Sgblack@eecs.umich.edu/// Target getrlimit() handler. 166811321Ssteve.reinhardt@amd.comtemplate <class OS> 16696684Stjones1@inf.ed.ac.ukSyscallReturn 16708737Skoansin.tan@gmail.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 16718852Sandreas.hansson@arm.com ThreadContext *tc) 16728852Sandreas.hansson@arm.com{ 16736684Stjones1@inf.ed.ac.uk int index = 0; 16746684Stjones1@inf.ed.ac.uk unsigned resource = process->getSyscallArg(tc, index); 16756684Stjones1@inf.ed.ac.uk TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 16762553SN/A 16772553SN/A switch (resource) { 16781354SN/A case OS::TGT_RLIMIT_STACK: 1679 // max stack size in bytes: make up a number (8MB for now) 1680 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1681 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1682 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1683 break; 1684 1685 case OS::TGT_RLIMIT_DATA: 1686 // max data segment size in bytes: make up a number 1687 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1688 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1689 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1690 break; 1691 1692 default: 1693 warn("getrlimit: unimplemented resource %d", resource); 1694 return -EINVAL; 1695 break; 1696 } 1697 1698 rlp.copyOut(tc->getMemProxy()); 1699 return 0; 1700} 1701 1702template <class OS> 1703SyscallReturn 1704prlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1705 ThreadContext *tc) 1706{ 1707 int index = 0; 1708 if (process->getSyscallArg(tc, index) != 0) 1709 { 1710 warn("prlimit: ignoring rlimits for nonzero pid"); 1711 return -EPERM; 1712 } 1713 int resource = process->getSyscallArg(tc, index); 1714 Addr n = process->getSyscallArg(tc, index); 1715 if (n != 0) 1716 warn("prlimit: ignoring new rlimit"); 1717 Addr o = process->getSyscallArg(tc, index); 1718 if (o != 0) 1719 { 1720 TypedBufferArg<typename OS::rlimit> rlp(o); 1721 switch (resource) { 1722 case OS::TGT_RLIMIT_STACK: 1723 // max stack size in bytes: make up a number (8MB for now) 1724 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1725 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1726 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1727 break; 1728 case OS::TGT_RLIMIT_DATA: 1729 // max data segment size in bytes: make up a number 1730 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 1731 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1732 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1733 default: 1734 warn("prlimit: unimplemented resource %d", resource); 1735 return -EINVAL; 1736 break; 1737 } 1738 rlp.copyOut(tc->getMemProxy()); 1739 } 1740 return 0; 1741} 1742 1743/// Target clock_gettime() function. 1744template <class OS> 1745SyscallReturn 1746clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1747{ 1748 int index = 1; 1749 //int clk_id = p->getSyscallArg(tc, index); 1750 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1751 1752 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 1753 tp->tv_sec += seconds_since_epoch; 1754 tp->tv_sec = TheISA::htog(tp->tv_sec); 1755 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 1756 1757 tp.copyOut(tc->getMemProxy()); 1758 1759 return 0; 1760} 1761 1762/// Target clock_getres() function. 1763template <class OS> 1764SyscallReturn 1765clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1766{ 1767 int index = 1; 1768 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1769 1770 // Set resolution at ns, which is what clock_gettime() returns 1771 tp->tv_sec = 0; 1772 tp->tv_nsec = 1; 1773 1774 tp.copyOut(tc->getMemProxy()); 1775 1776 return 0; 1777} 1778 1779/// Target gettimeofday() handler. 1780template <class OS> 1781SyscallReturn 1782gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 1783 ThreadContext *tc) 1784{ 1785 int index = 0; 1786 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1787 1788 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1789 tp->tv_sec += seconds_since_epoch; 1790 tp->tv_sec = TheISA::htog(tp->tv_sec); 1791 tp->tv_usec = TheISA::htog(tp->tv_usec); 1792 1793 tp.copyOut(tc->getMemProxy()); 1794 1795 return 0; 1796} 1797 1798 1799/// Target utimes() handler. 1800template <class OS> 1801SyscallReturn 1802utimesFunc(SyscallDesc *desc, int callnum, Process *process, 1803 ThreadContext *tc) 1804{ 1805 std::string path; 1806 1807 int index = 0; 1808 if (!tc->getMemProxy().tryReadString(path, 1809 process->getSyscallArg(tc, index))) { 1810 return -EFAULT; 1811 } 1812 1813 TypedBufferArg<typename OS::timeval [2]> 1814 tp(process->getSyscallArg(tc, index)); 1815 tp.copyIn(tc->getMemProxy()); 1816 1817 struct timeval hostTimeval[2]; 1818 for (int i = 0; i < 2; ++i) { 1819 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 1820 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 1821 } 1822 1823 // Adjust path for current working directory 1824 path = process->fullPath(path); 1825 1826 int result = utimes(path.c_str(), hostTimeval); 1827 1828 if (result < 0) 1829 return -errno; 1830 1831 return 0; 1832} 1833 1834template <class OS> 1835SyscallReturn 1836execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1837{ 1838 desc->setFlags(0); 1839 1840 int index = 0; 1841 std::string path; 1842 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 1843 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 1844 return -EFAULT; 1845 1846 if (access(path.c_str(), F_OK) == -1) 1847 return -EACCES; 1848 1849 auto read_in = [](std::vector<std::string> & vect, 1850 SETranslatingPortProxy & mem_proxy, 1851 Addr mem_loc) 1852 { 1853 for (int inc = 0; ; inc++) { 1854 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 1855 b.copyIn(mem_proxy); 1856 1857 if (!*(Addr*)b.bufferPtr()) 1858 break; 1859 1860 vect.push_back(std::string()); 1861 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 1862 } 1863 }; 1864 1865 /** 1866 * Note that ProcessParams is generated by swig and there are no other 1867 * examples of how to create anything but this default constructor. The 1868 * fields are manually initialized instead of passing parameters to the 1869 * constructor. 1870 */ 1871 ProcessParams *pp = new ProcessParams(); 1872 pp->executable = path; 1873 Addr argv_mem_loc = p->getSyscallArg(tc, index); 1874 read_in(pp->cmd, mem_proxy, argv_mem_loc); 1875 Addr envp_mem_loc = p->getSyscallArg(tc, index); 1876 read_in(pp->env, mem_proxy, envp_mem_loc); 1877 pp->uid = p->uid(); 1878 pp->egid = p->egid(); 1879 pp->euid = p->euid(); 1880 pp->gid = p->gid(); 1881 pp->ppid = p->ppid(); 1882 pp->pid = p->pid(); 1883 pp->input.assign("cin"); 1884 pp->output.assign("cout"); 1885 pp->errout.assign("cerr"); 1886 pp->cwd.assign(p->getcwd()); 1887 pp->system = p->system; 1888 /** 1889 * Prevent process object creation with identical PIDs (which will trip 1890 * a fatal check in Process constructor). The execve call is supposed to 1891 * take over the currently executing process' identity but replace 1892 * whatever it is doing with a new process image. Instead of hijacking 1893 * the process object in the simulator, we create a new process object 1894 * and bind to the previous process' thread below (hijacking the thread). 1895 */ 1896 p->system->PIDs.erase(p->pid()); 1897 Process *new_p = pp->create(); 1898 delete pp; 1899 1900 /** 1901 * Work through the file descriptor array and close any files marked 1902 * close-on-exec. 1903 */ 1904 new_p->fds = p->fds; 1905 for (int i = 0; i < new_p->fds->getSize(); i++) { 1906 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 1907 if (fdep && fdep->getCOE()) 1908 new_p->fds->closeFDEntry(i); 1909 } 1910 1911 *new_p->sigchld = true; 1912 1913 delete p; 1914 tc->clearArchRegs(); 1915 tc->setProcessPtr(new_p); 1916 new_p->assignThreadContext(tc->contextId()); 1917 new_p->initState(); 1918 tc->activate(); 1919 TheISA::PCState pcState = tc->pcState(); 1920 tc->setNPC(pcState.instAddr()); 1921 1922 desc->setFlags(SyscallDesc::SuppressReturnValue); 1923 return 0; 1924} 1925 1926/// Target getrusage() function. 1927template <class OS> 1928SyscallReturn 1929getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 1930 ThreadContext *tc) 1931{ 1932 int index = 0; 1933 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 1934 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1935 1936 rup->ru_utime.tv_sec = 0; 1937 rup->ru_utime.tv_usec = 0; 1938 rup->ru_stime.tv_sec = 0; 1939 rup->ru_stime.tv_usec = 0; 1940 rup->ru_maxrss = 0; 1941 rup->ru_ixrss = 0; 1942 rup->ru_idrss = 0; 1943 rup->ru_isrss = 0; 1944 rup->ru_minflt = 0; 1945 rup->ru_majflt = 0; 1946 rup->ru_nswap = 0; 1947 rup->ru_inblock = 0; 1948 rup->ru_oublock = 0; 1949 rup->ru_msgsnd = 0; 1950 rup->ru_msgrcv = 0; 1951 rup->ru_nsignals = 0; 1952 rup->ru_nvcsw = 0; 1953 rup->ru_nivcsw = 0; 1954 1955 switch (who) { 1956 case OS::TGT_RUSAGE_SELF: 1957 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1958 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 1959 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 1960 break; 1961 1962 case OS::TGT_RUSAGE_CHILDREN: 1963 // do nothing. We have no child processes, so they take no time. 1964 break; 1965 1966 default: 1967 // don't really handle THREAD or CHILDREN, but just warn and 1968 // plow ahead 1969 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1970 who); 1971 } 1972 1973 rup.copyOut(tc->getMemProxy()); 1974 1975 return 0; 1976} 1977 1978/// Target times() function. 1979template <class OS> 1980SyscallReturn 1981timesFunc(SyscallDesc *desc, int callnum, Process *process, 1982 ThreadContext *tc) 1983{ 1984 int index = 0; 1985 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 1986 1987 // Fill in the time structure (in clocks) 1988 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 1989 bufp->tms_utime = clocks; 1990 bufp->tms_stime = 0; 1991 bufp->tms_cutime = 0; 1992 bufp->tms_cstime = 0; 1993 1994 // Convert to host endianness 1995 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 1996 1997 // Write back 1998 bufp.copyOut(tc->getMemProxy()); 1999 2000 // Return clock ticks since system boot 2001 return clocks; 2002} 2003 2004/// Target time() function. 2005template <class OS> 2006SyscallReturn 2007timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 2008{ 2009 typename OS::time_t sec, usec; 2010 getElapsedTimeMicro(sec, usec); 2011 sec += seconds_since_epoch; 2012 2013 int index = 0; 2014 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2015 if (taddr != 0) { 2016 typename OS::time_t t = sec; 2017 t = TheISA::htog(t); 2018 SETranslatingPortProxy &p = tc->getMemProxy(); 2019 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2020 } 2021 return sec; 2022} 2023 2024template <class OS> 2025SyscallReturn 2026tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 2027{ 2028 int index = 0; 2029 int tgid = process->getSyscallArg(tc, index); 2030 int tid = process->getSyscallArg(tc, index); 2031 int sig = process->getSyscallArg(tc, index); 2032 2033 /** 2034 * This system call is intended to allow killing a specific thread 2035 * within an arbitrary thread group if sanctioned with permission checks. 2036 * It's usually true that threads share the termination signal as pointed 2037 * out by the pthread_kill man page and this seems to be the intended 2038 * usage. Due to this being an emulated environment, assume the following: 2039 * Threads are allowed to call tgkill because the EUID for all threads 2040 * should be the same. There is no signal handling mechanism for kernel 2041 * registration of signal handlers since signals are poorly supported in 2042 * emulation mode. Since signal handlers cannot be registered, all 2043 * threads within in a thread group must share the termination signal. 2044 * We never exhaust PIDs so there's no chance of finding the wrong one 2045 * due to PID rollover. 2046 */ 2047 2048 System *sys = tc->getSystemPtr(); 2049 Process *tgt_proc = nullptr; 2050 for (int i = 0; i < sys->numContexts(); i++) { 2051 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2052 if (temp->pid() == tid) { 2053 tgt_proc = temp; 2054 break; 2055 } 2056 } 2057 2058 if (sig != 0 || sig != OS::TGT_SIGABRT) 2059 return -EINVAL; 2060 2061 if (tgt_proc == nullptr) 2062 return -ESRCH; 2063 2064 if (tgid != -1 && tgt_proc->tgid() != tgid) 2065 return -ESRCH; 2066 2067 if (sig == OS::TGT_SIGABRT) 2068 exitGroupFunc(desc, 252, process, tc); 2069 2070 return 0; 2071} 2072 2073 2074#endif // __SIM_SYSCALL_EMUL_HH__ 2075