process.cc revision 13617:34a793c681ce
1/* 2 * Copyright (c) 2007-2008 The Florida State University 3 * Copyright (c) 2009 The University of Edinburgh 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer; 10 * redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution; 13 * neither the name of the copyright holders nor the names of its 14 * contributors may be used to endorse or promote products derived from 15 * this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * Authors: Stephen Hines 30 * Timothy M. Jones 31 */ 32 33#include "arch/power/process.hh" 34 35#include "arch/power/isa_traits.hh" 36#include "arch/power/types.hh" 37#include "base/loader/elf_object.hh" 38#include "base/loader/object_file.hh" 39#include "base/logging.hh" 40#include "cpu/thread_context.hh" 41#include "debug/Stack.hh" 42#include "mem/page_table.hh" 43#include "params/Process.hh" 44#include "sim/aux_vector.hh" 45#include "sim/process_impl.hh" 46#include "sim/syscall_return.hh" 47#include "sim/system.hh" 48 49using namespace std; 50using namespace PowerISA; 51 52PowerProcess::PowerProcess(ProcessParams *params, ObjectFile *objFile) 53 : Process(params, 54 new EmulationPageTable(params->name, params->pid, PageBytes), 55 objFile) 56{ 57 fatal_if(params->useArchPT, "Arch page tables not implemented."); 58 // Set up break point (Top of Heap) 59 Addr brk_point = objFile->dataBase() + objFile->dataSize() + 60 objFile->bssSize(); 61 brk_point = roundUp(brk_point, PageBytes); 62 63 Addr stack_base = 0xbf000000L; 64 65 Addr max_stack_size = 8 * 1024 * 1024; 66 67 // Set pointer for next thread stack. Reserve 8M for main stack. 68 Addr next_thread_stack_base = stack_base - max_stack_size; 69 70 // Set up region for mmaps. For now, start at bottom of kuseg space. 71 Addr mmap_end = 0x70000000L; 72 73 memState = make_shared<MemState>(brk_point, stack_base, max_stack_size, 74 next_thread_stack_base, mmap_end); 75} 76 77void 78PowerProcess::initState() 79{ 80 Process::initState(); 81 82 argsInit(MachineBytes, PageBytes); 83} 84 85void 86PowerProcess::argsInit(int intSize, int pageSize) 87{ 88 typedef AuxVector<uint32_t> auxv_t; 89 std::vector<auxv_t> auxv; 90 91 string filename; 92 if (argv.size() < 1) 93 filename = ""; 94 else 95 filename = argv[0]; 96 97 //We want 16 byte alignment 98 uint64_t align = 16; 99 100 // Patch the ld_bias for dynamic executables. 101 updateBias(); 102 103 // load object file into target memory 104 objFile->loadSections(initVirtMem); 105 106 //Setup the auxilliary vectors. These will already have endian conversion. 107 //Auxilliary vectors are loaded only for elf formatted executables. 108 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 109 if (elfObject) { 110 uint32_t features = 0; 111 112 //Bits which describe the system hardware capabilities 113 //XXX Figure out what these should be 114 auxv.push_back(auxv_t(M5_AT_HWCAP, features)); 115 //The system page size 116 auxv.push_back(auxv_t(M5_AT_PAGESZ, PowerISA::PageBytes)); 117 //Frequency at which times() increments 118 auxv.push_back(auxv_t(M5_AT_CLKTCK, 0x64)); 119 // For statically linked executables, this is the virtual address of the 120 // program header tables if they appear in the executable image 121 auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable())); 122 // This is the size of a program header entry from the elf file. 123 auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize())); 124 // This is the number of program headers from the original elf file. 125 auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount())); 126 // This is the base address of the ELF interpreter; it should be 127 // zero for static executables or contain the base address for 128 // dynamic executables. 129 auxv.push_back(auxv_t(M5_AT_BASE, getBias())); 130 //XXX Figure out what this should be. 131 auxv.push_back(auxv_t(M5_AT_FLAGS, 0)); 132 //The entry point to the program 133 auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint())); 134 //Different user and group IDs 135 auxv.push_back(auxv_t(M5_AT_UID, uid())); 136 auxv.push_back(auxv_t(M5_AT_EUID, euid())); 137 auxv.push_back(auxv_t(M5_AT_GID, gid())); 138 auxv.push_back(auxv_t(M5_AT_EGID, egid())); 139 //Whether to enable "secure mode" in the executable 140 auxv.push_back(auxv_t(M5_AT_SECURE, 0)); 141 //The filename of the program 142 auxv.push_back(auxv_t(M5_AT_EXECFN, 0)); 143 //The string "v51" with unknown meaning 144 auxv.push_back(auxv_t(M5_AT_PLATFORM, 0)); 145 } 146 147 //Figure out how big the initial stack nedes to be 148 149 // A sentry NULL void pointer at the top of the stack. 150 int sentry_size = intSize; 151 152 string platform = "v51"; 153 int platform_size = platform.size() + 1; 154 155 // The aux vectors are put on the stack in two groups. The first group are 156 // the vectors that are generated as the elf is loaded. The second group 157 // are the ones that were computed ahead of time and include the platform 158 // string. 159 int aux_data_size = filename.size() + 1; 160 161 int env_data_size = 0; 162 for (int i = 0; i < envp.size(); ++i) { 163 env_data_size += envp[i].size() + 1; 164 } 165 int arg_data_size = 0; 166 for (int i = 0; i < argv.size(); ++i) { 167 arg_data_size += argv[i].size() + 1; 168 } 169 170 int info_block_size = 171 sentry_size + env_data_size + arg_data_size + 172 aux_data_size + platform_size; 173 174 //Each auxilliary vector is two 4 byte words 175 int aux_array_size = intSize * 2 * (auxv.size() + 1); 176 177 int envp_array_size = intSize * (envp.size() + 1); 178 int argv_array_size = intSize * (argv.size() + 1); 179 180 int argc_size = intSize; 181 182 //Figure out the size of the contents of the actual initial frame 183 int frame_size = 184 info_block_size + 185 aux_array_size + 186 envp_array_size + 187 argv_array_size + 188 argc_size; 189 190 //There needs to be padding after the auxiliary vector data so that the 191 //very bottom of the stack is aligned properly. 192 int partial_size = frame_size; 193 int aligned_partial_size = roundUp(partial_size, align); 194 int aux_padding = aligned_partial_size - partial_size; 195 196 int space_needed = frame_size + aux_padding; 197 198 Addr stack_min = memState->getStackBase() - space_needed; 199 stack_min = roundDown(stack_min, align); 200 201 memState->setStackSize(memState->getStackBase() - stack_min); 202 203 // map memory 204 allocateMem(roundDown(stack_min, pageSize), 205 roundUp(memState->getStackSize(), pageSize)); 206 207 // map out initial stack contents 208 uint32_t sentry_base = memState->getStackBase() - sentry_size; 209 uint32_t aux_data_base = sentry_base - aux_data_size; 210 uint32_t env_data_base = aux_data_base - env_data_size; 211 uint32_t arg_data_base = env_data_base - arg_data_size; 212 uint32_t platform_base = arg_data_base - platform_size; 213 uint32_t auxv_array_base = platform_base - aux_array_size - aux_padding; 214 uint32_t envp_array_base = auxv_array_base - envp_array_size; 215 uint32_t argv_array_base = envp_array_base - argv_array_size; 216 uint32_t argc_base = argv_array_base - argc_size; 217 218 DPRINTF(Stack, "The addresses of items on the initial stack:\n"); 219 DPRINTF(Stack, "0x%x - aux data\n", aux_data_base); 220 DPRINTF(Stack, "0x%x - env data\n", env_data_base); 221 DPRINTF(Stack, "0x%x - arg data\n", arg_data_base); 222 DPRINTF(Stack, "0x%x - platform base\n", platform_base); 223 DPRINTF(Stack, "0x%x - auxv array\n", auxv_array_base); 224 DPRINTF(Stack, "0x%x - envp array\n", envp_array_base); 225 DPRINTF(Stack, "0x%x - argv array\n", argv_array_base); 226 DPRINTF(Stack, "0x%x - argc \n", argc_base); 227 DPRINTF(Stack, "0x%x - stack min\n", stack_min); 228 229 // write contents to stack 230 231 // figure out argc 232 uint32_t argc = argv.size(); 233 uint32_t guestArgc = PowerISA::htog(argc); 234 235 //Write out the sentry void * 236 uint32_t sentry_NULL = 0; 237 initVirtMem.writeBlob(sentry_base, 238 (uint8_t*)&sentry_NULL, sentry_size); 239 240 //Fix up the aux vectors which point to other data 241 for (int i = auxv.size() - 1; i >= 0; i--) { 242 if (auxv[i].getHostAuxType() == M5_AT_PLATFORM) { 243 auxv[i].setAuxVal(platform_base); 244 initVirtMem.writeString(platform_base, platform.c_str()); 245 } else if (auxv[i].getHostAuxType() == M5_AT_EXECFN) { 246 auxv[i].setAuxVal(aux_data_base); 247 initVirtMem.writeString(aux_data_base, filename.c_str()); 248 } 249 } 250 251 //Copy the aux stuff 252 for (int x = 0; x < auxv.size(); x++) 253 { 254 initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize, 255 (uint8_t*)&(auxv[x].getAuxType()), intSize); 256 initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 257 (uint8_t*)&(auxv[x].getAuxVal()), intSize); 258 } 259 //Write out the terminating zeroed auxilliary vector 260 const uint64_t zero = 0; 261 initVirtMem.writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 262 (uint8_t*)&zero, 2 * intSize); 263 264 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 265 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 266 267 initVirtMem.writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 268 269 ThreadContext *tc = system->getThreadContext(contextIds[0]); 270 271 //Set the stack pointer register 272 tc->setIntReg(StackPointerReg, stack_min); 273 274 tc->pcState(getStartPC()); 275 276 //Align the "stack_min" to a page boundary. 277 memState->setStackMin(roundDown(stack_min, pageSize)); 278} 279 280RegVal 281PowerProcess::getSyscallArg(ThreadContext *tc, int &i) 282{ 283 assert(i < 5); 284 return tc->readIntReg(ArgumentReg0 + i++); 285} 286 287void 288PowerProcess::setSyscallArg(ThreadContext *tc, int i, RegVal val) 289{ 290 assert(i < 5); 291 tc->setIntReg(ArgumentReg0 + i, val); 292} 293 294void 295PowerProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret) 296{ 297 Cr cr = tc->readIntReg(INTREG_CR); 298 if (sysret.successful()) { 299 cr.cr0.so = 0; 300 } else { 301 cr.cr0.so = 1; 302 } 303 tc->setIntReg(INTREG_CR, cr); 304 tc->setIntReg(ReturnValueReg, sysret.encodedValue()); 305} 306