2 This file implements Runtime Architectural Protocol as defined in the
3 Platform Initialization specification 1.0 VOLUME 2 DXE Core Interface.
5 This code is used to produce the EFI runtime virtual switch over
7 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT
9 The transition for calling EFI Runtime functions in physical mode to calling
10 them in virtual mode is very very complex. Every pointer in needs to be
11 converted from physical mode to virtual mode. Be very careful walking linked
12 lists! Then to make it really hard the code it's self needs be relocated into
13 the new virtual address space.
15 So here is the concept. The code in this module will never ever be called in
16 virtual mode. This is the code that collects the information needed to convert
17 to virtual mode (DXE core registers runtime stuff with this code). Since this
18 code is used to fix up all runtime images, it CAN NOT fix it's self up. So some
19 code has to stay behind and that is us.
21 Also you need to be careful about when you allocate memory, as once we are in
22 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer
25 Any runtime driver that gets loaded before us will not be callable in virtual
26 mode. This is due to the fact that the DXE core can not register the info
27 needed with us. This is good, since it keeps the code in this file from
33 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.
34 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service
35 Table now contains an item named CalculateCrc32.
38 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
39 SPDX-License-Identifier: BSD-2-Clause-Patent
48 EFI_MEMORY_DESCRIPTOR
*mVirtualMap
= NULL
;
49 UINTN mVirtualMapDescriptorSize
;
50 UINTN mVirtualMapMaxIndex
;
54 // The handle onto which the Runtime Architectural Protocol instance is installed
56 EFI_HANDLE mRuntimeHandle
= NULL
;
59 // The Runtime Architectural Protocol instance produced by this driver
61 EFI_RUNTIME_ARCH_PROTOCOL mRuntime
= {
62 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.ImageHead
),
63 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.EventHead
),
66 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
67 // prevent people from having pointer math bugs in their code.
68 // now you have to use *DescriptorSize to make things work.
70 sizeof (EFI_MEMORY_DESCRIPTOR
) + sizeof (UINT64
) - (sizeof (EFI_MEMORY_DESCRIPTOR
) % sizeof (UINT64
)),
71 EFI_MEMORY_DESCRIPTOR_VERSION
,
84 Calculate the 32-bit CRC in a EFI table using the Runtime Drivers
85 internal function. The EFI Boot Services Table can not be used because
86 the EFI Boot Services Table was destroyed at ExitBootServices().
87 This is a internal function.
90 @param Hdr Pointer to an EFI standard header
94 RuntimeDriverCalculateEfiHdrCrc (
95 IN OUT EFI_TABLE_HEADER
*Hdr
103 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
109 Determines the new virtual address that is to be used on subsequent memory accesses.
112 @param DebugDisposition Supplies type information for the pointer being converted.
113 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
114 for the new virtual address mappings being applied.
116 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
117 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
118 of the current memory map. This is normally fatal.
119 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
124 RuntimeDriverConvertPointer (
125 IN UINTN DebugDisposition
,
126 IN OUT VOID
**ConvertAddress
130 UINT64 VirtEndOfRange
;
131 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
135 // Make sure ConvertAddress is a valid pointer
137 if (ConvertAddress
== NULL
) {
138 return EFI_INVALID_PARAMETER
;
141 // Get the address to convert
143 Address
= (UINTN
) *ConvertAddress
;
146 // If this is a null pointer, return if it's allowed
149 if ((DebugDisposition
& EFI_OPTIONAL_PTR
) != 0) {
153 return EFI_INVALID_PARAMETER
;
156 VirtEntry
= mVirtualMap
;
157 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
159 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
160 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
161 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
164 ASSERT (((UINTN
) VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
166 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
167 if (Address
>= VirtEntry
->PhysicalStart
) {
168 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
169 if (Address
< VirtEndOfRange
) {
171 // Compute new address
173 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
179 VirtEntry
= NEXT_MEMORY_DESCRIPTOR (VirtEntry
, mVirtualMapDescriptorSize
);
182 return EFI_NOT_FOUND
;
187 Determines the new virtual address that is to be used on subsequent memory accesses
188 for internal pointers.
189 This is a internal function.
192 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
193 for the new virtual address mappings being applied.
195 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
196 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
197 of the current memory map. This is normally fatal.
198 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
202 RuntimeDriverConvertInternalPointer (
203 IN OUT VOID
**ConvertAddress
206 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
211 Changes the runtime addressing mode of EFI firmware from physical to virtual.
214 @param MemoryMapSize The size in bytes of VirtualMap.
215 @param DescriptorSize The size in bytes of an entry in the VirtualMap.
216 @param DescriptorVersion The version of the structure entries in VirtualMap.
217 @param VirtualMap An array of memory descriptors which contain new virtual
218 address mapping information for all runtime ranges.
220 @retval EFI_SUCCESS The virtual address map has been applied.
221 @retval EFI_UNSUPPORTED EFI firmware is not at runtime, or the EFI firmware is already in
222 virtual address mapped mode.
223 @retval EFI_INVALID_PARAMETER DescriptorSize or DescriptorVersion is invalid.
224 @retval EFI_NO_MAPPING A virtual address was not supplied for a range in the memory
225 map that requires a mapping.
226 @retval EFI_NOT_FOUND A virtual address was supplied for an address that is not found
232 RuntimeDriverSetVirtualAddressMap (
233 IN UINTN MemoryMapSize
,
234 IN UINTN DescriptorSize
,
235 IN UINT32 DescriptorVersion
,
236 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
240 EFI_RUNTIME_EVENT_ENTRY
*RuntimeEvent
;
241 EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
;
243 EFI_PHYSICAL_ADDRESS VirtImageBase
;
246 // Can only switch to virtual addresses once the memory map is locked down,
247 // and can only set it once
249 if (!mRuntime
.AtRuntime
|| mRuntime
.VirtualMode
) {
250 return EFI_UNSUPPORTED
;
253 // Only understand the original descriptor format
255 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
256 return EFI_INVALID_PARAMETER
;
259 // We are now committed to go to virtual mode, so lets get to it!
261 mRuntime
.VirtualMode
= TRUE
;
264 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
265 // globals we need to parse the virtual address map.
267 mVirtualMapDescriptorSize
= DescriptorSize
;
268 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
269 mVirtualMap
= VirtualMap
;
272 // ReporstStatusCodeLib will check and make sure this service can be called in runtime mode.
274 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_EFI_RUNTIME_SERVICE
| EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP
));
277 // Report Status Code here since EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event will be signalled.
279 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_VIRTUAL_ADDRESS_CHANGE_EVENT
));
282 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
283 // All runtime events are stored in a list in Runtime AP.
285 for (Link
= mRuntime
.EventHead
.ForwardLink
; Link
!= &mRuntime
.EventHead
; Link
= Link
->ForwardLink
) {
286 RuntimeEvent
= BASE_CR (Link
, EFI_RUNTIME_EVENT_ENTRY
, Link
);
287 if ((RuntimeEvent
->Type
& EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
288 RuntimeEvent
->NotifyFunction (
290 RuntimeEvent
->NotifyContext
296 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
298 for (Link
= mRuntime
.ImageHead
.ForwardLink
; Link
!= &mRuntime
.ImageHead
; Link
= Link
->ForwardLink
) {
299 RuntimeImage
= BASE_CR (Link
, EFI_RUNTIME_IMAGE_ENTRY
, Link
);
301 // We don't want to relocate our selves, as we only run in physical mode.
303 if (mMyImageBase
!= RuntimeImage
->ImageBase
) {
305 VirtImageBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
;
306 Status
= RuntimeDriverConvertPointer (0, (VOID
**) &VirtImageBase
);
307 ASSERT_EFI_ERROR (Status
);
309 PeCoffLoaderRelocateImageForRuntime (
310 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
,
312 (UINTN
) RuntimeImage
->ImageSize
,
313 RuntimeImage
->RelocationData
316 InvalidateInstructionCacheRange (RuntimeImage
->ImageBase
, (UINTN
) RuntimeImage
->ImageSize
);
321 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
322 // and recompute the CRC-32
324 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
325 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
326 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
327 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
328 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
329 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
330 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
331 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
332 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
333 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryVariableInfo
);
334 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->UpdateCapsule
);
335 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryCapsuleCapabilities
);
336 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
339 // UEFI don't require System Configuration Tables Conversion.
343 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
345 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
346 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
347 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
348 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
351 // At this point, gRT and gST are physical pointers, but the contents of these tables
352 // have been converted to runtime.
355 // mVirtualMap is only valid during SetVirtualAddressMap() call
363 Entry Point for Runtime driver.
365 This function installs Runtime Architectural Protocol and registers CalculateCrc32 boot services table,
366 SetVirtualAddressMap & ConvertPointer runtime services table.
368 @param ImageHandle Image handle of this driver.
369 @param SystemTable a Pointer to the EFI System Table.
371 @retval EFI_SUCEESS Runtime Driver Architectural Protocol is successfully installed
372 @return Others Some error occurs when installing Runtime Driver Architectural Protocol.
377 RuntimeDriverInitialize (
378 IN EFI_HANDLE ImageHandle
,
379 IN EFI_SYSTEM_TABLE
*SystemTable
383 EFI_LOADED_IMAGE_PROTOCOL
*MyLoadedImage
;
386 // This image needs to be excluded from relocation for virtual mode, so cache
387 // a copy of the Loaded Image protocol to test later.
389 Status
= gBS
->HandleProtocol (
391 &gEfiLoadedImageProtocolGuid
,
392 (VOID
**)&MyLoadedImage
394 ASSERT_EFI_ERROR (Status
);
395 mMyImageBase
= MyLoadedImage
->ImageBase
;
398 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
400 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
401 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
402 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
405 // Install the Runtime Architectural Protocol onto a new handle
407 Status
= gBS
->InstallMultipleProtocolInterfaces (
409 &gEfiRuntimeArchProtocolGuid
,
413 ASSERT_EFI_ERROR (Status
);