]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/SmmIoLib/SmmIoLib.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / SmmIoLib / SmmIoLib.c
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
14 #include <PiSmm.h>
15
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>
26
27 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace = NULL;
28 UINTN mSmmIoLibGcdMemNumberOfDesc = 0;
29
30 EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress = 0;
31
32 VOID *mSmmIoLibRegistrationEndOfDxe;
33 VOID *mSmmIoLibRegistrationReadyToLock;
34
35 BOOLEAN mSmmIoLibReadyToLock = FALSE;
36
37 /**
38 Calculate and save the maximum support address.
39
40 **/
41 VOID
42 SmmIoLibInternalCalculateMaximumSupportAddress (
43 VOID
44 )
45 {
46 VOID *Hob;
47 UINT32 RegEax;
48 UINT8 MemPhysicalAddressBits;
49
50 //
51 // Get physical address bits supported.
52 //
53 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
54 if (Hob != NULL) {
55 MemPhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
56 } else {
57 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
58 if (RegEax >= 0x80000008) {
59 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
60 MemPhysicalAddressBits = (UINT8) RegEax;
61 } else {
62 MemPhysicalAddressBits = 36;
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 // Overflow happen
114 //
115 DEBUG ((
116 DEBUG_ERROR,
117 "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
118 BaseAddress,
119 Length,
120 mSmmIoLibInternalMaximumSupportMemAddress
121 ));
122 return FALSE;
123 }
124
125 //
126 // Check override for valid MMIO region
127 //
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;
136 }
137 }
138
139 if (!InValidRegion) {
140 DEBUG ((
141 DEBUG_ERROR,
142 "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
143 BaseAddress,
144 Length
145 ));
146 return FALSE;
147 }
148 }
149 return TRUE;
150 }
151
152 /**
153 Merge continuous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
154
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.
161 **/
162 STATIC
163 VOID
164 MergeGcdMmioEntry (
165 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMap,
166 IN OUT UINTN *NumberOfDescriptors
167 )
168 {
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;
173
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;
180
181 do {
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;
188 }
189
190 NextGcdMemoryMapEntry = NextGcdMemoryMapEntry + 1;
191 continue;
192 } else {
193 GcdMemoryMapEntry = NextGcdMemoryMapEntry - 1;
194 break;
195 }
196 } while (TRUE);
197
198 GcdMemoryMapEntry = GcdMemoryMapEntry + 1;
199 NewGcdMemoryMapEntry = NewGcdMemoryMapEntry + 1;
200 }
201
202 *NumberOfDescriptors = ((UINTN)NewGcdMemoryMapEntry - (UINTN)GcdMemoryMap) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR);
203
204 return ;
205 }
206
207 /**
208 Notification for SMM EndOfDxe protocol.
209
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.
213
214 @retval EFI_SUCCESS Notification runs successfully.
215 @retval EFI_OUT_OF_RESOURCES No enough resources to save GCD MMIO map.
216 **/
217 EFI_STATUS
218 EFIAPI
219 SmmIoLibInternalEndOfDxeNotify (
220 IN CONST EFI_GUID *Protocol,
221 IN VOID *Interface,
222 IN EFI_HANDLE Handle
223 )
224 {
225 UINTN NumberOfDescriptors;
226 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
227 EFI_STATUS Status;
228
229 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
230 if (!EFI_ERROR (Status)) {
231
232 MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);
233
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;
239 }
240
241 mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;
242 gBS->FreePool (MemSpaceMap);
243 }
244
245 return EFI_SUCCESS;
246 }
247
248 /**
249 Notification for SMM ReadyToLock protocol.
250
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.
254
255 @retval EFI_SUCCESS Notification runs successfully.
256 **/
257 EFI_STATUS
258 EFIAPI
259 SmmIoLibInternalReadyToLockNotify (
260 IN CONST EFI_GUID *Protocol,
261 IN VOID *Interface,
262 IN EFI_HANDLE Handle
263 )
264 {
265 mSmmIoLibReadyToLock = TRUE;
266 return EFI_SUCCESS;
267 }
268
269 /**
270 The constructor function initializes the Smm IO library
271
272 @param ImageHandle The firmware allocated handle for the EFI image.
273 @param SystemTable A pointer to the EFI System Table.
274
275 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
276
277 **/
278 EFI_STATUS
279 EFIAPI
280 SmmIoLibConstructor (
281 IN EFI_HANDLE ImageHandle,
282 IN EFI_SYSTEM_TABLE *SystemTable
283 )
284 {
285 EFI_STATUS Status;
286
287 //
288 // Calculate and save maximum support address
289 //
290 SmmIoLibInternalCalculateMaximumSupportAddress ();
291
292 //
293 // Register EndOfDxe to get GCD resource map
294 //
295 Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);
296 ASSERT_EFI_ERROR (Status);
297
298 //
299 // Register ready to lock so that we can know when to check valid resource region
300 //
301 Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify, &mSmmIoLibRegistrationReadyToLock);
302 ASSERT_EFI_ERROR (Status);
303
304 return EFI_SUCCESS;
305 }
306
307 /**
308 The destructor function frees resource used in the Smm IO library
309
310 @param[in] ImageHandle The firmware allocated handle for the EFI image.
311 @param[in] SystemTable A pointer to the EFI System Table.
312
313 @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
314 **/
315 EFI_STATUS
316 EFIAPI
317 SmmIoLibDestructor (
318 IN EFI_HANDLE ImageHandle,
319 IN EFI_SYSTEM_TABLE *SystemTable
320 )
321 {
322 gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mSmmIoLibRegistrationEndOfDxe);
323 gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mSmmIoLibRegistrationReadyToLock);
324 return EFI_SUCCESS;
325 }