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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/** @file
Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "PiSmmCpuCommon.h"
/**
Get SmmProfileData.
@param[in, out] Size Return Size of SmmProfileData.
0 means the gMmProfileDataHobGuid does not exist.
@return Address of SmmProfileData
**/
EFI_PHYSICAL_ADDRESS
GetSmmProfileData (
IN OUT UINT64 *Size
)
{
EFI_PEI_HOB_POINTERS SmmProfileDataHob;
ASSERT (Size != NULL);
//
// Get Smm Profile Base from Memory Allocation HOB
//
SmmProfileDataHob.Raw = GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION);
while (SmmProfileDataHob.Raw != NULL) {
//
// Find gMmProfileDataHobGuid
//
if (CompareGuid (&SmmProfileDataHob.MemoryAllocation->AllocDescriptor.Name, &gMmProfileDataHobGuid)) {
break;
}
SmmProfileDataHob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, GET_NEXT_HOB (SmmProfileDataHob));
}
if (SmmProfileDataHob.Raw == NULL) {
*Size = 0;
return 0;
}
*Size = SmmProfileDataHob.MemoryAllocation->AllocDescriptor.MemoryLength;
return SmmProfileDataHob.MemoryAllocation->AllocDescriptor.MemoryBaseAddress;
}
/**
Return if the Address is the NonMmram logging Address.
@param[in] Address the address to be checked
@return TRUE The address is the NonMmram logging Address.
@return FALSE The address is not the NonMmram logging Address.
**/
BOOLEAN
IsNonMmramLoggingAddress (
IN UINT64 Address
)
{
EFI_PEI_HOB_POINTERS Hob;
Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
while (Hob.Raw != NULL) {
if ((Address >= Hob.ResourceDescriptor->PhysicalStart) && (Address < Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength)) {
if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) != 0) {
return TRUE;
}
return FALSE;
}
Hob.Raw = GET_NEXT_HOB (Hob);
Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
}
return FALSE;
}
/**
Return if the Address is forbidden as SMM communication buffer.
@param[in] Address the address to be checked
@return TRUE The address is forbidden as SMM communication buffer.
@return FALSE The address is allowed as SMM communication buffer.
**/
BOOLEAN
IsSmmCommBufferForbiddenAddress (
IN UINT64 Address
)
{
EFI_PEI_HOB_POINTERS Hob;
Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
while (Hob.Raw != NULL) {
if ((Address >= Hob.ResourceDescriptor->PhysicalStart) && (Address < Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength)) {
return FALSE;
}
Hob.Raw = GET_NEXT_HOB (Hob);
Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
}
return TRUE;
}
/**
Build Memory Region from ResourceDescriptor HOBs by excluding Logging attribute range.
@param[out] MemoryRegion Returned Non-Mmram Memory regions.
@param[out] MemoryRegionCount A pointer to the number of Memory regions.
**/
VOID
BuildMemoryMapFromResDescHobs (
OUT MM_CPU_MEMORY_REGION **MemoryRegion,
OUT UINTN *MemoryRegionCount
)
{
EFI_PEI_HOB_POINTERS Hob;
UINTN Count;
UINTN Index;
EFI_PHYSICAL_ADDRESS MaxPhysicalAddress;
EFI_PHYSICAL_ADDRESS ResourceHobEnd;
ASSERT (MemoryRegion != NULL && MemoryRegionCount != NULL);
*MemoryRegion = NULL;
*MemoryRegionCount = 0;
MaxPhysicalAddress = LShiftU64 (1, mPhysicalAddressBits);
//
// Get the count.
//
Count = 0;
Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
while (Hob.Raw != NULL) {
if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) == 0) {
ResourceHobEnd = Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength;
ASSERT (ResourceHobEnd <= MaxPhysicalAddress);
if (ResourceHobEnd > MaxPhysicalAddress) {
CpuDeadLoop ();
}
//
// Resource HOBs describe all accessible non-smram regions.
// Logging attribute range is treated as not present. Not-present ranges are not included in this memory map.
//
Count++;
}
Hob.Raw = GET_NEXT_HOB (Hob);
Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
}
*MemoryRegionCount = Count;
*MemoryRegion = (MM_CPU_MEMORY_REGION *)AllocateZeroPool (sizeof (MM_CPU_MEMORY_REGION) * Count);
ASSERT (*MemoryRegion != NULL);
Index = 0;
Hob.Raw = GetFirstHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR);
while (Hob.Raw != NULL) {
if ((Hob.ResourceDescriptor->ResourceAttribute & MM_RESOURCE_ATTRIBUTE_LOGGING) == 0) {
ASSERT (Index < Count);
(*MemoryRegion)[Index].Base = Hob.ResourceDescriptor->PhysicalStart;
(*MemoryRegion)[Index].Length = Hob.ResourceDescriptor->ResourceLength;
(*MemoryRegion)[Index].Attribute = EFI_MEMORY_XP;
if (Hob.ResourceDescriptor->ResourceAttribute == EFI_RESOURCE_ATTRIBUTE_READ_ONLY_PROTECTED) {
(*MemoryRegion)[Index].Attribute |= EFI_MEMORY_RO;
}
Index++;
}
Hob.Raw = GET_NEXT_HOB (Hob);
Hob.Raw = GetNextHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, Hob.Raw);
}
return;
}
/**
Build extended protection MemoryRegion.
The caller is responsible for freeing MemoryRegion via FreePool().
@param[out] MemoryRegion Returned Non-Mmram Memory regions.
@param[out] MemoryRegionCount A pointer to the number of Memory regions.
**/
VOID
CreateExtendedProtectionRange (
OUT MM_CPU_MEMORY_REGION **MemoryRegion,
OUT UINTN *MemoryRegionCount
)
{
BuildMemoryMapFromResDescHobs (MemoryRegion, MemoryRegionCount);
}
/**
Create the Non-Mmram Memory Region within the ResourceDescriptor HOBs
without Logging attribute.
The caller is responsible for freeing MemoryRegion via FreePool().
@param[in] PhysicalAddressBits The bits of physical address to map.
@param[out] MemoryRegion Returned Non-Mmram Memory regions.
@param[out] MemoryRegionCount A pointer to the number of Memory regions.
**/
VOID
CreateNonMmramMemMap (
IN UINT8 PhysicalAddressBits,
OUT MM_CPU_MEMORY_REGION **MemoryRegion,
OUT UINTN *MemoryRegionCount
)
{
BuildMemoryMapFromResDescHobs (MemoryRegion, MemoryRegionCount);
}
|