Skip to content

Commit e4da6c9

Browse files
committed
OvmfPkg/Library/TdxStartupLib: Add lazy memory accept to the TDVF.
Add unaccepted memory type to several structures and macro definitions to indicate the unaccepted memory. SEC Phase: Modify the Hob process function to partially accept memory. Modify the Hob transfer function to build different types of system memory Hobs for DXE phase, system memory is split to type of system memory and unaccepted memory. DXE Phase: Add case of unaccepted memory in the GCD memory service process and memory map.
1 parent ba06323 commit e4da6c9

10 files changed

Lines changed: 161 additions & 43 deletions

File tree

MdeModulePkg/Core/Dxe/Gcd/Gcd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 *mGcdMemoryTypeNames[] = {
115115
"MMIO ", // EfiGcdMemoryTypeMemoryMappedIo
116116
"PersisMem", // EfiGcdMemoryTypePersistent
117117
"MoreRelia", // EfiGcdMemoryTypeMoreReliable
118+
"Unaccepte", // EfiGcdMemoryTypeUnaccepted
118119
"Unknown " // EfiGcdMemoryTypeMaximum
119120
};
120121

@@ -2563,6 +2564,9 @@ CoreInitializeGcdServices (
25632564
case EFI_RESOURCE_MEMORY_RESERVED:
25642565
GcdMemoryType = EfiGcdMemoryTypeReserved;
25652566
break;
2567+
case EFI_RESOURCE_MEMORY_UNACCEPTED:
2568+
GcdMemoryType = EfiGcdMemoryTypeUnaccepted;
2569+
break;
25662570
case EFI_RESOURCE_IO:
25672571
GcdIoType = EfiGcdIoTypeIo;
25682572
break;

MdeModulePkg/Core/Dxe/Mem/Page.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ EFI_MEMORY_TYPE_STATISTICS mMemoryTypeStatistics[EfiMaxMemoryType + 1] = {
6161
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiMemoryMappedIOPortSpace
6262
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, TRUE }, // EfiPalCode
6363
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE }, // EfiPersistentMemory
64+
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, TRUE, FALSE }, // EfiUnacceptedMemory
6465
{ 0, MAX_ALLOC_ADDRESS, 0, 0, EfiMaxMemoryType, FALSE, FALSE } // EfiMaxMemoryType
6566
};
6667

