efi_64.c 22 KB

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