]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/QemuFlashFvbServicesRuntimeDxe: Use physical address with SEV-ES
authorTom Lendacky <thomas.lendacky@amd.com>
Sat, 23 Jan 2021 13:57:44 +0000 (07:57 -0600)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Tue, 26 Jan 2021 00:25:16 +0000 (00:25 +0000)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3183

Under SEV-ES, a write to the flash device is done using a direct VMGEXIT
to perform an MMIO write. The address provided to the MMIO write must be
the physical address of the MMIO write destitnation. During boot, OVMF
runs with an identity mapped pagetable structure so that VA == PA and the
VMGEXIT MMIO write destination is just the virtual address of the flash
area address being written.

However, when the UEFI SetVirtualAddressMap() API is invoked, an identity
mapped pagetable structure may not be in place and using the virtual
address for the flash area address is no longer valid. This results in
writes to the flash not being performed successfully. This can be seen
by attempting to change the boot order under Linux. The update will
appear to be performed, based on the output of the command. But rebooting
the guest will show that the new boot order has not been set.

To remedy this, save the value of the flash base physical address before
converting the address as part of SetVirtualAddressMap(). The physical
address can then be calculated by obtaining the offset of the MMIO target
virtual address relative to the flash base virtual address and adding that
to the original flash base physical address. The resulting value produces
a successful MMIO write during runtime services.

Fixes: 437eb3f7a8db7681afe0e6064d3a8edb12abb766
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Message-Id: <84a5f9161541db5aa3b57c96b737afbcb4b6189d.1611410263.git.thomas.lendacky@amd.com>
[lersek@redhat.com: SetVitualAddressMap() -> SetVirtualAddressMap() typo
 fix, in both the commit message and the code comment]
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
OvmfPkg/QemuFlashFvbServicesRuntimeDxe/QemuFlashDxe.c

index 1b0742967f719985d60a9e29ff4cb6a8f25f3c12..63daa0b55b4955fb47bfb8a27d3b9f10326bcfa9 100644 (file)
 \r
 #include "QemuFlash.h"\r
 \r
+STATIC EFI_PHYSICAL_ADDRESS mSevEsFlashPhysBase;\r
+\r
 VOID\r
 QemuFlashConvertPointers (\r
   VOID\r
   )\r
 {\r
+  if (MemEncryptSevEsIsEnabled ()) {\r
+    mSevEsFlashPhysBase = (UINTN) mFlashBase;\r
+  }\r
+\r
   EfiConvertPointer (0x0, (VOID **) &mFlashBase);\r
 }\r
 \r
@@ -52,11 +58,23 @@ QemuFlashPtrWrite (
   if (MemEncryptSevEsIsEnabled ()) {\r
     MSR_SEV_ES_GHCB_REGISTER  Msr;\r
     GHCB                      *Ghcb;\r
+    EFI_PHYSICAL_ADDRESS      PhysAddr;\r
     BOOLEAN                   InterruptState;\r
 \r
     Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);\r
     Ghcb = Msr.Ghcb;\r
 \r
+    //\r
+    // The MMIO write needs to be to the physical address of the flash pointer.\r
+    // Since this service is available as part of the EFI runtime services,\r
+    // account for a non-identity mapped VA after SetVirtualAddressMap().\r
+    //\r
+    if (mSevEsFlashPhysBase == 0) {\r
+      PhysAddr = (UINTN) Ptr;\r
+    } else {\r
+      PhysAddr = mSevEsFlashPhysBase + (Ptr - mFlashBase);\r
+    }\r
+\r
     //\r
     // Writing to flash is emulated by the hypervisor through the use of write\r
     // protection. This won't work for an SEV-ES guest because the write won't\r
@@ -68,7 +86,7 @@ QemuFlashPtrWrite (
     Ghcb->SharedBuffer[0] = Value;\r
     Ghcb->SaveArea.SwScratch = (UINT64) (UINTN) Ghcb->SharedBuffer;\r
     VmgSetOffsetValid (Ghcb, GhcbSwScratch);\r
-    VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, (UINT64) (UINTN) Ptr, 1);\r
+    VmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, PhysAddr, 1);\r
     VmgDone (Ghcb, InterruptState);\r
   } else {\r
     *Ptr = Value;\r