process.cc revision 11389
12SN/A/* 210298Salexandru.dutu@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc. 38852Sandreas.hansson@arm.com * Copyright (c) 2012 ARM Limited 48852Sandreas.hansson@arm.com * All rights reserved 58852Sandreas.hansson@arm.com * 68852Sandreas.hansson@arm.com * The license below extends only to copyright in the software and shall 78852Sandreas.hansson@arm.com * not be construed as granting a license to any other intellectual 88852Sandreas.hansson@arm.com * property including but not limited to intellectual property relating 98852Sandreas.hansson@arm.com * to a hardware implementation of the functionality of the software 108852Sandreas.hansson@arm.com * licensed hereunder. You may use the software subject to the license 118852Sandreas.hansson@arm.com * terms below provided that you ensure that this notice is replicated 128852Sandreas.hansson@arm.com * unmodified and in its entirety in all distributions of the software, 138852Sandreas.hansson@arm.com * modified or unmodified, in source code or in binary form. 148852Sandreas.hansson@arm.com * 151762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 162SN/A * All rights reserved. 172SN/A * 182SN/A * Redistribution and use in source and binary forms, with or without 192SN/A * modification, are permitted provided that the following conditions are 202SN/A * met: redistributions of source code must retain the above copyright 212SN/A * notice, this list of conditions and the following disclaimer; 222SN/A * redistributions in binary form must reproduce the above copyright 232SN/A * notice, this list of conditions and the following disclaimer in the 242SN/A * documentation and/or other materials provided with the distribution; 252SN/A * neither the name of the copyright holders nor the names of its 262SN/A * contributors may be used to endorse or promote products derived from 272SN/A * this software without specific prior written permission. 282SN/A * 292SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 302SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 312SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 322SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 332SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 342SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 352SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 362SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 372SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 382SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 392SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 402665Ssaidi@eecs.umich.edu * 412665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 422665Ssaidi@eecs.umich.edu * Steve Reinhardt 432665Ssaidi@eecs.umich.edu * Ali Saidi 442SN/A */ 452SN/A 468229Snate@binkert.org#include <fcntl.h> 472SN/A#include <unistd.h> 486712Snate@binkert.org 496712Snate@binkert.org#include <cstdio> 5010930Sbrandon.potter@amd.com#include <map> 512SN/A#include <string> 522SN/A 5356SN/A#include "base/loader/object_file.hh" 541158SN/A#include "base/loader/symtab.hh" 558229Snate@binkert.org#include "base/intmath.hh" 56146SN/A#include "base/statistics.hh" 576658Snate@binkert.org#include "config/the_isa.hh" 582680Sktlim@umich.edu#include "cpu/thread_context.hh" 592378SN/A#include "mem/page_table.hh" 6010298Salexandru.dutu@amd.com#include "mem/multi_level_page_table.hh" 618706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh" 628229Snate@binkert.org#include "params/LiveProcess.hh" 635154Sgblack@eecs.umich.edu#include "params/Process.hh" 645512SMichael.Adler@intel.com#include "sim/debug.hh" 65360SN/A#include "sim/process.hh" 664434Ssaidi@eecs.umich.edu#include "sim/process_impl.hh" 67695SN/A#include "sim/stats.hh" 682093SN/A#include "sim/syscall_emul.hh" 692378SN/A#include "sim/system.hh" 702SN/A 712715Sstever@eecs.umich.edu#if THE_ISA == ALPHA_ISA 722715Sstever@eecs.umich.edu#include "arch/alpha/linux/process.hh" 732715Sstever@eecs.umich.edu#include "arch/alpha/tru64/process.hh" 742715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 752715Sstever@eecs.umich.edu#include "arch/sparc/linux/process.hh" 762715Sstever@eecs.umich.edu#include "arch/sparc/solaris/process.hh" 772715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 782715Sstever@eecs.umich.edu#include "arch/mips/linux/process.hh" 795335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA 805335Shines@cs.fsu.edu#include "arch/arm/linux/process.hh" 8110810Sbr@bsdpad.com#include "arch/arm/freebsd/process.hh" 824157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 834166Sgblack@eecs.umich.edu#include "arch/x86/linux/process.hh" 846691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA 856691Stjones1@inf.ed.ac.uk#include "arch/power/linux/process.hh" 862715Sstever@eecs.umich.edu#else 872715Sstever@eecs.umich.edu#error "THE_ISA not set" 882715Sstever@eecs.umich.edu#endif 892715Sstever@eecs.umich.edu 902715Sstever@eecs.umich.edu 912SN/Ausing namespace std; 922107SN/Ausing namespace TheISA; 932SN/A 942SN/A// current number of allocated processes 952SN/Aint num_processes = 0; 962SN/A 975758Shsul@eecs.umich.edutemplate<class IntType> 9810932Sbrandon.potter@amd.com 995771Shsul@eecs.umich.eduAuxVector<IntType>::AuxVector(IntType type, IntType val) 1005758Shsul@eecs.umich.edu{ 1015758Shsul@eecs.umich.edu a_type = TheISA::htog(type); 1025758Shsul@eecs.umich.edu a_val = TheISA::htog(val); 1035758Shsul@eecs.umich.edu} 1045758Shsul@eecs.umich.edu 1058737Skoansin.tan@gmail.comtemplate struct AuxVector<uint32_t>; 1068737Skoansin.tan@gmail.comtemplate struct AuxVector<uint64_t>; 1075758Shsul@eecs.umich.edu 10810930Sbrandon.potter@amd.comstatic int 10910930Sbrandon.potter@amd.comopenFile(const string& filename, int flags, mode_t mode) 11010930Sbrandon.potter@amd.com{ 11110930Sbrandon.potter@amd.com int sim_fd = open(filename.c_str(), flags, mode); 11210930Sbrandon.potter@amd.com if (sim_fd != -1) 11310930Sbrandon.potter@amd.com return sim_fd; 11410930Sbrandon.potter@amd.com fatal("Unable to open %s with mode %O", filename, mode); 11510930Sbrandon.potter@amd.com} 11610930Sbrandon.potter@amd.com 11710930Sbrandon.potter@amd.comstatic int 11810930Sbrandon.potter@amd.comopenInputFile(const string &filename) 11910930Sbrandon.potter@amd.com{ 12010930Sbrandon.potter@amd.com return openFile(filename, O_RDONLY, 0); 12110930Sbrandon.potter@amd.com} 12210930Sbrandon.potter@amd.com 12310930Sbrandon.potter@amd.comstatic int 12410930Sbrandon.potter@amd.comopenOutputFile(const string &filename) 12510930Sbrandon.potter@amd.com{ 12610930Sbrandon.potter@amd.com return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); 12710930Sbrandon.potter@amd.com} 12810930Sbrandon.potter@amd.com 1295154Sgblack@eecs.umich.eduProcess::Process(ProcessParams * params) 1307532Ssteve.reinhardt@amd.com : SimObject(params), system(params->system), 13110559Sandreas.hansson@arm.com brk_point(0), stack_base(0), stack_size(0), stack_min(0), 1328852Sandreas.hansson@arm.com max_stack_size(params->max_stack_size), 13310559Sandreas.hansson@arm.com next_thread_stack_base(0), 1348852Sandreas.hansson@arm.com M5_pid(system->allocatePID()), 13510299Salexandru.dutu@amd.com useArchPT(params->useArchPT), 13610554Salexandru.dutu@amd.com kvmInSE(params->kvmInSE), 13710299Salexandru.dutu@amd.com pTable(useArchPT ? 13810299Salexandru.dutu@amd.com static_cast<PageTableBase *>(new ArchPageTable(name(), M5_pid, system)) : 13910299Salexandru.dutu@amd.com static_cast<PageTableBase *>(new FuncPageTable(name(), M5_pid)) ), 1408852Sandreas.hansson@arm.com initVirtMem(system->getSystemPort(), this, 14110929Sbrandon.potter@amd.com SETranslatingPortProxy::Always), 14210930Sbrandon.potter@amd.com fd_array(make_shared<array<FDEntry, NUM_FDS>>()), 14310930Sbrandon.potter@amd.com imap {{"", -1}, 14410930Sbrandon.potter@amd.com {"cin", STDIN_FILENO}, 14510930Sbrandon.potter@amd.com {"stdin", STDIN_FILENO}}, 14610930Sbrandon.potter@amd.com oemap{{"", -1}, 14710930Sbrandon.potter@amd.com {"cout", STDOUT_FILENO}, 14810930Sbrandon.potter@amd.com {"stdout", STDOUT_FILENO}, 14910930Sbrandon.potter@amd.com {"cerr", STDERR_FILENO}, 15010930Sbrandon.potter@amd.com {"stderr", STDERR_FILENO}} 1512SN/A{ 15210930Sbrandon.potter@amd.com int sim_fd; 15310930Sbrandon.potter@amd.com std::map<string,int>::iterator it; 1545154Sgblack@eecs.umich.edu 15510930Sbrandon.potter@amd.com // Search through the input options and set fd if match is found; 15610930Sbrandon.potter@amd.com // otherwise, open an input file and seek to location. 15710932Sbrandon.potter@amd.com FDEntry *fde_stdin = getFDEntry(STDIN_FILENO); 15810930Sbrandon.potter@amd.com if ((it = imap.find(params->input)) != imap.end()) 15910930Sbrandon.potter@amd.com sim_fd = it->second; 16010930Sbrandon.potter@amd.com else 16110930Sbrandon.potter@amd.com sim_fd = openInputFile(params->input); 16210930Sbrandon.potter@amd.com fde_stdin->set(sim_fd, params->input, O_RDONLY, -1, false); 1635154Sgblack@eecs.umich.edu 16410930Sbrandon.potter@amd.com // Search through the output/error options and set fd if match is found; 16510930Sbrandon.potter@amd.com // otherwise, open an output file and seek to location. 16610932Sbrandon.potter@amd.com FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO); 16710930Sbrandon.potter@amd.com if ((it = oemap.find(params->output)) != oemap.end()) 16810930Sbrandon.potter@amd.com sim_fd = it->second; 1695154Sgblack@eecs.umich.edu else 17010930Sbrandon.potter@amd.com sim_fd = openOutputFile(params->output); 17110930Sbrandon.potter@amd.com fde_stdout->set(sim_fd, params->output, O_WRONLY | O_CREAT | O_TRUNC, 17210930Sbrandon.potter@amd.com 0664, false); 1735154Sgblack@eecs.umich.edu 17410932Sbrandon.potter@amd.com FDEntry *fde_stderr = getFDEntry(STDERR_FILENO); 17510930Sbrandon.potter@amd.com if (params->output == params->errout) 17610930Sbrandon.potter@amd.com // Reuse the same file descriptor if these match. 17710930Sbrandon.potter@amd.com sim_fd = fde_stdout->fd; 17810930Sbrandon.potter@amd.com else if ((it = oemap.find(params->errout)) != oemap.end()) 17910930Sbrandon.potter@amd.com sim_fd = it->second; 1805154Sgblack@eecs.umich.edu else 18110930Sbrandon.potter@amd.com sim_fd = openOutputFile(params->errout); 18210930Sbrandon.potter@amd.com fde_stderr->set(sim_fd, params->errout, O_WRONLY | O_CREAT | O_TRUNC, 18310930Sbrandon.potter@amd.com 0664, false); 1842SN/A 18511386Ssteve.reinhardt@amd.com mmap_end = 0; 1861514SN/A nxm_start = nxm_end = 0; 1872SN/A // other parameters will be initialized when the program is loaded 1882SN/A} 1892SN/A 1902378SN/A 1912SN/Avoid 1922SN/AProcess::regStats() 1932SN/A{ 194729SN/A using namespace Stats; 1952SN/A 1962SN/A num_syscalls 1978240Snate@binkert.org .name(name() + ".num_syscalls") 1982SN/A .desc("Number of system calls") 1992SN/A ; 2002SN/A} 2012SN/A 20210930Sbrandon.potter@amd.comvoid 20310932Sbrandon.potter@amd.comProcess::inheritFDArray(Process *p) 2042SN/A{ 20510930Sbrandon.potter@amd.com fd_array = p->fd_array; 2062SN/A} 2072SN/A 2085713Shsul@eecs.umich.eduThreadContext * 2095713Shsul@eecs.umich.eduProcess::findFreeContext() 2102SN/A{ 21110929Sbrandon.potter@amd.com for (int id : contextIds) { 21210929Sbrandon.potter@amd.com ThreadContext *tc = system->getThreadContext(id); 21310929Sbrandon.potter@amd.com if (tc->status() == ThreadContext::Halted) 2145713Shsul@eecs.umich.edu return tc; 2155512SMichael.Adler@intel.com } 2165713Shsul@eecs.umich.edu return NULL; 2172SN/A} 2182SN/A 2191395SN/Avoid 2207532Ssteve.reinhardt@amd.comProcess::initState() 2211395SN/A{ 2225713Shsul@eecs.umich.edu if (contextIds.empty()) 2235713Shsul@eecs.umich.edu fatal("Process %s is not associated with any HW contexts!\n", name()); 2242378SN/A 2252680Sktlim@umich.edu // first thread context for this process... initialize & enable 2265713Shsul@eecs.umich.edu ThreadContext *tc = system->getThreadContext(contextIds[0]); 2271395SN/A 2281634SN/A // mark this context as active so it will start ticking. 22910407Smitch.hayenga@arm.com tc->activate(); 23010298Salexandru.dutu@amd.com 23110298Salexandru.dutu@amd.com pTable->initState(tc); 2321395SN/A} 2332SN/A 23410913Sandreas.sandberg@arm.comDrainState 23510913Sandreas.sandberg@arm.comProcess::drain() 23610905Sandreas.sandberg@arm.com{ 23710932Sbrandon.potter@amd.com findFileOffsets(); 23810913Sandreas.sandberg@arm.com return DrainState::Drained; 23910905Sandreas.sandberg@arm.com} 24010905Sandreas.sandberg@arm.com 2412SN/Aint 24210932Sbrandon.potter@amd.comProcess::allocFD(int sim_fd, const string& filename, int flags, int mode, 24310932Sbrandon.potter@amd.com bool pipe) 2442SN/A{ 24510930Sbrandon.potter@amd.com for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) { 24610932Sbrandon.potter@amd.com FDEntry *fde = getFDEntry(free_fd); 24710930Sbrandon.potter@amd.com if (fde->isFree()) { 24810930Sbrandon.potter@amd.com fde->set(sim_fd, filename, flags, mode, pipe); 2491970SN/A return free_fd; 2501970SN/A } 2512SN/A } 2522SN/A 25310930Sbrandon.potter@amd.com fatal("Out of target file descriptors"); 2541970SN/A} 2552SN/A 2561970SN/Avoid 25710932Sbrandon.potter@amd.comProcess::resetFDEntry(int tgt_fd) 2581970SN/A{ 25910932Sbrandon.potter@amd.com FDEntry *fde = getFDEntry(tgt_fd); 26010930Sbrandon.potter@amd.com assert(fde->fd > -1); 2611970SN/A 26210930Sbrandon.potter@amd.com fde->reset(); 2632SN/A} 2642SN/A 2652SN/Aint 26610932Sbrandon.potter@amd.comProcess::getSimFD(int tgt_fd) 2672SN/A{ 26810932Sbrandon.potter@amd.com FDEntry *entry = getFDEntry(tgt_fd); 26910930Sbrandon.potter@amd.com return entry ? entry->fd : -1; 2702SN/A} 2712SN/A 27210930Sbrandon.potter@amd.comFDEntry * 27310932Sbrandon.potter@amd.comProcess::getFDEntry(int tgt_fd) 2745282Srstrong@cs.ucsd.edu{ 27510930Sbrandon.potter@amd.com assert(0 <= tgt_fd && tgt_fd < fd_array->size()); 27610930Sbrandon.potter@amd.com return &(*fd_array)[tgt_fd]; 27710929Sbrandon.potter@amd.com} 2785282Srstrong@cs.ucsd.edu 27910929Sbrandon.potter@amd.comint 28010932Sbrandon.potter@amd.comProcess::getTgtFD(int sim_fd) 28110929Sbrandon.potter@amd.com{ 28210930Sbrandon.potter@amd.com for (int index = 0; index < fd_array->size(); index++) 28310930Sbrandon.potter@amd.com if ((*fd_array)[index].fd == sim_fd) 28410929Sbrandon.potter@amd.com return index; 28510929Sbrandon.potter@amd.com return -1; 2865282Srstrong@cs.ucsd.edu} 2877487Ssteve.reinhardt@amd.com 2888601Ssteve.reinhardt@amd.comvoid 2898601Ssteve.reinhardt@amd.comProcess::allocateMem(Addr vaddr, int64_t size, bool clobber) 2908601Ssteve.reinhardt@amd.com{ 29110318Sandreas.hansson@arm.com int npages = divCeil(size, (int64_t)PageBytes); 2928601Ssteve.reinhardt@amd.com Addr paddr = system->allocPhysPages(npages); 29311294Sandreas.hansson@arm.com pTable->map(vaddr, paddr, size, 29411294Sandreas.hansson@arm.com clobber ? PageTableBase::Clobber : PageTableBase::Zero); 2958601Ssteve.reinhardt@amd.com} 2968601Ssteve.reinhardt@amd.com 2974434Ssaidi@eecs.umich.edubool 2988539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr) 2994434Ssaidi@eecs.umich.edu{ 3008539Sgblack@eecs.umich.edu // Check if this is already on the stack and there's just no page there 3018539Sgblack@eecs.umich.edu // yet. 3024434Ssaidi@eecs.umich.edu if (vaddr >= stack_min && vaddr < stack_base) { 30310318Sandreas.hansson@arm.com allocateMem(roundDown(vaddr, PageBytes), PageBytes); 3044434Ssaidi@eecs.umich.edu return true; 3054434Ssaidi@eecs.umich.edu } 3064434Ssaidi@eecs.umich.edu 3078539Sgblack@eecs.umich.edu // We've accessed the next page of the stack, so extend it to include 3088539Sgblack@eecs.umich.edu // this address. 3095154Sgblack@eecs.umich.edu if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 3105154Sgblack@eecs.umich.edu while (vaddr < stack_min) { 3115154Sgblack@eecs.umich.edu stack_min -= TheISA::PageBytes; 3128539Sgblack@eecs.umich.edu if (stack_base - stack_min > max_stack_size) 3135154Sgblack@eecs.umich.edu fatal("Maximum stack size exceeded\n"); 3148601Ssteve.reinhardt@amd.com allocateMem(stack_min, TheISA::PageBytes); 3155823Ssaidi@eecs.umich.edu inform("Increasing stack size by one page."); 3165154Sgblack@eecs.umich.edu }; 3174434Ssaidi@eecs.umich.edu return true; 3184434Ssaidi@eecs.umich.edu } 3194434Ssaidi@eecs.umich.edu return false; 3204434Ssaidi@eecs.umich.edu} 3214434Ssaidi@eecs.umich.edu 3225282Srstrong@cs.ucsd.eduvoid 32310932Sbrandon.potter@amd.comProcess::fixFileOffsets() 3247487Ssteve.reinhardt@amd.com{ 32510930Sbrandon.potter@amd.com auto seek = [] (FDEntry *fde) 32610930Sbrandon.potter@amd.com { 32710930Sbrandon.potter@amd.com if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0) 32810930Sbrandon.potter@amd.com fatal("Unable to see to location in %s", fde->filename); 32910930Sbrandon.potter@amd.com }; 3305282Srstrong@cs.ucsd.edu 33110930Sbrandon.potter@amd.com std::map<string,int>::iterator it; 3325282Srstrong@cs.ucsd.edu 33310930Sbrandon.potter@amd.com // Search through the input options and set fd if match is found; 33410930Sbrandon.potter@amd.com // otherwise, open an input file and seek to location. 33510932Sbrandon.potter@amd.com FDEntry *fde_stdin = getFDEntry(STDIN_FILENO); 33610930Sbrandon.potter@amd.com if ((it = imap.find(fde_stdin->filename)) != imap.end()) { 33710930Sbrandon.potter@amd.com fde_stdin->fd = it->second; 33810930Sbrandon.potter@amd.com } else { 33910930Sbrandon.potter@amd.com fde_stdin->fd = openInputFile(fde_stdin->filename); 34010930Sbrandon.potter@amd.com seek(fde_stdin); 3415282Srstrong@cs.ucsd.edu } 3425282Srstrong@cs.ucsd.edu 34310930Sbrandon.potter@amd.com // Search through the output/error options and set fd if match is found; 34410930Sbrandon.potter@amd.com // otherwise, open an output file and seek to location. 34510932Sbrandon.potter@amd.com FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO); 34610930Sbrandon.potter@amd.com if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) { 34710930Sbrandon.potter@amd.com fde_stdout->fd = it->second; 34810930Sbrandon.potter@amd.com } else { 34910930Sbrandon.potter@amd.com fde_stdout->fd = openOutputFile(fde_stdout->filename); 35010930Sbrandon.potter@amd.com seek(fde_stdout); 3515282Srstrong@cs.ucsd.edu } 3525282Srstrong@cs.ucsd.edu 35310932Sbrandon.potter@amd.com FDEntry *fde_stderr = getFDEntry(STDERR_FILENO); 35410930Sbrandon.potter@amd.com if (fde_stdout->filename == fde_stderr->filename) { 35510930Sbrandon.potter@amd.com // Reuse the same file descriptor if these match. 35610930Sbrandon.potter@amd.com fde_stderr->fd = fde_stdout->fd; 35710930Sbrandon.potter@amd.com } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) { 35810930Sbrandon.potter@amd.com fde_stderr->fd = it->second; 35910930Sbrandon.potter@amd.com } else { 36010930Sbrandon.potter@amd.com fde_stderr->fd = openOutputFile(fde_stderr->filename); 36110930Sbrandon.potter@amd.com seek(fde_stderr); 3625514SMichael.Adler@intel.com } 3635282Srstrong@cs.ucsd.edu 36410930Sbrandon.potter@amd.com for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) { 36510932Sbrandon.potter@amd.com FDEntry *fde = getFDEntry(tgt_fd); 36610930Sbrandon.potter@amd.com if (fde->fd == -1) 36710930Sbrandon.potter@amd.com continue; 3685282Srstrong@cs.ucsd.edu 36910930Sbrandon.potter@amd.com if (fde->isPipe) { 37010930Sbrandon.potter@amd.com if (fde->filename == "PIPE-WRITE") 37110930Sbrandon.potter@amd.com continue; 37210930Sbrandon.potter@amd.com assert(fde->filename == "PIPE-READ"); 3735282Srstrong@cs.ucsd.edu 37410930Sbrandon.potter@amd.com int fds[2]; 37510930Sbrandon.potter@amd.com if (pipe(fds) < 0) 37610930Sbrandon.potter@amd.com fatal("Unable to create new pipe"); 3775282Srstrong@cs.ucsd.edu 37810930Sbrandon.potter@amd.com fde->fd = fds[0]; 3795282Srstrong@cs.ucsd.edu 38010932Sbrandon.potter@amd.com FDEntry *fde_write = getFDEntry(fde->readPipeSource); 38111378Sbrandon.potter@amd.com assert(fde_write->filename == "PIPE-WRITE"); 38210930Sbrandon.potter@amd.com fde_write->fd = fds[1]; 38310930Sbrandon.potter@amd.com } else { 38410930Sbrandon.potter@amd.com fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode); 38510930Sbrandon.potter@amd.com seek(fde); 3865282Srstrong@cs.ucsd.edu } 3875282Srstrong@cs.ucsd.edu } 3885282Srstrong@cs.ucsd.edu} 3897487Ssteve.reinhardt@amd.com 3905282Srstrong@cs.ucsd.eduvoid 39110932Sbrandon.potter@amd.comProcess::findFileOffsets() 3927487Ssteve.reinhardt@amd.com{ 39310930Sbrandon.potter@amd.com for (auto& fde : *fd_array) { 39410930Sbrandon.potter@amd.com if (fde.fd != -1) 39510930Sbrandon.potter@amd.com fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR); 3965282Srstrong@cs.ucsd.edu } 3975282Srstrong@cs.ucsd.edu} 3985282Srstrong@cs.ucsd.edu 3995282Srstrong@cs.ucsd.eduvoid 4007487Ssteve.reinhardt@amd.comProcess::setReadPipeSource(int read_pipe_fd, int source_fd) 4017487Ssteve.reinhardt@amd.com{ 40210932Sbrandon.potter@amd.com FDEntry *fde = getFDEntry(read_pipe_fd); 40310930Sbrandon.potter@amd.com assert(source_fd >= -1); 40410930Sbrandon.potter@amd.com fde->readPipeSource = source_fd; 4055282Srstrong@cs.ucsd.edu} 4065282Srstrong@cs.ucsd.edu 4073311Ssaidi@eecs.umich.eduvoid 40810905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const 4093311Ssaidi@eecs.umich.edu{ 4103311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(brk_point); 4113311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_base); 4123311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_size); 4133311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_min); 4143311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(next_thread_stack_base); 4153311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(mmap_end); 4163311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(nxm_start); 4173311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(nxm_end); 41810905Sandreas.sandberg@arm.com pTable->serialize(cp); 41910930Sbrandon.potter@amd.com for (int x = 0; x < fd_array->size(); x++) { 42010930Sbrandon.potter@amd.com (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x)); 4215282Srstrong@cs.ucsd.edu } 4226820SLisa.Hsu@amd.com SERIALIZE_SCALAR(M5_pid); 4233311Ssaidi@eecs.umich.edu 4243311Ssaidi@eecs.umich.edu} 4253311Ssaidi@eecs.umich.edu 4263311Ssaidi@eecs.umich.eduvoid 42710905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp) 4283311Ssaidi@eecs.umich.edu{ 4293311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(brk_point); 4303311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_base); 4313311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_size); 4323311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_min); 4333311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(next_thread_stack_base); 4343311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(mmap_end); 4353311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(nxm_start); 4363311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(nxm_end); 43710905Sandreas.sandberg@arm.com pTable->unserialize(cp); 43810930Sbrandon.potter@amd.com for (int x = 0; x < fd_array->size(); x++) { 43910932Sbrandon.potter@amd.com FDEntry *fde = getFDEntry(x); 44010930Sbrandon.potter@amd.com fde->unserializeSection(cp, csprintf("FDEntry%d", x)); 4417487Ssteve.reinhardt@amd.com } 44210932Sbrandon.potter@amd.com fixFileOffsets(); 4436820SLisa.Hsu@amd.com UNSERIALIZE_OPT_SCALAR(M5_pid); 4446820SLisa.Hsu@amd.com // The above returns a bool so that you could do something if you don't 4456820SLisa.Hsu@amd.com // find the param in the checkpoint if you wanted to, like set a default 44610930Sbrandon.potter@amd.com // but in this case we'll just stick with the instantiated value if not 44710929Sbrandon.potter@amd.com // found. 4483311Ssaidi@eecs.umich.edu} 4492SN/A 4502SN/A 4519110Ssteve.reinhardt@amd.combool 45210558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable) 4539110Ssteve.reinhardt@amd.com{ 45410558Salexandru.dutu@amd.com pTable->map(vaddr, paddr, size, 45511294Sandreas.hansson@arm.com cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable); 4569110Ssteve.reinhardt@amd.com return true; 4579110Ssteve.reinhardt@amd.com} 4589110Ssteve.reinhardt@amd.com 4599110Ssteve.reinhardt@amd.com 4602SN/A//////////////////////////////////////////////////////////////////////// 4612SN/A// 4622SN/A// LiveProcess member definitions 4632SN/A// 4642SN/A//////////////////////////////////////////////////////////////////////// 4652SN/A 46612SN/A 46710499Ssteve.reinhardt@amd.comLiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile) 4685154Sgblack@eecs.umich.edu : Process(params), objFile(_objFile), 46910496Ssteve.reinhardt@amd.com argv(params->cmd), envp(params->env), cwd(params->cwd), 47011140Sjthestness@gmail.com executable(params->executable), 47110499Ssteve.reinhardt@amd.com __uid(params->uid), __euid(params->euid), 47210499Ssteve.reinhardt@amd.com __gid(params->gid), __egid(params->egid), 47310499Ssteve.reinhardt@amd.com __pid(params->pid), __ppid(params->ppid), 47410496Ssteve.reinhardt@amd.com drivers(params->drivers) 4752SN/A{ 4763114Sgblack@eecs.umich.edu 4771158SN/A // load up symbols, if any... these may be used for debugging or 4781158SN/A // profiling. 4791158SN/A if (!debugSymbolTable) { 4801158SN/A debugSymbolTable = new SymbolTable(); 4811158SN/A if (!objFile->loadGlobalSymbols(debugSymbolTable) || 4829641Sguodeyuan@tsinghua.org.cn !objFile->loadLocalSymbols(debugSymbolTable) || 4839641Sguodeyuan@tsinghua.org.cn !objFile->loadWeakSymbols(debugSymbolTable)) { 4841158SN/A // didn't load any symbols 4851158SN/A delete debugSymbolTable; 4861158SN/A debugSymbolTable = NULL; 4871158SN/A } 4881158SN/A } 4892378SN/A} 4901158SN/A 4912378SN/Avoid 4922680Sktlim@umich.eduLiveProcess::syscall(int64_t callnum, ThreadContext *tc) 4932093SN/A{ 4942093SN/A num_syscalls++; 4952093SN/A 4962093SN/A SyscallDesc *desc = getDesc(callnum); 4972093SN/A if (desc == NULL) 4982093SN/A fatal("Syscall %d out of range", callnum); 4992093SN/A 5002680Sktlim@umich.edu desc->doSyscall(callnum, this, tc); 5012093SN/A} 5022SN/A 5036701Sgblack@eecs.umich.eduIntReg 5046701Sgblack@eecs.umich.eduLiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width) 5056701Sgblack@eecs.umich.edu{ 5066701Sgblack@eecs.umich.edu return getSyscallArg(tc, i); 5076701Sgblack@eecs.umich.edu} 5086701Sgblack@eecs.umich.edu 50910496Ssteve.reinhardt@amd.com 51010496Ssteve.reinhardt@amd.comEmulatedDriver * 51110496Ssteve.reinhardt@amd.comLiveProcess::findDriver(std::string filename) 51210496Ssteve.reinhardt@amd.com{ 51310496Ssteve.reinhardt@amd.com for (EmulatedDriver *d : drivers) { 51410496Ssteve.reinhardt@amd.com if (d->match(filename)) 51510496Ssteve.reinhardt@amd.com return d; 51610496Ssteve.reinhardt@amd.com } 51710496Ssteve.reinhardt@amd.com 51810496Ssteve.reinhardt@amd.com return NULL; 51910496Ssteve.reinhardt@amd.com} 52010496Ssteve.reinhardt@amd.com 52111389Sbrandon.potter@amd.comvoid 52211389Sbrandon.potter@amd.comLiveProcess::updateBias() 52311389Sbrandon.potter@amd.com{ 52411389Sbrandon.potter@amd.com ObjectFile *interp = objFile->getInterpreter(); 52511389Sbrandon.potter@amd.com 52611389Sbrandon.potter@amd.com if (!interp || !interp->relocatable()) 52711389Sbrandon.potter@amd.com return; 52811389Sbrandon.potter@amd.com 52911389Sbrandon.potter@amd.com // Determine how large the interpreters footprint will be in the process 53011389Sbrandon.potter@amd.com // address space. 53111389Sbrandon.potter@amd.com Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes); 53211389Sbrandon.potter@amd.com 53311389Sbrandon.potter@amd.com // We are allocating the memory area; set the bias to the lowest address 53411389Sbrandon.potter@amd.com // in the allocated memory region. 53511389Sbrandon.potter@amd.com Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end; 53611389Sbrandon.potter@amd.com 53711389Sbrandon.potter@amd.com // Adjust the process mmap area to give the interpreter room; the real 53811389Sbrandon.potter@amd.com // execve system call would just invoke the kernel's internal mmap 53911389Sbrandon.potter@amd.com // functions to make these adjustments. 54011389Sbrandon.potter@amd.com mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize; 54111389Sbrandon.potter@amd.com 54211389Sbrandon.potter@amd.com interp->updateBias(ld_bias); 54311389Sbrandon.potter@amd.com} 54411389Sbrandon.potter@amd.com 54511389Sbrandon.potter@amd.com 54611389Sbrandon.potter@amd.comAddr 54711389Sbrandon.potter@amd.comLiveProcess::getBias() 54811389Sbrandon.potter@amd.com{ 54911389Sbrandon.potter@amd.com ObjectFile *interp = objFile->getInterpreter(); 55011389Sbrandon.potter@amd.com 55111389Sbrandon.potter@amd.com return interp ? interp->bias() : objFile->bias(); 55211389Sbrandon.potter@amd.com} 55311389Sbrandon.potter@amd.com 55411389Sbrandon.potter@amd.com 55511389Sbrandon.potter@amd.comAddr 55611389Sbrandon.potter@amd.comLiveProcess::getStartPC() 55711389Sbrandon.potter@amd.com{ 55811389Sbrandon.potter@amd.com ObjectFile *interp = objFile->getInterpreter(); 55911389Sbrandon.potter@amd.com 56011389Sbrandon.potter@amd.com return interp ? interp->entryPoint() : objFile->entryPoint(); 56111389Sbrandon.potter@amd.com} 56211389Sbrandon.potter@amd.com 56310496Ssteve.reinhardt@amd.com 5642715Sstever@eecs.umich.eduLiveProcess * 5655154Sgblack@eecs.umich.eduLiveProcess::create(LiveProcessParams * params) 5662715Sstever@eecs.umich.edu{ 5672715Sstever@eecs.umich.edu LiveProcess *process = NULL; 5682715Sstever@eecs.umich.edu 56911140Sjthestness@gmail.com // If not specified, set the executable parameter equal to the 57011140Sjthestness@gmail.com // simulated system's zeroth command line parameter 57111140Sjthestness@gmail.com if (params->executable == "") { 57211140Sjthestness@gmail.com params->executable = params->cmd[0]; 57311140Sjthestness@gmail.com } 57411140Sjthestness@gmail.com 57511140Sjthestness@gmail.com ObjectFile *objFile = createObjectFile(params->executable); 5762715Sstever@eecs.umich.edu if (objFile == NULL) { 57711140Sjthestness@gmail.com fatal("Can't load object file %s", params->executable); 5782715Sstever@eecs.umich.edu } 5792715Sstever@eecs.umich.edu 5805089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA 5815753Ssteve.reinhardt@amd.com if (objFile->getArch() != ObjectFile::Alpha) 5825753Ssteve.reinhardt@amd.com fatal("Object file architecture does not match compiled ISA (Alpha)."); 5835753Ssteve.reinhardt@amd.com 5842715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 5852715Sstever@eecs.umich.edu case ObjectFile::Tru64: 5865154Sgblack@eecs.umich.edu process = new AlphaTru64Process(params, objFile); 5872715Sstever@eecs.umich.edu break; 5882715Sstever@eecs.umich.edu 5895753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 5905753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 5915753Ssteve.reinhardt@amd.com // fall through 5922715Sstever@eecs.umich.edu case ObjectFile::Linux: 5935154Sgblack@eecs.umich.edu process = new AlphaLinuxProcess(params, objFile); 5942715Sstever@eecs.umich.edu break; 5952715Sstever@eecs.umich.edu 5962715Sstever@eecs.umich.edu default: 5972715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 5982715Sstever@eecs.umich.edu } 5992715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 6005753Ssteve.reinhardt@amd.com if (objFile->getArch() != ObjectFile::SPARC64 && 6015753Ssteve.reinhardt@amd.com objFile->getArch() != ObjectFile::SPARC32) 6022715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (SPARC)."); 6032715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6045753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 6055753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 6065753Ssteve.reinhardt@amd.com // fall through 6072715Sstever@eecs.umich.edu case ObjectFile::Linux: 6084111Sgblack@eecs.umich.edu if (objFile->getArch() == ObjectFile::SPARC64) { 6095154Sgblack@eecs.umich.edu process = new Sparc64LinuxProcess(params, objFile); 6104111Sgblack@eecs.umich.edu } else { 6115154Sgblack@eecs.umich.edu process = new Sparc32LinuxProcess(params, objFile); 6124111Sgblack@eecs.umich.edu } 6132715Sstever@eecs.umich.edu break; 6142715Sstever@eecs.umich.edu 6152715Sstever@eecs.umich.edu 6162715Sstever@eecs.umich.edu case ObjectFile::Solaris: 6175154Sgblack@eecs.umich.edu process = new SparcSolarisProcess(params, objFile); 6182715Sstever@eecs.umich.edu break; 6195753Ssteve.reinhardt@amd.com 6202715Sstever@eecs.umich.edu default: 6212715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6222715Sstever@eecs.umich.edu } 6234157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 6245874Sgblack@eecs.umich.edu if (objFile->getArch() != ObjectFile::X86_64 && 6255874Sgblack@eecs.umich.edu objFile->getArch() != ObjectFile::I386) 6264166Sgblack@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (x86)."); 6274157Sgblack@eecs.umich.edu switch (objFile->getOpSys()) { 6285753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 6295753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 6305753Ssteve.reinhardt@amd.com // fall through 6314166Sgblack@eecs.umich.edu case ObjectFile::Linux: 6325874Sgblack@eecs.umich.edu if (objFile->getArch() == ObjectFile::X86_64) { 6335955Sgblack@eecs.umich.edu process = new X86_64LinuxProcess(params, objFile); 6345874Sgblack@eecs.umich.edu } else { 6355955Sgblack@eecs.umich.edu process = new I386LinuxProcess(params, objFile); 6365874Sgblack@eecs.umich.edu } 6374166Sgblack@eecs.umich.edu break; 6385753Ssteve.reinhardt@amd.com 6394157Sgblack@eecs.umich.edu default: 6404157Sgblack@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6414157Sgblack@eecs.umich.edu } 6422715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 6432715Sstever@eecs.umich.edu if (objFile->getArch() != ObjectFile::Mips) 6442715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (MIPS)."); 6452715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6465753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 6475753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 6485753Ssteve.reinhardt@amd.com // fall through 6492715Sstever@eecs.umich.edu case ObjectFile::Linux: 6505154Sgblack@eecs.umich.edu process = new MipsLinuxProcess(params, objFile); 6512715Sstever@eecs.umich.edu break; 6522715Sstever@eecs.umich.edu 6532715Sstever@eecs.umich.edu default: 6542715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6552715Sstever@eecs.umich.edu } 6565335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA 65710037SARM gem5 Developers ObjectFile::Arch arch = objFile->getArch(); 65810037SARM gem5 Developers if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb && 65910037SARM gem5 Developers arch != ObjectFile::Arm64) 6605335Shines@cs.fsu.edu fatal("Object file architecture does not match compiled ISA (ARM)."); 6615335Shines@cs.fsu.edu switch (objFile->getOpSys()) { 6625753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 6635753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 6645753Ssteve.reinhardt@amd.com // fall through 6655335Shines@cs.fsu.edu case ObjectFile::Linux: 66610037SARM gem5 Developers if (arch == ObjectFile::Arm64) { 66710037SARM gem5 Developers process = new ArmLinuxProcess64(params, objFile, 66810037SARM gem5 Developers objFile->getArch()); 66910037SARM gem5 Developers } else { 67010037SARM gem5 Developers process = new ArmLinuxProcess32(params, objFile, 67110037SARM gem5 Developers objFile->getArch()); 67210037SARM gem5 Developers } 6735335Shines@cs.fsu.edu break; 67410810Sbr@bsdpad.com case ObjectFile::FreeBSD: 67510810Sbr@bsdpad.com if (arch == ObjectFile::Arm64) { 67610810Sbr@bsdpad.com process = new ArmFreebsdProcess64(params, objFile, 67710810Sbr@bsdpad.com objFile->getArch()); 67810810Sbr@bsdpad.com } else { 67910810Sbr@bsdpad.com process = new ArmFreebsdProcess32(params, objFile, 68010810Sbr@bsdpad.com objFile->getArch()); 68110810Sbr@bsdpad.com } 68210810Sbr@bsdpad.com break; 6836392Ssaidi@eecs.umich.edu case ObjectFile::LinuxArmOABI: 6846392Ssaidi@eecs.umich.edu fatal("M5 does not support ARM OABI binaries. Please recompile with an" 6856392Ssaidi@eecs.umich.edu " EABI compiler."); 6865335Shines@cs.fsu.edu default: 6875335Shines@cs.fsu.edu fatal("Unknown/unsupported operating system."); 6885335Shines@cs.fsu.edu } 6896691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA 6906691Stjones1@inf.ed.ac.uk if (objFile->getArch() != ObjectFile::Power) 6916691Stjones1@inf.ed.ac.uk fatal("Object file architecture does not match compiled ISA (Power)."); 6926691Stjones1@inf.ed.ac.uk switch (objFile->getOpSys()) { 6936691Stjones1@inf.ed.ac.uk case ObjectFile::UnknownOpSys: 6946691Stjones1@inf.ed.ac.uk warn("Unknown operating system; assuming Linux."); 6956691Stjones1@inf.ed.ac.uk // fall through 6966691Stjones1@inf.ed.ac.uk case ObjectFile::Linux: 6976691Stjones1@inf.ed.ac.uk process = new PowerLinuxProcess(params, objFile); 6986691Stjones1@inf.ed.ac.uk break; 6996691Stjones1@inf.ed.ac.uk 7006691Stjones1@inf.ed.ac.uk default: 7016691Stjones1@inf.ed.ac.uk fatal("Unknown/unsupported operating system."); 7026691Stjones1@inf.ed.ac.uk } 7032715Sstever@eecs.umich.edu#else 7042715Sstever@eecs.umich.edu#error "THE_ISA not set" 7052715Sstever@eecs.umich.edu#endif 7062715Sstever@eecs.umich.edu 7072715Sstever@eecs.umich.edu if (process == NULL) 7082715Sstever@eecs.umich.edu fatal("Unknown error creating process object."); 7092715Sstever@eecs.umich.edu return process; 7102715Sstever@eecs.umich.edu} 7112715Sstever@eecs.umich.edu 7124762Snate@binkert.orgLiveProcess * 7134762Snate@binkert.orgLiveProcessParams::create() 7142715Sstever@eecs.umich.edu{ 7155154Sgblack@eecs.umich.edu return LiveProcess::create(this); 7162715Sstever@eecs.umich.edu} 717