efi_64.c 23 KB

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