syscall_emul.hh revision 12235
17405SAli.Saidi@ARM.com/* 27405SAli.Saidi@ARM.com * Copyright (c) 2012-2013, 2015 ARM Limited 37405SAli.Saidi@ARM.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 47405SAli.Saidi@ARM.com * All rights reserved 57405SAli.Saidi@ARM.com * 67405SAli.Saidi@ARM.com * The license below extends only to copyright in the software and shall 77405SAli.Saidi@ARM.com * not be construed as granting a license to any other intellectual 87405SAli.Saidi@ARM.com * property including but not limited to intellectual property relating 97405SAli.Saidi@ARM.com * to a hardware implementation of the functionality of the software 107405SAli.Saidi@ARM.com * licensed hereunder. You may use the software subject to the license 117405SAli.Saidi@ARM.com * terms below provided that you ensure that this notice is replicated 127405SAli.Saidi@ARM.com * unmodified and in its entirety in all distributions of the software, 137405SAli.Saidi@ARM.com * modified or unmodified, in source code or in binary form. 147405SAli.Saidi@ARM.com * 157405SAli.Saidi@ARM.com * Copyright (c) 2003-2005 The Regents of The University of Michigan 167405SAli.Saidi@ARM.com * All rights reserved. 177405SAli.Saidi@ARM.com * 187405SAli.Saidi@ARM.com * Redistribution and use in source and binary forms, with or without 197405SAli.Saidi@ARM.com * modification, are permitted provided that the following conditions are 207405SAli.Saidi@ARM.com * met: redistributions of source code must retain the above copyright 217405SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer; 227405SAli.Saidi@ARM.com * redistributions in binary form must reproduce the above copyright 237405SAli.Saidi@ARM.com * notice, this list of conditions and the following disclaimer in the 247405SAli.Saidi@ARM.com * documentation and/or other materials provided with the distribution; 257405SAli.Saidi@ARM.com * neither the name of the copyright holders nor the names of its 267405SAli.Saidi@ARM.com * contributors may be used to endorse or promote products derived from 277405SAli.Saidi@ARM.com * this software without specific prior written permission. 287405SAli.Saidi@ARM.com * 297405SAli.Saidi@ARM.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 307405SAli.Saidi@ARM.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 317405SAli.Saidi@ARM.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 327405SAli.Saidi@ARM.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 337405SAli.Saidi@ARM.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 347405SAli.Saidi@ARM.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 357405SAli.Saidi@ARM.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 367405SAli.Saidi@ARM.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 377405SAli.Saidi@ARM.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 387405SAli.Saidi@ARM.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 397405SAli.Saidi@ARM.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 407405SAli.Saidi@ARM.com * 417405SAli.Saidi@ARM.com * Authors: Steve Reinhardt 427678Sgblack@eecs.umich.edu * Kevin Lim 438059SAli.Saidi@ARM.com */ 447405SAli.Saidi@ARM.com 457405SAli.Saidi@ARM.com#ifndef __SIM_SYSCALL_EMUL_HH__ 467405SAli.Saidi@ARM.com#define __SIM_SYSCALL_EMUL_HH__ 477405SAli.Saidi@ARM.com 487427Sgblack@eecs.umich.edu#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 497427Sgblack@eecs.umich.edu defined(__FreeBSD__) || defined(__CYGWIN__) || \ 507427Sgblack@eecs.umich.edu defined(__NetBSD__)) 517427Sgblack@eecs.umich.edu#define NO_STAT64 1 527427Sgblack@eecs.umich.edu#else 537427Sgblack@eecs.umich.edu#define NO_STAT64 0 547427Sgblack@eecs.umich.edu#endif 557427Sgblack@eecs.umich.edu 567427Sgblack@eecs.umich.edu#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 577427Sgblack@eecs.umich.edu defined(__FreeBSD__) || defined(__NetBSD__)) 587427Sgblack@eecs.umich.edu#define NO_STATFS 1 597427Sgblack@eecs.umich.edu#else 607604SGene.Wu@arm.com#define NO_STATFS 0 617427Sgblack@eecs.umich.edu#endif 627427Sgblack@eecs.umich.edu 637427Sgblack@eecs.umich.edu#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 647427Sgblack@eecs.umich.edu defined(__FreeBSD__) || defined(__NetBSD__)) 657427Sgblack@eecs.umich.edu#define NO_FALLOCATE 1 667427Sgblack@eecs.umich.edu#else 677427Sgblack@eecs.umich.edu#define NO_FALLOCATE 0 687427Sgblack@eecs.umich.edu#endif 697427Sgblack@eecs.umich.edu 707427Sgblack@eecs.umich.edu/// 717427Sgblack@eecs.umich.edu/// @file syscall_emul.hh 727427Sgblack@eecs.umich.edu/// 737427Sgblack@eecs.umich.edu/// This file defines objects used to emulate syscalls from the target 747427Sgblack@eecs.umich.edu/// application on the host machine. 757427Sgblack@eecs.umich.edu 767427Sgblack@eecs.umich.edu#ifdef __CYGWIN32__ 777427Sgblack@eecs.umich.edu#include <sys/fcntl.h> 787427Sgblack@eecs.umich.edu 797645Sali.saidi@arm.com#endif 807645Sali.saidi@arm.com#include <fcntl.h> 817645Sali.saidi@arm.com#include <sys/mman.h> 827645Sali.saidi@arm.com#include <sys/stat.h> 837645Sali.saidi@arm.com#if (NO_STATFS == 0) 847427Sgblack@eecs.umich.edu#include <sys/statfs.h> 857427Sgblack@eecs.umich.edu#else 867427Sgblack@eecs.umich.edu#include <sys/mount.h> 877427Sgblack@eecs.umich.edu#endif 887427Sgblack@eecs.umich.edu#include <sys/time.h> 897427Sgblack@eecs.umich.edu#include <sys/uio.h> 907427Sgblack@eecs.umich.edu#include <unistd.h> 917427Sgblack@eecs.umich.edu 927427Sgblack@eecs.umich.edu#include <cerrno> 937427Sgblack@eecs.umich.edu#include <memory> 947427Sgblack@eecs.umich.edu#include <string> 957427Sgblack@eecs.umich.edu 967427Sgblack@eecs.umich.edu#include "arch/utility.hh" 977427Sgblack@eecs.umich.edu#include "base/intmath.hh" 987427Sgblack@eecs.umich.edu#include "base/loader/object_file.hh" 997427Sgblack@eecs.umich.edu#include "base/misc.hh" 1007427Sgblack@eecs.umich.edu#include "base/trace.hh" 1017427Sgblack@eecs.umich.edu#include "base/types.hh" 1027427Sgblack@eecs.umich.edu#include "config/the_isa.hh" 1037427Sgblack@eecs.umich.edu#include "cpu/base.hh" 1047427Sgblack@eecs.umich.edu#include "cpu/thread_context.hh" 1057427Sgblack@eecs.umich.edu#include "mem/page_table.hh" 1067427Sgblack@eecs.umich.edu#include "params/Process.hh" 1077427Sgblack@eecs.umich.edu#include "sim/emul_driver.hh" 1087427Sgblack@eecs.umich.edu#include "sim/futex_map.hh" 1097427Sgblack@eecs.umich.edu#include "sim/process.hh" 1107427Sgblack@eecs.umich.edu#include "sim/syscall_debug_macros.hh" 1117427Sgblack@eecs.umich.edu#include "sim/syscall_desc.hh" 1127436Sdam.sunwoo@arm.com#include "sim/syscall_emul_buf.hh" 1137436Sdam.sunwoo@arm.com#include "sim/syscall_return.hh" 1147436Sdam.sunwoo@arm.com 1157436Sdam.sunwoo@arm.com////////////////////////////////////////////////////////////////////// 1167436Sdam.sunwoo@arm.com// 1177436Sdam.sunwoo@arm.com// The following emulation functions are generic enough that they 1187436Sdam.sunwoo@arm.com// don't need to be recompiled for different emulated OS's. They are 1197436Sdam.sunwoo@arm.com// defined in sim/syscall_emul.cc. 1207436Sdam.sunwoo@arm.com// 1217436Sdam.sunwoo@arm.com////////////////////////////////////////////////////////////////////// 1227436Sdam.sunwoo@arm.com 1237436Sdam.sunwoo@arm.com 1247436Sdam.sunwoo@arm.com/// Handler for unimplemented syscalls that we haven't thought about. 1257436Sdam.sunwoo@arm.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 1267436Sdam.sunwoo@arm.com Process *p, ThreadContext *tc); 1277436Sdam.sunwoo@arm.com 1287436Sdam.sunwoo@arm.com/// Handler for unimplemented syscalls that we never intend to 1297436Sdam.sunwoo@arm.com/// implement (signal handling, etc.) and should not affect the correct 1307436Sdam.sunwoo@arm.com/// behavior of the program. Print a warning only if the appropriate 1317436Sdam.sunwoo@arm.com/// trace flag is enabled. Return success to the target program. 1327436Sdam.sunwoo@arm.comSyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1337436Sdam.sunwoo@arm.com Process *p, ThreadContext *tc); 1347436Sdam.sunwoo@arm.com 1357436Sdam.sunwoo@arm.com// Target fallocateFunc() handler. 1367436Sdam.sunwoo@arm.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 1377436Sdam.sunwoo@arm.com Process *p, ThreadContext *tc); 1387436Sdam.sunwoo@arm.com 1397436Sdam.sunwoo@arm.com/// Target exit() handler: terminate current context. 1407436Sdam.sunwoo@arm.comSyscallReturn exitFunc(SyscallDesc *desc, int num, 1417436Sdam.sunwoo@arm.com Process *p, ThreadContext *tc); 1427436Sdam.sunwoo@arm.com 1437436Sdam.sunwoo@arm.com/// Target exit_group() handler: terminate simulation. (exit all threads) 1447644Sali.saidi@arm.comSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1457644Sali.saidi@arm.com Process *p, ThreadContext *tc); 1468147SAli.Saidi@ARM.com 1478147SAli.Saidi@ARM.com/// Target set_tid_address() handler. 1488147SAli.Saidi@ARM.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1498147SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1508147SAli.Saidi@ARM.com 1518147SAli.Saidi@ARM.com/// Target getpagesize() handler. 1528147SAli.Saidi@ARM.comSyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1538147SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1548147SAli.Saidi@ARM.com 1558147SAli.Saidi@ARM.com/// Target brk() handler: set brk address. 1567427Sgblack@eecs.umich.eduSyscallReturn brkFunc(SyscallDesc *desc, int num, 1577427Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 1587427Sgblack@eecs.umich.edu 1597405SAli.Saidi@ARM.com/// Target close() handler. 1607405SAli.Saidi@ARM.comSyscallReturn closeFunc(SyscallDesc *desc, int num, 1617405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1627405SAli.Saidi@ARM.com 1637614Sminkyu.jeong@arm.com// Target read() handler. 1647614Sminkyu.jeong@arm.comSyscallReturn readFunc(SyscallDesc *desc, int num, 1657614Sminkyu.jeong@arm.com Process *p, ThreadContext *tc); 1667614Sminkyu.jeong@arm.com 1677614Sminkyu.jeong@arm.com/// Target write() handler. 1687614Sminkyu.jeong@arm.comSyscallReturn writeFunc(SyscallDesc *desc, int num, 1697614Sminkyu.jeong@arm.com Process *p, ThreadContext *tc); 1707614Sminkyu.jeong@arm.com 1717614Sminkyu.jeong@arm.com/// Target lseek() handler. 1727614Sminkyu.jeong@arm.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 1737614Sminkyu.jeong@arm.com Process *p, ThreadContext *tc); 1747405SAli.Saidi@ARM.com 1757405SAli.Saidi@ARM.com/// Target _llseek() handler. 1767405SAli.Saidi@ARM.comSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1777405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1787405SAli.Saidi@ARM.com 1797405SAli.Saidi@ARM.com/// Target munmap() handler. 1807405SAli.Saidi@ARM.comSyscallReturn munmapFunc(SyscallDesc *desc, int num, 1817405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1827720Sgblack@eecs.umich.edu 1837720Sgblack@eecs.umich.edu/// Target gethostname() handler. 1847720Sgblack@eecs.umich.eduSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 1857405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1867405SAli.Saidi@ARM.com 1877757SAli.Saidi@ARM.com/// Target getcwd() handler. 1887405SAli.Saidi@ARM.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1897405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1907757SAli.Saidi@ARM.com 1917405SAli.Saidi@ARM.com/// Target readlink() handler. 1927405SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1937731SAli.Saidi@ARM.com Process *p, ThreadContext *tc, 1947405SAli.Saidi@ARM.com int index = 0); 1957405SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1967731SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 1977405SAli.Saidi@ARM.com 1987405SAli.Saidi@ARM.com/// Target unlink() handler. 1997405SAli.Saidi@ARM.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2007588SAli.Saidi@arm.com Process *p, ThreadContext *tc, 2017588SAli.Saidi@arm.com int index); 2027588SAli.Saidi@arm.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2037583SAli.Saidi@arm.com Process *p, ThreadContext *tc); 2047583SAli.Saidi@arm.com 2057583SAli.Saidi@arm.com/// Target mkdir() handler. 2067583SAli.Saidi@arm.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2077583SAli.Saidi@arm.com Process *p, ThreadContext *tc); 2087583SAli.Saidi@arm.com 2097583SAli.Saidi@arm.com/// Target rename() handler. 2107583SAli.Saidi@arm.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 2117583SAli.Saidi@arm.com Process *p, ThreadContext *tc); 2127583SAli.Saidi@arm.com 2137583SAli.Saidi@arm.com 2147583SAli.Saidi@arm.com/// Target truncate() handler. 2157583SAli.Saidi@arm.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2167783SGiacomo.Gabrielli@arm.com Process *p, ThreadContext *tc); 2177783SGiacomo.Gabrielli@arm.com 2187783SGiacomo.Gabrielli@arm.com 2197783SGiacomo.Gabrielli@arm.com/// Target ftruncate() handler. 2207405SAli.Saidi@ARM.comSyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2217405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 2227405SAli.Saidi@ARM.com 2237405SAli.Saidi@ARM.com 2247405SAli.Saidi@ARM.com/// Target truncate64() handler. 2257405SAli.Saidi@ARM.comSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2267405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 2277405SAli.Saidi@ARM.com 2287614Sminkyu.jeong@arm.com/// Target ftruncate64() handler. 2297614Sminkyu.jeong@arm.comSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2307614Sminkyu.jeong@arm.com Process *p, ThreadContext *tc); 2317614Sminkyu.jeong@arm.com 2327614Sminkyu.jeong@arm.com 2337614Sminkyu.jeong@arm.com/// Target umask() handler. 2347614Sminkyu.jeong@arm.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2357614Sminkyu.jeong@arm.com Process *p, ThreadContext *tc); 2367614Sminkyu.jeong@arm.com 2377614Sminkyu.jeong@arm.com/// Target gettid() handler. 2387405SAli.Saidi@ARM.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 2397405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 2407405SAli.Saidi@ARM.com 2417405SAli.Saidi@ARM.com/// Target chown() handler. 2427405SAli.Saidi@ARM.comSyscallReturn chownFunc(SyscallDesc *desc, int num, 2437749SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 2447405SAli.Saidi@ARM.com 2457405SAli.Saidi@ARM.com/// Target setpgid() handler. 2467405SAli.Saidi@ARM.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2477749SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 2487749SAli.Saidi@ARM.com 2497749SAli.Saidi@ARM.com/// Target fchown() handler. 2507749SAli.Saidi@ARM.comSyscallReturn fchownFunc(SyscallDesc *desc, int num, 2517405SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 2527749SAli.Saidi@ARM.com 2537749SAli.Saidi@ARM.com/// Target dup() handler. 2547749SAli.Saidi@ARM.comSyscallReturn dupFunc(SyscallDesc *desc, int num, 2557749SAli.Saidi@ARM.com Process *process, ThreadContext *tc); 2567749SAli.Saidi@ARM.com 2577614Sminkyu.jeong@arm.com/// Target dup2() handler. 2587614Sminkyu.jeong@arm.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 2597720Sgblack@eecs.umich.edu Process *process, ThreadContext *tc); 2607720Sgblack@eecs.umich.edu 2617720Sgblack@eecs.umich.edu/// Target fcntl() handler. 2627720Sgblack@eecs.umich.eduSyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2637408Sgblack@eecs.umich.edu Process *process, ThreadContext *tc); 2647405SAli.Saidi@ARM.com 2657405SAli.Saidi@ARM.com/// Target fcntl64() handler. 2667405SAli.Saidi@ARM.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2677408Sgblack@eecs.umich.edu Process *process, ThreadContext *tc); 2687408Sgblack@eecs.umich.edu 2697408Sgblack@eecs.umich.edu/// Target setuid() handler. 2707408Sgblack@eecs.umich.eduSyscallReturn setuidFunc(SyscallDesc *desc, int num, 2718206SWilliam.Wang@arm.com Process *p, ThreadContext *tc); 2728206SWilliam.Wang@arm.com 2738206SWilliam.Wang@arm.com/// Target pipe() handler. 2748206SWilliam.Wang@arm.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 2758206SWilliam.Wang@arm.com Process *p, ThreadContext *tc); 2768206SWilliam.Wang@arm.com 2778206SWilliam.Wang@arm.com/// Internal pipe() handler. 2788206SWilliam.Wang@arm.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 2798206SWilliam.Wang@arm.com ThreadContext *tc, bool pseudoPipe); 2808206SWilliam.Wang@arm.com 2818206SWilliam.Wang@arm.com/// Target getpid() handler. 2827408Sgblack@eecs.umich.eduSyscallReturn getpidFunc(SyscallDesc *desc, int num, 2837408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2847408Sgblack@eecs.umich.edu 2857731SAli.Saidi@ARM.com/// Target getuid() handler. 2868206SWilliam.Wang@arm.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, 2877408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2887408Sgblack@eecs.umich.edu 2897408Sgblack@eecs.umich.edu/// Target getgid() handler. 2907408Sgblack@eecs.umich.eduSyscallReturn getgidFunc(SyscallDesc *desc, int num, 2917408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2927408Sgblack@eecs.umich.edu 2937408Sgblack@eecs.umich.edu/// Target getppid() handler. 2947408Sgblack@eecs.umich.eduSyscallReturn getppidFunc(SyscallDesc *desc, int num, 2957408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 2967408Sgblack@eecs.umich.edu 2977408Sgblack@eecs.umich.edu/// Target geteuid() handler. 2987408Sgblack@eecs.umich.eduSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 2997408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 3007408Sgblack@eecs.umich.edu 3017408Sgblack@eecs.umich.edu/// Target getegid() handler. 3027408Sgblack@eecs.umich.eduSyscallReturn getegidFunc(SyscallDesc *desc, int num, 3037408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 3047408Sgblack@eecs.umich.edu 3057408Sgblack@eecs.umich.edu/// Target access() handler 3067408Sgblack@eecs.umich.eduSyscallReturn accessFunc(SyscallDesc *desc, int num, 3077408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 3087408Sgblack@eecs.umich.eduSyscallReturn accessFunc(SyscallDesc *desc, int num, 3097408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc, 3107408Sgblack@eecs.umich.edu int index); 3117408Sgblack@eecs.umich.edu 3127783SGiacomo.Gabrielli@arm.com/// Futex system call 3137783SGiacomo.Gabrielli@arm.com/// Implemented by Daniel Sanchez 3147783SGiacomo.Gabrielli@arm.com/// Used by printf's in multi-threaded apps 3157783SGiacomo.Gabrielli@arm.comtemplate <class OS> 3167783SGiacomo.Gabrielli@arm.comSyscallReturn 3177783SGiacomo.Gabrielli@arm.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3187783SGiacomo.Gabrielli@arm.com ThreadContext *tc) 3197783SGiacomo.Gabrielli@arm.com{ 3207783SGiacomo.Gabrielli@arm.com using namespace std; 3217783SGiacomo.Gabrielli@arm.com 3227783SGiacomo.Gabrielli@arm.com int index = 0; 3237783SGiacomo.Gabrielli@arm.com Addr uaddr = process->getSyscallArg(tc, index); 3247408Sgblack@eecs.umich.edu int op = process->getSyscallArg(tc, index); 3257408Sgblack@eecs.umich.edu int val = process->getSyscallArg(tc, index); 3268206SWilliam.Wang@arm.com 3278206SWilliam.Wang@arm.com /* 3287408Sgblack@eecs.umich.edu * Unsupported option that does not affect the correctness of the 3297408Sgblack@eecs.umich.edu * application. This is a performance optimization utilized by Linux. 3307408Sgblack@eecs.umich.edu */ 3317408Sgblack@eecs.umich.edu op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3327408Sgblack@eecs.umich.edu 3337408Sgblack@eecs.umich.edu FutexMap &futex_map = tc->getSystemPtr()->futexMap; 3347408Sgblack@eecs.umich.edu 3357408Sgblack@eecs.umich.edu if (OS::TGT_FUTEX_WAIT == op) { 3367408Sgblack@eecs.umich.edu // Ensure futex system call accessed atomically. 3377408Sgblack@eecs.umich.edu BufferArg buf(uaddr, sizeof(int)); 3387408Sgblack@eecs.umich.edu buf.copyIn(tc->getMemProxy()); 3397408Sgblack@eecs.umich.edu int mem_val = *(int*)buf.bufferPtr(); 3407749SAli.Saidi@ARM.com 3417749SAli.Saidi@ARM.com /* 3427408Sgblack@eecs.umich.edu * The value in memory at uaddr is not equal with the expected val 3437408Sgblack@eecs.umich.edu * (a different thread must have changed it before the system call was 3447408Sgblack@eecs.umich.edu * invoked). In this case, we need to throw an error. 3457408Sgblack@eecs.umich.edu */ 3467408Sgblack@eecs.umich.edu if (val != mem_val) 3477408Sgblack@eecs.umich.edu return -OS::TGT_EWOULDBLOCK; 3487408Sgblack@eecs.umich.edu 3497408Sgblack@eecs.umich.edu futex_map.suspend(uaddr, process->tgid(), tc); 3507408Sgblack@eecs.umich.edu 3517408Sgblack@eecs.umich.edu return 0; 3527731SAli.Saidi@ARM.com } else if (OS::TGT_FUTEX_WAKE == op) { 3537408Sgblack@eecs.umich.edu return futex_map.wakeup(uaddr, process->tgid(), val); 3547408Sgblack@eecs.umich.edu } 3557408Sgblack@eecs.umich.edu 3567408Sgblack@eecs.umich.edu warn("futex: op %d not implemented; ignoring.", op); 3577408Sgblack@eecs.umich.edu return -ENOSYS; 3587408Sgblack@eecs.umich.edu} 3597408Sgblack@eecs.umich.edu 3607408Sgblack@eecs.umich.edu 3617408Sgblack@eecs.umich.edu/// Pseudo Funcs - These functions use a different return convension, 3627408Sgblack@eecs.umich.edu/// returning a second value in a register other than the normal return register 3637408Sgblack@eecs.umich.eduSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 3647731SAli.Saidi@ARM.com Process *process, ThreadContext *tc); 3657408Sgblack@eecs.umich.edu 3667408Sgblack@eecs.umich.edu/// Target getpidPseudo() handler. 3677408Sgblack@eecs.umich.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 3687408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 3697408Sgblack@eecs.umich.edu 3707408Sgblack@eecs.umich.edu/// Target getuidPseudo() handler. 3717408Sgblack@eecs.umich.eduSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 3727731SAli.Saidi@ARM.com Process *p, ThreadContext *tc); 3737408Sgblack@eecs.umich.edu 3747408Sgblack@eecs.umich.edu/// Target getgidPseudo() handler. 3757408Sgblack@eecs.umich.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 3767408Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 3777408Sgblack@eecs.umich.edu 3787731SAli.Saidi@ARM.com 3797408Sgblack@eecs.umich.edu/// A readable name for 1,000,000, for converting microseconds to seconds. 3807408Sgblack@eecs.umich.educonst int one_million = 1000000; 3817408Sgblack@eecs.umich.edu/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 3827408Sgblack@eecs.umich.educonst int one_billion = 1000000000; 3837408Sgblack@eecs.umich.edu 3847408Sgblack@eecs.umich.edu/// Approximate seconds since the epoch (1/1/1970). About a billion, 3857408Sgblack@eecs.umich.edu/// by my reckoning. We want to keep this a constant (not use the 3867408Sgblack@eecs.umich.edu/// real-world time) to keep simulations repeatable. 3877408Sgblack@eecs.umich.educonst unsigned seconds_since_epoch = 1000000000; 3887408Sgblack@eecs.umich.edu 3897408Sgblack@eecs.umich.edu/// Helper function to convert current elapsed time to seconds and 3907408Sgblack@eecs.umich.edu/// microseconds. 3917408Sgblack@eecs.umich.edutemplate <class T1, class T2> 3927408Sgblack@eecs.umich.eduvoid 3937408Sgblack@eecs.umich.edugetElapsedTimeMicro(T1 &sec, T2 &usec) 3947408Sgblack@eecs.umich.edu{ 3957405SAli.Saidi@ARM.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 3967583SAli.Saidi@arm.com sec = elapsed_usecs / one_million; 3977583SAli.Saidi@arm.com usec = elapsed_usecs % one_million; 3987583SAli.Saidi@arm.com} 3997583SAli.Saidi@arm.com 4008059SAli.Saidi@ARM.com/// Helper function to convert current elapsed time to seconds and 4018059SAli.Saidi@ARM.com/// nanoseconds. 4028059SAli.Saidi@ARM.comtemplate <class T1, class T2> 4038059SAli.Saidi@ARM.comvoid 4048059SAli.Saidi@ARM.comgetElapsedTimeNano(T1 &sec, T2 &nsec) 4058059SAli.Saidi@ARM.com{ 4068059SAli.Saidi@ARM.com uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 4078059SAli.Saidi@ARM.com sec = elapsed_nsecs / one_billion; 4088059SAli.Saidi@ARM.com nsec = elapsed_nsecs % one_billion; 4098059SAli.Saidi@ARM.com} 4108059SAli.Saidi@ARM.com 4118059SAli.Saidi@ARM.com////////////////////////////////////////////////////////////////////// 4127583SAli.Saidi@arm.com// 4137583SAli.Saidi@arm.com// The following emulation functions are generic, but need to be 4147583SAli.Saidi@arm.com// templated to account for differences in types, constants, etc. 4157583SAli.Saidi@arm.com// 4167583SAli.Saidi@arm.com////////////////////////////////////////////////////////////////////// 4177436Sdam.sunwoo@arm.com 4187436Sdam.sunwoo@arm.com typedef struct statfs hst_statfs; 4197436Sdam.sunwoo@arm.com#if NO_STAT64 4207436Sdam.sunwoo@arm.com typedef struct stat hst_stat; 4217436Sdam.sunwoo@arm.com typedef struct stat hst_stat64; 4227436Sdam.sunwoo@arm.com#else 4237436Sdam.sunwoo@arm.com typedef struct stat hst_stat; 4247436Sdam.sunwoo@arm.com typedef struct stat64 hst_stat64; 4257436Sdam.sunwoo@arm.com#endif 4267436Sdam.sunwoo@arm.com 4277436Sdam.sunwoo@arm.com//// Helper function to convert a host stat buffer to a target stat 4287436Sdam.sunwoo@arm.com//// buffer. Also copies the target buffer out to the simulated 4297436Sdam.sunwoo@arm.com//// memory space. Used by stat(), fstat(), and lstat(). 4307436Sdam.sunwoo@arm.com 4317436Sdam.sunwoo@arm.comtemplate <typename target_stat, typename host_stat> 4327436Sdam.sunwoo@arm.comvoid 4337436Sdam.sunwoo@arm.comconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 4347436Sdam.sunwoo@arm.com{ 4357436Sdam.sunwoo@arm.com using namespace TheISA; 4367436Sdam.sunwoo@arm.com 4377436Sdam.sunwoo@arm.com if (fakeTTY) 4387436Sdam.sunwoo@arm.com tgt->st_dev = 0xA; 4397436Sdam.sunwoo@arm.com else 4407436Sdam.sunwoo@arm.com tgt->st_dev = host->st_dev; 4417436Sdam.sunwoo@arm.com tgt->st_dev = TheISA::htog(tgt->st_dev); 4427436Sdam.sunwoo@arm.com tgt->st_ino = host->st_ino; 4437436Sdam.sunwoo@arm.com tgt->st_ino = TheISA::htog(tgt->st_ino); 4447436Sdam.sunwoo@arm.com tgt->st_mode = host->st_mode; 4457436Sdam.sunwoo@arm.com if (fakeTTY) { 4467436Sdam.sunwoo@arm.com // Claim to be a character device 4477442Ssaidi@eecs.umich.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4487436Sdam.sunwoo@arm.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4497436Sdam.sunwoo@arm.com } 4508208SAli.Saidi@ARM.com tgt->st_mode = TheISA::htog(tgt->st_mode); 4517720Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 4527436Sdam.sunwoo@arm.com tgt->st_nlink = TheISA::htog(tgt->st_nlink); 4537436Sdam.sunwoo@arm.com tgt->st_uid = host->st_uid; 4547436Sdam.sunwoo@arm.com tgt->st_uid = TheISA::htog(tgt->st_uid); 4557436Sdam.sunwoo@arm.com tgt->st_gid = host->st_gid; 4567436Sdam.sunwoo@arm.com tgt->st_gid = TheISA::htog(tgt->st_gid); 4577436Sdam.sunwoo@arm.com if (fakeTTY) 4587436Sdam.sunwoo@arm.com tgt->st_rdev = 0x880d; 4597436Sdam.sunwoo@arm.com else 4607436Sdam.sunwoo@arm.com tgt->st_rdev = host->st_rdev; 4617436Sdam.sunwoo@arm.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 4627436Sdam.sunwoo@arm.com tgt->st_size = host->st_size; 4637436Sdam.sunwoo@arm.com tgt->st_size = TheISA::htog(tgt->st_size); 4647436Sdam.sunwoo@arm.com tgt->st_atimeX = host->st_atime; 4657436Sdam.sunwoo@arm.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4667436Sdam.sunwoo@arm.com tgt->st_mtimeX = host->st_mtime; 4677436Sdam.sunwoo@arm.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4687436Sdam.sunwoo@arm.com tgt->st_ctimeX = host->st_ctime; 4697436Sdam.sunwoo@arm.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4707436Sdam.sunwoo@arm.com // Force the block size to be 8KB. This helps to ensure buffered io works 4717436Sdam.sunwoo@arm.com // consistently across different hosts. 4727749SAli.Saidi@ARM.com tgt->st_blksize = 0x2000; 4737749SAli.Saidi@ARM.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4747749SAli.Saidi@ARM.com tgt->st_blocks = host->st_blocks; 4757749SAli.Saidi@ARM.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4767749SAli.Saidi@ARM.com} 4777749SAli.Saidi@ARM.com 4787749SAli.Saidi@ARM.com// Same for stat64 4798208SAli.Saidi@ARM.com 4808208SAli.Saidi@ARM.comtemplate <typename target_stat, typename host_stat64> 4818208SAli.Saidi@ARM.comvoid 4828208SAli.Saidi@ARM.comconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 4838208SAli.Saidi@ARM.com{ 4848208SAli.Saidi@ARM.com using namespace TheISA; 4858208SAli.Saidi@ARM.com 4867405SAli.Saidi@ARM.com convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 4877405SAli.Saidi@ARM.com#if defined(STAT_HAVE_NSEC) 4887405SAli.Saidi@ARM.com tgt->st_atime_nsec = host->st_atime_nsec; 4897405SAli.Saidi@ARM.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 4907405SAli.Saidi@ARM.com tgt->st_mtime_nsec = host->st_mtime_nsec; 4917405SAli.Saidi@ARM.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 492 tgt->st_ctime_nsec = host->st_ctime_nsec; 493 tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 494#else 495 tgt->st_atime_nsec = 0; 496 tgt->st_mtime_nsec = 0; 497 tgt->st_ctime_nsec = 0; 498#endif 499} 500 501// Here are a couple of convenience functions 502template<class OS> 503void 504copyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 505 hst_stat *host, bool fakeTTY = false) 506{ 507 typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 508 tgt_stat_buf tgt(addr); 509 convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 510 tgt.copyOut(mem); 511} 512 513template<class OS> 514void 515copyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 516 hst_stat64 *host, bool fakeTTY = false) 517{ 518 typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 519 tgt_stat_buf tgt(addr); 520 convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 521 tgt.copyOut(mem); 522} 523 524template <class OS> 525void 526copyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 527 hst_statfs *host) 528{ 529 TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 530 531 tgt->f_type = TheISA::htog(host->f_type); 532#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 533 tgt->f_bsize = TheISA::htog(host->f_iosize); 534#else 535 tgt->f_bsize = TheISA::htog(host->f_bsize); 536#endif 537 tgt->f_blocks = TheISA::htog(host->f_blocks); 538 tgt->f_bfree = TheISA::htog(host->f_bfree); 539 tgt->f_bavail = TheISA::htog(host->f_bavail); 540 tgt->f_files = TheISA::htog(host->f_files); 541 tgt->f_ffree = TheISA::htog(host->f_ffree); 542 memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 543#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 544 tgt->f_namelen = TheISA::htog(host->f_namemax); 545 tgt->f_frsize = TheISA::htog(host->f_bsize); 546#elif defined(__APPLE__) 547 tgt->f_namelen = 0; 548 tgt->f_frsize = 0; 549#else 550 tgt->f_namelen = TheISA::htog(host->f_namelen); 551 tgt->f_frsize = TheISA::htog(host->f_frsize); 552#endif 553#if defined(__linux__) 554 memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 555#else 556 /* 557 * The fields are different sizes per OS. Don't bother with 558 * f_spare or f_reserved on non-Linux for now. 559 */ 560 memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 561#endif 562 563 tgt.copyOut(mem); 564} 565 566/// Target ioctl() handler. For the most part, programs call ioctl() 567/// only to find out if their stdout is a tty, to determine whether to 568/// do line or block buffering. We always claim that output fds are 569/// not TTYs to provide repeatable results. 570template <class OS> 571SyscallReturn 572ioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 573{ 574 int index = 0; 575 int tgt_fd = p->getSyscallArg(tc, index); 576 unsigned req = p->getSyscallArg(tc, index); 577 578 DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 579 580 if (OS::isTtyReq(req)) 581 return -ENOTTY; 582 583 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 584 if (!dfdp) 585 return -EBADF; 586 587 /** 588 * If the driver is valid, issue the ioctl through it. Otherwise, 589 * there's an implicit assumption that the device is a TTY type and we 590 * return that we do not have a valid TTY. 591 */ 592 EmulatedDriver *emul_driver = dfdp->getDriver(); 593 if (emul_driver) 594 return emul_driver->ioctl(p, tc, req); 595 596 /** 597 * For lack of a better return code, return ENOTTY. Ideally, we should 598 * return something better here, but at least we issue the warning. 599 */ 600 warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 601 tgt_fd, req, tc->pcState()); 602 return -ENOTTY; 603} 604 605template <class OS> 606SyscallReturn 607openImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 608 bool isopenat) 609{ 610 int index = 0; 611 int tgt_dirfd = -1; 612 613 /** 614 * If using the openat variant, read in the target directory file 615 * descriptor from the simulated process. 616 */ 617 if (isopenat) 618 tgt_dirfd = p->getSyscallArg(tc, index); 619 620 /** 621 * Retrieve the simulated process' memory proxy and then read in the path 622 * string from that memory space into the host's working memory space. 623 */ 624 std::string path; 625 if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 626 return -EFAULT; 627 628#ifdef __CYGWIN32__ 629 int host_flags = O_BINARY; 630#else 631 int host_flags = 0; 632#endif 633 /** 634 * Translate target flags into host flags. Flags exist which are not 635 * ported between architectures which can cause check failures. 636 */ 637 int tgt_flags = p->getSyscallArg(tc, index); 638 for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 639 if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 640 tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 641 host_flags |= OS::openFlagTable[i].hostFlag; 642 } 643 } 644 if (tgt_flags) { 645 warn("open%s: cannot decode flags 0x%x", 646 isopenat ? "at" : "", tgt_flags); 647 } 648#ifdef __CYGWIN32__ 649 host_flags |= O_BINARY; 650#endif 651 652 int mode = p->getSyscallArg(tc, index); 653 654 /** 655 * If the simulated process called open or openat with AT_FDCWD specified, 656 * take the current working directory value which was passed into the 657 * process class as a Python parameter and append the current path to 658 * create a full path. 659 * Otherwise, openat with a valid target directory file descriptor has 660 * been called. If the path option, which was passed in as a parameter, 661 * is not absolute, retrieve the directory file descriptor's path and 662 * prepend it to the path passed in as a parameter. 663 * In every case, we should have a full path (which is relevant to the 664 * host) to work with after this block has been passed. 665 */ 666 if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 667 path = p->fullPath(path); 668 } else if (!startswith(path, "/")) { 669 std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 670 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 671 if (!ffdp) 672 return -EBADF; 673 path.insert(0, ffdp->getFileName()); 674 } 675 676 /** 677 * Since this is an emulated environment, we create pseudo file 678 * descriptors for device requests that have been registered with 679 * the process class through Python; this allows us to create a file 680 * descriptor for subsequent ioctl or mmap calls. 681 */ 682 if (startswith(path, "/dev/")) { 683 std::string filename = path.substr(strlen("/dev/")); 684 EmulatedDriver *drv = p->findDriver(filename); 685 if (drv) { 686 DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 687 "driver open with path[%s]\n", 688 isopenat ? "at" : "", path.c_str()); 689 return drv->open(p, tc, mode, host_flags); 690 } 691 /** 692 * Fall through here for pass through to host devices, such 693 * as /dev/zero 694 */ 695 } 696 697 /** 698 * Some special paths and files cannot be called on the host and need 699 * to be handled as special cases inside the simulator. 700 * If the full path that was created above does not match any of the 701 * special cases, pass it through to the open call on the host to let 702 * the host open the file on our behalf. 703 * If the host cannot open the file, return the host's error code back 704 * through the system call to the simulated process. 705 */ 706 int sim_fd = -1; 707 std::vector<std::string> special_paths = 708 { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 709 for (auto entry : special_paths) { 710 if (startswith(path, entry)) 711 sim_fd = OS::openSpecialFile(path, p, tc); 712 } 713 if (sim_fd == -1) { 714 sim_fd = open(path.c_str(), host_flags, mode); 715 } 716 if (sim_fd == -1) { 717 int local = -errno; 718 DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 719 isopenat ? "at" : "", path.c_str()); 720 return local; 721 } 722 723 /** 724 * The file was opened successfully and needs to be recorded in the 725 * process' file descriptor array so that it can be retrieved later. 726 * The target file descriptor that is chosen will be the lowest unused 727 * file descriptor. 728 * Return the indirect target file descriptor back to the simulated 729 * process to act as a handle for the opened file. 730 */ 731 auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 732 int tgt_fd = p->fds->allocFD(ffdp); 733 DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 734 isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 735 return tgt_fd; 736} 737 738/// Target open() handler. 739template <class OS> 740SyscallReturn 741openFunc(SyscallDesc *desc, int callnum, Process *process, 742 ThreadContext *tc) 743{ 744 return openImpl<OS>(desc, callnum, process, tc, false); 745} 746 747/// Target openat() handler. 748template <class OS> 749SyscallReturn 750openatFunc(SyscallDesc *desc, int callnum, Process *process, 751 ThreadContext *tc) 752{ 753 return openImpl<OS>(desc, callnum, process, tc, true); 754} 755 756/// Target unlinkat() handler. 757template <class OS> 758SyscallReturn 759unlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 760 ThreadContext *tc) 761{ 762 int index = 0; 763 int dirfd = process->getSyscallArg(tc, index); 764 if (dirfd != OS::TGT_AT_FDCWD) 765 warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 766 767 return unlinkHelper(desc, callnum, process, tc, 1); 768} 769 770/// Target facessat() handler 771template <class OS> 772SyscallReturn 773faccessatFunc(SyscallDesc *desc, int callnum, Process *process, 774 ThreadContext *tc) 775{ 776 int index = 0; 777 int dirfd = process->getSyscallArg(tc, index); 778 if (dirfd != OS::TGT_AT_FDCWD) 779 warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 780 return accessFunc(desc, callnum, process, tc, 1); 781} 782 783/// Target readlinkat() handler 784template <class OS> 785SyscallReturn 786readlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 787 ThreadContext *tc) 788{ 789 int index = 0; 790 int dirfd = process->getSyscallArg(tc, index); 791 if (dirfd != OS::TGT_AT_FDCWD) 792 warn("openat: first argument not AT_FDCWD; unlikely to work"); 793 return readlinkFunc(desc, callnum, process, tc, 1); 794} 795 796/// Target renameat() handler. 797template <class OS> 798SyscallReturn 799renameatFunc(SyscallDesc *desc, int callnum, Process *process, 800 ThreadContext *tc) 801{ 802 int index = 0; 803 804 int olddirfd = process->getSyscallArg(tc, index); 805 if (olddirfd != OS::TGT_AT_FDCWD) 806 warn("renameat: first argument not AT_FDCWD; unlikely to work"); 807 808 std::string old_name; 809 810 if (!tc->getMemProxy().tryReadString(old_name, 811 process->getSyscallArg(tc, index))) 812 return -EFAULT; 813 814 int newdirfd = process->getSyscallArg(tc, index); 815 if (newdirfd != OS::TGT_AT_FDCWD) 816 warn("renameat: third argument not AT_FDCWD; unlikely to work"); 817 818 std::string new_name; 819 820 if (!tc->getMemProxy().tryReadString(new_name, 821 process->getSyscallArg(tc, index))) 822 return -EFAULT; 823 824 // Adjust path for current working directory 825 old_name = process->fullPath(old_name); 826 new_name = process->fullPath(new_name); 827 828 int result = rename(old_name.c_str(), new_name.c_str()); 829 return (result == -1) ? -errno : result; 830} 831 832/// Target sysinfo() handler. 833template <class OS> 834SyscallReturn 835sysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 836 ThreadContext *tc) 837{ 838 839 int index = 0; 840 TypedBufferArg<typename OS::tgt_sysinfo> 841 sysinfo(process->getSyscallArg(tc, index)); 842 843 sysinfo->uptime = seconds_since_epoch; 844 sysinfo->totalram = process->system->memSize(); 845 sysinfo->mem_unit = 1; 846 847 sysinfo.copyOut(tc->getMemProxy()); 848 849 return 0; 850} 851 852/// Target chmod() handler. 853template <class OS> 854SyscallReturn 855chmodFunc(SyscallDesc *desc, int callnum, Process *process, 856 ThreadContext *tc) 857{ 858 std::string path; 859 860 int index = 0; 861 if (!tc->getMemProxy().tryReadString(path, 862 process->getSyscallArg(tc, index))) { 863 return -EFAULT; 864 } 865 866 uint32_t mode = process->getSyscallArg(tc, index); 867 mode_t hostMode = 0; 868 869 // XXX translate mode flags via OS::something??? 870 hostMode = mode; 871 872 // Adjust path for current working directory 873 path = process->fullPath(path); 874 875 // do the chmod 876 int result = chmod(path.c_str(), hostMode); 877 if (result < 0) 878 return -errno; 879 880 return 0; 881} 882 883 884/// Target fchmod() handler. 885template <class OS> 886SyscallReturn 887fchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 888{ 889 int index = 0; 890 int tgt_fd = p->getSyscallArg(tc, index); 891 uint32_t mode = p->getSyscallArg(tc, index); 892 893 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 894 if (!ffdp) 895 return -EBADF; 896 int sim_fd = ffdp->getSimFD(); 897 898 mode_t hostMode = mode; 899 900 int result = fchmod(sim_fd, hostMode); 901 902 return (result < 0) ? -errno : 0; 903} 904 905/// Target mremap() handler. 906template <class OS> 907SyscallReturn 908mremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 909{ 910 int index = 0; 911 Addr start = process->getSyscallArg(tc, index); 912 uint64_t old_length = process->getSyscallArg(tc, index); 913 uint64_t new_length = process->getSyscallArg(tc, index); 914 uint64_t flags = process->getSyscallArg(tc, index); 915 uint64_t provided_address = 0; 916 bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 917 918 if (use_provided_address) 919 provided_address = process->getSyscallArg(tc, index); 920 921 if ((start % TheISA::PageBytes != 0) || 922 (provided_address % TheISA::PageBytes != 0)) { 923 warn("mremap failing: arguments not page aligned"); 924 return -EINVAL; 925 } 926 927 new_length = roundUp(new_length, TheISA::PageBytes); 928 929 if (new_length > old_length) { 930 std::shared_ptr<MemState> mem_state = process->memState; 931 Addr mmap_end = mem_state->getMmapEnd(); 932 933 if ((start + old_length) == mmap_end && 934 (!use_provided_address || provided_address == start)) { 935 // This case cannot occur when growing downward, as 936 // start is greater than or equal to mmap_end. 937 uint64_t diff = new_length - old_length; 938 process->allocateMem(mmap_end, diff); 939 mem_state->setMmapEnd(mmap_end + diff); 940 return start; 941 } else { 942 if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 943 warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 944 return -ENOMEM; 945 } else { 946 uint64_t new_start = provided_address; 947 if (!use_provided_address) { 948 new_start = process->mmapGrowsDown() ? 949 mmap_end - new_length : mmap_end; 950 mmap_end = process->mmapGrowsDown() ? 951 new_start : mmap_end + new_length; 952 mem_state->setMmapEnd(mmap_end); 953 } 954 955 process->pTable->remap(start, old_length, new_start); 956 warn("mremapping to new vaddr %08p-%08p, adding %d\n", 957 new_start, new_start + new_length, 958 new_length - old_length); 959 // add on the remaining unallocated pages 960 process->allocateMem(new_start + old_length, 961 new_length - old_length, 962 use_provided_address /* clobber */); 963 if (use_provided_address && 964 ((new_start + new_length > mem_state->getMmapEnd() && 965 !process->mmapGrowsDown()) || 966 (new_start < mem_state->getMmapEnd() && 967 process->mmapGrowsDown()))) { 968 // something fishy going on here, at least notify the user 969 // @todo: increase mmap_end? 970 warn("mmap region limit exceeded with MREMAP_FIXED\n"); 971 } 972 warn("returning %08p as start\n", new_start); 973 return new_start; 974 } 975 } 976 } else { 977 if (use_provided_address && provided_address != start) 978 process->pTable->remap(start, new_length, provided_address); 979 process->pTable->unmap(start + new_length, old_length - new_length); 980 return use_provided_address ? provided_address : start; 981 } 982} 983 984/// Target stat() handler. 985template <class OS> 986SyscallReturn 987statFunc(SyscallDesc *desc, int callnum, Process *process, 988 ThreadContext *tc) 989{ 990 std::string path; 991 992 int index = 0; 993 if (!tc->getMemProxy().tryReadString(path, 994 process->getSyscallArg(tc, index))) { 995 return -EFAULT; 996 } 997 Addr bufPtr = process->getSyscallArg(tc, index); 998 999 // Adjust path for current working directory 1000 path = process->fullPath(path); 1001 1002 struct stat hostBuf; 1003 int result = stat(path.c_str(), &hostBuf); 1004 1005 if (result < 0) 1006 return -errno; 1007 1008 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1009 1010 return 0; 1011} 1012 1013 1014/// Target stat64() handler. 1015template <class OS> 1016SyscallReturn 1017stat64Func(SyscallDesc *desc, int callnum, Process *process, 1018 ThreadContext *tc) 1019{ 1020 std::string path; 1021 1022 int index = 0; 1023 if (!tc->getMemProxy().tryReadString(path, 1024 process->getSyscallArg(tc, index))) 1025 return -EFAULT; 1026 Addr bufPtr = process->getSyscallArg(tc, index); 1027 1028 // Adjust path for current working directory 1029 path = process->fullPath(path); 1030 1031#if NO_STAT64 1032 struct stat hostBuf; 1033 int result = stat(path.c_str(), &hostBuf); 1034#else 1035 struct stat64 hostBuf; 1036 int result = stat64(path.c_str(), &hostBuf); 1037#endif 1038 1039 if (result < 0) 1040 return -errno; 1041 1042 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1043 1044 return 0; 1045} 1046 1047 1048/// Target fstatat64() handler. 1049template <class OS> 1050SyscallReturn 1051fstatat64Func(SyscallDesc *desc, int callnum, Process *process, 1052 ThreadContext *tc) 1053{ 1054 int index = 0; 1055 int dirfd = process->getSyscallArg(tc, index); 1056 if (dirfd != OS::TGT_AT_FDCWD) 1057 warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 1058 1059 std::string path; 1060 if (!tc->getMemProxy().tryReadString(path, 1061 process->getSyscallArg(tc, index))) 1062 return -EFAULT; 1063 Addr bufPtr = process->getSyscallArg(tc, index); 1064 1065 // Adjust path for current working directory 1066 path = process->fullPath(path); 1067 1068#if NO_STAT64 1069 struct stat hostBuf; 1070 int result = stat(path.c_str(), &hostBuf); 1071#else 1072 struct stat64 hostBuf; 1073 int result = stat64(path.c_str(), &hostBuf); 1074#endif 1075 1076 if (result < 0) 1077 return -errno; 1078 1079 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1080 1081 return 0; 1082} 1083 1084 1085/// Target fstat64() handler. 1086template <class OS> 1087SyscallReturn 1088fstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1089{ 1090 int index = 0; 1091 int tgt_fd = p->getSyscallArg(tc, index); 1092 Addr bufPtr = p->getSyscallArg(tc, index); 1093 1094 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1095 if (!ffdp) 1096 return -EBADF; 1097 int sim_fd = ffdp->getSimFD(); 1098 1099#if NO_STAT64 1100 struct stat hostBuf; 1101 int result = fstat(sim_fd, &hostBuf); 1102#else 1103 struct stat64 hostBuf; 1104 int result = fstat64(sim_fd, &hostBuf); 1105#endif 1106 1107 if (result < 0) 1108 return -errno; 1109 1110 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1111 1112 return 0; 1113} 1114 1115 1116/// Target lstat() handler. 1117template <class OS> 1118SyscallReturn 1119lstatFunc(SyscallDesc *desc, int callnum, Process *process, 1120 ThreadContext *tc) 1121{ 1122 std::string path; 1123 1124 int index = 0; 1125 if (!tc->getMemProxy().tryReadString(path, 1126 process->getSyscallArg(tc, index))) { 1127 return -EFAULT; 1128 } 1129 Addr bufPtr = process->getSyscallArg(tc, index); 1130 1131 // Adjust path for current working directory 1132 path = process->fullPath(path); 1133 1134 struct stat hostBuf; 1135 int result = lstat(path.c_str(), &hostBuf); 1136 1137 if (result < 0) 1138 return -errno; 1139 1140 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1141 1142 return 0; 1143} 1144 1145/// Target lstat64() handler. 1146template <class OS> 1147SyscallReturn 1148lstat64Func(SyscallDesc *desc, int callnum, Process *process, 1149 ThreadContext *tc) 1150{ 1151 std::string path; 1152 1153 int index = 0; 1154 if (!tc->getMemProxy().tryReadString(path, 1155 process->getSyscallArg(tc, index))) { 1156 return -EFAULT; 1157 } 1158 Addr bufPtr = process->getSyscallArg(tc, index); 1159 1160 // Adjust path for current working directory 1161 path = process->fullPath(path); 1162 1163#if NO_STAT64 1164 struct stat hostBuf; 1165 int result = lstat(path.c_str(), &hostBuf); 1166#else 1167 struct stat64 hostBuf; 1168 int result = lstat64(path.c_str(), &hostBuf); 1169#endif 1170 1171 if (result < 0) 1172 return -errno; 1173 1174 copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1175 1176 return 0; 1177} 1178 1179/// Target fstat() handler. 1180template <class OS> 1181SyscallReturn 1182fstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1183{ 1184 int index = 0; 1185 int tgt_fd = p->getSyscallArg(tc, index); 1186 Addr bufPtr = p->getSyscallArg(tc, index); 1187 1188 DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1189 1190 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1191 if (!ffdp) 1192 return -EBADF; 1193 int sim_fd = ffdp->getSimFD(); 1194 1195 struct stat hostBuf; 1196 int result = fstat(sim_fd, &hostBuf); 1197 1198 if (result < 0) 1199 return -errno; 1200 1201 copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 1202 1203 return 0; 1204} 1205 1206 1207/// Target statfs() handler. 1208template <class OS> 1209SyscallReturn 1210statfsFunc(SyscallDesc *desc, int callnum, Process *process, 1211 ThreadContext *tc) 1212{ 1213#if NO_STATFS 1214 warn("Host OS cannot support calls to statfs. Ignoring syscall"); 1215#else 1216 std::string path; 1217 1218 int index = 0; 1219 if (!tc->getMemProxy().tryReadString(path, 1220 process->getSyscallArg(tc, index))) { 1221 return -EFAULT; 1222 } 1223 Addr bufPtr = process->getSyscallArg(tc, index); 1224 1225 // Adjust path for current working directory 1226 path = process->fullPath(path); 1227 1228 struct statfs hostBuf; 1229 int result = statfs(path.c_str(), &hostBuf); 1230 1231 if (result < 0) 1232 return -errno; 1233 1234 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1235#endif 1236 return 0; 1237} 1238 1239template <class OS> 1240SyscallReturn 1241cloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1242{ 1243 int index = 0; 1244 TheISA::IntReg flags = p->getSyscallArg(tc, index); 1245 TheISA::IntReg newStack = p->getSyscallArg(tc, index); 1246 Addr ptidPtr = p->getSyscallArg(tc, index); 1247 Addr ctidPtr = p->getSyscallArg(tc, index); 1248 Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 1249 1250 if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 1251 ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 1252 ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 1253 ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 1254 ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 1255 ((flags & OS::TGT_CLONE_VM) && !(newStack))) 1256 return -EINVAL; 1257 1258 ThreadContext *ctc; 1259 if (!(ctc = p->findFreeContext())) 1260 fatal("clone: no spare thread context in system"); 1261 1262 /** 1263 * Note that ProcessParams is generated by swig and there are no other 1264 * examples of how to create anything but this default constructor. The 1265 * fields are manually initialized instead of passing parameters to the 1266 * constructor. 1267 */ 1268 ProcessParams *pp = new ProcessParams(); 1269 pp->executable.assign(*(new std::string(p->progName()))); 1270 pp->cmd.push_back(*(new std::string(p->progName()))); 1271 pp->system = p->system; 1272 pp->cwd.assign(p->getcwd()); 1273 pp->input.assign("stdin"); 1274 pp->output.assign("stdout"); 1275 pp->errout.assign("stderr"); 1276 pp->uid = p->uid(); 1277 pp->euid = p->euid(); 1278 pp->gid = p->gid(); 1279 pp->egid = p->egid(); 1280 1281 /* Find the first free PID that's less than the maximum */ 1282 std::set<int> const& pids = p->system->PIDs; 1283 int temp_pid = *pids.begin(); 1284 do { 1285 temp_pid++; 1286 } while (pids.find(temp_pid) != pids.end()); 1287 if (temp_pid >= System::maxPID) 1288 fatal("temp_pid is too large: %d", temp_pid); 1289 1290 pp->pid = temp_pid; 1291 pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 1292 Process *cp = pp->create(); 1293 delete pp; 1294 1295 Process *owner = ctc->getProcessPtr(); 1296 ctc->setProcessPtr(cp); 1297 cp->assignThreadContext(ctc->contextId()); 1298 owner->revokeThreadContext(ctc->contextId()); 1299 1300 if (flags & OS::TGT_CLONE_PARENT_SETTID) { 1301 BufferArg ptidBuf(ptidPtr, sizeof(long)); 1302 long *ptid = (long *)ptidBuf.bufferPtr(); 1303 *ptid = cp->pid(); 1304 ptidBuf.copyOut(tc->getMemProxy()); 1305 } 1306 1307 cp->initState(); 1308 p->clone(tc, ctc, cp, flags); 1309 1310 if (flags & OS::TGT_CLONE_THREAD) { 1311 delete cp->sigchld; 1312 cp->sigchld = p->sigchld; 1313 } else if (flags & OS::TGT_SIGCHLD) { 1314 *cp->sigchld = true; 1315 } 1316 1317 if (flags & OS::TGT_CLONE_CHILD_SETTID) { 1318 BufferArg ctidBuf(ctidPtr, sizeof(long)); 1319 long *ctid = (long *)ctidBuf.bufferPtr(); 1320 *ctid = cp->pid(); 1321 ctidBuf.copyOut(ctc->getMemProxy()); 1322 } 1323 1324 if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 1325 cp->childClearTID = (uint64_t)ctidPtr; 1326 1327 ctc->clearArchRegs(); 1328 1329#if THE_ISA == ALPHA_ISA 1330 TheISA::copyMiscRegs(tc, ctc); 1331#elif THE_ISA == SPARC_ISA 1332 TheISA::copyRegs(tc, ctc); 1333 ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 1334 ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 1335 ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 1336 ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 1337 ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 1338 ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 1339 ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 1340 ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 1341 for (int y = 8; y < 32; y++) 1342 ctc->setIntReg(y, tc->readIntReg(y)); 1343#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA 1344 TheISA::copyRegs(tc, ctc); 1345#endif 1346 1347#if THE_ISA == X86_ISA 1348 if (flags & OS::TGT_CLONE_SETTLS) { 1349 ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 1350 ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 1351 } 1352#endif 1353 1354 if (newStack) 1355 ctc->setIntReg(TheISA::StackPointerReg, newStack); 1356 1357 cp->setSyscallReturn(ctc, 0); 1358 1359#if THE_ISA == ALPHA_ISA 1360 ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 1361#elif THE_ISA == SPARC_ISA 1362 tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1363 ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 1364#endif 1365 1366 ctc->pcState(tc->nextInstAddr()); 1367 ctc->activate(); 1368 1369 return cp->pid(); 1370} 1371 1372/// Target fstatfs() handler. 1373template <class OS> 1374SyscallReturn 1375fstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1376{ 1377 int index = 0; 1378 int tgt_fd = p->getSyscallArg(tc, index); 1379 Addr bufPtr = p->getSyscallArg(tc, index); 1380 1381 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1382 if (!ffdp) 1383 return -EBADF; 1384 int sim_fd = ffdp->getSimFD(); 1385 1386 struct statfs hostBuf; 1387 int result = fstatfs(sim_fd, &hostBuf); 1388 1389 if (result < 0) 1390 return -errno; 1391 1392 copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1393 1394 return 0; 1395} 1396 1397 1398/// Target writev() handler. 1399template <class OS> 1400SyscallReturn 1401writevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1402{ 1403 int index = 0; 1404 int tgt_fd = p->getSyscallArg(tc, index); 1405 1406 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 1407 if (!hbfdp) 1408 return -EBADF; 1409 int sim_fd = hbfdp->getSimFD(); 1410 1411 SETranslatingPortProxy &prox = tc->getMemProxy(); 1412 uint64_t tiov_base = p->getSyscallArg(tc, index); 1413 size_t count = p->getSyscallArg(tc, index); 1414 struct iovec hiov[count]; 1415 for (size_t i = 0; i < count; ++i) { 1416 typename OS::tgt_iovec tiov; 1417 1418 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 1419 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 1420 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1421 hiov[i].iov_base = new char [hiov[i].iov_len]; 1422 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 1423 hiov[i].iov_len); 1424 } 1425 1426 int result = writev(sim_fd, hiov, count); 1427 1428 for (size_t i = 0; i < count; ++i) 1429 delete [] (char *)hiov[i].iov_base; 1430 1431 if (result < 0) 1432 return -errno; 1433 1434 return result; 1435} 1436 1437/// Real mmap handler. 1438template <class OS> 1439SyscallReturn 1440mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1441 bool is_mmap2) 1442{ 1443 int index = 0; 1444 Addr start = p->getSyscallArg(tc, index); 1445 uint64_t length = p->getSyscallArg(tc, index); 1446 int prot = p->getSyscallArg(tc, index); 1447 int tgt_flags = p->getSyscallArg(tc, index); 1448 int tgt_fd = p->getSyscallArg(tc, index); 1449 int offset = p->getSyscallArg(tc, index); 1450 1451 if (is_mmap2) 1452 offset *= TheISA::PageBytes; 1453 1454 if (start & (TheISA::PageBytes - 1) || 1455 offset & (TheISA::PageBytes - 1) || 1456 (tgt_flags & OS::TGT_MAP_PRIVATE && 1457 tgt_flags & OS::TGT_MAP_SHARED) || 1458 (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 1459 !(tgt_flags & OS::TGT_MAP_SHARED)) || 1460 !length) { 1461 return -EINVAL; 1462 } 1463 1464 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1465 // With shared mmaps, there are two cases to consider: 1466 // 1) anonymous: writes should modify the mapping and this should be 1467 // visible to observers who share the mapping. Currently, it's 1468 // difficult to update the shared mapping because there's no 1469 // structure which maintains information about the which virtual 1470 // memory areas are shared. If that structure existed, it would be 1471 // possible to make the translations point to the same frames. 1472 // 2) file-backed: writes should modify the mapping and the file 1473 // which is backed by the mapping. The shared mapping problem is the 1474 // same as what was mentioned about the anonymous mappings. For 1475 // file-backed mappings, the writes to the file are difficult 1476 // because it requires syncing what the mapping holds with the file 1477 // that resides on the host system. So, any write on a real system 1478 // would cause the change to be propagated to the file mapping at 1479 // some point in the future (the inode is tracked along with the 1480 // mapping). This isn't guaranteed to always happen, but it usually 1481 // works well enough. The guarantee is provided by the msync system 1482 // call. We could force the change through with shared mappings with 1483 // a call to msync, but that again would require more information 1484 // than we currently maintain. 1485 warn("mmap: writing to shared mmap region is currently " 1486 "unsupported. The write succeeds on the target, but it " 1487 "will not be propagated to the host or shared mappings"); 1488 } 1489 1490 length = roundUp(length, TheISA::PageBytes); 1491 1492 int sim_fd = -1; 1493 uint8_t *pmap = nullptr; 1494 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1495 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1496 1497 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1498 if (dfdp) { 1499 EmulatedDriver *emul_driver = dfdp->getDriver(); 1500 return emul_driver->mmap(p, tc, start, length, prot, 1501 tgt_flags, tgt_fd, offset); 1502 } 1503 1504 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1505 if (!ffdp) 1506 return -EBADF; 1507 sim_fd = ffdp->getSimFD(); 1508 1509 pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 1510 sim_fd, offset); 1511 1512 if (pmap == (decltype(pmap))-1) { 1513 warn("mmap: failed to map file into host address space"); 1514 return -errno; 1515 } 1516 } 1517 1518 // Extend global mmap region if necessary. Note that we ignore the 1519 // start address unless MAP_FIXED is specified. 1520 if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 1521 std::shared_ptr<MemState> mem_state = p->memState; 1522 Addr mmap_end = mem_state->getMmapEnd(); 1523 1524 start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1525 mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 1526 1527 mem_state->setMmapEnd(mmap_end); 1528 } 1529 1530 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1531 start, start + length - 1); 1532 1533 // We only allow mappings to overwrite existing mappings if 1534 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 1535 // because we ignore the start hint if TGT_MAP_FIXED is not set. 1536 int clobber = tgt_flags & OS::TGT_MAP_FIXED; 1537 if (clobber) { 1538 for (auto tc : p->system->threadContexts) { 1539 // If we might be overwriting old mappings, we need to 1540 // invalidate potentially stale mappings out of the TLBs. 1541 tc->getDTBPtr()->flushAll(); 1542 tc->getITBPtr()->flushAll(); 1543 } 1544 } 1545 1546 // Allocate physical memory and map it in. If the page table is already 1547 // mapped and clobber is not set, the simulator will issue throw a 1548 // fatal and bail out of the simulation. 1549 p->allocateMem(start, length, clobber); 1550 1551 // Transfer content into target address space. 1552 SETranslatingPortProxy &tp = tc->getMemProxy(); 1553 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1554 // In general, we should zero the mapped area for anonymous mappings, 1555 // with something like: 1556 // tp.memsetBlob(start, 0, length); 1557 // However, given that we don't support sparse mappings, and 1558 // some applications can map a couple of gigabytes of space 1559 // (intending sparse usage), that can get painfully expensive. 1560 // Fortunately, since we don't properly implement munmap either, 1561 // there's no danger of remapping used memory, so for now all 1562 // newly mapped memory should already be zeroed so we can skip it. 1563 } else { 1564 // It is possible to mmap an area larger than a file, however 1565 // accessing unmapped portions the system triggers a "Bus error" 1566 // on the host. We must know when to stop copying the file from 1567 // the host into the target address space. 1568 struct stat file_stat; 1569 if (fstat(sim_fd, &file_stat) > 0) 1570 fatal("mmap: cannot stat file"); 1571 1572 // Copy the portion of the file that is resident. This requires 1573 // checking both the mmap size and the filesize that we are 1574 // trying to mmap into this space; the mmap size also depends 1575 // on the specified offset into the file. 1576 uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1577 length); 1578 tp.writeBlob(start, pmap, size); 1579 1580 // Cleanup the mmap region before exiting this function. 1581 munmap(pmap, length); 1582 1583 // Maintain the symbol table for dynamic executables. 1584 // The loader will call mmap to map the images into its address 1585 // space and we intercept that here. We can verify that we are 1586 // executing inside the loader by checking the program counter value. 1587 // XXX: with multiprogrammed workloads or multi-node configurations, 1588 // this will not work since there is a single global symbol table. 1589 ObjectFile *interpreter = p->getInterpreter(); 1590 if (interpreter) { 1591 Addr text_start = interpreter->textBase(); 1592 Addr text_end = text_start + interpreter->textSize(); 1593 1594 Addr pc = tc->pcState().pc(); 1595 1596 if (pc >= text_start && pc < text_end) { 1597 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1598 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1599 ObjectFile *lib = createObjectFile(ffdp->getFileName()); 1600 1601 if (lib) { 1602 lib->loadAllSymbols(debugSymbolTable, 1603 lib->textBase(), start); 1604 } 1605 } 1606 } 1607 1608 // Note that we do not zero out the remainder of the mapping. This 1609 // is done by a real system, but it probably will not affect 1610 // execution (hopefully). 1611 } 1612 1613 return start; 1614} 1615 1616template <class OS> 1617SyscallReturn 1618pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1619{ 1620 int index = 0; 1621 int tgt_fd = p->getSyscallArg(tc, index); 1622 Addr bufPtr = p->getSyscallArg(tc, index); 1623 int nbytes = p->getSyscallArg(tc, index); 1624 int offset = p->getSyscallArg(tc, index); 1625 1626 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1627 if (!ffdp) 1628 return -EBADF; 1629 int sim_fd = ffdp->getSimFD(); 1630 1631 BufferArg bufArg(bufPtr, nbytes); 1632 bufArg.copyIn(tc->getMemProxy()); 1633 1634 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1635 1636 return (bytes_written == -1) ? -errno : bytes_written; 1637} 1638 1639/// Target mmap() handler. 1640template <class OS> 1641SyscallReturn 1642mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1643{ 1644 return mmapImpl<OS>(desc, num, p, tc, false); 1645} 1646 1647/// Target mmap2() handler. 1648template <class OS> 1649SyscallReturn 1650mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1651{ 1652 return mmapImpl<OS>(desc, num, p, tc, true); 1653} 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