process.cc revision 2632:1bb2f91485ea
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_PRIV, 0); 114 //Interrupts are enabled 115 execContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE_IE, 1); 116 //Round to nearest 117 execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_RD, 0); 118 //Floating point traps are not enabled 119 execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_TEM, 0); 120 //Turn non standard mode off 121 execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_NS, 0); 122 //The floating point queue is empty 123 execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_QNE, 0); 124 //There are no accrued eexecContext[0]eptions 125 execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_AEXC, 0); 126 //There are no current eexecContext[0]eptions 127 execContexts[0]->setMiscRegWithEffect(MISCREG_FSR_CEXC, 0); 128 129 /* 130 * Register window management registers 131 */ 132 133 //No windows contain info from other programs 134 execContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0); 135 //There are no windows to pop 136 execContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0); 137 //All windows are available to save into 138 execContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2); 139 //All windows are "clean" 140 execContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows); 141 //Start with register window 0 142 execContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0); 143} 144 145m5_auxv_t buildAuxVect(int64_t type, int64_t val) 146{ 147 m5_auxv_t result; 148 result.a_type = TheISA::htog(type); 149 result.a_val = TheISA::htog(val); 150 return result; 151} 152 153void 154SparcLiveProcess::argsInit(int intSize, int pageSize) 155{ 156 Process::startup(); 157 158 Addr alignmentMask = ~(intSize - 1); 159 160 // load object file into target memory 161 objFile->loadSections(initVirtMem); 162 163 //These are the auxilliary vector types 164 enum auxTypes 165 { 166 SPARC_AT_HWCAP = 16, 167 SPARC_AT_PAGESZ = 6, 168 SPARC_AT_CLKTCK = 17, 169 SPARC_AT_PHDR = 3, 170 SPARC_AT_PHENT = 4, 171 SPARC_AT_PHNUM = 5, 172 SPARC_AT_BASE = 7, 173 SPARC_AT_FLAGS = 8, 174 SPARC_AT_ENTRY = 9, 175 SPARC_AT_UID = 11, 176 SPARC_AT_EUID = 12, 177 SPARC_AT_GID = 13, 178 SPARC_AT_EGID = 14 179 }; 180 181 enum hardwareCaps 182 { 183 M5_HWCAP_SPARC_FLUSH = 1, 184 M5_HWCAP_SPARC_STBAR = 2, 185 M5_HWCAP_SPARC_SWAP = 4, 186 M5_HWCAP_SPARC_MULDIV = 8, 187 M5_HWCAP_SPARC_V9 = 16, 188 //This one should technically only be set 189 //if there is a cheetah or cheetah_plus tlb, 190 //but we'll use it all the time 191 M5_HWCAP_SPARC_ULTRA3 = 32 192 }; 193 194 const int64_t hwcap = 195 M5_HWCAP_SPARC_FLUSH | 196 M5_HWCAP_SPARC_STBAR | 197 M5_HWCAP_SPARC_SWAP | 198 M5_HWCAP_SPARC_MULDIV | 199 M5_HWCAP_SPARC_V9 | 200 M5_HWCAP_SPARC_ULTRA3; 201 202 //Setup the auxilliary vectors. These will already have 203 //endian conversion. 204 auxv.push_back(buildAuxVect(SPARC_AT_EGID, 100)); 205 auxv.push_back(buildAuxVect(SPARC_AT_GID, 100)); 206 auxv.push_back(buildAuxVect(SPARC_AT_EUID, 100)); 207 auxv.push_back(buildAuxVect(SPARC_AT_UID, 100)); 208 //This would work, but the entry point is a protected member 209 //auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entry)); 210 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0)); 211 //This is the address of the elf "interpreter", which I don't 212 //think we currently set up. It should be set to 0 (I think) 213 //auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0)); 214 //This is the number of headers which were in the original elf 215 //file. This information isn't avaibale by this point. 216 //auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, 3)); 217 //This is the size of a program header entry. This isn't easy 218 //to compute here. 219 //auxv.push_back(buildAuxVect(SPARC_AT_PHENT, blah)); 220 //This is should be set to load_addr (whatever that is) + 221 //e_phoff. I think it's a pointer to the program headers. 222 //auxv.push_back(buildAuxVect(SPARC_AT_PHDR, blah)); 223 //This should be easy to get right, but I won't set it for now 224 //auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, blah)); 225 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize)); 226 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap)); 227 228 //Figure out how big the initial stack needs to be 229 230 //Each auxilliary vector is two 8 byte words 231 int aux_data_size = 2 * intSize * auxv.size(); 232 int env_data_size = 0; 233 for (int i = 0; i < envp.size(); ++i) { 234 env_data_size += envp[i].size() + 1; 235 } 236 int arg_data_size = 0; 237 for (int i = 0; i < argv.size(); ++i) { 238 arg_data_size += argv[i].size() + 1; 239 } 240 241 int aux_array_size = intSize * 2 * (auxv.size() + 1); 242 243 int argv_array_size = intSize * (argv.size() + 1); 244 int envp_array_size = intSize * (envp.size() + 1); 245 246 int argc_size = intSize; 247 int window_save_size = intSize * 16; 248 249 int info_block_size = 250 (aux_data_size + 251 env_data_size + 252 arg_data_size + 253 ~alignmentMask) & alignmentMask; 254 255 int info_block_padding = 256 info_block_size - 257 aux_data_size - 258 env_data_size - 259 arg_data_size; 260 261 int space_needed = 262 info_block_size + 263 aux_array_size + 264 envp_array_size + 265 argv_array_size + 266 argc_size + 267 window_save_size; 268 269 stack_min = stack_base - space_needed; 270 stack_min &= alignmentMask; 271 stack_size = stack_base - stack_min; 272 273 // map memory 274 pTable->allocate(roundDown(stack_min, pageSize), 275 roundUp(stack_size, pageSize)); 276 277 // map out initial stack contents 278 Addr aux_data_base = stack_base - aux_data_size - info_block_padding; 279 Addr env_data_base = aux_data_base - env_data_size; 280 Addr arg_data_base = env_data_base - arg_data_size; 281 Addr auxv_array_base = arg_data_base - aux_array_size; 282 Addr envp_array_base = auxv_array_base - envp_array_size; 283 Addr argv_array_base = envp_array_base - argv_array_size; 284 Addr argc_base = argv_array_base - argc_size; 285 Addr window_save_base = argc_base - window_save_size; 286 287 DPRINTF(Sparc, "The addresses of items on the initial stack:\n"); 288 DPRINTF(Sparc, "0x%x - aux data\n", aux_data_base); 289 DPRINTF(Sparc, "0x%x - env data\n", env_data_base); 290 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base); 291 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base); 292 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base); 293 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base); 294 DPRINTF(Sparc, "0x%x - argc \n", argc_base); 295 DPRINTF(Sparc, "0x%x - window save\n", window_save_base); 296 DPRINTF(Sparc, "0x%x - stack min\n", stack_min); 297 298 // write contents to stack 299 uint64_t argc = argv.size(); 300 uint64_t guestArgc = TheISA::htog(argc); 301 302 //Copy the aux stuff 303 for(int x = 0; x < auxv.size(); x++) 304 { 305 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 306 (uint8_t*)&(auxv[x].a_type), intSize); 307 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 308 (uint8_t*)&(auxv[x].a_val), intSize); 309 } 310 //Write out the terminating zeroed auxilliary vector 311 const uint64_t zero = 0; 312 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 313 (uint8_t*)&zero, 2 * intSize); 314 315 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 316 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 317 318 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 319 320 execContexts[0]->setIntReg(ArgumentReg0, argc); 321 execContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 322 execContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias); 323 324 Addr prog_entry = objFile->entryPoint(); 325 execContexts[0]->setPC(prog_entry); 326 execContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 327 execContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 328 329// num_processes++; 330} 331 332 333BEGIN_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess) 334 335 VectorParam<string> cmd; 336 Param<string> executable; 337 Param<string> input; 338 Param<string> output; 339 VectorParam<string> env; 340 SimObjectParam<System *> system; 341 342END_DECLARE_SIM_OBJECT_PARAMS(SparcLiveProcess) 343 344 345BEGIN_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess) 346 347 INIT_PARAM(cmd, "command line (executable plus arguments)"), 348 INIT_PARAM(executable, "executable (overrides cmd[0] if set)"), 349 INIT_PARAM(input, "filename for stdin (dflt: use sim stdin)"), 350 INIT_PARAM(output, "filename for stdout/stderr (dflt: use sim stdout)"), 351 INIT_PARAM(env, "environment settings"), 352 INIT_PARAM(system, "system") 353 354END_INIT_SIM_OBJECT_PARAMS(SparcLiveProcess) 355 356 357CREATE_SIM_OBJECT(SparcLiveProcess) 358{ 359 string in = input; 360 string out = output; 361 362 // initialize file descriptors to default: same as simulator 363 int stdin_fd, stdout_fd, stderr_fd; 364 365 if (in == "stdin" || in == "cin") 366 stdin_fd = STDIN_FILENO; 367 else 368 stdin_fd = Process::openInputFile(input); 369 370 if (out == "stdout" || out == "cout") 371 stdout_fd = STDOUT_FILENO; 372 else if (out == "stderr" || out == "cerr") 373 stdout_fd = STDERR_FILENO; 374 else 375 stdout_fd = Process::openOutputFile(out); 376 377 stderr_fd = (stdout_fd != STDOUT_FILENO) ? stdout_fd : STDERR_FILENO; 378 379 return SparcLiveProcess::create(getInstanceName(), system, 380 stdin_fd, stdout_fd, stderr_fd, 381 (string)executable == "" ? cmd[0] : executable, 382 cmd, env); 383} 384 385 386REGISTER_SIM_OBJECT("SparcLiveProcess", SparcLiveProcess) 387 388 389