process.cc revision 3068:35963cbd0a49
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/isa_traits.hh" 33#include "arch/sparc/process.hh" 34#include "base/loader/object_file.hh" 35#include "base/loader/elf_object.hh" 36#include "base/misc.hh" 37#include "cpu/thread_context.hh" 38#include "mem/page_table.hh" 39#include "mem/translating_port.hh" 40#include "sim/system.hh" 41 42using namespace std; 43using namespace SparcISA; 44 45 46SparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile, 47 System *_system, int stdin_fd, int stdout_fd, int stderr_fd, 48 std::vector<std::string> &argv, std::vector<std::string> &envp) 49 : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd, 50 argv, envp) 51{ 52 53 // XXX all the below need to be updated for SPARC - Ali 54 brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 55 brk_point = roundUp(brk_point, VMPageSize); 56 57 // Set up stack. On SPARC Linux, stack goes from the top of memory 58 // downward, less the hole for the kernel address space. 59 stack_base = (Addr)0x80000000000ULL; 60 61 // Set up region for mmaps. Tru64 seems to start just above 0 and 62 // grow up from there. 63 mmap_start = mmap_end = 0xfffff80000000000ULL; 64 65 // Set pointer for next thread stack. Reserve 8M for main stack. 66 next_thread_stack_base = stack_base - (8 * 1024 * 1024); 67} 68 69void 70SparcLiveProcess::startup() 71{ 72 argsInit(MachineBytes, VMPageSize); 73 74 //From the SPARC ABI 75 76 //The process runs in user mode 77 threadContexts[0]->setMiscRegWithEffect(MISCREG_PSTATE, 0x02); 78 79 //Setup default FP state 80 threadContexts[0]->setMiscReg(MISCREG_FSR, 0); 81 82 threadContexts[0]->setMiscReg(MISCREG_TICK, 0); 83 // 84 /* 85 * Register window management registers 86 */ 87 88 //No windows contain info from other programs 89 threadContexts[0]->setMiscRegWithEffect(MISCREG_OTHERWIN, 0); 90 //There are no windows to pop 91 threadContexts[0]->setMiscRegWithEffect(MISCREG_CANRESTORE, 0); 92 //All windows are available to save into 93 threadContexts[0]->setMiscRegWithEffect(MISCREG_CANSAVE, NWindows - 2); 94 //All windows are "clean" 95 threadContexts[0]->setMiscRegWithEffect(MISCREG_CLEANWIN, NWindows); 96 //Start with register window 0 97 threadContexts[0]->setMiscRegWithEffect(MISCREG_CWP, 0); 98} 99 100m5_auxv_t buildAuxVect(int64_t type, int64_t val) 101{ 102 m5_auxv_t result; 103 result.a_type = TheISA::htog(type); 104 result.a_val = TheISA::htog(val); 105 return result; 106} 107 108void 109SparcLiveProcess::argsInit(int intSize, int pageSize) 110{ 111 Process::startup(); 112 113 string filename; 114 if(argv.size() < 1) 115 filename = ""; 116 else 117 filename = argv[0]; 118 119 Addr alignmentMask = ~(intSize - 1); 120 121 // load object file into target memory 122 objFile->loadSections(initVirtMem); 123 124 //These are the auxilliary vector types 125 enum auxTypes 126 { 127 SPARC_AT_HWCAP = 16, 128 SPARC_AT_PAGESZ = 6, 129 SPARC_AT_CLKTCK = 17, 130 SPARC_AT_PHDR = 3, 131 SPARC_AT_PHENT = 4, 132 SPARC_AT_PHNUM = 5, 133 SPARC_AT_BASE = 7, 134 SPARC_AT_FLAGS = 8, 135 SPARC_AT_ENTRY = 9, 136 SPARC_AT_UID = 11, 137 SPARC_AT_EUID = 12, 138 SPARC_AT_GID = 13, 139 SPARC_AT_EGID = 14, 140 SPARC_AT_SECURE = 23 141 }; 142 143 enum hardwareCaps 144 { 145 M5_HWCAP_SPARC_FLUSH = 1, 146 M5_HWCAP_SPARC_STBAR = 2, 147 M5_HWCAP_SPARC_SWAP = 4, 148 M5_HWCAP_SPARC_MULDIV = 8, 149 M5_HWCAP_SPARC_V9 = 16, 150 //This one should technically only be set 151 //if there is a cheetah or cheetah_plus tlb, 152 //but we'll use it all the time 153 M5_HWCAP_SPARC_ULTRA3 = 32 154 }; 155 156 const int64_t hwcap = 157 M5_HWCAP_SPARC_FLUSH | 158 M5_HWCAP_SPARC_STBAR | 159 M5_HWCAP_SPARC_SWAP | 160 M5_HWCAP_SPARC_MULDIV | 161 M5_HWCAP_SPARC_V9 | 162 M5_HWCAP_SPARC_ULTRA3; 163 164 165 //Setup the auxilliary vectors. These will already have endian conversion. 166 //Auxilliary vectors are loaded only for elf formatted executables. 167 ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 168 if(elfObject) 169 { 170 //Bits which describe the system hardware capabilities 171 auxv.push_back(buildAuxVect(SPARC_AT_HWCAP, hwcap)); 172 //The system page size 173 auxv.push_back(buildAuxVect(SPARC_AT_PAGESZ, SparcISA::VMPageSize)); 174 //Defined to be 100 in the kernel source. 175 //Frequency at which times() increments 176 auxv.push_back(buildAuxVect(SPARC_AT_CLKTCK, 100)); 177 // For statically linked executables, this is the virtual address of the 178 // program header tables if they appear in the executable image 179 auxv.push_back(buildAuxVect(SPARC_AT_PHDR, elfObject->programHeaderTable())); 180 // This is the size of a program header entry from the elf file. 181 auxv.push_back(buildAuxVect(SPARC_AT_PHENT, elfObject->programHeaderSize())); 182 // This is the number of program headers from the original elf file. 183 auxv.push_back(buildAuxVect(SPARC_AT_PHNUM, elfObject->programHeaderCount())); 184 //This is the address of the elf "interpreter", It should be set 185 //to 0 for regular executables. It should be something else 186 //(not sure what) for dynamic libraries. 187 auxv.push_back(buildAuxVect(SPARC_AT_BASE, 0)); 188 //This is hardwired to 0 in the elf loading code in the kernel 189 auxv.push_back(buildAuxVect(SPARC_AT_FLAGS, 0)); 190 //The entry point to the program 191 auxv.push_back(buildAuxVect(SPARC_AT_ENTRY, objFile->entryPoint())); 192 //Different user and group IDs 193 auxv.push_back(buildAuxVect(SPARC_AT_UID, uid)); 194 auxv.push_back(buildAuxVect(SPARC_AT_EUID, euid)); 195 auxv.push_back(buildAuxVect(SPARC_AT_GID, gid)); 196 auxv.push_back(buildAuxVect(SPARC_AT_EGID, egid)); 197 //Whether to enable "secure mode" in the executable 198 auxv.push_back(buildAuxVect(SPARC_AT_SECURE, 0)); 199 } 200 201 //Figure out how big the initial stack needs to be 202 203 // The unaccounted for 0 at the top of the stack 204 int mysterious_size = intSize; 205 206 //This is the name of the file which is present on the initial stack 207 //It's purpose is to let the user space linker examine the original file. 208 int file_name_size = filename.size() + 1; 209 210 int env_data_size = 0; 211 for (int i = 0; i < envp.size(); ++i) { 212 env_data_size += envp[i].size() + 1; 213 } 214 int arg_data_size = 0; 215 for (int i = 0; i < argv.size(); ++i) { 216 arg_data_size += argv[i].size() + 1; 217 } 218 219 //The info_block needs to be padded so it's size is a multiple of the 220 //alignment mask. Also, it appears that there needs to be at least some 221 //padding, so if the size is already a multiple, we need to increase it 222 //anyway. 223 int info_block_size = 224 (file_name_size + 225 env_data_size + 226 arg_data_size + 227 intSize) & alignmentMask; 228 229 int info_block_padding = 230 info_block_size - 231 file_name_size - 232 env_data_size - 233 arg_data_size; 234 235 //Each auxilliary vector is two 8 byte words 236 int aux_array_size = intSize * 2 * (auxv.size() + 1); 237 238 int envp_array_size = intSize * (envp.size() + 1); 239 int argv_array_size = intSize * (argv.size() + 1); 240 241 int argc_size = intSize; 242 int window_save_size = intSize * 16; 243 244 int space_needed = 245 mysterious_size + 246 info_block_size + 247 aux_array_size + 248 envp_array_size + 249 argv_array_size + 250 argc_size + 251 window_save_size; 252 253 stack_min = stack_base - space_needed; 254 stack_min &= alignmentMask; 255 stack_size = stack_base - stack_min; 256 257 // map memory 258 pTable->allocate(roundDown(stack_min, pageSize), 259 roundUp(stack_size, pageSize)); 260 261 // map out initial stack contents 262 Addr mysterious_base = stack_base - mysterious_size; 263 Addr file_name_base = mysterious_base - file_name_size; 264 Addr env_data_base = file_name_base - env_data_size; 265 Addr arg_data_base = env_data_base - arg_data_size; 266 Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding; 267 Addr envp_array_base = auxv_array_base - envp_array_size; 268 Addr argv_array_base = envp_array_base - argv_array_size; 269 Addr argc_base = argv_array_base - argc_size; 270#ifndef NDEBUG 271 // only used in DPRINTF 272 Addr window_save_base = argc_base - window_save_size; 273#endif 274 275 DPRINTF(Sparc, "The addresses of items on the initial stack:\n"); 276 DPRINTF(Sparc, "0x%x - file name\n", file_name_base); 277 DPRINTF(Sparc, "0x%x - env data\n", env_data_base); 278 DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base); 279 DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base); 280 DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base); 281 DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base); 282 DPRINTF(Sparc, "0x%x - argc \n", argc_base); 283 DPRINTF(Sparc, "0x%x - window save\n", window_save_base); 284 DPRINTF(Sparc, "0x%x - stack min\n", stack_min); 285 286 // write contents to stack 287 288 // figure out argc 289 uint64_t argc = argv.size(); 290 uint64_t guestArgc = TheISA::htog(argc); 291 292 //Write out the mysterious 0 293 uint64_t mysterious_zero = 0; 294 initVirtMem->writeBlob(mysterious_base, 295 (uint8_t*)&mysterious_zero, mysterious_size); 296 297 //Write the file name 298 initVirtMem->writeString(file_name_base, filename.c_str()); 299 300 //Copy the aux stuff 301 for(int x = 0; x < auxv.size(); x++) 302 { 303 initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 304 (uint8_t*)&(auxv[x].a_type), intSize); 305 initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 306 (uint8_t*)&(auxv[x].a_val), intSize); 307 } 308 //Write out the terminating zeroed auxilliary vector 309 const uint64_t zero = 0; 310 initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 311 (uint8_t*)&zero, 2 * intSize); 312 313 copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 314 copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 315 316 initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 317 318 threadContexts[0]->setIntReg(ArgumentReg0, argc); 319 threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 320 threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias); 321 322 Addr prog_entry = objFile->entryPoint(); 323 threadContexts[0]->setPC(prog_entry); 324 threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 325 threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 326 327// num_processes++; 328} 329