[PATCH] firmware: fw_base: make stack guard setup conditional on Zkr
Yu-Chine Peter Lin
peter.lin at sifive.com
Tue Mar 31 19:07:53 PDT 2026
Hi Joel,
Thanks for the patch,
On 3/31/26 5:59 PM, buenocalvachejoel at gmail.com wrote:
> From: Joel Bueno <buenocalvachejoel at gmail.com>
>
> 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.
>
> Signed-off-by: Joel Bueno <buenocalvachejoel at gmail.com>
> ---
> firmware/fw_base.S | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/firmware/fw_base.S b/firmware/fw_base.S
> index 63bb4473..9ea93907 100644
> --- a/firmware/fw_base.S
> +++ b/firmware/fw_base.S
> @@ -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
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
>
> /* Setup temporary trap handler */
> lla s4, _start_hang
More information about the opensbi
mailing list