[PATCH] target/riscv: make pmpcfg R=0|W=1 bit combo reserved [GL4820]

Daniel Henrique Barboza posted 1 patch 1 week, 2 days ago
target/riscv/tcg/pmp.c                      |  8 ++
tests/tcg/riscv64/system/meson.build        |  7 ++
tests/tcg/riscv64/test-pmpcfg-rw-reserved.S | 96 +++++++++++++++++++++
3 files changed, 111 insertions(+)
create mode 100644 tests/tcg/riscv64/test-pmpcfg-rw-reserved.S
[PATCH] target/riscv: make pmpcfg R=0|W=1 bit combo reserved [GL4820]
Posted by Daniel Henrique Barboza 1 week, 2 days ago
Priv spec says in the Physical Memory Protection CSRs section, w.r.t the
PMP configuration register:

"The R, W, and X fields form a collective WARL field for which the
combinations with R=0 and W=1 are reserved."

Smepmp changed the logic a bit: this bit combo is now reserved if there
is no smepmp or if mseccfg.MML is cleared.

Add this check in pmp_is_invalid_smepmp_cfg().

Fixes: 19cf1a7d9e59 ("target/riscv: pmp: fix checks on writes to pmpcfg in Smepmp MML mode")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4820
Signed-off-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
---
 target/riscv/tcg/pmp.c                      |  8 ++
 tests/tcg/riscv64/system/meson.build        |  7 ++
 tests/tcg/riscv64/test-pmpcfg-rw-reserved.S | 96 +++++++++++++++++++++
 3 files changed, 111 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-pmpcfg-rw-reserved.S

diff --git a/target/riscv/tcg/pmp.c b/target/riscv/tcg/pmp.c
index 04df53c234..021ec978b5 100644
--- a/target/riscv/tcg/pmp.c
+++ b/target/riscv/tcg/pmp.c
@@ -77,6 +77,14 @@ static int pmp_is_readonly(CPURISCVState *env, uint32_t pmp_index)
  */
 static int pmp_is_invalid_smepmp_cfg(CPURISCVState *env, uint8_t val)
 {
+    bool r_w_bits_reserved = !riscv_cpu_cfg(env)->ext_smepmp
+                             || !MSECCFG_MML_ISSET(env);
+
+    if (r_w_bits_reserved
+        && (val & (PMP_WRITE | PMP_READ)) == 0b10) {
+        return 1;
+    }
+
     /* No check if mseccfg.MML is not set or if mseccfg.RLB is set */
     if (!MSECCFG_MML_ISSET(env) || MSECCFG_RLB_ISSET(env)) {
         return 0;
diff --git a/tests/tcg/riscv64/system/meson.build b/tests/tcg/riscv64/system/meson.build
index 3c8b018d9e..4b905a0774 100644
--- a/tests/tcg/riscv64/system/meson.build
+++ b/tests/tcg/riscv64/system/meson.build
@@ -89,6 +89,13 @@ tests += {
   }
 }
 
+tests += {
+  'test-pmpcfg-rw-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-pmpcfg-rw-reserved.S b/tests/tcg/riscv64/test-pmpcfg-rw-reserved.S
new file mode 100644
index 0000000000..94a0d6f02f
--- /dev/null
+++ b/tests/tcg/riscv64/test-pmpcfg-rw-reserved.S
@@ -0,0 +1,96 @@
+/*
+ * Test reserved pmpcfg bits R and W.  Code derived from
+ * the Gitlab 4820 testcase.
+ *
+ * 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
+
+	/*
+	 * Write 0x2 (R=0, W=1) in pmp7cfg (last byte of pmpcfg0,
+	 * i.e. a 56 shift).  Store the result in s0.
+	 */
+	li	t0, 0xff
+	slli	t0, t0, 56
+	csrc	pmpcfg0, t0
+	li	t0, 0x02
+	slli	t0, t0, 56
+	csrs	pmpcfg0, t0	// pmp7cfg = 0x02
+	csrr	s0, pmpcfg0
+
+	/*
+	 * Likewise, now writing 0x6 (R=0, W=1, X=1) in pmp7cfg and
+	 * storing the result in s1.
+	 */
+	li	t0, 0xff
+	slli	t0, t0, 56
+	csrc	pmpcfg0, t0
+	li	t0, 0x06
+	slli	t0, t0, 56
+	csrs	pmpcfg0, t0	// pmp7cfg = 0x06
+	csrr	s1, pmpcfg0
+
+	li	t0, 0xff
+	slli	t0, t0, 56
+	csrc	pmpcfg0, t0
+
+	/*
+	 * rshift s0 to get to pmp7cfg and compare the first 2
+	 * bits (R and W) with 0x2.  They must be clear.
+	 */
+	srli t1, s0, 56
+	andi t1, t1, 3
+	li   t2, 2
+	beq  t1, t2, _fail
+
+	/* Do the same with s1 - R and W should be clear.  */
+	srli t1, s1, 56
+	andi t1, t1, 3
+	beq  t1, 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	64
+cbo_step:
+	.word 0
+buffer:
+	.fill 64, 1, 0xa5
+
+	.balign	16
+semiargs:
+	.space	16
+
-- 
2.43.0