3 Copyright (c) 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Runtime Architectural Protocol as defined in the DXE CIS
20 This code is used to produce the EFI runtime virtual switch over
22 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT
24 The transition for calling EFI Runtime functions in physical mode to calling
25 them in virtual mode is very very complex. Every pointer in needs to be
26 converted from physical mode to virtual mode. Be very careful walking linked
27 lists! Then to make it really hard the code it's self needs be relocated into
28 the new virtual address space.
30 So here is the concept. The code in this module will never ever be called in
31 virtual mode. This is the code that collects the information needed to convert
32 to virtual mode (DXE core registers runtime stuff with this code). Since this
33 code is used to fixup all runtime images, it CAN NOT fix it's self up. So some
34 code has to stay behind and that is us.
36 Also you need to be careful about when you allocate memory, as once we are in
37 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer
40 Any runtime driver that gets loaded before us will not be callable in virtual
41 mode. This is due to the fact that the DXE core can not register the info
42 needed with us. This is good, since it keeps the code in this file from
48 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.
49 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service
50 Table now contains an item named CalculateCrc32.
58 // This is a only short term solution.
59 // There is a change coming to the Runtime AP that
60 // will make it so the Runtime driver will not have to allocate any buffers.
62 #define MAX_RUNTIME_IMAGE_NUM (64)
63 #define MAX_RUNTIME_EVENT_NUM (64)
64 RUNTIME_IMAGE_RELOCATION_DATA mRuntimeImageBuffer
[MAX_RUNTIME_IMAGE_NUM
];
65 RUNTIME_NOTIFY_EVENT_DATA mRuntimeEventBuffer
[MAX_RUNTIME_EVENT_NUM
];
66 UINTN mRuntimeImageNumber
;
67 UINTN mRuntimeEventNumber
;
70 // The handle onto which the Runtime Architectural Protocol instance is installed
72 EFI_HANDLE mRuntimeHandle
= NULL
;
75 // The Runtime Architectural Protocol instance produced by this driver
77 EFI_RUNTIME_ARCH_PROTOCOL mRuntime
= {
78 RuntimeDriverRegisterImage
,
79 RuntimeDriverRegisterEvent
85 LIST_ENTRY mRelocationList
= INITIALIZE_LIST_HEAD_VARIABLE(mRelocationList
);
86 LIST_ENTRY mEventList
= INITIALIZE_LIST_HEAD_VARIABLE(mEventList
);
87 BOOLEAN mEfiVirtualMode
= FALSE
;
88 EFI_GUID mLocalEfiUgaIoProtocolGuid
= EFI_UGA_IO_PROTOCOL_GUID
;
89 EFI_MEMORY_DESCRIPTOR
*mVirtualMap
= NULL
;
90 UINTN mVirtualMapDescriptorSize
;
91 UINTN mVirtualMapMaxIndex
;
93 EFI_LOADED_IMAGE_PROTOCOL
*mMyLoadedImage
;
95 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
96 STATIC EFI_GUID mEfiCapsuleHeaderGuid
= EFI_CAPSULE_GUID
;
102 RuntimeDriverCalculateEfiHdrCrc (
103 IN OUT EFI_TABLE_HEADER
*Hdr
109 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
110 internal function. The EFI Boot Services Table can not be used because
111 the EFI Boot Services Table was destroyed at ExitBootServices()
115 Hdr - Pointer to an EFI standard header
128 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
134 RuntimeDriverRegisterImage (
135 IN EFI_RUNTIME_ARCH_PROTOCOL
*This
,
136 IN EFI_PHYSICAL_ADDRESS ImageBase
,
138 IN VOID
*RelocationData
144 When a SetVirtualAddressMap() is performed all the runtime images loaded by
145 DXE must be fixed up with the new virtual address map. To facilitate this the
146 Runtime Architectural Protocol needs to be informed of every runtime driver
147 that is registered. All the runtime images loaded by DXE should be registered
148 with this service by the DXE Core when ExitBootServices() is called. The
149 images that are registered with this service must have successfully been
150 loaded into memory with the Boot Service LoadImage(). As a result, no
151 parameter checking needs to be performed.
155 This - The EFI_RUNTIME_ARCH_PROTOCOL instance.
157 ImageBase - Start of image that has been loaded in memory. It is either
158 a pointer to the DOS or PE header of the image.
160 ImageSize - Size of the image in bytes.
162 RelocationData - Information about the fixups that were performed on ImageBase
163 when it was loaded into memory. This information is needed
164 when the virtual mode fix-ups are reapplied so that data that
165 has been programmatically updated will not be fixed up. If
166 code updates a global variable the code is responsible for
167 fixing up the variable for virtual mode.
171 EFI_SUCCESS - The ImageBase has been registered.
175 RUNTIME_IMAGE_RELOCATION_DATA
*RuntimeImage
;
177 if (mMyLoadedImage
->ImageBase
== (VOID
*) (UINTN
) ImageBase
) {
179 // We don't want to relocate our selves, as we only run in physical mode.
184 RuntimeImage
= &mRuntimeImageBuffer
[mRuntimeImageNumber
];
185 mRuntimeImageNumber
++;
186 ASSERT (mRuntimeImageNumber
< MAX_RUNTIME_IMAGE_NUM
);
188 RuntimeImage
->Valid
= TRUE
;
189 RuntimeImage
->ImageBase
= ImageBase
;
190 RuntimeImage
->ImageSize
= ImageSize
;
191 RuntimeImage
->RelocationData
= RelocationData
;
193 InsertTailList (&mRelocationList
, &RuntimeImage
->Link
);
199 RuntimeDriverRegisterEvent (
200 IN EFI_RUNTIME_ARCH_PROTOCOL
*This
,
202 IN EFI_TPL NotifyTpl
,
203 IN EFI_EVENT_NOTIFY NotifyFunction
,
204 IN VOID
*NotifyContext
,
211 This function is used to support the required runtime events. Currently only
212 runtime events of type EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE needs to be
213 registered with this service. All the runtime events that exist in the DXE
214 Core should be registered with this service when ExitBootServices() is called.
215 All the events that are registered with this service must have been created
216 with the Boot Service CreateEvent(). As a result, no parameter checking needs
221 This - The EFI_RUNTIME_ARCH_PROTOCOL instance.
223 Type - The same as Type passed into CreateEvent().
225 NotifyTpl - The same as NotifyTpl passed into CreateEvent().
227 NotifyFunction - The same as NotifyFunction passed into CreateEvent().
229 NotifyContext - The same as NotifyContext passed into CreateEvent().
231 Event - The EFI_EVENT returned by CreateEvent(). Event must be in
236 EFI_SUCCESS - The Event has been registered.
240 RUNTIME_NOTIFY_EVENT_DATA
*RuntimeEvent
;
242 RuntimeEvent
= &mRuntimeEventBuffer
[mRuntimeEventNumber
];
243 mRuntimeEventNumber
++;
244 ASSERT (mRuntimeEventNumber
< MAX_RUNTIME_EVENT_NUM
);
246 RuntimeEvent
->Type
= Type
;
247 RuntimeEvent
->NotifyTpl
= NotifyTpl
;
248 RuntimeEvent
->NotifyFunction
= NotifyFunction
;
249 RuntimeEvent
->NotifyContext
= NotifyContext
;
250 RuntimeEvent
->Event
= Event
;
252 InsertTailList (&mEventList
, &RuntimeEvent
->Link
);
258 RuntimeDriverConvertPointer (
259 IN UINTN DebugDisposition
,
260 IN OUT VOID
**ConvertAddress
264 VOID
*PlabelConvertAddress
;
265 UINT64 VirtEndOfRange
;
266 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
270 // Make sure ConvertAddress is a valid pointer
272 if (ConvertAddress
== NULL
) {
273 return EFI_INVALID_PARAMETER
;
276 // Get the address to convert
278 Address
= (UINTN
) *ConvertAddress
;
281 // If this is a null pointer, return if it's allowed
284 if (DebugDisposition
& EFI_OPTIONAL_POINTER
) {
288 return EFI_INVALID_PARAMETER
;
291 PlabelConvertAddress
= NULL
;
292 VirtEntry
= mVirtualMap
;
293 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
295 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
296 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
297 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
300 ASSERT ((VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
302 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
303 if (Address
>= VirtEntry
->PhysicalStart
) {
304 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
305 if (Address
< VirtEndOfRange
) {
307 // Compute new address
309 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
311 } else if (Address
< (VirtEndOfRange
+ 0x200000)) {
313 // On Itanium GP defines a window +/- 2 MB inside an image.
314 // The compiler may asign a GP value outside of the image. Thus
315 // it could fall out side of any of our valid regions
317 PlabelConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
322 VirtEntry
= NextMemoryDescriptor (VirtEntry
, mVirtualMapDescriptorSize
);
325 if (DebugDisposition
& EFI_IPF_GP_POINTER
) {
327 // If it's an IPF GP and the GP was outside the image handle that case.
329 if (PlabelConvertAddress
!= NULL
) {
330 *ConvertAddress
= PlabelConvertAddress
;
335 return EFI_NOT_FOUND
;
339 RuntimeDriverConvertInternalPointer (
340 IN OUT VOID
**ConvertAddress
343 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
348 RuntimeDriverSetVirtualAddressMap (
349 IN UINTN MemoryMapSize
,
350 IN UINTN DescriptorSize
,
351 IN UINT32 DescriptorVersion
,
352 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
355 RUNTIME_NOTIFY_EVENT_DATA
*RuntimeEvent
;
356 RUNTIME_IMAGE_RELOCATION_DATA
*RuntimeImage
;
360 EFI_DRIVER_OS_HANDOFF_HEADER
*DriverOsHandoffHeader
;
361 EFI_DRIVER_OS_HANDOFF
*DriverOsHandoff
;
362 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
363 EFI_CAPSULE_TABLE
*CapsuleTable
;
367 // Can only switch to virtual addresses once the memory map is locked down,
368 // and can only set it once
370 if (!EfiAtRuntime () || mEfiVirtualMode
) {
371 return EFI_UNSUPPORTED
;
374 // Only understand the original descriptor format
376 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
377 return EFI_INVALID_PARAMETER
;
380 // BugBug: Add code here to verify the memory map. We should
381 // cache a copy of the system memory map in the EFI System Table
382 // as a GUID pointer pair.
385 // Make sure all virtual translations are satisfied
387 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
390 // BugBug :The following code does not work hence commented out.
391 // Need to be replaced by something that works.
393 // VirtEntry = VirtualMap;
394 // for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
395 // if (((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) &&
396 // (VirtEntry->VirtualStart != 0) ) {
397 // return EFI_NO_MAPPING;
399 // VirtEntry = NextMemoryDescriptor(VirtEntry, DescriptorSize);
402 // We are now committed to go to virtual mode, so lets get to it!
404 mEfiVirtualMode
= TRUE
;
407 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
408 // globals we need to parse the virtual address map.
410 mVirtualMapDescriptorSize
= DescriptorSize
;
411 mVirtualMap
= VirtualMap
;
414 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
415 // The core call RuntimeDriverRegisterEvent() for
416 // every runtime event and we stored them in the mEventList
419 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
420 // check whether in Runtime or not (this is judged by looking at
421 // mEfiAtRuntime global So this ReportStatusCode will work
425 (EFI_SOFTWARE_EFI_BOOT_SERVICE
| EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP
)
429 // BugBug - Commented out for now because the status code driver is not
430 // ready for runtime yet. The Status Code driver calls data hub with does
431 // a bunch of Boot Service things (locks, AllocatePool) and hangs somewhere
434 // ReportStatusCode (
435 // EfiProgressCode, EfiMaxErrorSeverity,
436 // 0x03, 0x01, 12, // ReadyToBoot Progress code
437 // 0x00, 30, L"ConvertPointer"
440 for (Link
= mEventList
.ForwardLink
; Link
!= &mEventList
; Link
= Link
->ForwardLink
) {
441 RuntimeEvent
= _CR (Link
, RUNTIME_NOTIFY_EVENT_DATA
, Link
);
442 if ((RuntimeEvent
->Type
& EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
443 RuntimeEvent
->NotifyFunction (
445 RuntimeEvent
->NotifyContext
450 // Relocate runtime images. Runtime images loaded before the runtime AP was
451 // started will not be relocated, since they would not have gotten registered.
452 // This includes the code in this file.
454 for (Link
= mRelocationList
.ForwardLink
; Link
!= &mRelocationList
; Link
= Link
->ForwardLink
) {
455 RuntimeImage
= _CR (Link
, RUNTIME_IMAGE_RELOCATION_DATA
, Link
);
456 if (RuntimeImage
->Valid
) {
457 RelocatePeImageForRuntime (RuntimeImage
);
461 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
462 // and recompute the CRC-32
464 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
465 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
466 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
467 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
468 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
469 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
470 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
471 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
472 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
473 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
474 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryVariableInfo
);
475 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->UpdateCapsule
);
476 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryCapsuleCapabilities
);
478 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
481 // Convert the UGA OS Handoff Table if it is present in the Configuration Table
483 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
484 if (CompareGuid (&mLocalEfiUgaIoProtocolGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
485 DriverOsHandoffHeader
= gST
->ConfigurationTable
[Index
].VendorTable
;
486 for (Index1
= 0; Index1
< DriverOsHandoffHeader
->NumberOfEntries
; Index1
++) {
487 DriverOsHandoff
= (EFI_DRIVER_OS_HANDOFF
*)
489 (UINTN
) DriverOsHandoffHeader
+
490 DriverOsHandoffHeader
->HeaderSize
+
492 DriverOsHandoffHeader
->SizeOfEntries
494 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &DriverOsHandoff
->DevicePath
);
495 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &DriverOsHandoff
->PciRomImage
);
498 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &(gST
->ConfigurationTable
[Index
].VendorTable
));
500 #if (EFI_SPECIFICATION_VERSION >= 0x00020000)
501 if (CompareGuid (&mEfiCapsuleHeaderGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
502 CapsuleTable
= gST
->ConfigurationTable
[Index
].VendorTable
;
503 for (Index1
= 0; Index1
< CapsuleTable
->CapsuleArrayNumber
; Index1
++) {
504 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &CapsuleTable
->CapsulePtr
[Index1
]);
506 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &(gST
->ConfigurationTable
[Index
].VendorTable
));
511 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
513 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
514 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
515 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
516 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
519 // At this point, gRT and gST are physical pointers, but the contents of these tables
520 // have been converted to runtime.
523 // mVirtualMap is only valid during SetVirtualAddressMap() call
532 RuntimeDriverInitialize (
533 IN EFI_HANDLE ImageHandle
,
534 IN EFI_SYSTEM_TABLE
*SystemTable
539 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
540 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
541 ASSERT macros (they do ReportStatusCode).
544 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
548 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
550 Other - Return value from gBS->InstallMultipleProtocolInterfaces
557 // This image needs to be exclued from relocation for virtual mode, so cache
558 // a copy of the Loaded Image protocol to test later.
560 Status
= gBS
->HandleProtocol (
562 &gEfiLoadedImageProtocolGuid
,
563 (VOID
**) &mMyLoadedImage
565 ASSERT_EFI_ERROR (Status
);
568 // Initialize the table used to compute 32-bit CRCs
570 RuntimeDriverInitializeCrc32Table ();
573 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
575 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
576 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
577 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
580 // Install the Runtime Architectural Protocol onto a new handle
582 Status
= gBS
->InstallMultipleProtocolInterfaces (
584 &gEfiRuntimeArchProtocolGuid
,
588 ASSERT_EFI_ERROR (Status
);
590 mRuntimeImageNumber
= 0;
591 mRuntimeEventNumber
= 0;