]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/arm64/kernel/efi.c
arm64: add better page protections to arm64
[mirror_ubuntu-hirsute-kernel.git] / arch / arm64 / kernel / efi.c
CommitLineData
f84d0275
MS
1/*
2 * Extensible Firmware Interface
3 *
4 * Based on Extensible Firmware Interface Specification version 2.4
5 *
6 * Copyright (C) 2013, 2014 Linaro Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
f3cdfd23 14#include <linux/atomic.h>
d1ae8c00 15#include <linux/dmi.h>
f84d0275
MS
16#include <linux/efi.h>
17#include <linux/export.h>
18#include <linux/memblock.h>
f3cdfd23 19#include <linux/mm_types.h>
f84d0275
MS
20#include <linux/bootmem.h>
21#include <linux/of.h>
22#include <linux/of_fdt.h>
f3cdfd23
AB
23#include <linux/preempt.h>
24#include <linux/rbtree.h>
25#include <linux/rwsem.h>
f84d0275
MS
26#include <linux/sched.h>
27#include <linux/slab.h>
f3cdfd23 28#include <linux/spinlock.h>
f84d0275
MS
29
30#include <asm/cacheflush.h>
31#include <asm/efi.h>
32#include <asm/tlbflush.h>
33#include <asm/mmu_context.h>
f3cdfd23
AB
34#include <asm/mmu.h>
35#include <asm/pgtable.h>
f84d0275
MS
36
37struct efi_memory_map memmap;
38
f84d0275
MS
39static u64 efi_system_table;
40
41static int uefi_debug __initdata;
42static int __init uefi_debug_setup(char *str)
43{
44 uefi_debug = 1;
45
46 return 0;
47}
48early_param("uefi_debug", uefi_debug_setup);
49
50static int __init is_normal_ram(efi_memory_desc_t *md)
51{
52 if (md->attribute & EFI_MEMORY_WB)
53 return 1;
54 return 0;
55}
56
f3cdfd23
AB
57/*
58 * Translate a EFI virtual address into a physical address: this is necessary,
59 * as some data members of the EFI system table are virtually remapped after
60 * SetVirtualAddressMap() has been called.
61 */
62static phys_addr_t efi_to_phys(unsigned long addr)
63{
64 efi_memory_desc_t *md;
65
66 for_each_efi_memory_desc(&memmap, md) {
67 if (!(md->attribute & EFI_MEMORY_RUNTIME))
68 continue;
69 if (md->virt_addr == 0)
70 /* no virtual mapping has been installed by the stub */
71 break;
72 if (md->virt_addr <= addr &&
73 (addr - md->virt_addr) < (md->num_pages << EFI_PAGE_SHIFT))
74 return md->phys_addr + addr - md->virt_addr;
75 }
76 return addr;
77}
78
f84d0275
MS
79static int __init uefi_init(void)
80{
81 efi_char16_t *c16;
f3cdfd23
AB
82 void *config_tables;
83 u64 table_size;
f84d0275
MS
84 char vendor[100] = "unknown";
85 int i, retval;
86
87 efi.systab = early_memremap(efi_system_table,
88 sizeof(efi_system_table_t));
89 if (efi.systab == NULL) {
90 pr_warn("Unable to map EFI system table.\n");
91 return -ENOMEM;
92 }
93
94 set_bit(EFI_BOOT, &efi.flags);
95 set_bit(EFI_64BIT, &efi.flags);
96
97 /*
98 * Verify the EFI Table
99 */
100 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
101 pr_err("System table signature incorrect\n");
88f8abd5
DY
102 retval = -EINVAL;
103 goto out;
f84d0275
MS
104 }
105 if ((efi.systab->hdr.revision >> 16) < 2)
106 pr_warn("Warning: EFI system table version %d.%02d, expected 2.00 or greater\n",
107 efi.systab->hdr.revision >> 16,
108 efi.systab->hdr.revision & 0xffff);
109
110 /* Show what we know for posterity */
f3cdfd23 111 c16 = early_memremap(efi_to_phys(efi.systab->fw_vendor),
f84d0275
MS
112 sizeof(vendor));
113 if (c16) {
114 for (i = 0; i < (int) sizeof(vendor) - 1 && *c16; ++i)
115 vendor[i] = c16[i];
116 vendor[i] = '\0';
88f8abd5 117 early_memunmap(c16, sizeof(vendor));
f84d0275
MS
118 }
119
120 pr_info("EFI v%u.%.02u by %s\n",
121 efi.systab->hdr.revision >> 16,
122 efi.systab->hdr.revision & 0xffff, vendor);
123
f3cdfd23
AB
124 table_size = sizeof(efi_config_table_64_t) * efi.systab->nr_tables;
125 config_tables = early_memremap(efi_to_phys(efi.systab->tables),
126 table_size);
127
128 retval = efi_config_parse_tables(config_tables, efi.systab->nr_tables,
129 sizeof(efi_config_table_64_t), NULL);
f84d0275 130
f3cdfd23 131 early_memunmap(config_tables, table_size);
88f8abd5 132out:
f84d0275 133 early_memunmap(efi.systab, sizeof(efi_system_table_t));
f84d0275
MS
134 return retval;
135}
136
f84d0275
MS
137/*
138 * Return true for RAM regions we want to permanently reserve.
139 */
140static __init int is_reserve_region(efi_memory_desc_t *md)
141{
61139eb0
AB
142 switch (md->type) {
143 case EFI_LOADER_CODE:
144 case EFI_LOADER_DATA:
145 case EFI_BOOT_SERVICES_CODE:
146 case EFI_BOOT_SERVICES_DATA:
147 case EFI_CONVENTIONAL_MEMORY:
f84d0275 148 return 0;
61139eb0
AB
149 default:
150 break;
151 }
152 return is_normal_ram(md);
f84d0275
MS
153}
154
155static __init void reserve_regions(void)
156{
157 efi_memory_desc_t *md;
158 u64 paddr, npages, size;
159
160 if (uefi_debug)
161 pr_info("Processing EFI memory map:\n");
162
163 for_each_efi_memory_desc(&memmap, md) {
164 paddr = md->phys_addr;
165 npages = md->num_pages;
166
65ba758f
LE
167 if (uefi_debug) {
168 char buf[64];
169
170 pr_info(" 0x%012llx-0x%012llx %s",
f84d0275 171 paddr, paddr + (npages << EFI_PAGE_SHIFT) - 1,
65ba758f
LE
172 efi_md_typeattr_format(buf, sizeof(buf), md));
173 }
f84d0275
MS
174
175 memrange_efi_to_native(&paddr, &npages);
176 size = npages << PAGE_SHIFT;
177
178 if (is_normal_ram(md))
179 early_init_dt_add_memory_arch(paddr, size);
180
3033b845 181 if (is_reserve_region(md)) {
f84d0275
MS
182 memblock_reserve(paddr, size);
183 if (uefi_debug)
184 pr_cont("*");
185 }
186
187 if (uefi_debug)
188 pr_cont("\n");
189 }
86c8b27a
LL
190
191 set_bit(EFI_MEMMAP, &efi.flags);
f84d0275
MS
192}
193
f84d0275
MS
194void __init efi_init(void)
195{
196 struct efi_fdt_params params;
197
198 /* Grab UEFI information placed in FDT by stub */
199 if (!efi_get_fdt_params(&params, uefi_debug))
200 return;
201
202 efi_system_table = params.system_table;
203
204 memblock_reserve(params.mmap & PAGE_MASK,
205 PAGE_ALIGN(params.mmap_size + (params.mmap & ~PAGE_MASK)));
206 memmap.phys_map = (void *)params.mmap;
207 memmap.map = early_memremap(params.mmap, params.mmap_size);
208 memmap.map_end = memmap.map + params.mmap_size;
209 memmap.desc_size = params.desc_size;
210 memmap.desc_version = params.desc_ver;
211
212 if (uefi_init() < 0)
213 return;
214
215 reserve_regions();
216}
217
f84d0275 218/*
f3cdfd23
AB
219 * Enable the UEFI Runtime Services if all prerequisites are in place, i.e.,
220 * non-early mapping of the UEFI system table and virtual mappings for all
221 * EFI_MEMORY_RUNTIME regions.
f84d0275 222 */
f3cdfd23 223static int __init arm64_enable_runtime_services(void)
f84d0275 224{
f84d0275 225 u64 mapsize;
f84d0275
MS
226
227 if (!efi_enabled(EFI_BOOT)) {
228 pr_info("EFI services will not be available.\n");
229 return -1;
230 }
231
6632210f
DY
232 if (efi_runtime_disabled()) {
233 pr_info("EFI runtime services will be disabled.\n");
234 return -1;
235 }
236
237 pr_info("Remapping and enabling EFI services.\n");
7fe5d2b1
AB
238
239 mapsize = memmap.map_end - memmap.map;
f84d0275
MS
240 memmap.map = (__force void *)ioremap_cache((phys_addr_t)memmap.phys_map,
241 mapsize);
7fe5d2b1
AB
242 if (!memmap.map) {
243 pr_err("Failed to remap EFI memory map\n");
244 return -1;
245 }
f84d0275 246 memmap.map_end = memmap.map + mapsize;
f84d0275
MS
247 efi.memmap = &memmap;
248
f3cdfd23
AB
249 efi.systab = (__force void *)ioremap_cache(efi_system_table,
250 sizeof(efi_system_table_t));
99a5603e 251 if (!efi.systab) {
f3cdfd23
AB
252 pr_err("Failed to remap EFI System Table\n");
253 return -1;
99a5603e
AB
254 }
255 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
f84d0275 256
f3cdfd23
AB
257 if (!efi_enabled(EFI_VIRTMAP)) {
258 pr_err("No UEFI virtual mapping was installed -- runtime services will not be available\n");
f84d0275
MS
259 return -1;
260 }
261
262 /* Set up runtime services function pointers */
e15dd494 263 efi_native_runtime_setup();
f84d0275
MS
264 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
265
6a7519e8
SP
266 efi.runtime_version = efi.systab->hdr.revision;
267
f84d0275
MS
268 return 0;
269}
f3cdfd23 270early_initcall(arm64_enable_runtime_services);
d1ae8c00
YL
271
272static int __init arm64_dmi_init(void)
273{
274 /*
275 * On arm64, DMI depends on UEFI, and dmi_scan_machine() needs to
276 * be called early because dmi_id_init(), which is an arch_initcall
277 * itself, depends on dmi_scan_machine() having been called already.
278 */
279 dmi_scan_machine();
b07bfaa3
AB
280 if (dmi_available)
281 dmi_set_dump_stack_arch_desc();
d1ae8c00
YL
282 return 0;
283}
284core_initcall(arm64_dmi_init);
f3cdfd23
AB
285
286static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss;
287
288static struct mm_struct efi_mm = {
289 .mm_rb = RB_ROOT,
290 .pgd = efi_pgd,
291 .mm_users = ATOMIC_INIT(2),
292 .mm_count = ATOMIC_INIT(1),
293 .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem),
294 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
295 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
296 INIT_MM_CONTEXT(efi_mm)
297};
298
299static void efi_set_pgd(struct mm_struct *mm)
300{
301 cpu_switch_mm(mm->pgd, mm);
302 flush_tlb_all();
303 if (icache_is_aivivt())
304 __flush_icache_all();
305}
306
307void efi_virtmap_load(void)
308{
309 preempt_disable();
310 efi_set_pgd(&efi_mm);
311}
312
313void efi_virtmap_unload(void)
314{
315 efi_set_pgd(current->active_mm);
316 preempt_enable();
317}
318
319void __init efi_virtmap_init(void)
320{
321 efi_memory_desc_t *md;
322
323 if (!efi_enabled(EFI_BOOT))
324 return;
325
326 for_each_efi_memory_desc(&memmap, md) {
327 u64 paddr, npages, size;
328 pgprot_t prot;
329
330 if (!(md->attribute & EFI_MEMORY_RUNTIME))
331 continue;
332 if (WARN(md->virt_addr == 0,
333 "UEFI virtual mapping incomplete or missing -- no entry found for 0x%llx\n",
334 md->phys_addr))
335 return;
336
337 paddr = md->phys_addr;
338 npages = md->num_pages;
339 memrange_efi_to_native(&paddr, &npages);
340 size = npages << PAGE_SHIFT;
341
342 pr_info(" EFI remap 0x%016llx => %p\n",
343 md->phys_addr, (void *)md->virt_addr);
344
345 /*
346 * Only regions of type EFI_RUNTIME_SERVICES_CODE need to be
347 * executable, everything else can be mapped with the XN bits
348 * set.
349 */
350 if (!is_normal_ram(md))
351 prot = __pgprot(PROT_DEVICE_nGnRE);
352 else if (md->type == EFI_RUNTIME_SERVICES_CODE)
353 prot = PAGE_KERNEL_EXEC;
354 else
355 prot = PAGE_KERNEL;
356
357 create_pgd_mapping(&efi_mm, paddr, md->virt_addr, size, prot);
358 }
359 set_bit(EFI_VIRTMAP, &efi.flags);
9679be10 360 early_memunmap(memmap.map, memmap.map_end - memmap.map);
f3cdfd23 361}