blob: 6cbf9e036aa84711e6d6a031928abf31edb01e08 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
2 * x86_64 specific EFI support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
4 *
5 * Copyright (C) 2005-2008 Intel Co.
6 * Fenghua Yu <fenghua.yu@intel.com>
7 * Bibo Mao <bibo.mao@intel.com>
8 * Chandramouli Narayanan <mouli@linux.intel.com>
9 * Huang Ying <ying.huang@intel.com>
10 *
11 * Code to convert EFI to E820 map has been implemented in elilo bootloader
12 * based on a EFI patch by Edgar Hucek. Based on the E820 map, the page table
13 * is setup appropriately for EFI runtime code.
14 * - mouli 06/14/2007.
15 *
16 */
17
Matt Fleming26d7f652015-10-25 10:26:35 +000018#define pr_fmt(fmt) "efi: " fmt
19
Huang, Ying5b836832008-01-30 13:31:19 +010020#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/mm.h>
23#include <linux/types.h>
24#include <linux/spinlock.h>
25#include <linux/bootmem.h>
26#include <linux/ioport.h>
Paul Gortmakercc3ae7b2016-07-13 20:18:58 -040027#include <linux/init.h>
Arnd Bergmann5ab788d2016-05-30 20:57:50 +020028#include <linux/mc146818rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010029#include <linux/efi.h>
30#include <linux/uaccess.h>
31#include <linux/io.h>
32#include <linux/reboot.h>
David Howells0d01ff22013-04-11 23:51:01 +010033#include <linux/slab.h>
Matt Flemingf6697df2016-11-12 21:04:24 +000034#include <linux/ucs2_string.h>
Huang, Ying5b836832008-01-30 13:31:19 +010035
36#include <asm/setup.h>
37#include <asm/page.h>
38#include <asm/e820.h>
39#include <asm/pgtable.h>
40#include <asm/tlbflush.h>
Huang, Ying5b836832008-01-30 13:31:19 +010041#include <asm/proto.h>
42#include <asm/efi.h>
Huang, Ying4de0d4a2008-02-13 17:22:41 +080043#include <asm/cacheflush.h>
Brian Gerst3819cd42009-01-23 11:03:29 +090044#include <asm/fixmap.h>
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010045#include <asm/realmode.h>
Matt Fleming4f9dbcf2014-01-10 18:48:30 +000046#include <asm/time.h>
Matt Fleming67a91082015-11-27 21:09:34 +000047#include <asm/pgalloc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010048
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010049/*
Baoquan Heb1d17762017-04-04 17:02:43 +010050 * We allocate runtime services regions top-down, starting from -4G, i.e.
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010051 * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G.
52 */
Mathias Krause8266e312014-09-21 17:26:54 +020053static u64 efi_va = EFI_VA_START;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010054
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000055struct efi_scratch efi_scratch;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010056
Matthew Garrett9cd2b072011-05-05 15:19:43 -040057static void __init early_code_mapping_set_exec(int executable)
Huang, Ying5b836832008-01-30 13:31:19 +010058{
59 efi_memory_desc_t *md;
Huang, Ying5b836832008-01-30 13:31:19 +010060
Huang, Yinga2172e22008-01-30 13:33:55 +010061 if (!(__supported_pte_mask & _PAGE_NX))
62 return;
63
Matthew Garrett916f6762011-05-25 09:53:13 -040064 /* Make EFI service code area executable */
Matt Fleming78ce2482016-04-25 21:06:38 +010065 for_each_efi_memory_desc(md) {
Matthew Garrett916f6762011-05-25 09:53:13 -040066 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
67 md->type == EFI_BOOT_SERVICES_CODE)
Matthew Garrett9cd2b072011-05-05 15:19:43 -040068 efi_set_executable(md, executable);
Huang, Ying5b836832008-01-30 13:31:19 +010069 }
70}
71
Ingo Molnar744937b2015-03-03 07:48:50 +010072pgd_t * __init efi_call_phys_prolog(void)
Huang, Ying5b836832008-01-30 13:31:19 +010073{
74 unsigned long vaddress;
Ingo Molnar744937b2015-03-03 07:48:50 +010075 pgd_t *save_pgd;
76
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060077 int pgd;
78 int n_pgds;
Huang, Ying5b836832008-01-30 13:31:19 +010079
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000080 if (!efi_enabled(EFI_OLD_MEMMAP)) {
81 save_pgd = (pgd_t *)read_cr3();
82 write_cr3((unsigned long)efi_scratch.efi_pgt);
83 goto out;
84 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +010085
Matthew Garrett9cd2b072011-05-05 15:19:43 -040086 early_code_mapping_set_exec(1);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060087
88 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
Markus Elfring20ebc152016-08-25 11:34:03 +020089 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
Nathan Zimmerb8f2c212013-01-08 09:02:43 -060090
91 for (pgd = 0; pgd < n_pgds; pgd++) {
92 save_pgd[pgd] = *pgd_offset_k(pgd * PGDIR_SIZE);
93 vaddress = (unsigned long)__va(pgd * PGDIR_SIZE);
94 set_pgd(pgd_offset_k(pgd * PGDIR_SIZE), *pgd_offset_k(vaddress));
95 }
Matt Flemingc9f2a9a2015-11-27 21:09:33 +000096out:
Huang, Ying5b836832008-01-30 13:31:19 +010097 __flush_tlb_all();
Ingo Molnar744937b2015-03-03 07:48:50 +010098
99 return save_pgd;
Huang, Ying5b836832008-01-30 13:31:19 +0100100}
101
Ingo Molnar744937b2015-03-03 07:48:50 +0100102void __init efi_call_phys_epilog(pgd_t *save_pgd)
Huang, Ying5b836832008-01-30 13:31:19 +0100103{
104 /*
105 * After the lock is released, the original page table is restored.
106 */
Ingo Molnar744937b2015-03-03 07:48:50 +0100107 int pgd_idx;
108 int nr_pgds;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100109
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000110 if (!efi_enabled(EFI_OLD_MEMMAP)) {
111 write_cr3((unsigned long)save_pgd);
112 __flush_tlb_all();
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100113 return;
Matt Flemingc9f2a9a2015-11-27 21:09:33 +0000114 }
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100115
Ingo Molnar744937b2015-03-03 07:48:50 +0100116 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
117
118 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++)
119 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
120
Nathan Zimmerb8f2c212013-01-08 09:02:43 -0600121 kfree(save_pgd);
Ingo Molnar744937b2015-03-03 07:48:50 +0100122
Huang, Ying5b836832008-01-30 13:31:19 +0100123 __flush_tlb_all();
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400124 early_code_mapping_set_exec(0);
Huang, Ying5b836832008-01-30 13:31:19 +0100125}
Keith Packarde1ad7832011-12-11 16:12:42 -0800126
Matt Fleming67a91082015-11-27 21:09:34 +0000127static pgd_t *efi_pgd;
128
129/*
130 * We need our own copy of the higher levels of the page tables
131 * because we want to avoid inserting EFI region mappings (EFI_VA_END
132 * to EFI_VA_START) into the standard kernel page tables. Everything
133 * else can be shared, see efi_sync_low_kernel_mappings().
134 */
135int __init efi_alloc_page_tables(void)
136{
137 pgd_t *pgd;
138 pud_t *pud;
139 gfp_t gfp_mask;
140
141 if (efi_enabled(EFI_OLD_MEMMAP))
142 return 0;
143
Michal Hockof58f2302016-06-24 14:48:53 -0700144 gfp_mask = GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO;
Matt Fleming67a91082015-11-27 21:09:34 +0000145 efi_pgd = (pgd_t *)__get_free_page(gfp_mask);
146 if (!efi_pgd)
147 return -ENOMEM;
148
149 pgd = efi_pgd + pgd_index(EFI_VA_END);
150
151 pud = pud_alloc_one(NULL, 0);
152 if (!pud) {
153 free_page((unsigned long)efi_pgd);
154 return -ENOMEM;
155 }
156
157 pgd_populate(NULL, pgd, pud);
158
159 return 0;
160}
161
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100162/*
163 * Add low kernel mappings for passing arguments to EFI functions.
164 */
165void efi_sync_low_kernel_mappings(void)
166{
Matt Fleming67a91082015-11-27 21:09:34 +0000167 unsigned num_entries;
168 pgd_t *pgd_k, *pgd_efi;
169 pud_t *pud_k, *pud_efi;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100170
171 if (efi_enabled(EFI_OLD_MEMMAP))
172 return;
173
Matt Fleming67a91082015-11-27 21:09:34 +0000174 /*
175 * We can share all PGD entries apart from the one entry that
176 * covers the EFI runtime mapping space.
177 *
178 * Make sure the EFI runtime region mappings are guaranteed to
179 * only span a single PGD entry and that the entry also maps
180 * other important kernel regions.
181 */
182 BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
183 BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
184 (EFI_VA_END & PGDIR_MASK));
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100185
Matt Fleming67a91082015-11-27 21:09:34 +0000186 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
187 pgd_k = pgd_offset_k(PAGE_OFFSET);
188
189 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
190 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
191
192 /*
193 * We share all the PUD entries apart from those that map the
194 * EFI regions. Copy around them.
195 */
196 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
197 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
198
199 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
200 pud_efi = pud_offset(pgd_efi, 0);
201
202 pgd_k = pgd_offset_k(EFI_VA_END);
203 pud_k = pud_offset(pgd_k, 0);
204
205 num_entries = pud_index(EFI_VA_END);
206 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
207
208 pud_efi = pud_offset(pgd_efi, EFI_VA_START);
209 pud_k = pud_offset(pgd_k, EFI_VA_START);
210
211 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
212 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100213}
214
Matt Flemingf6697df2016-11-12 21:04:24 +0000215/*
216 * Wrapper for slow_virt_to_phys() that handles NULL addresses.
217 */
218static inline phys_addr_t
219virt_to_phys_or_null_size(void *va, unsigned long size)
220{
221 bool bad_size;
222
223 if (!va)
224 return 0;
225
226 if (virt_addr_valid(va))
227 return virt_to_phys(va);
228
229 /*
230 * A fully aligned variable on the stack is guaranteed not to
231 * cross a page bounary. Try to catch strings on the stack by
232 * checking that 'size' is a power of two.
233 */
234 bad_size = size > PAGE_SIZE || !is_power_of_2(size);
235
236 WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size);
237
238 return slow_virt_to_phys(va);
239}
240
241#define virt_to_phys_or_null(addr) \
242 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
243
Mathias Krause4e78eb052014-09-07 19:42:17 +0200244int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100245{
Matt Flemingedc3b912015-11-27 21:09:31 +0000246 unsigned long pfn, text;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000247 struct page *page;
Matt Fleming994448f2014-03-05 18:15:37 +0000248 unsigned npages;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100249 pgd_t *pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100250
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100251 if (efi_enabled(EFI_OLD_MEMMAP))
252 return 0;
253
Matt Fleming67a91082015-11-27 21:09:34 +0000254 efi_scratch.efi_pgt = (pgd_t *)__pa(efi_pgd);
255 pgd = efi_pgd;
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100256
257 /*
258 * It can happen that the physical address of new_memmap lands in memory
259 * which is not mapped in the EFI page table. Therefore we need to go
260 * and ident-map those pages containing the map before calling
261 * phys_efi_set_virtual_address_map().
262 */
Matt Flemingedc3b912015-11-27 21:09:31 +0000263 pfn = pa_memmap >> PAGE_SHIFT;
Sai Praneeth15f003d2016-02-17 12:36:04 +0000264 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, _PAGE_NX | _PAGE_RW)) {
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100265 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
266 return 1;
267 }
268
269 efi_scratch.use_pgd = true;
270
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000271 /*
Jiri Kosinabf29bdd2017-01-27 22:25:52 +0000272 * Certain firmware versions are way too sentimential and still believe
273 * they are exclusive and unquestionable owners of the first physical page,
274 * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY
275 * (but then write-access it later during SetVirtualAddressMap()).
276 *
277 * Create a 1:1 mapping for this page, to avoid triple faults during early
278 * boot with such firmware. We are free to hand this page to the BIOS,
279 * as trim_bios_range() will reserve the first page and isolate it away
280 * from memory allocators anyway.
281 */
282 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, _PAGE_RW)) {
283 pr_err("Failed to create 1:1 mapping for the first page!\n");
284 return 1;
285 }
286
287 /*
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000288 * When making calls to the firmware everything needs to be 1:1
289 * mapped and addressable with 32-bit pointers. Map the kernel
290 * text and allocate a new stack because we can't rely on the
291 * stack pointer being < 4GB.
292 */
Matt Fleming12976672016-09-19 13:09:09 +0100293 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
Matt Fleming994448f2014-03-05 18:15:37 +0000294 return 0;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000295
296 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
297 if (!page)
298 panic("Unable to allocate EFI runtime stack < 4GB\n");
299
300 efi_scratch.phys_stack = virt_to_phys(page_address(page));
301 efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
302
Sai Praneeth2ad510d2016-02-17 12:36:06 +0000303 npages = (_etext - _text) >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000304 text = __pa(_text);
Matt Flemingedc3b912015-11-27 21:09:31 +0000305 pfn = text >> PAGE_SHIFT;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000306
Sai Praneeth15f003d2016-02-17 12:36:04 +0000307 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, _PAGE_RW)) {
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000308 pr_err("Failed to map kernel text 1:1\n");
Matt Fleming994448f2014-03-05 18:15:37 +0000309 return 1;
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000310 }
Borislav Petkovb7b898a2014-01-18 12:48:17 +0100311
312 return 0;
313}
314
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100315static void __init __map_region(efi_memory_desc_t *md, u64 va)
316{
Sai Praneeth15f003d2016-02-17 12:36:04 +0000317 unsigned long flags = _PAGE_RW;
Matt Flemingedc3b912015-11-27 21:09:31 +0000318 unsigned long pfn;
Matt Fleming67a91082015-11-27 21:09:34 +0000319 pgd_t *pgd = efi_pgd;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100320
321 if (!(md->attribute & EFI_MEMORY_WB))
Matt Flemingedc3b912015-11-27 21:09:31 +0000322 flags |= _PAGE_PCD;
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100323
Matt Flemingedc3b912015-11-27 21:09:31 +0000324 pfn = md->phys_addr >> PAGE_SHIFT;
325 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100326 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
327 md->phys_addr, va);
328}
329
330void __init efi_map_region(efi_memory_desc_t *md)
331{
332 unsigned long size = md->num_pages << PAGE_SHIFT;
333 u64 pa = md->phys_addr;
334
335 if (efi_enabled(EFI_OLD_MEMMAP))
336 return old_map_region(md);
337
338 /*
339 * Make sure the 1:1 mappings are present as a catch-all for b0rked
340 * firmware which doesn't update all internal pointers after switching
341 * to virtual mode and would otherwise crap on us.
342 */
343 __map_region(md, md->phys_addr);
344
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000345 /*
346 * Enforce the 1:1 mapping as the default virtual address when
347 * booting in EFI mixed mode, because even though we may be
348 * running a 64-bit kernel, the firmware may only be 32-bit.
349 */
350 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
351 md->virt_addr = md->phys_addr;
352 return;
353 }
354
Borislav Petkovd2f7cbe2013-10-31 17:25:08 +0100355 efi_va -= size;
356
357 /* Is PA 2M-aligned? */
358 if (!(pa & (PMD_SIZE - 1))) {
359 efi_va &= PMD_MASK;
360 } else {
361 u64 pa_offset = pa & (PMD_SIZE - 1);
362 u64 prev_va = efi_va;
363
364 /* get us the same offset within this 2M page */
365 efi_va = (efi_va & PMD_MASK) + pa_offset;
366
367 if (efi_va > prev_va)
368 efi_va -= PMD_SIZE;
369 }
370
371 if (efi_va < EFI_VA_END) {
372 pr_warn(FW_WARN "VA address range overflow!\n");
373 return;
374 }
375
376 /* Do the VA map */
377 __map_region(md, efi_va);
378 md->virt_addr = efi_va;
379}
380
Dave Young3b266492013-12-20 18:02:14 +0800381/*
382 * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges.
383 * md->virt_addr is the original virtual address which had been mapped in kexec
384 * 1st kernel.
385 */
386void __init efi_map_region_fixed(efi_memory_desc_t *md)
387{
Alex Thorlton0513fe12016-08-05 18:59:35 -0500388 __map_region(md, md->phys_addr);
Dave Young3b266492013-12-20 18:02:14 +0800389 __map_region(md, md->virt_addr);
390}
391
Keith Packarde1ad7832011-12-11 16:12:42 -0800392void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
Matt Fleming3e8fa262012-10-19 13:25:46 +0100393 u32 type, u64 attribute)
Keith Packarde1ad7832011-12-11 16:12:42 -0800394{
395 unsigned long last_map_pfn;
396
397 if (type == EFI_MEMORY_MAPPED_IO)
398 return ioremap(phys_addr, size);
399
400 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
401 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
402 unsigned long top = last_map_pfn << PAGE_SHIFT;
Matt Fleming3e8fa262012-10-19 13:25:46 +0100403 efi_ioremap(top, size - (top - phys_addr), type, attribute);
Keith Packarde1ad7832011-12-11 16:12:42 -0800404 }
405
Matt Fleming3e8fa262012-10-19 13:25:46 +0100406 if (!(attribute & EFI_MEMORY_WB))
407 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
408
Keith Packarde1ad7832011-12-11 16:12:42 -0800409 return (void __iomem *)__va(phys_addr);
410}
Dave Young1fec0532013-12-20 18:02:19 +0800411
412void __init parse_efi_setup(u64 phys_addr, u32 data_len)
413{
414 efi_setup = phys_addr + sizeof(struct setup_data);
Dave Young1fec0532013-12-20 18:02:19 +0800415}
Borislav Petkovc55d0162014-02-14 08:24:24 +0100416
Sai Praneeth18141e82017-01-31 13:21:37 +0000417static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
Borislav Petkovc55d0162014-02-14 08:24:24 +0100418{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000419 unsigned long pfn;
420 pgd_t *pgd = efi_pgd;
Sai Praneeth18141e82017-01-31 13:21:37 +0000421 int err1, err2;
422
423 /* Update the 1:1 mapping */
424 pfn = md->phys_addr >> PAGE_SHIFT;
425 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
426 if (err1) {
427 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
428 md->phys_addr, md->virt_addr);
429 }
430
431 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
432 if (err2) {
433 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
434 md->phys_addr, md->virt_addr);
435 }
436
437 return err1 || err2;
438}
439
440static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
441{
442 unsigned long pf = 0;
443
444 if (md->attribute & EFI_MEMORY_XP)
445 pf |= _PAGE_NX;
446
447 if (!(md->attribute & EFI_MEMORY_RO))
448 pf |= _PAGE_RW;
449
450 return efi_update_mappings(md, pf);
451}
452
453void __init efi_runtime_update_mappings(void)
454{
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000455 efi_memory_desc_t *md;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000456
457 if (efi_enabled(EFI_OLD_MEMMAP)) {
458 if (__supported_pte_mask & _PAGE_NX)
459 runtime_code_page_mkexec();
460 return;
461 }
462
Sai Praneeth18141e82017-01-31 13:21:37 +0000463 /*
464 * Use the EFI Memory Attribute Table for mapping permissions if it
465 * exists, since it is intended to supersede EFI_PROPERTIES_TABLE.
466 */
467 if (efi_enabled(EFI_MEM_ATTR)) {
468 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
469 return;
470 }
471
472 /*
473 * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace
474 * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update
475 * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not
476 * published by the firmware. Even if we find a buggy implementation of
477 * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to
478 * EFI_PROPERTIES_TABLE, because of the same reason.
479 */
480
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000481 if (!efi_enabled(EFI_NX_PE_DATA))
Borislav Petkovc55d0162014-02-14 08:24:24 +0100482 return;
483
Matt Fleming78ce2482016-04-25 21:06:38 +0100484 for_each_efi_memory_desc(md) {
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000485 unsigned long pf = 0;
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000486
487 if (!(md->attribute & EFI_MEMORY_RUNTIME))
488 continue;
489
490 if (!(md->attribute & EFI_MEMORY_WB))
491 pf |= _PAGE_PCD;
492
493 if ((md->attribute & EFI_MEMORY_XP) ||
494 (md->type == EFI_RUNTIME_SERVICES_DATA))
495 pf |= _PAGE_NX;
496
497 if (!(md->attribute & EFI_MEMORY_RO) &&
498 (md->type != EFI_RUNTIME_SERVICES_CODE))
499 pf |= _PAGE_RW;
500
Sai Praneeth18141e82017-01-31 13:21:37 +0000501 efi_update_mappings(md, pf);
Sai Praneeth6d0cc882016-02-17 12:36:05 +0000502 }
Borislav Petkovc55d0162014-02-14 08:24:24 +0100503}
Borislav Petkov11cc8512014-01-18 12:48:15 +0100504
505void __init efi_dump_pagetable(void)
506{
507#ifdef CONFIG_EFI_PGT_DUMP
Matt Fleming67a91082015-11-27 21:09:34 +0000508 ptdump_walk_pgd_level(NULL, efi_pgd);
Borislav Petkov11cc8512014-01-18 12:48:15 +0100509#endif
510}
Matt Fleming994448f2014-03-05 18:15:37 +0000511
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000512#ifdef CONFIG_EFI_MIXED
513extern efi_status_t efi64_thunk(u32, ...);
514
515#define runtime_service32(func) \
516({ \
517 u32 table = (u32)(unsigned long)efi.systab; \
518 u32 *rt, *___f; \
519 \
520 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
521 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
522 *___f; \
523})
524
525/*
526 * Switch to the EFI page tables early so that we can access the 1:1
527 * runtime services mappings which are not mapped in any other page
528 * tables. This function must be called before runtime_service32().
529 *
530 * Also, disable interrupts because the IDT points to 64-bit handlers,
531 * which aren't going to function correctly when we switch to 32-bit.
532 */
533#define efi_thunk(f, ...) \
534({ \
535 efi_status_t __s; \
Alex Thorlton21f86622016-06-25 08:20:29 +0100536 unsigned long __flags; \
537 u32 __func; \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000538 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100539 local_irq_save(__flags); \
540 arch_efi_call_virt_setup(); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000541 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100542 __func = runtime_service32(f); \
543 __s = efi64_thunk(__func, __VA_ARGS__); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000544 \
Alex Thorlton21f86622016-06-25 08:20:29 +0100545 arch_efi_call_virt_teardown(); \
546 local_irq_restore(__flags); \
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000547 \
548 __s; \
549})
550
551efi_status_t efi_thunk_set_virtual_address_map(
552 void *phys_set_virtual_address_map,
553 unsigned long memory_map_size,
554 unsigned long descriptor_size,
555 u32 descriptor_version,
556 efi_memory_desc_t *virtual_map)
557{
558 efi_status_t status;
559 unsigned long flags;
560 u32 func;
561
562 efi_sync_low_kernel_mappings();
563 local_irq_save(flags);
564
565 efi_scratch.prev_cr3 = read_cr3();
566 write_cr3((unsigned long)efi_scratch.efi_pgt);
567 __flush_tlb_all();
568
569 func = (u32)(unsigned long)phys_set_virtual_address_map;
570 status = efi64_thunk(func, memory_map_size, descriptor_size,
571 descriptor_version, virtual_map);
572
573 write_cr3(efi_scratch.prev_cr3);
574 __flush_tlb_all();
575 local_irq_restore(flags);
576
577 return status;
578}
579
580static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
581{
582 efi_status_t status;
583 u32 phys_tm, phys_tc;
584
585 spin_lock(&rtc_lock);
586
Matt Flemingf6697df2016-11-12 21:04:24 +0000587 phys_tm = virt_to_phys_or_null(tm);
588 phys_tc = virt_to_phys_or_null(tc);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000589
590 status = efi_thunk(get_time, phys_tm, phys_tc);
591
592 spin_unlock(&rtc_lock);
593
594 return status;
595}
596
597static efi_status_t efi_thunk_set_time(efi_time_t *tm)
598{
599 efi_status_t status;
600 u32 phys_tm;
601
602 spin_lock(&rtc_lock);
603
Matt Flemingf6697df2016-11-12 21:04:24 +0000604 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000605
606 status = efi_thunk(set_time, phys_tm);
607
608 spin_unlock(&rtc_lock);
609
610 return status;
611}
612
613static efi_status_t
614efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
615 efi_time_t *tm)
616{
617 efi_status_t status;
618 u32 phys_enabled, phys_pending, phys_tm;
619
620 spin_lock(&rtc_lock);
621
Matt Flemingf6697df2016-11-12 21:04:24 +0000622 phys_enabled = virt_to_phys_or_null(enabled);
623 phys_pending = virt_to_phys_or_null(pending);
624 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000625
626 status = efi_thunk(get_wakeup_time, phys_enabled,
627 phys_pending, phys_tm);
628
629 spin_unlock(&rtc_lock);
630
631 return status;
632}
633
634static efi_status_t
635efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
636{
637 efi_status_t status;
638 u32 phys_tm;
639
640 spin_lock(&rtc_lock);
641
Matt Flemingf6697df2016-11-12 21:04:24 +0000642 phys_tm = virt_to_phys_or_null(tm);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000643
644 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
645
646 spin_unlock(&rtc_lock);
647
648 return status;
649}
650
Matt Flemingf6697df2016-11-12 21:04:24 +0000651static unsigned long efi_name_size(efi_char16_t *name)
652{
653 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
654}
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000655
656static efi_status_t
657efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
658 u32 *attr, unsigned long *data_size, void *data)
659{
660 efi_status_t status;
661 u32 phys_name, phys_vendor, phys_attr;
662 u32 phys_data_size, phys_data;
663
Matt Flemingf6697df2016-11-12 21:04:24 +0000664 phys_data_size = virt_to_phys_or_null(data_size);
665 phys_vendor = virt_to_phys_or_null(vendor);
666 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
667 phys_attr = virt_to_phys_or_null(attr);
668 phys_data = virt_to_phys_or_null_size(data, *data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000669
670 status = efi_thunk(get_variable, phys_name, phys_vendor,
671 phys_attr, phys_data_size, phys_data);
672
673 return status;
674}
675
676static efi_status_t
677efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
678 u32 attr, unsigned long data_size, void *data)
679{
680 u32 phys_name, phys_vendor, phys_data;
681 efi_status_t status;
682
Matt Flemingf6697df2016-11-12 21:04:24 +0000683 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
684 phys_vendor = virt_to_phys_or_null(vendor);
685 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000686
687 /* If data_size is > sizeof(u32) we've got problems */
688 status = efi_thunk(set_variable, phys_name, phys_vendor,
689 attr, data_size, phys_data);
690
691 return status;
692}
693
694static efi_status_t
695efi_thunk_get_next_variable(unsigned long *name_size,
696 efi_char16_t *name,
697 efi_guid_t *vendor)
698{
699 efi_status_t status;
700 u32 phys_name_size, phys_name, phys_vendor;
701
Matt Flemingf6697df2016-11-12 21:04:24 +0000702 phys_name_size = virt_to_phys_or_null(name_size);
703 phys_vendor = virt_to_phys_or_null(vendor);
704 phys_name = virt_to_phys_or_null_size(name, *name_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000705
706 status = efi_thunk(get_next_variable, phys_name_size,
707 phys_name, phys_vendor);
708
709 return status;
710}
711
712static efi_status_t
713efi_thunk_get_next_high_mono_count(u32 *count)
714{
715 efi_status_t status;
716 u32 phys_count;
717
Matt Flemingf6697df2016-11-12 21:04:24 +0000718 phys_count = virt_to_phys_or_null(count);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000719 status = efi_thunk(get_next_high_mono_count, phys_count);
720
721 return status;
722}
723
724static void
725efi_thunk_reset_system(int reset_type, efi_status_t status,
726 unsigned long data_size, efi_char16_t *data)
727{
728 u32 phys_data;
729
Matt Flemingf6697df2016-11-12 21:04:24 +0000730 phys_data = virt_to_phys_or_null_size(data, data_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000731
732 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
733}
734
735static efi_status_t
736efi_thunk_update_capsule(efi_capsule_header_t **capsules,
737 unsigned long count, unsigned long sg_list)
738{
739 /*
740 * To properly support this function we would need to repackage
741 * 'capsules' because the firmware doesn't understand 64-bit
742 * pointers.
743 */
744 return EFI_UNSUPPORTED;
745}
746
747static efi_status_t
748efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
749 u64 *remaining_space,
750 u64 *max_variable_size)
751{
752 efi_status_t status;
753 u32 phys_storage, phys_remaining, phys_max;
754
755 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
756 return EFI_UNSUPPORTED;
757
Matt Flemingf6697df2016-11-12 21:04:24 +0000758 phys_storage = virt_to_phys_or_null(storage_space);
759 phys_remaining = virt_to_phys_or_null(remaining_space);
760 phys_max = virt_to_phys_or_null(max_variable_size);
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000761
Matt Fleming9a110402014-03-16 17:46:46 +0000762 status = efi_thunk(query_variable_info, attr, phys_storage,
Matt Fleming4f9dbcf2014-01-10 18:48:30 +0000763 phys_remaining, phys_max);
764
765 return status;
766}
767
768static efi_status_t
769efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
770 unsigned long count, u64 *max_size,
771 int *reset_type)
772{
773 /*
774 * To properly support this function we would need to repackage
775 * 'capsules' because the firmware doesn't understand 64-bit
776 * pointers.
777 */
778 return EFI_UNSUPPORTED;
779}
780
781void efi_thunk_runtime_setup(void)
782{
783 efi.get_time = efi_thunk_get_time;
784 efi.set_time = efi_thunk_set_time;
785 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
786 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
787 efi.get_variable = efi_thunk_get_variable;
788 efi.get_next_variable = efi_thunk_get_next_variable;
789 efi.set_variable = efi_thunk_set_variable;
790 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
791 efi.reset_system = efi_thunk_reset_system;
792 efi.query_variable_info = efi_thunk_query_variable_info;
793 efi.update_capsule = efi_thunk_update_capsule;
794 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
795}
796#endif /* CONFIG_EFI_MIXED */