We need to check hideleg writable bits with mideleg and mvien. Priv spec
says:

"For bits of mideleg that are zero, the corresponding bits in hideleg,
hip, and hie are read-only zeros."

For mvien, the AIA spec says:

"When the H extension is implemented, if a bit is zero in the same
position in both mideleg and mvien, then that bit is read-only zero in
hideleg".

Filter hideleg writable bits by using mideleg and, if the proper conditions
are met, mvien.

Fixes: d028ac7512f1 ("target/riscv: Implement AIA CSRs for 64 local interrupts 
on RV32")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4814
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
 target/riscv/tcg/csr.c               | 22 ++++++++++
 tests/tcg/riscv64/system/meson.build |  8 ++++
 tests/tcg/riscv64/test-hideleg.S     | 64 ++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-hideleg.S

diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..c130eb2c8a 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -4997,6 +4997,28 @@ static RISCVException rmw_hideleg64(CPURISCVState *env, 
int csrno,
 {
     uint64_t mask = wr_mask & vs_delegable_ints;
 
+    if (riscv_has_ext(env, RVH)
+        && riscv_cpu_cfg(env)->ext_smaia) {
+        /*
+         * The AIA spec states in section "Interrupt filtering and virtual
+         * interrupts for supervisor level":
+         *
+         * "When the H extension is implemented, if a bit is zero in the
+         * same position in both mideleg and mvien, then that bit is
+         * read-only zero in hideleg (...). But if a bit for one of
+         * interrupts 13-63 is a one in either mideleg or mvien, then the
+         * same bit in hideleg may be writable or may be read-only zero,
+         * depending on the implementation."
+         *
+         * We're making the simpler choice of making all bits read-only
+         * zero if the bit is zero on both mideleg and mvien, and writable
+         * otherwise.  We're not going to special case bits 63:13.
+         */
+        mask &= (env->mideleg | env->mvien);
+    } else {
+        mask &= env->mideleg;
+    }
+
     if (ret_val) {
         *ret_val = env->hideleg & vs_delegable_ints;
     }
diff --git a/tests/tcg/riscv64/system/meson.build 
b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..6c08806cc9 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,14 @@ tests += {
   }
 }
 
+tests += {
+  'test-hideleg.S': {
+    'cflags': cflags,
+    'qemu_args': ['-machine', 'virt,aia=aplic-imsic,aia-guests=7',
+                  '-cpu', 'max', qemu_args],
+  }
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
diff --git a/tests/tcg/riscv64/test-hideleg.S b/tests/tcg/riscv64/test-hideleg.S
new file mode 100644
index 0000000000..a07d48430f
--- /dev/null
+++ b/tests/tcg/riscv64/test-hideleg.S
@@ -0,0 +1,64 @@
+/*
+ * Test if bits 63:16 from hideleg becomes writable when
+ * they shouldn't.  Derived from Gitlab 4814.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MVIEN 0x308
+#define CSR_HIDELEG 0x603
+
+.option norvc
+       .text
+       .global _start
+
+_start:
+       lla     t0, trap
+       csrw    mtvec, t0
+
+       /* Mask for 63:16.  */
+       li      t0, -1
+       slli    s0, t0, 16
+
+       /* mideleg = 0, mvien = 0: hideleg bits 63:16 should be zero.  */
+       li      t0, 0
+       csrw    mideleg, t0
+       csrw    CSR_MVIEN, t0
+       li      t1, -1
+       csrw    CSR_HIDELEG, t1
+
+       csrr    t1, CSR_HIDELEG
+       and     t1, t1, s0
+       bnez    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