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
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>
26 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*mSmmIoLibGcdMemSpace
= NULL
;
27 UINTN mSmmIoLibGcdMemNumberOfDesc
= 0;
29 EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress
= 0;
31 VOID
*mSmmIoLibRegistrationEndOfDxe
;
32 VOID
*mSmmIoLibRegistrationReadyToLock
;
34 BOOLEAN mSmmIoLibReadyToLock
= FALSE
;
37 Calculate and save the maximum support address.
41 SmmIoLibInternalCalculateMaximumSupportAddress (
47 UINT8 MemPhysicalAddressBits
;
50 // Get physical address bits supported.
52 Hob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
54 MemPhysicalAddressBits
= ((EFI_HOB_CPU
*)Hob
)->SizeOfMemorySpace
;
56 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
57 if (RegEax
>= 0x80000008) {
58 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
59 MemPhysicalAddressBits
= (UINT8
)RegEax
;
61 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)))))
118 "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
121 mSmmIoLibInternalMaximumSupportMemAddress
127 // Check override for valid MMIO region
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
)))
137 InValidRegion
= TRUE
;
141 if (!InValidRegion
) {
144 "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
156 Merge continuous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
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.
168 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMap
,
169 IN OUT UINTN
*NumberOfDescriptors
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
;
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;
185 if (((UINTN
)NextGcdMemoryMapEntry
< (UINTN
)GcdMemoryMapEnd
) &&
186 (GcdMemoryMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) && (NextGcdMemoryMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
187 ((GcdMemoryMapEntry
->BaseAddress
+ GcdMemoryMapEntry
->Length
) == NextGcdMemoryMapEntry
->BaseAddress
))
189 GcdMemoryMapEntry
->Length
+= NextGcdMemoryMapEntry
->Length
;
190 if (NewGcdMemoryMapEntry
!= GcdMemoryMapEntry
) {
191 NewGcdMemoryMapEntry
->Length
+= NextGcdMemoryMapEntry
->Length
;
194 NextGcdMemoryMapEntry
= NextGcdMemoryMapEntry
+ 1;
197 GcdMemoryMapEntry
= NextGcdMemoryMapEntry
- 1;
202 GcdMemoryMapEntry
= GcdMemoryMapEntry
+ 1;
203 NewGcdMemoryMapEntry
= NewGcdMemoryMapEntry
+ 1;
206 *NumberOfDescriptors
= ((UINTN
)NewGcdMemoryMapEntry
- (UINTN
)GcdMemoryMap
) / sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
);
212 Notification for SMM EndOfDxe protocol.
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.
218 @retval EFI_SUCCESS Notification runs successfully.
219 @retval EFI_OUT_OF_RESOURCES No enough resources to save GCD MMIO map.
223 SmmIoLibInternalEndOfDxeNotify (
224 IN CONST EFI_GUID
*Protocol
,
229 UINTN NumberOfDescriptors
;
230 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemSpaceMap
;
233 Status
= gDS
->GetMemorySpaceMap (&NumberOfDescriptors
, &MemSpaceMap
);
234 if (!EFI_ERROR (Status
)) {
235 MergeGcdMmioEntry (MemSpaceMap
, &NumberOfDescriptors
);
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
;
244 mSmmIoLibGcdMemNumberOfDesc
= NumberOfDescriptors
;
245 gBS
->FreePool (MemSpaceMap
);
252 Notification for SMM ReadyToLock protocol.
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.
258 @retval EFI_SUCCESS Notification runs successfully.
262 SmmIoLibInternalReadyToLockNotify (
263 IN CONST EFI_GUID
*Protocol
,
268 mSmmIoLibReadyToLock
= TRUE
;
273 The constructor function initializes the Smm IO library
275 @param ImageHandle The firmware allocated handle for the EFI image.
276 @param SystemTable A pointer to the EFI System Table.
278 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
283 SmmIoLibConstructor (
284 IN EFI_HANDLE ImageHandle
,
285 IN EFI_SYSTEM_TABLE
*SystemTable
291 // Calculate and save maximum support address
293 SmmIoLibInternalCalculateMaximumSupportAddress ();
296 // Register EndOfDxe to get GCD resource map
298 Status
= gSmst
->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid
, SmmIoLibInternalEndOfDxeNotify
, &mSmmIoLibRegistrationEndOfDxe
);
299 ASSERT_EFI_ERROR (Status
);
302 // Register ready to lock so that we can know when to check valid resource region
304 Status
= gSmst
->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid
, SmmIoLibInternalReadyToLockNotify
, &mSmmIoLibRegistrationReadyToLock
);
305 ASSERT_EFI_ERROR (Status
);
311 The destructor function frees resource used in the Smm IO library
313 @param[in] ImageHandle The firmware allocated handle for the EFI image.
314 @param[in] SystemTable A pointer to the EFI System Table.
316 @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
321 IN EFI_HANDLE ImageHandle
,
322 IN EFI_SYSTEM_TABLE
*SystemTable
325 gSmst
->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid
, NULL
, &mSmmIoLibRegistrationEndOfDxe
);
326 gSmst
->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid
, NULL
, &mSmmIoLibRegistrationReadyToLock
);