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
;
99 RuntimeDriverCalculateEfiHdrCrc (
100 IN OUT EFI_TABLE_HEADER
*Hdr
106 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
107 internal function. The EFI Boot Services Table can not be used because
108 the EFI Boot Services Table was destroyed at ExitBootServices()
112 Hdr - Pointer to an EFI standard header
125 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
131 RuntimeDriverRegisterImage (
132 IN EFI_RUNTIME_ARCH_PROTOCOL
*This
,
133 IN EFI_PHYSICAL_ADDRESS ImageBase
,
135 IN VOID
*RelocationData
141 When a SetVirtualAddressMap() is performed all the runtime images loaded by
142 DXE must be fixed up with the new virtual address map. To facilitate this the
143 Runtime Architectural Protocol needs to be informed of every runtime driver
144 that is registered. All the runtime images loaded by DXE should be registered
145 with this service by the DXE Core when ExitBootServices() is called. The
146 images that are registered with this service must have successfully been
147 loaded into memory with the Boot Service LoadImage(). As a result, no
148 parameter checking needs to be performed.
152 This - The EFI_RUNTIME_ARCH_PROTOCOL instance.
154 ImageBase - Start of image that has been loaded in memory. It is either
155 a pointer to the DOS or PE header of the image.
157 ImageSize - Size of the image in bytes.
159 RelocationData - Information about the fixups that were performed on ImageBase
160 when it was loaded into memory. This information is needed
161 when the virtual mode fix-ups are reapplied so that data that
162 has been programmatically updated will not be fixed up. If
163 code updates a global variable the code is responsible for
164 fixing up the variable for virtual mode.
168 EFI_SUCCESS - The ImageBase has been registered.
172 RUNTIME_IMAGE_RELOCATION_DATA
*RuntimeImage
;
174 if (mMyLoadedImage
->ImageBase
== (VOID
*) (UINTN
) ImageBase
) {
176 // We don't want to relocate our selves, as we only run in physical mode.
181 RuntimeImage
= &mRuntimeImageBuffer
[mRuntimeImageNumber
];
182 mRuntimeImageNumber
++;
183 ASSERT (mRuntimeImageNumber
< MAX_RUNTIME_IMAGE_NUM
);
185 RuntimeImage
->Valid
= TRUE
;
186 RuntimeImage
->ImageBase
= ImageBase
;
187 RuntimeImage
->ImageSize
= ImageSize
;
188 RuntimeImage
->RelocationData
= RelocationData
;
190 InsertTailList (&mRelocationList
, &RuntimeImage
->Link
);
196 RuntimeDriverRegisterEvent (
197 IN EFI_RUNTIME_ARCH_PROTOCOL
*This
,
199 IN EFI_TPL NotifyTpl
,
200 IN EFI_EVENT_NOTIFY NotifyFunction
,
201 IN VOID
*NotifyContext
,
208 This function is used to support the required runtime events. Currently only
209 runtime events of type EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE needs to be
210 registered with this service. All the runtime events that exist in the DXE
211 Core should be registered with this service when ExitBootServices() is called.
212 All the events that are registered with this service must have been created
213 with the Boot Service CreateEvent(). As a result, no parameter checking needs
218 This - The EFI_RUNTIME_ARCH_PROTOCOL instance.
220 Type - The same as Type passed into CreateEvent().
222 NotifyTpl - The same as NotifyTpl passed into CreateEvent().
224 NotifyFunction - The same as NotifyFunction passed into CreateEvent().
226 NotifyContext - The same as NotifyContext passed into CreateEvent().
228 Event - The EFI_EVENT returned by CreateEvent(). Event must be in
233 EFI_SUCCESS - The Event has been registered.
237 RUNTIME_NOTIFY_EVENT_DATA
*RuntimeEvent
;
239 RuntimeEvent
= &mRuntimeEventBuffer
[mRuntimeEventNumber
];
240 mRuntimeEventNumber
++;
241 ASSERT (mRuntimeEventNumber
< MAX_RUNTIME_EVENT_NUM
);
243 RuntimeEvent
->Type
= Type
;
244 RuntimeEvent
->NotifyTpl
= NotifyTpl
;
245 RuntimeEvent
->NotifyFunction
= NotifyFunction
;
246 RuntimeEvent
->NotifyContext
= NotifyContext
;
247 RuntimeEvent
->Event
= Event
;
249 InsertTailList (&mEventList
, &RuntimeEvent
->Link
);
255 RuntimeDriverConvertPointer (
256 IN UINTN DebugDisposition
,
257 IN OUT VOID
**ConvertAddress
261 VOID
*PlabelConvertAddress
;
262 UINT64 VirtEndOfRange
;
263 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
267 // Make sure ConvertAddress is a valid pointer
269 if (ConvertAddress
== NULL
) {
270 return EFI_INVALID_PARAMETER
;
273 // Get the address to convert
275 Address
= (UINTN
) *ConvertAddress
;
278 // If this is a null pointer, return if it's allowed
281 if (DebugDisposition
& EFI_OPTIONAL_POINTER
) {
285 return EFI_INVALID_PARAMETER
;
288 PlabelConvertAddress
= NULL
;
289 VirtEntry
= mVirtualMap
;
290 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
292 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
293 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
294 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
297 ASSERT ((VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
299 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
300 if (Address
>= VirtEntry
->PhysicalStart
) {
301 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
302 if (Address
< VirtEndOfRange
) {
304 // Compute new address
306 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
308 } else if (Address
< (VirtEndOfRange
+ 0x200000)) {
310 // On Itanium GP defines a window +/- 2 MB inside an image.
311 // The compiler may asign a GP value outside of the image. Thus
312 // it could fall out side of any of our valid regions
314 PlabelConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
319 VirtEntry
= NextMemoryDescriptor (VirtEntry
, mVirtualMapDescriptorSize
);
322 if (DebugDisposition
& EFI_IPF_GP_POINTER
) {
324 // If it's an IPF GP and the GP was outside the image handle that case.
326 if (PlabelConvertAddress
!= NULL
) {
327 *ConvertAddress
= PlabelConvertAddress
;
332 return EFI_NOT_FOUND
;
336 RuntimeDriverConvertInternalPointer (
337 IN OUT VOID
**ConvertAddress
340 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
345 RuntimeDriverSetVirtualAddressMap (
346 IN UINTN MemoryMapSize
,
347 IN UINTN DescriptorSize
,
348 IN UINT32 DescriptorVersion
,
349 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
352 RUNTIME_NOTIFY_EVENT_DATA
*RuntimeEvent
;
353 RUNTIME_IMAGE_RELOCATION_DATA
*RuntimeImage
;
357 EFI_DRIVER_OS_HANDOFF_HEADER
*DriverOsHandoffHeader
;
358 EFI_DRIVER_OS_HANDOFF
*DriverOsHandoff
;
361 // Can only switch to virtual addresses once the memory map is locked down,
362 // and can only set it once
364 if (!EfiAtRuntime () || mEfiVirtualMode
) {
365 return EFI_UNSUPPORTED
;
368 // Only understand the original descriptor format
370 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
371 return EFI_INVALID_PARAMETER
;
374 // BugBug: Add code here to verify the memory map. We should
375 // cache a copy of the system memory map in the EFI System Table
376 // as a GUID pointer pair.
379 // Make sure all virtual translations are satisfied
381 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
384 // BugBug :The following code does not work hence commented out.
385 // Need to be replaced by something that works.
387 // VirtEntry = VirtualMap;
388 // for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
389 // if (((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) &&
390 // (VirtEntry->VirtualStart != 0) ) {
391 // return EFI_NO_MAPPING;
393 // VirtEntry = NextMemoryDescriptor(VirtEntry, DescriptorSize);
396 // We are now committed to go to virtual mode, so lets get to it!
398 mEfiVirtualMode
= TRUE
;
401 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
402 // globals we need to parse the virtual address map.
404 mVirtualMapDescriptorSize
= DescriptorSize
;
405 mVirtualMap
= VirtualMap
;
408 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
409 // The core call RuntimeDriverRegisterEvent() for
410 // every runtime event and we stored them in the mEventList
413 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
414 // check whether in Runtime or not (this is judged by looking at
415 // mEfiAtRuntime global So this ReportStatusCode will work
419 (EFI_SOFTWARE_EFI_BOOT_SERVICE
| EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP
)
423 // BugBug - Commented out for now because the status code driver is not
424 // ready for runtime yet. The Status Code driver calls data hub with does
425 // a bunch of Boot Service things (locks, AllocatePool) and hangs somewhere
428 // ReportStatusCode (
429 // EfiProgressCode, EfiMaxErrorSeverity,
430 // 0x03, 0x01, 12, // ReadyToBoot Progress code
431 // 0x00, 30, L"ConvertPointer"
434 for (Link
= mEventList
.ForwardLink
; Link
!= &mEventList
; Link
= Link
->ForwardLink
) {
435 RuntimeEvent
= _CR (Link
, RUNTIME_NOTIFY_EVENT_DATA
, Link
);
436 if ((RuntimeEvent
->Type
& EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
437 RuntimeEvent
->NotifyFunction (
439 RuntimeEvent
->NotifyContext
444 // Relocate runtime images. Runtime images loaded before the runtime AP was
445 // started will not be relocated, since they would not have gotten registered.
446 // This includes the code in this file.
448 for (Link
= mRelocationList
.ForwardLink
; Link
!= &mRelocationList
; Link
= Link
->ForwardLink
) {
449 RuntimeImage
= _CR (Link
, RUNTIME_IMAGE_RELOCATION_DATA
, Link
);
450 if (RuntimeImage
->Valid
) {
451 RelocatePeImageForRuntime (RuntimeImage
);
455 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
456 // and recompute the CRC-32
458 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
459 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
460 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
461 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
462 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
463 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
464 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
465 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
466 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
467 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
470 // Convert the UGA OS Handoff Table if it is present in the Configuration Table
472 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
473 if (CompareGuid (&mLocalEfiUgaIoProtocolGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
474 DriverOsHandoffHeader
= gST
->ConfigurationTable
[Index
].VendorTable
;
475 for (Index1
= 0; Index1
< DriverOsHandoffHeader
->NumberOfEntries
; Index1
++) {
476 DriverOsHandoff
= (EFI_DRIVER_OS_HANDOFF
*)
478 (UINTN
) DriverOsHandoffHeader
+
479 DriverOsHandoffHeader
->HeaderSize
+
481 DriverOsHandoffHeader
->SizeOfEntries
483 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &DriverOsHandoff
->DevicePath
);
484 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &DriverOsHandoff
->PciRomImage
);
487 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &(gST
->ConfigurationTable
[Index
].VendorTable
));
491 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
493 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
494 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
495 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
496 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
499 // At this point, gRT and gST are physical pointers, but the contents of these tables
500 // have been converted to runtime.
503 // mVirtualMap is only valid during SetVirtualAddressMap() call
512 RuntimeDriverInitialize (
513 IN EFI_HANDLE ImageHandle
,
514 IN EFI_SYSTEM_TABLE
*SystemTable
519 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
520 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
521 ASSERT macros (they do ReportStatusCode).
524 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
528 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
530 Other - Return value from gBS->InstallMultipleProtocolInterfaces
537 // This image needs to be exclued from relocation for virtual mode, so cache
538 // a copy of the Loaded Image protocol to test later.
540 Status
= gBS
->HandleProtocol (
542 &gEfiLoadedImageProtocolGuid
,
543 (VOID
**) &mMyLoadedImage
545 ASSERT_EFI_ERROR (Status
);
548 // Initialize the table used to compute 32-bit CRCs
550 RuntimeDriverInitializeCrc32Table ();
553 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
555 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
556 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
557 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
560 // Install the Runtime Architectural Protocol onto a new handle
562 Status
= gBS
->InstallMultipleProtocolInterfaces (
564 &gEfiRuntimeArchProtocolGuid
,
568 ASSERT_EFI_ERROR (Status
);
570 mRuntimeImageNumber
= 0;
571 mRuntimeEventNumber
= 0;