3 Secure Encrypted Virtualization (SEV) library helper function
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/BaseLib.h>
12 #include <Library/DebugLib.h>
13 #include <Library/MemEncryptSevLib.h>
14 #include <Library/PcdLib.h>
15 #include <Register/Amd/Cpuid.h>
16 #include <Register/Amd/Msr.h>
17 #include <Register/Cpuid.h>
18 #include <Register/QemuSmramSaveStateMap.h>
19 #include <Register/SmramSaveStateMap.h>
20 #include <Uefi/UefiBaseType.h>
22 STATIC BOOLEAN mSevStatus
= FALSE
;
23 STATIC BOOLEAN mSevStatusChecked
= FALSE
;
27 Returns a boolean to indicate whether SEV is enabled
29 @retval TRUE SEV is enabled
30 @retval FALSE SEV is not enabled
35 InternalMemEncryptSevIsEnabled (
40 MSR_SEV_STATUS_REGISTER Msr
;
41 CPUID_MEMORY_ENCRYPTION_INFO_EAX Eax
;
44 // Check if memory encryption leaf exist
46 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &RegEax
, NULL
, NULL
, NULL
);
47 if (RegEax
>= CPUID_MEMORY_ENCRYPTION_INFO
) {
49 // CPUID Fn8000_001F[EAX] Bit 1 (Sev supported)
51 AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO
, &Eax
.Uint32
, NULL
, NULL
, NULL
);
53 if (Eax
.Bits
.SevBit
) {
55 // Check MSR_0xC0010131 Bit 0 (Sev Enabled)
57 Msr
.Uint32
= AsmReadMsr32 (MSR_SEV_STATUS
);
58 if (Msr
.Bits
.SevBit
) {
68 Returns a boolean to indicate whether SEV is enabled
70 @retval TRUE SEV is enabled
71 @retval FALSE SEV is not enabled
75 MemEncryptSevIsEnabled (
79 if (mSevStatusChecked
) {
83 mSevStatus
= InternalMemEncryptSevIsEnabled();
84 mSevStatusChecked
= TRUE
;
91 Locate the page range that covers the initial (pre-SMBASE-relocation) SMRAM
94 @param[out] BaseAddress The base address of the lowest-address page that
95 covers the initial SMRAM Save State Map.
97 @param[out] NumberOfPages The number of pages in the page range that covers
98 the initial SMRAM Save State Map.
100 @retval RETURN_SUCCESS BaseAddress and NumberOfPages have been set on
103 @retval RETURN_UNSUPPORTED SMM is unavailable.
107 MemEncryptSevLocateInitialSmramSaveStateMapPages (
108 OUT UINTN
*BaseAddress
,
109 OUT UINTN
*NumberOfPages
114 UINTN MapPagesStart
; // MapStart rounded down to page boundary
115 UINTN MapPagesEnd
; // MapEnd rounded up to page boundary
116 UINTN MapPagesSize
; // difference between MapPagesStart and MapPagesEnd
118 if (!FeaturePcdGet (PcdSmmSmramRequire
)) {
119 return RETURN_UNSUPPORTED
;
122 MapStart
= SMM_DEFAULT_SMBASE
+ SMRAM_SAVE_STATE_MAP_OFFSET
;
123 MapEnd
= MapStart
+ sizeof (QEMU_SMRAM_SAVE_STATE_MAP
);
124 MapPagesStart
= MapStart
& ~(UINTN
)EFI_PAGE_MASK
;
125 MapPagesEnd
= ALIGN_VALUE (MapEnd
, EFI_PAGE_SIZE
);
126 MapPagesSize
= MapPagesEnd
- MapPagesStart
;
128 ASSERT ((MapPagesSize
& EFI_PAGE_MASK
) == 0);
130 *BaseAddress
= MapPagesStart
;
131 *NumberOfPages
= MapPagesSize
>> EFI_PAGE_SHIFT
;
133 return RETURN_SUCCESS
;