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>
19 #include <asm/page_types.h>
20 #include <asm/processor-flags.h>
21 #include <asm/segment.h>
26 * When booting in 64-bit mode on 32-bit EFI firmware, startup_64_mixed_mode()
27 * is the first thing that runs after switching to long mode. Depending on
28 * whether the EFI handover protocol or the compat entry point was used to
29 * enter the kernel, it will either branch to the common 64-bit EFI stub
30 * entrypoint efi_stub_entry() directly, or via the 64-bit EFI PE/COFF
31 * entrypoint efi_pe_entry(). In the former case, the bootloader must provide a
32 * struct bootparams pointer as the third argument, so the presence of such a
33 * pointer is used to disambiguate.
36 * +------------------+ +------------+ +------>| efi_pe_entry |
37 * | efi32_pe_entry |---->| | | +-----------+--+
38 * +------------------+ | | +------+----------------+ |
39 * | startup_32 |---->| startup_64_mixed_mode | |
40 * +------------------+ | | +------+----------------+ |
41 * | efi32_stub_entry |---->| | | |
42 * +------------------+ +------------+ | |
44 * +------------+ +----------------+ |
45 * | startup_64 |<----| efi_stub_entry |<--------+
46 * +------------+ +----------------+
48 SYM_FUNC_START(startup_64_mixed_mode)
49 lea efi32_boot_args(%rip), %rdx
52 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
53 mov 8(%rdx), %edx // saved bootparams pointer
58 * efi_pe_entry uses MS calling convention, which requires 32 bytes of
59 * shadow space on the stack even if all arguments are passed in
60 * registers. We also need an additional 8 bytes for the space that
61 * would be occupied by the return address, and this also results in
62 * the correct stack alignment for entry.
65 mov %rdi, %rcx // MS calling convention
68 SYM_FUNC_END(startup_64_mixed_mode)
70 SYM_FUNC_START(__efi64_thunk)
81 /* Copy args passed on stack */
87 * Convert x86-64 ABI params to i386 ABI
106 * Switch to IDT and GDT with 32-bit segments. These are the firmware
107 * GDT and IDT that were installed when the kernel started executing.
108 * The pointers were saved by the efi32_entry() routine below.
110 * Pass the saved DS selector to the 32-bit code, and use far return to
111 * restore the saved CS selector.
113 lidt efi32_boot_idt(%rip)
114 lgdt efi32_boot_gdt(%rip)
116 movzwl efi32_boot_ds(%rip), %edx
117 movzwq efi32_boot_cs(%rip), %rax
119 leaq efi_enter32(%rip), %rax
132 /* Clear out 32-bit selector from FS and GS */
140 SYM_FUNC_END(__efi64_thunk)
143 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
144 SYM_FUNC_START(efi32_stub_entry)
150 leal (_bss - 1b)(%ecx), %edi
151 leal (_ebss - 1b)(%ecx), %ecx
157 add $0x4, %esp /* Discard return address */
162 SYM_FUNC_END(efi32_stub_entry)
166 * EFI service pointer must be in %edi.
168 * The stack should represent the 32-bit calling convention.
170 SYM_FUNC_START_LOCAL(efi_enter32)
171 /* Load firmware selector into data and stack segment registers */
178 /* Reload pgtables */
184 btrl $X86_CR0_PG_BIT, %eax
187 /* Disable long mode via EFER */
190 btrl $_EFER_LME, %eax
195 /* We must preserve return value */
199 * Some firmware will return with interrupts enabled. Be sure to
200 * disable them before we switch GDTs and IDTs.
208 btsl $(X86_CR4_PAE_BIT), %eax
216 btsl $_EFER_LME, %eax
227 btsl $X86_CR0_PG_BIT, %eax
230 SYM_FUNC_END(efi_enter32)
233 * This is the common EFI stub entry point for mixed mode.
235 * Arguments: %ecx image handle
236 * %edx EFI system table pointer
237 * %esi struct bootparams pointer (or NULL when not using
238 * the EFI handover protocol)
240 * Since this is the point of no return for ordinary execution, no registers
241 * are considered live except for the function parameters. [Note that the EFI
242 * stub may still exit and return to the firmware using the Exit() EFI boot
245 SYM_FUNC_START_LOCAL(efi32_entry)
249 /* Save firmware GDTR and code/data selectors */
250 sgdtl (efi32_boot_gdt - 1b)(%ebx)
251 movw %cs, (efi32_boot_cs - 1b)(%ebx)
252 movw %ds, (efi32_boot_ds - 1b)(%ebx)
254 /* Store firmware IDT descriptor */
255 sidtl (efi32_boot_idt - 1b)(%ebx)
257 /* Store boot arguments */
258 leal (efi32_boot_args - 1b)(%ebx), %ebx
262 movb $0x0, 12(%ebx) // efi_is64
266 btrl $X86_CR0_PG_BIT, %eax
270 SYM_FUNC_END(efi32_entry)
273 * efi_status_t efi32_pe_entry(efi_handle_t image_handle,
274 * efi_system_table_32_t *sys_table)
276 SYM_FUNC_START(efi32_pe_entry)
279 pushl %ebx // save callee-save registers
282 call verify_cpu // check for long mode support
284 movl $0x80000003, %eax // EFI_UNSUPPORTED
287 movl 8(%ebp), %ecx // image_handle
288 movl 12(%ebp), %edx // sys_table
290 jmp efi32_entry // pass %ecx, %edx, %esi
291 // no other registers remain live
293 2: popl %edi // restore callee-save registers
297 SYM_FUNC_END(efi32_pe_entry)
299 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
300 .org efi32_stub_entry + 0x200
302 SYM_FUNC_START_NOALIGN(efi64_stub_entry)
303 jmp efi_handover_entry
304 SYM_FUNC_END(efi64_stub_entry)
309 SYM_DATA_START_LOCAL(efi32_boot_gdt)
312 SYM_DATA_END(efi32_boot_gdt)
314 SYM_DATA_START_LOCAL(efi32_boot_idt)
317 SYM_DATA_END(efi32_boot_idt)
319 SYM_DATA_LOCAL(efi32_boot_cs, .word 0)
320 SYM_DATA_LOCAL(efi32_boot_ds, .word 0)
321 SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0)
322 SYM_DATA(efi_is64, .byte 1)