TianoCore EDK2 master
Loading...
Searching...
No Matches
MemDetect.c File Reference
#include <IndustryStandard/E820.h>
#include <IndustryStandard/I440FxPiix4.h>
#include <IndustryStandard/Q35MchIch9.h>
#include <IndustryStandard/CloudHv.h>
#include <IndustryStandard/Xen/arch-x86/hvm/start_info.h>
#include <PiPei.h>
#include <Register/Intel/SmramSaveStateMap.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/CcProbeLib.h>
#include <Library/DebugLib.h>
#include <Library/HardwareInfoLib.h>
#include <Library/HobLib.h>
#include <Library/IoLib.h>
#include <Library/MemEncryptSevLib.h>
#include <Library/PcdLib.h>
#include <Library/PciLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/ResourcePublicationLib.h>
#include <Library/MtrrLib.h>
#include <Library/QemuFwCfgLib.h>
#include <Library/QemuFwCfgSimpleParserLib.h>
#include <Library/TdxLib.h>
#include <Library/PlatformInitLib.h>
#include <Guid/AcpiS3Context.h>
#include <Guid/SmramMemoryReserve.h>

Go to the source code of this file.

Macros

#define MEGABYTE_SHIFT   20
 

Typedefs

typedef VOID(* E820_SCAN_CALLBACK) (EFI_E820_ENTRY64 *E820Entry, EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 

Functions

VOID EFIAPI PlatformQemuUc32BaseInitialization (IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC EFI_STATUS PlatformScanE820Tdx (IN E820_SCAN_CALLBACK Callback, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC VOID PlatformGetFirstNonAddressCB (IN EFI_E820_ENTRY64 *E820Entry, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC VOID PlatformGetLowMemoryCB (IN EFI_E820_ENTRY64 *E820Entry, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC VOID PlatformAddHobCB (IN EFI_E820_ENTRY64 *E820Entry, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC VOID PlatformReservationConflictCB (IN EFI_E820_ENTRY64 *E820Entry, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
EFI_STATUS GetPvhMemmapEntries (struct hvm_memmap_table_entry **Entries, UINT32 *Count)
 
STATIC EFI_STATUS PlatformScanE820Pvh (IN E820_SCAN_CALLBACK Callback, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC EFI_STATUS PlatformScanE820 (IN E820_SCAN_CALLBACK Callback, IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC UINT64 GetHighestSystemMemoryAddressFromPvhMemmap (BOOLEAN Below4gb)
 
VOID EFIAPI PlatformGetSystemMemorySizeBelow4gb (IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC UINT64 PlatformGetSystemMemorySizeAbove4gb ()
 
STATIC VOID PlatformGetFirstNonAddress (IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
VOID EFIAPI PlatformAddressWidthFromCpuid (IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob, IN BOOLEAN QemuQuirk)
 
VOID EFIAPI PlatformDynamicMmioWindow (IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC EFI_STATUS PlatformScanHostProvided64BitPciMmioEnd (OUT UINT64 *PciMmioAddressEnd)
 
VOID EFIAPI Switch4Level (VOID)
 
STATIC VOID PlatformSetupPagingLevel (IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
VOID EFIAPI PlatformAddressWidthInitialization (IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
STATIC VOID CreateSmmSmramMemoryHob (IN EFI_PHYSICAL_ADDRESS StartAddress, IN UINT32 Size)
 
STATIC VOID QemuInitializeRamBelow1gb (IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
VOID EFIAPI PlatformQemuInitializeRam (IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 
VOID EFIAPI PlatformQemuInitializeRamForS3 (IN EFI_HOB_PLATFORM_INFO *PlatformInfoHob)
 

Detailed Description

Memory Detection for Virtual Machines.

Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent

Module Name:

MemDetect.c

Definition in file MemDetect.c.

Macro Definition Documentation

◆ MEGABYTE_SHIFT

#define MEGABYTE_SHIFT   20

Definition at line 49 of file MemDetect.c.

Typedef Documentation

◆ E820_SCAN_CALLBACK

typedef VOID(* E820_SCAN_CALLBACK) (EFI_E820_ENTRY64 *E820Entry, EFI_HOB_PLATFORM_INFO *PlatformInfoHob)

Definition at line 105 of file MemDetect.c.

Function Documentation

◆ CreateSmmSmramMemoryHob()

STATIC VOID CreateSmmSmramMemoryHob ( IN EFI_PHYSICAL_ADDRESS  StartAddress,
IN UINT32  Size 
)

Create gEfiSmmSmramMemoryGuid HOB defined in the PI specification Vol. 3, section 5, which is used to describe the SMRAM memory regions supported by the platform.

Parameters
[in]StartAddressStartAddress of smram.
[in]SizeSize of smram.

Definition at line 1151 of file MemDetect.c.

◆ GetHighestSystemMemoryAddressFromPvhMemmap()

STATIC UINT64 GetHighestSystemMemoryAddressFromPvhMemmap ( BOOLEAN  Below4gb)

Definition at line 404 of file MemDetect.c.

◆ GetPvhMemmapEntries()

EFI_STATUS GetPvhMemmapEntries ( struct hvm_memmap_table_entry **  Entries,
UINT32 *  Count 
)

Returns PVH memmap

Parameters
EntriesPointer to PVH memmap
CountNumber of entries
Returns
EFI_STATUS

Definition at line 291 of file MemDetect.c.

◆ PlatformAddHobCB()

STATIC VOID PlatformAddHobCB ( IN EFI_E820_ENTRY64 E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Create HOBs for reservations and RAM (except low memory).

Definition at line 197 of file MemDetect.c.

◆ PlatformAddressWidthFromCpuid()

VOID EFIAPI PlatformAddressWidthFromCpuid ( IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob,
IN BOOLEAN  QemuQuirk 
)

Definition at line 663 of file MemDetect.c.

◆ PlatformAddressWidthInitialization()

VOID EFIAPI PlatformAddressWidthInitialization ( IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Initialize the PhysMemAddressWidth field in PlatformInfoHob based on guest RAM size.

Definition at line 1045 of file MemDetect.c.

◆ PlatformDynamicMmioWindow()

VOID EFIAPI PlatformDynamicMmioWindow ( IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Definition at line 789 of file MemDetect.c.

◆ PlatformGetFirstNonAddress()

STATIC VOID PlatformGetFirstNonAddress ( IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Return the highest address that DXE could possibly use, plus one.

Definition at line 506 of file MemDetect.c.

◆ PlatformGetFirstNonAddressCB()

STATIC VOID PlatformGetFirstNonAddressCB ( IN EFI_E820_ENTRY64 E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Store first address not used by e820 RAM entries in PlatformInfoHob->FirstNonAddress

Definition at line 146 of file MemDetect.c.

◆ PlatformGetLowMemoryCB()

STATIC VOID PlatformGetLowMemoryCB ( IN EFI_E820_ENTRY64 E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Store the low (below 4G) memory size in PlatformInfoHob->LowMemory

Definition at line 170 of file MemDetect.c.

◆ PlatformGetSystemMemorySizeAbove4gb()

STATIC UINT64 PlatformGetSystemMemorySizeAbove4gb ( )

Definition at line 479 of file MemDetect.c.

◆ PlatformGetSystemMemorySizeBelow4gb()

VOID EFIAPI PlatformGetSystemMemorySizeBelow4gb ( IN EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Definition at line 441 of file MemDetect.c.

◆ PlatformQemuInitializeRam()

VOID EFIAPI PlatformQemuInitializeRam ( IN EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Peform Memory Detection for QEMU / KVM

Definition at line 1231 of file MemDetect.c.

◆ PlatformQemuInitializeRamForS3()

VOID EFIAPI PlatformQemuInitializeRamForS3 ( IN EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Definition at line 1358 of file MemDetect.c.

◆ PlatformQemuUc32BaseInitialization()

VOID EFIAPI PlatformQemuUc32BaseInitialization ( IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Definition at line 53 of file MemDetect.c.

◆ PlatformReservationConflictCB()

STATIC VOID PlatformReservationConflictCB ( IN EFI_E820_ENTRY64 E820Entry,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Check whenever the 64bit PCI MMIO window overlaps with a reservation from qemu. If so move down the MMIO window to resolve the conflict.

This happens on (virtual) AMD machines with 1TB address space, because the AMD IOMMU uses an address window just below 1TB.

Definition at line 248 of file MemDetect.c.

◆ PlatformScanE820()

STATIC EFI_STATUS PlatformScanE820 ( IN E820_SCAN_CALLBACK  Callback,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Iterate over the entries in QEMU's fw_cfg E820 RAM map, call the passed callback for each entry.

Parameters
[in]CallbackThe callback function to be called.
[in,out]PlatformInfoHobPlatformInfo struct which is passed through to the callback.
Return values
EFI_SUCCESSThe fw_cfg E820 RAM map was found and processed.
EFI_PROTOCOL_ERRORThe RAM map was found, but its size wasn't a whole multiple of sizeof(EFI_E820_ENTRY64). No RAM entry was processed.
Returns
Error codes from QemuFwCfgFindFile(). No RAM entry was processed.

Definition at line 365 of file MemDetect.c.

◆ PlatformScanE820Pvh()

STATIC EFI_STATUS PlatformScanE820Pvh ( IN E820_SCAN_CALLBACK  Callback,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Definition at line 314 of file MemDetect.c.

◆ PlatformScanE820Tdx()

STATIC EFI_STATUS PlatformScanE820Tdx ( IN E820_SCAN_CALLBACK  Callback,
IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob 
)

Definition at line 112 of file MemDetect.c.

◆ PlatformScanHostProvided64BitPciMmioEnd()

STATIC EFI_STATUS PlatformScanHostProvided64BitPciMmioEnd ( OUT UINT64 *  PciMmioAddressEnd)

Iterate over the PCI host bridges resources information optionally provided in fw-cfg and find the highest address contained in the PCI MMIO windows. If the information is found, return the exclusive end; one past the last usable address.

Parameters
[out]PciMmioAddressEndPointer to one-after End Address updated with information extracted from host-provided data or zero if no information available or an error happened
Return values
EFI_SUCCESSPCI information was read and the output parameter updated with the last valid address in the 64-bit MMIO range.
EFI_INVALID_PARAMETERPointer parameter is invalid
EFI_INCOMPATIBLE_VERSIONHardware information found in fw-cfg has an incompatible format
EFI_UNSUPPORTEDFw-cfg is not supported, thus host provided information, if any, cannot be read
EFI_NOT_FOUNDNo PCI host bridge information provided by the host.

Definition at line 840 of file MemDetect.c.

◆ PlatformSetupPagingLevel()

STATIC VOID PlatformSetupPagingLevel ( IN OUT EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Configure x64 paging levels.

The OVMF ResetVector code will enter long mode with 5-level paging if the following conditions are true:

(1) OVMF has been built with PcdUse5LevelPageTable = TRUE, and (2) the CPU supports 5-level paging (aka la57), and (3) the CPU supports gigabyte pages, and (4) the VM is not running in SEV mode.

Condition (4) is a temporary stopgap for BaseMemEncryptSevLib not supporting 5-level paging yet.

This function looks at the virtual machine configuration, then decides whenever it will continue to use 5-level paging or downgrade to 4-level paging for better compatibility with older guest OS versions.

There is a fw_cfg config option to explicitly request 4 or 5-level paging using 'qemu -fw_cfg name=opt/org.tianocode/PagingLevel,string=4|5'. If the option is present the requested paging level will be used.

Should that not be the case the function checks the size of the address space needed, which is the RAM installed plus fw_cfg reservations. The downgrade to 4-level paging will happen for small guests where the address space needed is lower than 1TB.

This function will also log the paging level used and the reason for that.

Definition at line 975 of file MemDetect.c.

◆ QemuInitializeRamBelow1gb()

STATIC VOID QemuInitializeRamBelow1gb ( IN EFI_HOB_PLATFORM_INFO PlatformInfoHob)

Definition at line 1201 of file MemDetect.c.