efi_64.c 25.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
H
Huang, Ying 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * x86_64 specific EFI support functions
 * Based on Extensible Firmware Interface Specification version 1.0
 *
 * Copyright (C) 2005-2008 Intel Co.
 *	Fenghua Yu <fenghua.yu@intel.com>
 *	Bibo Mao <bibo.mao@intel.com>
 *	Chandramouli Narayanan <mouli@linux.intel.com>
 *	Huang Ying <ying.huang@intel.com>
 *
 * Code to convert EFI to E820 map has been implemented in elilo bootloader
 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
 * is setup appropriately for EFI runtime code.
 * - mouli 06/14/2007.
 *
 */

19 20
#define pr_fmt(fmt) "efi: " fmt

H
Huang, Ying 已提交
21 22 23 24 25 26 27
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/bootmem.h>
#include <linux/ioport.h>
28
#include <linux/mc146818rtc.h>
H
Huang, Ying 已提交
29
#include <linux/efi.h>
30
#include <linux/export.h>
H
Huang, Ying 已提交
31 32 33
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/reboot.h>
34
#include <linux/slab.h>
35
#include <linux/ucs2_string.h>
36
#include <linux/mem_encrypt.h>
37
#include <linux/sched/task.h>
H
Huang, Ying 已提交
38 39 40

#include <asm/setup.h>
#include <asm/page.h>
41
#include <asm/e820/api.h>
H
Huang, Ying 已提交
42 43 44 45
#include <asm/pgtable.h>
#include <asm/tlbflush.h>
#include <asm/proto.h>
#include <asm/efi.h>
46
#include <asm/cacheflush.h>
47
#include <asm/fixmap.h>
48
#include <asm/realmode.h>
49
#include <asm/time.h>
50
#include <asm/pgalloc.h>
H
Huang, Ying 已提交
51

52
/*
53
 * We allocate runtime services regions top-down, starting from -4G, i.e.
54 55
 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
 */
56
static u64 efi_va = EFI_VA_START;
57

58
struct efi_scratch efi_scratch;
59

60
static void __init early_code_mapping_set_exec(int executable)
H
Huang, Ying 已提交
61 62 63
{
	efi_memory_desc_t *md;

64 65 66
	if (!(__supported_pte_mask & _PAGE_NX))
		return;

67
	/* Make EFI service code area executable */
68
	for_each_efi_memory_desc(md) {
69 70
		if (md->type == EFI_RUNTIME_SERVICES_CODE ||
		    md->type == EFI_BOOT_SERVICES_CODE)
71
			efi_set_executable(md, executable);
H
Huang, Ying 已提交
72 73 74
	}
}

75
pgd_t * __init efi_call_phys_prolog(void)
H
Huang, Ying 已提交
76
{
77 78 79 80
	unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
	pgd_t *save_pgd, *pgd_k, *pgd_efi;
	p4d_t *p4d, *p4d_k, *p4d_efi;
	pud_t *pud;
81

82
	int pgd;
83
	int n_pgds, i, j;
H
Huang, Ying 已提交
84

85
	if (!efi_enabled(EFI_OLD_MEMMAP)) {
86
		efi_switch_mm(&efi_mm);
87
		return efi_mm.pgd;
88
	}
89

90
	early_code_mapping_set_exec(1);
91 92

	n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
93
	save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
94 95
	if (!save_pgd)
		return NULL;
96

97 98 99 100 101 102 103 104
	/*
	 * Build 1:1 identity mapping for efi=old_map usage. Note that
	 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
	 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
	 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
	 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
	 * This means here we can only reuse the PMD tables of the direct mapping.
	 */
105
	for (pgd = 0; pgd < n_pgds; pgd++) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
		vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
		pgd_efi = pgd_offset_k(addr_pgd);
		save_pgd[pgd] = *pgd_efi;

		p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
		if (!p4d) {
			pr_err("Failed to allocate p4d table!\n");
			goto out;
		}

		for (i = 0; i < PTRS_PER_P4D; i++) {
			addr_p4d = addr_pgd + i * P4D_SIZE;
			p4d_efi = p4d + p4d_index(addr_p4d);

			pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
			if (!pud) {
				pr_err("Failed to allocate pud table!\n");
				goto out;
			}

			for (j = 0; j < PTRS_PER_PUD; j++) {
				addr_pud = addr_p4d + j * PUD_SIZE;

				if (addr_pud > (max_pfn << PAGE_SHIFT))
					break;

				vaddr = (unsigned long)__va(addr_pud);

				pgd_k = pgd_offset_k(vaddr);
				p4d_k = p4d_offset(pgd_k, vaddr);
				pud[j] = *pud_offset(p4d_k, vaddr);
			}
		}
J
Jiri Kosina 已提交
140
		pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
141
	}
