48#include "debug/Stack.hh"
50#include "params/Process.hh"
75 const Addr stack_base = 0x7FFFFFFFFFFFFFFFL;
76 const Addr max_stack_size = 8 * 1024 * 1024;
77 const Addr next_thread_stack_base = stack_base - max_stack_size;
79 const Addr mmap_end = 0x4000000000000000L;
80 memState = std::make_shared<MemState>(
this, brk_point, stack_base,
81 max_stack_size, next_thread_stack_base, mmap_end);
88 const Addr stack_base = 0x7FFFFFFF;
89 const Addr max_stack_size = 8 * 1024 * 1024;
90 const Addr next_thread_stack_base = stack_base - max_stack_size;
92 const Addr mmap_end = 0x40000000L;
93 memState = std::make_shared<MemState>(
this, brk_point, stack_base,
94 max_stack_size, next_thread_stack_base, mmap_end);
104 auto *tc =
system->threads[ctx];
106 auto *isa =
dynamic_cast<ISA*
>(tc->getIsaPtr());
107 fatal_if(isa->rvType() !=
RV64,
"RISC V CPU should run in 64 bits mode");
110 "RISC V SE mode can't run without supervisor and user "
122 auto *tc =
system->threads[ctx];
124 auto *isa =
dynamic_cast<ISA*
>(tc->getIsaPtr());
125 fatal_if(isa->rvType() !=
RV32,
"RISC V CPU should run in 32 bits mode");
128 "RISC V SE mode can't run without supervisor and user "
133template<
class IntType>
void
136 const int RandomBytes = 16;
137 const int addrSize =
sizeof(IntType);
144 stack_top -= RandomBytes;
145 for (
const std::string& arg:
argv)
146 stack_top -= arg.size() + 1;
147 for (
const std::string& env:
envp)
148 stack_top -= env.size() + 1;
149 stack_top &= -addrSize;
152 if (elfObject !=
nullptr) {
162 stack_top -= (1 +
argv.size()) * addrSize +
163 (1 +
envp.size()) * addrSize +
164 addrSize + 2 *
sizeof(IntType) *
auxv.size();
165 stack_top &= -2*addrSize;
172 uint8_t at_random[RandomBytes];
173 std::generate(std::begin(at_random), std::end(at_random),
174 [&]{
return rng->random(0, 0xFF); });
179 for (
const std::string& arg:
argv) {
182 argPointers.push_back(
memState->getStackMin());
185 initVirtMem->readString(wrote, argPointers.back());
186 DPRINTFN(
"Wrote arg \"%s\" to address %p\n",
187 wrote, (
void*)
memState->getStackMin());
190 argPointers.push_back(0);
194 for (
const std::string& env:
envp) {
197 envPointers.push_back(
memState->getStackMin());
198 DPRINTF(Stack,
"Wrote env \"%s\" to address %p\n",
199 env, (
void*)
memState->getStackMin());
201 envPointers.push_back(0);
208 ((1 +
argv.size()) * addrSize +
209 (1 +
envp.size()) * addrSize +
210 addrSize + 2 *
sizeof(IntType) *
auxv.size()));
213 const auto pushOntoStack =
214 [
this, &
sp](IntType
data) {
220 IntType argc =
argv.size();
221 DPRINTF(Stack,
"Wrote argc %d to address %#x\n", argc,
sp);
224 for (
const Addr& argPointer: argPointers) {
225 DPRINTF(Stack,
"Wrote argv pointer %#x to address %#x\n",
227 pushOntoStack(argPointer);
231 for (
const Addr& envPointer: envPointers) {
232 DPRINTF(Stack,
"Wrote envp pointer %#x to address %#x\n",
234 pushOntoStack(envPointer);
238 std::map<IntType, std::string> aux_keys = {
248 for (
const auto &aux:
auxv) {
249 DPRINTF(Stack,
"Wrote aux key %s to address %#x\n",
250 aux_keys[aux.type],
sp);
251 pushOntoStack(aux.type);
252 DPRINTF(Stack,
"Wrote aux value %x to address %#x\n", aux.val,
sp);
253 pushOntoStack(aux.val);
virtual std::string name() const
loader::MemoryImage image
std::unique_ptr< SETranslatingPortProxy > initVirtMem
std::shared_ptr< MemState > memState
std::vector< std::string > argv
std::vector< ContextID > contextIds
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
std::vector< std::string > envp
Process(const ProcessParams ¶ms, EmulationPageTable *pTable, loader::ObjectFile *obj_file)
loader::ObjectFile * objFile
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
RiscvProcess32(const ProcessParams ¶ms, loader::ObjectFile *objFile)
RiscvProcess64(const ProcessParams ¶ms, loader::ObjectFile *objFile)
void initState() override
initState() is called on each SimObject when not restoring from a checkpoint.
RiscvProcess(const ProcessParams ¶ms, loader::ObjectFile *objFile)
void argsInit(int pageSize)
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void setReg(const RegId ®, RegVal val)
virtual const PCStateBase & pcState() const =0
static constexpr T roundDown(const T &val, const U &align)
This function is used to align addresses in memory.
static constexpr T roundUp(const T &val, const U &align)
This function is used to align addresses in memory.
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
const Params & params() const
constexpr auto & StackPointerReg
constexpr enums::RiscvType RV32
constexpr enums::RiscvType RV64
Copyright (c) 2024 Arm Limited All rights reserved.
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
int ContextID
Globally unique thread context ID.
Declarations of a non-full system Page Table.