]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/platform/efi/efi_64.c
Merge tag 'pm-4.15-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / platform / efi / efi_64.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5b83683f
HY
2/*
3 * x86_64 specific EFI support functions
4 * Based on Extensible Firmware Interface Specification version 1.0
5 *
6 * Copyright (C) 2005-2008 Intel Co.
7 * Fenghua Yu <fenghua.yu@intel.com>
8 * Bibo Mao <bibo.mao@intel.com>
9 * Chandramouli Narayanan <mouli@linux.intel.com>
10 * Huang Ying <ying.huang@intel.com>
11 *
12 * Code to convert EFI to E820 map has been implemented in elilo bootloader
13 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
14 * is setup appropriately for EFI runtime code.
15 * - mouli 06/14/2007.
16 *
17 */
18
26d7f65f
MF
19#define pr_fmt(fmt) "efi: " fmt
20
5b83683f
HY
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/mm.h>
24#include <linux/types.h>
25#include <linux/spinlock.h>
26#include <linux/bootmem.h>
27#include <linux/ioport.h>
cc3ae7b0 28#include <linux/init.h>
5ab788d7 29#include <linux/mc146818rtc.h>
5b83683f
HY
30#include <linux/efi.h>
31#include <linux/uaccess.h>
32#include <linux/io.h>
33#include <linux/reboot.h>
0d01ff25 34#include <linux/slab.h>
f6697df3 35#include <linux/ucs2_string.h>
1379edd5 36#include <linux/mem_encrypt.h>
5b83683f
HY
37
38#include <asm/setup.h>
39#include <asm/page.h>
66441bd3 40#include <asm/e820/api.h>
5b83683f
HY
41#include <asm/pgtable.h>
42#include <asm/tlbflush.h>
5b83683f
HY
43#include <asm/proto.h>
44#include <asm/efi.h>
4de0d4a6 45#include <asm/cacheflush.h>
3819cd48 46#include <asm/fixmap.h>
d2f7cbe7 47#include <asm/realmode.h>
4f9dbcfc 48#include <asm/time.h>
67a9108e 49#include <asm/pgalloc.h>
5b83683f 50
d2f7cbe7 51/*
b1d17761 52 * We allocate runtime services regions top-down, starting from -4G, i.e.
d2f7cbe7
BP
53 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
54 */
8266e31e 55static u64 efi_va = EFI_VA_START;
d2f7cbe7 56
c9f2a9a6 57struct efi_scratch efi_scratch;
d2f7cbe7 58
9cd2b07c 59static void __init early_code_mapping_set_exec(int executable)
5b83683f
HY
60{
61 efi_memory_desc_t *md;
5b83683f 62
a2172e25
HY
63 if (!(__supported_pte_mask & _PAGE_NX))
64 return;
65
916f676f 66 /* Make EFI service code area executable */
78ce248f 67 for_each_efi_memory_desc(md) {
916f676f
MG
68 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
69 md->type == EFI_BOOT_SERVICES_CODE)
9cd2b07c 70 efi_set_executable(md, executable);
5b83683f
HY
71 }
72}
73
744937b0 74pgd_t * __init efi_call_phys_prolog(void)
5b83683f 75{
94133e46
BH
76 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
77 pgd_t *save_pgd, *pgd_k, *pgd_efi;
78 p4d_t *p4d, *p4d_k, *p4d_efi;
79 pud_t *pud;
744937b0 80
b8f2c21d 81 int pgd;
94133e46 82 int n_pgds, i, j;
5b83683f 83
c9f2a9a6 84 if (!efi_enabled(EFI_OLD_MEMMAP)) {
6c690ee1 85 save_pgd = (pgd_t *)__read_cr3();
c9f2a9a6
MF
86 write_cr3((unsigned long)efi_scratch.efi_pgt);
87 goto out;
88 }
d2f7cbe7 89
9cd2b07c 90 early_code_mapping_set_exec(1);
b8f2c21d
NZ
91
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
20ebc15e 93 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
b8f2c21d 94
94133e46
BH
95 /*
96 * Build 1:1 identity mapping for efi=old_map usage. Note that
97 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
98 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
99 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
100 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
101 * This means here we can only reuse the PMD tables of the direct mapping.
102 */
b8f2c21d 103 for (pgd = 0; pgd < n_pgds; pgd++) {
94133e46
BH
104 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
105 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
106 pgd_efi = pgd_offset_k(addr_pgd);
107 save_pgd[pgd] = *pgd_efi;
108
109 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
110 if (!p4d) {
111 pr_err("Failed to allocate p4d table!\n");
112 goto out;
113 }
114
115 for (i = 0; i < PTRS_PER_P4D; i++) {
116 addr_p4d = addr_pgd + i * P4D_SIZE;
117 p4d_efi = p4d + p4d_index(addr_p4d);
118
119 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
120 if (!pud) {
121 pr_err("Failed to allocate pud table!\n");
122 goto out;
123 }
124
125 for (j = 0; j < PTRS_PER_PUD; j++) {
126 addr_pud = addr_p4d + j * PUD_SIZE;
127
128 if (addr_pud > (max_pfn << PAGE_SHIFT))
129 break;
130
131 vaddr = (unsigned long)__va(addr_pud);
132
133 pgd_k = pgd_offset_k(vaddr);
134 p4d_k = p4d_offset(pgd_k, vaddr);
135 pud[j] = *pud_offset(p4d_k, vaddr);
136 }
137 }
b8f2c21d 138 }
c9f2a9a6 139out:
5b83683f 140 __flush_tlb_all();
744937b0
IM
141
142 return save_pgd;
5b83683f
HY
143}
144
744937b0 145void __init efi_call_phys_epilog(pgd_t *save_pgd)
5b83683f
HY
146{
147 /*
148 * After the lock is released, the original page table is restored.
149 */
94133e46 150 int pgd_idx, i;
744937b0 151 int nr_pgds;
94133e46
BH
152 pgd_t *pgd;
153 p4d_t *p4d;
154 pud_t *pud;
d2f7cbe7 155
c9f2a9a6
MF
156 if (!efi_enabled(EFI_OLD_MEMMAP)) {
157 write_cr3((unsigned long)save_pgd);
158 __flush_tlb_all();
d2f7cbe7 159 return;
c9f2a9a6 160 }
d2f7cbe7 161
744937b0
IM
162 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
163
94133e46
BH
164 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
165 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
744937b0
IM
166 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
167
94133e46
BH
168 if (!(pgd_val(*pgd) & _PAGE_PRESENT))
169 continue;
170
171 for (i = 0; i < PTRS_PER_P4D; i++) {
172 p4d = p4d_offset(pgd,
173 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
174
175 if (!(p4d_val(*p4d) & _PAGE_PRESENT))
176 continue;
177
178 pud = (pud_t *)p4d_page_vaddr(*p4d);
179 pud_free(&init_mm, pud);
180 }
181
182 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
183 p4d_free(&init_mm, p4d);
184 }
185
b8f2c21d 186 kfree(save_pgd);
744937b0 187
5b83683f 188 __flush_tlb_all();
9cd2b07c 189 early_code_mapping_set_exec(0);
5b83683f 190}
e1ad783b 191
67a9108e
MF
192static pgd_t *efi_pgd;
193
194/*
195 * We need our own copy of the higher levels of the page tables
196 * because we want to avoid inserting EFI region mappings (EFI_VA_END
197 * to EFI_VA_START) into the standard kernel page tables. Everything
198 * else can be shared, see efi_sync_low_kernel_mappings().
199 */
200int __init efi_alloc_page_tables(void)
201{
202 pgd_t *pgd;
e981316f 203 p4d_t *p4d;
67a9108e
MF
204 pud_t *pud;
205 gfp_t gfp_mask;
206
207 if (efi_enabled(EFI_OLD_MEMMAP))
208 return 0;
209
75f296d9 210 gfp_mask = GFP_KERNEL | __GFP_ZERO;
67a9108e
MF
211 efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
212 if (!efi_pgd)
213 return -ENOMEM;
214
215 pgd = efi_pgd + pgd_index(EFI_VA_END);
e981316f
KS
216 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
217 if (!p4d) {
218 free_page((unsigned long)efi_pgd);
219 return -ENOMEM;
220 }
67a9108e 221
e981316f 222 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
67a9108e 223 if (!pud) {
e981316f
KS
224 if (CONFIG_PGTABLE_LEVELS > 4)
225 free_page((unsigned long) pgd_page_vaddr(*pgd));
67a9108e
MF
226 free_page((unsigned long)efi_pgd);
227 return -ENOMEM;
228 }
229
67a9108e
MF
230 return 0;
231}
232
d2f7cbe7
BP
233/*
234 * Add low kernel mappings for passing arguments to EFI functions.
235 */
236void efi_sync_low_kernel_mappings(void)
237{
67a9108e
MF
238 unsigned num_entries;
239 pgd_t *pgd_k, *pgd_efi;
e0c4f675 240 p4d_t *p4d_k, *p4d_efi;
67a9108e 241 pud_t *pud_k, *pud_efi;
d2f7cbe7
BP
242
243 if (efi_enabled(EFI_OLD_MEMMAP))
244 return;
245
67a9108e
MF
246 /*
247 * We can share all PGD entries apart from the one entry that
248 * covers the EFI runtime mapping space.
249 *
250 * Make sure the EFI runtime region mappings are guaranteed to
251 * only span a single PGD entry and that the entry also maps
252 * other important kernel regions.
253 */
254 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
255 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
256 (EFI_VA_END & PGDIR_MASK));
257
258 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
259 pgd_k = pgd_offset_k(PAGE_OFFSET);
260
261 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
262 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
d2f7cbe7 263
e981316f
KS
264 /*
265 * As with PGDs, we share all P4D entries apart from the one entry
266 * that covers the EFI runtime mapping space.
267 */
268 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
269 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
270
271 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
272 pgd_k = pgd_offset_k(EFI_VA_END);
273 p4d_efi = p4d_offset(pgd_efi, 0);
274 p4d_k = p4d_offset(pgd_k, 0);
275
276 num_entries = p4d_index(EFI_VA_END);
277 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
278
67a9108e
MF
279 /*
280 * We share all the PUD entries apart from those that map the
281 * EFI regions. Copy around them.
282 */
283 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
284 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
285
e981316f
KS
286 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
287 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
e0c4f675 288 pud_efi = pud_offset(p4d_efi, 0);
e0c4f675 289 pud_k = pud_offset(p4d_k, 0);
67a9108e
MF
290
291 num_entries = pud_index(EFI_VA_END);
292 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
293
e0c4f675 294 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
e0c4f675 295 pud_k = pud_offset(p4d_k, EFI_VA_START);
67a9108e
MF
296
297 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
298 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
d2f7cbe7
BP
299}
300
f6697df3
MF
301/*
302 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
303 */
304static inline phys_addr_t
305virt_to_phys_or_null_size(void *va, unsigned long size)
306{
307 bool bad_size;
308
309 if (!va)
310 return 0;
311
312 if (virt_addr_valid(va))
313 return virt_to_phys(va);
314
315 /*
316 * A fully aligned variable on the stack is guaranteed not to
317 * cross a page bounary. Try to catch strings on the stack by
318 * checking that 'size' is a power of two.
319 */
320 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
321
322 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
323
324 return slow_virt_to_phys(va);
325}
326
327#define virt_to_phys_or_null(addr) \
328 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
329
4e78eb05 330int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
d2f7cbe7 331{
38eecccd 332 unsigned long pfn, text, pf;
4f9dbcfc 333 struct page *page;
994448f1 334 unsigned npages;
b7b898ae
BP
335 pgd_t *pgd;
336
337 if (efi_enabled(EFI_OLD_MEMMAP))
338 return 0;
339
38eecccd
TL
340 /*
341 * Since the PGD is encrypted, set the encryption mask so that when
342 * this value is loaded into cr3 the PGD will be decrypted during
343 * the pagetable walk.
344 */
345 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
67a9108e 346 pgd = efi_pgd;
d2f7cbe7 347
b7b898ae
BP
348 /*
349 * It can happen that the physical address of new_memmap lands in memory
350 * which is not mapped in the EFI page table. Therefore we need to go
351 * and ident-map those pages containing the map before calling
352 * phys_efi_set_virtual_address_map().
353 */
edc3b912 354 pfn = pa_memmap >> PAGE_SHIFT;
38eecccd
TL
355 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
356 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
b7b898ae
BP
357 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
358 return 1;
359 }
360
361 efi_scratch.use_pgd = true;
362
bf29bddf
JK
363 /*
364 * Certain firmware versions are way too sentimential and still believe
365 * they are exclusive and unquestionable owners of the first physical page,
366 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
367 * (but then write-access it later during SetVirtualAddressMap()).
368 *
369 * Create a 1:1 mapping for this page, to avoid triple faults during early
370 * boot with such firmware. We are free to hand this page to the BIOS,
371 * as trim_bios_range() will reserve the first page and isolate it away
372 * from memory allocators anyway.
373 */
1379edd5
TL
374 pf = _PAGE_RW;
375 if (sev_active())
376 pf |= _PAGE_ENC;
377
378 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
bf29bddf
JK
379 pr_err("Failed to create 1:1 mapping for the first page!\n");
380 return 1;
381 }
382
4f9dbcfc
MF
383 /*
384 * When making calls to the firmware everything needs to be 1:1
385 * mapped and addressable with 32-bit pointers. Map the kernel
386 * text and allocate a new stack because we can't rely on the
387 * stack pointer being < 4GB.
388 */
12976670 389 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
994448f1 390 return 0;
4f9dbcfc
MF
391
392 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
393 if (!page)
394 panic("Unable to allocate EFI runtime stack < 4GB\n");
395
396 efi_scratch.phys_stack = virt_to_phys(page_address(page));
397 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
398
2ad510dc 399 npages = (_etext - _text) >> PAGE_SHIFT;
4f9dbcfc 400 text = __pa(_text);
edc3b912 401 pfn = text >> PAGE_SHIFT;
4f9dbcfc 402
38eecccd
TL
403 pf = _PAGE_RW | _PAGE_ENC;
404 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
4f9dbcfc 405 pr_err("Failed to map kernel text 1:1\n");
994448f1 406 return 1;
4f9dbcfc 407 }
b7b898ae
BP
408
409 return 0;
410}
411
d2f7cbe7
BP
412static void __init __map_region(efi_memory_desc_t *md, u64 va)
413{
15f003d2 414 unsigned long flags = _PAGE_RW;
edc3b912 415 unsigned long pfn;
67a9108e 416 pgd_t *pgd = efi_pgd;
d2f7cbe7
BP
417
418 if (!(md->attribute & EFI_MEMORY_WB))
edc3b912 419 flags |= _PAGE_PCD;
d2f7cbe7 420
1379edd5
TL
421 if (sev_active())
422 flags |= _PAGE_ENC;
423
edc3b912
MF
424 pfn = md->phys_addr >> PAGE_SHIFT;
425 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
d2f7cbe7
BP
426 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
427 md->phys_addr, va);
428}
429
430void __init efi_map_region(efi_memory_desc_t *md)
431{
432 unsigned long size = md->num_pages << PAGE_SHIFT;
433 u64 pa = md->phys_addr;
434
435 if (efi_enabled(EFI_OLD_MEMMAP))
436 return old_map_region(md);
437
438 /*
439 * Make sure the 1:1 mappings are present as a catch-all for b0rked
440 * firmware which doesn't update all internal pointers after switching
441 * to virtual mode and would otherwise crap on us.
442 */
443 __map_region(md, md->phys_addr);
444
4f9dbcfc
MF
445 /*
446 * Enforce the 1:1 mapping as the default virtual address when
447 * booting in EFI mixed mode, because even though we may be
448 * running a 64-bit kernel, the firmware may only be 32-bit.
449 */
450 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
451 md->virt_addr = md->phys_addr;
452 return;
453 }
454
d2f7cbe7
BP
455 efi_va -= size;
456
457 /* Is PA 2M-aligned? */
458 if (!(pa & (PMD_SIZE - 1))) {
459 efi_va &= PMD_MASK;
460 } else {
461 u64 pa_offset = pa & (PMD_SIZE - 1);
462 u64 prev_va = efi_va;
463
464 /* get us the same offset within this 2M page */
465 efi_va = (efi_va & PMD_MASK) + pa_offset;
466
467 if (efi_va > prev_va)
468 efi_va -= PMD_SIZE;
469 }
470
471 if (efi_va < EFI_VA_END) {
472 pr_warn(FW_WARN "VA address range overflow!\n");
473 return;
474 }
475
476 /* Do the VA map */
477 __map_region(md, efi_va);
478 md->virt_addr = efi_va;
479}
480
3b266496
DY
481/*
482 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
483 * md->virt_addr is the original virtual address which had been mapped in kexec
484 * 1st kernel.
485 */
486void __init efi_map_region_fixed(efi_memory_desc_t *md)
487{
0513fe1d 488 __map_region(md, md->phys_addr);
3b266496
DY
489 __map_region(md, md->virt_addr);
490}
491
e1ad783b 492void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
3e8fa263 493 u32 type, u64 attribute)
e1ad783b
KP
494{
495 unsigned long last_map_pfn;
496
497 if (type == EFI_MEMORY_MAPPED_IO)
498 return ioremap(phys_addr, size);
499
500 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
501 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
502 unsigned long top = last_map_pfn << PAGE_SHIFT;
3e8fa263 503 efi_ioremap(top, size - (top - phys_addr), type, attribute);
e1ad783b
KP
504 }
505
3e8fa263
MF
506 if (!(attribute & EFI_MEMORY_WB))
507 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
508
e1ad783b
KP
509 return (void __iomem *)__va(phys_addr);
510}
1fec0533
DY
511
512void __init parse_efi_setup(u64 phys_addr, u32 data_len)
513{
514 efi_setup = phys_addr + sizeof(struct setup_data);
1fec0533 515}
c55d016f 516
18141e89 517static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
c55d016f 518{
6d0cc887
SP
519 unsigned long pfn;
520 pgd_t *pgd = efi_pgd;
18141e89
SP
521 int err1, err2;
522
523 /* Update the 1:1 mapping */
524 pfn = md->phys_addr >> PAGE_SHIFT;
525 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
526 if (err1) {
527 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
528 md->phys_addr, md->virt_addr);
529 }
530
531 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
532 if (err2) {
533 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
534 md->phys_addr, md->virt_addr);
535 }
536
537 return err1 || err2;
538}
539
540static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
541{
542 unsigned long pf = 0;
543
544 if (md->attribute & EFI_MEMORY_XP)
545 pf |= _PAGE_NX;
546
547 if (!(md->attribute & EFI_MEMORY_RO))
548 pf |= _PAGE_RW;
549
1379edd5
TL
550 if (sev_active())
551 pf |= _PAGE_ENC;
552
18141e89
SP
553 return efi_update_mappings(md, pf);
554}
555
556void __init efi_runtime_update_mappings(void)
557{
6d0cc887 558 efi_memory_desc_t *md;
6d0cc887
SP
559
560 if (efi_enabled(EFI_OLD_MEMMAP)) {
561 if (__supported_pte_mask & _PAGE_NX)
562 runtime_code_page_mkexec();
563 return;
564 }
565
18141e89
SP
566 /*
567 * Use the EFI Memory Attribute Table for mapping permissions if it
568 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
569 */
570 if (efi_enabled(EFI_MEM_ATTR)) {
571 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
572 return;
573 }
574
575 /*
576 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
577 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
578 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
579 * published by the firmware. Even if we find a buggy implementation of
580 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
581 * EFI_PROPERTIES_TABLE, because of the same reason.
582 */
583
6d0cc887 584 if (!efi_enabled(EFI_NX_PE_DATA))
c55d016f
BP
585 return;
586
78ce248f 587 for_each_efi_memory_desc(md) {
6d0cc887 588 unsigned long pf = 0;
6d0cc887
SP
589
590 if (!(md->attribute & EFI_MEMORY_RUNTIME))
591 continue;
592
593 if (!(md->attribute & EFI_MEMORY_WB))
594 pf |= _PAGE_PCD;
595
596 if ((md->attribute & EFI_MEMORY_XP) ||
597 (md->type == EFI_RUNTIME_SERVICES_DATA))
598 pf |= _PAGE_NX;
599
600 if (!(md->attribute & EFI_MEMORY_RO) &&
601 (md->type != EFI_RUNTIME_SERVICES_CODE))
602 pf |= _PAGE_RW;
603
1379edd5
TL
604 if (sev_active())
605 pf |= _PAGE_ENC;
606
18141e89 607 efi_update_mappings(md, pf);
6d0cc887 608 }
c55d016f 609}
11cc8512
BP
610
611void __init efi_dump_pagetable(void)
612{
613#ifdef CONFIG_EFI_PGT_DUMP
ac81d3de
SP
614 if (efi_enabled(EFI_OLD_MEMMAP))
615 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
616 else
617 ptdump_walk_pgd_level(NULL, efi_pgd);
11cc8512
BP
618#endif
619}
994448f1 620
4f9dbcfc
MF
621#ifdef CONFIG_EFI_MIXED
622extern efi_status_t efi64_thunk(u32, ...);
623
624#define runtime_service32(func) \
625({ \
626 u32 table = (u32)(unsigned long)efi.systab; \
627 u32 *rt, *___f; \
628 \
629 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
630 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
631 *___f; \
632})
633
634/*
635 * Switch to the EFI page tables early so that we can access the 1:1
636 * runtime services mappings which are not mapped in any other page
637 * tables. This function must be called before runtime_service32().
638 *
639 * Also, disable interrupts because the IDT points to 64-bit handlers,
640 * which aren't going to function correctly when we switch to 32-bit.
641 */
642#define efi_thunk(f, ...) \
643({ \
644 efi_status_t __s; \
21f86625
AT
645 unsigned long __flags; \
646 u32 __func; \
4f9dbcfc 647 \
21f86625
AT
648 local_irq_save(__flags); \
649 arch_efi_call_virt_setup(); \
4f9dbcfc 650 \
21f86625
AT
651 __func = runtime_service32(f); \
652 __s = efi64_thunk(__func, __VA_ARGS__); \
4f9dbcfc 653 \
21f86625
AT
654 arch_efi_call_virt_teardown(); \
655 local_irq_restore(__flags); \
4f9dbcfc
MF
656 \
657 __s; \
658})
659
660efi_status_t efi_thunk_set_virtual_address_map(
661 void *phys_set_virtual_address_map,
662 unsigned long memory_map_size,
663 unsigned long descriptor_size,
664 u32 descriptor_version,
665 efi_memory_desc_t *virtual_map)
666{
667 efi_status_t status;
668 unsigned long flags;
669 u32 func;
670
671 efi_sync_low_kernel_mappings();
672 local_irq_save(flags);
673
6c690ee1 674 efi_scratch.prev_cr3 = __read_cr3();
4f9dbcfc
MF
675 write_cr3((unsigned long)efi_scratch.efi_pgt);
676 __flush_tlb_all();
677
678 func = (u32)(unsigned long)phys_set_virtual_address_map;
679 status = efi64_thunk(func, memory_map_size, descriptor_size,
680 descriptor_version, virtual_map);
681
682 write_cr3(efi_scratch.prev_cr3);
683 __flush_tlb_all();
684 local_irq_restore(flags);
685
686 return status;
687}
688
689static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
690{
691 efi_status_t status;
692 u32 phys_tm, phys_tc;
693
694 spin_lock(&rtc_lock);
695
f6697df3
MF
696 phys_tm = virt_to_phys_or_null(tm);
697 phys_tc = virt_to_phys_or_null(tc);
4f9dbcfc
MF
698
699 status = efi_thunk(get_time, phys_tm, phys_tc);
700
701 spin_unlock(&rtc_lock);
702
703 return status;
704}
705
706static efi_status_t efi_thunk_set_time(efi_time_t *tm)
707{
708 efi_status_t status;
709 u32 phys_tm;
710
711 spin_lock(&rtc_lock);
712
f6697df3 713 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
714
715 status = efi_thunk(set_time, phys_tm);
716
717 spin_unlock(&rtc_lock);
718
719 return status;
720}
721
722static efi_status_t
723efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
724 efi_time_t *tm)
725{
726 efi_status_t status;
727 u32 phys_enabled, phys_pending, phys_tm;
728
729 spin_lock(&rtc_lock);
730
f6697df3
MF
731 phys_enabled = virt_to_phys_or_null(enabled);
732 phys_pending = virt_to_phys_or_null(pending);
733 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
734
735 status = efi_thunk(get_wakeup_time, phys_enabled,
736 phys_pending, phys_tm);
737
738 spin_unlock(&rtc_lock);
739
740 return status;
741}
742
743static efi_status_t
744efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
745{
746 efi_status_t status;
747 u32 phys_tm;
748
749 spin_lock(&rtc_lock);
750
f6697df3 751 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
752
753 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
754
755 spin_unlock(&rtc_lock);
756
757 return status;
758}
759
f6697df3
MF
760static unsigned long efi_name_size(efi_char16_t *name)
761{
762 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
763}
4f9dbcfc
MF
764
765static efi_status_t
766efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
767 u32 *attr, unsigned long *data_size, void *data)
768{
769 efi_status_t status;
770 u32 phys_name, phys_vendor, phys_attr;
771 u32 phys_data_size, phys_data;
772
f6697df3
MF
773 phys_data_size = virt_to_phys_or_null(data_size);
774 phys_vendor = virt_to_phys_or_null(vendor);
775 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
776 phys_attr = virt_to_phys_or_null(attr);
777 phys_data = virt_to_phys_or_null_size(data, *data_size);
4f9dbcfc
MF
778
779 status = efi_thunk(get_variable, phys_name, phys_vendor,
780 phys_attr, phys_data_size, phys_data);
781
782 return status;
783}
784
785static efi_status_t
786efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
787 u32 attr, unsigned long data_size, void *data)
788{
789 u32 phys_name, phys_vendor, phys_data;
790 efi_status_t status;
791
f6697df3
MF
792 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
793 phys_vendor = virt_to_phys_or_null(vendor);
794 phys_data = virt_to_phys_or_null_size(data, data_size);
4f9dbcfc
MF
795
796 /* If data_size is > sizeof(u32) we've got problems */
797 status = efi_thunk(set_variable, phys_name, phys_vendor,
798 attr, data_size, phys_data);
799
800 return status;
801}
802
803static efi_status_t
804efi_thunk_get_next_variable(unsigned long *name_size,
805 efi_char16_t *name,
806 efi_guid_t *vendor)
807{
808 efi_status_t status;
809 u32 phys_name_size, phys_name, phys_vendor;
810
f6697df3
MF
811 phys_name_size = virt_to_phys_or_null(name_size);
812 phys_vendor = virt_to_phys_or_null(vendor);
813 phys_name = virt_to_phys_or_null_size(name, *name_size);
4f9dbcfc
MF
814
815 status = efi_thunk(get_next_variable, phys_name_size,
816 phys_name, phys_vendor);
817
818 return status;
819}
820
821static efi_status_t
822efi_thunk_get_next_high_mono_count(u32 *count)
823{
824 efi_status_t status;
825 u32 phys_count;
826
f6697df3 827 phys_count = virt_to_phys_or_null(count);
4f9dbcfc
MF
828 status = efi_thunk(get_next_high_mono_count, phys_count);
829
830 return status;
831}
832
833static void
834efi_thunk_reset_system(int reset_type, efi_status_t status,
835 unsigned long data_size, efi_char16_t *data)
836{
837 u32 phys_data;
838
f6697df3 839 phys_data = virt_to_phys_or_null_size(data, data_size);
4f9dbcfc
MF
840
841 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
842}
843
844static efi_status_t
845efi_thunk_update_capsule(efi_capsule_header_t **capsules,
846 unsigned long count, unsigned long sg_list)
847{
848 /*
849 * To properly support this function we would need to repackage
850 * 'capsules' because the firmware doesn't understand 64-bit
851 * pointers.
852 */
853 return EFI_UNSUPPORTED;
854}
855
856static efi_status_t
857efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
858 u64 *remaining_space,
859 u64 *max_variable_size)
860{
861 efi_status_t status;
862 u32 phys_storage, phys_remaining, phys_max;
863
864 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
865 return EFI_UNSUPPORTED;
866
f6697df3
MF
867 phys_storage = virt_to_phys_or_null(storage_space);
868 phys_remaining = virt_to_phys_or_null(remaining_space);
869 phys_max = virt_to_phys_or_null(max_variable_size);
4f9dbcfc 870
9a11040f 871 status = efi_thunk(query_variable_info, attr, phys_storage,
4f9dbcfc
MF
872 phys_remaining, phys_max);
873
874 return status;
875}
876
877static efi_status_t
878efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
879 unsigned long count, u64 *max_size,
880 int *reset_type)
881{
882 /*
883 * To properly support this function we would need to repackage
884 * 'capsules' because the firmware doesn't understand 64-bit
885 * pointers.
886 */
887 return EFI_UNSUPPORTED;
888}
889
890void efi_thunk_runtime_setup(void)
891{
892 efi.get_time = efi_thunk_get_time;
893 efi.set_time = efi_thunk_set_time;
894 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
895 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
896 efi.get_variable = efi_thunk_get_variable;
897 efi.get_next_variable = efi_thunk_get_next_variable;
898 efi.set_variable = efi_thunk_set_variable;
899 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
900 efi.reset_system = efi_thunk_reset_system;
901 efi.query_variable_info = efi_thunk_query_variable_info;
902 efi.update_capsule = efi_thunk_update_capsule;
903 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
904}
905#endif /* CONFIG_EFI_MIXED */