arch-riscv: Fix bad stack initialization
authorAlec Roelke <ar4jc@virginia.edu>
Tue, 16 May 2017 21:00:02 +0000 (17:00 -0400)
committerAlec Roelke <ar4jc@virginia.edu>
Tue, 23 May 2017 19:10:18 +0000 (19:10 +0000)
This patch fixes a problem with RISC-V initial stack setup in SE mode
where the AT_RANDOM aux vector value contains an address that is too
close to the top of the stack and doesn't fit the required 16 bytes. To
fix this, the program header table was added to the top of the stack
just like the RISC-V proxy kernel does.

Change-Id: I814562e060ff041cd0d7a7c54c3685645bd325a3
Reviewed-on: https://gem5-review.googlesource.com/3401
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Reviewed-by: Brandon Potter <Brandon.Potter@amd.com>
Maintainer: Alec Roelke <ar4jc@virginia.edu>

src/arch/riscv/process.cc

index 13038c17c93472929c6df580db9b35821ed7feb8..eeea5ee480460b495379c91f0fd93d51054c3eee 100644 (file)
@@ -88,6 +88,7 @@ RiscvProcess::argsInit(int pageSize)
 
     // Determine stack size and populate auxv
     Addr stack_top = memState->getStackMin();
+    stack_top -= elfObject->programHeaderSize();
     for (const string& arg: argv)
         stack_top -= arg.size() + 1;
     for (const string& env: envp)
@@ -113,6 +114,16 @@ RiscvProcess::argsInit(int pageSize)
     allocateMem(roundDown(stack_top, pageSize),
             roundUp(memState->getStackSize(), pageSize));
 
+    // Copy program headers to stack
+    memState->setStackMin(memState->getStackMin() -
+            elfObject->programHeaderSize());
+    uint8_t* phdr = new uint8_t[elfObject->programHeaderSize()];
+    initVirtMem.readBlob(elfObject->programHeaderTable(), phdr,
+            elfObject->programHeaderSize());
+    initVirtMem.writeBlob(memState->getStackMin(), phdr,
+            elfObject->programHeaderSize());
+    delete phdr;
+
     // Copy argv to stack
     vector<Addr> argPointers;
     for (const string& arg: argv) {