Skip to contentSkip to conversation

lib: sbi_illegal_insn: Emulate c.li

PATCH New Xiang W 3 participants 7 messages Nov 9, 2023

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

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]. So add Emulate c.li

---
 lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +17 -1
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+	unsigned long imm, rd;
+	unsigned long *regs_p = (unsigned long *)regs;
+
+	if ((insn & 0xe003) == 0x4001) { /* c.li */
+		imm = (insn >> 2) & 0x1f;
+		imm |= ((insn >> 12) & 1) ? -32 : 0;
+		rd = (insn >> 7) & 0x1f;
+		if (rd)
+			regs_p[rd] = imm;
+		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 */
@@ -160,7 +176,7 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
-			return truly_illegal_insn(insn, regs);
+			return compressed_insn(insn, regs);
 	}
 
 	return illegal_insn_table[(insn & 0x7c) >> 2](insn, regs);
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

Quoted 49 lines

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]. So add Emulate c.li

---
lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++-

1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +17 -2
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+	unsigned long imm, rd;
+	unsigned long *regs_p = (unsigned long *)regs;
+
+	if ((insn & 0xe003) == 0x4001) { /* c.li */
+		imm = (insn >> 2) & 0x1f;
+		imm |= ((insn >> 12) & 1) ? -32 : 0;
+		rd = (insn >> 7) & 0x1f;
+		if (rd)
+			regs_p[rd] = imm;
+		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 */
@@ -160,7 +176,7 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
-			return truly_illegal_insn(insn, regs);
+			return compressed_insn(insn, regs);
 	}
 
 	return illegal_insn_table[(insn & 0x7c) >> 2](insn, regs);
--
Quoted 2 lines

2.42.0

I think this patch may be unnecessary.

After searching the boot flow of UEFI, I only found two implementations:

For QEMU RISC-V virt platform and SG2042 platform:
SBI -> (SEC -> PEI -> DXE) -> bootloader -> OS
UEFI

For SiFive U540 platform:
(SEC -> OpenSBI -> PEI -> DXE) -> bootloader -> OS
UEFI

Both invoke SBI at a very early stage and use raw vaild address, and handling the UEFI header is more like bootloader's job. I have no idea about why this should be handled by SBI. FYI, Bj?rn T?pel.

author
#2 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

? 2023-11-09???? 17:35 +0800?Inochi Amaoto???

Quoted 60 lines
Quoted 49 lines

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]. So add Emulate c.li

---
lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++-

1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +17 -2
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+	unsigned long imm, rd;
+	unsigned long *regs_p = (unsigned long *)regs;
+
+	if ((insn & 0xe003) == 0x4001) { /* c.li */
+		imm = (insn >> 2) & 0x1f;
+		imm |= ((insn >> 12) & 1) ? -32 : 0;
+		rd = (insn >> 7) & 0x1f;
+		if (rd)
+			regs_p[rd] = imm;
+		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 */
@@ -160,7 +176,7 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
-			return truly_illegal_insn(insn, regs);
+			return compressed_insn(insn, regs);
 	}
 
 	return illegal_insn_table[(insn & 0x7c) >> 2](insn, regs);
--
Quoted 2 lines

2.42.0

I think this patch may be unnecessary.

After searching the boot flow of UEFI, I only found two implementations:

For QEMU RISC-V virt platform and SG2042 platform:
SBI -> (SEC -> PEI -> DXE) -> bootloader -> OS ?????????????? UEFI

Quoted 7 lines

For SiFive U540 platform:
(SEC -> OpenSBI -> PEI -> DXE) -> bootloader -> OS ???????????? UEFI

Both invoke SBI at a very early stage and use raw vaild address, and handling the UEFI header is more like bootloader's job. I have no idea about why this should be handled by SBI. FYI, Bj?rn T?pel.

This patch is used to solve the problem of non-UEFI firmware booting the kernel. The UEFI boot kernel should parse the PE file header, so this patch is not needed. But non-UEFI probably treats the kernel as a binary image.

