firmware: Initialize stack guard via Zkr
Xiang W
wxjstz at 126.com
Mon Oct 6 01:03:39 PDT 2025
Try to initialize stack protection guard via the zkr extension.
Signed-off-by: Xiang W <wxjstz at 126.com>
---
firmware/fw_base.S | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/firmware/fw_base.S b/firmware/fw_base.S
index 5300ecf2..953fba95 100644
--- a/firmware/fw_base.S
+++ b/firmware/fw_base.S
@@ -107,6 +107,30 @@ _bss_zero:
add s4, s4, __SIZEOF_POINTER__
blt s4, s5, _bss_zero
+ /* Trying to initialize the stack guard via the Zkr extension */
+ lla t0, __stack_chk_guard_done
+ csrw CSR_MTVEC, t0
+ li t0, 0
+ li t3, 2
+ li t4, 0xffff
+ li t5, __SIZEOF_POINTER__
+__stack_chk_guard_loop:
+ csrrw t1, CSR_SEED, x0
+ srli t2, t1, 30
+ andi t2, t2, 3
+ bgt t2, t3, __stack_chk_guard_done
+ blt t2, t3, __stack_chk_guard_loop
+ and t1, t1, t4
+ slli t0, t0, 16
+ or t0, t0, t1
+ addi t5, t5, -2
+ bgtz t5, __stack_chk_guard_loop
+ lla t1, __stack_chk_guard
+ REG_S t0, 0(t1)
+ j __stack_chk_guard_done
+ .align 3
+__stack_chk_guard_done:
+
/* Setup temporary trap handler */
lla s4, _start_hang
csrw CSR_MTVEC, s4
--
2.47.3
More information about the opensbi
mailing list