process.cc revision 13557
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
10412448Sgabeblack@google.comProcess::Process(ProcessParams *params, EmulationPageTable *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,
15913557Sgabeblack@google.com               Process *np, RegVal 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,
31312448Sgabeblack@google.com                clobber ? EmulationPageTable::Clobber :
31412460Sgabeblack@google.com                          EmulationPageTable::MappingFlags(0));
3158601Ssteve.reinhardt@amd.com}
3168601Ssteve.reinhardt@amd.com
31711886Sbrandon.potter@amd.comvoid
31811886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
31911886Sbrandon.potter@amd.com                       ThreadContext *new_tc, bool allocate_page)
32011886Sbrandon.potter@amd.com{
32111886Sbrandon.potter@amd.com    if (allocate_page)
32211886Sbrandon.potter@amd.com        new_paddr = system->allocPhysPages(1);
32311886Sbrandon.potter@amd.com
32411886Sbrandon.potter@amd.com    // Read from old physical page.
32511886Sbrandon.potter@amd.com    uint8_t *buf_p = new uint8_t[PageBytes];
32611886Sbrandon.potter@amd.com    old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes);
32711886Sbrandon.potter@amd.com
32811886Sbrandon.potter@amd.com    // Create new mapping in process address space by clobbering existing
32911886Sbrandon.potter@amd.com    // mapping (if any existed) and then write to the new physical page.
33011886Sbrandon.potter@amd.com    bool clobber = true;
33111886Sbrandon.potter@amd.com    pTable->map(vaddr, new_paddr, PageBytes, clobber);
33211886Sbrandon.potter@amd.com    new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes);
33311886Sbrandon.potter@amd.com    delete[] buf_p;
33411886Sbrandon.potter@amd.com}
33511886Sbrandon.potter@amd.com
3364434Ssaidi@eecs.umich.edubool
3378539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr)
3384434Ssaidi@eecs.umich.edu{
33911905SBrandon.Potter@amd.com    Addr stack_min = memState->getStackMin();
34011905SBrandon.Potter@amd.com    Addr stack_base = memState->getStackBase();
34111905SBrandon.Potter@amd.com    Addr max_stack_size = memState->getMaxStackSize();
34211905SBrandon.Potter@amd.com
3438539Sgblack@eecs.umich.edu    // Check if this is already on the stack and there's just no page there
3448539Sgblack@eecs.umich.edu    // yet.
34511905SBrandon.Potter@amd.com    if (vaddr >= stack_min && vaddr < stack_base) {
34610318Sandreas.hansson@arm.com        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
3474434Ssaidi@eecs.umich.edu        return true;
3484434Ssaidi@eecs.umich.edu    }
3494434Ssaidi@eecs.umich.edu
3508539Sgblack@eecs.umich.edu    // We've accessed the next page of the stack, so extend it to include
3518539Sgblack@eecs.umich.edu    // this address.
35211905SBrandon.Potter@amd.com    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
35311905SBrandon.Potter@amd.com        while (vaddr < stack_min) {
35411905SBrandon.Potter@amd.com            stack_min -= TheISA::PageBytes;
35511905SBrandon.Potter@amd.com            if (stack_base - stack_min > max_stack_size)
3565154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
35711905SBrandon.Potter@amd.com            allocateMem(stack_min, TheISA::PageBytes);
3585823Ssaidi@eecs.umich.edu            inform("Increasing stack size by one page.");
35911906SBrandon.Potter@amd.com        }
36011905SBrandon.Potter@amd.com        memState->setStackMin(stack_min);
3614434Ssaidi@eecs.umich.edu        return true;
3624434Ssaidi@eecs.umich.edu    }
3634434Ssaidi@eecs.umich.edu    return false;
3644434Ssaidi@eecs.umich.edu}
3654434Ssaidi@eecs.umich.edu
3665282Srstrong@cs.ucsd.eduvoid
36710905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const
3683311Ssaidi@eecs.umich.edu{
36912283Saustinharris@utexas.edu    memState->serialize(cp);
37010905Sandreas.sandberg@arm.com    pTable->serialize(cp);
37111856Sbrandon.potter@amd.com    /**
37211856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
37311856Sbrandon.potter@amd.com     * come back and fix them at a later date.
37411856Sbrandon.potter@amd.com     */
37511856Sbrandon.potter@amd.com
37611856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
37711856Sbrandon.potter@amd.com#if 0
37811856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
37911856Sbrandon.potter@amd.com        (*fds)[x].serializeSection(cp, csprintf("FDEntry%d", x));
38011856Sbrandon.potter@amd.com#endif
3813311Ssaidi@eecs.umich.edu
3823311Ssaidi@eecs.umich.edu}
3833311Ssaidi@eecs.umich.edu
3843311Ssaidi@eecs.umich.eduvoid
38510905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp)
3863311Ssaidi@eecs.umich.edu{
38712283Saustinharris@utexas.edu    memState->unserialize(cp);
38810905Sandreas.sandberg@arm.com    pTable->unserialize(cp);
38911856Sbrandon.potter@amd.com    /**
39011856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
39111856Sbrandon.potter@amd.com     * come back and fix them at a later date.
39211856Sbrandon.potter@amd.com     */
39311856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
39411856Sbrandon.potter@amd.com#if 0
39511856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
39611856Sbrandon.potter@amd.com        (*fds)[x]->unserializeSection(cp, csprintf("FDEntry%d", x));
39711856Sbrandon.potter@amd.com    fds->restoreFileOffsets();
39811856Sbrandon.potter@amd.com#endif
3996820SLisa.Hsu@amd.com    // The above returns a bool so that you could do something if you don't
4006820SLisa.Hsu@amd.com    // find the param in the checkpoint if you wanted to, like set a default
40110930Sbrandon.potter@amd.com    // but in this case we'll just stick with the instantiated value if not
40210929Sbrandon.potter@amd.com    // found.
4033311Ssaidi@eecs.umich.edu}
4042SN/A
4059110Ssteve.reinhardt@amd.combool
40610558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable)
4079110Ssteve.reinhardt@amd.com{
40810558Salexandru.dutu@amd.com    pTable->map(vaddr, paddr, size,
40912460Sgabeblack@google.com                cacheable ? EmulationPageTable::MappingFlags(0) :
41012448Sgabeblack@google.com                            EmulationPageTable::Uncacheable);
4119110Ssteve.reinhardt@amd.com    return true;
4129110Ssteve.reinhardt@amd.com}
4139110Ssteve.reinhardt@amd.com
4142378SN/Avoid
41511877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
4162093SN/A{
41711886Sbrandon.potter@amd.com    numSyscalls++;
4182093SN/A
4192093SN/A    SyscallDesc *desc = getDesc(callnum);
42011919SBrandon.Potter@amd.com    if (desc == nullptr)
4212093SN/A        fatal("Syscall %d out of range", callnum);
4222093SN/A
42311877Sbrandon.potter@amd.com    desc->doSyscall(callnum, this, tc, fault);
4242093SN/A}
4252SN/A
42613557Sgabeblack@google.comRegVal
42711851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
4286701Sgblack@eecs.umich.edu{
4296701Sgblack@eecs.umich.edu    return getSyscallArg(tc, i);
4306701Sgblack@eecs.umich.edu}
4316701Sgblack@eecs.umich.edu
43210496Ssteve.reinhardt@amd.comEmulatedDriver *
43311851Sbrandon.potter@amd.comProcess::findDriver(std::string filename)
43410496Ssteve.reinhardt@amd.com{
43510496Ssteve.reinhardt@amd.com    for (EmulatedDriver *d : drivers) {
43610496Ssteve.reinhardt@amd.com        if (d->match(filename))
43710496Ssteve.reinhardt@amd.com            return d;
43810496Ssteve.reinhardt@amd.com    }
43910496Ssteve.reinhardt@amd.com
44011919SBrandon.Potter@amd.com    return nullptr;
44110496Ssteve.reinhardt@amd.com}
44210496Ssteve.reinhardt@amd.com
44311389Sbrandon.potter@amd.comvoid
44411851Sbrandon.potter@amd.comProcess::updateBias()
44511389Sbrandon.potter@amd.com{
44611389Sbrandon.potter@amd.com    ObjectFile *interp = objFile->getInterpreter();
44711389Sbrandon.potter@amd.com
44811389Sbrandon.potter@amd.com    if (!interp || !interp->relocatable())
44911389Sbrandon.potter@amd.com        return;
45011389Sbrandon.potter@amd.com
45111389Sbrandon.potter@amd.com    // Determine how large the interpreters footprint will be in the process
45211389Sbrandon.potter@amd.com    // address space.
45311389Sbrandon.potter@amd.com    Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
45411389Sbrandon.potter@amd.com
45511389Sbrandon.potter@amd.com    // We are allocating the memory area; set the bias to the lowest address
45611389Sbrandon.potter@amd.com    // in the allocated memory region.
45711905SBrandon.Potter@amd.com    Addr mmap_end = memState->getMmapEnd();
45811905SBrandon.Potter@amd.com    Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
45911389Sbrandon.potter@amd.com
46011389Sbrandon.potter@amd.com    // Adjust the process mmap area to give the interpreter room; the real
46111389Sbrandon.potter@amd.com    // execve system call would just invoke the kernel's internal mmap
46211389Sbrandon.potter@amd.com    // functions to make these adjustments.
46311905SBrandon.Potter@amd.com    mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
46411905SBrandon.Potter@amd.com    memState->setMmapEnd(mmap_end);
46511389Sbrandon.potter@amd.com
46611389Sbrandon.potter@amd.com    interp->updateBias(ld_bias);
46711389Sbrandon.potter@amd.com}
46811389Sbrandon.potter@amd.com
46911392Sbrandon.potter@amd.comObjectFile *
47011851Sbrandon.potter@amd.comProcess::getInterpreter()
47111392Sbrandon.potter@amd.com{
47211392Sbrandon.potter@amd.com    return objFile->getInterpreter();
47311392Sbrandon.potter@amd.com}
47411392Sbrandon.potter@amd.com
47511389Sbrandon.potter@amd.comAddr
47611851Sbrandon.potter@amd.comProcess::getBias()
47711389Sbrandon.potter@amd.com{
47811392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
47911389Sbrandon.potter@amd.com
48011389Sbrandon.potter@amd.com    return interp ? interp->bias() : objFile->bias();
48111389Sbrandon.potter@amd.com}
48211389Sbrandon.potter@amd.com
48311389Sbrandon.potter@amd.comAddr
48411851Sbrandon.potter@amd.comProcess::getStartPC()
48511389Sbrandon.potter@amd.com{
48611392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
48711389Sbrandon.potter@amd.com
48811389Sbrandon.potter@amd.com    return interp ? interp->entryPoint() : objFile->entryPoint();
48911389Sbrandon.potter@amd.com}
49011389Sbrandon.potter@amd.com
49111851Sbrandon.potter@amd.comProcess *
49211851Sbrandon.potter@amd.comProcessParams::create()
4932715Sstever@eecs.umich.edu{
49411919SBrandon.Potter@amd.com    Process *process = nullptr;
4952715Sstever@eecs.umich.edu
49611140Sjthestness@gmail.com    // If not specified, set the executable parameter equal to the
49711140Sjthestness@gmail.com    // simulated system's zeroth command line parameter
49811851Sbrandon.potter@amd.com    if (executable == "") {
49911851Sbrandon.potter@amd.com        executable = cmd[0];
50011140Sjthestness@gmail.com    }
50111140Sjthestness@gmail.com
50211851Sbrandon.potter@amd.com    ObjectFile *obj_file = createObjectFile(executable);
50311919SBrandon.Potter@amd.com    if (obj_file == nullptr) {
50411851Sbrandon.potter@amd.com        fatal("Can't load object file %s", executable);
5052715Sstever@eecs.umich.edu    }
5062715Sstever@eecs.umich.edu
5075089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA
50811851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Alpha)
5095753Ssteve.reinhardt@amd.com        fatal("Object file architecture does not match compiled ISA (Alpha).");
5105753Ssteve.reinhardt@amd.com
51111851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5125753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5135753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5145753Ssteve.reinhardt@amd.com        // fall through
5152715Sstever@eecs.umich.edu      case ObjectFile::Linux:
51611851Sbrandon.potter@amd.com        process = new AlphaLinuxProcess(this, obj_file);
5172715Sstever@eecs.umich.edu        break;
5182715Sstever@eecs.umich.edu
5192715Sstever@eecs.umich.edu      default:
5202715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5212715Sstever@eecs.umich.edu    }
5222715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
52311851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::SPARC64 &&
52411851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::SPARC32)
5252715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (SPARC).");
52611851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5275753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5285753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5295753Ssteve.reinhardt@amd.com        // fall through
5302715Sstever@eecs.umich.edu      case ObjectFile::Linux:
53111851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::SPARC64) {
53211851Sbrandon.potter@amd.com            process = new Sparc64LinuxProcess(this, obj_file);
5334111Sgblack@eecs.umich.edu        } else {
53411851Sbrandon.potter@amd.com            process = new Sparc32LinuxProcess(this, obj_file);
5354111Sgblack@eecs.umich.edu        }
5362715Sstever@eecs.umich.edu        break;
5372715Sstever@eecs.umich.edu
5382715Sstever@eecs.umich.edu      case ObjectFile::Solaris:
53911851Sbrandon.potter@amd.com        process = new SparcSolarisProcess(this, obj_file);
5402715Sstever@eecs.umich.edu        break;
5415753Ssteve.reinhardt@amd.com
5422715Sstever@eecs.umich.edu      default:
5432715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5442715Sstever@eecs.umich.edu    }
5454157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
54611851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::X86_64 &&
54711851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::I386)
5484166Sgblack@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
54911851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5505753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5515753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5525753Ssteve.reinhardt@amd.com        // fall through
5534166Sgblack@eecs.umich.edu      case ObjectFile::Linux:
55411851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::X86_64) {
55511851Sbrandon.potter@amd.com            process = new X86_64LinuxProcess(this, obj_file);
5565874Sgblack@eecs.umich.edu        } else {
55711851Sbrandon.potter@amd.com            process = new I386LinuxProcess(this, obj_file);
5585874Sgblack@eecs.umich.edu        }
5594166Sgblack@eecs.umich.edu        break;
5605753Ssteve.reinhardt@amd.com
5614157Sgblack@eecs.umich.edu      default:
5624157Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5634157Sgblack@eecs.umich.edu    }
5642715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
56511851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Mips)
5662715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (MIPS).");
56711851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5685753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5695753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5705753Ssteve.reinhardt@amd.com        // fall through
5712715Sstever@eecs.umich.edu      case ObjectFile::Linux:
57211851Sbrandon.potter@amd.com        process = new MipsLinuxProcess(this, obj_file);
5732715Sstever@eecs.umich.edu        break;
5742715Sstever@eecs.umich.edu
5752715Sstever@eecs.umich.edu      default:
5762715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5772715Sstever@eecs.umich.edu    }
5785335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA
57911851Sbrandon.potter@amd.com    ObjectFile::Arch arch = obj_file->getArch();
58010037SARM gem5 Developers    if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
58110037SARM gem5 Developers        arch != ObjectFile::Arm64)
5825335Shines@cs.fsu.edu        fatal("Object file architecture does not match compiled ISA (ARM).");
58311851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5845753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5855753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5865753Ssteve.reinhardt@amd.com        // fall through
5875335Shines@cs.fsu.edu      case ObjectFile::Linux:
58810037SARM gem5 Developers        if (arch == ObjectFile::Arm64) {
58911851Sbrandon.potter@amd.com            process = new ArmLinuxProcess64(this, obj_file,
59011851Sbrandon.potter@amd.com                                            obj_file->getArch());
59110037SARM gem5 Developers        } else {
59211851Sbrandon.potter@amd.com            process = new ArmLinuxProcess32(this, obj_file,
59311851Sbrandon.potter@amd.com                                            obj_file->getArch());
59410037SARM gem5 Developers        }
5955335Shines@cs.fsu.edu        break;
59610810Sbr@bsdpad.com      case ObjectFile::FreeBSD:
59710810Sbr@bsdpad.com        if (arch == ObjectFile::Arm64) {
59811851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess64(this, obj_file,
59911851Sbrandon.potter@amd.com                                              obj_file->getArch());
60010810Sbr@bsdpad.com        } else {
60111851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess32(this, obj_file,
60211851Sbrandon.potter@amd.com                                              obj_file->getArch());
60310810Sbr@bsdpad.com        }
60410810Sbr@bsdpad.com        break;
6056392Ssaidi@eecs.umich.edu      case ObjectFile::LinuxArmOABI:
6066392Ssaidi@eecs.umich.edu        fatal("M5 does not support ARM OABI binaries. Please recompile with an"
6076392Ssaidi@eecs.umich.edu              " EABI compiler.");
6085335Shines@cs.fsu.edu      default:
6095335Shines@cs.fsu.edu        fatal("Unknown/unsupported operating system.");
6105335Shines@cs.fsu.edu    }
6116691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA
61211851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Power)
6136691Stjones1@inf.ed.ac.uk        fatal("Object file architecture does not match compiled ISA (Power).");
61411851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
6156691Stjones1@inf.ed.ac.uk      case ObjectFile::UnknownOpSys:
6166691Stjones1@inf.ed.ac.uk        warn("Unknown operating system; assuming Linux.");
6176691Stjones1@inf.ed.ac.uk        // fall through
6186691Stjones1@inf.ed.ac.uk      case ObjectFile::Linux:
61911851Sbrandon.potter@amd.com        process = new PowerLinuxProcess(this, obj_file);
6206691Stjones1@inf.ed.ac.uk        break;
6216691Stjones1@inf.ed.ac.uk
6226691Stjones1@inf.ed.ac.uk      default:
6236691Stjones1@inf.ed.ac.uk        fatal("Unknown/unsupported operating system.");
6246691Stjones1@inf.ed.ac.uk    }
62511723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA
62611851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Riscv)
62711723Sar4jc@virginia.edu        fatal("Object file architecture does not match compiled ISA (RISCV).");
62811851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
62911723Sar4jc@virginia.edu      case ObjectFile::UnknownOpSys:
63011723Sar4jc@virginia.edu        warn("Unknown operating system; assuming Linux.");
63111723Sar4jc@virginia.edu        // fall through
63211723Sar4jc@virginia.edu      case ObjectFile::Linux:
63311851Sbrandon.potter@amd.com        process = new RiscvLinuxProcess(this, obj_file);
63411723Sar4jc@virginia.edu        break;
63511723Sar4jc@virginia.edu      default:
63611723Sar4jc@virginia.edu        fatal("Unknown/unsupported operating system.");
63711723Sar4jc@virginia.edu    }
6382715Sstever@eecs.umich.edu#else
6392715Sstever@eecs.umich.edu#error "THE_ISA not set"
6402715Sstever@eecs.umich.edu#endif
6412715Sstever@eecs.umich.edu
64211919SBrandon.Potter@amd.com    if (process == nullptr)
6432715Sstever@eecs.umich.edu        fatal("Unknown error creating process object.");
6442715Sstever@eecs.umich.edu    return process;
6452715Sstever@eecs.umich.edu}
64611854Sbrandon.potter@amd.com
64711854Sbrandon.potter@amd.comstd::string
64811854Sbrandon.potter@amd.comProcess::fullPath(const std::string &file_name)
64911854Sbrandon.potter@amd.com{
65011854Sbrandon.potter@amd.com    if (file_name[0] == '/' || cwd.empty())
65111854Sbrandon.potter@amd.com        return file_name;
65211854Sbrandon.potter@amd.com
65311854Sbrandon.potter@amd.com    std::string full = cwd;
65411854Sbrandon.potter@amd.com
65511854Sbrandon.potter@amd.com    if (cwd[cwd.size() - 1] != '/')
65611854Sbrandon.potter@amd.com        full += '/';
65711854Sbrandon.potter@amd.com
65811854Sbrandon.potter@amd.com    return full + file_name;
65911854Sbrandon.potter@amd.com}
660