[PATCH] target/riscv: allow zicbo insns to run without RVS [GL4596]

Daniel Henrique Barboza posted 1 patch 1 week, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261113172603.107571-1-daniel.barboza@oss.qualcomm.com
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
target/riscv/tcg/op_helper.c         |  5 +-
tests/tcg/riscv64/system/meson.build |  7 ++
tests/tcg/riscv64/test-zicbo-umode.S | 98 ++++++++++++++++++++++++++++
3 files changed, 109 insertions(+), 1 deletion(-)
create mode 100644 tests/tcg/riscv64/test-zicbo-umode.S
[PATCH] target/riscv: allow zicbo insns to run without RVS [GL4596]
Posted by Daniel Henrique Barboza 1 week, 2 days ago
check_zicbo_envcfg() is validating access using S-mode constraints that
are only relevant if we have RVS enabled in the hart.  If we have a hart
that has M-mode and U-mode, but not S mode, zicbo insns will always
fault.

Execute the exclusive S-mode checks only if RVS is present.

Fixes: a939c500793a ("target/riscv: implement Zicboz extension")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4596
Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
---
 target/riscv/tcg/op_helper.c         |  5 +-
 tests/tcg/riscv64/system/meson.build |  7 ++
 tests/tcg/riscv64/test-zicbo-umode.S | 98 ++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+), 1 deletion(-)
 create mode 100644 tests/tcg/riscv64/test-zicbo-umode.S

diff --git a/target/riscv/tcg/op_helper.c b/target/riscv/tcg/op_helper.c
index f9c79fe6b9..b87e18a886 100644
--- a/target/riscv/tcg/op_helper.c
+++ b/target/riscv/tcg/op_helper.c
@@ -172,7 +172,10 @@ static void check_zicbo_envcfg(CPURISCVState *env, target_ulong envbits,
         riscv_raise_exception(env, RISCV_EXCP_VIRT_INSTRUCTION_FAULT, ra);
     }
 
-    if ((env->priv < PRV_S) && !get_field(env->senvcfg, envbits)) {
+    /* Skip S-mode exclusive checks if we don't have RVS.  */
+    if (riscv_has_ext(env, RVS)
+        && env->priv < PRV_S
+        && !get_field(env->senvcfg, envbits)) {
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, ra);
     }
 #endif
diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..3d6ef0fc42 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,13 @@ tests += {
   }
 }
 
+tests += {
+  'test-zicbo-umode.S': {
+   'cflags': cflags + ['-march=rv64gcv_zicbom_zicboz'],
+    'qemu_args': ['-cpu', 'rv64,s=off,h=off,zicbom=on,zicboz=on', qemu_args],
+  }
+}
+
 if 'qemu-system-riscv64' in emulators
   tcg_tests += {
     'riscv64-softmmu': {
diff --git a/tests/tcg/riscv64/test-zicbo-umode.S b/tests/tcg/riscv64/test-zicbo-umode.S
new file mode 100644
index 0000000000..4ce91baafc
--- /dev/null
+++ b/tests/tcg/riscv64/test-zicbo-umode.S
@@ -0,0 +1,98 @@
+/*
+ * Test if cbo instructions are executable without S mode
+ * available.  Code derived from the Gitlab 4596 testcase.
+ *
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define CSR_MENVCFG 0x30a
+#define MENVCFG_CB  ((1 << 7) | (1 << 6) | (1 << 4))  // CBZE | CBCFE | CBIE=01
+
+.option norvc
+	.text
+	.global _start
+
+_start:
+	lla	t0, trap
+	csrw	mtvec, t0
+
+	/*
+	 * PMP entry 0: NAPOT over all of memory, RWX, so U-mode
+	 * can fetch and access data.
+	 */
+	li	t0, -1
+	csrw	pmpaddr0, t0
+	li	t0, 0x1f
+	csrw	pmpcfg0, t0
+
+	/*
+	 * Enable the CBO instructions for U-mode and report what
+	 * menvcfg reads back.
+	 */
+	li	t0, MENVCFG_CB
+	csrs	CSR_MENVCFG, t0
+	csrr	s1, CSR_MENVCFG
+
+	// mret to U-mode at user_code (MPP = 0)
+	li	t0, 3 << 11
+	csrc	mstatus, t0
+	la	t0, user_code
+	csrw	mepc, t0
+	la	s2, cbo_step
+	mret
+
+user_code:
+	la 	a0, buffer       // address of buffer
+	li	t1, 1
+	sw	t1, 0(s2)
+	cbo.zero	(a0)
+	li	t1, 2
+	sw	t1, 0(s2)
+	cbo.clean	(a0)
+	li	t1, 3
+	sw	t1, 0(s2)
+	cbo.flush	(a0)
+	li	t1, 4
+	sw	t1, 0(s2)
+	cbo.inval	(a0)
+	ecall
+
+trap:
+	csrr s3, mcause
+	li t0, 8                   // environment call from U-mode
+	bne s3, t0, _fail
+	li	a0, 0
+	j	_exit
+
+_fail:
+	li	a0, 1
+	j	_exit
+
+# 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	64
+cbo_step:
+	.word 0
+buffer:
+	.fill 64, 1, 0xa5
+
+	.balign	16
+semiargs:
+	.space	16
+
-- 
2.43.0