process.cc revision 5514
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; 955514SMichael.Adler@intel.com string err = params->errout; 965154Sgblack@eecs.umich.edu 975154Sgblack@eecs.umich.edu // initialize file descriptors to default: same as simulator 985154Sgblack@eecs.umich.edu int stdin_fd, stdout_fd, stderr_fd; 995154Sgblack@eecs.umich.edu 1005154Sgblack@eecs.umich.edu if (in == "stdin" || in == "cin") 1015154Sgblack@eecs.umich.edu stdin_fd = STDIN_FILENO; 1025154Sgblack@eecs.umich.edu else if (in == "None") 1035154Sgblack@eecs.umich.edu stdin_fd = -1; 1045154Sgblack@eecs.umich.edu else 1055154Sgblack@eecs.umich.edu stdin_fd = Process::openInputFile(in); 1065154Sgblack@eecs.umich.edu 1075154Sgblack@eecs.umich.edu if (out == "stdout" || out == "cout") 1085154Sgblack@eecs.umich.edu stdout_fd = STDOUT_FILENO; 1095154Sgblack@eecs.umich.edu else if (out == "stderr" || out == "cerr") 1105154Sgblack@eecs.umich.edu stdout_fd = STDERR_FILENO; 1115154Sgblack@eecs.umich.edu else if (out == "None") 1125154Sgblack@eecs.umich.edu stdout_fd = -1; 1135154Sgblack@eecs.umich.edu else 1145154Sgblack@eecs.umich.edu stdout_fd = Process::openOutputFile(out); 1155154Sgblack@eecs.umich.edu 1165514SMichael.Adler@intel.com if (err == "stdout" || err == "cout") 1175514SMichael.Adler@intel.com stderr_fd = STDOUT_FILENO; 1185514SMichael.Adler@intel.com else if (err == "stderr" || err == "cerr") 1195514SMichael.Adler@intel.com stderr_fd = STDERR_FILENO; 1205514SMichael.Adler@intel.com else if (err == "None") 1215514SMichael.Adler@intel.com stderr_fd = -1; 1225514SMichael.Adler@intel.com else if (err == out) 1235514SMichael.Adler@intel.com stderr_fd = stdout_fd; 1245514SMichael.Adler@intel.com else 1255514SMichael.Adler@intel.com stderr_fd = Process::openOutputFile(err); 1265154Sgblack@eecs.umich.edu 1274997Sgblack@eecs.umich.edu M5_pid = system->allocatePID(); 1282SN/A // initialize first 3 fds (stdin, stdout, stderr) 1295282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[STDIN_FILENO]; 1305282Srstrong@cs.ucsd.edu fdo->fd = stdin_fd; 1315282Srstrong@cs.ucsd.edu fdo->filename = in; 1325282Srstrong@cs.ucsd.edu fdo->flags = O_RDONLY; 1335282Srstrong@cs.ucsd.edu fdo->mode = -1; 1345282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 1355282Srstrong@cs.ucsd.edu 1365282Srstrong@cs.ucsd.edu fdo = &fd_map[STDOUT_FILENO]; 1375282Srstrong@cs.ucsd.edu fdo->fd = stdout_fd; 1385282Srstrong@cs.ucsd.edu fdo->filename = out; 1395282Srstrong@cs.ucsd.edu fdo->flags = O_WRONLY | O_CREAT | O_TRUNC; 1405282Srstrong@cs.ucsd.edu fdo->mode = 0774; 1415282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 1425282Srstrong@cs.ucsd.edu 1435282Srstrong@cs.ucsd.edu fdo = &fd_map[STDERR_FILENO]; 1445282Srstrong@cs.ucsd.edu fdo->fd = stderr_fd; 1455514SMichael.Adler@intel.com fdo->filename = err; 1465282Srstrong@cs.ucsd.edu fdo->flags = O_WRONLY; 1475282Srstrong@cs.ucsd.edu fdo->mode = -1; 1485282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 1495282Srstrong@cs.ucsd.edu 1502SN/A 1512SN/A // mark remaining fds as free 1522SN/A for (int i = 3; i <= MAX_FD; ++i) { 1535282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[i]; 1545282Srstrong@cs.ucsd.edu fdo->fd = -1; 1552SN/A } 1562SN/A 1571450SN/A mmap_start = mmap_end = 0; 1581514SN/A nxm_start = nxm_end = 0; 1595184Sgblack@eecs.umich.edu pTable = new PageTable(this); 1602SN/A // other parameters will be initialized when the program is loaded 1612SN/A} 1622SN/A 1632378SN/A 1642SN/Avoid 1652SN/AProcess::regStats() 1662SN/A{ 167729SN/A using namespace Stats; 1682SN/A 1692SN/A num_syscalls 1702SN/A .name(name() + ".PROG:num_syscalls") 1712SN/A .desc("Number of system calls") 1722SN/A ; 1732SN/A} 1742SN/A 1752SN/A// 1762SN/A// static helper functions 1772SN/A// 1782SN/Aint 1792SN/AProcess::openInputFile(const string &filename) 1802SN/A{ 1812SN/A int fd = open(filename.c_str(), O_RDONLY); 1822SN/A 1832SN/A if (fd == -1) { 1842SN/A perror(NULL); 1852SN/A cerr << "unable to open \"" << filename << "\" for reading\n"; 1862SN/A fatal("can't open input file"); 1872SN/A } 1882SN/A 1892SN/A return fd; 1902SN/A} 1912SN/A 1922SN/A 1932SN/Aint 1942SN/AProcess::openOutputFile(const string &filename) 1952SN/A{ 1965514SMichael.Adler@intel.com int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664); 1972SN/A 1982SN/A if (fd == -1) { 1992SN/A perror(NULL); 2002SN/A cerr << "unable to open \"" << filename << "\" for writing\n"; 2012SN/A fatal("can't open output file"); 2022SN/A } 2032SN/A 2042SN/A return fd; 2052SN/A} 2062SN/A 2072SN/A 208180SN/Aint 2092680Sktlim@umich.eduProcess::registerThreadContext(ThreadContext *tc) 2102SN/A{ 211180SN/A // add to list 2122680Sktlim@umich.edu int myIndex = threadContexts.size(); 2132680Sktlim@umich.edu threadContexts.push_back(tc); 214180SN/A 2155512SMichael.Adler@intel.com int port = getRemoteGDBPort(); 2165512SMichael.Adler@intel.com if (port) { 2175512SMichael.Adler@intel.com RemoteGDB *rgdb = new RemoteGDB(system, tc); 2185512SMichael.Adler@intel.com GDBListener *gdbl = new GDBListener(rgdb, port + myIndex); 2195512SMichael.Adler@intel.com gdbl->listen(); 2203971Sgblack@eecs.umich.edu 2215512SMichael.Adler@intel.com remoteGDB.push_back(rgdb); 2225512SMichael.Adler@intel.com } 2233971Sgblack@eecs.umich.edu 2242378SN/A // return CPU number to caller 225180SN/A return myIndex; 2262SN/A} 2272SN/A 2281395SN/Avoid 2291395SN/AProcess::startup() 2301395SN/A{ 2312680Sktlim@umich.edu if (threadContexts.empty()) 2322378SN/A fatal("Process %s is not associated with any CPUs!\n", name()); 2332378SN/A 2342680Sktlim@umich.edu // first thread context for this process... initialize & enable 2352680Sktlim@umich.edu ThreadContext *tc = threadContexts[0]; 2361395SN/A 2371634SN/A // mark this context as active so it will start ticking. 2382680Sktlim@umich.edu tc->activate(0); 2392462SN/A 2402519SN/A Port *mem_port; 2412519SN/A mem_port = system->physmem->getPort("functional"); 2424434Ssaidi@eecs.umich.edu initVirtMem = new TranslatingPort("process init port", this, 2434434Ssaidi@eecs.umich.edu TranslatingPort::Always); 2442519SN/A mem_port->setPeer(initVirtMem); 2452519SN/A initVirtMem->setPeer(mem_port); 2461395SN/A} 2472SN/A 248180SN/Avoid 2492680Sktlim@umich.eduProcess::replaceThreadContext(ThreadContext *tc, int tcIndex) 250180SN/A{ 2512680Sktlim@umich.edu if (tcIndex >= threadContexts.size()) { 2522680Sktlim@umich.edu panic("replaceThreadContext: bad tcIndex, %d >= %d\n", 2532680Sktlim@umich.edu tcIndex, threadContexts.size()); 254180SN/A } 255180SN/A 2562680Sktlim@umich.edu threadContexts[tcIndex] = tc; 257180SN/A} 258180SN/A 2592SN/A// map simulator fd sim_fd to target fd tgt_fd 2602SN/Avoid 2612SN/AProcess::dup_fd(int sim_fd, int tgt_fd) 2622SN/A{ 2632SN/A if (tgt_fd < 0 || tgt_fd > MAX_FD) 2642SN/A panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 2652SN/A 2665282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[tgt_fd]; 2675282Srstrong@cs.ucsd.edu fdo->fd = sim_fd; 2682SN/A} 2692SN/A 2702SN/A 2712SN/A// generate new target fd for sim_fd 2722SN/Aint 2735282Srstrong@cs.ucsd.eduProcess::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe) 2742SN/A{ 2752SN/A // in case open() returns an error, don't allocate a new fd 2762SN/A if (sim_fd == -1) 2772SN/A return -1; 2782SN/A 2792SN/A // find first free target fd 2805282Srstrong@cs.ucsd.edu for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 2815282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[free_fd]; 2825282Srstrong@cs.ucsd.edu if (fdo->fd == -1) { 2835282Srstrong@cs.ucsd.edu fdo->fd = sim_fd; 2845282Srstrong@cs.ucsd.edu fdo->filename = filename; 2855282Srstrong@cs.ucsd.edu fdo->mode = mode; 2865282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 2875282Srstrong@cs.ucsd.edu fdo->flags = flags; 2885282Srstrong@cs.ucsd.edu fdo->isPipe = pipe; 2895282Srstrong@cs.ucsd.edu fdo->readPipeSource = 0; 2901970SN/A return free_fd; 2911970SN/A } 2922SN/A } 2932SN/A 2941970SN/A panic("Process::alloc_fd: out of file descriptors!"); 2951970SN/A} 2962SN/A 2971970SN/A 2981970SN/A// free target fd (e.g., after close) 2991970SN/Avoid 3001970SN/AProcess::free_fd(int tgt_fd) 3011970SN/A{ 3025282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[tgt_fd]; 3035282Srstrong@cs.ucsd.edu if (fdo->fd == -1) 3041970SN/A warn("Process::free_fd: request to free unused fd %d", tgt_fd); 3051970SN/A 3065282Srstrong@cs.ucsd.edu fdo->fd = -1; 3075282Srstrong@cs.ucsd.edu fdo->filename = "NULL"; 3085282Srstrong@cs.ucsd.edu fdo->mode = 0; 3095282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 3105282Srstrong@cs.ucsd.edu fdo->flags = 0; 3115282Srstrong@cs.ucsd.edu fdo->isPipe = false; 3125282Srstrong@cs.ucsd.edu fdo->readPipeSource = 0; 3132SN/A} 3142SN/A 3152SN/A 3162SN/A// look up simulator fd for given target fd 3172SN/Aint 3182SN/AProcess::sim_fd(int tgt_fd) 3192SN/A{ 3202SN/A if (tgt_fd > MAX_FD) 3212SN/A return -1; 3222SN/A 3235282Srstrong@cs.ucsd.edu return fd_map[tgt_fd].fd; 3242SN/A} 3252SN/A 3265282Srstrong@cs.ucsd.eduProcess::FdMap * 3275282Srstrong@cs.ucsd.eduProcess::sim_fd_obj(int tgt_fd) 3285282Srstrong@cs.ucsd.edu{ 3295282Srstrong@cs.ucsd.edu if (tgt_fd > MAX_FD) 3305282Srstrong@cs.ucsd.edu panic("sim_fd_obj called in fd out of range."); 3315282Srstrong@cs.ucsd.edu 3325282Srstrong@cs.ucsd.edu return &fd_map[tgt_fd]; 3335282Srstrong@cs.ucsd.edu} 3344434Ssaidi@eecs.umich.edubool 3354434Ssaidi@eecs.umich.eduProcess::checkAndAllocNextPage(Addr vaddr) 3364434Ssaidi@eecs.umich.edu{ 3374434Ssaidi@eecs.umich.edu // if this is an initial write we might not have 3384434Ssaidi@eecs.umich.edu if (vaddr >= stack_min && vaddr < stack_base) { 3394434Ssaidi@eecs.umich.edu pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 3404434Ssaidi@eecs.umich.edu return true; 3414434Ssaidi@eecs.umich.edu } 3424434Ssaidi@eecs.umich.edu 3434434Ssaidi@eecs.umich.edu // We've accessed the next page of the stack, so extend the stack 3444434Ssaidi@eecs.umich.edu // to cover it. 3455154Sgblack@eecs.umich.edu if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 3465154Sgblack@eecs.umich.edu while (vaddr < stack_min) { 3475154Sgblack@eecs.umich.edu stack_min -= TheISA::PageBytes; 3485154Sgblack@eecs.umich.edu if(stack_base - stack_min > max_stack_size) 3495154Sgblack@eecs.umich.edu fatal("Maximum stack size exceeded\n"); 3505154Sgblack@eecs.umich.edu if(stack_base - stack_min > 8*1024*1024) 3515154Sgblack@eecs.umich.edu fatal("Over max stack size for one thread\n"); 3525154Sgblack@eecs.umich.edu pTable->allocate(stack_min, TheISA::PageBytes); 3535154Sgblack@eecs.umich.edu warn("Increasing stack size by one page."); 3545154Sgblack@eecs.umich.edu }; 3554434Ssaidi@eecs.umich.edu return true; 3564434Ssaidi@eecs.umich.edu } 3574434Ssaidi@eecs.umich.edu return false; 3584434Ssaidi@eecs.umich.edu} 3594434Ssaidi@eecs.umich.edu 3605282Srstrong@cs.ucsd.edu // find all offsets for currently open files and save them 3615282Srstrong@cs.ucsd.eduvoid 3625282Srstrong@cs.ucsd.eduProcess::fix_file_offsets() { 3635282Srstrong@cs.ucsd.edu Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO]; 3645282Srstrong@cs.ucsd.edu Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO]; 3655282Srstrong@cs.ucsd.edu Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO]; 3665282Srstrong@cs.ucsd.edu string in = fdo_stdin->filename; 3675282Srstrong@cs.ucsd.edu string out = fdo_stdout->filename; 3685514SMichael.Adler@intel.com string err = fdo_stderr->filename; 3695282Srstrong@cs.ucsd.edu 3705282Srstrong@cs.ucsd.edu // initialize file descriptors to default: same as simulator 3715282Srstrong@cs.ucsd.edu int stdin_fd, stdout_fd, stderr_fd; 3725282Srstrong@cs.ucsd.edu 3735282Srstrong@cs.ucsd.edu if (in == "stdin" || in == "cin") 3745282Srstrong@cs.ucsd.edu stdin_fd = STDIN_FILENO; 3755282Srstrong@cs.ucsd.edu else if (in == "None") 3765282Srstrong@cs.ucsd.edu stdin_fd = -1; 3775282Srstrong@cs.ucsd.edu else{ 3785282Srstrong@cs.ucsd.edu //OPEN standard in and seek to the right location 3795282Srstrong@cs.ucsd.edu stdin_fd = Process::openInputFile(in); 3805282Srstrong@cs.ucsd.edu if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0) 3815282Srstrong@cs.ucsd.edu panic("Unable to seek to correct location in file: %s", in); 3825282Srstrong@cs.ucsd.edu } 3835282Srstrong@cs.ucsd.edu 3845282Srstrong@cs.ucsd.edu if (out == "stdout" || out == "cout") 3855282Srstrong@cs.ucsd.edu stdout_fd = STDOUT_FILENO; 3865282Srstrong@cs.ucsd.edu else if (out == "stderr" || out == "cerr") 3875282Srstrong@cs.ucsd.edu stdout_fd = STDERR_FILENO; 3885282Srstrong@cs.ucsd.edu else if (out == "None") 3895282Srstrong@cs.ucsd.edu stdout_fd = -1; 3905282Srstrong@cs.ucsd.edu else{ 3915282Srstrong@cs.ucsd.edu stdout_fd = Process::openOutputFile(out); 3925514SMichael.Adler@intel.com if (lseek(stdout_fd, fdo_stdout->fileOffset, SEEK_SET) < 0) 3935514SMichael.Adler@intel.com panic("Unable to seek to correct location in file: %s", out); 3945282Srstrong@cs.ucsd.edu } 3955282Srstrong@cs.ucsd.edu 3965514SMichael.Adler@intel.com if (err == "stdout" || err == "cout") 3975514SMichael.Adler@intel.com stderr_fd = STDOUT_FILENO; 3985514SMichael.Adler@intel.com else if (err == "stderr" || err == "cerr") 3995514SMichael.Adler@intel.com stderr_fd = STDERR_FILENO; 4005514SMichael.Adler@intel.com else if (err == "None") 4015514SMichael.Adler@intel.com stderr_fd = -1; 4025514SMichael.Adler@intel.com else if (err == out) 4035514SMichael.Adler@intel.com stderr_fd = stdout_fd; 4045514SMichael.Adler@intel.com else { 4055514SMichael.Adler@intel.com stderr_fd = Process::openOutputFile(err); 4065514SMichael.Adler@intel.com if (lseek(stderr_fd, fdo_stderr->fileOffset, SEEK_SET) < 0) 4075514SMichael.Adler@intel.com panic("Unable to seek to correct location in file: %s", err); 4085514SMichael.Adler@intel.com } 4095282Srstrong@cs.ucsd.edu 4105282Srstrong@cs.ucsd.edu fdo_stdin->fd = stdin_fd; 4115282Srstrong@cs.ucsd.edu fdo_stdout->fd = stdout_fd; 4125282Srstrong@cs.ucsd.edu fdo_stderr->fd = stderr_fd; 4135282Srstrong@cs.ucsd.edu 4145282Srstrong@cs.ucsd.edu 4155282Srstrong@cs.ucsd.edu for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) { 4165282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[free_fd]; 4175282Srstrong@cs.ucsd.edu if (fdo->fd != -1) { 4185282Srstrong@cs.ucsd.edu if (fdo->isPipe){ 4195282Srstrong@cs.ucsd.edu if (fdo->filename == "PIPE-WRITE") 4205282Srstrong@cs.ucsd.edu continue; 4215282Srstrong@cs.ucsd.edu else { 4225282Srstrong@cs.ucsd.edu assert (fdo->filename == "PIPE-READ"); 4235282Srstrong@cs.ucsd.edu //create a new pipe 4245282Srstrong@cs.ucsd.edu int fds[2]; 4255282Srstrong@cs.ucsd.edu int pipe_retval = pipe(fds); 4265282Srstrong@cs.ucsd.edu 4275282Srstrong@cs.ucsd.edu if (pipe_retval < 0) { 4285282Srstrong@cs.ucsd.edu // error 4295282Srstrong@cs.ucsd.edu panic("Unable to create new pipe."); 4305282Srstrong@cs.ucsd.edu } 4315282Srstrong@cs.ucsd.edu fdo->fd = fds[0]; //set read pipe 4325282Srstrong@cs.ucsd.edu Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource]; 4335282Srstrong@cs.ucsd.edu if (fdo_write->filename != "PIPE-WRITE") 4345282Srstrong@cs.ucsd.edu panic ("Couldn't find write end of the pipe"); 4355282Srstrong@cs.ucsd.edu 4365282Srstrong@cs.ucsd.edu fdo_write->fd = fds[1];//set write pipe 4375282Srstrong@cs.ucsd.edu } 4385282Srstrong@cs.ucsd.edu } else { 4395282Srstrong@cs.ucsd.edu //Open file 4405282Srstrong@cs.ucsd.edu int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode); 4415282Srstrong@cs.ucsd.edu 4425282Srstrong@cs.ucsd.edu if (fd == -1) 4435282Srstrong@cs.ucsd.edu panic("Unable to open file: %s", fdo->filename); 4445282Srstrong@cs.ucsd.edu fdo->fd = fd; 4455282Srstrong@cs.ucsd.edu 4465282Srstrong@cs.ucsd.edu //Seek to correct location before checkpoint 4475282Srstrong@cs.ucsd.edu if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0) 4485282Srstrong@cs.ucsd.edu panic("Unable to seek to correct location in file: %s", fdo->filename); 4495282Srstrong@cs.ucsd.edu } 4505282Srstrong@cs.ucsd.edu } 4515282Srstrong@cs.ucsd.edu } 4525282Srstrong@cs.ucsd.edu} 4535282Srstrong@cs.ucsd.eduvoid 4545282Srstrong@cs.ucsd.eduProcess::find_file_offsets(){ 4555282Srstrong@cs.ucsd.edu for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 4565282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[free_fd]; 4575282Srstrong@cs.ucsd.edu if (fdo->fd != -1) { 4585282Srstrong@cs.ucsd.edu fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR); 4595282Srstrong@cs.ucsd.edu } else { 4605282Srstrong@cs.ucsd.edu fdo->filename = "NULL"; 4615282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 4625282Srstrong@cs.ucsd.edu } 4635282Srstrong@cs.ucsd.edu } 4645282Srstrong@cs.ucsd.edu} 4655282Srstrong@cs.ucsd.edu 4665282Srstrong@cs.ucsd.eduvoid 4675282Srstrong@cs.ucsd.eduProcess::setReadPipeSource(int read_pipe_fd, int source_fd){ 4685282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[read_pipe_fd]; 4695282Srstrong@cs.ucsd.edu fdo->readPipeSource = source_fd; 4705282Srstrong@cs.ucsd.edu} 4715282Srstrong@cs.ucsd.edu 4725282Srstrong@cs.ucsd.eduvoid 4735282Srstrong@cs.ucsd.eduProcess::FdMap::serialize(std::ostream &os) 4745282Srstrong@cs.ucsd.edu{ 4755282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(fd); 4765282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(isPipe); 4775282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(filename); 4785282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(flags); 4795282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(readPipeSource); 4805282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(fileOffset); 4815282Srstrong@cs.ucsd.edu} 4825282Srstrong@cs.ucsd.edu 4835282Srstrong@cs.ucsd.eduvoid 4845282Srstrong@cs.ucsd.eduProcess::FdMap::unserialize(Checkpoint *cp, const std::string §ion) 4855282Srstrong@cs.ucsd.edu{ 4865282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(fd); 4875282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(isPipe); 4885282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(filename); 4895282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(flags); 4905282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(readPipeSource); 4915282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(fileOffset); 4925282Srstrong@cs.ucsd.edu} 4935282Srstrong@cs.ucsd.edu 4943311Ssaidi@eecs.umich.eduvoid 4953311Ssaidi@eecs.umich.eduProcess::serialize(std::ostream &os) 4963311Ssaidi@eecs.umich.edu{ 4973311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(initialContextLoaded); 4983311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(brk_point); 4993311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_base); 5003311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_size); 5013311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_min); 5023311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(next_thread_stack_base); 5033311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(mmap_start); 5043311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(mmap_end); 5053311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(nxm_start); 5063311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(nxm_end); 5075282Srstrong@cs.ucsd.edu find_file_offsets(); 5085282Srstrong@cs.ucsd.edu pTable->serialize(os); 5095282Srstrong@cs.ucsd.edu for (int x = 0; x <= MAX_FD; x++) { 5105282Srstrong@cs.ucsd.edu nameOut(os, csprintf("%s.FdMap%d", name(), x)); 5115282Srstrong@cs.ucsd.edu fd_map[x].serialize(os); 5125282Srstrong@cs.ucsd.edu } 5133311Ssaidi@eecs.umich.edu 5143311Ssaidi@eecs.umich.edu} 5153311Ssaidi@eecs.umich.edu 5163311Ssaidi@eecs.umich.eduvoid 5173311Ssaidi@eecs.umich.eduProcess::unserialize(Checkpoint *cp, const std::string §ion) 5183311Ssaidi@eecs.umich.edu{ 5193311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(initialContextLoaded); 5203311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(brk_point); 5213311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_base); 5223311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_size); 5233311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_min); 5243311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(next_thread_stack_base); 5253311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(mmap_start); 5263311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(mmap_end); 5273311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(nxm_start); 5283311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(nxm_end); 5293311Ssaidi@eecs.umich.edu pTable->unserialize(cp, section); 5305282Srstrong@cs.ucsd.edu for (int x = 0; x <= MAX_FD; x++) { 5315282Srstrong@cs.ucsd.edu fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x)); 5325282Srstrong@cs.ucsd.edu } 5335282Srstrong@cs.ucsd.edu fix_file_offsets(); 5345183Ssaidi@eecs.umich.edu 5355183Ssaidi@eecs.umich.edu checkpointRestored = true; 5365183Ssaidi@eecs.umich.edu 5373311Ssaidi@eecs.umich.edu} 5382SN/A 5392SN/A 5402SN/A//////////////////////////////////////////////////////////////////////// 5412SN/A// 5422SN/A// LiveProcess member definitions 5432SN/A// 5442SN/A//////////////////////////////////////////////////////////////////////// 5452SN/A 54612SN/A 5475154Sgblack@eecs.umich.eduLiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile) 5485154Sgblack@eecs.umich.edu : Process(params), objFile(_objFile), 5495154Sgblack@eecs.umich.edu argv(params->cmd), envp(params->env), cwd(params->cwd) 5502SN/A{ 5515154Sgblack@eecs.umich.edu __uid = params->uid; 5525154Sgblack@eecs.umich.edu __euid = params->euid; 5535154Sgblack@eecs.umich.edu __gid = params->gid; 5545154Sgblack@eecs.umich.edu __egid = params->egid; 5555154Sgblack@eecs.umich.edu __pid = params->pid; 5565154Sgblack@eecs.umich.edu __ppid = params->ppid; 5573114Sgblack@eecs.umich.edu 5585154Sgblack@eecs.umich.edu prog_fname = params->cmd[0]; 55912SN/A 5601158SN/A // load up symbols, if any... these may be used for debugging or 5611158SN/A // profiling. 5621158SN/A if (!debugSymbolTable) { 5631158SN/A debugSymbolTable = new SymbolTable(); 5641158SN/A if (!objFile->loadGlobalSymbols(debugSymbolTable) || 5651158SN/A !objFile->loadLocalSymbols(debugSymbolTable)) { 5661158SN/A // didn't load any symbols 5671158SN/A delete debugSymbolTable; 5681158SN/A debugSymbolTable = NULL; 5691158SN/A } 5701158SN/A } 5712378SN/A} 5721158SN/A 5732378SN/Avoid 5742474SN/ALiveProcess::argsInit(int intSize, int pageSize) 5752378SN/A{ 5762378SN/A Process::startup(); 57712SN/A 5782378SN/A // load object file into target memory 5792378SN/A objFile->loadSections(initVirtMem); 58012SN/A 58112SN/A // Calculate how much space we need for arg & env arrays. 5822474SN/A int argv_array_size = intSize * (argv.size() + 1); 5832474SN/A int envp_array_size = intSize * (envp.size() + 1); 58412SN/A int arg_data_size = 0; 58512SN/A for (int i = 0; i < argv.size(); ++i) { 58612SN/A arg_data_size += argv[i].size() + 1; 58712SN/A } 58812SN/A int env_data_size = 0; 58912SN/A for (int i = 0; i < envp.size(); ++i) { 59012SN/A env_data_size += envp[i].size() + 1; 59112SN/A } 59212SN/A 59312SN/A int space_needed = 59412SN/A argv_array_size + envp_array_size + arg_data_size + env_data_size; 5953005Sstever@eecs.umich.edu if (space_needed < 32*1024) 5963005Sstever@eecs.umich.edu space_needed = 32*1024; 59712SN/A 59812SN/A // set bottom of stack 59912SN/A stack_min = stack_base - space_needed; 60012SN/A // align it 6012800Ssaidi@eecs.umich.edu stack_min = roundDown(stack_min, pageSize); 60212SN/A stack_size = stack_base - stack_min; 6032378SN/A // map memory 6042800Ssaidi@eecs.umich.edu pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 60512SN/A 60612SN/A // map out initial stack contents 6072523SN/A Addr argv_array_base = stack_min + intSize; // room for argc 60812SN/A Addr envp_array_base = argv_array_base + argv_array_size; 60912SN/A Addr arg_data_base = envp_array_base + envp_array_size; 61012SN/A Addr env_data_base = arg_data_base + arg_data_size; 61112SN/A 61212SN/A // write contents to stack 61312SN/A uint64_t argc = argv.size(); 6142474SN/A if (intSize == 8) 6152474SN/A argc = htog((uint64_t)argc); 6162474SN/A else if (intSize == 4) 6172474SN/A argc = htog((uint32_t)argc); 6182474SN/A else 6192474SN/A panic("Unknown int size"); 6202474SN/A 6212474SN/A initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 62212SN/A 6232378SN/A copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 6242378SN/A copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 62512SN/A 6264772Sgblack@eecs.umich.edu assert(NumArgumentRegs >= 2); 6274772Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(ArgumentReg[0], argc); 6284772Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base); 6292680Sktlim@umich.edu threadContexts[0]->setIntReg(StackPointerReg, stack_min); 6302451SN/A 6312451SN/A Addr prog_entry = objFile->entryPoint(); 6322680Sktlim@umich.edu threadContexts[0]->setPC(prog_entry); 6332680Sktlim@umich.edu threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 6342817Sksewell@umich.edu 6352817Sksewell@umich.edu#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 6362680Sktlim@umich.edu threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 6372817Sksewell@umich.edu#endif 6382378SN/A 6392378SN/A num_processes++; 6402SN/A} 6412SN/A 6422093SN/Avoid 6432680Sktlim@umich.eduLiveProcess::syscall(int64_t callnum, ThreadContext *tc) 6442093SN/A{ 6452093SN/A num_syscalls++; 6462093SN/A 6472093SN/A SyscallDesc *desc = getDesc(callnum); 6482093SN/A if (desc == NULL) 6492093SN/A fatal("Syscall %d out of range", callnum); 6502093SN/A 6512680Sktlim@umich.edu desc->doSyscall(callnum, this, tc); 6522093SN/A} 6532SN/A 6542715Sstever@eecs.umich.eduLiveProcess * 6555154Sgblack@eecs.umich.eduLiveProcess::create(LiveProcessParams * params) 6562715Sstever@eecs.umich.edu{ 6572715Sstever@eecs.umich.edu LiveProcess *process = NULL; 6582715Sstever@eecs.umich.edu 6595154Sgblack@eecs.umich.edu string executable = 6605154Sgblack@eecs.umich.edu params->executable == "" ? params->cmd[0] : params->executable; 6612715Sstever@eecs.umich.edu ObjectFile *objFile = createObjectFile(executable); 6622715Sstever@eecs.umich.edu if (objFile == NULL) { 6632715Sstever@eecs.umich.edu fatal("Can't load object file %s", executable); 6642715Sstever@eecs.umich.edu } 6652715Sstever@eecs.umich.edu 6663917Ssaidi@eecs.umich.edu if (objFile->isDynamic()) 6673917Ssaidi@eecs.umich.edu fatal("Object file is a dynamic executable however only static " 6685070Ssaidi@eecs.umich.edu "executables are supported!\n Please recompile your " 6693917Ssaidi@eecs.umich.edu "executable as a static binary and try again.\n"); 6703917Ssaidi@eecs.umich.edu 6715089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA 6725070Ssaidi@eecs.umich.edu if (objFile->hasTLS()) 6735089Sgblack@eecs.umich.edu fatal("Object file has a TLS section and single threaded TLS is not\n" 6745089Sgblack@eecs.umich.edu " currently supported for Alpha! Please recompile your " 6755089Sgblack@eecs.umich.edu "executable with \n a non-TLS toolchain.\n"); 6765070Ssaidi@eecs.umich.edu 6772715Sstever@eecs.umich.edu if (objFile->getArch() != ObjectFile::Alpha) 6782715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (Alpha)."); 6792715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6802715Sstever@eecs.umich.edu case ObjectFile::Tru64: 6815154Sgblack@eecs.umich.edu process = new AlphaTru64Process(params, objFile); 6822715Sstever@eecs.umich.edu break; 6832715Sstever@eecs.umich.edu 6842715Sstever@eecs.umich.edu case ObjectFile::Linux: 6855154Sgblack@eecs.umich.edu process = new AlphaLinuxProcess(params, objFile); 6862715Sstever@eecs.umich.edu break; 6872715Sstever@eecs.umich.edu 6882715Sstever@eecs.umich.edu default: 6892715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6902715Sstever@eecs.umich.edu } 6912715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 6924111Sgblack@eecs.umich.edu if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32) 6932715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (SPARC)."); 6942715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6952715Sstever@eecs.umich.edu case ObjectFile::Linux: 6964111Sgblack@eecs.umich.edu if (objFile->getArch() == ObjectFile::SPARC64) { 6975154Sgblack@eecs.umich.edu process = new Sparc64LinuxProcess(params, objFile); 6984111Sgblack@eecs.umich.edu } else { 6995154Sgblack@eecs.umich.edu process = new Sparc32LinuxProcess(params, objFile); 7004111Sgblack@eecs.umich.edu } 7012715Sstever@eecs.umich.edu break; 7022715Sstever@eecs.umich.edu 7032715Sstever@eecs.umich.edu 7042715Sstever@eecs.umich.edu case ObjectFile::Solaris: 7055154Sgblack@eecs.umich.edu process = new SparcSolarisProcess(params, objFile); 7062715Sstever@eecs.umich.edu break; 7072715Sstever@eecs.umich.edu default: 7082715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 7092715Sstever@eecs.umich.edu } 7104157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 7114157Sgblack@eecs.umich.edu if (objFile->getArch() != ObjectFile::X86) 7124166Sgblack@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (x86)."); 7134157Sgblack@eecs.umich.edu switch (objFile->getOpSys()) { 7144166Sgblack@eecs.umich.edu case ObjectFile::Linux: 7155154Sgblack@eecs.umich.edu process = new X86LinuxProcess(params, objFile); 7164166Sgblack@eecs.umich.edu break; 7174157Sgblack@eecs.umich.edu default: 7184157Sgblack@eecs.umich.edu fatal("Unknown/unsupported operating system."); 7194157Sgblack@eecs.umich.edu } 7202715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 7212715Sstever@eecs.umich.edu if (objFile->getArch() != ObjectFile::Mips) 7222715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (MIPS)."); 7232715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 7242715Sstever@eecs.umich.edu case ObjectFile::Linux: 7255154Sgblack@eecs.umich.edu process = new MipsLinuxProcess(params, objFile); 7262715Sstever@eecs.umich.edu break; 7272715Sstever@eecs.umich.edu 7282715Sstever@eecs.umich.edu default: 7292715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 7302715Sstever@eecs.umich.edu } 7315335Shines@cs.fsu.edu#elif THE_ISA == ARM_ISA 7325335Shines@cs.fsu.edu if (objFile->getArch() != ObjectFile::Arm) 7335335Shines@cs.fsu.edu fatal("Object file architecture does not match compiled ISA (ARM)."); 7345335Shines@cs.fsu.edu switch (objFile->getOpSys()) { 7355335Shines@cs.fsu.edu case ObjectFile::Linux: 7365335Shines@cs.fsu.edu process = new ArmLinuxProcess(params, objFile); 7375335Shines@cs.fsu.edu break; 7385335Shines@cs.fsu.edu 7395335Shines@cs.fsu.edu default: 7405335Shines@cs.fsu.edu fatal("Unknown/unsupported operating system."); 7415335Shines@cs.fsu.edu } 7422715Sstever@eecs.umich.edu#else 7432715Sstever@eecs.umich.edu#error "THE_ISA not set" 7442715Sstever@eecs.umich.edu#endif 7452715Sstever@eecs.umich.edu 7462715Sstever@eecs.umich.edu 7472715Sstever@eecs.umich.edu if (process == NULL) 7482715Sstever@eecs.umich.edu fatal("Unknown error creating process object."); 7492715Sstever@eecs.umich.edu return process; 7502715Sstever@eecs.umich.edu} 7512715Sstever@eecs.umich.edu 7524762Snate@binkert.orgLiveProcess * 7534762Snate@binkert.orgLiveProcessParams::create() 7542715Sstever@eecs.umich.edu{ 7555154Sgblack@eecs.umich.edu return LiveProcess::create(this); 7562715Sstever@eecs.umich.edu} 757