1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2014, 2015 Intel Corporation; author Matt Fleming
5 * Early support for invoking 32-bit EFI services from a 64-bit kernel.
7 * Because this thunking occurs before ExitBootServices() we have to
8 * restore the firmware's 32-bit GDT and IDT before we make EFI service
11 * On the plus side, we don't have to worry about mangling 64-bit
12 * addresses into 32-bits because we're executing with an identity
13 * mapped pagetable and haven't transitioned to 64-bit virtual addresses
17 #include <linux/linkage.h>
18 #include <asm/asm-offsets.h>
20 #include <asm/page_types.h>
21 #include <asm/processor-flags.h>
22 #include <asm/segment.h>
23 #include <asm/setup.h>
28 * When booting in 64-bit mode on 32-bit EFI firmware, startup_64_mixed_mode()
29 * is the first thing that runs after switching to long mode. Depending on
30 * whether the EFI handover protocol or the compat entry point was used to
31 * enter the kernel, it will either branch to the common 64-bit EFI stub
32 * entrypoint efi_stub_entry() directly, or via the 64-bit EFI PE/COFF
33 * entrypoint efi_pe_entry(). In the former case, the bootloader must provide a
34 * struct bootparams pointer as the third argument, so the presence of such a
35 * pointer is used to disambiguate.
38 * +------------------+ +------------+ +------>| efi_pe_entry |
39 * | efi32_pe_entry |---->| | | +-----------+--+
40 * +------------------+ | | +------+----------------+ |
41 * | startup_32 |---->| startup_64_mixed_mode | |
42 * +------------------+ | | +------+----------------+ |
43 * | efi32_stub_entry |---->| | | |
44 * +------------------+ +------------+ | |
46 * +------------+ +----------------+ |
47 * | startup_64 |<----| efi_stub_entry |<--------+
48 * +------------+ +----------------+
50 SYM_FUNC_START(startup_64_mixed_mode)
51 lea efi32_boot_args(%rip), %rdx
55 /* Switch to the firmware's stack */
56 movl efi32_boot_sp(%rip), %esp
59 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
60 mov 8(%rdx), %edx // saved bootparams pointer
65 * efi_pe_entry uses MS calling convention, which requires 32 bytes of
66 * shadow space on the stack even if all arguments are passed in
67 * registers. We also need an additional 8 bytes for the space that
68 * would be occupied by the return address, and this also results in
69 * the correct stack alignment for entry.
72 mov %rdi, %rcx // MS calling convention
75 SYM_FUNC_END(startup_64_mixed_mode)
77 SYM_FUNC_START(__efi64_thunk)
88 /* Copy args passed on stack */
94 * Convert x86-64 ABI params to i386 ABI
101 movl %r9d, 0x10(%rsp)
102 movl %ebp, 0x14(%rsp)
103 movl %ebx, 0x18(%rsp)
104 movl %eax, 0x1c(%rsp)
106 leaq 0x20(%rsp), %rbx
113 * Switch to IDT and GDT with 32-bit segments. These are the firmware
114 * GDT and IDT that were installed when the kernel started executing.
115 * The pointers were saved by the efi32_entry() routine below.
117 * Pass the saved DS selector to the 32-bit code, and use far return to
118 * restore the saved CS selector.
120 lidt efi32_boot_idt(%rip)
121 lgdt efi32_boot_gdt(%rip)
123 movzwl efi32_boot_ds(%rip), %edx
124 movzwq efi32_boot_cs(%rip), %rax
126 leaq efi_enter32(%rip), %rax
139 /* Clear out 32-bit selector from FS and GS */
147 SYM_FUNC_END(__efi64_thunk)
150 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
151 SYM_FUNC_START(efi32_stub_entry)
154 leal (efi32_boot_args - 1b)(%ecx), %ebx
158 leal (_bss - 1b)(%ecx), %edi
159 leal (_ebss - 1b)(%ecx), %ecx
165 add $0x4, %esp /* Discard return address */
171 SYM_FUNC_END(efi32_stub_entry)
175 * EFI service pointer must be in %edi.
177 * The stack should represent the 32-bit calling convention.
179 SYM_FUNC_START_LOCAL(efi_enter32)
180 /* Load firmware selector into data and stack segment registers */
187 /* Reload pgtables */
193 btrl $X86_CR0_PG_BIT, %eax
196 /* Disable long mode via EFER */
199 btrl $_EFER_LME, %eax
204 /* We must preserve return value */
208 * Some firmware will return with interrupts enabled. Be sure to
209 * disable them before we switch GDTs and IDTs.
217 btsl $(X86_CR4_PAE_BIT), %eax
225 btsl $_EFER_LME, %eax
236 btsl $X86_CR0_PG_BIT, %eax
239 SYM_FUNC_END(efi_enter32)
242 * This is the common EFI stub entry point for mixed mode.
244 * Arguments: %ecx image handle
245 * %edx EFI system table pointer
247 * Since this is the point of no return for ordinary execution, no registers
248 * are considered live except for the function parameters. [Note that the EFI
249 * stub may still exit and return to the firmware using the Exit() EFI boot
252 SYM_FUNC_START_LOCAL(efi32_entry)
256 /* Save firmware GDTR and code/data selectors */
257 sgdtl (efi32_boot_gdt - 1b)(%ebx)
258 movw %cs, (efi32_boot_cs - 1b)(%ebx)
259 movw %ds, (efi32_boot_ds - 1b)(%ebx)
261 /* Store firmware IDT descriptor */
262 sidtl (efi32_boot_idt - 1b)(%ebx)
264 /* Store firmware stack pointer */
265 movl %esp, (efi32_boot_sp - 1b)(%ebx)
267 /* Store boot arguments */
268 leal (efi32_boot_args - 1b)(%ebx), %ebx
271 movb $0x0, 12(%ebx) // efi_is64
274 * Allocate some memory for a temporary struct boot_params, which only
275 * needs the minimal pieces that startup_32() relies on.
277 subl $PARAM_SIZE, %esp
279 movl $PAGE_SIZE, BP_kernel_alignment(%esi)
280 movl $_end - 1b, BP_init_size(%esi)
281 subl $startup_32 - 1b, BP_init_size(%esi)
285 btrl $X86_CR0_PG_BIT, %eax
289 SYM_FUNC_END(efi32_entry)
292 * efi_status_t efi32_pe_entry(efi_handle_t image_handle,
293 * efi_system_table_32_t *sys_table)
295 SYM_FUNC_START(efi32_pe_entry)
298 pushl %ebx // save callee-save registers
301 call verify_cpu // check for long mode support
303 movl $0x80000003, %eax // EFI_UNSUPPORTED
306 movl 8(%ebp), %ecx // image_handle
307 movl 12(%ebp), %edx // sys_table
308 jmp efi32_entry // pass %ecx, %edx
309 // no other registers remain live
311 2: popl %edi // restore callee-save registers
315 SYM_FUNC_END(efi32_pe_entry)
317 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
318 .org efi32_stub_entry + 0x200
320 SYM_FUNC_START_NOALIGN(efi64_stub_entry)
321 jmp efi_handover_entry
322 SYM_FUNC_END(efi64_stub_entry)
327 SYM_DATA_START_LOCAL(efi32_boot_gdt)
330 SYM_DATA_END(efi32_boot_gdt)
332 SYM_DATA_START_LOCAL(efi32_boot_idt)
335 SYM_DATA_END(efi32_boot_idt)
337 SYM_DATA_LOCAL(efi32_boot_cs, .word 0)
338 SYM_DATA_LOCAL(efi32_boot_ds, .word 0)
339 SYM_DATA_LOCAL(efi32_boot_sp, .long 0)
340 SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0)
341 SYM_DATA(efi_is64, .byte 1)