From: "Jan Beulich" Subject: [PATCH RFC] EFI: allow retry of ExitBootServices() call Date: Fri, 14 Nov 2014 12:37:30 +0000 Message-ID: <5466059A0200007800047A4B@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part3401089A.1__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1XpG84-0005Vy-Bb for xen-devel@lists.xenproject.org; Fri, 14 Nov 2014 12:37:32 +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: roy.franz@linaro.org 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. --=__Part3401089A.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The specification is kind of vague under what conditions ExitBootServices() may legitimately fail, requiring the OS loader to retry: "If MapKey value is incorrect, ExitBootServices() returns EFI_INVALID_PARAMETER and GetMemoryMap() with ExitBootServices() must be called again. Firmware implementation may choose to do a partial shutdown of the boot services during the first call to ExitBootServices(). EFI OS loader should not make calls to any boot service function other then GetMemoryMap() after the first call to ExitBootServices()." While our code guarantees the map key to be valid, there are systems where a firmware internal notification sent while processing ExitBootServices() reportedly results in changes to the memory map. In that case, make a best effort second try: Avoid any boot service calls other than the two named above, with the possible exception of error paths. Those aren't a problem, since if we end up needing to retry, we're hosed when something goes wrong as much as if we didn't make the retry attempt. For x86, a minimal adjustment to efi_arch_process_memory_map() is needed for it to cope with potentially being called a second time. For arm64, while efi_process_memory_map_bootinfo() is easy to verify that it can safely be called more than once without violating spec constraints, it's not so obvious for fdt_add_uefi_nodes(), hence a step by step approach: - deletion of memory nodes and memory reserve map entries: the 2nd pass shouldn't find any as the 1st one deleted them all, - a "chosen" node should be found as it got added in the 1st pass, - the various "linux,uefi-*" nodes all got added during the 1st pass and hence only their contents may get updated. Signed-off-by: Jan Beulich --- a/xen/arch/x86/efi/efi-boot.h +++ b/xen/arch/x86/efi/efi-boot.h @@ -140,7 +140,7 @@ static void __init efi_arch_process_memo =20 /* Populate E820 table and check trampoline area availability. */ e =3D e820map - 1; - for ( i =3D 0; i < map_size; i +=3D desc_size ) + for ( e820nr =3D i =3D 0; i < map_size; i +=3D desc_size ) { EFI_MEMORY_DESCRIPTOR *desc =3D map + i; u64 len =3D desc->NumberOfPages << EFI_PAGE_SHIFT; --- a/xen/common/efi/boot.c +++ b/xen/common/efi/boot.c @@ -703,7 +703,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY EFI_GRAPHICS_OUTPUT_PROTOCOL *gop =3D NULL; EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *mode_info; union string section =3D { NULL }, name; - bool_t base_video =3D 0; + bool_t base_video =3D 0, retry; char *option_str; bool_t use_cfg_file; =20 @@ -1051,17 +1051,23 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY if ( !efi_memmap ) blexit(L"Unable to allocate memory for EFI memory map"); =20 - status =3D efi_bs->GetMemoryMap(&efi_memmap_size, efi_memmap, = &map_key, - &efi_mdesc_size, &mdesc_ver); - if ( EFI_ERROR(status) ) - PrintErrMesg(L"Cannot obtain memory map", status); + for ( retry =3D 0; ; retry =3D 1 ) + { + status =3D efi_bs->GetMemoryMap(&efi_memmap_size, efi_memmap, = &map_key, + &efi_mdesc_size, &mdesc_ver); + if ( EFI_ERROR(status) ) + PrintErrMesg(L"Cannot obtain memory map", status); =20 - efi_arch_process_memory_map(SystemTable, efi_memmap, efi_memmap_size, - efi_mdesc_size, mdesc_ver); + efi_arch_process_memory_map(SystemTable, efi_memmap, efi_memmap_si= ze, + efi_mdesc_size, mdesc_ver); =20 - efi_arch_pre_exit_boot(); + efi_arch_pre_exit_boot(); + + status =3D efi_bs->ExitBootServices(ImageHandle, map_key); + if ( status !=3D EFI_INVALID_PARAMETER || retry ) + break; + } =20 - status =3D efi_bs->ExitBootServices(ImageHandle, map_key); if ( EFI_ERROR(status) ) PrintErrMesg(L"Cannot exit boot services", status); =20 --=__Part3401089A.1__= Content-Type: text/plain; name="EFI-exit-boot-services-retry.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="EFI-exit-boot-services-retry.patch" EFI: allow retry of ExitBootServices() call=0A=0AThe specification is kind = of vague under what conditions=0AExitBootServices() may legitimately fail, = requiring the OS loader to=0Aretry:=0A=0A"If MapKey value is incorrect, = ExitBootServices() returns=0A EFI_INVALID_PARAMETER and GetMemoryMap() = with ExitBootServices() must=0A be called again. Firmware implementation = may choose to do a partial=0A shutdown of the boot services during the = first call to=0A ExitBootServices(). EFI OS loader should not make calls = to any boot=0A service function other then GetMemoryMap() after the first = call to=0A ExitBootServices()."=0A=0AWhile our code guarantees the map key = to be valid, there are systems=0Awhere a firmware internal notification = sent while processing=0AExitBootServices() reportedly results in changes = to the memory map.=0AIn that case, make a best effort second try: Avoid = any boot service=0Acalls other than the two named above, with the possible = exception of=0Aerror paths. Those aren't a problem, since if we end up = needing to=0Aretry, we're hosed when something goes wrong as much as if we = didn't=0Amake the retry attempt.=0A=0AFor x86, a minimal adjustment to = efi_arch_process_memory_map() is=0Aneeded for it to cope with potentially = being called a second time.=0A=0AFor arm64, while efi_process_memory_map_bo= otinfo() is easy to verify=0Athat it can safely be called more than once = without violating spec=0Aconstraints, it's not so obvious for fdt_add_uefi_= nodes(), hence a=0Astep by step approach:=0A- deletion of memory nodes and = memory reserve map entries: the 2nd pass=0A shouldn't find any as the 1st = one deleted them all,=0A- a "chosen" node should be found as it got added = in the 1st pass,=0A- the various "linux,uefi-*" nodes all got added during = the 1st pass=0A and hence only their contents may get updated.=0A=0ASigned= -off-by: Jan Beulich =0A=0A--- a/xen/arch/x86/efi/efi-bo= ot.h=0A+++ b/xen/arch/x86/efi/efi-boot.h=0A@@ -140,7 +140,7 @@ static void = __init efi_arch_process_memo=0A =0A /* Populate E820 table and check = trampoline area availability. */=0A e =3D e820map - 1;=0A- for ( i = =3D 0; i < map_size; i +=3D desc_size )=0A+ for ( e820nr =3D i =3D 0; i = < map_size; i +=3D desc_size )=0A {=0A EFI_MEMORY_DESCRIPTOR = *desc =3D map + i;=0A u64 len =3D desc->NumberOfPages << EFI_PAGE_S= HIFT;=0A--- a/xen/common/efi/boot.c=0A+++ b/xen/common/efi/boot.c=0A@@ = -703,7 +703,7 @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY=0A EFI_GRAPHI= CS_OUTPUT_PROTOCOL *gop =3D NULL;=0A EFI_GRAPHICS_OUTPUT_MODE_INFORMATI= ON *mode_info;=0A union string section =3D { NULL }, name;=0A- = bool_t base_video =3D 0;=0A+ bool_t base_video =3D 0, retry;=0A = char *option_str;=0A bool_t use_cfg_file;=0A =0A@@ -1051,17 +1051,23 = @@ efi_start(EFI_HANDLE ImageHandle, EFI_SY=0A if ( !efi_memmap )=0A = blexit(L"Unable to allocate memory for EFI memory map");=0A =0A- = status =3D efi_bs->GetMemoryMap(&efi_memmap_size, efi_memmap, &map_key,=0A-= &efi_mdesc_size, &mdesc_ver);=0A- if = ( EFI_ERROR(status) )=0A- PrintErrMesg(L"Cannot obtain memory map", = status);=0A+ for ( retry =3D 0; ; retry =3D 1 )=0A+ {=0A+ = status =3D efi_bs->GetMemoryMap(&efi_memmap_size, efi_memmap, &map_key,=0A+= &efi_mdesc_size, &mdesc_ver);=0A+ = if ( EFI_ERROR(status) )=0A+ PrintErrMesg(L"Cannot obtain = memory map", status);=0A =0A- efi_arch_process_memory_map(SystemTable, = efi_memmap, efi_memmap_size,=0A- efi_mdesc_s= ize, mdesc_ver);=0A+ efi_arch_process_memory_map(SystemTable, = efi_memmap, efi_memmap_size,=0A+ = efi_mdesc_size, mdesc_ver);=0A =0A- efi_arch_pre_exit_boot();=0A+ = efi_arch_pre_exit_boot();=0A+=0A+ status =3D efi_bs->ExitBootServic= es(ImageHandle, map_key);=0A+ if ( status !=3D EFI_INVALID_PARAMETER= || retry )=0A+ break;=0A+ }=0A =0A- status =3D efi_bs->Ex= itBootServices(ImageHandle, map_key);=0A if ( EFI_ERROR(status) )=0A = PrintErrMesg(L"Cannot exit boot services", status);=0A =0A --=__Part3401089A.1__= 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 --=__Part3401089A.1__=--