[PATCH] lib: sbi: fix system_opcode_insn

Xiang W wxjstz at 126.com
Tue May 24 10:44:32 PDT 2022


If the csr's operation comes from M mode, it should not be forwarded
to low-privilege processing, this patch fixes this problem.

Signed-off-by: Xiang W <wxjstz at 126.com>
---
 lib/sbi/sbi_illegal_insn.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/sbi/sbi_illegal_insn.c b/lib/sbi/sbi_illegal_insn.c
index bfe7d61..deb77e6 100644
--- a/lib/sbi/sbi_illegal_insn.c
+++ b/lib/sbi/sbi_illegal_insn.c
@@ -37,12 +37,16 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
 	int do_write, rs1_num = (insn >> 15) & 0x1f;
 	ulong rs1_val = GET_RS1(insn, regs);
 	int csr_num   = (u32)insn >> 20;
+	ulong prev_mode = (regs->mstatus & MSTATUS_MPP) >> MSTATUS_MPP_SHIFT;
 	ulong csr_val, new_csr_val;
 
 	/* TODO: Ensure that we got CSR read/write instruction */
 
-	if (sbi_emulate_csr_read(csr_num, regs, &csr_val))
+	if (sbi_emulate_csr_read(csr_num, regs, &csr_val)) {
+		if (prev_mode == PRV_M)
+			sbi_panic("%s: Unknown CSR %#x", __func__, csr_num);
 		return truly_illegal_insn(insn, regs);
+	}
 
 	do_write = rs1_num;
 	switch (GET_RM(insn)) {
-- 
2.30.2




More information about the opensbi mailing list