Skip to contentSkip to conversation

Reset more security-related CSRs

PATCH Applied Radim Krčmář 2 participants 10 messages Apr 29, 2025 – Apr 30, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

v2 cover Reset more security-related CSRs

Hello,

all patches are marked as reviewed by Anup after implementing his suggestions.

v2: [1/8] Remove redundant newline. [Anup]

    [7/8] Improve comments. [Anup]
    [8/8] Remove pointless Smstateen check. [Anup]
    [1-8/8] Add Reviewed-by [Anup]

v1: https://lore.kernel.org/opensbi/[email protected]/

Radim Krčmář (8):
lib: sbi: add 64 bit csr macros
lib: sbi: use 64 bit csr macros
lib: sbi_hart: reset hstatus
lib: sbi_hart: reset sstateen and hstateen
lib: sbi_hart: fix sstateen emulation
lib: sbi_hart: reset mstateen0
lib: sbi_hart: add Ssstateen extension
lib: sbi_hart: properly reset Ssstateen

 include/sbi/riscv_asm.h | 20 ++++++++++++++++++
 include/sbi/sbi_hart.h  |  4 +++-
 lib/sbi/sbi_hart.c      | 45 ++++++++++++++++++++++++++---------------
 lib/sbi/sbi_timer.c     |  7 +------
 4 files changed, 53 insertions(+), 23 deletions(-)
Signature · 7 lines
2.48.1


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

PATCH v2 1/8 lib: sbi: add 64 bit csr macros

Most CSRs are XLEN bits wide, but some are 64 bit, so rv32 needs two accesses, plaguing the code with ifdefs.

Add new helpers that split 64 bit operation into two operations on rv32.

The helpers don't use "csr + 0x10", but append "H" at the end of the csr name to get a compile-time error when accessing a non 64 bit register. This has the downside that you have to use the name when accessing them. e.g. csr_read64(0x1234) or csr_read64(CSR_SATP) won't compile and the error messages you get for these bugs are not straightforward.

---
v2:
* Remove redundant newline. [Anup]
* Add Reviewed-by [Anup]

---
 include/sbi/riscv_asm.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
include/sbi/riscv_asm.h +20 -0
index 4605db20bd7a..ef48dc89adf1 100644
@@ -156,6 +156,26 @@
 				     : "memory");                  \
 	})
 
+#if __riscv_xlen == 64
+#define __csrrw64(op, csr, csrh, val) (true ? op(csr, val) : (uint64_t)csrh)
+#define __csrr64( op, csr, csrh)      (true ? op(csr)      : (uint64_t)csrh)
+#define __csrw64( op, csr, csrh, val) (true ? op(csr, val) : (uint64_t)csrh)
+#elif __riscv_xlen == 32
+#define __csrrw64(op, csr, csrh, val) (  op(csr, val) | (uint64_t)op(csrh, val >> 32) << 32)
+#define __csrr64( op, csr, csrh)      (  op(csr)      | (uint64_t)op(csrh)            << 32)
+#define __csrw64( op, csr, csrh, val) ({ op(csr, val);            op(csrh, val >> 32);    })
+#endif
+
+#define csr_swap64(        csr, val) __csrrw64(csr_swap,         csr, csr ## H, val)
+#define csr_read64(        csr)      __csrr64 (csr_read,         csr, csr ## H)
+#define csr_read_relaxed64(csr)      __csrr64 (csr_read_relaxed, csr, csr ## H)
+#define csr_write64(       csr, val) __csrw64 (csr_write,        csr, csr ## H, val)
+#define csr_read_set64(    csr, val) __csrrw64(csr_read_set,     csr, csr ## H, val)
+#define csr_set64(         csr, val) __csrw64 (csr_set,          csr, csr ## H, val)
+#define csr_clear64(       csr, val) __csrw64 (csr_clear,        csr, csr ## H, val)
+#define csr_read_clear64(  csr, val) __csrrw64(csr_read_clear,   csr, csr ## H, val)
+#define csr_clear64(       csr, val) __csrw64 (csr_clear,        csr, csr ## H, val)
+
 unsigned long csr_read_num(int csr_num);
 
 void csr_write_num(int csr_num, unsigned long val);
Signature · 7 lines
2.48.1


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

PATCH v2 2/8 lib: sbi: use 64 bit csr macros

In reply to Radim Krčmář

Switch the most obvious cases to new macros.

