blob: 29b267b8cb635fa4d321478758d6ab9cdfeb2d57 [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>
Arnd Bergmann5ab788d2016-05-30 20:57:50 +020028#include <linux/mc146818rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010029#include <linux/efi.h>
Andy Lutomirski116fef62018-01-31 07:56:22 -080030#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010031#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>
Tom Lendacky1379edd2017-10-20 09:30:49 -050036#include <linux/mem_encrypt.h>
Huang, Ying5b836832008-01-30 13:31:19 +010037
38#include <asm/setup.h>
39#include <asm/page.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010040#include <asm/e820/api.h>
Huang, Ying5b836832008-01-30 13:31:19 +010041#include <asm/pgtable.h>
42#include <asm/tlbflush.h>
Huang, Ying5b836832008-01-30 13:31:19 +010043#include <asm/proto.h>
44#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080045#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090046#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010047#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000048#include <asm/time.h>
Matt Fleming67a91082015-11-27 21:09:34 +000049#include <asm/pgalloc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010050
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010051/*
Baoquan Heb1d17762017-04-04 17:02:43 +010052 * We allocate runtime services regions top-down, starting from -4G, i.e.
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010053 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
54 */
Mathias Krause8266e312014-09-21 17:26:54 +020055static u64 efi_va = EFI_VA_START;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010056
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000057struct efi_scratch efi_scratch;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010058
Matthew Garrett9cd2b072011-05-05 15:19:43 -040059static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010060{
61 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +010062
Huang, Yinga2172e22008-01-30 13:33:55 +010063 if (!(__supported_pte_mask & _PAGE_NX))
64 return;
65
Matthew Garrett916f6762011-05-25 09:53:13 -040066 /* Make EFI service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +010067 for_each_efi_memory_desc(md) {
Matthew Garrett916f6762011-05-25 09:53:13 -040068 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
69 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040070 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010071 }
72}
73
Ingo Molnar744937b2015-03-03 07:48:50 +010074pgd_t * __init efi_call_phys_prolog(void)
Huang, Ying5b836832008-01-30 13:31:19 +010075{
Baoquan He94133e42017-05-26 12:36:50 +010076 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
77 pgd_t *save_pgd, *pgd_k, *pgd_efi;
78 p4d_t *p4d, *p4d_k, *p4d_efi;
79 pud_t *pud;
Ingo Molnar744937b2015-03-03 07:48:50 +010080
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060081 int pgd;
Baoquan He94133e42017-05-26 12:36:50 +010082 int n_pgds, i, j;
Huang, Ying5b836832008-01-30 13:31:19 +010083
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000084 if (!efi_enabled(EFI_OLD_MEMMAP)) {
Andy Lutomirski6c690ee2017-06-12 10:26:14 -070085 save_pgd = (pgd_t *)__read_cr3();
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000086 write_cr3((unsigned long)efi_scratch.efi_pgt);
87 goto out;
88 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010089
Matthew Garrett9cd2b072011-05-05 15:19:43 -040090 early_code_mapping_set_exec(1);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060091
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
Markus Elfring20ebc152016-08-25 11:34:03 +020093 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060094
Baoquan He94133e42017-05-26 12:36:50 +010095 /*
96 * Build 1:1 identity mapping for efi=old_map usage. Note that
97 * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while
98 * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical
99 * address X, the pud_index(X) != pud_index(__va(X)), we can only copy
100 * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping.
101 * This means here we can only reuse the PMD tables of the direct mapping.
102 */
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600103 for (pgd = 0; pgd < n_pgds; pgd++) {
Baoquan He94133e42017-05-26 12:36:50 +0100104 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
105 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
106 pgd_efi = pgd_offset_k(addr_pgd);
107 save_pgd[pgd] = *pgd_efi;
108
109 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
110 if (!p4d) {
111 pr_err("Failed to allocate p4d table!\n");
112 goto out;
113 }
114
115 for (i = 0; i < PTRS_PER_P4D; i++) {
116 addr_p4d = addr_pgd + i * P4D_SIZE;
117 p4d_efi = p4d + p4d_index(addr_p4d);
118
119 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
120 if (!pud) {
121 pr_err("Failed to allocate pud table!\n");
122 goto out;
123 }
124
125 for (j = 0; j < PTRS_PER_PUD; j++) {
126 addr_pud = addr_p4d + j * PUD_SIZE;
127
128 if (addr_pud > (max_pfn << PAGE_SHIFT))
129 break;
130
131 vaddr = (unsigned long)__va(addr_pud);
132
133 pgd_k = pgd_offset_k(vaddr);
134 p4d_k = p4d_offset(pgd_k, vaddr);
135 pud[j] = *pud_offset(p4d_k, vaddr);
136 }
137 }
Jiri Kosinade53c372018-01-05 22:35:41 +0100138 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600139 }
Jiri Kosinade53c372018-01-05 22:35:41 +0100140
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000141out:
Huang, Ying5b836832008-01-30 13:31:19 +0100142 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +0100143
144 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +0100145}
146
Ingo Molnar744937b2015-03-03 07:48:50 +0100147void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100148{
149 /*
150 * After the lock is released, the original page table is restored.
151 */
Baoquan He94133e42017-05-26 12:36:50 +0100152 int pgd_idx, i;
Ingo Molnar744937b2015-03-03 07:48:50 +0100153 int nr_pgds;
Baoquan He94133e42017-05-26 12:36:50 +0100154 pgd_t *pgd;
155 p4d_t *p4d;
156 pud_t *pud;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100157
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000158 if (!efi_enabled(EFI_OLD_MEMMAP)) {
159 write_cr3((unsigned long)save_pgd);
160 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100161 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000162 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100163
Ingo Molnar744937b2015-03-03 07:48:50 +0100164 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
165
Baoquan He94133e42017-05-26 12:36:50 +0100166 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
167 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
Ingo Molnar744937b2015-03-03 07:48:50 +0100168 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
169
Baoquan He94133e42017-05-26 12:36:50 +0100170 if (!(pgd_val(*pgd) & _PAGE_PRESENT))
171 continue;
172
173 for (i = 0; i < PTRS_PER_P4D; i++) {
174 p4d = p4d_offset(pgd,
175 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
176
177 if (!(p4d_val(*p4d) & _PAGE_PRESENT))
178 continue;
179
180 pud = (pud_t *)p4d_page_vaddr(*p4d);
181 pud_free(&init_mm, pud);
182 }
183
184 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
185 p4d_free(&init_mm, p4d);
186 }
187
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600188 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100189
Huang, Ying5b836832008-01-30 13:31:19 +0100190 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400191 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100192}
Keith Packarde1ad7832011-12-11 16:12:42 -0800193
Sai Praneeth3ede3412018-03-12 09:43:54 +0000194EXPORT_SYMBOL_GPL(efi_mm);
Matt Fleming67a91082015-11-27 21:09:34 +0000195
196/*
197 * We need our own copy of the higher levels of the page tables
198 * because we want to avoid inserting EFI region mappings (EFI_VA_END
199 * to EFI_VA_START) into the standard kernel page tables. Everything
200 * else can be shared, see efi_sync_low_kernel_mappings().
Dave Hansend9e9a642017-12-04 15:07:39 +0100201 *
202 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
203 * allocation.
Matt Fleming67a91082015-11-27 21:09:34 +0000204 */
205int __init efi_alloc_page_tables(void)
206{
Sai Praneeth3ede3412018-03-12 09:43:54 +0000207 pgd_t *pgd, *efi_pgd;
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300208 p4d_t *p4d;
Matt Fleming67a91082015-11-27 21:09:34 +0000209 pud_t *pud;
210 gfp_t gfp_mask;
211
212 if (efi_enabled(EFI_OLD_MEMMAP))
213 return 0;
214
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800215 gfp_mask = GFP_KERNEL | __GFP_ZERO;
Dave Hansend9e9a642017-12-04 15:07:39 +0100216 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
Matt Fleming67a91082015-11-27 21:09:34 +0000217 if (!efi_pgd)
218 return -ENOMEM;
219
220 pgd = efi_pgd + pgd_index(EFI_VA_END);
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300221 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
222 if (!p4d) {
Matt Fleming67a91082015-11-27 21:09:34 +0000223 free_page((unsigned long)efi_pgd);
224 return -ENOMEM;
225 }
226
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300227 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
228 if (!pud) {
229 if (CONFIG_PGTABLE_LEVELS > 4)
230 free_page((unsigned long) pgd_page_vaddr(*pgd));
231 free_page((unsigned long)efi_pgd);
232 return -ENOMEM;
233 }
Matt Fleming67a91082015-11-27 21:09:34 +0000234
Sai Praneeth3ede3412018-03-12 09:43:54 +0000235 efi_mm.pgd = efi_pgd;
Sai Praneeth7e904a92018-03-12 08:44:56 +0000236 mm_init_cpumask(&efi_mm);
237 init_new_context(NULL, &efi_mm);
238
Matt Fleming67a91082015-11-27 21:09:34 +0000239 return 0;
240}
241
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100242/*
243 * Add low kernel mappings for passing arguments to EFI functions.
244 */
245void efi_sync_low_kernel_mappings(void)
246{
Matt Fleming67a91082015-11-27 21:09:34 +0000247 unsigned num_entries;
248 pgd_t *pgd_k, *pgd_efi;
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300249 p4d_t *p4d_k, *p4d_efi;
Matt Fleming67a91082015-11-27 21:09:34 +0000250 pud_t *pud_k, *pud_efi;
Sai Praneeth3ede3412018-03-12 09:43:54 +0000251 pgd_t *efi_pgd = efi_mm.pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100252
253 if (efi_enabled(EFI_OLD_MEMMAP))
254 return;
255
Matt Fleming67a91082015-11-27 21:09:34 +0000256 /*
257 * We can share all PGD entries apart from the one entry that
258 * covers the EFI runtime mapping space.
259 *
260 * Make sure the EFI runtime region mappings are guaranteed to
261 * only span a single PGD entry and that the entry also maps
262 * other important kernel regions.
263 */
264 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
265 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
266 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100267
Matt Fleming67a91082015-11-27 21:09:34 +0000268 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
269 pgd_k = pgd_offset_k(PAGE_OFFSET);
270
271 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
272 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
273
274 /*
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300275 * As with PGDs, we share all P4D entries apart from the one entry
276 * that covers the EFI runtime mapping space.
277 */
278 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
279 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
280
281 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
282 pgd_k = pgd_offset_k(EFI_VA_END);
283 p4d_efi = p4d_offset(pgd_efi, 0);
284 p4d_k = p4d_offset(pgd_k, 0);
285
286 num_entries = p4d_index(EFI_VA_END);
287 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
288
289 /*
Matt Fleming67a91082015-11-27 21:09:34 +0000290 * We share all the PUD entries apart from those that map the
291 * EFI regions. Copy around them.
292 */
293 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
294 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
295
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300296 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
297 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300298 pud_efi = pud_offset(p4d_efi, 0);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300299 pud_k = pud_offset(p4d_k, 0);
Matt Fleming67a91082015-11-27 21:09:34 +0000300
301 num_entries = pud_index(EFI_VA_END);
302 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
303
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300304 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300305 pud_k = pud_offset(p4d_k, EFI_VA_START);
Matt Fleming67a91082015-11-27 21:09:34 +0000306
307 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
308 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100309}
310
Matt Flemingf6697df2016-11-12 21:04:24 +0000311/*
312 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
313 */
314static inline phys_addr_t
315virt_to_phys_or_null_size(void *va, unsigned long size)
316{
317 bool bad_size;
318
319 if (!va)
320 return 0;
321
322 if (virt_addr_valid(va))
323 return virt_to_phys(va);
324
325 /*
326 * A fully aligned variable on the stack is guaranteed not to
327 * cross a page bounary. Try to catch strings on the stack by
328 * checking that 'size' is a power of two.
329 */
330 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
331
332 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
333
334 return slow_virt_to_phys(va);
335}
336
337#define virt_to_phys_or_null(addr) \
338 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
339
Mathias Krause4e78eb052014-09-07 19:42:17 +0200340int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100341{
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500342 unsigned long pfn, text, pf;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000343 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000344 unsigned npages;
Sai Praneeth3ede3412018-03-12 09:43:54 +0000345 pgd_t *pgd = efi_mm.pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100346
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100347 if (efi_enabled(EFI_OLD_MEMMAP))
348 return 0;
349
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500350 /*
351 * Since the PGD is encrypted, set the encryption mask so that when
352 * this value is loaded into cr3 the PGD will be decrypted during
353 * the pagetable walk.
354 */
Sai Praneeth3ede3412018-03-12 09:43:54 +0000355 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(pgd);
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100356
357 /*
358 * It can happen that the physical address of new_memmap lands in memory
359 * which is not mapped in the EFI page table. Therefore we need to go
360 * and ident-map those pages containing the map before calling
361 * phys_efi_set_virtual_address_map().
362 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000363 pfn = pa_memmap >> PAGE_SHIFT;
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500364 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
365 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100366 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
367 return 1;
368 }
369
370 efi_scratch.use_pgd = true;
371
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000372 /*
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000373 * Certain firmware versions are way too sentimential and still believe
374 * they are exclusive and unquestionable owners of the first physical page,
375 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
376 * (but then write-access it later during SetVirtualAddressMap()).
377 *
378 * Create a 1:1 mapping for this page, to avoid triple faults during early
379 * boot with such firmware. We are free to hand this page to the BIOS,
380 * as trim_bios_range() will reserve the first page and isolate it away
381 * from memory allocators anyway.
382 */
Tom Lendacky1379edd2017-10-20 09:30:49 -0500383 pf = _PAGE_RW;
384 if (sev_active())
385 pf |= _PAGE_ENC;
386
387 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000388 pr_err("Failed to create 1:1 mapping for the first page!\n");
389 return 1;
390 }
391
392 /*
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000393 * When making calls to the firmware everything needs to be 1:1
394 * mapped and addressable with 32-bit pointers. Map the kernel
395 * text and allocate a new stack because we can't rely on the
396 * stack pointer being < 4GB.
397 */
Matt Fleming12976672016-09-19 13:09:09 +0100398 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000399 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000400
401 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
402 if (!page)
403 panic("Unable to allocate EFI runtime stack < 4GB\n");
404
405 efi_scratch.phys_stack = virt_to_phys(page_address(page));
406 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
407
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000408 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000409 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000410 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000411
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500412 pf = _PAGE_RW | _PAGE_ENC;
413 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000414 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000415 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000416 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100417
418 return 0;
419}
420
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100421static void __init __map_region(efi_memory_desc_t *md, u64 va)
422{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000423 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000424 unsigned long pfn;
Sai Praneeth3ede3412018-03-12 09:43:54 +0000425 pgd_t *pgd = efi_mm.pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100426
427 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000428 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100429
Tom Lendacky1379edd2017-10-20 09:30:49 -0500430 if (sev_active())
431 flags |= _PAGE_ENC;
432
Matt Flemingedc3b912015-11-27 21:09:31 +0000433 pfn = md->phys_addr >> PAGE_SHIFT;
434 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100435 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
436 md->phys_addr, va);
437}
438
439void __init efi_map_region(efi_memory_desc_t *md)
440{
441 unsigned long size = md->num_pages << PAGE_SHIFT;
442 u64 pa = md->phys_addr;
443
444 if (efi_enabled(EFI_OLD_MEMMAP))
445 return old_map_region(md);
446
447 /*
448 * Make sure the 1:1 mappings are present as a catch-all for b0rked
449 * firmware which doesn't update all internal pointers after switching
450 * to virtual mode and would otherwise crap on us.
451 */
452 __map_region(md, md->phys_addr);
453
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000454 /*
455 * Enforce the 1:1 mapping as the default virtual address when
456 * booting in EFI mixed mode, because even though we may be
457 * running a 64-bit kernel, the firmware may only be 32-bit.
458 */
459 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
460 md->virt_addr = md->phys_addr;
461 return;
462 }
463
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100464 efi_va -= size;
465
466 /* Is PA 2M-aligned? */
467 if (!(pa & (PMD_SIZE - 1))) {
468 efi_va &= PMD_MASK;
469 } else {
470 u64 pa_offset = pa & (PMD_SIZE - 1);
471 u64 prev_va = efi_va;
472
473 /* get us the same offset within this 2M page */
474 efi_va = (efi_va & PMD_MASK) + pa_offset;
475
476 if (efi_va > prev_va)
477 efi_va -= PMD_SIZE;
478 }
479
480 if (efi_va < EFI_VA_END) {
481 pr_warn(FW_WARN "VA address range overflow!\n");
482 return;
483 }
484
485 /* Do the VA map */
486 __map_region(md, efi_va);
487 md->virt_addr = efi_va;
488}
489
Dave Young3b266492013-12-20 18:02:14 +0800490/*
491 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
492 * md->virt_addr is the original virtual address which had been mapped in kexec
493 * 1st kernel.
494 */
495void __init efi_map_region_fixed(efi_memory_desc_t *md)
496{
Alex Thorlton0513fe12016-08-05 18:59:35 -0500497 __map_region(md, md->phys_addr);
Dave Young3b266492013-12-20 18:02:14 +0800498 __map_region(md, md->virt_addr);
499}
500
Keith Packarde1ad7832011-12-11 16:12:42 -0800501void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100502 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800503{
504 unsigned long last_map_pfn;
505
506 if (type == EFI_MEMORY_MAPPED_IO)
507 return ioremap(phys_addr, size);
508
509 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
510 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
511 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100512 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800513 }
514
Matt Fleming3e8fa262012-10-19 13:25:46 +0100515 if (!(attribute & EFI_MEMORY_WB))
516 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
517
Keith Packarde1ad7832011-12-11 16:12:42 -0800518 return (void __iomem *)__va(phys_addr);
519}
Dave Young1fec0532013-12-20 18:02:19 +0800520
521void __init parse_efi_setup(u64 phys_addr, u32 data_len)
522{
523 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800524}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100525
Sai Praneeth18141e82017-01-31 13:21:37 +0000526static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100527{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000528 unsigned long pfn;
Sai Praneeth3ede3412018-03-12 09:43:54 +0000529 pgd_t *pgd = efi_mm.pgd;
Sai Praneeth18141e82017-01-31 13:21:37 +0000530 int err1, err2;
531
532 /* Update the 1:1 mapping */
533 pfn = md->phys_addr >> PAGE_SHIFT;
534 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
535 if (err1) {
536 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
537 md->phys_addr, md->virt_addr);
538 }
539
540 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
541 if (err2) {
542 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
543 md->phys_addr, md->virt_addr);
544 }
545
546 return err1 || err2;
547}
548
549static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
550{
551 unsigned long pf = 0;
552
553 if (md->attribute & EFI_MEMORY_XP)
554 pf |= _PAGE_NX;
555
556 if (!(md->attribute & EFI_MEMORY_RO))
557 pf |= _PAGE_RW;
558
Tom Lendacky1379edd2017-10-20 09:30:49 -0500559 if (sev_active())
560 pf |= _PAGE_ENC;
561
Sai Praneeth18141e82017-01-31 13:21:37 +0000562 return efi_update_mappings(md, pf);
563}
564
565void __init efi_runtime_update_mappings(void)
566{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000567 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000568
569 if (efi_enabled(EFI_OLD_MEMMAP)) {
570 if (__supported_pte_mask & _PAGE_NX)
571 runtime_code_page_mkexec();
572 return;
573 }
574
Sai Praneeth18141e82017-01-31 13:21:37 +0000575 /*
576 * Use the EFI Memory Attribute Table for mapping permissions if it
577 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
578 */
579 if (efi_enabled(EFI_MEM_ATTR)) {
580 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
581 return;
582 }
583
584 /*
585 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
586 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
587 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
588 * published by the firmware. Even if we find a buggy implementation of
589 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
590 * EFI_PROPERTIES_TABLE, because of the same reason.
591 */
592
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000593 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100594 return;
595
Matt Fleming78ce2482016-04-25 21:06:38 +0100596 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000597 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000598
599 if (!(md->attribute & EFI_MEMORY_RUNTIME))
600 continue;
601
602 if (!(md->attribute & EFI_MEMORY_WB))
603 pf |= _PAGE_PCD;
604
605 if ((md->attribute & EFI_MEMORY_XP) ||
606 (md->type == EFI_RUNTIME_SERVICES_DATA))
607 pf |= _PAGE_NX;
608
609 if (!(md->attribute & EFI_MEMORY_RO) &&
610 (md->type != EFI_RUNTIME_SERVICES_CODE))
611 pf |= _PAGE_RW;
612
Tom Lendacky1379edd2017-10-20 09:30:49 -0500613 if (sev_active())
614 pf |= _PAGE_ENC;
615
Sai Praneeth18141e82017-01-31 13:21:37 +0000616 efi_update_mappings(md, pf);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000617 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100618}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100619
620void __init efi_dump_pagetable(void)
621{
622#ifdef CONFIG_EFI_PGT_DUMP
Sai Praneethac81d3d2017-06-02 13:52:06 +0000623 if (efi_enabled(EFI_OLD_MEMMAP))
624 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
625 else
Sai Praneeth3ede3412018-03-12 09:43:54 +0000626 ptdump_walk_pgd_level(NULL, efi_mm.pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100627#endif
628}
Matt Fleming994448f2014-03-05 18:15:37 +0000629
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000630#ifdef CONFIG_EFI_MIXED
631extern efi_status_t efi64_thunk(u32, ...);
632
633#define runtime_service32(func) \
634({ \
635 u32 table = (u32)(unsigned long)efi.systab; \
636 u32 *rt, *___f; \
637 \
638 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
639 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
640 *___f; \
641})
642
643/*
644 * Switch to the EFI page tables early so that we can access the 1:1
645 * runtime services mappings which are not mapped in any other page
646 * tables. This function must be called before runtime_service32().
647 *
648 * Also, disable interrupts because the IDT points to 64-bit handlers,
649 * which aren't going to function correctly when we switch to 32-bit.
650 */
651#define efi_thunk(f, ...) \
652({ \
653 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100654 unsigned long __flags; \
655 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000656 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100657 local_irq_save(__flags); \
658 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000659 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100660 __func = runtime_service32(f); \
661 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000662 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100663 arch_efi_call_virt_teardown(); \
664 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000665 \
666 __s; \
667})
668
669efi_status_t efi_thunk_set_virtual_address_map(
670 void *phys_set_virtual_address_map,
671 unsigned long memory_map_size,
672 unsigned long descriptor_size,
673 u32 descriptor_version,
674 efi_memory_desc_t *virtual_map)
675{
676 efi_status_t status;
677 unsigned long flags;
678 u32 func;
679
680 efi_sync_low_kernel_mappings();
681 local_irq_save(flags);
682
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700683 efi_scratch.prev_cr3 = __read_cr3();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000684 write_cr3((unsigned long)efi_scratch.efi_pgt);
685 __flush_tlb_all();
686
687 func = (u32)(unsigned long)phys_set_virtual_address_map;
688 status = efi64_thunk(func, memory_map_size, descriptor_size,
689 descriptor_version, virtual_map);
690
691 write_cr3(efi_scratch.prev_cr3);
692 __flush_tlb_all();
693 local_irq_restore(flags);
694
695 return status;
696}
697
698static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
699{
700 efi_status_t status;
701 u32 phys_tm, phys_tc;
702
703 spin_lock(&rtc_lock);
704
Matt Flemingf6697df2016-11-12 21:04:24 +0000705 phys_tm = virt_to_phys_or_null(tm);
706 phys_tc = virt_to_phys_or_null(tc);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000707
708 status = efi_thunk(get_time, phys_tm, phys_tc);
709
710 spin_unlock(&rtc_lock);
711
712 return status;
713}
714
715static efi_status_t efi_thunk_set_time(efi_time_t *tm)
716{
717 efi_status_t status;
718 u32 phys_tm;
719
720 spin_lock(&rtc_lock);
721
Matt Flemingf6697df2016-11-12 21:04:24 +0000722 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000723
724 status = efi_thunk(set_time, phys_tm);
725
726 spin_unlock(&rtc_lock);
727
728 return status;
729}
730
731static efi_status_t
732efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
733 efi_time_t *tm)
734{
735 efi_status_t status;
736 u32 phys_enabled, phys_pending, phys_tm;
737
738 spin_lock(&rtc_lock);
739
Matt Flemingf6697df2016-11-12 21:04:24 +0000740 phys_enabled = virt_to_phys_or_null(enabled);
741 phys_pending = virt_to_phys_or_null(pending);
742 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000743
744 status = efi_thunk(get_wakeup_time, phys_enabled,
745 phys_pending, phys_tm);
746
747 spin_unlock(&rtc_lock);
748
749 return status;
750}
751
752static efi_status_t
753efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
754{
755 efi_status_t status;
756 u32 phys_tm;
757
758 spin_lock(&rtc_lock);
759
Matt Flemingf6697df2016-11-12 21:04:24 +0000760 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000761
762 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
763
764 spin_unlock(&rtc_lock);
765
766 return status;
767}
768
Matt Flemingf6697df2016-11-12 21:04:24 +0000769static unsigned long efi_name_size(efi_char16_t *name)
770{
771 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
772}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000773
774static efi_status_t
775efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
776 u32 *attr, unsigned long *data_size, void *data)
777{
778 efi_status_t status;
779 u32 phys_name, phys_vendor, phys_attr;
780 u32 phys_data_size, phys_data;
781
Matt Flemingf6697df2016-11-12 21:04:24 +0000782 phys_data_size = virt_to_phys_or_null(data_size);
783 phys_vendor = virt_to_phys_or_null(vendor);
784 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
785 phys_attr = virt_to_phys_or_null(attr);
786 phys_data = virt_to_phys_or_null_size(data, *data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000787
788 status = efi_thunk(get_variable, phys_name, phys_vendor,
789 phys_attr, phys_data_size, phys_data);
790
791 return status;
792}
793
794static efi_status_t
795efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
796 u32 attr, unsigned long data_size, void *data)
797{
798 u32 phys_name, phys_vendor, phys_data;
799 efi_status_t status;
800
Matt Flemingf6697df2016-11-12 21:04:24 +0000801 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
802 phys_vendor = virt_to_phys_or_null(vendor);
803 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000804
805 /* If data_size is > sizeof(u32) we've got problems */
806 status = efi_thunk(set_variable, phys_name, phys_vendor,
807 attr, data_size, phys_data);
808
809 return status;
810}
811
812static efi_status_t
813efi_thunk_get_next_variable(unsigned long *name_size,
814 efi_char16_t *name,
815 efi_guid_t *vendor)
816{
817 efi_status_t status;
818 u32 phys_name_size, phys_name, phys_vendor;
819
Matt Flemingf6697df2016-11-12 21:04:24 +0000820 phys_name_size = virt_to_phys_or_null(name_size);
821 phys_vendor = virt_to_phys_or_null(vendor);
822 phys_name = virt_to_phys_or_null_size(name, *name_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000823
824 status = efi_thunk(get_next_variable, phys_name_size,
825 phys_name, phys_vendor);
826
827 return status;
828}
829
830static efi_status_t
831efi_thunk_get_next_high_mono_count(u32 *count)
832{
833 efi_status_t status;
834 u32 phys_count;
835
Matt Flemingf6697df2016-11-12 21:04:24 +0000836 phys_count = virt_to_phys_or_null(count);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000837 status = efi_thunk(get_next_high_mono_count, phys_count);
838
839 return status;
840}
841
842static void
843efi_thunk_reset_system(int reset_type, efi_status_t status,
844 unsigned long data_size, efi_char16_t *data)
845{
846 u32 phys_data;
847
Matt Flemingf6697df2016-11-12 21:04:24 +0000848 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000849
850 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
851}
852
853static efi_status_t
854efi_thunk_update_capsule(efi_capsule_header_t **capsules,
855 unsigned long count, unsigned long sg_list)
856{
857 /*
858 * To properly support this function we would need to repackage
859 * 'capsules' because the firmware doesn't understand 64-bit
860 * pointers.
861 */
862 return EFI_UNSUPPORTED;
863}
864
865static efi_status_t
866efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
867 u64 *remaining_space,
868 u64 *max_variable_size)
869{
870 efi_status_t status;
871 u32 phys_storage, phys_remaining, phys_max;
872
873 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
874 return EFI_UNSUPPORTED;
875
Matt Flemingf6697df2016-11-12 21:04:24 +0000876 phys_storage = virt_to_phys_or_null(storage_space);
877 phys_remaining = virt_to_phys_or_null(remaining_space);
878 phys_max = virt_to_phys_or_null(max_variable_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000879
Matt Fleming9a110402014-03-16 17:46:46 +0000880 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000881 phys_remaining, phys_max);
882
883 return status;
884}
885
886static efi_status_t
887efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
888 unsigned long count, u64 *max_size,
889 int *reset_type)
890{
891 /*
892 * To properly support this function we would need to repackage
893 * 'capsules' because the firmware doesn't understand 64-bit
894 * pointers.
895 */
896 return EFI_UNSUPPORTED;
897}
898
899void efi_thunk_runtime_setup(void)
900{
901 efi.get_time = efi_thunk_get_time;
902 efi.set_time = efi_thunk_set_time;
903 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
904 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
905 efi.get_variable = efi_thunk_get_variable;
906 efi.get_next_variable = efi_thunk_get_next_variable;
907 efi.set_variable = efi_thunk_set_variable;
908 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
909 efi.reset_system = efi_thunk_reset_system;
910 efi.query_variable_info = efi_thunk_query_variable_info;
911 efi.update_capsule = efi_thunk_update_capsule;
912 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
913}
914#endif /* CONFIG_EFI_MIXED */