27#define ALIGNED_2MB_MASK 0x1fffff
28#define MEGABYTE_SHIFT 20
30#define ACCEPT_CHUNK_SIZE SIZE_32MB
31#define AP_STACK_SIZE SIZE_16KB
32#define APS_STACK_SIZE(CpusNum) (ALIGN_VALUE(CpusNum*AP_STACK_SIZE, SIZE_2MB))
77 UINT32 AcceptPageSize;
88 TotalLength = PhysicalEnd - PhysicalAddress;
96 if (TotalLength == 0) {
100 if (
ALIGN_VALUE (PhysicalAddress, SIZE_2MB) != PhysicalAddress) {
101 StartAddress1 = PhysicalAddress;
102 Length1 =
ALIGN_VALUE (PhysicalAddress, SIZE_2MB) - PhysicalAddress;
103 if (Length1 >= TotalLength) {
104 Length1 = TotalLength;
107 PhysicalAddress += Length1;
108 TotalLength -= Length1;
111 if (TotalLength > SIZE_2MB) {
112 StartAddress2 = PhysicalAddress;
113 Length2 = TotalLength & ~(UINT64)ALIGNED_2MB_MASK;
114 PhysicalAddress += Length2;
115 TotalLength -= Length2;
119 StartAddress3 = PhysicalAddress;
120 Length3 = TotalLength;
125 Pages = Length1 / SIZE_4KB;
127 if (EFI_ERROR (Status)) {
133 Pages = Length2 / AcceptPageSize;
134 Status =
TdAcceptPages (StartAddress2, Pages, AcceptPageSize);
135 if (EFI_ERROR (Status)) {
141 Pages = Length3 / SIZE_4KB;
143 ASSERT (!EFI_ERROR (Status));
144 if (EFI_ERROR (Status)) {
179 UINT64 AcceptPageSize;
185 Stride = (
UINTN)CpusNum * ACCEPT_CHUNK_SIZE;
186 PhysicalAddress = PhysicalStart + ACCEPT_CHUNK_SIZE * (
UINTN)CpuIndex;
188 while (!EFI_ERROR (Status) && PhysicalAddress < PhysicalEnd) {
189 Pages =
MIN (ACCEPT_CHUNK_SIZE, PhysicalEnd - PhysicalAddress) / AcceptPageSize;
191 ASSERT (!EFI_ERROR (Status));
192 PhysicalAddress += Stride;
220 Status =
TdCall (TDCALL_TDINFO, 0, 0, 0, &TdReturnData);
221 if (Status != TDX_EXIT_REASON_SUCCESS) {
226 if ((CpuIndex == 0) || (CpuIndex >= TdReturnData.TdInfo.NumVcpus)) {
258 Length = PhysicalEnd - PhysicalStart;
260 DEBUG ((DEBUG_INFO,
"MpAccept : 0x%llx - 0x%llx (0x%llx)\n", PhysicalStart, PhysicalEnd, Length));
269 if (
ALIGN_VALUE (PhysicalStart, SIZE_2MB) != PhysicalStart) {
270 Length =
MIN (
ALIGN_VALUE (PhysicalStart, SIZE_2MB) - PhysicalStart, Length);
274 PhysicalStart += Length;
275 Length = PhysicalEnd - PhysicalStart;
285 if (Length <= ACCEPT_CHUNK_SIZE) {
295 MpProtectedModeWakeupCommandAcceptPages,
328 IN UINT32 APsStackSize,
336 UINT64 ResourceLength;
337 BOOLEAN MemoryRegionFound;
339 ASSERT (VmmHobList !=
NULL);
342 Hob.Raw = (UINT8 *)VmmHobList;
343 MemoryRegionFound =
FALSE;
345 DEBUG ((DEBUG_INFO,
"AcceptMemoryForAPsStack with APsStackSize=0x%x\n", APsStackSize));
350 while (!END_OF_HOB_LIST (Hob) && !MemoryRegionFound) {
351 if (Hob.Header->
HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
352 DEBUG ((DEBUG_INFO,
"\nResourceType: 0x%x\n", Hob.ResourceDescriptor->
ResourceType));
354 if (Hob.ResourceDescriptor->
ResourceType == EFI_RESOURCE_MEMORY_UNACCEPTED) {
357 PhysicalEnd = PhysicalStart + ResourceLength;
360 DEBUG ((DEBUG_INFO,
"PhysicalStart: 0x%llx\n", PhysicalStart));
361 DEBUG ((DEBUG_INFO,
"ResourceLength: 0x%llx\n", ResourceLength));
362 DEBUG ((DEBUG_INFO,
"Owner: %g\n\n", &Hob.ResourceDescriptor->
Owner));
364 if (ResourceLength >= APsStackSize) {
365 MemoryRegionFound =
TRUE;
366 if (ResourceLength > ACCEPT_CHUNK_SIZE) {
367 PhysicalEnd = Hob.ResourceDescriptor->
PhysicalStart + APsStackSize;
375 if (EFI_ERROR (Status)) {
381 Hob.Raw = GET_NEXT_HOB (Hob);
384 ASSERT (MemoryRegionFound);
385 *PhysicalAddressEnd = PhysicalEnd;
418 AcceptMemoryEndAddress = BASE_4GB;
420 ASSERT (VmmHobList !=
NULL);
421 Hob.Raw = (UINT8 *)VmmHobList;
423 DEBUG ((DEBUG_INFO,
"AcceptMemory under address of 4G\n"));
428 while (!END_OF_HOB_LIST (Hob)) {
429 if (Hob.Header->
HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
430 if (Hob.ResourceDescriptor->
ResourceType == EFI_RESOURCE_MEMORY_UNACCEPTED) {
432 PhysicalEnd = PhysicalStart + Hob.ResourceDescriptor->
ResourceLength;
434 if (PhysicalEnd <= PhysicalAddressStart) {
436 Hob.Raw = GET_NEXT_HOB (Hob);
440 if (PhysicalStart >= AcceptMemoryEndAddress) {
445 if (PhysicalStart >= PhysicalAddressStart) {
447 }
else if ((PhysicalStart < PhysicalAddressStart) && (PhysicalEnd > PhysicalAddressStart)) {
449 PhysicalStart = PhysicalAddressStart;
453 if (PhysicalEnd >= AcceptMemoryEndAddress) {
454 PhysicalEnd = AcceptMemoryEndAddress;
458 DEBUG ((DEBUG_INFO,
"PhysicalStart: 0x%llx\n", Hob.ResourceDescriptor->
PhysicalStart));
460 DEBUG ((DEBUG_INFO,
"Owner: %g\n\n", &Hob.ResourceDescriptor->
Owner));
469 APsStackStartAddress,
474 if (EFI_ERROR (Status)) {
479 if (PhysicalEnd == AcceptMemoryEndAddress) {
485 Hob.Raw = GET_NEXT_HOB (Hob);
507 IN UINT32 *ValidList,
508 IN UINT32 ValidListLength
513 if (ValidList ==
NULL) {
517 for (index = 0; index < ValidListLength; index++) {
518 if (ValidList[index] == Value) {
543 UINT32 EFI_BOOT_MODE_LIST[] = {
544 BOOT_WITH_FULL_CONFIGURATION,
545 BOOT_WITH_MINIMAL_CONFIGURATION,
546 BOOT_ASSUMING_NO_CONFIGURATION_CHANGES,
547 BOOT_WITH_FULL_CONFIGURATION_PLUS_DIAGNOSTICS,
548 BOOT_WITH_DEFAULT_SETTINGS,
551 BOOT_WITH_MFG_MODE_SETTINGS,
554 BOOT_ON_FLASH_UPDATE,
555 BOOT_IN_RECOVERY_MODE
558 UINT32 EFI_RESOURCE_TYPE_LIST[] = {
559 EFI_RESOURCE_SYSTEM_MEMORY,
560 EFI_RESOURCE_MEMORY_MAPPED_IO,
562 EFI_RESOURCE_FIRMWARE_DEVICE,
563 EFI_RESOURCE_MEMORY_MAPPED_IO_PORT,
564 EFI_RESOURCE_MEMORY_RESERVED,
565 EFI_RESOURCE_IO_RESERVED,
566 EFI_RESOURCE_MEMORY_UNACCEPTED
569 if (VmmHobList ==
NULL) {
570 DEBUG ((DEBUG_ERROR,
"HOB: HOB data pointer is NULL\n"));
574 Hob.Raw = (UINT8 *)VmmHobList;
579 while (!END_OF_HOB_LIST (Hob)) {
580 if (Hob.Header->
Reserved != (UINT32)0) {
581 DEBUG ((DEBUG_ERROR,
"HOB: Hob header Reserved filed should be zero\n"));
586 DEBUG ((DEBUG_ERROR,
"HOB: Hob header LEANGTH should not be zero\n"));
591 case EFI_HOB_TYPE_HANDOFF:
593 DEBUG ((DEBUG_ERROR,
"HOB: Hob length is not equal corresponding hob structure. Type: 0x%04x\n", EFI_HOB_TYPE_HANDOFF));
598 DEBUG ((DEBUG_ERROR,
"HOB: Unknow HandoffInformationTable BootMode type. Type: 0x%08x\n", Hob.HandoffInformationTable->
BootMode));
603 DEBUG ((DEBUG_ERROR,
"HOB: HandoffInformationTable EfiFreeMemoryTop address must be 4-KB aligned to meet page restrictions of UEFI.\
610 case EFI_HOB_TYPE_RESOURCE_DESCRIPTOR:
612 DEBUG ((DEBUG_ERROR,
"HOB: Hob length is not equal corresponding hob structure. Type: 0x%04x\n", EFI_HOB_TYPE_RESOURCE_DESCRIPTOR));
617 DEBUG ((DEBUG_ERROR,
"HOB: Unknow ResourceDescriptor ResourceType type. Type: 0x%08x\n", Hob.ResourceDescriptor->
ResourceType));
621 if ((Hob.ResourceDescriptor->
ResourceAttribute & (~(EFI_RESOURCE_ATTRIBUTE_PRESENT |
622 EFI_RESOURCE_ATTRIBUTE_INITIALIZED |
623 EFI_RESOURCE_ATTRIBUTE_TESTED |
624 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED |
625 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED |
626 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED |
627 EFI_RESOURCE_ATTRIBUTE_PERSISTENT |
628 EFI_RESOURCE_ATTRIBUTE_SINGLE_BIT_ECC |
629 EFI_RESOURCE_ATTRIBUTE_MULTIPLE_BIT_ECC |
630 EFI_RESOURCE_ATTRIBUTE_ECC_RESERVED_1 |
631 EFI_RESOURCE_ATTRIBUTE_ECC_RESERVED_2 |
632 EFI_RESOURCE_ATTRIBUTE_UNCACHEABLE |
633 EFI_RESOURCE_ATTRIBUTE_WRITE_COMBINEABLE |
634 EFI_RESOURCE_ATTRIBUTE_WRITE_THROUGH_CACHEABLE |
635 EFI_RESOURCE_ATTRIBUTE_WRITE_BACK_CACHEABLE |
636 EFI_RESOURCE_ATTRIBUTE_16_BIT_IO |
637 EFI_RESOURCE_ATTRIBUTE_32_BIT_IO |
638 EFI_RESOURCE_ATTRIBUTE_64_BIT_IO |
639 EFI_RESOURCE_ATTRIBUTE_UNCACHED_EXPORTED |
640 EFI_RESOURCE_ATTRIBUTE_READ_PROTECTABLE |
641 EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTABLE |
642 EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTABLE |
643 EFI_RESOURCE_ATTRIBUTE_PERSISTABLE |
644 EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED |
645 EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTABLE |
646 EFI_RESOURCE_ATTRIBUTE_ENCRYPTED|
647 EFI_RESOURCE_ATTRIBUTE_SPECIAL_PURPOSE |
648 EFI_RESOURCE_ATTRIBUTE_MORE_RELIABLE))) != 0)
650 DEBUG ((DEBUG_ERROR,
"HOB: Unknow ResourceDescriptor ResourceAttribute type. Type: 0x%08x\n", Hob.ResourceDescriptor->
ResourceAttribute));
657 case EFI_HOB_TYPE_GUID_EXTENSION:
660 case EFI_HOB_TYPE_FV:
662 DEBUG ((DEBUG_ERROR,
"HOB: Hob length is not equal corresponding hob structure. Type: 0x%04x\n", EFI_HOB_TYPE_FV));
668 case EFI_HOB_TYPE_FV2:
670 DEBUG ((DEBUG_ERROR,
"HOB: Hob length is not equal corresponding hob structure. Type: 0x%04x\n", EFI_HOB_TYPE_FV2));
676 case EFI_HOB_TYPE_FV3:
678 DEBUG ((DEBUG_ERROR,
"HOB: Hob length is not equal corresponding hob structure. Type: 0x%04x\n", EFI_HOB_TYPE_FV3));
684 case EFI_HOB_TYPE_CPU:
686 DEBUG ((DEBUG_ERROR,
"HOB: Hob length is not equal corresponding hob structure. Type: 0x%04x\n", EFI_HOB_TYPE_CPU));
690 for (UINT32 index = 0; index < 6; index++) {
691 if (Hob.Cpu->
Reserved[index] != 0) {
692 DEBUG ((DEBUG_ERROR,
"HOB: Cpu Reserved field will always be set to zero.\n"));
700 DEBUG ((DEBUG_ERROR,
"HOB: Hob type is not know. Type: 0x%04x\n", Hob.Header->
HobType));
705 Hob.Raw = (UINT8 *)(Hob.Raw + Hob.Header->
HobLength);
750 APsStackStartAddress = PhysicalEnd - APS_STACK_SIZE (CpusNum);
753 APsStackStartAddress = 0;
756 Status =
AcceptMemory (VmmHobList, CpusNum, APsStackStartAddress, PhysicalEnd);
782 Status =
TdCall (TDCALL_TDINFO, 0, 0, 0, &TdReturnData);
783 if (EFI_ERROR (Status)) {
789 "Intel Tdx Started with (GPAW: %d, Cpus: %d)\n",
790 TdReturnData.TdInfo.Gpaw,
791 TdReturnData.TdInfo.NumVcpus
798 return EFI_INVALID_PARAMETER;
833 if ((DataToHash ==
NULL) || (DataToHashLen == 0)) {
834 return EFI_INVALID_PARAMETER;
838 return EFI_INVALID_PARAMETER;
857 ASSERT (!EFI_ERROR (Status));
882 Hob.Raw = (UINT8 *)TdHob;
887 while (!END_OF_HOB_LIST (Hob)) {
888 Hob.Raw = GET_NEXT_HOB (Hob);
894 (
UINTN)((UINT8 *)Hob.Raw - (UINT8 *)TdHob),
899 if (EFI_ERROR (Status)) {
908 if (WorkArea ==
NULL) {
909 return EFI_DEVICE_ERROR;
912 WorkArea->TdxWorkArea.SecTdxWorkArea.TdxMeasurementsData.MeasurementsBitmap |= TDX_MEASUREMENT_TDHOB_BITMASK;
938 (UINT8 *)(
UINTN)
PcdGet32 (PcdOvmfFlashNvStorageVariableBase),
939 (UINT64)
PcdGet32 (PcdCfvRawDataSize),
944 if (EFI_ERROR (Status)) {
953 if (WorkArea ==
NULL) {
954 return EFI_DEVICE_ERROR;
957 WorkArea->TdxWorkArea.SecTdxWorkArea.TdxMeasurementsData.MeasurementsBitmap |= TDX_MEASUREMENT_CFVIMG_BITMASK;
976 #ifdef TDX_PEI_LESS_BOOT
979 return EFI_UNSUPPORTED;
BOOLEAN EFIAPI Sha384HashAll(IN CONST VOID *Data, IN UINTN DataSize, OUT UINT8 *HashValue)
#define SHA384_DIGEST_SIZE
UINTN EFIAPI TdCall(IN UINT64 Leaf, IN UINT64 Arg1, IN UINT64 Arg2, IN UINT64 Arg3, IN OUT VOID *Results)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define ALIGN_VALUE(Value, Alignment)
#define ARRAY_SIZE(Array)
#define DEBUG(Expression)
#define FixedPcdGet32(TokenName)
#define PcdGet32(TokenName)
STATIC EFI_STATUS EFIAPI AcceptMemoryForAPsStack(IN CONST VOID *VmmHobList, IN UINT32 APsStackSize, OUT EFI_PHYSICAL_ADDRESS *PhysicalAddressEnd)
STATIC EFI_STATUS HashAndExtendToRtmr(IN UINT32 RtmrIndex, IN VOID *DataToHash, IN UINTN DataToHashLen, OUT UINT8 *Digest, IN UINTN DigestLen)
STATIC BOOLEAN EFIAPI IsInValidList(IN UINT32 Value, IN UINT32 *ValidList, IN UINT32 ValidListLength)
STATIC EFI_STATUS EFIAPI ProcessHobList(IN CONST VOID *VmmHobList)
EFI_STATUS EFIAPI TdxHelperBuildGuidHobForTdxMeasurement(VOID)
STATIC EFI_STATUS EFIAPI MpAcceptMemoryResourceRange(IN EFI_PHYSICAL_ADDRESS PhysicalStart, IN EFI_PHYSICAL_ADDRESS PhysicalEnd, IN OUT EFI_PHYSICAL_ADDRESS APsStackAddress, IN UINT32 CpusNum)
STATIC EFI_STATUS EFIAPI BspApAcceptMemoryResourceRange(UINT32 CpuIndex, UINT32 CpusNum, EFI_PHYSICAL_ADDRESS PhysicalStart, EFI_PHYSICAL_ADDRESS PhysicalEnd)
EFI_STATUS EFIAPI TdxHelperProcessTdHob(VOID)
STATIC EFI_STATUS EFIAPI AcceptMemory(IN CONST VOID *VmmHobList, IN UINT32 CpusNum, IN EFI_PHYSICAL_ADDRESS APsStackStartAddress, IN EFI_PHYSICAL_ADDRESS PhysicalAddressStart)
STATIC EFI_STATUS EFIAPI BspAcceptMemoryResourceRange(IN EFI_PHYSICAL_ADDRESS PhysicalAddress, IN EFI_PHYSICAL_ADDRESS PhysicalEnd)
STATIC BOOLEAN EFIAPI ValidateHobList(IN CONST VOID *VmmHobList)
EFI_STATUS EFIAPI TdxHelperMeasureTdHob(VOID)
EFI_STATUS InternalBuildGuidHobForTdxMeasurement(VOID)
STATIC EFI_STATUS EFIAPI ApAcceptMemoryResourceRange(UINT32 CpuIndex, EFI_PHYSICAL_ADDRESS PhysicalStart, EFI_PHYSICAL_ADDRESS PhysicalEnd)
EFI_STATUS EFIAPI TdxHelperMeasureCfvImage(VOID)
EFI_STATUS EFIAPI TdAcceptPages(IN UINT64 StartAddress, IN UINT64 NumberOfPages, IN UINT32 PageSize)
EFI_STATUS EFIAPI TdExtendRtmr(IN UINT32 *Data, IN UINT32 DataLen, IN UINT8 Index)
VOID EFIAPI MpSerializeStart(VOID)
VOID EFIAPI MpSendWakeupCommand(IN UINT16 Command, IN UINT64 WakeupVector, IN UINT64 WakeupArgs1, IN UINT64 WakeupArgs2, IN UINT64 WakeupArgs3, IN UINT64 WakeupArgs4)
VOID EFIAPI MpSerializeEnd(VOID)
UINT32 EFIAPI GetCpusNum(VOID)
UINT64 EFI_PHYSICAL_ADDRESS
EFI_PHYSICAL_ADDRESS EfiFreeMemoryTop
EFI_PHYSICAL_ADDRESS PhysicalStart
EFI_RESOURCE_TYPE ResourceType
EFI_RESOURCE_ATTRIBUTE_TYPE ResourceAttribute