[PATCH RFC 2/5] efi/x86_64: set page table if provided by libstub

From: Baskov Evgeniy
Date: Wed Nov 10 2021 - 05:57:01 EST


It is desired to be able to switch pages tables before jumping
to potentially relocated code while booting via UEFI.
The easiest way to achieve that is to do it in assembly
immediately after returning from efi_main().

Add mechanism to switch page table to one provided by libstub.

Signed-off-by: Baskov Evgeniy <baskov@xxxxxxxxx>
---
arch/x86/boot/compressed/head_64.S | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S
index 572c535cf45b..1e467fdefd9d 100644
--- a/arch/x86/boot/compressed/head_64.S
+++ b/arch/x86/boot/compressed/head_64.S
@@ -540,6 +540,17 @@ SYM_FUNC_START_ALIAS(efi_stub_entry)
movq %rdx, %rbx /* save boot_params pointer */
call efi_main
movq %rbx,%rsi
+
+ /*
+ * Switch page table to the one constructed by libstub if provided
+ * It is required to be done here because stack is not mapped
+ * in new page table.
+ */
+ movq efi_temp_pgtable(%rip), %rbx
+ testq %rbx, %rbx
+ jz 1f
+ movq %rbx, %cr3
+1:
leaq rva(startup_64)(%rax), %rax
jmp *%rax
SYM_FUNC_END(efi64_stub_entry)
@@ -736,6 +747,7 @@ SYM_DATA_END_LABEL(boot32_idt, SYM_L_GLOBAL, boot32_idt_end)

#ifdef CONFIG_EFI_STUB
SYM_DATA(image_offset, .long 0)
+SYM_DATA(efi_temp_pgtable, .quad 0)
#endif
#ifdef CONFIG_EFI_MIXED
SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0)
--
2.33.1