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