From: "Jan Beulich" Subject: [PATCH] x86/EFI: properly handle run time memory regions outside the 1:1 map Date: Wed, 04 Sep 2013 10:26:01 +0100 Message-ID: <522718C902000078000F045B@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part596A20B9.0__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VH9Li-0005SR-RY for xen-devel@lists.xenproject.org; Wed, 04 Sep 2013 09:26:07 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part596A20B9.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Namely with PFN compression, MMIO ranges that the firmware may need runtime access to can live in the holes that gets shrunk/eliminated by PFN compression, and hence no mappings would result from simply copying Xen's direct mapping table's L3 page table entries. Build mappings for this "manually" in the EFI runtime call 1:1 page tables. Use the opportunity to also properly identify (via a forcibly undefined manifest constant) all the disabled code regions associated with it not being acceptable for us to call SetVirtualAddressMap(). Signed-off-by: Jan Beulich --- a/xen/arch/x86/efi/boot.c +++ b/xen/arch/x86/efi/boot.c @@ -26,6 +26,9 @@ #include #include =20 +/* Using SetVirtualAddressMap() is incompatible with kexec: */ +#undef USE_SET_VIRTUAL_ADDRESS_MAP + #define SHIM_LOCK_PROTOCOL_GUID \ { 0x605dab50, 0xe046, 0x4300, {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, = 0x8b, 0x23} } =20 @@ -1434,7 +1437,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY =20 /* Adjust pointers into EFI. */ efi_ct =3D (void *)efi_ct + DIRECTMAP_VIRT_START; -#if 0 /* Only needed when using virtual mode (see efi_init_memory()). */ +#ifdef USE_SET_VIRTUAL_ADDRESS_MAP efi_rs =3D (void *)efi_rs + DIRECTMAP_VIRT_START; #endif efi_memmap =3D (void *)efi_memmap + DIRECTMAP_VIRT_START; @@ -1477,6 +1480,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY for( ; ; ); /* not reached */ } =20 +#ifndef USE_SET_VIRTUAL_ADDRESS_MAP static __init void copy_mapping(unsigned long mfn, unsigned long end, bool_t (*is_valid)(unsigned long smfn, unsigned long emfn)) @@ -1520,6 +1524,7 @@ static bool_t __init rt_range_valid(unsi { return 1; } +#endif =20 #define INVALID_VIRTUAL_ADDRESS (0xBAAADUL << \ (EFI_PAGE_SHIFT + BITS_PER_LONG - 32)) @@ -1527,6 +1532,13 @@ static bool_t __init rt_range_valid(unsi void __init efi_init_memory(void) { unsigned int i; +#ifndef USE_SET_VIRTUAL_ADDRESS_MAP + struct rt_extra { + struct rt_extra *next; + unsigned long smfn, emfn; + unsigned int prot; + } *extra, *extra_head =3D NULL; +#endif =20 printk(XENLOG_INFO "EFI memory map:\n"); for ( i =3D 0; i < efi_memmap_size; i +=3D efi_mdesc_size ) @@ -1573,6 +1585,8 @@ void __init efi_init_memory(void) !(smfn & pfn_hole_mask) && !((smfn ^ (emfn - 1)) & ~pfn_pdx_bottom_mask) ) { + if ( (unsigned long)mfn_to_virt(emfn - 1) >=3D HYPERVISOR_VIRT= _END ) + prot &=3D ~_PAGE_GLOBAL; if ( map_pages_to_xen((unsigned long)mfn_to_virt(smfn), smfn, emfn - smfn, prot) =3D=3D 0 ) desc->VirtualStart =3D @@ -1581,15 +1595,29 @@ void __init efi_init_memory(void) printk(XENLOG_ERR "Could not map MFNs %#lx-%#lx\n", smfn, emfn - 1); } +#ifndef USE_SET_VIRTUAL_ADDRESS_MAP + else if ( !((desc->PhysicalStart + len - 1) >> (VADDR_BITS - 1)) = && + (extra =3D xmalloc(struct rt_extra)) !=3D NULL ) + { + extra->smfn =3D smfn; + extra->emfn =3D emfn; + extra->prot =3D prot & ~_PAGE_GLOBAL; + extra->next =3D extra_head; + extra_head =3D extra; + desc->VirtualStart =3D desc->PhysicalStart; + } +#endif else { +#ifdef USE_SET_VIRTUAL_ADDRESS_MAP /* XXX allocate e.g. down from FIXADDR_START */ +#endif printk(XENLOG_ERR "No mapping for MFNs %#lx-%#lx\n", smfn, emfn - 1); } } =20 -#if 0 /* Incompatible with kexec. */ +#ifdef USE_SET_VIRTUAL_ADDRESS_MAP efi_rs->SetVirtualAddressMap(efi_memmap_size, efi_mdesc_size, mdesc_ver, efi_memmap); #else @@ -1600,20 +1628,74 @@ void __init efi_init_memory(void) =20 copy_mapping(0, max_page, ram_range_valid); =20 - /* Insert non-RAM runtime mappings. */ + /* Insert non-RAM runtime mappings inside the direct map. */ for ( i =3D 0; i < efi_memmap_size; i +=3D efi_mdesc_size ) { const EFI_MEMORY_DESCRIPTOR *desc =3D efi_memmap + i; =20 - if ( desc->Attribute & EFI_MEMORY_RUNTIME ) + if ( (desc->Attribute & EFI_MEMORY_RUNTIME) && + desc->VirtualStart !=3D INVALID_VIRTUAL_ADDRESS && + desc->VirtualStart !=3D desc->PhysicalStart ) + copy_mapping(PFN_DOWN(desc->PhysicalStart), + PFN_UP(desc->PhysicalStart + + (desc->NumberOfPages << EFI_PAGE_SHIFT)), + rt_range_valid); + } + + /* Insert non-RAM runtime mappings outside of the direct map. */ + while ( (extra =3D extra_head) !=3D NULL ) + { + unsigned long addr =3D extra->smfn << PAGE_SHIFT; + l4_pgentry_t l4e =3D efi_l4_pgtable[l4_table_offset(addr)]; + l3_pgentry_t *pl3e; + l2_pgentry_t *pl2e; + l1_pgentry_t *l1t; + + if ( !(l4e_get_flags(l4e) & _PAGE_PRESENT) ) { - if ( desc->VirtualStart !=3D INVALID_VIRTUAL_ADDRESS ) - copy_mapping(PFN_DOWN(desc->PhysicalStart), - PFN_UP(desc->PhysicalStart + - (desc->NumberOfPages << EFI_PAGE_SHIFT= )), - rt_range_valid); - else - /* XXX */; + pl3e =3D alloc_xen_pagetable(); + BUG_ON(!pl3e); + clear_page(pl3e); + efi_l4_pgtable[l4_table_offset(addr)] =3D + l4e_from_paddr(virt_to_maddr(pl3e), __PAGE_HYPERVISOR); + } + else + pl3e =3D l4e_to_l3e(l4e); + pl3e +=3D l3_table_offset(addr); + if ( !(l3e_get_flags(*pl3e) & _PAGE_PRESENT) ) + { + pl2e =3D alloc_xen_pagetable(); + BUG_ON(!pl2e); + clear_page(pl2e); + *pl3e =3D l3e_from_paddr(virt_to_maddr(pl2e), __PAGE_HYPERVISO= R); + } + else + { + BUG_ON(l3e_get_flags(*pl3e) & _PAGE_PSE); + pl2e =3D l3e_to_l2e(*pl3e); + } + pl2e +=3D l2_table_offset(addr); + if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) ) + { + l1t =3D alloc_xen_pagetable(); + BUG_ON(!l1t); + clear_page(l1t); + *pl2e =3D l2e_from_paddr(virt_to_maddr(l1t), __PAGE_HYPERVISOR= ); + } + else + { + BUG_ON(l2e_get_flags(*pl2e) & _PAGE_PSE); + l1t =3D l2e_to_l1e(*pl2e); + } + for ( i =3D l1_table_offset(addr); + i < L1_PAGETABLE_ENTRIES && extra->smfn < extra->emfn; + ++i, ++extra->smfn ) + l1t[i] =3D l1e_from_pfn(extra->smfn, extra->prot); + + if ( extra->smfn =3D=3D extra->emfn ) + { + extra_head =3D extra->next; + xfree(extra); } } =20 --=__Part596A20B9.0__= Content-Type: text/plain; name="x86-EFI-handle-PFN-compression.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-EFI-handle-PFN-compression.patch" x86/EFI: properly handle run time memory regions outside the 1:1 map=0A=0AN= amely with PFN compression, MMIO ranges that the firmware may need=0Aruntim= e access to can live in the holes that gets shrunk/eliminated by=0APFN = compression, and hence no mappings would result from simply=0Acopying = Xen's direct mapping table's L3 page table entries. Build=0Amappings for = this "manually" in the EFI runtime call 1:1 page tables.=0A=0AUse the = opportunity to also properly identify (via a forcibly undefined=0Amanifest = constant) all the disabled code regions associated with it not=0Abeing = acceptable for us to call SetVirtualAddressMap().=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/arch/x86/efi/boot.c=0A+++ = b/xen/arch/x86/efi/boot.c=0A@@ -26,6 +26,9 @@=0A #include =0A = #include =0A =0A+/* Using SetVirtualAddressMap() is = incompatible with kexec: */=0A+#undef USE_SET_VIRTUAL_ADDRESS_MAP=0A+=0A = #define SHIM_LOCK_PROTOCOL_GUID \=0A { 0x605dab50, 0xe046, 0x4300, = {0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23} }=0A =0A@@ -1434,7 = +1437,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY=0A =0A /* Adjust = pointers into EFI. */=0A efi_ct =3D (void *)efi_ct + DIRECTMAP_VIRT_STA= RT;=0A-#if 0 /* Only needed when using virtual mode (see efi_init_memory())= . */=0A+#ifdef USE_SET_VIRTUAL_ADDRESS_MAP=0A efi_rs =3D (void = *)efi_rs + DIRECTMAP_VIRT_START;=0A #endif=0A efi_memmap =3D (void = *)efi_memmap + DIRECTMAP_VIRT_START;=0A@@ -1477,6 +1480,7 @@ efi_start(EFI_= HANDLE ImageHandle, EFI_SY=0A for( ; ; ); /* not reached */=0A }=0A = =0A+#ifndef USE_SET_VIRTUAL_ADDRESS_MAP=0A static __init void copy_mapping(= unsigned long mfn, unsigned long end,=0A = bool_t (*is_valid)(unsigned long smfn,=0A = unsigned long emfn))=0A@@ -1520,6 +1524,7 @@ static = bool_t __init rt_range_valid(unsi=0A {=0A return 1;=0A }=0A+#endif=0A = =0A #define INVALID_VIRTUAL_ADDRESS (0xBAAADUL << \=0A = (EFI_PAGE_SHIFT + BITS_PER_LONG - 32))=0A@@ -1527,6 +1532,13 = @@ static bool_t __init rt_range_valid(unsi=0A void __init efi_init_memory(= void)=0A {=0A unsigned int i;=0A+#ifndef USE_SET_VIRTUAL_ADDRESS_MAP=0A= + struct rt_extra {=0A+ struct rt_extra *next;=0A+ = unsigned long smfn, emfn;=0A+ unsigned int prot;=0A+ } *extra, = *extra_head =3D NULL;=0A+#endif=0A =0A printk(XENLOG_INFO "EFI memory = map:\n");=0A for ( i =3D 0; i < efi_memmap_size; i +=3D efi_mdesc_size = )=0A@@ -1573,6 +1585,8 @@ void __init efi_init_memory(void)=0A = !(smfn & pfn_hole_mask) &&=0A !((smfn ^ (emfn - 1)) & = ~pfn_pdx_bottom_mask) )=0A {=0A+ if ( (unsigned = long)mfn_to_virt(emfn - 1) >=3D HYPERVISOR_VIRT_END )=0A+ = prot &=3D ~_PAGE_GLOBAL;=0A if ( map_pages_to_xen((unsigned = long)mfn_to_virt(smfn),=0A smfn, emfn - = smfn, prot) =3D=3D 0 )=0A desc->VirtualStart =3D=0A@@ = -1581,15 +1595,29 @@ void __init efi_init_memory(void)=0A = printk(XENLOG_ERR "Could not map MFNs %#lx-%#lx\n",=0A = smfn, emfn - 1);=0A }=0A+#ifndef USE_SET_VIRTUAL_ADDRESS_MAP=0A+= else if ( !((desc->PhysicalStart + len - 1) >> (VADDR_BITS - 1)) = &&=0A+ (extra =3D xmalloc(struct rt_extra)) !=3D NULL = )=0A+ {=0A+ extra->smfn =3D smfn;=0A+ = extra->emfn =3D emfn;=0A+ extra->prot =3D prot & ~_PAGE_GLOBAL;= =0A+ extra->next =3D extra_head;=0A+ extra_head =3D = extra;=0A+ desc->VirtualStart =3D desc->PhysicalStart;=0A+ = }=0A+#endif=0A else=0A {=0A+#ifdef USE_SET_VIRTUAL_ADDRES= S_MAP=0A /* XXX allocate e.g. down from FIXADDR_START = */=0A+#endif=0A printk(XENLOG_ERR "No mapping for MFNs = %#lx-%#lx\n",=0A smfn, emfn - 1);=0A }=0A = }=0A =0A-#if 0 /* Incompatible with kexec. */=0A+#ifdef USE_SET_VIRTUAL_ADD= RESS_MAP=0A efi_rs->SetVirtualAddressMap(efi_memmap_size, efi_mdesc_siz= e,=0A mdesc_ver, efi_memmap);=0A = #else=0A@@ -1600,20 +1628,74 @@ void __init efi_init_memory(void)=0A =0A = copy_mapping(0, max_page, ram_range_valid);=0A =0A- /* Insert non-RAM = runtime mappings. */=0A+ /* Insert non-RAM runtime mappings inside the = direct map. */=0A for ( i =3D 0; i < efi_memmap_size; i +=3D efi_mdesc_= size )=0A {=0A const EFI_MEMORY_DESCRIPTOR *desc =3D efi_memmap= + i;=0A =0A- if ( desc->Attribute & EFI_MEMORY_RUNTIME )=0A+ = if ( (desc->Attribute & EFI_MEMORY_RUNTIME) &&=0A+ desc->Virtu= alStart !=3D INVALID_VIRTUAL_ADDRESS &&=0A+ desc->VirtualStart = !=3D desc->PhysicalStart )=0A+ copy_mapping(PFN_DOWN(desc->Physi= calStart),=0A+ PFN_UP(desc->PhysicalStart +=0A+ = (desc->NumberOfPages << EFI_PAGE_SHIFT)),=0A+ = rt_range_valid);=0A+ }=0A+=0A+ /* Insert = non-RAM runtime mappings outside of the direct map. */=0A+ while ( = (extra =3D extra_head) !=3D NULL )=0A+ {=0A+ unsigned long addr = =3D extra->smfn << PAGE_SHIFT;=0A+ l4_pgentry_t l4e =3D efi_l4_pgtab= le[l4_table_offset(addr)];=0A+ l3_pgentry_t *pl3e;=0A+ = l2_pgentry_t *pl2e;=0A+ l1_pgentry_t *l1t;=0A+=0A+ if ( = !(l4e_get_flags(l4e) & _PAGE_PRESENT) )=0A {=0A- if ( = desc->VirtualStart !=3D INVALID_VIRTUAL_ADDRESS )=0A- = copy_mapping(PFN_DOWN(desc->PhysicalStart),=0A- = PFN_UP(desc->PhysicalStart +=0A- = (desc->NumberOfPages << EFI_PAGE_SHIFT)),=0A- = rt_range_valid);=0A- else=0A- /* XXX */;=0A+ = pl3e =3D alloc_xen_pagetable();=0A+ BUG_ON(!pl3e);=0A+ = clear_page(pl3e);=0A+ efi_l4_pgtable[l4_table_offset(ad= dr)] =3D=0A+ l4e_from_paddr(virt_to_maddr(pl3e), __PAGE_HYPE= RVISOR);=0A+ }=0A+ else=0A+ pl3e =3D l4e_to_l3e(l4= e);=0A+ pl3e +=3D l3_table_offset(addr);=0A+ if ( !(l3e_get_f= lags(*pl3e) & _PAGE_PRESENT) )=0A+ {=0A+ pl2e =3D = alloc_xen_pagetable();=0A+ BUG_ON(!pl2e);=0A+ = clear_page(pl2e);=0A+ *pl3e =3D l3e_from_paddr(virt_to_maddr(pl2= e), __PAGE_HYPERVISOR);=0A+ }=0A+ else=0A+ {=0A+ = BUG_ON(l3e_get_flags(*pl3e) & _PAGE_PSE);=0A+ pl2e =3D = l3e_to_l2e(*pl3e);=0A+ }=0A+ pl2e +=3D l2_table_offset(addr);= =0A+ if ( !(l2e_get_flags(*pl2e) & _PAGE_PRESENT) )=0A+ = {=0A+ l1t =3D alloc_xen_pagetable();=0A+ BUG_ON(!l1t)= ;=0A+ clear_page(l1t);=0A+ *pl2e =3D l2e_from_paddr(v= irt_to_maddr(l1t), __PAGE_HYPERVISOR);=0A+ }=0A+ else=0A+ = {=0A+ BUG_ON(l2e_get_flags(*pl2e) & _PAGE_PSE);=0A+ = l1t =3D l2e_to_l1e(*pl2e);=0A+ }=0A+ for ( i =3D = l1_table_offset(addr);=0A+ i < L1_PAGETABLE_ENTRIES && = extra->smfn < extra->emfn;=0A+ ++i, ++extra->smfn )=0A+ = l1t[i] =3D l1e_from_pfn(extra->smfn, extra->prot);=0A+=0A+ if = ( extra->smfn =3D=3D extra->emfn )=0A+ {=0A+ extra_head = =3D extra->next;=0A+ xfree(extra);=0A }=0A }=0A =0A --=__Part596A20B9.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xen.org http://lists.xen.org/xen-devel --=__Part596A20B9.0__=--