]>
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 | |
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 | |
33 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace = NULL;\r | |
34 | UINTN mSmmIoLibGcdMemNumberOfDesc = 0;\r | |
35 | \r | |
36 | EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress = 0;\r | |
37 | \r | |
38 | VOID *mSmmIoLibRegistrationEndOfDxe;\r | |
39 | VOID *mSmmIoLibRegistrationReadyToLock;\r | |
40 | \r | |
41 | BOOLEAN mSmmIoLibReadyToLock = FALSE;\r | |
42 | \r | |
43 | /**\r | |
44 | Calculate and save the maximum support address.\r | |
45 | \r | |
46 | **/\r | |
47 | VOID\r | |
48 | SmmIoLibInternalCalculateMaximumSupportAddress (\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 | |
99 | BOOLEAN\r | |
100 | EFIAPI\r | |
101 | SmmIsMmioValid (\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 | |
7b5d848d | 159 | Merge continuous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.\r |
f9320738 JY |
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 | |
168 | STATIC\r | |
169 | VOID\r | |
170 | MergeGcdMmioEntry (\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 | |
7b5d848d SZ |
220 | @retval EFI_SUCCESS Notification runs successfully.\r |
221 | @retval EFI_OUT_OF_RESOURCES No enough resources to save GCD MMIO map.\r | |
f9320738 JY |
222 | **/\r |
223 | EFI_STATUS\r | |
224 | EFIAPI\r | |
225 | SmmIoLibInternalEndOfDxeNotify (\r | |
226 | IN CONST EFI_GUID *Protocol,\r | |
227 | IN VOID *Interface,\r | |
228 | IN EFI_HANDLE Handle\r | |
229 | )\r | |
230 | {\r | |
231 | UINTN NumberOfDescriptors;\r | |
232 | EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;\r | |
233 | EFI_STATUS Status;\r | |
234 | \r | |
235 | Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);\r | |
236 | if (!EFI_ERROR (Status)) {\r | |
237 | \r | |
238 | MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);\r | |
239 | \r | |
240 | mSmmIoLibGcdMemSpace = AllocateCopyPool (NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);\r | |
7b5d848d SZ |
241 | ASSERT (mSmmIoLibGcdMemSpace != NULL);\r |
242 | if (mSmmIoLibGcdMemSpace == NULL) {\r | |
f9320738 | 243 | gBS->FreePool (MemSpaceMap);\r |
7b5d848d | 244 | return EFI_OUT_OF_RESOURCES;\r |
f9320738 JY |
245 | }\r |
246 | \r | |
247 | mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;\r | |
248 | gBS->FreePool (MemSpaceMap);\r | |
249 | }\r | |
250 | \r | |
251 | return EFI_SUCCESS;\r | |
252 | }\r | |
253 | \r | |
254 | /**\r | |
255 | Notification for SMM ReadyToLock protocol.\r | |
256 | \r | |
257 | @param[in] Protocol Points to the protocol's unique identifier.\r | |
258 | @param[in] Interface Points to the interface instance.\r | |
259 | @param[in] Handle The handle on which the interface was installed.\r | |
260 | \r | |
261 | @retval EFI_SUCCESS Notification runs successfully.\r | |
262 | **/\r | |
263 | EFI_STATUS\r | |
264 | EFIAPI\r | |
265 | SmmIoLibInternalReadyToLockNotify (\r | |
266 | IN CONST EFI_GUID *Protocol,\r | |
267 | IN VOID *Interface,\r | |
268 | IN EFI_HANDLE Handle\r | |
269 | )\r | |
270 | {\r | |
271 | mSmmIoLibReadyToLock = TRUE;\r | |
272 | return EFI_SUCCESS;\r | |
273 | }\r | |
274 | \r | |
275 | /**\r | |
276 | The constructor function initializes the Smm IO library\r | |
277 | \r | |
278 | @param ImageHandle The firmware allocated handle for the EFI image.\r | |
279 | @param SystemTable A pointer to the EFI System Table.\r | |
280 | \r | |
281 | @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r | |
282 | \r | |
283 | **/\r | |
284 | EFI_STATUS\r | |
285 | EFIAPI\r | |
286 | SmmIoLibConstructor (\r | |
287 | IN EFI_HANDLE ImageHandle,\r | |
288 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
289 | )\r | |
290 | {\r | |
291 | EFI_STATUS Status;\r | |
292 | \r | |
293 | //\r | |
294 | // Calculate and save maximum support address\r | |
295 | //\r | |
296 | SmmIoLibInternalCalculateMaximumSupportAddress ();\r | |
297 | \r | |
298 | //\r | |
299 | // Register EndOfDxe to get GCD resource map\r | |
300 | //\r | |
301 | Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);\r | |
302 | ASSERT_EFI_ERROR (Status);\r | |
303 | \r | |
304 | //\r | |
305 | // Register ready to lock so that we can know when to check valid resource region\r | |
306 | //\r | |
307 | Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify, &mSmmIoLibRegistrationReadyToLock);\r | |
308 | ASSERT_EFI_ERROR (Status);\r | |
309 | \r | |
310 | return EFI_SUCCESS;\r | |
311 | }\r | |
312 | \r | |
313 | /**\r | |
314 | The destructor function frees resource used in the Smm IO library\r | |
315 | \r | |
316 | @param[in] ImageHandle The firmware allocated handle for the EFI image.\r | |
317 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
318 | \r | |
319 | @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.\r | |
320 | **/\r | |
321 | EFI_STATUS\r | |
322 | EFIAPI\r | |
323 | SmmIoLibDestructor (\r | |
324 | IN EFI_HANDLE ImageHandle,\r | |
325 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
326 | )\r | |
327 | {\r | |
328 | gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mSmmIoLibRegistrationEndOfDxe);\r | |
329 | gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mSmmIoLibRegistrationReadyToLock);\r | |
330 | return EFI_SUCCESS;\r | |
331 | }\r |