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 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/DebugLib.h>
25 #include <Library/MemoryAllocationLib.h>
26 #include <Library/UefiBootServicesTableLib.h>
27 #include <Library/SmmServicesTableLib.h>
28 #include <Library/HobLib.h>
29 #include <Library/DxeServicesTableLib.h>
30 #include <Protocol/SmmReadyToLock.h>
31 #include <Protocol/SmmEndOfDxe.h>
33 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*mSmmIoLibGcdMemSpace
= NULL
;
34 UINTN mSmmIoLibGcdMemNumberOfDesc
= 0;
36 EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress
= 0;
38 VOID
*mSmmIoLibRegistrationEndOfDxe
;
39 VOID
*mSmmIoLibRegistrationReadyToLock
;
41 BOOLEAN mSmmIoLibReadyToLock
= FALSE
;
44 Calculate and save the maximum support address.
48 SmmIoLibInternalCalculateMaximumSupportAddress (
54 UINT8 MemPhysicalAddressBits
;
57 // Get physical address bits supported.
59 Hob
= GetFirstHob (EFI_HOB_TYPE_CPU
);
61 MemPhysicalAddressBits
= ((EFI_HOB_CPU
*) Hob
)->SizeOfMemorySpace
;
63 AsmCpuid (0x80000000, &RegEax
, NULL
, NULL
, NULL
);
64 if (RegEax
>= 0x80000008) {
65 AsmCpuid (0x80000008, &RegEax
, NULL
, NULL
, NULL
);
66 MemPhysicalAddressBits
= (UINT8
) RegEax
;
68 MemPhysicalAddressBits
= 36;
72 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
74 ASSERT (MemPhysicalAddressBits
<= 52);
75 if (MemPhysicalAddressBits
> 48) {
76 MemPhysicalAddressBits
= 48;
80 // Save the maximum support address in one global variable
82 mSmmIoLibInternalMaximumSupportMemAddress
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(LShiftU64 (1, MemPhysicalAddressBits
) - 1);
83 DEBUG ((DEBUG_INFO
, "mSmmIoLibInternalMaximumSupportMemAddress = 0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress
));
87 This function check if the MMIO resource is valid per processor architecture and
88 valid per platform design.
90 @param BaseAddress The MMIO start address to be checked.
91 @param Length The MMIO length to be checked.
92 @param Owner A GUID representing the owner of the resource.
93 This GUID may be used by producer to correlate the device ownership of the resource.
94 NULL means no specific owner.
96 @retval TRUE This MMIO resource is valid per processor architecture and valid per platform design.
97 @retval FALSE This MMIO resource is not valid per processor architecture or valid per platform design.
102 IN EFI_PHYSICAL_ADDRESS BaseAddress
,
104 IN EFI_GUID
*Owner OPTIONAL
108 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*Desc
;
109 BOOLEAN InValidRegion
;
113 // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
115 if ((Length
> mSmmIoLibInternalMaximumSupportMemAddress
) ||
116 (BaseAddress
> mSmmIoLibInternalMaximumSupportMemAddress
) ||
117 ((Length
!= 0) && (BaseAddress
> (mSmmIoLibInternalMaximumSupportMemAddress
- (Length
- 1)))) ) {
123 "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
126 mSmmIoLibInternalMaximumSupportMemAddress
132 // Check override for valid MMIO region
134 if (mSmmIoLibReadyToLock
) {
135 InValidRegion
= FALSE
;
136 for (Index
= 0; Index
< mSmmIoLibGcdMemNumberOfDesc
; Index
++) {
137 Desc
= &mSmmIoLibGcdMemSpace
[Index
];
138 if ((Desc
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
139 (BaseAddress
>= Desc
->BaseAddress
) &&
140 ((BaseAddress
+ Length
) <= (Desc
->BaseAddress
+ Desc
->Length
))) {
141 InValidRegion
= TRUE
;
145 if (!InValidRegion
) {
148 "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
159 Merge continous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
161 @param[in, out] GcdMemoryMap A pointer to the buffer in which firmware places
162 the current GCD memory map.
163 @param[in, out] NumberOfDescriptors A pointer to the number of the
164 GcdMemoryMap buffer. On input, this is the number of
165 the current GCD memory map. On output,
166 it is the number of new GCD memory map after merge.
171 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMap
,
172 IN OUT UINTN
*NumberOfDescriptors
175 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMapEntry
;
176 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*GcdMemoryMapEnd
;
177 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*NewGcdMemoryMapEntry
;
178 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*NextGcdMemoryMapEntry
;
180 GcdMemoryMapEntry
= GcdMemoryMap
;
181 NewGcdMemoryMapEntry
= GcdMemoryMap
;
182 GcdMemoryMapEnd
= (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*) ((UINT8
*) GcdMemoryMap
+ (*NumberOfDescriptors
) * sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
183 while ((UINTN
)GcdMemoryMapEntry
< (UINTN
)GcdMemoryMapEnd
) {
184 CopyMem (NewGcdMemoryMapEntry
, GcdMemoryMapEntry
, sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR
));
185 NextGcdMemoryMapEntry
= GcdMemoryMapEntry
+ 1;
188 if (((UINTN
)NextGcdMemoryMapEntry
< (UINTN
)GcdMemoryMapEnd
) &&
189 (GcdMemoryMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) && (NextGcdMemoryMapEntry
->GcdMemoryType
== EfiGcdMemoryTypeMemoryMappedIo
) &&
190 ((GcdMemoryMapEntry
->BaseAddress
+ GcdMemoryMapEntry
->Length
) == NextGcdMemoryMapEntry
->BaseAddress
)) {
191 GcdMemoryMapEntry
->Length
+= NextGcdMemoryMapEntry
->Length
;
192 if (NewGcdMemoryMapEntry
!= GcdMemoryMapEntry
) {
193 NewGcdMemoryMapEntry
->Length
+= NextGcdMemoryMapEntry
->Length
;
196 NextGcdMemoryMapEntry
= NextGcdMemoryMapEntry
+ 1;
199 GcdMemoryMapEntry
= NextGcdMemoryMapEntry
- 1;
204 GcdMemoryMapEntry
= GcdMemoryMapEntry
+ 1;
205 NewGcdMemoryMapEntry
= NewGcdMemoryMapEntry
+ 1;
208 *NumberOfDescriptors
= ((UINTN
)NewGcdMemoryMapEntry
- (UINTN
)GcdMemoryMap
) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR
);
214 Notification for SMM EndOfDxe protocol.
216 @param[in] Protocol Points to the protocol's unique identifier.
217 @param[in] Interface Points to the interface instance.
218 @param[in] Handle The handle on which the interface was installed.
220 @retval EFI_SUCCESS Notification runs successfully.
224 SmmIoLibInternalEndOfDxeNotify (
225 IN CONST EFI_GUID
*Protocol
,
230 UINTN NumberOfDescriptors
;
231 EFI_GCD_MEMORY_SPACE_DESCRIPTOR
*MemSpaceMap
;
234 Status
= gDS
->GetMemorySpaceMap (&NumberOfDescriptors
, &MemSpaceMap
);
235 if (!EFI_ERROR (Status
)) {
237 MergeGcdMmioEntry (MemSpaceMap
, &NumberOfDescriptors
);
239 mSmmIoLibGcdMemSpace
= AllocateCopyPool (NumberOfDescriptors
* sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR
), MemSpaceMap
);
240 ASSERT_EFI_ERROR (Status
);
241 if (EFI_ERROR (Status
)) {
242 gBS
->FreePool (MemSpaceMap
);
246 mSmmIoLibGcdMemNumberOfDesc
= NumberOfDescriptors
;
247 gBS
->FreePool (MemSpaceMap
);
254 Notification for SMM ReadyToLock protocol.
256 @param[in] Protocol Points to the protocol's unique identifier.
257 @param[in] Interface Points to the interface instance.
258 @param[in] Handle The handle on which the interface was installed.
260 @retval EFI_SUCCESS Notification runs successfully.
264 SmmIoLibInternalReadyToLockNotify (
265 IN CONST EFI_GUID
*Protocol
,
270 mSmmIoLibReadyToLock
= TRUE
;
275 The constructor function initializes the Smm IO library
277 @param ImageHandle The firmware allocated handle for the EFI image.
278 @param SystemTable A pointer to the EFI System Table.
280 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
285 SmmIoLibConstructor (
286 IN EFI_HANDLE ImageHandle
,
287 IN EFI_SYSTEM_TABLE
*SystemTable
293 // Calculate and save maximum support address
295 SmmIoLibInternalCalculateMaximumSupportAddress ();
298 // Register EndOfDxe to get GCD resource map
300 Status
= gSmst
->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid
, SmmIoLibInternalEndOfDxeNotify
, &mSmmIoLibRegistrationEndOfDxe
);
301 ASSERT_EFI_ERROR (Status
);
304 // Register ready to lock so that we can know when to check valid resource region
306 Status
= gSmst
->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid
, SmmIoLibInternalReadyToLockNotify
, &mSmmIoLibRegistrationReadyToLock
);
307 ASSERT_EFI_ERROR (Status
);
313 The destructor function frees resource used in the Smm IO library
315 @param[in] ImageHandle The firmware allocated handle for the EFI image.
316 @param[in] SystemTable A pointer to the EFI System Table.
318 @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
323 IN EFI_HANDLE ImageHandle
,
324 IN EFI_SYSTEM_TABLE
*SystemTable
327 gSmst
->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid
, NULL
, &mSmmIoLibRegistrationEndOfDxe
);
328 gSmst
->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid
, NULL
, &mSmmIoLibRegistrationReadyToLock
);