All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib: sbi_illegal_insn: Emulate 'MZ'/c.li s4,-13 instruction
@ 2023-11-08 19:13 Björn Töpel
  2023-11-08 21:55 ` Andreas Schwab
  0 siblings, 1 reply; 12+ messages in thread
From: Björn Töpel @ 2023-11-08 19:13 UTC (permalink / raw)
  To: opensbi

From: Bj?rn T?pel <bjorn@rivosinc.com>

The Linux kernel RISC-V image format allows that UEFI Images can also
be booted by non-UEFI firmware. However for that to work, the PE/Image
combo requires that 'MZ' is a valid instruction. On RISC-V, 'MZ' is
only a valid instruction if the hardware is C capable [1].

Emulate "c.li s4,-13" for non-C capable hardware.

Link: https://lore.kernel.org/linux-riscv/20231024192648.25527-1-bjorn at kernel.org/ # [1]
Signed-off-by: Bj?rn T?pel <bjorn@rivosinc.com>
---
 lib/sbi/sbi_illegal_insn.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/lib/sbi/sbi_illegal_insn.c b/lib/sbi/sbi_illegal_insn.c
index 2be47575a365..a3ade71295ce 100644
--- a/lib/sbi/sbi_illegal_insn.c
+++ b/lib/sbi/sbi_illegal_insn.c
@@ -102,6 +102,19 @@ static int system_opcode_insn(ulong insn, struct sbi_trap_regs *regs)
 	return 0;
 }
 
+static int compressed_insn(ulong insn, struct sbi_trap_regs *regs)
+{
+	/* Only handle 'MZ'/c.li s4,-13/0x5a3d */
+	if (!misa_extension('C') && (insn & 0xffff) == 0x5a4d) {
+		regs->s4 = -13;
+		regs->mepc += 4;
+
+		return 0;
+	}
+
+	return truly_illegal_insn(insn, regs);
+}
+
 static const illegal_insn_func illegal_insn_table[32] = {
 	truly_illegal_insn, /* 0 */
 	truly_illegal_insn, /* 1 */
@@ -140,6 +153,7 @@ static const illegal_insn_func illegal_insn_table[32] = {
 int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 {
 	struct sbi_trap_info uptrap;
+	ulong tmp;
 
 	/*
 	 * We only deal with 32-bit (or longer) illegal instructions. If we
@@ -159,7 +173,11 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 			uptrap.epc = regs->mepc;
 			return sbi_trap_redirect(regs, &uptrap);
 		}
-		if ((insn & 3) != 3)
+
+		tmp = insn & 3;
+		if (tmp == 1)
+			return compressed_insn(insn, regs);
+		else if (tmp != 3)
 			return truly_illegal_insn(insn, regs);
 	}
 

base-commit: cbdd86973901b6be2a1a2d3d6b54f3184fdf9a44
-- 
2.40.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-11-09  7:54 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-08 19:13 [PATCH] lib: sbi_illegal_insn: Emulate 'MZ'/c.li s4,-13 instruction Björn Töpel
2023-11-08 21:55 ` Andreas Schwab
2023-11-09  0:02   ` Björn Töpel
2023-11-09  0:11     ` Jessica Clarke
2023-11-09  0:22       ` Björn Töpel
2023-11-09  4:04         ` Xiang W
2023-11-09  6:24           ` Jessica Clarke
2023-11-09  6:55             ` Xiang W
2023-11-09  7:00               ` Jessica Clarke
2023-11-09  7:10                 ` Xiang W
2023-11-09  7:19                   ` Jessica Clarke
2023-11-09  7:54                     ` Inochi Amaoto

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.