J
Jiri Kosina 已提交
142

H
Huang, Ying 已提交
143
	__flush_tlb_all();
144
	return save_pgd;
145 146 147
out:
	efi_call_phys_epilog(save_pgd);
	return NULL;
H
Huang, Ying 已提交
148 149
}

150
void __init efi_call_phys_epilog(pgd_t *save_pgd)
H
Huang, Ying 已提交
151 152 153 154
{
	/*
	 * After the lock is released, the original page table is restored.
	 */
155
	int pgd_idx, i;
156
	int nr_pgds;
157 158 159
	pgd_t *pgd;
	p4d_t *p4d;
	pud_t *pud;
160

161
	if (!efi_enabled(EFI_OLD_MEMMAP)) {
162
		efi_switch_mm(efi_scratch.prev_mm);
163
		return;
164
	}
165

166 167
	nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);

168 169
	for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
		pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
170 171
		set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);

172
		if (!pgd_present(*pgd))
173 174 175 176 177 178
			continue;

		for (i = 0; i < PTRS_PER_P4D; i++) {
			p4d = p4d_offset(pgd,
					 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);

179
			if (!p4d_present(*p4d))
180 181 182 183 184 185 186 187 188 189
				continue;

			pud = (pud_t *)p4d_page_vaddr(*p4d);
			pud_free(&init_mm, pud);
		}

		p4d = (p4d_t *)pgd_page_vaddr(*pgd);
		p4d_free(&init_mm, p4d);
	}

190
	kfree(save_pgd);
191

H
Huang, Ying 已提交
192
	__flush_tlb_all();
193
	early_code_mapping_set_exec(0);
H
Huang, Ying 已提交
194
}
195

196
EXPORT_SYMBOL_GPL(efi_mm);
197 198 199 200 201 202

/*
 * We need our own copy of the higher levels of the page tables
 * because we want to avoid inserting EFI region mappings (EFI_VA_END
 * to EFI_VA_START) into the standard kernel page tables. Everything
 * else can be shared, see efi_sync_low_kernel_mappings().
203 204 205
 *
 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
 * allocation.
206 207 208
 */
int __init efi_alloc_page_tables(void)
{
209
	pgd_t *pgd, *efi_pgd;
210
	p4d_t *p4d;
211 212 213 214 215 216
	pud_t *pud;
	gfp_t gfp_mask;

	if (efi_enabled(EFI_OLD_MEMMAP))
		return 0;

217
	gfp_mask = GFP_KERNEL | __GFP_ZERO;
218
	efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
219 220 221 222
	if (!efi_pgd)
		return -ENOMEM;

	pgd = efi_pgd + pgd_index(EFI_VA_END);
223 224 225 226 227
	p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
	if (!p4d) {
		free_page((unsigned long)efi_pgd);
		return -ENOMEM;
	}
228

229
	pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
230
	if (!pud) {
231
		if (pgtable_l5_enabled())
232
			free_page((unsigned long) pgd_page_vaddr(*pgd));
233
		free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
234 235 236
		return -ENOMEM;
	}

237
	efi_mm.pgd = efi_pgd;
238 239 240
	mm_init_cpumask(&efi_mm);
	init_new_context(NULL, &efi_mm);

241 242 243
	return 0;
}

