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#include <string> 36 37#include "arch/remote_gdb.hh" 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 "params/Process.hh" 48#include "params/LiveProcess.hh" 49#include "sim/debug.hh" 50#include "sim/process.hh" 51#include "sim/process_impl.hh" 52#include "sim/stats.hh" 53#include "sim/syscall_emul.hh" 54#include "sim/system.hh" 55 56#include "arch/isa_specific.hh" 57#if THE_ISA == ALPHA_ISA 58#include "arch/alpha/linux/process.hh" 59#include "arch/alpha/tru64/process.hh" 60#elif THE_ISA == SPARC_ISA 61#include "arch/sparc/linux/process.hh" 62#include "arch/sparc/solaris/process.hh" 63#elif THE_ISA == MIPS_ISA 64#include "arch/mips/linux/process.hh" 65#elif THE_ISA == ARM_ISA 66#include "arch/arm/linux/process.hh" 67#elif THE_ISA == X86_ISA 68#include "arch/x86/linux/process.hh" 69#else 70#error "THE_ISA not set" 71#endif 72 73 74using namespace std; 75using namespace TheISA; 76 77// 78// The purpose of this code is to fake the loader & syscall mechanism 79// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 80// mode when we do have an OS 81// 82#if FULL_SYSTEM 83#error "process.cc not compatible with FULL_SYSTEM" 84#endif 85 86// current number of allocated processes 87int num_processes = 0; 88 89Process::Process(ProcessParams * params) 90 : SimObject(params), system(params->system), checkpointRestored(false), 91 max_stack_size(params->max_stack_size) 92{ 93 string in = params->input; 94 string out = params->output; 95 string err = params->errout; 96 97 // initialize file descriptors to default: same as simulator 98 int stdin_fd, stdout_fd, stderr_fd; 99 100 if (in == "stdin" || in == "cin") 101 stdin_fd = STDIN_FILENO; 102 else if (in == "None") 103 stdin_fd = -1; 104 else 105 stdin_fd = Process::openInputFile(in); 106 107 if (out == "stdout" || out == "cout") 108 stdout_fd = STDOUT_FILENO; 109 else if (out == "stderr" || out == "cerr") 110 stdout_fd = STDERR_FILENO; 111 else if (out == "None") 112 stdout_fd = -1; 113 else 114 stdout_fd = Process::openOutputFile(out); 115 116 if (err == "stdout" || err == "cout") 117 stderr_fd = STDOUT_FILENO; 118 else if (err == "stderr" || err == "cerr") 119 stderr_fd = STDERR_FILENO; 120 else if (err == "None") 121 stderr_fd = -1; 122 else if (err == out) 123 stderr_fd = stdout_fd; 124 else 125 stderr_fd = Process::openOutputFile(err); 126 127 M5_pid = system->allocatePID(); 128 // initialize first 3 fds (stdin, stdout, stderr) 129 Process::FdMap *fdo = &fd_map[STDIN_FILENO]; 130 fdo->fd = stdin_fd; 131 fdo->filename = in; 132 fdo->flags = O_RDONLY; 133 fdo->mode = -1; 134 fdo->fileOffset = 0; 135 136 fdo = &fd_map[STDOUT_FILENO]; 137 fdo->fd = stdout_fd; 138 fdo->filename = out; 139 fdo->flags = O_WRONLY | O_CREAT | O_TRUNC; 140 fdo->mode = 0774; 141 fdo->fileOffset = 0; 142 143 fdo = &fd_map[STDERR_FILENO]; 144 fdo->fd = stderr_fd; 145 fdo->filename = err; 146 fdo->flags = O_WRONLY; 147 fdo->mode = -1; 148 fdo->fileOffset = 0; 149 150 151 // mark remaining fds as free 152 for (int i = 3; i <= MAX_FD; ++i) { 153 Process::FdMap *fdo = &fd_map[i]; 154 fdo->fd = -1; 155 } 156 157 mmap_start = mmap_end = 0; 158 nxm_start = nxm_end = 0; 159 pTable = new PageTable(this); 160 // other parameters will be initialized when the program is loaded 161} 162 163 164void 165Process::regStats() 166{ 167 using namespace Stats; 168 169 num_syscalls 170 .name(name() + ".PROG:num_syscalls") 171 .desc("Number of system calls") 172 ; 173} 174 175// 176// static helper functions 177// 178int 179Process::openInputFile(const string &filename) 180{ 181 int fd = open(filename.c_str(), O_RDONLY); 182 183 if (fd == -1) { 184 perror(NULL); 185 cerr << "unable to open \"" << filename << "\" for reading\n"; 186 fatal("can't open input file"); 187 } 188 189 return fd; 190} 191 192 193int 194Process::openOutputFile(const string &filename) 195{ 196 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664); 197 198 if (fd == -1) { 199 perror(NULL); 200 cerr << "unable to open \"" << filename << "\" for writing\n"; 201 fatal("can't open output file"); 202 } 203 204 return fd; 205} 206 207ThreadContext * 208Process::findFreeContext() 209{ 210 int size = contextIds.size(); 211 ThreadContext *tc; 212 for (int i = 0; i < size; ++i) { 213 tc = system->getThreadContext(contextIds[i]); 214 if (tc->status() == ThreadContext::Unallocated) { 215 // inactive context, free to use 216 return tc; 217 } 218 } 219 return NULL; 220} 221 222void 223Process::startup() 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(0); 233 234 Port *mem_port; 235 mem_port = system->physmem->getPort("functional"); 236 initVirtMem = new TranslatingPort("process init port", this, 237 TranslatingPort::Always); 238 mem_port->setPeer(initVirtMem); 239 initVirtMem->setPeer(mem_port); 240} 241 242// map simulator fd sim_fd to target fd tgt_fd 243void 244Process::dup_fd(int sim_fd, int tgt_fd) 245{ 246 if (tgt_fd < 0 || tgt_fd > MAX_FD) 247 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 248 249 Process::FdMap *fdo = &fd_map[tgt_fd]; 250 fdo->fd = sim_fd; 251} 252 253 254// generate new target fd for sim_fd 255int 256Process::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe) 257{ 258 // in case open() returns an error, don't allocate a new fd 259 if (sim_fd == -1) 260 return -1; 261 262 // find first free target fd 263 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 264 Process::FdMap *fdo = &fd_map[free_fd]; 265 if (fdo->fd == -1) { 266 fdo->fd = sim_fd; 267 fdo->filename = filename; 268 fdo->mode = mode; 269 fdo->fileOffset = 0; 270 fdo->flags = flags; 271 fdo->isPipe = pipe; 272 fdo->readPipeSource = 0; 273 return free_fd; 274 } 275 } 276 277 panic("Process::alloc_fd: out of file descriptors!"); 278} 279 280 281// free target fd (e.g., after close) 282void 283Process::free_fd(int tgt_fd) 284{ 285 Process::FdMap *fdo = &fd_map[tgt_fd]; 286 if (fdo->fd == -1) 287 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 288 289 fdo->fd = -1; 290 fdo->filename = "NULL"; 291 fdo->mode = 0; 292 fdo->fileOffset = 0; 293 fdo->flags = 0; 294 fdo->isPipe = false; 295 fdo->readPipeSource = 0; 296} 297 298 299// look up simulator fd for given target fd 300int 301Process::sim_fd(int tgt_fd) 302{ 303 if (tgt_fd > MAX_FD) 304 return -1; 305 306 return fd_map[tgt_fd].fd; 307} 308 309Process::FdMap * 310Process::sim_fd_obj(int tgt_fd) 311{ 312 if (tgt_fd > MAX_FD) 313 panic("sim_fd_obj called in fd out of range."); 314 315 return &fd_map[tgt_fd]; 316} 317bool 318Process::checkAndAllocNextPage(Addr vaddr) 319{ 320 // if this is an initial write we might not have 321 if (vaddr >= stack_min && vaddr < stack_base) { 322 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 323 return true; 324 } 325 326 // We've accessed the next page of the stack, so extend the stack 327 // to cover it. 328 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 329 while (vaddr < stack_min) { 330 stack_min -= TheISA::PageBytes; 331 if(stack_base - stack_min > max_stack_size) 332 fatal("Maximum stack size exceeded\n"); 333 if(stack_base - stack_min > 8*1024*1024) 334 fatal("Over max stack size for one thread\n"); 335 pTable->allocate(stack_min, TheISA::PageBytes); 336 warn("Increasing stack size by one page."); 337 }; 338 return true; 339 } 340 return false; 341} 342 343 // find all offsets for currently open files and save them 344void 345Process::fix_file_offsets() { 346 Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO]; 347 Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO]; 348 Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO]; 349 string in = fdo_stdin->filename; 350 string out = fdo_stdout->filename; 351 string err = fdo_stderr->filename; 352 353 // initialize file descriptors to default: same as simulator 354 int stdin_fd, stdout_fd, stderr_fd; 355 356 if (in == "stdin" || in == "cin") 357 stdin_fd = STDIN_FILENO; 358 else if (in == "None") 359 stdin_fd = -1; 360 else{ 361 //OPEN standard in and seek to the right location 362 stdin_fd = Process::openInputFile(in); 363 if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0) 364 panic("Unable to seek to correct location in file: %s", in); 365 } 366 367 if (out == "stdout" || out == "cout") 368 stdout_fd = STDOUT_FILENO; 369 else if (out == "stderr" || out == "cerr") 370 stdout_fd = STDERR_FILENO; 371 else if (out == "None") 372 stdout_fd = -1; 373 else{ 374 stdout_fd = Process::openOutputFile(out); 375 if (lseek(stdout_fd, fdo_stdout->fileOffset, SEEK_SET) < 0) 376 panic("Unable to seek to correct location in file: %s", out); 377 } 378 379 if (err == "stdout" || err == "cout") 380 stderr_fd = STDOUT_FILENO; 381 else if (err == "stderr" || err == "cerr") 382 stderr_fd = STDERR_FILENO; 383 else if (err == "None") 384 stderr_fd = -1; 385 else if (err == out) 386 stderr_fd = stdout_fd; 387 else { 388 stderr_fd = Process::openOutputFile(err); 389 if (lseek(stderr_fd, fdo_stderr->fileOffset, SEEK_SET) < 0) 390 panic("Unable to seek to correct location in file: %s", err); 391 } 392 393 fdo_stdin->fd = stdin_fd; 394 fdo_stdout->fd = stdout_fd; 395 fdo_stderr->fd = stderr_fd; 396 397 398 for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) { 399 Process::FdMap *fdo = &fd_map[free_fd]; 400 if (fdo->fd != -1) { 401 if (fdo->isPipe){ 402 if (fdo->filename == "PIPE-WRITE") 403 continue; 404 else { 405 assert (fdo->filename == "PIPE-READ"); 406 //create a new pipe 407 int fds[2]; 408 int pipe_retval = pipe(fds); 409 410 if (pipe_retval < 0) { 411 // error 412 panic("Unable to create new pipe."); 413 } 414 fdo->fd = fds[0]; //set read pipe 415 Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource]; 416 if (fdo_write->filename != "PIPE-WRITE") 417 panic ("Couldn't find write end of the pipe"); 418 419 fdo_write->fd = fds[1];//set write pipe 420 } 421 } else { 422 //Open file 423 int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode); 424 425 if (fd == -1) 426 panic("Unable to open file: %s", fdo->filename); 427 fdo->fd = fd; 428 429 //Seek to correct location before checkpoint 430 if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0) 431 panic("Unable to seek to correct location in file: %s", fdo->filename); 432 } 433 } 434 } 435} 436void 437Process::find_file_offsets(){ 438 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 439 Process::FdMap *fdo = &fd_map[free_fd]; 440 if (fdo->fd != -1) { 441 fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR); 442 } else { 443 fdo->filename = "NULL"; 444 fdo->fileOffset = 0; 445 } 446 } 447} 448 449void 450Process::setReadPipeSource(int read_pipe_fd, int source_fd){ 451 Process::FdMap *fdo = &fd_map[read_pipe_fd]; 452 fdo->readPipeSource = source_fd; 453} 454 455void 456Process::FdMap::serialize(std::ostream &os) 457{ 458 SERIALIZE_SCALAR(fd); 459 SERIALIZE_SCALAR(isPipe); 460 SERIALIZE_SCALAR(filename); 461 SERIALIZE_SCALAR(flags); 462 SERIALIZE_SCALAR(readPipeSource); 463 SERIALIZE_SCALAR(fileOffset); 464} 465 466void 467Process::FdMap::unserialize(Checkpoint *cp, const std::string §ion) 468{ 469 UNSERIALIZE_SCALAR(fd); 470 UNSERIALIZE_SCALAR(isPipe); 471 UNSERIALIZE_SCALAR(filename); 472 UNSERIALIZE_SCALAR(flags); 473 UNSERIALIZE_SCALAR(readPipeSource); 474 UNSERIALIZE_SCALAR(fileOffset); 475} 476 477void 478Process::serialize(std::ostream &os) 479{ 480 SERIALIZE_SCALAR(initialContextLoaded); 481 SERIALIZE_SCALAR(brk_point); 482 SERIALIZE_SCALAR(stack_base); 483 SERIALIZE_SCALAR(stack_size); 484 SERIALIZE_SCALAR(stack_min); 485 SERIALIZE_SCALAR(next_thread_stack_base); 486 SERIALIZE_SCALAR(mmap_start); 487 SERIALIZE_SCALAR(mmap_end); 488 SERIALIZE_SCALAR(nxm_start); 489 SERIALIZE_SCALAR(nxm_end); 490 find_file_offsets(); 491 pTable->serialize(os); 492 for (int x = 0; x <= MAX_FD; x++) { 493 nameOut(os, csprintf("%s.FdMap%d", name(), x)); 494 fd_map[x].serialize(os); 495 } 496 497} 498 499void 500Process::unserialize(Checkpoint *cp, const std::string §ion) 501{ 502 UNSERIALIZE_SCALAR(initialContextLoaded); 503 UNSERIALIZE_SCALAR(brk_point); 504 UNSERIALIZE_SCALAR(stack_base); 505 UNSERIALIZE_SCALAR(stack_size); 506 UNSERIALIZE_SCALAR(stack_min); 507 UNSERIALIZE_SCALAR(next_thread_stack_base); 508 UNSERIALIZE_SCALAR(mmap_start); 509 UNSERIALIZE_SCALAR(mmap_end); 510 UNSERIALIZE_SCALAR(nxm_start); 511 UNSERIALIZE_SCALAR(nxm_end); 512 pTable->unserialize(cp, section); 513 for (int x = 0; x <= MAX_FD; x++) { 514 fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x)); 515 } 516 fix_file_offsets(); 517 518 checkpointRestored = true; 519 520} 521 522 523//////////////////////////////////////////////////////////////////////// 524// 525// LiveProcess member definitions 526// 527//////////////////////////////////////////////////////////////////////// 528 529 530LiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile) 531 : Process(params), objFile(_objFile), 532 argv(params->cmd), envp(params->env), cwd(params->cwd) 533{ 534 __uid = params->uid; 535 __euid = params->euid; 536 __gid = params->gid; 537 __egid = params->egid; 538 __pid = params->pid; 539 __ppid = params->ppid; 540 541 prog_fname = params->cmd[0]; 542 543 // load up symbols, if any... these may be used for debugging or 544 // profiling. 545 if (!debugSymbolTable) { 546 debugSymbolTable = new SymbolTable(); 547 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 548 !objFile->loadLocalSymbols(debugSymbolTable)) { 549 // didn't load any symbols 550 delete debugSymbolTable; 551 debugSymbolTable = NULL; 552 } 553 } 554} 555 556void 557LiveProcess::argsInit(int intSize, int pageSize) 558{ 559 Process::startup(); 560 561 // load object file into target memory 562 objFile->loadSections(initVirtMem); 563 564 // Calculate how much space we need for arg & env arrays. 565 int argv_array_size = intSize * (argv.size() + 1); 566 int envp_array_size = intSize * (envp.size() + 1); 567 int arg_data_size = 0; 568 for (int i = 0; i < argv.size(); ++i) { 569 arg_data_size += argv[i].size() + 1; 570 } 571 int env_data_size = 0; 572 for (int i = 0; i < envp.size(); ++i) { 573 env_data_size += envp[i].size() + 1; 574 } 575 576 int space_needed = 577 argv_array_size + envp_array_size + arg_data_size + env_data_size; 578 if (space_needed < 32*1024) 579 space_needed = 32*1024; 580 581 // set bottom of stack 582 stack_min = stack_base - space_needed; 583 // align it 584 stack_min = roundDown(stack_min, pageSize); 585 stack_size = stack_base - stack_min; 586 // map memory 587 pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 588 589 // map out initial stack contents 590 Addr argv_array_base = stack_min + intSize; // room for argc 591 Addr envp_array_base = argv_array_base + argv_array_size; 592 Addr arg_data_base = envp_array_base + envp_array_size; 593 Addr env_data_base = arg_data_base + arg_data_size; 594 595 // write contents to stack 596 uint64_t argc = argv.size(); 597 if (intSize == 8) 598 argc = htog((uint64_t)argc); 599 else if (intSize == 4) 600 argc = htog((uint32_t)argc); 601 else 602 panic("Unknown int size"); 603 604 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 605 606 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 607 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 608 609 assert(NumArgumentRegs >= 2); 610 611 ThreadContext *tc = system->getThreadContext(contextIds[0]); 612 613 tc->setIntReg(ArgumentReg[0], argc); 614 tc->setIntReg(ArgumentReg[1], argv_array_base); 615 tc->setIntReg(StackPointerReg, stack_min); 616 617 Addr prog_entry = objFile->entryPoint(); 618 tc->setPC(prog_entry); 619 tc->setNextPC(prog_entry + sizeof(MachInst)); 620 621#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 622 tc->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 623#endif 624 625 num_processes++; 626} 627 628void 629LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 630{ 631 num_syscalls++; 632 633 SyscallDesc *desc = getDesc(callnum); 634 if (desc == NULL) 635 fatal("Syscall %d out of range", callnum); 636 637 desc->doSyscall(callnum, this, tc); 638} 639 640LiveProcess * 641LiveProcess::create(LiveProcessParams * params) 642{ 643 LiveProcess *process = NULL; 644 645 string executable = 646 params->executable == "" ? params->cmd[0] : params->executable; 647 ObjectFile *objFile = createObjectFile(executable); 648 if (objFile == NULL) { 649 fatal("Can't load object file %s", executable); 650 } 651 652 if (objFile->isDynamic()) 653 fatal("Object file is a dynamic executable however only static " 654 "executables are supported!\n Please recompile your " 655 "executable as a static binary and try again.\n"); 656 657#if THE_ISA == ALPHA_ISA
| 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#include <string> 36 37#include "arch/remote_gdb.hh" 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 "params/Process.hh" 48#include "params/LiveProcess.hh" 49#include "sim/debug.hh" 50#include "sim/process.hh" 51#include "sim/process_impl.hh" 52#include "sim/stats.hh" 53#include "sim/syscall_emul.hh" 54#include "sim/system.hh" 55 56#include "arch/isa_specific.hh" 57#if THE_ISA == ALPHA_ISA 58#include "arch/alpha/linux/process.hh" 59#include "arch/alpha/tru64/process.hh" 60#elif THE_ISA == SPARC_ISA 61#include "arch/sparc/linux/process.hh" 62#include "arch/sparc/solaris/process.hh" 63#elif THE_ISA == MIPS_ISA 64#include "arch/mips/linux/process.hh" 65#elif THE_ISA == ARM_ISA 66#include "arch/arm/linux/process.hh" 67#elif THE_ISA == X86_ISA 68#include "arch/x86/linux/process.hh" 69#else 70#error "THE_ISA not set" 71#endif 72 73 74using namespace std; 75using namespace TheISA; 76 77// 78// The purpose of this code is to fake the loader & syscall mechanism 79// when there's no OS: thus there's no resone to use it in FULL_SYSTEM 80// mode when we do have an OS 81// 82#if FULL_SYSTEM 83#error "process.cc not compatible with FULL_SYSTEM" 84#endif 85 86// current number of allocated processes 87int num_processes = 0; 88 89Process::Process(ProcessParams * params) 90 : SimObject(params), system(params->system), checkpointRestored(false), 91 max_stack_size(params->max_stack_size) 92{ 93 string in = params->input; 94 string out = params->output; 95 string err = params->errout; 96 97 // initialize file descriptors to default: same as simulator 98 int stdin_fd, stdout_fd, stderr_fd; 99 100 if (in == "stdin" || in == "cin") 101 stdin_fd = STDIN_FILENO; 102 else if (in == "None") 103 stdin_fd = -1; 104 else 105 stdin_fd = Process::openInputFile(in); 106 107 if (out == "stdout" || out == "cout") 108 stdout_fd = STDOUT_FILENO; 109 else if (out == "stderr" || out == "cerr") 110 stdout_fd = STDERR_FILENO; 111 else if (out == "None") 112 stdout_fd = -1; 113 else 114 stdout_fd = Process::openOutputFile(out); 115 116 if (err == "stdout" || err == "cout") 117 stderr_fd = STDOUT_FILENO; 118 else if (err == "stderr" || err == "cerr") 119 stderr_fd = STDERR_FILENO; 120 else if (err == "None") 121 stderr_fd = -1; 122 else if (err == out) 123 stderr_fd = stdout_fd; 124 else 125 stderr_fd = Process::openOutputFile(err); 126 127 M5_pid = system->allocatePID(); 128 // initialize first 3 fds (stdin, stdout, stderr) 129 Process::FdMap *fdo = &fd_map[STDIN_FILENO]; 130 fdo->fd = stdin_fd; 131 fdo->filename = in; 132 fdo->flags = O_RDONLY; 133 fdo->mode = -1; 134 fdo->fileOffset = 0; 135 136 fdo = &fd_map[STDOUT_FILENO]; 137 fdo->fd = stdout_fd; 138 fdo->filename = out; 139 fdo->flags = O_WRONLY | O_CREAT | O_TRUNC; 140 fdo->mode = 0774; 141 fdo->fileOffset = 0; 142 143 fdo = &fd_map[STDERR_FILENO]; 144 fdo->fd = stderr_fd; 145 fdo->filename = err; 146 fdo->flags = O_WRONLY; 147 fdo->mode = -1; 148 fdo->fileOffset = 0; 149 150 151 // mark remaining fds as free 152 for (int i = 3; i <= MAX_FD; ++i) { 153 Process::FdMap *fdo = &fd_map[i]; 154 fdo->fd = -1; 155 } 156 157 mmap_start = mmap_end = 0; 158 nxm_start = nxm_end = 0; 159 pTable = new PageTable(this); 160 // other parameters will be initialized when the program is loaded 161} 162 163 164void 165Process::regStats() 166{ 167 using namespace Stats; 168 169 num_syscalls 170 .name(name() + ".PROG:num_syscalls") 171 .desc("Number of system calls") 172 ; 173} 174 175// 176// static helper functions 177// 178int 179Process::openInputFile(const string &filename) 180{ 181 int fd = open(filename.c_str(), O_RDONLY); 182 183 if (fd == -1) { 184 perror(NULL); 185 cerr << "unable to open \"" << filename << "\" for reading\n"; 186 fatal("can't open input file"); 187 } 188 189 return fd; 190} 191 192 193int 194Process::openOutputFile(const string &filename) 195{ 196 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0664); 197 198 if (fd == -1) { 199 perror(NULL); 200 cerr << "unable to open \"" << filename << "\" for writing\n"; 201 fatal("can't open output file"); 202 } 203 204 return fd; 205} 206 207ThreadContext * 208Process::findFreeContext() 209{ 210 int size = contextIds.size(); 211 ThreadContext *tc; 212 for (int i = 0; i < size; ++i) { 213 tc = system->getThreadContext(contextIds[i]); 214 if (tc->status() == ThreadContext::Unallocated) { 215 // inactive context, free to use 216 return tc; 217 } 218 } 219 return NULL; 220} 221 222void 223Process::startup() 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(0); 233 234 Port *mem_port; 235 mem_port = system->physmem->getPort("functional"); 236 initVirtMem = new TranslatingPort("process init port", this, 237 TranslatingPort::Always); 238 mem_port->setPeer(initVirtMem); 239 initVirtMem->setPeer(mem_port); 240} 241 242// map simulator fd sim_fd to target fd tgt_fd 243void 244Process::dup_fd(int sim_fd, int tgt_fd) 245{ 246 if (tgt_fd < 0 || tgt_fd > MAX_FD) 247 panic("Process::dup_fd tried to dup past MAX_FD (%d)", tgt_fd); 248 249 Process::FdMap *fdo = &fd_map[tgt_fd]; 250 fdo->fd = sim_fd; 251} 252 253 254// generate new target fd for sim_fd 255int 256Process::alloc_fd(int sim_fd, string filename, int flags, int mode, bool pipe) 257{ 258 // in case open() returns an error, don't allocate a new fd 259 if (sim_fd == -1) 260 return -1; 261 262 // find first free target fd 263 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 264 Process::FdMap *fdo = &fd_map[free_fd]; 265 if (fdo->fd == -1) { 266 fdo->fd = sim_fd; 267 fdo->filename = filename; 268 fdo->mode = mode; 269 fdo->fileOffset = 0; 270 fdo->flags = flags; 271 fdo->isPipe = pipe; 272 fdo->readPipeSource = 0; 273 return free_fd; 274 } 275 } 276 277 panic("Process::alloc_fd: out of file descriptors!"); 278} 279 280 281// free target fd (e.g., after close) 282void 283Process::free_fd(int tgt_fd) 284{ 285 Process::FdMap *fdo = &fd_map[tgt_fd]; 286 if (fdo->fd == -1) 287 warn("Process::free_fd: request to free unused fd %d", tgt_fd); 288 289 fdo->fd = -1; 290 fdo->filename = "NULL"; 291 fdo->mode = 0; 292 fdo->fileOffset = 0; 293 fdo->flags = 0; 294 fdo->isPipe = false; 295 fdo->readPipeSource = 0; 296} 297 298 299// look up simulator fd for given target fd 300int 301Process::sim_fd(int tgt_fd) 302{ 303 if (tgt_fd > MAX_FD) 304 return -1; 305 306 return fd_map[tgt_fd].fd; 307} 308 309Process::FdMap * 310Process::sim_fd_obj(int tgt_fd) 311{ 312 if (tgt_fd > MAX_FD) 313 panic("sim_fd_obj called in fd out of range."); 314 315 return &fd_map[tgt_fd]; 316} 317bool 318Process::checkAndAllocNextPage(Addr vaddr) 319{ 320 // if this is an initial write we might not have 321 if (vaddr >= stack_min && vaddr < stack_base) { 322 pTable->allocate(roundDown(vaddr, VMPageSize), VMPageSize); 323 return true; 324 } 325 326 // We've accessed the next page of the stack, so extend the stack 327 // to cover it. 328 if (vaddr < stack_min && vaddr >= stack_base - max_stack_size) { 329 while (vaddr < stack_min) { 330 stack_min -= TheISA::PageBytes; 331 if(stack_base - stack_min > max_stack_size) 332 fatal("Maximum stack size exceeded\n"); 333 if(stack_base - stack_min > 8*1024*1024) 334 fatal("Over max stack size for one thread\n"); 335 pTable->allocate(stack_min, TheISA::PageBytes); 336 warn("Increasing stack size by one page."); 337 }; 338 return true; 339 } 340 return false; 341} 342 343 // find all offsets for currently open files and save them 344void 345Process::fix_file_offsets() { 346 Process::FdMap *fdo_stdin = &fd_map[STDIN_FILENO]; 347 Process::FdMap *fdo_stdout = &fd_map[STDOUT_FILENO]; 348 Process::FdMap *fdo_stderr = &fd_map[STDERR_FILENO]; 349 string in = fdo_stdin->filename; 350 string out = fdo_stdout->filename; 351 string err = fdo_stderr->filename; 352 353 // initialize file descriptors to default: same as simulator 354 int stdin_fd, stdout_fd, stderr_fd; 355 356 if (in == "stdin" || in == "cin") 357 stdin_fd = STDIN_FILENO; 358 else if (in == "None") 359 stdin_fd = -1; 360 else{ 361 //OPEN standard in and seek to the right location 362 stdin_fd = Process::openInputFile(in); 363 if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0) 364 panic("Unable to seek to correct location in file: %s", in); 365 } 366 367 if (out == "stdout" || out == "cout") 368 stdout_fd = STDOUT_FILENO; 369 else if (out == "stderr" || out == "cerr") 370 stdout_fd = STDERR_FILENO; 371 else if (out == "None") 372 stdout_fd = -1; 373 else{ 374 stdout_fd = Process::openOutputFile(out); 375 if (lseek(stdout_fd, fdo_stdout->fileOffset, SEEK_SET) < 0) 376 panic("Unable to seek to correct location in file: %s", out); 377 } 378 379 if (err == "stdout" || err == "cout") 380 stderr_fd = STDOUT_FILENO; 381 else if (err == "stderr" || err == "cerr") 382 stderr_fd = STDERR_FILENO; 383 else if (err == "None") 384 stderr_fd = -1; 385 else if (err == out) 386 stderr_fd = stdout_fd; 387 else { 388 stderr_fd = Process::openOutputFile(err); 389 if (lseek(stderr_fd, fdo_stderr->fileOffset, SEEK_SET) < 0) 390 panic("Unable to seek to correct location in file: %s", err); 391 } 392 393 fdo_stdin->fd = stdin_fd; 394 fdo_stdout->fd = stdout_fd; 395 fdo_stderr->fd = stderr_fd; 396 397 398 for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) { 399 Process::FdMap *fdo = &fd_map[free_fd]; 400 if (fdo->fd != -1) { 401 if (fdo->isPipe){ 402 if (fdo->filename == "PIPE-WRITE") 403 continue; 404 else { 405 assert (fdo->filename == "PIPE-READ"); 406 //create a new pipe 407 int fds[2]; 408 int pipe_retval = pipe(fds); 409 410 if (pipe_retval < 0) { 411 // error 412 panic("Unable to create new pipe."); 413 } 414 fdo->fd = fds[0]; //set read pipe 415 Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource]; 416 if (fdo_write->filename != "PIPE-WRITE") 417 panic ("Couldn't find write end of the pipe"); 418 419 fdo_write->fd = fds[1];//set write pipe 420 } 421 } else { 422 //Open file 423 int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode); 424 425 if (fd == -1) 426 panic("Unable to open file: %s", fdo->filename); 427 fdo->fd = fd; 428 429 //Seek to correct location before checkpoint 430 if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0) 431 panic("Unable to seek to correct location in file: %s", fdo->filename); 432 } 433 } 434 } 435} 436void 437Process::find_file_offsets(){ 438 for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) { 439 Process::FdMap *fdo = &fd_map[free_fd]; 440 if (fdo->fd != -1) { 441 fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR); 442 } else { 443 fdo->filename = "NULL"; 444 fdo->fileOffset = 0; 445 } 446 } 447} 448 449void 450Process::setReadPipeSource(int read_pipe_fd, int source_fd){ 451 Process::FdMap *fdo = &fd_map[read_pipe_fd]; 452 fdo->readPipeSource = source_fd; 453} 454 455void 456Process::FdMap::serialize(std::ostream &os) 457{ 458 SERIALIZE_SCALAR(fd); 459 SERIALIZE_SCALAR(isPipe); 460 SERIALIZE_SCALAR(filename); 461 SERIALIZE_SCALAR(flags); 462 SERIALIZE_SCALAR(readPipeSource); 463 SERIALIZE_SCALAR(fileOffset); 464} 465 466void 467Process::FdMap::unserialize(Checkpoint *cp, const std::string §ion) 468{ 469 UNSERIALIZE_SCALAR(fd); 470 UNSERIALIZE_SCALAR(isPipe); 471 UNSERIALIZE_SCALAR(filename); 472 UNSERIALIZE_SCALAR(flags); 473 UNSERIALIZE_SCALAR(readPipeSource); 474 UNSERIALIZE_SCALAR(fileOffset); 475} 476 477void 478Process::serialize(std::ostream &os) 479{ 480 SERIALIZE_SCALAR(initialContextLoaded); 481 SERIALIZE_SCALAR(brk_point); 482 SERIALIZE_SCALAR(stack_base); 483 SERIALIZE_SCALAR(stack_size); 484 SERIALIZE_SCALAR(stack_min); 485 SERIALIZE_SCALAR(next_thread_stack_base); 486 SERIALIZE_SCALAR(mmap_start); 487 SERIALIZE_SCALAR(mmap_end); 488 SERIALIZE_SCALAR(nxm_start); 489 SERIALIZE_SCALAR(nxm_end); 490 find_file_offsets(); 491 pTable->serialize(os); 492 for (int x = 0; x <= MAX_FD; x++) { 493 nameOut(os, csprintf("%s.FdMap%d", name(), x)); 494 fd_map[x].serialize(os); 495 } 496 497} 498 499void 500Process::unserialize(Checkpoint *cp, const std::string §ion) 501{ 502 UNSERIALIZE_SCALAR(initialContextLoaded); 503 UNSERIALIZE_SCALAR(brk_point); 504 UNSERIALIZE_SCALAR(stack_base); 505 UNSERIALIZE_SCALAR(stack_size); 506 UNSERIALIZE_SCALAR(stack_min); 507 UNSERIALIZE_SCALAR(next_thread_stack_base); 508 UNSERIALIZE_SCALAR(mmap_start); 509 UNSERIALIZE_SCALAR(mmap_end); 510 UNSERIALIZE_SCALAR(nxm_start); 511 UNSERIALIZE_SCALAR(nxm_end); 512 pTable->unserialize(cp, section); 513 for (int x = 0; x <= MAX_FD; x++) { 514 fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x)); 515 } 516 fix_file_offsets(); 517 518 checkpointRestored = true; 519 520} 521 522 523//////////////////////////////////////////////////////////////////////// 524// 525// LiveProcess member definitions 526// 527//////////////////////////////////////////////////////////////////////// 528 529 530LiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile) 531 : Process(params), objFile(_objFile), 532 argv(params->cmd), envp(params->env), cwd(params->cwd) 533{ 534 __uid = params->uid; 535 __euid = params->euid; 536 __gid = params->gid; 537 __egid = params->egid; 538 __pid = params->pid; 539 __ppid = params->ppid; 540 541 prog_fname = params->cmd[0]; 542 543 // load up symbols, if any... these may be used for debugging or 544 // profiling. 545 if (!debugSymbolTable) { 546 debugSymbolTable = new SymbolTable(); 547 if (!objFile->loadGlobalSymbols(debugSymbolTable) || 548 !objFile->loadLocalSymbols(debugSymbolTable)) { 549 // didn't load any symbols 550 delete debugSymbolTable; 551 debugSymbolTable = NULL; 552 } 553 } 554} 555 556void 557LiveProcess::argsInit(int intSize, int pageSize) 558{ 559 Process::startup(); 560 561 // load object file into target memory 562 objFile->loadSections(initVirtMem); 563 564 // Calculate how much space we need for arg & env arrays. 565 int argv_array_size = intSize * (argv.size() + 1); 566 int envp_array_size = intSize * (envp.size() + 1); 567 int arg_data_size = 0; 568 for (int i = 0; i < argv.size(); ++i) { 569 arg_data_size += argv[i].size() + 1; 570 } 571 int env_data_size = 0; 572 for (int i = 0; i < envp.size(); ++i) { 573 env_data_size += envp[i].size() + 1; 574 } 575 576 int space_needed = 577 argv_array_size + envp_array_size + arg_data_size + env_data_size; 578 if (space_needed < 32*1024) 579 space_needed = 32*1024; 580 581 // set bottom of stack 582 stack_min = stack_base - space_needed; 583 // align it 584 stack_min = roundDown(stack_min, pageSize); 585 stack_size = stack_base - stack_min; 586 // map memory 587 pTable->allocate(stack_min, roundUp(stack_size, pageSize)); 588 589 // map out initial stack contents 590 Addr argv_array_base = stack_min + intSize; // room for argc 591 Addr envp_array_base = argv_array_base + argv_array_size; 592 Addr arg_data_base = envp_array_base + envp_array_size; 593 Addr env_data_base = arg_data_base + arg_data_size; 594 595 // write contents to stack 596 uint64_t argc = argv.size(); 597 if (intSize == 8) 598 argc = htog((uint64_t)argc); 599 else if (intSize == 4) 600 argc = htog((uint32_t)argc); 601 else 602 panic("Unknown int size"); 603 604 initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize); 605 606 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 607 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 608 609 assert(NumArgumentRegs >= 2); 610 611 ThreadContext *tc = system->getThreadContext(contextIds[0]); 612 613 tc->setIntReg(ArgumentReg[0], argc); 614 tc->setIntReg(ArgumentReg[1], argv_array_base); 615 tc->setIntReg(StackPointerReg, stack_min); 616 617 Addr prog_entry = objFile->entryPoint(); 618 tc->setPC(prog_entry); 619 tc->setNextPC(prog_entry + sizeof(MachInst)); 620 621#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc 622 tc->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 623#endif 624 625 num_processes++; 626} 627 628void 629LiveProcess::syscall(int64_t callnum, ThreadContext *tc) 630{ 631 num_syscalls++; 632 633 SyscallDesc *desc = getDesc(callnum); 634 if (desc == NULL) 635 fatal("Syscall %d out of range", callnum); 636 637 desc->doSyscall(callnum, this, tc); 638} 639 640LiveProcess * 641LiveProcess::create(LiveProcessParams * params) 642{ 643 LiveProcess *process = NULL; 644 645 string executable = 646 params->executable == "" ? params->cmd[0] : params->executable; 647 ObjectFile *objFile = createObjectFile(executable); 648 if (objFile == NULL) { 649 fatal("Can't load object file %s", executable); 650 } 651 652 if (objFile->isDynamic()) 653 fatal("Object file is a dynamic executable however only static " 654 "executables are supported!\n Please recompile your " 655 "executable as a static binary and try again.\n"); 656 657#if THE_ISA == ALPHA_ISA
|