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