244 245 246 247 248
/*
 * Add low kernel mappings for passing arguments to EFI functions.
 */
void efi_sync_low_kernel_mappings(void)
{
249 250
	unsigned num_entries;
	pgd_t *pgd_k, *pgd_efi;
251
	p4d_t *p4d_k, *p4d_efi;
252
	pud_t *pud_k, *pud_efi;
253
	pgd_t *efi_pgd = efi_mm.pgd;
254 255 256 257

	if (efi_enabled(EFI_OLD_MEMMAP))
		return;

258 259 260 261 262 263 264 265
	/*
	 * We can share all PGD entries apart from the one entry that
	 * covers the EFI runtime mapping space.
	 *
	 * Make sure the EFI runtime region mappings are guaranteed to
	 * only span a single PGD entry and that the entry also maps
	 * other important kernel regions.
	 */
266 267
	MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
	MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
268 269 270 271 272 273 274
			(EFI_VA_END & PGDIR_MASK));

	pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
	pgd_k = pgd_offset_k(PAGE_OFFSET);

	num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
	memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
275

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	/*
	 * As with PGDs, we share all P4D entries apart from the one entry
	 * that covers the EFI runtime mapping space.
	 */
	BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
	BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));

	pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
	pgd_k = pgd_offset_k(EFI_VA_END);
	p4d_efi = p4d_offset(pgd_efi, 0);
	p4d_k = p4d_offset(pgd_k, 0);

	num_entries = p4d_index(EFI_VA_END);
	memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);

291 292 293 294 295 296 297
	/*
	 * We share all the PUD entries apart from those that map the
	 * EFI regions. Copy around them.
	 */
	BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
	BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);

298 299
	p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
	p4d_k = p4d_offset(pgd_k, EFI_VA_END);
300 301
	pud_efi = pud_offset(p4d_efi, 0);
	pud_k = pud_offset(p4d_k, 0);
302 303 304 305

	num_entries = pud_index(EFI_VA_END);
	memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);

306 307
	pud_efi = pud_offset(p4d_efi, EFI_VA_START);
	pud_k = pud_offset(p4d_k, EFI_VA_START);
308 309 310

	num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
	memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
311 312
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/*
 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
 */
static inline phys_addr_t
virt_to_phys_or_null_size(void *va, unsigned long size)
{
	bool bad_size;

	if (!va)
		return 0;

	if (virt_addr_valid(va))
		return virt_to_phys(va);

	/*
	 * A fully aligned variable on the stack is guaranteed not to
	 * cross a page bounary. Try to catch strings on the stack by
	 * checking that 'size' is a power of two.
	 */
	bad_size = size > PAGE_SIZE || !is_power_of_2(size);

	WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);

	return slow_virt_to_phys(va);
}

#define virt_to_phys_or_null(addr)				\
	virt_to_phys_or_null_size((addr), sizeof(*(addr)))

342
int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
343
{
344
	unsigned long pfn, text, pf;
345
	struct page *page;
346
	unsigned npages;
347
	pgd_t *pgd = efi_mm.pgd;
348 349 350 351 352 353 354 355 356 357

	if (efi_enabled(EFI_OLD_MEMMAP))
		return 0;

	/*
	 * It can happen that the physical address of new_memmap lands in memory
	 * which is not mapped in the EFI page table. Therefore we need to go
	 * and ident-map those pages containing the map before calling
	 * phys_efi_set_virtual_address_map().
	 */
358
	pfn = pa_memmap >> PAGE_SHIFT;
359 360
	pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
	if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
361 362 363 364
		pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
		return 1;
	}

365 366 367 368 369 370 371 372 373 374 375
	/*
	 * Certain firmware versions are way too sentimential and still believe
	 * they are exclusive and unquestionable owners of the first physical page,
	 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
	 * (but then write-access it later during SetVirtualAddressMap()).
	 *
	 * Create a 1:1 mapping for this page, to avoid triple faults during early
	 * boot with such firmware. We are free to hand this page to the BIOS,
	 * as trim_bios_range() will reserve the first page and isolate it away
	 * from memory allocators anyway.
	 */
