1 | /** @file
|
---|
2 |
|
---|
3 | The protocol provides support to allocate, free, map and umap a DMA buffer
|
---|
4 | for bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations
|
---|
5 | must be performed on unencrypted buffer hence we use a bounce buffer to map
|
---|
6 | the guest buffer into an unencrypted DMA buffer.
|
---|
7 |
|
---|
8 | Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
|
---|
9 | Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
|
---|
10 |
|
---|
11 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
12 |
|
---|
13 | **/
|
---|
14 |
|
---|
15 | #include "AmdSevIoMmu.h"
|
---|
16 |
|
---|
17 | #define MAP_INFO_SIG SIGNATURE_64 ('M', 'A', 'P', '_', 'I', 'N', 'F', 'O')
|
---|
18 |
|
---|
19 | typedef struct {
|
---|
20 | UINT64 Signature;
|
---|
21 | LIST_ENTRY Link;
|
---|
22 | EDKII_IOMMU_OPERATION Operation;
|
---|
23 | UINTN NumberOfBytes;
|
---|
24 | UINTN NumberOfPages;
|
---|
25 | EFI_PHYSICAL_ADDRESS CryptedAddress;
|
---|
26 | EFI_PHYSICAL_ADDRESS PlainTextAddress;
|
---|
27 | } MAP_INFO;
|
---|
28 |
|
---|
29 | //
|
---|
30 | // List of the MAP_INFO structures that have been set up by IoMmuMap() and not
|
---|
31 | // yet torn down by IoMmuUnmap(). The list represents the full set of mappings
|
---|
32 | // currently in effect.
|
---|
33 | //
|
---|
34 | STATIC LIST_ENTRY mMapInfos = INITIALIZE_LIST_HEAD_VARIABLE (mMapInfos);
|
---|
35 |
|
---|
36 | #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')
|
---|
37 |
|
---|
38 | #ifndef MDEPKG_NDEBUG /* VBox addition to silence compiler warnings. */
|
---|
39 | //
|
---|
40 | // ASCII names for EDKII_IOMMU_OPERATION constants, for debug logging.
|
---|
41 | //
|
---|
42 | STATIC CONST CHAR8 * CONST
|
---|
43 | mBusMasterOperationName[EdkiiIoMmuOperationMaximum] = {
|
---|
44 | "Read",
|
---|
45 | "Write",
|
---|
46 | "CommonBuffer",
|
---|
47 | "Read64",
|
---|
48 | "Write64",
|
---|
49 | "CommonBuffer64"
|
---|
50 | };
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | //
|
---|
54 | // The following structure enables Map() and Unmap() to perform in-place
|
---|
55 | // decryption and encryption, respectively, for BusMasterCommonBuffer[64]
|
---|
56 | // operations, without dynamic memory allocation or release.
|
---|
57 | //
|
---|
58 | // Both COMMON_BUFFER_HEADER and COMMON_BUFFER_HEADER.StashBuffer are allocated
|
---|
59 | // by AllocateBuffer() and released by FreeBuffer().
|
---|
60 | //
|
---|
61 | #pragma pack (1)
|
---|
62 | typedef struct {
|
---|
63 | UINT64 Signature;
|
---|
64 |
|
---|
65 | //
|
---|
66 | // Always allocated from EfiBootServicesData type memory, and always
|
---|
67 | // encrypted.
|
---|
68 | //
|
---|
69 | VOID *StashBuffer;
|
---|
70 |
|
---|
71 | //
|
---|
72 | // Followed by the actual common buffer, starting at the next page.
|
---|
73 | //
|
---|
74 | } COMMON_BUFFER_HEADER;
|
---|
75 | #pragma pack ()
|
---|
76 |
|
---|
77 | /**
|
---|
78 | Provides the controller-specific addresses required to access system memory
|
---|
79 | from a DMA bus master. On SEV guest, the DMA operations must be performed on
|
---|
80 | shared buffer hence we allocate a bounce buffer to map the HostAddress to a
|
---|
81 | DeviceAddress. The Encryption attribute is removed from the DeviceAddress
|
---|
82 | buffer.
|
---|
83 |
|
---|
84 | @param This The protocol instance pointer.
|
---|
85 | @param Operation Indicates if the bus master is going to read or
|
---|
86 | write to system memory.
|
---|
87 | @param HostAddress The system memory address to map to the PCI
|
---|
88 | controller.
|
---|
89 | @param NumberOfBytes On input the number of bytes to map. On output
|
---|
90 | the number of bytes that were mapped.
|
---|
91 | @param DeviceAddress The resulting map address for the bus master
|
---|
92 | PCI controller to use to access the hosts
|
---|
93 | HostAddress.
|
---|
94 | @param Mapping A resulting value to pass to Unmap().
|
---|
95 |
|
---|
96 | @retval EFI_SUCCESS The range was mapped for the returned
|
---|
97 | NumberOfBytes.
|
---|
98 | @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
|
---|
99 | buffer.
|
---|
100 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
|
---|
101 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
|
---|
102 | lack of resources.
|
---|
103 | @retval EFI_DEVICE_ERROR The system hardware could not map the requested
|
---|
104 | address.
|
---|
105 |
|
---|
106 | **/
|
---|
107 | EFI_STATUS
|
---|
108 | EFIAPI
|
---|
109 | IoMmuMap (
|
---|
110 | IN EDKII_IOMMU_PROTOCOL *This,
|
---|
111 | IN EDKII_IOMMU_OPERATION Operation,
|
---|
112 | IN VOID *HostAddress,
|
---|
113 | IN OUT UINTN *NumberOfBytes,
|
---|
114 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
|
---|
115 | OUT VOID **Mapping
|
---|
116 | )
|
---|
117 | {
|
---|
118 | EFI_STATUS Status;
|
---|
119 | MAP_INFO *MapInfo;
|
---|
120 | EFI_ALLOCATE_TYPE AllocateType;
|
---|
121 | COMMON_BUFFER_HEADER *CommonBufferHeader;
|
---|
122 | VOID *DecryptionSource;
|
---|
123 |
|
---|
124 | DEBUG ((
|
---|
125 | DEBUG_VERBOSE,
|
---|
126 | "%a: Operation=%a Host=0x%p Bytes=0x%Lx\n",
|
---|
127 | __FUNCTION__,
|
---|
128 | ((Operation >= 0 &&
|
---|
129 | Operation < ARRAY_SIZE (mBusMasterOperationName)) ?
|
---|
130 | mBusMasterOperationName[Operation] :
|
---|
131 | "Invalid"),
|
---|
132 | HostAddress,
|
---|
133 | (UINT64)((NumberOfBytes == NULL) ? 0 : *NumberOfBytes)
|
---|
134 | ));
|
---|
135 |
|
---|
136 | if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||
|
---|
137 | Mapping == NULL) {
|
---|
138 | return EFI_INVALID_PARAMETER;
|
---|
139 | }
|
---|
140 |
|
---|
141 | //
|
---|
142 | // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
|
---|
143 | // called later.
|
---|
144 | //
|
---|
145 | MapInfo = AllocatePool (sizeof (MAP_INFO));
|
---|
146 | if (MapInfo == NULL) {
|
---|
147 | Status = EFI_OUT_OF_RESOURCES;
|
---|
148 | goto Failed;
|
---|
149 | }
|
---|
150 |
|
---|
151 | //
|
---|
152 | // Initialize the MAP_INFO structure, except the PlainTextAddress field
|
---|
153 | //
|
---|
154 | ZeroMem (&MapInfo->Link, sizeof MapInfo->Link);
|
---|
155 | MapInfo->Signature = MAP_INFO_SIG;
|
---|
156 | MapInfo->Operation = Operation;
|
---|
157 | MapInfo->NumberOfBytes = *NumberOfBytes;
|
---|
158 | MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);
|
---|
159 | MapInfo->CryptedAddress = (UINTN)HostAddress;
|
---|
160 |
|
---|
161 | //
|
---|
162 | // In the switch statement below, we point "MapInfo->PlainTextAddress" to the
|
---|
163 | // plaintext buffer, according to Operation. We also set "DecryptionSource".
|
---|
164 | //
|
---|
165 | MapInfo->PlainTextAddress = MAX_ADDRESS;
|
---|
166 | AllocateType = AllocateAnyPages;
|
---|
167 | DecryptionSource = (VOID *)(UINTN)MapInfo->CryptedAddress;
|
---|
168 | switch (Operation) {
|
---|
169 | //
|
---|
170 | // For BusMasterRead[64] and BusMasterWrite[64] operations, a bounce buffer
|
---|
171 | // is necessary regardless of whether the original (crypted) buffer crosses
|
---|
172 | // the 4GB limit or not -- we have to allocate a separate plaintext buffer.
|
---|
173 | // The only variable is whether the plaintext buffer should be under 4GB.
|
---|
174 | //
|
---|
175 | case EdkiiIoMmuOperationBusMasterRead:
|
---|
176 | case EdkiiIoMmuOperationBusMasterWrite:
|
---|
177 | MapInfo->PlainTextAddress = BASE_4GB - 1;
|
---|
178 | AllocateType = AllocateMaxAddress;
|
---|
179 | //
|
---|
180 | // fall through
|
---|
181 | //
|
---|
182 | case EdkiiIoMmuOperationBusMasterRead64:
|
---|
183 | case EdkiiIoMmuOperationBusMasterWrite64:
|
---|
184 | //
|
---|
185 | // Allocate the implicit plaintext bounce buffer.
|
---|
186 | //
|
---|
187 | Status = gBS->AllocatePages (
|
---|
188 | AllocateType,
|
---|
189 | EfiBootServicesData,
|
---|
190 | MapInfo->NumberOfPages,
|
---|
191 | &MapInfo->PlainTextAddress
|
---|
192 | );
|
---|
193 | if (EFI_ERROR (Status)) {
|
---|
194 | goto FreeMapInfo;
|
---|
195 | }
|
---|
196 | break;
|
---|
197 |
|
---|
198 | //
|
---|
199 | // For BusMasterCommonBuffer[64] operations, a to-be-plaintext buffer and a
|
---|
200 | // stash buffer (for in-place decryption) have been allocated already, with
|
---|
201 | // AllocateBuffer(). We only check whether the address of the to-be-plaintext
|
---|
202 | // buffer is low enough for the requested operation.
|
---|
203 | //
|
---|
204 | case EdkiiIoMmuOperationBusMasterCommonBuffer:
|
---|
205 | if ((MapInfo->CryptedAddress > BASE_4GB) ||
|
---|
206 | (EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages) >
|
---|
207 | BASE_4GB - MapInfo->CryptedAddress)) {
|
---|
208 | //
|
---|
209 | // CommonBuffer operations cannot be remapped. If the common buffer is
|
---|
210 | // above 4GB, then it is not possible to generate a mapping, so return an
|
---|
211 | // error.
|
---|
212 | //
|
---|
213 | Status = EFI_UNSUPPORTED;
|
---|
214 | goto FreeMapInfo;
|
---|
215 | }
|
---|
216 | //
|
---|
217 | // fall through
|
---|
218 | //
|
---|
219 | case EdkiiIoMmuOperationBusMasterCommonBuffer64:
|
---|
220 | //
|
---|
221 | // The buffer at MapInfo->CryptedAddress comes from AllocateBuffer().
|
---|
222 | //
|
---|
223 | MapInfo->PlainTextAddress = MapInfo->CryptedAddress;
|
---|
224 | //
|
---|
225 | // Stash the crypted data.
|
---|
226 | //
|
---|
227 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(
|
---|
228 | (UINTN)MapInfo->CryptedAddress - EFI_PAGE_SIZE
|
---|
229 | );
|
---|
230 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);
|
---|
231 | CopyMem (
|
---|
232 | CommonBufferHeader->StashBuffer,
|
---|
233 | (VOID *)(UINTN)MapInfo->CryptedAddress,
|
---|
234 | MapInfo->NumberOfBytes
|
---|
235 | );
|
---|
236 | //
|
---|
237 | // Point "DecryptionSource" to the stash buffer so that we decrypt
|
---|
238 | // it to the original location, after the switch statement.
|
---|
239 | //
|
---|
240 | DecryptionSource = CommonBufferHeader->StashBuffer;
|
---|
241 | break;
|
---|
242 |
|
---|
243 | default:
|
---|
244 | //
|
---|
245 | // Operation is invalid
|
---|
246 | //
|
---|
247 | Status = EFI_INVALID_PARAMETER;
|
---|
248 | goto FreeMapInfo;
|
---|
249 | }
|
---|
250 |
|
---|
251 | //
|
---|
252 | // Clear the memory encryption mask on the plaintext buffer.
|
---|
253 | //
|
---|
254 | Status = MemEncryptSevClearPageEncMask (
|
---|
255 | 0,
|
---|
256 | MapInfo->PlainTextAddress,
|
---|
257 | MapInfo->NumberOfPages,
|
---|
258 | TRUE
|
---|
259 | );
|
---|
260 | ASSERT_EFI_ERROR (Status);
|
---|
261 | if (EFI_ERROR (Status)) {
|
---|
262 | CpuDeadLoop ();
|
---|
263 | }
|
---|
264 |
|
---|
265 | //
|
---|
266 | // If this is a read operation from the Bus Master's point of view,
|
---|
267 | // then copy the contents of the real buffer into the mapped buffer
|
---|
268 | // so the Bus Master can read the contents of the real buffer.
|
---|
269 | //
|
---|
270 | // For BusMasterCommonBuffer[64] operations, the CopyMem() below will decrypt
|
---|
271 | // the original data (from the stash buffer) back to the original location.
|
---|
272 | //
|
---|
273 | if (Operation == EdkiiIoMmuOperationBusMasterRead ||
|
---|
274 | Operation == EdkiiIoMmuOperationBusMasterRead64 ||
|
---|
275 | Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||
|
---|
276 | Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {
|
---|
277 | CopyMem (
|
---|
278 | (VOID *) (UINTN) MapInfo->PlainTextAddress,
|
---|
279 | DecryptionSource,
|
---|
280 | MapInfo->NumberOfBytes
|
---|
281 | );
|
---|
282 | }
|
---|
283 |
|
---|
284 | //
|
---|
285 | // Track all MAP_INFO structures.
|
---|
286 | //
|
---|
287 | InsertHeadList (&mMapInfos, &MapInfo->Link);
|
---|
288 | //
|
---|
289 | // Populate output parameters.
|
---|
290 | //
|
---|
291 | *DeviceAddress = MapInfo->PlainTextAddress;
|
---|
292 | *Mapping = MapInfo;
|
---|
293 |
|
---|
294 | DEBUG ((
|
---|
295 | DEBUG_VERBOSE,
|
---|
296 | "%a: Mapping=0x%p Device(PlainText)=0x%Lx Crypted=0x%Lx Pages=0x%Lx\n",
|
---|
297 | __FUNCTION__,
|
---|
298 | MapInfo,
|
---|
299 | MapInfo->PlainTextAddress,
|
---|
300 | MapInfo->CryptedAddress,
|
---|
301 | (UINT64)MapInfo->NumberOfPages
|
---|
302 | ));
|
---|
303 |
|
---|
304 | return EFI_SUCCESS;
|
---|
305 |
|
---|
306 | FreeMapInfo:
|
---|
307 | FreePool (MapInfo);
|
---|
308 |
|
---|
309 | Failed:
|
---|
310 | *NumberOfBytes = 0;
|
---|
311 | return Status;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | Completes the Map() operation and releases any corresponding resources.
|
---|
316 |
|
---|
317 | This is an internal worker function that only extends the Map() API with
|
---|
318 | the MemoryMapLocked parameter.
|
---|
319 |
|
---|
320 | @param This The protocol instance pointer.
|
---|
321 | @param Mapping The mapping value returned from Map().
|
---|
322 | @param MemoryMapLocked The function is executing on the stack of
|
---|
323 | gBS->ExitBootServices(); changes to the UEFI
|
---|
324 | memory map are forbidden.
|
---|
325 |
|
---|
326 | @retval EFI_SUCCESS The range was unmapped.
|
---|
327 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
|
---|
328 | Map().
|
---|
329 | @retval EFI_DEVICE_ERROR The data was not committed to the target system
|
---|
330 | memory.
|
---|
331 | **/
|
---|
332 | STATIC
|
---|
333 | EFI_STATUS
|
---|
334 | EFIAPI
|
---|
335 | IoMmuUnmapWorker (
|
---|
336 | IN EDKII_IOMMU_PROTOCOL *This,
|
---|
337 | IN VOID *Mapping,
|
---|
338 | IN BOOLEAN MemoryMapLocked
|
---|
339 | )
|
---|
340 | {
|
---|
341 | MAP_INFO *MapInfo;
|
---|
342 | EFI_STATUS Status;
|
---|
343 | COMMON_BUFFER_HEADER *CommonBufferHeader;
|
---|
344 | VOID *EncryptionTarget;
|
---|
345 |
|
---|
346 | DEBUG ((
|
---|
347 | DEBUG_VERBOSE,
|
---|
348 | "%a: Mapping=0x%p MemoryMapLocked=%d\n",
|
---|
349 | __FUNCTION__,
|
---|
350 | Mapping,
|
---|
351 | MemoryMapLocked
|
---|
352 | ));
|
---|
353 |
|
---|
354 | if (Mapping == NULL) {
|
---|
355 | return EFI_INVALID_PARAMETER;
|
---|
356 | }
|
---|
357 |
|
---|
358 | MapInfo = (MAP_INFO *)Mapping;
|
---|
359 |
|
---|
360 | //
|
---|
361 | // set CommonBufferHeader to suppress incorrect compiler/analyzer warnings
|
---|
362 | //
|
---|
363 | CommonBufferHeader = NULL;
|
---|
364 |
|
---|
365 | //
|
---|
366 | // For BusMasterWrite[64] operations and BusMasterCommonBuffer[64] operations
|
---|
367 | // we have to encrypt the results, ultimately to the original place (i.e.,
|
---|
368 | // "MapInfo->CryptedAddress").
|
---|
369 | //
|
---|
370 | // For BusMasterCommonBuffer[64] operations however, this encryption has to
|
---|
371 | // land in-place, so divert the encryption to the stash buffer first.
|
---|
372 | //
|
---|
373 | EncryptionTarget = (VOID *)(UINTN)MapInfo->CryptedAddress;
|
---|
374 |
|
---|
375 | switch (MapInfo->Operation) {
|
---|
376 | case EdkiiIoMmuOperationBusMasterCommonBuffer:
|
---|
377 | case EdkiiIoMmuOperationBusMasterCommonBuffer64:
|
---|
378 | ASSERT (MapInfo->PlainTextAddress == MapInfo->CryptedAddress);
|
---|
379 |
|
---|
380 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(
|
---|
381 | (UINTN)MapInfo->PlainTextAddress - EFI_PAGE_SIZE
|
---|
382 | );
|
---|
383 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);
|
---|
384 | EncryptionTarget = CommonBufferHeader->StashBuffer;
|
---|
385 | //
|
---|
386 | // fall through
|
---|
387 | //
|
---|
388 |
|
---|
389 | case EdkiiIoMmuOperationBusMasterWrite:
|
---|
390 | case EdkiiIoMmuOperationBusMasterWrite64:
|
---|
391 | CopyMem (
|
---|
392 | EncryptionTarget,
|
---|
393 | (VOID *) (UINTN) MapInfo->PlainTextAddress,
|
---|
394 | MapInfo->NumberOfBytes
|
---|
395 | );
|
---|
396 | break;
|
---|
397 |
|
---|
398 | default:
|
---|
399 | //
|
---|
400 | // nothing to encrypt after BusMasterRead[64] operations
|
---|
401 | //
|
---|
402 | break;
|
---|
403 | }
|
---|
404 |
|
---|
405 | //
|
---|
406 | // Restore the memory encryption mask on the area we used to hold the
|
---|
407 | // plaintext.
|
---|
408 | //
|
---|
409 | Status = MemEncryptSevSetPageEncMask (
|
---|
410 | 0,
|
---|
411 | MapInfo->PlainTextAddress,
|
---|
412 | MapInfo->NumberOfPages,
|
---|
413 | TRUE
|
---|
414 | );
|
---|
415 | ASSERT_EFI_ERROR (Status);
|
---|
416 | if (EFI_ERROR (Status)) {
|
---|
417 | CpuDeadLoop ();
|
---|
418 | }
|
---|
419 |
|
---|
420 | //
|
---|
421 | // For BusMasterCommonBuffer[64] operations, copy the stashed data to the
|
---|
422 | // original (now encrypted) location.
|
---|
423 | //
|
---|
424 | // For all other operations, fill the late bounce buffer (which existed as
|
---|
425 | // plaintext at some point) with zeros, and then release it (unless the UEFI
|
---|
426 | // memory map is locked).
|
---|
427 | //
|
---|
428 | if (MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||
|
---|
429 | MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {
|
---|
430 | CopyMem (
|
---|
431 | (VOID *)(UINTN)MapInfo->CryptedAddress,
|
---|
432 | CommonBufferHeader->StashBuffer,
|
---|
433 | MapInfo->NumberOfBytes
|
---|
434 | );
|
---|
435 | } else {
|
---|
436 | ZeroMem (
|
---|
437 | (VOID *)(UINTN)MapInfo->PlainTextAddress,
|
---|
438 | EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages)
|
---|
439 | );
|
---|
440 | if (!MemoryMapLocked) {
|
---|
441 | gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | //
|
---|
446 | // Forget the MAP_INFO structure, then free it (unless the UEFI memory map is
|
---|
447 | // locked).
|
---|
448 | //
|
---|
449 | RemoveEntryList (&MapInfo->Link);
|
---|
450 | if (!MemoryMapLocked) {
|
---|
451 | FreePool (MapInfo);
|
---|
452 | }
|
---|
453 |
|
---|
454 | return EFI_SUCCESS;
|
---|
455 | }
|
---|
456 |
|
---|
457 | /**
|
---|
458 | Completes the Map() operation and releases any corresponding resources.
|
---|
459 |
|
---|
460 | @param This The protocol instance pointer.
|
---|
461 | @param Mapping The mapping value returned from Map().
|
---|
462 |
|
---|
463 | @retval EFI_SUCCESS The range was unmapped.
|
---|
464 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
|
---|
465 | Map().
|
---|
466 | @retval EFI_DEVICE_ERROR The data was not committed to the target system
|
---|
467 | memory.
|
---|
468 | **/
|
---|
469 | EFI_STATUS
|
---|
470 | EFIAPI
|
---|
471 | IoMmuUnmap (
|
---|
472 | IN EDKII_IOMMU_PROTOCOL *This,
|
---|
473 | IN VOID *Mapping
|
---|
474 | )
|
---|
475 | {
|
---|
476 | return IoMmuUnmapWorker (
|
---|
477 | This,
|
---|
478 | Mapping,
|
---|
479 | FALSE // MemoryMapLocked
|
---|
480 | );
|
---|
481 | }
|
---|
482 |
|
---|
483 | /**
|
---|
484 | Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
|
---|
485 | OperationBusMasterCommonBuffer64 mapping.
|
---|
486 |
|
---|
487 | @param This The protocol instance pointer.
|
---|
488 | @param Type This parameter is not used and must be ignored.
|
---|
489 | @param MemoryType The type of memory to allocate,
|
---|
490 | EfiBootServicesData or EfiRuntimeServicesData.
|
---|
491 | @param Pages The number of pages to allocate.
|
---|
492 | @param HostAddress A pointer to store the base system memory
|
---|
493 | address of the allocated range.
|
---|
494 | @param Attributes The requested bit mask of attributes for the
|
---|
495 | allocated range.
|
---|
496 |
|
---|
497 | @retval EFI_SUCCESS The requested memory pages were allocated.
|
---|
498 | @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
|
---|
499 | attribute bits are MEMORY_WRITE_COMBINE and
|
---|
500 | MEMORY_CACHED.
|
---|
501 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
|
---|
502 | @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
|
---|
503 |
|
---|
504 | **/
|
---|
505 | EFI_STATUS
|
---|
506 | EFIAPI
|
---|
507 | IoMmuAllocateBuffer (
|
---|
508 | IN EDKII_IOMMU_PROTOCOL *This,
|
---|
509 | IN EFI_ALLOCATE_TYPE Type,
|
---|
510 | IN EFI_MEMORY_TYPE MemoryType,
|
---|
511 | IN UINTN Pages,
|
---|
512 | IN OUT VOID **HostAddress,
|
---|
513 | IN UINT64 Attributes
|
---|
514 | )
|
---|
515 | {
|
---|
516 | EFI_STATUS Status;
|
---|
517 | EFI_PHYSICAL_ADDRESS PhysicalAddress;
|
---|
518 | VOID *StashBuffer;
|
---|
519 | UINTN CommonBufferPages;
|
---|
520 | COMMON_BUFFER_HEADER *CommonBufferHeader;
|
---|
521 |
|
---|
522 | DEBUG ((
|
---|
523 | DEBUG_VERBOSE,
|
---|
524 | "%a: MemoryType=%u Pages=0x%Lx Attributes=0x%Lx\n",
|
---|
525 | __FUNCTION__,
|
---|
526 | (UINT32)MemoryType,
|
---|
527 | (UINT64)Pages,
|
---|
528 | Attributes
|
---|
529 | ));
|
---|
530 |
|
---|
531 | //
|
---|
532 | // Validate Attributes
|
---|
533 | //
|
---|
534 | if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {
|
---|
535 | return EFI_UNSUPPORTED;
|
---|
536 | }
|
---|
537 |
|
---|
538 | //
|
---|
539 | // Check for invalid inputs
|
---|
540 | //
|
---|
541 | if (HostAddress == NULL) {
|
---|
542 | return EFI_INVALID_PARAMETER;
|
---|
543 | }
|
---|
544 |
|
---|
545 | //
|
---|
546 | // The only valid memory types are EfiBootServicesData and
|
---|
547 | // EfiRuntimeServicesData
|
---|
548 | //
|
---|
549 | if (MemoryType != EfiBootServicesData &&
|
---|
550 | MemoryType != EfiRuntimeServicesData) {
|
---|
551 | return EFI_INVALID_PARAMETER;
|
---|
552 | }
|
---|
553 |
|
---|
554 | //
|
---|
555 | // We'll need a header page for the COMMON_BUFFER_HEADER structure.
|
---|
556 | //
|
---|
557 | if (Pages > MAX_UINTN - 1) {
|
---|
558 | return EFI_OUT_OF_RESOURCES;
|
---|
559 | }
|
---|
560 | CommonBufferPages = Pages + 1;
|
---|
561 |
|
---|
562 | //
|
---|
563 | // Allocate the stash in EfiBootServicesData type memory.
|
---|
564 | //
|
---|
565 | // Map() will temporarily save encrypted data in the stash for
|
---|
566 | // BusMasterCommonBuffer[64] operations, so the data can be decrypted to the
|
---|
567 | // original location.
|
---|
568 | //
|
---|
569 | // Unmap() will temporarily save plaintext data in the stash for
|
---|
570 | // BusMasterCommonBuffer[64] operations, so the data can be encrypted to the
|
---|
571 | // original location.
|
---|
572 | //
|
---|
573 | // StashBuffer always resides in encrypted memory.
|
---|
574 | //
|
---|
575 | StashBuffer = AllocatePages (Pages);
|
---|
576 | if (StashBuffer == NULL) {
|
---|
577 | return EFI_OUT_OF_RESOURCES;
|
---|
578 | }
|
---|
579 |
|
---|
580 | PhysicalAddress = (UINTN)-1;
|
---|
581 | if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {
|
---|
582 | //
|
---|
583 | // Limit allocations to memory below 4GB
|
---|
584 | //
|
---|
585 | PhysicalAddress = SIZE_4GB - 1;
|
---|
586 | }
|
---|
587 | Status = gBS->AllocatePages (
|
---|
588 | AllocateMaxAddress,
|
---|
589 | MemoryType,
|
---|
590 | CommonBufferPages,
|
---|
591 | &PhysicalAddress
|
---|
592 | );
|
---|
593 | if (EFI_ERROR (Status)) {
|
---|
594 | goto FreeStashBuffer;
|
---|
595 | }
|
---|
596 |
|
---|
597 | CommonBufferHeader = (VOID *)(UINTN)PhysicalAddress;
|
---|
598 | PhysicalAddress += EFI_PAGE_SIZE;
|
---|
599 |
|
---|
600 | CommonBufferHeader->Signature = COMMON_BUFFER_SIG;
|
---|
601 | CommonBufferHeader->StashBuffer = StashBuffer;
|
---|
602 |
|
---|
603 | *HostAddress = (VOID *)(UINTN)PhysicalAddress;
|
---|
604 |
|
---|
605 | DEBUG ((
|
---|
606 | DEBUG_VERBOSE,
|
---|
607 | "%a: Host=0x%Lx Stash=0x%p\n",
|
---|
608 | __FUNCTION__,
|
---|
609 | PhysicalAddress,
|
---|
610 | StashBuffer
|
---|
611 | ));
|
---|
612 | return EFI_SUCCESS;
|
---|
613 |
|
---|
614 | FreeStashBuffer:
|
---|
615 | FreePages (StashBuffer, Pages);
|
---|
616 | return Status;
|
---|
617 | }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | Frees memory that was allocated with AllocateBuffer().
|
---|
621 |
|
---|
622 | @param This The protocol instance pointer.
|
---|
623 | @param Pages The number of pages to free.
|
---|
624 | @param HostAddress The base system memory address of the allocated
|
---|
625 | range.
|
---|
626 |
|
---|
627 | @retval EFI_SUCCESS The requested memory pages were freed.
|
---|
628 | @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
|
---|
629 | Pages was not allocated with AllocateBuffer().
|
---|
630 |
|
---|
631 | **/
|
---|
632 | EFI_STATUS
|
---|
633 | EFIAPI
|
---|
634 | IoMmuFreeBuffer (
|
---|
635 | IN EDKII_IOMMU_PROTOCOL *This,
|
---|
636 | IN UINTN Pages,
|
---|
637 | IN VOID *HostAddress
|
---|
638 | )
|
---|
639 | {
|
---|
640 | UINTN CommonBufferPages;
|
---|
641 | COMMON_BUFFER_HEADER *CommonBufferHeader;
|
---|
642 |
|
---|
643 | DEBUG ((
|
---|
644 | DEBUG_VERBOSE,
|
---|
645 | "%a: Host=0x%p Pages=0x%Lx\n",
|
---|
646 | __FUNCTION__,
|
---|
647 | HostAddress,
|
---|
648 | (UINT64)Pages
|
---|
649 | ));
|
---|
650 |
|
---|
651 | CommonBufferPages = Pages + 1;
|
---|
652 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(
|
---|
653 | (UINTN)HostAddress - EFI_PAGE_SIZE
|
---|
654 | );
|
---|
655 |
|
---|
656 | //
|
---|
657 | // Check the signature.
|
---|
658 | //
|
---|
659 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);
|
---|
660 | if (CommonBufferHeader->Signature != COMMON_BUFFER_SIG) {
|
---|
661 | return EFI_INVALID_PARAMETER;
|
---|
662 | }
|
---|
663 |
|
---|
664 | //
|
---|
665 | // Free the stash buffer. This buffer was always encrypted, so no need to
|
---|
666 | // zero it.
|
---|
667 | //
|
---|
668 | FreePages (CommonBufferHeader->StashBuffer, Pages);
|
---|
669 |
|
---|
670 | //
|
---|
671 | // Release the common buffer itself. Unmap() has re-encrypted it in-place, so
|
---|
672 | // no need to zero it.
|
---|
673 | //
|
---|
674 | return gBS->FreePages ((UINTN)CommonBufferHeader, CommonBufferPages);
|
---|
675 | }
|
---|
676 |
|
---|
677 |
|
---|
678 | /**
|
---|
679 | Set IOMMU attribute for a system memory.
|
---|
680 |
|
---|
681 | If the IOMMU protocol exists, the system memory cannot be used
|
---|
682 | for DMA by default.
|
---|
683 |
|
---|
684 | When a device requests a DMA access for a system memory,
|
---|
685 | the device driver need use SetAttribute() to update the IOMMU
|
---|
686 | attribute to request DMA access (read and/or write).
|
---|
687 |
|
---|
688 | The DeviceHandle is used to identify which device submits the request.
|
---|
689 | The IOMMU implementation need translate the device path to an IOMMU device
|
---|
690 | ID, and set IOMMU hardware register accordingly.
|
---|
691 | 1) DeviceHandle can be a standard PCI device.
|
---|
692 | The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
|
---|
693 | The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
|
---|
694 | The memory for BusMasterCommonBuffer need set
|
---|
695 | EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
|
---|
696 | After the memory is used, the memory need set 0 to keep it being
|
---|
697 | protected.
|
---|
698 | 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
|
---|
699 | The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
|
---|
700 | EDKII_IOMMU_ACCESS_WRITE.
|
---|
701 |
|
---|
702 | @param[in] This The protocol instance pointer.
|
---|
703 | @param[in] DeviceHandle The device who initiates the DMA access
|
---|
704 | request.
|
---|
705 | @param[in] Mapping The mapping value returned from Map().
|
---|
706 | @param[in] IoMmuAccess The IOMMU access.
|
---|
707 |
|
---|
708 | @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
|
---|
709 | specified by DeviceAddress and Length.
|
---|
710 | @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
|
---|
711 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
|
---|
712 | Map().
|
---|
713 | @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
|
---|
714 | of access.
|
---|
715 | @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
|
---|
716 | @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
|
---|
717 | by the IOMMU.
|
---|
718 | @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
|
---|
719 | specified by Mapping.
|
---|
720 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
|
---|
721 | modify the IOMMU access.
|
---|
722 | @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
|
---|
723 | attempting the operation.
|
---|
724 |
|
---|
725 | **/
|
---|
726 | EFI_STATUS
|
---|
727 | EFIAPI
|
---|
728 | IoMmuSetAttribute (
|
---|
729 | IN EDKII_IOMMU_PROTOCOL *This,
|
---|
730 | IN EFI_HANDLE DeviceHandle,
|
---|
731 | IN VOID *Mapping,
|
---|
732 | IN UINT64 IoMmuAccess
|
---|
733 | )
|
---|
734 | {
|
---|
735 | return EFI_UNSUPPORTED;
|
---|
736 | }
|
---|
737 |
|
---|
738 | EDKII_IOMMU_PROTOCOL mAmdSev = {
|
---|
739 | EDKII_IOMMU_PROTOCOL_REVISION,
|
---|
740 | IoMmuSetAttribute,
|
---|
741 | IoMmuMap,
|
---|
742 | IoMmuUnmap,
|
---|
743 | IoMmuAllocateBuffer,
|
---|
744 | IoMmuFreeBuffer,
|
---|
745 | };
|
---|
746 |
|
---|
747 | /**
|
---|
748 | Notification function that is queued when gBS->ExitBootServices() signals the
|
---|
749 | EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. This function signals another
|
---|
750 | event, received as Context, and returns.
|
---|
751 |
|
---|
752 | Signaling an event in this context is safe. The UEFI spec allows
|
---|
753 | gBS->SignalEvent() to return EFI_SUCCESS only; EFI_OUT_OF_RESOURCES is not
|
---|
754 | listed, hence memory is not allocated. The edk2 implementation also does not
|
---|
755 | release memory (and we only have to care about the edk2 implementation
|
---|
756 | because EDKII_IOMMU_PROTOCOL is edk2-specific anyway).
|
---|
757 |
|
---|
758 | @param[in] Event Event whose notification function is being invoked.
|
---|
759 | Event is permitted to request the queueing of this
|
---|
760 | function at TPL_CALLBACK or TPL_NOTIFY task
|
---|
761 | priority level.
|
---|
762 |
|
---|
763 | @param[in] EventToSignal Identifies the EFI_EVENT to signal. EventToSignal
|
---|
764 | is permitted to request the queueing of its
|
---|
765 | notification function only at TPL_CALLBACK level.
|
---|
766 | **/
|
---|
767 | STATIC
|
---|
768 | VOID
|
---|
769 | EFIAPI
|
---|
770 | AmdSevExitBoot (
|
---|
771 | IN EFI_EVENT Event,
|
---|
772 | IN VOID *EventToSignal
|
---|
773 | )
|
---|
774 | {
|
---|
775 | //
|
---|
776 | // (1) The NotifyFunctions of all the events in
|
---|
777 | // EFI_EVENT_GROUP_EXIT_BOOT_SERVICES will have been queued before
|
---|
778 | // AmdSevExitBoot() is entered.
|
---|
779 | //
|
---|
780 | // (2) AmdSevExitBoot() is executing minimally at TPL_CALLBACK.
|
---|
781 | //
|
---|
782 | // (3) AmdSevExitBoot() has been queued in unspecified order relative to the
|
---|
783 | // NotifyFunctions of all the other events in
|
---|
784 | // EFI_EVENT_GROUP_EXIT_BOOT_SERVICES whose NotifyTpl is the same as
|
---|
785 | // Event's.
|
---|
786 | //
|
---|
787 | // Consequences:
|
---|
788 | //
|
---|
789 | // - If Event's NotifyTpl is TPL_CALLBACK, then some other NotifyFunctions
|
---|
790 | // queued at TPL_CALLBACK may be invoked after AmdSevExitBoot() returns.
|
---|
791 | //
|
---|
792 | // - If Event's NotifyTpl is TPL_NOTIFY, then some other NotifyFunctions
|
---|
793 | // queued at TPL_NOTIFY may be invoked after AmdSevExitBoot() returns; plus
|
---|
794 | // *all* NotifyFunctions queued at TPL_CALLBACK will be invoked strictly
|
---|
795 | // after all NotifyFunctions queued at TPL_NOTIFY, including
|
---|
796 | // AmdSevExitBoot(), have been invoked.
|
---|
797 | //
|
---|
798 | // - By signaling EventToSignal here, whose NotifyTpl is TPL_CALLBACK, we
|
---|
799 | // queue EventToSignal's NotifyFunction after the NotifyFunctions of *all*
|
---|
800 | // events in EFI_EVENT_GROUP_EXIT_BOOT_SERVICES.
|
---|
801 | //
|
---|
802 | DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
|
---|
803 | gBS->SignalEvent (EventToSignal);
|
---|
804 | }
|
---|
805 |
|
---|
806 | /**
|
---|
807 | Notification function that is queued after the notification functions of all
|
---|
808 | events in the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. The same memory
|
---|
809 | map restrictions apply.
|
---|
810 |
|
---|
811 | This function unmaps all currently existing IOMMU mappings.
|
---|
812 |
|
---|
813 | @param[in] Event Event whose notification function is being invoked. Event
|
---|
814 | is permitted to request the queueing of this function
|
---|
815 | only at TPL_CALLBACK task priority level.
|
---|
816 |
|
---|
817 | @param[in] Context Ignored.
|
---|
818 | **/
|
---|
819 | STATIC
|
---|
820 | VOID
|
---|
821 | EFIAPI
|
---|
822 | AmdSevUnmapAllMappings (
|
---|
823 | IN EFI_EVENT Event,
|
---|
824 | IN VOID *Context
|
---|
825 | )
|
---|
826 | {
|
---|
827 | LIST_ENTRY *Node;
|
---|
828 | LIST_ENTRY *NextNode;
|
---|
829 | MAP_INFO *MapInfo;
|
---|
830 |
|
---|
831 | DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
|
---|
832 |
|
---|
833 | //
|
---|
834 | // All drivers that had set up IOMMU mappings have halted their respective
|
---|
835 | // controllers by now; tear down the mappings.
|
---|
836 | //
|
---|
837 | for (Node = GetFirstNode (&mMapInfos); Node != &mMapInfos; Node = NextNode) {
|
---|
838 | NextNode = GetNextNode (&mMapInfos, Node);
|
---|
839 | MapInfo = CR (Node, MAP_INFO, Link, MAP_INFO_SIG);
|
---|
840 | IoMmuUnmapWorker (
|
---|
841 | &mAmdSev, // This
|
---|
842 | MapInfo, // Mapping
|
---|
843 | TRUE // MemoryMapLocked
|
---|
844 | );
|
---|
845 | }
|
---|
846 | }
|
---|
847 |
|
---|
848 | /**
|
---|
849 | Initialize Iommu Protocol.
|
---|
850 |
|
---|
851 | **/
|
---|
852 | EFI_STATUS
|
---|
853 | EFIAPI
|
---|
854 | AmdSevInstallIoMmuProtocol (
|
---|
855 | VOID
|
---|
856 | )
|
---|
857 | {
|
---|
858 | EFI_STATUS Status;
|
---|
859 | EFI_EVENT UnmapAllMappingsEvent;
|
---|
860 | EFI_EVENT ExitBootEvent;
|
---|
861 | EFI_HANDLE Handle;
|
---|
862 |
|
---|
863 | //
|
---|
864 | // Create the "late" event whose notification function will tear down all
|
---|
865 | // left-over IOMMU mappings.
|
---|
866 | //
|
---|
867 | Status = gBS->CreateEvent (
|
---|
868 | EVT_NOTIFY_SIGNAL, // Type
|
---|
869 | TPL_CALLBACK, // NotifyTpl
|
---|
870 | AmdSevUnmapAllMappings, // NotifyFunction
|
---|
871 | NULL, // NotifyContext
|
---|
872 | &UnmapAllMappingsEvent // Event
|
---|
873 | );
|
---|
874 | if (EFI_ERROR (Status)) {
|
---|
875 | return Status;
|
---|
876 | }
|
---|
877 |
|
---|
878 | //
|
---|
879 | // Create the event whose notification function will be queued by
|
---|
880 | // gBS->ExitBootServices() and will signal the event created above.
|
---|
881 | //
|
---|
882 | Status = gBS->CreateEvent (
|
---|
883 | EVT_SIGNAL_EXIT_BOOT_SERVICES, // Type
|
---|
884 | TPL_CALLBACK, // NotifyTpl
|
---|
885 | AmdSevExitBoot, // NotifyFunction
|
---|
886 | UnmapAllMappingsEvent, // NotifyContext
|
---|
887 | &ExitBootEvent // Event
|
---|
888 | );
|
---|
889 | if (EFI_ERROR (Status)) {
|
---|
890 | goto CloseUnmapAllMappingsEvent;
|
---|
891 | }
|
---|
892 |
|
---|
893 | Handle = NULL;
|
---|
894 | Status = gBS->InstallMultipleProtocolInterfaces (
|
---|
895 | &Handle,
|
---|
896 | &gEdkiiIoMmuProtocolGuid, &mAmdSev,
|
---|
897 | NULL
|
---|
898 | );
|
---|
899 | if (EFI_ERROR (Status)) {
|
---|
900 | goto CloseExitBootEvent;
|
---|
901 | }
|
---|
902 |
|
---|
903 | return EFI_SUCCESS;
|
---|
904 |
|
---|
905 | CloseExitBootEvent:
|
---|
906 | gBS->CloseEvent (ExitBootEvent);
|
---|
907 |
|
---|
908 | CloseUnmapAllMappingsEvent:
|
---|
909 | gBS->CloseEvent (UnmapAllMappingsEvent);
|
---|
910 |
|
---|
911 | return Status;
|
---|
912 | }
|
---|