From: Xiang W <wxjstz@126.com>
To: opensbi@lists.infradead.org
Cc: Xiang W <wxjstz@126.com>, anup@brainfault.org
Subject: [PATCH v3 1/1] firmware: Initialize stack guard via Zkr
Date: Sun, 4 Jan 2026 12:07:23 +0800 [thread overview]
Message-ID: <20260104040726.106760-1-wxjstz@126.com> (raw)
Try to initialize stack protection guard via the zkr extension.
Signed-off-by: Xiang W <wxjstz@126.com>
---
firmware/fw_base.S | 24 ++++++++++++++++++++++++
include/sbi/riscv_encoding.h | 10 ++++++++++
2 files changed, 34 insertions(+)
diff --git a/firmware/fw_base.S b/firmware/fw_base.S
index bce9e226..16e2e283 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, SEED_OPTS_ES16
+ li t4, SEED_ENTROPY_MASK
+ li t5, __SIZEOF_POINTER__
+__stack_chk_guard_loop:
+ csrrw t1, CSR_SEED, x0
+ li t2, SEED_OPTS_MASK
+ andi t2, t2, t1
+ bgtu t2, t3, __stack_chk_guard_done
+ bltu 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
diff --git a/include/sbi/riscv_encoding.h b/include/sbi/riscv_encoding.h
index b5a4ce81..3c1d5256 100644
--- a/include/sbi/riscv_encoding.h
+++ b/include/sbi/riscv_encoding.h
@@ -261,6 +261,16 @@
#define CSR_FRM 0x002
#define CSR_FCSR 0x003
+/* User entropy source */
+#define CSR_SEED 0x015
+#define SEED_OPTS_SHIFT 30
+#define SEED_OPTS_MASK (_UL(3) << SEED_OPTS_SHIFT)
+#define SEED_OPTS_BIST (_UL(0) << SEED_OPTS_SHIFT)
+#define SEED_OPTS_WAIT (_UL(1) << SEED_OPTS_SHIFT)
+#define SEED_OPTS_ES16 (_UL(2) << SEED_OPTS_SHIFT)
+#define SEED_OPTS_DEAD (_UL(3) << SEED_OPTS_SHIFT)
+#define SEED_ENTROPY_MASK 0xffff
+
/* User Counters/Timers */
#define CSR_CYCLE 0xc00
#define CSR_TIME 0xc01
--
2.47.3
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
next reply other threads:[~2026-01-04 4:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-04 4:07 Xiang W [this message]
2026-01-04 5:25 ` [PATCH v3 1/1] firmware: Initialize stack guard via Zkr Xiang W
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260104040726.106760-1-wxjstz@126.com \
--to=wxjstz@126.com \
--cc=anup@brainfault.org \
--cc=opensbi@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.