Skip to contentSkip to conversation

target/riscv: Fix MEPC/SEPC bit masking

PATCH Applied Charalampos Mitrodimas 3 participants 7 messages Jul 3, 2025 – Jul 4, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

cover target/riscv: Fix MEPC/SEPC bit masking

This patch series fixes incorrect behavior in MEPC/SEPC CSRs where the lower bits were not properly masked according to the RISC-V specification.

The issue was discovered when vectored mode bits from STVEC were written to MEPC and not properly cleared, causing incorrect behavior on MRET.

Charalampos Mitrodimas (2):
target/riscv: Fix MEPC/SEPC bit masking for IALIGN tests/tcg/riscv64: Add test for MEPC bit masking

 target/riscv/csr.c                        |  8 +--
 target/riscv/internals.h                  | 11 ++++
 target/riscv/op_helper.c                  |  4 +-
 tests/tcg/riscv64/Makefile.softmmu-target |  4 ++
 tests/tcg/riscv64/test-mepc-masking.S     | 73 +++++++++++++++++++++++
 5 files changed, 94 insertions(+), 6 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-mepc-masking.S
Signature · 1 line
2.47.2
#1 lore raw

PATCH 1/2 target/riscv: Fix MEPC/SEPC bit masking for IALIGN

According to the RISC-V Privileged Architecture specification, the low bit of MEPC/SEPC must always be zero. When IALIGN=32, the two low bits must be zero.

This commit fixes the behavior of MEPC/SEPC CSR reads and writes, and the implicit reads by MRET/SRET instructions to properly mask the lowest bit(s) based on whether the C extension is enabled:
- When C extension is enabled (IALIGN=16): mask bit 0
- When C extension is disabled (IALIGN=32): mask bits [1:0]

Previously, when vectored mode bits from STVEC (which sets bit 0 for vectored mode) were written to MEPC, the bits would not be cleared correctly, causing incorrect behavior on MRET.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2855

---
 target/riscv/csr.c       |  8 ++++----
 target/riscv/internals.h | 11 +++++++++++
 target/riscv/op_helper.c |  4 ++--
 3 files changed, 17 insertions(+), 6 deletions(-)