@@ -83,6 +84,7 @@ EFI_MEMORY_TYPE_INFORMATION gMemoryTypeInformation[EfiMaxMemoryType + 1] = {
8384
{ EfiMemoryMappedIOPortSpace, 0 },
8485
{ EfiPalCode, 0 },
8586
{ EfiPersistentMemory, 0 },
87+
{ EfiGcdMemoryTypeUnaccepted, 0 },
8688
{ EfiMaxMemoryType, 0 }
8789
};
8890
//
@@ -1912,6 +1914,32 @@ CoreGetMemoryMap (
19121914
//
19131915
MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);
19141916
}
1917+
1918+
if (MergeGcdMapEntry.GcdMemoryType == EfiGcdMemoryTypeUnaccepted) {
1919+
//
1920+
// Page Align GCD range is required. When it is converted to EFI_MEMORY_DESCRIPTOR,
1921+
// it will be recorded as page PhysicalStart and NumberOfPages.
1922+
//
1923+
ASSERT ((MergeGcdMapEntry.BaseAddress & EFI_PAGE_MASK) == 0);
1924+
ASSERT (((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1) & EFI_PAGE_MASK) == 0);
1925+
1926+
//
1927+
// Create EFI_MEMORY_DESCRIPTOR for every Unaccepted GCD entries
1928+
//
1929+
MemoryMap->PhysicalStart = MergeGcdMapEntry.BaseAddress;
1930+
MemoryMap->VirtualStart = 0;
1931+
MemoryMap->NumberOfPages = RShiftU64 ((MergeGcdMapEntry.EndAddress - MergeGcdMapEntry.BaseAddress + 1), EFI_PAGE_SHIFT);
1932+
MemoryMap->Attribute = MergeGcdMapEntry.Attributes |
1933+
(MergeGcdMapEntry.Capabilities & (EFI_MEMORY_RP | EFI_MEMORY_WP | EFI_MEMORY_XP | EFI_MEMORY_RO |
1934+
EFI_MEMORY_UC | EFI_MEMORY_UCE | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB));
1935+
MemoryMap->Type = EfiUnacceptedMemory;
1936+
1937+
//
1938+
// Check to see if the new Memory Map Descriptor can be merged with an
1939+
// existing descriptor if they are adjacent and have the same attributes
1940+
//
1941+
MemoryMap = MergeMemoryMapDescriptor (MemoryMapStart, MemoryMap, Size);
1942+
}
19151943
if (Link == &mGcdMemorySpaceMap) {
19161944
//
19171945
// break loop when arrive at head.

MdePkg/Include/Pi/PiDxeCis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef enum {
5656
/// system. If all memory has the same reliability, then this bit is not used.
5757
///
5858
EfiGcdMemoryTypeMoreReliable,
59+
EfiGcdMemoryTypeUnaccepted,
5960
EfiGcdMemoryTypeMaximum
6061
} EFI_GCD_MEMORY_TYPE;
6162

MdePkg/Include/Pi/PiHob.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ typedef UINT32 EFI_RESOURCE_TYPE;
234234
#define EFI_RESOURCE_MEMORY_MAPPED_IO_PORT 0x00000004
235235
#define EFI_RESOURCE_MEMORY_RESERVED 0x00000005
236236
#define EFI_RESOURCE_IO_RESERVED 0x00000006
237-
#define EFI_RESOURCE_MAX_MEMORY_TYPE 0x00000007
237+
#define EFI_RESOURCE_MEMORY_UNACCEPTED 0x00000007
238+
#define EFI_RESOURCE_MAX_MEMORY_TYPE 0x00000008
238239

239240
///
240241
/// A type of recount attribute type.

MdePkg/Include/Uefi/UefiMultiPhase.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ typedef enum {
103103
/// however it happens to also support byte-addressable non-volatility.
104104
///
105105
EfiPersistentMemory,
106+
///
107+
/// A memory region that describes system memory that has not been accepted
108+
/// by a corresponding call to the underlying isolation architecture.
109+
///
110+
EfiUnacceptedMemory,
106111
EfiMaxMemoryType
107112
} EFI_MEMORY_TYPE;
108113

OvmfPkg/Library/TdxStartupLib/Hob.c

Lines changed: 103 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <IndustryStandard/UefiTcgPlatform.h>
2020
#include "TdxStartupInternal.h"
2121

22+
UINT64 mTdxAcceptMemSize = 0;
23+
2224
VOID
2325
EFIAPI
2426
DEBUG_HOBLIST (
@@ -280,17 +282,27 @@ ProcessHobList (
280282
{
281283
EFI_PEI_HOB_POINTERS Hob;
282284
EFI_PHYSICAL_ADDRESS PhysicalEnd;
283-
EFI_PHYSICAL_ADDRESS PhysicalStart;
284-
UINT64 Length;
285-
EFI_HOB_RESOURCE_DESCRIPTOR *LowMemoryResource = NULL;
285+
UINT64 ResourceLength;
286+
UINT64 AccumulateAccepted;
287+
EFI_PHYSICAL_ADDRESS LowMemoryStart;
288+
UINT64 LowMemoryLength;
286289

287290
ASSERT (VmmHobList != NULL);
291+
292+
AccumulateAccepted = 0;
288293
Hob.Raw = (UINT8 *) VmmHobList;
294+
LowMemoryLength = 0;
295+
296+
mTdxAcceptMemSize = FixedPcdGet64(PcdTdxAcceptPartialMemorySize);
297+
if (mTdxAcceptMemSize <= 0) {
298+
mTdxAcceptMemSize = ~(UINT64) 0;;
299+
}
300+
DEBUG ((DEBUG_INFO, "mTdxAcceptMemSize: 0x%llx\n", mTdxAcceptMemSize));
289301

290302
//
291303
// Parse the HOB list until end of list or matching type is found.
292304
//
293-
while (!END_OF_HOB_LIST (Hob)) {
305+
while (!END_OF_HOB_LIST (Hob) && AccumulateAccepted < mTdxAcceptMemSize) {
294306

295307
if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
296308
DEBUG ((DEBUG_INFO, "\nResourceType: 0x%x\n", Hob.ResourceDescriptor->ResourceType));
@@ -311,43 +323,49 @@ ProcessHobList (
311323
DEBUG ((DEBUG_INFO, "Owner: %g\n\n", &Hob.ResourceDescriptor->Owner));
312324

313325
PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;
314-
326+
ResourceLength = Hob.ResourceDescriptor->ResourceLength;
327+
328+
if (AccumulateAccepted + ResourceLength > mTdxAcceptMemSize) {
329+
//
330+
// If the memory can't be accepted completely, accept the part of it to meet the
331+
// TDX_PARTIAL_ACCEPTED_MEM_SIZE.
332+
//
333+
ResourceLength = mTdxAcceptMemSize - AccumulateAccepted;
334+
PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + ResourceLength;
335+
}
315336
if (PhysicalEnd <= BASE_4GB) {
316-
if ((LowMemoryResource == NULL) || (Hob.ResourceDescriptor->ResourceLength > LowMemoryResource->ResourceLength)) {
317-
LowMemoryResource = Hob.ResourceDescriptor;
337+
if (ResourceLength > LowMemoryLength) {
338+
LowMemoryStart = Hob.ResourceDescriptor->PhysicalStart;
339+
LowMemoryLength = ResourceLength;
318340
}
319341
}
320-
342+
DEBUG ((DEBUG_INFO, "Accept Start and End: %x, %x\n", Hob.ResourceDescriptor->PhysicalStart, PhysicalEnd));
321343
MpAcceptMemoryResourceRange (
322344
Hob.ResourceDescriptor->PhysicalStart,
323345
PhysicalEnd);
346+
347+
AccumulateAccepted += PhysicalEnd - Hob.ResourceDescriptor->PhysicalStart;
324348
}
325349
}
326350
Hob.Raw = GET_NEXT_HOB (Hob);
327351
}
328352

329-
ASSERT (LowMemoryResource != NULL);
330-
331-
PhysicalStart = LowMemoryResource->PhysicalStart;
332-
Length = LowMemoryResource->ResourceLength;
333-
334353
//
335354
// HobLib doesn't like HobStart at address 0 so adjust is needed
336355
//
337-
if (PhysicalStart == 0) {
338-
PhysicalStart += EFI_PAGE_SIZE;
339-
Length -= EFI_PAGE_SIZE;
356+
if (LowMemoryStart == 0) {
357+
LowMemoryStart += EFI_PAGE_SIZE;
358+
LowMemoryLength -= EFI_PAGE_SIZE;
340359
}
341360

342-
343361
HobConstructor (
344-
(VOID *) PhysicalStart,
345-
Length,
346-
(VOID *) PhysicalStart,
347-
(VOID *) (PhysicalStart + Length)
362+
(VOID *) LowMemoryStart,
363+
LowMemoryLength,
364+
(VOID *) LowMemoryStart,
365+
(VOID *) (LowMemoryStart + LowMemoryLength)
348366
);
349367

350-
PrePeiSetHobList ((VOID *)(UINT64)PhysicalStart);
368+
PrePeiSetHobList ((VOID *)(UINT64)LowMemoryStart);
351369
}
352370

353371
/**
@@ -363,28 +381,78 @@ TransferHobList (
363381
)
364382
{
365383
EFI_PEI_HOB_POINTERS Hob;
384+
EFI_RESOURCE_TYPE ResourceType;
366385
EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute;
367-
EFI_PHYSICAL_ADDRESS PhysicalEnd;
386+
EFI_PHYSICAL_ADDRESS PhysicalStart;
387+
UINT64 ResourceLength;
388+
UINT64 AccumulateAccepted;
389+
390+
Hob.Raw = (UINT8 *) VmmHobList;
391+
AccumulateAccepted = 0;
368392

369-
Hob.Raw = (UINT8 *) VmmHobList;
370393
while (!END_OF_HOB_LIST (Hob)) {
371394
switch (Hob.Header->HobType) {
372395
case EFI_HOB_TYPE_RESOURCE_DESCRIPTOR:
373396
ResourceAttribute = Hob.ResourceDescriptor->ResourceAttribute;
374-
PhysicalEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;
375-
376-
//
377-
// We mark each resource that we issue AcceptPage to with EFI_RESOURCE_SYSTEM_MEMORY
378-
//
379-
if ((Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&
380-
(PhysicalEnd <= BASE_4GB)) {
381-
ResourceAttribute |= EFI_RESOURCE_ATTRIBUTE_ENCRYPTED;
397+
ResourceLength = Hob.ResourceDescriptor->ResourceLength;
398+
ResourceType = Hob.ResourceDescriptor->ResourceType;
399+
PhysicalStart = Hob.ResourceDescriptor->PhysicalStart;
400+
401+
if (ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
402+
ResourceAttribute |= EFI_RESOURCE_ATTRIBUTE_PRESENT | EFI_RESOURCE_ATTRIBUTE_INITIALIZED;
403+
404+
//
405+
// Set type of systme memory less than TDX_PARTIAL_ACCEPTED_MEM_SIZE to
406+
// EFI_RESOURCE_SYSTEM_MEMORY and set other to EFI_RESOURCE_MEMORY_UNACCEPTED.
407+
//
408+
if (AccumulateAccepted >= mTdxAcceptMemSize) {
409+
ResourceType = EFI_RESOURCE_MEMORY_UNACCEPTED;
410+
ResourceAttribute &= ~(EFI_RESOURCE_ATTRIBUTE_TESTED | EFI_RESOURCE_ATTRIBUTE_ENCRYPTED);
411+
} else {
412+
//
413+
// Judge if the whole memory is accepted.
414+
//
415+
if (AccumulateAccepted + ResourceLength <= mTdxAcceptMemSize) {
416+
AccumulateAccepted += ResourceLength;
417+
ResourceAttribute |= EFI_RESOURCE_ATTRIBUTE_TESTED;
418+
if (PhysicalStart + ResourceLength <= BASE_4GB) {
419+
ResourceAttribute |= EFI_RESOURCE_ATTRIBUTE_ENCRYPTED;
420+
}
421+
} else {
422+
//
423+
// Set the resouce type, attribute and memory range of the the accepted part
424+
// of the memory.
425+
//
426+
ResourceType = EFI_RESOURCE_SYSTEM_MEMORY;
427+
ResourceLength = mTdxAcceptMemSize - AccumulateAccepted;
428+
429+
ResourceAttribute |= EFI_RESOURCE_ATTRIBUTE_TESTED;
430+
if (PhysicalStart + ResourceLength <= BASE_4GB) {
431+
ResourceAttribute |= EFI_RESOURCE_ATTRIBUTE_ENCRYPTED;
432+
}
433+
BuildResourceDescriptorHob (
434+
ResourceType,
435+
ResourceAttribute,
436+
PhysicalStart,
437+
ResourceLength);
438+
AccumulateAccepted += ResourceLength;
439+
440+
//
441+
// Transfer the other part to the unaccepted memory.
442+
//
443+
PhysicalStart = PhysicalStart + ResourceLength;
444+
ResourceLength = Hob.ResourceDescriptor->ResourceLength - ResourceLength;
445+
ResourceType = EFI_RESOURCE_MEMORY_UNACCEPTED;
446+
ResourceAttribute &= ~(EFI_RESOURCE_ATTRIBUTE_TESTED | EFI_RESOURCE_ATTRIBUTE_ENCRYPTED);
447+
}
448+
}
382449
}
450+
383451
BuildResourceDescriptorHob (
384-
Hob.ResourceDescriptor->ResourceType,
452+
ResourceType,
385453
ResourceAttribute,
386-
Hob.ResourceDescriptor->PhysicalStart,
387-
Hob.ResourceDescriptor->ResourceLength);
454+
PhysicalStart,
455+
ResourceLength);
388456
break;
389457
case EFI_HOB_TYPE_MEMORY_ALLOCATION:
390458
BuildMemoryAllocationHob (

OvmfPkg/Library/TdxStartupLib/TdxStartupLib.inf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,14 @@
8484
gUefiOvmfPkgTokenSpaceGuid.PcdBfvRawDataSize
8585
gUefiOvmfPkgTokenSpaceGuid.PcdUseTdxEmulation
8686
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageChunkSize
87-
gUefiOvmfPkgTokenSpaceGuid.PcdTdxSetNxForStack
88-
gUefiOvmfPkgTokenSpaceGuid.PcdTdxPteMemoryEncryptionAddressOrMask
87+
gUefiOvmfPkgTokenSpaceGuid.PcdTdxSetNxForStack
88+
gUefiOvmfPkgTokenSpaceGuid.PcdTdxPteMemoryEncryptionAddressOrMask
89+
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPartialMemorySize
8990

90-
//
91-
// TODO check these PCDs' impact on Ovmf
92-
//
93-
gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack ## CONSUMES
91+
//
92+
// TODO check these PCDs' impact on Ovmf
93+
//
94+
gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack ## CONSUMES
9495
gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplBuildPageTables ## CONSUMES
9596
gEfiMdeModulePkgTokenSpaceGuid.PcdCpuStackGuard ## CONSUMES
9697
gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable ## SOMETIMES_CONSUMES

OvmfPkg/OvmfPkg.dec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@
342342
gUefiOvmfPkgTokenSpaceGuid.PcdTdxSetNxForStack|FALSE|BOOLEAN|0x5b
343343
gUefiOvmfPkgTokenSpaceGuid.PcdTdxPteMemoryEncryptionAddressOrMask|0|UINT64|0x5c
344344

345+
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPartialMemorySize|0|UINT64|0x5d
345346

346347
[PcdsDynamic, PcdsDynamicEx]
347348
gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2

OvmfPkg/OvmfPkgX64.dsc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
DEFINE TDX_IGNORE_VE_HLT = FALSE
4242
DEFINE TDX_EMULATION_ENABLE = FALSE
4343
DEFINE TDX_SUPPORT = TRUE
44+
DEFINE TDX_MEM_PARTIAL_ACCEPT = 0
45+
4446
# Network definition
4547
#
4648
DEFINE NETWORK_TLS_ENABLE = FALSE
@@ -606,7 +608,8 @@
606608
!endif
607609
# 32M
608610
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageChunkSize|0x2000000
609-
611+
# Accept memory size.
612+
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPartialMemorySize|$(TDX_MEM_PARTIAL_ACCEPT)
610613
# Noexec settings for DXE.
611614
# TDX doesn't allow us to change EFER so make sure these are disabled
612615
gEfiMdeModulePkgTokenSpaceGuid.PcdImageProtectionPolicy|0x00000000

ShellPkg/Library/UefiShellDebug1CommandsLib/MemMap.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ STATIC CONST CHAR16 NameEfiACPIMemoryNVS[] = L"ACPIMemoryNVS";
2626
STATIC CONST CHAR16 NameEfiMemoryMappedIO[] = L"MemoryMappedIO";
2727
STATIC CONST CHAR16 NameEfiMemoryMappedIOPortSpace[] = L"MemoryMappedIOPortSpace";
2828
STATIC CONST CHAR16 NameEfiPalCode[] = L"PalCode";
29+
STATIC CONST CHAR16 NameEfiUnacceptedMemory[] = L"Unaccepted";
2930

3031
//
3132
// Need short names for some memory types
@@ -300,6 +301,11 @@ ShellCommandRunMemMap (
300301
TotalPages += Walker->NumberOfPages;
301302
PalCodePages += Walker->NumberOfPages;
302303
break;
304+
case EfiUnacceptedMemory:
305+
ShellPrintHiiEx(-1, -1, NULL, (EFI_STRING_ID)(!Sfo?STRING_TOKEN (STR_MEMMAP_LIST_ITEM):STRING_TOKEN (STR_MEMMAP_LIST_ITEM_SFO)), gShellDebug1HiiHandle, NameEfiUnacceptedMemory, Walker->PhysicalStart, Walker->PhysicalStart+MultU64x64(SIZE_4KB,Walker->NumberOfPages)-1, Walker->NumberOfPages, Walker->Attribute);
306+
TotalPages += Walker->NumberOfPages;
307+
PalCodePages += Walker->NumberOfPages;
308+
break;
303309
default:
304310
//
305311
// Shell Spec defines the SFO format.

0 commit comments

Comments
 (0)