From: Björn Töpel <bjorn@kernel.org>
To: opensbi@lists.infradead.org
Subject: [PATCH] lib: sbi_illegal_insn: Emulate 'MZ'/c.li s4,-13 instruction
Date: Wed, 8 Nov 2023 20:13:10 +0100 [thread overview]
Message-ID: <20231108191310.49950-1-bjorn@kernel.org> (raw)
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
next reply other threads:[~2023-11-08 19:13 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-08 19:13 Björn Töpel [this message]
2023-11-08 21:55 ` [PATCH] lib: sbi_illegal_insn: Emulate 'MZ'/c.li s4,-13 instruction 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231108191310.49950-1-bjorn@kernel.org \
--to=bjorn@kernel.org \
--cc=opensbi@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox