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
,
85 Calculate the 32-bit CRC in a EFI table using the Runtime Drivers
86 internal function. The EFI Boot Services Table can not be used because
87 the EFI Boot Services Table was destroyed at ExitBootServices().
88 This is a internal function.
91 @param Hdr Pointer to an EFI standard header
95 RuntimeDriverCalculateEfiHdrCrc (
96 IN OUT EFI_TABLE_HEADER
*Hdr
104 RuntimeDriverCalculateCrc32 ((UINT8
*)Hdr
, Hdr
->HeaderSize
, &Crc
);
110 Determines the new virtual address that is to be used on subsequent memory accesses.
113 @param DebugDisposition Supplies type information for the pointer being converted.
114 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
115 for the new virtual address mappings being applied.
117 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
118 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
119 of the current memory map. This is normally fatal.
120 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
125 RuntimeDriverConvertPointer (
126 IN UINTN DebugDisposition
,
127 IN OUT VOID
**ConvertAddress
131 UINT64 VirtEndOfRange
;
132 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
136 // Make sure ConvertAddress is a valid pointer
138 if (ConvertAddress
== NULL
) {
139 return EFI_INVALID_PARAMETER
;
143 // Get the address to convert
145 Address
= (UINTN
)*ConvertAddress
;
148 // If this is a null pointer, return if it's allowed
151 if ((DebugDisposition
& EFI_OPTIONAL_PTR
) != 0) {
155 return EFI_INVALID_PARAMETER
;
158 VirtEntry
= mVirtualMap
;
159 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
161 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
162 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
163 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
166 ASSERT (((UINTN
)VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
168 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
169 if (Address
>= VirtEntry
->PhysicalStart
) {
170 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
)VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
171 if (Address
< VirtEndOfRange
) {
173 // Compute new address
175 *ConvertAddress
= (VOID
*)(Address
- (UINTN
)VirtEntry
->PhysicalStart
+ (UINTN
)VirtEntry
->VirtualStart
);
181 VirtEntry
= NEXT_MEMORY_DESCRIPTOR (VirtEntry
, mVirtualMapDescriptorSize
);
184 return EFI_NOT_FOUND
;
189 Determines the new virtual address that is to be used on subsequent memory accesses
190 for internal pointers.
191 This is a internal function.
194 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
195 for the new virtual address mappings being applied.
197 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
198 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
199 of the current memory map. This is normally fatal.
200 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
204 RuntimeDriverConvertInternalPointer (
205 IN OUT VOID
**ConvertAddress
208 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
213 Changes the runtime addressing mode of EFI firmware from physical to virtual.
216 @param MemoryMapSize The size in bytes of VirtualMap.
217 @param DescriptorSize The size in bytes of an entry in the VirtualMap.
218 @param DescriptorVersion The version of the structure entries in VirtualMap.
219 @param VirtualMap An array of memory descriptors which contain new virtual
220 address mapping information for all runtime ranges.
222 @retval EFI_SUCCESS The virtual address map has been applied.
223 @retval EFI_UNSUPPORTED EFI firmware is not at runtime, or the EFI firmware is already in
224 virtual address mapped mode.
225 @retval EFI_INVALID_PARAMETER DescriptorSize or DescriptorVersion is invalid.
226 @retval EFI_NO_MAPPING A virtual address was not supplied for a range in the memory
227 map that requires a mapping.
228 @retval EFI_NOT_FOUND A virtual address was supplied for an address that is not found
234 RuntimeDriverSetVirtualAddressMap (
235 IN UINTN MemoryMapSize
,
236 IN UINTN DescriptorSize
,
237 IN UINT32 DescriptorVersion
,
238 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
242 EFI_RUNTIME_EVENT_ENTRY
*RuntimeEvent
;
243 EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
;
245 EFI_PHYSICAL_ADDRESS VirtImageBase
;
248 // Can only switch to virtual addresses once the memory map is locked down,
249 // and can only set it once
251 if (!mRuntime
.AtRuntime
|| mRuntime
.VirtualMode
) {
252 return EFI_UNSUPPORTED
;
256 // Only understand the original descriptor format
258 if ((DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
) || (DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
))) {
259 return EFI_INVALID_PARAMETER
;
263 // We are now committed to go to virtual mode, so lets get to it!
265 mRuntime
.VirtualMode
= TRUE
;
268 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
269 // globals we need to parse the virtual address map.
271 mVirtualMapDescriptorSize
= DescriptorSize
;
272 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
273 mVirtualMap
= VirtualMap
;
276 // ReporstStatusCodeLib will check and make sure this service can be called in runtime mode.
278 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_EFI_RUNTIME_SERVICE
| EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP
));
281 // Report Status Code here since EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event will be signalled.
283 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, (EFI_SOFTWARE_DXE_BS_DRIVER
| EFI_SW_DXE_BS_PC_VIRTUAL_ADDRESS_CHANGE_EVENT
));
286 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
287 // All runtime events are stored in a list in Runtime AP.
289 for (Link
= mRuntime
.EventHead
.ForwardLink
; Link
!= &mRuntime
.EventHead
; Link
= Link
->ForwardLink
) {
290 RuntimeEvent
= BASE_CR (Link
, EFI_RUNTIME_EVENT_ENTRY
, Link
);
291 if ((RuntimeEvent
->Type
& EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
293 // Work around the bug in the Platform Init specification (v1.7),
294 // reported as Mantis#2017: "EFI_RUNTIME_EVENT_ENTRY.Event" should have
295 // type EFI_EVENT, not (EFI_EVENT*). The PI spec documents the field
296 // correctly as "The EFI_EVENT returned by CreateEvent()", but the type
297 // of the field doesn't match the natural language description. Therefore
298 // we need an explicit cast here.
300 RuntimeEvent
->NotifyFunction (
301 (EFI_EVENT
)RuntimeEvent
->Event
,
302 RuntimeEvent
->NotifyContext
308 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
310 for (Link
= mRuntime
.ImageHead
.ForwardLink
; Link
!= &mRuntime
.ImageHead
; Link
= Link
->ForwardLink
) {
311 RuntimeImage
= BASE_CR (Link
, EFI_RUNTIME_IMAGE_ENTRY
, Link
);
313 // We don't want to relocate our selves, as we only run in physical mode.
315 if (mMyImageBase
!= RuntimeImage
->ImageBase
) {
316 VirtImageBase
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
;
317 Status
= RuntimeDriverConvertPointer (0, (VOID
**)&VirtImageBase
);
318 ASSERT_EFI_ERROR (Status
);
320 PeCoffLoaderRelocateImageForRuntime (
321 (EFI_PHYSICAL_ADDRESS
)(UINTN
)RuntimeImage
->ImageBase
,
323 (UINTN
)RuntimeImage
->ImageSize
,
324 RuntimeImage
->RelocationData
327 InvalidateInstructionCacheRange (RuntimeImage
->ImageBase
, (UINTN
)RuntimeImage
->ImageSize
);
332 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
333 // and recompute the CRC-32
335 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->GetTime
);
336 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->SetTime
);
337 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->GetWakeupTime
);
338 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->SetWakeupTime
);
339 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->ResetSystem
);
340 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->GetNextHighMonotonicCount
);
341 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->GetVariable
);
342 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->SetVariable
);
343 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->GetNextVariableName
);
344 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->QueryVariableInfo
);
345 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->UpdateCapsule
);
346 RuntimeDriverConvertInternalPointer ((VOID
**)&gRT
->QueryCapsuleCapabilities
);
347 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
350 // UEFI don't require System Configuration Tables Conversion.
354 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
356 RuntimeDriverConvertInternalPointer ((VOID
**)&gST
->FirmwareVendor
);
357 RuntimeDriverConvertInternalPointer ((VOID
**)&gST
->ConfigurationTable
);
358 RuntimeDriverConvertInternalPointer ((VOID
**)&gST
->RuntimeServices
);
359 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
362 // At this point, gRT and gST are physical pointers, but the contents of these tables
363 // have been converted to runtime.
366 // mVirtualMap is only valid during SetVirtualAddressMap() call
369 mVirtualMapMaxIndex
= 0;
375 Entry Point for Runtime driver.
377 This function installs Runtime Architectural Protocol and registers CalculateCrc32 boot services table,
378 SetVirtualAddressMap & ConvertPointer runtime services table.
380 @param ImageHandle Image handle of this driver.
381 @param SystemTable a Pointer to the EFI System Table.
383 @retval EFI_SUCEESS Runtime Driver Architectural Protocol is successfully installed
384 @return Others Some error occurs when installing Runtime Driver Architectural Protocol.
389 RuntimeDriverInitialize (
390 IN EFI_HANDLE ImageHandle
,
391 IN EFI_SYSTEM_TABLE
*SystemTable
395 EFI_LOADED_IMAGE_PROTOCOL
*MyLoadedImage
;
398 // This image needs to be excluded from relocation for virtual mode, so cache
399 // a copy of the Loaded Image protocol to test later.
401 Status
= gBS
->HandleProtocol (
403 &gEfiLoadedImageProtocolGuid
,
404 (VOID
**)&MyLoadedImage
406 ASSERT_EFI_ERROR (Status
);
407 mMyImageBase
= MyLoadedImage
->ImageBase
;
410 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
412 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
413 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
414 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
417 // Install the Runtime Architectural Protocol onto a new handle
419 Status
= gBS
->InstallMultipleProtocolInterfaces (
421 &gEfiRuntimeArchProtocolGuid
,
425 ASSERT_EFI_ERROR (Status
);