Skip to content

Commit 22dc925

Browse files
committed
Raise exception when RV32E instructions use x16-x31
Modify rv_decode() to enforce RV32E register constraints by validating rd, rs1, rs2, and rs3, 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 798ffcd commit 22dc925

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/decode.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,16 @@ bool rv_decode(rv_insn_t *ir, uint32_t insn)
20262026
/* decode instruction (compressed instructions) */
20272027
const decode_t op = rvc_jump_table[c_index];
20282028
assert(op);
2029+
#if !RV32_HAS(RV32E)
20292030
return op(ir, insn);
2031+
#else
2032+
if (!op(ir, insn))
2033+
return false;
2034+
2035+
if (ir->rd > 15 || ir->rs1 > 15 || ir->rs2 > 15 || ir->rs3 > 15)
2036+
return false;
2037+
return true;
2038+
#endif
20302039
}
20312040
#endif
20322041

@@ -2036,7 +2045,14 @@ bool rv_decode(rv_insn_t *ir, uint32_t insn)
20362045
/* decode instruction */
20372046
const decode_t op = rv_jump_table[index];
20382047
assert(op);
2039-
return op(ir, insn);
2048+
if (!op(ir, insn))
2049+
return false;
2050+
2051+
#if RV32_HAS(RV32E)
2052+
if (ir->rd > 15 || ir->rs1 > 15 || ir->rs2 > 15 || ir->rs3 > 15)
2053+
return false;
2054+
#endif
2055+
return true;
20402056

20412057
#undef OP_UNIMP
20422058
#undef OP

0 commit comments

Comments
 (0)