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