process.cc revision 8232
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/handlers.hh" 34#include "arch/sparc/isa_traits.hh" 35#include "arch/sparc/process.hh" 36#include "arch/sparc/registers.hh" 37#include "arch/sparc/types.hh" 38#include "base/loader/elf_object.hh" 39#include "base/loader/object_file.hh" 40#include "base/misc.hh" 41#include "cpu/thread_context.hh" 42#include "debug/Stack.hh" 43#include "mem/page_table.hh" 44#include "mem/translating_port.hh" 45#include "sim/process_impl.hh" 46#include "sim/system.hh" 47 48using namespace std; 49using namespace SparcISA; 50 51static const int FirstArgumentReg = 8; 52 53 54SparcLiveProcess::SparcLiveProcess(LiveProcessParams * params, 55 ObjectFile *objFile, Addr _StackBias) 56 : LiveProcess(params, objFile), StackBias(_StackBias) 57{ 58 59 // XXX all the below need to be updated for SPARC - Ali 60 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 61 brk_point = roundUp(brk_point, VMPageSize); 62 63 // Set pointer for next thread stack. Reserve 8M for main stack. 64 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 65 66 // Initialize these to 0s 67 fillStart = 0; 68 spillStart = 0; 69} 70 71void 72SparcLiveProcess::handleTrap(int trapNum, ThreadContext *tc) 73{ 74 PCState pc = tc->pcState(); 75 switch (trapNum) { 76 case 0x01: // Software breakpoint 77 warn("Software breakpoint encountered at pc %#x.\n", pc.pc()); 78 break; 79 case 0x02: // Division by zero 80 warn("Software signaled a division by zero at pc %#x.\n", pc.pc()); 81 break; 82 case 0x03: // Flush window trap 83 flushWindows(tc); 84 break; 85 case 0x04: // Clean windows 86 warn("Ignoring process request for clean register " 87 "windows at pc %#x.\n", pc.pc()); 88 break; 89 case 0x05: // Range check 90 warn("Software signaled a range check at pc %#x.\n", pc.pc()); 91 break; 92 case 0x06: // Fix alignment 93 warn("Ignoring process request for os assisted unaligned accesses " 94 "at pc %#x.\n", pc.pc()); 95 break; 96 case 0x07: // Integer overflow 97 warn("Software signaled an integer overflow at pc %#x.\n", pc.pc()); 98 break; 99 case 0x32: // Get integer condition codes 100 warn("Ignoring process request to get the integer condition codes " 101 "at pc %#x.\n", pc.pc()); 102 break; 103 case 0x33: // Set integer condition codes 104 warn("Ignoring process request to set the integer condition codes " 105 "at pc %#x.\n", pc.pc()); 106 break; 107 default: 108 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum); 109 } 110} 111 112void 113SparcLiveProcess::initState() 114{ 115 LiveProcess::initState(); 116 117 ThreadContext *tc = system->getThreadContext(contextIds[0]); 118 // From the SPARC ABI 119 120 // Setup default FP state 121 tc->setMiscRegNoEffect(MISCREG_FSR, 0); 122 123 tc->setMiscRegNoEffect(MISCREG_TICK, 0); 124 125 /* 126 * Register window management registers 127 */ 128 129 // No windows contain info from other programs 130 // tc->setMiscRegNoEffect(MISCREG_OTHERWIN, 0); 131 tc->setIntReg(NumIntArchRegs + 6, 0); 132 // There are no windows to pop 133 // tc->setMiscRegNoEffect(MISCREG_CANRESTORE, 0); 134 tc->setIntReg(NumIntArchRegs + 4, 0); 135 // All windows are available to save into 136 // tc->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2); 137 tc->setIntReg(NumIntArchRegs + 3, NWindows - 2); 138 // All windows are "clean" 139 // tc->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows); 140 tc->setIntReg(NumIntArchRegs + 5, NWindows); 141 // Start with register window 0 142 tc->setMiscReg(MISCREG_CWP, 0); 143 // Always use spill and fill traps 0 144 // tc->setMiscRegNoEffect(MISCREG_WSTATE, 0); 145 tc->setIntReg(NumIntArchRegs + 7, 0); 146 // Set the trap level to 0 147 tc->setMiscRegNoEffect(MISCREG_TL, 0); 148 // Set the ASI register to something fixed 149 tc->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY); 150 151 /* 152 * T1 specific registers 153 */ 154 // Turn on the icache, dcache, dtb translation, and itb translation. 155 tc->setMiscRegNoEffect(MISCREG_MMU_LSU_CTRL, 15); 156} 157 158void 159Sparc32LiveProcess::initState() 160{ 161 SparcLiveProcess::initState(); 162 163 ThreadContext *tc = system->getThreadContext(contextIds[0]); 164 // The process runs in user mode with 32 bit addresses 165 tc->setMiscReg(MISCREG_PSTATE, 0x0a); 166 167 argsInit(32 / 8, VMPageSize); 168} 169 170void 171Sparc64LiveProcess::initState() 172{ 173 SparcLiveProcess::initState(); 174 175 ThreadContext *tc = system->getThreadContext(contextIds[0]); 176 // The process runs in user mode 177 tc->setMiscReg(MISCREG_PSTATE, 0x02); 178 179 argsInit(sizeof(IntReg), VMPageSize); 180} 181 182template<class IntType> 183void 184SparcLiveProcess::argsInit(int pageSize) 185{ 186 int intSize = sizeof(IntType); 187 188 typedef AuxVector<IntType> auxv_t; 189 190 std::vector<auxv_t> auxv; 191 192 string filename; 193 if (argv.size() < 1) 194 filename = ""; 195 else 196 filename = argv[0]; 197 198 // Even for a 32 bit process, the ABI says we still need to 199 // maintain double word alignment of the stack pointer. 200 uint64_t align = 16; 201 202 // load object file into target memory 203 objFile->loadSections(initVirtMem); 204 205 enum hardwareCaps 206 { 207 M5_HWCAP_SPARC_FLUSH = 1, 208 M5_HWCAP_SPARC_STBAR = 2, 209 M5_HWCAP_SPARC_SWAP = 4, 210 M5_HWCAP_SPARC_MULDIV = 8, 211 M5_HWCAP_SPARC_V9 = 16, 212 // This one should technically only be set 213 // if there is a cheetah or cheetah_plus tlb, 214 // but we'll use it all the time 215 M5_HWCAP_SPARC_ULTRA3 = 32 216 }; 217 218 const int64_t hwcap = 219 M5_HWCAP_SPARC_FLUSH | 220 M5_HWCAP_SPARC_STBAR | 221 M5_HWCAP_SPARC_SWAP | 222 M5_HWCAP_SPARC_MULDIV | 223 M5_HWCAP_SPARC_V9 | 224 M5_HWCAP_SPARC_ULTRA3; 225 226 // Setup the auxilliary vectors. These will already have endian conversion. 227 // Auxilliary vectors are loaded only for elf formatted executables. 228 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 229 if (elfObject) { 230 // Bits which describe the system hardware capabilities 231 auxv.push_back(auxv_t(M5_AT_HWCAP, hwcap)); 232 // The system page size 233 auxv.push_back(auxv_t(M5_AT_PAGESZ, SparcISA::VMPageSize)); 234 // Defined to be 100 in the kernel source. 235 // Frequency at which times() increments 236 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100)); 237 // For statically linked executables, this is the virtual address of the 238 // program header tables if they appear in the executable image 239 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable())); 240 // This is the size of a program header entry from the elf file. 241 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize())); 242 // This is the number of program headers from the original elf file. 243 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount())); 244 // This is the address of the elf "interpreter", It should be set 245 // to 0 for regular executables. It should be something else 246 // (not sure what) for dynamic libraries. 247 auxv.push_back(auxv_t(M5_AT_BASE, 0)); 248 // This is hardwired to 0 in the elf loading code in the kernel 249 auxv.push_back(auxv_t(M5_AT_FLAGS, 0)); 250 // The entry point to the program 251 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint())); 252 // Different user and group IDs 253 auxv.push_back(auxv_t(M5_AT_UID, uid())); 254 auxv.push_back(auxv_t(M5_AT_EUID, euid())); 255 auxv.push_back(auxv_t(M5_AT_GID, gid())); 256 auxv.push_back(auxv_t(M5_AT_EGID, egid())); 257 // Whether to enable "secure mode" in the executable 258 auxv.push_back(auxv_t(M5_AT_SECURE, 0)); 259 } 260 261 // Figure out how big the initial stack needs to be 262 263 // The unaccounted for 8 byte 0 at the top of the stack 264 int sentry_size = 8; 265 266 // This is the name of the file which is present on the initial stack 267 // It's purpose is to let the user space linker examine the original file. 268 int file_name_size = filename.size() + 1; 269 270 int env_data_size = 0; 271 for (int i = 0; i < envp.size(); ++i) { 272 env_data_size += envp[i].size() + 1; 273 } 274 int arg_data_size = 0; 275 for (int i = 0; i < argv.size(); ++i) { 276 arg_data_size += argv[i].size() + 1; 277 } 278 279 // The info_block. 280 int base_info_block_size = 281 sentry_size + file_name_size + env_data_size + arg_data_size; 282 283 int info_block_size = roundUp(base_info_block_size, align); 284 285 int info_block_padding = info_block_size - base_info_block_size; 286 287 // Each auxilliary vector is two words 288 int aux_array_size = intSize * 2 * (auxv.size() + 1); 289 290 int envp_array_size = intSize * (envp.size() + 1); 291 int argv_array_size = intSize * (argv.size() + 1); 292 293 int argc_size = intSize; 294 int window_save_size = intSize * 16; 295 296 // Figure out the size of the contents of the actual initial frame 297 int frame_size = 298 aux_array_size + 299 envp_array_size + 300 argv_array_size + 301 argc_size + 302 window_save_size; 303 304 // There needs to be padding after the auxiliary vector data so that the 305 // very bottom of the stack is aligned properly. 306 int aligned_partial_size = roundUp(frame_size, align); 307 int aux_padding = aligned_partial_size - frame_size; 308 309 int space_needed = 310 info_block_size + 311 aux_padding + 312 frame_size; 313 314 stack_min = stack_base - space_needed; 315 stack_min = roundDown(stack_min, align); 316 stack_size = stack_base - stack_min; 317 318 // Allocate space for the stack 319 pTable->allocate(roundDown(stack_min, pageSize), 320 roundUp(stack_size, pageSize)); 321 322 // map out initial stack contents 323 IntType sentry_base = stack_base - sentry_size; 324 IntType file_name_base = sentry_base - file_name_size; 325 IntType env_data_base = file_name_base - env_data_size; 326 IntType arg_data_base = env_data_base - arg_data_size; 327 IntType auxv_array_base = arg_data_base - 328 info_block_padding - aux_array_size - aux_padding; 329 IntType envp_array_base = auxv_array_base - envp_array_size; 330 IntType argv_array_base = envp_array_base - argv_array_size; 331 IntType argc_base = argv_array_base - argc_size; 332#if TRACING_ON 333 IntType window_save_base = argc_base - window_save_size; 334#endif 335 336 DPRINTF(Stack, "The addresses of items on the initial stack:\n"); 337 DPRINTF(Stack, "%#x - sentry NULL\n", sentry_base); 338 DPRINTF(Stack, "filename = %s\n", filename); 339 DPRINTF(Stack, "%#x - file name\n", file_name_base); 340 DPRINTF(Stack, "%#x - env data\n", env_data_base); 341 DPRINTF(Stack, "%#x - arg data\n", arg_data_base); 342 DPRINTF(Stack, "%#x - auxv array\n", auxv_array_base); 343 DPRINTF(Stack, "%#x - envp array\n", envp_array_base); 344 DPRINTF(Stack, "%#x - argv array\n", argv_array_base); 345 DPRINTF(Stack, "%#x - argc \n", argc_base); 346 DPRINTF(Stack, "%#x - window save\n", window_save_base); 347 DPRINTF(Stack, "%#x - stack min\n", stack_min); 348 349 assert(window_save_base == stack_min); 350 351 // write contents to stack 352 353 // figure out argc 354 IntType argc = argv.size(); 355 IntType guestArgc = SparcISA::htog(argc); 356 357 // Write out the sentry void * 358 uint64_t sentry_NULL = 0; 359 initVirtMem->writeBlob(sentry_base, 360 (uint8_t*)&sentry_NULL, sentry_size); 361 362 // Write the file name 363 initVirtMem->writeString(file_name_base, filename.c_str()); 364 365 // Copy the aux stuff 366 for (int x = 0; x < auxv.size(); x++) { 367 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 368 (uint8_t*)&(auxv[x].a_type), intSize); 369 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 370 (uint8_t*)&(auxv[x].a_val), intSize); 371 } 372 373 // Write out the terminating zeroed auxilliary vector 374 const IntType zero = 0; 375 initVirtMem->writeBlob(auxv_array_base + intSize * 2 * auxv.size(), 376 (uint8_t*)&zero, intSize); 377 initVirtMem->writeBlob(auxv_array_base + intSize * (2 * auxv.size() + 1), 378 (uint8_t*)&zero, intSize); 379 380 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 381 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 382 383 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 384 385 // Set up space for the trap handlers into the processes address space. 386 // Since the stack grows down and there is reserved address space abov 387 // it, we can put stuff above it and stay out of the way. 388 fillStart = stack_base; 389 spillStart = fillStart + sizeof(MachInst) * numFillInsts; 390 391 ThreadContext *tc = system->getThreadContext(contextIds[0]); 392 // Set up the thread context to start running the process 393 // assert(NumArgumentRegs >= 2); 394 // tc->setIntReg(ArgumentReg[0], argc); 395 // tc->setIntReg(ArgumentReg[1], argv_array_base); 396 tc->setIntReg(StackPointerReg, stack_min - StackBias); 397 398 // %g1 is a pointer to a function that should be run at exit. Since we 399 // don't have anything like that, it should be set to 0. 400 tc->setIntReg(1, 0); 401 402 tc->pcState(objFile->entryPoint()); 403 404 // Align the "stack_min" to a page boundary. 405 stack_min = roundDown(stack_min, pageSize); 406 407// num_processes++; 408} 409 410void 411Sparc64LiveProcess::argsInit(int intSize, int pageSize) 412{ 413 SparcLiveProcess::argsInit<uint64_t>(pageSize); 414 415 // Stuff the trap handlers into the process address space 416 initVirtMem->writeBlob(fillStart, 417 (uint8_t*)fillHandler64, sizeof(MachInst) * numFillInsts); 418 initVirtMem->writeBlob(spillStart, 419 (uint8_t*)spillHandler64, sizeof(MachInst) * numSpillInsts); 420} 421 422void 423Sparc32LiveProcess::argsInit(int intSize, int pageSize) 424{ 425 SparcLiveProcess::argsInit<uint32_t>(pageSize); 426 427 // Stuff the trap handlers into the process address space 428 initVirtMem->writeBlob(fillStart, 429 (uint8_t*)fillHandler32, sizeof(MachInst) * numFillInsts); 430 initVirtMem->writeBlob(spillStart, 431 (uint8_t*)spillHandler32, sizeof(MachInst) * numSpillInsts); 432} 433 434void Sparc32LiveProcess::flushWindows(ThreadContext *tc) 435{ 436 IntReg Cansave = tc->readIntReg(NumIntArchRegs + 3); 437 IntReg Canrestore = tc->readIntReg(NumIntArchRegs + 4); 438 IntReg Otherwin = tc->readIntReg(NumIntArchRegs + 6); 439 MiscReg CWP = tc->readMiscReg(MISCREG_CWP); 440 MiscReg origCWP = CWP; 441 CWP = (CWP + Cansave + 2) % NWindows; 442 while (NWindows - 2 - Cansave != 0) { 443 if (Otherwin) { 444 panic("Otherwin non-zero.\n"); 445 } else { 446 tc->setMiscReg(MISCREG_CWP, CWP); 447 // Do the stores 448 IntReg sp = tc->readIntReg(StackPointerReg); 449 for (int index = 16; index < 32; index++) { 450 uint32_t regVal = tc->readIntReg(index); 451 regVal = htog(regVal); 452 if (!tc->getMemPort()->tryWriteBlob( 453 sp + (index - 16) * 4, (uint8_t *)®Val, 4)) { 454 warn("Failed to save register to the stack when " 455 "flushing windows.\n"); 456 } 457 } 458 Canrestore--; 459 Cansave++; 460 CWP = (CWP + 1) % NWindows; 461 } 462 } 463 tc->setIntReg(NumIntArchRegs + 3, Cansave); 464 tc->setIntReg(NumIntArchRegs + 4, Canrestore); 465 tc->setMiscReg(MISCREG_CWP, origCWP); 466} 467 468void 469Sparc64LiveProcess::flushWindows(ThreadContext *tc) 470{ 471 IntReg Cansave = tc->readIntReg(NumIntArchRegs + 3); 472 IntReg Canrestore = tc->readIntReg(NumIntArchRegs + 4); 473 IntReg Otherwin = tc->readIntReg(NumIntArchRegs + 6); 474 MiscReg CWP = tc->readMiscReg(MISCREG_CWP); 475 MiscReg origCWP = CWP; 476 CWP = (CWP + Cansave + 2) % NWindows; 477 while (NWindows - 2 - Cansave != 0) { 478 if (Otherwin) { 479 panic("Otherwin non-zero.\n"); 480 } else { 481 tc->setMiscReg(MISCREG_CWP, CWP); 482 // Do the stores 483 IntReg sp = tc->readIntReg(StackPointerReg); 484 for (int index = 16; index < 32; index++) { 485 IntReg regVal = tc->readIntReg(index); 486 regVal = htog(regVal); 487 if (!tc->getMemPort()->tryWriteBlob( 488 sp + 2047 + (index - 16) * 8, (uint8_t *)®Val, 8)) { 489 warn("Failed to save register to the stack when " 490 "flushing windows.\n"); 491 } 492 } 493 Canrestore--; 494 Cansave++; 495 CWP = (CWP + 1) % NWindows; 496 } 497 } 498 tc->setIntReg(NumIntArchRegs + 3, Cansave); 499 tc->setIntReg(NumIntArchRegs + 4, Canrestore); 500 tc->setMiscReg(MISCREG_CWP, origCWP); 501} 502 503IntReg 504Sparc32LiveProcess::getSyscallArg(ThreadContext *tc, int &i) 505{ 506 assert(i < 6); 507 return bits(tc->readIntReg(FirstArgumentReg + i++), 31, 0); 508} 509 510void 511Sparc32LiveProcess::setSyscallArg(ThreadContext *tc, int i, IntReg val) 512{ 513 assert(i < 6); 514 tc->setIntReg(FirstArgumentReg + i, bits(val, 31, 0)); 515} 516 517IntReg 518Sparc64LiveProcess::getSyscallArg(ThreadContext *tc, int &i) 519{ 520 assert(i < 6); 521 return tc->readIntReg(FirstArgumentReg + i++); 522} 523 524void 525Sparc64LiveProcess::setSyscallArg(ThreadContext *tc, int i, IntReg val) 526{ 527 assert(i < 6); 528 tc->setIntReg(FirstArgumentReg + i, val); 529} 530 531void 532SparcLiveProcess::setSyscallReturn(ThreadContext *tc, 533 SyscallReturn return_value) 534{ 535 // check for error condition. SPARC syscall convention is to 536 // indicate success/failure in reg the carry bit of the ccr 537 // and put the return value itself in the standard return value reg (). 538 if (return_value.successful()) { 539 // no error, clear XCC.C 540 tc->setIntReg(NumIntArchRegs + 2, 541 tc->readIntReg(NumIntArchRegs + 2) & 0xEE); 542 // tc->setMiscRegNoEffect(MISCREG_CCR, tc->readMiscRegNoEffect(MISCREG_CCR) & 0xEE); 543 IntReg val = return_value.value(); 544 if (bits(tc->readMiscRegNoEffect( 545 SparcISA::MISCREG_PSTATE), 3, 3)) { 546 val = bits(val, 31, 0); 547 } 548 tc->setIntReg(ReturnValueReg, val); 549 } else { 550 // got an error, set XCC.C 551 tc->setIntReg(NumIntArchRegs + 2, 552 tc->readIntReg(NumIntArchRegs + 2) | 0x11); 553 // tc->setMiscRegNoEffect(MISCREG_CCR, tc->readMiscRegNoEffect(MISCREG_CCR) | 0x11); 554 IntReg val = -return_value.value(); 555 if (bits(tc->readMiscRegNoEffect( 556 SparcISA::MISCREG_PSTATE), 3, 3)) { 557 val = bits(val, 31, 0); 558 } 559 tc->setIntReg(ReturnValueReg, val); 560 } 561} 562