Skip to contentSkip to conversation

target/riscv: gitlab fixes super pack

PATCH New Daniel Henrique Barboza 1 participant 16 messages Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover target/riscv: gitlab fixes super pack

Greetings,

This is a collection of gitlab fixes I've sent in the last 2 weeks or so. They're scattered around the mailing and, to my surprise, they can't be applied independently on top of alistair/riscv-to-apply.next without generating conflicts with one another, which is quite unpleasant to deal with.

I decided then to create a super pack and send them together, hopefully creating a better upstreaming experience for all of us.

The following Gitlab issues are fixed by this series:

https://gitlab.com/qemu-project/qemu/-/work_items/4821
https://gitlab.com/qemu-project/qemu/-/work_items/3526
https://gitlab.com/qemu-project/qemu/-/work_items/3133
https://gitlab.com/qemu-project/qemu/-/work_items/4592
https://gitlab.com/qemu-project/qemu/-/work_items/4591
https://gitlab.com/qemu-project/qemu/-/work_items/4593
https://gitlab.com/qemu-project/qemu/-/work_items/4596
https://gitlab.com/qemu-project/qemu/-/work_items/4216
https://gitlab.com/qemu-project/qemu/-/work_items/4826
https://gitlab.com/qemu-project/qemu/-/work_items/4811
https://gitlab.com/qemu-project/qemu/-/work_items/4488
https://gitlab.com/qemu-project/qemu/-/work_items/4827
https://gitlab.com/qemu-project/qemu/-/work_items/4818
https://gitlab.com/qemu-project/qemu/-/work_items/4680

Acks given for each individual fix were carried over. Only the latest version of each patches was included.

Patches based on alistair/riscv-to-apply.next.

Daniel Henrique Barboza (15):
target/riscv: do not write SXL < UXL
target/riscv: avoid amocas.* x0 clobbering target/riscv/tcg/csr: filter bits when r/w HVIP(H) target/riscv: fix hedeleg writable mask
target/riscv: clear hgatp bits from write mask target/riscv: allow hvip.VSTIP writes when (m|h)envcfg.STCE is set target/riscv: allow zicbo insns to run without RVS target/riscv: filter out reserved (m|h)envcfg.CBIE value target/riscv: sanitize mnstatus.MNPP field target/riscv: do not write RVG in misa CSR target/riscv: do not clear mstatush after mstatus write target/riscv: clear mstatus.MIE if mstatush.MDT is set target/riscv/tcg/pmp.c: remove no-op in mseccfg_csr_write() target/riscv/tcg: refactor mseccfg_csr_write() target/riscv/tcg/csr.c: add mseccfgh CSR

 target/riscv/cpu_bits.h                       |   9 +
 target/riscv/tcg/csr.c                        | 187 ++++++++++++++++--
 .../riscv/tcg/insn_trans/trans_rvzacas.c.inc  |  15 +-
 target/riscv/tcg/op_helper.c                  |   5 +-
 target/riscv/tcg/pmp.c                        |  62 +++---
 target/riscv/tcg/translate.c                  |   5 +-
 tests/tcg/riscv32/system/meson.build          |  30 +++
 tests/tcg/riscv32/test-mseccfg-bits-1.S       |  69 +++++++
 tests/tcg/riscv32/test-mseccfg-bits-2.S       |  56 ++++++
 tests/tcg/riscv32/test-mseccfg-bits-3.S       |  56 ++++++
 tests/tcg/riscv32/test-mseccfg-bits-4.S       |  55 ++++++
 tests/tcg/riscv32/test-mstatush-clear.S       |  57 ++++++
 tests/tcg/riscv32/test-mstatush-mdt-mie.S     |  55 ++++++
 tests/tcg/riscv64/system/meson.build          |  62 ++++++
 tests/tcg/riscv64/test-amocas-x0.S            |  49 +++++
 tests/tcg/riscv64/test-hedeleg-write.S        |  57 ++++++
 tests/tcg/riscv64/test-hgatp-rw.S             |  61 ++++++
 tests/tcg/riscv64/test-hvip-read.S            |  41 ++++
 tests/tcg/riscv64/test-hvip-vstip.S           |  65 ++++++
 tests/tcg/riscv64/test-menvcfg-cbie.S         |  82 ++++++++
 tests/tcg/riscv64/test-misa-G.S               |  51 +++++
 tests/tcg/riscv64/test-mseccfg-pmm-clear.S    |  63 ++++++
 tests/tcg/riscv64/test-mseccfg-pmm-reserved.S |  63 ++++++
 tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S |  75 +++++++
 tests/tcg/riscv64/test-uxl-sxl.S              |  59 ++++++
 tests/tcg/riscv64/test-zicbo-umode.S          |  98 +++++++++
 26 files changed, 1437 insertions(+), 50 deletions(-)
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-1.S
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-2.S
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-3.S
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-4.S
 create mode 100644 tests/tcg/riscv32/test-mstatush-clear.S
 create mode 100644 tests/tcg/riscv32/test-mstatush-mdt-mie.S
 create mode 100644 tests/tcg/riscv64/test-amocas-x0.S
 create mode 100644 tests/tcg/riscv64/test-hedeleg-write.S
 create mode 100644 tests/tcg/riscv64/test-hgatp-rw.S
 create mode 100644 tests/tcg/riscv64/test-hvip-read.S
 create mode 100644 tests/tcg/riscv64/test-hvip-vstip.S
 create mode 100644 tests/tcg/riscv64/test-menvcfg-cbie.S
 create mode 100644 tests/tcg/riscv64/test-misa-G.S
 create mode 100644 tests/tcg/riscv64/test-mseccfg-pmm-clear.S
 create mode 100644 tests/tcg/riscv64/test-mseccfg-pmm-reserved.S
 create mode 100644 tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S
 create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S
 create mode 100644 tests/tcg/riscv64/test-zicbo-umode.S
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/15 target/riscv: do not write SXL < UXL

RISC-V priv spec says: "If S-mode is implemented, the set of legal values that the uxl field may assume excludes those that would cause UXLEN to be greater than SXLEN.". At this moment there's no relation between SXL and UXL writes, i.e. we're happy to accept SXL < UXL.

We'll start by renaming riscv_write_uxl() to riscv_update_xl_field(), making it closer to what the function actually does. Then, in the same function:

