2 Initialize Secure Encrypted Virtualization (SEV) support
4 Copyright (c) 2017, Advanced Micro Devices. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 // The package level header files this module uses
12 #include <Library/DebugLib.h>
13 #include <Library/HobLib.h>
14 #include <Library/MemEncryptSevLib.h>
15 #include <Library/PcdLib.h>
17 #include <Register/Amd/Cpuid.h>
18 #include <Register/Cpuid.h>
24 Function checks if SEV support is available, if present then it sets
25 the dynamic PcdPteMemoryEncryptionAddressOrMask with memory encryption mask.
33 CPUID_MEMORY_ENCRYPTION_INFO_EBX Ebx
;
34 UINT64 EncryptionMask
;
35 RETURN_STATUS PcdStatus
;
38 // Check if SEV is enabled
40 if (!MemEncryptSevIsEnabled ()) {
45 // CPUID Fn8000_001F[EBX] Bit 0:5 (memory encryption bit position)
47 AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO
, NULL
, &Ebx
.Uint32
, NULL
, NULL
);
48 EncryptionMask
= LShiftU64 (1, Ebx
.Bits
.PtePosBits
);
51 // Set Memory Encryption Mask PCD
53 PcdStatus
= PcdSet64S (PcdPteMemoryEncryptionAddressOrMask
, EncryptionMask
);
54 ASSERT_RETURN_ERROR (PcdStatus
);
56 DEBUG ((DEBUG_INFO
, "SEV is enabled (mask 0x%lx)\n", EncryptionMask
));
59 // Set Pcd to Deny the execution of option ROM when security
62 PcdStatus
= PcdSet32S (PcdOptionRomImageVerificationPolicy
, 0x4);
63 ASSERT_RETURN_ERROR (PcdStatus
);
66 // When SMM is required, cover the pages containing the initial SMRAM Save
67 // State Map with a memory allocation HOB:
69 // There's going to be a time interval between our decrypting those pages for
70 // SMBASE relocation and re-encrypting the same pages after SMBASE
71 // relocation. We shall ensure that the DXE phase stay away from those pages
72 // until after re-encryption, in order to prevent an information leak to the
75 if (FeaturePcdGet (PcdSmmSmramRequire
) && (mBootMode
!= BOOT_ON_S3_RESUME
)) {
76 RETURN_STATUS LocateMapStatus
;
80 LocateMapStatus
= MemEncryptSevLocateInitialSmramSaveStateMapPages (
84 ASSERT_RETURN_ERROR (LocateMapStatus
);
86 BuildMemoryAllocationHob (
87 MapPagesBase
, // BaseAddress
88 EFI_PAGES_TO_SIZE (MapPagesCount
), // Length
89 EfiBootServicesData
// MemoryType