From d10e17eb06f79f6325267cb7fff9babd513c4103 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Sun, 5 May 2019 15:30:02 +0800 Subject: [PATCH] fix rv64 stack pointer alignment --- kernel/src/arch/riscv32/boot/trap.asm | 14 +++++++------- kernel/src/arch/riscv32/context.rs | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/kernel/src/arch/riscv32/boot/trap.asm b/kernel/src/arch/riscv32/boot/trap.asm index 17ff77e..aa8a8ce 100644 --- a/kernel/src/arch/riscv32/boot/trap.asm +++ b/kernel/src/arch/riscv32/boot/trap.asm @@ -12,11 +12,11 @@ bnez sp, trap_from_user trap_from_kernel: csrr sp, sscratch - STORE gp, -1 + STORE gp, -36 # sscratch = previous-sp, sp = kernel-sp trap_from_user: # provide room for trap frame - addi sp, sp, -37 * XLENB + addi sp, sp, -36 * XLENB # save x registers except x2 (sp) STORE x1, 1 STORE x3, 3 @@ -49,8 +49,8 @@ trap_from_user: STORE x30, 30 STORE x31, 31 - # load hartid to gp from sp[36] - LOAD gp, 36 + # load hartid to gp from sp[0] + LOAD gp, 0 # get sp, sstatus, sepc, stval, scause # set sscratch = 0 @@ -73,9 +73,9 @@ trap_from_user: andi s0, s1, 1 << 8 # sstatus.SPP = 1 bnez s0, _to_kernel # s0 = back to kernel? _to_user: - addi s0, sp, 37*XLENB - csrw sscratch, s0 # sscratch = kernel-sp - STORE gp, 36 # store hartid from gp to sp[36] + addi s0, sp, 36*XLENB + csrw sscratch, s0 # sscratch = kernel-sp + STORE gp, 0 # store hartid from gp to sp[0] _to_kernel: # restore sstatus, sepc csrw sstatus, s1 diff --git a/kernel/src/arch/riscv32/context.rs b/kernel/src/arch/riscv32/context.rs index c8ac7ea..b82883e 100644 --- a/kernel/src/arch/riscv32/context.rs +++ b/kernel/src/arch/riscv32/context.rs @@ -1,6 +1,9 @@ use riscv::register::{scause::Scause, sstatus, sstatus::Sstatus}; /// Saved registers on a trap. +/// +/// WARN: sp needs to be 128-bit (16 Bytes) aligned +/// but we found it actually needs 32 Bytes aligned ??? #[derive(Clone)] #[repr(C)] pub struct TrapFrame { @@ -14,8 +17,6 @@ pub struct TrapFrame { pub stval: usize, /// Supervisor Cause pub scause: Scause, - /// Reserve space for hartid - pub _hartid: usize, } impl TrapFrame {