efi_64.c 26 KB

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