Date   

Re: [PATCH 1/2] UefiPayloadPkg: Add new structure for BootManagerMenuFile HOB

Guo Dong
 

+ gUniversalPayloadBootManagerMenuFileGuid

Had better rename this guid to remove "UniversalPayload" from the name since this guid is not defined in universal payload specification.

Thanks,
Guo

-----Original Message-----
From: Tan, Dun <dun.tan@...>
Sent: Monday, June 21, 2021 1:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>; Tan,
Dun <dun.tan@...>
Subject: [PATCH 1/2] UefiPayloadPkg: Add new structure for
BootManagerMenuFile HOB

Add new structure for BootManagerMenuFile HOB in UefiPayloadPkg

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>

Signed-off-by: DunTan <dun.tan@...>
---
UefiPayloadPkg/Include/Guid/BootManagerMenu.h | 27
+++++++++++++++++++++++++++
UefiPayloadPkg/UefiPayloadPkg.dec | 3 +++
2 files changed, 30 insertions(+)

diff --git a/UefiPayloadPkg/Include/Guid/BootManagerMenu.h
b/UefiPayloadPkg/Include/Guid/BootManagerMenu.h
new file mode 100644
index 0000000000..10fb874640
--- /dev/null
+++ b/UefiPayloadPkg/Include/Guid/BootManagerMenu.h
@@ -0,0 +1,27 @@
+/** @file
+ Define the structure for the Boot Manager Menu File.
+
+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU_H_
+#define UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU_H_
+
+#include <Uefi.h>
+#include <UniversalPayload/UniversalPayload.h>
+
+#pragma pack (1)
+
+typedef struct {
+ UNIVERSAL_PAYLOAD_GENERIC_HEADER Header;
+ GUID FileName;
+} UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU;
+
+#pragma pack()
+
+#define UNIVERSAL_PAYLOAD_BOOT_MANAGER_MENU_REVISION 1
+
+extern GUID gUniversalPayloadBootManagerMenuFileGuid;
+#endif
diff --git a/UefiPayloadPkg/UefiPayloadPkg.dec
b/UefiPayloadPkg/UefiPayloadPkg.dec
index 105e1f5a1c..850592976d 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dec
+++ b/UefiPayloadPkg/UefiPayloadPkg.dec
@@ -29,6 +29,9 @@
#
gBmpImageGuid = { 0x878AC2CC, 0x5343, 0x46F2, { 0xB5, 0x63,
0x51, 0xF8, 0x9D, 0xAF, 0x56, 0xBA } }

+ ##include/Guid/BootManagerMenu.h
+ gUniversalPayloadBootManagerMenuFileGuid = { 0xdf939333, 0x42fc,
0x4b2a, { 0xa5, 0x9e, 0xbb, 0xae, 0x82, 0x81, 0xfe, 0xef }}
+
gUefiSystemTableInfoGuid = {0x16c8a6d0, 0xfe8a, 0x4082, {0xa2, 0x8, 0xcf,
0x89, 0xc4, 0x29, 0x4, 0x33}}
gUefiAcpiBoardInfoGuid = {0xad3d31b, 0xb3d8, 0x4506, {0xae, 0x71, 0x2e,
0xf1, 0x10, 0x6, 0xd9, 0xf}}
gUefiSerialPortInfoGuid = { 0x6c6872fe, 0x56a9, 0x4403, { 0xbb, 0x98, 0x95,
0x8d, 0x62, 0xde, 0x87, 0xf1 } }
--
2.31.1.windows.1


Re: [PATCH 11/12] UefiPayloadPkg: Add PcdInstallAcpiSdtProtocol feature in UefiPayloadPkg

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 11/12] UefiPayloadPkg: Add PcdInstallAcpiSdtProtocol
feature in UefiPayloadPkg

To install ACPI SDT protocol, define PcdInstallAcpiSdtProtocol as TRUE.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>

Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadPkg.dsc | 2 ++
1 file changed, 2 insertions(+)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 99b0e49d46..930831d30c 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -290,6 +290,8 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode|TRUE

gEfiMdeModulePkgTokenSpaceGuid.PcdConOutGopSupport|TRUE

gEfiMdeModulePkgTokenSpaceGuid.PcdConOutUgaSupport|FALSE

+ ## This PCD specified whether ACPI SDT protocol is installed.

+ gEfiMdeModulePkgTokenSpaceGuid.PcdInstallAcpiSdtProtocol|TRUE



[PcdsFixedAtBuild]

gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000

--
2.16.2.windows.1


Re: [PATCH 12/12] UefiPayloadPkg: Add PcdResetOnMemoryTypeInformationChange in UefiPayloadPkg

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 12/12] UefiPayloadPkg: Add
PcdResetOnMemoryTypeInformationChange in UefiPayloadPkg

This PCD will be consumed by Universal Payload

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadPkg.dsc | 1 +
1 file changed, 1 insertion(+)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 930831d30c..f1bf80bc6d 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -362,6 +362,7 @@

##########################################################
######################



[PcdsDynamicExDefault]

+
gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationC
hange|FALSE

gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0

gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0


gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0

--
2.16.2.windows.1


Re: [PATCH 09/12] UefiPayloadPkg: Remove assert when reserve MMIO/IO resource for devices

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 09/12] UefiPayloadPkg: Remove assert when reserve
MMIO/IO resource for devices

Some boot loader may already reserve MMIO/IO resource for IOAPIC and
HPET,
so remove the assert when reserve MMIO/IO resource for IOAPIC and HPET

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/BlSupportDxe/BlSupportDxe.c | 20 +++++++++++---------
1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/UefiPayloadPkg/BlSupportDxe/BlSupportDxe.c
b/UefiPayloadPkg/BlSupportDxe/BlSupportDxe.c
index ffd3427fb3..04e968a232 100644
--- a/UefiPayloadPkg/BlSupportDxe/BlSupportDxe.c
+++ b/UefiPayloadPkg/BlSupportDxe/BlSupportDxe.c
@@ -41,13 +41,12 @@ ReserveResourceInGcd (
);

if (EFI_ERROR (Status)) {

DEBUG ((

- DEBUG_ERROR,

+ DEBUG_WARN,

"Failed to add memory space :0x%lx 0x%lx\n",

BaseAddress,

Length

));

}

- ASSERT_EFI_ERROR (Status);

Status = gDS->AllocateMemorySpace (

EfiGcdAllocateAddress,

GcdType,

@@ -57,14 +56,20 @@ ReserveResourceInGcd (
ImageHandle,

NULL

);

- ASSERT_EFI_ERROR (Status);

} else {

Status = gDS->AddIoSpace (

GcdType,

BaseAddress,

Length

);

- ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ DEBUG ((

+ DEBUG_WARN,

+ "Failed to add IO space :0x%lx 0x%lx\n",

+ BaseAddress,

+ Length

+ ));

+ }

Status = gDS->AllocateIoSpace (

EfiGcdAllocateAddress,

GcdType,

@@ -74,7 +79,6 @@ ReserveResourceInGcd (
ImageHandle,

NULL

);

- ASSERT_EFI_ERROR (Status);

}

return Status;

}

@@ -106,11 +110,9 @@ BlDxeEntryPoint (
//

// Report MMIO/IO Resources

//

- Status = ReserveResourceInGcd (TRUE,
EfiGcdMemoryTypeMemoryMappedIo, 0xFEC00000, SIZE_4KB, 0,
ImageHandle); // IOAPIC

- ASSERT_EFI_ERROR (Status);

+ ReserveResourceInGcd (TRUE, EfiGcdMemoryTypeMemoryMappedIo,
0xFEC00000, SIZE_4KB, 0, ImageHandle); // IOAPIC



- Status = ReserveResourceInGcd (TRUE,
EfiGcdMemoryTypeMemoryMappedIo, 0xFED00000, SIZE_1KB, 0,
ImageHandle); // HPET

- ASSERT_EFI_ERROR (Status);

+ ReserveResourceInGcd (TRUE, EfiGcdMemoryTypeMemoryMappedIo,
0xFED00000, SIZE_1KB, 0, ImageHandle); // HPET



//

// Find the frame buffer information and update PCDs

--
2.16.2.windows.1


Re: [PATCH 08/12] UefiPayloadPkg: Include UniversalPayLoad modules in UefiPayloadPkg.dsc

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 08/12] UefiPayloadPkg: Include UniversalPayLoad modules
in UefiPayloadPkg.dsc

Add a new macro "UNIVERSAL_PAYLOAD" to build Universal Payload.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>

Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadPkg.dsc | 22 ++++++++++++++++++----
UefiPayloadPkg/UefiPayloadPkg.fdf | 3 ++-
2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
b/UefiPayloadPkg/UefiPayloadPkg.dsc
index e3d669a6d6..155aea4bc4 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -196,7 +196,11 @@
TimerLib|UefiPayloadPkg/Library/AcpiTimerLib/AcpiTimerLib.inf


ResetSystemLib|UefiPayloadPkg/Library/ResetSystemLib/ResetSystemLib.in
f


SerialPortLib|MdeModulePkg/Library/BaseSerialPortLib16550/BaseSerialPort
Lib16550.inf

+!if $(UNIVERSAL_PAYLOAD) == TRUE

+
PlatformHookLib|UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib
/PlatformHookLib.inf

+!else


PlatformHookLib|UefiPayloadPkg/Library/PlatformHookLib/PlatformHookLib
.inf

+!endif


PlatformBootManagerLib|UefiPayloadPkg/Library/PlatformBootManagerLib/
PlatformBootManagerLib.inf

IoApicLib|PcAtChipsetPkg/Library/BaseIoApicLib/BaseIoApicLib.inf



@@ -213,10 +217,12 @@

DebugAgentLib|MdeModulePkg/Library/DebugAgentLibNull/DebugAgentLi
bNull.inf

!endif


PlatformSupportLib|UefiPayloadPkg/Library/PlatformSupportLibNull/Platfor
mSupportLibNull.inf

-!if $(BOOTLOADER) == "COREBOOT"

- BlParseLib|UefiPayloadPkg/Library/CbParseLib/CbParseLib.inf

-!else

- BlParseLib|UefiPayloadPkg/Library/SblParseLib/SblParseLib.inf

+!if $(UNIVERSAL_PAYLOAD) == FALSE

+ !if $(BOOTLOADER) == "COREBOOT"

+ BlParseLib|UefiPayloadPkg/Library/CbParseLib/CbParseLib.inf

+ !else

+ BlParseLib|UefiPayloadPkg/Library/SblParseLib/SblParseLib.inf

+ !endif

!endif




DebugLib|MdePkg/Library/BaseDebugLibSerialPort/BaseDebugLibSerialPort.
inf

@@ -383,10 +389,18 @@


!if "IA32" in $(ARCH)

[Components.IA32]

+ !if $(UNIVERSAL_PAYLOAD) == TRUE

+ UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf

+ !else

UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf

+ !endif

!else

[Components.X64]

+ !if $(UNIVERSAL_PAYLOAD) == TRUE

+ UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf

+ !else

UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf

+ !endif

!endif



[Components.X64]

diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf
b/UefiPayloadPkg/UefiPayloadPkg.fdf
index ed7fbcaddb..6073f9c1b4 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.fdf
+++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
@@ -52,8 +52,9 @@ READ_STATUS = TRUE
READ_LOCK_CAP = TRUE

READ_LOCK_STATUS = TRUE



+!if $(UNIVERSAL_PAYLOAD) == FALSE

INF UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf

-

+!endif

FILE FV_IMAGE = 4E35FD93-9C72-4c15-8C4B-E77F1DB2D793 {

SECTION FV_IMAGE = DXEFV

}

--
2.16.2.windows.1


Re: [PATCH 06/12] UefiPayloadPkg: Get and enter DxeCore for Universal Payload

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 06/12] UefiPayloadPkg: Get and enter DxeCore for Universal
Payload

From gUniversalPayloadExtraDataGuid Guid Hob, get the Dxe FV information,
and get the Dxe Core from the FV.
Also, make sure if there are muliple FV hob, the FV hob pointing to this FV
will be the first in the hob list.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c | 47
+++++++++++++++++++++++++++++++++++++++++++++++
UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h | 17
+++++++++++++++++
UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c | 44
++++++++++++++++++++++++++++++++++++++++++--
UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf | 1 +
UefiPayloadPkg/UefiPayloadEntry/X64/DxeLoadFunc.c | 4 ++--
5 files changed, 109 insertions(+), 4 deletions(-)

diff --git a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
index de9dbb0b0e..f5d70c59f8 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
@@ -305,3 +305,50 @@ LoadDxeCore (


return EFI_SUCCESS;

}

+

+/**

+ Find DXE core from FV and build DXE core HOBs.

+

+ @param[in] DxeFv The FV where to find the DXE core.

+ @param[out] DxeCoreEntryPoint DXE core entry point

+

+ @retval EFI_SUCCESS If it completed successfully.

+ @retval EFI_NOT_FOUND If it failed to load DXE FV.

+**/

+EFI_STATUS

+UniversalLoadDxeCore (

+ IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv,

+ OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint

+ )

+{

+ EFI_STATUS Status;

+ EFI_FFS_FILE_HEADER *FileHeader;

+ VOID *PeCoffImage;

+ EFI_PHYSICAL_ADDRESS ImageAddress;

+ UINT64 ImageSize;

+

+ //

+ // Find DXE core file from DXE FV

+ //

+ Status = FvFindFile (DxeFv, EFI_FV_FILETYPE_DXE_CORE, &FileHeader);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ Status = FileFindSection (FileHeader, EFI_SECTION_PE32, (VOID
**)&PeCoffImage);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ //

+ // Get DXE core info

+ //

+ Status = LoadPeCoffImage (PeCoffImage, &ImageAddress, &ImageSize,
DxeCoreEntryPoint);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ BuildModuleHob (&FileHeader->Name, ImageAddress,
EFI_SIZE_TO_PAGES ((UINT32) ImageSize) * EFI_PAGE_SIZE,
*DxeCoreEntryPoint);

+

+ return EFI_SUCCESS;

+}

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
index e9c3ec3073..35098f5141 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
@@ -33,6 +33,8 @@
#include <Guid/GraphicsInfoHob.h>

#include <UniversalPayload/SmbiosTable.h>

#include <UniversalPayload/AcpiTable.h>

+#include <UniversalPayload/UniversalPayload.h>

+#include <UniversalPayload/ExtraData.h>



#define LEGACY_8259_MASK_REGISTER_MASTER 0x21

#define LEGACY_8259_MASK_REGISTER_SLAVE 0xA1

@@ -117,6 +119,21 @@ LoadDxeCore (
OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint

);



+/**

+ Find DXE core from FV and build DXE core HOBs.

+

+ @param[in] DxeFv The FV where to find the DXE core.

+ @param[out] DxeCoreEntryPoint DXE core entry point

+

+ @retval EFI_SUCCESS If it completed successfully.

+ @retval EFI_NOT_FOUND If it failed to load DXE FV.

+**/

+EFI_STATUS

+UniversalLoadDxeCore (

+ IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv,

+ OUT PHYSICAL_ADDRESS *DxeCoreEntryPoint

+ );

+

/**

Transfers control to DxeCore.



diff --git a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
index 66e87bcb9b..9d59454486 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
@@ -179,7 +179,8 @@ FindAnotherHighestBelow4GResourceDescriptor (
**/

EFI_STATUS

BuildHobs (

- IN UINTN BootloaderParameter

+ IN UINTN BootloaderParameter,

+ OUT EFI_FIRMWARE_VOLUME_HEADER **DxeFv

)

{

EFI_PEI_HOB_POINTERS Hob;

@@ -190,6 +191,10 @@ BuildHobs (
EFI_PHYSICAL_ADDRESS MemoryTop;

EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob;

EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;

+ UNIVERSAL_PAYLOAD_EXTRA_DATA *ExtraData;

+ UINT8 *GuidHob;

+ EFI_HOB_FIRMWARE_VOLUME *FvHob;

+ UNIVERSAL_PAYLOAD_GENERIC_HEADER *GenericHeader;



Hob.Raw = (UINT8 *) BootloaderParameter;

MinimalNeededSize = FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);

@@ -254,6 +259,10 @@ BuildHobs (
// From now on, mHobList will point to the new Hob range.

//



+ //

+ // Create an empty FvHob for the DXE FV that contains DXE core.

+ //

+ BuildFvHob ((EFI_PHYSICAL_ADDRESS) 0, 0);

//

// Since payload created new Hob, move all hobs except PHIT from boot
loader hob list.

//

@@ -265,6 +274,29 @@ BuildHobs (
Hob.Raw = GET_NEXT_HOB (Hob);

}



+ //

+ // Get DXE FV location

+ //

+ GuidHob = GetFirstGuidHob(&gUniversalPayloadExtraDataGuid);

+ ASSERT (GuidHob != NULL);

+ GenericHeader = (UNIVERSAL_PAYLOAD_GENERIC_HEADER *)
GET_GUID_HOB_DATA (GuidHob);

+ ASSERT (sizeof (UNIVERSAL_PAYLOAD_GENERIC_HEADER) <=
GET_GUID_HOB_DATA_SIZE (GuidHob));

+ ASSERT (GenericHeader->Length <= GET_GUID_HOB_DATA_SIZE
(GuidHob));

+ ASSERT (GenericHeader->Length >= (sizeof
(UNIVERSAL_PAYLOAD_EXTRA_DATA) + sizeof
(UNIVERSAL_PAYLOAD_EXTRA_DATA_ENTRY)));

+ ExtraData = (UNIVERSAL_PAYLOAD_EXTRA_DATA *)
GET_GUID_HOB_DATA (GuidHob);

+ ASSERT (ExtraData->Count == 1);

+ ASSERT (AsciiStrCmp (ExtraData->Entry[0].Identifier, "uefi_fv") == 0);

+

+ *DxeFv = (EFI_FIRMWARE_VOLUME_HEADER *) (UINTN) ExtraData-
Entry[0].Base;
+ ASSERT ((*DxeFv)->FvLength == ExtraData->Entry[0].Size);

+

+ //

+ // Update DXE FV information to first fv hob in the hob list, which

+ // is the empty FvHob created before.

+ //

+ FvHob = GetFirstHob (EFI_HOB_TYPE_FV);

+ FvHob->BaseAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) *DxeFv;

+ FvHob->Length = (*DxeFv)->FvLength;

return EFI_SUCCESS;

}



@@ -280,10 +312,13 @@ _ModuleEntryPoint (
)

