From: "Jan Beulich" Subject: [PATCH 1/2] x86/EFI: make trampoline allocation more flexible Date: Thu, 07 Nov 2013 07:54:02 +0000 Message-ID: <527B552A020000780010076B@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part0C3F280A.0__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VeKPs-0000Qy-Sm for xen-devel@lists.xenproject.org; Thu, 07 Nov 2013 07:54:13 +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 Cc: Keir Fraser 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. --=__Part0C3F280A.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Certain UEFI implementations reserve all memory below 1Mb at boot time, making it impossible to properly allocate the chunk necessary for the trampoline. Fall back to simply grabbing a chunk from EfiBootServices* regions immediately prior to calling ExitBootServices(). Signed-off-by: Jan Beulich --- a/xen/arch/x86/efi/boot.c +++ b/xen/arch/x86/efi/boot.c @@ -746,6 +746,22 @@ static void __init relocate_image(unsign extern const s32 __trampoline_rel_start[], __trampoline_rel_stop[]; extern const s32 __trampoline_seg_start[], __trampoline_seg_stop[]; =20 +static void __init relocate_trampoline(unsigned long phys) +{ + const s32 *trampoline_ptr; + + trampoline_phys =3D phys; + /* Apply relocations to trampoline. */ + for ( trampoline_ptr =3D __trampoline_rel_start; + trampoline_ptr < __trampoline_rel_stop; + ++trampoline_ptr ) + *(u32 *)(*trampoline_ptr + (long)trampoline_ptr) +=3D phys; + for ( trampoline_ptr =3D __trampoline_seg_start; + trampoline_ptr < __trampoline_seg_stop; + ++trampoline_ptr ) + *(u16 *)(*trampoline_ptr + (long)trampoline_ptr) =3D phys >> 4; +} + void EFIAPI __init __attribute__((__noreturn__)) efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { @@ -765,7 +781,6 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *mode_info; EFI_FILE_HANDLE dir_handle; union string section =3D { NULL }, name; - const s32 *trampoline_ptr; struct e820entry *e; u64 efer; bool_t base_video =3D 0; @@ -1268,23 +1283,13 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY cfg.size =3D trampoline_end - trampoline_start; status =3D efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData, PFN_UP(cfg.size), &cfg.addr); - if ( EFI_ERROR(status) ) + if ( status =3D=3D EFI_SUCCESS ) + relocate_trampoline(cfg.addr); + else { cfg.addr =3D 0; - blexit(L"No memory for trampoline\r\n"); + PrintStr(L"Trampoline space cannot be allocated; will try = fallback.\r\n"); } - trampoline_phys =3D cfg.addr; - /* Apply relocations to trampoline. */ - for ( trampoline_ptr =3D __trampoline_rel_start; - trampoline_ptr < __trampoline_rel_stop; - ++trampoline_ptr ) - *(u32 *)(*trampoline_ptr + (long)trampoline_ptr) +=3D - trampoline_phys; - for ( trampoline_ptr =3D __trampoline_seg_start; - trampoline_ptr < __trampoline_seg_stop; - ++trampoline_ptr ) - *(u16 *)(*trampoline_ptr + (long)trampoline_ptr) =3D - trampoline_phys >> 4; =20 /* Initialise L2 identity-map and boot-map page table entries (16MB). = */ for ( i =3D 0; i < 8; ++i ) @@ -1400,10 +1405,14 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY type =3D E820_RESERVED; break; case EfiConventionalMemory: - case EfiLoaderCode: - case EfiLoaderData: case EfiBootServicesCode: case EfiBootServicesData: + if ( !trampoline_phys && desc->PhysicalStart + len <=3D = 0x100000 && + len >=3D cfg.size && desc->PhysicalStart + len > = cfg.addr ) + cfg.addr =3D (desc->PhysicalStart + len - cfg.size) & = PAGE_MASK; + /* fall through */ + case EfiLoaderCode: + case EfiLoaderData: if ( desc->Attribute & EFI_MEMORY_WB ) type =3D E820_RAM; else @@ -1431,6 +1440,12 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY ++e820nr; } } + if ( !trampoline_phys ) + { + if ( !cfg.addr ) + blexit(L"No memory for trampoline"); + relocate_trampoline(cfg.addr); + } =20 status =3D efi_bs->ExitBootServices(ImageHandle, map_key); if ( EFI_ERROR(status) ) --=__Part0C3F280A.0__= Content-Type: text/plain; name="x86-EFI-trampoline-fallback.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-EFI-trampoline-fallback.patch" x86/EFI: make trampoline allocation more flexible=0A=0ACertain UEFI = implementations reserve all memory below 1Mb at boot time,=0Amaking it = impossible to properly allocate the chunk necessary for the=0Atrampoline. = Fall back to simply grabbing a chunk from EfiBootServices*=0Aregions = immediately prior to calling ExitBootServices().=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/arch/x86/efi/boot.c=0A+++ = b/xen/arch/x86/efi/boot.c=0A@@ -746,6 +746,22 @@ static void __init = relocate_image(unsign=0A extern const s32 __trampoline_rel_start[], = __trampoline_rel_stop[];=0A extern const s32 __trampoline_seg_start[], = __trampoline_seg_stop[];=0A =0A+static void __init relocate_trampoline(unsi= gned long phys)=0A+{=0A+ const s32 *trampoline_ptr;=0A+=0A+ = trampoline_phys =3D phys;=0A+ /* Apply relocations to trampoline. = */=0A+ for ( trampoline_ptr =3D __trampoline_rel_start;=0A+ = trampoline_ptr < __trampoline_rel_stop;=0A+ ++trampoline_ptr = )=0A+ *(u32 *)(*trampoline_ptr + (long)trampoline_ptr) +=3D = phys;=0A+ for ( trampoline_ptr =3D __trampoline_seg_start;=0A+ = trampoline_ptr < __trampoline_seg_stop;=0A+ ++trampoline_ptr = )=0A+ *(u16 *)(*trampoline_ptr + (long)trampoline_ptr) =3D phys >> = 4;=0A+}=0A+=0A void EFIAPI __init __attribute__((__noreturn__))=0A = efi_start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)=0A {=0A@@ = -765,7 +781,6 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY=0A EFI_GRAPHI= CS_OUTPUT_MODE_INFORMATION *mode_info;=0A EFI_FILE_HANDLE dir_handle;= =0A union string section =3D { NULL }, name;=0A- const s32 = *trampoline_ptr;=0A struct e820entry *e;=0A u64 efer;=0A = bool_t base_video =3D 0;=0A@@ -1268,23 +1283,13 @@ efi_start(EFI_HANDLE = ImageHandle, EFI_SY=0A cfg.size =3D trampoline_end - trampoline_start;= =0A status =3D efi_bs->AllocatePages(AllocateMaxAddress, EfiLoaderData,= =0A PFN_UP(cfg.size), &cfg.addr);=0A- = if ( EFI_ERROR(status) )=0A+ if ( status =3D=3D EFI_SUCCESS )=0A+ = relocate_trampoline(cfg.addr);=0A+ else=0A {=0A cfg.addr = =3D 0;=0A- blexit(L"No memory for trampoline\r\n");=0A+ = PrintStr(L"Trampoline space cannot be allocated; will try fallback.\r\n");= =0A }=0A- trampoline_phys =3D cfg.addr;=0A- /* Apply relocations = to trampoline. */=0A- for ( trampoline_ptr =3D __trampoline_rel_start;= =0A- trampoline_ptr < __trampoline_rel_stop;=0A- = ++trampoline_ptr )=0A- *(u32 *)(*trampoline_ptr + (long)trampoline_p= tr) +=3D=0A- trampoline_phys;=0A- for ( trampoline_ptr =3D = __trampoline_seg_start;=0A- trampoline_ptr < __trampoline_seg_stop= ;=0A- ++trampoline_ptr )=0A- *(u16 *)(*trampoline_ptr + = (long)trampoline_ptr) =3D=0A- trampoline_phys >> 4;=0A =0A = /* Initialise L2 identity-map and boot-map page table entries (16MB). = */=0A for ( i =3D 0; i < 8; ++i )=0A@@ -1400,10 +1405,14 @@ efi_start(E= FI_HANDLE ImageHandle, EFI_SY=0A type =3D E820_RESERVED;=0A = break;=0A case EfiConventionalMemory:=0A- case = EfiLoaderCode:=0A- case EfiLoaderData:=0A case EfiBootServic= esCode:=0A case EfiBootServicesData:=0A+ if ( !trampolin= e_phys && desc->PhysicalStart + len <=3D 0x100000 &&=0A+ = len >=3D cfg.size && desc->PhysicalStart + len > cfg.addr )=0A+ = cfg.addr =3D (desc->PhysicalStart + len - cfg.size) & PAGE_MASK;=0A+ = /* fall through */=0A+ case EfiLoaderCode:=0A+ case = EfiLoaderData:=0A if ( desc->Attribute & EFI_MEMORY_WB )=0A = type =3D E820_RAM;=0A else=0A@@ -1431,6 +1440,12 = @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY=0A ++e820nr;=0A = }=0A }=0A+ if ( !trampoline_phys )=0A+ {=0A+ if ( = !cfg.addr )=0A+ blexit(L"No memory for trampoline");=0A+ = relocate_trampoline(cfg.addr);=0A+ }=0A =0A status =3D efi_bs->ExitB= ootServices(ImageHandle, map_key);=0A if ( EFI_ERROR(status) )=0A --=__Part0C3F280A.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 --=__Part0C3F280A.0__=--