process.cc revision 11886
12207SN/A/* 25254Sksewell@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan 35254Sksewell@umich.edu * All rights reserved. 42207SN/A * 55254Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without 65254Sksewell@umich.edu * modification, are permitted provided that the following conditions are 75254Sksewell@umich.edu * met: redistributions of source code must retain the above copyright 85254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer; 95254Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright 105254Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the 115254Sksewell@umich.edu * documentation and/or other materials provided with the distribution; 125254Sksewell@umich.edu * neither the name of the copyright holders nor the names of its 135254Sksewell@umich.edu * contributors may be used to endorse or promote products derived from 145254Sksewell@umich.edu * this software without specific prior written permission. 152207SN/A * 165254Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 175254Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 185254Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 195254Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 205254Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 215254Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225254Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 235254Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245254Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255254Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265254Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 285254Sksewell@umich.edu * Authors: Gabe Black 295254Sksewell@umich.edu * Ali Saidi 305254Sksewell@umich.edu * Korey Sewell 312207SN/A */ 322207SN/A 3311793Sbrandon.potter@amd.com#include "arch/mips/process.hh" 3411793Sbrandon.potter@amd.com 352474SN/A#include "arch/mips/isa_traits.hh" 368229Snate@binkert.org#include "base/loader/elf_object.hh" 372454SN/A#include "base/loader/object_file.hh" 382454SN/A#include "base/misc.hh" 392680Sktlim@umich.edu#include "cpu/thread_context.hh" 408232Snate@binkert.org#include "debug/Loader.hh" 416650Sksewell@umich.edu#include "mem/page_table.hh" 4211854Sbrandon.potter@amd.com#include "sim/aux_vector.hh" 436650Sksewell@umich.edu#include "sim/process.hh" 446650Sksewell@umich.edu#include "sim/process_impl.hh" 4511800Sbrandon.potter@amd.com#include "sim/syscall_return.hh" 462474SN/A#include "sim/system.hh" 472207SN/A 482447SN/Ausing namespace std; 492474SN/Ausing namespace MipsISA; 502447SN/A 5111851Sbrandon.potter@amd.comMipsProcess::MipsProcess(ProcessParams * params, ObjectFile *objFile) 5211851Sbrandon.potter@amd.com : Process(params, objFile) 532474SN/A{ 542686Sksewell@umich.edu // Set up stack. On MIPS, stack starts at the top of kuseg 552686Sksewell@umich.edu // user address space. MIPS stack grows down from here 5611886Sbrandon.potter@amd.com memState->stackBase = 0x7FFFFFFF; 572474SN/A 582474SN/A // Set pointer for next thread stack. Reserve 8M for main stack. 5911886Sbrandon.potter@amd.com memState->nextThreadStackBase = memState->stackBase - (8 * 1024 * 1024); 602474SN/A 612686Sksewell@umich.edu // Set up break point (Top of Heap) 6211886Sbrandon.potter@amd.com memState->brkPoint = objFile->dataBase() + objFile->dataSize() + 6311886Sbrandon.potter@amd.com objFile->bssSize(); 6411886Sbrandon.potter@amd.com memState->brkPoint = roundUp(memState->brkPoint, PageBytes); 652686Sksewell@umich.edu 666811SMatt DeVuyst // Set up region for mmaps. Start it 1GB above the top of the heap. 6711886Sbrandon.potter@amd.com memState->mmapEnd = memState->brkPoint + 0x40000000L; 682474SN/A} 692474SN/A 702474SN/Avoid 7111851Sbrandon.potter@amd.comMipsProcess::initState() 722474SN/A{ 7311851Sbrandon.potter@amd.com Process::initState(); 746650Sksewell@umich.edu 7510318Sandreas.hansson@arm.com argsInit<uint32_t>(PageBytes); 762474SN/A} 775958Sgblack@eecs.umich.edu 786811SMatt DeVuysttemplate<class IntType> 796650Sksewell@umich.eduvoid 8011851Sbrandon.potter@amd.comMipsProcess::argsInit(int pageSize) 816650Sksewell@umich.edu{ 826811SMatt DeVuyst int intSize = sizeof(IntType); 836811SMatt DeVuyst 8411389Sbrandon.potter@amd.com // Patch the ld_bias for dynamic executables. 8511389Sbrandon.potter@amd.com updateBias(); 8611389Sbrandon.potter@amd.com 876650Sksewell@umich.edu // load object file into target memory 886650Sksewell@umich.edu objFile->loadSections(initVirtMem); 896650Sksewell@umich.edu 906811SMatt DeVuyst typedef AuxVector<IntType> auxv_t; 916811SMatt DeVuyst std::vector<auxv_t> auxv; 926811SMatt DeVuyst 936811SMatt DeVuyst ElfObject * elfObject = dynamic_cast<ElfObject *>(objFile); 946811SMatt DeVuyst if (elfObject) 956811SMatt DeVuyst { 966811SMatt DeVuyst // Set the system page size 9710318Sandreas.hansson@arm.com auxv.push_back(auxv_t(M5_AT_PAGESZ, MipsISA::PageBytes)); 986811SMatt DeVuyst // Set the frequency at which time() increments 996811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_CLKTCK, 100)); 1006811SMatt DeVuyst // For statically linked executables, this is the virtual 1016811SMatt DeVuyst // address of the program header tables if they appear in the 1026811SMatt DeVuyst // executable image. 1036811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_PHDR, elfObject->programHeaderTable())); 1046811SMatt DeVuyst DPRINTF(Loader, "auxv at PHDR %08p\n", elfObject->programHeaderTable()); 1056811SMatt DeVuyst // This is the size of a program header entry from the elf file. 1066811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_PHENT, elfObject->programHeaderSize())); 1076811SMatt DeVuyst // This is the number of program headers from the original elf file. 1086811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_PHNUM, elfObject->programHeaderCount())); 10911389Sbrandon.potter@amd.com // This is the base address of the ELF interpreter; it should be 11011389Sbrandon.potter@amd.com // zero for static executables or contain the base address for 11111389Sbrandon.potter@amd.com // dynamic executables. 11211389Sbrandon.potter@amd.com auxv.push_back(auxv_t(M5_AT_BASE, getBias())); 1136811SMatt DeVuyst //The entry point to the program 1146811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_ENTRY, objFile->entryPoint())); 1156811SMatt DeVuyst //Different user and group IDs 1166811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_UID, uid())); 1176811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_EUID, euid())); 1186811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_GID, gid())); 1196811SMatt DeVuyst auxv.push_back(auxv_t(M5_AT_EGID, egid())); 1206811SMatt DeVuyst } 1216811SMatt DeVuyst 1226811SMatt DeVuyst // Calculate how much space we need for arg & env & auxv arrays. 1236650Sksewell@umich.edu int argv_array_size = intSize * (argv.size() + 1); 1246650Sksewell@umich.edu int envp_array_size = intSize * (envp.size() + 1); 1256811SMatt DeVuyst int auxv_array_size = intSize * 2 * (auxv.size() + 1); 1266811SMatt DeVuyst 1276650Sksewell@umich.edu int arg_data_size = 0; 1286650Sksewell@umich.edu for (vector<string>::size_type i = 0; i < argv.size(); ++i) { 1296650Sksewell@umich.edu arg_data_size += argv[i].size() + 1; 1306650Sksewell@umich.edu } 1316650Sksewell@umich.edu int env_data_size = 0; 1326650Sksewell@umich.edu for (vector<string>::size_type i = 0; i < envp.size(); ++i) { 1336650Sksewell@umich.edu env_data_size += envp[i].size() + 1; 1346650Sksewell@umich.edu } 1356650Sksewell@umich.edu 1366650Sksewell@umich.edu int space_needed = 1376811SMatt DeVuyst argv_array_size + 1386811SMatt DeVuyst envp_array_size + 1396811SMatt DeVuyst auxv_array_size + 1406811SMatt DeVuyst arg_data_size + 1416811SMatt DeVuyst env_data_size; 1426650Sksewell@umich.edu 1436650Sksewell@umich.edu // set bottom of stack 14411886Sbrandon.potter@amd.com memState->stackMin = memState->stackBase - space_needed; 1456650Sksewell@umich.edu // align it 14611886Sbrandon.potter@amd.com memState->stackMin = roundDown(memState->stackMin, pageSize); 14711886Sbrandon.potter@amd.com memState->stackSize = memState->stackBase - memState->stackMin; 1486650Sksewell@umich.edu // map memory 14911886Sbrandon.potter@amd.com allocateMem(memState->stackMin, roundUp(memState->stackSize, pageSize)); 1506650Sksewell@umich.edu 1516650Sksewell@umich.edu // map out initial stack contents 15211886Sbrandon.potter@amd.com IntType argv_array_base = memState->stackMin + intSize; // room for argc 1536811SMatt DeVuyst IntType envp_array_base = argv_array_base + argv_array_size; 1546811SMatt DeVuyst IntType auxv_array_base = envp_array_base + envp_array_size; 1556811SMatt DeVuyst IntType arg_data_base = auxv_array_base + auxv_array_size; 1566811SMatt DeVuyst IntType env_data_base = arg_data_base + arg_data_size; 1576650Sksewell@umich.edu 1586650Sksewell@umich.edu // write contents to stack 1596811SMatt DeVuyst IntType argc = argv.size(); 1606650Sksewell@umich.edu 1616811SMatt DeVuyst argc = htog((IntType)argc); 1626650Sksewell@umich.edu 16311886Sbrandon.potter@amd.com initVirtMem.writeBlob(memState->stackMin, (uint8_t*)&argc, intSize); 1646650Sksewell@umich.edu 1656650Sksewell@umich.edu copyStringArray(argv, argv_array_base, arg_data_base, initVirtMem); 1666650Sksewell@umich.edu 1676650Sksewell@umich.edu copyStringArray(envp, envp_array_base, env_data_base, initVirtMem); 1686650Sksewell@umich.edu 1696811SMatt DeVuyst // Copy the aux vector 1706811SMatt DeVuyst for (typename vector<auxv_t>::size_type x = 0; x < auxv.size(); x++) { 1718852Sandreas.hansson@arm.com initVirtMem.writeBlob(auxv_array_base + x * 2 * intSize, 1726811SMatt DeVuyst (uint8_t*)&(auxv[x].a_type), intSize); 1738852Sandreas.hansson@arm.com initVirtMem.writeBlob(auxv_array_base + (x * 2 + 1) * intSize, 1746811SMatt DeVuyst (uint8_t*)&(auxv[x].a_val), intSize); 1756811SMatt DeVuyst } 1766811SMatt DeVuyst 1776811SMatt DeVuyst // Write out the terminating zeroed auxilliary vector 1786811SMatt DeVuyst for (unsigned i = 0; i < 2; i++) { 1796811SMatt DeVuyst const IntType zero = 0; 1806811SMatt DeVuyst const Addr addr = auxv_array_base + 2 * intSize * (auxv.size() + i); 1818852Sandreas.hansson@arm.com initVirtMem.writeBlob(addr, (uint8_t*)&zero, intSize); 1826811SMatt DeVuyst } 1836811SMatt DeVuyst 1846650Sksewell@umich.edu ThreadContext *tc = system->getThreadContext(contextIds[0]); 1856650Sksewell@umich.edu 1866650Sksewell@umich.edu setSyscallArg(tc, 0, argc); 1876650Sksewell@umich.edu setSyscallArg(tc, 1, argv_array_base); 18811886Sbrandon.potter@amd.com tc->setIntReg(StackPointerReg, memState->stackMin); 1896650Sksewell@umich.edu 19011389Sbrandon.potter@amd.com tc->pcState(getStartPC()); 1916650Sksewell@umich.edu} 1926650Sksewell@umich.edu 1936650Sksewell@umich.edu 1945958Sgblack@eecs.umich.eduMipsISA::IntReg 19511851Sbrandon.potter@amd.comMipsProcess::getSyscallArg(ThreadContext *tc, int &i) 1965958Sgblack@eecs.umich.edu{ 1975958Sgblack@eecs.umich.edu assert(i < 6); 1986701Sgblack@eecs.umich.edu return tc->readIntReg(FirstArgumentReg + i++); 1995958Sgblack@eecs.umich.edu} 2005958Sgblack@eecs.umich.edu 2015958Sgblack@eecs.umich.eduvoid 20211851Sbrandon.potter@amd.comMipsProcess::setSyscallArg(ThreadContext *tc, int i, MipsISA::IntReg val) 2035958Sgblack@eecs.umich.edu{ 2045958Sgblack@eecs.umich.edu assert(i < 6); 2055958Sgblack@eecs.umich.edu tc->setIntReg(FirstArgumentReg + i, val); 2065958Sgblack@eecs.umich.edu} 2075958Sgblack@eecs.umich.edu 2085958Sgblack@eecs.umich.eduvoid 20911851Sbrandon.potter@amd.comMipsProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret) 2105958Sgblack@eecs.umich.edu{ 21110223Ssteve.reinhardt@amd.com if (sysret.successful()) { 2125958Sgblack@eecs.umich.edu // no error 2135958Sgblack@eecs.umich.edu tc->setIntReg(SyscallSuccessReg, 0); 21410223Ssteve.reinhardt@amd.com tc->setIntReg(ReturnValueReg, sysret.returnValue()); 2155958Sgblack@eecs.umich.edu } else { 2165958Sgblack@eecs.umich.edu // got an error, return details 2175958Sgblack@eecs.umich.edu tc->setIntReg(SyscallSuccessReg, (IntReg) -1); 21810223Ssteve.reinhardt@amd.com tc->setIntReg(ReturnValueReg, sysret.errnoValue()); 2195958Sgblack@eecs.umich.edu } 2205958Sgblack@eecs.umich.edu} 221