process.cc revision 2474
1/* 2 * Copyright (c) 2001-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <unistd.h> 30#include <fcntl.h> 31 32#include <cstdio> 33#include <string> 34 35#include "base/intmath.hh" 36#include "base/loader/object_file.hh" 37#include "base/loader/symtab.hh" 38#include "base/statistics.hh" 39#include "config/full_system.hh" 40#include "cpu/exec_context.hh" 41#include "mem/page_table.hh" 42#include "mem/mem_object.hh" 43#include "mem/translating_port.hh" 44#include "sim/builder.hh" 45#include "sim/process.hh" 46#include "sim/stats.hh" 47#include "sim/syscall_emul.hh" 48#include "sim/system.hh" 49 50using namespace std; 51using namespace TheISA; 52 53// 54// The purpose of this code is to fake the loader & syscall mechanism 55// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 56// mode when we do have an OS 57// 58#if FULL_SYSTEM 59#error "process.cc not compatible with FULL_SYSTEM" 60#endif 61 62// current number of allocated processes 63int num_processes = 0; 64 65Process::Process(const string &nm, 66 System *_system, 67 int stdin_fd, // initial I/O descriptors 68 int stdout_fd, 69 int stderr_fd) 70 : SimObject(nm), system(_system) 71{ 72 // initialize first 3 fds (stdin, stdout, stderr) 73 fd_map[STDIN_FILENO] = stdin_fd; 74 fd_map[STDOUT_FILENO] = stdout_fd; 75 fd_map[STDERR_FILENO] = stderr_fd; 76 77 // mark remaining fds as free 78 for (int i = 3; i <= MAX_FD; ++i) { 79 fd_map[i] = -1; 80 } 81 82 mmap_start = mmap_end = 0; 83 nxm_start = nxm_end = 0; 84 pTable = new PageTable(system); 85 // other parameters will be initialized when the program is loaded 86} 87 88 89void 90Process::regStats() 91{ 92 using namespace Stats; 93 94 num_syscalls 95 .name(name() + ".PROG:num_syscalls") 96 .desc("Number of system calls") 97 ; 98} 99 100// 101// static helper functions 102// 103int 104Process::openInputFile(const string &filename) 105{ 106 int fd = open(filename.c_str(), O_RDONLY); 107 108 if (fd == -1) { 109 perror(NULL); 110 cerr << "unable to open \"" << filename << "\" for reading\n"; 111 fatal("can't open input file"); 112 } 113 114 return fd; 115} 116 117 118int 119Process::openOutputFile(const string &filename) 120{ 121 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 122 123 if (fd == -1) { 124 perror(NULL); 125 cerr << "unable to open \"" << filename << "\" for writing\n"; 126 fatal("can't open output file"); 127 } 128 129 return fd; 130} 131 132 133int 134Process::registerExecContext(ExecContext *xc) 135{ 136 // add to list 137 int myIndex = execContexts.size(); 138 execContexts.push_back(xc); 139 140 // return CPU number to caller 141 return myIndex; 142} 143 144void 145Process::startup() 146{ 147 if (execContexts.empty()) 148 fatal("Process %s is not associated with any CPUs!\n", name()); 149 150 // first exec context for this process... initialize & enable 151 ExecContext *xc = execContexts[0]; 152 153 // mark this context as active so it will start ticking. 154 xc->activate(0); 155 156 // Here we are grabbing the memory port of the CPU hosting the 157 // initial execution context for initialization. In the long run 158 // this is not what we want, since it means that all 159 // initialization accesses (e.g., loading object file sections) 160 // will be done a cache block at a time through the CPU's cache. 161 // We really want something more like: 162 // 163 // memport = system->physmem->getPort(); 164 // myPort.setPeer(memport); 165 // memport->setPeer(&myPort); 166 // initVirtMem = new TranslatingPort(myPort, pTable); 167 // 168 // but we need our own dummy port "myPort" that doesn't exist. 169 // In the short term it works just fine though. 170 initVirtMem = xc->getMemPort(); 171} 172 173void 174Process::replaceExecContext(ExecContext *xc, int xcIndex) 175{ 176 if (xcIndex >= execContexts.size()) { 177 panic("replaceExecContext: bad xcIndex, %d >= %d\n", 178 xcIndex, execContexts.size()); 179 } 180 181 execContexts[xcIndex] = xc; 182} 183 184// map simulator fd sim_fd to target fd tgt_fd 185void 186Process::dup_fd(int sim_fd, int tgt_fd) 187{ 188 if (tgt_fd < 0 || tgt_fd > MAX_FD) 189 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 190 191 fd_map[tgt_fd] = sim_fd; 192} 193 194 195// generate new target fd for sim_fd 196int 197Process::alloc_fd(int sim_fd) 198{ 199 // in case open() returns an error, don't allocate a new fd 200 if (sim_fd == -1) 201 return -1; 202 203 // find first free target fd 204 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 205 if (fd_map[free_fd] == -1) { 206 fd_map[free_fd] = sim_fd; 207 return free_fd; 208 } 209 } 210 211 panic("Process::alloc_fd: out of file descriptors!"); 212} 213 214 215// free target fd (e.g., after close) 216void 217Process::free_fd(int tgt_fd) 218{ 219 if (fd_map[tgt_fd] == -1) 220 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 221 222 fd_map[tgt_fd] = -1; 223} 224 225 226// look up simulator fd for given target fd 227int 228Process::sim_fd(int tgt_fd) 229{ 230 if (tgt_fd > MAX_FD) 231 return -1; 232 233 return fd_map[tgt_fd]; 234} 235 236 237 238// 239// need to declare these here since there is no concrete Process type 240// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 241// which is where these get declared for concrete types). 242// 243DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 244 245 246//////////////////////////////////////////////////////////////////////// 247// 248// LiveProcess member definitions 249// 250//////////////////////////////////////////////////////////////////////// 251 252 253static void 254copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 255 TranslatingPort* memPort) 256{ 257 Addr data_ptr_swap; 258 for (int i = 0; i < strings.size(); ++i) { 259 data_ptr_swap = htog(data_ptr); 260 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap, sizeof(Addr)); 261 memPort->writeString(data_ptr, strings[i].c_str()); 262 array_ptr += sizeof(Addr); 263 data_ptr += strings[i].size() + 1; 264 } 265 // add NULL terminator 266 data_ptr = 0; 267 268 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 269} 270 271LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 272 System *_system, 273 int stdin_fd, int stdout_fd, int stderr_fd, 274 vector<string> &_argv, vector<string> &_envp) 275 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 276 objFile(_objFile), argv(_argv), envp(_envp) 277{ 278 prog_fname = argv[0]; 279 280 // load up symbols, if any... these may be used for debugging or 281 // profiling. 282 if (!debugSymbolTable) { 283 debugSymbolTable = new SymbolTable(); 284 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 285 !objFile->loadLocalSymbols(debugSymbolTable)) { 286 // didn't load any symbols 287 delete debugSymbolTable; 288 debugSymbolTable = NULL; 289 } 290 } 291} 292 293void 294LiveProcess::argsInit(int intSize, int pageSize) 295{ 296 Process::startup(); 297 298 // load object file into target memory 299 objFile->loadSections(initVirtMem); 300 301 // Calculate how much space we need for arg & env arrays. 302 int argv_array_size = intSize * (argv.size() + 1); 303 int envp_array_size = intSize * (envp.size() + 1); 304 int arg_data_size = 0; 305 for (int i = 0; i < argv.size(); ++i) { 306 arg_data_size += argv[i].size() + 1; 307 } 308 int env_data_size = 0; 309 for (int i = 0; i < envp.size(); ++i) { 310 env_data_size += envp[i].size() + 1; 311 } 312 313 int space_needed = 314 argv_array_size + envp_array_size + arg_data_size + env_data_size; 315 // for SimpleScalar compatibility 316 if (space_needed < 16384) 317 space_needed = 16384; 318 319 // set bottom of stack 320 stack_min = stack_base - space_needed; 321 // align it 322 stack_min &= ~(intSize-1); 323 stack_size = stack_base - stack_min; 324 // map memory 325 pTable->allocate(roundDown(stack_min, pageSize), 326 roundUp(stack_size, pageSize)); 327 328 // map out initial stack contents 329 Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 330 Addr envp_array_base = argv_array_base + argv_array_size; 331 Addr arg_data_base = envp_array_base + envp_array_size; 332 Addr env_data_base = arg_data_base + arg_data_size; 333 334 // write contents to stack 335 uint64_t argc = argv.size(); 336 if (intSize == 8) 337 argc = htog((uint64_t)argc); 338 else if (intSize == 4) 339 argc = htog((uint32_t)argc); 340 else 341 panic("Unknown int size"); 342 343 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 344 345 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 346 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 347 348 execContexts[0]->setIntReg(ArgumentReg0, argc); 349 execContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 350 execContexts[0]->setIntReg(StackPointerReg, stack_min); 351 352 Addr prog_entry = objFile->entryPoint(); 353 execContexts[0]->setPC(prog_entry); 354 execContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 355 execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 356 357 num_processes++; 358} 359 360void 361LiveProcess::syscall(ExecContext *xc) 362{ 363 num_syscalls++; 364 365 int64_t callnum = xc->readIntReg(SyscallNumReg); 366 367 SyscallDesc *desc = getDesc(callnum); 368 if (desc == NULL) 369 fatal("Syscall %d out of range", callnum); 370 371 desc->doSyscall(callnum, this, xc); 372} 373 374DEFINE_SIM_OBJECT_CLASS_NAME("LiveProcess", LiveProcess); 375