xref: /linux/arch/arm64/kernel/efi.c (revision fbf46565c67c626849c7ce2a326972d3008d2a91)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Extensible Firmware Interface
4  *
5  * Based on Extensible Firmware Interface Specification version 2.4
6  *
7  * Copyright (C) 2013, 2014 Linaro Ltd.
8  */
9 
10 #include <linux/efi.h>
11 #include <linux/init.h>
12 
13 #include <asm/efi.h>
14 #include <asm/stacktrace.h>
15 
16 static bool region_is_misaligned(const efi_memory_desc_t *md)
17 {
18 	if (PAGE_SIZE == EFI_PAGE_SIZE)
19 		return false;
20 	return !PAGE_ALIGNED(md->phys_addr) ||
21 	       !PAGE_ALIGNED(md->num_pages << EFI_PAGE_SHIFT);
22 }
23 
24 /*
25  * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
26  * executable, everything else can be mapped with the XN bits
27  * set. Also take the new (optional) RO/XP bits into account.
28  */
29 static __init pteval_t create_mapping_protection(efi_memory_desc_t *md)
30 {
31 	u64 attr = md->attribute;
32 	u32 type = md->type;
33 
34 	if (type == EFI_MEMORY_MAPPED_IO)
35 		return PROT_DEVICE_nGnRE;
36 
37 	if (region_is_misaligned(md)) {
38 		static bool __initdata code_is_misaligned;
39 
40 		/*
41 		 * Regions that are not aligned to the OS page size cannot be
42 		 * mapped with strict permissions, as those might interfere
43 		 * with the permissions that are needed by the adjacent
44 		 * region's mapping. However, if we haven't encountered any
45 		 * misaligned runtime code regions so far, we can safely use
46 		 * non-executable permissions for non-code regions.
47 		 */
48 		code_is_misaligned |= (type == EFI_RUNTIME_SERVICES_CODE);
49 
50 		return code_is_misaligned ? pgprot_val(PAGE_KERNEL_EXEC)
51 					  : pgprot_val(PAGE_KERNEL);
52 	}
53 
54 	/* R-- */
55 	if ((attr & (EFI_MEMORY_XP | EFI_MEMORY_RO)) ==
56 	    (EFI_MEMORY_XP | EFI_MEMORY_RO))
57 		return pgprot_val(PAGE_KERNEL_RO);
58 
59 	/* R-X */
60 	if (attr & EFI_MEMORY_RO)
61 		return pgprot_val(PAGE_KERNEL_ROX);
62 
63 	/* RW- */
64 	if (((attr & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP)) ==
65 	     EFI_MEMORY_XP) ||
66 	    type != EFI_RUNTIME_SERVICES_CODE)
67 		return pgprot_val(PAGE_KERNEL);
68 
69 	/* RWX */
70 	return pgprot_val(PAGE_KERNEL_EXEC);
71 }
72 
73 /* we will fill this structure from the stub, so don't put it in .bss */
74 struct screen_info screen_info __section(".data");
75 EXPORT_SYMBOL(screen_info);
76 
77 int __init efi_create_mapping(struct mm_struct *mm, efi_memory_desc_t *md)
78 {
79 	pteval_t prot_val = create_mapping_protection(md);
80 	bool page_mappings_only = (md->type == EFI_RUNTIME_SERVICES_CODE ||
81 				   md->type == EFI_RUNTIME_SERVICES_DATA);
82 
83 	/*
84 	 * If this region is not aligned to the page size used by the OS, the
85 	 * mapping will be rounded outwards, and may end up sharing a page
86 	 * frame with an adjacent runtime memory region. Given that the page
87 	 * table descriptor covering the shared page will be rewritten when the
88 	 * adjacent region gets mapped, we must avoid block mappings here so we
89 	 * don't have to worry about splitting them when that happens.
90 	 */
91 	if (region_is_misaligned(md))
92 		page_mappings_only = true;
93 
94 	create_pgd_mapping(mm, md->phys_addr, md->virt_addr,
95 			   md->num_pages << EFI_PAGE_SHIFT,
96 			   __pgprot(prot_val | PTE_NG), page_mappings_only);
97 	return 0;
98 }
99 
100 static int __init set_permissions(pte_t *ptep, unsigned long addr, void *data)
101 {
102 	efi_memory_desc_t *md = data;
103 	pte_t pte = READ_ONCE(*ptep);
104 
105 	if (md->attribute & EFI_MEMORY_RO)
106 		pte = set_pte_bit(pte, __pgprot(PTE_RDONLY));
107 	if (md->attribute & EFI_MEMORY_XP)
108 		pte = set_pte_bit(pte, __pgprot(PTE_PXN));
109 	set_pte(ptep, pte);
110 	return 0;
111 }
112 
113 int __init efi_set_mapping_permissions(struct mm_struct *mm,
114 				       efi_memory_desc_t *md)
115 {
116 	BUG_ON(md->type != EFI_RUNTIME_SERVICES_CODE &&
117 	       md->type != EFI_RUNTIME_SERVICES_DATA);
118 
119 	if (region_is_misaligned(md))
120 		return 0;
121 
122 	/*
123 	 * Calling apply_to_page_range() is only safe on regions that are
124 	 * guaranteed to be mapped down to pages. Since we are only called
125 	 * for regions that have been mapped using efi_create_mapping() above
126 	 * (and this is checked by the generic Memory Attributes table parsing
127 	 * routines), there is no need to check that again here.
128 	 */
129 	return apply_to_page_range(mm, md->virt_addr,
130 				   md->num_pages << EFI_PAGE_SHIFT,
131 				   set_permissions, md);
132 }
133 
134 /*
135  * UpdateCapsule() depends on the system being shutdown via
136  * ResetSystem().
137  */
138 bool efi_poweroff_required(void)
139 {
140 	return efi_enabled(EFI_RUNTIME_SERVICES);
141 }
142 
143 asmlinkage efi_status_t efi_handle_corrupted_x18(efi_status_t s, const char *f)
144 {
145 	pr_err_ratelimited(FW_BUG "register x18 corrupted by EFI %s\n", f);
146 	return s;
147 }
148 
149 DEFINE_SPINLOCK(efi_rt_lock);
150 
151 asmlinkage u64 *efi_rt_stack_top __ro_after_init;
152 
153 asmlinkage efi_status_t __efi_rt_asm_recover(void);
154 
155 bool efi_runtime_fixup_exception(struct pt_regs *regs, const char *msg)
156 {
157 	 /* Check whether the exception occurred while running the firmware */
158 	if (!current_in_efi() || regs->pc >= TASK_SIZE_64)
159 		return false;
160 
161 	pr_err(FW_BUG "Unable to handle %s in EFI runtime service\n", msg);
162 	add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK);
163 	clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
164 
165 	regs->regs[0]	= EFI_ABORTED;
166 	regs->regs[30]	= efi_rt_stack_top[-1];
167 	regs->pc	= (u64)__efi_rt_asm_recover;
168 
169 	if (IS_ENABLED(CONFIG_SHADOW_CALL_STACK))
170 		regs->regs[18] = efi_rt_stack_top[-2];
171 
172 	return true;
173 }
174 
175 /* EFI requires 8 KiB of stack space for runtime services */
176 static_assert(THREAD_SIZE >= SZ_8K);
177 
178 static int __init arm64_efi_rt_init(void)
179 {
180 	void *p;
181 
182 	if (!efi_enabled(EFI_RUNTIME_SERVICES))
183 		return 0;
184 
185 	p = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, GFP_KERNEL,
186 			   NUMA_NO_NODE, &&l);
187 l:	if (!p) {
188 		pr_warn("Failed to allocate EFI runtime stack\n");
189 		clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
190 		return -ENOMEM;
191 	}
192 
193 	efi_rt_stack_top = p + THREAD_SIZE;
194 	return 0;
195 }
196 core_initcall(arm64_efi_rt_init);
197