process.cc revision 2378
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 "cpu/smt.hh" 42#include "encumbered/cpu/full/thread.hh" 43#include "encumbered/eio/eio.hh" 44#include "mem/page_table.hh" 45#include "mem/functional/physical.hh" 46#include "mem/functional/proxy.hh" 47#include "sim/builder.hh" 48#include "sim/fake_syscall.hh" 49#include "sim/process.hh" 50#include "sim/stats.hh" 51#include "sim/system.hh" 52 53#ifdef TARGET_ALPHA 54#include "arch/alpha/alpha_tru64_process.hh" 55#include "arch/alpha/alpha_linux_process.hh" 56#endif 57 58using namespace std; 59 60// 61// The purpose of this code is to fake the loader & syscall mechanism 62// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 63// mode when we do have an OS 64// 65#if FULL_SYSTEM 66#error "process.cc not compatible with FULL_SYSTEM" 67#endif 68 69// current number of allocated processes 70int num_processes = 0; 71 72Process::Process(const string &nm, 73 System *_system, 74 int stdin_fd, // initial I/O descriptors 75 int stdout_fd, 76 int stderr_fd) 77 : SimObject(nm), system(_system) 78{ 79 // initialize first 3 fds (stdin, stdout, stderr) 80 fd_map[STDIN_FILENO] = stdin_fd; 81 fd_map[STDOUT_FILENO] = stdout_fd; 82 fd_map[STDERR_FILENO] = stderr_fd; 83 84 // mark remaining fds as free 85 for (int i = 3; i <= MAX_FD; ++i) { 86 fd_map[i] = -1; 87 } 88 89 mmap_start = mmap_end = 0; 90 nxm_start = nxm_end = 0; 91 pTable = new PageTable(system); 92 // other parameters will be initialized when the program is loaded 93} 94 95 96void 97Process::regStats() 98{ 99 using namespace Stats; 100 101 num_syscalls 102 .name(name() + ".PROG:num_syscalls") 103 .desc("Number of system calls") 104 ; 105} 106 107// 108// static helper functions 109// 110int 111Process::openInputFile(const string &filename) 112{ 113 int fd = open(filename.c_str(), O_RDONLY); 114 115 if (fd == -1) { 116 perror(NULL); 117 cerr << "unable to open \"" << filename << "\" for reading\n"; 118 fatal("can't open input file"); 119 } 120 121 return fd; 122} 123 124 125int 126Process::openOutputFile(const string &filename) 127{ 128 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 129 130 if (fd == -1) { 131 perror(NULL); 132 cerr << "unable to open \"" << filename << "\" for writing\n"; 133 fatal("can't open output file"); 134 } 135 136 return fd; 137} 138 139 140int 141Process::registerExecContext(ExecContext *xc) 142{ 143 // add to list 144 int myIndex = execContexts.size(); 145 execContexts.push_back(xc); 146 147 // return CPU number to caller 148 return myIndex; 149} 150 151void 152Process::startup() 153{ 154 if (execContexts.empty()) 155 fatal("Process %s is not associated with any CPUs!\n", name()); 156 157 initVirtMem = new ProxyMemory<FunctionalMemory>(system->physmem, pTable); 158 159 // first exec context for this process... initialize & enable 160 ExecContext *xc = execContexts[0]; 161 162 // mark this context as active so it will start ticking. 163 xc->activate(0); 164} 165 166void 167Process::replaceExecContext(ExecContext *xc, int xcIndex) 168{ 169 if (xcIndex >= execContexts.size()) { 170 panic("replaceExecContext: bad xcIndex, %d >= %d\n", 171 xcIndex, execContexts.size()); 172 } 173 174 execContexts[xcIndex] = xc; 175} 176 177// map simulator fd sim_fd to target fd tgt_fd 178void 179Process::dup_fd(int sim_fd, int tgt_fd) 180{ 181 if (tgt_fd < 0 || tgt_fd > MAX_FD) 182 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 183 184 fd_map[tgt_fd] = sim_fd; 185} 186 187 188// generate new target fd for sim_fd 189int 190Process::alloc_fd(int sim_fd) 191{ 192 // in case open() returns an error, don't allocate a new fd 193 if (sim_fd == -1) 194 return -1; 195 196 // find first free target fd 197 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 198 if (fd_map[free_fd] == -1) { 199 fd_map[free_fd] = sim_fd; 200 return free_fd; 201 } 202 } 203 204 panic("Process::alloc_fd: out of file descriptors!"); 205} 206 207 208// free target fd (e.g., after close) 209void 210Process::free_fd(int tgt_fd) 211{ 212 if (fd_map[tgt_fd] == -1) 213 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 214 215 fd_map[tgt_fd] = -1; 216} 217 218 219// look up simulator fd for given target fd 220int 221Process::sim_fd(int tgt_fd) 222{ 223 if (tgt_fd > MAX_FD) 224 return -1; 225 226 return fd_map[tgt_fd]; 227} 228 229 230 231// 232// need to declare these here since there is no concrete Process type 233// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 234// which is where these get declared for concrete types). 235// 236DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 237 238 239//////////////////////////////////////////////////////////////////////// 240// 241// LiveProcess member definitions 242// 243//////////////////////////////////////////////////////////////////////// 244 245 246static void 247copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 248 FunctionalMemory *func) 249{ 250 for (int i = 0; i < strings.size(); ++i) { 251 func->prot_write(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 252 func->writeString(data_ptr, strings[i].c_str()); 253 array_ptr += sizeof(Addr); 254 data_ptr += strings[i].size() + 1; 255 } 256 // add NULL terminator 257 data_ptr = 0; 258 259 func->prot_write(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 260} 261 262LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 263 System *_system, 264 int stdin_fd, int stdout_fd, int stderr_fd, 265 vector<string> &_argv, vector<string> &_envp) 266 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 267 objFile(_objFile), argv(_argv), envp(_envp) 268{ 269 prog_fname = argv[0]; 270 271 prog_entry = objFile->entryPoint(); 272 text_base = objFile->textBase(); 273 text_size = objFile->textSize(); 274 data_base = objFile->dataBase(); 275 data_size = objFile->dataSize() + objFile->bssSize(); 276 brk_point = = roundUp(data_base + data_size, VMPageSize); 277 278 // Set up stack. On Alpha, stack goes below text section. This 279 // code should get moved to some architecture-specific spot. 280 stack_base = text_base - (409600+4096); 281 282 // Set up region for mmaps. Tru64 seems to start just above 0 and 283 // grow up from there. 284 mmap_start = mmap_end = 0x10000; 285 286 // Set pointer for next thread stack. Reserve 8M for main stack. 287 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 288 289 // load up symbols, if any... these may be used for debugging or 290 // profiling. 291 if (!debugSymbolTable) { 292 debugSymbolTable = new SymbolTable(); 293 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 294 !objFile->loadLocalSymbols(debugSymbolTable)) { 295 // didn't load any symbols 296 delete debugSymbolTable; 297 debugSymbolTable = NULL; 298 } 299 } 300} 301 302void 303LiveProcess::startup() 304{ 305 Process::startup(); 306 307 // load object file into target memory 308 objFile->loadSections(initVirtMem); 309 310 // Calculate how much space we need for arg & env arrays. 311 int argv_array_size = sizeof(Addr) * (argv.size() + 1); 312 int envp_array_size = sizeof(Addr) * (envp.size() + 1); 313 int arg_data_size = 0; 314 for (int i = 0; i < argv.size(); ++i) { 315 arg_data_size += argv[i].size() + 1; 316 } 317 int env_data_size = 0; 318 for (int i = 0; i < envp.size(); ++i) { 319 env_data_size += envp[i].size() + 1; 320 } 321 322 int space_needed = 323 argv_array_size + envp_array_size + arg_data_size + env_data_size; 324 // for SimpleScalar compatibility 325 if (space_needed < 16384) 326 space_needed = 16384; 327 328 // set bottom of stack 329 stack_min = stack_base - space_needed; 330 // align it 331 stack_min &= ~7; 332 stack_size = stack_base - stack_min; 333 // map memory 334 pTable->allocate(stack_min, stack_size); 335 336 // map out initial stack contents 337 Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 338 Addr envp_array_base = argv_array_base + argv_array_size; 339 Addr arg_data_base = envp_array_base + envp_array_size; 340 Addr env_data_base = arg_data_base + arg_data_size; 341 342 // write contents to stack 343 uint64_t argc = argv.size(); 344 initVirtMem->prot_write(stack_min, (uint8_t*)&argc, sizeof(uint64_t)); 345 346 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 347 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 348 349 RegFile *init_regs = &(execContexts[0]->regs); 350 351 init_regs->intRegFile[ArgumentReg0] = argc; 352 init_regs->intRegFile[ArgumentReg1] = argv_array_base; 353 init_regs->intRegFile[StackPointerReg] = stack_min; 354 init_regs->intRegFile[GlobalPointerReg] = objFile->globalPointer(); 355 init_regs->pc = prog_entry; 356 init_regs->npc = prog_entry + sizeof(MachInst); 357 358 num_processes++; 359} 360 361 362LiveProcess * 363LiveProcess::create(const string &nm, System *system, 364 int stdin_fd, int stdout_fd, int stderr_fd, 365 string executable, 366 vector<string> &argv, vector<string> &envp) 367{ 368 LiveProcess *process = NULL; 369 ObjectFile *objFile = createObjectFile(executable); 370 if (objFile == NULL) { 371 fatal("Can't load object file %s", executable); 372 } 373 374 // check object type & set up syscall emulation pointer 375 if (objFile->getArch() == ObjectFile::Alpha) { 376 377 switch (objFile->getOpSys()) { 378 case ObjectFile::Tru64: 379 process = new AlphaTru64Process(nm, objFile, system, 380 stdin_fd, stdout_fd, stderr_fd, 381 argv, envp); 382 383 break; 384 385 case ObjectFile::Linux: 386 process = new AlphaLinuxProcess(nm, objFile, system, 387 stdin_fd, stdout_fd, stderr_fd, 388 argv, envp); 389 390 break; 391 392 default: 393 fatal("Unknown/unsupported operating system."); 394 } 395 } else { 396 fatal("Unknown object file architecture."); 397 } 398 399 delete objFile; 400 401 if (process == NULL) 402 fatal("Unknown error creating process object."); 403 404 return process; 405} 406 407 408BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 409 410 VectorParam<string> cmd; 411 Param<string> executable; 412 Param<string> input; 413 Param<string> output; 414 VectorParam<string> env; 415 SimObjectParam<System *> system; 416 417END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 418 419 420BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 421 422 INIT_PARAM(cmd, "command line (executable plus arguments)"), 423 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 424 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 425 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 426 INIT_PARAM(env, "environment settings"), 427 INIT_PARAM(system, "system") 428 429END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 430 431 432CREATE_SIM_OBJECT(LiveProcess) 433{ 434 string in = input; 435 string out = output; 436 437 // initialize file descriptors to default: same as simulator 438 int stdin_fd, stdout_fd, stderr_fd; 439 440 if (in == "stdin" || in == "cin") 441 stdin_fd = STDIN_FILENO; 442 else 443 stdin_fd = Process::openInputFile(input); 444 445 if (out == "stdout" || out == "cout") 446 stdout_fd = STDOUT_FILENO; 447 else if (out == "stderr" || out == "cerr") 448 stdout_fd = STDERR_FILENO; 449 else 450 stdout_fd = Process::openOutputFile(out); 451 452 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 453 454 return LiveProcess::create(getInstanceName(), system, 455 stdin_fd, stdout_fd, stderr_fd, 456 (string)executable == "" ? cmd[0] : executable, 457 cmd, env); 458} 459 460REGISTER_SIM_OBJECT("LiveProcess", LiveProcess) 461