process.cc revision 5512
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#include <string>
362SN/A
373971Sgblack@eecs.umich.edu#include "arch/remote_gdb.hh"
3856SN/A#include "base/intmath.hh"
3956SN/A#include "base/loader/object_file.hh"
401158SN/A#include "base/loader/symtab.hh"
41146SN/A#include "base/statistics.hh"
421858SN/A#include "config/full_system.hh"
432680Sktlim@umich.edu#include "cpu/thread_context.hh"
442378SN/A#include "mem/page_table.hh"
452522SN/A#include "mem/physical.hh"
462401SN/A#include "mem/translating_port.hh"
475154Sgblack@eecs.umich.edu#include "params/Process.hh"
484762Snate@binkert.org#include "params/LiveProcess.hh"
495512SMichael.Adler@intel.com#include "sim/debug.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 == ARM_ISA
665335Shines@cs.fsu.edu#include "arch/arm/linux/process.hh"
674157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
684166Sgblack@eecs.umich.edu#include "arch/x86/linux/process.hh"
692715Sstever@eecs.umich.edu#else
702715Sstever@eecs.umich.edu#error "THE_ISA not set"
712715Sstever@eecs.umich.edu#endif
722715Sstever@eecs.umich.edu
732715Sstever@eecs.umich.edu
742SN/Ausing namespace std;
752107SN/Ausing namespace TheISA;
762SN/A
772SN/A//
782SN/A// The purpose of this code is to fake the loader & syscall mechanism
792SN/A// when there's no OS: thus there's no resone to use it in FULL_SYSTEM
802SN/A// mode when we do have an OS
812SN/A//
821858SN/A#if FULL_SYSTEM
83360SN/A#error "process.cc not compatible with FULL_SYSTEM"
842SN/A#endif
852SN/A
862SN/A// current number of allocated processes
872SN/Aint num_processes = 0;
882SN/A
895154Sgblack@eecs.umich.eduProcess::Process(ProcessParams * params)
905183Ssaidi@eecs.umich.edu    : SimObject(params), system(params->system), checkpointRestored(false),
915154Sgblack@eecs.umich.edu    max_stack_size(params->max_stack_size)
922SN/A{
935154Sgblack@eecs.umich.edu    string in = params->input;
945154Sgblack@eecs.umich.edu    string out = params->output;
955154Sgblack@eecs.umich.edu
965154Sgblack@eecs.umich.edu    // initialize file descriptors to default: same as simulator
975154Sgblack@eecs.umich.edu    int stdin_fd, stdout_fd, stderr_fd;
985154Sgblack@eecs.umich.edu
995154Sgblack@eecs.umich.edu    if (in == "stdin" || in == "cin")
1005154Sgblack@eecs.umich.edu        stdin_fd = STDIN_FILENO;
1015154Sgblack@eecs.umich.edu    else if (in == "None")
1025154Sgblack@eecs.umich.edu        stdin_fd = -1;
1035154Sgblack@eecs.umich.edu    else
1045154Sgblack@eecs.umich.edu        stdin_fd = Process::openInputFile(in);
1055154Sgblack@eecs.umich.edu
1065154Sgblack@eecs.umich.edu    if (out == "stdout" || out == "cout")
1075154Sgblack@eecs.umich.edu        stdout_fd = STDOUT_FILENO;
1085154Sgblack@eecs.umich.edu    else if (out == "stderr" || out == "cerr")
1095154Sgblack@eecs.umich.edu        stdout_fd = STDERR_FILENO;
1105154Sgblack@eecs.umich.edu    else if (out == "None")
1115154Sgblack@eecs.umich.edu        stdout_fd = -1;
1125154Sgblack@eecs.umich.edu    else
1135154Sgblack@eecs.umich.edu        stdout_fd = Process::openOutputFile(out);
1145154Sgblack@eecs.umich.edu
1155154Sgblack@eecs.umich.edu    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
1165154Sgblack@eecs.umich.edu
1174997Sgblack@eecs.umich.edu    M5_pid = system->allocatePID();
1182SN/A    // initialize first 3 fds (stdin, stdout, stderr)
1195282Srstrong@cs.ucsd.edu    Process::FdMap *fdo = &fd_map[STDIN_FILENO];
1205282Srstrong@cs.ucsd.edu    fdo->fd = stdin_fd;
1215282Srstrong@cs.ucsd.edu    fdo->filename = in;
1225282Srstrong@cs.ucsd.edu    fdo->flags = O_RDONLY;
1235282Srstrong@cs.ucsd.edu    fdo->mode = -1;
1245282Srstrong@cs.ucsd.edu    fdo->fileOffset = 0;
1255282Srstrong@cs.ucsd.edu
1265282Srstrong@cs.ucsd.edu    fdo =  &fd_map[STDOUT_FILENO];
1275282Srstrong@cs.ucsd.edu    fdo->fd = stdout_fd;
1285282Srstrong@cs.ucsd.edu    fdo->filename = out;
1295282Srstrong@cs.ucsd.edu    fdo->flags =  O_WRONLY | O_CREAT | O_TRUNC;
1305282Srstrong@cs.ucsd.edu    fdo->mode = 0774;
1315282Srstrong@cs.ucsd.edu    fdo->fileOffset = 0;
1325282Srstrong@cs.ucsd.edu
1335282Srstrong@cs.ucsd.edu    fdo = &fd_map[STDERR_FILENO];
1345282Srstrong@cs.ucsd.edu    fdo->fd = stderr_fd;
1355282Srstrong@cs.ucsd.edu    fdo->filename = "STDERR";
1365282Srstrong@cs.ucsd.edu    fdo->flags = O_WRONLY;
1375282Srstrong@cs.ucsd.edu    fdo->mode = -1;
1385282Srstrong@cs.ucsd.edu    fdo->fileOffset = 0;
1395282Srstrong@cs.ucsd.edu
1402SN/A
1412SN/A    // mark remaining fds as free
1422SN/A    for (int i = 3; i <= MAX_FD; ++i) {
1435282Srstrong@cs.ucsd.edu        Process::FdMap *fdo = &fd_map[i];
1445282Srstrong@cs.ucsd.edu        fdo->fd = -1;
1452SN/A    }
1462SN/A
1471450SN/A    mmap_start = mmap_end = 0;
1481514SN/A    nxm_start = nxm_end = 0;
1495184Sgblack@eecs.umich.edu    pTable = new PageTable(this);
1502SN/A    // other parameters will be initialized when the program is loaded
1512SN/A}
1522SN/A
1532378SN/A
1542SN/Avoid
1552SN/AProcess::regStats()
1562SN/A{
157729SN/A    using namespace Stats;
1582SN/A
1592SN/A    num_syscalls
1602SN/A        .name(name() + ".PROG:num_syscalls")
1612SN/A        .desc("Number of system calls")
1622SN/A        ;
1632SN/A}
1642SN/A
1652SN/A//
1662SN/A// static helper functions
1672SN/A//
1682SN/Aint
1692SN/AProcess::openInputFile(const string &filename)
1702SN/A{
1712SN/A    int fd = open(filename.c_str(), O_RDONLY);
1722SN/A
1732SN/A    if (fd == -1) {
1742SN/A        perror(NULL);
1752SN/A        cerr << "unable to open \"" << filename << "\" for reading\n";
1762SN/A        fatal("can't open input file");
1772SN/A    }
1782SN/A
1792SN/A    return fd;
1802SN/A}
1812SN/A
1822SN/A
1832SN/Aint
1842SN/AProcess::openOutputFile(const string &filename)
1852SN/A{
1862SN/A    int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774);
1872SN/A
1882SN/A    if (fd == -1) {
1892SN/A        perror(NULL);
1902SN/A        cerr << "unable to open \"" << filename << "\" for writing\n";
1912SN/A        fatal("can't open output file");
1922SN/A    }
1932SN/A
1942SN/A    return fd;
1952SN/A}
1962SN/A
1972SN/A
198180SN/Aint
1992680Sktlim@umich.eduProcess::registerThreadContext(ThreadContext *tc)
2002SN/A{
201180SN/A    // add to list
2022680Sktlim@umich.edu    int myIndex = threadContexts.size();
2032680Sktlim@umich.edu    threadContexts.push_back(tc);
204180SN/A
2055512SMichael.Adler@intel.com    int port = getRemoteGDBPort();
2065512SMichael.Adler@intel.com    if (port) {
2075512SMichael.Adler@intel.com        RemoteGDB *rgdb = new RemoteGDB(system, tc);
2085512SMichael.Adler@intel.com        GDBListener *gdbl = new GDBListener(rgdb, port + myIndex);
2095512SMichael.Adler@intel.com        gdbl->listen();
2103971Sgblack@eecs.umich.edu
2115512SMichael.Adler@intel.com        remoteGDB.push_back(rgdb);
2125512SMichael.Adler@intel.com    }
2133971Sgblack@eecs.umich.edu
2142378SN/A    // return CPU number to caller
215180SN/A    return myIndex;
2162SN/A}
2172SN/A
2181395SN/Avoid
2191395SN/AProcess::startup()
2201395SN/A{
2212680Sktlim@umich.edu    if (threadContexts.empty())
2222378SN/A        fatal("Process %s is not associated with any CPUs!\n", name());
2232378SN/A
2242680Sktlim@umich.edu    // first thread context for this process... initialize & enable
2252680Sktlim@umich.edu    ThreadContext *tc = threadContexts[0];
2261395SN/A
2271634SN/A    // mark this context as active so it will start ticking.
2282680Sktlim@umich.edu    tc->activate(0);
2292462SN/A
2302519SN/A    Port *mem_port;
2312519SN/A    mem_port = system->physmem->getPort("functional");
2324434Ssaidi@eecs.umich.edu    initVirtMem = new TranslatingPort("process init port", this,
2334434Ssaidi@eecs.umich.edu            TranslatingPort::Always);
2342519SN/A    mem_port->setPeer(initVirtMem);
2352519SN/A    initVirtMem->setPeer(mem_port);
2361395SN/A}
2372SN/A
238180SN/Avoid
2392680Sktlim@umich.eduProcess::replaceThreadContext(ThreadContext *tc, int tcIndex)
240180SN/A{
2412680Sktlim@umich.edu    if (tcIndex >= threadContexts.size()) {
2422680Sktlim@umich.edu        panic("replaceThreadContext: bad tcIndex, %d >= %d\n",
2432680Sktlim@umich.edu              tcIndex, threadContexts.size());
244180SN/A    }
245180SN/A
2462680Sktlim@umich.edu    threadContexts[tcIndex] = tc;
247180SN/A}
248180SN/A
2492SN/A// map simulator fd sim_fd to target fd tgt_fd
2502SN/Avoid
2512SN/AProcess::dup_fd(int sim_fd, int tgt_fd)
2522SN/A{
2532SN/A    if (tgt_fd < 0 || tgt_fd > MAX_FD)
2542SN/A        panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd);
2552SN/A
2565282Srstrong@cs.ucsd.edu    Process::FdMap *fdo = &fd_map[tgt_fd];
2575282Srstrong@cs.ucsd.edu    fdo->fd = sim_fd;
2582SN/A}
2592SN/A
2602SN/A
2612SN/A// generate new target fd for sim_fd
2622SN/Aint
2635282Srstrong@cs.ucsd.eduProcess::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe)
2642SN/A{
2652SN/A    // in case open() returns an error, don't allocate a new fd
2662SN/A    if (sim_fd == -1)
2672SN/A        return -1;
2682SN/A
2692SN/A    // find first free target fd
2705282Srstrong@cs.ucsd.edu    for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
2715282Srstrong@cs.ucsd.edu        Process::FdMap *fdo = &fd_map[free_fd];
2725282Srstrong@cs.ucsd.edu        if (fdo->fd == -1) {
2735282Srstrong@cs.ucsd.edu            fdo->fd = sim_fd;
2745282Srstrong@cs.ucsd.edu            fdo->filename = filename;
2755282Srstrong@cs.ucsd.edu            fdo->mode = mode;
2765282Srstrong@cs.ucsd.edu            fdo->fileOffset = 0;
2775282Srstrong@cs.ucsd.edu            fdo->flags = flags;
2785282Srstrong@cs.ucsd.edu            fdo->isPipe = pipe;
2795282Srstrong@cs.ucsd.edu            fdo->readPipeSource = 0;
2801970SN/A            return free_fd;
2811970SN/A        }
2822SN/A    }
2832SN/A
2841970SN/A    panic("Process::alloc_fd: out of file descriptors!");
2851970SN/A}
2862SN/A
2871970SN/A
2881970SN/A// free target fd (e.g., after close)
2891970SN/Avoid
2901970SN/AProcess::free_fd(int tgt_fd)
2911970SN/A{
2925282Srstrong@cs.ucsd.edu    Process::FdMap *fdo = &fd_map[tgt_fd];
2935282Srstrong@cs.ucsd.edu    if (fdo->fd == -1)
2941970SN/A        warn("Process::free_fd: request to free unused fd %d", tgt_fd);
2951970SN/A
2965282Srstrong@cs.ucsd.edu    fdo->fd = -1;
2975282Srstrong@cs.ucsd.edu    fdo->filename = "NULL";
2985282Srstrong@cs.ucsd.edu    fdo->mode = 0;
2995282Srstrong@cs.ucsd.edu    fdo->fileOffset = 0;
3005282Srstrong@cs.ucsd.edu    fdo->flags = 0;
3015282Srstrong@cs.ucsd.edu    fdo->isPipe = false;
3025282Srstrong@cs.ucsd.edu    fdo->readPipeSource = 0;
3032SN/A}
3042SN/A
3052SN/A
3062SN/A// look up simulator fd for given target fd
3072SN/Aint
3082SN/AProcess::sim_fd(int tgt_fd)
3092SN/A{
3102SN/A    if (tgt_fd > MAX_FD)
3112SN/A        return -1;
3122SN/A
3135282Srstrong@cs.ucsd.edu    return fd_map[tgt_fd].fd;
3142SN/A}
3152SN/A
3165282Srstrong@cs.ucsd.eduProcess::FdMap *
3175282Srstrong@cs.ucsd.eduProcess::sim_fd_obj(int tgt_fd)
3185282Srstrong@cs.ucsd.edu{
3195282Srstrong@cs.ucsd.edu    if (tgt_fd > MAX_FD)
3205282Srstrong@cs.ucsd.edu        panic("sim_fd_obj called in fd out of range.");
3215282Srstrong@cs.ucsd.edu
3225282Srstrong@cs.ucsd.edu    return &fd_map[tgt_fd];
3235282Srstrong@cs.ucsd.edu}
3244434Ssaidi@eecs.umich.edubool
3254434Ssaidi@eecs.umich.eduProcess::checkAndAllocNextPage(Addr vaddr)
3264434Ssaidi@eecs.umich.edu{
3274434Ssaidi@eecs.umich.edu    // if this is an initial write we might not have
3284434Ssaidi@eecs.umich.edu    if (vaddr >= stack_min && vaddr < stack_base) {
3294434Ssaidi@eecs.umich.edu        pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize);
3304434Ssaidi@eecs.umich.edu        return true;
3314434Ssaidi@eecs.umich.edu    }
3324434Ssaidi@eecs.umich.edu
3334434Ssaidi@eecs.umich.edu    // We've accessed the next page of the stack, so extend the stack
3344434Ssaidi@eecs.umich.edu    // to cover it.
3355154Sgblack@eecs.umich.edu    if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) {
3365154Sgblack@eecs.umich.edu        while (vaddr < stack_min) {
3375154Sgblack@eecs.umich.edu            stack_min -= TheISA::PageBytes;
3385154Sgblack@eecs.umich.edu            if(stack_base - stack_min > max_stack_size)
3395154Sgblack@eecs.umich.edu                fatal("Maximum stack size exceeded\n");
3405154Sgblack@eecs.umich.edu            if(stack_base - stack_min > 8*1024*1024)
3415154Sgblack@eecs.umich.edu                fatal("Over max stack size for one thread\n");
3425154Sgblack@eecs.umich.edu            pTable->allocate(stack_min, TheISA::PageBytes);
3435154Sgblack@eecs.umich.edu            warn("Increasing stack size by one page.");
3445154Sgblack@eecs.umich.edu        };
3454434Ssaidi@eecs.umich.edu        return true;
3464434Ssaidi@eecs.umich.edu    }
3474434Ssaidi@eecs.umich.edu    return false;
3484434Ssaidi@eecs.umich.edu}
3494434Ssaidi@eecs.umich.edu
3505282Srstrong@cs.ucsd.edu // find all offsets for currently open files and save them
3515282Srstrong@cs.ucsd.eduvoid
3525282Srstrong@cs.ucsd.eduProcess::fix_file_offsets() {
3535282Srstrong@cs.ucsd.edu    Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO];
3545282Srstrong@cs.ucsd.edu    Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO];
3555282Srstrong@cs.ucsd.edu    Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO];
3565282Srstrong@cs.ucsd.edu    string in = fdo_stdin->filename;
3575282Srstrong@cs.ucsd.edu    string out = fdo_stdout->filename;
3585282Srstrong@cs.ucsd.edu
3595282Srstrong@cs.ucsd.edu    // initialize file descriptors to default: same as simulator
3605282Srstrong@cs.ucsd.edu    int stdin_fd, stdout_fd, stderr_fd;
3615282Srstrong@cs.ucsd.edu
3625282Srstrong@cs.ucsd.edu    if (in == "stdin" || in == "cin")
3635282Srstrong@cs.ucsd.edu        stdin_fd = STDIN_FILENO;
3645282Srstrong@cs.ucsd.edu    else if (in == "None")
3655282Srstrong@cs.ucsd.edu        stdin_fd = -1;
3665282Srstrong@cs.ucsd.edu    else{
3675282Srstrong@cs.ucsd.edu        //OPEN standard in and seek to the right location
3685282Srstrong@cs.ucsd.edu        stdin_fd = Process::openInputFile(in);
3695282Srstrong@cs.ucsd.edu        if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0)
3705282Srstrong@cs.ucsd.edu            panic("Unable to seek to correct location in file: %s", in);
3715282Srstrong@cs.ucsd.edu    }
3725282Srstrong@cs.ucsd.edu
3735282Srstrong@cs.ucsd.edu    if (out == "stdout" || out == "cout")
3745282Srstrong@cs.ucsd.edu        stdout_fd = STDOUT_FILENO;
3755282Srstrong@cs.ucsd.edu    else if (out == "stderr" || out == "cerr")
3765282Srstrong@cs.ucsd.edu        stdout_fd = STDERR_FILENO;
3775282Srstrong@cs.ucsd.edu    else if (out == "None")
3785282Srstrong@cs.ucsd.edu        stdout_fd = -1;
3795282Srstrong@cs.ucsd.edu    else{
3805282Srstrong@cs.ucsd.edu        stdout_fd = Process::openOutputFile(out);
3815282Srstrong@cs.ucsd.edu        if (lseek(stdin_fd, fdo_stdout->fileOffset, SEEK_SET) < 0)
3825282Srstrong@cs.ucsd.edu            panic("Unable to seek to correct in file: %s", out);
3835282Srstrong@cs.ucsd.edu    }
3845282Srstrong@cs.ucsd.edu
3855282Srstrong@cs.ucsd.edu    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;
3865282Srstrong@cs.ucsd.edu
3875282Srstrong@cs.ucsd.edu    fdo_stdin->fd = stdin_fd;
3885282Srstrong@cs.ucsd.edu    fdo_stdout->fd = stdout_fd;
3895282Srstrong@cs.ucsd.edu    fdo_stderr->fd = stderr_fd;
3905282Srstrong@cs.ucsd.edu
3915282Srstrong@cs.ucsd.edu
3925282Srstrong@cs.ucsd.edu    for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) {
3935282Srstrong@cs.ucsd.edu        Process::FdMap *fdo = &fd_map[free_fd];
3945282Srstrong@cs.ucsd.edu        if (fdo->fd != -1) {
3955282Srstrong@cs.ucsd.edu            if (fdo->isPipe){
3965282Srstrong@cs.ucsd.edu                if (fdo->filename == "PIPE-WRITE")
3975282Srstrong@cs.ucsd.edu                    continue;
3985282Srstrong@cs.ucsd.edu                else {
3995282Srstrong@cs.ucsd.edu                    assert (fdo->filename == "PIPE-READ");
4005282Srstrong@cs.ucsd.edu                    //create a new pipe
4015282Srstrong@cs.ucsd.edu                    int fds[2];
4025282Srstrong@cs.ucsd.edu                    int pipe_retval = pipe(fds);
4035282Srstrong@cs.ucsd.edu
4045282Srstrong@cs.ucsd.edu                    if (pipe_retval < 0) {
4055282Srstrong@cs.ucsd.edu                        // error
4065282Srstrong@cs.ucsd.edu                        panic("Unable to create new pipe.");
4075282Srstrong@cs.ucsd.edu                    }
4085282Srstrong@cs.ucsd.edu                    fdo->fd = fds[0]; //set read pipe
4095282Srstrong@cs.ucsd.edu                    Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource];
4105282Srstrong@cs.ucsd.edu                    if (fdo_write->filename != "PIPE-WRITE")
4115282Srstrong@cs.ucsd.edu                        panic ("Couldn't find write end of the pipe");
4125282Srstrong@cs.ucsd.edu
4135282Srstrong@cs.ucsd.edu                    fdo_write->fd = fds[1];//set write pipe
4145282Srstrong@cs.ucsd.edu               }
4155282Srstrong@cs.ucsd.edu            } else {
4165282Srstrong@cs.ucsd.edu                //Open file
4175282Srstrong@cs.ucsd.edu                int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode);
4185282Srstrong@cs.ucsd.edu
4195282Srstrong@cs.ucsd.edu                if (fd == -1)
4205282Srstrong@cs.ucsd.edu                    panic("Unable to open file: %s", fdo->filename);
4215282Srstrong@cs.ucsd.edu                fdo->fd = fd;
4225282Srstrong@cs.ucsd.edu
4235282Srstrong@cs.ucsd.edu                //Seek to correct location before checkpoint
4245282Srstrong@cs.ucsd.edu                if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0)
4255282Srstrong@cs.ucsd.edu                    panic("Unable to seek to correct location in file: %s", fdo->filename);
4265282Srstrong@cs.ucsd.edu            }
4275282Srstrong@cs.ucsd.edu        }
4285282Srstrong@cs.ucsd.edu    }
4295282Srstrong@cs.ucsd.edu}
4305282Srstrong@cs.ucsd.eduvoid
4315282Srstrong@cs.ucsd.eduProcess::find_file_offsets(){
4325282Srstrong@cs.ucsd.edu    for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {
4335282Srstrong@cs.ucsd.edu        Process::FdMap *fdo = &fd_map[free_fd];
4345282Srstrong@cs.ucsd.edu        if (fdo->fd != -1) {
4355282Srstrong@cs.ucsd.edu            fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR);
4365282Srstrong@cs.ucsd.edu        }  else {
4375282Srstrong@cs.ucsd.edu                fdo->filename = "NULL";
4385282Srstrong@cs.ucsd.edu                fdo->fileOffset = 0;
4395282Srstrong@cs.ucsd.edu        }
4405282Srstrong@cs.ucsd.edu    }
4415282Srstrong@cs.ucsd.edu}
4425282Srstrong@cs.ucsd.edu
4435282Srstrong@cs.ucsd.eduvoid
4445282Srstrong@cs.ucsd.eduProcess::setReadPipeSource(int read_pipe_fd, int source_fd){
4455282Srstrong@cs.ucsd.edu    Process::FdMap *fdo = &fd_map[read_pipe_fd];
4465282Srstrong@cs.ucsd.edu    fdo->readPipeSource = source_fd;
4475282Srstrong@cs.ucsd.edu}
4485282Srstrong@cs.ucsd.edu
4495282Srstrong@cs.ucsd.eduvoid
4505282Srstrong@cs.ucsd.eduProcess::FdMap::serialize(std::ostream &os)
4515282Srstrong@cs.ucsd.edu{
4525282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(fd);
4535282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(isPipe);
4545282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(filename);
4555282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(flags);
4565282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(readPipeSource);
4575282Srstrong@cs.ucsd.edu    SERIALIZE_SCALAR(fileOffset);
4585282Srstrong@cs.ucsd.edu}
4595282Srstrong@cs.ucsd.edu
4605282Srstrong@cs.ucsd.eduvoid
4615282Srstrong@cs.ucsd.eduProcess::FdMap::unserialize(Checkpoint *cp, const std::string &section)
4625282Srstrong@cs.ucsd.edu{
4635282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(fd);
4645282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(isPipe);
4655282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(filename);
4665282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(flags);
4675282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(readPipeSource);
4685282Srstrong@cs.ucsd.edu    UNSERIALIZE_SCALAR(fileOffset);
4695282Srstrong@cs.ucsd.edu}
4705282Srstrong@cs.ucsd.edu
4713311Ssaidi@eecs.umich.eduvoid
4723311Ssaidi@eecs.umich.eduProcess::serialize(std::ostream &os)
4733311Ssaidi@eecs.umich.edu{
4743311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(initialContextLoaded);
4753311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(brk_point);
4763311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(stack_base);
4773311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(stack_size);
4783311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(stack_min);
4793311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(next_thread_stack_base);
4803311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(mmap_start);
4813311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(mmap_end);
4823311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(nxm_start);
4833311Ssaidi@eecs.umich.edu    SERIALIZE_SCALAR(nxm_end);
4845282Srstrong@cs.ucsd.edu    find_file_offsets();
4855282Srstrong@cs.ucsd.edu    pTable->serialize(os);
4865282Srstrong@cs.ucsd.edu    for (int x = 0; x <= MAX_FD; x++) {
4875282Srstrong@cs.ucsd.edu        nameOut(os, csprintf("%s.FdMap%d", name(), x));
4885282Srstrong@cs.ucsd.edu        fd_map[x].serialize(os);
4895282Srstrong@cs.ucsd.edu    }
4903311Ssaidi@eecs.umich.edu
4913311Ssaidi@eecs.umich.edu}
4923311Ssaidi@eecs.umich.edu
4933311Ssaidi@eecs.umich.eduvoid
4943311Ssaidi@eecs.umich.eduProcess::unserialize(Checkpoint *cp, const std::string &section)
4953311Ssaidi@eecs.umich.edu{
4963311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(initialContextLoaded);
4973311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(brk_point);
4983311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(stack_base);
4993311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(stack_size);
5003311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(stack_min);
5013311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(next_thread_stack_base);
5023311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(mmap_start);
5033311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(mmap_end);
5043311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(nxm_start);
5053311Ssaidi@eecs.umich.edu    UNSERIALIZE_SCALAR(nxm_end);
5063311Ssaidi@eecs.umich.edu    pTable->unserialize(cp, section);
5075282Srstrong@cs.ucsd.edu    for (int x = 0; x <= MAX_FD; x++) {
5085282Srstrong@cs.ucsd.edu        fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x));
5095282Srstrong@cs.ucsd.edu     }
5105282Srstrong@cs.ucsd.edu    fix_file_offsets();
5115183Ssaidi@eecs.umich.edu
5125183Ssaidi@eecs.umich.edu    checkpointRestored = true;
5135183Ssaidi@eecs.umich.edu
5143311Ssaidi@eecs.umich.edu}
5152SN/A
5162SN/A
5172SN/A////////////////////////////////////////////////////////////////////////
5182SN/A//
5192SN/A// LiveProcess member definitions
5202SN/A//
5212SN/A////////////////////////////////////////////////////////////////////////
5222SN/A
52312SN/A
5245154Sgblack@eecs.umich.eduLiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile)
5255154Sgblack@eecs.umich.edu    : Process(params), objFile(_objFile),
5265154Sgblack@eecs.umich.edu      argv(params->cmd), envp(params->env), cwd(params->cwd)
5272SN/A{
5285154Sgblack@eecs.umich.edu    __uid = params->uid;
5295154Sgblack@eecs.umich.edu    __euid = params->euid;
5305154Sgblack@eecs.umich.edu    __gid = params->gid;
5315154Sgblack@eecs.umich.edu    __egid = params->egid;
5325154Sgblack@eecs.umich.edu    __pid = params->pid;
5335154Sgblack@eecs.umich.edu    __ppid = params->ppid;
5343114Sgblack@eecs.umich.edu
5355154Sgblack@eecs.umich.edu    prog_fname = params->cmd[0];
53612SN/A
5371158SN/A    // load up symbols, if any... these may be used for debugging or
5381158SN/A    // profiling.
5391158SN/A    if (!debugSymbolTable) {
5401158SN/A        debugSymbolTable = new SymbolTable();
5411158SN/A        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||
5421158SN/A            !objFile->loadLocalSymbols(debugSymbolTable)) {
5431158SN/A            // didn't load any symbols
5441158SN/A            delete debugSymbolTable;
5451158SN/A            debugSymbolTable = NULL;
5461158SN/A        }
5471158SN/A    }
5482378SN/A}
5491158SN/A
5502378SN/Avoid
5512474SN/ALiveProcess::argsInit(int intSize, int pageSize)
5522378SN/A{
5532378SN/A    Process::startup();
55412SN/A
5552378SN/A    // load object file into target memory
5562378SN/A    objFile->loadSections(initVirtMem);
55712SN/A
55812SN/A    // Calculate how much space we need for arg & env arrays.
5592474SN/A    int argv_array_size = intSize * (argv.size() + 1);
5602474SN/A    int envp_array_size = intSize * (envp.size() + 1);
56112SN/A    int arg_data_size = 0;
56212SN/A    for (int i = 0; i < argv.size(); ++i) {
56312SN/A        arg_data_size += argv[i].size() + 1;
56412SN/A    }
56512SN/A    int env_data_size = 0;
56612SN/A    for (int i = 0; i < envp.size(); ++i) {
56712SN/A        env_data_size += envp[i].size() + 1;
56812SN/A    }
56912SN/A
57012SN/A    int space_needed =
57112SN/A        argv_array_size + envp_array_size + arg_data_size + env_data_size;
5723005Sstever@eecs.umich.edu    if (space_needed < 32*1024)
5733005Sstever@eecs.umich.edu        space_needed = 32*1024;
57412SN/A
57512SN/A    // set bottom of stack
57612SN/A    stack_min = stack_base - space_needed;
57712SN/A    // align it
5782800Ssaidi@eecs.umich.edu    stack_min = roundDown(stack_min, pageSize);
57912SN/A    stack_size = stack_base - stack_min;
5802378SN/A    // map memory
5812800Ssaidi@eecs.umich.edu    pTable->allocate(stack_min, roundUp(stack_size, pageSize));
58212SN/A
58312SN/A    // map out initial stack contents
5842523SN/A    Addr argv_array_base = stack_min + intSize; // room for argc
58512SN/A    Addr envp_array_base = argv_array_base + argv_array_size;
58612SN/A    Addr arg_data_base = envp_array_base + envp_array_size;
58712SN/A    Addr env_data_base = arg_data_base + arg_data_size;
58812SN/A
58912SN/A    // write contents to stack
59012SN/A    uint64_t argc = argv.size();
5912474SN/A    if (intSize == 8)
5922474SN/A        argc = htog((uint64_t)argc);
5932474SN/A    else if (intSize == 4)
5942474SN/A        argc = htog((uint32_t)argc);
5952474SN/A    else
5962474SN/A        panic("Unknown int size");
5972474SN/A
5982474SN/A    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);
59912SN/A
6002378SN/A    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);
6012378SN/A    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);
60212SN/A
6034772Sgblack@eecs.umich.edu    assert(NumArgumentRegs >= 2);
6044772Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(ArgumentReg[0], argc);
6054772Sgblack@eecs.umich.edu    threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);
6062680Sktlim@umich.edu    threadContexts[0]->setIntReg(StackPointerReg, stack_min);
6072451SN/A
6082451SN/A    Addr prog_entry = objFile->entryPoint();
6092680Sktlim@umich.edu    threadContexts[0]->setPC(prog_entry);
6102680Sktlim@umich.edu    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));
6112817Sksewell@umich.edu
6122817Sksewell@umich.edu#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc
6132680Sktlim@umich.edu    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));
6142817Sksewell@umich.edu#endif
6152378SN/A
6162378SN/A    num_processes++;
6172SN/A}
6182SN/A
6192093SN/Avoid
6202680Sktlim@umich.eduLiveProcess::syscall(int64_t callnum, ThreadContext *tc)
6212093SN/A{
6222093SN/A    num_syscalls++;
6232093SN/A
6242093SN/A    SyscallDesc *desc = getDesc(callnum);
6252093SN/A    if (desc == NULL)
6262093SN/A        fatal("Syscall %d out of range", callnum);
6272093SN/A
6282680Sktlim@umich.edu    desc->doSyscall(callnum, this, tc);
6292093SN/A}
6302SN/A
6312715Sstever@eecs.umich.eduLiveProcess *
6325154Sgblack@eecs.umich.eduLiveProcess::create(LiveProcessParams * params)
6332715Sstever@eecs.umich.edu{
6342715Sstever@eecs.umich.edu    LiveProcess *process = NULL;
6352715Sstever@eecs.umich.edu
6365154Sgblack@eecs.umich.edu    string executable =
6375154Sgblack@eecs.umich.edu        params->executable == "" ? params->cmd[0] : params->executable;
6382715Sstever@eecs.umich.edu    ObjectFile *objFile = createObjectFile(executable);
6392715Sstever@eecs.umich.edu    if (objFile == NULL) {
6402715Sstever@eecs.umich.edu        fatal("Can't load object file %s", executable);
6412715Sstever@eecs.umich.edu    }
6422715Sstever@eecs.umich.edu
6433917Ssaidi@eecs.umich.edu    if (objFile->isDynamic())
6443917Ssaidi@eecs.umich.edu       fatal("Object file is a dynamic executable however only static "
6455070Ssaidi@eecs.umich.edu             "executables are supported!\n       Please recompile your "
6463917Ssaidi@eecs.umich.edu             "executable as a static binary and try again.\n");
6473917Ssaidi@eecs.umich.edu
6485089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA
6495070Ssaidi@eecs.umich.edu    if (objFile->hasTLS())
6505089Sgblack@eecs.umich.edu        fatal("Object file has a TLS section and single threaded TLS is not\n"
6515089Sgblack@eecs.umich.edu              "       currently supported for Alpha! Please recompile your "
6525089Sgblack@eecs.umich.edu              "executable with \n       a non-TLS toolchain.\n");
6535070Ssaidi@eecs.umich.edu
6542715Sstever@eecs.umich.edu    if (objFile->getArch() != ObjectFile::Alpha)
6552715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (Alpha).");
6562715Sstever@eecs.umich.edu    switch (objFile->getOpSys()) {
6572715Sstever@eecs.umich.edu      case ObjectFile::Tru64:
6585154Sgblack@eecs.umich.edu        process = new AlphaTru64Process(params, objFile);
6592715Sstever@eecs.umich.edu        break;
6602715Sstever@eecs.umich.edu
6612715Sstever@eecs.umich.edu      case ObjectFile::Linux:
6625154Sgblack@eecs.umich.edu        process = new AlphaLinuxProcess(params, objFile);
6632715Sstever@eecs.umich.edu        break;
6642715Sstever@eecs.umich.edu
6652715Sstever@eecs.umich.edu      default:
6662715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
6672715Sstever@eecs.umich.edu    }
6682715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA
6694111Sgblack@eecs.umich.edu    if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32)
6702715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (SPARC).");
6712715Sstever@eecs.umich.edu    switch (objFile->getOpSys()) {
6722715Sstever@eecs.umich.edu      case ObjectFile::Linux:
6734111Sgblack@eecs.umich.edu        if (objFile->getArch() == ObjectFile::SPARC64) {
6745154Sgblack@eecs.umich.edu            process = new Sparc64LinuxProcess(params, objFile);
6754111Sgblack@eecs.umich.edu        } else {
6765154Sgblack@eecs.umich.edu            process = new Sparc32LinuxProcess(params, objFile);
6774111Sgblack@eecs.umich.edu        }
6782715Sstever@eecs.umich.edu        break;
6792715Sstever@eecs.umich.edu
6802715Sstever@eecs.umich.edu
6812715Sstever@eecs.umich.edu      case ObjectFile::Solaris:
6825154Sgblack@eecs.umich.edu        process = new SparcSolarisProcess(params, objFile);
6832715Sstever@eecs.umich.edu        break;
6842715Sstever@eecs.umich.edu      default:
6852715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
6862715Sstever@eecs.umich.edu    }
6874157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA
6884157Sgblack@eecs.umich.edu    if (objFile->getArch() != ObjectFile::X86)
6894166Sgblack@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (x86).");
6904157Sgblack@eecs.umich.edu    switch (objFile->getOpSys()) {
6914166Sgblack@eecs.umich.edu      case ObjectFile::Linux:
6925154Sgblack@eecs.umich.edu        process = new X86LinuxProcess(params, objFile);
6934166Sgblack@eecs.umich.edu        break;
6944157Sgblack@eecs.umich.edu      default:
6954157Sgblack@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
6964157Sgblack@eecs.umich.edu    }
6972715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA
6982715Sstever@eecs.umich.edu    if (objFile->getArch() != ObjectFile::Mips)
6992715Sstever@eecs.umich.edu        fatal("Object file architecture does not match compiled ISA (MIPS).");
7002715Sstever@eecs.umich.edu    switch (objFile->getOpSys()) {
7012715Sstever@eecs.umich.edu      case ObjectFile::Linux:
7025154Sgblack@eecs.umich.edu        process = new MipsLinuxProcess(params, objFile);
7032715Sstever@eecs.umich.edu        break;
7042715Sstever@eecs.umich.edu
7052715Sstever@eecs.umich.edu      default:
7062715Sstever@eecs.umich.edu        fatal("Unknown/unsupported operating system.");
7072715Sstever@eecs.umich.edu    }
7085335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA
7095335Shines@cs.fsu.edu    if (objFile->getArch() != ObjectFile::Arm)
7105335Shines@cs.fsu.edu        fatal("Object file architecture does not match compiled ISA (ARM).");
7115335Shines@cs.fsu.edu    switch (objFile->getOpSys()) {
7125335Shines@cs.fsu.edu      case ObjectFile::Linux:
7135335Shines@cs.fsu.edu        process = new ArmLinuxProcess(params, objFile);
7145335Shines@cs.fsu.edu        break;
7155335Shines@cs.fsu.edu
7165335Shines@cs.fsu.edu      default:
7175335Shines@cs.fsu.edu        fatal("Unknown/unsupported operating system.");
7185335Shines@cs.fsu.edu    }
7192715Sstever@eecs.umich.edu#else
7202715Sstever@eecs.umich.edu#error "THE_ISA not set"
7212715Sstever@eecs.umich.edu#endif
7222715Sstever@eecs.umich.edu
7232715Sstever@eecs.umich.edu
7242715Sstever@eecs.umich.edu    if (process == NULL)
7252715Sstever@eecs.umich.edu        fatal("Unknown error creating process object.");
7262715Sstever@eecs.umich.edu    return process;
7272715Sstever@eecs.umich.edu}
7282715Sstever@eecs.umich.edu
7294762Snate@binkert.orgLiveProcess *
7304762Snate@binkert.orgLiveProcessParams::create()
7312715Sstever@eecs.umich.edu{
7325154Sgblack@eecs.umich.edu    return LiveProcess::create(this);
7332715Sstever@eecs.umich.edu}
734