syscall_emul.hh revision 13570
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 119360SN/A////////////////////////////////////////////////////////////////////// 120360SN/A// 121360SN/A// The following emulation functions are generic enough that they 122360SN/A// don't need to be recompiled for different emulated OS's. They are 123360SN/A// defined in sim/syscall_emul.cc. 124360SN/A// 125360SN/A////////////////////////////////////////////////////////////////////// 126360SN/A 127360SN/A 128378SN/A/// Handler for unimplemented syscalls that we haven't thought about. 1291706SN/ASyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 13011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 131378SN/A 132378SN/A/// Handler for unimplemented syscalls that we never intend to 133378SN/A/// implement (signal handling, etc.) and should not affect the correct 134378SN/A/// behavior of the program. Print a warning only if the appropriate 135378SN/A/// trace flag is enabled. Return success to the target program. 1361706SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 13711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 138360SN/A 13911760Sbrandon.potter@amd.com// Target fallocateFunc() handler. 14011760Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 14111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 14211760Sbrandon.potter@amd.com 1436109Ssanchezd@stanford.edu/// Target exit() handler: terminate current context. 1441706SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 14511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 146378SN/A 1476109Ssanchezd@stanford.edu/// Target exit_group() handler: terminate simulation. (exit all threads) 1486109Ssanchezd@stanford.eduSyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 14911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1506109Ssanchezd@stanford.edu 15111886Sbrandon.potter@amd.com/// Target set_tid_address() handler. 15211886Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 15311886Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 15411886Sbrandon.potter@amd.com 155378SN/A/// Target getpagesize() handler. 1561706SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 15711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 158378SN/A 1595748SSteve.Reinhardt@amd.com/// Target brk() handler: set brk address. 1605748SSteve.Reinhardt@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 16111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 162378SN/A 163378SN/A/// Target close() handler. 1641706SN/ASyscallReturn closeFunc(SyscallDesc *desc, int num, 16511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 166378SN/A 167378SN/A/// Target lseek() handler. 1681706SN/ASyscallReturn lseekFunc(SyscallDesc *desc, int num, 16911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 170378SN/A 1714118Sgblack@eecs.umich.edu/// Target _llseek() handler. 1724118Sgblack@eecs.umich.eduSyscallReturn _llseekFunc(SyscallDesc *desc, int num, 17311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1744118Sgblack@eecs.umich.edu 175378SN/A/// Target munmap() handler. 1761706SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 17711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 178378SN/A 17913568Sbrandon.potter@amd.com/// Target shutdown() handler. 18013568Sbrandon.potter@amd.comSyscallReturn shutdownFunc(SyscallDesc *desc, int num, 18113568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 18213568Sbrandon.potter@amd.com 183378SN/A/// Target gethostname() handler. 1841706SN/ASyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 18511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 186360SN/A 1875513SMichael.Adler@intel.com/// Target getcwd() handler. 1885513SMichael.Adler@intel.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 18911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1905513SMichael.Adler@intel.com 19110203SAli.Saidi@ARM.com/// Target readlink() handler. 19210203SAli.Saidi@ARM.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 19410203SAli.Saidi@ARM.com int index = 0); 1955513SMichael.Adler@intel.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 19611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1975513SMichael.Adler@intel.com 198511SN/A/// Target unlink() handler. 19910633Smichaelupton@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 20011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 20110633Smichaelupton@gmail.com int index); 2021706SN/ASyscallReturn unlinkFunc(SyscallDesc *desc, int num, 20311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 204511SN/A 20512795Smattdsinclair@gmail.com/// Target link() handler 20612795Smattdsinclair@gmail.comSyscallReturn linkFunc(SyscallDesc *desc, int num, Process *p, 20712795Smattdsinclair@gmail.com ThreadContext *tc); 20812795Smattdsinclair@gmail.com 20912796Smattdsinclair@gmail.com/// Target symlink() handler. 21012796Smattdsinclair@gmail.comSyscallReturn symlinkFunc(SyscallDesc *desc, int num, Process *p, 21112796Smattdsinclair@gmail.com ThreadContext *tc); 21212796Smattdsinclair@gmail.com 2135513SMichael.Adler@intel.com/// Target mkdir() handler. 2145513SMichael.Adler@intel.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 21511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2165513SMichael.Adler@intel.com 21713031Sbrandon.potter@amd.com/// Target mknod() handler. 21813031Sbrandon.potter@amd.comSyscallReturn mknodFunc(SyscallDesc *desc, int num, 21913031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22013031Sbrandon.potter@amd.com 22113031Sbrandon.potter@amd.com/// Target chdir() handler. 22213031Sbrandon.potter@amd.comSyscallReturn chdirFunc(SyscallDesc *desc, int num, 22313031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22413031Sbrandon.potter@amd.com 22513031Sbrandon.potter@amd.com// Target rmdir() handler. 22613031Sbrandon.potter@amd.comSyscallReturn rmdirFunc(SyscallDesc *desc, int num, 22713031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 22813031Sbrandon.potter@amd.com 229511SN/A/// Target rename() handler. 2301706SN/ASyscallReturn renameFunc(SyscallDesc *desc, int num, 23111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2321706SN/A 2331706SN/A 2341706SN/A/// Target truncate() handler. 2351706SN/ASyscallReturn truncateFunc(SyscallDesc *desc, int num, 23611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2371706SN/A 2381706SN/A 2391706SN/A/// Target ftruncate() handler. 2401706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 24111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2421706SN/A 243511SN/A 2446703Svince@csl.cornell.edu/// Target truncate64() handler. 2456703Svince@csl.cornell.eduSyscallReturn truncate64Func(SyscallDesc *desc, int num, 24611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2476703Svince@csl.cornell.edu 2486685Stjones1@inf.ed.ac.uk/// Target ftruncate64() handler. 2496685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 25011851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2516685Stjones1@inf.ed.ac.uk 2526685Stjones1@inf.ed.ac.uk 2535513SMichael.Adler@intel.com/// Target umask() handler. 2545513SMichael.Adler@intel.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 25511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2565513SMichael.Adler@intel.com 25711885Sbrandon.potter@amd.com/// Target gettid() handler. 25811885Sbrandon.potter@amd.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 25911885Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2605513SMichael.Adler@intel.com 2611999SN/A/// Target chown() handler. 2621999SN/ASyscallReturn chownFunc(SyscallDesc *desc, int num, 26311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2641999SN/A 26511885Sbrandon.potter@amd.com/// Target setpgid() handler. 26611885Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 26711885Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2681999SN/A 2691999SN/A/// Target fchown() handler. 2701999SN/ASyscallReturn fchownFunc(SyscallDesc *desc, int num, 27111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2721999SN/A 2733079Sstever@eecs.umich.edu/// Target dup() handler. 2743079Sstever@eecs.umich.eduSyscallReturn dupFunc(SyscallDesc *desc, int num, 27511851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2763079Sstever@eecs.umich.edu 27711908SBrandon.Potter@amd.com/// Target dup2() handler. 27811908SBrandon.Potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 27911908SBrandon.Potter@amd.com Process *process, ThreadContext *tc); 28011908SBrandon.Potter@amd.com 28111875Sbrandon.potter@amd.com/// Target fcntl() handler. 2822093SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 28311851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2842093SN/A 2852687Sksewell@umich.edu/// Target fcntl64() handler. 2862687Sksewell@umich.eduSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 28711851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2882687Sksewell@umich.edu 2892238SN/A/// Target setuid() handler. 2902238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 29111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2922238SN/A 29311908SBrandon.Potter@amd.com/// Target pipe() handler. 29411908SBrandon.Potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 29511908SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 29611908SBrandon.Potter@amd.com 29711908SBrandon.Potter@amd.com/// Internal pipe() handler. 29811908SBrandon.Potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 29911908SBrandon.Potter@amd.com ThreadContext *tc, bool pseudoPipe); 30011908SBrandon.Potter@amd.com 3012238SN/A/// Target getpid() handler. 3022238SN/ASyscallReturn getpidFunc(SyscallDesc *desc, int num, 30311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3042238SN/A 30513568Sbrandon.potter@amd.com// Target bind() handler. 30613568Sbrandon.potter@amd.comSyscallReturn bindFunc(SyscallDesc *desc, int num, 30713568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 30813568Sbrandon.potter@amd.com 30913568Sbrandon.potter@amd.com// Target listen() handler. 31013568Sbrandon.potter@amd.comSyscallReturn listenFunc(SyscallDesc *desc, int num, 31113568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 31213568Sbrandon.potter@amd.com 31313568Sbrandon.potter@amd.com// Target connect() handler. 31413568Sbrandon.potter@amd.comSyscallReturn connectFunc(SyscallDesc *desc, int num, 31513568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 31613568Sbrandon.potter@amd.com 31713448Sciro.santilli@arm.com#if defined(SYS_getdents) 31813031Sbrandon.potter@amd.com// Target getdents() handler. 31913031Sbrandon.potter@amd.comSyscallReturn getdentsFunc(SyscallDesc *desc, int num, 32013031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 32113448Sciro.santilli@arm.com#endif 32213031Sbrandon.potter@amd.com 32313539Sjavier.setoain@arm.com#if defined(SYS_getdents64) 32413539Sjavier.setoain@arm.com// Target getdents() handler. 32513539Sjavier.setoain@arm.comSyscallReturn getdents64Func(SyscallDesc *desc, int num, 32613539Sjavier.setoain@arm.com Process *p, ThreadContext *tc); 32713539Sjavier.setoain@arm.com#endif 32813539Sjavier.setoain@arm.com 32913569Sbrandon.potter@amd.com// Target sendto() handler. 33013569Sbrandon.potter@amd.comSyscallReturn sendtoFunc(SyscallDesc *desc, int num, 33113569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 33213569Sbrandon.potter@amd.com 33313569Sbrandon.potter@amd.com// Target recvfrom() handler. 33413569Sbrandon.potter@amd.comSyscallReturn recvfromFunc(SyscallDesc *desc, int num, 33513569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 33613569Sbrandon.potter@amd.com 33713569Sbrandon.potter@amd.com// Target recvmsg() handler. 33813569Sbrandon.potter@amd.comSyscallReturn recvmsgFunc(SyscallDesc *desc, int num, 33913569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 34013569Sbrandon.potter@amd.com 34113569Sbrandon.potter@amd.com// Target sendmsg() handler. 34213569Sbrandon.potter@amd.comSyscallReturn sendmsgFunc(SyscallDesc *desc, int num, 34313569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 34413569Sbrandon.potter@amd.com 34513031Sbrandon.potter@amd.com// Target getuid() handler. 3462238SN/ASyscallReturn getuidFunc(SyscallDesc *desc, int num, 34711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3482238SN/A 3492238SN/A/// Target getgid() handler. 3502238SN/ASyscallReturn getgidFunc(SyscallDesc *desc, int num, 35111851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3522238SN/A 3532238SN/A/// Target getppid() handler. 3542238SN/ASyscallReturn getppidFunc(SyscallDesc *desc, int num, 35511851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3562238SN/A 3572238SN/A/// Target geteuid() handler. 3582238SN/ASyscallReturn geteuidFunc(SyscallDesc *desc, int num, 35911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3602238SN/A 3612238SN/A/// Target getegid() handler. 3622238SN/ASyscallReturn getegidFunc(SyscallDesc *desc, int num, 36311851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 3642238SN/A 3659455Smitch.hayenga+gem5@gmail.com/// Target access() handler 3669455Smitch.hayenga+gem5@gmail.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 36711851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 36810203SAli.Saidi@ARM.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 36911851Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 37011851Sbrandon.potter@amd.com int index); 3719455Smitch.hayenga+gem5@gmail.com 3729112Smarc.orr@gmail.com/// Futex system call 37311906SBrandon.Potter@amd.com/// Implemented by Daniel Sanchez 37411906SBrandon.Potter@amd.com/// Used by printf's in multi-threaded apps 3759112Smarc.orr@gmail.comtemplate <class OS> 3769112Smarc.orr@gmail.comSyscallReturn 37711851Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3789112Smarc.orr@gmail.com ThreadContext *tc) 3799112Smarc.orr@gmail.com{ 38011911SBrandon.Potter@amd.com using namespace std; 3819112Smarc.orr@gmail.com 38211911SBrandon.Potter@amd.com int index = 0; 38311911SBrandon.Potter@amd.com Addr uaddr = process->getSyscallArg(tc, index); 38411911SBrandon.Potter@amd.com int op = process->getSyscallArg(tc, index); 38511911SBrandon.Potter@amd.com int val = process->getSyscallArg(tc, index); 3869112Smarc.orr@gmail.com 38711911SBrandon.Potter@amd.com /* 38811911SBrandon.Potter@amd.com * Unsupported option that does not affect the correctness of the 38911911SBrandon.Potter@amd.com * application. This is a performance optimization utilized by Linux. 39011911SBrandon.Potter@amd.com */ 3919238Slluc.alvarez@bsc.es op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 3929112Smarc.orr@gmail.com 39311911SBrandon.Potter@amd.com FutexMap &futex_map = tc->getSystemPtr()->futexMap; 3949112Smarc.orr@gmail.com 39511911SBrandon.Potter@amd.com if (OS::TGT_FUTEX_WAIT == op) { 39611911SBrandon.Potter@amd.com // Ensure futex system call accessed atomically. 39711911SBrandon.Potter@amd.com BufferArg buf(uaddr, sizeof(int)); 39811911SBrandon.Potter@amd.com buf.copyIn(tc->getMemProxy()); 39911911SBrandon.Potter@amd.com int mem_val = *(int*)buf.bufferPtr(); 4009112Smarc.orr@gmail.com 40111911SBrandon.Potter@amd.com /* 40211911SBrandon.Potter@amd.com * The value in memory at uaddr is not equal with the expected val 40311911SBrandon.Potter@amd.com * (a different thread must have changed it before the system call was 40411911SBrandon.Potter@amd.com * invoked). In this case, we need to throw an error. 40511911SBrandon.Potter@amd.com */ 40611911SBrandon.Potter@amd.com if (val != mem_val) 4079112Smarc.orr@gmail.com return -OS::TGT_EWOULDBLOCK; 4089112Smarc.orr@gmail.com 40911911SBrandon.Potter@amd.com futex_map.suspend(uaddr, process->tgid(), tc); 41011911SBrandon.Potter@amd.com 4119112Smarc.orr@gmail.com return 0; 41211911SBrandon.Potter@amd.com } else if (OS::TGT_FUTEX_WAKE == op) { 41311911SBrandon.Potter@amd.com return futex_map.wakeup(uaddr, process->tgid(), val); 4149112Smarc.orr@gmail.com } 4159112Smarc.orr@gmail.com 41611911SBrandon.Potter@amd.com warn("futex: op %d not implemented; ignoring.", op); 41711911SBrandon.Potter@amd.com return -ENOSYS; 4189112Smarc.orr@gmail.com} 4199112Smarc.orr@gmail.com 4202238SN/A 4212238SN/A/// Pseudo Funcs - These functions use a different return convension, 4222238SN/A/// returning a second value in a register other than the normal return register 4232238SN/ASyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 42411851Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 4252238SN/A 4262238SN/A/// Target getpidPseudo() handler. 4272238SN/ASyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 42811851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 4292238SN/A 4302238SN/A/// Target getuidPseudo() handler. 4312238SN/ASyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 43211851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 4332238SN/A 4342238SN/A/// Target getgidPseudo() handler. 4352238SN/ASyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 43611851Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 4372238SN/A 4382238SN/A 4391354SN/A/// A readable name for 1,000,000, for converting microseconds to seconds. 4401354SN/Aconst int one_million = 1000000; 44110796Sbrandon.potter@amd.com/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 44210796Sbrandon.potter@amd.comconst int one_billion = 1000000000; 4431354SN/A 4441354SN/A/// Approximate seconds since the epoch (1/1/1970). About a billion, 4451354SN/A/// by my reckoning. We want to keep this a constant (not use the 4461354SN/A/// real-world time) to keep simulations repeatable. 4471354SN/Aconst unsigned seconds_since_epoch = 1000000000; 4481354SN/A 4491354SN/A/// Helper function to convert current elapsed time to seconds and 4501354SN/A/// microseconds. 4511354SN/Atemplate <class T1, class T2> 4521354SN/Avoid 45310796Sbrandon.potter@amd.comgetElapsedTimeMicro(T1 &sec, T2 &usec) 4541354SN/A{ 45510796Sbrandon.potter@amd.com uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 4561354SN/A sec = elapsed_usecs / one_million; 4571354SN/A usec = elapsed_usecs % one_million; 4581354SN/A} 4591354SN/A 46010796Sbrandon.potter@amd.com/// Helper function to convert current elapsed time to seconds and 46110796Sbrandon.potter@amd.com/// nanoseconds. 46210796Sbrandon.potter@amd.comtemplate <class T1, class T2> 46310796Sbrandon.potter@amd.comvoid 46410796Sbrandon.potter@amd.comgetElapsedTimeNano(T1 &sec, T2 &nsec) 46510796Sbrandon.potter@amd.com{ 46610796Sbrandon.potter@amd.com uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 46710796Sbrandon.potter@amd.com sec = elapsed_nsecs / one_billion; 46810796Sbrandon.potter@amd.com nsec = elapsed_nsecs % one_billion; 46910796Sbrandon.potter@amd.com} 47010796Sbrandon.potter@amd.com 471360SN/A////////////////////////////////////////////////////////////////////// 472360SN/A// 473360SN/A// The following emulation functions are generic, but need to be 474360SN/A// templated to account for differences in types, constants, etc. 475360SN/A// 476360SN/A////////////////////////////////////////////////////////////////////// 477360SN/A 47811759Sbrandon.potter@amd.com typedef struct statfs hst_statfs; 4793113Sgblack@eecs.umich.edu#if NO_STAT64 4803113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 4813113Sgblack@eecs.umich.edu typedef struct stat hst_stat64; 4823113Sgblack@eecs.umich.edu#else 4833113Sgblack@eecs.umich.edu typedef struct stat hst_stat; 4843113Sgblack@eecs.umich.edu typedef struct stat64 hst_stat64; 4853113Sgblack@eecs.umich.edu#endif 4863113Sgblack@eecs.umich.edu 4873113Sgblack@eecs.umich.edu//// Helper function to convert a host stat buffer to a target stat 4883113Sgblack@eecs.umich.edu//// buffer. Also copies the target buffer out to the simulated 4893113Sgblack@eecs.umich.edu//// memory space. Used by stat(), fstat(), and lstat(). 4903113Sgblack@eecs.umich.edu 4913113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat> 49212032Sandreas.sandberg@arm.comvoid 4933113Sgblack@eecs.umich.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 4943113Sgblack@eecs.umich.edu{ 4954189Sgblack@eecs.umich.edu using namespace TheISA; 4964189Sgblack@eecs.umich.edu 4973113Sgblack@eecs.umich.edu if (fakeTTY) 4983113Sgblack@eecs.umich.edu tgt->st_dev = 0xA; 4993113Sgblack@eecs.umich.edu else 5003113Sgblack@eecs.umich.edu tgt->st_dev = host->st_dev; 5018737Skoansin.tan@gmail.com tgt->st_dev = TheISA::htog(tgt->st_dev); 5023113Sgblack@eecs.umich.edu tgt->st_ino = host->st_ino; 5038737Skoansin.tan@gmail.com tgt->st_ino = TheISA::htog(tgt->st_ino); 5043277Sgblack@eecs.umich.edu tgt->st_mode = host->st_mode; 5055515SMichael.Adler@intel.com if (fakeTTY) { 5065515SMichael.Adler@intel.com // Claim to be a character device 5075515SMichael.Adler@intel.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 5085515SMichael.Adler@intel.com tgt->st_mode |= S_IFCHR; // Set S_IFCHR 5095515SMichael.Adler@intel.com } 5108737Skoansin.tan@gmail.com tgt->st_mode = TheISA::htog(tgt->st_mode); 5113277Sgblack@eecs.umich.edu tgt->st_nlink = host->st_nlink; 5128737Skoansin.tan@gmail.com tgt->st_nlink = TheISA::htog(tgt->st_nlink); 5133277Sgblack@eecs.umich.edu tgt->st_uid = host->st_uid; 5148737Skoansin.tan@gmail.com tgt->st_uid = TheISA::htog(tgt->st_uid); 5153277Sgblack@eecs.umich.edu tgt->st_gid = host->st_gid; 5168737Skoansin.tan@gmail.com tgt->st_gid = TheISA::htog(tgt->st_gid); 5173113Sgblack@eecs.umich.edu if (fakeTTY) 5183113Sgblack@eecs.umich.edu tgt->st_rdev = 0x880d; 5193113Sgblack@eecs.umich.edu else 5203113Sgblack@eecs.umich.edu tgt->st_rdev = host->st_rdev; 5218737Skoansin.tan@gmail.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 5223113Sgblack@eecs.umich.edu tgt->st_size = host->st_size; 5238737Skoansin.tan@gmail.com tgt->st_size = TheISA::htog(tgt->st_size); 5243114Sgblack@eecs.umich.edu tgt->st_atimeX = host->st_atime; 5258737Skoansin.tan@gmail.com tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 5263114Sgblack@eecs.umich.edu tgt->st_mtimeX = host->st_mtime; 5278737Skoansin.tan@gmail.com tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 5283114Sgblack@eecs.umich.edu tgt->st_ctimeX = host->st_ctime; 5298737Skoansin.tan@gmail.com tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 53011906SBrandon.Potter@amd.com // Force the block size to be 8KB. This helps to ensure buffered io works 5314061Sgblack@eecs.umich.edu // consistently across different hosts. 5324061Sgblack@eecs.umich.edu tgt->st_blksize = 0x2000; 5338737Skoansin.tan@gmail.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 5343113Sgblack@eecs.umich.edu tgt->st_blocks = host->st_blocks; 5358737Skoansin.tan@gmail.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 5363113Sgblack@eecs.umich.edu} 5373113Sgblack@eecs.umich.edu 5383113Sgblack@eecs.umich.edu// Same for stat64 5393113Sgblack@eecs.umich.edu 5403113Sgblack@eecs.umich.edutemplate <typename target_stat, typename host_stat64> 54112032Sandreas.sandberg@arm.comvoid 5423113Sgblack@eecs.umich.educonvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 5433113Sgblack@eecs.umich.edu{ 5444189Sgblack@eecs.umich.edu using namespace TheISA; 5454189Sgblack@eecs.umich.edu 5463113Sgblack@eecs.umich.edu convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 5473113Sgblack@eecs.umich.edu#if defined(STAT_HAVE_NSEC) 5483113Sgblack@eecs.umich.edu tgt->st_atime_nsec = host->st_atime_nsec; 5498737Skoansin.tan@gmail.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 5503113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = host->st_mtime_nsec; 5518737Skoansin.tan@gmail.com tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 5523113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = host->st_ctime_nsec; 5538737Skoansin.tan@gmail.com tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 5543113Sgblack@eecs.umich.edu#else 5553113Sgblack@eecs.umich.edu tgt->st_atime_nsec = 0; 5563113Sgblack@eecs.umich.edu tgt->st_mtime_nsec = 0; 5573113Sgblack@eecs.umich.edu tgt->st_ctime_nsec = 0; 5583113Sgblack@eecs.umich.edu#endif 5593113Sgblack@eecs.umich.edu} 5603113Sgblack@eecs.umich.edu 56111906SBrandon.Potter@amd.com// Here are a couple of convenience functions 5623113Sgblack@eecs.umich.edutemplate<class OS> 56312032Sandreas.sandberg@arm.comvoid 5648852Sandreas.hansson@arm.comcopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 56511906SBrandon.Potter@amd.com hst_stat *host, bool fakeTTY = false) 5663113Sgblack@eecs.umich.edu{ 5673113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5683113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5693113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5703113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5713113Sgblack@eecs.umich.edu} 5723113Sgblack@eecs.umich.edu 5733113Sgblack@eecs.umich.edutemplate<class OS> 57412032Sandreas.sandberg@arm.comvoid 5758852Sandreas.hansson@arm.comcopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 57611906SBrandon.Potter@amd.com hst_stat64 *host, bool fakeTTY = false) 5773113Sgblack@eecs.umich.edu{ 5783113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5793113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5806686Stjones1@inf.ed.ac.uk convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5813113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5823113Sgblack@eecs.umich.edu} 5833113Sgblack@eecs.umich.edu 58411759Sbrandon.potter@amd.comtemplate <class OS> 58512032Sandreas.sandberg@arm.comvoid 58611759Sbrandon.potter@amd.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 58711759Sbrandon.potter@amd.com hst_statfs *host) 58811759Sbrandon.potter@amd.com{ 58911759Sbrandon.potter@amd.com TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 59011759Sbrandon.potter@amd.com 59111812Sbaz21@cam.ac.uk tgt->f_type = TheISA::htog(host->f_type); 59211812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 59311812Sbaz21@cam.ac.uk tgt->f_bsize = TheISA::htog(host->f_iosize); 59411759Sbrandon.potter@amd.com#else 59511812Sbaz21@cam.ac.uk tgt->f_bsize = TheISA::htog(host->f_bsize); 59611759Sbrandon.potter@amd.com#endif 59711759Sbrandon.potter@amd.com tgt->f_blocks = TheISA::htog(host->f_blocks); 59811759Sbrandon.potter@amd.com tgt->f_bfree = TheISA::htog(host->f_bfree); 59911759Sbrandon.potter@amd.com tgt->f_bavail = TheISA::htog(host->f_bavail); 60011759Sbrandon.potter@amd.com tgt->f_files = TheISA::htog(host->f_files); 60111759Sbrandon.potter@amd.com tgt->f_ffree = TheISA::htog(host->f_ffree); 60211759Sbrandon.potter@amd.com memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 60311812Sbaz21@cam.ac.uk#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 60411812Sbaz21@cam.ac.uk tgt->f_namelen = TheISA::htog(host->f_namemax); 60511812Sbaz21@cam.ac.uk tgt->f_frsize = TheISA::htog(host->f_bsize); 60611812Sbaz21@cam.ac.uk#elif defined(__APPLE__) 60711812Sbaz21@cam.ac.uk tgt->f_namelen = 0; 60811812Sbaz21@cam.ac.uk tgt->f_frsize = 0; 60911812Sbaz21@cam.ac.uk#else 61011759Sbrandon.potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 61111759Sbrandon.potter@amd.com tgt->f_frsize = TheISA::htog(host->f_frsize); 61211812Sbaz21@cam.ac.uk#endif 61311812Sbaz21@cam.ac.uk#if defined(__linux__) 61411759Sbrandon.potter@amd.com memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 61511812Sbaz21@cam.ac.uk#else 61611812Sbaz21@cam.ac.uk /* 61711812Sbaz21@cam.ac.uk * The fields are different sizes per OS. Don't bother with 61811812Sbaz21@cam.ac.uk * f_spare or f_reserved on non-Linux for now. 61911812Sbaz21@cam.ac.uk */ 62011812Sbaz21@cam.ac.uk memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 62111812Sbaz21@cam.ac.uk#endif 62211759Sbrandon.potter@amd.com 62311759Sbrandon.potter@amd.com tgt.copyOut(mem); 62411759Sbrandon.potter@amd.com} 62511759Sbrandon.potter@amd.com 626378SN/A/// Target ioctl() handler. For the most part, programs call ioctl() 627378SN/A/// only to find out if their stdout is a tty, to determine whether to 6289141Smarc.orr@gmail.com/// do line or block buffering. We always claim that output fds are 6299141Smarc.orr@gmail.com/// not TTYs to provide repeatable results. 630360SN/Atemplate <class OS> 6311450SN/ASyscallReturn 63211856Sbrandon.potter@amd.comioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 633360SN/A{ 6346701Sgblack@eecs.umich.edu int index = 0; 63511856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 63611856Sbrandon.potter@amd.com unsigned req = p->getSyscallArg(tc, index); 637360SN/A 63810930Sbrandon.potter@amd.com DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 639360SN/A 64011856Sbrandon.potter@amd.com if (OS::isTtyReq(req)) 64111856Sbrandon.potter@amd.com return -ENOTTY; 64210496Ssteve.reinhardt@amd.com 64311856Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 64411856Sbrandon.potter@amd.com if (!dfdp) 6451458SN/A return -EBADF; 646360SN/A 64711856Sbrandon.potter@amd.com /** 64811856Sbrandon.potter@amd.com * If the driver is valid, issue the ioctl through it. Otherwise, 64911856Sbrandon.potter@amd.com * there's an implicit assumption that the device is a TTY type and we 65011856Sbrandon.potter@amd.com * return that we do not have a valid TTY. 65111856Sbrandon.potter@amd.com */ 65211856Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 65311856Sbrandon.potter@amd.com if (emul_driver) 65411856Sbrandon.potter@amd.com return emul_driver->ioctl(p, tc, req); 65510496Ssteve.reinhardt@amd.com 65611856Sbrandon.potter@amd.com /** 65711856Sbrandon.potter@amd.com * For lack of a better return code, return ENOTTY. Ideally, we should 65811856Sbrandon.potter@amd.com * return something better here, but at least we issue the warning. 65911856Sbrandon.potter@amd.com */ 66011856Sbrandon.potter@amd.com warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 66110930Sbrandon.potter@amd.com tgt_fd, req, tc->pcState()); 6629141Smarc.orr@gmail.com return -ENOTTY; 663360SN/A} 664360SN/A 665360SN/Atemplate <class OS> 66611907SBrandon.Potter@amd.comSyscallReturn 66711907SBrandon.Potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 66811907SBrandon.Potter@amd.com bool isopenat) 669360SN/A{ 67011907SBrandon.Potter@amd.com int index = 0; 67111907SBrandon.Potter@amd.com int tgt_dirfd = -1; 67211907SBrandon.Potter@amd.com 67311907SBrandon.Potter@amd.com /** 67411907SBrandon.Potter@amd.com * If using the openat variant, read in the target directory file 67511907SBrandon.Potter@amd.com * descriptor from the simulated process. 67611907SBrandon.Potter@amd.com */ 67711907SBrandon.Potter@amd.com if (isopenat) 67811907SBrandon.Potter@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 67911907SBrandon.Potter@amd.com 68011907SBrandon.Potter@amd.com /** 68111907SBrandon.Potter@amd.com * Retrieve the simulated process' memory proxy and then read in the path 68211907SBrandon.Potter@amd.com * string from that memory space into the host's working memory space. 68311907SBrandon.Potter@amd.com */ 684360SN/A std::string path; 68511907SBrandon.Potter@amd.com if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 6861458SN/A return -EFAULT; 687360SN/A 68811907SBrandon.Potter@amd.com#ifdef __CYGWIN32__ 68911907SBrandon.Potter@amd.com int host_flags = O_BINARY; 69011907SBrandon.Potter@amd.com#else 69111907SBrandon.Potter@amd.com int host_flags = 0; 69211907SBrandon.Potter@amd.com#endif 69311907SBrandon.Potter@amd.com /** 69411907SBrandon.Potter@amd.com * Translate target flags into host flags. Flags exist which are not 69511907SBrandon.Potter@amd.com * ported between architectures which can cause check failures. 69611907SBrandon.Potter@amd.com */ 69711907SBrandon.Potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 698360SN/A for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 69911907SBrandon.Potter@amd.com if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 70011907SBrandon.Potter@amd.com tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 70111907SBrandon.Potter@amd.com host_flags |= OS::openFlagTable[i].hostFlag; 702360SN/A } 703360SN/A } 70411907SBrandon.Potter@amd.com if (tgt_flags) { 70511907SBrandon.Potter@amd.com warn("open%s: cannot decode flags 0x%x", 70611907SBrandon.Potter@amd.com isopenat ? "at" : "", tgt_flags); 70711907SBrandon.Potter@amd.com } 708360SN/A#ifdef __CYGWIN32__ 70911907SBrandon.Potter@amd.com host_flags |= O_BINARY; 710360SN/A#endif 711360SN/A 71211907SBrandon.Potter@amd.com int mode = p->getSyscallArg(tc, index); 7133669Sbinkertn@umich.edu 71411907SBrandon.Potter@amd.com /** 71511907SBrandon.Potter@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 71611907SBrandon.Potter@amd.com * take the current working directory value which was passed into the 71711907SBrandon.Potter@amd.com * process class as a Python parameter and append the current path to 71811907SBrandon.Potter@amd.com * create a full path. 71911907SBrandon.Potter@amd.com * Otherwise, openat with a valid target directory file descriptor has 72011907SBrandon.Potter@amd.com * been called. If the path option, which was passed in as a parameter, 72111907SBrandon.Potter@amd.com * is not absolute, retrieve the directory file descriptor's path and 72211907SBrandon.Potter@amd.com * prepend it to the path passed in as a parameter. 72311907SBrandon.Potter@amd.com * In every case, we should have a full path (which is relevant to the 72411907SBrandon.Potter@amd.com * host) to work with after this block has been passed. 72511907SBrandon.Potter@amd.com */ 72611907SBrandon.Potter@amd.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 72711907SBrandon.Potter@amd.com path = p->fullPath(path); 72811907SBrandon.Potter@amd.com } else if (!startswith(path, "/")) { 72911907SBrandon.Potter@amd.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 73011907SBrandon.Potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 73111907SBrandon.Potter@amd.com if (!ffdp) 73211907SBrandon.Potter@amd.com return -EBADF; 73313371Sciro.santilli@arm.com path.insert(0, ffdp->getFileName() + "/"); 73411907SBrandon.Potter@amd.com } 7351706SN/A 73611907SBrandon.Potter@amd.com /** 73711907SBrandon.Potter@amd.com * Since this is an emulated environment, we create pseudo file 73811907SBrandon.Potter@amd.com * descriptors for device requests that have been registered with 73911907SBrandon.Potter@amd.com * the process class through Python; this allows us to create a file 74011907SBrandon.Potter@amd.com * descriptor for subsequent ioctl or mmap calls. 74111907SBrandon.Potter@amd.com */ 74210496Ssteve.reinhardt@amd.com if (startswith(path, "/dev/")) { 74310496Ssteve.reinhardt@amd.com std::string filename = path.substr(strlen("/dev/")); 74411907SBrandon.Potter@amd.com EmulatedDriver *drv = p->findDriver(filename); 74511907SBrandon.Potter@amd.com if (drv) { 74611907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 74711907SBrandon.Potter@amd.com "driver open with path[%s]\n", 74811907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 74911907SBrandon.Potter@amd.com return drv->open(p, tc, mode, host_flags); 75010496Ssteve.reinhardt@amd.com } 75111907SBrandon.Potter@amd.com /** 75211907SBrandon.Potter@amd.com * Fall through here for pass through to host devices, such 75311907SBrandon.Potter@amd.com * as /dev/zero 75411907SBrandon.Potter@amd.com */ 75510496Ssteve.reinhardt@amd.com } 75610496Ssteve.reinhardt@amd.com 75711907SBrandon.Potter@amd.com /** 75811907SBrandon.Potter@amd.com * Some special paths and files cannot be called on the host and need 75911907SBrandon.Potter@amd.com * to be handled as special cases inside the simulator. 76011907SBrandon.Potter@amd.com * If the full path that was created above does not match any of the 76111907SBrandon.Potter@amd.com * special cases, pass it through to the open call on the host to let 76211907SBrandon.Potter@amd.com * the host open the file on our behalf. 76311907SBrandon.Potter@amd.com * If the host cannot open the file, return the host's error code back 76411907SBrandon.Potter@amd.com * through the system call to the simulated process. 76511907SBrandon.Potter@amd.com */ 76611907SBrandon.Potter@amd.com int sim_fd = -1; 76711907SBrandon.Potter@amd.com std::vector<std::string> special_paths = 76811907SBrandon.Potter@amd.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 76911907SBrandon.Potter@amd.com for (auto entry : special_paths) { 77011907SBrandon.Potter@amd.com if (startswith(path, entry)) 77111907SBrandon.Potter@amd.com sim_fd = OS::openSpecialFile(path, p, tc); 77211907SBrandon.Potter@amd.com } 77311907SBrandon.Potter@amd.com if (sim_fd == -1) { 77411907SBrandon.Potter@amd.com sim_fd = open(path.c_str(), host_flags, mode); 77511907SBrandon.Potter@amd.com } 77611907SBrandon.Potter@amd.com if (sim_fd == -1) { 77711907SBrandon.Potter@amd.com int local = -errno; 77811907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 77911907SBrandon.Potter@amd.com isopenat ? "at" : "", path.c_str()); 78011907SBrandon.Potter@amd.com return local; 78111907SBrandon.Potter@amd.com } 782360SN/A 78311907SBrandon.Potter@amd.com /** 78411907SBrandon.Potter@amd.com * The file was opened successfully and needs to be recorded in the 78511907SBrandon.Potter@amd.com * process' file descriptor array so that it can be retrieved later. 78611907SBrandon.Potter@amd.com * The target file descriptor that is chosen will be the lowest unused 78711907SBrandon.Potter@amd.com * file descriptor. 78811907SBrandon.Potter@amd.com * Return the indirect target file descriptor back to the simulated 78911907SBrandon.Potter@amd.com * process to act as a handle for the opened file. 79011907SBrandon.Potter@amd.com */ 79111907SBrandon.Potter@amd.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 79211907SBrandon.Potter@amd.com int tgt_fd = p->fds->allocFD(ffdp); 79311907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 79411907SBrandon.Potter@amd.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 79511907SBrandon.Potter@amd.com return tgt_fd; 796360SN/A} 797360SN/A 79810027SChris.Adeniyi-Jones@arm.com/// Target open() handler. 79910027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 80010027SChris.Adeniyi-Jones@arm.comSyscallReturn 80111851Sbrandon.potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 80210027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 80310027SChris.Adeniyi-Jones@arm.com{ 80411907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, false); 80510027SChris.Adeniyi-Jones@arm.com} 80610027SChris.Adeniyi-Jones@arm.com 80710027SChris.Adeniyi-Jones@arm.com/// Target openat() handler. 80810027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 80910027SChris.Adeniyi-Jones@arm.comSyscallReturn 81011851Sbrandon.potter@amd.comopenatFunc(SyscallDesc *desc, int callnum, Process *process, 81111851Sbrandon.potter@amd.com ThreadContext *tc) 81210027SChris.Adeniyi-Jones@arm.com{ 81311907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, true); 81410027SChris.Adeniyi-Jones@arm.com} 81510027SChris.Adeniyi-Jones@arm.com 81610633Smichaelupton@gmail.com/// Target unlinkat() handler. 81710633Smichaelupton@gmail.comtemplate <class OS> 81810633Smichaelupton@gmail.comSyscallReturn 81911851Sbrandon.potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 82010633Smichaelupton@gmail.com ThreadContext *tc) 82110633Smichaelupton@gmail.com{ 82210633Smichaelupton@gmail.com int index = 0; 82310633Smichaelupton@gmail.com int dirfd = process->getSyscallArg(tc, index); 82410633Smichaelupton@gmail.com if (dirfd != OS::TGT_AT_FDCWD) 82510633Smichaelupton@gmail.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 82610633Smichaelupton@gmail.com 82710633Smichaelupton@gmail.com return unlinkHelper(desc, callnum, process, tc, 1); 82810633Smichaelupton@gmail.com} 82910633Smichaelupton@gmail.com 83010203SAli.Saidi@ARM.com/// Target facessat() handler 83110203SAli.Saidi@ARM.comtemplate <class OS> 83210203SAli.Saidi@ARM.comSyscallReturn 83311851Sbrandon.potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 83411851Sbrandon.potter@amd.com ThreadContext *tc) 83510203SAli.Saidi@ARM.com{ 83610203SAli.Saidi@ARM.com int index = 0; 83710203SAli.Saidi@ARM.com int dirfd = process->getSyscallArg(tc, index); 83810203SAli.Saidi@ARM.com if (dirfd != OS::TGT_AT_FDCWD) 83910203SAli.Saidi@ARM.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 84010203SAli.Saidi@ARM.com return accessFunc(desc, callnum, process, tc, 1); 84110203SAli.Saidi@ARM.com} 84210203SAli.Saidi@ARM.com 84310203SAli.Saidi@ARM.com/// Target readlinkat() handler 84410203SAli.Saidi@ARM.comtemplate <class OS> 84510203SAli.Saidi@ARM.comSyscallReturn 84611851Sbrandon.potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 84711851Sbrandon.potter@amd.com ThreadContext *tc) 84810203SAli.Saidi@ARM.com{ 84910203SAli.Saidi@ARM.com int index = 0; 85010203SAli.Saidi@ARM.com int dirfd = process->getSyscallArg(tc, index); 85110203SAli.Saidi@ARM.com if (dirfd != OS::TGT_AT_FDCWD) 85210203SAli.Saidi@ARM.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 85310203SAli.Saidi@ARM.com return readlinkFunc(desc, callnum, process, tc, 1); 85410203SAli.Saidi@ARM.com} 85510203SAli.Saidi@ARM.com 85610850SGiacomo.Gabrielli@arm.com/// Target renameat() handler. 85710850SGiacomo.Gabrielli@arm.comtemplate <class OS> 85810850SGiacomo.Gabrielli@arm.comSyscallReturn 85911851Sbrandon.potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process, 86010850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 86110850SGiacomo.Gabrielli@arm.com{ 86210850SGiacomo.Gabrielli@arm.com int index = 0; 86310850SGiacomo.Gabrielli@arm.com 86410850SGiacomo.Gabrielli@arm.com int olddirfd = process->getSyscallArg(tc, index); 86510850SGiacomo.Gabrielli@arm.com if (olddirfd != OS::TGT_AT_FDCWD) 86610850SGiacomo.Gabrielli@arm.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 86710850SGiacomo.Gabrielli@arm.com 86810850SGiacomo.Gabrielli@arm.com std::string old_name; 86910850SGiacomo.Gabrielli@arm.com 87010850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(old_name, 87110850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 87210850SGiacomo.Gabrielli@arm.com return -EFAULT; 87310850SGiacomo.Gabrielli@arm.com 87410850SGiacomo.Gabrielli@arm.com int newdirfd = process->getSyscallArg(tc, index); 87510850SGiacomo.Gabrielli@arm.com if (newdirfd != OS::TGT_AT_FDCWD) 87610850SGiacomo.Gabrielli@arm.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 87710850SGiacomo.Gabrielli@arm.com 87810850SGiacomo.Gabrielli@arm.com std::string new_name; 87910850SGiacomo.Gabrielli@arm.com 88010850SGiacomo.Gabrielli@arm.com if (!tc->getMemProxy().tryReadString(new_name, 88110850SGiacomo.Gabrielli@arm.com process->getSyscallArg(tc, index))) 88210850SGiacomo.Gabrielli@arm.com return -EFAULT; 88310850SGiacomo.Gabrielli@arm.com 88410850SGiacomo.Gabrielli@arm.com // Adjust path for current working directory 88510850SGiacomo.Gabrielli@arm.com old_name = process->fullPath(old_name); 88610850SGiacomo.Gabrielli@arm.com new_name = process->fullPath(new_name); 88710850SGiacomo.Gabrielli@arm.com 88810850SGiacomo.Gabrielli@arm.com int result = rename(old_name.c_str(), new_name.c_str()); 88910850SGiacomo.Gabrielli@arm.com return (result == -1) ? -errno : result; 89010850SGiacomo.Gabrielli@arm.com} 89110850SGiacomo.Gabrielli@arm.com 8926640Svince@csl.cornell.edu/// Target sysinfo() handler. 8936640Svince@csl.cornell.edutemplate <class OS> 8946640Svince@csl.cornell.eduSyscallReturn 89511851Sbrandon.potter@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 89611851Sbrandon.potter@amd.com ThreadContext *tc) 8976640Svince@csl.cornell.edu{ 8986640Svince@csl.cornell.edu 8996701Sgblack@eecs.umich.edu int index = 0; 9006701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tgt_sysinfo> 90110793Sbrandon.potter@amd.com sysinfo(process->getSyscallArg(tc, index)); 9026640Svince@csl.cornell.edu 90311758Sbrandon.potter@amd.com sysinfo->uptime = seconds_since_epoch; 90411758Sbrandon.potter@amd.com sysinfo->totalram = process->system->memSize(); 90511758Sbrandon.potter@amd.com sysinfo->mem_unit = 1; 9066640Svince@csl.cornell.edu 9078706Sandreas.hansson@arm.com sysinfo.copyOut(tc->getMemProxy()); 9086640Svince@csl.cornell.edu 9096701Sgblack@eecs.umich.edu return 0; 9106640Svince@csl.cornell.edu} 911360SN/A 9121999SN/A/// Target chmod() handler. 9131999SN/Atemplate <class OS> 9141999SN/ASyscallReturn 91511851Sbrandon.potter@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process, 9162680Sktlim@umich.edu ThreadContext *tc) 9171999SN/A{ 9181999SN/A std::string path; 9191999SN/A 9206701Sgblack@eecs.umich.edu int index = 0; 9218852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 9226701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 9231999SN/A return -EFAULT; 9246701Sgblack@eecs.umich.edu } 9251999SN/A 9266701Sgblack@eecs.umich.edu uint32_t mode = process->getSyscallArg(tc, index); 9271999SN/A mode_t hostMode = 0; 9281999SN/A 9291999SN/A // XXX translate mode flags via OS::something??? 9301999SN/A hostMode = mode; 9311999SN/A 9323669Sbinkertn@umich.edu // Adjust path for current working directory 9333669Sbinkertn@umich.edu path = process->fullPath(path); 9343669Sbinkertn@umich.edu 9351999SN/A // do the chmod 9361999SN/A int result = chmod(path.c_str(), hostMode); 9371999SN/A if (result < 0) 9382218SN/A return -errno; 9391999SN/A 9401999SN/A return 0; 9411999SN/A} 9421999SN/A 94313570Sbrandon.potter@amd.comtemplate <class OS> 94413570Sbrandon.potter@amd.comSyscallReturn 94513570Sbrandon.potter@amd.compollFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 94613570Sbrandon.potter@amd.com{ 94713570Sbrandon.potter@amd.com int index = 0; 94813570Sbrandon.potter@amd.com Addr fdsPtr = p->getSyscallArg(tc, index); 94913570Sbrandon.potter@amd.com int nfds = p->getSyscallArg(tc, index); 95013570Sbrandon.potter@amd.com int tmout = p->getSyscallArg(tc, index); 95113570Sbrandon.potter@amd.com 95213570Sbrandon.potter@amd.com BufferArg fdsBuf(fdsPtr, sizeof(struct pollfd) * nfds); 95313570Sbrandon.potter@amd.com fdsBuf.copyIn(tc->getMemProxy()); 95413570Sbrandon.potter@amd.com 95513570Sbrandon.potter@amd.com /** 95613570Sbrandon.potter@amd.com * Record the target file descriptors in a local variable. We need to 95713570Sbrandon.potter@amd.com * replace them with host file descriptors but we need a temporary copy 95813570Sbrandon.potter@amd.com * for later. Afterwards, replace each target file descriptor in the 95913570Sbrandon.potter@amd.com * poll_fd array with its host_fd. 96013570Sbrandon.potter@amd.com */ 96113570Sbrandon.potter@amd.com int temp_tgt_fds[nfds]; 96213570Sbrandon.potter@amd.com for (index = 0; index < nfds; index++) { 96313570Sbrandon.potter@amd.com temp_tgt_fds[index] = ((struct pollfd *)fdsBuf.bufferPtr())[index].fd; 96413570Sbrandon.potter@amd.com auto tgt_fd = temp_tgt_fds[index]; 96513570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 96613570Sbrandon.potter@amd.com if (!hbfdp) 96713570Sbrandon.potter@amd.com return -EBADF; 96813570Sbrandon.potter@amd.com auto host_fd = hbfdp->getSimFD(); 96913570Sbrandon.potter@amd.com ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = host_fd; 97013570Sbrandon.potter@amd.com } 97113570Sbrandon.potter@amd.com 97213570Sbrandon.potter@amd.com /** 97313570Sbrandon.potter@amd.com * We cannot allow an infinite poll to occur or it will inevitably cause 97413570Sbrandon.potter@amd.com * a deadlock in the gem5 simulator with clone. We must pass in tmout with 97513570Sbrandon.potter@amd.com * a non-negative value, however it also makes no sense to poll on the 97613570Sbrandon.potter@amd.com * underlying host for any other time than tmout a zero timeout. 97713570Sbrandon.potter@amd.com */ 97813570Sbrandon.potter@amd.com int status; 97913570Sbrandon.potter@amd.com if (tmout < 0) { 98013570Sbrandon.potter@amd.com status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 98113570Sbrandon.potter@amd.com if (status == 0) { 98213570Sbrandon.potter@amd.com /** 98313570Sbrandon.potter@amd.com * If blocking indefinitely, check the signal list to see if a 98413570Sbrandon.potter@amd.com * signal would break the poll out of the retry cycle and try 98513570Sbrandon.potter@amd.com * to return the signal interrupt instead. 98613570Sbrandon.potter@amd.com */ 98713570Sbrandon.potter@amd.com System *sysh = tc->getSystemPtr(); 98813570Sbrandon.potter@amd.com std::list<BasicSignal>::iterator it; 98913570Sbrandon.potter@amd.com for (it=sysh->signalList.begin(); it!=sysh->signalList.end(); it++) 99013570Sbrandon.potter@amd.com if (it->receiver == p) 99113570Sbrandon.potter@amd.com return -EINTR; 99213570Sbrandon.potter@amd.com return SyscallReturn::retry(); 99313570Sbrandon.potter@amd.com } 99413570Sbrandon.potter@amd.com } else 99513570Sbrandon.potter@amd.com status = poll((struct pollfd *)fdsBuf.bufferPtr(), nfds, 0); 99613570Sbrandon.potter@amd.com 99713570Sbrandon.potter@amd.com if (status == -1) 99813570Sbrandon.potter@amd.com return -errno; 99913570Sbrandon.potter@amd.com 100013570Sbrandon.potter@amd.com /** 100113570Sbrandon.potter@amd.com * Replace each host_fd in the returned poll_fd array with its original 100213570Sbrandon.potter@amd.com * target file descriptor. 100313570Sbrandon.potter@amd.com */ 100413570Sbrandon.potter@amd.com for (index = 0; index < nfds; index++) { 100513570Sbrandon.potter@amd.com auto tgt_fd = temp_tgt_fds[index]; 100613570Sbrandon.potter@amd.com ((struct pollfd *)fdsBuf.bufferPtr())[index].fd = tgt_fd; 100713570Sbrandon.potter@amd.com } 100813570Sbrandon.potter@amd.com 100913570Sbrandon.potter@amd.com /** 101013570Sbrandon.potter@amd.com * Copy out the pollfd struct because the host may have updated fields 101113570Sbrandon.potter@amd.com * in the structure. 101213570Sbrandon.potter@amd.com */ 101313570Sbrandon.potter@amd.com fdsBuf.copyOut(tc->getMemProxy()); 101413570Sbrandon.potter@amd.com 101513570Sbrandon.potter@amd.com return status; 101613570Sbrandon.potter@amd.com} 10171999SN/A 10181999SN/A/// Target fchmod() handler. 10191999SN/Atemplate <class OS> 10201999SN/ASyscallReturn 102111856Sbrandon.potter@amd.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 10221999SN/A{ 10236701Sgblack@eecs.umich.edu int index = 0; 102411856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 102511856Sbrandon.potter@amd.com uint32_t mode = p->getSyscallArg(tc, index); 102610931Sbrandon.potter@amd.com 102711856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 102811856Sbrandon.potter@amd.com if (!ffdp) 10291999SN/A return -EBADF; 103011856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 10311999SN/A 103211856Sbrandon.potter@amd.com mode_t hostMode = mode; 10331999SN/A 103411856Sbrandon.potter@amd.com int result = fchmod(sim_fd, hostMode); 10351999SN/A 103611856Sbrandon.potter@amd.com return (result < 0) ? -errno : 0; 10371999SN/A} 10381999SN/A 10395877Shsul@eecs.umich.edu/// Target mremap() handler. 10405877Shsul@eecs.umich.edutemplate <class OS> 10415877Shsul@eecs.umich.eduSyscallReturn 104211851Sbrandon.potter@amd.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 10435877Shsul@eecs.umich.edu{ 10446701Sgblack@eecs.umich.edu int index = 0; 10456701Sgblack@eecs.umich.edu Addr start = process->getSyscallArg(tc, index); 10466701Sgblack@eecs.umich.edu uint64_t old_length = process->getSyscallArg(tc, index); 10476701Sgblack@eecs.umich.edu uint64_t new_length = process->getSyscallArg(tc, index); 10486701Sgblack@eecs.umich.edu uint64_t flags = process->getSyscallArg(tc, index); 104910027SChris.Adeniyi-Jones@arm.com uint64_t provided_address = 0; 105010027SChris.Adeniyi-Jones@arm.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 105110027SChris.Adeniyi-Jones@arm.com 105210027SChris.Adeniyi-Jones@arm.com if (use_provided_address) 105310027SChris.Adeniyi-Jones@arm.com provided_address = process->getSyscallArg(tc, index); 10545877Shsul@eecs.umich.edu 105510318Sandreas.hansson@arm.com if ((start % TheISA::PageBytes != 0) || 105610318Sandreas.hansson@arm.com (provided_address % TheISA::PageBytes != 0)) { 10575877Shsul@eecs.umich.edu warn("mremap failing: arguments not page aligned"); 10585877Shsul@eecs.umich.edu return -EINVAL; 10595877Shsul@eecs.umich.edu } 10605877Shsul@eecs.umich.edu 106110486Stjablin@gmail.com new_length = roundUp(new_length, TheISA::PageBytes); 106210486Stjablin@gmail.com 10635877Shsul@eecs.umich.edu if (new_length > old_length) { 106411905SBrandon.Potter@amd.com std::shared_ptr<MemState> mem_state = process->memState; 106511905SBrandon.Potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 106611905SBrandon.Potter@amd.com 106711905SBrandon.Potter@amd.com if ((start + old_length) == mmap_end && 106810027SChris.Adeniyi-Jones@arm.com (!use_provided_address || provided_address == start)) { 106912206Srico.amslinger@informatik.uni-augsburg.de // This case cannot occur when growing downward, as 107012206Srico.amslinger@informatik.uni-augsburg.de // start is greater than or equal to mmap_end. 10715877Shsul@eecs.umich.edu uint64_t diff = new_length - old_length; 107211905SBrandon.Potter@amd.com process->allocateMem(mmap_end, diff); 107311905SBrandon.Potter@amd.com mem_state->setMmapEnd(mmap_end + diff); 10745877Shsul@eecs.umich.edu return start; 10755877Shsul@eecs.umich.edu } else { 107610027SChris.Adeniyi-Jones@arm.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 10775877Shsul@eecs.umich.edu warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 10785877Shsul@eecs.umich.edu return -ENOMEM; 10795877Shsul@eecs.umich.edu } else { 108012206Srico.amslinger@informatik.uni-augsburg.de uint64_t new_start = provided_address; 108112206Srico.amslinger@informatik.uni-augsburg.de if (!use_provided_address) { 108212206Srico.amslinger@informatik.uni-augsburg.de new_start = process->mmapGrowsDown() ? 108312206Srico.amslinger@informatik.uni-augsburg.de mmap_end - new_length : mmap_end; 108412206Srico.amslinger@informatik.uni-augsburg.de mmap_end = process->mmapGrowsDown() ? 108512206Srico.amslinger@informatik.uni-augsburg.de new_start : mmap_end + new_length; 108612206Srico.amslinger@informatik.uni-augsburg.de mem_state->setMmapEnd(mmap_end); 108712206Srico.amslinger@informatik.uni-augsburg.de } 108812206Srico.amslinger@informatik.uni-augsburg.de 108910027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, old_length, new_start); 109010027SChris.Adeniyi-Jones@arm.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 109110027SChris.Adeniyi-Jones@arm.com new_start, new_start + new_length, 109210027SChris.Adeniyi-Jones@arm.com new_length - old_length); 10935877Shsul@eecs.umich.edu // add on the remaining unallocated pages 109410027SChris.Adeniyi-Jones@arm.com process->allocateMem(new_start + old_length, 109510027SChris.Adeniyi-Jones@arm.com new_length - old_length, 109610027SChris.Adeniyi-Jones@arm.com use_provided_address /* clobber */); 109710027SChris.Adeniyi-Jones@arm.com if (use_provided_address && 109812206Srico.amslinger@informatik.uni-augsburg.de ((new_start + new_length > mem_state->getMmapEnd() && 109912206Srico.amslinger@informatik.uni-augsburg.de !process->mmapGrowsDown()) || 110012206Srico.amslinger@informatik.uni-augsburg.de (new_start < mem_state->getMmapEnd() && 110112206Srico.amslinger@informatik.uni-augsburg.de process->mmapGrowsDown()))) { 110210027SChris.Adeniyi-Jones@arm.com // something fishy going on here, at least notify the user 110310027SChris.Adeniyi-Jones@arm.com // @todo: increase mmap_end? 110410027SChris.Adeniyi-Jones@arm.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 110510027SChris.Adeniyi-Jones@arm.com } 110610027SChris.Adeniyi-Jones@arm.com warn("returning %08p as start\n", new_start); 110710027SChris.Adeniyi-Jones@arm.com return new_start; 11085877Shsul@eecs.umich.edu } 11095877Shsul@eecs.umich.edu } 11105877Shsul@eecs.umich.edu } else { 111110027SChris.Adeniyi-Jones@arm.com if (use_provided_address && provided_address != start) 111210027SChris.Adeniyi-Jones@arm.com process->pTable->remap(start, new_length, provided_address); 11138601Ssteve.reinhardt@amd.com process->pTable->unmap(start + new_length, old_length - new_length); 111410027SChris.Adeniyi-Jones@arm.com return use_provided_address ? provided_address : start; 11155877Shsul@eecs.umich.edu } 11165877Shsul@eecs.umich.edu} 11171999SN/A 1118378SN/A/// Target stat() handler. 1119360SN/Atemplate <class OS> 11201450SN/ASyscallReturn 112111851Sbrandon.potter@amd.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 11222680Sktlim@umich.edu ThreadContext *tc) 1123360SN/A{ 1124360SN/A std::string path; 1125360SN/A 11266701Sgblack@eecs.umich.edu int index = 0; 11278852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 11286701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 11296701Sgblack@eecs.umich.edu return -EFAULT; 11306701Sgblack@eecs.umich.edu } 11316701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1132360SN/A 11333669Sbinkertn@umich.edu // Adjust path for current working directory 11343669Sbinkertn@umich.edu path = process->fullPath(path); 11353669Sbinkertn@umich.edu 1136360SN/A struct stat hostBuf; 1137360SN/A int result = stat(path.c_str(), &hostBuf); 1138360SN/A 1139360SN/A if (result < 0) 11402218SN/A return -errno; 1141360SN/A 11428706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1143360SN/A 11441458SN/A return 0; 1145360SN/A} 1146360SN/A 1147360SN/A 11485074Ssaidi@eecs.umich.edu/// Target stat64() handler. 11495074Ssaidi@eecs.umich.edutemplate <class OS> 11505074Ssaidi@eecs.umich.eduSyscallReturn 115111851Sbrandon.potter@amd.comstat64Func(SyscallDesc *desc, int callnum, Process *process, 11525074Ssaidi@eecs.umich.edu ThreadContext *tc) 11535074Ssaidi@eecs.umich.edu{ 11545074Ssaidi@eecs.umich.edu std::string path; 11555074Ssaidi@eecs.umich.edu 11566701Sgblack@eecs.umich.edu int index = 0; 11578852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 11586701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) 11595074Ssaidi@eecs.umich.edu return -EFAULT; 11606701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11615074Ssaidi@eecs.umich.edu 11625074Ssaidi@eecs.umich.edu // Adjust path for current working directory 11635074Ssaidi@eecs.umich.edu path = process->fullPath(path); 11645074Ssaidi@eecs.umich.edu 11655208Ssaidi@eecs.umich.edu#if NO_STAT64 11665208Ssaidi@eecs.umich.edu struct stat hostBuf; 11675208Ssaidi@eecs.umich.edu int result = stat(path.c_str(), &hostBuf); 11685208Ssaidi@eecs.umich.edu#else 11695074Ssaidi@eecs.umich.edu struct stat64 hostBuf; 11705074Ssaidi@eecs.umich.edu int result = stat64(path.c_str(), &hostBuf); 11715208Ssaidi@eecs.umich.edu#endif 11725074Ssaidi@eecs.umich.edu 11735074Ssaidi@eecs.umich.edu if (result < 0) 11745074Ssaidi@eecs.umich.edu return -errno; 11755074Ssaidi@eecs.umich.edu 11768706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11775074Ssaidi@eecs.umich.edu 11785074Ssaidi@eecs.umich.edu return 0; 11795074Ssaidi@eecs.umich.edu} 11805074Ssaidi@eecs.umich.edu 11815074Ssaidi@eecs.umich.edu 118210027SChris.Adeniyi-Jones@arm.com/// Target fstatat64() handler. 118310027SChris.Adeniyi-Jones@arm.comtemplate <class OS> 118410027SChris.Adeniyi-Jones@arm.comSyscallReturn 118511851Sbrandon.potter@amd.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process, 118610027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 118710027SChris.Adeniyi-Jones@arm.com{ 118810027SChris.Adeniyi-Jones@arm.com int index = 0; 118910027SChris.Adeniyi-Jones@arm.com int dirfd = process->getSyscallArg(tc, index); 119010027SChris.Adeniyi-Jones@arm.com if (dirfd != OS::TGT_AT_FDCWD) 119110793Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 119210027SChris.Adeniyi-Jones@arm.com 119310027SChris.Adeniyi-Jones@arm.com std::string path; 119410027SChris.Adeniyi-Jones@arm.com if (!tc->getMemProxy().tryReadString(path, 119510027SChris.Adeniyi-Jones@arm.com process->getSyscallArg(tc, index))) 119610027SChris.Adeniyi-Jones@arm.com return -EFAULT; 119710027SChris.Adeniyi-Jones@arm.com Addr bufPtr = process->getSyscallArg(tc, index); 119810027SChris.Adeniyi-Jones@arm.com 119910027SChris.Adeniyi-Jones@arm.com // Adjust path for current working directory 120010027SChris.Adeniyi-Jones@arm.com path = process->fullPath(path); 120110027SChris.Adeniyi-Jones@arm.com 120210027SChris.Adeniyi-Jones@arm.com#if NO_STAT64 120310027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 120410027SChris.Adeniyi-Jones@arm.com int result = stat(path.c_str(), &hostBuf); 120510027SChris.Adeniyi-Jones@arm.com#else 120610027SChris.Adeniyi-Jones@arm.com struct stat64 hostBuf; 120710027SChris.Adeniyi-Jones@arm.com int result = stat64(path.c_str(), &hostBuf); 120810027SChris.Adeniyi-Jones@arm.com#endif 120910027SChris.Adeniyi-Jones@arm.com 121010027SChris.Adeniyi-Jones@arm.com if (result < 0) 121110027SChris.Adeniyi-Jones@arm.com return -errno; 121210027SChris.Adeniyi-Jones@arm.com 121310027SChris.Adeniyi-Jones@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 121410027SChris.Adeniyi-Jones@arm.com 121510027SChris.Adeniyi-Jones@arm.com return 0; 121610027SChris.Adeniyi-Jones@arm.com} 121710027SChris.Adeniyi-Jones@arm.com 121810027SChris.Adeniyi-Jones@arm.com 12191999SN/A/// Target fstat64() handler. 12201999SN/Atemplate <class OS> 12211999SN/ASyscallReturn 122211856Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 12231999SN/A{ 12246701Sgblack@eecs.umich.edu int index = 0; 122511856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 122611856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 122710931Sbrandon.potter@amd.com 122811856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 122911856Sbrandon.potter@amd.com if (!ffdp) 12301999SN/A return -EBADF; 123111856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 12321999SN/A 12332764Sstever@eecs.umich.edu#if NO_STAT64 12342064SN/A struct stat hostBuf; 123510931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 12362064SN/A#else 12372064SN/A struct stat64 hostBuf; 123810931Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 12392064SN/A#endif 12401999SN/A 12411999SN/A if (result < 0) 12422218SN/A return -errno; 12431999SN/A 124410931Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 12451999SN/A 12461999SN/A return 0; 12471999SN/A} 12481999SN/A 12491999SN/A 1250378SN/A/// Target lstat() handler. 1251360SN/Atemplate <class OS> 12521450SN/ASyscallReturn 125311851Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 12542680Sktlim@umich.edu ThreadContext *tc) 1255360SN/A{ 1256360SN/A std::string path; 1257360SN/A 12586701Sgblack@eecs.umich.edu int index = 0; 12598852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 12606701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 12616701Sgblack@eecs.umich.edu return -EFAULT; 12626701Sgblack@eecs.umich.edu } 12636701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 1264360SN/A 12653669Sbinkertn@umich.edu // Adjust path for current working directory 12663669Sbinkertn@umich.edu path = process->fullPath(path); 12673669Sbinkertn@umich.edu 1268360SN/A struct stat hostBuf; 1269360SN/A int result = lstat(path.c_str(), &hostBuf); 1270360SN/A 1271360SN/A if (result < 0) 12721458SN/A return -errno; 1273360SN/A 12748706Sandreas.hansson@arm.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 1275360SN/A 12761458SN/A return 0; 1277360SN/A} 1278360SN/A 12791999SN/A/// Target lstat64() handler. 12801999SN/Atemplate <class OS> 12811999SN/ASyscallReturn 128211851Sbrandon.potter@amd.comlstat64Func(SyscallDesc *desc, int callnum, Process *process, 12832680Sktlim@umich.edu ThreadContext *tc) 12841999SN/A{ 12851999SN/A std::string path; 12861999SN/A 12876701Sgblack@eecs.umich.edu int index = 0; 12888852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 12896701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 12906701Sgblack@eecs.umich.edu return -EFAULT; 12916701Sgblack@eecs.umich.edu } 12926701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 12931999SN/A 12943669Sbinkertn@umich.edu // Adjust path for current working directory 12953669Sbinkertn@umich.edu path = process->fullPath(path); 12963669Sbinkertn@umich.edu 12972764Sstever@eecs.umich.edu#if NO_STAT64 12982064SN/A struct stat hostBuf; 12992064SN/A int result = lstat(path.c_str(), &hostBuf); 13002064SN/A#else 13011999SN/A struct stat64 hostBuf; 13021999SN/A int result = lstat64(path.c_str(), &hostBuf); 13032064SN/A#endif 13041999SN/A 13051999SN/A if (result < 0) 13061999SN/A return -errno; 13071999SN/A 13088706Sandreas.hansson@arm.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 13091999SN/A 13101999SN/A return 0; 13111999SN/A} 13121999SN/A 1313378SN/A/// Target fstat() handler. 1314360SN/Atemplate <class OS> 13151450SN/ASyscallReturn 131611856Sbrandon.potter@amd.comfstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1317360SN/A{ 13186701Sgblack@eecs.umich.edu int index = 0; 131911856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 132011856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 1321360SN/A 132211380Salexandru.dutu@amd.com DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 1323360SN/A 132411856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 132511856Sbrandon.potter@amd.com if (!ffdp) 13261458SN/A return -EBADF; 132711856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 1328360SN/A 1329360SN/A struct stat hostBuf; 133010931Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 1331360SN/A 1332360SN/A if (result < 0) 13331458SN/A return -errno; 1334360SN/A 133510931Sbrandon.potter@amd.com copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 13362021SN/A 13371458SN/A return 0; 1338360SN/A} 1339360SN/A 13401706SN/A/// Target statfs() handler. 13411706SN/Atemplate <class OS> 13421706SN/ASyscallReturn 134311851Sbrandon.potter@amd.comstatfsFunc(SyscallDesc *desc, int callnum, Process *process, 13442680Sktlim@umich.edu ThreadContext *tc) 13451706SN/A{ 134611799Sbrandon.potter@amd.com#if NO_STATFS 134711799Sbrandon.potter@amd.com warn("Host OS cannot support calls to statfs. Ignoring syscall"); 134811799Sbrandon.potter@amd.com#else 13491706SN/A std::string path; 13501706SN/A 13516701Sgblack@eecs.umich.edu int index = 0; 13528852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 13536701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 13546701Sgblack@eecs.umich.edu return -EFAULT; 13556701Sgblack@eecs.umich.edu } 13566701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 13571706SN/A 13583669Sbinkertn@umich.edu // Adjust path for current working directory 13593669Sbinkertn@umich.edu path = process->fullPath(path); 13603669Sbinkertn@umich.edu 13611706SN/A struct statfs hostBuf; 13621706SN/A int result = statfs(path.c_str(), &hostBuf); 13631706SN/A 13641706SN/A if (result < 0) 13652218SN/A return -errno; 13661706SN/A 136711759Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 136811799Sbrandon.potter@amd.com#endif 13691706SN/A return 0; 13701706SN/A} 13711706SN/A 137211886Sbrandon.potter@amd.comtemplate <class OS> 137311886Sbrandon.potter@amd.comSyscallReturn 137411886Sbrandon.potter@amd.comcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 137511886Sbrandon.potter@amd.com{ 137611886Sbrandon.potter@amd.com int index = 0; 137712426Sqtt2@cornell.edu 137813557Sgabeblack@google.com RegVal flags = p->getSyscallArg(tc, index); 137913557Sgabeblack@google.com RegVal newStack = p->getSyscallArg(tc, index); 138011886Sbrandon.potter@amd.com Addr ptidPtr = p->getSyscallArg(tc, index); 138112426Sqtt2@cornell.edu 138213534Sandreas.sandberg@arm.com#if THE_ISA == RISCV_ISA or THE_ISA == ARM_ISA 138312426Sqtt2@cornell.edu /** 138413534Sandreas.sandberg@arm.com * Linux sets CLONE_BACKWARDS flag for RISC-V and Arm. 138512426Sqtt2@cornell.edu * The flag defines the list of clone() arguments in the following 138612426Sqtt2@cornell.edu * order: flags -> newStack -> ptidPtr -> tlsPtr -> ctidPtr 138712426Sqtt2@cornell.edu */ 138813536Sandreas.sandberg@arm.com Addr tlsPtr = p->getSyscallArg(tc, index); 138912426Sqtt2@cornell.edu Addr ctidPtr = p->getSyscallArg(tc, index); 139012426Sqtt2@cornell.edu#else 139111886Sbrandon.potter@amd.com Addr ctidPtr = p->getSyscallArg(tc, index); 139213536Sandreas.sandberg@arm.com Addr tlsPtr = p->getSyscallArg(tc, index); 139312426Sqtt2@cornell.edu#endif 139411886Sbrandon.potter@amd.com 139511886Sbrandon.potter@amd.com if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 139611886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 139711886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 139811886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 139911886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 140011886Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_VM) && !(newStack))) 140111886Sbrandon.potter@amd.com return -EINVAL; 140211886Sbrandon.potter@amd.com 140311886Sbrandon.potter@amd.com ThreadContext *ctc; 140411886Sbrandon.potter@amd.com if (!(ctc = p->findFreeContext())) 140511886Sbrandon.potter@amd.com fatal("clone: no spare thread context in system"); 140611886Sbrandon.potter@amd.com 140711886Sbrandon.potter@amd.com /** 140811886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 140911886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 141011886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 141111886Sbrandon.potter@amd.com * constructor. 141211886Sbrandon.potter@amd.com */ 141311886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 141411886Sbrandon.potter@amd.com pp->executable.assign(*(new std::string(p->progName()))); 141511886Sbrandon.potter@amd.com pp->cmd.push_back(*(new std::string(p->progName()))); 141611886Sbrandon.potter@amd.com pp->system = p->system; 141711886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 141811886Sbrandon.potter@amd.com pp->input.assign("stdin"); 141911886Sbrandon.potter@amd.com pp->output.assign("stdout"); 142011886Sbrandon.potter@amd.com pp->errout.assign("stderr"); 142111886Sbrandon.potter@amd.com pp->uid = p->uid(); 142211886Sbrandon.potter@amd.com pp->euid = p->euid(); 142311886Sbrandon.potter@amd.com pp->gid = p->gid(); 142411886Sbrandon.potter@amd.com pp->egid = p->egid(); 142511886Sbrandon.potter@amd.com 142611886Sbrandon.potter@amd.com /* Find the first free PID that's less than the maximum */ 142711886Sbrandon.potter@amd.com std::set<int> const& pids = p->system->PIDs; 142811886Sbrandon.potter@amd.com int temp_pid = *pids.begin(); 142911886Sbrandon.potter@amd.com do { 143011886Sbrandon.potter@amd.com temp_pid++; 143111886Sbrandon.potter@amd.com } while (pids.find(temp_pid) != pids.end()); 143211886Sbrandon.potter@amd.com if (temp_pid >= System::maxPID) 143311886Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 143411886Sbrandon.potter@amd.com 143511886Sbrandon.potter@amd.com pp->pid = temp_pid; 143611886Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 143711886Sbrandon.potter@amd.com Process *cp = pp->create(); 143811886Sbrandon.potter@amd.com delete pp; 143911886Sbrandon.potter@amd.com 144011886Sbrandon.potter@amd.com Process *owner = ctc->getProcessPtr(); 144111886Sbrandon.potter@amd.com ctc->setProcessPtr(cp); 144211886Sbrandon.potter@amd.com cp->assignThreadContext(ctc->contextId()); 144311886Sbrandon.potter@amd.com owner->revokeThreadContext(ctc->contextId()); 144411886Sbrandon.potter@amd.com 144511886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 144611886Sbrandon.potter@amd.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 144711886Sbrandon.potter@amd.com long *ptid = (long *)ptidBuf.bufferPtr(); 144811886Sbrandon.potter@amd.com *ptid = cp->pid(); 144911886Sbrandon.potter@amd.com ptidBuf.copyOut(tc->getMemProxy()); 145011886Sbrandon.potter@amd.com } 145111886Sbrandon.potter@amd.com 145211886Sbrandon.potter@amd.com cp->initState(); 145311886Sbrandon.potter@amd.com p->clone(tc, ctc, cp, flags); 145411886Sbrandon.potter@amd.com 145511911SBrandon.Potter@amd.com if (flags & OS::TGT_CLONE_THREAD) { 145611911SBrandon.Potter@amd.com delete cp->sigchld; 145711911SBrandon.Potter@amd.com cp->sigchld = p->sigchld; 145811911SBrandon.Potter@amd.com } else if (flags & OS::TGT_SIGCHLD) { 145911911SBrandon.Potter@amd.com *cp->sigchld = true; 146011911SBrandon.Potter@amd.com } 146111911SBrandon.Potter@amd.com 146211886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 146311886Sbrandon.potter@amd.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 146411886Sbrandon.potter@amd.com long *ctid = (long *)ctidBuf.bufferPtr(); 146511886Sbrandon.potter@amd.com *ctid = cp->pid(); 146611886Sbrandon.potter@amd.com ctidBuf.copyOut(ctc->getMemProxy()); 146711886Sbrandon.potter@amd.com } 146811886Sbrandon.potter@amd.com 146911886Sbrandon.potter@amd.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 147011886Sbrandon.potter@amd.com cp->childClearTID = (uint64_t)ctidPtr; 147111886Sbrandon.potter@amd.com 147211886Sbrandon.potter@amd.com ctc->clearArchRegs(); 147311886Sbrandon.potter@amd.com 147413536Sandreas.sandberg@arm.com OS::archClone(flags, p, cp, tc, ctc, newStack, tlsPtr); 147511886Sbrandon.potter@amd.com 147611886Sbrandon.potter@amd.com cp->setSyscallReturn(ctc, 0); 147711886Sbrandon.potter@amd.com 147811886Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 147911886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 148011886Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 148111886Sbrandon.potter@amd.com tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 148211886Sbrandon.potter@amd.com ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 148311886Sbrandon.potter@amd.com#endif 148411886Sbrandon.potter@amd.com 148513535Sandreas.sandberg@arm.com TheISA::PCState cpc = tc->pcState(); 148613535Sandreas.sandberg@arm.com cpc.advance(); 148713535Sandreas.sandberg@arm.com ctc->pcState(cpc); 148811886Sbrandon.potter@amd.com ctc->activate(); 148911886Sbrandon.potter@amd.com 149011886Sbrandon.potter@amd.com return cp->pid(); 149111886Sbrandon.potter@amd.com} 14921706SN/A 14931706SN/A/// Target fstatfs() handler. 14941706SN/Atemplate <class OS> 14951706SN/ASyscallReturn 149611856Sbrandon.potter@amd.comfstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 14971706SN/A{ 14986701Sgblack@eecs.umich.edu int index = 0; 149911856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 150011856Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 15011706SN/A 150211856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 150311856Sbrandon.potter@amd.com if (!ffdp) 15041706SN/A return -EBADF; 150511856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 15061706SN/A 15071706SN/A struct statfs hostBuf; 150810931Sbrandon.potter@amd.com int result = fstatfs(sim_fd, &hostBuf); 15091706SN/A 15101706SN/A if (result < 0) 15112218SN/A return -errno; 15121706SN/A 151311759Sbrandon.potter@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 15141706SN/A 15151706SN/A return 0; 15161706SN/A} 15171706SN/A 15181706SN/A 15191999SN/A/// Target writev() handler. 15201999SN/Atemplate <class OS> 15211999SN/ASyscallReturn 152211856Sbrandon.potter@amd.comwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 15231999SN/A{ 15246701Sgblack@eecs.umich.edu int index = 0; 152511856Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 152610931Sbrandon.potter@amd.com 152711856Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 152811856Sbrandon.potter@amd.com if (!hbfdp) 15291999SN/A return -EBADF; 153011856Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 15311999SN/A 153211856Sbrandon.potter@amd.com SETranslatingPortProxy &prox = tc->getMemProxy(); 153311856Sbrandon.potter@amd.com uint64_t tiov_base = p->getSyscallArg(tc, index); 153411856Sbrandon.potter@amd.com size_t count = p->getSyscallArg(tc, index); 15351999SN/A struct iovec hiov[count]; 15366227Snate@binkert.org for (size_t i = 0; i < count; ++i) { 15371999SN/A typename OS::tgt_iovec tiov; 15382461SN/A 153911856Sbrandon.potter@amd.com prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 154011856Sbrandon.potter@amd.com (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 15418737Skoansin.tan@gmail.com hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 15421999SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 154311856Sbrandon.potter@amd.com prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 154411856Sbrandon.potter@amd.com hiov[i].iov_len); 15451999SN/A } 15461999SN/A 154710931Sbrandon.potter@amd.com int result = writev(sim_fd, hiov, count); 15481999SN/A 15496227Snate@binkert.org for (size_t i = 0; i < count; ++i) 15501999SN/A delete [] (char *)hiov[i].iov_base; 15511999SN/A 15521999SN/A if (result < 0) 15532218SN/A return -errno; 15541999SN/A 155510629Sjthestness@gmail.com return result; 15561999SN/A} 15571999SN/A 155811385Sbrandon.potter@amd.com/// Real mmap handler. 1559360SN/Atemplate <class OS> 15601450SN/ASyscallReturn 156111851Sbrandon.potter@amd.commmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 156211385Sbrandon.potter@amd.com bool is_mmap2) 1563360SN/A{ 15646701Sgblack@eecs.umich.edu int index = 0; 15656701Sgblack@eecs.umich.edu Addr start = p->getSyscallArg(tc, index); 15666701Sgblack@eecs.umich.edu uint64_t length = p->getSyscallArg(tc, index); 156711383Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 156811383Sbrandon.potter@amd.com int tgt_flags = p->getSyscallArg(tc, index); 15698324Ssteve.reinhardt@amd.com int tgt_fd = p->getSyscallArg(tc, index); 157010486Stjablin@gmail.com int offset = p->getSyscallArg(tc, index); 1571360SN/A 157211385Sbrandon.potter@amd.com if (is_mmap2) 157311385Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 15749008Sgblack@eecs.umich.edu 157511383Sbrandon.potter@amd.com if (start & (TheISA::PageBytes - 1) || 157611383Sbrandon.potter@amd.com offset & (TheISA::PageBytes - 1) || 157711383Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 157811383Sbrandon.potter@amd.com tgt_flags & OS::TGT_MAP_SHARED) || 157911383Sbrandon.potter@amd.com (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 158011383Sbrandon.potter@amd.com !(tgt_flags & OS::TGT_MAP_SHARED)) || 158111383Sbrandon.potter@amd.com !length) { 158211383Sbrandon.potter@amd.com return -EINVAL; 158311383Sbrandon.potter@amd.com } 15848324Ssteve.reinhardt@amd.com 158511383Sbrandon.potter@amd.com if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 158611383Sbrandon.potter@amd.com // With shared mmaps, there are two cases to consider: 158711383Sbrandon.potter@amd.com // 1) anonymous: writes should modify the mapping and this should be 158811383Sbrandon.potter@amd.com // visible to observers who share the mapping. Currently, it's 158911383Sbrandon.potter@amd.com // difficult to update the shared mapping because there's no 159011383Sbrandon.potter@amd.com // structure which maintains information about the which virtual 159111383Sbrandon.potter@amd.com // memory areas are shared. If that structure existed, it would be 159211383Sbrandon.potter@amd.com // possible to make the translations point to the same frames. 159311383Sbrandon.potter@amd.com // 2) file-backed: writes should modify the mapping and the file 159411383Sbrandon.potter@amd.com // which is backed by the mapping. The shared mapping problem is the 159511383Sbrandon.potter@amd.com // same as what was mentioned about the anonymous mappings. For 159611383Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 159711383Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 159811383Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 159911383Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 160011383Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 160111383Sbrandon.potter@amd.com // mapping). This isn't guaranteed to always happen, but it usually 160211383Sbrandon.potter@amd.com // works well enough. The guarantee is provided by the msync system 160311383Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 160411383Sbrandon.potter@amd.com // a call to msync, but that again would require more information 160511383Sbrandon.potter@amd.com // than we currently maintain. 160611383Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 160711383Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 160811383Sbrandon.potter@amd.com "will not be propagated to the host or shared mappings"); 16098324Ssteve.reinhardt@amd.com } 16105877Shsul@eecs.umich.edu 161110486Stjablin@gmail.com length = roundUp(length, TheISA::PageBytes); 161210486Stjablin@gmail.com 161311383Sbrandon.potter@amd.com int sim_fd = -1; 161411383Sbrandon.potter@amd.com uint8_t *pmap = nullptr; 161511383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 161611856Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 161711624Smichael.lebeane@amd.com 161811856Sbrandon.potter@amd.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 161911856Sbrandon.potter@amd.com if (dfdp) { 162011856Sbrandon.potter@amd.com EmulatedDriver *emul_driver = dfdp->getDriver(); 162111856Sbrandon.potter@amd.com return emul_driver->mmap(p, tc, start, length, prot, 162211624Smichael.lebeane@amd.com tgt_flags, tgt_fd, offset); 162311624Smichael.lebeane@amd.com } 162411624Smichael.lebeane@amd.com 162511856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 162611856Sbrandon.potter@amd.com if (!ffdp) 162711383Sbrandon.potter@amd.com return -EBADF; 162811856Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 1629360SN/A 163011913SBrandon.Potter@amd.com pmap = (decltype(pmap))mmap(nullptr, length, PROT_READ, MAP_PRIVATE, 163111383Sbrandon.potter@amd.com sim_fd, offset); 16328600Ssteve.reinhardt@amd.com 163311383Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 163411383Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 163511383Sbrandon.potter@amd.com return -errno; 16368600Ssteve.reinhardt@amd.com } 16372544SN/A } 16382544SN/A 163911383Sbrandon.potter@amd.com // Extend global mmap region if necessary. Note that we ignore the 164011383Sbrandon.potter@amd.com // start address unless MAP_FIXED is specified. 164111383Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 164211905SBrandon.Potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 164311905SBrandon.Potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 164411905SBrandon.Potter@amd.com 164511905SBrandon.Potter@amd.com start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 164611905SBrandon.Potter@amd.com mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 164711905SBrandon.Potter@amd.com 164811905SBrandon.Potter@amd.com mem_state->setMmapEnd(mmap_end); 164911383Sbrandon.potter@amd.com } 165011383Sbrandon.potter@amd.com 165111383Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 165211383Sbrandon.potter@amd.com start, start + length - 1); 165311383Sbrandon.potter@amd.com 165411383Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 165511383Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 165611383Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 165711383Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 165811383Sbrandon.potter@amd.com if (clobber) { 165911383Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 166011383Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 166111383Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 166211383Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 166311383Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 16648600Ssteve.reinhardt@amd.com } 16656672Sgblack@eecs.umich.edu } 16668600Ssteve.reinhardt@amd.com 166711383Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 166811383Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 166911383Sbrandon.potter@amd.com // fatal and bail out of the simulation. 16708601Ssteve.reinhardt@amd.com p->allocateMem(start, length, clobber); 16712544SN/A 167211383Sbrandon.potter@amd.com // Transfer content into target address space. 167311383Sbrandon.potter@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 167411383Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 167511383Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 167611383Sbrandon.potter@amd.com // with something like: 167711383Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 167811383Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 167911383Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 168011383Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 168111383Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 168211383Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 168311383Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 168411383Sbrandon.potter@amd.com } else { 168511383Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 168611383Sbrandon.potter@amd.com // accessing unmapped portions the system triggers a "Bus error" 168711383Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 168811383Sbrandon.potter@amd.com // the host into the target address space. 168911383Sbrandon.potter@amd.com struct stat file_stat; 169011383Sbrandon.potter@amd.com if (fstat(sim_fd, &file_stat) > 0) 169111383Sbrandon.potter@amd.com fatal("mmap: cannot stat file"); 169211383Sbrandon.potter@amd.com 169311383Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 169411383Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 169511383Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 169611383Sbrandon.potter@amd.com // on the specified offset into the file. 169711383Sbrandon.potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 169811383Sbrandon.potter@amd.com length); 169911383Sbrandon.potter@amd.com tp.writeBlob(start, pmap, size); 170011383Sbrandon.potter@amd.com 170111383Sbrandon.potter@amd.com // Cleanup the mmap region before exiting this function. 170211383Sbrandon.potter@amd.com munmap(pmap, length); 170311383Sbrandon.potter@amd.com 170411392Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 170511392Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 170611392Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 170711392Sbrandon.potter@amd.com // executing inside the loader by checking the program counter value. 170811392Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 170911392Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 171011392Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 171111392Sbrandon.potter@amd.com if (interpreter) { 171211392Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 171311392Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 171411392Sbrandon.potter@amd.com 171511392Sbrandon.potter@amd.com Addr pc = tc->pcState().pc(); 171611392Sbrandon.potter@amd.com 171711392Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 171811856Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 171911856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 172011856Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 172111392Sbrandon.potter@amd.com 172211392Sbrandon.potter@amd.com if (lib) { 172311392Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 172411392Sbrandon.potter@amd.com lib->textBase(), start); 172511392Sbrandon.potter@amd.com } 172611392Sbrandon.potter@amd.com } 172711392Sbrandon.potter@amd.com } 172811392Sbrandon.potter@amd.com 172911383Sbrandon.potter@amd.com // Note that we do not zero out the remainder of the mapping. This 173011383Sbrandon.potter@amd.com // is done by a real system, but it probably will not affect 173111383Sbrandon.potter@amd.com // execution (hopefully). 173211383Sbrandon.potter@amd.com } 173311383Sbrandon.potter@amd.com 17341458SN/A return start; 1735360SN/A} 1736360SN/A 173711593Santhony.gutierrez@amd.comtemplate <class OS> 173811593Santhony.gutierrez@amd.comSyscallReturn 173911851Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 174011593Santhony.gutierrez@amd.com{ 174111593Santhony.gutierrez@amd.com int index = 0; 174211593Santhony.gutierrez@amd.com int tgt_fd = p->getSyscallArg(tc, index); 174311593Santhony.gutierrez@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 174411593Santhony.gutierrez@amd.com int nbytes = p->getSyscallArg(tc, index); 174511593Santhony.gutierrez@amd.com int offset = p->getSyscallArg(tc, index); 174611593Santhony.gutierrez@amd.com 174711856Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 174811856Sbrandon.potter@amd.com if (!ffdp) 174911593Santhony.gutierrez@amd.com return -EBADF; 175011856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 175111593Santhony.gutierrez@amd.com 175211593Santhony.gutierrez@amd.com BufferArg bufArg(bufPtr, nbytes); 175311593Santhony.gutierrez@amd.com bufArg.copyIn(tc->getMemProxy()); 175411593Santhony.gutierrez@amd.com 175511594Santhony.gutierrez@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 175611593Santhony.gutierrez@amd.com 175711593Santhony.gutierrez@amd.com return (bytes_written == -1) ? -errno : bytes_written; 175811593Santhony.gutierrez@amd.com} 175911593Santhony.gutierrez@amd.com 176011385Sbrandon.potter@amd.com/// Target mmap() handler. 176111385Sbrandon.potter@amd.comtemplate <class OS> 176211385Sbrandon.potter@amd.comSyscallReturn 176311851Sbrandon.potter@amd.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 176411385Sbrandon.potter@amd.com{ 176511385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, false); 176611385Sbrandon.potter@amd.com} 176711385Sbrandon.potter@amd.com 176811385Sbrandon.potter@amd.com/// Target mmap2() handler. 176911385Sbrandon.potter@amd.comtemplate <class OS> 177011385Sbrandon.potter@amd.comSyscallReturn 177111851Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 177211385Sbrandon.potter@amd.com{ 177311385Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 177411385Sbrandon.potter@amd.com} 177511385Sbrandon.potter@amd.com 1776378SN/A/// Target getrlimit() handler. 1777360SN/Atemplate <class OS> 17781450SN/ASyscallReturn 177911851Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 178011851Sbrandon.potter@amd.com ThreadContext *tc) 1781360SN/A{ 17826701Sgblack@eecs.umich.edu int index = 0; 17836701Sgblack@eecs.umich.edu unsigned resource = process->getSyscallArg(tc, index); 17846701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 1785360SN/A 1786360SN/A switch (resource) { 178711906SBrandon.Potter@amd.com case OS::TGT_RLIMIT_STACK: 178811906SBrandon.Potter@amd.com // max stack size in bytes: make up a number (8MB for now) 178911906SBrandon.Potter@amd.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 179011906SBrandon.Potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 179111906SBrandon.Potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 179211906SBrandon.Potter@amd.com break; 1793360SN/A 179411906SBrandon.Potter@amd.com case OS::TGT_RLIMIT_DATA: 179511906SBrandon.Potter@amd.com // max data segment size in bytes: make up a number 179611906SBrandon.Potter@amd.com rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 179711906SBrandon.Potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 179811906SBrandon.Potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 179911906SBrandon.Potter@amd.com break; 18005877Shsul@eecs.umich.edu 180111906SBrandon.Potter@amd.com default: 180211906SBrandon.Potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 180311906SBrandon.Potter@amd.com return -EINVAL; 180411906SBrandon.Potter@amd.com break; 1805360SN/A } 1806360SN/A 18078706Sandreas.hansson@arm.com rlp.copyOut(tc->getMemProxy()); 18081458SN/A return 0; 1809360SN/A} 1810360SN/A 181112235Sar4jc@virginia.edutemplate <class OS> 181212235Sar4jc@virginia.eduSyscallReturn 181312235Sar4jc@virginia.eduprlimitFunc(SyscallDesc *desc, int callnum, Process *process, 181412235Sar4jc@virginia.edu ThreadContext *tc) 181512235Sar4jc@virginia.edu{ 181612235Sar4jc@virginia.edu int index = 0; 181712235Sar4jc@virginia.edu if (process->getSyscallArg(tc, index) != 0) 181812235Sar4jc@virginia.edu { 181912235Sar4jc@virginia.edu warn("prlimit: ignoring rlimits for nonzero pid"); 182012235Sar4jc@virginia.edu return -EPERM; 182112235Sar4jc@virginia.edu } 182212235Sar4jc@virginia.edu int resource = process->getSyscallArg(tc, index); 182312235Sar4jc@virginia.edu Addr n = process->getSyscallArg(tc, index); 182412235Sar4jc@virginia.edu if (n != 0) 182512235Sar4jc@virginia.edu warn("prlimit: ignoring new rlimit"); 182612235Sar4jc@virginia.edu Addr o = process->getSyscallArg(tc, index); 182712235Sar4jc@virginia.edu if (o != 0) 182812235Sar4jc@virginia.edu { 182912416Sqtt2@cornell.edu TypedBufferArg<typename OS::rlimit> rlp(o); 183012235Sar4jc@virginia.edu switch (resource) { 183112235Sar4jc@virginia.edu case OS::TGT_RLIMIT_STACK: 183212235Sar4jc@virginia.edu // max stack size in bytes: make up a number (8MB for now) 183312235Sar4jc@virginia.edu rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 183412235Sar4jc@virginia.edu rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 183512235Sar4jc@virginia.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 183612235Sar4jc@virginia.edu break; 183712235Sar4jc@virginia.edu case OS::TGT_RLIMIT_DATA: 183812235Sar4jc@virginia.edu // max data segment size in bytes: make up a number 183912235Sar4jc@virginia.edu rlp->rlim_cur = rlp->rlim_max = 256*1024*1024; 184012235Sar4jc@virginia.edu rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 184112235Sar4jc@virginia.edu rlp->rlim_max = TheISA::htog(rlp->rlim_max); 184212593Sjason@lowepower.com break; 184312235Sar4jc@virginia.edu default: 184412235Sar4jc@virginia.edu warn("prlimit: unimplemented resource %d", resource); 184512235Sar4jc@virginia.edu return -EINVAL; 184612235Sar4jc@virginia.edu break; 184712235Sar4jc@virginia.edu } 184812235Sar4jc@virginia.edu rlp.copyOut(tc->getMemProxy()); 184912235Sar4jc@virginia.edu } 185012235Sar4jc@virginia.edu return 0; 185112235Sar4jc@virginia.edu} 185212235Sar4jc@virginia.edu 185310796Sbrandon.potter@amd.com/// Target clock_gettime() function. 185410796Sbrandon.potter@amd.comtemplate <class OS> 185510796Sbrandon.potter@amd.comSyscallReturn 185611851Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 185710796Sbrandon.potter@amd.com{ 185810796Sbrandon.potter@amd.com int index = 1; 185910796Sbrandon.potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 186010796Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 186110796Sbrandon.potter@amd.com 186210796Sbrandon.potter@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 186310796Sbrandon.potter@amd.com tp->tv_sec += seconds_since_epoch; 186410796Sbrandon.potter@amd.com tp->tv_sec = TheISA::htog(tp->tv_sec); 186510796Sbrandon.potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 186610796Sbrandon.potter@amd.com 186710796Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 186810796Sbrandon.potter@amd.com 186910796Sbrandon.potter@amd.com return 0; 187010796Sbrandon.potter@amd.com} 187110796Sbrandon.potter@amd.com 187211337SMichael.Lebeane@amd.com/// Target clock_getres() function. 187311337SMichael.Lebeane@amd.comtemplate <class OS> 187411337SMichael.Lebeane@amd.comSyscallReturn 187511851Sbrandon.potter@amd.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 187611337SMichael.Lebeane@amd.com{ 187711337SMichael.Lebeane@amd.com int index = 1; 187811337SMichael.Lebeane@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 187911337SMichael.Lebeane@amd.com 188011337SMichael.Lebeane@amd.com // Set resolution at ns, which is what clock_gettime() returns 188111337SMichael.Lebeane@amd.com tp->tv_sec = 0; 188211337SMichael.Lebeane@amd.com tp->tv_nsec = 1; 188311337SMichael.Lebeane@amd.com 188411337SMichael.Lebeane@amd.com tp.copyOut(tc->getMemProxy()); 188511337SMichael.Lebeane@amd.com 188611337SMichael.Lebeane@amd.com return 0; 188711337SMichael.Lebeane@amd.com} 188811337SMichael.Lebeane@amd.com 1889378SN/A/// Target gettimeofday() handler. 1890360SN/Atemplate <class OS> 18911450SN/ASyscallReturn 189211851Sbrandon.potter@amd.comgettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 189311851Sbrandon.potter@amd.com ThreadContext *tc) 1894360SN/A{ 18956701Sgblack@eecs.umich.edu int index = 0; 18966701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 1897360SN/A 189810796Sbrandon.potter@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 1899360SN/A tp->tv_sec += seconds_since_epoch; 19006109Ssanchezd@stanford.edu tp->tv_sec = TheISA::htog(tp->tv_sec); 19016109Ssanchezd@stanford.edu tp->tv_usec = TheISA::htog(tp->tv_usec); 1902360SN/A 19038706Sandreas.hansson@arm.com tp.copyOut(tc->getMemProxy()); 1904360SN/A 19051458SN/A return 0; 1906360SN/A} 1907360SN/A 1908360SN/A 19091999SN/A/// Target utimes() handler. 19101999SN/Atemplate <class OS> 19111999SN/ASyscallReturn 191211851Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process, 19132680Sktlim@umich.edu ThreadContext *tc) 19141999SN/A{ 19151999SN/A std::string path; 19161999SN/A 19176701Sgblack@eecs.umich.edu int index = 0; 19188852Sandreas.hansson@arm.com if (!tc->getMemProxy().tryReadString(path, 19196701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 19206701Sgblack@eecs.umich.edu return -EFAULT; 19216701Sgblack@eecs.umich.edu } 19221999SN/A 19236701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::timeval [2]> 19246701Sgblack@eecs.umich.edu tp(process->getSyscallArg(tc, index)); 19258706Sandreas.hansson@arm.com tp.copyIn(tc->getMemProxy()); 19261999SN/A 19271999SN/A struct timeval hostTimeval[2]; 192811906SBrandon.Potter@amd.com for (int i = 0; i < 2; ++i) { 19298737Skoansin.tan@gmail.com hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 19308737Skoansin.tan@gmail.com hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 19311999SN/A } 19323669Sbinkertn@umich.edu 19333669Sbinkertn@umich.edu // Adjust path for current working directory 19343669Sbinkertn@umich.edu path = process->fullPath(path); 19353669Sbinkertn@umich.edu 19361999SN/A int result = utimes(path.c_str(), hostTimeval); 19371999SN/A 19381999SN/A if (result < 0) 19391999SN/A return -errno; 19401999SN/A 19411999SN/A return 0; 19421999SN/A} 194311886Sbrandon.potter@amd.com 194411886Sbrandon.potter@amd.comtemplate <class OS> 194511886Sbrandon.potter@amd.comSyscallReturn 194611886Sbrandon.potter@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 194711886Sbrandon.potter@amd.com{ 194811886Sbrandon.potter@amd.com desc->setFlags(0); 194911886Sbrandon.potter@amd.com 195011886Sbrandon.potter@amd.com int index = 0; 195111886Sbrandon.potter@amd.com std::string path; 195211886Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 195311886Sbrandon.potter@amd.com if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 195411886Sbrandon.potter@amd.com return -EFAULT; 195511886Sbrandon.potter@amd.com 195611886Sbrandon.potter@amd.com if (access(path.c_str(), F_OK) == -1) 195711886Sbrandon.potter@amd.com return -EACCES; 195811886Sbrandon.potter@amd.com 195911886Sbrandon.potter@amd.com auto read_in = [](std::vector<std::string> & vect, 196011886Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy, 196111886Sbrandon.potter@amd.com Addr mem_loc) 196211886Sbrandon.potter@amd.com { 196311886Sbrandon.potter@amd.com for (int inc = 0; ; inc++) { 196411886Sbrandon.potter@amd.com BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 196511886Sbrandon.potter@amd.com b.copyIn(mem_proxy); 196611886Sbrandon.potter@amd.com 196711886Sbrandon.potter@amd.com if (!*(Addr*)b.bufferPtr()) 196811886Sbrandon.potter@amd.com break; 196911886Sbrandon.potter@amd.com 197011886Sbrandon.potter@amd.com vect.push_back(std::string()); 197111886Sbrandon.potter@amd.com mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 197211886Sbrandon.potter@amd.com } 197311886Sbrandon.potter@amd.com }; 197411886Sbrandon.potter@amd.com 197511886Sbrandon.potter@amd.com /** 197611886Sbrandon.potter@amd.com * Note that ProcessParams is generated by swig and there are no other 197711886Sbrandon.potter@amd.com * examples of how to create anything but this default constructor. The 197811886Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 197911886Sbrandon.potter@amd.com * constructor. 198011886Sbrandon.potter@amd.com */ 198111886Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 198211886Sbrandon.potter@amd.com pp->executable = path; 198311886Sbrandon.potter@amd.com Addr argv_mem_loc = p->getSyscallArg(tc, index); 198411886Sbrandon.potter@amd.com read_in(pp->cmd, mem_proxy, argv_mem_loc); 198511886Sbrandon.potter@amd.com Addr envp_mem_loc = p->getSyscallArg(tc, index); 198611886Sbrandon.potter@amd.com read_in(pp->env, mem_proxy, envp_mem_loc); 198711886Sbrandon.potter@amd.com pp->uid = p->uid(); 198811886Sbrandon.potter@amd.com pp->egid = p->egid(); 198911886Sbrandon.potter@amd.com pp->euid = p->euid(); 199011886Sbrandon.potter@amd.com pp->gid = p->gid(); 199111886Sbrandon.potter@amd.com pp->ppid = p->ppid(); 199211886Sbrandon.potter@amd.com pp->pid = p->pid(); 199311886Sbrandon.potter@amd.com pp->input.assign("cin"); 199411886Sbrandon.potter@amd.com pp->output.assign("cout"); 199511886Sbrandon.potter@amd.com pp->errout.assign("cerr"); 199611886Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 199711886Sbrandon.potter@amd.com pp->system = p->system; 199811886Sbrandon.potter@amd.com /** 199911886Sbrandon.potter@amd.com * Prevent process object creation with identical PIDs (which will trip 200011886Sbrandon.potter@amd.com * a fatal check in Process constructor). The execve call is supposed to 200111886Sbrandon.potter@amd.com * take over the currently executing process' identity but replace 200211886Sbrandon.potter@amd.com * whatever it is doing with a new process image. Instead of hijacking 200311886Sbrandon.potter@amd.com * the process object in the simulator, we create a new process object 200411886Sbrandon.potter@amd.com * and bind to the previous process' thread below (hijacking the thread). 200511886Sbrandon.potter@amd.com */ 200611886Sbrandon.potter@amd.com p->system->PIDs.erase(p->pid()); 200711886Sbrandon.potter@amd.com Process *new_p = pp->create(); 200811886Sbrandon.potter@amd.com delete pp; 200911886Sbrandon.potter@amd.com 201011886Sbrandon.potter@amd.com /** 201111886Sbrandon.potter@amd.com * Work through the file descriptor array and close any files marked 201211886Sbrandon.potter@amd.com * close-on-exec. 201311886Sbrandon.potter@amd.com */ 201411886Sbrandon.potter@amd.com new_p->fds = p->fds; 201511886Sbrandon.potter@amd.com for (int i = 0; i < new_p->fds->getSize(); i++) { 201611886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 201711886Sbrandon.potter@amd.com if (fdep && fdep->getCOE()) 201811886Sbrandon.potter@amd.com new_p->fds->closeFDEntry(i); 201911886Sbrandon.potter@amd.com } 202011886Sbrandon.potter@amd.com 202111886Sbrandon.potter@amd.com *new_p->sigchld = true; 202211886Sbrandon.potter@amd.com 202311886Sbrandon.potter@amd.com delete p; 202411886Sbrandon.potter@amd.com tc->clearArchRegs(); 202511886Sbrandon.potter@amd.com tc->setProcessPtr(new_p); 202611886Sbrandon.potter@amd.com new_p->assignThreadContext(tc->contextId()); 202711886Sbrandon.potter@amd.com new_p->initState(); 202811886Sbrandon.potter@amd.com tc->activate(); 202911886Sbrandon.potter@amd.com TheISA::PCState pcState = tc->pcState(); 203011886Sbrandon.potter@amd.com tc->setNPC(pcState.instAddr()); 203111886Sbrandon.potter@amd.com 203211886Sbrandon.potter@amd.com desc->setFlags(SyscallDesc::SuppressReturnValue); 203311886Sbrandon.potter@amd.com return 0; 203411886Sbrandon.potter@amd.com} 203511886Sbrandon.potter@amd.com 2036378SN/A/// Target getrusage() function. 2037360SN/Atemplate <class OS> 20381450SN/ASyscallReturn 203911851Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 20402680Sktlim@umich.edu ThreadContext *tc) 2041360SN/A{ 20426701Sgblack@eecs.umich.edu int index = 0; 20436701Sgblack@eecs.umich.edu int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 20446701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 2045360SN/A 20463670Sbinkertn@umich.edu rup->ru_utime.tv_sec = 0; 20473670Sbinkertn@umich.edu rup->ru_utime.tv_usec = 0; 2048360SN/A rup->ru_stime.tv_sec = 0; 2049360SN/A rup->ru_stime.tv_usec = 0; 2050360SN/A rup->ru_maxrss = 0; 2051360SN/A rup->ru_ixrss = 0; 2052360SN/A rup->ru_idrss = 0; 2053360SN/A rup->ru_isrss = 0; 2054360SN/A rup->ru_minflt = 0; 2055360SN/A rup->ru_majflt = 0; 2056360SN/A rup->ru_nswap = 0; 2057360SN/A rup->ru_inblock = 0; 2058360SN/A rup->ru_oublock = 0; 2059360SN/A rup->ru_msgsnd = 0; 2060360SN/A rup->ru_msgrcv = 0; 2061360SN/A rup->ru_nsignals = 0; 2062360SN/A rup->ru_nvcsw = 0; 2063360SN/A rup->ru_nivcsw = 0; 2064360SN/A 20653670Sbinkertn@umich.edu switch (who) { 20663670Sbinkertn@umich.edu case OS::TGT_RUSAGE_SELF: 206710796Sbrandon.potter@amd.com getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 20688737Skoansin.tan@gmail.com rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 20698737Skoansin.tan@gmail.com rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 20703670Sbinkertn@umich.edu break; 20713670Sbinkertn@umich.edu 20723670Sbinkertn@umich.edu case OS::TGT_RUSAGE_CHILDREN: 20733670Sbinkertn@umich.edu // do nothing. We have no child processes, so they take no time. 20743670Sbinkertn@umich.edu break; 20753670Sbinkertn@umich.edu 20763670Sbinkertn@umich.edu default: 20773670Sbinkertn@umich.edu // don't really handle THREAD or CHILDREN, but just warn and 20783670Sbinkertn@umich.edu // plow ahead 20793670Sbinkertn@umich.edu warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 20803670Sbinkertn@umich.edu who); 20813670Sbinkertn@umich.edu } 20823670Sbinkertn@umich.edu 20838706Sandreas.hansson@arm.com rup.copyOut(tc->getMemProxy()); 2084360SN/A 20851458SN/A return 0; 2086360SN/A} 2087360SN/A 20886683Stjones1@inf.ed.ac.uk/// Target times() function. 20896683Stjones1@inf.ed.ac.uktemplate <class OS> 20906683Stjones1@inf.ed.ac.ukSyscallReturn 209111851Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process, 209211851Sbrandon.potter@amd.com ThreadContext *tc) 20936683Stjones1@inf.ed.ac.uk{ 20946701Sgblack@eecs.umich.edu int index = 0; 20956701Sgblack@eecs.umich.edu TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 20966683Stjones1@inf.ed.ac.uk 20976683Stjones1@inf.ed.ac.uk // Fill in the time structure (in clocks) 20987823Ssteve.reinhardt@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 20996683Stjones1@inf.ed.ac.uk bufp->tms_utime = clocks; 21006683Stjones1@inf.ed.ac.uk bufp->tms_stime = 0; 21016683Stjones1@inf.ed.ac.uk bufp->tms_cutime = 0; 21026683Stjones1@inf.ed.ac.uk bufp->tms_cstime = 0; 21036683Stjones1@inf.ed.ac.uk 21046683Stjones1@inf.ed.ac.uk // Convert to host endianness 21058737Skoansin.tan@gmail.com bufp->tms_utime = TheISA::htog(bufp->tms_utime); 21066683Stjones1@inf.ed.ac.uk 21076683Stjones1@inf.ed.ac.uk // Write back 21088706Sandreas.hansson@arm.com bufp.copyOut(tc->getMemProxy()); 21096683Stjones1@inf.ed.ac.uk 21106683Stjones1@inf.ed.ac.uk // Return clock ticks since system boot 21116683Stjones1@inf.ed.ac.uk return clocks; 21126683Stjones1@inf.ed.ac.uk} 21132553SN/A 21146684Stjones1@inf.ed.ac.uk/// Target time() function. 21156684Stjones1@inf.ed.ac.uktemplate <class OS> 21166684Stjones1@inf.ed.ac.ukSyscallReturn 211711851Sbrandon.potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 21186684Stjones1@inf.ed.ac.uk{ 21196684Stjones1@inf.ed.ac.uk typename OS::time_t sec, usec; 212010796Sbrandon.potter@amd.com getElapsedTimeMicro(sec, usec); 21216684Stjones1@inf.ed.ac.uk sec += seconds_since_epoch; 21226684Stjones1@inf.ed.ac.uk 21236701Sgblack@eecs.umich.edu int index = 0; 21246701Sgblack@eecs.umich.edu Addr taddr = (Addr)process->getSyscallArg(tc, index); 212511321Ssteve.reinhardt@amd.com if (taddr != 0) { 21266684Stjones1@inf.ed.ac.uk typename OS::time_t t = sec; 21278737Skoansin.tan@gmail.com t = TheISA::htog(t); 21288852Sandreas.hansson@arm.com SETranslatingPortProxy &p = tc->getMemProxy(); 21298852Sandreas.hansson@arm.com p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 21306684Stjones1@inf.ed.ac.uk } 21316684Stjones1@inf.ed.ac.uk return sec; 21326684Stjones1@inf.ed.ac.uk} 21332553SN/A 213411910SBrandon.Potter@amd.comtemplate <class OS> 213511910SBrandon.Potter@amd.comSyscallReturn 213611910SBrandon.Potter@amd.comtgkillFunc(SyscallDesc *desc, int num, Process *process, ThreadContext *tc) 213711910SBrandon.Potter@amd.com{ 213811910SBrandon.Potter@amd.com int index = 0; 213911910SBrandon.Potter@amd.com int tgid = process->getSyscallArg(tc, index); 214011910SBrandon.Potter@amd.com int tid = process->getSyscallArg(tc, index); 214111910SBrandon.Potter@amd.com int sig = process->getSyscallArg(tc, index); 214211910SBrandon.Potter@amd.com 214311910SBrandon.Potter@amd.com /** 214411910SBrandon.Potter@amd.com * This system call is intended to allow killing a specific thread 214511910SBrandon.Potter@amd.com * within an arbitrary thread group if sanctioned with permission checks. 214611910SBrandon.Potter@amd.com * It's usually true that threads share the termination signal as pointed 214711910SBrandon.Potter@amd.com * out by the pthread_kill man page and this seems to be the intended 214811910SBrandon.Potter@amd.com * usage. Due to this being an emulated environment, assume the following: 214911910SBrandon.Potter@amd.com * Threads are allowed to call tgkill because the EUID for all threads 215011910SBrandon.Potter@amd.com * should be the same. There is no signal handling mechanism for kernel 215111910SBrandon.Potter@amd.com * registration of signal handlers since signals are poorly supported in 215211910SBrandon.Potter@amd.com * emulation mode. Since signal handlers cannot be registered, all 215311910SBrandon.Potter@amd.com * threads within in a thread group must share the termination signal. 215411910SBrandon.Potter@amd.com * We never exhaust PIDs so there's no chance of finding the wrong one 215511910SBrandon.Potter@amd.com * due to PID rollover. 215611910SBrandon.Potter@amd.com */ 215711910SBrandon.Potter@amd.com 215811910SBrandon.Potter@amd.com System *sys = tc->getSystemPtr(); 215911910SBrandon.Potter@amd.com Process *tgt_proc = nullptr; 216011910SBrandon.Potter@amd.com for (int i = 0; i < sys->numContexts(); i++) { 216111910SBrandon.Potter@amd.com Process *temp = sys->threadContexts[i]->getProcessPtr(); 216211910SBrandon.Potter@amd.com if (temp->pid() == tid) { 216311910SBrandon.Potter@amd.com tgt_proc = temp; 216411910SBrandon.Potter@amd.com break; 216511910SBrandon.Potter@amd.com } 216611910SBrandon.Potter@amd.com } 216711910SBrandon.Potter@amd.com 216811910SBrandon.Potter@amd.com if (sig != 0 || sig != OS::TGT_SIGABRT) 216911910SBrandon.Potter@amd.com return -EINVAL; 217011910SBrandon.Potter@amd.com 217111910SBrandon.Potter@amd.com if (tgt_proc == nullptr) 217211910SBrandon.Potter@amd.com return -ESRCH; 217311910SBrandon.Potter@amd.com 217411910SBrandon.Potter@amd.com if (tgid != -1 && tgt_proc->tgid() != tgid) 217511910SBrandon.Potter@amd.com return -ESRCH; 217611910SBrandon.Potter@amd.com 217711910SBrandon.Potter@amd.com if (sig == OS::TGT_SIGABRT) 217811910SBrandon.Potter@amd.com exitGroupFunc(desc, 252, process, tc); 217911910SBrandon.Potter@amd.com 218011910SBrandon.Potter@amd.com return 0; 218111910SBrandon.Potter@amd.com} 218211910SBrandon.Potter@amd.com 218313568Sbrandon.potter@amd.comtemplate <class OS> 218413568Sbrandon.potter@amd.comSyscallReturn 218513568Sbrandon.potter@amd.comsocketFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 218613568Sbrandon.potter@amd.com{ 218713568Sbrandon.potter@amd.com int index = 0; 218813568Sbrandon.potter@amd.com int domain = p->getSyscallArg(tc, index); 218913568Sbrandon.potter@amd.com int type = p->getSyscallArg(tc, index); 219013568Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 219113568Sbrandon.potter@amd.com 219213568Sbrandon.potter@amd.com int sim_fd = socket(domain, type, prot); 219313568Sbrandon.potter@amd.com if (sim_fd == -1) 219413568Sbrandon.potter@amd.com return -errno; 219513568Sbrandon.potter@amd.com 219613568Sbrandon.potter@amd.com auto sfdp = std::make_shared<SocketFDEntry>(sim_fd, domain, type, prot); 219713568Sbrandon.potter@amd.com int tgt_fd = p->fds->allocFD(sfdp); 219813568Sbrandon.potter@amd.com 219913568Sbrandon.potter@amd.com return tgt_fd; 220013568Sbrandon.potter@amd.com} 220113568Sbrandon.potter@amd.com 220213568Sbrandon.potter@amd.comtemplate <class OS> 220313568Sbrandon.potter@amd.comSyscallReturn 220413568Sbrandon.potter@amd.comsocketpairFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 220513568Sbrandon.potter@amd.com{ 220613568Sbrandon.potter@amd.com int index = 0; 220713568Sbrandon.potter@amd.com int domain = p->getSyscallArg(tc, index); 220813568Sbrandon.potter@amd.com int type = p->getSyscallArg(tc, index); 220913568Sbrandon.potter@amd.com int prot = p->getSyscallArg(tc, index); 221013568Sbrandon.potter@amd.com Addr svPtr = p->getSyscallArg(tc, index); 221113568Sbrandon.potter@amd.com 221213568Sbrandon.potter@amd.com BufferArg svBuf((Addr)svPtr, 2 * sizeof(int)); 221313568Sbrandon.potter@amd.com int status = socketpair(domain, type, prot, (int *)svBuf.bufferPtr()); 221413568Sbrandon.potter@amd.com if (status == -1) 221513568Sbrandon.potter@amd.com return -errno; 221613568Sbrandon.potter@amd.com 221713568Sbrandon.potter@amd.com int *fds = (int *)svBuf.bufferPtr(); 221813568Sbrandon.potter@amd.com 221913568Sbrandon.potter@amd.com auto sfdp1 = std::make_shared<SocketFDEntry>(fds[0], domain, type, prot); 222013568Sbrandon.potter@amd.com fds[0] = p->fds->allocFD(sfdp1); 222113568Sbrandon.potter@amd.com auto sfdp2 = std::make_shared<SocketFDEntry>(fds[1], domain, type, prot); 222213568Sbrandon.potter@amd.com fds[1] = p->fds->allocFD(sfdp2); 222313568Sbrandon.potter@amd.com svBuf.copyOut(tc->getMemProxy()); 222413568Sbrandon.potter@amd.com 222513568Sbrandon.potter@amd.com return status; 222613568Sbrandon.potter@amd.com} 22272553SN/A 222813570Sbrandon.potter@amd.comtemplate <class OS> 222913570Sbrandon.potter@amd.comSyscallReturn 223013570Sbrandon.potter@amd.comselectFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 223113570Sbrandon.potter@amd.com{ 223213570Sbrandon.potter@amd.com int retval; 223313570Sbrandon.potter@amd.com 223413570Sbrandon.potter@amd.com int index = 0; 223513570Sbrandon.potter@amd.com int nfds_t = p->getSyscallArg(tc, index); 223613570Sbrandon.potter@amd.com Addr fds_read_ptr = p->getSyscallArg(tc, index); 223713570Sbrandon.potter@amd.com Addr fds_writ_ptr = p->getSyscallArg(tc, index); 223813570Sbrandon.potter@amd.com Addr fds_excp_ptr = p->getSyscallArg(tc, index); 223913570Sbrandon.potter@amd.com Addr time_val_ptr = p->getSyscallArg(tc, index); 224013570Sbrandon.potter@amd.com 224113570Sbrandon.potter@amd.com TypedBufferArg<typename OS::fd_set> rd_t(fds_read_ptr); 224213570Sbrandon.potter@amd.com TypedBufferArg<typename OS::fd_set> wr_t(fds_writ_ptr); 224313570Sbrandon.potter@amd.com TypedBufferArg<typename OS::fd_set> ex_t(fds_excp_ptr); 224413570Sbrandon.potter@amd.com TypedBufferArg<typename OS::timeval> tp(time_val_ptr); 224513570Sbrandon.potter@amd.com 224613570Sbrandon.potter@amd.com /** 224713570Sbrandon.potter@amd.com * Host fields. Notice that these use the definitions from the system 224813570Sbrandon.potter@amd.com * headers instead of the gem5 headers and libraries. If the host and 224913570Sbrandon.potter@amd.com * target have different header file definitions, this will not work. 225013570Sbrandon.potter@amd.com */ 225113570Sbrandon.potter@amd.com fd_set rd_h; 225213570Sbrandon.potter@amd.com FD_ZERO(&rd_h); 225313570Sbrandon.potter@amd.com fd_set wr_h; 225413570Sbrandon.potter@amd.com FD_ZERO(&wr_h); 225513570Sbrandon.potter@amd.com fd_set ex_h; 225613570Sbrandon.potter@amd.com FD_ZERO(&ex_h); 225713570Sbrandon.potter@amd.com 225813570Sbrandon.potter@amd.com /** 225913570Sbrandon.potter@amd.com * Copy in the fd_set from the target. 226013570Sbrandon.potter@amd.com */ 226113570Sbrandon.potter@amd.com if (fds_read_ptr) 226213570Sbrandon.potter@amd.com rd_t.copyIn(tc->getMemProxy()); 226313570Sbrandon.potter@amd.com if (fds_writ_ptr) 226413570Sbrandon.potter@amd.com wr_t.copyIn(tc->getMemProxy()); 226513570Sbrandon.potter@amd.com if (fds_excp_ptr) 226613570Sbrandon.potter@amd.com ex_t.copyIn(tc->getMemProxy()); 226713570Sbrandon.potter@amd.com 226813570Sbrandon.potter@amd.com /** 226913570Sbrandon.potter@amd.com * We need to translate the target file descriptor set into a host file 227013570Sbrandon.potter@amd.com * descriptor set. This involves both our internal process fd array 227113570Sbrandon.potter@amd.com * and the fd_set defined in Linux header files. The nfds field also 227213570Sbrandon.potter@amd.com * needs to be updated as it will be only target specific after 227313570Sbrandon.potter@amd.com * retrieving it from the target; the nfds value is expected to be the 227413570Sbrandon.potter@amd.com * highest file descriptor that needs to be checked, so we need to extend 227513570Sbrandon.potter@amd.com * it out for nfds_h when we do the update. 227613570Sbrandon.potter@amd.com */ 227713570Sbrandon.potter@amd.com int nfds_h = 0; 227813570Sbrandon.potter@amd.com std::map<int, int> trans_map; 227913570Sbrandon.potter@amd.com auto try_add_host_set = [&](fd_set *tgt_set_entry, 228013570Sbrandon.potter@amd.com fd_set *hst_set_entry, 228113570Sbrandon.potter@amd.com int iter) -> bool 228213570Sbrandon.potter@amd.com { 228313570Sbrandon.potter@amd.com /** 228413570Sbrandon.potter@amd.com * By this point, we know that we are looking at a valid file 228513570Sbrandon.potter@amd.com * descriptor set on the target. We need to check if the target file 228613570Sbrandon.potter@amd.com * descriptor value passed in as iter is part of the set. 228713570Sbrandon.potter@amd.com */ 228813570Sbrandon.potter@amd.com if (FD_ISSET(iter, tgt_set_entry)) { 228913570Sbrandon.potter@amd.com /** 229013570Sbrandon.potter@amd.com * We know that the target file descriptor belongs to the set, 229113570Sbrandon.potter@amd.com * but we do not yet know if the file descriptor is valid or 229213570Sbrandon.potter@amd.com * that we have a host mapping. Check that now. 229313570Sbrandon.potter@amd.com */ 229413570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[iter]); 229513570Sbrandon.potter@amd.com if (!hbfdp) 229613570Sbrandon.potter@amd.com return true; 229713570Sbrandon.potter@amd.com auto sim_fd = hbfdp->getSimFD(); 229813570Sbrandon.potter@amd.com 229913570Sbrandon.potter@amd.com /** 230013570Sbrandon.potter@amd.com * Add the sim_fd to tgt_fd translation into trans_map for use 230113570Sbrandon.potter@amd.com * later when we need to zero the target fd_set structures and 230213570Sbrandon.potter@amd.com * then update them with hits returned from the host select call. 230313570Sbrandon.potter@amd.com */ 230413570Sbrandon.potter@amd.com trans_map[sim_fd] = iter; 230513570Sbrandon.potter@amd.com 230613570Sbrandon.potter@amd.com /** 230713570Sbrandon.potter@amd.com * We know that the host file descriptor exists so now we check 230813570Sbrandon.potter@amd.com * if we need to update the max count for nfds_h before passing 230913570Sbrandon.potter@amd.com * the duplicated structure into the host. 231013570Sbrandon.potter@amd.com */ 231113570Sbrandon.potter@amd.com nfds_h = std::max(nfds_h - 1, sim_fd + 1); 231213570Sbrandon.potter@amd.com 231313570Sbrandon.potter@amd.com /** 231413570Sbrandon.potter@amd.com * Add the host file descriptor to the set that we are going to 231513570Sbrandon.potter@amd.com * pass into the host. 231613570Sbrandon.potter@amd.com */ 231713570Sbrandon.potter@amd.com FD_SET(sim_fd, hst_set_entry); 231813570Sbrandon.potter@amd.com } 231913570Sbrandon.potter@amd.com return false; 232013570Sbrandon.potter@amd.com }; 232113570Sbrandon.potter@amd.com 232213570Sbrandon.potter@amd.com for (int i = 0; i < nfds_t; i++) { 232313570Sbrandon.potter@amd.com if (fds_read_ptr) { 232413570Sbrandon.potter@amd.com bool ebadf = try_add_host_set((fd_set*)&*rd_t, &rd_h, i); 232513570Sbrandon.potter@amd.com if (ebadf) return -EBADF; 232613570Sbrandon.potter@amd.com } 232713570Sbrandon.potter@amd.com if (fds_writ_ptr) { 232813570Sbrandon.potter@amd.com bool ebadf = try_add_host_set((fd_set*)&*wr_t, &wr_h, i); 232913570Sbrandon.potter@amd.com if (ebadf) return -EBADF; 233013570Sbrandon.potter@amd.com } 233113570Sbrandon.potter@amd.com if (fds_excp_ptr) { 233213570Sbrandon.potter@amd.com bool ebadf = try_add_host_set((fd_set*)&*ex_t, &ex_h, i); 233313570Sbrandon.potter@amd.com if (ebadf) return -EBADF; 233413570Sbrandon.potter@amd.com } 233513570Sbrandon.potter@amd.com } 233613570Sbrandon.potter@amd.com 233713570Sbrandon.potter@amd.com if (time_val_ptr) { 233813570Sbrandon.potter@amd.com /** 233913570Sbrandon.potter@amd.com * It might be possible to decrement the timeval based on some 234013570Sbrandon.potter@amd.com * derivation of wall clock determined from elapsed simulator ticks 234113570Sbrandon.potter@amd.com * but that seems like overkill. Rather, we just set the timeval with 234213570Sbrandon.potter@amd.com * zero timeout. (There is no reason to block during the simulation 234313570Sbrandon.potter@amd.com * as it only decreases simulator performance.) 234413570Sbrandon.potter@amd.com */ 234513570Sbrandon.potter@amd.com tp->tv_sec = 0; 234613570Sbrandon.potter@amd.com tp->tv_usec = 0; 234713570Sbrandon.potter@amd.com 234813570Sbrandon.potter@amd.com retval = select(nfds_h, 234913570Sbrandon.potter@amd.com fds_read_ptr ? &rd_h : nullptr, 235013570Sbrandon.potter@amd.com fds_writ_ptr ? &wr_h : nullptr, 235113570Sbrandon.potter@amd.com fds_excp_ptr ? &ex_h : nullptr, 235213570Sbrandon.potter@amd.com (timeval*)&*tp); 235313570Sbrandon.potter@amd.com } else { 235413570Sbrandon.potter@amd.com /** 235513570Sbrandon.potter@amd.com * If the timeval pointer is null, setup a new timeval structure to 235613570Sbrandon.potter@amd.com * pass into the host select call. Unfortunately, we will need to 235713570Sbrandon.potter@amd.com * manually check the return value and throw a retry fault if the 235813570Sbrandon.potter@amd.com * return value is zero. Allowing the system call to block will 235913570Sbrandon.potter@amd.com * likely deadlock the event queue. 236013570Sbrandon.potter@amd.com */ 236113570Sbrandon.potter@amd.com struct timeval tv = { 0, 0 }; 236213570Sbrandon.potter@amd.com 236313570Sbrandon.potter@amd.com retval = select(nfds_h, 236413570Sbrandon.potter@amd.com fds_read_ptr ? &rd_h : nullptr, 236513570Sbrandon.potter@amd.com fds_writ_ptr ? &wr_h : nullptr, 236613570Sbrandon.potter@amd.com fds_excp_ptr ? &ex_h : nullptr, 236713570Sbrandon.potter@amd.com &tv); 236813570Sbrandon.potter@amd.com 236913570Sbrandon.potter@amd.com if (retval == 0) { 237013570Sbrandon.potter@amd.com /** 237113570Sbrandon.potter@amd.com * If blocking indefinitely, check the signal list to see if a 237213570Sbrandon.potter@amd.com * signal would break the poll out of the retry cycle and try to 237313570Sbrandon.potter@amd.com * return the signal interrupt instead. 237413570Sbrandon.potter@amd.com */ 237513570Sbrandon.potter@amd.com for (auto sig : tc->getSystemPtr()->signalList) 237613570Sbrandon.potter@amd.com if (sig.receiver == p) 237713570Sbrandon.potter@amd.com return -EINTR; 237813570Sbrandon.potter@amd.com return SyscallReturn::retry(); 237913570Sbrandon.potter@amd.com } 238013570Sbrandon.potter@amd.com } 238113570Sbrandon.potter@amd.com 238213570Sbrandon.potter@amd.com if (retval == -1) 238313570Sbrandon.potter@amd.com return -errno; 238413570Sbrandon.potter@amd.com 238513570Sbrandon.potter@amd.com FD_ZERO((fd_set*)&*rd_t); 238613570Sbrandon.potter@amd.com FD_ZERO((fd_set*)&*wr_t); 238713570Sbrandon.potter@amd.com FD_ZERO((fd_set*)&*ex_t); 238813570Sbrandon.potter@amd.com 238913570Sbrandon.potter@amd.com /** 239013570Sbrandon.potter@amd.com * We need to translate the host file descriptor set into a target file 239113570Sbrandon.potter@amd.com * descriptor set. This involves both our internal process fd array 239213570Sbrandon.potter@amd.com * and the fd_set defined in header files. 239313570Sbrandon.potter@amd.com */ 239413570Sbrandon.potter@amd.com for (int i = 0; i < nfds_h; i++) { 239513570Sbrandon.potter@amd.com if (fds_read_ptr) { 239613570Sbrandon.potter@amd.com if (FD_ISSET(i, &rd_h)) 239713570Sbrandon.potter@amd.com FD_SET(trans_map[i], (fd_set*)&*rd_t); 239813570Sbrandon.potter@amd.com } 239913570Sbrandon.potter@amd.com 240013570Sbrandon.potter@amd.com if (fds_writ_ptr) { 240113570Sbrandon.potter@amd.com if (FD_ISSET(i, &wr_h)) 240213570Sbrandon.potter@amd.com FD_SET(trans_map[i], (fd_set*)&*wr_t); 240313570Sbrandon.potter@amd.com } 240413570Sbrandon.potter@amd.com 240513570Sbrandon.potter@amd.com if (fds_excp_ptr) { 240613570Sbrandon.potter@amd.com if (FD_ISSET(i, &ex_h)) 240713570Sbrandon.potter@amd.com FD_SET(trans_map[i], (fd_set*)&*ex_t); 240813570Sbrandon.potter@amd.com } 240913570Sbrandon.potter@amd.com } 241013570Sbrandon.potter@amd.com 241113570Sbrandon.potter@amd.com if (fds_read_ptr) 241213570Sbrandon.potter@amd.com rd_t.copyOut(tc->getMemProxy()); 241313570Sbrandon.potter@amd.com if (fds_writ_ptr) 241413570Sbrandon.potter@amd.com wr_t.copyOut(tc->getMemProxy()); 241513570Sbrandon.potter@amd.com if (fds_excp_ptr) 241613570Sbrandon.potter@amd.com ex_t.copyOut(tc->getMemProxy()); 241713570Sbrandon.potter@amd.com if (time_val_ptr) 241813570Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 241913570Sbrandon.potter@amd.com 242013570Sbrandon.potter@amd.com return retval; 242113570Sbrandon.potter@amd.com} 242213570Sbrandon.potter@amd.com 242313570Sbrandon.potter@amd.comtemplate <class OS> 242413570Sbrandon.potter@amd.comSyscallReturn 242513570Sbrandon.potter@amd.comreadFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 242613570Sbrandon.potter@amd.com{ 242713570Sbrandon.potter@amd.com int index = 0; 242813570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 242913570Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 243013570Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 243113570Sbrandon.potter@amd.com 243213570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 243313570Sbrandon.potter@amd.com if (!hbfdp) 243413570Sbrandon.potter@amd.com return -EBADF; 243513570Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 243613570Sbrandon.potter@amd.com 243713570Sbrandon.potter@amd.com struct pollfd pfd; 243813570Sbrandon.potter@amd.com pfd.fd = sim_fd; 243913570Sbrandon.potter@amd.com pfd.events = POLLIN | POLLPRI; 244013570Sbrandon.potter@amd.com if ((poll(&pfd, 1, 0) == 0) 244113570Sbrandon.potter@amd.com && !(hbfdp->getFlags() & OS::TGT_O_NONBLOCK)) 244213570Sbrandon.potter@amd.com return SyscallReturn::retry(); 244313570Sbrandon.potter@amd.com 244413570Sbrandon.potter@amd.com BufferArg buf_arg(buf_ptr, nbytes); 244513570Sbrandon.potter@amd.com int bytes_read = read(sim_fd, buf_arg.bufferPtr(), nbytes); 244613570Sbrandon.potter@amd.com 244713570Sbrandon.potter@amd.com if (bytes_read > 0) 244813570Sbrandon.potter@amd.com buf_arg.copyOut(tc->getMemProxy()); 244913570Sbrandon.potter@amd.com 245013570Sbrandon.potter@amd.com return (bytes_read == -1) ? -errno : bytes_read; 245113570Sbrandon.potter@amd.com} 245213570Sbrandon.potter@amd.com 245313570Sbrandon.potter@amd.comtemplate <class OS> 245413570Sbrandon.potter@amd.comSyscallReturn 245513570Sbrandon.potter@amd.comwriteFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 245613570Sbrandon.potter@amd.com{ 245713570Sbrandon.potter@amd.com int index = 0; 245813570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 245913570Sbrandon.potter@amd.com Addr buf_ptr = p->getSyscallArg(tc, index); 246013570Sbrandon.potter@amd.com int nbytes = p->getSyscallArg(tc, index); 246113570Sbrandon.potter@amd.com 246213570Sbrandon.potter@amd.com auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 246313570Sbrandon.potter@amd.com if (!hbfdp) 246413570Sbrandon.potter@amd.com return -EBADF; 246513570Sbrandon.potter@amd.com int sim_fd = hbfdp->getSimFD(); 246613570Sbrandon.potter@amd.com 246713570Sbrandon.potter@amd.com BufferArg buf_arg(buf_ptr, nbytes); 246813570Sbrandon.potter@amd.com buf_arg.copyIn(tc->getMemProxy()); 246913570Sbrandon.potter@amd.com 247013570Sbrandon.potter@amd.com struct pollfd pfd; 247113570Sbrandon.potter@amd.com pfd.fd = sim_fd; 247213570Sbrandon.potter@amd.com pfd.events = POLLOUT; 247313570Sbrandon.potter@amd.com 247413570Sbrandon.potter@amd.com /** 247513570Sbrandon.potter@amd.com * We don't want to poll on /dev/random. The kernel will not enable the 247613570Sbrandon.potter@amd.com * file descriptor for writing unless the entropy in the system falls 247713570Sbrandon.potter@amd.com * below write_wakeup_threshold. This is not guaranteed to happen 247813570Sbrandon.potter@amd.com * depending on host settings. 247913570Sbrandon.potter@amd.com */ 248013570Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(hbfdp); 248113570Sbrandon.potter@amd.com if (ffdp && (ffdp->getFileName() != "/dev/random")) { 248213570Sbrandon.potter@amd.com if (!poll(&pfd, 1, 0) && !(ffdp->getFlags() & OS::TGT_O_NONBLOCK)) 248313570Sbrandon.potter@amd.com return SyscallReturn::retry(); 248413570Sbrandon.potter@amd.com } 248513570Sbrandon.potter@amd.com 248613570Sbrandon.potter@amd.com int bytes_written = write(sim_fd, buf_arg.bufferPtr(), nbytes); 248713570Sbrandon.potter@amd.com 248813570Sbrandon.potter@amd.com if (bytes_written != -1) 248913570Sbrandon.potter@amd.com fsync(sim_fd); 249013570Sbrandon.potter@amd.com 249113570Sbrandon.potter@amd.com return (bytes_written == -1) ? -errno : bytes_written; 249213570Sbrandon.potter@amd.com} 249313570Sbrandon.potter@amd.com 249413570Sbrandon.potter@amd.comtemplate <class OS> 249513570Sbrandon.potter@amd.comSyscallReturn 249613570Sbrandon.potter@amd.comwait4Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 249713570Sbrandon.potter@amd.com{ 249813570Sbrandon.potter@amd.com int index = 0; 249913570Sbrandon.potter@amd.com pid_t pid = p->getSyscallArg(tc, index); 250013570Sbrandon.potter@amd.com Addr statPtr = p->getSyscallArg(tc, index); 250113570Sbrandon.potter@amd.com int options = p->getSyscallArg(tc, index); 250213570Sbrandon.potter@amd.com Addr rusagePtr = p->getSyscallArg(tc, index); 250313570Sbrandon.potter@amd.com 250413570Sbrandon.potter@amd.com if (rusagePtr) 250513570Sbrandon.potter@amd.com DPRINTFR(SyscallVerbose, 250613570Sbrandon.potter@amd.com "%d: %s: syscall wait4: rusage pointer provided however " 250713570Sbrandon.potter@amd.com "functionality not supported. Ignoring rusage pointer.\n", 250813570Sbrandon.potter@amd.com curTick(), tc->getCpuPtr()->name()); 250913570Sbrandon.potter@amd.com 251013570Sbrandon.potter@amd.com /** 251113570Sbrandon.potter@amd.com * Currently, wait4 is only implemented so that it will wait for children 251213570Sbrandon.potter@amd.com * exit conditions which are denoted by a SIGCHLD signals posted into the 251313570Sbrandon.potter@amd.com * system signal list. We return no additional information via any of the 251413570Sbrandon.potter@amd.com * parameters supplied to wait4. If nothing is found in the system signal 251513570Sbrandon.potter@amd.com * list, we will wait indefinitely for SIGCHLD to post by retrying the 251613570Sbrandon.potter@amd.com * call. 251713570Sbrandon.potter@amd.com */ 251813570Sbrandon.potter@amd.com System *sysh = tc->getSystemPtr(); 251913570Sbrandon.potter@amd.com std::list<BasicSignal>::iterator iter; 252013570Sbrandon.potter@amd.com for (iter=sysh->signalList.begin(); iter!=sysh->signalList.end(); iter++) { 252113570Sbrandon.potter@amd.com if (iter->receiver == p) { 252213570Sbrandon.potter@amd.com if (pid < -1) { 252313570Sbrandon.potter@amd.com if ((iter->sender->pgid() == -pid) 252413570Sbrandon.potter@amd.com && (iter->signalValue == OS::TGT_SIGCHLD)) 252513570Sbrandon.potter@amd.com goto success; 252613570Sbrandon.potter@amd.com } else if (pid == -1) { 252713570Sbrandon.potter@amd.com if (iter->signalValue == OS::TGT_SIGCHLD) 252813570Sbrandon.potter@amd.com goto success; 252913570Sbrandon.potter@amd.com } else if (pid == 0) { 253013570Sbrandon.potter@amd.com if ((iter->sender->pgid() == p->pgid()) 253113570Sbrandon.potter@amd.com && (iter->signalValue == OS::TGT_SIGCHLD)) 253213570Sbrandon.potter@amd.com goto success; 253313570Sbrandon.potter@amd.com } else { 253413570Sbrandon.potter@amd.com if ((iter->sender->pid() == pid) 253513570Sbrandon.potter@amd.com && (iter->signalValue == OS::TGT_SIGCHLD)) 253613570Sbrandon.potter@amd.com goto success; 253713570Sbrandon.potter@amd.com } 253813570Sbrandon.potter@amd.com } 253913570Sbrandon.potter@amd.com } 254013570Sbrandon.potter@amd.com 254113570Sbrandon.potter@amd.com return (options & OS::TGT_WNOHANG) ? 0 : SyscallReturn::retry(); 254213570Sbrandon.potter@amd.com 254313570Sbrandon.potter@amd.comsuccess: 254413570Sbrandon.potter@amd.com // Set status to EXITED for WIFEXITED evaluations. 254513570Sbrandon.potter@amd.com const int EXITED = 0; 254613570Sbrandon.potter@amd.com BufferArg statusBuf(statPtr, sizeof(int)); 254713570Sbrandon.potter@amd.com *(int *)statusBuf.bufferPtr() = EXITED; 254813570Sbrandon.potter@amd.com statusBuf.copyOut(tc->getMemProxy()); 254913570Sbrandon.potter@amd.com 255013570Sbrandon.potter@amd.com // Return the child PID. 255113570Sbrandon.potter@amd.com pid_t retval = iter->sender->pid(); 255213570Sbrandon.potter@amd.com sysh->signalList.erase(iter); 255313570Sbrandon.potter@amd.com return retval; 255413570Sbrandon.potter@amd.com} 255513570Sbrandon.potter@amd.com 255613570Sbrandon.potter@amd.comtemplate <class OS> 255713570Sbrandon.potter@amd.comSyscallReturn 255813570Sbrandon.potter@amd.comacceptFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 255913570Sbrandon.potter@amd.com{ 256013570Sbrandon.potter@amd.com struct sockaddr sa; 256113570Sbrandon.potter@amd.com socklen_t addrLen; 256213570Sbrandon.potter@amd.com int host_fd; 256313570Sbrandon.potter@amd.com int index = 0; 256413570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 256513570Sbrandon.potter@amd.com Addr addrPtr = p->getSyscallArg(tc, index); 256613570Sbrandon.potter@amd.com Addr lenPtr = p->getSyscallArg(tc, index); 256713570Sbrandon.potter@amd.com 256813570Sbrandon.potter@amd.com BufferArg *lenBufPtr = nullptr; 256913570Sbrandon.potter@amd.com BufferArg *addrBufPtr = nullptr; 257013570Sbrandon.potter@amd.com 257113570Sbrandon.potter@amd.com auto sfdp = std::dynamic_pointer_cast<SocketFDEntry>((*p->fds)[tgt_fd]); 257213570Sbrandon.potter@amd.com if (!sfdp) 257313570Sbrandon.potter@amd.com return -EBADF; 257413570Sbrandon.potter@amd.com int sim_fd = sfdp->getSimFD(); 257513570Sbrandon.potter@amd.com 257613570Sbrandon.potter@amd.com /** 257713570Sbrandon.potter@amd.com * We poll the socket file descriptor first to guarantee that we do not 257813570Sbrandon.potter@amd.com * block on our accept call. The socket can be opened without the 257913570Sbrandon.potter@amd.com * non-blocking flag (it blocks). This will cause deadlocks between 258013570Sbrandon.potter@amd.com * communicating processes. 258113570Sbrandon.potter@amd.com */ 258213570Sbrandon.potter@amd.com struct pollfd pfd; 258313570Sbrandon.potter@amd.com pfd.fd = sim_fd; 258413570Sbrandon.potter@amd.com pfd.events = POLLIN | POLLPRI; 258513570Sbrandon.potter@amd.com if ((poll(&pfd, 1, 0) == 0) 258613570Sbrandon.potter@amd.com && !(sfdp->getFlags() & OS::TGT_O_NONBLOCK)) 258713570Sbrandon.potter@amd.com return SyscallReturn::retry(); 258813570Sbrandon.potter@amd.com 258913570Sbrandon.potter@amd.com if (lenPtr) { 259013570Sbrandon.potter@amd.com lenBufPtr = new BufferArg(lenPtr, sizeof(socklen_t)); 259113570Sbrandon.potter@amd.com lenBufPtr->copyIn(tc->getMemProxy()); 259213570Sbrandon.potter@amd.com memcpy(&addrLen, (socklen_t *)lenBufPtr->bufferPtr(), 259313570Sbrandon.potter@amd.com sizeof(socklen_t)); 259413570Sbrandon.potter@amd.com } 259513570Sbrandon.potter@amd.com 259613570Sbrandon.potter@amd.com if (addrPtr) { 259713570Sbrandon.potter@amd.com addrBufPtr = new BufferArg(addrPtr, sizeof(struct sockaddr)); 259813570Sbrandon.potter@amd.com addrBufPtr->copyIn(tc->getMemProxy()); 259913570Sbrandon.potter@amd.com memcpy(&sa, (struct sockaddr *)addrBufPtr->bufferPtr(), 260013570Sbrandon.potter@amd.com sizeof(struct sockaddr)); 260113570Sbrandon.potter@amd.com } 260213570Sbrandon.potter@amd.com 260313570Sbrandon.potter@amd.com host_fd = accept(sim_fd, &sa, &addrLen); 260413570Sbrandon.potter@amd.com 260513570Sbrandon.potter@amd.com if (host_fd == -1) 260613570Sbrandon.potter@amd.com return -errno; 260713570Sbrandon.potter@amd.com 260813570Sbrandon.potter@amd.com if (addrPtr) { 260913570Sbrandon.potter@amd.com memcpy(addrBufPtr->bufferPtr(), &sa, sizeof(sa)); 261013570Sbrandon.potter@amd.com addrBufPtr->copyOut(tc->getMemProxy()); 261113570Sbrandon.potter@amd.com delete(addrBufPtr); 261213570Sbrandon.potter@amd.com } 261313570Sbrandon.potter@amd.com 261413570Sbrandon.potter@amd.com if (lenPtr) { 261513570Sbrandon.potter@amd.com *(socklen_t *)lenBufPtr->bufferPtr() = addrLen; 261613570Sbrandon.potter@amd.com lenBufPtr->copyOut(tc->getMemProxy()); 261713570Sbrandon.potter@amd.com delete(lenBufPtr); 261813570Sbrandon.potter@amd.com } 261913570Sbrandon.potter@amd.com 262013570Sbrandon.potter@amd.com auto afdp = std::make_shared<SocketFDEntry>(host_fd, sfdp->_domain, 262113570Sbrandon.potter@amd.com sfdp->_type, sfdp->_protocol); 262213570Sbrandon.potter@amd.com return p->fds->allocFD(afdp); 262313570Sbrandon.potter@amd.com} 262413570Sbrandon.potter@amd.com 26251354SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 2626