process.cc revision 3669
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/sparc/asi.hh" 33#include "arch/sparc/isa_traits.hh" 34#include "arch/sparc/process.hh" 35#include "base/loader/object_file.hh" 36#include "base/loader/elf_object.hh" 37#include "base/misc.hh" 38#include "cpu/thread_context.hh" 39#include "mem/page_table.hh" 40#include "mem/translating_port.hh" 41#include "sim/system.hh" 42 43using namespace std; 44using namespace SparcISA; 45 46 47SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile, 48 System *_system, int stdin_fd, int stdout_fd, int stderr_fd, 49 std::vector<std::string> &argv, std::vector<std::string> &envp, 50 const std::string &cwd, 51 uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, 52 uint64_t _pid, uint64_t _ppid) 53 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd, 54 argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid) 55{ 56 57 // XXX all the below need to be updated for SPARC - Ali 58 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 59 brk_point = roundUp(brk_point, VMPageSize); 60 61 // Set up stack. On SPARC Linux, stack goes from the top of memory 62 // downward, less the hole for the kernel address space. 63 stack_base = (Addr)0x80000000000ULL; 64 65 // Set up region for mmaps. Tru64 seems to start just above 0 and 66 // grow up from there. 67 mmap_start = mmap_end = 0xfffff80000000000ULL; 68 69 // Set pointer for next thread stack. Reserve 8M for main stack. 70 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 71 72 //Initialize these to 0s 73 fillStart = 0; 74 spillStart = 0; 75} 76 77void 78SparcLiveProcess::startup() 79{ 80 argsInit(MachineBytes, VMPageSize); 81 82 //From the SPARC ABI 83 84 //The process runs in user mode 85 threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02); 86 87 //Setup default FP state 88 threadContexts[0]->setMiscReg(MISCREG_FSR, 0); 89 90 threadContexts[0]->setMiscReg(MISCREG_TICK, 0); 91 // 92 /* 93 * Register window management registers 94 */ 95 96 //No windows contain info from other programs 97 threadContexts[0]->setMiscReg(MISCREG_OTHERWIN, 0); 98 //There are no windows to pop 99 threadContexts[0]->setMiscReg(MISCREG_CANRESTORE, 0); 100 //All windows are available to save into 101 threadContexts[0]->setMiscReg(MISCREG_CANSAVE, NWindows - 2); 102 //All windows are "clean" 103 threadContexts[0]->setMiscReg(MISCREG_CLEANWIN, NWindows); 104 //Start with register window 0 105 threadContexts[0]->setMiscReg(MISCREG_CWP, 0); 106 //Always use spill and fill traps 0 107 threadContexts[0]->setMiscReg(MISCREG_WSTATE, 0); 108 //Set the trap level to 0 109 threadContexts[0]->setMiscReg(MISCREG_TL, 0); 110 //Set the ASI register to something fixed 111 threadContexts[0]->setMiscReg(MISCREG_ASI, ASI_PRIMARY); 112} 113 114m5_auxv_t buildAuxVect(int64_t type, int64_t val) 115{ 116 m5_auxv_t result; 117 result.a_type = TheISA::htog(type); 118 result.a_val = TheISA::htog(val); 119 return result; 120} 121 122//We only use 19 instructions for the trap handlers, but there would be 123//space for 32 in a real SPARC trap table. 124const int numFillInsts = 32; 125const int numSpillInsts = 32; 126 127MachInst fillHandler[numFillInsts] = 128{ 129 htog(0x87802018), //wr %g0, ASI_AIUP, %asi 130 htog(0xe0dba7ff), //ldxa [%sp + BIAS + (0*8)] %asi, %l0 131 htog(0xe2dba807), //ldxa [%sp + BIAS + (1*8)] %asi, %l1 132 htog(0xe4dba80f), //ldxa [%sp + BIAS + (2*8)] %asi, %l2 133 htog(0xe6dba817), //ldxa [%sp + BIAS + (3*8)] %asi, %l3 134 htog(0xe8dba81f), //ldxa [%sp + BIAS + (4*8)] %asi, %l4 135 htog(0xeadba827), //ldxa [%sp + BIAS + (5*8)] %asi, %l5 136 htog(0xecdba82f), //ldxa [%sp + BIAS + (6*8)] %asi, %l6 137 htog(0xeedba837), //ldxa [%sp + BIAS + (7*8)] %asi, %l7 138 htog(0xf0dba83f), //ldxa [%sp + BIAS + (8*8)] %asi, %i0 139 htog(0xf2dba847), //ldxa [%sp + BIAS + (9*8)] %asi, %i1 140 htog(0xf4dba84f), //ldxa [%sp + BIAS + (10*8)] %asi, %i2 141 htog(0xf6dba857), //ldxa [%sp + BIAS + (11*8)] %asi, %i3 142 htog(0xf8dba85f), //ldxa [%sp + BIAS + (12*8)] %asi, %i4 143 htog(0xfadba867), //ldxa [%sp + BIAS + (13*8)] %asi, %i5 144 htog(0xfcdba86f), //ldxa [%sp + BIAS + (14*8)] %asi, %i6 145 htog(0xfedba877), //ldxa [%sp + BIAS + (15*8)] %asi, %i7 146 htog(0x83880000), //restored 147 htog(0x83F00000), //retry 148 htog(0x00000000), //illtrap 149 htog(0x00000000), //illtrap 150 htog(0x00000000), //illtrap 151 htog(0x00000000), //illtrap 152 htog(0x00000000), //illtrap 153 htog(0x00000000), //illtrap 154 htog(0x00000000), //illtrap 155 htog(0x00000000), //illtrap 156 htog(0x00000000), //illtrap 157 htog(0x00000000), //illtrap 158 htog(0x00000000), //illtrap 159 htog(0x00000000), //illtrap 160 htog(0x00000000) //illtrap 161}; 162 163MachInst spillHandler[numSpillInsts] = 164{ 165 htog(0x87802018), //wr %g0, ASI_AIUP, %asi 166 htog(0xe0f3a7ff), //stxa %l0, [%sp + BIAS + (0*8)] %asi 167 htog(0xe2f3a807), //stxa %l1, [%sp + BIAS + (1*8)] %asi 168 htog(0xe4f3a80f), //stxa %l2, [%sp + BIAS + (2*8)] %asi 169 htog(0xe6f3a817), //stxa %l3, [%sp + BIAS + (3*8)] %asi 170 htog(0xe8f3a81f), //stxa %l4, [%sp + BIAS + (4*8)] %asi 171 htog(0xeaf3a827), //stxa %l5, [%sp + BIAS + (5*8)] %asi 172 htog(0xecf3a82f), //stxa %l6, [%sp + BIAS + (6*8)] %asi 173 htog(0xeef3a837), //stxa %l7, [%sp + BIAS + (7*8)] %asi 174 htog(0xf0f3a83f), //stxa %i0, [%sp + BIAS + (8*8)] %asi 175 htog(0xf2f3a847), //stxa %i1, [%sp + BIAS + (9*8)] %asi 176 htog(0xf4f3a84f), //stxa %i2, [%sp + BIAS + (10*8)] %asi 177 htog(0xf6f3a857), //stxa %i3, [%sp + BIAS + (11*8)] %asi 178 htog(0xf8f3a85f), //stxa %i4, [%sp + BIAS + (12*8)] %asi 179 htog(0xfaf3a867), //stxa %i5, [%sp + BIAS + (13*8)] %asi 180 htog(0xfcf3a86f), //stxa %i6, [%sp + BIAS + (14*8)] %asi 181 htog(0xfef3a877), //stxa %i7, [%sp + BIAS + (15*8)] %asi 182 htog(0x81880000), //saved 183 htog(0x83F00000), //retry 184 htog(0x00000000), //illtrap 185 htog(0x00000000), //illtrap 186 htog(0x00000000), //illtrap 187 htog(0x00000000), //illtrap 188 htog(0x00000000), //illtrap 189 htog(0x00000000), //illtrap 190 htog(0x00000000), //illtrap 191 htog(0x00000000), //illtrap 192 htog(0x00000000), //illtrap 193 htog(0x00000000), //illtrap 194 htog(0x00000000), //illtrap 195 htog(0x00000000), //illtrap 196 htog(0x00000000) //illtrap 197}; 198 199void 200SparcLiveProcess::argsInit(int intSize, int pageSize) 201{ 202 Process::startup(); 203 204 string filename; 205 if(argv.size() < 1) 206 filename = ""; 207 else 208 filename = argv[0]; 209 210 Addr alignmentMask = ~(intSize - 1); 211 212 // load object file into target memory 213 objFile->loadSections(initVirtMem); 214 215 //These are the auxilliary vector types 216 enum auxTypes 217 { 218 SPARC_AT_HWCAP = 16, 219 SPARC_AT_PAGESZ = 6, 220 SPARC_AT_CLKTCK = 17, 221 SPARC_AT_PHDR = 3, 222 SPARC_AT_PHENT = 4, 223 SPARC_AT_PHNUM = 5, 224 SPARC_AT_BASE = 7, 225 SPARC_AT_FLAGS = 8, 226 SPARC_AT_ENTRY = 9, 227 SPARC_AT_UID = 11, 228 SPARC_AT_EUID = 12, 229 SPARC_AT_GID = 13, 230 SPARC_AT_EGID = 14, 231 SPARC_AT_SECURE = 23 232 }; 233 234 enum hardwareCaps 235 { 236 M5_HWCAP_SPARC_FLUSH = 1, 237 M5_HWCAP_SPARC_STBAR = 2, 238 M5_HWCAP_SPARC_SWAP = 4, 239 M5_HWCAP_SPARC_MULDIV = 8, 240 M5_HWCAP_SPARC_V9 = 16, 241 //This one should technically only be set 242 //if there is a cheetah or cheetah_plus tlb, 243 //but we'll use it all the time 244 M5_HWCAP_SPARC_ULTRA3 = 32 245 }; 246 247 const int64_t hwcap = 248 M5_HWCAP_SPARC_FLUSH | 249 M5_HWCAP_SPARC_STBAR | 250 M5_HWCAP_SPARC_SWAP | 251 M5_HWCAP_SPARC_MULDIV | 252 M5_HWCAP_SPARC_V9 | 253 M5_HWCAP_SPARC_ULTRA3; 254 255 256 //Setup the auxilliary vectors. These will already have endian conversion. 257 //Auxilliary vectors are loaded only for elf formatted executables. 258 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 259 if(elfObject) 260 { 261 //Bits which describe the system hardware capabilities 262 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap)); 263 //The system page size 264 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize)); 265 //Defined to be 100 in the kernel source. 266 //Frequency at which times() increments 267 auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100)); 268 // For statically linked executables, this is the virtual address of the 269 // program header tables if they appear in the executable image 270 auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable())); 271 // This is the size of a program header entry from the elf file. 272 auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize())); 273 // This is the number of program headers from the original elf file. 274 auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount())); 275 //This is the address of the elf "interpreter", It should be set 276 //to 0 for regular executables. It should be something else 277 //(not sure what) for dynamic libraries. 278 auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0)); 279 //This is hardwired to 0 in the elf loading code in the kernel 280 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0)); 281 //The entry point to the program 282 auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint())); 283 //Different user and group IDs 284 auxv.push_back(buildAuxVect(SPARC_AT_UID, uid())); 285 auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid())); 286 auxv.push_back(buildAuxVect(SPARC_AT_GID, gid())); 287 auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid())); 288 //Whether to enable "secure mode" in the executable 289 auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0)); 290 } 291 292 //Figure out how big the initial stack needs to be 293 294 // The unaccounted for 0 at the top of the stack 295 int mysterious_size = intSize; 296 297 //This is the name of the file which is present on the initial stack 298 //It's purpose is to let the user space linker examine the original file. 299 int file_name_size = filename.size() + 1; 300 301 int env_data_size = 0; 302 for (int i = 0; i < envp.size(); ++i) { 303 env_data_size += envp[i].size() + 1; 304 } 305 int arg_data_size = 0; 306 for (int i = 0; i < argv.size(); ++i) { 307 arg_data_size += argv[i].size() + 1; 308 } 309 310 //The info_block needs to be padded so it's size is a multiple of the 311 //alignment mask. Also, it appears that there needs to be at least some 312 //padding, so if the size is already a multiple, we need to increase it 313 //anyway. 314 int info_block_size = 315 (file_name_size + 316 env_data_size + 317 arg_data_size + 318 intSize) & alignmentMask; 319 320 int info_block_padding = 321 info_block_size - 322 file_name_size - 323 env_data_size - 324 arg_data_size; 325 326 //Each auxilliary vector is two 8 byte words 327 int aux_array_size = intSize * 2 * (auxv.size() + 1); 328 329 int envp_array_size = intSize * (envp.size() + 1); 330 int argv_array_size = intSize * (argv.size() + 1); 331 332 int argc_size = intSize; 333 int window_save_size = intSize * 16; 334 335 int space_needed = 336 mysterious_size + 337 info_block_size + 338 aux_array_size + 339 envp_array_size + 340 argv_array_size + 341 argc_size + 342 window_save_size; 343 344 stack_min = stack_base - space_needed; 345 stack_min &= alignmentMask; 346 stack_size = stack_base - stack_min; 347 348 // map memory 349 pTable->allocate(roundDown(stack_min, pageSize), 350 roundUp(stack_size, pageSize)); 351 352 // map out initial stack contents 353 Addr mysterious_base = stack_base - mysterious_size; 354 Addr file_name_base = mysterious_base - file_name_size; 355 Addr env_data_base = file_name_base - env_data_size; 356 Addr arg_data_base = env_data_base - arg_data_size; 357 Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding; 358 Addr envp_array_base = auxv_array_base - envp_array_size; 359 Addr argv_array_base = envp_array_base - argv_array_size; 360 Addr argc_base = argv_array_base - argc_size; 361#ifndef NDEBUG 362 // only used in DPRINTF 363 Addr window_save_base = argc_base - window_save_size; 364#endif 365 366 DPRINTF(Sparc, "The addresses of items on the initial stack:\n"); 367 DPRINTF(Sparc, "0x%x - file name\n", file_name_base); 368 DPRINTF(Sparc, "0x%x - env data\n", env_data_base); 369 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base); 370 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base); 371 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base); 372 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base); 373 DPRINTF(Sparc, "0x%x - argc \n", argc_base); 374 DPRINTF(Sparc, "0x%x - window save\n", window_save_base); 375 DPRINTF(Sparc, "0x%x - stack min\n", stack_min); 376 377 // write contents to stack 378 379 // figure out argc 380 uint64_t argc = argv.size(); 381 uint64_t guestArgc = TheISA::htog(argc); 382 383 //Write out the mysterious 0 384 uint64_t mysterious_zero = 0; 385 initVirtMem->writeBlob(mysterious_base, 386 (uint8_t*)&mysterious_zero, mysterious_size); 387 388 //Write the file name 389 initVirtMem->writeString(file_name_base, filename.c_str()); 390 391 //Copy the aux stuff 392 for(int x = 0; x < auxv.size(); x++) 393 { 394 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 395 (uint8_t*)&(auxv[x].a_type), intSize); 396 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 397 (uint8_t*)&(auxv[x].a_val), intSize); 398 } 399 //Write out the terminating zeroed auxilliary vector 400 const uint64_t zero = 0; 401 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 402 (uint8_t*)&zero, 2 * intSize); 403 404 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 405 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 406 407 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 408 409 //Stuff the trap handlers into the processes address space. 410 //Since the stack grows down and is the highest area in the processes 411 //address space, we can put stuff above it and stay out of the way. 412 int fillSize = sizeof(MachInst) * numFillInsts; 413 int spillSize = sizeof(MachInst) * numSpillInsts; 414 fillStart = stack_base; 415 spillStart = fillStart + fillSize; 416 initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler, fillSize); 417 initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler, spillSize); 418 419 //Set up the thread context to start running the process 420 threadContexts[0]->setIntReg(ArgumentReg0, argc); 421 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 422 threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias); 423 424 Addr prog_entry = objFile->entryPoint(); 425 threadContexts[0]->setPC(prog_entry); 426 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 427 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 428 429// num_processes++; 430} 431