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
| 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
|
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 uint64_t _uid, uint64_t _euid, 283 uint64_t _gid, uint64_t _egid, 284 uint64_t _pid, uint64_t _ppid) 285 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 286 objFile(_objFile), argv(_argv), envp(_envp) 287{ 288 __uid = _uid; 289 __euid = _euid; 290 __gid = _gid; 291 __egid = _egid; 292 __pid = _pid; 293 __ppid = _ppid; 294 295 prog_fname = argv[0]; 296 297 // load up symbols, if any... these may be used for debugging or 298 // profiling. 299 if (!debugSymbolTable) { 300 debugSymbolTable = new SymbolTable(); 301 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 302 !objFile->loadLocalSymbols(debugSymbolTable)) { 303 // didn't load any symbols 304 delete debugSymbolTable; 305 debugSymbolTable = NULL; 306 } 307 } 308} 309 310void 311LiveProcess::argsInit(int intSize, int pageSize) 312{ 313 Process::startup(); 314 315 // load object file into target memory 316 objFile->loadSections(initVirtMem); 317 318 // Calculate how much space we need for arg & env arrays. 319 int argv_array_size = intSize * (argv.size() + 1); 320 int envp_array_size = intSize * (envp.size() + 1); 321 int arg_data_size = 0; 322 for (int i = 0; i < argv.size(); ++i) { 323 arg_data_size += argv[i].size() + 1; 324 } 325 int env_data_size = 0; 326 for (int i = 0; i < envp.size(); ++i) { 327 env_data_size += envp[i].size() + 1; 328 } 329 330 int space_needed = 331 argv_array_size + envp_array_size + arg_data_size + env_data_size; 332 if (space_needed < 32*1024) 333 space_needed = 32*1024; 334 335 // set bottom of stack 336 stack_min = stack_base - space_needed; 337 // align it 338 stack_min = roundDown(stack_min, pageSize); 339 stack_size = stack_base - stack_min; 340 // map memory 341 pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 342 343 // map out initial stack contents 344 Addr argv_array_base = stack_min + intSize; // room for argc 345 Addr envp_array_base = argv_array_base + argv_array_size; 346 Addr arg_data_base = envp_array_base + envp_array_size; 347 Addr env_data_base = arg_data_base + arg_data_size; 348 349 // write contents to stack 350 uint64_t argc = argv.size(); 351 if (intSize == 8) 352 argc = htog((uint64_t)argc); 353 else if (intSize == 4) 354 argc = htog((uint32_t)argc); 355 else 356 panic("Unknown int size"); 357 358 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 359 360 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 361 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 362 363 threadContexts[0]->setIntReg(ArgumentReg0, argc); 364 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 365 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 366 367 Addr prog_entry = objFile->entryPoint(); 368 threadContexts[0]->setPC(prog_entry); 369 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 370 371#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 372 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 373#endif 374 375 num_processes++; 376} 377 378void 379LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 380{ 381 num_syscalls++; 382 383 SyscallDesc *desc = getDesc(callnum); 384 if (desc == NULL) 385 fatal("Syscall %d out of range", callnum); 386 387 desc->doSyscall(callnum, this, tc); 388} 389 390LiveProcess * 391LiveProcess::create(const std::string &nm, System *system, int stdin_fd, 392 int stdout_fd, int stderr_fd, std::string executable, 393 std::vector<std::string> &argv, 394 std::vector<std::string> &envp, 395 uint64_t _uid, uint64_t _euid, 396 uint64_t _gid, uint64_t _egid, 397 uint64_t _pid, uint64_t _ppid) 398{ 399 LiveProcess *process = NULL; 400 401 ObjectFile *objFile = createObjectFile(executable); 402 if (objFile == NULL) { 403 fatal("Can't load object file %s", executable); 404 } 405 406#if THE_ISA == ALPHA_ISA 407 if (objFile->getArch() != ObjectFile::Alpha) 408 fatal("Object file architecture does not match compiled ISA (Alpha)."); 409 switch (objFile->getOpSys()) { 410 case ObjectFile::Tru64: 411 process = new AlphaTru64Process(nm, objFile, system, 412 stdin_fd, stdout_fd, stderr_fd, 413 argv, envp, 414 _uid, _euid, _gid, _egid, _pid, _ppid); 415 break; 416 417 case ObjectFile::Linux: 418 process = new AlphaLinuxProcess(nm, objFile, system, 419 stdin_fd, stdout_fd, stderr_fd, 420 argv, envp, 421 _uid, _euid, _gid, _egid, _pid, _ppid); 422 break; 423 424 default: 425 fatal("Unknown/unsupported operating system."); 426 } 427#elif THE_ISA == SPARC_ISA 428 if (objFile->getArch() != ObjectFile::SPARC) 429 fatal("Object file architecture does not match compiled ISA (SPARC)."); 430 switch (objFile->getOpSys()) { 431 case ObjectFile::Linux: 432 process = new SparcLinuxProcess(nm, objFile, system, 433 stdin_fd, stdout_fd, stderr_fd, 434 argv, envp, 435 _uid, _euid, _gid, _egid, _pid, _ppid); 436 break; 437 438 439 case ObjectFile::Solaris: 440 process = new SparcSolarisProcess(nm, objFile, system, 441 stdin_fd, stdout_fd, stderr_fd, 442 argv, envp, 443 _uid, _euid, _gid, _egid, _pid, _ppid); 444 break; 445 default: 446 fatal("Unknown/unsupported operating system."); 447 } 448#elif THE_ISA == MIPS_ISA 449 if (objFile->getArch() != ObjectFile::Mips) 450 fatal("Object file architecture does not match compiled ISA (MIPS)."); 451 switch (objFile->getOpSys()) { 452 case ObjectFile::Linux: 453 process = new MipsLinuxProcess(nm, objFile, system, 454 stdin_fd, stdout_fd, stderr_fd, 455 argv, envp, 456 _uid, _euid, _gid, _egid, _pid, _ppid); 457 break; 458 459 default: 460 fatal("Unknown/unsupported operating system."); 461 } 462#else 463#error "THE_ISA not set" 464#endif 465 466 467 if (process == NULL) 468 fatal("Unknown error creating process object."); 469 return process; 470} 471 472 473BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 474 475 VectorParam<string> cmd; 476 Param<string> executable; 477 Param<string> input; 478 Param<string> output; 479 VectorParam<string> env; 480 SimObjectParam<System *> system; 481 Param<uint64_t> uid; 482 Param<uint64_t> euid; 483 Param<uint64_t> gid; 484 Param<uint64_t> egid; 485 Param<uint64_t> pid; 486 Param<uint64_t> ppid; 487 488END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 489 490 491BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 492 493 INIT_PARAM(cmd, "command line (executable plus arguments)"), 494 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 495 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 496 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 497 INIT_PARAM(env, "environment settings"), 498 INIT_PARAM(system, "system"), 499 INIT_PARAM(uid, "user id"), 500 INIT_PARAM(euid, "effective user id"), 501 INIT_PARAM(gid, "group id"), 502 INIT_PARAM(egid, "effective group id"), 503 INIT_PARAM(pid, "process id"), 504 INIT_PARAM(ppid, "parent process id") 505 506END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 507 508 509CREATE_SIM_OBJECT(LiveProcess) 510{ 511 string in = input; 512 string out = output; 513 514 // initialize file descriptors to default: same as simulator 515 int stdin_fd, stdout_fd, stderr_fd; 516 517 if (in == "stdin" || in == "cin") 518 stdin_fd = STDIN_FILENO; 519 else 520 stdin_fd = Process::openInputFile(input); 521 522 if (out == "stdout" || out == "cout") 523 stdout_fd = STDOUT_FILENO; 524 else if (out == "stderr" || out == "cerr") 525 stdout_fd = STDERR_FILENO; 526 else 527 stdout_fd = Process::openOutputFile(out); 528 529 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 530 531 return LiveProcess::create(getInstanceName(), system, 532 stdin_fd, stdout_fd, stderr_fd, 533 (string)executable == "" ? cmd[0] : executable, 534 cmd, env, 535 uid, euid, gid, egid, pid, ppid); 536} 537 538 539REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
| 280// 281// need to declare these here since there is no concrete Process type 282// that can be constructed (i.e., no REGISTER_SIM_OBJECT() macro call, 283// which is where these get declared for concrete types). 284// 285DEFINE_SIM_OBJECT_CLASS_NAME("Process", Process) 286 287 288//////////////////////////////////////////////////////////////////////// 289// 290// LiveProcess member definitions 291// 292//////////////////////////////////////////////////////////////////////// 293 294 295void 296copyStringArray(vector<string> &strings, Addr array_ptr, Addr data_ptr, 297 TranslatingPort* memPort) 298{ 299 Addr data_ptr_swap; 300 for (int i = 0; i < strings.size(); ++i) { 301 data_ptr_swap = htog(data_ptr); 302 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr_swap, sizeof(Addr)); 303 memPort->writeString(data_ptr, strings[i].c_str()); 304 array_ptr += sizeof(Addr); 305 data_ptr += strings[i].size() + 1; 306 } 307 // add NULL terminator 308 data_ptr = 0; 309 310 memPort->writeBlob(array_ptr, (uint8_t*)&data_ptr, sizeof(Addr)); 311} 312 313LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 314 System *_system, 315 int stdin_fd, int stdout_fd, int stderr_fd, 316 vector<string> &_argv, vector<string> &_envp, 317 uint64_t _uid, uint64_t _euid, 318 uint64_t _gid, uint64_t _egid, 319 uint64_t _pid, uint64_t _ppid) 320 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 321 objFile(_objFile), argv(_argv), envp(_envp) 322{ 323 __uid = _uid; 324 __euid = _euid; 325 __gid = _gid; 326 __egid = _egid; 327 __pid = _pid; 328 __ppid = _ppid; 329 330 prog_fname = argv[0]; 331 332 // load up symbols, if any... these may be used for debugging or 333 // profiling. 334 if (!debugSymbolTable) { 335 debugSymbolTable = new SymbolTable(); 336 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 337 !objFile->loadLocalSymbols(debugSymbolTable)) { 338 // didn't load any symbols 339 delete debugSymbolTable; 340 debugSymbolTable = NULL; 341 } 342 } 343} 344 345void 346LiveProcess::argsInit(int intSize, int pageSize) 347{ 348 Process::startup(); 349 350 // load object file into target memory 351 objFile->loadSections(initVirtMem); 352 353 // Calculate how much space we need for arg & env arrays. 354 int argv_array_size = intSize * (argv.size() + 1); 355 int envp_array_size = intSize * (envp.size() + 1); 356 int arg_data_size = 0; 357 for (int i = 0; i < argv.size(); ++i) { 358 arg_data_size += argv[i].size() + 1; 359 } 360 int env_data_size = 0; 361 for (int i = 0; i < envp.size(); ++i) { 362 env_data_size += envp[i].size() + 1; 363 } 364 365 int space_needed = 366 argv_array_size + envp_array_size + arg_data_size + env_data_size; 367 if (space_needed < 32*1024) 368 space_needed = 32*1024; 369 370 // set bottom of stack 371 stack_min = stack_base - space_needed; 372 // align it 373 stack_min = roundDown(stack_min, pageSize); 374 stack_size = stack_base - stack_min; 375 // map memory 376 pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 377 378 // map out initial stack contents 379 Addr argv_array_base = stack_min + intSize; // room for argc 380 Addr envp_array_base = argv_array_base + argv_array_size; 381 Addr arg_data_base = envp_array_base + envp_array_size; 382 Addr env_data_base = arg_data_base + arg_data_size; 383 384 // write contents to stack 385 uint64_t argc = argv.size(); 386 if (intSize == 8) 387 argc = htog((uint64_t)argc); 388 else if (intSize == 4) 389 argc = htog((uint32_t)argc); 390 else 391 panic("Unknown int size"); 392 393 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 394 395 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 396 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 397 398 threadContexts[0]->setIntReg(ArgumentReg0, argc); 399 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 400 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 401 402 Addr prog_entry = objFile->entryPoint(); 403 threadContexts[0]->setPC(prog_entry); 404 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 405 406#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 407 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 408#endif 409 410 num_processes++; 411} 412 413void 414LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 415{ 416 num_syscalls++; 417 418 SyscallDesc *desc = getDesc(callnum); 419 if (desc == NULL) 420 fatal("Syscall %d out of range", callnum); 421 422 desc->doSyscall(callnum, this, tc); 423} 424 425LiveProcess * 426LiveProcess::create(const std::string &nm, System *system, int stdin_fd, 427 int stdout_fd, int stderr_fd, std::string executable, 428 std::vector<std::string> &argv, 429 std::vector<std::string> &envp, 430 uint64_t _uid, uint64_t _euid, 431 uint64_t _gid, uint64_t _egid, 432 uint64_t _pid, uint64_t _ppid) 433{ 434 LiveProcess *process = NULL; 435 436 ObjectFile *objFile = createObjectFile(executable); 437 if (objFile == NULL) { 438 fatal("Can't load object file %s", executable); 439 } 440 441#if THE_ISA == ALPHA_ISA 442 if (objFile->getArch() != ObjectFile::Alpha) 443 fatal("Object file architecture does not match compiled ISA (Alpha)."); 444 switch (objFile->getOpSys()) { 445 case ObjectFile::Tru64: 446 process = new AlphaTru64Process(nm, objFile, system, 447 stdin_fd, stdout_fd, stderr_fd, 448 argv, envp, 449 _uid, _euid, _gid, _egid, _pid, _ppid); 450 break; 451 452 case ObjectFile::Linux: 453 process = new AlphaLinuxProcess(nm, objFile, system, 454 stdin_fd, stdout_fd, stderr_fd, 455 argv, envp, 456 _uid, _euid, _gid, _egid, _pid, _ppid); 457 break; 458 459 default: 460 fatal("Unknown/unsupported operating system."); 461 } 462#elif THE_ISA == SPARC_ISA 463 if (objFile->getArch() != ObjectFile::SPARC) 464 fatal("Object file architecture does not match compiled ISA (SPARC)."); 465 switch (objFile->getOpSys()) { 466 case ObjectFile::Linux: 467 process = new SparcLinuxProcess(nm, objFile, system, 468 stdin_fd, stdout_fd, stderr_fd, 469 argv, envp, 470 _uid, _euid, _gid, _egid, _pid, _ppid); 471 break; 472 473 474 case ObjectFile::Solaris: 475 process = new SparcSolarisProcess(nm, objFile, system, 476 stdin_fd, stdout_fd, stderr_fd, 477 argv, envp, 478 _uid, _euid, _gid, _egid, _pid, _ppid); 479 break; 480 default: 481 fatal("Unknown/unsupported operating system."); 482 } 483#elif THE_ISA == MIPS_ISA 484 if (objFile->getArch() != ObjectFile::Mips) 485 fatal("Object file architecture does not match compiled ISA (MIPS)."); 486 switch (objFile->getOpSys()) { 487 case ObjectFile::Linux: 488 process = new MipsLinuxProcess(nm, objFile, system, 489 stdin_fd, stdout_fd, stderr_fd, 490 argv, envp, 491 _uid, _euid, _gid, _egid, _pid, _ppid); 492 break; 493 494 default: 495 fatal("Unknown/unsupported operating system."); 496 } 497#else 498#error "THE_ISA not set" 499#endif 500 501 502 if (process == NULL) 503 fatal("Unknown error creating process object."); 504 return process; 505} 506 507 508BEGIN_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 509 510 VectorParam<string> cmd; 511 Param<string> executable; 512 Param<string> input; 513 Param<string> output; 514 VectorParam<string> env; 515 SimObjectParam<System *> system; 516 Param<uint64_t> uid; 517 Param<uint64_t> euid; 518 Param<uint64_t> gid; 519 Param<uint64_t> egid; 520 Param<uint64_t> pid; 521 Param<uint64_t> ppid; 522 523END_DECLARE_SIM_OBJECT_PARAMS(LiveProcess) 524 525 526BEGIN_INIT_SIM_OBJECT_PARAMS(LiveProcess) 527 528 INIT_PARAM(cmd, "command line (executable plus arguments)"), 529 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 530 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 531 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 532 INIT_PARAM(env, "environment settings"), 533 INIT_PARAM(system, "system"), 534 INIT_PARAM(uid, "user id"), 535 INIT_PARAM(euid, "effective user id"), 536 INIT_PARAM(gid, "group id"), 537 INIT_PARAM(egid, "effective group id"), 538 INIT_PARAM(pid, "process id"), 539 INIT_PARAM(ppid, "parent process id") 540 541END_INIT_SIM_OBJECT_PARAMS(LiveProcess) 542 543 544CREATE_SIM_OBJECT(LiveProcess) 545{ 546 string in = input; 547 string out = output; 548 549 // initialize file descriptors to default: same as simulator 550 int stdin_fd, stdout_fd, stderr_fd; 551 552 if (in == "stdin" || in == "cin") 553 stdin_fd = STDIN_FILENO; 554 else 555 stdin_fd = Process::openInputFile(input); 556 557 if (out == "stdout" || out == "cout") 558 stdout_fd = STDOUT_FILENO; 559 else if (out == "stderr" || out == "cerr") 560 stdout_fd = STDERR_FILENO; 561 else 562 stdout_fd = Process::openOutputFile(out); 563 564 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 565 566 return LiveProcess::create(getInstanceName(), system, 567 stdin_fd, stdout_fd, stderr_fd, 568 (string)executable == "" ? cmd[0] : executable, 569 cmd, env, 570 uid, euid, gid, egid, pid, ppid); 571} 572 573 574REGISTER_SIM_OBJECT("LiveProcess", LiveProcess)
|