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