93 // initialize first 3 fds (stdin, stdout, stderr) 94 fd_map[STDIN_FILENO] = stdin_fd; 95 fd_map[STDOUT_FILENO] = stdout_fd; 96 fd_map[STDERR_FILENO] = stderr_fd; 97 98 // mark remaining fds as free 99 for (int i = 3; i <= MAX_FD; ++i) { 100 fd_map[i] = -1; 101 } 102 103 mmap_start = mmap_end = 0; 104 nxm_start = nxm_end = 0; 105 pTable = new PageTable(system); 106 // other parameters will be initialized when the program is loaded 107} 108 109 110void 111Process::regStats() 112{ 113 using namespace Stats; 114 115 num_syscalls 116 .name(name() + ".PROG:num_syscalls") 117 .desc("Number of system calls") 118 ; 119} 120 121// 122// static helper functions 123// 124int 125Process::openInputFile(const string &filename) 126{ 127 int fd = open(filename.c_str(), O_RDONLY); 128 129 if (fd == -1) { 130 perror(NULL); 131 cerr << "unable to open \"" << filename << "\" for reading\n"; 132 fatal("can't open input file"); 133 } 134 135 return fd; 136} 137 138 139int 140Process::openOutputFile(const string &filename) 141{ 142 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 143 144 if (fd == -1) { 145 perror(NULL); 146 cerr << "unable to open \"" << filename << "\" for writing\n"; 147 fatal("can't open output file"); 148 } 149 150 return fd; 151} 152 153 154int 155Process::registerThreadContext(ThreadContext *tc) 156{ 157 // add to list 158 int myIndex = threadContexts.size(); 159 threadContexts.push_back(tc); 160 161// RemoteGDB *rgdb = new RemoteGDB(system, tc); 162// GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex); 163// gdbl->listen(); 164 //gdbl->accept(); 165 166// remoteGDB.push_back(rgdb); 167 168 // return CPU number to caller 169 return myIndex; 170} 171 172void 173Process::startup() 174{ 175 if (threadContexts.empty()) 176 fatal("Process %s is not associated with any CPUs!\n", name()); 177 178 // first thread context for this process... initialize & enable 179 ThreadContext *tc = threadContexts[0]; 180 181 // mark this context as active so it will start ticking. 182 tc->activate(0); 183 184 Port *mem_port; 185 mem_port = system->physmem->getPort("functional"); 186 initVirtMem = new TranslatingPort("process init port", this, 187 TranslatingPort::Always); 188 mem_port->setPeer(initVirtMem); 189 initVirtMem->setPeer(mem_port); 190} 191 192void 193Process::replaceThreadContext(ThreadContext *tc, int tcIndex) 194{ 195 if (tcIndex >= threadContexts.size()) { 196 panic("replaceThreadContext: bad tcIndex, %d >= %d\n", 197 tcIndex, threadContexts.size()); 198 } 199 200 threadContexts[tcIndex] = tc; 201} 202 203// map simulator fd sim_fd to target fd tgt_fd 204void 205Process::dup_fd(int sim_fd, int tgt_fd) 206{ 207 if (tgt_fd < 0 || tgt_fd > MAX_FD) 208 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 209 210 fd_map[tgt_fd] = sim_fd; 211} 212 213 214// generate new target fd for sim_fd 215int 216Process::alloc_fd(int sim_fd) 217{ 218 // in case open() returns an error, don't allocate a new fd 219 if (sim_fd == -1) 220 return -1; 221 222 // find first free target fd 223 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 224 if (fd_map[free_fd] == -1) { 225 fd_map[free_fd] = sim_fd; 226 return free_fd; 227 } 228 } 229 230 panic("Process::alloc_fd: out of file descriptors!"); 231} 232 233 234// free target fd (e.g., after close) 235void 236Process::free_fd(int tgt_fd) 237{ 238 if (fd_map[tgt_fd] == -1) 239 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 240 241 fd_map[tgt_fd] = -1; 242} 243 244 245// look up simulator fd for given target fd 246int 247Process::sim_fd(int tgt_fd) 248{ 249 if (tgt_fd > MAX_FD) 250 return -1; 251 252 return fd_map[tgt_fd]; 253} 254 255bool 256Process::checkAndAllocNextPage(Addr vaddr) 257{ 258 // if this is an initial write we might not have 259 if (vaddr >= stack_min && vaddr < stack_base) { 260 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 261 return true; 262 } 263 264 // We've accessed the next page of the stack, so extend the stack 265 // to cover it. 266 if(vaddr < stack_min && vaddr >= stack_min - TheISA::PageBytes) 267 { 268 stack_min -= TheISA::PageBytes; 269 if(stack_base - stack_min > 8*1024*1024) 270 fatal("Over max stack size for one thread\n"); 271 pTable->allocate(stack_min, TheISA::PageBytes); 272 warn("Increasing stack size by one page."); 273 return true; 274 } 275 return false; 276} 277 278void 279Process::serialize(std::ostream &os) 280{ 281 SERIALIZE_SCALAR(initialContextLoaded); 282 SERIALIZE_SCALAR(brk_point); 283 SERIALIZE_SCALAR(stack_base); 284 SERIALIZE_SCALAR(stack_size); 285 SERIALIZE_SCALAR(stack_min); 286 SERIALIZE_SCALAR(next_thread_stack_base); 287 SERIALIZE_SCALAR(mmap_start); 288 SERIALIZE_SCALAR(mmap_end); 289 SERIALIZE_SCALAR(nxm_start); 290 SERIALIZE_SCALAR(nxm_end); 291 SERIALIZE_ARRAY(fd_map, MAX_FD); 292 293 pTable->serialize(os); 294} 295 296void 297Process::unserialize(Checkpoint *cp, const std::string §ion) 298{ 299 UNSERIALIZE_SCALAR(initialContextLoaded); 300 UNSERIALIZE_SCALAR(brk_point); 301 UNSERIALIZE_SCALAR(stack_base); 302 UNSERIALIZE_SCALAR(stack_size); 303 UNSERIALIZE_SCALAR(stack_min); 304 UNSERIALIZE_SCALAR(next_thread_stack_base); 305 UNSERIALIZE_SCALAR(mmap_start); 306 UNSERIALIZE_SCALAR(mmap_end); 307 UNSERIALIZE_SCALAR(nxm_start); 308 UNSERIALIZE_SCALAR(nxm_end); 309 UNSERIALIZE_ARRAY(fd_map, MAX_FD); 310 311 pTable->unserialize(cp, section); 312} 313 314 315//////////////////////////////////////////////////////////////////////// 316// 317// LiveProcess member definitions 318// 319//////////////////////////////////////////////////////////////////////// 320 321 322LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 323 System *_system, 324 int stdin_fd, int stdout_fd, int stderr_fd, 325 vector<string> &_argv, vector<string> &_envp, 326 const string &_cwd, 327 uint64_t _uid, uint64_t _euid, 328 uint64_t _gid, uint64_t _egid, 329 uint64_t _pid, uint64_t _ppid) 330 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 331 objFile(_objFile), argv(_argv), envp(_envp), cwd(_cwd) 332{ 333 __uid = _uid; 334 __euid = _euid; 335 __gid = _gid; 336 __egid = _egid; 337 __pid = _pid; 338 __ppid = _ppid; 339 340 prog_fname = argv[0]; 341 342 // load up symbols, if any... these may be used for debugging or 343 // profiling. 344 if (!debugSymbolTable) { 345 debugSymbolTable = new SymbolTable(); 346 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 347 !objFile->loadLocalSymbols(debugSymbolTable)) { 348 // didn't load any symbols 349 delete debugSymbolTable; 350 debugSymbolTable = NULL; 351 } 352 } 353} 354 355void 356LiveProcess::argsInit(int intSize, int pageSize) 357{ 358 Process::startup(); 359 360 // load object file into target memory 361 objFile->loadSections(initVirtMem); 362 363 // Calculate how much space we need for arg & env arrays. 364 int argv_array_size = intSize * (argv.size() + 1); 365 int envp_array_size = intSize * (envp.size() + 1); 366 int arg_data_size = 0; 367 for (int i = 0; i < argv.size(); ++i) { 368 arg_data_size += argv[i].size() + 1; 369 } 370 int env_data_size = 0; 371 for (int i = 0; i < envp.size(); ++i) { 372 env_data_size += envp[i].size() + 1; 373 } 374 375 int space_needed = 376 argv_array_size + envp_array_size + arg_data_size + env_data_size; 377 if (space_needed < 32*1024) 378 space_needed = 32*1024; 379 380 // set bottom of stack 381 stack_min = stack_base - space_needed; 382 // align it 383 stack_min = roundDown(stack_min, pageSize); 384 stack_size = stack_base - stack_min; 385 // map memory 386 pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 387 388 // map out initial stack contents 389 Addr argv_array_base = stack_min + intSize; // room for argc 390 Addr envp_array_base = argv_array_base + argv_array_size; 391 Addr arg_data_base = envp_array_base + envp_array_size; 392 Addr env_data_base = arg_data_base + arg_data_size; 393 394 // write contents to stack 395 uint64_t argc = argv.size(); 396 if (intSize == 8) 397 argc = htog((uint64_t)argc); 398 else if (intSize == 4) 399 argc = htog((uint32_t)argc); 400 else 401 panic("Unknown int size"); 402 403 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 404 405 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 406 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 407 408 assert(NumArgumentRegs >= 2); 409 threadContexts[0]->setIntReg(ArgumentReg[0], argc); 410 threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base); 411 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 412 413 Addr prog_entry = objFile->entryPoint(); 414 threadContexts[0]->setPC(prog_entry); 415 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 416 417#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 418 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 419#endif 420 421 num_processes++; 422} 423 424void 425LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 426{ 427 num_syscalls++; 428 429 SyscallDesc *desc = getDesc(callnum); 430 if (desc == NULL) 431 fatal("Syscall %d out of range", callnum); 432 433 desc->doSyscall(callnum, this, tc); 434} 435 436LiveProcess * 437LiveProcess::create(const std::string &nm, System *system, int stdin_fd, 438 int stdout_fd, int stderr_fd, std::string executable, 439 std::vector<std::string> &argv, 440 std::vector<std::string> &envp, 441 const std::string &cwd, 442 uint64_t _uid, uint64_t _euid, 443 uint64_t _gid, uint64_t _egid, 444 uint64_t _pid, uint64_t _ppid) 445{ 446 LiveProcess *process = NULL; 447 448 ObjectFile *objFile = createObjectFile(executable); 449 if (objFile == NULL) { 450 fatal("Can't load object file %s", executable); 451 } 452 453 if (objFile->isDynamic()) 454 fatal("Object file is a dynamic executable however only static " 455 "executables are supported!\n Please recompile your " 456 "executable as a static binary and try again.\n"); 457 458#if THE_ISA == ALPHA_ISA 459 if (objFile->getArch() != ObjectFile::Alpha) 460 fatal("Object file architecture does not match compiled ISA (Alpha)."); 461 switch (objFile->getOpSys()) { 462 case ObjectFile::Tru64: 463 process = new AlphaTru64Process(nm, objFile, system, 464 stdin_fd, stdout_fd, stderr_fd, 465 argv, envp, cwd, 466 _uid, _euid, _gid, _egid, _pid, _ppid); 467 break; 468 469 case ObjectFile::Linux: 470 process = new AlphaLinuxProcess(nm, objFile, system, 471 stdin_fd, stdout_fd, stderr_fd, 472 argv, envp, cwd, 473 _uid, _euid, _gid, _egid, _pid, _ppid); 474 break; 475 476 default: 477 fatal("Unknown/unsupported operating system."); 478 } 479#elif THE_ISA == SPARC_ISA 480 if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32) 481 fatal("Object file architecture does not match compiled ISA (SPARC)."); 482 switch (objFile->getOpSys()) { 483 case ObjectFile::Linux: 484 if (objFile->getArch() == ObjectFile::SPARC64) { 485 process = new Sparc64LinuxProcess(nm, objFile, system, 486 stdin_fd, stdout_fd, stderr_fd, 487 argv, envp, cwd, 488 _uid, _euid, _gid, 489 _egid, _pid, _ppid); 490 } else { 491 process = new Sparc32LinuxProcess(nm, objFile, system, 492 stdin_fd, stdout_fd, stderr_fd, 493 argv, envp, cwd, 494 _uid, _euid, _gid, 495 _egid, _pid, _ppid); 496 } 497 break; 498 499 500 case ObjectFile::Solaris: 501 process = new SparcSolarisProcess(nm, objFile, system, 502 stdin_fd, stdout_fd, stderr_fd, 503 argv, envp, cwd, 504 _uid, _euid, _gid, _egid, _pid, _ppid); 505 break; 506 default: 507 fatal("Unknown/unsupported operating system."); 508 } 509#elif THE_ISA == X86_ISA 510 if (objFile->getArch() != ObjectFile::X86) 511 fatal("Object file architecture does not match compiled ISA (x86)."); 512 switch (objFile->getOpSys()) { 513 case ObjectFile::Linux: 514 process = new X86LinuxProcess(nm, objFile, system, 515 stdin_fd, stdout_fd, stderr_fd, 516 argv, envp, cwd, 517 _uid, _euid, _gid, 518 _egid, _pid, _ppid); 519 break; 520 default: 521 fatal("Unknown/unsupported operating system."); 522 } 523#elif THE_ISA == MIPS_ISA 524 if (objFile->getArch() != ObjectFile::Mips) 525 fatal("Object file architecture does not match compiled ISA (MIPS)."); 526 switch (objFile->getOpSys()) { 527 case ObjectFile::Linux: 528 process = new MipsLinuxProcess(nm, objFile, system, 529 stdin_fd, stdout_fd, stderr_fd, 530 argv, envp, cwd, 531 _uid, _euid, _gid, _egid, _pid, _ppid); 532 break; 533 534 default: 535 fatal("Unknown/unsupported operating system."); 536 } 537#else 538#error "THE_ISA not set" 539#endif 540 541 542 if (process == NULL) 543 fatal("Unknown error creating process object."); 544 return process; 545} 546 547LiveProcess * 548LiveProcessParams::create() 549{ 550 string in = input; 551 string out = output; 552 553 // initialize file descriptors to default: same as simulator 554 int stdin_fd, stdout_fd, stderr_fd; 555 556 if (in == "stdin" || in == "cin") 557 stdin_fd = STDIN_FILENO; 558 else 559 stdin_fd = Process::openInputFile(input); 560 561 if (out == "stdout" || out == "cout") 562 stdout_fd = STDOUT_FILENO; 563 else if (out == "stderr" || out == "cerr") 564 stdout_fd = STDERR_FILENO; 565 else 566 stdout_fd = Process::openOutputFile(out); 567 568 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 569 570 return LiveProcess::create(name, system, 571 stdin_fd, stdout_fd, stderr_fd, 572 (string)executable == "" ? cmd[0] : executable, 573 cmd, env, cwd, 574 uid, euid, gid, egid, pid, ppid); 575}
| 94 // initialize first 3 fds (stdin, stdout, stderr) 95 fd_map[STDIN_FILENO] = stdin_fd; 96 fd_map[STDOUT_FILENO] = stdout_fd; 97 fd_map[STDERR_FILENO] = stderr_fd; 98 99 // mark remaining fds as free 100 for (int i = 3; i <= MAX_FD; ++i) { 101 fd_map[i] = -1; 102 } 103 104 mmap_start = mmap_end = 0; 105 nxm_start = nxm_end = 0; 106 pTable = new PageTable(system); 107 // other parameters will be initialized when the program is loaded 108} 109 110 111void 112Process::regStats() 113{ 114 using namespace Stats; 115 116 num_syscalls 117 .name(name() + ".PROG:num_syscalls") 118 .desc("Number of system calls") 119 ; 120} 121 122// 123// static helper functions 124// 125int 126Process::openInputFile(const string &filename) 127{ 128 int fd = open(filename.c_str(), O_RDONLY); 129 130 if (fd == -1) { 131 perror(NULL); 132 cerr << "unable to open \"" << filename << "\" for reading\n"; 133 fatal("can't open input file"); 134 } 135 136 return fd; 137} 138 139 140int 141Process::openOutputFile(const string &filename) 142{ 143 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0774); 144 145 if (fd == -1) { 146 perror(NULL); 147 cerr << "unable to open \"" << filename << "\" for writing\n"; 148 fatal("can't open output file"); 149 } 150 151 return fd; 152} 153 154 155int 156Process::registerThreadContext(ThreadContext *tc) 157{ 158 // add to list 159 int myIndex = threadContexts.size(); 160 threadContexts.push_back(tc); 161 162// RemoteGDB *rgdb = new RemoteGDB(system, tc); 163// GDBListener *gdbl = new GDBListener(rgdb, 7000 + myIndex); 164// gdbl->listen(); 165 //gdbl->accept(); 166 167// remoteGDB.push_back(rgdb); 168 169 // return CPU number to caller 170 return myIndex; 171} 172 173void 174Process::startup() 175{ 176 if (threadContexts.empty()) 177 fatal("Process %s is not associated with any CPUs!\n", name()); 178 179 // first thread context for this process... initialize & enable 180 ThreadContext *tc = threadContexts[0]; 181 182 // mark this context as active so it will start ticking. 183 tc->activate(0); 184 185 Port *mem_port; 186 mem_port = system->physmem->getPort("functional"); 187 initVirtMem = new TranslatingPort("process init port", this, 188 TranslatingPort::Always); 189 mem_port->setPeer(initVirtMem); 190 initVirtMem->setPeer(mem_port); 191} 192 193void 194Process::replaceThreadContext(ThreadContext *tc, int tcIndex) 195{ 196 if (tcIndex >= threadContexts.size()) { 197 panic("replaceThreadContext: bad tcIndex, %d >= %d\n", 198 tcIndex, threadContexts.size()); 199 } 200 201 threadContexts[tcIndex] = tc; 202} 203 204// map simulator fd sim_fd to target fd tgt_fd 205void 206Process::dup_fd(int sim_fd, int tgt_fd) 207{ 208 if (tgt_fd < 0 || tgt_fd > MAX_FD) 209 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 210 211 fd_map[tgt_fd] = sim_fd; 212} 213 214 215// generate new target fd for sim_fd 216int 217Process::alloc_fd(int sim_fd) 218{ 219 // in case open() returns an error, don't allocate a new fd 220 if (sim_fd == -1) 221 return -1; 222 223 // find first free target fd 224 for (int free_fd = 0; free_fd < MAX_FD; ++free_fd) { 225 if (fd_map[free_fd] == -1) { 226 fd_map[free_fd] = sim_fd; 227 return free_fd; 228 } 229 } 230 231 panic("Process::alloc_fd: out of file descriptors!"); 232} 233 234 235// free target fd (e.g., after close) 236void 237Process::free_fd(int tgt_fd) 238{ 239 if (fd_map[tgt_fd] == -1) 240 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 241 242 fd_map[tgt_fd] = -1; 243} 244 245 246// look up simulator fd for given target fd 247int 248Process::sim_fd(int tgt_fd) 249{ 250 if (tgt_fd > MAX_FD) 251 return -1; 252 253 return fd_map[tgt_fd]; 254} 255 256bool 257Process::checkAndAllocNextPage(Addr vaddr) 258{ 259 // if this is an initial write we might not have 260 if (vaddr >= stack_min && vaddr < stack_base) { 261 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 262 return true; 263 } 264 265 // We've accessed the next page of the stack, so extend the stack 266 // to cover it. 267 if(vaddr < stack_min && vaddr >= stack_min - TheISA::PageBytes) 268 { 269 stack_min -= TheISA::PageBytes; 270 if(stack_base - stack_min > 8*1024*1024) 271 fatal("Over max stack size for one thread\n"); 272 pTable->allocate(stack_min, TheISA::PageBytes); 273 warn("Increasing stack size by one page."); 274 return true; 275 } 276 return false; 277} 278 279void 280Process::serialize(std::ostream &os) 281{ 282 SERIALIZE_SCALAR(initialContextLoaded); 283 SERIALIZE_SCALAR(brk_point); 284 SERIALIZE_SCALAR(stack_base); 285 SERIALIZE_SCALAR(stack_size); 286 SERIALIZE_SCALAR(stack_min); 287 SERIALIZE_SCALAR(next_thread_stack_base); 288 SERIALIZE_SCALAR(mmap_start); 289 SERIALIZE_SCALAR(mmap_end); 290 SERIALIZE_SCALAR(nxm_start); 291 SERIALIZE_SCALAR(nxm_end); 292 SERIALIZE_ARRAY(fd_map, MAX_FD); 293 294 pTable->serialize(os); 295} 296 297void 298Process::unserialize(Checkpoint *cp, const std::string §ion) 299{ 300 UNSERIALIZE_SCALAR(initialContextLoaded); 301 UNSERIALIZE_SCALAR(brk_point); 302 UNSERIALIZE_SCALAR(stack_base); 303 UNSERIALIZE_SCALAR(stack_size); 304 UNSERIALIZE_SCALAR(stack_min); 305 UNSERIALIZE_SCALAR(next_thread_stack_base); 306 UNSERIALIZE_SCALAR(mmap_start); 307 UNSERIALIZE_SCALAR(mmap_end); 308 UNSERIALIZE_SCALAR(nxm_start); 309 UNSERIALIZE_SCALAR(nxm_end); 310 UNSERIALIZE_ARRAY(fd_map, MAX_FD); 311 312 pTable->unserialize(cp, section); 313} 314 315 316//////////////////////////////////////////////////////////////////////// 317// 318// LiveProcess member definitions 319// 320//////////////////////////////////////////////////////////////////////// 321 322 323LiveProcess::LiveProcess(const string &nm, ObjectFile *_objFile, 324 System *_system, 325 int stdin_fd, int stdout_fd, int stderr_fd, 326 vector<string> &_argv, vector<string> &_envp, 327 const string &_cwd, 328 uint64_t _uid, uint64_t _euid, 329 uint64_t _gid, uint64_t _egid, 330 uint64_t _pid, uint64_t _ppid) 331 : Process(nm, _system, stdin_fd, stdout_fd, stderr_fd), 332 objFile(_objFile), argv(_argv), envp(_envp), cwd(_cwd) 333{ 334 __uid = _uid; 335 __euid = _euid; 336 __gid = _gid; 337 __egid = _egid; 338 __pid = _pid; 339 __ppid = _ppid; 340 341 prog_fname = argv[0]; 342 343 // load up symbols, if any... these may be used for debugging or 344 // profiling. 345 if (!debugSymbolTable) { 346 debugSymbolTable = new SymbolTable(); 347 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 348 !objFile->loadLocalSymbols(debugSymbolTable)) { 349 // didn't load any symbols 350 delete debugSymbolTable; 351 debugSymbolTable = NULL; 352 } 353 } 354} 355 356void 357LiveProcess::argsInit(int intSize, int pageSize) 358{ 359 Process::startup(); 360 361 // load object file into target memory 362 objFile->loadSections(initVirtMem); 363 364 // Calculate how much space we need for arg & env arrays. 365 int argv_array_size = intSize * (argv.size() + 1); 366 int envp_array_size = intSize * (envp.size() + 1); 367 int arg_data_size = 0; 368 for (int i = 0; i < argv.size(); ++i) { 369 arg_data_size += argv[i].size() + 1; 370 } 371 int env_data_size = 0; 372 for (int i = 0; i < envp.size(); ++i) { 373 env_data_size += envp[i].size() + 1; 374 } 375 376 int space_needed = 377 argv_array_size + envp_array_size + arg_data_size + env_data_size; 378 if (space_needed < 32*1024) 379 space_needed = 32*1024; 380 381 // set bottom of stack 382 stack_min = stack_base - space_needed; 383 // align it 384 stack_min = roundDown(stack_min, pageSize); 385 stack_size = stack_base - stack_min; 386 // map memory 387 pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 388 389 // map out initial stack contents 390 Addr argv_array_base = stack_min + intSize; // room for argc 391 Addr envp_array_base = argv_array_base + argv_array_size; 392 Addr arg_data_base = envp_array_base + envp_array_size; 393 Addr env_data_base = arg_data_base + arg_data_size; 394 395 // write contents to stack 396 uint64_t argc = argv.size(); 397 if (intSize == 8) 398 argc = htog((uint64_t)argc); 399 else if (intSize == 4) 400 argc = htog((uint32_t)argc); 401 else 402 panic("Unknown int size"); 403 404 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 405 406 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 407 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 408 409 assert(NumArgumentRegs >= 2); 410 threadContexts[0]->setIntReg(ArgumentReg[0], argc); 411 threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base); 412 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 413 414 Addr prog_entry = objFile->entryPoint(); 415 threadContexts[0]->setPC(prog_entry); 416 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 417 418#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 419 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 420#endif 421 422 num_processes++; 423} 424 425void 426LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 427{ 428 num_syscalls++; 429 430 SyscallDesc *desc = getDesc(callnum); 431 if (desc == NULL) 432 fatal("Syscall %d out of range", callnum); 433 434 desc->doSyscall(callnum, this, tc); 435} 436 437LiveProcess * 438LiveProcess::create(const std::string &nm, System *system, int stdin_fd, 439 int stdout_fd, int stderr_fd, std::string executable, 440 std::vector<std::string> &argv, 441 std::vector<std::string> &envp, 442 const std::string &cwd, 443 uint64_t _uid, uint64_t _euid, 444 uint64_t _gid, uint64_t _egid, 445 uint64_t _pid, uint64_t _ppid) 446{ 447 LiveProcess *process = NULL; 448 449 ObjectFile *objFile = createObjectFile(executable); 450 if (objFile == NULL) { 451 fatal("Can't load object file %s", executable); 452 } 453 454 if (objFile->isDynamic()) 455 fatal("Object file is a dynamic executable however only static " 456 "executables are supported!\n Please recompile your " 457 "executable as a static binary and try again.\n"); 458 459#if THE_ISA == ALPHA_ISA 460 if (objFile->getArch() != ObjectFile::Alpha) 461 fatal("Object file architecture does not match compiled ISA (Alpha)."); 462 switch (objFile->getOpSys()) { 463 case ObjectFile::Tru64: 464 process = new AlphaTru64Process(nm, objFile, system, 465 stdin_fd, stdout_fd, stderr_fd, 466 argv, envp, cwd, 467 _uid, _euid, _gid, _egid, _pid, _ppid); 468 break; 469 470 case ObjectFile::Linux: 471 process = new AlphaLinuxProcess(nm, objFile, system, 472 stdin_fd, stdout_fd, stderr_fd, 473 argv, envp, cwd, 474 _uid, _euid, _gid, _egid, _pid, _ppid); 475 break; 476 477 default: 478 fatal("Unknown/unsupported operating system."); 479 } 480#elif THE_ISA == SPARC_ISA 481 if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32) 482 fatal("Object file architecture does not match compiled ISA (SPARC)."); 483 switch (objFile->getOpSys()) { 484 case ObjectFile::Linux: 485 if (objFile->getArch() == ObjectFile::SPARC64) { 486 process = new Sparc64LinuxProcess(nm, objFile, system, 487 stdin_fd, stdout_fd, stderr_fd, 488 argv, envp, cwd, 489 _uid, _euid, _gid, 490 _egid, _pid, _ppid); 491 } else { 492 process = new Sparc32LinuxProcess(nm, objFile, system, 493 stdin_fd, stdout_fd, stderr_fd, 494 argv, envp, cwd, 495 _uid, _euid, _gid, 496 _egid, _pid, _ppid); 497 } 498 break; 499 500 501 case ObjectFile::Solaris: 502 process = new SparcSolarisProcess(nm, objFile, system, 503 stdin_fd, stdout_fd, stderr_fd, 504 argv, envp, cwd, 505 _uid, _euid, _gid, _egid, _pid, _ppid); 506 break; 507 default: 508 fatal("Unknown/unsupported operating system."); 509 } 510#elif THE_ISA == X86_ISA 511 if (objFile->getArch() != ObjectFile::X86) 512 fatal("Object file architecture does not match compiled ISA (x86)."); 513 switch (objFile->getOpSys()) { 514 case ObjectFile::Linux: 515 process = new X86LinuxProcess(nm, objFile, system, 516 stdin_fd, stdout_fd, stderr_fd, 517 argv, envp, cwd, 518 _uid, _euid, _gid, 519 _egid, _pid, _ppid); 520 break; 521 default: 522 fatal("Unknown/unsupported operating system."); 523 } 524#elif THE_ISA == MIPS_ISA 525 if (objFile->getArch() != ObjectFile::Mips) 526 fatal("Object file architecture does not match compiled ISA (MIPS)."); 527 switch (objFile->getOpSys()) { 528 case ObjectFile::Linux: 529 process = new MipsLinuxProcess(nm, objFile, system, 530 stdin_fd, stdout_fd, stderr_fd, 531 argv, envp, cwd, 532 _uid, _euid, _gid, _egid, _pid, _ppid); 533 break; 534 535 default: 536 fatal("Unknown/unsupported operating system."); 537 } 538#else 539#error "THE_ISA not set" 540#endif 541 542 543 if (process == NULL) 544 fatal("Unknown error creating process object."); 545 return process; 546} 547 548LiveProcess * 549LiveProcessParams::create() 550{ 551 string in = input; 552 string out = output; 553 554 // initialize file descriptors to default: same as simulator 555 int stdin_fd, stdout_fd, stderr_fd; 556 557 if (in == "stdin" || in == "cin") 558 stdin_fd = STDIN_FILENO; 559 else 560 stdin_fd = Process::openInputFile(input); 561 562 if (out == "stdout" || out == "cout") 563 stdout_fd = STDOUT_FILENO; 564 else if (out == "stderr" || out == "cerr") 565 stdout_fd = STDERR_FILENO; 566 else 567 stdout_fd = Process::openOutputFile(out); 568 569 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 570 571 return LiveProcess::create(name, system, 572 stdin_fd, stdout_fd, stderr_fd, 573 (string)executable == "" ? cmd[0] : executable, 574 cmd, env, cwd, 575 uid, euid, gid, egid, pid, ppid); 576}
|