OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
MemoryDebug.c
Go to the documentation of this file.
1
15#include <Uefi.h>
16
17#include <Guid/MemoryAttributesTable.h>
18#include <Library/BaseMemoryLib.h>
19#include <Library/BaseOverflowLib.h>
20#include <Library/DebugLib.h>
21#include <Library/MemoryAllocationLib.h>
22#include <Library/OcMemoryLib.h>
23#include <Library/UefiBootServicesTableLib.h>
24#include <Library/UefiLib.h>
25
26STATIC CONST CHAR8 *mEfiMemoryTypeDesc[EfiMaxMemoryType] = {
27 "Reserved ",
28 "LDR Code ",
29 "LDR Data ",
30 "BS Code ",
31 "BS Data ",
32 "RT Code ",
33 "RT Data ",
34 "Available",
35 "Unusable ",
36 "ACPI RECL",
37 "ACPI NVS ",
38 "MemMapIO ",
39 "MemPortIO",
40 "PAL Code ",
41 "Persist "
42};
43
44STATIC
45VOID
47 IN EFI_MEMORY_DESCRIPTOR *Desc
48 )
49{
50 CONST CHAR8 *Type;
51 CONST CHAR8 *SizeType;
52 UINT64 SizeValue;
53
54 if (Desc->Type < ARRAY_SIZE (mEfiMemoryTypeDesc)) {
55 Type = mEfiMemoryTypeDesc[Desc->Type];
56 } else {
57 Type = "Invalid ";
58 }
59
60 SizeValue = EFI_PAGES_TO_SIZE (Desc->NumberOfPages);
61 if (SizeValue >= BASE_1MB) {
62 SizeValue /= BASE_1MB;
63 SizeType = "MB";
64 } else {
65 SizeValue /= BASE_1KB;
66 SizeType = "KB";
67 }
68
69 DEBUG ((
70 DEBUG_INFO,
71 "OCMM: %a [%a|%a|%a|%a|%a|%a|%a|%a|%a|%a|%a|%a|%a|%a] 0x%016LX-0x%016LX -> 0x%016X (%Lu %a)\n",
72 Type,
73 (Desc->Attribute & EFI_MEMORY_RUNTIME) != 0 ? "RUN" : " ",
74 (Desc->Attribute & EFI_MEMORY_CPU_CRYPTO) != 0 ? "CRY" : " ",
75 (Desc->Attribute & EFI_MEMORY_SP) != 0 ? "SP" : " ",
76 (Desc->Attribute & EFI_MEMORY_RO) != 0 ? "RO" : " ",
77 (Desc->Attribute & EFI_MEMORY_MORE_RELIABLE) != 0 ? "MR" : " ",
78 (Desc->Attribute & EFI_MEMORY_NV) != 0 ? "NV" : " ",
79 (Desc->Attribute & EFI_MEMORY_XP) != 0 ? "XP" : " ",
80 (Desc->Attribute & EFI_MEMORY_RP) != 0 ? "RP" : " ",
81 (Desc->Attribute & EFI_MEMORY_WP) != 0 ? "WP" : " ",
82 (Desc->Attribute & EFI_MEMORY_UCE) != 0 ? "UCE" : " ",
83 (Desc->Attribute & EFI_MEMORY_WB) != 0 ? "WB" : " ",
84 (Desc->Attribute & EFI_MEMORY_WT) != 0 ? "WT" : " ",
85 (Desc->Attribute & EFI_MEMORY_WC) != 0 ? "WC" : " ",
86 (Desc->Attribute & EFI_MEMORY_UC) != 0 ? "UC" : " ",
87 Desc->PhysicalStart,
88 Desc->PhysicalStart + (EFI_PAGES_TO_SIZE (Desc->NumberOfPages) - 1),
89 Desc->VirtualStart,
90 SizeValue,
91 SizeType
92 ));
93}
94
95VOID
97 VOID
98 )
99{
100 UINTN Index;
101 UINTN RealSize;
102 CONST EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;
103 EFI_MEMORY_DESCRIPTOR *MemoryAttributesEntry;
104
105 MemoryAttributesTable = OcGetMemoryAttributes (NULL);
106 if (MemoryAttributesTable == NULL) {
107 DEBUG ((DEBUG_INFO, "OCMM: MemoryAttributesTable is not present!\n"));
108 return;
109 }
110
111 //
112 // Printing may reallocate, so we create a copy of the memory attributes.
113 //
114 STATIC UINT8 mMemoryAttributesTable[OC_DEFAULT_MEMORY_MAP_SIZE];
115
116 RealSize = (UINTN)(sizeof (EFI_MEMORY_ATTRIBUTES_TABLE)
117 + MemoryAttributesTable->NumberOfEntries * MemoryAttributesTable->DescriptorSize);
118
119 if (RealSize > sizeof (mMemoryAttributesTable)) {
120 DEBUG ((DEBUG_INFO, "OCMM: MemoryAttributesTable has too large size %u!\n", (UINT32)RealSize));
121 return;
122 }
123
124 CopyMem (mMemoryAttributesTable, MemoryAttributesTable, RealSize);
125
126 MemoryAttributesTable = (EFI_MEMORY_ATTRIBUTES_TABLE *)mMemoryAttributesTable;
127 MemoryAttributesEntry = (EFI_MEMORY_DESCRIPTOR *)(MemoryAttributesTable + 1);
128
129 DEBUG ((DEBUG_INFO, "OCMM: MemoryAttributesTable:\n"));
130 DEBUG ((DEBUG_INFO, "OCMM: Version - 0x%08x\n", MemoryAttributesTable->Version));
131 DEBUG ((DEBUG_INFO, "OCMM: NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));
132 DEBUG ((DEBUG_INFO, "OCMM: DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));
133
134 for (Index = 0; Index < MemoryAttributesTable->NumberOfEntries; ++Index) {
135 OcPrintMemoryDescriptor (MemoryAttributesEntry);
136 MemoryAttributesEntry = NEXT_MEMORY_DESCRIPTOR (
137 MemoryAttributesEntry,
138 MemoryAttributesTable->DescriptorSize
139 );
140 }
141}
142
143VOID
145 IN UINTN MemoryMapSize,
146 IN EFI_MEMORY_DESCRIPTOR *MemoryMap,
147 IN UINTN DescriptorSize
148 )
149{
150 UINTN Index;
151 UINT32 NumberOfEntries;
152
153 NumberOfEntries = (UINT32)(MemoryMapSize / DescriptorSize);
154
155 DEBUG ((DEBUG_INFO, "OCMM: MemoryMap:\n"));
156 DEBUG ((DEBUG_INFO, "OCMM: Size - 0x%08x\n", MemoryMapSize));
157 DEBUG ((DEBUG_INFO, "OCMM: NumberOfEntries - 0x%08x\n", NumberOfEntries));
158 DEBUG ((DEBUG_INFO, "OCMM: DescriptorSize - 0x%08x\n", DescriptorSize));
159
160 for (Index = 0; Index < NumberOfEntries; ++Index) {
161 OcPrintMemoryDescriptor (MemoryMap);
162 MemoryMap = NEXT_MEMORY_DESCRIPTOR (
163 MemoryMap,
164 DescriptorSize
165 );
166 }
167}
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
STATIC CONST CHAR8 * mEfiMemoryTypeDesc[EfiMaxMemoryType]
Definition MemoryDebug.c:26
VOID OcPrintMemoryMap(IN UINTN MemoryMapSize, IN EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN DescriptorSize)
STATIC VOID OcPrintMemoryDescriptor(IN EFI_MEMORY_DESCRIPTOR *Desc)
Definition MemoryDebug.c:46
VOID OcPrintMemoryAttributesTable(VOID)
Definition MemoryDebug.c:96
EFI_MEMORY_ATTRIBUTES_TABLE * OcGetMemoryAttributes(OUT EFI_MEMORY_DESCRIPTOR **MemoryAttributesEntry OPTIONAL)
#define OC_DEFAULT_MEMORY_MAP_SIZE
Definition OcMemoryLib.h:51
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)