We have a couple of Gitlab issues that lands on mseccfg_csr_write().
Both are related to bits that are WPRI/read-only being written, bits
that are supposed to behave as usual being considered sticky and so on.
mseccfg_csr_write() is in a weird shape. We have a 'mask' variable that in
theory is a write mask like write_mstatus() and others use, but then
we're clearing 'val' directly using 'mask' in certain conditions. The
sticky bits check is also using 'mask', which includes bits that aren't
sticky, and then all sorts of fun and games happen (see [1]). And in
the end we're doing 'env->mseccfg = val' and writing whatever is left in
'val', which can and will include WPRI bits.
The function was introduced by 2582a95c3c46 and it already had problems
with the WPRI bits being written since there were no write mask concept in
it. Commit 33ca99a111b4 introduced a 'mask' variable that isn't exactly
a write mask, or even a clear mask, and it has sticky bits mixed in
middle.
I tried to do minimal code changes to address these bugs, but with the
current structure it can't be done without making things even weirder.
So I decided to rewrite it as follows:
- 'mask' is now a classic write mask variable, like write_mstatus() and
others use;
- a new 'sticky_bits_mask' variable is added. Sticky bits aren't
handled the same as regular bits and we're now making a clear
distinction between them;
- env->mseccfg is calculated by writing the writable mask and
IORing the sticky bits;
- there are no bit clearing of 'val' if there's no smepmp present.
These changes are enough to fix GL4818. Tests for a second gitlab bug
(GL4680) are added as support for the refactory. We'll solve the latter
in the next patch.
Fixes: 2582a95c3c46 ("target/riscv: Add ePMP CSR access functions")
Fixes: 33ca99a111b4 ("target/riscv: Add new CSR fields for S{sn, mn, m}pm
extensions as part of Zjpm v1.0")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4818
Signed-off-by: Daniel Henrique Barboza <[email protected]>
---
target/riscv/tcg/pmp.c | 61 ++++++++++++------
tests/tcg/riscv32/system/meson.build | 19 ++++++
tests/tcg/riscv32/test-mseccfg-bits-1.S | 56 +++++++++++++++++
tests/tcg/riscv32/test-mseccfg-bits-2.S | 56 +++++++++++++++++
tests/tcg/riscv32/test-mseccfg-bits-3.S | 56 +++++++++++++++++
tests/tcg/riscv32/test-mseccfg-bits-4.S | 55 ++++++++++++++++
tests/tcg/riscv64/system/meson.build | 11 ++++
tests/tcg/riscv64/test-mseccfg-pmm-clear.S | 63 +++++++++++++++++++
tests/tcg/riscv64/test-mseccfg-pmm-reserved.S | 63 +++++++++++++++++++
9 files changed, 421 insertions(+), 19 deletions(-)
create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-1.S
create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-2.S
create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-3.S
create mode 100644 tests/tcg/riscv32/test-mseccfg-bits-4.S
create mode 100644 tests/tcg/riscv64/test-mseccfg-pmm-clear.S
create mode 100644 tests/tcg/riscv64/test-mseccfg-pmm-reserved.S
diff --git a/target/riscv/tcg/pmp.c b/target/riscv/tcg/pmp.c
index 7b41c628f3..57079478e2 100644
--- a/target/riscv/tcg/pmp.c
+++ b/target/riscv/tcg/pmp.c
@@ -679,9 +679,11 @@ target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t
addr_index)
*/
void mseccfg_csr_write(CPURISCVState *env, uint64_t val)
{
- int i;
- uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
- uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+ uint64_t sticky_bits_mask = 0;
+ uint64_t mask = 0;
+
+ trace_mseccfg_csr_write(env->mhartid, val);
+
/* Update PMM field only if the value is valid according to Zjpm v1.0 */
if (riscv_cpu_cfg(env)->ext_smmpm &&
riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -689,30 +691,51 @@ void mseccfg_csr_write(CPURISCVState *env, uint64_t val)
mask |= MSECCFG_PMM;
}
- trace_mseccfg_csr_write(env->mhartid, val);
+ if (riscv_cpu_cfg(env)->ext_smepmp) {
+ bool rlb_writable = true;
+
+ /*
+ * RLB cannot be enabled if it's already 0 and
+ * if any regions are locked.
+ */
+ if (!MSECCFG_RLB_ISSET(env)) {
+ uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
- /* RLB cannot be enabled if it's already 0 and if any regions are locked */
- if (!MSECCFG_RLB_ISSET(env)) {
- for (i = 0; i < pmp_regions; i++) {
- if (pmp_is_locked(env, i)) {
- val &= ~MSECCFG_RLB;
- break;
+ for (int i = 0; i < pmp_regions; i++) {
+ if (pmp_is_locked(env, i)) {
+ rlb_writable = false;
+ break;
+ }
}
}
- }
- if (riscv_cpu_cfg(env)->ext_smepmp) {
- /* Sticky bits */
- val |= (env->mseccfg & mask);
- if ((val ^ env->mseccfg) & mask) {
+ if (rlb_writable) {
+ mask |= MSECCFG_RLB;
+ }
+
+ sticky_bits_mask |= MSECCFG_MMWP | MSECCFG_MML;
+ val |= (env->mseccfg & sticky_bits_mask);
+
+ if ((val ^ env->mseccfg) & sticky_bits_mask) {
tlb_flush(env_cpu(env));
}
- } else {
- mask |= MSECCFG_RLB;
- val &= ~(mask);
}
- env->mseccfg = val;
+ if (riscv_cpu_cfg(env)->ext_zicfilp) {
+ mask |= MSECCFG_MLPE;
+ }
+
+ if (riscv_cpu_cfg(env)->ext_zkr) {
+ if (riscv_has_ext(env, RVS)) {
+ mask |= MSECCFG_SSEED;
+ }
+
+ if (riscv_has_ext(env, RVU)) {
+ mask |= MSECCFG_USEED;
+ }
+ }
+
+ env->mseccfg = (env->mseccfg & ~mask) | (val & (mask | sticky_bits_mask));
}
/*
diff --git a/tests/tcg/riscv32/system/meson.build
b/tests/tcg/riscv32/system/meson.build
index 117ae6fbce..5d9a41b13c 100644
--- a/tests/tcg/riscv32/system/meson.build
+++ b/tests/tcg/riscv32/system/meson.build
@@ -22,6 +22,25 @@ tests += {
},
}
+tests += {
+ 'test-mseccfg-bits-1.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'rv32,u=off,h=off,s=off,zkr=on,zicfilp=off',
qemu_args],
+ },
+ 'test-mseccfg-bits-2.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'rv32,smepmp=true,zkr=false,zicfilp=false',
qemu_args],
+ },
+ 'test-mseccfg-bits-3.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'rv32,zkr=false,zicfilp=true', qemu_args],
+ },
+ 'test-mseccfg-bits-4.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'rv32,zkr=true', qemu_args],
+ },
+}
+
if 'qemu-system-riscv32' in emulators
tcg_tests += {
'riscv32-softmmu': {
diff --git a/tests/tcg/riscv32/test-mseccfg-bits-1.S
b/tests/tcg/riscv32/test-mseccfg-bits-1.S
new file mode 100644
index 0000000000..ffa88f5f2f
--- /dev/null
+++ b/tests/tcg/riscv32/test-mseccfg-bits-1.S
@@ -0,0 +1,56 @@
+/*
+ * Verify mseccfg bits USEED, SSEED, MLPE and WPRI.
+ *
+ * With "rv32,u=off,h=off,s=off,zkr=on,zicfilp=off" we expect no
+ * bits being written in mseccfg.
+ *
+ * Based on Gitlab 4680 testcase.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MSECCFG 0x747
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ li t0, -8 // all ones except bits 2:0
+ csrs CSR_MSECCFG, t0 // mseccfg
+ csrr t1, CSR_MSECCFG
+
+ /* WPRI fields are always read-only zero. */
+ li t2, 0xfffff8f8 // WPRI bits 31:11 and 7:3 (RV32 low half)
+ and t3, t1, t2
+ bnez t3, _fail
+
+ /*
+ * USEED: read-only zero if not zkr or no U-mode.
+ * SSEED: read-only zero if not zkr or no S-mode.
+ * MLPE: read-only zero if no Zifilp present.
+ */
+ srli t3, t1, 8
+ andi t3, t3, 7 // USEED, SSEED, MLPE -> bits 1, 2, 3
+ bnez t3, _fail
+
+ j _pass
+
+trap:
+ j _fail
+
+_pass:
+ li t0, 0x100000
+ li t1, 0x5555 /* FINISHER_PASS */
+ sw t1, 0(t0)
+ j .
+
+_fail:
+ li t0, 0x100000
+ li t1, 0x13333 /* status = FINISHER_FAIL, code = 1 */
+ sw t1, 0(t0)
+ j .
diff --git a/tests/tcg/riscv32/test-mseccfg-bits-2.S
b/tests/tcg/riscv32/test-mseccfg-bits-2.S
new file mode 100644
index 0000000000..acc022784d
--- /dev/null
+++ b/tests/tcg/riscv32/test-mseccfg-bits-2.S
@@ -0,0 +1,56 @@
+/*
+ * Verify mseccfg bits USEED, SSEED, MLPE and WPRI.
+ *
+ * With "rv32,smepmp=true,zkr=false,zicfilp=false" we expect no bits
+ * being written in mseccfg.
+ *
+ * Based on Gitlab 4680 testcase.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MSECCFG 0x747
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ li t0, -8 // all ones except bits 2:0
+ csrs CSR_MSECCFG, t0 // mseccfg
+ csrr t1, CSR_MSECCFG
+
+ /* WPRI fields are always read-only zero. */
+ li t2, 0xfffff8f8 // WPRI bits 31:11 and 7:3 (RV32 low half)
+ and t3, t1, t2
+ bnez t3, _fail
+
+ /*
+ * USEED: read-only zero if not zkr or no U-mode.
+ * SSEED: read-only zero if not zkr or no S-mode.
+ * MLPE: read-only zero if no Zifilp present.
+ */
+ srli t3, t1, 8
+ andi t3, t3, 7 // USEED, SSEED, MLPE -> bits 1, 2, 3
+ bnez t3, _fail
+
+ j _pass
+
+trap:
+ j _fail
+
+_pass:
+ li t0, 0x100000
+ li t1, 0x5555 /* FINISHER_PASS */
+ sw t1, 0(t0)
+ j .
+
+_fail:
+ li t0, 0x100000
+ li t1, 0x13333 /* status = FINISHER_FAIL, code = 1 */
+ sw t1, 0(t0)
+ j .
diff --git a/tests/tcg/riscv32/test-mseccfg-bits-3.S
b/tests/tcg/riscv32/test-mseccfg-bits-3.S
new file mode 100644
index 0000000000..2c0501bf96
--- /dev/null
+++ b/tests/tcg/riscv32/test-mseccfg-bits-3.S
@@ -0,0 +1,56 @@
+/*
+ * Verify mseccfg bits USEED, SSEED, MLPE and WPRI.
+ *
+ * with "rv32,zkr=false,zicfilp=true" only MLPE should be set.
+ *
+ * Based on Gitlab 4680 testcases.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MSECCFG 0x747
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ li t0, -8 // all ones except bits 2:0
+ csrs CSR_MSECCFG, t0 // mseccfg
+ csrr t1, CSR_MSECCFG
+
+ /* WPRI fields are always read-only zero. */
+ li t2, 0xfffff8f8 // WPRI bits 31:11 and 7:3 (RV32 low half)
+ and t3, t1, t2
+ bnez t3, _fail
+
+ /*
+ * USEED: read-only zero if not zkr or no U-mode.
+ * SSEED: read-only zero if not zkr or no S-mode.
+ * MLPE: must be enabled since we have Zifilp present.
+ */
+ srli t3, t1, 8
+ andi t3, t3, 7 // USEED, SSEED, MLPE -> bits 1, 2, 3
+ li t2, 4
+ bne t3, t2, _fail
+
+ j _pass
+
+trap:
+ j _fail
+
+_pass:
+ li t0, 0x100000
+ li t1, 0x5555 /* FINISHER_PASS */
+ sw t1, 0(t0)
+ j .
+
+_fail:
+ li t0, 0x100000
+ li t1, 0x13333 /* status = FINISHER_FAIL, code = 1 */
+ sw t1, 0(t0)
+ j .
diff --git a/tests/tcg/riscv32/test-mseccfg-bits-4.S
b/tests/tcg/riscv32/test-mseccfg-bits-4.S
new file mode 100644
index 0000000000..952c4bb964
--- /dev/null
+++ b/tests/tcg/riscv32/test-mseccfg-bits-4.S
@@ -0,0 +1,55 @@
+/*
+ * Verify mseccfg bits USEED, SSEED, MLPE and WPRI.
+ *
+ * with "rv32,zkr=true" we expect USEED and SSEED to be set.
+ *
+ * Based on Gitlab 4680 testcases.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MSECCFG 0x747
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ li t0, -8 // all ones except bits 2:0
+ csrs CSR_MSECCFG, t0 // mseccfg
+ csrr t1, CSR_MSECCFG
+
+ /* WPRI fields are always read-only zero. */
+ li t2, 0xfffff8f8 // WPRI bits 31:11 and 7:3 (RV32 low half)
+ and t3, t1, t2
+ bnez t3, _fail
+
+ /*
+ * USEED and SSEED: present.
+ * MLPE: read-only since we don't have Zifilp.
+ */
+ srli t3, t1, 8
+ andi t3, t3, 7 // USEED, SSEED, MLPE -> bits 1, 2, 3
+ li t2, 3
+ bne t3, t2, _fail
+
+ j _pass
+
+trap:
+ j _fail
+
+_pass:
+ li t0, 0x100000
+ li t1, 0x5555 /* FINISHER_PASS */
+ sw t1, 0(t0)
+ j .
+
+_fail:
+ li t0, 0x100000
+ li t1, 0x13333 /* status = FINISHER_FAIL, code = 1 */
+ sw t1, 0(t0)
+ j .
diff --git a/tests/tcg/riscv64/system/meson.build
b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..27709e248e 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,17 @@ tests += {
}
}
+tests += {
+ 'test-mseccfg-pmm-clear.S': {
+ 'cflags': cflags,
+ 'qemu_args': ['-cpu', 'max', qemu_args],
+ },
+ 'test-mseccfg-pmm-reserved.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-mseccfg-pmm-clear.S
b/tests/tcg/riscv64/test-mseccfg-pmm-clear.S
new file mode 100644
index 0000000000..c03dbb01ff
--- /dev/null
+++ b/tests/tcg/riscv64/test-mseccfg-pmm-clear.S
@@ -0,0 +1,63 @@
+/*
+ * Test if mseccfg.PMM is cleared correctly. From Gitlab 4818:
+ *
+ * "Smmpm: mseccfg.PMM (bits 33:32) is a WARL field whose legal
+ * values are 00, 10 and 11. Write 11, then clear it with csrc:
+ * it must read 00 (00 is the reset value, so it is supported).
+ * Pass (exit 0): PMM reads 00 after the clear."
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MSECCFG 0x747
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ /* Write (11) to PMM (bits 33:32). */
+ li t0, 3
+ slli t0, t0, 32
+ csrs CSR_MSECCFG, t0
+
+ csrr s0, CSR_MSECCFG
+
+ /* Clear PMM. */
+ csrc CSR_MSECCFG, t0
+
+ /* Read mseccfg again. PMM must read 00. */
+ csrr s1, CSR_MSECCFG
+ srli t1, s1, 32
+ andi t1, t1, 3
+ snez a0, t1
+
+ 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
diff --git a/tests/tcg/riscv64/test-mseccfg-pmm-reserved.S
b/tests/tcg/riscv64/test-mseccfg-pmm-reserved.S
new file mode 100644
index 0000000000..88764f24fb
--- /dev/null
+++ b/tests/tcg/riscv64/test-mseccfg-pmm-reserved.S
@@ -0,0 +1,63 @@
+/*
+ * Test if writing a reserved mseccfg.PMM value reads back a
+ * legal value. From Gitlab 4818:
+ *
+ * "Smmpm: write the reserved value 01 to mseccfg.PMM. It must
+ * read back a legal value (00, 10 or 11), and an M-mode load
+ * must then work. Pass (exit 0)."
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MSECCFG 0x747
+
+.option norvc
+ .text
+ .global _start
+
+_start:
+ lla t0, trap
+ csrw mtvec, t0
+
+ li t0, 1
+ slli t0, t0, 32
+ csrs CSR_MSECCFG, t0
+ csrr s0, CSR_MSECCFG
+
+ srli s1, s0, 32
+ andi s1, s1, 3
+
+ li t0, 1
+ beq s1, t0, _fail // 01 stored: 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