Skip to contentSkip to conversation

target/riscv: do not write SXL < UXL

PATCH New Daniel Henrique Barboza 1 participant 2 messages Nov 25

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

v3 cover target/riscv: do not write SXL < UXL

Hi,

In this version we're adding a comment in riscv_update_xl_field() explaining the intended use of the 'min_field_val' argument. We also took the chance of fixing a conflict with the alistair/riscv-to-apply.next branch.

No other changes made. Patch based on alistair/riscv-to-apply.next.

Changes in v3:
- added 'min_field_val' intended use comment in riscv_update_xl_field();
- rebased on top of alistair/riscv-to-apply.next.
- v2 link: https://lore.kernel.org/qemu-devel/[email protected]/

Daniel Henrique Barboza (1):
target/riscv: do not write SXL < UXL

 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
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/1 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..8938fc6caa 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