1919#include <IndustryStandard/UefiTcgPlatform.h>
2020#include "TdxStartupInternal.h"
2121
22+ UINT64 mTdxAcceptMemSize = 0 ;
23+
2224VOID
2325EFIAPI
2426DEBUG_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 (
0 commit comments