SecMemEncryptSevLibInternal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /** @file
  2. Secure Encrypted Virtualization (SEV) library helper function
  3. Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Library/BaseLib.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/MemEncryptSevLib.h>
  9. #include <Library/PcdLib.h>
  10. #include <Register/Amd/Cpuid.h>
  11. #include <Register/Amd/Msr.h>
  12. #include <Register/Cpuid.h>
  13. #include <Uefi/UefiBaseType.h>
  14. /**
  15. Read the workarea to determine whether SEV is enabled. If enabled,
  16. then return the SevEsWorkArea pointer.
  17. **/
  18. STATIC
  19. SEC_SEV_ES_WORK_AREA *
  20. EFIAPI
  21. GetSevEsWorkArea (
  22. VOID
  23. )
  24. {
  25. OVMF_WORK_AREA *WorkArea;
  26. WorkArea = (OVMF_WORK_AREA *)FixedPcdGet32 (PcdOvmfWorkAreaBase);
  27. //
  28. // If its not SEV guest then SevEsWorkArea is not valid.
  29. //
  30. if ((WorkArea == NULL) || (WorkArea->Header.GuestType != CcGuestTypeAmdSev)) {
  31. return NULL;
  32. }
  33. return (SEC_SEV_ES_WORK_AREA *)FixedPcdGet32 (PcdSevEsWorkAreaBase);
  34. }
  35. /**
  36. Read the SEV Status MSR value from the workarea
  37. **/
  38. STATIC
  39. UINT32
  40. EFIAPI
  41. InternalMemEncryptSevStatus (
  42. VOID
  43. )
  44. {
  45. SEC_SEV_ES_WORK_AREA *SevEsWorkArea;
  46. SevEsWorkArea = GetSevEsWorkArea ();
  47. if (SevEsWorkArea == NULL) {
  48. return 0;
  49. }
  50. return (UINT32)(UINTN)SevEsWorkArea->SevStatusMsrValue;
  51. }
  52. /**
  53. Returns a boolean to indicate whether SEV-SNP is enabled.
  54. @retval TRUE SEV-SNP is enabled
  55. @retval FALSE SEV-SNP is not enabled
  56. **/
  57. BOOLEAN
  58. EFIAPI
  59. MemEncryptSevSnpIsEnabled (
  60. VOID
  61. )
  62. {
  63. MSR_SEV_STATUS_REGISTER Msr;
  64. Msr.Uint32 = InternalMemEncryptSevStatus ();
  65. return Msr.Bits.SevSnpBit ? TRUE : FALSE;
  66. }
  67. /**
  68. Returns a boolean to indicate whether SEV-ES is enabled.
  69. @retval TRUE SEV-ES is enabled
  70. @retval FALSE SEV-ES is not enabled
  71. **/
  72. BOOLEAN
  73. EFIAPI
  74. MemEncryptSevEsIsEnabled (
  75. VOID
  76. )
  77. {
  78. MSR_SEV_STATUS_REGISTER Msr;
  79. Msr.Uint32 = InternalMemEncryptSevStatus ();
  80. return Msr.Bits.SevEsBit ? TRUE : FALSE;
  81. }
  82. /**
  83. Returns a boolean to indicate whether SEV is enabled.
  84. @retval TRUE SEV is enabled
  85. @retval FALSE SEV is not enabled
  86. **/
  87. BOOLEAN
  88. EFIAPI
  89. MemEncryptSevIsEnabled (
  90. VOID
  91. )
  92. {
  93. MSR_SEV_STATUS_REGISTER Msr;
  94. Msr.Uint32 = InternalMemEncryptSevStatus ();
  95. return Msr.Bits.SevBit ? TRUE : FALSE;
  96. }
  97. /**
  98. Returns the SEV encryption mask.
  99. @return The SEV pagtable encryption mask
  100. **/
  101. UINT64
  102. EFIAPI
  103. MemEncryptSevGetEncryptionMask (
  104. VOID
  105. )
  106. {
  107. SEC_SEV_ES_WORK_AREA *SevEsWorkArea;
  108. SevEsWorkArea = GetSevEsWorkArea ();
  109. if (SevEsWorkArea == NULL) {
  110. return 0;
  111. }
  112. return SevEsWorkArea->EncryptionMask;
  113. }
  114. /**
  115. Locate the page range that covers the initial (pre-SMBASE-relocation) SMRAM
  116. Save State Map.
  117. @param[out] BaseAddress The base address of the lowest-address page that
  118. covers the initial SMRAM Save State Map.
  119. @param[out] NumberOfPages The number of pages in the page range that covers
  120. the initial SMRAM Save State Map.
  121. @retval RETURN_SUCCESS BaseAddress and NumberOfPages have been set on
  122. output.
  123. @retval RETURN_UNSUPPORTED SMM is unavailable.
  124. **/
  125. RETURN_STATUS
  126. EFIAPI
  127. MemEncryptSevLocateInitialSmramSaveStateMapPages (
  128. OUT UINTN *BaseAddress,
  129. OUT UINTN *NumberOfPages
  130. )
  131. {
  132. return RETURN_UNSUPPORTED;
  133. }