1 | /** @file
|
---|
2 | Instance of SMM IO check library.
|
---|
3 |
|
---|
4 | SMM IO check library library implementation. This library consumes GCD to collect all valid
|
---|
5 | IO space defined by a platform.
|
---|
6 | A platform may have its own SmmIoLib instance to exclude more IO space.
|
---|
7 |
|
---|
8 | Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
|
---|
9 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
10 |
|
---|
11 | **/
|
---|
12 |
|
---|
13 | #include <PiSmm.h>
|
---|
14 |
|
---|
15 | #include <Library/BaseLib.h>
|
---|
16 | #include <Library/BaseMemoryLib.h>
|
---|
17 | #include <Library/DebugLib.h>
|
---|
18 | #include <Library/MemoryAllocationLib.h>
|
---|
19 | #include <Library/UefiBootServicesTableLib.h>
|
---|
20 | #include <Library/SmmServicesTableLib.h>
|
---|
21 | #include <Library/HobLib.h>
|
---|
22 | #include <Library/DxeServicesTableLib.h>
|
---|
23 | #include <Protocol/SmmReadyToLock.h>
|
---|
24 | #include <Protocol/SmmEndOfDxe.h>
|
---|
25 |
|
---|
26 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace = NULL;
|
---|
27 | UINTN mSmmIoLibGcdMemNumberOfDesc = 0;
|
---|
28 |
|
---|
29 | EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress = 0;
|
---|
30 |
|
---|
31 | VOID *mSmmIoLibRegistrationEndOfDxe;
|
---|
32 | VOID *mSmmIoLibRegistrationReadyToLock;
|
---|
33 |
|
---|
34 | BOOLEAN mSmmIoLibReadyToLock = FALSE;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | Calculate and save the maximum support address.
|
---|
38 |
|
---|
39 | **/
|
---|
40 | VOID
|
---|
41 | SmmIoLibInternalCalculateMaximumSupportAddress (
|
---|
42 | VOID
|
---|
43 | )
|
---|
44 | {
|
---|
45 | VOID *Hob;
|
---|
46 | UINT32 RegEax;
|
---|
47 | UINT8 MemPhysicalAddressBits;
|
---|
48 |
|
---|
49 | //
|
---|
50 | // Get physical address bits supported.
|
---|
51 | //
|
---|
52 | Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
|
---|
53 | if (Hob != NULL) {
|
---|
54 | MemPhysicalAddressBits = ((EFI_HOB_CPU *)Hob)->SizeOfMemorySpace;
|
---|
55 | } else {
|
---|
56 | AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
|
---|
57 | if (RegEax >= 0x80000008) {
|
---|
58 | AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
|
---|
59 | MemPhysicalAddressBits = (UINT8)RegEax;
|
---|
60 | } else {
|
---|
61 | MemPhysicalAddressBits = 36;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | //
|
---|
66 | // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
|
---|
67 | //
|
---|
68 | ASSERT (MemPhysicalAddressBits <= 52);
|
---|
69 | if (MemPhysicalAddressBits > 48) {
|
---|
70 | MemPhysicalAddressBits = 48;
|
---|
71 | }
|
---|
72 |
|
---|
73 | //
|
---|
74 | // Save the maximum support address in one global variable
|
---|
75 | //
|
---|
76 | mSmmIoLibInternalMaximumSupportMemAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, MemPhysicalAddressBits) - 1);
|
---|
77 | DEBUG ((DEBUG_INFO, "mSmmIoLibInternalMaximumSupportMemAddress = 0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress));
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | This function check if the MMIO resource is valid per processor architecture and
|
---|
82 | valid per platform design.
|
---|
83 |
|
---|
84 | @param BaseAddress The MMIO start address to be checked.
|
---|
85 | @param Length The MMIO length to be checked.
|
---|
86 | @param Owner A GUID representing the owner of the resource.
|
---|
87 | This GUID may be used by producer to correlate the device ownership of the resource.
|
---|
88 | NULL means no specific owner.
|
---|
89 |
|
---|
90 | @retval TRUE This MMIO resource is valid per processor architecture and valid per platform design.
|
---|
91 | @retval FALSE This MMIO resource is not valid per processor architecture or valid per platform design.
|
---|
92 | **/
|
---|
93 | BOOLEAN
|
---|
94 | EFIAPI
|
---|
95 | SmmIsMmioValid (
|
---|
96 | IN EFI_PHYSICAL_ADDRESS BaseAddress,
|
---|
97 | IN UINT64 Length,
|
---|
98 | IN EFI_GUID *Owner OPTIONAL
|
---|
99 | )
|
---|
100 | {
|
---|
101 | UINTN Index;
|
---|
102 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
|
---|
103 | BOOLEAN InValidRegion;
|
---|
104 |
|
---|
105 | //
|
---|
106 | // Check override.
|
---|
107 | // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
|
---|
108 | //
|
---|
109 | if ((Length > mSmmIoLibInternalMaximumSupportMemAddress) ||
|
---|
110 | (BaseAddress > mSmmIoLibInternalMaximumSupportMemAddress) ||
|
---|
111 | ((Length != 0) && (BaseAddress > (mSmmIoLibInternalMaximumSupportMemAddress - (Length - 1)))))
|
---|
112 | {
|
---|
113 | //
|
---|
114 | // Overflow happen
|
---|
115 | //
|
---|
116 | DEBUG ((
|
---|
117 | DEBUG_ERROR,
|
---|
118 | "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
|
---|
119 | BaseAddress,
|
---|
120 | Length,
|
---|
121 | mSmmIoLibInternalMaximumSupportMemAddress
|
---|
122 | ));
|
---|
123 | return FALSE;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //
|
---|
127 | // Check override for valid MMIO region
|
---|
128 | //
|
---|
129 | if (mSmmIoLibReadyToLock) {
|
---|
130 | InValidRegion = FALSE;
|
---|
131 | for (Index = 0; Index < mSmmIoLibGcdMemNumberOfDesc; Index++) {
|
---|
132 | Desc = &mSmmIoLibGcdMemSpace[Index];
|
---|
133 | if ((Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
|
---|
134 | (BaseAddress >= Desc->BaseAddress) &&
|
---|
135 | ((BaseAddress + Length) <= (Desc->BaseAddress + Desc->Length)))
|
---|
136 | {
|
---|
137 | InValidRegion = TRUE;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (!InValidRegion) {
|
---|
142 | DEBUG ((
|
---|
143 | DEBUG_ERROR,
|
---|
144 | "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
|
---|
145 | BaseAddress,
|
---|
146 | Length
|
---|
147 | ));
|
---|
148 | return FALSE;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return TRUE;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | Merge continuous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
|
---|
157 |
|
---|
158 | @param[in, out] GcdMemoryMap A pointer to the buffer in which firmware places
|
---|
159 | the current GCD memory map.
|
---|
160 | @param[in, out] NumberOfDescriptors A pointer to the number of the
|
---|
161 | GcdMemoryMap buffer. On input, this is the number of
|
---|
162 | the current GCD memory map. On output,
|
---|
163 | it is the number of new GCD memory map after merge.
|
---|
164 | **/
|
---|
165 | STATIC
|
---|
166 | VOID
|
---|
167 | MergeGcdMmioEntry (
|
---|
168 | IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMap,
|
---|
169 | IN OUT UINTN *NumberOfDescriptors
|
---|
170 | )
|
---|
171 | {
|
---|
172 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEntry;
|
---|
173 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMapEnd;
|
---|
174 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *NewGcdMemoryMapEntry;
|
---|
175 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *NextGcdMemoryMapEntry;
|
---|
176 |
|
---|
177 | GcdMemoryMapEntry = GcdMemoryMap;
|
---|
178 | NewGcdMemoryMapEntry = GcdMemoryMap;
|
---|
179 | GcdMemoryMapEnd = (EFI_GCD_MEMORY_SPACE_DESCRIPTOR *)((UINT8 *)GcdMemoryMap + (*NumberOfDescriptors) * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
|
---|
180 | while ((UINTN)GcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) {
|
---|
181 | CopyMem (NewGcdMemoryMapEntry, GcdMemoryMapEntry, sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
|
---|
182 | NextGcdMemoryMapEntry = GcdMemoryMapEntry + 1;
|
---|
183 |
|
---|
184 | do {
|
---|
185 | if (((UINTN)NextGcdMemoryMapEntry < (UINTN)GcdMemoryMapEnd) &&
|
---|
186 | (GcdMemoryMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) && (NextGcdMemoryMapEntry->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo) &&
|
---|
187 | ((GcdMemoryMapEntry->BaseAddress + GcdMemoryMapEntry->Length) == NextGcdMemoryMapEntry->BaseAddress))
|
---|
188 | {
|
---|
189 | GcdMemoryMapEntry->Length += NextGcdMemoryMapEntry->Length;
|
---|
190 | if (NewGcdMemoryMapEntry != GcdMemoryMapEntry) {
|
---|
191 | NewGcdMemoryMapEntry->Length += NextGcdMemoryMapEntry->Length;
|
---|
192 | }
|
---|
193 |
|
---|
194 | NextGcdMemoryMapEntry = NextGcdMemoryMapEntry + 1;
|
---|
195 | continue;
|
---|
196 | } else {
|
---|
197 | GcdMemoryMapEntry = NextGcdMemoryMapEntry - 1;
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | } while (TRUE);
|
---|
201 |
|
---|
202 | GcdMemoryMapEntry = GcdMemoryMapEntry + 1;
|
---|
203 | NewGcdMemoryMapEntry = NewGcdMemoryMapEntry + 1;
|
---|
204 | }
|
---|
205 |
|
---|
206 | *NumberOfDescriptors = ((UINTN)NewGcdMemoryMapEntry - (UINTN)GcdMemoryMap) / sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR);
|
---|
207 |
|
---|
208 | return;
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | Notification for SMM EndOfDxe protocol.
|
---|
213 |
|
---|
214 | @param[in] Protocol Points to the protocol's unique identifier.
|
---|
215 | @param[in] Interface Points to the interface instance.
|
---|
216 | @param[in] Handle The handle on which the interface was installed.
|
---|
217 |
|
---|
218 | @retval EFI_SUCCESS Notification runs successfully.
|
---|
219 | @retval EFI_OUT_OF_RESOURCES No enough resources to save GCD MMIO map.
|
---|
220 | **/
|
---|
221 | EFI_STATUS
|
---|
222 | EFIAPI
|
---|
223 | SmmIoLibInternalEndOfDxeNotify (
|
---|
224 | IN CONST EFI_GUID *Protocol,
|
---|
225 | IN VOID *Interface,
|
---|
226 | IN EFI_HANDLE Handle
|
---|
227 | )
|
---|
228 | {
|
---|
229 | UINTN NumberOfDescriptors;
|
---|
230 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
|
---|
231 | EFI_STATUS Status;
|
---|
232 |
|
---|
233 | Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
|
---|
234 | if (!EFI_ERROR (Status)) {
|
---|
235 | MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);
|
---|
236 |
|
---|
237 | mSmmIoLibGcdMemSpace = AllocateCopyPool (NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);
|
---|
238 | ASSERT (mSmmIoLibGcdMemSpace != NULL);
|
---|
239 | if (mSmmIoLibGcdMemSpace == NULL) {
|
---|
240 | gBS->FreePool (MemSpaceMap);
|
---|
241 | return EFI_OUT_OF_RESOURCES;
|
---|
242 | }
|
---|
243 |
|
---|
244 | mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;
|
---|
245 | gBS->FreePool (MemSpaceMap);
|
---|
246 | }
|
---|
247 |
|
---|
248 | return EFI_SUCCESS;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | Notification for SMM ReadyToLock protocol.
|
---|
253 |
|
---|
254 | @param[in] Protocol Points to the protocol's unique identifier.
|
---|
255 | @param[in] Interface Points to the interface instance.
|
---|
256 | @param[in] Handle The handle on which the interface was installed.
|
---|
257 |
|
---|
258 | @retval EFI_SUCCESS Notification runs successfully.
|
---|
259 | **/
|
---|
260 | EFI_STATUS
|
---|
261 | EFIAPI
|
---|
262 | SmmIoLibInternalReadyToLockNotify (
|
---|
263 | IN CONST EFI_GUID *Protocol,
|
---|
264 | IN VOID *Interface,
|
---|
265 | IN EFI_HANDLE Handle
|
---|
266 | )
|
---|
267 | {
|
---|
268 | mSmmIoLibReadyToLock = TRUE;
|
---|
269 | return EFI_SUCCESS;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | The constructor function initializes the Smm IO library
|
---|
274 |
|
---|
275 | @param ImageHandle The firmware allocated handle for the EFI image.
|
---|
276 | @param SystemTable A pointer to the EFI System Table.
|
---|
277 |
|
---|
278 | @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
|
---|
279 |
|
---|
280 | **/
|
---|
281 | EFI_STATUS
|
---|
282 | EFIAPI
|
---|
283 | SmmIoLibConstructor (
|
---|
284 | IN EFI_HANDLE ImageHandle,
|
---|
285 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
286 | )
|
---|
287 | {
|
---|
288 | EFI_STATUS Status;
|
---|
289 |
|
---|
290 | //
|
---|
291 | // Calculate and save maximum support address
|
---|
292 | //
|
---|
293 | SmmIoLibInternalCalculateMaximumSupportAddress ();
|
---|
294 |
|
---|
295 | //
|
---|
296 | // Register EndOfDxe to get GCD resource map
|
---|
297 | //
|
---|
298 | Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);
|
---|
299 | ASSERT_EFI_ERROR (Status);
|
---|
300 |
|
---|
301 | //
|
---|
302 | // Register ready to lock so that we can know when to check valid resource region
|
---|
303 | //
|
---|
304 | Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify, &mSmmIoLibRegistrationReadyToLock);
|
---|
305 | ASSERT_EFI_ERROR (Status);
|
---|
306 |
|
---|
307 | return EFI_SUCCESS;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | The destructor function frees resource used in the Smm IO library
|
---|
312 |
|
---|
313 | @param[in] ImageHandle The firmware allocated handle for the EFI image.
|
---|
314 | @param[in] SystemTable A pointer to the EFI System Table.
|
---|
315 |
|
---|
316 | @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
|
---|
317 | **/
|
---|
318 | EFI_STATUS
|
---|
319 | EFIAPI
|
---|
320 | SmmIoLibDestructor (
|
---|
321 | IN EFI_HANDLE ImageHandle,
|
---|
322 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
323 | )
|
---|
324 | {
|
---|
325 | gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mSmmIoLibRegistrationEndOfDxe);
|
---|
326 | gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mSmmIoLibRegistrationReadyToLock);
|
---|
327 | return EFI_SUCCESS;
|
---|
328 | }
|
---|