diff mbox series

firmware: Initialize stack guard via Zkr

Message ID 330f2eb8d65ea7abfb4442ecdffef308855aed73.camel@126.com
State Changes Requested
Headers show
Series firmware: Initialize stack guard via Zkr | expand

Commit Message

Xiang W Oct. 6, 2025, 8:03 a.m. UTC
Try to initialize stack protection guard via the zkr extension.

Signed-off-by: Xiang W <wxjstz@126.com>
---
 firmware/fw_base.S | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Comments

Anup Patel Nov. 4, 2025, 3:52 a.m. UTC | #1
On Mon, Oct 6, 2025 at 1:33 PM Xiang W <wxjstz@126.com> wrote:
>
> Try to initialize stack protection guard via the zkr extension.
>
> Signed-off-by: Xiang W <wxjstz@126.com>

The patch subject is lacking the "[PATCH]" prefix otherwise
it looks good to me.

Reviewed-by: Anup Patel <anup@brainfault.org>

Regards,
Anup

> ---
>  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
>
diff mbox series

Patch

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