process.cc revision 12441
12SN/A/* 211856Sbrandon.potter@amd.com * Copyright (c) 2014-2016 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 4411856Sbrandon.potter@amd.com * Brandon Potter 452SN/A */ 462SN/A 4711793Sbrandon.potter@amd.com#include "sim/process.hh" 4811793Sbrandon.potter@amd.com 498229Snate@binkert.org#include <fcntl.h> 502SN/A#include <unistd.h> 516712Snate@binkert.org 5211800Sbrandon.potter@amd.com#include <array> 5311911SBrandon.Potter@amd.com#include <csignal> 5410930Sbrandon.potter@amd.com#include <map> 552SN/A#include <string> 5611800Sbrandon.potter@amd.com#include <vector> 572SN/A 5811793Sbrandon.potter@amd.com#include "base/intmath.hh" 5956SN/A#include "base/loader/object_file.hh" 601158SN/A#include "base/loader/symtab.hh" 61146SN/A#include "base/statistics.hh" 626658Snate@binkert.org#include "config/the_isa.hh" 632680Sktlim@umich.edu#include "cpu/thread_context.hh" 642378SN/A#include "mem/page_table.hh" 658706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh" 665154Sgblack@eecs.umich.edu#include "params/Process.hh" 6711800Sbrandon.potter@amd.com#include "sim/emul_driver.hh" 6811856Sbrandon.potter@amd.com#include "sim/fd_array.hh" 6911856Sbrandon.potter@amd.com#include "sim/fd_entry.hh" 7011794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh" 712378SN/A#include "sim/system.hh" 722SN/A 732715Sstever@eecs.umich.edu#if THE_ISA == ALPHA_ISA 742715Sstever@eecs.umich.edu#include "arch/alpha/linux/process.hh" 7511911SBrandon.Potter@amd.com 762715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 772715Sstever@eecs.umich.edu#include "arch/sparc/linux/process.hh" 782715Sstever@eecs.umich.edu#include "arch/sparc/solaris/process.hh" 7911911SBrandon.Potter@amd.com 802715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 812715Sstever@eecs.umich.edu#include "arch/mips/linux/process.hh" 8211911SBrandon.Potter@amd.com 835335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA 8411911SBrandon.Potter@amd.com#include "arch/arm/freebsd/process.hh" 855335Shines@cs.fsu.edu#include "arch/arm/linux/process.hh" 8611911SBrandon.Potter@amd.com 874157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 884166Sgblack@eecs.umich.edu#include "arch/x86/linux/process.hh" 8911911SBrandon.Potter@amd.com 906691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA 916691Stjones1@inf.ed.ac.uk#include "arch/power/linux/process.hh" 9211911SBrandon.Potter@amd.com 9311723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA 9411723Sar4jc@virginia.edu#include "arch/riscv/linux/process.hh" 9511911SBrandon.Potter@amd.com 962715Sstever@eecs.umich.edu#else 972715Sstever@eecs.umich.edu#error "THE_ISA not set" 982715Sstever@eecs.umich.edu#endif 992715Sstever@eecs.umich.edu 1002715Sstever@eecs.umich.edu 1012SN/Ausing namespace std; 1022107SN/Ausing namespace TheISA; 1032SN/A 10412431Sgabeblack@google.comProcess::Process(ProcessParams *params, PageTableBase *pTable, 10512431Sgabeblack@google.com ObjectFile *obj_file) 1067532Ssteve.reinhardt@amd.com : SimObject(params), system(params->system), 10710299Salexandru.dutu@amd.com useArchPT(params->useArchPT), 10810554Salexandru.dutu@amd.com kvmInSE(params->kvmInSE), 10912431Sgabeblack@google.com pTable(pTable), 1108852Sandreas.hansson@arm.com initVirtMem(system->getSystemPort(), this, 11110929Sbrandon.potter@amd.com SETranslatingPortProxy::Always), 11211851Sbrandon.potter@amd.com objFile(obj_file), 11311851Sbrandon.potter@amd.com argv(params->cmd), envp(params->env), cwd(params->cwd), 11411851Sbrandon.potter@amd.com executable(params->executable), 11511801Sbrandon.potter@amd.com _uid(params->uid), _euid(params->euid), 11611801Sbrandon.potter@amd.com _gid(params->gid), _egid(params->egid), 11711851Sbrandon.potter@amd.com _pid(params->pid), _ppid(params->ppid), 11811885Sbrandon.potter@amd.com _pgid(params->pgid), drivers(params->drivers), 11911886Sbrandon.potter@amd.com fds(make_shared<FDArray>(params->input, params->output, params->errout)), 12011886Sbrandon.potter@amd.com childClearTID(0) 1212SN/A{ 12211885Sbrandon.potter@amd.com if (_pid >= System::maxPID) 12311885Sbrandon.potter@amd.com fatal("_pid is too large: %d", _pid); 12411885Sbrandon.potter@amd.com 12511885Sbrandon.potter@amd.com auto ret_pair = system->PIDs.emplace(_pid); 12611885Sbrandon.potter@amd.com if (!ret_pair.second) 12711885Sbrandon.potter@amd.com fatal("_pid %d is already used", _pid); 12811885Sbrandon.potter@amd.com 12911886Sbrandon.potter@amd.com /** 13011886Sbrandon.potter@amd.com * Linux bundles together processes into this concept called a thread 13111886Sbrandon.potter@amd.com * group. The thread group is responsible for recording which processes 13211886Sbrandon.potter@amd.com * behave as threads within a process context. The thread group leader 13311886Sbrandon.potter@amd.com * is the process who's tgid is equal to its pid. Other processes which 13411886Sbrandon.potter@amd.com * belong to the thread group, but do not lead the thread group, are 13511886Sbrandon.potter@amd.com * treated as child threads. These threads are created by the clone system 13611886Sbrandon.potter@amd.com * call with options specified to create threads (differing from the 13711886Sbrandon.potter@amd.com * options used to implement a fork). By default, set up the tgid/pid 13811886Sbrandon.potter@amd.com * with a new, equivalent value. If CLONE_THREAD is specified, patch 13911886Sbrandon.potter@amd.com * the tgid value with the old process' value. 14011886Sbrandon.potter@amd.com */ 14111886Sbrandon.potter@amd.com _tgid = params->pid; 14211886Sbrandon.potter@amd.com 14311886Sbrandon.potter@amd.com exitGroup = new bool(); 14411886Sbrandon.potter@amd.com sigchld = new bool(); 14511886Sbrandon.potter@amd.com 14611851Sbrandon.potter@amd.com if (!debugSymbolTable) { 14711851Sbrandon.potter@amd.com debugSymbolTable = new SymbolTable(); 14811851Sbrandon.potter@amd.com if (!objFile->loadGlobalSymbols(debugSymbolTable) || 14911851Sbrandon.potter@amd.com !objFile->loadLocalSymbols(debugSymbolTable) || 15011851Sbrandon.potter@amd.com !objFile->loadWeakSymbols(debugSymbolTable)) { 15111851Sbrandon.potter@amd.com delete debugSymbolTable; 15211919SBrandon.Potter@amd.com debugSymbolTable = nullptr; 15311851Sbrandon.potter@amd.com } 15411851Sbrandon.potter@amd.com } 1552SN/A} 1562SN/A 1572SN/Avoid 15811886Sbrandon.potter@amd.comProcess::clone(ThreadContext *otc, ThreadContext *ntc, 15911886Sbrandon.potter@amd.com Process *np, TheISA::IntReg flags) 16011886Sbrandon.potter@amd.com{ 16111920SBrandon.Potter@amd.com#ifndef CLONE_VM 16211920SBrandon.Potter@amd.com#define CLONE_VM 0 16311920SBrandon.Potter@amd.com#endif 16411920SBrandon.Potter@amd.com#ifndef CLONE_FILES 16511920SBrandon.Potter@amd.com#define CLONE_FILES 0 16611920SBrandon.Potter@amd.com#endif 16711920SBrandon.Potter@amd.com#ifndef CLONE_THREAD 16811920SBrandon.Potter@amd.com#define CLONE_THREAD 0 16911920SBrandon.Potter@amd.com#endif 17011886Sbrandon.potter@amd.com if (CLONE_VM & flags) { 17111886Sbrandon.potter@amd.com /** 17211886Sbrandon.potter@amd.com * Share the process memory address space between the new process 17311886Sbrandon.potter@amd.com * and the old process. Changes in one will be visible in the other 17411886Sbrandon.potter@amd.com * due to the pointer use. 17511886Sbrandon.potter@amd.com */ 17611886Sbrandon.potter@amd.com delete np->pTable; 17711886Sbrandon.potter@amd.com np->pTable = pTable; 17811886Sbrandon.potter@amd.com ntc->getMemProxy().setPageTable(np->pTable); 17911886Sbrandon.potter@amd.com 18011886Sbrandon.potter@amd.com np->memState = memState; 18111886Sbrandon.potter@amd.com } else { 18211886Sbrandon.potter@amd.com /** 18311886Sbrandon.potter@amd.com * Duplicate the process memory address space. The state needs to be 18411886Sbrandon.potter@amd.com * copied over (rather than using pointers to share everything). 18511886Sbrandon.potter@amd.com */ 18611886Sbrandon.potter@amd.com typedef std::vector<pair<Addr,Addr>> MapVec; 18711886Sbrandon.potter@amd.com MapVec mappings; 18811886Sbrandon.potter@amd.com pTable->getMappings(&mappings); 18911886Sbrandon.potter@amd.com 19011886Sbrandon.potter@amd.com for (auto map : mappings) { 19111886Sbrandon.potter@amd.com Addr paddr, vaddr = map.first; 19211886Sbrandon.potter@amd.com bool alloc_page = !(np->pTable->translate(vaddr, paddr)); 19311886Sbrandon.potter@amd.com np->replicatePage(vaddr, paddr, otc, ntc, alloc_page); 19411886Sbrandon.potter@amd.com } 19511886Sbrandon.potter@amd.com 19611886Sbrandon.potter@amd.com *np->memState = *memState; 19711886Sbrandon.potter@amd.com } 19811886Sbrandon.potter@amd.com 19911886Sbrandon.potter@amd.com if (CLONE_FILES & flags) { 20011886Sbrandon.potter@amd.com /** 20111886Sbrandon.potter@amd.com * The parent and child file descriptors are shared because the 20211886Sbrandon.potter@amd.com * two FDArray pointers are pointing to the same FDArray. Opening 20311886Sbrandon.potter@amd.com * and closing file descriptors will be visible to both processes. 20411886Sbrandon.potter@amd.com */ 20511886Sbrandon.potter@amd.com np->fds = fds; 20611886Sbrandon.potter@amd.com } else { 20711886Sbrandon.potter@amd.com /** 20811886Sbrandon.potter@amd.com * Copy the file descriptors from the old process into the new 20911886Sbrandon.potter@amd.com * child process. The file descriptors entry can be opened and 21011886Sbrandon.potter@amd.com * closed independently of the other process being considered. The 21111886Sbrandon.potter@amd.com * host file descriptors are also dup'd so that the flags for the 21211886Sbrandon.potter@amd.com * host file descriptor is independent of the other process. 21311886Sbrandon.potter@amd.com */ 21411886Sbrandon.potter@amd.com for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) { 21511886Sbrandon.potter@amd.com std::shared_ptr<FDArray> nfds = np->fds; 21611886Sbrandon.potter@amd.com std::shared_ptr<FDEntry> this_fde = (*fds)[tgt_fd]; 21711886Sbrandon.potter@amd.com if (!this_fde) { 21811886Sbrandon.potter@amd.com nfds->setFDEntry(tgt_fd, nullptr); 21911886Sbrandon.potter@amd.com continue; 22011886Sbrandon.potter@amd.com } 22111886Sbrandon.potter@amd.com nfds->setFDEntry(tgt_fd, this_fde->clone()); 22211886Sbrandon.potter@amd.com 22311886Sbrandon.potter@amd.com auto this_hbfd = std::dynamic_pointer_cast<HBFDEntry>(this_fde); 22411886Sbrandon.potter@amd.com if (!this_hbfd) 22511886Sbrandon.potter@amd.com continue; 22611886Sbrandon.potter@amd.com 22711886Sbrandon.potter@amd.com int this_sim_fd = this_hbfd->getSimFD(); 22811886Sbrandon.potter@amd.com if (this_sim_fd <= 2) 22911886Sbrandon.potter@amd.com continue; 23011886Sbrandon.potter@amd.com 23111886Sbrandon.potter@amd.com int np_sim_fd = dup(this_sim_fd); 23211886Sbrandon.potter@amd.com assert(np_sim_fd != -1); 23311886Sbrandon.potter@amd.com 23411886Sbrandon.potter@amd.com auto nhbfd = std::dynamic_pointer_cast<HBFDEntry>((*nfds)[tgt_fd]); 23511886Sbrandon.potter@amd.com nhbfd->setSimFD(np_sim_fd); 23611886Sbrandon.potter@amd.com } 23711886Sbrandon.potter@amd.com } 23811886Sbrandon.potter@amd.com 23911886Sbrandon.potter@amd.com if (CLONE_THREAD & flags) { 24011886Sbrandon.potter@amd.com np->_tgid = _tgid; 24111886Sbrandon.potter@amd.com delete np->exitGroup; 24211886Sbrandon.potter@amd.com np->exitGroup = exitGroup; 24311886Sbrandon.potter@amd.com } 24411886Sbrandon.potter@amd.com 24511886Sbrandon.potter@amd.com np->argv.insert(np->argv.end(), argv.begin(), argv.end()); 24611886Sbrandon.potter@amd.com np->envp.insert(np->envp.end(), envp.begin(), envp.end()); 24711886Sbrandon.potter@amd.com} 24811886Sbrandon.potter@amd.com 24911886Sbrandon.potter@amd.comvoid 2502SN/AProcess::regStats() 2512SN/A{ 25211522Sstephan.diestelhorst@arm.com SimObject::regStats(); 25311522Sstephan.diestelhorst@arm.com 254729SN/A using namespace Stats; 2552SN/A 25611886Sbrandon.potter@amd.com numSyscalls 25711886Sbrandon.potter@amd.com .name(name() + ".numSyscalls") 2582SN/A .desc("Number of system calls") 2592SN/A ; 2602SN/A} 2612SN/A 2625713Shsul@eecs.umich.eduThreadContext * 2635713Shsul@eecs.umich.eduProcess::findFreeContext() 2642SN/A{ 26511886Sbrandon.potter@amd.com for (auto &it : system->threadContexts) { 26611886Sbrandon.potter@amd.com if (ThreadContext::Halted == it->status()) 26711886Sbrandon.potter@amd.com return it; 2685512SMichael.Adler@intel.com } 26911919SBrandon.Potter@amd.com return nullptr; 2702SN/A} 2712SN/A 2721395SN/Avoid 27311886Sbrandon.potter@amd.comProcess::revokeThreadContext(int context_id) 27411886Sbrandon.potter@amd.com{ 27511886Sbrandon.potter@amd.com std::vector<ContextID>::iterator it; 27611886Sbrandon.potter@amd.com for (it = contextIds.begin(); it != contextIds.end(); it++) { 27711886Sbrandon.potter@amd.com if (*it == context_id) { 27811886Sbrandon.potter@amd.com contextIds.erase(it); 27911886Sbrandon.potter@amd.com return; 28011886Sbrandon.potter@amd.com } 28111886Sbrandon.potter@amd.com } 28211886Sbrandon.potter@amd.com warn("Unable to find thread context to revoke"); 28311886Sbrandon.potter@amd.com} 28411886Sbrandon.potter@amd.com 28511886Sbrandon.potter@amd.comvoid 2867532Ssteve.reinhardt@amd.comProcess::initState() 2871395SN/A{ 2885713Shsul@eecs.umich.edu if (contextIds.empty()) 2895713Shsul@eecs.umich.edu fatal("Process %s is not associated with any HW contexts!\n", name()); 2902378SN/A 2912680Sktlim@umich.edu // first thread context for this process... initialize & enable 2925713Shsul@eecs.umich.edu ThreadContext *tc = system->getThreadContext(contextIds[0]); 2931395SN/A 2941634SN/A // mark this context as active so it will start ticking. 29510407Smitch.hayenga@arm.com tc->activate(); 29610298Salexandru.dutu@amd.com 29710298Salexandru.dutu@amd.com pTable->initState(tc); 2981395SN/A} 2992SN/A 30010913Sandreas.sandberg@arm.comDrainState 30110913Sandreas.sandberg@arm.comProcess::drain() 30210905Sandreas.sandberg@arm.com{ 30311856Sbrandon.potter@amd.com fds->updateFileOffsets(); 30410913Sandreas.sandberg@arm.com return DrainState::Drained; 30510905Sandreas.sandberg@arm.com} 30610905Sandreas.sandberg@arm.com 3078601Ssteve.reinhardt@amd.comvoid 3088601Ssteve.reinhardt@amd.comProcess::allocateMem(Addr vaddr, int64_t size, bool clobber) 3098601Ssteve.reinhardt@amd.com{ 31010318Sandreas.hansson@arm.com int npages = divCeil(size, (int64_t)PageBytes); 3118601Ssteve.reinhardt@amd.com Addr paddr = system->allocPhysPages(npages); 31211294Sandreas.hansson@arm.com pTable->map(vaddr, paddr, size, 31311294Sandreas.hansson@arm.com clobber ? PageTableBase::Clobber : PageTableBase::Zero); 3148601Ssteve.reinhardt@amd.com} 3158601Ssteve.reinhardt@amd.com 31611886Sbrandon.potter@amd.comvoid 31711886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc, 31811886Sbrandon.potter@amd.com ThreadContext *new_tc, bool allocate_page) 31911886Sbrandon.potter@amd.com{ 32011886Sbrandon.potter@amd.com if (allocate_page) 32111886Sbrandon.potter@amd.com new_paddr = system->allocPhysPages(1); 32211886Sbrandon.potter@amd.com 32311886Sbrandon.potter@amd.com // Read from old physical page. 32411886Sbrandon.potter@amd.com uint8_t *buf_p = new uint8_t[PageBytes]; 32511886Sbrandon.potter@amd.com old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes); 32611886Sbrandon.potter@amd.com 32711886Sbrandon.potter@amd.com // Create new mapping in process address space by clobbering existing 32811886Sbrandon.potter@amd.com // mapping (if any existed) and then write to the new physical page. 32911886Sbrandon.potter@amd.com bool clobber = true; 33011886Sbrandon.potter@amd.com pTable->map(vaddr, new_paddr, PageBytes, clobber); 33111886Sbrandon.potter@amd.com new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes); 33211886Sbrandon.potter@amd.com delete[] buf_p; 33311886Sbrandon.potter@amd.com} 33411886Sbrandon.potter@amd.com 3354434Ssaidi@eecs.umich.edubool 3368539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr) 3374434Ssaidi@eecs.umich.edu{ 33811905SBrandon.Potter@amd.com Addr stack_min = memState->getStackMin(); 33911905SBrandon.Potter@amd.com Addr stack_base = memState->getStackBase(); 34011905SBrandon.Potter@amd.com Addr max_stack_size = memState->getMaxStackSize(); 34111905SBrandon.Potter@amd.com 3428539Sgblack@eecs.umich.edu // Check if this is already on the stack and there's just no page there 3438539Sgblack@eecs.umich.edu // yet. 34411905SBrandon.Potter@amd.com if (vaddr >= stack_min && vaddr < stack_base) { 34510318Sandreas.hansson@arm.com allocateMem(roundDown(vaddr, PageBytes), PageBytes); 3464434Ssaidi@eecs.umich.edu return true; 3474434Ssaidi@eecs.umich.edu } 3484434Ssaidi@eecs.umich.edu 3498539Sgblack@eecs.umich.edu // We've accessed the next page of the stack, so extend it to include 3508539Sgblack@eecs.umich.edu // this address. 35111905SBrandon.Potter@amd.com if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 35211905SBrandon.Potter@amd.com while (vaddr < stack_min) { 35311905SBrandon.Potter@amd.com stack_min -= TheISA::PageBytes; 35411905SBrandon.Potter@amd.com if (stack_base - stack_min > max_stack_size) 3555154Sgblack@eecs.umich.edu fatal("Maximum stack size exceeded\n"); 35611905SBrandon.Potter@amd.com allocateMem(stack_min, TheISA::PageBytes); 3575823Ssaidi@eecs.umich.edu inform("Increasing stack size by one page."); 35811906SBrandon.Potter@amd.com } 35911905SBrandon.Potter@amd.com memState->setStackMin(stack_min); 3604434Ssaidi@eecs.umich.edu return true; 3614434Ssaidi@eecs.umich.edu } 3624434Ssaidi@eecs.umich.edu return false; 3634434Ssaidi@eecs.umich.edu} 3644434Ssaidi@eecs.umich.edu 3655282Srstrong@cs.ucsd.eduvoid 36610905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const 3673311Ssaidi@eecs.umich.edu{ 36812283Saustinharris@utexas.edu memState->serialize(cp); 36910905Sandreas.sandberg@arm.com pTable->serialize(cp); 37011856Sbrandon.potter@amd.com /** 37111856Sbrandon.potter@amd.com * Checkpoints for file descriptors currently do not work. Need to 37211856Sbrandon.potter@amd.com * come back and fix them at a later date. 37311856Sbrandon.potter@amd.com */ 37411856Sbrandon.potter@amd.com 37511856Sbrandon.potter@amd.com warn("Checkpoints for file descriptors currently do not work."); 37611856Sbrandon.potter@amd.com#if 0 37711856Sbrandon.potter@amd.com for (int x = 0; x < fds->getSize(); x++) 37811856Sbrandon.potter@amd.com (*fds)[x].serializeSection(cp, csprintf("FDEntry%d", x)); 37911856Sbrandon.potter@amd.com#endif 3803311Ssaidi@eecs.umich.edu 3813311Ssaidi@eecs.umich.edu} 3823311Ssaidi@eecs.umich.edu 3833311Ssaidi@eecs.umich.eduvoid 38410905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp) 3853311Ssaidi@eecs.umich.edu{ 38612283Saustinharris@utexas.edu memState->unserialize(cp); 38710905Sandreas.sandberg@arm.com pTable->unserialize(cp); 38811856Sbrandon.potter@amd.com /** 38911856Sbrandon.potter@amd.com * Checkpoints for file descriptors currently do not work. Need to 39011856Sbrandon.potter@amd.com * come back and fix them at a later date. 39111856Sbrandon.potter@amd.com */ 39211856Sbrandon.potter@amd.com warn("Checkpoints for file descriptors currently do not work."); 39311856Sbrandon.potter@amd.com#if 0 39411856Sbrandon.potter@amd.com for (int x = 0; x < fds->getSize(); x++) 39511856Sbrandon.potter@amd.com (*fds)[x]->unserializeSection(cp, csprintf("FDEntry%d", x)); 39611856Sbrandon.potter@amd.com fds->restoreFileOffsets(); 39711856Sbrandon.potter@amd.com#endif 3986820SLisa.Hsu@amd.com // The above returns a bool so that you could do something if you don't 3996820SLisa.Hsu@amd.com // find the param in the checkpoint if you wanted to, like set a default 40010930Sbrandon.potter@amd.com // but in this case we'll just stick with the instantiated value if not 40110929Sbrandon.potter@amd.com // found. 4023311Ssaidi@eecs.umich.edu} 4032SN/A 4049110Ssteve.reinhardt@amd.combool 40510558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable) 4069110Ssteve.reinhardt@amd.com{ 40710558Salexandru.dutu@amd.com pTable->map(vaddr, paddr, size, 40811294Sandreas.hansson@arm.com cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable); 4099110Ssteve.reinhardt@amd.com return true; 4109110Ssteve.reinhardt@amd.com} 4119110Ssteve.reinhardt@amd.com 4122378SN/Avoid 41311877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault) 4142093SN/A{ 41511886Sbrandon.potter@amd.com numSyscalls++; 4162093SN/A 4172093SN/A SyscallDesc *desc = getDesc(callnum); 41811919SBrandon.Potter@amd.com if (desc == nullptr) 4192093SN/A fatal("Syscall %d out of range", callnum); 4202093SN/A 42111877Sbrandon.potter@amd.com desc->doSyscall(callnum, this, tc, fault); 4222093SN/A} 4232SN/A 4246701Sgblack@eecs.umich.eduIntReg 42511851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width) 4266701Sgblack@eecs.umich.edu{ 4276701Sgblack@eecs.umich.edu return getSyscallArg(tc, i); 4286701Sgblack@eecs.umich.edu} 4296701Sgblack@eecs.umich.edu 43010496Ssteve.reinhardt@amd.comEmulatedDriver * 43111851Sbrandon.potter@amd.comProcess::findDriver(std::string filename) 43210496Ssteve.reinhardt@amd.com{ 43310496Ssteve.reinhardt@amd.com for (EmulatedDriver *d : drivers) { 43410496Ssteve.reinhardt@amd.com if (d->match(filename)) 43510496Ssteve.reinhardt@amd.com return d; 43610496Ssteve.reinhardt@amd.com } 43710496Ssteve.reinhardt@amd.com 43811919SBrandon.Potter@amd.com return nullptr; 43910496Ssteve.reinhardt@amd.com} 44010496Ssteve.reinhardt@amd.com 44111389Sbrandon.potter@amd.comvoid 44211851Sbrandon.potter@amd.comProcess::updateBias() 44311389Sbrandon.potter@amd.com{ 44411389Sbrandon.potter@amd.com ObjectFile *interp = objFile->getInterpreter(); 44511389Sbrandon.potter@amd.com 44611389Sbrandon.potter@amd.com if (!interp || !interp->relocatable()) 44711389Sbrandon.potter@amd.com return; 44811389Sbrandon.potter@amd.com 44911389Sbrandon.potter@amd.com // Determine how large the interpreters footprint will be in the process 45011389Sbrandon.potter@amd.com // address space. 45111389Sbrandon.potter@amd.com Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes); 45211389Sbrandon.potter@amd.com 45311389Sbrandon.potter@amd.com // We are allocating the memory area; set the bias to the lowest address 45411389Sbrandon.potter@amd.com // in the allocated memory region. 45511905SBrandon.Potter@amd.com Addr mmap_end = memState->getMmapEnd(); 45611905SBrandon.Potter@amd.com Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end; 45711389Sbrandon.potter@amd.com 45811389Sbrandon.potter@amd.com // Adjust the process mmap area to give the interpreter room; the real 45911389Sbrandon.potter@amd.com // execve system call would just invoke the kernel's internal mmap 46011389Sbrandon.potter@amd.com // functions to make these adjustments. 46111905SBrandon.Potter@amd.com mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize; 46211905SBrandon.Potter@amd.com memState->setMmapEnd(mmap_end); 46311389Sbrandon.potter@amd.com 46411389Sbrandon.potter@amd.com interp->updateBias(ld_bias); 46511389Sbrandon.potter@amd.com} 46611389Sbrandon.potter@amd.com 46711392Sbrandon.potter@amd.comObjectFile * 46811851Sbrandon.potter@amd.comProcess::getInterpreter() 46911392Sbrandon.potter@amd.com{ 47011392Sbrandon.potter@amd.com return objFile->getInterpreter(); 47111392Sbrandon.potter@amd.com} 47211392Sbrandon.potter@amd.com 47311389Sbrandon.potter@amd.comAddr 47411851Sbrandon.potter@amd.comProcess::getBias() 47511389Sbrandon.potter@amd.com{ 47611392Sbrandon.potter@amd.com ObjectFile *interp = getInterpreter(); 47711389Sbrandon.potter@amd.com 47811389Sbrandon.potter@amd.com return interp ? interp->bias() : objFile->bias(); 47911389Sbrandon.potter@amd.com} 48011389Sbrandon.potter@amd.com 48111389Sbrandon.potter@amd.comAddr 48211851Sbrandon.potter@amd.comProcess::getStartPC() 48311389Sbrandon.potter@amd.com{ 48411392Sbrandon.potter@amd.com ObjectFile *interp = getInterpreter(); 48511389Sbrandon.potter@amd.com 48611389Sbrandon.potter@amd.com return interp ? interp->entryPoint() : objFile->entryPoint(); 48711389Sbrandon.potter@amd.com} 48811389Sbrandon.potter@amd.com 48911851Sbrandon.potter@amd.comProcess * 49011851Sbrandon.potter@amd.comProcessParams::create() 4912715Sstever@eecs.umich.edu{ 49211919SBrandon.Potter@amd.com Process *process = nullptr; 4932715Sstever@eecs.umich.edu 49411140Sjthestness@gmail.com // If not specified, set the executable parameter equal to the 49511140Sjthestness@gmail.com // simulated system's zeroth command line parameter 49611851Sbrandon.potter@amd.com if (executable == "") { 49711851Sbrandon.potter@amd.com executable = cmd[0]; 49811140Sjthestness@gmail.com } 49911140Sjthestness@gmail.com 50011851Sbrandon.potter@amd.com ObjectFile *obj_file = createObjectFile(executable); 50111919SBrandon.Potter@amd.com if (obj_file == nullptr) { 50211851Sbrandon.potter@amd.com fatal("Can't load object file %s", executable); 5032715Sstever@eecs.umich.edu } 5042715Sstever@eecs.umich.edu 5055089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA 50611851Sbrandon.potter@amd.com if (obj_file->getArch() != ObjectFile::Alpha) 5075753Ssteve.reinhardt@amd.com fatal("Object file architecture does not match compiled ISA (Alpha)."); 5085753Ssteve.reinhardt@amd.com 50911851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 5105753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 5115753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 5125753Ssteve.reinhardt@amd.com // fall through 5132715Sstever@eecs.umich.edu case ObjectFile::Linux: 51411851Sbrandon.potter@amd.com process = new AlphaLinuxProcess(this, obj_file); 5152715Sstever@eecs.umich.edu break; 5162715Sstever@eecs.umich.edu 5172715Sstever@eecs.umich.edu default: 5182715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 5192715Sstever@eecs.umich.edu } 5202715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 52111851Sbrandon.potter@amd.com if (obj_file->getArch() != ObjectFile::SPARC64 && 52211851Sbrandon.potter@amd.com obj_file->getArch() != ObjectFile::SPARC32) 5232715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (SPARC)."); 52411851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 5255753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 5265753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 5275753Ssteve.reinhardt@amd.com // fall through 5282715Sstever@eecs.umich.edu case ObjectFile::Linux: 52911851Sbrandon.potter@amd.com if (obj_file->getArch() == ObjectFile::SPARC64) { 53011851Sbrandon.potter@amd.com process = new Sparc64LinuxProcess(this, obj_file); 5314111Sgblack@eecs.umich.edu } else { 53211851Sbrandon.potter@amd.com process = new Sparc32LinuxProcess(this, obj_file); 5334111Sgblack@eecs.umich.edu } 5342715Sstever@eecs.umich.edu break; 5352715Sstever@eecs.umich.edu 5362715Sstever@eecs.umich.edu case ObjectFile::Solaris: 53711851Sbrandon.potter@amd.com process = new SparcSolarisProcess(this, obj_file); 5382715Sstever@eecs.umich.edu break; 5395753Ssteve.reinhardt@amd.com 5402715Sstever@eecs.umich.edu default: 5412715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 5422715Sstever@eecs.umich.edu } 5434157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 54411851Sbrandon.potter@amd.com if (obj_file->getArch() != ObjectFile::X86_64 && 54511851Sbrandon.potter@amd.com obj_file->getArch() != ObjectFile::I386) 5464166Sgblack@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (x86)."); 54711851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 5485753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 5495753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 5505753Ssteve.reinhardt@amd.com // fall through 5514166Sgblack@eecs.umich.edu case ObjectFile::Linux: 55211851Sbrandon.potter@amd.com if (obj_file->getArch() == ObjectFile::X86_64) { 55311851Sbrandon.potter@amd.com process = new X86_64LinuxProcess(this, obj_file); 5545874Sgblack@eecs.umich.edu } else { 55511851Sbrandon.potter@amd.com process = new I386LinuxProcess(this, obj_file); 5565874Sgblack@eecs.umich.edu } 5574166Sgblack@eecs.umich.edu break; 5585753Ssteve.reinhardt@amd.com 5594157Sgblack@eecs.umich.edu default: 5604157Sgblack@eecs.umich.edu fatal("Unknown/unsupported operating system."); 5614157Sgblack@eecs.umich.edu } 5622715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 56311851Sbrandon.potter@amd.com if (obj_file->getArch() != ObjectFile::Mips) 5642715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (MIPS)."); 56511851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 5665753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 5675753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 5685753Ssteve.reinhardt@amd.com // fall through 5692715Sstever@eecs.umich.edu case ObjectFile::Linux: 57011851Sbrandon.potter@amd.com process = new MipsLinuxProcess(this, obj_file); 5712715Sstever@eecs.umich.edu break; 5722715Sstever@eecs.umich.edu 5732715Sstever@eecs.umich.edu default: 5742715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 5752715Sstever@eecs.umich.edu } 5765335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA 57711851Sbrandon.potter@amd.com ObjectFile::Arch arch = obj_file->getArch(); 57810037SARM gem5 Developers if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb && 57910037SARM gem5 Developers arch != ObjectFile::Arm64) 5805335Shines@cs.fsu.edu fatal("Object file architecture does not match compiled ISA (ARM)."); 58111851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 5825753Ssteve.reinhardt@amd.com case ObjectFile::UnknownOpSys: 5835753Ssteve.reinhardt@amd.com warn("Unknown operating system; assuming Linux."); 5845753Ssteve.reinhardt@amd.com // fall through 5855335Shines@cs.fsu.edu case ObjectFile::Linux: 58610037SARM gem5 Developers if (arch == ObjectFile::Arm64) { 58711851Sbrandon.potter@amd.com process = new ArmLinuxProcess64(this, obj_file, 58811851Sbrandon.potter@amd.com obj_file->getArch()); 58910037SARM gem5 Developers } else { 59011851Sbrandon.potter@amd.com process = new ArmLinuxProcess32(this, obj_file, 59111851Sbrandon.potter@amd.com obj_file->getArch()); 59210037SARM gem5 Developers } 5935335Shines@cs.fsu.edu break; 59410810Sbr@bsdpad.com case ObjectFile::FreeBSD: 59510810Sbr@bsdpad.com if (arch == ObjectFile::Arm64) { 59611851Sbrandon.potter@amd.com process = new ArmFreebsdProcess64(this, obj_file, 59711851Sbrandon.potter@amd.com obj_file->getArch()); 59810810Sbr@bsdpad.com } else { 59911851Sbrandon.potter@amd.com process = new ArmFreebsdProcess32(this, obj_file, 60011851Sbrandon.potter@amd.com obj_file->getArch()); 60110810Sbr@bsdpad.com } 60210810Sbr@bsdpad.com break; 6036392Ssaidi@eecs.umich.edu case ObjectFile::LinuxArmOABI: 6046392Ssaidi@eecs.umich.edu fatal("M5 does not support ARM OABI binaries. Please recompile with an" 6056392Ssaidi@eecs.umich.edu " EABI compiler."); 6065335Shines@cs.fsu.edu default: 6075335Shines@cs.fsu.edu fatal("Unknown/unsupported operating system."); 6085335Shines@cs.fsu.edu } 6096691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA 61011851Sbrandon.potter@amd.com if (obj_file->getArch() != ObjectFile::Power) 6116691Stjones1@inf.ed.ac.uk fatal("Object file architecture does not match compiled ISA (Power)."); 61211851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 6136691Stjones1@inf.ed.ac.uk case ObjectFile::UnknownOpSys: 6146691Stjones1@inf.ed.ac.uk warn("Unknown operating system; assuming Linux."); 6156691Stjones1@inf.ed.ac.uk // fall through 6166691Stjones1@inf.ed.ac.uk case ObjectFile::Linux: 61711851Sbrandon.potter@amd.com process = new PowerLinuxProcess(this, obj_file); 6186691Stjones1@inf.ed.ac.uk break; 6196691Stjones1@inf.ed.ac.uk 6206691Stjones1@inf.ed.ac.uk default: 6216691Stjones1@inf.ed.ac.uk fatal("Unknown/unsupported operating system."); 6226691Stjones1@inf.ed.ac.uk } 62311723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA 62411851Sbrandon.potter@amd.com if (obj_file->getArch() != ObjectFile::Riscv) 62511723Sar4jc@virginia.edu fatal("Object file architecture does not match compiled ISA (RISCV)."); 62611851Sbrandon.potter@amd.com switch (obj_file->getOpSys()) { 62711723Sar4jc@virginia.edu case ObjectFile::UnknownOpSys: 62811723Sar4jc@virginia.edu warn("Unknown operating system; assuming Linux."); 62911723Sar4jc@virginia.edu // fall through 63011723Sar4jc@virginia.edu case ObjectFile::Linux: 63111851Sbrandon.potter@amd.com process = new RiscvLinuxProcess(this, obj_file); 63211723Sar4jc@virginia.edu break; 63311723Sar4jc@virginia.edu default: 63411723Sar4jc@virginia.edu fatal("Unknown/unsupported operating system."); 63511723Sar4jc@virginia.edu } 6362715Sstever@eecs.umich.edu#else 6372715Sstever@eecs.umich.edu#error "THE_ISA not set" 6382715Sstever@eecs.umich.edu#endif 6392715Sstever@eecs.umich.edu 64011919SBrandon.Potter@amd.com if (process == nullptr) 6412715Sstever@eecs.umich.edu fatal("Unknown error creating process object."); 6422715Sstever@eecs.umich.edu return process; 6432715Sstever@eecs.umich.edu} 64411854Sbrandon.potter@amd.com 64511854Sbrandon.potter@amd.comstd::string 64611854Sbrandon.potter@amd.comProcess::fullPath(const std::string &file_name) 64711854Sbrandon.potter@amd.com{ 64811854Sbrandon.potter@amd.com if (file_name[0] == '/' || cwd.empty()) 64911854Sbrandon.potter@amd.com return file_name; 65011854Sbrandon.potter@amd.com 65111854Sbrandon.potter@amd.com std::string full = cwd; 65211854Sbrandon.potter@amd.com 65311854Sbrandon.potter@amd.com if (cwd[cwd.size() - 1] != '/') 65411854Sbrandon.potter@amd.com full += '/'; 65511854Sbrandon.potter@amd.com 65611854Sbrandon.potter@amd.com return full + file_name; 65711854Sbrandon.potter@amd.com} 658