]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c
OvmfPkg/BaseMemcryptSevLib: Add SEV helper library
[mirror_edk2.git] / OvmfPkg / Library / BaseMemEncryptSevLib / MemEncryptSevLibInternal.c
diff --git a/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c b/OvmfPkg/Library/BaseMemEncryptSevLib/MemEncryptSevLibInternal.c
new file mode 100644 (file)
index 0000000..002f079
--- /dev/null
@@ -0,0 +1,90 @@
+/** @file\r
+\r
+  Secure Encrypted Virtualization (SEV) library helper function\r
+\r
+  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD\r
+  License which accompanies this distribution.  The full text of the license may\r
+  be found at http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Register/Cpuid.h>\r
+#include <Register/Amd/Cpuid.h>\r
+#include <Register/Amd/Msr.h>\r
+#include <Library/MemEncryptSevLib.h>\r
+\r
+STATIC BOOLEAN mSevStatus = FALSE;\r
+STATIC BOOLEAN mSevStatusChecked = FALSE;\r
+\r
+/**\r
+\r
+  Returns a boolean to indicate whether SEV is enabled\r
+\r
+  @retval TRUE           SEV is enabled\r
+  @retval FALSE          SEV is not enabled\r
+  **/\r
+STATIC\r
+BOOLEAN\r
+EFIAPI\r
+InternalMemEncryptSevIsEnabled (\r
+  VOID\r
+  )\r
+{\r
+  UINT32                            RegEax;\r
+  MSR_SEV_STATUS_REGISTER           Msr;\r
+  CPUID_MEMORY_ENCRYPTION_INFO_EAX  Eax;\r
+\r
+  //\r
+  // Check if memory encryption leaf exist\r
+  //\r
+  AsmCpuid (CPUID_EXTENDED_FUNCTION, &RegEax, NULL, NULL, NULL);\r
+  if (RegEax >= CPUID_MEMORY_ENCRYPTION_INFO) {\r
+    //\r
+    // CPUID Fn8000_001F[EAX] Bit 1 (Sev supported)\r
+    //\r
+    AsmCpuid (CPUID_MEMORY_ENCRYPTION_INFO, &Eax.Uint32, NULL, NULL, NULL);\r
+\r
+    if (Eax.Bits.SevBit) {\r
+      //\r
+      // Check MSR_0xC0010131 Bit 0 (Sev Enabled)\r
+      //\r
+      Msr.Uint32 = AsmReadMsr32 (MSR_SEV_STATUS);\r
+      if (Msr.Bits.SevBit) {\r
+        return TRUE;\r
+      }\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+\r
+  Returns a boolean to indicate whether SEV is enabled\r
+\r
+  @retval TRUE           SEV is enabled\r
+  @retval FALSE          SEV is not enabled\r
+  **/\r
+BOOLEAN\r
+EFIAPI\r
+MemEncryptSevIsEnabled (\r
+  VOID\r
+  )\r
+{\r
+  if (mSevStatusChecked) {\r
+    return mSevStatus;\r
+  }\r
+\r
+  mSevStatus = InternalMemEncryptSevIsEnabled();\r
+  mSevStatusChecked = TRUE;\r
+\r
+  return mSevStatus;\r
+}\r