]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/PlatformPei: clear CPU caches
authorMarc-André Lureau <marcandre.lureau@redhat.com>
Tue, 2 Oct 2018 12:17:25 +0000 (16:17 +0400)
committerLaszlo Ersek <lersek@redhat.com>
Fri, 5 Oct 2018 20:02:55 +0000 (22:02 +0200)
This is for conformance with the TCG "Platform Reset Attack Mitigation
Specification". Because clearing the CPU caches at boot doesn't impact
performance significantly, do it unconditionally, for simplicity's
sake.

Flush the cache on all logical processors, thanks to
EFI_PEI_MP_SERVICES_PPI and CacheMaintenanceLib.

Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Anthony Perard <anthony.perard@citrix.com>
Cc: Julien Grall <julien.grall@linaro.org>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Tested-by: Anthony PERARD <anthony.perard@citrix.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Regression-tested-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
[lersek@redhat.com: remove bogus Message-Id line from commit msg]

OvmfPkg/PlatformPei/ClearCache.c [new file with mode: 0644]
OvmfPkg/PlatformPei/Platform.c
OvmfPkg/PlatformPei/Platform.h
OvmfPkg/PlatformPei/PlatformPei.inf

diff --git a/OvmfPkg/PlatformPei/ClearCache.c b/OvmfPkg/PlatformPei/ClearCache.c
new file mode 100644 (file)
index 0000000..7d15fd9
--- /dev/null
@@ -0,0 +1,117 @@
+/**@file\r
+  Install a callback to clear cache on all processors.\r
+  This is for conformance with the TCG "Platform Reset Attack Mitigation\r
+  Specification". Because clearing the CPU caches at boot doesn't impact\r
+  performance significantly, do it unconditionally, for simplicity's\r
+  sake.\r
+\r
+  Copyright (C) 2018, Red Hat, Inc.\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+**/\r
+\r
+#include <Library/CacheMaintenanceLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PeiServicesLib.h>\r
+#include <Ppi/MpServices.h>\r
+\r
+#include "Platform.h"\r
+\r
+/**\r
+  Invalidate data & instruction caches.\r
+  All APs execute this function in parallel. The BSP executes the function\r
+  separately.\r
+\r
+  @param[in,out] WorkSpace  Pointer to the input/output argument workspace\r
+                            shared by all processors.\r
+**/\r
+STATIC\r
+VOID\r
+EFIAPI\r
+ClearCache (\r
+  IN OUT VOID *WorkSpace\r
+  )\r
+{\r
+  WriteBackInvalidateDataCache ();\r
+  InvalidateInstructionCache ();\r
+}\r
+\r
+/**\r
+  Notification function called when EFI_PEI_MP_SERVICES_PPI becomes available.\r
+\r
+  @param[in] PeiServices      Indirect reference to the PEI Services Table.\r
+  @param[in] NotifyDescriptor Address of the notification descriptor data\r
+                              structure.\r
+  @param[in] Ppi              Address of the PPI that was installed.\r
+\r
+  @return  Status of the notification. The status code returned from this\r
+           function is ignored.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+ClearCacheOnMpServicesAvailable (\r
+  IN EFI_PEI_SERVICES           **PeiServices,\r
+  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,\r
+  IN VOID                       *Ppi\r
+  )\r
+{\r
+  EFI_PEI_MP_SERVICES_PPI *MpServices;\r
+  EFI_STATUS              Status;\r
+\r
+  DEBUG ((DEBUG_INFO, "%a: %a\n", gEfiCallerBaseName, __FUNCTION__));\r
+\r
+  //\r
+  // Clear cache on all the APs in parallel.\r
+  //\r
+  MpServices = Ppi;\r
+  Status = MpServices->StartupAllAPs (\r
+                         (CONST EFI_PEI_SERVICES **)PeiServices,\r
+                         MpServices,\r
+                         ClearCache,          // Procedure\r
+                         FALSE,               // SingleThread\r
+                         0,                   // TimeoutInMicroSeconds: inf.\r
+                         NULL                 // ProcedureArgument\r
+                         );\r
+  if (EFI_ERROR (Status) && Status != EFI_NOT_STARTED) {\r
+    DEBUG ((DEBUG_ERROR, "%a: StartupAllAps(): %r\n", __FUNCTION__, Status));\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Now clear cache on the BSP too.\r
+  //\r
+  ClearCache (NULL);\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+//\r
+// Notification object for registering the callback, for when\r
+// EFI_PEI_MP_SERVICES_PPI becomes available.\r
+//\r
+STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mMpServicesNotify = {\r
+  EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | // Flags\r
+  EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,\r
+  &gEfiPeiMpServicesPpiGuid,               // Guid\r
+  ClearCacheOnMpServicesAvailable          // Notify\r
+};\r
+\r
+VOID\r
+InstallClearCacheCallback (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS           Status;\r
+\r
+  Status = PeiServicesNotifyPpi (&mMpServicesNotify);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((DEBUG_ERROR, "%a: failed to set up MP Services callback: %r\n",\r
+      __FUNCTION__, Status));\r
+  }\r
+}\r
index 5a78668126b4cc70eaf0aab84ed062d33928d0ca..22139a64cbf4ef856ff6a3f1131e46df24493bbf 100644 (file)
@@ -672,6 +672,7 @@ InitializePlatform (
     NoexecDxeInitialization ();\r
   }\r
 \r
+  InstallClearCacheCallback ();\r
   AmdSevInitialize ();\r
   MiscInitialization ();\r
   InstallFeatureControlCallback ();\r
index f942e61bb4f90ab16bbc18420ca97f582e1b5fea..b12a5c1f5f78e70ae476a62ba7d77460db66aa8c 100644 (file)
@@ -83,6 +83,11 @@ InstallFeatureControlCallback (
   VOID\r
   );\r
 \r
+VOID\r
+InstallClearCacheCallback (\r
+  VOID\r
+  );\r
+\r
 EFI_STATUS\r
 InitializeXen (\r
   VOID\r
index 9c5ad9961c4a3608c27f1c3d9a9bc4ccbdc9df13..5c8dd0fe6d72239bc4ba6613bc9c441ed5e1dd0f 100644 (file)
@@ -30,6 +30,7 @@
 \r
 [Sources]\r
   AmdSev.c\r
+  ClearCache.c\r
   Cmos.c\r
   Cmos.h\r
   FeatureControl.c\r
@@ -54,6 +55,7 @@
 \r
 [LibraryClasses]\r
   BaseLib\r
+  CacheMaintenanceLib\r
   DebugLib\r
   HobLib\r
   IoLib\r