MemTypeInfo.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /** @file
  2. Produce the memory type information HOB.
  3. Copyright (C) 2017-2020, Red Hat, Inc.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Guid/MemoryTypeInformation.h>
  7. #include <Library/BaseLib.h>
  8. #include <Library/DebugLib.h>
  9. #include <Library/HobLib.h>
  10. #include <Library/PcdLib.h>
  11. #include <Library/PeiServicesLib.h>
  12. #include <Ppi/ReadOnlyVariable2.h>
  13. #include <Uefi/UefiMultiPhase.h>
  14. #include "Platform.h"
  15. #define MEMORY_TYPE_INFO_DEFAULT(Type) \
  16. { Type, FixedPcdGet32 (PcdMemoryType ## Type) }
  17. STATIC EFI_MEMORY_TYPE_INFORMATION mMemoryTypeInformation[] = {
  18. MEMORY_TYPE_INFO_DEFAULT (EfiACPIMemoryNVS),
  19. MEMORY_TYPE_INFO_DEFAULT (EfiACPIReclaimMemory),
  20. MEMORY_TYPE_INFO_DEFAULT (EfiReservedMemoryType),
  21. MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesCode),
  22. MEMORY_TYPE_INFO_DEFAULT (EfiRuntimeServicesData),
  23. { EfiMaxMemoryType, 0}
  24. };
  25. STATIC
  26. VOID
  27. BuildMemTypeInfoHob (
  28. VOID
  29. )
  30. {
  31. BuildGuidDataHob (
  32. &gEfiMemoryTypeInformationGuid,
  33. mMemoryTypeInformation,
  34. sizeof mMemoryTypeInformation
  35. );
  36. }
  37. /**
  38. Refresh the mMemoryTypeInformation array (which we'll turn into the
  39. MemoryTypeInformation HOB) from the MemoryTypeInformation UEFI variable.
  40. Normally, the DXE IPL PEIM builds the HOB from the UEFI variable. But it does
  41. so *transparently*. Instead, we consider the UEFI variable as a list of
  42. hints, for updating our HOB defaults:
  43. - Record types not covered in mMemoryTypeInformation are ignored. In
  44. particular, this hides record types from the UEFI variable that may lead to
  45. reboots without benefiting SMM security, such as EfiBootServicesData.
  46. - Records that would lower the defaults in mMemoryTypeInformation are also
  47. ignored.
  48. @param[in] ReadOnlyVariable2 The EFI_PEI_READ_ONLY_VARIABLE2_PPI used for
  49. retrieving the MemoryTypeInformation UEFI
  50. variable.
  51. **/
  52. STATIC
  53. VOID
  54. RefreshMemTypeInfo (
  55. IN EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable2
  56. )
  57. {
  58. UINTN DataSize;
  59. EFI_MEMORY_TYPE_INFORMATION Entries[EfiMaxMemoryType + 1];
  60. EFI_STATUS Status;
  61. UINTN NumEntries;
  62. UINTN HobRecordIdx;
  63. //
  64. // Read the MemoryTypeInformation UEFI variable from the
  65. // gEfiMemoryTypeInformationGuid namespace.
  66. //
  67. DataSize = sizeof Entries;
  68. Status = ReadOnlyVariable2->GetVariable (
  69. ReadOnlyVariable2,
  70. EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
  71. &gEfiMemoryTypeInformationGuid,
  72. NULL,
  73. &DataSize,
  74. Entries
  75. );
  76. if (EFI_ERROR (Status)) {
  77. //
  78. // If the UEFI variable does not exist (EFI_NOT_FOUND), we can't use it for
  79. // udpating mMemoryTypeInformation.
  80. //
  81. // If the UEFI variable exists but Entries is too small to hold it
  82. // (EFI_BUFFER_TOO_SMALL), then the variable contents are arguably invalid.
  83. // That's because Entries has room for every distinct EFI_MEMORY_TYPE,
  84. // including the terminator record with EfiMaxMemoryType. Thus, we can't
  85. // use the UEFI variable for updating mMemoryTypeInformation.
  86. //
  87. // If the UEFI variable couldn't be read for some other reason, we
  88. // similarly can't use it for udpating mMemoryTypeInformation.
  89. //
  90. DEBUG ((DEBUG_ERROR, "%a: GetVariable(): %r\n", __FUNCTION__, Status));
  91. return;
  92. }
  93. //
  94. // Sanity-check the UEFI variable size against the record size.
  95. //
  96. if (DataSize % sizeof Entries[0] != 0) {
  97. DEBUG ((
  98. DEBUG_ERROR,
  99. "%a: invalid UEFI variable size %Lu\n",
  100. __FUNCTION__,
  101. (UINT64)DataSize
  102. ));
  103. return;
  104. }
  105. NumEntries = DataSize / sizeof Entries[0];
  106. //
  107. // For each record in mMemoryTypeInformation, except the terminator record,
  108. // look up the first match (if any) in the UEFI variable, based on the memory
  109. // type.
  110. //
  111. for (HobRecordIdx = 0;
  112. HobRecordIdx < ARRAY_SIZE (mMemoryTypeInformation) - 1;
  113. HobRecordIdx++)
  114. {
  115. EFI_MEMORY_TYPE_INFORMATION *HobRecord;
  116. UINTN Idx;
  117. EFI_MEMORY_TYPE_INFORMATION *VariableRecord;
  118. HobRecord = &mMemoryTypeInformation[HobRecordIdx];
  119. for (Idx = 0; Idx < NumEntries; Idx++) {
  120. VariableRecord = &Entries[Idx];
  121. if (VariableRecord->Type == HobRecord->Type) {
  122. break;
  123. }
  124. }
  125. //
  126. // If there is a match, allow the UEFI variable to increase NumberOfPages.
  127. //
  128. if ((Idx < NumEntries) &&
  129. (HobRecord->NumberOfPages < VariableRecord->NumberOfPages))
  130. {
  131. DEBUG ((
  132. DEBUG_VERBOSE,
  133. "%a: Type 0x%x: NumberOfPages 0x%x -> 0x%x\n",
  134. __FUNCTION__,
  135. HobRecord->Type,
  136. HobRecord->NumberOfPages,
  137. VariableRecord->NumberOfPages
  138. ));
  139. HobRecord->NumberOfPages = VariableRecord->NumberOfPages;
  140. }
  141. }
  142. }
  143. /**
  144. Notification function called when EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes
  145. available.
  146. @param[in] PeiServices Indirect reference to the PEI Services Table.
  147. @param[in] NotifyDescriptor Address of the notification descriptor data
  148. structure.
  149. @param[in] Ppi Address of the PPI that was installed.
  150. @return Status of the notification. The status code returned from this
  151. function is ignored.
  152. **/
  153. STATIC
  154. EFI_STATUS
  155. EFIAPI
  156. OnReadOnlyVariable2Available (
  157. IN EFI_PEI_SERVICES **PeiServices,
  158. IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
  159. IN VOID *Ppi
  160. )
  161. {
  162. DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
  163. RefreshMemTypeInfo (Ppi);
  164. BuildMemTypeInfoHob ();
  165. return EFI_SUCCESS;
  166. }
  167. //
  168. // Notification object for registering the callback, for when
  169. // EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.
  170. //
  171. STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify = {
  172. (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH |
  173. EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), // Flags
  174. &gEfiPeiReadOnlyVariable2PpiGuid, // Guid
  175. OnReadOnlyVariable2Available // Notify
  176. };
  177. VOID
  178. MemTypeInfoInitialization (
  179. IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob
  180. )
  181. {
  182. EFI_STATUS Status;
  183. if (!PlatformInfoHob->SmmSmramRequire) {
  184. //
  185. // EFI_PEI_READ_ONLY_VARIABLE2_PPI will never be available; install
  186. // the default memory type information HOB right away.
  187. //
  188. BuildMemTypeInfoHob ();
  189. return;
  190. }
  191. Status = PeiServicesNotifyPpi (&mReadOnlyVariable2Notify);
  192. if (EFI_ERROR (Status)) {
  193. DEBUG ((
  194. DEBUG_ERROR,
  195. "%a: failed to set up R/O Variable 2 callback: %r\n",
  196. __FUNCTION__,
  197. Status
  198. ));
  199. ASSERT (FALSE);
  200. CpuDeadLoop ();
  201. }
  202. }