376 377 378 379 380
	pf = _PAGE_RW;
	if (sev_active())
		pf |= _PAGE_ENC;

	if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
381 382 383 384
		pr_err("Failed to create 1:1 mapping for the first page!\n");
		return 1;
	}

385 386 387 388 389 390
	/*
	 * When making calls to the firmware everything needs to be 1:1
	 * mapped and addressable with 32-bit pointers. Map the kernel
	 * text and allocate a new stack because we can't rely on the
	 * stack pointer being < 4GB.
	 */
391
	if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
392
		return 0;
393 394 395 396 397 398 399 400

	page = alloc_page(GFP_KERNEL|__GFP_DMA32);
	if (!page)
		panic("Unable to allocate EFI runtime stack < 4GB\n");

	efi_scratch.phys_stack = virt_to_phys(page_address(page));
	efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */

401
	npages = (_etext - _text) >> PAGE_SHIFT;
402
	text = __pa(_text);
403
	pfn = text >> PAGE_SHIFT;
404

405 406
	pf = _PAGE_RW | _PAGE_ENC;
	if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
407
		pr_err("Failed to map kernel text 1:1\n");
408
		return 1;
409
	}
410 411 412 413

	return 0;
}

414 415
static void __init __map_region(efi_memory_desc_t *md, u64 va)
{
416
	unsigned long flags = _PAGE_RW;
417
	unsigned long pfn;
418
	pgd_t *pgd = efi_mm.pgd;
419 420

	if (!(md->attribute & EFI_MEMORY_WB))
421
		flags |= _PAGE_PCD;
422

423
	if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
424 425
		flags |= _PAGE_ENC;

426 427
	pfn = md->phys_addr >> PAGE_SHIFT;
	if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
		pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
			   md->phys_addr, va);
}

void __init efi_map_region(efi_memory_desc_t *md)
{
	unsigned long size = md->num_pages << PAGE_SHIFT;
	u64 pa = md->phys_addr;

	if (efi_enabled(EFI_OLD_MEMMAP))
		return old_map_region(md);

	/*
	 * Make sure the 1:1 mappings are present as a catch-all for b0rked
	 * firmware which doesn't update all internal pointers after switching
	 * to virtual mode and would otherwise crap on us.
	 */
	__map_region(md, md->phys_addr);

447 448 449 450 451 452 453 454 455 456
	/*
	 * Enforce the 1:1 mapping as the default virtual address when
	 * booting in EFI mixed mode, because even though we may be
	 * running a 64-bit kernel, the firmware may only be 32-bit.
	 */
	if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
		md->virt_addr = md->phys_addr;
		return;
	}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	efi_va -= size;

	/* Is PA 2M-aligned? */
	if (!(pa & (PMD_SIZE - 1))) {
		efi_va &= PMD_MASK;
	} else {
		u64 pa_offset = pa & (PMD_SIZE - 1);
		u64 prev_va = efi_va;

		/* get us the same offset within this 2M page */
		efi_va = (efi_va & PMD_MASK) + pa_offset;

		if (efi_va > prev_va)
			efi_va -= PMD_SIZE;
	}

	if (efi_va < EFI_VA_END) {
		pr_warn(FW_WARN "VA address range overflow!\n");
		return;
	}

	/* Do the VA map */
	__map_region(md, efi_va);
	md->virt_addr = efi_va;
}

483 484 485 486 487 488 489
/*
 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
 * md->virt_addr is the original virtual address which had been mapped in kexec
 * 1st kernel.
 */
void __init efi_map_region_fixed(efi_memory_desc_t *md)
{
490
	__map_region(md, md->phys_addr);
491 492 493
	__map_region(md, md->virt_addr);
}

494
void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
495
				 u32 type, u64 attribute)
496 497 498 499 500 501 502 503 504
{
	unsigned long last_map_pfn;

	if (type == EFI_MEMORY_MAPPED_IO)
		return ioremap(phys_addr, size);

	last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
	if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
		unsigned long top = last_map_pfn << PAGE_SHIFT;
505
		efi_ioremap(top, size - (top - phys_addr), type, attribute);
506 507
	}

