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