Skip to contentSkip to conversation

target/riscv: do not write SXL < UXL [GL4821]

PATCH Superseded Daniel Henrique Barboza 1 participant 2 messages Nov 12

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover target/riscv: do not write SXL < UXL [GL4821]

Based-on: [email protected] ("target/riscv: change mstatus bits for read-only if S ou U unsupported")

Hi,

This patch fixes a Gitlab issue where we're writing a SXL val smaller than UXL in mstatus, which turns out to be a spec violation.

The patch is based on the following patch from Christian:

"[PATCH v2] target/riscv: change mstatus bits for read-only if S ou U unsupported"

Christian ended up sending a fix in the same code region and I decided to avoid the conflict by making this fix dependent on his. In case his fix is delay I can re-send this based on another commit.

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

 target/riscv/tcg/csr.c               | 40 +++++++++++++------
 tests/tcg/riscv64/system/meson.build |  7 ++++
 tests/tcg/riscv64/test-uxl-sxl.S     | 59 ++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 13 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 target/riscv: do not write SXL < UXL [GL4821]

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

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

---
 target/riscv/tcg/csr.c               | 40 +++++++++++++------
 tests/tcg/riscv64/system/meson.build |  7 ++++
 tests/tcg/riscv64/test-uxl-sxl.S     | 59 ++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+), 13 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-uxl-sxl.S
target/riscv/tcg/csr.c +27 -13
index 24eda54bac..c54fa026a2 100644
@@ -2049,15 +2049,25 @@ 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)
+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 < min_field_val) {
+        xl = min_field_val;
+        field_updated = true;
+    }
 
-    if (xl != MXL_RV128 && uxl == MXL_RV128) {
-        uxl = xl;
-        val = set_field(val, field, uxl);
+    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 +2128,18 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
         if (riscv_has_ext(env, RVH)) {
             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 = mask & MSTATUS64_UXL
+                               ? get_field(val, MSTATUS64_UXL) : 0;
+
+            mask |= MSTATUS64_SXL;
+            val = riscv_update_xl_field(env, val, MSTATUS64_SXL, min_val);
         }
     }
 
@@ -5474,7 +5488,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 3c8b018d9e..65bcc2bdbc 100644
@@ -89,6 +89,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