Skip to contentSkip to conversation

lib: sbi: Configure seed bits when MSECCFG is readable

PATCH New Samuel Ortiz 3 participants 3 messages Jul 12, 2023 – Jul 13, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi: Configure seed bits when MSECCFG is readable

When MSECCFFG is not trapped, giving exclusive access to the SEED CSR to S-Mode (SSEED=1, USEED=0) seems like a reasonable default. It gives the Linux kernel the ability to add entropy to its randomness pool while preventing user mode from accessing it.

Eventually, this check will be enhanced with a check for the Zkr extensions through the riscv,isa-extensions dt-bindings.

---
 include/sbi/riscv_encoding.h |  5 +++++
 lib/sbi/sbi_hart.c           | 13 +++++++++++++
 2 files changed, 18 insertions(+)
include/sbi/riscv_encoding.h +5 -0
index 4ebed97..6a66d32 100644
@@ -223,6 +223,9 @@
 #define ENVCFG_CBIE_INV			_UL(0x3)
 #define ENVCFG_FIOM			_UL(0x1)
 
+#define SECCFG_SSEED			(_ULL(1) << 9)
+#define SECCFG_USEED			(_ULL(1) << 8)
+
 /* ===== User-level CSRs ===== */
 
 /* User Trap Setup (N-extension) */
@@ -444,6 +447,8 @@
 /* Machine Configuration */
 #define CSR_MENVCFG			0x30a
 #define CSR_MENVCFGH			0x31a
+#define CSR_MSECCFG			0x747
+#define CSR_MSECCFGH			0x757
 
 /* Machine Trap Handling */
 #define CSR_MSCRATCH			0x340
lib/sbi/sbi_hart.c +13 -0
index 0c27fd7..39af37c 100644
@@ -703,6 +703,19 @@ __mhpm_skip:
 					SBI_HART_EXT_SMSTATEEN, true);
 	}
 
+	if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_11) {
+		val = csr_read_allowed(CSR_MSECCFG, (unsigned long)&trap);
+		if (!trap.cause) {
+			/* Disable unprivileged access to the SEED CSR */
+			val &= ~SECCFG_USEED;
+
+			/* Enable S-Mode access to the SEED CSR */
+			val |= SECCFG_SSEED;
+
+			csr_write(CSR_MSECCFG, val);
+		}
+	}
+
 	/* Let platform populate extensions */
 	rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(),
 					  hfeatures);

base-commit: ea6533ada828cf71b9359f566be0f9c640022543

Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/1 lib: sbi: Configure seed bits when MSECCFG is readable

? 2023-07-12???? 10:32 +0200?Samuel Ortiz???

Quoted 50 lines

When MSECCFFG is not trapped, giving exclusive access to the SEED CSR to S-Mode (SSEED=1, USEED=0) seems like a reasonable default. It gives the Linux kernel the ability to add entropy to its randomness pool while preventing user mode from accessing it.

Eventually, this check will be enhanced with a check for the Zkr extensions through the riscv,isa-extensions dt-bindings.

---
?include/sbi/riscv_encoding.h |? 5 +++++
?lib/sbi/sbi_hart.c?????????? | 13 +++++++++++++
?2 files changed, 18 insertions(+)

include/sbi/riscv_encoding.h +0 -0
index 4ebed97..6a66d32 100644
@@ -223,6 +223,9 @@
 ?#define ENVCFG_CBIE_INV????????????????????????_UL(0x3)
 ?#define ENVCFG_FIOM????????????????????_UL(0x1)

?

include/sbi/riscv_encoding.h +3 -0
+#define SECCFG_SSEED???????????????????(_ULL(1) << 9)
+#define SECCFG_USEED???????????????????(_ULL(1) << 8)
+

?/* ===== User-level CSRs ===== */
?
?/* User Trap Setup (N-extension) */

include/sbi/riscv_encoding.h +0 -0
@@ -444,6 +447,8 @@

?/* Machine Configuration */
?#define CSR_MENVCFG????????????????????0x30a
?#define CSR_MENVCFGH???????????????????0x31a

include/sbi/riscv_encoding.h +2 -0
+#define CSR_MSECCFG????????????????????0x747
+#define CSR_MSECCFGH???????????????????0x757

?
?/* Machine Trap Handling */
?#define CSR_MSCRATCH???????????????????0x340

lib/sbi/sbi_hart.c +0 -0
index 0c27fd7..39af37c 100644
@@ -703,6 +703,19 @@ __mhpm_skip:
 ????????????????????????????????????????SBI_HART_EXT_SMSTATEEN, true);
 ????????}

