process.cc revision 56
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#include <stdio.h> 32 33#include <string> 34 35#include "mem/functional_mem/main_memory.hh" 36#include "sim/prog.hh" 37 38#include "eio/eio.hh" 39#include "base/intmath.hh" 40#include "cpu/full_cpu/thread.hh" 41#include "sim/fake_syscall.hh" 42#include "base/loader/object_file.hh" 43#include "cpu/exec_context.hh" 44#include "cpu/full_cpu/smt.hh" 45 46#include "base/statistics.hh" 47#include "sim/sim_stats.hh" 48 49using namespace std; 50 51// 52// The purpose of this code is to fake the loader & syscall mechanism 53// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 54// mode when we do have an OS 55// 56#ifdef FULL_SYSTEM 57#error "prog.cc not compatible with FULL_SYSTEM" 58#endif 59 60// max allowable number of processes: should be no real cost to 61// cranking this up if necessary 62const int MAX_PROCESSES = 8; 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 numCpus = 0; 90 91 num_syscalls = 0; 92 93 // other parameters will be initialized when the program is loaded 94} 95 96void 97Process::regStats() 98{ 99 using namespace Statistics; 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 140void 141Process::registerExecContext(ExecContext *ec) 142{ 143 if (execContexts.empty()) { 144 // first exec context for this process... initialize & enable 145 146 // copy process's initial regs struct 147 ec->regs = *init_regs; 148 149 // mark this context as active 150 ec->setStatus(ExecContext::Active); 151 } 152 else { 153 ec->setStatus(ExecContext::Unallocated); 154 } 155 156 // add to list 157 execContexts.push_back(ec); 158 159 // increment available CPU count 160 ++numCpus; 161} 162 163 164// map simulator fd sim_fd to target fd tgt_fd 165void 166Process::dup_fd(int sim_fd, int tgt_fd) 167{ 168 if (tgt_fd < 0 || tgt_fd > MAX_FD) 169 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 170 171 fd_map[tgt_fd] = sim_fd; 172} 173 174 175// generate new target fd for sim_fd 176int 177Process::open_fd(int sim_fd) 178{ 179 int free_fd; 180 181 // in case open() returns an error, don't allocate a new fd 182 if (sim_fd == -1) 183 return -1; 184 185 // find first free target fd 186 for (free_fd = 0; fd_map[free_fd] >= 0; ++free_fd) { 187 if (free_fd == MAX_FD) 188 panic("Process::open_fd: out of file descriptors!"); 189 } 190 191 fd_map[free_fd] = sim_fd; 192 193 return free_fd; 194} 195 196 197// look up simulator fd for given target fd 198int 199Process::sim_fd(int tgt_fd) 200{ 201 if (tgt_fd > MAX_FD) 202 return -1; 203 204 return fd_map[tgt_fd]; 205} 206 207 208 209// 210// need to declare these here since there is no concrete Process type 211// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 212// which is where these get declared for concrete types). 213// 214DEFINE_SIM_OBJECT_CLASS_NAME("Process object", Process) 215 216 217//////////////////////////////////////////////////////////////////////// 218// 219// LiveProcess member definitions 220// 221//////////////////////////////////////////////////////////////////////// 222 223 224static void 225copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 226 FunctionalMemory *memory) 227{ 228 for (int i = 0; i < strings.size(); ++i) { 229 memory->access(Write, array_ptr, &data_ptr, sizeof(Addr)); 230 memory->writeString(data_ptr, strings[i].c_str()); 231 array_ptr += sizeof(Addr); 232 data_ptr += strings[i].size() + 1; 233 } 234 // add NULL terminator 235 data_ptr = 0; 236 memory->access(Write, array_ptr, &data_ptr, sizeof(Addr)); 237} 238 239LiveProcess::LiveProcess(const string &name, 240 int stdin_fd, int stdout_fd, int stderr_fd, 241 vector<string> &argv, vector<string> &envp) 242 : Process(name, stdin_fd, stdout_fd, stderr_fd) 243{ 244 prog_fname = argv[0]; 245 ObjectFile *objFile = createObjectFile(prog_fname); 246 if (objFile == NULL) { 247 fatal("Can't load object file %s", prog_fname); 248 } 249 250 prog_entry = objFile->entryPoint(); 251 text_base = objFile->textBase(); 252 text_size = objFile->textSize(); 253 data_base = objFile->dataBase(); 254 data_size = objFile->dataSize() + objFile->bssSize(); 255 brk_point = RoundUp<uint64_t>(data_base + data_size, VMPageSize); 256 257 // load object file into target memory 258 objFile->loadSections(memory); 259 260 // Set up stack. On Alpha, stack goes below text section. This 261 // code should get moved to some architecture-specific spot. 262 stack_base = text_base - (409600+4096); 263 264 // Set pointer for next thread stack. Reserve 8M for main stack. 265 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 266 267 // Calculate how much space we need for arg & env arrays. 268 int argv_array_size = sizeof(Addr) * (argv.size() + 1); 269 int envp_array_size = sizeof(Addr) * (envp.size() + 1); 270 int arg_data_size = 0; 271 for (int i = 0; i < argv.size(); ++i) { 272 arg_data_size += argv[i].size() + 1; 273 } 274 int env_data_size = 0; 275 for (int i = 0; i < envp.size(); ++i) { 276 env_data_size += envp[i].size() + 1; 277 } 278 279 int space_needed = 280 argv_array_size + envp_array_size + arg_data_size + env_data_size; 281 // for SimpleScalar compatibility 282 if (space_needed < 16384) 283 space_needed = 16384; 284 285 // set bottom of stack 286 stack_min = stack_base - space_needed; 287 // align it 288 stack_min &= ~7; 289 stack_size = stack_base - stack_min; 290 291 // map out initial stack contents 292 Addr argv_array_base = stack_min + sizeof(uint64_t); // room for argc 293 Addr envp_array_base = argv_array_base + argv_array_size; 294 Addr arg_data_base = envp_array_base + envp_array_size; 295 Addr env_data_base = arg_data_base + arg_data_size; 296 297 // write contents to stack 298 uint64_t argc = argv.size(); 299 memory->access(Write, stack_min, &argc, sizeof(uint64_t)); 300 301 copyStringArray(argv, argv_array_base, arg_data_base, memory); 302 copyStringArray(envp, envp_array_base, env_data_base, memory); 303 304 init_regs->intRegFile[ArgumentReg0] = argc; 305 init_regs->intRegFile[ArgumentReg1] = argv_array_base; 306 init_regs->intRegFile[StackPointerReg] = stack_min; 307 init_regs->intRegFile[GlobalPointerReg] = objFile->globalPointer(); 308 init_regs->pc = prog_entry; 309 init_regs->npc = prog_entry + sizeof(MachInst); 310} 311 312 313void 314LiveProcess::syscall(ExecContext *xc) 315{ 316 num_syscalls++; 317 318 fake_syscall(this, xc); 319} 320 321 322BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 323 324 VectorParam<string> cmd; 325 Param<string> input; 326 Param<string> output; 327 VectorParam<string> env; 328 329END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 330 331 332BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 333 334 INIT_PARAM(cmd, "command line (executable plus arguments)"), 335 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 336 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 337 INIT_PARAM(env, "environment settings") 338 339END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 340 341 342CREATE_SIM_OBJECT(LiveProcess) 343{ 344 // initialize file descriptors to default: same as simulator 345 int stdin_fd = input.isValid() ? Process::openInputFile(input) : 0; 346 int stdout_fd = output.isValid() ? Process::openOutputFile(output) : 1; 347 int stderr_fd = output.isValid() ? stdout_fd : 2; 348 349 // dummy for default env 350 vector<string> null_vec; 351 352 // We do this with "temp" because of the bogus compiler warning 353 // you get with g++ 2.95 -O if you just "return new LiveProcess(..." 354 LiveProcess *temp = new LiveProcess(getInstanceName(), 355 stdin_fd, stdout_fd, stderr_fd, 356 cmd, 357 env.isValid() ? env : null_vec); 358 359 return temp; 360} 361 362 363REGISTER_SIM_OBJECT("LiveProcess", LiveProcess) 364