[PATCH 2/2] lib: sbi: Disable compressed instructions in sbi_get_insn()
Ilya Mamay
mmamayka01 at gmail.com
Tue Nov 24 04:49:00 PST 2026
When an exception occurs during unprivileged memory access,
sbi_hart_expected_trap() skips the faulting instruction by
unconditionally advancing `mepc` by 4 bytes.
If the compiler with Zcb enabled emits c.lhu inside the inline
assembly block advancing `mepc` by 4 bytes on a trap will land
in the middle of a subsequent 32-bit instruction, corrupting
the execution flow.
Wrap the inline assembly block with `.option push`, `.option norvc`,
and `.option pop` to guarantee that all instructions inside are
32-bit wide.
Signed-off-by: Ilya Mamay <mmamayka01 at gmail.com>
---
lib/sbi/sbi_unpriv.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/lib/sbi/sbi_unpriv.c b/lib/sbi/sbi_unpriv.c
index 3fd9d9ff..c265d54f 100644
--- a/lib/sbi/sbi_unpriv.c
+++ b/lib/sbi/sbi_unpriv.c
@@ -213,6 +213,8 @@ ulong sbi_get_insn(ulong mepc, struct sbi_trap_info *trap)
trap->cause = 0;
asm volatile(
+ ".option push\n"
+ ".option norvc\n"
"add %[tinfo], %[taddr], zero\n"
"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n"
"csrrs %[mstatus], " STR(CSR_MSTATUS) ", %[mprv]\n"
@@ -224,7 +226,8 @@ ulong sbi_get_insn(ulong mepc, struct sbi_trap_info *trap)
"sll %[ttmp], %[ttmp], 16\n"
"add %[insn], %[insn], %[ttmp]\n"
"2: csrw " STR(CSR_MSTATUS) ", %[mstatus]\n"
- "csrw " STR(CSR_MTVEC) ", %[mtvec]"
+ "csrw " STR(CSR_MTVEC) ", %[mtvec]\n"
+ ".option pop\n"
: [mstatus] "+&r"(mstatus), [mtvec] "+&r"(mtvec),
[tinfo] "+&r"(tinfo), [ttmp] "+&r"(ttmp),
[insn] "+&r"(insn)
--
2.43.0
More information about the opensbi
mailing list