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