Regards,
Xiang W

#3 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

Quoted 65 lines

? 2023-11-09???? 17:35 +0800?Inochi Amaoto???

Quoted 64 lines
Quoted 49 lines

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]. So add Emulate c.li

---
lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++-

1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +17 -2
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+	unsigned long imm, rd;
+	unsigned long *regs_p = (unsigned long *)regs;
+
+	if ((insn & 0xe003) == 0x4001) { /* c.li */
+		imm = (insn >> 2) & 0x1f;
+		imm |= ((insn >> 12) & 1) ? -32 : 0;
+		rd = (insn >> 7) & 0x1f;
+		if (rd)
+			regs_p[rd] = imm;
+		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 */
@@ -160,7 +176,7 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
-			return truly_illegal_insn(insn, regs);
+			return compressed_insn(insn, regs);
 	}
 
 	return illegal_insn_table[(insn & 0x7c) >> 2](insn, regs);
--
Quoted 2 lines

2.42.0

I think this patch may be unnecessary.

After searching the boot flow of UEFI, I only found two implementations:

For QEMU RISC-V virt platform and SG2042 platform:
SBI -> (SEC -> PEI -> DXE) -> bootloader -> OS
UEFI

For SiFive U540 platform:
(SEC -> OpenSBI -> PEI -> DXE) -> bootloader -> OS
UEFI

Quoted 7 lines
Quoted 3 lines

Both invoke SBI at a very early stage and use raw vaild address, and handling the UEFI header is more like bootloader's job. I have no idea about why this should be handled by SBI. FYI, Bj?rn T?pel.

This patch is used to solve the problem of non-UEFI firmware booting the kernel. The UEFI boot kernel should parse the PE file header, so this patch is not needed. But non-UEFI probably treats the kernel as a binary image.

This is not a vaild scenario for UEFI binary. IIRC, even the aarch64 arch does not support using UEFI binary in this way. I agree with Jessica about the fact that they are different. If you insist, I suggest using a custom bootloader to handle this.

Quoted 5 lines

Regards,
Xiang W

author
#4 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

? 2023-11-09???? 19:20 +0800?Inochi Amaoto???

Quoted 73 lines
Quoted 65 lines

? 2023-11-09???? 17:35 +0800?Inochi Amaoto???

Quoted 64 lines
Quoted 49 lines

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]. So add Emulate c.li

---
lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++-

1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +17 -2
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+	unsigned long imm, rd;
+	unsigned long *regs_p = (unsigned long *)regs;
+
+	if ((insn & 0xe003) == 0x4001) { /* c.li */
+		imm = (insn >> 2) & 0x1f;
+		imm |= ((insn >> 12) & 1) ? -32 : 0;
+		rd = (insn >> 7) & 0x1f;
+		if (rd)
+			regs_p[rd] = imm;
+		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 */
@@ -160,7 +176,7 @@ int sbi_illegal_insn_handler(ulong insn, struct sbi_trap_regs *regs)
 			return sbi_trap_redirect(regs, &uptrap);
 		}
 		if ((insn & 3) != 3)
-			return truly_illegal_insn(insn, regs);
+			return compressed_insn(insn, regs);
 	}
 
 	return illegal_insn_table[(insn & 0x7c) >> 2](insn, regs);
--
Quoted 2 lines

2.42.0

I think this patch may be unnecessary.

After searching the boot flow of UEFI, I only found two implementations:

For QEMU RISC-V virt platform and SG2042 platform:
SBI -> (SEC -> PEI -> DXE) -> bootloader -> OS ?????????????? UEFI

For SiFive U540 platform:
(SEC -> OpenSBI -> PEI -> DXE) -> bootloader -> OS ???????????? UEFI

Quoted 7 lines
Quoted 3 lines

Both invoke SBI at a very early stage and use raw vaild address, and handling the UEFI header is more like bootloader's job. I have no idea about why this should be handled by SBI. FYI, Bj?rn T?pel.

