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>
5313883Sdavid.hashe@amd.com#include <climits>
5411911SBrandon.Potter@amd.com#include <csignal>
5510930Sbrandon.potter@amd.com#include <map>
562SN/A#include <string>
5711800Sbrandon.potter@amd.com#include <vector>
582SN/A
5911793Sbrandon.potter@amd.com#include "base/intmath.hh"
6056SN/A#include "base/loader/object_file.hh"
611158SN/A#include "base/loader/symtab.hh"
62146SN/A#include "base/statistics.hh"
636658Snate@binkert.org#include "config/the_isa.hh"
642680Sktlim@umich.edu#include "cpu/thread_context.hh"
652378SN/A#include "mem/page_table.hh"
668706Sandreas.hansson@arm.com#include "mem/se_translating_port_proxy.hh"
675154Sgblack@eecs.umich.edu#include "params/Process.hh"
6811800Sbrandon.potter@amd.com#include "sim/emul_driver.hh"
6911856Sbrandon.potter@amd.com#include "sim/fd_array.hh"
7011856Sbrandon.potter@amd.com#include "sim/fd_entry.hh"
7113883Sdavid.hashe@amd.com#include "sim/redirect_path.hh"
7211794Sbrandon.potter@amd.com#include "sim/syscall_desc.hh"
732378SN/A#include "sim/system.hh"
742SN/A
752SN/Ausing namespace std;
762107SN/Ausing namespace TheISA;
772SN/A
7813883Sdavid.hashe@amd.comstatic std::string
7913883Sdavid.hashe@amd.comnormalize(std::string& directory)
8013883Sdavid.hashe@amd.com{
8113883Sdavid.hashe@amd.com    if (directory.back() != '/')
8213883Sdavid.hashe@amd.com        directory += '/';
8313883Sdavid.hashe@amd.com    return directory;
8413883Sdavid.hashe@amd.com}
8513883Sdavid.hashe@amd.com
8612448Sgabeblack@google.comProcess::Process(ProcessParams *params, EmulationPageTable *pTable,
8712431Sgabeblack@google.com                 ObjectFile *obj_file)
887532Ssteve.reinhardt@amd.com    : SimObject(params), system(params->system),
8910299Salexandru.dutu@amd.com      useArchPT(params->useArchPT),
9010554Salexandru.dutu@amd.com      kvmInSE(params->kvmInSE),
9113867Salexandru.dutu@amd.com      useForClone(false),
9212431Sgabeblack@google.com      pTable(pTable),
938852Sandreas.hansson@arm.com      initVirtMem(system->getSystemPort(), this,
9410929Sbrandon.potter@amd.com                  SETranslatingPortProxy::Always),
9511851Sbrandon.potter@amd.com      objFile(obj_file),
9613883Sdavid.hashe@amd.com      argv(params->cmd), envp(params->env),
9711851Sbrandon.potter@amd.com      executable(params->executable),
9813883Sdavid.hashe@amd.com      tgtCwd(normalize(params->cwd)),
9913883Sdavid.hashe@amd.com      hostCwd(checkPathRedirect(tgtCwd)),
10014014Sciro.santilli@arm.com      release(params->release),
10111801Sbrandon.potter@amd.com      _uid(params->uid), _euid(params->euid),
10211801Sbrandon.potter@amd.com      _gid(params->gid), _egid(params->egid),
10311851Sbrandon.potter@amd.com      _pid(params->pid), _ppid(params->ppid),
10411885Sbrandon.potter@amd.com      _pgid(params->pgid), drivers(params->drivers),
10511886Sbrandon.potter@amd.com      fds(make_shared<FDArray>(params->input, params->output, params->errout)),
10611886Sbrandon.potter@amd.com      childClearTID(0)
1072SN/A{
10811885Sbrandon.potter@amd.com    if (_pid >= System::maxPID)
10911885Sbrandon.potter@amd.com        fatal("_pid is too large: %d", _pid);
11011885Sbrandon.potter@amd.com
11111885Sbrandon.potter@amd.com    auto ret_pair = system->PIDs.emplace(_pid);
11211885Sbrandon.potter@amd.com    if (!ret_pair.second)
11311885Sbrandon.potter@amd.com        fatal("_pid %d is already used", _pid);
11411885Sbrandon.potter@amd.com
11511886Sbrandon.potter@amd.com    /**
11611886Sbrandon.potter@amd.com     * Linux bundles together processes into this concept called a thread
11711886Sbrandon.potter@amd.com     * group. The thread group is responsible for recording which processes
11811886Sbrandon.potter@amd.com     * behave as threads within a process context. The thread group leader
11911886Sbrandon.potter@amd.com     * is the process who's tgid is equal to its pid. Other processes which
12011886Sbrandon.potter@amd.com     * belong to the thread group, but do not lead the thread group, are
12111886Sbrandon.potter@amd.com     * treated as child threads. These threads are created by the clone system
12211886Sbrandon.potter@amd.com     * call with options specified to create threads (differing from the
12311886Sbrandon.potter@amd.com     * options used to implement a fork). By default, set up the tgid/pid
12411886Sbrandon.potter@amd.com     * with a new, equivalent value. If CLONE_THREAD is specified, patch
12511886Sbrandon.potter@amd.com     * the tgid value with the old process' value.
12611886Sbrandon.potter@amd.com     */
12711886Sbrandon.potter@amd.com    _tgid = params->pid;
12811886Sbrandon.potter@amd.com
12911886Sbrandon.potter@amd.com    exitGroup = new bool();
13011886Sbrandon.potter@amd.com    sigchld = new bool();
13111886Sbrandon.potter@amd.com
13211851Sbrandon.potter@amd.com    if (!debugSymbolTable) {
13311851Sbrandon.potter@amd.com        debugSymbolTable = new SymbolTable();
13411851Sbrandon.potter@amd.com        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
13511851Sbrandon.potter@amd.com            !objFile->loadLocalSymbols(debugSymbolTable) ||
13611851Sbrandon.potter@amd.com            !objFile->loadWeakSymbols(debugSymbolTable)) {
13711851Sbrandon.potter@amd.com            delete debugSymbolTable;
13811919SBrandon.Potter@amd.com            debugSymbolTable = nullptr;
13911851Sbrandon.potter@amd.com        }
14011851Sbrandon.potter@amd.com    }
1412SN/A}
1422SN/A
1432SN/Avoid
14411886Sbrandon.potter@amd.comProcess::clone(ThreadContext *otc, ThreadContext *ntc,
14513557Sgabeblack@google.com               Process *np, RegVal flags)
14611886Sbrandon.potter@amd.com{
14711920SBrandon.Potter@amd.com#ifndef CLONE_VM
14811920SBrandon.Potter@amd.com#define CLONE_VM 0
14911920SBrandon.Potter@amd.com#endif
15011920SBrandon.Potter@amd.com#ifndef CLONE_FILES
15111920SBrandon.Potter@amd.com#define CLONE_FILES 0
15211920SBrandon.Potter@amd.com#endif
15311920SBrandon.Potter@amd.com#ifndef CLONE_THREAD
15411920SBrandon.Potter@amd.com#define CLONE_THREAD 0
15511920SBrandon.Potter@amd.com#endif
15611886Sbrandon.potter@amd.com    if (CLONE_VM & flags) {
15711886Sbrandon.potter@amd.com        /**
15811886Sbrandon.potter@amd.com         * Share the process memory address space between the new process
15911886Sbrandon.potter@amd.com         * and the old process. Changes in one will be visible in the other
16011886Sbrandon.potter@amd.com         * due to the pointer use.
16111886Sbrandon.potter@amd.com         */
16211886Sbrandon.potter@amd.com        delete np->pTable;
16311886Sbrandon.potter@amd.com        np->pTable = pTable;
16414022Sgabeblack@google.com        auto &proxy = dynamic_cast<SETranslatingPortProxy &>(
16514024Sgabeblack@google.com                ntc->getVirtProxy());
16614022Sgabeblack@google.com        proxy.setPageTable(np->pTable);
16711886Sbrandon.potter@amd.com
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         */
20214121Sbrandon.potter@amd.com        std::shared_ptr<FDArray> nfds = np->fds;
20311886Sbrandon.potter@amd.com        for (int tgt_fd = 0; tgt_fd < fds->getSize(); tgt_fd++) {
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    }
25711919SBrandon.Potter@amd.com    return nullptr;
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
28514138Sbrandon.potter@amd.com    pTable->initState();
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,
30112448Sgabeblack@google.com                clobber ? EmulationPageTable::Clobber :
30212460Sgabeblack@google.com                          EmulationPageTable::MappingFlags(0));
3038601Ssteve.reinhardt@amd.com}
3048601Ssteve.reinhardt@amd.com
30511886Sbrandon.potter@amd.comvoid
30611886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
30711886Sbrandon.potter@amd.com                       ThreadContext *new_tc, bool allocate_page)
30811886Sbrandon.potter@amd.com{
30911886Sbrandon.potter@amd.com    if (allocate_page)
31011886Sbrandon.potter@amd.com        new_paddr = system->allocPhysPages(1);
31111886Sbrandon.potter@amd.com
31211886Sbrandon.potter@amd.com    // Read from old physical page.
31311886Sbrandon.potter@amd.com    uint8_t *buf_p = new uint8_t[PageBytes];
31414024Sgabeblack@google.com    old_tc->getVirtProxy().readBlob(vaddr, buf_p, PageBytes);
31511886Sbrandon.potter@amd.com
31611886Sbrandon.potter@amd.com    // Create new mapping in process address space by clobbering existing
31711886Sbrandon.potter@amd.com    // mapping (if any existed) and then write to the new physical page.
31811886Sbrandon.potter@amd.com    bool clobber = true;
31911886Sbrandon.potter@amd.com    pTable->map(vaddr, new_paddr, PageBytes, clobber);
32014024Sgabeblack@google.com    new_tc->getVirtProxy().writeBlob(vaddr, buf_p, PageBytes);
32111886Sbrandon.potter@amd.com    delete[] buf_p;
32211886Sbrandon.potter@amd.com}
32311886Sbrandon.potter@amd.com
3244434Ssaidi@eecs.umich.edubool
3258539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr)
3264434Ssaidi@eecs.umich.edu{
32711905SBrandon.Potter@amd.com    Addr stack_min = memState->getStackMin();
32811905SBrandon.Potter@amd.com    Addr stack_base = memState->getStackBase();
32911905SBrandon.Potter@amd.com    Addr max_stack_size = memState->getMaxStackSize();
33011905SBrandon.Potter@amd.com
3318539Sgblack@eecs.umich.edu    // Check if this is already on the stack and there's just no page there
3328539Sgblack@eecs.umich.edu    // yet.
33311905SBrandon.Potter@amd.com    if (vaddr >= stack_min && vaddr < stack_base) {
33410318Sandreas.hansson@arm.com        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
3354434Ssaidi@eecs.umich.edu        return true;
3364434Ssaidi@eecs.umich.edu    }
3374434Ssaidi@eecs.umich.edu
3388539Sgblack@eecs.umich.edu    // We've accessed the next page of the stack, so extend it to include
3398539Sgblack@eecs.umich.edu    // this address.
34011905SBrandon.Potter@amd.com    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
34111905SBrandon.Potter@amd.com        while (vaddr < stack_min) {
34211905SBrandon.Potter@amd.com            stack_min -= TheISA::PageBytes;
34311905SBrandon.Potter@amd.com            if (stack_base - stack_min > max_stack_size)
3445154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
34511905SBrandon.Potter@amd.com            allocateMem(stack_min, TheISA::PageBytes);
3465823Ssaidi@eecs.umich.edu            inform("Increasing stack size by one page.");
34711906SBrandon.Potter@amd.com        }
34811905SBrandon.Potter@amd.com        memState->setStackMin(stack_min);
3494434Ssaidi@eecs.umich.edu        return true;
3504434Ssaidi@eecs.umich.edu    }
3514434Ssaidi@eecs.umich.edu    return false;
3524434Ssaidi@eecs.umich.edu}
3534434Ssaidi@eecs.umich.edu
3545282Srstrong@cs.ucsd.eduvoid
35510905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const
3563311Ssaidi@eecs.umich.edu{
35712283Saustinharris@utexas.edu    memState->serialize(cp);
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.");
3653311Ssaidi@eecs.umich.edu}
3663311Ssaidi@eecs.umich.edu
3673311Ssaidi@eecs.umich.eduvoid
36810905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp)
3693311Ssaidi@eecs.umich.edu{
37012283Saustinharris@utexas.edu    memState->unserialize(cp);
37110905Sandreas.sandberg@arm.com    pTable->unserialize(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    warn("Checkpoints for file descriptors currently do not work.");
3776820SLisa.Hsu@amd.com    // The above returns a bool so that you could do something if you don't
3786820SLisa.Hsu@amd.com    // find the param in the checkpoint if you wanted to, like set a default
37910930Sbrandon.potter@amd.com    // but in this case we'll just stick with the instantiated value if not
38010929Sbrandon.potter@amd.com    // found.
3813311Ssaidi@eecs.umich.edu}
3822SN/A
3839110Ssteve.reinhardt@amd.combool
38410558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable)
3859110Ssteve.reinhardt@amd.com{
38610558Salexandru.dutu@amd.com    pTable->map(vaddr, paddr, size,
38712460Sgabeblack@google.com                cacheable ? EmulationPageTable::MappingFlags(0) :
38812448Sgabeblack@google.com                            EmulationPageTable::Uncacheable);
3899110Ssteve.reinhardt@amd.com    return true;
3909110Ssteve.reinhardt@amd.com}
3919110Ssteve.reinhardt@amd.com
3922378SN/Avoid
39311877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
3942093SN/A{
39511886Sbrandon.potter@amd.com    numSyscalls++;
3962093SN/A
3972093SN/A    SyscallDesc *desc = getDesc(callnum);
39811919SBrandon.Potter@amd.com    if (desc == nullptr)
3992093SN/A        fatal("Syscall %d out of range", callnum);
4002093SN/A
40113995Sbrandon.potter@amd.com    desc->doSyscall(callnum, tc, fault);
4022093SN/A}
4032SN/A
40413557Sgabeblack@google.comRegVal
40511851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
4066701Sgblack@eecs.umich.edu{
4076701Sgblack@eecs.umich.edu    return getSyscallArg(tc, i);
4086701Sgblack@eecs.umich.edu}
4096701Sgblack@eecs.umich.edu
41010496Ssteve.reinhardt@amd.comEmulatedDriver *
41111851Sbrandon.potter@amd.comProcess::findDriver(std::string filename)
41210496Ssteve.reinhardt@amd.com{
41310496Ssteve.reinhardt@amd.com    for (EmulatedDriver *d : drivers) {
41410496Ssteve.reinhardt@amd.com        if (d->match(filename))
41510496Ssteve.reinhardt@amd.com            return d;
41610496Ssteve.reinhardt@amd.com    }
41710496Ssteve.reinhardt@amd.com
41811919SBrandon.Potter@amd.com    return nullptr;
41910496Ssteve.reinhardt@amd.com}
42010496Ssteve.reinhardt@amd.com
42113883Sdavid.hashe@amd.comstd::string
42213883Sdavid.hashe@amd.comProcess::checkPathRedirect(const std::string &filename)
42313883Sdavid.hashe@amd.com{
42413906Sbrandon.potter@amd.com    // If the input parameter contains a relative path, convert it.
42513906Sbrandon.potter@amd.com    // The target version of the current working directory is fine since
42613906Sbrandon.potter@amd.com    // we immediately convert it using redirect paths into a host version.
42713906Sbrandon.potter@amd.com    auto abs_path = absolutePath(filename, false);
42813883Sdavid.hashe@amd.com
42913883Sdavid.hashe@amd.com    for (auto path : system->redirectPaths) {
43013883Sdavid.hashe@amd.com        // Search through the redirect paths to see if a starting substring of
43113883Sdavid.hashe@amd.com        // our path falls into any buckets which need to redirected.
43213906Sbrandon.potter@amd.com        if (startswith(abs_path, path->appPath())) {
43313906Sbrandon.potter@amd.com            std::string tail = abs_path.substr(path->appPath().size());
43413883Sdavid.hashe@amd.com
43513883Sdavid.hashe@amd.com            // If this path needs to be redirected, search through a list
43613883Sdavid.hashe@amd.com            // of targets to see if we can match a valid file (or directory).
43713883Sdavid.hashe@amd.com            for (auto host_path : path->hostPaths()) {
43813883Sdavid.hashe@amd.com                if (access((host_path + tail).c_str(), R_OK) == 0) {
43913883Sdavid.hashe@amd.com                    // Return the valid match.
44013883Sdavid.hashe@amd.com                    return host_path + tail;
44113883Sdavid.hashe@amd.com                }
44213883Sdavid.hashe@amd.com            }
44313883Sdavid.hashe@amd.com            // The path needs to be redirected, but the file or directory
44413883Sdavid.hashe@amd.com            // does not exist on the host filesystem. Return the first
44513883Sdavid.hashe@amd.com            // host path as a default.
44613883Sdavid.hashe@amd.com            return path->hostPaths()[0] + tail;
44713883Sdavid.hashe@amd.com        }
44813883Sdavid.hashe@amd.com    }
44913883Sdavid.hashe@amd.com
45013883Sdavid.hashe@amd.com    // The path does not need to be redirected.
45113906Sbrandon.potter@amd.com    return abs_path;
45213883Sdavid.hashe@amd.com}
45313883Sdavid.hashe@amd.com
45411389Sbrandon.potter@amd.comvoid
45511851Sbrandon.potter@amd.comProcess::updateBias()
45611389Sbrandon.potter@amd.com{
45711389Sbrandon.potter@amd.com    ObjectFile *interp = objFile->getInterpreter();
45811389Sbrandon.potter@amd.com
45911389Sbrandon.potter@amd.com    if (!interp || !interp->relocatable())
46011389Sbrandon.potter@amd.com        return;
46111389Sbrandon.potter@amd.com
46211389Sbrandon.potter@amd.com    // Determine how large the interpreters footprint will be in the process
46311389Sbrandon.potter@amd.com    // address space.
46411389Sbrandon.potter@amd.com    Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
46511389Sbrandon.potter@amd.com
46611389Sbrandon.potter@amd.com    // We are allocating the memory area; set the bias to the lowest address
46711389Sbrandon.potter@amd.com    // in the allocated memory region.
46811905SBrandon.Potter@amd.com    Addr mmap_end = memState->getMmapEnd();
46911905SBrandon.Potter@amd.com    Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
47011389Sbrandon.potter@amd.com
47111389Sbrandon.potter@amd.com    // Adjust the process mmap area to give the interpreter room; the real
47211389Sbrandon.potter@amd.com    // execve system call would just invoke the kernel's internal mmap
47311389Sbrandon.potter@amd.com    // functions to make these adjustments.
47411905SBrandon.Potter@amd.com    mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
47511905SBrandon.Potter@amd.com    memState->setMmapEnd(mmap_end);
47611389Sbrandon.potter@amd.com
47711389Sbrandon.potter@amd.com    interp->updateBias(ld_bias);
47811389Sbrandon.potter@amd.com}
47911389Sbrandon.potter@amd.com
48011392Sbrandon.potter@amd.comObjectFile *
48111851Sbrandon.potter@amd.comProcess::getInterpreter()
48211392Sbrandon.potter@amd.com{
48311392Sbrandon.potter@amd.com    return objFile->getInterpreter();
48411392Sbrandon.potter@amd.com}
48511392Sbrandon.potter@amd.com
48611389Sbrandon.potter@amd.comAddr
48711851Sbrandon.potter@amd.comProcess::getBias()
48811389Sbrandon.potter@amd.com{
48911392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
49011389Sbrandon.potter@amd.com
49111389Sbrandon.potter@amd.com    return interp ? interp->bias() : objFile->bias();
49211389Sbrandon.potter@amd.com}
49311389Sbrandon.potter@amd.com
49411389Sbrandon.potter@amd.comAddr
49511851Sbrandon.potter@amd.comProcess::getStartPC()
49611389Sbrandon.potter@amd.com{
49711392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
49811389Sbrandon.potter@amd.com
49911389Sbrandon.potter@amd.com    return interp ? interp->entryPoint() : objFile->entryPoint();
50011389Sbrandon.potter@amd.com}
50111389Sbrandon.potter@amd.com
50213883Sdavid.hashe@amd.comstd::string
50313883Sdavid.hashe@amd.comProcess::absolutePath(const std::string &filename, bool host_filesystem)
50413883Sdavid.hashe@amd.com{
50513883Sdavid.hashe@amd.com    if (filename.empty() || startswith(filename, "/"))
50613883Sdavid.hashe@amd.com        return filename;
50713883Sdavid.hashe@amd.com
50813883Sdavid.hashe@amd.com    // Construct the absolute path given the current working directory for
50913883Sdavid.hashe@amd.com    // either the host filesystem or target filesystem. The distinction only
51013883Sdavid.hashe@amd.com    // matters if filesystem redirection is utilized in the simulation.
51113906Sbrandon.potter@amd.com    auto path_base = std::string();
51213906Sbrandon.potter@amd.com    if (host_filesystem) {
51313906Sbrandon.potter@amd.com        path_base = hostCwd;
51413906Sbrandon.potter@amd.com        assert(!hostCwd.empty());
51513906Sbrandon.potter@amd.com    } else {
51613906Sbrandon.potter@amd.com        path_base = tgtCwd;
51713906Sbrandon.potter@amd.com        assert(!tgtCwd.empty());
51813906Sbrandon.potter@amd.com    }
51913883Sdavid.hashe@amd.com
52013883Sdavid.hashe@amd.com    // Add a trailing '/' if the current working directory did not have one.
52113883Sdavid.hashe@amd.com    normalize(path_base);
52213883Sdavid.hashe@amd.com
52313883Sdavid.hashe@amd.com    // Append the filename onto the current working path.
52413883Sdavid.hashe@amd.com    auto absolute_path = path_base + filename;
52513883Sdavid.hashe@amd.com
52613883Sdavid.hashe@amd.com    return absolute_path;
52713883Sdavid.hashe@amd.com}
52813883Sdavid.hashe@amd.com
52911851Sbrandon.potter@amd.comProcess *
53011851Sbrandon.potter@amd.comProcessParams::create()
5312715Sstever@eecs.umich.edu{
53211140Sjthestness@gmail.com    // If not specified, set the executable parameter equal to the
53311140Sjthestness@gmail.com    // simulated system's zeroth command line parameter
53411851Sbrandon.potter@amd.com    if (executable == "") {
53511851Sbrandon.potter@amd.com        executable = cmd[0];
53611140Sjthestness@gmail.com    }
53711140Sjthestness@gmail.com
53811851Sbrandon.potter@amd.com    ObjectFile *obj_file = createObjectFile(executable);
53914139Sbrandon.potter@amd.com    fatal_if(!obj_file, "Cannot load object file %s.", executable);
5402715Sstever@eecs.umich.edu
54114139Sbrandon.potter@amd.com    Process *process = ObjectFile::tryLoaders(this, obj_file);
54213990Sgabeblack@google.com    fatal_if(!process, "Unknown error creating process object.");
5435753Ssteve.reinhardt@amd.com
5442715Sstever@eecs.umich.edu    return process;
5452715Sstever@eecs.umich.edu}
546