process.cc revision 4648
12207SN/A/* 22207SN/A * Copyright (c) 2003-2004 The Regents of The University of Michigan 32207SN/A * All rights reserved. 42207SN/A * 52207SN/A * Redistribution and use in source and binary forms, with or without 62207SN/A * modification, are permitted provided that the following conditions are 72207SN/A * met: redistributions of source code must retain the above copyright 82207SN/A * notice, this list of conditions and the following disclaimer; 92207SN/A * redistributions in binary form must reproduce the above copyright 102207SN/A * notice, this list of conditions and the following disclaimer in the 112207SN/A * documentation and/or other materials provided with the distribution; 122207SN/A * neither the name of the copyright holders nor the names of its 132207SN/A * contributors may be used to endorse or promote products derived from 142207SN/A * this software without specific prior written permission. 152207SN/A * 162207SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172207SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182207SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192207SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202207SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212207SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222207SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232207SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242207SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252207SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262207SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Gabe Black 292665Ssaidi@eecs.umich.edu * Ali Saidi 302207SN/A */ 312207SN/A 323589Sgblack@eecs.umich.edu#include "arch/sparc/asi.hh" 334111Sgblack@eecs.umich.edu#include "arch/sparc/handlers.hh" 342474SN/A#include "arch/sparc/isa_traits.hh" 352207SN/A#include "arch/sparc/process.hh" 363760Sgblack@eecs.umich.edu#include "arch/sparc/types.hh" 372454SN/A#include "base/loader/object_file.hh" 382976Sgblack@eecs.umich.edu#include "base/loader/elf_object.hh" 392454SN/A#include "base/misc.hh" 402680Sktlim@umich.edu#include "cpu/thread_context.hh" 412561SN/A#include "mem/page_table.hh" 422561SN/A#include "mem/translating_port.hh" 432474SN/A#include "sim/system.hh" 442207SN/A 452458SN/Ausing namespace std; 462474SN/Ausing namespace SparcISA; 472458SN/A 482207SN/A 492474SN/ASparcLiveProcess::SparcLiveProcess(const std::string &nm, ObjectFile *objFile, 502474SN/A System *_system, int stdin_fd, int stdout_fd, int stderr_fd, 513114Sgblack@eecs.umich.edu std::vector<std::string> &argv, std::vector<std::string> &envp, 523669Sbinkertn@umich.edu const std::string &cwd, 533114Sgblack@eecs.umich.edu uint64_t _uid, uint64_t _euid, uint64_t _gid, uint64_t _egid, 543114Sgblack@eecs.umich.edu uint64_t _pid, uint64_t _ppid) 552474SN/A : LiveProcess(nm, objFile, _system, stdin_fd, stdout_fd, stderr_fd, 563669Sbinkertn@umich.edu argv, envp, cwd, _uid, _euid, _gid, _egid, _pid, _ppid) 572474SN/A{ 582474SN/A 592474SN/A // XXX all the below need to be updated for SPARC - Ali 602474SN/A brk_point = objFile->dataBase() + objFile->dataSize() + objFile->bssSize(); 612474SN/A brk_point = roundUp(brk_point, VMPageSize); 622474SN/A 632474SN/A // Set pointer for next thread stack. Reserve 8M for main stack. 642474SN/A next_thread_stack_base = stack_base - (8 * 1024 * 1024); 653415Sgblack@eecs.umich.edu 663415Sgblack@eecs.umich.edu //Initialize these to 0s 673415Sgblack@eecs.umich.edu fillStart = 0; 683415Sgblack@eecs.umich.edu spillStart = 0; 692474SN/A} 702474SN/A 714111Sgblack@eecs.umich.eduvoid SparcLiveProcess::handleTrap(int trapNum, ThreadContext *tc) 724111Sgblack@eecs.umich.edu{ 734111Sgblack@eecs.umich.edu switch(trapNum) 744111Sgblack@eecs.umich.edu { 754111Sgblack@eecs.umich.edu case 0x03: //Flush window trap 764111Sgblack@eecs.umich.edu warn("Ignoring request to flush register windows.\n"); 774111Sgblack@eecs.umich.edu break; 784111Sgblack@eecs.umich.edu default: 794111Sgblack@eecs.umich.edu panic("Unimplemented trap to operating system: trap number %#x.\n", trapNum); 804111Sgblack@eecs.umich.edu } 814111Sgblack@eecs.umich.edu} 824111Sgblack@eecs.umich.edu 832474SN/Avoid 844111Sgblack@eecs.umich.eduSparc32LiveProcess::startup() 854111Sgblack@eecs.umich.edu{ 864111Sgblack@eecs.umich.edu argsInit(32 / 8, VMPageSize); 874111Sgblack@eecs.umich.edu 884111Sgblack@eecs.umich.edu //From the SPARC ABI 894111Sgblack@eecs.umich.edu 904648Sgblack@eecs.umich.edu //The process runs in user mode with 32 bit addresses 914648Sgblack@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x0a); 924111Sgblack@eecs.umich.edu 934111Sgblack@eecs.umich.edu //Setup default FP state 944172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0); 954111Sgblack@eecs.umich.edu 964172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0); 974111Sgblack@eecs.umich.edu // 984111Sgblack@eecs.umich.edu /* 994111Sgblack@eecs.umich.edu * Register window management registers 1004111Sgblack@eecs.umich.edu */ 1014111Sgblack@eecs.umich.edu 1024111Sgblack@eecs.umich.edu //No windows contain info from other programs 1034172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0); 1044111Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0); 1054111Sgblack@eecs.umich.edu //There are no windows to pop 1064172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0); 1074111Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0); 1084111Sgblack@eecs.umich.edu //All windows are available to save into 1094172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2); 1104111Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2); 1114111Sgblack@eecs.umich.edu //All windows are "clean" 1124172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows); 1134111Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows); 1144111Sgblack@eecs.umich.edu //Start with register window 0 1154172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0); 1164111Sgblack@eecs.umich.edu //Always use spill and fill traps 0 1174172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0); 1184111Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0); 1194111Sgblack@eecs.umich.edu //Set the trap level to 0 1204172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0); 1214111Sgblack@eecs.umich.edu //Set the ASI register to something fixed 1224172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY); 1234111Sgblack@eecs.umich.edu} 1244111Sgblack@eecs.umich.edu 1254111Sgblack@eecs.umich.eduvoid 1264111Sgblack@eecs.umich.eduSparc64LiveProcess::startup() 1272474SN/A{ 1283760Sgblack@eecs.umich.edu argsInit(sizeof(IntReg), VMPageSize); 1292561SN/A 1302561SN/A //From the SPARC ABI 1312561SN/A 1322561SN/A //The process runs in user mode 1334172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscReg(MISCREG_PSTATE, 0x02); 1342561SN/A 1352646Ssaidi@eecs.umich.edu //Setup default FP state 1364172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_FSR, 0); 1372646Ssaidi@eecs.umich.edu 1384172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_TICK, 0); 1392646Ssaidi@eecs.umich.edu // 1402561SN/A /* 1412561SN/A * Register window management registers 1422561SN/A */ 1432561SN/A 1442561SN/A //No windows contain info from other programs 1454172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_OTHERWIN, 0); 1463761Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 6, 0); 1472561SN/A //There are no windows to pop 1484172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANRESTORE, 0); 1493761Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 4, 0); 1502561SN/A //All windows are available to save into 1514172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_CANSAVE, NWindows - 2); 1523761Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 3, NWindows - 2); 1532561SN/A //All windows are "clean" 1544172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_CLEANWIN, NWindows); 1553761Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 5, NWindows); 1562561SN/A //Start with register window 0 1574172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_CWP, 0); 1583415Sgblack@eecs.umich.edu //Always use spill and fill traps 0 1594172Ssaidi@eecs.umich.edu //threadContexts[0]->setMiscRegNoEffect(MISCREG_WSTATE, 0); 1603761Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(NumIntArchRegs + 7, 0); 1613415Sgblack@eecs.umich.edu //Set the trap level to 0 1624172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_TL, 0); 1633589Sgblack@eecs.umich.edu //Set the ASI register to something fixed 1644172Ssaidi@eecs.umich.edu threadContexts[0]->setMiscRegNoEffect(MISCREG_ASI, ASI_PRIMARY); 1652474SN/A} 1662474SN/A 1674111Sgblack@eecs.umich.eduM5_32_auxv_t::M5_32_auxv_t(int32_t type, int32_t val) 1682585SN/A{ 1694111Sgblack@eecs.umich.edu a_type = TheISA::htog(type); 1704111Sgblack@eecs.umich.edu a_val = TheISA::htog(val); 1712585SN/A} 1722585SN/A 1734111Sgblack@eecs.umich.eduM5_64_auxv_t::M5_64_auxv_t(int64_t type, int64_t val) 1743415Sgblack@eecs.umich.edu{ 1754111Sgblack@eecs.umich.edu a_type = TheISA::htog(type); 1764111Sgblack@eecs.umich.edu a_val = TheISA::htog(val); 1774111Sgblack@eecs.umich.edu} 1783415Sgblack@eecs.umich.edu 1792561SN/Avoid 1804111Sgblack@eecs.umich.eduSparc64LiveProcess::argsInit(int intSize, int pageSize) 1812561SN/A{ 1824111Sgblack@eecs.umich.edu typedef M5_64_auxv_t auxv_t; 1832561SN/A Process::startup(); 1842474SN/A 1853044Sgblack@eecs.umich.edu string filename; 1863044Sgblack@eecs.umich.edu if(argv.size() < 1) 1873044Sgblack@eecs.umich.edu filename = ""; 1883044Sgblack@eecs.umich.edu else 1893044Sgblack@eecs.umich.edu filename = argv[0]; 1903044Sgblack@eecs.umich.edu 1912561SN/A Addr alignmentMask = ~(intSize - 1); 1922561SN/A 1932561SN/A // load object file into target memory 1942561SN/A objFile->loadSections(initVirtMem); 1952561SN/A 1962585SN/A //These are the auxilliary vector types 1972585SN/A enum auxTypes 1982585SN/A { 1992585SN/A SPARC_AT_HWCAP = 16, 2002585SN/A SPARC_AT_PAGESZ = 6, 2012585SN/A SPARC_AT_CLKTCK = 17, 2022585SN/A SPARC_AT_PHDR = 3, 2032585SN/A SPARC_AT_PHENT = 4, 2042585SN/A SPARC_AT_PHNUM = 5, 2052585SN/A SPARC_AT_BASE = 7, 2062585SN/A SPARC_AT_FLAGS = 8, 2072585SN/A SPARC_AT_ENTRY = 9, 2082585SN/A SPARC_AT_UID = 11, 2092585SN/A SPARC_AT_EUID = 12, 2102585SN/A SPARC_AT_GID = 13, 2112976Sgblack@eecs.umich.edu SPARC_AT_EGID = 14, 2122976Sgblack@eecs.umich.edu SPARC_AT_SECURE = 23 2132585SN/A }; 2142585SN/A 2152585SN/A enum hardwareCaps 2162585SN/A { 2172585SN/A M5_HWCAP_SPARC_FLUSH = 1, 2182585SN/A M5_HWCAP_SPARC_STBAR = 2, 2192585SN/A M5_HWCAP_SPARC_SWAP = 4, 2202585SN/A M5_HWCAP_SPARC_MULDIV = 8, 2212585SN/A M5_HWCAP_SPARC_V9 = 16, 2222585SN/A //This one should technically only be set 2232585SN/A //if there is a cheetah or cheetah_plus tlb, 2242585SN/A //but we'll use it all the time 2252585SN/A M5_HWCAP_SPARC_ULTRA3 = 32 2262585SN/A }; 2272585SN/A 2282585SN/A const int64_t hwcap = 2292585SN/A M5_HWCAP_SPARC_FLUSH | 2302585SN/A M5_HWCAP_SPARC_STBAR | 2312585SN/A M5_HWCAP_SPARC_SWAP | 2322585SN/A M5_HWCAP_SPARC_MULDIV | 2332585SN/A M5_HWCAP_SPARC_V9 | 2342585SN/A M5_HWCAP_SPARC_ULTRA3; 2352585SN/A 2362976Sgblack@eecs.umich.edu 2372976Sgblack@eecs.umich.edu //Setup the auxilliary vectors. These will already have endian conversion. 2382976Sgblack@eecs.umich.edu //Auxilliary vectors are loaded only for elf formatted executables. 2392976Sgblack@eecs.umich.edu ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 2402976Sgblack@eecs.umich.edu if(elfObject) 2412976Sgblack@eecs.umich.edu { 2422976Sgblack@eecs.umich.edu //Bits which describe the system hardware capabilities 2434111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap)); 2442976Sgblack@eecs.umich.edu //The system page size 2454111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize)); 2462976Sgblack@eecs.umich.edu //Defined to be 100 in the kernel source. 2472976Sgblack@eecs.umich.edu //Frequency at which times() increments 2484111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100)); 2492976Sgblack@eecs.umich.edu // For statically linked executables, this is the virtual address of the 2502976Sgblack@eecs.umich.edu // program header tables if they appear in the executable image 2514111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable())); 2522976Sgblack@eecs.umich.edu // This is the size of a program header entry from the elf file. 2534111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize())); 2542976Sgblack@eecs.umich.edu // This is the number of program headers from the original elf file. 2554111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount())); 2562976Sgblack@eecs.umich.edu //This is the address of the elf "interpreter", It should be set 2572976Sgblack@eecs.umich.edu //to 0 for regular executables. It should be something else 2582976Sgblack@eecs.umich.edu //(not sure what) for dynamic libraries. 2594111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_BASE, 0)); 2602976Sgblack@eecs.umich.edu //This is hardwired to 0 in the elf loading code in the kernel 2614111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0)); 2622976Sgblack@eecs.umich.edu //The entry point to the program 2634111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint())); 2642976Sgblack@eecs.umich.edu //Different user and group IDs 2654111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_UID, uid())); 2664111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_EUID, euid())); 2674111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_GID, gid())); 2684111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_EGID, egid())); 2692976Sgblack@eecs.umich.edu //Whether to enable "secure mode" in the executable 2704111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_SECURE, 0)); 2712976Sgblack@eecs.umich.edu } 2722585SN/A 2732561SN/A //Figure out how big the initial stack needs to be 2742561SN/A 2753044Sgblack@eecs.umich.edu // The unaccounted for 0 at the top of the stack 2763044Sgblack@eecs.umich.edu int mysterious_size = intSize; 2773044Sgblack@eecs.umich.edu 2783044Sgblack@eecs.umich.edu //This is the name of the file which is present on the initial stack 2793044Sgblack@eecs.umich.edu //It's purpose is to let the user space linker examine the original file. 2803044Sgblack@eecs.umich.edu int file_name_size = filename.size() + 1; 2813044Sgblack@eecs.umich.edu 2822561SN/A int env_data_size = 0; 2832561SN/A for (int i = 0; i < envp.size(); ++i) { 2842561SN/A env_data_size += envp[i].size() + 1; 2852561SN/A } 2862561SN/A int arg_data_size = 0; 2872561SN/A for (int i = 0; i < argv.size(); ++i) { 2882561SN/A arg_data_size += argv[i].size() + 1; 2892561SN/A } 2902561SN/A 2913044Sgblack@eecs.umich.edu //The info_block needs to be padded so it's size is a multiple of the 2923044Sgblack@eecs.umich.edu //alignment mask. Also, it appears that there needs to be at least some 2933044Sgblack@eecs.umich.edu //padding, so if the size is already a multiple, we need to increase it 2943044Sgblack@eecs.umich.edu //anyway. 2953044Sgblack@eecs.umich.edu int info_block_size = 2963044Sgblack@eecs.umich.edu (file_name_size + 2973044Sgblack@eecs.umich.edu env_data_size + 2983044Sgblack@eecs.umich.edu arg_data_size + 2993044Sgblack@eecs.umich.edu intSize) & alignmentMask; 3003044Sgblack@eecs.umich.edu 3013044Sgblack@eecs.umich.edu int info_block_padding = 3023044Sgblack@eecs.umich.edu info_block_size - 3033044Sgblack@eecs.umich.edu file_name_size - 3043044Sgblack@eecs.umich.edu env_data_size - 3053044Sgblack@eecs.umich.edu arg_data_size; 3063044Sgblack@eecs.umich.edu 3073044Sgblack@eecs.umich.edu //Each auxilliary vector is two 8 byte words 3082561SN/A int aux_array_size = intSize * 2 * (auxv.size() + 1); 3092561SN/A 3103044Sgblack@eecs.umich.edu int envp_array_size = intSize * (envp.size() + 1); 3112561SN/A int argv_array_size = intSize * (argv.size() + 1); 3122561SN/A 3132561SN/A int argc_size = intSize; 3142561SN/A int window_save_size = intSize * 16; 3152561SN/A 3162561SN/A int space_needed = 3173044Sgblack@eecs.umich.edu mysterious_size + 3182561SN/A info_block_size + 3192561SN/A aux_array_size + 3202561SN/A envp_array_size + 3212561SN/A argv_array_size + 3222561SN/A argc_size + 3232561SN/A window_save_size; 3242561SN/A 3252561SN/A stack_min = stack_base - space_needed; 3262561SN/A stack_min &= alignmentMask; 3272561SN/A stack_size = stack_base - stack_min; 3282561SN/A 3292561SN/A // map memory 3302561SN/A pTable->allocate(roundDown(stack_min, pageSize), 3312561SN/A roundUp(stack_size, pageSize)); 3322561SN/A 3332561SN/A // map out initial stack contents 3343044Sgblack@eecs.umich.edu Addr mysterious_base = stack_base - mysterious_size; 3353044Sgblack@eecs.umich.edu Addr file_name_base = mysterious_base - file_name_size; 3363044Sgblack@eecs.umich.edu Addr env_data_base = file_name_base - env_data_size; 3372561SN/A Addr arg_data_base = env_data_base - arg_data_size; 3383044Sgblack@eecs.umich.edu Addr auxv_array_base = arg_data_base - aux_array_size - info_block_padding; 3392585SN/A Addr envp_array_base = auxv_array_base - envp_array_size; 3402561SN/A Addr argv_array_base = envp_array_base - argv_array_size; 3412561SN/A Addr argc_base = argv_array_base - argc_size; 3423039Sstever@eecs.umich.edu#ifndef NDEBUG 3433039Sstever@eecs.umich.edu // only used in DPRINTF 3442561SN/A Addr window_save_base = argc_base - window_save_size; 3453039Sstever@eecs.umich.edu#endif 3462561SN/A 3472561SN/A DPRINTF(Sparc, "The addresses of items on the initial stack:\n"); 3483044Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - file name\n", file_name_base); 3492561SN/A DPRINTF(Sparc, "0x%x - env data\n", env_data_base); 3502561SN/A DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base); 3512585SN/A DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base); 3522585SN/A DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base); 3532585SN/A DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base); 3542561SN/A DPRINTF(Sparc, "0x%x - argc \n", argc_base); 3552561SN/A DPRINTF(Sparc, "0x%x - window save\n", window_save_base); 3562561SN/A DPRINTF(Sparc, "0x%x - stack min\n", stack_min); 3572561SN/A 3582561SN/A // write contents to stack 3593044Sgblack@eecs.umich.edu 3603044Sgblack@eecs.umich.edu // figure out argc 3612561SN/A uint64_t argc = argv.size(); 3622585SN/A uint64_t guestArgc = TheISA::htog(argc); 3632561SN/A 3643044Sgblack@eecs.umich.edu //Write out the mysterious 0 3653044Sgblack@eecs.umich.edu uint64_t mysterious_zero = 0; 3663044Sgblack@eecs.umich.edu initVirtMem->writeBlob(mysterious_base, 3673044Sgblack@eecs.umich.edu (uint8_t*)&mysterious_zero, mysterious_size); 3683044Sgblack@eecs.umich.edu 3693044Sgblack@eecs.umich.edu //Write the file name 3703044Sgblack@eecs.umich.edu initVirtMem->writeString(file_name_base, filename.c_str()); 3713044Sgblack@eecs.umich.edu 3722585SN/A //Copy the aux stuff 3732585SN/A for(int x = 0; x < auxv.size(); x++) 3742585SN/A { 3752585SN/A initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 3762585SN/A (uint8_t*)&(auxv[x].a_type), intSize); 3772585SN/A initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 3782585SN/A (uint8_t*)&(auxv[x].a_val), intSize); 3792585SN/A } 3802585SN/A //Write out the terminating zeroed auxilliary vector 3812561SN/A const uint64_t zero = 0; 3822585SN/A initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 3832585SN/A (uint8_t*)&zero, 2 * intSize); 3842561SN/A 3852561SN/A copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 3862561SN/A copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 3872561SN/A 3882585SN/A initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 3892561SN/A 3903415Sgblack@eecs.umich.edu //Stuff the trap handlers into the processes address space. 3913415Sgblack@eecs.umich.edu //Since the stack grows down and is the highest area in the processes 3923415Sgblack@eecs.umich.edu //address space, we can put stuff above it and stay out of the way. 3933415Sgblack@eecs.umich.edu int fillSize = sizeof(MachInst) * numFillInsts; 3943415Sgblack@eecs.umich.edu int spillSize = sizeof(MachInst) * numSpillInsts; 3953415Sgblack@eecs.umich.edu fillStart = stack_base; 3963415Sgblack@eecs.umich.edu spillStart = fillStart + fillSize; 3974111Sgblack@eecs.umich.edu initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler64, fillSize); 3984111Sgblack@eecs.umich.edu initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler64, spillSize); 3993415Sgblack@eecs.umich.edu 4003415Sgblack@eecs.umich.edu //Set up the thread context to start running the process 4012680Sktlim@umich.edu threadContexts[0]->setIntReg(ArgumentReg0, argc); 4022680Sktlim@umich.edu threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 4032680Sktlim@umich.edu threadContexts[0]->setIntReg(StackPointerReg, stack_min - StackBias); 4042561SN/A 4052561SN/A Addr prog_entry = objFile->entryPoint(); 4062680Sktlim@umich.edu threadContexts[0]->setPC(prog_entry); 4072680Sktlim@umich.edu threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 4082680Sktlim@umich.edu threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 4092561SN/A 4103959Sgblack@eecs.umich.edu //Align the "stack_min" to a page boundary. 4113959Sgblack@eecs.umich.edu stack_min = roundDown(stack_min, pageSize); 4123959Sgblack@eecs.umich.edu 4132561SN/A// num_processes++; 4142561SN/A} 4154111Sgblack@eecs.umich.edu 4164111Sgblack@eecs.umich.eduvoid 4174111Sgblack@eecs.umich.eduSparc32LiveProcess::argsInit(int intSize, int pageSize) 4184111Sgblack@eecs.umich.edu{ 4194111Sgblack@eecs.umich.edu typedef M5_32_auxv_t auxv_t; 4204111Sgblack@eecs.umich.edu Process::startup(); 4214111Sgblack@eecs.umich.edu 4224111Sgblack@eecs.umich.edu string filename; 4234111Sgblack@eecs.umich.edu if(argv.size() < 1) 4244111Sgblack@eecs.umich.edu filename = ""; 4254111Sgblack@eecs.umich.edu else 4264111Sgblack@eecs.umich.edu filename = argv[0]; 4274111Sgblack@eecs.umich.edu 4284117Sgblack@eecs.umich.edu //Even though this is a 32 bit process, the ABI says we still need to 4294117Sgblack@eecs.umich.edu //maintain double word alignment of the stack pointer. 4304117Sgblack@eecs.umich.edu Addr alignmentMask = ~(8 - 1); 4314111Sgblack@eecs.umich.edu 4324111Sgblack@eecs.umich.edu // load object file into target memory 4334111Sgblack@eecs.umich.edu objFile->loadSections(initVirtMem); 4344111Sgblack@eecs.umich.edu 4354111Sgblack@eecs.umich.edu //These are the auxilliary vector types 4364111Sgblack@eecs.umich.edu enum auxTypes 4374111Sgblack@eecs.umich.edu { 4384111Sgblack@eecs.umich.edu SPARC_AT_HWCAP = 16, 4394111Sgblack@eecs.umich.edu SPARC_AT_PAGESZ = 6, 4404111Sgblack@eecs.umich.edu SPARC_AT_CLKTCK = 17, 4414111Sgblack@eecs.umich.edu SPARC_AT_PHDR = 3, 4424111Sgblack@eecs.umich.edu SPARC_AT_PHENT = 4, 4434111Sgblack@eecs.umich.edu SPARC_AT_PHNUM = 5, 4444111Sgblack@eecs.umich.edu SPARC_AT_BASE = 7, 4454111Sgblack@eecs.umich.edu SPARC_AT_FLAGS = 8, 4464111Sgblack@eecs.umich.edu SPARC_AT_ENTRY = 9, 4474111Sgblack@eecs.umich.edu SPARC_AT_UID = 11, 4484111Sgblack@eecs.umich.edu SPARC_AT_EUID = 12, 4494111Sgblack@eecs.umich.edu SPARC_AT_GID = 13, 4504111Sgblack@eecs.umich.edu SPARC_AT_EGID = 14, 4514111Sgblack@eecs.umich.edu SPARC_AT_SECURE = 23 4524111Sgblack@eecs.umich.edu }; 4534111Sgblack@eecs.umich.edu 4544111Sgblack@eecs.umich.edu enum hardwareCaps 4554111Sgblack@eecs.umich.edu { 4564111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_FLUSH = 1, 4574111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_STBAR = 2, 4584111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_SWAP = 4, 4594111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_MULDIV = 8, 4604111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_V9 = 16, 4614111Sgblack@eecs.umich.edu //This one should technically only be set 4624111Sgblack@eecs.umich.edu //if there is a cheetah or cheetah_plus tlb, 4634111Sgblack@eecs.umich.edu //but we'll use it all the time 4644111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_ULTRA3 = 32 4654111Sgblack@eecs.umich.edu }; 4664111Sgblack@eecs.umich.edu 4674111Sgblack@eecs.umich.edu const int64_t hwcap = 4684111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_FLUSH | 4694111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_STBAR | 4704111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_SWAP | 4714111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_MULDIV | 4724111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_V9 | 4734111Sgblack@eecs.umich.edu M5_HWCAP_SPARC_ULTRA3; 4744111Sgblack@eecs.umich.edu 4754111Sgblack@eecs.umich.edu 4764111Sgblack@eecs.umich.edu //Setup the auxilliary vectors. These will already have endian conversion. 4774111Sgblack@eecs.umich.edu //Auxilliary vectors are loaded only for elf formatted executables. 4784111Sgblack@eecs.umich.edu ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 4794111Sgblack@eecs.umich.edu if(elfObject) 4804111Sgblack@eecs.umich.edu { 4814111Sgblack@eecs.umich.edu //Bits which describe the system hardware capabilities 4824111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_HWCAP, hwcap)); 4834111Sgblack@eecs.umich.edu //The system page size 4844111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PAGESZ, SparcISA::VMPageSize)); 4854111Sgblack@eecs.umich.edu //Defined to be 100 in the kernel source. 4864111Sgblack@eecs.umich.edu //Frequency at which times() increments 4874111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_CLKTCK, 100)); 4884111Sgblack@eecs.umich.edu // For statically linked executables, this is the virtual address of the 4894111Sgblack@eecs.umich.edu // program header tables if they appear in the executable image 4904111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PHDR, elfObject->programHeaderTable())); 4914111Sgblack@eecs.umich.edu // This is the size of a program header entry from the elf file. 4924111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PHENT, elfObject->programHeaderSize())); 4934111Sgblack@eecs.umich.edu // This is the number of program headers from the original elf file. 4944111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_PHNUM, elfObject->programHeaderCount())); 4954111Sgblack@eecs.umich.edu //This is the address of the elf "interpreter", It should be set 4964111Sgblack@eecs.umich.edu //to 0 for regular executables. It should be something else 4974111Sgblack@eecs.umich.edu //(not sure what) for dynamic libraries. 4984111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_BASE, 0)); 4994111Sgblack@eecs.umich.edu //This is hardwired to 0 in the elf loading code in the kernel 5004111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_FLAGS, 0)); 5014111Sgblack@eecs.umich.edu //The entry point to the program 5024111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_ENTRY, objFile->entryPoint())); 5034111Sgblack@eecs.umich.edu //Different user and group IDs 5044111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_UID, uid())); 5054111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_EUID, euid())); 5064111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_GID, gid())); 5074111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_EGID, egid())); 5084111Sgblack@eecs.umich.edu //Whether to enable "secure mode" in the executable 5094111Sgblack@eecs.umich.edu auxv.push_back(auxv_t(SPARC_AT_SECURE, 0)); 5104111Sgblack@eecs.umich.edu } 5114111Sgblack@eecs.umich.edu 5124111Sgblack@eecs.umich.edu //Figure out how big the initial stack needs to be 5134111Sgblack@eecs.umich.edu 5144164Sgblack@eecs.umich.edu // The unaccounted for 8 byte 0 at the top of the stack 5154164Sgblack@eecs.umich.edu int mysterious_size = 8; 5164111Sgblack@eecs.umich.edu 5174111Sgblack@eecs.umich.edu //This is the name of the file which is present on the initial stack 5184111Sgblack@eecs.umich.edu //It's purpose is to let the user space linker examine the original file. 5194111Sgblack@eecs.umich.edu int file_name_size = filename.size() + 1; 5204111Sgblack@eecs.umich.edu 5214111Sgblack@eecs.umich.edu int env_data_size = 0; 5224111Sgblack@eecs.umich.edu for (int i = 0; i < envp.size(); ++i) { 5234111Sgblack@eecs.umich.edu env_data_size += envp[i].size() + 1; 5244111Sgblack@eecs.umich.edu } 5254111Sgblack@eecs.umich.edu int arg_data_size = 0; 5264111Sgblack@eecs.umich.edu for (int i = 0; i < argv.size(); ++i) { 5274111Sgblack@eecs.umich.edu arg_data_size += argv[i].size() + 1; 5284111Sgblack@eecs.umich.edu } 5294111Sgblack@eecs.umich.edu 5304164Sgblack@eecs.umich.edu //The info_block - This seems to need an pad for some reason. 5314111Sgblack@eecs.umich.edu int info_block_size = 5324164Sgblack@eecs.umich.edu (mysterious_size + 5334164Sgblack@eecs.umich.edu file_name_size + 5344111Sgblack@eecs.umich.edu env_data_size + 5354164Sgblack@eecs.umich.edu arg_data_size + intSize); 5364111Sgblack@eecs.umich.edu 5374164Sgblack@eecs.umich.edu //Each auxilliary vector is two 4 byte words 5384111Sgblack@eecs.umich.edu int aux_array_size = intSize * 2 * (auxv.size() + 1); 5394111Sgblack@eecs.umich.edu 5404111Sgblack@eecs.umich.edu int envp_array_size = intSize * (envp.size() + 1); 5414111Sgblack@eecs.umich.edu int argv_array_size = intSize * (argv.size() + 1); 5424111Sgblack@eecs.umich.edu 5434111Sgblack@eecs.umich.edu int argc_size = intSize; 5444111Sgblack@eecs.umich.edu int window_save_size = intSize * 16; 5454111Sgblack@eecs.umich.edu 5464111Sgblack@eecs.umich.edu int space_needed = 5474164Sgblack@eecs.umich.edu info_block_size + 5484111Sgblack@eecs.umich.edu aux_array_size + 5494111Sgblack@eecs.umich.edu envp_array_size + 5504111Sgblack@eecs.umich.edu argv_array_size + 5514111Sgblack@eecs.umich.edu argc_size + 5524111Sgblack@eecs.umich.edu window_save_size; 5534111Sgblack@eecs.umich.edu 5544111Sgblack@eecs.umich.edu stack_min = stack_base - space_needed; 5554111Sgblack@eecs.umich.edu stack_min &= alignmentMask; 5564111Sgblack@eecs.umich.edu stack_size = stack_base - stack_min; 5574111Sgblack@eecs.umich.edu 5584111Sgblack@eecs.umich.edu // map memory 5594111Sgblack@eecs.umich.edu pTable->allocate(roundDown(stack_min, pageSize), 5604111Sgblack@eecs.umich.edu roundUp(stack_size, pageSize)); 5614111Sgblack@eecs.umich.edu 5624111Sgblack@eecs.umich.edu // map out initial stack contents 5634117Sgblack@eecs.umich.edu uint32_t window_save_base = stack_min; 5644117Sgblack@eecs.umich.edu uint32_t argc_base = window_save_base + window_save_size; 5654117Sgblack@eecs.umich.edu uint32_t argv_array_base = argc_base + argc_size; 5664117Sgblack@eecs.umich.edu uint32_t envp_array_base = argv_array_base + argv_array_size; 5674117Sgblack@eecs.umich.edu uint32_t auxv_array_base = envp_array_base + envp_array_size; 5684117Sgblack@eecs.umich.edu //The info block is pushed up against the top of the stack, while 5694117Sgblack@eecs.umich.edu //the rest of the initial stack frame is aligned to an 8 byte boudary. 5704164Sgblack@eecs.umich.edu uint32_t arg_data_base = stack_base - info_block_size + intSize; 5714117Sgblack@eecs.umich.edu uint32_t env_data_base = arg_data_base + arg_data_size; 5724117Sgblack@eecs.umich.edu uint32_t file_name_base = env_data_base + env_data_size; 5734117Sgblack@eecs.umich.edu uint32_t mysterious_base = file_name_base + file_name_size; 5744111Sgblack@eecs.umich.edu 5754111Sgblack@eecs.umich.edu DPRINTF(Sparc, "The addresses of items on the initial stack:\n"); 5764111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - file name\n", file_name_base); 5774111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - env data\n", env_data_base); 5784111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - arg data\n", arg_data_base); 5794111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - auxv array\n", auxv_array_base); 5804111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - envp array\n", envp_array_base); 5814111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - argv array\n", argv_array_base); 5824111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - argc \n", argc_base); 5834111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - window save\n", window_save_base); 5844111Sgblack@eecs.umich.edu DPRINTF(Sparc, "0x%x - stack min\n", stack_min); 5854111Sgblack@eecs.umich.edu 5864111Sgblack@eecs.umich.edu // write contents to stack 5874111Sgblack@eecs.umich.edu 5884111Sgblack@eecs.umich.edu // figure out argc 5894111Sgblack@eecs.umich.edu uint32_t argc = argv.size(); 5904111Sgblack@eecs.umich.edu uint32_t guestArgc = TheISA::htog(argc); 5914111Sgblack@eecs.umich.edu 5924111Sgblack@eecs.umich.edu //Write out the mysterious 0 5934111Sgblack@eecs.umich.edu uint64_t mysterious_zero = 0; 5944111Sgblack@eecs.umich.edu initVirtMem->writeBlob(mysterious_base, 5954111Sgblack@eecs.umich.edu (uint8_t*)&mysterious_zero, mysterious_size); 5964111Sgblack@eecs.umich.edu 5974111Sgblack@eecs.umich.edu //Write the file name 5984111Sgblack@eecs.umich.edu initVirtMem->writeString(file_name_base, filename.c_str()); 5994111Sgblack@eecs.umich.edu 6004111Sgblack@eecs.umich.edu //Copy the aux stuff 6014111Sgblack@eecs.umich.edu for(int x = 0; x < auxv.size(); x++) 6024111Sgblack@eecs.umich.edu { 6034111Sgblack@eecs.umich.edu initVirtMem->writeBlob(auxv_array_base + x * 2 * intSize, 6044111Sgblack@eecs.umich.edu (uint8_t*)&(auxv[x].a_type), intSize); 6054111Sgblack@eecs.umich.edu initVirtMem->writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 6064111Sgblack@eecs.umich.edu (uint8_t*)&(auxv[x].a_val), intSize); 6074111Sgblack@eecs.umich.edu } 6084111Sgblack@eecs.umich.edu //Write out the terminating zeroed auxilliary vector 6094111Sgblack@eecs.umich.edu const uint64_t zero = 0; 6104111Sgblack@eecs.umich.edu initVirtMem->writeBlob(auxv_array_base + 2 * intSize * auxv.size(), 6114111Sgblack@eecs.umich.edu (uint8_t*)&zero, 2 * intSize); 6124111Sgblack@eecs.umich.edu 6134117Sgblack@eecs.umich.edu copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 6144117Sgblack@eecs.umich.edu copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 6154111Sgblack@eecs.umich.edu 6164111Sgblack@eecs.umich.edu initVirtMem->writeBlob(argc_base, (uint8_t*)&guestArgc, intSize); 6174111Sgblack@eecs.umich.edu 6184111Sgblack@eecs.umich.edu //Stuff the trap handlers into the processes address space. 6194111Sgblack@eecs.umich.edu //Since the stack grows down and is the highest area in the processes 6204111Sgblack@eecs.umich.edu //address space, we can put stuff above it and stay out of the way. 6214111Sgblack@eecs.umich.edu int fillSize = sizeof(MachInst) * numFillInsts; 6224111Sgblack@eecs.umich.edu int spillSize = sizeof(MachInst) * numSpillInsts; 6234111Sgblack@eecs.umich.edu fillStart = stack_base; 6244111Sgblack@eecs.umich.edu spillStart = fillStart + fillSize; 6254111Sgblack@eecs.umich.edu initVirtMem->writeBlob(fillStart, (uint8_t*)fillHandler32, fillSize); 6264111Sgblack@eecs.umich.edu initVirtMem->writeBlob(spillStart, (uint8_t*)spillHandler32, spillSize); 6274111Sgblack@eecs.umich.edu 6284111Sgblack@eecs.umich.edu //Set up the thread context to start running the process 6294164Sgblack@eecs.umich.edu //threadContexts[0]->setIntReg(ArgumentReg0, argc); 6304164Sgblack@eecs.umich.edu //threadContexts[0]->setIntReg(ArgumentReg1, argv_array_base); 6314111Sgblack@eecs.umich.edu threadContexts[0]->setIntReg(StackPointerReg, stack_min); 6324111Sgblack@eecs.umich.edu 6334117Sgblack@eecs.umich.edu uint32_t prog_entry = objFile->entryPoint(); 6344111Sgblack@eecs.umich.edu threadContexts[0]->setPC(prog_entry); 6354111Sgblack@eecs.umich.edu threadContexts[0]->setNextPC(prog_entry + sizeof(MachInst)); 6364111Sgblack@eecs.umich.edu threadContexts[0]->setNextNPC(prog_entry + (2 * sizeof(MachInst))); 6374111Sgblack@eecs.umich.edu 6384111Sgblack@eecs.umich.edu //Align the "stack_min" to a page boundary. 6394111Sgblack@eecs.umich.edu stack_min = roundDown(stack_min, pageSize); 6404111Sgblack@eecs.umich.edu 6414111Sgblack@eecs.umich.edu// num_processes++; 6424111Sgblack@eecs.umich.edu} 643