process.cc revision 11905
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>
5310930Sbrandon.potter@amd.com#include <map>
542SN/A#include <string>
5511800Sbrandon.potter@amd.com#include <vector>
562SN/A
5711793Sbrandon.potter@amd.com#include "base/intmath.hh"
5856SN/A#include "base/loader/object_file.hh"
591158SN/A#include "base/loader/symtab.hh"
60146SN/A#include "base/statistics.hh"
616658Snate@binkert.org#include "config/the_isa.hh"
622680Sktlim@umich.edu#include "cpu/thread_context.hh"
632378SN/A#include "mem/page_table.hh"
648706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
655154Sgblack@eecs.umich.edu#include "params/Process.hh"
6611800Sbrandon.potter@amd.com#include "sim/emul_driver.hh"
6711856Sbrandon.potter@amd.com#include "sim/fd_array.hh"
6811856Sbrandon.potter@amd.com#include "sim/fd_entry.hh"
6911794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
702378SN/A#include "sim/system.hh"
712SN/A
722715Sstever@eecs.umich.edu#if THE_ISA == ALPHA_ISA
732715Sstever@eecs.umich.edu#include "arch/alpha/linux/process.hh"
742715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
752715Sstever@eecs.umich.edu#include "arch/sparc/linux/process.hh"
762715Sstever@eecs.umich.edu#include "arch/sparc/solaris/process.hh"
772715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
782715Sstever@eecs.umich.edu#include "arch/mips/linux/process.hh"
795335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA
805335Shines@cs.fsu.edu#include "arch/arm/linux/process.hh"
8110810Sbr@bsdpad.com#include "arch/arm/freebsd/process.hh"
824157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
834166Sgblack@eecs.umich.edu#include "arch/x86/linux/process.hh"
846691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA
856691Stjones1@inf.ed.ac.uk#include "arch/power/linux/process.hh"
8611723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA
8711723Sar4jc@virginia.edu#include "arch/riscv/linux/process.hh"
882715Sstever@eecs.umich.edu#else
892715Sstever@eecs.umich.edu#error "THE_ISA not set"
902715Sstever@eecs.umich.edu#endif
912715Sstever@eecs.umich.edu
922715Sstever@eecs.umich.edu
932SN/Ausing namespace std;
942107SN/Ausing namespace TheISA;
952SN/A
9611851Sbrandon.potter@amd.comProcess::Process(ProcessParams * params, ObjectFile * obj_file)
977532Ssteve.reinhardt@amd.com    : SimObject(params), system(params->system),
9810299Salexandru.dutu@amd.com      useArchPT(params->useArchPT),
9910554Salexandru.dutu@amd.com      kvmInSE(params->kvmInSE),
10010299Salexandru.dutu@amd.com      pTable(useArchPT ?
10111813Sbaz21@cam.ac.uk        static_cast<PageTableBase *>(new ArchPageTable(name(), params->pid,
10211813Sbaz21@cam.ac.uk            system)) :
10311813Sbaz21@cam.ac.uk        static_cast<PageTableBase *>(new FuncPageTable(name(), params->pid))),
1048852Sandreas.hansson@arm.com      initVirtMem(system->getSystemPort(), this,
10510929Sbrandon.potter@amd.com                  SETranslatingPortProxy::Always),
10611851Sbrandon.potter@amd.com      objFile(obj_file),
10711851Sbrandon.potter@amd.com      argv(params->cmd), envp(params->env), cwd(params->cwd),
10811851Sbrandon.potter@amd.com      executable(params->executable),
10911801Sbrandon.potter@amd.com      _uid(params->uid), _euid(params->euid),
11011801Sbrandon.potter@amd.com      _gid(params->gid), _egid(params->egid),
11111851Sbrandon.potter@amd.com      _pid(params->pid), _ppid(params->ppid),
11211885Sbrandon.potter@amd.com      _pgid(params->pgid), drivers(params->drivers),
11311886Sbrandon.potter@amd.com      fds(make_shared<FDArray>(params->input, params->output, params->errout)),
11411886Sbrandon.potter@amd.com      childClearTID(0)
1152SN/A{
11611885Sbrandon.potter@amd.com    if (_pid >= System::maxPID)
11711885Sbrandon.potter@amd.com        fatal("_pid is too large: %d", _pid);
11811885Sbrandon.potter@amd.com
11911885Sbrandon.potter@amd.com    auto ret_pair = system->PIDs.emplace(_pid);
12011885Sbrandon.potter@amd.com    if (!ret_pair.second)
12111885Sbrandon.potter@amd.com        fatal("_pid %d is already used", _pid);
12211885Sbrandon.potter@amd.com
12311886Sbrandon.potter@amd.com    /**
12411886Sbrandon.potter@amd.com     * Linux bundles together processes into this concept called a thread
12511886Sbrandon.potter@amd.com     * group. The thread group is responsible for recording which processes
12611886Sbrandon.potter@amd.com     * behave as threads within a process context. The thread group leader
12711886Sbrandon.potter@amd.com     * is the process who's tgid is equal to its pid. Other processes which
12811886Sbrandon.potter@amd.com     * belong to the thread group, but do not lead the thread group, are
12911886Sbrandon.potter@amd.com     * treated as child threads. These threads are created by the clone system
13011886Sbrandon.potter@amd.com     * call with options specified to create threads (differing from the
13111886Sbrandon.potter@amd.com     * options used to implement a fork). By default, set up the tgid/pid
13211886Sbrandon.potter@amd.com     * with a new, equivalent value. If CLONE_THREAD is specified, patch
13311886Sbrandon.potter@amd.com     * the tgid value with the old process' value.
13411886Sbrandon.potter@amd.com     */
13511886Sbrandon.potter@amd.com    _tgid = params->pid;
13611886Sbrandon.potter@amd.com
13711886Sbrandon.potter@amd.com    exitGroup = new bool();
13811886Sbrandon.potter@amd.com    sigchld = new bool();
13911886Sbrandon.potter@amd.com
14011851Sbrandon.potter@amd.com    if (!debugSymbolTable) {
14111851Sbrandon.potter@amd.com        debugSymbolTable = new SymbolTable();
14211851Sbrandon.potter@amd.com        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
14311851Sbrandon.potter@amd.com            !objFile->loadLocalSymbols(debugSymbolTable) ||
14411851Sbrandon.potter@amd.com            !objFile->loadWeakSymbols(debugSymbolTable)) {
14511851Sbrandon.potter@amd.com            delete debugSymbolTable;
14611851Sbrandon.potter@amd.com            debugSymbolTable = NULL;
14711851Sbrandon.potter@amd.com        }
14811851Sbrandon.potter@amd.com    }
1492SN/A}
1502SN/A
1512SN/Avoid
15211886Sbrandon.potter@amd.comProcess::clone(ThreadContext *otc, ThreadContext *ntc,
15311886Sbrandon.potter@amd.com               Process *np, TheISA::IntReg flags)
15411886Sbrandon.potter@amd.com{
15511886Sbrandon.potter@amd.com    if (CLONE_VM & flags) {
15611886Sbrandon.potter@amd.com        /**
15711886Sbrandon.potter@amd.com         * Share the process memory address space between the new process
15811886Sbrandon.potter@amd.com         * and the old process. Changes in one will be visible in the other
15911886Sbrandon.potter@amd.com         * due to the pointer use.
16011886Sbrandon.potter@amd.com         */
16111886Sbrandon.potter@amd.com        delete np->pTable;
16211886Sbrandon.potter@amd.com        np->pTable = pTable;
16311886Sbrandon.potter@amd.com        ntc->getMemProxy().setPageTable(np->pTable);
16411886Sbrandon.potter@amd.com
16511886Sbrandon.potter@amd.com        np->memState = memState;
16611886Sbrandon.potter@amd.com    } else {
16711886Sbrandon.potter@amd.com        /**
16811886Sbrandon.potter@amd.com         * Duplicate the process memory address space. The state needs to be
16911886Sbrandon.potter@amd.com         * copied over (rather than using pointers to share everything).
17011886Sbrandon.potter@amd.com         */
17111886Sbrandon.potter@amd.com        typedef std::vector<pair<Addr,Addr>> MapVec;
17211886Sbrandon.potter@amd.com        MapVec mappings;
17311886Sbrandon.potter@amd.com        pTable->getMappings(&mappings);
17411886Sbrandon.potter@amd.com
17511886Sbrandon.potter@amd.com        for (auto map : mappings) {
17611886Sbrandon.potter@amd.com            Addr paddr, vaddr = map.first;
17711886Sbrandon.potter@amd.com            bool alloc_page = !(np->pTable->translate(vaddr, paddr));
17811886Sbrandon.potter@amd.com            np->replicatePage(vaddr, paddr, otc, ntc, alloc_page);
17911886Sbrandon.potter@amd.com        }
18011886Sbrandon.potter@amd.com
18111886Sbrandon.potter@amd.com        *np->memState = *memState;
18211886Sbrandon.potter@amd.com    }
18311886Sbrandon.potter@amd.com
18411886Sbrandon.potter@amd.com    if (CLONE_FILES & flags) {
18511886Sbrandon.potter@amd.com        /**
18611886Sbrandon.potter@amd.com         * The parent and child file descriptors are shared because the
18711886Sbrandon.potter@amd.com         * two FDArray pointers are pointing to the same FDArray. Opening
18811886Sbrandon.potter@amd.com         * and closing file descriptors will be visible to both processes.
18911886Sbrandon.potter@amd.com         */
19011886Sbrandon.potter@amd.com        np->fds = fds;
19111886Sbrandon.potter@amd.com    } else {
19211886Sbrandon.potter@amd.com        /**
19311886Sbrandon.potter@amd.com         * Copy the file descriptors from the old process into the new
19411886Sbrandon.potter@amd.com         * child process. The file descriptors entry can be opened and
19511886Sbrandon.potter@amd.com         * closed independently of the other process being considered. The
19611886Sbrandon.potter@amd.com         * host file descriptors are also dup'd so that the flags for the
19711886Sbrandon.potter@amd.com         * host file descriptor is independent of the other process.
19811886Sbrandon.potter@amd.com         */
19911886Sbrandon.potter@amd.com        for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) {
20011886Sbrandon.potter@amd.com            std::shared_ptr<FDArray> nfds = np->fds;
20111886Sbrandon.potter@amd.com            std::shared_ptr<FDEntry> this_fde = (*fds)[tgt_fd];
20211886Sbrandon.potter@amd.com            if (!this_fde) {
20311886Sbrandon.potter@amd.com                nfds->setFDEntry(tgt_fd, nullptr);
20411886Sbrandon.potter@amd.com                continue;
20511886Sbrandon.potter@amd.com            }
20611886Sbrandon.potter@amd.com            nfds->setFDEntry(tgt_fd, this_fde->clone());
20711886Sbrandon.potter@amd.com
20811886Sbrandon.potter@amd.com            auto this_hbfd = std::dynamic_pointer_cast<HBFDEntry>(this_fde);
20911886Sbrandon.potter@amd.com            if (!this_hbfd)
21011886Sbrandon.potter@amd.com                continue;
21111886Sbrandon.potter@amd.com
21211886Sbrandon.potter@amd.com            int this_sim_fd = this_hbfd->getSimFD();
21311886Sbrandon.potter@amd.com            if (this_sim_fd <= 2)
21411886Sbrandon.potter@amd.com                continue;
21511886Sbrandon.potter@amd.com
21611886Sbrandon.potter@amd.com            int np_sim_fd = dup(this_sim_fd);
21711886Sbrandon.potter@amd.com            assert(np_sim_fd != -1);
21811886Sbrandon.potter@amd.com
21911886Sbrandon.potter@amd.com            auto nhbfd = std::dynamic_pointer_cast<HBFDEntry>((*nfds)[tgt_fd]);
22011886Sbrandon.potter@amd.com            nhbfd->setSimFD(np_sim_fd);
22111886Sbrandon.potter@amd.com        }
22211886Sbrandon.potter@amd.com    }
22311886Sbrandon.potter@amd.com
22411886Sbrandon.potter@amd.com    if (CLONE_THREAD & flags) {
22511886Sbrandon.potter@amd.com        np->_tgid = _tgid;
22611886Sbrandon.potter@amd.com        delete np->exitGroup;
22711886Sbrandon.potter@amd.com        np->exitGroup = exitGroup;
22811886Sbrandon.potter@amd.com    }
22911886Sbrandon.potter@amd.com
23011886Sbrandon.potter@amd.com    np->argv.insert(np->argv.end(), argv.begin(), argv.end());
23111886Sbrandon.potter@amd.com    np->envp.insert(np->envp.end(), envp.begin(), envp.end());
23211886Sbrandon.potter@amd.com}
23311886Sbrandon.potter@amd.com
23411886Sbrandon.potter@amd.comvoid
2352SN/AProcess::regStats()
2362SN/A{
23711522Sstephan.diestelhorst@arm.com    SimObject::regStats();
23811522Sstephan.diestelhorst@arm.com
239729SN/A    using namespace Stats;
2402SN/A
24111886Sbrandon.potter@amd.com    numSyscalls
24211886Sbrandon.potter@amd.com        .name(name() + ".numSyscalls")
2432SN/A        .desc("Number of system calls")
2442SN/A        ;
2452SN/A}
2462SN/A
2475713Shsul@eecs.umich.eduThreadContext *
2485713Shsul@eecs.umich.eduProcess::findFreeContext()
2492SN/A{
25011886Sbrandon.potter@amd.com    for (auto &it : system->threadContexts) {
25111886Sbrandon.potter@amd.com        if (ThreadContext::Halted == it->status())
25211886Sbrandon.potter@amd.com            return it;
2535512SMichael.Adler@intel.com    }
2545713Shsul@eecs.umich.edu    return NULL;
2552SN/A}
2562SN/A
2571395SN/Avoid
25811886Sbrandon.potter@amd.comProcess::revokeThreadContext(int context_id)
25911886Sbrandon.potter@amd.com{
26011886Sbrandon.potter@amd.com    std::vector<ContextID>::iterator it;
26111886Sbrandon.potter@amd.com    for (it = contextIds.begin(); it != contextIds.end(); it++) {
26211886Sbrandon.potter@amd.com        if (*it == context_id) {
26311886Sbrandon.potter@amd.com            contextIds.erase(it);
26411886Sbrandon.potter@amd.com            return;
26511886Sbrandon.potter@amd.com        }
26611886Sbrandon.potter@amd.com    }
26711886Sbrandon.potter@amd.com    warn("Unable to find thread context to revoke");
26811886Sbrandon.potter@amd.com}
26911886Sbrandon.potter@amd.com
27011886Sbrandon.potter@amd.comvoid
2717532Ssteve.reinhardt@amd.comProcess::initState()
2721395SN/A{
2735713Shsul@eecs.umich.edu    if (contextIds.empty())
2745713Shsul@eecs.umich.edu        fatal("Process %s is not associated with any HW contexts!\n", name());
2752378SN/A
2762680Sktlim@umich.edu    // first thread context for this process... initialize & enable
2775713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
2781395SN/A
2791634SN/A    // mark this context as active so it will start ticking.
28010407Smitch.hayenga@arm.com    tc->activate();
28110298Salexandru.dutu@amd.com
28210298Salexandru.dutu@amd.com    pTable->initState(tc);
2831395SN/A}
2842SN/A
28510913Sandreas.sandberg@arm.comDrainState
28610913Sandreas.sandberg@arm.comProcess::drain()
28710905Sandreas.sandberg@arm.com{
28811856Sbrandon.potter@amd.com    fds->updateFileOffsets();
28910913Sandreas.sandberg@arm.com    return DrainState::Drained;
29010905Sandreas.sandberg@arm.com}
29110905Sandreas.sandberg@arm.com
2928601Ssteve.reinhardt@amd.comvoid
2938601Ssteve.reinhardt@amd.comProcess::allocateMem(Addr vaddr, int64_t size, bool clobber)
2948601Ssteve.reinhardt@amd.com{
29510318Sandreas.hansson@arm.com    int npages = divCeil(size, (int64_t)PageBytes);
2968601Ssteve.reinhardt@amd.com    Addr paddr = system->allocPhysPages(npages);
29711294Sandreas.hansson@arm.com    pTable->map(vaddr, paddr, size,
29811294Sandreas.hansson@arm.com                clobber ? PageTableBase::Clobber : PageTableBase::Zero);
2998601Ssteve.reinhardt@amd.com}
3008601Ssteve.reinhardt@amd.com
30111886Sbrandon.potter@amd.comvoid
30211886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
30311886Sbrandon.potter@amd.com                       ThreadContext *new_tc, bool allocate_page)
30411886Sbrandon.potter@amd.com{
30511886Sbrandon.potter@amd.com    if (allocate_page)
30611886Sbrandon.potter@amd.com        new_paddr = system->allocPhysPages(1);
30711886Sbrandon.potter@amd.com
30811886Sbrandon.potter@amd.com    // Read from old physical page.
30911886Sbrandon.potter@amd.com    uint8_t *buf_p = new uint8_t[PageBytes];
31011886Sbrandon.potter@amd.com    old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes);
31111886Sbrandon.potter@amd.com
31211886Sbrandon.potter@amd.com    // Create new mapping in process address space by clobbering existing
31311886Sbrandon.potter@amd.com    // mapping (if any existed) and then write to the new physical page.
31411886Sbrandon.potter@amd.com    bool clobber = true;
31511886Sbrandon.potter@amd.com    pTable->map(vaddr, new_paddr, PageBytes, clobber);
31611886Sbrandon.potter@amd.com    new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes);
31711886Sbrandon.potter@amd.com    delete[] buf_p;
31811886Sbrandon.potter@amd.com}
31911886Sbrandon.potter@amd.com
3204434Ssaidi@eecs.umich.edubool
3218539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr)
3224434Ssaidi@eecs.umich.edu{
32311905SBrandon.Potter@amd.com    Addr stack_min = memState->getStackMin();
32411905SBrandon.Potter@amd.com    Addr stack_base = memState->getStackBase();
32511905SBrandon.Potter@amd.com    Addr max_stack_size = memState->getMaxStackSize();
32611905SBrandon.Potter@amd.com
3278539Sgblack@eecs.umich.edu    // Check if this is already on the stack and there's just no page there
3288539Sgblack@eecs.umich.edu    // yet.
32911905SBrandon.Potter@amd.com    if (vaddr >= stack_min && vaddr < stack_base) {
33010318Sandreas.hansson@arm.com        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
3314434Ssaidi@eecs.umich.edu        return true;
3324434Ssaidi@eecs.umich.edu    }
3334434Ssaidi@eecs.umich.edu
3348539Sgblack@eecs.umich.edu    // We've accessed the next page of the stack, so extend it to include
3358539Sgblack@eecs.umich.edu    // this address.
33611905SBrandon.Potter@amd.com    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
33711905SBrandon.Potter@amd.com        while (vaddr < stack_min) {
33811905SBrandon.Potter@amd.com            stack_min -= TheISA::PageBytes;
33911905SBrandon.Potter@amd.com            if (stack_base - stack_min > max_stack_size)
3405154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
34111905SBrandon.Potter@amd.com            allocateMem(stack_min, TheISA::PageBytes);
3425823Ssaidi@eecs.umich.edu            inform("Increasing stack size by one page.");
3435154Sgblack@eecs.umich.edu        };
34411905SBrandon.Potter@amd.com        memState->setStackMin(stack_min);
3454434Ssaidi@eecs.umich.edu        return true;
3464434Ssaidi@eecs.umich.edu    }
3474434Ssaidi@eecs.umich.edu    return false;
3484434Ssaidi@eecs.umich.edu}
3494434Ssaidi@eecs.umich.edu
3505282Srstrong@cs.ucsd.eduvoid
35110905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const
3523311Ssaidi@eecs.umich.edu{
35310905Sandreas.sandberg@arm.com    pTable->serialize(cp);
35411856Sbrandon.potter@amd.com    /**
35511856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
35611856Sbrandon.potter@amd.com     * come back and fix them at a later date.
35711856Sbrandon.potter@amd.com     */
35811856Sbrandon.potter@amd.com
35911856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
36011856Sbrandon.potter@amd.com#if 0
36111856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
36211856Sbrandon.potter@amd.com        (*fds)[x].serializeSection(cp, csprintf("FDEntry%d", x));
36311856Sbrandon.potter@amd.com#endif
3643311Ssaidi@eecs.umich.edu
3653311Ssaidi@eecs.umich.edu}
3663311Ssaidi@eecs.umich.edu
3673311Ssaidi@eecs.umich.eduvoid
36810905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp)
3693311Ssaidi@eecs.umich.edu{
37010905Sandreas.sandberg@arm.com    pTable->unserialize(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    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]->unserializeSection(cp, csprintf("FDEntry%d", x));
37911856Sbrandon.potter@amd.com    fds->restoreFileOffsets();
38011856Sbrandon.potter@amd.com#endif
3816820SLisa.Hsu@amd.com    // The above returns a bool so that you could do something if you don't
3826820SLisa.Hsu@amd.com    // find the param in the checkpoint if you wanted to, like set a default
38310930Sbrandon.potter@amd.com    // but in this case we'll just stick with the instantiated value if not
38410929Sbrandon.potter@amd.com    // found.
3853311Ssaidi@eecs.umich.edu}
3862SN/A
3879110Ssteve.reinhardt@amd.combool
38810558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable)
3899110Ssteve.reinhardt@amd.com{
39010558Salexandru.dutu@amd.com    pTable->map(vaddr, paddr, size,
39111294Sandreas.hansson@arm.com                cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable);
3929110Ssteve.reinhardt@amd.com    return true;
3939110Ssteve.reinhardt@amd.com}
3949110Ssteve.reinhardt@amd.com
3952378SN/Avoid
39611877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
3972093SN/A{
39811886Sbrandon.potter@amd.com    numSyscalls++;
3992093SN/A
4002093SN/A    SyscallDesc *desc = getDesc(callnum);
4012093SN/A    if (desc == NULL)
4022093SN/A        fatal("Syscall %d out of range", callnum);
4032093SN/A
40411877Sbrandon.potter@amd.com    desc->doSyscall(callnum, this, tc, fault);
4052093SN/A}
4062SN/A
4076701Sgblack@eecs.umich.eduIntReg
40811851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
4096701Sgblack@eecs.umich.edu{
4106701Sgblack@eecs.umich.edu    return getSyscallArg(tc, i);
4116701Sgblack@eecs.umich.edu}
4126701Sgblack@eecs.umich.edu
41310496Ssteve.reinhardt@amd.comEmulatedDriver *
41411851Sbrandon.potter@amd.comProcess::findDriver(std::string filename)
41510496Ssteve.reinhardt@amd.com{
41610496Ssteve.reinhardt@amd.com    for (EmulatedDriver *d : drivers) {
41710496Ssteve.reinhardt@amd.com        if (d->match(filename))
41810496Ssteve.reinhardt@amd.com            return d;
41910496Ssteve.reinhardt@amd.com    }
42010496Ssteve.reinhardt@amd.com
42110496Ssteve.reinhardt@amd.com    return NULL;
42210496Ssteve.reinhardt@amd.com}
42310496Ssteve.reinhardt@amd.com
42411389Sbrandon.potter@amd.comvoid
42511851Sbrandon.potter@amd.comProcess::updateBias()
42611389Sbrandon.potter@amd.com{
42711389Sbrandon.potter@amd.com    ObjectFile *interp = objFile->getInterpreter();
42811389Sbrandon.potter@amd.com
42911389Sbrandon.potter@amd.com    if (!interp || !interp->relocatable())
43011389Sbrandon.potter@amd.com        return;
43111389Sbrandon.potter@amd.com
43211389Sbrandon.potter@amd.com    // Determine how large the interpreters footprint will be in the process
43311389Sbrandon.potter@amd.com    // address space.
43411389Sbrandon.potter@amd.com    Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
43511389Sbrandon.potter@amd.com
43611389Sbrandon.potter@amd.com    // We are allocating the memory area; set the bias to the lowest address
43711389Sbrandon.potter@amd.com    // in the allocated memory region.
43811905SBrandon.Potter@amd.com    Addr mmap_end = memState->getMmapEnd();
43911905SBrandon.Potter@amd.com    Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
44011389Sbrandon.potter@amd.com
44111389Sbrandon.potter@amd.com    // Adjust the process mmap area to give the interpreter room; the real
44211389Sbrandon.potter@amd.com    // execve system call would just invoke the kernel's internal mmap
44311389Sbrandon.potter@amd.com    // functions to make these adjustments.
44411905SBrandon.Potter@amd.com    mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
44511905SBrandon.Potter@amd.com    memState->setMmapEnd(mmap_end);
44611389Sbrandon.potter@amd.com
44711389Sbrandon.potter@amd.com    interp->updateBias(ld_bias);
44811389Sbrandon.potter@amd.com}
44911389Sbrandon.potter@amd.com
45011392Sbrandon.potter@amd.comObjectFile *
45111851Sbrandon.potter@amd.comProcess::getInterpreter()
45211392Sbrandon.potter@amd.com{
45311392Sbrandon.potter@amd.com    return objFile->getInterpreter();
45411392Sbrandon.potter@amd.com}
45511392Sbrandon.potter@amd.com
45611389Sbrandon.potter@amd.comAddr
45711851Sbrandon.potter@amd.comProcess::getBias()
45811389Sbrandon.potter@amd.com{
45911392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
46011389Sbrandon.potter@amd.com
46111389Sbrandon.potter@amd.com    return interp ? interp->bias() : objFile->bias();
46211389Sbrandon.potter@amd.com}
46311389Sbrandon.potter@amd.com
46411389Sbrandon.potter@amd.comAddr
46511851Sbrandon.potter@amd.comProcess::getStartPC()
46611389Sbrandon.potter@amd.com{
46711392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
46811389Sbrandon.potter@amd.com
46911389Sbrandon.potter@amd.com    return interp ? interp->entryPoint() : objFile->entryPoint();
47011389Sbrandon.potter@amd.com}
47111389Sbrandon.potter@amd.com
47211851Sbrandon.potter@amd.comProcess *
47311851Sbrandon.potter@amd.comProcessParams::create()
4742715Sstever@eecs.umich.edu{
47511851Sbrandon.potter@amd.com    Process *process = NULL;
4762715Sstever@eecs.umich.edu
47711140Sjthestness@gmail.com    // If not specified, set the executable parameter equal to the
47811140Sjthestness@gmail.com    // simulated system's zeroth command line parameter
47911851Sbrandon.potter@amd.com    if (executable == "") {
48011851Sbrandon.potter@amd.com        executable = cmd[0];
48111140Sjthestness@gmail.com    }
48211140Sjthestness@gmail.com
48311851Sbrandon.potter@amd.com    ObjectFile *obj_file = createObjectFile(executable);
48411851Sbrandon.potter@amd.com    if (obj_file == NULL) {
48511851Sbrandon.potter@amd.com        fatal("Can't load object file %s", executable);
4862715Sstever@eecs.umich.edu    }
4872715Sstever@eecs.umich.edu
4885089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA
48911851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Alpha)
4905753Ssteve.reinhardt@amd.com        fatal("Object file architecture does not match compiled ISA (Alpha).");
4915753Ssteve.reinhardt@amd.com
49211851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
4935753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
4945753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
4955753Ssteve.reinhardt@amd.com        // fall through
4962715Sstever@eecs.umich.edu      case ObjectFile::Linux:
49711851Sbrandon.potter@amd.com        process = new AlphaLinuxProcess(this, obj_file);
4982715Sstever@eecs.umich.edu        break;
4992715Sstever@eecs.umich.edu
5002715Sstever@eecs.umich.edu      default:
5012715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5022715Sstever@eecs.umich.edu    }
5032715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
50411851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::SPARC64 &&
50511851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::SPARC32)
5062715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (SPARC).");
50711851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5085753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5095753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5105753Ssteve.reinhardt@amd.com        // fall through
5112715Sstever@eecs.umich.edu      case ObjectFile::Linux:
51211851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::SPARC64) {
51311851Sbrandon.potter@amd.com            process = new Sparc64LinuxProcess(this, obj_file);
5144111Sgblack@eecs.umich.edu        } else {
51511851Sbrandon.potter@amd.com            process = new Sparc32LinuxProcess(this, obj_file);
5164111Sgblack@eecs.umich.edu        }
5172715Sstever@eecs.umich.edu        break;
5182715Sstever@eecs.umich.edu
5192715Sstever@eecs.umich.edu      case ObjectFile::Solaris:
52011851Sbrandon.potter@amd.com        process = new SparcSolarisProcess(this, obj_file);
5212715Sstever@eecs.umich.edu        break;
5225753Ssteve.reinhardt@amd.com
5232715Sstever@eecs.umich.edu      default:
5242715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5252715Sstever@eecs.umich.edu    }
5264157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
52711851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::X86_64 &&
52811851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::I386)
5294166Sgblack@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
53011851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5315753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5325753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5335753Ssteve.reinhardt@amd.com        // fall through
5344166Sgblack@eecs.umich.edu      case ObjectFile::Linux:
53511851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::X86_64) {
53611851Sbrandon.potter@amd.com            process = new X86_64LinuxProcess(this, obj_file);
5375874Sgblack@eecs.umich.edu        } else {
53811851Sbrandon.potter@amd.com            process = new I386LinuxProcess(this, obj_file);
5395874Sgblack@eecs.umich.edu        }
5404166Sgblack@eecs.umich.edu        break;
5415753Ssteve.reinhardt@amd.com
5424157Sgblack@eecs.umich.edu      default:
5434157Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5444157Sgblack@eecs.umich.edu    }
5452715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
54611851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Mips)
5472715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (MIPS).");
54811851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5495753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5505753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5515753Ssteve.reinhardt@amd.com        // fall through
5522715Sstever@eecs.umich.edu      case ObjectFile::Linux:
55311851Sbrandon.potter@amd.com        process = new MipsLinuxProcess(this, obj_file);
5542715Sstever@eecs.umich.edu        break;
5552715Sstever@eecs.umich.edu
5562715Sstever@eecs.umich.edu      default:
5572715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5582715Sstever@eecs.umich.edu    }
5595335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA
56011851Sbrandon.potter@amd.com    ObjectFile::Arch arch = obj_file->getArch();
56110037SARM gem5 Developers    if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
56210037SARM gem5 Developers        arch != ObjectFile::Arm64)
5635335Shines@cs.fsu.edu        fatal("Object file architecture does not match compiled ISA (ARM).");
56411851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5655753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5665753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5675753Ssteve.reinhardt@amd.com        // fall through
5685335Shines@cs.fsu.edu      case ObjectFile::Linux:
56910037SARM gem5 Developers        if (arch == ObjectFile::Arm64) {
57011851Sbrandon.potter@amd.com            process = new ArmLinuxProcess64(this, obj_file,
57111851Sbrandon.potter@amd.com                                            obj_file->getArch());
57210037SARM gem5 Developers        } else {
57311851Sbrandon.potter@amd.com            process = new ArmLinuxProcess32(this, obj_file,
57411851Sbrandon.potter@amd.com                                            obj_file->getArch());
57510037SARM gem5 Developers        }
5765335Shines@cs.fsu.edu        break;
57710810Sbr@bsdpad.com      case ObjectFile::FreeBSD:
57810810Sbr@bsdpad.com        if (arch == ObjectFile::Arm64) {
57911851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess64(this, obj_file,
58011851Sbrandon.potter@amd.com                                              obj_file->getArch());
58110810Sbr@bsdpad.com        } else {
58211851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess32(this, obj_file,
58311851Sbrandon.potter@amd.com                                              obj_file->getArch());
58410810Sbr@bsdpad.com        }
58510810Sbr@bsdpad.com        break;
5866392Ssaidi@eecs.umich.edu      case ObjectFile::LinuxArmOABI:
5876392Ssaidi@eecs.umich.edu        fatal("M5 does not support ARM OABI binaries. Please recompile with an"
5886392Ssaidi@eecs.umich.edu              " EABI compiler.");
5895335Shines@cs.fsu.edu      default:
5905335Shines@cs.fsu.edu        fatal("Unknown/unsupported operating system.");
5915335Shines@cs.fsu.edu    }
5926691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA
59311851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Power)
5946691Stjones1@inf.ed.ac.uk        fatal("Object file architecture does not match compiled ISA (Power).");
59511851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5966691Stjones1@inf.ed.ac.uk      case ObjectFile::UnknownOpSys:
5976691Stjones1@inf.ed.ac.uk        warn("Unknown operating system; assuming Linux.");
5986691Stjones1@inf.ed.ac.uk        // fall through
5996691Stjones1@inf.ed.ac.uk      case ObjectFile::Linux:
60011851Sbrandon.potter@amd.com        process = new PowerLinuxProcess(this, obj_file);
6016691Stjones1@inf.ed.ac.uk        break;
6026691Stjones1@inf.ed.ac.uk
6036691Stjones1@inf.ed.ac.uk      default:
6046691Stjones1@inf.ed.ac.uk        fatal("Unknown/unsupported operating system.");
6056691Stjones1@inf.ed.ac.uk    }
60611723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA
60711851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Riscv)
60811723Sar4jc@virginia.edu        fatal("Object file architecture does not match compiled ISA (RISCV).");
60911851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
61011723Sar4jc@virginia.edu      case ObjectFile::UnknownOpSys:
61111723Sar4jc@virginia.edu        warn("Unknown operating system; assuming Linux.");
61211723Sar4jc@virginia.edu        // fall through
61311723Sar4jc@virginia.edu      case ObjectFile::Linux:
61411851Sbrandon.potter@amd.com        process = new RiscvLinuxProcess(this, obj_file);
61511723Sar4jc@virginia.edu        break;
61611723Sar4jc@virginia.edu      default:
61711723Sar4jc@virginia.edu        fatal("Unknown/unsupported operating system.");
61811723Sar4jc@virginia.edu    }
6192715Sstever@eecs.umich.edu#else
6202715Sstever@eecs.umich.edu#error "THE_ISA not set"
6212715Sstever@eecs.umich.edu#endif
6222715Sstever@eecs.umich.edu
6232715Sstever@eecs.umich.edu    if (process == NULL)
6242715Sstever@eecs.umich.edu        fatal("Unknown error creating process object.");
6252715Sstever@eecs.umich.edu    return process;
6262715Sstever@eecs.umich.edu}
62711854Sbrandon.potter@amd.com
62811854Sbrandon.potter@amd.comstd::string
62911854Sbrandon.potter@amd.comProcess::fullPath(const std::string &file_name)
63011854Sbrandon.potter@amd.com{
63111854Sbrandon.potter@amd.com    if (file_name[0] == '/' || cwd.empty())
63211854Sbrandon.potter@amd.com        return file_name;
63311854Sbrandon.potter@amd.com
63411854Sbrandon.potter@amd.com    std::string full = cwd;
63511854Sbrandon.potter@amd.com
63611854Sbrandon.potter@amd.com    if (cwd[cwd.size() - 1] != '/')
63711854Sbrandon.potter@amd.com        full += '/';
63811854Sbrandon.potter@amd.com
63911854Sbrandon.potter@amd.com    return full + file_name;
64011854Sbrandon.potter@amd.com}
641