]> xenbits.xensource.com Git - people/aperard/ovmf.git/commitdiff
OvmfPkg/OvmfXen: Introduce Xen's ResetSystemLib, to use xen hypercall br.fix-pvh-shutdown-v2-v1
authorAnthony PERARD <anthony.perard@citrix.com>
Mon, 19 Sep 2022 16:18:16 +0000 (17:18 +0100)
committerAnthony PERARD <anthony.perard@citrix.com>
Fri, 11 Nov 2022 15:02:00 +0000 (15:02 +0000)
When booting OvmfXen, the ACPI interface for shutdown/reset might not
be available, instead use the hypercall interface.

While it's probably possible to use the hypercall in all cases, we
keep using the same interface while it still possible. That is ACPI on
HVM guest, and fallback to hypercall on PVH guest.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
OvmfPkg/Library/ResetSystemLib/BaseResetShutdownXen.c [new file with mode: 0644]
OvmfPkg/Library/ResetSystemLib/BaseResetSystemLibXen.inf [new file with mode: 0644]
OvmfPkg/Library/ResetSystemLib/DxeResetShutdownXen.c [new file with mode: 0644]
OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf [new file with mode: 0644]
OvmfPkg/OvmfXen.dsc

diff --git a/OvmfPkg/Library/ResetSystemLib/BaseResetShutdownXen.c b/OvmfPkg/Library/ResetSystemLib/BaseResetShutdownXen.c
new file mode 100644 (file)
index 0000000..f45d912
--- /dev/null
@@ -0,0 +1,65 @@
+/** @file\r
+  Base Reset System Library Shutdown API implementation for OVMF.\r
+\r
+  Copyright (C) 2020, Red Hat, Inc.\r
+  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2022, Citrix Systems, Inc.\r
+\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Base.h>                   // BIT13\r
+\r
+#include <Library/BaseLib.h>        // CpuDeadLoop()\r
+#include <Library/DebugLib.h>       // ASSERT()\r
+#include <Library/IoLib.h>          // IoOr16()\r
+#include <Library/PciLib.h>         // PciRead16()\r
+#include <Library/ResetSystemLib.h> // ResetShutdown()\r
+#include <Library/XenHypercallLib.h>\r
+#include <OvmfPlatforms.h>          // OVMF_HOSTBRIDGE_DID\r
+\r
+/**\r
+  Calling this function causes the system to enter a power state equivalent\r
+  to the ACPI G2/S5 or G3 states.\r
+\r
+  System shutdown should not return, if it returns, it means the system does\r
+  not support shut down reset.\r
+**/\r
+VOID\r
+EFIAPI\r
+ResetShutdown (\r
+  VOID\r
+  )\r
+{\r
+  UINT16  AcpiPmBaseAddress;\r
+  UINT16  HostBridgeDevId;\r
+\r
+  AcpiPmBaseAddress = 0;\r
+  HostBridgeDevId   = PciRead16 (OVMF_HOSTBRIDGE_DID);\r
+  switch (HostBridgeDevId) {\r
+    case INTEL_82441_DEVICE_ID:\r
+      AcpiPmBaseAddress = PIIX4_PMBA_VALUE;\r
+      break;\r
+    case INTEL_Q35_MCH_DEVICE_ID:\r
+      AcpiPmBaseAddress = ICH9_PMBASE_VALUE;\r
+      break;\r
+    default:\r
+    {\r
+      //\r
+      // Fallback to using hypercall.\r
+      // Necessary for PVH guest, but should work for HVM guest.\r
+      //\r
+      INTN                ReturnCode;\r
+      XEN_SCHED_SHUTDOWN  ShutdownOp = {\r
+        .Reason = XEN_SHED_SHUTDOWN_POWEROFF,\r
+      };\r
+      ReturnCode = XenHypercallSchedOp (XEN_SCHEDOP_SHUTDOWN, ShutdownOp);\r
+      ASSERT (ReturnCode == 0);\r
+      CpuDeadLoop ();\r
+    }\r
+  }\r
+\r
+  IoBitFieldWrite16 (AcpiPmBaseAddress + 4, 10, 13, 0);\r
+  IoOr16 (AcpiPmBaseAddress + 4, BIT13);\r
+  CpuDeadLoop ();\r
+}\r
diff --git a/OvmfPkg/Library/ResetSystemLib/BaseResetSystemLibXen.inf b/OvmfPkg/Library/ResetSystemLib/BaseResetSystemLibXen.inf
new file mode 100644 (file)
index 0000000..8d75dd5
--- /dev/null
@@ -0,0 +1,41 @@
+## @file\r
+#  Base library instance for ResetSystem library class for Xen\r
+#\r
+#  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2022, Citrix Systems, Inc.\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = BaseResetSystemLib\r
+  FILE_GUID                      = 9ef32aa1-9e82-4fb1-9c49-0eff538601f8\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = ResetSystemLib|SEC PEI_CORE PEIM DXE_CORE\r
+\r
+#\r
+# The following information is for reference only and not required by the build\r
+# tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  BaseResetShutdownXen.c\r
+  ResetSystemLib.c\r
+\r
+[Packages]\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  MdePkg/MdePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  DebugLib\r
+  IoLib\r
+  PciLib\r
+  TimerLib\r
+  XenHypercallLib\r
diff --git a/OvmfPkg/Library/ResetSystemLib/DxeResetShutdownXen.c b/OvmfPkg/Library/ResetSystemLib/DxeResetShutdownXen.c
new file mode 100644 (file)
index 0000000..f7f3276
--- /dev/null
@@ -0,0 +1,77 @@
+/** @file\r
+  DXE Reset System Library Shutdown API implementation for OVMF.\r
+\r
+  Copyright (C) 2020, Red Hat, Inc.\r
+  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+**/\r
+\r
+#include <Base.h>                   // BIT13\r
+\r
+#include <IndustryStandard/Xen/sched.h>\r
+#include <Library/BaseLib.h>        // CpuDeadLoop()\r
+#include <Library/DebugLib.h>       // ASSERT()\r
+#include <Library/IoLib.h>          // IoOr16()\r
+#include <Library/PcdLib.h>         // PcdGet16()\r
+#include <Library/ResetSystemLib.h> // ResetShutdown()\r
+#include <Library/XenHypercallLib.h>\r
+#include <OvmfPlatforms.h>          // PIIX4_PMBA_VALUE\r
+\r
+STATIC UINT16  mAcpiPmBaseAddress;\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+DxeResetInit (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  UINT16  HostBridgeDevId;\r
+\r
+  HostBridgeDevId = PcdGet16 (PcdOvmfHostBridgePciDevId);\r
+  switch (HostBridgeDevId) {\r
+    case INTEL_82441_DEVICE_ID:\r
+      mAcpiPmBaseAddress = PIIX4_PMBA_VALUE;\r
+      break;\r
+    case INTEL_Q35_MCH_DEVICE_ID:\r
+      mAcpiPmBaseAddress = ICH9_PMBASE_VALUE;\r
+      break;\r
+    default:\r
+      //\r
+      // Fallback to using hypercall.\r
+      // Necessary for PVH guest, but should work for HVM guest.\r
+      //\r
+      mAcpiPmBaseAddress = 0xffff;\r
+      break;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Calling this function causes the system to enter a power state equivalent\r
+  to the ACPI G2/S5 or G3 states.\r
+\r
+  System shutdown should not return, if it returns, it means the system does\r
+  not support shut down reset.\r
+**/\r
+VOID\r
+EFIAPI\r
+ResetShutdown (\r
+  VOID\r
+  )\r
+{\r
+  if (mAcpiPmBaseAddress != 0xffff) {\r
+    IoBitFieldWrite16 (mAcpiPmBaseAddress + 4, 10, 13, 0);\r
+    IoOr16 (mAcpiPmBaseAddress + 4, BIT13);\r
+  } else {\r
+    INTN                ReturnCode;\r
+    XEN_SCHED_SHUTDOWN  ShutdownOp = {\r
+      .Reason = XEN_SHED_SHUTDOWN_POWEROFF,\r
+    };\r
+    ReturnCode = XenHypercallSchedOp (XEN_SCHEDOP_SHUTDOWN, &ShutdownOp);\r
+    ASSERT (ReturnCode == 0);\r
+  }\r
+\r
+  CpuDeadLoop ();\r
+}\r
diff --git a/OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf b/OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf
new file mode 100644 (file)
index 0000000..ccee69e
--- /dev/null
@@ -0,0 +1,46 @@
+## @file\r
+#  DXE library instance for ResetSystem library class for Xen\r
+#\r
+#  Copyright (C) 2020, Red Hat, Inc.\r
+#  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2022, Citrix Systems, Inc.\r
+#\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 1.29\r
+  BASE_NAME                      = DxeResetSystemLibXen\r
+  FILE_GUID                      = a5ac25e6-4dc5-4fd9-92cd-74e46bd2e72a\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = ResetSystemLib|DXE_DRIVER DXE_RUNTIME_DRIVER SMM_CORE DXE_SMM_DRIVER UEFI_DRIVER UEFI_APPLICATION\r
+  CONSTRUCTOR                    = DxeResetInit\r
+\r
+#\r
+# The following information is for reference only and not required by the build\r
+# tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  DxeResetShutdownXen.c\r
+  ResetSystemLib.c\r
+\r
+[Packages]\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  MdePkg/MdePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseLib\r
+  DebugLib\r
+  IoLib\r
+  PcdLib\r
+  TimerLib\r
+  XenHypercallLib\r
+\r
+[Pcd]\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfHostBridgePciDevId ## CONSUMES\r
index 8df173a0ee84657a587f3fd35d181e19fce467f6..18f7c0d7664337a69dd1dba1cb0fee19ae646c4f 100644 (file)
 [LibraryClasses]\r
   PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf\r
   TimerLib|MdePkg/Library/SecPeiDxeTimerLibCpu/SecPeiDxeTimerLibCpu.inf\r
-  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLib.inf\r
+  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/BaseResetSystemLibXen.inf\r
   PrintLib|MdePkg/Library/BasePrintLib/BasePrintLib.inf\r
   BaseMemoryLib|MdePkg/Library/BaseMemoryLibRepStr/BaseMemoryLibRepStr.inf\r
   BaseLib|MdePkg/Library/BaseLib/BaseLib.inf\r
 \r
 [LibraryClasses.common.DXE_RUNTIME_DRIVER]\r
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf\r
-  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLib.inf\r
+  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
   DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf\r
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf\r
 \r
 [LibraryClasses.common.UEFI_DRIVER]\r
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf\r
-  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLib.inf\r
+  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
   DxeCoreEntryPoint|MdePkg/Library/DxeCoreEntryPoint/DxeCoreEntryPoint.inf\r
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf\r
 \r
 [LibraryClasses.common.DXE_DRIVER]\r
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf\r
-  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLib.inf\r
+  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf\r
   ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeReportStatusCodeLib.inf\r
 \r
 [LibraryClasses.common.UEFI_APPLICATION]\r
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf\r
-  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLib.inf\r
+  ResetSystemLib|OvmfPkg/Library/ResetSystemLib/DxeResetSystemLibXen.inf\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf\r
   ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/DxeReportStatusCodeLib.inf\r