summaryrefslogtreecommitdiff
path: root/MdePkg/Library/BaseLib/X64/SevProbe.c
blob: 2d1ab01f72706edc3e6a50a97c5a2aef26be21f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/** @file

  Copyright (c) 2025, Advanced Micro Devices, Inc. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Library/BaseLib.h>
#include <Register/Amd/Cpuid.h>
#include <Register/Amd/SevSnpMsr.h>
#include <Register/Intel/Cpuid.h>

/**
  Probe if running as some kind of SEV guest.

  @return FALSE   Not running as a guest under any kind of SEV
  @return TRUE    Running as a guest under any kind of SEV
**/
BOOLEAN
EFIAPI
SevGuestIsEnabled (
  VOID
  )
{
  UINT32                            MaxExtendedLeaf;
  MSR_SEV_STATUS_REGISTER           SevStatus;
  CPUID_MEMORY_ENCRYPTION_INFO_EAX  MemoryEncryptionInfoEax;

  // Check max extended CPUID leaf
  AsmCpuid (CPUID_EXTENDED_FUNCTION, &MaxExtendedLeaf, NULL, NULL, NULL);
  if (MaxExtendedLeaf < CPUID_MEMORY_ENCRYPTION_INFO) {
    return FALSE;
  }

  // Check SEV support
  AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, &MemoryEncryptionInfoEax.Uint32, NULL, NULL, NULL);
  if (MemoryEncryptionInfoEax.Bits.SevBit == 0) {
    return FALSE;
  }

  // Read SEV_STATUS MSR
  SevStatus.Uint64 = AsmReadMsr64 (MSR_SEV_STATUS);

  return (SevStatus.Bits.SevBit | SevStatus.Bits.SevEsBit | SevStatus.Bits.SevSnpBit) ? TRUE : FALSE;
}