syscall_emul.hh revision 11908
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#define NO_STAT64 (defined(__APPLE__) || defined(__OpenBSD__) || \ 4912018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__CYGWIN__) || \ 5012018Sandreas.sandberg@arm.com defined(__NetBSD__)) 5112018Sandreas.sandberg@arm.com 5212018Sandreas.sandberg@arm.com#define NO_STATFS (defined(__APPLE__) || defined(__OpenBSD__) || \ 5312018Sandreas.sandberg@arm.com defined(__FreeBSD__) || defined(__NetBSD__)) 5412018Sandreas.sandberg@arm.com 552064SN/A#define NO_FALLOCATE (defined(__APPLE__) || defined(__OpenBSD__) || \ 56360SN/A defined(__FreeBSD__) || defined(__NetBSD__)) 57360SN/A 58360SN/A/// 59360SN/A/// @file syscall_emul.hh 60360SN/A/// 61360SN/A/// This file defines objects used to emulate syscalls from the target 6213936SAndrea.Mondelli@ucf.edu/// application on the host machine. 6313933Sbrandon.potter@amd.com 6413933Sbrandon.potter@amd.com#ifdef __CYGWIN32__ 6513933Sbrandon.potter@amd.com#include <sys/fcntl.h> 6613936SAndrea.Mondelli@ucf.edu 6713936SAndrea.Mondelli@ucf.edu#endif 6813936SAndrea.Mondelli@ucf.edu#include <fcntl.h> 6913933Sbrandon.potter@amd.com#include <sys/mman.h> 7013933Sbrandon.potter@amd.com#include <sys/stat.h> 711809SN/A#if (NO_STATFS == 0) 7211800Sbrandon.potter@amd.com#include <sys/statfs.h> 7311392Sbrandon.potter@amd.com#else 741809SN/A#include <sys/mount.h> 7511392Sbrandon.potter@amd.com#endif 7613902Sbrandon.potter@amd.com#include <sys/time.h> 7713570Sbrandon.potter@amd.com#include <sys/uio.h> 7813902Sbrandon.potter@amd.com#include <unistd.h> 7911383Sbrandon.potter@amd.com 8013568Sbrandon.potter@amd.com#include <cerrno> 813113Sgblack@eecs.umich.edu#include <memory> 828229Snate@binkert.org#include <string> 8313570Sbrandon.potter@amd.com 848229Snate@binkert.org#include "arch/utility.hh" 8511594Santhony.gutierrez@amd.com#include "base/intmath.hh" 867075Snate@binkert.org#include "base/loader/object_file.hh" 878229Snate@binkert.org#include "base/misc.hh" 8811856Sbrandon.potter@amd.com#include "base/trace.hh" 897075Snate@binkert.org#include "base/types.hh" 90360SN/A#include "config/the_isa.hh" 9112461Sgabeblack@google.com#include "cpu/base.hh" 9211886Sbrandon.potter@amd.com#include "cpu/thread_context.hh" 9311800Sbrandon.potter@amd.com#include "mem/page_table.hh" 9411392Sbrandon.potter@amd.com#include "params/Process.hh" 9512334Sgabeblack@google.com#include "sim/emul_driver.hh" 961354SN/A#include "sim/process.hh" 976216Snate@binkert.org#include "sim/syscall_debug_macros.hh" 986658Snate@binkert.org#include "sim/syscall_desc.hh" 992474SN/A#include "sim/syscall_emul_buf.hh" 1002680Sktlim@umich.edu#include "sim/syscall_return.hh" 1018229Snate@binkert.org 10211886Sbrandon.potter@amd.com////////////////////////////////////////////////////////////////////// 10310496Ssteve.reinhardt@amd.com// 10411911SBrandon.Potter@amd.com// The following emulation functions are generic enough that they 1058229Snate@binkert.org// don't need to be recompiled for different emulated OS's. They are 10611794Sbrandon.potter@amd.com// defined in sim/syscall_emul.cc. 10711886Sbrandon.potter@amd.com// 10810497Ssteve.reinhardt@amd.com////////////////////////////////////////////////////////////////////// 10911794Sbrandon.potter@amd.com 110360SN/A 11113629SAndrea.Mondelli@ucf.edu/// Handler for unimplemented syscalls that we haven't thought about. 11213629SAndrea.Mondelli@ucf.eduSyscallReturn unimplementedFunc(SyscallDesc *desc, int num, 11313629SAndrea.Mondelli@ucf.edu Process *p, ThreadContext *tc); 11413629SAndrea.Mondelli@ucf.edu 115360SN/A/// Handler for unimplemented syscalls that we never intend to 116360SN/A/// implement (signal handling, etc.) and should not affect the correct 117360SN/A/// behavior of the program. Print a warning only if the appropriate 118360SN/A/// trace flag is enabled. Return success to the target program. 119360SN/ASyscallReturn ignoreFunc(SyscallDesc *desc, int num, 120360SN/A Process *p, ThreadContext *tc); 121360SN/A 122360SN/A// Target fallocateFunc() handler. 12313933Sbrandon.potter@amd.comSyscallReturn fallocateFunc(SyscallDesc *desc, int num, 124360SN/A Process *p, ThreadContext *tc); 125378SN/A 12613995Sbrandon.potter@amd.com/// Target exit() handler: terminate current context. 127378SN/ASyscallReturn exitFunc(SyscallDesc *desc, int num, 128378SN/A Process *p, ThreadContext *tc); 129378SN/A 130378SN/A/// Target exit_group() handler: terminate simulation. (exit all threads) 131378SN/ASyscallReturn exitGroupFunc(SyscallDesc *desc, int num, 13213995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 133360SN/A 13411760Sbrandon.potter@amd.com/// Target set_tid_address() handler. 13513995Sbrandon.potter@amd.comSyscallReturn setTidAddressFunc(SyscallDesc *desc, int num, 13611760Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1376109Ssanchezd@stanford.edu 13813995Sbrandon.potter@amd.com/// Target getpagesize() handler. 139378SN/ASyscallReturn getpagesizeFunc(SyscallDesc *desc, int num, 1406109Ssanchezd@stanford.edu Process *p, ThreadContext *tc); 14113995Sbrandon.potter@amd.com 1426109Ssanchezd@stanford.edu/// Target brk() handler: set brk address. 14311886Sbrandon.potter@amd.comSyscallReturn brkFunc(SyscallDesc *desc, int num, 14413995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 14511886Sbrandon.potter@amd.com 146378SN/A/// Target close() handler. 14713995Sbrandon.potter@amd.comSyscallReturn closeFunc(SyscallDesc *desc, int num, 148378SN/A Process *p, ThreadContext *tc); 1495748SSteve.Reinhardt@amd.com 15013995Sbrandon.potter@amd.com// Target read() handler. 151378SN/ASyscallReturn readFunc(SyscallDesc *desc, int num, 152378SN/A Process *p, ThreadContext *tc); 15313995Sbrandon.potter@amd.com 154378SN/A/// Target write() handler. 155378SN/ASyscallReturn writeFunc(SyscallDesc *desc, int num, 15613995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 157378SN/A 1584118Sgblack@eecs.umich.edu/// Target lseek() handler. 15913995Sbrandon.potter@amd.comSyscallReturn lseekFunc(SyscallDesc *desc, int num, 1604118Sgblack@eecs.umich.edu Process *p, ThreadContext *tc); 161378SN/A 16213995Sbrandon.potter@amd.com/// Target _llseek() handler. 163378SN/ASyscallReturn _llseekFunc(SyscallDesc *desc, int num, 16413568Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 16513995Sbrandon.potter@amd.com 16613568Sbrandon.potter@amd.com/// Target munmap() handler. 167378SN/ASyscallReturn munmapFunc(SyscallDesc *desc, int num, 16813995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 169360SN/A 1705513SMichael.Adler@intel.com/// Target gethostname() handler. 17113995Sbrandon.potter@amd.comSyscallReturn gethostnameFunc(SyscallDesc *desc, int num, 1725513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 17310203SAli.Saidi@ARM.com 17413995Sbrandon.potter@amd.com/// Target getcwd() handler. 17510203SAli.Saidi@ARM.comSyscallReturn getcwdFunc(SyscallDesc *desc, int num, 17613995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1775513SMichael.Adler@intel.com 178511SN/A/// Target readlink() handler. 17913995Sbrandon.potter@amd.comSyscallReturn readlinkFunc(SyscallDesc *desc, int num, 18010633Smichaelupton@gmail.com Process *p, ThreadContext *tc, 18113995Sbrandon.potter@amd.com int index = 0); 182511SN/ASyscallReturn readlinkFunc(SyscallDesc *desc, int num, 18312795Smattdsinclair@gmail.com Process *p, ThreadContext *tc); 18413995Sbrandon.potter@amd.com 18512795Smattdsinclair@gmail.com/// Target unlink() handler. 18612796Smattdsinclair@gmail.comSyscallReturn unlinkHelper(SyscallDesc *desc, int num, 18713995Sbrandon.potter@amd.com Process *p, ThreadContext *tc, 18812796Smattdsinclair@gmail.com int index); 1895513SMichael.Adler@intel.comSyscallReturn unlinkFunc(SyscallDesc *desc, int num, 19013995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 1915513SMichael.Adler@intel.com 19213031Sbrandon.potter@amd.com/// Target mkdir() handler. 19313995Sbrandon.potter@amd.comSyscallReturn mkdirFunc(SyscallDesc *desc, int num, 19413031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 19513031Sbrandon.potter@amd.com 19613995Sbrandon.potter@amd.com/// Target rename() handler. 19713031Sbrandon.potter@amd.comSyscallReturn renameFunc(SyscallDesc *desc, int num, 19813031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 19913995Sbrandon.potter@amd.com 20013031Sbrandon.potter@amd.com 201511SN/A/// Target truncate() handler. 20213995Sbrandon.potter@amd.comSyscallReturn truncateFunc(SyscallDesc *desc, int num, 2031706SN/A Process *p, ThreadContext *tc); 2041706SN/A 2051706SN/A 20613995Sbrandon.potter@amd.com/// Target ftruncate() handler. 2071706SN/ASyscallReturn ftruncateFunc(SyscallDesc *desc, int num, 2081706SN/A Process *p, ThreadContext *tc); 2091706SN/A 21013995Sbrandon.potter@amd.com 2111706SN/A/// Target truncate64() handler. 212511SN/ASyscallReturn truncate64Func(SyscallDesc *desc, int num, 2136703Svince@csl.cornell.edu Process *p, ThreadContext *tc); 21413995Sbrandon.potter@amd.com 2156703Svince@csl.cornell.edu/// Target ftruncate64() handler. 2166685Stjones1@inf.ed.ac.ukSyscallReturn ftruncate64Func(SyscallDesc *desc, int num, 21713995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2186685Stjones1@inf.ed.ac.uk 2196685Stjones1@inf.ed.ac.uk 2205513SMichael.Adler@intel.com/// Target umask() handler. 22113995Sbrandon.potter@amd.comSyscallReturn umaskFunc(SyscallDesc *desc, int num, 2225513SMichael.Adler@intel.com Process *p, ThreadContext *tc); 22311885Sbrandon.potter@amd.com 22413995Sbrandon.potter@amd.com/// Target gettid() handler. 2255513SMichael.Adler@intel.comSyscallReturn gettidFunc(SyscallDesc *desc, int num, 2261999SN/A Process *p, ThreadContext *tc); 22713995Sbrandon.potter@amd.com 2281999SN/A/// Target chown() handler. 22911885Sbrandon.potter@amd.comSyscallReturn chownFunc(SyscallDesc *desc, int num, 23013995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2311999SN/A 2321999SN/A/// Target setpgid() handler. 23313995Sbrandon.potter@amd.comSyscallReturn setpgidFunc(SyscallDesc *desc, int num, 2341999SN/A Process *p, ThreadContext *tc); 2353079Sstever@eecs.umich.edu 23613995Sbrandon.potter@amd.com/// Target fchown() handler. 2373079Sstever@eecs.umich.eduSyscallReturn fchownFunc(SyscallDesc *desc, int num, 23811908SBrandon.Potter@amd.com Process *p, ThreadContext *tc); 23913995Sbrandon.potter@amd.com 24011908SBrandon.Potter@amd.com/// Target dup() handler. 24111875Sbrandon.potter@amd.comSyscallReturn dupFunc(SyscallDesc *desc, int num, 24213995Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 2432093SN/A 2442687Sksewell@umich.edu/// Target dup2() handler. 24513995Sbrandon.potter@amd.comSyscallReturn dup2Func(SyscallDesc *desc, int num, 2462687Sksewell@umich.edu Process *process, ThreadContext *tc); 2472238SN/A 24813995Sbrandon.potter@amd.com/// Target fcntl() handler. 2492238SN/ASyscallReturn fcntlFunc(SyscallDesc *desc, int num, 25011908SBrandon.Potter@amd.com Process *process, ThreadContext *tc); 25113995Sbrandon.potter@amd.com 25211908SBrandon.Potter@amd.com/// Target fcntl64() handler. 25311908SBrandon.Potter@amd.comSyscallReturn fcntl64Func(SyscallDesc *desc, int num, 25413995Sbrandon.potter@amd.com Process *process, ThreadContext *tc); 25513995Sbrandon.potter@amd.com 25611908SBrandon.Potter@amd.com/// Target setuid() handler. 2572238SN/ASyscallReturn setuidFunc(SyscallDesc *desc, int num, 25813995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 2592238SN/A 26013571Sbrandon.potter@amd.com/// Target pipe() handler. 26113995Sbrandon.potter@amd.comSyscallReturn pipeFunc(SyscallDesc *desc, int num, 26213571Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 26313568Sbrandon.potter@amd.com 26413995Sbrandon.potter@amd.com/// Internal pipe() handler. 26513568Sbrandon.potter@amd.comSyscallReturn pipeImpl(SyscallDesc *desc, int num, Process *p, 26613568Sbrandon.potter@amd.com ThreadContext *tc, bool pseudoPipe); 26713995Sbrandon.potter@amd.com 26813568Sbrandon.potter@amd.com/// Target getpid() handler. 26913568Sbrandon.potter@amd.comSyscallReturn getpidFunc(SyscallDesc *desc, int num, 27013995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 27113568Sbrandon.potter@amd.com 27213448Sciro.santilli@arm.com/// Target getuid() handler. 27313031Sbrandon.potter@amd.comSyscallReturn getuidFunc(SyscallDesc *desc, int num, 27413995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 27513448Sciro.santilli@arm.com 27613031Sbrandon.potter@amd.com/// Target getgid() handler. 27713539Sjavier.setoain@arm.comSyscallReturn getgidFunc(SyscallDesc *desc, int num, 27813539Sjavier.setoain@arm.com Process *p, ThreadContext *tc); 27913995Sbrandon.potter@amd.com 28013539Sjavier.setoain@arm.com/// Target getppid() handler. 28113539Sjavier.setoain@arm.comSyscallReturn getppidFunc(SyscallDesc *desc, int num, 28213569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 28313995Sbrandon.potter@amd.com 28413569Sbrandon.potter@amd.com/// Target geteuid() handler. 28513569Sbrandon.potter@amd.comSyscallReturn geteuidFunc(SyscallDesc *desc, int num, 28613995Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 28713569Sbrandon.potter@amd.com 28813569Sbrandon.potter@amd.com/// Target getegid() handler. 28913995Sbrandon.potter@amd.comSyscallReturn getegidFunc(SyscallDesc *desc, int num, 29013569Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 29113569Sbrandon.potter@amd.com 29213995Sbrandon.potter@amd.com/// Target access() handler 29313569Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 29413031Sbrandon.potter@amd.com Process *p, ThreadContext *tc); 29513995Sbrandon.potter@amd.comSyscallReturn accessFunc(SyscallDesc *desc, int num, 2962238SN/A Process *p, ThreadContext *tc, 2972238SN/A int index); 29813995Sbrandon.potter@amd.com 2992238SN/A/// Futex system call 3002238SN/A/// Implemented by Daniel Sanchez 30113995Sbrandon.potter@amd.com/// Used by printf's in multi-threaded apps 3022238SN/Atemplate <class OS> 3032238SN/ASyscallReturn 30413995Sbrandon.potter@amd.comfutexFunc(SyscallDesc *desc, int callnum, Process *process, 3052238SN/A ThreadContext *tc) 3062238SN/A{ 30713995Sbrandon.potter@amd.com int index_uaddr = 0; 3082238SN/A int index_op = 1; 3099455Smitch.hayenga+gem5@gmail.com int index_val = 2; 31013995Sbrandon.potter@amd.com int index_timeout = 3; 31113995Sbrandon.potter@amd.com 31211851Sbrandon.potter@amd.com uint64_t uaddr = process->getSyscallArg(tc, index_uaddr); 3139455Smitch.hayenga+gem5@gmail.com int op = process->getSyscallArg(tc, index_op); 31413571Sbrandon.potter@amd.com int val = process->getSyscallArg(tc, index_val); 31513995Sbrandon.potter@amd.com uint64_t timeout = process->getSyscallArg(tc, index_timeout); 31613571Sbrandon.potter@amd.com 31713571Sbrandon.potter@amd.com std::map<uint64_t, std::list<ThreadContext *> * > 31813995Sbrandon.potter@amd.com &futex_map = tc->getSystemPtr()->futexMap; 31913571Sbrandon.potter@amd.com 32013571Sbrandon.potter@amd.com DPRINTF(SyscallVerbose, "futex: Address=%llx, op=%d, val=%d\n", 32113995Sbrandon.potter@amd.com uaddr, op, val); 32213571Sbrandon.potter@amd.com 3239112Smarc.orr@gmail.com op &= ~OS::TGT_FUTEX_PRIVATE_FLAG; 32411906SBrandon.Potter@amd.com 32511906SBrandon.Potter@amd.com if (op == OS::TGT_FUTEX_WAIT) { 3269112Smarc.orr@gmail.com if (timeout != 0) { 3279112Smarc.orr@gmail.com warn("futex: FUTEX_WAIT with non-null timeout unimplemented;" 32813995Sbrandon.potter@amd.com "we'll wait indefinitely"); 3299112Smarc.orr@gmail.com } 33011911SBrandon.Potter@amd.com 3319112Smarc.orr@gmail.com uint8_t *buf = new uint8_t[sizeof(int)]; 33211911SBrandon.Potter@amd.com tc->getMemProxy().readBlob((Addr)uaddr, buf, (int)sizeof(int)); 33313995Sbrandon.potter@amd.com int mem_val = *((int *)buf); 33413995Sbrandon.potter@amd.com delete[] buf; 33511911SBrandon.Potter@amd.com 33611911SBrandon.Potter@amd.com if (val != mem_val) { 33711911SBrandon.Potter@amd.com DPRINTF(SyscallVerbose, "futex: FUTEX_WAKE, read: %d, " 33813642Sqtt2@cornell.edu "expected: %d\n", mem_val, val); 33913642Sqtt2@cornell.edu return -OS::TGT_EWOULDBLOCK; 34013642Sqtt2@cornell.edu } 3419112Smarc.orr@gmail.com 34211911SBrandon.Potter@amd.com // Queue the thread context 34311911SBrandon.Potter@amd.com std::list<ThreadContext *> * tcWaitList; 34411911SBrandon.Potter@amd.com if (futex_map.count(uaddr)) { 34511911SBrandon.Potter@amd.com tcWaitList = futex_map.find(uaddr)->second; 3469238Slluc.alvarez@bsc.es } else { 34713642Sqtt2@cornell.edu tcWaitList = new std::list<ThreadContext *>(); 3489112Smarc.orr@gmail.com futex_map.insert(std::pair< uint64_t, 34911911SBrandon.Potter@amd.com std::list<ThreadContext *> * >(uaddr, tcWaitList)); 3509112Smarc.orr@gmail.com } 35113642Sqtt2@cornell.edu tcWaitList->push_back(tc); 35211911SBrandon.Potter@amd.com DPRINTF(SyscallVerbose, "futex: FUTEX_WAIT, suspending calling thread " 35311911SBrandon.Potter@amd.com "context on address 0x%lx\n", uaddr); 35414024Sgabeblack@google.com tc->suspend(); 35511911SBrandon.Potter@amd.com return 0; 3569112Smarc.orr@gmail.com } else if (op == OS::TGT_FUTEX_WAKE){ 35711911SBrandon.Potter@amd.com int wokenUp = 0; 35811911SBrandon.Potter@amd.com std::list<ThreadContext *> * tcWaitList; 35911911SBrandon.Potter@amd.com if (futex_map.count(uaddr)) { 36011911SBrandon.Potter@amd.com tcWaitList = futex_map.find(uaddr)->second; 36111911SBrandon.Potter@amd.com while (tcWaitList->size() > 0 && wokenUp < val) { 36211911SBrandon.Potter@amd.com tcWaitList->front()->activate(); 3639112Smarc.orr@gmail.com tcWaitList->pop_front(); 3649112Smarc.orr@gmail.com wokenUp++; 36513642Sqtt2@cornell.edu } 36613642Sqtt2@cornell.edu if (tcWaitList->empty()) { 36713642Sqtt2@cornell.edu futex_map.erase(uaddr); 36813642Sqtt2@cornell.edu delete tcWaitList; 36913642Sqtt2@cornell.edu } 37011911SBrandon.Potter@amd.com } 3719112Smarc.orr@gmail.com DPRINTF(SyscallVerbose, "futex: FUTEX_WAKE, activated %d waiting " 37211911SBrandon.Potter@amd.com "thread context on address 0x%lx\n", 37311911SBrandon.Potter@amd.com wokenUp, uaddr); 37413642Sqtt2@cornell.edu return wokenUp; 37513642Sqtt2@cornell.edu } else { 37613650Smw828@cornell.edu warn("futex: op %d is not implemented, just returning...", op); 37713650Smw828@cornell.edu return 0; 37813650Smw828@cornell.edu } 37913650Smw828@cornell.edu 38013650Smw828@cornell.edu} 38114024Sgabeblack@google.com 38213650Smw828@cornell.edu 38313650Smw828@cornell.edu/// Pseudo Funcs - These functions use a different return convension, 38413650Smw828@cornell.edu/// returning a second value in a register other than the normal return register 38513650Smw828@cornell.eduSyscallReturn pipePseudoFunc(SyscallDesc *desc, int num, 38613650Smw828@cornell.edu Process *process, ThreadContext *tc); 38713650Smw828@cornell.edu 38813650Smw828@cornell.edu/// Target getpidPseudo() handler. 38913650Smw828@cornell.eduSyscallReturn getpidPseudoFunc(SyscallDesc *desc, int num, 39013651Smw828@cornell.edu Process *p, ThreadContext *tc); 39113651Smw828@cornell.edu 39213651Smw828@cornell.edu/// Target getuidPseudo() handler. 39313651Smw828@cornell.eduSyscallReturn getuidPseudoFunc(SyscallDesc *desc, int num, 39413651Smw828@cornell.edu Process *p, ThreadContext *tc); 39513651Smw828@cornell.edu 39613651Smw828@cornell.edu/// Target getgidPseudo() handler. 39713651Smw828@cornell.eduSyscallReturn getgidPseudoFunc(SyscallDesc *desc, int num, 39813651Smw828@cornell.edu Process *p, ThreadContext *tc); 39913651Smw828@cornell.edu 40013651Smw828@cornell.edu 40113651Smw828@cornell.edu/// A readable name for 1,000,000, for converting microseconds to seconds. 40213651Smw828@cornell.educonst int one_million = 1000000; 40313651Smw828@cornell.edu/// A readable name for 1,000,000,000, for converting nanoseconds to seconds. 40413651Smw828@cornell.educonst int one_billion = 1000000000; 40513651Smw828@cornell.edu 40613651Smw828@cornell.edu/// Approximate seconds since the epoch (1/1/1970). About a billion, 40713651Smw828@cornell.edu/// by my reckoning. We want to keep this a constant (not use the 40813651Smw828@cornell.edu/// real-world time) to keep simulations repeatable. 40913651Smw828@cornell.educonst unsigned seconds_since_epoch = 1000000000; 41013651Smw828@cornell.edu 41113651Smw828@cornell.edu/// Helper function to convert current elapsed time to seconds and 41213651Smw828@cornell.edu/// microseconds. 41313651Smw828@cornell.edutemplate <class T1, class T2> 41414024Sgabeblack@google.comvoid 41513651Smw828@cornell.edugetElapsedTimeMicro(T1 &sec, T2 &usec) 41613651Smw828@cornell.edu{ 41713651Smw828@cornell.edu uint64_t elapsed_usecs = curTick() / SimClock::Int::us; 41813651Smw828@cornell.edu sec = elapsed_usecs / one_million; 41913651Smw828@cornell.edu usec = elapsed_usecs % one_million; 42013651Smw828@cornell.edu} 42113651Smw828@cornell.edu 42213651Smw828@cornell.edu/// Helper function to convert current elapsed time to seconds and 42313651Smw828@cornell.edu/// nanoseconds. 42413651Smw828@cornell.edutemplate <class T1, class T2> 42513651Smw828@cornell.eduvoid 42613651Smw828@cornell.edugetElapsedTimeNano(T1 &sec, T2 &nsec) 42713651Smw828@cornell.edu{ 42813651Smw828@cornell.edu uint64_t elapsed_nsecs = curTick() / SimClock::Int::ns; 42913651Smw828@cornell.edu sec = elapsed_nsecs / one_billion; 43013651Smw828@cornell.edu nsec = elapsed_nsecs % one_billion; 43113651Smw828@cornell.edu} 43213651Smw828@cornell.edu 43313651Smw828@cornell.edu////////////////////////////////////////////////////////////////////// 43413651Smw828@cornell.edu// 43513651Smw828@cornell.edu// The following emulation functions are generic, but need to be 43613651Smw828@cornell.edu// templated to account for differences in types, constants, etc. 43713651Smw828@cornell.edu// 43814024Sgabeblack@google.com////////////////////////////////////////////////////////////////////// 43913651Smw828@cornell.edu 44013651Smw828@cornell.edu typedef struct statfs hst_statfs; 44113651Smw828@cornell.edu#if NO_STAT64 44213651Smw828@cornell.edu typedef struct stat hst_stat; 44313651Smw828@cornell.edu typedef struct stat hst_stat64; 44413651Smw828@cornell.edu#else 44513651Smw828@cornell.edu typedef struct stat hst_stat; 44613651Smw828@cornell.edu typedef struct stat64 hst_stat64; 44713651Smw828@cornell.edu#endif 44813651Smw828@cornell.edu 44913651Smw828@cornell.edu//// Helper function to convert a host stat buffer to a target stat 45013651Smw828@cornell.edu//// buffer. Also copies the target buffer out to the simulated 45113651Smw828@cornell.edu//// memory space. Used by stat(), fstat(), and lstat(). 45213651Smw828@cornell.edu 45313651Smw828@cornell.edutemplate <typename target_stat, typename host_stat> 45413651Smw828@cornell.edustatic void 45513651Smw828@cornell.educonvertStatBuf(target_stat &tgt, host_stat *host, bool fakeTTY = false) 45613651Smw828@cornell.edu{ 45713651Smw828@cornell.edu using namespace TheISA; 45813651Smw828@cornell.edu 45913651Smw828@cornell.edu if (fakeTTY) 46013651Smw828@cornell.edu tgt->st_dev = 0xA; 4619112Smarc.orr@gmail.com else 46211911SBrandon.Potter@amd.com tgt->st_dev = host->st_dev; 46311911SBrandon.Potter@amd.com tgt->st_dev = TheISA::htog(tgt->st_dev); 4649112Smarc.orr@gmail.com tgt->st_ino = host->st_ino; 4659112Smarc.orr@gmail.com tgt->st_ino = TheISA::htog(tgt->st_ino); 4662238SN/A tgt->st_mode = host->st_mode; 4672238SN/A if (fakeTTY) { 4682238SN/A // Claim to be a character device 46913995Sbrandon.potter@amd.com tgt->st_mode &= ~S_IFMT; // Clear S_IFMT 4702238SN/A tgt->st_mode |= S_IFCHR; // Set S_IFCHR 4712238SN/A } 47213995Sbrandon.potter@amd.com tgt->st_mode = TheISA::htog(tgt->st_mode); 4732238SN/A tgt->st_nlink = host->st_nlink; 4742238SN/A tgt->st_nlink = TheISA::htog(tgt->st_nlink); 47513995Sbrandon.potter@amd.com tgt->st_uid = host->st_uid; 4762238SN/A tgt->st_uid = TheISA::htog(tgt->st_uid); 4772238SN/A tgt->st_gid = host->st_gid; 47813995Sbrandon.potter@amd.com tgt->st_gid = TheISA::htog(tgt->st_gid); 4792238SN/A if (fakeTTY) 4802238SN/A tgt->st_rdev = 0x880d; 4811354SN/A else 4821354SN/A tgt->st_rdev = host->st_rdev; 48310796Sbrandon.potter@amd.com tgt->st_rdev = TheISA::htog(tgt->st_rdev); 48410796Sbrandon.potter@amd.com tgt->st_size = host->st_size; 4851354SN/A tgt->st_size = TheISA::htog(tgt->st_size); 4861354SN/A tgt->st_atimeX = host->st_atime; 4871354SN/A tgt->st_atimeX = TheISA::htog(tgt->st_atimeX); 4881354SN/A tgt->st_mtimeX = host->st_mtime; 4891354SN/A tgt->st_mtimeX = TheISA::htog(tgt->st_mtimeX); 4901354SN/A tgt->st_ctimeX = host->st_ctime; 4911354SN/A tgt->st_ctimeX = TheISA::htog(tgt->st_ctimeX); 4921354SN/A // Force the block size to be 8KB. This helps to ensure buffered io works 4931354SN/A // consistently across different hosts. 4941354SN/A tgt->st_blksize = 0x2000; 49510796Sbrandon.potter@amd.com tgt->st_blksize = TheISA::htog(tgt->st_blksize); 4961354SN/A tgt->st_blocks = host->st_blocks; 49710796Sbrandon.potter@amd.com tgt->st_blocks = TheISA::htog(tgt->st_blocks); 4981354SN/A} 4991354SN/A 5001354SN/A// Same for stat64 5011354SN/A 50210796Sbrandon.potter@amd.comtemplate <typename target_stat, typename host_stat64> 50310796Sbrandon.potter@amd.comstatic void 50410796Sbrandon.potter@amd.comconvertStat64Buf(target_stat &tgt, host_stat64 *host, bool fakeTTY = false) 50510796Sbrandon.potter@amd.com{ 50610796Sbrandon.potter@amd.com using namespace TheISA; 50710796Sbrandon.potter@amd.com 50810796Sbrandon.potter@amd.com convertStatBuf<target_stat, host_stat64>(tgt, host, fakeTTY); 50910796Sbrandon.potter@amd.com#if defined(STAT_HAVE_NSEC) 51010796Sbrandon.potter@amd.com tgt->st_atime_nsec = host->st_atime_nsec; 51110796Sbrandon.potter@amd.com tgt->st_atime_nsec = TheISA::htog(tgt->st_atime_nsec); 51210796Sbrandon.potter@amd.com tgt->st_mtime_nsec = host->st_mtime_nsec; 513360SN/A tgt->st_mtime_nsec = TheISA::htog(tgt->st_mtime_nsec); 514360SN/A tgt->st_ctime_nsec = host->st_ctime_nsec; 515360SN/A tgt->st_ctime_nsec = TheISA::htog(tgt->st_ctime_nsec); 516360SN/A#else 517360SN/A tgt->st_atime_nsec = 0; 518360SN/A tgt->st_mtime_nsec = 0; 519360SN/A tgt->st_ctime_nsec = 0; 52011759Sbrandon.potter@amd.com#endif 5213113Sgblack@eecs.umich.edu} 5223113Sgblack@eecs.umich.edu 5233113Sgblack@eecs.umich.edu// Here are a couple of convenience functions 5243113Sgblack@eecs.umich.edutemplate<class OS> 5253113Sgblack@eecs.umich.edustatic void 5263113Sgblack@eecs.umich.educopyOutStatBuf(SETranslatingPortProxy &mem, Addr addr, 5273113Sgblack@eecs.umich.edu hst_stat *host, bool fakeTTY = false) 5283113Sgblack@eecs.umich.edu{ 5293113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat> tgt_stat_buf; 5303113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5313113Sgblack@eecs.umich.edu convertStatBuf<tgt_stat_buf, hst_stat>(tgt, host, fakeTTY); 5323113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5333113Sgblack@eecs.umich.edu} 53412032Sandreas.sandberg@arm.com 5353113Sgblack@eecs.umich.edutemplate<class OS> 5363113Sgblack@eecs.umich.edustatic void 5374189Sgblack@eecs.umich.educopyOutStat64Buf(SETranslatingPortProxy &mem, Addr addr, 5384189Sgblack@eecs.umich.edu hst_stat64 *host, bool fakeTTY = false) 5393113Sgblack@eecs.umich.edu{ 5403113Sgblack@eecs.umich.edu typedef TypedBufferArg<typename OS::tgt_stat64> tgt_stat_buf; 5413113Sgblack@eecs.umich.edu tgt_stat_buf tgt(addr); 5423113Sgblack@eecs.umich.edu convertStat64Buf<tgt_stat_buf, hst_stat64>(tgt, host, fakeTTY); 5438737Skoansin.tan@gmail.com tgt.copyOut(mem); 5443113Sgblack@eecs.umich.edu} 5458737Skoansin.tan@gmail.com 5463277Sgblack@eecs.umich.edutemplate <class OS> 5475515SMichael.Adler@intel.comstatic void 5485515SMichael.Adler@intel.comcopyOutStatfsBuf(SETranslatingPortProxy &mem, Addr addr, 5495515SMichael.Adler@intel.com hst_statfs *host) 5505515SMichael.Adler@intel.com{ 5515515SMichael.Adler@intel.com TypedBufferArg<typename OS::tgt_statfs> tgt(addr); 5528737Skoansin.tan@gmail.com 5533277Sgblack@eecs.umich.edu tgt->f_type = TheISA::htog(host->f_type); 5548737Skoansin.tan@gmail.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5553277Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_iosize); 5568737Skoansin.tan@gmail.com#else 5573277Sgblack@eecs.umich.edu tgt->f_bsize = TheISA::htog(host->f_bsize); 5588737Skoansin.tan@gmail.com#endif 5593113Sgblack@eecs.umich.edu tgt->f_blocks = TheISA::htog(host->f_blocks); 5603113Sgblack@eecs.umich.edu tgt->f_bfree = TheISA::htog(host->f_bfree); 5613113Sgblack@eecs.umich.edu tgt->f_bavail = TheISA::htog(host->f_bavail); 5623113Sgblack@eecs.umich.edu tgt->f_files = TheISA::htog(host->f_files); 5638737Skoansin.tan@gmail.com tgt->f_ffree = TheISA::htog(host->f_ffree); 5643113Sgblack@eecs.umich.edu memcpy(&tgt->f_fsid, &host->f_fsid, sizeof(host->f_fsid)); 5658737Skoansin.tan@gmail.com#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 5663114Sgblack@eecs.umich.edu tgt->f_namelen = TheISA::htog(host->f_namemax); 5678737Skoansin.tan@gmail.com tgt->f_frsize = TheISA::htog(host->f_bsize); 5683114Sgblack@eecs.umich.edu#elif defined(__APPLE__) 5698737Skoansin.tan@gmail.com tgt->f_namelen = 0; 5703114Sgblack@eecs.umich.edu tgt->f_frsize = 0; 5718737Skoansin.tan@gmail.com#else 57211906SBrandon.Potter@amd.com tgt->f_namelen = TheISA::htog(host->f_namelen); 5734061Sgblack@eecs.umich.edu tgt->f_frsize = TheISA::htog(host->f_frsize); 5744061Sgblack@eecs.umich.edu#endif 5758737Skoansin.tan@gmail.com#if defined(__linux__) 5763113Sgblack@eecs.umich.edu memcpy(&tgt->f_spare, &host->f_spare, sizeof(host->f_spare)); 5778737Skoansin.tan@gmail.com#else 5783113Sgblack@eecs.umich.edu /* 5793113Sgblack@eecs.umich.edu * The fields are different sizes per OS. Don't bother with 5803113Sgblack@eecs.umich.edu * f_spare or f_reserved on non-Linux for now. 5813113Sgblack@eecs.umich.edu */ 5823113Sgblack@eecs.umich.edu memset(&tgt->f_spare, 0, sizeof(tgt->f_spare)); 58312032Sandreas.sandberg@arm.com#endif 5843113Sgblack@eecs.umich.edu 5853113Sgblack@eecs.umich.edu tgt.copyOut(mem); 5864189Sgblack@eecs.umich.edu} 5874189Sgblack@eecs.umich.edu 5883113Sgblack@eecs.umich.edu/// Target ioctl() handler. For the most part, programs call ioctl() 5893113Sgblack@eecs.umich.edu/// only to find out if their stdout is a tty, to determine whether to 5903113Sgblack@eecs.umich.edu/// do line or block buffering. We always claim that output fds are 5918737Skoansin.tan@gmail.com/// not TTYs to provide repeatable results. 5923113Sgblack@eecs.umich.edutemplate <class OS> 5938737Skoansin.tan@gmail.comSyscallReturn 5943113Sgblack@eecs.umich.eduioctlFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 5958737Skoansin.tan@gmail.com{ 5963113Sgblack@eecs.umich.edu int index = 0; 5973113Sgblack@eecs.umich.edu int tgt_fd = p->getSyscallArg(tc, index); 5983113Sgblack@eecs.umich.edu unsigned req = p->getSyscallArg(tc, index); 5993113Sgblack@eecs.umich.edu 6003113Sgblack@eecs.umich.edu DPRINTF(SyscallVerbose, "ioctl(%d, 0x%x, ...)\n", tgt_fd, req); 6013113Sgblack@eecs.umich.edu 6023113Sgblack@eecs.umich.edu if (OS::isTtyReq(req)) 60311906SBrandon.Potter@amd.com return -ENOTTY; 6043113Sgblack@eecs.umich.edu 60512032Sandreas.sandberg@arm.com auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>((*p->fds)[tgt_fd]); 60614020Sgabeblack@google.com if (!dfdp) 60711906SBrandon.Potter@amd.com return -EBADF; 6083113Sgblack@eecs.umich.edu 6093113Sgblack@eecs.umich.edu /** 6103113Sgblack@eecs.umich.edu * If the driver is valid, issue the ioctl through it. Otherwise, 6113113Sgblack@eecs.umich.edu * there's an implicit assumption that the device is a TTY type and we 6123113Sgblack@eecs.umich.edu * return that we do not have a valid TTY. 6133113Sgblack@eecs.umich.edu */ 6143113Sgblack@eecs.umich.edu EmulatedDriver *emul_driver = dfdp->getDriver(); 6153113Sgblack@eecs.umich.edu if (emul_driver) 61612032Sandreas.sandberg@arm.com return emul_driver->ioctl(p, tc, req); 61714020Sgabeblack@google.com 61811906SBrandon.Potter@amd.com /** 6193113Sgblack@eecs.umich.edu * For lack of a better return code, return ENOTTY. Ideally, we should 6203113Sgblack@eecs.umich.edu * return something better here, but at least we issue the warning. 6213113Sgblack@eecs.umich.edu */ 6226686Stjones1@inf.ed.ac.uk warn("Unsupported ioctl call (return ENOTTY): ioctl(%d, 0x%x, ...) @ \n", 6233113Sgblack@eecs.umich.edu tgt_fd, req, tc->pcState()); 6243113Sgblack@eecs.umich.edu return -ENOTTY; 6253113Sgblack@eecs.umich.edu} 62611759Sbrandon.potter@amd.com 62712032Sandreas.sandberg@arm.comtemplate <class OS> 62814020Sgabeblack@google.comSyscallReturn 62911759Sbrandon.potter@amd.comopenImpl(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc, 63011759Sbrandon.potter@amd.com bool isopenat) 63111759Sbrandon.potter@amd.com{ 63211759Sbrandon.potter@amd.com int index = 0; 63311812Sbaz21@cam.ac.uk int tgt_dirfd = -1; 63411812Sbaz21@cam.ac.uk 63511812Sbaz21@cam.ac.uk /** 63611759Sbrandon.potter@amd.com * If using the openat variant, read in the target directory file 63711812Sbaz21@cam.ac.uk * descriptor from the simulated process. 63811759Sbrandon.potter@amd.com */ 63911759Sbrandon.potter@amd.com if (isopenat) 64011759Sbrandon.potter@amd.com tgt_dirfd = p->getSyscallArg(tc, index); 64111759Sbrandon.potter@amd.com 64211759Sbrandon.potter@amd.com /** 64311759Sbrandon.potter@amd.com * Retrieve the simulated process' memory proxy and then read in the path 64411759Sbrandon.potter@amd.com * string from that memory space into the host's working memory space. 64511812Sbaz21@cam.ac.uk */ 64611812Sbaz21@cam.ac.uk std::string path; 64711812Sbaz21@cam.ac.uk if (!tc->getMemProxy().tryReadString(path, p->getSyscallArg(tc, index))) 64811812Sbaz21@cam.ac.uk return -EFAULT; 64911812Sbaz21@cam.ac.uk 65011812Sbaz21@cam.ac.uk#ifdef __CYGWIN32__ 65111812Sbaz21@cam.ac.uk int host_flags = O_BINARY; 65211759Sbrandon.potter@amd.com#else 65311759Sbrandon.potter@amd.com int host_flags = 0; 65411812Sbaz21@cam.ac.uk#endif 65511812Sbaz21@cam.ac.uk /** 65611759Sbrandon.potter@amd.com * Translate target flags into host flags. Flags exist which are not 65711812Sbaz21@cam.ac.uk * ported between architectures which can cause check failures. 65811812Sbaz21@cam.ac.uk */ 65911812Sbaz21@cam.ac.uk int tgt_flags = p->getSyscallArg(tc, index); 66011812Sbaz21@cam.ac.uk for (int i = 0; i < OS::NUM_OPEN_FLAGS; i++) { 66111812Sbaz21@cam.ac.uk if (tgt_flags & OS::openFlagTable[i].tgtFlag) { 66211812Sbaz21@cam.ac.uk tgt_flags &= ~OS::openFlagTable[i].tgtFlag; 66311812Sbaz21@cam.ac.uk host_flags |= OS::openFlagTable[i].hostFlag; 66411759Sbrandon.potter@amd.com } 66511759Sbrandon.potter@amd.com } 66611759Sbrandon.potter@amd.com if (tgt_flags) { 66711759Sbrandon.potter@amd.com warn("open%s: cannot decode flags 0x%x", 668378SN/A isopenat ? "at" : "", tgt_flags); 669378SN/A } 6709141Smarc.orr@gmail.com#ifdef __CYGWIN32__ 6719141Smarc.orr@gmail.com host_flags |= O_BINARY; 672360SN/A#endif 6731450SN/A 67413995Sbrandon.potter@amd.com int mode = p->getSyscallArg(tc, index); 675360SN/A 6766701Sgblack@eecs.umich.edu /** 67713995Sbrandon.potter@amd.com * If the simulated process called open or openat with AT_FDCWD specified, 67813995Sbrandon.potter@amd.com * take the current working directory value which was passed into the 67911856Sbrandon.potter@amd.com * process class as a Python parameter and append the current path to 68011856Sbrandon.potter@amd.com * create a full path. 681360SN/A * Otherwise, openat with a valid target directory file descriptor has 68213907Salexandru.dutu@amd.com * been called. If the path option, which was passed in as a parameter, 683360SN/A * is not absolute, retrieve the directory file descriptor's path and 68411856Sbrandon.potter@amd.com * prepend it to the path passed in as a parameter. 68511856Sbrandon.potter@amd.com * In every case, we should have a full path (which is relevant to the 68610496Ssteve.reinhardt@amd.com * host) to work with after this block has been passed. 68711856Sbrandon.potter@amd.com */ 68813902Sbrandon.potter@amd.com if (!isopenat || (isopenat && tgt_dirfd == OS::TGT_AT_FDCWD)) { 68913902Sbrandon.potter@amd.com path = p->fullPath(path); 69013902Sbrandon.potter@amd.com } else if (!startswith(path, "/")) { 69113995Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = ((*p->fds)[tgt_dirfd]); 69213902Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 69313902Sbrandon.potter@amd.com if (!ffdp) 69413902Sbrandon.potter@amd.com return -EBADF; 69513902Sbrandon.potter@amd.com path.insert(0, ffdp->getFileName()); 69613902Sbrandon.potter@amd.com } 69713902Sbrandon.potter@amd.com 69813902Sbrandon.potter@amd.com /** 69913902Sbrandon.potter@amd.com * Since this is an emulated environment, we create pseudo file 70013902Sbrandon.potter@amd.com * descriptors for device requests that have been registered with 70113902Sbrandon.potter@amd.com * the process class through Python; this allows us to create a file 70214024Sgabeblack@google.com * descriptor for subsequent ioctl or mmap calls. 70313902Sbrandon.potter@amd.com */ 70413902Sbrandon.potter@amd.com if (startswith(path, "/dev/")) { 70513902Sbrandon.potter@amd.com std::string filename = path.substr(strlen("/dev/")); 70613902Sbrandon.potter@amd.com EmulatedDriver *drv = p->findDriver(filename); 70714024Sgabeblack@google.com if (drv) { 70813902Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: passing call to " 70913902Sbrandon.potter@amd.com "driver open with path[%s]\n", 71013902Sbrandon.potter@amd.com isopenat ? "at" : "", path.c_str()); 71113902Sbrandon.potter@amd.com return drv->open(p, tc, mode, host_flags); 71213902Sbrandon.potter@amd.com } 71313902Sbrandon.potter@amd.com /** 71414024Sgabeblack@google.com * Fall through here for pass through to host devices, such 71514024Sgabeblack@google.com * as /dev/zero 71613902Sbrandon.potter@amd.com */ 71713902Sbrandon.potter@amd.com } 71813902Sbrandon.potter@amd.com 71913902Sbrandon.potter@amd.com /** 72013902Sbrandon.potter@amd.com * Some special paths and files cannot be called on the host and need 72113936SAndrea.Mondelli@ucf.edu * to be handled as special cases inside the simulator. 72213902Sbrandon.potter@amd.com * If the full path that was created above does not match any of the 72313902Sbrandon.potter@amd.com * special cases, pass it through to the open call on the host to let 72413902Sbrandon.potter@amd.com * the host open the file on our behalf. 72513902Sbrandon.potter@amd.com * If the host cannot open the file, return the host's error code back 72613936SAndrea.Mondelli@ucf.edu * through the system call to the simulated process. 72713902Sbrandon.potter@amd.com */ 72813902Sbrandon.potter@amd.com int sim_fd = -1; 72913902Sbrandon.potter@amd.com std::vector<std::string> special_paths = 73013902Sbrandon.potter@amd.com { "/proc/", "/system/", "/sys/", "/platform/", "/etc/passwd" }; 73113902Sbrandon.potter@amd.com for (auto entry : special_paths) { 73214024Sgabeblack@google.com if (startswith(path, entry)) 73313902Sbrandon.potter@amd.com sim_fd = OS::openSpecialFile(path, p, tc); 73413902Sbrandon.potter@amd.com } 73513902Sbrandon.potter@amd.com if (sim_fd == -1) { 73614024Sgabeblack@google.com sim_fd = open(path.c_str(), host_flags, mode); 73713902Sbrandon.potter@amd.com } 73813902Sbrandon.potter@amd.com if (sim_fd == -1) { 73913902Sbrandon.potter@amd.com int local = -errno; 74013902Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: failed -> path:%s\n", 74110496Ssteve.reinhardt@amd.com isopenat ? "at" : "", path.c_str()); 74211856Sbrandon.potter@amd.com return local; 74311856Sbrandon.potter@amd.com } 74411856Sbrandon.potter@amd.com 74511856Sbrandon.potter@amd.com /** 74611856Sbrandon.potter@amd.com * The file was opened successfully and needs to be recorded in the 74710930Sbrandon.potter@amd.com * process' file descriptor array so that it can be retrieved later. 7489141Smarc.orr@gmail.com * The target file descriptor that is chosen will be the lowest unused 749360SN/A * file descriptor. 750360SN/A * Return the indirect target file descriptor back to the simulated 751360SN/A * process to act as a handle for the opened file. 75211907SBrandon.Potter@amd.com */ 75313995Sbrandon.potter@amd.com auto ffdp = std::make_shared<FileFDEntry>(sim_fd, host_flags, path, 0); 754360SN/A int tgt_fd = p->fds->allocFD(ffdp); 75511907SBrandon.Potter@amd.com DPRINTF_SYSCALL(Verbose, "open%s: sim_fd[%d], target_fd[%d] -> path:%s\n", 75613995Sbrandon.potter@amd.com isopenat ? "at" : "", sim_fd, tgt_fd, path.c_str()); 75711907SBrandon.Potter@amd.com return tgt_fd; 75811907SBrandon.Potter@amd.com} 75911907SBrandon.Potter@amd.com 76011907SBrandon.Potter@amd.com/// Target open() handler. 76111907SBrandon.Potter@amd.comtemplate <class OS> 76211907SBrandon.Potter@amd.comSyscallReturn 76311907SBrandon.Potter@amd.comopenFunc(SyscallDesc *desc, int callnum, Process *process, 76411907SBrandon.Potter@amd.com ThreadContext *tc) 76511907SBrandon.Potter@amd.com{ 76611907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, false); 76711907SBrandon.Potter@amd.com} 76811907SBrandon.Potter@amd.com 76911907SBrandon.Potter@amd.com/// Target openat() handler. 770360SN/Atemplate <class OS> 77114024Sgabeblack@google.comSyscallReturn 7721458SN/AopenatFunc(SyscallDesc *desc, int callnum, Process *process, 773360SN/A ThreadContext *tc) 77411907SBrandon.Potter@amd.com{ 77511907SBrandon.Potter@amd.com return openImpl<OS>(desc, callnum, process, tc, true); 77611907SBrandon.Potter@amd.com} 77711907SBrandon.Potter@amd.com 77811907SBrandon.Potter@amd.com/// Target unlinkat() handler. 77911907SBrandon.Potter@amd.comtemplate <class OS> 78011907SBrandon.Potter@amd.comSyscallReturn 78111907SBrandon.Potter@amd.comunlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 78211907SBrandon.Potter@amd.com ThreadContext *tc) 78311907SBrandon.Potter@amd.com{ 784360SN/A int index = 0; 78511907SBrandon.Potter@amd.com int dirfd = process->getSyscallArg(tc, index); 78611907SBrandon.Potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 78711907SBrandon.Potter@amd.com warn("unlinkat: first argument not AT_FDCWD; unlikely to work"); 788360SN/A 789360SN/A return unlinkHelper(desc, callnum, process, tc, 1); 79011907SBrandon.Potter@amd.com} 79111907SBrandon.Potter@amd.com 79211907SBrandon.Potter@amd.com/// Target facessat() handler 79311907SBrandon.Potter@amd.comtemplate <class OS> 794360SN/ASyscallReturn 79511907SBrandon.Potter@amd.comfaccessatFunc(SyscallDesc *desc, int callnum, Process *process, 796360SN/A ThreadContext *tc) 797360SN/A{ 79811907SBrandon.Potter@amd.com int index = 0; 7993669Sbinkertn@umich.edu int dirfd = process->getSyscallArg(tc, index); 80011907SBrandon.Potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 80111907SBrandon.Potter@amd.com warn("faccessat: first argument not AT_FDCWD; unlikely to work"); 80211907SBrandon.Potter@amd.com return accessFunc(desc, callnum, process, tc, 1); 80311907SBrandon.Potter@amd.com} 80411907SBrandon.Potter@amd.com 80511907SBrandon.Potter@amd.com/// Target readlinkat() handler 80611907SBrandon.Potter@amd.comtemplate <class OS> 80711907SBrandon.Potter@amd.comSyscallReturn 80811907SBrandon.Potter@amd.comreadlinkatFunc(SyscallDesc *desc, int callnum, Process *process, 80911907SBrandon.Potter@amd.com ThreadContext *tc) 81011907SBrandon.Potter@amd.com{ 81111907SBrandon.Potter@amd.com int index = 0; 81213883Sdavid.hashe@amd.com int dirfd = process->getSyscallArg(tc, index); 81313883Sdavid.hashe@amd.com if (dirfd != OS::TGT_AT_FDCWD) 81413883Sdavid.hashe@amd.com warn("openat: first argument not AT_FDCWD; unlikely to work"); 81513883Sdavid.hashe@amd.com return readlinkFunc(desc, callnum, process, tc, 1); 81613883Sdavid.hashe@amd.com} 81711907SBrandon.Potter@amd.com 81811907SBrandon.Potter@amd.com/// Target renameat() handler. 81911907SBrandon.Potter@amd.comtemplate <class OS> 82011907SBrandon.Potter@amd.comSyscallReturn 82111907SBrandon.Potter@amd.comrenameatFunc(SyscallDesc *desc, int callnum, Process *process, 82213883Sdavid.hashe@amd.com ThreadContext *tc) 82313883Sdavid.hashe@amd.com{ 82411907SBrandon.Potter@amd.com int index = 0; 8251706SN/A 82611907SBrandon.Potter@amd.com int olddirfd = process->getSyscallArg(tc, index); 82711907SBrandon.Potter@amd.com if (olddirfd != OS::TGT_AT_FDCWD) 82811907SBrandon.Potter@amd.com warn("renameat: first argument not AT_FDCWD; unlikely to work"); 82911907SBrandon.Potter@amd.com 83011907SBrandon.Potter@amd.com std::string old_name; 83111907SBrandon.Potter@amd.com 83213883Sdavid.hashe@amd.com if (!tc->getMemProxy().tryReadString(old_name, 83313883Sdavid.hashe@amd.com process->getSyscallArg(tc, index))) 83411907SBrandon.Potter@amd.com return -EFAULT; 83511907SBrandon.Potter@amd.com 83611907SBrandon.Potter@amd.com int newdirfd = process->getSyscallArg(tc, index); 83711907SBrandon.Potter@amd.com if (newdirfd != OS::TGT_AT_FDCWD) 83813883Sdavid.hashe@amd.com warn("renameat: third argument not AT_FDCWD; unlikely to work"); 83913995Sbrandon.potter@amd.com 84010496Ssteve.reinhardt@amd.com std::string new_name; 84111907SBrandon.Potter@amd.com 84211907SBrandon.Potter@amd.com if (!tc->getMemProxy().tryReadString(new_name, 84311907SBrandon.Potter@amd.com process->getSyscallArg(tc, index))) 84411907SBrandon.Potter@amd.com return -EFAULT; 84510496Ssteve.reinhardt@amd.com 84610496Ssteve.reinhardt@amd.com // Adjust path for current working directory 84711907SBrandon.Potter@amd.com old_name = process->fullPath(old_name); 84813883Sdavid.hashe@amd.com new_name = process->fullPath(new_name); 84913883Sdavid.hashe@amd.com 85013883Sdavid.hashe@amd.com int result = rename(old_name.c_str(), new_name.c_str()); 85113883Sdavid.hashe@amd.com return (result == -1) ? -errno : result; 85213883Sdavid.hashe@amd.com} 85313883Sdavid.hashe@amd.com 85413883Sdavid.hashe@amd.com/// Target sysinfo() handler. 85513883Sdavid.hashe@amd.comtemplate <class OS> 85613883Sdavid.hashe@amd.comSyscallReturn 85713883Sdavid.hashe@amd.comsysinfoFunc(SyscallDesc *desc, int callnum, Process *process, 85813883Sdavid.hashe@amd.com ThreadContext *tc) 85913883Sdavid.hashe@amd.com{ 86013883Sdavid.hashe@amd.com 86113883Sdavid.hashe@amd.com int index = 0; 86213883Sdavid.hashe@amd.com TypedBufferArg<typename OS::tgt_sysinfo> 86313883Sdavid.hashe@amd.com sysinfo(process->getSyscallArg(tc, index)); 86413883Sdavid.hashe@amd.com 86513883Sdavid.hashe@amd.com sysinfo->uptime = seconds_since_epoch; 86613883Sdavid.hashe@amd.com sysinfo->totalram = process->system->memSize(); 86713883Sdavid.hashe@amd.com sysinfo->mem_unit = 1; 86813883Sdavid.hashe@amd.com 86913883Sdavid.hashe@amd.com sysinfo.copyOut(tc->getMemProxy()); 87011907SBrandon.Potter@amd.com 87111907SBrandon.Potter@amd.com return 0; 87213883Sdavid.hashe@amd.com} 87311907SBrandon.Potter@amd.com 87413994Santhony.gutierrez@amd.com/// Target chmod() handler. 87511907SBrandon.Potter@amd.comtemplate <class OS> 87613883Sdavid.hashe@amd.comSyscallReturn 87713883Sdavid.hashe@amd.comchmodFunc(SyscallDesc *desc, int callnum, Process *process, 87813883Sdavid.hashe@amd.com ThreadContext *tc) 87913883Sdavid.hashe@amd.com{ 88011907SBrandon.Potter@amd.com std::string path; 88111907SBrandon.Potter@amd.com 88213883Sdavid.hashe@amd.com int index = 0; 88313883Sdavid.hashe@amd.com if (!tc->getMemProxy().tryReadString(path, 88411907SBrandon.Potter@amd.com process->getSyscallArg(tc, index))) { 88511907SBrandon.Potter@amd.com return -EFAULT; 88611907SBrandon.Potter@amd.com } 88713883Sdavid.hashe@amd.com 88813883Sdavid.hashe@amd.com uint32_t mode = process->getSyscallArg(tc, index); 88913883Sdavid.hashe@amd.com mode_t hostMode = 0; 89011907SBrandon.Potter@amd.com 89111907SBrandon.Potter@amd.com // XXX translate mode flags via OS::something??? 892360SN/A hostMode = mode; 89311907SBrandon.Potter@amd.com 89411907SBrandon.Potter@amd.com // Adjust path for current working directory 89511907SBrandon.Potter@amd.com path = process->fullPath(path); 89611907SBrandon.Potter@amd.com 89711907SBrandon.Potter@amd.com // do the chmod 89811907SBrandon.Potter@amd.com int result = chmod(path.c_str(), hostMode); 89911907SBrandon.Potter@amd.com if (result < 0) 90011907SBrandon.Potter@amd.com return -errno; 90111907SBrandon.Potter@amd.com 90211907SBrandon.Potter@amd.com return 0; 90313883Sdavid.hashe@amd.com} 90413883Sdavid.hashe@amd.com 90513883Sdavid.hashe@amd.com 90611907SBrandon.Potter@amd.com/// Target fchmod() handler. 907360SN/Atemplate <class OS> 908360SN/ASyscallReturn 90910027SChris.Adeniyi-Jones@arm.comfchmodFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 91010027SChris.Adeniyi-Jones@arm.com{ 91110027SChris.Adeniyi-Jones@arm.com int index = 0; 91213995Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 91310027SChris.Adeniyi-Jones@arm.com uint32_t mode = p->getSyscallArg(tc, index); 91413995Sbrandon.potter@amd.com 91510027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 91610027SChris.Adeniyi-Jones@arm.com if (!ffdp) 91710027SChris.Adeniyi-Jones@arm.com return -EBADF; 91810027SChris.Adeniyi-Jones@arm.com int sim_fd = ffdp->getSimFD(); 91910027SChris.Adeniyi-Jones@arm.com 92013995Sbrandon.potter@amd.com mode_t hostMode = mode; 92110027SChris.Adeniyi-Jones@arm.com 92213995Sbrandon.potter@amd.com int result = fchmod(sim_fd, hostMode); 92310027SChris.Adeniyi-Jones@arm.com 92410027SChris.Adeniyi-Jones@arm.com return (result < 0) ? -errno : 0; 92510633Smichaelupton@gmail.com} 92610633Smichaelupton@gmail.com 92710633Smichaelupton@gmail.com/// Target mremap() handler. 92813995Sbrandon.potter@amd.comtemplate <class OS> 92910633Smichaelupton@gmail.comSyscallReturn 93010633Smichaelupton@gmail.commremapFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 93113995Sbrandon.potter@amd.com{ 93210633Smichaelupton@gmail.com int index = 0; 93310633Smichaelupton@gmail.com Addr start = process->getSyscallArg(tc, index); 93410633Smichaelupton@gmail.com uint64_t old_length = process->getSyscallArg(tc, index); 93510633Smichaelupton@gmail.com uint64_t new_length = process->getSyscallArg(tc, index); 93613995Sbrandon.potter@amd.com uint64_t flags = process->getSyscallArg(tc, index); 93710633Smichaelupton@gmail.com uint64_t provided_address = 0; 93810633Smichaelupton@gmail.com bool use_provided_address = flags & OS::TGT_MREMAP_FIXED; 93910203SAli.Saidi@ARM.com 94010203SAli.Saidi@ARM.com if (use_provided_address) 94110203SAli.Saidi@ARM.com provided_address = process->getSyscallArg(tc, index); 94213995Sbrandon.potter@amd.com 94310203SAli.Saidi@ARM.com if ((start % TheISA::PageBytes != 0) || 94410203SAli.Saidi@ARM.com (provided_address % TheISA::PageBytes != 0)) { 94513995Sbrandon.potter@amd.com warn("mremap failing: arguments not page aligned"); 94610203SAli.Saidi@ARM.com return -EINVAL; 94710203SAli.Saidi@ARM.com } 94810203SAli.Saidi@ARM.com 94913995Sbrandon.potter@amd.com new_length = roundUp(new_length, TheISA::PageBytes); 95010203SAli.Saidi@ARM.com 95110203SAli.Saidi@ARM.com if (new_length > old_length) { 95210203SAli.Saidi@ARM.com std::shared_ptr<MemState> mem_state = process->memState; 95310203SAli.Saidi@ARM.com Addr mmap_end = mem_state->getMmapEnd(); 95410203SAli.Saidi@ARM.com 95513995Sbrandon.potter@amd.com if ((start + old_length) == mmap_end && 95610203SAli.Saidi@ARM.com (!use_provided_address || provided_address == start)) { 95710203SAli.Saidi@ARM.com uint64_t diff = new_length - old_length; 95813995Sbrandon.potter@amd.com process->allocateMem(mmap_end, diff); 95910203SAli.Saidi@ARM.com mem_state->setMmapEnd(mmap_end + diff); 96010203SAli.Saidi@ARM.com return start; 96110203SAli.Saidi@ARM.com } else { 96213995Sbrandon.potter@amd.com if (!use_provided_address && !(flags & OS::TGT_MREMAP_MAYMOVE)) { 96310203SAli.Saidi@ARM.com warn("can't remap here and MREMAP_MAYMOVE flag not set\n"); 96410203SAli.Saidi@ARM.com return -ENOMEM; 96510850SGiacomo.Gabrielli@arm.com } else { 96610850SGiacomo.Gabrielli@arm.com uint64_t new_start = use_provided_address ? 96710850SGiacomo.Gabrielli@arm.com provided_address : mmap_end; 96813995Sbrandon.potter@amd.com process->pTable->remap(start, old_length, new_start); 96910850SGiacomo.Gabrielli@arm.com warn("mremapping to new vaddr %08p-%08p, adding %d\n", 97010850SGiacomo.Gabrielli@arm.com new_start, new_start + new_length, 97113995Sbrandon.potter@amd.com new_length - old_length); 97210850SGiacomo.Gabrielli@arm.com // add on the remaining unallocated pages 97310850SGiacomo.Gabrielli@arm.com process->allocateMem(new_start + old_length, 97410850SGiacomo.Gabrielli@arm.com new_length - old_length, 97510850SGiacomo.Gabrielli@arm.com use_provided_address /* clobber */); 97610850SGiacomo.Gabrielli@arm.com if (!use_provided_address) 97710850SGiacomo.Gabrielli@arm.com mem_state->setMmapEnd(mmap_end + new_length); 97810850SGiacomo.Gabrielli@arm.com if (use_provided_address && 97914024Sgabeblack@google.com new_start + new_length > mem_state->getMmapEnd()) { 98010850SGiacomo.Gabrielli@arm.com // something fishy going on here, at least notify the user 98110850SGiacomo.Gabrielli@arm.com // @todo: increase mmap_end? 98210850SGiacomo.Gabrielli@arm.com warn("mmap region limit exceeded with MREMAP_FIXED\n"); 98310850SGiacomo.Gabrielli@arm.com } 98410850SGiacomo.Gabrielli@arm.com warn("returning %08p as start\n", new_start); 98510850SGiacomo.Gabrielli@arm.com return new_start; 98610850SGiacomo.Gabrielli@arm.com } 98710850SGiacomo.Gabrielli@arm.com } 98810850SGiacomo.Gabrielli@arm.com } else { 98914024Sgabeblack@google.com if (use_provided_address && provided_address != start) 99010850SGiacomo.Gabrielli@arm.com process->pTable->remap(start, new_length, provided_address); 99110850SGiacomo.Gabrielli@arm.com process->pTable->unmap(start + new_length, old_length - new_length); 99210850SGiacomo.Gabrielli@arm.com return use_provided_address ? provided_address : start; 99313883Sdavid.hashe@amd.com } 99413883Sdavid.hashe@amd.com} 99513883Sdavid.hashe@amd.com 99610850SGiacomo.Gabrielli@arm.com/// Target stat() handler. 99710850SGiacomo.Gabrielli@arm.comtemplate <class OS> 99810850SGiacomo.Gabrielli@arm.comSyscallReturn 99910850SGiacomo.Gabrielli@arm.comstatFunc(SyscallDesc *desc, int callnum, Process *process, 100010850SGiacomo.Gabrielli@arm.com ThreadContext *tc) 10016640Svince@csl.cornell.edu{ 10026640Svince@csl.cornell.edu std::string path; 10036640Svince@csl.cornell.edu 100413995Sbrandon.potter@amd.com int index = 0; 10056640Svince@csl.cornell.edu if (!tc->getMemProxy().tryReadString(path, 10066701Sgblack@eecs.umich.edu process->getSyscallArg(tc, index))) { 100713995Sbrandon.potter@amd.com return -EFAULT; 100813995Sbrandon.potter@amd.com } 10096701Sgblack@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 101010793Sbrandon.potter@amd.com 10116640Svince@csl.cornell.edu // Adjust path for current working directory 101211758Sbrandon.potter@amd.com path = process->fullPath(path); 101311758Sbrandon.potter@amd.com 101411758Sbrandon.potter@amd.com struct stat hostBuf; 10156640Svince@csl.cornell.edu int result = stat(path.c_str(), &hostBuf); 101614024Sgabeblack@google.com 10176640Svince@csl.cornell.edu if (result < 0) 10186701Sgblack@eecs.umich.edu return -errno; 10196640Svince@csl.cornell.edu 1020360SN/A copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 10211999SN/A 10221999SN/A return 0; 10231999SN/A} 102413995Sbrandon.potter@amd.com 10251999SN/A 10261999SN/A/// Target stat64() handler. 102713995Sbrandon.potter@amd.comtemplate <class OS> 10281999SN/ASyscallReturn 10296701Sgblack@eecs.umich.edustat64Func(SyscallDesc *desc, int callnum, Process *process, 103014024Sgabeblack@google.com ThreadContext *tc) 10316701Sgblack@eecs.umich.edu{ 10321999SN/A std::string path; 10336701Sgblack@eecs.umich.edu 10341999SN/A int index = 0; 10356701Sgblack@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 10361999SN/A process->getSyscallArg(tc, index))) 10371999SN/A return -EFAULT; 10381999SN/A Addr bufPtr = process->getSyscallArg(tc, index); 10391999SN/A 10401999SN/A // Adjust path for current working directory 104113883Sdavid.hashe@amd.com path = process->fullPath(path); 104213883Sdavid.hashe@amd.com 10433669Sbinkertn@umich.edu#if NO_STAT64 10441999SN/A struct stat hostBuf; 10451999SN/A int result = stat(path.c_str(), &hostBuf); 10461999SN/A#else 10472218SN/A struct stat64 hostBuf; 10481999SN/A int result = stat64(path.c_str(), &hostBuf); 10491999SN/A#endif 10501999SN/A 10511999SN/A if (result < 0) 105213570Sbrandon.potter@amd.com return -errno; 105313570Sbrandon.potter@amd.com 105413995Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 105513570Sbrandon.potter@amd.com 105613570Sbrandon.potter@amd.com return 0; 105713995Sbrandon.potter@amd.com} 105813570Sbrandon.potter@amd.com 105913570Sbrandon.potter@amd.com 106013570Sbrandon.potter@amd.com/// Target fstatat64() handler. 106113570Sbrandon.potter@amd.comtemplate <class OS> 106213570Sbrandon.potter@amd.comSyscallReturn 106314024Sgabeblack@google.comfstatat64Func(SyscallDesc *desc, int callnum, Process *process, 106413570Sbrandon.potter@amd.com ThreadContext *tc) 106513570Sbrandon.potter@amd.com{ 106613570Sbrandon.potter@amd.com int index = 0; 106713570Sbrandon.potter@amd.com int dirfd = process->getSyscallArg(tc, index); 106813570Sbrandon.potter@amd.com if (dirfd != OS::TGT_AT_FDCWD) 106913570Sbrandon.potter@amd.com warn("fstatat64: first argument not AT_FDCWD; unlikely to work"); 107013570Sbrandon.potter@amd.com 107113570Sbrandon.potter@amd.com std::string path; 107213570Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 107313570Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) 107413570Sbrandon.potter@amd.com return -EFAULT; 107513570Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 107613570Sbrandon.potter@amd.com 107713570Sbrandon.potter@amd.com // Adjust path for current working directory 107813570Sbrandon.potter@amd.com path = process->fullPath(path); 107913570Sbrandon.potter@amd.com 108013570Sbrandon.potter@amd.com#if NO_STAT64 108113570Sbrandon.potter@amd.com struct stat hostBuf; 108213570Sbrandon.potter@amd.com int result = stat(path.c_str(), &hostBuf); 108313570Sbrandon.potter@amd.com#else 108413570Sbrandon.potter@amd.com struct stat64 hostBuf; 108513570Sbrandon.potter@amd.com int result = stat64(path.c_str(), &hostBuf); 108613570Sbrandon.potter@amd.com#endif 108713570Sbrandon.potter@amd.com 108813570Sbrandon.potter@amd.com if (result < 0) 108913570Sbrandon.potter@amd.com return -errno; 109013570Sbrandon.potter@amd.com 109113570Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 109213570Sbrandon.potter@amd.com 109313570Sbrandon.potter@amd.com return 0; 109413570Sbrandon.potter@amd.com} 109513570Sbrandon.potter@amd.com 109613570Sbrandon.potter@amd.com 109713570Sbrandon.potter@amd.com/// Target fstat64() handler. 109813570Sbrandon.potter@amd.comtemplate <class OS> 109913570Sbrandon.potter@amd.comSyscallReturn 110013570Sbrandon.potter@amd.comfstat64Func(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 110113570Sbrandon.potter@amd.com{ 110213570Sbrandon.potter@amd.com int index = 0; 110313570Sbrandon.potter@amd.com int tgt_fd = p->getSyscallArg(tc, index); 110413570Sbrandon.potter@amd.com Addr bufPtr = p->getSyscallArg(tc, index); 110513570Sbrandon.potter@amd.com 110613570Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 110713570Sbrandon.potter@amd.com if (!ffdp) 110813570Sbrandon.potter@amd.com return -EBADF; 110913570Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 111013570Sbrandon.potter@amd.com 111113570Sbrandon.potter@amd.com#if NO_STAT64 111213570Sbrandon.potter@amd.com struct stat hostBuf; 111313570Sbrandon.potter@amd.com int result = fstat(sim_fd, &hostBuf); 111413570Sbrandon.potter@amd.com#else 111513570Sbrandon.potter@amd.com struct stat64 hostBuf; 111613570Sbrandon.potter@amd.com int result = fstat64(sim_fd, &hostBuf); 111713570Sbrandon.potter@amd.com#endif 111813570Sbrandon.potter@amd.com 111913570Sbrandon.potter@amd.com if (result < 0) 112013570Sbrandon.potter@amd.com return -errno; 112113570Sbrandon.potter@amd.com 112213570Sbrandon.potter@amd.com copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 112314024Sgabeblack@google.com 112413570Sbrandon.potter@amd.com return 0; 112513570Sbrandon.potter@amd.com} 112613570Sbrandon.potter@amd.com 11271999SN/A 11281999SN/A/// Target lstat() handler. 11291999SN/Atemplate <class OS> 11301999SN/ASyscallReturn 113113995Sbrandon.potter@amd.comlstatFunc(SyscallDesc *desc, int callnum, Process *process, 11321999SN/A ThreadContext *tc) 11336701Sgblack@eecs.umich.edu{ 113413995Sbrandon.potter@amd.com std::string path; 113511856Sbrandon.potter@amd.com 113611856Sbrandon.potter@amd.com int index = 0; 113710931Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 113811856Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 113911856Sbrandon.potter@amd.com return -EFAULT; 11401999SN/A } 114111856Sbrandon.potter@amd.com Addr bufPtr = process->getSyscallArg(tc, index); 11421999SN/A 114311856Sbrandon.potter@amd.com // Adjust path for current working directory 11441999SN/A path = process->fullPath(path); 114511856Sbrandon.potter@amd.com 11461999SN/A struct stat hostBuf; 114711856Sbrandon.potter@amd.com int result = lstat(path.c_str(), &hostBuf); 11481999SN/A 11491999SN/A if (result < 0) 11505877Shsul@eecs.umich.edu return -errno; 11515877Shsul@eecs.umich.edu 11525877Shsul@eecs.umich.edu copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 115313995Sbrandon.potter@amd.com 11545877Shsul@eecs.umich.edu return 0; 11556701Sgblack@eecs.umich.edu} 115613995Sbrandon.potter@amd.com 11576701Sgblack@eecs.umich.edu/// Target lstat64() handler. 11586701Sgblack@eecs.umich.edutemplate <class OS> 11596701Sgblack@eecs.umich.eduSyscallReturn 11606701Sgblack@eecs.umich.edulstat64Func(SyscallDesc *desc, int callnum, Process *process, 116110027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 116210027SChris.Adeniyi-Jones@arm.com{ 116310027SChris.Adeniyi-Jones@arm.com std::string path; 116410027SChris.Adeniyi-Jones@arm.com 116510027SChris.Adeniyi-Jones@arm.com int index = 0; 11665877Shsul@eecs.umich.edu if (!tc->getMemProxy().tryReadString(path, 116710318Sandreas.hansson@arm.com process->getSyscallArg(tc, index))) { 116810318Sandreas.hansson@arm.com return -EFAULT; 11695877Shsul@eecs.umich.edu } 11705877Shsul@eecs.umich.edu Addr bufPtr = process->getSyscallArg(tc, index); 11715877Shsul@eecs.umich.edu 11725877Shsul@eecs.umich.edu // Adjust path for current working directory 117310486Stjablin@gmail.com path = process->fullPath(path); 117410486Stjablin@gmail.com 11755877Shsul@eecs.umich.edu#if NO_STAT64 117611905SBrandon.Potter@amd.com struct stat hostBuf; 117711905SBrandon.Potter@amd.com int result = lstat(path.c_str(), &hostBuf); 117811905SBrandon.Potter@amd.com#else 117911905SBrandon.Potter@amd.com struct stat64 hostBuf; 118010027SChris.Adeniyi-Jones@arm.com int result = lstat64(path.c_str(), &hostBuf); 118112206Srico.amslinger@informatik.uni-augsburg.de#endif 118212206Srico.amslinger@informatik.uni-augsburg.de 11835877Shsul@eecs.umich.edu if (result < 0) 118411905SBrandon.Potter@amd.com return -errno; 118511905SBrandon.Potter@amd.com 11865877Shsul@eecs.umich.edu copyOutStat64Buf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 11875877Shsul@eecs.umich.edu 118810027SChris.Adeniyi-Jones@arm.com return 0; 11895877Shsul@eecs.umich.edu} 11905877Shsul@eecs.umich.edu 11915877Shsul@eecs.umich.edu/// Target fstat() handler. 119212206Srico.amslinger@informatik.uni-augsburg.detemplate <class OS> 119312206Srico.amslinger@informatik.uni-augsburg.deSyscallReturn 119412206Srico.amslinger@informatik.uni-augsburg.defstatFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 119512206Srico.amslinger@informatik.uni-augsburg.de{ 119612206Srico.amslinger@informatik.uni-augsburg.de int index = 0; 119712206Srico.amslinger@informatik.uni-augsburg.de int tgt_fd = p->getSyscallArg(tc, index); 119812206Srico.amslinger@informatik.uni-augsburg.de Addr bufPtr = p->getSyscallArg(tc, index); 119912206Srico.amslinger@informatik.uni-augsburg.de 120012206Srico.amslinger@informatik.uni-augsburg.de DPRINTF_SYSCALL(Verbose, "fstat(%d, ...)\n", tgt_fd); 120110027SChris.Adeniyi-Jones@arm.com 120210027SChris.Adeniyi-Jones@arm.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 120310027SChris.Adeniyi-Jones@arm.com if (!ffdp) 120410027SChris.Adeniyi-Jones@arm.com return -EBADF; 12055877Shsul@eecs.umich.edu int sim_fd = ffdp->getSimFD(); 120610027SChris.Adeniyi-Jones@arm.com 120710027SChris.Adeniyi-Jones@arm.com struct stat hostBuf; 120810027SChris.Adeniyi-Jones@arm.com int result = fstat(sim_fd, &hostBuf); 120910027SChris.Adeniyi-Jones@arm.com 121012206Srico.amslinger@informatik.uni-augsburg.de if (result < 0) 121112206Srico.amslinger@informatik.uni-augsburg.de return -errno; 121212206Srico.amslinger@informatik.uni-augsburg.de 121312206Srico.amslinger@informatik.uni-augsburg.de copyOutStatBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf, (sim_fd == 1)); 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 121910027SChris.Adeniyi-Jones@arm.com/// Target statfs() handler. 12205877Shsul@eecs.umich.edutemplate <class OS> 12215877Shsul@eecs.umich.eduSyscallReturn 12225877Shsul@eecs.umich.edustatfsFunc(SyscallDesc *desc, int callnum, Process *process, 122310027SChris.Adeniyi-Jones@arm.com ThreadContext *tc) 122410027SChris.Adeniyi-Jones@arm.com{ 12258601Ssteve.reinhardt@amd.com#if NO_STATFS 122610027SChris.Adeniyi-Jones@arm.com warn("Host OS cannot support calls to statfs. Ignoring syscall"); 12275877Shsul@eecs.umich.edu#else 12285877Shsul@eecs.umich.edu std::string path; 12291999SN/A 1230378SN/A int index = 0; 1231360SN/A if (!tc->getMemProxy().tryReadString(path, 12321450SN/A process->getSyscallArg(tc, index))) { 123313995Sbrandon.potter@amd.com return -EFAULT; 1234360SN/A } 1235360SN/A Addr bufPtr = process->getSyscallArg(tc, index); 123613995Sbrandon.potter@amd.com 1237360SN/A // Adjust path for current working directory 12386701Sgblack@eecs.umich.edu path = process->fullPath(path); 123914024Sgabeblack@google.com 12406701Sgblack@eecs.umich.edu struct statfs hostBuf; 12416701Sgblack@eecs.umich.edu int result = statfs(path.c_str(), &hostBuf); 12426701Sgblack@eecs.umich.edu 12436701Sgblack@eecs.umich.edu if (result < 0) 1244360SN/A return -errno; 124513883Sdavid.hashe@amd.com 124613883Sdavid.hashe@amd.com copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 12473669Sbinkertn@umich.edu#endif 1248360SN/A return 0; 1249360SN/A} 1250360SN/A 1251360SN/Atemplate <class OS> 12522218SN/ASyscallReturn 1253360SN/AcloneFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 125414024Sgabeblack@google.com{ 1255360SN/A int index = 0; 12561458SN/A TheISA::IntReg flags = p->getSyscallArg(tc, index); 1257360SN/A TheISA::IntReg newStack = p->getSyscallArg(tc, index); 1258360SN/A Addr ptidPtr = p->getSyscallArg(tc, index); 1259360SN/A Addr ctidPtr = p->getSyscallArg(tc, index); 12605074Ssaidi@eecs.umich.edu Addr tlsPtr M5_VAR_USED = p->getSyscallArg(tc, index); 12615074Ssaidi@eecs.umich.edu 12625074Ssaidi@eecs.umich.edu if (((flags & OS::TGT_CLONE_SIGHAND)&& !(flags & OS::TGT_CLONE_VM)) || 126313995Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_THREAD) && !(flags & OS::TGT_CLONE_SIGHAND)) || 12645074Ssaidi@eecs.umich.edu ((flags & OS::TGT_CLONE_FS) && (flags & OS::TGT_CLONE_NEWNS)) || 12655074Ssaidi@eecs.umich.edu ((flags & OS::TGT_CLONE_NEWIPC) && (flags & OS::TGT_CLONE_SYSVSEM)) || 126613995Sbrandon.potter@amd.com ((flags & OS::TGT_CLONE_NEWPID) && (flags & OS::TGT_CLONE_THREAD)) || 12675074Ssaidi@eecs.umich.edu ((flags & OS::TGT_CLONE_VM) && !(newStack))) 12686701Sgblack@eecs.umich.edu return -EINVAL; 126914024Sgabeblack@google.com 12706701Sgblack@eecs.umich.edu ThreadContext *ctc; 12715074Ssaidi@eecs.umich.edu if (!(ctc = p->findFreeContext())) 12726701Sgblack@eecs.umich.edu fatal("clone: no spare thread context in system"); 12735074Ssaidi@eecs.umich.edu 127413883Sdavid.hashe@amd.com /** 127513883Sdavid.hashe@amd.com * Note that ProcessParams is generated by swig and there are no other 12765074Ssaidi@eecs.umich.edu * examples of how to create anything but this default constructor. The 12775208Ssaidi@eecs.umich.edu * fields are manually initialized instead of passing parameters to the 12785208Ssaidi@eecs.umich.edu * constructor. 12795208Ssaidi@eecs.umich.edu */ 12805208Ssaidi@eecs.umich.edu ProcessParams *pp = new ProcessParams(); 12815074Ssaidi@eecs.umich.edu pp->executable.assign(*(new std::string(p->progName()))); 12825074Ssaidi@eecs.umich.edu pp->cmd.push_back(*(new std::string(p->progName()))); 12835208Ssaidi@eecs.umich.edu pp->system = p->system; 12845074Ssaidi@eecs.umich.edu pp->cwd.assign(p->getcwd()); 12855074Ssaidi@eecs.umich.edu pp->input.assign("stdin"); 12865074Ssaidi@eecs.umich.edu pp->output.assign("stdout"); 12875074Ssaidi@eecs.umich.edu pp->errout.assign("stderr"); 128814024Sgabeblack@google.com pp->uid = p->uid(); 12895074Ssaidi@eecs.umich.edu pp->euid = p->euid(); 12905074Ssaidi@eecs.umich.edu pp->gid = p->gid(); 12915074Ssaidi@eecs.umich.edu pp->egid = p->egid(); 12925074Ssaidi@eecs.umich.edu 12935074Ssaidi@eecs.umich.edu /* Find the first free PID that's less than the maximum */ 129410027SChris.Adeniyi-Jones@arm.com std::set<int> const& pids = p->system->PIDs; 129510027SChris.Adeniyi-Jones@arm.com int temp_pid = *pids.begin(); 129610027SChris.Adeniyi-Jones@arm.com do { 129713995Sbrandon.potter@amd.com temp_pid++; 129810027SChris.Adeniyi-Jones@arm.com } while (pids.find(temp_pid) != pids.end()); 129910027SChris.Adeniyi-Jones@arm.com if (temp_pid >= System::maxPID) 130013995Sbrandon.potter@amd.com fatal("temp_pid is too large: %d", temp_pid); 130110027SChris.Adeniyi-Jones@arm.com 130210027SChris.Adeniyi-Jones@arm.com pp->pid = temp_pid; 130310793Sbrandon.potter@amd.com pp->ppid = (flags & OS::TGT_CLONE_THREAD) ? p->ppid() : p->pid(); 130410027SChris.Adeniyi-Jones@arm.com Process *cp = pp->create(); 130510027SChris.Adeniyi-Jones@arm.com delete pp; 130614024Sgabeblack@google.com 130710027SChris.Adeniyi-Jones@arm.com Process *owner = ctc->getProcessPtr(); 130810027SChris.Adeniyi-Jones@arm.com ctc->setProcessPtr(cp); 130910027SChris.Adeniyi-Jones@arm.com cp->assignThreadContext(ctc->contextId()); 131010027SChris.Adeniyi-Jones@arm.com owner->revokeThreadContext(ctc->contextId()); 131113883Sdavid.hashe@amd.com 131213883Sdavid.hashe@amd.com if (flags & OS::TGT_CLONE_PARENT_SETTID) { 131310027SChris.Adeniyi-Jones@arm.com BufferArg ptidBuf(ptidPtr, sizeof(long)); 131410027SChris.Adeniyi-Jones@arm.com long *ptid = (long *)ptidBuf.bufferPtr(); 131510027SChris.Adeniyi-Jones@arm.com *ptid = cp->pid(); 131610027SChris.Adeniyi-Jones@arm.com ptidBuf.copyOut(tc->getMemProxy()); 131710027SChris.Adeniyi-Jones@arm.com } 131810027SChris.Adeniyi-Jones@arm.com 131910027SChris.Adeniyi-Jones@arm.com cp->initState(); 132010027SChris.Adeniyi-Jones@arm.com p->clone(tc, ctc, cp, flags); 132110027SChris.Adeniyi-Jones@arm.com 132210027SChris.Adeniyi-Jones@arm.com if (flags & OS::TGT_CLONE_CHILD_SETTID) { 132310027SChris.Adeniyi-Jones@arm.com BufferArg ctidBuf(ctidPtr, sizeof(long)); 132410027SChris.Adeniyi-Jones@arm.com long *ctid = (long *)ctidBuf.bufferPtr(); 132514024Sgabeblack@google.com *ctid = cp->pid(); 132610027SChris.Adeniyi-Jones@arm.com ctidBuf.copyOut(ctc->getMemProxy()); 132710027SChris.Adeniyi-Jones@arm.com } 132810027SChris.Adeniyi-Jones@arm.com 132910027SChris.Adeniyi-Jones@arm.com if (flags & OS::TGT_CLONE_CHILD_CLEARTID) 133010027SChris.Adeniyi-Jones@arm.com cp->childClearTID = (uint64_t)ctidPtr; 13311999SN/A 13321999SN/A ctc->clearArchRegs(); 13331999SN/A 133413995Sbrandon.potter@amd.com#if THE_ISA == ALPHA_ISA 13351999SN/A TheISA::copyMiscRegs(tc, ctc); 13366701Sgblack@eecs.umich.edu#elif THE_ISA == SPARC_ISA 133713995Sbrandon.potter@amd.com TheISA::copyRegs(tc, ctc); 133811856Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 6, 0); 133911856Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 4, 0); 134010931Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 3, TheISA::NWindows - 2); 134111856Sbrandon.potter@amd.com ctc->setIntReg(TheISA::NumIntArchRegs + 5, TheISA::NWindows); 134211856Sbrandon.potter@amd.com ctc->setMiscReg(TheISA::MISCREG_CWP, 0); 13431999SN/A ctc->setIntReg(TheISA::NumIntArchRegs + 7, 0); 134411856Sbrandon.potter@amd.com ctc->setMiscRegNoEffect(TheISA::MISCREG_TL, 0); 13451999SN/A ctc->setMiscReg(TheISA::MISCREG_ASI, TheISA::ASI_PRIMARY); 13462764Sstever@eecs.umich.edu for (int y = 8; y < 32; y++) 13472064SN/A ctc->setIntReg(y, tc->readIntReg(y)); 134810931Sbrandon.potter@amd.com#elif THE_ISA == ARM_ISA or THE_ISA == X86_ISA 13492064SN/A TheISA::copyRegs(tc, ctc); 13502064SN/A#endif 135110931Sbrandon.potter@amd.com 13522064SN/A#if THE_ISA == X86_ISA 13531999SN/A if (flags & OS::TGT_CLONE_SETTLS) { 13541999SN/A ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_BASE, tlsPtr); 13552218SN/A ctc->setMiscRegNoEffect(TheISA::MISCREG_FS_EFF_BASE, tlsPtr); 13561999SN/A } 135714024Sgabeblack@google.com#endif 13581999SN/A 13591999SN/A if (newStack) 13601999SN/A ctc->setIntReg(TheISA::StackPointerReg, newStack); 13611999SN/A 13621999SN/A cp->setSyscallReturn(ctc, 0); 1363378SN/A 1364360SN/A#if THE_ISA == ALPHA_ISA 13651450SN/A ctc->setIntReg(TheISA::SyscallSuccessReg, 0); 136613995Sbrandon.potter@amd.com#elif THE_ISA == SPARC_ISA 1367360SN/A tc->setIntReg(TheISA::SyscallPseudoReturnReg, 0); 1368360SN/A ctc->setIntReg(TheISA::SyscallPseudoReturnReg, 1); 136913995Sbrandon.potter@amd.com#endif 1370360SN/A 13716701Sgblack@eecs.umich.edu ctc->pcState(tc->nextInstAddr()); 137214024Sgabeblack@google.com ctc->activate(); 13736701Sgblack@eecs.umich.edu 13746701Sgblack@eecs.umich.edu return cp->pid(); 13756701Sgblack@eecs.umich.edu} 13766701Sgblack@eecs.umich.edu 1377360SN/A/// Target fstatfs() handler. 137813883Sdavid.hashe@amd.comtemplate <class OS> 137913883Sdavid.hashe@amd.comSyscallReturn 13803669Sbinkertn@umich.edufstatfsFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 1381360SN/A{ 1382360SN/A int index = 0; 1383360SN/A int tgt_fd = p->getSyscallArg(tc, index); 1384360SN/A Addr bufPtr = p->getSyscallArg(tc, index); 13851458SN/A 1386360SN/A auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 138714024Sgabeblack@google.com if (!ffdp) 1388360SN/A return -EBADF; 13891458SN/A int sim_fd = ffdp->getSimFD(); 1390360SN/A 1391360SN/A struct statfs hostBuf; 13921999SN/A int result = fstatfs(sim_fd, &hostBuf); 13931999SN/A 13941999SN/A if (result < 0) 139513995Sbrandon.potter@amd.com return -errno; 13961999SN/A 13971999SN/A copyOutStatfsBuf<OS>(tc->getMemProxy(), bufPtr, &hostBuf); 139813995Sbrandon.potter@amd.com 13991999SN/A return 0; 14006701Sgblack@eecs.umich.edu} 140114024Sgabeblack@google.com 14026701Sgblack@eecs.umich.edu 14036701Sgblack@eecs.umich.edu/// Target writev() handler. 14046701Sgblack@eecs.umich.edutemplate <class OS> 14056701Sgblack@eecs.umich.eduSyscallReturn 14061999SN/AwritevFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 140713883Sdavid.hashe@amd.com{ 140813883Sdavid.hashe@amd.com int index = 0; 14093669Sbinkertn@umich.edu int tgt_fd = p->getSyscallArg(tc, index); 14102764Sstever@eecs.umich.edu 14112064SN/A auto hbfdp = std::dynamic_pointer_cast<HBFDEntry>((*p->fds)[tgt_fd]); 14122064SN/A if (!hbfdp) 14132064SN/A return -EBADF; 14141999SN/A int sim_fd = hbfdp->getSimFD(); 14151999SN/A 14162064SN/A SETranslatingPortProxy &prox = tc->getMemProxy(); 14171999SN/A uint64_t tiov_base = p->getSyscallArg(tc, index); 14181999SN/A size_t count = p->getSyscallArg(tc, index); 14191999SN/A struct iovec hiov[count]; 14201999SN/A for (size_t i = 0; i < count; ++i) { 142114024Sgabeblack@google.com typename OS::tgt_iovec tiov; 14221999SN/A 14231999SN/A prox.readBlob(tiov_base + i*sizeof(typename OS::tgt_iovec), 14241999SN/A (uint8_t*)&tiov, sizeof(typename OS::tgt_iovec)); 14251999SN/A hiov[i].iov_len = TheISA::gtoh(tiov.iov_len); 1426378SN/A hiov[i].iov_base = new char [hiov[i].iov_len]; 1427360SN/A prox.readBlob(TheISA::gtoh(tiov.iov_base), (uint8_t *)hiov[i].iov_base, 14281450SN/A hiov[i].iov_len); 142913995Sbrandon.potter@amd.com } 1430360SN/A 14316701Sgblack@eecs.umich.edu int result = writev(sim_fd, hiov, count); 143213995Sbrandon.potter@amd.com 143311856Sbrandon.potter@amd.com for (size_t i = 0; i < count; ++i) 143411856Sbrandon.potter@amd.com delete [] (char *)hiov[i].iov_base; 1435360SN/A 143611380Salexandru.dutu@amd.com if (result < 0) 1437360SN/A return -errno; 143811856Sbrandon.potter@amd.com 143911856Sbrandon.potter@amd.com return result; 14401458SN/A} 144111856Sbrandon.potter@amd.com 1442360SN/A/// Real mmap handler. 1443360SN/Atemplate <class OS> 144410931Sbrandon.potter@amd.comSyscallReturn 1445360SN/AmmapImpl(SyscallDesc *desc, int num, Process *p, ThreadContext *tc, 1446360SN/A bool is_mmap2) 14471458SN/A{ 1448360SN/A int index = 0; 144914024Sgabeblack@google.com Addr start = p->getSyscallArg(tc, index); 14502021SN/A uint64_t length = p->getSyscallArg(tc, index); 14511458SN/A int prot = p->getSyscallArg(tc, index); 1452360SN/A int tgt_flags = p->getSyscallArg(tc, index); 1453360SN/A int tgt_fd = p->getSyscallArg(tc, index); 14541706SN/A int offset = p->getSyscallArg(tc, index); 14551706SN/A 14561706SN/A if (is_mmap2) 145713995Sbrandon.potter@amd.com offset *= TheISA::PageBytes; 14581706SN/A 145913936SAndrea.Mondelli@ucf.edu if (start & (TheISA::PageBytes - 1) || 14601706SN/A offset & (TheISA::PageBytes - 1) || 146113995Sbrandon.potter@amd.com (tgt_flags & OS::TGT_MAP_PRIVATE && 14621706SN/A tgt_flags & OS::TGT_MAP_SHARED) || 14636701Sgblack@eecs.umich.edu (!(tgt_flags & OS::TGT_MAP_PRIVATE) && 146414024Sgabeblack@google.com !(tgt_flags & OS::TGT_MAP_SHARED)) || 14656701Sgblack@eecs.umich.edu !length) { 14666701Sgblack@eecs.umich.edu return -EINVAL; 14676701Sgblack@eecs.umich.edu } 14686701Sgblack@eecs.umich.edu 14691706SN/A if ((prot & PROT_WRITE) && (tgt_flags & OS::TGT_MAP_SHARED)) { 147013883Sdavid.hashe@amd.com // With shared mmaps, there are two cases to consider: 147113883Sdavid.hashe@amd.com // 1) anonymous: writes should modify the mapping and this should be 14723669Sbinkertn@umich.edu // visible to observers who share the mapping. Currently, it's 14731706SN/A // difficult to update the shared mapping because there's no 14741706SN/A // structure which maintains information about the which virtual 14751706SN/A // memory areas are shared. If that structure existed, it would be 14761706SN/A // possible to make the translations point to the same frames. 14772218SN/A // 2) file-backed: writes should modify the mapping and the file 14781706SN/A // which is backed by the mapping. The shared mapping problem is the 147914024Sgabeblack@google.com // same as what was mentioned about the anonymous mappings. For 148013933Sbrandon.potter@amd.com // file-backed mappings, the writes to the file are difficult 148113933Sbrandon.potter@amd.com // because it requires syncing what the mapping holds with the file 148213933Sbrandon.potter@amd.com // that resides on the host system. So, any write on a real system 148313933Sbrandon.potter@amd.com // would cause the change to be propagated to the file mapping at 148411799Sbrandon.potter@amd.com // some point in the future (the inode is tracked along with the 14851706SN/A // mapping). This isn't guaranteed to always happen, but it usually 14861706SN/A // works well enough. The guarantee is provided by the msync system 148711886Sbrandon.potter@amd.com // call. We could force the change through with shared mappings with 148811886Sbrandon.potter@amd.com // a call to msync, but that again would require more information 148913995Sbrandon.potter@amd.com // than we currently maintain. 149011886Sbrandon.potter@amd.com warn("mmap: writing to shared mmap region is currently " 149111886Sbrandon.potter@amd.com "unsupported. The write succeeds on the target, but it " 149212426Sqtt2@cornell.edu "will not be propagated to the host or shared mappings"); 149313995Sbrandon.potter@amd.com } 149413557Sgabeblack@google.com 149513557Sgabeblack@google.com length = roundUp(length, TheISA::PageBytes); 149611886Sbrandon.potter@amd.com 149712426Sqtt2@cornell.edu int sim_fd = -1; 149813534Sandreas.sandberg@arm.com uint8_t *pmap = nullptr; 149912426Sqtt2@cornell.edu if (!(tgt_flags & OS::TGT_MAP_ANONYMOUS)) { 150013534Sandreas.sandberg@arm.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 150112426Sqtt2@cornell.edu 150212426Sqtt2@cornell.edu auto dfdp = std::dynamic_pointer_cast<DeviceFDEntry>(fdep); 150312426Sqtt2@cornell.edu if (dfdp) { 150413536Sandreas.sandberg@arm.com EmulatedDriver *emul_driver = dfdp->getDriver(); 150512426Sqtt2@cornell.edu return emul_driver->mmap(p, tc, start, length, prot, 150612426Sqtt2@cornell.edu tgt_flags, tgt_fd, offset); 150711886Sbrandon.potter@amd.com } 150813536Sandreas.sandberg@arm.com 150912426Sqtt2@cornell.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 151011886Sbrandon.potter@amd.com if (!ffdp) 151111886Sbrandon.potter@amd.com return -EBADF; 151211886Sbrandon.potter@amd.com sim_fd = ffdp->getSimFD(); 151311886Sbrandon.potter@amd.com 151411886Sbrandon.potter@amd.com pmap = (decltype(pmap))mmap(NULL, length, PROT_READ, MAP_PRIVATE, 151511886Sbrandon.potter@amd.com sim_fd, offset); 151611886Sbrandon.potter@amd.com 151711886Sbrandon.potter@amd.com if (pmap == (decltype(pmap))-1) { 151811886Sbrandon.potter@amd.com warn("mmap: failed to map file into host address space"); 151911886Sbrandon.potter@amd.com return -errno; 152013649Sqtt2@cornell.edu } 152113649Sqtt2@cornell.edu } 152213649Sqtt2@cornell.edu 152313649Sqtt2@cornell.edu // Extend global mmap region if necessary. Note that we ignore the 152413649Sqtt2@cornell.edu // start address unless MAP_FIXED is specified. 152511886Sbrandon.potter@amd.com if (!(tgt_flags & OS::TGT_MAP_FIXED)) { 152611886Sbrandon.potter@amd.com std::shared_ptr<MemState> mem_state = p->memState; 152711886Sbrandon.potter@amd.com Addr mmap_end = mem_state->getMmapEnd(); 152811886Sbrandon.potter@amd.com 152911886Sbrandon.potter@amd.com start = p->mmapGrowsDown() ? mmap_end - length : mmap_end; 153011886Sbrandon.potter@amd.com mmap_end = p->mmapGrowsDown() ? start : mmap_end + length; 153111886Sbrandon.potter@amd.com 153211886Sbrandon.potter@amd.com mem_state->setMmapEnd(mmap_end); 153311886Sbrandon.potter@amd.com } 153411886Sbrandon.potter@amd.com 153511886Sbrandon.potter@amd.com DPRINTF_SYSCALL(Verbose, " mmap range is 0x%x - 0x%x\n", 153613883Sdavid.hashe@amd.com start, start + length - 1); 153711886Sbrandon.potter@amd.com 153811886Sbrandon.potter@amd.com // We only allow mappings to overwrite existing mappings if 153911886Sbrandon.potter@amd.com // TGT_MAP_FIXED is set. Otherwise it shouldn't be a problem 154011886Sbrandon.potter@amd.com // because we ignore the start hint if TGT_MAP_FIXED is not set. 154111886Sbrandon.potter@amd.com int clobber = tgt_flags & OS::TGT_MAP_FIXED; 154211886Sbrandon.potter@amd.com if (clobber) { 154311886Sbrandon.potter@amd.com for (auto tc : p->system->threadContexts) { 154411886Sbrandon.potter@amd.com // If we might be overwriting old mappings, we need to 154511886Sbrandon.potter@amd.com // invalidate potentially stale mappings out of the TLBs. 154611886Sbrandon.potter@amd.com tc->getDTBPtr()->flushAll(); 154711886Sbrandon.potter@amd.com tc->getITBPtr()->flushAll(); 154811886Sbrandon.potter@amd.com } 154911886Sbrandon.potter@amd.com } 155011886Sbrandon.potter@amd.com 155111886Sbrandon.potter@amd.com // Allocate physical memory and map it in. If the page table is already 155211886Sbrandon.potter@amd.com // mapped and clobber is not set, the simulator will issue throw a 155311886Sbrandon.potter@amd.com // fatal and bail out of the simulation. 155411886Sbrandon.potter@amd.com p->allocateMem(start, length, clobber); 155511886Sbrandon.potter@amd.com 155613867Salexandru.dutu@amd.com // Transfer content into target address space. 155713867Salexandru.dutu@amd.com SETranslatingPortProxy &tp = tc->getMemProxy(); 155811886Sbrandon.potter@amd.com if (tgt_flags & OS::TGT_MAP_ANONYMOUS) { 155911886Sbrandon.potter@amd.com // In general, we should zero the mapped area for anonymous mappings, 156011886Sbrandon.potter@amd.com // with something like: 156111886Sbrandon.potter@amd.com // tp.memsetBlob(start, 0, length); 156211886Sbrandon.potter@amd.com // However, given that we don't support sparse mappings, and 156311886Sbrandon.potter@amd.com // some applications can map a couple of gigabytes of space 156411886Sbrandon.potter@amd.com // (intending sparse usage), that can get painfully expensive. 156511886Sbrandon.potter@amd.com // Fortunately, since we don't properly implement munmap either, 156611886Sbrandon.potter@amd.com // there's no danger of remapping used memory, so for now all 156711886Sbrandon.potter@amd.com // newly mapped memory should already be zeroed so we can skip it. 156811886Sbrandon.potter@amd.com } else { 156911886Sbrandon.potter@amd.com // It is possible to mmap an area larger than a file, however 157014024Sgabeblack@google.com // accessing unmapped portions the system triggers a "Bus error" 157111886Sbrandon.potter@amd.com // on the host. We must know when to stop copying the file from 157211886Sbrandon.potter@amd.com // the host into the target address space. 157313867Salexandru.dutu@amd.com struct stat file_stat; 157413867Salexandru.dutu@amd.com if (fstat(sim_fd, &file_stat) > 0) 157513867Salexandru.dutu@amd.com fatal("mmap: cannot stat file"); 157613867Salexandru.dutu@amd.com 157711886Sbrandon.potter@amd.com // Copy the portion of the file that is resident. This requires 157811886Sbrandon.potter@amd.com // checking both the mmap size and the filesize that we are 157911886Sbrandon.potter@amd.com // trying to mmap into this space; the mmap size also depends 158011911SBrandon.Potter@amd.com // on the specified offset into the file. 158111911SBrandon.Potter@amd.com uint64_t size = std::min((uint64_t)file_stat.st_size - offset, 158211911SBrandon.Potter@amd.com length); 158311911SBrandon.Potter@amd.com tp.writeBlob(start, pmap, size); 158411911SBrandon.Potter@amd.com 158511911SBrandon.Potter@amd.com // Cleanup the mmap region before exiting this function. 158611911SBrandon.Potter@amd.com munmap(pmap, length); 158711886Sbrandon.potter@amd.com 158811886Sbrandon.potter@amd.com // Maintain the symbol table for dynamic executables. 158911886Sbrandon.potter@amd.com // The loader will call mmap to map the images into its address 159011886Sbrandon.potter@amd.com // space and we intercept that here. We can verify that we are 159114024Sgabeblack@google.com // executing inside the loader by checking the program counter value. 159211886Sbrandon.potter@amd.com // XXX: with multiprogrammed workloads or multi-node configurations, 159311886Sbrandon.potter@amd.com // this will not work since there is a single global symbol table. 159411886Sbrandon.potter@amd.com ObjectFile *interpreter = p->getInterpreter(); 159511886Sbrandon.potter@amd.com if (interpreter) { 159611886Sbrandon.potter@amd.com Addr text_start = interpreter->textBase(); 159711886Sbrandon.potter@amd.com Addr text_end = text_start + interpreter->textSize(); 159811886Sbrandon.potter@amd.com 159913536Sandreas.sandberg@arm.com Addr pc = tc->pcState().pc(); 160011886Sbrandon.potter@amd.com 160111886Sbrandon.potter@amd.com if (pc >= text_start && pc < text_end) { 160211886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*p->fds)[tgt_fd]; 160311886Sbrandon.potter@amd.com auto ffdp = std::dynamic_pointer_cast<FileFDEntry>(fdep); 160411886Sbrandon.potter@amd.com ObjectFile *lib = createObjectFile(ffdp->getFileName()); 160511886Sbrandon.potter@amd.com 160611886Sbrandon.potter@amd.com if (lib) { 160711886Sbrandon.potter@amd.com lib->loadAllSymbols(debugSymbolTable, 160811886Sbrandon.potter@amd.com lib->textBase(), start); 160911886Sbrandon.potter@amd.com } 161013867Salexandru.dutu@amd.com } 161113867Salexandru.dutu@amd.com } 161213867Salexandru.dutu@amd.com 161313867Salexandru.dutu@amd.com // Note that we do not zero out the remainder of the mapping. This 161413867Salexandru.dutu@amd.com // is done by a real system, but it probably will not affect 161513867Salexandru.dutu@amd.com // execution (hopefully). 161613867Salexandru.dutu@amd.com } 161713867Salexandru.dutu@amd.com 161813867Salexandru.dutu@amd.com return start; 161913867Salexandru.dutu@amd.com} 162013867Salexandru.dutu@amd.com 162111886Sbrandon.potter@amd.comtemplate <class OS> 162211886Sbrandon.potter@amd.comSyscallReturn 162311886Sbrandon.potter@amd.compwrite64Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 162411886Sbrandon.potter@amd.com{ 16251706SN/A int index = 0; 16261706SN/A int tgt_fd = p->getSyscallArg(tc, index); 16271706SN/A Addr bufPtr = p->getSyscallArg(tc, index); 16281706SN/A int nbytes = p->getSyscallArg(tc, index); 162913995Sbrandon.potter@amd.com int offset = p->getSyscallArg(tc, index); 16301706SN/A 16316701Sgblack@eecs.umich.edu auto ffdp = std::dynamic_pointer_cast<FileFDEntry>((*p->fds)[tgt_fd]); 163213995Sbrandon.potter@amd.com if (!ffdp) 163311856Sbrandon.potter@amd.com return -EBADF; 163411856Sbrandon.potter@amd.com int sim_fd = ffdp->getSimFD(); 16351706SN/A 163611856Sbrandon.potter@amd.com BufferArg bufArg(bufPtr, nbytes); 163711856Sbrandon.potter@amd.com bufArg.copyIn(tc->getMemProxy()); 16381706SN/A 163911856Sbrandon.potter@amd.com int bytes_written = pwrite(sim_fd, bufArg.bufferPtr(), nbytes, offset); 16401706SN/A 16411706SN/A return (bytes_written == -1) ? -errno : bytes_written; 164210931Sbrandon.potter@amd.com} 16431706SN/A 16441706SN/A/// Target mmap() handler. 16452218SN/Atemplate <class OS> 16461706SN/ASyscallReturn 164714024Sgabeblack@google.commmapFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 16481706SN/A{ 16491706SN/A return mmapImpl<OS>(desc, num, p, tc, false); 16501706SN/A} 16511706SN/A 165213572Sbrandon.potter@amd.com/// Target mmap2() handler. 165313572Sbrandon.potter@amd.comtemplate <class OS> 165413572Sbrandon.potter@amd.comSyscallReturn 165513995Sbrandon.potter@amd.commmap2Func(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 165613572Sbrandon.potter@amd.com{ 165713572Sbrandon.potter@amd.com return mmapImpl<OS>(desc, num, p, tc, true); 165813995Sbrandon.potter@amd.com} 165913572Sbrandon.potter@amd.com 166013572Sbrandon.potter@amd.com/// Target getrlimit() handler. 166113572Sbrandon.potter@amd.comtemplate <class OS> 166213572Sbrandon.potter@amd.comSyscallReturn 166313572Sbrandon.potter@amd.comgetrlimitFunc(SyscallDesc *desc, int callnum, Process *process, 166413572Sbrandon.potter@amd.com ThreadContext *tc) 166513572Sbrandon.potter@amd.com{ 166614024Sgabeblack@google.com int index = 0; 166713572Sbrandon.potter@amd.com unsigned resource = process->getSyscallArg(tc, index); 166813572Sbrandon.potter@amd.com TypedBufferArg<typename OS::rlimit> rlp(process->getSyscallArg(tc, index)); 166913572Sbrandon.potter@amd.com 167013572Sbrandon.potter@amd.com switch (resource) { 167113572Sbrandon.potter@amd.com case OS::TGT_RLIMIT_STACK: 167213572Sbrandon.potter@amd.com // max stack size in bytes: make up a number (8MB for now) 167314010Sgabeblack@google.com rlp->rlim_cur = rlp->rlim_max = 8 * 1024 * 1024; 167413572Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 167513572Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 167613572Sbrandon.potter@amd.com break; 167713572Sbrandon.potter@amd.com 167813572Sbrandon.potter@amd.com case OS::TGT_RLIMIT_DATA: 167913572Sbrandon.potter@amd.com // max data segment size in bytes: make up a number 168013572Sbrandon.potter@amd.com rlp->rlim_cur = rlp->rlim_max = 256 * 1024 * 1024; 168113572Sbrandon.potter@amd.com rlp->rlim_cur = TheISA::htog(rlp->rlim_cur); 168213572Sbrandon.potter@amd.com rlp->rlim_max = TheISA::htog(rlp->rlim_max); 168313572Sbrandon.potter@amd.com break; 168414010Sgabeblack@google.com 168513572Sbrandon.potter@amd.com default: 168613572Sbrandon.potter@amd.com warn("getrlimit: unimplemented resource %d", resource); 168713572Sbrandon.potter@amd.com return -EINVAL; 168813572Sbrandon.potter@amd.com break; 168913572Sbrandon.potter@amd.com } 169013572Sbrandon.potter@amd.com 16911706SN/A rlp.copyOut(tc->getMemProxy()); 16921999SN/A return 0; 16931999SN/A} 16941999SN/A 169513995Sbrandon.potter@amd.com/// Target clock_gettime() function. 16961999SN/Atemplate <class OS> 16976701Sgblack@eecs.umich.eduSyscallReturn 169813995Sbrandon.potter@amd.comclock_gettimeFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 169911856Sbrandon.potter@amd.com{ 170010931Sbrandon.potter@amd.com int index = 1; 170111856Sbrandon.potter@amd.com //int clk_id = p->getSyscallArg(tc, index); 170211856Sbrandon.potter@amd.com TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 17031999SN/A 170411856Sbrandon.potter@amd.com getElapsedTimeNano(tp->tv_sec, tp->tv_nsec); 17051999SN/A tp->tv_sec += seconds_since_epoch; 170614024Sgabeblack@google.com tp->tv_sec = TheISA::htog(tp->tv_sec); 170711856Sbrandon.potter@amd.com tp->tv_nsec = TheISA::htog(tp->tv_nsec); 170811856Sbrandon.potter@amd.com 17091999SN/A tp.copyOut(tc->getMemProxy()); 17106227Snate@binkert.org 17111999SN/A return 0; 17122461SN/A} 171311856Sbrandon.potter@amd.com 171414010Sgabeblack@google.com/// Target clock_getres() function. 17158737Skoansin.tan@gmail.comtemplate <class OS> 17161999SN/ASyscallReturn 171714010Sgabeblack@google.comclock_getresFunc(SyscallDesc *desc, int num, Process *p, ThreadContext *tc) 171811856Sbrandon.potter@amd.com{ 17191999SN/A int index = 1; 17201999SN/A TypedBufferArg<typename OS::timespec> tp(p->getSyscallArg(tc, index)); 172110931Sbrandon.potter@amd.com 17221999SN/A // Set resolution at ns, which is what clock_gettime() returns 17236227Snate@binkert.org tp->tv_sec = 0; 17241999SN/A tp->tv_nsec = 1; 17251999SN/A 172613572Sbrandon.potter@amd.com tp.copyOut(tc->getMemProxy()); 17271999SN/A 17281999SN/A return 0; 172911385Sbrandon.potter@amd.com} 1730360SN/A 17311450SN/A/// Target gettimeofday() handler. 173213995Sbrandon.potter@amd.comtemplate <class OS> 1733360SN/ASyscallReturn 17346701Sgblack@eecs.umich.edugettimeofdayFunc(SyscallDesc *desc, int callnum, Process *process, 173513995Sbrandon.potter@amd.com ThreadContext *tc) 17366701Sgblack@eecs.umich.edu{ 17376701Sgblack@eecs.umich.edu int index = 0; 173811383Sbrandon.potter@amd.com TypedBufferArg<typename OS::timeval> tp(process->getSyscallArg(tc, index)); 173911383Sbrandon.potter@amd.com 17408324Ssteve.reinhardt@amd.com getElapsedTimeMicro(tp->tv_sec, tp->tv_usec); 174110486Stjablin@gmail.com tp->tv_sec += seconds_since_epoch; 1742360SN/A tp->tv_sec = TheISA::htog(tp->tv_sec); 174311385Sbrandon.potter@amd.com tp->tv_usec = TheISA::htog(tp->tv_usec); 174411385Sbrandon.potter@amd.com 17459008Sgblack@eecs.umich.edu tp.copyOut(tc->getMemProxy()); 174611383Sbrandon.potter@amd.com 174711383Sbrandon.potter@amd.com return 0; 174811383Sbrandon.potter@amd.com} 174911383Sbrandon.potter@amd.com 175011383Sbrandon.potter@amd.com 175111383Sbrandon.potter@amd.com/// Target utimes() handler. 175211383Sbrandon.potter@amd.comtemplate <class OS> 175311383Sbrandon.potter@amd.comSyscallReturn 175411383Sbrandon.potter@amd.comutimesFunc(SyscallDesc *desc, int callnum, Process *process, 17558324Ssteve.reinhardt@amd.com ThreadContext *tc) 175611383Sbrandon.potter@amd.com{ 175711383Sbrandon.potter@amd.com std::string path; 175811383Sbrandon.potter@amd.com 175911383Sbrandon.potter@amd.com int index = 0; 176011383Sbrandon.potter@amd.com if (!tc->getMemProxy().tryReadString(path, 176111383Sbrandon.potter@amd.com process->getSyscallArg(tc, index))) { 176211383Sbrandon.potter@amd.com return -EFAULT; 176311383Sbrandon.potter@amd.com } 176411383Sbrandon.potter@amd.com 176511383Sbrandon.potter@amd.com TypedBufferArg<typename OS::timeval [2]> 176611383Sbrandon.potter@amd.com tp(process->getSyscallArg(tc, index)); 176711383Sbrandon.potter@amd.com tp.copyIn(tc->getMemProxy()); 176811383Sbrandon.potter@amd.com 176911383Sbrandon.potter@amd.com struct timeval hostTimeval[2]; 177011383Sbrandon.potter@amd.com for (int i = 0; i < 2; ++i) { 177111383Sbrandon.potter@amd.com hostTimeval[i].tv_sec = TheISA::gtoh((*tp)[i].tv_sec); 177211383Sbrandon.potter@amd.com hostTimeval[i].tv_usec = TheISA::gtoh((*tp)[i].tv_usec); 177311383Sbrandon.potter@amd.com } 177411383Sbrandon.potter@amd.com 177511383Sbrandon.potter@amd.com // Adjust path for current working directory 177611383Sbrandon.potter@amd.com path = process->fullPath(path); 177711383Sbrandon.potter@amd.com 177811383Sbrandon.potter@amd.com int result = utimes(path.c_str(), hostTimeval); 177911383Sbrandon.potter@amd.com 17808324Ssteve.reinhardt@amd.com if (result < 0) 17815877Shsul@eecs.umich.edu return -errno; 178210486Stjablin@gmail.com 178310486Stjablin@gmail.com return 0; 178411383Sbrandon.potter@amd.com} 178511383Sbrandon.potter@amd.com 178611383Sbrandon.potter@amd.comtemplate <class OS> 178711856Sbrandon.potter@amd.comSyscallReturn 178811624Smichael.lebeane@amd.comexecveFunc(SyscallDesc *desc, int callnum, Process *p, ThreadContext *tc) 178911856Sbrandon.potter@amd.com{ 179011856Sbrandon.potter@amd.com desc->setFlags(0); 179111856Sbrandon.potter@amd.com 179213995Sbrandon.potter@amd.com int index = 0; 179313995Sbrandon.potter@amd.com std::string path; 179411624Smichael.lebeane@amd.com SETranslatingPortProxy & mem_proxy = tc->getMemProxy(); 179511624Smichael.lebeane@amd.com if (!mem_proxy.tryReadString(path, p->getSyscallArg(tc, index))) 179611856Sbrandon.potter@amd.com return -EFAULT; 179711856Sbrandon.potter@amd.com 179811383Sbrandon.potter@amd.com if (access(path.c_str(), F_OK) == -1) 179911856Sbrandon.potter@amd.com return -EACCES; 1800360SN/A 180111913SBrandon.Potter@amd.com auto read_in = [](std::vector<std::string> & vect, 180211383Sbrandon.potter@amd.com SETranslatingPortProxy & mem_proxy, 18038600Ssteve.reinhardt@amd.com Addr mem_loc) 180411383Sbrandon.potter@amd.com { 180511383Sbrandon.potter@amd.com for (int inc = 0; ; inc++) { 180611383Sbrandon.potter@amd.com BufferArg b((mem_loc + sizeof(Addr) * inc), sizeof(Addr)); 18078600Ssteve.reinhardt@amd.com b.copyIn(mem_proxy); 18082544SN/A 18092544SN/A if (!*(Addr*)b.bufferPtr()) 181011383Sbrandon.potter@amd.com break; 181111383Sbrandon.potter@amd.com 181211383Sbrandon.potter@amd.com vect.push_back(std::string()); 181311905SBrandon.Potter@amd.com mem_proxy.tryReadString(vect[inc], *(Addr*)b.bufferPtr()); 181411905SBrandon.Potter@amd.com } 181511905SBrandon.Potter@amd.com }; 181611905SBrandon.Potter@amd.com 181711905SBrandon.Potter@amd.com /** 181811905SBrandon.Potter@amd.com * Note that ProcessParams is generated by swig and there are no other 181911905SBrandon.Potter@amd.com * examples of how to create anything but this default constructor. The 182011383Sbrandon.potter@amd.com * fields are manually initialized instead of passing parameters to the 182111383Sbrandon.potter@amd.com * constructor. 182211383Sbrandon.potter@amd.com */ 182311383Sbrandon.potter@amd.com ProcessParams *pp = new ProcessParams(); 182411383Sbrandon.potter@amd.com pp->executable = path; 182511383Sbrandon.potter@amd.com Addr argv_mem_loc = p->getSyscallArg(tc, index); 182611383Sbrandon.potter@amd.com read_in(pp->cmd, mem_proxy, argv_mem_loc); 182711383Sbrandon.potter@amd.com Addr envp_mem_loc = p->getSyscallArg(tc, index); 182811383Sbrandon.potter@amd.com read_in(pp->env, mem_proxy, envp_mem_loc); 182911383Sbrandon.potter@amd.com pp->uid = p->uid(); 183011383Sbrandon.potter@amd.com pp->egid = p->egid(); 183111383Sbrandon.potter@amd.com pp->euid = p->euid(); 183211383Sbrandon.potter@amd.com pp->gid = p->gid(); 183311383Sbrandon.potter@amd.com pp->ppid = p->ppid(); 183411383Sbrandon.potter@amd.com pp->pid = p->pid(); 18358600Ssteve.reinhardt@amd.com pp->input.assign("cin"); 18366672Sgblack@eecs.umich.edu pp->output.assign("cout"); 18378600Ssteve.reinhardt@amd.com pp->errout.assign("cerr"); 183811383Sbrandon.potter@amd.com pp->cwd.assign(p->getcwd()); 183911383Sbrandon.potter@amd.com pp->system = p->system; 184011383Sbrandon.potter@amd.com /** 18418601Ssteve.reinhardt@amd.com * Prevent process object creation with identical PIDs (which will trip 18422544SN/A * a fatal check in Process constructor). The execve call is supposed to 184311383Sbrandon.potter@amd.com * take over the currently executing process' identity but replace 184414024Sgabeblack@google.com * whatever it is doing with a new process image. Instead of hijacking 184511383Sbrandon.potter@amd.com * the process object in the simulator, we create a new process object 184611383Sbrandon.potter@amd.com * and bind to the previous process' thread below (hijacking the thread). 184711383Sbrandon.potter@amd.com */ 184811383Sbrandon.potter@amd.com p->system->PIDs.erase(p->pid()); 184911383Sbrandon.potter@amd.com Process *new_p = pp->create(); 185011383Sbrandon.potter@amd.com delete pp; 185111383Sbrandon.potter@amd.com 185211383Sbrandon.potter@amd.com /** 185311383Sbrandon.potter@amd.com * Work through the file descriptor array and close any files marked 185411383Sbrandon.potter@amd.com * close-on-exec. 185511383Sbrandon.potter@amd.com */ 185611383Sbrandon.potter@amd.com new_p->fds = p->fds; 185711383Sbrandon.potter@amd.com for (int i = 0; i < new_p->fds->getSize(); i++) { 185811383Sbrandon.potter@amd.com std::shared_ptr<FDEntry> fdep = (*new_p->fds)[i]; 185911383Sbrandon.potter@amd.com if (fdep && fdep->getCOE()) 186011383Sbrandon.potter@amd.com new_p->fds->closeFDEntry(i); 186111383Sbrandon.potter@amd.com } 186211383Sbrandon.potter@amd.com 186311383Sbrandon.potter@amd.com *new_p->sigchld = true; 186411383Sbrandon.potter@amd.com 186511383Sbrandon.potter@amd.com delete p; 186611383Sbrandon.potter@amd.com tc->clearArchRegs(); 186711383Sbrandon.potter@amd.com tc->setProcessPtr(new_p); 186811383Sbrandon.potter@amd.com new_p->assignThreadContext(tc->contextId()); 186911383Sbrandon.potter@amd.com new_p->initState(); 187011383Sbrandon.potter@amd.com tc->activate(); 187111383Sbrandon.potter@amd.com TheISA::PCState pcState = tc->pcState(); 187211383Sbrandon.potter@amd.com tc->setNPC(pcState.instAddr()); 187311383Sbrandon.potter@amd.com 187411383Sbrandon.potter@amd.com desc->setFlags(SyscallDesc::SuppressReturnValue); 187511392Sbrandon.potter@amd.com return 0; 187611392Sbrandon.potter@amd.com} 187711392Sbrandon.potter@amd.com 187811392Sbrandon.potter@amd.com/// Target getrusage() function. 187911392Sbrandon.potter@amd.comtemplate <class OS> 188011392Sbrandon.potter@amd.comSyscallReturn 188111392Sbrandon.potter@amd.comgetrusageFunc(SyscallDesc *desc, int callnum, Process *process, 188211392Sbrandon.potter@amd.com ThreadContext *tc) 188311392Sbrandon.potter@amd.com{ 188411392Sbrandon.potter@amd.com int index = 0; 188511392Sbrandon.potter@amd.com int who = process->getSyscallArg(tc, index); // THREAD, SELF, or CHILDREN 188611392Sbrandon.potter@amd.com TypedBufferArg<typename OS::rusage> rup(process->getSyscallArg(tc, index)); 188711392Sbrandon.potter@amd.com 188811392Sbrandon.potter@amd.com rup->ru_utime.tv_sec = 0; 188911856Sbrandon.potter@amd.com rup->ru_utime.tv_usec = 0; 189011856Sbrandon.potter@amd.com rup->ru_stime.tv_sec = 0; 189111856Sbrandon.potter@amd.com rup->ru_stime.tv_usec = 0; 189211392Sbrandon.potter@amd.com rup->ru_maxrss = 0; 189311392Sbrandon.potter@amd.com rup->ru_ixrss = 0; 189411392Sbrandon.potter@amd.com rup->ru_idrss = 0; 189511392Sbrandon.potter@amd.com rup->ru_isrss = 0; 189611392Sbrandon.potter@amd.com rup->ru_minflt = 0; 189711392Sbrandon.potter@amd.com rup->ru_majflt = 0; 189811392Sbrandon.potter@amd.com rup->ru_nswap = 0; 189911392Sbrandon.potter@amd.com rup->ru_inblock = 0; 190011383Sbrandon.potter@amd.com rup->ru_oublock = 0; 190111383Sbrandon.potter@amd.com rup->ru_msgsnd = 0; 190211383Sbrandon.potter@amd.com rup->ru_msgrcv = 0; 190311383Sbrandon.potter@amd.com rup->ru_nsignals = 0; 190411383Sbrandon.potter@amd.com rup->ru_nvcsw = 0; 19051458SN/A rup->ru_nivcsw = 0; 1906360SN/A 1907360SN/A switch (who) { 190811593Santhony.gutierrez@amd.com case OS::TGT_RUSAGE_SELF: 190911593Santhony.gutierrez@amd.com getElapsedTimeMicro(rup->ru_utime.tv_sec, rup->ru_utime.tv_usec); 191013995Sbrandon.potter@amd.com rup->ru_utime.tv_sec = TheISA::htog(rup->ru_utime.tv_sec); 191111593Santhony.gutierrez@amd.com rup->ru_utime.tv_usec = TheISA::htog(rup->ru_utime.tv_usec); 191211593Santhony.gutierrez@amd.com break; 191313995Sbrandon.potter@amd.com 191411593Santhony.gutierrez@amd.com case OS::TGT_RUSAGE_CHILDREN: 191511593Santhony.gutierrez@amd.com // do nothing. We have no child processes, so they take no time. 191611593Santhony.gutierrez@amd.com break; 191711593Santhony.gutierrez@amd.com 191811593Santhony.gutierrez@amd.com default: 191911856Sbrandon.potter@amd.com // don't really handle THREAD or CHILDREN, but just warn and 192011856Sbrandon.potter@amd.com // plow ahead 192111593Santhony.gutierrez@amd.com warn("getrusage() only supports RUSAGE_SELF. Parameter %d ignored.", 192211856Sbrandon.potter@amd.com who); 192311593Santhony.gutierrez@amd.com } 192411593Santhony.gutierrez@amd.com 192514024Sgabeblack@google.com rup.copyOut(tc->getMemProxy()); 192611593Santhony.gutierrez@amd.com 192711594Santhony.gutierrez@amd.com return 0; 192811593Santhony.gutierrez@amd.com} 192911593Santhony.gutierrez@amd.com 193011593Santhony.gutierrez@amd.com/// Target times() function. 193111593Santhony.gutierrez@amd.comtemplate <class OS> 193211385Sbrandon.potter@amd.comSyscallReturn 193311385Sbrandon.potter@amd.comtimesFunc(SyscallDesc *desc, int callnum, Process *process, 193411385Sbrandon.potter@amd.com ThreadContext *tc) 193513995Sbrandon.potter@amd.com{ 193611385Sbrandon.potter@amd.com int index = 0; 193713995Sbrandon.potter@amd.com TypedBufferArg<typename OS::tms> bufp(process->getSyscallArg(tc, index)); 193811385Sbrandon.potter@amd.com 193911385Sbrandon.potter@amd.com // Fill in the time structure (in clocks) 194011385Sbrandon.potter@amd.com int64_t clocks = curTick() * OS::M5_SC_CLK_TCK / SimClock::Int::s; 194111385Sbrandon.potter@amd.com bufp->tms_utime = clocks; 194211385Sbrandon.potter@amd.com bufp->tms_stime = 0; 194313995Sbrandon.potter@amd.com bufp->tms_cutime = 0; 194411385Sbrandon.potter@amd.com bufp->tms_cstime = 0; 194513995Sbrandon.potter@amd.com 194611385Sbrandon.potter@amd.com // Convert to host endianness 194711385Sbrandon.potter@amd.com bufp->tms_utime = TheISA::htog(bufp->tms_utime); 1948378SN/A 1949360SN/A // Write back 19501450SN/A bufp.copyOut(tc->getMemProxy()); 195113995Sbrandon.potter@amd.com 1952360SN/A // Return clock ticks since system boot 19536701Sgblack@eecs.umich.edu return clocks; 195413995Sbrandon.potter@amd.com} 19556701Sgblack@eecs.umich.edu 19566701Sgblack@eecs.umich.edu/// Target time() function. 1957360SN/Atemplate <class OS> 1958360SN/ASyscallReturn 195911906SBrandon.Potter@amd.comtimeFunc(SyscallDesc *desc, int callnum, Process *process, ThreadContext *tc) 196011906SBrandon.Potter@amd.com{ 196111906SBrandon.Potter@amd.com typename OS::time_t sec, usec; 196211906SBrandon.Potter@amd.com getElapsedTimeMicro(sec, usec); 196311906SBrandon.Potter@amd.com sec += seconds_since_epoch; 196411906SBrandon.Potter@amd.com 1965360SN/A int index = 0; 196611906SBrandon.Potter@amd.com Addr taddr = (Addr)process->getSyscallArg(tc, index); 196711906SBrandon.Potter@amd.com if (taddr != 0) { 196811906SBrandon.Potter@amd.com typename OS::time_t t = sec; 196911906SBrandon.Potter@amd.com t = TheISA::htog(t); 197011906SBrandon.Potter@amd.com SETranslatingPortProxy &p = tc->getMemProxy(); 197111906SBrandon.Potter@amd.com p.writeBlob(taddr, (uint8_t*)&t, (int)sizeof(typename OS::time_t)); 19725877Shsul@eecs.umich.edu } 197311906SBrandon.Potter@amd.com return sec; 197411906SBrandon.Potter@amd.com} 197511906SBrandon.Potter@amd.com 197611906SBrandon.Potter@amd.com 1977360SN/A#endif // __SIM_SYSCALL_EMUL_HH__ 1978360SN/A