process.cc revision 5955
11689SN/A/* 21689SN/A * Copyright (c) 2001-2005 The Regents of The University of Michigan 31689SN/A * All rights reserved. 41689SN/A * 51689SN/A * Redistribution and use in source and binary forms, with or without 61689SN/A * modification, are permitted provided that the following conditions are 71689SN/A * met: redistributions of source code must retain the above copyright 81689SN/A * notice, this list of conditions and the following disclaimer; 91689SN/A * redistributions in binary form must reproduce the above copyright 101689SN/A * notice, this list of conditions and the following disclaimer in the 111689SN/A * documentation and/or other materials provided with the distribution; 121689SN/A * neither the name of the copyright holders nor the names of its 131689SN/A * contributors may be used to endorse or promote products derived from 141689SN/A * this software without specific prior written permission. 151689SN/A * 161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 292756Sksewell@umich.edu * Steve Reinhardt 301689SN/A * Ali Saidi 311689SN/A */ 322325SN/A 332325SN/A#include <unistd.h> 341060SN/A#include <fcntl.h> 351060SN/A#include <string> 361060SN/A 372292SN/A#include "arch/remote_gdb.hh" 382292SN/A#include "base/intmath.hh" 391681SN/A#include "base/loader/object_file.hh" 401060SN/A#include "base/loader/symtab.hh" 412980Sgblack@eecs.umich.edu#include "base/statistics.hh" 421060SN/A#include "config/full_system.hh" 431060SN/A#include "cpu/thread_context.hh" 441858SN/A#include "mem/page_table.hh" 452325SN/A#include "mem/physical.hh" 461717SN/A#include "mem/translating_port.hh" 472683Sktlim@umich.edu#include "params/Process.hh" 481717SN/A#include "params/LiveProcess.hh" 491717SN/A#include "sim/debug.hh" 502292SN/A#include "sim/process.hh" 512292SN/A#include "sim/process_impl.hh" 522817Sksewell@umich.edu#include "sim/stats.hh" 531060SN/A#include "sim/syscall_emul.hh" 541060SN/A#include "sim/system.hh" 552316SN/A 562316SN/A#include "arch/isa_specific.hh" 572680Sktlim@umich.edu#if THE_ISA == ALPHA_ISA 582817Sksewell@umich.edu#include "arch/alpha/linux/process.hh" 592817Sksewell@umich.edu#include "arch/alpha/tru64/process.hh" 602843Sktlim@umich.edu#elif THE_ISA == SPARC_ISA 612843Sktlim@umich.edu#include "arch/sparc/linux/process.hh" 622669Sktlim@umich.edu#include "arch/sparc/solaris/process.hh" 631060SN/A#elif THE_ISA == MIPS_ISA 641060SN/A#include "arch/mips/linux/process.hh" 652733Sktlim@umich.edu#elif THE_ISA == ARM_ISA 661060SN/A#include "arch/arm/linux/process.hh" 671060SN/A#elif THE_ISA == X86_ISA 681060SN/A#include "arch/x86/linux/process.hh" 691464SN/A#else 701061SN/A#error "THE_ISA not set" 712733Sktlim@umich.edu#endif 722292SN/A 732292SN/A 742632Sstever@eecs.umich.eduusing namespace std; 752817Sksewell@umich.eduusing namespace TheISA; 762817Sksewell@umich.edu 772817Sksewell@umich.edu// 782817Sksewell@umich.edu// The purpose of this code is to fake the loader & syscall mechanism 792669Sktlim@umich.edu// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 801681SN/A// mode when we do have an OS 811685SN/A// 821681SN/A#if FULL_SYSTEM 831060SN/A#error "process.cc not compatible with FULL_SYSTEM" 841060SN/A#endif 852348SN/A 862348SN/A// current number of allocated processes 872348SN/Aint num_processes = 0; 882348SN/A 892348SN/Atemplate<class IntType> 901060SN/AAuxVector<IntType>::AuxVector(IntType type, IntType val) 912733Sktlim@umich.edu{ 921060SN/A a_type = TheISA::htog(type); 931060SN/A a_val = TheISA::htog(val); 942325SN/A} 951060SN/A 961060SN/Atemplate class AuxVector<uint32_t>; 971061SN/Atemplate class AuxVector<uint64_t>; 984329Sktlim@umich.edu 991060SN/AProcess::Process(ProcessParams * params) 1002292SN/A : SimObject(params), system(params->system), checkpointRestored(false), 1012292SN/A max_stack_size(params->max_stack_size) 1022292SN/A{ 1032292SN/A string in = params->input; 1042817Sksewell@umich.edu string out = params->output; 1052829Sksewell@umich.edu string err = params->errout; 1061060SN/A 1071060SN/A // initialize file descriptors to default: same as simulator 1081060SN/A int stdin_fd, stdout_fd, stderr_fd; 1091060SN/A 1101060SN/A if (in == "stdin" || in == "cin") 1112307SN/A stdin_fd = STDIN_FILENO; 1122307SN/A else if (in == "None") 1131060SN/A stdin_fd = -1; 1141060SN/A else 1153781Sgblack@eecs.umich.edu stdin_fd = Process::openInputFile(in); 1163781Sgblack@eecs.umich.edu 1173781Sgblack@eecs.umich.edu if (out == "stdout" || out == "cout") 1183781Sgblack@eecs.umich.edu stdout_fd = STDOUT_FILENO; 1193781Sgblack@eecs.umich.edu else if (out == "stderr" || out == "cerr") 1202292SN/A stdout_fd = STDERR_FILENO; 1211060SN/A else if (out == "None") 1221060SN/A stdout_fd = -1; 1232829Sksewell@umich.edu else 1242829Sksewell@umich.edu stdout_fd = Process::openOutputFile(out); 1252829Sksewell@umich.edu 1261060SN/A if (err == "stdout" || err == "cout") 1271060SN/A stderr_fd = STDOUT_FILENO; 1281060SN/A else if (err == "stderr" || err == "cerr") 1291060SN/A stderr_fd = STDERR_FILENO; 1302292SN/A else if (err == "None") 1311755SN/A stderr_fd = -1; 1321060SN/A else if (err == out) 1331060SN/A stderr_fd = stdout_fd; 1342292SN/A else 1351755SN/A stderr_fd = Process::openOutputFile(err); 1362292SN/A 1372292SN/A M5_pid = system->allocatePID(); 1381060SN/A // initialize first 3 fds (stdin, stdout, stderr) 1392292SN/A Process::FdMap *fdo = &fd_map[STDIN_FILENO]; 1401060SN/A fdo->fd = stdin_fd; 1411060SN/A fdo->filename = in; 1421060SN/A fdo->flags = O_RDONLY; 1432292SN/A fdo->mode = -1; 1441060SN/A fdo->fileOffset = 0; 1451060SN/A 1462292SN/A fdo = &fd_map[STDOUT_FILENO]; 1471060SN/A fdo->fd = stdout_fd; 1481060SN/A fdo->filename = out; 1491060SN/A fdo->flags = O_WRONLY | O_CREAT | O_TRUNC; 1504030Sktlim@umich.edu fdo->mode = 0774; 1511060SN/A fdo->fileOffset = 0; 1524030Sktlim@umich.edu 1531060SN/A fdo = &fd_map[STDERR_FILENO]; 1541060SN/A fdo->fd = stderr_fd; 1552292SN/A fdo->filename = err; 1561060SN/A fdo->flags = O_WRONLY; 1571060SN/A fdo->mode = -1; 1581060SN/A fdo->fileOffset = 0; 1591060SN/A 1601060SN/A 1611060SN/A // mark remaining fds as free 1622829Sksewell@umich.edu for (int i = 3; i <= MAX_FD; ++i) { 1632829Sksewell@umich.edu Process::FdMap *fdo = &fd_map[i]; 1642829Sksewell@umich.edu fdo->fd = -1; 1652829Sksewell@umich.edu } 1662829Sksewell@umich.edu 1672829Sksewell@umich.edu mmap_start = mmap_end = 0; 1682829Sksewell@umich.edu nxm_start = nxm_end = 0; 1692829Sksewell@umich.edu pTable = new PageTable(this); 1702829Sksewell@umich.edu // other parameters will be initialized when the program is loaded 1712829Sksewell@umich.edu} 1722829Sksewell@umich.edu 1732829Sksewell@umich.edu 1742829Sksewell@umich.eduvoid 1752829Sksewell@umich.eduProcess::regStats() 1762829Sksewell@umich.edu{ 1772829Sksewell@umich.edu using namespace Stats; 1782829Sksewell@umich.edu 1792829Sksewell@umich.edu num_syscalls 1802829Sksewell@umich.edu .name(name() + ".PROG:num_syscalls") 1812829Sksewell@umich.edu .desc("Number of system calls") 1822829Sksewell@umich.edu ; 1832829Sksewell@umich.edu} 1842829Sksewell@umich.edu 1852829Sksewell@umich.edu// 1862829Sksewell@umich.edu// static helper functions 1872829Sksewell@umich.edu// 1882829Sksewell@umich.eduint 1892829Sksewell@umich.eduProcess::openInputFile(const string &filename) 1904030Sktlim@umich.edu{ 1914030Sktlim@umich.edu int fd = open(filename.c_str(), O_RDONLY); 1922829Sksewell@umich.edu 1934030Sktlim@umich.edu if (fd == -1) { 1944030Sktlim@umich.edu perror(NULL); 1952829Sksewell@umich.edu cerr << "unable to open \"" << filename << "\" for reading\n"; 1962829Sksewell@umich.edu fatal("can't open input file"); 1972829Sksewell@umich.edu } 1982829Sksewell@umich.edu 1992829Sksewell@umich.edu return fd; 2002829Sksewell@umich.edu} 2012829Sksewell@umich.edu 2022829Sksewell@umich.edu 2032829Sksewell@umich.eduint 2042829Sksewell@umich.eduProcess::openOutputFile(const string &filename) 2052829Sksewell@umich.edu{ 2062829Sksewell@umich.edu int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664); 2072875Sksewell@umich.edu 2082875Sksewell@umich.edu if (fd == -1) { 2092875Sksewell@umich.edu perror(NULL); 2103221Sktlim@umich.edu cerr << "unable to open \"" << filename << "\" for writing\n"; 2112875Sksewell@umich.edu fatal("can't open output file"); 2122875Sksewell@umich.edu } 2133221Sktlim@umich.edu 2143221Sktlim@umich.edu return fd; 2153221Sktlim@umich.edu} 2162875Sksewell@umich.edu 2172875Sksewell@umich.eduThreadContext * 2182875Sksewell@umich.eduProcess::findFreeContext() 2192875Sksewell@umich.edu{ 2202875Sksewell@umich.edu int size = contextIds.size(); 2212875Sksewell@umich.edu ThreadContext *tc; 2222875Sksewell@umich.edu for (int i = 0; i < size; ++i) { 2232875Sksewell@umich.edu tc = system->getThreadContext(contextIds[i]); 2242875Sksewell@umich.edu if (tc->status() == ThreadContext::Unallocated) { 2252875Sksewell@umich.edu // inactive context, free to use 2262875Sksewell@umich.edu return tc; 2272875Sksewell@umich.edu } 2282875Sksewell@umich.edu } 2293221Sktlim@umich.edu return NULL; 2303221Sktlim@umich.edu} 2313221Sktlim@umich.edu 2322875Sksewell@umich.eduvoid 2332875Sksewell@umich.eduProcess::startup() 2342875Sksewell@umich.edu{ 2352875Sksewell@umich.edu if (contextIds.empty()) 2362875Sksewell@umich.edu fatal("Process %s is not associated with any HW contexts!\n", name()); 2373221Sktlim@umich.edu 2382875Sksewell@umich.edu // first thread context for this process... initialize & enable 2392875Sksewell@umich.edu ThreadContext *tc = system->getThreadContext(contextIds[0]); 2402875Sksewell@umich.edu 2414030Sktlim@umich.edu // mark this context as active so it will start ticking. 2424030Sktlim@umich.edu tc->activate(0); 2432875Sksewell@umich.edu 2444030Sktlim@umich.edu Port *mem_port; 2454030Sktlim@umich.edu mem_port = system->physmem->getPort("functional"); 2462875Sksewell@umich.edu initVirtMem = new TranslatingPort("process init port", this, 2472875Sksewell@umich.edu TranslatingPort::Always); 2482875Sksewell@umich.edu mem_port->setPeer(initVirtMem); 2492875Sksewell@umich.edu initVirtMem->setPeer(mem_port); 2502875Sksewell@umich.edu} 2512875Sksewell@umich.edu 2522875Sksewell@umich.edu// map simulator fd sim_fd to target fd tgt_fd 2532875Sksewell@umich.eduvoid 2542875Sksewell@umich.eduProcess::dup_fd(int sim_fd, int tgt_fd) 2552875Sksewell@umich.edu{ 2562875Sksewell@umich.edu if (tgt_fd < 0 || tgt_fd > MAX_FD) 2572875Sksewell@umich.edu panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 2581060SN/A 2592292SN/A Process::FdMap *fdo = &fd_map[tgt_fd]; 2604329Sktlim@umich.edu fdo->fd = sim_fd; 2612292SN/A} 2621755SN/A 2631060SN/A 2642292SN/A// generate new target fd for sim_fd 2651684SN/Aint 2661684SN/AProcess::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe) 2672871Sktlim@umich.edu{ 2682871Sktlim@umich.edu // in case open() returns an error, don't allocate a new fd 2692871Sktlim@umich.edu if (sim_fd == -1) 2702292SN/A return -1; 2712292SN/A 2722292SN/A // find first free target fd 2731684SN/A for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 2741684SN/A Process::FdMap *fdo = &fd_map[free_fd]; 2752292SN/A if (fdo->fd == -1) { 2761060SN/A fdo->fd = sim_fd; 2771060SN/A fdo->filename = filename; 2782834Sksewell@umich.edu fdo->mode = mode; 2792834Sksewell@umich.edu fdo->fileOffset = 0; 2802834Sksewell@umich.edu fdo->flags = flags; 2812834Sksewell@umich.edu fdo->isPipe = pipe; 2822829Sksewell@umich.edu fdo->readPipeSource = 0; 2832875Sksewell@umich.edu return free_fd; 2842875Sksewell@umich.edu } 2852875Sksewell@umich.edu } 2862875Sksewell@umich.edu 2872829Sksewell@umich.edu panic("Process::alloc_fd: out of file descriptors!"); 2882292SN/A} 2892292SN/A 2901060SN/A 2912292SN/A// free target fd (e.g., after close) 2922292SN/Avoid 2932292SN/AProcess::free_fd(int tgt_fd) 2942292SN/A{ 2952292SN/A Process::FdMap *fdo = &fd_map[tgt_fd]; 2962292SN/A if (fdo->fd == -1) 2972292SN/A warn("Process::free_fd: request to free unused fd %d", tgt_fd); 2982292SN/A 2992292SN/A fdo->fd = -1; 3002292SN/A fdo->filename = "NULL"; 3012292SN/A fdo->mode = 0; 3022292SN/A fdo->fileOffset = 0; 3032292SN/A fdo->flags = 0; 3042292SN/A fdo->isPipe = false; 3052292SN/A fdo->readPipeSource = 0; 3062292SN/A} 3072292SN/A 3082292SN/A 3092292SN/A// look up simulator fd for given target fd 3102292SN/Aint 3112292SN/AProcess::sim_fd(int tgt_fd) 3123221Sktlim@umich.edu{ 3132292SN/A if (tgt_fd > MAX_FD) 3143221Sktlim@umich.edu return -1; 3152292SN/A 3162292SN/A return fd_map[tgt_fd].fd; 3172292SN/A} 3182292SN/A 3192292SN/AProcess::FdMap * 3202292SN/AProcess::sim_fd_obj(int tgt_fd) 3212292SN/A{ 3222292SN/A if (tgt_fd > MAX_FD) 3232292SN/A panic("sim_fd_obj called in fd out of range."); 3242292SN/A 3252292SN/A return &fd_map[tgt_fd]; 3262292SN/A} 3272292SN/Abool 3282292SN/AProcess::checkAndAllocNextPage(Addr vaddr) 3292292SN/A{ 3302864Sktlim@umich.edu // if this is an initial write we might not have 3312864Sktlim@umich.edu if (vaddr >= stack_min && vaddr < stack_base) { 3322864Sktlim@umich.edu pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 3332864Sktlim@umich.edu return true; 3342864Sktlim@umich.edu } 3352864Sktlim@umich.edu 3362864Sktlim@umich.edu // We've accessed the next page of the stack, so extend the stack 3372292SN/A // to cover it. 3382292SN/A if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 3392292SN/A while (vaddr < stack_min) { 3402292SN/A stack_min -= TheISA::PageBytes; 3412292SN/A if(stack_base - stack_min > max_stack_size) 3422325SN/A fatal("Maximum stack size exceeded\n"); 3432292SN/A if(stack_base - stack_min > 8*1024*1024) 3442843Sktlim@umich.edu fatal("Over max stack size for one thread\n"); 3452843Sktlim@umich.edu pTable->allocate(stack_min, TheISA::PageBytes); 3462905Sktlim@umich.edu inform("Increasing stack size by one page."); 3472843Sktlim@umich.edu }; 3482843Sktlim@umich.edu return true; 3492843Sktlim@umich.edu } 3502292SN/A return false; 3512348SN/A} 3522843Sktlim@umich.edu 3532843Sktlim@umich.edu // find all offsets for currently open files and save them 3542843Sktlim@umich.eduvoid 3552843Sktlim@umich.eduProcess::fix_file_offsets() { 3562316SN/A Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO]; 3572348SN/A Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO]; 3582843Sktlim@umich.edu Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO]; 3591060SN/A string in = fdo_stdin->filename; 3601060SN/A string out = fdo_stdout->filename; 3612316SN/A string err = fdo_stderr->filename; 3622316SN/A 3631060SN/A // initialize file descriptors to default: same as simulator 3641858SN/A int stdin_fd, stdout_fd, stderr_fd; 3654192Sktlim@umich.edu 3664192Sktlim@umich.edu if (in == "stdin" || in == "cin") 3674192Sktlim@umich.edu stdin_fd = STDIN_FILENO; 3684192Sktlim@umich.edu else if (in == "None") 3691060SN/A stdin_fd = -1; 3701060SN/A else{ 3711060SN/A //OPEN standard in and seek to the right location 3721060SN/A stdin_fd = Process::openInputFile(in); 3731060SN/A if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0) 3741060SN/A panic("Unable to seek to correct location in file: %s", in); 3751060SN/A } 3762292SN/A 3772292SN/A if (out == "stdout" || out == "cout") 3781060SN/A stdout_fd = STDOUT_FILENO; 3791060SN/A else if (out == "stderr" || out == "cerr") 3802292SN/A stdout_fd = STDERR_FILENO; 3812292SN/A else if (out == "None") 3821060SN/A stdout_fd = -1; 3832292SN/A else{ 3842292SN/A stdout_fd = Process::openOutputFile(out); 3852683Sktlim@umich.edu if (lseek(stdout_fd, fdo_stdout->fileOffset, SEEK_SET) < 0) 3861060SN/A panic("Unable to seek to correct location in file: %s", out); 3872292SN/A } 3882292SN/A 3892683Sktlim@umich.edu if (err == "stdout" || err == "cout") 3901060SN/A stderr_fd = STDOUT_FILENO; 3911060SN/A else if (err == "stderr" || err == "cerr") 3921060SN/A stderr_fd = STDERR_FILENO; 3932348SN/A else if (err == "None") 3941060SN/A stderr_fd = -1; 3951060SN/A else if (err == out) 3963781Sgblack@eecs.umich.edu stderr_fd = stdout_fd; 3971060SN/A else { 3983781Sgblack@eecs.umich.edu stderr_fd = Process::openOutputFile(err); 3991060SN/A if (lseek(stderr_fd, fdo_stderr->fileOffset, SEEK_SET) < 0) 4003781Sgblack@eecs.umich.edu panic("Unable to seek to correct location in file: %s", err); 4012455SN/A } 4023781Sgblack@eecs.umich.edu 4031060SN/A fdo_stdin->fd = stdin_fd; 4041060SN/A fdo_stdout->fd = stdout_fd; 4051060SN/A fdo_stderr->fd = stderr_fd; 4063781Sgblack@eecs.umich.edu 4071060SN/A 4083781Sgblack@eecs.umich.edu for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) { 4091060SN/A Process::FdMap *fdo = &fd_map[free_fd]; 4103781Sgblack@eecs.umich.edu if (fdo->fd != -1) { 4112455SN/A if (fdo->isPipe){ 4123781Sgblack@eecs.umich.edu if (fdo->filename == "PIPE-WRITE") 4131060SN/A continue; 4142292SN/A else { 4151060SN/A assert (fdo->filename == "PIPE-READ"); 4162292SN/A //create a new pipe 4171060SN/A int fds[2]; 4182292SN/A int pipe_retval = pipe(fds); 4192292SN/A 4202292SN/A if (pipe_retval < 0) { 4212292SN/A // error 4222348SN/A panic("Unable to create new pipe."); 4232348SN/A } 4242348SN/A fdo->fd = fds[0]; //set read pipe 4252348SN/A Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource]; 4262348SN/A if (fdo_write->filename != "PIPE-WRITE") 4272292SN/A panic ("Couldn't find write end of the pipe"); 4282292SN/A 4292292SN/A fdo_write->fd = fds[1];//set write pipe 4302292SN/A } 4312292SN/A } else { 4322292SN/A //Open file 4332292SN/A int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode); 4342292SN/A 4352348SN/A if (fd == -1) 4362292SN/A panic("Unable to open file: %s", fdo->filename); 4372292SN/A fdo->fd = fd; 4382348SN/A 4392348SN/A //Seek to correct location before checkpoint 4402292SN/A if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0) 4412348SN/A panic("Unable to seek to correct location in file: %s", fdo->filename); 4422292SN/A } 4432292SN/A } 4442348SN/A } 4452348SN/A} 4461060SN/Avoid 4472756Sksewell@umich.eduProcess::find_file_offsets(){ 4482756Sksewell@umich.edu for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 4492756Sksewell@umich.edu Process::FdMap *fdo = &fd_map[free_fd]; 4502756Sksewell@umich.edu if (fdo->fd != -1) { 4512756Sksewell@umich.edu fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR); 4522756Sksewell@umich.edu } else { 4531060SN/A fdo->filename = "NULL"; 4541060SN/A fdo->fileOffset = 0; 4551060SN/A } 4562292SN/A } 4571060SN/A} 4581060SN/A 4592292SN/Avoid 4601060SN/AProcess::setReadPipeSource(int read_pipe_fd, int source_fd){ 4612292SN/A Process::FdMap *fdo = &fd_map[read_pipe_fd]; 4622292SN/A fdo->readPipeSource = source_fd; 4631060SN/A} 4642325SN/A 4652325SN/Avoid 4661060SN/AProcess::FdMap::serialize(std::ostream &os) 4671061SN/A{ 4681060SN/A SERIALIZE_SCALAR(fd); 4692935Sksewell@umich.edu SERIALIZE_SCALAR(isPipe); 4702935Sksewell@umich.edu SERIALIZE_SCALAR(filename); 4714632Sgblack@eecs.umich.edu SERIALIZE_SCALAR(flags); 4721060SN/A SERIALIZE_SCALAR(readPipeSource); 4731062SN/A SERIALIZE_SCALAR(fileOffset); 4742292SN/A} 4752292SN/A 4762348SN/Avoid 4772292SN/AProcess::FdMap::unserialize(Checkpoint *cp, const std::string §ion) 4782292SN/A{ 4792348SN/A UNSERIALIZE_SCALAR(fd); 4802292SN/A UNSERIALIZE_SCALAR(isPipe); 4811062SN/A UNSERIALIZE_SCALAR(filename); 4822348SN/A UNSERIALIZE_SCALAR(flags); 4831060SN/A UNSERIALIZE_SCALAR(readPipeSource); 4841060SN/A UNSERIALIZE_SCALAR(fileOffset); 4851060SN/A} 4861060SN/A 4872292SN/Avoid 4881060SN/AProcess::serialize(std::ostream &os) 4892292SN/A{ 4902292SN/A SERIALIZE_SCALAR(initialContextLoaded); 4912292SN/A SERIALIZE_SCALAR(brk_point); 4922292SN/A SERIALIZE_SCALAR(stack_base); 4932292SN/A SERIALIZE_SCALAR(stack_size); 4942325SN/A SERIALIZE_SCALAR(stack_min); 4952348SN/A SERIALIZE_SCALAR(next_thread_stack_base); 4962348SN/A SERIALIZE_SCALAR(mmap_start); 4972348SN/A SERIALIZE_SCALAR(mmap_end); 4982292SN/A SERIALIZE_SCALAR(nxm_start); 4992325SN/A SERIALIZE_SCALAR(nxm_end); 5002292SN/A find_file_offsets(); 5012325SN/A pTable->serialize(os); 5022325SN/A for (int x = 0; x <= MAX_FD; x++) { 5032292SN/A nameOut(os, csprintf("%s.FdMap%d", name(), x)); 5042292SN/A fd_map[x].serialize(os); 5052292SN/A } 5061060SN/A 5071060SN/A} 5081060SN/A 5091060SN/Avoid 5101060SN/AProcess::unserialize(Checkpoint *cp, const std::string §ion) 5111060SN/A{ 5121060SN/A UNSERIALIZE_SCALAR(initialContextLoaded); 5131060SN/A UNSERIALIZE_SCALAR(brk_point); 5141060SN/A UNSERIALIZE_SCALAR(stack_base); 5151060SN/A UNSERIALIZE_SCALAR(stack_size); 5161060SN/A UNSERIALIZE_SCALAR(stack_min); 5171060SN/A UNSERIALIZE_SCALAR(next_thread_stack_base); 5181060SN/A UNSERIALIZE_SCALAR(mmap_start); 5191060SN/A UNSERIALIZE_SCALAR(mmap_end); 5201060SN/A UNSERIALIZE_SCALAR(nxm_start); 5211060SN/A UNSERIALIZE_SCALAR(nxm_end); 5221060SN/A pTable->unserialize(cp, section); 5231060SN/A for (int x = 0; x <= MAX_FD; x++) { 5241060SN/A fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x)); 5251060SN/A } 5261060SN/A fix_file_offsets(); 5271060SN/A 5281060SN/A checkpointRestored = true; 5292292SN/A 5302292SN/A} 5312292SN/A 5322292SN/A 5331060SN/A//////////////////////////////////////////////////////////////////////// 5341060SN/A// 5351060SN/A// LiveProcess member definitions 5361060SN/A// 5372292SN/A//////////////////////////////////////////////////////////////////////// 5382292SN/A 5392292SN/A 5402292SN/ALiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile) 5412292SN/A : Process(params), objFile(_objFile), 5422292SN/A argv(params->cmd), envp(params->env), cwd(params->cwd) 5431060SN/A{ 5442292SN/A __uid = params->uid; 5452292SN/A __euid = params->euid; 5462292SN/A __gid = params->gid; 5472292SN/A __egid = params->egid; 5482292SN/A __pid = params->pid; 5492292SN/A __ppid = params->ppid; 5502292SN/A 5512292SN/A prog_fname = params->cmd[0]; 5522292SN/A 5532292SN/A // load up symbols, if any... these may be used for debugging or 5542292SN/A // profiling. 5552292SN/A if (!debugSymbolTable) { 5561060SN/A debugSymbolTable = new SymbolTable(); 5571060SN/A if (!objFile->loadGlobalSymbols(debugSymbolTable) || 5581060SN/A !objFile->loadLocalSymbols(debugSymbolTable)) { 5591061SN/A // didn't load any symbols 5601060SN/A delete debugSymbolTable; 5611061SN/A debugSymbolTable = NULL; 5621060SN/A } 5631061SN/A } 5641060SN/A} 5651061SN/A 5661060SN/Avoid 5671061SN/ALiveProcess::argsInit(int intSize, int pageSize) 5681060SN/A{ 5691060SN/A Process::startup(); 5701060SN/A 5711060SN/A // load object file into target memory 5721060SN/A objFile->loadSections(initVirtMem); 5731060SN/A 5741060SN/A // Calculate how much space we need for arg & env arrays. 5751060SN/A int argv_array_size = intSize * (argv.size() + 1); 5761060SN/A int envp_array_size = intSize * (envp.size() + 1); 5771060SN/A int arg_data_size = 0; 5781060SN/A for (int i = 0; i < argv.size(); ++i) { 5791060SN/A arg_data_size += argv[i].size() + 1; 5801060SN/A } 5811060SN/A int env_data_size = 0; 5821060SN/A for (int i = 0; i < envp.size(); ++i) { 5831060SN/A env_data_size += envp[i].size() + 1; 5842348SN/A } 5852348SN/A 5862348SN/A int space_needed = 5872348SN/A argv_array_size + envp_array_size + arg_data_size + env_data_size; 5882348SN/A if (space_needed < 32*1024) 5892325SN/A space_needed = 32*1024; 5901060SN/A 5912348SN/A // set bottom of stack 5922348SN/A stack_min = stack_base - space_needed; 5932325SN/A // align it 5942292SN/A stack_min = roundDown(stack_min, pageSize); 5952348SN/A stack_size = stack_base - stack_min; 5962325SN/A // map memory 5972325SN/A pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 5982292SN/A 5992348SN/A // map out initial stack contents 6002325SN/A Addr argv_array_base = stack_min + intSize; // room for argc 6012325SN/A Addr envp_array_base = argv_array_base + argv_array_size; 6022292SN/A Addr arg_data_base = envp_array_base + envp_array_size; 6032292SN/A Addr env_data_base = arg_data_base + arg_data_size; 6042292SN/A 6052260SN/A // write contents to stack 6062292SN/A uint64_t argc = argv.size(); 6072292SN/A if (intSize == 8) 6082292SN/A argc = htog((uint64_t)argc); 6092292SN/A else if (intSize == 4) 6102680Sktlim@umich.edu argc = htog((uint32_t)argc); 6112680Sktlim@umich.edu else 6121681SN/A panic("Unknown int size"); 6132680Sktlim@umich.edu 6142190SN/A initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 6152190SN/A 6162292SN/A copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 6173093Sksewell@umich.edu copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 6181060SN/A 6192348SN/A assert(NumArgumentRegs >= 2); 6202348SN/A 6212348SN/A ThreadContext *tc = system->getThreadContext(contextIds[0]); 6222348SN/A 6232316SN/A tc->setIntReg(ArgumentReg[0], argc); 6242316SN/A tc->setIntReg(ArgumentReg[1], argv_array_base); 6251858SN/A tc->setIntReg(StackPointerReg, stack_min); 6262292SN/A 6271060SN/A Addr prog_entry = objFile->entryPoint(); 6281060SN/A tc->setPC(prog_entry); 6292292SN/A tc->setNextPC(prog_entry + sizeof(MachInst)); 6301060SN/A 6312292SN/A#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 6321060SN/A tc->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 6332843Sktlim@umich.edu#endif 6342843Sktlim@umich.edu 6352843Sktlim@umich.edu num_processes++; 6362843Sktlim@umich.edu} 6372843Sktlim@umich.edu 6382316SN/Avoid 6392348SN/ALiveProcess::syscall(int64_t callnum, ThreadContext *tc) 6402292SN/A{ 6412260SN/A num_syscalls++; 6422292SN/A 6431060SN/A SyscallDesc *desc = getDesc(callnum); 6441060SN/A if (desc == NULL) 6452292SN/A fatal("Syscall %d out of range", callnum); 6462292SN/A 6471060SN/A desc->doSyscall(callnum, this, tc); 6482292SN/A} 6492292SN/A 6502292SN/ALiveProcess * 6512292SN/ALiveProcess::create(LiveProcessParams * params) 6522292SN/A{ 6532292SN/A LiveProcess *process = NULL; 6542829Sksewell@umich.edu 6552829Sksewell@umich.edu string executable = 6562829Sksewell@umich.edu params->executable == "" ? params->cmd[0] : params->executable; 6572292SN/A ObjectFile *objFile = createObjectFile(executable); 6582292SN/A if (objFile == NULL) { 6592292SN/A fatal("Can't load object file %s", executable); 6602292SN/A } 6612292SN/A 6622292SN/A if (objFile->isDynamic()) 6632292SN/A fatal("Object file is a dynamic executable however only static " 6642292SN/A "executables are supported!\n Please recompile your " 6652292SN/A "executable as a static binary and try again.\n"); 6662292SN/A 6672292SN/A#if THE_ISA == ALPHA_ISA 6682292SN/A if (objFile->getArch() != ObjectFile::Alpha) 6692292SN/A fatal("Object file architecture does not match compiled ISA (Alpha)."); 6702292SN/A 6712292SN/A switch (objFile->getOpSys()) { 6722292SN/A case ObjectFile::Tru64: 6732292SN/A process = new AlphaTru64Process(params, objFile); 6742292SN/A break; 6752292SN/A 6762292SN/A case ObjectFile::UnknownOpSys: 6772292SN/A warn("Unknown operating system; assuming Linux."); 6782292SN/A // fall through 6792292SN/A case ObjectFile::Linux: 6802292SN/A process = new AlphaLinuxProcess(params, objFile); 6812292SN/A break; 6821060SN/A 6831060SN/A default: 6842325SN/A fatal("Unknown/unsupported operating system."); 685 } 686#elif THE_ISA == SPARC_ISA 687 if (objFile->getArch() != ObjectFile::SPARC64 && 688 objFile->getArch() != ObjectFile::SPARC32) 689 fatal("Object file architecture does not match compiled ISA (SPARC)."); 690 switch (objFile->getOpSys()) { 691 case ObjectFile::UnknownOpSys: 692 warn("Unknown operating system; assuming Linux."); 693 // fall through 694 case ObjectFile::Linux: 695 if (objFile->getArch() == ObjectFile::SPARC64) { 696 process = new Sparc64LinuxProcess(params, objFile); 697 } else { 698 process = new Sparc32LinuxProcess(params, objFile); 699 } 700 break; 701 702 703 case ObjectFile::Solaris: 704 process = new SparcSolarisProcess(params, objFile); 705 break; 706 707 default: 708 fatal("Unknown/unsupported operating system."); 709 } 710#elif THE_ISA == X86_ISA 711 if (objFile->getArch() != ObjectFile::X86_64 && 712 objFile->getArch() != ObjectFile::I386) 713 fatal("Object file architecture does not match compiled ISA (x86)."); 714 switch (objFile->getOpSys()) { 715 case ObjectFile::UnknownOpSys: 716 warn("Unknown operating system; assuming Linux."); 717 // fall through 718 case ObjectFile::Linux: 719 if (objFile->getArch() == ObjectFile::X86_64) { 720 process = new X86_64LinuxProcess(params, objFile); 721 } else { 722 process = new I386LinuxProcess(params, objFile); 723 } 724 break; 725 726 default: 727 fatal("Unknown/unsupported operating system."); 728 } 729#elif THE_ISA == MIPS_ISA 730 if (objFile->getArch() != ObjectFile::Mips) 731 fatal("Object file architecture does not match compiled ISA (MIPS)."); 732 switch (objFile->getOpSys()) { 733 case ObjectFile::UnknownOpSys: 734 warn("Unknown operating system; assuming Linux."); 735 // fall through 736 case ObjectFile::Linux: 737 process = new MipsLinuxProcess(params, objFile); 738 break; 739 740 default: 741 fatal("Unknown/unsupported operating system."); 742 } 743#elif THE_ISA == ARM_ISA 744 if (objFile->getArch() != ObjectFile::Arm) 745 fatal("Object file architecture does not match compiled ISA (ARM)."); 746 switch (objFile->getOpSys()) { 747 case ObjectFile::UnknownOpSys: 748 warn("Unknown operating system; assuming Linux."); 749 // fall through 750 case ObjectFile::Linux: 751 process = new ArmLinuxProcess(params, objFile); 752 break; 753 754 default: 755 fatal("Unknown/unsupported operating system."); 756 } 757#else 758#error "THE_ISA not set" 759#endif 760 761 762 if (process == NULL) 763 fatal("Unknown error creating process object."); 764 return process; 765} 766 767LiveProcess * 768LiveProcessParams::create() 769{ 770 return LiveProcess::create(this); 771} 772