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