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
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
 target/riscv/cpu_bits.h              |  9 ++++
 target/riscv/tcg/csr.c               |  8 ++++
 tests/tcg/riscv64/system/meson.build |  7 ++++
 tests/tcg/riscv64/test-hgatp-rw.S    | 61 ++++++++++++++++++++++++++++
 4 files changed, 85 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-hgatp-rw.S

diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
index 3579789a1f..dcd3f239fe 100644
--- a/target/riscv/cpu_bits.h
+++ b/target/riscv/cpu_bits.h
@@ -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
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..7d05a50586 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -5275,6 +5275,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;
 }
 
diff --git a/tests/tcg/riscv64/system/meson.build 
b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..23f05d0ec1 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,13 @@ tests += {
   }
 }
 
+tests += {
+  'test-hgatp-rw.S': {
+    'cflags': cflags,
+    'qemu_args': ['-cpu', 'max', qemu_args],
+  }
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
diff --git a/tests/tcg/riscv64/test-hgatp-rw.S 
b/tests/tcg/riscv64/test-hgatp-rw.S
new file mode 100644
index 0000000000..4bda3c38f5
--- /dev/null
+++ b/tests/tcg/riscv64/test-hgatp-rw.S
@@ -0,0 +1,61 @@
+/*
+ * Test for writable hgatp bits.  See Gitlab 4591 for reference.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+.option norvc
+       .text
+       .global _start
+
+_start:
+       lla     t0, trap
+       csrw    mtvec, t0
+
+       /*
+        * Write in HGATP the same value written in Gitlab 4591
+        * testcase (0x8c00000000000003).
+        */
+       li      t0, 0x8c
+       slli    t0, t0, 56
+       addi    t0, t0, 3
+       csrw    hgatp, t0
+
+       /* hgatp expected is 0x8000000000000000. */
+       li      t0, 0x8
+       slli    t0, t0, 60
+       csrr    t1, hgatp
+
+       bne     t0, t1, _fail
+       li      a0, 0
+       j       _exit
+
+_fail:
+       li      a0, 1
+       j       _exit
+
+trap:
+       # Any trap is a fail code 1
+       li      a0, 1
+
+# Exit code in a0
+_exit:
+       lla     a1, semiargs
+       li      t0, 0x20026     # ADP_Stopped_ApplicationExit
+       sd      t0, 0(a1)
+       sd      a0, 8(a1)
+       li      a0, 0x20        # TARGET_SYS_EXIT_EXTENDED
+
+       # Semihosting call sequence
+       .balign 16
+       slli    zero, zero, 0x1f
+       ebreak
+       srai    zero, zero, 0x7
+       j       .
+
+       .data
+       .balign 16
+semiargs:
+       .space  16
+
-- 
2.43.0


Reply via email to