process.cc revision 13867
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),
10913867Salexandru.dutu@amd.com      useForClone(false),
11012431Sgabeblack@google.com      pTable(pTable),
1118852Sandreas.hansson@arm.com      initVirtMem(system->getSystemPort(), this,
11210929Sbrandon.potter@amd.com                  SETranslatingPortProxy::Always),
11311851Sbrandon.potter@amd.com      objFile(obj_file),
11411851Sbrandon.potter@amd.com      argv(params->cmd), envp(params->env), cwd(params->cwd),
11511851Sbrandon.potter@amd.com      executable(params->executable),
11611801Sbrandon.potter@amd.com      _uid(params->uid), _euid(params->euid),
11711801Sbrandon.potter@amd.com      _gid(params->gid), _egid(params->egid),
11811851Sbrandon.potter@amd.com      _pid(params->pid), _ppid(params->ppid),
11911885Sbrandon.potter@amd.com      _pgid(params->pgid), drivers(params->drivers),
12011886Sbrandon.potter@amd.com      fds(make_shared<FDArray>(params->input, params->output, params->errout)),
12111886Sbrandon.potter@amd.com      childClearTID(0)
1222SN/A{
12311885Sbrandon.potter@amd.com    if (_pid >= System::maxPID)
12411885Sbrandon.potter@amd.com        fatal("_pid is too large: %d", _pid);
12511885Sbrandon.potter@amd.com
12611885Sbrandon.potter@amd.com    auto ret_pair = system->PIDs.emplace(_pid);
12711885Sbrandon.potter@amd.com    if (!ret_pair.second)
12811885Sbrandon.potter@amd.com        fatal("_pid %d is already used", _pid);
12911885Sbrandon.potter@amd.com
13011886Sbrandon.potter@amd.com    /**
13111886Sbrandon.potter@amd.com     * Linux bundles together processes into this concept called a thread
13211886Sbrandon.potter@amd.com     * group. The thread group is responsible for recording which processes
13311886Sbrandon.potter@amd.com     * behave as threads within a process context. The thread group leader
13411886Sbrandon.potter@amd.com     * is the process who's tgid is equal to its pid. Other processes which
13511886Sbrandon.potter@amd.com     * belong to the thread group, but do not lead the thread group, are
13611886Sbrandon.potter@amd.com     * treated as child threads. These threads are created by the clone system
13711886Sbrandon.potter@amd.com     * call with options specified to create threads (differing from the
13811886Sbrandon.potter@amd.com     * options used to implement a fork). By default, set up the tgid/pid
13911886Sbrandon.potter@amd.com     * with a new, equivalent value. If CLONE_THREAD is specified, patch
14011886Sbrandon.potter@amd.com     * the tgid value with the old process' value.
14111886Sbrandon.potter@amd.com     */
14211886Sbrandon.potter@amd.com    _tgid = params->pid;
14311886Sbrandon.potter@amd.com
14411886Sbrandon.potter@amd.com    exitGroup = new bool();
14511886Sbrandon.potter@amd.com    sigchld = new bool();
14611886Sbrandon.potter@amd.com
14711851Sbrandon.potter@amd.com    if (!debugSymbolTable) {
14811851Sbrandon.potter@amd.com        debugSymbolTable = new SymbolTable();
14911851Sbrandon.potter@amd.com        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
15011851Sbrandon.potter@amd.com            !objFile->loadLocalSymbols(debugSymbolTable) ||
15111851Sbrandon.potter@amd.com            !objFile->loadWeakSymbols(debugSymbolTable)) {
15211851Sbrandon.potter@amd.com            delete debugSymbolTable;
15311919SBrandon.Potter@amd.com            debugSymbolTable = nullptr;
15411851Sbrandon.potter@amd.com        }
15511851Sbrandon.potter@amd.com    }
1562SN/A}
1572SN/A
1582SN/Avoid
15911886Sbrandon.potter@amd.comProcess::clone(ThreadContext *otc, ThreadContext *ntc,
16013557Sgabeblack@google.com               Process *np, RegVal flags)
16111886Sbrandon.potter@amd.com{
16211920SBrandon.Potter@amd.com#ifndef CLONE_VM
16311920SBrandon.Potter@amd.com#define CLONE_VM 0
16411920SBrandon.Potter@amd.com#endif
16511920SBrandon.Potter@amd.com#ifndef CLONE_FILES
16611920SBrandon.Potter@amd.com#define CLONE_FILES 0
16711920SBrandon.Potter@amd.com#endif
16811920SBrandon.Potter@amd.com#ifndef CLONE_THREAD
16911920SBrandon.Potter@amd.com#define CLONE_THREAD 0
17011920SBrandon.Potter@amd.com#endif
17111886Sbrandon.potter@amd.com    if (CLONE_VM & flags) {
17211886Sbrandon.potter@amd.com        /**
17311886Sbrandon.potter@amd.com         * Share the process memory address space between the new process
17411886Sbrandon.potter@amd.com         * and the old process. Changes in one will be visible in the other
17511886Sbrandon.potter@amd.com         * due to the pointer use.
17611886Sbrandon.potter@amd.com         */
17711886Sbrandon.potter@amd.com        delete np->pTable;
17811886Sbrandon.potter@amd.com        np->pTable = pTable;
17911886Sbrandon.potter@amd.com        ntc->getMemProxy().setPageTable(np->pTable);
18011886Sbrandon.potter@amd.com
18111886Sbrandon.potter@amd.com        np->memState = memState;
18211886Sbrandon.potter@amd.com    } else {
18311886Sbrandon.potter@amd.com        /**
18411886Sbrandon.potter@amd.com         * Duplicate the process memory address space. The state needs to be
18511886Sbrandon.potter@amd.com         * copied over (rather than using pointers to share everything).
18611886Sbrandon.potter@amd.com         */
18711886Sbrandon.potter@amd.com        typedef std::vector<pair<Addr,Addr>> MapVec;
18811886Sbrandon.potter@amd.com        MapVec mappings;
18911886Sbrandon.potter@amd.com        pTable->getMappings(&mappings);
19011886Sbrandon.potter@amd.com
19111886Sbrandon.potter@amd.com        for (auto map : mappings) {
19211886Sbrandon.potter@amd.com            Addr paddr, vaddr = map.first;
19311886Sbrandon.potter@amd.com            bool alloc_page = !(np->pTable->translate(vaddr, paddr));
19411886Sbrandon.potter@amd.com            np->replicatePage(vaddr, paddr, otc, ntc, alloc_page);
19511886Sbrandon.potter@amd.com        }
19611886Sbrandon.potter@amd.com
19711886Sbrandon.potter@amd.com        *np->memState = *memState;
19811886Sbrandon.potter@amd.com    }
19911886Sbrandon.potter@amd.com
20011886Sbrandon.potter@amd.com    if (CLONE_FILES & flags) {
20111886Sbrandon.potter@amd.com        /**
20211886Sbrandon.potter@amd.com         * The parent and child file descriptors are shared because the
20311886Sbrandon.potter@amd.com         * two FDArray pointers are pointing to the same FDArray. Opening
20411886Sbrandon.potter@amd.com         * and closing file descriptors will be visible to both processes.
20511886Sbrandon.potter@amd.com         */
20611886Sbrandon.potter@amd.com        np->fds = fds;
20711886Sbrandon.potter@amd.com    } else {
20811886Sbrandon.potter@amd.com        /**
20911886Sbrandon.potter@amd.com         * Copy the file descriptors from the old process into the new
21011886Sbrandon.potter@amd.com         * child process. The file descriptors entry can be opened and
21111886Sbrandon.potter@amd.com         * closed independently of the other process being considered. The
21211886Sbrandon.potter@amd.com         * host file descriptors are also dup'd so that the flags for the
21311886Sbrandon.potter@amd.com         * host file descriptor is independent of the other process.
21411886Sbrandon.potter@amd.com         */
21511886Sbrandon.potter@amd.com        for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) {
21611886Sbrandon.potter@amd.com            std::shared_ptr<FDArray> nfds = np->fds;
21711886Sbrandon.potter@amd.com            std::shared_ptr<FDEntry> this_fde = (*fds)[tgt_fd];
21811886Sbrandon.potter@amd.com            if (!this_fde) {
21911886Sbrandon.potter@amd.com                nfds->setFDEntry(tgt_fd, nullptr);
22011886Sbrandon.potter@amd.com                continue;
22111886Sbrandon.potter@amd.com            }
22211886Sbrandon.potter@amd.com            nfds->setFDEntry(tgt_fd, this_fde->clone());
22311886Sbrandon.potter@amd.com
22411886Sbrandon.potter@amd.com            auto this_hbfd = std::dynamic_pointer_cast<HBFDEntry>(this_fde);
22511886Sbrandon.potter@amd.com            if (!this_hbfd)
22611886Sbrandon.potter@amd.com                continue;
22711886Sbrandon.potter@amd.com
22811886Sbrandon.potter@amd.com            int this_sim_fd = this_hbfd->getSimFD();
22911886Sbrandon.potter@amd.com            if (this_sim_fd <= 2)
23011886Sbrandon.potter@amd.com                continue;
23111886Sbrandon.potter@amd.com
23211886Sbrandon.potter@amd.com            int np_sim_fd = dup(this_sim_fd);
23311886Sbrandon.potter@amd.com            assert(np_sim_fd != -1);
23411886Sbrandon.potter@amd.com
23511886Sbrandon.potter@amd.com            auto nhbfd = std::dynamic_pointer_cast<HBFDEntry>((*nfds)[tgt_fd]);
23611886Sbrandon.potter@amd.com            nhbfd->setSimFD(np_sim_fd);
23711886Sbrandon.potter@amd.com        }
23811886Sbrandon.potter@amd.com    }
23911886Sbrandon.potter@amd.com
24011886Sbrandon.potter@amd.com    if (CLONE_THREAD & flags) {
24111886Sbrandon.potter@amd.com        np->_tgid = _tgid;
24211886Sbrandon.potter@amd.com        delete np->exitGroup;
24311886Sbrandon.potter@amd.com        np->exitGroup = exitGroup;
24411886Sbrandon.potter@amd.com    }
24511886Sbrandon.potter@amd.com
24611886Sbrandon.potter@amd.com    np->argv.insert(np->argv.end(), argv.begin(), argv.end());
24711886Sbrandon.potter@amd.com    np->envp.insert(np->envp.end(), envp.begin(), envp.end());
24811886Sbrandon.potter@amd.com}
24911886Sbrandon.potter@amd.com
25011886Sbrandon.potter@amd.comvoid
2512SN/AProcess::regStats()
2522SN/A{
25311522Sstephan.diestelhorst@arm.com    SimObject::regStats();
25411522Sstephan.diestelhorst@arm.com
255729SN/A    using namespace Stats;
2562SN/A
25711886Sbrandon.potter@amd.com    numSyscalls
25811886Sbrandon.potter@amd.com        .name(name() + ".numSyscalls")
2592SN/A        .desc("Number of system calls")
2602SN/A        ;
2612SN/A}
2622SN/A
2635713Shsul@eecs.umich.eduThreadContext *
2645713Shsul@eecs.umich.eduProcess::findFreeContext()
2652SN/A{
26611886Sbrandon.potter@amd.com    for (auto &it : system->threadContexts) {
26711886Sbrandon.potter@amd.com        if (ThreadContext::Halted == it->status())
26811886Sbrandon.potter@amd.com            return it;
2695512SMichael.Adler@intel.com    }
27011919SBrandon.Potter@amd.com    return nullptr;
2712SN/A}
2722SN/A
2731395SN/Avoid
27411886Sbrandon.potter@amd.comProcess::revokeThreadContext(int context_id)
27511886Sbrandon.potter@amd.com{
27611886Sbrandon.potter@amd.com    std::vector<ContextID>::iterator it;
27711886Sbrandon.potter@amd.com    for (it = contextIds.begin(); it != contextIds.end(); it++) {
27811886Sbrandon.potter@amd.com        if (*it == context_id) {
27911886Sbrandon.potter@amd.com            contextIds.erase(it);
28011886Sbrandon.potter@amd.com            return;
28111886Sbrandon.potter@amd.com        }
28211886Sbrandon.potter@amd.com    }
28311886Sbrandon.potter@amd.com    warn("Unable to find thread context to revoke");
28411886Sbrandon.potter@amd.com}
28511886Sbrandon.potter@amd.com
28611886Sbrandon.potter@amd.comvoid
2877532Ssteve.reinhardt@amd.comProcess::initState()
2881395SN/A{
2895713Shsul@eecs.umich.edu    if (contextIds.empty())
2905713Shsul@eecs.umich.edu        fatal("Process %s is not associated with any HW contexts!\n", name());
2912378SN/A
2922680Sktlim@umich.edu    // first thread context for this process... initialize & enable
2935713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
2941395SN/A
2951634SN/A    // mark this context as active so it will start ticking.
29610407Smitch.hayenga@arm.com    tc->activate();
29710298Salexandru.dutu@amd.com
29810298Salexandru.dutu@amd.com    pTable->initState(tc);
2991395SN/A}
3002SN/A
30110913Sandreas.sandberg@arm.comDrainState
30210913Sandreas.sandberg@arm.comProcess::drain()
30310905Sandreas.sandberg@arm.com{
30411856Sbrandon.potter@amd.com    fds->updateFileOffsets();
30510913Sandreas.sandberg@arm.com    return DrainState::Drained;
30610905Sandreas.sandberg@arm.com}
30710905Sandreas.sandberg@arm.com
3088601Ssteve.reinhardt@amd.comvoid
3098601Ssteve.reinhardt@amd.comProcess::allocateMem(Addr vaddr, int64_t size, bool clobber)
3108601Ssteve.reinhardt@amd.com{
31110318Sandreas.hansson@arm.com    int npages = divCeil(size, (int64_t)PageBytes);
3128601Ssteve.reinhardt@amd.com    Addr paddr = system->allocPhysPages(npages);
31311294Sandreas.hansson@arm.com    pTable->map(vaddr, paddr, size,
31412448Sgabeblack@google.com                clobber ? EmulationPageTable::Clobber :
31512460Sgabeblack@google.com                          EmulationPageTable::MappingFlags(0));
3168601Ssteve.reinhardt@amd.com}
3178601Ssteve.reinhardt@amd.com
31811886Sbrandon.potter@amd.comvoid
31911886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
32011886Sbrandon.potter@amd.com                       ThreadContext *new_tc, bool allocate_page)
32111886Sbrandon.potter@amd.com{
32211886Sbrandon.potter@amd.com    if (allocate_page)
32311886Sbrandon.potter@amd.com        new_paddr = system->allocPhysPages(1);
32411886Sbrandon.potter@amd.com
32511886Sbrandon.potter@amd.com    // Read from old physical page.
32611886Sbrandon.potter@amd.com    uint8_t *buf_p = new uint8_t[PageBytes];
32711886Sbrandon.potter@amd.com    old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes);
32811886Sbrandon.potter@amd.com
32911886Sbrandon.potter@amd.com    // Create new mapping in process address space by clobbering existing
33011886Sbrandon.potter@amd.com    // mapping (if any existed) and then write to the new physical page.
33111886Sbrandon.potter@amd.com    bool clobber = true;
33211886Sbrandon.potter@amd.com    pTable->map(vaddr, new_paddr, PageBytes, clobber);
33311886Sbrandon.potter@amd.com    new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes);
33411886Sbrandon.potter@amd.com    delete[] buf_p;
33511886Sbrandon.potter@amd.com}
33611886Sbrandon.potter@amd.com
3374434Ssaidi@eecs.umich.edubool
3388539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr)
3394434Ssaidi@eecs.umich.edu{
34011905SBrandon.Potter@amd.com    Addr stack_min = memState->getStackMin();
34111905SBrandon.Potter@amd.com    Addr stack_base = memState->getStackBase();
34211905SBrandon.Potter@amd.com    Addr max_stack_size = memState->getMaxStackSize();
34311905SBrandon.Potter@amd.com
3448539Sgblack@eecs.umich.edu    // Check if this is already on the stack and there's just no page there
3458539Sgblack@eecs.umich.edu    // yet.
34611905SBrandon.Potter@amd.com    if (vaddr >= stack_min && vaddr < stack_base) {
34710318Sandreas.hansson@arm.com        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
3484434Ssaidi@eecs.umich.edu        return true;
3494434Ssaidi@eecs.umich.edu    }
3504434Ssaidi@eecs.umich.edu
3518539Sgblack@eecs.umich.edu    // We've accessed the next page of the stack, so extend it to include
3528539Sgblack@eecs.umich.edu    // this address.
35311905SBrandon.Potter@amd.com    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
35411905SBrandon.Potter@amd.com        while (vaddr < stack_min) {
35511905SBrandon.Potter@amd.com            stack_min -= TheISA::PageBytes;
35611905SBrandon.Potter@amd.com            if (stack_base - stack_min > max_stack_size)
3575154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
35811905SBrandon.Potter@amd.com            allocateMem(stack_min, TheISA::PageBytes);
3595823Ssaidi@eecs.umich.edu            inform("Increasing stack size by one page.");
36011906SBrandon.Potter@amd.com        }
36111905SBrandon.Potter@amd.com        memState->setStackMin(stack_min);
3624434Ssaidi@eecs.umich.edu        return true;
3634434Ssaidi@eecs.umich.edu    }
3644434Ssaidi@eecs.umich.edu    return false;
3654434Ssaidi@eecs.umich.edu}
3664434Ssaidi@eecs.umich.edu
3675282Srstrong@cs.ucsd.eduvoid
36810905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const
3693311Ssaidi@eecs.umich.edu{
37012283Saustinharris@utexas.edu    memState->serialize(cp);
37110905Sandreas.sandberg@arm.com    pTable->serialize(cp);
37211856Sbrandon.potter@amd.com    /**
37311856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
37411856Sbrandon.potter@amd.com     * come back and fix them at a later date.
37511856Sbrandon.potter@amd.com     */
37611856Sbrandon.potter@amd.com
37711856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
37811856Sbrandon.potter@amd.com#if 0
37911856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
38011856Sbrandon.potter@amd.com        (*fds)[x].serializeSection(cp, csprintf("FDEntry%d", x));
38111856Sbrandon.potter@amd.com#endif
3823311Ssaidi@eecs.umich.edu
3833311Ssaidi@eecs.umich.edu}
3843311Ssaidi@eecs.umich.edu
3853311Ssaidi@eecs.umich.eduvoid
38610905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp)
3873311Ssaidi@eecs.umich.edu{
38812283Saustinharris@utexas.edu    memState->unserialize(cp);
38910905Sandreas.sandberg@arm.com    pTable->unserialize(cp);
39011856Sbrandon.potter@amd.com    /**
39111856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
39211856Sbrandon.potter@amd.com     * come back and fix them at a later date.
39311856Sbrandon.potter@amd.com     */
39411856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
39511856Sbrandon.potter@amd.com#if 0
39611856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
39711856Sbrandon.potter@amd.com        (*fds)[x]->unserializeSection(cp, csprintf("FDEntry%d", x));
39811856Sbrandon.potter@amd.com    fds->restoreFileOffsets();
39911856Sbrandon.potter@amd.com#endif
4006820SLisa.Hsu@amd.com    // The above returns a bool so that you could do something if you don't
4016820SLisa.Hsu@amd.com    // find the param in the checkpoint if you wanted to, like set a default
40210930Sbrandon.potter@amd.com    // but in this case we'll just stick with the instantiated value if not
40310929Sbrandon.potter@amd.com    // found.
4043311Ssaidi@eecs.umich.edu}
4052SN/A
4069110Ssteve.reinhardt@amd.combool
40710558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable)
4089110Ssteve.reinhardt@amd.com{
40910558Salexandru.dutu@amd.com    pTable->map(vaddr, paddr, size,
41012460Sgabeblack@google.com                cacheable ? EmulationPageTable::MappingFlags(0) :
41112448Sgabeblack@google.com                            EmulationPageTable::Uncacheable);
4129110Ssteve.reinhardt@amd.com    return true;
4139110Ssteve.reinhardt@amd.com}
4149110Ssteve.reinhardt@amd.com
4152378SN/Avoid
41611877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
4172093SN/A{
41811886Sbrandon.potter@amd.com    numSyscalls++;
4192093SN/A
4202093SN/A    SyscallDesc *desc = getDesc(callnum);
42111919SBrandon.Potter@amd.com    if (desc == nullptr)
4222093SN/A        fatal("Syscall %d out of range", callnum);
4232093SN/A
42411877Sbrandon.potter@amd.com    desc->doSyscall(callnum, this, tc, fault);
4252093SN/A}
4262SN/A
42713557Sgabeblack@google.comRegVal
42811851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
4296701Sgblack@eecs.umich.edu{
4306701Sgblack@eecs.umich.edu    return getSyscallArg(tc, i);
4316701Sgblack@eecs.umich.edu}
4326701Sgblack@eecs.umich.edu
43310496Ssteve.reinhardt@amd.comEmulatedDriver *
43411851Sbrandon.potter@amd.comProcess::findDriver(std::string filename)
43510496Ssteve.reinhardt@amd.com{
43610496Ssteve.reinhardt@amd.com    for (EmulatedDriver *d : drivers) {
43710496Ssteve.reinhardt@amd.com        if (d->match(filename))
43810496Ssteve.reinhardt@amd.com            return d;
43910496Ssteve.reinhardt@amd.com    }
44010496Ssteve.reinhardt@amd.com
44111919SBrandon.Potter@amd.com    return nullptr;
44210496Ssteve.reinhardt@amd.com}
44310496Ssteve.reinhardt@amd.com
44411389Sbrandon.potter@amd.comvoid
44511851Sbrandon.potter@amd.comProcess::updateBias()
44611389Sbrandon.potter@amd.com{
44711389Sbrandon.potter@amd.com    ObjectFile *interp = objFile->getInterpreter();
44811389Sbrandon.potter@amd.com
44911389Sbrandon.potter@amd.com    if (!interp || !interp->relocatable())
45011389Sbrandon.potter@amd.com        return;
45111389Sbrandon.potter@amd.com
45211389Sbrandon.potter@amd.com    // Determine how large the interpreters footprint will be in the process
45311389Sbrandon.potter@amd.com    // address space.
45411389Sbrandon.potter@amd.com    Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
45511389Sbrandon.potter@amd.com
45611389Sbrandon.potter@amd.com    // We are allocating the memory area; set the bias to the lowest address
45711389Sbrandon.potter@amd.com    // in the allocated memory region.
45811905SBrandon.Potter@amd.com    Addr mmap_end = memState->getMmapEnd();
45911905SBrandon.Potter@amd.com    Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
46011389Sbrandon.potter@amd.com
46111389Sbrandon.potter@amd.com    // Adjust the process mmap area to give the interpreter room; the real
46211389Sbrandon.potter@amd.com    // execve system call would just invoke the kernel's internal mmap
46311389Sbrandon.potter@amd.com    // functions to make these adjustments.
46411905SBrandon.Potter@amd.com    mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
46511905SBrandon.Potter@amd.com    memState->setMmapEnd(mmap_end);
46611389Sbrandon.potter@amd.com
46711389Sbrandon.potter@amd.com    interp->updateBias(ld_bias);
46811389Sbrandon.potter@amd.com}
46911389Sbrandon.potter@amd.com
47011392Sbrandon.potter@amd.comObjectFile *
47111851Sbrandon.potter@amd.comProcess::getInterpreter()
47211392Sbrandon.potter@amd.com{
47311392Sbrandon.potter@amd.com    return objFile->getInterpreter();
47411392Sbrandon.potter@amd.com}
47511392Sbrandon.potter@amd.com
47611389Sbrandon.potter@amd.comAddr
47711851Sbrandon.potter@amd.comProcess::getBias()
47811389Sbrandon.potter@amd.com{
47911392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
48011389Sbrandon.potter@amd.com
48111389Sbrandon.potter@amd.com    return interp ? interp->bias() : objFile->bias();
48211389Sbrandon.potter@amd.com}
48311389Sbrandon.potter@amd.com
48411389Sbrandon.potter@amd.comAddr
48511851Sbrandon.potter@amd.comProcess::getStartPC()
48611389Sbrandon.potter@amd.com{
48711392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
48811389Sbrandon.potter@amd.com
48911389Sbrandon.potter@amd.com    return interp ? interp->entryPoint() : objFile->entryPoint();
49011389Sbrandon.potter@amd.com}
49111389Sbrandon.potter@amd.com
49211851Sbrandon.potter@amd.comProcess *
49311851Sbrandon.potter@amd.comProcessParams::create()
4942715Sstever@eecs.umich.edu{
49511919SBrandon.Potter@amd.com    Process *process = nullptr;
4962715Sstever@eecs.umich.edu
49711140Sjthestness@gmail.com    // If not specified, set the executable parameter equal to the
49811140Sjthestness@gmail.com    // simulated system's zeroth command line parameter
49911851Sbrandon.potter@amd.com    if (executable == "") {
50011851Sbrandon.potter@amd.com        executable = cmd[0];
50111140Sjthestness@gmail.com    }
50211140Sjthestness@gmail.com
50311851Sbrandon.potter@amd.com    ObjectFile *obj_file = createObjectFile(executable);
50411919SBrandon.Potter@amd.com    if (obj_file == nullptr) {
50511851Sbrandon.potter@amd.com        fatal("Can't load object file %s", executable);
5062715Sstever@eecs.umich.edu    }
5072715Sstever@eecs.umich.edu
5085089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA
50911851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Alpha)
5105753Ssteve.reinhardt@amd.com        fatal("Object file architecture does not match compiled ISA (Alpha).");
5115753Ssteve.reinhardt@amd.com
51211851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5135753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5145753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5155753Ssteve.reinhardt@amd.com        // fall through
5162715Sstever@eecs.umich.edu      case ObjectFile::Linux:
51711851Sbrandon.potter@amd.com        process = new AlphaLinuxProcess(this, obj_file);
5182715Sstever@eecs.umich.edu        break;
5192715Sstever@eecs.umich.edu
5202715Sstever@eecs.umich.edu      default:
5212715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5222715Sstever@eecs.umich.edu    }
5232715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
52411851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::SPARC64 &&
52511851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::SPARC32)
5262715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (SPARC).");
52711851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5285753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5295753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5305753Ssteve.reinhardt@amd.com        // fall through
5312715Sstever@eecs.umich.edu      case ObjectFile::Linux:
53211851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::SPARC64) {
53311851Sbrandon.potter@amd.com            process = new Sparc64LinuxProcess(this, obj_file);
5344111Sgblack@eecs.umich.edu        } else {
53511851Sbrandon.potter@amd.com            process = new Sparc32LinuxProcess(this, obj_file);
5364111Sgblack@eecs.umich.edu        }
5372715Sstever@eecs.umich.edu        break;
5382715Sstever@eecs.umich.edu
5392715Sstever@eecs.umich.edu      case ObjectFile::Solaris:
54011851Sbrandon.potter@amd.com        process = new SparcSolarisProcess(this, obj_file);
5412715Sstever@eecs.umich.edu        break;
5425753Ssteve.reinhardt@amd.com
5432715Sstever@eecs.umich.edu      default:
5442715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5452715Sstever@eecs.umich.edu    }
5464157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
54711851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::X86_64 &&
54811851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::I386)
5494166Sgblack@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
55011851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5515753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5525753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5535753Ssteve.reinhardt@amd.com        // fall through
5544166Sgblack@eecs.umich.edu      case ObjectFile::Linux:
55511851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::X86_64) {
55611851Sbrandon.potter@amd.com            process = new X86_64LinuxProcess(this, obj_file);
5575874Sgblack@eecs.umich.edu        } else {
55811851Sbrandon.potter@amd.com            process = new I386LinuxProcess(this, obj_file);
5595874Sgblack@eecs.umich.edu        }
5604166Sgblack@eecs.umich.edu        break;
5615753Ssteve.reinhardt@amd.com
5624157Sgblack@eecs.umich.edu      default:
5634157Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5644157Sgblack@eecs.umich.edu    }
5652715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
56611851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Mips)
5672715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (MIPS).");
56811851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5695753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5705753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5715753Ssteve.reinhardt@amd.com        // fall through
5722715Sstever@eecs.umich.edu      case ObjectFile::Linux:
57311851Sbrandon.potter@amd.com        process = new MipsLinuxProcess(this, obj_file);
5742715Sstever@eecs.umich.edu        break;
5752715Sstever@eecs.umich.edu
5762715Sstever@eecs.umich.edu      default:
5772715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5782715Sstever@eecs.umich.edu    }
5795335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA
58011851Sbrandon.potter@amd.com    ObjectFile::Arch arch = obj_file->getArch();
58110037SARM gem5 Developers    if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
58210037SARM gem5 Developers        arch != ObjectFile::Arm64)
5835335Shines@cs.fsu.edu        fatal("Object file architecture does not match compiled ISA (ARM).");
58411851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5855753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5865753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5875753Ssteve.reinhardt@amd.com        // fall through
5885335Shines@cs.fsu.edu      case ObjectFile::Linux:
58910037SARM gem5 Developers        if (arch == ObjectFile::Arm64) {
59011851Sbrandon.potter@amd.com            process = new ArmLinuxProcess64(this, obj_file,
59111851Sbrandon.potter@amd.com                                            obj_file->getArch());
59210037SARM gem5 Developers        } else {
59311851Sbrandon.potter@amd.com            process = new ArmLinuxProcess32(this, obj_file,
59411851Sbrandon.potter@amd.com                                            obj_file->getArch());
59510037SARM gem5 Developers        }
5965335Shines@cs.fsu.edu        break;
59710810Sbr@bsdpad.com      case ObjectFile::FreeBSD:
59810810Sbr@bsdpad.com        if (arch == ObjectFile::Arm64) {
59911851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess64(this, obj_file,
60011851Sbrandon.potter@amd.com                                              obj_file->getArch());
60110810Sbr@bsdpad.com        } else {
60211851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess32(this, obj_file,
60311851Sbrandon.potter@amd.com                                              obj_file->getArch());
60410810Sbr@bsdpad.com        }
60510810Sbr@bsdpad.com        break;
6066392Ssaidi@eecs.umich.edu      case ObjectFile::LinuxArmOABI:
6076392Ssaidi@eecs.umich.edu        fatal("M5 does not support ARM OABI binaries. Please recompile with an"
6086392Ssaidi@eecs.umich.edu              " EABI compiler.");
6095335Shines@cs.fsu.edu      default:
6105335Shines@cs.fsu.edu        fatal("Unknown/unsupported operating system.");
6115335Shines@cs.fsu.edu    }
6126691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA
61311851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Power)
6146691Stjones1@inf.ed.ac.uk        fatal("Object file architecture does not match compiled ISA (Power).");
61511851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
6166691Stjones1@inf.ed.ac.uk      case ObjectFile::UnknownOpSys:
6176691Stjones1@inf.ed.ac.uk        warn("Unknown operating system; assuming Linux.");
6186691Stjones1@inf.ed.ac.uk        // fall through
6196691Stjones1@inf.ed.ac.uk      case ObjectFile::Linux:
62011851Sbrandon.potter@amd.com        process = new PowerLinuxProcess(this, obj_file);
6216691Stjones1@inf.ed.ac.uk        break;
6226691Stjones1@inf.ed.ac.uk
6236691Stjones1@inf.ed.ac.uk      default:
6246691Stjones1@inf.ed.ac.uk        fatal("Unknown/unsupported operating system.");
6256691Stjones1@inf.ed.ac.uk    }
62611723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA
62713634Saustinharris@utexas.edu    ObjectFile::Arch arch = obj_file->getArch();
62813634Saustinharris@utexas.edu    if (arch != ObjectFile::Riscv64 && arch != ObjectFile::Riscv32)
62911723Sar4jc@virginia.edu        fatal("Object file architecture does not match compiled ISA (RISCV).");
63011851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
63111723Sar4jc@virginia.edu      case ObjectFile::UnknownOpSys:
63211723Sar4jc@virginia.edu        warn("Unknown operating system; assuming Linux.");
63311723Sar4jc@virginia.edu        // fall through
63411723Sar4jc@virginia.edu      case ObjectFile::Linux:
63513634Saustinharris@utexas.edu        if (arch == ObjectFile::Riscv64) {
63613634Saustinharris@utexas.edu            process = new RiscvLinuxProcess64(this, obj_file);
63713634Saustinharris@utexas.edu        } else {
63813634Saustinharris@utexas.edu            process = new RiscvLinuxProcess32(this, obj_file);
63913634Saustinharris@utexas.edu        }
64011723Sar4jc@virginia.edu        break;
64111723Sar4jc@virginia.edu      default:
64211723Sar4jc@virginia.edu        fatal("Unknown/unsupported operating system.");
64311723Sar4jc@virginia.edu    }
6442715Sstever@eecs.umich.edu#else
6452715Sstever@eecs.umich.edu#error "THE_ISA not set"
6462715Sstever@eecs.umich.edu#endif
6472715Sstever@eecs.umich.edu
64811919SBrandon.Potter@amd.com    if (process == nullptr)
6492715Sstever@eecs.umich.edu        fatal("Unknown error creating process object.");
6502715Sstever@eecs.umich.edu    return process;
6512715Sstever@eecs.umich.edu}
65211854Sbrandon.potter@amd.com
65311854Sbrandon.potter@amd.comstd::string
65411854Sbrandon.potter@amd.comProcess::fullPath(const std::string &file_name)
65511854Sbrandon.potter@amd.com{
65611854Sbrandon.potter@amd.com    if (file_name[0] == '/' || cwd.empty())
65711854Sbrandon.potter@amd.com        return file_name;
65811854Sbrandon.potter@amd.com
65911854Sbrandon.potter@amd.com    std::string full = cwd;
66011854Sbrandon.potter@amd.com
66111854Sbrandon.potter@amd.com    if (cwd[cwd.size() - 1] != '/')
66211854Sbrandon.potter@amd.com        full += '/';
66311854Sbrandon.potter@amd.com
66411854Sbrandon.potter@amd.com    return full + file_name;
66511854Sbrandon.potter@amd.com}
666