- 'xl' is renamed to 'mlx' since it stores riscv_cpu_mxl();
- 'uxl' is renamed to 'xl', indicating that the function is dealing with multiple XL files (SXL, UXL, VSSTATUS.UXL ...);
- add a new 'min_field_val' field.

With this improved helper what's left is to change SXL/UXL update order in write_mstatus() and, when updating SXL, we'll use the written UXL value as a minimal value for SXL.

This means that if we're in a SXL < UXL case, e.g. SXL = 1 and UXL = 2, we'll write SXL = 2/UXL = 2. Another possibility would be to decrement UXL and write SXL = 1/UXL = 1, but the bug report in [1] mentions that bumping SXL is the default behavior of other emulators. For the sake of compatibility we're doing the same.

[1] https://gitlab.com/qemu-project/qemu/-/work_items/4821

Fixes ebef6685a1a2 ("target/riscv: use SXL instead of MXL for read_sstatus")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4821

---
 target/riscv/tcg/csr.c               | 47 +++++++++++++++-------
 tests/tcg/riscv64/system/meson.build |  7 ++++
 tests/tcg/riscv64/test-uxl-sxl.S     | 59 ++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 14 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S
target/riscv/tcg/csr.c +33 -14
index 2a37fb735f..0e2ef8530b 100644
@@ -2049,15 +2049,32 @@ static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp,
     return val;
 }
 
-static uint64_t riscv_write_uxl(CPURISCVState *env, uint64_t val,
-                                uint64_t field)
+/*
+ * The intended use of this function w.r.t 'min_field_val' based on
+ * our current design of always making SXL >= UXL:
+ *
+ * - set min_field_val = 0 when updating UXL fields;
+ * - set min_field_val = existing_UXL_val when updating SXL fields.
+ */
+static uint64_t riscv_update_xl_field(CPURISCVState *env, uint64_t val,
+                                      uint64_t field, uint32_t min_field_val)
 {
-    RISCVMXL xl = riscv_cpu_mxl(env);
-    uint64_t uxl = get_field(val, field);
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    uint64_t xl = get_field(val, field);
+    bool field_updated = false;
 
-    if (xl != MXL_RV128 && uxl == MXL_RV128) {
-        uxl = xl;
-        val = set_field(val, field, uxl);
+    if (xl < min_field_val) {
+        xl = min_field_val;
+        field_updated = true;
+    }
+
+    if (mxl != MXL_RV128 && xl == MXL_RV128) {
+        xl = mxl;
+        field_updated = true;
+    }
+
+    if (field_updated) {
+        val = set_field(val, field, xl);
     }
 
     return val;
@@ -2118,14 +2135,16 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
             mask |= MSTATUS_MPV | MSTATUS_GVA;
         }
 
-        if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) {
-            mask |= MSTATUS64_SXL;
-            val = riscv_write_uxl(env, val, MSTATUS64_SXL);
-        }
-
         if (riscv_has_ext(env, RVU) && (val & MSTATUS64_UXL) != 0) {
             mask |= MSTATUS64_UXL;
-            val = riscv_write_uxl(env, val, MSTATUS64_UXL);
+            val = riscv_update_xl_field(env, val, MSTATUS64_UXL, 0);
+        }
+
+        if (riscv_has_ext(env, RVS) && (val & MSTATUS64_SXL) != 0) {
+            uint32_t min_val = get_field(val, MSTATUS64_UXL);
+
+            mask |= MSTATUS64_SXL;
+            val = riscv_update_xl_field(env, val, MSTATUS64_SXL, min_val);
         }
     }
 
