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