HighMemDxe.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /** @file
  2. * High memory node enumeration DXE driver for ARM and RISC-V
  3. * Virtual Machines
  4. *
  5. * Copyright (c) 2015-2016, Linaro Ltd. All rights reserved.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause-Patent
  8. *
  9. **/
  10. #include <Library/BaseLib.h>
  11. #include <Library/DebugLib.h>
  12. #include <Library/DxeServicesTableLib.h>
  13. #include <Library/PcdLib.h>
  14. #include <Library/UefiBootServicesTableLib.h>
  15. #include <Protocol/Cpu.h>
  16. #include <Protocol/FdtClient.h>
  17. EFI_STATUS
  18. EFIAPI
  19. InitializeHighMemDxe (
  20. IN EFI_HANDLE ImageHandle,
  21. IN EFI_SYSTEM_TABLE *SystemTable
  22. )
  23. {
  24. FDT_CLIENT_PROTOCOL *FdtClient;
  25. EFI_CPU_ARCH_PROTOCOL *Cpu;
  26. EFI_STATUS Status, FindNodeStatus;
  27. INT32 Node;
  28. CONST UINT32 *Reg;
  29. UINT32 RegSize;
  30. UINTN AddressCells, SizeCells;
  31. UINT64 CurBase;
  32. UINT64 CurSize;
  33. UINT64 Attributes;
  34. EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;
  35. Status = gBS->LocateProtocol (
  36. &gFdtClientProtocolGuid,
  37. NULL,
  38. (VOID **)&FdtClient
  39. );
  40. ASSERT_EFI_ERROR (Status);
  41. Status = gBS->LocateProtocol (
  42. &gEfiCpuArchProtocolGuid,
  43. NULL,
  44. (VOID **)&Cpu
  45. );
  46. ASSERT_EFI_ERROR (Status);
  47. //
  48. // Check for memory node and add the memory spaces except the lowest one
  49. //
  50. for (FindNodeStatus = FdtClient->FindMemoryNodeReg (
  51. FdtClient,
  52. &Node,
  53. (CONST VOID **)&Reg,
  54. &AddressCells,
  55. &SizeCells,
  56. &RegSize
  57. );
  58. !EFI_ERROR (FindNodeStatus);
  59. FindNodeStatus = FdtClient->FindNextMemoryNodeReg (
  60. FdtClient,
  61. Node,
  62. &Node,
  63. (CONST VOID **)&Reg,
  64. &AddressCells,
  65. &SizeCells,
  66. &RegSize
  67. ))
  68. {
  69. ASSERT (AddressCells <= 2);
  70. ASSERT (SizeCells <= 2);
  71. while (RegSize > 0) {
  72. CurBase = SwapBytes32 (*Reg++);
  73. if (AddressCells > 1) {
  74. CurBase = (CurBase << 32) | SwapBytes32 (*Reg++);
  75. }
  76. CurSize = SwapBytes32 (*Reg++);
  77. if (SizeCells > 1) {
  78. CurSize = (CurSize << 32) | SwapBytes32 (*Reg++);
  79. }
  80. RegSize -= (AddressCells + SizeCells) * sizeof (UINT32);
  81. Status = gDS->GetMemorySpaceDescriptor (CurBase, &GcdDescriptor);
  82. if (EFI_ERROR (Status)) {
  83. DEBUG ((
  84. DEBUG_WARN,
  85. "%a: Region 0x%lx - 0x%lx not found in the GCD memory space map\n",
  86. __FUNCTION__,
  87. CurBase,
  88. CurBase + CurSize - 1
  89. ));
  90. continue;
  91. }
  92. if (GcdDescriptor.GcdMemoryType == EfiGcdMemoryTypeNonExistent) {
  93. Status = gDS->AddMemorySpace (
  94. EfiGcdMemoryTypeSystemMemory,
  95. CurBase,
  96. CurSize,
  97. EFI_MEMORY_WB
  98. );
  99. if (EFI_ERROR (Status)) {
  100. DEBUG ((
  101. DEBUG_ERROR,
  102. "%a: Failed to add System RAM @ 0x%lx - 0x%lx (%r)\n",
  103. __FUNCTION__,
  104. CurBase,
  105. CurBase + CurSize - 1,
  106. Status
  107. ));
  108. continue;
  109. }
  110. Status = gDS->SetMemorySpaceAttributes (
  111. CurBase,
  112. CurSize,
  113. EFI_MEMORY_WB
  114. );
  115. if (EFI_ERROR (Status)) {
  116. DEBUG ((
  117. DEBUG_WARN,
  118. "%a: gDS->SetMemorySpaceAttributes() failed on region 0x%lx - 0x%lx (%r)\n",
  119. __FUNCTION__,
  120. CurBase,
  121. CurBase + CurSize - 1,
  122. Status
  123. ));
  124. }
  125. //
  126. // Due to the ambiguous nature of the RO/XP GCD memory space attributes,
  127. // it is impossible to add a memory space with the XP attribute in a way
  128. // that does not result in the XP attribute being set on *all* UEFI
  129. // memory map entries that are carved from it, including code regions
  130. // that require executable permissions.
  131. //
  132. // So instead, we never set the RO/XP attributes in the GCD memory space
  133. // capabilities or attribute fields, and apply any protections directly
  134. // on the page table mappings by going through the cpu arch protocol.
  135. //
  136. Attributes = EFI_MEMORY_WB;
  137. if ((PcdGet64 (PcdDxeNxMemoryProtectionPolicy) &
  138. (1U << (UINT32)EfiConventionalMemory)) != 0)
  139. {
  140. Attributes |= EFI_MEMORY_XP;
  141. }
  142. Status = Cpu->SetMemoryAttributes (Cpu, CurBase, CurSize, Attributes);
  143. if (EFI_ERROR (Status)) {
  144. DEBUG ((
  145. DEBUG_ERROR,
  146. "%a: Failed to set System RAM @ 0x%lx - 0x%lx attribute (%r)\n",
  147. __FUNCTION__,
  148. CurBase,
  149. CurBase + CurSize - 1,
  150. Status
  151. ));
  152. } else {
  153. DEBUG ((
  154. DEBUG_INFO,
  155. "%a: Add System RAM @ 0x%lx - 0x%lx\n",
  156. __FUNCTION__,
  157. CurBase,
  158. CurBase + CurSize - 1
  159. ));
  160. }
  161. }
  162. }
  163. }
  164. return EFI_SUCCESS;
  165. }