process.cc revision 5282
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" 49360SN/A#include "sim/process.hh" 504434Ssaidi@eecs.umich.edu#include "sim/process_impl.hh" 51695SN/A#include "sim/stats.hh" 522093SN/A#include "sim/syscall_emul.hh" 532378SN/A#include "sim/system.hh" 542SN/A 552715Sstever@eecs.umich.edu#include "arch/isa_specific.hh" 562715Sstever@eecs.umich.edu#if THE_ISA == ALPHA_ISA 572715Sstever@eecs.umich.edu#include "arch/alpha/linux/process.hh" 582715Sstever@eecs.umich.edu#include "arch/alpha/tru64/process.hh" 592715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 602715Sstever@eecs.umich.edu#include "arch/sparc/linux/process.hh" 612715Sstever@eecs.umich.edu#include "arch/sparc/solaris/process.hh" 622715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 632715Sstever@eecs.umich.edu#include "arch/mips/linux/process.hh" 644157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 654166Sgblack@eecs.umich.edu#include "arch/x86/linux/process.hh" 662715Sstever@eecs.umich.edu#else 672715Sstever@eecs.umich.edu#error "THE_ISA not set" 682715Sstever@eecs.umich.edu#endif 692715Sstever@eecs.umich.edu 702715Sstever@eecs.umich.edu 712SN/Ausing namespace std; 722107SN/Ausing namespace TheISA; 732SN/A 742SN/A// 752SN/A// The purpose of this code is to fake the loader & syscall mechanism 762SN/A// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 772SN/A// mode when we do have an OS 782SN/A// 791858SN/A#if FULL_SYSTEM 80360SN/A#error "process.cc not compatible with FULL_SYSTEM" 812SN/A#endif 822SN/A 832SN/A// current number of allocated processes 842SN/Aint num_processes = 0; 852SN/A 865154Sgblack@eecs.umich.eduProcess::Process(ProcessParams * params) 875183Ssaidi@eecs.umich.edu : SimObject(params), system(params->system), checkpointRestored(false), 885154Sgblack@eecs.umich.edu max_stack_size(params->max_stack_size) 892SN/A{ 905154Sgblack@eecs.umich.edu string in = params->input; 915154Sgblack@eecs.umich.edu string out = params->output; 925154Sgblack@eecs.umich.edu 935154Sgblack@eecs.umich.edu // initialize file descriptors to default: same as simulator 945154Sgblack@eecs.umich.edu int stdin_fd, stdout_fd, stderr_fd; 955154Sgblack@eecs.umich.edu 965154Sgblack@eecs.umich.edu if (in == "stdin" || in == "cin") 975154Sgblack@eecs.umich.edu stdin_fd = STDIN_FILENO; 985154Sgblack@eecs.umich.edu else if (in == "None") 995154Sgblack@eecs.umich.edu stdin_fd = -1; 1005154Sgblack@eecs.umich.edu else 1015154Sgblack@eecs.umich.edu stdin_fd = Process::openInputFile(in); 1025154Sgblack@eecs.umich.edu 1035154Sgblack@eecs.umich.edu if (out == "stdout" || out == "cout") 1045154Sgblack@eecs.umich.edu stdout_fd = STDOUT_FILENO; 1055154Sgblack@eecs.umich.edu else if (out == "stderr" || out == "cerr") 1065154Sgblack@eecs.umich.edu stdout_fd = STDERR_FILENO; 1075154Sgblack@eecs.umich.edu else if (out == "None") 1085154Sgblack@eecs.umich.edu stdout_fd = -1; 1095154Sgblack@eecs.umich.edu else 1105154Sgblack@eecs.umich.edu stdout_fd = Process::openOutputFile(out); 1115154Sgblack@eecs.umich.edu 1125154Sgblack@eecs.umich.edu stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 1135154Sgblack@eecs.umich.edu 1144997Sgblack@eecs.umich.edu M5_pid = system->allocatePID(); 1152SN/A // initialize first 3 fds (stdin, stdout, stderr) 1165282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[STDIN_FILENO]; 1175282Srstrong@cs.ucsd.edu fdo->fd = stdin_fd; 1185282Srstrong@cs.ucsd.edu fdo->filename = in; 1195282Srstrong@cs.ucsd.edu fdo->flags = O_RDONLY; 1205282Srstrong@cs.ucsd.edu fdo->mode = -1; 1215282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 1225282Srstrong@cs.ucsd.edu 1235282Srstrong@cs.ucsd.edu fdo = &fd_map[STDOUT_FILENO]; 1245282Srstrong@cs.ucsd.edu fdo->fd = stdout_fd; 1255282Srstrong@cs.ucsd.edu fdo->filename = out; 1265282Srstrong@cs.ucsd.edu fdo->flags = O_WRONLY | O_CREAT | O_TRUNC; 1275282Srstrong@cs.ucsd.edu fdo->mode = 0774; 1285282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 1295282Srstrong@cs.ucsd.edu 1305282Srstrong@cs.ucsd.edu fdo = &fd_map[STDERR_FILENO]; 1315282Srstrong@cs.ucsd.edu fdo->fd = stderr_fd; 1325282Srstrong@cs.ucsd.edu fdo->filename = "STDERR"; 1335282Srstrong@cs.ucsd.edu fdo->flags = O_WRONLY; 1345282Srstrong@cs.ucsd.edu fdo->mode = -1; 1355282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 1365282Srstrong@cs.ucsd.edu 1372SN/A 1382SN/A // mark remaining fds as free 1392SN/A for (int i = 3; i <= MAX_FD; ++i) { 1405282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[i]; 1415282Srstrong@cs.ucsd.edu fdo->fd = -1; 1422SN/A } 1432SN/A 1441450SN/A mmap_start = mmap_end = 0; 1451514SN/A nxm_start = nxm_end = 0; 1465184Sgblack@eecs.umich.edu pTable = new PageTable(this); 1472SN/A // other parameters will be initialized when the program is loaded 1482SN/A} 1492SN/A 1502378SN/A 1512SN/Avoid 1522SN/AProcess::regStats() 1532SN/A{ 154729SN/A using namespace Stats; 1552SN/A 1562SN/A num_syscalls 1572SN/A .name(name() + ".PROG:num_syscalls") 1582SN/A .desc("Number of system calls") 1592SN/A ; 1602SN/A} 1612SN/A 1622SN/A// 1632SN/A// static helper functions 1642SN/A// 1652SN/Aint 1662SN/AProcess::openInputFile(const string &filename) 1672SN/A{ 1682SN/A int fd = open(filename.c_str(), O_RDONLY); 1692SN/A 1702SN/A if (fd == -1) { 1712SN/A perror(NULL); 1722SN/A cerr << "unable to open \"" << filename << "\" for reading\n"; 1732SN/A fatal("can't open input file"); 1742SN/A } 1752SN/A 1762SN/A return fd; 1772SN/A} 1782SN/A 1792SN/A 1802SN/Aint 1812SN/AProcess::openOutputFile(const string &filename) 1822SN/A{ 1832SN/A int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 1842SN/A 1852SN/A if (fd == -1) { 1862SN/A perror(NULL); 1872SN/A cerr << "unable to open \"" << filename << "\" for writing\n"; 1882SN/A fatal("can't open output file"); 1892SN/A } 1902SN/A 1912SN/A return fd; 1922SN/A} 1932SN/A 1942SN/A 195180SN/Aint 1962680Sktlim@umich.eduProcess::registerThreadContext(ThreadContext *tc) 1972SN/A{ 198180SN/A // add to list 1992680Sktlim@umich.edu int myIndex = threadContexts.size(); 2002680Sktlim@umich.edu threadContexts.push_back(tc); 201180SN/A 2025109Sgblack@eecs.umich.edu RemoteGDB *rgdb = new RemoteGDB(system, tc); 2035109Sgblack@eecs.umich.edu GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex); 2045109Sgblack@eecs.umich.edu gdbl->listen(); 2053971Sgblack@eecs.umich.edu //gdbl->accept(); 2063971Sgblack@eecs.umich.edu 2075109Sgblack@eecs.umich.edu remoteGDB.push_back(rgdb); 2083971Sgblack@eecs.umich.edu 2092378SN/A // return CPU number to caller 210180SN/A return myIndex; 2112SN/A} 2122SN/A 2131395SN/Avoid 2141395SN/AProcess::startup() 2151395SN/A{ 2162680Sktlim@umich.edu if (threadContexts.empty()) 2172378SN/A fatal("Process %s is not associated with any CPUs!\n", name()); 2182378SN/A 2192680Sktlim@umich.edu // first thread context for this process... initialize & enable 2202680Sktlim@umich.edu ThreadContext *tc = threadContexts[0]; 2211395SN/A 2221634SN/A // mark this context as active so it will start ticking. 2232680Sktlim@umich.edu tc->activate(0); 2242462SN/A 2252519SN/A Port *mem_port; 2262519SN/A mem_port = system->physmem->getPort("functional"); 2274434Ssaidi@eecs.umich.edu initVirtMem = new TranslatingPort("process init port", this, 2284434Ssaidi@eecs.umich.edu TranslatingPort::Always); 2292519SN/A mem_port->setPeer(initVirtMem); 2302519SN/A initVirtMem->setPeer(mem_port); 2311395SN/A} 2322SN/A 233180SN/Avoid 2342680Sktlim@umich.eduProcess::replaceThreadContext(ThreadContext *tc, int tcIndex) 235180SN/A{ 2362680Sktlim@umich.edu if (tcIndex >= threadContexts.size()) { 2372680Sktlim@umich.edu panic("replaceThreadContext: bad tcIndex, %d >= %d\n", 2382680Sktlim@umich.edu tcIndex, threadContexts.size()); 239180SN/A } 240180SN/A 2412680Sktlim@umich.edu threadContexts[tcIndex] = tc; 242180SN/A} 243180SN/A 2442SN/A// map simulator fd sim_fd to target fd tgt_fd 2452SN/Avoid 2462SN/AProcess::dup_fd(int sim_fd, int tgt_fd) 2472SN/A{ 2482SN/A if (tgt_fd < 0 || tgt_fd > MAX_FD) 2492SN/A panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 2502SN/A 2515282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[tgt_fd]; 2525282Srstrong@cs.ucsd.edu fdo->fd = sim_fd; 2532SN/A} 2542SN/A 2552SN/A 2562SN/A// generate new target fd for sim_fd 2572SN/Aint 2585282Srstrong@cs.ucsd.eduProcess::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe) 2592SN/A{ 2602SN/A // in case open() returns an error, don't allocate a new fd 2612SN/A if (sim_fd == -1) 2622SN/A return -1; 2632SN/A 2642SN/A // find first free target fd 2655282Srstrong@cs.ucsd.edu for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 2665282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[free_fd]; 2675282Srstrong@cs.ucsd.edu if (fdo->fd == -1) { 2685282Srstrong@cs.ucsd.edu fdo->fd = sim_fd; 2695282Srstrong@cs.ucsd.edu fdo->filename = filename; 2705282Srstrong@cs.ucsd.edu fdo->mode = mode; 2715282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 2725282Srstrong@cs.ucsd.edu fdo->flags = flags; 2735282Srstrong@cs.ucsd.edu fdo->isPipe = pipe; 2745282Srstrong@cs.ucsd.edu fdo->readPipeSource = 0; 2751970SN/A return free_fd; 2761970SN/A } 2772SN/A } 2782SN/A 2791970SN/A panic("Process::alloc_fd: out of file descriptors!"); 2801970SN/A} 2812SN/A 2821970SN/A 2831970SN/A// free target fd (e.g., after close) 2841970SN/Avoid 2851970SN/AProcess::free_fd(int tgt_fd) 2861970SN/A{ 2875282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[tgt_fd]; 2885282Srstrong@cs.ucsd.edu if (fdo->fd == -1) 2891970SN/A warn("Process::free_fd: request to free unused fd %d", tgt_fd); 2901970SN/A 2915282Srstrong@cs.ucsd.edu fdo->fd = -1; 2925282Srstrong@cs.ucsd.edu fdo->filename = "NULL"; 2935282Srstrong@cs.ucsd.edu fdo->mode = 0; 2945282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 2955282Srstrong@cs.ucsd.edu fdo->flags = 0; 2965282Srstrong@cs.ucsd.edu fdo->isPipe = false; 2975282Srstrong@cs.ucsd.edu fdo->readPipeSource = 0; 2982SN/A} 2992SN/A 3002SN/A 3012SN/A// look up simulator fd for given target fd 3022SN/Aint 3032SN/AProcess::sim_fd(int tgt_fd) 3042SN/A{ 3052SN/A if (tgt_fd > MAX_FD) 3062SN/A return -1; 3072SN/A 3085282Srstrong@cs.ucsd.edu return fd_map[tgt_fd].fd; 3092SN/A} 3102SN/A 3115282Srstrong@cs.ucsd.eduProcess::FdMap * 3125282Srstrong@cs.ucsd.eduProcess::sim_fd_obj(int tgt_fd) 3135282Srstrong@cs.ucsd.edu{ 3145282Srstrong@cs.ucsd.edu if (tgt_fd > MAX_FD) 3155282Srstrong@cs.ucsd.edu panic("sim_fd_obj called in fd out of range."); 3165282Srstrong@cs.ucsd.edu 3175282Srstrong@cs.ucsd.edu return &fd_map[tgt_fd]; 3185282Srstrong@cs.ucsd.edu} 3194434Ssaidi@eecs.umich.edubool 3204434Ssaidi@eecs.umich.eduProcess::checkAndAllocNextPage(Addr vaddr) 3214434Ssaidi@eecs.umich.edu{ 3224434Ssaidi@eecs.umich.edu // if this is an initial write we might not have 3234434Ssaidi@eecs.umich.edu if (vaddr >= stack_min && vaddr < stack_base) { 3244434Ssaidi@eecs.umich.edu pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 3254434Ssaidi@eecs.umich.edu return true; 3264434Ssaidi@eecs.umich.edu } 3274434Ssaidi@eecs.umich.edu 3284434Ssaidi@eecs.umich.edu // We've accessed the next page of the stack, so extend the stack 3294434Ssaidi@eecs.umich.edu // to cover it. 3305154Sgblack@eecs.umich.edu if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 3315154Sgblack@eecs.umich.edu while (vaddr < stack_min) { 3325154Sgblack@eecs.umich.edu stack_min -= TheISA::PageBytes; 3335154Sgblack@eecs.umich.edu if(stack_base - stack_min > max_stack_size) 3345154Sgblack@eecs.umich.edu fatal("Maximum stack size exceeded\n"); 3355154Sgblack@eecs.umich.edu if(stack_base - stack_min > 8*1024*1024) 3365154Sgblack@eecs.umich.edu fatal("Over max stack size for one thread\n"); 3375154Sgblack@eecs.umich.edu pTable->allocate(stack_min, TheISA::PageBytes); 3385154Sgblack@eecs.umich.edu warn("Increasing stack size by one page."); 3395154Sgblack@eecs.umich.edu }; 3404434Ssaidi@eecs.umich.edu return true; 3414434Ssaidi@eecs.umich.edu } 3424434Ssaidi@eecs.umich.edu return false; 3434434Ssaidi@eecs.umich.edu} 3444434Ssaidi@eecs.umich.edu 3455282Srstrong@cs.ucsd.edu // find all offsets for currently open files and save them 3465282Srstrong@cs.ucsd.eduvoid 3475282Srstrong@cs.ucsd.eduProcess::fix_file_offsets() { 3485282Srstrong@cs.ucsd.edu Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO]; 3495282Srstrong@cs.ucsd.edu Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO]; 3505282Srstrong@cs.ucsd.edu Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO]; 3515282Srstrong@cs.ucsd.edu string in = fdo_stdin->filename; 3525282Srstrong@cs.ucsd.edu string out = fdo_stdout->filename; 3535282Srstrong@cs.ucsd.edu 3545282Srstrong@cs.ucsd.edu // initialize file descriptors to default: same as simulator 3555282Srstrong@cs.ucsd.edu int stdin_fd, stdout_fd, stderr_fd; 3565282Srstrong@cs.ucsd.edu 3575282Srstrong@cs.ucsd.edu if (in == "stdin" || in == "cin") 3585282Srstrong@cs.ucsd.edu stdin_fd = STDIN_FILENO; 3595282Srstrong@cs.ucsd.edu else if (in == "None") 3605282Srstrong@cs.ucsd.edu stdin_fd = -1; 3615282Srstrong@cs.ucsd.edu else{ 3625282Srstrong@cs.ucsd.edu //OPEN standard in and seek to the right location 3635282Srstrong@cs.ucsd.edu stdin_fd = Process::openInputFile(in); 3645282Srstrong@cs.ucsd.edu if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0) 3655282Srstrong@cs.ucsd.edu panic("Unable to seek to correct location in file: %s", in); 3665282Srstrong@cs.ucsd.edu } 3675282Srstrong@cs.ucsd.edu 3685282Srstrong@cs.ucsd.edu if (out == "stdout" || out == "cout") 3695282Srstrong@cs.ucsd.edu stdout_fd = STDOUT_FILENO; 3705282Srstrong@cs.ucsd.edu else if (out == "stderr" || out == "cerr") 3715282Srstrong@cs.ucsd.edu stdout_fd = STDERR_FILENO; 3725282Srstrong@cs.ucsd.edu else if (out == "None") 3735282Srstrong@cs.ucsd.edu stdout_fd = -1; 3745282Srstrong@cs.ucsd.edu else{ 3755282Srstrong@cs.ucsd.edu stdout_fd = Process::openOutputFile(out); 3765282Srstrong@cs.ucsd.edu if (lseek(stdin_fd, fdo_stdout->fileOffset, SEEK_SET) < 0) 3775282Srstrong@cs.ucsd.edu panic("Unable to seek to correct in file: %s", out); 3785282Srstrong@cs.ucsd.edu } 3795282Srstrong@cs.ucsd.edu 3805282Srstrong@cs.ucsd.edu stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 3815282Srstrong@cs.ucsd.edu 3825282Srstrong@cs.ucsd.edu fdo_stdin->fd = stdin_fd; 3835282Srstrong@cs.ucsd.edu fdo_stdout->fd = stdout_fd; 3845282Srstrong@cs.ucsd.edu fdo_stderr->fd = stderr_fd; 3855282Srstrong@cs.ucsd.edu 3865282Srstrong@cs.ucsd.edu 3875282Srstrong@cs.ucsd.edu for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) { 3885282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[free_fd]; 3895282Srstrong@cs.ucsd.edu if (fdo->fd != -1) { 3905282Srstrong@cs.ucsd.edu if (fdo->isPipe){ 3915282Srstrong@cs.ucsd.edu if (fdo->filename == "PIPE-WRITE") 3925282Srstrong@cs.ucsd.edu continue; 3935282Srstrong@cs.ucsd.edu else { 3945282Srstrong@cs.ucsd.edu assert (fdo->filename == "PIPE-READ"); 3955282Srstrong@cs.ucsd.edu //create a new pipe 3965282Srstrong@cs.ucsd.edu int fds[2]; 3975282Srstrong@cs.ucsd.edu int pipe_retval = pipe(fds); 3985282Srstrong@cs.ucsd.edu 3995282Srstrong@cs.ucsd.edu if (pipe_retval < 0) { 4005282Srstrong@cs.ucsd.edu // error 4015282Srstrong@cs.ucsd.edu panic("Unable to create new pipe."); 4025282Srstrong@cs.ucsd.edu } 4035282Srstrong@cs.ucsd.edu fdo->fd = fds[0]; //set read pipe 4045282Srstrong@cs.ucsd.edu Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource]; 4055282Srstrong@cs.ucsd.edu if (fdo_write->filename != "PIPE-WRITE") 4065282Srstrong@cs.ucsd.edu panic ("Couldn't find write end of the pipe"); 4075282Srstrong@cs.ucsd.edu 4085282Srstrong@cs.ucsd.edu fdo_write->fd = fds[1];//set write pipe 4095282Srstrong@cs.ucsd.edu } 4105282Srstrong@cs.ucsd.edu } else { 4115282Srstrong@cs.ucsd.edu //Open file 4125282Srstrong@cs.ucsd.edu int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode); 4135282Srstrong@cs.ucsd.edu 4145282Srstrong@cs.ucsd.edu if (fd == -1) 4155282Srstrong@cs.ucsd.edu panic("Unable to open file: %s", fdo->filename); 4165282Srstrong@cs.ucsd.edu fdo->fd = fd; 4175282Srstrong@cs.ucsd.edu 4185282Srstrong@cs.ucsd.edu //Seek to correct location before checkpoint 4195282Srstrong@cs.ucsd.edu if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0) 4205282Srstrong@cs.ucsd.edu panic("Unable to seek to correct location in file: %s", fdo->filename); 4215282Srstrong@cs.ucsd.edu } 4225282Srstrong@cs.ucsd.edu } 4235282Srstrong@cs.ucsd.edu } 4245282Srstrong@cs.ucsd.edu} 4255282Srstrong@cs.ucsd.eduvoid 4265282Srstrong@cs.ucsd.eduProcess::find_file_offsets(){ 4275282Srstrong@cs.ucsd.edu for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 4285282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[free_fd]; 4295282Srstrong@cs.ucsd.edu if (fdo->fd != -1) { 4305282Srstrong@cs.ucsd.edu fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR); 4315282Srstrong@cs.ucsd.edu } else { 4325282Srstrong@cs.ucsd.edu fdo->filename = "NULL"; 4335282Srstrong@cs.ucsd.edu fdo->fileOffset = 0; 4345282Srstrong@cs.ucsd.edu } 4355282Srstrong@cs.ucsd.edu } 4365282Srstrong@cs.ucsd.edu} 4375282Srstrong@cs.ucsd.edu 4385282Srstrong@cs.ucsd.eduvoid 4395282Srstrong@cs.ucsd.eduProcess::setReadPipeSource(int read_pipe_fd, int source_fd){ 4405282Srstrong@cs.ucsd.edu Process::FdMap *fdo = &fd_map[read_pipe_fd]; 4415282Srstrong@cs.ucsd.edu fdo->readPipeSource = source_fd; 4425282Srstrong@cs.ucsd.edu} 4435282Srstrong@cs.ucsd.edu 4445282Srstrong@cs.ucsd.eduvoid 4455282Srstrong@cs.ucsd.eduProcess::FdMap::serialize(std::ostream &os) 4465282Srstrong@cs.ucsd.edu{ 4475282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(fd); 4485282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(isPipe); 4495282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(filename); 4505282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(flags); 4515282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(readPipeSource); 4525282Srstrong@cs.ucsd.edu SERIALIZE_SCALAR(fileOffset); 4535282Srstrong@cs.ucsd.edu} 4545282Srstrong@cs.ucsd.edu 4555282Srstrong@cs.ucsd.eduvoid 4565282Srstrong@cs.ucsd.eduProcess::FdMap::unserialize(Checkpoint *cp, const std::string §ion) 4575282Srstrong@cs.ucsd.edu{ 4585282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(fd); 4595282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(isPipe); 4605282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(filename); 4615282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(flags); 4625282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(readPipeSource); 4635282Srstrong@cs.ucsd.edu UNSERIALIZE_SCALAR(fileOffset); 4645282Srstrong@cs.ucsd.edu} 4655282Srstrong@cs.ucsd.edu 4663311Ssaidi@eecs.umich.eduvoid 4673311Ssaidi@eecs.umich.eduProcess::serialize(std::ostream &os) 4683311Ssaidi@eecs.umich.edu{ 4693311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(initialContextLoaded); 4703311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(brk_point); 4713311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_base); 4723311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_size); 4733311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(stack_min); 4743311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(next_thread_stack_base); 4753311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(mmap_start); 4763311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(mmap_end); 4773311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(nxm_start); 4783311Ssaidi@eecs.umich.edu SERIALIZE_SCALAR(nxm_end); 4795282Srstrong@cs.ucsd.edu find_file_offsets(); 4805282Srstrong@cs.ucsd.edu pTable->serialize(os); 4815282Srstrong@cs.ucsd.edu for (int x = 0; x <= MAX_FD; x++) { 4825282Srstrong@cs.ucsd.edu nameOut(os, csprintf("%s.FdMap%d", name(), x)); 4835282Srstrong@cs.ucsd.edu fd_map[x].serialize(os); 4845282Srstrong@cs.ucsd.edu } 4853311Ssaidi@eecs.umich.edu 4863311Ssaidi@eecs.umich.edu} 4873311Ssaidi@eecs.umich.edu 4883311Ssaidi@eecs.umich.eduvoid 4893311Ssaidi@eecs.umich.eduProcess::unserialize(Checkpoint *cp, const std::string §ion) 4903311Ssaidi@eecs.umich.edu{ 4913311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(initialContextLoaded); 4923311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(brk_point); 4933311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_base); 4943311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_size); 4953311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(stack_min); 4963311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(next_thread_stack_base); 4973311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(mmap_start); 4983311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(mmap_end); 4993311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(nxm_start); 5003311Ssaidi@eecs.umich.edu UNSERIALIZE_SCALAR(nxm_end); 5013311Ssaidi@eecs.umich.edu pTable->unserialize(cp, section); 5025282Srstrong@cs.ucsd.edu for (int x = 0; x <= MAX_FD; x++) { 5035282Srstrong@cs.ucsd.edu fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x)); 5045282Srstrong@cs.ucsd.edu } 5055282Srstrong@cs.ucsd.edu fix_file_offsets(); 5065183Ssaidi@eecs.umich.edu 5075183Ssaidi@eecs.umich.edu checkpointRestored = true; 5085183Ssaidi@eecs.umich.edu 5093311Ssaidi@eecs.umich.edu} 5102SN/A 5112SN/A 5122SN/A//////////////////////////////////////////////////////////////////////// 5132SN/A// 5142SN/A// LiveProcess member definitions 5152SN/A// 5162SN/A//////////////////////////////////////////////////////////////////////// 5172SN/A 51812SN/A 5195154Sgblack@eecs.umich.eduLiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile) 5205154Sgblack@eecs.umich.edu : Process(params), objFile(_objFile), 5215154Sgblack@eecs.umich.edu argv(params->cmd), envp(params->env), cwd(params->cwd) 5222SN/A{ 5235154Sgblack@eecs.umich.edu __uid = params->uid; 5245154Sgblack@eecs.umich.edu __euid = params->euid; 5255154Sgblack@eecs.umich.edu __gid = params->gid; 5265154Sgblack@eecs.umich.edu __egid = params->egid; 5275154Sgblack@eecs.umich.edu __pid = params->pid; 5285154Sgblack@eecs.umich.edu __ppid = params->ppid; 5293114Sgblack@eecs.umich.edu 5305154Sgblack@eecs.umich.edu prog_fname = params->cmd[0]; 53112SN/A 5321158SN/A // load up symbols, if any... these may be used for debugging or 5331158SN/A // profiling. 5341158SN/A if (!debugSymbolTable) { 5351158SN/A debugSymbolTable = new SymbolTable(); 5361158SN/A if (!objFile->loadGlobalSymbols(debugSymbolTable) || 5371158SN/A !objFile->loadLocalSymbols(debugSymbolTable)) { 5381158SN/A // didn't load any symbols 5391158SN/A delete debugSymbolTable; 5401158SN/A debugSymbolTable = NULL; 5411158SN/A } 5421158SN/A } 5432378SN/A} 5441158SN/A 5452378SN/Avoid 5462474SN/ALiveProcess::argsInit(int intSize, int pageSize) 5472378SN/A{ 5482378SN/A Process::startup(); 54912SN/A 5502378SN/A // load object file into target memory 5512378SN/A objFile->loadSections(initVirtMem); 55212SN/A 55312SN/A // Calculate how much space we need for arg & env arrays. 5542474SN/A int argv_array_size = intSize * (argv.size() + 1); 5552474SN/A int envp_array_size = intSize * (envp.size() + 1); 55612SN/A int arg_data_size = 0; 55712SN/A for (int i = 0; i < argv.size(); ++i) { 55812SN/A arg_data_size += argv[i].size() + 1; 55912SN/A } 56012SN/A int env_data_size = 0; 56112SN/A for (int i = 0; i < envp.size(); ++i) { 56212SN/A env_data_size += envp[i].size() + 1; 56312SN/A } 56412SN/A 56512SN/A int space_needed = 56612SN/A argv_array_size + envp_array_size + arg_data_size + env_data_size; 5673005Sstever@eecs.umich.edu if (space_needed < 32*1024) 5683005Sstever@eecs.umich.edu space_needed = 32*1024; 56912SN/A 57012SN/A // set bottom of stack 57112SN/A stack_min = stack_base - space_needed; 57212SN/A // align it 5732800Ssaidi@eecs.umich.edu stack_min = roundDown(stack_min, pageSize); 57412SN/A stack_size = stack_base - stack_min; 5752378SN/A // map memory 5762800Ssaidi@eecs.umich.edu pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 57712SN/A 57812SN/A // map out initial stack contents 5792523SN/A Addr argv_array_base = stack_min + intSize; // room for argc 58012SN/A Addr envp_array_base = argv_array_base + argv_array_size; 58112SN/A Addr arg_data_base = envp_array_base + envp_array_size; 58212SN/A Addr env_data_base = arg_data_base + arg_data_size; 58312SN/A 58412SN/A // write contents to stack 58512SN/A uint64_t argc = argv.size(); 5862474SN/A if (intSize == 8) 5872474SN/A argc = htog((uint64_t)argc); 5882474SN/A else if (intSize == 4) 5892474SN/A argc = htog((uint32_t)argc); 5902474SN/A else 5912474SN/A panic("Unknown int size"); 5922474SN/A 5932474SN/A initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 59412SN/A 5952378SN/A copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 5962378SN/A copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 59712SN/A 5984772Sgblack@eecs.umich.edu assert(NumArgumentRegs >= 2); 5994772Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(ArgumentReg[0], argc); 6004772Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base); 6012680Sktlim@umich.edu threadContexts[0]->setIntReg(StackPointerReg, stack_min); 6022451SN/A 6032451SN/A Addr prog_entry = objFile->entryPoint(); 6042680Sktlim@umich.edu threadContexts[0]->setPC(prog_entry); 6052680Sktlim@umich.edu threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 6062817Sksewell@umich.edu 6072817Sksewell@umich.edu#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 6082680Sktlim@umich.edu threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 6092817Sksewell@umich.edu#endif 6102378SN/A 6112378SN/A num_processes++; 6122SN/A} 6132SN/A 6142093SN/Avoid 6152680Sktlim@umich.eduLiveProcess::syscall(int64_t callnum, ThreadContext *tc) 6162093SN/A{ 6172093SN/A num_syscalls++; 6182093SN/A 6192093SN/A SyscallDesc *desc = getDesc(callnum); 6202093SN/A if (desc == NULL) 6212093SN/A fatal("Syscall %d out of range", callnum); 6222093SN/A 6232680Sktlim@umich.edu desc->doSyscall(callnum, this, tc); 6242093SN/A} 6252SN/A 6262715Sstever@eecs.umich.eduLiveProcess * 6275154Sgblack@eecs.umich.eduLiveProcess::create(LiveProcessParams * params) 6282715Sstever@eecs.umich.edu{ 6292715Sstever@eecs.umich.edu LiveProcess *process = NULL; 6302715Sstever@eecs.umich.edu 6315154Sgblack@eecs.umich.edu string executable = 6325154Sgblack@eecs.umich.edu params->executable == "" ? params->cmd[0] : params->executable; 6332715Sstever@eecs.umich.edu ObjectFile *objFile = createObjectFile(executable); 6342715Sstever@eecs.umich.edu if (objFile == NULL) { 6352715Sstever@eecs.umich.edu fatal("Can't load object file %s", executable); 6362715Sstever@eecs.umich.edu } 6372715Sstever@eecs.umich.edu 6383917Ssaidi@eecs.umich.edu if (objFile->isDynamic()) 6393917Ssaidi@eecs.umich.edu fatal("Object file is a dynamic executable however only static " 6405070Ssaidi@eecs.umich.edu "executables are supported!\n Please recompile your " 6413917Ssaidi@eecs.umich.edu "executable as a static binary and try again.\n"); 6423917Ssaidi@eecs.umich.edu 6435089Sgblack@eecs.umich.edu#if THE_ISA == ALPHA_ISA 6445070Ssaidi@eecs.umich.edu if (objFile->hasTLS()) 6455089Sgblack@eecs.umich.edu fatal("Object file has a TLS section and single threaded TLS is not\n" 6465089Sgblack@eecs.umich.edu " currently supported for Alpha! Please recompile your " 6475089Sgblack@eecs.umich.edu "executable with \n a non-TLS toolchain.\n"); 6485070Ssaidi@eecs.umich.edu 6492715Sstever@eecs.umich.edu if (objFile->getArch() != ObjectFile::Alpha) 6502715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (Alpha)."); 6512715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6522715Sstever@eecs.umich.edu case ObjectFile::Tru64: 6535154Sgblack@eecs.umich.edu process = new AlphaTru64Process(params, objFile); 6542715Sstever@eecs.umich.edu break; 6552715Sstever@eecs.umich.edu 6562715Sstever@eecs.umich.edu case ObjectFile::Linux: 6575154Sgblack@eecs.umich.edu process = new AlphaLinuxProcess(params, objFile); 6582715Sstever@eecs.umich.edu break; 6592715Sstever@eecs.umich.edu 6602715Sstever@eecs.umich.edu default: 6612715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6622715Sstever@eecs.umich.edu } 6632715Sstever@eecs.umich.edu#elif THE_ISA == SPARC_ISA 6644111Sgblack@eecs.umich.edu if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32) 6652715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (SPARC)."); 6662715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6672715Sstever@eecs.umich.edu case ObjectFile::Linux: 6684111Sgblack@eecs.umich.edu if (objFile->getArch() == ObjectFile::SPARC64) { 6695154Sgblack@eecs.umich.edu process = new Sparc64LinuxProcess(params, objFile); 6704111Sgblack@eecs.umich.edu } else { 6715154Sgblack@eecs.umich.edu process = new Sparc32LinuxProcess(params, objFile); 6724111Sgblack@eecs.umich.edu } 6732715Sstever@eecs.umich.edu break; 6742715Sstever@eecs.umich.edu 6752715Sstever@eecs.umich.edu 6762715Sstever@eecs.umich.edu case ObjectFile::Solaris: 6775154Sgblack@eecs.umich.edu process = new SparcSolarisProcess(params, objFile); 6782715Sstever@eecs.umich.edu break; 6792715Sstever@eecs.umich.edu default: 6802715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6812715Sstever@eecs.umich.edu } 6824157Sgblack@eecs.umich.edu#elif THE_ISA == X86_ISA 6834157Sgblack@eecs.umich.edu if (objFile->getArch() != ObjectFile::X86) 6844166Sgblack@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (x86)."); 6854157Sgblack@eecs.umich.edu switch (objFile->getOpSys()) { 6864166Sgblack@eecs.umich.edu case ObjectFile::Linux: 6875154Sgblack@eecs.umich.edu process = new X86LinuxProcess(params, objFile); 6884166Sgblack@eecs.umich.edu break; 6894157Sgblack@eecs.umich.edu default: 6904157Sgblack@eecs.umich.edu fatal("Unknown/unsupported operating system."); 6914157Sgblack@eecs.umich.edu } 6922715Sstever@eecs.umich.edu#elif THE_ISA == MIPS_ISA 6932715Sstever@eecs.umich.edu if (objFile->getArch() != ObjectFile::Mips) 6942715Sstever@eecs.umich.edu fatal("Object file architecture does not match compiled ISA (MIPS)."); 6952715Sstever@eecs.umich.edu switch (objFile->getOpSys()) { 6962715Sstever@eecs.umich.edu case ObjectFile::Linux: 6975154Sgblack@eecs.umich.edu process = new MipsLinuxProcess(params, objFile); 6982715Sstever@eecs.umich.edu break; 6992715Sstever@eecs.umich.edu 7002715Sstever@eecs.umich.edu default: 7012715Sstever@eecs.umich.edu fatal("Unknown/unsupported operating system."); 7022715Sstever@eecs.umich.edu } 7032715Sstever@eecs.umich.edu#else 7042715Sstever@eecs.umich.edu#error "THE_ISA not set" 7052715Sstever@eecs.umich.edu#endif 7062715Sstever@eecs.umich.edu 7072715Sstever@eecs.umich.edu 7082715Sstever@eecs.umich.edu if (process == NULL) 7092715Sstever@eecs.umich.edu fatal("Unknown error creating process object."); 7102715Sstever@eecs.umich.edu return process; 7112715Sstever@eecs.umich.edu} 7122715Sstever@eecs.umich.edu 7134762Snate@binkert.orgLiveProcess * 7144762Snate@binkert.orgLiveProcessParams::create() 7152715Sstever@eecs.umich.edu{ 7165154Sgblack@eecs.umich.edu return LiveProcess::create(this); 7172715Sstever@eecs.umich.edu} 718