process.cc revision 11886
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      maxStackSize(params->maxStackSize),
11511886Sbrandon.potter@amd.com      childClearTID(0)
1162SN/A{
11711885Sbrandon.potter@amd.com    if (_pid >= System::maxPID)
11811885Sbrandon.potter@amd.com        fatal("_pid is too large: %d", _pid);
11911885Sbrandon.potter@amd.com
12011885Sbrandon.potter@amd.com    auto ret_pair = system->PIDs.emplace(_pid);
12111885Sbrandon.potter@amd.com    if (!ret_pair.second)
12211885Sbrandon.potter@amd.com        fatal("_pid %d is already used", _pid);
12311885Sbrandon.potter@amd.com
12411886Sbrandon.potter@amd.com    /**
12511886Sbrandon.potter@amd.com     * Linux bundles together processes into this concept called a thread
12611886Sbrandon.potter@amd.com     * group. The thread group is responsible for recording which processes
12711886Sbrandon.potter@amd.com     * behave as threads within a process context. The thread group leader
12811886Sbrandon.potter@amd.com     * is the process who's tgid is equal to its pid. Other processes which
12911886Sbrandon.potter@amd.com     * belong to the thread group, but do not lead the thread group, are
13011886Sbrandon.potter@amd.com     * treated as child threads. These threads are created by the clone system
13111886Sbrandon.potter@amd.com     * call with options specified to create threads (differing from the
13211886Sbrandon.potter@amd.com     * options used to implement a fork). By default, set up the tgid/pid
13311886Sbrandon.potter@amd.com     * with a new, equivalent value. If CLONE_THREAD is specified, patch
13411886Sbrandon.potter@amd.com     * the tgid value with the old process' value.
13511886Sbrandon.potter@amd.com     */
13611886Sbrandon.potter@amd.com    _tgid = params->pid;
13711886Sbrandon.potter@amd.com
13811886Sbrandon.potter@amd.com    exitGroup = new bool();
13911886Sbrandon.potter@amd.com    memState = new MemState();
14011886Sbrandon.potter@amd.com    sigchld = new bool();
14111886Sbrandon.potter@amd.com
14211851Sbrandon.potter@amd.com    if (!debugSymbolTable) {
14311851Sbrandon.potter@amd.com        debugSymbolTable = new SymbolTable();
14411851Sbrandon.potter@amd.com        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
14511851Sbrandon.potter@amd.com            !objFile->loadLocalSymbols(debugSymbolTable) ||
14611851Sbrandon.potter@amd.com            !objFile->loadWeakSymbols(debugSymbolTable)) {
14711851Sbrandon.potter@amd.com            delete debugSymbolTable;
14811851Sbrandon.potter@amd.com            debugSymbolTable = NULL;
14911851Sbrandon.potter@amd.com        }
15011851Sbrandon.potter@amd.com    }
1512SN/A}
1522SN/A
1532SN/Avoid
15411886Sbrandon.potter@amd.comProcess::clone(ThreadContext *otc, ThreadContext *ntc,
15511886Sbrandon.potter@amd.com               Process *np, TheISA::IntReg flags)
15611886Sbrandon.potter@amd.com{
15711886Sbrandon.potter@amd.com    if (CLONE_VM & flags) {
15811886Sbrandon.potter@amd.com        /**
15911886Sbrandon.potter@amd.com         * Share the process memory address space between the new process
16011886Sbrandon.potter@amd.com         * and the old process. Changes in one will be visible in the other
16111886Sbrandon.potter@amd.com         * due to the pointer use.
16211886Sbrandon.potter@amd.com         */
16311886Sbrandon.potter@amd.com        delete np->pTable;
16411886Sbrandon.potter@amd.com        np->pTable = pTable;
16511886Sbrandon.potter@amd.com        ntc->getMemProxy().setPageTable(np->pTable);
16611886Sbrandon.potter@amd.com
16711886Sbrandon.potter@amd.com        delete np->memState;
16811886Sbrandon.potter@amd.com        np->memState = memState;
16911886Sbrandon.potter@amd.com    } else {
17011886Sbrandon.potter@amd.com        /**
17111886Sbrandon.potter@amd.com         * Duplicate the process memory address space. The state needs to be
17211886Sbrandon.potter@amd.com         * copied over (rather than using pointers to share everything).
17311886Sbrandon.potter@amd.com         */
17411886Sbrandon.potter@amd.com        typedef std::vector<pair<Addr,Addr>> MapVec;
17511886Sbrandon.potter@amd.com        MapVec mappings;
17611886Sbrandon.potter@amd.com        pTable->getMappings(&mappings);
17711886Sbrandon.potter@amd.com
17811886Sbrandon.potter@amd.com        for (auto map : mappings) {
17911886Sbrandon.potter@amd.com            Addr paddr, vaddr = map.first;
18011886Sbrandon.potter@amd.com            bool alloc_page = !(np->pTable->translate(vaddr, paddr));
18111886Sbrandon.potter@amd.com            np->replicatePage(vaddr, paddr, otc, ntc, alloc_page);
18211886Sbrandon.potter@amd.com        }
18311886Sbrandon.potter@amd.com
18411886Sbrandon.potter@amd.com        *np->memState = *memState;
18511886Sbrandon.potter@amd.com    }
18611886Sbrandon.potter@amd.com
18711886Sbrandon.potter@amd.com    if (CLONE_FILES & flags) {
18811886Sbrandon.potter@amd.com        /**
18911886Sbrandon.potter@amd.com         * The parent and child file descriptors are shared because the
19011886Sbrandon.potter@amd.com         * two FDArray pointers are pointing to the same FDArray. Opening
19111886Sbrandon.potter@amd.com         * and closing file descriptors will be visible to both processes.
19211886Sbrandon.potter@amd.com         */
19311886Sbrandon.potter@amd.com        np->fds = fds;
19411886Sbrandon.potter@amd.com    } else {
19511886Sbrandon.potter@amd.com        /**
19611886Sbrandon.potter@amd.com         * Copy the file descriptors from the old process into the new
19711886Sbrandon.potter@amd.com         * child process. The file descriptors entry can be opened and
19811886Sbrandon.potter@amd.com         * closed independently of the other process being considered. The
19911886Sbrandon.potter@amd.com         * host file descriptors are also dup'd so that the flags for the
20011886Sbrandon.potter@amd.com         * host file descriptor is independent of the other process.
20111886Sbrandon.potter@amd.com         */
20211886Sbrandon.potter@amd.com        for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) {
20311886Sbrandon.potter@amd.com            std::shared_ptr<FDArray> nfds = np->fds;
20411886Sbrandon.potter@amd.com            std::shared_ptr<FDEntry> this_fde = (*fds)[tgt_fd];
20511886Sbrandon.potter@amd.com            if (!this_fde) {
20611886Sbrandon.potter@amd.com                nfds->setFDEntry(tgt_fd, nullptr);
20711886Sbrandon.potter@amd.com                continue;
20811886Sbrandon.potter@amd.com            }
20911886Sbrandon.potter@amd.com            nfds->setFDEntry(tgt_fd, this_fde->clone());
21011886Sbrandon.potter@amd.com
21111886Sbrandon.potter@amd.com            auto this_hbfd = std::dynamic_pointer_cast<HBFDEntry>(this_fde);
21211886Sbrandon.potter@amd.com            if (!this_hbfd)
21311886Sbrandon.potter@amd.com                continue;
21411886Sbrandon.potter@amd.com
21511886Sbrandon.potter@amd.com            int this_sim_fd = this_hbfd->getSimFD();
21611886Sbrandon.potter@amd.com            if (this_sim_fd <= 2)
21711886Sbrandon.potter@amd.com                continue;
21811886Sbrandon.potter@amd.com
21911886Sbrandon.potter@amd.com            int np_sim_fd = dup(this_sim_fd);
22011886Sbrandon.potter@amd.com            assert(np_sim_fd != -1);
22111886Sbrandon.potter@amd.com
22211886Sbrandon.potter@amd.com            auto nhbfd = std::dynamic_pointer_cast<HBFDEntry>((*nfds)[tgt_fd]);
22311886Sbrandon.potter@amd.com            nhbfd->setSimFD(np_sim_fd);
22411886Sbrandon.potter@amd.com        }
22511886Sbrandon.potter@amd.com    }
22611886Sbrandon.potter@amd.com
22711886Sbrandon.potter@amd.com    if (CLONE_THREAD & flags) {
22811886Sbrandon.potter@amd.com        np->_tgid = _tgid;
22911886Sbrandon.potter@amd.com        delete np->exitGroup;
23011886Sbrandon.potter@amd.com        np->exitGroup = exitGroup;
23111886Sbrandon.potter@amd.com    }
23211886Sbrandon.potter@amd.com
23311886Sbrandon.potter@amd.com    np->argv.insert(np->argv.end(), argv.begin(), argv.end());
23411886Sbrandon.potter@amd.com    np->envp.insert(np->envp.end(), envp.begin(), envp.end());
23511886Sbrandon.potter@amd.com}
23611886Sbrandon.potter@amd.com
23711886Sbrandon.potter@amd.comvoid
2382SN/AProcess::regStats()
2392SN/A{
24011522Sstephan.diestelhorst@arm.com    SimObject::regStats();
24111522Sstephan.diestelhorst@arm.com
242729SN/A    using namespace Stats;
2432SN/A
24411886Sbrandon.potter@amd.com    numSyscalls
24511886Sbrandon.potter@amd.com        .name(name() + ".numSyscalls")
2462SN/A        .desc("Number of system calls")
2472SN/A        ;
2482SN/A}
2492SN/A
2505713Shsul@eecs.umich.eduThreadContext *
2515713Shsul@eecs.umich.eduProcess::findFreeContext()
2522SN/A{
25311886Sbrandon.potter@amd.com    for (auto &it : system->threadContexts) {
25411886Sbrandon.potter@amd.com        if (ThreadContext::Halted == it->status())
25511886Sbrandon.potter@amd.com            return it;
2565512SMichael.Adler@intel.com    }
2575713Shsul@eecs.umich.edu    return NULL;
2582SN/A}
2592SN/A
2601395SN/Avoid
26111886Sbrandon.potter@amd.comProcess::revokeThreadContext(int context_id)
26211886Sbrandon.potter@amd.com{
26311886Sbrandon.potter@amd.com    std::vector<ContextID>::iterator it;
26411886Sbrandon.potter@amd.com    for (it = contextIds.begin(); it != contextIds.end(); it++) {
26511886Sbrandon.potter@amd.com        if (*it == context_id) {
26611886Sbrandon.potter@amd.com            contextIds.erase(it);
26711886Sbrandon.potter@amd.com            return;
26811886Sbrandon.potter@amd.com        }
26911886Sbrandon.potter@amd.com    }
27011886Sbrandon.potter@amd.com    warn("Unable to find thread context to revoke");
27111886Sbrandon.potter@amd.com}
27211886Sbrandon.potter@amd.com
27311886Sbrandon.potter@amd.comvoid
2747532Ssteve.reinhardt@amd.comProcess::initState()
2751395SN/A{
2765713Shsul@eecs.umich.edu    if (contextIds.empty())
2775713Shsul@eecs.umich.edu        fatal("Process %s is not associated with any HW contexts!\n", name());
2782378SN/A
2792680Sktlim@umich.edu    // first thread context for this process... initialize & enable
2805713Shsul@eecs.umich.edu    ThreadContext *tc = system->getThreadContext(contextIds[0]);
2811395SN/A
2821634SN/A    // mark this context as active so it will start ticking.
28310407Smitch.hayenga@arm.com    tc->activate();
28410298Salexandru.dutu@amd.com
28510298Salexandru.dutu@amd.com    pTable->initState(tc);
2861395SN/A}
2872SN/A
28810913Sandreas.sandberg@arm.comDrainState
28910913Sandreas.sandberg@arm.comProcess::drain()
29010905Sandreas.sandberg@arm.com{
29111856Sbrandon.potter@amd.com    fds->updateFileOffsets();
29210913Sandreas.sandberg@arm.com    return DrainState::Drained;
29310905Sandreas.sandberg@arm.com}
29410905Sandreas.sandberg@arm.com
2958601Ssteve.reinhardt@amd.comvoid
2968601Ssteve.reinhardt@amd.comProcess::allocateMem(Addr vaddr, int64_t size, bool clobber)
2978601Ssteve.reinhardt@amd.com{
29810318Sandreas.hansson@arm.com    int npages = divCeil(size, (int64_t)PageBytes);
2998601Ssteve.reinhardt@amd.com    Addr paddr = system->allocPhysPages(npages);
30011294Sandreas.hansson@arm.com    pTable->map(vaddr, paddr, size,
30111294Sandreas.hansson@arm.com                clobber ? PageTableBase::Clobber : PageTableBase::Zero);
3028601Ssteve.reinhardt@amd.com}
3038601Ssteve.reinhardt@amd.com
30411886Sbrandon.potter@amd.comvoid
30511886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
30611886Sbrandon.potter@amd.com                       ThreadContext *new_tc, bool allocate_page)
30711886Sbrandon.potter@amd.com{
30811886Sbrandon.potter@amd.com    if (allocate_page)
30911886Sbrandon.potter@amd.com        new_paddr = system->allocPhysPages(1);
31011886Sbrandon.potter@amd.com
31111886Sbrandon.potter@amd.com    // Read from old physical page.
31211886Sbrandon.potter@amd.com    uint8_t *buf_p = new uint8_t[PageBytes];
31311886Sbrandon.potter@amd.com    old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes);
31411886Sbrandon.potter@amd.com
31511886Sbrandon.potter@amd.com    // Create new mapping in process address space by clobbering existing
31611886Sbrandon.potter@amd.com    // mapping (if any existed) and then write to the new physical page.
31711886Sbrandon.potter@amd.com    bool clobber = true;
31811886Sbrandon.potter@amd.com    pTable->map(vaddr, new_paddr, PageBytes, clobber);
31911886Sbrandon.potter@amd.com    new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes);
32011886Sbrandon.potter@amd.com    delete[] buf_p;
32111886Sbrandon.potter@amd.com}
32211886Sbrandon.potter@amd.com
3234434Ssaidi@eecs.umich.edubool
3248539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr)
3254434Ssaidi@eecs.umich.edu{
3268539Sgblack@eecs.umich.edu    // Check if this is already on the stack and there's just no page there
3278539Sgblack@eecs.umich.edu    // yet.
32811886Sbrandon.potter@amd.com    if (vaddr >= memState->stackMin && vaddr < memState->stackBase) {
32910318Sandreas.hansson@arm.com        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
3304434Ssaidi@eecs.umich.edu        return true;
3314434Ssaidi@eecs.umich.edu    }
3324434Ssaidi@eecs.umich.edu
3338539Sgblack@eecs.umich.edu    // We've accessed the next page of the stack, so extend it to include
3348539Sgblack@eecs.umich.edu    // this address.
33511886Sbrandon.potter@amd.com    if (vaddr < memState->stackMin
33611886Sbrandon.potter@amd.com        && vaddr >= memState->stackBase - maxStackSize) {
33711886Sbrandon.potter@amd.com        while (vaddr < memState->stackMin) {
33811886Sbrandon.potter@amd.com            memState->stackMin -= TheISA::PageBytes;
33911886Sbrandon.potter@amd.com            if (memState->stackBase - memState->stackMin > maxStackSize)
3405154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
34111886Sbrandon.potter@amd.com            allocateMem(memState->stackMin, TheISA::PageBytes);
3425823Ssaidi@eecs.umich.edu            inform("Increasing stack size by one page.");
3435154Sgblack@eecs.umich.edu        };
3444434Ssaidi@eecs.umich.edu        return true;
3454434Ssaidi@eecs.umich.edu    }
3464434Ssaidi@eecs.umich.edu    return false;
3474434Ssaidi@eecs.umich.edu}
3484434Ssaidi@eecs.umich.edu
3495282Srstrong@cs.ucsd.eduvoid
35010905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const
3513311Ssaidi@eecs.umich.edu{
35211886Sbrandon.potter@amd.com    SERIALIZE_SCALAR(memState->brkPoint);
35311886Sbrandon.potter@amd.com    SERIALIZE_SCALAR(memState->stackBase);
35411886Sbrandon.potter@amd.com    SERIALIZE_SCALAR(memState->stackSize);
35511886Sbrandon.potter@amd.com    SERIALIZE_SCALAR(memState->stackMin);
35611886Sbrandon.potter@amd.com    SERIALIZE_SCALAR(memState->nextThreadStackBase);
35711886Sbrandon.potter@amd.com    SERIALIZE_SCALAR(memState->mmapEnd);
35810905Sandreas.sandberg@arm.com    pTable->serialize(cp);
35911856Sbrandon.potter@amd.com    /**
36011856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
36111856Sbrandon.potter@amd.com     * come back and fix them at a later date.
36211856Sbrandon.potter@amd.com     */
36311856Sbrandon.potter@amd.com
36411856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
36511856Sbrandon.potter@amd.com#if 0
36611856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
36711856Sbrandon.potter@amd.com        (*fds)[x].serializeSection(cp, csprintf("FDEntry%d", x));
36811856Sbrandon.potter@amd.com#endif
3693311Ssaidi@eecs.umich.edu
3703311Ssaidi@eecs.umich.edu}
3713311Ssaidi@eecs.umich.edu
3723311Ssaidi@eecs.umich.eduvoid
37310905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp)
3743311Ssaidi@eecs.umich.edu{
37511886Sbrandon.potter@amd.com    UNSERIALIZE_SCALAR(memState->brkPoint);
37611886Sbrandon.potter@amd.com    UNSERIALIZE_SCALAR(memState->stackBase);
37711886Sbrandon.potter@amd.com    UNSERIALIZE_SCALAR(memState->stackSize);
37811886Sbrandon.potter@amd.com    UNSERIALIZE_SCALAR(memState->stackMin);
37911886Sbrandon.potter@amd.com    UNSERIALIZE_SCALAR(memState->nextThreadStackBase);
38011886Sbrandon.potter@amd.com    UNSERIALIZE_SCALAR(memState->mmapEnd);
38110905Sandreas.sandberg@arm.com    pTable->unserialize(cp);
38211856Sbrandon.potter@amd.com    /**
38311856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
38411856Sbrandon.potter@amd.com     * come back and fix them at a later date.
38511856Sbrandon.potter@amd.com     */
38611856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
38711856Sbrandon.potter@amd.com#if 0
38811856Sbrandon.potter@amd.com    for (int x = 0; x < fds->getSize(); x++)
38911856Sbrandon.potter@amd.com        (*fds)[x]->unserializeSection(cp, csprintf("FDEntry%d", x));
39011856Sbrandon.potter@amd.com    fds->restoreFileOffsets();
39111856Sbrandon.potter@amd.com#endif
3926820SLisa.Hsu@amd.com    // The above returns a bool so that you could do something if you don't
3936820SLisa.Hsu@amd.com    // find the param in the checkpoint if you wanted to, like set a default
39410930Sbrandon.potter@amd.com    // but in this case we'll just stick with the instantiated value if not
39510929Sbrandon.potter@amd.com    // found.
3963311Ssaidi@eecs.umich.edu}
3972SN/A
3989110Ssteve.reinhardt@amd.combool
39910558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable)
4009110Ssteve.reinhardt@amd.com{
40110558Salexandru.dutu@amd.com    pTable->map(vaddr, paddr, size,
40211294Sandreas.hansson@arm.com                cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable);
4039110Ssteve.reinhardt@amd.com    return true;
4049110Ssteve.reinhardt@amd.com}
4059110Ssteve.reinhardt@amd.com
4062378SN/Avoid
40711877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
4082093SN/A{
40911886Sbrandon.potter@amd.com    numSyscalls++;
4102093SN/A
4112093SN/A    SyscallDesc *desc = getDesc(callnum);
4122093SN/A    if (desc == NULL)
4132093SN/A        fatal("Syscall %d out of range", callnum);
4142093SN/A
41511877Sbrandon.potter@amd.com    desc->doSyscall(callnum, this, tc, fault);
4162093SN/A}
4172SN/A
4186701Sgblack@eecs.umich.eduIntReg
41911851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
4206701Sgblack@eecs.umich.edu{
4216701Sgblack@eecs.umich.edu    return getSyscallArg(tc, i);
4226701Sgblack@eecs.umich.edu}
4236701Sgblack@eecs.umich.edu
42410496Ssteve.reinhardt@amd.comEmulatedDriver *
42511851Sbrandon.potter@amd.comProcess::findDriver(std::string filename)
42610496Ssteve.reinhardt@amd.com{
42710496Ssteve.reinhardt@amd.com    for (EmulatedDriver *d : drivers) {
42810496Ssteve.reinhardt@amd.com        if (d->match(filename))
42910496Ssteve.reinhardt@amd.com            return d;
43010496Ssteve.reinhardt@amd.com    }
43110496Ssteve.reinhardt@amd.com
43210496Ssteve.reinhardt@amd.com    return NULL;
43310496Ssteve.reinhardt@amd.com}
43410496Ssteve.reinhardt@amd.com
43511389Sbrandon.potter@amd.comvoid
43611851Sbrandon.potter@amd.comProcess::updateBias()
43711389Sbrandon.potter@amd.com{
43811389Sbrandon.potter@amd.com    ObjectFile *interp = objFile->getInterpreter();
43911389Sbrandon.potter@amd.com
44011389Sbrandon.potter@amd.com    if (!interp || !interp->relocatable())
44111389Sbrandon.potter@amd.com        return;
44211389Sbrandon.potter@amd.com
44311389Sbrandon.potter@amd.com    // Determine how large the interpreters footprint will be in the process
44411389Sbrandon.potter@amd.com    // address space.
44511389Sbrandon.potter@amd.com    Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
44611389Sbrandon.potter@amd.com
44711389Sbrandon.potter@amd.com    // We are allocating the memory area; set the bias to the lowest address
44811389Sbrandon.potter@amd.com    // in the allocated memory region.
44911886Sbrandon.potter@amd.com    Addr *end = &memState->mmapEnd;
45011886Sbrandon.potter@amd.com    Addr ld_bias = mmapGrowsDown() ? *end - interp_mapsize : *end;
45111389Sbrandon.potter@amd.com
45211389Sbrandon.potter@amd.com    // Adjust the process mmap area to give the interpreter room; the real
45311389Sbrandon.potter@amd.com    // execve system call would just invoke the kernel's internal mmap
45411389Sbrandon.potter@amd.com    // functions to make these adjustments.
45511886Sbrandon.potter@amd.com    *end = mmapGrowsDown() ? ld_bias : *end + interp_mapsize;
45611389Sbrandon.potter@amd.com
45711389Sbrandon.potter@amd.com    interp->updateBias(ld_bias);
45811389Sbrandon.potter@amd.com}
45911389Sbrandon.potter@amd.com
46011392Sbrandon.potter@amd.comObjectFile *
46111851Sbrandon.potter@amd.comProcess::getInterpreter()
46211392Sbrandon.potter@amd.com{
46311392Sbrandon.potter@amd.com    return objFile->getInterpreter();
46411392Sbrandon.potter@amd.com}
46511392Sbrandon.potter@amd.com
46611389Sbrandon.potter@amd.comAddr
46711851Sbrandon.potter@amd.comProcess::getBias()
46811389Sbrandon.potter@amd.com{
46911392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
47011389Sbrandon.potter@amd.com
47111389Sbrandon.potter@amd.com    return interp ? interp->bias() : objFile->bias();
47211389Sbrandon.potter@amd.com}
47311389Sbrandon.potter@amd.com
47411389Sbrandon.potter@amd.comAddr
47511851Sbrandon.potter@amd.comProcess::getStartPC()
47611389Sbrandon.potter@amd.com{
47711392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
47811389Sbrandon.potter@amd.com
47911389Sbrandon.potter@amd.com    return interp ? interp->entryPoint() : objFile->entryPoint();
48011389Sbrandon.potter@amd.com}
48111389Sbrandon.potter@amd.com
48211851Sbrandon.potter@amd.comProcess *
48311851Sbrandon.potter@amd.comProcessParams::create()
4842715Sstever@eecs.umich.edu{
48511851Sbrandon.potter@amd.com    Process *process = NULL;
4862715Sstever@eecs.umich.edu
48711140Sjthestness@gmail.com    // If not specified, set the executable parameter equal to the
48811140Sjthestness@gmail.com    // simulated system's zeroth command line parameter
48911851Sbrandon.potter@amd.com    if (executable == "") {
49011851Sbrandon.potter@amd.com        executable = cmd[0];
49111140Sjthestness@gmail.com    }
49211140Sjthestness@gmail.com
49311851Sbrandon.potter@amd.com    ObjectFile *obj_file = createObjectFile(executable);
49411851Sbrandon.potter@amd.com    if (obj_file == NULL) {
49511851Sbrandon.potter@amd.com        fatal("Can't load object file %s", executable);
4962715Sstever@eecs.umich.edu    }
4972715Sstever@eecs.umich.edu
4985089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA
49911851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Alpha)
5005753Ssteve.reinhardt@amd.com        fatal("Object file architecture does not match compiled ISA (Alpha).");
5015753Ssteve.reinhardt@amd.com
50211851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5035753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5045753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5055753Ssteve.reinhardt@amd.com        // fall through
5062715Sstever@eecs.umich.edu      case ObjectFile::Linux:
50711851Sbrandon.potter@amd.com        process = new AlphaLinuxProcess(this, obj_file);
5082715Sstever@eecs.umich.edu        break;
5092715Sstever@eecs.umich.edu
5102715Sstever@eecs.umich.edu      default:
5112715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5122715Sstever@eecs.umich.edu    }
5132715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
51411851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::SPARC64 &&
51511851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::SPARC32)
5162715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (SPARC).");
51711851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5185753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5195753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5205753Ssteve.reinhardt@amd.com        // fall through
5212715Sstever@eecs.umich.edu      case ObjectFile::Linux:
52211851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::SPARC64) {
52311851Sbrandon.potter@amd.com            process = new Sparc64LinuxProcess(this, obj_file);
5244111Sgblack@eecs.umich.edu        } else {
52511851Sbrandon.potter@amd.com            process = new Sparc32LinuxProcess(this, obj_file);
5264111Sgblack@eecs.umich.edu        }
5272715Sstever@eecs.umich.edu        break;
5282715Sstever@eecs.umich.edu
5292715Sstever@eecs.umich.edu      case ObjectFile::Solaris:
53011851Sbrandon.potter@amd.com        process = new SparcSolarisProcess(this, obj_file);
5312715Sstever@eecs.umich.edu        break;
5325753Ssteve.reinhardt@amd.com
5332715Sstever@eecs.umich.edu      default:
5342715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5352715Sstever@eecs.umich.edu    }
5364157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
53711851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::X86_64 &&
53811851Sbrandon.potter@amd.com        obj_file->getArch() != ObjectFile::I386)
5394166Sgblack@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
54011851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5415753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5425753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5435753Ssteve.reinhardt@amd.com        // fall through
5444166Sgblack@eecs.umich.edu      case ObjectFile::Linux:
54511851Sbrandon.potter@amd.com        if (obj_file->getArch() == ObjectFile::X86_64) {
54611851Sbrandon.potter@amd.com            process = new X86_64LinuxProcess(this, obj_file);
5475874Sgblack@eecs.umich.edu        } else {
54811851Sbrandon.potter@amd.com            process = new I386LinuxProcess(this, obj_file);
5495874Sgblack@eecs.umich.edu        }
5504166Sgblack@eecs.umich.edu        break;
5515753Ssteve.reinhardt@amd.com
5524157Sgblack@eecs.umich.edu      default:
5534157Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5544157Sgblack@eecs.umich.edu    }
5552715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
55611851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Mips)
5572715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (MIPS).");
55811851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5595753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5605753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5615753Ssteve.reinhardt@amd.com        // fall through
5622715Sstever@eecs.umich.edu      case ObjectFile::Linux:
56311851Sbrandon.potter@amd.com        process = new MipsLinuxProcess(this, obj_file);
5642715Sstever@eecs.umich.edu        break;
5652715Sstever@eecs.umich.edu
5662715Sstever@eecs.umich.edu      default:
5672715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5682715Sstever@eecs.umich.edu    }
5695335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA
57011851Sbrandon.potter@amd.com    ObjectFile::Arch arch = obj_file->getArch();
57110037SARM gem5 Developers    if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb &&
57210037SARM gem5 Developers        arch != ObjectFile::Arm64)
5735335Shines@cs.fsu.edu        fatal("Object file architecture does not match compiled ISA (ARM).");
57411851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
5755753Ssteve.reinhardt@amd.com      case ObjectFile::UnknownOpSys:
5765753Ssteve.reinhardt@amd.com        warn("Unknown operating system; assuming Linux.");
5775753Ssteve.reinhardt@amd.com        // fall through
5785335Shines@cs.fsu.edu      case ObjectFile::Linux:
57910037SARM gem5 Developers        if (arch == ObjectFile::Arm64) {
58011851Sbrandon.potter@amd.com            process = new ArmLinuxProcess64(this, obj_file,
58111851Sbrandon.potter@amd.com                                            obj_file->getArch());
58210037SARM gem5 Developers        } else {
58311851Sbrandon.potter@amd.com            process = new ArmLinuxProcess32(this, obj_file,
58411851Sbrandon.potter@amd.com                                            obj_file->getArch());
58510037SARM gem5 Developers        }
5865335Shines@cs.fsu.edu        break;
58710810Sbr@bsdpad.com      case ObjectFile::FreeBSD:
58810810Sbr@bsdpad.com        if (arch == ObjectFile::Arm64) {
58911851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess64(this, obj_file,
59011851Sbrandon.potter@amd.com                                              obj_file->getArch());
59110810Sbr@bsdpad.com        } else {
59211851Sbrandon.potter@amd.com            process = new ArmFreebsdProcess32(this, obj_file,
59311851Sbrandon.potter@amd.com                                              obj_file->getArch());
59410810Sbr@bsdpad.com        }
59510810Sbr@bsdpad.com        break;
5966392Ssaidi@eecs.umich.edu      case ObjectFile::LinuxArmOABI:
5976392Ssaidi@eecs.umich.edu        fatal("M5 does not support ARM OABI binaries. Please recompile with an"
5986392Ssaidi@eecs.umich.edu              " EABI compiler.");
5995335Shines@cs.fsu.edu      default:
6005335Shines@cs.fsu.edu        fatal("Unknown/unsupported operating system.");
6015335Shines@cs.fsu.edu    }
6026691Stjones1@inf.ed.ac.uk#elif THE_ISA == POWER_ISA
60311851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Power)
6046691Stjones1@inf.ed.ac.uk        fatal("Object file architecture does not match compiled ISA (Power).");
60511851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
6066691Stjones1@inf.ed.ac.uk      case ObjectFile::UnknownOpSys:
6076691Stjones1@inf.ed.ac.uk        warn("Unknown operating system; assuming Linux.");
6086691Stjones1@inf.ed.ac.uk        // fall through
6096691Stjones1@inf.ed.ac.uk      case ObjectFile::Linux:
61011851Sbrandon.potter@amd.com        process = new PowerLinuxProcess(this, obj_file);
6116691Stjones1@inf.ed.ac.uk        break;
6126691Stjones1@inf.ed.ac.uk
6136691Stjones1@inf.ed.ac.uk      default:
6146691Stjones1@inf.ed.ac.uk        fatal("Unknown/unsupported operating system.");
6156691Stjones1@inf.ed.ac.uk    }
61611723Sar4jc@virginia.edu#elif THE_ISA == RISCV_ISA
61711851Sbrandon.potter@amd.com    if (obj_file->getArch() != ObjectFile::Riscv)
61811723Sar4jc@virginia.edu        fatal("Object file architecture does not match compiled ISA (RISCV).");
61911851Sbrandon.potter@amd.com    switch (obj_file->getOpSys()) {
62011723Sar4jc@virginia.edu      case ObjectFile::UnknownOpSys:
62111723Sar4jc@virginia.edu        warn("Unknown operating system; assuming Linux.");
62211723Sar4jc@virginia.edu        // fall through
62311723Sar4jc@virginia.edu      case ObjectFile::Linux:
62411851Sbrandon.potter@amd.com        process = new RiscvLinuxProcess(this, obj_file);
62511723Sar4jc@virginia.edu        break;
62611723Sar4jc@virginia.edu      default:
62711723Sar4jc@virginia.edu        fatal("Unknown/unsupported operating system.");
62811723Sar4jc@virginia.edu    }
6292715Sstever@eecs.umich.edu#else
6302715Sstever@eecs.umich.edu#error "THE_ISA not set"
6312715Sstever@eecs.umich.edu#endif
6322715Sstever@eecs.umich.edu
6332715Sstever@eecs.umich.edu    if (process == NULL)
6342715Sstever@eecs.umich.edu        fatal("Unknown error creating process object.");
6352715Sstever@eecs.umich.edu    return process;
6362715Sstever@eecs.umich.edu}
63711854Sbrandon.potter@amd.com
63811854Sbrandon.potter@amd.comstd::string
63911854Sbrandon.potter@amd.comProcess::fullPath(const std::string &file_name)
64011854Sbrandon.potter@amd.com{
64111854Sbrandon.potter@amd.com    if (file_name[0] == '/' || cwd.empty())
64211854Sbrandon.potter@amd.com        return file_name;
64311854Sbrandon.potter@amd.com
64411854Sbrandon.potter@amd.com    std::string full = cwd;
64511854Sbrandon.potter@amd.com
64611854Sbrandon.potter@amd.com    if (cwd[cwd.size() - 1] != '/')
64711854Sbrandon.potter@amd.com        full += '/';
64811854Sbrandon.potter@amd.com
64911854Sbrandon.potter@amd.com    return full + file_name;
65011854Sbrandon.potter@amd.com}
651