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