2015-10-26 15:58:39 +01:00
|
|
|
/** @file
|
|
|
|
OVMF support for QEMU system firmware flash device: functions specific to the
|
|
|
|
runtime DXE driver build.
|
|
|
|
|
|
|
|
Copyright (C) 2015, Red Hat, Inc.
|
|
|
|
Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
|
|
|
|
|
2019-04-04 01:06:33 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2015-10-26 15:58:39 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include <Library/UefiRuntimeLib.h>
|
2020-08-12 22:21:42 +02:00
|
|
|
#include <Library/MemEncryptSevLib.h>
|
2022-11-07 07:30:26 +01:00
|
|
|
#include <Library/CcExitLib.h>
|
2020-08-12 22:21:42 +02:00
|
|
|
#include <Register/Amd/Msr.h>
|
2015-10-26 15:58:39 +01:00
|
|
|
|
|
|
|
#include "QemuFlash.h"
|
|
|
|
|
2021-01-23 14:57:44 +01:00
|
|
|
STATIC EFI_PHYSICAL_ADDRESS mSevEsFlashPhysBase;
|
|
|
|
|
2015-10-26 15:58:39 +01:00
|
|
|
VOID
|
|
|
|
QemuFlashConvertPointers (
|
|
|
|
VOID
|
|
|
|
)
|
|
|
|
{
|
2021-01-23 14:57:44 +01:00
|
|
|
if (MemEncryptSevEsIsEnabled ()) {
|
|
|
|
mSevEsFlashPhysBase = (UINTN)mFlashBase;
|
|
|
|
}
|
|
|
|
|
2015-10-26 15:58:39 +01:00
|
|
|
EfiConvertPointer (0x0, (VOID **)&mFlashBase);
|
|
|
|
}
|
2018-03-09 17:06:03 +01:00
|
|
|
|
|
|
|
VOID
|
|
|
|
QemuFlashBeforeProbe (
|
|
|
|
IN EFI_PHYSICAL_ADDRESS BaseAddress,
|
|
|
|
IN UINTN FdBlockSize,
|
|
|
|
IN UINTN FdBlockCount
|
|
|
|
)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Do nothing
|
|
|
|
//
|
|
|
|
}
|
2020-08-12 22:21:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
Write to QEMU Flash
|
|
|
|
|
|
|
|
@param[in] Ptr Pointer to the location to write.
|
|
|
|
@param[in] Value The value to write.
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
QemuFlashPtrWrite (
|
|
|
|
IN volatile UINT8 *Ptr,
|
|
|
|
IN UINT8 Value
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (MemEncryptSevEsIsEnabled ()) {
|
|
|
|
MSR_SEV_ES_GHCB_REGISTER Msr;
|
|
|
|
GHCB *Ghcb;
|
2021-01-23 14:57:44 +01:00
|
|
|
EFI_PHYSICAL_ADDRESS PhysAddr;
|
2020-11-06 18:53:12 +01:00
|
|
|
BOOLEAN InterruptState;
|
2020-08-12 22:21:42 +02:00
|
|
|
|
|
|
|
Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
|
|
|
|
Ghcb = Msr.Ghcb;
|
|
|
|
|
2021-01-23 14:57:44 +01:00
|
|
|
//
|
|
|
|
// The MMIO write needs to be to the physical address of the flash pointer.
|
|
|
|
// Since this service is available as part of the EFI runtime services,
|
|
|
|
// account for a non-identity mapped VA after SetVirtualAddressMap().
|
|
|
|
//
|
|
|
|
if (mSevEsFlashPhysBase == 0) {
|
|
|
|
PhysAddr = (UINTN)Ptr;
|
|
|
|
} else {
|
|
|
|
PhysAddr = mSevEsFlashPhysBase + (Ptr - mFlashBase);
|
|
|
|
}
|
|
|
|
|
2020-08-12 22:21:42 +02:00
|
|
|
//
|
|
|
|
// Writing to flash is emulated by the hypervisor through the use of write
|
|
|
|
// protection. This won't work for an SEV-ES guest because the write won't
|
|
|
|
// be recognized as a true MMIO write, which would result in the required
|
2022-08-23 18:00:22 +02:00
|
|
|
// #VC exception. Instead, use the VMGEXIT MMIO write support directly
|
2020-08-12 22:21:42 +02:00
|
|
|
// to perform the update.
|
|
|
|
//
|
2022-11-07 08:50:11 +01:00
|
|
|
CcExitVmgInit (Ghcb, &InterruptState);
|
2020-08-12 22:21:42 +02:00
|
|
|
Ghcb->SharedBuffer[0] = Value;
|
|
|
|
Ghcb->SaveArea.SwScratch = (UINT64)(UINTN)Ghcb->SharedBuffer;
|
2022-11-07 08:50:11 +01:00
|
|
|
CcExitVmgSetOffsetValid (Ghcb, GhcbSwScratch);
|
|
|
|
CcExitVmgExit (Ghcb, SVM_EXIT_MMIO_WRITE, PhysAddr, 1);
|
|
|
|
CcExitVmgDone (Ghcb, InterruptState);
|
2020-08-12 22:21:42 +02:00
|
|
|
} else {
|
|
|
|
*Ptr = Value;
|
|
|
|
}
|
|
|
|
}
|