target/riscv/csr.c +4 -4
index fb14972169..c33a6e86d2 100644
@@ -3126,14 +3126,14 @@ static RISCVException write_mscratch(CPURISCVState *env, int csrno,
 static RISCVException read_mepc(CPURISCVState *env, int csrno,
                                 target_ulong *val)
 {
-    *val = env->mepc;
+    *val = env->mepc & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
 static RISCVException write_mepc(CPURISCVState *env, int csrno,
                                  target_ulong val, uintptr_t ra)
 {
-    env->mepc = val;
+    env->mepc = val & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
@@ -4113,14 +4113,14 @@ static RISCVException write_sscratch(CPURISCVState *env, int csrno,
 static RISCVException read_sepc(CPURISCVState *env, int csrno,
                                 target_ulong *val)
 {
-    *val = env->sepc;
+    *val = env->sepc & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
 static RISCVException write_sepc(CPURISCVState *env, int csrno,
                                  target_ulong val, uintptr_t ra)
 {
-    env->sepc = val;
+    env->sepc = val & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
target/riscv/internals.h +11 -0
index 4570bd50be..89ac6a160f 100644
@@ -142,6 +142,17 @@ static inline float16 check_nanbox_h(CPURISCVState *env, uint64_t f)
     }
 }
 
+static inline target_ulong get_xepc_mask(CPURISCVState *env)
+{
+    /* When IALIGN=32, both low bits must be zero.
+     * When IALIGN=16 (has C extension), only bit 0 must be zero. */
+    if (riscv_has_ext(env, RVC)) {
+        return ~(target_ulong)1;
+    } else {
+        return ~(target_ulong)3;
+    }
+}
+
 #ifndef CONFIG_USER_ONLY
 /* Our implementation of SysemuCPUOps::has_work */
 bool riscv_cpu_has_work(CPUState *cs);
target/riscv/op_helper.c +2 -2
index 557807ba4b..15460bf84b 100644
@@ -280,7 +280,7 @@ target_ulong helper_sret(CPURISCVState *env)
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
     }
 
-    target_ulong retpc = env->sepc;
+    target_ulong retpc = env->sepc & get_xepc_mask(env);
     if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
                                     env->priv_ver,
                                     env->misa_ext) && (retpc & 0x3)) {
@@ -391,7 +391,7 @@ static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus,
 
 target_ulong helper_mret(CPURISCVState *env)
 {
-    target_ulong retpc = env->mepc;
+    target_ulong retpc = env->mepc & get_xepc_mask(env);
     uint64_t mstatus = env->mstatus;
     target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
 
Signature · 1 line
2.47.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] target/riscv: Fix MEPC/SEPC bit masking for IALIGN

Quoted 10 lines · Charalampos Mitrodimas

According to the RISC-V Privileged Architecture specification, the low bit of MEPC/SEPC must always be zero. When IALIGN=32, the two low bits must be zero.

This commit fixes the behavior of MEPC/SEPC CSR reads and writes, and the implicit reads by MRET/SRET instructions to properly mask the lowest bit(s) based on whether the C extension is enabled:
- When C extension is enabled (IALIGN=16): mask bit 0
- When C extension is disabled (IALIGN=32): mask bits [1:0]

Quoted 7 lines

Previously, when vectored mode bits from STVEC (which sets bit 0 for vectored mode) were written to MEPC, the bits would not be cleared correctly, causing incorrect behavior on MRET.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2855

---

Quoted 87 lines
  target/riscv/csr.c       |  8 ++++----
  target/riscv/internals.h | 11 +++++++++++
  target/riscv/op_helper.c |  4 ++--
  3 files changed, 17 insertions(+), 6 deletions(-)
target/riscv/csr.c +4 -4
index fb14972169..c33a6e86d2 100644
@@ -3126,14 +3126,14 @@ static RISCVException write_mscratch(CPURISCVState *env, int csrno,
  static RISCVException read_mepc(CPURISCVState *env, int csrno,
                                  target_ulong *val)
  {
-    *val = env->mepc;
+    *val = env->mepc & get_xepc_mask(env);
      return RISCV_EXCP_NONE;
  }
 
  static RISCVException write_mepc(CPURISCVState *env, int csrno,
                                   target_ulong val, uintptr_t ra)
  {
-    env->mepc = val;
+    env->mepc = val & get_xepc_mask(env);
      return RISCV_EXCP_NONE;
  }
 
@@ -4113,14 +4113,14 @@ static RISCVException write_sscratch(CPURISCVState *env, int csrno,
  static RISCVException read_sepc(CPURISCVState *env, int csrno,
                                  target_ulong *val)
  {
-    *val = env->sepc;
+    *val = env->sepc & get_xepc_mask(env);
      return RISCV_EXCP_NONE;
  }
 
  static RISCVException write_sepc(CPURISCVState *env, int csrno,
                                   target_ulong val, uintptr_t ra)
  {
-    env->sepc = val;
+    env->sepc = val & get_xepc_mask(env);
      return RISCV_EXCP_NONE;
  }
 
target/riscv/internals.h +11 -0
index 4570bd50be..89ac6a160f 100644
@@ -142,6 +142,17 @@ static inline float16 check_nanbox_h(CPURISCVState *env, uint64_t f)
      }
  }
 
+static inline target_ulong get_xepc_mask(CPURISCVState *env)
+{
+    /* When IALIGN=32, both low bits must be zero.
+     * When IALIGN=16 (has C extension), only bit 0 must be zero. */
+    if (riscv_has_ext(env, RVC)) {
+        return ~(target_ulong)1;
+    } else {
+        return ~(target_ulong)3;
+    }
+}
+
  #ifndef CONFIG_USER_ONLY
  /* Our implementation of SysemuCPUOps::has_work */
  bool riscv_cpu_has_work(CPUState *cs);
target/riscv/op_helper.c +2 -2
index 557807ba4b..15460bf84b 100644
@@ -280,7 +280,7 @@ target_ulong helper_sret(CPURISCVState *env)
          riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
      }
 
-    target_ulong retpc = env->sepc;
+    target_ulong retpc = env->sepc & get_xepc_mask(env);
      if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
                                      env->priv_ver,
                                      env->misa_ext) && (retpc & 0x3)) {
@@ -391,7 +391,7 @@ static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus,
 
  target_ulong helper_mret(CPURISCVState *env)
  {
-    target_ulong retpc = env->mepc;
+    target_ulong retpc = env->mepc & get_xepc_mask(env);
      uint64_t mstatus = env->mstatus;
      target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
 
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] target/riscv: Fix MEPC/SEPC bit masking for IALIGN

In reply to Charalampos Mitrodimas
Quoted 11 lines · Charalampos Mitrodimas

According to the RISC-V Privileged Architecture specification, the low bit of MEPC/SEPC must always be zero. When IALIGN=32, the two low bits must be zero.

This commit fixes the behavior of MEPC/SEPC CSR reads and writes, and the implicit reads by MRET/SRET instructions to properly mask the lowest bit(s) based on whether the C extension is enabled:
- When C extension is enabled (IALIGN=16): mask bit 0
- When C extension is disabled (IALIGN=32): mask bits [1:0]

Quoted 6 lines

Previously, when vectored mode bits from STVEC (which sets bit 0 for vectored mode) were written to MEPC, the bits would not be cleared correctly, causing incorrect behavior on MRET.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2855

Alistair

Quoted 92 lines
---
 target/riscv/csr.c       |  8 ++++----
 target/riscv/internals.h | 11 +++++++++++
 target/riscv/op_helper.c |  4 ++--
 3 files changed, 17 insertions(+), 6 deletions(-)
target/riscv/csr.c +4 -4
index fb14972169..c33a6e86d2 100644
@@ -3126,14 +3126,14 @@ static RISCVException write_mscratch(CPURISCVState *env, int csrno,
 static RISCVException read_mepc(CPURISCVState *env, int csrno,
                                 target_ulong *val)
 {
-    *val = env->mepc;
+    *val = env->mepc & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
 static RISCVException write_mepc(CPURISCVState *env, int csrno,
                                  target_ulong val, uintptr_t ra)
 {
-    env->mepc = val;
+    env->mepc = val & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
@@ -4113,14 +4113,14 @@ static RISCVException write_sscratch(CPURISCVState *env, int csrno,
 static RISCVException read_sepc(CPURISCVState *env, int csrno,
                                 target_ulong *val)
 {
-    *val = env->sepc;
+    *val = env->sepc & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
 static RISCVException write_sepc(CPURISCVState *env, int csrno,
                                  target_ulong val, uintptr_t ra)
 {
-    env->sepc = val;
+    env->sepc = val & get_xepc_mask(env);
     return RISCV_EXCP_NONE;
 }
 
target/riscv/internals.h +11 -0
index 4570bd50be..89ac6a160f 100644
@@ -142,6 +142,17 @@ static inline float16 check_nanbox_h(CPURISCVState *env, uint64_t f)
     }
 }
 
+static inline target_ulong get_xepc_mask(CPURISCVState *env)
+{
+    /* When IALIGN=32, both low bits must be zero.
+     * When IALIGN=16 (has C extension), only bit 0 must be zero. */
+    if (riscv_has_ext(env, RVC)) {
+        return ~(target_ulong)1;
+    } else {
+        return ~(target_ulong)3;
+    }
+}
+
 #ifndef CONFIG_USER_ONLY
 /* Our implementation of SysemuCPUOps::has_work */
 bool riscv_cpu_has_work(CPUState *cs);
target/riscv/op_helper.c +2 -3
index 557807ba4b..15460bf84b 100644
@@ -280,7 +280,7 @@ target_ulong helper_sret(CPURISCVState *env)
         riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
     }
 
-    target_ulong retpc = env->sepc;
+    target_ulong retpc = env->sepc & get_xepc_mask(env);
     if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
                                     env->priv_ver,
                                     env->misa_ext) && (retpc & 0x3)) {
@@ -391,7 +391,7 @@ static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus,
 
 target_ulong helper_mret(CPURISCVState *env)
 {
-    target_ulong retpc = env->mepc;
+    target_ulong retpc = env->mepc & get_xepc_mask(env);
     uint64_t mstatus = env->mstatus;
     target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
 
--

2.47.2

#4 lore raw

PATCH 2/2 tests/tcg/riscv64: Add test for MEPC bit masking

In reply to Charalampos Mitrodimas

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).

---
 tests/tcg/riscv64/Makefile.softmmu-target |  4 ++
 tests/tcg/riscv64/test-mepc-masking.S     | 73 +++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-mepc-masking.S
tests/tcg/riscv64/Makefile.softmmu-target +4 -0
index 7c1d44d3f4..3ca595335d 100644
@@ -20,5 +20,9 @@ EXTRA_RUNS += run-issue1060
 run-issue1060: issue1060
 	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
 
+EXTRA_RUNS += run-test-mepc-masking
+run-test-mepc-masking: test-mepc-masking
+	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
+
 # We don't currently support the multiarch system tests
 undefine MULTIARCH_TESTS
tests/tcg/riscv64/test-mepc-masking.S new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
2.47.2
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] tests/tcg/riscv64: Add test for MEPC bit masking

Quoted 9 lines · Charalampos Mitrodimas

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.

Quoted 5 lines

Without the fix, MEPC retains the mode bits (returns non-zero/FAIL). With the fix, MEPC clears bits [1:0] (returns 0/PASS).

---

The additional test is appreciated. Thanks!

Quoted 98 lines
  tests/tcg/riscv64/Makefile.softmmu-target |  4 ++
  tests/tcg/riscv64/test-mepc-masking.S     | 73 +++++++++++++++++++++++
  2 files changed, 77 insertions(+)

create mode 100644 tests/tcg/riscv64/test-mepc-masking.S

tests/tcg/riscv64/Makefile.softmmu-target +4 -0
index 7c1d44d3f4..3ca595335d 100644
@@ -20,5 +20,9 @@ EXTRA_RUNS += run-issue1060
  run-issue1060: issue1060
  	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
 
+EXTRA_RUNS += run-test-mepc-masking
+run-test-mepc-masking: test-mepc-masking
+	$(call run-test, $<, $(QEMU) $(QEMU_OPTS)$<)
+
  # We don't currently support the multiarch system tests
  undefine MULTIARCH_TESTS
tests/tcg/riscv64/test-mepc-masking.S new file +73 -0
new file mode 100644
index 0000000000..fccd2a7ac4
@@ -0,0 +1,73 @@
+/*
+ * Test for MEPC masking bug fix
+ *
+ * This test verifies that MEPC properly masks the lower bits according
+ * to the RISC-V specification when vectored mode bits from STVEC are
+ * written to MEPC.
+ */
+
+	.option	norvc
+
+	.text
+	.global _start
+_start:
+	/* Set up machine trap vector */
+	lla	t0, machine_trap_handler
+	csrw	mtvec, t0
+
+	/* Set STVEC with vectored mode (mode bits = 01) */
+	li	t0, 0x80004001
+	csrw	stvec, t0
+
+	/* Clear medeleg to handle exceptions in M-mode */
+	csrw	medeleg, zero
+
+	/* Trigger illegal instruction exception */
+	.word	0xffffffff
+
+test_completed:
+	/* Exit with result in a0 */
+	/* a0 = 0: success (bits [1:0] were masked) */
+	/* a0 != 0: failure (some bits were not masked) */
+	j	_exit
+
+machine_trap_handler:
+	/* Check if illegal instruction (mcause = 2) */
+	csrr	t0, mcause
+	li	t1, 2
+	bne	t0, t1, skip_test
+
+	/* Test: Copy STVEC (with mode bits) to MEPC */
+	csrr	t0, stvec	/* t0 = 0x80004001 */
+	csrw	mepc, t0	/* Write to MEPC */
+	csrr	t1, mepc	/* Read back MEPC */
+
+	/* Check if bits [1:0] are masked (IALIGN=32 without RVC) */
+	andi	a0, t1, 3	/* a0 = 0 if both bits masked correctly */
+
+	/* Set correct return address */
+	lla	t0, test_completed
+	csrw	mepc, t0
+
+skip_test:
+	mret
+
+/* Exit with semihosting */
+_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	8
+semiargs:
+	.space	16
#6 lore raw
In reply to Charalampos Mitrodimas
Quoted 11 lines · Charalampos Mitrodimas

This patch series fixes incorrect behavior in MEPC/SEPC CSRs where the lower bits were not properly masked according to the RISC-V specification.

The issue was discovered when vectored mode bits from STVEC were written to MEPC and not properly cleared, causing incorrect behavior on MRET.

Charalampos Mitrodimas (2):
target/riscv: Fix MEPC/SEPC bit masking for IALIGN tests/tcg/riscv64: Add test for MEPC bit masking

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 13 lines
 target/riscv/csr.c                        |  8 +--
 target/riscv/internals.h                  | 11 ++++
 target/riscv/op_helper.c                  |  4 +-
 tests/tcg/riscv64/Makefile.softmmu-target |  4 ++
 tests/tcg/riscv64/test-mepc-masking.S     | 73 +++++++++++++++++++++++
 5 files changed, 94 insertions(+), 6 deletions(-)
 create mode 100644 tests/tcg/riscv64/test-mepc-masking.S

--
2.47.2