1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2014 Intel Corporation; author Matt Fleming
4 *
5 * Support for invoking 32-bit EFI runtime services from a 64-bit
6 * kernel.
7 *
8 * The below thunking functions are only used after ExitBootServices()
9 * has been called. This simplifies things considerably as compared with
10 * the early EFI thunking because we can leave all the kernel state
11 * intact (GDT, IDT, etc) and simply invoke the 32-bit EFI runtime
12 * services from __KERNEL32_CS. This means we can continue to service
13 * interrupts across an EFI mixed mode call.
14 *
15 * We do however, need to handle the fact that we're running in a full
16 * 64-bit virtual address space. Things like the stack and instruction
17 * addresses need to be accessible by the 32-bit firmware, so we rely on
18 * using the identity mappings in the EFI page table to access the stack
19 * and kernel text (see efi_setup_page_tables()).
20 */
21
22#include <linux/linkage.h>
23#include <linux/objtool.h>
24#include <asm/page_types.h>
25#include <asm/segment.h>
26#include <asm/nospec-branch.h>
27
28	.text
29	.code64
30SYM_FUNC_START(__efi64_thunk)
31STACK_FRAME_NON_STANDARD __efi64_thunk
32	push	%rbp
33	push	%rbx
34
35	/*
36	 * Switch to 1:1 mapped 32-bit stack pointer.
37	 */
38	movq	%rsp, %rax
39	movq	efi_mixed_mode_stack_pa(%rip), %rsp
40	push	%rax
41
42	/*
43	 * Copy args passed via the stack
44	 */
45	subq	$0x24, %rsp
46	movq	0x18(%rax), %rbp
47	movq	0x20(%rax), %rbx
48	movq	0x28(%rax), %rax
49	movl	%ebp, 0x18(%rsp)
50	movl	%ebx, 0x1c(%rsp)
51	movl	%eax, 0x20(%rsp)
52
53	/*
54	 * Calculate the physical address of the kernel text.
55	 */
56	movq	$__START_KERNEL_map, %rax
57	subq	phys_base(%rip), %rax
58
59	leaq	1f(%rip), %rbp
60	leaq	2f(%rip), %rbx
61	subq	%rax, %rbp
62	subq	%rax, %rbx
63
64	movl	%ebx, 0x0(%rsp)		/* return address */
65	movl	%esi, 0x4(%rsp)
66	movl	%edx, 0x8(%rsp)
67	movl	%ecx, 0xc(%rsp)
68	movl	%r8d, 0x10(%rsp)
69	movl	%r9d, 0x14(%rsp)
70
71	/* Switch to 32-bit descriptor */
72	pushq	$__KERNEL32_CS
73	pushq	%rdi			/* EFI runtime service address */
74	lretq
75
761:	movq	0x20(%rsp), %rsp
77	pop	%rbx
78	pop	%rbp
79	ANNOTATE_UNRET_SAFE
80	ret
81	int3
82
83	.code32
842:	pushl	$__KERNEL_CS
85	pushl	%ebp
86	lret
87SYM_FUNC_END(__efi64_thunk)
88
89	.bss
90	.balign 8
91SYM_DATA(efi_mixed_mode_stack_pa, .quad 0)
92