This source file includes following definitions.
- early_code_mapping_set_exec
- efi_call_phys_prolog
- efi_call_phys_epilog
- efi_alloc_page_tables
- efi_sync_low_kernel_mappings
- virt_to_phys_or_null_size
- efi_setup_page_tables
- __map_region
- efi_map_region
- efi_map_region_fixed
- efi_ioremap
- parse_efi_setup
- efi_update_mappings
- efi_update_mem_attr
- efi_runtime_update_mappings
- efi_dump_pagetable
- efi_switch_mm
- efi_thunk_set_virtual_address_map
- efi_thunk_get_time
- efi_thunk_set_time
- efi_thunk_get_wakeup_time
- efi_thunk_set_wakeup_time
- efi_name_size
- efi_thunk_get_variable
- efi_thunk_set_variable
- efi_thunk_set_variable_nonblocking
- efi_thunk_get_next_variable
- efi_thunk_get_next_high_mono_count
- efi_thunk_reset_system
- efi_thunk_update_capsule
- efi_thunk_query_variable_info
- efi_thunk_query_variable_info_nonblocking
- efi_thunk_query_capsule_caps
- efi_thunk_runtime_setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #define pr_fmt(fmt) "efi: " fmt
20
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/memblock.h>
27 #include <linux/ioport.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/efi.h>
30 #include <linux/export.h>
31 #include <linux/uaccess.h>
32 #include <linux/io.h>
33 #include <linux/reboot.h>
34 #include <linux/slab.h>
35 #include <linux/ucs2_string.h>
36 #include <linux/mem_encrypt.h>
37 #include <linux/sched/task.h>
38
39 #include <asm/setup.h>
40 #include <asm/page.h>
41 #include <asm/e820/api.h>
42 #include <asm/pgtable.h>
43 #include <asm/tlbflush.h>
44 #include <asm/proto.h>
45 #include <asm/efi.h>
46 #include <asm/cacheflush.h>
47 #include <asm/fixmap.h>
48 #include <asm/realmode.h>
49 #include <asm/time.h>
50 #include <asm/pgalloc.h>
51
52
53
54
55
56 static u64 efi_va = EFI_VA_START;
57
58 struct efi_scratch efi_scratch;
59
60 static void __init early_code_mapping_set_exec(int executable)
61 {
62 efi_memory_desc_t *md;
63
64 if (!(__supported_pte_mask & _PAGE_NX))
65 return;
66
67
68 for_each_efi_memory_desc(md) {
69 if (md->type == EFI_RUNTIME_SERVICES_CODE ||
70 md->type == EFI_BOOT_SERVICES_CODE)
71 efi_set_executable(md, executable);
72 }
73 }
74
75 pgd_t * __init efi_call_phys_prolog(void)
76 {
77 unsigned long vaddr, addr_pgd, addr_p4d, addr_pud;
78 pgd_t *save_pgd, *pgd_k, *pgd_efi;
79 p4d_t *p4d, *p4d_k, *p4d_efi;
80 pud_t *pud;
81
82 int pgd;
83 int n_pgds, i, j;
84
85 if (!efi_enabled(EFI_OLD_MEMMAP)) {
86 efi_switch_mm(&efi_mm);
87 return efi_mm.pgd;
88 }
89
90 early_code_mapping_set_exec(1);
91
92 n_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT), PGDIR_SIZE);
93 save_pgd = kmalloc_array(n_pgds, sizeof(*save_pgd), GFP_KERNEL);
94 if (!save_pgd)
95 return NULL;
96
97
98
99
100
101
102
103
104
105 for (pgd = 0; pgd < n_pgds; pgd++) {
106 addr_pgd = (unsigned long)(pgd * PGDIR_SIZE);
107 vaddr = (unsigned long)__va(pgd * PGDIR_SIZE);
108 pgd_efi = pgd_offset_k(addr_pgd);
109 save_pgd[pgd] = *pgd_efi;
110
111 p4d = p4d_alloc(&init_mm, pgd_efi, addr_pgd);
112 if (!p4d) {
113 pr_err("Failed to allocate p4d table!\n");
114 goto out;
115 }
116
117 for (i = 0; i < PTRS_PER_P4D; i++) {
118 addr_p4d = addr_pgd + i * P4D_SIZE;
119 p4d_efi = p4d + p4d_index(addr_p4d);
120
121 pud = pud_alloc(&init_mm, p4d_efi, addr_p4d);
122 if (!pud) {
123 pr_err("Failed to allocate pud table!\n");
124 goto out;
125 }
126
127 for (j = 0; j < PTRS_PER_PUD; j++) {
128 addr_pud = addr_p4d + j * PUD_SIZE;
129
130 if (addr_pud > (max_pfn << PAGE_SHIFT))
131 break;
132
133 vaddr = (unsigned long)__va(addr_pud);
134
135 pgd_k = pgd_offset_k(vaddr);
136 p4d_k = p4d_offset(pgd_k, vaddr);
137 pud[j] = *pud_offset(p4d_k, vaddr);
138 }
139 }
140 pgd_offset_k(pgd * PGDIR_SIZE)->pgd &= ~_PAGE_NX;
141 }
142
143 __flush_tlb_all();
144 return save_pgd;
145 out:
146 efi_call_phys_epilog(save_pgd);
147 return NULL;
148 }
149
150 void __init efi_call_phys_epilog(pgd_t *save_pgd)
151 {
152
153
154
155 int pgd_idx, i;
156 int nr_pgds;
157 pgd_t *pgd;
158 p4d_t *p4d;
159 pud_t *pud;
160
161 if (!efi_enabled(EFI_OLD_MEMMAP)) {
162 efi_switch_mm(efi_scratch.prev_mm);
163 return;
164 }
165
166 nr_pgds = DIV_ROUND_UP((max_pfn << PAGE_SHIFT) , PGDIR_SIZE);
167
168 for (pgd_idx = 0; pgd_idx < nr_pgds; pgd_idx++) {
169 pgd = pgd_offset_k(pgd_idx * PGDIR_SIZE);
170 set_pgd(pgd_offset_k(pgd_idx * PGDIR_SIZE), save_pgd[pgd_idx]);
171
172 if (!pgd_present(*pgd))
173 continue;
174
175 for (i = 0; i < PTRS_PER_P4D; i++) {
176 p4d = p4d_offset(pgd,
177 pgd_idx * PGDIR_SIZE + i * P4D_SIZE);
178
179 if (!p4d_present(*p4d))
180 continue;
181
182 pud = (pud_t *)p4d_page_vaddr(*p4d);
183 pud_free(&init_mm, pud);
184 }
185
186 p4d = (p4d_t *)pgd_page_vaddr(*pgd);
187 p4d_free(&init_mm, p4d);
188 }
189
190 kfree(save_pgd);
191
192 __flush_tlb_all();
193 early_code_mapping_set_exec(0);
194 }
195
196 EXPORT_SYMBOL_GPL(efi_mm);
197
198
199
200
201
202
203
204
205
206
207 int __init efi_alloc_page_tables(void)
208 {
209 pgd_t *pgd, *efi_pgd;
210 p4d_t *p4d;
211 pud_t *pud;
212 gfp_t gfp_mask;
213
214 if (efi_enabled(EFI_OLD_MEMMAP))
215 return 0;
216
217 gfp_mask = GFP_KERNEL | __GFP_ZERO;
218 efi_pgd = (pgd_t *)__get_free_pages(gfp_mask, PGD_ALLOCATION_ORDER);
219 if (!efi_pgd)
220 return -ENOMEM;
221
222 pgd = efi_pgd + pgd_index(EFI_VA_END);
223 p4d = p4d_alloc(&init_mm, pgd, EFI_VA_END);
224 if (!p4d) {
225 free_page((unsigned long)efi_pgd);
226 return -ENOMEM;
227 }
228
229 pud = pud_alloc(&init_mm, p4d, EFI_VA_END);
230 if (!pud) {
231 if (pgtable_l5_enabled())
232 free_page((unsigned long) pgd_page_vaddr(*pgd));
233 free_pages((unsigned long)efi_pgd, PGD_ALLOCATION_ORDER);
234 return -ENOMEM;
235 }
236
237 efi_mm.pgd = efi_pgd;
238 mm_init_cpumask(&efi_mm);
239 init_new_context(NULL, &efi_mm);
240
241 return 0;
242 }
243
244
245
246
247 void efi_sync_low_kernel_mappings(void)
248 {
249 unsigned num_entries;
250 pgd_t *pgd_k, *pgd_efi;
251 p4d_t *p4d_k, *p4d_efi;
252 pud_t *pud_k, *pud_efi;
253 pgd_t *efi_pgd = efi_mm.pgd;
254
255 if (efi_enabled(EFI_OLD_MEMMAP))
256 return;
257
258
259
260
261
262
263
264
265
266 MAYBE_BUILD_BUG_ON(pgd_index(EFI_VA_END) != pgd_index(MODULES_END));
267 MAYBE_BUILD_BUG_ON((EFI_VA_START & PGDIR_MASK) !=
268 (EFI_VA_END & PGDIR_MASK));
269
270 pgd_efi = efi_pgd + pgd_index(PAGE_OFFSET);
271 pgd_k = pgd_offset_k(PAGE_OFFSET);
272
273 num_entries = pgd_index(EFI_VA_END) - pgd_index(PAGE_OFFSET);
274 memcpy(pgd_efi, pgd_k, sizeof(pgd_t) * num_entries);
275
276
277
278
279
280 BUILD_BUG_ON(p4d_index(EFI_VA_END) != p4d_index(MODULES_END));
281 BUILD_BUG_ON((EFI_VA_START & P4D_MASK) != (EFI_VA_END & P4D_MASK));
282
283 pgd_efi = efi_pgd + pgd_index(EFI_VA_END);
284 pgd_k = pgd_offset_k(EFI_VA_END);
285 p4d_efi = p4d_offset(pgd_efi, 0);
286 p4d_k = p4d_offset(pgd_k, 0);
287
288 num_entries = p4d_index(EFI_VA_END);
289 memcpy(p4d_efi, p4d_k, sizeof(p4d_t) * num_entries);
290
291
292
293
294
295 BUILD_BUG_ON((EFI_VA_START & ~PUD_MASK) != 0);
296 BUILD_BUG_ON((EFI_VA_END & ~PUD_MASK) != 0);
297
298 p4d_efi = p4d_offset(pgd_efi, EFI_VA_END);
299 p4d_k = p4d_offset(pgd_k, EFI_VA_END);
300 pud_efi = pud_offset(p4d_efi, 0);
301 pud_k = pud_offset(p4d_k, 0);
302
303 num_entries = pud_index(EFI_VA_END);
304 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
305
306 pud_efi = pud_offset(p4d_efi, EFI_VA_START);
307 pud_k = pud_offset(p4d_k, EFI_VA_START);
308
309 num_entries = PTRS_PER_PUD - pud_index(EFI_VA_START);
310 memcpy(pud_efi, pud_k, sizeof(pud_t) * num_entries);
311 }
312
313
314
315
316 static inline phys_addr_t
317 virt_to_phys_or_null_size(void *va, unsigned long size)
318 {
319 phys_addr_t pa;
320
321 if (!va)
322 return 0;
323
324 if (virt_addr_valid(va))
325 return virt_to_phys(va);
326
327 pa = slow_virt_to_phys(va);
328
329
330 if (WARN_ON((pa ^ (pa + size - 1)) & PAGE_MASK))
331 return 0;
332
333 return pa;
334 }
335
336 #define virt_to_phys_or_null(addr) \
337 virt_to_phys_or_null_size((addr), sizeof(*(addr)))
338
339 int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
340 {
341 unsigned long pfn, text, pf;
342 struct page *page;
343 unsigned npages;
344 pgd_t *pgd = efi_mm.pgd;
345
346 if (efi_enabled(EFI_OLD_MEMMAP))
347 return 0;
348
349
350
351
352
353
354
355 pfn = pa_memmap >> PAGE_SHIFT;
356 pf = _PAGE_NX | _PAGE_RW | _PAGE_ENC;
357 if (kernel_map_pages_in_pgd(pgd, pfn, pa_memmap, num_pages, pf)) {
358 pr_err("Error ident-mapping new memmap (0x%lx)!\n", pa_memmap);
359 return 1;
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373 pf = _PAGE_RW;
374 if (sev_active())
375 pf |= _PAGE_ENC;
376
377 if (kernel_map_pages_in_pgd(pgd, 0x0, 0x0, 1, pf)) {
378 pr_err("Failed to create 1:1 mapping for the first page!\n");
379 return 1;
380 }
381
382
383
384
385
386
387
388 if (!IS_ENABLED(CONFIG_EFI_MIXED) || efi_is_native())
389 return 0;
390
391 page = alloc_page(GFP_KERNEL|__GFP_DMA32);
392 if (!page) {
393 pr_err("Unable to allocate EFI runtime stack < 4GB\n");
394 return 1;
395 }
396
397 efi_scratch.phys_stack = page_to_phys(page + 1);
398
399 npages = (_etext - _text) >> PAGE_SHIFT;
400 text = __pa(_text);
401 pfn = text >> PAGE_SHIFT;
402
403 pf = _PAGE_RW | _PAGE_ENC;
404 if (kernel_map_pages_in_pgd(pgd, pfn, text, npages, pf)) {
405 pr_err("Failed to map kernel text 1:1\n");
406 return 1;
407 }
408
409 return 0;
410 }
411
412 static void __init __map_region(efi_memory_desc_t *md, u64 va)
413 {
414 unsigned long flags = _PAGE_RW;
415 unsigned long pfn;
416 pgd_t *pgd = efi_mm.pgd;
417
418 if (!(md->attribute & EFI_MEMORY_WB))
419 flags |= _PAGE_PCD;
420
421 if (sev_active() && md->type != EFI_MEMORY_MAPPED_IO)
422 flags |= _PAGE_ENC;
423
424 pfn = md->phys_addr >> PAGE_SHIFT;
425 if (kernel_map_pages_in_pgd(pgd, pfn, va, md->num_pages, flags))
426 pr_warn("Error mapping PA 0x%llx -> VA 0x%llx!\n",
427 md->phys_addr, va);
428 }
429
430 void __init efi_map_region(efi_memory_desc_t *md)
431 {
432 unsigned long size = md->num_pages << PAGE_SHIFT;
433 u64 pa = md->phys_addr;
434
435 if (efi_enabled(EFI_OLD_MEMMAP))
436 return old_map_region(md);
437
438
439
440
441
442
443 __map_region(md, md->phys_addr);
444
445
446
447
448
449
450 if (!efi_is_native () && IS_ENABLED(CONFIG_EFI_MIXED)) {
451 md->virt_addr = md->phys_addr;
452 return;
453 }
454
455 efi_va -= size;
456
457
458 if (!(pa & (PMD_SIZE - 1))) {
459 efi_va &= PMD_MASK;
460 } else {
461 u64 pa_offset = pa & (PMD_SIZE - 1);
462 u64 prev_va = efi_va;
463
464
465 efi_va = (efi_va & PMD_MASK) + pa_offset;
466
467 if (efi_va > prev_va)
468 efi_va -= PMD_SIZE;
469 }
470
471 if (efi_va < EFI_VA_END) {
472 pr_warn(FW_WARN "VA address range overflow!\n");
473 return;
474 }
475
476
477 __map_region(md, efi_va);
478 md->virt_addr = efi_va;
479 }
480
481
482
483
484
485
486 void __init efi_map_region_fixed(efi_memory_desc_t *md)
487 {
488 __map_region(md, md->phys_addr);
489 __map_region(md, md->virt_addr);
490 }
491
492 void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size,
493 u32 type, u64 attribute)
494 {
495 unsigned long last_map_pfn;
496
497 if (type == EFI_MEMORY_MAPPED_IO)
498 return ioremap(phys_addr, size);
499
500 last_map_pfn = init_memory_mapping(phys_addr, phys_addr + size);
501 if ((last_map_pfn << PAGE_SHIFT) < phys_addr + size) {
502 unsigned long top = last_map_pfn << PAGE_SHIFT;
503 efi_ioremap(top, size - (top - phys_addr), type, attribute);
504 }
505
506 if (!(attribute & EFI_MEMORY_WB))
507 efi_memory_uc((u64)(unsigned long)__va(phys_addr), size);
508
509 return (void __iomem *)__va(phys_addr);
510 }
511
512 void __init parse_efi_setup(u64 phys_addr, u32 data_len)
513 {
514 efi_setup = phys_addr + sizeof(struct setup_data);
515 }
516
517 static int __init efi_update_mappings(efi_memory_desc_t *md, unsigned long pf)
518 {
519 unsigned long pfn;
520 pgd_t *pgd = efi_mm.pgd;
521 int err1, err2;
522
523
524 pfn = md->phys_addr >> PAGE_SHIFT;
525 err1 = kernel_map_pages_in_pgd(pgd, pfn, md->phys_addr, md->num_pages, pf);
526 if (err1) {
527 pr_err("Error while updating 1:1 mapping PA 0x%llx -> VA 0x%llx!\n",
528 md->phys_addr, md->virt_addr);
529 }
530
531 err2 = kernel_map_pages_in_pgd(pgd, pfn, md->virt_addr, md->num_pages, pf);
532 if (err2) {
533 pr_err("Error while updating VA mapping PA 0x%llx -> VA 0x%llx!\n",
534 md->phys_addr, md->virt_addr);
535 }
536
537 return err1 || err2;
538 }
539
540 static int __init efi_update_mem_attr(struct mm_struct *mm, efi_memory_desc_t *md)
541 {
542 unsigned long pf = 0;
543
544 if (md->attribute & EFI_MEMORY_XP)
545 pf |= _PAGE_NX;
546
547 if (!(md->attribute & EFI_MEMORY_RO))
548 pf |= _PAGE_RW;
549
550 if (sev_active())
551 pf |= _PAGE_ENC;
552
553 return efi_update_mappings(md, pf);
554 }
555
556 void __init efi_runtime_update_mappings(void)
557 {
558 efi_memory_desc_t *md;
559
560 if (efi_enabled(EFI_OLD_MEMMAP)) {
561 if (__supported_pte_mask & _PAGE_NX)
562 runtime_code_page_mkexec();
563 return;
564 }
565
566
567
568
569
570 if (efi_enabled(EFI_MEM_ATTR)) {
571 efi_memattr_apply_permissions(NULL, efi_update_mem_attr);
572 return;
573 }
574
575
576
577
578
579
580
581
582
583
584 if (!efi_enabled(EFI_NX_PE_DATA))
585 return;
586
587 for_each_efi_memory_desc(md) {
588 unsigned long pf = 0;
589
590 if (!(md->attribute & EFI_MEMORY_RUNTIME))
591 continue;
592
593 if (!(md->attribute & EFI_MEMORY_WB))
594 pf |= _PAGE_PCD;
595
596 if ((md->attribute & EFI_MEMORY_XP) ||
597 (md->type == EFI_RUNTIME_SERVICES_DATA))
598 pf |= _PAGE_NX;
599
600 if (!(md->attribute & EFI_MEMORY_RO) &&
601 (md->type != EFI_RUNTIME_SERVICES_CODE))
602 pf |= _PAGE_RW;
603
604 if (sev_active())
605 pf |= _PAGE_ENC;
606
607 efi_update_mappings(md, pf);
608 }
609 }
610
611 void __init efi_dump_pagetable(void)
612 {
613 #ifdef CONFIG_EFI_PGT_DUMP
614 if (efi_enabled(EFI_OLD_MEMMAP))
615 ptdump_walk_pgd_level(NULL, swapper_pg_dir);
616 else
617 ptdump_walk_pgd_level(NULL, efi_mm.pgd);
618 #endif
619 }
620
621
622
623
624
625
626
627
628 void efi_switch_mm(struct mm_struct *mm)
629 {
630 efi_scratch.prev_mm = current->active_mm;
631 current->active_mm = mm;
632 switch_mm(efi_scratch.prev_mm, mm, NULL);
633 }
634
635 #ifdef CONFIG_EFI_MIXED
636 extern efi_status_t efi64_thunk(u32, ...);
637
638 static DEFINE_SPINLOCK(efi_runtime_lock);
639
640 #define runtime_service32(func) \
641 ({ \
642 u32 table = (u32)(unsigned long)efi.systab; \
643 u32 *rt, *___f; \
644 \
645 rt = (u32 *)(table + offsetof(efi_system_table_32_t, runtime)); \
646 ___f = (u32 *)(*rt + offsetof(efi_runtime_services_32_t, func)); \
647 *___f; \
648 })
649
650
651
652
653
654
655
656
657
658 #define efi_thunk(f, ...) \
659 ({ \
660 efi_status_t __s; \
661 u32 __func; \
662 \
663 arch_efi_call_virt_setup(); \
664 \
665 __func = runtime_service32(f); \
666 __s = efi64_thunk(__func, __VA_ARGS__); \
667 \
668 arch_efi_call_virt_teardown(); \
669 \
670 __s; \
671 })
672
673 efi_status_t efi_thunk_set_virtual_address_map(
674 void *phys_set_virtual_address_map,
675 unsigned long memory_map_size,
676 unsigned long descriptor_size,
677 u32 descriptor_version,
678 efi_memory_desc_t *virtual_map)
679 {
680 efi_status_t status;
681 unsigned long flags;
682 u32 func;
683
684 efi_sync_low_kernel_mappings();
685 local_irq_save(flags);
686
687 efi_switch_mm(&efi_mm);
688
689 func = (u32)(unsigned long)phys_set_virtual_address_map;
690 status = efi64_thunk(func, memory_map_size, descriptor_size,
691 descriptor_version, virtual_map);
692
693 efi_switch_mm(efi_scratch.prev_mm);
694 local_irq_restore(flags);
695
696 return status;
697 }
698
699 static efi_status_t efi_thunk_get_time(efi_time_t *tm, efi_time_cap_t *tc)
700 {
701 efi_status_t status;
702 u32 phys_tm, phys_tc;
703 unsigned long flags;
704
705 spin_lock(&rtc_lock);
706 spin_lock_irqsave(&efi_runtime_lock, flags);
707
708 phys_tm = virt_to_phys_or_null(tm);
709 phys_tc = virt_to_phys_or_null(tc);
710
711 status = efi_thunk(get_time, phys_tm, phys_tc);
712
713 spin_unlock_irqrestore(&efi_runtime_lock, flags);
714 spin_unlock(&rtc_lock);
715
716 return status;
717 }
718
719 static efi_status_t efi_thunk_set_time(efi_time_t *tm)
720 {
721 efi_status_t status;
722 u32 phys_tm;
723 unsigned long flags;
724
725 spin_lock(&rtc_lock);
726 spin_lock_irqsave(&efi_runtime_lock, flags);
727
728 phys_tm = virt_to_phys_or_null(tm);
729
730 status = efi_thunk(set_time, phys_tm);
731
732 spin_unlock_irqrestore(&efi_runtime_lock, flags);
733 spin_unlock(&rtc_lock);
734
735 return status;
736 }
737
738 static efi_status_t
739 efi_thunk_get_wakeup_time(efi_bool_t *enabled, efi_bool_t *pending,
740 efi_time_t *tm)
741 {
742 efi_status_t status;
743 u32 phys_enabled, phys_pending, phys_tm;
744 unsigned long flags;
745
746 spin_lock(&rtc_lock);
747 spin_lock_irqsave(&efi_runtime_lock, flags);
748
749 phys_enabled = virt_to_phys_or_null(enabled);
750 phys_pending = virt_to_phys_or_null(pending);
751 phys_tm = virt_to_phys_or_null(tm);
752
753 status = efi_thunk(get_wakeup_time, phys_enabled,
754 phys_pending, phys_tm);
755
756 spin_unlock_irqrestore(&efi_runtime_lock, flags);
757 spin_unlock(&rtc_lock);
758
759 return status;
760 }
761
762 static efi_status_t
763 efi_thunk_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
764 {
765 efi_status_t status;
766 u32 phys_tm;
767 unsigned long flags;
768
769 spin_lock(&rtc_lock);
770 spin_lock_irqsave(&efi_runtime_lock, flags);
771
772 phys_tm = virt_to_phys_or_null(tm);
773
774 status = efi_thunk(set_wakeup_time, enabled, phys_tm);
775
776 spin_unlock_irqrestore(&efi_runtime_lock, flags);
777 spin_unlock(&rtc_lock);
778
779 return status;
780 }
781
782 static unsigned long efi_name_size(efi_char16_t *name)
783 {
784 return ucs2_strsize(name, EFI_VAR_NAME_LEN) + 1;
785 }
786
787 static efi_status_t
788 efi_thunk_get_variable(efi_char16_t *name, efi_guid_t *vendor,
789 u32 *attr, unsigned long *data_size, void *data)
790 {
791 u8 buf[24] __aligned(8);
792 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
793 efi_status_t status;
794 u32 phys_name, phys_vendor, phys_attr;
795 u32 phys_data_size, phys_data;
796 unsigned long flags;
797
798 spin_lock_irqsave(&efi_runtime_lock, flags);
799
800 *vnd = *vendor;
801
802 phys_data_size = virt_to_phys_or_null(data_size);
803 phys_vendor = virt_to_phys_or_null(vnd);
804 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
805 phys_attr = virt_to_phys_or_null(attr);
806 phys_data = virt_to_phys_or_null_size(data, *data_size);
807
808 if (!phys_name || (data && !phys_data))
809 status = EFI_INVALID_PARAMETER;
810 else
811 status = efi_thunk(get_variable, phys_name, phys_vendor,
812 phys_attr, phys_data_size, phys_data);
813
814 spin_unlock_irqrestore(&efi_runtime_lock, flags);
815
816 return status;
817 }
818
819 static efi_status_t
820 efi_thunk_set_variable(efi_char16_t *name, efi_guid_t *vendor,
821 u32 attr, unsigned long data_size, void *data)
822 {
823 u8 buf[24] __aligned(8);
824 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
825 u32 phys_name, phys_vendor, phys_data;
826 efi_status_t status;
827 unsigned long flags;
828
829 spin_lock_irqsave(&efi_runtime_lock, flags);
830
831 *vnd = *vendor;
832
833 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
834 phys_vendor = virt_to_phys_or_null(vnd);
835 phys_data = virt_to_phys_or_null_size(data, data_size);
836
837 if (!phys_name || (data && !phys_data))
838 status = EFI_INVALID_PARAMETER;
839 else
840 status = efi_thunk(set_variable, phys_name, phys_vendor,
841 attr, data_size, phys_data);
842
843 spin_unlock_irqrestore(&efi_runtime_lock, flags);
844
845 return status;
846 }
847
848 static efi_status_t
849 efi_thunk_set_variable_nonblocking(efi_char16_t *name, efi_guid_t *vendor,
850 u32 attr, unsigned long data_size,
851 void *data)
852 {
853 u8 buf[24] __aligned(8);
854 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
855 u32 phys_name, phys_vendor, phys_data;
856 efi_status_t status;
857 unsigned long flags;
858
859 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
860 return EFI_NOT_READY;
861
862 *vnd = *vendor;
863
864 phys_name = virt_to_phys_or_null_size(name, efi_name_size(name));
865 phys_vendor = virt_to_phys_or_null(vnd);
866 phys_data = virt_to_phys_or_null_size(data, data_size);
867
868 if (!phys_name || (data && !phys_data))
869 status = EFI_INVALID_PARAMETER;
870 else
871 status = efi_thunk(set_variable, phys_name, phys_vendor,
872 attr, data_size, phys_data);
873
874 spin_unlock_irqrestore(&efi_runtime_lock, flags);
875
876 return status;
877 }
878
879 static efi_status_t
880 efi_thunk_get_next_variable(unsigned long *name_size,
881 efi_char16_t *name,
882 efi_guid_t *vendor)
883 {
884 u8 buf[24] __aligned(8);
885 efi_guid_t *vnd = PTR_ALIGN((efi_guid_t *)buf, sizeof(*vnd));
886 efi_status_t status;
887 u32 phys_name_size, phys_name, phys_vendor;
888 unsigned long flags;
889
890 spin_lock_irqsave(&efi_runtime_lock, flags);
891
892 *vnd = *vendor;
893
894 phys_name_size = virt_to_phys_or_null(name_size);
895 phys_vendor = virt_to_phys_or_null(vnd);
896 phys_name = virt_to_phys_or_null_size(name, *name_size);
897
898 if (!phys_name)
899 status = EFI_INVALID_PARAMETER;
900 else
901 status = efi_thunk(get_next_variable, phys_name_size,
902 phys_name, phys_vendor);
903
904 spin_unlock_irqrestore(&efi_runtime_lock, flags);
905
906 *vendor = *vnd;
907 return status;
908 }
909
910 static efi_status_t
911 efi_thunk_get_next_high_mono_count(u32 *count)
912 {
913 efi_status_t status;
914 u32 phys_count;
915 unsigned long flags;
916
917 spin_lock_irqsave(&efi_runtime_lock, flags);
918
919 phys_count = virt_to_phys_or_null(count);
920 status = efi_thunk(get_next_high_mono_count, phys_count);
921
922 spin_unlock_irqrestore(&efi_runtime_lock, flags);
923
924 return status;
925 }
926
927 static void
928 efi_thunk_reset_system(int reset_type, efi_status_t status,
929 unsigned long data_size, efi_char16_t *data)
930 {
931 u32 phys_data;
932 unsigned long flags;
933
934 spin_lock_irqsave(&efi_runtime_lock, flags);
935
936 phys_data = virt_to_phys_or_null_size(data, data_size);
937
938 efi_thunk(reset_system, reset_type, status, data_size, phys_data);
939
940 spin_unlock_irqrestore(&efi_runtime_lock, flags);
941 }
942
943 static efi_status_t
944 efi_thunk_update_capsule(efi_capsule_header_t **capsules,
945 unsigned long count, unsigned long sg_list)
946 {
947
948
949
950
951
952 return EFI_UNSUPPORTED;
953 }
954
955 static efi_status_t
956 efi_thunk_query_variable_info(u32 attr, u64 *storage_space,
957 u64 *remaining_space,
958 u64 *max_variable_size)
959 {
960 efi_status_t status;
961 u32 phys_storage, phys_remaining, phys_max;
962 unsigned long flags;
963
964 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
965 return EFI_UNSUPPORTED;
966
967 spin_lock_irqsave(&efi_runtime_lock, flags);
968
969 phys_storage = virt_to_phys_or_null(storage_space);
970 phys_remaining = virt_to_phys_or_null(remaining_space);
971 phys_max = virt_to_phys_or_null(max_variable_size);
972
973 status = efi_thunk(query_variable_info, attr, phys_storage,
974 phys_remaining, phys_max);
975
976 spin_unlock_irqrestore(&efi_runtime_lock, flags);
977
978 return status;
979 }
980
981 static efi_status_t
982 efi_thunk_query_variable_info_nonblocking(u32 attr, u64 *storage_space,
983 u64 *remaining_space,
984 u64 *max_variable_size)
985 {
986 efi_status_t status;
987 u32 phys_storage, phys_remaining, phys_max;
988 unsigned long flags;
989
990 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
991 return EFI_UNSUPPORTED;
992
993 if (!spin_trylock_irqsave(&efi_runtime_lock, flags))
994 return EFI_NOT_READY;
995
996 phys_storage = virt_to_phys_or_null(storage_space);
997 phys_remaining = virt_to_phys_or_null(remaining_space);
998 phys_max = virt_to_phys_or_null(max_variable_size);
999
1000 status = efi_thunk(query_variable_info, attr, phys_storage,
1001 phys_remaining, phys_max);
1002
1003 spin_unlock_irqrestore(&efi_runtime_lock, flags);
1004
1005 return status;
1006 }
1007
1008 static efi_status_t
1009 efi_thunk_query_capsule_caps(efi_capsule_header_t **capsules,
1010 unsigned long count, u64 *max_size,
1011 int *reset_type)
1012 {
1013
1014
1015
1016
1017
1018 return EFI_UNSUPPORTED;
1019 }
1020
1021 void efi_thunk_runtime_setup(void)
1022 {
1023 efi.get_time = efi_thunk_get_time;
1024 efi.set_time = efi_thunk_set_time;
1025 efi.get_wakeup_time = efi_thunk_get_wakeup_time;
1026 efi.set_wakeup_time = efi_thunk_set_wakeup_time;
1027 efi.get_variable = efi_thunk_get_variable;
1028 efi.get_next_variable = efi_thunk_get_next_variable;
1029 efi.set_variable = efi_thunk_set_variable;
1030 efi.set_variable_nonblocking = efi_thunk_set_variable_nonblocking;
1031 efi.get_next_high_mono_count = efi_thunk_get_next_high_mono_count;
1032 efi.reset_system = efi_thunk_reset_system;
1033 efi.query_variable_info = efi_thunk_query_variable_info;
1034 efi.query_variable_info_nonblocking = efi_thunk_query_variable_info_nonblocking;
1035 efi.update_capsule = efi_thunk_update_capsule;
1036 efi.query_capsule_caps = efi_thunk_query_capsule_caps;
1037 }
1038 #endif