Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5b83683f HY |
2 | /* |
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 | ||
26d7f65f MF |
19 | #define pr_fmt(fmt) "efi: " fmt |
20 | ||
5b83683f HY |
21 | #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> | |
5ab788d7 | 28 | #include <linux/mc146818rtc.h> |
5b83683f HY |
29 | #include <linux/efi.h> |
30 | #include <linux/uaccess.h> | |
31 | #include <linux/io.h> | |
32 | #include <linux/reboot.h> | |
0d01ff25 | 33 | #include <linux/slab.h> |
f6697df3 | 34 | #include <linux/ucs2_string.h> |
1379edd5 | 35 | #include <linux/mem_encrypt.h> |
5b83683f HY |
36 | |
37 | #include <asm/setup.h> | |
38 | #include <asm/page.h> | |
66441bd3 | 39 | #include <asm/e820/api.h> |
5b83683f HY |
40 | #include <asm/pgtable.h> |
41 | #include <asm/tlbflush.h> | |
5b83683f HY |
42 | #include <asm/proto.h> |
43 | #include <asm/efi.h> | |
4de0d4a6 | 44 | #include <asm/cacheflush.h> |
3819cd48 | 45 | #include <asm/fixmap.h> |
d2f7cbe7 | 46 | #include <asm/realmode.h> |
4f9dbcfc | 47 | #include <asm/time.h> |
67a9108e | 48 | #include <asm/pgalloc.h> |
5b83683f | 49 | |
d2f7cbe7 | 50 | /* |
b1d17761 | 51 | * We allocate runtime services regions top-down, starting from -4G, i.e. |
d2f7cbe7 BP |
52 | * 0xffff_ffff_0000_0000 and limit EFI VA mapping space to 64G. |
53 | */ | |
8266e31e | 54 | static u64 efi_va = EFI_VA_START; |
d2f7cbe7 | 55 | |
c9f2a9a6 | 56 | struct efi_scratch efi_scratch; |
d2f7cbe7 | 57 | |
9cd2b07c | 58 | static void __init early_code_mapping_set_exec(int executable) |
5b83683f HY |
59 | { |
60 | efi_memory_desc_t *md; | |
5b83683f | 61 | |
a2172e25 HY |
62 | if (!(__supported_pte_mask & _PAGE_NX)) |
63 | return; | |
64 | ||
916f676f | 65 | /* Make EFI service code area executable */ |
78ce248f | 66 | for_each_efi_memory_desc(md) { |
916f676f MG |
67 | if (md->type == EFI_RUNTIME_SERVICES_CODE || |
68 | md->type == EFI_BOOT_SERVICES_CODE) | |
9cd2b07c | 69 | efi_set_executable(md, executable); |
5b83683f HY |
70 | } |
71 | } | |
72 | ||
744937b0 | 73 | pgd_t * __init efi_call_phys_prolog(void) |
5b83683f | 74 | { |
94133e46 BH |
75 | unsigned long vaddr, addr_pgd, addr_p4d, addr_pud; |
76 | pgd_t *save_pgd, *pgd_k, *pgd_efi; | |
77 | p4d_t *p4d, *p4d_k, *p4d_efi; | |
78 | pud_t *pud; | |
744937b0 | 79 | |
b8f2c21d | 80 | int pgd; |
94133e46 | 81 | int n_pgds, i, j; |
5b83683f | 82 | |
c9f2a9a6 | 83 | if (!efi_enabled(EFI_OLD_MEMMAP)) { |
6c690ee1 | 84 | save_pgd = (pgd_t *)__read_cr3(); |
c9f2a9a6 MF |
85 | write_cr3((unsigned long)efi_scratch.efi_pgt); |
86 | goto out; | |
87 | } | |
d2f7cbe7 | 88 | |
9cd2b07c | 89 | early_code_mapping_set_exec(1); |
b8f2c21d NZ |
90 | |
91 | n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE); | |
20ebc15e | 92 | save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL); |
b8f2c21d | 93 | |
94133e46 BH |
94 | /* |
95 | * Build 1:1 identity mapping for efi=old_map usage. Note that | |
96 | * PAGE_OFFSET is PGDIR_SIZE aligned when KASLR is disabled, while | |
97 | * it is PUD_SIZE ALIGNED with KASLR enabled. So for a given physical | |
98 | * address X, the pud_index(X) != pud_index(__va(X)), we can only copy | |
99 | * PUD entry of __va(X) to fill in pud entry of X to build 1:1 mapping. | |
100 | * This means here we can only reuse the PMD tables of the direct mapping. | |
101 | */ | |
b8f2c21d | 102 | for (pgd = 0; pgd < n_pgds; pgd++) { |
94133e46 BH |
103 | addr_pgd = (unsigned long)(pgd * PGDIR_SIZE); |
104 | vaddr = (unsigned long)__va(pgd * PGDIR_SIZE); | |
105 | pgd_efi = pgd_offset_k(addr_pgd); | |
106 | save_pgd[pgd] = *pgd_efi; | |
107 | ||
108 | p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd); | |
109 | if (!p4d) { | |
110 | pr_err("Failed to allocate p4d table!\n"); | |
111 | goto out; | |
112 | } | |
113 | ||
114 | for (i = 0; i < PTRS_PER_P4D; i++) { | |
115 | addr_p4d = addr_pgd + i * P4D_SIZE; | |
116 | p4d_efi = p4d + p4d_index(addr_p4d); | |
117 | ||
118 | pud = pud_alloc(&init_mm, p4d_efi, addr_p4d); | |
119 | if (!pud) { | |
120 | pr_err("Failed to allocate pud table!\n"); | |
121 | goto out; | |
122 | } | |
123 | ||
124 | for (j = 0; j < PTRS_PER_PUD; j++) { | |
125 | addr_pud = addr_p4d + j * PUD_SIZE; | |
126 | ||
127 | if (addr_pud > (max_pfn << PAGE_SHIFT)) | |
128 | break; | |
129 | ||
130 | vaddr = (unsigned long)__va(addr_pud); | |
131 | ||
132 | pgd_k = pgd_offset_k(vaddr); | |
133 | p4d_k = p4d_offset(pgd_k, vaddr); | |
134 | pud[j] = *pud_offset(p4d_k, vaddr); | |
135 | } | |
136 | } | |
de53c378 | 137 | pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX; |
b8f2c21d | 138 | } |
de53c378 | 139 | |
c9f2a9a6 | 140 | out: |
5b83683f | 141 | __flush_tlb_all(); |
744937b0 IM |
142 | |
143 | return save_pgd; | |
5b83683f HY |
144 | } |
145 | ||
744937b0 | 146 | void __init efi_call_phys_epilog(pgd_t *save_pgd) |
5b83683f HY |
147 | { |
148 | /* | |
149 | * After the lock is released, the original page table is restored. | |
150 | */ | |
94133e46 | 151 | int pgd_idx, i; |
744937b0 | 152 | int nr_pgds; |
94133e46 BH |
153 | pgd_t *pgd; |
154 | p4d_t *p4d; | |
155 | pud_t *pud; | |
d2f7cbe7 | 156 | |
c9f2a9a6 MF |
157 | if (!efi_enabled(EFI_OLD_MEMMAP)) { |
158 | write_cr3((unsigned long)save_pgd); | |
159 | __flush_tlb_all(); | |
d2f7cbe7 | 160 | return; |
c9f2a9a6 | 161 | } |
d2f7cbe7 | 162 | |
744937b0 IM |
163 | nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE); |
164 | ||
94133e46 BH |
165 | for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) { |
166 | pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE); | |
744937b0 IM |
167 | set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]); |
168 | ||
94133e46 BH |
169 | if (!(pgd_val(*pgd) & _PAGE_PRESENT)) |
170 | continue; | |
171 | ||
172 | for (i = 0; i < PTRS_PER_P4D; i++) { | |
173 | p4d = p4d_offset(pgd, | |
174 | pgd_idx * PGDIR_SIZE + i * P4D_SIZE); | |
175 | ||
176 | if (!(p4d_val(*p4d) & _PAGE_PRESENT)) | |
177 | continue; | |
178 | ||
179 | pud = (pud_t *)p4d_page_vaddr(*p4d); | |
180 | pud_free(&init_mm, pud); | |
181 | } | |
182 | ||
183 | p4d = (p4d_t *)pgd_page_vaddr(*pgd); | |
184 | p4d_free(&init_mm, p4d); | |
185 | } | |
186 | ||
b8f2c21d | 187 | kfree(save_pgd); |
744937b0 | 188 | |
5b83683f | 189 | __flush_tlb_all(); |
9cd2b07c | 190 | early_code_mapping_set_exec(0); |
5b83683f | 191 | } |
e1ad783b | 192 | |
67a9108e MF |
193 | static pgd_t *efi_pgd; |
194 | ||
195 | /* | |
196 | * We need our own copy of the higher levels of the page tables | |
197 | * because we want to avoid inserting EFI region mappings (EFI_VA_END | |
198 | * to EFI_VA_START) into the standard kernel page tables. Everything | |
199 | * else can be shared, see efi_sync_low_kernel_mappings(). | |
d9e9a641 DH |
200 | * |
201 | * We don't want the pgd on the pgd_list and cannot use pgd_alloc() for the | |
202 | * allocation. | |
67a9108e MF |
203 | */ |
204 | int __init efi_alloc_page_tables(void) | |
205 | { | |
206 | pgd_t *pgd; | |
e981316f | 207 | p4d_t *p4d; |
67a9108e MF |
208 | pud_t *pud; |
209 | gfp_t gfp_mask; | |
210 | ||
211 | if (efi_enabled(EFI_OLD_MEMMAP)) | |
212 | return 0; | |
213 | ||
75f296d9 | 214 | gfp_mask = GFP_KERNEL | __GFP_ZERO; |
d9e9a641 | 215 | efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER); |
67a9108e MF |
216 | if (!efi_pgd) |
217 | return -ENOMEM; | |
218 | ||
219 | pgd = efi_pgd + pgd_index(EFI_VA_END); | |
e981316f KS |
220 | p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END); |
221 | if (!p4d) { | |
222 | free_page((unsigned long)efi_pgd); | |
223 | return -ENOMEM; | |
224 | } | |
67a9108e | 225 | |
e981316f | 226 | pud = pud_alloc(&init_mm, p4d, EFI_VA_END); |
67a9108e | 227 | if (!pud) { |
e981316f KS |
228 | if (CONFIG_PGTABLE_LEVELS > 4) |
229 | free_page((unsigned long) pgd_page_vaddr(*pgd)); | |
67a9108e MF |
230 | free_page((unsigned long)efi_pgd); |
231 | return -ENOMEM; | |
232 | } | |
233 | ||
67a9108e MF |
234 | return 0; |
235 | } | |
236 | ||
d2f7cbe7 BP |
237 | /* |
238 | * Add low kernel mappings for passing arguments to EFI functions. | |
239 | */ | |
240 | void efi_sync_low_kernel_mappings(void) | |
241 | { | |
67a9108e MF |
242 | unsigned num_entries; |
243 | pgd_t *pgd_k, *pgd_efi; | |
e0c4f675 | 244 | p4d_t *p4d_k, *p4d_efi; |
67a9108e | 245 | pud_t *pud_k, *pud_efi; |
d2f7cbe7 BP |
246 | |
247 | if (efi_enabled(EFI_OLD_MEMMAP)) | |
248 | return; | |
249 | ||
67a9108e MF |
250 | /* |
251 | * We can share all PGD entries apart from the one entry that | |
252 | * covers the EFI runtime mapping space. | |
253 | * | |
254 | * Make sure the EFI runtime region mappings are guaranteed to | |
255 | * only span a single PGD entry and that the entry also maps | |
256 | * other important kernel regions. | |
257 | */ | |
258 | BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END)); | |
259 | BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) != | |
260 | (EFI_VA_END & PGDIR_MASK)); | |
261 | ||
262 | pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET); | |
263 | pgd_k = pgd_offset_k(PAGE_OFFSET); | |
264 | ||
265 | num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET); | |
266 | memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries); | |
d2f7cbe7 | 267 | |
e981316f KS |
268 | /* |
269 | * As with PGDs, we share all P4D entries apart from the one entry | |
270 | * that covers the EFI runtime mapping space. | |
271 | */ | |
272 | BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END)); | |
273 | BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK)); | |
274 | ||
275 | pgd_efi = efi_pgd + pgd_index(EFI_VA_END); | |
276 | pgd_k = pgd_offset_k(EFI_VA_END); | |
277 | p4d_efi = p4d_offset(pgd_efi, 0); | |
278 | p4d_k = p4d_offset(pgd_k, 0); | |
279 | ||
280 | num_entries = p4d_index(EFI_VA_END); | |
281 | memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries); | |
282 | ||
67a9108e MF |
283 | /* |
284 | * We share all the PUD entries apart from those that map the | |
285 | * EFI regions. Copy around them. | |
286 | */ | |
287 | BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0); | |
288 | BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0); | |
289 | ||
e981316f KS |
290 | p4d_efi = p4d_offset(pgd_efi, EFI_VA_END); |
291 | p4d_k = p4d_offset(pgd_k, EFI_VA_END); | |
e0c4f675 | 292 | pud_efi = pud_offset(p4d_efi, 0); |
e0c4f675 | 293 | pud_k = pud_offset(p4d_k, 0); |
67a9108e MF |
294 | |
295 | num_entries = pud_index(EFI_VA_END); | |
296 | memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries); | |
297 | ||
e0c4f675 | 298 | pud_efi = pud_offset(p4d_efi, EFI_VA_START); |
e0c4f675 | 299 | pud_k = pud_offset(p4d_k, EFI_VA_START); |
67a9108e MF |
300 | |
301 | num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START); | |
302 | memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries); | |
d2f7cbe7 BP |
303 | } |
304 | ||
f6697df3 MF |
305 | /* |
306 | * Wrapper for slow_virt_to_phys() that handles NULL addresses. | |
307 | */ | |
308 | static inline phys_addr_t | |
309 | virt_to_phys_or_null_size(void *va, unsigned long size) | |
310 | { | |
311 | bool bad_size; | |
312 | ||
313 | if (!va) | |
314 | return 0; | |
315 | ||
316 | if (virt_addr_valid(va)) | |
317 | return virt_to_phys(va); | |
318 | ||
319 | /* | |
320 | * A fully aligned variable on the stack is guaranteed not to | |
321 | * cross a page bounary. Try to catch strings on the stack by | |
322 | * checking that 'size' is a power of two. | |
323 | */ | |
324 | bad_size = size > PAGE_SIZE || !is_power_of_2(size); | |
325 | ||
326 | WARN_ON(!IS_ALIGNED((unsigned long)va, size) || bad_size); | |
327 | ||
328 | return slow_virt_to_phys(va); | |
329 | } | |
330 | ||
331 | #define virt_to_phys_or_null(addr) \ | |
332 | virt_to_phys_or_null_size((addr), sizeof(*(addr))) | |
333 | ||
4e78eb05 | 334 | int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages) |
d2f7cbe7 | 335 | { |
38eecccd | 336 | unsigned long pfn, text, pf; |
4f9dbcfc | 337 | struct page *page; |
994448f1 | 338 | unsigned npages; |
b7b898ae BP |
339 | pgd_t *pgd; |
340 | ||
341 | if (efi_enabled(EFI_OLD_MEMMAP)) | |
342 | return 0; | |
343 | ||
38eecccd TL |
344 | /* |
345 | * Since the PGD is encrypted, set the encryption mask so that when | |
346 | * this value is loaded into cr3 the PGD will be decrypted during | |
347 | * the pagetable walk. | |
348 | */ | |
349 | efi_scratch.efi_pgt = (pgd_t *)__sme_pa(efi_pgd); | |
67a9108e | 350 | pgd = efi_pgd; |
d2f7cbe7 | 351 | |
b7b898ae BP |
352 | /* |
353 | * It can happen that the physical address of new_memmap lands in memory | |
354 | * which is not mapped in the EFI page table. Therefore we need to go | |
355 | * and ident-map those pages containing the map before calling | |
356 | * phys_efi_set_virtual_address_map(). | |
357 | */ | |
edc3b912 | 358 | pfn = pa_memmap >> PAGE_SHIFT; |
38eecccd TL |
359 | pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC; |
360 | if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) { | |
b7b898ae BP |
361 | pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap); |
362 | return 1; | |
363 | } | |
364 | ||
365 | efi_scratch.use_pgd = true; | |
366 | ||
bf29bddf JK |
367 | /* |
368 | * Certain firmware versions are way too sentimential and still believe | |
369 | * they are exclusive and unquestionable owners of the first physical page, | |
370 | * even though they explicitly mark it as EFI_CONVENTIONAL_MEMORY | |
371 | * (but then write-access it later during SetVirtualAddressMap()). | |
372 | * | |
373 | * Create a 1:1 mapping for this page, to avoid triple faults during early | |
374 | * boot with such firmware. We are free to hand this page to the BIOS, | |
375 | * as trim_bios_range() will reserve the first page and isolate it away | |
376 | * from memory allocators anyway. | |
377 | */ | |
1379edd5 TL |
378 | pf = _PAGE_RW; |
379 | if (sev_active()) | |
380 | pf |= _PAGE_ENC; | |
381 | ||
382 | if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) { | |
bf29bddf JK |
383 | pr_err("Failed to create 1:1 mapping for the first page!\n"); |
384 | return 1; | |
385 | } | |
386 | ||
4f9dbcfc MF |
387 | /* |
388 | * When making calls to the firmware everything needs to be 1:1 | |
389 | * mapped and addressable with 32-bit pointers. Map the kernel | |
390 | * text and allocate a new stack because we can't rely on the | |
391 | * stack pointer being < 4GB. | |
392 | */ | |
12976670 | 393 | if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native()) |
994448f1 | 394 | return 0; |
4f9dbcfc MF |
395 | |
396 | page = alloc_page(GFP_KERNEL|__GFP_DMA32); | |
397 | if (!page) | |
398 | panic("Unable to allocate EFI runtime stack < 4GB\n"); | |
399 | ||
400 | efi_scratch.phys_stack = virt_to_phys(page_address(page)); | |
401 | efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */ | |
402 | ||
2ad510dc | 403 | npages = (_etext - _text) >> PAGE_SHIFT; |
4f9dbcfc | 404 | text = __pa(_text); |
edc3b912 | 405 | pfn = text >> PAGE_SHIFT; |
4f9dbcfc | 406 | |
38eecccd TL |
407 | pf = _PAGE_RW | _PAGE_ENC; |
408 | if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) { | |
4f9dbcfc | 409 | pr_err("Failed to map kernel text 1:1\n"); |
994448f1 | 410 | return 1; |
4f9dbcfc | 411 | } |
b7b898ae BP |
412 | |
413 | return 0; | |
414 | } | |
415 | ||
d2f7cbe7 BP |
416 | static void __init __map_region(efi_memory_desc_t *md, u64 va) |
417 | { | |
15f003d2 | 418 | unsigned long flags = _PAGE_RW; |
edc3b912 | 419 | unsigned long pfn; |
67a9108e | 420 | pgd_t *pgd = efi_pgd; |
d2f7cbe7 BP |
421 | |
422 | if (!(md->attribute & EFI_MEMORY_WB)) | |
edc3b912 | 423 | flags |= _PAGE_PCD; |
d2f7cbe7 | 424 | |
1379edd5 TL |
425 | if (sev_active()) |
426 | flags |= _PAGE_ENC; | |
427 | ||
edc3b912 MF |
428 | pfn = md->phys_addr >> PAGE_SHIFT; |
429 | if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags)) | |
d2f7cbe7 BP |
430 | pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n", |
431 | md->phys_addr, va); | |
432 | } | |
433 | ||
434 | void __init efi_map_region(efi_memory_desc_t *md) | |
435 | { | |
436 | unsigned long size = md->num_pages << PAGE_SHIFT; | |
437 | u64 pa = md->phys_addr; | |
438 | ||
439 | if (efi_enabled(EFI_OLD_MEMMAP)) | |
440 | return old_map_region(md); | |
441 | ||
442 | /* | |
443 | * Make sure the 1:1 mappings are present as a catch-all for b0rked | |
444 | * firmware which doesn't update all internal pointers after switching | |
445 | * to virtual mode and would otherwise crap on us. | |
446 | */ | |
447 | __map_region(md, md->phys_addr); | |
448 | ||
4f9dbcfc MF |
449 | /* |
450 | * Enforce the 1:1 mapping as the default virtual address when | |
451 | * booting in EFI mixed mode, because even though we may be | |
452 | * running a 64-bit kernel, the firmware may only be 32-bit. | |
453 | */ | |
454 | if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) { | |
455 | md->virt_addr = md->phys_addr; | |
456 | return; | |
457 | } | |
458 | ||
d2f7cbe7 BP |
459 | efi_va -= size; |
460 | ||
461 | /* Is PA 2M-aligned? */ | |
462 | if (!(pa & (PMD_SIZE - 1))) { | |
463 | efi_va &= PMD_MASK; | |
464 | } else { | |
465 | u64 pa_offset = pa & (PMD_SIZE - 1); | |
466 | u64 prev_va = efi_va; | |
467 | ||
468 | /* get us the same offset within this 2M page */ | |
469 | efi_va = (efi_va & PMD_MASK) + pa_offset; | |
470 | ||
471 | if (efi_va > prev_va) | |
472 | efi_va -= PMD_SIZE; | |
473 | } | |
474 | ||
475 | if (efi_va < EFI_VA_END) { | |
476 | pr_warn(FW_WARN "VA address range overflow!\n"); | |
477 | return; | |
478 | } | |
479 | ||
480 | /* Do the VA map */ | |
481 | __map_region(md, efi_va); | |
482 | md->virt_addr = efi_va; | |
483 | } | |
484 | ||
3b266496 DY |
485 | /* |
486 | * kexec kernel will use efi_map_region_fixed to map efi runtime memory ranges. | |
487 | * md->virt_addr is the original virtual address which had been mapped in kexec | |
488 | * 1st kernel. | |
489 | */ | |
490 | void __init efi_map_region_fixed(efi_memory_desc_t *md) | |
491 | { | |
0513fe1d | 492 | __map_region(md, md->phys_addr); |
3b266496 DY |
493 | __map_region(md, md->virt_addr); |
494 | } | |
495 | ||
e1ad783b | 496 | void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size, |
3e8fa263 | 497 | u32 type, u64 attribute) |
e1ad783b KP |
498 | { |
499 | unsigned long last_map_pfn; | |
500 | ||
501 | if (type == EFI_MEMORY_MAPPED_IO) | |
502 | return ioremap(phys_addr, size); | |
503 | ||
504 | last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size); | |
505 | if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) { | |
506 | unsigned long top = last_map_pfn << PAGE_SHIFT; | |
3e8fa263 | 507 | efi_ioremap(top, size - (top - phys_addr), type, attribute); |
e1ad783b KP |
508 | } |
509 | ||
3e8fa263 MF |
510 | if (!(attribute & EFI_MEMORY_WB)) |
511 | efi_memory_uc((u64)(unsigned long)__va(phys_addr), size); | |
512 | ||
e1ad783b KP |
513 | return (void __iomem *)__va(phys_addr); |
514 | } | |
1fec0533 DY |
515 | |
516 | void __init parse_efi_setup(u64 phys_addr, u32 data_len) | |
517 | { | |
518 | efi_setup = phys_addr + sizeof(struct setup_data); | |
1fec0533 | 519 | } |
c55d016f | 520 | |
18141e89 | 521 | static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf) |
c55d016f | 522 | { |
6d0cc887 SP |
523 | unsigned long pfn; |
524 | pgd_t *pgd = efi_pgd; | |
18141e89 SP |
525 | int err1, err2; |
526 | ||
527 | /* Update the 1:1 mapping */ | |
528 | pfn = md->phys_addr >> PAGE_SHIFT; | |
529 | err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf); | |
530 | if (err1) { | |
531 | pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n", | |
532 | md->phys_addr, md->virt_addr); | |
533 | } | |
534 | ||
535 | err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf); | |
536 | if (err2) { | |
537 | pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n", | |
538 | md->phys_addr, md->virt_addr); | |
539 | } | |
540 | ||
541 | return err1 || err2; | |
542 | } | |
543 | ||
544 | static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md) | |
545 | { | |
546 | unsigned long pf = 0; | |
547 | ||
548 | if (md->attribute & EFI_MEMORY_XP) | |
549 | pf |= _PAGE_NX; | |
550 | ||
551 | if (!(md->attribute & EFI_MEMORY_RO)) | |
552 | pf |= _PAGE_RW; | |
553 | ||
1379edd5 TL |
554 | if (sev_active()) |
555 | pf |= _PAGE_ENC; | |
556 | ||
18141e89 SP |
557 | return efi_update_mappings(md, pf); |
558 | } | |
559 | ||
560 | void __init efi_runtime_update_mappings(void) | |
561 | { | |
6d0cc887 | 562 | efi_memory_desc_t *md; |
6d0cc887 SP |
563 | |
564 | if (efi_enabled(EFI_OLD_MEMMAP)) { | |
565 | if (__supported_pte_mask & _PAGE_NX) | |
566 | runtime_code_page_mkexec(); | |
567 | return; | |
568 | } | |
569 | ||
18141e89 SP |
570 | /* |
571 | * Use the EFI Memory Attribute Table for mapping permissions if it | |
572 | * exists, since it is intended to supersede EFI_PROPERTIES_TABLE. | |
573 | */ | |
574 | if (efi_enabled(EFI_MEM_ATTR)) { | |
575 | efi_memattr_apply_permissions(NULL, efi_update_mem_attr); | |
576 | return; | |
577 | } | |
578 | ||
579 | /* | |
580 | * EFI_MEMORY_ATTRIBUTES_TABLE is intended to replace | |
581 | * EFI_PROPERTIES_TABLE. So, use EFI_PROPERTIES_TABLE to update | |
582 | * permissions only if EFI_MEMORY_ATTRIBUTES_TABLE is not | |
583 | * published by the firmware. Even if we find a buggy implementation of | |
584 | * EFI_MEMORY_ATTRIBUTES_TABLE, don't fall back to | |
585 | * EFI_PROPERTIES_TABLE, because of the same reason. | |
586 | */ | |
587 | ||
6d0cc887 | 588 | if (!efi_enabled(EFI_NX_PE_DATA)) |
c55d016f BP |
589 | return; |
590 | ||
78ce248f | 591 | for_each_efi_memory_desc(md) { |
6d0cc887 | 592 | unsigned long pf = 0; |
6d0cc887 SP |
593 | |
594 | if (!(md->attribute & EFI_MEMORY_RUNTIME)) | |
595 | continue; | |
596 | ||
597 | if (!(md->attribute & EFI_MEMORY_WB)) | |
598 | pf |= _PAGE_PCD; | |
599 | ||
600 | if ((md->attribute & EFI_MEMORY_XP) || | |
601 | (md->type == EFI_RUNTIME_SERVICES_DATA)) | |
602 | pf |= _PAGE_NX; | |
603 | ||
604 | if (!(md->attribute & EFI_MEMORY_RO) && | |
605 | (md->type != EFI_RUNTIME_SERVICES_CODE)) | |
606 | pf |= _PAGE_RW; | |
607 | ||
1379edd5 TL |
608 | if (sev_active()) |
609 | pf |= _PAGE_ENC; | |
610 | ||
18141e89 | 611 | efi_update_mappings(md, pf); |
6d0cc887 | 612 | } |
c55d016f | 613 | } |
11cc8512 BP |
614 | |
615 | void __init efi_dump_pagetable(void) | |
616 | { | |
617 | #ifdef CONFIG_EFI_PGT_DUMP | |
ac81d3de SP |
618 | if (efi_enabled(EFI_OLD_MEMMAP)) |
619 | ptdump_walk_pgd_level(NULL, swapper_pg_dir); | |
620 | else | |
621 | ptdump_walk_pgd_level(NULL, efi_pgd); | |
11cc8512 BP |
622 | #endif |
623 | } | |
994448f1 | 624 | |
4f9dbcfc MF |
625 | #ifdef CONFIG_EFI_MIXED |
626 | extern efi_status_t efi64_thunk(u32, ...); | |
627 | ||
628 | #define runtime_service32(func) \ | |
629 | ({ \ | |
630 | u32 table = (u32)(unsigned long)efi.systab; \ | |
631 | u32 *rt, *___f; \ | |
632 | \ | |
633 | rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \ | |
634 | ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \ | |
635 | *___f; \ | |
636 | }) | |
637 | ||
638 | /* | |
639 | * Switch to the EFI page tables early so that we can access the 1:1 | |
640 | * runtime services mappings which are not mapped in any other page | |
641 | * tables. This function must be called before runtime_service32(). | |
642 | * | |
643 | * Also, disable interrupts because the IDT points to 64-bit handlers, | |
644 | * which aren't going to function correctly when we switch to 32-bit. | |
645 | */ | |
646 | #define efi_thunk(f, ...) \ | |
647 | ({ \ | |
648 | efi_status_t __s; \ | |
21f86625 AT |
649 | unsigned long __flags; \ |
650 | u32 __func; \ | |
4f9dbcfc | 651 | \ |
21f86625 AT |
652 | local_irq_save(__flags); \ |
653 | arch_efi_call_virt_setup(); \ | |
4f9dbcfc | 654 | \ |
21f86625 AT |
655 | __func = runtime_service32(f); \ |
656 | __s = efi64_thunk(__func, __VA_ARGS__); \ | |
4f9dbcfc | 657 | \ |
21f86625 AT |
658 | arch_efi_call_virt_teardown(); \ |
659 | local_irq_restore(__flags); \ | |
4f9dbcfc MF |
660 | \ |
661 | __s; \ | |
662 | }) | |
663 | ||
664 | efi_status_t efi_thunk_set_virtual_address_map( | |
665 | void *phys_set_virtual_address_map, | |
666 | unsigned long memory_map_size, | |
667 | unsigned long descriptor_size, | |
668 | u32 descriptor_version, | |
669 | efi_memory_desc_t *virtual_map) | |
670 | { | |
671 | efi_status_t status; | |
672 | unsigned long flags; | |
673 | u32 func; | |
674 | ||
675 | efi_sync_low_kernel_mappings(); | |
676 | local_irq_save(flags); | |
677 | ||
6c690ee1 | 678 | efi_scratch.prev_cr3 = __read_cr3(); |
4f9dbcfc MF |
679 | write_cr3((unsigned long)efi_scratch.efi_pgt); |
680 | __flush_tlb_all(); | |
681 | ||
682 | func = (u32)(unsigned long)phys_set_virtual_address_map; | |
683 | status = efi64_thunk(func, memory_map_size, descriptor_size, | |
684 | descriptor_version, virtual_map); | |
685 | ||
686 | write_cr3(efi_scratch.prev_cr3); | |
687 | __flush_tlb_all(); | |
688 | local_irq_restore(flags); | |
689 | ||
690 | return status; | |
691 | } | |
692 | ||
693 | static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc) | |
694 | { | |
695 | efi_status_t status; | |
696 | u32 phys_tm, phys_tc; | |
697 | ||
698 | spin_lock(&rtc_lock); | |
699 | ||
f6697df3 MF |
700 | phys_tm = virt_to_phys_or_null(tm); |
701 | phys_tc = virt_to_phys_or_null(tc); | |
4f9dbcfc MF |
702 | |
703 | status = efi_thunk(get_time, phys_tm, phys_tc); | |
704 | ||
705 | spin_unlock(&rtc_lock); | |
706 | ||
707 | return status; | |
708 | } | |
709 | ||
710 | static efi_status_t efi_thunk_set_time(efi_time_t *tm) | |
711 | { | |
712 | efi_status_t status; | |
713 | u32 phys_tm; | |
714 | ||
715 | spin_lock(&rtc_lock); | |
716 | ||
f6697df3 | 717 | phys_tm = virt_to_phys_or_null(tm); |
4f9dbcfc MF |
718 | |
719 | status = efi_thunk(set_time, phys_tm); | |
720 | ||
721 | spin_unlock(&rtc_lock); | |
722 | ||
723 | return status; | |
724 | } | |
725 | ||
726 | static efi_status_t | |
727 | efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending, | |
728 | efi_time_t *tm) | |
729 | { | |
730 | efi_status_t status; | |
731 | u32 phys_enabled, phys_pending, phys_tm; | |
732 | ||
733 | spin_lock(&rtc_lock); | |
734 | ||
f6697df3 MF |
735 | phys_enabled = virt_to_phys_or_null(enabled); |
736 | phys_pending = virt_to_phys_or_null(pending); | |
737 | phys_tm = virt_to_phys_or_null(tm); | |
4f9dbcfc MF |
738 | |
739 | status = efi_thunk(get_wakeup_time, phys_enabled, | |
740 | phys_pending, phys_tm); | |
741 | ||
742 | spin_unlock(&rtc_lock); | |
743 | ||
744 | return status; | |
745 | } | |
746 | ||
747 | static efi_status_t | |
748 | efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) | |
749 | { | |
750 | efi_status_t status; | |
751 | u32 phys_tm; | |
752 | ||
753 | spin_lock(&rtc_lock); | |
754 | ||
f6697df3 | 755 | phys_tm = virt_to_phys_or_null(tm); |
4f9dbcfc MF |
756 | |
757 | status = efi_thunk(set_wakeup_time, enabled, phys_tm); | |
758 | ||
759 | spin_unlock(&rtc_lock); | |
760 | ||
761 | return status; | |
762 | } | |
763 | ||
f6697df3 MF |
764 | static unsigned long efi_name_size(efi_char16_t *name) |
765 | { | |
766 | return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1; | |
767 | } | |
4f9dbcfc MF |
768 | |
769 | static efi_status_t | |
770 | efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor, | |
771 | u32 *attr, unsigned long *data_size, void *data) | |
772 | { | |
773 | efi_status_t status; | |
774 | u32 phys_name, phys_vendor, phys_attr; | |
775 | u32 phys_data_size, phys_data; | |
776 | ||
f6697df3 MF |
777 | phys_data_size = virt_to_phys_or_null(data_size); |
778 | phys_vendor = virt_to_phys_or_null(vendor); | |
779 | phys_name = virt_to_phys_or_null_size(name, efi_name_size(name)); | |
780 | phys_attr = virt_to_phys_or_null(attr); | |
781 | phys_data = virt_to_phys_or_null_size(data, *data_size); | |
4f9dbcfc MF |
782 | |
783 | status = efi_thunk(get_variable, phys_name, phys_vendor, | |
784 | phys_attr, phys_data_size, phys_data); | |
785 | ||
786 | return status; | |
787 | } | |
788 | ||
789 | static efi_status_t | |
790 | efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor, | |
791 | u32 attr, unsigned long data_size, void *data) | |
792 | { | |
793 | u32 phys_name, phys_vendor, phys_data; | |
794 | efi_status_t status; | |
795 | ||
f6697df3 MF |
796 | phys_name = virt_to_phys_or_null_size(name, efi_name_size(name)); |
797 | phys_vendor = virt_to_phys_or_null(vendor); | |
798 | phys_data = virt_to_phys_or_null_size(data, data_size); | |
4f9dbcfc MF |
799 | |
800 | /* If data_size is > sizeof(u32) we've got problems */ | |
801 | status = efi_thunk(set_variable, phys_name, phys_vendor, | |
802 | attr, data_size, phys_data); | |
803 | ||
804 | return status; | |
805 | } | |
806 | ||
807 | static efi_status_t | |
808 | efi_thunk_get_next_variable(unsigned long *name_size, | |
809 | efi_char16_t *name, | |
810 | efi_guid_t *vendor) | |
811 | { | |
812 | efi_status_t status; | |
813 | u32 phys_name_size, phys_name, phys_vendor; | |
814 | ||
f6697df3 MF |
815 | phys_name_size = virt_to_phys_or_null(name_size); |
816 | phys_vendor = virt_to_phys_or_null(vendor); | |
817 | phys_name = virt_to_phys_or_null_size(name, *name_size); | |
4f9dbcfc MF |
818 | |
819 | status = efi_thunk(get_next_variable, phys_name_size, | |
820 | phys_name, phys_vendor); | |
821 | ||
822 | return status; | |
823 | } | |
824 | ||
825 | static efi_status_t | |
826 | efi_thunk_get_next_high_mono_count(u32 *count) | |
827 | { | |
828 | efi_status_t status; | |
829 | u32 phys_count; | |
830 | ||
f6697df3 | 831 | phys_count = virt_to_phys_or_null(count); |
4f9dbcfc MF |
832 | status = efi_thunk(get_next_high_mono_count, phys_count); |
833 | ||
834 | return status; | |
835 | } | |
836 | ||
837 | static void | |
838 | efi_thunk_reset_system(int reset_type, efi_status_t status, | |
839 | unsigned long data_size, efi_char16_t *data) | |
840 | { | |
841 | u32 phys_data; | |
842 | ||
f6697df3 | 843 | phys_data = virt_to_phys_or_null_size(data, data_size); |
4f9dbcfc MF |
844 | |
845 | efi_thunk(reset_system, reset_type, status, data_size, phys_data); | |
846 | } | |
847 | ||
848 | static efi_status_t | |
849 | efi_thunk_update_capsule(efi_capsule_header_t **capsules, | |
850 | unsigned long count, unsigned long sg_list) | |
851 | { | |
852 | /* | |
853 | * To properly support this function we would need to repackage | |
854 | * 'capsules' because the firmware doesn't understand 64-bit | |
855 | * pointers. | |
856 | */ | |
857 | return EFI_UNSUPPORTED; | |
858 | } | |
859 | ||
860 | static efi_status_t | |
861 | efi_thunk_query_variable_info(u32 attr, u64 *storage_space, | |
862 | u64 *remaining_space, | |
863 | u64 *max_variable_size) | |
864 | { | |
865 | efi_status_t status; | |
866 | u32 phys_storage, phys_remaining, phys_max; | |
867 | ||
868 | if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION) | |
869 | return EFI_UNSUPPORTED; | |
870 | ||
f6697df3 MF |
871 | phys_storage = virt_to_phys_or_null(storage_space); |
872 | phys_remaining = virt_to_phys_or_null(remaining_space); | |
873 | phys_max = virt_to_phys_or_null(max_variable_size); | |
4f9dbcfc | 874 | |
9a11040f | 875 | status = efi_thunk(query_variable_info, attr, phys_storage, |
4f9dbcfc MF |
876 | phys_remaining, phys_max); |
877 | ||
878 | return status; | |
879 | } | |
880 | ||
881 | static efi_status_t | |
882 | efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules, | |
883 | unsigned long count, u64 *max_size, | |
884 | int *reset_type) | |
885 | { | |
886 | /* | |
887 | * To properly support this function we would need to repackage | |
888 | * 'capsules' because the firmware doesn't understand 64-bit | |
889 | * pointers. | |
890 | */ | |
891 | return EFI_UNSUPPORTED; | |
892 | } | |
893 | ||
894 | void efi_thunk_runtime_setup(void) | |
895 | { | |
896 | efi.get_time = efi_thunk_get_time; | |
897 | efi.set_time = efi_thunk_set_time; | |
898 | efi.get_wakeup_time = efi_thunk_get_wakeup_time; | |
899 | efi.set_wakeup_time = efi_thunk_set_wakeup_time; | |
900 | efi.get_variable = efi_thunk_get_variable; | |
901 | efi.get_next_variable = efi_thunk_get_next_variable; | |
902 | efi.set_variable = efi_thunk_set_variable; | |
903 | efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count; | |
904 | efi.reset_system = efi_thunk_reset_system; | |
905 | efi.query_variable_info = efi_thunk_query_variable_info; | |
906 | efi.update_capsule = efi_thunk_update_capsule; | |
907 | efi.query_capsule_caps = efi_thunk_query_capsule_caps; | |
908 | } | |
909 | #endif /* CONFIG_EFI_MIXED */ |