From 17ba0a3429f394522db806ddfbf03f859fbd74b4 Mon Sep 17 00:00:00 2001 From: occheung Date: Fri, 8 Oct 2021 10:59:40 +0800 Subject: [PATCH 1/2] riscv32: align stack to 4k boundary --- src/arch/riscv32.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arch/riscv32.rs b/src/arch/riscv32.rs index 54e5a01..16303be 100644 --- a/src/arch/riscv32.rs +++ b/src/arch/riscv32.rs @@ -44,7 +44,7 @@ use core::mem; use stack::Stack; -pub const STACK_ALIGNMENT: usize = 16; +pub const STACK_ALIGNMENT: usize = 4096; #[derive(Debug, Clone, Copy)] #[repr(transparent)] -- 2.44.1 From 3ecbe53f7644b18ee46ebd5b2ca12c9cbceec43a Mon Sep 17 00:00:00 2001 From: occheung Date: Fri, 8 Oct 2021 11:00:30 +0800 Subject: [PATCH 2/2] riscv32: env call on context switch --- src/arch/riscv32.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/arch/riscv32.rs b/src/arch/riscv32.rs index 16303be..4d18ecb 100644 --- a/src/arch/riscv32.rs +++ b/src/arch/riscv32.rs @@ -159,6 +159,12 @@ pub unsafe fn swap(arg: usize, new_sp: StackPointer, // Just pass a dummy pointer if we aren't linking the stack &mut dummy }; + let stack_limit = if let Some(new_stack) = new_stack { + new_stack.limit() as *mut usize + } else { + // Stack should not reach all the way to address 0 + 0 as *mut usize + }; #[naked] unsafe extern "C" fn trampoline() { @@ -174,6 +180,9 @@ pub unsafe fn swap(arg: usize, new_sp: StackPointer, .cfi_offset fp, 4 .cfi_offset ra, 0 + # Perform environment call to modify the PMP regions + ecall + # Link the call stacks together by writing the current stack bottom # address to the CFA slot in the new stack. sw sp, 0(a3) @@ -209,6 +218,7 @@ pub unsafe fn swap(arg: usize, new_sp: StackPointer, "{a0}" (arg) "{a2}" (new_sp.0) "{a3}" (new_cfa) + "{a7}" (stack_limit) :/*"zero",*/"ra",/*"sp","gp","tp",*/"t0","t1","t2", /*"fp",*/"s1",/*"a0", "a1"*/"a2", "a3", "a4", "a5", "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", -- 2.44.1