]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ArmPkg/ArmSmcPsciResetSystemLib: add missing call to ExitBootServices()
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 20 Nov 2018 01:53:13 +0000 (17:53 -0800)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Wed, 21 Nov 2018 11:02:34 +0000 (12:02 +0100)
Our poor man's implementation of EnterS3WithImmediateWake () currently
sets a high TPL level to disable interrupts, and simply calls the
PEI entrypoint again after disabling the MMU.

Unfortunately, this is not sufficient: DMA capable devices such as
network controllers or USB controllers may still be enabled and
writing to memory, e.g., in response to incoming network packets.

So instead, do the full ExitBootServices() dance: allocate space and
get the memory map, call ExitBootServices(), and in case it fails, get
the memory map again and call ExitBootServices() again. This ensures
that all cleanup related to DMA capable devices is performed before
doing the warm reset.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Leif Lindholm <leif.lindholm@linaro.org>
ArmPkg/Library/ArmSmcPsciResetSystemLib/AArch64/Reset.S [new file with mode: 0644]
ArmPkg/Library/ArmSmcPsciResetSystemLib/AArch64/Reset.asm [new file with mode: 0644]
ArmPkg/Library/ArmSmcPsciResetSystemLib/Arm/Reset.S [new file with mode: 0644]
ArmPkg/Library/ArmSmcPsciResetSystemLib/Arm/Reset.asm [new file with mode: 0644]
ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c
ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.inf