508 509 510
	if (!(attribute & EFI_MEMORY_WB))
		efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);

511 512
	return (void __iomem *)__va(phys_addr);
}
513 514 515 516 517

void __init parse_efi_setup(u64 phys_addr, u32 data_len)
{
	efi_setup = phys_addr + sizeof(struct setup_data);
}
B
Borislav Petkov 已提交
518

519
static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
B
Borislav Petkov 已提交
520
{
521
	unsigned long pfn;
522
	pgd_t *pgd = efi_mm.pgd;
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
	int err1, err2;

	/* Update the 1:1 mapping */
	pfn = md->phys_addr >> PAGE_SHIFT;
	err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
	if (err1) {
		pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
			   md->phys_addr, md->virt_addr);
	}

	err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
	if (err2) {
		pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
			   md->phys_addr, md->virt_addr);
	}

	return err1 || err2;
}

static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
{
	unsigned long pf = 0;

	if (md->attribute & EFI_MEMORY_XP)
		pf |= _PAGE_NX;

	if (!(md->attribute & EFI_MEMORY_RO))
		pf |= _PAGE_RW;

552 553 554
	if (sev_active())
		pf |= _PAGE_ENC;

555 556 557 558 559
	return efi_update_mappings(md, pf);
}

void __init efi_runtime_update_mappings(void)
{
560 561 562 563 564 565 566 567
	efi_memory_desc_t *md;

	if (efi_enabled(EFI_OLD_MEMMAP)) {
		if (__supported_pte_mask & _PAGE_NX)
			runtime_code_page_mkexec();
		return;
	}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	/*
	 * Use the EFI Memory Attribute Table for mapping permissions if it
	 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
	 */
	if (efi_enabled(EFI_MEM_ATTR)) {
		efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
		return;
	}

	/*
	 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
	 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
	 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
	 * published by the firmware. Even if we find a buggy implementation of
	 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
	 * EFI_PROPERTIES_TABLE, because of the same reason.
	 */

586
	if (!efi_enabled(EFI_NX_PE_DATA))
B
Borislav Petkov 已提交
587 588
		return;

589
	for_each_efi_memory_desc(md) {
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
		unsigned long pf = 0;

		if (!(md->attribute & EFI_MEMORY_RUNTIME))
			continue;

		if (!(md->attribute & EFI_MEMORY_WB))
			pf |= _PAGE_PCD;

		if ((md->attribute & EFI_MEMORY_XP) ||
			(md->type == EFI_RUNTIME_SERVICES_DATA))
			pf |= _PAGE_NX;

		if (!(md->attribute & EFI_MEMORY_RO) &&
			(md->type != EFI_RUNTIME_SERVICES_CODE))
			pf |= _PAGE_RW;

606 607 608
		if (sev_active())
			pf |= _PAGE_ENC;

609
		efi_update_mappings(md, pf);
610
	}
B
Borislav Petkov 已提交
611
}
612 613 614 615

void __init efi_dump_pagetable(void)
{
#ifdef CONFIG_EFI_PGT_DUMP
616 617 618
	if (efi_enabled(EFI_OLD_MEMMAP))
		ptdump_walk_pgd_level(NULL, swapper_pg_dir);
	else
619
		ptdump_walk_pgd_level(NULL, efi_mm.pgd);
620 621
#endif
}
622

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
/*
 * Makes the calling thread switch to/from efi_mm context. Can be used
 * for SetVirtualAddressMap() i.e. current->active_mm == init_mm as well
 * as during efi runtime calls i.e current->active_mm == current_mm.
 * We are not mm_dropping()/mm_grabbing() any mm, because we are not
 * losing/creating any references.
 */
void efi_switch_mm(struct mm_struct *mm)
{
	task_lock(current);
	efi_scratch.prev_mm = current->active_mm;
	current->active_mm = mm;
	switch_mm(efi_scratch.prev_mm, mm, NULL);
	task_unlock(current);
}

