Skip to content

Commit a1f44e0

Browse files
charmitroalistair23
authored andcommitted
tests/tcg/riscv64: Add test for MEPC bit masking
Add a regression test to verify that MEPC properly masks the lower bits when an address with mode bits is written to it, as required by the RISC-V Privileged Architecture specification. The test sets STVEC to an address with bit 0 set (vectored mode), triggers an illegal instruction exception, copies STVEC to MEPC in the trap handler, and verifies that MEPC masks bits [1:0] correctly for IALIGN=32. Without the fix, MEPC retains the mode bits (returns non-zero/FAIL). With the fix, MEPC clears bits [1:0] (returns 0/PASS). Signed-off-by: Charalampos Mitrodimas <[email protected]> Reviewed-by: Daniel Henrique Barboza <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent b345245 commit a1f44e0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

tests/tcg/riscv64/Makefile.softmmu-target

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ EXTRA_RUNS += run-issue1060
2020
run-issue1060: issue1060
2121
$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
2222

23+
EXTRA_RUNS += run-test-mepc-masking
24+
run-test-mepc-masking: test-mepc-masking
25+
$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
26+
2327
# We don't currently support the multiarch system tests
2428
undefine MULTIARCH_TESTS
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Test for MEPC masking bug fix
3+
*
4+
* This test verifies that MEPC properly masks the lower bits according
5+
* to the RISC-V specification when vectored mode bits from STVEC are
6+
* written to MEPC.
7+
*/
8+
9+
.option norvc
10+
11+
.text
12+
.global _start
13+
_start:
14+
/* Set up machine trap vector */
15+
lla t0, machine_trap_handler
16+
csrw mtvec, t0
17+
18+
/* Set STVEC with vectored mode (mode bits = 01) */
19+
li t0, 0x80004001
20+
csrw stvec, t0
21+
22+
/* Clear medeleg to handle exceptions in M-mode */
23+
csrw medeleg, zero
24+
25+
/* Trigger illegal instruction exception */
26+
.word 0xffffffff
27+
28+
test_completed:
29+
/* Exit with result in a0 */
30+
/* a0 = 0: success (bits [1:0] were masked) */
31+
/* a0 != 0: failure (some bits were not masked) */
32+
j _exit
33+
34+
machine_trap_handler:
35+
/* Check if illegal instruction (mcause = 2) */
36+
csrr t0, mcause
37+
li t1, 2
38+
bne t0, t1, skip_test
39+
40+
/* Test: Copy STVEC (with mode bits) to MEPC */
41+
csrr t0, stvec /* t0 = 0x80004001 */
42+
csrw mepc, t0 /* Write to MEPC */
43+
csrr t1, mepc /* Read back MEPC */
44+
45+
/* Check if bits [1:0] are masked (IALIGN=32 without RVC) */
46+
andi a0, t1, 3 /* a0 = 0 if both bits masked correctly */
47+
48+
/* Set correct return address */
49+
lla t0, test_completed
50+
csrw mepc, t0
51+
52+
skip_test:
53+
mret
54+
55+
/* Exit with semihosting */
56+
_exit:
57+
lla a1, semiargs
58+
li t0, 0x20026 /* ADP_Stopped_ApplicationExit */
59+
sd t0, 0(a1)
60+
sd a0, 8(a1)
61+
li a0, 0x20 /* TARGET_SYS_EXIT_EXTENDED */
62+
63+
/* Semihosting call sequence */
64+
.balign 16
65+
slli zero, zero, 0x1f
66+
ebreak
67+
srai zero, zero, 0x7
68+
j .
69+
70+
.data
71+
.balign 8
72+
semiargs:
73+
.space 16

0 commit comments

Comments
 (0)