process.cc revision 5154
11689SN/A/*
29444SAndreas.Sandberg@ARM.com * Copyright (c) 2001-2005 The Regents of The University of Michigan
38707Sandreas.hansson@arm.com * All rights reserved.
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
138707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
157897Shestness@cs.utexas.edu *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271689SN/A *
281689SN/A * Authors: Nathan Binkert
291689SN/A *          Steve Reinhardt
301689SN/A *          Ali Saidi
311689SN/A */
321689SN/A
331689SN/A#include <unistd.h>
341689SN/A#include <fcntl.h>
351689SN/A
361689SN/A#include <string>
371689SN/A
381689SN/A#include "arch/remote_gdb.hh"
391689SN/A#include "base/intmath.hh"
402665Ssaidi@eecs.umich.edu#include "base/loader/object_file.hh"
412665Ssaidi@eecs.umich.edu#include "base/loader/symtab.hh"
422756Sksewell@umich.edu#include "base/statistics.hh"
437897Shestness@cs.utexas.edu#include "config/full_system.hh"
441689SN/A#include "cpu/thread_context.hh"
451689SN/A#include "mem/page_table.hh"
462325SN/A#include "mem/physical.hh"
472325SN/A#include "mem/translating_port.hh"
481060SN/A#include "params/Process.hh"
491060SN/A#include "params/LiveProcess.hh"
501060SN/A#include "sim/process.hh"
512292SN/A#include "sim/process_impl.hh"
522292SN/A#include "sim/stats.hh"
531681SN/A#include "sim/syscall_emul.hh"
541060SN/A#include "sim/system.hh"
552980Sgblack@eecs.umich.edu
561060SN/A#include "arch/isa_specific.hh"
576658Snate@binkert.org#if THE_ISA == ALPHA_ISA
581717SN/A#include "arch/alpha/linux/process.hh"
591717SN/A#include "arch/alpha/tru64/process.hh"
602292SN/A#elif THE_ISA == SPARC_ISA
612292SN/A#include "arch/sparc/linux/process.hh"
628229Snate@binkert.org#include "arch/sparc/solaris/process.hh"
638229Snate@binkert.org#elif THE_ISA == MIPS_ISA
648229Snate@binkert.org#include "arch/mips/linux/process.hh"
658229Snate@binkert.org#elif THE_ISA == X86_ISA
662817Sksewell@umich.edu#include "arch/x86/linux/process.hh"
678229Snate@binkert.org#else
681060SN/A#error "THE_ISA not set"
691060SN/A#endif
702316SN/A
712316SN/A
722680Sktlim@umich.eduusing namespace std;
732817Sksewell@umich.eduusing namespace TheISA;
742817Sksewell@umich.edu
752843Sktlim@umich.edu//
762843Sktlim@umich.edu// The purpose of this code is to fake the loader & syscall mechanism
772669Sktlim@umich.edu// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
781060SN/A// mode when we do have an OS
791060SN/A//
808737Skoansin.tan@gmail.com#if FULL_SYSTEM
815529Snate@binkert.org#error "process.cc not compatible with FULL_SYSTEM"
822733Sktlim@umich.edu#endif
831060SN/A
841060SN/A// current number of allocated processes
851060SN/Aint num_processes = 0;
865529Snate@binkert.org
872292SN/AProcess::Process(ProcessParams * params)
882292SN/A    : SimObject(params), system(params->system),
891060SN/A    max_stack_size(params->max_stack_size)
901060SN/A{
912348SN/A    string in = params->input;
922348SN/A    string out = params->output;
932348SN/A
942348SN/A    // initialize file descriptors to default: same as simulator
952348SN/A    int stdin_fd, stdout_fd, stderr_fd;
961060SN/A
972733Sktlim@umich.edu    if (in == "stdin" || in == "cin")
981060SN/A        stdin_fd = STDIN_FILENO;
991060SN/A    else if (in == "None")
1002325SN/A        stdin_fd = -1;
1011060SN/A    else
1021061SN/A        stdin_fd = Process::openInputFile(in);
1034329Sktlim@umich.edu
1041060SN/A    if (out == "stdout" || out == "cout")
1055595Sgblack@eecs.umich.edu        stdout_fd = STDOUT_FILENO;
1062292SN/A    else if (out == "stderr" || out == "cerr")
1072292SN/A        stdout_fd = STDERR_FILENO;
1082292SN/A    else if (out == "None")
1092292SN/A        stdout_fd = -1;
1102817Sksewell@umich.edu    else
1112829Sksewell@umich.edu        stdout_fd = Process::openOutputFile(out);
1121060SN/A
1131060SN/A    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
1141060SN/A
1151060SN/A    M5_pid = system->allocatePID();
1161060SN/A    // initialize first 3 fds (stdin, stdout, stderr)
1172307SN/A    fd_map[STDIN_FILENO] = stdin_fd;
1182307SN/A    fd_map[STDOUT_FILENO] = stdout_fd;
1191060SN/A    fd_map[STDERR_FILENO] = stderr_fd;
1201060SN/A
1216022Sgblack@eecs.umich.edu    // mark remaining fds as free
1226022Sgblack@eecs.umich.edu    for (int i = 3; i <= MAX_FD; ++i) {
1233781Sgblack@eecs.umich.edu        fd_map[i] = -1;
1242292SN/A    }
1251060SN/A
1261060SN/A    mmap_start = mmap_end = 0;
1271060SN/A    nxm_start = nxm_end = 0;
1288707Sandreas.hansson@arm.com    pTable = new PageTable(system);
1298707Sandreas.hansson@arm.com    // other parameters will be initialized when the program is loaded
1308707Sandreas.hansson@arm.com}
1318707Sandreas.hansson@arm.com
1328707Sandreas.hansson@arm.com
1338707Sandreas.hansson@arm.comvoid
1348707Sandreas.hansson@arm.comProcess::regStats()
1358707Sandreas.hansson@arm.com{
1368707Sandreas.hansson@arm.com    using namespace Stats;
1378707Sandreas.hansson@arm.com
1388707Sandreas.hansson@arm.com    num_syscalls
1398707Sandreas.hansson@arm.com        .name(name() + ".PROG:num_syscalls")
1408707Sandreas.hansson@arm.com        .desc("Number of system calls")
1419095Sandreas.hansson@arm.com        ;
1428707Sandreas.hansson@arm.com}
1438707Sandreas.hansson@arm.com
1448707Sandreas.hansson@arm.com//
1458707Sandreas.hansson@arm.com// static helper functions
1468707Sandreas.hansson@arm.com//
1478707Sandreas.hansson@arm.comint
1488975Sandreas.hansson@arm.comProcess::openInputFile(const string &filename)
1498975Sandreas.hansson@arm.com{
1508707Sandreas.hansson@arm.com    int fd = open(filename.c_str(), O_RDONLY);
1518707Sandreas.hansson@arm.com
1528707Sandreas.hansson@arm.com    if (fd == -1) {
1538707Sandreas.hansson@arm.com        perror(NULL);
1548707Sandreas.hansson@arm.com        cerr << "unable to open \"" << filename << "\" for reading\n";
1558707Sandreas.hansson@arm.com        fatal("can't open input file");
1568707Sandreas.hansson@arm.com    }
1578707Sandreas.hansson@arm.com
1588707Sandreas.hansson@arm.com    return fd;
1598707Sandreas.hansson@arm.com}
1608707Sandreas.hansson@arm.com
1618707Sandreas.hansson@arm.com
1628707Sandreas.hansson@arm.comint
1638707Sandreas.hansson@arm.comProcess::openOutputFile(const string &filename)
1648707Sandreas.hansson@arm.com{
1658707Sandreas.hansson@arm.com    int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
1668707Sandreas.hansson@arm.com
1678707Sandreas.hansson@arm.com    if (fd == -1) {
1689095Sandreas.hansson@arm.com        perror(NULL);
1698707Sandreas.hansson@arm.com        cerr << "unable to open \"" << filename << "\" for writing\n";
1708707Sandreas.hansson@arm.com        fatal("can't open output file");
1718707Sandreas.hansson@arm.com    }
1728707Sandreas.hansson@arm.com
1738707Sandreas.hansson@arm.com    return fd;
1748707Sandreas.hansson@arm.com}
1758707Sandreas.hansson@arm.com
1768975Sandreas.hansson@arm.com
1778975Sandreas.hansson@arm.comint
1788707Sandreas.hansson@arm.comProcess::registerThreadContext(ThreadContext *tc)
1798707Sandreas.hansson@arm.com{
1808707Sandreas.hansson@arm.com    // add to list
1818707Sandreas.hansson@arm.com    int myIndex = threadContexts.size();
1828707Sandreas.hansson@arm.com    threadContexts.push_back(tc);
1838707Sandreas.hansson@arm.com
1848707Sandreas.hansson@arm.com    RemoteGDB *rgdb = new RemoteGDB(system, tc);
1858707Sandreas.hansson@arm.com    GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex);
1868711Sandreas.hansson@arm.com    gdbl->listen();
1878707Sandreas.hansson@arm.com    //gdbl->accept();
1888922Swilliam.wang@arm.com
1898707Sandreas.hansson@arm.com    remoteGDB.push_back(rgdb);
1908707Sandreas.hansson@arm.com
1911060SN/A    // return CPU number to caller
1921060SN/A    return myIndex;
1931060SN/A}
1942292SN/A
1951755SN/Avoid
1961060SN/AProcess::startup()
1971060SN/A{
1982292SN/A    if (threadContexts.empty())
1991755SN/A        fatal("Process %s is not associated with any CPUs!\n", name());
2002292SN/A
2012292SN/A    // first thread context for this process... initialize & enable
2021060SN/A    ThreadContext *tc = threadContexts[0];
2032292SN/A
2045336Shines@cs.fsu.edu    // mark this context as active so it will start ticking.
2051060SN/A    tc->activate(0);
2061060SN/A
2072292SN/A    Port *mem_port;
2081060SN/A    mem_port = system->physmem->getPort("functional");
2091060SN/A    initVirtMem = new TranslatingPort("process init port", this,
2102292SN/A            TranslatingPort::Always);
2119180Sandreas.hansson@arm.com    mem_port->setPeer(initVirtMem);
2121060SN/A    initVirtMem->setPeer(mem_port);
2131060SN/A}
2149179Sandreas.hansson@arm.com
2151060SN/Avoid
2169179Sandreas.hansson@arm.comProcess::replaceThreadContext(ThreadContext *tc, int tcIndex)
2171060SN/A{
2181060SN/A    if (tcIndex >= threadContexts.size()) {
2192292SN/A        panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
2201060SN/A              tcIndex, threadContexts.size());
2211060SN/A    }
2221060SN/A
2231060SN/A    threadContexts[tcIndex] = tc;
2241060SN/A}
2251060SN/A
2262829Sksewell@umich.edu// map simulator fd sim_fd to target fd tgt_fd
2272829Sksewell@umich.eduvoid
2282829Sksewell@umich.eduProcess::dup_fd(int sim_fd, int tgt_fd)
2292829Sksewell@umich.edu{
2306221Snate@binkert.org    if (tgt_fd < 0 || tgt_fd > MAX_FD)
2312829Sksewell@umich.edu        panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
2322829Sksewell@umich.edu
2332829Sksewell@umich.edu    fd_map[tgt_fd] = sim_fd;
2342829Sksewell@umich.edu}
2352829Sksewell@umich.edu
2362829Sksewell@umich.edu
2372829Sksewell@umich.edu// generate new target fd for sim_fd
2382829Sksewell@umich.eduint
2392829Sksewell@umich.eduProcess::alloc_fd(int sim_fd)
2402829Sksewell@umich.edu{
2412829Sksewell@umich.edu    // in case open() returns an error, don't allocate a new fd
2422829Sksewell@umich.edu    if (sim_fd == -1)
2432829Sksewell@umich.edu        return -1;
2442829Sksewell@umich.edu
2452829Sksewell@umich.edu    // find first free target fd
2465336Shines@cs.fsu.edu    for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) {
2472829Sksewell@umich.edu        if (fd_map[free_fd] == -1) {
2482829Sksewell@umich.edu            fd_map[free_fd] = sim_fd;
2492829Sksewell@umich.edu            return free_fd;
2506221Snate@binkert.org        }
2519180Sandreas.hansson@arm.com    }
2522829Sksewell@umich.edu
2532829Sksewell@umich.edu    panic("Process::alloc_fd: out of file descriptors!");
2542829Sksewell@umich.edu}
2555606Snate@binkert.org
2569179Sandreas.hansson@arm.com
2578518Sgeoffrey.blake@arm.com// free target fd (e.g., after close)
2589179Sandreas.hansson@arm.comvoid
2598518Sgeoffrey.blake@arm.comProcess::free_fd(int tgt_fd)
2608518Sgeoffrey.blake@arm.com{
2618518Sgeoffrey.blake@arm.com    if (fd_map[tgt_fd] == -1)
2628518Sgeoffrey.blake@arm.com        warn("Process::free_fd: request to free unused fd %d", tgt_fd);
2638518Sgeoffrey.blake@arm.com
2648518Sgeoffrey.blake@arm.com    fd_map[tgt_fd] = -1;
2658518Sgeoffrey.blake@arm.com}
2668518Sgeoffrey.blake@arm.com
2678518Sgeoffrey.blake@arm.com
2688518Sgeoffrey.blake@arm.com// look up simulator fd for given target fd
2698518Sgeoffrey.blake@arm.comint
2702829Sksewell@umich.eduProcess::sim_fd(int tgt_fd)
2712829Sksewell@umich.edu{
2722829Sksewell@umich.edu    if (tgt_fd > MAX_FD)
2736221Snate@binkert.org        return -1;
2746221Snate@binkert.org
2752829Sksewell@umich.edu    return fd_map[tgt_fd];
2762829Sksewell@umich.edu}
2772829Sksewell@umich.edu
2782829Sksewell@umich.edubool
2792829Sksewell@umich.eduProcess::checkAndAllocNextPage(Addr vaddr)
2802829Sksewell@umich.edu{
2812829Sksewell@umich.edu    // if this is an initial write we might not have
2822829Sksewell@umich.edu    if (vaddr >= stack_min && vaddr < stack_base) {
2832875Sksewell@umich.edu        pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
2842875Sksewell@umich.edu        return true;
2852875Sksewell@umich.edu    }
2863221Sktlim@umich.edu
2876221Snate@binkert.org    // We've accessed the next page of the stack, so extend the stack
2882875Sksewell@umich.edu    // to cover it.
2893221Sktlim@umich.edu    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
2903221Sktlim@umich.edu        while (vaddr < stack_min) {
2913221Sktlim@umich.edu            stack_min -= TheISA::PageBytes;
2922875Sksewell@umich.edu            if(stack_base - stack_min > max_stack_size)
2932875Sksewell@umich.edu                fatal("Maximum stack size exceeded\n");
2942875Sksewell@umich.edu            if(stack_base - stack_min > 8*1024*1024)
2952875Sksewell@umich.edu                fatal("Over max stack size for one thread\n");
2962875Sksewell@umich.edu            pTable->allocate(stack_min, TheISA::PageBytes);
2972875Sksewell@umich.edu            warn("Increasing stack size by one page.");
2982875Sksewell@umich.edu        };
2992875Sksewell@umich.edu        return true;
3002875Sksewell@umich.edu    }
3012875Sksewell@umich.edu    return false;
3022875Sksewell@umich.edu}
3032875Sksewell@umich.edu
3042875Sksewell@umich.eduvoid
3053221Sktlim@umich.eduProcess::serialize(std::ostream &os)
3063221Sktlim@umich.edu{
3073221Sktlim@umich.edu    SERIALIZE_SCALAR(initialContextLoaded);
3082875Sksewell@umich.edu    SERIALIZE_SCALAR(brk_point);
3095336Shines@cs.fsu.edu    SERIALIZE_SCALAR(stack_base);
3102875Sksewell@umich.edu    SERIALIZE_SCALAR(stack_size);
3112875Sksewell@umich.edu    SERIALIZE_SCALAR(stack_min);
3122875Sksewell@umich.edu    SERIALIZE_SCALAR(next_thread_stack_base);
3136221Snate@binkert.org    SERIALIZE_SCALAR(mmap_start);
3149180Sandreas.hansson@arm.com    SERIALIZE_SCALAR(mmap_end);
3152875Sksewell@umich.edu    SERIALIZE_SCALAR(nxm_start);
3162875Sksewell@umich.edu    SERIALIZE_SCALAR(nxm_end);
3172875Sksewell@umich.edu    SERIALIZE_ARRAY(fd_map, MAX_FD);
3185606Snate@binkert.org
3199179Sandreas.hansson@arm.com    pTable->serialize(os);
3202875Sksewell@umich.edu}
3215606Snate@binkert.org
3229179Sandreas.hansson@arm.comvoid
3232875Sksewell@umich.eduProcess::unserialize(Checkpoint *cp, const std::string &section)
3242875Sksewell@umich.edu{
3252875Sksewell@umich.edu    UNSERIALIZE_SCALAR(initialContextLoaded);
3266221Snate@binkert.org    UNSERIALIZE_SCALAR(brk_point);
3276221Snate@binkert.org    UNSERIALIZE_SCALAR(stack_base);
3282875Sksewell@umich.edu    UNSERIALIZE_SCALAR(stack_size);
3292875Sksewell@umich.edu    UNSERIALIZE_SCALAR(stack_min);
3302875Sksewell@umich.edu    UNSERIALIZE_SCALAR(next_thread_stack_base);
3312875Sksewell@umich.edu    UNSERIALIZE_SCALAR(mmap_start);
3322875Sksewell@umich.edu    UNSERIALIZE_SCALAR(mmap_end);
3332875Sksewell@umich.edu    UNSERIALIZE_SCALAR(nxm_start);
3342875Sksewell@umich.edu    UNSERIALIZE_SCALAR(nxm_end);
3352875Sksewell@umich.edu    UNSERIALIZE_ARRAY(fd_map, MAX_FD);
3369444SAndreas.Sandberg@ARM.com
3379444SAndreas.Sandberg@ARM.com    pTable->unserialize(cp, section);
3389444SAndreas.Sandberg@ARM.com}
3399444SAndreas.Sandberg@ARM.com
3409444SAndreas.Sandberg@ARM.com
3419444SAndreas.Sandberg@ARM.com////////////////////////////////////////////////////////////////////////
3429444SAndreas.Sandberg@ARM.com//
3439444SAndreas.Sandberg@ARM.com// LiveProcess member definitions
3449444SAndreas.Sandberg@ARM.com//
3459444SAndreas.Sandberg@ARM.com////////////////////////////////////////////////////////////////////////
3469444SAndreas.Sandberg@ARM.com
3479444SAndreas.Sandberg@ARM.com
3489444SAndreas.Sandberg@ARM.comLiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile)
3499444SAndreas.Sandberg@ARM.com    : Process(params), objFile(_objFile),
3509444SAndreas.Sandberg@ARM.com      argv(params->cmd), envp(params->env), cwd(params->cwd)
3519444SAndreas.Sandberg@ARM.com{
3529444SAndreas.Sandberg@ARM.com    __uid = params->uid;
3539444SAndreas.Sandberg@ARM.com    __euid = params->euid;
3549444SAndreas.Sandberg@ARM.com    __gid = params->gid;
3559444SAndreas.Sandberg@ARM.com    __egid = params->egid;
3569444SAndreas.Sandberg@ARM.com    __pid = params->pid;
3579444SAndreas.Sandberg@ARM.com    __ppid = params->ppid;
3589444SAndreas.Sandberg@ARM.com
3599444SAndreas.Sandberg@ARM.com    prog_fname = params->cmd[0];
3609444SAndreas.Sandberg@ARM.com
3619444SAndreas.Sandberg@ARM.com    // load up symbols, if any... these may be used for debugging or
3629444SAndreas.Sandberg@ARM.com    // profiling.
3631060SN/A    if (!debugSymbolTable) {
3642292SN/A        debugSymbolTable = new SymbolTable();
3655595Sgblack@eecs.umich.edu        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
3662292SN/A            !objFile->loadLocalSymbols(debugSymbolTable)) {
3671755SN/A            // didn't load any symbols
3681060SN/A            delete debugSymbolTable;
3692292SN/A            debugSymbolTable = NULL;
3705595Sgblack@eecs.umich.edu        }
3711684SN/A    }
3725358Sgblack@eecs.umich.edu}
3735358Sgblack@eecs.umich.edu
3745358Sgblack@eecs.umich.eduvoid
3755358Sgblack@eecs.umich.eduLiveProcess::argsInit(int intSize, int pageSize)
3765358Sgblack@eecs.umich.edu{
3775358Sgblack@eecs.umich.edu    Process::startup();
3785358Sgblack@eecs.umich.edu
3795358Sgblack@eecs.umich.edu    // load object file into target memory
3805358Sgblack@eecs.umich.edu    objFile->loadSections(initVirtMem);
3815358Sgblack@eecs.umich.edu
3825358Sgblack@eecs.umich.edu    // Calculate how much space we need for arg & env arrays.
3835358Sgblack@eecs.umich.edu    int argv_array_size = intSize * (argv.size() + 1);
3845358Sgblack@eecs.umich.edu    int envp_array_size = intSize * (envp.size() + 1);
3855358Sgblack@eecs.umich.edu    int arg_data_size = 0;
3865358Sgblack@eecs.umich.edu    for (int i = 0; i < argv.size(); ++i) {
3875358Sgblack@eecs.umich.edu        arg_data_size += argv[i].size() + 1;
3882292SN/A    }
3892292SN/A    int env_data_size = 0;
3902292SN/A    for (int i = 0; i < envp.size(); ++i) {
3911684SN/A        env_data_size += envp[i].size() + 1;
3921684SN/A    }
3932292SN/A
3941060SN/A    int space_needed =
3951060SN/A        argv_array_size + envp_array_size + arg_data_size + env_data_size;
3969427SAndreas.Sandberg@ARM.com    if (space_needed < 32*1024)
3979427SAndreas.Sandberg@ARM.com        space_needed = 32*1024;
3982834Sksewell@umich.edu
3992834Sksewell@umich.edu    // set bottom of stack
4002834Sksewell@umich.edu    stack_min = stack_base - space_needed;
4012834Sksewell@umich.edu    // align it
4022829Sksewell@umich.edu    stack_min = roundDown(stack_min, pageSize);
4036221Snate@binkert.org    stack_size = stack_base - stack_min;
4042875Sksewell@umich.edu    // map memory
4052875Sksewell@umich.edu    pTable->allocate(stack_min, roundUp(stack_size, pageSize));
4066221Snate@binkert.org
4072829Sksewell@umich.edu    // map out initial stack contents
4082292SN/A    Addr argv_array_base = stack_min + intSize; // room for argc
4096221Snate@binkert.org    Addr envp_array_base = argv_array_base + argv_array_size;
4101060SN/A    Addr arg_data_base = envp_array_base + envp_array_size;
4112292SN/A    Addr env_data_base = arg_data_base + arg_data_size;
4126221Snate@binkert.org
4132292SN/A    // write contents to stack
4142292SN/A    uint64_t argc = argv.size();
4158834Satgutier@umich.edu    if (intSize == 8)
4168834Satgutier@umich.edu        argc = htog((uint64_t)argc);
4178834Satgutier@umich.edu    else if (intSize == 4)
4188834Satgutier@umich.edu        argc = htog((uint32_t)argc);
4192292SN/A    else
4202292SN/A        panic("Unknown int size");
4219180Sandreas.hansson@arm.com
4222292SN/A    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
4232292SN/A
4246221Snate@binkert.org    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
4252292SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
4262292SN/A
4273221Sktlim@umich.edu    assert(NumArgumentRegs >= 2);
4282292SN/A    threadContexts[0]->setIntReg(ArgumentReg[0], argc);
4299180Sandreas.hansson@arm.com    threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
4309180Sandreas.hansson@arm.com    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
4312292SN/A
4322292SN/A    Addr prog_entry = objFile->entryPoint();
4332292SN/A    threadContexts[0]->setPC(prog_entry);
4342292SN/A    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
4356221Snate@binkert.org
4362292SN/A#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
4372292SN/A    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
4386221Snate@binkert.org#endif
4392292SN/A
4402292SN/A    num_processes++;
4412292SN/A}
4422292SN/A
4432292SN/Avoid
4442292SN/ALiveProcess::syscall(int64_t callnum, ThreadContext *tc)
4452292SN/A{
4469444SAndreas.Sandberg@ARM.com    num_syscalls++;
4479444SAndreas.Sandberg@ARM.com
4489444SAndreas.Sandberg@ARM.com    SyscallDesc *desc = getDesc(callnum);
4499448SAndreas.Sandberg@ARM.com    if (desc == NULL)
4502864Sktlim@umich.edu        fatal("Syscall %d out of range", callnum);
4519448SAndreas.Sandberg@ARM.com
4529448SAndreas.Sandberg@ARM.com    desc->doSyscall(callnum, this, tc);
4532864Sktlim@umich.edu}
4542864Sktlim@umich.edu
4555595Sgblack@eecs.umich.eduLiveProcess *
4565595Sgblack@eecs.umich.eduLiveProcess::create(LiveProcessParams * params)
4572292SN/A{
4586221Snate@binkert.org    LiveProcess *process = NULL;
4592292SN/A
4602843Sktlim@umich.edu    string executable =
4612843Sktlim@umich.edu        params->executable == "" ? params->cmd[0] : params->executable;
4629342SAndreas.Sandberg@arm.com    ObjectFile *objFile = createObjectFile(executable);
4632843Sktlim@umich.edu    if (objFile == NULL) {
4642843Sktlim@umich.edu        fatal("Can't load object file %s", executable);
4659342SAndreas.Sandberg@arm.com    }
4662292SN/A
4679444SAndreas.Sandberg@ARM.com    if (objFile->isDynamic())
4689444SAndreas.Sandberg@ARM.com       fatal("Object file is a dynamic executable however only static "
4699444SAndreas.Sandberg@ARM.com             "executables are supported!\n       Please recompile your "
4709444SAndreas.Sandberg@ARM.com             "executable as a static binary and try again.\n");
4719444SAndreas.Sandberg@ARM.com
4729444SAndreas.Sandberg@ARM.com#if THE_ISA == ALPHA_ISA
4739444SAndreas.Sandberg@ARM.com    if (objFile->hasTLS())
4749444SAndreas.Sandberg@ARM.com        fatal("Object file has a TLS section and single threaded TLS is not\n"
4752843Sktlim@umich.edu              "       currently supported for Alpha! Please recompile your "
4762843Sktlim@umich.edu              "executable with \n       a non-TLS toolchain.\n");
4772843Sktlim@umich.edu
4782316SN/A    if (objFile->getArch() != ObjectFile::Alpha)
4792348SN/A        fatal("Object file architecture does not match compiled ISA (Alpha).");
4802843Sktlim@umich.edu    switch (objFile->getOpSys()) {
4811060SN/A      case ObjectFile::Tru64:
4829523SAndreas.Sandberg@ARM.com        process = new AlphaTru64Process(params, objFile);
4839523SAndreas.Sandberg@ARM.com        break;
4841060SN/A
4852316SN/A      case ObjectFile::Linux:
4862316SN/A        process = new AlphaLinuxProcess(params, objFile);
4871060SN/A        break;
4885595Sgblack@eecs.umich.edu
4897684Sgblack@eecs.umich.edu      default:
4905595Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
4915702Ssaidi@eecs.umich.edu    }
4926221Snate@binkert.org#elif THE_ISA == SPARC_ISA
4935702Ssaidi@eecs.umich.edu    if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32)
4946221Snate@binkert.org        fatal("Object file architecture does not match compiled ISA (SPARC).");
4955702Ssaidi@eecs.umich.edu    switch (objFile->getOpSys()) {
4965595Sgblack@eecs.umich.edu      case ObjectFile::Linux:
4975595Sgblack@eecs.umich.edu        if (objFile->getArch() == ObjectFile::SPARC64) {
4985595Sgblack@eecs.umich.edu            process = new Sparc64LinuxProcess(params, objFile);
4995595Sgblack@eecs.umich.edu        } else {
5005595Sgblack@eecs.umich.edu            process = new Sparc32LinuxProcess(params, objFile);
5015595Sgblack@eecs.umich.edu        }
5025595Sgblack@eecs.umich.edu        break;
5035595Sgblack@eecs.umich.edu
5045595Sgblack@eecs.umich.edu
5051060SN/A      case ObjectFile::Solaris:
5061060SN/A        process = new SparcSolarisProcess(params, objFile);
5071060SN/A        break;
5081060SN/A      default:
5091060SN/A        fatal("Unknown/unsupported operating system.");
5101060SN/A    }
5112348SN/A#elif THE_ISA == X86_ISA
5125595Sgblack@eecs.umich.edu    if (objFile->getArch() != ObjectFile::X86)
5135595Sgblack@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
5146221Snate@binkert.org    switch (objFile->getOpSys()) {
5155595Sgblack@eecs.umich.edu      case ObjectFile::Linux:
5165595Sgblack@eecs.umich.edu        process = new X86LinuxProcess(params, objFile);
5175595Sgblack@eecs.umich.edu        break;
5185595Sgblack@eecs.umich.edu      default:
5196221Snate@binkert.org        fatal("Unknown/unsupported operating system.");
5205595Sgblack@eecs.umich.edu    }
5215595Sgblack@eecs.umich.edu#elif THE_ISA == MIPS_ISA
5226221Snate@binkert.org    if (objFile->getArch() != ObjectFile::Mips)
5236221Snate@binkert.org        fatal("Object file architecture does not match compiled ISA (MIPS).");
5245595Sgblack@eecs.umich.edu    switch (objFile->getOpSys()) {
5255595Sgblack@eecs.umich.edu      case ObjectFile::Linux:
5265595Sgblack@eecs.umich.edu        process = new MipsLinuxProcess(params, objFile);
5275595Sgblack@eecs.umich.edu        break;
5285595Sgblack@eecs.umich.edu
5296221Snate@binkert.org      default:
5305595Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
5311060SN/A    }
5321060SN/A#else
5333781Sgblack@eecs.umich.edu#error "THE_ISA not set"
5341060SN/A#endif
5353781Sgblack@eecs.umich.edu
5362455SN/A
5371060SN/A    if (process == NULL)
5381060SN/A        fatal("Unknown error creating process object.");
5393781Sgblack@eecs.umich.edu    return process;
5401060SN/A}
5413781Sgblack@eecs.umich.edu
5422455SN/ALiveProcess *
5436221Snate@binkert.orgLiveProcessParams::create()
5441060SN/A{
5456314Sgblack@eecs.umich.edu    return LiveProcess::create(this);
5462292SN/A}
5476221Snate@binkert.org