2 OVMF support for QEMU system firmware flash device: functions specific to the
3 runtime DXE driver build.
5 Copyright (C) 2015, Red Hat, Inc.
6 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/UefiRuntimeLib.h>
13 #include <Library/MemEncryptSevLib.h>
14 #include <Library/VmgExitLib.h>
15 #include <Register/Amd/Msr.h>
17 #include "QemuFlash.h"
19 STATIC EFI_PHYSICAL_ADDRESS mSevEsFlashPhysBase
;
22 QemuFlashConvertPointers (
26 if (MemEncryptSevEsIsEnabled ()) {
27 mSevEsFlashPhysBase
= (UINTN
)mFlashBase
;
30 EfiConvertPointer (0x0, (VOID
**)&mFlashBase
);
34 QemuFlashBeforeProbe (
35 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
48 @param[in] Ptr Pointer to the location to write.
49 @param[in] Value The value to write.
54 IN
volatile UINT8
*Ptr
,
58 if (MemEncryptSevEsIsEnabled ()) {
59 MSR_SEV_ES_GHCB_REGISTER Msr
;
61 EFI_PHYSICAL_ADDRESS PhysAddr
;
62 BOOLEAN InterruptState
;
64 Msr
.GhcbPhysicalAddress
= AsmReadMsr64 (MSR_SEV_ES_GHCB
);
68 // The MMIO write needs to be to the physical address of the flash pointer.
69 // Since this service is available as part of the EFI runtime services,
70 // account for a non-identity mapped VA after SetVirtualAddressMap().
72 if (mSevEsFlashPhysBase
== 0) {
73 PhysAddr
= (UINTN
)Ptr
;
75 PhysAddr
= mSevEsFlashPhysBase
+ (Ptr
- mFlashBase
);
79 // Writing to flash is emulated by the hypervisor through the use of write
80 // protection. This won't work for an SEV-ES guest because the write won't
81 // be recognized as a true MMIO write, which would result in the required
82 // #VC exception. Instead, use the the VMGEXIT MMIO write support directly
83 // to perform the update.
85 VmgInit (Ghcb
, &InterruptState
);
86 Ghcb
->SharedBuffer
[0] = Value
;
87 Ghcb
->SaveArea
.SwScratch
= (UINT64
)(UINTN
)Ghcb
->SharedBuffer
;
88 VmgSetOffsetValid (Ghcb
, GhcbSwScratch
);
89 VmgExit (Ghcb
, SVM_EXIT_MMIO_WRITE
, PhysAddr
, 1);
90 VmgDone (Ghcb
, InterruptState
);