2 Memory Detection for Virtual Machines.
4 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
14 // The package level header files this module uses
16 #include <IndustryStandard/E820.h>
17 #include <IndustryStandard/I440FxPiix4.h>
18 #include <IndustryStandard/Q35MchIch9.h>
22 // The Library classes this module consumes
24 #include <Library/BaseLib.h>
25 #include <Library/BaseMemoryLib.h>
26 #include <Library/DebugLib.h>
27 #include <Library/HobLib.h>
28 #include <Library/IoLib.h>
29 #include <Library/PcdLib.h>
30 #include <Library/PciLib.h>
31 #include <Library/PeimEntryPoint.h>
32 #include <Library/ResourcePublicationLib.h>
33 #include <Library/MtrrLib.h>
34 #include <Library/QemuFwCfgLib.h>
39 UINT8 mPhysMemAddressWidth
;
41 STATIC UINT32 mS3AcpiReservedMemoryBase
;
42 STATIC UINT32 mS3AcpiReservedMemorySize
;
44 STATIC UINT16 mQ35TsegMbytes
;
49 Q35TsegMbytesInitialization (
53 UINT16 ExtendedTsegMbytes
;
54 RETURN_STATUS PcdStatus
;
56 if (mHostBridgeDevId
!= INTEL_Q35_MCH_DEVICE_ID
) {
59 "%a: no TSEG (SMRAM) on host bridge DID=0x%04x; "
60 "only DID=0x%04x (Q35) is supported\n",
63 INTEL_Q35_MCH_DEVICE_ID
70 // Check if QEMU offers an extended TSEG.
72 // This can be seen from writing MCH_EXT_TSEG_MB_QUERY to the MCH_EXT_TSEG_MB
73 // register, and reading back the register.
75 // On a QEMU machine type that does not offer an extended TSEG, the initial
76 // write overwrites whatever value a malicious guest OS may have placed in
77 // the (unimplemented) register, before entering S3 or rebooting.
78 // Subsequently, the read returns MCH_EXT_TSEG_MB_QUERY unchanged.
80 // On a QEMU machine type that offers an extended TSEG, the initial write
81 // triggers an update to the register. Subsequently, the value read back
82 // (which is guaranteed to differ from MCH_EXT_TSEG_MB_QUERY) tells us the
83 // number of megabytes.
85 PciWrite16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB
), MCH_EXT_TSEG_MB_QUERY
);
86 ExtendedTsegMbytes
= PciRead16 (DRAMC_REGISTER_Q35 (MCH_EXT_TSEG_MB
));
87 if (ExtendedTsegMbytes
== MCH_EXT_TSEG_MB_QUERY
) {
88 mQ35TsegMbytes
= PcdGet16 (PcdQ35TsegMbytes
);
94 "%a: QEMU offers an extended TSEG (%d MB)\n",
98 PcdStatus
= PcdSet16S (PcdQ35TsegMbytes
, ExtendedTsegMbytes
);
99 ASSERT_RETURN_ERROR (PcdStatus
);
100 mQ35TsegMbytes
= ExtendedTsegMbytes
;
105 QemuUc32BaseInitialization (
109 UINT32 LowerMemorySize
;
116 if (mHostBridgeDevId
== INTEL_Q35_MCH_DEVICE_ID
) {
118 // On q35, the 32-bit area that we'll mark as UC, through variable MTRRs,
119 // starts at PcdPciExpressBaseAddress. The platform DSC is responsible for
120 // setting PcdPciExpressBaseAddress such that describing the
121 // [PcdPciExpressBaseAddress, 4GB) range require a very small number of
122 // variable MTRRs (preferably 1 or 2).
124 ASSERT (FixedPcdGet64 (PcdPciExpressBaseAddress
) <= MAX_UINT32
);
125 mQemuUc32Base
= (UINT32
)FixedPcdGet64 (PcdPciExpressBaseAddress
);
129 ASSERT (mHostBridgeDevId
== INTEL_82441_DEVICE_ID
);
131 // On i440fx, start with the [LowerMemorySize, 4GB) range. Make sure one
132 // variable MTRR suffices by truncating the size to a whole power of two,
133 // while keeping the end affixed to 4GB. This will round the base up.
135 LowerMemorySize
= GetSystemMemorySizeBelow4gb ();
136 Uc32Size
= GetPowerOfTwo32 ((UINT32
)(SIZE_4GB
- LowerMemorySize
));
137 mQemuUc32Base
= (UINT32
)(SIZE_4GB
- Uc32Size
);
139 // Assuming that LowerMemorySize is at least 1 byte, Uc32Size is at most 2GB.
140 // Therefore mQemuUc32Base is at least 2GB.
142 ASSERT (mQemuUc32Base
>= BASE_2GB
);
144 if (mQemuUc32Base
!= LowerMemorySize
) {
145 DEBUG ((DEBUG_VERBOSE
, "%a: rounded UC32 base from 0x%x up to 0x%x, for "
146 "an UC32 size of 0x%x\n", __FUNCTION__
, LowerMemorySize
, mQemuUc32Base
,
153 Iterate over the RAM entries in QEMU's fw_cfg E820 RAM map that start outside
154 of the 32-bit address range.
156 Find the highest exclusive >=4GB RAM address, or produce memory resource
157 descriptor HOBs for RAM entries that start at or above 4GB.
159 @param[out] MaxAddress If MaxAddress is NULL, then ScanOrAdd64BitE820Ram()
160 produces memory resource descriptor HOBs for RAM
161 entries that start at or above 4GB.
163 Otherwise, MaxAddress holds the highest exclusive
164 >=4GB RAM address on output. If QEMU's fw_cfg E820
165 RAM map contains no RAM entry that starts outside of
166 the 32-bit address range, then MaxAddress is exactly
169 @retval EFI_SUCCESS The fw_cfg E820 RAM map was found and processed.
171 @retval EFI_PROTOCOL_ERROR The RAM map was found, but its size wasn't a
172 whole multiple of sizeof(EFI_E820_ENTRY64). No
173 RAM entry was processed.
175 @return Error codes from QemuFwCfgFindFile(). No RAM
180 ScanOrAdd64BitE820Ram (
181 OUT UINT64
*MaxAddress OPTIONAL
185 FIRMWARE_CONFIG_ITEM FwCfgItem
;
187 EFI_E820_ENTRY64 E820Entry
;
190 Status
= QemuFwCfgFindFile ("etc/e820", &FwCfgItem
, &FwCfgSize
);
191 if (EFI_ERROR (Status
)) {
194 if (FwCfgSize
% sizeof E820Entry
!= 0) {
195 return EFI_PROTOCOL_ERROR
;
198 if (MaxAddress
!= NULL
) {
199 *MaxAddress
= BASE_4GB
;
202 QemuFwCfgSelectItem (FwCfgItem
);
203 for (Processed
= 0; Processed
< FwCfgSize
; Processed
+= sizeof E820Entry
) {
204 QemuFwCfgReadBytes (sizeof E820Entry
, &E820Entry
);
207 "%a: Base=0x%Lx Length=0x%Lx Type=%u\n",
213 if (E820Entry
.Type
== EfiAcpiAddressRangeMemory
&&
214 E820Entry
.BaseAddr
>= BASE_4GB
) {
215 if (MaxAddress
== NULL
) {
220 // Round up the start address, and round down the end address.
222 Base
= ALIGN_VALUE (E820Entry
.BaseAddr
, (UINT64
)EFI_PAGE_SIZE
);
223 End
= (E820Entry
.BaseAddr
+ E820Entry
.Length
) &
224 ~(UINT64
)EFI_PAGE_MASK
;
226 AddMemoryRangeHob (Base
, End
);
229 "%a: AddMemoryRangeHob [0x%Lx, 0x%Lx)\n",
238 Candidate
= E820Entry
.BaseAddr
+ E820Entry
.Length
;
239 if (Candidate
> *MaxAddress
) {
240 *MaxAddress
= Candidate
;
243 "%a: MaxAddress=0x%Lx\n",
256 GetSystemMemorySizeBelow4gb (
264 // CMOS 0x34/0x35 specifies the system memory above 16 MB.
265 // * CMOS(0x35) is the high byte
266 // * CMOS(0x34) is the low byte
267 // * The size is specified in 64kb chunks
268 // * Since this is memory above 16MB, the 16MB must be added
269 // into the calculation to get the total memory size.
272 Cmos0x34
= (UINT8
) CmosRead8 (0x34);
273 Cmos0x35
= (UINT8
) CmosRead8 (0x35);
275 return (UINT32
) (((UINTN
)((Cmos0x35
<< 8) + Cmos0x34
) << 16) + SIZE_16MB
);
281 GetSystemMemorySizeAbove4gb (
288 // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.
289 // * CMOS(0x5d) is the most significant size byte
290 // * CMOS(0x5c) is the middle size byte
291 // * CMOS(0x5b) is the least significant size byte
292 // * The size is specified in 64kb chunks
296 for (CmosIndex
= 0x5d; CmosIndex
>= 0x5b; CmosIndex
--) {
297 Size
= (UINT32
) (Size
<< 8) + (UINT32
) CmosRead8 (CmosIndex
);
300 return LShiftU64 (Size
, 16);
305 Return the highest address that DXE could possibly use, plus one.
313 UINT64 FirstNonAddress
;
314 UINT64 Pci64Base
, Pci64Size
;
315 CHAR8 MbString
[7 + 1];
317 FIRMWARE_CONFIG_ITEM FwCfgItem
;
319 UINT64 HotPlugMemoryEnd
;
320 RETURN_STATUS PcdStatus
;
323 // set FirstNonAddress to suppress incorrect compiler/analyzer warnings
328 // If QEMU presents an E820 map, then get the highest exclusive >=4GB RAM
329 // address from it. This can express an address >= 4GB+1TB.
331 // Otherwise, get the flat size of the memory above 4GB from the CMOS (which
332 // can only express a size smaller than 1TB), and add it to 4GB.
334 Status
= ScanOrAdd64BitE820Ram (&FirstNonAddress
);
335 if (EFI_ERROR (Status
)) {
336 FirstNonAddress
= BASE_4GB
+ GetSystemMemorySizeAbove4gb ();
340 // If DXE is 32-bit, then we're done; PciBusDxe will degrade 64-bit MMIO
341 // resources to 32-bit anyway. See DegradeResource() in
342 // "PciResourceSupport.c".
345 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
346 return FirstNonAddress
;
351 // Otherwise, in order to calculate the highest address plus one, we must
352 // consider the 64-bit PCI host aperture too. Fetch the default size.
354 Pci64Size
= PcdGet64 (PcdPciMmio64Size
);
357 // See if the user specified the number of megabytes for the 64-bit PCI host
358 // aperture. The number of non-NUL characters in MbString allows for
359 // 9,999,999 MB, which is approximately 10 TB.
361 // As signaled by the "X-" prefix, this knob is experimental, and might go
364 Status
= QemuFwCfgFindFile ("opt/ovmf/X-PciMmio64Mb", &FwCfgItem
,
366 if (!EFI_ERROR (Status
)) {
367 if (FwCfgSize
>= sizeof MbString
) {
369 "%a: ignoring malformed 64-bit PCI host aperture size from fw_cfg\n",
372 QemuFwCfgSelectItem (FwCfgItem
);
373 QemuFwCfgReadBytes (FwCfgSize
, MbString
);
374 MbString
[FwCfgSize
] = '\0';
375 Pci64Size
= LShiftU64 (AsciiStrDecimalToUint64 (MbString
), 20);
379 if (Pci64Size
== 0) {
380 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
381 DEBUG ((EFI_D_INFO
, "%a: disabling 64-bit PCI host aperture\n",
383 PcdStatus
= PcdSet64S (PcdPciMmio64Size
, 0);
384 ASSERT_RETURN_ERROR (PcdStatus
);
388 // There's nothing more to do; the amount of memory above 4GB fully
389 // determines the highest address plus one. The memory hotplug area (see
390 // below) plays no role for the firmware in this case.
392 return FirstNonAddress
;
396 // The "etc/reserved-memory-end" fw_cfg file, when present, contains an
397 // absolute, exclusive end address for the memory hotplug area. This area
398 // starts right at the end of the memory above 4GB. The 64-bit PCI host
399 // aperture must be placed above it.
401 Status
= QemuFwCfgFindFile ("etc/reserved-memory-end", &FwCfgItem
,
403 if (!EFI_ERROR (Status
) && FwCfgSize
== sizeof HotPlugMemoryEnd
) {
404 QemuFwCfgSelectItem (FwCfgItem
);
405 QemuFwCfgReadBytes (FwCfgSize
, &HotPlugMemoryEnd
);
406 DEBUG ((DEBUG_VERBOSE
, "%a: HotPlugMemoryEnd=0x%Lx\n", __FUNCTION__
,
409 ASSERT (HotPlugMemoryEnd
>= FirstNonAddress
);
410 FirstNonAddress
= HotPlugMemoryEnd
;
414 // SeaBIOS aligns both boundaries of the 64-bit PCI host aperture to 1GB, so
415 // that the host can map it with 1GB hugepages. Follow suit.
417 Pci64Base
= ALIGN_VALUE (FirstNonAddress
, (UINT64
)SIZE_1GB
);
418 Pci64Size
= ALIGN_VALUE (Pci64Size
, (UINT64
)SIZE_1GB
);
421 // The 64-bit PCI host aperture should also be "naturally" aligned. The
422 // alignment is determined by rounding the size of the aperture down to the
423 // next smaller or equal power of two. That is, align the aperture by the
424 // largest BAR size that can fit into it.
426 Pci64Base
= ALIGN_VALUE (Pci64Base
, GetPowerOfTwo64 (Pci64Size
));
428 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
430 // The core PciHostBridgeDxe driver will automatically add this range to
431 // the GCD memory space map through our PciHostBridgeLib instance; here we
432 // only need to set the PCDs.
434 PcdStatus
= PcdSet64S (PcdPciMmio64Base
, Pci64Base
);
435 ASSERT_RETURN_ERROR (PcdStatus
);
436 PcdStatus
= PcdSet64S (PcdPciMmio64Size
, Pci64Size
);
437 ASSERT_RETURN_ERROR (PcdStatus
);
439 DEBUG ((EFI_D_INFO
, "%a: Pci64Base=0x%Lx Pci64Size=0x%Lx\n",
440 __FUNCTION__
, Pci64Base
, Pci64Size
));
444 // The useful address space ends with the 64-bit PCI host aperture.
446 FirstNonAddress
= Pci64Base
+ Pci64Size
;
447 return FirstNonAddress
;
452 Initialize the mPhysMemAddressWidth variable, based on guest RAM size.
455 AddressWidthInitialization (
459 UINT64 FirstNonAddress
;
462 // As guest-physical memory size grows, the permanent PEI RAM requirements
463 // are dominated by the identity-mapping page tables built by the DXE IPL.
464 // The DXL IPL keys off of the physical address bits advertized in the CPU
465 // HOB. To conserve memory, we calculate the minimum address width here.
467 FirstNonAddress
= GetFirstNonAddress ();
468 mPhysMemAddressWidth
= (UINT8
)HighBitSet64 (FirstNonAddress
);
471 // If FirstNonAddress is not an integral power of two, then we need an
474 if ((FirstNonAddress
& (FirstNonAddress
- 1)) != 0) {
475 ++mPhysMemAddressWidth
;
479 // The minimum address width is 36 (covers up to and excluding 64 GB, which
480 // is the maximum for Ia32 + PAE). The theoretical architecture maximum for
481 // X64 long mode is 52 bits, but the DXE IPL clamps that down to 48 bits. We
482 // can simply assert that here, since 48 bits are good enough for 256 TB.
484 if (mPhysMemAddressWidth
<= 36) {
485 mPhysMemAddressWidth
= 36;
487 ASSERT (mPhysMemAddressWidth
<= 48);
492 Calculate the cap for the permanent PEI memory.
500 BOOLEAN Page1GSupport
;
508 // If DXE is 32-bit, then just return the traditional 64 MB cap.
511 if (!FeaturePcdGet (PcdDxeIplSwitchToLongMode
)) {
517 // Dependent on physical address width, PEI memory allocations can be
518 // dominated by the page tables built for 64-bit DXE. So we key the cap off
519 // of those. The code below is based on CreateIdentityMappingPageTables() in
520 // "MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c".
522 Page1GSupport
= FALSE
;
523 if (PcdGetBool (PcdUse1GPageTable
)) {
524 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
525 if (RegEax
>= 0x80000001) {
526 AsmCpuid (0x80000001, NULL
, NULL
, NULL
, &RegEdx
);
527 if ((RegEdx
& BIT26
) != 0) {
528 Page1GSupport
= TRUE
;
533 if (mPhysMemAddressWidth
<= 39) {
535 PdpEntries
= 1 << (mPhysMemAddressWidth
- 30);
536 ASSERT (PdpEntries
<= 0x200);
538 Pml4Entries
= 1 << (mPhysMemAddressWidth
- 39);
539 ASSERT (Pml4Entries
<= 0x200);
543 TotalPages
= Page1GSupport
? Pml4Entries
+ 1 :
544 (PdpEntries
+ 1) * Pml4Entries
+ 1;
545 ASSERT (TotalPages
<= 0x40201);
548 // Add 64 MB for miscellaneous allocations. Note that for
549 // mPhysMemAddressWidth values close to 36, the cap will actually be
550 // dominated by this increment.
552 return (UINT32
)(EFI_PAGES_TO_SIZE (TotalPages
) + SIZE_64MB
);
557 Publish PEI core memory
559 @return EFI_SUCCESS The PEIM initialized successfully.
568 EFI_PHYSICAL_ADDRESS MemoryBase
;
570 UINT32 LowerMemorySize
;
573 LowerMemorySize
= GetSystemMemorySizeBelow4gb ();
574 if (FeaturePcdGet (PcdSmmSmramRequire
)) {
576 // TSEG is chipped from the end of low RAM
578 LowerMemorySize
-= mQ35TsegMbytes
* SIZE_1MB
;
582 // If S3 is supported, then the S3 permanent PEI memory is placed next,
583 // downwards. Its size is primarily dictated by CpuMpPei. The formula below
584 // is an approximation.
587 mS3AcpiReservedMemorySize
= SIZE_512KB
+
589 PcdGet32 (PcdCpuApStackSize
);
590 mS3AcpiReservedMemoryBase
= LowerMemorySize
- mS3AcpiReservedMemorySize
;
591 LowerMemorySize
= mS3AcpiReservedMemoryBase
;
594 if (mBootMode
== BOOT_ON_S3_RESUME
) {
595 MemoryBase
= mS3AcpiReservedMemoryBase
;
596 MemorySize
= mS3AcpiReservedMemorySize
;
598 PeiMemoryCap
= GetPeiMemoryCap ();
599 DEBUG ((EFI_D_INFO
, "%a: mPhysMemAddressWidth=%d PeiMemoryCap=%u KB\n",
600 __FUNCTION__
, mPhysMemAddressWidth
, PeiMemoryCap
>> 10));
603 // Determine the range of memory to use during PEI
605 // Technically we could lay the permanent PEI RAM over SEC's temporary
606 // decompression and scratch buffer even if "secure S3" is needed, since
607 // their lifetimes don't overlap. However, PeiFvInitialization() will cover
608 // RAM up to PcdOvmfDecompressionScratchEnd with an EfiACPIMemoryNVS memory
609 // allocation HOB, and other allocations served from the permanent PEI RAM
610 // shouldn't overlap with that HOB.
612 MemoryBase
= mS3Supported
&& FeaturePcdGet (PcdSmmSmramRequire
) ?
613 PcdGet32 (PcdOvmfDecompressionScratchEnd
) :
614 PcdGet32 (PcdOvmfDxeMemFvBase
) + PcdGet32 (PcdOvmfDxeMemFvSize
);
615 MemorySize
= LowerMemorySize
- MemoryBase
;
616 if (MemorySize
> PeiMemoryCap
) {
617 MemoryBase
= LowerMemorySize
- PeiMemoryCap
;
618 MemorySize
= PeiMemoryCap
;
623 // Publish this memory to the PEI Core
625 Status
= PublishSystemMemory(MemoryBase
, MemorySize
);
626 ASSERT_EFI_ERROR (Status
);
633 Peform Memory Detection for QEMU / KVM
642 UINT64 LowerMemorySize
;
643 UINT64 UpperMemorySize
;
644 MTRR_SETTINGS MtrrSettings
;
647 DEBUG ((EFI_D_INFO
, "%a called\n", __FUNCTION__
));
650 // Determine total memory size available
652 LowerMemorySize
= GetSystemMemorySizeBelow4gb ();
653 UpperMemorySize
= GetSystemMemorySizeAbove4gb ();
655 if (mBootMode
== BOOT_ON_S3_RESUME
) {
657 // Create the following memory HOB as an exception on the S3 boot path.
659 // Normally we'd create memory HOBs only on the normal boot path. However,
660 // CpuMpPei specifically needs such a low-memory HOB on the S3 path as
661 // well, for "borrowing" a subset of it temporarily, for the AP startup
664 // CpuMpPei saves the original contents of the borrowed area in permanent
665 // PEI RAM, in a backup buffer allocated with the normal PEI services.
666 // CpuMpPei restores the original contents ("returns" the borrowed area) at
667 // End-of-PEI. End-of-PEI in turn is emitted by S3Resume2Pei before
668 // transferring control to the OS's wakeup vector in the FACS.
670 // We expect any other PEIMs that "borrow" memory similarly to CpuMpPei to
671 // restore the original contents. Furthermore, we expect all such PEIMs
672 // (CpuMpPei included) to claim the borrowed areas by producing memory
673 // allocation HOBs, and to honor preexistent memory allocation HOBs when
674 // looking for an area to borrow.
676 AddMemoryRangeHob (0, BASE_512KB
+ BASE_128KB
);
679 // Create memory HOBs
681 AddMemoryRangeHob (0, BASE_512KB
+ BASE_128KB
);
683 if (FeaturePcdGet (PcdSmmSmramRequire
)) {
686 TsegSize
= mQ35TsegMbytes
* SIZE_1MB
;
687 AddMemoryRangeHob (BASE_1MB
, LowerMemorySize
- TsegSize
);
688 AddReservedMemoryBaseSizeHob (LowerMemorySize
- TsegSize
, TsegSize
,
691 AddMemoryRangeHob (BASE_1MB
, LowerMemorySize
);
695 // If QEMU presents an E820 map, then create memory HOBs for the >=4GB RAM
696 // entries. Otherwise, create a single memory HOB with the flat >=4GB
697 // memory size read from the CMOS.
699 Status
= ScanOrAdd64BitE820Ram (NULL
);
700 if (EFI_ERROR (Status
) && UpperMemorySize
!= 0) {
701 AddMemoryBaseSizeHob (BASE_4GB
, UpperMemorySize
);
706 // We'd like to keep the following ranges uncached:
708 // - [LowerMemorySize, 4 GB)
710 // Everything else should be WB. Unfortunately, programming the inverse (ie.
711 // keeping the default UC, and configuring the complement set of the above as
712 // WB) is not reliable in general, because the end of the upper RAM can have
713 // practically any alignment, and we may not have enough variable MTRRs to
716 if (IsMtrrSupported ()) {
717 MtrrGetAllMtrrs (&MtrrSettings
);
720 // MTRRs disabled, fixed MTRRs disabled, default type is uncached
722 ASSERT ((MtrrSettings
.MtrrDefType
& BIT11
) == 0);
723 ASSERT ((MtrrSettings
.MtrrDefType
& BIT10
) == 0);
724 ASSERT ((MtrrSettings
.MtrrDefType
& 0xFF) == 0);
727 // flip default type to writeback
729 SetMem (&MtrrSettings
.Fixed
, sizeof MtrrSettings
.Fixed
, 0x06);
730 ZeroMem (&MtrrSettings
.Variables
, sizeof MtrrSettings
.Variables
);
731 MtrrSettings
.MtrrDefType
|= BIT11
| BIT10
| 6;
732 MtrrSetAllMtrrs (&MtrrSettings
);
735 // Set memory range from 640KB to 1MB to uncacheable
737 Status
= MtrrSetMemoryAttribute (BASE_512KB
+ BASE_128KB
,
738 BASE_1MB
- (BASE_512KB
+ BASE_128KB
), CacheUncacheable
);
739 ASSERT_EFI_ERROR (Status
);
742 // Set the memory range from the start of the 32-bit MMIO area (32-bit PCI
743 // MMIO aperture on i440fx, PCIEXBAR on q35) to 4GB as uncacheable.
745 Status
= MtrrSetMemoryAttribute (mQemuUc32Base
, SIZE_4GB
- mQemuUc32Base
,
747 ASSERT_EFI_ERROR (Status
);
752 Publish system RAM and reserve memory regions
756 InitializeRamRegions (
761 QemuInitializeRam ();
763 XenPublishRamRegions ();
766 if (mS3Supported
&& mBootMode
!= BOOT_ON_S3_RESUME
) {
768 // This is the memory range that will be used for PEI on S3 resume
770 BuildMemoryAllocationHob (
771 mS3AcpiReservedMemoryBase
,
772 mS3AcpiReservedMemorySize
,
777 // Cover the initial RAM area used as stack and temporary PEI heap.
779 // This is reserved as ACPI NVS so it can be used on S3 resume.
781 BuildMemoryAllocationHob (
782 PcdGet32 (PcdOvmfSecPeiTempRamBase
),
783 PcdGet32 (PcdOvmfSecPeiTempRamSize
),
788 // SEC stores its table of GUIDed section handlers here.
790 BuildMemoryAllocationHob (
791 PcdGet64 (PcdGuidedExtractHandlerTableAddress
),
792 PcdGet32 (PcdGuidedExtractHandlerTableSize
),
798 // Reserve the initial page tables built by the reset vector code.
800 // Since this memory range will be used by the Reset Vector on S3
801 // resume, it must be reserved as ACPI NVS.
803 BuildMemoryAllocationHob (
804 (EFI_PHYSICAL_ADDRESS
)(UINTN
) PcdGet32 (PcdOvmfSecPageTablesBase
),
805 (UINT64
)(UINTN
) PcdGet32 (PcdOvmfSecPageTablesSize
),
811 if (mBootMode
!= BOOT_ON_S3_RESUME
) {
812 if (!FeaturePcdGet (PcdSmmSmramRequire
)) {
814 // Reserve the lock box storage area
816 // Since this memory range will be used on S3 resume, it must be
817 // reserved as ACPI NVS.
819 // If S3 is unsupported, then various drivers might still write to the
820 // LockBox area. We ought to prevent DXE from serving allocation requests
821 // such that they would overlap the LockBox storage.
824 (VOID
*)(UINTN
) PcdGet32 (PcdOvmfLockBoxStorageBase
),
825 (UINTN
) PcdGet32 (PcdOvmfLockBoxStorageSize
)
827 BuildMemoryAllocationHob (
828 (EFI_PHYSICAL_ADDRESS
)(UINTN
) PcdGet32 (PcdOvmfLockBoxStorageBase
),
829 (UINT64
)(UINTN
) PcdGet32 (PcdOvmfLockBoxStorageSize
),
830 mS3Supported
? EfiACPIMemoryNVS
: EfiBootServicesData
834 if (FeaturePcdGet (PcdSmmSmramRequire
)) {
838 // Make sure the TSEG area that we reported as a reserved memory resource
839 // cannot be used for reserved memory allocations.
841 TsegSize
= mQ35TsegMbytes
* SIZE_1MB
;
842 BuildMemoryAllocationHob (
843 GetSystemMemorySizeBelow4gb() - TsegSize
,
845 EfiReservedMemoryType