Skip to contentSkip to conversation

lib: sbi: fix system_opcode_insn

PATCH Superseded Xiang W 2 participants 2 messages May 26, 2022

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH v3 1/1 lib: sbi: fix system_opcode_insn

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

---
Changes in v3:
- move checking prev_mode before sbi_emulate_csr_read and return failure by Anup's suggestion

Changes in v2:
- add missing head file

 lib/sbi/sbi_illegal_insn.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/sbi/sbi_illegal_insn.c +7 -0
index bfe7d61..7976957 100644
@@ -16,6 +16,7 @@
 #include <sbi/sbi_pmu.h>
 #include <sbi/sbi_trap.h>
 #include <sbi/sbi_unpriv.h>
+#include <sbi/sbi_console.h>
 
 typedef int (*illegal_insn_func)(ulong insn, struct sbi_trap_regs *regs);
 
@@ -37,8 +38,14 @@ 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;
 
+	if (prev_mode == PRV_M) {
+		sbi_printf("%s: Unknown CSR %#x", __func__, csr_num);
+		return SBI_EFAIL;
+	}
+
 	/* TODO: Ensure that we got CSR read/write instruction */
 
 	if (sbi_emulate_csr_read(csr_num, regs, &csr_val))
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/1 lib: sbi: fix system_opcode_insn

Quoted 29 lines · Xiang W

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

---
Changes in v3:
- move checking prev_mode before sbi_emulate_csr_read and return failure by Anup's suggestion

Changes in v2:
- add missing head file

 lib/sbi/sbi_illegal_insn.c | 7 +++++++
 1 file changed, 7 insertions(+)
lib/sbi/sbi_illegal_insn.c +1 -0
index bfe7d61..7976957 100644
@@ -16,6 +16,7 @@
 #include <sbi/sbi_pmu.h>
 #include <sbi/sbi_trap.h>
 #include <sbi/sbi_unpriv.h>
+#include <sbi/sbi_console.h>
 
 typedef int (*illegal_insn_func)(ulong insn, struct sbi_trap_regs *regs);
 

lib/sbi/sbi_illegal_insn.c static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs) · line 45

Quoted 8 lines
lib/sbi/sbi_illegal_insn.c +3 -0
@@ -37,8 +38,14 @@ 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;
 
+       if (prev_mode == PRV_M) {
+               sbi_printf("%s: Unknown CSR %#x", __func__, csr_num);

This print statement is a bit misleading.

Maybe something like below is better:
sbi_printf("%s: CSR %#x access from M-mode", __func__, csr_num);

Quoted 9 lines
lib/sbi/sbi_illegal_insn.c +3 -1
+               return SBI_EFAIL;
+       }
+
        /* TODO: Ensure that we got CSR read/write instruction */
 
        if (sbi_emulate_csr_read(csr_num, regs, &csr_val))
--

2.30.2

Apart from above, this looks good to me.

Regards,
Anup