process.cc revision 2091
11142Shsul@eecs.umich.edu/* 21142Shsul@eecs.umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan 31142Shsul@eecs.umich.edu * All rights reserved. 41142Shsul@eecs.umich.edu * 51142Shsul@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 61142Shsul@eecs.umich.edu * modification, are permitted provided that the following conditions are 71142Shsul@eecs.umich.edu * met: redistributions of source code must retain the above copyright 81142Shsul@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 91142Shsul@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 101142Shsul@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 111142Shsul@eecs.umich.edu * documentation and/or other materials provided with the distribution; 121142Shsul@eecs.umich.edu * neither the name of the copyright holders nor the names of its 131142Shsul@eecs.umich.edu * contributors may be used to endorse or promote products derived from 141142Shsul@eecs.umich.edu * this software without specific prior written permission. 151142Shsul@eecs.umich.edu * 161142Shsul@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171142Shsul@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181142Shsul@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191142Shsul@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201142Shsul@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211175Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221142Shsul@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231142Shsul@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241142Shsul@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251142Shsul@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261175Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271142Shsul@eecs.umich.edu */ 281142Shsul@eecs.umich.edu 291142Shsul@eecs.umich.edu#include <unistd.h> 301142Shsul@eecs.umich.edu#include <fcntl.h> 311175Sbinkertn@umich.edu 321142Shsul@eecs.umich.edu#include <cstdio> 331142Shsul@eecs.umich.edu#include <string> 341142Shsul@eecs.umich.edu 351142Shsul@eecs.umich.edu#include "base/intmath.hh" 361142Shsul@eecs.umich.edu#include "base/loader/object_file.hh" 371142Shsul@eecs.umich.edu#include "base/loader/symtab.hh" 381142Shsul@eecs.umich.edu#include "base/statistics.hh" 391142Shsul@eecs.umich.edu#include "config/full_system.hh" 401177Sbinkertn@umich.edu#include "cpu/exec_context.hh" 411142Shsul@eecs.umich.edu#include "cpu/smt.hh" 421142Shsul@eecs.umich.edu#include "encumbered/cpu/full/thread.hh" 431142Shsul@eecs.umich.edu#include "encumbered/eio/eio.hh" 441142Shsul@eecs.umich.edu#include "encumbered/mem/functional/main.hh" 451142Shsul@eecs.umich.edu#include "sim/builder.hh" 461142Shsul@eecs.umich.edu#include "sim/fake_syscall.hh" 471142Shsul@eecs.umich.edu#include "sim/process.hh" 48#include "sim/stats.hh" 49 50#ifdef TARGET_ALPHA 51#include "arch/alpha/alpha_tru64_process.hh" 52#include "arch/alpha/alpha_linux_process.hh" 53#endif 54 55using namespace std; 56 57// 58// The purpose of this code is to fake the loader & syscall mechanism 59// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 60// mode when we do have an OS 61// 62#if FULL_SYSTEM 63#error "process.cc not compatible with FULL_SYSTEM" 64#endif 65 66// current number of allocated processes 67int num_processes = 0; 68 69Process::Process(const string &nm, 70 int stdin_fd, // initial I/O descriptors 71 int stdout_fd, 72 int stderr_fd) 73 : SimObject(nm) 74{ 75 // allocate memory space 76 memory = new MainMemory(nm + ".MainMem"); 77 78 // allocate initial register file 79 init_regs = new RegFile; 80 memset(init_regs, 0, sizeof(RegFile)); 81 82 // initialize first 3 fds (stdin, stdout, stderr) 83 fd_map[STDIN_FILENO] = stdin_fd; 84 fd_map[STDOUT_FILENO] = stdout_fd; 85 fd_map[STDERR_FILENO] = stderr_fd; 86 87 // mark remaining fds as free 88 for (int i = 3; i <= MAX_FD; ++i) { 89 fd_map[i] = -1; 90 } 91 92 mmap_start = mmap_end = 0; 93 nxm_start = nxm_end = 0; 94 // other parameters will be initialized when the program is loaded 95} 96 97void 98Process::regStats() 99{ 100 using namespace Stats; 101 102 num_syscalls 103 .name(name() + ".PROG:num_syscalls") 104 .desc("Number of system calls") 105 ; 106} 107 108// 109// static helper functions 110// 111int 112Process::openInputFile(const string &filename) 113{ 114 int fd = open(filename.c_str(), O_RDONLY); 115 116 if (fd == -1) { 117 perror(NULL); 118 cerr << "unable to open \"" << filename << "\" for reading\n"; 119 fatal("can't open input file"); 120 } 121 122 return fd; 123} 124 125 126int 127Process::openOutputFile(const string &filename) 128{ 129 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 130 131 if (fd == -1) { 132 perror(NULL); 133 cerr << "unable to open \"" << filename << "\" for writing\n"; 134 fatal("can't open output file"); 135 } 136 137 return fd; 138} 139 140 141int 142Process::registerExecContext(ExecContext *xc) 143{ 144 // add to list 145 int myIndex = execContexts.size(); 146 execContexts.push_back(xc); 147 148 if (myIndex == 0) { 149 // copy process's initial regs struct 150 xc->regs = *init_regs; 151 } 152 153 // return CPU number to caller and increment available CPU count 154 return myIndex; 155} 156 157void 158Process::startup() 159{ 160 if (execContexts.empty()) 161 return; 162 163 // first exec context for this process... initialize & enable 164 ExecContext *xc = execContexts[0]; 165 166 // mark this context as active so it will start ticking. 167 xc->activate(0); 168} 169 170void 171Process::replaceExecContext(ExecContext *xc, int xcIndex) 172{ 173 if (xcIndex >= execContexts.size()) { 174 panic("replaceExecContext: bad xcIndex, %d >= %d\n", 175 xcIndex, execContexts.size()); 176 } 177 178 execContexts[xcIndex] = xc; 179} 180 181// map simulator fd sim_fd to target fd tgt_fd 182void 183Process::dup_fd(int sim_fd, int tgt_fd) 184{ 185 if (tgt_fd < 0 || tgt_fd > MAX_FD) 186 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 187 188 fd_map[tgt_fd] = sim_fd; 189} 190 191 192// generate new target fd for sim_fd 193int 194Process::alloc_fd(int sim_fd) 195{ 196 // in case open() returns an error, don't allocate a new fd 197 if (sim_fd == -1) 198 return -1; 199 200 // find first free target fd 201 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 202 if (fd_map[free_fd] == -1) { 203 fd_map[free_fd] = sim_fd; 204 return free_fd; 205 } 206 } 207 208 panic("Process::alloc_fd: out of file descriptors!"); 209} 210 211 212// free target fd (e.g., after close) 213void 214Process::free_fd(int tgt_fd) 215{ 216 if (fd_map[tgt_fd] == -1) 217 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 218 219 fd_map[tgt_fd] = -1; 220} 221 222 223// look up simulator fd for given target fd 224int 225Process::sim_fd(int tgt_fd) 226{ 227 if (tgt_fd > MAX_FD) 228 return -1; 229 230 return fd_map[tgt_fd]; 231} 232 233 234 235// 236// need to declare these here since there is no concrete Process type 237// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 238// which is where these get declared for concrete types). 239// 240DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 241 242 243//////////////////////////////////////////////////////////////////////// 244// 245// LiveProcess member definitions 246// 247//////////////////////////////////////////////////////////////////////// 248 249 250static void 251copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 252 FunctionalMemory *memory) 253{ 254 Addr data_ptr_swap; 255 for (int i = 0; i < strings.size(); ++i) { 256 data_ptr_swap = htog(data_ptr); 257 memory->access(Write, array_ptr, &data_ptr_swap, sizeof(Addr)); 258 memory->writeString(data_ptr, strings[i].c_str()); 259 array_ptr += sizeof(Addr); 260 data_ptr += strings[i].size() + 1; 261 } 262 // add NULL terminator 263 data_ptr = 0; 264 memory->access(Write, array_ptr, &data_ptr, sizeof(Addr)); 265} 266 267LiveProcess::LiveProcess(const string &nm, ObjectFile *objFile, 268 int stdin_fd, int stdout_fd, int stderr_fd, 269 vector<string> &argv, vector<string> &envp) 270 : Process(nm, stdin_fd, stdout_fd, stderr_fd) 271{ 272 prog_fname = argv[0]; 273 274 prog_entry = objFile->entryPoint(); 275 text_base = objFile->textBase(); 276 text_size = objFile->textSize(); 277 data_base = objFile->dataBase(); 278 data_size = objFile->dataSize() + objFile->bssSize(); 279 brk_point = roundUp(data_base + data_size, VMPageSize); 280 281 // load object file into target memory 282 objFile->loadSections(memory); 283 284 // load up symbols, if any... these may be used for debugging or 285 // profiling. 286 if (!debugSymbolTable) { 287 debugSymbolTable = new SymbolTable(); 288 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 289 !objFile->loadLocalSymbols(debugSymbolTable)) { 290 // didn't load any symbols 291 delete debugSymbolTable; 292 debugSymbolTable = NULL; 293 } 294 } 295 296 // Set up stack. On Alpha, stack goes below text section. This 297 // code should get moved to some architecture-specific spot. 298 stack_base = text_base - (409600+4096); 299 300 // Set up region for mmaps. Tru64 seems to start just above 0 and 301 // grow up from there. 302 mmap_start = mmap_end = 0x10000; 303 304 // Set pointer for next thread stack. Reserve 8M for main stack. 305 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 306 307 // Calculate how much space we need for arg & env arrays. 308 int argv_array_size = sizeof(Addr) * (argv.size() + 1); 309 int envp_array_size = sizeof(Addr) * (envp.size() + 1); 310 int arg_data_size = 0; 311 for (int i = 0; i < argv.size(); ++i) { 312 arg_data_size += argv[i].size() + 1; 313 } 314 int env_data_size = 0; 315 for (int i = 0; i < envp.size(); ++i) { 316 env_data_size += envp[i].size() + 1; 317 } 318 319 int space_needed = 320 argv_array_size + envp_array_size + arg_data_size + env_data_size; 321 // for SimpleScalar compatibility 322 if (space_needed < 16384) 323 space_needed = 16384; 324 325 // set bottom of stack 326 stack_min = stack_base - space_needed; 327 // align it 328 stack_min &= ~7; 329 stack_size = stack_base - stack_min; 330 331 // map out initial stack contents 332 Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 333 Addr envp_array_base = argv_array_base + argv_array_size; 334 Addr arg_data_base = envp_array_base + envp_array_size; 335 Addr env_data_base = arg_data_base + arg_data_size; 336 337 // write contents to stack 338 uint64_t argc = argv.size(); 339 argc = htog(argc); 340 memory->access(Write, stack_min, &argc, sizeof(uint64_t)); 341 342 copyStringArray(argv, argv_array_base, arg_data_base, memory); 343 copyStringArray(envp, envp_array_base, env_data_base, memory); 344 345 init_regs->intRegFile[ArgumentReg0] = argc; 346 init_regs->intRegFile[ArgumentReg1] = argv_array_base; 347 init_regs->intRegFile[StackPointerReg] = stack_min; 348 init_regs->intRegFile[GlobalPointerReg] = objFile->globalPointer(); 349 init_regs->pc = prog_entry; 350 init_regs->npc = prog_entry + sizeof(MachInst); 351} 352 353 354LiveProcess * 355LiveProcess::create(const string &nm, 356 int stdin_fd, int stdout_fd, int stderr_fd, 357 string executable, 358 vector<string> &argv, vector<string> &envp) 359{ 360 LiveProcess *process = NULL; 361 ObjectFile *objFile = createObjectFile(executable); 362 if (objFile == NULL) { 363 fatal("Can't load object file %s", executable); 364 } 365 366 // check object type & set up syscall emulation pointer 367 if (objFile->getArch() == ObjectFile::Alpha) { 368 switch (objFile->getOpSys()) { 369 case ObjectFile::Tru64: 370 process = new AlphaTru64Process(nm, objFile, 371 stdin_fd, stdout_fd, stderr_fd, 372 argv, envp); 373 break; 374 375 case ObjectFile::Linux: 376 process = new AlphaLinuxProcess(nm, objFile, 377 stdin_fd, stdout_fd, stderr_fd, 378 argv, envp); 379 break; 380 381 default: 382 fatal("Unknown/unsupported operating system."); 383 } 384 } else { 385 fatal("Unknown object file architecture."); 386 } 387 388 delete objFile; 389 390 if (process == NULL) 391 fatal("Unknown error creating process object."); 392 393 return process; 394} 395 396 397BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 398 399 VectorParam<string> cmd; 400 Param<string> executable; 401 Param<string> input; 402 Param<string> output; 403 VectorParam<string> env; 404 405END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 406 407 408BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 409 410 INIT_PARAM(cmd, "command line (executable plus arguments)"), 411 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 412 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 413 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 414 INIT_PARAM(env, "environment settings") 415 416END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 417 418 419CREATE_SIM_OBJECT(LiveProcess) 420{ 421 string in = input; 422 string out = output; 423 424 // initialize file descriptors to default: same as simulator 425 int stdin_fd, stdout_fd, stderr_fd; 426 427 if (in == "stdin" || in == "cin") 428 stdin_fd = STDIN_FILENO; 429 else 430 stdin_fd = Process::openInputFile(input); 431 432 if (out == "stdout" || out == "cout") 433 stdout_fd = STDOUT_FILENO; 434 else if (out == "stderr" || out == "cerr") 435 stdout_fd = STDERR_FILENO; 436 else 437 stdout_fd = Process::openOutputFile(out); 438 439 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 440 441 return LiveProcess::create(getInstanceName(), 442 stdin_fd, stdout_fd, stderr_fd, 443 (string)executable == "" ? cmd[0] : executable, 444 cmd, env); 445} 446 447REGISTER_SIM_OBJECT("LiveProcess", LiveProcess) 448