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