49#include "debug/Stack.hh"
51#include "params/Process.hh"
61using namespace RiscvISA;
76 const Addr stack_base = 0x7FFFFFFFFFFFFFFFL;
77 const Addr max_stack_size = 8 * 1024 * 1024;
78 const Addr next_thread_stack_base = stack_base - max_stack_size;
80 const Addr mmap_end = 0x4000000000000000L;
81 memState = std::make_shared<MemState>(
this, brk_point, stack_base,
82 max_stack_size, next_thread_stack_base, mmap_end);
89 const Addr stack_base = 0x7FFFFFFF;
90 const Addr max_stack_size = 8 * 1024 * 1024;
91 const Addr next_thread_stack_base = stack_base - max_stack_size;
93 const Addr mmap_end = 0x40000000L;
94 memState = std::make_shared<MemState>(
this, brk_point, stack_base,
95 max_stack_size, next_thread_stack_base, mmap_end);
105 auto *tc =
system->threads[ctx];
107 auto *isa =
dynamic_cast<ISA*
>(tc->getIsaPtr());
108 fatal_if(isa->rvType() !=
RV64,
"RISC V CPU should run in 64 bits mode");
119 auto *tc =
system->threads[ctx];
121 auto *isa =
dynamic_cast<ISA*
>(tc->getIsaPtr());
122 fatal_if(isa->rvType() !=
RV32,
"RISC V CPU should run in 32 bits mode");
126template<
class IntType>
void
129 const int RandomBytes = 16;
130 const int addrSize =
sizeof(IntType);
137 stack_top -= RandomBytes;
138 for (
const std::string& arg:
argv)
139 stack_top -= arg.size() + 1;
140 for (
const std::string& env:
envp)
141 stack_top -= env.size() + 1;
142 stack_top &= -addrSize;
145 if (elfObject !=
nullptr) {
155 stack_top -= (1 +
argv.size()) * addrSize +
156 (1 +
envp.size()) * addrSize +
157 addrSize + 2 *
sizeof(IntType) * auxv.size();
158 stack_top &= -2*addrSize;
165 uint8_t at_random[RandomBytes];
166 std::generate(std::begin(at_random), std::end(at_random),
172 for (
const std::string& arg:
argv) {
175 argPointers.push_back(
memState->getStackMin());
178 initVirtMem->readString(wrote, argPointers.back());
179 DPRINTFN(
"Wrote arg \"%s\" to address %p\n",
180 wrote, (
void*)
memState->getStackMin());
183 argPointers.push_back(0);
187 for (
const std::string& env:
envp) {
190 envPointers.push_back(
memState->getStackMin());
191 DPRINTF(Stack,
"Wrote env \"%s\" to address %p\n",
192 env, (
void*)
memState->getStackMin());
194 envPointers.push_back(0);
201 ((1 +
argv.size()) * addrSize +
202 (1 +
envp.size()) * addrSize +
203 addrSize + 2 *
sizeof(IntType) * auxv.size()));
206 const auto pushOntoStack =
207 [
this, &
sp](IntType
data) {
213 IntType argc =
argv.size();
214 DPRINTF(Stack,
"Wrote argc %d to address %#x\n", argc,
sp);
217 for (
const Addr& argPointer: argPointers) {
218 DPRINTF(Stack,
"Wrote argv pointer %#x to address %#x\n",
220 pushOntoStack(argPointer);
224 for (
const Addr& envPointer: envPointers) {
225 DPRINTF(Stack,
"Wrote envp pointer %#x to address %#x\n",
227 pushOntoStack(envPointer);
231 std::map<IntType, std::string> aux_keys = {
241 for (
const auto &aux: auxv) {
242 DPRINTF(Stack,
"Wrote aux key %s to address %#x\n",
243 aux_keys[aux.type],
sp);
244 pushOntoStack(aux.type);
245 DPRINTF(Stack,
"Wrote aux value %x to address %#x\n", aux.val,
sp);
246 pushOntoStack(aux.val);
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
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.
std::enable_if_t< std::is_integral_v< T >, T > random()
Use the SFINAE idiom to choose an implementation based on whether the type is integral or floating po...
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
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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.
const std::string & name()