---
v2: Add Reviewed-by [Anup]

---
 lib/sbi/sbi_hart.c  | 20 ++++----------------
 lib/sbi/sbi_timer.c |  7 +------
 2 files changed, 5 insertions(+), 22 deletions(-)
lib/sbi/sbi_hart.c +4 -16
index eefb03f2b9ba..26d746d31dfd 100644
@@ -86,10 +86,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
 	}
 
 	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMSTATEEN)) {
-		mstateen_val = csr_read(CSR_MSTATEEN0);
-#if __riscv_xlen == 32
-		mstateen_val |= ((uint64_t)csr_read(CSR_MSTATEEN0H)) << 32;
-#endif
+		mstateen_val = csr_read64(CSR_MSTATEEN0);
 		mstateen_val |= SMSTATEEN_STATEN;
 		mstateen_val |= SMSTATEEN0_CONTEXT;
 		mstateen_val |= SMSTATEEN0_HSENVCFG;
@@ -110,17 +107,11 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		else
 			mstateen_val &= ~SMSTATEEN0_CTR;
 
-		csr_write(CSR_MSTATEEN0, mstateen_val);
-#if __riscv_xlen == 32
-		csr_write(CSR_MSTATEEN0H, mstateen_val >> 32);
-#endif
+		csr_write64(CSR_MSTATEEN0, mstateen_val);
 	}
 
 	if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_12) {
-		menvcfg_val = csr_read(CSR_MENVCFG);
-#if __riscv_xlen == 32
-		menvcfg_val |= ((uint64_t)csr_read(CSR_MENVCFGH)) << 32;
-#endif
+		menvcfg_val = csr_read64(CSR_MENVCFG);
 
 		/* Disable double trap by default */
 		menvcfg_val &= ~ENVCFG_DTE;
@@ -156,10 +147,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SVADE))
 			menvcfg_val &= ~ENVCFG_ADUE;
 
-		csr_write(CSR_MENVCFG, menvcfg_val);
-#if __riscv_xlen == 32
-		csr_write(CSR_MENVCFGH, menvcfg_val >> 32);
-#endif
+		csr_write64(CSR_MENVCFG, menvcfg_val);
 
 		/* Enable S-mode access to seed CSR */
 		if (sbi_hart_has_extension(scratch, SBI_HART_EXT_ZKR)) {
lib/sbi/sbi_timer.c +1 -6
index 86e0db5ab399..998a9a67c2f0 100644
@@ -139,12 +139,7 @@ void sbi_timer_event_start(u64 next_event)
 	 * the older software to leverage sstc extension on newer hardware.
 	 */
 	if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(), SBI_HART_EXT_SSTC)) {
-#if __riscv_xlen == 32
-		csr_write(CSR_STIMECMP, next_event & 0xFFFFFFFF);
-		csr_write(CSR_STIMECMPH, next_event >> 32);
-#else
-		csr_write(CSR_STIMECMP, next_event);
-#endif
+		csr_write64(CSR_STIMECMP, next_event);
 	} else if (timer_dev && timer_dev->timer_event_start) {
 		timer_dev->timer_event_start(next_event);
 		csr_clear(CSR_MIP, MIP_STIP);
Signature · 7 lines
2.48.1


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

PATCH v2 3/8 lib: sbi_hart: reset hstatus

In reply to Radim Krčmář

hstatus.HU must be cleared, because U-mode could otherwise use the HLS/HSV instructions. This would allow U-mode to read physical memory directly if vgatp and vsatp was 0.

The remaining fields don't seem like a security vulnerability now, but clearing the whole CSR is not an issue, so do that be safe.

---
v2: Add Reviewed-by [Anup]

---
 lib/sbi/sbi_hart.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_hart.c +3 -0
index 26d746d31dfd..5ea7328a18c9 100644
@@ -85,6 +85,9 @@ static void mstatus_init(struct sbi_scratch *scratch)
 #endif
 	}
 
