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.
60 EFI_MEMORY_DESCRIPTOR
*mVirtualMap
= NULL
;
61 UINTN mVirtualMapDescriptorSize
;
62 UINTN mVirtualMapMaxIndex
;
66 // The handle onto which the Runtime Architectural Protocol instance is installed
68 EFI_HANDLE mRuntimeHandle
= NULL
;
71 // The Runtime Architectural Protocol instance produced by this driver
73 EFI_RUNTIME_ARCH_PROTOCOL mRuntime
= {
74 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.ImageHead
),
75 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.EventHead
),
78 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
79 // prevent people from having pointer math bugs in their code.
80 // now you have to use *DescriptorSize to make things work.
82 sizeof (EFI_MEMORY_DESCRIPTOR
) + sizeof (UINT64
) - (sizeof (EFI_MEMORY_DESCRIPTOR
) % sizeof (UINT64
)),
83 EFI_MEMORY_DESCRIPTOR_VERSION
,
96 RuntimeDriverCalculateEfiHdrCrc (
97 IN OUT EFI_TABLE_HEADER
*Hdr
103 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
104 internal function. The EFI Boot Services Table can not be used because
105 the EFI Boot Services Table was destroyed at ExitBootServices()
109 Hdr - Pointer to an EFI standard header
122 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
128 RuntimeDriverConvertPointer (
129 IN UINTN DebugDisposition
,
130 IN OUT VOID
**ConvertAddress
136 Determines the new virtual address that is to be used on subsequent memory accesses.
140 DebugDisposition - Supplies type information for the pointer being converted.
141 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
142 for the new virtual address mappings being applied.
146 EFI_SUCCESS - The pointer pointed to by Address was modified.
147 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
148 of the current memory map. This is normally fatal.
149 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
154 VOID
*PlabelConvertAddress
;
155 UINT64 VirtEndOfRange
;
156 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
160 // Make sure ConvertAddress is a valid pointer
162 if (ConvertAddress
== NULL
) {
163 return EFI_INVALID_PARAMETER
;
166 // Get the address to convert
168 Address
= (UINTN
) *ConvertAddress
;
171 // If this is a null pointer, return if it's allowed
174 if (DebugDisposition
& EFI_OPTIONAL_POINTER
) {
178 return EFI_INVALID_PARAMETER
;
181 PlabelConvertAddress
= NULL
;
182 VirtEntry
= mVirtualMap
;
183 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
185 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
186 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
187 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
190 ASSERT (((UINTN
) VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
192 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
193 if (Address
>= VirtEntry
->PhysicalStart
) {
194 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
195 if (Address
< VirtEndOfRange
) {
197 // Compute new address
199 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
201 } else if (Address
< (VirtEndOfRange
+ 0x200000)) {
203 // On Itanium GP defines a window +/- 2 MB inside an image.
204 // The compiler may asign a GP value outside of the image. Thus
205 // it could fall out side of any of our valid regions
207 PlabelConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
212 VirtEntry
= NextMemoryDescriptor (VirtEntry
, mVirtualMapDescriptorSize
);
215 if (DebugDisposition
& EFI_IPF_GP_POINTER
) {
217 // If it's an IPF GP and the GP was outside the image handle that case.
219 if (PlabelConvertAddress
!= NULL
) {
220 *ConvertAddress
= PlabelConvertAddress
;
225 return EFI_NOT_FOUND
;
230 RuntimeDriverConvertInternalPointer (
231 IN OUT VOID
**ConvertAddress
237 Determines the new virtual address that is to be used on subsequent memory accesses
238 for internal pointers.
242 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
243 for the new virtual address mappings being applied.
247 EFI_SUCCESS - The pointer pointed to by Address was modified.
248 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
249 of the current memory map. This is normally fatal.
250 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
254 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
259 RuntimeDriverSetVirtualAddressMap (
260 IN UINTN MemoryMapSize
,
261 IN UINTN DescriptorSize
,
262 IN UINT32 DescriptorVersion
,
263 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
269 Changes the runtime addressing mode of EFI firmware from physical to virtual.
273 MemoryMapSize - The size in bytes of VirtualMap.
274 DescriptorSize - The size in bytes of an entry in the VirtualMap.
275 DescriptorVersion - The version of the structure entries in VirtualMap.
276 VirtualMap - An array of memory descriptors which contain new virtual
277 address mapping information for all runtime ranges.
281 EFI_SUCCESS - The virtual address map has been applied.
282 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in
283 virtual address mapped mode.
284 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.
285 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory
286 map that requires a mapping.
287 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found
293 EFI_RUNTIME_EVENT_ENTRY
*RuntimeEvent
;
294 EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
;
298 EFI_DRIVER_OS_HANDOFF_HEADER
*DriverOsHandoffHeader
;
299 EFI_DRIVER_OS_HANDOFF
*DriverOsHandoff
;
300 EFI_PHYSICAL_ADDRESS VirtImageBase
;
301 EFI_CAPSULE_TABLE
*CapsuleTable
;
304 // Can only switch to virtual addresses once the memory map is locked down,
305 // and can only set it once
307 if (!mRuntime
.AtRuntime
|| mRuntime
.VirtualMode
) {
308 return EFI_UNSUPPORTED
;
311 // Only understand the original descriptor format
313 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
314 return EFI_INVALID_PARAMETER
;
317 // We are now committed to go to virtual mode, so lets get to it!
319 mRuntime
.VirtualMode
= TRUE
;
322 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
323 // globals we need to parse the virtual address map.
325 mVirtualMapDescriptorSize
= DescriptorSize
;
326 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
327 mVirtualMap
= VirtualMap
;
330 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
331 // check whether in Runtime or not (this is judged by looking at
332 // mEfiAtRuntime global So this ReportStatusCode will work
336 (EFI_SOFTWARE_EFI_BOOT_SERVICE
| EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP
)
340 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
341 // All runtime events are stored in a list in Runtime AP.
343 for (Link
= mRuntime
.EventHead
.ForwardLink
; Link
!= &mRuntime
.EventHead
; Link
= Link
->ForwardLink
) {
344 RuntimeEvent
= _CR (Link
, EFI_RUNTIME_EVENT_ENTRY
, Link
);
345 if ((RuntimeEvent
->Type
& EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
346 RuntimeEvent
->NotifyFunction (
348 RuntimeEvent
->NotifyContext
354 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
356 for (Link
= mRuntime
.ImageHead
.ForwardLink
; Link
!= &mRuntime
.ImageHead
; Link
= Link
->ForwardLink
) {
357 RuntimeImage
= _CR (Link
, EFI_RUNTIME_IMAGE_ENTRY
, Link
);
359 // We don't want to relocate our selves, as we only run in physical mode.
361 if (mMyImageBase
!= RuntimeImage
->ImageBase
) {
363 VirtImageBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
;
364 Status
= RuntimeDriverConvertPointer (0, (VOID
**) &VirtImageBase
);
365 ASSERT_EFI_ERROR (Status
);
367 PeCoffLoaderRelocateImageForRuntime (
368 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
,
370 (UINTN
) RuntimeImage
->ImageSize
,
371 RuntimeImage
->RelocationData
374 InvalidateInstructionCacheRange (RuntimeImage
->ImageBase
, (UINTN
)RuntimeImage
->ImageSize
);
379 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
380 // and recompute the CRC-32
382 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
383 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
384 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
385 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
386 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
387 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
388 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
389 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
390 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
391 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryVariableInfo
);
392 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->UpdateCapsule
);
393 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryCapsuleCapabilities
);
394 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
397 // Convert the UGA OS Handoff Table if it is present in the Configuration Table
399 for (Index
= 0; Index
< gST
->NumberOfTableEntries
; Index
++) {
400 if (CompareGuid (&gEfiUgaIoProtocolGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
401 DriverOsHandoffHeader
= gST
->ConfigurationTable
[Index
].VendorTable
;
402 for (Index1
= 0; Index1
< DriverOsHandoffHeader
->NumberOfEntries
; Index1
++) {
403 DriverOsHandoff
= (EFI_DRIVER_OS_HANDOFF
*)
405 (UINTN
) DriverOsHandoffHeader
+
406 DriverOsHandoffHeader
->HeaderSize
+
408 DriverOsHandoffHeader
->SizeOfEntries
410 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &DriverOsHandoff
->DevicePath
);
411 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &DriverOsHandoff
->PciRomImage
);
414 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &(gST
->ConfigurationTable
[Index
].VendorTable
));
417 if (CompareGuid (&gEfiCapsuleGuid
, &(gST
->ConfigurationTable
[Index
].VendorGuid
))) {
418 CapsuleTable
= gST
->ConfigurationTable
[Index
].VendorTable
;
419 for (Index1
= 0; Index1
< CapsuleTable
->CapsuleArrayNumber
; Index1
++) {
420 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &CapsuleTable
->CapsulePtr
[Index1
]);
422 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER
, (VOID
**) &(gST
->ConfigurationTable
[Index
].VendorTable
));
426 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
428 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
429 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
430 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
431 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
434 // At this point, gRT and gST are physical pointers, but the contents of these tables
435 // have been converted to runtime.
438 // mVirtualMap is only valid during SetVirtualAddressMap() call
447 RuntimeDriverInitialize (
448 IN EFI_HANDLE ImageHandle
,
449 IN EFI_SYSTEM_TABLE
*SystemTable
454 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
455 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
456 ASSERT macros (they do ReportStatusCode).
459 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
463 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
465 Other - Return value from gBS->InstallMultipleProtocolInterfaces
470 EFI_LOADED_IMAGE_PROTOCOL
*MyLoadedImage
;
473 // This image needs to be exclued from relocation for virtual mode, so cache
474 // a copy of the Loaded Image protocol to test later.
476 Status
= gBS
->HandleProtocol (
478 &gEfiLoadedImageProtocolGuid
,
479 (VOID
**)&MyLoadedImage
481 ASSERT_EFI_ERROR (Status
);
482 mMyImageBase
= MyLoadedImage
->ImageBase
;
485 // Initialize the table used to compute 32-bit CRCs
487 RuntimeDriverInitializeCrc32Table ();
490 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
492 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
493 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
494 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
497 // Install the Runtime Architectural Protocol onto a new handle
499 Status
= gBS
->InstallMultipleProtocolInterfaces (
501 &gEfiRuntimeArchProtocolGuid
,
505 ASSERT_EFI_ERROR (Status
);