1/* 2 * Copyright (c) 2001-2005 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 * Authors: Nathan Binkert 29 * Steve Reinhardt 30 * Ali Saidi 31 */ 32 33#include <unistd.h> 34#include <fcntl.h> 35 36#include <string> 37 38#include "base/intmath.hh" 39#include "base/loader/object_file.hh" 40#include "base/loader/symtab.hh" 41#include "base/statistics.hh" 42#include "config/full_system.hh" 43#include "cpu/thread_context.hh" 44#include "mem/page_table.hh" 45#include "mem/physical.hh" 46#include "mem/translating_port.hh" 47#include "sim/builder.hh" 48#include "sim/process.hh" 49#include "sim/stats.hh" 50#include "sim/syscall_emul.hh" 51#include "sim/system.hh" 52 53#include "arch/isa_specific.hh" 54#if THE_ISA == ALPHA_ISA 55#include "arch/alpha/linux/process.hh" 56#include "arch/alpha/tru64/process.hh" 57#elif THE_ISA == SPARC_ISA 58#include "arch/sparc/linux/process.hh" 59#include "arch/sparc/solaris/process.hh" 60#elif THE_ISA == MIPS_ISA 61#include "arch/mips/linux/process.hh" 62#else 63#error "THE_ISA not set" 64#endif 65 66 67using namespace std; 68using namespace TheISA; 69 70// 71// The purpose of this code is to fake the loader & syscall mechanism 72// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 73// mode when we do have an OS 74// 75#if FULL_SYSTEM 76#error "process.cc not compatible with FULL_SYSTEM" 77#endif 78 79// current number of allocated processes 80int num_processes = 0; 81 82Process::Process(const string &nm, 83 System *_system, 84 int stdin_fd, // initial I/O descriptors 85 int stdout_fd, 86 int stderr_fd) 87 : SimObject(nm), system(_system) 88{ 89 // initialize first 3 fds (stdin, stdout, stderr) 90 fd_map[STDIN_FILENO] = stdin_fd; 91 fd_map[STDOUT_FILENO] = stdout_fd; 92 fd_map[STDERR_FILENO] = stderr_fd; 93 94 // mark remaining fds as free 95 for (int i = 3; i <= MAX_FD; ++i) { 96 fd_map[i] = -1; 97 } 98 99 mmap_start = mmap_end = 0; 100 nxm_start = nxm_end = 0; 101 pTable = new PageTable(system); 102 // other parameters will be initialized when the program is loaded 103} 104 105 106void 107Process::regStats() 108{ 109 using namespace Stats; 110 111 num_syscalls 112 .name(name() + ".PROG:num_syscalls") 113 .desc("Number of system calls") 114 ; 115} 116 117// 118// static helper functions 119// 120int 121Process::openInputFile(const string &filename) 122{ 123 int fd = open(filename.c_str(), O_RDONLY); 124 125 if (fd == -1) { 126 perror(NULL); 127 cerr << "unable to open \"" << filename << "\" for reading\n"; 128 fatal("can't open input file"); 129 } 130 131 return fd; 132} 133 134 135int 136Process::openOutputFile(const string &filename) 137{ 138 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 139 140 if (fd == -1) { 141 perror(NULL); 142 cerr << "unable to open \"" << filename << "\" for writing\n"; 143 fatal("can't open output file"); 144 } 145 146 return fd; 147} 148 149 150int 151Process::registerThreadContext(ThreadContext *tc) 152{ 153 // add to list 154 int myIndex = threadContexts.size(); 155 threadContexts.push_back(tc); 156 157 // return CPU number to caller 158 return myIndex; 159} 160 161void 162Process::startup() 163{ 164 if (threadContexts.empty()) 165 fatal("Process %s is not associated with any CPUs!\n", name()); 166 167 // first thread context for this process... initialize & enable 168 ThreadContext *tc = threadContexts[0]; 169 170 // mark this context as active so it will start ticking. 171 tc->activate(0); 172 173 Port *mem_port; 174 mem_port = system->physmem->getPort("functional"); 175 initVirtMem = new TranslatingPort("process init port", pTable, true); 176 mem_port->setPeer(initVirtMem); 177 initVirtMem->setPeer(mem_port); 178} 179 180void 181Process::replaceThreadContext(ThreadContext *tc, int tcIndex) 182{ 183 if (tcIndex >= threadContexts.size()) { 184 panic("replaceThreadContext: bad tcIndex, %d >= %d\n", 185 tcIndex, threadContexts.size()); 186 } 187 188 threadContexts[tcIndex] = tc; 189} 190 191// map simulator fd sim_fd to target fd tgt_fd 192void 193Process::dup_fd(int sim_fd, int tgt_fd) 194{ 195 if (tgt_fd < 0 || tgt_fd > MAX_FD) 196 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 197 198 fd_map[tgt_fd] = sim_fd; 199} 200 201 202// generate new target fd for sim_fd 203int 204Process::alloc_fd(int sim_fd) 205{ 206 // in case open() returns an error, don't allocate a new fd 207 if (sim_fd == -1) 208 return -1; 209 210 // find first free target fd 211 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 212 if (fd_map[free_fd] == -1) { 213 fd_map[free_fd] = sim_fd; 214 return free_fd; 215 } 216 } 217 218 panic("Process::alloc_fd: out of file descriptors!"); 219} 220 221 222// free target fd (e.g., after close) 223void 224Process::free_fd(int tgt_fd) 225{ 226 if (fd_map[tgt_fd] == -1) 227 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 228 229 fd_map[tgt_fd] = -1; 230} 231 232 233// look up simulator fd for given target fd 234int 235Process::sim_fd(int tgt_fd) 236{ 237 if (tgt_fd > MAX_FD) 238 return -1; 239 240 return fd_map[tgt_fd]; 241} 242 243 244 245// 246// need to declare these here since there is no concrete Process type 247// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 248// which is where these get declared for concrete types). 249// 250DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 251 252 253//////////////////////////////////////////////////////////////////////// 254// 255// LiveProcess member definitions 256// 257//////////////////////////////////////////////////////////////////////// 258 259 260void 261copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 262 TranslatingPort* memPort) 263{ 264 Addr data_ptr_swap; 265 for (int i = 0; i < strings.size(); ++i) { 266 data_ptr_swap = htog(data_ptr); 267 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap, sizeof(Addr)); 268 memPort->writeString(data_ptr, strings[i].c_str()); 269 array_ptr += sizeof(Addr); 270 data_ptr += strings[i].size() + 1; 271 } 272 // add NULL terminator 273 data_ptr = 0; 274 275 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 276} 277 278LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 279 System *_system, 280 int stdin_fd, int stdout_fd, int stderr_fd, 281 vector<string> &_argv, vector<string> &_envp) 282 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 283 objFile(_objFile), argv(_argv), envp(_envp) 284{ 285 prog_fname = argv[0]; 286 287 // load up symbols, if any... these may be used for debugging or 288 // profiling. 289 if (!debugSymbolTable) { 290 debugSymbolTable = new SymbolTable(); 291 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 292 !objFile->loadLocalSymbols(debugSymbolTable)) { 293 // didn't load any symbols 294 delete debugSymbolTable; 295 debugSymbolTable = NULL; 296 } 297 } 298} 299 300void 301LiveProcess::argsInit(int intSize, int pageSize) 302{ 303 Process::startup(); 304 305 // load object file into target memory 306 objFile->loadSections(initVirtMem); 307 308 // Calculate how much space we need for arg & env arrays. 309 int argv_array_size = intSize * (argv.size() + 1); 310 int envp_array_size = intSize * (envp.size() + 1); 311 int arg_data_size = 0; 312 for (int i = 0; i < argv.size(); ++i) { 313 arg_data_size += argv[i].size() + 1; 314 } 315 int env_data_size = 0; 316 for (int i = 0; i < envp.size(); ++i) { 317 env_data_size += envp[i].size() + 1; 318 } 319 320 int space_needed = 321 argv_array_size + envp_array_size + arg_data_size + env_data_size; 322 // for SimpleScalar compatibility 323 if (space_needed < 16384) 324 space_needed = 16384; 325 326 // set bottom of stack 327 stack_min = stack_base - space_needed; 328 // align it
| 1/* 2 * Copyright (c) 2001-2005 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 * Authors: Nathan Binkert 29 * Steve Reinhardt 30 * Ali Saidi 31 */ 32 33#include <unistd.h> 34#include <fcntl.h> 35 36#include <string> 37 38#include "base/intmath.hh" 39#include "base/loader/object_file.hh" 40#include "base/loader/symtab.hh" 41#include "base/statistics.hh" 42#include "config/full_system.hh" 43#include "cpu/thread_context.hh" 44#include "mem/page_table.hh" 45#include "mem/physical.hh" 46#include "mem/translating_port.hh" 47#include "sim/builder.hh" 48#include "sim/process.hh" 49#include "sim/stats.hh" 50#include "sim/syscall_emul.hh" 51#include "sim/system.hh" 52 53#include "arch/isa_specific.hh" 54#if THE_ISA == ALPHA_ISA 55#include "arch/alpha/linux/process.hh" 56#include "arch/alpha/tru64/process.hh" 57#elif THE_ISA == SPARC_ISA 58#include "arch/sparc/linux/process.hh" 59#include "arch/sparc/solaris/process.hh" 60#elif THE_ISA == MIPS_ISA 61#include "arch/mips/linux/process.hh" 62#else 63#error "THE_ISA not set" 64#endif 65 66 67using namespace std; 68using namespace TheISA; 69 70// 71// The purpose of this code is to fake the loader & syscall mechanism 72// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 73// mode when we do have an OS 74// 75#if FULL_SYSTEM 76#error "process.cc not compatible with FULL_SYSTEM" 77#endif 78 79// current number of allocated processes 80int num_processes = 0; 81 82Process::Process(const string &nm, 83 System *_system, 84 int stdin_fd, // initial I/O descriptors 85 int stdout_fd, 86 int stderr_fd) 87 : SimObject(nm), system(_system) 88{ 89 // initialize first 3 fds (stdin, stdout, stderr) 90 fd_map[STDIN_FILENO] = stdin_fd; 91 fd_map[STDOUT_FILENO] = stdout_fd; 92 fd_map[STDERR_FILENO] = stderr_fd; 93 94 // mark remaining fds as free 95 for (int i = 3; i <= MAX_FD; ++i) { 96 fd_map[i] = -1; 97 } 98 99 mmap_start = mmap_end = 0; 100 nxm_start = nxm_end = 0; 101 pTable = new PageTable(system); 102 // other parameters will be initialized when the program is loaded 103} 104 105 106void 107Process::regStats() 108{ 109 using namespace Stats; 110 111 num_syscalls 112 .name(name() + ".PROG:num_syscalls") 113 .desc("Number of system calls") 114 ; 115} 116 117// 118// static helper functions 119// 120int 121Process::openInputFile(const string &filename) 122{ 123 int fd = open(filename.c_str(), O_RDONLY); 124 125 if (fd == -1) { 126 perror(NULL); 127 cerr << "unable to open \"" << filename << "\" for reading\n"; 128 fatal("can't open input file"); 129 } 130 131 return fd; 132} 133 134 135int 136Process::openOutputFile(const string &filename) 137{ 138 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 139 140 if (fd == -1) { 141 perror(NULL); 142 cerr << "unable to open \"" << filename << "\" for writing\n"; 143 fatal("can't open output file"); 144 } 145 146 return fd; 147} 148 149 150int 151Process::registerThreadContext(ThreadContext *tc) 152{ 153 // add to list 154 int myIndex = threadContexts.size(); 155 threadContexts.push_back(tc); 156 157 // return CPU number to caller 158 return myIndex; 159} 160 161void 162Process::startup() 163{ 164 if (threadContexts.empty()) 165 fatal("Process %s is not associated with any CPUs!\n", name()); 166 167 // first thread context for this process... initialize & enable 168 ThreadContext *tc = threadContexts[0]; 169 170 // mark this context as active so it will start ticking. 171 tc->activate(0); 172 173 Port *mem_port; 174 mem_port = system->physmem->getPort("functional"); 175 initVirtMem = new TranslatingPort("process init port", pTable, true); 176 mem_port->setPeer(initVirtMem); 177 initVirtMem->setPeer(mem_port); 178} 179 180void 181Process::replaceThreadContext(ThreadContext *tc, int tcIndex) 182{ 183 if (tcIndex >= threadContexts.size()) { 184 panic("replaceThreadContext: bad tcIndex, %d >= %d\n", 185 tcIndex, threadContexts.size()); 186 } 187 188 threadContexts[tcIndex] = tc; 189} 190 191// map simulator fd sim_fd to target fd tgt_fd 192void 193Process::dup_fd(int sim_fd, int tgt_fd) 194{ 195 if (tgt_fd < 0 || tgt_fd > MAX_FD) 196 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 197 198 fd_map[tgt_fd] = sim_fd; 199} 200 201 202// generate new target fd for sim_fd 203int 204Process::alloc_fd(int sim_fd) 205{ 206 // in case open() returns an error, don't allocate a new fd 207 if (sim_fd == -1) 208 return -1; 209 210 // find first free target fd 211 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 212 if (fd_map[free_fd] == -1) { 213 fd_map[free_fd] = sim_fd; 214 return free_fd; 215 } 216 } 217 218 panic("Process::alloc_fd: out of file descriptors!"); 219} 220 221 222// free target fd (e.g., after close) 223void 224Process::free_fd(int tgt_fd) 225{ 226 if (fd_map[tgt_fd] == -1) 227 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 228 229 fd_map[tgt_fd] = -1; 230} 231 232 233// look up simulator fd for given target fd 234int 235Process::sim_fd(int tgt_fd) 236{ 237 if (tgt_fd > MAX_FD) 238 return -1; 239 240 return fd_map[tgt_fd]; 241} 242 243 244 245// 246// need to declare these here since there is no concrete Process type 247// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 248// which is where these get declared for concrete types). 249// 250DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 251 252 253//////////////////////////////////////////////////////////////////////// 254// 255// LiveProcess member definitions 256// 257//////////////////////////////////////////////////////////////////////// 258 259 260void 261copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 262 TranslatingPort* memPort) 263{ 264 Addr data_ptr_swap; 265 for (int i = 0; i < strings.size(); ++i) { 266 data_ptr_swap = htog(data_ptr); 267 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap, sizeof(Addr)); 268 memPort->writeString(data_ptr, strings[i].c_str()); 269 array_ptr += sizeof(Addr); 270 data_ptr += strings[i].size() + 1; 271 } 272 // add NULL terminator 273 data_ptr = 0; 274 275 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 276} 277 278LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 279 System *_system, 280 int stdin_fd, int stdout_fd, int stderr_fd, 281 vector<string> &_argv, vector<string> &_envp) 282 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 283 objFile(_objFile), argv(_argv), envp(_envp) 284{ 285 prog_fname = argv[0]; 286 287 // load up symbols, if any... these may be used for debugging or 288 // profiling. 289 if (!debugSymbolTable) { 290 debugSymbolTable = new SymbolTable(); 291 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 292 !objFile->loadLocalSymbols(debugSymbolTable)) { 293 // didn't load any symbols 294 delete debugSymbolTable; 295 debugSymbolTable = NULL; 296 } 297 } 298} 299 300void 301LiveProcess::argsInit(int intSize, int pageSize) 302{ 303 Process::startup(); 304 305 // load object file into target memory 306 objFile->loadSections(initVirtMem); 307 308 // Calculate how much space we need for arg & env arrays. 309 int argv_array_size = intSize * (argv.size() + 1); 310 int envp_array_size = intSize * (envp.size() + 1); 311 int arg_data_size = 0; 312 for (int i = 0; i < argv.size(); ++i) { 313 arg_data_size += argv[i].size() + 1; 314 } 315 int env_data_size = 0; 316 for (int i = 0; i < envp.size(); ++i) { 317 env_data_size += envp[i].size() + 1; 318 } 319 320 int space_needed = 321 argv_array_size + envp_array_size + arg_data_size + env_data_size; 322 // for SimpleScalar compatibility 323 if (space_needed < 16384) 324 space_needed = 16384; 325 326 // set bottom of stack 327 stack_min = stack_base - space_needed; 328 // align it
|
329 stack_min &= ~(intSize-1);
| 329 stack_min = roundDown(stack_min, pageSize);
|
330 stack_size = stack_base - stack_min; 331 // map memory
| 330 stack_size = stack_base - stack_min; 331 // map memory
|
332 pTable->allocate(roundDown(stack_min, pageSize), 333 roundUp(stack_size, pageSize));
| 332 pTable->allocate(stack_min, roundUp(stack_size, pageSize));
|
334 335 // map out initial stack contents 336 Addr argv_array_base = stack_min + intSize; // room for argc 337 Addr envp_array_base = argv_array_base + argv_array_size; 338 Addr arg_data_base = envp_array_base + envp_array_size; 339 Addr env_data_base = arg_data_base + arg_data_size; 340 341 // write contents to stack 342 uint64_t argc = argv.size(); 343 if (intSize == 8) 344 argc = htog((uint64_t)argc); 345 else if (intSize == 4) 346 argc = htog((uint32_t)argc); 347 else 348 panic("Unknown int size"); 349 350 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 351 352 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 353 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 354 355 threadContexts[0]->setIntReg(ArgumentReg0, argc); 356 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 357 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 358 359 Addr prog_entry = objFile->entryPoint(); 360 threadContexts[0]->setPC(prog_entry); 361 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 362 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 363 364 num_processes++; 365} 366 367void 368LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 369{ 370 num_syscalls++; 371 372 SyscallDesc *desc = getDesc(callnum); 373 if (desc == NULL) 374 fatal("Syscall %d out of range", callnum); 375 376 desc->doSyscall(callnum, this, tc); 377} 378 379LiveProcess * 380LiveProcess::create(const std::string &nm, System *system, int stdin_fd, 381 int stdout_fd, int stderr_fd, std::string executable, 382 std::vector<std::string> &argv, 383 std::vector<std::string> &envp) 384{ 385 LiveProcess *process = NULL; 386 387 ObjectFile *objFile = createObjectFile(executable); 388 if (objFile == NULL) { 389 fatal("Can't load object file %s", executable); 390 } 391 392#if THE_ISA == ALPHA_ISA 393 if (objFile->getArch() != ObjectFile::Alpha) 394 fatal("Object file architecture does not match compiled ISA (Alpha)."); 395 switch (objFile->getOpSys()) { 396 case ObjectFile::Tru64: 397 process = new AlphaTru64Process(nm, objFile, system, 398 stdin_fd, stdout_fd, stderr_fd, 399 argv, envp); 400 break; 401 402 case ObjectFile::Linux: 403 process = new AlphaLinuxProcess(nm, objFile, system, 404 stdin_fd, stdout_fd, stderr_fd, 405 argv, envp); 406 break; 407 408 default: 409 fatal("Unknown/unsupported operating system."); 410 } 411#elif THE_ISA == SPARC_ISA 412 if (objFile->getArch() != ObjectFile::SPARC) 413 fatal("Object file architecture does not match compiled ISA (SPARC)."); 414 switch (objFile->getOpSys()) { 415 case ObjectFile::Linux: 416 process = new SparcLinuxProcess(nm, objFile, system, 417 stdin_fd, stdout_fd, stderr_fd, 418 argv, envp); 419 break; 420 421 422 case ObjectFile::Solaris: 423 process = new SparcSolarisProcess(nm, objFile, system, 424 stdin_fd, stdout_fd, stderr_fd, 425 argv, envp); 426 break; 427 default: 428 fatal("Unknown/unsupported operating system."); 429 } 430#elif THE_ISA == MIPS_ISA 431 if (objFile->getArch() != ObjectFile::Mips) 432 fatal("Object file architecture does not match compiled ISA (MIPS)."); 433 switch (objFile->getOpSys()) { 434 case ObjectFile::Linux: 435 process = new MipsLinuxProcess(nm, objFile, system, 436 stdin_fd, stdout_fd, stderr_fd, 437 argv, envp); 438 break; 439 440 default: 441 fatal("Unknown/unsupported operating system."); 442 } 443#else 444#error "THE_ISA not set" 445#endif 446 447 448 if (process == NULL) 449 fatal("Unknown error creating process object."); 450 return process; 451} 452 453 454BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 455 456 VectorParam<string> cmd; 457 Param<string> executable; 458 Param<string> input; 459 Param<string> output; 460 VectorParam<string> env; 461 SimObjectParam<System *> system; 462 463END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 464 465 466BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 467 468 INIT_PARAM(cmd, "command line (executable plus arguments)"), 469 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 470 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 471 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 472 INIT_PARAM(env, "environment settings"), 473 INIT_PARAM(system, "system") 474 475END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 476 477 478CREATE_SIM_OBJECT(LiveProcess) 479{ 480 string in = input; 481 string out = output; 482 483 // initialize file descriptors to default: same as simulator 484 int stdin_fd, stdout_fd, stderr_fd; 485 486 if (in == "stdin" || in == "cin") 487 stdin_fd = STDIN_FILENO; 488 else 489 stdin_fd = Process::openInputFile(input); 490 491 if (out == "stdout" || out == "cout") 492 stdout_fd = STDOUT_FILENO; 493 else if (out == "stderr" || out == "cerr") 494 stdout_fd = STDERR_FILENO; 495 else 496 stdout_fd = Process::openOutputFile(out); 497 498 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 499 500 return LiveProcess::create(getInstanceName(), system, 501 stdin_fd, stdout_fd, stderr_fd, 502 (string)executable == "" ? cmd[0] : executable, 503 cmd, env); 504} 505 506 507REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
| 333 334 // map out initial stack contents 335 Addr argv_array_base = stack_min + intSize; // room for argc 336 Addr envp_array_base = argv_array_base + argv_array_size; 337 Addr arg_data_base = envp_array_base + envp_array_size; 338 Addr env_data_base = arg_data_base + arg_data_size; 339 340 // write contents to stack 341 uint64_t argc = argv.size(); 342 if (intSize == 8) 343 argc = htog((uint64_t)argc); 344 else if (intSize == 4) 345 argc = htog((uint32_t)argc); 346 else 347 panic("Unknown int size"); 348 349 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 350 351 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 352 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 353 354 threadContexts[0]->setIntReg(ArgumentReg0, argc); 355 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 356 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 357 358 Addr prog_entry = objFile->entryPoint(); 359 threadContexts[0]->setPC(prog_entry); 360 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 361 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 362 363 num_processes++; 364} 365 366void 367LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 368{ 369 num_syscalls++; 370 371 SyscallDesc *desc = getDesc(callnum); 372 if (desc == NULL) 373 fatal("Syscall %d out of range", callnum); 374 375 desc->doSyscall(callnum, this, tc); 376} 377 378LiveProcess * 379LiveProcess::create(const std::string &nm, System *system, int stdin_fd, 380 int stdout_fd, int stderr_fd, std::string executable, 381 std::vector<std::string> &argv, 382 std::vector<std::string> &envp) 383{ 384 LiveProcess *process = NULL; 385 386 ObjectFile *objFile = createObjectFile(executable); 387 if (objFile == NULL) { 388 fatal("Can't load object file %s", executable); 389 } 390 391#if THE_ISA == ALPHA_ISA 392 if (objFile->getArch() != ObjectFile::Alpha) 393 fatal("Object file architecture does not match compiled ISA (Alpha)."); 394 switch (objFile->getOpSys()) { 395 case ObjectFile::Tru64: 396 process = new AlphaTru64Process(nm, objFile, system, 397 stdin_fd, stdout_fd, stderr_fd, 398 argv, envp); 399 break; 400 401 case ObjectFile::Linux: 402 process = new AlphaLinuxProcess(nm, objFile, system, 403 stdin_fd, stdout_fd, stderr_fd, 404 argv, envp); 405 break; 406 407 default: 408 fatal("Unknown/unsupported operating system."); 409 } 410#elif THE_ISA == SPARC_ISA 411 if (objFile->getArch() != ObjectFile::SPARC) 412 fatal("Object file architecture does not match compiled ISA (SPARC)."); 413 switch (objFile->getOpSys()) { 414 case ObjectFile::Linux: 415 process = new SparcLinuxProcess(nm, objFile, system, 416 stdin_fd, stdout_fd, stderr_fd, 417 argv, envp); 418 break; 419 420 421 case ObjectFile::Solaris: 422 process = new SparcSolarisProcess(nm, objFile, system, 423 stdin_fd, stdout_fd, stderr_fd, 424 argv, envp); 425 break; 426 default: 427 fatal("Unknown/unsupported operating system."); 428 } 429#elif THE_ISA == MIPS_ISA 430 if (objFile->getArch() != ObjectFile::Mips) 431 fatal("Object file architecture does not match compiled ISA (MIPS)."); 432 switch (objFile->getOpSys()) { 433 case ObjectFile::Linux: 434 process = new MipsLinuxProcess(nm, objFile, system, 435 stdin_fd, stdout_fd, stderr_fd, 436 argv, envp); 437 break; 438 439 default: 440 fatal("Unknown/unsupported operating system."); 441 } 442#else 443#error "THE_ISA not set" 444#endif 445 446 447 if (process == NULL) 448 fatal("Unknown error creating process object."); 449 return process; 450} 451 452 453BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 454 455 VectorParam<string> cmd; 456 Param<string> executable; 457 Param<string> input; 458 Param<string> output; 459 VectorParam<string> env; 460 SimObjectParam<System *> system; 461 462END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 463 464 465BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 466 467 INIT_PARAM(cmd, "command line (executable plus arguments)"), 468 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 469 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 470 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 471 INIT_PARAM(env, "environment settings"), 472 INIT_PARAM(system, "system") 473 474END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 475 476 477CREATE_SIM_OBJECT(LiveProcess) 478{ 479 string in = input; 480 string out = output; 481 482 // initialize file descriptors to default: same as simulator 483 int stdin_fd, stdout_fd, stderr_fd; 484 485 if (in == "stdin" || in == "cin") 486 stdin_fd = STDIN_FILENO; 487 else 488 stdin_fd = Process::openInputFile(input); 489 490 if (out == "stdout" || out == "cout") 491 stdout_fd = STDOUT_FILENO; 492 else if (out == "stderr" || out == "cerr") 493 stdout_fd = STDERR_FILENO; 494 else 495 stdout_fd = Process::openOutputFile(out); 496 497 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 498 499 return LiveProcess::create(getInstanceName(), system, 500 stdin_fd, stdout_fd, stderr_fd, 501 (string)executable == "" ? cmd[0] : executable, 502 cmd, env); 503} 504 505 506REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
|