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 }
|