blob: 39c4b35ac7a4a9cc0d2b54cbc7b7ea58997da09f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Huang, Ying5b836832008-01-30 13:31:19 +01002/*
3 * x86_64 specific EFI support functions
4 * Based on Extensible Firmware Interface Specification version 1.0
5 *
6 * Copyright (C) 2005-2008 Intel Co.
7 * Fenghua Yu <fenghua.yu@intel.com>
8 * Bibo Mao <bibo.mao@intel.com>
9 * Chandramouli Narayanan <mouli@linux.intel.com>
10 * Huang Ying <ying.huang@intel.com>
11 *
12 * Code to convert EFI to E820 map has been implemented in elilo bootloader
13 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
14 * is setup appropriately for EFI runtime code.
15 * - mouli 06/14/2007.
16 *
17 */
18
Matt Fleming26d7f652015-10-25 10:26:35 +000019#define pr_fmt(fmt) "efi: " fmt
20
Huang, Ying5b836832008-01-30 13:31:19 +010021#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/mm.h>
24#include <linux/types.h>
25#include <linux/spinlock.h>
26#include <linux/bootmem.h>
27#include <linux/ioport.h>
Paul Gortmakercc3ae7b2016-07-13 20:18:58 -040028#include <linux/init.h>
Arnd Bergmann5ab788d2016-05-30 20:57:50 +020029#include <linux/mc146818rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010030#include <linux/efi.h>
31#include <linux/uaccess.h>
32#include <linux/io.h>
33#include <linux/reboot.h>
David Howells0d01ff22013-04-11 23:51:01 +010034#include <linux/slab.h>
Matt Flemingf6697df2016-11-12 21:04:24 +000035#include <linux/ucs2_string.h>
Huang, Ying5b836832008-01-30 13:31:19 +010036
37#include <asm/setup.h>
38#include <asm/page.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010039#include <asm/e820/api.h>
Huang, Ying5b836832008-01-30 13:31:19 +010040#include <asm/pgtable.h>
41#include <asm/tlbflush.h>
Huang, Ying5b836832008-01-30 13:31:19 +010042#include <asm/proto.h>
43#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080044#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090045#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010046#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000047#include <asm/time.h>
Matt Fleming67a91082015-11-27 21:09:34 +000048#include <asm/pgalloc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010049
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010050/*
Baoquan Heb1d17762017-04-04 17:02:43 +010051 * We allocate runtime services regions top-down, starting from -4G, i.e.
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010052 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
53 */
Mathias Krause8266e312014-09-21 17:26:54 +020054static u64 efi_va = EFI_VA_START;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010055
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000056struct efi_scratch efi_scratch;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010057
Matthew Garrett9cd2b072011-05-05 15:19:43 -040058static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010059{
60 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +010061
Huang, Yinga2172e22008-01-30 13:33:55 +010062 if (!(__supported_pte_mask & _PAGE_NX))
63 return;
64
Matthew Garrett916f6762011-05-25 09:53:13 -040065 /* Make EFI service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +010066 for_each_efi_memory_desc(md) {
Matthew Garrett916f6762011-05-25 09:53:13 -040067 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
68 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040069 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010070 }
71}
72
Ingo Molnar744937b2015-03-03 07:48:50 +010073pgd_t * __init efi_call_phys_prolog(void)
Huang, Ying5b836832008-01-30 13:31:19 +010074{
Baoquan He94133e42017-05-26 12:36:50 +010075 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
76 pgd_t *save_pgd, *pgd_k, *pgd_efi;
77 p4d_t *p4d, *p4d_k, *p4d_efi;
78 pud_t *pud;
Ingo Molnar744937b2015-03-03 07:48:50 +010079
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060080 int pgd;
Baoquan He94133e42017-05-26 12:36:50 +010081 int n_pgds, i, j;
Huang, Ying5b836832008-01-30 13:31:19 +010082
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000083 if (!efi_enabled(EFI_OLD_MEMMAP)) {
Andy Lutomirski6c690ee2017-06-12 10:26:14 -070084 save_pgd = (pgd_t *)__read_cr3();
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000085 write_cr3((unsigned long)efi_scratch.efi_pgt);
86 goto out;
87 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010088
Matthew Garrett9cd2b072011-05-05 15:19:43 -040089 early_code_mapping_set_exec(1);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060090
91 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
Markus Elfring20ebc152016-08-25 11:34:03 +020092 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060093
Baoquan He94133e42017-05-26 12:36:50 +010094 /*
95 * Build 1:1 identity mapping for efi=old_map usage. Note that
96 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
97 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
98 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
99 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
100 * This means here we can only reuse the PMD tables of the direct mapping.
101 */
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600102 for (pgd = 0; pgd < n_pgds; pgd++) {
Baoquan He94133e42017-05-26 12:36:50 +0100103 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
104 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
105 pgd_efi = pgd_offset_k(addr_pgd);
106 save_pgd[pgd] = *pgd_efi;
107
108 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
109 if (!p4d) {
110 pr_err("Failed to allocate p4d table!\n");
111 goto out;
112 }
113
114 for (i = 0; i < PTRS_PER_P4D; i++) {
115 addr_p4d = addr_pgd + i * P4D_SIZE;
116 p4d_efi = p4d + p4d_index(addr_p4d);
117
118 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
119 if (!pud) {
120 pr_err("Failed to allocate pud table!\n");
121 goto out;
122 }
123
124 for (j = 0; j < PTRS_PER_PUD; j++) {
125 addr_pud = addr_p4d + j * PUD_SIZE;
126
127 if (addr_pud > (max_pfn << PAGE_SHIFT))
128 break;
129
130 vaddr = (unsigned long)__va(addr_pud);
131
132 pgd_k = pgd_offset_k(vaddr);
133 p4d_k = p4d_offset(pgd_k, vaddr);
134 pud[j] = *pud_offset(p4d_k, vaddr);
135 }
136 }
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600137 }
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000138out:
Huang, Ying5b836832008-01-30 13:31:19 +0100139 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +0100140
141 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +0100142}
143
Ingo Molnar744937b2015-03-03 07:48:50 +0100144void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100145{
146 /*
147 * After the lock is released, the original page table is restored.
148 */
Baoquan He94133e42017-05-26 12:36:50 +0100149 int pgd_idx, i;
Ingo Molnar744937b2015-03-03 07:48:50 +0100150 int nr_pgds;
Baoquan He94133e42017-05-26 12:36:50 +0100151 pgd_t *pgd;
152 p4d_t *p4d;
153 pud_t *pud;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100154
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000155 if (!efi_enabled(EFI_OLD_MEMMAP)) {
156 write_cr3((unsigned long)save_pgd);
157 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100158 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000159 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100160
Ingo Molnar744937b2015-03-03 07:48:50 +0100161 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
162
Baoquan He94133e42017-05-26 12:36:50 +0100163 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
164 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
Ingo Molnar744937b2015-03-03 07:48:50 +0100165 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
166
Baoquan He94133e42017-05-26 12:36:50 +0100167 if (!(pgd_val(*pgd) & _PAGE_PRESENT))
168 continue;
169
170 for (i = 0; i < PTRS_PER_P4D; i++) {
171 p4d = p4d_offset(pgd,
172 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
173
174 if (!(p4d_val(*p4d) & _PAGE_PRESENT))
175 continue;
176
177 pud = (pud_t *)p4d_page_vaddr(*p4d);
178 pud_free(&init_mm, pud);
179 }
180
181 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
182 p4d_free(&init_mm, p4d);
183 }
184
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600185 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100186
Huang, Ying5b836832008-01-30 13:31:19 +0100187 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400188 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100189}
Keith Packarde1ad7832011-12-11 16:12:42 -0800190
Matt Fleming67a91082015-11-27 21:09:34 +0000191static pgd_t *efi_pgd;
192
193/*
194 * We need our own copy of the higher levels of the page tables
195 * because we want to avoid inserting EFI region mappings (EFI_VA_END
196 * to EFI_VA_START) into the standard kernel page tables. Everything
197 * else can be shared, see efi_sync_low_kernel_mappings().
Dave Hansend9e9a642017-12-04 15:07:39 +0100198 *
199 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
200 * allocation.
Matt Fleming67a91082015-11-27 21:09:34 +0000201 */
202int __init efi_alloc_page_tables(void)
203{
204 pgd_t *pgd;
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300205 p4d_t *p4d;
Matt Fleming67a91082015-11-27 21:09:34 +0000206 pud_t *pud;
207 gfp_t gfp_mask;
208
209 if (efi_enabled(EFI_OLD_MEMMAP))
210 return 0;
211
Michal Hockof58f2302016-06-24 14:48:53 -0700212 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
Dave Hansend9e9a642017-12-04 15:07:39 +0100213 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
Matt Fleming67a91082015-11-27 21:09:34 +0000214 if (!efi_pgd)
215 return -ENOMEM;
216
217 pgd = efi_pgd + pgd_index(EFI_VA_END);
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300218 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
219 if (!p4d) {
Matt Fleming67a91082015-11-27 21:09:34 +0000220 free_page((unsigned long)efi_pgd);
221 return -ENOMEM;
222 }
223
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300224 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
225 if (!pud) {
226 if (CONFIG_PGTABLE_LEVELS > 4)
227 free_page((unsigned long) pgd_page_vaddr(*pgd));
228 free_page((unsigned long)efi_pgd);
229 return -ENOMEM;
230 }
Matt Fleming67a91082015-11-27 21:09:34 +0000231
232 return 0;
233}
234
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100235/*
236 * Add low kernel mappings for passing arguments to EFI functions.
237 */
238void efi_sync_low_kernel_mappings(void)
239{
Matt Fleming67a91082015-11-27 21:09:34 +0000240 unsigned num_entries;
241 pgd_t *pgd_k, *pgd_efi;
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300242 p4d_t *p4d_k, *p4d_efi;
Matt Fleming67a91082015-11-27 21:09:34 +0000243 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100244
245 if (efi_enabled(EFI_OLD_MEMMAP))
246 return;
247
Matt Fleming67a91082015-11-27 21:09:34 +0000248 /*
249 * We can share all PGD entries apart from the one entry that
250 * covers the EFI runtime mapping space.
251 *
252 * Make sure the EFI runtime region mappings are guaranteed to
253 * only span a single PGD entry and that the entry also maps
254 * other important kernel regions.
255 */
256 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
257 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
258 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100259
Matt Fleming67a91082015-11-27 21:09:34 +0000260 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
261 pgd_k = pgd_offset_k(PAGE_OFFSET);
262
263 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
264 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
265
266 /*
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300267 * As with PGDs, we share all P4D entries apart from the one entry
268 * that covers the EFI runtime mapping space.
269 */
270 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
271 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
272
273 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
274 pgd_k = pgd_offset_k(EFI_VA_END);
275 p4d_efi = p4d_offset(pgd_efi, 0);
276 p4d_k = p4d_offset(pgd_k, 0);
277
278 num_entries = p4d_index(EFI_VA_END);
279 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
280
281 /*
Matt Fleming67a91082015-11-27 21:09:34 +0000282 * We share all the PUD entries apart from those that map the
283 * EFI regions. Copy around them.
284 */
285 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
286 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
287
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300288 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
289 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300290 pud_efi = pud_offset(p4d_efi, 0);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300291 pud_k = pud_offset(p4d_k, 0);
Matt Fleming67a91082015-11-27 21:09:34 +0000292
293 num_entries = pud_index(EFI_VA_END);
294 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
295
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300296 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300297 pud_k = pud_offset(p4d_k, EFI_VA_START);
Matt Fleming67a91082015-11-27 21:09:34 +0000298
299 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
300 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100301}
302
Matt Flemingf6697df2016-11-12 21:04:24 +0000303/*
304 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
305 */
306static inline phys_addr_t
307virt_to_phys_or_null_size(void *va, unsigned long size)
308{
309 bool bad_size;
310
311 if (!va)
312 return 0;
313
314 if (virt_addr_valid(va))
315 return virt_to_phys(va);
316
317 /*
318 * A fully aligned variable on the stack is guaranteed not to
319 * cross a page bounary. Try to catch strings on the stack by
320 * checking that 'size' is a power of two.
321 */
322 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
323
324 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
325
326 return slow_virt_to_phys(va);
327}
328
329#define virt_to_phys_or_null(addr) \
330 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
331
Mathias Krause4e78eb052014-09-07 19:42:17 +0200332int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100333{
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500334 unsigned long pfn, text, pf;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000335 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000336 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100337 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100338
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100339 if (efi_enabled(EFI_OLD_MEMMAP))
340 return 0;
341
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500342 /*
343 * Since the PGD is encrypted, set the encryption mask so that when
344 * this value is loaded into cr3 the PGD will be decrypted during
345 * the pagetable walk.
346 */
347 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
Matt Fleming67a91082015-11-27 21:09:34 +0000348 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100349
350 /*
351 * It can happen that the physical address of new_memmap lands in memory
352 * which is not mapped in the EFI page table. Therefore we need to go
353 * and ident-map those pages containing the map before calling
354 * phys_efi_set_virtual_address_map().
355 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000356 pfn = pa_memmap >> PAGE_SHIFT;
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500357 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
358 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100359 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
360 return 1;
361 }
362
363 efi_scratch.use_pgd = true;
364
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000365 /*
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000366 * Certain firmware versions are way too sentimential and still believe
367 * they are exclusive and unquestionable owners of the first physical page,
368 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
369 * (but then write-access it later during SetVirtualAddressMap()).
370 *
371 * Create a 1:1 mapping for this page, to avoid triple faults during early
372 * boot with such firmware. We are free to hand this page to the BIOS,
373 * as trim_bios_range() will reserve the first page and isolate it away
374 * from memory allocators anyway.
375 */
376 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
377 pr_err("Failed to create 1:1 mapping for the first page!\n");
378 return 1;
379 }
380
381 /*
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000382 * When making calls to the firmware everything needs to be 1:1
383 * mapped and addressable with 32-bit pointers. Map the kernel
384 * text and allocate a new stack because we can't rely on the
385 * stack pointer being < 4GB.
386 */
Matt Fleming12976672016-09-19 13:09:09 +0100387 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000388 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000389
390 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
391 if (!page)
392 panic("Unable to allocate EFI runtime stack < 4GB\n");
393
394 efi_scratch.phys_stack = virt_to_phys(page_address(page));
395 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
396
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000397 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000398 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000399 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000400
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500401 pf = _PAGE_RW | _PAGE_ENC;
402 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000403 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000404 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000405 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100406
407 return 0;
408}
409
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100410static void __init __map_region(efi_memory_desc_t *md, u64 va)
411{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000412 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000413 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000414 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100415
416 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000417 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100418
Matt Flemingedc3b912015-11-27 21:09:31 +0000419 pfn = md->phys_addr >> PAGE_SHIFT;
420 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100421 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
422 md->phys_addr, va);
423}
424
425void __init efi_map_region(efi_memory_desc_t *md)
426{
427 unsigned long size = md->num_pages << PAGE_SHIFT;
428 u64 pa = md->phys_addr;
429
430 if (efi_enabled(EFI_OLD_MEMMAP))
431 return old_map_region(md);
432
433 /*
434 * Make sure the 1:1 mappings are present as a catch-all for b0rked
435 * firmware which doesn't update all internal pointers after switching
436 * to virtual mode and would otherwise crap on us.
437 */
438 __map_region(md, md->phys_addr);
439
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000440 /*
441 * Enforce the 1:1 mapping as the default virtual address when
442 * booting in EFI mixed mode, because even though we may be
443 * running a 64-bit kernel, the firmware may only be 32-bit.
444 */
445 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
446 md->virt_addr = md->phys_addr;
447 return;
448 }
449
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100450 efi_va -= size;
451
452 /* Is PA 2M-aligned? */
453 if (!(pa & (PMD_SIZE - 1))) {
454 efi_va &= PMD_MASK;
455 } else {
456 u64 pa_offset = pa & (PMD_SIZE - 1);
457 u64 prev_va = efi_va;
458
459 /* get us the same offset within this 2M page */
460 efi_va = (efi_va & PMD_MASK) + pa_offset;
461
462 if (efi_va > prev_va)
463 efi_va -= PMD_SIZE;
464 }
465
466 if (efi_va < EFI_VA_END) {
467 pr_warn(FW_WARN "VA address range overflow!\n");
468 return;
469 }
470
471 /* Do the VA map */
472 __map_region(md, efi_va);
473 md->virt_addr = efi_va;
474}
475
Dave Young3b266492013-12-20 18:02:14 +0800476/*
477 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
478 * md->virt_addr is the original virtual address which had been mapped in kexec
479 * 1st kernel.
480 */
481void __init efi_map_region_fixed(efi_memory_desc_t *md)
482{
Alex Thorlton0513fe12016-08-05 18:59:35 -0500483 __map_region(md, md->phys_addr);
Dave Young3b266492013-12-20 18:02:14 +0800484 __map_region(md, md->virt_addr);
485}
486
Keith Packarde1ad7832011-12-11 16:12:42 -0800487void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100488 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800489{
490 unsigned long last_map_pfn;
491
492 if (type == EFI_MEMORY_MAPPED_IO)
493 return ioremap(phys_addr, size);
494
495 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
496 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
497 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100498 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800499 }
500
Matt Fleming3e8fa262012-10-19 13:25:46 +0100501 if (!(attribute & EFI_MEMORY_WB))
502 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
503
Keith Packarde1ad7832011-12-11 16:12:42 -0800504 return (void __iomem *)__va(phys_addr);
505}
Dave Young1fec0532013-12-20 18:02:19 +0800506
507void __init parse_efi_setup(u64 phys_addr, u32 data_len)
508{
509 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800510}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100511
Sai Praneeth18141e82017-01-31 13:21:37 +0000512static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100513{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000514 unsigned long pfn;
515 pgd_t *pgd = efi_pgd;
Sai Praneeth18141e82017-01-31 13:21:37 +0000516 int err1, err2;
517
518 /* Update the 1:1 mapping */
519 pfn = md->phys_addr >> PAGE_SHIFT;
520 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
521 if (err1) {
522 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
523 md->phys_addr, md->virt_addr);
524 }
525
526 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
527 if (err2) {
528 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
529 md->phys_addr, md->virt_addr);
530 }
531
532 return err1 || err2;
533}
534
535static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
536{
537 unsigned long pf = 0;
538
539 if (md->attribute & EFI_MEMORY_XP)
540 pf |= _PAGE_NX;
541
542 if (!(md->attribute & EFI_MEMORY_RO))
543 pf |= _PAGE_RW;
544
545 return efi_update_mappings(md, pf);
546}
547
548void __init efi_runtime_update_mappings(void)
549{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000550 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000551
552 if (efi_enabled(EFI_OLD_MEMMAP)) {
553 if (__supported_pte_mask & _PAGE_NX)
554 runtime_code_page_mkexec();
555 return;
556 }
557
Sai Praneeth18141e82017-01-31 13:21:37 +0000558 /*
559 * Use the EFI Memory Attribute Table for mapping permissions if it
560 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
561 */
562 if (efi_enabled(EFI_MEM_ATTR)) {
563 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
564 return;
565 }
566
567 /*
568 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
569 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
570 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
571 * published by the firmware. Even if we find a buggy implementation of
572 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
573 * EFI_PROPERTIES_TABLE, because of the same reason.
574 */
575
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000576 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100577 return;
578
Matt Fleming78ce2482016-04-25 21:06:38 +0100579 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000580 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000581
582 if (!(md->attribute & EFI_MEMORY_RUNTIME))
583 continue;
584
585 if (!(md->attribute & EFI_MEMORY_WB))
586 pf |= _PAGE_PCD;
587
588 if ((md->attribute & EFI_MEMORY_XP) ||
589 (md->type == EFI_RUNTIME_SERVICES_DATA))
590 pf |= _PAGE_NX;
591
592 if (!(md->attribute & EFI_MEMORY_RO) &&
593 (md->type != EFI_RUNTIME_SERVICES_CODE))
594 pf |= _PAGE_RW;
595
Sai Praneeth18141e82017-01-31 13:21:37 +0000596 efi_update_mappings(md, pf);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000597 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100598}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100599
600void __init efi_dump_pagetable(void)
601{
602#ifdef CONFIG_EFI_PGT_DUMP
Sai Praneethac81d3d2017-06-02 13:52:06 +0000603 if (efi_enabled(EFI_OLD_MEMMAP))
604 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
605 else
606 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100607#endif
608}
Matt Fleming994448f2014-03-05 18:15:37 +0000609
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000610#ifdef CONFIG_EFI_MIXED
611extern efi_status_t efi64_thunk(u32, ...);
612
613#define runtime_service32(func) \
614({ \
615 u32 table = (u32)(unsigned long)efi.systab; \
616 u32 *rt, *___f; \
617 \
618 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
619 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
620 *___f; \
621})
622
623/*
624 * Switch to the EFI page tables early so that we can access the 1:1
625 * runtime services mappings which are not mapped in any other page
626 * tables. This function must be called before runtime_service32().
627 *
628 * Also, disable interrupts because the IDT points to 64-bit handlers,
629 * which aren't going to function correctly when we switch to 32-bit.
630 */
631#define efi_thunk(f, ...) \
632({ \
633 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100634 unsigned long __flags; \
635 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000636 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100637 local_irq_save(__flags); \
638 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000639 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100640 __func = runtime_service32(f); \
641 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000642 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100643 arch_efi_call_virt_teardown(); \
644 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000645 \
646 __s; \
647})
648
649efi_status_t efi_thunk_set_virtual_address_map(
650 void *phys_set_virtual_address_map,
651 unsigned long memory_map_size,
652 unsigned long descriptor_size,
653 u32 descriptor_version,
654 efi_memory_desc_t *virtual_map)
655{
656 efi_status_t status;
657 unsigned long flags;
658 u32 func;
659
660 efi_sync_low_kernel_mappings();
661 local_irq_save(flags);
662
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700663 efi_scratch.prev_cr3 = __read_cr3();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000664 write_cr3((unsigned long)efi_scratch.efi_pgt);
665 __flush_tlb_all();
666
667 func = (u32)(unsigned long)phys_set_virtual_address_map;
668 status = efi64_thunk(func, memory_map_size, descriptor_size,
669 descriptor_version, virtual_map);
670
671 write_cr3(efi_scratch.prev_cr3);
672 __flush_tlb_all();
673 local_irq_restore(flags);
674
675 return status;
676}
677
678static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
679{
680 efi_status_t status;
681 u32 phys_tm, phys_tc;
682
683 spin_lock(&rtc_lock);
684
Matt Flemingf6697df2016-11-12 21:04:24 +0000685 phys_tm = virt_to_phys_or_null(tm);
686 phys_tc = virt_to_phys_or_null(tc);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000687
688 status = efi_thunk(get_time, phys_tm, phys_tc);
689
690 spin_unlock(&rtc_lock);
691
692 return status;
693}
694
695static efi_status_t efi_thunk_set_time(efi_time_t *tm)
696{
697 efi_status_t status;
698 u32 phys_tm;
699
700 spin_lock(&rtc_lock);
701
Matt Flemingf6697df2016-11-12 21:04:24 +0000702 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000703
704 status = efi_thunk(set_time, phys_tm);
705
706 spin_unlock(&rtc_lock);
707
708 return status;
709}
710
711static efi_status_t
712efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
713 efi_time_t *tm)
714{
715 efi_status_t status;
716 u32 phys_enabled, phys_pending, phys_tm;
717
718 spin_lock(&rtc_lock);
719
Matt Flemingf6697df2016-11-12 21:04:24 +0000720 phys_enabled = virt_to_phys_or_null(enabled);
721 phys_pending = virt_to_phys_or_null(pending);
722 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000723
724 status = efi_thunk(get_wakeup_time, phys_enabled,
725 phys_pending, phys_tm);
726
727 spin_unlock(&rtc_lock);
728
729 return status;
730}
731
732static efi_status_t
733efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
734{
735 efi_status_t status;
736 u32 phys_tm;
737
738 spin_lock(&rtc_lock);
739
Matt Flemingf6697df2016-11-12 21:04:24 +0000740 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000741
742 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
743
744 spin_unlock(&rtc_lock);
745
746 return status;
747}
748
Matt Flemingf6697df2016-11-12 21:04:24 +0000749static unsigned long efi_name_size(efi_char16_t *name)
750{
751 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
752}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000753
754static efi_status_t
755efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
756 u32 *attr, unsigned long *data_size, void *data)
757{
758 efi_status_t status;
759 u32 phys_name, phys_vendor, phys_attr;
760 u32 phys_data_size, phys_data;
761
Matt Flemingf6697df2016-11-12 21:04:24 +0000762 phys_data_size = virt_to_phys_or_null(data_size);
763 phys_vendor = virt_to_phys_or_null(vendor);
764 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
765 phys_attr = virt_to_phys_or_null(attr);
766 phys_data = virt_to_phys_or_null_size(data, *data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000767
768 status = efi_thunk(get_variable, phys_name, phys_vendor,
769 phys_attr, phys_data_size, phys_data);
770
771 return status;
772}
773
774static efi_status_t
775efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
776 u32 attr, unsigned long data_size, void *data)
777{
778 u32 phys_name, phys_vendor, phys_data;
779 efi_status_t status;
780
Matt Flemingf6697df2016-11-12 21:04:24 +0000781 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
782 phys_vendor = virt_to_phys_or_null(vendor);
783 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000784
785 /* If data_size is > sizeof(u32) we've got problems */
786 status = efi_thunk(set_variable, phys_name, phys_vendor,
787 attr, data_size, phys_data);
788
789 return status;
790}
791
792static efi_status_t
793efi_thunk_get_next_variable(unsigned long *name_size,
794 efi_char16_t *name,
795 efi_guid_t *vendor)
796{
797 efi_status_t status;
798 u32 phys_name_size, phys_name, phys_vendor;
799
Matt Flemingf6697df2016-11-12 21:04:24 +0000800 phys_name_size = virt_to_phys_or_null(name_size);
801 phys_vendor = virt_to_phys_or_null(vendor);
802 phys_name = virt_to_phys_or_null_size(name, *name_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000803
804 status = efi_thunk(get_next_variable, phys_name_size,
805 phys_name, phys_vendor);
806
807 return status;
808}
809
810static efi_status_t
811efi_thunk_get_next_high_mono_count(u32 *count)
812{
813 efi_status_t status;
814 u32 phys_count;
815
Matt Flemingf6697df2016-11-12 21:04:24 +0000816 phys_count = virt_to_phys_or_null(count);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000817 status = efi_thunk(get_next_high_mono_count, phys_count);
818
819 return status;
820}
821
822static void
823efi_thunk_reset_system(int reset_type, efi_status_t status,
824 unsigned long data_size, efi_char16_t *data)
825{
826 u32 phys_data;
827
Matt Flemingf6697df2016-11-12 21:04:24 +0000828 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000829
830 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
831}
832
833static efi_status_t
834efi_thunk_update_capsule(efi_capsule_header_t **capsules,
835 unsigned long count, unsigned long sg_list)
836{
837 /*
838 * To properly support this function we would need to repackage
839 * 'capsules' because the firmware doesn't understand 64-bit
840 * pointers.
841 */
842 return EFI_UNSUPPORTED;
843}
844
845static efi_status_t
846efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
847 u64 *remaining_space,
848 u64 *max_variable_size)
849{
850 efi_status_t status;
851 u32 phys_storage, phys_remaining, phys_max;
852
853 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
854 return EFI_UNSUPPORTED;
855
Matt Flemingf6697df2016-11-12 21:04:24 +0000856 phys_storage = virt_to_phys_or_null(storage_space);
857 phys_remaining = virt_to_phys_or_null(remaining_space);
858 phys_max = virt_to_phys_or_null(max_variable_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000859
Matt Fleming9a110402014-03-16 17:46:46 +0000860 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000861 phys_remaining, phys_max);
862
863 return status;
864}
865
866static efi_status_t
867efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
868 unsigned long count, u64 *max_size,
869 int *reset_type)
870{
871 /*
872 * To properly support this function we would need to repackage
873 * 'capsules' because the firmware doesn't understand 64-bit
874 * pointers.
875 */
876 return EFI_UNSUPPORTED;
877}
878
879void efi_thunk_runtime_setup(void)
880{
881 efi.get_time = efi_thunk_get_time;
882 efi.set_time = efi_thunk_set_time;
883 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
884 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
885 efi.get_variable = efi_thunk_get_variable;
886 efi.get_next_variable = efi_thunk_get_next_variable;
887 efi.set_variable = efi_thunk_set_variable;
888 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
889 efi.reset_system = efi_thunk_reset_system;
890 efi.query_variable_info = efi_thunk_query_variable_info;
891 efi.update_capsule = efi_thunk_update_capsule;
892 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
893}
894#endif /* CONFIG_EFI_MIXED */