@@ -5509,7 +5528,7 @@ static RISCVException write_vsstatus(CPURISCVState *env, int csrno,
     if ((val & VSSTATUS64_UXL) == 0) {
         mask &= ~VSSTATUS64_UXL;
     } else {
-        val = riscv_write_uxl(env, val, VSSTATUS64_UXL);
+        val = riscv_update_xl_field(env, val, VSSTATUS64_UXL, 0);
     }
     if ((env->henvcfg & HENVCFG_DTE)) {
         if ((val & SSTATUS_SDT) != 0) {
tests/tcg/riscv64/system/meson.build +7 -0
index cdd2e99d22..c9435bf2b3 100644
@@ -116,6 +116,13 @@ tests += {
   }
 }
 
+tests += {
+  'test-uxl-sxl.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
tests/tcg/riscv64/test-uxl-sxl.S new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/15 target/riscv: avoid amocas.* x0 clobbering

In reply to Daniel Henrique Barboza

gen_cmpxchg* and trans_amocas_q() load amocas registers using get_gpr(). When the register is x0/zero get_gpr() returns ctx->zero, the shared TCG constant that every x0 read uses. As it is today, the amocas helpers uses tcg_gen_atomic_cmpxchg_* helpers using the dest register retrieved by get_gpr().

So, if we have something like:

amocas.d zero,s0,(a0)

gen_cmpxchg() uses ctx->zero as 'dest' for tcg_gen_atomic_cmpxchg_tl(), which will end up clobbering it with whatever is in 'rs1'. ctx->zero will be reset back to normal in init_disas_context(), but if there's something else that ends up reading it in the same TB, e.g.:

beqz a2,24

This will read a clobbered ctx->zero and then all bets are off.

The fix is to use a temp variable as 'dest' for gen_atomic_cmpxchg_* calls instead of the result of get_gpr(). We are already executing gen_set_grp() in all cases using 'dest', making this change a bit simpler.

Fixes b52d49e97fc4 ("target/riscv: Add support for Zacas extension")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3526

---
 .../riscv/tcg/insn_trans/trans_rvzacas.c.inc  | 15 ++++--
 target/riscv/tcg/translate.c                  |  5 +-
 tests/tcg/riscv64/system/meson.build          |  5 ++
 tests/tcg/riscv64/test-amocas-x0.S            | 49 +++++++++++++++++++
 4 files changed, 67 insertions(+), 7 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-amocas-x0.S
target/riscv/tcg/insn_trans/trans_rvzacas.c.inc +10 -5
index 79bca1e957..e908ed2377 100644
@@ -72,13 +72,14 @@ static bool gen_cmpxchg64(DisasContext *ctx, arg_atomic *a, MemOp mop)
         return false;
     }
 
-    TCGv_i64 dest = get_gpr_pair(ctx, a->rd);
+    TCGv_i64 cmp = get_gpr_pair(ctx, a->rd);
     TCGv src1 = get_address(ctx, a->rs1, 0);
     TCGv_i64 src2 = get_gpr_pair(ctx, a->rs2);
+    TCGv_i64 dest = tcg_temp_new_i64();
 
     mop |= ctx->mo_endianness;
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
-    tcg_gen_atomic_cmpxchg_i64(dest, src1, dest, src2, ctx->mem_idx, mop);
+    tcg_gen_atomic_cmpxchg_i64(dest, src1, cmp, src2, ctx->mem_idx, mop);
 
     gen_set_gpr_pair(ctx, a->rd, dest);
     return true;
@@ -117,15 +118,19 @@ static bool trans_amocas_q(DisasContext *ctx, arg_amocas_q *a)
     TCGv_i128 src2 = tcg_temp_new_i128();
     TCGv_i64 src2l = get_gpr(ctx, a->rs2, EXT_NONE);
     TCGv_i64 src2h = get_gpr(ctx, a->rs2 == 0 ? 0 : a->rs2 + 1, EXT_NONE);
-    TCGv_i64 destl = get_gpr(ctx, a->rd, EXT_NONE);
-    TCGv_i64 desth = get_gpr(ctx, a->rd == 0 ? 0 : a->rd + 1, EXT_NONE);
+    TCGv_i64 cmpl = get_gpr(ctx, a->rd, EXT_NONE);
+    TCGv_i64 cmph = get_gpr(ctx, a->rd == 0 ? 0 : a->rd + 1, EXT_NONE);
+    TCGv_i64 destl = tcg_temp_new_i64();
+    TCGv_i64 desth = tcg_temp_new_i64();
+    TCGv_i128 cmp = tcg_temp_new_i128();
     MemOp memop = MO_ALIGN | MO_UO;
 
     memop |= ctx->mo_endianness;
+    tcg_gen_concat_i64_i128(cmp, cmpl, cmph);
     tcg_gen_concat_i64_i128(src2, src2l, src2h);
     tcg_gen_concat_i64_i128(dest, destl, desth);
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
-    tcg_gen_atomic_cmpxchg_i128(dest, src1, dest, src2, ctx->mem_idx, memop);
+    tcg_gen_atomic_cmpxchg_i128(dest, src1, cmp, src2, ctx->mem_idx, memop);
 
     tcg_gen_extr_i128_i64(destl, desth, dest);
 
target/riscv/tcg/translate.c +3 -2
index 72d36764d8..fdf9c39bef 100644
@@ -1176,13 +1176,14 @@ static bool gen_amo(DisasContext *ctx, arg_atomic *a,
 
 static bool gen_cmpxchg(DisasContext *ctx, arg_atomic *a, MemOp mop)
 {
-    TCGv dest = get_gpr(ctx, a->rd, EXT_NONE);
+    TCGv cmp = get_gpr(ctx, a->rd, EXT_NONE);
     TCGv src1 = get_address(ctx, a->rs1, 0);
     TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
+    TCGv dest = tcg_temp_new();
 
     mop |= ctx->mo_endianness;
     decode_save_opc(ctx, RISCV_UW2_ALWAYS_STORE_AMO);
-    tcg_gen_atomic_cmpxchg_tl(dest, src1, dest, src2, ctx->mem_idx, mop);
+    tcg_gen_atomic_cmpxchg_tl(dest, src1, cmp, src2, ctx->mem_idx, mop);
 
     gen_set_gpr(ctx, a->rd, dest);
     return true;
tests/tcg/riscv64/system/meson.build +5 -0
index c9435bf2b3..6c3de32965 100644
@@ -121,6 +121,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-amocas-x0.S': {
+    'cflags': cflags + ['-march=rv64gc'],
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-amocas-x0.S new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/15 target/riscv/tcg/csr: filter bits when r/w HVIP(H)

In reply to Daniel Henrique Barboza

rmw_hvip64() is not filtering out the bits from mip/sip, hosted in the 'alias_mask' variable, when dealing with HVIP. The bug is rather straightforward. Given:

uint64_t alias_mask = (env->hideleg | ~env->hvien) | VS_MODE_INTERRUPTS;

when env->hvien is 0, alias_mask will be a full 1s mask. This mask is then used to retrieve the mip bits (via rmw_mip64) and then later when filtering ret_mip. The end result is that HVIP ends up providing read access to bits it can't write. The spec says:

"Bits of hvip that are not writable are read-only zeros."

So we're in a direct and trivial spec violation.

Fixes 40336d5b1d4c ("target/riscv: Add HS-mode virtual interrupt and IRQ filtering support.")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3133

---
 target/riscv/tcg/csr.c               |  8 ++++++
 tests/tcg/riscv64/system/meson.build |  5 ++++
 tests/tcg/riscv64/test-hvip-read.S   | 41 ++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-hvip-read.S
target/riscv/tcg/csr.c +8 -0
index 0e2ef8530b..c94ce88594 100644
@@ -5139,6 +5139,14 @@ static RISCVException rmw_hvip64(CPURISCVState *env, int csrno,
     uint64_t wr_mask_hvip;
     uint64_t wr_mask_mip;
 
+    /*
+     * alias_mask, aka the bits that comes from sip/mip, should be capped
+     * to just VS_MODE_INTERRUPTS when reading/writing HVIP.
+     */
+    if (csrno == CSR_HVIP || csrno == CSR_HVIPH) {
+        alias_mask = VS_MODE_INTERRUPTS;
+    }
+
     /*
      * Both alias and non-alias mask remain same for vsip except:
      *  1- For VS* bits if they are zero in hideleg.
tests/tcg/riscv64/system/meson.build +5 -0
index 6c3de32965..9754c2a55a 100644
@@ -126,6 +126,11 @@ tests += {
     'cflags': cflags + ['-march=rv64gc'],
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-hvip-read.S': {
+    'cflags': cflags + ['-march=rv64gcv'],
+    'qemu_args': ['-cpu', 'max,smpmpmt=false,pmu-mask=0xfffffff8,smdbltrp=true,ssdbltrp=true', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-hvip-read.S new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/15 target/riscv: fix hedeleg writable mask

In reply to Daniel Henrique Barboza

Bit 19 (hardware error) must be writable in hedeleg, i.e. it must be delegable. Add it DELEGABLE_EXCPS.

While we're at it, hedeleg bit 0 is only writable if IALIGN = 16, i.e. RVC is enabled. Remove bit 0 from the delegation/writable mask if RVC is enabled.

Fixes bc083a51caff ("target/riscv: hardwire bits in hideleg and hedeleg")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4592

---
 target/riscv/tcg/csr.c                 | 11 +++++
 tests/tcg/riscv64/system/meson.build   |  5 +++
 tests/tcg/riscv64/test-hedeleg-write.S | 57 ++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-hedeleg-write.S
target/riscv/tcg/csr.c +11 -0
index c94ce88594..e132690389 100644
@@ -1837,6 +1837,7 @@ static const uint64_t all_ints = M_MODE_INTERRUPTS | S_MODE_INTERRUPTS |
                          (1ULL << (RISCV_EXCP_LOAD_PAGE_FAULT)) | \
                          (1ULL << (RISCV_EXCP_STORE_PAGE_FAULT)) | \
                          (1ULL << (RISCV_EXCP_SW_CHECK)) | \
+                         (1ULL << (RISCV_EXCP_HW_ERR)) | \
                          (1ULL << (RISCV_EXCP_INST_GUEST_PAGE_FAULT)) | \
                          (1ULL << (RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT)) | \
                          (1ULL << (RISCV_EXCP_VIRT_INSTRUCTION_FAULT)) | \
@@ -4947,6 +4948,16 @@ static RISCVException write_hedeleg(CPURISCVState *env, int csrno,
                                     target_ulong val, uintptr_t ra)
 {
     env->hedeleg = val & vs_delegable_excps;
+
+    /*
+     * delegable_excps includes bit 0, which is writable only if
+     * IALIGN = 32 (no RVC present).  Ergo clear bit 0 if we have
+     * RVC.
+     */
+    if (riscv_has_ext(env, RVC)) {
+        env->hedeleg &= ~1ULL;
+    }
+
     return RISCV_EXCP_NONE;
 }
 
tests/tcg/riscv64/system/meson.build +5 -0
index 9754c2a55a..e176587d1d 100644
@@ -131,6 +131,11 @@ tests += {
     'cflags': cflags + ['-march=rv64gcv'],
     'qemu_args': ['-cpu', 'max,smpmpmt=false,pmu-mask=0xfffffff8,smdbltrp=true,ssdbltrp=true', qemu_args],
   },
+
+  'test-hedeleg-write.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-hedeleg-write.S new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/15 target/riscv: clear hgatp bits from write mask

In reply to Daniel Henrique Barboza

We're allowing writes on bits that the ISA demands to be zeroed.

In theory we could let PPN[1:0] be writable for Bare modes. In the 'hgatp' section the spec says:

"An implementation that supports only the defined paged virtual-memory schemes and/or Bare may make PPN[1:0] read-only zero."

This is our case since we only support sv32, sv39, sv48 and sv57 and Bare, thus let's make our lives easier and zero these bits all the time.

Fixes 1349f9695208 ("target/riscv: UPDATE xATP write CSR")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4591

---
 target/riscv/cpu_bits.h              |  9 ++++
 target/riscv/tcg/csr.c               |  8 ++++
 tests/tcg/riscv64/system/meson.build |  5 +++
 tests/tcg/riscv64/test-hgatp-rw.S    | 61 ++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-hgatp-rw.S
target/riscv/cpu_bits.h +9 -0
index 3579789a1f..dcd3f239fe 100644
@@ -744,6 +744,15 @@ typedef enum {
 #define SATP64_ASID         0x0FFFF00000000000ULL
 #define SATP64_PPN          0x00000FFFFFFFFFFFULL
 
+/*
+ * Priv spec, hgatp section: "An implementation that supports only the
+ * defined paged virtual-memory schemes and/or Bare may make PPN[1:0]
+ * read-only zero.".  This is our case so we're choosing to clear
+ * PPN[1:0] all the time.
+ */
+#define HGATP32_ZERO        (MAKE_64BIT_MASK(29, 2) | 0b11)
+#define HGATP64_ZERO        (MAKE_64BIT_MASK(58, 2) | 0b11)
+
 /* RNMI mnstatus CSR mask */
 #define MNSTATUS_NMIE       0x00000008
 #define MNSTATUS_MNPV       0x00000080
target/riscv/tcg/csr.c +8 -0
index e132690389..6989393bac 100644
@@ -5355,6 +5355,14 @@ static RISCVException write_hgatp(CPURISCVState *env, int csrno,
                                   target_ulong val, uintptr_t ra)
 {
     env->hgatp = legalize_xatp(env, env->hgatp, val);
+
+    /* Clear HGATP zero fields.  */
+    if (riscv_cpu_mxl(env) == MXL_RV32) {
+        env->hgatp &= ~HGATP32_ZERO;
+    } else {
+        env->hgatp &= ~HGATP64_ZERO;
+    }
+
     return RISCV_EXCP_NONE;
 }
 
tests/tcg/riscv64/system/meson.build +5 -0
index e176587d1d..9684628014 100644
@@ -136,6 +136,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-hgatp-rw.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-hgatp-rw.S new file +61 -0

61 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

PATCH 6/15 target/riscv: allow hvip.VSTIP writes when (m|h)envcfg.STCE is set

In reply to Daniel Henrique Barboza

Priv spec says in the hvip section: "Bits VSEIP, VSTIP, and VSSIP of hvip are writable.". There's no reservation about hvip.VSTIP being writable, i.e. this should be the case at all times.

The issue lies in rmw_mip64(). The function, used by multiple CSRs, is filtering out VSTIP based on the STCE bits being set.

Make an exception for hvip.VSTIP.

Fixes 3cb2edae7401 ("target/riscv: Fix VSTIP bit in sstc extension.")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4593

---
 target/riscv/tcg/csr.c               |  8 +++-
 tests/tcg/riscv64/system/meson.build |  5 +++
 tests/tcg/riscv64/test-hvip-vstip.S  | 65 ++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-hvip-vstip.S
target/riscv/tcg/csr.c +6 -2
index 6989393bac..a715f48e01 100644
@@ -4041,8 +4041,12 @@ static RISCVException rmw_mip64(CPURISCVState *env, int csrno,
         /* STIP is not writable when menvcfg.STCE is enabled. */
         mask = mask & ~MIP_STIP;
 
-        /* VSTIP is not writable when both [mh]envcfg.STCE are enabled. */
-        if (get_field(env->henvcfg, HENVCFG_STCE)) {
+        /*
+         * VSTIP is not writable when both [mh]envcfg.STCE are enabled.
+         * Note: VSTIP is writable via CSR_HVIP at all times.
+         */
+        if (csrno != CSR_HVIP
+            && get_field(env->henvcfg, HENVCFG_STCE)) {
             mask = mask & ~MIP_VSTIP;
         }
     }
tests/tcg/riscv64/system/meson.build +5 -0
index 9684628014..87a26389ee 100644
@@ -141,6 +141,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-hvip-vstip.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-hvip-vstip.S new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

PATCH 7/15 target/riscv: allow zicbo insns to run without RVS

In reply to Daniel Henrique Barboza

check_zicbo_envcfg() is validating access using S-mode constraints that are only relevant if we have RVS enabled in the hart. If we have a hart that has M-mode and U-mode, but not S mode, zicbo insns will always fault.

Execute the exclusive S-mode checks only if RVS is present.

Fixes a939c500793a ("target/riscv: implement Zicboz extension")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4596

---
 target/riscv/tcg/op_helper.c         |  5 +-
 tests/tcg/riscv64/system/meson.build |  5 ++
 tests/tcg/riscv64/test-zicbo-umode.S | 98 ++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/riscv64/test-zicbo-umode.S
target/riscv/tcg/op_helper.c +4 -1
index 52aa545eec..db4cea326a 100644
@@ -172,7 +172,10 @@ static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits,
         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra);
     }
 
-    if ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)) {
+    /* Skip S-mode exclusive checks if we don't have RVS.  */
+    if (riscv_has_ext(env, RVS)
+        && env->priv < PRV_S
+        && !get_field(env->senvcfg, envbits)) {
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
     }
 #endif
tests/tcg/riscv64/system/meson.build +5 -0
index 87a26389ee..fd52d1e4ec 100644
@@ -146,6 +146,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-zicbo-umode.S': {
+   'cflags': cflags + ['-march=rv64gcv_zicbom_zicboz'],
+    'qemu_args': ['-cpu', 'rv64,s=off,h=off,zicbom=on,zicboz=on', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-zicbo-umode.S new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
2.43.0
#8 lore raw

PATCH 8/15 target/riscv: filter out reserved (m|h)envcfg.CBIE value

In reply to Daniel Henrique Barboza

We can't allow CBIE = 10b to be written since it's marked as Reserved.

Fixes e979f6a15552 ("target/riscv: gate envcfg CBO fields on extensions")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4216

---
 target/riscv/tcg/csr.c                | 14 ++++-
 tests/tcg/riscv64/system/meson.build  |  5 ++
 tests/tcg/riscv64/test-menvcfg-cbie.S | 82 +++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-menvcfg-cbie.S
target/riscv/tcg/csr.c +12 -2
index a715f48e01..83eb44a590 100644
@@ -3431,7 +3431,12 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
 
     /* CBIE/CBCFE are read-only zero when Zicbom is not implemented. */
     if (cfg->ext_zicbom) {
-        mask |= MENVCFG_CBIE | MENVCFG_CBCFE;
+        mask |= MENVCFG_CBCFE;
+
+        /* CBIE = 0b10 is marked as Reserved.  */
+        if (get_field(val, MENVCFG_CBIE) != 0b10) {
+            mask |= MENVCFG_CBIE;
+        }
     }
 
     /* CBZE is read-only zero when Zicboz is not implemented. */
@@ -3613,7 +3618,12 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
 
     /* CBIE/CBCFE are read-only zero when Zicbom is not implemented. */
     if (cfg->ext_zicbom) {
-        mask |= HENVCFG_CBIE | HENVCFG_CBCFE;
+        mask |= HENVCFG_CBCFE;
+
+        /* CBIE = 0b10 is marked as Reserved.  */
+        if (get_field(val, HENVCFG_CBIE) != 0b10) {
+            mask |= HENVCFG_CBIE;
+        }
     }
 
     /* CBZE is read-only zero when Zicboz is not implemented. */
tests/tcg/riscv64/system/meson.build +5 -0
index fd52d1e4ec..be9d892a95 100644
@@ -151,6 +151,11 @@ tests += {
    'cflags': cflags + ['-march=rv64gcv_zicbom_zicboz'],
     'qemu_args': ['-cpu', 'rv64,s=off,h=off,zicbom=on,zicboz=on', qemu_args],
   },
+
+  'test-menvcfg-cbie.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-menvcfg-cbie.S new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

PATCH 9/15 target/riscv: sanitize mnstatus.MNPP field

In reply to Daniel Henrique Barboza

We need to handle mnstatus.MNPP the same way we already do with mstatus.MPP - do not allow the reserved value 10b to be written.

Fixes 5db557f82bff ("target/riscv: Add Smrnmi CSRs")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4826

---
 target/riscv/tcg/csr.c                        | 11 +++
 tests/tcg/riscv64/system/meson.build          |  5 ++
 tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S | 75 +++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S
target/riscv/tcg/csr.c +11 -0
index 83eb44a590..ab278a97bc 100644
@@ -2024,6 +2024,10 @@ static target_ulong legalize_xatp(CPURISCVState *env, target_ulong old_xatp,
     return old_xatp;
 }
 
+/*
+ * This function is also used to legalize mnstatus.MNPP since both
+ * fields are located in 0x1800 and uses the same encoding.
+ */
 static target_ulong legalize_mpp(CPURISCVState *env, target_ulong old_mpp,
                                  target_ulong val)
 {
@@ -5891,6 +5895,13 @@ static RISCVException write_mnstatus(CPURISCVState *env, int csrno,
 {
     target_ulong mask = (MNSTATUS_NMIE | MNSTATUS_MNPP);
 
+    /*
+     * MNPP uses the same encoding as mstatus.MPP, ergo we need to
+     * 'legalize' it the same way.  Since they map to the same bit
+     * field (0x1800) we'll just the same helper.
+     */
+    val = legalize_mpp(env, get_field(env->mnstatus, MNSTATUS_MNPP), val);
+
     if (riscv_has_ext(env, RVH)) {
         /* Flush tlb on mnstatus fields that affect VM. */
         if ((val ^ env->mnstatus) & MNSTATUS_MNPV) {
tests/tcg/riscv64/system/meson.build +5 -0
index be9d892a95..9cd62c27e9 100644
@@ -156,6 +156,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-mstatus-mnstatus-mpp.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max,smpmpmt=false,smdbltrp=true,ssdbltrp=true,smrnmi=true', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.43.0
#10 lore raw

PATCH 10/15 target/riscv: do not write RVG in misa CSR

In reply to Daniel Henrique Barboza

It seems like we can't write RVG into misa. The bit has been marked as "Reserved" for priv specs 1.11+, i.e. it does not represent the extension pack "IMAFD_Zicsr_Zifencei". The official explanation, at least from what I could find, is that setting RVG is redundant with reading riscv,isa and/or other extension discovery mechanisms.

To make matters more interesting, 'g' is still supposed to be present in riscv,isa. Toolchains will keep using 'g' in their 'march' normally too. So aside from sparing one bit in the MISA every other use of RVG is still valid.

We have a good chunk of code that uses RVG as a synonym for that extension pack and, for now, I don't see a reason to refactor it. We will cross that bridge when/if RVG acquires another official use.

For now, if we're running priv spec 1.11+, filter the bit out of misa.

Fixes 4f13abcb2bcb ("target/riscv: add RVG and remove cpu->cfg.ext_g")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4811

---
 target/riscv/tcg/csr.c               | 17 ++++++++++
 tests/tcg/riscv64/system/meson.build |  5 +++
 tests/tcg/riscv64/test-misa-G.S      | 51 ++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-misa-G.S
target/riscv/tcg/csr.c +17 -0
index ab278a97bc..929ee0a279 100644
@@ -2239,6 +2239,15 @@ static RISCVException read_misa(CPURISCVState *env, int csrno,
     }
 
     *val = misa | env->misa_ext;
+
+     /*
+      * RVG is reserved for priv > 1.10 but we're setting the
+      * bit internally as a shortcut for IMAFD_Zicsr_Zifencei.
+      */
+    if (env->priv_ver > PRIV_VERSION_1_10_0) {
+        *val &= ~RVG;
+    }
+
     return RISCV_EXCP_NONE;
 }
 
@@ -2267,6 +2276,14 @@ static RISCVException write_misa(CPURISCVState *env, int csrno,
         return RISCV_EXCP_NONE;
     }
 
+     /*
+      * RVG is reserved for priv > 1.10 but we're setting the
+      * bit internally as a shortcut for IMAFD_Zicsr_Zifencei.
+      */
+    if (env->priv_ver > PRIV_VERSION_1_10_0) {
+        val &= ~RVG;
+    }
+
     /* Mask extensions that are not supported by this hart */
     val &= env->misa_ext_mask;
 
tests/tcg/riscv64/system/meson.build +5 -0
index 9cd62c27e9..d91e28a4d2 100644
@@ -161,6 +161,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max,smpmpmt=false,smdbltrp=true,ssdbltrp=true,smrnmi=true', qemu_args],
   },
+
+  'test-misa-G.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-misa-G.S new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.43.0
#11 lore raw

PATCH 11/15 target/riscv: do not clear mstatush after mstatus write

In reply to Daniel Henrique Barboza

For RV32 we have mstatus and mstatush, both 32 bit CSRs, that shares the same 64 bit container in env->mstatus. mstatus will read/write the lower 32 bits of env->mstatus, mstatush will read/write the higher 32 bits of env->mstatus.

We forgot about that in write_mstatus() and, when running in 32 bits, we're happily setting the write mask with bits that belongs to higher half of env->mstatus. The result is that whatever was written there via mstatush is being cleared.

The solution is to clear the higher half of the mask if we're running 32 bits.

Fixes 284d697c74ef ("target/riscv: Merge m/vsstatus and m/vsstatush into one uint64_t unit")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4488

---
 target/riscv/tcg/csr.c                  |  9 ++++
 tests/tcg/riscv32/system/meson.build    |  5 +++
 tests/tcg/riscv32/test-mstatush-clear.S | 57 +++++++++++++++++++++++++
 3 files changed, 71 insertions(+)
 create mode 100644 tests/tcg/riscv32/test-mstatush-clear.S
target/riscv/tcg/csr.c +9 -0
index 929ee0a279..9414f741d5 100644
@@ -2161,6 +2161,15 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
         }
     }
 
+    /*
+     * For 32 bit accesses we're writing just the lower 32 bits of
+     * the env->mstatus container.  The higher bits are written via
+     * write_mstatush().
+     */
+    if (xl == MXL_RV32) {
+        mask &= MAKE_64BIT_MASK(0, 32);
+    }
+
     mstatus = (mstatus & ~mask) | (val & mask);
 
     env->mstatus = mstatus;
tests/tcg/riscv32/system/meson.build +5 -0
index 415a1c212d..3217c51951 100644
@@ -27,6 +27,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-mstatush-clear.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max,smdbltrp=on', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv32' in emulators
tests/tcg/riscv32/test-mstatush-clear.S new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.43.0
#12 lore raw

PATCH 12/15 target/riscv: clear mstatus.MIE if mstatush.MDT is set

In reply to Daniel Henrique Barboza

This is the same logic used by rv64 with the difference that, for rv32, MDT is located in mstatush and MIE is still on mstatus:

- write_mstatush(): clear mstatus.MIE if mstatush.MDT is going to be set;
- write_mstatus(): for rv32, if mstatush.MDT is already set, clear mstatus.MIE from 'val'.

The logic for rv64 is unchanged.

Fixes d2e92f1c6d44 ("target/riscv: Add Smdbltrp CSRs handling")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4827

---
 target/riscv/tcg/csr.c                    | 22 ++++++++-
 tests/tcg/riscv32/system/meson.build      |  5 +++
 tests/tcg/riscv32/test-mstatush-mdt-mie.S | 55 +++++++++++++++++++++++
 3 files changed, 80 insertions(+), 2 deletions(-)
 create mode 100644 tests/tcg/riscv32/test-mstatush-mdt-mie.S
target/riscv/tcg/csr.c +20 -2
index 9414f741d5..429d7093a9 100644
@@ -2091,6 +2091,7 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
     uint64_t mstatus = env->mstatus;
     uint64_t mask = 0;
     RISCVMXL xl = riscv_cpu_mxl(env);
+    bool is_rv32 = xl == MXL_RV32;
 
     /*
      * MPP field have been made WARL since priv version 1.11. However,
@@ -2130,7 +2131,19 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
 
     if (riscv_cpu_cfg(env)->ext_smdbltrp) {
         mask |= MSTATUS_MDT;
-        if ((val & MSTATUS_MDT) != 0) {
+
+        /*
+         * For RV64, we'll have both MDT and MIE in the same
+         * register so the MDT state that matters is the one being
+         * written in 'val'.
+         *
+         * For RV32 MDT is located in mstatush, thus we'll have to
+         * check env->mstatus to see its current state.
+         *
+         * For both cases, if MDT = 1, MIE must be zero.
+         */
+        if ((!is_rv32 && (val & MSTATUS_MDT))
+            || (is_rv32 && (env->mstatus & MSTATUS_MDT))) {
             val &= ~MSTATUS_MIE;
         }
     }
@@ -2206,7 +2219,12 @@ static RISCVException write_mstatush(CPURISCVState *env, int csrno,
     if (riscv_cpu_cfg(env)->ext_smdbltrp) {
         mask |= MSTATUS_MDT;
         if ((valh & MSTATUS_MDT) != 0) {
-            mask |= MSTATUS_MIE;
+            /*
+             * Priv spec: When the MDT bit is set to 1 by an explicit
+             * CSR write, the MIE (Machine Interrupt Enable) bit is
+             * cleared to 0.
+             */
+            env->mstatus &= ~MSTATUS_MIE;
         }
     }
     env->mstatus = (env->mstatus & ~mask) | (valh & mask);
tests/tcg/riscv32/system/meson.build +5 -0
index 3217c51951..79ad7b89ac 100644
@@ -32,6 +32,11 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max,smdbltrp=on', qemu_args],
   },
+
+  'test-mstatush-mdt-mie.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max,smdbltrp=on', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv32' in emulators
tests/tcg/riscv32/test-mstatush-mdt-mie.S new file +55 -0

55 lines not on this page · raw message

Signature · 1 line
2.43.0
#13 lore raw

PATCH 13/15 target/riscv/tcg/pmp.c: remove no-op in mseccfg_csr_write()

In reply to Daniel Henrique Barboza

This is a no-op:

  val |= (val & MSECCFG_MLPE);

Doing an IOR with 'val' and a bit from 'val' and storing it in 'val' does nothing. 'val' is unchanged.

If we analyze the commit that introduced it we might have a hint on what was the original intent of this line (a bit clear ... maybe). We're going to rework mseccfg_csr_write() in the next patch though, so instead of trying to figure it out we'll just remove it.

Fixes 4923f672e3d7 ("target/riscv: Introduce elp state and enabling controls for zicfilp")
---
 target/riscv/tcg/pmp.c | 5 -----
 1 file changed, 5 deletions(-)
target/riscv/tcg/pmp.c +0 -5
index 021ec978b5..17ec81f028 100644
@@ -720,11 +720,6 @@ void mseccfg_csr_write(CPURISCVState *env, uint64_t val)
         val &= ~(mask);
     }
 
-    /* M-mode forward cfi to be enabled if cfi extension is implemented */
-    if (env_archcpu(env)->cfg.ext_zicfilp) {
-        val |= (val & MSECCFG_MLPE);
-    }
-
     env->mseccfg = val;
 }
 
Signature · 1 line
2.43.0
#14 lore raw

PATCH 14/15 target/riscv/tcg: refactor mseccfg_csr_write()

In reply to Daniel Henrique Barboza

We have a couple of Gitlab issues that lands on mseccfg_csr_write(). Both are related to bits that are WPRI/read-only being written, bits that are supposed to behave as usual being considered sticky and so on.

mseccfg_csr_write() is in a weird shape. We have a 'mask' variable that in theory is a write mask like write_mstatus() and others use, but then we're clearing 'val' directly using 'mask' in certain conditions. The sticky bits check is also using 'mask', which includes bits that aren't sticky, and then all sorts of fun and games happen (see [1]). And in the end we're doing 'env->mseccfg = val' and writing whatever is left in 'val', which can and will include WPRI bits.

The function was introduced by 2582a95c3c46 and it already had problems with the WPRI bits being written since there were no write mask concept in it. Commit 33ca99a111b4 introduced a 'mask' variable that isn't exactly a write mask, or even a clear mask, and it has sticky bits mixed in middle.

I tried to do minimal code changes to address these bugs, but with the current structure it can't be done without making things even weirder. So I decided to rewrite it as follows:

- 'mask' is now a classic write mask variable, like write_mstatus() and others use;
- a new 'sticky_bits_mask' variable is added. Sticky bits aren't handled the same as regular bits and we're now making a clear distinction between them;
- env->mseccfg is calculated by writing the writable mask and IORing the sticky bits;
- there are no bit clearing of 'val' if there's no smepmp present.

These changes are enough to fix GL4818. Tests for a second gitlab bug (GL4680) are added as support for the refactory. We'll solve the latter in the next patch.

Fixes 2582a95c3c46 ("target/riscv: Add ePMP CSR access functions")
Fixes 33ca99a111b4 ("target/riscv: Add new CSR fields for S{sn, mn, m}pm extensions as part of Zjpm v1.0")

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4818

---
 target/riscv/tcg/pmp.c                        | 61 ++++++++++++------
 tests/tcg/riscv32/system/meson.build          | 20 ++++++
 tests/tcg/riscv32/test-mseccfg-bits-1.S       | 56 +++++++++++++++++
 tests/tcg/riscv32/test-mseccfg-bits-2.S       | 56 +++++++++++++++++
 tests/tcg/riscv32/test-mseccfg-bits-3.S       | 56 +++++++++++++++++
 tests/tcg/riscv32/test-mseccfg-bits-4.S       | 55 ++++++++++++++++
 tests/tcg/riscv64/system/meson.build          | 10 +++
 tests/tcg/riscv64/test-mseccfg-pmm-clear.S    | 63 +++++++++++++++++++
 tests/tcg/riscv64/test-mseccfg-pmm-reserved.S | 63 +++++++++++++++++++
 9 files changed, 421 insertions(+), 19 deletions(-)
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-1.S
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-2.S
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-3.S
 create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-4.S
 create mode 100644 tests/tcg/riscv64/test-mseccfg-pmm-clear.S
 create mode 100644 tests/tcg/riscv64/test-mseccfg-pmm-reserved.S
target/riscv/tcg/pmp.c +42 -19
index 17ec81f028..5ea52d55a8 100644
@@ -687,9 +687,11 @@ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
  */
 void mseccfg_csr_write(CPURISCVState *env, uint64_t val)
 {
-    int i;
-    uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
-    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+    uint64_t sticky_bits_mask = 0;
+    uint64_t mask = 0;
+
+    trace_mseccfg_csr_write(env->mhartid, val);
+
     /* Update PMM field only if the value is valid according to Zjpm v1.0 */
     if (riscv_cpu_cfg(env)->ext_smmpm &&
         riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -697,30 +699,51 @@ void mseccfg_csr_write(CPURISCVState *env, uint64_t val)
         mask |= MSECCFG_PMM;
     }
 
-    trace_mseccfg_csr_write(env->mhartid, val);
+    if (riscv_cpu_cfg(env)->ext_smepmp) {
+        bool rlb_writable = true;
+
+        /*
+         * RLB cannot be enabled if it's already 0 and
+         * if any regions are locked.
+         */
+        if (!MSECCFG_RLB_ISSET(env)) {
+            uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    /* RLB cannot be enabled if it's already 0 and if any regions are locked */
-    if (!MSECCFG_RLB_ISSET(env)) {
-        for (i = 0; i < pmp_regions; i++) {
-            if (pmp_is_locked(env, i)) {
-                val &= ~MSECCFG_RLB;
-                break;
+            for (int i = 0; i < pmp_regions; i++) {
+                if (pmp_is_locked(env, i)) {
+                    rlb_writable = false;
+                    break;
+                }
             }
         }
-    }
 
-    if (riscv_cpu_cfg(env)->ext_smepmp) {
-        /* Sticky bits */
-        val |= (env->mseccfg & mask);
-        if ((val ^ env->mseccfg) & mask) {
+        if (rlb_writable) {
+            mask |= MSECCFG_RLB;
+        }
+
+        sticky_bits_mask |= MSECCFG_MMWP | MSECCFG_MML;
+        val |= (env->mseccfg & sticky_bits_mask);
+
+        if ((val ^ env->mseccfg) & sticky_bits_mask) {
             tlb_flush(env_cpu(env));
         }
-    } else {
-        mask |= MSECCFG_RLB;
-        val &= ~(mask);
     }
 
-    env->mseccfg = val;
+    if (riscv_cpu_cfg(env)->ext_zicfilp) {
+        mask |= MSECCFG_MLPE;
+    }
+
+    if (riscv_cpu_cfg(env)->ext_zkr) {
+        if (riscv_has_ext(env, RVS)) {
+            mask |= MSECCFG_SSEED;
+        }
+
+        if (riscv_has_ext(env, RVU)) {
+            mask |= MSECCFG_USEED;
+        }
+    }
+
+    env->mseccfg = (env->mseccfg & ~mask) | (val & (mask | sticky_bits_mask));
 }
 
 /*
tests/tcg/riscv32/system/meson.build +20 -0
index 79ad7b89ac..44388af101 100644
@@ -37,6 +37,26 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max,smdbltrp=on', qemu_args],
   },
+
+  'test-mseccfg-bits-1.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'rv32,u=off,h=off,s=off,zkr=on,zicfilp=off', qemu_args],
+  },
+
+  'test-mseccfg-bits-2.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'rv32,smepmp=true,zkr=false,zicfilp=false', qemu_args],
+  },
+
+  'test-mseccfg-bits-3.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'rv32,zkr=false,zicfilp=true', qemu_args],
+  },
+
+  'test-mseccfg-bits-4.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'rv32,zkr=true', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv32' in emulators
tests/tcg/riscv32/test-mseccfg-bits-1.S new file +56 -0

56 lines not on this page · raw message

tests/tcg/riscv32/test-mseccfg-bits-2.S new file +56 -0

56 lines not on this page · raw message

tests/tcg/riscv32/test-mseccfg-bits-3.S new file +56 -0

56 lines not on this page · raw message

tests/tcg/riscv32/test-mseccfg-bits-4.S new file +55 -0

55 lines not on this page · raw message

tests/tcg/riscv64/system/meson.build +10 -0
index d91e28a4d2..c6968dc37a 100644
@@ -166,6 +166,16 @@ tests += {
     'cflags': cflags,
     'qemu_args': ['-cpu', 'max', qemu_args],
   },
+
+  'test-mseccfg-pmm-clear.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
+
+  'test-mseccfg-pmm-reserved.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  },
 }
 
 if 'qemu-system-riscv64' in emulators
tests/tcg/riscv64/test-mseccfg-pmm-clear.S new file +63 -0

63 lines not on this page · raw message

tests/tcg/riscv64/test-mseccfg-pmm-reserved.S new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
#15 lore raw

PATCH 15/15 target/riscv/tcg/csr.c: add mseccfgh CSR

In reply to Daniel Henrique Barboza

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

---
 target/riscv/tcg/csr.c                  | 32 +++++++++++++++++++++++++
 tests/tcg/riscv32/test-mseccfg-bits-1.S | 15 +++++++++++-
 2 files changed, 46 insertions(+), 1 deletion(-)
target/riscv/tcg/csr.c +32 -0
index 429d7093a9..b4288b5576 100644
@@ -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) {
@@ -5762,6 +5771,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)
 {
@@ -6665,6 +6694,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  },
tests/tcg/riscv32/test-mseccfg-bits-1.S +14 -1
index ffa88f5f2f..01c29577e1 100644
@@ -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:
Signature · 1 line
2.43.0