85#else 86#error "THE_ISA not set" 87#endif 88 89 90using namespace std; 91using namespace TheISA; 92 93// current number of allocated processes 94int num_processes = 0; 95 96template<class IntType> 97 98AuxVector<IntType>::AuxVector(IntType type, IntType val) 99{ 100 a_type = TheISA::htog(type); 101 a_val = TheISA::htog(val); 102} 103 104template struct AuxVector<uint32_t>; 105template struct AuxVector<uint64_t>; 106 107static int 108openFile(const string& filename, int flags, mode_t mode) 109{ 110 int sim_fd = open(filename.c_str(), flags, mode); 111 if (sim_fd != -1) 112 return sim_fd; 113 fatal("Unable to open %s with mode %O", filename, mode); 114} 115 116static int 117openInputFile(const string &filename) 118{ 119 return openFile(filename, O_RDONLY, 0); 120} 121 122static int 123openOutputFile(const string &filename) 124{ 125 return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); 126} 127 128Process::Process(ProcessParams * params) 129 : SimObject(params), system(params->system), 130 brk_point(0), stack_base(0), stack_size(0), stack_min(0), 131 max_stack_size(params->max_stack_size), 132 next_thread_stack_base(0), 133 M5_pid(system->allocatePID()), 134 useArchPT(params->useArchPT), 135 kvmInSE(params->kvmInSE), 136 pTable(useArchPT ? 137 static_cast<PageTableBase *>(new ArchPageTable(name(), M5_pid, system)) : 138 static_cast<PageTableBase *>(new FuncPageTable(name(), M5_pid)) ), 139 initVirtMem(system->getSystemPort(), this, 140 SETranslatingPortProxy::Always), 141 fd_array(make_shared<array<FDEntry, NUM_FDS>>()), 142 imap {{"", -1}, 143 {"cin", STDIN_FILENO}, 144 {"stdin", STDIN_FILENO}}, 145 oemap{{"", -1}, 146 {"cout", STDOUT_FILENO}, 147 {"stdout", STDOUT_FILENO}, 148 {"cerr", STDERR_FILENO}, 149 {"stderr", STDERR_FILENO}} 150{ 151 int sim_fd; 152 std::map<string,int>::iterator it; 153 154 // Search through the input options and set fd if match is found; 155 // otherwise, open an input file and seek to location. 156 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO); 157 if ((it = imap.find(params->input)) != imap.end()) 158 sim_fd = it->second; 159 else 160 sim_fd = openInputFile(params->input); 161 fde_stdin->set(sim_fd, params->input, O_RDONLY, -1, false); 162 163 // Search through the output/error options and set fd if match is found; 164 // otherwise, open an output file and seek to location. 165 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO); 166 if ((it = oemap.find(params->output)) != oemap.end()) 167 sim_fd = it->second; 168 else 169 sim_fd = openOutputFile(params->output); 170 fde_stdout->set(sim_fd, params->output, O_WRONLY | O_CREAT | O_TRUNC, 171 0664, false); 172 173 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO); 174 if (params->output == params->errout) 175 // Reuse the same file descriptor if these match. 176 sim_fd = fde_stdout->fd; 177 else if ((it = oemap.find(params->errout)) != oemap.end()) 178 sim_fd = it->second; 179 else 180 sim_fd = openOutputFile(params->errout); 181 fde_stderr->set(sim_fd, params->errout, O_WRONLY | O_CREAT | O_TRUNC, 182 0664, false); 183 184 mmap_end = 0; 185 nxm_start = nxm_end = 0; 186 // other parameters will be initialized when the program is loaded 187} 188 189 190void 191Process::regStats() 192{ 193 SimObject::regStats(); 194 195 using namespace Stats; 196 197 num_syscalls 198 .name(name() + ".num_syscalls") 199 .desc("Number of system calls") 200 ; 201} 202 203void 204Process::inheritFDArray(Process *p) 205{ 206 fd_array = p->fd_array; 207} 208 209ThreadContext * 210Process::findFreeContext() 211{ 212 for (int id : contextIds) { 213 ThreadContext *tc = system->getThreadContext(id); 214 if (tc->status() == ThreadContext::Halted) 215 return tc; 216 } 217 return NULL; 218} 219 220void 221Process::initState() 222{ 223 if (contextIds.empty()) 224 fatal("Process %s is not associated with any HW contexts!\n", name()); 225 226 // first thread context for this process... initialize & enable 227 ThreadContext *tc = system->getThreadContext(contextIds[0]); 228 229 // mark this context as active so it will start ticking. 230 tc->activate(); 231 232 pTable->initState(tc); 233} 234 235DrainState 236Process::drain() 237{ 238 findFileOffsets(); 239 return DrainState::Drained; 240} 241 242int 243Process::allocFD(int sim_fd, const string& filename, int flags, int mode, 244 bool pipe) 245{ 246 for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) { 247 FDEntry *fde = getFDEntry(free_fd); 248 if (fde->isFree()) { 249 fde->set(sim_fd, filename, flags, mode, pipe); 250 return free_fd; 251 } 252 } 253 254 fatal("Out of target file descriptors"); 255} 256 257void 258Process::resetFDEntry(int tgt_fd) 259{ 260 FDEntry *fde = getFDEntry(tgt_fd); 261 assert(fde->fd > -1); 262 263 fde->reset(); 264} 265 266int 267Process::getSimFD(int tgt_fd) 268{ 269 FDEntry *entry = getFDEntry(tgt_fd); 270 return entry ? entry->fd : -1; 271} 272 273FDEntry * 274Process::getFDEntry(int tgt_fd) 275{ 276 assert(0 <= tgt_fd && tgt_fd < fd_array->size()); 277 return &(*fd_array)[tgt_fd]; 278} 279 280int 281Process::getTgtFD(int sim_fd) 282{ 283 for (int index = 0; index < fd_array->size(); index++) 284 if ((*fd_array)[index].fd == sim_fd) 285 return index; 286 return -1; 287} 288 289void 290Process::allocateMem(Addr vaddr, int64_t size, bool clobber) 291{ 292 int npages = divCeil(size, (int64_t)PageBytes); 293 Addr paddr = system->allocPhysPages(npages); 294 pTable->map(vaddr, paddr, size, 295 clobber ? PageTableBase::Clobber : PageTableBase::Zero); 296} 297 298bool 299Process::fixupStackFault(Addr vaddr) 300{ 301 // Check if this is already on the stack and there's just no page there 302 // yet. 303 if (vaddr >= stack_min && vaddr < stack_base) { 304 allocateMem(roundDown(vaddr, PageBytes), PageBytes); 305 return true; 306 } 307 308 // We've accessed the next page of the stack, so extend it to include 309 // this address. 310 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 311 while (vaddr < stack_min) { 312 stack_min -= TheISA::PageBytes; 313 if (stack_base - stack_min > max_stack_size) 314 fatal("Maximum stack size exceeded\n"); 315 allocateMem(stack_min, TheISA::PageBytes); 316 inform("Increasing stack size by one page."); 317 }; 318 return true; 319 } 320 return false; 321} 322 323void 324Process::fixFileOffsets() 325{ 326 auto seek = [] (FDEntry *fde) 327 { 328 if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0) 329 fatal("Unable to see to location in %s", fde->filename); 330 }; 331 332 std::map<string,int>::iterator it; 333 334 // Search through the input options and set fd if match is found; 335 // otherwise, open an input file and seek to location. 336 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO); 337 if ((it = imap.find(fde_stdin->filename)) != imap.end()) { 338 fde_stdin->fd = it->second; 339 } else { 340 fde_stdin->fd = openInputFile(fde_stdin->filename); 341 seek(fde_stdin); 342 } 343 344 // Search through the output/error options and set fd if match is found; 345 // otherwise, open an output file and seek to location. 346 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO); 347 if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) { 348 fde_stdout->fd = it->second; 349 } else { 350 fde_stdout->fd = openOutputFile(fde_stdout->filename); 351 seek(fde_stdout); 352 } 353 354 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO); 355 if (fde_stdout->filename == fde_stderr->filename) { 356 // Reuse the same file descriptor if these match. 357 fde_stderr->fd = fde_stdout->fd; 358 } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) { 359 fde_stderr->fd = it->second; 360 } else { 361 fde_stderr->fd = openOutputFile(fde_stderr->filename); 362 seek(fde_stderr); 363 } 364 365 for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) { 366 FDEntry *fde = getFDEntry(tgt_fd); 367 if (fde->fd == -1) 368 continue; 369 370 if (fde->isPipe) { 371 if (fde->filename == "PIPE-WRITE") 372 continue; 373 assert(fde->filename == "PIPE-READ"); 374 375 int fds[2]; 376 if (pipe(fds) < 0) 377 fatal("Unable to create new pipe"); 378 379 fde->fd = fds[0]; 380 381 FDEntry *fde_write = getFDEntry(fde->readPipeSource); 382 assert(fde_write->filename == "PIPE-WRITE"); 383 fde_write->fd = fds[1]; 384 } else { 385 fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode); 386 seek(fde); 387 } 388 } 389} 390 391void 392Process::findFileOffsets() 393{ 394 for (auto& fde : *fd_array) { 395 if (fde.fd != -1) 396 fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR); 397 } 398} 399 400void 401Process::setReadPipeSource(int read_pipe_fd, int source_fd) 402{ 403 FDEntry *fde = getFDEntry(read_pipe_fd); 404 assert(source_fd >= -1); 405 fde->readPipeSource = source_fd; 406} 407 408void 409Process::serialize(CheckpointOut &cp) const 410{ 411 SERIALIZE_SCALAR(brk_point); 412 SERIALIZE_SCALAR(stack_base); 413 SERIALIZE_SCALAR(stack_size); 414 SERIALIZE_SCALAR(stack_min); 415 SERIALIZE_SCALAR(next_thread_stack_base); 416 SERIALIZE_SCALAR(mmap_end); 417 SERIALIZE_SCALAR(nxm_start); 418 SERIALIZE_SCALAR(nxm_end); 419 pTable->serialize(cp); 420 for (int x = 0; x < fd_array->size(); x++) { 421 (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x)); 422 } 423 SERIALIZE_SCALAR(M5_pid); 424 425} 426 427void 428Process::unserialize(CheckpointIn &cp) 429{ 430 UNSERIALIZE_SCALAR(brk_point); 431 UNSERIALIZE_SCALAR(stack_base); 432 UNSERIALIZE_SCALAR(stack_size); 433 UNSERIALIZE_SCALAR(stack_min); 434 UNSERIALIZE_SCALAR(next_thread_stack_base); 435 UNSERIALIZE_SCALAR(mmap_end); 436 UNSERIALIZE_SCALAR(nxm_start); 437 UNSERIALIZE_SCALAR(nxm_end); 438 pTable->unserialize(cp); 439 for (int x = 0; x < fd_array->size(); x++) { 440 FDEntry *fde = getFDEntry(x); 441 fde->unserializeSection(cp, csprintf("FDEntry%d", x)); 442 } 443 fixFileOffsets(); 444 UNSERIALIZE_OPT_SCALAR(M5_pid); 445 // The above returns a bool so that you could do something if you don't 446 // find the param in the checkpoint if you wanted to, like set a default 447 // but in this case we'll just stick with the instantiated value if not 448 // found. 449} 450 451 452bool 453Process::map(Addr vaddr, Addr paddr, int size, bool cacheable) 454{ 455 pTable->map(vaddr, paddr, size, 456 cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable); 457 return true; 458} 459 460 461//////////////////////////////////////////////////////////////////////// 462// 463// LiveProcess member definitions 464// 465//////////////////////////////////////////////////////////////////////// 466 467 468LiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile) 469 : Process(params), objFile(_objFile), 470 argv(params->cmd), envp(params->env), cwd(params->cwd), 471 executable(params->executable), 472 __uid(params->uid), __euid(params->euid), 473 __gid(params->gid), __egid(params->egid), 474 __pid(params->pid), __ppid(params->ppid), 475 drivers(params->drivers) 476{ 477 478 // load up symbols, if any... these may be used for debugging or 479 // profiling. 480 if (!debugSymbolTable) { 481 debugSymbolTable = new SymbolTable(); 482 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 483 !objFile->loadLocalSymbols(debugSymbolTable) || 484 !objFile->loadWeakSymbols(debugSymbolTable)) { 485 // didn't load any symbols 486 delete debugSymbolTable; 487 debugSymbolTable = NULL; 488 } 489 } 490} 491 492void 493LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 494{ 495 num_syscalls++; 496 497 SyscallDesc *desc = getDesc(callnum); 498 if (desc == NULL) 499 fatal("Syscall %d out of range", callnum); 500 501 desc->doSyscall(callnum, this, tc); 502} 503 504IntReg 505LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width) 506{ 507 return getSyscallArg(tc, i); 508} 509 510 511EmulatedDriver * 512LiveProcess::findDriver(std::string filename) 513{ 514 for (EmulatedDriver *d : drivers) { 515 if (d->match(filename)) 516 return d; 517 } 518 519 return NULL; 520} 521 522void 523LiveProcess::updateBias() 524{ 525 ObjectFile *interp = objFile->getInterpreter(); 526 527 if (!interp || !interp->relocatable()) 528 return; 529 530 // Determine how large the interpreters footprint will be in the process 531 // address space. 532 Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes); 533 534 // We are allocating the memory area; set the bias to the lowest address 535 // in the allocated memory region. 536 Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end; 537 538 // Adjust the process mmap area to give the interpreter room; the real 539 // execve system call would just invoke the kernel's internal mmap 540 // functions to make these adjustments. 541 mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize; 542 543 interp->updateBias(ld_bias); 544} 545 546 547ObjectFile * 548LiveProcess::getInterpreter() 549{ 550 return objFile->getInterpreter(); 551} 552 553 554Addr 555LiveProcess::getBias() 556{ 557 ObjectFile *interp = getInterpreter(); 558 559 return interp ? interp->bias() : objFile->bias(); 560} 561 562 563Addr 564LiveProcess::getStartPC() 565{ 566 ObjectFile *interp = getInterpreter(); 567 568 return interp ? interp->entryPoint() : objFile->entryPoint(); 569} 570 571 572LiveProcess * 573LiveProcess::create(LiveProcessParams * params) 574{ 575 LiveProcess *process = NULL; 576 577 // If not specified, set the executable parameter equal to the 578 // simulated system's zeroth command line parameter 579 if (params->executable == "") { 580 params->executable = params->cmd[0]; 581 } 582 583 ObjectFile *objFile = createObjectFile(params->executable); 584 if (objFile == NULL) { 585 fatal("Can't load object file %s", params->executable); 586 } 587 588#if THE_ISA == ALPHA_ISA 589 if (objFile->getArch() != ObjectFile::Alpha) 590 fatal("Object file architecture does not match compiled ISA (Alpha)."); 591 592 switch (objFile->getOpSys()) { 593 case ObjectFile::UnknownOpSys: 594 warn("Unknown operating system; assuming Linux."); 595 // fall through 596 case ObjectFile::Linux: 597 process = new AlphaLinuxProcess(params, objFile); 598 break; 599 600 default: 601 fatal("Unknown/unsupported operating system."); 602 } 603#elif THE_ISA == SPARC_ISA 604 if (objFile->getArch() != ObjectFile::SPARC64 && 605 objFile->getArch() != ObjectFile::SPARC32) 606 fatal("Object file architecture does not match compiled ISA (SPARC)."); 607 switch (objFile->getOpSys()) { 608 case ObjectFile::UnknownOpSys: 609 warn("Unknown operating system; assuming Linux."); 610 // fall through 611 case ObjectFile::Linux: 612 if (objFile->getArch() == ObjectFile::SPARC64) { 613 process = new Sparc64LinuxProcess(params, objFile); 614 } else { 615 process = new Sparc32LinuxProcess(params, objFile); 616 } 617 break; 618 619 620 case ObjectFile::Solaris: 621 process = new SparcSolarisProcess(params, objFile); 622 break; 623 624 default: 625 fatal("Unknown/unsupported operating system."); 626 } 627#elif THE_ISA == X86_ISA 628 if (objFile->getArch() != ObjectFile::X86_64 && 629 objFile->getArch() != ObjectFile::I386) 630 fatal("Object file architecture does not match compiled ISA (x86)."); 631 switch (objFile->getOpSys()) { 632 case ObjectFile::UnknownOpSys: 633 warn("Unknown operating system; assuming Linux."); 634 // fall through 635 case ObjectFile::Linux: 636 if (objFile->getArch() == ObjectFile::X86_64) { 637 process = new X86_64LinuxProcess(params, objFile); 638 } else { 639 process = new I386LinuxProcess(params, objFile); 640 } 641 break; 642 643 default: 644 fatal("Unknown/unsupported operating system."); 645 } 646#elif THE_ISA == MIPS_ISA 647 if (objFile->getArch() != ObjectFile::Mips) 648 fatal("Object file architecture does not match compiled ISA (MIPS)."); 649 switch (objFile->getOpSys()) { 650 case ObjectFile::UnknownOpSys: 651 warn("Unknown operating system; assuming Linux."); 652 // fall through 653 case ObjectFile::Linux: 654 process = new MipsLinuxProcess(params, objFile); 655 break; 656 657 default: 658 fatal("Unknown/unsupported operating system."); 659 } 660#elif THE_ISA == ARM_ISA 661 ObjectFile::Arch arch = objFile->getArch(); 662 if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb && 663 arch != ObjectFile::Arm64) 664 fatal("Object file architecture does not match compiled ISA (ARM)."); 665 switch (objFile->getOpSys()) { 666 case ObjectFile::UnknownOpSys: 667 warn("Unknown operating system; assuming Linux."); 668 // fall through 669 case ObjectFile::Linux: 670 if (arch == ObjectFile::Arm64) { 671 process = new ArmLinuxProcess64(params, objFile, 672 objFile->getArch()); 673 } else { 674 process = new ArmLinuxProcess32(params, objFile, 675 objFile->getArch()); 676 } 677 break; 678 case ObjectFile::FreeBSD: 679 if (arch == ObjectFile::Arm64) { 680 process = new ArmFreebsdProcess64(params, objFile, 681 objFile->getArch()); 682 } else { 683 process = new ArmFreebsdProcess32(params, objFile, 684 objFile->getArch()); 685 } 686 break; 687 case ObjectFile::LinuxArmOABI: 688 fatal("M5 does not support ARM OABI binaries. Please recompile with an" 689 " EABI compiler."); 690 default: 691 fatal("Unknown/unsupported operating system."); 692 } 693#elif THE_ISA == POWER_ISA 694 if (objFile->getArch() != ObjectFile::Power) 695 fatal("Object file architecture does not match compiled ISA (Power)."); 696 switch (objFile->getOpSys()) { 697 case ObjectFile::UnknownOpSys: 698 warn("Unknown operating system; assuming Linux."); 699 // fall through 700 case ObjectFile::Linux: 701 process = new PowerLinuxProcess(params, objFile); 702 break; 703 704 default: 705 fatal("Unknown/unsupported operating system."); 706 }
| 87#else 88#error "THE_ISA not set" 89#endif 90 91 92using namespace std; 93using namespace TheISA; 94 95// current number of allocated processes 96int num_processes = 0; 97 98template<class IntType> 99 100AuxVector<IntType>::AuxVector(IntType type, IntType val) 101{ 102 a_type = TheISA::htog(type); 103 a_val = TheISA::htog(val); 104} 105 106template struct AuxVector<uint32_t>; 107template struct AuxVector<uint64_t>; 108 109static int 110openFile(const string& filename, int flags, mode_t mode) 111{ 112 int sim_fd = open(filename.c_str(), flags, mode); 113 if (sim_fd != -1) 114 return sim_fd; 115 fatal("Unable to open %s with mode %O", filename, mode); 116} 117 118static int 119openInputFile(const string &filename) 120{ 121 return openFile(filename, O_RDONLY, 0); 122} 123 124static int 125openOutputFile(const string &filename) 126{ 127 return openFile(filename, O_WRONLY | O_CREAT | O_TRUNC, 0664); 128} 129 130Process::Process(ProcessParams * params) 131 : SimObject(params), system(params->system), 132 brk_point(0), stack_base(0), stack_size(0), stack_min(0), 133 max_stack_size(params->max_stack_size), 134 next_thread_stack_base(0), 135 M5_pid(system->allocatePID()), 136 useArchPT(params->useArchPT), 137 kvmInSE(params->kvmInSE), 138 pTable(useArchPT ? 139 static_cast<PageTableBase *>(new ArchPageTable(name(), M5_pid, system)) : 140 static_cast<PageTableBase *>(new FuncPageTable(name(), M5_pid)) ), 141 initVirtMem(system->getSystemPort(), this, 142 SETranslatingPortProxy::Always), 143 fd_array(make_shared<array<FDEntry, NUM_FDS>>()), 144 imap {{"", -1}, 145 {"cin", STDIN_FILENO}, 146 {"stdin", STDIN_FILENO}}, 147 oemap{{"", -1}, 148 {"cout", STDOUT_FILENO}, 149 {"stdout", STDOUT_FILENO}, 150 {"cerr", STDERR_FILENO}, 151 {"stderr", STDERR_FILENO}} 152{ 153 int sim_fd; 154 std::map<string,int>::iterator it; 155 156 // Search through the input options and set fd if match is found; 157 // otherwise, open an input file and seek to location. 158 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO); 159 if ((it = imap.find(params->input)) != imap.end()) 160 sim_fd = it->second; 161 else 162 sim_fd = openInputFile(params->input); 163 fde_stdin->set(sim_fd, params->input, O_RDONLY, -1, false); 164 165 // Search through the output/error options and set fd if match is found; 166 // otherwise, open an output file and seek to location. 167 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO); 168 if ((it = oemap.find(params->output)) != oemap.end()) 169 sim_fd = it->second; 170 else 171 sim_fd = openOutputFile(params->output); 172 fde_stdout->set(sim_fd, params->output, O_WRONLY | O_CREAT | O_TRUNC, 173 0664, false); 174 175 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO); 176 if (params->output == params->errout) 177 // Reuse the same file descriptor if these match. 178 sim_fd = fde_stdout->fd; 179 else if ((it = oemap.find(params->errout)) != oemap.end()) 180 sim_fd = it->second; 181 else 182 sim_fd = openOutputFile(params->errout); 183 fde_stderr->set(sim_fd, params->errout, O_WRONLY | O_CREAT | O_TRUNC, 184 0664, false); 185 186 mmap_end = 0; 187 nxm_start = nxm_end = 0; 188 // other parameters will be initialized when the program is loaded 189} 190 191 192void 193Process::regStats() 194{ 195 SimObject::regStats(); 196 197 using namespace Stats; 198 199 num_syscalls 200 .name(name() + ".num_syscalls") 201 .desc("Number of system calls") 202 ; 203} 204 205void 206Process::inheritFDArray(Process *p) 207{ 208 fd_array = p->fd_array; 209} 210 211ThreadContext * 212Process::findFreeContext() 213{ 214 for (int id : contextIds) { 215 ThreadContext *tc = system->getThreadContext(id); 216 if (tc->status() == ThreadContext::Halted) 217 return tc; 218 } 219 return NULL; 220} 221 222void 223Process::initState() 224{ 225 if (contextIds.empty()) 226 fatal("Process %s is not associated with any HW contexts!\n", name()); 227 228 // first thread context for this process... initialize & enable 229 ThreadContext *tc = system->getThreadContext(contextIds[0]); 230 231 // mark this context as active so it will start ticking. 232 tc->activate(); 233 234 pTable->initState(tc); 235} 236 237DrainState 238Process::drain() 239{ 240 findFileOffsets(); 241 return DrainState::Drained; 242} 243 244int 245Process::allocFD(int sim_fd, const string& filename, int flags, int mode, 246 bool pipe) 247{ 248 for (int free_fd = 0; free_fd < fd_array->size(); free_fd++) { 249 FDEntry *fde = getFDEntry(free_fd); 250 if (fde->isFree()) { 251 fde->set(sim_fd, filename, flags, mode, pipe); 252 return free_fd; 253 } 254 } 255 256 fatal("Out of target file descriptors"); 257} 258 259void 260Process::resetFDEntry(int tgt_fd) 261{ 262 FDEntry *fde = getFDEntry(tgt_fd); 263 assert(fde->fd > -1); 264 265 fde->reset(); 266} 267 268int 269Process::getSimFD(int tgt_fd) 270{ 271 FDEntry *entry = getFDEntry(tgt_fd); 272 return entry ? entry->fd : -1; 273} 274 275FDEntry * 276Process::getFDEntry(int tgt_fd) 277{ 278 assert(0 <= tgt_fd && tgt_fd < fd_array->size()); 279 return &(*fd_array)[tgt_fd]; 280} 281 282int 283Process::getTgtFD(int sim_fd) 284{ 285 for (int index = 0; index < fd_array->size(); index++) 286 if ((*fd_array)[index].fd == sim_fd) 287 return index; 288 return -1; 289} 290 291void 292Process::allocateMem(Addr vaddr, int64_t size, bool clobber) 293{ 294 int npages = divCeil(size, (int64_t)PageBytes); 295 Addr paddr = system->allocPhysPages(npages); 296 pTable->map(vaddr, paddr, size, 297 clobber ? PageTableBase::Clobber : PageTableBase::Zero); 298} 299 300bool 301Process::fixupStackFault(Addr vaddr) 302{ 303 // Check if this is already on the stack and there's just no page there 304 // yet. 305 if (vaddr >= stack_min && vaddr < stack_base) { 306 allocateMem(roundDown(vaddr, PageBytes), PageBytes); 307 return true; 308 } 309 310 // We've accessed the next page of the stack, so extend it to include 311 // this address. 312 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 313 while (vaddr < stack_min) { 314 stack_min -= TheISA::PageBytes; 315 if (stack_base - stack_min > max_stack_size) 316 fatal("Maximum stack size exceeded\n"); 317 allocateMem(stack_min, TheISA::PageBytes); 318 inform("Increasing stack size by one page."); 319 }; 320 return true; 321 } 322 return false; 323} 324 325void 326Process::fixFileOffsets() 327{ 328 auto seek = [] (FDEntry *fde) 329 { 330 if (lseek(fde->fd, fde->fileOffset, SEEK_SET) < 0) 331 fatal("Unable to see to location in %s", fde->filename); 332 }; 333 334 std::map<string,int>::iterator it; 335 336 // Search through the input options and set fd if match is found; 337 // otherwise, open an input file and seek to location. 338 FDEntry *fde_stdin = getFDEntry(STDIN_FILENO); 339 if ((it = imap.find(fde_stdin->filename)) != imap.end()) { 340 fde_stdin->fd = it->second; 341 } else { 342 fde_stdin->fd = openInputFile(fde_stdin->filename); 343 seek(fde_stdin); 344 } 345 346 // Search through the output/error options and set fd if match is found; 347 // otherwise, open an output file and seek to location. 348 FDEntry *fde_stdout = getFDEntry(STDOUT_FILENO); 349 if ((it = oemap.find(fde_stdout->filename)) != oemap.end()) { 350 fde_stdout->fd = it->second; 351 } else { 352 fde_stdout->fd = openOutputFile(fde_stdout->filename); 353 seek(fde_stdout); 354 } 355 356 FDEntry *fde_stderr = getFDEntry(STDERR_FILENO); 357 if (fde_stdout->filename == fde_stderr->filename) { 358 // Reuse the same file descriptor if these match. 359 fde_stderr->fd = fde_stdout->fd; 360 } else if ((it = oemap.find(fde_stderr->filename)) != oemap.end()) { 361 fde_stderr->fd = it->second; 362 } else { 363 fde_stderr->fd = openOutputFile(fde_stderr->filename); 364 seek(fde_stderr); 365 } 366 367 for (int tgt_fd = 3; tgt_fd < fd_array->size(); tgt_fd++) { 368 FDEntry *fde = getFDEntry(tgt_fd); 369 if (fde->fd == -1) 370 continue; 371 372 if (fde->isPipe) { 373 if (fde->filename == "PIPE-WRITE") 374 continue; 375 assert(fde->filename == "PIPE-READ"); 376 377 int fds[2]; 378 if (pipe(fds) < 0) 379 fatal("Unable to create new pipe"); 380 381 fde->fd = fds[0]; 382 383 FDEntry *fde_write = getFDEntry(fde->readPipeSource); 384 assert(fde_write->filename == "PIPE-WRITE"); 385 fde_write->fd = fds[1]; 386 } else { 387 fde->fd = openFile(fde->filename.c_str(), fde->flags, fde->mode); 388 seek(fde); 389 } 390 } 391} 392 393void 394Process::findFileOffsets() 395{ 396 for (auto& fde : *fd_array) { 397 if (fde.fd != -1) 398 fde.fileOffset = lseek(fde.fd, 0, SEEK_CUR); 399 } 400} 401 402void 403Process::setReadPipeSource(int read_pipe_fd, int source_fd) 404{ 405 FDEntry *fde = getFDEntry(read_pipe_fd); 406 assert(source_fd >= -1); 407 fde->readPipeSource = source_fd; 408} 409 410void 411Process::serialize(CheckpointOut &cp) const 412{ 413 SERIALIZE_SCALAR(brk_point); 414 SERIALIZE_SCALAR(stack_base); 415 SERIALIZE_SCALAR(stack_size); 416 SERIALIZE_SCALAR(stack_min); 417 SERIALIZE_SCALAR(next_thread_stack_base); 418 SERIALIZE_SCALAR(mmap_end); 419 SERIALIZE_SCALAR(nxm_start); 420 SERIALIZE_SCALAR(nxm_end); 421 pTable->serialize(cp); 422 for (int x = 0; x < fd_array->size(); x++) { 423 (*fd_array)[x].serializeSection(cp, csprintf("FDEntry%d", x)); 424 } 425 SERIALIZE_SCALAR(M5_pid); 426 427} 428 429void 430Process::unserialize(CheckpointIn &cp) 431{ 432 UNSERIALIZE_SCALAR(brk_point); 433 UNSERIALIZE_SCALAR(stack_base); 434 UNSERIALIZE_SCALAR(stack_size); 435 UNSERIALIZE_SCALAR(stack_min); 436 UNSERIALIZE_SCALAR(next_thread_stack_base); 437 UNSERIALIZE_SCALAR(mmap_end); 438 UNSERIALIZE_SCALAR(nxm_start); 439 UNSERIALIZE_SCALAR(nxm_end); 440 pTable->unserialize(cp); 441 for (int x = 0; x < fd_array->size(); x++) { 442 FDEntry *fde = getFDEntry(x); 443 fde->unserializeSection(cp, csprintf("FDEntry%d", x)); 444 } 445 fixFileOffsets(); 446 UNSERIALIZE_OPT_SCALAR(M5_pid); 447 // The above returns a bool so that you could do something if you don't 448 // find the param in the checkpoint if you wanted to, like set a default 449 // but in this case we'll just stick with the instantiated value if not 450 // found. 451} 452 453 454bool 455Process::map(Addr vaddr, Addr paddr, int size, bool cacheable) 456{ 457 pTable->map(vaddr, paddr, size, 458 cacheable ? PageTableBase::Zero : PageTableBase::Uncacheable); 459 return true; 460} 461 462 463//////////////////////////////////////////////////////////////////////// 464// 465// LiveProcess member definitions 466// 467//////////////////////////////////////////////////////////////////////// 468 469 470LiveProcess::LiveProcess(LiveProcessParams *params, ObjectFile *_objFile) 471 : Process(params), objFile(_objFile), 472 argv(params->cmd), envp(params->env), cwd(params->cwd), 473 executable(params->executable), 474 __uid(params->uid), __euid(params->euid), 475 __gid(params->gid), __egid(params->egid), 476 __pid(params->pid), __ppid(params->ppid), 477 drivers(params->drivers) 478{ 479 480 // load up symbols, if any... these may be used for debugging or 481 // profiling. 482 if (!debugSymbolTable) { 483 debugSymbolTable = new SymbolTable(); 484 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 485 !objFile->loadLocalSymbols(debugSymbolTable) || 486 !objFile->loadWeakSymbols(debugSymbolTable)) { 487 // didn't load any symbols 488 delete debugSymbolTable; 489 debugSymbolTable = NULL; 490 } 491 } 492} 493 494void 495LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 496{ 497 num_syscalls++; 498 499 SyscallDesc *desc = getDesc(callnum); 500 if (desc == NULL) 501 fatal("Syscall %d out of range", callnum); 502 503 desc->doSyscall(callnum, this, tc); 504} 505 506IntReg 507LiveProcess::getSyscallArg(ThreadContext *tc, int &i, int width) 508{ 509 return getSyscallArg(tc, i); 510} 511 512 513EmulatedDriver * 514LiveProcess::findDriver(std::string filename) 515{ 516 for (EmulatedDriver *d : drivers) { 517 if (d->match(filename)) 518 return d; 519 } 520 521 return NULL; 522} 523 524void 525LiveProcess::updateBias() 526{ 527 ObjectFile *interp = objFile->getInterpreter(); 528 529 if (!interp || !interp->relocatable()) 530 return; 531 532 // Determine how large the interpreters footprint will be in the process 533 // address space. 534 Addr interp_mapsize = roundUp(interp->mapSize(), TheISA::PageBytes); 535 536 // We are allocating the memory area; set the bias to the lowest address 537 // in the allocated memory region. 538 Addr ld_bias = mmapGrowsDown() ? mmap_end - interp_mapsize : mmap_end; 539 540 // Adjust the process mmap area to give the interpreter room; the real 541 // execve system call would just invoke the kernel's internal mmap 542 // functions to make these adjustments. 543 mmap_end = mmapGrowsDown() ? ld_bias : mmap_end + interp_mapsize; 544 545 interp->updateBias(ld_bias); 546} 547 548 549ObjectFile * 550LiveProcess::getInterpreter() 551{ 552 return objFile->getInterpreter(); 553} 554 555 556Addr 557LiveProcess::getBias() 558{ 559 ObjectFile *interp = getInterpreter(); 560 561 return interp ? interp->bias() : objFile->bias(); 562} 563 564 565Addr 566LiveProcess::getStartPC() 567{ 568 ObjectFile *interp = getInterpreter(); 569 570 return interp ? interp->entryPoint() : objFile->entryPoint(); 571} 572 573 574LiveProcess * 575LiveProcess::create(LiveProcessParams * params) 576{ 577 LiveProcess *process = NULL; 578 579 // If not specified, set the executable parameter equal to the 580 // simulated system's zeroth command line parameter 581 if (params->executable == "") { 582 params->executable = params->cmd[0]; 583 } 584 585 ObjectFile *objFile = createObjectFile(params->executable); 586 if (objFile == NULL) { 587 fatal("Can't load object file %s", params->executable); 588 } 589 590#if THE_ISA == ALPHA_ISA 591 if (objFile->getArch() != ObjectFile::Alpha) 592 fatal("Object file architecture does not match compiled ISA (Alpha)."); 593 594 switch (objFile->getOpSys()) { 595 case ObjectFile::UnknownOpSys: 596 warn("Unknown operating system; assuming Linux."); 597 // fall through 598 case ObjectFile::Linux: 599 process = new AlphaLinuxProcess(params, objFile); 600 break; 601 602 default: 603 fatal("Unknown/unsupported operating system."); 604 } 605#elif THE_ISA == SPARC_ISA 606 if (objFile->getArch() != ObjectFile::SPARC64 && 607 objFile->getArch() != ObjectFile::SPARC32) 608 fatal("Object file architecture does not match compiled ISA (SPARC)."); 609 switch (objFile->getOpSys()) { 610 case ObjectFile::UnknownOpSys: 611 warn("Unknown operating system; assuming Linux."); 612 // fall through 613 case ObjectFile::Linux: 614 if (objFile->getArch() == ObjectFile::SPARC64) { 615 process = new Sparc64LinuxProcess(params, objFile); 616 } else { 617 process = new Sparc32LinuxProcess(params, objFile); 618 } 619 break; 620 621 622 case ObjectFile::Solaris: 623 process = new SparcSolarisProcess(params, objFile); 624 break; 625 626 default: 627 fatal("Unknown/unsupported operating system."); 628 } 629#elif THE_ISA == X86_ISA 630 if (objFile->getArch() != ObjectFile::X86_64 && 631 objFile->getArch() != ObjectFile::I386) 632 fatal("Object file architecture does not match compiled ISA (x86)."); 633 switch (objFile->getOpSys()) { 634 case ObjectFile::UnknownOpSys: 635 warn("Unknown operating system; assuming Linux."); 636 // fall through 637 case ObjectFile::Linux: 638 if (objFile->getArch() == ObjectFile::X86_64) { 639 process = new X86_64LinuxProcess(params, objFile); 640 } else { 641 process = new I386LinuxProcess(params, objFile); 642 } 643 break; 644 645 default: 646 fatal("Unknown/unsupported operating system."); 647 } 648#elif THE_ISA == MIPS_ISA 649 if (objFile->getArch() != ObjectFile::Mips) 650 fatal("Object file architecture does not match compiled ISA (MIPS)."); 651 switch (objFile->getOpSys()) { 652 case ObjectFile::UnknownOpSys: 653 warn("Unknown operating system; assuming Linux."); 654 // fall through 655 case ObjectFile::Linux: 656 process = new MipsLinuxProcess(params, objFile); 657 break; 658 659 default: 660 fatal("Unknown/unsupported operating system."); 661 } 662#elif THE_ISA == ARM_ISA 663 ObjectFile::Arch arch = objFile->getArch(); 664 if (arch != ObjectFile::Arm && arch != ObjectFile::Thumb && 665 arch != ObjectFile::Arm64) 666 fatal("Object file architecture does not match compiled ISA (ARM)."); 667 switch (objFile->getOpSys()) { 668 case ObjectFile::UnknownOpSys: 669 warn("Unknown operating system; assuming Linux."); 670 // fall through 671 case ObjectFile::Linux: 672 if (arch == ObjectFile::Arm64) { 673 process = new ArmLinuxProcess64(params, objFile, 674 objFile->getArch()); 675 } else { 676 process = new ArmLinuxProcess32(params, objFile, 677 objFile->getArch()); 678 } 679 break; 680 case ObjectFile::FreeBSD: 681 if (arch == ObjectFile::Arm64) { 682 process = new ArmFreebsdProcess64(params, objFile, 683 objFile->getArch()); 684 } else { 685 process = new ArmFreebsdProcess32(params, objFile, 686 objFile->getArch()); 687 } 688 break; 689 case ObjectFile::LinuxArmOABI: 690 fatal("M5 does not support ARM OABI binaries. Please recompile with an" 691 " EABI compiler."); 692 default: 693 fatal("Unknown/unsupported operating system."); 694 } 695#elif THE_ISA == POWER_ISA 696 if (objFile->getArch() != ObjectFile::Power) 697 fatal("Object file architecture does not match compiled ISA (Power)."); 698 switch (objFile->getOpSys()) { 699 case ObjectFile::UnknownOpSys: 700 warn("Unknown operating system; assuming Linux."); 701 // fall through 702 case ObjectFile::Linux: 703 process = new PowerLinuxProcess(params, objFile); 704 break; 705 706 default: 707 fatal("Unknown/unsupported operating system."); 708 }
|