blob: 61975b6bcb1a0a51d88d2f9be779411915cd5e79 [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 }
Jiri Kosinade53c372018-01-05 22:35:41 +0100137 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600138 }
Jiri Kosinade53c372018-01-05 22:35:41 +0100139
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000140out:
Huang, Ying5b836832008-01-30 13:31:19 +0100141 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +0100142
143 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +0100144}
145
Ingo Molnar744937b2015-03-03 07:48:50 +0100146void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100147{
148 /*
149 * After the lock is released, the original page table is restored.
150 */
Baoquan He94133e42017-05-26 12:36:50 +0100151 int pgd_idx, i;
Ingo Molnar744937b2015-03-03 07:48:50 +0100152 int nr_pgds;
Baoquan He94133e42017-05-26 12:36:50 +0100153 pgd_t *pgd;
154 p4d_t *p4d;
155 pud_t *pud;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100156
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000157 if (!efi_enabled(EFI_OLD_MEMMAP)) {
158 write_cr3((unsigned long)save_pgd);
159 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100160 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000161 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100162
Ingo Molnar744937b2015-03-03 07:48:50 +0100163 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
164
Baoquan He94133e42017-05-26 12:36:50 +0100165 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
166 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
Ingo Molnar744937b2015-03-03 07:48:50 +0100167 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
168
Baoquan He94133e42017-05-26 12:36:50 +0100169 if (!(pgd_val(*pgd) & _PAGE_PRESENT))
170 continue;
171
172 for (i = 0; i < PTRS_PER_P4D; i++) {
173 p4d = p4d_offset(pgd,
174 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
175
176 if (!(p4d_val(*p4d) & _PAGE_PRESENT))
177 continue;
178
179 pud = (pud_t *)p4d_page_vaddr(*p4d);
180 pud_free(&init_mm, pud);
181 }
182
183 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
184 p4d_free(&init_mm, p4d);
185 }
186
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600187 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100188
Huang, Ying5b836832008-01-30 13:31:19 +0100189 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400190 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100191}
Keith Packarde1ad7832011-12-11 16:12:42 -0800192
Matt Fleming67a91082015-11-27 21:09:34 +0000193static pgd_t *efi_pgd;
194
195/*
196 * We need our own copy of the higher levels of the page tables
197 * because we want to avoid inserting EFI region mappings (EFI_VA_END
198 * to EFI_VA_START) into the standard kernel page tables. Everything
199 * else can be shared, see efi_sync_low_kernel_mappings().
Dave Hansend9e9a642017-12-04 15:07:39 +0100200 *
201 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
202 * allocation.
Matt Fleming67a91082015-11-27 21:09:34 +0000203 */
204int __init efi_alloc_page_tables(void)
205{
206 pgd_t *pgd;
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300207 p4d_t *p4d;
Matt Fleming67a91082015-11-27 21:09:34 +0000208 pud_t *pud;
209 gfp_t gfp_mask;
210
211 if (efi_enabled(EFI_OLD_MEMMAP))
212 return 0;
213
Michal Hockof58f2302016-06-24 14:48:53 -0700214 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
Dave Hansend9e9a642017-12-04 15:07:39 +0100215 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
Matt Fleming67a91082015-11-27 21:09:34 +0000216 if (!efi_pgd)
217 return -ENOMEM;
218
219 pgd = efi_pgd + pgd_index(EFI_VA_END);
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300220 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
221 if (!p4d) {
Matt Fleming67a91082015-11-27 21:09:34 +0000222 free_page((unsigned long)efi_pgd);
223 return -ENOMEM;
224 }
225
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300226 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
227 if (!pud) {
228 if (CONFIG_PGTABLE_LEVELS > 4)
229 free_page((unsigned long) pgd_page_vaddr(*pgd));
230 free_page((unsigned long)efi_pgd);
231 return -ENOMEM;
232 }
Matt Fleming67a91082015-11-27 21:09:34 +0000233
234 return 0;
235}
236
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100237/*
238 * Add low kernel mappings for passing arguments to EFI functions.
239 */
240void efi_sync_low_kernel_mappings(void)
241{
Matt Fleming67a91082015-11-27 21:09:34 +0000242 unsigned num_entries;
243 pgd_t *pgd_k, *pgd_efi;
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300244 p4d_t *p4d_k, *p4d_efi;
Matt Fleming67a91082015-11-27 21:09:34 +0000245 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100246
247 if (efi_enabled(EFI_OLD_MEMMAP))
248 return;
249
Matt Fleming67a91082015-11-27 21:09:34 +0000250 /*
251 * We can share all PGD entries apart from the one entry that
252 * covers the EFI runtime mapping space.
253 *
254 * Make sure the EFI runtime region mappings are guaranteed to
255 * only span a single PGD entry and that the entry also maps
256 * other important kernel regions.
257 */
258 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
259 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
260 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100261
Matt Fleming67a91082015-11-27 21:09:34 +0000262 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
263 pgd_k = pgd_offset_k(PAGE_OFFSET);
264
265 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
266 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
267
268 /*
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300269 * As with PGDs, we share all P4D entries apart from the one entry
270 * that covers the EFI runtime mapping space.
271 */
272 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
273 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
274
275 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
276 pgd_k = pgd_offset_k(EFI_VA_END);
277 p4d_efi = p4d_offset(pgd_efi, 0);
278 p4d_k = p4d_offset(pgd_k, 0);
279
280 num_entries = p4d_index(EFI_VA_END);
281 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
282
283 /*
Matt Fleming67a91082015-11-27 21:09:34 +0000284 * We share all the PUD entries apart from those that map the
285 * EFI regions. Copy around them.
286 */
287 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
288 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
289
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300290 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
291 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300292 pud_efi = pud_offset(p4d_efi, 0);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300293 pud_k = pud_offset(p4d_k, 0);
Matt Fleming67a91082015-11-27 21:09:34 +0000294
295 num_entries = pud_index(EFI_VA_END);
296 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
297
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300298 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300299 pud_k = pud_offset(p4d_k, EFI_VA_START);
Matt Fleming67a91082015-11-27 21:09:34 +0000300
301 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
302 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100303}
304
Matt Flemingf6697df2016-11-12 21:04:24 +0000305/*
306 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
307 */
308static inline phys_addr_t
309virt_to_phys_or_null_size(void *va, unsigned long size)
310{
311 bool bad_size;
312
313 if (!va)
314 return 0;
315
316 if (virt_addr_valid(va))
317 return virt_to_phys(va);
318
319 /*
320 * A fully aligned variable on the stack is guaranteed not to
321 * cross a page bounary. Try to catch strings on the stack by
322 * checking that 'size' is a power of two.
323 */
324 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
325
326 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
327
328 return slow_virt_to_phys(va);
329}
330
331#define virt_to_phys_or_null(addr) \
332 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
333
Mathias Krause4e78eb052014-09-07 19:42:17 +0200334int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100335{
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500336 unsigned long pfn, text, pf;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000337 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000338 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100339 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100340
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100341 if (efi_enabled(EFI_OLD_MEMMAP))
342 return 0;
343
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500344 /*
345 * Since the PGD is encrypted, set the encryption mask so that when
346 * this value is loaded into cr3 the PGD will be decrypted during
347 * the pagetable walk.
348 */
349 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
Matt Fleming67a91082015-11-27 21:09:34 +0000350 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100351
352 /*
353 * It can happen that the physical address of new_memmap lands in memory
354 * which is not mapped in the EFI page table. Therefore we need to go
355 * and ident-map those pages containing the map before calling
356 * phys_efi_set_virtual_address_map().
357 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000358 pfn = pa_memmap >> PAGE_SHIFT;
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500359 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
360 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100361 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
362 return 1;
363 }
364
365 efi_scratch.use_pgd = true;
366
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000367 /*
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000368 * Certain firmware versions are way too sentimential and still believe
369 * they are exclusive and unquestionable owners of the first physical page,
370 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
371 * (but then write-access it later during SetVirtualAddressMap()).
372 *
373 * Create a 1:1 mapping for this page, to avoid triple faults during early
374 * boot with such firmware. We are free to hand this page to the BIOS,
375 * as trim_bios_range() will reserve the first page and isolate it away
376 * from memory allocators anyway.
377 */
378 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
379 pr_err("Failed to create 1:1 mapping for the first page!\n");
380 return 1;
381 }
382
383 /*
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000384 * When making calls to the firmware everything needs to be 1:1
385 * mapped and addressable with 32-bit pointers. Map the kernel
386 * text and allocate a new stack because we can't rely on the
387 * stack pointer being < 4GB.
388 */
Matt Fleming12976672016-09-19 13:09:09 +0100389 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000390 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000391
392 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
393 if (!page)
394 panic("Unable to allocate EFI runtime stack < 4GB\n");
395
396 efi_scratch.phys_stack = virt_to_phys(page_address(page));
397 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
398
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000399 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000400 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000401 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000402
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500403 pf = _PAGE_RW | _PAGE_ENC;
404 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000405 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000406 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000407 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100408
409 return 0;
410}
411
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100412static void __init __map_region(efi_memory_desc_t *md, u64 va)
413{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000414 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000415 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000416 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100417
418 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000419 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100420
Matt Flemingedc3b912015-11-27 21:09:31 +0000421 pfn = md->phys_addr >> PAGE_SHIFT;
422 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100423 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
424 md->phys_addr, va);
425}
426
427void __init efi_map_region(efi_memory_desc_t *md)
428{
429 unsigned long size = md->num_pages << PAGE_SHIFT;
430 u64 pa = md->phys_addr;
431
432 if (efi_enabled(EFI_OLD_MEMMAP))
433 return old_map_region(md);
434
435 /*
436 * Make sure the 1:1 mappings are present as a catch-all for b0rked
437 * firmware which doesn't update all internal pointers after switching
438 * to virtual mode and would otherwise crap on us.
439 */
440 __map_region(md, md->phys_addr);
441
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000442 /*
443 * Enforce the 1:1 mapping as the default virtual address when
444 * booting in EFI mixed mode, because even though we may be
445 * running a 64-bit kernel, the firmware may only be 32-bit.
446 */
447 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
448 md->virt_addr = md->phys_addr;
449 return;
450 }
451
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100452 efi_va -= size;
453
454 /* Is PA 2M-aligned? */
455 if (!(pa & (PMD_SIZE - 1))) {
456 efi_va &= PMD_MASK;
457 } else {
458 u64 pa_offset = pa & (PMD_SIZE - 1);
459 u64 prev_va = efi_va;
460
461 /* get us the same offset within this 2M page */
462 efi_va = (efi_va & PMD_MASK) + pa_offset;
463
464 if (efi_va > prev_va)
465 efi_va -= PMD_SIZE;
466 }
467
468 if (efi_va < EFI_VA_END) {
469 pr_warn(FW_WARN "VA address range overflow!\n");
470 return;
471 }
472
473 /* Do the VA map */
474 __map_region(md, efi_va);
475 md->virt_addr = efi_va;
476}
477
Dave Young3b266492013-12-20 18:02:14 +0800478/*
479 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
480 * md->virt_addr is the original virtual address which had been mapped in kexec
481 * 1st kernel.
482 */
483void __init efi_map_region_fixed(efi_memory_desc_t *md)
484{
Alex Thorlton0513fe12016-08-05 18:59:35 -0500485 __map_region(md, md->phys_addr);
Dave Young3b266492013-12-20 18:02:14 +0800486 __map_region(md, md->virt_addr);
487}
488
Keith Packarde1ad7832011-12-11 16:12:42 -0800489void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100490 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800491{
492 unsigned long last_map_pfn;
493
494 if (type == EFI_MEMORY_MAPPED_IO)
495 return ioremap(phys_addr, size);
496
497 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
498 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
499 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100500 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800501 }
502
Matt Fleming3e8fa262012-10-19 13:25:46 +0100503 if (!(attribute & EFI_MEMORY_WB))
504 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
505
Keith Packarde1ad7832011-12-11 16:12:42 -0800506 return (void __iomem *)__va(phys_addr);
507}
Dave Young1fec0532013-12-20 18:02:19 +0800508
509void __init parse_efi_setup(u64 phys_addr, u32 data_len)
510{
511 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800512}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100513
Sai Praneeth18141e82017-01-31 13:21:37 +0000514static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100515{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000516 unsigned long pfn;
517 pgd_t *pgd = efi_pgd;
Sai Praneeth18141e82017-01-31 13:21:37 +0000518 int err1, err2;
519
520 /* Update the 1:1 mapping */
521 pfn = md->phys_addr >> PAGE_SHIFT;
522 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
523 if (err1) {
524 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
525 md->phys_addr, md->virt_addr);
526 }
527
528 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
529 if (err2) {
530 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
531 md->phys_addr, md->virt_addr);
532 }
533
534 return err1 || err2;
535}
536
537static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
538{
539 unsigned long pf = 0;
540
541 if (md->attribute & EFI_MEMORY_XP)
542 pf |= _PAGE_NX;
543
544 if (!(md->attribute & EFI_MEMORY_RO))
545 pf |= _PAGE_RW;
546
547 return efi_update_mappings(md, pf);
548}
549
550void __init efi_runtime_update_mappings(void)
551{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000552 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000553
554 if (efi_enabled(EFI_OLD_MEMMAP)) {
555 if (__supported_pte_mask & _PAGE_NX)
556 runtime_code_page_mkexec();
557 return;
558 }
559
Sai Praneeth18141e82017-01-31 13:21:37 +0000560 /*
561 * Use the EFI Memory Attribute Table for mapping permissions if it
562 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
563 */
564 if (efi_enabled(EFI_MEM_ATTR)) {
565 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
566 return;
567 }
568
569 /*
570 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
571 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
572 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
573 * published by the firmware. Even if we find a buggy implementation of
574 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
575 * EFI_PROPERTIES_TABLE, because of the same reason.
576 */
577
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000578 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100579 return;
580
Matt Fleming78ce2482016-04-25 21:06:38 +0100581 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000582 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000583
584 if (!(md->attribute & EFI_MEMORY_RUNTIME))
585 continue;
586
587 if (!(md->attribute & EFI_MEMORY_WB))
588 pf |= _PAGE_PCD;
589
590 if ((md->attribute & EFI_MEMORY_XP) ||
591 (md->type == EFI_RUNTIME_SERVICES_DATA))
592 pf |= _PAGE_NX;
593
594 if (!(md->attribute & EFI_MEMORY_RO) &&
595 (md->type != EFI_RUNTIME_SERVICES_CODE))
596 pf |= _PAGE_RW;
597
Sai Praneeth18141e82017-01-31 13:21:37 +0000598 efi_update_mappings(md, pf);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000599 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100600}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100601
602void __init efi_dump_pagetable(void)
603{
604#ifdef CONFIG_EFI_PGT_DUMP
Sai Praneethac81d3d2017-06-02 13:52:06 +0000605 if (efi_enabled(EFI_OLD_MEMMAP))
606 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
607 else
608 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100609#endif
610}
Matt Fleming994448f2014-03-05 18:15:37 +0000611
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000612#ifdef CONFIG_EFI_MIXED
613extern efi_status_t efi64_thunk(u32, ...);
614
615#define runtime_service32(func) \
616({ \
617 u32 table = (u32)(unsigned long)efi.systab; \
618 u32 *rt, *___f; \
619 \
620 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
621 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
622 *___f; \
623})
624
625/*
626 * Switch to the EFI page tables early so that we can access the 1:1
627 * runtime services mappings which are not mapped in any other page
628 * tables. This function must be called before runtime_service32().
629 *
630 * Also, disable interrupts because the IDT points to 64-bit handlers,
631 * which aren't going to function correctly when we switch to 32-bit.
632 */
633#define efi_thunk(f, ...) \
634({ \
635 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100636 unsigned long __flags; \
637 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000638 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100639 local_irq_save(__flags); \
640 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000641 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100642 __func = runtime_service32(f); \
643 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000644 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100645 arch_efi_call_virt_teardown(); \
646 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000647 \
648 __s; \
649})
650
651efi_status_t efi_thunk_set_virtual_address_map(
652 void *phys_set_virtual_address_map,
653 unsigned long memory_map_size,
654 unsigned long descriptor_size,
655 u32 descriptor_version,
656 efi_memory_desc_t *virtual_map)
657{
658 efi_status_t status;
659 unsigned long flags;
660 u32 func;
661
662 efi_sync_low_kernel_mappings();
663 local_irq_save(flags);
664
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700665 efi_scratch.prev_cr3 = __read_cr3();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000666 write_cr3((unsigned long)efi_scratch.efi_pgt);
667 __flush_tlb_all();
668
669 func = (u32)(unsigned long)phys_set_virtual_address_map;
670 status = efi64_thunk(func, memory_map_size, descriptor_size,
671 descriptor_version, virtual_map);
672
673 write_cr3(efi_scratch.prev_cr3);
674 __flush_tlb_all();
675 local_irq_restore(flags);
676
677 return status;
678}
679
680static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
681{
682 efi_status_t status;
683 u32 phys_tm, phys_tc;
684
685 spin_lock(&rtc_lock);
686
Matt Flemingf6697df2016-11-12 21:04:24 +0000687 phys_tm = virt_to_phys_or_null(tm);
688 phys_tc = virt_to_phys_or_null(tc);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000689
690 status = efi_thunk(get_time, phys_tm, phys_tc);
691
692 spin_unlock(&rtc_lock);
693
694 return status;
695}
696
697static efi_status_t efi_thunk_set_time(efi_time_t *tm)
698{
699 efi_status_t status;
700 u32 phys_tm;
701
702 spin_lock(&rtc_lock);
703
Matt Flemingf6697df2016-11-12 21:04:24 +0000704 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000705
706 status = efi_thunk(set_time, phys_tm);
707
708 spin_unlock(&rtc_lock);
709
710 return status;
711}
712
713static efi_status_t
714efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
715 efi_time_t *tm)
716{
717 efi_status_t status;
718 u32 phys_enabled, phys_pending, phys_tm;
719
720 spin_lock(&rtc_lock);
721
Matt Flemingf6697df2016-11-12 21:04:24 +0000722 phys_enabled = virt_to_phys_or_null(enabled);
723 phys_pending = virt_to_phys_or_null(pending);
724 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000725
726 status = efi_thunk(get_wakeup_time, phys_enabled,
727 phys_pending, phys_tm);
728
729 spin_unlock(&rtc_lock);
730
731 return status;
732}
733
734static efi_status_t
735efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
736{
737 efi_status_t status;
738 u32 phys_tm;
739
740 spin_lock(&rtc_lock);
741
Matt Flemingf6697df2016-11-12 21:04:24 +0000742 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000743
744 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
745
746 spin_unlock(&rtc_lock);
747
748 return status;
749}
750
Matt Flemingf6697df2016-11-12 21:04:24 +0000751static unsigned long efi_name_size(efi_char16_t *name)
752{
753 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
754}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000755
756static efi_status_t
757efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
758 u32 *attr, unsigned long *data_size, void *data)
759{
760 efi_status_t status;
761 u32 phys_name, phys_vendor, phys_attr;
762 u32 phys_data_size, phys_data;
763
Matt Flemingf6697df2016-11-12 21:04:24 +0000764 phys_data_size = virt_to_phys_or_null(data_size);
765 phys_vendor = virt_to_phys_or_null(vendor);
766 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
767 phys_attr = virt_to_phys_or_null(attr);
768 phys_data = virt_to_phys_or_null_size(data, *data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000769
770 status = efi_thunk(get_variable, phys_name, phys_vendor,
771 phys_attr, phys_data_size, phys_data);
772
773 return status;
774}
775
776static efi_status_t
777efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
778 u32 attr, unsigned long data_size, void *data)
779{
780 u32 phys_name, phys_vendor, phys_data;
781 efi_status_t status;
782
Matt Flemingf6697df2016-11-12 21:04:24 +0000783 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
784 phys_vendor = virt_to_phys_or_null(vendor);
785 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000786
787 /* If data_size is > sizeof(u32) we've got problems */
788 status = efi_thunk(set_variable, phys_name, phys_vendor,
789 attr, data_size, phys_data);
790
791 return status;
792}
793
794static efi_status_t
795efi_thunk_get_next_variable(unsigned long *name_size,
796 efi_char16_t *name,
797 efi_guid_t *vendor)
798{
799 efi_status_t status;
800 u32 phys_name_size, phys_name, phys_vendor;
801
Matt Flemingf6697df2016-11-12 21:04:24 +0000802 phys_name_size = virt_to_phys_or_null(name_size);
803 phys_vendor = virt_to_phys_or_null(vendor);
804 phys_name = virt_to_phys_or_null_size(name, *name_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000805
806 status = efi_thunk(get_next_variable, phys_name_size,
807 phys_name, phys_vendor);
808
809 return status;
810}
811
812static efi_status_t
813efi_thunk_get_next_high_mono_count(u32 *count)
814{
815 efi_status_t status;
816 u32 phys_count;
817
Matt Flemingf6697df2016-11-12 21:04:24 +0000818 phys_count = virt_to_phys_or_null(count);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000819 status = efi_thunk(get_next_high_mono_count, phys_count);
820
821 return status;
822}
823
824static void
825efi_thunk_reset_system(int reset_type, efi_status_t status,
826 unsigned long data_size, efi_char16_t *data)
827{
828 u32 phys_data;
829
Matt Flemingf6697df2016-11-12 21:04:24 +0000830 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000831
832 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
833}
834
835static efi_status_t
836efi_thunk_update_capsule(efi_capsule_header_t **capsules,
837 unsigned long count, unsigned long sg_list)
838{
839 /*
840 * To properly support this function we would need to repackage
841 * 'capsules' because the firmware doesn't understand 64-bit
842 * pointers.
843 */
844 return EFI_UNSUPPORTED;
845}
846
847static efi_status_t
848efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
849 u64 *remaining_space,
850 u64 *max_variable_size)
851{
852 efi_status_t status;
853 u32 phys_storage, phys_remaining, phys_max;
854
855 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
856 return EFI_UNSUPPORTED;
857
Matt Flemingf6697df2016-11-12 21:04:24 +0000858 phys_storage = virt_to_phys_or_null(storage_space);
859 phys_remaining = virt_to_phys_or_null(remaining_space);
860 phys_max = virt_to_phys_or_null(max_variable_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000861
Matt Fleming9a110402014-03-16 17:46:46 +0000862 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000863 phys_remaining, phys_max);
864
865 return status;
866}
867
868static efi_status_t
869efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
870 unsigned long count, u64 *max_size,
871 int *reset_type)
872{
873 /*
874 * To properly support this function we would need to repackage
875 * 'capsules' because the firmware doesn't understand 64-bit
876 * pointers.
877 */
878 return EFI_UNSUPPORTED;
879}
880
881void efi_thunk_runtime_setup(void)
882{
883 efi.get_time = efi_thunk_get_time;
884 efi.set_time = efi_thunk_set_time;
885 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
886 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
887 efi.get_variable = efi_thunk_get_variable;
888 efi.get_next_variable = efi_thunk_get_next_variable;
889 efi.set_variable = efi_thunk_set_variable;
890 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
891 efi.reset_system = efi_thunk_reset_system;
892 efi.query_variable_info = efi_thunk_query_variable_info;
893 efi.update_capsule = efi_thunk_update_capsule;
894 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
895}
896#endif /* CONFIG_EFI_MIXED */