syscall_emul.hh revision 13867
11689SN/A/* 29783Sandreas.hansson@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 37598Sminkyu.jeong@arm.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 47598Sminkyu.jeong@arm.com * All rights reserved 57598Sminkyu.jeong@arm.com * 67598Sminkyu.jeong@arm.com * The license below extends only to copyright in the software and shall 77598Sminkyu.jeong@arm.com * not be construed as granting a license to any other intellectual 87598Sminkyu.jeong@arm.com * property including but not limited to intellectual property relating 97598Sminkyu.jeong@arm.com * to a hardware implementation of the functionality of the software 107598Sminkyu.jeong@arm.com * licensed hereunder. You may use the software subject to the license 117598Sminkyu.jeong@arm.com * terms below provided that you ensure that this notice is replicated 127598Sminkyu.jeong@arm.com * unmodified and in its entirety in all distributions of the software, 137598Sminkyu.jeong@arm.com * modified or unmodified, in source code or in binary form. 142326SN/A * 151689SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 161689SN/A * All rights reserved. 171689SN/A * 181689SN/A * Redistribution and use in source and binary forms, with or without 191689SN/A * modification, are permitted provided that the following conditions are 201689SN/A * met: redistributions of source code must retain the above copyright 211689SN/A * notice, this list of conditions and the following disclaimer; 221689SN/A * redistributions in binary form must reproduce the above copyright 231689SN/A * notice, this list of conditions and the following disclaimer in the 241689SN/A * documentation and/or other materials provided with the distribution; 251689SN/A * neither the name of the copyright holders nor the names of its 261689SN/A * contributors may be used to endorse or promote products derived from 271689SN/A * this software without specific prior written permission. 281689SN/A * 291689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 301689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 311689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 321689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 331689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 341689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 351689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 361689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 371689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 381689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 392665Ssaidi@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 411689SN/A * Authors: Steve Reinhardt 421689SN/A * Kevin Lim 439944Smatt.horsnell@ARM.com */ 449944Smatt.horsnell@ARM.com 459944Smatt.horsnell@ARM.com#ifndef __SIM_SYSCALL_EMUL_HH__ 461060SN/A#define __SIM_SYSCALL_EMUL_HH__ 471060SN/A 481689SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 491060SN/A defined(__FreeBSD__) || defined(__CYGWIN__) || \ 501060SN/A defined(__NetBSD__)) 511060SN/A#define NO_STAT64 1 528230Snate@binkert.org#else 536658Snate@binkert.org#define NO_STAT64 0 548887Sgeoffrey.blake@arm.com#endif 552292SN/A 561717SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 578229Snate@binkert.org defined(__FreeBSD__) || defined(__NetBSD__)) 588232Snate@binkert.org#define NO_STATFS 1 599444SAndreas.Sandberg@ARM.com#else 608232Snate@binkert.org#define NO_STATFS 0 619527SMatt.Horsnell@arm.com#endif 625529Snate@binkert.org 631060SN/A#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 646221Snate@binkert.org defined(__FreeBSD__) || defined(__NetBSD__)) 656221Snate@binkert.org#define NO_FALLOCATE 1 661681SN/A#else 675529Snate@binkert.org#define NO_FALLOCATE 0 682873Sktlim@umich.edu#endif 694329Sktlim@umich.edu 704329Sktlim@umich.edu/// 714329Sktlim@umich.edu/// @file syscall_emul.hh 722292SN/A/// 732292SN/A/// This file defines objects used to emulate syscalls from the target 742292SN/A/// application on the host machine. 752292SN/A 762820Sktlim@umich.edu#ifdef __CYGWIN32__ 772292SN/A#include <sys/fcntl.h> 782820Sktlim@umich.edu 792820Sktlim@umich.edu#endif 809444SAndreas.Sandberg@ARM.com#include <fcntl.h> 811060SN/A#include <poll.h> 8210172Sdam.sunwoo@arm.com#include <sys/mman.h> 8310172Sdam.sunwoo@arm.com#include <sys/socket.h> 8410172Sdam.sunwoo@arm.com#include <sys/stat.h> 8510172Sdam.sunwoo@arm.com#if (NO_STATFS == 0) 8610172Sdam.sunwoo@arm.com#include <sys/statfs.h> 8710172Sdam.sunwoo@arm.com#else 8810172Sdam.sunwoo@arm.com#include <sys/mount.h> 8910172Sdam.sunwoo@arm.com#endif 9010172Sdam.sunwoo@arm.com#include <sys/time.h> 9110172Sdam.sunwoo@arm.com#include <sys/types.h> 9210172Sdam.sunwoo@arm.com#include <sys/uio.h> 9310172Sdam.sunwoo@arm.com#include <unistd.h> 9410172Sdam.sunwoo@arm.com 952292SN/A#include <cerrno> 962292SN/A#include <memory> 972292SN/A#include <string> 981060SN/A 991060SN/A#include "arch/generic/tlb.hh" 1001060SN/A#include "arch/utility.hh" 1011060SN/A#include "base/intmath.hh" 1021060SN/A#include "base/loader/object_file.hh" 1031060SN/A#include "base/logging.hh" 1041681SN/A#include "base/trace.hh" 1056221Snate@binkert.org#include "base/types.hh" 1066221Snate@binkert.org#include "config/the_isa.hh" 1076221Snate@binkert.org#include "cpu/base.hh" 1086221Snate@binkert.org#include "cpu/thread_context.hh" 1092292SN/A#include "mem/page_table.hh" 1102292SN/A#include "params/Process.hh" 1112820Sktlim@umich.edu#include "sim/emul_driver.hh" 1122820Sktlim@umich.edu#include "sim/futex_map.hh" 1132292SN/A#include "sim/process.hh" 1142292SN/A#include "sim/syscall_debug_macros.hh" 1152820Sktlim@umich.edu#include "sim/syscall_desc.hh" 1162820Sktlim@umich.edu#include "sim/syscall_emul_buf.hh" 1172292SN/A#include "sim/syscall_return.hh" 1182292SN/A 1192292SN/A#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN) 1202292SN/A#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) 1212292SN/A#endif 1222292SN/A 1232292SN/A////////////////////////////////////////////////////////////////////// 1242292SN/A// 1251060SN/A// The following emulation functions are generic enough that they 1261060SN/A// don't need to be recompiled for different emulated OS's. They are 1271681SN/A// defined in sim/syscall_emul.cc. 1281062SN/A// 12910023Smatt.horsnell@ARM.com////////////////////////////////////////////////////////////////////// 13010023Smatt.horsnell@ARM.com 13110023Smatt.horsnell@ARM.com 13210023Smatt.horsnell@ARM.com/// Handler for unimplemented syscalls that we haven't thought about. 13310023Smatt.horsnell@ARM.comSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 13410023Smatt.horsnell@ARM.com Process *p, ThreadContext *tc); 13510023Smatt.horsnell@ARM.com 13610023Smatt.horsnell@ARM.com/// Handler for unimplemented syscalls that we never intend to 1372292SN/A/// implement (signal handling, etc.) and should not affect the correct 1381062SN/A/// behavior of the program. Print a warning only if the appropriate 1392301SN/A/// trace flag is enabled. Return success to the target program. 1402301SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 1411062SN/A Process *p, ThreadContext *tc); 1422727Sktlim@umich.edu 1431062SN/A// Target fallocateFunc() handler. 1441062SN/ASyscallReturn fallocateFunc(SyscallDesc *desc, int num, 1451062SN/A Process *p, ThreadContext *tc); 1461062SN/A 1471062SN/A/// Target exit() handler: terminate current context. 1481062SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 1491062SN/A Process *p, ThreadContext *tc); 1501062SN/A 1511062SN/A/// Target exit_group() handler: terminate simulation. (exit all threads) 1521062SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 1531062SN/A Process *p, ThreadContext *tc); 1541062SN/A 1551062SN/A/// Target set_tid_address() handler. 1561062SN/ASyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 1571062SN/A Process *p, ThreadContext *tc); 1581062SN/A 1591062SN/A/// Target getpagesize() handler. 1601062SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1611062SN/A Process *p, ThreadContext *tc); 1621062SN/A 1631062SN/A/// Target brk() handler: set brk address. 1641062SN/ASyscallReturn brkFunc(SyscallDesc *desc, int num, 1651062SN/A Process *p, ThreadContext *tc); 1661062SN/A 1671062SN/A/// Target close() handler. 1681062SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 1691062SN/A Process *p, ThreadContext *tc); 1701062SN/A 1711062SN/A/// Target lseek() handler. 1721062SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 1731062SN/A Process *p, ThreadContext *tc); 1741062SN/A 1751062SN/A/// Target _llseek() handler. 1761062SN/ASyscallReturn _llseekFunc(SyscallDesc *desc, int num, 1771062SN/A Process *p, ThreadContext *tc); 1781062SN/A 1791062SN/A/// Target munmap() handler. 1801062SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 1811062SN/A Process *p, ThreadContext *tc); 1821062SN/A 1831062SN/A/// Target shutdown() handler. 1842292SN/ASyscallReturn shutdownFunc(SyscallDesc *desc, int num, 1852292SN/A Process *p, ThreadContext *tc); 1862292SN/A 1872292SN/A/// Target gethostname() handler. 1881062SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 1891062SN/A Process *p, ThreadContext *tc); 1901062SN/A 1911062SN/A/// Target getcwd() handler. 1921062SN/ASyscallReturn getcwdFunc(SyscallDesc *desc, int num, 1931062SN/A Process *p, ThreadContext *tc); 1941062SN/A 1952292SN/A/// Target readlink() handler. 1962292SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, 1972292SN/A Process *p, ThreadContext *tc, 1982292SN/A int index = 0); 1992292SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, 2002292SN/A Process *p, ThreadContext *tc); 2012292SN/A 2022292SN/A/// Target unlink() handler. 2032292SN/ASyscallReturn unlinkHelper(SyscallDesc *desc, int num, 2042292SN/A Process *p, ThreadContext *tc, 2052301SN/A int index); 2062727Sktlim@umich.eduSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 2072353SN/A Process *p, ThreadContext *tc); 2082727Sktlim@umich.edu 2092727Sktlim@umich.edu/// Target link() handler 2102727Sktlim@umich.eduSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p, 2116221Snate@binkert.org ThreadContext *tc); 2122353SN/A 2132727Sktlim@umich.edu/// Target symlink() handler. 2142727Sktlim@umich.eduSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p, 2152727Sktlim@umich.edu ThreadContext *tc); 2162727Sktlim@umich.edu 2172353SN/A/// Target mkdir() handler. 2182727Sktlim@umich.eduSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 2192727Sktlim@umich.edu Process *p, ThreadContext *tc); 2202727Sktlim@umich.edu 2216221Snate@binkert.org/// Target mknod() handler. 2228240Snate@binkert.orgSyscallReturn mknodFunc(SyscallDesc *desc, int num, 2232301SN/A Process *p, ThreadContext *tc); 2242727Sktlim@umich.edu 2252301SN/A/// Target chdir() handler. 2262727Sktlim@umich.eduSyscallReturn chdirFunc(SyscallDesc *desc, int num, 2276221Snate@binkert.org Process *p, ThreadContext *tc); 2288240Snate@binkert.org 2292301SN/A// Target rmdir() handler. 2302727Sktlim@umich.eduSyscallReturn rmdirFunc(SyscallDesc *desc, int num, 2312301SN/A Process *p, ThreadContext *tc); 2322727Sktlim@umich.edu 2336221Snate@binkert.org/// Target rename() handler. 2348240Snate@binkert.orgSyscallReturn renameFunc(SyscallDesc *desc, int num, 2352301SN/A Process *p, ThreadContext *tc); 2362727Sktlim@umich.edu 2372301SN/A 2382727Sktlim@umich.edu/// Target truncate() handler. 2396221Snate@binkert.orgSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2408240Snate@binkert.org Process *p, ThreadContext *tc); 2412301SN/A 2422727Sktlim@umich.edu 2432301SN/A/// Target ftruncate() handler. 2442301SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2458240Snate@binkert.org Process *p, ThreadContext *tc); 2462301SN/A 2472727Sktlim@umich.edu 2482727Sktlim@umich.edu/// Target truncate64() handler. 2492727Sktlim@umich.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 2502727Sktlim@umich.edu Process *p, ThreadContext *tc); 2518240Snate@binkert.org 2522727Sktlim@umich.edu/// Target ftruncate64() handler. 2532727Sktlim@umich.eduSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 2542727Sktlim@umich.edu Process *p, ThreadContext *tc); 2552727Sktlim@umich.edu 2562301SN/A 2572301SN/A/// Target umask() handler. 2586221Snate@binkert.orgSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2598240Snate@binkert.org Process *p, ThreadContext *tc); 2602301SN/A 2612727Sktlim@umich.edu/// Target gettid() handler. 2622301SN/ASyscallReturn gettidFunc(SyscallDesc *desc, int num, 2632326SN/A Process *p, ThreadContext *tc); 2646221Snate@binkert.org 2658240Snate@binkert.org/// Target chown() handler. 2662301SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 2672727Sktlim@umich.edu Process *p, ThreadContext *tc); 2682301SN/A 2692326SN/A/// Target setpgid() handler. 2706221Snate@binkert.orgSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2718240Snate@binkert.org Process *p, ThreadContext *tc); 2722301SN/A 2732727Sktlim@umich.edu/// Target fchown() handler. 2742301SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 2752326SN/A Process *p, ThreadContext *tc); 2766221Snate@binkert.org 2778240Snate@binkert.org/// Target dup() handler. 2782301SN/ASyscallReturn dupFunc(SyscallDesc *desc, int num, 2792727Sktlim@umich.edu Process *process, ThreadContext *tc); 2802301SN/A 2812326SN/A/// Target dup2() handler. 2826221Snate@binkert.orgSyscallReturn dup2Func(SyscallDesc *desc, int num, 2838240Snate@binkert.org Process *process, ThreadContext *tc); 2842301SN/A 2852727Sktlim@umich.edu/// Target fcntl() handler. 2862301SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 2872326SN/A Process *process, ThreadContext *tc); 2888240Snate@binkert.org 2892301SN/A/// Target fcntl64() handler. 2902727Sktlim@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 2912301SN/A Process *process, ThreadContext *tc); 2922326SN/A 2932301SN/A/// Target setuid() handler. 2942326SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 2958240Snate@binkert.org Process *p, ThreadContext *tc); 2962301SN/A 2972727Sktlim@umich.edu/// Target pipe() handler. 2982301SN/ASyscallReturn pipeFunc(SyscallDesc *desc, int num, 2992326SN/A Process *p, ThreadContext *tc); 3002301SN/A 3012326SN/A/// Internal pipe() handler. 3028240Snate@binkert.orgSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 3032301SN/A ThreadContext *tc, bool pseudoPipe); 3042727Sktlim@umich.edu 3052326SN/A/// Target getpid() handler. 3061062SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 3071062SN/A Process *p, ThreadContext *tc); 3081681SN/A 3091060SN/A// Target getpeername() handler. 3109427SAndreas.Sandberg@ARM.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num, 3111060SN/A Process *p, ThreadContext *tc); 3126221Snate@binkert.org 3132292SN/A// Target bind() handler. 3142292SN/ASyscallReturn bindFunc(SyscallDesc *desc, int num, 3152292SN/A Process *p, ThreadContext *tc); 3162292SN/A 3172292SN/A// Target listen() handler. 3182292SN/ASyscallReturn listenFunc(SyscallDesc *desc, int num, 3192292SN/A Process *p, ThreadContext *tc); 3202292SN/A 3212292SN/A// Target connect() handler. 3228887Sgeoffrey.blake@arm.comSyscallReturn connectFunc(SyscallDesc *desc, int num, 3238733Sgeoffrey.blake@arm.com Process *p, ThreadContext *tc); 3248850Sandreas.hansson@arm.com 3258887Sgeoffrey.blake@arm.com#if defined(SYS_getdents) 3268733Sgeoffrey.blake@arm.com// Target getdents() handler. 3272733Sktlim@umich.eduSyscallReturn getdentsFunc(SyscallDesc *desc, int num, 3281060SN/A Process *p, ThreadContext *tc); 3291060SN/A#endif 3301681SN/A 3311060SN/A#if defined(SYS_getdents64) 3322292SN/A// Target getdents() handler. 3331060SN/ASyscallReturn getdents64Func(SyscallDesc *desc, int num, 3341060SN/A Process *p, ThreadContext *tc); 3351060SN/A#endif 3361060SN/A 3371060SN/A// Target sendto() handler. 3381060SN/ASyscallReturn sendtoFunc(SyscallDesc *desc, int num, 3391060SN/A Process *p, ThreadContext *tc); 3401060SN/A 3411060SN/A// Target recvfrom() handler. 3422292SN/ASyscallReturn recvfromFunc(SyscallDesc *desc, int num, 3432292SN/A Process *p, ThreadContext *tc); 3441060SN/A 3451060SN/A// Target recvmsg() handler. 3461060SN/ASyscallReturn recvmsgFunc(SyscallDesc *desc, int num, 3471060SN/A Process *p, ThreadContext *tc); 3481681SN/A 3491060SN/A// Target sendmsg() handler. 3502292SN/ASyscallReturn sendmsgFunc(SyscallDesc *desc, int num, 3511060SN/A Process *p, ThreadContext *tc); 3521060SN/A 3531060SN/A// Target getuid() handler. 3541060SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 3551060SN/A Process *p, ThreadContext *tc); 3561060SN/A 3571060SN/A/// Target getgid() handler. 3581681SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 3591060SN/A Process *p, ThreadContext *tc); 3602292SN/A 3611060SN/A/// Target getppid() handler. 3621060SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 3631060SN/A Process *p, ThreadContext *tc); 3641060SN/A 3651060SN/A/// Target geteuid() handler. 3661060SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 3671060SN/A Process *p, ThreadContext *tc); 3681681SN/A 3691060SN/A/// Target getegid() handler. 3706221Snate@binkert.orgSyscallReturn getegidFunc(SyscallDesc *desc, int num, 3711060SN/A Process *p, ThreadContext *tc); 3722292SN/A 3732292SN/A/// Target access() handler 3742292SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num, 3752292SN/A Process *p, ThreadContext *tc); 3761060SN/ASyscallReturn accessFunc(SyscallDesc *desc, int num, 3771060SN/A Process *p, ThreadContext *tc, 3781681SN/A int index); 3791060SN/A 3802292SN/A// Target getsockopt() handler. 3811060SN/ASyscallReturn getsockoptFunc(SyscallDesc *desc, int num, 3822292SN/A Process *p, ThreadContext *tc); 3831060SN/A 3841060SN/A// Target setsockopt() handler. 3852307SN/ASyscallReturn setsockoptFunc(SyscallDesc *desc, int num, 3862863Sktlim@umich.edu Process *p, ThreadContext *tc); 3879444SAndreas.Sandberg@ARM.com 3882307SN/A// Target getsockname() handler. 3899444SAndreas.Sandberg@ARM.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num, 3909444SAndreas.Sandberg@ARM.com Process *p, ThreadContext *tc); 3919444SAndreas.Sandberg@ARM.com 3929444SAndreas.Sandberg@ARM.com/// Futex system call 3939444SAndreas.Sandberg@ARM.com/// Implemented by Daniel Sanchez 3949444SAndreas.Sandberg@ARM.com/// Used by printf's in multi-threaded apps 3959444SAndreas.Sandberg@ARM.comtemplate <class OS> 3969444SAndreas.Sandberg@ARM.comSyscallReturn 3979444SAndreas.Sandberg@ARM.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3989444SAndreas.Sandberg@ARM.com ThreadContext *tc) 3999444SAndreas.Sandberg@ARM.com{ 4009444SAndreas.Sandberg@ARM.com using namespace std; 4019444SAndreas.Sandberg@ARM.com 4029783Sandreas.hansson@arm.com int index = 0; 4039783Sandreas.hansson@arm.com Addr uaddr = process->getSyscallArg(tc, index); 4049783Sandreas.hansson@arm.com int op = process->getSyscallArg(tc, index); 4059783Sandreas.hansson@arm.com int val = process->getSyscallArg(tc, index); 4069783Sandreas.hansson@arm.com int timeout M5_VAR_USED = process->getSyscallArg(tc, index); 4079783Sandreas.hansson@arm.com Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index); 4089783Sandreas.hansson@arm.com int val3 = process->getSyscallArg(tc, index); 4099783Sandreas.hansson@arm.com 4109444SAndreas.Sandberg@ARM.com /* 4111681SN/A * Unsupported option that does not affect the correctness of the 4121681SN/A * application. This is a performance optimization utilized by Linux. 4132316SN/A */ 4141681SN/A op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 4159444SAndreas.Sandberg@ARM.com op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG; 4162843Sktlim@umich.edu 4179444SAndreas.Sandberg@ARM.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 4182843Sktlim@umich.edu 4199444SAndreas.Sandberg@ARM.com if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) { 4209444SAndreas.Sandberg@ARM.com // Ensure futex system call accessed atomically. 4211681SN/A BufferArg buf(uaddr, sizeof(int)); 4221681SN/A buf.copyIn(tc->getMemProxy()); 4232307SN/A int mem_val = *(int*)buf.bufferPtr(); 4241681SN/A 4252307SN/A /* 4261060SN/A * The value in memory at uaddr is not equal with the expected val 4272348SN/A * (a different thread must have changed it before the system call was 4282307SN/A * invoked). In this case, we need to throw an error. 4292307SN/A */ 4302307SN/A if (val != mem_val) 4311060SN/A return -OS::TGT_EWOULDBLOCK; 4322307SN/A 4332307SN/A if (OS::TGT_FUTEX_WAIT) { 4349444SAndreas.Sandberg@ARM.com futex_map.suspend(uaddr, process->tgid(), tc); 4351060SN/A } else { 4369427SAndreas.Sandberg@ARM.com futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3); 4372307SN/A } 4381060SN/A 4396221Snate@binkert.org return 0; 4406221Snate@binkert.org } else if (OS::TGT_FUTEX_WAKE == op) { 4416221Snate@binkert.org return futex_map.wakeup(uaddr, process->tgid(), val); 4426221Snate@binkert.org } else if (OS::TGT_FUTEX_WAKE_BITSET == op) { 4432307SN/A return futex_map.wakeup_bitset(uaddr, process->tgid(), val3); 4441060SN/A } else if (OS::TGT_FUTEX_REQUEUE == op || 4452307SN/A OS::TGT_FUTEX_CMP_REQUEUE == op) { 4462307SN/A 4472873Sktlim@umich.edu // Ensure futex system call accessed atomically. 4482307SN/A BufferArg buf(uaddr, sizeof(int)); 4491060SN/A buf.copyIn(tc->getMemProxy()); 4501060SN/A int mem_val = *(int*)buf.bufferPtr(); 4511060SN/A /* 4521681SN/A * For CMP_REQUEUE, the whole operation is only started only if 4531060SN/A * val3 is still the value of the futex pointed to by uaddr. 4546221Snate@binkert.org */ 4552107SN/A if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val) 4566221Snate@binkert.org return -OS::TGT_EWOULDBLOCK; 4572107SN/A return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2); 4582292SN/A } else if (OS::TGT_FUTEX_WAKE_OP == op) { 4592292SN/A /* 4602107SN/A * The FUTEX_WAKE_OP operation is equivalent to executing the 4612292SN/A * following code atomically and totally ordered with respect to 4622326SN/A * other futex operations on any of the two supplied futex words: 4632292SN/A * 4642107SN/A * int oldval = *(int *) addr2; 4652292SN/A * *(int *) addr2 = oldval op oparg; 4662935Sksewell@umich.edu * futex(addr1, FUTEX_WAKE, val, 0, 0, 0); 4674632Sgblack@eecs.umich.edu * if (oldval cmp cmparg) 4682935Sksewell@umich.edu * futex(addr2, FUTEX_WAKE, val2, 0, 0, 0); 4692292SN/A * 4702292SN/A * (op, oparg, cmp, cmparg are encoded in val3) 4712292SN/A * 4722292SN/A * +---+---+-----------+-----------+ 4732292SN/A * |op |cmp| oparg | cmparg | 4742107SN/A * +---+---+-----------+-----------+ 4752292SN/A * 4 4 12 12 <== # of bits 4762107SN/A * 4772292SN/A * reference: http://man7.org/linux/man-pages/man2/futex.2.html 4782292SN/A * 4792107SN/A */ 4802702Sktlim@umich.edu // get value from simulated-space 4812107SN/A BufferArg buf(uaddr2, sizeof(int)); 4822107SN/A buf.copyIn(tc->getMemProxy()); 4832107SN/A int oldval = *(int*)buf.bufferPtr(); 4842107SN/A int newval = oldval; 4856221Snate@binkert.org // extract op, oparg, cmp, cmparg from val3 4862292SN/A int wake_cmparg = val3 & 0xfff; 4877720Sgblack@eecs.umich.edu int wake_oparg = (val3 & 0xfff000) >> 12; 4887720Sgblack@eecs.umich.edu int wake_cmp = (val3 & 0xf000000) >> 24; 4892292SN/A int wake_op = (val3 & 0xf0000000) >> 28; 49010231Ssteve.reinhardt@amd.com if ((wake_op & OS::TGT_FUTEX_OP_ARG_SHIFT) >> 3 == 1) 4917852SMatt.Horsnell@arm.com wake_oparg = (1 << wake_oparg); 4927852SMatt.Horsnell@arm.com wake_op &= ~OS::TGT_FUTEX_OP_ARG_SHIFT; 4937852SMatt.Horsnell@arm.com // perform operation on the value of the second futex 4947852SMatt.Horsnell@arm.com if (wake_op == OS::TGT_FUTEX_OP_SET) 4952935Sksewell@umich.edu newval = wake_oparg; 4967852SMatt.Horsnell@arm.com else if (wake_op == OS::TGT_FUTEX_OP_ADD) 4977852SMatt.Horsnell@arm.com newval += wake_oparg; 4982292SN/A else if (wake_op == OS::TGT_FUTEX_OP_OR) 4997852SMatt.Horsnell@arm.com newval |= wake_oparg; 5007852SMatt.Horsnell@arm.com else if (wake_op == OS::TGT_FUTEX_OP_ANDN) 5017852SMatt.Horsnell@arm.com newval &= ~wake_oparg; 5022292SN/A else if (wake_op == OS::TGT_FUTEX_OP_XOR) 5037852SMatt.Horsnell@arm.com newval ^= wake_oparg; 5047852SMatt.Horsnell@arm.com // copy updated value back to simulated-space 5057852SMatt.Horsnell@arm.com *(int*)buf.bufferPtr() = newval; 5062292SN/A buf.copyOut(tc->getMemProxy()); 5072292SN/A // perform the first wake-up 5082292SN/A int woken1 = futex_map.wakeup(uaddr, process->tgid(), val); 5092292SN/A int woken2 = 0; 5106221Snate@binkert.org // calculate the condition of the second wake-up 5112292SN/A bool is_wake2 = false; 5128513SGiacomo.Gabrielli@arm.com if (wake_cmp == OS::TGT_FUTEX_OP_CMP_EQ) 5138513SGiacomo.Gabrielli@arm.com is_wake2 = oldval == wake_cmparg; 5148513SGiacomo.Gabrielli@arm.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_NE) 5158513SGiacomo.Gabrielli@arm.com is_wake2 = oldval != wake_cmparg; 5168513SGiacomo.Gabrielli@arm.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LT) 5178513SGiacomo.Gabrielli@arm.com is_wake2 = oldval < wake_cmparg; 5188513SGiacomo.Gabrielli@arm.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_LE) 5198513SGiacomo.Gabrielli@arm.com is_wake2 = oldval <= wake_cmparg; 52010231Ssteve.reinhardt@amd.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GT) 5218513SGiacomo.Gabrielli@arm.com is_wake2 = oldval > wake_cmparg; 5228513SGiacomo.Gabrielli@arm.com else if (wake_cmp == OS::TGT_FUTEX_OP_CMP_GE) 5232292SN/A is_wake2 = oldval >= wake_cmparg; 5247852SMatt.Horsnell@arm.com // perform the second wake-up 5258513SGiacomo.Gabrielli@arm.com if (is_wake2) 5268137SAli.Saidi@ARM.com woken2 = futex_map.wakeup(uaddr2, process->tgid(), timeout); 5272292SN/A 5288513SGiacomo.Gabrielli@arm.com return woken1 + woken2; 5298513SGiacomo.Gabrielli@arm.com } 5302292SN/A warn("futex: op %d not implemented; ignoring.", op); 5317852SMatt.Horsnell@arm.com return -ENOSYS; 5327852SMatt.Horsnell@arm.com} 5332292SN/A 5342292SN/A 5352292SN/A/// Pseudo Funcs - These functions use a different return convension, 5362292SN/A/// returning a second value in a register other than the normal return register 5376221Snate@binkert.orgSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 5382292SN/A Process *process, ThreadContext *tc); 5392292SN/A 5407720Sgblack@eecs.umich.edu/// Target getpidPseudo() handler. 54110231Ssteve.reinhardt@amd.comSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 5427852SMatt.Horsnell@arm.com Process *p, ThreadContext *tc); 5437852SMatt.Horsnell@arm.com 5442292SN/A/// Target getuidPseudo() handler. 5457852SMatt.Horsnell@arm.comSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 5467852SMatt.Horsnell@arm.com Process *p, ThreadContext *tc); 5478137SAli.Saidi@ARM.com 5482292SN/A/// Target getgidPseudo() handler. 5497852SMatt.Horsnell@arm.comSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 5507852SMatt.Horsnell@arm.com Process *p, ThreadContext *tc); 5512292SN/A 5527852SMatt.Horsnell@arm.com 5532292SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 5547852SMatt.Horsnell@arm.comconst int one_million = 1000000; 5557852SMatt.Horsnell@arm.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 5562292SN/Aconst int one_billion = 1000000000; 5572292SN/A 5582292SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 5592292SN/A/// by my reckoning. We want to keep this a constant (not use the 5606221Snate@binkert.org/// real-world time) to keep simulations repeatable. 5612292SN/Aconst unsigned seconds_since_epoch = 1000000000; 5622292SN/A 5632292SN/A/// Helper function to convert current elapsed time to seconds and 5642292SN/A/// microseconds. 5652292SN/Atemplate <class T1, class T2> 5662292SN/Avoid 5672292SN/AgetElapsedTimeMicro(T1 &sec, T2 &usec) 5682292SN/A{ 5692292SN/A uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 5702292SN/A sec = elapsed_usecs / one_million; 5712292SN/A usec = elapsed_usecs % one_million; 5722292SN/A} 5732292SN/A 5742292SN/A/// Helper function to convert current elapsed time to seconds and 5752292SN/A/// nanoseconds. 5762292SN/Atemplate <class T1, class T2> 5772292SN/Avoid 5782292SN/AgetElapsedTimeNano(T1 &sec, T2 &nsec) 5796221Snate@binkert.org{ 5802292SN/A uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 5812292SN/A sec = elapsed_nsecs / one_billion; 5822292SN/A nsec = elapsed_nsecs % one_billion; 5832292SN/A} 5842292SN/A 5852292SN/A////////////////////////////////////////////////////////////////////// 5862292SN/A// 5872292SN/A// The following emulation functions are generic, but need to be 5882292SN/A// templated to account for differences in types, constants, etc. 5892292SN/A// 5902292SN/A////////////////////////////////////////////////////////////////////// 5912292SN/A 5922292SN/A typedef struct statfs hst_statfs; 5932292SN/A#if NO_STAT64 5942292SN/A typedef struct stat hst_stat; 5952292SN/A typedef struct stat hst_stat64; 5962292SN/A#else 5971060SN/A typedef struct stat hst_stat; 5981681SN/A typedef struct stat64 hst_stat64; 5991060SN/A#endif 6001060SN/A 6012292SN/A//// Helper function to convert a host stat buffer to a target stat 6022292SN/A//// buffer. Also copies the target buffer out to the simulated 6032292SN/A//// memory space. Used by stat(), fstat(), and lstat(). 6042292SN/A 6052292SN/Atemplate <typename target_stat, typename host_stat> 6062292SN/Avoid 6071681SN/AconvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 6081681SN/A{ 6091060SN/A using namespace TheISA; 6102292SN/A 6111060SN/A if (fakeTTY) 6122292SN/A tgt->st_dev = 0xA; 6132292SN/A else 6141060SN/A tgt->st_dev = host->st_dev; 6152292SN/A tgt->st_dev = TheISA::htog(tgt->st_dev); 6162292SN/A tgt->st_ino = host->st_ino; 6172292SN/A tgt->st_ino = TheISA::htog(tgt->st_ino); 6182292SN/A tgt->st_mode = host->st_mode; 6193221Sktlim@umich.edu if (fakeTTY) { 6203221Sktlim@umich.edu // Claim to be a character device 6213221Sktlim@umich.edu tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 6223221Sktlim@umich.edu tgt->st_mode |= S_IFCHR; // Set S_IFCHR 6233221Sktlim@umich.edu } 6242292SN/A tgt->st_mode = TheISA::htog(tgt->st_mode); 6252292SN/A tgt->st_nlink = host->st_nlink; 6262292SN/A tgt->st_nlink = TheISA::htog(tgt->st_nlink); 6272292SN/A tgt->st_uid = host->st_uid; 6282326SN/A tgt->st_uid = TheISA::htog(tgt->st_uid); 6292292SN/A tgt->st_gid = host->st_gid; 6302292SN/A tgt->st_gid = TheISA::htog(tgt->st_gid); 6312820Sktlim@umich.edu if (fakeTTY) 6322292SN/A tgt->st_rdev = 0x880d; 6332292SN/A else 6342292SN/A tgt->st_rdev = host->st_rdev; 6352292SN/A tgt->st_rdev = TheISA::htog(tgt->st_rdev); 6362353SN/A tgt->st_size = host->st_size; 6372292SN/A tgt->st_size = TheISA::htog(tgt->st_size); 6382292SN/A tgt->st_atimeX = host->st_atime; 6392353SN/A tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 6402353SN/A tgt->st_mtimeX = host->st_mtime; 6412292SN/A tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 6422292SN/A tgt->st_ctimeX = host->st_ctime; 6432292SN/A tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 6442292SN/A // Force the block size to be 8KB. This helps to ensure buffered io works 6452292SN/A // consistently across different hosts. 6462292SN/A tgt->st_blksize = 0x2000; 6472292SN/A tgt->st_blksize = TheISA::htog(tgt->st_blksize); 6482292SN/A tgt->st_blocks = host->st_blocks; 6492292SN/A tgt->st_blocks = TheISA::htog(tgt->st_blocks); 6502292SN/A} 6512292SN/A 6522292SN/A// Same for stat64 6532731Sktlim@umich.edu 6542292SN/Atemplate <typename target_stat, typename host_stat64> 6552292SN/Avoid 6562292SN/AconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 6572292SN/A{ 6582292SN/A using namespace TheISA; 6592292SN/A 6602292SN/A convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 6612292SN/A#if defined(STAT_HAVE_NSEC) 6626221Snate@binkert.org tgt->st_atime_nsec = host->st_atime_nsec; 6632292SN/A tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 6642292SN/A tgt->st_mtime_nsec = host->st_mtime_nsec; 6652292SN/A tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 6662292SN/A tgt->st_ctime_nsec = host->st_ctime_nsec; 6672292SN/A tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 6682292SN/A#else 6692292SN/A tgt->st_atime_nsec = 0; 6702292SN/A tgt->st_mtime_nsec = 0; 6719937SFaissal.Sleiman@arm.com tgt->st_ctime_nsec = 0; 6722292SN/A#endif 6737720Sgblack@eecs.umich.edu} 6742292SN/A 6752292SN/A// Here are a couple of convenience functions 6762292SN/Atemplate<class OS> 6772292SN/Avoid 6782292SN/AcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 6792292SN/A hst_stat *host, bool fakeTTY = false) 6802292SN/A{ 6812292SN/A typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 6822292SN/A tgt_stat_buf tgt(addr); 6832292SN/A convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 6842292SN/A tgt.copyOut(mem); 6852292SN/A} 6862292SN/A 6872292SN/Atemplate<class OS> 6886221Snate@binkert.orgvoid 6896221Snate@binkert.orgcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 6902292SN/A hst_stat64 *host, bool fakeTTY = false) 6913867Sbinkertn@umich.edu{ 6926221Snate@binkert.org typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 6933867Sbinkertn@umich.edu tgt_stat_buf tgt(addr); 6942292SN/A convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 6952292SN/A tgt.copyOut(mem); 6962292SN/A} 6972292SN/A 6982292SN/Atemplate <class OS> 6992292SN/Avoid 7002292SN/AcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 7012292SN/A hst_statfs *host) 7022292SN/A{ 7032292SN/A TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 7042292SN/A 7056221Snate@binkert.org tgt->f_type = TheISA::htog(host->f_type); 7066221Snate@binkert.org#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 7072292SN/A tgt->f_bsize = TheISA::htog(host->f_iosize); 7083867Sbinkertn@umich.edu#else 7096221Snate@binkert.org tgt->f_bsize = TheISA::htog(host->f_bsize); 7103867Sbinkertn@umich.edu#endif 7113867Sbinkertn@umich.edu tgt->f_blocks = TheISA::htog(host->f_blocks); 7122292SN/A tgt->f_bfree = TheISA::htog(host->f_bfree); 7132292SN/A tgt->f_bavail = TheISA::htog(host->f_bavail); 7142292SN/A tgt->f_files = TheISA::htog(host->f_files); 7152292SN/A tgt->f_ffree = TheISA::htog(host->f_ffree); 7161062SN/A memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 7171062SN/A#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 7181681SN/A tgt->f_namelen = TheISA::htog(host->f_namemax); 7191062SN/A tgt->f_frsize = TheISA::htog(host->f_bsize); 7202292SN/A#elif defined(__APPLE__) 7211062SN/A tgt->f_namelen = 0; 7222292SN/A tgt->f_frsize = 0; 7231062SN/A#else 7246221Snate@binkert.org tgt->f_namelen = TheISA::htog(host->f_namelen); 7256221Snate@binkert.org tgt->f_frsize = TheISA::htog(host->f_frsize); 7261062SN/A#endif 7273867Sbinkertn@umich.edu#if defined(__linux__) 7286221Snate@binkert.org memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 7291062SN/A#else 7302292SN/A /* 7312292SN/A * The fields are different sizes per OS. Don't bother with 7322292SN/A * f_spare or f_reserved on non-Linux for now. 7332292SN/A */ 7342292SN/A memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 7351062SN/A#endif 7362292SN/A 7372292SN/A tgt.copyOut(mem); 7382292SN/A} 7397897Shestness@cs.utexas.edu 7402292SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 7412292SN/A/// only to find out if their stdout is a tty, to determine whether to 7422292SN/A/// do line or block buffering. We always claim that output fds are 7431062SN/A/// not TTYs to provide repeatable results. 7442292SN/Atemplate <class OS> 7451062SN/ASyscallReturn 7462292SN/AioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 7472292SN/A{ 7482292SN/A int index = 0; 7492292SN/A int tgt_fd = p->getSyscallArg(tc, index); 7502292SN/A unsigned req = p->getSyscallArg(tc, index); 7512292SN/A 7521062SN/A DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 7532292SN/A 7541062SN/A if (OS::isTtyReq(req)) 7552292SN/A return -ENOTTY; 7561062SN/A 7571062SN/A auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 7581062SN/A if (!dfdp) 7591681SN/A return -EBADF; 7601062SN/A 7612292SN/A /** 7621062SN/A * If the driver is valid, issue the ioctl through it. Otherwise, 7632292SN/A * there's an implicit assumption that the device is a TTY type and we 7642292SN/A * return that we do not have a valid TTY. 7652292SN/A */ 7661062SN/A EmulatedDriver *emul_driver = dfdp->getDriver(); 7672292SN/A if (emul_driver) 7682292SN/A return emul_driver->ioctl(p, tc, req); 7696221Snate@binkert.org 7702292SN/A /** 7712292SN/A * For lack of a better return code, return ENOTTY. Ideally, we should 7722292SN/A * return something better here, but at least we issue the warning. 7732292SN/A */ 7741062SN/A warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 7752292SN/A tgt_fd, req, tc->pcState()); 7762292SN/A return -ENOTTY; 7772292SN/A} 7782292SN/A 7792292SN/Atemplate <class OS> 7802292SN/ASyscallReturn 7812292SN/AopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 7822292SN/A bool isopenat) 7836221Snate@binkert.org{ 7842292SN/A int index = 0; 7852292SN/A int tgt_dirfd = -1; 7862292SN/A 7872292SN/A /** 7882292SN/A * If using the openat variant, read in the target directory file 7892292SN/A * descriptor from the simulated process. 7902292SN/A */ 7912292SN/A if (isopenat) 7922292SN/A tgt_dirfd = p->getSyscallArg(tc, index); 7932292SN/A 7942292SN/A /** 7952292SN/A * Retrieve the simulated process' memory proxy and then read in the path 7962292SN/A * string from that memory space into the host's working memory space. 7972292SN/A */ 7982292SN/A std::string path; 7992292SN/A if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 8002292SN/A return -EFAULT; 8012292SN/A 8022292SN/A#ifdef __CYGWIN32__ 8032292SN/A int host_flags = O_BINARY; 8042292SN/A#else 8052292SN/A int host_flags = 0; 8062292SN/A#endif 8072292SN/A /** 8082292SN/A * Translate target flags into host flags. Flags exist which are not 8092292SN/A * ported between architectures which can cause check failures. 8102292SN/A */ 8112292SN/A int tgt_flags = p->getSyscallArg(tc, index); 8122292SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 8132292SN/A if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 8142292SN/A tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 8152292SN/A host_flags |= OS::openFlagTable[i].hostFlag; 8162292SN/A } 8172292SN/A } 8182292SN/A if (tgt_flags) { 8196221Snate@binkert.org warn("open%s: cannot decode flags 0x%x", 8202292SN/A isopenat ? "at" : "", tgt_flags); 8212292SN/A } 8222292SN/A#ifdef __CYGWIN32__ 8232292SN/A host_flags |= O_BINARY; 8242292SN/A#endif 8252292SN/A 8262292SN/A int mode = p->getSyscallArg(tc, index); 8272292SN/A 8282292SN/A /** 8292292SN/A * If the simulated process called open or openat with AT_FDCWD specified, 8302292SN/A * take the current working directory value which was passed into the 8312292SN/A * process class as a Python parameter and append the current path to 8322292SN/A * create a full path. 8332292SN/A * Otherwise, openat with a valid target directory file descriptor has 8342292SN/A * been called. If the path option, which was passed in as a parameter, 8352292SN/A * is not absolute, retrieve the directory file descriptor's path and 8362292SN/A * prepend it to the path passed in as a parameter. 8372292SN/A * In every case, we should have a full path (which is relevant to the 8382292SN/A * host) to work with after this block has been passed. 8392292SN/A */ 8402292SN/A if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 8412292SN/A path = p->fullPath(path); 8422292SN/A } else if (!startswith(path, "/")) { 8432292SN/A std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 8442292SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 8452702Sktlim@umich.edu if (!ffdp) 8462292SN/A return -EBADF; 8472292SN/A path.insert(0, ffdp->getFileName() + "/"); 8482702Sktlim@umich.edu } 8492702Sktlim@umich.edu 8502292SN/A /** 8512292SN/A * Since this is an emulated environment, we create pseudo file 8522292SN/A * descriptors for device requests that have been registered with 8532292SN/A * the process class through Python; this allows us to create a file 8542292SN/A * descriptor for subsequent ioctl or mmap calls. 8552292SN/A */ 8562292SN/A if (startswith(path, "/dev/")) { 8572292SN/A std::string filename = path.substr(strlen("/dev/")); 8582292SN/A EmulatedDriver *drv = p->findDriver(filename); 8592292SN/A if (drv) { 8602292SN/A DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 8612292SN/A "driver open with path[%s]\n", 8622292SN/A isopenat ? "at" : "", path.c_str()); 8632292SN/A return drv->open(p, tc, mode, host_flags); 8642292SN/A } 8652292SN/A /** 8662292SN/A * Fall through here for pass through to host devices, such 8672292SN/A * as /dev/zero 8682292SN/A */ 8692292SN/A } 8702292SN/A 8712292SN/A /** 8722292SN/A * Some special paths and files cannot be called on the host and need 8732292SN/A * to be handled as special cases inside the simulator. 8742292SN/A * If the full path that was created above does not match any of the 8752292SN/A * special cases, pass it through to the open call on the host to let 8762292SN/A * the host open the file on our behalf. 8772292SN/A * If the host cannot open the file, return the host's error code back 8782292SN/A * through the system call to the simulated process. 8792292SN/A */ 8802292SN/A int sim_fd = -1; 8812292SN/A std::vector<std::string> special_paths = 8822292SN/A { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 8832292SN/A for (auto entry : special_paths) { 8842292SN/A if (startswith(path, entry)) 8852292SN/A sim_fd = OS::openSpecialFile(path, p, tc); 8862292SN/A } 8872292SN/A if (sim_fd == -1) { 8882292SN/A sim_fd = open(path.c_str(), host_flags, mode); 8892326SN/A } 8906221Snate@binkert.org if (sim_fd == -1) { 8916221Snate@binkert.org int local = -errno; 8922326SN/A DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 8932292SN/A isopenat ? "at" : "", path.c_str()); 8942292SN/A return local; 8952292SN/A } 8962292SN/A 8972292SN/A /** 8982292SN/A * The file was opened successfully and needs to be recorded in the 8992292SN/A * process' file descriptor array so that it can be retrieved later. 9006221Snate@binkert.org * The target file descriptor that is chosen will be the lowest unused 9012702Sktlim@umich.edu * file descriptor. 9024632Sgblack@eecs.umich.edu * Return the indirect target file descriptor back to the simulated 9032935Sksewell@umich.edu * process to act as a handle for the opened file. 9042702Sktlim@umich.edu */ 9052935Sksewell@umich.edu auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 9062702Sktlim@umich.edu int tgt_fd = p->fds->allocFD(ffdp); 9072702Sktlim@umich.edu DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 9082702Sktlim@umich.edu isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 9092702Sktlim@umich.edu return tgt_fd; 9102702Sktlim@umich.edu} 9112702Sktlim@umich.edu 9122702Sktlim@umich.edu/// Target open() handler. 9132702Sktlim@umich.edutemplate <class OS> 9142702Sktlim@umich.eduSyscallReturn 9152702Sktlim@umich.eduopenFunc(SyscallDesc *desc, int callnum, Process *process, 9162702Sktlim@umich.edu ThreadContext *tc) 9172702Sktlim@umich.edu{ 9182702Sktlim@umich.edu return openImpl<OS>(desc, callnum, process, tc, false); 9192292SN/A} 9202292SN/A 9212292SN/A/// Target openat() handler. 9222292SN/Atemplate <class OS> 9232292SN/ASyscallReturn 9242292SN/AopenatFunc(SyscallDesc *desc, int callnum, Process *process, 9252292SN/A ThreadContext *tc) 9262292SN/A{ 9272292SN/A return openImpl<OS>(desc, callnum, process, tc, true); 9282292SN/A} 9292292SN/A 9302292SN/A/// Target unlinkat() handler. 9312292SN/Atemplate <class OS> 9322292SN/ASyscallReturn 9332292SN/AunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 9342292SN/A ThreadContext *tc) 9352292SN/A{ 9362292SN/A int index = 0; 9372733Sktlim@umich.edu int dirfd = process->getSyscallArg(tc, index); 9382292SN/A if (dirfd != OS::TGT_AT_FDCWD) 9392292SN/A warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 9402292SN/A 9412292SN/A return unlinkHelper(desc, callnum, process, tc, 1); 9422292SN/A} 9432292SN/A 9442292SN/A/// Target facessat() handler 9452733Sktlim@umich.edutemplate <class OS> 9462292SN/ASyscallReturn 9472292SN/AfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 9482292SN/A ThreadContext *tc) 9492292SN/A{ 9506221Snate@binkert.org int index = 0; 9512292SN/A int dirfd = process->getSyscallArg(tc, index); 9522292SN/A if (dirfd != OS::TGT_AT_FDCWD) 9532292SN/A warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 9542292SN/A return accessFunc(desc, callnum, process, tc, 1); 9552292SN/A} 9562292SN/A 9572292SN/A/// Target readlinkat() handler 9582292SN/Atemplate <class OS> 9592292SN/ASyscallReturn 9602292SN/AreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 9612292SN/A ThreadContext *tc) 9622292SN/A{ 9632292SN/A int index = 0; 9642292SN/A int dirfd = process->getSyscallArg(tc, index); 9652292SN/A if (dirfd != OS::TGT_AT_FDCWD) 9662292SN/A warn("openat: first argument not AT_FDCWD; unlikely to work"); 9672292SN/A return readlinkFunc(desc, callnum, process, tc, 1); 9682292SN/A} 9692292SN/A 9702292SN/A/// Target renameat() handler. 9712292SN/Atemplate <class OS> 9722292SN/ASyscallReturn 9732292SN/ArenameatFunc(SyscallDesc *desc, int callnum, Process *process, 9742292SN/A ThreadContext *tc) 9752292SN/A{ 9762292SN/A int index = 0; 9772292SN/A 9782292SN/A int olddirfd = process->getSyscallArg(tc, index); 9792292SN/A if (olddirfd != OS::TGT_AT_FDCWD) 9802292SN/A warn("renameat: first argument not AT_FDCWD; unlikely to work"); 9812292SN/A 9822292SN/A std::string old_name; 9832292SN/A 9842292SN/A if (!tc->getMemProxy().tryReadString(old_name, 9852292SN/A process->getSyscallArg(tc, index))) 9865215Sgblack@eecs.umich.edu return -EFAULT; 9872292SN/A 9882292SN/A int newdirfd = process->getSyscallArg(tc, index); 9892292SN/A if (newdirfd != OS::TGT_AT_FDCWD) 9902292SN/A warn("renameat: third argument not AT_FDCWD; unlikely to work"); 9912292SN/A 9922292SN/A std::string new_name; 9932292SN/A 9942292SN/A if (!tc->getMemProxy().tryReadString(new_name, 9952292SN/A process->getSyscallArg(tc, index))) 9962292SN/A return -EFAULT; 9972292SN/A 9986221Snate@binkert.org // Adjust path for current working directory 9992292SN/A old_name = process->fullPath(old_name); 10002292SN/A new_name = process->fullPath(new_name); 10012292SN/A 10022292SN/A int result = rename(old_name.c_str(), new_name.c_str()); 10032292SN/A return (result == -1) ? -errno : result; 10042292SN/A} 10052292SN/A 10062292SN/A/// Target sysinfo() handler. 10072292SN/Atemplate <class OS> 10082292SN/ASyscallReturn 10092292SN/AsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 10102292SN/A ThreadContext *tc) 10112292SN/A{ 10122292SN/A 10132292SN/A int index = 0; 10142292SN/A TypedBufferArg<typename OS::tgt_sysinfo> 10152820Sktlim@umich.edu sysinfo(process->getSyscallArg(tc, index)); 10162292SN/A 10172292SN/A sysinfo->uptime = seconds_since_epoch; 10182292SN/A sysinfo->totalram = process->system->memSize(); 10192292SN/A sysinfo->mem_unit = 1; 10202292SN/A 10212292SN/A sysinfo.copyOut(tc->getMemProxy()); 10222292SN/A 10232292SN/A return 0; 10242292SN/A} 10252292SN/A 10262292SN/A/// Target chmod() handler. 10272292SN/Atemplate <class OS> 10287720Sgblack@eecs.umich.eduSyscallReturn 10292292SN/AchmodFunc(SyscallDesc *desc, int callnum, Process *process, 10307720Sgblack@eecs.umich.edu ThreadContext *tc) 10312292SN/A{ 10322292SN/A std::string path; 10332292SN/A 10342292SN/A int index = 0; 10352292SN/A if (!tc->getMemProxy().tryReadString(path, 10362292SN/A process->getSyscallArg(tc, index))) { 10372292SN/A return -EFAULT; 10382292SN/A } 10392292SN/A 10402292SN/A uint32_t mode = process->getSyscallArg(tc, index); 10412292SN/A mode_t hostMode = 0; 10422292SN/A 10432292SN/A // XXX translate mode flags via OS::something??? 10442292SN/A hostMode = mode; 10452292SN/A 10462292SN/A // Adjust path for current working directory 10472292SN/A path = process->fullPath(path); 10482292SN/A 10492292SN/A // do the chmod 10502292SN/A int result = chmod(path.c_str(), hostMode); 10512292SN/A if (result < 0) 10522292SN/A return -errno; 10532292SN/A 10542292SN/A return 0; 10552292SN/A} 10562292SN/A 10572292SN/Atemplate <class OS> 10582292SN/ASyscallReturn 10592292SN/ApollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 10602292SN/A{ 10612292SN/A int index = 0; 10622292SN/A Addr fdsPtr = p->getSyscallArg(tc, index); 10632292SN/A int nfds = p->getSyscallArg(tc, index); 10642292SN/A int tmout = p->getSyscallArg(tc, index); 10652292SN/A 10662292SN/A BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 10672292SN/A fdsBuf.copyIn(tc->getMemProxy()); 10682292SN/A 10692292SN/A /** 10702292SN/A * Record the target file descriptors in a local variable. We need to 10712292SN/A * replace them with host file descriptors but we need a temporary copy 10722292SN/A * for later. Afterwards, replace each target file descriptor in the 10732292SN/A * poll_fd array with its host_fd. 10742292SN/A */ 10752292SN/A int temp_tgt_fds[nfds]; 10762292SN/A for (index = 0; index < nfds; index++) { 10772292SN/A temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 10782292SN/A auto tgt_fd = temp_tgt_fds[index]; 10792292SN/A auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 10802292SN/A if (!hbfdp) 10812292SN/A return -EBADF; 10822292SN/A auto host_fd = hbfdp->getSimFD(); 10832292SN/A ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 10842292SN/A } 10852292SN/A 10862292SN/A /** 10872292SN/A * We cannot allow an infinite poll to occur or it will inevitably cause 10882292SN/A * a deadlock in the gem5 simulator with clone. We must pass in tmout with 10892292SN/A * a non-negative value, however it also makes no sense to poll on the 10902292SN/A * underlying host for any other time than tmout a zero timeout. 10912292SN/A */ 10922292SN/A int status; 10932292SN/A if (tmout < 0) { 10942292SN/A status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 10952292SN/A if (status == 0) { 10962292SN/A /** 10972292SN/A * If blocking indefinitely, check the signal list to see if a 10982292SN/A * signal would break the poll out of the retry cycle and try 10992292SN/A * to return the signal interrupt instead. 11002292SN/A */ 11012292SN/A System *sysh = tc->getSystemPtr(); 11022292SN/A std::list<BasicSignal>::iterator it; 11032292SN/A for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 11042292SN/A if (it->receiver == p) 11052336SN/A return -EINTR; 11062336SN/A return SyscallReturn::retry(); 11072336SN/A } 11082336SN/A } else 11092348SN/A status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 11102292SN/A 11112292SN/A if (status == -1) 11122292SN/A return -errno; 11132292SN/A 11142292SN/A /** 11152292SN/A * Replace each host_fd in the returned poll_fd array with its original 11162292SN/A * target file descriptor. 11172292SN/A */ 11182292SN/A for (index = 0; index < nfds; index++) { 11192292SN/A auto tgt_fd = temp_tgt_fds[index]; 11202292SN/A ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 11212326SN/A } 11222292SN/A 11232292SN/A /** 11242292SN/A * Copy out the pollfd struct because the host may have updated fields 11252292SN/A * in the structure. 11262292SN/A */ 11272292SN/A fdsBuf.copyOut(tc->getMemProxy()); 11282292SN/A 11292292SN/A return status; 11302292SN/A} 11312292SN/A 11322292SN/A/// Target fchmod() handler. 11332326SN/Atemplate <class OS> 11342292SN/ASyscallReturn 11352727Sktlim@umich.edufchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 11362301SN/A{ 11372292SN/A int index = 0; 11382292SN/A int tgt_fd = p->getSyscallArg(tc, index); 11392292SN/A uint32_t mode = p->getSyscallArg(tc, index); 11402292SN/A 11412292SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 11422292SN/A if (!ffdp) 11432292SN/A return -EBADF; 11442292SN/A int sim_fd = ffdp->getSimFD(); 11452292SN/A 11462326SN/A mode_t hostMode = mode; 11472292SN/A 11482292SN/A int result = fchmod(sim_fd, hostMode); 11492292SN/A 11502292SN/A return (result < 0) ? -errno : 0; 11512292SN/A} 11524033Sktlim@umich.edu 11534033Sktlim@umich.edu/// Target mremap() handler. 11544033Sktlim@umich.edutemplate <class OS> 11554033Sktlim@umich.eduSyscallReturn 11564033Sktlim@umich.edumremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 11574033Sktlim@umich.edu{ 11584033Sktlim@umich.edu int index = 0; 11594033Sktlim@umich.edu Addr start = process->getSyscallArg(tc, index); 11604033Sktlim@umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 11614033Sktlim@umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 11624033Sktlim@umich.edu uint64_t flags = process->getSyscallArg(tc, index); 11634033Sktlim@umich.edu uint64_t provided_address = 0; 11644033Sktlim@umich.edu bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 11654033Sktlim@umich.edu 11662292SN/A if (use_provided_address) 11672292SN/A provided_address = process->getSyscallArg(tc, index); 11682292SN/A 11692292SN/A if ((start % TheISA::PageBytes != 0) || 11702292SN/A (provided_address % TheISA::PageBytes != 0)) { 11712292SN/A warn("mremap failing: arguments not page aligned"); 11722292SN/A return -EINVAL; 11732292SN/A } 11742292SN/A 11752292SN/A new_length = roundUp(new_length, TheISA::PageBytes); 11762292SN/A 11772292SN/A if (new_length > old_length) { 11788471SGiacomo.Gabrielli@arm.com std::shared_ptr<MemState> mem_state = process->memState; 11798471SGiacomo.Gabrielli@arm.com Addr mmap_end = mem_state->getMmapEnd(); 11809046SAli.Saidi@ARM.com 11818471SGiacomo.Gabrielli@arm.com if ((start + old_length) == mmap_end && 118210023Smatt.horsnell@ARM.com (!use_provided_address || provided_address == start)) { 11832292SN/A // This case cannot occur when growing downward, as 11842292SN/A // start is greater than or equal to mmap_end. 11852292SN/A uint64_t diff = new_length - old_length; 11862935Sksewell@umich.edu process->allocateMem(mmap_end, diff); 11872292SN/A mem_state->setMmapEnd(mmap_end + diff); 11882292SN/A return start; 11892292SN/A } else { 11902292SN/A if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 11912292SN/A warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 11922292SN/A return -ENOMEM; 11932292SN/A } else { 11942292SN/A uint64_t new_start = provided_address; 11952292SN/A if (!use_provided_address) { 11962292SN/A new_start = process->mmapGrowsDown() ? 11972292SN/A mmap_end - new_length : mmap_end; 11982292SN/A mmap_end = process->mmapGrowsDown() ? 11992292SN/A new_start : mmap_end + new_length; 12002292SN/A mem_state->setMmapEnd(mmap_end); 12012292SN/A } 12022292SN/A 12032292SN/A process->pTable->remap(start, old_length, new_start); 12042292SN/A warn("mremapping to new vaddr %08p-%08p, adding %d\n", 12052292SN/A new_start, new_start + new_length, 12062980Sgblack@eecs.umich.edu new_length - old_length); 12072292SN/A // add on the remaining unallocated pages 12082292SN/A process->allocateMem(new_start + old_length, 12092292SN/A new_length - old_length, 12102980Sgblack@eecs.umich.edu use_provided_address /* clobber */); 12112292SN/A if (use_provided_address && 12127720Sgblack@eecs.umich.edu ((new_start + new_length > mem_state->getMmapEnd() && 12132292SN/A !process->mmapGrowsDown()) || 12142292SN/A (new_start < mem_state->getMmapEnd() && 12152292SN/A process->mmapGrowsDown()))) { 12162292SN/A // something fishy going on here, at least notify the user 12172292SN/A // @todo: increase mmap_end? 12182292SN/A warn("mmap region limit exceeded with MREMAP_FIXED\n"); 12192292SN/A } 12202980Sgblack@eecs.umich.edu warn("returning %08p as start\n", new_start); 12212292SN/A return new_start; 12222292SN/A } 12232292SN/A } 12242292SN/A } else { 12252292SN/A if (use_provided_address && provided_address != start) 12262292SN/A process->pTable->remap(start, new_length, provided_address); 12272292SN/A process->pTable->unmap(start + new_length, old_length - new_length); 12282292SN/A return use_provided_address ? provided_address : start; 12292292SN/A } 12306221Snate@binkert.org} 12316221Snate@binkert.org 12322292SN/A/// Target stat() handler. 12333867Sbinkertn@umich.edutemplate <class OS> 12346221Snate@binkert.orgSyscallReturn 12352292SN/AstatFunc(SyscallDesc *desc, int callnum, Process *process, 12362292SN/A ThreadContext *tc) 12372292SN/A{ 12382698Sktlim@umich.edu std::string path; 12397599Sminkyu.jeong@arm.com 12402698Sktlim@umich.edu int index = 0; 12411062SN/A if (!tc->getMemProxy().tryReadString(path, 12421062SN/A process->getSyscallArg(tc, index))) { 12432333SN/A return -EFAULT; 12442292SN/A } 12452333SN/A Addr bufPtr = process->getSyscallArg(tc, index); 12462326SN/A 12471062SN/A // Adjust path for current working directory 12482292SN/A path = process->fullPath(path); 12491062SN/A 12502333SN/A struct stat hostBuf; 12511062SN/A int result = stat(path.c_str(), &hostBuf); 12527720Sgblack@eecs.umich.edu 12537720Sgblack@eecs.umich.edu if (result < 0) 12541062SN/A return -errno; 12551062SN/A 12561062SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12578315Sgeoffrey.blake@arm.com 12588315Sgeoffrey.blake@arm.com return 0; 12598315Sgeoffrey.blake@arm.com} 12601062SN/A 12611062SN/A 12621062SN/A/// Target stat64() handler. 12631062SN/Atemplate <class OS> 12641062SN/ASyscallReturn 12652292SN/Astat64Func(SyscallDesc *desc, int callnum, Process *process, 12662292SN/A ThreadContext *tc) 12672292SN/A{ 12681062SN/A std::string path; 12691062SN/A 12701062SN/A int index = 0; 12712820Sktlim@umich.edu if (!tc->getMemProxy().tryReadString(path, 12721062SN/A process->getSyscallArg(tc, index))) 12731062SN/A return -EFAULT; 12741062SN/A Addr bufPtr = process->getSyscallArg(tc, index); 12752292SN/A 12761062SN/A // Adjust path for current working directory 12771062SN/A path = process->fullPath(path); 12781062SN/A 12791062SN/A#if NO_STAT64 12807850SMatt.Horsnell@arm.com struct stat hostBuf; 12812292SN/A int result = stat(path.c_str(), &hostBuf); 12821062SN/A#else 12831062SN/A struct stat64 hostBuf; 12841062SN/A int result = stat64(path.c_str(), &hostBuf); 12851062SN/A#endif 12862292SN/A 12872292SN/A if (result < 0) 12882292SN/A return -errno; 12897944SGiacomo.Gabrielli@arm.com 12907944SGiacomo.Gabrielli@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12917944SGiacomo.Gabrielli@arm.com 12927944SGiacomo.Gabrielli@arm.com return 0; 12937944SGiacomo.Gabrielli@arm.com} 12947944SGiacomo.Gabrielli@arm.com 12957944SGiacomo.Gabrielli@arm.com 12967944SGiacomo.Gabrielli@arm.com/// Target fstatat64() handler. 12977944SGiacomo.Gabrielli@arm.comtemplate <class OS> 12987944SGiacomo.Gabrielli@arm.comSyscallReturn 12997944SGiacomo.Gabrielli@arm.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process, 13007850SMatt.Horsnell@arm.com ThreadContext *tc) 13018073SAli.Saidi@ARM.com{ 13027850SMatt.Horsnell@arm.com int index = 0; 13031062SN/A int dirfd = process->getSyscallArg(tc, index); 13042367SN/A if (dirfd != OS::TGT_AT_FDCWD) 13051062SN/A warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 13067944SGiacomo.Gabrielli@arm.com 13077944SGiacomo.Gabrielli@arm.com std::string path; 13087944SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(path, 13097944SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 13107944SGiacomo.Gabrielli@arm.com return -EFAULT; 13117944SGiacomo.Gabrielli@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 13127944SGiacomo.Gabrielli@arm.com 13137944SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 13147944SGiacomo.Gabrielli@arm.com path = process->fullPath(path); 13157944SGiacomo.Gabrielli@arm.com 13162292SN/A#if NO_STAT64 131710231Ssteve.reinhardt@amd.com struct stat hostBuf; 13187782Sminkyu.jeong@arm.com int result = stat(path.c_str(), &hostBuf); 13197782Sminkyu.jeong@arm.com#else 13207782Sminkyu.jeong@arm.com struct stat64 hostBuf; 13212367SN/A int result = stat64(path.c_str(), &hostBuf); 13222367SN/A#endif 13232367SN/A 13242367SN/A if (result < 0) 13252367SN/A return -errno; 13262292SN/A 13272326SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13282326SN/A 13292326SN/A return 0; 13302326SN/A} 13311062SN/A 13322292SN/A 13331062SN/A/// Target fstat64() handler. 13341062SN/Atemplate <class OS> 13351062SN/ASyscallReturn 13367847Sminkyu.jeong@arm.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 13377847Sminkyu.jeong@arm.com{ 13387847Sminkyu.jeong@arm.com int index = 0; 13397847Sminkyu.jeong@arm.com int tgt_fd = p->getSyscallArg(tc, index); 13407847Sminkyu.jeong@arm.com Addr bufPtr = p->getSyscallArg(tc, index); 13417847Sminkyu.jeong@arm.com 134210231Ssteve.reinhardt@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 13437848SAli.Saidi@ARM.com if (!ffdp) 13447847Sminkyu.jeong@arm.com return -EBADF; 13451062SN/A int sim_fd = ffdp->getSimFD(); 13462292SN/A 13472292SN/A#if NO_STAT64 13482292SN/A struct stat hostBuf; 13491062SN/A int result = fstat(sim_fd, &hostBuf); 13501062SN/A#else 13512301SN/A struct stat64 hostBuf; 13521681SN/A int result = fstat64(sim_fd, &hostBuf); 13532326SN/A#endif 13542326SN/A 13552326SN/A if (result < 0) 13562107SN/A return -errno; 13571681SN/A 13582292SN/A copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 13592292SN/A 13602292SN/A return 0; 13616221Snate@binkert.org} 13621062SN/A 13633732Sktlim@umich.edu 13647852SMatt.Horsnell@arm.com/// Target lstat() handler. 13653732Sktlim@umich.edutemplate <class OS> 13661062SN/ASyscallReturn 13677856SMatt.Horsnell@arm.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 13687856SMatt.Horsnell@arm.com ThreadContext *tc) 13697856SMatt.Horsnell@arm.com{ 13707856SMatt.Horsnell@arm.com std::string path; 13717856SMatt.Horsnell@arm.com 13722292SN/A int index = 0; 13731062SN/A if (!tc->getMemProxy().tryReadString(path, 13742292SN/A process->getSyscallArg(tc, index))) { 13758674Snilay@cs.wisc.edu return -EFAULT; 13768674Snilay@cs.wisc.edu } 13777720Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 13788674Snilay@cs.wisc.edu 13791062SN/A // Adjust path for current working directory 13802292SN/A path = process->fullPath(path); 13811062SN/A 138210023Smatt.horsnell@ARM.com struct stat hostBuf; 138310023Smatt.horsnell@ARM.com int result = lstat(path.c_str(), &hostBuf); 13843795Sgblack@eecs.umich.edu 13851062SN/A if (result < 0) 13862292SN/A return -errno; 13872292SN/A 13881062SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13892292SN/A 13904033Sktlim@umich.edu return 0; 13912326SN/A} 13922326SN/A 13932292SN/A/// Target lstat64() handler. 13942292SN/Atemplate <class OS> 13952292SN/ASyscallReturn 13961062SN/Alstat64Func(SyscallDesc *desc, int callnum, Process *process, 13977720Sgblack@eecs.umich.edu ThreadContext *tc) 13987720Sgblack@eecs.umich.edu{ 13997720Sgblack@eecs.umich.edu std::string path; 14007720Sgblack@eecs.umich.edu 14017720Sgblack@eecs.umich.edu int index = 0; 14023732Sktlim@umich.edu if (!tc->getMemProxy().tryReadString(path, 14033732Sktlim@umich.edu process->getSyscallArg(tc, index))) { 14041062SN/A return -EFAULT; 14051062SN/A } 14061062SN/A Addr bufPtr = process->getSyscallArg(tc, index); 14071062SN/A 14088513SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 14091062SN/A path = process->fullPath(path); 14101062SN/A 14112292SN/A#if NO_STAT64 14122292SN/A struct stat hostBuf; 14132292SN/A int result = lstat(path.c_str(), &hostBuf); 14142292SN/A#else 14152292SN/A struct stat64 hostBuf; 14167720Sgblack@eecs.umich.edu int result = lstat64(path.c_str(), &hostBuf); 14177720Sgblack@eecs.umich.edu#endif 14182292SN/A 14192292SN/A if (result < 0) 14201062SN/A return -errno; 14214033Sktlim@umich.edu 14224033Sktlim@umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 14234033Sktlim@umich.edu 14244033Sktlim@umich.edu return 0; 14254033Sktlim@umich.edu} 14264033Sktlim@umich.edu 14274033Sktlim@umich.edu/// Target fstat() handler. 14284033Sktlim@umich.edutemplate <class OS> 14294033Sktlim@umich.eduSyscallReturn 14307720Sgblack@eecs.umich.edufstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 14317720Sgblack@eecs.umich.edu{ 14327720Sgblack@eecs.umich.edu int index = 0; 14334033Sktlim@umich.edu int tgt_fd = p->getSyscallArg(tc, index); 14344033Sktlim@umich.edu Addr bufPtr = p->getSyscallArg(tc, index); 14354033Sktlim@umich.edu 14364033Sktlim@umich.edu DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 14374033Sktlim@umich.edu 14384033Sktlim@umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 14394033Sktlim@umich.edu if (!ffdp) 14404033Sktlim@umich.edu return -EBADF; 14417720Sgblack@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 14427720Sgblack@eecs.umich.edu 14434033Sktlim@umich.edu struct stat hostBuf; 14444033Sktlim@umich.edu int result = fstat(sim_fd, &hostBuf); 14454033Sktlim@umich.edu 14464033Sktlim@umich.edu if (result < 0) 14474033Sktlim@umich.edu return -errno; 14484033Sktlim@umich.edu 14491062SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 14501062SN/A 14512292SN/A return 0; 14522348SN/A} 14532292SN/A 14542292SN/A/// Target statfs() handler. 14552292SN/Atemplate <class OS> 14562292SN/ASyscallReturn 14572292SN/AstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 14582292SN/A ThreadContext *tc) 14592292SN/A{ 14602292SN/A#if NO_STATFS 14612292SN/A warn("Host OS cannot support calls to statfs. Ignoring syscall"); 14622292SN/A#else 14632292SN/A std::string path; 14642292SN/A 14652292SN/A int index = 0; 14662292SN/A if (!tc->getMemProxy().tryReadString(path, 14677852SMatt.Horsnell@arm.com process->getSyscallArg(tc, index))) { 14682107SN/A return -EFAULT; 14692107SN/A } 14702292SN/A Addr bufPtr = process->getSyscallArg(tc, index); 14712107SN/A 14722292SN/A // Adjust path for current working directory 14732107SN/A path = process->fullPath(path); 14742326SN/A 14752326SN/A struct statfs hostBuf; 14762326SN/A int result = statfs(path.c_str(), &hostBuf); 14772326SN/A 14782326SN/A if (result < 0) 14793958Sgblack@eecs.umich.edu return -errno; 14802292SN/A 14812107SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 14826221Snate@binkert.org#endif 14832107SN/A return 0; 14847720Sgblack@eecs.umich.edu} 14857720Sgblack@eecs.umich.edu 14862107SN/Atemplate <class OS> 14872301SN/ASyscallReturn 14882301SN/AcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 14892292SN/A{ 14902292SN/A int index = 0; 14912292SN/A 14922292SN/A RegVal flags = p->getSyscallArg(tc, index); 14932292SN/A RegVal newStack = p->getSyscallArg(tc, index); 14942367SN/A Addr ptidPtr = p->getSyscallArg(tc, index); 14952301SN/A 14962107SN/A#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 14972292SN/A /** 14982292SN/A * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 14992292SN/A * The flag defines the list of clone() arguments in the following 15002292SN/A * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 15012292SN/A */ 15022107SN/A Addr tlsPtr = p->getSyscallArg(tc, index); 15032301SN/A Addr ctidPtr = p->getSyscallArg(tc, index); 15042348SN/A#else 15052348SN/A Addr ctidPtr = p->getSyscallArg(tc, index); 15062348SN/A Addr tlsPtr = p->getSyscallArg(tc, index); 15072348SN/A#endif 15082326SN/A 15092107SN/A if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 15102820Sktlim@umich.edu ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 15112820Sktlim@umich.edu ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 15122107SN/A ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 15131060SN/A ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 15141060SN/A ((flags & OS::TGT_CLONE_VM) && !(newStack))) 15151681SN/A return -EINVAL; 15161060SN/A 15172292SN/A ThreadContext *ctc; 15181060SN/A if (!(ctc = p->findFreeContext())) { 15192292SN/A DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system" 15202292SN/A "[cpu %d, thread %d]", tc->cpuId(), tc->threadId()); 15211060SN/A return -EAGAIN; 15222292SN/A } 15232292SN/A 15241060SN/A /** 15252292SN/A * Note that ProcessParams is generated by swig and there are no other 15261060SN/A * examples of how to create anything but this default constructor. The 15272326SN/A * fields are manually initialized instead of passing parameters to the 15282326SN/A * constructor. 15291062SN/A */ 15306221Snate@binkert.org ProcessParams *pp = new ProcessParams(); 15316221Snate@binkert.org pp->executable.assign(*(new std::string(p->progName()))); 15321060SN/A pp->cmd.push_back(*(new std::string(p->progName()))); 15332326SN/A pp->system = p->system; 15343867Sbinkertn@umich.edu pp->cwd.assign(p->getcwd()); 15356221Snate@binkert.org pp->input.assign("stdin"); 15361060SN/A pp->output.assign("stdout"); 15372292SN/A pp->errout.assign("stderr"); 15381060SN/A pp->uid = p->uid(); 15392292SN/A pp->euid = p->euid(); 15402292SN/A pp->gid = p->gid(); 15411060SN/A pp->egid = p->egid(); 15421060SN/A 15432292SN/A /* Find the first free PID that's less than the maximum */ 15442292SN/A std::set<int> const& pids = p->system->PIDs; 15451060SN/A int temp_pid = *pids.begin(); 15462292SN/A do { 15472292SN/A temp_pid++; 15482292SN/A } while (pids.find(temp_pid) != pids.end()); 15492292SN/A if (temp_pid >= System::maxPID) 15502292SN/A fatal("temp_pid is too large: %d", temp_pid); 15512292SN/A 15522292SN/A pp->pid = temp_pid; 15532292SN/A pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 15542292SN/A pp->useArchPT = p->useArchPT; 15552292SN/A pp->kvmInSE = p->kvmInSE; 15562292SN/A Process *cp = pp->create(); 15572292SN/A delete pp; 15582292SN/A 15592292SN/A Process *owner = ctc->getProcessPtr(); 15602292SN/A ctc->setProcessPtr(cp); 15612292SN/A cp->assignThreadContext(ctc->contextId()); 15622292SN/A owner->revokeThreadContext(ctc->contextId()); 15632292SN/A 15642292SN/A if (flags & OS::TGT_CLONE_PARENT_SETTID) { 15652292SN/A BufferArg ptidBuf(ptidPtr, sizeof(long)); 15662292SN/A long *ptid = (long *)ptidBuf.bufferPtr(); 15672292SN/A *ptid = cp->pid(); 15681681SN/A ptidBuf.copyOut(tc->getMemProxy()); 15691681SN/A } 15701061SN/A 15711061SN/A if (flags & OS::TGT_CLONE_THREAD) { 15721061SN/A cp->pTable->shared = true; 15731681SN/A cp->useForClone = true; 15742292SN/A } 15753867Sbinkertn@umich.edu cp->initState(); 15763867Sbinkertn@umich.edu p->clone(tc, ctc, cp, flags); 15776221Snate@binkert.org 15782292SN/A if (flags & OS::TGT_CLONE_THREAD) { 15792292SN/A delete cp->sigchld; 15802292SN/A cp->sigchld = p->sigchld; 15812348SN/A } else if (flags & OS::TGT_SIGCHLD) { 15822292SN/A *cp->sigchld = true; 15832292SN/A } 15842292SN/A 15852292SN/A if (flags & OS::TGT_CLONE_CHILD_SETTID) { 15862292SN/A BufferArg ctidBuf(ctidPtr, sizeof(long)); 15872292SN/A long *ctid = (long *)ctidBuf.bufferPtr(); 15882292SN/A *ctid = cp->pid(); 15892292SN/A ctidBuf.copyOut(ctc->getMemProxy()); 15902292SN/A } 15912292SN/A 15922292SN/A if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 15932292SN/A cp->childClearTID = (uint64_t)ctidPtr; 15942292SN/A 15952292SN/A ctc->clearArchRegs(); 15962292SN/A 15972292SN/A OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 15982292SN/A 15994033Sktlim@umich.edu cp->setSyscallReturn(ctc, 0); 16002292SN/A 16012292SN/A#if THE_ISA == ALPHA_ISA 16022292SN/A ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 16032292SN/A#elif THE_ISA == SPARC_ISA 16042292SN/A tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 16052292SN/A ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 16062292SN/A#endif 16072292SN/A 16082292SN/A if (p->kvmInSE) { 16092292SN/A#if THE_ISA == X86_ISA 16102292SN/A ctc->pcState(tc->readIntReg(TheISA::INTREG_RCX)); 16112292SN/A#else 16122292SN/A panic("KVM CPU model is not supported for this ISA"); 16132292SN/A#endif 161410164Ssleimanf@umich.edu } else { 16152292SN/A TheISA::PCState cpc = tc->pcState(); 16162292SN/A cpc.advance(); 16172292SN/A ctc->pcState(cpc); 16182292SN/A } 16192292SN/A ctc->activate(); 16202292SN/A 16212292SN/A return cp->pid(); 16222292SN/A} 16232292SN/A 16241061SN/A/// Target fstatfs() handler. 16251061SN/Atemplate <class OS> 16262292SN/ASyscallReturn 16272292SN/AfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 16282292SN/A{ 16292292SN/A int index = 0; 16302292SN/A int tgt_fd = p->getSyscallArg(tc, index); 16312292SN/A Addr bufPtr = p->getSyscallArg(tc, index); 16322292SN/A 16332292SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16342292SN/A if (!ffdp) 16352292SN/A return -EBADF; 16361061SN/A int sim_fd = ffdp->getSimFD(); 16371060SN/A 16381060SN/A struct statfs hostBuf; 16392301SN/A int result = fstatfs(sim_fd, &hostBuf); 16401060SN/A 16412301SN/A if (result < 0) 16421060SN/A return -errno; 16436221Snate@binkert.org 16441060SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 16452669Sktlim@umich.edu 16461060SN/A return 0; 16478471SGiacomo.Gabrielli@arm.com} 16489527SMatt.Horsnell@arm.com 16499527SMatt.Horsnell@arm.com/// Target readv() handler. 16509527SMatt.Horsnell@arm.comtemplate <class OS> 16518471SGiacomo.Gabrielli@arm.comSyscallReturn 16528471SGiacomo.Gabrielli@arm.comreadvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 16532301SN/A{ 16542301SN/A int index = 0; 16552301SN/A int tgt_fd = p->getSyscallArg(tc, index); 16562301SN/A 16576221Snate@binkert.org auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 16581060SN/A if (!ffdp) 16592301SN/A return -EBADF; 16602301SN/A int sim_fd = ffdp->getSimFD(); 16612301SN/A 16622301SN/A SETranslatingPortProxy &prox = tc->getMemProxy(); 16636221Snate@binkert.org uint64_t tiov_base = p->getSyscallArg(tc, index); 16641060SN/A size_t count = p->getSyscallArg(tc, index); 16652301SN/A typename OS::tgt_iovec tiov[count]; 16666221Snate@binkert.org struct iovec hiov[count]; 16671060SN/A for (size_t i = 0; i < count; ++i) { 16681060SN/A prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)), 16691060SN/A (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec)); 16707598Sminkyu.jeong@arm.com hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len); 16717598Sminkyu.jeong@arm.com hiov[i].iov_base = new char [hiov[i].iov_len]; 16727598Sminkyu.jeong@arm.com } 16737598Sminkyu.jeong@arm.com 16747598Sminkyu.jeong@arm.com int result = readv(sim_fd, hiov, count); 16757598Sminkyu.jeong@arm.com int local_errno = errno; 16767598Sminkyu.jeong@arm.com 16777598Sminkyu.jeong@arm.com for (size_t i = 0; i < count; ++i) { 16787852SMatt.Horsnell@arm.com if (result != -1) { 16797598Sminkyu.jeong@arm.com prox.writeBlob(TheISA::htog(tiov[i].iov_base), 16807598Sminkyu.jeong@arm.com (uint8_t*)hiov[i].iov_base, hiov[i].iov_len); 16817598Sminkyu.jeong@arm.com } 16827598Sminkyu.jeong@arm.com delete [] (char *)hiov[i].iov_base; 16837598Sminkyu.jeong@arm.com } 16847598Sminkyu.jeong@arm.com 16857598Sminkyu.jeong@arm.com return (result == -1) ? -local_errno : result; 16867720Sgblack@eecs.umich.edu} 16877598Sminkyu.jeong@arm.com 16887720Sgblack@eecs.umich.edu/// Target writev() handler. 16897720Sgblack@eecs.umich.edutemplate <class OS> 16907598Sminkyu.jeong@arm.comSyscallReturn 16917598Sminkyu.jeong@arm.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 16927598Sminkyu.jeong@arm.com{ 16937598Sminkyu.jeong@arm.com int index = 0; 16947598Sminkyu.jeong@arm.com int tgt_fd = p->getSyscallArg(tc, index); 16957598Sminkyu.jeong@arm.com 16967598Sminkyu.jeong@arm.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 16977598Sminkyu.jeong@arm.com if (!hbfdp) 16987598Sminkyu.jeong@arm.com return -EBADF; 16997598Sminkyu.jeong@arm.com int sim_fd = hbfdp->getSimFD(); 17007598Sminkyu.jeong@arm.com 17019944Smatt.horsnell@ARM.com SETranslatingPortProxy &prox = tc->getMemProxy(); 17029944Smatt.horsnell@ARM.com uint64_t tiov_base = p->getSyscallArg(tc, index); 1703 size_t count = p->getSyscallArg(tc, index); 1704 struct iovec hiov[count]; 1705 for (size_t i = 0; i < count; ++i) { 1706 typename OS::tgt_iovec tiov; 1707 1708 prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 1709 (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 1710 hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1711 hiov[i].iov_base = new char [hiov[i].iov_len]; 1712 prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 1713 hiov[i].iov_len); 1714 } 1715 1716 int result = writev(sim_fd, hiov, count); 1717 1718 for (size_t i = 0; i < count; ++i) 1719 delete [] (char *)hiov[i].iov_base; 1720 1721 return (result == -1) ? -errno : result; 1722} 1723 1724/// Real mmap handler. 1725template <class OS> 1726SyscallReturn 1727mmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1728 bool is_mmap2) 1729{ 1730 int index = 0; 1731 Addr start = p->getSyscallArg(tc, index); 1732 uint64_t length = p->getSyscallArg(tc, index); 1733 int prot = p->getSyscallArg(tc, index); 1734 int tgt_flags = p->getSyscallArg(tc, index); 1735 int tgt_fd = p->getSyscallArg(tc, index); 1736 int offset = p->getSyscallArg(tc, index); 1737 1738 if (is_mmap2) 1739 offset *= TheISA::PageBytes; 1740 1741 if (start & (TheISA::PageBytes - 1) || 1742 offset & (TheISA::PageBytes - 1) || 1743 (tgt_flags & OS::TGT_MAP_PRIVATE && 1744 tgt_flags & OS::TGT_MAP_SHARED) || 1745 (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 1746 !(tgt_flags & OS::TGT_MAP_SHARED)) || 1747 !length) { 1748 return -EINVAL; 1749 } 1750 1751 if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 1752 // With shared mmaps, there are two cases to consider: 1753 // 1) anonymous: writes should modify the mapping and this should be 1754 // visible to observers who share the mapping. Currently, it's 1755 // difficult to update the shared mapping because there's no 1756 // structure which maintains information about the which virtual 1757 // memory areas are shared. If that structure existed, it would be 1758 // possible to make the translations point to the same frames. 1759 // 2) file-backed: writes should modify the mapping and the file 1760 // which is backed by the mapping. The shared mapping problem is the 1761 // same as what was mentioned about the anonymous mappings. For 1762 // file-backed mappings, the writes to the file are difficult 1763 // because it requires syncing what the mapping holds with the file 1764 // that resides on the host system. So, any write on a real system 1765 // would cause the change to be propagated to the file mapping at 1766 // some point in the future (the inode is tracked along with the 1767 // mapping). This isn't guaranteed to always happen, but it usually 1768 // works well enough. The guarantee is provided by the msync system 1769 // call. We could force the change through with shared mappings with 1770 // a call to msync, but that again would require more information 1771 // than we currently maintain. 1772 warn("mmap: writing to shared mmap region is currently " 1773 "unsupported. The write succeeds on the target, but it " 1774 "will not be propagated to the host or shared mappings"); 1775 } 1776 1777 length = roundUp(length, TheISA::PageBytes); 1778 1779 int sim_fd = -1; 1780 uint8_t *pmap = nullptr; 1781 if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 1782 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1783 1784 auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 1785 if (dfdp) { 1786 EmulatedDriver *emul_driver = dfdp->getDriver(); 1787 return emul_driver->mmap(p, tc, start, length, prot, 1788 tgt_flags, tgt_fd, offset); 1789 } 1790 1791 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1792 if (!ffdp) 1793 return -EBADF; 1794 sim_fd = ffdp->getSimFD(); 1795 1796 pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 1797 sim_fd, offset); 1798 1799 if (pmap == (decltype(pmap))-1) { 1800 warn("mmap: failed to map file into host address space"); 1801 return -errno; 1802 } 1803 } 1804 1805 // Extend global mmap region if necessary. Note that we ignore the 1806 // start address unless MAP_FIXED is specified. 1807 if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 1808 std::shared_ptr<MemState> mem_state = p->memState; 1809 Addr mmap_end = mem_state->getMmapEnd(); 1810 1811 start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 1812 mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 1813 1814 mem_state->setMmapEnd(mmap_end); 1815 } 1816 1817 DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 1818 start, start + length - 1); 1819 1820 // We only allow mappings to overwrite existing mappings if 1821 // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 1822 // because we ignore the start hint if TGT_MAP_FIXED is not set. 1823 int clobber = tgt_flags & OS::TGT_MAP_FIXED; 1824 if (clobber) { 1825 for (auto tc : p->system->threadContexts) { 1826 // If we might be overwriting old mappings, we need to 1827 // invalidate potentially stale mappings out of the TLBs. 1828 tc->getDTBPtr()->flushAll(); 1829 tc->getITBPtr()->flushAll(); 1830 } 1831 } 1832 1833 // Allocate physical memory and map it in. If the page table is already 1834 // mapped and clobber is not set, the simulator will issue throw a 1835 // fatal and bail out of the simulation. 1836 p->allocateMem(start, length, clobber); 1837 1838 // Transfer content into target address space. 1839 SETranslatingPortProxy &tp = tc->getMemProxy(); 1840 if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 1841 // In general, we should zero the mapped area for anonymous mappings, 1842 // with something like: 1843 // tp.memsetBlob(start, 0, length); 1844 // However, given that we don't support sparse mappings, and 1845 // some applications can map a couple of gigabytes of space 1846 // (intending sparse usage), that can get painfully expensive. 1847 // Fortunately, since we don't properly implement munmap either, 1848 // there's no danger of remapping used memory, so for now all 1849 // newly mapped memory should already be zeroed so we can skip it. 1850 } else { 1851 // It is possible to mmap an area larger than a file, however 1852 // accessing unmapped portions the system triggers a "Bus error" 1853 // on the host. We must know when to stop copying the file from 1854 // the host into the target address space. 1855 struct stat file_stat; 1856 if (fstat(sim_fd, &file_stat) > 0) 1857 fatal("mmap: cannot stat file"); 1858 1859 // Copy the portion of the file that is resident. This requires 1860 // checking both the mmap size and the filesize that we are 1861 // trying to mmap into this space; the mmap size also depends 1862 // on the specified offset into the file. 1863 uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 1864 length); 1865 tp.writeBlob(start, pmap, size); 1866 1867 // Cleanup the mmap region before exiting this function. 1868 munmap(pmap, length); 1869 1870 // Maintain the symbol table for dynamic executables. 1871 // The loader will call mmap to map the images into its address 1872 // space and we intercept that here. We can verify that we are 1873 // executing inside the loader by checking the program counter value. 1874 // XXX: with multiprogrammed workloads or multi-node configurations, 1875 // this will not work since there is a single global symbol table. 1876 ObjectFile *interpreter = p->getInterpreter(); 1877 if (interpreter) { 1878 Addr text_start = interpreter->textBase(); 1879 Addr text_end = text_start + interpreter->textSize(); 1880 1881 Addr pc = tc->pcState().pc(); 1882 1883 if (pc >= text_start && pc < text_end) { 1884 std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 1885 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 1886 ObjectFile *lib = createObjectFile(ffdp->getFileName()); 1887 1888 if (lib) { 1889 lib->loadAllSymbols(debugSymbolTable, 1890 lib->textBase(), start); 1891 } 1892 } 1893 } 1894 1895 // Note that we do not zero out the remainder of the mapping. This 1896 // is done by a real system, but it probably will not affect 1897 // execution (hopefully). 1898 } 1899 1900 return start; 1901} 1902 1903template <class OS> 1904SyscallReturn 1905pwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1906{ 1907 int index = 0; 1908 int tgt_fd = p->getSyscallArg(tc, index); 1909 Addr bufPtr = p->getSyscallArg(tc, index); 1910 int nbytes = p->getSyscallArg(tc, index); 1911 int offset = p->getSyscallArg(tc, index); 1912 1913 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 1914 if (!ffdp) 1915 return -EBADF; 1916 int sim_fd = ffdp->getSimFD(); 1917 1918 BufferArg bufArg(bufPtr, nbytes); 1919 bufArg.copyIn(tc->getMemProxy()); 1920 1921 int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 1922 1923 return (bytes_written == -1) ? -errno : bytes_written; 1924} 1925 1926/// Target mmap() handler. 1927template <class OS> 1928SyscallReturn 1929mmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1930{ 1931 return mmapImpl<OS>(desc, num, p, tc, false); 1932} 1933 1934/// Target mmap2() handler. 1935template <class OS> 1936SyscallReturn 1937mmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 1938{ 1939 return mmapImpl<OS>(desc, num, p, tc, true); 1940} 1941 1942/// Target getrlimit() handler. 1943template <class OS> 1944SyscallReturn 1945getrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1946 ThreadContext *tc) 1947{ 1948 int index = 0; 1949 unsigned resource = process->getSyscallArg(tc, index); 1950 TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1951 1952 switch (resource) { 1953 case OS::TGT_RLIMIT_STACK: 1954 // max stack size in bytes: make up a number (8MB for now) 1955 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 1956 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1957 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1958 break; 1959 1960 case OS::TGT_RLIMIT_DATA: 1961 // max data segment size in bytes: make up a number 1962 rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 1963 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 1964 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 1965 break; 1966 1967 default: 1968 warn("getrlimit: unimplemented resource %d", resource); 1969 return -EINVAL; 1970 break; 1971 } 1972 1973 rlp.copyOut(tc->getMemProxy()); 1974 return 0; 1975} 1976 1977template <class OS> 1978SyscallReturn 1979prlimitFunc(SyscallDesc *desc, int callnum, Process *process, 1980 ThreadContext *tc) 1981{ 1982 int index = 0; 1983 if (process->getSyscallArg(tc, index) != 0) 1984 { 1985 warn("prlimit: ignoring rlimits for nonzero pid"); 1986 return -EPERM; 1987 } 1988 int resource = process->getSyscallArg(tc, index); 1989 Addr n = process->getSyscallArg(tc, index); 1990 if (n != 0) 1991 warn("prlimit: ignoring new rlimit"); 1992 Addr o = process->getSyscallArg(tc, index); 1993 if (o != 0) 1994 { 1995 TypedBufferArg<typename OS::rlimit> rlp(o); 1996 switch (resource) { 1997 case OS::TGT_RLIMIT_STACK: 1998 // max stack size in bytes: make up a number (8MB for now) 1999 rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 2000 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2001 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2002 break; 2003 case OS::TGT_RLIMIT_DATA: 2004 // max data segment size in bytes: make up a number 2005 rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 2006 rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 2007 rlp->rlim_max = TheISA::htog(rlp->rlim_max); 2008 break; 2009 default: 2010 warn("prlimit: unimplemented resource %d", resource); 2011 return -EINVAL; 2012 break; 2013 } 2014 rlp.copyOut(tc->getMemProxy()); 2015 } 2016 return 0; 2017} 2018 2019/// Target clock_gettime() function. 2020template <class OS> 2021SyscallReturn 2022clock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2023{ 2024 int index = 1; 2025 //int clk_id = p->getSyscallArg(tc, index); 2026 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 2027 2028 getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 2029 tp->tv_sec += seconds_since_epoch; 2030 tp->tv_sec = TheISA::htog(tp->tv_sec); 2031 tp->tv_nsec = TheISA::htog(tp->tv_nsec); 2032 2033 tp.copyOut(tc->getMemProxy()); 2034 2035 return 0; 2036} 2037 2038/// Target clock_getres() function. 2039template <class OS> 2040SyscallReturn 2041clock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2042{ 2043 int index = 1; 2044 TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 2045 2046 // Set resolution at ns, which is what clock_gettime() returns 2047 tp->tv_sec = 0; 2048 tp->tv_nsec = 1; 2049 2050 tp.copyOut(tc->getMemProxy()); 2051 2052 return 0; 2053} 2054 2055/// Target gettimeofday() handler. 2056template <class OS> 2057SyscallReturn 2058gettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 2059 ThreadContext *tc) 2060{ 2061 int index = 0; 2062 TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 2063 2064 getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 2065 tp->tv_sec += seconds_since_epoch; 2066 tp->tv_sec = TheISA::htog(tp->tv_sec); 2067 tp->tv_usec = TheISA::htog(tp->tv_usec); 2068 2069 tp.copyOut(tc->getMemProxy()); 2070 2071 return 0; 2072} 2073 2074 2075/// Target utimes() handler. 2076template <class OS> 2077SyscallReturn 2078utimesFunc(SyscallDesc *desc, int callnum, Process *process, 2079 ThreadContext *tc) 2080{ 2081 std::string path; 2082 2083 int index = 0; 2084 if (!tc->getMemProxy().tryReadString(path, 2085 process->getSyscallArg(tc, index))) { 2086 return -EFAULT; 2087 } 2088 2089 TypedBufferArg<typename OS::timeval [2]> 2090 tp(process->getSyscallArg(tc, index)); 2091 tp.copyIn(tc->getMemProxy()); 2092 2093 struct timeval hostTimeval[2]; 2094 for (int i = 0; i < 2; ++i) { 2095 hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 2096 hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 2097 } 2098 2099 // Adjust path for current working directory 2100 path = process->fullPath(path); 2101 2102 int result = utimes(path.c_str(), hostTimeval); 2103 2104 if (result < 0) 2105 return -errno; 2106 2107 return 0; 2108} 2109 2110template <class OS> 2111SyscallReturn 2112execveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 2113{ 2114 desc->setFlags(0); 2115 2116 int index = 0; 2117 std::string path; 2118 SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 2119 if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 2120 return -EFAULT; 2121 2122 if (access(path.c_str(), F_OK) == -1) 2123 return -EACCES; 2124 2125 auto read_in = [](std::vector<std::string> & vect, 2126 SETranslatingPortProxy & mem_proxy, 2127 Addr mem_loc) 2128 { 2129 for (int inc = 0; ; inc++) { 2130 BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 2131 b.copyIn(mem_proxy); 2132 2133 if (!*(Addr*)b.bufferPtr()) 2134 break; 2135 2136 vect.push_back(std::string()); 2137 mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 2138 } 2139 }; 2140 2141 /** 2142 * Note that ProcessParams is generated by swig and there are no other 2143 * examples of how to create anything but this default constructor. The 2144 * fields are manually initialized instead of passing parameters to the 2145 * constructor. 2146 */ 2147 ProcessParams *pp = new ProcessParams(); 2148 pp->executable = path; 2149 Addr argv_mem_loc = p->getSyscallArg(tc, index); 2150 read_in(pp->cmd, mem_proxy, argv_mem_loc); 2151 Addr envp_mem_loc = p->getSyscallArg(tc, index); 2152 read_in(pp->env, mem_proxy, envp_mem_loc); 2153 pp->uid = p->uid(); 2154 pp->egid = p->egid(); 2155 pp->euid = p->euid(); 2156 pp->gid = p->gid(); 2157 pp->ppid = p->ppid(); 2158 pp->pid = p->pid(); 2159 pp->input.assign("cin"); 2160 pp->output.assign("cout"); 2161 pp->errout.assign("cerr"); 2162 pp->cwd.assign(p->getcwd()); 2163 pp->system = p->system; 2164 /** 2165 * Prevent process object creation with identical PIDs (which will trip 2166 * a fatal check in Process constructor). The execve call is supposed to 2167 * take over the currently executing process' identity but replace 2168 * whatever it is doing with a new process image. Instead of hijacking 2169 * the process object in the simulator, we create a new process object 2170 * and bind to the previous process' thread below (hijacking the thread). 2171 */ 2172 p->system->PIDs.erase(p->pid()); 2173 Process *new_p = pp->create(); 2174 delete pp; 2175 2176 /** 2177 * Work through the file descriptor array and close any files marked 2178 * close-on-exec. 2179 */ 2180 new_p->fds = p->fds; 2181 for (int i = 0; i < new_p->fds->getSize(); i++) { 2182 std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 2183 if (fdep && fdep->getCOE()) 2184 new_p->fds->closeFDEntry(i); 2185 } 2186 2187 *new_p->sigchld = true; 2188 2189 delete p; 2190 tc->clearArchRegs(); 2191 tc->setProcessPtr(new_p); 2192 new_p->assignThreadContext(tc->contextId()); 2193 new_p->initState(); 2194 tc->activate(); 2195 TheISA::PCState pcState = tc->pcState(); 2196 tc->setNPC(pcState.instAddr()); 2197 2198 desc->setFlags(SyscallDesc::SuppressReturnValue); 2199 return 0; 2200} 2201 2202/// Target getrusage() function. 2203template <class OS> 2204SyscallReturn 2205getrusageFunc(SyscallDesc *desc, int callnum, Process *process, 2206 ThreadContext *tc) 2207{ 2208 int index = 0; 2209 int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 2210 TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2211 2212 rup->ru_utime.tv_sec = 0; 2213 rup->ru_utime.tv_usec = 0; 2214 rup->ru_stime.tv_sec = 0; 2215 rup->ru_stime.tv_usec = 0; 2216 rup->ru_maxrss = 0; 2217 rup->ru_ixrss = 0; 2218 rup->ru_idrss = 0; 2219 rup->ru_isrss = 0; 2220 rup->ru_minflt = 0; 2221 rup->ru_majflt = 0; 2222 rup->ru_nswap = 0; 2223 rup->ru_inblock = 0; 2224 rup->ru_oublock = 0; 2225 rup->ru_msgsnd = 0; 2226 rup->ru_msgrcv = 0; 2227 rup->ru_nsignals = 0; 2228 rup->ru_nvcsw = 0; 2229 rup->ru_nivcsw = 0; 2230 2231 switch (who) { 2232 case OS::TGT_RUSAGE_SELF: 2233 getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 2234 rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 2235 rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 2236 break; 2237 2238 case OS::TGT_RUSAGE_CHILDREN: 2239 // do nothing. We have no child processes, so they take no time. 2240 break; 2241 2242 default: 2243 // don't really handle THREAD or CHILDREN, but just warn and 2244 // plow ahead 2245 warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 2246 who); 2247 } 2248 2249 rup.copyOut(tc->getMemProxy()); 2250 2251 return 0; 2252} 2253 2254/// Target times() function. 2255template <class OS> 2256SyscallReturn 2257timesFunc(SyscallDesc *desc, int callnum, Process *process, 2258 ThreadContext *tc) 2259{ 2260 int index = 0; 2261 TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 2262 2263 // Fill in the time structure (in clocks) 2264 int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 2265 bufp->tms_utime = clocks; 2266 bufp->tms_stime = 0; 2267 bufp->tms_cutime = 0; 2268 bufp->tms_cstime = 0; 2269 2270 // Convert to host endianness 2271 bufp->tms_utime = TheISA::htog(bufp->tms_utime); 2272 2273 // Write back 2274 bufp.copyOut(tc->getMemProxy()); 2275 2276 // Return clock ticks since system boot 2277 return clocks; 2278} 2279 2280/// Target time() function. 2281template <class OS> 2282SyscallReturn 2283timeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 2284{ 2285 typename OS::time_t sec, usec; 2286 getElapsedTimeMicro(sec, usec); 2287 sec += seconds_since_epoch; 2288 2289 int index = 0; 2290 Addr taddr = (Addr)process->getSyscallArg(tc, index); 2291 if (taddr != 0) { 2292 typename OS::time_t t = sec; 2293 t = TheISA::htog(t); 2294 SETranslatingPortProxy &p = tc->getMemProxy(); 2295 p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 2296 } 2297 return sec; 2298} 2299 2300template <class OS> 2301SyscallReturn 2302tgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 2303{ 2304 int index = 0; 2305 int tgid = process->getSyscallArg(tc, index); 2306 int tid = process->getSyscallArg(tc, index); 2307 int sig = process->getSyscallArg(tc, index); 2308 2309 /** 2310 * This system call is intended to allow killing a specific thread 2311 * within an arbitrary thread group if sanctioned with permission checks. 2312 * It's usually true that threads share the termination signal as pointed 2313 * out by the pthread_kill man page and this seems to be the intended 2314 * usage. Due to this being an emulated environment, assume the following: 2315 * Threads are allowed to call tgkill because the EUID for all threads 2316 * should be the same. There is no signal handling mechanism for kernel 2317 * registration of signal handlers since signals are poorly supported in 2318 * emulation mode. Since signal handlers cannot be registered, all 2319 * threads within in a thread group must share the termination signal. 2320 * We never exhaust PIDs so there's no chance of finding the wrong one 2321 * due to PID rollover. 2322 */ 2323 2324 System *sys = tc->getSystemPtr(); 2325 Process *tgt_proc = nullptr; 2326 for (int i = 0; i < sys->numContexts(); i++) { 2327 Process *temp = sys->threadContexts[i]->getProcessPtr(); 2328 if (temp->pid() == tid) { 2329 tgt_proc = temp; 2330 break; 2331 } 2332 } 2333 2334 if (sig != 0 || sig != OS::TGT_SIGABRT) 2335 return -EINVAL; 2336 2337 if (tgt_proc == nullptr) 2338 return -ESRCH; 2339 2340 if (tgid != -1 && tgt_proc->tgid() != tgid) 2341 return -ESRCH; 2342 2343 if (sig == OS::TGT_SIGABRT) 2344 exitGroupFunc(desc, 252, process, tc); 2345 2346 return 0; 2347} 2348 2349template <class OS> 2350SyscallReturn 2351socketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2352{ 2353 int index = 0; 2354 int domain = p->getSyscallArg(tc, index); 2355 int type = p->getSyscallArg(tc, index); 2356 int prot = p->getSyscallArg(tc, index); 2357 2358 int sim_fd = socket(domain, type, prot); 2359 if (sim_fd == -1) 2360 return -errno; 2361 2362 auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 2363 int tgt_fd = p->fds->allocFD(sfdp); 2364 2365 return tgt_fd; 2366} 2367 2368template <class OS> 2369SyscallReturn 2370socketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2371{ 2372 int index = 0; 2373 int domain = p->getSyscallArg(tc, index); 2374 int type = p->getSyscallArg(tc, index); 2375 int prot = p->getSyscallArg(tc, index); 2376 Addr svPtr = p->getSyscallArg(tc, index); 2377 2378 BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 2379 int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 2380 if (status == -1) 2381 return -errno; 2382 2383 int *fds = (int *)svBuf.bufferPtr(); 2384 2385 auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 2386 fds[0] = p->fds->allocFD(sfdp1); 2387 auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 2388 fds[1] = p->fds->allocFD(sfdp2); 2389 svBuf.copyOut(tc->getMemProxy()); 2390 2391 return status; 2392} 2393 2394template <class OS> 2395SyscallReturn 2396selectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 2397{ 2398 int retval; 2399 2400 int index = 0; 2401 int nfds_t = p->getSyscallArg(tc, index); 2402 Addr fds_read_ptr = p->getSyscallArg(tc, index); 2403 Addr fds_writ_ptr = p->getSyscallArg(tc, index); 2404 Addr fds_excp_ptr = p->getSyscallArg(tc, index); 2405 Addr time_val_ptr = p->getSyscallArg(tc, index); 2406 2407 TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 2408 TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 2409 TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 2410 TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 2411 2412 /** 2413 * Host fields. Notice that these use the definitions from the system 2414 * headers instead of the gem5 headers and libraries. If the host and 2415 * target have different header file definitions, this will not work. 2416 */ 2417 fd_set rd_h; 2418 FD_ZERO(&rd_h); 2419 fd_set wr_h; 2420 FD_ZERO(&wr_h); 2421 fd_set ex_h; 2422 FD_ZERO(&ex_h); 2423 2424 /** 2425 * Copy in the fd_set from the target. 2426 */ 2427 if (fds_read_ptr) 2428 rd_t.copyIn(tc->getMemProxy()); 2429 if (fds_writ_ptr) 2430 wr_t.copyIn(tc->getMemProxy()); 2431 if (fds_excp_ptr) 2432 ex_t.copyIn(tc->getMemProxy()); 2433 2434 /** 2435 * We need to translate the target file descriptor set into a host file 2436 * descriptor set. This involves both our internal process fd array 2437 * and the fd_set defined in Linux header files. The nfds field also 2438 * needs to be updated as it will be only target specific after 2439 * retrieving it from the target; the nfds value is expected to be the 2440 * highest file descriptor that needs to be checked, so we need to extend 2441 * it out for nfds_h when we do the update. 2442 */ 2443 int nfds_h = 0; 2444 std::map<int, int> trans_map; 2445 auto try_add_host_set = [&](fd_set *tgt_set_entry, 2446 fd_set *hst_set_entry, 2447 int iter) -> bool 2448 { 2449 /** 2450 * By this point, we know that we are looking at a valid file 2451 * descriptor set on the target. We need to check if the target file 2452 * descriptor value passed in as iter is part of the set. 2453 */ 2454 if (FD_ISSET(iter, tgt_set_entry)) { 2455 /** 2456 * We know that the target file descriptor belongs to the set, 2457 * but we do not yet know if the file descriptor is valid or 2458 * that we have a host mapping. Check that now. 2459 */ 2460 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 2461 if (!hbfdp) 2462 return true; 2463 auto sim_fd = hbfdp->getSimFD(); 2464 2465 /** 2466 * Add the sim_fd to tgt_fd translation into trans_map for use 2467 * later when we need to zero the target fd_set structures and 2468 * then update them with hits returned from the host select call. 2469 */ 2470 trans_map[sim_fd] = iter; 2471 2472 /** 2473 * We know that the host file descriptor exists so now we check 2474 * if we need to update the max count for nfds_h before passing 2475 * the duplicated structure into the host. 2476 */ 2477 nfds_h = std::max(nfds_h - 1, sim_fd + 1); 2478 2479 /** 2480 * Add the host file descriptor to the set that we are going to 2481 * pass into the host. 2482 */ 2483 FD_SET(sim_fd, hst_set_entry); 2484 } 2485 return false; 2486 }; 2487 2488 for (int i = 0; i < nfds_t; i++) { 2489 if (fds_read_ptr) { 2490 bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 2491 if (ebadf) return -EBADF; 2492 } 2493 if (fds_writ_ptr) { 2494 bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 2495 if (ebadf) return -EBADF; 2496 } 2497 if (fds_excp_ptr) { 2498 bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 2499 if (ebadf) return -EBADF; 2500 } 2501 } 2502 2503 if (time_val_ptr) { 2504 /** 2505 * It might be possible to decrement the timeval based on some 2506 * derivation of wall clock determined from elapsed simulator ticks 2507 * but that seems like overkill. Rather, we just set the timeval with 2508 * zero timeout. (There is no reason to block during the simulation 2509 * as it only decreases simulator performance.) 2510 */ 2511 tp->tv_sec = 0; 2512 tp->tv_usec = 0; 2513 2514 retval = select(nfds_h, 2515 fds_read_ptr ? &rd_h : nullptr, 2516 fds_writ_ptr ? &wr_h : nullptr, 2517 fds_excp_ptr ? &ex_h : nullptr, 2518 (timeval*)&*tp); 2519 } else { 2520 /** 2521 * If the timeval pointer is null, setup a new timeval structure to 2522 * pass into the host select call. Unfortunately, we will need to 2523 * manually check the return value and throw a retry fault if the 2524 * return value is zero. Allowing the system call to block will 2525 * likely deadlock the event queue. 2526 */ 2527 struct timeval tv = { 0, 0 }; 2528 2529 retval = select(nfds_h, 2530 fds_read_ptr ? &rd_h : nullptr, 2531 fds_writ_ptr ? &wr_h : nullptr, 2532 fds_excp_ptr ? &ex_h : nullptr, 2533 &tv); 2534 2535 if (retval == 0) { 2536 /** 2537 * If blocking indefinitely, check the signal list to see if a 2538 * signal would break the poll out of the retry cycle and try to 2539 * return the signal interrupt instead. 2540 */ 2541 for (auto sig : tc->getSystemPtr()->signalList) 2542 if (sig.receiver == p) 2543 return -EINTR; 2544 return SyscallReturn::retry(); 2545 } 2546 } 2547 2548 if (retval == -1) 2549 return -errno; 2550 2551 FD_ZERO((fd_set*)&*rd_t); 2552 FD_ZERO((fd_set*)&*wr_t); 2553 FD_ZERO((fd_set*)&*ex_t); 2554 2555 /** 2556 * We need to translate the host file descriptor set into a target file 2557 * descriptor set. This involves both our internal process fd array 2558 * and the fd_set defined in header files. 2559 */ 2560 for (int i = 0; i < nfds_h; i++) { 2561 if (fds_read_ptr) { 2562 if (FD_ISSET(i, &rd_h)) 2563 FD_SET(trans_map[i], (fd_set*)&*rd_t); 2564 } 2565 2566 if (fds_writ_ptr) { 2567 if (FD_ISSET(i, &wr_h)) 2568 FD_SET(trans_map[i], (fd_set*)&*wr_t); 2569 } 2570 2571 if (fds_excp_ptr) { 2572 if (FD_ISSET(i, &ex_h)) 2573 FD_SET(trans_map[i], (fd_set*)&*ex_t); 2574 } 2575 } 2576 2577 if (fds_read_ptr) 2578 rd_t.copyOut(tc->getMemProxy()); 2579 if (fds_writ_ptr) 2580 wr_t.copyOut(tc->getMemProxy()); 2581 if (fds_excp_ptr) 2582 ex_t.copyOut(tc->getMemProxy()); 2583 if (time_val_ptr) 2584 tp.copyOut(tc->getMemProxy()); 2585 2586 return retval; 2587} 2588 2589template <class OS> 2590SyscallReturn 2591readFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2592{ 2593 int index = 0; 2594 int tgt_fd = p->getSyscallArg(tc, index); 2595 Addr buf_ptr = p->getSyscallArg(tc, index); 2596 int nbytes = p->getSyscallArg(tc, index); 2597 2598 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2599 if (!hbfdp) 2600 return -EBADF; 2601 int sim_fd = hbfdp->getSimFD(); 2602 2603 struct pollfd pfd; 2604 pfd.fd = sim_fd; 2605 pfd.events = POLLIN | POLLPRI; 2606 if ((poll(&pfd, 1, 0) == 0) 2607 && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2608 return SyscallReturn::retry(); 2609 2610 BufferArg buf_arg(buf_ptr, nbytes); 2611 int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 2612 2613 if (bytes_read > 0) 2614 buf_arg.copyOut(tc->getMemProxy()); 2615 2616 return (bytes_read == -1) ? -errno : bytes_read; 2617} 2618 2619template <class OS> 2620SyscallReturn 2621writeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2622{ 2623 int index = 0; 2624 int tgt_fd = p->getSyscallArg(tc, index); 2625 Addr buf_ptr = p->getSyscallArg(tc, index); 2626 int nbytes = p->getSyscallArg(tc, index); 2627 2628 auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 2629 if (!hbfdp) 2630 return -EBADF; 2631 int sim_fd = hbfdp->getSimFD(); 2632 2633 BufferArg buf_arg(buf_ptr, nbytes); 2634 buf_arg.copyIn(tc->getMemProxy()); 2635 2636 struct pollfd pfd; 2637 pfd.fd = sim_fd; 2638 pfd.events = POLLOUT; 2639 2640 /** 2641 * We don't want to poll on /dev/random. The kernel will not enable the 2642 * file descriptor for writing unless the entropy in the system falls 2643 * below write_wakeup_threshold. This is not guaranteed to happen 2644 * depending on host settings. 2645 */ 2646 auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 2647 if (ffdp && (ffdp->getFileName() != "/dev/random")) { 2648 if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 2649 return SyscallReturn::retry(); 2650 } 2651 2652 int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 2653 2654 if (bytes_written != -1) 2655 fsync(sim_fd); 2656 2657 return (bytes_written == -1) ? -errno : bytes_written; 2658} 2659 2660template <class OS> 2661SyscallReturn 2662wait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2663{ 2664 int index = 0; 2665 pid_t pid = p->getSyscallArg(tc, index); 2666 Addr statPtr = p->getSyscallArg(tc, index); 2667 int options = p->getSyscallArg(tc, index); 2668 Addr rusagePtr = p->getSyscallArg(tc, index); 2669 2670 if (rusagePtr) 2671 DPRINTFR(SyscallVerbose, 2672 "%d: %s: syscall wait4: rusage pointer provided however " 2673 "functionality not supported. Ignoring rusage pointer.\n", 2674 curTick(), tc->getCpuPtr()->name()); 2675 2676 /** 2677 * Currently, wait4 is only implemented so that it will wait for children 2678 * exit conditions which are denoted by a SIGCHLD signals posted into the 2679 * system signal list. We return no additional information via any of the 2680 * parameters supplied to wait4. If nothing is found in the system signal 2681 * list, we will wait indefinitely for SIGCHLD to post by retrying the 2682 * call. 2683 */ 2684 System *sysh = tc->getSystemPtr(); 2685 std::list<BasicSignal>::iterator iter; 2686 for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 2687 if (iter->receiver == p) { 2688 if (pid < -1) { 2689 if ((iter->sender->pgid() == -pid) 2690 && (iter->signalValue == OS::TGT_SIGCHLD)) 2691 goto success; 2692 } else if (pid == -1) { 2693 if (iter->signalValue == OS::TGT_SIGCHLD) 2694 goto success; 2695 } else if (pid == 0) { 2696 if ((iter->sender->pgid() == p->pgid()) 2697 && (iter->signalValue == OS::TGT_SIGCHLD)) 2698 goto success; 2699 } else { 2700 if ((iter->sender->pid() == pid) 2701 && (iter->signalValue == OS::TGT_SIGCHLD)) 2702 goto success; 2703 } 2704 } 2705 } 2706 2707 return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 2708 2709success: 2710 // Set status to EXITED for WIFEXITED evaluations. 2711 const int EXITED = 0; 2712 BufferArg statusBuf(statPtr, sizeof(int)); 2713 *(int *)statusBuf.bufferPtr() = EXITED; 2714 statusBuf.copyOut(tc->getMemProxy()); 2715 2716 // Return the child PID. 2717 pid_t retval = iter->sender->pid(); 2718 sysh->signalList.erase(iter); 2719 return retval; 2720} 2721 2722template <class OS> 2723SyscallReturn 2724acceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 2725{ 2726 struct sockaddr sa; 2727 socklen_t addrLen; 2728 int host_fd; 2729 int index = 0; 2730 int tgt_fd = p->getSyscallArg(tc, index); 2731 Addr addrPtr = p->getSyscallArg(tc, index); 2732 Addr lenPtr = p->getSyscallArg(tc, index); 2733 2734 BufferArg *lenBufPtr = nullptr; 2735 BufferArg *addrBufPtr = nullptr; 2736 2737 auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 2738 if (!sfdp) 2739 return -EBADF; 2740 int sim_fd = sfdp->getSimFD(); 2741 2742 /** 2743 * We poll the socket file descriptor first to guarantee that we do not 2744 * block on our accept call. The socket can be opened without the 2745 * non-blocking flag (it blocks). This will cause deadlocks between 2746 * communicating processes. 2747 */ 2748 struct pollfd pfd; 2749 pfd.fd = sim_fd; 2750 pfd.events = POLLIN | POLLPRI; 2751 if ((poll(&pfd, 1, 0) == 0) 2752 && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 2753 return SyscallReturn::retry(); 2754 2755 if (lenPtr) { 2756 lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 2757 lenBufPtr->copyIn(tc->getMemProxy()); 2758 memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 2759 sizeof(socklen_t)); 2760 } 2761 2762 if (addrPtr) { 2763 addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 2764 addrBufPtr->copyIn(tc->getMemProxy()); 2765 memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 2766 sizeof(struct sockaddr)); 2767 } 2768 2769 host_fd = accept(sim_fd, &sa, &addrLen); 2770 2771 if (host_fd == -1) 2772 return -errno; 2773 2774 if (addrPtr) { 2775 memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 2776 addrBufPtr->copyOut(tc->getMemProxy()); 2777 delete(addrBufPtr); 2778 } 2779 2780 if (lenPtr) { 2781 *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 2782 lenBufPtr->copyOut(tc->getMemProxy()); 2783 delete(lenBufPtr); 2784 } 2785 2786 auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 2787 sfdp->_type, sfdp->_protocol); 2788 return p->fds->allocFD(afdp); 2789} 2790 2791#endif // __SIM_SYSCALL_EMUL_HH__ 2792