target/riscv/tcg/csr.c | 14 ++++- tests/tcg/riscv64/system/meson.build | 7 +++ tests/tcg/riscv64/test-menvcfg-cbie.S | 82 +++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 tests/tcg/riscv64/test-menvcfg-cbie.S
We can't allow CBIE = 10b to be written since it's marked as Reserved.
Fixes: e979f6a15552 ("target/riscv: gate envcfg CBO fields on extensions")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4216
Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
---
target/riscv/tcg/csr.c | 14 ++++-
tests/tcg/riscv64/system/meson.build | 7 +++
tests/tcg/riscv64/test-menvcfg-cbie.S | 82 +++++++++++++++++++++++++++
3 files changed, 101 insertions(+), 2 deletions(-)
create mode 100644 tests/tcg/riscv64/test-menvcfg-cbie.S
diff --git a/target/riscv/tcg/csr.c b/target/riscv/tcg/csr.c
index af444e3e6f..1ba5835e1b 100644
--- a/target/riscv/tcg/csr.c
+++ b/target/riscv/tcg/csr.c
@@ -3396,7 +3396,12 @@ static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
/* CBIE/CBCFE are read-only zero when Zicbom is not implemented. */
if (cfg->ext_zicbom) {
- mask |= MENVCFG_CBIE | MENVCFG_CBCFE;
+ mask |= MENVCFG_CBCFE;
+
+ /* CBIE = 0b10 is marked as Reserved. */
+ if (get_field(val, MENVCFG_CBIE) != 0b10) {
+ mask |= MENVCFG_CBIE;
+ }
}
/* CBZE is read-only zero when Zicboz is not implemented. */
@@ -3578,7 +3583,12 @@ static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
/* CBIE/CBCFE are read-only zero when Zicbom is not implemented. */
if (cfg->ext_zicbom) {
- mask |= HENVCFG_CBIE | HENVCFG_CBCFE;
+ mask |= HENVCFG_CBCFE;
+
+ /* CBIE = 0b10 is marked as Reserved. */
+ if (get_field(val, HENVCFG_CBIE) != 0b10) {
+ mask |= HENVCFG_CBIE;
+ }
}
/* CBZE is read-only zero when Zicboz is not implemented. */
diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..3e82fc83a0 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,13 @@ tests += {
}
}
+tests += {
+ 'test-menvcfg-cbie.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-menvcfg-cbie.S b/tests/tcg/riscv64/test-menvcfg-cbie.S
new file mode 100644
index 0000000000..324993d214
--- /dev/null
+++ b/tests/tcg/riscv64/test-menvcfg-cbie.S
@@ -0,0 +1,82 @@
+/*
+ * Test if menvcfg.CBIE/henvcfg.CBIE reserved value 10b isn't written.
+ * See Gitlab 4216 for more info.
+ *
+ * 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
+
+ /* Set menvcfg.CBIE [5:4] to 0b00. */
+ li t0, 3
+ slli t0, t0, 4
+ csrc menvcfg, t0
+
+ /* Now set it to 0b10. */
+ li t1, 1
+ slli t1, t1, 5
+ csrs menvcfg, t1
+
+ /* Read it back, rshift it, check if it's 00. */
+ csrr s0, menvcfg
+ srli s0, s0, 4
+ andi s0, s0, 3
+ bnez s0, _fail
+
+ /*
+ * The same constraints apply to henvcfg.CBIE, thus we're
+ * testing it like above: set it to 00b, then 10b, then
+ * see if it's zero.
+ */
+ li t0, 3
+ slli t0, t0, 4
+ csrc henvcfg, t0
+
+ /* Now set it to 0b10. */
+ li t1, 1
+ slli t1, t1, 5
+ csrs henvcfg, t1
+
+ /* Read it back, rshift it, check if it's 00. */
+ csrr s0, henvcfg
+ srli s0, s0, 4
+ andi s0, s0, 3
+ bnez s0, _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.