Skip to content

Commit b345245

Browse files
charmitroalistair23
authored andcommitted
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 Signed-off-by: Charalampos Mitrodimas <charmitro@posteo.net> Reviewed-by: Alistair Francis <alistair.francis@wdc.com> Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com> Message-ID: <20250703182157.281320-2-charmitro@posteo.net> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
1 parent bc22001 commit b345245

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

target/riscv/csr.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,14 +3129,14 @@ static RISCVException write_mscratch(CPURISCVState *env, int csrno,
31293129
static RISCVException read_mepc(CPURISCVState *env, int csrno,
31303130
target_ulong *val)
31313131
{
3132-
*val = env->mepc;
3132+
*val = env->mepc & get_xepc_mask(env);
31333133
return RISCV_EXCP_NONE;
31343134
}
31353135

31363136
static RISCVException write_mepc(CPURISCVState *env, int csrno,
31373137
target_ulong val, uintptr_t ra)
31383138
{
3139-
env->mepc = val;
3139+
env->mepc = val & get_xepc_mask(env);
31403140
return RISCV_EXCP_NONE;
31413141
}
31423142

@@ -4169,14 +4169,14 @@ static RISCVException write_sscratch(CPURISCVState *env, int csrno,
41694169
static RISCVException read_sepc(CPURISCVState *env, int csrno,
41704170
target_ulong *val)
41714171
{
4172-
*val = env->sepc;
4172+
*val = env->sepc & get_xepc_mask(env);
41734173
return RISCV_EXCP_NONE;
41744174
}
41754175

41764176
static RISCVException write_sepc(CPURISCVState *env, int csrno,
41774177
target_ulong val, uintptr_t ra)
41784178
{
4179-
env->sepc = val;
4179+
env->sepc = val & get_xepc_mask(env);
41804180
return RISCV_EXCP_NONE;
41814181
}
41824182

target/riscv/internals.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ static inline float16 check_nanbox_bf16(CPURISCVState *env, uint64_t f)
158158
}
159159
}
160160

161+
static inline target_ulong get_xepc_mask(CPURISCVState *env)
162+
{
163+
/* When IALIGN=32, both low bits must be zero.
164+
* When IALIGN=16 (has C extension), only bit 0 must be zero. */
165+
if (riscv_has_ext(env, RVC)) {
166+
return ~(target_ulong)1;
167+
} else {
168+
return ~(target_ulong)3;
169+
}
170+
}
171+
161172
#ifndef CONFIG_USER_ONLY
162173
/* Our implementation of SysemuCPUOps::has_work */
163174
bool riscv_cpu_has_work(CPUState *cs);

target/riscv/op_helper.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ target_ulong helper_sret(CPURISCVState *env)
280280
riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
281281
}
282282

283-
target_ulong retpc = env->sepc;
283+
target_ulong retpc = env->sepc & get_xepc_mask(env);
284284
if (!riscv_cpu_allow_16bit_insn(&env_archcpu(env)->cfg,
285285
env->priv_ver,
286286
env->misa_ext) && (retpc & 0x3)) {
@@ -391,7 +391,7 @@ static target_ulong ssdbltrp_mxret(CPURISCVState *env, target_ulong mstatus,
391391

392392
target_ulong helper_mret(CPURISCVState *env)
393393
{
394-
target_ulong retpc = env->mepc;
394+
target_ulong retpc = env->mepc & get_xepc_mask(env);
395395
uint64_t mstatus = env->mstatus;
396396
target_ulong prev_priv = get_field(mstatus, MSTATUS_MPP);
397397

0 commit comments

Comments
 (0)