process.cc revision 2597
1/* 2 * Copyright (c) 2003-2004 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 29#include "arch/mips/isa_traits.hh" 30#include "arch/mips/process.hh" 31#include "arch/mips/linux/process.hh" 32#include "base/loader/object_file.hh" 33#include "base/misc.hh" 34#include "cpu/exec_context.hh" 35#include "sim/builder.hh" 36#include "sim/system.hh" 37 38using namespace std; 39using namespace MipsISA; 40 41 42MipsLiveProcess * 43MipsLiveProcess::create(const std::string &nm, System *system, int stdin_fd, 44 int stdout_fd, int stderr_fd, std::string executable, 45 std::vector<std::string> &argv, std::vector<std::string> &envp) 46{ 47 MipsLiveProcess *process = NULL; 48 49 ObjectFile *objFile = createObjectFile(executable); 50 if (objFile == NULL) { 51 fatal("Can't load object file %s", executable); 52 } 53 54 55 if (objFile->getArch() != ObjectFile::Mips) 56 fatal("Object file does not match architecture."); 57 switch (objFile->getOpSys()) { 58 case ObjectFile::Linux: 59 process = new MipsLinuxProcess(nm, objFile, system, 60 stdin_fd, stdout_fd, stderr_fd, 61 argv, envp); 62 break; 63 64 default: 65 fatal("Unknown/unsupported operating system."); 66 } 67 68 if (process == NULL) 69 fatal("Unknown error creating process object."); 70 return process; 71} 72 73MipsLiveProcess::MipsLiveProcess(const std::string &nm, ObjectFile *objFile, 74 System *_system, int stdin_fd, int stdout_fd, int stderr_fd, 75 std::vector<std::string> &argv, std::vector<std::string> &envp) 76 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd, 77 argv, envp) 78{ 79 80 // XXX all the below need to be updated for SPARC - Ali 81 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 82 brk_point = roundUp(brk_point, VMPageSize); 83 84 // Set up stack. On Alpha, stack goes below text section. This 85 // code should get moved to some architecture-specific spot. 86 stack_base = objFile->textBase() - (409600+4096); 87 88 // Set up region for mmaps. Tru64 seems to start just above 0 and 89 // grow up from there. 90 mmap_start = mmap_end = 0x10000; 91 92 // Set pointer for next thread stack. Reserve 8M for main stack. 93 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 94 95} 96 97void 98MipsLiveProcess::startup() 99{ 100 argsInit(MachineBytes, VMPageSize); 101} 102 103 104 105 106BEGIN_DECLARE_SIM_OBJECT_PARAMS(MipsLiveProcess) 107 108 VectorParam<string> cmd; 109 Param<string> executable; 110 Param<string> input; 111 Param<string> output; 112 VectorParam<string> env; 113 SimObjectParam<System *> system; 114 115END_DECLARE_SIM_OBJECT_PARAMS(MipsLiveProcess) 116 117 118BEGIN_INIT_SIM_OBJECT_PARAMS(MipsLiveProcess) 119 120 INIT_PARAM(cmd, "command line (executable plus arguments)"), 121 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 122 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 123 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 124 INIT_PARAM(env, "environment settings"), 125 INIT_PARAM(system, "system") 126 127END_INIT_SIM_OBJECT_PARAMS(MipsLiveProcess) 128 129 130CREATE_SIM_OBJECT(MipsLiveProcess) 131{ 132 string in = input; 133 string out = output; 134 135 // initialize file descriptors to default: same as simulator 136 int stdin_fd, stdout_fd, stderr_fd; 137 138 if (in == "stdin" || in == "cin") 139 stdin_fd = STDIN_FILENO; 140 else 141 stdin_fd = Process::openInputFile(input); 142 143 if (out == "stdout" || out == "cout") 144 stdout_fd = STDOUT_FILENO; 145 else if (out == "stderr" || out == "cerr") 146 stdout_fd = STDERR_FILENO; 147 else 148 stdout_fd = Process::openOutputFile(out); 149 150 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 151 152 return MipsLiveProcess::create(getInstanceName(), system, 153 stdin_fd, stdout_fd, stderr_fd, 154 (string)executable == "" ? cmd[0] : executable, 155 cmd, env); 156} 157 158 159REGISTER_SIM_OBJECT("MipsLiveProcess", MipsLiveProcess) 160 161 162