2 Runtime Architectural Protocol as defined in the DXE CIS.
4 Copyright (c) 2006, Intel Corporation. <BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 This code is used to produce the EFI runtime virtual switch over
21 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT
23 The transition for calling EFI Runtime functions in physical mode to calling
24 them in virtual mode is very very complex. Every pointer in needs to be
25 converted from physical mode to virtual mode. Be very careful walking linked
26 lists! Then to make it really hard the code it's self needs be relocated into
27 the new virtual address space.
29 So here is the concept. The code in this module will never ever be called in
30 virtual mode. This is the code that collects the information needed to convert
31 to virtual mode (DXE core registers runtime stuff with this code). Since this
32 code is used to fixup all runtime images, it CAN NOT fix it's self up. So some
33 code has to stay behind and that is us.
35 Also you need to be careful about when you allocate memory, as once we are in
36 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer
39 Any runtime driver that gets loaded before us will not be callable in virtual
40 mode. This is due to the fact that the DXE core can not register the info
41 needed with us. This is good, since it keeps the code in this file from
47 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.
48 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service
49 Table now contains an item named CalculateCrc32.
58 EFI_MEMORY_DESCRIPTOR
*mVirtualMap
= NULL
;
59 UINTN mVirtualMapDescriptorSize
;
60 UINTN mVirtualMapMaxIndex
;
64 // The handle onto which the Runtime Architectural Protocol instance is installed
66 EFI_HANDLE mRuntimeHandle
= NULL
;
69 // The Runtime Architectural Protocol instance produced by this driver
71 EFI_RUNTIME_ARCH_PROTOCOL mRuntime
= {
72 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.ImageHead
),
73 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.EventHead
),
76 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
77 // prevent people from having pointer math bugs in their code.
78 // now you have to use *DescriptorSize to make things work.
80 sizeof (EFI_MEMORY_DESCRIPTOR
) + sizeof (UINT64
) - (sizeof (EFI_MEMORY_DESCRIPTOR
) % sizeof (UINT64
)),
81 EFI_MEMORY_DESCRIPTOR_VERSION
,
94 RuntimeDriverCalculateEfiHdrCrc (
95 IN OUT EFI_TABLE_HEADER
*Hdr
101 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
102 internal function. The EFI Boot Services Table can not be used because
103 the EFI Boot Services Table was destroyed at ExitBootServices()
107 Hdr - Pointer to an EFI standard header
120 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
126 RuntimeDriverConvertPointer (
127 IN UINTN DebugDisposition
,
128 IN OUT VOID
**ConvertAddress
134 Determines the new virtual address that is to be used on subsequent memory accesses.
138 DebugDisposition - Supplies type information for the pointer being converted.
139 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
140 for the new virtual address mappings being applied.
144 EFI_SUCCESS - The pointer pointed to by Address was modified.
145 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
146 of the current memory map. This is normally fatal.
147 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
152 UINT64 VirtEndOfRange
;
153 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
157 // Make sure ConvertAddress is a valid pointer
159 if (ConvertAddress
== NULL
) {
160 return EFI_INVALID_PARAMETER
;
163 // Get the address to convert
165 Address
= (UINTN
) *ConvertAddress
;
168 // If this is a null pointer, return if it's allowed
171 if (DebugDisposition
& EFI_OPTIONAL_POINTER
) {
175 return EFI_INVALID_PARAMETER
;
178 VirtEntry
= mVirtualMap
;
179 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
181 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
182 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
183 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
186 ASSERT (((UINTN
) VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
188 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
189 if (Address
>= VirtEntry
->PhysicalStart
) {
190 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
191 if (Address
< VirtEndOfRange
) {
193 // Compute new address
195 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
201 VirtEntry
= NextMemoryDescriptor (VirtEntry
, mVirtualMapDescriptorSize
);
204 return EFI_NOT_FOUND
;
209 RuntimeDriverConvertInternalPointer (
210 IN OUT VOID
**ConvertAddress
216 Determines the new virtual address that is to be used on subsequent memory accesses
217 for internal pointers.
221 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
222 for the new virtual address mappings being applied.
226 EFI_SUCCESS - The pointer pointed to by Address was modified.
227 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
228 of the current memory map. This is normally fatal.
229 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
233 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
238 RuntimeDriverSetVirtualAddressMap (
239 IN UINTN MemoryMapSize
,
240 IN UINTN DescriptorSize
,
241 IN UINT32 DescriptorVersion
,
242 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
248 Changes the runtime addressing mode of EFI firmware from physical to virtual.
252 MemoryMapSize - The size in bytes of VirtualMap.
253 DescriptorSize - The size in bytes of an entry in the VirtualMap.
254 DescriptorVersion - The version of the structure entries in VirtualMap.
255 VirtualMap - An array of memory descriptors which contain new virtual
256 address mapping information for all runtime ranges.
260 EFI_SUCCESS - The virtual address map has been applied.
261 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in
262 virtual address mapped mode.
263 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.
264 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory
265 map that requires a mapping.
266 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found
272 EFI_RUNTIME_EVENT_ENTRY
*RuntimeEvent
;
273 EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
;
275 EFI_PHYSICAL_ADDRESS VirtImageBase
;
278 // Can only switch to virtual addresses once the memory map is locked down,
279 // and can only set it once
281 if (!mRuntime
.AtRuntime
|| mRuntime
.VirtualMode
) {
282 return EFI_UNSUPPORTED
;
285 // Only understand the original descriptor format
287 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
288 return EFI_INVALID_PARAMETER
;
291 // We are now committed to go to virtual mode, so lets get to it!
293 mRuntime
.VirtualMode
= TRUE
;
296 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
297 // globals we need to parse the virtual address map.
299 mVirtualMapDescriptorSize
= DescriptorSize
;
300 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
301 mVirtualMap
= VirtualMap
;
304 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
305 // check whether in Runtime or not (this is judged by looking at
306 // mEfiAtRuntime global So this ReportStatusCode will work
308 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdStatusCodeValueSetVirtualAddressMap
));
311 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
312 // All runtime events are stored in a list in Runtime AP.
314 for (Link
= mRuntime
.EventHead
.ForwardLink
; Link
!= &mRuntime
.EventHead
; Link
= Link
->ForwardLink
) {
315 RuntimeEvent
= _CR (Link
, EFI_RUNTIME_EVENT_ENTRY
, Link
);
316 if ((RuntimeEvent
->Type
& EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
317 RuntimeEvent
->NotifyFunction (
319 RuntimeEvent
->NotifyContext
325 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
327 for (Link
= mRuntime
.ImageHead
.ForwardLink
; Link
!= &mRuntime
.ImageHead
; Link
= Link
->ForwardLink
) {
328 RuntimeImage
= _CR (Link
, EFI_RUNTIME_IMAGE_ENTRY
, Link
);
330 // We don't want to relocate our selves, as we only run in physical mode.
332 if (mMyImageBase
!= RuntimeImage
->ImageBase
) {
334 VirtImageBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
;
335 Status
= RuntimeDriverConvertPointer (0, (VOID
**) &VirtImageBase
);
336 ASSERT_EFI_ERROR (Status
);
338 PeCoffLoaderRelocateImageForRuntime (
339 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
,
341 (UINTN
) RuntimeImage
->ImageSize
,
342 RuntimeImage
->RelocationData
345 InvalidateInstructionCacheRange (RuntimeImage
->ImageBase
, (UINTN
)RuntimeImage
->ImageSize
);
350 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
351 // and recompute the CRC-32
353 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
354 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
355 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
356 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
357 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
358 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
359 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
360 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
361 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
362 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryVariableInfo
);
363 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->UpdateCapsule
);
364 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryCapsuleCapabilities
);
365 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
368 // UEFI don't require System Configuration Tables Conversion.
372 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
374 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
375 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
376 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
377 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
380 // At this point, gRT and gST are physical pointers, but the contents of these tables
381 // have been converted to runtime.
384 // mVirtualMap is only valid during SetVirtualAddressMap() call
393 RuntimeDriverInitialize (
394 IN EFI_HANDLE ImageHandle
,
395 IN EFI_SYSTEM_TABLE
*SystemTable
400 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
401 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
402 ASSERT macros (they do ReportStatusCode).
405 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
409 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
411 Other - Return value from gBS->InstallMultipleProtocolInterfaces
416 EFI_LOADED_IMAGE_PROTOCOL
*MyLoadedImage
;
419 // This image needs to be exclued from relocation for virtual mode, so cache
420 // a copy of the Loaded Image protocol to test later.
422 Status
= gBS
->HandleProtocol (
424 &gEfiLoadedImageProtocolGuid
,
425 (VOID
**)&MyLoadedImage
427 ASSERT_EFI_ERROR (Status
);
428 mMyImageBase
= MyLoadedImage
->ImageBase
;
431 // Initialize the table used to compute 32-bit CRCs
433 RuntimeDriverInitializeCrc32Table ();
436 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
438 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
439 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
440 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
443 // Install the Runtime Architectural Protocol onto a new handle
445 Status
= gBS
->InstallMultipleProtocolInterfaces (
447 &gEfiRuntimeArchProtocolGuid
,
451 ASSERT_EFI_ERROR (Status
);