Skip to contentSkip to conversation

firmware: fw_base: make stack guard setup conditional on Zkr

PATCH In review [email protected] 2 participants 2 messages Mar 31 – Apr 1

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: fw_base: make stack guard setup conditional on Zkr

From: Joel Bueno <[email protected]>

Zkr isn't explicitly selected as a mandatory extension to run OpenSBI as per: docs/platform_requirements.md

Since this happens very early on the boot flow of OpenSBI where the dynamic extension checks haven't yet happened, we must guard the Zkr stack setup behind an ifdef block so that it only gets compiled for targets that do actually have the extension.

---
 firmware/fw_base.S | 2 ++
 1 file changed, 2 insertions(+)
firmware/fw_base.S +2 -0
index 63bb4473..9ea93907 100644
@@ -107,6 +107,7 @@ _bss_zero:
 	add	s4, s4, __SIZEOF_POINTER__
 	blt	s4, s5, _bss_zero
 
+#if defined(__riscv_zkr)
 	/* Trying to initialize the stack guard via the Zkr extension */
 	lla	t0, __stack_chk_guard_done
 	csrw	CSR_MTVEC, t0
@@ -130,6 +131,7 @@ __stack_chk_guard_loop:
 	j	__stack_chk_guard_done
 	.align 3
 __stack_chk_guard_done:
+#endif
 
 	/* Setup temporary trap handler */
 	lla	s4, _start_hang
Signature · 7 lines
2.43.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

Hi Joel,

Thanks for the patch,

Quoted 28 lines · On 3/31/26 5:59 PM, [email protected] wrote:

From: Joel Bueno <[email protected]>

Zkr isn't explicitly selected as a mandatory extension to run OpenSBI as per: docs/platform_requirements.md

Since this happens very early on the boot flow of OpenSBI where the dynamic extension checks haven't yet happened, we must guard the Zkr stack setup behind an ifdef block so that it only gets compiled for targets that do actually have the extension.

---
  firmware/fw_base.S | 2 ++
  1 file changed, 2 insertions(+)
firmware/fw_base.S +1 -0
index 63bb4473..9ea93907 100644
@@ -107,6 +107,7 @@ _bss_zero:
  	add	s4, s4, __SIZEOF_POINTER__
  	blt	s4, s5, _bss_zero
 
+#if defined(__riscv_zkr)
  	/* Trying to initialize the stack guard via the Zkr extension */
  	lla	t0, __stack_chk_guard_done
  	csrw	CSR_MTVEC, t0

firmware/fw_base.S __stack_chk_guard_loop: · line 134

Quoted 4 lines
firmware/fw_base.S +1 -0
@@ -130,6 +131,7 @@ __stack_chk_guard_loop:
  	j	__stack_chk_guard_done
  	.align 3
  __stack_chk_guard_done:
+#endif

The current trap-based detection mechanism is intentional and works correctly on systems both with and without Zkr.
Using #ifdef guards would break binary portability, e.g. using single binary on heterogeneous systems (e.g., SMP with mixed Zkr/non-Zkr cores)

Regards,
Peter Lin

Quoted 3 lines
firmware/fw_base.S +0 -0
  	/* Setup temporary trap handler */
  	lla	s4, _start_hang
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi