Even though the mseccfgh CSR is a read-only zero the spec requires its
existence for RV32.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4680
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
 target/riscv/tcg/csr.c                  | 32 +++++++++++++++++++++++++
 tests/tcg/riscv32/test-mseccfg-bits-1.S | 15 +++++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..f0b59f5f94 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -805,6 +805,15 @@ static RISCVException have_mseccfg(CPURISCVState *env, int 
csrno)
     return RISCV_EXCP_ILLEGAL_INST;
 }
 
+static RISCVException have_mseccfgh(CPURISCVState *env, int csrno)
+{
+    if (riscv_cpu_mxl(env) != MXL_RV32) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    return have_mseccfg(env, csrno);
+}
+
 static RISCVException debug(CPURISCVState *env, int csrno)
 {
     if (riscv_cpu_cfg(env)->debug || riscv_cpu_cfg(env)->ext_sdtrig) {
@@ -5612,6 +5621,26 @@ static RISCVException write_mseccfg(CPURISCVState *env, 
int csrno,
     return RISCV_EXCP_NONE;
 }
 
+static RISCVException read_mseccfgh(CPURISCVState *env, int csrno,
+                                    target_ulong *val)
+{
+    /*
+     * The higher half of mseccfg has only PMM.  The spec says:
+     *  "The PMM field is read-only zero for RV32".
+     * Ergo mseccfgh is always 0.
+     */
+    *val = 0;
+
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_mseccfgh(CPURISCVState *env, int csrno,
+                                     target_ulong val, uintptr_t ra)
+{
+    /* See read_mseccfgh().  */
+    return RISCV_EXCP_NONE;
+}
+
 static RISCVException read_pmpcfg(CPURISCVState *env, int csrno,
                                   target_ulong *val)
 {
@@ -6508,6 +6537,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
     /* Physical Memory Protection */
     [CSR_MSECCFG]    = { "mseccfg",   have_mseccfg, read_mseccfg, 
write_mseccfg,
                          .min_priv_ver = PRIV_VERSION_1_11_0           },
+    [CSR_MSECCFGH]    = { "mseccfgh", have_mseccfgh,
+                          read_mseccfgh, write_mseccfgh,
+                          .min_priv_ver = PRIV_VERSION_1_11_0           },
     [CSR_PMPCFG0]    = { "pmpcfg0",   pmp, read_pmpcfg,  write_pmpcfg  },
     [CSR_PMPCFG1]    = { "pmpcfg1",   pmp, read_pmpcfg,  write_pmpcfg  },
     [CSR_PMPCFG2]    = { "pmpcfg2",   pmp, read_pmpcfg,  write_pmpcfg  },
diff --git a/tests/tcg/riscv32/test-mseccfg-bits-1.S 
b/tests/tcg/riscv32/test-mseccfg-bits-1.S
index ffa88f5f2f..01c29577e1 100644
--- a/tests/tcg/riscv32/test-mseccfg-bits-1.S
+++ b/tests/tcg/riscv32/test-mseccfg-bits-1.S
@@ -1,5 +1,6 @@
 /*
- * Verify mseccfg bits USEED, SSEED, MLPE and WPRI.
+ * Verify mseccfg bits USEED, SSEED, MLPE and WPRI.  Also verifies
+ * if mseccfgh exists and it's read-only zero.
  *
  * With "rv32,u=off,h=off,s=off,zkr=on,zicfilp=off" we expect no
  * bits being written in mseccfg.
@@ -11,6 +12,7 @@
  */
 
 #define CSR_MSECCFG 0x747
+#define CSR_MSECCFGH 0x757
 
 .option norvc
        .text
@@ -38,6 +40,17 @@ _start:
        andi    t3, t3, 7       // USEED, SSEED, MLPE -> bits 1, 2, 3
        bnez    t3, _fail
 
+       /*
+        * mseccfgh reads the higher half of mseccfg, which contains
+        * only mseccfg.PMM.  This bit is read-only zero in RV32,
+        * thus mseccfgh must be always zeroed.  Write a full 1s
+        * mask in it and verify.
+        */
+       li      t0, -1
+       csrw    CSR_MSECCFGH, t0
+       csrr    t0, CSR_MSECCFGH
+       bnez    t0, _fail
+
        j       _pass
 
 trap:
-- 
2.43.0


Reply via email to