efi_64.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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/memblock.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. 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 (pgtable_l5_enabled())
  189. free_page((unsigned long) pgd_page_vaddr(*pgd));
  190. free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
  191. return -ENOMEM;
  192. }
  193. efi_mm.pgd = efi_pgd;
  194. mm_init_cpumask(&efi_mm);
  195. init_new_context(NULL, &efi_mm);
  196. return 0;
  197. }
  198. /*
  199. * Add low kernel mappings for passing arguments to EFI functions.
  200. */
  201. void efi_sync_low_kernel_mappings(void)
  202. {
  203. unsigned num_entries;
  204. pgd_t *pgd_k, *pgd_efi;
  205. p4d_t *p4d_k, *p4d_efi;
  206. pud_t *pud_k, *pud_efi;
  207. pgd_t *efi_pgd = efi_mm.pgd;
  208. if (efi_enabled(EFI_OLD_MEMMAP))
  209. return;
  210. /*
  211. * We can share all PGD entries apart from the one entry that
  212. * covers the EFI runtime mapping space.
  213. *
  214. * Make sure the EFI runtime region mappings are guaranteed to
  215. * only span a single PGD entry and that the entry also maps
  216. * other important kernel regions.
  217. */
  218. MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
  219. MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
  220. (EFI_VA_END & PGDIR_MASK));
  221. pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
  222. pgd_k = pgd_offset_k(PAGE_OFFSET);
  223. num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
  224. memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
  225. /*
  226. * As with PGDs, we share all P4D entries apart from the one entry
  227. * that covers the EFI runtime mapping space.
  228. */
  229. BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
  230. BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
  231. pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
  232. pgd_k = pgd_offset_k(EFI_VA_END);
  233. p4d_efi = p4d_offset(pgd_efi, 0);
  234. p4d_k = p4d_offset(pgd_k, 0);
  235. num_entries = p4d_index(EFI_VA_END);
  236. memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
  237. /*
  238. * We share all the PUD entries apart from those that map the
  239. * EFI regions. Copy around them.
  240. */
  241. BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
  242. BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
  243. p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
  244. p4d_k = p4d_offset(pgd_k, EFI_VA_END);
  245. pud_efi = pud_offset(p4d_efi, 0);
  246. pud_k = pud_offset(p4d_k, 0);
  247. num_entries = pud_index(EFI_VA_END);
  248. memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
  249. pud_efi = pud_offset(p4d_efi, EFI_VA_START);
  250. pud_k = pud_offset(p4d_k, EFI_VA_START);
  251. num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
  252. memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
  253. }
  254. /*
  255. * Wrapper for slow_virt_to_phys() that handles NULL addresses.
  256. */
  257. static inline phys_addr_t
  258. virt_to_phys_or_null_size(void *va, unsigned long size)
  259. {
  260. bool bad_size;
  261. if (!va)
  262. return 0;
  263. if (virt_addr_valid(va))
  264. return virt_to_phys(va);
  265. /*
  266. * A fully aligned variable on the stack is guaranteed not to
  267. * cross a page bounary. Try to catch strings on the stack by
  268. * checking that 'size' is a power of two.
  269. */
  270. bad_size = size > PAGE_SIZE || !is_power_of_2(size);
  271. WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
  272. return slow_virt_to_phys(va);
  273. }
  274. #define virt_to_phys_or_null(addr) \
  275. virt_to_phys_or_null_size((addr), sizeof(*(addr)))
  276. int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
  277. {
  278. unsigned long pfn, text, pf;
  279. struct page *page;
  280. unsigned npages;
  281. pgd_t *pgd = efi_mm.pgd;
  282. if (efi_enabled(EFI_OLD_MEMMAP))
  283. return 0;
  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. /*
  297. * Certain firmware versions are way too sentimential and still believe
  298. * they are exclusive and unquestionable owners of the first physical page,
  299. * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
  300. * (but then write-access it later during SetVirtualAddressMap()).
  301. *
  302. * Create a 1:1 mapping for this page, to avoid triple faults during early
  303. * boot with such firmware. We are free to hand this page to the BIOS,
  304. * as trim_bios_range() will reserve the first page and isolate it away
  305. * from memory allocators anyway.
  306. */
  307. pf = _PAGE_RW;
  308. if (sev_active())
  309. pf |= _PAGE_ENC;
  310. if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
  311. pr_err("Failed to create 1:1 mapping for the first page!\n");
  312. return 1;
  313. }
  314. /*
  315. * When making calls to the firmware everything needs to be 1:1
  316. * mapped and addressable with 32-bit pointers. Map the kernel
  317. * text and allocate a new stack because we can't rely on the
  318. * stack pointer being < 4GB.
  319. */
  320. if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
  321. return 0;
  322. page = alloc_page(GFP_KERNEL|__GFP_DMA32);
  323. if (!page)
  324. panic("Unable to allocate EFI runtime stack < 4GB\n");
  325. efi_scratch.phys_stack = virt_to_phys(page_address(page));
  326. efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
  327. npages = (_etext - _text) >> PAGE_SHIFT;
  328. text = __pa(_text);
  329. pfn = text >> PAGE_SHIFT;
  330. pf = _PAGE_RW | _PAGE_ENC;
  331. if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
  332. pr_err("Failed to map kernel text 1:1\n");
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. static void __init __map_region(efi_memory_desc_t *md, u64 va)
  338. {
  339. unsigned long flags = _PAGE_RW;
  340. unsigned long pfn;
  341. pgd_t *pgd = efi_mm.pgd;
  342. if (!(md->attribute & EFI_MEMORY_WB))
  343. flags |= _PAGE_PCD;
  344. if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
  345. flags |= _PAGE_ENC;
  346. pfn = md->phys_addr >> PAGE_SHIFT;
  347. if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
  348. pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
  349. md->phys_addr, va);
  350. }
  351. void __init efi_map_region(efi_memory_desc_t *md)
  352. {
  353. unsigned long size = md->num_pages << PAGE_SHIFT;
  354. u64 pa = md->phys_addr;
  355. if (efi_enabled(EFI_OLD_MEMMAP))
  356. return old_map_region(md);
  357. /*
  358. * Make sure the 1:1 mappings are present as a catch-all for b0rked
  359. * firmware which doesn't update all internal pointers after switching
  360. * to virtual mode and would otherwise crap on us.
  361. */
  362. __map_region(md, md->phys_addr);
  363. /*
  364. * Enforce the 1:1 mapping as the default virtual address when
  365. * booting in EFI mixed mode, because even though we may be
  366. * running a 64-bit kernel, the firmware may only be 32-bit.
  367. */
  368. if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
  369. md->virt_addr = md->phys_addr;
  370. return;
  371. }
  372. efi_va -= size;
  373. /* Is PA 2M-aligned? */
  374. if (!(pa & (PMD_SIZE - 1))) {
  375. efi_va &= PMD_MASK;
  376. } else {
  377. u64 pa_offset = pa & (PMD_SIZE - 1);
  378. u64 prev_va = efi_va;
  379. /* get us the same offset within this 2M page */
  380. efi_va = (efi_va & PMD_MASK) + pa_offset;
  381. if (efi_va > prev_va)
  382. efi_va -= PMD_SIZE;
  383. }
  384. if (efi_va < EFI_VA_END) {
  385. pr_warn(FW_WARN "VA address range overflow!\n");
  386. return;
  387. }
  388. /* Do the VA map */
  389. __map_region(md, efi_va);
  390. md->virt_addr = efi_va;
  391. }
  392. /*
  393. * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
  394. * md->virt_addr is the original virtual address which had been mapped in kexec
  395. * 1st kernel.
  396. */
  397. void __init efi_map_region_fixed(efi_memory_desc_t *md)
  398. {
  399. __map_region(md, md->phys_addr);
  400. __map_region(md, md->virt_addr);
  401. }
  402. void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
  403. u32 type, u64 attribute)
  404. {
  405. unsigned long last_map_pfn;
  406. if (type == EFI_MEMORY_MAPPED_IO)
  407. return ioremap(phys_addr, size);
  408. last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
  409. if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
  410. unsigned long top = last_map_pfn << PAGE_SHIFT;
  411. efi_ioremap(top, size - (top - phys_addr), type, attribute);
  412. }
  413. if (!(attribute & EFI_MEMORY_WB))
  414. efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
  415. return (void __iomem *)__va(phys_addr);
  416. }
  417. void __init parse_efi_setup(u64 phys_addr, u32 data_len)
  418. {
  419. efi_setup = phys_addr + sizeof(struct setup_data);
  420. }
  421. static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
  422. {
  423. unsigned long pfn;
  424. pgd_t *pgd = efi_mm.pgd;
  425. int err1, err2;
  426. /* Update the 1:1 mapping */
  427. pfn = md->phys_addr >> PAGE_SHIFT;
  428. err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
  429. if (err1) {
  430. pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
  431. md->phys_addr, md->virt_addr);
  432. }
  433. err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
  434. if (err2) {
  435. pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
  436. md->phys_addr, md->virt_addr);
  437. }
  438. return err1 || err2;
  439. }
  440. static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
  441. {
  442. unsigned long pf = 0;
  443. if (md->attribute & EFI_MEMORY_XP)
  444. pf |= _PAGE_NX;
  445. if (!(md->attribute & EFI_MEMORY_RO))
  446. pf |= _PAGE_RW;
  447. if (sev_active())
  448. pf |= _PAGE_ENC;
  449. return efi_update_mappings(md, pf);
  450. }
  451. void __init efi_runtime_update_mappings(void)
  452. {
  453. efi_memory_desc_t *md;
  454. if (efi_enabled(EFI_OLD_MEMMAP)) {
  455. if (__supported_pte_mask & _PAGE_NX)
  456. runtime_code_page_mkexec();
  457. return;
  458. }
  459. /*
  460. * Use the EFI Memory Attribute Table for mapping permissions if it
  461. * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
  462. */
  463. if (efi_enabled(EFI_MEM_ATTR)) {
  464. efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
  465. return;
  466. }
  467. /*
  468. * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
  469. * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
  470. * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
  471. * published by the firmware. Even if we find a buggy implementation of
  472. * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
  473. * EFI_PROPERTIES_TABLE, because of the same reason.
  474. */
  475. if (!efi_enabled(EFI_NX_PE_DATA))
  476. return;
  477. for_each_efi_memory_desc(md) {
  478. unsigned long pf = 0;
  479. if (!(md->attribute & EFI_MEMORY_RUNTIME))
  480. continue;
  481. if (!(md->attribute & EFI_MEMORY_WB))
  482. pf |= _PAGE_PCD;
  483. if ((md->attribute & EFI_MEMORY_XP) ||
  484. (md->type == EFI_RUNTIME_SERVICES_DATA))
  485. pf |= _PAGE_NX;
  486. if (!(md->attribute & EFI_MEMORY_RO) &&
  487. (md->type != EFI_RUNTIME_SERVICES_CODE))
  488. pf |= _PAGE_RW;
  489. if (sev_active())
  490. pf |= _PAGE_ENC;
  491. efi_update_mappings(md, pf);
  492. }
  493. }
  494. void __init efi_dump_pagetable(void)
  495. {
  496. #ifdef CONFIG_EFI_PGT_DUMP
  497. if (efi_enabled(EFI_OLD_MEMMAP))
  498. ptdump_walk_pgd_level(NULL, swapper_pg_dir);
  499. else
  500. ptdump_walk_pgd_level(NULL, efi_mm.pgd);
  501. #endif
  502. }
  503. /*
  504. * Makes the calling thread switch to/from efi_mm context. Can be used
  505. * in a kernel thread and user context. Preemption needs to remain disabled
  506. * while the EFI-mm is borrowed. mmgrab()/mmdrop() is not used because the mm
  507. * can not change under us.
  508. * It should be ensured that there are no concurent calls to this function.
  509. */
  510. void efi_switch_mm(struct mm_struct *mm)
  511. {
  512. efi_scratch.prev_mm = current->active_mm;
  513. current->active_mm = mm;
  514. switch_mm(efi_scratch.prev_mm, mm, NULL);
  515. }
  516. #ifdef CONFIG_EFI_MIXED
  517. extern efi_status_t efi64_thunk(u32, ...);
  518. static DEFINE_SPINLOCK(efi_runtime_lock);
  519. #define runtime_service32(func) \
  520. ({ \
  521. u32 table = (u32)(unsigned long)efi.systab; \
  522. u32 *rt, *___f; \
  523. \
  524. rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
  525. ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
  526. *___f; \
  527. })
  528. /*
  529. * Switch to the EFI page tables early so that we can access the 1:1
  530. * runtime services mappings which are not mapped in any other page
  531. * tables. This function must be called before runtime_service32().
  532. *
  533. * Also, disable interrupts because the IDT points to 64-bit handlers,
  534. * which aren't going to function correctly when we switch to 32-bit.
  535. */
  536. #define efi_thunk(f, ...) \
  537. ({ \
  538. efi_status_t __s; \
  539. u32 __func; \
  540. \
  541. arch_efi_call_virt_setup(); \
  542. \
  543. __func = runtime_service32(f); \
  544. __s = efi64_thunk(__func, __VA_ARGS__); \
  545. \
  546. arch_efi_call_virt_teardown(); \
  547. \
  548. __s; \
  549. })
  550. efi_status_t efi_thunk_set_virtual_address_map(
  551. void *phys_set_virtual_address_map,
  552. unsigned long memory_map_size,
  553. unsigned long descriptor_size,
  554. u32 descriptor_version,
  555. efi_memory_desc_t *virtual_map)
  556. {
  557. efi_status_t status;
  558. unsigned long flags;
  559. u32 func;
  560. efi_sync_low_kernel_mappings();
  561. local_irq_save(flags);
  562. efi_switch_mm(&efi_mm);
  563. func = (u32)(unsigned long)phys_set_virtual_address_map;
  564. status = efi64_thunk(func, memory_map_size, descriptor_size,
  565. descriptor_version, virtual_map);
  566. efi_switch_mm(efi_scratch.prev_mm);
  567. local_irq_restore(flags);
  568. return status;
  569. }
  570. static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
  571. {
  572. efi_status_t status;
  573. u32 phys_tm, phys_tc;
  574. unsigned long flags;
  575. spin_lock(&rtc_lock);
  576. spin_lock_irqsave(&efi_runtime_lock, flags);
  577. phys_tm = virt_to_phys_or_null(tm);
  578. phys_tc = virt_to_phys_or_null(tc);
  579. status = efi_thunk(get_time, phys_tm, phys_tc);
  580. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  581. spin_unlock(&rtc_lock);
  582. return status;
  583. }
  584. static efi_status_t efi_thunk_set_time(efi_time_t *tm)
  585. {
  586. efi_status_t status;
  587. u32 phys_tm;
  588. unsigned long flags;
  589. spin_lock(&rtc_lock);
  590. spin_lock_irqsave(&efi_runtime_lock, flags);
  591. phys_tm = virt_to_phys_or_null(tm);
  592. status = efi_thunk(set_time, phys_tm);
  593. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  594. spin_unlock(&rtc_lock);
  595. return status;
  596. }
  597. static efi_status_t
  598. efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
  599. efi_time_t *tm)
  600. {
  601. efi_status_t status;
  602. u32 phys_enabled, phys_pending, phys_tm;
  603. unsigned long flags;
  604. spin_lock(&rtc_lock);
  605. spin_lock_irqsave(&efi_runtime_lock, flags);
  606. phys_enabled = virt_to_phys_or_null(enabled);
  607. phys_pending = virt_to_phys_or_null(pending);
  608. phys_tm = virt_to_phys_or_null(tm);
  609. status = efi_thunk(get_wakeup_time, phys_enabled,
  610. phys_pending, phys_tm);
  611. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  612. spin_unlock(&rtc_lock);
  613. return status;
  614. }
  615. static efi_status_t
  616. efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
  617. {
  618. efi_status_t status;
  619. u32 phys_tm;
  620. unsigned long flags;
  621. spin_lock(&rtc_lock);
  622. spin_lock_irqsave(&efi_runtime_lock, flags);
  623. phys_tm = virt_to_phys_or_null(tm);
  624. status = efi_thunk(set_wakeup_time, enabled, phys_tm);
  625. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  626. spin_unlock(&rtc_lock);
  627. return status;
  628. }
  629. static unsigned long efi_name_size(efi_char16_t *name)
  630. {
  631. return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
  632. }
  633. static efi_status_t
  634. efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
  635. u32 *attr, unsigned long *data_size, void *data)
  636. {
  637. efi_status_t status;
  638. u32 phys_name, phys_vendor, phys_attr;
  639. u32 phys_data_size, phys_data;
  640. unsigned long flags;
  641. spin_lock_irqsave(&efi_runtime_lock, flags);
  642. phys_data_size = virt_to_phys_or_null(data_size);
  643. phys_vendor = virt_to_phys_or_null(vendor);
  644. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  645. phys_attr = virt_to_phys_or_null(attr);
  646. phys_data = virt_to_phys_or_null_size(data, *data_size);
  647. status = efi_thunk(get_variable, phys_name, phys_vendor,
  648. phys_attr, phys_data_size, phys_data);
  649. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  650. return status;
  651. }
  652. static efi_status_t
  653. efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
  654. u32 attr, unsigned long data_size, void *data)
  655. {
  656. u32 phys_name, phys_vendor, phys_data;
  657. efi_status_t status;
  658. unsigned long flags;
  659. spin_lock_irqsave(&efi_runtime_lock, flags);
  660. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  661. phys_vendor = virt_to_phys_or_null(vendor);
  662. phys_data = virt_to_phys_or_null_size(data, data_size);
  663. /* If data_size is > sizeof(u32) we've got problems */
  664. status = efi_thunk(set_variable, phys_name, phys_vendor,
  665. attr, data_size, phys_data);
  666. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  667. return status;
  668. }
  669. static efi_status_t
  670. efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
  671. u32 attr, unsigned long data_size,
  672. void *data)
  673. {
  674. u32 phys_name, phys_vendor, phys_data;
  675. efi_status_t status;
  676. unsigned long flags;
  677. if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
  678. return EFI_NOT_READY;
  679. phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
  680. phys_vendor = virt_to_phys_or_null(vendor);
  681. phys_data = virt_to_phys_or_null_size(data, data_size);
  682. /* If data_size is > sizeof(u32) we've got problems */
  683. status = efi_thunk(set_variable, phys_name, phys_vendor,
  684. attr, data_size, phys_data);
  685. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  686. return status;
  687. }
  688. static efi_status_t
  689. efi_thunk_get_next_variable(unsigned long *name_size,
  690. efi_char16_t *name,
  691. efi_guid_t *vendor)
  692. {
  693. efi_status_t status;
  694. u32 phys_name_size, phys_name, phys_vendor;
  695. unsigned long flags;
  696. spin_lock_irqsave(&efi_runtime_lock, flags);
  697. phys_name_size = virt_to_phys_or_null(name_size);
  698. phys_vendor = virt_to_phys_or_null(vendor);
  699. phys_name = virt_to_phys_or_null_size(name, *name_size);
  700. status = efi_thunk(get_next_variable, phys_name_size,
  701. phys_name, phys_vendor);
  702. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  703. return status;
  704. }
  705. static efi_status_t
  706. efi_thunk_get_next_high_mono_count(u32 *count)
  707. {
  708. efi_status_t status;
  709. u32 phys_count;
  710. unsigned long flags;
  711. spin_lock_irqsave(&efi_runtime_lock, flags);
  712. phys_count = virt_to_phys_or_null(count);
  713. status = efi_thunk(get_next_high_mono_count, phys_count);
  714. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  715. return status;
  716. }
  717. static void
  718. efi_thunk_reset_system(int reset_type, efi_status_t status,
  719. unsigned long data_size, efi_char16_t *data)
  720. {
  721. u32 phys_data;
  722. unsigned long flags;
  723. spin_lock_irqsave(&efi_runtime_lock, flags);
  724. phys_data = virt_to_phys_or_null_size(data, data_size);
  725. efi_thunk(reset_system, reset_type, status, data_size, phys_data);
  726. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  727. }
  728. static efi_status_t
  729. efi_thunk_update_capsule(efi_capsule_header_t **capsules,
  730. unsigned long count, unsigned long sg_list)
  731. {
  732. /*
  733. * To properly support this function we would need to repackage
  734. * 'capsules' because the firmware doesn't understand 64-bit
  735. * pointers.
  736. */
  737. return EFI_UNSUPPORTED;
  738. }
  739. static efi_status_t
  740. efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
  741. u64 *remaining_space,
  742. u64 *max_variable_size)
  743. {
  744. efi_status_t status;
  745. u32 phys_storage, phys_remaining, phys_max;
  746. unsigned long flags;
  747. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  748. return EFI_UNSUPPORTED;
  749. spin_lock_irqsave(&efi_runtime_lock, flags);
  750. phys_storage = virt_to_phys_or_null(storage_space);
  751. phys_remaining = virt_to_phys_or_null(remaining_space);
  752. phys_max = virt_to_phys_or_null(max_variable_size);
  753. status = efi_thunk(query_variable_info, attr, phys_storage,
  754. phys_remaining, phys_max);
  755. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  756. return status;
  757. }
  758. static efi_status_t
  759. efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
  760. u64 *remaining_space,
  761. u64 *max_variable_size)
  762. {
  763. efi_status_t status;
  764. u32 phys_storage, phys_remaining, phys_max;
  765. unsigned long flags;
  766. if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
  767. return EFI_UNSUPPORTED;
  768. if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
  769. return EFI_NOT_READY;
  770. phys_storage = virt_to_phys_or_null(storage_space);
  771. phys_remaining = virt_to_phys_or_null(remaining_space);
  772. phys_max = virt_to_phys_or_null(max_variable_size);
  773. status = efi_thunk(query_variable_info, attr, phys_storage,
  774. phys_remaining, phys_max);
  775. spin_unlock_irqrestore(&efi_runtime_lock, flags);
  776. return status;
  777. }
  778. static efi_status_t
  779. efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
  780. unsigned long count, u64 *max_size,
  781. int *reset_type)
  782. {
  783. /*
  784. * To properly support this function we would need to repackage
  785. * 'capsules' because the firmware doesn't understand 64-bit
  786. * pointers.
  787. */
  788. return EFI_UNSUPPORTED;
  789. }
  790. void efi_thunk_runtime_setup(void)
  791. {
  792. efi.get_time = efi_thunk_get_time;
  793. efi.set_time = efi_thunk_set_time;
  794. efi.get_wakeup_time = efi_thunk_get_wakeup_time;
  795. efi.set_wakeup_time = efi_thunk_set_wakeup_time;
  796. efi.get_variable = efi_thunk_get_variable;
  797. efi.get_next_variable = efi_thunk_get_next_variable;
  798. efi.set_variable = efi_thunk_set_variable;
  799. efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
  800. efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
  801. efi.reset_system = efi_thunk_reset_system;
  802. efi.query_variable_info = efi_thunk_query_variable_info;
  803. efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
  804. efi.update_capsule = efi_thunk_update_capsule;
  805. efi.query_capsule_caps = efi_thunk_query_capsule_caps;
  806. }
  807. #endif /* CONFIG_EFI_MIXED */