process.cc revision 13995
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)),
10011801Sbrandon.potter@amd.com      _uid(params->uid), _euid(params->euid),
10111801Sbrandon.potter@amd.com      _gid(params->gid), _egid(params->egid),
10211851Sbrandon.potter@amd.com      _pid(params->pid), _ppid(params->ppid),
10311885Sbrandon.potter@amd.com      _pgid(params->pgid), drivers(params->drivers),
10411886Sbrandon.potter@amd.com      fds(make_shared<FDArray>(params->input, params->output, params->errout)),
10511886Sbrandon.potter@amd.com      childClearTID(0)
1062SN/A{
10711885Sbrandon.potter@amd.com    if (_pid >= System::maxPID)
10811885Sbrandon.potter@amd.com        fatal("_pid is too large: %d", _pid);
10911885Sbrandon.potter@amd.com
11011885Sbrandon.potter@amd.com    auto ret_pair = system->PIDs.emplace(_pid);
11111885Sbrandon.potter@amd.com    if (!ret_pair.second)
11211885Sbrandon.potter@amd.com        fatal("_pid %d is already used", _pid);
11311885Sbrandon.potter@amd.com
11411886Sbrandon.potter@amd.com    /**
11511886Sbrandon.potter@amd.com     * Linux bundles together processes into this concept called a thread
11611886Sbrandon.potter@amd.com     * group. The thread group is responsible for recording which processes
11711886Sbrandon.potter@amd.com     * behave as threads within a process context. The thread group leader
11811886Sbrandon.potter@amd.com     * is the process who's tgid is equal to its pid. Other processes which
11911886Sbrandon.potter@amd.com     * belong to the thread group, but do not lead the thread group, are
12011886Sbrandon.potter@amd.com     * treated as child threads. These threads are created by the clone system
12111886Sbrandon.potter@amd.com     * call with options specified to create threads (differing from the
12211886Sbrandon.potter@amd.com     * options used to implement a fork). By default, set up the tgid/pid
12311886Sbrandon.potter@amd.com     * with a new, equivalent value. If CLONE_THREAD is specified, patch
12411886Sbrandon.potter@amd.com     * the tgid value with the old process' value.
12511886Sbrandon.potter@amd.com     */
12611886Sbrandon.potter@amd.com    _tgid = params->pid;
12711886Sbrandon.potter@amd.com
12811886Sbrandon.potter@amd.com    exitGroup = new bool();
12911886Sbrandon.potter@amd.com    sigchld = new bool();
13011886Sbrandon.potter@amd.com
13111851Sbrandon.potter@amd.com    if (!debugSymbolTable) {
13211851Sbrandon.potter@amd.com        debugSymbolTable = new SymbolTable();
13311851Sbrandon.potter@amd.com        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
13411851Sbrandon.potter@amd.com            !objFile->loadLocalSymbols(debugSymbolTable) ||
13511851Sbrandon.potter@amd.com            !objFile->loadWeakSymbols(debugSymbolTable)) {
13611851Sbrandon.potter@amd.com            delete debugSymbolTable;
13711919SBrandon.Potter@amd.com            debugSymbolTable = nullptr;
13811851Sbrandon.potter@amd.com        }
13911851Sbrandon.potter@amd.com    }
1402SN/A}
1412SN/A
1422SN/Avoid
14311886Sbrandon.potter@amd.comProcess::clone(ThreadContext *otc, ThreadContext *ntc,
14413557Sgabeblack@google.com               Process *np, RegVal flags)
14511886Sbrandon.potter@amd.com{
14611920SBrandon.Potter@amd.com#ifndef CLONE_VM
14711920SBrandon.Potter@amd.com#define CLONE_VM 0
14811920SBrandon.Potter@amd.com#endif
14911920SBrandon.Potter@amd.com#ifndef CLONE_FILES
15011920SBrandon.Potter@amd.com#define CLONE_FILES 0
15111920SBrandon.Potter@amd.com#endif
15211920SBrandon.Potter@amd.com#ifndef CLONE_THREAD
15311920SBrandon.Potter@amd.com#define CLONE_THREAD 0
15411920SBrandon.Potter@amd.com#endif
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    }
25411919SBrandon.Potter@amd.com    return nullptr;
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,
29812448Sgabeblack@google.com                clobber ? EmulationPageTable::Clobber :
29912460Sgabeblack@google.com                          EmulationPageTable::MappingFlags(0));
3008601Ssteve.reinhardt@amd.com}
3018601Ssteve.reinhardt@amd.com
30211886Sbrandon.potter@amd.comvoid
30311886Sbrandon.potter@amd.comProcess::replicatePage(Addr vaddr, Addr new_paddr, ThreadContext *old_tc,
30411886Sbrandon.potter@amd.com                       ThreadContext *new_tc, bool allocate_page)
30511886Sbrandon.potter@amd.com{
30611886Sbrandon.potter@amd.com    if (allocate_page)
30711886Sbrandon.potter@amd.com        new_paddr = system->allocPhysPages(1);
30811886Sbrandon.potter@amd.com
30911886Sbrandon.potter@amd.com    // Read from old physical page.
31011886Sbrandon.potter@amd.com    uint8_t *buf_p = new uint8_t[PageBytes];
31111886Sbrandon.potter@amd.com    old_tc->getMemProxy().readBlob(vaddr, buf_p, PageBytes);
31211886Sbrandon.potter@amd.com
31311886Sbrandon.potter@amd.com    // Create new mapping in process address space by clobbering existing
31411886Sbrandon.potter@amd.com    // mapping (if any existed) and then write to the new physical page.
31511886Sbrandon.potter@amd.com    bool clobber = true;
31611886Sbrandon.potter@amd.com    pTable->map(vaddr, new_paddr, PageBytes, clobber);
31711886Sbrandon.potter@amd.com    new_tc->getMemProxy().writeBlob(vaddr, buf_p, PageBytes);
31811886Sbrandon.potter@amd.com    delete[] buf_p;
31911886Sbrandon.potter@amd.com}
32011886Sbrandon.potter@amd.com
3214434Ssaidi@eecs.umich.edubool
3228539Sgblack@eecs.umich.eduProcess::fixupStackFault(Addr vaddr)
3234434Ssaidi@eecs.umich.edu{
32411905SBrandon.Potter@amd.com    Addr stack_min = memState->getStackMin();
32511905SBrandon.Potter@amd.com    Addr stack_base = memState->getStackBase();
32611905SBrandon.Potter@amd.com    Addr max_stack_size = memState->getMaxStackSize();
32711905SBrandon.Potter@amd.com
3288539Sgblack@eecs.umich.edu    // Check if this is already on the stack and there's just no page there
3298539Sgblack@eecs.umich.edu    // yet.
33011905SBrandon.Potter@amd.com    if (vaddr >= stack_min && vaddr < stack_base) {
33110318Sandreas.hansson@arm.com        allocateMem(roundDown(vaddr, PageBytes), PageBytes);
3324434Ssaidi@eecs.umich.edu        return true;
3334434Ssaidi@eecs.umich.edu    }
3344434Ssaidi@eecs.umich.edu
3358539Sgblack@eecs.umich.edu    // We've accessed the next page of the stack, so extend it to include
3368539Sgblack@eecs.umich.edu    // this address.
33711905SBrandon.Potter@amd.com    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
33811905SBrandon.Potter@amd.com        while (vaddr < stack_min) {
33911905SBrandon.Potter@amd.com            stack_min -= TheISA::PageBytes;
34011905SBrandon.Potter@amd.com            if (stack_base - stack_min > max_stack_size)
3415154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
34211905SBrandon.Potter@amd.com            allocateMem(stack_min, TheISA::PageBytes);
3435823Ssaidi@eecs.umich.edu            inform("Increasing stack size by one page.");
34411906SBrandon.Potter@amd.com        }
34511905SBrandon.Potter@amd.com        memState->setStackMin(stack_min);
3464434Ssaidi@eecs.umich.edu        return true;
3474434Ssaidi@eecs.umich.edu    }
3484434Ssaidi@eecs.umich.edu    return false;
3494434Ssaidi@eecs.umich.edu}
3504434Ssaidi@eecs.umich.edu
3515282Srstrong@cs.ucsd.eduvoid
35210905Sandreas.sandberg@arm.comProcess::serialize(CheckpointOut &cp) const
3533311Ssaidi@eecs.umich.edu{
35412283Saustinharris@utexas.edu    memState->serialize(cp);
35510905Sandreas.sandberg@arm.com    pTable->serialize(cp);
35611856Sbrandon.potter@amd.com    /**
35711856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
35811856Sbrandon.potter@amd.com     * come back and fix them at a later date.
35911856Sbrandon.potter@amd.com     */
36011856Sbrandon.potter@amd.com
36111856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
3623311Ssaidi@eecs.umich.edu}
3633311Ssaidi@eecs.umich.edu
3643311Ssaidi@eecs.umich.eduvoid
36510905Sandreas.sandberg@arm.comProcess::unserialize(CheckpointIn &cp)
3663311Ssaidi@eecs.umich.edu{
36712283Saustinharris@utexas.edu    memState->unserialize(cp);
36810905Sandreas.sandberg@arm.com    pTable->unserialize(cp);
36911856Sbrandon.potter@amd.com    /**
37011856Sbrandon.potter@amd.com     * Checkpoints for file descriptors currently do not work. Need to
37111856Sbrandon.potter@amd.com     * come back and fix them at a later date.
37211856Sbrandon.potter@amd.com     */
37311856Sbrandon.potter@amd.com    warn("Checkpoints for file descriptors currently do not work.");
3746820SLisa.Hsu@amd.com    // The above returns a bool so that you could do something if you don't
3756820SLisa.Hsu@amd.com    // find the param in the checkpoint if you wanted to, like set a default
37610930Sbrandon.potter@amd.com    // but in this case we'll just stick with the instantiated value if not
37710929Sbrandon.potter@amd.com    // found.
3783311Ssaidi@eecs.umich.edu}
3792SN/A
3809110Ssteve.reinhardt@amd.combool
38110558Salexandru.dutu@amd.comProcess::map(Addr vaddr, Addr paddr, int size, bool cacheable)
3829110Ssteve.reinhardt@amd.com{
38310558Salexandru.dutu@amd.com    pTable->map(vaddr, paddr, size,
38412460Sgabeblack@google.com                cacheable ? EmulationPageTable::MappingFlags(0) :
38512448Sgabeblack@google.com                            EmulationPageTable::Uncacheable);
3869110Ssteve.reinhardt@amd.com    return true;
3879110Ssteve.reinhardt@amd.com}
3889110Ssteve.reinhardt@amd.com
3892378SN/Avoid
39011877Sbrandon.potter@amd.comProcess::syscall(int64_t callnum, ThreadContext *tc, Fault *fault)
3912093SN/A{
39211886Sbrandon.potter@amd.com    numSyscalls++;
3932093SN/A
3942093SN/A    SyscallDesc *desc = getDesc(callnum);
39511919SBrandon.Potter@amd.com    if (desc == nullptr)
3962093SN/A        fatal("Syscall %d out of range", callnum);
3972093SN/A
39813995Sbrandon.potter@amd.com    desc->doSyscall(callnum, tc, fault);
3992093SN/A}
4002SN/A
40113557Sgabeblack@google.comRegVal
40211851Sbrandon.potter@amd.comProcess::getSyscallArg(ThreadContext *tc, int &i, int width)
4036701Sgblack@eecs.umich.edu{
4046701Sgblack@eecs.umich.edu    return getSyscallArg(tc, i);
4056701Sgblack@eecs.umich.edu}
4066701Sgblack@eecs.umich.edu
40710496Ssteve.reinhardt@amd.comEmulatedDriver *
40811851Sbrandon.potter@amd.comProcess::findDriver(std::string filename)
40910496Ssteve.reinhardt@amd.com{
41010496Ssteve.reinhardt@amd.com    for (EmulatedDriver *d : drivers) {
41110496Ssteve.reinhardt@amd.com        if (d->match(filename))
41210496Ssteve.reinhardt@amd.com            return d;
41310496Ssteve.reinhardt@amd.com    }
41410496Ssteve.reinhardt@amd.com
41511919SBrandon.Potter@amd.com    return nullptr;
41610496Ssteve.reinhardt@amd.com}
41710496Ssteve.reinhardt@amd.com
41813883Sdavid.hashe@amd.comstd::string
41913883Sdavid.hashe@amd.comProcess::checkPathRedirect(const std::string &filename)
42013883Sdavid.hashe@amd.com{
42113906Sbrandon.potter@amd.com    // If the input parameter contains a relative path, convert it.
42213906Sbrandon.potter@amd.com    // The target version of the current working directory is fine since
42313906Sbrandon.potter@amd.com    // we immediately convert it using redirect paths into a host version.
42413906Sbrandon.potter@amd.com    auto abs_path = absolutePath(filename, false);
42513883Sdavid.hashe@amd.com
42613883Sdavid.hashe@amd.com    for (auto path : system->redirectPaths) {
42713883Sdavid.hashe@amd.com        // Search through the redirect paths to see if a starting substring of
42813883Sdavid.hashe@amd.com        // our path falls into any buckets which need to redirected.
42913906Sbrandon.potter@amd.com        if (startswith(abs_path, path->appPath())) {
43013906Sbrandon.potter@amd.com            std::string tail = abs_path.substr(path->appPath().size());
43113883Sdavid.hashe@amd.com
43213883Sdavid.hashe@amd.com            // If this path needs to be redirected, search through a list
43313883Sdavid.hashe@amd.com            // of targets to see if we can match a valid file (or directory).
43413883Sdavid.hashe@amd.com            for (auto host_path : path->hostPaths()) {
43513883Sdavid.hashe@amd.com                if (access((host_path + tail).c_str(), R_OK) == 0) {
43613883Sdavid.hashe@amd.com                    // Return the valid match.
43713883Sdavid.hashe@amd.com                    return host_path + tail;
43813883Sdavid.hashe@amd.com                }
43913883Sdavid.hashe@amd.com            }
44013883Sdavid.hashe@amd.com            // The path needs to be redirected, but the file or directory
44113883Sdavid.hashe@amd.com            // does not exist on the host filesystem. Return the first
44213883Sdavid.hashe@amd.com            // host path as a default.
44313883Sdavid.hashe@amd.com            return path->hostPaths()[0] + tail;
44413883Sdavid.hashe@amd.com        }
44513883Sdavid.hashe@amd.com    }
44613883Sdavid.hashe@amd.com
44713883Sdavid.hashe@amd.com    // The path does not need to be redirected.
44813906Sbrandon.potter@amd.com    return abs_path;
44913883Sdavid.hashe@amd.com}
45013883Sdavid.hashe@amd.com
45111389Sbrandon.potter@amd.comvoid
45211851Sbrandon.potter@amd.comProcess::updateBias()
45311389Sbrandon.potter@amd.com{
45411389Sbrandon.potter@amd.com    ObjectFile *interp = objFile->getInterpreter();
45511389Sbrandon.potter@amd.com
45611389Sbrandon.potter@amd.com    if (!interp || !interp->relocatable())
45711389Sbrandon.potter@amd.com        return;
45811389Sbrandon.potter@amd.com
45911389Sbrandon.potter@amd.com    // Determine how large the interpreters footprint will be in the process
46011389Sbrandon.potter@amd.com    // address space.
46111389Sbrandon.potter@amd.com    Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes);
46211389Sbrandon.potter@amd.com
46311389Sbrandon.potter@amd.com    // We are allocating the memory area; set the bias to the lowest address
46411389Sbrandon.potter@amd.com    // in the allocated memory region.
46511905SBrandon.Potter@amd.com    Addr mmap_end = memState->getMmapEnd();
46611905SBrandon.Potter@amd.com    Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end;
46711389Sbrandon.potter@amd.com
46811389Sbrandon.potter@amd.com    // Adjust the process mmap area to give the interpreter room; the real
46911389Sbrandon.potter@amd.com    // execve system call would just invoke the kernel's internal mmap
47011389Sbrandon.potter@amd.com    // functions to make these adjustments.
47111905SBrandon.Potter@amd.com    mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize;
47211905SBrandon.Potter@amd.com    memState->setMmapEnd(mmap_end);
47311389Sbrandon.potter@amd.com
47411389Sbrandon.potter@amd.com    interp->updateBias(ld_bias);
47511389Sbrandon.potter@amd.com}
47611389Sbrandon.potter@amd.com
47711392Sbrandon.potter@amd.comObjectFile *
47811851Sbrandon.potter@amd.comProcess::getInterpreter()
47911392Sbrandon.potter@amd.com{
48011392Sbrandon.potter@amd.com    return objFile->getInterpreter();
48111392Sbrandon.potter@amd.com}
48211392Sbrandon.potter@amd.com
48311389Sbrandon.potter@amd.comAddr
48411851Sbrandon.potter@amd.comProcess::getBias()
48511389Sbrandon.potter@amd.com{
48611392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
48711389Sbrandon.potter@amd.com
48811389Sbrandon.potter@amd.com    return interp ? interp->bias() : objFile->bias();
48911389Sbrandon.potter@amd.com}
49011389Sbrandon.potter@amd.com
49111389Sbrandon.potter@amd.comAddr
49211851Sbrandon.potter@amd.comProcess::getStartPC()
49311389Sbrandon.potter@amd.com{
49411392Sbrandon.potter@amd.com    ObjectFile *interp = getInterpreter();
49511389Sbrandon.potter@amd.com
49611389Sbrandon.potter@amd.com    return interp ? interp->entryPoint() : objFile->entryPoint();
49711389Sbrandon.potter@amd.com}
49811389Sbrandon.potter@amd.com
49913883Sdavid.hashe@amd.comstd::string
50013883Sdavid.hashe@amd.comProcess::absolutePath(const std::string &filename, bool host_filesystem)
50113883Sdavid.hashe@amd.com{
50213883Sdavid.hashe@amd.com    if (filename.empty() || startswith(filename, "/"))
50313883Sdavid.hashe@amd.com        return filename;
50413883Sdavid.hashe@amd.com
50513883Sdavid.hashe@amd.com    // Construct the absolute path given the current working directory for
50613883Sdavid.hashe@amd.com    // either the host filesystem or target filesystem. The distinction only
50713883Sdavid.hashe@amd.com    // matters if filesystem redirection is utilized in the simulation.
50813906Sbrandon.potter@amd.com    auto path_base = std::string();
50913906Sbrandon.potter@amd.com    if (host_filesystem) {
51013906Sbrandon.potter@amd.com        path_base = hostCwd;
51113906Sbrandon.potter@amd.com        assert(!hostCwd.empty());
51213906Sbrandon.potter@amd.com    } else {
51313906Sbrandon.potter@amd.com        path_base = tgtCwd;
51413906Sbrandon.potter@amd.com        assert(!tgtCwd.empty());
51513906Sbrandon.potter@amd.com    }
51613883Sdavid.hashe@amd.com
51713883Sdavid.hashe@amd.com    // Add a trailing '/' if the current working directory did not have one.
51813883Sdavid.hashe@amd.com    normalize(path_base);
51913883Sdavid.hashe@amd.com
52013883Sdavid.hashe@amd.com    // Append the filename onto the current working path.
52113883Sdavid.hashe@amd.com    auto absolute_path = path_base + filename;
52213883Sdavid.hashe@amd.com
52313883Sdavid.hashe@amd.com    return absolute_path;
52413883Sdavid.hashe@amd.com}
52513883Sdavid.hashe@amd.com
52611851Sbrandon.potter@amd.comProcess *
52711851Sbrandon.potter@amd.comProcessParams::create()
5282715Sstever@eecs.umich.edu{
52911919SBrandon.Potter@amd.com    Process *process = nullptr;
5302715Sstever@eecs.umich.edu
53111140Sjthestness@gmail.com    // If not specified, set the executable parameter equal to the
53211140Sjthestness@gmail.com    // simulated system's zeroth command line parameter
53311851Sbrandon.potter@amd.com    if (executable == "") {
53411851Sbrandon.potter@amd.com        executable = cmd[0];
53511140Sjthestness@gmail.com    }
53611140Sjthestness@gmail.com
53711851Sbrandon.potter@amd.com    ObjectFile *obj_file = createObjectFile(executable);
53813990Sgabeblack@google.com    fatal_if(!obj_file, "Can't load object file %s", executable);
5392715Sstever@eecs.umich.edu
54013990Sgabeblack@google.com    process = ObjectFile::tryLoaders(this, obj_file);
54113990Sgabeblack@google.com    fatal_if(!process, "Unknown error creating process object.");
5425753Ssteve.reinhardt@amd.com
5432715Sstever@eecs.umich.edu    return process;
5442715Sstever@eecs.umich.edu}
545