process.cc revision 4863
1/* 2 * Copyright (c) 2003-2006 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/* 33 * Copyright (c) 2007 The Hewlett-Packard Development Company 34 * All rights reserved. 35 * 36 * Redistribution and use of this software in source and binary forms, 37 * with or without modification, are permitted provided that the 38 * following conditions are met: 39 * 40 * The software must be used only for Non-Commercial Use which means any 41 * use which is NOT directed to receiving any direct monetary 42 * compensation for, or commercial advantage from such use. Illustrative 43 * examples of non-commercial use are academic research, personal study, 44 * teaching, education and corporate research & development. 45 * Illustrative examples of commercial use are distributing products for 46 * commercial advantage and providing services using the software for 47 * commercial advantage. 48 * 49 * If you wish to use this software or functionality therein that may be 50 * covered by patents for commercial use, please contact: 51 * Director of Intellectual Property Licensing 52 * Office of Strategy and Technology 53 * Hewlett-Packard Company 54 * 1501 Page Mill Road 55 * Palo Alto, California 94304 56 * 57 * Redistributions of source code must retain the above copyright notice, 58 * this list of conditions and the following disclaimer. Redistributions 59 * in binary form must reproduce the above copyright notice, this list of 60 * conditions and the following disclaimer in the documentation and/or 61 * 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 "cpu/thread_context.hh" 97#include "mem/page_table.hh" 98#include "mem/translating_port.hh" 99#include "sim/process_impl.hh" 100#include "sim/system.hh" 101 102using namespace std; 103using namespace X86ISA; 104 105M5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val) 106{ 107 a_type = TheISA::htog(type); 108 a_val = TheISA::htog(val); 109} 110 111X86LiveProcess::X86LiveProcess(const std::string &nm, ObjectFile *objFile, 112 System *_system, int stdin_fd, int stdout_fd, int stderr_fd, 113 std::vector<std::string> &argv, std::vector<std::string> &envp, 114 const std::string &cwd, 115 uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, 116 uint64_t _pid, uint64_t _ppid) 117 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd, 118 argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid) 119{ 120 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 121 brk_point = roundUp(brk_point, VMPageSize); 122 123 // Set pointer for next thread stack. Reserve 8M for main stack. 124 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 125 126 // Set up stack. On X86_64 Linux, stack goes from the top of memory 127 // downward, less the hole for the kernel address space plus one page 128 // for undertermined purposes. 129 stack_base = (Addr)0x7FFFFFFFF000ULL; 130 131 // Set up region for mmaps. This was determined empirically and may not 132 // always be correct. 133 mmap_start = mmap_end = 0x2aaaaaaab000; 134} 135 136void X86LiveProcess::handleTrap(int trapNum, ThreadContext *tc) 137{ 138 switch(trapNum) 139 { 140 default: 141 panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum); 142 } 143} 144 145void 146X86LiveProcess::startup() 147{ 148 argsInit(sizeof(IntReg), VMPageSize); 149 for(int i = 0; i < NUM_SEGMENTREGS; i++) 150 threadContexts[0]->setMiscRegNoEffect(MISCREG_ES_BASE + i, 0); 151} 152 153void 154X86LiveProcess::argsInit(int intSize, int pageSize) 155{ 156 typedef M5_64_auxv_t auxv_t; 157 Process::startup(); 158 159 string filename; 160 if(argv.size() < 1) 161 filename = ""; 162 else 163 filename = argv[0]; 164 165 //We want 16 byte alignment 166 uint64_t align = 16; 167 168 // load object file into target memory 169 objFile->loadSections(initVirtMem); 170 171 enum X86CpuFeature { 172 X86_OnboardFPU = 1 << 0, 173 X86_VirtualModeExtensions = 1 << 1, 174 X86_DebuggingExtensions = 1 << 2, 175 X86_PageSizeExtensions = 1 << 3, 176 177 X86_TimeStampCounter = 1 << 4, 178 X86_ModelSpecificRegisters = 1 << 5, 179 X86_PhysicalAddressExtensions = 1 << 6, 180 X86_MachineCheckExtensions = 1 << 7, 181 182 X86_CMPXCHG8Instruction = 1 << 8, 183 X86_OnboardAPIC = 1 << 9, 184 X86_SYSENTER_SYSEXIT = 1 << 11, 185 186 X86_MemoryTypeRangeRegisters = 1 << 12, 187 X86_PageGlobalEnable = 1 << 13, 188 X86_MachineCheckArchitecture = 1 << 14, 189 X86_CMOVInstruction = 1 << 15, 190 191 X86_PageAttributeTable = 1 << 16, 192 X86_36BitPSEs = 1 << 17, 193 X86_ProcessorSerialNumber = 1 << 18, 194 X86_CLFLUSHInstruction = 1 << 19, 195 196 X86_DebugTraceStore = 1 << 21, 197 X86_ACPIViaMSR = 1 << 22, 198 X86_MultimediaExtensions = 1 << 23, 199 200 X86_FXSAVE_FXRSTOR = 1 << 24, 201 X86_StreamingSIMDExtensions = 1 << 25, 202 X86_StreamingSIMDExtensions2 = 1 << 26, 203 X86_CPUSelfSnoop = 1 << 27, 204 205 X86_HyperThreading = 1 << 28, 206 X86_AutomaticClockControl = 1 << 29, 207 X86_IA64Processor = 1 << 30 208 }; 209 210 //Setup the auxilliary vectors. These will already have endian conversion. 211 //Auxilliary vectors are loaded only for elf formatted executables. 212 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 213 if(elfObject) 214 { 215 uint64_t features = 216 X86_OnboardFPU | 217 X86_VirtualModeExtensions | 218 X86_DebuggingExtensions | 219 X86_PageSizeExtensions | 220 X86_TimeStampCounter | 221 X86_ModelSpecificRegisters | 222 X86_PhysicalAddressExtensions | 223 X86_MachineCheckExtensions | 224 X86_CMPXCHG8Instruction | 225 X86_OnboardAPIC | 226 X86_SYSENTER_SYSEXIT | 227 X86_MemoryTypeRangeRegisters | 228 X86_PageGlobalEnable | 229 X86_MachineCheckArchitecture | 230 X86_CMOVInstruction | 231 X86_PageAttributeTable | 232 X86_36BitPSEs | 233// X86_ProcessorSerialNumber | 234 X86_CLFLUSHInstruction | 235// X86_DebugTraceStore | 236// X86_ACPIViaMSR | 237 X86_MultimediaExtensions | 238 X86_FXSAVE_FXRSTOR | 239 X86_StreamingSIMDExtensions | 240 X86_StreamingSIMDExtensions2 | 241// X86_CPUSelfSnoop | 242// X86_HyperThreading | 243// X86_AutomaticClockControl | 244// X86_IA64Processor | 245 0; 246 247 //Bits which describe the system hardware capabilities 248 //XXX Figure out what these should be 249 auxv.push_back(auxv_t(M5_AT_HWCAP, features)); 250 //The system page size 251 auxv.push_back(auxv_t(M5_AT_PAGESZ, X86ISA::VMPageSize)); 252 //Frequency at which times() increments 253 auxv.push_back(auxv_t(M5_AT_CLKTCK, 100)); 254 // For statically linked executables, this is the virtual address of the 255 // program header tables if they appear in the executable image 256 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable())); 257 // This is the size of a program header entry from the elf file. 258 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize())); 259 // This is the number of program headers from the original elf file. 260 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount())); 261 //Defined to be 100 in the kernel source. 262 //This is the address of the elf "interpreter", It should be set 263 //to 0 for regular executables. It should be something else 264 //(not sure what) for dynamic libraries. 265 auxv.push_back(auxv_t(M5_AT_BASE, 0)); 266 267 //XXX Figure out what this should be. 268 auxv.push_back(auxv_t(M5_AT_FLAGS, 0)); 269 //The entry point to the program 270 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint())); 271 //Different user and group IDs 272 auxv.push_back(auxv_t(M5_AT_UID, uid())); 273 auxv.push_back(auxv_t(M5_AT_EUID, euid())); 274 auxv.push_back(auxv_t(M5_AT_GID, gid())); 275 auxv.push_back(auxv_t(M5_AT_EGID, egid())); 276 //Whether to enable "secure mode" in the executable 277 auxv.push_back(auxv_t(M5_AT_SECURE, 0)); 278 //The string "x86_64" with unknown meaning 279 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0)); 280 } 281 282 //Figure out how big the initial stack needs to be 283 284 // A sentry NULL void pointer at the top of the stack. 285 int sentry_size = intSize; 286 287 //This is the name of the file which is present on the initial stack 288 //It's purpose is to let the user space linker examine the original file. 289 int file_name_size = filename.size() + 1; 290 291 string platform = "x86_64"; 292 int aux_data_size = platform.size() + 1; 293 294 int env_data_size = 0; 295 for (int i = 0; i < envp.size(); ++i) { 296 env_data_size += envp[i].size() + 1; 297 } 298 int arg_data_size = 0; 299 for (int i = 0; i < argv.size(); ++i) { 300 arg_data_size += argv[i].size() + 1; 301 } 302 303 //The info_block needs to be padded so it's size is a multiple of the 304 //alignment mask. Also, it appears that there needs to be at least some 305 //padding, so if the size is already a multiple, we need to increase it 306 //anyway. 307 int base_info_block_size = 308 sentry_size + file_name_size + env_data_size + arg_data_size; 309 310 int info_block_size = roundUp(base_info_block_size, align); 311 312 int info_block_padding = info_block_size - base_info_block_size; 313 314 //Each auxilliary vector is two 8 byte words 315 int aux_array_size = intSize * 2 * (auxv.size() + 1); 316 317 int envp_array_size = intSize * (envp.size() + 1); 318 int argv_array_size = intSize * (argv.size() + 1); 319 320 int argc_size = intSize; 321 322 //Figure out the size of the contents of the actual initial frame 323 int frame_size = 324 aux_array_size + 325 envp_array_size + 326 argv_array_size + 327 argc_size; 328 329 //There needs to be padding after the auxiliary vector data so that the 330 //very bottom of the stack is aligned properly. 331 int partial_size = frame_size + aux_data_size; 332 int aligned_partial_size = roundUp(partial_size, align); 333 int aux_padding = aligned_partial_size - partial_size; 334 335 int space_needed = 336 info_block_size + 337 aux_data_size + 338 aux_padding + 339 frame_size; 340 341 stack_min = stack_base - space_needed; 342 stack_min = roundDown(stack_min, align); 343 stack_size = stack_base - stack_min; 344 345 // map memory 346 pTable->allocate(roundDown(stack_min, pageSize), 347 roundUp(stack_size, pageSize)); 348 349 // map out initial stack contents 350 Addr sentry_base = stack_base - sentry_size; 351 Addr file_name_base = sentry_base - file_name_size; 352 Addr env_data_base = file_name_base - env_data_size; 353 Addr arg_data_base = env_data_base - arg_data_size; 354 Addr aux_data_base = arg_data_base - info_block_padding - aux_data_size; 355 Addr auxv_array_base = aux_data_base - aux_array_size - aux_padding; 356 Addr envp_array_base = auxv_array_base - envp_array_size; 357 Addr argv_array_base = envp_array_base - argv_array_size; 358 Addr argc_base = argv_array_base - argc_size; 359 360 DPRINTF(X86, "The addresses of items on the initial stack:\n"); 361 DPRINTF(X86, "0x%x - file name\n", file_name_base); 362 DPRINTF(X86, "0x%x - env data\n", env_data_base); 363 DPRINTF(X86, "0x%x - arg data\n", arg_data_base); 364 DPRINTF(X86, "0x%x - aux data\n", aux_data_base); 365 DPRINTF(X86, "0x%x - auxv array\n", auxv_array_base); 366 DPRINTF(X86, "0x%x - envp array\n", envp_array_base); 367 DPRINTF(X86, "0x%x - argv array\n", argv_array_base); 368 DPRINTF(X86, "0x%x - argc \n", argc_base); 369 DPRINTF(X86, "0x%x - stack min\n", stack_min); 370 371 // write contents to stack 372 373 // figure out argc 374 uint64_t argc = argv.size(); 375 uint64_t guestArgc = TheISA::htog(argc); 376 377 //Write out the sentry void * 378 uint64_t sentry_NULL = 0; 379 initVirtMem->writeBlob(sentry_base, 380 (uint8_t*)&sentry_NULL, sentry_size); 381 382 //Write the file name 383 initVirtMem->writeString(file_name_base, filename.c_str()); 384 385 //Fix up the aux vector which points to the "platform" string 386 assert(auxv[auxv.size() - 1].a_type = M5_AT_PLATFORM); 387 auxv[auxv.size() - 1].a_val = aux_data_base; 388 389 //Copy the aux stuff 390 for(int x = 0; x < auxv.size(); x++) 391 { 392 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 393 (uint8_t*)&(auxv[x].a_type), intSize); 394 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 395 (uint8_t*)&(auxv[x].a_val), intSize); 396 } 397 //Write out the terminating zeroed auxilliary vector 398 const uint64_t zero = 0; 399 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 400 (uint8_t*)&zero, 2 * intSize); 401 402 initVirtMem->writeString(aux_data_base, platform.c_str()); 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 //Set the stack pointer register 410 threadContexts[0]->setIntReg(StackPointerReg, stack_min); 411 412 Addr prog_entry = objFile->entryPoint(); 413 threadContexts[0]->setPC(prog_entry); 414 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 415 416 //Align the "stack_min" to a page boundary. 417 stack_min = roundDown(stack_min, pageSize); 418 419// num_processes++; 420} 421