process.cc

来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· CC 代码 · 共 719 行 · 第 1/2 页

CC
719
字号
    else{        //OPEN standard in and seek to the right location        stdin_fd = Process::openInputFile(in);        if (lseek(stdin_fd, fdo_stdin->fileOffset, SEEK_SET) < 0)            panic("Unable to seek to correct location in file: %s", in);    }    if (out == "stdout" || out == "cout")        stdout_fd = STDOUT_FILENO;    else if (out == "stderr" || out == "cerr")        stdout_fd = STDERR_FILENO;    else if (out == "None")        stdout_fd = -1;    else{        stdout_fd = Process::openOutputFile(out);        if (lseek(stdin_fd, fdo_stdout->fileOffset, SEEK_SET) < 0)            panic("Unable to seek to correct in file: %s", out);    }    stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO;    fdo_stdin->fd = stdin_fd;    fdo_stdout->fd = stdout_fd;    fdo_stderr->fd = stderr_fd;    for (int free_fd = 3; free_fd <= MAX_FD; ++free_fd) {        Process::FdMap *fdo = &fd_map[free_fd];        if (fdo->fd != -1) {            if (fdo->isPipe){                if (fdo->filename == "PIPE-WRITE")                    continue;                else {                    assert (fdo->filename == "PIPE-READ");                    //create a new pipe                    int fds[2];                    int pipe_retval = pipe(fds);                    if (pipe_retval < 0) {                        // error                        panic("Unable to create new pipe.");                    }                    fdo->fd = fds[0]; //set read pipe                    Process::FdMap *fdo_write = &fd_map[fdo->readPipeSource];                    if (fdo_write->filename != "PIPE-WRITE")                        panic ("Couldn't find write end of the pipe");                    fdo_write->fd = fds[1];//set write pipe               }            } else {                //Open file                int fd = open(fdo->filename.c_str(), fdo->flags, fdo->mode);                if (fd == -1)                    panic("Unable to open file: %s", fdo->filename);                fdo->fd = fd;                //Seek to correct location before checkpoint                if (lseek(fd,fdo->fileOffset, SEEK_SET) < 0)                    panic("Unable to seek to correct location in file: %s", fdo->filename);            }        }    }}voidProcess::find_file_offsets(){    for (int free_fd = 0; free_fd <= MAX_FD; ++free_fd) {        Process::FdMap *fdo = &fd_map[free_fd];        if (fdo->fd != -1) {            fdo->fileOffset = lseek(fdo->fd, 0, SEEK_CUR);        }  else {                fdo->filename = "NULL";                fdo->fileOffset = 0;        }    }}voidProcess::setReadPipeSource(int read_pipe_fd, int source_fd){    Process::FdMap *fdo = &fd_map[read_pipe_fd];    fdo->readPipeSource = source_fd;}voidProcess::FdMap::serialize(std::ostream &os){    SERIALIZE_SCALAR(fd);    SERIALIZE_SCALAR(isPipe);    SERIALIZE_SCALAR(filename);    SERIALIZE_SCALAR(flags);    SERIALIZE_SCALAR(readPipeSource);    SERIALIZE_SCALAR(fileOffset);}voidProcess::FdMap::unserialize(Checkpoint *cp, const std::string &section){    UNSERIALIZE_SCALAR(fd);    UNSERIALIZE_SCALAR(isPipe);    UNSERIALIZE_SCALAR(filename);    UNSERIALIZE_SCALAR(flags);    UNSERIALIZE_SCALAR(readPipeSource);    UNSERIALIZE_SCALAR(fileOffset);}voidProcess::serialize(std::ostream &os){    SERIALIZE_SCALAR(initialContextLoaded);    SERIALIZE_SCALAR(brk_point);    SERIALIZE_SCALAR(stack_base);    SERIALIZE_SCALAR(stack_size);    SERIALIZE_SCALAR(stack_min);    SERIALIZE_SCALAR(next_thread_stack_base);    SERIALIZE_SCALAR(mmap_start);    SERIALIZE_SCALAR(mmap_end);    SERIALIZE_SCALAR(nxm_start);    SERIALIZE_SCALAR(nxm_end);    find_file_offsets();    pTable->serialize(os);    for (int x = 0; x <= MAX_FD; x++) {        nameOut(os, csprintf("%s.FdMap%d", name(), x));        fd_map[x].serialize(os);    }}voidProcess::unserialize(Checkpoint *cp, const std::string &section){    UNSERIALIZE_SCALAR(initialContextLoaded);    UNSERIALIZE_SCALAR(brk_point);    UNSERIALIZE_SCALAR(stack_base);    UNSERIALIZE_SCALAR(stack_size);    UNSERIALIZE_SCALAR(stack_min);    UNSERIALIZE_SCALAR(next_thread_stack_base);    UNSERIALIZE_SCALAR(mmap_start);    UNSERIALIZE_SCALAR(mmap_end);    UNSERIALIZE_SCALAR(nxm_start);    UNSERIALIZE_SCALAR(nxm_end);    pTable->unserialize(cp, section);    for (int x = 0; x <= MAX_FD; x++) {        fd_map[x].unserialize(cp, csprintf("%s.FdMap%d", section, x));     }    fix_file_offsets();        checkpointRestored = true;}//////////////////////////////////////////////////////////////////////////// LiveProcess member definitions//////////////////////////////////////////////////////////////////////////LiveProcess::LiveProcess(LiveProcessParams * params, ObjectFile *_objFile)    : Process(params), objFile(_objFile),      argv(params->cmd), envp(params->env), cwd(params->cwd){    __uid = params->uid;    __euid = params->euid;    __gid = params->gid;    __egid = params->egid;    __pid = params->pid;    __ppid = params->ppid;    prog_fname = params->cmd[0];    // load up symbols, if any... these may be used for debugging or    // profiling.    if (!debugSymbolTable) {        debugSymbolTable = new SymbolTable();        if (!objFile->loadGlobalSymbols(debugSymbolTable) ||            !objFile->loadLocalSymbols(debugSymbolTable)) {            // didn't load any symbols            delete debugSymbolTable;            debugSymbolTable = NULL;        }    }}voidLiveProcess::argsInit(int intSize, int pageSize){    Process::startup();    // load object file into target memory    objFile->loadSections(initVirtMem);    // Calculate how much space we need for arg & env arrays.    int argv_array_size = intSize * (argv.size() + 1);    int envp_array_size = intSize * (envp.size() + 1);    int arg_data_size = 0;    for (int i = 0; i < argv.size(); ++i) {        arg_data_size += argv[i].size() + 1;    }    int env_data_size = 0;    for (int i = 0; i < envp.size(); ++i) {        env_data_size += envp[i].size() + 1;    }    int space_needed =        argv_array_size + envp_array_size + arg_data_size + env_data_size;    if (space_needed < 32*1024)        space_needed = 32*1024;    // set bottom of stack    stack_min = stack_base - space_needed;    // align it    stack_min = roundDown(stack_min, pageSize);    stack_size = stack_base - stack_min;    // map memory    pTable->allocate(stack_min, roundUp(stack_size, pageSize));    // map out initial stack contents    Addr argv_array_base = stack_min + intSize; // room for argc    Addr envp_array_base = argv_array_base + argv_array_size;    Addr arg_data_base = envp_array_base + envp_array_size;    Addr env_data_base = arg_data_base + arg_data_size;    // write contents to stack    uint64_t argc = argv.size();    if (intSize == 8)        argc = htog((uint64_t)argc);    else if (intSize == 4)        argc = htog((uint32_t)argc);    else        panic("Unknown int size");    initVirtMem->writeBlob(stack_min, (uint8_t*)&argc, intSize);    copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem);    copyStringArray(envp, envp_array_base, env_data_base, initVirtMem);    assert(NumArgumentRegs >= 2);    threadContexts[0]->setIntReg(ArgumentReg[0], argc);    threadContexts[0]->setIntReg(ArgumentReg[1], argv_array_base);    threadContexts[0]->setIntReg(StackPointerReg, stack_min);    Addr prog_entry = objFile->entryPoint();    threadContexts[0]->setPC(prog_entry);    threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst));#if THE_ISA != ALPHA_ISA //e.g. MIPS or Sparc    threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst)));#endif    num_processes++;}voidLiveProcess::syscall(int64_t callnum, ThreadContext *tc){    num_syscalls++;    SyscallDesc *desc = getDesc(callnum);    if (desc == NULL)        fatal("Syscall %d out of range", callnum);    desc->doSyscall(callnum, this, tc);}LiveProcess *LiveProcess::create(LiveProcessParams * params){    LiveProcess *process = NULL;    string executable =        params->executable == "" ? params->cmd[0] : params->executable;    ObjectFile *objFile = createObjectFile(executable);    if (objFile == NULL) {        fatal("Can't load object file %s", executable);    }    if (objFile->isDynamic())       fatal("Object file is a dynamic executable however only static "             "executables are supported!\n       Please recompile your "             "executable as a static binary and try again.\n");#if THE_ISA == ALPHA_ISA    if (objFile->hasTLS())        fatal("Object file has a TLS section and single threaded TLS is not\n"              "       currently supported for Alpha! Please recompile your "              "executable with \n       a non-TLS toolchain.\n");    if (objFile->getArch() != ObjectFile::Alpha)        fatal("Object file architecture does not match compiled ISA (Alpha).");    switch (objFile->getOpSys()) {      case ObjectFile::Tru64:        process = new AlphaTru64Process(params, objFile);        break;      case ObjectFile::Linux:        process = new AlphaLinuxProcess(params, objFile);        break;      default:        fatal("Unknown/unsupported operating system.");    }#elif THE_ISA == SPARC_ISA    if (objFile->getArch() != ObjectFile::SPARC64 && objFile->getArch() != ObjectFile::SPARC32)        fatal("Object file architecture does not match compiled ISA (SPARC).");    switch (objFile->getOpSys()) {      case ObjectFile::Linux:        if (objFile->getArch() == ObjectFile::SPARC64) {            process = new Sparc64LinuxProcess(params, objFile);        } else {            process = new Sparc32LinuxProcess(params, objFile);        }        break;      case ObjectFile::Solaris:        process = new SparcSolarisProcess(params, objFile);        break;      default:        fatal("Unknown/unsupported operating system.");    }#elif THE_ISA == MIPS_ISA    if (objFile->getArch() != ObjectFile::Mips)        fatal("Object file architecture does not match compiled ISA (MIPS).");    switch (objFile->getOpSys()) {      case ObjectFile::Linux:        process = new MipsLinuxProcess(params, objFile);        break;      default:        fatal("Unknown/unsupported operating system.");    }#elif THE_ISA == ARM_ISA    if (objFile->getArch() != ObjectFile::Arm)        fatal("Object file architecture does not match compiled ISA (ARM).");    switch (objFile->getOpSys()) {      case ObjectFile::Linux:        process = new ArmLinuxProcess(params, objFile);        break;      default:        fatal("Unknown/unsupported operating system.");    }#else#error "THE_ISA not set"#endif    if (process == NULL)        fatal("Unknown error creating process object.");    return process;}LiveProcess *LiveProcessParams::create(){    return LiveProcess::create(this);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?