639 640 641
#ifdef CONFIG_EFI_MIXED
extern efi_status_t efi64_thunk(u32, ...);

642 643
static DEFINE_SPINLOCK(efi_runtime_lock);

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
#define runtime_service32(func)						 \
({									 \
	u32 table = (u32)(unsigned long)efi.systab;			 \
	u32 *rt, *___f;							 \
									 \
	rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime));	 \
	___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
	*___f;								 \
})

/*
 * Switch to the EFI page tables early so that we can access the 1:1
 * runtime services mappings which are not mapped in any other page
 * tables. This function must be called before runtime_service32().
 *
 * Also, disable interrupts because the IDT points to 64-bit handlers,
 * which aren't going to function correctly when we switch to 32-bit.
 */
#define efi_thunk(f, ...)						\
({									\
	efi_status_t __s;						\
665
	u32 __func;							\
666
									\
667
	arch_efi_call_virt_setup();					\
668
									\
669 670
	__func = runtime_service32(f);					\
	__s = efi64_thunk(__func, __VA_ARGS__);				\
671
									\
672
	arch_efi_call_virt_teardown();					\
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
									\
	__s;								\
})

efi_status_t efi_thunk_set_virtual_address_map(
	void *phys_set_virtual_address_map,
	unsigned long memory_map_size,
	unsigned long descriptor_size,
	u32 descriptor_version,
	efi_memory_desc_t *virtual_map)
{
	efi_status_t status;
	unsigned long flags;
	u32 func;

	efi_sync_low_kernel_mappings();
	local_irq_save(flags);

691
	efi_switch_mm(&efi_mm);
692 693 694 695 696

	func = (u32)(unsigned long)phys_set_virtual_address_map;
	status = efi64_thunk(func, memory_map_size, descriptor_size,
			     descriptor_version, virtual_map);

697
	efi_switch_mm(efi_scratch.prev_mm);
698 699 700 701 702 703 704 705 706
	local_irq_restore(flags);

	return status;
}

static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
{
	efi_status_t status;
	u32 phys_tm, phys_tc;
707
	unsigned long flags;
708 709

	spin_lock(&rtc_lock);
710
	spin_lock_irqsave(&efi_runtime_lock, flags);
711

712 713
	phys_tm = virt_to_phys_or_null(tm);
	phys_tc = virt_to_phys_or_null(tc);
714 715 716

	status = efi_thunk(get_time, phys_tm, phys_tc);

717
	spin_unlock_irqrestore(&efi_runtime_lock, flags);
718 719 720 721 722 723 724 725 726
	spin_unlock(&rtc_lock);

	return status;
}

static efi_status_t efi_thunk_set_time(efi_time_t *tm)
{
	efi_status_t status;
	u32 phys_tm;
727
	unsigned long flags;
728 729

	spin_lock(&rtc_lock);
730
	spin_lock_irqsave(&efi_runtime_lock, flags);
731

732
	phys_tm = virt_to_phys_or_null(tm);
733 734 735

	status = efi_thunk(set_time, phys_tm);

736
	spin_unlock_irqrestore(&efi_runtime_lock, flags);
737 738 739 740 741 742 743 744 745 746 747
	spin_unlock(&rtc_lock);

	return status;
}

static efi_status_t
efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
			  efi_time_t *tm)
{
	efi_status_t status;
	u32 phys_enabled, phys_pending, phys_tm;
748
	unsigned long flags;
749 750

	spin_lock(&rtc_lock);
751
	spin_lock_irqsave(&efi_runtime_lock, flags);
752

753 754 755
	phys_enabled = virt_to_phys_or_null(enabled);
	phys_pending = virt_to_phys_or_null(pending);
	phys_tm = virt_to_phys_or_null(tm);
756 757 758 759

	status = efi_thunk(get_wakeup_time, phys_enabled,
			     phys_pending, phys_tm);

760
	spin_unlock_irqrestore(&efi_runtime_lock, flags);
761 762 763 764 765 766 767 768 769 770
	spin_unlock(&rtc_lock);

	return status;
}

