process.cc revision 5154
12SN/A/*
21762SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292665Ssaidi@eecs.umich.edu *          Steve Reinhardt
302665Ssaidi@eecs.umich.edu *          Ali Saidi
312SN/A */
322SN/A
332SN/A#include <unistd.h>
342SN/A#include <fcntl.h>
352SN/A
362SN/A#include <string>
373971Sgblack@eecs.umich.edu
3856SN/A#include "arch/remote_gdb.hh"
3956SN/A#include "base/intmath.hh"
401158SN/A#include "base/loader/object_file.hh"
41146SN/A#include "base/loader/symtab.hh"
421858SN/A#include "base/statistics.hh"
432680Sktlim@umich.edu#include "config/full_system.hh"
442378SN/A#include "cpu/thread_context.hh"
452522SN/A#include "mem/page_table.hh"
462401SN/A#include "mem/physical.hh"
475154Sgblack@eecs.umich.edu#include "mem/translating_port.hh"
484762Snate@binkert.org#include "params/Process.hh"
495512SMichael.Adler@intel.com#include "params/LiveProcess.hh"
50360SN/A#include "sim/process.hh"
514434Ssaidi@eecs.umich.edu#include "sim/process_impl.hh"
52695SN/A#include "sim/stats.hh"
532093SN/A#include "sim/syscall_emul.hh"
542378SN/A#include "sim/system.hh"
552SN/A
562715Sstever@eecs.umich.edu#include "arch/isa_specific.hh"
572715Sstever@eecs.umich.edu#if THE_ISA == ALPHA_ISA
582715Sstever@eecs.umich.edu#include "arch/alpha/linux/process.hh"
592715Sstever@eecs.umich.edu#include "arch/alpha/tru64/process.hh"
602715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
612715Sstever@eecs.umich.edu#include "arch/sparc/linux/process.hh"
622715Sstever@eecs.umich.edu#include "arch/sparc/solaris/process.hh"
632715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
642715Sstever@eecs.umich.edu#include "arch/mips/linux/process.hh"
655335Shines@cs.fsu.edu#elif THE_ISA == X86_ISA
665335Shines@cs.fsu.edu#include "arch/x86/linux/process.hh"
674157Sgblack@eecs.umich.edu#else
684166Sgblack@eecs.umich.edu#error "THE_ISA not set"
692715Sstever@eecs.umich.edu#endif
702715Sstever@eecs.umich.edu
712715Sstever@eecs.umich.edu
722715Sstever@eecs.umich.eduusing namespace std;
732715Sstever@eecs.umich.eduusing namespace TheISA;
742SN/A
752107SN/A//
762SN/A// The purpose of this code is to fake the loader & syscall mechanism
772SN/A// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
782SN/A// mode when we do have an OS
792SN/A//
802SN/A#if FULL_SYSTEM
812SN/A#error "process.cc not compatible with FULL_SYSTEM"
821858SN/A#endif
83360SN/A
842SN/A// current number of allocated processes
852SN/Aint num_processes = 0;
862SN/A
872SN/AProcess::Process(ProcessParams * params)
882SN/A    : SimObject(params), system(params->system),
895758Shsul@eecs.umich.edu    max_stack_size(params->max_stack_size)
905771Shsul@eecs.umich.edu{
915758Shsul@eecs.umich.edu    string in = params->input;
925758Shsul@eecs.umich.edu    string out = params->output;
935758Shsul@eecs.umich.edu
945758Shsul@eecs.umich.edu    // initialize file descriptors to default: same as simulator
955758Shsul@eecs.umich.edu    int stdin_fd, stdout_fd, stderr_fd;
965771Shsul@eecs.umich.edu
975771Shsul@eecs.umich.edu    if (in == "stdin" || in == "cin")
985758Shsul@eecs.umich.edu        stdin_fd = STDIN_FILENO;
995154Sgblack@eecs.umich.edu    else if (in == "None")
1005183Ssaidi@eecs.umich.edu        stdin_fd = -1;
1015154Sgblack@eecs.umich.edu    else
1022SN/A        stdin_fd = Process::openInputFile(in);
1035154Sgblack@eecs.umich.edu
1045154Sgblack@eecs.umich.edu    if (out == "stdout" || out == "cout")
1055514SMichael.Adler@intel.com        stdout_fd = STDOUT_FILENO;
1065154Sgblack@eecs.umich.edu    else if (out == "stderr" || out == "cerr")
1075154Sgblack@eecs.umich.edu        stdout_fd = STDERR_FILENO;
1085154Sgblack@eecs.umich.edu    else if (out == "None")
1095154Sgblack@eecs.umich.edu        stdout_fd = -1;
1105154Sgblack@eecs.umich.edu    else
1115154Sgblack@eecs.umich.edu        stdout_fd = Process::openOutputFile(out);
1125154Sgblack@eecs.umich.edu
1135154Sgblack@eecs.umich.edu    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
1145154Sgblack@eecs.umich.edu
1155154Sgblack@eecs.umich.edu    M5_pid = system->allocatePID();
1165154Sgblack@eecs.umich.edu    // initialize first 3 fds (stdin, stdout, stderr)
1175154Sgblack@eecs.umich.edu    fd_map[STDIN_FILENO] = stdin_fd;
1185154Sgblack@eecs.umich.edu    fd_map[STDOUT_FILENO] = stdout_fd;
1195154Sgblack@eecs.umich.edu    fd_map[STDERR_FILENO] = stderr_fd;
1205154Sgblack@eecs.umich.edu
1215154Sgblack@eecs.umich.edu    // mark remaining fds as free
1225154Sgblack@eecs.umich.edu    for (int i = 3; i <= MAX_FD; ++i) {
1235154Sgblack@eecs.umich.edu        fd_map[i] = -1;
1245154Sgblack@eecs.umich.edu    }
1255154Sgblack@eecs.umich.edu
1265514SMichael.Adler@intel.com    mmap_start = mmap_end = 0;
1275514SMichael.Adler@intel.com    nxm_start = nxm_end = 0;
1285514SMichael.Adler@intel.com    pTable = new PageTable(system);
1295514SMichael.Adler@intel.com    // other parameters will be initialized when the program is loaded
1305514SMichael.Adler@intel.com}
1315514SMichael.Adler@intel.com
1325514SMichael.Adler@intel.com
1335514SMichael.Adler@intel.comvoid
1345514SMichael.Adler@intel.comProcess::regStats()
1355514SMichael.Adler@intel.com{
1365154Sgblack@eecs.umich.edu    using namespace Stats;
1374997Sgblack@eecs.umich.edu
1382SN/A    num_syscalls
1395282Srstrong@cs.ucsd.edu        .name(name() + ".PROG:num_syscalls")
1405282Srstrong@cs.ucsd.edu        .desc("Number of system calls")
1415282Srstrong@cs.ucsd.edu        ;
1425282Srstrong@cs.ucsd.edu}
1435282Srstrong@cs.ucsd.edu
1445282Srstrong@cs.ucsd.edu//
1455282Srstrong@cs.ucsd.edu// static helper functions
1465282Srstrong@cs.ucsd.edu//
1475282Srstrong@cs.ucsd.eduint
1485282Srstrong@cs.ucsd.eduProcess::openInputFile(const string &filename)
1495282Srstrong@cs.ucsd.edu{
1505282Srstrong@cs.ucsd.edu    int fd = open(filename.c_str(), O_RDONLY);
1515282Srstrong@cs.ucsd.edu
1525282Srstrong@cs.ucsd.edu    if (fd == -1) {
1535282Srstrong@cs.ucsd.edu        perror(NULL);
1545282Srstrong@cs.ucsd.edu        cerr << "unable to open \"" << filename << "\" for reading\n";
1555514SMichael.Adler@intel.com        fatal("can't open input file");
1565282Srstrong@cs.ucsd.edu    }
1575282Srstrong@cs.ucsd.edu
1585282Srstrong@cs.ucsd.edu    return fd;
1595282Srstrong@cs.ucsd.edu}
1602SN/A
1612SN/A
1622SN/Aint
1635282Srstrong@cs.ucsd.eduProcess::openOutputFile(const string &filename)
1645282Srstrong@cs.ucsd.edu{
1652SN/A    int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
1662SN/A
1671450SN/A    if (fd == -1) {
1681514SN/A        perror(NULL);
1695184Sgblack@eecs.umich.edu        cerr << "unable to open \"" << filename << "\" for writing\n";
1702SN/A        fatal("can't open output file");
1712SN/A    }
1722SN/A
1732378SN/A    return fd;
1742SN/A}
1752SN/A
1762SN/A
177729SN/Aint
1782SN/AProcess::registerThreadContext(ThreadContext *tc)
1792SN/A{
1802SN/A    // add to list
1812SN/A    int myIndex = threadContexts.size();
1822SN/A    threadContexts.push_back(tc);
1832SN/A
1842SN/A    RemoteGDB *rgdb = new RemoteGDB(system, tc);
1852SN/A    GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex);
1862SN/A    gdbl->listen();
1872SN/A    //gdbl->accept();
1882SN/A
1892SN/A    remoteGDB.push_back(rgdb);
1902SN/A
1912SN/A    // return CPU number to caller
1922SN/A    return myIndex;
1932SN/A}
1942SN/A
1952SN/Avoid
1962SN/AProcess::startup()
1972SN/A{
1982SN/A    if (threadContexts.empty())
1992SN/A        fatal("Process %s is not associated with any CPUs!\n", name());
2002SN/A
2012SN/A    // first thread context for this process... initialize & enable
2022SN/A    ThreadContext *tc = threadContexts[0];
2032SN/A
2042SN/A    // mark this context as active so it will start ticking.
2052SN/A    tc->activate(0);
2065514SMichael.Adler@intel.com
2072SN/A    Port *mem_port;
2082SN/A    mem_port = system->physmem->getPort("functional");
2092SN/A    initVirtMem = new TranslatingPort("process init port", this,
2102SN/A            TranslatingPort::Always);
2112SN/A    mem_port->setPeer(initVirtMem);
2122SN/A    initVirtMem->setPeer(mem_port);
2132SN/A}
2142SN/A
2152SN/Avoid
2162SN/AProcess::replaceThreadContext(ThreadContext *tc, int tcIndex)
2175713Shsul@eecs.umich.edu{
2185713Shsul@eecs.umich.edu    if (tcIndex >= threadContexts.size()) {
2192SN/A        panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
2205713Shsul@eecs.umich.edu              tcIndex, threadContexts.size());
2215713Shsul@eecs.umich.edu    }
2225713Shsul@eecs.umich.edu
2235713Shsul@eecs.umich.edu    threadContexts[tcIndex] = tc;
2246029Ssteve.reinhardt@amd.com}
2255713Shsul@eecs.umich.edu
2265713Shsul@eecs.umich.edu// map simulator fd sim_fd to target fd tgt_fd
2275713Shsul@eecs.umich.eduvoid
2285512SMichael.Adler@intel.comProcess::dup_fd(int sim_fd, int tgt_fd)
2295713Shsul@eecs.umich.edu{
2302SN/A    if (tgt_fd < 0 || tgt_fd > MAX_FD)
2312SN/A        panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
2321395SN/A
2331395SN/A    fd_map[tgt_fd] = sim_fd;
2341395SN/A}
2355713Shsul@eecs.umich.edu
2365713Shsul@eecs.umich.edu
2372378SN/A// generate new target fd for sim_fd
2382680Sktlim@umich.eduint
2395713Shsul@eecs.umich.eduProcess::alloc_fd(int sim_fd)
2401395SN/A{
2411634SN/A    // in case open() returns an error, don't allocate a new fd
2422680Sktlim@umich.edu    if (sim_fd == -1)
2432462SN/A        return -1;
2442519SN/A
2452519SN/A    // find first free target fd
2464434Ssaidi@eecs.umich.edu    for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) {
2474434Ssaidi@eecs.umich.edu        if (fd_map[free_fd] == -1) {
2482519SN/A            fd_map[free_fd] = sim_fd;
2492519SN/A            return free_fd;
2501395SN/A        }
2512SN/A    }
2522SN/A
2532SN/A    panic("Process::alloc_fd: out of file descriptors!");
2542SN/A}
2552SN/A
2562SN/A
2572SN/A// free target fd (e.g., after close)
2582SN/Avoid
2595282Srstrong@cs.ucsd.eduProcess::free_fd(int tgt_fd)
2605282Srstrong@cs.ucsd.edu{
2612SN/A    if (fd_map[tgt_fd] == -1)
2622SN/A        warn("Process::free_fd: request to free unused fd %d", tgt_fd);
2632SN/A
2642SN/A    fd_map[tgt_fd] = -1;
2652SN/A}
2665282Srstrong@cs.ucsd.edu
2672SN/A
2682SN/A// look up simulator fd for given target fd
2692SN/Aint
2702SN/AProcess::sim_fd(int tgt_fd)
2712SN/A{
2722SN/A    if (tgt_fd > MAX_FD)
2735282Srstrong@cs.ucsd.edu        return -1;
2745282Srstrong@cs.ucsd.edu
2755282Srstrong@cs.ucsd.edu    return fd_map[tgt_fd];
2765282Srstrong@cs.ucsd.edu}
2775282Srstrong@cs.ucsd.edu
2785282Srstrong@cs.ucsd.edubool
2795282Srstrong@cs.ucsd.eduProcess::checkAndAllocNextPage(Addr vaddr)
2805282Srstrong@cs.ucsd.edu{
2815282Srstrong@cs.ucsd.edu    // if this is an initial write we might not have
2825282Srstrong@cs.ucsd.edu    if (vaddr >= stack_min && vaddr < stack_base) {
2831970SN/A        pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
2841970SN/A        return true;
2852SN/A    }
2862SN/A
2871970SN/A    // We've accessed the next page of the stack, so extend the stack
2881970SN/A    // to cover it.
2892SN/A    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
2901970SN/A        while (vaddr < stack_min) {
2911970SN/A            stack_min -= TheISA::PageBytes;
2921970SN/A            if(stack_base - stack_min > max_stack_size)
2931970SN/A                fatal("Maximum stack size exceeded\n");
2941970SN/A            if(stack_base - stack_min > 8*1024*1024)
2955282Srstrong@cs.ucsd.edu                fatal("Over max stack size for one thread\n");
2965282Srstrong@cs.ucsd.edu            pTable->allocate(stack_min, TheISA::PageBytes);
2971970SN/A            warn("Increasing stack size by one page.");
2981970SN/A        };
2995282Srstrong@cs.ucsd.edu        return true;
3005282Srstrong@cs.ucsd.edu    }
3015282Srstrong@cs.ucsd.edu    return false;
3025282Srstrong@cs.ucsd.edu}
3035282Srstrong@cs.ucsd.edu
3045282Srstrong@cs.ucsd.eduvoid
3055282Srstrong@cs.ucsd.eduProcess::serialize(std::ostream &os)
3062SN/A{
3072SN/A    SERIALIZE_SCALAR(initialContextLoaded);
3082SN/A    SERIALIZE_SCALAR(brk_point);
3092SN/A    SERIALIZE_SCALAR(stack_base);
3102SN/A    SERIALIZE_SCALAR(stack_size);
3112SN/A    SERIALIZE_SCALAR(stack_min);
3122SN/A    SERIALIZE_SCALAR(next_thread_stack_base);
3132SN/A    SERIALIZE_SCALAR(mmap_start);
3142SN/A    SERIALIZE_SCALAR(mmap_end);
3152SN/A    SERIALIZE_SCALAR(nxm_start);
3165282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(nxm_end);
3172SN/A    SERIALIZE_ARRAY(fd_map, MAX_FD);
3182SN/A
3195282Srstrong@cs.ucsd.edu    pTable->serialize(os);
3205282Srstrong@cs.ucsd.edu}
3215282Srstrong@cs.ucsd.edu
3225282Srstrong@cs.ucsd.eduvoid
3235282Srstrong@cs.ucsd.eduProcess::unserialize(Checkpoint *cp, const std::string &section)
3245282Srstrong@cs.ucsd.edu{
3255282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(initialContextLoaded);
3265282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(brk_point);
3274434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(stack_base);
3284434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(stack_size);
3294434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(stack_min);
3304434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(next_thread_stack_base);
3314434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(mmap_start);
3324434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(mmap_end);
3334434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(nxm_start);
3344434Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(nxm_end);
3354434Ssaidi@eecs.umich.edu    UNSERIALIZE_ARRAY(fd_map, MAX_FD);
3364434Ssaidi@eecs.umich.edu
3374434Ssaidi@eecs.umich.edu    pTable->unserialize(cp, section);
3385154Sgblack@eecs.umich.edu}
3395154Sgblack@eecs.umich.edu
3405154Sgblack@eecs.umich.edu
3415154Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////////
3425154Sgblack@eecs.umich.edu//
3435154Sgblack@eecs.umich.edu// LiveProcess member definitions
3445154Sgblack@eecs.umich.edu//
3455154Sgblack@eecs.umich.edu////////////////////////////////////////////////////////////////////////
3465823Ssaidi@eecs.umich.edu
3475154Sgblack@eecs.umich.edu
3484434Ssaidi@eecs.umich.eduLiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile)
3494434Ssaidi@eecs.umich.edu    : Process(params), objFile(_objFile),
3504434Ssaidi@eecs.umich.edu      argv(params->cmd), envp(params->env), cwd(params->cwd)
3514434Ssaidi@eecs.umich.edu{
3524434Ssaidi@eecs.umich.edu    __uid = params->uid;
3535282Srstrong@cs.ucsd.edu    __euid = params->euid;
3545282Srstrong@cs.ucsd.edu    __gid = params->gid;
3555282Srstrong@cs.ucsd.edu    __egid = params->egid;
3565282Srstrong@cs.ucsd.edu    __pid = params->pid;
3575282Srstrong@cs.ucsd.edu    __ppid = params->ppid;
3585282Srstrong@cs.ucsd.edu
3595282Srstrong@cs.ucsd.edu    prog_fname = params->cmd[0];
3605282Srstrong@cs.ucsd.edu
3615514SMichael.Adler@intel.com    // load up symbols, if any... these may be used for debugging or
3625282Srstrong@cs.ucsd.edu    // profiling.
3635282Srstrong@cs.ucsd.edu    if (!debugSymbolTable) {
3645282Srstrong@cs.ucsd.edu        debugSymbolTable = new SymbolTable();
3655282Srstrong@cs.ucsd.edu        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
3665282Srstrong@cs.ucsd.edu            !objFile->loadLocalSymbols(debugSymbolTable)) {
3675282Srstrong@cs.ucsd.edu            // didn't load any symbols
3685282Srstrong@cs.ucsd.edu            delete debugSymbolTable;
3695282Srstrong@cs.ucsd.edu            debugSymbolTable = NULL;
3705282Srstrong@cs.ucsd.edu        }
3715282Srstrong@cs.ucsd.edu    }
3725282Srstrong@cs.ucsd.edu}
3735282Srstrong@cs.ucsd.edu
3745282Srstrong@cs.ucsd.eduvoid
3755282Srstrong@cs.ucsd.eduLiveProcess::argsInit(int intSize, int pageSize)
3765282Srstrong@cs.ucsd.edu{
3775282Srstrong@cs.ucsd.edu    Process::startup();
3785282Srstrong@cs.ucsd.edu
3795282Srstrong@cs.ucsd.edu    // load object file into target memory
3805282Srstrong@cs.ucsd.edu    objFile->loadSections(initVirtMem);
3815282Srstrong@cs.ucsd.edu
3825282Srstrong@cs.ucsd.edu    // Calculate how much space we need for arg & env arrays.
3835282Srstrong@cs.ucsd.edu    int argv_array_size = intSize * (argv.size() + 1);
3845282Srstrong@cs.ucsd.edu    int envp_array_size = intSize * (envp.size() + 1);
3855514SMichael.Adler@intel.com    int arg_data_size = 0;
3865514SMichael.Adler@intel.com    for (int i = 0; i < argv.size(); ++i) {
3875282Srstrong@cs.ucsd.edu        arg_data_size += argv[i].size() + 1;
3885282Srstrong@cs.ucsd.edu    }
3895514SMichael.Adler@intel.com    int env_data_size = 0;
3905514SMichael.Adler@intel.com    for (int i = 0; i < envp.size(); ++i) {
3915514SMichael.Adler@intel.com        env_data_size += envp[i].size() + 1;
3925514SMichael.Adler@intel.com    }
3935514SMichael.Adler@intel.com
3945514SMichael.Adler@intel.com    int space_needed =
3955514SMichael.Adler@intel.com        argv_array_size + envp_array_size + arg_data_size + env_data_size;
3965514SMichael.Adler@intel.com    if (space_needed < 32*1024)
3975514SMichael.Adler@intel.com        space_needed = 32*1024;
3985514SMichael.Adler@intel.com
3995514SMichael.Adler@intel.com    // set bottom of stack
4005514SMichael.Adler@intel.com    stack_min = stack_base - space_needed;
4015514SMichael.Adler@intel.com    // align it
4025282Srstrong@cs.ucsd.edu    stack_min = roundDown(stack_min, pageSize);
4035282Srstrong@cs.ucsd.edu    stack_size = stack_base - stack_min;
4045282Srstrong@cs.ucsd.edu    // map memory
4055282Srstrong@cs.ucsd.edu    pTable->allocate(stack_min, roundUp(stack_size, pageSize));
4065282Srstrong@cs.ucsd.edu
4075282Srstrong@cs.ucsd.edu    // map out initial stack contents
4085282Srstrong@cs.ucsd.edu    Addr argv_array_base = stack_min + intSize; // room for argc
4095282Srstrong@cs.ucsd.edu    Addr envp_array_base = argv_array_base + argv_array_size;
4105282Srstrong@cs.ucsd.edu    Addr arg_data_base = envp_array_base + envp_array_size;
4115282Srstrong@cs.ucsd.edu    Addr env_data_base = arg_data_base + arg_data_size;
4125282Srstrong@cs.ucsd.edu
4135282Srstrong@cs.ucsd.edu    // write contents to stack
4145282Srstrong@cs.ucsd.edu    uint64_t argc = argv.size();
4155282Srstrong@cs.ucsd.edu    if (intSize == 8)
4165282Srstrong@cs.ucsd.edu        argc = htog((uint64_t)argc);
4175282Srstrong@cs.ucsd.edu    else if (intSize == 4)
4185282Srstrong@cs.ucsd.edu        argc = htog((uint32_t)argc);
4195282Srstrong@cs.ucsd.edu    else
4205282Srstrong@cs.ucsd.edu        panic("Unknown int size");
4215282Srstrong@cs.ucsd.edu
4225282Srstrong@cs.ucsd.edu    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
4235282Srstrong@cs.ucsd.edu
4245282Srstrong@cs.ucsd.edu    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
4255282Srstrong@cs.ucsd.edu    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
4265282Srstrong@cs.ucsd.edu
4275282Srstrong@cs.ucsd.edu    assert(NumArgumentRegs >= 2);
4285282Srstrong@cs.ucsd.edu    threadContexts[0]->setIntReg(ArgumentReg[0], argc);
4295282Srstrong@cs.ucsd.edu    threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
4305282Srstrong@cs.ucsd.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
4315282Srstrong@cs.ucsd.edu
4325282Srstrong@cs.ucsd.edu    Addr prog_entry = objFile->entryPoint();
4335282Srstrong@cs.ucsd.edu    threadContexts[0]->setPC(prog_entry);
4345282Srstrong@cs.ucsd.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
4355282Srstrong@cs.ucsd.edu
4365282Srstrong@cs.ucsd.edu#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
4375282Srstrong@cs.ucsd.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
4385282Srstrong@cs.ucsd.edu#endif
4395282Srstrong@cs.ucsd.edu
4405282Srstrong@cs.ucsd.edu    num_processes++;
4415282Srstrong@cs.ucsd.edu}
4425282Srstrong@cs.ucsd.edu
4435282Srstrong@cs.ucsd.eduvoid
4445282Srstrong@cs.ucsd.eduLiveProcess::syscall(int64_t callnum, ThreadContext *tc)
4455282Srstrong@cs.ucsd.edu{
4465282Srstrong@cs.ucsd.edu    num_syscalls++;
4475282Srstrong@cs.ucsd.edu
4485282Srstrong@cs.ucsd.edu    SyscallDesc *desc = getDesc(callnum);
4495282Srstrong@cs.ucsd.edu    if (desc == NULL)
4505282Srstrong@cs.ucsd.edu        fatal("Syscall %d out of range", callnum);
4515282Srstrong@cs.ucsd.edu
4525282Srstrong@cs.ucsd.edu    desc->doSyscall(callnum, this, tc);
4535282Srstrong@cs.ucsd.edu}
4545282Srstrong@cs.ucsd.edu
4555282Srstrong@cs.ucsd.eduLiveProcess *
4565282Srstrong@cs.ucsd.eduLiveProcess::create(LiveProcessParams * params)
4575282Srstrong@cs.ucsd.edu{
4585282Srstrong@cs.ucsd.edu    LiveProcess *process = NULL;
4595282Srstrong@cs.ucsd.edu
4605282Srstrong@cs.ucsd.edu    string executable =
4615282Srstrong@cs.ucsd.edu        params->executable == "" ? params->cmd[0] : params->executable;
4625282Srstrong@cs.ucsd.edu    ObjectFile *objFile = createObjectFile(executable);
4635282Srstrong@cs.ucsd.edu    if (objFile == NULL) {
4645282Srstrong@cs.ucsd.edu        fatal("Can't load object file %s", executable);
4655282Srstrong@cs.ucsd.edu    }
4665282Srstrong@cs.ucsd.edu
4675282Srstrong@cs.ucsd.edu    if (objFile->isDynamic())
4685282Srstrong@cs.ucsd.edu       fatal("Object file is a dynamic executable however only static "
4695282Srstrong@cs.ucsd.edu             "executables are supported!\n       Please recompile your "
4705282Srstrong@cs.ucsd.edu             "executable as a static binary and try again.\n");
4715282Srstrong@cs.ucsd.edu
4725282Srstrong@cs.ucsd.edu#if THE_ISA == ALPHA_ISA
4735282Srstrong@cs.ucsd.edu    if (objFile->hasTLS())
4745282Srstrong@cs.ucsd.edu        fatal("Object file has a TLS section and single threaded TLS is not\n"
4755282Srstrong@cs.ucsd.edu              "       currently supported for Alpha! Please recompile your "
4765282Srstrong@cs.ucsd.edu              "executable with \n       a non-TLS toolchain.\n");
4775282Srstrong@cs.ucsd.edu
4785282Srstrong@cs.ucsd.edu    if (objFile->getArch() != ObjectFile::Alpha)
4795282Srstrong@cs.ucsd.edu        fatal("Object file architecture does not match compiled ISA (Alpha).");
4805282Srstrong@cs.ucsd.edu    switch (objFile->getOpSys()) {
4815282Srstrong@cs.ucsd.edu      case ObjectFile::Tru64:
4825282Srstrong@cs.ucsd.edu        process = new AlphaTru64Process(params, objFile);
4835282Srstrong@cs.ucsd.edu        break;
4845282Srstrong@cs.ucsd.edu
4855282Srstrong@cs.ucsd.edu      case ObjectFile::Linux:
4865282Srstrong@cs.ucsd.edu        process = new AlphaLinuxProcess(params, objFile);
4873311Ssaidi@eecs.umich.edu        break;
4883311Ssaidi@eecs.umich.edu
4893311Ssaidi@eecs.umich.edu      default:
4903311Ssaidi@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
4913311Ssaidi@eecs.umich.edu    }
4923311Ssaidi@eecs.umich.edu#elif THE_ISA == SPARC_ISA
4933311Ssaidi@eecs.umich.edu    if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32)
4943311Ssaidi@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (SPARC).");
4953311Ssaidi@eecs.umich.edu    switch (objFile->getOpSys()) {
4963311Ssaidi@eecs.umich.edu      case ObjectFile::Linux:
4973311Ssaidi@eecs.umich.edu        if (objFile->getArch() == ObjectFile::SPARC64) {
4983311Ssaidi@eecs.umich.edu            process = new Sparc64LinuxProcess(params, objFile);
4993311Ssaidi@eecs.umich.edu        } else {
5005282Srstrong@cs.ucsd.edu            process = new Sparc32LinuxProcess(params, objFile);
5015282Srstrong@cs.ucsd.edu        }
5025282Srstrong@cs.ucsd.edu        break;
5035282Srstrong@cs.ucsd.edu
5045282Srstrong@cs.ucsd.edu
5055282Srstrong@cs.ucsd.edu      case ObjectFile::Solaris:
5063311Ssaidi@eecs.umich.edu        process = new SparcSolarisProcess(params, objFile);
5073311Ssaidi@eecs.umich.edu        break;
5083311Ssaidi@eecs.umich.edu      default:
5093311Ssaidi@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5103311Ssaidi@eecs.umich.edu    }
5113311Ssaidi@eecs.umich.edu#elif THE_ISA == X86_ISA
5123311Ssaidi@eecs.umich.edu    if (objFile->getArch() != ObjectFile::X86)
5133311Ssaidi@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
5143311Ssaidi@eecs.umich.edu    switch (objFile->getOpSys()) {
5153311Ssaidi@eecs.umich.edu      case ObjectFile::Linux:
5163311Ssaidi@eecs.umich.edu        process = new X86LinuxProcess(params, objFile);
5173311Ssaidi@eecs.umich.edu        break;
5183311Ssaidi@eecs.umich.edu      default:
5193311Ssaidi@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5203311Ssaidi@eecs.umich.edu    }
5213311Ssaidi@eecs.umich.edu#elif THE_ISA == MIPS_ISA
5223311Ssaidi@eecs.umich.edu    if (objFile->getArch() != ObjectFile::Mips)
5235282Srstrong@cs.ucsd.edu        fatal("Object file architecture does not match compiled ISA (MIPS).");
5245282Srstrong@cs.ucsd.edu    switch (objFile->getOpSys()) {
5255282Srstrong@cs.ucsd.edu      case ObjectFile::Linux:
5265282Srstrong@cs.ucsd.edu        process = new MipsLinuxProcess(params, objFile);
5275183Ssaidi@eecs.umich.edu        break;
5285183Ssaidi@eecs.umich.edu
5295183Ssaidi@eecs.umich.edu      default:
5303311Ssaidi@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5312SN/A    }
5322SN/A#else
5332SN/A#error "THE_ISA not set"
5342SN/A#endif
5352SN/A
5362SN/A
5372SN/A    if (process == NULL)
5382SN/A        fatal("Unknown error creating process object.");
53912SN/A    return process;
5405154Sgblack@eecs.umich.edu}
5415154Sgblack@eecs.umich.edu
5425154Sgblack@eecs.umich.eduLiveProcess *
5432SN/ALiveProcessParams::create()
5445154Sgblack@eecs.umich.edu{
5455154Sgblack@eecs.umich.edu    return LiveProcess::create(this);
5465154Sgblack@eecs.umich.edu}
5475154Sgblack@eecs.umich.edu