forked from acidanthera/OpenCorePkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryDebug.c
166 lines (144 loc) · 5.31 KB
/
MemoryDebug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/** @file
Copyright (C) 2019, vit9696. All rights reserved.
All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include <Guid/MemoryAttributesTable.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcGuardLib.h>
#include <Library/OcMemoryLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiLib.h>
STATIC CONST CHAR8 *mEfiMemoryTypeDesc[EfiMaxMemoryType] = {
"Reserved ",
"LDR Code ",
"LDR Data ",
"BS Code ",
"BS Data ",
"RT Code ",
"RT Data ",
"Available",
"Unusable ",
"ACPI RECL",
"ACPI NVS ",
"MemMapIO ",
"MemPortIO",
"PAL Code ",
"Persist "
};
STATIC
VOID
OcPrintMemoryDescritptor (
IN EFI_MEMORY_DESCRIPTOR *Desc
)
{
CONST CHAR8 *Type;
CONST CHAR8 *SizeType;
UINT64 SizeValue;
if (Desc->Type < ARRAY_SIZE (mEfiMemoryTypeDesc)) {
Type = mEfiMemoryTypeDesc[Desc->Type];
} else {
Type = "Invalid ";
}
SizeValue = EFI_PAGES_TO_SIZE (Desc->NumberOfPages);
if (SizeValue >= BASE_1MB) {
SizeValue /= BASE_1MB;
SizeType = "MB";
} else {
SizeValue /= BASE_1KB;
SizeType = "KB";
}
DEBUG ((
DEBUG_INFO,
"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",
Type,
(Desc->Attribute & EFI_MEMORY_RUNTIME) != 0 ? "RUN" : " ",
(Desc->Attribute & EFI_MEMORY_CPU_CRYPTO) != 0 ? "CRY" : " ",
(Desc->Attribute & EFI_MEMORY_SP) != 0 ? "SP" : " ",
(Desc->Attribute & EFI_MEMORY_RO) != 0 ? "RO" : " ",
(Desc->Attribute & EFI_MEMORY_MORE_RELIABLE) != 0 ? "MR" : " ",
(Desc->Attribute & EFI_MEMORY_NV) != 0 ? "NV" : " ",
(Desc->Attribute & EFI_MEMORY_XP) != 0 ? "XP" : " ",
(Desc->Attribute & EFI_MEMORY_RP) != 0 ? "RP" : " ",
(Desc->Attribute & EFI_MEMORY_WP) != 0 ? "WP" : " ",
(Desc->Attribute & EFI_MEMORY_UCE) != 0 ? "UCE" : " ",
(Desc->Attribute & EFI_MEMORY_WB) != 0 ? "WB" : " ",
(Desc->Attribute & EFI_MEMORY_WT) != 0 ? "WT" : " ",
(Desc->Attribute & EFI_MEMORY_WC) != 0 ? "WC" : " ",
(Desc->Attribute & EFI_MEMORY_UC) != 0 ? "UC" : " ",
Desc->PhysicalStart,
Desc->PhysicalStart + (EFI_PAGES_TO_SIZE (Desc->NumberOfPages) - 1),
Desc->VirtualStart,
SizeValue,
SizeType
));
}
VOID
OcPrintMemoryAttributesTable (
VOID
)
{
UINTN Index;
UINTN RealSize;
CONST EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable;
EFI_MEMORY_DESCRIPTOR *MemoryAttributesEntry;
MemoryAttributesTable = OcGetMemoryAttributes (NULL);
if (MemoryAttributesTable == NULL) {
DEBUG ((DEBUG_INFO, "OCMM: MemoryAttributesTable is not present!\n"));
return;
}
//
// Printing may reallocate, so we create a copy of the memory attributes.
//
STATIC UINT8 mMemoryAttributesTable[OC_DEFAULT_MEMORY_MAP_SIZE];
RealSize = (UINTN) (sizeof (EFI_MEMORY_ATTRIBUTES_TABLE)
+ MemoryAttributesTable->NumberOfEntries * MemoryAttributesTable->DescriptorSize);
if (RealSize > sizeof(mMemoryAttributesTable)) {
DEBUG ((DEBUG_INFO, "OCMM: MemoryAttributesTable has too large size %u!\n", (UINT32) RealSize));
return;
}
CopyMem (mMemoryAttributesTable, MemoryAttributesTable, RealSize);
MemoryAttributesTable = (EFI_MEMORY_ATTRIBUTES_TABLE *) mMemoryAttributesTable;
MemoryAttributesEntry = (EFI_MEMORY_DESCRIPTOR *) (MemoryAttributesTable + 1);
DEBUG ((DEBUG_INFO, "OCMM: MemoryAttributesTable:\n"));
DEBUG ((DEBUG_INFO, "OCMM: Version - 0x%08x\n", MemoryAttributesTable->Version));
DEBUG ((DEBUG_INFO, "OCMM: NumberOfEntries - 0x%08x\n", MemoryAttributesTable->NumberOfEntries));
DEBUG ((DEBUG_INFO, "OCMM: DescriptorSize - 0x%08x\n", MemoryAttributesTable->DescriptorSize));
for (Index = 0; Index < MemoryAttributesTable->NumberOfEntries; ++Index) {
OcPrintMemoryDescritptor (MemoryAttributesEntry);
MemoryAttributesEntry = NEXT_MEMORY_DESCRIPTOR (
MemoryAttributesEntry,
MemoryAttributesTable->DescriptorSize
);
}
}
VOID
OcPrintMemoryMap (
IN UINTN MemoryMapSize,
IN EFI_MEMORY_DESCRIPTOR *MemoryMap,
IN UINTN DescriptorSize
)
{
UINTN Index;
UINT32 NumberOfEntries;
NumberOfEntries = (UINT32) (MemoryMapSize / DescriptorSize);
DEBUG ((DEBUG_INFO, "OCMM: MemoryMap:\n"));
DEBUG ((DEBUG_INFO, "OCMM: Size - 0x%08x\n", MemoryMapSize));
DEBUG ((DEBUG_INFO, "OCMM: NumberOfEntries - 0x%08x\n", NumberOfEntries));
DEBUG ((DEBUG_INFO, "OCMM: DescriptorSize - 0x%08x\n", DescriptorSize));
for (Index = 0; Index < NumberOfEntries; ++Index) {
OcPrintMemoryDescritptor (MemoryMap);
MemoryMap = NEXT_MEMORY_DESCRIPTOR (
MemoryMap,
DescriptorSize
);
}
}