PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/edk2/Clover/OsxEfiLdr/Support.c

https://gitlab.com/envieidoc/Clover
C | 321 lines | 198 code | 25 blank | 98 comment | 48 complexity | 9f71f5bd339882afa77fd465b84fa6ba MD5 | raw file
  1. /*++
  2. Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials
  4. are licensed and made available under the terms and conditions of the BSD License
  5. which accompanies this distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. Support.c
  11. Abstract:
  12. Revision History:
  13. --*/
  14. #include "EfiLdr.h"
  15. //#include "Debug.h"
  16. EFI_STATUS
  17. EfiAddMemoryDescriptor(
  18. UINTN *NoDesc,
  19. EFI_MEMORY_DESCRIPTOR *Desc,
  20. EFI_MEMORY_TYPE Type,
  21. EFI_PHYSICAL_ADDRESS BaseAddress,
  22. UINT64 NoPages,
  23. UINT64 Attribute
  24. )
  25. {
  26. UINTN NumberOfDesc;
  27. UINT64 Temp;
  28. UINTN Index;
  29. if (NoPages == 0) {
  30. return EFI_SUCCESS;
  31. }
  32. //
  33. // See if the new memory descriptor needs to be carved out of an existing memory descriptor
  34. //
  35. NumberOfDesc = *NoDesc;
  36. for (Index = 0; Index < NumberOfDesc; Index++) {
  37. if (Desc[Index].Type == EfiConventionalMemory) {
  38. Temp = DivU64x32 ((BaseAddress - Desc[Index].PhysicalStart), EFI_PAGE_SIZE) + NoPages;
  39. if ((Desc[Index].PhysicalStart < BaseAddress) && (Desc[Index].NumberOfPages >= Temp)) {
  40. if (Desc[Index].NumberOfPages > Temp) {
  41. Desc[*NoDesc].Type = EfiConventionalMemory;
  42. Desc[*NoDesc].PhysicalStart = BaseAddress + MultU64x32 (NoPages, EFI_PAGE_SIZE);
  43. Desc[*NoDesc].NumberOfPages = Desc[Index].NumberOfPages - Temp;
  44. Desc[*NoDesc].VirtualStart = 0;
  45. Desc[*NoDesc].Attribute = Desc[Index].Attribute;
  46. *NoDesc = *NoDesc + 1;
  47. }
  48. Desc[Index].NumberOfPages = Temp - NoPages;
  49. }
  50. if ((Desc[Index].PhysicalStart == BaseAddress) && (Desc[Index].NumberOfPages == NoPages)) {
  51. Desc[Index].Type = Type;
  52. Desc[Index].Attribute = Attribute;
  53. return EFI_SUCCESS;
  54. }
  55. if ((Desc[Index].PhysicalStart == BaseAddress) && (Desc[Index].NumberOfPages > NoPages)) {
  56. Desc[Index].NumberOfPages -= NoPages;
  57. Desc[Index].PhysicalStart += MultU64x32 (NoPages, EFI_PAGE_SIZE);
  58. }
  59. }
  60. }
  61. //
  62. // Add the new memory descriptor
  63. //
  64. Desc[*NoDesc].Type = Type;
  65. Desc[*NoDesc].PhysicalStart = BaseAddress;
  66. Desc[*NoDesc].NumberOfPages = NoPages;
  67. Desc[*NoDesc].VirtualStart = 0;
  68. Desc[*NoDesc].Attribute = Attribute;
  69. *NoDesc = *NoDesc + 1;
  70. return EFI_SUCCESS;
  71. }
  72. UINTN
  73. FindSpace (
  74. UINTN NoPages,
  75. IN UINTN *NumberOfMemoryMapEntries,
  76. IN EFI_MEMORY_DESCRIPTOR *EfiMemoryDescriptor,
  77. EFI_MEMORY_TYPE Type,
  78. UINT64 Attribute
  79. )
  80. {
  81. EFI_PHYSICAL_ADDRESS MaxPhysicalStart;
  82. UINT64 MaxNoPages;
  83. UINTN Index;
  84. EFI_MEMORY_DESCRIPTOR *CurrentMemoryDescriptor;
  85. MaxPhysicalStart = 0;
  86. MaxNoPages = 0;
  87. CurrentMemoryDescriptor = NULL;
  88. for (Index = 0; Index < *NumberOfMemoryMapEntries; Index++) {
  89. if (EfiMemoryDescriptor[Index].PhysicalStart + LShiftU64(EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT) <= 0x100000) {
  90. continue;
  91. }
  92. if ((EfiMemoryDescriptor[Index].Type == EfiConventionalMemory) &&
  93. (EfiMemoryDescriptor[Index].NumberOfPages >= NoPages)) {
  94. if (EfiMemoryDescriptor[Index].PhysicalStart > MaxPhysicalStart) {
  95. if (EfiMemoryDescriptor[Index].PhysicalStart + LShiftU64(EfiMemoryDescriptor[Index].NumberOfPages, EFI_PAGE_SHIFT) <= 0x100000000ULL) {
  96. MaxPhysicalStart = EfiMemoryDescriptor[Index].PhysicalStart;
  97. MaxNoPages = EfiMemoryDescriptor[Index].NumberOfPages;
  98. CurrentMemoryDescriptor = &EfiMemoryDescriptor[Index];
  99. }
  100. }
  101. }
  102. if ((EfiMemoryDescriptor[Index].Type == EfiReservedMemoryType) ||
  103. (EfiMemoryDescriptor[Index].Type >= EfiACPIReclaimMemory) ) {
  104. continue;
  105. }
  106. if ((EfiMemoryDescriptor[Index].Type == EfiRuntimeServicesCode) ||
  107. (EfiMemoryDescriptor[Index].Type == EfiRuntimeServicesData)) {
  108. break;
  109. }
  110. }
  111. if (MaxPhysicalStart == 0) {
  112. return 0;
  113. }
  114. if (MaxNoPages != NoPages) {
  115. CurrentMemoryDescriptor->NumberOfPages = MaxNoPages - NoPages;
  116. EfiMemoryDescriptor[*NumberOfMemoryMapEntries].Type = Type;
  117. EfiMemoryDescriptor[*NumberOfMemoryMapEntries].PhysicalStart = MaxPhysicalStart + LShiftU64(MaxNoPages - NoPages, EFI_PAGE_SHIFT);
  118. EfiMemoryDescriptor[*NumberOfMemoryMapEntries].NumberOfPages = NoPages;
  119. EfiMemoryDescriptor[*NumberOfMemoryMapEntries].VirtualStart = 0;
  120. EfiMemoryDescriptor[*NumberOfMemoryMapEntries].Attribute = Attribute;
  121. *NumberOfMemoryMapEntries = *NumberOfMemoryMapEntries + 1;
  122. } else {
  123. CurrentMemoryDescriptor->Type = Type;
  124. CurrentMemoryDescriptor->Attribute = Attribute;
  125. }
  126. return (UINTN)(MaxPhysicalStart + LShiftU64(MaxNoPages - NoPages, EFI_PAGE_SHIFT));
  127. }
  128. VOID
  129. GenMemoryMap (
  130. UINTN *NumberOfMemoryMapEntries,
  131. EFI_MEMORY_DESCRIPTOR *EfiMemoryDescriptor,
  132. BIOS_MEMORY_MAP *BiosMemoryMap
  133. )
  134. {
  135. UINT64 BaseAddress;
  136. UINT64 Length;
  137. EFI_MEMORY_TYPE Type;
  138. UINTN Index, NumMap;
  139. UINTN Attr;
  140. UINT64 Ceiling;
  141. UINT64 EBDAaddr; // = 0x9E000;
  142. UINT64 EBDAmax = 0x100000;
  143. UINT64 EBDAsize = 2;
  144. // EBDA memory protection
  145. EBDAaddr = LShiftU64((UINT64)(*(UINT16 *)(UINTN)(0x40E)), 4);
  146. //fool proof
  147. if (EBDAaddr < 0x90000 || EBDAaddr > 0x9F800) {
  148. EBDAaddr = 0x9A000;
  149. }
  150. NumMap = BiosMemoryMap->MemoryMapSize / sizeof(BIOS_MEMORY_MAP_ENTRY);
  151. // PrintString("Number of entries = %d\n", NumMap);
  152. Ceiling = 0xFFFFFFFF;
  153. for (Index = 0; Index < NumMap; Index++) {
  154. switch (BiosMemoryMap->MemoryMapEntry[Index].Type) {
  155. case (INT15_E820_AddressRangeMemory): //1 kMemoryRangeUsable
  156. Type = EfiConventionalMemory;
  157. Attr = EFI_MEMORY_WB;
  158. break;
  159. case (INT15_E820_AddressRangeReserved): //2 (Do not use)
  160. Type = EfiReservedMemoryType;
  161. Attr = EFI_MEMORY_UC;
  162. break;
  163. case (INT15_E820_AddressRangeACPI): //3
  164. Type = EfiACPIReclaimMemory;
  165. Attr = EFI_MEMORY_WB;
  166. break;
  167. case (INT15_E820_AddressRangeNVS): //4 (Do not use)
  168. Type = EfiACPIMemoryNVS;
  169. Attr = EFI_MEMORY_UC;
  170. break;
  171. default:
  172. // We should not get here, according to ACPI 2.0 Spec.
  173. // BIOS behaviour of the Int15h, E820h
  174. Type = EfiReservedMemoryType;
  175. Attr = EFI_MEMORY_UC; //(Do not use)
  176. break;
  177. }
  178. if (Type == EfiConventionalMemory) {
  179. BaseAddress = BiosMemoryMap->MemoryMapEntry[Index].BaseAddress;
  180. Length = BiosMemoryMap->MemoryMapEntry[Index].Length;
  181. if (BaseAddress & EFI_PAGE_MASK) {
  182. Length = Length + (BaseAddress & EFI_PAGE_MASK) - EFI_PAGE_SIZE;
  183. BaseAddress = LShiftU64 (RShiftU64 (BaseAddress + EFI_PAGE_MASK, EFI_PAGE_SHIFT), EFI_PAGE_SHIFT);
  184. }
  185. } else {
  186. BaseAddress = BiosMemoryMap->MemoryMapEntry[Index].BaseAddress;
  187. Length = BiosMemoryMap->MemoryMapEntry[Index].Length + (BaseAddress & EFI_PAGE_MASK);
  188. BaseAddress = LShiftU64 (RShiftU64 (BaseAddress, EFI_PAGE_SHIFT), EFI_PAGE_SHIFT);
  189. if (Length & EFI_PAGE_MASK) {
  190. Length = LShiftU64 (RShiftU64 (Length, EFI_PAGE_SHIFT) + 1, EFI_PAGE_SHIFT);
  191. }
  192. //
  193. // Update Memory Ceiling
  194. //
  195. //Slice - there was (BaseAddress >= 0x100000ULL) - the bred of sieve of cable 0x60000000ULL
  196. if ((BaseAddress >= 0x100000ULL) && (BaseAddress < 0x100000000ULL)) {
  197. if (Ceiling > BaseAddress) {
  198. Ceiling = BaseAddress;
  199. }
  200. }
  201. // Ignore the EBDA and bios rom area
  202. if (BaseAddress < EBDAaddr) {
  203. if ((BaseAddress + Length) >= EBDAaddr) {
  204. continue;
  205. }
  206. } else if (BaseAddress < EBDAmax) {
  207. continue;
  208. }
  209. }
  210. //ugly patch
  211. /* if (BaseAddress == 0x9b000) {
  212. //EBDA2 protection
  213. EfiAddMemoryDescriptor (
  214. NumberOfMemoryMapEntries,
  215. EfiMemoryDescriptor,
  216. EfiACPIMemoryNVS,
  217. (EFI_PHYSICAL_ADDRESS)BaseAddress,
  218. RShiftU64 (Length + EFI_PAGE_MASK, EFI_PAGE_SHIFT),
  219. EFI_MEMORY_UC
  220. );
  221. } else { */
  222. EfiAddMemoryDescriptor (
  223. NumberOfMemoryMapEntries,
  224. EfiMemoryDescriptor,
  225. Type,
  226. (EFI_PHYSICAL_ADDRESS)BaseAddress,
  227. RShiftU64 (Length, EFI_PAGE_SHIFT),
  228. Attr
  229. );
  230. // }
  231. }
  232. //Slice - Add two more descriptors?
  233. /* dmazar: does not have effect, so removed */
  234. //Slice - or no! This is only thing that resolves memory KP in SnowLeopard
  235. //usr-sse2 http://www.projectosx.com/forum/index.php?showtopic=2008&view=findpost&p=13284
  236. //slice http://www.projectosx.com/forum/index.php?showtopic=2008&view=findpost&p=14702
  237. //dmazar http://www.projectosx.com/forum/index.php?showtopic=2008&view=findpost&p=16046
  238. //solution half a year later http://www.projectosx.com/forum/index.php?showtopic=2008&view=findpost&p=16405
  239. /*
  240. before I am proposing 9E000 and 2 page = 8kb. It is not common case.
  241. */
  242. //protect from the EBDA to the 1MB barrier
  243. EBDAsize = EBDAmax - EBDAaddr;
  244. EfiAddMemoryDescriptor (
  245. NumberOfMemoryMapEntries,
  246. EfiMemoryDescriptor,
  247. EfiReservedMemoryType,
  248. (EFI_PHYSICAL_ADDRESS)EBDAaddr,
  249. RShiftU64 (EBDAsize + EFI_PAGE_MASK, EFI_PAGE_SHIFT),
  250. EFI_MEMORY_UC
  251. );
  252. //EBDA2 protection
  253. /* EfiAddMemoryDescriptor (
  254. NumberOfMemoryMapEntries,
  255. EfiMemoryDescriptor,
  256. EfiACPIMemoryNVS,
  257. (EFI_PHYSICAL_ADDRESS)0x9b000,
  258. 1,
  259. EFI_MEMORY_UC
  260. );
  261. */
  262. // this is just BIOS rom protection. Seems to be not needed.
  263. /*
  264. EfiAddMemoryDescriptor (
  265. NumberOfMemoryMapEntries,
  266. EfiMemoryDescriptor,
  267. EfiReservedMemoryType,
  268. (EFI_PHYSICAL_ADDRESS)0xE0000,
  269. 0x20,
  270. EFI_MEMORY_UC
  271. );
  272. // */
  273. //
  274. // Update MemoryMap according to Ceiling
  275. //
  276. /* dmazar: Or not?
  277. * We'll leave BIOS mem map untouched and add those EfiConventionalMemory
  278. * areas to UEFI mem map in BdsPlatformLib:UpdateMemoryMap().
  279. *
  280. for (Index = 0; Index < *NumberOfMemoryMapEntries; Index++) {
  281. if ((EfiMemoryDescriptor[Index].Type == EfiConventionalMemory) &&
  282. (EfiMemoryDescriptor[Index].PhysicalStart > 0x100000ULL) &&
  283. (EfiMemoryDescriptor[Index].PhysicalStart < 0x100000000ULL)) {
  284. if (EfiMemoryDescriptor[Index].PhysicalStart >= Ceiling){
  285. EfiMemoryDescriptor[Index].Type = EfiReservedMemoryType;
  286. }
  287. }
  288. }
  289. */
  290. }