2 Instance of SMM IO check library.
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.
8 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
9 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Library/BaseLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/SmmServicesTableLib.h>
22 #include <Library/HobLib.h>
23 #include <Library/DxeServicesTableLib.h>
24 #include <Protocol/SmmReadyToLock.h>
25 #include <Protocol/SmmEndOfDxe.h>
27 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*mSmmIoLibGcdMemSpace
= NULL
;
28 UINTN mSmmIoLibGcdMemNumberOfDesc
= 0;
30 EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress
= 0;
32 VOID
*mSmmIoLibRegistrationEndOfDxe
;
33 VOID
*mSmmIoLibRegistrationReadyToLock
;
35 BOOLEAN mSmmIoLibReadyToLock
= FALSE
;
38 Calculate and save the maximum support address.
42 SmmIoLibInternalCalculateMaximumSupportAddress (
48 UINT8 MemPhysicalAddressBits
;
51 // Get physical address bits supported.
53 Hob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
55 MemPhysicalAddressBits
= ((EFI_HOB_CPU
*) Hob
)->SizeOfMemorySpace
;
57 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
58 if (RegEax
>= 0x80000008) {
59 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
60 MemPhysicalAddressBits
= (UINT8
) RegEax
;
62 MemPhysicalAddressBits
= 36;
66 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
68 ASSERT (MemPhysicalAddressBits
<= 52);
69 if (MemPhysicalAddressBits
> 48) {
70 MemPhysicalAddressBits
= 48;
74 // Save the maximum support address in one global variable
76 mSmmIoLibInternalMaximumSupportMemAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(LShiftU64 (1, MemPhysicalAddressBits
) - 1);
77 DEBUG ((DEBUG_INFO
, "mSmmIoLibInternalMaximumSupportMemAddress = 0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress
));
81 This function check if the MMIO resource is valid per processor architecture and
82 valid per platform design.
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.
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.
96 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
98 IN EFI_GUID
*Owner OPTIONAL
102 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Desc
;
103 BOOLEAN InValidRegion
;
107 // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
109 if ((Length
> mSmmIoLibInternalMaximumSupportMemAddress
) ||
110 (BaseAddress
> mSmmIoLibInternalMaximumSupportMemAddress
) ||
111 ((Length
!= 0) && (BaseAddress
> (mSmmIoLibInternalMaximumSupportMemAddress
- (Length
- 1)))) ) {
117 "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
120 mSmmIoLibInternalMaximumSupportMemAddress
126 // Check override for valid MMIO region
128 if (mSmmIoLibReadyToLock
) {
129 InValidRegion
= FALSE
;
130 for (Index
= 0; Index
< mSmmIoLibGcdMemNumberOfDesc
; Index
++) {
131 Desc
= &mSmmIoLibGcdMemSpace
[Index
];
132 if ((Desc
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
133 (BaseAddress
>= Desc
->BaseAddress
) &&
134 ((BaseAddress
+ Length
) <= (Desc
->BaseAddress
+ Desc
->Length
))) {
135 InValidRegion
= TRUE
;
139 if (!InValidRegion
) {
142 "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
153 Merge continuous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
155 @param[in, out] GcdMemoryMap A pointer to the buffer in which firmware places
156 the current GCD memory map.
157 @param[in, out] NumberOfDescriptors A pointer to the number of the
158 GcdMemoryMap buffer. On input, this is the number of
159 the current GCD memory map. On output,
160 it is the number of new GCD memory map after merge.
165 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMap
,
166 IN OUT UINTN
*NumberOfDescriptors
169 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMapEntry
;
170 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMapEnd
;
171 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*NewGcdMemoryMapEntry
;
172 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*NextGcdMemoryMapEntry
;
174 GcdMemoryMapEntry
= GcdMemoryMap
;
175 NewGcdMemoryMapEntry
= GcdMemoryMap
;
176 GcdMemoryMapEnd
= (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*) ((UINT8
*) GcdMemoryMap
+ (*NumberOfDescriptors
) * sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
177 while ((UINTN
)GcdMemoryMapEntry
< (UINTN
)GcdMemoryMapEnd
) {
178 CopyMem (NewGcdMemoryMapEntry
, GcdMemoryMapEntry
, sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
179 NextGcdMemoryMapEntry
= GcdMemoryMapEntry
+ 1;
182 if (((UINTN
)NextGcdMemoryMapEntry
< (UINTN
)GcdMemoryMapEnd
) &&
183 (GcdMemoryMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) && (NextGcdMemoryMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
184 ((GcdMemoryMapEntry
->BaseAddress
+ GcdMemoryMapEntry
->Length
) == NextGcdMemoryMapEntry
->BaseAddress
)) {
185 GcdMemoryMapEntry
->Length
+= NextGcdMemoryMapEntry
->Length
;
186 if (NewGcdMemoryMapEntry
!= GcdMemoryMapEntry
) {
187 NewGcdMemoryMapEntry
->Length
+= NextGcdMemoryMapEntry
->Length
;
190 NextGcdMemoryMapEntry
= NextGcdMemoryMapEntry
+ 1;
193 GcdMemoryMapEntry
= NextGcdMemoryMapEntry
- 1;
198 GcdMemoryMapEntry
= GcdMemoryMapEntry
+ 1;
199 NewGcdMemoryMapEntry
= NewGcdMemoryMapEntry
+ 1;
202 *NumberOfDescriptors
= ((UINTN
)NewGcdMemoryMapEntry
- (UINTN
)GcdMemoryMap
) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR
);
208 Notification for SMM EndOfDxe protocol.
210 @param[in] Protocol Points to the protocol's unique identifier.
211 @param[in] Interface Points to the interface instance.
212 @param[in] Handle The handle on which the interface was installed.
214 @retval EFI_SUCCESS Notification runs successfully.
215 @retval EFI_OUT_OF_RESOURCES No enough resources to save GCD MMIO map.
219 SmmIoLibInternalEndOfDxeNotify (
220 IN CONST EFI_GUID
*Protocol
,
225 UINTN NumberOfDescriptors
;
226 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemSpaceMap
;
229 Status
= gDS
->GetMemorySpaceMap (&NumberOfDescriptors
, &MemSpaceMap
);
230 if (!EFI_ERROR (Status
)) {
232 MergeGcdMmioEntry (MemSpaceMap
, &NumberOfDescriptors
);
234 mSmmIoLibGcdMemSpace
= AllocateCopyPool (NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
), MemSpaceMap
);
235 ASSERT (mSmmIoLibGcdMemSpace
!= NULL
);
236 if (mSmmIoLibGcdMemSpace
== NULL
) {
237 gBS
->FreePool (MemSpaceMap
);
238 return EFI_OUT_OF_RESOURCES
;
241 mSmmIoLibGcdMemNumberOfDesc
= NumberOfDescriptors
;
242 gBS
->FreePool (MemSpaceMap
);
249 Notification for SMM ReadyToLock protocol.
251 @param[in] Protocol Points to the protocol's unique identifier.
252 @param[in] Interface Points to the interface instance.
253 @param[in] Handle The handle on which the interface was installed.
255 @retval EFI_SUCCESS Notification runs successfully.
259 SmmIoLibInternalReadyToLockNotify (
260 IN CONST EFI_GUID
*Protocol
,
265 mSmmIoLibReadyToLock
= TRUE
;
270 The constructor function initializes the Smm IO library
272 @param ImageHandle The firmware allocated handle for the EFI image.
273 @param SystemTable A pointer to the EFI System Table.
275 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
280 SmmIoLibConstructor (
281 IN EFI_HANDLE ImageHandle
,
282 IN EFI_SYSTEM_TABLE
*SystemTable
288 // Calculate and save maximum support address
290 SmmIoLibInternalCalculateMaximumSupportAddress ();
293 // Register EndOfDxe to get GCD resource map
295 Status
= gSmst
->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid
, SmmIoLibInternalEndOfDxeNotify
, &mSmmIoLibRegistrationEndOfDxe
);
296 ASSERT_EFI_ERROR (Status
);
299 // Register ready to lock so that we can know when to check valid resource region
301 Status
= gSmst
->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid
, SmmIoLibInternalReadyToLockNotify
, &mSmmIoLibRegistrationReadyToLock
);
302 ASSERT_EFI_ERROR (Status
);
308 The destructor function frees resource used in the Smm IO library
310 @param[in] ImageHandle The firmware allocated handle for the EFI image.
311 @param[in] SystemTable A pointer to the EFI System Table.
313 @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
318 IN EFI_HANDLE ImageHandle
,
319 IN EFI_SYSTEM_TABLE
*SystemTable
322 gSmst
->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid
, NULL
, &mSmmIoLibRegistrationEndOfDxe
);
323 gSmst
->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid
, NULL
, &mSmmIoLibRegistrationReadyToLock
);