This patch is used to solve the problem of non-UEFI firmware booting the kernel. The UEFI boot kernel should parse the PE file header, so this patch is not needed. But non-UEFI probably treats the kernel as a binary image.

Quoted 4 lines

This is not a vaild scenario for UEFI binary. IIRC, even the aarch64 arch does not support using UEFI binary in this way. I agree with Jessica about the fact that they are different. If you insist, I suggest using a custom bootloader to handle this.

Now this modification only adds an instruction to simulate and will not affect the other programs.

Regards,
Xiang W

Quoted 6 lines
Quoted 6 lines

Regards,
Xiang W

#5 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

In reply to Xiang W
Quoted 23 lines · Xiang W

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]. So add Emulate c.li

---
 lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_illegal_insn.c +2 -0
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+       unsigned long imm, rd;

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

Quoted 10 lines
lib/sbi/sbi_illegal_insn.c +10 -0
+       unsigned long *regs_p = (unsigned long *)regs;
+
+       if ((insn & 0xe003) == 0x4001) { /* c.li */
+               imm = (insn >> 2) & 0x1f;
+               imm |= ((insn >> 12) & 1) ? -32 : 0;
+               rd = (insn >> 7) & 0x1f;
+               if (rd)
+                       regs_p[rd] = imm;
+               return 0;
+       }

The mepc update is missing, so this patch will not work.

That aside, what Jess pointed out is that on a machine *NOT* supporting C, we're not emulating anything. The 16b instruction parcels do not exist here, so we cannot really emulate that. Instead, what the firmware gets is a 32b bogus/nonexisting instruction. Emulating that is... weird. A valid concern! ;-)

The Linux Image spec says that code0 can be 'MZ' (and will be for UEFI images). One correct fix (I think) is changing the non-UEFI loader, fixing up code0 if it's MZ, making sure not to execute that (well, you could on C capable machines). That, or changing specs.

Bj?rn

author
#6 lore raw

PATCH 1/1 lib: sbi_illegal_insn: Emulate c.li

? 2023-11-09???? 15:48 +0100?Bj?rn T?pel???

Quoted 43 lines · Xiang W
Quoted 23 lines

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]. So add Emulate c.li

---
?lib/sbi/sbi_illegal_insn.c | 18 +++++++++++++++++- ?1 file changed, 17 insertions(+), 1 deletion(-)

lib/sbi/sbi_illegal_insn.c +2 -0
index 2be4757..4ab10f4 100644
@@ -102,6 +102,22 @@ 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) {
+?????? unsigned long imm, rd;

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

Quoted 10 lines
lib/sbi/sbi_illegal_insn.c +10 -0
+?????? unsigned long *regs_p = (unsigned long *)regs;
+
+?????? if ((insn & 0xe003) == 0x4001) { /* c.li */
+?????????????? imm = (insn >> 2) & 0x1f;
+?????????????? imm |= ((insn >> 12) & 1) ? -32 : 0;
+?????????????? rd = (insn >> 7) & 0x1f;
+?????????????? if (rd)
+?????????????????????? regs_p[rd] = imm;
+?????????????? return 0;
+?????? }

The mepc update is missing, so this patch will not work.

That aside, what Jess pointed out is that on a machine *NOT* supporting C, we're not emulating anything. The 16b instruction parcels do not exist here, so we cannot really emulate that. Instead, what the firmware gets is a 32b bogus/nonexisting instruction. Emulating that is... weird. A valid concern! ;-)

Quoted 4 lines

The Linux Image spec says that code0 can be 'MZ' (and will be for UEFI images). One correct fix (I think) is changing the non-UEFI loader, fixing up code0 if it's MZ, making sure not to execute that (well, you could on C capable machines).? That, or changing specs.

Ok. let's abandon.

Regards,
Xiang W

Quoted 3 lines

Bj?rn