process.cc revision 2646:c5f20661d9f3
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/sparc/isa_traits.hh" 30#include "arch/sparc/process.hh" 31#include "arch/sparc/linux/process.hh" 32#include "arch/sparc/solaris/process.hh" 33#include "base/loader/object_file.hh" 34#include "base/misc.hh" 35#include "cpu/exec_context.hh" 36#include "mem/page_table.hh" 37#include "mem/translating_port.hh" 38#include "sim/builder.hh" 39#include "sim/system.hh" 40 41using namespace std; 42using namespace SparcISA; 43 44SparcLiveProcess * 45SparcLiveProcess::create(const std::string &nm, System *system, int stdin_fd, 46 int stdout_fd, int stderr_fd, std::string executable, 47 std::vector<std::string> &argv, std::vector<std::string> &envp) 48{ 49 SparcLiveProcess *process = NULL; 50 51 ObjectFile *objFile = createObjectFile(executable); 52 if (objFile == NULL) { 53 fatal("Can't load object file %s", executable); 54 } 55 56 57 if (objFile->getArch() != ObjectFile::SPARC) 58 fatal("Object file with arch %x does not match architecture %x.", 59 objFile->getArch(), ObjectFile::SPARC); 60 switch (objFile->getOpSys()) { 61 case ObjectFile::Linux: 62 process = new SparcLinuxProcess(nm, objFile, system, 63 stdin_fd, stdout_fd, stderr_fd, 64 argv, envp); 65 break; 66 67 68 case ObjectFile::Solaris: 69 process = new SparcSolarisProcess(nm, objFile, system, 70 stdin_fd, stdout_fd, stderr_fd, 71 argv, envp); 72 break; 73 default: 74 fatal("Unknown/unsupported operating system."); 75 } 76 77 if (process == NULL) 78 fatal("Unknown error creating process object."); 79 return process; 80} 81 82SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile, 83 System *_system, int stdin_fd, int stdout_fd, int stderr_fd, 84 std::vector<std::string> &argv, std::vector<std::string> &envp) 85 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd, 86 argv, envp) 87{ 88 89 // XXX all the below need to be updated for SPARC - Ali 90 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 91 brk_point = roundUp(brk_point, VMPageSize); 92 93 // Set up stack. On SPARC Linux, stack goes from the top of memory 94 // downward, less the hole for the kernel address space. 95 stack_base = ((Addr)0x80000000000ULL); 96 97 // Set up region for mmaps. Tru64 seems to start just above 0 and 98 // grow up from there. 99 mmap_start = mmap_end = 0x800000; 100 101 // Set pointer for next thread stack. Reserve 8M for main stack. 102 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 103} 104 105void 106SparcLiveProcess::startup() 107{ 108 argsInit(MachineBytes, VMPageSize); 109 110 //From the SPARC ABI 111 112 //The process runs in user mode 113 execContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02); 114 115 //Setup default FP state 116 execContexts[0]->setMiscReg(MISCREG_FSR, 0); 117 118 execContexts[0]->setMiscReg(MISCREG_TICK, 0); 119 // 120 /* 121 * Register window management registers 122 */ 123 124 //No windows contain info from other programs 125 execContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0); 126 //There are no windows to pop 127 execContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0); 128 //All windows are available to save into 129 execContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2); 130 //All windows are "clean" 131 execContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows); 132 //Start with register window 0 133 execContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0); 134} 135 136m5_auxv_t buildAuxVect(int64_t type, int64_t val) 137{ 138 m5_auxv_t result; 139 result.a_type = TheISA::htog(type); 140 result.a_val = TheISA::htog(val); 141 return result; 142} 143 144void 145SparcLiveProcess::argsInit(int intSize, int pageSize) 146{ 147 Process::startup(); 148 149 Addr alignmentMask = ~(intSize - 1); 150 151 // load object file into target memory 152 objFile->loadSections(initVirtMem); 153 154 //These are the auxilliary vector types 155 enum auxTypes 156 { 157 SPARC_AT_HWCAP = 16, 158 SPARC_AT_PAGESZ = 6, 159 SPARC_AT_CLKTCK = 17, 160 SPARC_AT_PHDR = 3, 161 SPARC_AT_PHENT = 4, 162 SPARC_AT_PHNUM = 5, 163 SPARC_AT_BASE = 7, 164 SPARC_AT_FLAGS = 8, 165 SPARC_AT_ENTRY = 9, 166 SPARC_AT_UID = 11, 167 SPARC_AT_EUID = 12, 168 SPARC_AT_GID = 13, 169 SPARC_AT_EGID = 14 170 }; 171 172 enum hardwareCaps 173 { 174 M5_HWCAP_SPARC_FLUSH = 1, 175 M5_HWCAP_SPARC_STBAR = 2, 176 M5_HWCAP_SPARC_SWAP = 4, 177 M5_HWCAP_SPARC_MULDIV = 8, 178 M5_HWCAP_SPARC_V9 = 16, 179 //This one should technically only be set 180 //if there is a cheetah or cheetah_plus tlb, 181 //but we'll use it all the time 182 M5_HWCAP_SPARC_ULTRA3 = 32 183 }; 184 185 const int64_t hwcap = 186 M5_HWCAP_SPARC_FLUSH | 187 M5_HWCAP_SPARC_STBAR | 188 M5_HWCAP_SPARC_SWAP | 189 M5_HWCAP_SPARC_MULDIV | 190 M5_HWCAP_SPARC_V9 | 191 M5_HWCAP_SPARC_ULTRA3; 192 193 //Setup the auxilliary vectors. These will already have 194 //endian conversion. 195 auxv.push_back(buildAuxVect(SPARC_AT_EGID, 100)); 196 auxv.push_back(buildAuxVect(SPARC_AT_GID, 100)); 197 auxv.push_back(buildAuxVect(SPARC_AT_EUID, 100)); 198 auxv.push_back(buildAuxVect(SPARC_AT_UID, 100)); 199 //This would work, but the entry point is a protected member 200 //auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entry)); 201 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0)); 202 //This is the address of the elf "interpreter", which I don't 203 //think we currently set up. It should be set to 0 (I think) 204 //auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0)); 205 //This is the number of headers which were in the original elf 206 //file. This information isn't avaibale by this point. 207 //auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, 3)); 208 //This is the size of a program header entry. This isn't easy 209 //to compute here. 210 //auxv.push_back(buildAuxVect(SPARC_AT_PHENT, blah)); 211 //This is should be set to load_addr (whatever that is) + 212 //e_phoff. I think it's a pointer to the program headers. 213 //auxv.push_back(buildAuxVect(SPARC_AT_PHDR, blah)); 214 //This should be easy to get right, but I won't set it for now 215 //auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, blah)); 216 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize)); 217 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap)); 218 219 //Figure out how big the initial stack needs to be 220 221 //Each auxilliary vector is two 8 byte words 222 int aux_data_size = 2 * intSize * auxv.size(); 223 int env_data_size = 0; 224 for (int i = 0; i < envp.size(); ++i) { 225 env_data_size += envp[i].size() + 1; 226 } 227 int arg_data_size = 0; 228 for (int i = 0; i < argv.size(); ++i) { 229 arg_data_size += argv[i].size() + 1; 230 } 231 232 int aux_array_size = intSize * 2 * (auxv.size() + 1); 233 234 int argv_array_size = intSize * (argv.size() + 1); 235 int envp_array_size = intSize * (envp.size() + 1); 236 237 int argc_size = intSize; 238 int window_save_size = intSize * 16; 239 240 int info_block_size = 241 (aux_data_size + 242 env_data_size + 243 arg_data_size + 244 ~alignmentMask) & alignmentMask; 245 246 int info_block_padding = 247 info_block_size - 248 aux_data_size - 249 env_data_size - 250 arg_data_size; 251 252 int space_needed = 253 info_block_size + 254 aux_array_size + 255 envp_array_size + 256 argv_array_size + 257 argc_size + 258 window_save_size; 259 260 stack_min = stack_base - space_needed; 261 stack_min &= alignmentMask; 262 stack_size = stack_base - stack_min; 263 264 // map memory 265 pTable->allocate(roundDown(stack_min, pageSize), 266 roundUp(stack_size, pageSize)); 267 268 // map out initial stack contents 269 Addr aux_data_base = stack_base - aux_data_size - info_block_padding; 270 Addr env_data_base = aux_data_base - env_data_size; 271 Addr arg_data_base = env_data_base - arg_data_size; 272 Addr auxv_array_base = arg_data_base - aux_array_size; 273 Addr envp_array_base = auxv_array_base - envp_array_size; 274 Addr argv_array_base = envp_array_base - argv_array_size; 275 Addr argc_base = argv_array_base - argc_size; 276 Addr window_save_base = argc_base - window_save_size; 277 278 DPRINTF(Sparc, "The addresses of items on the initial stack:\n"); 279 DPRINTF(Sparc, "0x%x - aux data\n", aux_data_base); 280 DPRINTF(Sparc, "0x%x - env data\n", env_data_base); 281 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base); 282 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base); 283 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base); 284 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base); 285 DPRINTF(Sparc, "0x%x - argc \n", argc_base); 286 DPRINTF(Sparc, "0x%x - window save\n", window_save_base); 287 DPRINTF(Sparc, "0x%x - stack min\n", stack_min); 288 289 // write contents to stack 290 uint64_t argc = argv.size(); 291 uint64_t guestArgc = TheISA::htog(argc); 292 293 //Copy the aux stuff 294 for(int x = 0; x < auxv.size(); x++) 295 { 296 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 297 (uint8_t*)&(auxv[x].a_type), intSize); 298 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 299 (uint8_t*)&(auxv[x].a_val), intSize); 300 } 301 //Write out the terminating zeroed auxilliary vector 302 const uint64_t zero = 0; 303 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 304 (uint8_t*)&zero, 2 * intSize); 305 306 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 307 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 308 309 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 310 311 execContexts[0]->setIntReg(ArgumentReg0, argc); 312 execContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 313 execContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias); 314 315 Addr prog_entry = objFile->entryPoint(); 316 execContexts[0]->setPC(prog_entry); 317 execContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 318 execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 319 320// num_processes++; 321} 322 323 324BEGIN_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess) 325 326 VectorParam<string> cmd; 327 Param<string> executable; 328 Param<string> input; 329 Param<string> output; 330 VectorParam<string> env; 331 SimObjectParam<System *> system; 332 333END_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess) 334 335 336BEGIN_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess) 337 338 INIT_PARAM(cmd, "command line (executable plus arguments)"), 339 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 340 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 341 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 342 INIT_PARAM(env, "environment settings"), 343 INIT_PARAM(system, "system") 344 345END_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess) 346 347 348CREATE_SIM_OBJECT(SparcLiveProcess) 349{ 350 string in = input; 351 string out = output; 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 359 stdin_fd = Process::openInputFile(input); 360 361 if (out == "stdout" || out == "cout") 362 stdout_fd = STDOUT_FILENO; 363 else if (out == "stderr" || out == "cerr") 364 stdout_fd = STDERR_FILENO; 365 else 366 stdout_fd = Process::openOutputFile(out); 367 368 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 369 370 return SparcLiveProcess::create(getInstanceName(), system, 371 stdin_fd, stdout_fd, stderr_fd, 372 (string)executable == "" ? cmd[0] : executable, 373 cmd, env); 374} 375 376 377REGISTER_SIM_OBJECT("SparcLiveProcess", SparcLiveProcess) 378 379 380