+	if (misa_extension('H'))
+		csr_write(CSR_HSTATUS, 0);
+
 	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMSTATEEN)) {
 		mstateen_val = csr_read64(CSR_MSTATEEN0);
 		mstateen_val |= SMSTATEEN_STATEN;
Signature · 7 lines
2.48.1


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

PATCH v2 4/8 lib: sbi_hart: reset sstateen and hstateen

In reply to Radim Krčmář

Not resetting sstateen is a potential security hole, because U might be able to access state that S does not properly context-switch. Similar for hstateen with VS and HS.

---
v2: Add Reviewed-by [Anup]

---
 lib/sbi/sbi_hart.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/sbi/sbi_hart.c +6 -0
index 5ea7328a18c9..bdf66ef767ed 100644
@@ -111,6 +111,12 @@ static void mstatus_init(struct sbi_scratch *scratch)
 			mstateen_val &= ~SMSTATEEN0_CTR;
 
 		csr_write64(CSR_MSTATEEN0, mstateen_val);
+
+		if (misa_extension('S'))
+			csr_write(CSR_SSTATEEN0, 0);
+
+		if (misa_extension('H'))
+			csr_write64(CSR_HSTATEEN0, (uint64_t)0);
 	}
 
 	if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_12) {
Signature · 7 lines
2.48.1


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

PATCH v2 5/8 lib: sbi_hart: fix sstateen emulation

In reply to Radim Krčmář

The Sstateen extension defines 4 sstateen registers, but SBI currently configures the execution environment to throw illegal instruction exception when accessing sstateen1-3.

SBI should implement all sstateen registers, so delegate the implementation to hardware by setting the SE bit.

---
v2: Add Reviewed-by [Anup]

---
 lib/sbi/sbi_hart.c | 3 +++
 1 file changed, 3 insertions(+)
lib/sbi/sbi_hart.c +3 -0
index bdf66ef767ed..fc37b249b892 100644
@@ -111,6 +111,9 @@ static void mstatus_init(struct sbi_scratch *scratch)
 			mstateen_val &= ~SMSTATEEN0_CTR;
 
 		csr_write64(CSR_MSTATEEN0, mstateen_val);
+		csr_write64(CSR_MSTATEEN1, SMSTATEEN_STATEN);
+		csr_write64(CSR_MSTATEEN2, SMSTATEEN_STATEN);
+		csr_write64(CSR_MSTATEEN3, SMSTATEEN_STATEN);
 
 		if (misa_extension('S'))
 			csr_write(CSR_SSTATEEN0, 0);
Signature · 7 lines
2.48.1


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

PATCH v2 6/8 lib: sbi_hart: reset mstateen0

In reply to Radim Krčmář

The current logic clears some bits based on SBI known extensions. Be safe and do not leave enabled anything that SBI doesn't control.

This is not a breaking change, because the register must be initialized to 0 by the ISA on reset, but it is better to not depend on it when we don't need to.

---
v2: Add Reviewed-by [Anup]

---
 lib/sbi/sbi_hart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_hart.c +1 -1
index fc37b249b892..c6a779490c70 100644
@@ -89,7 +89,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		csr_write(CSR_HSTATUS, 0);
 
 	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMSTATEEN)) {
-		mstateen_val = csr_read64(CSR_MSTATEEN0);
+		mstateen_val = 0;
 		mstateen_val |= SMSTATEEN_STATEN;
 		mstateen_val |= SMSTATEEN0_CONTEXT;
 		mstateen_val |= SMSTATEEN0_HSENVCFG;
Signature · 7 lines
2.48.1


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

PATCH v2 7/8 lib: sbi_hart: add Ssstateen extension

In reply to Radim Krčmář

We already detect Smstateen, but Ssstateen exists as well and it doesn't have the M-state CSRs.

---
v2:
* Improve comments. [Anup]
* Add Reviewed-by [Anup]

---
 include/sbi/sbi_hart.h | 4 +++-
 lib/sbi/sbi_hart.c     | 4 ++++
 2 files changed, 7 insertions(+), 1 deletion(-)
include/sbi/sbi_hart.h +3 -1
index c3a7feb753d9..bce88c5984f4 100644
@@ -31,7 +31,7 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SMAIA = 0,
 	/** HART has Smepmp */
 	SBI_HART_EXT_SMEPMP,
-	/** HART has Smstateen CSR **/
+	/** HART has Smstateen extension **/
 	SBI_HART_EXT_SMSTATEEN,
 	/** Hart has Sscofpmt extension */
 	SBI_HART_EXT_SSCOFPMF,
@@ -79,6 +79,8 @@ enum sbi_hart_extensions {
 	SBI_HART_EXT_SMCTR,
 	/** HART has CTR S-mode CSRs */
 	SBI_HART_EXT_SSCTR,
+	/** HART has Ssstateen extension **/
+	SBI_HART_EXT_SSSTATEEN,
 
 	/** Maximum index of Hart extension */
 	SBI_HART_EXT_MAX,
lib/sbi/sbi_hart.c +4 -0
index c6a779490c70..8dc61d6ff230 100644
@@ -695,6 +695,7 @@ const struct sbi_hart_ext_data sbi_hart_ext[] = {
 	__SBI_HART_EXT_DATA(ssdbltrp, SBI_HART_EXT_SSDBLTRP),
 	__SBI_HART_EXT_DATA(smctr, SBI_HART_EXT_SMCTR),
 	__SBI_HART_EXT_DATA(ssctr, SBI_HART_EXT_SSCTR),
+	__SBI_HART_EXT_DATA(ssstateen, SBI_HART_EXT_SSSTATEEN),
 };
 
 _Static_assert(SBI_HART_EXT_MAX == array_size(sbi_hart_ext),
@@ -940,6 +941,9 @@ __pmp_skip:
 	/* Detect if hart supports mstateen CSRs */
 	__check_ext_csr(SBI_HART_PRIV_VER_1_12,
 			CSR_MSTATEEN0, SBI_HART_EXT_SMSTATEEN);
+	/* Detect if hart supports sstateen CSRs */
+	__check_ext_csr(SBI_HART_PRIV_VER_1_12,
+			CSR_SSTATEEN0, SBI_HART_EXT_SSSTATEEN);
 	/* Detect if hart supports smcntrpmf */
 	__check_ext_csr(SBI_HART_PRIV_VER_1_12,
 			CSR_MCYCLECFG, SBI_HART_EXT_SMCNTRPMF);
Signature · 7 lines
2.48.1


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

PATCH v2 8/8 lib: sbi_hart: properly reset Ssstateen

In reply to Radim Krčmář

sstateen* and hstateen* CSRs must be zeroed by M-mode if the mstateen* registers are missing, to avoid security issues.

---
v2:
* Remove pointless Smstateen check. [Anup]
* Add Reviewed-by [Anup]

---
 lib/sbi/sbi_hart.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
lib/sbi/sbi_hart.c +12 -3
index 8dc61d6ff230..fc4925b57625 100644
@@ -114,12 +114,21 @@ static void mstatus_init(struct sbi_scratch *scratch)
 		csr_write64(CSR_MSTATEEN1, SMSTATEEN_STATEN);
 		csr_write64(CSR_MSTATEEN2, SMSTATEEN_STATEN);
 		csr_write64(CSR_MSTATEEN3, SMSTATEEN_STATEN);
+	}
 
-		if (misa_extension('S'))
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSSTATEEN)) {
+		if (misa_extension('S')) {
 			csr_write(CSR_SSTATEEN0, 0);
-
-		if (misa_extension('H'))
+			csr_write(CSR_SSTATEEN1, 0);
+			csr_write(CSR_SSTATEEN2, 0);
+			csr_write(CSR_SSTATEEN3, 0);
+		}
+		if (misa_extension('H')) {
 			csr_write64(CSR_HSTATEEN0, (uint64_t)0);
+			csr_write64(CSR_HSTATEEN1, (uint64_t)0);
+			csr_write64(CSR_HSTATEEN2, (uint64_t)0);
+			csr_write64(CSR_HSTATEEN3, (uint64_t)0);
+		}
 	}
 
 	if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_12) {
Signature · 7 lines
2.48.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Applied
#9 lore raw
In reply to Radim Krčmář
Quoted 12 lines · Radim Krčmář

Hello,

all patches are marked as reviewed by Anup after implementing his suggestions.

v2: [1/8] Remove redundant newline. [Anup]

    [7/8] Improve comments. [Anup]
    [8/8] Remove pointless Smstateen check. [Anup]
    [1-8/8] Add Reviewed-by [Anup]

v1: https://lore.kernel.org/opensbi/[email protected]/

Quoted 9 lines

Radim Krčmář (8):
lib: sbi: add 64 bit csr macros
lib: sbi: use 64 bit csr macros
lib: sbi_hart: reset hstatus
lib: sbi_hart: reset sstateen and hstateen
lib: sbi_hart: fix sstateen emulation
lib: sbi_hart: reset mstateen0
lib: sbi_hart: add Ssstateen extension
lib: sbi_hart: properly reset Ssstateen

Applied this series to the riscv/opensbi repo.

Thanks,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi