mm: remove include/linux/bootmem.h
[linux-2.6-block.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>
57c8a661 26#include <linux/memblock.h>
5b83683f 27#include <linux/ioport.h>
5ab788d7 28#include <linux/mc146818rtc.h>
5b83683f 29#include <linux/efi.h>
116fef64 30#include <linux/export.h>
5b83683f
HY
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>
03781e40 37#include <linux/sched/task.h>
5b83683f
HY
38
39#include <asm/setup.h>
40#include <asm/page.h>
66441bd3 41#include <asm/e820/api.h>
5b83683f
HY
42#include <asm/pgtable.h>
43#include <asm/tlbflush.h>
5b83683f
HY
44#include <asm/proto.h>
45#include <asm/efi.h>
4de0d4a6 46#include <asm/cacheflush.h>
3819cd48 47#include <asm/fixmap.h>
d2f7cbe7 48#include <asm/realmode.h>
4f9dbcfc 49#include <asm/time.h>
67a9108e 50#include <asm/pgalloc.h>
5b83683f 51
d2f7cbe7 52/*
b1d17761 53 * We allocate runtime services regions top-down, starting from -4G, i.e.
d2f7cbe7
BP
54 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
55 */
8266e31e 56static u64 efi_va = EFI_VA_START;
d2f7cbe7 57
c9f2a9a6 58struct efi_scratch efi_scratch;
d2f7cbe7 59
9cd2b07c 60static void __init early_code_mapping_set_exec(int executable)
5b83683f
HY
61{
62 efi_memory_desc_t *md;
5b83683f 63
a2172e25
HY
64 if (!(__supported_pte_mask & _PAGE_NX))
65 return;
66
916f676f 67 /* Make EFI service code area executable */
78ce248f 68 for_each_efi_memory_desc(md) {
916f676f
MG
69 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
70 md->type == EFI_BOOT_SERVICES_CODE)
9cd2b07c 71 efi_set_executable(md, executable);
5b83683f
HY
72 }
73}
74
744937b0 75pgd_t * __init efi_call_phys_prolog(void)
5b83683f 76{
94133e46
BH
77 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
78 pgd_t *save_pgd, *pgd_k, *pgd_efi;
79 p4d_t *p4d, *p4d_k, *p4d_efi;
80 pud_t *pud;
744937b0 81
b8f2c21d 82 int pgd;
94133e46 83 int n_pgds, i, j;
5b83683f 84
c9f2a9a6 85 if (!efi_enabled(EFI_OLD_MEMMAP)) {
03781e40
SP
86 efi_switch_mm(&efi_mm);
87 return NULL;
c9f2a9a6 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 }
de53c378 138 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
b8f2c21d 139 }
de53c378 140
c9f2a9a6 141out:
5b83683f 142 __flush_tlb_all();
744937b0
IM
143
144 return save_pgd;
5b83683f
HY
145}
146
744937b0 147void __init efi_call_phys_epilog(pgd_t *save_pgd)
5b83683f
HY
148{
149 /*
150 * After the lock is released, the original page table is restored.
151 */
94133e46 152 int pgd_idx, i;
744937b0 153 int nr_pgds;
94133e46
BH
154 pgd_t *pgd;
155 p4d_t *p4d;
156 pud_t *pud;
d2f7cbe7 157
c9f2a9a6 158 if (!efi_enabled(EFI_OLD_MEMMAP)) {
03781e40 159 efi_switch_mm(efi_scratch.prev_mm);
d2f7cbe7 160 return;
c9f2a9a6 161 }
d2f7cbe7 162
744937b0
IM
163 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
164
94133e46
BH
165 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
166 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
744937b0
IM
167 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
168
cfe19577 169 if (!pgd_present(*pgd))
94133e46
BH
170 continue;
171
172 for (i = 0; i < PTRS_PER_P4D; i++) {
173 p4d = p4d_offset(pgd,
174 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
175
cfe19577 176 if (!p4d_present(*p4d))
94133e46
BH
177 continue;
178
179 pud = (pud_t *)p4d_page_vaddr(*p4d);
180 pud_free(&init_mm, pud);
181 }
182
183 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
184 p4d_free(&init_mm, p4d);
185 }
186
b8f2c21d 187 kfree(save_pgd);
744937b0 188
5b83683f 189 __flush_tlb_all();
9cd2b07c 190 early_code_mapping_set_exec(0);
5b83683f 191}
e1ad783b 192
3ede3417 193EXPORT_SYMBOL_GPL(efi_mm);
67a9108e
MF
194
195/*
196 * We need our own copy of the higher levels of the page tables
197 * because we want to avoid inserting EFI region mappings (EFI_VA_END
198 * to EFI_VA_START) into the standard kernel page tables. Everything
199 * else can be shared, see efi_sync_low_kernel_mappings().
d9e9a641
DH
200 *
201 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
202 * allocation.
67a9108e
MF
203 */
204int __init efi_alloc_page_tables(void)
205{
3ede3417 206 pgd_t *pgd, *efi_pgd;
e981316f 207 p4d_t *p4d;
67a9108e
MF
208 pud_t *pud;
209 gfp_t gfp_mask;
210
211 if (efi_enabled(EFI_OLD_MEMMAP))
212 return 0;
213
75f296d9 214 gfp_mask = GFP_KERNEL | __GFP_ZERO;
d9e9a641 215 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
67a9108e
MF
216 if (!efi_pgd)
217 return -ENOMEM;
218
219 pgd = efi_pgd + pgd_index(EFI_VA_END);
e981316f
KS
220 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
221 if (!p4d) {
222 free_page((unsigned long)efi_pgd);
223 return -ENOMEM;
224 }
67a9108e 225
e981316f 226 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
67a9108e 227 if (!pud) {
ed7588d5 228 if (pgtable_l5_enabled())
e981316f 229 free_page((unsigned long) pgd_page_vaddr(*pgd));
06ace26f 230 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
67a9108e
MF
231 return -ENOMEM;
232 }
233
3ede3417 234 efi_mm.pgd = efi_pgd;
7e904a91
SP
235 mm_init_cpumask(&efi_mm);
236 init_new_context(NULL, &efi_mm);
237
67a9108e
MF
238 return 0;
239}
240
d2f7cbe7
BP
241/*
242 * Add low kernel mappings for passing arguments to EFI functions.
243 */
244void efi_sync_low_kernel_mappings(void)
245{
67a9108e
MF
246 unsigned num_entries;
247 pgd_t *pgd_k, *pgd_efi;
e0c4f675 248 p4d_t *p4d_k, *p4d_efi;
67a9108e 249 pud_t *pud_k, *pud_efi;
3ede3417 250 pgd_t *efi_pgd = efi_mm.pgd;
d2f7cbe7
BP
251
252 if (efi_enabled(EFI_OLD_MEMMAP))
253 return;
254
67a9108e
MF
255 /*
256 * We can share all PGD entries apart from the one entry that
257 * covers the EFI runtime mapping space.
258 *
259 * Make sure the EFI runtime region mappings are guaranteed to
260 * only span a single PGD entry and that the entry also maps
261 * other important kernel regions.
262 */
c65e774f
KS
263 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
264 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
67a9108e
MF
265 (EFI_VA_END & PGDIR_MASK));
266
267 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
268 pgd_k = pgd_offset_k(PAGE_OFFSET);
269
270 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
271 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
d2f7cbe7 272
e981316f
KS
273 /*
274 * As with PGDs, we share all P4D entries apart from the one entry
275 * that covers the EFI runtime mapping space.
276 */
277 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
278 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
279
280 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
281 pgd_k = pgd_offset_k(EFI_VA_END);
282 p4d_efi = p4d_offset(pgd_efi, 0);
283 p4d_k = p4d_offset(pgd_k, 0);
284
285 num_entries = p4d_index(EFI_VA_END);
286 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
287
67a9108e
MF
288 /*
289 * We share all the PUD entries apart from those that map the
290 * EFI regions. Copy around them.
291 */
292 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
293 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
294
e981316f
KS
295 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
296 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
e0c4f675 297 pud_efi = pud_offset(p4d_efi, 0);
e0c4f675 298 pud_k = pud_offset(p4d_k, 0);
67a9108e
MF
299
300 num_entries = pud_index(EFI_VA_END);
301 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
302
e0c4f675 303 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
e0c4f675 304 pud_k = pud_offset(p4d_k, EFI_VA_START);
67a9108e
MF
305
306 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
307 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
d2f7cbe7
BP
308}
309
f6697df3
MF
310/*
311 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
312 */
313static inline phys_addr_t
314virt_to_phys_or_null_size(void *va, unsigned long size)
315{
316 bool bad_size;
317
318 if (!va)
319 return 0;
320
321 if (virt_addr_valid(va))
322 return virt_to_phys(va);
323
324 /*
325 * A fully aligned variable on the stack is guaranteed not to
326 * cross a page bounary. Try to catch strings on the stack by
327 * checking that 'size' is a power of two.
328 */
329 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
330
331 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
332
333 return slow_virt_to_phys(va);
334}
335
336#define virt_to_phys_or_null(addr) \
337 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
338
4e78eb05 339int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
d2f7cbe7 340{
38eecccd 341 unsigned long pfn, text, pf;
4f9dbcfc 342 struct page *page;
994448f1 343 unsigned npages;
3ede3417 344 pgd_t *pgd = efi_mm.pgd;
b7b898ae
BP
345
346 if (efi_enabled(EFI_OLD_MEMMAP))
347 return 0;
348
b7b898ae
BP
349 /*
350 * It can happen that the physical address of new_memmap lands in memory
351 * which is not mapped in the EFI page table. Therefore we need to go
352 * and ident-map those pages containing the map before calling
353 * phys_efi_set_virtual_address_map().
354 */
edc3b912 355 pfn = pa_memmap >> PAGE_SHIFT;
38eecccd
TL
356 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
357 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
b7b898ae
BP
358 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
359 return 1;
360 }
361
bf29bddf
JK
362 /*
363 * Certain firmware versions are way too sentimential and still believe
364 * they are exclusive and unquestionable owners of the first physical page,
365 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
366 * (but then write-access it later during SetVirtualAddressMap()).
367 *
368 * Create a 1:1 mapping for this page, to avoid triple faults during early
369 * boot with such firmware. We are free to hand this page to the BIOS,
370 * as trim_bios_range() will reserve the first page and isolate it away
371 * from memory allocators anyway.
372 */
1379edd5
TL
373 pf = _PAGE_RW;
374 if (sev_active())
375 pf |= _PAGE_ENC;
376
377 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
bf29bddf
JK
378 pr_err("Failed to create 1:1 mapping for the first page!\n");
379 return 1;
380 }
381
4f9dbcfc
MF
382 /*
383 * When making calls to the firmware everything needs to be 1:1
384 * mapped and addressable with 32-bit pointers. Map the kernel
385 * text and allocate a new stack because we can't rely on the
386 * stack pointer being < 4GB.
387 */
12976670 388 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
994448f1 389 return 0;
4f9dbcfc
MF
390
391 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
392 if (!page)
393 panic("Unable to allocate EFI runtime stack < 4GB\n");
394
395 efi_scratch.phys_stack = virt_to_phys(page_address(page));
396 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
397
2ad510dc 398 npages = (_etext - _text) >> PAGE_SHIFT;
4f9dbcfc 399 text = __pa(_text);
edc3b912 400 pfn = text >> PAGE_SHIFT;
4f9dbcfc 401
38eecccd
TL
402 pf = _PAGE_RW | _PAGE_ENC;
403 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
4f9dbcfc 404 pr_err("Failed to map kernel text 1:1\n");
994448f1 405 return 1;
4f9dbcfc 406 }
b7b898ae
BP
407
408 return 0;
409}
410
d2f7cbe7
BP
411static void __init __map_region(efi_memory_desc_t *md, u64 va)
412{
15f003d2 413 unsigned long flags = _PAGE_RW;
edc3b912 414 unsigned long pfn;
3ede3417 415 pgd_t *pgd = efi_mm.pgd;
d2f7cbe7
BP
416
417 if (!(md->attribute & EFI_MEMORY_WB))
edc3b912 418 flags |= _PAGE_PCD;
d2f7cbe7 419
9b788f32 420 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
1379edd5
TL
421 flags |= _PAGE_ENC;
422
edc3b912
MF
423 pfn = md->phys_addr >> PAGE_SHIFT;
424 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
d2f7cbe7
BP
425 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
426 md->phys_addr, va);
427}
428
429void __init efi_map_region(efi_memory_desc_t *md)
430{
431 unsigned long size = md->num_pages << PAGE_SHIFT;
432 u64 pa = md->phys_addr;
433
434 if (efi_enabled(EFI_OLD_MEMMAP))
435 return old_map_region(md);
436
437 /*
438 * Make sure the 1:1 mappings are present as a catch-all for b0rked
439 * firmware which doesn't update all internal pointers after switching
440 * to virtual mode and would otherwise crap on us.
441 */
442 __map_region(md, md->phys_addr);
443
4f9dbcfc
MF
444 /*
445 * Enforce the 1:1 mapping as the default virtual address when
446 * booting in EFI mixed mode, because even though we may be
447 * running a 64-bit kernel, the firmware may only be 32-bit.
448 */
449 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
450 md->virt_addr = md->phys_addr;
451 return;
452 }
453
d2f7cbe7
BP
454 efi_va -= size;
455
456 /* Is PA 2M-aligned? */
457 if (!(pa & (PMD_SIZE - 1))) {
458 efi_va &= PMD_MASK;
459 } else {
460 u64 pa_offset = pa & (PMD_SIZE - 1);
461 u64 prev_va = efi_va;
462
463 /* get us the same offset within this 2M page */
464 efi_va = (efi_va & PMD_MASK) + pa_offset;
465
466 if (efi_va > prev_va)
467 efi_va -= PMD_SIZE;
468 }
469
470 if (efi_va < EFI_VA_END) {
471 pr_warn(FW_WARN "VA address range overflow!\n");
472 return;
473 }
474
475 /* Do the VA map */
476 __map_region(md, efi_va);
477 md->virt_addr = efi_va;
478}
479
3b266496
DY
480/*
481 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
482 * md->virt_addr is the original virtual address which had been mapped in kexec
483 * 1st kernel.
484 */
485void __init efi_map_region_fixed(efi_memory_desc_t *md)
486{
0513fe1d 487 __map_region(md, md->phys_addr);
3b266496
DY
488 __map_region(md, md->virt_addr);
489}
490
e1ad783b 491void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
3e8fa263 492 u32 type, u64 attribute)
e1ad783b
KP
493{
494 unsigned long last_map_pfn;
495
496 if (type == EFI_MEMORY_MAPPED_IO)
497 return ioremap(phys_addr, size);
498
499 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
500 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
501 unsigned long top = last_map_pfn << PAGE_SHIFT;
3e8fa263 502 efi_ioremap(top, size - (top - phys_addr), type, attribute);
e1ad783b
KP
503 }
504
3e8fa263
MF
505 if (!(attribute & EFI_MEMORY_WB))
506 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
507
e1ad783b
KP
508 return (void __iomem *)__va(phys_addr);
509}
1fec0533
DY
510
511void __init parse_efi_setup(u64 phys_addr, u32 data_len)
512{
513 efi_setup = phys_addr + sizeof(struct setup_data);
1fec0533 514}
c55d016f 515
18141e89 516static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
c55d016f 517{
6d0cc887 518 unsigned long pfn;
3ede3417 519 pgd_t *pgd = efi_mm.pgd;
18141e89
SP
520 int err1, err2;
521
522 /* Update the 1:1 mapping */
523 pfn = md->phys_addr >> PAGE_SHIFT;
524 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
525 if (err1) {
526 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
527 md->phys_addr, md->virt_addr);
528 }
529
530 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
531 if (err2) {
532 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
533 md->phys_addr, md->virt_addr);
534 }
535
536 return err1 || err2;
537}
538
539static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
540{
541 unsigned long pf = 0;
542
543 if (md->attribute & EFI_MEMORY_XP)
544 pf |= _PAGE_NX;
545
546 if (!(md->attribute & EFI_MEMORY_RO))
547 pf |= _PAGE_RW;
548
1379edd5
TL
549 if (sev_active())
550 pf |= _PAGE_ENC;
551
18141e89
SP
552 return efi_update_mappings(md, pf);
553}
554
555void __init efi_runtime_update_mappings(void)
556{
6d0cc887 557 efi_memory_desc_t *md;
6d0cc887
SP
558
559 if (efi_enabled(EFI_OLD_MEMMAP)) {
560 if (__supported_pte_mask & _PAGE_NX)
561 runtime_code_page_mkexec();
562 return;
563 }
564
18141e89
SP
565 /*
566 * Use the EFI Memory Attribute Table for mapping permissions if it
567 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
568 */
569 if (efi_enabled(EFI_MEM_ATTR)) {
570 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
571 return;
572 }
573
574 /*
575 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
576 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
577 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
578 * published by the firmware. Even if we find a buggy implementation of
579 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
580 * EFI_PROPERTIES_TABLE, because of the same reason.
581 */
582
6d0cc887 583 if (!efi_enabled(EFI_NX_PE_DATA))
c55d016f
BP
584 return;
585
78ce248f 586 for_each_efi_memory_desc(md) {
6d0cc887 587 unsigned long pf = 0;
6d0cc887
SP
588
589 if (!(md->attribute & EFI_MEMORY_RUNTIME))
590 continue;
591
592 if (!(md->attribute & EFI_MEMORY_WB))
593 pf |= _PAGE_PCD;
594
595 if ((md->attribute & EFI_MEMORY_XP) ||
596 (md->type == EFI_RUNTIME_SERVICES_DATA))
597 pf |= _PAGE_NX;
598
599 if (!(md->attribute & EFI_MEMORY_RO) &&
600 (md->type != EFI_RUNTIME_SERVICES_CODE))
601 pf |= _PAGE_RW;
602
1379edd5
TL
603 if (sev_active())
604 pf |= _PAGE_ENC;
605
18141e89 606 efi_update_mappings(md, pf);
6d0cc887 607 }
c55d016f 608}
11cc8512
BP
609
610void __init efi_dump_pagetable(void)
611{
612#ifdef CONFIG_EFI_PGT_DUMP
ac81d3de
SP
613 if (efi_enabled(EFI_OLD_MEMMAP))
614 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
615 else
3ede3417 616 ptdump_walk_pgd_level(NULL, efi_mm.pgd);
11cc8512
BP
617#endif
618}
994448f1 619
03781e40
SP
620/*
621 * Makes the calling thread switch to/from efi_mm context. Can be used
4eda1117
SAS
622 * in a kernel thread and user context. Preemption needs to remain disabled
623 * while the EFI-mm is borrowed. mmgrab()/mmdrop() is not used because the mm
624 * can not change under us.
625 * It should be ensured that there are no concurent calls to this function.
03781e40
SP
626 */
627void efi_switch_mm(struct mm_struct *mm)
628{
03781e40
SP
629 efi_scratch.prev_mm = current->active_mm;
630 current->active_mm = mm;
631 switch_mm(efi_scratch.prev_mm, mm, NULL);
03781e40
SP
632}
633
4f9dbcfc
MF
634#ifdef CONFIG_EFI_MIXED
635extern efi_status_t efi64_thunk(u32, ...);
636
83a0a2ea
AB
637static DEFINE_SPINLOCK(efi_runtime_lock);
638
4f9dbcfc
MF
639#define runtime_service32(func) \
640({ \
641 u32 table = (u32)(unsigned long)efi.systab; \
642 u32 *rt, *___f; \
643 \
644 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
645 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
646 *___f; \
647})
648
649/*
650 * Switch to the EFI page tables early so that we can access the 1:1
651 * runtime services mappings which are not mapped in any other page
652 * tables. This function must be called before runtime_service32().
653 *
654 * Also, disable interrupts because the IDT points to 64-bit handlers,
655 * which aren't going to function correctly when we switch to 32-bit.
656 */
657#define efi_thunk(f, ...) \
658({ \
659 efi_status_t __s; \
21f86625 660 u32 __func; \
4f9dbcfc 661 \
21f86625 662 arch_efi_call_virt_setup(); \
4f9dbcfc 663 \
21f86625
AT
664 __func = runtime_service32(f); \
665 __s = efi64_thunk(__func, __VA_ARGS__); \
4f9dbcfc 666 \
21f86625 667 arch_efi_call_virt_teardown(); \
4f9dbcfc
MF
668 \
669 __s; \
670})
671
672efi_status_t efi_thunk_set_virtual_address_map(
673 void *phys_set_virtual_address_map,
674 unsigned long memory_map_size,
675 unsigned long descriptor_size,
676 u32 descriptor_version,
677 efi_memory_desc_t *virtual_map)
678{
679 efi_status_t status;
680 unsigned long flags;
681 u32 func;
682
683 efi_sync_low_kernel_mappings();
684 local_irq_save(flags);
685
03781e40 686 efi_switch_mm(&efi_mm);
4f9dbcfc
MF
687
688 func = (u32)(unsigned long)phys_set_virtual_address_map;
689 status = efi64_thunk(func, memory_map_size, descriptor_size,
690 descriptor_version, virtual_map);
691
03781e40 692 efi_switch_mm(efi_scratch.prev_mm);
4f9dbcfc
MF
693 local_irq_restore(flags);
694
695 return status;
696}
697
698static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
699{
700 efi_status_t status;
701 u32 phys_tm, phys_tc;
83a0a2ea 702 unsigned long flags;
4f9dbcfc
MF
703
704 spin_lock(&rtc_lock);
83a0a2ea 705 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 706
f6697df3
MF
707 phys_tm = virt_to_phys_or_null(tm);
708 phys_tc = virt_to_phys_or_null(tc);
4f9dbcfc
MF
709
710 status = efi_thunk(get_time, phys_tm, phys_tc);
711
83a0a2ea 712 spin_unlock_irqrestore(&efi_runtime_lock, flags);
4f9dbcfc
MF
713 spin_unlock(&rtc_lock);
714
715 return status;
716}
717
718static efi_status_t efi_thunk_set_time(efi_time_t *tm)
719{
720 efi_status_t status;
721 u32 phys_tm;
83a0a2ea 722 unsigned long flags;
4f9dbcfc
MF
723
724 spin_lock(&rtc_lock);
83a0a2ea 725 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 726
f6697df3 727 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
728
729 status = efi_thunk(set_time, phys_tm);
730
83a0a2ea 731 spin_unlock_irqrestore(&efi_runtime_lock, flags);
4f9dbcfc
MF
732 spin_unlock(&rtc_lock);
733
734 return status;
735}
736
737static efi_status_t
738efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
739 efi_time_t *tm)
740{
741 efi_status_t status;
742 u32 phys_enabled, phys_pending, phys_tm;
83a0a2ea 743 unsigned long flags;
4f9dbcfc
MF
744
745 spin_lock(&rtc_lock);
83a0a2ea 746 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 747
f6697df3
MF
748 phys_enabled = virt_to_phys_or_null(enabled);
749 phys_pending = virt_to_phys_or_null(pending);
750 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
751
752 status = efi_thunk(get_wakeup_time, phys_enabled,
753 phys_pending, phys_tm);
754
83a0a2ea 755 spin_unlock_irqrestore(&efi_runtime_lock, flags);
4f9dbcfc
MF
756 spin_unlock(&rtc_lock);
757
758 return status;
759}
760
761static efi_status_t
762efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
763{
764 efi_status_t status;
765 u32 phys_tm;
83a0a2ea 766 unsigned long flags;
4f9dbcfc
MF
767
768 spin_lock(&rtc_lock);
83a0a2ea 769 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 770
f6697df3 771 phys_tm = virt_to_phys_or_null(tm);
4f9dbcfc
MF
772
773 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
774
83a0a2ea 775 spin_unlock_irqrestore(&efi_runtime_lock, flags);
4f9dbcfc
MF
776 spin_unlock(&rtc_lock);
777
778 return status;
779}
780
f6697df3
MF
781static unsigned long efi_name_size(efi_char16_t *name)
782{
783 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
784}
4f9dbcfc
MF
785
786static efi_status_t
787efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
788 u32 *attr, unsigned long *data_size, void *data)
789{
790 efi_status_t status;
791 u32 phys_name, phys_vendor, phys_attr;
792 u32 phys_data_size, phys_data;
83a0a2ea
AB
793 unsigned long flags;
794
795 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 796
f6697df3
MF
797 phys_data_size = virt_to_phys_or_null(data_size);
798 phys_vendor = virt_to_phys_or_null(vendor);
799 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
800 phys_attr = virt_to_phys_or_null(attr);
801 phys_data = virt_to_phys_or_null_size(data, *data_size);
4f9dbcfc
MF
802
803 status = efi_thunk(get_variable, phys_name, phys_vendor,
804 phys_attr, phys_data_size, phys_data);
805
83a0a2ea
AB
806 spin_unlock_irqrestore(&efi_runtime_lock, flags);
807
4f9dbcfc
MF
808 return status;
809}
810
811static efi_status_t
812efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
813 u32 attr, unsigned long data_size, void *data)
814{
815 u32 phys_name, phys_vendor, phys_data;
816 efi_status_t status;
83a0a2ea
AB
817 unsigned long flags;
818
819 spin_lock_irqsave(&efi_runtime_lock, flags);
820
821 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
822 phys_vendor = virt_to_phys_or_null(vendor);
823 phys_data = virt_to_phys_or_null_size(data, data_size);
824
825 /* If data_size is > sizeof(u32) we've got problems */
826 status = efi_thunk(set_variable, phys_name, phys_vendor,
827 attr, data_size, phys_data);
828
829 spin_unlock_irqrestore(&efi_runtime_lock, flags);
830
831 return status;
832}
833
834static efi_status_t
835efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
836 u32 attr, unsigned long data_size,
837 void *data)
838{
839 u32 phys_name, phys_vendor, phys_data;
840 efi_status_t status;
841 unsigned long flags;
842
843 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
844 return EFI_NOT_READY;
4f9dbcfc 845
f6697df3
MF
846 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
847 phys_vendor = virt_to_phys_or_null(vendor);
848 phys_data = virt_to_phys_or_null_size(data, data_size);
4f9dbcfc
MF
849
850 /* If data_size is > sizeof(u32) we've got problems */
851 status = efi_thunk(set_variable, phys_name, phys_vendor,
852 attr, data_size, phys_data);
853
83a0a2ea
AB
854 spin_unlock_irqrestore(&efi_runtime_lock, flags);
855
4f9dbcfc
MF
856 return status;
857}
858
859static efi_status_t
860efi_thunk_get_next_variable(unsigned long *name_size,
861 efi_char16_t *name,
862 efi_guid_t *vendor)
863{
864 efi_status_t status;
865 u32 phys_name_size, phys_name, phys_vendor;
83a0a2ea
AB
866 unsigned long flags;
867
868 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 869
f6697df3
MF
870 phys_name_size = virt_to_phys_or_null(name_size);
871 phys_vendor = virt_to_phys_or_null(vendor);
872 phys_name = virt_to_phys_or_null_size(name, *name_size);
4f9dbcfc
MF
873
874 status = efi_thunk(get_next_variable, phys_name_size,
875 phys_name, phys_vendor);
876
83a0a2ea
AB
877 spin_unlock_irqrestore(&efi_runtime_lock, flags);
878
4f9dbcfc
MF
879 return status;
880}
881
882static efi_status_t
883efi_thunk_get_next_high_mono_count(u32 *count)
884{
885 efi_status_t status;
886 u32 phys_count;
83a0a2ea
AB
887 unsigned long flags;
888
889 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 890
f6697df3 891 phys_count = virt_to_phys_or_null(count);
4f9dbcfc
MF
892 status = efi_thunk(get_next_high_mono_count, phys_count);
893
83a0a2ea
AB
894 spin_unlock_irqrestore(&efi_runtime_lock, flags);
895
4f9dbcfc
MF
896 return status;
897}
898
899static void
900efi_thunk_reset_system(int reset_type, efi_status_t status,
901 unsigned long data_size, efi_char16_t *data)
902{
903 u32 phys_data;
83a0a2ea
AB
904 unsigned long flags;
905
906 spin_lock_irqsave(&efi_runtime_lock, flags);
4f9dbcfc 907
f6697df3 908 phys_data = virt_to_phys_or_null_size(data, data_size);
4f9dbcfc
MF
909
910 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
83a0a2ea
AB
911
912 spin_unlock_irqrestore(&efi_runtime_lock, flags);
4f9dbcfc
MF
913}
914
915static efi_status_t
916efi_thunk_update_capsule(efi_capsule_header_t **capsules,
917 unsigned long count, unsigned long sg_list)
918{
919 /*
920 * To properly support this function we would need to repackage
921 * 'capsules' because the firmware doesn't understand 64-bit
922 * pointers.
923 */
924 return EFI_UNSUPPORTED;
925}
926
927static efi_status_t
928efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
929 u64 *remaining_space,
930 u64 *max_variable_size)
931{
932 efi_status_t status;
933 u32 phys_storage, phys_remaining, phys_max;
83a0a2ea 934 unsigned long flags;
4f9dbcfc
MF
935
936 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
937 return EFI_UNSUPPORTED;
938
83a0a2ea
AB
939 spin_lock_irqsave(&efi_runtime_lock, flags);
940
f6697df3
MF
941 phys_storage = virt_to_phys_or_null(storage_space);
942 phys_remaining = virt_to_phys_or_null(remaining_space);
943 phys_max = virt_to_phys_or_null(max_variable_size);
4f9dbcfc 944
9a11040f 945 status = efi_thunk(query_variable_info, attr, phys_storage,
4f9dbcfc
MF
946 phys_remaining, phys_max);
947
83a0a2ea
AB
948 spin_unlock_irqrestore(&efi_runtime_lock, flags);
949
950 return status;
951}
952
953static efi_status_t
954efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
955 u64 *remaining_space,
956 u64 *max_variable_size)
957{
958 efi_status_t status;
959 u32 phys_storage, phys_remaining, phys_max;
960 unsigned long flags;
961
962 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
963 return EFI_UNSUPPORTED;
964
965 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
966 return EFI_NOT_READY;
967
968 phys_storage = virt_to_phys_or_null(storage_space);
969 phys_remaining = virt_to_phys_or_null(remaining_space);
970 phys_max = virt_to_phys_or_null(max_variable_size);
971
972 status = efi_thunk(query_variable_info, attr, phys_storage,
973 phys_remaining, phys_max);
974
975 spin_unlock_irqrestore(&efi_runtime_lock, flags);
976
4f9dbcfc
MF
977 return status;
978}
979
980static efi_status_t
981efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
982 unsigned long count, u64 *max_size,
983 int *reset_type)
984{
985 /*
986 * To properly support this function we would need to repackage
987 * 'capsules' because the firmware doesn't understand 64-bit
988 * pointers.
989 */
990 return EFI_UNSUPPORTED;
991}
992
993void efi_thunk_runtime_setup(void)
994{
995 efi.get_time = efi_thunk_get_time;
996 efi.set_time = efi_thunk_set_time;
997 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
998 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
999 efi.get_variable = efi_thunk_get_variable;
1000 efi.get_next_variable = efi_thunk_get_next_variable;
1001 efi.set_variable = efi_thunk_set_variable;
83a0a2ea 1002 efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
4f9dbcfc
MF
1003 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
1004 efi.reset_system = efi_thunk_reset_system;
1005 efi.query_variable_info = efi_thunk_query_variable_info;
83a0a2ea 1006 efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
4f9dbcfc
MF
1007 efi.update_capsule = efi_thunk_update_capsule;
1008 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
1009}
1010#endif /* CONFIG_EFI_MIXED */