process.cc revision 2091
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 292665Ssaidi@eecs.umich.edu#include <unistd.h> 302665Ssaidi@eecs.umich.edu#include <fcntl.h> 312SN/A 322SN/A#include <cstdio> 332SN/A#include <string> 342SN/A 352SN/A#include "base/intmath.hh" 362SN/A#include "base/loader/object_file.hh" 372SN/A#include "base/loader/symtab.hh" 383971Sgblack@eecs.umich.edu#include "base/statistics.hh" 3956SN/A#include "config/full_system.hh" 4056SN/A#include "cpu/exec_context.hh" 411158SN/A#include "cpu/smt.hh" 42146SN/A#include "encumbered/cpu/full/thread.hh" 431858SN/A#include "encumbered/eio/eio.hh" 442680Sktlim@umich.edu#include "encumbered/mem/functional/main.hh" 452378SN/A#include "sim/builder.hh" 462522SN/A#include "sim/fake_syscall.hh" 472401SN/A#include "sim/process.hh" 484762Snate@binkert.org#include "sim/stats.hh" 49360SN/A 504434Ssaidi@eecs.umich.edu#ifdef TARGET_ALPHA 51695SN/A#include "arch/alpha/alpha_tru64_process.hh" 522093SN/A#include "arch/alpha/alpha_linux_process.hh" 532378SN/A#endif 542SN/A 552715Sstever@eecs.umich.eduusing namespace std; 562715Sstever@eecs.umich.edu 572715Sstever@eecs.umich.edu// 582715Sstever@eecs.umich.edu// The purpose of this code is to fake the loader & syscall mechanism 592715Sstever@eecs.umich.edu// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 602715Sstever@eecs.umich.edu// mode when we do have an OS 612715Sstever@eecs.umich.edu// 622715Sstever@eecs.umich.edu#if FULL_SYSTEM 632715Sstever@eecs.umich.edu#error "process.cc not compatible with FULL_SYSTEM" 644157Sgblack@eecs.umich.edu#endif 654166Sgblack@eecs.umich.edu 662715Sstever@eecs.umich.edu// current number of allocated processes 672715Sstever@eecs.umich.eduint num_processes = 0; 682715Sstever@eecs.umich.edu 692715Sstever@eecs.umich.eduProcess::Process(const string &nm, 702715Sstever@eecs.umich.edu int stdin_fd, // initial I/O descriptors 712SN/A int stdout_fd, 722107SN/A int stderr_fd) 732SN/A : SimObject(nm) 742SN/A{ 752SN/A // allocate memory space 762SN/A memory = new MainMemory(nm + ".MainMem"); 772SN/A 782SN/A // allocate initial register file 791858SN/A init_regs = new RegFile; 80360SN/A memset(init_regs, 0, sizeof(RegFile)); 812SN/A 822SN/A // initialize first 3 fds (stdin, stdout, stderr) 832SN/A fd_map[STDIN_FILENO] = stdin_fd; 842SN/A fd_map[STDOUT_FILENO] = stdout_fd; 852SN/A fd_map[STDERR_FILENO] = stderr_fd; 861450SN/A 872378SN/A // mark remaining fds as free 882SN/A for (int i = 3; i <= MAX_FD; ++i) { 892SN/A fd_map[i] = -1; 902SN/A } 915034Smilesck@eecs.umich.edu 922SN/A mmap_start = mmap_end = 0; 934997Sgblack@eecs.umich.edu nxm_start = nxm_end = 0; 942SN/A // other parameters will be initialized when the program is loaded 952SN/A} 962SN/A 972SN/Avoid 982SN/AProcess::regStats() 992SN/A{ 1002SN/A using namespace Stats; 1012SN/A 1022SN/A num_syscalls 1032SN/A .name(name() + ".PROG:num_syscalls") 1041450SN/A .desc("Number of system calls") 1051514SN/A ; 1062378SN/A} 1072SN/A 1082SN/A// 1092SN/A// static helper functions 1102378SN/A// 1112SN/Aint 1122SN/AProcess::openInputFile(const string &filename) 1132SN/A{ 114729SN/A int fd = open(filename.c_str(), O_RDONLY); 1152SN/A 1162SN/A if (fd == -1) { 1172SN/A perror(NULL); 1182SN/A cerr << "unable to open \"" << filename << "\" for reading\n"; 1192SN/A fatal("can't open input file"); 1202SN/A } 1212SN/A 1222SN/A return fd; 1232SN/A} 1242SN/A 1252SN/A 1262SN/Aint 1272SN/AProcess::openOutputFile(const string &filename) 1282SN/A{ 1292SN/A int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 1302SN/A 1312SN/A if (fd == -1) { 1322SN/A perror(NULL); 1332SN/A cerr << "unable to open \"" << filename << "\" for writing\n"; 1342SN/A fatal("can't open output file"); 1352SN/A } 1362SN/A 1372SN/A return fd; 1382SN/A} 1392SN/A 1402SN/A 1412SN/Aint 1422SN/AProcess::registerExecContext(ExecContext *xc) 1432SN/A{ 1442SN/A // add to list 1452SN/A int myIndex = execContexts.size(); 1462SN/A execContexts.push_back(xc); 1472SN/A 1482SN/A if (myIndex == 0) { 1492SN/A // copy process's initial regs struct 1502SN/A xc->regs = *init_regs; 1512SN/A } 1522SN/A 1532SN/A // return CPU number to caller and increment available CPU count 1542SN/A return myIndex; 155180SN/A} 1562680Sktlim@umich.edu 1572SN/Avoid 158180SN/AProcess::startup() 1592680Sktlim@umich.edu{ 1602680Sktlim@umich.edu if (execContexts.empty()) 161180SN/A return; 1625109Sgblack@eecs.umich.edu 1635109Sgblack@eecs.umich.edu // first exec context for this process... initialize & enable 1645109Sgblack@eecs.umich.edu ExecContext *xc = execContexts[0]; 1653971Sgblack@eecs.umich.edu 1663971Sgblack@eecs.umich.edu // mark this context as active so it will start ticking. 1675109Sgblack@eecs.umich.edu xc->activate(0); 1683971Sgblack@eecs.umich.edu} 1692378SN/A 170180SN/Avoid 1712SN/AProcess::replaceExecContext(ExecContext *xc, int xcIndex) 1722SN/A{ 1731395SN/A if (xcIndex >= execContexts.size()) { 1741395SN/A panic("replaceExecContext: bad xcIndex, %d >= %d\n", 1751395SN/A xcIndex, execContexts.size()); 1762680Sktlim@umich.edu } 1772378SN/A 1782378SN/A execContexts[xcIndex] = xc; 1792680Sktlim@umich.edu} 1802680Sktlim@umich.edu 1811395SN/A// map simulator fd sim_fd to target fd tgt_fd 1821634SN/Avoid 1832680Sktlim@umich.eduProcess::dup_fd(int sim_fd, int tgt_fd) 1842462SN/A{ 1852519SN/A if (tgt_fd < 0 || tgt_fd > MAX_FD) 1862519SN/A panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 1874434Ssaidi@eecs.umich.edu 1884434Ssaidi@eecs.umich.edu fd_map[tgt_fd] = sim_fd; 1892519SN/A} 1902519SN/A 1911395SN/A 1922SN/A// generate new target fd for sim_fd 193180SN/Aint 1942680Sktlim@umich.eduProcess::alloc_fd(int sim_fd) 195180SN/A{ 1962680Sktlim@umich.edu // in case open() returns an error, don't allocate a new fd 1972680Sktlim@umich.edu if (sim_fd == -1) 1982680Sktlim@umich.edu return -1; 199180SN/A 200180SN/A // find first free target fd 2012680Sktlim@umich.edu for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 202180SN/A if (fd_map[free_fd] == -1) { 203180SN/A fd_map[free_fd] = sim_fd; 2042SN/A return free_fd; 2052SN/A } 2062SN/A } 2072SN/A 2082SN/A panic("Process::alloc_fd: out of file descriptors!"); 2092SN/A} 2102SN/A 2112SN/A 2122SN/A// free target fd (e.g., after close) 2132SN/Avoid 2142SN/AProcess::free_fd(int tgt_fd) 2152SN/A{ 2162SN/A if (fd_map[tgt_fd] == -1) 2171970SN/A warn("Process::free_fd: request to free unused fd %d", tgt_fd); 2182SN/A 2192SN/A fd_map[tgt_fd] = -1; 2202SN/A} 2212SN/A 2222SN/A 2232SN/A// look up simulator fd for given target fd 2241970SN/Aint 2251970SN/AProcess::sim_fd(int tgt_fd) 2261970SN/A{ 2271970SN/A if (tgt_fd > MAX_FD) 2281970SN/A return -1; 2292SN/A 2302SN/A return fd_map[tgt_fd]; 2311970SN/A} 2321970SN/A 2332SN/A 2341970SN/A 2351970SN/A// 2361970SN/A// need to declare these here since there is no concrete Process type 2371970SN/A// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 2381970SN/A// which is where these get declared for concrete types). 2391970SN/A// 2401970SN/ADEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 2411970SN/A 2421970SN/A 2432SN/A//////////////////////////////////////////////////////////////////////// 2442SN/A// 2452SN/A// LiveProcess member definitions 2462SN/A// 2472SN/A//////////////////////////////////////////////////////////////////////// 2482SN/A 2492SN/A 2502SN/Astatic void 2512SN/AcopyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 2522SN/A FunctionalMemory *memory) 2532SN/A{ 2542SN/A Addr data_ptr_swap; 2552SN/A for (int i = 0; i < strings.size(); ++i) { 2564434Ssaidi@eecs.umich.edu data_ptr_swap = htog(data_ptr); 2574434Ssaidi@eecs.umich.edu memory->access(Write, array_ptr, &data_ptr_swap, sizeof(Addr)); 2584434Ssaidi@eecs.umich.edu memory->writeString(data_ptr, strings[i].c_str()); 2594434Ssaidi@eecs.umich.edu array_ptr += sizeof(Addr); 2604434Ssaidi@eecs.umich.edu data_ptr += strings[i].size() + 1; 2614434Ssaidi@eecs.umich.edu } 2624434Ssaidi@eecs.umich.edu // add NULL terminator 2634434Ssaidi@eecs.umich.edu data_ptr = 0; 2644434Ssaidi@eecs.umich.edu memory->access(Write, array_ptr, &data_ptr, sizeof(Addr)); 2654434Ssaidi@eecs.umich.edu} 2664434Ssaidi@eecs.umich.edu 2674434Ssaidi@eecs.umich.eduLiveProcess::LiveProcess(const string &nm, ObjectFile *objFile, 2684434Ssaidi@eecs.umich.edu int stdin_fd, int stdout_fd, int stderr_fd, 2694434Ssaidi@eecs.umich.edu vector<string> &argv, vector<string> &envp) 2704434Ssaidi@eecs.umich.edu : Process(nm, stdin_fd, stdout_fd, stderr_fd) 2714434Ssaidi@eecs.umich.edu{ 2724434Ssaidi@eecs.umich.edu prog_fname = argv[0]; 2734434Ssaidi@eecs.umich.edu 2744434Ssaidi@eecs.umich.edu prog_entry = objFile->entryPoint(); 2754434Ssaidi@eecs.umich.edu text_base = objFile->textBase(); 2764434Ssaidi@eecs.umich.edu text_size = objFile->textSize(); 2774434Ssaidi@eecs.umich.edu data_base = objFile->dataBase(); 2784434Ssaidi@eecs.umich.edu data_size = objFile->dataSize() + objFile->bssSize(); 2793311Ssaidi@eecs.umich.edu brk_point = roundUp(data_base + data_size, VMPageSize); 2803311Ssaidi@eecs.umich.edu 2813311Ssaidi@eecs.umich.edu // load object file into target memory 2823311Ssaidi@eecs.umich.edu objFile->loadSections(memory); 2833311Ssaidi@eecs.umich.edu 2843311Ssaidi@eecs.umich.edu // load up symbols, if any... these may be used for debugging or 2853311Ssaidi@eecs.umich.edu // profiling. 2863311Ssaidi@eecs.umich.edu if (!debugSymbolTable) { 2873311Ssaidi@eecs.umich.edu debugSymbolTable = new SymbolTable(); 2883311Ssaidi@eecs.umich.edu if (!objFile->loadGlobalSymbols(debugSymbolTable) || 2893311Ssaidi@eecs.umich.edu !objFile->loadLocalSymbols(debugSymbolTable)) { 2903311Ssaidi@eecs.umich.edu // didn't load any symbols 2913311Ssaidi@eecs.umich.edu delete debugSymbolTable; 2923311Ssaidi@eecs.umich.edu debugSymbolTable = NULL; 2933311Ssaidi@eecs.umich.edu } 2943311Ssaidi@eecs.umich.edu } 2953311Ssaidi@eecs.umich.edu 2963311Ssaidi@eecs.umich.edu // Set up stack. On Alpha, stack goes below text section. This 2973311Ssaidi@eecs.umich.edu // code should get moved to some architecture-specific spot. 2983311Ssaidi@eecs.umich.edu stack_base = text_base - (409600+4096); 2993311Ssaidi@eecs.umich.edu 3003311Ssaidi@eecs.umich.edu // Set up region for mmaps. Tru64 seems to start just above 0 and 3013311Ssaidi@eecs.umich.edu // grow up from there. 3023311Ssaidi@eecs.umich.edu mmap_start = mmap_end = 0x10000; 3033311Ssaidi@eecs.umich.edu 3043311Ssaidi@eecs.umich.edu // Set pointer for next thread stack. Reserve 8M for main stack. 3053311Ssaidi@eecs.umich.edu next_thread_stack_base = stack_base - (8 * 1024 * 1024); 3063311Ssaidi@eecs.umich.edu 3073311Ssaidi@eecs.umich.edu // Calculate how much space we need for arg & env arrays. 3083311Ssaidi@eecs.umich.edu int argv_array_size = sizeof(Addr) * (argv.size() + 1); 3093311Ssaidi@eecs.umich.edu int envp_array_size = sizeof(Addr) * (envp.size() + 1); 3103311Ssaidi@eecs.umich.edu int arg_data_size = 0; 3113311Ssaidi@eecs.umich.edu for (int i = 0; i < argv.size(); ++i) { 3123311Ssaidi@eecs.umich.edu arg_data_size += argv[i].size() + 1; 3133311Ssaidi@eecs.umich.edu } 3142SN/A int env_data_size = 0; 3152SN/A for (int i = 0; i < envp.size(); ++i) { 3162SN/A env_data_size += envp[i].size() + 1; 3172SN/A } 3182SN/A 3192SN/A int space_needed = 3202SN/A argv_array_size + envp_array_size + arg_data_size + env_data_size; 3212SN/A // for SimpleScalar compatibility 32212SN/A if (space_needed < 16384) 3232378SN/A space_needed = 16384; 3242378SN/A 3252SN/A // set bottom of stack 3263114Sgblack@eecs.umich.edu stack_min = stack_base - space_needed; 3273669Sbinkertn@umich.edu // align it 3283114Sgblack@eecs.umich.edu stack_min &= ~7; 3293114Sgblack@eecs.umich.edu stack_size = stack_base - stack_min; 3303114Sgblack@eecs.umich.edu 3312378SN/A // map out initial stack contents 3323669Sbinkertn@umich.edu Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 3332SN/A Addr envp_array_base = argv_array_base + argv_array_size; 3343114Sgblack@eecs.umich.edu Addr arg_data_base = envp_array_base + envp_array_size; 3353114Sgblack@eecs.umich.edu Addr env_data_base = arg_data_base + arg_data_size; 3363114Sgblack@eecs.umich.edu 3373114Sgblack@eecs.umich.edu // write contents to stack 3383114Sgblack@eecs.umich.edu uint64_t argc = argv.size(); 3393114Sgblack@eecs.umich.edu argc = htog(argc); 3403114Sgblack@eecs.umich.edu memory->access(Write, stack_min, &argc, sizeof(uint64_t)); 34112SN/A 34212SN/A copyStringArray(argv, argv_array_base, arg_data_base, memory); 3431158SN/A copyStringArray(envp, envp_array_base, env_data_base, memory); 3441158SN/A 3451158SN/A init_regs->intRegFile[ArgumentReg0] = argc; 3461158SN/A init_regs->intRegFile[ArgumentReg1] = argv_array_base; 3471158SN/A init_regs->intRegFile[StackPointerReg] = stack_min; 3481158SN/A init_regs->intRegFile[GlobalPointerReg] = objFile->globalPointer(); 3491158SN/A init_regs->pc = prog_entry; 3501158SN/A init_regs->npc = prog_entry + sizeof(MachInst); 3511158SN/A} 3521158SN/A 3531158SN/A 3542378SN/ALiveProcess * 3551158SN/ALiveProcess::create(const string &nm, 3562378SN/A int stdin_fd, int stdout_fd, int stderr_fd, 3572474SN/A string executable, 3582378SN/A vector<string> &argv, vector<string> &envp) 3592378SN/A{ 36012SN/A LiveProcess *process = NULL; 3612378SN/A ObjectFile *objFile = createObjectFile(executable); 3622378SN/A if (objFile == NULL) { 36312SN/A fatal("Can't load object file %s", executable); 36412SN/A } 3652474SN/A 3662474SN/A // check object type & set up syscall emulation pointer 36712SN/A if (objFile->getArch() == ObjectFile::Alpha) { 36812SN/A switch (objFile->getOpSys()) { 36912SN/A case ObjectFile::Tru64: 37012SN/A process = new AlphaTru64Process(nm, objFile, 37112SN/A stdin_fd, stdout_fd, stderr_fd, 37212SN/A argv, envp); 37312SN/A break; 37412SN/A 37512SN/A case ObjectFile::Linux: 37612SN/A process = new AlphaLinuxProcess(nm, objFile, 37712SN/A stdin_fd, stdout_fd, stderr_fd, 3783005Sstever@eecs.umich.edu argv, envp); 3793005Sstever@eecs.umich.edu break; 38012SN/A 38112SN/A default: 38212SN/A fatal("Unknown/unsupported operating system."); 38312SN/A } 3842800Ssaidi@eecs.umich.edu } else { 38512SN/A fatal("Unknown object file architecture."); 3862378SN/A } 3872800Ssaidi@eecs.umich.edu 38812SN/A delete objFile; 38912SN/A 3902523SN/A if (process == NULL) 39112SN/A fatal("Unknown error creating process object."); 39212SN/A 39312SN/A return process; 39412SN/A} 39512SN/A 39612SN/A 3972474SN/ABEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 3982474SN/A 3992474SN/A VectorParam<string> cmd; 4002474SN/A Param<string> executable; 4012474SN/A Param<string> input; 4022474SN/A Param<string> output; 4032474SN/A VectorParam<string> env; 4042474SN/A 40512SN/AEND_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 4062378SN/A 4072378SN/A 40812SN/ABEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 4094772Sgblack@eecs.umich.edu 4104772Sgblack@eecs.umich.edu INIT_PARAM(cmd, "command line (executable plus arguments)"), 4114772Sgblack@eecs.umich.edu INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 4122680Sktlim@umich.edu INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 4132451SN/A INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 4142451SN/A INIT_PARAM(env, "environment settings") 4152680Sktlim@umich.edu 4162680Sktlim@umich.eduEND_INIT_SIM_OBJECT_PARAMS(LiveProcess) 4172817Sksewell@umich.edu 4182817Sksewell@umich.edu 4192680Sktlim@umich.eduCREATE_SIM_OBJECT(LiveProcess) 4202817Sksewell@umich.edu{ 4212378SN/A string in = input; 4222378SN/A string out = output; 4232SN/A 4242SN/A // initialize file descriptors to default: same as simulator 4252093SN/A int stdin_fd, stdout_fd, stderr_fd; 4262680Sktlim@umich.edu 4272093SN/A if (in == "stdin" || in == "cin") 4282093SN/A stdin_fd = STDIN_FILENO; 4292093SN/A else 4302093SN/A stdin_fd = Process::openInputFile(input); 4312093SN/A 4322093SN/A if (out == "stdout" || out == "cout") 4332093SN/A stdout_fd = STDOUT_FILENO; 4342680Sktlim@umich.edu else if (out == "stderr" || out == "cerr") 4352093SN/A stdout_fd = STDERR_FILENO; 4362SN/A else 4372715Sstever@eecs.umich.edu stdout_fd = Process::openOutputFile(out); 4382715Sstever@eecs.umich.edu 4392715Sstever@eecs.umich.edu stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 4402715Sstever@eecs.umich.edu 4413114Sgblack@eecs.umich.edu return LiveProcess::create(getInstanceName(), 4423669Sbinkertn@umich.edu stdin_fd, stdout_fd, stderr_fd, 4433114Sgblack@eecs.umich.edu (string)executable == "" ? cmd[0] : executable, 4443114Sgblack@eecs.umich.edu cmd, env); 4453114Sgblack@eecs.umich.edu} 4462715Sstever@eecs.umich.edu 4472715Sstever@eecs.umich.eduREGISTER_SIM_OBJECT("LiveProcess", LiveProcess) 4482715Sstever@eecs.umich.edu