efi_64.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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/init.h>
  27. #include <linux/mc146818rtc.h>
  28. #include <linux/efi.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 <asm/setup.h>
  36. #include <asm/page.h>
  37. #include <asm/e820/api.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/proto.h>
  41. #include <asm/efi.h>
  42. #include <asm/cacheflush.h>
  43. #include <asm/fixmap.h>
  44. #include <asm/realmode.h>
  45. #include <asm/time.h>
  46. #include <asm/pgalloc.h>
  47. /*
  48. * We allocate runtime services regions top-down, starting from -4G, i.e.
  49. * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
  50. */
  51. static u64 efi_va = EFI_VA_START;
  52. struct efi_scratch efi_scratch;
  53. static void __init early_code_mapping_set_exec(int executable)
  54. {
  55. efi_memory_desc_t *md;
  56. if (!(__supported_pte_mask & _PAGE_NX))
  57. return;
  58. /* Make EFI service code area executable */
  59. for_each_efi_memory_desc(md) {
  60. if (md->type == EFI_RUNTIME_SERVICES_CODE ||
  61. md->type == EFI_BOOT_SERVICES_CODE)
  62. efi_set_executable(md, executable);
  63. }
  64. }
  65. pgd_t * __init efi_call_phys_prolog(void)
  66. {
  67. unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
  68. pgd_t *save_pgd, *pgd_k, *pgd_efi;
  69. p4d_t *p4d, *p4d_k, *p4d_efi;
  70. pud_t *pud;
  71. int pgd;
  72. int n_pgds, i, j;
  73. if (!efi_enabled(EFI_OLD_MEMMAP)) {
  74. save_pgd = (pgd_t *)__read_cr3();
  75. write_cr3((unsigned long)efi_scratch.efi_pgt);
  76. goto out;
  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. }
  118. out:
  119. __flush_tlb_all();
  120. return save_pgd;
  121. }
  122. void __init efi_call_phys_epilog(pgd_t *save_pgd)
  123. {
  124. /*
  125. * After the lock is released, the original page table is restored.
  126. */
  127. int pgd_idx, i;
  128. int nr_pgds;
  129. pgd_t *pgd;
  130. p4d_t *p4d;
  131. pud_t *pud;
  132. if (!efi_enabled(EFI_OLD_MEMMAP)) {
  133. write_cr3((unsigned long)save_pgd);
  134. __flush_tlb_all();
  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_val(*pgd) & _PAGE_PRESENT))
  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_val(*p4d) & _PAGE_PRESENT))
  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. static pgd_t *efi_pgd;
  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. int __init efi_alloc_page_tables(void)
  166. {
  167. pgd_t *pgd;
  168. p4d_t *p4d;
  169. pud_t *pud;
  170. gfp_t gfp_mask;
  171. if (efi_enabled(EFI_OLD_MEMMAP))
  172. return 0;
  173. gfp_mask = GFP_KERNEL | __GFP_ZERO;
  174. efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
  175. if (!efi_pgd)
  176. return -ENOMEM;
  177. pgd = efi_pgd + pgd_index(EFI_VA_END);
  178. p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
  179. if (!p4d) {
  180. free_page((unsigned long)efi_pgd);
  181. return -ENOMEM;
  182. }
  183. pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
  184. if (!pud) {
  185. if (CONFIG_PGTABLE_LEVELS > 4)
  186. free_page((unsigned long) pgd_page_vaddr(*pgd));
  187. free_page((unsigned long)efi_pgd);
  188. return -ENOMEM;
  189. }
  190. return 0;
  191. }
  192. /*
  193. * Add low kernel mappings for passing arguments to EFI functions.
  194. */
  195. void efi_sync_low_kernel_mappings(void)
  196. {
  197. unsigned num_entries;
  198. pgd_t *pgd_k, *pgd_efi;
  199. p4d_t *p4d_k, *p4d_efi;
  200. pud_t *pud_k, *pud_efi;
  201. if (efi_enabled(EFI_OLD_MEMMAP))
  202. return;
  203. /*
  204. * We can share all PGD entries apart from the one entry that
  205. * covers the EFI runtime mapping space.
  206. *
  207. * Make sure the EFI runtime region mappings are guaranteed to
  208. * only span a single PGD entry and that the entry also maps
  209. * other important kernel regions.
  210. */
  211. BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
  212. BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
  213. (EFI_VA_END & PGDIR_MASK));
  214. pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
  215. pgd_k = pgd_offset_k(PAGE_OFFSET);
  216. num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
  217. memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
  218. /*
  219. * As with PGDs, we share all P4D entries apart from the one entry
  220. * that covers the EFI runtime mapping space.
  221. */
  222. BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
  223. BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
  224. pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
  225. pgd_k = pgd_offset_k(EFI_VA_END);
  226. p4d_efi = p4d_offset(pgd_efi, 0);
  227. p4d_k = p4d_offset(pgd_k, 0);
  228. num_entries = p4d_index(EFI_VA_END);
  229. memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
  230. /*
  231. * We share all the PUD entries apart from those that map the
  232. * EFI regions. Copy around them.
  233. */
  234. BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
  235. BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
  236. p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
  237. p4d_k = p4d_offset(pgd_k, EFI_VA_END);
  238. pud_efi = pud_offset(p4d_efi, 0);
  239. pud_k = pud_offset(p4d_k, 0);
  240. num_entries = pud_index(EFI_VA_END);
  241. memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
  242. pud_efi = pud_offset(p4d_efi, EFI_VA_START);
  243. pud_k = pud_offset(p4d_k, EFI_VA_START);
  244. num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
  245. memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
  246. }
  247. /*
  248. * Wrapper for slow_virt_to_phys() that handles NULL addresses.
  249. */
  250. static inline phys_addr_t
  251. virt_to_phys_or_null_size(void *va, unsigned long size)
  252. {
  253. bool bad_size;
  254. if (!va)
  255. return 0;
  256. if (virt_addr_valid(va))
  257. return virt_to_phys(va);
  258. /*
  259. * A fully aligned variable on the stack is guaranteed not to
  260. * cross a page bounary. Try to catch strings on the stack by
  261. * checking that 'size' is a power of two.
  262. */
  263. bad_size = size > PAGE_SIZE || !is_power_of_2(size);
  264. WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
  265. return slow_virt_to_phys(va);
  266. }
  267. #define virt_to_phys_or_null(addr) \
  268. virt_to_phys_or_null_size((addr), sizeof(*(addr)))
  269. int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
  270. {
  271. unsigned long pfn, text, pf;
  272. struct page *page;
  273. unsigned npages;
  274. pgd_t *pgd;
  275. if (efi_enabled(EFI_OLD_MEMMAP))
  276. return 0;
  277. /*
  278. * Since the PGD is encrypted, set the encryption mask so that when
  279. * this value is loaded into cr3 the PGD will be decrypted during
  280. * the pagetable walk.
  281. */
  282. efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
  283. pgd = efi_pgd;
  284. /*
  285. * It can happen that the physical address of new_memmap lands in memory
  286. * which is not mapped in the EFI page table. Therefore we need to go
  287. * and ident-map those pages containing the map before calling
  288. * phys_efi_set_virtual_address_map().
  289. */
  290. pfn = pa_memmap >> PAGE_SHIFT;
  291. pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
  292. if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
  293. pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
  294. return 1;
  295. }
  296. efi_scratch.use_pgd = true;
  297. /*
  298. * Certain firmware versions are way too sentimential and still believe
  299. * they are exclusive and unquestionable owners of the first physical page,
  300. * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
  301. * (but then write-access it later during SetVirtualAddressMap()).
  302. *
  303. * Create a 1:1 mapping for this page, to avoid triple faults during early
  304. * boot with such firmware. We are free to hand this page to the BIOS,
  305. * as trim_bios_range() will reserve the first page and isolate it away
  306. * from memory allocators anyway.
  307. */
  308. pf = _PAGE_RW;
  309. if (sev_active())
  310. pf |= _PAGE_ENC;
  311. if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
  312. pr_err("Failed to create 1:1 mapping for the first page!\n");
  313. return 1;
  314. }
  315. /*
  316. * When making calls to the firmware everything needs to be 1:1
  317. * mapped and addressable with 32-bit pointers. Map the kernel
  318. * text and allocate a new stack because we can't rely on the
  319. * stack pointer being < 4GB.
  320. */
  321. if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
  322. return 0;
  323. page = alloc_page(GFP_KERNEL|__GFP_DMA32);
  324. if (!page)
  325. panic("Unable to allocate EFI runtime stack < 4GB\n");
  326. efi_scratch.phys_stack = virt_to_phys(page_address(page));
  327. efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
  328. npages = (_etext - _text) >> PAGE_SHIFT;
  329. text = __pa(_text);
  330. pfn = text >> PAGE_SHIFT;
  331. pf = _PAGE_RW | _PAGE_ENC;
  332. if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
  333. pr_err("Failed to map kernel text 1:1\n");
  334. return 1;
  335. }
  336. return 0;
  337. }
  338. static void __init __map_region(efi_memory_desc_t *md, u64 va)
  339. {
  340. unsigned long flags = _PAGE_RW;
  341. unsigned long pfn;
  342. pgd_t *pgd = efi_pgd;
  343. if (!(md->attribute & EFI_MEMORY_WB))
  344. flags |= _PAGE_PCD;
  345. if (sev_active())
  346. flags |= _PAGE_ENC;
  347. pfn = md->phys_addr >> PAGE_SHIFT;
  348. if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
  349. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  350. md->phys_addr, va);
  351. }
  352. void __init efi_map_region(efi_memory_desc_t *md)
  353. {
  354. unsigned long size = md->num_pages << PAGE_SHIFT;
  355. u64 pa = md->phys_addr;
  356. if (efi_enabled(EFI_OLD_MEMMAP))
  357. return old_map_region(md);
  358. /*
  359. * Make sure the 1:1 mappings are present as a catch-all for b0rked
  360. * firmware which doesn't update all internal pointers after switching
  361. * to virtual mode and would otherwise crap on us.
  362. */
  363. __map_region(md, md->phys_addr);
  364. /*
  365. * Enforce the 1:1 mapping as the default virtual address when
  366. * booting in EFI mixed mode, because even though we may be
  367. * running a 64-bit kernel, the firmware may only be 32-bit.
  368. */
  369. if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
  370. md->virt_addr = md->phys_addr;
  371. return;
  372. }
  373. efi_va -= size;
  374. /* Is PA 2M-aligned? */
  375. if (!(pa & (PMD_SIZE - 1))) {
  376. efi_va &= PMD_MASK;
  377. } else {
  378. u64 pa_offset = pa & (PMD_SIZE - 1);
  379. u64 prev_va = efi_va;
  380. /* get us the same offset within this 2M page */
  381. efi_va = (efi_va & PMD_MASK) + pa_offset;
  382. if (efi_va > prev_va)
  383. efi_va -= PMD_SIZE;
  384. }
  385. if (efi_va < EFI_VA_END) {
  386. pr_warn(FW_WARN "VA address range overflow!\n");
  387. return;
  388. }
  389. /* Do the VA map */
  390. __map_region(md, efi_va);
  391. md->virt_addr = efi_va;
  392. }
  393. /*
  394. * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
  395. * md->virt_addr is the original virtual address which had been mapped in kexec
  396. * 1st kernel.
  397. */
  398. void __init efi_map_region_fixed(efi_memory_desc_t *md)
  399. {
  400. __map_region(md, md->phys_addr);
  401. __map_region(md, md->virt_addr);
  402. }
  403. void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
  404. u32 type, u64 attribute)
  405. {
  406. unsigned long last_map_pfn;
  407. if (type == EFI_MEMORY_MAPPED_IO)
  408. return ioremap(phys_addr, size);
  409. last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
  410. if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
  411. unsigned long top = last_map_pfn << PAGE_SHIFT;
  412. efi_ioremap(top, size - (top - phys_addr), type, attribute);
  413. }
  414. if (!(attribute & EFI_MEMORY_WB))
  415. efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
  416. return (void __iomem *)__va(phys_addr);
  417. }
  418. void __init parse_efi_setup(u64 phys_addr, u32 data_len)
  419. {
  420. efi_setup = phys_addr + sizeof(struct setup_data);
  421. }
  422. static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
  423. {
  424. unsigned long pfn;
  425. pgd_t *pgd = efi_pgd;
  426. int err1, err2;
  427. /* Update the 1:1 mapping */
  428. pfn = md->phys_addr >> PAGE_SHIFT;
  429. err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
  430. if (err1) {
  431. pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
  432. md->phys_addr, md->virt_addr);
  433. }
  434. err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
  435. if (err2) {
  436. pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
  437. md->phys_addr, md->virt_addr);
  438. }
  439. return err1 || err2;
  440. }
  441. static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
  442. {
  443. unsigned long pf = 0;
  444. if (md->attribute & EFI_MEMORY_XP)
  445. pf |= _PAGE_NX;
  446. if (!(md->attribute & EFI_MEMORY_RO))
  447. pf |= _PAGE_RW;
  448. if (sev_active())
  449. pf |= _PAGE_ENC;
  450. return efi_update_mappings(md, pf);
  451. }
  452. void __init efi_runtime_update_mappings(void)
  453. {
  454. efi_memory_desc_t *md;
  455. if (efi_enabled(EFI_OLD_MEMMAP)) {
  456. if (__supported_pte_mask & _PAGE_NX)
  457. runtime_code_page_mkexec();
  458. return;
  459. }
  460. /*
  461. * Use the EFI Memory Attribute Table for mapping permissions if it
  462. * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
  463. */
  464. if (efi_enabled(EFI_MEM_ATTR)) {
  465. efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
  466. return;
  467. }
  468. /*
  469. * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
  470. * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
  471. * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
  472. * published by the firmware. Even if we find a buggy implementation of
  473. * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
  474. * EFI_PROPERTIES_TABLE, because of the same reason.
  475. */
  476. if (!efi_enabled(EFI_NX_PE_DATA))
  477. return;
  478. for_each_efi_memory_desc(md) {
  479. unsigned long pf = 0;
  480. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  481. continue;
  482. if (!(md->attribute & EFI_MEMORY_WB))
  483. pf |= _PAGE_PCD;
  484. if ((md->attribute & EFI_MEMORY_XP) ||
  485. (md->type == EFI_RUNTIME_SERVICES_DATA))
  486. pf |= _PAGE_NX;
  487. if (!(md->attribute & EFI_MEMORY_RO) &&
  488. (md->type != EFI_RUNTIME_SERVICES_CODE))
  489. pf |= _PAGE_RW;
  490. if (sev_active())
  491. pf |= _PAGE_ENC;
  492. efi_update_mappings(md, pf);
  493. }
  494. }
  495. void __init efi_dump_pagetable(void)
  496. {
  497. #ifdef CONFIG_EFI_PGT_DUMP
  498. if (efi_enabled(EFI_OLD_MEMMAP))
  499. ptdump_walk_pgd_level(NULL, swapper_pg_dir);
  500. else
  501. ptdump_walk_pgd_level(NULL, efi_pgd);
  502. #endif
  503. }
  504. #ifdef CONFIG_EFI_MIXED
  505. extern efi_status_t efi64_thunk(u32, ...);
  506. #define runtime_service32(func) \
  507. ({ \
  508. u32 table = (u32)(unsigned long)efi.systab; \
  509. u32 *rt, *___f; \
  510. \
  511. rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
  512. ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
  513. *___f; \
  514. })
  515. /*
  516. * Switch to the EFI page tables early so that we can access the 1:1
  517. * runtime services mappings which are not mapped in any other page
  518. * tables. This function must be called before runtime_service32().
  519. *
  520. * Also, disable interrupts because the IDT points to 64-bit handlers,
  521. * which aren't going to function correctly when we switch to 32-bit.
  522. */
  523. #define efi_thunk(f, ...) \
  524. ({ \
  525. efi_status_t __s; \
  526. unsigned long __flags; \
  527. u32 __func; \
  528. \
  529. local_irq_save(__flags); \
  530. arch_efi_call_virt_setup(); \
  531. \
  532. __func = runtime_service32(f); \
  533. __s = efi64_thunk(__func, __VA_ARGS__); \
  534. \
  535. arch_efi_call_virt_teardown(); \
  536. local_irq_restore(__flags); \
  537. \
  538. __s; \
  539. })
  540. efi_status_t efi_thunk_set_virtual_address_map(
  541. void *phys_set_virtual_address_map,
  542. unsigned long memory_map_size,
  543. unsigned long descriptor_size,
  544. u32 descriptor_version,
  545. efi_memory_desc_t *virtual_map)
  546. {
  547. efi_status_t status;
  548. unsigned long flags;
  549. u32 func;
  550. efi_sync_low_kernel_mappings();
  551. local_irq_save(flags);
  552. efi_scratch.prev_cr3 = __read_cr3();
  553. write_cr3((unsigned long)efi_scratch.efi_pgt);
  554. __flush_tlb_all();
  555. func = (u32)(unsigned long)phys_set_virtual_address_map;
  556. status = efi64_thunk(func, memory_map_size, descriptor_size,
  557. descriptor_version, virtual_map);
  558. write_cr3(efi_scratch.prev_cr3);
  559. __flush_tlb_all();
  560. local_irq_restore(flags);
  561. return status;
  562. }
  563. static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  564. {
  565. efi_status_t status;
  566. u32 phys_tm, phys_tc;
  567. spin_lock(&rtc_lock);
  568. phys_tm = virt_to_phys_or_null(tm);
  569. phys_tc = virt_to_phys_or_null(tc);
  570. status = efi_thunk(get_time, phys_tm, phys_tc);
  571. spin_unlock(&rtc_lock);
  572. return status;
  573. }
  574. static efi_status_t efi_thunk_set_time(efi_time_t *tm)
  575. {
  576. efi_status_t status;
  577. u32 phys_tm;
  578. spin_lock(&rtc_lock);
  579. phys_tm = virt_to_phys_or_null(tm);
  580. status = efi_thunk(set_time, phys_tm);
  581. spin_unlock(&rtc_lock);
  582. return status;
  583. }
  584. static efi_status_t
  585. efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
  586. efi_time_t *tm)
  587. {
  588. efi_status_t status;
  589. u32 phys_enabled, phys_pending, phys_tm;
  590. spin_lock(&rtc_lock);
  591. phys_enabled = virt_to_phys_or_null(enabled);
  592. phys_pending = virt_to_phys_or_null(pending);
  593. phys_tm = virt_to_phys_or_null(tm);
  594. status = efi_thunk(get_wakeup_time, phys_enabled,
  595. phys_pending, phys_tm);
  596. spin_unlock(&rtc_lock);
  597. return status;
  598. }
  599. static efi_status_t
  600. efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  601. {
  602. efi_status_t status;
  603. u32 phys_tm;
  604. spin_lock(&rtc_lock);
  605. phys_tm = virt_to_phys_or_null(tm);
  606. status = efi_thunk(set_wakeup_time, enabled, phys_tm);
  607. spin_unlock(&rtc_lock);
  608. return status;
  609. }
  610. static unsigned long efi_name_size(efi_char16_t *name)
  611. {
  612. return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
  613. }
  614. static efi_status_t
  615. efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  616. u32 *attr, unsigned long *data_size, void *data)
  617. {
  618. efi_status_t status;
  619. u32 phys_name, phys_vendor, phys_attr;
  620. u32 phys_data_size, phys_data;
  621. phys_data_size = virt_to_phys_or_null(data_size);
  622. phys_vendor = virt_to_phys_or_null(vendor);
  623. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  624. phys_attr = virt_to_phys_or_null(attr);
  625. phys_data = virt_to_phys_or_null_size(data, *data_size);
  626. status = efi_thunk(get_variable, phys_name, phys_vendor,
  627. phys_attr, phys_data_size, phys_data);
  628. return status;
  629. }
  630. static efi_status_t
  631. efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  632. u32 attr, unsigned long data_size, void *data)
  633. {
  634. u32 phys_name, phys_vendor, phys_data;
  635. efi_status_t status;
  636. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  637. phys_vendor = virt_to_phys_or_null(vendor);
  638. phys_data = virt_to_phys_or_null_size(data, data_size);
  639. /* If data_size is > sizeof(u32) we've got problems */
  640. status = efi_thunk(set_variable, phys_name, phys_vendor,
  641. attr, data_size, phys_data);
  642. return status;
  643. }
  644. static efi_status_t
  645. efi_thunk_get_next_variable(unsigned long *name_size,
  646. efi_char16_t *name,
  647. efi_guid_t *vendor)
  648. {
  649. efi_status_t status;
  650. u32 phys_name_size, phys_name, phys_vendor;
  651. phys_name_size = virt_to_phys_or_null(name_size);
  652. phys_vendor = virt_to_phys_or_null(vendor);
  653. phys_name = virt_to_phys_or_null_size(name, *name_size);
  654. status = efi_thunk(get_next_variable, phys_name_size,
  655. phys_name, phys_vendor);
  656. return status;
  657. }
  658. static efi_status_t
  659. efi_thunk_get_next_high_mono_count(u32 *count)
  660. {
  661. efi_status_t status;
  662. u32 phys_count;
  663. phys_count = virt_to_phys_or_null(count);
  664. status = efi_thunk(get_next_high_mono_count, phys_count);
  665. return status;
  666. }
  667. static void
  668. efi_thunk_reset_system(int reset_type, efi_status_t status,
  669. unsigned long data_size, efi_char16_t *data)
  670. {
  671. u32 phys_data;
  672. phys_data = virt_to_phys_or_null_size(data, data_size);
  673. efi_thunk(reset_system, reset_type, status, data_size, phys_data);
  674. }
  675. static efi_status_t
  676. efi_thunk_update_capsule(efi_capsule_header_t **capsules,
  677. unsigned long count, unsigned long sg_list)
  678. {
  679. /*
  680. * To properly support this function we would need to repackage
  681. * 'capsules' because the firmware doesn't understand 64-bit
  682. * pointers.
  683. */
  684. return EFI_UNSUPPORTED;
  685. }
  686. static efi_status_t
  687. efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
  688. u64 *remaining_space,
  689. u64 *max_variable_size)
  690. {
  691. efi_status_t status;
  692. u32 phys_storage, phys_remaining, phys_max;
  693. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  694. return EFI_UNSUPPORTED;
  695. phys_storage = virt_to_phys_or_null(storage_space);
  696. phys_remaining = virt_to_phys_or_null(remaining_space);
  697. phys_max = virt_to_phys_or_null(max_variable_size);
  698. status = efi_thunk(query_variable_info, attr, phys_storage,
  699. phys_remaining, phys_max);
  700. return status;
  701. }
  702. static efi_status_t
  703. efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
  704. unsigned long count, u64 *max_size,
  705. int *reset_type)
  706. {
  707. /*
  708. * To properly support this function we would need to repackage
  709. * 'capsules' because the firmware doesn't understand 64-bit
  710. * pointers.
  711. */
  712. return EFI_UNSUPPORTED;
  713. }
  714. void efi_thunk_runtime_setup(void)
  715. {
  716. efi.get_time = efi_thunk_get_time;
  717. efi.set_time = efi_thunk_set_time;
  718. efi.get_wakeup_time = efi_thunk_get_wakeup_time;
  719. efi.set_wakeup_time = efi_thunk_set_wakeup_time;
  720. efi.get_variable = efi_thunk_get_variable;
  721. efi.get_next_variable = efi_thunk_get_next_variable;
  722. efi.set_variable = efi_thunk_set_variable;
  723. efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
  724. efi.reset_system = efi_thunk_reset_system;
  725. efi.query_variable_info = efi_thunk_query_variable_info;
  726. efi.update_capsule = efi_thunk_update_capsule;
  727. efi.query_capsule_caps = efi_thunk_query_capsule_caps;
  728. }
  729. #endif /* CONFIG_EFI_MIXED */