OvmfPkg/IntelTdx: Fix TDVF boot failure with odd-sized memory below 2816M

TDVF boot in QEMU fails when the VMM is configured with
odd memory sizes (in MB) below 2816M (e.g., 2815M, 2813M).
This is due to the RAM split defined in pc_q35.c:
when RAM exceeds 2816M, low memory is truncated to 2048M
and the remainder is placed above the 4G boundary.
This split causes failures only with odd sizes below 2816M.

TDVF uses a default page size of 2M (hugepage),
which leads to a corner case where the last 1M may not be accepted.
To resolve this, on BSP, always accept any remaining memory using a 4K page size.

Signed-off-by: Stanislaw Grams <stanislaw.grams@intel.com>
This commit is contained in:
Stanislaw Grams 2025-09-17 17:31:39 +02:00 committed by mergify[bot]
parent 17691a2641
commit b15f98e68f

View file

@ -179,12 +179,18 @@ BspApAcceptMemoryResourceRange (
UINT64 Stride;
UINT64 AcceptPageSize;
EFI_PHYSICAL_ADDRESS PhysicalAddress;
UINT64 TotalSize;
UINT64 ProcessedSize;
UINT64 RemainderSize;
UINT64 RemainderPages;
UINT64 RemainderStart;
AcceptPageSize = (UINT64)(UINTN)FixedPcdGet32 (PcdTdxAcceptPageSize);
Status = EFI_SUCCESS;
Stride = (UINTN)CpusNum * ACCEPT_CHUNK_SIZE;
PhysicalAddress = PhysicalStart + ACCEPT_CHUNK_SIZE * (UINTN)CpuIndex;
TotalSize = PhysicalEnd - PhysicalStart;
while (!EFI_ERROR (Status) && PhysicalAddress < PhysicalEnd) {
Pages = MIN (ACCEPT_CHUNK_SIZE, PhysicalEnd - PhysicalAddress) / AcceptPageSize;
@ -193,6 +199,27 @@ BspApAcceptMemoryResourceRange (
PhysicalAddress += Stride;
}
if (CpuIndex == 0) {
ProcessedSize = (TotalSize / AcceptPageSize) * AcceptPageSize;
RemainderStart = PhysicalStart + ProcessedSize;
RemainderSize = TotalSize % AcceptPageSize;
if (RemainderSize > 0) {
RemainderPages = RemainderSize / SIZE_4KB;
if (RemainderPages > 0) {
Status = TdAcceptPages (
RemainderStart,
RemainderPages,
SIZE_4KB
);
ASSERT (!EFI_ERROR (Status));
if (EFI_ERROR (Status)) {
return Status;
}
}
}
}
return EFI_SUCCESS;
}