target/riscv/tcg/csr.c | 11 +++ tests/tcg/riscv64/system/meson.build | 7 ++ tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S | 75 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S
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
Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
---
target/riscv/tcg/csr.c | 11 +++
tests/tcg/riscv64/system/meson.build | 7 ++
tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S | 75 +++++++++++++++++++
3 files changed, 93 insertions(+)
create mode 100644 tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..eff5aec586 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -2023,6 +2023,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)
{
@@ -5789,6 +5793,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) {
diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/system/meson.build
index 9edab1fd7a..a16ef84104 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -81,6 +81,13 @@ tests += {
}
}
+tests += {
+ 'test-mstatus-mnstatus-mpp.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'max,smpmpmt=false,smdbltrp=true,ssdbltrp=true,smrnmi=true', qemu_args],
+ }
+}
+
tests += {
'test-xsfvqmacc.S': {
'cflags': cflags,
diff --git a/tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S b/tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S
new file mode 100644
index 0000000000..b3a734ccf3
--- /dev/null
+++ b/tests/tcg/riscv64/test-mstatus-mnstatus-mpp.S
@@ -0,0 +1,75 @@
+/*
+ * Test if mstatus.MPP and mnstatus.MNPP will write reserved values.
+ * See Gitlab 4826 for more info.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MNSTATUS 0x744
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ /* Set mstatus.MPP [12:11] and mnstatus.MNPP to 11b */
+ li t0, 0b11
+ slli t0, t0, 11
+ csrs mstatus, t0
+ csrs CSR_MNSTATUS, t0
+
+ /* Set mstatus.MPP to reserved value 10b by clearing bit 11. */
+ li t1, 1
+ slli t1, t1, 11
+ csrs mstatus, t1
+
+ /* mstatus.MPP must be equal to 11b (ignoring the last write). */
+ csrr s0, mstatus
+ srli s0, s0, 11
+ li t2, 3
+ and s0, s0, t2
+ bne s0, t2, _fail
+
+ /* Set mnstatus.MNPP to reserved value 10b by clearing bit 11. */
+ csrs CSR_MNSTATUS, t1
+
+ /* mnstatus.MNPP must be equal to 11b (ignoring the last write). */
+ csrr s0, CSR_MNSTATUS
+ srli s0, s0, 11
+ and s0, s0, t2
+ bne s0, t2, _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
© 2016 - 2026 Red Hat, Inc.