blob: d87ac96e37ede3ea93dabb67d99538fdadc03de0 [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>
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 }
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600138 }
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000139out:
Huang, Ying5b836832008-01-30 13:31:19 +0100140 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +0100141
142 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +0100143}
144
Ingo Molnar744937b2015-03-03 07:48:50 +0100145void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100146{
147 /*
148 * After the lock is released, the original page table is restored.
149 */
Baoquan He94133e42017-05-26 12:36:50 +0100150 int pgd_idx, i;
Ingo Molnar744937b2015-03-03 07:48:50 +0100151 int nr_pgds;
Baoquan He94133e42017-05-26 12:36:50 +0100152 pgd_t *pgd;
153 p4d_t *p4d;
154 pud_t *pud;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100155
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000156 if (!efi_enabled(EFI_OLD_MEMMAP)) {
157 write_cr3((unsigned long)save_pgd);
158 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100159 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000160 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100161
Ingo Molnar744937b2015-03-03 07:48:50 +0100162 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
163
Baoquan He94133e42017-05-26 12:36:50 +0100164 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
165 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
Ingo Molnar744937b2015-03-03 07:48:50 +0100166 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
167
Baoquan He94133e42017-05-26 12:36:50 +0100168 if (!(pgd_val(*pgd) & _PAGE_PRESENT))
169 continue;
170
171 for (i = 0; i < PTRS_PER_P4D; i++) {
172 p4d = p4d_offset(pgd,
173 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
174
175 if (!(p4d_val(*p4d) & _PAGE_PRESENT))
176 continue;
177
178 pud = (pud_t *)p4d_page_vaddr(*p4d);
179 pud_free(&init_mm, pud);
180 }
181
182 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
183 p4d_free(&init_mm, p4d);
184 }
185
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600186 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100187
Huang, Ying5b836832008-01-30 13:31:19 +0100188 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400189 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100190}
Keith Packarde1ad7832011-12-11 16:12:42 -0800191
Matt Fleming67a91082015-11-27 21:09:34 +0000192static pgd_t *efi_pgd;
193
194/*
195 * We need our own copy of the higher levels of the page tables
196 * because we want to avoid inserting EFI region mappings (EFI_VA_END
197 * to EFI_VA_START) into the standard kernel page tables. Everything
198 * else can be shared, see efi_sync_low_kernel_mappings().
Dave Hansend9e9a642017-12-04 15:07:39 +0100199 *
200 * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the
201 * allocation.
Matt Fleming67a91082015-11-27 21:09:34 +0000202 */
203int __init efi_alloc_page_tables(void)
204{
205 pgd_t *pgd;
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300206 p4d_t *p4d;
Matt Fleming67a91082015-11-27 21:09:34 +0000207 pud_t *pud;
208 gfp_t gfp_mask;
209
210 if (efi_enabled(EFI_OLD_MEMMAP))
211 return 0;
212
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -0800213 gfp_mask = GFP_KERNEL | __GFP_ZERO;
Dave Hansend9e9a642017-12-04 15:07:39 +0100214 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
Matt Fleming67a91082015-11-27 21:09:34 +0000215 if (!efi_pgd)
216 return -ENOMEM;
217
218 pgd = efi_pgd + pgd_index(EFI_VA_END);
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300219 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
220 if (!p4d) {
Matt Fleming67a91082015-11-27 21:09:34 +0000221 free_page((unsigned long)efi_pgd);
222 return -ENOMEM;
223 }
224
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300225 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
226 if (!pud) {
227 if (CONFIG_PGTABLE_LEVELS > 4)
228 free_page((unsigned long) pgd_page_vaddr(*pgd));
229 free_page((unsigned long)efi_pgd);
230 return -ENOMEM;
231 }
Matt Fleming67a91082015-11-27 21:09:34 +0000232
233 return 0;
234}
235
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100236/*
237 * Add low kernel mappings for passing arguments to EFI functions.
238 */
239void efi_sync_low_kernel_mappings(void)
240{
Matt Fleming67a91082015-11-27 21:09:34 +0000241 unsigned num_entries;
242 pgd_t *pgd_k, *pgd_efi;
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300243 p4d_t *p4d_k, *p4d_efi;
Matt Fleming67a91082015-11-27 21:09:34 +0000244 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100245
246 if (efi_enabled(EFI_OLD_MEMMAP))
247 return;
248
Matt Fleming67a91082015-11-27 21:09:34 +0000249 /*
250 * We can share all PGD entries apart from the one entry that
251 * covers the EFI runtime mapping space.
252 *
253 * Make sure the EFI runtime region mappings are guaranteed to
254 * only span a single PGD entry and that the entry also maps
255 * other important kernel regions.
256 */
257 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
258 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
259 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100260
Matt Fleming67a91082015-11-27 21:09:34 +0000261 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
262 pgd_k = pgd_offset_k(PAGE_OFFSET);
263
264 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
265 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
266
267 /*
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300268 * As with PGDs, we share all P4D entries apart from the one entry
269 * that covers the EFI runtime mapping space.
270 */
271 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
272 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
273
274 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
275 pgd_k = pgd_offset_k(EFI_VA_END);
276 p4d_efi = p4d_offset(pgd_efi, 0);
277 p4d_k = p4d_offset(pgd_k, 0);
278
279 num_entries = p4d_index(EFI_VA_END);
280 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
281
282 /*
Matt Fleming67a91082015-11-27 21:09:34 +0000283 * We share all the PUD entries apart from those that map the
284 * EFI regions. Copy around them.
285 */
286 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
287 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
288
Kirill A. Shutemove9813162017-03-17 21:55:11 +0300289 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
290 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300291 pud_efi = pud_offset(p4d_efi, 0);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300292 pud_k = pud_offset(p4d_k, 0);
Matt Fleming67a91082015-11-27 21:09:34 +0000293
294 num_entries = pud_index(EFI_VA_END);
295 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
296
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300297 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
Kirill A. Shutemove0c4f672017-03-13 17:33:05 +0300298 pud_k = pud_offset(p4d_k, EFI_VA_START);
Matt Fleming67a91082015-11-27 21:09:34 +0000299
300 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
301 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100302}
303
Matt Flemingf6697df2016-11-12 21:04:24 +0000304/*
305 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
306 */
307static inline phys_addr_t
308virt_to_phys_or_null_size(void *va, unsigned long size)
309{
310 bool bad_size;
311
312 if (!va)
313 return 0;
314
315 if (virt_addr_valid(va))
316 return virt_to_phys(va);
317
318 /*
319 * A fully aligned variable on the stack is guaranteed not to
320 * cross a page bounary. Try to catch strings on the stack by
321 * checking that 'size' is a power of two.
322 */
323 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
324
325 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
326
327 return slow_virt_to_phys(va);
328}
329
330#define virt_to_phys_or_null(addr) \
331 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
332
Mathias Krause4e78eb052014-09-07 19:42:17 +0200333int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100334{
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500335 unsigned long pfn, text, pf;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000336 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000337 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100338 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100339
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100340 if (efi_enabled(EFI_OLD_MEMMAP))
341 return 0;
342
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500343 /*
344 * Since the PGD is encrypted, set the encryption mask so that when
345 * this value is loaded into cr3 the PGD will be decrypted during
346 * the pagetable walk.
347 */
348 efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd);
Matt Fleming67a91082015-11-27 21:09:34 +0000349 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100350
351 /*
352 * It can happen that the physical address of new_memmap lands in memory
353 * which is not mapped in the EFI page table. Therefore we need to go
354 * and ident-map those pages containing the map before calling
355 * phys_efi_set_virtual_address_map().
356 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000357 pfn = pa_memmap >> PAGE_SHIFT;
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500358 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
359 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100360 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
361 return 1;
362 }
363
364 efi_scratch.use_pgd = true;
365
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000366 /*
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000367 * Certain firmware versions are way too sentimential and still believe
368 * they are exclusive and unquestionable owners of the first physical page,
369 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
370 * (but then write-access it later during SetVirtualAddressMap()).
371 *
372 * Create a 1:1 mapping for this page, to avoid triple faults during early
373 * boot with such firmware. We are free to hand this page to the BIOS,
374 * as trim_bios_range() will reserve the first page and isolate it away
375 * from memory allocators anyway.
376 */
Tom Lendacky1379edd2017-10-20 09:30:49 -0500377 pf = _PAGE_RW;
378 if (sev_active())
379 pf |= _PAGE_ENC;
380
381 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000382 pr_err("Failed to create 1:1 mapping for the first page!\n");
383 return 1;
384 }
385
386 /*
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000387 * When making calls to the firmware everything needs to be 1:1
388 * mapped and addressable with 32-bit pointers. Map the kernel
389 * text and allocate a new stack because we can't rely on the
390 * stack pointer being < 4GB.
391 */
Matt Fleming12976672016-09-19 13:09:09 +0100392 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000393 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000394
395 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
396 if (!page)
397 panic("Unable to allocate EFI runtime stack < 4GB\n");
398
399 efi_scratch.phys_stack = virt_to_phys(page_address(page));
400 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
401
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000402 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000403 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000404 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000405
Tom Lendacky38eeccc2017-07-17 16:10:15 -0500406 pf = _PAGE_RW | _PAGE_ENC;
407 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000408 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000409 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000410 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100411
412 return 0;
413}
414
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100415static void __init __map_region(efi_memory_desc_t *md, u64 va)
416{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000417 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000418 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000419 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100420
421 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000422 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100423
Tom Lendacky1379edd2017-10-20 09:30:49 -0500424 if (sev_active())
425 flags |= _PAGE_ENC;
426
Matt Flemingedc3b912015-11-27 21:09:31 +0000427 pfn = md->phys_addr >> PAGE_SHIFT;
428 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100429 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
430 md->phys_addr, va);
431}
432
433void __init efi_map_region(efi_memory_desc_t *md)
434{
435 unsigned long size = md->num_pages << PAGE_SHIFT;
436 u64 pa = md->phys_addr;
437
438 if (efi_enabled(EFI_OLD_MEMMAP))
439 return old_map_region(md);
440
441 /*
442 * Make sure the 1:1 mappings are present as a catch-all for b0rked
443 * firmware which doesn't update all internal pointers after switching
444 * to virtual mode and would otherwise crap on us.
445 */
446 __map_region(md, md->phys_addr);
447
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000448 /*
449 * Enforce the 1:1 mapping as the default virtual address when
450 * booting in EFI mixed mode, because even though we may be
451 * running a 64-bit kernel, the firmware may only be 32-bit.
452 */
453 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
454 md->virt_addr = md->phys_addr;
455 return;
456 }
457
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100458 efi_va -= size;
459
460 /* Is PA 2M-aligned? */
461 if (!(pa & (PMD_SIZE - 1))) {
462 efi_va &= PMD_MASK;
463 } else {
464 u64 pa_offset = pa & (PMD_SIZE - 1);
465 u64 prev_va = efi_va;
466
467 /* get us the same offset within this 2M page */
468 efi_va = (efi_va & PMD_MASK) + pa_offset;
469
470 if (efi_va > prev_va)
471 efi_va -= PMD_SIZE;
472 }
473
474 if (efi_va < EFI_VA_END) {
475 pr_warn(FW_WARN "VA address range overflow!\n");
476 return;
477 }
478
479 /* Do the VA map */
480 __map_region(md, efi_va);
481 md->virt_addr = efi_va;
482}
483
Dave Young3b266492013-12-20 18:02:14 +0800484/*
485 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
486 * md->virt_addr is the original virtual address which had been mapped in kexec
487 * 1st kernel.
488 */
489void __init efi_map_region_fixed(efi_memory_desc_t *md)
490{
Alex Thorlton0513fe12016-08-05 18:59:35 -0500491 __map_region(md, md->phys_addr);
Dave Young3b266492013-12-20 18:02:14 +0800492 __map_region(md, md->virt_addr);
493}
494
Keith Packarde1ad7832011-12-11 16:12:42 -0800495void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100496 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800497{
498 unsigned long last_map_pfn;
499
500 if (type == EFI_MEMORY_MAPPED_IO)
501 return ioremap(phys_addr, size);
502
503 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
504 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
505 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100506 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800507 }
508
Matt Fleming3e8fa262012-10-19 13:25:46 +0100509 if (!(attribute & EFI_MEMORY_WB))
510 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
511
Keith Packarde1ad7832011-12-11 16:12:42 -0800512 return (void __iomem *)__va(phys_addr);
513}
Dave Young1fec0532013-12-20 18:02:19 +0800514
515void __init parse_efi_setup(u64 phys_addr, u32 data_len)
516{
517 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800518}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100519
Sai Praneeth18141e82017-01-31 13:21:37 +0000520static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100521{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000522 unsigned long pfn;
523 pgd_t *pgd = efi_pgd;
Sai Praneeth18141e82017-01-31 13:21:37 +0000524 int err1, err2;
525
526 /* Update the 1:1 mapping */
527 pfn = md->phys_addr >> PAGE_SHIFT;
528 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
529 if (err1) {
530 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
531 md->phys_addr, md->virt_addr);
532 }
533
534 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
535 if (err2) {
536 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
537 md->phys_addr, md->virt_addr);
538 }
539
540 return err1 || err2;
541}
542
543static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
544{
545 unsigned long pf = 0;
546
547 if (md->attribute & EFI_MEMORY_XP)
548 pf |= _PAGE_NX;
549
550 if (!(md->attribute & EFI_MEMORY_RO))
551 pf |= _PAGE_RW;
552
Tom Lendacky1379edd2017-10-20 09:30:49 -0500553 if (sev_active())
554 pf |= _PAGE_ENC;
555
Sai Praneeth18141e82017-01-31 13:21:37 +0000556 return efi_update_mappings(md, pf);
557}
558
559void __init efi_runtime_update_mappings(void)
560{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000561 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000562
563 if (efi_enabled(EFI_OLD_MEMMAP)) {
564 if (__supported_pte_mask & _PAGE_NX)
565 runtime_code_page_mkexec();
566 return;
567 }
568
Sai Praneeth18141e82017-01-31 13:21:37 +0000569 /*
570 * Use the EFI Memory Attribute Table for mapping permissions if it
571 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
572 */
573 if (efi_enabled(EFI_MEM_ATTR)) {
574 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
575 return;
576 }
577
578 /*
579 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
580 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
581 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
582 * published by the firmware. Even if we find a buggy implementation of
583 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
584 * EFI_PROPERTIES_TABLE, because of the same reason.
585 */
586
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000587 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100588 return;
589
Matt Fleming78ce2482016-04-25 21:06:38 +0100590 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000591 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000592
593 if (!(md->attribute & EFI_MEMORY_RUNTIME))
594 continue;
595
596 if (!(md->attribute & EFI_MEMORY_WB))
597 pf |= _PAGE_PCD;
598
599 if ((md->attribute & EFI_MEMORY_XP) ||
600 (md->type == EFI_RUNTIME_SERVICES_DATA))
601 pf |= _PAGE_NX;
602
603 if (!(md->attribute & EFI_MEMORY_RO) &&
604 (md->type != EFI_RUNTIME_SERVICES_CODE))
605 pf |= _PAGE_RW;
606
Tom Lendacky1379edd2017-10-20 09:30:49 -0500607 if (sev_active())
608 pf |= _PAGE_ENC;
609
Sai Praneeth18141e82017-01-31 13:21:37 +0000610 efi_update_mappings(md, pf);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000611 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100612}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100613
614void __init efi_dump_pagetable(void)
615{
616#ifdef CONFIG_EFI_PGT_DUMP
Sai Praneethac81d3d2017-06-02 13:52:06 +0000617 if (efi_enabled(EFI_OLD_MEMMAP))
618 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
619 else
620 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100621#endif
622}
Matt Fleming994448f2014-03-05 18:15:37 +0000623
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000624#ifdef CONFIG_EFI_MIXED
625extern efi_status_t efi64_thunk(u32, ...);
626
627#define runtime_service32(func) \
628({ \
629 u32 table = (u32)(unsigned long)efi.systab; \
630 u32 *rt, *___f; \
631 \
632 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
633 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
634 *___f; \
635})
636
637/*
638 * Switch to the EFI page tables early so that we can access the 1:1
639 * runtime services mappings which are not mapped in any other page
640 * tables. This function must be called before runtime_service32().
641 *
642 * Also, disable interrupts because the IDT points to 64-bit handlers,
643 * which aren't going to function correctly when we switch to 32-bit.
644 */
645#define efi_thunk(f, ...) \
646({ \
647 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100648 unsigned long __flags; \
649 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000650 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100651 local_irq_save(__flags); \
652 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000653 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100654 __func = runtime_service32(f); \
655 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000656 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100657 arch_efi_call_virt_teardown(); \
658 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000659 \
660 __s; \
661})
662
663efi_status_t efi_thunk_set_virtual_address_map(
664 void *phys_set_virtual_address_map,
665 unsigned long memory_map_size,
666 unsigned long descriptor_size,
667 u32 descriptor_version,
668 efi_memory_desc_t *virtual_map)
669{
670 efi_status_t status;
671 unsigned long flags;
672 u32 func;
673
674 efi_sync_low_kernel_mappings();
675 local_irq_save(flags);
676
Andy Lutomirski6c690ee2017-06-12 10:26:14 -0700677 efi_scratch.prev_cr3 = __read_cr3();
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000678 write_cr3((unsigned long)efi_scratch.efi_pgt);
679 __flush_tlb_all();
680
681 func = (u32)(unsigned long)phys_set_virtual_address_map;
682 status = efi64_thunk(func, memory_map_size, descriptor_size,
683 descriptor_version, virtual_map);
684
685 write_cr3(efi_scratch.prev_cr3);
686 __flush_tlb_all();
687 local_irq_restore(flags);
688
689 return status;
690}
691
692static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
693{
694 efi_status_t status;
695 u32 phys_tm, phys_tc;
696
697 spin_lock(&rtc_lock);
698
Matt Flemingf6697df2016-11-12 21:04:24 +0000699 phys_tm = virt_to_phys_or_null(tm);
700 phys_tc = virt_to_phys_or_null(tc);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000701
702 status = efi_thunk(get_time, phys_tm, phys_tc);
703
704 spin_unlock(&rtc_lock);
705
706 return status;
707}
708
709static efi_status_t efi_thunk_set_time(efi_time_t *tm)
710{
711 efi_status_t status;
712 u32 phys_tm;
713
714 spin_lock(&rtc_lock);
715
Matt Flemingf6697df2016-11-12 21:04:24 +0000716 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000717
718 status = efi_thunk(set_time, phys_tm);
719
720 spin_unlock(&rtc_lock);
721
722 return status;
723}
724
725static efi_status_t
726efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
727 efi_time_t *tm)
728{
729 efi_status_t status;
730 u32 phys_enabled, phys_pending, phys_tm;
731
732 spin_lock(&rtc_lock);
733
Matt Flemingf6697df2016-11-12 21:04:24 +0000734 phys_enabled = virt_to_phys_or_null(enabled);
735 phys_pending = virt_to_phys_or_null(pending);
736 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000737
738 status = efi_thunk(get_wakeup_time, phys_enabled,
739 phys_pending, phys_tm);
740
741 spin_unlock(&rtc_lock);
742
743 return status;
744}
745
746static efi_status_t
747efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
748{
749 efi_status_t status;
750 u32 phys_tm;
751
752 spin_lock(&rtc_lock);
753
Matt Flemingf6697df2016-11-12 21:04:24 +0000754 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000755
756 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
757
758 spin_unlock(&rtc_lock);
759
760 return status;
761}
762
Matt Flemingf6697df2016-11-12 21:04:24 +0000763static unsigned long efi_name_size(efi_char16_t *name)
764{
765 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
766}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000767
768static efi_status_t
769efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
770 u32 *attr, unsigned long *data_size, void *data)
771{
772 efi_status_t status;
773 u32 phys_name, phys_vendor, phys_attr;
774 u32 phys_data_size, phys_data;
775
Matt Flemingf6697df2016-11-12 21:04:24 +0000776 phys_data_size = virt_to_phys_or_null(data_size);
777 phys_vendor = virt_to_phys_or_null(vendor);
778 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
779 phys_attr = virt_to_phys_or_null(attr);
780 phys_data = virt_to_phys_or_null_size(data, *data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000781
782 status = efi_thunk(get_variable, phys_name, phys_vendor,
783 phys_attr, phys_data_size, phys_data);
784
785 return status;
786}
787
788static efi_status_t
789efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
790 u32 attr, unsigned long data_size, void *data)
791{
792 u32 phys_name, phys_vendor, phys_data;
793 efi_status_t status;
794
Matt Flemingf6697df2016-11-12 21:04:24 +0000795 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
796 phys_vendor = virt_to_phys_or_null(vendor);
797 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000798
799 /* If data_size is > sizeof(u32) we've got problems */
800 status = efi_thunk(set_variable, phys_name, phys_vendor,
801 attr, data_size, phys_data);
802
803 return status;
804}
805
806static efi_status_t
807efi_thunk_get_next_variable(unsigned long *name_size,
808 efi_char16_t *name,
809 efi_guid_t *vendor)
810{
811 efi_status_t status;
812 u32 phys_name_size, phys_name, phys_vendor;
813
Matt Flemingf6697df2016-11-12 21:04:24 +0000814 phys_name_size = virt_to_phys_or_null(name_size);
815 phys_vendor = virt_to_phys_or_null(vendor);
816 phys_name = virt_to_phys_or_null_size(name, *name_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000817
818 status = efi_thunk(get_next_variable, phys_name_size,
819 phys_name, phys_vendor);
820
821 return status;
822}
823
824static efi_status_t
825efi_thunk_get_next_high_mono_count(u32 *count)
826{
827 efi_status_t status;
828 u32 phys_count;
829
Matt Flemingf6697df2016-11-12 21:04:24 +0000830 phys_count = virt_to_phys_or_null(count);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000831 status = efi_thunk(get_next_high_mono_count, phys_count);
832
833 return status;
834}
835
836static void
837efi_thunk_reset_system(int reset_type, efi_status_t status,
838 unsigned long data_size, efi_char16_t *data)
839{
840 u32 phys_data;
841
Matt Flemingf6697df2016-11-12 21:04:24 +0000842 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000843
844 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
845}
846
847static efi_status_t
848efi_thunk_update_capsule(efi_capsule_header_t **capsules,
849 unsigned long count, unsigned long sg_list)
850{
851 /*
852 * To properly support this function we would need to repackage
853 * 'capsules' because the firmware doesn't understand 64-bit
854 * pointers.
855 */
856 return EFI_UNSUPPORTED;
857}
858
859static efi_status_t
860efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
861 u64 *remaining_space,
862 u64 *max_variable_size)
863{
864 efi_status_t status;
865 u32 phys_storage, phys_remaining, phys_max;
866
867 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
868 return EFI_UNSUPPORTED;
869
Matt Flemingf6697df2016-11-12 21:04:24 +0000870 phys_storage = virt_to_phys_or_null(storage_space);
871 phys_remaining = virt_to_phys_or_null(remaining_space);
872 phys_max = virt_to_phys_or_null(max_variable_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000873
Matt Fleming9a110402014-03-16 17:46:46 +0000874 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000875 phys_remaining, phys_max);
876
877 return status;
878}
879
880static efi_status_t
881efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
882 unsigned long count, u64 *max_size,
883 int *reset_type)
884{
885 /*
886 * To properly support this function we would need to repackage
887 * 'capsules' because the firmware doesn't understand 64-bit
888 * pointers.
889 */
890 return EFI_UNSUPPORTED;
891}
892
893void efi_thunk_runtime_setup(void)
894{
895 efi.get_time = efi_thunk_get_time;
896 efi.set_time = efi_thunk_set_time;
897 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
898 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
899 efi.get_variable = efi_thunk_get_variable;
900 efi.get_next_variable = efi_thunk_get_next_variable;
901 efi.set_variable = efi_thunk_set_variable;
902 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
903 efi.reset_system = efi_thunk_reset_system;
904 efi.query_variable_info = efi_thunk_query_variable_info;
905 efi.update_capsule = efi_thunk_update_capsule;
906 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
907}
908#endif /* CONFIG_EFI_MIXED */