EFI runtime services 1:1 mapping
Borislav Petkov
maintainercover EFI runtime services 1:1 mapping
From: Borislav Petkov <[email protected]>
Ok,
this is a first attempt to establish EFI runtime services regions mappings into ->trampoline_pgd so that the prior can be used in kexec, for example.
The more I'm dealing with this, the more I can't shake the idiocy of the decision to make SetVirtualAddressMap() be callable only once per boot. And for that decision, we're jumping through hoops in the kernel. Oh well, one can hack on EFI and hate it at the same time :-).
So this is 64-only for now, it barely works in the sense that I can call efi.get_time() through the new mappings.
What still is an issue is GetNextVariableName, for example, whose second and third arg are I/O and, of course, not mapped. I still need to think about how to do that (map them temporary, have a single mapped page where I shovel data to and fro or something even better).
Anyway, this is just an RFC to sense the kosher-ness of the general direction and to get people's opinion early enough.
Thanks for the review.
Borislav Petkov (2):
x86, cpa: Map in an arbitrary pgd
x86, efi: Add 1:1 mapping of runtime services
arch/x86/include/asm/efi.h | 2 + arch/x86/include/asm/pgtable_types.h | 3 +- arch/x86/mm/pageattr.c | 80 ++++++++++++++++++++++++++-------- arch/x86/platform/efi/efi.c | 84 ++++++++++++++++++++++++++---------- arch/x86/platform/efi/efi_stub_64.S | 39 +++++++++++++++++ 5 files changed, 167 insertions(+), 41 deletions(-)
Signature · 1 line
1.8.2.135.g7b592fa
Borislav Petkov
maintainerRFC PATCH 1/2 x86, cpa: Map in an arbitrary pgd
From: Borislav Petkov <[email protected]>
Add the ability to map pages in an arbitrary pgd.
--- arch/x86/include/asm/pgtable_types.h | 3 +- arch/x86/mm/pageattr.c | 80 ++++++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 18 deletions(-)
arch/x86/include/asm/pgtable_types.h +2 -1
@@ -352,7 +352,8 @@ static inline void update_page_count(int level, unsigned long pages) { }*/ extern pte_t *lookup_address (unsigned long address,unsigned int *level);extern phys_addr_t slow_virt_to_phys (void *__address); - +extern void kernel_map_pages_in_pgd (pgd_t *pgd,unsigned long address, +unsigned numpages,unsigned long page_flags);#endif /* !__ASSEMBLY__ */ #endif /* _ASM_X86_PGTABLE_DEFS_H */
arch/x86/mm/pageattr.c +63 -17
155 lines not on this page · raw message
Signature · 1 line
1.8.2.135.g7b592fa
Borislav Petkov
maintainerRFC PATCH 2/2 x86, efi: Add 1:1 mapping of runtime services
In reply to Borislav PetkovFrom: Borislav Petkov <[email protected]>
Map EFI runtime services 1:1 into the trampoline pgd so that all those functions can be used in a kexec kernel. As we all know, the braindead design of SetVirtualAddressMap() doesn't allow a subsequent call to this function to reestablish virtual mappings, leading us to do all kinds of crazy dances in the kernel.
64-bit only for now.
--- arch/x86/include/asm/efi.h | 2 + arch/x86/platform/efi/efi.c | 84 +++++++++++++++++++++++++++---------- arch/x86/platform/efi/efi_stub_64.S | 39 +++++++++++++++++ 3 files changed, 102 insertions(+), 23 deletions(-)
arch/x86/include/asm/efi.h +2 -0
@@ -39,6 +39,8 @@ extern unsigned long asmlinkage efi_call_phys(void *, ...);#else /* !CONFIG_X86_32 */ +extern pgd_t *efi_pgt; +#define EFI_LOADER_SIGNATURE "EL64"extern u64 efi_call0 (void *fp);
arch/x86/platform/efi/efi.c +61 -23
134 lines not on this page · raw message
arch/x86/platform/efi/efi_stub_64.S +39 -0
@@ -34,10 +34,34 @@ mov %rsi, %cr0; \ mov (%rsp), %rsp +/* happily stolen from gcc, see __flush_tlb_global() */ +#define FLUSH_TLB_ALL \ + movq %cr4, %r14; \ + movq %r14, %r13; \ + and $0x7f, %r13b; \ + movq %r13, %cr4; \ + movq %r14, %cr4 + +/* + * %r15 is a non-volatile register and is preserved by UEFI so use + * it for stashing previous PGD in there. + */ +#define SWITCH_PGT \ + movq %cr3, %r15; \ + movq efi_pgt, %rax; \ + movq %rax, %cr3; \ + FLUSH_TLB_ALL + +#define RESTORE_PGT \ + movq %r15, %cr3; \ + FLUSH_TLB_ALL + ENTRY(efi_call0) SAVE_XMM subq $32, %rsp + SWITCH_PGT call *%rdi + RESTORE_PGT addq $32, %rsp RESTORE_XMM ret
@@ -47,7 +71,9 @@ ENTRY(efi_call1) SAVE_XMM subq $32, %rsp mov %rsi, %rcx + SWITCH_PGT call *%rdi + RESTORE_PGT addq $32, %rsp RESTORE_XMM ret
@@ -57,7 +83,9 @@ ENTRY(efi_call2) SAVE_XMM subq $32, %rsp mov %rsi, %rcx + SWITCH_PGT call *%rdi + RESTORE_PGT addq $32, %rsp RESTORE_XMM ret
@@ -68,7 +96,9 @@ ENTRY(efi_call3) subq $32, %rsp mov %rcx, %r8 mov %rsi, %rcx + SWITCH_PGT call *%rdi + RESTORE_PGT addq $32, %rsp RESTORE_XMM ret
@@ -80,7 +110,9 @@ ENTRY(efi_call4) mov %r8, %r9 mov %rcx, %r8 mov %rsi, %rcx + SWITCH_PGT call *%rdi + RESTORE_PGT addq $32, %rsp RESTORE_XMM ret
@@ -93,7 +125,9 @@ ENTRY(efi_call5) mov %r8, %r9 mov %rcx, %r8 mov %rsi, %rcx + SWITCH_PGT call *%rdi + RESTORE_PGT addq $48, %rsp RESTORE_XMM ret
@@ -109,8 +143,13 @@ ENTRY(efi_call6) mov %r8, %r9 mov %rcx, %r8 mov %rsi, %rcx + SWITCH_PGT call *%rdi + RESTORE_PGT addq $48, %rsp RESTORE_XMM ret ENDPROC(efi_call6) + +GLOBAL(efi_pgt) + .quad 0
Signature · 1 line
1.8.2.135.g7b592fa
Re: [RFC PATCH 2/2] x86, efi: Add 1:1 mapping of runtime services
Quoted 16 lines · Borislav Petkov
From: Borislav Petkov <[email protected]>
Map EFI runtime services 1:1 into the trampoline pgd so that all those functions can be used in a kexec kernel. As we all know, the braindead design of
SetVirtualAddressMap()doesn't allow a subsequent call to this function to reestablish virtual mappings, leading us to do all kinds of crazy dances in the kernel.64-bit only for now.
Signed-off-by Borislav Petkov <[email protected]>--- arch/x86/include/asm/efi.h | 2 + arch/x86/platform/efi/efi.c | 84 +++++++++++++++++++++++++++---------- arch/x86/platform/efi/efi_stub_64.S | 39 +++++++++++++++++ 3 files changed, 102 insertions(+), 23 deletions(-)
[...]
Quoted 25 lines
arch/x86/platform/efi/efi.c +8
-9@@ -649,15 +649,24 @@ static int __init efi_runtime_init(void)pr_err ("Could not map the runtime service table!\n" );return -ENOMEM ; } -/* -* We will only need *early* access to the following -* two EFI runtime services before set_virtual_address_map -* is invoked. -*/ - efi_phys.get_time = (efi_get_time_t *)runtime->get_time; - efi_phys.set_virtual_address_map = - (efi_set_virtual_address_map_t *) - runtime->set_virtual_address_map; + +#define efi_phys_assign (f) \ + efi_phys.f = (efi_ ##f## _t *)runtime->f + +efi_phys_assign (get_time); +efi_phys_assign (set_time); +efi_phys_assign (get_wakeup_time); +efi_phys_assign (set_wakeup_time);
arch/x86/platform/efi/efi.c static int __init efi_runtime_init(void) · line 669
Quoted 10 lines
arch/x86/platform/efi/efi.c +10
-0+efi_phys_assign (get_variable); +efi_phys_assign (get_next_variable); +efi_phys_assign (set_variable); +efi_phys_assign (get_next_high_mono_count); +efi_phys_assign (reset_system); +efi_phys_assign (set_virtual_address_map); +efi_phys_assign (query_variable_info); +efi_phys_assign (update_capsule); +efi_phys_assign (query_capsule_caps); +
Why is this change needed? We still don't access these other functions via their early addresses.
Quoted 39 lines
arch/x86/platform/efi/efi.c +14
-2/* * Make efi_get_time can be called before entering * virtual mode. @@ -845,9 +854,10 @@ void efi_memory_uc(u64 addr, unsigned long size)*/ void __init efi_enter_virtual_mode (void ) { +pgd_t *pgd = (pgd_t *)__va (real_mode_header->trampoline_pgd);efi_memory_desc_t *md, *prev_md =NULL ;efi_status_t status; -unsigned long size; +unsigned long size, page_flags;u64 end, systab, start_pfn, end_pfn;void *p, *va, *new_memmap =NULL ;int count =0 ;@@ -895,7 +905,8 @@ void __init efi_enter_virtual_mode(void) md = p;if (!(md->attribute &EFI_MEMORY_RUNTIME ) && md->type !=EFI_BOOT_SERVICES_CODE && - md->type !=EFI_BOOT_SERVICES_DATA ) + md->type !=EFI_BOOT_SERVICES_DATA && + md->type !=EFI_CONVENTIONAL_MEMORY )continue ; size = md->num_pages <<EFI_PAGE_SHIFT ;@@ -920,11 +931,26 @@ void __init efi_enter_virtual_mode(void)continue ; } + page_flags =0 ; + +if (md->type ==EFI_RUNTIME_SERVICES_DATA || + md->type ==EFI_BOOT_SERVICES_DATA ) + page_flags = _PAGE_NX; + +if (!(md->attribute &EFI_MEMORY_WB )) + page_flags |= _PAGE_PCD; + + kernel_map_pages_in_pgd(pgd, md->phys_addr,
arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 953
Quoted 10 lines
arch/x86/platform/efi/efi.c +5
-0+ md->num_pages, page_flags); + systab = (u64 ) (unsigned long ) efi_phys.systab;if (md->phys_addr <= systab && systab < end) { systab += md->virt_addr - md->phys_addr; efi.systab = (efi_system_table_t *) (unsigned long ) systab; } + + md->virt_addr = md->phys_addr; +
Now we have the EFI regions mapped in two places synchronisation is probably required, e.g. mappings are accessible via the direct kernel mapping/ioremap space, and via the 1:1 mapping. Also, you'll want to look at fixing efi_lookup_mapped_addr() which assumes it can access md->virt_addr in the current pgd.
Actually, I _think_ we can get away with only double mapping those regions that we use as ram, see do_add_efi_memmap() and exit_boot() in arch/x86/boot/compressed/eboot.c. We can probably skip the ioremap() calls now, since they're only for the benefit of firmware. Which in turn should mean we can delete efi_ioremap().
Signature · 1 line
Matt Fleming, Intel Open Source Technology Center
Borislav Petkov
maintainerRe: [RFC PATCH 2/2] x86, efi: Add 1:1 mapping of runtime services
Quoted 30 lines · Matt Fleming
Quoted 30 lines
arch/x86/platform/efi/efi.c +13
-9@@ -649,15 +649,24 @@ static int __init efi_runtime_init(void)pr_err ("Could not map the runtime service table!\n" );return -ENOMEM ; } -/* -* We will only need *early* access to the following -* two EFI runtime services before set_virtual_address_map -* is invoked. -*/ - efi_phys.get_time = (efi_get_time_t *)runtime->get_time; - efi_phys.set_virtual_address_map = - (efi_set_virtual_address_map_t *) - runtime->set_virtual_address_map; + +#define efi_phys_assign (f) \ + efi_phys.f = (efi_ ##f## _t *)runtime->f + +efi_phys_assign (get_time); +efi_phys_assign (set_time); +efi_phys_assign (get_wakeup_time); +efi_phys_assign (set_wakeup_time); +efi_phys_assign (get_variable); +efi_phys_assign (get_next_variable); +efi_phys_assign (set_variable); +efi_phys_assign (get_next_high_mono_count); +efi_phys_assign (reset_system);
Quoted 8 lines
arch/x86/platform/efi/efi.c static int __init efi_runtime_init(void) · line 669
Quoted 5 lines
arch/x86/platform/efi/efi.c +5
-0+efi_phys_assign (set_virtual_address_map); +efi_phys_assign (query_variable_info); +efi_phys_assign (update_capsule); +efi_phys_assign (query_capsule_caps); +Why is this change needed? We still don't access these other functions via their early addresses.
Because I need the physical addresses of the EFI runtime functions and assign them to efi.systab->runtime->[function_name] later, see efi_assign in efi_enter_virtual_mode(). And efi_phys is already there so we can use it.
Unless you have a better idea, of course.
Quoted 45 lines
Quoted 45 lines
arch/x86/platform/efi/efi.c +16
-2/* * Make efi_get_time can be called before entering * virtual mode. @@ -845,9 +854,10 @@ void efi_memory_uc(u64 addr, unsigned long size)*/ void __init efi_enter_virtual_mode (void ) { +pgd_t *pgd = (pgd_t *)__va (real_mode_header->trampoline_pgd);efi_memory_desc_t *md, *prev_md =NULL ;efi_status_t status; -unsigned long size; +unsigned long size, page_flags;u64 end, systab, start_pfn, end_pfn;void *p, *va, *new_memmap =NULL ;int count =0 ;@@ -895,7 +905,8 @@ void __init efi_enter_virtual_mode(void) md = p;if (!(md->attribute &EFI_MEMORY_RUNTIME ) && md->type !=EFI_BOOT_SERVICES_CODE && - md->type !=EFI_BOOT_SERVICES_DATA ) + md->type !=EFI_BOOT_SERVICES_DATA && + md->type !=EFI_CONVENTIONAL_MEMORY )continue ; size = md->num_pages <<EFI_PAGE_SHIFT ;@@ -920,11 +931,26 @@ void __init efi_enter_virtual_mode(void)continue ; } + page_flags =0 ; + +if (md->type ==EFI_RUNTIME_SERVICES_DATA || + md->type ==EFI_BOOT_SERVICES_DATA ) + page_flags = _PAGE_NX; + +if (!(md->attribute &EFI_MEMORY_WB )) + page_flags |= _PAGE_PCD; + +kernel_map_pages_in_pgd (pgd, md->phys_addr, + md->num_pages, page_flags); + systab = (u64 ) (unsigned long ) efi_phys.systab;if (md->phys_addr <= systab && systab < end) { systab += md->virt_addr - md->phys_addr; efi.systab = (efi_system_table_t *) (unsigned long ) systab;
Quoted 8 lines
arch/x86/platform/efi/efi.c void __init efi_enter_virtual_mode(void) · line 953
Quoted 4 lines
arch/x86/platform/efi/efi.c +3
-0} + + md->virt_addr = md->phys_addr; +Now we have the EFI regions mapped in two places synchronisation is probably required, e.g. mappings are accessible via the direct kernel mapping/ioremap space, and via the 1:1 mapping.
Actually, after the efi_assign things above are done, all efi_virt* calls are done solely through the 1:1 mapping.
And Matthew wanted to keep the virtual mappings around for those funny Macs.
Which means, we need some sort of synchronization. Hmm.
So, what do we want to do?
Have a bunch of functions called phys_efi_*, in the manner of phys_efi_get_time, which call the address saved in efi_phys.<function_name> and they all use the 1:1 mappings so that they can be used later in kexec, etc.
Then we can keep all those efi_virt* functions untouched (specifically, do not do ->virt_addr = ->phys_addr).
phys_efi* and virt_efi* functions will use a global lock and there's your synchronization.
However, this all depends on whether we can use md->phys_addr to call runtime services after SetVirtualAddressMap() has run. Can we? Because if we do, then that way it should work. Otherwise either the Macs or kexec is hosed.
We probably should discuss this further on IRC.
Quoted 2 lines
Also, you'll want to look at fixing
efi_lookup_mapped_addr()which assumes it can accessmd->virt_addrin the current pgd.
No need to if we keep the ioremap mappings.
Quoted 5 lines
Actually, I _think_ we can get away with only double mapping those regions that we use as ram, see
do_add_efi_memmap()andexit_boot()inarch/x86/boot/compressed/eboot.c. We can probably skip theioremap()calls now, since they're only for the benefit of firmware. Which in turn should mean we can deleteefi_ioremap().
Yeah, I think this doesn't change if we keep two sets of mappings.
Thanks.
Signature · 5 lines
Regards/Gruss,
Boris.
Sent from a fat crate under my desk. Formatting is fine.
--