]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/SmmIoLib/SmmIoLib.c
f1cb0dace5006fbbd8c6faa079ad32ca97d9127f
[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 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
13
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.
16
17 **/
18
19
20 #include <PiSmm.h>
21
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>
32
33 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmIoLibGcdMemSpace = NULL;
34 UINTN mSmmIoLibGcdMemNumberOfDesc = 0;
35
36 EFI_PHYSICAL_ADDRESS mSmmIoLibInternalMaximumSupportMemAddress = 0;
37
38 VOID *mSmmIoLibRegistrationEndOfDxe;
39 VOID *mSmmIoLibRegistrationReadyToLock;
40
41 BOOLEAN mSmmIoLibReadyToLock = FALSE;
42
43 /**
44 Calculate and save the maximum support address.
45
46 **/
47 VOID
48 SmmIoLibInternalCalculateMaximumSupportAddress (
49 VOID
50 )
51 {
52 VOID *Hob;
53 UINT32 RegEax;
54 UINT8 MemPhysicalAddressBits;
55
56 //
57 // Get physical address bits supported.
58 //
59 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
60 if (Hob != NULL) {
61 MemPhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;
62 } else {
63 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
64 if (RegEax >= 0x80000008) {
65 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
66 MemPhysicalAddressBits = (UINT8) RegEax;
67 } else {
68 MemPhysicalAddressBits = 36;
69 }
70 }
71 //
72 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
73 //
74 ASSERT (MemPhysicalAddressBits <= 52);
75 if (MemPhysicalAddressBits > 48) {
76 MemPhysicalAddressBits = 48;
77 }
78
79 //
80 // Save the maximum support address in one global variable
81 //
82 mSmmIoLibInternalMaximumSupportMemAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)(LShiftU64 (1, MemPhysicalAddressBits) - 1);
83 DEBUG ((DEBUG_INFO, "mSmmIoLibInternalMaximumSupportMemAddress = 0x%lx\n", mSmmIoLibInternalMaximumSupportMemAddress));
84 }
85
86 /**
87 This function check if the MMIO resource is valid per processor architecture and
88 valid per platform design.
89
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.
95
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.
98 **/
99 BOOLEAN
100 EFIAPI
101 SmmIsMmioValid (
102 IN EFI_PHYSICAL_ADDRESS BaseAddress,
103 IN UINT64 Length,
104 IN EFI_GUID *Owner OPTIONAL
105 )
106 {
107 UINTN Index;
108 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
109 BOOLEAN InValidRegion;
110
111 //
112 // Check override.
113 // NOTE: (B:0->L:4G) is invalid for IA32, but (B:1->L:4G-1)/(B:4G-1->L:1) is valid.
114 //
115 if ((Length > mSmmIoLibInternalMaximumSupportMemAddress) ||
116 (BaseAddress > mSmmIoLibInternalMaximumSupportMemAddress) ||
117 ((Length != 0) && (BaseAddress > (mSmmIoLibInternalMaximumSupportMemAddress - (Length - 1)))) ) {
118 //
119 // Overflow happen
120 //
121 DEBUG ((
122 DEBUG_ERROR,
123 "SmmIsMmioValid: Overflow: BaseAddress (0x%lx) - Length (0x%lx), MaximumSupportMemAddress (0x%lx)\n",
124 BaseAddress,
125 Length,
126 mSmmIoLibInternalMaximumSupportMemAddress
127 ));
128 return FALSE;
129 }
130
131 //
132 // Check override for valid MMIO region
133 //
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;
142 }
143 }
144
145 if (!InValidRegion) {
146 DEBUG ((
147 DEBUG_ERROR,
148 "SmmIsMmioValid: Not in valid MMIO region: BaseAddress (0x%lx) - Length (0x%lx)\n",
149 BaseAddress,
150 Length
151 ));
152 return FALSE;
153 }
154 }
155 return TRUE;
156 }
157
158 /**
159 Merge continuous entries whose type is EfiGcdMemoryTypeMemoryMappedIo.
160
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.
167 **/
168 STATIC
169 VOID
170 MergeGcdMmioEntry (
171 IN OUT EFI_GCD_MEMORY_SPACE_DESCRIPTOR *GcdMemoryMap,
172 IN OUT UINTN *NumberOfDescriptors
173 )
174 {
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;
179
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;
186
187 do {
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;
194 }
195
196 NextGcdMemoryMapEntry = NextGcdMemoryMapEntry + 1;
197 continue;
198 } else {
199 GcdMemoryMapEntry = NextGcdMemoryMapEntry - 1;
200 break;
201 }
202 } while (TRUE);
203
204 GcdMemoryMapEntry = GcdMemoryMapEntry + 1;
205 NewGcdMemoryMapEntry = NewGcdMemoryMapEntry + 1;
206 }
207
208 *NumberOfDescriptors = ((UINTN)NewGcdMemoryMapEntry - (UINTN)GcdMemoryMap) / sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR);
209
210 return ;
211 }
212
213 /**
214 Notification for SMM EndOfDxe protocol.
215
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.
219
220 @retval EFI_SUCCESS Notification runs successfully.
221 @retval EFI_OUT_OF_RESOURCES No enough resources to save GCD MMIO map.
222 **/
223 EFI_STATUS
224 EFIAPI
225 SmmIoLibInternalEndOfDxeNotify (
226 IN CONST EFI_GUID *Protocol,
227 IN VOID *Interface,
228 IN EFI_HANDLE Handle
229 )
230 {
231 UINTN NumberOfDescriptors;
232 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
233 EFI_STATUS Status;
234
235 Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
236 if (!EFI_ERROR (Status)) {
237
238 MergeGcdMmioEntry (MemSpaceMap, &NumberOfDescriptors);
239
240 mSmmIoLibGcdMemSpace = AllocateCopyPool (NumberOfDescriptors * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR), MemSpaceMap);
241 ASSERT (mSmmIoLibGcdMemSpace != NULL);
242 if (mSmmIoLibGcdMemSpace == NULL) {
243 gBS->FreePool (MemSpaceMap);
244 return EFI_OUT_OF_RESOURCES;
245 }
246
247 mSmmIoLibGcdMemNumberOfDesc = NumberOfDescriptors;
248 gBS->FreePool (MemSpaceMap);
249 }
250
251 return EFI_SUCCESS;
252 }
253
254 /**
255 Notification for SMM ReadyToLock protocol.
256
257 @param[in] Protocol Points to the protocol's unique identifier.
258 @param[in] Interface Points to the interface instance.
259 @param[in] Handle The handle on which the interface was installed.
260
261 @retval EFI_SUCCESS Notification runs successfully.
262 **/
263 EFI_STATUS
264 EFIAPI
265 SmmIoLibInternalReadyToLockNotify (
266 IN CONST EFI_GUID *Protocol,
267 IN VOID *Interface,
268 IN EFI_HANDLE Handle
269 )
270 {
271 mSmmIoLibReadyToLock = TRUE;
272 return EFI_SUCCESS;
273 }
274
275 /**
276 The constructor function initializes the Smm IO library
277
278 @param ImageHandle The firmware allocated handle for the EFI image.
279 @param SystemTable A pointer to the EFI System Table.
280
281 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
282
283 **/
284 EFI_STATUS
285 EFIAPI
286 SmmIoLibConstructor (
287 IN EFI_HANDLE ImageHandle,
288 IN EFI_SYSTEM_TABLE *SystemTable
289 )
290 {
291 EFI_STATUS Status;
292
293 //
294 // Calculate and save maximum support address
295 //
296 SmmIoLibInternalCalculateMaximumSupportAddress ();
297
298 //
299 // Register EndOfDxe to get GCD resource map
300 //
301 Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, SmmIoLibInternalEndOfDxeNotify, &mSmmIoLibRegistrationEndOfDxe);
302 ASSERT_EFI_ERROR (Status);
303
304 //
305 // Register ready to lock so that we can know when to check valid resource region
306 //
307 Status = gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, SmmIoLibInternalReadyToLockNotify, &mSmmIoLibRegistrationReadyToLock);
308 ASSERT_EFI_ERROR (Status);
309
310 return EFI_SUCCESS;
311 }
312
313 /**
314 The destructor function frees resource used in the Smm IO library
315
316 @param[in] ImageHandle The firmware allocated handle for the EFI image.
317 @param[in] SystemTable A pointer to the EFI System Table.
318
319 @retval EFI_SUCCESS The deconstructor always returns EFI_SUCCESS.
320 **/
321 EFI_STATUS
322 EFIAPI
323 SmmIoLibDestructor (
324 IN EFI_HANDLE ImageHandle,
325 IN EFI_SYSTEM_TABLE *SystemTable
326 )
327 {
328 gSmst->SmmRegisterProtocolNotify (&gEfiSmmEndOfDxeProtocolGuid, NULL, &mSmmIoLibRegistrationEndOfDxe);
329 gSmst->SmmRegisterProtocolNotify (&gEfiSmmReadyToLockProtocolGuid, NULL, &mSmmIoLibRegistrationReadyToLock);
330 return EFI_SUCCESS;
331 }