static efi_status_t
efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
{
	efi_status_t status;
	u32 phys_tm;
771
	unsigned long flags;
772 773

	spin_lock(&rtc_lock);
774
	spin_lock_irqsave(&efi_runtime_lock, flags);
775

776
	phys_tm = virt_to_phys_or_null(tm);
777 778 779

	status = efi_thunk(set_wakeup_time, enabled, phys_tm);

780
	spin_unlock_irqrestore(&efi_runtime_lock, flags);
781 782 783 784 785
	spin_unlock(&rtc_lock);

	return status;
}

786 787 788 789
static unsigned long efi_name_size(efi_char16_t *name)
{
	return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
}
790 791 792 793 794 795 796 797

static efi_status_t
efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
		       u32 *attr, unsigned long *data_size, void *data)
{
	efi_status_t status;
	u32 phys_name, phys_vendor, phys_attr;
	u32 phys_data_size, phys_data;
798 799 800
	unsigned long flags;

	spin_lock_irqsave(&efi_runtime_lock, flags);
801

802 803 804 805 806
	phys_data_size = virt_to_phys_or_null(data_size);
	phys_vendor = virt_to_phys_or_null(vendor);
	phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
	phys_attr = virt_to_phys_or_null(attr);
	phys_data = virt_to_phys_or_null_size(data, *data_size);
807 808 809 810

	status = efi_thunk(get_variable, phys_name, phys_vendor,
			   phys_attr, phys_data_size, phys_data);

811 812
	spin_unlock_irqrestore(&efi_runtime_lock, flags);

813 814 815 816 817 818 819 820 821
	return status;
}

static efi_status_t
efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
		       u32 attr, unsigned long data_size, void *data)
{
	u32 phys_name, phys_vendor, phys_data;
	efi_status_t status;
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	unsigned long flags;

	spin_lock_irqsave(&efi_runtime_lock, flags);

	phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
	phys_vendor = virt_to_phys_or_null(vendor);
	phys_data = virt_to_phys_or_null_size(data, data_size);

	/* If data_size is > sizeof(u32) we've got problems */
	status = efi_thunk(set_variable, phys_name, phys_vendor,
			   attr, data_size, phys_data);

	spin_unlock_irqrestore(&efi_runtime_lock, flags);

	return status;
}

static efi_status_t
efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
				   u32 attr, unsigned long data_size,
				   void *data)
{
	u32 phys_name, phys_vendor, phys_data;
	efi_status_t status;
	unsigned long flags;

	if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
		return EFI_NOT_READY;
850

851 852 853
	phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
	phys_vendor = virt_to_phys_or_null(vendor);
	phys_data = virt_to_phys_or_null_size(data, data_size);
854 855 856 857 858

	/* If data_size is > sizeof(u32) we've got problems */
	status = efi_thunk(set_variable, phys_name, phys_vendor,
			   attr, data_size, phys_data);

859 860
	spin_unlock_irqrestore(&efi_runtime_lock, flags);

861 862 863 864 865 866 867 868 869 870
	return status;
}

static efi_status_t
efi_thunk_get_next_variable(unsigned long *name_size,
			    efi_char16_t *name,
			    efi_guid_t *vendor)
{
	efi_status_t status;
	u32 phys_name_size, phys_name, phys_vendor;
871 872 873
	unsigned long flags;

	spin_lock_irqsave(&efi_runtime_lock, flags);
874

875 876 877
	phys_name_size = virt_to_phys_or_null(name_size);
	phys_vendor = virt_to_phys_or_null(vendor);
	phys_name = virt_to_phys_or_null_size(name, *name_size);
878 879 880 881

	status = efi_thunk(get_next_variable, phys_name_size,
			   phys_name, phys_vendor);

882 883
	spin_unlock_irqrestore(&efi_runtime_lock, flags);

884 885 886 887 888 889 890 891
	return status;
}

