2 Runtime Architectural Protocol as defined in the DXE CIS.
4 This code is used to produce the EFI runtime virtual switch over
6 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT
8 The transition for calling EFI Runtime functions in physical mode to calling
9 them in virtual mode is very very complex. Every pointer in needs to be
10 converted from physical mode to virtual mode. Be very careful walking linked
11 lists! Then to make it really hard the code it's self needs be relocated into
12 the new virtual address space.
14 So here is the concept. The code in this module will never ever be called in
15 virtual mode. This is the code that collects the information needed to convert
16 to virtual mode (DXE core registers runtime stuff with this code). Since this
17 code is used to fixup all runtime images, it CAN NOT fix it's self up. So some
18 code has to stay behind and that is us.
20 Also you need to be careful about when you allocate memory, as once we are in
21 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer
24 Any runtime driver that gets loaded before us will not be callable in virtual
25 mode. This is due to the fact that the DXE core can not register the info
26 needed with us. This is good, since it keeps the code in this file from
32 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.
33 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service
34 Table now contains an item named CalculateCrc32.
37 Copyright (c) 2006, Intel Corporation. <BR>
38 All rights reserved. This program and the accompanying materials
39 are licensed and made available under the terms and conditions of the BSD License
40 which accompanies this distribution. The full text of the license may be found at
41 http://opensource.org/licenses/bsd-license.php
43 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
44 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
53 EFI_MEMORY_DESCRIPTOR
*mVirtualMap
= NULL
;
54 UINTN mVirtualMapDescriptorSize
;
55 UINTN mVirtualMapMaxIndex
;
59 // The handle onto which the Runtime Architectural Protocol instance is installed
61 EFI_HANDLE mRuntimeHandle
= NULL
;
64 // The Runtime Architectural Protocol instance produced by this driver
66 EFI_RUNTIME_ARCH_PROTOCOL mRuntime
= {
67 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.ImageHead
),
68 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.EventHead
),
71 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
72 // prevent people from having pointer math bugs in their code.
73 // now you have to use *DescriptorSize to make things work.
75 sizeof (EFI_MEMORY_DESCRIPTOR
) + sizeof (UINT64
) - (sizeof (EFI_MEMORY_DESCRIPTOR
) % sizeof (UINT64
)),
76 EFI_MEMORY_DESCRIPTOR_VERSION
,
89 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
90 internal function. The EFI Boot Services Table can not be used because
91 the EFI Boot Services Table was destroyed at ExitBootServices().
92 This is a internal function.
95 @param Hdr Pointer to an EFI standard header
99 RuntimeDriverCalculateEfiHdrCrc (
100 IN OUT EFI_TABLE_HEADER
*Hdr
108 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
114 Determines the new virtual address that is to be used on subsequent memory accesses.
117 @param DebugDisposition Supplies type information for the pointer being converted.
118 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
119 for the new virtual address mappings being applied.
121 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
122 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
123 of the current memory map. This is normally fatal.
124 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
129 RuntimeDriverConvertPointer (
130 IN UINTN DebugDisposition
,
131 IN OUT VOID
**ConvertAddress
135 UINT64 VirtEndOfRange
;
136 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
140 // Make sure ConvertAddress is a valid pointer
142 if (ConvertAddress
== NULL
) {
143 return EFI_INVALID_PARAMETER
;
146 // Get the address to convert
148 Address
= (UINTN
) *ConvertAddress
;
151 // If this is a null pointer, return if it's allowed
154 if ((DebugDisposition
& EFI_OPTIONAL_POINTER
) != 0) {
158 return EFI_INVALID_PARAMETER
;
161 VirtEntry
= mVirtualMap
;
162 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
164 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
165 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
166 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
169 ASSERT (((UINTN
) VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
171 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
172 if (Address
>= VirtEntry
->PhysicalStart
) {
173 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
174 if (Address
< VirtEndOfRange
) {
176 // Compute new address
178 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
184 VirtEntry
= NEXT_MEMORY_DESCRIPTOR (VirtEntry
, mVirtualMapDescriptorSize
);
187 return EFI_NOT_FOUND
;
192 Determines the new virtual address that is to be used on subsequent memory accesses
193 for internal pointers.
194 This is a internal function.
197 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
198 for the new virtual address mappings being applied.
200 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
201 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
202 of the current memory map. This is normally fatal.
203 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
207 RuntimeDriverConvertInternalPointer (
208 IN OUT VOID
**ConvertAddress
211 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
216 Changes the runtime addressing mode of EFI firmware from physical to virtual.
219 @param MemoryMapSize The size in bytes of VirtualMap.
220 @param DescriptorSize The size in bytes of an entry in the VirtualMap.
221 @param DescriptorVersion The version of the structure entries in VirtualMap.
222 @param VirtualMap An array of memory descriptors which contain new virtual
223 address mapping information for all runtime ranges.
225 @retval EFI_SUCCESS The virtual address map has been applied.
226 @retval EFI_UNSUPPORTED EFI firmware is not at runtime, or the EFI firmware is already in
227 virtual address mapped mode.
228 @retval EFI_INVALID_PARAMETER DescriptorSize or DescriptorVersion is invalid.
229 @retval EFI_NO_MAPPING A virtual address was not supplied for a range in the memory
230 map that requires a mapping.
231 @retval EFI_NOT_FOUND A virtual address was supplied for an address that is not found
237 RuntimeDriverSetVirtualAddressMap (
238 IN UINTN MemoryMapSize
,
239 IN UINTN DescriptorSize
,
240 IN UINT32 DescriptorVersion
,
241 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
245 EFI_RUNTIME_EVENT_ENTRY
*RuntimeEvent
;
246 EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
;
248 EFI_PHYSICAL_ADDRESS VirtImageBase
;
251 // Can only switch to virtual addresses once the memory map is locked down,
252 // and can only set it once
254 if (!mRuntime
.AtRuntime
|| mRuntime
.VirtualMode
) {
255 return EFI_UNSUPPORTED
;
258 // Only understand the original descriptor format
260 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
261 return EFI_INVALID_PARAMETER
;
264 // We are now committed to go to virtual mode, so lets get to it!
266 mRuntime
.VirtualMode
= TRUE
;
269 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
270 // globals we need to parse the virtual address map.
272 mVirtualMapDescriptorSize
= DescriptorSize
;
273 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
274 mVirtualMap
= VirtualMap
;
277 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
278 // check whether in Runtime or not (this is judged by looking at
279 // mEfiAtRuntime global So this ReportStatusCode will work
281 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdStatusCodeValueSetVirtualAddressMap
));
284 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
285 // All runtime events are stored in a list in Runtime AP.
287 for (Link
= mRuntime
.EventHead
.ForwardLink
; Link
!= &mRuntime
.EventHead
; Link
= Link
->ForwardLink
) {
288 RuntimeEvent
= BASE_CR (Link
, EFI_RUNTIME_EVENT_ENTRY
, Link
);
289 if ((RuntimeEvent
->Type
& EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
290 RuntimeEvent
->NotifyFunction (
292 RuntimeEvent
->NotifyContext
298 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
300 for (Link
= mRuntime
.ImageHead
.ForwardLink
; Link
!= &mRuntime
.ImageHead
; Link
= Link
->ForwardLink
) {
301 RuntimeImage
= BASE_CR (Link
, EFI_RUNTIME_IMAGE_ENTRY
, Link
);
303 // We don't want to relocate our selves, as we only run in physical mode.
305 if (mMyImageBase
!= RuntimeImage
->ImageBase
) {
307 VirtImageBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
;
308 Status
= RuntimeDriverConvertPointer (0, (VOID
**) &VirtImageBase
);
309 ASSERT_EFI_ERROR (Status
);
311 PeCoffLoaderRelocateImageForRuntime (
312 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
,
314 (UINTN
) RuntimeImage
->ImageSize
,
315 RuntimeImage
->RelocationData
318 InvalidateInstructionCacheRange (RuntimeImage
->ImageBase
, (UINTN
)RuntimeImage
->ImageSize
);
323 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
324 // and recompute the CRC-32
326 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
327 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
328 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
329 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
330 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
331 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
332 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
333 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
334 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
335 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryVariableInfo
);
336 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->UpdateCapsule
);
337 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryCapsuleCapabilities
);
338 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
341 // UEFI don't require System Configuration Tables Conversion.
345 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
347 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
348 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
349 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
350 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
353 // At this point, gRT and gST are physical pointers, but the contents of these tables
354 // have been converted to runtime.
357 // mVirtualMap is only valid during SetVirtualAddressMap() call
365 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
366 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
367 ASSERT macros (they do ReportStatusCode).
370 @param ImageHandle Image handle of this driver.
371 @param SystemTable Pointer to the EFI System Table.
373 @retval EFI_SUCEESS Runtime Driver Architectural Protocol Installed
374 @return Other value if gBS->InstallMultipleProtocolInterfaces fails. Check
375 gBS->InstallMultipleProtocolInterfaces for details.
380 RuntimeDriverInitialize (
381 IN EFI_HANDLE ImageHandle
,
382 IN EFI_SYSTEM_TABLE
*SystemTable
386 EFI_LOADED_IMAGE_PROTOCOL
*MyLoadedImage
;
389 // This image needs to be exclued from relocation for virtual mode, so cache
390 // a copy of the Loaded Image protocol to test later.
392 Status
= gBS
->HandleProtocol (
394 &gEfiLoadedImageProtocolGuid
,
395 (VOID
**)&MyLoadedImage
397 ASSERT_EFI_ERROR (Status
);
398 mMyImageBase
= MyLoadedImage
->ImageBase
;
401 // Initialize the table used to compute 32-bit CRCs
403 RuntimeDriverInitializeCrc32Table ();
406 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
408 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
409 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
410 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
413 // Install the Runtime Architectural Protocol onto a new handle
415 Status
= gBS
->InstallMultipleProtocolInterfaces (
417 &gEfiRuntimeArchProtocolGuid
,
421 ASSERT_EFI_ERROR (Status
);