process.cc revision 13028
111723Sar4jc@virginia.edu/* 211723Sar4jc@virginia.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan 311723Sar4jc@virginia.edu * Copyright (c) 2016 The University of Virginia 411723Sar4jc@virginia.edu * All rights reserved. 511723Sar4jc@virginia.edu * 611723Sar4jc@virginia.edu * Redistribution and use in source and binary forms, with or without 711723Sar4jc@virginia.edu * modification, are permitted provided that the following conditions are 811723Sar4jc@virginia.edu * met: redistributions of source code must retain the above copyright 911723Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer; 1011723Sar4jc@virginia.edu * redistributions in binary form must reproduce the above copyright 1111723Sar4jc@virginia.edu * notice, this list of conditions and the following disclaimer in the 1211723Sar4jc@virginia.edu * documentation and/or other materials provided with the distribution; 1311723Sar4jc@virginia.edu * neither the name of the copyright holders nor the names of its 1411723Sar4jc@virginia.edu * contributors may be used to endorse or promote products derived from 1511723Sar4jc@virginia.edu * this software without specific prior written permission. 1611723Sar4jc@virginia.edu * 1711723Sar4jc@virginia.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1811723Sar4jc@virginia.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1911723Sar4jc@virginia.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 2011723Sar4jc@virginia.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2111723Sar4jc@virginia.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2211723Sar4jc@virginia.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2311723Sar4jc@virginia.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2411723Sar4jc@virginia.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2511723Sar4jc@virginia.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2611723Sar4jc@virginia.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2711723Sar4jc@virginia.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2811723Sar4jc@virginia.edu * 2911723Sar4jc@virginia.edu * Authors: Gabe Black 3011723Sar4jc@virginia.edu * Ali Saidi 3111723Sar4jc@virginia.edu * Korey Sewell 3211723Sar4jc@virginia.edu * Alec Roelke 3311723Sar4jc@virginia.edu */ 3411723Sar4jc@virginia.edu#include "arch/riscv/process.hh" 3511723Sar4jc@virginia.edu 3611964Sar4jc@virginia.edu#include <algorithm> 3711964Sar4jc@virginia.edu#include <cstddef> 3811964Sar4jc@virginia.edu#include <iostream> 3912394Sar4jc@virginia.edu#include <iterator> 4011964Sar4jc@virginia.edu#include <map> 4111964Sar4jc@virginia.edu#include <string> 4211723Sar4jc@virginia.edu#include <vector> 4311723Sar4jc@virginia.edu 4412695Sar4jc@virginia.edu#include "arch/riscv/isa.hh" 4511723Sar4jc@virginia.edu#include "arch/riscv/isa_traits.hh" 4612695Sar4jc@virginia.edu#include "arch/riscv/registers.hh" 4711723Sar4jc@virginia.edu#include "base/loader/elf_object.hh" 4811723Sar4jc@virginia.edu#include "base/loader/object_file.hh" 4912334Sgabeblack@google.com#include "base/logging.hh" 5012394Sar4jc@virginia.edu#include "base/random.hh" 5111723Sar4jc@virginia.edu#include "cpu/thread_context.hh" 5211964Sar4jc@virginia.edu#include "debug/Stack.hh" 5311723Sar4jc@virginia.edu#include "mem/page_table.hh" 5411964Sar4jc@virginia.edu#include "params/Process.hh" 5511854Sbrandon.potter@amd.com#include "sim/aux_vector.hh" 5611723Sar4jc@virginia.edu#include "sim/process.hh" 5711723Sar4jc@virginia.edu#include "sim/process_impl.hh" 5811800Sbrandon.potter@amd.com#include "sim/syscall_return.hh" 5911723Sar4jc@virginia.edu#include "sim/system.hh" 6011723Sar4jc@virginia.edu 6111723Sar4jc@virginia.eduusing namespace std; 6211723Sar4jc@virginia.eduusing namespace RiscvISA; 6311723Sar4jc@virginia.edu 6412431Sgabeblack@google.comRiscvProcess::RiscvProcess(ProcessParams *params, ObjectFile *objFile) : 6512448Sgabeblack@google.com Process(params, 6612448Sgabeblack@google.com new EmulationPageTable(params->name, params->pid, PageBytes), 6712432Sgabeblack@google.com objFile) 6811723Sar4jc@virginia.edu{ 6912441Sgabeblack@google.com fatal_if(params->useArchPT, "Arch page tables not implemented."); 7011970Sar4jc@virginia.edu const Addr stack_base = 0x7FFFFFFFFFFFFFFFL; 7112393Sar4jc@virginia.edu const Addr max_stack_size = 8 * 1024 * 1024; 7211964Sar4jc@virginia.edu const Addr next_thread_stack_base = stack_base - max_stack_size; 7311964Sar4jc@virginia.edu const Addr brk_point = roundUp(objFile->bssBase() + objFile->bssSize(), 7411964Sar4jc@virginia.edu PageBytes); 7511970Sar4jc@virginia.edu const Addr mmap_end = 0x4000000000000000L; 7611905SBrandon.Potter@amd.com memState = make_shared<MemState>(brk_point, stack_base, max_stack_size, 7711964Sar4jc@virginia.edu next_thread_stack_base, mmap_end); 7811723Sar4jc@virginia.edu} 7911723Sar4jc@virginia.edu 8011723Sar4jc@virginia.eduvoid 8111851Sbrandon.potter@amd.comRiscvProcess::initState() 8211723Sar4jc@virginia.edu{ 8311851Sbrandon.potter@amd.com Process::initState(); 8411723Sar4jc@virginia.edu 8511723Sar4jc@virginia.edu argsInit<uint64_t>(PageBytes); 8612695Sar4jc@virginia.edu for (ContextID ctx: contextIds) 8712695Sar4jc@virginia.edu system->getThreadContext(ctx)->setMiscRegNoEffect(MISCREG_PRV, PRV_U); 8811723Sar4jc@virginia.edu} 8911723Sar4jc@virginia.edu 9011723Sar4jc@virginia.edutemplate<class IntType> void 9111851Sbrandon.potter@amd.comRiscvProcess::argsInit(int pageSize) 9211723Sar4jc@virginia.edu{ 9312394Sar4jc@virginia.edu const int RandomBytes = 16; 9412394Sar4jc@virginia.edu 9511723Sar4jc@virginia.edu updateBias(); 9611964Sar4jc@virginia.edu objFile->loadSections(initVirtMem); 9711964Sar4jc@virginia.edu ElfObject* elfObject = dynamic_cast<ElfObject*>(objFile); 9811964Sar4jc@virginia.edu memState->setStackMin(memState->getStackBase()); 9911723Sar4jc@virginia.edu 10011964Sar4jc@virginia.edu // Determine stack size and populate auxv 10111964Sar4jc@virginia.edu Addr stack_top = memState->getStackMin(); 10212394Sar4jc@virginia.edu stack_top -= RandomBytes; 10311964Sar4jc@virginia.edu for (const string& arg: argv) 10411964Sar4jc@virginia.edu stack_top -= arg.size() + 1; 10511964Sar4jc@virginia.edu for (const string& env: envp) 10611964Sar4jc@virginia.edu stack_top -= env.size() + 1; 10711964Sar4jc@virginia.edu stack_top &= -sizeof(Addr); 10811723Sar4jc@virginia.edu 10911964Sar4jc@virginia.edu vector<AuxVector<IntType>> auxv; 11011964Sar4jc@virginia.edu if (elfObject != nullptr) { 11111964Sar4jc@virginia.edu auxv.push_back({M5_AT_ENTRY, objFile->entryPoint()}); 11211964Sar4jc@virginia.edu auxv.push_back({M5_AT_PHNUM, elfObject->programHeaderCount()}); 11311964Sar4jc@virginia.edu auxv.push_back({M5_AT_PHENT, elfObject->programHeaderSize()}); 11411964Sar4jc@virginia.edu auxv.push_back({M5_AT_PHDR, elfObject->programHeaderTable()}); 11511964Sar4jc@virginia.edu auxv.push_back({M5_AT_PAGESZ, PageBytes}); 11611964Sar4jc@virginia.edu auxv.push_back({M5_AT_SECURE, 0}); 11711964Sar4jc@virginia.edu auxv.push_back({M5_AT_RANDOM, stack_top}); 11811964Sar4jc@virginia.edu auxv.push_back({M5_AT_NULL, 0}); 11911964Sar4jc@virginia.edu } 12011964Sar4jc@virginia.edu stack_top -= (1 + argv.size()) * sizeof(Addr) + 12111964Sar4jc@virginia.edu (1 + envp.size()) * sizeof(Addr) + 12211964Sar4jc@virginia.edu sizeof(Addr) + 2 * sizeof(IntType) * auxv.size(); 12311964Sar4jc@virginia.edu stack_top &= -2*sizeof(Addr); 12411964Sar4jc@virginia.edu memState->setStackSize(memState->getStackBase() - stack_top); 12511964Sar4jc@virginia.edu allocateMem(roundDown(stack_top, pageSize), 12611964Sar4jc@virginia.edu roundUp(memState->getStackSize(), pageSize)); 12711964Sar4jc@virginia.edu 12812394Sar4jc@virginia.edu // Copy random bytes (for AT_RANDOM) to stack 12912394Sar4jc@virginia.edu memState->setStackMin(memState->getStackMin() - RandomBytes); 13012394Sar4jc@virginia.edu uint8_t at_random[RandomBytes]; 13112394Sar4jc@virginia.edu generate(begin(at_random), end(at_random), 13212394Sar4jc@virginia.edu [&]{ return random_mt.random(0, 0xFF); }); 13312394Sar4jc@virginia.edu initVirtMem.writeBlob(memState->getStackMin(), at_random, RandomBytes); 13412039Sar4jc@virginia.edu 13511964Sar4jc@virginia.edu // Copy argv to stack 13611964Sar4jc@virginia.edu vector<Addr> argPointers; 13711964Sar4jc@virginia.edu for (const string& arg: argv) { 13811964Sar4jc@virginia.edu memState->setStackMin(memState->getStackMin() - (arg.size() + 1)); 13911964Sar4jc@virginia.edu initVirtMem.writeString(memState->getStackMin(), arg.c_str()); 14011964Sar4jc@virginia.edu argPointers.push_back(memState->getStackMin()); 14111964Sar4jc@virginia.edu if (DTRACE(Stack)) { 14211964Sar4jc@virginia.edu string wrote; 14311964Sar4jc@virginia.edu initVirtMem.readString(wrote, argPointers.back()); 14411964Sar4jc@virginia.edu DPRINTFN("Wrote arg \"%s\" to address %p\n", 14511964Sar4jc@virginia.edu wrote, (void*)memState->getStackMin()); 14611964Sar4jc@virginia.edu } 14711964Sar4jc@virginia.edu } 14811964Sar4jc@virginia.edu argPointers.push_back(0); 14911964Sar4jc@virginia.edu 15011964Sar4jc@virginia.edu // Copy envp to stack 15111964Sar4jc@virginia.edu vector<Addr> envPointers; 15211964Sar4jc@virginia.edu for (const string& env: envp) { 15311964Sar4jc@virginia.edu memState->setStackMin(memState->getStackMin() - (env.size() + 1)); 15411964Sar4jc@virginia.edu initVirtMem.writeString(memState->getStackMin(), env.c_str()); 15511964Sar4jc@virginia.edu envPointers.push_back(memState->getStackMin()); 15611964Sar4jc@virginia.edu DPRINTF(Stack, "Wrote env \"%s\" to address %p\n", 15711964Sar4jc@virginia.edu env, (void*)memState->getStackMin()); 15811964Sar4jc@virginia.edu } 15911964Sar4jc@virginia.edu envPointers.push_back(0); 16011964Sar4jc@virginia.edu 16111964Sar4jc@virginia.edu // Align stack 16211964Sar4jc@virginia.edu memState->setStackMin(memState->getStackMin() & -sizeof(Addr)); 16311964Sar4jc@virginia.edu 16411964Sar4jc@virginia.edu // Calculate bottom of stack 16511964Sar4jc@virginia.edu memState->setStackMin(memState->getStackMin() - 16611964Sar4jc@virginia.edu ((1 + argv.size()) * sizeof(Addr) + 16711964Sar4jc@virginia.edu (1 + envp.size()) * sizeof(Addr) + 16811964Sar4jc@virginia.edu sizeof(Addr) + 2 * sizeof(IntType) * auxv.size())); 16911964Sar4jc@virginia.edu memState->setStackMin(memState->getStackMin() & -2*sizeof(Addr)); 17011964Sar4jc@virginia.edu Addr sp = memState->getStackMin(); 17111964Sar4jc@virginia.edu const auto pushOntoStack = 17211964Sar4jc@virginia.edu [this, &sp](const uint8_t* data, const size_t size) { 17311964Sar4jc@virginia.edu initVirtMem.writeBlob(sp, data, size); 17411964Sar4jc@virginia.edu sp += size; 17511964Sar4jc@virginia.edu }; 17611964Sar4jc@virginia.edu 17711964Sar4jc@virginia.edu // Push argc and argv pointers onto stack 17811964Sar4jc@virginia.edu IntType argc = htog((IntType)argv.size()); 17911964Sar4jc@virginia.edu DPRINTF(Stack, "Wrote argc %d to address %p\n", 18011964Sar4jc@virginia.edu argv.size(), (void*)sp); 18111964Sar4jc@virginia.edu pushOntoStack((uint8_t*)&argc, sizeof(IntType)); 18211964Sar4jc@virginia.edu for (const Addr& argPointer: argPointers) { 18311964Sar4jc@virginia.edu DPRINTF(Stack, "Wrote argv pointer %p to address %p\n", 18411964Sar4jc@virginia.edu (void*)argPointer, (void*)sp); 18511964Sar4jc@virginia.edu pushOntoStack((uint8_t*)&argPointer, sizeof(Addr)); 18611723Sar4jc@virginia.edu } 18711723Sar4jc@virginia.edu 18811964Sar4jc@virginia.edu // Push env pointers onto stack 18911964Sar4jc@virginia.edu for (const Addr& envPointer: envPointers) { 19011964Sar4jc@virginia.edu DPRINTF(Stack, "Wrote envp pointer %p to address %p\n", 19111964Sar4jc@virginia.edu (void*)envPointer, (void*)sp); 19211964Sar4jc@virginia.edu pushOntoStack((uint8_t*)&envPointer, sizeof(Addr)); 19311723Sar4jc@virginia.edu } 19411723Sar4jc@virginia.edu 19511964Sar4jc@virginia.edu // Push aux vector onto stack 19611964Sar4jc@virginia.edu std::map<IntType, string> aux_keys = { 19711964Sar4jc@virginia.edu {M5_AT_ENTRY, "M5_AT_ENTRY"}, 19811964Sar4jc@virginia.edu {M5_AT_PHNUM, "M5_AT_PHNUM"}, 19911964Sar4jc@virginia.edu {M5_AT_PHENT, "M5_AT_PHENT"}, 20011964Sar4jc@virginia.edu {M5_AT_PHDR, "M5_AT_PHDR"}, 20111964Sar4jc@virginia.edu {M5_AT_PAGESZ, "M5_AT_PAGESZ"}, 20211964Sar4jc@virginia.edu {M5_AT_SECURE, "M5_AT_SECURE"}, 20311964Sar4jc@virginia.edu {M5_AT_RANDOM, "M5_AT_RANDOM"}, 20411964Sar4jc@virginia.edu {M5_AT_NULL, "M5_AT_NULL"} 20511964Sar4jc@virginia.edu }; 20611964Sar4jc@virginia.edu for (const AuxVector<IntType>& aux: auxv) { 20711964Sar4jc@virginia.edu DPRINTF(Stack, "Wrote aux key %s to address %p\n", 20813028Sbrandon.potter@amd.com aux_keys[aux.getAuxType()], (void*)sp); 20913028Sbrandon.potter@amd.com pushOntoStack((uint8_t*)&aux.getAuxType(), sizeof(IntType)); 21011964Sar4jc@virginia.edu DPRINTF(Stack, "Wrote aux value %x to address %p\n", 21113028Sbrandon.potter@amd.com aux.getAuxVal(), (void*)sp); 21213028Sbrandon.potter@amd.com pushOntoStack((uint8_t*)&aux.getAuxVal(), sizeof(IntType)); 21311723Sar4jc@virginia.edu } 21411723Sar4jc@virginia.edu 21511723Sar4jc@virginia.edu ThreadContext *tc = system->getThreadContext(contextIds[0]); 21611905SBrandon.Potter@amd.com tc->setIntReg(StackPointerReg, memState->getStackMin()); 21711723Sar4jc@virginia.edu tc->pcState(getStartPC()); 21811964Sar4jc@virginia.edu 21911964Sar4jc@virginia.edu memState->setStackMin(roundDown(memState->getStackMin(), pageSize)); 22011723Sar4jc@virginia.edu} 22111723Sar4jc@virginia.edu 22211723Sar4jc@virginia.eduRiscvISA::IntReg 22311851Sbrandon.potter@amd.comRiscvProcess::getSyscallArg(ThreadContext *tc, int &i) 22411723Sar4jc@virginia.edu{ 22512412Sar4jc@virginia.edu // If a larger index is requested than there are syscall argument 22612412Sar4jc@virginia.edu // registers, return 0 22711806Sar4jc@virginia.edu RiscvISA::IntReg retval = 0; 22812412Sar4jc@virginia.edu if (i < SyscallArgumentRegs.size()) 22911806Sar4jc@virginia.edu retval = tc->readIntReg(SyscallArgumentRegs[i]); 23011806Sar4jc@virginia.edu i++; 23111806Sar4jc@virginia.edu return retval; 23211723Sar4jc@virginia.edu} 23311723Sar4jc@virginia.edu 23411723Sar4jc@virginia.eduvoid 23511851Sbrandon.potter@amd.comRiscvProcess::setSyscallArg(ThreadContext *tc, int i, RiscvISA::IntReg val) 23611723Sar4jc@virginia.edu{ 23711723Sar4jc@virginia.edu tc->setIntReg(SyscallArgumentRegs[i], val); 23811723Sar4jc@virginia.edu} 23911723Sar4jc@virginia.edu 24011723Sar4jc@virginia.eduvoid 24111851Sbrandon.potter@amd.comRiscvProcess::setSyscallReturn(ThreadContext *tc, SyscallReturn sysret) 24211723Sar4jc@virginia.edu{ 24311723Sar4jc@virginia.edu if (sysret.successful()) { 24411723Sar4jc@virginia.edu // no error 24511723Sar4jc@virginia.edu tc->setIntReg(SyscallPseudoReturnReg, sysret.returnValue()); 24611723Sar4jc@virginia.edu } else { 24711723Sar4jc@virginia.edu // got an error, return details 24811723Sar4jc@virginia.edu tc->setIntReg(SyscallPseudoReturnReg, sysret.errnoValue()); 24911723Sar4jc@virginia.edu } 25011723Sar4jc@virginia.edu} 251