process.cc revision 5188
15659Sgblack@eecs.umich.edu/* 25659Sgblack@eecs.umich.edu * Copyright (c) 2003-2006 The Regents of The University of Michigan 35659Sgblack@eecs.umich.edu * All rights reserved. 45659Sgblack@eecs.umich.edu * 55659Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 65659Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are 75659Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright 85659Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 95659Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 105659Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 115659Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution; 125659Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its 135659Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 145659Sgblack@eecs.umich.edu * this software without specific prior written permission. 155659Sgblack@eecs.umich.edu * 165659Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175659Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185659Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195659Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205659Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215659Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225659Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235659Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245659Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255659Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265659Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275659Sgblack@eecs.umich.edu * 285659Sgblack@eecs.umich.edu * Authors: Gabe Black 295659Sgblack@eecs.umich.edu * Ali Saidi 305659Sgblack@eecs.umich.edu */ 315659Sgblack@eecs.umich.edu 325659Sgblack@eecs.umich.edu/* 335659Sgblack@eecs.umich.edu * Copyright (c) 2007 The Hewlett-Packard Development Company 345659Sgblack@eecs.umich.edu * All rights reserved. 355659Sgblack@eecs.umich.edu * 365659Sgblack@eecs.umich.edu * Redistribution and use of this software in source and binary forms, 375659Sgblack@eecs.umich.edu * with or without modification, are permitted provided that the 385659Sgblack@eecs.umich.edu * following conditions are met: 395659Sgblack@eecs.umich.edu * 405659Sgblack@eecs.umich.edu * The software must be used only for Non-Commercial Use which means any 415659Sgblack@eecs.umich.edu * use which is NOT directed to receiving any direct monetary 425659Sgblack@eecs.umich.edu * compensation for, or commercial advantage from such use. Illustrative 435659Sgblack@eecs.umich.edu * examples of non-commercial use are academic research, personal study, 445659Sgblack@eecs.umich.edu * teaching, education and corporate research & development. 455659Sgblack@eecs.umich.edu * Illustrative examples of commercial use are distributing products for 465659Sgblack@eecs.umich.edu * commercial advantage and providing services using the software for 475659Sgblack@eecs.umich.edu * commercial advantage. 485659Sgblack@eecs.umich.edu * 495659Sgblack@eecs.umich.edu * If you wish to use this software or functionality therein that may be 505659Sgblack@eecs.umich.edu * covered by patents for commercial use, please contact: 515659Sgblack@eecs.umich.edu * Director of Intellectual Property Licensing 525659Sgblack@eecs.umich.edu * Office of Strategy and Technology 535659Sgblack@eecs.umich.edu * Hewlett-Packard Company 545659Sgblack@eecs.umich.edu * 1501 Page Mill Road 555659Sgblack@eecs.umich.edu * Palo Alto, California 94304 565659Sgblack@eecs.umich.edu * 575659Sgblack@eecs.umich.edu * Redistributions of source code must retain the above copyright notice, 585659Sgblack@eecs.umich.edu * this list of conditions and the following disclaimer. Redistributions 595659Sgblack@eecs.umich.edu * in binary form must reproduce the above copyright notice, this list of 605659Sgblack@eecs.umich.edu * conditions and the following disclaimer in the documentation and/or 615659Sgblack@eecs.umich.edu * other materials provided with the distribution. Neither the name of 62 * the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its 63 * contributors may be used to endorse or promote products derived from 64 * this software without specific prior written permission. No right of 65 * sublicense is granted herewith. Derivatives of the software and 66 * output created using the software may be prepared, but only for 67 * Non-Commercial Uses. Derivatives of the software may be shared with 68 * others provided: (i) the others agree to abide by the list of 69 * conditions herein which includes the Non-Commercial Use restrictions; 70 * and (ii) such Derivatives of the software include the above copyright 71 * notice to acknowledge the contribution from this software where 72 * applicable, this list of conditions and the disclaimer below. 73 * 74 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 75 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 76 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 77 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 78 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 79 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 80 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 81 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 82 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 83 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 85 * 86 * Authors: Gabe Black 87 */ 88 89#include "arch/x86/isa_traits.hh" 90#include "arch/x86/process.hh" 91#include "arch/x86/segmentregs.hh" 92#include "arch/x86/types.hh" 93#include "base/loader/object_file.hh" 94#include "base/loader/elf_object.hh" 95#include "base/misc.hh" 96#include "base/trace.hh" 97#include "cpu/thread_context.hh" 98#include "mem/page_table.hh" 99#include "mem/translating_port.hh" 100#include "sim/process_impl.hh" 101#include "sim/system.hh" 102 103using namespace std; 104using namespace X86ISA; 105 106M5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val) 107{ 108 a_type = TheISA::htog(type); 109 a_val = TheISA::htog(val); 110} 111 112X86LiveProcess::X86LiveProcess(LiveProcessParams * params, 113 ObjectFile *objFile) 114 : LiveProcess(params, objFile) 115{ 116 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 117 brk_point = roundUp(brk_point, VMPageSize); 118 119 // Set pointer for next thread stack. Reserve 8M for main stack. 120 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 121 122 // Set up stack. On X86_64 Linux, stack goes from the top of memory 123 // downward, less the hole for the kernel address space plus one page 124 // for undertermined purposes. 125 stack_base = (Addr)0x7FFFFFFFF000ULL; 126 127 // Set up region for mmaps. This was determined empirically and may not 128 // always be correct. 129 mmap_start = mmap_end = (Addr)0x2aaaaaaab000ULL; 130} 131 132void X86LiveProcess::handleTrap(int trapNum, ThreadContext *tc) 133{ 134 switch(trapNum) 135 { 136 default: 137 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum); 138 } 139} 140 141void 142X86LiveProcess::startup() 143{ 144 if (checkpointRestored) 145 return; 146 147 argsInit(sizeof(IntReg), VMPageSize); 148 149 for (int i = 0; i < threadContexts.size(); i++) { 150 ThreadContext * tc = threadContexts[i]; 151 152 SegAttr dataAttr = 0; 153 dataAttr.writable = 1; 154 dataAttr.readable = 1; 155 dataAttr.expandDown = 0; 156 dataAttr.dpl = 3; 157 dataAttr.defaultSize = 0; 158 dataAttr.longMode = 1; 159 160 //Initialize the segment registers. 161 for(int seg = 0; seg < NUM_SEGMENTREGS; seg++) { 162 tc->setMiscRegNoEffect(MISCREG_SEG_BASE(seg), 0); 163 tc->setMiscRegNoEffect(MISCREG_SEG_ATTR(seg), dataAttr); 164 } 165 166 SegAttr csAttr = 0; 167 csAttr.writable = 0; 168 csAttr.readable = 1; 169 csAttr.expandDown = 0; 170 csAttr.dpl = 3; 171 csAttr.defaultSize = 0; 172 csAttr.longMode = 1; 173 174 tc->setMiscRegNoEffect(MISCREG_CS_ATTR, csAttr); 175 176 //Set up the registers that describe the operating mode. 177 CR0 cr0 = 0; 178 cr0.pg = 1; // Turn on paging. 179 cr0.cd = 0; // Don't disable caching. 180 cr0.nw = 0; // This is bit is defined to be ignored. 181 cr0.am = 0; // No alignment checking 182 cr0.wp = 0; // Supervisor mode can write read only pages 183 cr0.ne = 1; 184 cr0.et = 1; // This should always be 1 185 cr0.ts = 0; // We don't do task switching, so causing fp exceptions 186 // would be pointless. 187 cr0.em = 0; // Allow x87 instructions to execute natively. 188 cr0.mp = 1; // This doesn't really matter, but the manual suggests 189 // setting it to one. 190 cr0.pe = 1; // We're definitely in protected mode. 191 tc->setMiscReg(MISCREG_CR0, cr0); 192 193 Efer efer = 0; 194 efer.sce = 1; // Enable system call extensions. 195 efer.lme = 1; // Enable long mode. 196 efer.lma = 1; // Activate long mode. 197 efer.nxe = 1; // Enable nx support. 198 efer.svme = 0; // Disable svm support for now. It isn't implemented. 199 efer.ffxsr = 1; // Turn on fast fxsave and fxrstor. 200 tc->setMiscReg(MISCREG_EFER, efer); 201 } 202} 203 204void 205X86LiveProcess::argsInit(int intSize, int pageSize) 206{ 207 typedef M5_64_auxv_t auxv_t; 208 Process::startup(); 209 210 string filename; 211 if(argv.size() < 1) 212 filename = ""; 213 else 214 filename = argv[0]; 215 216 //We want 16 byte alignment 217 uint64_t align = 16; 218 219 // load object file into target memory 220 objFile->loadSections(initVirtMem); 221 222 enum X86CpuFeature { 223 X86_OnboardFPU = 1 << 0, 224 X86_VirtualModeExtensions = 1 << 1, 225 X86_DebuggingExtensions = 1 << 2, 226 X86_PageSizeExtensions = 1 << 3, 227 228 X86_TimeStampCounter = 1 << 4, 229 X86_ModelSpecificRegisters = 1 << 5, 230 X86_PhysicalAddressExtensions = 1 << 6, 231 X86_MachineCheckExtensions = 1 << 7, 232 233 X86_CMPXCHG8Instruction = 1 << 8, 234 X86_OnboardAPIC = 1 << 9, 235 X86_SYSENTER_SYSEXIT = 1 << 11, 236 237 X86_MemoryTypeRangeRegisters = 1 << 12, 238 X86_PageGlobalEnable = 1 << 13, 239 X86_MachineCheckArchitecture = 1 << 14, 240 X86_CMOVInstruction = 1 << 15, 241 242 X86_PageAttributeTable = 1 << 16, 243 X86_36BitPSEs = 1 << 17, 244 X86_ProcessorSerialNumber = 1 << 18, 245 X86_CLFLUSHInstruction = 1 << 19, 246 247 X86_DebugTraceStore = 1 << 21, 248 X86_ACPIViaMSR = 1 << 22, 249 X86_MultimediaExtensions = 1 << 23, 250 251 X86_FXSAVE_FXRSTOR = 1 << 24, 252 X86_StreamingSIMDExtensions = 1 << 25, 253 X86_StreamingSIMDExtensions2 = 1 << 26, 254 X86_CPUSelfSnoop = 1 << 27, 255 256 X86_HyperThreading = 1 << 28, 257 X86_AutomaticClockControl = 1 << 29, 258 X86_IA64Processor = 1 << 30 259 }; 260 261 //Setup the auxilliary vectors. These will already have endian conversion. 262 //Auxilliary vectors are loaded only for elf formatted executables. 263 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 264 if(elfObject) 265 { 266 uint64_t features = 267 X86_OnboardFPU | 268 X86_VirtualModeExtensions | 269 X86_DebuggingExtensions | 270 X86_PageSizeExtensions | 271 X86_TimeStampCounter | 272 X86_ModelSpecificRegisters | 273 X86_PhysicalAddressExtensions | 274 X86_MachineCheckExtensions | 275 X86_CMPXCHG8Instruction | 276 X86_OnboardAPIC | 277 X86_SYSENTER_SYSEXIT | 278 X86_MemoryTypeRangeRegisters | 279 X86_PageGlobalEnable | 280 X86_MachineCheckArchitecture | 281 X86_CMOVInstruction | 282 X86_PageAttributeTable | 283 X86_36BitPSEs | 284// X86_ProcessorSerialNumber | 285 X86_CLFLUSHInstruction | 286// X86_DebugTraceStore | 287// X86_ACPIViaMSR | 288 X86_MultimediaExtensions | 289 X86_FXSAVE_FXRSTOR | 290 X86_StreamingSIMDExtensions | 291 X86_StreamingSIMDExtensions2 | 292// X86_CPUSelfSnoop | 293// X86_HyperThreading | 294// X86_AutomaticClockControl | 295// X86_IA64Processor | 296 0; 297 298 //Bits which describe the system hardware capabilities 299 //XXX Figure out what these should be 300 auxv.push_back(auxv_t(M5_AT_HWCAP, features)); 301 //The system page size 302 auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize)); 303 //Frequency at which times() increments 304 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100)); 305 // For statically linked executables, this is the virtual address of the 306 // program header tables if they appear in the executable image 307 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable())); 308 // This is the size of a program header entry from the elf file. 309 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize())); 310 // This is the number of program headers from the original elf file. 311 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount())); 312 //Defined to be 100 in the kernel source. 313 //This is the address of the elf "interpreter", It should be set 314 //to 0 for regular executables. It should be something else 315 //(not sure what) for dynamic libraries. 316 auxv.push_back(auxv_t(M5_AT_BASE, 0)); 317 318 //XXX Figure out what this should be. 319 auxv.push_back(auxv_t(M5_AT_FLAGS, 0)); 320 //The entry point to the program 321 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint())); 322 //Different user and group IDs 323 auxv.push_back(auxv_t(M5_AT_UID, uid())); 324 auxv.push_back(auxv_t(M5_AT_EUID, euid())); 325 auxv.push_back(auxv_t(M5_AT_GID, gid())); 326 auxv.push_back(auxv_t(M5_AT_EGID, egid())); 327 //Whether to enable "secure mode" in the executable 328 auxv.push_back(auxv_t(M5_AT_SECURE, 0)); 329 //The string "x86_64" with unknown meaning 330 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0)); 331 } 332 333 //Figure out how big the initial stack needs to be 334 335 // A sentry NULL void pointer at the top of the stack. 336 int sentry_size = intSize; 337 338 //This is the name of the file which is present on the initial stack 339 //It's purpose is to let the user space linker examine the original file. 340 int file_name_size = filename.size() + 1; 341 342 string platform = "x86_64"; 343 int aux_data_size = platform.size() + 1; 344 345 int env_data_size = 0; 346 for (int i = 0; i < envp.size(); ++i) { 347 env_data_size += envp[i].size() + 1; 348 } 349 int arg_data_size = 0; 350 for (int i = 0; i < argv.size(); ++i) { 351 arg_data_size += argv[i].size() + 1; 352 } 353 354 //The info_block needs to be padded so it's size is a multiple of the 355 //alignment mask. Also, it appears that there needs to be at least some 356 //padding, so if the size is already a multiple, we need to increase it 357 //anyway. 358 int base_info_block_size = 359 sentry_size + file_name_size + env_data_size + arg_data_size; 360 361 int info_block_size = roundUp(base_info_block_size, align); 362 363 int info_block_padding = info_block_size - base_info_block_size; 364 365 //Each auxilliary vector is two 8 byte words 366 int aux_array_size = intSize * 2 * (auxv.size() + 1); 367 368 int envp_array_size = intSize * (envp.size() + 1); 369 int argv_array_size = intSize * (argv.size() + 1); 370 371 int argc_size = intSize; 372 373 //Figure out the size of the contents of the actual initial frame 374 int frame_size = 375 aux_array_size + 376 envp_array_size + 377 argv_array_size + 378 argc_size; 379 380 //There needs to be padding after the auxiliary vector data so that the 381 //very bottom of the stack is aligned properly. 382 int partial_size = frame_size + aux_data_size; 383 int aligned_partial_size = roundUp(partial_size, align); 384 int aux_padding = aligned_partial_size - partial_size; 385 386 int space_needed = 387 info_block_size + 388 aux_data_size + 389 aux_padding + 390 frame_size; 391 392 stack_min = stack_base - space_needed; 393 stack_min = roundDown(stack_min, align); 394 stack_size = stack_base - stack_min; 395 396 // map memory 397 pTable->allocate(roundDown(stack_min, pageSize), 398 roundUp(stack_size, pageSize)); 399 400 // map out initial stack contents 401 Addr sentry_base = stack_base - sentry_size; 402 Addr file_name_base = sentry_base - file_name_size; 403 Addr env_data_base = file_name_base - env_data_size; 404 Addr arg_data_base = env_data_base - arg_data_size; 405 Addr aux_data_base = arg_data_base - info_block_padding - aux_data_size; 406 Addr auxv_array_base = aux_data_base - aux_array_size - aux_padding; 407 Addr envp_array_base = auxv_array_base - envp_array_size; 408 Addr argv_array_base = envp_array_base - argv_array_size; 409 Addr argc_base = argv_array_base - argc_size; 410 411 DPRINTF(X86, "The addresses of items on the initial stack:\n"); 412 DPRINTF(X86, "0x%x - file name\n", file_name_base); 413 DPRINTF(X86, "0x%x - env data\n", env_data_base); 414 DPRINTF(X86, "0x%x - arg data\n", arg_data_base); 415 DPRINTF(X86, "0x%x - aux data\n", aux_data_base); 416 DPRINTF(X86, "0x%x - auxv array\n", auxv_array_base); 417 DPRINTF(X86, "0x%x - envp array\n", envp_array_base); 418 DPRINTF(X86, "0x%x - argv array\n", argv_array_base); 419 DPRINTF(X86, "0x%x - argc \n", argc_base); 420 DPRINTF(X86, "0x%x - stack min\n", stack_min); 421 422 // write contents to stack 423 424 // figure out argc 425 uint64_t argc = argv.size(); 426 uint64_t guestArgc = TheISA::htog(argc); 427 428 //Write out the sentry void * 429 uint64_t sentry_NULL = 0; 430 initVirtMem->writeBlob(sentry_base, 431 (uint8_t*)&sentry_NULL, sentry_size); 432 433 //Write the file name 434 initVirtMem->writeString(file_name_base, filename.c_str()); 435 436 //Fix up the aux vector which points to the "platform" string 437 assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM); 438 auxv[auxv.size() - 1].a_val = aux_data_base; 439 440 //Copy the aux stuff 441 for(int x = 0; x < auxv.size(); x++) 442 { 443 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 444 (uint8_t*)&(auxv[x].a_type), intSize); 445 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 446 (uint8_t*)&(auxv[x].a_val), intSize); 447 } 448 //Write out the terminating zeroed auxilliary vector 449 const uint64_t zero = 0; 450 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 451 (uint8_t*)&zero, 2 * intSize); 452 453 initVirtMem->writeString(aux_data_base, platform.c_str()); 454 455 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 456 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 457 458 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 459 460 //Set the stack pointer register 461 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 462 463 Addr prog_entry = objFile->entryPoint(); 464 threadContexts[0]->setPC(prog_entry); 465 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 466 467 //Align the "stack_min" to a page boundary. 468 stack_min = roundDown(stack_min, pageSize); 469 470// num_processes++; 471} 472