process.cc revision 2519
11689SN/A/* 22325SN/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 292756Sksewell@umich.edu#include <unistd.h> 301689SN/A#include <fcntl.h> 311689SN/A 321858SN/A#include <cstdio> 332733Sktlim@umich.edu#include <string> 341858SN/A 354762Snate@binkert.org#include "base/intmath.hh" 364762Snate@binkert.org#include "base/loader/object_file.hh" 374762Snate@binkert.org#include "base/loader/symtab.hh" 384762Snate@binkert.org#include "base/statistics.hh" 394762Snate@binkert.org#include "config/full_system.hh" 405595Sgblack@eecs.umich.edu#include "cpu/exec_context.hh" 414762Snate@binkert.org#include "mem/page_table.hh" 424762Snate@binkert.org#include "mem/mem_object.hh" 434762Snate@binkert.org#include "mem/translating_port.hh" 444762Snate@binkert.org#include "sim/builder.hh" 451858SN/A#include "sim/process.hh" 462356SN/A#include "sim/stats.hh" 471060SN/A#include "sim/syscall_emul.hh" 481060SN/A#include "sim/system.hh" 491060SN/A 501060SN/Ausing namespace std; 511060SN/Ausing namespace TheISA; 522794Sktlim@umich.edu 532794Sktlim@umich.edu// 542794Sktlim@umich.edu// The purpose of this code is to fake the loader & syscall mechanism 552794Sktlim@umich.edu// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 565595Sgblack@eecs.umich.edu// mode when we do have an OS 575595Sgblack@eecs.umich.edu// 585595Sgblack@eecs.umich.edu#if FULL_SYSTEM 595595Sgblack@eecs.umich.edu#error "process.cc not compatible with FULL_SYSTEM" 605529Snate@binkert.org#endif 615529Snate@binkert.org 622669Sktlim@umich.edu// current number of allocated processes 631060SN/Aint num_processes = 0; 645529Snate@binkert.org 652292SN/AProcess::Process(const string &nm, 661060SN/A System *_system, 671060SN/A int stdin_fd, // initial I/O descriptors 681060SN/A int stdout_fd, 692292SN/A int stderr_fd) 702733Sktlim@umich.edu : SimObject(nm), system(_system) 712292SN/A{ 722292SN/A // initialize first 3 fds (stdin, stdout, stderr) 732292SN/A fd_map[STDIN_FILENO] = stdin_fd; 742292SN/A fd_map[STDOUT_FILENO] = stdout_fd; 751060SN/A fd_map[STDERR_FILENO] = stderr_fd; 761755SN/A 775606Snate@binkert.org // mark remaining fds as free 781060SN/A for (int i = 3; i <= MAX_FD; ++i) { 791060SN/A fd_map[i] = -1; 801060SN/A } 811060SN/A 821060SN/A mmap_start = mmap_end = 0; 831755SN/A nxm_start = nxm_end = 0; 841060SN/A pTable = new PageTable(system); 851060SN/A // other parameters will be initialized when the program is loaded 861060SN/A} 871060SN/A 881060SN/A 891060SN/Avoid 905336Shines@cs.fsu.eduProcess::regStats() 911060SN/A{ 924873Sstever@eecs.umich.edu using namespace Stats; 931060SN/A 941060SN/A num_syscalls 951060SN/A .name(name() + ".PROG:num_syscalls") 962829Sksewell@umich.edu .desc("Number of system calls") 975606Snate@binkert.org ; 982829Sksewell@umich.edu} 992829Sksewell@umich.edu 1002829Sksewell@umich.edu// 1012829Sksewell@umich.edu// static helper functions 1022829Sksewell@umich.edu// 1032829Sksewell@umich.eduint 1042829Sksewell@umich.eduProcess::openInputFile(const string &filename) 1052829Sksewell@umich.edu{ 1062829Sksewell@umich.edu int fd = open(filename.c_str(), O_RDONLY); 1072829Sksewell@umich.edu 1082829Sksewell@umich.edu if (fd == -1) { 1092829Sksewell@umich.edu perror(NULL); 1102829Sksewell@umich.edu cerr << "unable to open \"" << filename << "\" for reading\n"; 1112829Sksewell@umich.edu fatal("can't open input file"); 1122829Sksewell@umich.edu } 1132829Sksewell@umich.edu 1142829Sksewell@umich.edu return fd; 1152829Sksewell@umich.edu} 1162829Sksewell@umich.edu 1172829Sksewell@umich.edu 1182829Sksewell@umich.eduint 1195336Shines@cs.fsu.eduProcess::openOutputFile(const string &filename) 1202829Sksewell@umich.edu{ 1214873Sstever@eecs.umich.edu int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 1222829Sksewell@umich.edu 1232829Sksewell@umich.edu if (fd == -1) { 1242829Sksewell@umich.edu perror(NULL); 1252875Sksewell@umich.edu cerr << "unable to open \"" << filename << "\" for writing\n"; 1265606Snate@binkert.org fatal("can't open output file"); 1272875Sksewell@umich.edu } 1282875Sksewell@umich.edu 1292875Sksewell@umich.edu return fd; 1302875Sksewell@umich.edu} 1312875Sksewell@umich.edu 1322875Sksewell@umich.edu 1333859Sbinkertn@umich.eduint 1342875Sksewell@umich.eduProcess::registerExecContext(ExecContext *xc) 1352875Sksewell@umich.edu{ 1362875Sksewell@umich.edu // add to list 1373859Sbinkertn@umich.edu int myIndex = execContexts.size(); 1382875Sksewell@umich.edu execContexts.push_back(xc); 1392875Sksewell@umich.edu 1402875Sksewell@umich.edu // return CPU number to caller 1412875Sksewell@umich.edu return myIndex; 1422875Sksewell@umich.edu} 1432875Sksewell@umich.edu 1442875Sksewell@umich.eduvoid 1453221Sktlim@umich.eduProcess::startup() 1463221Sktlim@umich.edu{ 1472875Sksewell@umich.edu if (execContexts.empty()) 1482875Sksewell@umich.edu fatal("Process %s is not associated with any CPUs!\n", name()); 1492875Sksewell@umich.edu 1502875Sksewell@umich.edu // first exec context for this process... initialize & enable 1515336Shines@cs.fsu.edu ExecContext *xc = execContexts[0]; 1522875Sksewell@umich.edu 1534873Sstever@eecs.umich.edu // mark this context as active so it will start ticking. 1542875Sksewell@umich.edu xc->activate(0); 1552875Sksewell@umich.edu 1562875Sksewell@umich.edu Port *mem_port; 1575595Sgblack@eecs.umich.edu mem_port = system->physmem->getPort("functional"); 1582733Sktlim@umich.edu initVirtMem = new TranslatingPort(pTable, true); 1593781Sgblack@eecs.umich.edu mem_port->setPeer(initVirtMem); 1603781Sgblack@eecs.umich.edu initVirtMem->setPeer(mem_port); 1611060SN/A} 1622292SN/A 1635595Sgblack@eecs.umich.eduvoid 1645595Sgblack@eecs.umich.eduProcess::replaceExecContext(ExecContext *xc, int xcIndex) 1655595Sgblack@eecs.umich.edu{ 1665595Sgblack@eecs.umich.edu if (xcIndex >= execContexts.size()) { 1675595Sgblack@eecs.umich.edu panic("replaceExecContext: bad xcIndex, %d >= %d\n", 1681060SN/A xcIndex, execContexts.size()); 1695595Sgblack@eecs.umich.edu } 1704329Sktlim@umich.edu 1711060SN/A execContexts[xcIndex] = xc; 1725529Snate@binkert.org} 1732292SN/A 1742292SN/A// map simulator fd sim_fd to target fd tgt_fd 1751060SN/Avoid 1765595Sgblack@eecs.umich.eduProcess::dup_fd(int sim_fd, int tgt_fd) 1774329Sktlim@umich.edu{ 1782292SN/A if (tgt_fd < 0 || tgt_fd > MAX_FD) 1795529Snate@binkert.org panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 1801060SN/A 1815529Snate@binkert.org fd_map[tgt_fd] = sim_fd; 1822292SN/A} 1832292SN/A 1842292SN/A 1852292SN/A// generate new target fd for sim_fd 1861060SN/Aint 1872873Sktlim@umich.eduProcess::alloc_fd(int sim_fd) 1882873Sktlim@umich.edu{ 1892873Sktlim@umich.edu // in case open() returns an error, don't allocate a new fd 1902873Sktlim@umich.edu if (sim_fd == -1) 1912873Sktlim@umich.edu return -1; 1922873Sktlim@umich.edu 1932873Sktlim@umich.edu // find first free target fd 1942873Sktlim@umich.edu for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 1951060SN/A if (fd_map[free_fd] == -1) { 1961060SN/A fd_map[free_fd] = sim_fd; 1971858SN/A return free_fd; 1982292SN/A } 1991060SN/A } 2001060SN/A 2012843Sktlim@umich.edu panic("Process::alloc_fd: out of file descriptors!"); 2025529Snate@binkert.org} 2032316SN/A 2041060SN/A 2053221Sktlim@umich.edu// free target fd (e.g., after close) 2063221Sktlim@umich.eduvoid 2073221Sktlim@umich.eduProcess::free_fd(int tgt_fd) 2083221Sktlim@umich.edu{ 2093221Sktlim@umich.edu if (fd_map[tgt_fd] == -1) 2101681SN/A warn("Process::free_fd: request to free unused fd %d", tgt_fd); 2114598Sbinkertn@umich.edu 2122794Sktlim@umich.edu fd_map[tgt_fd] = -1; 2132316SN/A} 2142316SN/A 2152316SN/A 2162316SN/A// look up simulator fd for given target fd 2172316SN/Aint 2184598Sbinkertn@umich.eduProcess::sim_fd(int tgt_fd) 2194598Sbinkertn@umich.edu{ 2204598Sbinkertn@umich.edu if (tgt_fd > MAX_FD) 2212794Sktlim@umich.edu return -1; 2222316SN/A 2231858SN/A return fd_map[tgt_fd]; 2242292SN/A} 2252292SN/A 2261681SN/A 2271681SN/A 2282325SN/A// 2292325SN/A// need to declare these here since there is no concrete Process type 2302325SN/A// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 2311060SN/A// which is where these get declared for concrete types). 2322292SN/A// 2332292SN/ADEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 2342292SN/A 2352292SN/A 2362292SN/A//////////////////////////////////////////////////////////////////////// 2372292SN/A// 2381060SN/A// LiveProcess member definitions 2391060SN/A// 2401060SN/A//////////////////////////////////////////////////////////////////////// 2411060SN/A 2421060SN/A 2431060SN/Astatic void 2441060SN/AcopyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 2451060SN/A TranslatingPort* memPort) 2461060SN/A{ 2471060SN/A Addr data_ptr_swap; 2481060SN/A for (int i = 0; i < strings.size(); ++i) { 2492292SN/A data_ptr_swap = htog(data_ptr); 2501060SN/A memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap, sizeof(Addr)); 2511060SN/A memPort->writeString(data_ptr, strings[i].c_str()); 2521060SN/A array_ptr += sizeof(Addr); 2531060SN/A data_ptr += strings[i].size() + 1; 2541060SN/A } 2551060SN/A // add NULL terminator 2561060SN/A data_ptr = 0; 2571060SN/A 2582292SN/A memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 2592292SN/A} 2602292SN/A 2612292SN/ALiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 2622292SN/A System *_system, 2632307SN/A int stdin_fd, int stdout_fd, int stderr_fd, 2642831Sksewell@umich.edu vector<string> &_argv, vector<string> &_envp) 2652831Sksewell@umich.edu : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 2662831Sksewell@umich.edu objFile(_objFile), argv(_argv), envp(_envp) 2672831Sksewell@umich.edu{ 2682831Sksewell@umich.edu prog_fname = argv[0]; 2692831Sksewell@umich.edu 2702292SN/A // load up symbols, if any... these may be used for debugging or 2712307SN/A // profiling. 2722292SN/A if (!debugSymbolTable) { 2732292SN/A debugSymbolTable = new SymbolTable(); 2742316SN/A if (!objFile->loadGlobalSymbols(debugSymbolTable) || 2752292SN/A !objFile->loadLocalSymbols(debugSymbolTable)) { 2762292SN/A // didn't load any symbols 2772292SN/A delete debugSymbolTable; 2782292SN/A debugSymbolTable = NULL; 2792292SN/A } 2802292SN/A } 2811060SN/A} 2822292SN/A 2832292SN/Avoid 2841060SN/ALiveProcess::argsInit(int intSize, int pageSize) 2852292SN/A{ 2862307SN/A Process::startup(); 2872292SN/A 2882292SN/A // load object file into target memory 2892292SN/A objFile->loadSections(initVirtMem); 2902325SN/A 2912292SN/A // Calculate how much space we need for arg & env arrays. 2922292SN/A int argv_array_size = intSize * (argv.size() + 1); 2932292SN/A int envp_array_size = intSize * (envp.size() + 1); 2942325SN/A int arg_data_size = 0; 2952292SN/A for (int i = 0; i < argv.size(); ++i) { 2962292SN/A arg_data_size += argv[i].size() + 1; 2972292SN/A } 2982292SN/A int env_data_size = 0; 2992292SN/A for (int i = 0; i < envp.size(); ++i) { 3002292SN/A env_data_size += envp[i].size() + 1; 3012292SN/A } 3022292SN/A 3032292SN/A int space_needed = 3042292SN/A argv_array_size + envp_array_size + arg_data_size + env_data_size; 3052292SN/A // for SimpleScalar compatibility 3062325SN/A if (space_needed < 16384) 3072292SN/A space_needed = 16384; 3082292SN/A 3092292SN/A // set bottom of stack 3102325SN/A stack_min = stack_base - space_needed; 3112292SN/A // align it 3122292SN/A stack_min &= ~(intSize-1); 3132292SN/A stack_size = stack_base - stack_min; 3142292SN/A // map memory 3152292SN/A pTable->allocate(roundDown(stack_min, pageSize), 3162292SN/A roundUp(stack_size, pageSize)); 3172292SN/A 3182292SN/A // map out initial stack contents 3193221Sktlim@umich.edu Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 3203221Sktlim@umich.edu Addr envp_array_base = argv_array_base + argv_array_size; 3213221Sktlim@umich.edu Addr arg_data_base = envp_array_base + envp_array_size; 3222292SN/A Addr env_data_base = arg_data_base + arg_data_size; 3232292SN/A 3242292SN/A // write contents to stack 3252292SN/A uint64_t argc = argv.size(); 3262292SN/A if (intSize == 8) 3272292SN/A argc = htog((uint64_t)argc); 3282292SN/A else if (intSize == 4) 3292292SN/A argc = htog((uint32_t)argc); 3302292SN/A else 3311060SN/A panic("Unknown int size"); 3322292SN/A 3331060SN/A initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 3341060SN/A 3352292SN/A copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 3362292SN/A copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 3372292SN/A 3382829Sksewell@umich.edu execContexts[0]->setIntReg(ArgumentReg0, argc); 3392829Sksewell@umich.edu execContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 3403093Sksewell@umich.edu execContexts[0]->setIntReg(StackPointerReg, stack_min); 3413093Sksewell@umich.edu 3423093Sksewell@umich.edu Addr prog_entry = objFile->entryPoint(); 3433093Sksewell@umich.edu execContexts[0]->setPC(prog_entry); 3443093Sksewell@umich.edu execContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 3452292SN/A execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 3465595Sgblack@eecs.umich.edu 3475595Sgblack@eecs.umich.edu num_processes++; 3485595Sgblack@eecs.umich.edu} 3495595Sgblack@eecs.umich.edu 3505595Sgblack@eecs.umich.eduvoid 3515595Sgblack@eecs.umich.eduLiveProcess::syscall(ExecContext *xc) 3525595Sgblack@eecs.umich.edu{ 3535595Sgblack@eecs.umich.edu num_syscalls++; 3545595Sgblack@eecs.umich.edu 3555595Sgblack@eecs.umich.edu int64_t callnum = xc->readIntReg(SyscallNumReg); 3565595Sgblack@eecs.umich.edu 3575595Sgblack@eecs.umich.edu SyscallDesc *desc = getDesc(callnum); 3585595Sgblack@eecs.umich.edu if (desc == NULL) 3595595Sgblack@eecs.umich.edu fatal("Syscall %d out of range", callnum); 3605595Sgblack@eecs.umich.edu 3615595Sgblack@eecs.umich.edu desc->doSyscall(callnum, this, xc); 3625595Sgblack@eecs.umich.edu} 3635595Sgblack@eecs.umich.edu 3645595Sgblack@eecs.umich.eduDEFINE_SIM_OBJECT_CLASS_NAME("LiveProcess", LiveProcess); 3655595Sgblack@eecs.umich.edu