syscall_emul.hh revision 13650
1360SN/A/* 210850SGiacomo.Gabrielli@arm.com * Copyright (c) 2012-2013, 2015 ARM Limited 310796Sbrandon.potter@amd.com * Copyright (c) 2015 Advanced Micro Devices, Inc. 410027SChris.Adeniyi-Jones@arm.com * All rights reserved 510027SChris.Adeniyi-Jones@arm.com * 610027SChris.Adeniyi-Jones@arm.com * The license below extends only to copyright in the software and shall 710027SChris.Adeniyi-Jones@arm.com * not be construed as granting a license to any other intellectual 810027SChris.Adeniyi-Jones@arm.com * property including but not limited to intellectual property relating 910027SChris.Adeniyi-Jones@arm.com * to a hardware implementation of the functionality of the software 1010027SChris.Adeniyi-Jones@arm.com * licensed hereunder. You may use the software subject to the license 1110027SChris.Adeniyi-Jones@arm.com * terms below provided that you ensure that this notice is replicated 1210027SChris.Adeniyi-Jones@arm.com * unmodified and in its entirety in all distributions of the software, 1310027SChris.Adeniyi-Jones@arm.com * modified or unmodified, in source code or in binary form. 1410027SChris.Adeniyi-Jones@arm.com * 151458SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan 16360SN/A * All rights reserved. 17360SN/A * 18360SN/A * Redistribution and use in source and binary forms, with or without 19360SN/A * modification, are permitted provided that the following conditions are 20360SN/A * met: redistributions of source code must retain the above copyright 21360SN/A * notice, this list of conditions and the following disclaimer; 22360SN/A * redistributions in binary form must reproduce the above copyright 23360SN/A * notice, this list of conditions and the following disclaimer in the 24360SN/A * documentation and/or other materials provided with the distribution; 25360SN/A * neither the name of the copyright holders nor the names of its 26360SN/A * contributors may be used to endorse or promote products derived from 27360SN/A * this software without specific prior written permission. 28360SN/A * 29360SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30360SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31360SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 32360SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33360SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 34360SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 35360SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 36360SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 37360SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 38360SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 39360SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 412665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt 422665Ssaidi@eecs.umich.edu * Kevin Lim 43360SN/A */ 44360SN/A 451354SN/A#ifndef __SIM_SYSCALL_EMUL_HH__ 461354SN/A#define __SIM_SYSCALL_EMUL_HH__ 47360SN/A 4812018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 4912018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__CYGWIN__) || \ 5012018Sandreas.sandberg@arm.com defined(__NetBSD__)) 5112018Sandreas.sandberg@arm.com#define NO_STAT64 1 5212018Sandreas.sandberg@arm.com#else 5312018Sandreas.sandberg@arm.com#define NO_STAT64 0 5412018Sandreas.sandberg@arm.com#endif 552064SN/A 5612018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 5712018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__NetBSD__)) 5812018Sandreas.sandberg@arm.com#define NO_STATFS 1 5912018Sandreas.sandberg@arm.com#else 6012018Sandreas.sandberg@arm.com#define NO_STATFS 0 6112018Sandreas.sandberg@arm.com#endif 6211799Sbrandon.potter@amd.com 6312018Sandreas.sandberg@arm.com#if (defined(__APPLE__) || defined(__OpenBSD__) || \ 6412018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__NetBSD__)) 6512018Sandreas.sandberg@arm.com#define NO_FALLOCATE 1 6612018Sandreas.sandberg@arm.com#else 6712018Sandreas.sandberg@arm.com#define NO_FALLOCATE 0 6812018Sandreas.sandberg@arm.com#endif 6911799Sbrandon.potter@amd.com 70360SN/A/// 71360SN/A/// @file syscall_emul.hh 72360SN/A/// 73360SN/A/// This file defines objects used to emulate syscalls from the target 74360SN/A/// application on the host machine. 75360SN/A 761809SN/A#ifdef __CYGWIN32__ 7711800Sbrandon.potter@amd.com#include <sys/fcntl.h> 7811392Sbrandon.potter@amd.com 791809SN/A#endif 8011392Sbrandon.potter@amd.com#include <fcntl.h> 8113570Sbrandon.potter@amd.com#include <poll.h> 8211383Sbrandon.potter@amd.com#include <sys/mman.h> 8313568Sbrandon.potter@amd.com#include <sys/socket.h> 843113Sgblack@eecs.umich.edu#include <sys/stat.h> 8511799Sbrandon.potter@amd.com#if (NO_STATFS == 0) 8611759Sbrandon.potter@amd.com#include <sys/statfs.h> 8711812Sbaz21@cam.ac.uk#else 8811812Sbaz21@cam.ac.uk#include <sys/mount.h> 8911799Sbrandon.potter@amd.com#endif 908229Snate@binkert.org#include <sys/time.h> 9113570Sbrandon.potter@amd.com#include <sys/types.h> 928229Snate@binkert.org#include <sys/uio.h> 9311594Santhony.gutierrez@amd.com#include <unistd.h> 947075Snate@binkert.org 958229Snate@binkert.org#include <cerrno> 9611856Sbrandon.potter@amd.com#include <memory> 977075Snate@binkert.org#include <string> 98360SN/A 9912461Sgabeblack@google.com#include "arch/generic/tlb.hh" 10011886Sbrandon.potter@amd.com#include "arch/utility.hh" 10111800Sbrandon.potter@amd.com#include "base/intmath.hh" 10211392Sbrandon.potter@amd.com#include "base/loader/object_file.hh" 10312334Sgabeblack@google.com#include "base/logging.hh" 1041354SN/A#include "base/trace.hh" 1056216Snate@binkert.org#include "base/types.hh" 1066658Snate@binkert.org#include "config/the_isa.hh" 1072474SN/A#include "cpu/base.hh" 1082680Sktlim@umich.edu#include "cpu/thread_context.hh" 1098229Snate@binkert.org#include "mem/page_table.hh" 11011886Sbrandon.potter@amd.com#include "params/Process.hh" 11110496Ssteve.reinhardt@amd.com#include "sim/emul_driver.hh" 11211911SBrandon.Potter@amd.com#include "sim/futex_map.hh" 1138229Snate@binkert.org#include "sim/process.hh" 11411794Sbrandon.potter@amd.com#include "sim/syscall_debug_macros.hh" 11511886Sbrandon.potter@amd.com#include "sim/syscall_desc.hh" 11610497Ssteve.reinhardt@amd.com#include "sim/syscall_emul_buf.hh" 11711794Sbrandon.potter@amd.com#include "sim/syscall_return.hh" 118360SN/A 11913629SAndrea.Mondelli@ucf.edu#if defined(__APPLE__) && defined(__MACH__) && !defined(CMSG_ALIGN) 12013629SAndrea.Mondelli@ucf.edu#define CMSG_ALIGN(len) (((len) + sizeof(size_t) - 1) & ~(sizeof(size_t) - 1)) 12113629SAndrea.Mondelli@ucf.edu#endif 12213629SAndrea.Mondelli@ucf.edu 123360SN/A////////////////////////////////////////////////////////////////////// 124360SN/A// 125360SN/A// The following emulation functions are generic enough that they 126360SN/A// don't need to be recompiled for different emulated OS's. They are 127360SN/A// defined in sim/syscall_emul.cc. 128360SN/A// 129360SN/A////////////////////////////////////////////////////////////////////// 130360SN/A 131360SN/A 132378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1331706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 13411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 135378SN/A 136378SN/A/// Handler for unimplemented syscalls that we never intend to 137378SN/A/// implement (signal handling, etc.) and should not affect the correct 138378SN/A/// behavior of the program. Print a warning only if the appropriate 139378SN/A/// trace flag is enabled. Return success to the target program. 1401706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 14111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 142360SN/A 14311760Sbrandon.potter@amd.com// Target fallocateFunc() handler. 14411760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 14511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 14611760Sbrandon.potter@amd.com 1476109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1481706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 14911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 150378SN/A 1516109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1526109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 15311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1546109Ssanchezd@stanford.edu 15511886Sbrandon.potter@amd.com/// Target set_tid_address() handler. 15611886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 15711886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 15811886Sbrandon.potter@amd.com 159378SN/A/// Target getpagesize() handler. 1601706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 16111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 162378SN/A 1635748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 1645748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 16511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 166378SN/A 167378SN/A/// Target close() handler. 1681706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 16911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 170378SN/A 171378SN/A/// Target lseek() handler. 1721706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 17311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 174378SN/A 1754118Sgblack@eecs.umich.edu/// Target _llseek() handler. 1764118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 17711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1784118Sgblack@eecs.umich.edu 179378SN/A/// Target munmap() handler. 1801706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 18111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 182378SN/A 18313568Sbrandon.potter@amd.com/// Target shutdown() handler. 18413568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num, 18513568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 18613568Sbrandon.potter@amd.com 187378SN/A/// Target gethostname() handler. 1881706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 18911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 190360SN/A 1915513SMichael.Adler@intel.com/// Target getcwd() handler. 1925513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 19311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1945513SMichael.Adler@intel.com 19510203SAli.Saidi@ARM.com/// Target readlink() handler. 19610203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 19810203SAli.Saidi@ARM.com int index = 0); 1995513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 20011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2015513SMichael.Adler@intel.com 202511SN/A/// Target unlink() handler. 20310633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 20411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 20510633Smichaelupton@gmail.com int index); 2061706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 20711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 208511SN/A 20912795Smattdsinclair@gmail.com/// Target link() handler 21012795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p, 21112795Smattdsinclair@gmail.com ThreadContext *tc); 21212795Smattdsinclair@gmail.com 21312796Smattdsinclair@gmail.com/// Target symlink() handler. 21412796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p, 21512796Smattdsinclair@gmail.com ThreadContext *tc); 21612796Smattdsinclair@gmail.com 2175513SMichael.Adler@intel.com/// Target mkdir() handler. 2185513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 21911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2205513SMichael.Adler@intel.com 22113031Sbrandon.potter@amd.com/// Target mknod() handler. 22213031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, 22313031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22413031Sbrandon.potter@amd.com 22513031Sbrandon.potter@amd.com/// Target chdir() handler. 22613031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num, 22713031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22813031Sbrandon.potter@amd.com 22913031Sbrandon.potter@amd.com// Target rmdir() handler. 23013031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num, 23113031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 23213031Sbrandon.potter@amd.com 233511SN/A/// Target rename() handler. 2341706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 23511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2361706SN/A 2371706SN/A 2381706SN/A/// Target truncate() handler. 2391706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 24011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2411706SN/A 2421706SN/A 2431706SN/A/// Target ftruncate() handler. 2441706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 24511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2461706SN/A 247511SN/A 2486703Svince@csl.cornell.edu/// Target truncate64() handler. 2496703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 25011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2516703Svince@csl.cornell.edu 2526685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2536685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 25411851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2556685Stjones1@inf.ed.ac.uk 2566685Stjones1@inf.ed.ac.uk 2575513SMichael.Adler@intel.com/// Target umask() handler. 2585513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 25911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2605513SMichael.Adler@intel.com 26111885Sbrandon.potter@amd.com/// Target gettid() handler. 26211885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 26311885Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2645513SMichael.Adler@intel.com 2651999SN/A/// Target chown() handler. 2661999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 26711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2681999SN/A 26911885Sbrandon.potter@amd.com/// Target setpgid() handler. 27011885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 27111885Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2721999SN/A 2731999SN/A/// Target fchown() handler. 2741999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 27511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2761999SN/A 2773079Sstever@eecs.umich.edu/// Target dup() handler. 2783079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 27911851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2803079Sstever@eecs.umich.edu 28111908SBrandon.Potter@amd.com/// Target dup2() handler. 28211908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 28311908SBrandon.Potter@amd.com Process *process, ThreadContext *tc); 28411908SBrandon.Potter@amd.com 28511875Sbrandon.potter@amd.com/// Target fcntl() handler. 2862093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 28711851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2882093SN/A 2892687Sksewell@umich.edu/// Target fcntl64() handler. 2902687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 29111851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2922687Sksewell@umich.edu 2932238SN/A/// Target setuid() handler. 2942238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 29511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2962238SN/A 29711908SBrandon.Potter@amd.com/// Target pipe() handler. 29811908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 29911908SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 30011908SBrandon.Potter@amd.com 30111908SBrandon.Potter@amd.com/// Internal pipe() handler. 30211908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 30311908SBrandon.Potter@amd.com ThreadContext *tc, bool pseudoPipe); 30411908SBrandon.Potter@amd.com 3052238SN/A/// Target getpid() handler. 3062238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 30711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3082238SN/A 30913571Sbrandon.potter@amd.com// Target getpeername() handler. 31013571Sbrandon.potter@amd.comSyscallReturn getpeernameFunc(SyscallDesc *desc, int num, 31113571Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 31213571Sbrandon.potter@amd.com 31313568Sbrandon.potter@amd.com// Target bind() handler. 31413568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num, 31513568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 31613568Sbrandon.potter@amd.com 31713568Sbrandon.potter@amd.com// Target listen() handler. 31813568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num, 31913568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 32013568Sbrandon.potter@amd.com 32113568Sbrandon.potter@amd.com// Target connect() handler. 32213568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num, 32313568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 32413568Sbrandon.potter@amd.com 32513448Sciro.santilli@arm.com#if defined(SYS_getdents) 32613031Sbrandon.potter@amd.com// Target getdents() handler. 32713031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num, 32813031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 32913448Sciro.santilli@arm.com#endif 33013031Sbrandon.potter@amd.com 33113539Sjavier.setoain@arm.com#if defined(SYS_getdents64) 33213539Sjavier.setoain@arm.com// Target getdents() handler. 33313539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num, 33413539Sjavier.setoain@arm.com Process *p, ThreadContext *tc); 33513539Sjavier.setoain@arm.com#endif 33613539Sjavier.setoain@arm.com 33713569Sbrandon.potter@amd.com// Target sendto() handler. 33813569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num, 33913569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 34013569Sbrandon.potter@amd.com 34113569Sbrandon.potter@amd.com// Target recvfrom() handler. 34213569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num, 34313569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 34413569Sbrandon.potter@amd.com 34513569Sbrandon.potter@amd.com// Target recvmsg() handler. 34613569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num, 34713569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 34813569Sbrandon.potter@amd.com 34913569Sbrandon.potter@amd.com// Target sendmsg() handler. 35013569Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num, 35113569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 35213569Sbrandon.potter@amd.com 35313031Sbrandon.potter@amd.com// Target getuid() handler. 3542238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 35511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3562238SN/A 3572238SN/A/// Target getgid() handler. 3582238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 35911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3602238SN/A 3612238SN/A/// Target getppid() handler. 3622238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 36311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3642238SN/A 3652238SN/A/// Target geteuid() handler. 3662238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 36711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3682238SN/A 3692238SN/A/// Target getegid() handler. 3702238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 37111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3722238SN/A 3739455Smitch.hayenga+gem5@gmail.com/// Target access() handler 3749455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 37511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 37610203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 37711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 37811851Sbrandon.potter@amd.com int index); 3799455Smitch.hayenga+gem5@gmail.com 38013571Sbrandon.potter@amd.com// Target getsockopt() handler. 38113571Sbrandon.potter@amd.comSyscallReturn getsockoptFunc(SyscallDesc *desc, int num, 38213571Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 38313571Sbrandon.potter@amd.com 38413571Sbrandon.potter@amd.com// Target setsockopt() handler. 38513571Sbrandon.potter@amd.comSyscallReturn setsockoptFunc(SyscallDesc *desc, int num, 38613571Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 38713571Sbrandon.potter@amd.com 38813571Sbrandon.potter@amd.com// Target getsockname() handler. 38913571Sbrandon.potter@amd.comSyscallReturn getsocknameFunc(SyscallDesc *desc, int num, 39013571Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 39113571Sbrandon.potter@amd.com 3929112Smarc.orr@gmail.com/// Futex system call 39311906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez 39411906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps 3959112Smarc.orr@gmail.comtemplate <class OS> 3969112Smarc.orr@gmail.comSyscallReturn 39711851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3989112Smarc.orr@gmail.com ThreadContext *tc) 3999112Smarc.orr@gmail.com{ 40011911SBrandon.Potter@amd.com using namespace std; 4019112Smarc.orr@gmail.com 40211911SBrandon.Potter@amd.com int index = 0; 40311911SBrandon.Potter@amd.com Addr uaddr = process->getSyscallArg(tc, index); 40411911SBrandon.Potter@amd.com int op = process->getSyscallArg(tc, index); 40511911SBrandon.Potter@amd.com int val = process->getSyscallArg(tc, index); 40613642Sqtt2@cornell.edu int timeout M5_VAR_USED = process->getSyscallArg(tc, index); 40713642Sqtt2@cornell.edu Addr uaddr2 M5_VAR_USED = process->getSyscallArg(tc, index); 40813642Sqtt2@cornell.edu int val3 = process->getSyscallArg(tc, index); 4099112Smarc.orr@gmail.com 41011911SBrandon.Potter@amd.com /* 41111911SBrandon.Potter@amd.com * Unsupported option that does not affect the correctness of the 41211911SBrandon.Potter@amd.com * application. This is a performance optimization utilized by Linux. 41311911SBrandon.Potter@amd.com */ 4149238Slluc.alvarez@bsc.es op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 41513642Sqtt2@cornell.edu op &= ~OS::TGT_FUTEX_CLOCK_REALTIME_FLAG; 4169112Smarc.orr@gmail.com 41711911SBrandon.Potter@amd.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 4189112Smarc.orr@gmail.com 41913642Sqtt2@cornell.edu if (OS::TGT_FUTEX_WAIT == op || OS::TGT_FUTEX_WAIT_BITSET == op) { 42011911SBrandon.Potter@amd.com // Ensure futex system call accessed atomically. 42111911SBrandon.Potter@amd.com BufferArg buf(uaddr, sizeof(int)); 42211911SBrandon.Potter@amd.com buf.copyIn(tc->getMemProxy()); 42311911SBrandon.Potter@amd.com int mem_val = *(int*)buf.bufferPtr(); 4249112Smarc.orr@gmail.com 42511911SBrandon.Potter@amd.com /* 42611911SBrandon.Potter@amd.com * The value in memory at uaddr is not equal with the expected val 42711911SBrandon.Potter@amd.com * (a different thread must have changed it before the system call was 42811911SBrandon.Potter@amd.com * invoked). In this case, we need to throw an error. 42911911SBrandon.Potter@amd.com */ 43011911SBrandon.Potter@amd.com if (val != mem_val) 4319112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 4329112Smarc.orr@gmail.com 43313642Sqtt2@cornell.edu if (OS::TGT_FUTEX_WAIT) { 43413642Sqtt2@cornell.edu futex_map.suspend(uaddr, process->tgid(), tc); 43513642Sqtt2@cornell.edu } else { 43613642Sqtt2@cornell.edu futex_map.suspend_bitset(uaddr, process->tgid(), tc, val3); 43713642Sqtt2@cornell.edu } 43811911SBrandon.Potter@amd.com 4399112Smarc.orr@gmail.com return 0; 44011911SBrandon.Potter@amd.com } else if (OS::TGT_FUTEX_WAKE == op) { 44111911SBrandon.Potter@amd.com return futex_map.wakeup(uaddr, process->tgid(), val); 44213642Sqtt2@cornell.edu } else if (OS::TGT_FUTEX_WAKE_BITSET == op) { 44313642Sqtt2@cornell.edu return futex_map.wakeup_bitset(uaddr, process->tgid(), val3); 44413650Smw828@cornell.edu } else if (OS::TGT_FUTEX_REQUEUE == op || 44513650Smw828@cornell.edu OS::TGT_FUTEX_CMP_REQUEUE == op) { 44613650Smw828@cornell.edu 44713650Smw828@cornell.edu // Ensure futex system call accessed atomically. 44813650Smw828@cornell.edu BufferArg buf(uaddr, sizeof(int)); 44913650Smw828@cornell.edu buf.copyIn(tc->getMemProxy()); 45013650Smw828@cornell.edu int mem_val = *(int*)buf.bufferPtr(); 45113650Smw828@cornell.edu /* 45213650Smw828@cornell.edu * For CMP_REQUEUE, the whole operation is only started only if 45313650Smw828@cornell.edu * val3 is still the value of the futex pointed to by uaddr. 45413650Smw828@cornell.edu */ 45513650Smw828@cornell.edu if (OS::TGT_FUTEX_CMP_REQUEUE && val3 != mem_val) 45613650Smw828@cornell.edu return -OS::TGT_EWOULDBLOCK; 45713650Smw828@cornell.edu return futex_map.requeue(uaddr, process->tgid(), val, timeout, uaddr2); 4589112Smarc.orr@gmail.com } 45911911SBrandon.Potter@amd.com warn("futex: op %d not implemented; ignoring.", op); 46011911SBrandon.Potter@amd.com return -ENOSYS; 4619112Smarc.orr@gmail.com} 4629112Smarc.orr@gmail.com 4632238SN/A 4642238SN/A/// Pseudo Funcs - These functions use a different return convension, 4652238SN/A/// returning a second value in a register other than the normal return register 4662238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 46711851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 4682238SN/A 4692238SN/A/// Target getpidPseudo() handler. 4702238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 47111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 4722238SN/A 4732238SN/A/// Target getuidPseudo() handler. 4742238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 47511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 4762238SN/A 4772238SN/A/// Target getgidPseudo() handler. 4782238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 47911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 4802238SN/A 4812238SN/A 4821354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 4831354SN/Aconst int one_million = 1000000; 48410796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 48510796Sbrandon.potter@amd.comconst int one_billion = 1000000000; 4861354SN/A 4871354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 4881354SN/A/// by my reckoning. We want to keep this a constant (not use the 4891354SN/A/// real-world time) to keep simulations repeatable. 4901354SN/Aconst unsigned seconds_since_epoch = 1000000000; 4911354SN/A 4921354SN/A/// Helper function to convert current elapsed time to seconds and 4931354SN/A/// microseconds. 4941354SN/Atemplate <class T1, class T2> 4951354SN/Avoid 49610796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec) 4971354SN/A{ 49810796Sbrandon.potter@amd.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 4991354SN/A sec = elapsed_usecs / one_million; 5001354SN/A usec = elapsed_usecs % one_million; 5011354SN/A} 5021354SN/A 50310796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and 50410796Sbrandon.potter@amd.com/// nanoseconds. 50510796Sbrandon.potter@amd.comtemplate <class T1, class T2> 50610796Sbrandon.potter@amd.comvoid 50710796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec) 50810796Sbrandon.potter@amd.com{ 50910796Sbrandon.potter@amd.com uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 51010796Sbrandon.potter@amd.com sec = elapsed_nsecs / one_billion; 51110796Sbrandon.potter@amd.com nsec = elapsed_nsecs % one_billion; 51210796Sbrandon.potter@amd.com} 51310796Sbrandon.potter@amd.com 514360SN/A////////////////////////////////////////////////////////////////////// 515360SN/A// 516360SN/A// The following emulation functions are generic, but need to be 517360SN/A// templated to account for differences in types, constants, etc. 518360SN/A// 519360SN/A////////////////////////////////////////////////////////////////////// 520360SN/A 52111759Sbrandon.potter@amd.com typedef struct statfs hst_statfs; 5223113Sgblack@eecs.umich.edu#if NO_STAT64 5233113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 5243113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 5253113Sgblack@eecs.umich.edu#else 5263113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 5273113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 5283113Sgblack@eecs.umich.edu#endif 5293113Sgblack@eecs.umich.edu 5303113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 5313113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 5323113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 5333113Sgblack@eecs.umich.edu 5343113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 53512032Sandreas.sandberg@arm.comvoid 5363113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 5373113Sgblack@eecs.umich.edu{ 5384189Sgblack@eecs.umich.edu using namespace TheISA; 5394189Sgblack@eecs.umich.edu 5403113Sgblack@eecs.umich.edu if (fakeTTY) 5413113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 5423113Sgblack@eecs.umich.edu else 5433113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 5448737Skoansin.tan@gmail.com tgt->st_dev = TheISA::htog(tgt->st_dev); 5453113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 5468737Skoansin.tan@gmail.com tgt->st_ino = TheISA::htog(tgt->st_ino); 5473277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 5485515SMichael.Adler@intel.com if (fakeTTY) { 5495515SMichael.Adler@intel.com // Claim to be a character device 5505515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 5515515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 5525515SMichael.Adler@intel.com } 5538737Skoansin.tan@gmail.com tgt->st_mode = TheISA::htog(tgt->st_mode); 5543277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 5558737Skoansin.tan@gmail.com tgt->st_nlink = TheISA::htog(tgt->st_nlink); 5563277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 5578737Skoansin.tan@gmail.com tgt->st_uid = TheISA::htog(tgt->st_uid); 5583277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 5598737Skoansin.tan@gmail.com tgt->st_gid = TheISA::htog(tgt->st_gid); 5603113Sgblack@eecs.umich.edu if (fakeTTY) 5613113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 5623113Sgblack@eecs.umich.edu else 5633113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 5648737Skoansin.tan@gmail.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 5653113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 5668737Skoansin.tan@gmail.com tgt->st_size = TheISA::htog(tgt->st_size); 5673114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 5688737Skoansin.tan@gmail.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 5693114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 5708737Skoansin.tan@gmail.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 5713114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 5728737Skoansin.tan@gmail.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 57311906SBrandon.Potter@amd.com // Force the block size to be 8KB. This helps to ensure buffered io works 5744061Sgblack@eecs.umich.edu // consistently across different hosts. 5754061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 5768737Skoansin.tan@gmail.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 5773113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 5788737Skoansin.tan@gmail.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 5793113Sgblack@eecs.umich.edu} 5803113Sgblack@eecs.umich.edu 5813113Sgblack@eecs.umich.edu// Same for stat64 5823113Sgblack@eecs.umich.edu 5833113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 58412032Sandreas.sandberg@arm.comvoid 5853113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 5863113Sgblack@eecs.umich.edu{ 5874189Sgblack@eecs.umich.edu using namespace TheISA; 5884189Sgblack@eecs.umich.edu 5893113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 5903113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 5913113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 5928737Skoansin.tan@gmail.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 5933113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 5948737Skoansin.tan@gmail.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 5953113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 5968737Skoansin.tan@gmail.com tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 5973113Sgblack@eecs.umich.edu#else 5983113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 5993113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 6003113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 6013113Sgblack@eecs.umich.edu#endif 6023113Sgblack@eecs.umich.edu} 6033113Sgblack@eecs.umich.edu 60411906SBrandon.Potter@amd.com// Here are a couple of convenience functions 6053113Sgblack@eecs.umich.edutemplate<class OS> 60612032Sandreas.sandberg@arm.comvoid 6078852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 60811906SBrandon.Potter@amd.com hst_stat *host, bool fakeTTY = false) 6093113Sgblack@eecs.umich.edu{ 6103113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 6113113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 6123113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 6133113Sgblack@eecs.umich.edu tgt.copyOut(mem); 6143113Sgblack@eecs.umich.edu} 6153113Sgblack@eecs.umich.edu 6163113Sgblack@eecs.umich.edutemplate<class OS> 61712032Sandreas.sandberg@arm.comvoid 6188852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 61911906SBrandon.Potter@amd.com hst_stat64 *host, bool fakeTTY = false) 6203113Sgblack@eecs.umich.edu{ 6213113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 6223113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 6236686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 6243113Sgblack@eecs.umich.edu tgt.copyOut(mem); 6253113Sgblack@eecs.umich.edu} 6263113Sgblack@eecs.umich.edu 62711759Sbrandon.potter@amd.comtemplate <class OS> 62812032Sandreas.sandberg@arm.comvoid 62911759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 63011759Sbrandon.potter@amd.com hst_statfs *host) 63111759Sbrandon.potter@amd.com{ 63211759Sbrandon.potter@amd.com TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 63311759Sbrandon.potter@amd.com 63411812Sbaz21@cam.ac.uk tgt->f_type = TheISA::htog(host->f_type); 63511812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 63611812Sbaz21@cam.ac.uk tgt->f_bsize = TheISA::htog(host->f_iosize); 63711759Sbrandon.potter@amd.com#else 63811812Sbaz21@cam.ac.uk tgt->f_bsize = TheISA::htog(host->f_bsize); 63911759Sbrandon.potter@amd.com#endif 64011759Sbrandon.potter@amd.com tgt->f_blocks = TheISA::htog(host->f_blocks); 64111759Sbrandon.potter@amd.com tgt->f_bfree = TheISA::htog(host->f_bfree); 64211759Sbrandon.potter@amd.com tgt->f_bavail = TheISA::htog(host->f_bavail); 64311759Sbrandon.potter@amd.com tgt->f_files = TheISA::htog(host->f_files); 64411759Sbrandon.potter@amd.com tgt->f_ffree = TheISA::htog(host->f_ffree); 64511759Sbrandon.potter@amd.com memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 64611812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 64711812Sbaz21@cam.ac.uk tgt->f_namelen = TheISA::htog(host->f_namemax); 64811812Sbaz21@cam.ac.uk tgt->f_frsize = TheISA::htog(host->f_bsize); 64911812Sbaz21@cam.ac.uk#elif defined(__APPLE__) 65011812Sbaz21@cam.ac.uk tgt->f_namelen = 0; 65111812Sbaz21@cam.ac.uk tgt->f_frsize = 0; 65211812Sbaz21@cam.ac.uk#else 65311759Sbrandon.potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 65411759Sbrandon.potter@amd.com tgt->f_frsize = TheISA::htog(host->f_frsize); 65511812Sbaz21@cam.ac.uk#endif 65611812Sbaz21@cam.ac.uk#if defined(__linux__) 65711759Sbrandon.potter@amd.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 65811812Sbaz21@cam.ac.uk#else 65911812Sbaz21@cam.ac.uk /* 66011812Sbaz21@cam.ac.uk * The fields are different sizes per OS. Don't bother with 66111812Sbaz21@cam.ac.uk * f_spare or f_reserved on non-Linux for now. 66211812Sbaz21@cam.ac.uk */ 66311812Sbaz21@cam.ac.uk memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 66411812Sbaz21@cam.ac.uk#endif 66511759Sbrandon.potter@amd.com 66611759Sbrandon.potter@amd.com tgt.copyOut(mem); 66711759Sbrandon.potter@amd.com} 66811759Sbrandon.potter@amd.com 669378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 670378SN/A/// only to find out if their stdout is a tty, to determine whether to 6719141Smarc.orr@gmail.com/// do line or block buffering. We always claim that output fds are 6729141Smarc.orr@gmail.com/// not TTYs to provide repeatable results. 673360SN/Atemplate <class OS> 6741450SN/ASyscallReturn 67511856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 676360SN/A{ 6776701Sgblack@eecs.umich.edu int index = 0; 67811856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 67911856Sbrandon.potter@amd.com unsigned req = p->getSyscallArg(tc, index); 680360SN/A 68110930Sbrandon.potter@amd.com DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 682360SN/A 68311856Sbrandon.potter@amd.com if (OS::isTtyReq(req)) 68411856Sbrandon.potter@amd.com return -ENOTTY; 68510496Ssteve.reinhardt@amd.com 68611856Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 68711856Sbrandon.potter@amd.com if (!dfdp) 6881458SN/A return -EBADF; 689360SN/A 69011856Sbrandon.potter@amd.com /** 69111856Sbrandon.potter@amd.com * If the driver is valid, issue the ioctl through it. Otherwise, 69211856Sbrandon.potter@amd.com * there's an implicit assumption that the device is a TTY type and we 69311856Sbrandon.potter@amd.com * return that we do not have a valid TTY. 69411856Sbrandon.potter@amd.com */ 69511856Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 69611856Sbrandon.potter@amd.com if (emul_driver) 69711856Sbrandon.potter@amd.com return emul_driver->ioctl(p, tc, req); 69810496Ssteve.reinhardt@amd.com 69911856Sbrandon.potter@amd.com /** 70011856Sbrandon.potter@amd.com * For lack of a better return code, return ENOTTY. Ideally, we should 70111856Sbrandon.potter@amd.com * return something better here, but at least we issue the warning. 70211856Sbrandon.potter@amd.com */ 70311856Sbrandon.potter@amd.com warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 70410930Sbrandon.potter@amd.com tgt_fd, req, tc->pcState()); 7059141Smarc.orr@gmail.com return -ENOTTY; 706360SN/A} 707360SN/A 708360SN/Atemplate <class OS> 70911907SBrandon.Potter@amd.comSyscallReturn 71011907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 71111907SBrandon.Potter@amd.com bool isopenat) 712360SN/A{ 71311907SBrandon.Potter@amd.com int index = 0; 71411907SBrandon.Potter@amd.com int tgt_dirfd = -1; 71511907SBrandon.Potter@amd.com 71611907SBrandon.Potter@amd.com /** 71711907SBrandon.Potter@amd.com * If using the openat variant, read in the target directory file 71811907SBrandon.Potter@amd.com * descriptor from the simulated process. 71911907SBrandon.Potter@amd.com */ 72011907SBrandon.Potter@amd.com if (isopenat) 72111907SBrandon.Potter@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 72211907SBrandon.Potter@amd.com 72311907SBrandon.Potter@amd.com /** 72411907SBrandon.Potter@amd.com * Retrieve the simulated process' memory proxy and then read in the path 72511907SBrandon.Potter@amd.com * string from that memory space into the host's working memory space. 72611907SBrandon.Potter@amd.com */ 727360SN/A std::string path; 72811907SBrandon.Potter@amd.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 7291458SN/A return -EFAULT; 730360SN/A 73111907SBrandon.Potter@amd.com#ifdef __CYGWIN32__ 73211907SBrandon.Potter@amd.com int host_flags = O_BINARY; 73311907SBrandon.Potter@amd.com#else 73411907SBrandon.Potter@amd.com int host_flags = 0; 73511907SBrandon.Potter@amd.com#endif 73611907SBrandon.Potter@amd.com /** 73711907SBrandon.Potter@amd.com * Translate target flags into host flags. Flags exist which are not 73811907SBrandon.Potter@amd.com * ported between architectures which can cause check failures. 73911907SBrandon.Potter@amd.com */ 74011907SBrandon.Potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 741360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 74211907SBrandon.Potter@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 74311907SBrandon.Potter@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 74411907SBrandon.Potter@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 745360SN/A } 746360SN/A } 74711907SBrandon.Potter@amd.com if (tgt_flags) { 74811907SBrandon.Potter@amd.com warn("open%s: cannot decode flags 0x%x", 74911907SBrandon.Potter@amd.com isopenat ? "at" : "", tgt_flags); 75011907SBrandon.Potter@amd.com } 751360SN/A#ifdef __CYGWIN32__ 75211907SBrandon.Potter@amd.com host_flags |= O_BINARY; 753360SN/A#endif 754360SN/A 75511907SBrandon.Potter@amd.com int mode = p->getSyscallArg(tc, index); 7563669Sbinkertn@umich.edu 75711907SBrandon.Potter@amd.com /** 75811907SBrandon.Potter@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 75911907SBrandon.Potter@amd.com * take the current working directory value which was passed into the 76011907SBrandon.Potter@amd.com * process class as a Python parameter and append the current path to 76111907SBrandon.Potter@amd.com * create a full path. 76211907SBrandon.Potter@amd.com * Otherwise, openat with a valid target directory file descriptor has 76311907SBrandon.Potter@amd.com * been called. If the path option, which was passed in as a parameter, 76411907SBrandon.Potter@amd.com * is not absolute, retrieve the directory file descriptor's path and 76511907SBrandon.Potter@amd.com * prepend it to the path passed in as a parameter. 76611907SBrandon.Potter@amd.com * In every case, we should have a full path (which is relevant to the 76711907SBrandon.Potter@amd.com * host) to work with after this block has been passed. 76811907SBrandon.Potter@amd.com */ 76911907SBrandon.Potter@amd.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 77011907SBrandon.Potter@amd.com path = p->fullPath(path); 77111907SBrandon.Potter@amd.com } else if (!startswith(path, "/")) { 77211907SBrandon.Potter@amd.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 77311907SBrandon.Potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 77411907SBrandon.Potter@amd.com if (!ffdp) 77511907SBrandon.Potter@amd.com return -EBADF; 77613371Sciro.santilli@arm.com path.insert(0, ffdp->getFileName() + "/"); 77711907SBrandon.Potter@amd.com } 7781706SN/A 77911907SBrandon.Potter@amd.com /** 78011907SBrandon.Potter@amd.com * Since this is an emulated environment, we create pseudo file 78111907SBrandon.Potter@amd.com * descriptors for device requests that have been registered with 78211907SBrandon.Potter@amd.com * the process class through Python; this allows us to create a file 78311907SBrandon.Potter@amd.com * descriptor for subsequent ioctl or mmap calls. 78411907SBrandon.Potter@amd.com */ 78510496Ssteve.reinhardt@amd.com if (startswith(path, "/dev/")) { 78610496Ssteve.reinhardt@amd.com std::string filename = path.substr(strlen("/dev/")); 78711907SBrandon.Potter@amd.com EmulatedDriver *drv = p->findDriver(filename); 78811907SBrandon.Potter@amd.com if (drv) { 78911907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 79011907SBrandon.Potter@amd.com "driver open with path[%s]\n", 79111907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 79211907SBrandon.Potter@amd.com return drv->open(p, tc, mode, host_flags); 79310496Ssteve.reinhardt@amd.com } 79411907SBrandon.Potter@amd.com /** 79511907SBrandon.Potter@amd.com * Fall through here for pass through to host devices, such 79611907SBrandon.Potter@amd.com * as /dev/zero 79711907SBrandon.Potter@amd.com */ 79810496Ssteve.reinhardt@amd.com } 79910496Ssteve.reinhardt@amd.com 80011907SBrandon.Potter@amd.com /** 80111907SBrandon.Potter@amd.com * Some special paths and files cannot be called on the host and need 80211907SBrandon.Potter@amd.com * to be handled as special cases inside the simulator. 80311907SBrandon.Potter@amd.com * If the full path that was created above does not match any of the 80411907SBrandon.Potter@amd.com * special cases, pass it through to the open call on the host to let 80511907SBrandon.Potter@amd.com * the host open the file on our behalf. 80611907SBrandon.Potter@amd.com * If the host cannot open the file, return the host's error code back 80711907SBrandon.Potter@amd.com * through the system call to the simulated process. 80811907SBrandon.Potter@amd.com */ 80911907SBrandon.Potter@amd.com int sim_fd = -1; 81011907SBrandon.Potter@amd.com std::vector<std::string> special_paths = 81111907SBrandon.Potter@amd.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 81211907SBrandon.Potter@amd.com for (auto entry : special_paths) { 81311907SBrandon.Potter@amd.com if (startswith(path, entry)) 81411907SBrandon.Potter@amd.com sim_fd = OS::openSpecialFile(path, p, tc); 81511907SBrandon.Potter@amd.com } 81611907SBrandon.Potter@amd.com if (sim_fd == -1) { 81711907SBrandon.Potter@amd.com sim_fd = open(path.c_str(), host_flags, mode); 81811907SBrandon.Potter@amd.com } 81911907SBrandon.Potter@amd.com if (sim_fd == -1) { 82011907SBrandon.Potter@amd.com int local = -errno; 82111907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 82211907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 82311907SBrandon.Potter@amd.com return local; 82411907SBrandon.Potter@amd.com } 825360SN/A 82611907SBrandon.Potter@amd.com /** 82711907SBrandon.Potter@amd.com * The file was opened successfully and needs to be recorded in the 82811907SBrandon.Potter@amd.com * process' file descriptor array so that it can be retrieved later. 82911907SBrandon.Potter@amd.com * The target file descriptor that is chosen will be the lowest unused 83011907SBrandon.Potter@amd.com * file descriptor. 83111907SBrandon.Potter@amd.com * Return the indirect target file descriptor back to the simulated 83211907SBrandon.Potter@amd.com * process to act as a handle for the opened file. 83311907SBrandon.Potter@amd.com */ 83411907SBrandon.Potter@amd.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 83511907SBrandon.Potter@amd.com int tgt_fd = p->fds->allocFD(ffdp); 83611907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 83711907SBrandon.Potter@amd.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 83811907SBrandon.Potter@amd.com return tgt_fd; 839360SN/A} 840360SN/A 84110027SChris.Adeniyi-Jones@arm.com/// Target open() handler. 84210027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 84310027SChris.Adeniyi-Jones@arm.comSyscallReturn 84411851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 84510027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 84610027SChris.Adeniyi-Jones@arm.com{ 84711907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, false); 84810027SChris.Adeniyi-Jones@arm.com} 84910027SChris.Adeniyi-Jones@arm.com 85010027SChris.Adeniyi-Jones@arm.com/// Target openat() handler. 85110027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 85210027SChris.Adeniyi-Jones@arm.comSyscallReturn 85311851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process, 85411851Sbrandon.potter@amd.com ThreadContext *tc) 85510027SChris.Adeniyi-Jones@arm.com{ 85611907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, true); 85710027SChris.Adeniyi-Jones@arm.com} 85810027SChris.Adeniyi-Jones@arm.com 85910633Smichaelupton@gmail.com/// Target unlinkat() handler. 86010633Smichaelupton@gmail.comtemplate <class OS> 86110633Smichaelupton@gmail.comSyscallReturn 86211851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 86310633Smichaelupton@gmail.com ThreadContext *tc) 86410633Smichaelupton@gmail.com{ 86510633Smichaelupton@gmail.com int index = 0; 86610633Smichaelupton@gmail.com int dirfd = process->getSyscallArg(tc, index); 86710633Smichaelupton@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 86810633Smichaelupton@gmail.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 86910633Smichaelupton@gmail.com 87010633Smichaelupton@gmail.com return unlinkHelper(desc, callnum, process, tc, 1); 87110633Smichaelupton@gmail.com} 87210633Smichaelupton@gmail.com 87310203SAli.Saidi@ARM.com/// Target facessat() handler 87410203SAli.Saidi@ARM.comtemplate <class OS> 87510203SAli.Saidi@ARM.comSyscallReturn 87611851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 87711851Sbrandon.potter@amd.com ThreadContext *tc) 87810203SAli.Saidi@ARM.com{ 87910203SAli.Saidi@ARM.com int index = 0; 88010203SAli.Saidi@ARM.com int dirfd = process->getSyscallArg(tc, index); 88110203SAli.Saidi@ARM.com if (dirfd != OS::TGT_AT_FDCWD) 88210203SAli.Saidi@ARM.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 88310203SAli.Saidi@ARM.com return accessFunc(desc, callnum, process, tc, 1); 88410203SAli.Saidi@ARM.com} 88510203SAli.Saidi@ARM.com 88610203SAli.Saidi@ARM.com/// Target readlinkat() handler 88710203SAli.Saidi@ARM.comtemplate <class OS> 88810203SAli.Saidi@ARM.comSyscallReturn 88911851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 89011851Sbrandon.potter@amd.com ThreadContext *tc) 89110203SAli.Saidi@ARM.com{ 89210203SAli.Saidi@ARM.com int index = 0; 89310203SAli.Saidi@ARM.com int dirfd = process->getSyscallArg(tc, index); 89410203SAli.Saidi@ARM.com if (dirfd != OS::TGT_AT_FDCWD) 89510203SAli.Saidi@ARM.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 89610203SAli.Saidi@ARM.com return readlinkFunc(desc, callnum, process, tc, 1); 89710203SAli.Saidi@ARM.com} 89810203SAli.Saidi@ARM.com 89910850SGiacomo.Gabrielli@arm.com/// Target renameat() handler. 90010850SGiacomo.Gabrielli@arm.comtemplate <class OS> 90110850SGiacomo.Gabrielli@arm.comSyscallReturn 90211851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process, 90310850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 90410850SGiacomo.Gabrielli@arm.com{ 90510850SGiacomo.Gabrielli@arm.com int index = 0; 90610850SGiacomo.Gabrielli@arm.com 90710850SGiacomo.Gabrielli@arm.com int olddirfd = process->getSyscallArg(tc, index); 90810850SGiacomo.Gabrielli@arm.com if (olddirfd != OS::TGT_AT_FDCWD) 90910850SGiacomo.Gabrielli@arm.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 91010850SGiacomo.Gabrielli@arm.com 91110850SGiacomo.Gabrielli@arm.com std::string old_name; 91210850SGiacomo.Gabrielli@arm.com 91310850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(old_name, 91410850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 91510850SGiacomo.Gabrielli@arm.com return -EFAULT; 91610850SGiacomo.Gabrielli@arm.com 91710850SGiacomo.Gabrielli@arm.com int newdirfd = process->getSyscallArg(tc, index); 91810850SGiacomo.Gabrielli@arm.com if (newdirfd != OS::TGT_AT_FDCWD) 91910850SGiacomo.Gabrielli@arm.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 92010850SGiacomo.Gabrielli@arm.com 92110850SGiacomo.Gabrielli@arm.com std::string new_name; 92210850SGiacomo.Gabrielli@arm.com 92310850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(new_name, 92410850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 92510850SGiacomo.Gabrielli@arm.com return -EFAULT; 92610850SGiacomo.Gabrielli@arm.com 92710850SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 92810850SGiacomo.Gabrielli@arm.com old_name = process->fullPath(old_name); 92910850SGiacomo.Gabrielli@arm.com new_name = process->fullPath(new_name); 93010850SGiacomo.Gabrielli@arm.com 93110850SGiacomo.Gabrielli@arm.com int result = rename(old_name.c_str(), new_name.c_str()); 93210850SGiacomo.Gabrielli@arm.com return (result == -1) ? -errno : result; 93310850SGiacomo.Gabrielli@arm.com} 93410850SGiacomo.Gabrielli@arm.com 9356640Svince@csl.cornell.edu/// Target sysinfo() handler. 9366640Svince@csl.cornell.edutemplate <class OS> 9376640Svince@csl.cornell.eduSyscallReturn 93811851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 93911851Sbrandon.potter@amd.com ThreadContext *tc) 9406640Svince@csl.cornell.edu{ 9416640Svince@csl.cornell.edu 9426701Sgblack@eecs.umich.edu int index = 0; 9436701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 94410793Sbrandon.potter@amd.com sysinfo(process->getSyscallArg(tc, index)); 9456640Svince@csl.cornell.edu 94611758Sbrandon.potter@amd.com sysinfo->uptime = seconds_since_epoch; 94711758Sbrandon.potter@amd.com sysinfo->totalram = process->system->memSize(); 94811758Sbrandon.potter@amd.com sysinfo->mem_unit = 1; 9496640Svince@csl.cornell.edu 9508706Sandreas.hansson@arm.com sysinfo.copyOut(tc->getMemProxy()); 9516640Svince@csl.cornell.edu 9526701Sgblack@eecs.umich.edu return 0; 9536640Svince@csl.cornell.edu} 954360SN/A 9551999SN/A/// Target chmod() handler. 9561999SN/Atemplate <class OS> 9571999SN/ASyscallReturn 95811851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process, 9592680Sktlim@umich.edu ThreadContext *tc) 9601999SN/A{ 9611999SN/A std::string path; 9621999SN/A 9636701Sgblack@eecs.umich.edu int index = 0; 9648852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 9656701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9661999SN/A return -EFAULT; 9676701Sgblack@eecs.umich.edu } 9681999SN/A 9696701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 9701999SN/A mode_t hostMode = 0; 9711999SN/A 9721999SN/A // XXX translate mode flags via OS::something??? 9731999SN/A hostMode = mode; 9741999SN/A 9753669Sbinkertn@umich.edu // Adjust path for current working directory 9763669Sbinkertn@umich.edu path = process->fullPath(path); 9773669Sbinkertn@umich.edu 9781999SN/A // do the chmod 9791999SN/A int result = chmod(path.c_str(), hostMode); 9801999SN/A if (result < 0) 9812218SN/A return -errno; 9821999SN/A 9831999SN/A return 0; 9841999SN/A} 9851999SN/A 98613570Sbrandon.potter@amd.comtemplate <class OS> 98713570Sbrandon.potter@amd.comSyscallReturn 98813570Sbrandon.potter@amd.compollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 98913570Sbrandon.potter@amd.com{ 99013570Sbrandon.potter@amd.com int index = 0; 99113570Sbrandon.potter@amd.com Addr fdsPtr = p->getSyscallArg(tc, index); 99213570Sbrandon.potter@amd.com int nfds = p->getSyscallArg(tc, index); 99313570Sbrandon.potter@amd.com int tmout = p->getSyscallArg(tc, index); 99413570Sbrandon.potter@amd.com 99513570Sbrandon.potter@amd.com BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 99613570Sbrandon.potter@amd.com fdsBuf.copyIn(tc->getMemProxy()); 99713570Sbrandon.potter@amd.com 99813570Sbrandon.potter@amd.com /** 99913570Sbrandon.potter@amd.com * Record the target file descriptors in a local variable. We need to 100013570Sbrandon.potter@amd.com * replace them with host file descriptors but we need a temporary copy 100113570Sbrandon.potter@amd.com * for later. Afterwards, replace each target file descriptor in the 100213570Sbrandon.potter@amd.com * poll_fd array with its host_fd. 100313570Sbrandon.potter@amd.com */ 100413570Sbrandon.potter@amd.com int temp_tgt_fds[nfds]; 100513570Sbrandon.potter@amd.com for (index = 0; index < nfds; index++) { 100613570Sbrandon.potter@amd.com temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 100713570Sbrandon.potter@amd.com auto tgt_fd = temp_tgt_fds[index]; 100813570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 100913570Sbrandon.potter@amd.com if (!hbfdp) 101013570Sbrandon.potter@amd.com return -EBADF; 101113570Sbrandon.potter@amd.com auto host_fd = hbfdp->getSimFD(); 101213570Sbrandon.potter@amd.com ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 101313570Sbrandon.potter@amd.com } 101413570Sbrandon.potter@amd.com 101513570Sbrandon.potter@amd.com /** 101613570Sbrandon.potter@amd.com * We cannot allow an infinite poll to occur or it will inevitably cause 101713570Sbrandon.potter@amd.com * a deadlock in the gem5 simulator with clone. We must pass in tmout with 101813570Sbrandon.potter@amd.com * a non-negative value, however it also makes no sense to poll on the 101913570Sbrandon.potter@amd.com * underlying host for any other time than tmout a zero timeout. 102013570Sbrandon.potter@amd.com */ 102113570Sbrandon.potter@amd.com int status; 102213570Sbrandon.potter@amd.com if (tmout < 0) { 102313570Sbrandon.potter@amd.com status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 102413570Sbrandon.potter@amd.com if (status == 0) { 102513570Sbrandon.potter@amd.com /** 102613570Sbrandon.potter@amd.com * If blocking indefinitely, check the signal list to see if a 102713570Sbrandon.potter@amd.com * signal would break the poll out of the retry cycle and try 102813570Sbrandon.potter@amd.com * to return the signal interrupt instead. 102913570Sbrandon.potter@amd.com */ 103013570Sbrandon.potter@amd.com System *sysh = tc->getSystemPtr(); 103113570Sbrandon.potter@amd.com std::list<BasicSignal>::iterator it; 103213570Sbrandon.potter@amd.com for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 103313570Sbrandon.potter@amd.com if (it->receiver == p) 103413570Sbrandon.potter@amd.com return -EINTR; 103513570Sbrandon.potter@amd.com return SyscallReturn::retry(); 103613570Sbrandon.potter@amd.com } 103713570Sbrandon.potter@amd.com } else 103813570Sbrandon.potter@amd.com status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 103913570Sbrandon.potter@amd.com 104013570Sbrandon.potter@amd.com if (status == -1) 104113570Sbrandon.potter@amd.com return -errno; 104213570Sbrandon.potter@amd.com 104313570Sbrandon.potter@amd.com /** 104413570Sbrandon.potter@amd.com * Replace each host_fd in the returned poll_fd array with its original 104513570Sbrandon.potter@amd.com * target file descriptor. 104613570Sbrandon.potter@amd.com */ 104713570Sbrandon.potter@amd.com for (index = 0; index < nfds; index++) { 104813570Sbrandon.potter@amd.com auto tgt_fd = temp_tgt_fds[index]; 104913570Sbrandon.potter@amd.com ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 105013570Sbrandon.potter@amd.com } 105113570Sbrandon.potter@amd.com 105213570Sbrandon.potter@amd.com /** 105313570Sbrandon.potter@amd.com * Copy out the pollfd struct because the host may have updated fields 105413570Sbrandon.potter@amd.com * in the structure. 105513570Sbrandon.potter@amd.com */ 105613570Sbrandon.potter@amd.com fdsBuf.copyOut(tc->getMemProxy()); 105713570Sbrandon.potter@amd.com 105813570Sbrandon.potter@amd.com return status; 105913570Sbrandon.potter@amd.com} 10601999SN/A 10611999SN/A/// Target fchmod() handler. 10621999SN/Atemplate <class OS> 10631999SN/ASyscallReturn 106411856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 10651999SN/A{ 10666701Sgblack@eecs.umich.edu int index = 0; 106711856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 106811856Sbrandon.potter@amd.com uint32_t mode = p->getSyscallArg(tc, index); 106910931Sbrandon.potter@amd.com 107011856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 107111856Sbrandon.potter@amd.com if (!ffdp) 10721999SN/A return -EBADF; 107311856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 10741999SN/A 107511856Sbrandon.potter@amd.com mode_t hostMode = mode; 10761999SN/A 107711856Sbrandon.potter@amd.com int result = fchmod(sim_fd, hostMode); 10781999SN/A 107911856Sbrandon.potter@amd.com return (result < 0) ? -errno : 0; 10801999SN/A} 10811999SN/A 10825877Shsul@eecs.umich.edu/// Target mremap() handler. 10835877Shsul@eecs.umich.edutemplate <class OS> 10845877Shsul@eecs.umich.eduSyscallReturn 108511851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 10865877Shsul@eecs.umich.edu{ 10876701Sgblack@eecs.umich.edu int index = 0; 10886701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 10896701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 10906701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 10916701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 109210027SChris.Adeniyi-Jones@arm.com uint64_t provided_address = 0; 109310027SChris.Adeniyi-Jones@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 109410027SChris.Adeniyi-Jones@arm.com 109510027SChris.Adeniyi-Jones@arm.com if (use_provided_address) 109610027SChris.Adeniyi-Jones@arm.com provided_address = process->getSyscallArg(tc, index); 10975877Shsul@eecs.umich.edu 109810318Sandreas.hansson@arm.com if ((start % TheISA::PageBytes != 0) || 109910318Sandreas.hansson@arm.com (provided_address % TheISA::PageBytes != 0)) { 11005877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 11015877Shsul@eecs.umich.edu return -EINVAL; 11025877Shsul@eecs.umich.edu } 11035877Shsul@eecs.umich.edu 110410486Stjablin@gmail.com new_length = roundUp(new_length, TheISA::PageBytes); 110510486Stjablin@gmail.com 11065877Shsul@eecs.umich.edu if (new_length > old_length) { 110711905SBrandon.Potter@amd.com std::shared_ptr<MemState> mem_state = process->memState; 110811905SBrandon.Potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 110911905SBrandon.Potter@amd.com 111011905SBrandon.Potter@amd.com if ((start + old_length) == mmap_end && 111110027SChris.Adeniyi-Jones@arm.com (!use_provided_address || provided_address == start)) { 111212206Srico.amslinger@informatik.uni-augsburg.de // This case cannot occur when growing downward, as 111312206Srico.amslinger@informatik.uni-augsburg.de // start is greater than or equal to mmap_end. 11145877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 111511905SBrandon.Potter@amd.com process->allocateMem(mmap_end, diff); 111611905SBrandon.Potter@amd.com mem_state->setMmapEnd(mmap_end + diff); 11175877Shsul@eecs.umich.edu return start; 11185877Shsul@eecs.umich.edu } else { 111910027SChris.Adeniyi-Jones@arm.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 11205877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 11215877Shsul@eecs.umich.edu return -ENOMEM; 11225877Shsul@eecs.umich.edu } else { 112312206Srico.amslinger@informatik.uni-augsburg.de uint64_t new_start = provided_address; 112412206Srico.amslinger@informatik.uni-augsburg.de if (!use_provided_address) { 112512206Srico.amslinger@informatik.uni-augsburg.de new_start = process->mmapGrowsDown() ? 112612206Srico.amslinger@informatik.uni-augsburg.de mmap_end - new_length : mmap_end; 112712206Srico.amslinger@informatik.uni-augsburg.de mmap_end = process->mmapGrowsDown() ? 112812206Srico.amslinger@informatik.uni-augsburg.de new_start : mmap_end + new_length; 112912206Srico.amslinger@informatik.uni-augsburg.de mem_state->setMmapEnd(mmap_end); 113012206Srico.amslinger@informatik.uni-augsburg.de } 113112206Srico.amslinger@informatik.uni-augsburg.de 113210027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, old_length, new_start); 113310027SChris.Adeniyi-Jones@arm.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 113410027SChris.Adeniyi-Jones@arm.com new_start, new_start + new_length, 113510027SChris.Adeniyi-Jones@arm.com new_length - old_length); 11365877Shsul@eecs.umich.edu // add on the remaining unallocated pages 113710027SChris.Adeniyi-Jones@arm.com process->allocateMem(new_start + old_length, 113810027SChris.Adeniyi-Jones@arm.com new_length - old_length, 113910027SChris.Adeniyi-Jones@arm.com use_provided_address /* clobber */); 114010027SChris.Adeniyi-Jones@arm.com if (use_provided_address && 114112206Srico.amslinger@informatik.uni-augsburg.de ((new_start + new_length > mem_state->getMmapEnd() && 114212206Srico.amslinger@informatik.uni-augsburg.de !process->mmapGrowsDown()) || 114312206Srico.amslinger@informatik.uni-augsburg.de (new_start < mem_state->getMmapEnd() && 114412206Srico.amslinger@informatik.uni-augsburg.de process->mmapGrowsDown()))) { 114510027SChris.Adeniyi-Jones@arm.com // something fishy going on here, at least notify the user 114610027SChris.Adeniyi-Jones@arm.com // @todo: increase mmap_end? 114710027SChris.Adeniyi-Jones@arm.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 114810027SChris.Adeniyi-Jones@arm.com } 114910027SChris.Adeniyi-Jones@arm.com warn("returning %08p as start\n", new_start); 115010027SChris.Adeniyi-Jones@arm.com return new_start; 11515877Shsul@eecs.umich.edu } 11525877Shsul@eecs.umich.edu } 11535877Shsul@eecs.umich.edu } else { 115410027SChris.Adeniyi-Jones@arm.com if (use_provided_address && provided_address != start) 115510027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 11568601Ssteve.reinhardt@amd.com process->pTable->unmap(start + new_length, old_length - new_length); 115710027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 11585877Shsul@eecs.umich.edu } 11595877Shsul@eecs.umich.edu} 11601999SN/A 1161378SN/A/// Target stat() handler. 1162360SN/Atemplate <class OS> 11631450SN/ASyscallReturn 116411851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 11652680Sktlim@umich.edu ThreadContext *tc) 1166360SN/A{ 1167360SN/A std::string path; 1168360SN/A 11696701Sgblack@eecs.umich.edu int index = 0; 11708852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 11716701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11726701Sgblack@eecs.umich.edu return -EFAULT; 11736701Sgblack@eecs.umich.edu } 11746701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1175360SN/A 11763669Sbinkertn@umich.edu // Adjust path for current working directory 11773669Sbinkertn@umich.edu path = process->fullPath(path); 11783669Sbinkertn@umich.edu 1179360SN/A struct stat hostBuf; 1180360SN/A int result = stat(path.c_str(), &hostBuf); 1181360SN/A 1182360SN/A if (result < 0) 11832218SN/A return -errno; 1184360SN/A 11858706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1186360SN/A 11871458SN/A return 0; 1188360SN/A} 1189360SN/A 1190360SN/A 11915074Ssaidi@eecs.umich.edu/// Target stat64() handler. 11925074Ssaidi@eecs.umich.edutemplate <class OS> 11935074Ssaidi@eecs.umich.eduSyscallReturn 119411851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process, 11955074Ssaidi@eecs.umich.edu ThreadContext *tc) 11965074Ssaidi@eecs.umich.edu{ 11975074Ssaidi@eecs.umich.edu std::string path; 11985074Ssaidi@eecs.umich.edu 11996701Sgblack@eecs.umich.edu int index = 0; 12008852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 12016701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 12025074Ssaidi@eecs.umich.edu return -EFAULT; 12036701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 12045074Ssaidi@eecs.umich.edu 12055074Ssaidi@eecs.umich.edu // Adjust path for current working directory 12065074Ssaidi@eecs.umich.edu path = process->fullPath(path); 12075074Ssaidi@eecs.umich.edu 12085208Ssaidi@eecs.umich.edu#if NO_STAT64 12095208Ssaidi@eecs.umich.edu struct stat hostBuf; 12105208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 12115208Ssaidi@eecs.umich.edu#else 12125074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 12135074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 12145208Ssaidi@eecs.umich.edu#endif 12155074Ssaidi@eecs.umich.edu 12165074Ssaidi@eecs.umich.edu if (result < 0) 12175074Ssaidi@eecs.umich.edu return -errno; 12185074Ssaidi@eecs.umich.edu 12198706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12205074Ssaidi@eecs.umich.edu 12215074Ssaidi@eecs.umich.edu return 0; 12225074Ssaidi@eecs.umich.edu} 12235074Ssaidi@eecs.umich.edu 12245074Ssaidi@eecs.umich.edu 122510027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler. 122610027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 122710027SChris.Adeniyi-Jones@arm.comSyscallReturn 122811851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process, 122910027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 123010027SChris.Adeniyi-Jones@arm.com{ 123110027SChris.Adeniyi-Jones@arm.com int index = 0; 123210027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 123310027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 123410793Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 123510027SChris.Adeniyi-Jones@arm.com 123610027SChris.Adeniyi-Jones@arm.com std::string path; 123710027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 123810027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) 123910027SChris.Adeniyi-Jones@arm.com return -EFAULT; 124010027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 124110027SChris.Adeniyi-Jones@arm.com 124210027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 124310027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 124410027SChris.Adeniyi-Jones@arm.com 124510027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 124610027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 124710027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 124810027SChris.Adeniyi-Jones@arm.com#else 124910027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 125010027SChris.Adeniyi-Jones@arm.com int result = stat64(path.c_str(), &hostBuf); 125110027SChris.Adeniyi-Jones@arm.com#endif 125210027SChris.Adeniyi-Jones@arm.com 125310027SChris.Adeniyi-Jones@arm.com if (result < 0) 125410027SChris.Adeniyi-Jones@arm.com return -errno; 125510027SChris.Adeniyi-Jones@arm.com 125610027SChris.Adeniyi-Jones@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 125710027SChris.Adeniyi-Jones@arm.com 125810027SChris.Adeniyi-Jones@arm.com return 0; 125910027SChris.Adeniyi-Jones@arm.com} 126010027SChris.Adeniyi-Jones@arm.com 126110027SChris.Adeniyi-Jones@arm.com 12621999SN/A/// Target fstat64() handler. 12631999SN/Atemplate <class OS> 12641999SN/ASyscallReturn 126511856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 12661999SN/A{ 12676701Sgblack@eecs.umich.edu int index = 0; 126811856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 126911856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 127010931Sbrandon.potter@amd.com 127111856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 127211856Sbrandon.potter@amd.com if (!ffdp) 12731999SN/A return -EBADF; 127411856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 12751999SN/A 12762764Sstever@eecs.umich.edu#if NO_STAT64 12772064SN/A struct stat hostBuf; 127810931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 12792064SN/A#else 12802064SN/A struct stat64 hostBuf; 128110931Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 12822064SN/A#endif 12831999SN/A 12841999SN/A if (result < 0) 12852218SN/A return -errno; 12861999SN/A 128710931Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12881999SN/A 12891999SN/A return 0; 12901999SN/A} 12911999SN/A 12921999SN/A 1293378SN/A/// Target lstat() handler. 1294360SN/Atemplate <class OS> 12951450SN/ASyscallReturn 129611851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 12972680Sktlim@umich.edu ThreadContext *tc) 1298360SN/A{ 1299360SN/A std::string path; 1300360SN/A 13016701Sgblack@eecs.umich.edu int index = 0; 13028852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 13036701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 13046701Sgblack@eecs.umich.edu return -EFAULT; 13056701Sgblack@eecs.umich.edu } 13066701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1307360SN/A 13083669Sbinkertn@umich.edu // Adjust path for current working directory 13093669Sbinkertn@umich.edu path = process->fullPath(path); 13103669Sbinkertn@umich.edu 1311360SN/A struct stat hostBuf; 1312360SN/A int result = lstat(path.c_str(), &hostBuf); 1313360SN/A 1314360SN/A if (result < 0) 13151458SN/A return -errno; 1316360SN/A 13178706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1318360SN/A 13191458SN/A return 0; 1320360SN/A} 1321360SN/A 13221999SN/A/// Target lstat64() handler. 13231999SN/Atemplate <class OS> 13241999SN/ASyscallReturn 132511851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process, 13262680Sktlim@umich.edu ThreadContext *tc) 13271999SN/A{ 13281999SN/A std::string path; 13291999SN/A 13306701Sgblack@eecs.umich.edu int index = 0; 13318852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 13326701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 13336701Sgblack@eecs.umich.edu return -EFAULT; 13346701Sgblack@eecs.umich.edu } 13356701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 13361999SN/A 13373669Sbinkertn@umich.edu // Adjust path for current working directory 13383669Sbinkertn@umich.edu path = process->fullPath(path); 13393669Sbinkertn@umich.edu 13402764Sstever@eecs.umich.edu#if NO_STAT64 13412064SN/A struct stat hostBuf; 13422064SN/A int result = lstat(path.c_str(), &hostBuf); 13432064SN/A#else 13441999SN/A struct stat64 hostBuf; 13451999SN/A int result = lstat64(path.c_str(), &hostBuf); 13462064SN/A#endif 13471999SN/A 13481999SN/A if (result < 0) 13491999SN/A return -errno; 13501999SN/A 13518706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13521999SN/A 13531999SN/A return 0; 13541999SN/A} 13551999SN/A 1356378SN/A/// Target fstat() handler. 1357360SN/Atemplate <class OS> 13581450SN/ASyscallReturn 135911856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1360360SN/A{ 13616701Sgblack@eecs.umich.edu int index = 0; 136211856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 136311856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 1364360SN/A 136511380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1366360SN/A 136711856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 136811856Sbrandon.potter@amd.com if (!ffdp) 13691458SN/A return -EBADF; 137011856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 1371360SN/A 1372360SN/A struct stat hostBuf; 137310931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 1374360SN/A 1375360SN/A if (result < 0) 13761458SN/A return -errno; 1377360SN/A 137810931Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 13792021SN/A 13801458SN/A return 0; 1381360SN/A} 1382360SN/A 13831706SN/A/// Target statfs() handler. 13841706SN/Atemplate <class OS> 13851706SN/ASyscallReturn 138611851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 13872680Sktlim@umich.edu ThreadContext *tc) 13881706SN/A{ 138911799Sbrandon.potter@amd.com#if NO_STATFS 139011799Sbrandon.potter@amd.com warn("Host OS cannot support calls to statfs. Ignoring syscall"); 139111799Sbrandon.potter@amd.com#else 13921706SN/A std::string path; 13931706SN/A 13946701Sgblack@eecs.umich.edu int index = 0; 13958852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 13966701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 13976701Sgblack@eecs.umich.edu return -EFAULT; 13986701Sgblack@eecs.umich.edu } 13996701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 14001706SN/A 14013669Sbinkertn@umich.edu // Adjust path for current working directory 14023669Sbinkertn@umich.edu path = process->fullPath(path); 14033669Sbinkertn@umich.edu 14041706SN/A struct statfs hostBuf; 14051706SN/A int result = statfs(path.c_str(), &hostBuf); 14061706SN/A 14071706SN/A if (result < 0) 14082218SN/A return -errno; 14091706SN/A 141011759Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 141111799Sbrandon.potter@amd.com#endif 14121706SN/A return 0; 14131706SN/A} 14141706SN/A 141511886Sbrandon.potter@amd.comtemplate <class OS> 141611886Sbrandon.potter@amd.comSyscallReturn 141711886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 141811886Sbrandon.potter@amd.com{ 141911886Sbrandon.potter@amd.com int index = 0; 142012426Sqtt2@cornell.edu 142113557Sgabeblack@google.com RegVal flags = p->getSyscallArg(tc, index); 142213557Sgabeblack@google.com RegVal newStack = p->getSyscallArg(tc, index); 142311886Sbrandon.potter@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 142412426Sqtt2@cornell.edu 142513534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 142612426Sqtt2@cornell.edu /** 142713534Sandreas.sandberg@arm.com * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 142812426Sqtt2@cornell.edu * The flag defines the list of clone() arguments in the following 142912426Sqtt2@cornell.edu * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 143012426Sqtt2@cornell.edu */ 143113536Sandreas.sandberg@arm.com Addr tlsPtr = p->getSyscallArg(tc, index); 143212426Sqtt2@cornell.edu Addr ctidPtr = p->getSyscallArg(tc, index); 143312426Sqtt2@cornell.edu#else 143411886Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 143513536Sandreas.sandberg@arm.com Addr tlsPtr = p->getSyscallArg(tc, index); 143612426Sqtt2@cornell.edu#endif 143711886Sbrandon.potter@amd.com 143811886Sbrandon.potter@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 143911886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 144011886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 144111886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 144211886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 144311886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 144411886Sbrandon.potter@amd.com return -EINVAL; 144511886Sbrandon.potter@amd.com 144611886Sbrandon.potter@amd.com ThreadContext *ctc; 144713649Sqtt2@cornell.edu if (!(ctc = p->findFreeContext())) { 144813649Sqtt2@cornell.edu DPRINTF_SYSCALL(Verbose, "clone: no spare thread context in system" 144913649Sqtt2@cornell.edu "[cpu %d, thread %d]", tc->cpuId(), tc->threadId()); 145013649Sqtt2@cornell.edu return -EAGAIN; 145113649Sqtt2@cornell.edu } 145211886Sbrandon.potter@amd.com 145311886Sbrandon.potter@amd.com /** 145411886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 145511886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 145611886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 145711886Sbrandon.potter@amd.com * constructor. 145811886Sbrandon.potter@amd.com */ 145911886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 146011886Sbrandon.potter@amd.com pp->executable.assign(*(new std::string(p->progName()))); 146111886Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 146211886Sbrandon.potter@amd.com pp->system = p->system; 146311886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 146411886Sbrandon.potter@amd.com pp->input.assign("stdin"); 146511886Sbrandon.potter@amd.com pp->output.assign("stdout"); 146611886Sbrandon.potter@amd.com pp->errout.assign("stderr"); 146711886Sbrandon.potter@amd.com pp->uid = p->uid(); 146811886Sbrandon.potter@amd.com pp->euid = p->euid(); 146911886Sbrandon.potter@amd.com pp->gid = p->gid(); 147011886Sbrandon.potter@amd.com pp->egid = p->egid(); 147111886Sbrandon.potter@amd.com 147211886Sbrandon.potter@amd.com /* Find the first free PID that's less than the maximum */ 147311886Sbrandon.potter@amd.com std::set<int> const& pids = p->system->PIDs; 147411886Sbrandon.potter@amd.com int temp_pid = *pids.begin(); 147511886Sbrandon.potter@amd.com do { 147611886Sbrandon.potter@amd.com temp_pid++; 147711886Sbrandon.potter@amd.com } while (pids.find(temp_pid) != pids.end()); 147811886Sbrandon.potter@amd.com if (temp_pid >= System::maxPID) 147911886Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 148011886Sbrandon.potter@amd.com 148111886Sbrandon.potter@amd.com pp->pid = temp_pid; 148211886Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 148311886Sbrandon.potter@amd.com Process *cp = pp->create(); 148411886Sbrandon.potter@amd.com delete pp; 148511886Sbrandon.potter@amd.com 148611886Sbrandon.potter@amd.com Process *owner = ctc->getProcessPtr(); 148711886Sbrandon.potter@amd.com ctc->setProcessPtr(cp); 148811886Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 148911886Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 149011886Sbrandon.potter@amd.com 149111886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 149211886Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 149311886Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 149411886Sbrandon.potter@amd.com *ptid = cp->pid(); 149511886Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 149611886Sbrandon.potter@amd.com } 149711886Sbrandon.potter@amd.com 149811886Sbrandon.potter@amd.com cp->initState(); 149911886Sbrandon.potter@amd.com p->clone(tc, ctc, cp, flags); 150011886Sbrandon.potter@amd.com 150111911SBrandon.Potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 150211911SBrandon.Potter@amd.com delete cp->sigchld; 150311911SBrandon.Potter@amd.com cp->sigchld = p->sigchld; 150411911SBrandon.Potter@amd.com } else if (flags & OS::TGT_SIGCHLD) { 150511911SBrandon.Potter@amd.com *cp->sigchld = true; 150611911SBrandon.Potter@amd.com } 150711911SBrandon.Potter@amd.com 150811886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 150911886Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 151011886Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 151111886Sbrandon.potter@amd.com *ctid = cp->pid(); 151211886Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 151311886Sbrandon.potter@amd.com } 151411886Sbrandon.potter@amd.com 151511886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 151611886Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 151711886Sbrandon.potter@amd.com 151811886Sbrandon.potter@amd.com ctc->clearArchRegs(); 151911886Sbrandon.potter@amd.com 152013536Sandreas.sandberg@arm.com OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 152111886Sbrandon.potter@amd.com 152211886Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 152311886Sbrandon.potter@amd.com 152411886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 152511886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 152611886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 152711886Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 152811886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 152911886Sbrandon.potter@amd.com#endif 153011886Sbrandon.potter@amd.com 153113535Sandreas.sandberg@arm.com TheISA::PCState cpc = tc->pcState(); 153213535Sandreas.sandberg@arm.com cpc.advance(); 153313535Sandreas.sandberg@arm.com ctc->pcState(cpc); 153411886Sbrandon.potter@amd.com ctc->activate(); 153511886Sbrandon.potter@amd.com 153611886Sbrandon.potter@amd.com return cp->pid(); 153711886Sbrandon.potter@amd.com} 15381706SN/A 15391706SN/A/// Target fstatfs() handler. 15401706SN/Atemplate <class OS> 15411706SN/ASyscallReturn 154211856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 15431706SN/A{ 15446701Sgblack@eecs.umich.edu int index = 0; 154511856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 154611856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 15471706SN/A 154811856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 154911856Sbrandon.potter@amd.com if (!ffdp) 15501706SN/A return -EBADF; 155111856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 15521706SN/A 15531706SN/A struct statfs hostBuf; 155410931Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 15551706SN/A 15561706SN/A if (result < 0) 15572218SN/A return -errno; 15581706SN/A 155911759Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 15601706SN/A 15611706SN/A return 0; 15621706SN/A} 15631706SN/A 156413572Sbrandon.potter@amd.com/// Target readv() handler. 156513572Sbrandon.potter@amd.comtemplate <class OS> 156613572Sbrandon.potter@amd.comSyscallReturn 156713572Sbrandon.potter@amd.comreadvFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 156813572Sbrandon.potter@amd.com{ 156913572Sbrandon.potter@amd.com int index = 0; 157013572Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 157113572Sbrandon.potter@amd.com 157213572Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 157313572Sbrandon.potter@amd.com if (!ffdp) 157413572Sbrandon.potter@amd.com return -EBADF; 157513572Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 157613572Sbrandon.potter@amd.com 157713572Sbrandon.potter@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 157813572Sbrandon.potter@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 157913572Sbrandon.potter@amd.com size_t count = p->getSyscallArg(tc, index); 158013572Sbrandon.potter@amd.com typename OS::tgt_iovec tiov[count]; 158113572Sbrandon.potter@amd.com struct iovec hiov[count]; 158213572Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) { 158313572Sbrandon.potter@amd.com prox.readBlob(tiov_base + (i * sizeof(typename OS::tgt_iovec)), 158413572Sbrandon.potter@amd.com (uint8_t*)&tiov[i], sizeof(typename OS::tgt_iovec)); 158513572Sbrandon.potter@amd.com hiov[i].iov_len = TheISA::gtoh(tiov[i].iov_len); 158613572Sbrandon.potter@amd.com hiov[i].iov_base = new char [hiov[i].iov_len]; 158713572Sbrandon.potter@amd.com } 158813572Sbrandon.potter@amd.com 158913572Sbrandon.potter@amd.com int result = readv(sim_fd, hiov, count); 159013572Sbrandon.potter@amd.com int local_errno = errno; 159113572Sbrandon.potter@amd.com 159213572Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) { 159313572Sbrandon.potter@amd.com if (result != -1) { 159413572Sbrandon.potter@amd.com prox.writeBlob(TheISA::htog(tiov[i].iov_base), 159513572Sbrandon.potter@amd.com (uint8_t*)hiov[i].iov_base, hiov[i].iov_len); 159613572Sbrandon.potter@amd.com } 159713572Sbrandon.potter@amd.com delete [] (char *)hiov[i].iov_base; 159813572Sbrandon.potter@amd.com } 159913572Sbrandon.potter@amd.com 160013572Sbrandon.potter@amd.com return (result == -1) ? -local_errno : result; 160113572Sbrandon.potter@amd.com} 16021706SN/A 16031999SN/A/// Target writev() handler. 16041999SN/Atemplate <class OS> 16051999SN/ASyscallReturn 160611856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 16071999SN/A{ 16086701Sgblack@eecs.umich.edu int index = 0; 160911856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 161010931Sbrandon.potter@amd.com 161111856Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 161211856Sbrandon.potter@amd.com if (!hbfdp) 16131999SN/A return -EBADF; 161411856Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 16151999SN/A 161611856Sbrandon.potter@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 161711856Sbrandon.potter@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 161811856Sbrandon.potter@amd.com size_t count = p->getSyscallArg(tc, index); 16191999SN/A struct iovec hiov[count]; 16206227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 16211999SN/A typename OS::tgt_iovec tiov; 16222461SN/A 162311856Sbrandon.potter@amd.com prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 162411856Sbrandon.potter@amd.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 16258737Skoansin.tan@gmail.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 16261999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 162711856Sbrandon.potter@amd.com prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 162811856Sbrandon.potter@amd.com hiov[i].iov_len); 16291999SN/A } 16301999SN/A 163110931Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 16321999SN/A 16336227Snate@binkert.org for (size_t i = 0; i < count; ++i) 16341999SN/A delete [] (char *)hiov[i].iov_base; 16351999SN/A 163613572Sbrandon.potter@amd.com return (result == -1) ? -errno : result; 16371999SN/A} 16381999SN/A 163911385Sbrandon.potter@amd.com/// Real mmap handler. 1640360SN/Atemplate <class OS> 16411450SN/ASyscallReturn 164211851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 164311385Sbrandon.potter@amd.com bool is_mmap2) 1644360SN/A{ 16456701Sgblack@eecs.umich.edu int index = 0; 16466701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 16476701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 164811383Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 164911383Sbrandon.potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 16508324Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 165110486Stjablin@gmail.com int offset = p->getSyscallArg(tc, index); 1652360SN/A 165311385Sbrandon.potter@amd.com if (is_mmap2) 165411385Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 16559008Sgblack@eecs.umich.edu 165611383Sbrandon.potter@amd.com if (start & (TheISA::PageBytes - 1) || 165711383Sbrandon.potter@amd.com offset & (TheISA::PageBytes - 1) || 165811383Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 165911383Sbrandon.potter@amd.com tgt_flags & OS::TGT_MAP_SHARED) || 166011383Sbrandon.potter@amd.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 166111383Sbrandon.potter@amd.com !(tgt_flags & OS::TGT_MAP_SHARED)) || 166211383Sbrandon.potter@amd.com !length) { 166311383Sbrandon.potter@amd.com return -EINVAL; 166411383Sbrandon.potter@amd.com } 16658324Ssteve.reinhardt@amd.com 166611383Sbrandon.potter@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 166711383Sbrandon.potter@amd.com // With shared mmaps, there are two cases to consider: 166811383Sbrandon.potter@amd.com // 1) anonymous: writes should modify the mapping and this should be 166911383Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 167011383Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 167111383Sbrandon.potter@amd.com // structure which maintains information about the which virtual 167211383Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 167311383Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 167411383Sbrandon.potter@amd.com // 2) file-backed: writes should modify the mapping and the file 167511383Sbrandon.potter@amd.com // which is backed by the mapping. The shared mapping problem is the 167611383Sbrandon.potter@amd.com // same as what was mentioned about the anonymous mappings. For 167711383Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 167811383Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 167911383Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 168011383Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 168111383Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 168211383Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 168311383Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 168411383Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 168511383Sbrandon.potter@amd.com // a call to msync, but that again would require more information 168611383Sbrandon.potter@amd.com // than we currently maintain. 168711383Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 168811383Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 168911383Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 16908324Ssteve.reinhardt@amd.com } 16915877Shsul@eecs.umich.edu 169210486Stjablin@gmail.com length = roundUp(length, TheISA::PageBytes); 169310486Stjablin@gmail.com 169411383Sbrandon.potter@amd.com int sim_fd = -1; 169511383Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 169611383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 169711856Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 169811624Smichael.lebeane@amd.com 169911856Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 170011856Sbrandon.potter@amd.com if (dfdp) { 170111856Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 170211856Sbrandon.potter@amd.com return emul_driver->mmap(p, tc, start, length, prot, 170311624Smichael.lebeane@amd.com tgt_flags, tgt_fd, offset); 170411624Smichael.lebeane@amd.com } 170511624Smichael.lebeane@amd.com 170611856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 170711856Sbrandon.potter@amd.com if (!ffdp) 170811383Sbrandon.potter@amd.com return -EBADF; 170911856Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 1710360SN/A 171111913SBrandon.Potter@amd.com pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 171211383Sbrandon.potter@amd.com sim_fd, offset); 17138600Ssteve.reinhardt@amd.com 171411383Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 171511383Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 171611383Sbrandon.potter@amd.com return -errno; 17178600Ssteve.reinhardt@amd.com } 17182544SN/A } 17192544SN/A 172011383Sbrandon.potter@amd.com // Extend global mmap region if necessary. Note that we ignore the 172111383Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 172211383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 172311905SBrandon.Potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 172411905SBrandon.Potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 172511905SBrandon.Potter@amd.com 172611905SBrandon.Potter@amd.com start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 172711905SBrandon.Potter@amd.com mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 172811905SBrandon.Potter@amd.com 172911905SBrandon.Potter@amd.com mem_state->setMmapEnd(mmap_end); 173011383Sbrandon.potter@amd.com } 173111383Sbrandon.potter@amd.com 173211383Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 173311383Sbrandon.potter@amd.com start, start + length - 1); 173411383Sbrandon.potter@amd.com 173511383Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 173611383Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 173711383Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 173811383Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 173911383Sbrandon.potter@amd.com if (clobber) { 174011383Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 174111383Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 174211383Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 174311383Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 174411383Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 17458600Ssteve.reinhardt@amd.com } 17466672Sgblack@eecs.umich.edu } 17478600Ssteve.reinhardt@amd.com 174811383Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 174911383Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 175011383Sbrandon.potter@amd.com // fatal and bail out of the simulation. 17518601Ssteve.reinhardt@amd.com p->allocateMem(start, length, clobber); 17522544SN/A 175311383Sbrandon.potter@amd.com // Transfer content into target address space. 175411383Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 175511383Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 175611383Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 175711383Sbrandon.potter@amd.com // with something like: 175811383Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 175911383Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 176011383Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 176111383Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 176211383Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 176311383Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 176411383Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 176511383Sbrandon.potter@amd.com } else { 176611383Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 176711383Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 176811383Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 176911383Sbrandon.potter@amd.com // the host into the target address space. 177011383Sbrandon.potter@amd.com struct stat file_stat; 177111383Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 177211383Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 177311383Sbrandon.potter@amd.com 177411383Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 177511383Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 177611383Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 177711383Sbrandon.potter@amd.com // on the specified offset into the file. 177811383Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 177911383Sbrandon.potter@amd.com length); 178011383Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 178111383Sbrandon.potter@amd.com 178211383Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 178311383Sbrandon.potter@amd.com munmap(pmap, length); 178411383Sbrandon.potter@amd.com 178511392Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 178611392Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 178711392Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 178811392Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 178911392Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 179011392Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 179111392Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 179211392Sbrandon.potter@amd.com if (interpreter) { 179311392Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 179411392Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 179511392Sbrandon.potter@amd.com 179611392Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 179711392Sbrandon.potter@amd.com 179811392Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 179911856Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 180011856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 180111856Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 180211392Sbrandon.potter@amd.com 180311392Sbrandon.potter@amd.com if (lib) { 180411392Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 180511392Sbrandon.potter@amd.com lib->textBase(), start); 180611392Sbrandon.potter@amd.com } 180711392Sbrandon.potter@amd.com } 180811392Sbrandon.potter@amd.com } 180911392Sbrandon.potter@amd.com 181011383Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 181111383Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 181211383Sbrandon.potter@amd.com // execution (hopefully). 181311383Sbrandon.potter@amd.com } 181411383Sbrandon.potter@amd.com 18151458SN/A return start; 1816360SN/A} 1817360SN/A 181811593Santhony.gutierrez@amd.comtemplate <class OS> 181911593Santhony.gutierrez@amd.comSyscallReturn 182011851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 182111593Santhony.gutierrez@amd.com{ 182211593Santhony.gutierrez@amd.com int index = 0; 182311593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 182411593Santhony.gutierrez@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 182511593Santhony.gutierrez@amd.com int nbytes = p->getSyscallArg(tc, index); 182611593Santhony.gutierrez@amd.com int offset = p->getSyscallArg(tc, index); 182711593Santhony.gutierrez@amd.com 182811856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 182911856Sbrandon.potter@amd.com if (!ffdp) 183011593Santhony.gutierrez@amd.com return -EBADF; 183111856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 183211593Santhony.gutierrez@amd.com 183311593Santhony.gutierrez@amd.com BufferArg bufArg(bufPtr, nbytes); 183411593Santhony.gutierrez@amd.com bufArg.copyIn(tc->getMemProxy()); 183511593Santhony.gutierrez@amd.com 183611594Santhony.gutierrez@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 183711593Santhony.gutierrez@amd.com 183811593Santhony.gutierrez@amd.com return (bytes_written == -1) ? -errno : bytes_written; 183911593Santhony.gutierrez@amd.com} 184011593Santhony.gutierrez@amd.com 184111385Sbrandon.potter@amd.com/// Target mmap() handler. 184211385Sbrandon.potter@amd.comtemplate <class OS> 184311385Sbrandon.potter@amd.comSyscallReturn 184411851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 184511385Sbrandon.potter@amd.com{ 184611385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, false); 184711385Sbrandon.potter@amd.com} 184811385Sbrandon.potter@amd.com 184911385Sbrandon.potter@amd.com/// Target mmap2() handler. 185011385Sbrandon.potter@amd.comtemplate <class OS> 185111385Sbrandon.potter@amd.comSyscallReturn 185211851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 185311385Sbrandon.potter@amd.com{ 185411385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 185511385Sbrandon.potter@amd.com} 185611385Sbrandon.potter@amd.com 1857378SN/A/// Target getrlimit() handler. 1858360SN/Atemplate <class OS> 18591450SN/ASyscallReturn 186011851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 186111851Sbrandon.potter@amd.com ThreadContext *tc) 1862360SN/A{ 18636701Sgblack@eecs.umich.edu int index = 0; 18646701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 18656701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1866360SN/A 1867360SN/A switch (resource) { 186811906SBrandon.Potter@amd.com case OS::TGT_RLIMIT_STACK: 186911906SBrandon.Potter@amd.com // max stack size in bytes: make up a number (8MB for now) 187011906SBrandon.Potter@amd.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 187111906SBrandon.Potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 187211906SBrandon.Potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 187311906SBrandon.Potter@amd.com break; 1874360SN/A 187511906SBrandon.Potter@amd.com case OS::TGT_RLIMIT_DATA: 187611906SBrandon.Potter@amd.com // max data segment size in bytes: make up a number 187711906SBrandon.Potter@amd.com rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 187811906SBrandon.Potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 187911906SBrandon.Potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 188011906SBrandon.Potter@amd.com break; 18815877Shsul@eecs.umich.edu 188211906SBrandon.Potter@amd.com default: 188311906SBrandon.Potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 188411906SBrandon.Potter@amd.com return -EINVAL; 188511906SBrandon.Potter@amd.com break; 1886360SN/A } 1887360SN/A 18888706Sandreas.hansson@arm.com rlp.copyOut(tc->getMemProxy()); 18891458SN/A return 0; 1890360SN/A} 1891360SN/A 189212235Sar4jc@virginia.edutemplate <class OS> 189312235Sar4jc@virginia.eduSyscallReturn 189412235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process, 189512235Sar4jc@virginia.edu ThreadContext *tc) 189612235Sar4jc@virginia.edu{ 189712235Sar4jc@virginia.edu int index = 0; 189812235Sar4jc@virginia.edu if (process->getSyscallArg(tc, index) != 0) 189912235Sar4jc@virginia.edu { 190012235Sar4jc@virginia.edu warn("prlimit: ignoring rlimits for nonzero pid"); 190112235Sar4jc@virginia.edu return -EPERM; 190212235Sar4jc@virginia.edu } 190312235Sar4jc@virginia.edu int resource = process->getSyscallArg(tc, index); 190412235Sar4jc@virginia.edu Addr n = process->getSyscallArg(tc, index); 190512235Sar4jc@virginia.edu if (n != 0) 190612235Sar4jc@virginia.edu warn("prlimit: ignoring new rlimit"); 190712235Sar4jc@virginia.edu Addr o = process->getSyscallArg(tc, index); 190812235Sar4jc@virginia.edu if (o != 0) 190912235Sar4jc@virginia.edu { 191012416Sqtt2@cornell.edu TypedBufferArg<typename OS::rlimit> rlp(o); 191112235Sar4jc@virginia.edu switch (resource) { 191212235Sar4jc@virginia.edu case OS::TGT_RLIMIT_STACK: 191312235Sar4jc@virginia.edu // max stack size in bytes: make up a number (8MB for now) 191412235Sar4jc@virginia.edu rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 191512235Sar4jc@virginia.edu rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 191612235Sar4jc@virginia.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 191712235Sar4jc@virginia.edu break; 191812235Sar4jc@virginia.edu case OS::TGT_RLIMIT_DATA: 191912235Sar4jc@virginia.edu // max data segment size in bytes: make up a number 192012235Sar4jc@virginia.edu rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 192112235Sar4jc@virginia.edu rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 192212235Sar4jc@virginia.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 192312593Sjason@lowepower.com break; 192412235Sar4jc@virginia.edu default: 192512235Sar4jc@virginia.edu warn("prlimit: unimplemented resource %d", resource); 192612235Sar4jc@virginia.edu return -EINVAL; 192712235Sar4jc@virginia.edu break; 192812235Sar4jc@virginia.edu } 192912235Sar4jc@virginia.edu rlp.copyOut(tc->getMemProxy()); 193012235Sar4jc@virginia.edu } 193112235Sar4jc@virginia.edu return 0; 193212235Sar4jc@virginia.edu} 193312235Sar4jc@virginia.edu 193410796Sbrandon.potter@amd.com/// Target clock_gettime() function. 193510796Sbrandon.potter@amd.comtemplate <class OS> 193610796Sbrandon.potter@amd.comSyscallReturn 193711851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 193810796Sbrandon.potter@amd.com{ 193910796Sbrandon.potter@amd.com int index = 1; 194010796Sbrandon.potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 194110796Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 194210796Sbrandon.potter@amd.com 194310796Sbrandon.potter@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 194410796Sbrandon.potter@amd.com tp->tv_sec += seconds_since_epoch; 194510796Sbrandon.potter@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 194610796Sbrandon.potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 194710796Sbrandon.potter@amd.com 194810796Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 194910796Sbrandon.potter@amd.com 195010796Sbrandon.potter@amd.com return 0; 195110796Sbrandon.potter@amd.com} 195210796Sbrandon.potter@amd.com 195311337SMichael.Lebeane@amd.com/// Target clock_getres() function. 195411337SMichael.Lebeane@amd.comtemplate <class OS> 195511337SMichael.Lebeane@amd.comSyscallReturn 195611851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 195711337SMichael.Lebeane@amd.com{ 195811337SMichael.Lebeane@amd.com int index = 1; 195911337SMichael.Lebeane@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 196011337SMichael.Lebeane@amd.com 196111337SMichael.Lebeane@amd.com // Set resolution at ns, which is what clock_gettime() returns 196211337SMichael.Lebeane@amd.com tp->tv_sec = 0; 196311337SMichael.Lebeane@amd.com tp->tv_nsec = 1; 196411337SMichael.Lebeane@amd.com 196511337SMichael.Lebeane@amd.com tp.copyOut(tc->getMemProxy()); 196611337SMichael.Lebeane@amd.com 196711337SMichael.Lebeane@amd.com return 0; 196811337SMichael.Lebeane@amd.com} 196911337SMichael.Lebeane@amd.com 1970378SN/A/// Target gettimeofday() handler. 1971360SN/Atemplate <class OS> 19721450SN/ASyscallReturn 197311851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 197411851Sbrandon.potter@amd.com ThreadContext *tc) 1975360SN/A{ 19766701Sgblack@eecs.umich.edu int index = 0; 19776701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1978360SN/A 197910796Sbrandon.potter@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1980360SN/A tp->tv_sec += seconds_since_epoch; 19816109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 19826109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1983360SN/A 19848706Sandreas.hansson@arm.com tp.copyOut(tc->getMemProxy()); 1985360SN/A 19861458SN/A return 0; 1987360SN/A} 1988360SN/A 1989360SN/A 19901999SN/A/// Target utimes() handler. 19911999SN/Atemplate <class OS> 19921999SN/ASyscallReturn 199311851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process, 19942680Sktlim@umich.edu ThreadContext *tc) 19951999SN/A{ 19961999SN/A std::string path; 19971999SN/A 19986701Sgblack@eecs.umich.edu int index = 0; 19998852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 20006701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 20016701Sgblack@eecs.umich.edu return -EFAULT; 20026701Sgblack@eecs.umich.edu } 20031999SN/A 20046701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 20056701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 20068706Sandreas.hansson@arm.com tp.copyIn(tc->getMemProxy()); 20071999SN/A 20081999SN/A struct timeval hostTimeval[2]; 200911906SBrandon.Potter@amd.com for (int i = 0; i < 2; ++i) { 20108737Skoansin.tan@gmail.com hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 20118737Skoansin.tan@gmail.com hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 20121999SN/A } 20133669Sbinkertn@umich.edu 20143669Sbinkertn@umich.edu // Adjust path for current working directory 20153669Sbinkertn@umich.edu path = process->fullPath(path); 20163669Sbinkertn@umich.edu 20171999SN/A int result = utimes(path.c_str(), hostTimeval); 20181999SN/A 20191999SN/A if (result < 0) 20201999SN/A return -errno; 20211999SN/A 20221999SN/A return 0; 20231999SN/A} 202411886Sbrandon.potter@amd.com 202511886Sbrandon.potter@amd.comtemplate <class OS> 202611886Sbrandon.potter@amd.comSyscallReturn 202711886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 202811886Sbrandon.potter@amd.com{ 202911886Sbrandon.potter@amd.com desc->setFlags(0); 203011886Sbrandon.potter@amd.com 203111886Sbrandon.potter@amd.com int index = 0; 203211886Sbrandon.potter@amd.com std::string path; 203311886Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 203411886Sbrandon.potter@amd.com if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 203511886Sbrandon.potter@amd.com return -EFAULT; 203611886Sbrandon.potter@amd.com 203711886Sbrandon.potter@amd.com if (access(path.c_str(), F_OK) == -1) 203811886Sbrandon.potter@amd.com return -EACCES; 203911886Sbrandon.potter@amd.com 204011886Sbrandon.potter@amd.com auto read_in = [](std::vector<std::string> & vect, 204111886Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy, 204211886Sbrandon.potter@amd.com Addr mem_loc) 204311886Sbrandon.potter@amd.com { 204411886Sbrandon.potter@amd.com for (int inc = 0; ; inc++) { 204511886Sbrandon.potter@amd.com BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 204611886Sbrandon.potter@amd.com b.copyIn(mem_proxy); 204711886Sbrandon.potter@amd.com 204811886Sbrandon.potter@amd.com if (!*(Addr*)b.bufferPtr()) 204911886Sbrandon.potter@amd.com break; 205011886Sbrandon.potter@amd.com 205111886Sbrandon.potter@amd.com vect.push_back(std::string()); 205211886Sbrandon.potter@amd.com mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 205311886Sbrandon.potter@amd.com } 205411886Sbrandon.potter@amd.com }; 205511886Sbrandon.potter@amd.com 205611886Sbrandon.potter@amd.com /** 205711886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 205811886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 205911886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 206011886Sbrandon.potter@amd.com * constructor. 206111886Sbrandon.potter@amd.com */ 206211886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 206311886Sbrandon.potter@amd.com pp->executable = path; 206411886Sbrandon.potter@amd.com Addr argv_mem_loc = p->getSyscallArg(tc, index); 206511886Sbrandon.potter@amd.com read_in(pp->cmd, mem_proxy, argv_mem_loc); 206611886Sbrandon.potter@amd.com Addr envp_mem_loc = p->getSyscallArg(tc, index); 206711886Sbrandon.potter@amd.com read_in(pp->env, mem_proxy, envp_mem_loc); 206811886Sbrandon.potter@amd.com pp->uid = p->uid(); 206911886Sbrandon.potter@amd.com pp->egid = p->egid(); 207011886Sbrandon.potter@amd.com pp->euid = p->euid(); 207111886Sbrandon.potter@amd.com pp->gid = p->gid(); 207211886Sbrandon.potter@amd.com pp->ppid = p->ppid(); 207311886Sbrandon.potter@amd.com pp->pid = p->pid(); 207411886Sbrandon.potter@amd.com pp->input.assign("cin"); 207511886Sbrandon.potter@amd.com pp->output.assign("cout"); 207611886Sbrandon.potter@amd.com pp->errout.assign("cerr"); 207711886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 207811886Sbrandon.potter@amd.com pp->system = p->system; 207911886Sbrandon.potter@amd.com /** 208011886Sbrandon.potter@amd.com * Prevent process object creation with identical PIDs (which will trip 208111886Sbrandon.potter@amd.com * a fatal check in Process constructor). The execve call is supposed to 208211886Sbrandon.potter@amd.com * take over the currently executing process' identity but replace 208311886Sbrandon.potter@amd.com * whatever it is doing with a new process image. Instead of hijacking 208411886Sbrandon.potter@amd.com * the process object in the simulator, we create a new process object 208511886Sbrandon.potter@amd.com * and bind to the previous process' thread below (hijacking the thread). 208611886Sbrandon.potter@amd.com */ 208711886Sbrandon.potter@amd.com p->system->PIDs.erase(p->pid()); 208811886Sbrandon.potter@amd.com Process *new_p = pp->create(); 208911886Sbrandon.potter@amd.com delete pp; 209011886Sbrandon.potter@amd.com 209111886Sbrandon.potter@amd.com /** 209211886Sbrandon.potter@amd.com * Work through the file descriptor array and close any files marked 209311886Sbrandon.potter@amd.com * close-on-exec. 209411886Sbrandon.potter@amd.com */ 209511886Sbrandon.potter@amd.com new_p->fds = p->fds; 209611886Sbrandon.potter@amd.com for (int i = 0; i < new_p->fds->getSize(); i++) { 209711886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 209811886Sbrandon.potter@amd.com if (fdep && fdep->getCOE()) 209911886Sbrandon.potter@amd.com new_p->fds->closeFDEntry(i); 210011886Sbrandon.potter@amd.com } 210111886Sbrandon.potter@amd.com 210211886Sbrandon.potter@amd.com *new_p->sigchld = true; 210311886Sbrandon.potter@amd.com 210411886Sbrandon.potter@amd.com delete p; 210511886Sbrandon.potter@amd.com tc->clearArchRegs(); 210611886Sbrandon.potter@amd.com tc->setProcessPtr(new_p); 210711886Sbrandon.potter@amd.com new_p->assignThreadContext(tc->contextId()); 210811886Sbrandon.potter@amd.com new_p->initState(); 210911886Sbrandon.potter@amd.com tc->activate(); 211011886Sbrandon.potter@amd.com TheISA::PCState pcState = tc->pcState(); 211111886Sbrandon.potter@amd.com tc->setNPC(pcState.instAddr()); 211211886Sbrandon.potter@amd.com 211311886Sbrandon.potter@amd.com desc->setFlags(SyscallDesc::SuppressReturnValue); 211411886Sbrandon.potter@amd.com return 0; 211511886Sbrandon.potter@amd.com} 211611886Sbrandon.potter@amd.com 2117378SN/A/// Target getrusage() function. 2118360SN/Atemplate <class OS> 21191450SN/ASyscallReturn 212011851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 21212680Sktlim@umich.edu ThreadContext *tc) 2122360SN/A{ 21236701Sgblack@eecs.umich.edu int index = 0; 21246701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 21256701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2126360SN/A 21273670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 21283670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 2129360SN/A rup->ru_stime.tv_sec = 0; 2130360SN/A rup->ru_stime.tv_usec = 0; 2131360SN/A rup->ru_maxrss = 0; 2132360SN/A rup->ru_ixrss = 0; 2133360SN/A rup->ru_idrss = 0; 2134360SN/A rup->ru_isrss = 0; 2135360SN/A rup->ru_minflt = 0; 2136360SN/A rup->ru_majflt = 0; 2137360SN/A rup->ru_nswap = 0; 2138360SN/A rup->ru_inblock = 0; 2139360SN/A rup->ru_oublock = 0; 2140360SN/A rup->ru_msgsnd = 0; 2141360SN/A rup->ru_msgrcv = 0; 2142360SN/A rup->ru_nsignals = 0; 2143360SN/A rup->ru_nvcsw = 0; 2144360SN/A rup->ru_nivcsw = 0; 2145360SN/A 21463670Sbinkertn@umich.edu switch (who) { 21473670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 214810796Sbrandon.potter@amd.com getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 21498737Skoansin.tan@gmail.com rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 21508737Skoansin.tan@gmail.com rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 21513670Sbinkertn@umich.edu break; 21523670Sbinkertn@umich.edu 21533670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 21543670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 21553670Sbinkertn@umich.edu break; 21563670Sbinkertn@umich.edu 21573670Sbinkertn@umich.edu default: 21583670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 21593670Sbinkertn@umich.edu // plow ahead 21603670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 21613670Sbinkertn@umich.edu who); 21623670Sbinkertn@umich.edu } 21633670Sbinkertn@umich.edu 21648706Sandreas.hansson@arm.com rup.copyOut(tc->getMemProxy()); 2165360SN/A 21661458SN/A return 0; 2167360SN/A} 2168360SN/A 21696683Stjones1@inf.ed.ac.uk/// Target times() function. 21706683Stjones1@inf.ed.ac.uktemplate <class OS> 21716683Stjones1@inf.ed.ac.ukSyscallReturn 217211851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process, 217311851Sbrandon.potter@amd.com ThreadContext *tc) 21746683Stjones1@inf.ed.ac.uk{ 21756701Sgblack@eecs.umich.edu int index = 0; 21766701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 21776683Stjones1@inf.ed.ac.uk 21786683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 21797823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 21806683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 21816683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 21826683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 21836683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 21846683Stjones1@inf.ed.ac.uk 21856683Stjones1@inf.ed.ac.uk // Convert to host endianness 21868737Skoansin.tan@gmail.com bufp->tms_utime = TheISA::htog(bufp->tms_utime); 21876683Stjones1@inf.ed.ac.uk 21886683Stjones1@inf.ed.ac.uk // Write back 21898706Sandreas.hansson@arm.com bufp.copyOut(tc->getMemProxy()); 21906683Stjones1@inf.ed.ac.uk 21916683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 21926683Stjones1@inf.ed.ac.uk return clocks; 21936683Stjones1@inf.ed.ac.uk} 21942553SN/A 21956684Stjones1@inf.ed.ac.uk/// Target time() function. 21966684Stjones1@inf.ed.ac.uktemplate <class OS> 21976684Stjones1@inf.ed.ac.ukSyscallReturn 219811851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 21996684Stjones1@inf.ed.ac.uk{ 22006684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 220110796Sbrandon.potter@amd.com getElapsedTimeMicro(sec, usec); 22026684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 22036684Stjones1@inf.ed.ac.uk 22046701Sgblack@eecs.umich.edu int index = 0; 22056701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 220611321Ssteve.reinhardt@amd.com if (taddr != 0) { 22076684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 22088737Skoansin.tan@gmail.com t = TheISA::htog(t); 22098852Sandreas.hansson@arm.com SETranslatingPortProxy &p = tc->getMemProxy(); 22108852Sandreas.hansson@arm.com p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 22116684Stjones1@inf.ed.ac.uk } 22126684Stjones1@inf.ed.ac.uk return sec; 22136684Stjones1@inf.ed.ac.uk} 22142553SN/A 221511910SBrandon.Potter@amd.comtemplate <class OS> 221611910SBrandon.Potter@amd.comSyscallReturn 221711910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 221811910SBrandon.Potter@amd.com{ 221911910SBrandon.Potter@amd.com int index = 0; 222011910SBrandon.Potter@amd.com int tgid = process->getSyscallArg(tc, index); 222111910SBrandon.Potter@amd.com int tid = process->getSyscallArg(tc, index); 222211910SBrandon.Potter@amd.com int sig = process->getSyscallArg(tc, index); 222311910SBrandon.Potter@amd.com 222411910SBrandon.Potter@amd.com /** 222511910SBrandon.Potter@amd.com * This system call is intended to allow killing a specific thread 222611910SBrandon.Potter@amd.com * within an arbitrary thread group if sanctioned with permission checks. 222711910SBrandon.Potter@amd.com * It's usually true that threads share the termination signal as pointed 222811910SBrandon.Potter@amd.com * out by the pthread_kill man page and this seems to be the intended 222911910SBrandon.Potter@amd.com * usage. Due to this being an emulated environment, assume the following: 223011910SBrandon.Potter@amd.com * Threads are allowed to call tgkill because the EUID for all threads 223111910SBrandon.Potter@amd.com * should be the same. There is no signal handling mechanism for kernel 223211910SBrandon.Potter@amd.com * registration of signal handlers since signals are poorly supported in 223311910SBrandon.Potter@amd.com * emulation mode. Since signal handlers cannot be registered, all 223411910SBrandon.Potter@amd.com * threads within in a thread group must share the termination signal. 223511910SBrandon.Potter@amd.com * We never exhaust PIDs so there's no chance of finding the wrong one 223611910SBrandon.Potter@amd.com * due to PID rollover. 223711910SBrandon.Potter@amd.com */ 223811910SBrandon.Potter@amd.com 223911910SBrandon.Potter@amd.com System *sys = tc->getSystemPtr(); 224011910SBrandon.Potter@amd.com Process *tgt_proc = nullptr; 224111910SBrandon.Potter@amd.com for (int i = 0; i < sys->numContexts(); i++) { 224211910SBrandon.Potter@amd.com Process *temp = sys->threadContexts[i]->getProcessPtr(); 224311910SBrandon.Potter@amd.com if (temp->pid() == tid) { 224411910SBrandon.Potter@amd.com tgt_proc = temp; 224511910SBrandon.Potter@amd.com break; 224611910SBrandon.Potter@amd.com } 224711910SBrandon.Potter@amd.com } 224811910SBrandon.Potter@amd.com 224911910SBrandon.Potter@amd.com if (sig != 0 || sig != OS::TGT_SIGABRT) 225011910SBrandon.Potter@amd.com return -EINVAL; 225111910SBrandon.Potter@amd.com 225211910SBrandon.Potter@amd.com if (tgt_proc == nullptr) 225311910SBrandon.Potter@amd.com return -ESRCH; 225411910SBrandon.Potter@amd.com 225511910SBrandon.Potter@amd.com if (tgid != -1 && tgt_proc->tgid() != tgid) 225611910SBrandon.Potter@amd.com return -ESRCH; 225711910SBrandon.Potter@amd.com 225811910SBrandon.Potter@amd.com if (sig == OS::TGT_SIGABRT) 225911910SBrandon.Potter@amd.com exitGroupFunc(desc, 252, process, tc); 226011910SBrandon.Potter@amd.com 226111910SBrandon.Potter@amd.com return 0; 226211910SBrandon.Potter@amd.com} 226311910SBrandon.Potter@amd.com 226413568Sbrandon.potter@amd.comtemplate <class OS> 226513568Sbrandon.potter@amd.comSyscallReturn 226613568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 226713568Sbrandon.potter@amd.com{ 226813568Sbrandon.potter@amd.com int index = 0; 226913568Sbrandon.potter@amd.com int domain = p->getSyscallArg(tc, index); 227013568Sbrandon.potter@amd.com int type = p->getSyscallArg(tc, index); 227113568Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 227213568Sbrandon.potter@amd.com 227313568Sbrandon.potter@amd.com int sim_fd = socket(domain, type, prot); 227413568Sbrandon.potter@amd.com if (sim_fd == -1) 227513568Sbrandon.potter@amd.com return -errno; 227613568Sbrandon.potter@amd.com 227713568Sbrandon.potter@amd.com auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 227813568Sbrandon.potter@amd.com int tgt_fd = p->fds->allocFD(sfdp); 227913568Sbrandon.potter@amd.com 228013568Sbrandon.potter@amd.com return tgt_fd; 228113568Sbrandon.potter@amd.com} 228213568Sbrandon.potter@amd.com 228313568Sbrandon.potter@amd.comtemplate <class OS> 228413568Sbrandon.potter@amd.comSyscallReturn 228513568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 228613568Sbrandon.potter@amd.com{ 228713568Sbrandon.potter@amd.com int index = 0; 228813568Sbrandon.potter@amd.com int domain = p->getSyscallArg(tc, index); 228913568Sbrandon.potter@amd.com int type = p->getSyscallArg(tc, index); 229013568Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 229113568Sbrandon.potter@amd.com Addr svPtr = p->getSyscallArg(tc, index); 229213568Sbrandon.potter@amd.com 229313568Sbrandon.potter@amd.com BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 229413568Sbrandon.potter@amd.com int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 229513568Sbrandon.potter@amd.com if (status == -1) 229613568Sbrandon.potter@amd.com return -errno; 229713568Sbrandon.potter@amd.com 229813568Sbrandon.potter@amd.com int *fds = (int *)svBuf.bufferPtr(); 229913568Sbrandon.potter@amd.com 230013568Sbrandon.potter@amd.com auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 230113568Sbrandon.potter@amd.com fds[0] = p->fds->allocFD(sfdp1); 230213568Sbrandon.potter@amd.com auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 230313568Sbrandon.potter@amd.com fds[1] = p->fds->allocFD(sfdp2); 230413568Sbrandon.potter@amd.com svBuf.copyOut(tc->getMemProxy()); 230513568Sbrandon.potter@amd.com 230613568Sbrandon.potter@amd.com return status; 230713568Sbrandon.potter@amd.com} 23082553SN/A 230913570Sbrandon.potter@amd.comtemplate <class OS> 231013570Sbrandon.potter@amd.comSyscallReturn 231113570Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 231213570Sbrandon.potter@amd.com{ 231313570Sbrandon.potter@amd.com int retval; 231413570Sbrandon.potter@amd.com 231513570Sbrandon.potter@amd.com int index = 0; 231613570Sbrandon.potter@amd.com int nfds_t = p->getSyscallArg(tc, index); 231713570Sbrandon.potter@amd.com Addr fds_read_ptr = p->getSyscallArg(tc, index); 231813570Sbrandon.potter@amd.com Addr fds_writ_ptr = p->getSyscallArg(tc, index); 231913570Sbrandon.potter@amd.com Addr fds_excp_ptr = p->getSyscallArg(tc, index); 232013570Sbrandon.potter@amd.com Addr time_val_ptr = p->getSyscallArg(tc, index); 232113570Sbrandon.potter@amd.com 232213570Sbrandon.potter@amd.com TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 232313570Sbrandon.potter@amd.com TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 232413570Sbrandon.potter@amd.com TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 232513570Sbrandon.potter@amd.com TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 232613570Sbrandon.potter@amd.com 232713570Sbrandon.potter@amd.com /** 232813570Sbrandon.potter@amd.com * Host fields. Notice that these use the definitions from the system 232913570Sbrandon.potter@amd.com * headers instead of the gem5 headers and libraries. If the host and 233013570Sbrandon.potter@amd.com * target have different header file definitions, this will not work. 233113570Sbrandon.potter@amd.com */ 233213570Sbrandon.potter@amd.com fd_set rd_h; 233313570Sbrandon.potter@amd.com FD_ZERO(&rd_h); 233413570Sbrandon.potter@amd.com fd_set wr_h; 233513570Sbrandon.potter@amd.com FD_ZERO(&wr_h); 233613570Sbrandon.potter@amd.com fd_set ex_h; 233713570Sbrandon.potter@amd.com FD_ZERO(&ex_h); 233813570Sbrandon.potter@amd.com 233913570Sbrandon.potter@amd.com /** 234013570Sbrandon.potter@amd.com * Copy in the fd_set from the target. 234113570Sbrandon.potter@amd.com */ 234213570Sbrandon.potter@amd.com if (fds_read_ptr) 234313570Sbrandon.potter@amd.com rd_t.copyIn(tc->getMemProxy()); 234413570Sbrandon.potter@amd.com if (fds_writ_ptr) 234513570Sbrandon.potter@amd.com wr_t.copyIn(tc->getMemProxy()); 234613570Sbrandon.potter@amd.com if (fds_excp_ptr) 234713570Sbrandon.potter@amd.com ex_t.copyIn(tc->getMemProxy()); 234813570Sbrandon.potter@amd.com 234913570Sbrandon.potter@amd.com /** 235013570Sbrandon.potter@amd.com * We need to translate the target file descriptor set into a host file 235113570Sbrandon.potter@amd.com * descriptor set. This involves both our internal process fd array 235213570Sbrandon.potter@amd.com * and the fd_set defined in Linux header files. The nfds field also 235313570Sbrandon.potter@amd.com * needs to be updated as it will be only target specific after 235413570Sbrandon.potter@amd.com * retrieving it from the target; the nfds value is expected to be the 235513570Sbrandon.potter@amd.com * highest file descriptor that needs to be checked, so we need to extend 235613570Sbrandon.potter@amd.com * it out for nfds_h when we do the update. 235713570Sbrandon.potter@amd.com */ 235813570Sbrandon.potter@amd.com int nfds_h = 0; 235913570Sbrandon.potter@amd.com std::map<int, int> trans_map; 236013570Sbrandon.potter@amd.com auto try_add_host_set = [&](fd_set *tgt_set_entry, 236113570Sbrandon.potter@amd.com fd_set *hst_set_entry, 236213570Sbrandon.potter@amd.com int iter) -> bool 236313570Sbrandon.potter@amd.com { 236413570Sbrandon.potter@amd.com /** 236513570Sbrandon.potter@amd.com * By this point, we know that we are looking at a valid file 236613570Sbrandon.potter@amd.com * descriptor set on the target. We need to check if the target file 236713570Sbrandon.potter@amd.com * descriptor value passed in as iter is part of the set. 236813570Sbrandon.potter@amd.com */ 236913570Sbrandon.potter@amd.com if (FD_ISSET(iter, tgt_set_entry)) { 237013570Sbrandon.potter@amd.com /** 237113570Sbrandon.potter@amd.com * We know that the target file descriptor belongs to the set, 237213570Sbrandon.potter@amd.com * but we do not yet know if the file descriptor is valid or 237313570Sbrandon.potter@amd.com * that we have a host mapping. Check that now. 237413570Sbrandon.potter@amd.com */ 237513570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 237613570Sbrandon.potter@amd.com if (!hbfdp) 237713570Sbrandon.potter@amd.com return true; 237813570Sbrandon.potter@amd.com auto sim_fd = hbfdp->getSimFD(); 237913570Sbrandon.potter@amd.com 238013570Sbrandon.potter@amd.com /** 238113570Sbrandon.potter@amd.com * Add the sim_fd to tgt_fd translation into trans_map for use 238213570Sbrandon.potter@amd.com * later when we need to zero the target fd_set structures and 238313570Sbrandon.potter@amd.com * then update them with hits returned from the host select call. 238413570Sbrandon.potter@amd.com */ 238513570Sbrandon.potter@amd.com trans_map[sim_fd] = iter; 238613570Sbrandon.potter@amd.com 238713570Sbrandon.potter@amd.com /** 238813570Sbrandon.potter@amd.com * We know that the host file descriptor exists so now we check 238913570Sbrandon.potter@amd.com * if we need to update the max count for nfds_h before passing 239013570Sbrandon.potter@amd.com * the duplicated structure into the host. 239113570Sbrandon.potter@amd.com */ 239213570Sbrandon.potter@amd.com nfds_h = std::max(nfds_h - 1, sim_fd + 1); 239313570Sbrandon.potter@amd.com 239413570Sbrandon.potter@amd.com /** 239513570Sbrandon.potter@amd.com * Add the host file descriptor to the set that we are going to 239613570Sbrandon.potter@amd.com * pass into the host. 239713570Sbrandon.potter@amd.com */ 239813570Sbrandon.potter@amd.com FD_SET(sim_fd, hst_set_entry); 239913570Sbrandon.potter@amd.com } 240013570Sbrandon.potter@amd.com return false; 240113570Sbrandon.potter@amd.com }; 240213570Sbrandon.potter@amd.com 240313570Sbrandon.potter@amd.com for (int i = 0; i < nfds_t; i++) { 240413570Sbrandon.potter@amd.com if (fds_read_ptr) { 240513570Sbrandon.potter@amd.com bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 240613570Sbrandon.potter@amd.com if (ebadf) return -EBADF; 240713570Sbrandon.potter@amd.com } 240813570Sbrandon.potter@amd.com if (fds_writ_ptr) { 240913570Sbrandon.potter@amd.com bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 241013570Sbrandon.potter@amd.com if (ebadf) return -EBADF; 241113570Sbrandon.potter@amd.com } 241213570Sbrandon.potter@amd.com if (fds_excp_ptr) { 241313570Sbrandon.potter@amd.com bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 241413570Sbrandon.potter@amd.com if (ebadf) return -EBADF; 241513570Sbrandon.potter@amd.com } 241613570Sbrandon.potter@amd.com } 241713570Sbrandon.potter@amd.com 241813570Sbrandon.potter@amd.com if (time_val_ptr) { 241913570Sbrandon.potter@amd.com /** 242013570Sbrandon.potter@amd.com * It might be possible to decrement the timeval based on some 242113570Sbrandon.potter@amd.com * derivation of wall clock determined from elapsed simulator ticks 242213570Sbrandon.potter@amd.com * but that seems like overkill. Rather, we just set the timeval with 242313570Sbrandon.potter@amd.com * zero timeout. (There is no reason to block during the simulation 242413570Sbrandon.potter@amd.com * as it only decreases simulator performance.) 242513570Sbrandon.potter@amd.com */ 242613570Sbrandon.potter@amd.com tp->tv_sec = 0; 242713570Sbrandon.potter@amd.com tp->tv_usec = 0; 242813570Sbrandon.potter@amd.com 242913570Sbrandon.potter@amd.com retval = select(nfds_h, 243013570Sbrandon.potter@amd.com fds_read_ptr ? &rd_h : nullptr, 243113570Sbrandon.potter@amd.com fds_writ_ptr ? &wr_h : nullptr, 243213570Sbrandon.potter@amd.com fds_excp_ptr ? &ex_h : nullptr, 243313570Sbrandon.potter@amd.com (timeval*)&*tp); 243413570Sbrandon.potter@amd.com } else { 243513570Sbrandon.potter@amd.com /** 243613570Sbrandon.potter@amd.com * If the timeval pointer is null, setup a new timeval structure to 243713570Sbrandon.potter@amd.com * pass into the host select call. Unfortunately, we will need to 243813570Sbrandon.potter@amd.com * manually check the return value and throw a retry fault if the 243913570Sbrandon.potter@amd.com * return value is zero. Allowing the system call to block will 244013570Sbrandon.potter@amd.com * likely deadlock the event queue. 244113570Sbrandon.potter@amd.com */ 244213570Sbrandon.potter@amd.com struct timeval tv = { 0, 0 }; 244313570Sbrandon.potter@amd.com 244413570Sbrandon.potter@amd.com retval = select(nfds_h, 244513570Sbrandon.potter@amd.com fds_read_ptr ? &rd_h : nullptr, 244613570Sbrandon.potter@amd.com fds_writ_ptr ? &wr_h : nullptr, 244713570Sbrandon.potter@amd.com fds_excp_ptr ? &ex_h : nullptr, 244813570Sbrandon.potter@amd.com &tv); 244913570Sbrandon.potter@amd.com 245013570Sbrandon.potter@amd.com if (retval == 0) { 245113570Sbrandon.potter@amd.com /** 245213570Sbrandon.potter@amd.com * If blocking indefinitely, check the signal list to see if a 245313570Sbrandon.potter@amd.com * signal would break the poll out of the retry cycle and try to 245413570Sbrandon.potter@amd.com * return the signal interrupt instead. 245513570Sbrandon.potter@amd.com */ 245613570Sbrandon.potter@amd.com for (auto sig : tc->getSystemPtr()->signalList) 245713570Sbrandon.potter@amd.com if (sig.receiver == p) 245813570Sbrandon.potter@amd.com return -EINTR; 245913570Sbrandon.potter@amd.com return SyscallReturn::retry(); 246013570Sbrandon.potter@amd.com } 246113570Sbrandon.potter@amd.com } 246213570Sbrandon.potter@amd.com 246313570Sbrandon.potter@amd.com if (retval == -1) 246413570Sbrandon.potter@amd.com return -errno; 246513570Sbrandon.potter@amd.com 246613570Sbrandon.potter@amd.com FD_ZERO((fd_set*)&*rd_t); 246713570Sbrandon.potter@amd.com FD_ZERO((fd_set*)&*wr_t); 246813570Sbrandon.potter@amd.com FD_ZERO((fd_set*)&*ex_t); 246913570Sbrandon.potter@amd.com 247013570Sbrandon.potter@amd.com /** 247113570Sbrandon.potter@amd.com * We need to translate the host file descriptor set into a target file 247213570Sbrandon.potter@amd.com * descriptor set. This involves both our internal process fd array 247313570Sbrandon.potter@amd.com * and the fd_set defined in header files. 247413570Sbrandon.potter@amd.com */ 247513570Sbrandon.potter@amd.com for (int i = 0; i < nfds_h; i++) { 247613570Sbrandon.potter@amd.com if (fds_read_ptr) { 247713570Sbrandon.potter@amd.com if (FD_ISSET(i, &rd_h)) 247813570Sbrandon.potter@amd.com FD_SET(trans_map[i], (fd_set*)&*rd_t); 247913570Sbrandon.potter@amd.com } 248013570Sbrandon.potter@amd.com 248113570Sbrandon.potter@amd.com if (fds_writ_ptr) { 248213570Sbrandon.potter@amd.com if (FD_ISSET(i, &wr_h)) 248313570Sbrandon.potter@amd.com FD_SET(trans_map[i], (fd_set*)&*wr_t); 248413570Sbrandon.potter@amd.com } 248513570Sbrandon.potter@amd.com 248613570Sbrandon.potter@amd.com if (fds_excp_ptr) { 248713570Sbrandon.potter@amd.com if (FD_ISSET(i, &ex_h)) 248813570Sbrandon.potter@amd.com FD_SET(trans_map[i], (fd_set*)&*ex_t); 248913570Sbrandon.potter@amd.com } 249013570Sbrandon.potter@amd.com } 249113570Sbrandon.potter@amd.com 249213570Sbrandon.potter@amd.com if (fds_read_ptr) 249313570Sbrandon.potter@amd.com rd_t.copyOut(tc->getMemProxy()); 249413570Sbrandon.potter@amd.com if (fds_writ_ptr) 249513570Sbrandon.potter@amd.com wr_t.copyOut(tc->getMemProxy()); 249613570Sbrandon.potter@amd.com if (fds_excp_ptr) 249713570Sbrandon.potter@amd.com ex_t.copyOut(tc->getMemProxy()); 249813570Sbrandon.potter@amd.com if (time_val_ptr) 249913570Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 250013570Sbrandon.potter@amd.com 250113570Sbrandon.potter@amd.com return retval; 250213570Sbrandon.potter@amd.com} 250313570Sbrandon.potter@amd.com 250413570Sbrandon.potter@amd.comtemplate <class OS> 250513570Sbrandon.potter@amd.comSyscallReturn 250613570Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 250713570Sbrandon.potter@amd.com{ 250813570Sbrandon.potter@amd.com int index = 0; 250913570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 251013570Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 251113570Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 251213570Sbrandon.potter@amd.com 251313570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 251413570Sbrandon.potter@amd.com if (!hbfdp) 251513570Sbrandon.potter@amd.com return -EBADF; 251613570Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 251713570Sbrandon.potter@amd.com 251813570Sbrandon.potter@amd.com struct pollfd pfd; 251913570Sbrandon.potter@amd.com pfd.fd = sim_fd; 252013570Sbrandon.potter@amd.com pfd.events = POLLIN | POLLPRI; 252113570Sbrandon.potter@amd.com if ((poll(&pfd, 1, 0) == 0) 252213570Sbrandon.potter@amd.com && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 252313570Sbrandon.potter@amd.com return SyscallReturn::retry(); 252413570Sbrandon.potter@amd.com 252513570Sbrandon.potter@amd.com BufferArg buf_arg(buf_ptr, nbytes); 252613570Sbrandon.potter@amd.com int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 252713570Sbrandon.potter@amd.com 252813570Sbrandon.potter@amd.com if (bytes_read > 0) 252913570Sbrandon.potter@amd.com buf_arg.copyOut(tc->getMemProxy()); 253013570Sbrandon.potter@amd.com 253113570Sbrandon.potter@amd.com return (bytes_read == -1) ? -errno : bytes_read; 253213570Sbrandon.potter@amd.com} 253313570Sbrandon.potter@amd.com 253413570Sbrandon.potter@amd.comtemplate <class OS> 253513570Sbrandon.potter@amd.comSyscallReturn 253613570Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 253713570Sbrandon.potter@amd.com{ 253813570Sbrandon.potter@amd.com int index = 0; 253913570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 254013570Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 254113570Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 254213570Sbrandon.potter@amd.com 254313570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 254413570Sbrandon.potter@amd.com if (!hbfdp) 254513570Sbrandon.potter@amd.com return -EBADF; 254613570Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 254713570Sbrandon.potter@amd.com 254813570Sbrandon.potter@amd.com BufferArg buf_arg(buf_ptr, nbytes); 254913570Sbrandon.potter@amd.com buf_arg.copyIn(tc->getMemProxy()); 255013570Sbrandon.potter@amd.com 255113570Sbrandon.potter@amd.com struct pollfd pfd; 255213570Sbrandon.potter@amd.com pfd.fd = sim_fd; 255313570Sbrandon.potter@amd.com pfd.events = POLLOUT; 255413570Sbrandon.potter@amd.com 255513570Sbrandon.potter@amd.com /** 255613570Sbrandon.potter@amd.com * We don't want to poll on /dev/random. The kernel will not enable the 255713570Sbrandon.potter@amd.com * file descriptor for writing unless the entropy in the system falls 255813570Sbrandon.potter@amd.com * below write_wakeup_threshold. This is not guaranteed to happen 255913570Sbrandon.potter@amd.com * depending on host settings. 256013570Sbrandon.potter@amd.com */ 256113570Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 256213570Sbrandon.potter@amd.com if (ffdp && (ffdp->getFileName() != "/dev/random")) { 256313570Sbrandon.potter@amd.com if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 256413570Sbrandon.potter@amd.com return SyscallReturn::retry(); 256513570Sbrandon.potter@amd.com } 256613570Sbrandon.potter@amd.com 256713570Sbrandon.potter@amd.com int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 256813570Sbrandon.potter@amd.com 256913570Sbrandon.potter@amd.com if (bytes_written != -1) 257013570Sbrandon.potter@amd.com fsync(sim_fd); 257113570Sbrandon.potter@amd.com 257213570Sbrandon.potter@amd.com return (bytes_written == -1) ? -errno : bytes_written; 257313570Sbrandon.potter@amd.com} 257413570Sbrandon.potter@amd.com 257513570Sbrandon.potter@amd.comtemplate <class OS> 257613570Sbrandon.potter@amd.comSyscallReturn 257713570Sbrandon.potter@amd.comwait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 257813570Sbrandon.potter@amd.com{ 257913570Sbrandon.potter@amd.com int index = 0; 258013570Sbrandon.potter@amd.com pid_t pid = p->getSyscallArg(tc, index); 258113570Sbrandon.potter@amd.com Addr statPtr = p->getSyscallArg(tc, index); 258213570Sbrandon.potter@amd.com int options = p->getSyscallArg(tc, index); 258313570Sbrandon.potter@amd.com Addr rusagePtr = p->getSyscallArg(tc, index); 258413570Sbrandon.potter@amd.com 258513570Sbrandon.potter@amd.com if (rusagePtr) 258613570Sbrandon.potter@amd.com DPRINTFR(SyscallVerbose, 258713570Sbrandon.potter@amd.com "%d: %s: syscall wait4: rusage pointer provided however " 258813570Sbrandon.potter@amd.com "functionality not supported. Ignoring rusage pointer.\n", 258913570Sbrandon.potter@amd.com curTick(), tc->getCpuPtr()->name()); 259013570Sbrandon.potter@amd.com 259113570Sbrandon.potter@amd.com /** 259213570Sbrandon.potter@amd.com * Currently, wait4 is only implemented so that it will wait for children 259313570Sbrandon.potter@amd.com * exit conditions which are denoted by a SIGCHLD signals posted into the 259413570Sbrandon.potter@amd.com * system signal list. We return no additional information via any of the 259513570Sbrandon.potter@amd.com * parameters supplied to wait4. If nothing is found in the system signal 259613570Sbrandon.potter@amd.com * list, we will wait indefinitely for SIGCHLD to post by retrying the 259713570Sbrandon.potter@amd.com * call. 259813570Sbrandon.potter@amd.com */ 259913570Sbrandon.potter@amd.com System *sysh = tc->getSystemPtr(); 260013570Sbrandon.potter@amd.com std::list<BasicSignal>::iterator iter; 260113570Sbrandon.potter@amd.com for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 260213570Sbrandon.potter@amd.com if (iter->receiver == p) { 260313570Sbrandon.potter@amd.com if (pid < -1) { 260413570Sbrandon.potter@amd.com if ((iter->sender->pgid() == -pid) 260513570Sbrandon.potter@amd.com && (iter->signalValue == OS::TGT_SIGCHLD)) 260613570Sbrandon.potter@amd.com goto success; 260713570Sbrandon.potter@amd.com } else if (pid == -1) { 260813570Sbrandon.potter@amd.com if (iter->signalValue == OS::TGT_SIGCHLD) 260913570Sbrandon.potter@amd.com goto success; 261013570Sbrandon.potter@amd.com } else if (pid == 0) { 261113570Sbrandon.potter@amd.com if ((iter->sender->pgid() == p->pgid()) 261213570Sbrandon.potter@amd.com && (iter->signalValue == OS::TGT_SIGCHLD)) 261313570Sbrandon.potter@amd.com goto success; 261413570Sbrandon.potter@amd.com } else { 261513570Sbrandon.potter@amd.com if ((iter->sender->pid() == pid) 261613570Sbrandon.potter@amd.com && (iter->signalValue == OS::TGT_SIGCHLD)) 261713570Sbrandon.potter@amd.com goto success; 261813570Sbrandon.potter@amd.com } 261913570Sbrandon.potter@amd.com } 262013570Sbrandon.potter@amd.com } 262113570Sbrandon.potter@amd.com 262213570Sbrandon.potter@amd.com return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 262313570Sbrandon.potter@amd.com 262413570Sbrandon.potter@amd.comsuccess: 262513570Sbrandon.potter@amd.com // Set status to EXITED for WIFEXITED evaluations. 262613570Sbrandon.potter@amd.com const int EXITED = 0; 262713570Sbrandon.potter@amd.com BufferArg statusBuf(statPtr, sizeof(int)); 262813570Sbrandon.potter@amd.com *(int *)statusBuf.bufferPtr() = EXITED; 262913570Sbrandon.potter@amd.com statusBuf.copyOut(tc->getMemProxy()); 263013570Sbrandon.potter@amd.com 263113570Sbrandon.potter@amd.com // Return the child PID. 263213570Sbrandon.potter@amd.com pid_t retval = iter->sender->pid(); 263313570Sbrandon.potter@amd.com sysh->signalList.erase(iter); 263413570Sbrandon.potter@amd.com return retval; 263513570Sbrandon.potter@amd.com} 263613570Sbrandon.potter@amd.com 263713570Sbrandon.potter@amd.comtemplate <class OS> 263813570Sbrandon.potter@amd.comSyscallReturn 263913570Sbrandon.potter@amd.comacceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 264013570Sbrandon.potter@amd.com{ 264113570Sbrandon.potter@amd.com struct sockaddr sa; 264213570Sbrandon.potter@amd.com socklen_t addrLen; 264313570Sbrandon.potter@amd.com int host_fd; 264413570Sbrandon.potter@amd.com int index = 0; 264513570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 264613570Sbrandon.potter@amd.com Addr addrPtr = p->getSyscallArg(tc, index); 264713570Sbrandon.potter@amd.com Addr lenPtr = p->getSyscallArg(tc, index); 264813570Sbrandon.potter@amd.com 264913570Sbrandon.potter@amd.com BufferArg *lenBufPtr = nullptr; 265013570Sbrandon.potter@amd.com BufferArg *addrBufPtr = nullptr; 265113570Sbrandon.potter@amd.com 265213570Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 265313570Sbrandon.potter@amd.com if (!sfdp) 265413570Sbrandon.potter@amd.com return -EBADF; 265513570Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 265613570Sbrandon.potter@amd.com 265713570Sbrandon.potter@amd.com /** 265813570Sbrandon.potter@amd.com * We poll the socket file descriptor first to guarantee that we do not 265913570Sbrandon.potter@amd.com * block on our accept call. The socket can be opened without the 266013570Sbrandon.potter@amd.com * non-blocking flag (it blocks). This will cause deadlocks between 266113570Sbrandon.potter@amd.com * communicating processes. 266213570Sbrandon.potter@amd.com */ 266313570Sbrandon.potter@amd.com struct pollfd pfd; 266413570Sbrandon.potter@amd.com pfd.fd = sim_fd; 266513570Sbrandon.potter@amd.com pfd.events = POLLIN | POLLPRI; 266613570Sbrandon.potter@amd.com if ((poll(&pfd, 1, 0) == 0) 266713570Sbrandon.potter@amd.com && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 266813570Sbrandon.potter@amd.com return SyscallReturn::retry(); 266913570Sbrandon.potter@amd.com 267013570Sbrandon.potter@amd.com if (lenPtr) { 267113570Sbrandon.potter@amd.com lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 267213570Sbrandon.potter@amd.com lenBufPtr->copyIn(tc->getMemProxy()); 267313570Sbrandon.potter@amd.com memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 267413570Sbrandon.potter@amd.com sizeof(socklen_t)); 267513570Sbrandon.potter@amd.com } 267613570Sbrandon.potter@amd.com 267713570Sbrandon.potter@amd.com if (addrPtr) { 267813570Sbrandon.potter@amd.com addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 267913570Sbrandon.potter@amd.com addrBufPtr->copyIn(tc->getMemProxy()); 268013570Sbrandon.potter@amd.com memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 268113570Sbrandon.potter@amd.com sizeof(struct sockaddr)); 268213570Sbrandon.potter@amd.com } 268313570Sbrandon.potter@amd.com 268413570Sbrandon.potter@amd.com host_fd = accept(sim_fd, &sa, &addrLen); 268513570Sbrandon.potter@amd.com 268613570Sbrandon.potter@amd.com if (host_fd == -1) 268713570Sbrandon.potter@amd.com return -errno; 268813570Sbrandon.potter@amd.com 268913570Sbrandon.potter@amd.com if (addrPtr) { 269013570Sbrandon.potter@amd.com memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 269113570Sbrandon.potter@amd.com addrBufPtr->copyOut(tc->getMemProxy()); 269213570Sbrandon.potter@amd.com delete(addrBufPtr); 269313570Sbrandon.potter@amd.com } 269413570Sbrandon.potter@amd.com 269513570Sbrandon.potter@amd.com if (lenPtr) { 269613570Sbrandon.potter@amd.com *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 269713570Sbrandon.potter@amd.com lenBufPtr->copyOut(tc->getMemProxy()); 269813570Sbrandon.potter@amd.com delete(lenBufPtr); 269913570Sbrandon.potter@amd.com } 270013570Sbrandon.potter@amd.com 270113570Sbrandon.potter@amd.com auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 270213570Sbrandon.potter@amd.com sfdp->_type, sfdp->_protocol); 270313570Sbrandon.potter@amd.com return p->fds->allocFD(afdp); 270413570Sbrandon.potter@amd.com} 270513570Sbrandon.potter@amd.com 27061354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 2707