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