3 SEV-SNP Page Validation functions.
5 Copyright (c) 2021 AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Uefi/UefiBaseType.h>
12 #include <Library/BaseLib.h>
13 #include <Library/MemEncryptSevLib.h>
15 #include "SnpPageStateChange.h"
20 } SNP_PRE_VALIDATED_RANGE
;
22 STATIC SNP_PRE_VALIDATED_RANGE mPreValidatedRange
[] = {
23 // The below address range was part of the SEV OVMF metadata, and range
24 // should be pre-validated by the Hypervisor.
26 FixedPcdGet32 (PcdOvmfSecPageTablesBase
),
27 FixedPcdGet32 (PcdOvmfPeiMemFvBase
),
33 DetectPreValidatedOverLap (
34 IN PHYSICAL_ADDRESS StartAddress
,
35 IN PHYSICAL_ADDRESS EndAddress
,
36 OUT SNP_PRE_VALIDATED_RANGE
*OverlapRange
42 // Check if the specified address range exist in pre-validated array.
44 for (i
= 0; i
< ARRAY_SIZE (mPreValidatedRange
); i
++) {
45 if ((mPreValidatedRange
[i
].StartAddress
< EndAddress
) &&
46 (StartAddress
< mPreValidatedRange
[i
].EndAddress
))
48 OverlapRange
->StartAddress
= mPreValidatedRange
[i
].StartAddress
;
49 OverlapRange
->EndAddress
= mPreValidatedRange
[i
].EndAddress
;
58 Pre-validate the system RAM when SEV-SNP is enabled in the guest VM.
60 @param[in] BaseAddress Base address
61 @param[in] NumPages Number of pages starting from the base address
66 MemEncryptSevSnpPreValidateSystemRam (
67 IN PHYSICAL_ADDRESS BaseAddress
,
71 PHYSICAL_ADDRESS EndAddress
;
72 SNP_PRE_VALIDATED_RANGE OverlapRange
;
74 if (!MemEncryptSevSnpIsEnabled ()) {
78 EndAddress
= BaseAddress
+ EFI_PAGES_TO_SIZE (NumPages
);
80 while (BaseAddress
< EndAddress
) {
82 // Check if the range overlaps with the pre-validated ranges.
84 if (DetectPreValidatedOverLap (BaseAddress
, EndAddress
, &OverlapRange
)) {
85 // Validate the non-overlap regions.
86 if (BaseAddress
< OverlapRange
.StartAddress
) {
87 NumPages
= EFI_SIZE_TO_PAGES (OverlapRange
.StartAddress
- BaseAddress
);
89 InternalSetPageState (BaseAddress
, NumPages
, SevSnpPagePrivate
, TRUE
);
92 BaseAddress
= OverlapRange
.EndAddress
;
96 // Validate the remaining pages.
97 NumPages
= EFI_SIZE_TO_PAGES (EndAddress
- BaseAddress
);
98 InternalSetPageState (BaseAddress
, NumPages
, SevSnpPagePrivate
, TRUE
);
99 BaseAddress
= EndAddress
;