Skip to content

Commit cf29716

Browse files
committed
Raise exception when RV32E instructions use x16-x31
Modify rv_decode() to enforce RV32E register constraints by validating rd, rs1, rs2, ensuring they remain within x0-x15. If an instruction attempts to use x16-x31, trigger an illegal instruction exception, aligning with RISC-V privileged architecture behavior. According to The RISC-V Instruction Set Manual Volume I: Unprivileged Architecture, Version 20240411: "RV32E and RV64E ... only registers x0-x15 are provided. All encodings specifying the other registers x16-x31 are reserved."
1 parent c78b27e commit cf29716

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/decode.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,7 @@ typedef bool (*decode_t)(rv_insn_t *ir, uint32_t insn);
19831983
/* decode RISC-V instruction */
19841984
bool rv_decode(rv_insn_t *ir, uint32_t insn)
19851985
{
1986+
bool ret;
19861987
assert(ir);
19871988

19881989
#define OP_UNIMP op_unimp
@@ -2026,7 +2027,9 @@ bool rv_decode(rv_insn_t *ir, uint32_t insn)
20262027
/* decode instruction (compressed instructions) */
20272028
const decode_t op = rvc_jump_table[c_index];
20282029
assert(op);
2029-
return op(ir, insn);
2030+
ret = op(ir, insn);
2031+
2032+
goto end;
20302033
}
20312034
#endif
20322035

@@ -2036,7 +2039,19 @@ bool rv_decode(rv_insn_t *ir, uint32_t insn)
20362039
/* decode instruction */
20372040
const decode_t op = rv_jump_table[index];
20382041
assert(op);
2039-
return op(ir, insn);
2042+
ret = op(ir, insn);
2043+
2044+
end:
2045+
2046+
#if RV32_HAS(RV32E)
2047+
/* Trigger illegal instruction exception if RV32E uses x16-x31 (float regs
2048+
* excluded). */
2049+
if ((op != op_store_fp && op != op_load_fp && op != op_op_fp) &&
2050+
unlikely(ir->rd > 15 || ir->rs1 > 15 || ir->rs2 > 15))
2051+
ret = false;
2052+
#endif
2053+
2054+
return ret;
20402055

20412056
#undef OP_UNIMP
20422057
#undef OP

0 commit comments

Comments
 (0)