process.cc revision 1858
13134Srdreslin@umich.edu/* 23134Srdreslin@umich.edu * Copyright (c) 2001-2005 The Regents of The University of Michigan 33134Srdreslin@umich.edu * All rights reserved. 43134Srdreslin@umich.edu * 53134Srdreslin@umich.edu * Redistribution and use in source and binary forms, with or without 63134Srdreslin@umich.edu * modification, are permitted provided that the following conditions are 73134Srdreslin@umich.edu * met: redistributions of source code must retain the above copyright 83134Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer; 93134Srdreslin@umich.edu * redistributions in binary form must reproduce the above copyright 103134Srdreslin@umich.edu * notice, this list of conditions and the following disclaimer in the 113134Srdreslin@umich.edu * documentation and/or other materials provided with the distribution; 123134Srdreslin@umich.edu * neither the name of the copyright holders nor the names of its 133134Srdreslin@umich.edu * contributors may be used to endorse or promote products derived from 143134Srdreslin@umich.edu * this software without specific prior written permission. 153134Srdreslin@umich.edu * 163134Srdreslin@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 173134Srdreslin@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 183134Srdreslin@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 193134Srdreslin@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 203134Srdreslin@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 213134Srdreslin@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 223134Srdreslin@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 233134Srdreslin@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 243134Srdreslin@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 253134Srdreslin@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 263134Srdreslin@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 273134Srdreslin@umich.edu */ 283134Srdreslin@umich.edu 293134Srdreslin@umich.edu#include <unistd.h> 303134Srdreslin@umich.edu#include <fcntl.h> 313134Srdreslin@umich.edu 323134Srdreslin@umich.edu#include <cstdio> 333134Srdreslin@umich.edu#include <string> 343134Srdreslin@umich.edu 353134Srdreslin@umich.edu#include "base/intmath.hh" 363134Srdreslin@umich.edu#include "base/loader/object_file.hh" 373134Srdreslin@umich.edu#include "base/loader/symtab.hh" 383134Srdreslin@umich.edu#include "base/statistics.hh" 393134Srdreslin@umich.edu#include "config/full_system.hh" 403134Srdreslin@umich.edu#include "cpu/exec_context.hh" 413134Srdreslin@umich.edu#include "cpu/smt.hh" 423134Srdreslin@umich.edu#include "encumbered/cpu/full/thread.hh" 433134Srdreslin@umich.edu#include "encumbered/eio/eio.hh" 443134Srdreslin@umich.edu#include "encumbered/mem/functional/main.hh" 453134Srdreslin@umich.edu#include "sim/builder.hh" 463134Srdreslin@umich.edu#include "sim/fake_syscall.hh" 473134Srdreslin@umich.edu#include "sim/process.hh" 483134Srdreslin@umich.edu#include "sim/stats.hh" 493134Srdreslin@umich.edu 503134Srdreslin@umich.edu#ifdef TARGET_ALPHA 513134Srdreslin@umich.edu#include "arch/alpha/alpha_tru64_process.hh" 523134Srdreslin@umich.edu#include "arch/alpha/alpha_linux_process.hh" 533134Srdreslin@umich.edu#endif 543134Srdreslin@umich.edu 553134Srdreslin@umich.eduusing namespace std; 563230Sktlim@umich.edu 573134Srdreslin@umich.edu// 583134Srdreslin@umich.edu// The purpose of this code is to fake the loader & syscall mechanism 593134Srdreslin@umich.edu// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 603134Srdreslin@umich.edu// mode when we do have an OS 613134Srdreslin@umich.edu// 623134Srdreslin@umich.edu#if FULL_SYSTEM 633134Srdreslin@umich.edu#error "process.cc not compatible with FULL_SYSTEM" 643134Srdreslin@umich.edu#endif 653134Srdreslin@umich.edu 663134Srdreslin@umich.edu// current number of allocated processes 673134Srdreslin@umich.eduint num_processes = 0; 683134Srdreslin@umich.edu 693134Srdreslin@umich.eduProcess::Process(const string &nm, 703134Srdreslin@umich.edu int stdin_fd, // initial I/O descriptors 713134Srdreslin@umich.edu int stdout_fd, 723134Srdreslin@umich.edu int stderr_fd) 733134Srdreslin@umich.edu : SimObject(nm) 743134Srdreslin@umich.edu{ 753134Srdreslin@umich.edu // allocate memory space 763134Srdreslin@umich.edu memory = new MainMemory(nm + ".MainMem"); 773134Srdreslin@umich.edu 783134Srdreslin@umich.edu // allocate initial register file 793134Srdreslin@umich.edu init_regs = new RegFile; 803134Srdreslin@umich.edu memset(init_regs, 0, sizeof(RegFile)); 813134Srdreslin@umich.edu 823134Srdreslin@umich.edu // initialize first 3 fds (stdin, stdout, stderr) 833134Srdreslin@umich.edu fd_map[STDIN_FILENO] = stdin_fd; 843134Srdreslin@umich.edu fd_map[STDOUT_FILENO] = stdout_fd; 853134Srdreslin@umich.edu fd_map[STDERR_FILENO] = stderr_fd; 863134Srdreslin@umich.edu 873134Srdreslin@umich.edu // mark remaining fds as free 883200Srdreslin@umich.edu for (int i = 3; i <= MAX_FD; ++i) { 893134Srdreslin@umich.edu 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::open_fd(int sim_fd) 195{ 196 int free_fd; 197 198 // in case open() returns an error, don't allocate a new fd 199 if (sim_fd == -1) 200 return -1; 201 202 // find first free target fd 203 for (free_fd = 0; fd_map[free_fd] >= 0; ++free_fd) { 204 if (free_fd == MAX_FD) 205 panic("Process::open_fd: out of file descriptors!"); 206 } 207 208 fd_map[free_fd] = sim_fd; 209 210 return free_fd; 211} 212 213 214// look up simulator fd for given target fd 215int 216Process::sim_fd(int tgt_fd) 217{ 218 if (tgt_fd > MAX_FD) 219 return -1; 220 221 return fd_map[tgt_fd]; 222} 223 224 225 226// 227// need to declare these here since there is no concrete Process type 228// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 229// which is where these get declared for concrete types). 230// 231DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 232 233 234//////////////////////////////////////////////////////////////////////// 235// 236// LiveProcess member definitions 237// 238//////////////////////////////////////////////////////////////////////// 239 240 241static void 242copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 243 FunctionalMemory *memory) 244{ 245 for (int i = 0; i < strings.size(); ++i) { 246 memory->access(Write, array_ptr, &data_ptr, sizeof(Addr)); 247 memory->writeString(data_ptr, strings[i].c_str()); 248 array_ptr += sizeof(Addr); 249 data_ptr += strings[i].size() + 1; 250 } 251 // add NULL terminator 252 data_ptr = 0; 253 memory->access(Write, array_ptr, &data_ptr, sizeof(Addr)); 254} 255 256LiveProcess::LiveProcess(const string &nm, ObjectFile *objFile, 257 int stdin_fd, int stdout_fd, int stderr_fd, 258 vector<string> &argv, vector<string> &envp) 259 : Process(nm, stdin_fd, stdout_fd, stderr_fd) 260{ 261 prog_fname = argv[0]; 262 263 prog_entry = objFile->entryPoint(); 264 text_base = objFile->textBase(); 265 text_size = objFile->textSize(); 266 data_base = objFile->dataBase(); 267 data_size = objFile->dataSize() + objFile->bssSize(); 268 brk_point = RoundUp<uint64_t>(data_base + data_size, VMPageSize); 269 270 // load object file into target memory 271 objFile->loadSections(memory); 272 273 // load up symbols, if any... these may be used for debugging or 274 // profiling. 275 if (!debugSymbolTable) { 276 debugSymbolTable = new SymbolTable(); 277 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 278 !objFile->loadLocalSymbols(debugSymbolTable)) { 279 // didn't load any symbols 280 delete debugSymbolTable; 281 debugSymbolTable = NULL; 282 } 283 } 284 285 // Set up stack. On Alpha, stack goes below text section. This 286 // code should get moved to some architecture-specific spot. 287 stack_base = text_base - (409600+4096); 288 289 // Set up region for mmaps. Tru64 seems to start just above 0 and 290 // grow up from there. 291 mmap_start = mmap_end = 0x10000; 292 293 // Set pointer for next thread stack. Reserve 8M for main stack. 294 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 295 296 // Calculate how much space we need for arg & env arrays. 297 int argv_array_size = sizeof(Addr) * (argv.size() + 1); 298 int envp_array_size = sizeof(Addr) * (envp.size() + 1); 299 int arg_data_size = 0; 300 for (int i = 0; i < argv.size(); ++i) { 301 arg_data_size += argv[i].size() + 1; 302 } 303 int env_data_size = 0; 304 for (int i = 0; i < envp.size(); ++i) { 305 env_data_size += envp[i].size() + 1; 306 } 307 308 int space_needed = 309 argv_array_size + envp_array_size + arg_data_size + env_data_size; 310 // for SimpleScalar compatibility 311 if (space_needed < 16384) 312 space_needed = 16384; 313 314 // set bottom of stack 315 stack_min = stack_base - space_needed; 316 // align it 317 stack_min &= ~7; 318 stack_size = stack_base - stack_min; 319 320 // map out initial stack contents 321 Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 322 Addr envp_array_base = argv_array_base + argv_array_size; 323 Addr arg_data_base = envp_array_base + envp_array_size; 324 Addr env_data_base = arg_data_base + arg_data_size; 325 326 // write contents to stack 327 uint64_t argc = argv.size(); 328 memory->access(Write, stack_min, &argc, sizeof(uint64_t)); 329 330 copyStringArray(argv, argv_array_base, arg_data_base, memory); 331 copyStringArray(envp, envp_array_base, env_data_base, memory); 332 333 init_regs->intRegFile[ArgumentReg0] = argc; 334 init_regs->intRegFile[ArgumentReg1] = argv_array_base; 335 init_regs->intRegFile[StackPointerReg] = stack_min; 336 init_regs->intRegFile[GlobalPointerReg] = objFile->globalPointer(); 337 init_regs->pc = prog_entry; 338 init_regs->npc = prog_entry + sizeof(MachInst); 339} 340 341 342LiveProcess * 343LiveProcess::create(const string &nm, 344 int stdin_fd, int stdout_fd, int stderr_fd, 345 vector<string> &argv, vector<string> &envp) 346{ 347 LiveProcess *process = NULL; 348 ObjectFile *objFile = createObjectFile(argv[0]); 349 if (objFile == NULL) { 350 fatal("Can't load object file %s", argv[0]); 351 } 352 353 // check object type & set up syscall emulation pointer 354 if (objFile->getArch() == ObjectFile::Alpha) { 355 switch (objFile->getOpSys()) { 356 case ObjectFile::Tru64: 357 process = new AlphaTru64Process(nm, objFile, 358 stdin_fd, stdout_fd, stderr_fd, 359 argv, envp); 360 break; 361 362 case ObjectFile::Linux: 363 process = new AlphaLinuxProcess(nm, objFile, 364 stdin_fd, stdout_fd, stderr_fd, 365 argv, envp); 366 break; 367 368 default: 369 fatal("Unknown/unsupported operating system."); 370 } 371 } else { 372 fatal("Unknown object file architecture."); 373 } 374 375 delete objFile; 376 377 if (process == NULL) 378 fatal("Unknown error creating process object."); 379 380 return process; 381} 382 383 384BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 385 386 VectorParam<string> cmd; 387 Param<string> input; 388 Param<string> output; 389 VectorParam<string> env; 390 391END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 392 393 394BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 395 396 INIT_PARAM(cmd, "command line (executable plus arguments)"), 397 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 398 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 399 INIT_PARAM(env, "environment settings") 400 401END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 402 403 404CREATE_SIM_OBJECT(LiveProcess) 405{ 406 string in = input; 407 string out = output; 408 409 // initialize file descriptors to default: same as simulator 410 int stdin_fd, stdout_fd, stderr_fd; 411 412 if (in == "stdin" || in == "cin") 413 stdin_fd = STDIN_FILENO; 414 else 415 stdin_fd = Process::openInputFile(input); 416 417 if (out == "stdout" || out == "cout") 418 stdout_fd = STDOUT_FILENO; 419 else if (out == "stderr" || out == "cerr") 420 stdout_fd = STDERR_FILENO; 421 else 422 stdout_fd = Process::openOutputFile(out); 423 424 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 425 426 return LiveProcess::create(getInstanceName(), 427 stdin_fd, stdout_fd, stderr_fd, 428 cmd, env); 429} 430 431REGISTER_SIM_OBJECT("LiveProcess", LiveProcess) 432