syscall_emul.hh revision 12235
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 52360SN/A#else 53360SN/A#define NO_STAT64 0 54360SN/A#endif 55360SN/A 56360SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 57360SN/A defined(__FreeBSD__) || defined(__NetBSD__)) 581809SN/A#define NO_STATFS 1 595543Ssaidi@eecs.umich.edu#else 6011392Sbrandon.potter@amd.com#define NO_STATFS 0 611809SN/A#endif 6211392Sbrandon.potter@amd.com 6311383Sbrandon.potter@amd.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 643113Sgblack@eecs.umich.edu defined(__FreeBSD__) || defined(__NetBSD__)) 6511759Sbrandon.potter@amd.com#define NO_FALLOCATE 1 668229Snate@binkert.org#else 678229Snate@binkert.org#define NO_FALLOCATE 0 6811594Santhony.gutierrez@amd.com#endif 697075Snate@binkert.org 708229Snate@binkert.org/// 717075Snate@binkert.org/// @file syscall_emul.hh 72360SN/A/// 732474SN/A/// This file defines objects used to emulate syscalls from the target 745543Ssaidi@eecs.umich.edu/// application on the host machine. 7511392Sbrandon.potter@amd.com 762462SN/A#ifdef __CYGWIN32__ 771354SN/A#include <sys/fcntl.h> 786216Snate@binkert.org 796658Snate@binkert.org#endif 802474SN/A#include <fcntl.h> 812680Sktlim@umich.edu#include <sys/mman.h> 828229Snate@binkert.org#include <sys/stat.h> 837678Sgblack@eecs.umich.edu#if (NO_STATFS == 0) 8410496Ssteve.reinhardt@amd.com#include <sys/statfs.h> 858229Snate@binkert.org#else 8611794Sbrandon.potter@amd.com#include <sys/mount.h> 8710497Ssteve.reinhardt@amd.com#endif 8811794Sbrandon.potter@amd.com#include <sys/time.h> 896640Svince@csl.cornell.edu#include <sys/uio.h> 90360SN/A#include <unistd.h> 9111794Sbrandon.potter@amd.com 92360SN/A#include <cerrno> 93360SN/A#include <memory> 94360SN/A#include <string> 95360SN/A 96360SN/A#include "arch/utility.hh" 97360SN/A#include "base/intmath.hh" 98360SN/A#include "base/loader/object_file.hh" 99360SN/A#include "base/misc.hh" 100360SN/A#include "base/trace.hh" 101360SN/A#include "base/types.hh" 102378SN/A#include "config/the_isa.hh" 1031706SN/A#include "cpu/base.hh" 1043114Sgblack@eecs.umich.edu#include "cpu/thread_context.hh" 105378SN/A#include "mem/page_table.hh" 106378SN/A#include "params/Process.hh" 107378SN/A#include "sim/emul_driver.hh" 108378SN/A#include "sim/futex_map.hh" 109378SN/A#include "sim/process.hh" 1101706SN/A#include "sim/syscall_debug_macros.hh" 1113114Sgblack@eecs.umich.edu#include "sim/syscall_desc.hh" 112360SN/A#include "sim/syscall_emul_buf.hh" 11311760Sbrandon.potter@amd.com#include "sim/syscall_return.hh" 11411760Sbrandon.potter@amd.com 11511760Sbrandon.potter@amd.com////////////////////////////////////////////////////////////////////// 11611760Sbrandon.potter@amd.com// 1176109Ssanchezd@stanford.edu// The following emulation functions are generic enough that they 1181706SN/A// don't need to be recompiled for different emulated OS's. They are 1193114Sgblack@eecs.umich.edu// defined in sim/syscall_emul.cc. 120378SN/A// 1216109Ssanchezd@stanford.edu////////////////////////////////////////////////////////////////////// 1226109Ssanchezd@stanford.edu 1236109Ssanchezd@stanford.edu 1246109Ssanchezd@stanford.edu/// Handler for unimplemented syscalls that we haven't thought about. 125378SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1261706SN/A Process *p, ThreadContext *tc); 1273114Sgblack@eecs.umich.edu 128378SN/A/// Handler for unimplemented syscalls that we never intend to 1295748SSteve.Reinhardt@amd.com/// implement (signal handling, etc.) and should not affect the correct 1305748SSteve.Reinhardt@amd.com/// behavior of the program. Print a warning only if the appropriate 1315748SSteve.Reinhardt@amd.com/// trace flag is enabled. Return success to the target program. 132378SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 133378SN/A Process *p, ThreadContext *tc); 1341706SN/A 1353114Sgblack@eecs.umich.edu// Target fallocateFunc() handler. 136378SN/ASyscallReturn fallocateFunc(SyscallDesc *desc, int num, 137378SN/A Process *p, ThreadContext *tc); 1381706SN/A 1393114Sgblack@eecs.umich.edu/// Target exit() handler: terminate current context. 140378SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 141378SN/A Process *p, ThreadContext *tc); 1421706SN/A 1433114Sgblack@eecs.umich.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 144378SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 145378SN/A Process *p, ThreadContext *tc); 1461706SN/A 1473114Sgblack@eecs.umich.edu/// Target set_tid_address() handler. 148378SN/ASyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1494118Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 1504118Sgblack@eecs.umich.edu 1514118Sgblack@eecs.umich.edu/// Target getpagesize() handler. 1524118Sgblack@eecs.umich.eduSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 153378SN/A Process *p, ThreadContext *tc); 1541706SN/A 1553114Sgblack@eecs.umich.edu/// Target brk() handler: set brk address. 156378SN/ASyscallReturn brkFunc(SyscallDesc *desc, int num, 157378SN/A Process *p, ThreadContext *tc); 1581706SN/A 1593114Sgblack@eecs.umich.edu/// Target close() handler. 160360SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 1615513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 1625513SMichael.Adler@intel.com 1635513SMichael.Adler@intel.com// Target read() handler. 1645513SMichael.Adler@intel.comSyscallReturn readFunc(SyscallDesc *desc, int num, 16510203SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 16610203SAli.Saidi@ARM.com 16710203SAli.Saidi@ARM.com/// Target write() handler. 16810203SAli.Saidi@ARM.comSyscallReturn writeFunc(SyscallDesc *desc, int num, 1695513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 1705513SMichael.Adler@intel.com 1715513SMichael.Adler@intel.com/// Target lseek() handler. 172511SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 17310633Smichaelupton@gmail.com Process *p, ThreadContext *tc); 17410633Smichaelupton@gmail.com 17510633Smichaelupton@gmail.com/// Target _llseek() handler. 1761706SN/ASyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1773114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 178511SN/A 1795513SMichael.Adler@intel.com/// Target munmap() handler. 1805513SMichael.Adler@intel.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 1815513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 1825513SMichael.Adler@intel.com 183511SN/A/// Target gethostname() handler. 1841706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 1853114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 1861706SN/A 1871706SN/A/// Target getcwd() handler. 1881706SN/ASyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1891706SN/A Process *p, ThreadContext *tc); 1903114Sgblack@eecs.umich.edu 1911706SN/A/// Target readlink() handler. 1921706SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1931706SN/A Process *p, ThreadContext *tc, 1941706SN/A int index = 0); 1953114Sgblack@eecs.umich.eduSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1961706SN/A Process *p, ThreadContext *tc); 197511SN/A 1986703Svince@csl.cornell.edu/// Target unlink() handler. 1996703Svince@csl.cornell.eduSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2006703Svince@csl.cornell.edu Process *p, ThreadContext *tc, 2016703Svince@csl.cornell.edu int index); 2026685Stjones1@inf.ed.ac.ukSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2036685Stjones1@inf.ed.ac.uk Process *p, ThreadContext *tc); 2046685Stjones1@inf.ed.ac.uk 2056685Stjones1@inf.ed.ac.uk/// Target mkdir() handler. 2066685Stjones1@inf.ed.ac.ukSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2075513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 2085513SMichael.Adler@intel.com 2095513SMichael.Adler@intel.com/// Target rename() handler. 2105513SMichael.Adler@intel.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 2115513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 2121999SN/A 2131999SN/A 2143114Sgblack@eecs.umich.edu/// Target truncate() handler. 2151999SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 2161999SN/A Process *p, ThreadContext *tc); 2171999SN/A 2181999SN/A 2193114Sgblack@eecs.umich.edu/// Target ftruncate() handler. 2201999SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2213079Sstever@eecs.umich.edu Process *p, ThreadContext *tc); 2223079Sstever@eecs.umich.edu 2233114Sgblack@eecs.umich.edu 2243079Sstever@eecs.umich.edu/// Target truncate64() handler. 2252093SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num, 2262093SN/A Process *p, ThreadContext *tc); 2273114Sgblack@eecs.umich.edu 2282093SN/A/// Target ftruncate64() handler. 2292687Sksewell@umich.eduSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2302687Sksewell@umich.edu Process *p, ThreadContext *tc); 2313114Sgblack@eecs.umich.edu 2322687Sksewell@umich.edu 2332238SN/A/// Target umask() handler. 2342238SN/ASyscallReturn umaskFunc(SyscallDesc *desc, int num, 2353114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2362238SN/A 2372238SN/A/// Target gettid() handler. 2382238SN/ASyscallReturn gettidFunc(SyscallDesc *desc, int num, 2393114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2402238SN/A 2412238SN/A/// Target chown() handler. 2422238SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2433114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2442238SN/A 2452238SN/A/// Target setpgid() handler. 2462238SN/ASyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2473114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2482238SN/A 2492238SN/A/// Target fchown() handler. 2502238SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2513114Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2522238SN/A 2532238SN/A/// Target dup() handler. 2542238SN/ASyscallReturn dupFunc(SyscallDesc *desc, int num, 2553114Sgblack@eecs.umich.edu Process *process, ThreadContext *tc); 2562238SN/A 2572238SN/A/// Target dup2() handler. 2582238SN/ASyscallReturn dup2Func(SyscallDesc *desc, int num, 2593114Sgblack@eecs.umich.edu Process *process, ThreadContext *tc); 2602238SN/A 2616109Ssanchezd@stanford.edu/// Target fcntl() handler. 2626109Ssanchezd@stanford.eduSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2636109Ssanchezd@stanford.edu Process *process, ThreadContext *tc); 2642238SN/A 2659455Smitch.hayenga+gem5@gmail.com/// Target fcntl64() handler. 2669455Smitch.hayenga+gem5@gmail.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2679455Smitch.hayenga+gem5@gmail.com Process *process, ThreadContext *tc); 26810203SAli.Saidi@ARM.com 26910203SAli.Saidi@ARM.com/// Target setuid() handler. 27010203SAli.Saidi@ARM.comSyscallReturn setuidFunc(SyscallDesc *desc, int num, 2719455Smitch.hayenga+gem5@gmail.com Process *p, ThreadContext *tc); 2729112Smarc.orr@gmail.com 2739112Smarc.orr@gmail.com/// Target pipe() handler. 2749112Smarc.orr@gmail.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 2759112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 2769112Smarc.orr@gmail.com 2779112Smarc.orr@gmail.com/// Internal pipe() handler. 2789112Smarc.orr@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); 2849112Smarc.orr@gmail.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); 2969238Slluc.alvarez@bsc.es 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, 3039112Smarc.orr@gmail.com Process *p, ThreadContext *tc); 3049112Smarc.orr@gmail.com 3059112Smarc.orr@gmail.com/// Target access() handler 3069112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 30711367Sandreas.hansson@arm.com Process *p, ThreadContext *tc); 3089112Smarc.orr@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 30911321Ssteve.reinhardt@amd.com Process *p, ThreadContext *tc, 3109112Smarc.orr@gmail.com int index); 3119112Smarc.orr@gmail.com 3129112Smarc.orr@gmail.com/// Futex system call 3139112Smarc.orr@gmail.com/// Implemented by Daniel Sanchez 3149112Smarc.orr@gmail.com/// Used by printf's in multi-threaded apps 3159112Smarc.orr@gmail.comtemplate <class OS> 3169112Smarc.orr@gmail.comSyscallReturn 3179112Smarc.orr@gmail.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3189112Smarc.orr@gmail.com ThreadContext *tc) 3199112Smarc.orr@gmail.com{ 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()); 33911321Ssteve.reinhardt@amd.com int mem_val = *(int*)buf.bufferPtr(); 3409112Smarc.orr@gmail.com 3419112Smarc.orr@gmail.com /* 3429112Smarc.orr@gmail.com * The value in memory at uaddr is not equal with the expected val 3439112Smarc.orr@gmail.com * (a different thread must have changed it before the system call was 3449112Smarc.orr@gmail.com * invoked). In this case, we need to throw an error. 3459112Smarc.orr@gmail.com */ 3469112Smarc.orr@gmail.com if (val != mem_val) 3479112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 3489238Slluc.alvarez@bsc.es 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); 3542238SN/A } 3552238SN/A 3562238SN/A warn("futex: op %d not implemented; ignoring.", op); 3572238SN/A return -ENOSYS; 3583114Sgblack@eecs.umich.edu} 3592238SN/A 3602238SN/A 3612238SN/A/// Pseudo Funcs - These functions use a different return convension, 3623114Sgblack@eecs.umich.edu/// 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); 3652238SN/A 3663114Sgblack@eecs.umich.edu/// Target getpidPseudo() handler. 3672238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3682238SN/A Process *p, ThreadContext *tc); 3692238SN/A 3703114Sgblack@eecs.umich.edu/// Target getuidPseudo() handler. 3712238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3722238SN/A Process *p, ThreadContext *tc); 3731354SN/A 3741354SN/A/// Target getgidPseudo() handler. 37510796Sbrandon.potter@amd.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 37610796Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3771354SN/A 3781354SN/A 3791354SN/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. 3821354SN/Aconst int one_billion = 1000000000; 3831354SN/A 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. 38710796Sbrandon.potter@amd.comconst unsigned seconds_since_epoch = 1000000000; 3881354SN/A 38910796Sbrandon.potter@amd.com/// 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{ 39510796Sbrandon.potter@amd.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 39610796Sbrandon.potter@amd.com sec = elapsed_usecs / one_million; 39710796Sbrandon.potter@amd.com usec = elapsed_usecs % one_million; 39810796Sbrandon.potter@amd.com} 39910796Sbrandon.potter@amd.com 40010796Sbrandon.potter@amd.com/// 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) 405360SN/A{ 406360SN/A uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 407360SN/A sec = elapsed_nsecs / one_billion; 408360SN/A nsec = elapsed_nsecs % one_billion; 409360SN/A} 410360SN/A 411360SN/A////////////////////////////////////////////////////////////////////// 41211759Sbrandon.potter@amd.com// 4133113Sgblack@eecs.umich.edu// The following emulation functions are generic, but need to be 4143113Sgblack@eecs.umich.edu// templated to account for differences in types, constants, etc. 4153113Sgblack@eecs.umich.edu// 4163113Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////// 4173113Sgblack@eecs.umich.edu 4183113Sgblack@eecs.umich.edu typedef struct statfs hst_statfs; 4193113Sgblack@eecs.umich.edu#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 4294189Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 4304189Sgblack@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{ 4358737Skoansin.tan@gmail.com using namespace TheISA; 4363113Sgblack@eecs.umich.edu 4378737Skoansin.tan@gmail.com if (fakeTTY) 4383277Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 4395515SMichael.Adler@intel.com else 4405515SMichael.Adler@intel.com tgt->st_dev = host->st_dev; 4415515SMichael.Adler@intel.com tgt->st_dev = TheISA::htog(tgt->st_dev); 4425515SMichael.Adler@intel.com tgt->st_ino = host->st_ino; 4435515SMichael.Adler@intel.com tgt->st_ino = TheISA::htog(tgt->st_ino); 4448737Skoansin.tan@gmail.com tgt->st_mode = host->st_mode; 4453277Sgblack@eecs.umich.edu if (fakeTTY) { 4468737Skoansin.tan@gmail.com // Claim to be a character device 4473277Sgblack@eecs.umich.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4488737Skoansin.tan@gmail.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4493277Sgblack@eecs.umich.edu } 4508737Skoansin.tan@gmail.com tgt->st_mode = TheISA::htog(tgt->st_mode); 4513113Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4523113Sgblack@eecs.umich.edu tgt->st_nlink = TheISA::htog(tgt->st_nlink); 4533113Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 4543113Sgblack@eecs.umich.edu tgt->st_uid = TheISA::htog(tgt->st_uid); 4558737Skoansin.tan@gmail.com tgt->st_gid = host->st_gid; 4563113Sgblack@eecs.umich.edu tgt->st_gid = TheISA::htog(tgt->st_gid); 4578737Skoansin.tan@gmail.com if (fakeTTY) 4583114Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 4598737Skoansin.tan@gmail.com else 4603114Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 4618737Skoansin.tan@gmail.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 4623114Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 4638737Skoansin.tan@gmail.com tgt->st_size = TheISA::htog(tgt->st_size); 4644061Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 4654061Sgblack@eecs.umich.edu tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4664061Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 4678737Skoansin.tan@gmail.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4683113Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 4698737Skoansin.tan@gmail.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4703113Sgblack@eecs.umich.edu // Force the block size to be 8KB. This helps to ensure buffered io works 4713113Sgblack@eecs.umich.edu // consistently across different hosts. 4723113Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 4733113Sgblack@eecs.umich.edu tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4743113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 4753113Sgblack@eecs.umich.edu tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4763113Sgblack@eecs.umich.edu} 4773113Sgblack@eecs.umich.edu 4784189Sgblack@eecs.umich.edu// Same for stat64 4794189Sgblack@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) 4838737Skoansin.tan@gmail.com{ 4843113Sgblack@eecs.umich.edu using namespace TheISA; 4858737Skoansin.tan@gmail.com 4863113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4878737Skoansin.tan@gmail.com#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); 4903113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 4913113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 4923113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 4933113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 4943113Sgblack@eecs.umich.edu#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; 4988852Sandreas.hansson@arm.com#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, 5053113Sgblack@eecs.umich.edu 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); 5098852Sandreas.hansson@arm.com 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> 5146686Stjones1@inf.ed.ac.ukvoid 5153113Sgblack@eecs.umich.educopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5163113Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5173113Sgblack@eecs.umich.edu{ 51811759Sbrandon.potter@amd.com typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 51911759Sbrandon.potter@amd.com tgt_stat_buf tgt(addr); 52011759Sbrandon.potter@amd.com convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 52111759Sbrandon.potter@amd.com tgt.copyOut(mem); 52211759Sbrandon.potter@amd.com} 52311759Sbrandon.potter@amd.com 52411759Sbrandon.potter@amd.comtemplate <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); 53211759Sbrandon.potter@amd.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 53311759Sbrandon.potter@amd.com tgt->f_bsize = TheISA::htog(host->f_iosize); 53411759Sbrandon.potter@amd.com#else 53511759Sbrandon.potter@amd.com tgt->f_bsize = TheISA::htog(host->f_bsize); 53611759Sbrandon.potter@amd.com#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__) 544378SN/A tgt->f_namelen = TheISA::htog(host->f_namemax); 545378SN/A tgt->f_frsize = TheISA::htog(host->f_bsize); 5469141Smarc.orr@gmail.com#elif defined(__APPLE__) 5479141Smarc.orr@gmail.com tgt->f_namelen = 0; 548360SN/A tgt->f_frsize = 0; 5491450SN/A#else 5503114Sgblack@eecs.umich.edu tgt->f_namelen = TheISA::htog(host->f_namelen); 5512680Sktlim@umich.edu tgt->f_frsize = TheISA::htog(host->f_frsize); 552360SN/A#endif 5536701Sgblack@eecs.umich.edu#if defined(__linux__) 55410930Sbrandon.potter@amd.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 5556701Sgblack@eecs.umich.edu#else 556360SN/A /* 55710930Sbrandon.potter@amd.com * The fields are different sizes per OS. Don't bother with 558360SN/A * f_spare or f_reserved on non-Linux for now. 55910932Sbrandon.potter@amd.com */ 56010496Ssteve.reinhardt@amd.com memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 56110930Sbrandon.potter@amd.com#endif 562360SN/A 5631458SN/A tgt.copyOut(mem); 564360SN/A} 565360SN/A 56610930Sbrandon.potter@amd.com/// Target ioctl() handler. For the most part, programs call ioctl() 56710930Sbrandon.potter@amd.com/// only to find out if their stdout is a tty, to determine whether to 56810496Ssteve.reinhardt@amd.com/// do line or block buffering. We always claim that output fds are 56910496Ssteve.reinhardt@amd.com/// not TTYs to provide repeatable results. 5709141Smarc.orr@gmail.comtemplate <class OS> 5711458SN/ASyscallReturn 5729141Smarc.orr@gmail.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 573360SN/A{ 5749141Smarc.orr@gmail.com int index = 0; 57510930Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 5769141Smarc.orr@gmail.com unsigned req = p->getSyscallArg(tc, index); 577360SN/A 578360SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 579360SN/A 58010027SChris.Adeniyi-Jones@arm.com if (OS::isTtyReq(req)) 5813114Sgblack@eecs.umich.edu return -ENOTTY; 58210027SChris.Adeniyi-Jones@arm.com 583360SN/A auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 584360SN/A if (!dfdp) 585360SN/A return -EBADF; 5868852Sandreas.hansson@arm.com 5876701Sgblack@eecs.umich.edu /** 5881458SN/A * If the driver is valid, issue the ioctl through it. Otherwise, 589360SN/A * there's an implicit assumption that the device is a TTY type and we 5906701Sgblack@eecs.umich.edu * return that we do not have a valid TTY. 5916701Sgblack@eecs.umich.edu */ 592360SN/A EmulatedDriver *emul_driver = dfdp->getDriver(); 593360SN/A if (emul_driver) 594360SN/A return emul_driver->ioctl(p, tc, req); 595360SN/A 596360SN/A /** 597360SN/A * For lack of a better return code, return ENOTTY. Ideally, we should 598360SN/A * return something better here, but at least we issue the warning. 599360SN/A */ 600360SN/A warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 601360SN/A tgt_fd, req, tc->pcState()); 602360SN/A return -ENOTTY; 603360SN/A} 6041706SN/A 605360SN/Atemplate <class OS> 606360SN/ASyscallReturn 607360SN/AopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 608360SN/A bool isopenat) 609360SN/A{ 6103669Sbinkertn@umich.edu int index = 0; 6113669Sbinkertn@umich.edu int tgt_dirfd = -1; 6123669Sbinkertn@umich.edu 6131706SN/A /** 6141706SN/A * If using the openat variant, read in the target directory file 61510496Ssteve.reinhardt@amd.com * descriptor from the simulated process. 61610496Ssteve.reinhardt@amd.com */ 61710496Ssteve.reinhardt@amd.com if (isopenat) 61810496Ssteve.reinhardt@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 61910496Ssteve.reinhardt@amd.com 62010496Ssteve.reinhardt@amd.com /** 62110496Ssteve.reinhardt@amd.com * Retrieve the simulated process' memory proxy and then read in the path 62210496Ssteve.reinhardt@amd.com * string from that memory space into the host's working memory space. 62310496Ssteve.reinhardt@amd.com */ 62410496Ssteve.reinhardt@amd.com std::string path; 62510496Ssteve.reinhardt@amd.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 62610496Ssteve.reinhardt@amd.com return -EFAULT; 62710496Ssteve.reinhardt@amd.com 62810496Ssteve.reinhardt@amd.com#ifdef __CYGWIN32__ 62910496Ssteve.reinhardt@amd.com int host_flags = O_BINARY; 63010496Ssteve.reinhardt@amd.com#else 63110496Ssteve.reinhardt@amd.com int host_flags = 0; 63210496Ssteve.reinhardt@amd.com#endif 63310496Ssteve.reinhardt@amd.com /** 63410496Ssteve.reinhardt@amd.com * Translate target flags into host flags. Flags exist which are not 6355795Ssaidi@eecs.umich.edu * ported between architectures which can cause check failures. 6369143Ssteve.reinhardt@amd.com */ 6379142Ssteve.reinhardt@amd.com int tgt_flags = p->getSyscallArg(tc, index); 6389142Ssteve.reinhardt@amd.com for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 6399143Ssteve.reinhardt@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 6405795Ssaidi@eecs.umich.edu tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 6419143Ssteve.reinhardt@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 6425795Ssaidi@eecs.umich.edu } 6435795Ssaidi@eecs.umich.edu } 6445795Ssaidi@eecs.umich.edu if (tgt_flags) { 6459143Ssteve.reinhardt@amd.com warn("open%s: cannot decode flags 0x%x", 6465795Ssaidi@eecs.umich.edu isopenat ? "at" : "", tgt_flags); 647360SN/A } 6489143Ssteve.reinhardt@amd.com#ifdef __CYGWIN32__ 6499143Ssteve.reinhardt@amd.com host_flags |= O_BINARY; 6509143Ssteve.reinhardt@amd.com#endif 65110932Sbrandon.potter@amd.com 652360SN/A int mode = p->getSyscallArg(tc, index); 653360SN/A 65410027SChris.Adeniyi-Jones@arm.com /** 65510027SChris.Adeniyi-Jones@arm.com * If the simulated process called open or openat with AT_FDCWD specified, 65610027SChris.Adeniyi-Jones@arm.com * take the current working directory value which was passed into the 65710027SChris.Adeniyi-Jones@arm.com * process class as a Python parameter and append the current path to 65810027SChris.Adeniyi-Jones@arm.com * create a full path. 65910027SChris.Adeniyi-Jones@arm.com * Otherwise, openat with a valid target directory file descriptor has 66010027SChris.Adeniyi-Jones@arm.com * been called. If the path option, which was passed in as a parameter, 66110027SChris.Adeniyi-Jones@arm.com * is not absolute, retrieve the directory file descriptor's path and 66210027SChris.Adeniyi-Jones@arm.com * prepend it to the path passed in as a parameter. 66310027SChris.Adeniyi-Jones@arm.com * In every case, we should have a full path (which is relevant to the 66410027SChris.Adeniyi-Jones@arm.com * host) to work with after this block has been passed. 66510027SChris.Adeniyi-Jones@arm.com */ 66610027SChris.Adeniyi-Jones@arm.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 66710027SChris.Adeniyi-Jones@arm.com path = p->fullPath(path); 66810027SChris.Adeniyi-Jones@arm.com } else if (!startswith(path, "/")) { 66910027SChris.Adeniyi-Jones@arm.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 67010027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 67110027SChris.Adeniyi-Jones@arm.com if (!ffdp) 67210027SChris.Adeniyi-Jones@arm.com return -EBADF; 67310027SChris.Adeniyi-Jones@arm.com path.insert(0, ffdp->getFileName()); 67410027SChris.Adeniyi-Jones@arm.com } 67510027SChris.Adeniyi-Jones@arm.com 67610633Smichaelupton@gmail.com /** 67710633Smichaelupton@gmail.com * Since this is an emulated environment, we create pseudo file 67810633Smichaelupton@gmail.com * descriptors for device requests that have been registered with 67910633Smichaelupton@gmail.com * the process class through Python; this allows us to create a file 68010633Smichaelupton@gmail.com * descriptor for subsequent ioctl or mmap calls. 68110633Smichaelupton@gmail.com */ 68210633Smichaelupton@gmail.com if (startswith(path, "/dev/")) { 68310633Smichaelupton@gmail.com std::string filename = path.substr(strlen("/dev/")); 68410633Smichaelupton@gmail.com EmulatedDriver *drv = p->findDriver(filename); 68510633Smichaelupton@gmail.com if (drv) { 68610633Smichaelupton@gmail.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 68710633Smichaelupton@gmail.com "driver open with path[%s]\n", 68810633Smichaelupton@gmail.com isopenat ? "at" : "", path.c_str()); 68910633Smichaelupton@gmail.com return drv->open(p, tc, mode, host_flags); 69010203SAli.Saidi@ARM.com } 69110203SAli.Saidi@ARM.com /** 69210203SAli.Saidi@ARM.com * Fall through here for pass through to host devices, such 69310203SAli.Saidi@ARM.com * as /dev/zero 69410203SAli.Saidi@ARM.com */ 69510203SAli.Saidi@ARM.com } 69610203SAli.Saidi@ARM.com 69710203SAli.Saidi@ARM.com /** 69810203SAli.Saidi@ARM.com * Some special paths and files cannot be called on the host and need 69910203SAli.Saidi@ARM.com * to be handled as special cases inside the simulator. 70010203SAli.Saidi@ARM.com * If the full path that was created above does not match any of the 70110203SAli.Saidi@ARM.com * special cases, pass it through to the open call on the host to let 70210203SAli.Saidi@ARM.com * the host open the file on our behalf. 70310203SAli.Saidi@ARM.com * If the host cannot open the file, return the host's error code back 70410203SAli.Saidi@ARM.com * through the system call to the simulated process. 70510203SAli.Saidi@ARM.com */ 70610203SAli.Saidi@ARM.com int sim_fd = -1; 70710203SAli.Saidi@ARM.com std::vector<std::string> special_paths = 70810203SAli.Saidi@ARM.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 70910203SAli.Saidi@ARM.com for (auto entry : special_paths) { 71010203SAli.Saidi@ARM.com if (startswith(path, entry)) 71110203SAli.Saidi@ARM.com sim_fd = OS::openSpecialFile(path, p, tc); 71210203SAli.Saidi@ARM.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 } 71610850SGiacomo.Gabrielli@arm.com if (sim_fd == -1) { 71710850SGiacomo.Gabrielli@arm.com int local = -errno; 71810850SGiacomo.Gabrielli@arm.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 71910850SGiacomo.Gabrielli@arm.com isopenat ? "at" : "", path.c_str()); 72010850SGiacomo.Gabrielli@arm.com return local; 72110850SGiacomo.Gabrielli@arm.com } 72210850SGiacomo.Gabrielli@arm.com 72310850SGiacomo.Gabrielli@arm.com /** 72410850SGiacomo.Gabrielli@arm.com * The file was opened successfully and needs to be recorded in the 72510850SGiacomo.Gabrielli@arm.com * process' file descriptor array so that it can be retrieved later. 72610850SGiacomo.Gabrielli@arm.com * The target file descriptor that is chosen will be the lowest unused 72710850SGiacomo.Gabrielli@arm.com * file descriptor. 72810850SGiacomo.Gabrielli@arm.com * Return the indirect target file descriptor back to the simulated 72910850SGiacomo.Gabrielli@arm.com * process to act as a handle for the opened file. 73010850SGiacomo.Gabrielli@arm.com */ 73110850SGiacomo.Gabrielli@arm.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 73210850SGiacomo.Gabrielli@arm.com int tgt_fd = p->fds->allocFD(ffdp); 73310850SGiacomo.Gabrielli@arm.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 73410850SGiacomo.Gabrielli@arm.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 73510850SGiacomo.Gabrielli@arm.com return tgt_fd; 73610850SGiacomo.Gabrielli@arm.com} 73710850SGiacomo.Gabrielli@arm.com 73810850SGiacomo.Gabrielli@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, 74210850SGiacomo.Gabrielli@arm.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) 7526640Svince@csl.cornell.edu{ 7536640Svince@csl.cornell.edu return openImpl<OS>(desc, callnum, process, tc, true); 7546640Svince@csl.cornell.edu} 7556640Svince@csl.cornell.edu 7566640Svince@csl.cornell.edu/// Target unlinkat() handler. 7576640Svince@csl.cornell.edutemplate <class OS> 7586640Svince@csl.cornell.eduSyscallReturn 7596701Sgblack@eecs.umich.eduunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 7606701Sgblack@eecs.umich.edu ThreadContext *tc) 76110793Sbrandon.potter@amd.com{ 7626640Svince@csl.cornell.edu int index = 0; 76311758Sbrandon.potter@amd.com int dirfd = process->getSyscallArg(tc, index); 76411758Sbrandon.potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 76511758Sbrandon.potter@amd.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 7666640Svince@csl.cornell.edu 7678706Sandreas.hansson@arm.com return unlinkHelper(desc, callnum, process, tc, 1); 7686640Svince@csl.cornell.edu} 7696701Sgblack@eecs.umich.edu 7706640Svince@csl.cornell.edu/// Target facessat() handler 771360SN/Atemplate <class OS> 7721999SN/ASyscallReturn 7731999SN/AfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 7741999SN/A ThreadContext *tc) 7753114Sgblack@eecs.umich.edu{ 7762680Sktlim@umich.edu int index = 0; 7771999SN/A int dirfd = process->getSyscallArg(tc, index); 7781999SN/A if (dirfd != OS::TGT_AT_FDCWD) 7791999SN/A warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 7806701Sgblack@eecs.umich.edu return accessFunc(desc, callnum, process, tc, 1); 7818852Sandreas.hansson@arm.com} 7826701Sgblack@eecs.umich.edu 7831999SN/A/// Target readlinkat() handler 7846701Sgblack@eecs.umich.edutemplate <class OS> 7851999SN/ASyscallReturn 7866701Sgblack@eecs.umich.edureadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 7871999SN/A ThreadContext *tc) 7881999SN/A{ 7891999SN/A int index = 0; 7901999SN/A int dirfd = process->getSyscallArg(tc, index); 7911999SN/A if (dirfd != OS::TGT_AT_FDCWD) 7923669Sbinkertn@umich.edu warn("openat: first argument not AT_FDCWD; unlikely to work"); 7933669Sbinkertn@umich.edu return readlinkFunc(desc, callnum, process, tc, 1); 7943669Sbinkertn@umich.edu} 7951999SN/A 7961999SN/A/// Target renameat() handler. 7971999SN/Atemplate <class OS> 7982218SN/ASyscallReturn 7991999SN/ArenameatFunc(SyscallDesc *desc, int callnum, Process *process, 8001999SN/A ThreadContext *tc) 8011999SN/A{ 8021999SN/A int index = 0; 8031999SN/A 8041999SN/A int olddirfd = process->getSyscallArg(tc, index); 8051999SN/A if (olddirfd != OS::TGT_AT_FDCWD) 8061999SN/A warn("renameat: first argument not AT_FDCWD; unlikely to work"); 8073114Sgblack@eecs.umich.edu 8082680Sktlim@umich.edu std::string old_name; 8091999SN/A 8106701Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(old_name, 81110931Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 81210931Sbrandon.potter@amd.com return -EFAULT; 81310931Sbrandon.potter@amd.com 81410932Sbrandon.potter@amd.com int newdirfd = process->getSyscallArg(tc, index); 81510931Sbrandon.potter@amd.com if (newdirfd != OS::TGT_AT_FDCWD) 8161999SN/A warn("renameat: third argument not AT_FDCWD; unlikely to work"); 8171999SN/A 8181999SN/A std::string new_name; 8191999SN/A 8201999SN/A if (!tc->getMemProxy().tryReadString(new_name, 8211999SN/A process->getSyscallArg(tc, index))) 8221999SN/A return -EFAULT; 8231999SN/A 82410931Sbrandon.potter@amd.com // Adjust path for current working directory 8251999SN/A old_name = process->fullPath(old_name); 8262218SN/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; 8301999SN/A} 8315877Shsul@eecs.umich.edu 8325877Shsul@eecs.umich.edu/// Target sysinfo() handler. 8335877Shsul@eecs.umich.edutemplate <class OS> 8345877Shsul@eecs.umich.eduSyscallReturn 8355877Shsul@eecs.umich.edusysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 8366701Sgblack@eecs.umich.edu ThreadContext *tc) 8376701Sgblack@eecs.umich.edu{ 8386701Sgblack@eecs.umich.edu 8396701Sgblack@eecs.umich.edu int index = 0; 8406701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 84110027SChris.Adeniyi-Jones@arm.com sysinfo(process->getSyscallArg(tc, index)); 84210027SChris.Adeniyi-Jones@arm.com 84310027SChris.Adeniyi-Jones@arm.com sysinfo->uptime = seconds_since_epoch; 84410027SChris.Adeniyi-Jones@arm.com sysinfo->totalram = process->system->memSize(); 84510027SChris.Adeniyi-Jones@arm.com sysinfo->mem_unit = 1; 8465877Shsul@eecs.umich.edu 84710318Sandreas.hansson@arm.com sysinfo.copyOut(tc->getMemProxy()); 84810318Sandreas.hansson@arm.com 8495877Shsul@eecs.umich.edu return 0; 8505877Shsul@eecs.umich.edu} 8515877Shsul@eecs.umich.edu 8525877Shsul@eecs.umich.edu/// Target chmod() handler. 85310486Stjablin@gmail.comtemplate <class OS> 85410486Stjablin@gmail.comSyscallReturn 8555877Shsul@eecs.umich.educhmodFunc(SyscallDesc *desc, int callnum, Process *process, 85610027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 85710027SChris.Adeniyi-Jones@arm.com{ 8585877Shsul@eecs.umich.edu std::string path; 8598601Ssteve.reinhardt@amd.com 8605877Shsul@eecs.umich.edu int index = 0; 8615877Shsul@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 8625877Shsul@eecs.umich.edu process->getSyscallArg(tc, index))) { 86310027SChris.Adeniyi-Jones@arm.com return -EFAULT; 8645877Shsul@eecs.umich.edu } 8655877Shsul@eecs.umich.edu 8665877Shsul@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 86710027SChris.Adeniyi-Jones@arm.com mode_t hostMode = 0; 86810027SChris.Adeniyi-Jones@arm.com 86910027SChris.Adeniyi-Jones@arm.com // XXX translate mode flags via OS::something??? 87010027SChris.Adeniyi-Jones@arm.com hostMode = mode; 87110027SChris.Adeniyi-Jones@arm.com 87210027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 8735877Shsul@eecs.umich.edu path = process->fullPath(path); 87410027SChris.Adeniyi-Jones@arm.com 87510027SChris.Adeniyi-Jones@arm.com // do the chmod 87610027SChris.Adeniyi-Jones@arm.com int result = chmod(path.c_str(), hostMode); 87710027SChris.Adeniyi-Jones@arm.com if (result < 0) 87810027SChris.Adeniyi-Jones@arm.com return -errno; 87910027SChris.Adeniyi-Jones@arm.com 88010027SChris.Adeniyi-Jones@arm.com return 0; 88110027SChris.Adeniyi-Jones@arm.com} 88210027SChris.Adeniyi-Jones@arm.com 88310027SChris.Adeniyi-Jones@arm.com 88410027SChris.Adeniyi-Jones@arm.com/// Target fchmod() handler. 88510027SChris.Adeniyi-Jones@arm.comtemplate <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); 8928601Ssteve.reinhardt@amd.com 89310027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 8945877Shsul@eecs.umich.edu if (!ffdp) 8955877Shsul@eecs.umich.edu return -EBADF; 8961999SN/A int sim_fd = ffdp->getSimFD(); 897378SN/A 898360SN/A mode_t hostMode = mode; 8991450SN/A 9003114Sgblack@eecs.umich.edu int result = fchmod(sim_fd, hostMode); 9012680Sktlim@umich.edu 902360SN/A return (result < 0) ? -errno : 0; 903360SN/A} 904360SN/A 9056701Sgblack@eecs.umich.edu/// Target mremap() handler. 9068852Sandreas.hansson@arm.comtemplate <class OS> 9076701Sgblack@eecs.umich.eduSyscallReturn 9086701Sgblack@eecs.umich.edumremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 9096701Sgblack@eecs.umich.edu{ 9106701Sgblack@eecs.umich.edu int index = 0; 911360SN/A Addr start = process->getSyscallArg(tc, index); 9123669Sbinkertn@umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 9133669Sbinkertn@umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 9143669Sbinkertn@umich.edu uint64_t flags = process->getSyscallArg(tc, index); 915360SN/A uint64_t provided_address = 0; 916360SN/A bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 917360SN/A 918360SN/A if (use_provided_address) 9192218SN/A provided_address = process->getSyscallArg(tc, index); 920360SN/A 9218706Sandreas.hansson@arm.com if ((start % TheISA::PageBytes != 0) || 922360SN/A (provided_address % TheISA::PageBytes != 0)) { 9231458SN/A warn("mremap failing: arguments not page aligned"); 924360SN/A return -EINVAL; 925360SN/A } 926360SN/A 9275074Ssaidi@eecs.umich.edu new_length = roundUp(new_length, TheISA::PageBytes); 9285074Ssaidi@eecs.umich.edu 9295074Ssaidi@eecs.umich.edu if (new_length > old_length) { 9305074Ssaidi@eecs.umich.edu std::shared_ptr<MemState> mem_state = process->memState; 9315074Ssaidi@eecs.umich.edu Addr mmap_end = mem_state->getMmapEnd(); 9325074Ssaidi@eecs.umich.edu 9335074Ssaidi@eecs.umich.edu if ((start + old_length) == mmap_end && 9345074Ssaidi@eecs.umich.edu (!use_provided_address || provided_address == start)) { 9356701Sgblack@eecs.umich.edu // This case cannot occur when growing downward, as 9368852Sandreas.hansson@arm.com // start is greater than or equal to mmap_end. 9376701Sgblack@eecs.umich.edu uint64_t diff = new_length - old_length; 9385074Ssaidi@eecs.umich.edu process->allocateMem(mmap_end, diff); 9396701Sgblack@eecs.umich.edu mem_state->setMmapEnd(mmap_end + diff); 9405074Ssaidi@eecs.umich.edu return start; 9415074Ssaidi@eecs.umich.edu } else { 9425074Ssaidi@eecs.umich.edu if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 9435074Ssaidi@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 9445208Ssaidi@eecs.umich.edu return -ENOMEM; 9455208Ssaidi@eecs.umich.edu } else { 9465208Ssaidi@eecs.umich.edu uint64_t new_start = provided_address; 9475208Ssaidi@eecs.umich.edu if (!use_provided_address) { 9485074Ssaidi@eecs.umich.edu new_start = process->mmapGrowsDown() ? 9495074Ssaidi@eecs.umich.edu mmap_end - new_length : mmap_end; 9505208Ssaidi@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); 9535074Ssaidi@eecs.umich.edu } 9545074Ssaidi@eecs.umich.edu 9558706Sandreas.hansson@arm.com 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, 9585074Ssaidi@eecs.umich.edu new_length - old_length); 9595074Ssaidi@eecs.umich.edu // add on the remaining unallocated pages 9605074Ssaidi@eecs.umich.edu process->allocateMem(new_start + old_length, 96110027SChris.Adeniyi-Jones@arm.com new_length - old_length, 96210027SChris.Adeniyi-Jones@arm.com use_provided_address /* clobber */); 96310027SChris.Adeniyi-Jones@arm.com if (use_provided_address && 96410027SChris.Adeniyi-Jones@arm.com ((new_start + new_length > mem_state->getMmapEnd() && 96510027SChris.Adeniyi-Jones@arm.com !process->mmapGrowsDown()) || 96610027SChris.Adeniyi-Jones@arm.com (new_start < mem_state->getMmapEnd() && 96710027SChris.Adeniyi-Jones@arm.com process->mmapGrowsDown()))) { 96810027SChris.Adeniyi-Jones@arm.com // something fishy going on here, at least notify the user 96910027SChris.Adeniyi-Jones@arm.com // @todo: increase mmap_end? 97010793Sbrandon.potter@amd.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 97110027SChris.Adeniyi-Jones@arm.com } 97210027SChris.Adeniyi-Jones@arm.com warn("returning %08p as start\n", new_start); 97310027SChris.Adeniyi-Jones@arm.com return new_start; 97410027SChris.Adeniyi-Jones@arm.com } 97510027SChris.Adeniyi-Jones@arm.com } 97610027SChris.Adeniyi-Jones@arm.com } else { 97710027SChris.Adeniyi-Jones@arm.com if (use_provided_address && provided_address != start) 97810027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 97910027SChris.Adeniyi-Jones@arm.com process->pTable->unmap(start + new_length, old_length - new_length); 98010027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 98110027SChris.Adeniyi-Jones@arm.com } 98210027SChris.Adeniyi-Jones@arm.com} 98310027SChris.Adeniyi-Jones@arm.com 98410027SChris.Adeniyi-Jones@arm.com/// Target stat() handler. 98510027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 98610027SChris.Adeniyi-Jones@arm.comSyscallReturn 98710027SChris.Adeniyi-Jones@arm.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; 99310027SChris.Adeniyi-Jones@arm.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); 9981999SN/A 9991999SN/A // Adjust path for current working directory 10001999SN/A path = process->fullPath(path); 10013114Sgblack@eecs.umich.edu 10022680Sktlim@umich.edu struct stat hostBuf; 10031999SN/A int result = stat(path.c_str(), &hostBuf); 10046701Sgblack@eecs.umich.edu 100510931Sbrandon.potter@amd.com if (result < 0) 10066701Sgblack@eecs.umich.edu return -errno; 100710931Sbrandon.potter@amd.com 100810932Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 100910931Sbrandon.potter@amd.com 10101999SN/A return 0; 10111999SN/A} 10122764Sstever@eecs.umich.edu 10132064SN/A 101410931Sbrandon.potter@amd.com/// Target stat64() handler. 10152064SN/Atemplate <class OS> 10162064SN/ASyscallReturn 101710931Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process, 10182064SN/A ThreadContext *tc) 10191999SN/A{ 10201999SN/A std::string path; 10212218SN/A 10221999SN/A int index = 0; 102310931Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 10241999SN/A process->getSyscallArg(tc, index))) 10251999SN/A return -EFAULT; 10261999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 10271999SN/A 10281999SN/A // Adjust path for current working directory 1029378SN/A path = process->fullPath(path); 1030360SN/A 10311450SN/A#if NO_STAT64 10323114Sgblack@eecs.umich.edu struct stat hostBuf; 10332680Sktlim@umich.edu int result = stat(path.c_str(), &hostBuf); 1034360SN/A#else 1035360SN/A struct stat64 hostBuf; 1036360SN/A int result = stat64(path.c_str(), &hostBuf); 10376701Sgblack@eecs.umich.edu#endif 10388852Sandreas.hansson@arm.com 10396701Sgblack@eecs.umich.edu if (result < 0) 10406701Sgblack@eecs.umich.edu return -errno; 10416701Sgblack@eecs.umich.edu 10426701Sgblack@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1043360SN/A 10443669Sbinkertn@umich.edu return 0; 10453669Sbinkertn@umich.edu} 10463669Sbinkertn@umich.edu 1047360SN/A 1048360SN/A/// Target fstatat64() handler. 1049360SN/Atemplate <class OS> 1050360SN/ASyscallReturn 10511458SN/Afstatat64Func(SyscallDesc *desc, int callnum, Process *process, 1052360SN/A ThreadContext *tc) 10538706Sandreas.hansson@arm.com{ 1054360SN/A int index = 0; 10551458SN/A int dirfd = process->getSyscallArg(tc, index); 1056360SN/A if (dirfd != OS::TGT_AT_FDCWD) 1057360SN/A warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 10581999SN/A 10591999SN/A std::string path; 10601999SN/A if (!tc->getMemProxy().tryReadString(path, 10613114Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 10622680Sktlim@umich.edu return -EFAULT; 10631999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 10641999SN/A 10651999SN/A // Adjust path for current working directory 10666701Sgblack@eecs.umich.edu path = process->fullPath(path); 10678852Sandreas.hansson@arm.com 10686701Sgblack@eecs.umich.edu#if NO_STAT64 10696701Sgblack@eecs.umich.edu struct stat hostBuf; 10706701Sgblack@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 10716701Sgblack@eecs.umich.edu#else 10721999SN/A struct stat64 hostBuf; 10733669Sbinkertn@umich.edu int result = stat64(path.c_str(), &hostBuf); 10743669Sbinkertn@umich.edu#endif 10753669Sbinkertn@umich.edu 10762764Sstever@eecs.umich.edu if (result < 0) 10772064SN/A return -errno; 10782064SN/A 10792064SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10801999SN/A 10811999SN/A return 0; 10822064SN/A} 10831999SN/A 10841999SN/A 10851999SN/A/// Target fstat64() handler. 10861999SN/Atemplate <class OS> 10878706Sandreas.hansson@arm.comSyscallReturn 10881999SN/Afstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 10891999SN/A{ 10901999SN/A int index = 0; 10911999SN/A int tgt_fd = p->getSyscallArg(tc, index); 1092378SN/A Addr bufPtr = p->getSyscallArg(tc, index); 1093360SN/A 10941450SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 10953114Sgblack@eecs.umich.edu if (!ffdp) 10962680Sktlim@umich.edu return -EBADF; 1097360SN/A int sim_fd = ffdp->getSimFD(); 10986701Sgblack@eecs.umich.edu 109910931Sbrandon.potter@amd.com#if NO_STAT64 11006701Sgblack@eecs.umich.edu struct stat hostBuf; 1101360SN/A int result = fstat(sim_fd, &hostBuf); 110211380Salexandru.dutu@amd.com#else 1103360SN/A struct stat64 hostBuf; 110410932Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 110510931Sbrandon.potter@amd.com#endif 11061458SN/A 1107360SN/A if (result < 0) 1108360SN/A return -errno; 110910931Sbrandon.potter@amd.com 1110360SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1111360SN/A 11121458SN/A return 0; 1113360SN/A} 111410931Sbrandon.potter@amd.com 11152021SN/A 11161458SN/A/// Target lstat() handler. 1117360SN/Atemplate <class OS> 1118360SN/ASyscallReturn 1119360SN/AlstatFunc(SyscallDesc *desc, int callnum, Process *process, 11201706SN/A ThreadContext *tc) 11211706SN/A{ 11221706SN/A std::string path; 11233114Sgblack@eecs.umich.edu 11242680Sktlim@umich.edu int index = 0; 11251706SN/A if (!tc->getMemProxy().tryReadString(path, 11261706SN/A process->getSyscallArg(tc, index))) { 11271706SN/A return -EFAULT; 11286701Sgblack@eecs.umich.edu } 11298852Sandreas.hansson@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 11306701Sgblack@eecs.umich.edu 11316701Sgblack@eecs.umich.edu // Adjust path for current working directory 11326701Sgblack@eecs.umich.edu path = process->fullPath(path); 11336701Sgblack@eecs.umich.edu 11341706SN/A struct stat hostBuf; 11353669Sbinkertn@umich.edu int result = lstat(path.c_str(), &hostBuf); 11363669Sbinkertn@umich.edu 11373669Sbinkertn@umich.edu if (result < 0) 11381706SN/A return -errno; 11391706SN/A 11401706SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11411706SN/A 11422218SN/A return 0; 11431706SN/A} 114411759Sbrandon.potter@amd.com 11451706SN/A/// Target lstat64() handler. 11461706SN/Atemplate <class OS> 11471706SN/ASyscallReturn 11481706SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 11491706SN/A ThreadContext *tc) 11501706SN/A{ 11511706SN/A std::string path; 11521706SN/A 11533114Sgblack@eecs.umich.edu int index = 0; 11542680Sktlim@umich.edu if (!tc->getMemProxy().tryReadString(path, 11551706SN/A process->getSyscallArg(tc, index))) { 11566701Sgblack@eecs.umich.edu return -EFAULT; 115710931Sbrandon.potter@amd.com } 11586701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11591706SN/A 116010932Sbrandon.potter@amd.com // Adjust path for current working directory 116110931Sbrandon.potter@amd.com path = process->fullPath(path); 11621706SN/A 11631706SN/A#if NO_STAT64 11641706SN/A struct stat hostBuf; 116510931Sbrandon.potter@amd.com 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 11711706SN/A if (result < 0) 11721706SN/A return -errno; 11731706SN/A 11741706SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11751706SN/A 11761999SN/A return 0; 11771999SN/A} 11781999SN/A 11793114Sgblack@eecs.umich.edu/// Target fstat() handler. 11802680Sktlim@umich.edutemplate <class OS> 11811999SN/ASyscallReturn 11826701Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 118310931Sbrandon.potter@amd.com{ 118410931Sbrandon.potter@amd.com int index = 0; 118510932Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 118610931Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 11871999SN/A 11881999SN/A DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 11898852Sandreas.hansson@arm.com 11906701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 11916701Sgblack@eecs.umich.edu if (!ffdp) 11921999SN/A return -EBADF; 11936227Snate@binkert.org int sim_fd = ffdp->getSimFD(); 11941999SN/A 11952461SN/A struct stat hostBuf; 11968852Sandreas.hansson@arm.com int result = fstat(sim_fd, &hostBuf); 11978852Sandreas.hansson@arm.com 11988737Skoansin.tan@gmail.com if (result < 0) 11991999SN/A return -errno; 12008852Sandreas.hansson@arm.com 12018852Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12021999SN/A 12031999SN/A return 0; 120410931Sbrandon.potter@amd.com} 12051999SN/A 12066227Snate@binkert.org 12071999SN/A/// Target statfs() handler. 12081999SN/Atemplate <class OS> 12091999SN/ASyscallReturn 12102218SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 12111999SN/A ThreadContext *tc) 121210629Sjthestness@gmail.com{ 12131999SN/A#if NO_STATFS 12141999SN/A warn("Host OS cannot support calls to statfs. Ignoring syscall"); 121511385Sbrandon.potter@amd.com#else 1216360SN/A std::string path; 12171450SN/A 121811385Sbrandon.potter@amd.com int index = 0; 121911385Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 1220360SN/A process->getSyscallArg(tc, index))) { 12216701Sgblack@eecs.umich.edu return -EFAULT; 12226701Sgblack@eecs.umich.edu } 12236701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 122411383Sbrandon.potter@amd.com 122511383Sbrandon.potter@amd.com // Adjust path for current working directory 12268324Ssteve.reinhardt@amd.com path = process->fullPath(path); 122710486Stjablin@gmail.com 1228360SN/A struct statfs hostBuf; 122911385Sbrandon.potter@amd.com int result = statfs(path.c_str(), &hostBuf); 123011385Sbrandon.potter@amd.com 12319008Sgblack@eecs.umich.edu if (result < 0) 123211383Sbrandon.potter@amd.com return -errno; 123311383Sbrandon.potter@amd.com 123411383Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 123511383Sbrandon.potter@amd.com#endif 123611383Sbrandon.potter@amd.com return 0; 123711383Sbrandon.potter@amd.com} 123811383Sbrandon.potter@amd.com 123911383Sbrandon.potter@amd.comtemplate <class OS> 124011383Sbrandon.potter@amd.comSyscallReturn 12418324Ssteve.reinhardt@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 124211383Sbrandon.potter@amd.com{ 124311383Sbrandon.potter@amd.com int index = 0; 124411383Sbrandon.potter@amd.com TheISA::IntReg flags = p->getSyscallArg(tc, index); 124511383Sbrandon.potter@amd.com TheISA::IntReg newStack = p->getSyscallArg(tc, index); 124611383Sbrandon.potter@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 124711383Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 124811383Sbrandon.potter@amd.com Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 124911383Sbrandon.potter@amd.com 125011383Sbrandon.potter@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 125111383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 125211383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 125311383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 125411383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 125511383Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 125611383Sbrandon.potter@amd.com return -EINVAL; 125711383Sbrandon.potter@amd.com 125811383Sbrandon.potter@amd.com ThreadContext *ctc; 125911383Sbrandon.potter@amd.com if (!(ctc = p->findFreeContext())) 126011383Sbrandon.potter@amd.com fatal("clone: no spare thread context in system"); 126111383Sbrandon.potter@amd.com 126211383Sbrandon.potter@amd.com /** 126311383Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 126411383Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 126511383Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 12668324Ssteve.reinhardt@amd.com * constructor. 12675877Shsul@eecs.umich.edu */ 126810486Stjablin@gmail.com ProcessParams *pp = new ProcessParams(); 126910486Stjablin@gmail.com pp->executable.assign(*(new std::string(p->progName()))); 127011383Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 127111383Sbrandon.potter@amd.com pp->system = p->system; 127211383Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 127311624Smichael.lebeane@amd.com pp->input.assign("stdin"); 127411624Smichael.lebeane@amd.com pp->output.assign("stdout"); 127511624Smichael.lebeane@amd.com pp->errout.assign("stderr"); 127611624Smichael.lebeane@amd.com pp->uid = p->uid(); 127711624Smichael.lebeane@amd.com pp->euid = p->euid(); 127811624Smichael.lebeane@amd.com pp->gid = p->gid(); 127911624Smichael.lebeane@amd.com pp->egid = p->egid(); 128011624Smichael.lebeane@amd.com 128111624Smichael.lebeane@amd.com /* Find the first free PID that's less than the maximum */ 128211624Smichael.lebeane@amd.com std::set<int> const& pids = p->system->PIDs; 128311624Smichael.lebeane@amd.com int temp_pid = *pids.begin(); 128411383Sbrandon.potter@amd.com do { 128511383Sbrandon.potter@amd.com temp_pid++; 1286360SN/A } while (pids.find(temp_pid) != pids.end()); 128711383Sbrandon.potter@amd.com if (temp_pid >= System::maxPID) 128811383Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 12898600Ssteve.reinhardt@amd.com 129011383Sbrandon.potter@amd.com pp->pid = temp_pid; 129111383Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 129211383Sbrandon.potter@amd.com Process *cp = pp->create(); 12938600Ssteve.reinhardt@amd.com delete pp; 12942544SN/A 12952544SN/A Process *owner = ctc->getProcessPtr(); 129611383Sbrandon.potter@amd.com ctc->setProcessPtr(cp); 129711383Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 129811383Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 129911386Ssteve.reinhardt@amd.com 130011386Ssteve.reinhardt@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 130111383Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 130211383Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 130311383Sbrandon.potter@amd.com *ptid = cp->pid(); 130411383Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 130511383Sbrandon.potter@amd.com } 130611383Sbrandon.potter@amd.com 130711383Sbrandon.potter@amd.com cp->initState(); 130811383Sbrandon.potter@amd.com p->clone(tc, ctc, cp, flags); 130911383Sbrandon.potter@amd.com 131011383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 131111383Sbrandon.potter@amd.com delete cp->sigchld; 131211383Sbrandon.potter@amd.com cp->sigchld = p->sigchld; 131311383Sbrandon.potter@amd.com } else if (flags & OS::TGT_SIGCHLD) { 131411383Sbrandon.potter@amd.com *cp->sigchld = true; 131511383Sbrandon.potter@amd.com } 13168600Ssteve.reinhardt@amd.com 13176672Sgblack@eecs.umich.edu if (flags & OS::TGT_CLONE_CHILD_SETTID) { 13188600Ssteve.reinhardt@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 131911383Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 132011383Sbrandon.potter@amd.com *ctid = cp->pid(); 132111383Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 13228601Ssteve.reinhardt@amd.com } 13232544SN/A 132411383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 132511383Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 132611383Sbrandon.potter@amd.com 132711383Sbrandon.potter@amd.com ctc->clearArchRegs(); 132811383Sbrandon.potter@amd.com 132911383Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 133011383Sbrandon.potter@amd.com TheISA::copyMiscRegs(tc, ctc); 133111383Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 133211383Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 133311383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 133411383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 133511383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 133611383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 133711383Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 133811383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 133911383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 134011383Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 134111383Sbrandon.potter@amd.com for (int y = 8; y < 32; y++) 134211383Sbrandon.potter@amd.com ctc->setIntReg(y, tc->readIntReg(y)); 134311383Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA 134411383Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 134511383Sbrandon.potter@amd.com#endif 134611383Sbrandon.potter@amd.com 134711383Sbrandon.potter@amd.com#if THE_ISA == X86_ISA 134811383Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_SETTLS) { 134911383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 135011383Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 135111383Sbrandon.potter@amd.com } 135211383Sbrandon.potter@amd.com#endif 135311383Sbrandon.potter@amd.com 135411383Sbrandon.potter@amd.com if (newStack) 135511383Sbrandon.potter@amd.com ctc->setIntReg(TheISA::StackPointerReg, newStack); 135611392Sbrandon.potter@amd.com 135711392Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 135811392Sbrandon.potter@amd.com 135911392Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 136011392Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 136111392Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 136211392Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 136311392Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 136411392Sbrandon.potter@amd.com#endif 136511392Sbrandon.potter@amd.com 136611392Sbrandon.potter@amd.com ctc->pcState(tc->nextInstAddr()); 136711392Sbrandon.potter@amd.com ctc->activate(); 136811392Sbrandon.potter@amd.com 136911392Sbrandon.potter@amd.com return cp->pid(); 137011392Sbrandon.potter@amd.com} 137111392Sbrandon.potter@amd.com 137211392Sbrandon.potter@amd.com/// Target fstatfs() handler. 137311392Sbrandon.potter@amd.comtemplate <class OS> 137411392Sbrandon.potter@amd.comSyscallReturn 137511392Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 137611392Sbrandon.potter@amd.com{ 137711392Sbrandon.potter@amd.com int index = 0; 137811392Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 137911392Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 138011392Sbrandon.potter@amd.com 138111383Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 138211383Sbrandon.potter@amd.com if (!ffdp) 138311383Sbrandon.potter@amd.com return -EBADF; 138411383Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 138511383Sbrandon.potter@amd.com 13861458SN/A struct statfs hostBuf; 1387360SN/A int result = fstatfs(sim_fd, &hostBuf); 1388360SN/A 138911593Santhony.gutierrez@amd.com if (result < 0) 139011593Santhony.gutierrez@amd.com return -errno; 139111593Santhony.gutierrez@amd.com 139211593Santhony.gutierrez@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 139311593Santhony.gutierrez@amd.com 139411593Santhony.gutierrez@amd.com return 0; 139511593Santhony.gutierrez@amd.com} 139611593Santhony.gutierrez@amd.com 139711593Santhony.gutierrez@amd.com 139811593Santhony.gutierrez@amd.com/// Target writev() handler. 139911593Santhony.gutierrez@amd.comtemplate <class OS> 140011593Santhony.gutierrez@amd.comSyscallReturn 140111593Santhony.gutierrez@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 140211593Santhony.gutierrez@amd.com{ 140311593Santhony.gutierrez@amd.com int index = 0; 140411593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 140511593Santhony.gutierrez@amd.com 140611594Santhony.gutierrez@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 140711593Santhony.gutierrez@amd.com if (!hbfdp) 140811593Santhony.gutierrez@amd.com return -EBADF; 140911593Santhony.gutierrez@amd.com int sim_fd = hbfdp->getSimFD(); 141011593Santhony.gutierrez@amd.com 141111385Sbrandon.potter@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 141211385Sbrandon.potter@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 141311385Sbrandon.potter@amd.com size_t count = p->getSyscallArg(tc, index); 141411385Sbrandon.potter@amd.com struct iovec hiov[count]; 141511385Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) { 141611385Sbrandon.potter@amd.com typename OS::tgt_iovec tiov; 141711385Sbrandon.potter@amd.com 141811385Sbrandon.potter@amd.com prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 141911385Sbrandon.potter@amd.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 142011385Sbrandon.potter@amd.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 142111385Sbrandon.potter@amd.com hiov[i].iov_base = new char [hiov[i].iov_len]; 142211385Sbrandon.potter@amd.com prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 142311385Sbrandon.potter@amd.com hiov[i].iov_len); 142411385Sbrandon.potter@amd.com } 142511385Sbrandon.potter@amd.com 142611385Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 1427378SN/A 1428360SN/A for (size_t i = 0; i < count; ++i) 14291450SN/A delete [] (char *)hiov[i].iov_base; 14303114Sgblack@eecs.umich.edu 14312680Sktlim@umich.edu if (result < 0) 1432360SN/A return -errno; 14336701Sgblack@eecs.umich.edu 14346701Sgblack@eecs.umich.edu return result; 14356701Sgblack@eecs.umich.edu} 1436360SN/A 1437360SN/A/// Real mmap handler. 14382064SN/Atemplate <class OS> 14395877Shsul@eecs.umich.eduSyscallReturn 14402064SN/AmmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 14418737Skoansin.tan@gmail.com bool is_mmap2) 14428737Skoansin.tan@gmail.com{ 14432064SN/A int index = 0; 1444360SN/A Addr start = p->getSyscallArg(tc, index); 14455877Shsul@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 14465877Shsul@eecs.umich.edu int prot = p->getSyscallArg(tc, index); 14475877Shsul@eecs.umich.edu int tgt_flags = p->getSyscallArg(tc, index); 14488737Skoansin.tan@gmail.com int tgt_fd = p->getSyscallArg(tc, index); 14498737Skoansin.tan@gmail.com int offset = p->getSyscallArg(tc, index); 14505877Shsul@eecs.umich.edu 14515877Shsul@eecs.umich.edu if (is_mmap2) 14522064SN/A offset *= TheISA::PageBytes; 145310794Sbrandon.potter@amd.com 145410794Sbrandon.potter@amd.com if (start & (TheISA::PageBytes - 1) || 14552064SN/A offset & (TheISA::PageBytes - 1) || 1456360SN/A (tgt_flags & OS::TGT_MAP_PRIVATE && 1457360SN/A tgt_flags & OS::TGT_MAP_SHARED) || 14588706Sandreas.hansson@arm.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 14591458SN/A !(tgt_flags & OS::TGT_MAP_SHARED)) || 1460360SN/A !length) { 1461360SN/A return -EINVAL; 146210796Sbrandon.potter@amd.com } 146310796Sbrandon.potter@amd.com 146410796Sbrandon.potter@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 146510796Sbrandon.potter@amd.com // With shared mmaps, there are two cases to consider: 146610796Sbrandon.potter@amd.com // 1) anonymous: writes should modify the mapping and this should be 146710796Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 146810796Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 146910796Sbrandon.potter@amd.com // structure which maintains information about the which virtual 147010796Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 147110796Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 147210796Sbrandon.potter@amd.com // 2) file-backed: writes should modify the mapping and the file 147310796Sbrandon.potter@amd.com // which is backed by the mapping. The shared mapping problem is the 147410796Sbrandon.potter@amd.com // same as what was mentioned about the anonymous mappings. For 147510796Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 147610796Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 147710796Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 147810796Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 147910796Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 148010796Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 148111337SMichael.Lebeane@amd.com // works well enough. The guarantee is provided by the msync system 148211337SMichael.Lebeane@amd.com // call. We could force the change through with shared mappings with 148311337SMichael.Lebeane@amd.com // a call to msync, but that again would require more information 148411337SMichael.Lebeane@amd.com // than we currently maintain. 148511337SMichael.Lebeane@amd.com warn("mmap: writing to shared mmap region is currently " 148611337SMichael.Lebeane@amd.com "unsupported. The write succeeds on the target, but it " 148711337SMichael.Lebeane@amd.com "will not be propagated to the host or shared mappings"); 148811337SMichael.Lebeane@amd.com } 148911337SMichael.Lebeane@amd.com 149011337SMichael.Lebeane@amd.com length = roundUp(length, TheISA::PageBytes); 149111337SMichael.Lebeane@amd.com 149211337SMichael.Lebeane@amd.com int sim_fd = -1; 149311337SMichael.Lebeane@amd.com uint8_t *pmap = nullptr; 149411337SMichael.Lebeane@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 149511337SMichael.Lebeane@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 149611337SMichael.Lebeane@amd.com 149711337SMichael.Lebeane@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1498378SN/A if (dfdp) { 1499360SN/A EmulatedDriver *emul_driver = dfdp->getDriver(); 15001450SN/A return emul_driver->mmap(p, tc, start, length, prot, 15013114Sgblack@eecs.umich.edu tgt_flags, tgt_fd, offset); 15022680Sktlim@umich.edu } 1503360SN/A 15046701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 15056701Sgblack@eecs.umich.edu if (!ffdp) 1506360SN/A return -EBADF; 150710796Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 1508360SN/A 15096109Ssanchezd@stanford.edu pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 15106109Ssanchezd@stanford.edu sim_fd, offset); 1511360SN/A 15128706Sandreas.hansson@arm.com if (pmap == (decltype(pmap))-1) { 1513360SN/A warn("mmap: failed to map file into host address space"); 15141458SN/A return -errno; 1515360SN/A } 1516360SN/A } 1517360SN/A 15181999SN/A // Extend global mmap region if necessary. Note that we ignore the 15191999SN/A // start address unless MAP_FIXED is specified. 15201999SN/A if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 15213114Sgblack@eecs.umich.edu std::shared_ptr<MemState> mem_state = p->memState; 15222680Sktlim@umich.edu Addr mmap_end = mem_state->getMmapEnd(); 15231999SN/A 15241999SN/A start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 15251999SN/A mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 15266701Sgblack@eecs.umich.edu 15278852Sandreas.hansson@arm.com mem_state->setMmapEnd(mmap_end); 15286701Sgblack@eecs.umich.edu } 15296701Sgblack@eecs.umich.edu 15306701Sgblack@eecs.umich.edu DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 15311999SN/A start, start + length - 1); 15326701Sgblack@eecs.umich.edu 15336701Sgblack@eecs.umich.edu // We only allow mappings to overwrite existing mappings if 15348706Sandreas.hansson@arm.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 15351999SN/A // because we ignore the start hint if TGT_MAP_FIXED is not set. 15361999SN/A int clobber = tgt_flags & OS::TGT_MAP_FIXED; 15371999SN/A if (clobber) { 15381999SN/A for (auto tc : p->system->threadContexts) { 15398737Skoansin.tan@gmail.com // If we might be overwriting old mappings, we need to 15408737Skoansin.tan@gmail.com // invalidate potentially stale mappings out of the TLBs. 15411999SN/A tc->getDTBPtr()->flushAll(); 15423669Sbinkertn@umich.edu tc->getITBPtr()->flushAll(); 15433669Sbinkertn@umich.edu } 15443669Sbinkertn@umich.edu } 15453669Sbinkertn@umich.edu 15461999SN/A // Allocate physical memory and map it in. If the page table is already 15471999SN/A // mapped and clobber is not set, the simulator will issue throw a 15481999SN/A // fatal and bail out of the simulation. 15491999SN/A p->allocateMem(start, length, clobber); 15501999SN/A 15511999SN/A // Transfer content into target address space. 15521999SN/A SETranslatingPortProxy &tp = tc->getMemProxy(); 1553378SN/A if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1554360SN/A // In general, we should zero the mapped area for anonymous mappings, 15551450SN/A // with something like: 15563114Sgblack@eecs.umich.edu // tp.memsetBlob(start, 0, length); 15572680Sktlim@umich.edu // However, given that we don't support sparse mappings, and 1558360SN/A // some applications can map a couple of gigabytes of space 15596701Sgblack@eecs.umich.edu // (intending sparse usage), that can get painfully expensive. 15606701Sgblack@eecs.umich.edu // Fortunately, since we don't properly implement munmap either, 15616701Sgblack@eecs.umich.edu // there's no danger of remapping used memory, so for now all 1562360SN/A // newly mapped memory should already be zeroed so we can skip it. 15633670Sbinkertn@umich.edu } else { 15643670Sbinkertn@umich.edu // It is possible to mmap an area larger than a file, however 1565360SN/A // accessing unmapped portions the system triggers a "Bus error" 1566360SN/A // on the host. We must know when to stop copying the file from 1567360SN/A // the host into the target address space. 1568360SN/A struct stat file_stat; 1569360SN/A if (fstat(sim_fd, &file_stat) > 0) 1570360SN/A fatal("mmap: cannot stat file"); 1571360SN/A 1572360SN/A // Copy the portion of the file that is resident. This requires 1573360SN/A // checking both the mmap size and the filesize that we are 1574360SN/A // trying to mmap into this space; the mmap size also depends 1575360SN/A // on the specified offset into the file. 1576360SN/A uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1577360SN/A length); 1578360SN/A tp.writeBlob(start, pmap, size); 1579360SN/A 1580360SN/A // Cleanup the mmap region before exiting this function. 1581360SN/A munmap(pmap, length); 15823670Sbinkertn@umich.edu 15833670Sbinkertn@umich.edu // Maintain the symbol table for dynamic executables. 158410796Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 15858737Skoansin.tan@gmail.com // space and we intercept that here. We can verify that we are 15868737Skoansin.tan@gmail.com // executing inside the loader by checking the program counter value. 15873670Sbinkertn@umich.edu // XXX: with multiprogrammed workloads or multi-node configurations, 15883670Sbinkertn@umich.edu // this will not work since there is a single global symbol table. 15893670Sbinkertn@umich.edu ObjectFile *interpreter = p->getInterpreter(); 15903670Sbinkertn@umich.edu if (interpreter) { 15913670Sbinkertn@umich.edu Addr text_start = interpreter->textBase(); 15923670Sbinkertn@umich.edu Addr text_end = text_start + interpreter->textSize(); 15933670Sbinkertn@umich.edu 15943670Sbinkertn@umich.edu Addr pc = tc->pcState().pc(); 15953670Sbinkertn@umich.edu 15963670Sbinkertn@umich.edu if (pc >= text_start && pc < text_end) { 15973670Sbinkertn@umich.edu std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 15983670Sbinkertn@umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 15993670Sbinkertn@umich.edu ObjectFile *lib = createObjectFile(ffdp->getFileName()); 16008706Sandreas.hansson@arm.com 1601360SN/A if (lib) { 16021458SN/A lib->loadAllSymbols(debugSymbolTable, 1603360SN/A lib->textBase(), start); 1604360SN/A } 16056683Stjones1@inf.ed.ac.uk } 16066683Stjones1@inf.ed.ac.uk } 16076683Stjones1@inf.ed.ac.uk 16086683Stjones1@inf.ed.ac.uk // Note that we do not zero out the remainder of the mapping. This 16096683Stjones1@inf.ed.ac.uk // is done by a real system, but it probably will not affect 16106683Stjones1@inf.ed.ac.uk // execution (hopefully). 16116701Sgblack@eecs.umich.edu } 16126701Sgblack@eecs.umich.edu 16136683Stjones1@inf.ed.ac.uk return start; 16146683Stjones1@inf.ed.ac.uk} 16157823Ssteve.reinhardt@amd.com 16166683Stjones1@inf.ed.ac.uktemplate <class OS> 16176683Stjones1@inf.ed.ac.ukSyscallReturn 16186683Stjones1@inf.ed.ac.ukpwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16196683Stjones1@inf.ed.ac.uk{ 16206683Stjones1@inf.ed.ac.uk int index = 0; 16216683Stjones1@inf.ed.ac.uk int tgt_fd = p->getSyscallArg(tc, index); 16228737Skoansin.tan@gmail.com Addr bufPtr = p->getSyscallArg(tc, index); 16236683Stjones1@inf.ed.ac.uk int nbytes = p->getSyscallArg(tc, index); 16246683Stjones1@inf.ed.ac.uk int offset = p->getSyscallArg(tc, index); 16258706Sandreas.hansson@arm.com 16266683Stjones1@inf.ed.ac.uk auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16276683Stjones1@inf.ed.ac.uk if (!ffdp) 16286683Stjones1@inf.ed.ac.uk return -EBADF; 16296683Stjones1@inf.ed.ac.uk int sim_fd = ffdp->getSimFD(); 16302553SN/A 16316684Stjones1@inf.ed.ac.uk BufferArg bufArg(bufPtr, nbytes); 16326684Stjones1@inf.ed.ac.uk bufArg.copyIn(tc->getMemProxy()); 16336684Stjones1@inf.ed.ac.uk 16346684Stjones1@inf.ed.ac.uk int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 16356684Stjones1@inf.ed.ac.uk 16366684Stjones1@inf.ed.ac.uk return (bytes_written == -1) ? -errno : bytes_written; 16376684Stjones1@inf.ed.ac.uk} 163810796Sbrandon.potter@amd.com 16396684Stjones1@inf.ed.ac.uk/// Target mmap() handler. 16406684Stjones1@inf.ed.ac.uktemplate <class OS> 16416701Sgblack@eecs.umich.eduSyscallReturn 16426701Sgblack@eecs.umich.edummapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 164311321Ssteve.reinhardt@amd.com{ 16446684Stjones1@inf.ed.ac.uk return mmapImpl<OS>(desc, num, p, tc, false); 16458737Skoansin.tan@gmail.com} 16468852Sandreas.hansson@arm.com 16478852Sandreas.hansson@arm.com/// Target mmap2() handler. 16486684Stjones1@inf.ed.ac.uktemplate <class OS> 16496684Stjones1@inf.ed.ac.ukSyscallReturn 16506684Stjones1@inf.ed.ac.ukmmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16512553SN/A{ 16522553SN/A return mmapImpl<OS>(desc, num, p, tc, true); 16531354SN/A} 1654 1655/// Target getrlimit() handler. 1656template <class OS> 1657SyscallReturn 1658getrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1659 ThreadContext *tc) 1660{ 1661 int index = 0; 1662 unsigned resource = process->getSyscallArg(tc, index); 1663 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1664 1665 switch (resource) { 1666 case OS::TGT_RLIMIT_STACK: 1667 // max stack size in bytes: make up a number (8MB for now) 1668 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1669 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1670 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1671 break; 1672 1673 case OS::TGT_RLIMIT_DATA: 1674 // max data segment size in bytes: make up a number 1675 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1676 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1677 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1678 break; 1679 1680 default: 1681 warn("getrlimit: unimplemented resource %d", resource); 1682 return -EINVAL; 1683 break; 1684 } 1685 1686 rlp.copyOut(tc->getMemProxy()); 1687 return 0; 1688} 1689 1690template <class OS> 1691SyscallReturn 1692prlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1693 ThreadContext *tc) 1694{ 1695 int index = 0; 1696 if (process->getSyscallArg(tc, index) != 0) 1697 { 1698 warn("prlimit: ignoring rlimits for nonzero pid"); 1699 return -EPERM; 1700 } 1701 int resource = process->getSyscallArg(tc, index); 1702 Addr n = process->getSyscallArg(tc, index); 1703 if (n != 0) 1704 warn("prlimit: ignoring new rlimit"); 1705 Addr o = process->getSyscallArg(tc, index); 1706 if (o != 0) 1707 { 1708 TypedBufferArg<typename OS::rlimit> rlp( 1709 process->getSyscallArg(tc, index)); 1710 switch (resource) { 1711 case OS::TGT_RLIMIT_STACK: 1712 // max stack size in bytes: make up a number (8MB for now) 1713 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1714 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1715 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1716 break; 1717 case OS::TGT_RLIMIT_DATA: 1718 // max data segment size in bytes: make up a number 1719 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 1720 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1721 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1722 default: 1723 warn("prlimit: unimplemented resource %d", resource); 1724 return -EINVAL; 1725 break; 1726 } 1727 rlp.copyOut(tc->getMemProxy()); 1728 } 1729 return 0; 1730} 1731 1732/// Target clock_gettime() function. 1733template <class OS> 1734SyscallReturn 1735clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1736{ 1737 int index = 1; 1738 //int clk_id = p->getSyscallArg(tc, index); 1739 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1740 1741 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 1742 tp->tv_sec += seconds_since_epoch; 1743 tp->tv_sec = TheISA::htog(tp->tv_sec); 1744 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 1745 1746 tp.copyOut(tc->getMemProxy()); 1747 1748 return 0; 1749} 1750 1751/// Target clock_getres() function. 1752template <class OS> 1753SyscallReturn 1754clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1755{ 1756 int index = 1; 1757 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 1758 1759 // Set resolution at ns, which is what clock_gettime() returns 1760 tp->tv_sec = 0; 1761 tp->tv_nsec = 1; 1762 1763 tp.copyOut(tc->getMemProxy()); 1764 1765 return 0; 1766} 1767 1768/// Target gettimeofday() handler. 1769template <class OS> 1770SyscallReturn 1771gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 1772 ThreadContext *tc) 1773{ 1774 int index = 0; 1775 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1776 1777 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1778 tp->tv_sec += seconds_since_epoch; 1779 tp->tv_sec = TheISA::htog(tp->tv_sec); 1780 tp->tv_usec = TheISA::htog(tp->tv_usec); 1781 1782 tp.copyOut(tc->getMemProxy()); 1783 1784 return 0; 1785} 1786 1787 1788/// Target utimes() handler. 1789template <class OS> 1790SyscallReturn 1791utimesFunc(SyscallDesc *desc, int callnum, Process *process, 1792 ThreadContext *tc) 1793{ 1794 std::string path; 1795 1796 int index = 0; 1797 if (!tc->getMemProxy().tryReadString(path, 1798 process->getSyscallArg(tc, index))) { 1799 return -EFAULT; 1800 } 1801 1802 TypedBufferArg<typename OS::timeval [2]> 1803 tp(process->getSyscallArg(tc, index)); 1804 tp.copyIn(tc->getMemProxy()); 1805 1806 struct timeval hostTimeval[2]; 1807 for (int i = 0; i < 2; ++i) { 1808 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 1809 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 1810 } 1811 1812 // Adjust path for current working directory 1813 path = process->fullPath(path); 1814 1815 int result = utimes(path.c_str(), hostTimeval); 1816 1817 if (result < 0) 1818 return -errno; 1819 1820 return 0; 1821} 1822 1823template <class OS> 1824SyscallReturn 1825execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1826{ 1827 desc->setFlags(0); 1828 1829 int index = 0; 1830 std::string path; 1831 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 1832 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 1833 return -EFAULT; 1834 1835 if (access(path.c_str(), F_OK) == -1) 1836 return -EACCES; 1837 1838 auto read_in = [](std::vector<std::string> & vect, 1839 SETranslatingPortProxy & mem_proxy, 1840 Addr mem_loc) 1841 { 1842 for (int inc = 0; ; inc++) { 1843 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 1844 b.copyIn(mem_proxy); 1845 1846 if (!*(Addr*)b.bufferPtr()) 1847 break; 1848 1849 vect.push_back(std::string()); 1850 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 1851 } 1852 }; 1853 1854 /** 1855 * Note that ProcessParams is generated by swig and there are no other 1856 * examples of how to create anything but this default constructor. The 1857 * fields are manually initialized instead of passing parameters to the 1858 * constructor. 1859 */ 1860 ProcessParams *pp = new ProcessParams(); 1861 pp->executable = path; 1862 Addr argv_mem_loc = p->getSyscallArg(tc, index); 1863 read_in(pp->cmd, mem_proxy, argv_mem_loc); 1864 Addr envp_mem_loc = p->getSyscallArg(tc, index); 1865 read_in(pp->env, mem_proxy, envp_mem_loc); 1866 pp->uid = p->uid(); 1867 pp->egid = p->egid(); 1868 pp->euid = p->euid(); 1869 pp->gid = p->gid(); 1870 pp->ppid = p->ppid(); 1871 pp->pid = p->pid(); 1872 pp->input.assign("cin"); 1873 pp->output.assign("cout"); 1874 pp->errout.assign("cerr"); 1875 pp->cwd.assign(p->getcwd()); 1876 pp->system = p->system; 1877 /** 1878 * Prevent process object creation with identical PIDs (which will trip 1879 * a fatal check in Process constructor). The execve call is supposed to 1880 * take over the currently executing process' identity but replace 1881 * whatever it is doing with a new process image. Instead of hijacking 1882 * the process object in the simulator, we create a new process object 1883 * and bind to the previous process' thread below (hijacking the thread). 1884 */ 1885 p->system->PIDs.erase(p->pid()); 1886 Process *new_p = pp->create(); 1887 delete pp; 1888 1889 /** 1890 * Work through the file descriptor array and close any files marked 1891 * close-on-exec. 1892 */ 1893 new_p->fds = p->fds; 1894 for (int i = 0; i < new_p->fds->getSize(); i++) { 1895 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 1896 if (fdep && fdep->getCOE()) 1897 new_p->fds->closeFDEntry(i); 1898 } 1899 1900 *new_p->sigchld = true; 1901 1902 delete p; 1903 tc->clearArchRegs(); 1904 tc->setProcessPtr(new_p); 1905 new_p->assignThreadContext(tc->contextId()); 1906 new_p->initState(); 1907 tc->activate(); 1908 TheISA::PCState pcState = tc->pcState(); 1909 tc->setNPC(pcState.instAddr()); 1910 1911 desc->setFlags(SyscallDesc::SuppressReturnValue); 1912 return 0; 1913} 1914 1915/// Target getrusage() function. 1916template <class OS> 1917SyscallReturn 1918getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 1919 ThreadContext *tc) 1920{ 1921 int index = 0; 1922 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 1923 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 1924 1925 rup->ru_utime.tv_sec = 0; 1926 rup->ru_utime.tv_usec = 0; 1927 rup->ru_stime.tv_sec = 0; 1928 rup->ru_stime.tv_usec = 0; 1929 rup->ru_maxrss = 0; 1930 rup->ru_ixrss = 0; 1931 rup->ru_idrss = 0; 1932 rup->ru_isrss = 0; 1933 rup->ru_minflt = 0; 1934 rup->ru_majflt = 0; 1935 rup->ru_nswap = 0; 1936 rup->ru_inblock = 0; 1937 rup->ru_oublock = 0; 1938 rup->ru_msgsnd = 0; 1939 rup->ru_msgrcv = 0; 1940 rup->ru_nsignals = 0; 1941 rup->ru_nvcsw = 0; 1942 rup->ru_nivcsw = 0; 1943 1944 switch (who) { 1945 case OS::TGT_RUSAGE_SELF: 1946 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 1947 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 1948 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 1949 break; 1950 1951 case OS::TGT_RUSAGE_CHILDREN: 1952 // do nothing. We have no child processes, so they take no time. 1953 break; 1954 1955 default: 1956 // don't really handle THREAD or CHILDREN, but just warn and 1957 // plow ahead 1958 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 1959 who); 1960 } 1961 1962 rup.copyOut(tc->getMemProxy()); 1963 1964 return 0; 1965} 1966 1967/// Target times() function. 1968template <class OS> 1969SyscallReturn 1970timesFunc(SyscallDesc *desc, int callnum, Process *process, 1971 ThreadContext *tc) 1972{ 1973 int index = 0; 1974 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 1975 1976 // Fill in the time structure (in clocks) 1977 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 1978 bufp->tms_utime = clocks; 1979 bufp->tms_stime = 0; 1980 bufp->tms_cutime = 0; 1981 bufp->tms_cstime = 0; 1982 1983 // Convert to host endianness 1984 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 1985 1986 // Write back 1987 bufp.copyOut(tc->getMemProxy()); 1988 1989 // Return clock ticks since system boot 1990 return clocks; 1991} 1992 1993/// Target time() function. 1994template <class OS> 1995SyscallReturn 1996timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 1997{ 1998 typename OS::time_t sec, usec; 1999 getElapsedTimeMicro(sec, usec); 2000 sec += seconds_since_epoch; 2001 2002 int index = 0; 2003 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2004 if (taddr != 0) { 2005 typename OS::time_t t = sec; 2006 t = TheISA::htog(t); 2007 SETranslatingPortProxy &p = tc->getMemProxy(); 2008 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2009 } 2010 return sec; 2011} 2012 2013template <class OS> 2014SyscallReturn 2015tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 2016{ 2017 int index = 0; 2018 int tgid = process->getSyscallArg(tc, index); 2019 int tid = process->getSyscallArg(tc, index); 2020 int sig = process->getSyscallArg(tc, index); 2021 2022 /** 2023 * This system call is intended to allow killing a specific thread 2024 * within an arbitrary thread group if sanctioned with permission checks. 2025 * It's usually true that threads share the termination signal as pointed 2026 * out by the pthread_kill man page and this seems to be the intended 2027 * usage. Due to this being an emulated environment, assume the following: 2028 * Threads are allowed to call tgkill because the EUID for all threads 2029 * should be the same. There is no signal handling mechanism for kernel 2030 * registration of signal handlers since signals are poorly supported in 2031 * emulation mode. Since signal handlers cannot be registered, all 2032 * threads within in a thread group must share the termination signal. 2033 * We never exhaust PIDs so there's no chance of finding the wrong one 2034 * due to PID rollover. 2035 */ 2036 2037 System *sys = tc->getSystemPtr(); 2038 Process *tgt_proc = nullptr; 2039 for (int i = 0; i < sys->numContexts(); i++) { 2040 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2041 if (temp->pid() == tid) { 2042 tgt_proc = temp; 2043 break; 2044 } 2045 } 2046 2047 if (sig != 0 || sig != OS::TGT_SIGABRT) 2048 return -EINVAL; 2049 2050 if (tgt_proc == nullptr) 2051 return -ESRCH; 2052 2053 if (tgid != -1 && tgt_proc->tgid() != tgid) 2054 return -ESRCH; 2055 2056 if (sig == OS::TGT_SIGABRT) 2057 exitGroupFunc(desc, 252, process, tc); 2058 2059 return 0; 2060} 2061 2062 2063#endif // __SIM_SYSCALL_EMUL_HH__ 2064