{

EFI_STATUS Status;

+ PHYSICAL_ADDRESS DxeCoreEntryPoint;

EFI_HOB_HANDOFF_INFO_TABLE *HandoffHobTable;

EFI_PEI_HOB_POINTERS Hob;

+ EFI_FIRMWARE_VOLUME_HEADER *DxeFv;



mHobList = (VOID *) BootloaderParameter;

+ DxeFv = NULL;

// Call constructor for all libraries

ProcessLibraryConstructorList ();



@@ -294,7 +329,11 @@ _ModuleEntryPoint (
InitializeFloatingPointUnits ();



// Build HOB based on information from Bootloader

- Status = BuildHobs (BootloaderParameter);

+ Status = BuildHobs (BootloaderParameter, &DxeFv);

+ ASSERT_EFI_ERROR (Status);

+

+ Status = UniversalLoadDxeCore (DxeFv, &DxeCoreEntryPoint);

+ ASSERT_EFI_ERROR (Status);



//

// Mask off all legacy 8259 interrupt sources

@@ -304,6 +343,7 @@ _ModuleEntryPoint (


HandoffHobTable = (EFI_HOB_HANDOFF_INFO_TABLE *)
GetFirstHob(EFI_HOB_TYPE_HANDOFF);

Hob.HandoffInformationTable = HandoffHobTable;

+ HandOffToDxeCore (DxeCoreEntryPoint, Hob);



// Should not get here

CpuDeadLoop ();

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
index 58ff87d969..77cd25aafd 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
+++ b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
@@ -62,6 +62,7 @@
gEfiSmbiosTableGuid

gEfiAcpiTableGuid

gUefiSerialPortInfoGuid

+ gUniversalPayloadExtraDataGuid



[FeaturePcd.IA32]

gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode ##
CONSUMES

diff --git a/UefiPayloadPkg/UefiPayloadEntry/X64/DxeLoadFunc.c
b/UefiPayloadPkg/UefiPayloadEntry/X64/DxeLoadFunc.c
index 73ea30e7a2..dec87ee1ef 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/X64/DxeLoadFunc.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/X64/DxeLoadFunc.c
@@ -66,8 +66,8 @@ HandOffToDxeCore (
//

// Get the address and size of the GHCB pages

//

- GhcbBase = (VOID *) PcdGet64 (PcdGhcbBase);

- GhcbSize = PcdGet64 (PcdGhcbSize);

+ GhcbBase = 0;

+ GhcbSize = 0;



PageTables = 0;

if (FeaturePcdGet (PcdDxeIplBuildPageTables)) {

--
2.16.2.windows.1


Re: [PATCH 05/12] UefiPayloadPkg: Create separate Payload Entry for UniversalPayload

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 05/12] UefiPayloadPkg: Create separate Payload Entry for
UniversalPayload

This patch create the UniversalPayload Entry based on the UefiPayload
Entry. It implements the logic to find a proper memory range to create the
new Hob and migrate the Hobs from Bootloader.
To make the change history clear, the logic to get the DxeCore will be in
the next patch.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c | 311
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++
UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf | 91
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++
2 files changed, 402 insertions(+)

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
new file mode 100644
index 0000000000..66e87bcb9b
--- /dev/null
+++ b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
@@ -0,0 +1,311 @@
+/** @file

+

+ Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include "UefiPayloadEntry.h"

+

+#define MEMORY_ATTRIBUTE_MASK
(EFI_RESOURCE_ATTRIBUTE_PRESENT | \

+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \

+ EFI_RESOURCE_ATTRIBUTE_TESTED | \

+ EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED | \

+ EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED | \

+ EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED | \

+ EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED |
\

+ EFI_RESOURCE_ATTRIBUTE_16_BIT_IO | \

+ EFI_RESOURCE_ATTRIBUTE_32_BIT_IO | \

+ EFI_RESOURCE_ATTRIBUTE_64_BIT_IO | \

+ EFI_RESOURCE_ATTRIBUTE_PERSISTENT )

+

+#define TESTED_MEMORY_ATTRIBUTES
(EFI_RESOURCE_ATTRIBUTE_PRESENT | \

+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED | \

+ EFI_RESOURCE_ATTRIBUTE_TESTED )

+

+extern VOID *mHobList;

+

+/**

+ Add HOB into HOB list

+

+ @param[in] Hob The HOB to be added into the HOB list.

+**/

+VOID

+AddNewHob (

+ IN EFI_PEI_HOB_POINTERS *Hob

+ )

+{

+ EFI_PEI_HOB_POINTERS NewHob;

+

+ if (Hob->Raw == NULL) {

+ return;

+ }

+ NewHob.Header = CreateHob (Hob->Header->HobType, Hob->Header-
HobLength);
+

+ if (NewHob.Header != NULL) {

+ CopyMem (NewHob.Header + 1, Hob->Header + 1, Hob->Header-
HobLength - sizeof (EFI_HOB_GENERIC_HEADER));
+ }

+}

+

+/**

+ Found the Resource Descriptor HOB that contains a range

+

+ @param[in] Base Memory start address

+ @param[in] Top Memory Top.

+

+ @return The pointer to the Resource Descriptor HOB.

+**/

+EFI_HOB_RESOURCE_DESCRIPTOR *

+FindResourceDescriptorByRange (

+ VOID *HobList,

+ EFI_PHYSICAL_ADDRESS Base,

+ EFI_PHYSICAL_ADDRESS Top

+ )

+{

+ EFI_PEI_HOB_POINTERS Hob;

+ EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;

+

+ for (Hob.Raw = (UINT8 *) HobList; !END_OF_HOB_LIST(Hob); Hob.Raw =
GET_NEXT_HOB(Hob)) {

+ //

+ // Skip all HOBs except Resource Descriptor HOBs

+ //

+ if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {

+ continue;

+ }

+

+ //

+ // Skip Resource Descriptor HOBs that do not describe tested system
memory

+ //

+ ResourceHob = Hob.ResourceDescriptor;

+ if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {

+ continue;

+ }

+ if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) !=
TESTED_MEMORY_ATTRIBUTES) {

+ continue;

+ }

+

+ //

+ // Skip Resource Descriptor HOBs that do not contain the PHIT range
EfiFreeMemoryBottom..EfiFreeMemoryTop

+ //

+ if (Base < ResourceHob->PhysicalStart) {

+ continue;

+ }

+ if (Top > (ResourceHob->PhysicalStart + ResourceHob->ResourceLength))
{

+ continue;

+ }

+ return ResourceHob;

+ }

+ return NULL;

+}

+

+/**

+ Find the highest below 4G memory resource descriptor, except the input
Resource Descriptor.

+

+ @param[in] HobList Hob start address

+ @param[in] MinimalNeededSize Minimal needed size.

+ @param[in] ExceptResourceHob Ignore this Resource Descriptor.

+

+ @return The pointer to the Resource Descriptor HOB.

+**/

+EFI_HOB_RESOURCE_DESCRIPTOR *

+FindAnotherHighestBelow4GResourceDescriptor (

+ IN VOID *HobList,

+ IN UINTN MinimalNeededSize,

+ IN EFI_HOB_RESOURCE_DESCRIPTOR *ExceptResourceHob

+ )

+{

+ EFI_PEI_HOB_POINTERS Hob;

+ EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;

+ EFI_HOB_RESOURCE_DESCRIPTOR *ReturnResourceHob;

+ ReturnResourceHob = NULL;

+

+ for (Hob.Raw = (UINT8 *) HobList; !END_OF_HOB_LIST(Hob); Hob.Raw =
GET_NEXT_HOB(Hob)) {

+ //

+ // Skip all HOBs except Resource Descriptor HOBs

+ //

+ if (GET_HOB_TYPE (Hob) != EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {

+ continue;

+ }

+

+ //

+ // Skip Resource Descriptor HOBs that do not describe tested system
memory

+ //

+ ResourceHob = Hob.ResourceDescriptor;

+ if (ResourceHob->ResourceType != EFI_RESOURCE_SYSTEM_MEMORY) {

+ continue;

+ }

+ if ((ResourceHob->ResourceAttribute & MEMORY_ATTRIBUTE_MASK) !=
TESTED_MEMORY_ATTRIBUTES) {

+ continue;

+ }

+

+ //

+ // Skip if the Resource Descriptor HOB equals to ExceptResourceHob

+ //

+ if (ResourceHob == ExceptResourceHob) {

+ continue;

+ }

+ //

+ // Skip Resource Descriptor HOBs that are beyond 4G

+ //

+ if ((ResourceHob->PhysicalStart + ResourceHob->ResourceLength) >
BASE_4GB) {

+ continue;

+ }

+ //

+ // Skip Resource Descriptor HOBs that are too small

+ //

+ if (ResourceHob->ResourceLength < MinimalNeededSize) {

+ continue;

+ }

+

+ //

+ // Return the topest Resource Descriptor

+ //

+ if (ReturnResourceHob == NULL) {

+ ReturnResourceHob = ResourceHob;

+ } else {

+ if (ReturnResourceHob->PhysicalStart < ResourceHob->PhysicalStart) {

+ ReturnResourceHob = ResourceHob;

+ }

+ }

+ }

+ return ReturnResourceHob;

+}

+

+/**

+ It will build HOBs based on information from bootloaders.

+

+ @retval EFI_SUCCESS If it completed successfully.

+ @retval Others If it failed to build required HOBs.

+**/

+EFI_STATUS

+BuildHobs (

+ IN UINTN BootloaderParameter

+ )

+{

+ EFI_PEI_HOB_POINTERS Hob;

+ UINTN MinimalNeededSize;

+ EFI_PHYSICAL_ADDRESS FreeMemoryBottom;

+ EFI_PHYSICAL_ADDRESS FreeMemoryTop;

+ EFI_PHYSICAL_ADDRESS MemoryBottom;

+ EFI_PHYSICAL_ADDRESS MemoryTop;

+ EFI_HOB_RESOURCE_DESCRIPTOR *PhitResourceHob;

+ EFI_HOB_RESOURCE_DESCRIPTOR *ResourceHob;

+

+ Hob.Raw = (UINT8 *) BootloaderParameter;

+ MinimalNeededSize = FixedPcdGet32 (PcdSystemMemoryUefiRegionSize);

+

+ ASSERT (Hob.Raw != NULL);

+ ASSERT ((UINTN) Hob.HandoffInformationTable->EfiFreeMemoryTop ==
Hob.HandoffInformationTable->EfiFreeMemoryTop);

+ ASSERT ((UINTN) Hob.HandoffInformationTable->EfiMemoryTop ==
Hob.HandoffInformationTable->EfiMemoryTop);

+ ASSERT ((UINTN) Hob.HandoffInformationTable->EfiFreeMemoryBottom
== Hob.HandoffInformationTable->EfiFreeMemoryBottom);

+ ASSERT ((UINTN) Hob.HandoffInformationTable->EfiMemoryBottom ==
Hob.HandoffInformationTable->EfiMemoryBottom);

+

+

+ //

+ // Try to find Resource Descriptor HOB that contains Hob range
EfiMemoryBottom..EfiMemoryTop

+ //

+ PhitResourceHob = FindResourceDescriptorByRange(Hob.Raw,
Hob.HandoffInformationTable->EfiMemoryBottom,
Hob.HandoffInformationTable->EfiMemoryTop);

+ if (PhitResourceHob == NULL) {

+ //

+ // Boot loader's Phit Hob is not in an available Resource Descriptor, find
another Resource Descriptor for new Phit Hob

+ //

+ ResourceHob =
FindAnotherHighestBelow4GResourceDescriptor(Hob.Raw,
MinimalNeededSize, NULL);

+ if (ResourceHob == NULL) {

+ return EFI_NOT_FOUND;

+ }

+

+ MemoryBottom = ResourceHob->PhysicalStart + ResourceHob-
ResourceLength - MinimalNeededSize;
+ FreeMemoryBottom = MemoryBottom;

+ FreeMemoryTop = ResourceHob->PhysicalStart + ResourceHob-
ResourceLength;
+ MemoryTop = FreeMemoryTop;

+ } else if (PhitResourceHob->PhysicalStart + PhitResourceHob-
ResourceLength - Hob.HandoffInformationTable->EfiMemoryTop >=
MinimalNeededSize) {

+ //

+ // New availiable Memory range in new hob is right above memory top in
old hob.

+ //

+ MemoryBottom = Hob.HandoffInformationTable->EfiFreeMemoryTop;

+ FreeMemoryBottom = Hob.HandoffInformationTable->EfiMemoryTop;

+ FreeMemoryTop = FreeMemoryBottom + MinimalNeededSize;

+ MemoryTop = FreeMemoryTop;

+ } else if (Hob.HandoffInformationTable->EfiMemoryBottom -
PhitResourceHob->PhysicalStart >= MinimalNeededSize) {

+ //

+ // New availiable Memory range in new hob is right below memory
bottom in old hob.

+ //

+ MemoryBottom = Hob.HandoffInformationTable->EfiMemoryBottom -
MinimalNeededSize;

+ FreeMemoryBottom = MemoryBottom;

+ FreeMemoryTop = Hob.HandoffInformationTable->EfiMemoryBottom;

+ MemoryTop = Hob.HandoffInformationTable->EfiMemoryTop;

+ } else {

+ //

+ // In the Resource Descriptor HOB contains boot loader Hob, there is no
enough free memory size for payload hob

+ // Find another Resource Descriptor Hob

+ //

+ ResourceHob =
FindAnotherHighestBelow4GResourceDescriptor(Hob.Raw,
MinimalNeededSize, PhitResourceHob);

+ if (ResourceHob == NULL) {

+ return EFI_NOT_FOUND;

+ }

+

+ MemoryBottom = ResourceHob->PhysicalStart + ResourceHob-
ResourceLength - MinimalNeededSize;
+ FreeMemoryBottom = MemoryBottom;

+ FreeMemoryTop = ResourceHob->PhysicalStart + ResourceHob-
ResourceLength;
+ MemoryTop = FreeMemoryTop;

+ }

+ HobConstructor ((VOID *) (UINTN) MemoryBottom, (VOID *) (UINTN)
MemoryTop, (VOID *) (UINTN) FreeMemoryBottom, (VOID *) (UINTN)
FreeMemoryTop);

+ //

+ // From now on, mHobList will point to the new Hob range.

+ //

+

+ //

+ // Since payload created new Hob, move all hobs except PHIT from boot
loader hob list.

+ //

+ while (!END_OF_HOB_LIST (Hob)) {

+ if (Hob.Header->HobType != EFI_HOB_TYPE_HANDOFF) {

+ // Add this hob to payload HOB

+ AddNewHob (&Hob);

+ }

+ Hob.Raw = GET_NEXT_HOB (Hob);

+ }

+

+ return EFI_SUCCESS;

+}

+

+/**

+ Entry point to the C language phase of UEFI payload.

+

+ @retval It will not return if SUCCESS, and return error when passing
bootloader parameter.

+**/

+EFI_STATUS

+EFIAPI

+_ModuleEntryPoint (

+ IN UINTN BootloaderParameter

+ )

+{

+ EFI_STATUS Status;

+ EFI_HOB_HANDOFF_INFO_TABLE *HandoffHobTable;

+ EFI_PEI_HOB_POINTERS Hob;

+

+ mHobList = (VOID *) BootloaderParameter;

+ // Call constructor for all libraries

+ ProcessLibraryConstructorList ();

+

+ DEBUG ((DEBUG_INFO, "Entering Universal Payload...\n"));

+ DEBUG ((DEBUG_INFO, "sizeof(UINTN) = 0x%x\n", sizeof(UINTN)));

+

+ // Initialize floating point operating environment to be compliant with UEFI
spec.

+ InitializeFloatingPointUnits ();

+

+ // Build HOB based on information from Bootloader

+ Status = BuildHobs (BootloaderParameter);

+

+ //

+ // Mask off all legacy 8259 interrupt sources

+ //

+ IoWrite8 (LEGACY_8259_MASK_REGISTER_MASTER, 0xFF);

+ IoWrite8 (LEGACY_8259_MASK_REGISTER_SLAVE, 0xFF);

+

+ HandoffHobTable = (EFI_HOB_HANDOFF_INFO_TABLE *)
GetFirstHob(EFI_HOB_TYPE_HANDOFF);

+ Hob.HandoffInformationTable = HandoffHobTable;

+

+ // Should not get here

+ CpuDeadLoop ();

+ return EFI_SUCCESS;

+}

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
new file mode 100644
index 0000000000..58ff87d969
--- /dev/null
+++ b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
@@ -0,0 +1,91 @@
+## @file

+# This is the first module for UEFI payload.

+#

+# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010005

+ BASE_NAME = UniversalPayloadEntry

+ FILE_GUID = D4F0F269-1209-4A66-8039-C4D5A700EA4E

+ MODULE_TYPE = SEC

+ VERSION_STRING = 1.0

+

+#

+# The following information is for reference only and not required by the
build tools.

+#

+# VALID_ARCHITECTURES = IA32 X64

+#

+

+[Sources]

+ UniversalPayloadEntry.c

+ LoadDxeCore.c

+ MemoryAllocation.c

+

+[Sources.Ia32]

+ X64/VirtualMemory.h

+ X64/VirtualMemory.c

+ Ia32/DxeLoadFunc.c

+ Ia32/IdtVectorAsm.nasm

+

+[Sources.X64]

+ X64/VirtualMemory.h

+ X64/VirtualMemory.c

+ X64/DxeLoadFunc.c

+

+[Packages]

+ MdePkg/MdePkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ UefiCpuPkg/UefiCpuPkg.dec

+ UefiPayloadPkg/UefiPayloadPkg.dec

+

+[LibraryClasses]

+ BaseMemoryLib

+ DebugLib

+ BaseLib

+ SerialPortLib

+ IoLib

+ HobLib

+ PeCoffLib

+ UefiCpuLib

+

+[Guids]

+ gEfiMemoryTypeInformationGuid

+ gEfiFirmwareFileSystem2Guid

+ gUefiSystemTableInfoGuid

+ gEfiGraphicsInfoHobGuid

+ gEfiGraphicsDeviceInfoHobGuid

+ gUefiAcpiBoardInfoGuid

+ gEfiSmbiosTableGuid

+ gEfiAcpiTableGuid

+ gUefiSerialPortInfoGuid

+

+[FeaturePcd.IA32]

+ gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode ##
CONSUMES

+

+[FeaturePcd.X64]

+ gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplBuildPageTables ##
CONSUMES

+

+

+[Pcd.IA32,Pcd.X64]

+ gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable ##
SOMETIMES_CONSUMES

+
gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrM
ask ## CONSUMES

+
gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask
## CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdHeapGuardPropertyMask
## CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ##
CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdUse5LevelPageTable ##
SOMETIMES_CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase ##
CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbSize ##
CONSUMES

+

+ gUefiPayloadPkgTokenSpaceGuid.PcdPayloadFdMemBase

+ gUefiPayloadPkgTokenSpaceGuid.PcdPayloadFdMemSize

+ gUefiPayloadPkgTokenSpaceGuid.PcdPayloadStackTop

+ gUefiPayloadPkgTokenSpaceGuid.PcdSystemMemoryUefiRegionSize

+

+ gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack ##
SOMETIMES_CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdDxeNxMemoryProtectionPolicy
## SOMETIMES_CONSUMES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdImageProtectionPolicy ##
SOMETIMES_CONSUMES

+

--
2.16.2.windows.1


Re: [PATCH v4 2/4] OvmfPkg/BaseMemEncryptLib: Support to issue unencrypted hypercall

Lendacky, Thomas
 

On 6/21/21 8:57 AM, Ashish Kalra wrote:
From: Brijesh Singh <brijesh.singh@...>

By default all the SEV guest memory regions are considered encrypted,
if a guest changes the encryption attribute of the page (e.g mark a
page as decrypted) then notify hypervisor. Hypervisor will need to
track the unencrypted pages. The information will be used during
guest live migration, guest page migration and guest debugging.

Invoke hypercall via the new hypercall library.

This hypercall is used to notify hypervisor when a page is marked as
'decrypted' (i.e C-bit removed).

Cc: Jordan Justen <jordan.l.justen@...>
Cc: Laszlo Ersek <lersek@...>
Cc: Ard Biesheuvel <ard.biesheuvel@...>

Signed-off-by: Brijesh Singh <brijesh.singh@...>
Signed-off-by: Ashish Kalra <ashish.kalra@...>
---
OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf | 1 +
OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf | 1 +
OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c | 22 ++++++++++++++++++++
3 files changed, 24 insertions(+)

diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
index f2e162d680..aefcd7c0f7 100644
--- a/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+++ b/OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
@@ -49,6 +49,7 @@
DebugLib
MemoryAllocationLib
PcdLib
+ MemEncryptHypercallLib

[FeaturePcd]
gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire
diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf
index 03a78c32df..7503f56a0b 100644
--- a/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf
+++ b/OvmfPkg/Library/BaseMemEncryptSevLib/PeiMemEncryptSevLib.inf
@@ -49,6 +49,7 @@
DebugLib
MemoryAllocationLib
PcdLib
+ MemEncryptHypercallLib

[FeaturePcd]
gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire
diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c
index c696745f9d..12b3a9fcfb 100644
--- a/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c
+++ b/OvmfPkg/Library/BaseMemEncryptSevLib/X64/PeiDxeVirtualMemory.c
@@ -15,6 +15,7 @@
#include <Library/MemEncryptSevLib.h>
#include <Register/Amd/Cpuid.h>
#include <Register/Cpuid.h>
+#include <Library/MemEncryptHypercallLib.h>

#include "VirtualMemory.h"

@@ -585,6 +586,9 @@ SetMemoryEncDec (
UINT64 AddressEncMask;
BOOLEAN IsWpEnabled;
RETURN_STATUS Status;
+ UINTN Size;
+ BOOLEAN CBitChanged;
+ PHYSICAL_ADDRESS OrigPhysicalAddress;

//
// Set PageMapLevel4Entry to suppress incorrect compiler/analyzer warnings.
@@ -636,6 +640,10 @@ SetMemoryEncDec (

Status = EFI_SUCCESS;

+ Size = Length;
+ CBitChanged = FALSE;
+ OrigPhysicalAddress = PhysicalAddress;
+
while (Length != 0)
{
//
@@ -695,6 +703,7 @@ SetMemoryEncDec (
));
PhysicalAddress += BIT30;
Length -= BIT30;
+ CBitChanged = TRUE;
} else {
//
// We must split the page
@@ -749,6 +758,7 @@ SetMemoryEncDec (
SetOrClearCBit (&PageDirectory2MEntry->Uint64, Mode);
PhysicalAddress += BIT21;
Length -= BIT21;
+ CBitChanged = TRUE;
} else {
//
// We must split up this page into 4K pages
@@ -791,6 +801,7 @@ SetMemoryEncDec (
SetOrClearCBit (&PageTableEntry->Uint64, Mode);
PhysicalAddress += EFI_PAGE_SIZE;
Length -= EFI_PAGE_SIZE;
+ CBitChanged = TRUE;
}
}
}
@@ -808,6 +819,17 @@ SetMemoryEncDec (
//
CpuFlushTlb();

+ //
+ // Notify Hypervisor on C-bit status
+ //
+ if (CBitChanged) {
+ SetMemoryEncDecHypercall3 (
+ OrigPhysicalAddress,
+ EFI_SIZE_TO_PAGES(Size),
+ KVM_MAP_GPA_RANGE_ENC_STAT(!Mode)
+ );
+ }
Maybe add a comment here that the "Mode" doesn't care whether the change
was for 4k vs 2mb vs 1gb. It is just tracking the total number of pages
changed (should you just remove those #defines for the page sizes then?)
which can be done by calculating everything at the 4k level.

Thanks,
Tom

+
Done:
//
// Restore page table write protection, if any.


Re: [PATCH 10/12] UefiPayloadPkg: Add macro to disable some drivers

Guo Dong
 

+ DEFINE DISABLE_VARIABLE = FALSE
Could you change this MACRO definition as below to specifically disable EMU variable?
DEFINE EMU_VARIABLE_ENABLE = FALSE

This way, late we could enable other variable drivers (e.g. SMM variable) in UEFI payload.

Thanks,
Guo

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 10/12] UefiPayloadPkg: Add macro to disable some drivers

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadPkg.dsc | 11 ++++++++++-
UefiPayloadPkg/UefiPayloadPkg.fdf | 7 +++++++
2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 155aea4bc4..99b0e49d46 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -87,6 +87,12 @@
#

DEFINE SHELL_TYPE = BUILD_SHELL



+ #

+ # Disable features

+ #

+ DEFINE DISABLE_VARIABLE = FALSE

+ DEFINE DISABLE_RESET_SYSTEM = FALSE

+

[BuildOptions]

*_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES

GCC:*_UNIXGCC_*_CC_FLAGS = -DMDEPKG_NDEBUG

@@ -431,10 +437,13 @@
MdeModulePkg/Core/RuntimeDxe/RuntimeDxe.inf

MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf


MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCoun
terRuntimeDxe.inf

+!if $(DISABLE_RESET_SYSTEM) == FALSE


MdeModulePkg/Universal/ResetSystemRuntimeDxe/ResetSystemRuntime
Dxe.inf

+!endif


PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntime
Dxe.inf

+!if $(DISABLE_VARIABLE) == FALSE

MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf

-

+!endif

#

# Following are the DXE drivers

#

diff --git a/UefiPayloadPkg/UefiPayloadPkg.fdf
b/UefiPayloadPkg/UefiPayloadPkg.fdf
index 6073f9c1b4..3b6adc2cfe 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.fdf
+++ b/UefiPayloadPkg/UefiPayloadPkg.fdf
@@ -105,9 +105,16 @@ INF
MdeModulePkg/Universal/WatchdogTimerDxe/WatchdogTimer.inf
INF MdeModulePkg/Core/RuntimeDxe/RuntimeDxe.inf

INF MdeModulePkg/Universal/CapsuleRuntimeDxe/CapsuleRuntimeDxe.inf

INF
MdeModulePkg/Universal/MonotonicCounterRuntimeDxe/MonotonicCoun
terRuntimeDxe.inf

+

+!if $(DISABLE_RESET_SYSTEM) == FALSE

INF
MdeModulePkg/Universal/ResetSystemRuntimeDxe/ResetSystemRuntime
Dxe.inf

+!endif

+

INF
PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntime
Dxe.inf

+

+!if $(DISABLE_VARIABLE) == FALSE

INF
MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf

+!endif



INF UefiCpuPkg/CpuIo2Dxe/CpuIo2Dxe.inf

INF MdeModulePkg/Universal/DevicePathDxe/DevicePathDxe.inf

--
2.16.2.windows.1


Re: [PATCH 07/12] UefiPayloadPkg: Fix up UPL Pcd database

Guo Dong
 

Please update the below function description to match the function impl.

+/**

+ Find DXE core from FV and build DXE core HOBs.

+

+ @param[in] FvBase FV base to load DXE core from

+ @param[out] DxeCoreEntryPoint DXE core entry point

+

+ @retval EFI_SUCCESS If it completed successfully.

+ @retval EFI_NOT_FOUND If it failed to load DXE FV.

+**/

+EFI_STATUS

+FixUpPcdDatabase (

+ IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv

+ )


Remove the original one "*FileHeader = File;" since a new one is added in Guid check condition.

@@ -172,7 +175,10 @@ FvFindFile (
//

if (File->Type == FileType) {

*FileHeader = File;

- return EFI_SUCCESS;

+ if (Guid == NULL || CompareGuid(&File->Name, Guid)) {

+ *FileHeader = File;

+ return EFI_SUCCESS;

+ }
Thanks,
Guo

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 07/12] UefiPayloadPkg: Fix up UPL Pcd database

Edk2 bootloader will pass the pei pcd database, and UPL also contain a
PCD database.
Dxe PCD driver has the assumption that the two PCD database can be
catenated and the local token number should be successive。
This patch will manually fix up the UPL PCD database to meet that
assumption.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c | 18
++++++++++++------
UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h | 45
+++++++++++++++++++++++++++++++++++++++++++++
UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c | 55
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf | 2 ++
UefiPayloadPkg/UefiPayloadPkg.dec | 2 ++
UefiPayloadPkg/UefiPayloadPkg.dsc | 1 +
6 files changed, 117 insertions(+), 6 deletions(-)

diff --git a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
index f5d70c59f8..f943a2632c 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c
@@ -114,20 +114,23 @@ LoadPeCoffImage (
}



/**

- This function searchs a given file type within a valid FV.

+ This function searchs a given file type with a given Guid within a valid FV.

+ If input Guid is NULL, will locate the first section having the given file type



@param FvHeader A pointer to firmware volume header that contains
the set of files

to be searched.

@param FileType File type to be searched.

+ @param Guid Will ignore if it is NULL.

@param FileHeader A pointer to the discovered file, if successful.



@retval EFI_SUCCESS Successfully found FileType

@retval EFI_NOT_FOUND File type can't be found.

**/

EFI_STATUS

-FvFindFile (

+FvFindFileByTypeGuid (

IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,

IN EFI_FV_FILETYPE FileType,

+ IN EFI_GUID *Guid OPTIONAL,

OUT EFI_FFS_FILE_HEADER **FileHeader

)

{

@@ -172,7 +175,10 @@ FvFindFile (
//

if (File->Type == FileType) {

*FileHeader = File;

- return EFI_SUCCESS;

+ if (Guid == NULL || CompareGuid(&File->Name, Guid)) {

+ *FileHeader = File;

+ return EFI_SUCCESS;

+ }

}

}



@@ -266,7 +272,7 @@ LoadDxeCore (
//

// DXE FV is inside Payload FV. Here find DXE FV from Payload FV

//

- Status = FvFindFile (PayloadFv,
EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, &FileHeader);

+ Status = FvFindFileByTypeGuid (PayloadFv,
EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, NULL, &FileHeader);

if (EFI_ERROR (Status)) {

return Status;

}

@@ -283,7 +289,7 @@ LoadDxeCore (
//

// Find DXE core file from DXE FV

//

- Status = FvFindFile (DxeCoreFv, EFI_FV_FILETYPE_DXE_CORE, &FileHeader);

+ Status = FvFindFileByTypeGuid (DxeCoreFv, EFI_FV_FILETYPE_DXE_CORE,
NULL, &FileHeader);

if (EFI_ERROR (Status)) {

return Status;

}

@@ -330,7 +336,7 @@ UniversalLoadDxeCore (
//

// Find DXE core file from DXE FV

//

- Status = FvFindFile (DxeFv, EFI_FV_FILETYPE_DXE_CORE, &FileHeader);

+ Status = FvFindFileByTypeGuid (DxeFv, EFI_FV_FILETYPE_DXE_CORE, NULL,
&FileHeader);

if (EFI_ERROR (Status)) {

return Status;

}

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
index 35098f5141..78a67fa1a5 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
@@ -35,6 +35,7 @@
#include <UniversalPayload/AcpiTable.h>

#include <UniversalPayload/UniversalPayload.h>

#include <UniversalPayload/ExtraData.h>

+#include <Guid/PcdDataBaseSignatureGuid.h>



#define LEGACY_8259_MASK_REGISTER_MASTER 0x21

#define LEGACY_8259_MASK_REGISTER_SLAVE 0xA1

@@ -149,4 +150,48 @@ HandOffToDxeCore (
IN EFI_PEI_HOB_POINTERS HobList

);



+EFI_STATUS

+FixUpPcdDatabase (

+ IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv

+ );

+

+/**

+ This function searchs a given section type within a valid FFS file.

+

+ @param FileHeader A pointer to the file header that contains the set
of sections to

+ be searched.

+ @param SearchType The value of the section type to search.

+ @param SectionData A pointer to the discovered section, if
successful.

+

+ @retval EFI_SUCCESS The section was found.

+ @retval EFI_NOT_FOUND The section was not found.

+

+**/

+EFI_STATUS

+FileFindSection (

+ IN EFI_FFS_FILE_HEADER *FileHeader,

+ IN EFI_SECTION_TYPE SectionType,

+ OUT VOID **SectionData

+ );

+

+/**

+ This function searchs a given file type with a given Guid within a valid FV.

+ If input Guid is NULL, will locate the first section having the given file type

+

+ @param FvHeader A pointer to firmware volume header that contains
the set of files

+ to be searched.

+ @param FileType File type to be searched.

+ @param Guid Will ignore if it is NULL.

+ @param FileHeader A pointer to the discovered file, if successful.

+

+ @retval EFI_SUCCESS Successfully found FileType

+ @retval EFI_NOT_FOUND File type can't be found.

+**/

+EFI_STATUS

+FvFindFileByTypeGuid (

+ IN EFI_FIRMWARE_VOLUME_HEADER *FvHeader,

+ IN EFI_FV_FILETYPE FileType,

+ IN EFI_GUID *Guid OPTIONAL,

+ OUT EFI_FFS_FILE_HEADER **FileHeader

+ );

#endif

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
index 9d59454486..dcf750befc 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.c
@@ -25,6 +25,60 @@


extern VOID *mHobList;



+/**

+ Find DXE core from FV and build DXE core HOBs.

+

+ @param[in] FvBase FV base to load DXE core from

+ @param[out] DxeCoreEntryPoint DXE core entry point

+

+ @retval EFI_SUCCESS If it completed successfully.

+ @retval EFI_NOT_FOUND If it failed to load DXE FV.

+**/

+EFI_STATUS

+FixUpPcdDatabase (

+ IN EFI_FIRMWARE_VOLUME_HEADER *DxeFv

+ )

+{

+ EFI_STATUS Status;

+ EFI_FFS_FILE_HEADER *FileHeader;

+ VOID *PcdRawData;

+ PEI_PCD_DATABASE *PeiDatabase;

+ PEI_PCD_DATABASE *UplDatabase;

+ EFI_HOB_GUID_TYPE *GuidHob;

+ DYNAMICEX_MAPPING *ExMapTable;

+ UINTN Index;

+

+ GuidHob = GetFirstGuidHob (&gPcdDataBaseHobGuid);

+ if (GuidHob == NULL) {

+ //

+ // No fix-up is needed.

+ //

+ return EFI_SUCCESS;

+ }

+ PeiDatabase = (PEI_PCD_DATABASE *) GET_GUID_HOB_DATA (GuidHob);

+ DEBUG ((DEBUG_INFO, "Find the Pei PCD data base, the total local token
number is %d\n", PeiDatabase->LocalTokenCount));

+

+ Status = FvFindFileByTypeGuid (DxeFv, EFI_FV_FILETYPE_DRIVER,
PcdGetPtr (PcdPcdDriverFile), &FileHeader);

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+ Status = FileFindSection (FileHeader, EFI_SECTION_RAW, &PcdRawData);

+ ASSERT_EFI_ERROR (Status);

+ if (EFI_ERROR (Status)) {

+ return Status;

+ }

+

+ UplDatabase = (PEI_PCD_DATABASE *) PcdRawData;

+ ExMapTable = (DYNAMICEX_MAPPING *) (UINTN) ((UINTN) PcdRawData
+ UplDatabase->ExMapTableOffset);

+

+ for (Index = 0; Index < UplDatabase->ExTokenCount; Index++) {

+ ExMapTable[Index].TokenNumber += PeiDatabase->LocalTokenCount;

+ }

+ DEBUG ((DEBUG_INFO, "Fix up UPL PCD database successfully\n"));

+ return EFI_SUCCESS;

+}

+

/**

Add HOB into HOB list



@@ -332,6 +386,7 @@ _ModuleEntryPoint (
Status = BuildHobs (BootloaderParameter, &DxeFv);

ASSERT_EFI_ERROR (Status);



+ FixUpPcdDatabase (DxeFv);

Status = UniversalLoadDxeCore (DxeFv, &DxeCoreEntryPoint);

ASSERT_EFI_ERROR (Status);



diff --git a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
index 77cd25aafd..76d7e4791c 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
+++ b/UefiPayloadPkg/UefiPayloadEntry/UniversalPayloadEntry.inf
@@ -63,6 +63,7 @@
gEfiAcpiTableGuid

gUefiSerialPortInfoGuid

gUniversalPayloadExtraDataGuid

+ gPcdDataBaseHobGuid



[FeaturePcd.IA32]

gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode ##
CONSUMES

@@ -72,6 +73,7 @@




[Pcd.IA32,Pcd.X64]

+ gUefiPayloadPkgTokenSpaceGuid.PcdPcdDriverFile

gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable ##
SOMETIMES_CONSUMES


gEfiMdeModulePkgTokenSpaceGuid.PcdPteMemoryEncryptionAddressOrM
ask ## CONSUMES


gEfiMdeModulePkgTokenSpaceGuid.PcdNullPointerDetectionPropertyMask
## CONSUMES

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dec
b/UefiPayloadPkg/UefiPayloadPkg.dec
index 105e1f5a1c..d84f560995 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dec
+++ b/UefiPayloadPkg/UefiPayloadPkg.dec
@@ -72,3 +72,5 @@
gUefiPayloadPkgTokenSpaceGuid.PcdMemoryTypeEfiRuntimeServicesCode
|0x80|UINT32|0x


# Size of the region used by UEFI in permanent memory


gUefiPayloadPkgTokenSpaceGuid.PcdSystemMemoryUefiRegionSize|0x020
00000|UINT32|0x00000017

+

+gUefiPayloadPkgTokenSpaceGuid.PcdPcdDriverFile|{ 0x57, 0x72, 0xcf, 0x80,
0xab, 0x87, 0xf9, 0x47, 0xa3, 0xfe, 0xD5, 0x0B, 0x76, 0xd8, 0x95,
0x41 }|VOID*|0x00000018

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
b/UefiPayloadPkg/UefiPayloadPkg.dsc
index d8277efccd..e3d669a6d6 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -298,6 +298,7 @@
gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE

gEfiMdeModulePkgTokenSpaceGuid.PcdBootManagerMenuFile|{ 0x21,
0xaa, 0x2c, 0x46, 0x14, 0x76, 0x03, 0x45, 0x83, 0x6e, 0x8a, 0xb6, 0xf4, 0x66,
0x23, 0x31 }



+ gUefiPayloadPkgTokenSpaceGuid.PcdPcdDriverFile|{ 0x57, 0x72, 0xcf,
0x80, 0xab, 0x87, 0xf9, 0x47, 0xa3, 0xfe, 0xD5, 0x0B, 0x76, 0xd8, 0x95, 0x41 }



!if $(SOURCE_DEBUG_ENABLE)


gEfiSourceLevelDebugPkgTokenSpaceGuid.PcdDebugLoadImageMethod|0x
2

--
2.16.2.windows.1


Re: [PATCH 04/12] UefiPayloadPkg: Update the function definition of HobConstructor

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 04/12] UefiPayloadPkg: Update the function definition of
HobConstructor

Update the function defination of HobConstructor to align the Phit Hob
structure.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c | 26 +++++++++++++--
-----------
UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c | 5 +----
UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h | 16 ++++++++--------
3 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
b/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
index 23a749edf1..768c3db770 100644
--- a/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
+++ b/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
@@ -39,14 +39,14 @@ GetHobList (
/**

Build a Handoff Information Table HOB



- This function initialize a HOB region from EfiMemoryBegin with length

- EfiMemoryLength. And EfiFreeMemoryBottom and EfiFreeMemoryTop
should

+ This function initialize a HOB region from EfiMemoryBegin to

+ EfiMemoryTop. And EfiFreeMemoryBottom and EfiFreeMemoryTop
should

be inside the HOB region.



- @param[in] EfiMemoryBegin Total memory start address

- @param[in] EfiMemoryLength Total memory length reported in handoff
HOB.

- @param[in] EfiFreeMemoryBottom Free memory start address

- @param[in] EfiFreeMemoryTop Free memory end address.

+ @param[in] EfiMemoryBottom Total memory start address

+ @param[in] EfiMemoryTop Total memory end address.

+ @param[in] EfiFreeMemoryBottom Free memory start address

+ @param[in] EfiFreeMemoryTop Free memory end address.



@return The pointer to the handoff HOB table.



@@ -54,8 +54,8 @@ GetHobList (
EFI_HOB_HANDOFF_INFO_TABLE*

EFIAPI

HobConstructor (

- IN VOID *EfiMemoryBegin,

- IN UINTN EfiMemoryLength,

+ IN VOID *EfiMemoryBottom,

+ IN VOID *EfiMemoryTop,

IN VOID *EfiFreeMemoryBottom,

IN VOID *EfiFreeMemoryTop

)

@@ -77,11 +77,11 @@ HobConstructor (
Hob->Version = EFI_HOB_HANDOFF_TABLE_VERSION;

Hob->BootMode = BOOT_WITH_FULL_CONFIGURATION;



- Hob->EfiMemoryTop = (UINTN)EfiMemoryBegin + EfiMemoryLength;

- Hob->EfiMemoryBottom = (UINTN)EfiMemoryBegin;

- Hob->EfiFreeMemoryTop = (UINTN)EfiFreeMemoryTop;

- Hob->EfiFreeMemoryBottom =
(EFI_PHYSICAL_ADDRESS)(UINTN)(HobEnd+1);

- Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS)(UINTN)HobEnd;

+ Hob->EfiMemoryTop = (EFI_PHYSICAL_ADDRESS) EfiMemoryTop;

+ Hob->EfiMemoryBottom = (EFI_PHYSICAL_ADDRESS) EfiMemoryBottom;

+ Hob->EfiFreeMemoryTop = (EFI_PHYSICAL_ADDRESS)
EfiFreeMemoryTop;

+ Hob->EfiFreeMemoryBottom = (EFI_PHYSICAL_ADDRESS) (UINTN)
(HobEnd+1);

+ Hob->EfiEndOfHobList = (EFI_PHYSICAL_ADDRESS) (UINTN) HobEnd;



mHobList = Hob;

return Hob;

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
index 8c6f7e326f..4308936d63 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
@@ -382,7 +382,6 @@ PayloadEntry (
PHYSICAL_ADDRESS DxeCoreEntryPoint;

EFI_HOB_HANDOFF_INFO_TABLE *HandoffHobTable;

UINTN MemBase;

- UINTN MemSize;

UINTN HobMemBase;

UINTN HobMemTop;

EFI_PEI_HOB_POINTERS Hob;

@@ -401,9 +400,7 @@ PayloadEntry (
HobMemBase = ALIGN_VALUE (MemBase + PcdGet32
(PcdPayloadFdMemSize), SIZE_1MB);

HobMemTop = HobMemBase + FixedPcdGet32
(PcdSystemMemoryUefiRegionSize);



- // DXE core assumes the memory below HOB region could be used, so
include the FV region memory into HOB range.

- MemSize = HobMemTop - MemBase;

- HandoffHobTable = HobConstructor ((VOID *)MemBase, MemSize, (VOID
*)HobMemBase, (VOID *)HobMemTop);

+ HobConstructor ((VOID *)MemBase, (VOID *)HobMemTop, (VOID
*)HobMemBase, (VOID *)HobMemTop);



// Build HOB based on information from Bootloader

Status = BuildHobFromBl ();

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
index a4c9da128e..e9c3ec3073 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
@@ -83,14 +83,14 @@ UpdateStackHob (
/**

Build a Handoff Information Table HOB



- This function initialize a HOB region from EfiMemoryBegin with length

- EfiMemoryLength. And EfiFreeMemoryBottom and EfiFreeMemoryTop
should

+ This function initialize a HOB region from EfiMemoryBegin to

+ EfiMemoryTop. And EfiFreeMemoryBottom and EfiFreeMemoryTop
should

be inside the HOB region.



- @param[in] EfiMemoryBegin Total memory start address

- @param[in] EfiMemoryLength Total memory length reported in handoff
HOB.

- @param[in] EfiFreeMemoryBottom Free memory start address

- @param[in] EfiFreeMemoryTop Free memory end address.

+ @param[in] EfiMemoryBottom Total memory start address

+ @param[in] EfiMemoryTop Total memory end address.

+ @param[in] EfiFreeMemoryBottom Free memory start address

+ @param[in] EfiFreeMemoryTop Free memory end address.



@return The pointer to the handoff HOB table.



@@ -98,8 +98,8 @@ UpdateStackHob (
EFI_HOB_HANDOFF_INFO_TABLE*

EFIAPI

HobConstructor (

- IN VOID *EfiMemoryBegin,

- IN UINTN EfiMemoryLength,

+ IN VOID *EfiMemoryBottom,

+ IN VOID *EfiMemoryTop,

IN VOID *EfiFreeMemoryBottom,

IN VOID *EfiFreeMemoryTop

);

--
2.16.2.windows.1


Re: [PATCH 03/12] UefiPayloadPkg: Add a separate PlatformHookLib for Universal Payload

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH 03/12] UefiPayloadPkg: Add a separate PlatformHookLib for
Universal Payload

Add a new separate PlatformHookLib for Universal Payload to consume Guid
Hob gUniversalPayloadSerialPortInfoGuid to get serial port information

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---

UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLib.
c | 82
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++

UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLib.
inf | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 123 insertions(+)

diff --git
a/UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLi
b.c
b/UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLi
b.c
new file mode 100644
index 0000000000..6705f29505
--- /dev/null
+++
b/UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLi
b.c
@@ -0,0 +1,82 @@
+/** @file

+ Platform Hook Library instance for UART device.

+

+ Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include <Base.h>

+#include <PiDxe.h>

+#include <UniversalPayload/SerialPortInfo.h>

+#include <Library/PlatformHookLib.h>

+#include <Library/PcdLib.h>

+#include <Library/HobLib.h>

+

+/**

+ Performs platform specific initialization required for the CPU to access

+ the hardware associated with a SerialPortLib instance. This function does

+ not initialize the serial port hardware itself. Instead, it initializes

+ hardware devices that are required for the CPU to access the serial port

+ hardware. This function may be called more than once.

+

+ @retval RETURN_SUCCESS The platform specific initialization succeeded.

+ @retval RETURN_DEVICE_ERROR The platform specific initialization could
not be completed.

+

+**/

+RETURN_STATUS

+EFIAPI

+PlatformHookSerialPortInitialize (

+ VOID

+ )

+{

+ RETURN_STATUS Status;

+ UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *SerialPortInfo;

+ UINT8 *GuidHob;

+ UNIVERSAL_PAYLOAD_GENERIC_HEADER *GenericHeader;

+

+ GuidHob = GetFirstGuidHob (&gUniversalPayloadSerialPortInfoGuid);

+ if (GuidHob == NULL) {

+ return EFI_NOT_FOUND;

+ }

+

+ GenericHeader = (UNIVERSAL_PAYLOAD_GENERIC_HEADER *)
GET_GUID_HOB_DATA (GuidHob);

+ if ((sizeof (UNIVERSAL_PAYLOAD_GENERIC_HEADER) >
GET_GUID_HOB_DATA_SIZE (GuidHob)) || (GenericHeader->Length >
GET_GUID_HOB_DATA_SIZE (GuidHob))) {

+ return EFI_NOT_FOUND;

+ }

+

+ if (GenericHeader->Revision ==
UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO_REVISION) {

+ SerialPortInfo = (UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO *)
GET_GUID_HOB_DATA (GuidHob);

+ if (GenericHeader->Length <
UNIVERSAL_PAYLOAD_SIZEOF_THROUGH_FIELD
(UNIVERSAL_PAYLOAD_SERIAL_PORT_INFO, RegisterBase)) {

+ //

+ // Return if can't find the Serial Port Info Hob with enough length

+ //

+ return EFI_NOT_FOUND;

+ }

+

+ Status = PcdSetBoolS (PcdSerialUseMmio, SerialPortInfo->UseMmio);

+ if (RETURN_ERROR (Status)) {

+ return Status;

+ }

+ Status = PcdSet64S (PcdSerialRegisterBase, SerialPortInfo->RegisterBase);

+ if (RETURN_ERROR (Status)) {

+ return Status;

+ }

+ Status = PcdSet32S (PcdSerialRegisterStride, SerialPortInfo-
RegisterStride);
+ if (RETURN_ERROR (Status)) {

+ return Status;

+ }

+ Status = PcdSet32S (PcdSerialBaudRate, SerialPortInfo->BaudRate);

+ if (RETURN_ERROR (Status)) {

+ return Status;

+ }

+ Status = PcdSet64S (PcdUartDefaultBaudRate, SerialPortInfo->BaudRate);

+ if (RETURN_ERROR (Status)) {

+ return Status;

+ }

+

+ return RETURN_SUCCESS;

+ }

+

+ return EFI_NOT_FOUND;

+}

diff --git
a/UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLi
b.inf
b/UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLi
b.inf
new file mode 100644
index 0000000000..41e05ddf54
--- /dev/null
+++
b/UefiPayloadPkg/Library/UniversalPayloadPlatformHookLib/PlatformHookLi
b.inf
@@ -0,0 +1,41 @@
+## @file

+# Platform Hook Library instance for UART device for Universal Payload.

+#

+# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010005

+ BASE_NAME = PlatformHookLib

+ FILE_GUID = 807E05AB-9411-429F-97F0-FE425BF6B094

+ MODULE_TYPE = BASE

+ VERSION_STRING = 1.0

+ LIBRARY_CLASS = PlatformHookLib

+ CONSTRUCTOR = PlatformHookSerialPortInitialize

+

+[Sources]

+ PlatformHookLib.c

+

+[LibraryClasses]

+ PcdLib

+ BaseLib

+ HobLib

+ DxeHobListLib

+

+[Packages]

+ MdePkg/MdePkg.dec

+ MdeModulePkg/MdeModulePkg.dec

+ UefiPayloadPkg/UefiPayloadPkg.dec

+

+[Guids]

+ gUniversalPayloadSerialPortInfoGuid

+

+[Pcd]

+ gEfiMdeModulePkgTokenSpaceGuid.PcdSerialUseMmio ## PRODUCES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterBase ##
PRODUCES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdSerialBaudRate ## PRODUCES

+ gEfiMdeModulePkgTokenSpaceGuid.PcdSerialRegisterStride ##
PRODUCES

+ gEfiMdePkgTokenSpaceGuid.PcdUartDefaultBaudRate ## PRODUCES

--
2.16.2.windows.1


Re: [PATCH 01/12] UefiPayloadPkg: Add HobLib for UniversalPayload

Guo Dong
 

Could you add description why you need add new library DxeHobLib + DxeHobListLib for DXE drivers instead of using MdePkg/Library/DxeHobLib/DxeHobLib.inf?
And please check if UefiLib is still required for new DxeHobLib since it is listed in the INF file.

BTW, it looks there are some comments updated in the HobLib from MdePkg. Maybe sync the changes to new Hob.

Thanks,
Guo

-----Original Message-----
From: Liu, Zhiguang <zhiguang.liu@...>
Sent: Sunday, June 20, 2021 8:47 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>; Ni, Ray
<ray.ni@...>
Subject: [PATCH 01/12] UefiPayloadPkg: Add HobLib for UniversalPayload

For payload entry, use PayloadEntryHobLib as HobLib and payload entry
should initialize hob base.
For DxeCore, use new added DxeHobLib as HobLib, and DxeCore will
initialize hob base.
For Dxe Driver, use new added DxeHobLib as HobLib, and use DxeHobListLib
to initialize hob base.

Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <benjamin.you@...>
Signed-off-by: Ray Ni <ray.ni@...>
Signed-off-by: Zhiguang Liu <zhiguang.liu@...>
---
UefiPayloadPkg/Include/Library/DxeHobListLib.h | 27
+++++++++++++++++++++++++++
UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.inf | 40
++++++++++++++++++++++++++++++++++++++++
UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.uni | 17
+++++++++++++++++
UefiPayloadPkg/Library/DxeHobLib/HobLib.c | 597
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++
UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.c | 66
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++
UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.inf | 35
+++++++++++++++++++++++++++++++++++
UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.c | 20
++++++++++++++++++++
UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.inf | 28
++++++++++++++++++++++++++++
UefiPayloadPkg/Library/{HobLib => PayloadEntryHobLib}/Hob.c | 2 +-
UefiPayloadPkg/Library/{HobLib => PayloadEntryHobLib}/HobLib.inf | 2 +-
UefiPayloadPkg/UefiPayloadPkg.dsc | 15 +++++++++++----
11 files changed, 843 insertions(+), 6 deletions(-)

diff --git a/UefiPayloadPkg/Include/Library/DxeHobListLib.h
b/UefiPayloadPkg/Include/Library/DxeHobListLib.h
new file mode 100644
index 0000000000..7e9b23f6d7
--- /dev/null
+++ b/UefiPayloadPkg/Include/Library/DxeHobListLib.h
@@ -0,0 +1,27 @@
+/** @file

+ Provides a service to retrieve a pointer to the start of HOB list.

+ Only available to DXE module types.

+

+ This library does not contain any functions or macros. It simply exports a
global

+ pointer to the start of HOB list as defined in the Platform Initialization
Driver

+ Execution Environment Core Interface Specification. The library
constructor must

+ initialize this global pointer to the start of HOB list, so it is available at the

+ module's entry point. Since there is overhead in looking up the pointer to
the start

+ of HOB list, only those modules that actually require access to the HOB list

+ should use this library.

+

+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#ifndef __DXE_HOB_LIST_LIB_H__

+#define __DXE_HOB_LIST_LIB_H__

+

+///

+/// Cache copy of the start of HOB list

+///

+extern VOID *gHobList;

+

+#endif

+

diff --git a/UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.inf
b/UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.inf
new file mode 100644
index 0000000000..a146befd25
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.inf
@@ -0,0 +1,40 @@
+## @file

+# Instance of HOB Library using HOB list from EFI Configuration Table.

+#

+# HOB Library implementation that retrieves the HOB List

+# from the System Configuration Table in the EFI System Table.

+#

+# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010005

+ BASE_NAME = DxeHobLib

+ MODULE_UNI_FILE = DxeHobLib.uni

+ FILE_GUID = 1a15b8b3-3e8a-4698-87b9-65aad9993b52

+ MODULE_TYPE = DXE_DRIVER

+ VERSION_STRING = 1.0

+ LIBRARY_CLASS = HobLib|DXE_DRIVER DXE_RUNTIME_DRIVER
SMM_CORE DXE_SMM_DRIVER UEFI_APPLICATION UEFI_DRIVER

+

+#

+# VALID_ARCHITECTURES = IA32 X64 EBC

+#

+

+[Sources]

+ HobLib.c

+

+

+[Packages]

+ MdePkg/MdePkg.dec

+ UefiPayloadPkg/UefiPayloadPkg.dec

+

+

+[LibraryClasses]

+ BaseMemoryLib

+ DebugLib

+ UefiLib

+ DxeHobListLib

diff --git a/UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.uni
b/UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.uni
new file mode 100644
index 0000000000..f1a18b7984
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.uni
@@ -0,0 +1,17 @@
+// /** @file

+// Instance of HOB Library using HOB list from EFI Configuration Table.

+//

+// HOB Library implementation that retrieves the HOB List

+// from the System Configuration Table in the EFI System Table.

+//

+// Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+//

+// SPDX-License-Identifier: BSD-2-Clause-Patent

+//

+// **/

+

+

+#string STR_MODULE_ABSTRACT #language en-US "Instance of HOB
Library using HOB list from EFI Configuration Table"

+

+#string STR_MODULE_DESCRIPTION #language en-US "The HOB Library
implementation that retrieves the HOB List from the System Configuration
Table in the EFI System Table."

+

diff --git a/UefiPayloadPkg/Library/DxeHobLib/HobLib.c
b/UefiPayloadPkg/Library/DxeHobLib/HobLib.c
new file mode 100644
index 0000000000..8a2e773c59
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobLib/HobLib.c
@@ -0,0 +1,597 @@
+/** @file

+ HOB Library implementation for Payload Phase.

+

+Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+#include <PiDxe.h>

+

+#include <Library/HobLib.h>

+#include <Library/DebugLib.h>

+#include <Library/BaseMemoryLib.h>

+#include <Library/DxeHobListLib.h>

+

+/**

+ Returns the pointer to the HOB list.

+

+ This function returns the pointer to first HOB in the list.

+ For PEI phase, the PEI service GetHobList() can be used to retrieve the
pointer

+ to the HOB list. For the DXE phase, the HOB list pointer can be retrieved
through

+ the EFI System Table by looking up theHOB list GUID in the System
Configuration Table.

+ Since the System Configuration Table does not exist that the time the DXE
Core is

+ launched, the DXE Core uses a global variable from the DXE Core Entry
Point Library

+ to manage the pointer to the HOB list.

+

+ If the pointer to the HOB list is NULL, then ASSERT().

+

+ @return The pointer to the HOB list.

+

+**/

+VOID *

+EFIAPI

+GetHobList (

+ VOID

+ )

+{

+ ASSERT (gHobList != NULL);

+ return gHobList;

+}

+

+/**

+ Returns the next instance of a HOB type from the starting HOB.

+

+ This function searches the first instance of a HOB type from the starting
HOB pointer.

+ If there does not exist such HOB type from the starting HOB pointer, it will
return NULL.

+ In contrast with macro GET_NEXT_HOB(), this function does not skip the
starting HOB pointer

+ unconditionally: it returns HobStart back if HobStart itself meets the
requirement;

+ caller is required to use GET_NEXT_HOB() if it wishes to skip current
HobStart.

+

+ If HobStart is NULL, then ASSERT().

+

+ @param Type The HOB type to return.

+ @param HobStart The starting HOB pointer to search from.

+

+ @return The next instance of a HOB type from the starting HOB.

+

+**/

+VOID *

+EFIAPI

+GetNextHob (

+ IN UINT16 Type,

+ IN CONST VOID *HobStart

+ )

+{

+ EFI_PEI_HOB_POINTERS Hob;

+

+ ASSERT (HobStart != NULL);

+

+ Hob.Raw = (UINT8 *) HobStart;

+ //

+ // Parse the HOB list until end of list or matching type is found.

+ //

+ while (!END_OF_HOB_LIST (Hob)) {

+ if (Hob.Header->HobType == Type) {

+ return Hob.Raw;

+ }

+ Hob.Raw = GET_NEXT_HOB (Hob);

+ }

+ return NULL;

+}

+

+/**

+ Returns the first instance of a HOB type among the whole HOB list.

+

+ This function searches the first instance of a HOB type among the whole
HOB list.

+ If there does not exist such HOB type in the HOB list, it will return NULL.

+

+ If the pointer to the HOB list is NULL, then ASSERT().

+

+ @param Type The HOB type to return.

+

+ @return The next instance of a HOB type from the starting HOB.

+

+**/

+VOID *

+EFIAPI

+GetFirstHob (

+ IN UINT16 Type

+ )

+{

+ VOID *HobList;

+

+ HobList = GetHobList ();

+ return GetNextHob (Type, HobList);

+}

+

+/**

+ Returns the next instance of the matched GUID HOB from the starting HOB.

+

+ This function searches the first instance of a HOB from the starting HOB
pointer.

+ Such HOB should satisfy two conditions:

+ its HOB type is EFI_HOB_TYPE_GUID_EXTENSION, and its GUID Name
equals to the input Guid.

+ If such a HOB from the starting HOB pointer does not exist, it will return
NULL.

+ Caller is required to apply GET_GUID_HOB_DATA () and
GET_GUID_HOB_DATA_SIZE ()

+ to extract the data section and its size information, respectively.

+ In contrast with macro GET_NEXT_HOB(), this function does not skip the
starting HOB pointer

+ unconditionally: it returns HobStart back if HobStart itself meets the
requirement;

+ caller is required to use GET_NEXT_HOB() if it wishes to skip current
HobStart.

+

+ If Guid is NULL, then ASSERT().

+ If HobStart is NULL, then ASSERT().

+

+ @param Guid The GUID to match with in the HOB list.

+ @param HobStart A pointer to a Guid.

+

+ @return The next instance of the matched GUID HOB from the starting
HOB.

+

+**/

+VOID *

+EFIAPI

+GetNextGuidHob (

+ IN CONST EFI_GUID *Guid,

+ IN CONST VOID *HobStart

+ )

+{

+ EFI_PEI_HOB_POINTERS GuidHob;

+

+ GuidHob.Raw = (UINT8 *) HobStart;

+ while ((GuidHob.Raw = GetNextHob (EFI_HOB_TYPE_GUID_EXTENSION,
GuidHob.Raw)) != NULL) {

+ if (CompareGuid (Guid, &GuidHob.Guid->Name)) {

+ break;

+ }

+ GuidHob.Raw = GET_NEXT_HOB (GuidHob);

+ }

+ return GuidHob.Raw;

+}

+

+/**

+ Returns the first instance of the matched GUID HOB among the whole HOB
list.

+

+ This function searches the first instance of a HOB among the whole HOB list.

+ Such HOB should satisfy two conditions:

+ its HOB type is EFI_HOB_TYPE_GUID_EXTENSION and its GUID Name
equals to the input Guid.

+ If such a HOB from the starting HOB pointer does not exist, it will return
NULL.

+ Caller is required to apply GET_GUID_HOB_DATA () and
GET_GUID_HOB_DATA_SIZE ()

+ to extract the data section and its size information, respectively.

+

+ If the pointer to the HOB list is NULL, then ASSERT().

+ If Guid is NULL, then ASSERT().

+

+ @param Guid The GUID to match with in the HOB list.

+

+ @return The first instance of the matched GUID HOB among the whole
HOB list.

+

+**/

+VOID *

+EFIAPI

+GetFirstGuidHob (

+ IN CONST EFI_GUID *Guid

+ )

+{

+ VOID *HobList;

+

+ HobList = GetHobList ();

+ return GetNextGuidHob (Guid, HobList);

+}

+

+/**

+ Get the system boot mode from the HOB list.

+

+ This function returns the system boot mode information from the

+ PHIT HOB in HOB list.

+

+ If the pointer to the HOB list is NULL, then ASSERT().

+

+ @param VOID

+

+ @return The Boot Mode.

+

+**/

+EFI_BOOT_MODE

+EFIAPI

+GetBootModeHob (

+ VOID

+ )

+{

+ EFI_HOB_HANDOFF_INFO_TABLE *HandOffHob;

+

+ HandOffHob = (EFI_HOB_HANDOFF_INFO_TABLE *) GetHobList ();

+

+ return HandOffHob->BootMode;

+}

+

+/**

+ Builds a HOB for a loaded PE32 module.

+

+ This function builds a HOB for a loaded PE32 module.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If ModuleName is NULL, then ASSERT().

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param ModuleName The GUID File Name of the module.

+ @param MemoryAllocationModule The 64 bit physical address of the
module.

+ @param ModuleLength The length of the module in bytes.

+ @param EntryPoint The 64 bit physical address of the module entry
point.

+

+**/

+VOID

+EFIAPI

+BuildModuleHob (

+ IN CONST EFI_GUID *ModuleName,

+ IN EFI_PHYSICAL_ADDRESS MemoryAllocationModule,

+ IN UINT64 ModuleLength,

+ IN EFI_PHYSICAL_ADDRESS EntryPoint

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a HOB that describes a chunk of system memory with Owner GUID.

+

+ This function builds a HOB that describes a chunk of system memory.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param ResourceType The type of resource described by this HOB.

+ @param ResourceAttribute The resource attributes of the memory
described by this HOB.

+ @param PhysicalStart The 64 bit physical address of memory described
by this HOB.

+ @param NumberOfBytes The length of the memory described by this
HOB in bytes.

+ @param OwnerGUID GUID for the owner of this resource.

+

+**/

+VOID

+EFIAPI

+BuildResourceDescriptorWithOwnerHob (

+ IN EFI_RESOURCE_TYPE ResourceType,

+ IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,

+ IN EFI_PHYSICAL_ADDRESS PhysicalStart,

+ IN UINT64 NumberOfBytes,

+ IN EFI_GUID *OwnerGUID

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a HOB that describes a chunk of system memory.

+

+ This function builds a HOB that describes a chunk of system memory.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param ResourceType The type of resource described by this HOB.

+ @param ResourceAttribute The resource attributes of the memory
described by this HOB.

+ @param PhysicalStart The 64 bit physical address of memory described
by this HOB.

+ @param NumberOfBytes The length of the memory described by this
HOB in bytes.

+

+**/

+VOID

+EFIAPI

+BuildResourceDescriptorHob (

+ IN EFI_RESOURCE_TYPE ResourceType,

+ IN EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute,

+ IN EFI_PHYSICAL_ADDRESS PhysicalStart,

+ IN UINT64 NumberOfBytes

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a customized HOB tagged with a GUID for identification and returns

+ the start address of GUID HOB data.

+

+ This function builds a customized HOB tagged with a GUID for identification

+ and returns the start address of GUID HOB data so that caller can fill the
customized data.

+ The HOB Header and Name field is already stripped.

+ It can only be invoked during PEI phase.

+ For DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If Guid is NULL, then ASSERT().

+ If there is no additional space for HOB creation, then ASSERT().

+ If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().

+ HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is
0xFFF8.

+

+ @param Guid The GUID to tag the customized HOB.

+ @param DataLength The size of the data payload for the GUID HOB.

+

+ @retval NULL The GUID HOB could not be allocated.

+ @retval others The start address of GUID HOB data.

+

+**/

+VOID *

+EFIAPI

+BuildGuidHob (

+ IN CONST EFI_GUID *Guid,

+ IN UINTN DataLength

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+ return NULL;

+}

+

+/**

+ Builds a customized HOB tagged with a GUID for identification, copies the
input data to the HOB

+ data field, and returns the start address of the GUID HOB data.

+

+ This function builds a customized HOB tagged with a GUID for identification
and copies the input

+ data to the HOB data field and returns the start address of the GUID HOB
data. It can only be

+ invoked during PEI phase; for DXE phase, it will ASSERT() because PEI HOB
is read-only for DXE phase.

+ The HOB Header and Name field is already stripped.

+ It can only be invoked during PEI phase.

+ For DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If Guid is NULL, then ASSERT().

+ If Data is NULL and DataLength > 0, then ASSERT().

+ If there is no additional space for HOB creation, then ASSERT().

+ If DataLength > (0xFFF8 - sizeof (EFI_HOB_GUID_TYPE)), then ASSERT().

+ HobLength is UINT16 and multiples of 8 bytes, so the max HobLength is
0xFFF8.

+

+ @param Guid The GUID to tag the customized HOB.

+ @param Data The data to be copied into the data field of the GUID
HOB.

+ @param DataLength The size of the data payload for the GUID HOB.

+

+ @retval NULL The GUID HOB could not be allocated.

+ @retval others The start address of GUID HOB data.

+

+**/

+VOID *

+EFIAPI

+BuildGuidDataHob (

+ IN CONST EFI_GUID *Guid,

+ IN VOID *Data,

+ IN UINTN DataLength

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+ return NULL;

+}

+

+/**

+ Builds a Firmware Volume HOB.

+

+ This function builds a Firmware Volume HOB.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+ If the FvImage buffer is not at its required alignment, then ASSERT().

+

+ @param BaseAddress The base address of the Firmware Volume.

+ @param Length The size of the Firmware Volume in bytes.

+

+**/

+VOID

+EFIAPI

+BuildFvHob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a EFI_HOB_TYPE_FV2 HOB.

+

+ This function builds a EFI_HOB_TYPE_FV2 HOB.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+ If the FvImage buffer is not at its required alignment, then ASSERT().

+

+ @param BaseAddress The base address of the Firmware Volume.

+ @param Length The size of the Firmware Volume in bytes.

+ @param FvName The name of the Firmware Volume.

+ @param FileName The name of the file.

+

+**/

+VOID

+EFIAPI

+BuildFv2Hob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length,

+ IN CONST EFI_GUID *FvName,

+ IN CONST EFI_GUID *FileName

+ )

+{

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a EFI_HOB_TYPE_FV3 HOB.

+

+ This function builds a EFI_HOB_TYPE_FV3 HOB.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() since PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+ If the FvImage buffer is not at its required alignment, then ASSERT().

+

+ @param BaseAddress The base address of the Firmware Volume.

+ @param Length The size of the Firmware Volume in bytes.

+ @param AuthenticationStatus The authentication status.

+ @param ExtractedFv TRUE if the FV was extracted as a file within

+ another firmware volume. FALSE otherwise.

+ @param FvName The name of the Firmware Volume.

+ Valid only if IsExtractedFv is TRUE.

+ @param FileName The name of the file.

+ Valid only if IsExtractedFv is TRUE.

+

+**/

+VOID

+EFIAPI

+BuildFv3Hob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length,

+ IN UINT32 AuthenticationStatus,

+ IN BOOLEAN ExtractedFv,

+ IN CONST EFI_GUID *FvName, OPTIONAL

+ IN CONST EFI_GUID *FileName OPTIONAL

+ )

+{

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a Capsule Volume HOB.

+

+ This function builds a Capsule Volume HOB.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If the platform does not support Capsule Volume HOBs, then ASSERT().

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param BaseAddress The base address of the Capsule Volume.

+ @param Length The size of the Capsule Volume in bytes.

+

+**/

+VOID

+EFIAPI

+BuildCvHob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a HOB for the CPU.

+

+ This function builds a HOB for the CPU.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param SizeOfMemorySpace The maximum physical memory
addressability of the processor.

+ @param SizeOfIoSpace The maximum physical I/O addressability of the
processor.

+

+**/

+VOID

+EFIAPI

+BuildCpuHob (

+ IN UINT8 SizeOfMemorySpace,

+ IN UINT8 SizeOfIoSpace

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a HOB for the Stack.

+

+ This function builds a HOB for the stack.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param BaseAddress The 64 bit physical address of the Stack.

+ @param Length The length of the stack in bytes.

+

+**/

+VOID

+EFIAPI

+BuildStackHob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a HOB for the BSP store.

+

+ This function builds a HOB for BSP store.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param BaseAddress The 64 bit physical address of the BSP.

+ @param Length The length of the BSP store in bytes.

+ @param MemoryType Type of memory allocated by this HOB.

+

+**/

+VOID

+EFIAPI

+BuildBspStoreHob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length,

+ IN EFI_MEMORY_TYPE MemoryType

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

+

+/**

+ Builds a HOB for the memory allocation.

+

+ This function builds a HOB for the memory allocation.

+ It can only be invoked during PEI phase;

+ for DXE phase, it will ASSERT() because PEI HOB is read-only for DXE phase.

+

+ If there is no additional space for HOB creation, then ASSERT().

+

+ @param BaseAddress The 64 bit physical address of the memory.

+ @param Length The length of the memory allocation in bytes.

+ @param MemoryType Type of memory allocated by this HOB.

+

+**/

+VOID

+EFIAPI

+BuildMemoryAllocationHob (

+ IN EFI_PHYSICAL_ADDRESS BaseAddress,

+ IN UINT64 Length,

+ IN EFI_MEMORY_TYPE MemoryType

+ )

+{

+ //

+ // PEI HOB is read only for DXE phase

+ //

+ ASSERT (FALSE);

+}

diff --git a/UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.c
b/UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.c
new file mode 100644
index 0000000000..8bb31b3f9c
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.c
@@ -0,0 +1,66 @@
+/** @file

+ This library retrieve the EFI_BOOT_SERVICES pointer from EFI system table
in

+ library's constructor.

+

+ Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+

+#include <Uefi.h>

+

+VOID *gHobList = NULL;

+

+/**

+ Local implementation of GUID comparasion that doesn't depend on
DebugLib::ASSERT().

+

+ This function compares Guid1 to Guid2. If the GUIDs are identical then
TRUE is returned.

+ If there are any bit differences in the two GUIDs, then FALSE is returned.

+

+ @param Guid1 A pointer to a 128 bit GUID.

+ @param Guid2 A pointer to a 128 bit GUID.

+

+ @retval TRUE Guid1 and Guid2 are identical.

+ @retval FALSE Guid1 and Guid2 are not identical.

+**/

+BOOLEAN

+LocalCompareGuid (

+ IN CONST GUID *Guid1,

+ IN CONST GUID *Guid2

+ )

+{

+ UINT64 *Left;

+ UINT64 *Right;

+

+ Left = (UINT64 *) Guid1;

+ Right = (UINT64 *) Guid2;

+

+ return (BOOLEAN) (Left[0] == Right[0] && Left[1] == Right[1]);

+}

+

+/**

+ @param ImageHandle The firmware allocated handle for the EFI image.

+ @param SystemTable A pointer to the EFI System Table.

+

+ @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.

+

+**/

+EFI_STATUS

+EFIAPI

+DxeHobListLibConstructor (

+ IN EFI_HANDLE ImageHandle,

+ IN EFI_SYSTEM_TABLE *SystemTable

+ )

+{

+ UINTN Index;

+

+ for (Index = 0; Index < SystemTable->NumberOfTableEntries; Index++) {

+ if (LocalCompareGuid (&gEfiHobListGuid, &SystemTable-
ConfigurationTable[Index].VendorGuid)) {
+ gHobList = SystemTable->ConfigurationTable[Index].VendorTable;

+ return EFI_SUCCESS;

+ }

+ }

+

+ return EFI_NOT_FOUND;

+}

diff --git a/UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.inf
b/UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.inf
new file mode 100644
index 0000000000..1115949556
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.inf
@@ -0,0 +1,35 @@
+## @file

+# UEFI Boot Services Table Library implementation.

+#

+# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010005

+ BASE_NAME = UefiPayloadInitHobLib

+ FILE_GUID = ff5c7a21-ab7a-4366-8616-11c6e53247b6

+ MODULE_TYPE = UEFI_DRIVER

+ VERSION_STRING = 1.0

+ LIBRARY_CLASS = DxeHobListLib|DXE_DRIVER
DXE_RUNTIME_DRIVER DXE_SMM_DRIVER UEFI_APPLICATION
UEFI_DRIVER SMM_CORE

+

+ CONSTRUCTOR = DxeHobListLibConstructor

+

+#

+# VALID_ARCHITECTURES = IA32 X64 EBC

+#

+

+[Sources]

+ DxeHobListLib.c

+

+[Packages]

+ MdePkg/MdePkg.dec

+ UefiPayloadPkg/UefiPayloadPkg.dec

+

+[Guids]

+ gEfiHobListGuid ## CONSUMES

+

+

diff --git a/UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.c
b/UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.c
new file mode 100644
index 0000000000..92d3e17ef0
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.c
@@ -0,0 +1,20 @@
+/** @file

+ This library retrieve the EFI_BOOT_SERVICES pointer from EFI system table
in

+ library's constructor.

+

+ Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+ SPDX-License-Identifier: BSD-2-Clause-Patent

+

+**/

+

+

+#include <Uefi.h>

+

+EFI_STATUS

+EFIAPI

+DxeHobListLibNullConstructor (

+ VOID

+ )

+{

+ return EFI_SUCCESS;

+}

diff --git a/UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.inf
b/UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.inf
new file mode 100644
index 0000000000..f17e5ebdbd
--- /dev/null
+++ b/UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull.inf
@@ -0,0 +1,28 @@
+## @file

+# UEFI Boot Services Table Library implementation.

+#

+# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>

+#

+# SPDX-License-Identifier: BSD-2-Clause-Patent

+#

+#

+##

+

+[Defines]

+ INF_VERSION = 0x00010005

+ BASE_NAME = DxeHobListLibNull

+ MODULE_TYPE = BASE

+ FILE_GUID = 060876c2-0e4e-4c63-8996-6af3710cfa64

+ VERSION_STRING = 1.0

+ LIBRARY_CLASS = DxeHobListLib

+ CONSTRUCTOR = DxeHobListLibNullConstructor

+

+#

+# VALID_ARCHITECTURES = IA32 X64 EBC

+#

+

+[Packages]

+ MdePkg/MdePkg.dec

+

+[Sources]

+ DxeHobListLibNull.c

diff --git a/UefiPayloadPkg/Library/HobLib/Hob.c
b/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
similarity index 96%
rename from UefiPayloadPkg/Library/HobLib/Hob.c
rename to UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
index c0b4cc0b0f..23a749edf1 100644
--- a/UefiPayloadPkg/Library/HobLib/Hob.c
+++ b/UefiPayloadPkg/Library/PayloadEntryHobLib/Hob.c
@@ -1,7 +1,7 @@
/** @file



Copyright (c) 2010, Apple Inc. All rights reserved.<BR>

- Copyright (c) 2017 - 2020, Intel Corporation. All rights reserved.<BR>

+ Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.<BR>



SPDX-License-Identifier: BSD-2-Clause-Patent



diff --git a/UefiPayloadPkg/Library/HobLib/HobLib.inf
b/UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf
similarity index 87%
rename from UefiPayloadPkg/Library/HobLib/HobLib.inf
rename to UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf
index 030e22a810..cbb4f02efc 100644
--- a/UefiPayloadPkg/Library/HobLib/HobLib.inf
+++ b/UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf
@@ -1,6 +1,6 @@
#/** @file

#

-# Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.<BR>

+# Copyright (c) 2018 - 2021, Intel Corporation. All rights reserved.<BR>

# Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>

#

# SPDX-License-Identifier: BSD-2-Clause-Patent

diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc
b/UefiPayloadPkg/UefiPayloadPkg.dsc
index 21b360256b..d8277efccd 100644
--- a/UefiPayloadPkg/UefiPayloadPkg.dsc
+++ b/UefiPayloadPkg/UefiPayloadPkg.dsc
@@ -27,6 +27,7 @@


DEFINE SOURCE_DEBUG_ENABLE = FALSE

DEFINE PS2_KEYBOARD_ENABLE = FALSE

+ DEFINE UNIVERSAL_PAYLOAD = FALSE



#

# SBL: UEFI payload for Slim Bootloader

@@ -146,6 +147,13 @@

PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/Base
PeCoffGetEntryPointLib.inf


CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLib/BaseCac
heMaintenanceLib.inf

SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf

+ DxeHobListLib|UefiPayloadPkg/Library/DxeHobListLib/DxeHobListLib.inf

+

+!if $(UNIVERSAL_PAYLOAD) == TRUE

+ HobLib|UefiPayloadPkg/Library/DxeHobLib/DxeHobLib.inf

+!else

+ HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf

+!endif



#

# UEFI & PI

@@ -221,10 +229,12 @@

VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/Va
riablePolicyHelperLib.inf



[LibraryClasses.common.SEC]

- HobLib|UefiPayloadPkg/Library/HobLib/HobLib.inf

+ HobLib|UefiPayloadPkg/Library/PayloadEntryHobLib/HobLib.inf

PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf

+
DxeHobListLib|UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull
.inf



[LibraryClasses.common.DXE_CORE]

+
DxeHobListLib|UefiPayloadPkg/Library/DxeHobListLibNull/DxeHobListLibNull
.inf

PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf

HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf


MemoryAllocationLib|MdeModulePkg/Library/DxeCoreMemoryAllocationLi
b/DxeCoreMemoryAllocationLib.inf

@@ -238,7 +248,6 @@


[LibraryClasses.common.DXE_DRIVER]

PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

- HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf


MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemo
ryAllocationLib.inf


ExtractGuidedSectionLib|MdePkg/Library/DxeExtractGuidedSectionLib/DxeE
xtractGuidedSectionLib.inf


ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/Dx
eReportStatusCodeLib.inf

@@ -251,7 +260,6 @@


[LibraryClasses.common.DXE_RUNTIME_DRIVER]

PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf

- HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf


MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemo
ryAllocationLib.inf


ReportStatusCodeLib|MdeModulePkg/Library/RuntimeDxeReportStatusCod
eLib/RuntimeDxeReportStatusCodeLib.inf


VariablePolicyLib|MdeModulePkg/Library/VariablePolicyLib/VariablePolicyLi
bRuntimeDxe.inf

@@ -260,7 +268,6 @@
PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf


MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemo
ryAllocationLib.inf


ReportStatusCodeLib|MdeModulePkg/Library/DxeReportStatusCodeLib/Dx
eReportStatusCodeLib.inf

- HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf




##########################################################
######################

#

--
2.16.2.windows.1


Re: [PATCH v4 1/4] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls.

Lendacky, Thomas
 

On 6/21/21 8:56 AM, Ashish Kalra wrote:
From: Ashish Kalra <ashish.kalra@...>

Add SEV and SEV-ES hypercall abstraction library to support SEV Page
encryption/deceryption status hypercalls for SEV and SEV-ES guests.
Does this have to be a new library? It's just a single function and so I
would think it could live in the BaseMemEncryptSevLib library where the
change to the c-bit is being done anyway.


Cc: Jordan Justen <jordan.l.justen@...>
Cc: Laszlo Ersek <lersek@...>
Cc: Ard Biesheuvel <ard.biesheuvel@...>

Signed-off-by: Ashish Kalra <ashish.kalra@...>
---
Maintainers.txt | 2 +
OvmfPkg/Include/Library/MemEncryptHypercallLib.h | 43 ++++++++
OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c | 37 +++++++
OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf | 42 ++++++++
OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm | 28 ++++++
OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c | 105 ++++++++++++++++++++
OvmfPkg/OvmfPkgIa32.dsc | 1 +
OvmfPkg/OvmfPkgIa32X64.dsc | 1 +
OvmfPkg/OvmfPkgX64.dsc | 1 +
OvmfPkg/OvmfXen.dsc | 1 +
10 files changed, 261 insertions(+)

diff --git a/Maintainers.txt b/Maintainers.txt
index ea54e0b7e9..8ecc8464ba 100644
--- a/Maintainers.txt
+++ b/Maintainers.txt
@@ -449,8 +449,10 @@ F: OvmfPkg/AmdSev/
F: OvmfPkg/AmdSevDxe/
F: OvmfPkg/Include/Guid/ConfidentialComputingSecret.h
F: OvmfPkg/Include/Library/MemEncryptSevLib.h
+F: OvmfPkg/Include/Library/MemEncryptHypercallLib.h
F: OvmfPkg/IoMmuDxe/AmdSevIoMmu.*
F: OvmfPkg/Library/BaseMemEncryptSevLib/
+F: OvmfPkg/Library/MemEncryptHypercallLib/
F: OvmfPkg/Library/PlatformBootManagerLibGrub/
F: OvmfPkg/Library/VmgExitLib/
F: OvmfPkg/PlatformPei/AmdSev.c
diff --git a/OvmfPkg/Include/Library/MemEncryptHypercallLib.h b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h
new file mode 100644
index 0000000000..b241a189b6
--- /dev/null
+++ b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h
@@ -0,0 +1,43 @@
+/** @file
+
+ Define Secure Encrypted Virtualization (SEV) hypercall library.
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _MEM_ENCRYPT_HYPERCALL_LIB_H_
+#define _MEM_ENCRYPT_HYPERCALL_LIB_H_
+
+#include <Base.h>
+
+#define KVM_HC_MAP_GPA_RANGE 12
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_4K 0
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M (1 << 0)
Use the BIT0 define, e.g.:
#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M BIT0

+#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G (1 << 1)
BIT1

Also, where are these used? Are they supposed to be part of the "Mode"
parameter, in which case the comment for Mode below is incorrect.

+#define KVM_MAP_GPA_RANGE_ENC_STAT(n) ((n) << 4)
+#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(1)
+#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(0)
+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN UINTN PhysicalAddress,
+ IN UINTN Length,
+ IN UINTN Mode
+ );
+
+#endif
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c
new file mode 100644
index 0000000000..2e73d47ee6
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c
@@ -0,0 +1,37 @@
+/** @file
+
+ Secure Encrypted Virtualization (SEV) hypercall helper library
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/BaseLib.h>
+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
This isn't actually SetCBit or ClearCBit based on how I see it used below.

+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN PHYSICAL_ADDRESS PhysicalAddress,
+ IN UINTN Pages,
This doesn't match the comment above.

+ IN UINTN Mode
+ )
+{
+ //
+ // Memory encryption bit is not accessible in 32-bit mode
+ //
+}
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
new file mode 100644
index 0000000000..a77d58a7e6
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
@@ -0,0 +1,42 @@
+## @file
+# Library provides the hypervisor helper functions for SEV guest
+#
+# Copyright (c) 2020 Advanced Micro Devices. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+ INF_VERSION = 1.25
+ BASE_NAME = MemEncryptHypercallLib
+ FILE_GUID = 86f2501e-f128-45f3-91c4-3cff31656ca8
+ MODULE_TYPE = BASE
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = MemEncryptHypercallLib
+
+#
+# The following information is for reference only and not required by the build
+# tools.
+#
+# VALID_ARCHITECTURES = IA32 X64
+#
+
+[Packages]
+ MdeModulePkg/MdeModulePkg.dec
+ MdePkg/MdePkg.dec
+ UefiCpuPkg/UefiCpuPkg.dec
+ OvmfPkg/OvmfPkg.dec
+
+[Sources.X64]
+ X64/MemEncryptHypercallLib.c
+ X64/AsmHelperStub.nasm
+
+[Sources.IA32]
+ Ia32/MemEncryptHypercallLib.c
+
+[LibraryClasses]
+ BaseLib
+ DebugLib
+ VmgExitLib
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm
new file mode 100644
index 0000000000..f29b96f9b0
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm
@@ -0,0 +1,28 @@
+DEFAULT REL
+SECTION .text
+
+; VOID
+; EFIAPI
+; SetMemoryEncDecHypercall3AsmStub (
+; IN UINT HypercallNum,
+; IN INTN Arg1,
+; IN INTN Arg2,
+; IN INTN Arg3
+; );
+global ASM_PFX(SetMemoryEncDecHypercall3AsmStub)
+ASM_PFX(SetMemoryEncDecHypercall3AsmStub):
+ ; UEFI calling conventions require RBX to
+ ; be nonvolatile/callee-saved.
+ push rbx
+ ; Copy HypercallNumber to rax
Placing these comments on the same line will make this more readable, e.g.:

mov rax, rcx ; Copy HypercallNum to rax

+ mov rax, rcx
+ ; Copy Arg1 to the register expected by KVM
+ mov rbx, rdx
+ ; Copy Arg2 to register expected by KVM
+ mov rcx, r8
+ ; Copy Arg2 to register expected by KVM
+ mov rdx, r9
+ ; Call VMMCALL
+ vmmcall
+ pop rbx
+ ret
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c
new file mode 100644
index 0000000000..1c09ea012b
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c
@@ -0,0 +1,105 @@
+/** @file
+
+ Secure Encrypted Virtualization (SEV) hypercall helper library
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/VmgExitLib.h>
+#include <Register/Amd/Ghcb.h>
+#include <Register/Amd/Msr.h>
+#include <Library/MemEncryptSevLib.h>
+#include <Library/MemEncryptHypercallLib.h>
+
+//
+// Interface exposed by the ASM implementation of the core hypercall
+//
+//
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3AsmStub (
+ IN UINTN HypercallNum,
+ IN UINTN PhysicalAddress,
+ IN UINTN Length,
+ IN UINTN Mode
+ );
+
+STATIC
+VOID
+GhcbSetRegValid (
+ IN OUT GHCB *Ghcb,
+ IN GHCB_REGISTER Reg
+ )
+{
+ UINT32 RegIndex;
+ UINT32 RegBit;
+
+ RegIndex = Reg / 8;
+ RegBit = Reg & 0x07;
+
+ Ghcb->SaveArea.ValidBitmap[RegIndex] |= (1 << RegBit);
+}
As Brijesh noted, you should be using the function defined in VmgExitLib.h.

+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN PHYSICAL_ADDRESS PhysicalAddress,
+ IN UINTN Pages,
+ IN UINTN Mode
+ )
+{
+ if (MemEncryptSevEsIsEnabled ()) {
+ MSR_SEV_ES_GHCB_REGISTER Msr;
+ GHCB *Ghcb;
+ BOOLEAN InterruptState;
+ UINT64 Status;
+
+ Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
+ Ghcb = Msr.Ghcb;
+
+ VmgInit (Ghcb, &InterruptState);
+
+ Ghcb->SaveArea.Rax = KVM_HC_MAP_GPA_RANGE;
+ GhcbSetRegValid (Ghcb, GhcbRax);
+ Ghcb->SaveArea.Rbx = PhysicalAddress;
+ GhcbSetRegValid (Ghcb, GhcbRbx);
+ Ghcb->SaveArea.Rcx = Pages;
+ GhcbSetRegValid (Ghcb, GhcbRcx);
+ Ghcb->SaveArea.Rdx = Mode;
+ GhcbSetRegValid (Ghcb, GhcbRdx);
+ Ghcb->SaveArea.Cpl = AsmReadCs() & 0x3;
+ GhcbSetRegValid (Ghcb, GhcbCpl);
+
+ Status = VmgExit (Ghcb, SVM_EXIT_VMMCALL, 0, 0);
+ if (Status) {
+ DEBUG ((DEBUG_ERROR, "SVM_EXIT_VMMCALL failed %lx\n", Status));
+ }
+ VmgDone (Ghcb, InterruptState);
+ } else {
+ SetMemoryEncDecHypercall3AsmStub (
+ KVM_HC_MAP_GPA_RANGE,
+ PhysicalAddress,
+ Pages,
+ Mode
+ );
+ }
+}
You could just issue the VMMCALL and, for SEV-ES, let the VC handler take
care of this. You would just have to add some smarts to the VC handler to
compare the hypercall number and add the additional register values. You
could probably get rid of a level of function calls that way. Thoughts?

Thanks,
Tom

diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index f53efeae79..36f1d82ce7 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -176,6 +176,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index b3662e17f2..2a743688b4 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -180,6 +180,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index 0a237a9058..eb9da51a15 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -180,6 +180,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc
index 3c1ca6bfd4..de0c052832 100644
--- a/OvmfPkg/OvmfXen.dsc
+++ b/OvmfPkg/OvmfXen.dsc
@@ -167,6 +167,7 @@
QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgDxeLib.inf
QemuLoadImageLib|OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf
FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf


Re: [PATCH v4 3/4] OvmfPkg/PlatformPei: Mark SEC GHCB page as unencrypted via hypercall

Brijesh Singh
 

On 6/21/2021 8:57 AM, Ashish Kalra wrote:
From: Ashish Kalra <ashish.kalra@...>

Mark the SEC GHCB page (that is mapped as unencrypted in
ResetVector code) in the hypervisor page status tracking.

Cc: Jordan Justen <jordan.l.justen@...>
Cc: Laszlo Ersek <lersek@...>
Cc: Ard Biesheuvel <ard.biesheuvel@...>
Remove this new line.

Signed-off-by: Ashish Kalra <ashish.kalra@...>
---
OvmfPkg/PlatformPei/AmdSev.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c
index a8bf610022..3f642ecb06 100644
--- a/OvmfPkg/PlatformPei/AmdSev.c
+++ b/OvmfPkg/PlatformPei/AmdSev.c
@@ -15,6 +15,7 @@
#include <Library/HobLib.h>
#include <Library/MemEncryptSevLib.h>
#include <Library/MemoryAllocationLib.h>
+#include <Library/MemEncryptHypercallLib.h>
#include <Library/PcdLib.h>
#include <PiPei.h>
#include <Register/Amd/Msr.h>
@@ -52,6 +53,15 @@ AmdSevEsInitialize (
PcdStatus = PcdSetBoolS (PcdSevEsIsEnabled, TRUE);
ASSERT_RETURN_ERROR (PcdStatus);

+ //
+ // GHCB_BASE setup during reset-vector needs to be marked as
+ // decrypted in the hypervisor page encryption bitmap.
+ //
+ SetMemoryEncDecHypercall3 (FixedPcdGet32 (PcdOvmfSecGhcbBase),
+ EFI_SIZE_TO_PAGES(FixedPcdGet32 (PcdOvmfSecGhcbSize)),
+ KVM_MAP_GPA_RANGE_DECRYPTED
+ );
+
Typically we should invoke the HC as soon as the page state is changed in the PTE.
Why we are notifying it too late? Is this because you are trying to avoid asm code
or there is no MSR protocol for VMMCALL NAE ?

I am okay with not notifying in ASM code, but at least we should notify the change
during the ES protocol negotiation and before the GHCB is setup. In other words,
do it inside the SevEsProtocolCheck() [Sec/SecMain.c].

-Brijesh


Re: [PATCH v4 1/4] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls.

Brijesh Singh
 

On 6/22/2021 2:47 PM, Brijesh Singh wrote:


On 6/21/2021 8:56 AM, Ashish Kalra wrote:
From: Ashish Kalra <ashish.kalra@...>

Add SEV and SEV-ES hypercall abstraction library to support SEV Page
encryption/deceryption status hypercalls for SEV and SEV-ES guests.

Cc: Jordan Justen <jordan.l.justen@...>
Cc: Laszlo Ersek <lersek@...>
Cc: Ard Biesheuvel <ard.biesheuvel@...>
Remove this newline.

Signed-off-by: Ashish Kalra <ashish.kalra@...>
---
Maintainers.txt | 2 +
OvmfPkg/Include/Library/MemEncryptHypercallLib.h | 43 ++++++++
OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c | 37 +++++++
OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf | 42 ++++++++
OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm | 28 ++++++
OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c | 105 ++++++++++++++++++++
OvmfPkg/OvmfPkgIa32.dsc | 1 +
OvmfPkg/OvmfPkgIa32X64.dsc | 1 +
OvmfPkg/OvmfPkgX64.dsc | 1 +
OvmfPkg/OvmfXen.dsc | 1 +
10 files changed, 261 insertions(+)

diff --git a/Maintainers.txt b/Maintainers.txt
index ea54e0b7e9..8ecc8464ba 100644
--- a/Maintainers.txt
+++ b/Maintainers.txt
@@ -449,8 +449,10 @@ F: OvmfPkg/AmdSev/
F: OvmfPkg/AmdSevDxe/
F: OvmfPkg/Include/Guid/ConfidentialComputingSecret.h
F: OvmfPkg/Include/Library/MemEncryptSevLib.h
+F: OvmfPkg/Include/Library/MemEncryptHypercallLib.h
F: OvmfPkg/IoMmuDxe/AmdSevIoMmu.*
F: OvmfPkg/Library/BaseMemEncryptSevLib/
+F: OvmfPkg/Library/MemEncryptHypercallLib/
F: OvmfPkg/Library/PlatformBootManagerLibGrub/
F: OvmfPkg/Library/VmgExitLib/
F: OvmfPkg/PlatformPei/AmdSev.c
diff --git a/OvmfPkg/Include/Library/MemEncryptHypercallLib.h b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h
new file mode 100644
index 0000000000..b241a189b6
--- /dev/null
+++ b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h
@@ -0,0 +1,43 @@
+/** @file
+
+ Define Secure Encrypted Virtualization (SEV) hypercall library.
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
^^^^
2021

+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _MEM_ENCRYPT_HYPERCALL_LIB_H_
+#define _MEM_ENCRYPT_HYPERCALL_LIB_H_
+
+#include <Base.h>
+
+#define KVM_HC_MAP_GPA_RANGE 12
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_4K 0
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M (1 << 0)
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G (1 << 1)
+#define KVM_MAP_GPA_RANGE_ENC_STAT(n) ((n) << 4)
+#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(1)
+#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(0)
+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
Looking at the function signature it seems this routine is used for both set
and clear. Please update the comment accordingly.


+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN UINTN PhysicalAddress,
+ IN UINTN Length,
+ IN UINTN Mode
+ );
+
+#endif
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c
new file mode 100644
index 0000000000..2e73d47ee6
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c
@@ -0,0 +1,37 @@
+/** @file
+
+ Secure Encrypted Virtualization (SEV) hypercall helper library
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
^^^^
2021

+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/BaseLib.h>
+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN PHYSICAL_ADDRESS PhysicalAddress,
+ IN UINTN Pages,
+ IN UINTN Mode
+ )
+{
+ //
+ // Memory encryption bit is not accessible in 32-bit mode
+ //
+}
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
new file mode 100644
index 0000000000..a77d58a7e6
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
@@ -0,0 +1,42 @@
+## @file
+# Library provides the hypervisor helper functions for SEV guest
+#
+# Copyright (c) 2020 Advanced Micro Devices. All rights reserved.<BR>
^^^^
2021
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+ INF_VERSION = 1.25
+ BASE_NAME = MemEncryptHypercallLib
+ FILE_GUID = 86f2501e-f128-45f3-91c4-3cff31656ca8
+ MODULE_TYPE = BASE
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = MemEncryptHypercallLib
+
+#
+# The following information is for reference only and not required by the build
+# tools.
+#
+# VALID_ARCHITECTURES = IA32 X64
+#
+
+[Packages]
+ MdeModulePkg/MdeModulePkg.dec
+ MdePkg/MdePkg.dec
+ UefiCpuPkg/UefiCpuPkg.dec
+ OvmfPkg/OvmfPkg.dec
+
+[Sources.X64]
+ X64/MemEncryptHypercallLib.c
+ X64/AsmHelperStub.nasm
+
+[Sources.IA32]
+ Ia32/MemEncryptHypercallLib.c
+
+[LibraryClasses]
+ BaseLib
+ DebugLib
+ VmgExitLib
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm
new file mode 100644
index 0000000000..f29b96f9b0
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm
@@ -0,0 +1,28 @@
+DEFAULT REL
+SECTION .text
+
+; VOID
+; EFIAPI
+; SetMemoryEncDecHypercall3AsmStub (
+; IN UINT HypercallNum,
+; IN INTN Arg1,
+; IN INTN Arg2,
+; IN INTN Arg3
+; );
+global ASM_PFX(SetMemoryEncDecHypercall3AsmStub)
+ASM_PFX(SetMemoryEncDecHypercall3AsmStub):
+ ; UEFI calling conventions require RBX to
+ ; be nonvolatile/callee-saved.
+ push rbx
+ ; Copy HypercallNumber to rax
+ mov rax, rcx
+ ; Copy Arg1 to the register expected by KVM
+ mov rbx, rdx
+ ; Copy Arg2 to register expected by KVM
+ mov rcx, r8
+ ; Copy Arg2 to register expected by KVM
+ mov rdx, r9
+ ; Call VMMCALL
+ vmmcall
+ pop rbx
+ ret
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c
new file mode 100644
index 0000000000..1c09ea012b
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c
@@ -0,0 +1,105 @@
+/** @file
+
+ Secure Encrypted Virtualization (SEV) hypercall helper library
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
^^^^
2021
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/VmgExitLib.h>
+#include <Register/Amd/Ghcb.h>
+#include <Register/Amd/Msr.h>
+#include <Library/MemEncryptSevLib.h>
+#include <Library/MemEncryptHypercallLib.h>
+
+//
+// Interface exposed by the ASM implementation of the core hypercall
+//
+//
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3AsmStub (
+ IN UINTN HypercallNum,
+ IN UINTN PhysicalAddress,
+ IN UINTN Length,
+ IN UINTN Mode
+ );
+
The function signature does not match with documented signature. Fix the
SetMemoryEncDecHypercall3AsmStub() documented in AsmHelperStub.nasm to use
UINTN.


+STATIC
+VOID
+GhcbSetRegValid (
+ IN OUT GHCB *Ghcb,
+ IN GHCB_REGISTER Reg
+ )
+{
+ UINT32 RegIndex;
+ UINT32 RegBit;
+
+ RegIndex = Reg / 8;
+ RegBit = Reg & 0x07;
+
+ Ghcb->SaveArea.ValidBitmap[RegIndex] |= (1 << RegBit);
+}
+
This looks similar to VmgSetOffsetValid().

+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+Please update the comment.
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN PHYSICAL_ADDRESS PhysicalAddress,
+ IN UINTN Pages,
+ IN UINTN Mode
+ )
+{
+ if (MemEncryptSevEsIsEnabled ()) {> + MSR_SEV_ES_GHCB_REGISTER Msr;
+ GHCB *Ghcb;
+ BOOLEAN InterruptState;
+ UINT64 Status;
+
+ Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
+ Ghcb = Msr.Ghcb;
+
+ VmgInit (Ghcb, &InterruptState);
+
+ Ghcb->SaveArea.Rax = KVM_HC_MAP_GPA_RANGE;
+ GhcbSetRegValid (Ghcb, GhcbRax);
+ Ghcb->SaveArea.Rbx = PhysicalAddress;
+ GhcbSetRegValid (Ghcb, GhcbRbx);
+ Ghcb->SaveArea.Rcx = Pages;
+ GhcbSetRegValid (Ghcb, GhcbRcx);
+ Ghcb->SaveArea.Rdx = Mode;
+ GhcbSetRegValid (Ghcb, GhcbRdx);
+ Ghcb->SaveArea.Cpl = AsmReadCs() & 0x3;
+ GhcbSetRegValid (Ghcb, GhcbCpl);
+
+ Status = VmgExit (Ghcb, SVM_EXIT_VMMCALL, 0, 0);
+ if (Status) {
+ DEBUG ((DEBUG_ERROR, "SVM_EXIT_VMMCALL failed %lx\n", Status));
You need to issue an SEV-ES guest termination vmexit followed by a deadloop to ensure
that boot does not proceed.

You probably also need to check for the RAX register for the return code.


+ }
+ VmgDone (Ghcb, InterruptState);
+ } else {
+ SetMemoryEncDecHypercall3AsmStub (
+ KVM_HC_MAP_GPA_RANGE,
+ PhysicalAddress,
+ Pages,
+ Mode
+ );
How do you know whether the hyperviosr supports the Live migration ? In other
words, is it safe to call the HC without knowing if HV supports the feature ?
I see that in patch#4 you are adding a support to query the HV features flag
to determine whether HV supports the Live migration. You probably need to
pull that function into this library and and check the feature flag before
invoking HC.

This library can also export the function for others to use. You can cache
the value on the first call (similar to how BaseMemEncryptSevLib does for
the MemEncryptSevIsEnabled).


Also, what will happen if we pass a bogus GPA. Does the HC return an error ?
Same as SEV-ES block, you probably need to check the RAX register for the
return code. On failure, cause an assert() and terminate the boot.


+ }
+}
diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index f53efeae79..36f1d82ce7 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -176,6 +176,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index b3662e17f2..2a743688b4 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -180,6 +180,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index 0a237a9058..eb9da51a15 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -180,6 +180,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc
index 3c1ca6bfd4..de0c052832 100644
--- a/OvmfPkg/OvmfXen.dsc
+++ b/OvmfPkg/OvmfXen.dsc
@@ -167,6 +167,7 @@
QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgDxeLib.inf
QemuLoadImageLib|OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf
FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf
Update the AmdSev.dsc to include this library.


-Brijesh


Re: [PATCH v4 1/4] OvmfPkg/MemEncryptHypercallLib: add library to support SEV hypercalls.

Brijesh Singh
 

On 6/21/2021 8:56 AM, Ashish Kalra wrote:
From: Ashish Kalra <ashish.kalra@...>

Add SEV and SEV-ES hypercall abstraction library to support SEV Page
encryption/deceryption status hypercalls for SEV and SEV-ES guests.

Cc: Jordan Justen <jordan.l.justen@...>
Cc: Laszlo Ersek <lersek@...>
Cc: Ard Biesheuvel <ard.biesheuvel@...>
Remove this newline.

Signed-off-by: Ashish Kalra <ashish.kalra@...>
---
Maintainers.txt | 2 +
OvmfPkg/Include/Library/MemEncryptHypercallLib.h | 43 ++++++++
OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c | 37 +++++++
OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf | 42 ++++++++
OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm | 28 ++++++
OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c | 105 ++++++++++++++++++++
OvmfPkg/OvmfPkgIa32.dsc | 1 +
OvmfPkg/OvmfPkgIa32X64.dsc | 1 +
OvmfPkg/OvmfPkgX64.dsc | 1 +
OvmfPkg/OvmfXen.dsc | 1 +
10 files changed, 261 insertions(+)

diff --git a/Maintainers.txt b/Maintainers.txt
index ea54e0b7e9..8ecc8464ba 100644
--- a/Maintainers.txt
+++ b/Maintainers.txt
@@ -449,8 +449,10 @@ F: OvmfPkg/AmdSev/
F: OvmfPkg/AmdSevDxe/
F: OvmfPkg/Include/Guid/ConfidentialComputingSecret.h
F: OvmfPkg/Include/Library/MemEncryptSevLib.h
+F: OvmfPkg/Include/Library/MemEncryptHypercallLib.h
F: OvmfPkg/IoMmuDxe/AmdSevIoMmu.*
F: OvmfPkg/Library/BaseMemEncryptSevLib/
+F: OvmfPkg/Library/MemEncryptHypercallLib/
F: OvmfPkg/Library/PlatformBootManagerLibGrub/
F: OvmfPkg/Library/VmgExitLib/
F: OvmfPkg/PlatformPei/AmdSev.c
diff --git a/OvmfPkg/Include/Library/MemEncryptHypercallLib.h b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h
new file mode 100644
index 0000000000..b241a189b6
--- /dev/null
+++ b/OvmfPkg/Include/Library/MemEncryptHypercallLib.h
@@ -0,0 +1,43 @@
+/** @file
+
+ Define Secure Encrypted Virtualization (SEV) hypercall library.
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
^^^^
2021

+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef _MEM_ENCRYPT_HYPERCALL_LIB_H_
+#define _MEM_ENCRYPT_HYPERCALL_LIB_H_
+
+#include <Base.h>
+
+#define KVM_HC_MAP_GPA_RANGE 12
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_4K 0
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_2M (1 << 0)
+#define KVM_MAP_GPA_RANGE_PAGE_SZ_1G (1 << 1)
+#define KVM_MAP_GPA_RANGE_ENC_STAT(n) ((n) << 4)
+#define KVM_MAP_GPA_RANGE_ENCRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(1)
+#define KVM_MAP_GPA_RANGE_DECRYPTED KVM_MAP_GPA_RANGE_ENC_STAT(0)
+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
Looking at the function signature it seems this routine is used for both set
and clear. Please update the comment accordingly.


+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN UINTN PhysicalAddress,
+ IN UINTN Length,
+ IN UINTN Mode
+ );
+
+#endif
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c
new file mode 100644
index 0000000000..2e73d47ee6
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/Ia32/MemEncryptHypercallLib.c
@@ -0,0 +1,37 @@
+/** @file
+
+ Secure Encrypted Virtualization (SEV) hypercall helper library
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
^^^^
2021

+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/BaseLib.h>
+
+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN PHYSICAL_ADDRESS PhysicalAddress,
+ IN UINTN Pages,
+ IN UINTN Mode
+ )
+{
+ //
+ // Memory encryption bit is not accessible in 32-bit mode
+ //
+}
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
new file mode 100644
index 0000000000..a77d58a7e6
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
@@ -0,0 +1,42 @@
+## @file
+# Library provides the hypervisor helper functions for SEV guest
+#
+# Copyright (c) 2020 Advanced Micro Devices. All rights reserved.<BR>
^^^^
2021
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#
+##
+
+[Defines]
+ INF_VERSION = 1.25
+ BASE_NAME = MemEncryptHypercallLib
+ FILE_GUID = 86f2501e-f128-45f3-91c4-3cff31656ca8
+ MODULE_TYPE = BASE
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = MemEncryptHypercallLib
+
+#
+# The following information is for reference only and not required by the build
+# tools.
+#
+# VALID_ARCHITECTURES = IA32 X64
+#
+
+[Packages]
+ MdeModulePkg/MdeModulePkg.dec
+ MdePkg/MdePkg.dec
+ UefiCpuPkg/UefiCpuPkg.dec
+ OvmfPkg/OvmfPkg.dec
+
+[Sources.X64]
+ X64/MemEncryptHypercallLib.c
+ X64/AsmHelperStub.nasm
+
+[Sources.IA32]
+ Ia32/MemEncryptHypercallLib.c
+
+[LibraryClasses]
+ BaseLib
+ DebugLib
+ VmgExitLib
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm
new file mode 100644
index 0000000000..f29b96f9b0
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/AsmHelperStub.nasm
@@ -0,0 +1,28 @@
+DEFAULT REL
+SECTION .text
+
+; VOID
+; EFIAPI
+; SetMemoryEncDecHypercall3AsmStub (
+; IN UINT HypercallNum,
+; IN INTN Arg1,
+; IN INTN Arg2,
+; IN INTN Arg3
+; );
+global ASM_PFX(SetMemoryEncDecHypercall3AsmStub)
+ASM_PFX(SetMemoryEncDecHypercall3AsmStub):
+ ; UEFI calling conventions require RBX to
+ ; be nonvolatile/callee-saved.
+ push rbx
+ ; Copy HypercallNumber to rax
+ mov rax, rcx
+ ; Copy Arg1 to the register expected by KVM
+ mov rbx, rdx
+ ; Copy Arg2 to register expected by KVM
+ mov rcx, r8
+ ; Copy Arg2 to register expected by KVM
+ mov rdx, r9
+ ; Call VMMCALL
+ vmmcall
+ pop rbx
+ ret
diff --git a/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c b/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c
new file mode 100644
index 0000000000..1c09ea012b
--- /dev/null
+++ b/OvmfPkg/Library/MemEncryptHypercallLib/X64/MemEncryptHypercallLib.c
@@ -0,0 +1,105 @@
+/** @file
+
+ Secure Encrypted Virtualization (SEV) hypercall helper library
+
+ Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
^^^^
2021
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Uefi/UefiBaseType.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/VmgExitLib.h>
+#include <Register/Amd/Ghcb.h>
+#include <Register/Amd/Msr.h>
+#include <Library/MemEncryptSevLib.h>
+#include <Library/MemEncryptHypercallLib.h>
+
+//
+// Interface exposed by the ASM implementation of the core hypercall
+//
+//
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3AsmStub (
+ IN UINTN HypercallNum,
+ IN UINTN PhysicalAddress,
+ IN UINTN Length,
+ IN UINTN Mode
+ );
+
The function signature does not match with documented signature. Fix the
SetMemoryEncDecHypercall3AsmStub() documented in AsmHelperStub.nasm to use
UINTN.


+STATIC
+VOID
+GhcbSetRegValid (
+ IN OUT GHCB *Ghcb,
+ IN GHCB_REGISTER Reg
+ )
+{
+ UINT32 RegIndex;
+ UINT32 RegBit;
+
+ RegIndex = Reg / 8;
+ RegBit = Reg & 0x07;
+
+ Ghcb->SaveArea.ValidBitmap[RegIndex] |= (1 << RegBit);
+}
+
This looks similar to VmgSetOffsetValid().

+/**
+ This hyercall is used to notify hypervisor when a page is marked as
+ 'decrypted' (i.e C-bit removed).
+Please update the comment.
+ @param[in] PhysicalAddress The physical address that is the start address
+ of a memory region.
+ @param[in] Length The length of memory region
+ @param[in] Mode SetCBit or ClearCBit
+
+**/
+
+VOID
+EFIAPI
+SetMemoryEncDecHypercall3 (
+ IN PHYSICAL_ADDRESS PhysicalAddress,
+ IN UINTN Pages,
+ IN UINTN Mode
+ )
+{
+ if (MemEncryptSevEsIsEnabled ()) {> + MSR_SEV_ES_GHCB_REGISTER Msr;
+ GHCB *Ghcb;
+ BOOLEAN InterruptState;
+ UINT64 Status;
+
+ Msr.GhcbPhysicalAddress = AsmReadMsr64 (MSR_SEV_ES_GHCB);
+ Ghcb = Msr.Ghcb;
+
+ VmgInit (Ghcb, &InterruptState);
+
+ Ghcb->SaveArea.Rax = KVM_HC_MAP_GPA_RANGE;
+ GhcbSetRegValid (Ghcb, GhcbRax);
+ Ghcb->SaveArea.Rbx = PhysicalAddress;
+ GhcbSetRegValid (Ghcb, GhcbRbx);
+ Ghcb->SaveArea.Rcx = Pages;
+ GhcbSetRegValid (Ghcb, GhcbRcx);
+ Ghcb->SaveArea.Rdx = Mode;
+ GhcbSetRegValid (Ghcb, GhcbRdx);
+ Ghcb->SaveArea.Cpl = AsmReadCs() & 0x3;
+ GhcbSetRegValid (Ghcb, GhcbCpl);
+
+ Status = VmgExit (Ghcb, SVM_EXIT_VMMCALL, 0, 0);
+ if (Status) {
+ DEBUG ((DEBUG_ERROR, "SVM_EXIT_VMMCALL failed %lx\n", Status));
You need to issue an SEV-ES guest termination vmexit followed by a deadloop to ensure
that boot does not proceed.

You probably also need to check for the RAX register for the return code.


+ }
+ VmgDone (Ghcb, InterruptState);
+ } else {
+ SetMemoryEncDecHypercall3AsmStub (
+ KVM_HC_MAP_GPA_RANGE,
+ PhysicalAddress,
+ Pages,
+ Mode
+ );
How do you know whether the hyperviosr supports the Live migration ? In other
words, is it safe to call the HC without knowing if HV supports the feature ?

Also, what will happen if we pass a bogus GPA. Does the HC return an error ?
Same as SEV-ES block, you probably need to check the RAX register for the
return code. On failure, cause an assert() and terminate the boot.


+ }
+}
diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index f53efeae79..36f1d82ce7 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -176,6 +176,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index b3662e17f2..2a743688b4 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -180,6 +180,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index 0a237a9058..eb9da51a15 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -180,6 +180,7 @@
VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf
LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
!if $(SMM_REQUIRE) == FALSE
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
!endif
diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc
index 3c1ca6bfd4..de0c052832 100644
--- a/OvmfPkg/OvmfXen.dsc
+++ b/OvmfPkg/OvmfXen.dsc
@@ -167,6 +167,7 @@
QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgDxeLib.inf
QemuLoadImageLib|OvmfPkg/Library/GenericQemuLoadImageLib/GenericQemuLoadImageLib.inf
MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
+ MemEncryptHypercallLib|OvmfPkg/Library/MemEncryptHypercallLib/MemEncryptHypercallLib.inf
LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf
FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf
Update the AmdSev.dsc to include this library.


-Brijesh


Re: [PATCH v3] UefiPayloadPkg/UefiPayloadEntry: Improve bootloader memrange parsing

Guo Dong
 

-----Original Message-----
From: Patrick Rudolph <patrick.rudolph@...>
Sent: Monday, June 21, 2021 1:10 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH v3] UefiPayloadPkg/UefiPayloadEntry: Improve bootloader
memrange parsing

Currently several DXE crash due to invalid memory resource settings.
The PciHostBridgeDxe which expects the MMCONF and PCI Aperature
to be EfiMemoryMappedIO, but currently those regions are (partly)
mapped as EfiReservedMemoryType.

coreboot and slimbootloader provide an e820 compatible memory map,
which doesn't work well with EDK2 as the e820 spec is missing MMIO regions.
In e820 'reserved' could either mean "DRAM used by boot firmware" or
"MMIO
in use and not detectable by OS".

Guess Top of lower usable DRAM (TOLUD) by walking the bootloader
provided
memory ranges. Memory types of RAM, ACPI and ACPI NVS below 4 GiB are
used
to increment TOLUD and reserved memory ranges touching TOLUD at the
base
are also assumed to be reserved DRAM, which increment TOLUD.

Then mark everything reserved below TOLUD as EfiReservedMemoryType
and
everything reserved above TOLUD as EfiMemoryMappedIO.

This fixes assertions seen in PciHostBridgeDxe.

Signed-off-by: Patrick Rudolph <patrick.rudolph@...>
---
.../UefiPayloadEntry/UefiPayloadEntry.c | 190 +++++++++++++++++-
.../UefiPayloadEntry/UefiPayloadEntry.h | 10 +
2 files changed, 197 insertions(+), 3 deletions(-)

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
index 805f5448d9..04c58f776c 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
@@ -7,10 +7,159 @@


#include "UefiPayloadEntry.h"



+STATIC UINT32 mTopOfLowerUsableDram = 0;

+

/**

Callback function to build resource descriptor HOB



This function build a HOB based on the memory map entry info.

+ It creates only EFI_RESOURCE_MEMORY_MAPPED_IO and
EFI_RESOURCE_MEMORY_RESERVED

+ resources.

+

+ @param MemoryMapEntry Memory map entry info got from
bootloader.

+ @param Params A pointer to ACPI_BOARD_INFO.

+

+ @retval EFI_SUCCESS Successfully build a HOB.

+ @retval EFI_INVALID_PARAMETER Invalid parameter provided.

+**/

+EFI_STATUS

+MemInfoCallbackMmio (

+ IN MEMROY_MAP_ENTRY *MemoryMapEntry,

+ IN VOID *Params

+ )

+{

+ EFI_PHYSICAL_ADDRESS Base;

+ EFI_RESOURCE_TYPE Type;

+ UINT64 Size;

+ EFI_RESOURCE_ATTRIBUTE_TYPE Attribue;

+ ACPI_BOARD_INFO *AcpiBoardInfo;

+

+ AcpiBoardInfo = (ACPI_BOARD_INFO *)Params;

+ if (AcpiBoardInfo == NULL) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ //

+ // Skip types already handled in MemInfoCallback

+ //

+ if (MemoryMapEntry->Type == E820_RAM || MemoryMapEntry->Type ==
E820_ACPI) {

+ return EFI_SUCCESS;

+ }

+

+ if (MemoryMapEntry->Base == AcpiBoardInfo->PcieBaseAddress) {

+ //

+ // MMCONF is always MMIO

+ //

+ Type = EFI_RESOURCE_MEMORY_MAPPED_IO;

+ } else if (MemoryMapEntry->Base < mTopOfLowerUsableDram) {

+ //

+ // It's in DRAM and thus must be reserved

+ //

+ Type = EFI_RESOURCE_MEMORY_RESERVED;

+ } else if ((MemoryMapEntry->Base < 0x100000000ULL) &&
(MemoryMapEntry->Base >= mTopOfLowerUsableDram)) {

+ //

+ // It's not in DRAM, must be MMIO

+ //

+ Type = EFI_RESOURCE_MEMORY_MAPPED_IO;

+ } else {

+ Type = EFI_RESOURCE_MEMORY_RESERVED;

+ }

+

+ Base = MemoryMapEntry->Base;

+ Size = MemoryMapEntry->Size;

+

+ Attribue = EFI_RESOURCE_ATTRIBUTE_PRESENT |

+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED |

+ EFI_RESOURCE_ATTRIBUTE_TESTED |

+ EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |

+ EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |

+ EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |

+ EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE;

+

+ BuildResourceDescriptorHob (Type, Attribue,
(EFI_PHYSICAL_ADDRESS)Base, Size);

+ DEBUG ((DEBUG_INFO , "buildhob: base = 0x%lx, size = 0x%lx, type =
0x%x\n", Base, Size, Type));

+

+ if (MemoryMapEntry->Type == E820_UNUSABLE ||

+ MemoryMapEntry->Type == E820_DISABLED) {

+ BuildMemoryAllocationHob (Base, Size, EfiUnusableMemory);

+ } else if (MemoryMapEntry->Type == E820_PMEM) {

+ BuildMemoryAllocationHob (Base, Size, EfiPersistentMemory);

+ }

+

+ return EFI_SUCCESS;

+}

+

+

+/**

+ Callback function to find TOLUD (Top of Lower Usable DRAM)

+

+ Estimate where TOLUD (Top of Lower Usable DRAM) resides. The exact
position

+ would require platform specific code.

+

+ @param MemoryMapEntry Memory map entry info got from
bootloader.

+ @param Params Not used for now.

+

+ @retval EFI_SUCCESS Successfully updated
mTopOfLowerUsableDram.

+**/

+EFI_STATUS

+FindToludCallback (

+ IN MEMROY_MAP_ENTRY *MemoryMapEntry,

+ IN VOID *Params

+ )

+{

+ //

+ // This code assumes that the memory map on this x86 machine below
4GiB is continous

+ // until TOLUD. In addition it assumes that the bootloader provided
memory tables have

+ // no "holes" and thus the first memory range not covered by e820 marks
the end of

+ // usable DRAM. In addition it's assumed that every reserved memory
region touching

+ // usable RAM is also covering DRAM, everything else that is marked
reserved thus must be

+ // MMIO not detectable by bootloader/OS

+ //

+

+ //

+ // Skip memory types not RAM or reserved

+ //

+ if ((MemoryMapEntry->Type == E820_UNUSABLE) || (MemoryMapEntry-
Type == E820_DISABLED) ||
+ (MemoryMapEntry->Type == E820_PMEM)) {

+ return EFI_SUCCESS;

+ }

+

+ //

+ // Skip resources above 4GiB

+ //

+ if ((MemoryMapEntry->Base + MemoryMapEntry->Size) >
0x100000000ULL) {

+ return EFI_SUCCESS;

+ }

+

+ if ((MemoryMapEntry->Type == E820_RAM) || (MemoryMapEntry->Type
== E820_ACPI) ||

+ (MemoryMapEntry->Type == E820_NVS)) {

+ //

+ // It's usable DRAM. Update TOLUD.

+ //

+ if (mTopOfLowerUsableDram < (MemoryMapEntry->Base +
MemoryMapEntry->Size)) {

+ mTopOfLowerUsableDram = (UINT32)(MemoryMapEntry->Base +
MemoryMapEntry->Size);

+ }

+ } else {

+ //

+ // It might be 'reserved DRAM' or 'MMIO'.

+ //

+ // If it touches usable DRAM at Base assume it's DRAM as well,

+ // as it could be bootloader installed tables, TSEG, GTT, ...

+ //

+ if (mTopOfLowerUsableDram == MemoryMapEntry->Base) {

+ mTopOfLowerUsableDram = (UINT32)(MemoryMapEntry->Base +
MemoryMapEntry->Size);

+ }

+ }

+

+ return EFI_SUCCESS;

+}

+

+

+/**

+ Callback function to build resource descriptor HOB

+

+ This function build a HOB based on the memory map entry info.

+ Only add EFI_RESOURCE_SYSTEM_MEMORY.



@param MemoryMapEntry Memory map entry info got from
bootloader.

@param Params Not used for now.

@@ -28,7 +177,16 @@ MemInfoCallback (
UINT64 Size;

EFI_RESOURCE_ATTRIBUTE_TYPE Attribue;



- Type = (MemoryMapEntry->Type == 1) ?
EFI_RESOURCE_SYSTEM_MEMORY : EFI_RESOURCE_MEMORY_RESERVED;

+ //

+ // Skip everything not known to be usable DRAM.

+ // It will be added later.

+ //

+ if ((MemoryMapEntry->Type != E820_RAM) && (MemoryMapEntry-
Type != E820_ACPI) &&
+ (MemoryMapEntry->Type != E820_NVS)) {

+ return RETURN_SUCCESS;

+ }

+

+ Type = EFI_RESOURCE_SYSTEM_MEMORY;

Base = MemoryMapEntry->Base;

Size = MemoryMapEntry->Size;



@@ -40,7 +198,7 @@ MemInfoCallback (
EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |

EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE;



- if (Base >= BASE_4GB ) {

+ if (Base >= BASE_4GB) {

// Remove tested attribute to avoid DXE core to dispatch driver to memory
above 4GB

Attribue &= ~EFI_RESOURCE_ATTRIBUTE_TESTED;

}

@@ -48,6 +206,12 @@ MemInfoCallback (
BuildResourceDescriptorHob (Type, Attribue,
(EFI_PHYSICAL_ADDRESS)Base, Size);

DEBUG ((DEBUG_INFO , "buildhob: base = 0x%lx, size = 0x%lx, type =
0x%x\n", Base, Size, Type));



+ if (MemoryMapEntry->Type == E820_ACPI) {

+ BuildMemoryAllocationHob (Base, Size, EfiACPIReclaimMemory);

+ } else if (MemoryMapEntry->Type == E820_NVS) {

+ BuildMemoryAllocationHob (Base, Size, EfiACPIMemoryNVS);

+ }

+

return RETURN_SUCCESS;

}



@@ -236,8 +400,19 @@ BuildHobFromBl (
EFI_PEI_GRAPHICS_DEVICE_INFO_HOB *NewGfxDeviceInfo;



//

- // Parse memory info and build memory HOBs

+ // First find TOLUD

+ //

+ DEBUG ((DEBUG_INFO , "Guessing Top of Lower Usable DRAM:\n"));

+ Status = ParseMemoryInfo (FindToludCallback, NULL);

+ if (EFI_ERROR(Status)) {

+ return Status;

+ }

+ DEBUG ((DEBUG_INFO , "Assuming TOLUD = 0x%x\n",
mTopOfLowerUsableDram));

+

+ //

+ // Parse memory info and build memory HOBs for Usable RAM

//

+ DEBUG ((DEBUG_INFO , "Building ResourceDescriptorHobs for usable
memory:\n"));

Status = ParseMemoryInfo (MemInfoCallback, NULL);

if (EFI_ERROR(Status)) {

return Status;

@@ -289,6 +464,15 @@ BuildHobFromBl (
DEBUG ((DEBUG_INFO, "Create acpi board info guid hob\n"));

}



+ //

+ // Parse memory info and build memory HOBs for reserved DRAM and
MMIO

+ //

+ DEBUG ((DEBUG_INFO , "Building ResourceDescriptorHobs for reserved
memory:\n"));

+ Status = ParseMemoryInfo (MemInfoCallbackMmio, &AcpiBoardInfo);

+ if (EFI_ERROR(Status)) {

+ return Status;

+ }

+

//

// Parse platform specific information.

//

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
index 2c84d6ed53..4fd50e47cd 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
@@ -38,6 +38,16 @@
#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \

((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) &
((Alignment) - 1)))



+

+#define E820_RAM 1

+#define E820_RESERVED 2

+#define E820_ACPI 3

+#define E820_NVS 4

+#define E820_UNUSABLE 5

+#define E820_DISABLED 6

+#define E820_PMEM 7

+#define E820_UNDEFINED 8

+

/**

Auto-generated function that calls the library constructors for all of the
module's

dependent libraries.

--
2.30.2


Re: [PATCH] OvmfPkg/Bhyve: clean up TPM_ENABLE remnants

Laszlo Ersek
 

On 06/12/21 22:43, Rebecca Cran wrote:
TPM support hasn't been tested and any lines in the .dsc and .fdf files
that appear to show support are bogus. Remove them.

This fixes https://bugzilla.tianocore.org/show_bug.cgi?id=3354 .

Signed-off-by: Rebecca Cran <rebecca@...>
---
OvmfPkg/Bhyve/BhyveX64.dsc | 64 --------------------
OvmfPkg/Bhyve/BhyveX64.fdf | 15 -----
2 files changed, 79 deletions(-)
I'll try to review this patch soon; I'm dealing with the usual post-PTO
email apocalypse.

Thanks,
Laszlo


Re: [PATCH v3] UefiPayloadPkg/UefiPayloadEntry: Improve bootloader memrange parsing

Guo Dong
 

Reviewed-by: Guo Dong <guo.dong@...>

-----Original Message-----
From: Patrick Rudolph <patrick.rudolph@...>
Sent: Monday, June 21, 2021 1:10 AM
To: devel@edk2.groups.io
Cc: Ma, Maurice <maurice.ma@...>; Dong, Guo
<guo.dong@...>; You, Benjamin <benjamin.you@...>
Subject: [PATCH v3] UefiPayloadPkg/UefiPayloadEntry: Improve bootloader
memrange parsing

Currently several DXE crash due to invalid memory resource settings.
The PciHostBridgeDxe which expects the MMCONF and PCI Aperature
to be EfiMemoryMappedIO, but currently those regions are (partly)
mapped as EfiReservedMemoryType.

coreboot and slimbootloader provide an e820 compatible memory map,
which doesn't work well with EDK2 as the e820 spec is missing MMIO regions.
In e820 'reserved' could either mean "DRAM used by boot firmware" or
"MMIO
in use and not detectable by OS".

Guess Top of lower usable DRAM (TOLUD) by walking the bootloader
provided
memory ranges. Memory types of RAM, ACPI and ACPI NVS below 4 GiB are
used
to increment TOLUD and reserved memory ranges touching TOLUD at the
base
are also assumed to be reserved DRAM, which increment TOLUD.

Then mark everything reserved below TOLUD as EfiReservedMemoryType
and
everything reserved above TOLUD as EfiMemoryMappedIO.

This fixes assertions seen in PciHostBridgeDxe.

Signed-off-by: Patrick Rudolph <patrick.rudolph@...>
---
.../UefiPayloadEntry/UefiPayloadEntry.c | 190 +++++++++++++++++-
.../UefiPayloadEntry/UefiPayloadEntry.h | 10 +
2 files changed, 197 insertions(+), 3 deletions(-)

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
index 805f5448d9..04c58f776c 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c
@@ -7,10 +7,159 @@


#include "UefiPayloadEntry.h"



+STATIC UINT32 mTopOfLowerUsableDram = 0;

+

/**

Callback function to build resource descriptor HOB



This function build a HOB based on the memory map entry info.

+ It creates only EFI_RESOURCE_MEMORY_MAPPED_IO and
EFI_RESOURCE_MEMORY_RESERVED

+ resources.

+

+ @param MemoryMapEntry Memory map entry info got from
bootloader.

+ @param Params A pointer to ACPI_BOARD_INFO.

+

+ @retval EFI_SUCCESS Successfully build a HOB.

+ @retval EFI_INVALID_PARAMETER Invalid parameter provided.

+**/

+EFI_STATUS

+MemInfoCallbackMmio (

+ IN MEMROY_MAP_ENTRY *MemoryMapEntry,

+ IN VOID *Params

+ )

+{

+ EFI_PHYSICAL_ADDRESS Base;

+ EFI_RESOURCE_TYPE Type;

+ UINT64 Size;

+ EFI_RESOURCE_ATTRIBUTE_TYPE Attribue;

+ ACPI_BOARD_INFO *AcpiBoardInfo;

+

+ AcpiBoardInfo = (ACPI_BOARD_INFO *)Params;

+ if (AcpiBoardInfo == NULL) {

+ return EFI_INVALID_PARAMETER;

+ }

+

+ //

+ // Skip types already handled in MemInfoCallback

+ //

+ if (MemoryMapEntry->Type == E820_RAM || MemoryMapEntry->Type ==
E820_ACPI) {

+ return EFI_SUCCESS;

+ }

+

+ if (MemoryMapEntry->Base == AcpiBoardInfo->PcieBaseAddress) {

+ //

+ // MMCONF is always MMIO

+ //

+ Type = EFI_RESOURCE_MEMORY_MAPPED_IO;

+ } else if (MemoryMapEntry->Base < mTopOfLowerUsableDram) {

+ //

+ // It's in DRAM and thus must be reserved

+ //

+ Type = EFI_RESOURCE_MEMORY_RESERVED;

+ } else if ((MemoryMapEntry->Base < 0x100000000ULL) &&
(MemoryMapEntry->Base >= mTopOfLowerUsableDram)) {

+ //

+ // It's not in DRAM, must be MMIO

+ //

+ Type = EFI_RESOURCE_MEMORY_MAPPED_IO;

+ } else {

+ Type = EFI_RESOURCE_MEMORY_RESERVED;

+ }

+

+ Base = MemoryMapEntry->Base;

+ Size = MemoryMapEntry->Size;

+

+ Attribue = EFI_RESOURCE_ATTRIBUTE_PRESENT |

+ EFI_RESOURCE_ATTRIBUTE_INITIALIZED |

+ EFI_RESOURCE_ATTRIBUTE_TESTED |

+ EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |

+ EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |

+ EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |

+ EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE;

+

+ BuildResourceDescriptorHob (Type, Attribue,
(EFI_PHYSICAL_ADDRESS)Base, Size);

+ DEBUG ((DEBUG_INFO , "buildhob: base = 0x%lx, size = 0x%lx, type =
0x%x\n", Base, Size, Type));

+

+ if (MemoryMapEntry->Type == E820_UNUSABLE ||

+ MemoryMapEntry->Type == E820_DISABLED) {

+ BuildMemoryAllocationHob (Base, Size, EfiUnusableMemory);

+ } else if (MemoryMapEntry->Type == E820_PMEM) {

+ BuildMemoryAllocationHob (Base, Size, EfiPersistentMemory);

+ }

+

+ return EFI_SUCCESS;

+}

+

+

+/**

+ Callback function to find TOLUD (Top of Lower Usable DRAM)

+

+ Estimate where TOLUD (Top of Lower Usable DRAM) resides. The exact
position

+ would require platform specific code.

+

+ @param MemoryMapEntry Memory map entry info got from
bootloader.

+ @param Params Not used for now.

+

+ @retval EFI_SUCCESS Successfully updated
mTopOfLowerUsableDram.

+**/

+EFI_STATUS

+FindToludCallback (

+ IN MEMROY_MAP_ENTRY *MemoryMapEntry,

+ IN VOID *Params

+ )

+{

+ //

+ // This code assumes that the memory map on this x86 machine below
4GiB is continous

+ // until TOLUD. In addition it assumes that the bootloader provided
memory tables have

+ // no "holes" and thus the first memory range not covered by e820 marks
the end of

+ // usable DRAM. In addition it's assumed that every reserved memory
region touching

+ // usable RAM is also covering DRAM, everything else that is marked
reserved thus must be

+ // MMIO not detectable by bootloader/OS

+ //

+

+ //

+ // Skip memory types not RAM or reserved

+ //

+ if ((MemoryMapEntry->Type == E820_UNUSABLE) || (MemoryMapEntry-
Type == E820_DISABLED) ||
+ (MemoryMapEntry->Type == E820_PMEM)) {

+ return EFI_SUCCESS;

+ }

+

+ //

+ // Skip resources above 4GiB

+ //

+ if ((MemoryMapEntry->Base + MemoryMapEntry->Size) >
0x100000000ULL) {

+ return EFI_SUCCESS;

+ }

+

+ if ((MemoryMapEntry->Type == E820_RAM) || (MemoryMapEntry->Type
== E820_ACPI) ||

+ (MemoryMapEntry->Type == E820_NVS)) {

+ //

+ // It's usable DRAM. Update TOLUD.

+ //

+ if (mTopOfLowerUsableDram < (MemoryMapEntry->Base +
MemoryMapEntry->Size)) {

+ mTopOfLowerUsableDram = (UINT32)(MemoryMapEntry->Base +
MemoryMapEntry->Size);

+ }

+ } else {

+ //

+ // It might be 'reserved DRAM' or 'MMIO'.

+ //

+ // If it touches usable DRAM at Base assume it's DRAM as well,

+ // as it could be bootloader installed tables, TSEG, GTT, ...

+ //

+ if (mTopOfLowerUsableDram == MemoryMapEntry->Base) {

+ mTopOfLowerUsableDram = (UINT32)(MemoryMapEntry->Base +
MemoryMapEntry->Size);

+ }

+ }

+

+ return EFI_SUCCESS;

+}

+

+

+/**

+ Callback function to build resource descriptor HOB

+

+ This function build a HOB based on the memory map entry info.

+ Only add EFI_RESOURCE_SYSTEM_MEMORY.



@param MemoryMapEntry Memory map entry info got from
bootloader.

@param Params Not used for now.

@@ -28,7 +177,16 @@ MemInfoCallback (
UINT64 Size;

EFI_RESOURCE_ATTRIBUTE_TYPE Attribue;



- Type = (MemoryMapEntry->Type == 1) ?
EFI_RESOURCE_SYSTEM_MEMORY : EFI_RESOURCE_MEMORY_RESERVED;

+ //

+ // Skip everything not known to be usable DRAM.

+ // It will be added later.

+ //

+ if ((MemoryMapEntry->Type != E820_RAM) && (MemoryMapEntry-
Type != E820_ACPI) &&
+ (MemoryMapEntry->Type != E820_NVS)) {

+ return RETURN_SUCCESS;

+ }

+

+ Type = EFI_RESOURCE_SYSTEM_MEMORY;

Base = MemoryMapEntry->Base;

Size = MemoryMapEntry->Size;



@@ -40,7 +198,7 @@ MemInfoCallback (
EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |

EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE;



- if (Base >= BASE_4GB ) {

+ if (Base >= BASE_4GB) {

// Remove tested attribute to avoid DXE core to dispatch driver to memory
above 4GB

Attribue &= ~EFI_RESOURCE_ATTRIBUTE_TESTED;

}

@@ -48,6 +206,12 @@ MemInfoCallback (
BuildResourceDescriptorHob (Type, Attribue,
(EFI_PHYSICAL_ADDRESS)Base, Size);

DEBUG ((DEBUG_INFO , "buildhob: base = 0x%lx, size = 0x%lx, type =
0x%x\n", Base, Size, Type));



+ if (MemoryMapEntry->Type == E820_ACPI) {

+ BuildMemoryAllocationHob (Base, Size, EfiACPIReclaimMemory);

+ } else if (MemoryMapEntry->Type == E820_NVS) {

+ BuildMemoryAllocationHob (Base, Size, EfiACPIMemoryNVS);

+ }

+

return RETURN_SUCCESS;

}



@@ -236,8 +400,19 @@ BuildHobFromBl (
EFI_PEI_GRAPHICS_DEVICE_INFO_HOB *NewGfxDeviceInfo;



//

- // Parse memory info and build memory HOBs

+ // First find TOLUD

+ //

+ DEBUG ((DEBUG_INFO , "Guessing Top of Lower Usable DRAM:\n"));

+ Status = ParseMemoryInfo (FindToludCallback, NULL);

+ if (EFI_ERROR(Status)) {

+ return Status;

+ }

+ DEBUG ((DEBUG_INFO , "Assuming TOLUD = 0x%x\n",
mTopOfLowerUsableDram));

+

+ //

+ // Parse memory info and build memory HOBs for Usable RAM

//

+ DEBUG ((DEBUG_INFO , "Building ResourceDescriptorHobs for usable
memory:\n"));

Status = ParseMemoryInfo (MemInfoCallback, NULL);

if (EFI_ERROR(Status)) {

return Status;

@@ -289,6 +464,15 @@ BuildHobFromBl (
DEBUG ((DEBUG_INFO, "Create acpi board info guid hob\n"));

}



+ //

+ // Parse memory info and build memory HOBs for reserved DRAM and
MMIO

+ //

+ DEBUG ((DEBUG_INFO , "Building ResourceDescriptorHobs for reserved
memory:\n"));

+ Status = ParseMemoryInfo (MemInfoCallbackMmio, &AcpiBoardInfo);

+ if (EFI_ERROR(Status)) {

+ return Status;

+ }

+

//

// Parse platform specific information.

//

diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
index 2c84d6ed53..4fd50e47cd 100644
--- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
+++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h
@@ -38,6 +38,16 @@
#define GET_OCCUPIED_SIZE(ActualSize, Alignment) \

((ActualSize) + (((Alignment) - ((ActualSize) & ((Alignment) - 1))) &
((Alignment) - 1)))



+

+#define E820_RAM 1

+#define E820_RESERVED 2

+#define E820_ACPI 3

+#define E820_NVS 4

+#define E820_UNUSABLE 5

+#define E820_DISABLED 6

+#define E820_PMEM 7

+#define E820_UNDEFINED 8

+

/**

Auto-generated function that calls the library constructors for all of the
module's

dependent libraries.

--
2.30.2

19841 - 19860 of 96641