diff --git a/ArmPkg/Library/ArmSmcPsciResetSystemLib/AArch64/Reset.S b/ArmPkg/Library/ArmSmcPsciResetSystemLib/AArch64/Reset.S
new file mode 100644 (file)
index 0000000..1edca94
--- /dev/null
@@ -0,0 +1,30 @@
+/** @file
+  ResetSystemLib implementation using PSCI calls
+
+  Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution. The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <AsmMacroIoLibV8.h>
+
+ASM_FUNC(DisableMmuAndReenterPei)
+  stp   x29, x30, [sp, #-16]!
+  mov   x29, sp
+
+  bl    ArmDisableMmu
+
+  // no memory accesses after MMU and caches have been disabled
+
+  MOV64 (x0, FixedPcdGet64 (PcdFvBaseAddress))
+  blr   x0
+
+  // never returns
+  nop
diff --git a/ArmPkg/Library/ArmSmcPsciResetSystemLib/AArch64/Reset.asm b/ArmPkg/Library/ArmSmcPsciResetSystemLib/AArch64/Reset.asm
new file mode 100644 (file)
index 0000000..9e30c13
--- /dev/null
@@ -0,0 +1,35 @@
+;/** @file
+;  ResetSystemLib implementation using PSCI calls
+;
+;  Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
+;
+;  This program and the accompanying materials
+;  are licensed and made available under the terms and conditions of the BSD License
+;  which accompanies this distribution. The full text of the license may be found at
+;  http://opensource.org/licenses/bsd-license.php
+;
+;  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+;  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+;
+;**/
+
+  AREA Reset, CODE, READONLY
+
+  EXPORT DisableMmuAndReenterPei
+  IMPORT ArmDisableMmu
+
+DisableMmuAndReenterPei
+  stp   x29, x30, [sp, #-16]!
+  mov   x29, sp
+
+  bl    ArmDisableMmu
+
+  ; no memory accesses after MMU and caches have been disabled
+
+  movl  x0, FixedPcdGet64 (PcdFvBaseAddress)
+  blr   x0
+
+  ; never returns
+  nop
+
+  END
diff --git a/ArmPkg/Library/ArmSmcPsciResetSystemLib/Arm/Reset.S b/ArmPkg/Library/ArmSmcPsciResetSystemLib/Arm/Reset.S
new file mode 100644 (file)
index 0000000..b6fe2bd
--- /dev/null
@@ -0,0 +1,29 @@
+/** @file
+  ResetSystemLib implementation using PSCI calls
+
+  Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
+
+  This program and the accompanying materials
+  are licensed and made available under the terms and conditions of the BSD License
+  which accompanies this distribution. The full text of the license may be found at
+  http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+
+**/
+
+#include <AsmMacroIoLib.h>
+
+ASM_FUNC(DisableMmuAndReenterPei)
+  push  {lr}
+
+  bl    ArmDisableMmu
+
+  // no memory accesses after MMU and caches have been disabled
+
+  MOV32 (r0, FixedPcdGet64 (PcdFvBaseAddress))
+  blx   r0
+
+  // never returns
+  nop
diff --git a/ArmPkg/Library/ArmSmcPsciResetSystemLib/Arm/Reset.asm b/ArmPkg/Library/ArmSmcPsciResetSystemLib/Arm/Reset.asm
new file mode 100644 (file)
index 0000000..fa4fbd0
--- /dev/null
@@ -0,0 +1,34 @@
+;/** @file
+;  ResetSystemLib implementation using PSCI calls
+;
+;  Copyright (c) 2018, Linaro Ltd. All rights reserved.<BR>
+;
+;  This program and the accompanying materials
+;  are licensed and made available under the terms and conditions of the BSD License
+;  which accompanies this distribution. The full text of the license may be found at
+;  http://opensource.org/licenses/bsd-license.php
+;
+;  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
+;  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
+;
+;**/
+
+  INCLUDE AsmMacroExport.inc
+  PRESERVE8
+
+  IMPORT ArmDisableMmu
+
+RVCT_ASM_EXPORT DisableMmuAndReenterPei
+  push  {lr}
+
+  bl    ArmDisableMmu
+
+  ; no memory accesses after MMU and caches have been disabled
+
+  mov32 r0, FixedPcdGet64 (PcdFvBaseAddress)
+  blx   r0
+
+  ; never returns
+  nop
+
+  END
index 10ceafd14d5de0de5465817615f7650a81ae7073..3f7b8ae0b16907a31fc3b5f9cd374cc0e18d9c0c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   ResetSystemLib implementation using PSCI calls\r
 \r
 /** @file\r
   ResetSystemLib implementation using PSCI calls\r
 \r
-  Copyright (c) 2017, Linaro Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2017 - 2018, Linaro Ltd. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -81,6 +81,8 @@ ResetShutdown (
   ArmCallSmc (&ArmSmcArgs);\r
 }\r
 \r
   ArmCallSmc (&ArmSmcArgs);\r
 }\r
 \r
+VOID DisableMmuAndReenterPei (VOID);\r
+\r
 /**\r
   This function causes the system to enter S3 and then wake up immediately.\r
 \r
 /**\r
   This function causes the system to enter S3 and then wake up immediately.\r
 \r
@@ -92,7 +94,12 @@ EnterS3WithImmediateWake (
   VOID\r
   )\r
 {\r
   VOID\r
   )\r
 {\r
-  VOID (*Reset)(VOID);\r
+  EFI_PHYSICAL_ADDRESS        Alloc;\r
+  EFI_MEMORY_DESCRIPTOR       *MemMap;\r
+  UINTN                       MemMapSize;\r
+  UINTN                       MapKey, DescriptorSize;\r
+  UINT32                      DescriptorVersion;\r
+  EFI_STATUS                  Status;\r
 \r
   if (FeaturePcdGet (PcdArmReenterPeiForCapsuleWarmReboot) &&\r
       !EfiAtRuntime ()) {\r
 \r
   if (FeaturePcdGet (PcdArmReenterPeiForCapsuleWarmReboot) &&\r
       !EfiAtRuntime ()) {\r
@@ -101,11 +108,49 @@ EnterS3WithImmediateWake (
     // immediate wake (which is used by capsule update) by disabling the MMU\r
     // and interrupts, and jumping to the PEI entry point.\r
     //\r
     // immediate wake (which is used by capsule update) by disabling the MMU\r
     // and interrupts, and jumping to the PEI entry point.\r
     //\r
-    Reset = (VOID (*)(VOID))(UINTN)FixedPcdGet64 (PcdFvBaseAddress);\r
 \r
 \r
-    gBS->RaiseTPL (TPL_HIGH_LEVEL);\r
-    ArmDisableMmu ();\r
-    Reset ();\r
+    //\r
+    // Obtain the size of the memory map\r
+    //\r
+    MemMapSize = 0;\r
+    MemMap = NULL;\r
+    Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,\r
+                    &DescriptorVersion);\r
+    ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
+\r
+    //\r
+    // Add some slack to the allocation to cater for changes in the memory\r
+    // map if ExitBootServices () fails the first time around.\r
+    //\r
+    MemMapSize += SIZE_4KB;\r
+    Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData,\r
+                    EFI_SIZE_TO_PAGES (MemMapSize), &Alloc);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    MemMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)Alloc;\r
+\r
+    Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,\r
+                    &DescriptorVersion);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    Status = gBS->ExitBootServices (gImageHandle, MapKey);\r
+    if (EFI_ERROR (Status)) {\r
+      //\r
+      // ExitBootServices () may fail the first time around if an event fired\r
+      // right after the call to GetMemoryMap() which allocated or freed memory.\r
+      // Since that first call to ExitBootServices () will disarm the timer,\r
+      // this is guaranteed not to happen again, so one additional attempt\r
+      // should suffice.\r
+      //\r
+      Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize,\r
+                      &DescriptorVersion);\r
+      ASSERT_EFI_ERROR (Status);\r
+\r
+      Status = gBS->ExitBootServices (gImageHandle, MapKey);\r
+      ASSERT_EFI_ERROR (Status);\r
+    }\r
+\r
+    DisableMmuAndReenterPei ();\r
   }\r
 }\r
 \r
   }\r
 }\r
 \r
index 19021cd1e8b695e840bdbf8db9d06e52c9c1d0ce..3b8925c6f9cd9bfbb6e0beea79ee74611cd463ed 100644 (file)
   VERSION_STRING                 = 1.0\r
   LIBRARY_CLASS                  = ResetSystemLib\r
 \r
   VERSION_STRING                 = 1.0\r
   LIBRARY_CLASS                  = ResetSystemLib\r
 \r
+[Sources.AARCH64]\r
+  AArch64/Reset.S   | GCC\r
+  AArch64/Reset.asm | MSFT\r
+\r
+[Sources.ARM]\r
+  Arm/Reset.S       | GCC\r
+  Arm/Reset.asm     | RVCT\r
+\r
 [Sources]\r
   ArmSmcPsciResetSystemLib.c\r
 \r
 [Sources]\r
   ArmSmcPsciResetSystemLib.c\r
 \r