blob: 12e83888e5b96714a4ad7bd8b9cc84844dd91ded [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
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 Fleming26d7f652015-10-25 10:26:35 +000018#define pr_fmt(fmt) "efi: " fmt
19
Huang, Ying5b836832008-01-30 13:31:19 +010020#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 Gortmakercc3ae7b2016-07-13 20:18:58 -040027#include <linux/init.h>
Arnd Bergmann5ab788d2016-05-30 20:57:50 +020028#include <linux/mc146818rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010029#include <linux/efi.h>
30#include <linux/uaccess.h>
31#include <linux/io.h>
32#include <linux/reboot.h>
David Howells0d01ff22013-04-11 23:51:01 +010033#include <linux/slab.h>
Matt Flemingf6697df2016-11-12 21:04:24 +000034#include <linux/ucs2_string.h>
Huang, Ying5b836832008-01-30 13:31:19 +010035
36#include <asm/setup.h>
37#include <asm/page.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010038#include <asm/e820/api.h>
Huang, Ying5b836832008-01-30 13:31:19 +010039#include <asm/pgtable.h>
40#include <asm/tlbflush.h>
Huang, Ying5b836832008-01-30 13:31:19 +010041#include <asm/proto.h>
42#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080043#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090044#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010045#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000046#include <asm/time.h>
Matt Fleming67a91082015-11-27 21:09:34 +000047#include <asm/pgalloc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010048
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010049/*
Baoquan Heb1d17762017-04-04 17:02:43 +010050 * We allocate runtime services regions top-down, starting from -4G, i.e.
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010051 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
52 */
Mathias Krause8266e312014-09-21 17:26:54 +020053static u64 efi_va = EFI_VA_START;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010054
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000055struct efi_scratch efi_scratch;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010056
Matthew Garrett9cd2b072011-05-05 15:19:43 -040057static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010058{
59 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +010060
Huang, Yinga2172e22008-01-30 13:33:55 +010061 if (!(__supported_pte_mask & _PAGE_NX))
62 return;
63
Matthew Garrett916f6762011-05-25 09:53:13 -040064 /* Make EFI service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +010065 for_each_efi_memory_desc(md) {
Matthew Garrett916f6762011-05-25 09:53:13 -040066 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
67 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040068 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010069 }
70}
71
Ingo Molnar744937b2015-03-03 07:48:50 +010072pgd_t * __init efi_call_phys_prolog(void)
Huang, Ying5b836832008-01-30 13:31:19 +010073{
Baoquan He94133e42017-05-26 12:36:50 +010074 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 Molnar744937b2015-03-03 07:48:50 +010078
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060079 int pgd;
Baoquan He94133e42017-05-26 12:36:50 +010080 int n_pgds, i, j;
Huang, Ying5b836832008-01-30 13:31:19 +010081
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000082 if (!efi_enabled(EFI_OLD_MEMMAP)) {
Andy Lutomirski6c690ee2017-06-12 10:26:14 -070083 save_pgd = (pgd_t *)__read_cr3();
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000084 write_cr3((unsigned long)efi_scratch.efi_pgt);
85 goto out;
86 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010087
Matthew Garrett9cd2b072011-05-05 15:19:43 -040088 early_code_mapping_set_exec(1);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060089
90 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
Markus Elfring20ebc152016-08-25 11:34:03 +020091 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060092
Baoquan He94133e42017-05-26 12:36:50 +010093 /*
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 Zimmerb8f2c212013-01-08 09:02:43 -0600101 for (pgd = 0; pgd < n_pgds; pgd++) {
Baoquan He94133e42017-05-26 12:36:50 +0100102 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 Zimmerb8f2c212013-01-08 09:02:43 -0600136 }
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000137out:
Huang, Ying5b836832008-01-30 13:31:19 +0100138 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +0100139
140 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +0100141}
142
Ingo Molnar744937b2015-03-03 07:48:50 +0100143void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100144{
145 /*
146 * After the lock is released, the original page table is restored.
147 */
Baoquan He94133e42017-05-26 12:36:50 +0100148 int pgd_idx, i;
Ingo Molnar744937b2015-03-03 07:48:50 +0100149 int nr_pgds;
Baoquan He94133e42017-05-26 12:36:50 +0100150 pgd_t *pgd;
151 p4d_t *p4d;
152 pud_t *pud;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100153
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000154 if (!efi_enabled(EFI_OLD_MEMMAP)) {
155 write_cr3((unsigned long)save_pgd);
156 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100157 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000158 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100159
Ingo Molnar744937b2015-03-03 07:48:50 +0100160 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
161
Baoquan He94133e42017-05-26 12:36:50 +0100162 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
163 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
Ingo Molnar744937b2015-03-03 07:48:50 +0100164 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
165
Baoquan He94133e42017-05-26 12:36:50 +0100166 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 Zimmerb8f2c212013-01-08 09:02:43 -0600184 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100185
Huang, Ying5b836832008-01-30 13:31:19 +0100186 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400187 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100188}
Keith Packarde1ad7832011-12-11 16:12:42 -0800189
Matt Fleming67a91082015-11-27 21:09:34 +0000190static 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 */
198int __init efi_alloc_page_tables(void)
199{
200 pgd_t *pgd;
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300201 p4d_t *p4d;
Matt Fleming67a91082015-11-27 21:09:34 +0000202 pud_t *pud;
203 gfp_t gfp_mask;
204
205 if (efi_enabled(EFI_OLD_MEMMAP))
206 return 0;
207
Michal Hockof58f2302016-06-24 14:48:53 -0700208 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
Matt Fleming67a91082015-11-27 21:09:34 +0000209 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. Shutemove9813162017-03-17 21:55:11 +0300214 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
215 if (!p4d) {
Matt Fleming67a91082015-11-27 21:09:34 +0000216 free_page((unsigned long)efi_pgd);
217 return -ENOMEM;
218 }
219
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300220 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 Fleming67a91082015-11-27 21:09:34 +0000227
228 return 0;
229}
230
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100231/*
232 * Add low kernel mappings for passing arguments to EFI functions.
233 */
234void efi_sync_low_kernel_mappings(void)
235{
Matt Fleming67a91082015-11-27 21:09:34 +0000236 unsigned num_entries;
237 pgd_t *pgd_k, *pgd_efi;
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300238 p4d_t *p4d_k, *p4d_efi;
Matt Fleming67a91082015-11-27 21:09:34 +0000239 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100240
241 if (efi_enabled(EFI_OLD_MEMMAP))
242 return;
243
Matt Fleming67a91082015-11-27 21:09:34 +0000244 /*
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 Petkovd2f7cbe2013-10-31 17:25:08 +0100255
Matt Fleming67a91082015-11-27 21:09:34 +0000256 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. Shutemove9813162017-03-17 21:55:11 +0300263 * 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 Fleming67a91082015-11-27 21:09:34 +0000278 * 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. Shutemove9813162017-03-17 21:55:11 +0300284 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
285 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300286 pud_efi = pud_offset(p4d_efi, 0);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300287 pud_k = pud_offset(p4d_k, 0);
Matt Fleming67a91082015-11-27 21:09:34 +0000288
289 num_entries = pud_index(EFI_VA_END);
290 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
291
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300292 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300293 pud_k = pud_offset(p4d_k, EFI_VA_START);
Matt Fleming67a91082015-11-27 21:09:34 +0000294
295 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
296 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100297}
298
Matt Flemingf6697df2016-11-12 21:04:24 +0000299/*
300 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
301 */
302static inline phys_addr_t
303virt_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 Krause4e78eb052014-09-07 19:42:17 +0200328int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100329{
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500330 unsigned long pfn, text, pf;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000331 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000332 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100333 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100334
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100335 if (efi_enabled(EFI_OLD_MEMMAP))
336 return 0;
337
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500338 /*
339 * Since the PGD is encrypted, set the encryption mask so that when
340 * this value is loaded into cr3 the PGD will be decrypted during
341 * the pagetable walk.
342 */
343 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
Matt Fleming67a91082015-11-27 21:09:34 +0000344 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100345
346 /*
347 * It can happen that the physical address of new_memmap lands in memory
348 * which is not mapped in the EFI page table. Therefore we need to go
349 * and ident-map those pages containing the map before calling
350 * phys_efi_set_virtual_address_map().
351 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000352 pfn = pa_memmap >> PAGE_SHIFT;
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500353 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
354 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100355 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
356 return 1;
357 }
358
359 efi_scratch.use_pgd = true;
360
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000361 /*
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000362 * Certain firmware versions are way too sentimential and still believe
363 * they are exclusive and unquestionable owners of the first physical page,
364 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
365 * (but then write-access it later during SetVirtualAddressMap()).
366 *
367 * Create a 1:1 mapping for this page, to avoid triple faults during early
368 * boot with such firmware. We are free to hand this page to the BIOS,
369 * as trim_bios_range() will reserve the first page and isolate it away
370 * from memory allocators anyway.
371 */
372 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
373 pr_err("Failed to create 1:1 mapping for the first page!\n");
374 return 1;
375 }
376
377 /*
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000378 * When making calls to the firmware everything needs to be 1:1
379 * mapped and addressable with 32-bit pointers. Map the kernel
380 * text and allocate a new stack because we can't rely on the
381 * stack pointer being < 4GB.
382 */
Matt Fleming12976672016-09-19 13:09:09 +0100383 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000384 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000385
386 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
387 if (!page)
388 panic("Unable to allocate EFI runtime stack < 4GB\n");
389
390 efi_scratch.phys_stack = virt_to_phys(page_address(page));
391 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
392
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000393 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000394 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000395 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000396
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500397 pf = _PAGE_RW | _PAGE_ENC;
398 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000399 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000400 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000401 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100402
403 return 0;
404}
405
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100406static void __init __map_region(efi_memory_desc_t *md, u64 va)
407{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000408 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000409 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000410 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100411
412 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000413 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100414
Matt Flemingedc3b912015-11-27 21:09:31 +0000415 pfn = md->phys_addr >> PAGE_SHIFT;
416 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100417 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
418 md->phys_addr, va);
419}
420
421void __init efi_map_region(efi_memory_desc_t *md)
422{
423 unsigned long size = md->num_pages << PAGE_SHIFT;
424 u64 pa = md->phys_addr;
425
426 if (efi_enabled(EFI_OLD_MEMMAP))
427 return old_map_region(md);
428
429 /*
430 * Make sure the 1:1 mappings are present as a catch-all for b0rked
431 * firmware which doesn't update all internal pointers after switching
432 * to virtual mode and would otherwise crap on us.
433 */
434 __map_region(md, md->phys_addr);
435
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000436 /*
437 * Enforce the 1:1 mapping as the default virtual address when
438 * booting in EFI mixed mode, because even though we may be
439 * running a 64-bit kernel, the firmware may only be 32-bit.
440 */
441 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
442 md->virt_addr = md->phys_addr;
443 return;
444 }
445
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100446 efi_va -= size;
447
448 /* Is PA 2M-aligned? */
449 if (!(pa & (PMD_SIZE - 1))) {
450 efi_va &= PMD_MASK;
451 } else {
452 u64 pa_offset = pa & (PMD_SIZE - 1);
453 u64 prev_va = efi_va;
454
455 /* get us the same offset within this 2M page */
456 efi_va = (efi_va & PMD_MASK) + pa_offset;
457
458 if (efi_va > prev_va)
459 efi_va -= PMD_SIZE;
460 }
461
462 if (efi_va < EFI_VA_END) {
463 pr_warn(FW_WARN "VA address range overflow!\n");
464 return;
465 }
466
467 /* Do the VA map */
468 __map_region(md, efi_va);
469 md->virt_addr = efi_va;
470}
471
Dave Young3b266492013-12-20 18:02:14 +0800472/*
473 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
474 * md->virt_addr is the original virtual address which had been mapped in kexec
475 * 1st kernel.
476 */
477void __init efi_map_region_fixed(efi_memory_desc_t *md)
478{
Alex Thorlton0513fe12016-08-05 18:59:35 -0500479 __map_region(md, md->phys_addr);
Dave Young3b266492013-12-20 18:02:14 +0800480 __map_region(md, md->virt_addr);
481}
482
Keith Packarde1ad7832011-12-11 16:12:42 -0800483void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100484 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800485{
486 unsigned long last_map_pfn;
487
488 if (type == EFI_MEMORY_MAPPED_IO)
489 return ioremap(phys_addr, size);
490
491 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
492 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
493 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100494 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800495 }
496
Matt Fleming3e8fa262012-10-19 13:25:46 +0100497 if (!(attribute & EFI_MEMORY_WB))
498 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
499
Keith Packarde1ad7832011-12-11 16:12:42 -0800500 return (void __iomem *)__va(phys_addr);
501}
Dave Young1fec0532013-12-20 18:02:19 +0800502
503void __init parse_efi_setup(u64 phys_addr, u32 data_len)
504{
505 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800506}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100507
Sai Praneeth18141e82017-01-31 13:21:37 +0000508static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100509{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000510 unsigned long pfn;
511 pgd_t *pgd = efi_pgd;
Sai Praneeth18141e82017-01-31 13:21:37 +0000512 int err1, err2;
513
514 /* Update the 1:1 mapping */
515 pfn = md->phys_addr >> PAGE_SHIFT;
516 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
517 if (err1) {
518 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
519 md->phys_addr, md->virt_addr);
520 }
521
522 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
523 if (err2) {
524 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
525 md->phys_addr, md->virt_addr);
526 }
527
528 return err1 || err2;
529}
530
531static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
532{
533 unsigned long pf = 0;
534
535 if (md->attribute & EFI_MEMORY_XP)
536 pf |= _PAGE_NX;
537
538 if (!(md->attribute & EFI_MEMORY_RO))
539 pf |= _PAGE_RW;
540
541 return efi_update_mappings(md, pf);
542}
543
544void __init efi_runtime_update_mappings(void)
545{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000546 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000547
548 if (efi_enabled(EFI_OLD_MEMMAP)) {
549 if (__supported_pte_mask & _PAGE_NX)
550 runtime_code_page_mkexec();
551 return;
552 }
553
Sai Praneeth18141e82017-01-31 13:21:37 +0000554 /*
555 * Use the EFI Memory Attribute Table for mapping permissions if it
556 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
557 */
558 if (efi_enabled(EFI_MEM_ATTR)) {
559 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
560 return;
561 }
562
563 /*
564 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
565 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
566 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
567 * published by the firmware. Even if we find a buggy implementation of
568 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
569 * EFI_PROPERTIES_TABLE, because of the same reason.
570 */
571
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000572 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100573 return;
574
Matt Fleming78ce2482016-04-25 21:06:38 +0100575 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000576 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000577
578 if (!(md->attribute & EFI_MEMORY_RUNTIME))
579 continue;
580
581 if (!(md->attribute & EFI_MEMORY_WB))
582 pf |= _PAGE_PCD;
583
584 if ((md->attribute & EFI_MEMORY_XP) ||
585 (md->type == EFI_RUNTIME_SERVICES_DATA))
586 pf |= _PAGE_NX;
587
588 if (!(md->attribute & EFI_MEMORY_RO) &&
589 (md->type != EFI_RUNTIME_SERVICES_CODE))
590 pf |= _PAGE_RW;
591
Sai Praneeth18141e82017-01-31 13:21:37 +0000592 efi_update_mappings(md, pf);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000593 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100594}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100595
596void __init efi_dump_pagetable(void)
597{
598#ifdef CONFIG_EFI_PGT_DUMP
Sai Praneethac81d3d2017-06-02 13:52:06 +0000599 if (efi_enabled(EFI_OLD_MEMMAP))
600 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
601 else
602 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100603#endif
604}
Matt Fleming994448f2014-03-05 18:15:37 +0000605
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000606#ifdef CONFIG_EFI_MIXED
607extern efi_status_t efi64_thunk(u32, ...);
608
609#define runtime_service32(func) \
610({ \
611 u32 table = (u32)(unsigned long)efi.systab; \
612 u32 *rt, *___f; \
613 \
614 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
615 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
616 *___f; \
617})
618
619/*
620 * Switch to the EFI page tables early so that we can access the 1:1
621 * runtime services mappings which are not mapped in any other page
622 * tables. This function must be called before runtime_service32().
623 *
624 * Also, disable interrupts because the IDT points to 64-bit handlers,
625 * which aren't going to function correctly when we switch to 32-bit.
626 */
627#define efi_thunk(f, ...) \
628({ \
629 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100630 unsigned long __flags; \
631 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000632 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100633 local_irq_save(__flags); \
634 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000635 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100636 __func = runtime_service32(f); \
637 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000638 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100639 arch_efi_call_virt_teardown(); \
640 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000641 \
642 __s; \
643})
644
645efi_status_t efi_thunk_set_virtual_address_map(
646 void *phys_set_virtual_address_map,
647 unsigned long memory_map_size,
648 unsigned long descriptor_size,
649 u32 descriptor_version,
650 efi_memory_desc_t *virtual_map)
651{
652 efi_status_t status;
653 unsigned long flags;
654 u32 func;
655
656 efi_sync_low_kernel_mappings();
657 local_irq_save(flags);
658
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700659 efi_scratch.prev_cr3 = __read_cr3();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000660 write_cr3((unsigned long)efi_scratch.efi_pgt);
661 __flush_tlb_all();
662
663 func = (u32)(unsigned long)phys_set_virtual_address_map;
664 status = efi64_thunk(func, memory_map_size, descriptor_size,
665 descriptor_version, virtual_map);
666
667 write_cr3(efi_scratch.prev_cr3);
668 __flush_tlb_all();
669 local_irq_restore(flags);
670
671 return status;
672}
673
674static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
675{
676 efi_status_t status;
677 u32 phys_tm, phys_tc;
678
679 spin_lock(&rtc_lock);
680
Matt Flemingf6697df2016-11-12 21:04:24 +0000681 phys_tm = virt_to_phys_or_null(tm);
682 phys_tc = virt_to_phys_or_null(tc);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000683
684 status = efi_thunk(get_time, phys_tm, phys_tc);
685
686 spin_unlock(&rtc_lock);
687
688 return status;
689}
690
691static efi_status_t efi_thunk_set_time(efi_time_t *tm)
692{
693 efi_status_t status;
694 u32 phys_tm;
695
696 spin_lock(&rtc_lock);
697
Matt Flemingf6697df2016-11-12 21:04:24 +0000698 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000699
700 status = efi_thunk(set_time, phys_tm);
701
702 spin_unlock(&rtc_lock);
703
704 return status;
705}
706
707static efi_status_t
708efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
709 efi_time_t *tm)
710{
711 efi_status_t status;
712 u32 phys_enabled, phys_pending, phys_tm;
713
714 spin_lock(&rtc_lock);
715
Matt Flemingf6697df2016-11-12 21:04:24 +0000716 phys_enabled = virt_to_phys_or_null(enabled);
717 phys_pending = virt_to_phys_or_null(pending);
718 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000719
720 status = efi_thunk(get_wakeup_time, phys_enabled,
721 phys_pending, phys_tm);
722
723 spin_unlock(&rtc_lock);
724
725 return status;
726}
727
728static efi_status_t
729efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
730{
731 efi_status_t status;
732 u32 phys_tm;
733
734 spin_lock(&rtc_lock);
735
Matt Flemingf6697df2016-11-12 21:04:24 +0000736 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000737
738 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
739
740 spin_unlock(&rtc_lock);
741
742 return status;
743}
744
Matt Flemingf6697df2016-11-12 21:04:24 +0000745static unsigned long efi_name_size(efi_char16_t *name)
746{
747 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
748}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000749
750static efi_status_t
751efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
752 u32 *attr, unsigned long *data_size, void *data)
753{
754 efi_status_t status;
755 u32 phys_name, phys_vendor, phys_attr;
756 u32 phys_data_size, phys_data;
757
Matt Flemingf6697df2016-11-12 21:04:24 +0000758 phys_data_size = virt_to_phys_or_null(data_size);
759 phys_vendor = virt_to_phys_or_null(vendor);
760 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
761 phys_attr = virt_to_phys_or_null(attr);
762 phys_data = virt_to_phys_or_null_size(data, *data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000763
764 status = efi_thunk(get_variable, phys_name, phys_vendor,
765 phys_attr, phys_data_size, phys_data);
766
767 return status;
768}
769
770static efi_status_t
771efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
772 u32 attr, unsigned long data_size, void *data)
773{
774 u32 phys_name, phys_vendor, phys_data;
775 efi_status_t status;
776
Matt Flemingf6697df2016-11-12 21:04:24 +0000777 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
778 phys_vendor = virt_to_phys_or_null(vendor);
779 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000780
781 /* If data_size is > sizeof(u32) we've got problems */
782 status = efi_thunk(set_variable, phys_name, phys_vendor,
783 attr, data_size, phys_data);
784
785 return status;
786}
787
788static efi_status_t
789efi_thunk_get_next_variable(unsigned long *name_size,
790 efi_char16_t *name,
791 efi_guid_t *vendor)
792{
793 efi_status_t status;
794 u32 phys_name_size, phys_name, phys_vendor;
795
Matt Flemingf6697df2016-11-12 21:04:24 +0000796 phys_name_size = virt_to_phys_or_null(name_size);
797 phys_vendor = virt_to_phys_or_null(vendor);
798 phys_name = virt_to_phys_or_null_size(name, *name_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000799
800 status = efi_thunk(get_next_variable, phys_name_size,
801 phys_name, phys_vendor);
802
803 return status;
804}
805
806static efi_status_t
807efi_thunk_get_next_high_mono_count(u32 *count)
808{
809 efi_status_t status;
810 u32 phys_count;
811
Matt Flemingf6697df2016-11-12 21:04:24 +0000812 phys_count = virt_to_phys_or_null(count);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000813 status = efi_thunk(get_next_high_mono_count, phys_count);
814
815 return status;
816}
817
818static void
819efi_thunk_reset_system(int reset_type, efi_status_t status,
820 unsigned long data_size, efi_char16_t *data)
821{
822 u32 phys_data;
823
Matt Flemingf6697df2016-11-12 21:04:24 +0000824 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000825
826 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
827}
828
829static efi_status_t
830efi_thunk_update_capsule(efi_capsule_header_t **capsules,
831 unsigned long count, unsigned long sg_list)
832{
833 /*
834 * To properly support this function we would need to repackage
835 * 'capsules' because the firmware doesn't understand 64-bit
836 * pointers.
837 */
838 return EFI_UNSUPPORTED;
839}
840
841static efi_status_t
842efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
843 u64 *remaining_space,
844 u64 *max_variable_size)
845{
846 efi_status_t status;
847 u32 phys_storage, phys_remaining, phys_max;
848
849 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
850 return EFI_UNSUPPORTED;
851
Matt Flemingf6697df2016-11-12 21:04:24 +0000852 phys_storage = virt_to_phys_or_null(storage_space);
853 phys_remaining = virt_to_phys_or_null(remaining_space);
854 phys_max = virt_to_phys_or_null(max_variable_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000855
Matt Fleming9a110402014-03-16 17:46:46 +0000856 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000857 phys_remaining, phys_max);
858
859 return status;
860}
861
862static efi_status_t
863efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
864 unsigned long count, u64 *max_size,
865 int *reset_type)
866{
867 /*
868 * To properly support this function we would need to repackage
869 * 'capsules' because the firmware doesn't understand 64-bit
870 * pointers.
871 */
872 return EFI_UNSUPPORTED;
873}
874
875void efi_thunk_runtime_setup(void)
876{
877 efi.get_time = efi_thunk_get_time;
878 efi.set_time = efi_thunk_set_time;
879 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
880 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
881 efi.get_variable = efi_thunk_get_variable;
882 efi.get_next_variable = efi_thunk_get_next_variable;
883 efi.set_variable = efi_thunk_set_variable;
884 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
885 efi.reset_system = efi_thunk_reset_system;
886 efi.query_variable_info = efi_thunk_query_variable_info;
887 efi.update_capsule = efi_thunk_update_capsule;
888 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
889}
890#endif /* CONFIG_EFI_MIXED */