?
+???????if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_11) { +???????????????val = csr_read_allowed(CSR_MSECCFG, (unsigned long)&trap); +???????????????if (!trap.cause) {
+???????????????????????/* Disable unprivileged access to the SEED CSR */ +???????????????????????val &= ~SECCFG_USEED;

lib/sbi/sbi_hart.c

Quoted 5 lines
lib/sbi/sbi_hart.c +5 -0
+
+???????????????????????/* Enable S-Mode access to the SEED CSR */
+???????????????????????val |= SECCFG_SSEED;
+
+???????????????????????csr_write(CSR_MSECCFG, val);

add SBI_HART_EXT_ZKR to sbi_hart_extension_id2string/sbi_hart_extensions

Wait for "lib: utils: Add detector of Smepmp from ISA string in FDT" to merge, then add Zkr's detection

Regards,
Xiang W

Quoted 12 lines

+???????????????}
+???????}
+
????????/* Let platform populate extensions */
????????rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(), ????????????????????????????????????????? hfeatures);

base-commit: ea6533ada828cf71b9359f566be0f9c640022543

Signature · 1 line
2.41.0
#2 lore raw

PATCH 1/1 lib: sbi: Configure seed bits when MSECCFG is readable

In reply to Samuel Ortiz
Quoted 10 lines · On Wed, Jul 12, 2023 at 2:03?PM Samuel Ortiz <[email protected]> wrote:

When MSECCFFG is not trapped, giving exclusive access to the SEED CSR to S-Mode (SSEED=1, USEED=0) seems like a reasonable default. It gives the Linux kernel the ability to add entropy to its randomness pool while preventing user mode from accessing it.

Eventually, this check will be enhanced with a check for the Zkr extensions through the riscv,isa-extensions dt-bindings.

Latest OpenSBI has ISA string parsing which can be used to detect more extensions. You can even enhance this to support the new DT bindings as well.

Quoted 33 lines
---
 include/sbi/riscv_encoding.h |  5 +++++
 lib/sbi/sbi_hart.c           | 13 +++++++++++++
 2 files changed, 18 insertions(+)
include/sbi/riscv_encoding.h +5 -0
index 4ebed97..6a66d32 100644
@@ -223,6 +223,9 @@
 #define ENVCFG_CBIE_INV                        _UL(0x3)
 #define ENVCFG_FIOM                    _UL(0x1)
 
+#define SECCFG_SSEED                   (_ULL(1) << 9)
+#define SECCFG_USEED                   (_ULL(1) << 8)
+
 /* ===== User-level CSRs ===== */
 
 /* User Trap Setup (N-extension) */
@@ -444,6 +447,8 @@
 /* Machine Configuration */
 #define CSR_MENVCFG                    0x30a
 #define CSR_MENVCFGH                   0x31a
+#define CSR_MSECCFG                    0x747
+#define CSR_MSECCFGH                   0x757
 
 /* Machine Trap Handling */
 #define CSR_MSCRATCH                   0x340
lib/sbi/sbi_hart.c +0 -0
index 0c27fd7..39af37c 100644

lib/sbi/sbi_hart.c __mhpm_skip: · line 706

Quoted 4 lines
lib/sbi/sbi_hart.c +1 -0
@@ -703,6 +703,19 @@ __mhpm_skip:
                                        SBI_HART_EXT_SMSTATEEN, true);
        }
 
+       if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_11) {

This should be >= SBI_HART_PRIV_VER_1_12 since mseccfg CSR was introduced with Priv v1.12

lib/sbi/sbi_hart.c __mhpm_skip: · line 715

Quoted 9 lines
lib/sbi/sbi_hart.c +9 -0
+               val = csr_read_allowed(CSR_MSECCFG, (unsigned long)&trap);
+               if (!trap.cause) {
+                       /* Disable unprivileged access to the SEED CSR */
+                       val &= ~SECCFG_USEED;
+
+                       /* Enable S-Mode access to the SEED CSR */
+                       val |= SECCFG_SSEED;
+
+                       csr_write(CSR_MSECCFG, val);

Over here, we should only detect seed CSR (i.e. Zksed extension) while the mseccfg CSR should be programmed in mstatus_init().

Quoted 12 lines
lib/sbi/sbi_hart.c +3 -0
+               }
+       }
+
        /* Let platform populate extensions */
        rc = sbi_platform_extensions_init(sbi_platform_thishart_ptr(),
                                          hfeatures);

base-commit: ea6533ada828cf71b9359f566be0f9c640022543 --
2.41.0

Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup