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