static efi_status_t
efi_thunk_get_next_high_mono_count(u32 *count)
{
	efi_status_t status;
	u32 phys_count;
892 893 894
	unsigned long flags;

	spin_lock_irqsave(&efi_runtime_lock, flags);
895

896
	phys_count = virt_to_phys_or_null(count);
897 898
	status = efi_thunk(get_next_high_mono_count, phys_count);

899 900
	spin_unlock_irqrestore(&efi_runtime_lock, flags);

901 902 903 904 905 906 907 908
	return status;
}

static void
efi_thunk_reset_system(int reset_type, efi_status_t status,
		       unsigned long data_size, efi_char16_t *data)
{
	u32 phys_data;
909 910 911
	unsigned long flags;

	spin_lock_irqsave(&efi_runtime_lock, flags);
912

913
	phys_data = virt_to_phys_or_null_size(data, data_size);
914 915

	efi_thunk(reset_system, reset_type, status, data_size, phys_data);
916 917

	spin_unlock_irqrestore(&efi_runtime_lock, flags);
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
}

static efi_status_t
efi_thunk_update_capsule(efi_capsule_header_t **capsules,
			 unsigned long count, unsigned long sg_list)
{
	/*
	 * To properly support this function we would need to repackage
	 * 'capsules' because the firmware doesn't understand 64-bit
	 * pointers.
	 */
	return EFI_UNSUPPORTED;
}

static efi_status_t
efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
			      u64 *remaining_space,
			      u64 *max_variable_size)
{
	efi_status_t status;
	u32 phys_storage, phys_remaining, phys_max;
939
	unsigned long flags;
940 941 942 943

	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

944 945
	spin_lock_irqsave(&efi_runtime_lock, flags);

946 947 948
	phys_storage = virt_to_phys_or_null(storage_space);
	phys_remaining = virt_to_phys_or_null(remaining_space);
	phys_max = virt_to_phys_or_null(max_variable_size);
949

950
	status = efi_thunk(query_variable_info, attr, phys_storage,
951 952
			   phys_remaining, phys_max);

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
	spin_unlock_irqrestore(&efi_runtime_lock, flags);

	return status;
}

static efi_status_t
efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
					  u64 *remaining_space,
					  u64 *max_variable_size)
{
	efi_status_t status;
	u32 phys_storage, phys_remaining, phys_max;
	unsigned long flags;

	if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
		return EFI_UNSUPPORTED;

	if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
		return EFI_NOT_READY;

	phys_storage = virt_to_phys_or_null(storage_space);
	phys_remaining = virt_to_phys_or_null(remaining_space);
	phys_max = virt_to_phys_or_null(max_variable_size);

	status = efi_thunk(query_variable_info, attr, phys_storage,
			   phys_remaining, phys_max);

	spin_unlock_irqrestore(&efi_runtime_lock, flags);

982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	return status;
}

static efi_status_t
efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
			     unsigned long count, u64 *max_size,
			     int *reset_type)
{
	/*
	 * To properly support this function we would need to repackage
	 * 'capsules' because the firmware doesn't understand 64-bit
	 * pointers.
	 */
	return EFI_UNSUPPORTED;
}

void efi_thunk_runtime_setup(void)
{
	efi.get_time = efi_thunk_get_time;
	efi.set_time = efi_thunk_set_time;
	efi.get_wakeup_time = efi_thunk_get_wakeup_time;
	efi.set_wakeup_time = efi_thunk_set_wakeup_time;
	efi.get_variable = efi_thunk_get_variable;
	efi.get_next_variable = efi_thunk_get_next_variable;
	efi.set_variable = efi_thunk_set_variable;
1007
	efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
1008 1009 1010
	efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
	efi.reset_system = efi_thunk_reset_system;
	efi.query_variable_info = efi_thunk_query_variable_info;
1011
	efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
1012 1013 1014 1015
	efi.update_capsule = efi_thunk_update_capsule;
	efi.query_capsule_caps = efi_thunk_query_capsule_caps;
}
#endif /* CONFIG_EFI_MIXED */