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.
59 EFI_MEMORY_DESCRIPTOR
*mVirtualMap
= NULL
;
60 UINTN mVirtualMapDescriptorSize
;
61 UINTN mVirtualMapMaxIndex
;
65 // The handle onto which the Runtime Architectural Protocol instance is installed
67 EFI_HANDLE mRuntimeHandle
= NULL
;
70 // The Runtime Architectural Protocol instance produced by this driver
72 EFI_RUNTIME_ARCH_PROTOCOL mRuntime
= {
73 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.ImageHead
),
74 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime
.EventHead
),
77 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
78 // prevent people from having pointer math bugs in their code.
79 // now you have to use *DescriptorSize to make things work.
81 sizeof (EFI_MEMORY_DESCRIPTOR
) + sizeof (UINT64
) - (sizeof (EFI_MEMORY_DESCRIPTOR
) % sizeof (UINT64
)),
82 EFI_MEMORY_DESCRIPTOR_VERSION
,
95 RuntimeDriverCalculateEfiHdrCrc (
96 IN OUT EFI_TABLE_HEADER
*Hdr
102 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
103 internal function. The EFI Boot Services Table can not be used because
104 the EFI Boot Services Table was destroyed at ExitBootServices()
108 Hdr - Pointer to an EFI standard header
121 RuntimeDriverCalculateCrc32 ((UINT8
*) Hdr
, Hdr
->HeaderSize
, &Crc
);
127 RuntimeDriverConvertPointer (
128 IN UINTN DebugDisposition
,
129 IN OUT VOID
**ConvertAddress
135 Determines the new virtual address that is to be used on subsequent memory accesses.
139 DebugDisposition - Supplies type information for the pointer being converted.
140 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
141 for the new virtual address mappings being applied.
145 EFI_SUCCESS - The pointer pointed to by Address was modified.
146 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
147 of the current memory map. This is normally fatal.
148 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
153 VOID
*PlabelConvertAddress
;
154 UINT64 VirtEndOfRange
;
155 EFI_MEMORY_DESCRIPTOR
*VirtEntry
;
159 // Make sure ConvertAddress is a valid pointer
161 if (ConvertAddress
== NULL
) {
162 return EFI_INVALID_PARAMETER
;
165 // Get the address to convert
167 Address
= (UINTN
) *ConvertAddress
;
170 // If this is a null pointer, return if it's allowed
173 if (DebugDisposition
& EFI_OPTIONAL_POINTER
) {
177 return EFI_INVALID_PARAMETER
;
180 PlabelConvertAddress
= NULL
;
181 VirtEntry
= mVirtualMap
;
182 for (Index
= 0; Index
< mVirtualMapMaxIndex
; Index
++) {
184 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
185 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
186 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
189 ASSERT (((UINTN
) VirtEntry
->NumberOfPages
< 0xffffffff) || (sizeof (UINTN
) > 4));
191 if ((VirtEntry
->Attribute
& EFI_MEMORY_RUNTIME
) == EFI_MEMORY_RUNTIME
) {
192 if (Address
>= VirtEntry
->PhysicalStart
) {
193 VirtEndOfRange
= VirtEntry
->PhysicalStart
+ (((UINTN
) VirtEntry
->NumberOfPages
) * EFI_PAGE_SIZE
);
194 if (Address
< VirtEndOfRange
) {
196 // Compute new address
198 *ConvertAddress
= (VOID
*) (Address
- (UINTN
) VirtEntry
->PhysicalStart
+ (UINTN
) VirtEntry
->VirtualStart
);
204 VirtEntry
= NextMemoryDescriptor (VirtEntry
, mVirtualMapDescriptorSize
);
207 return EFI_NOT_FOUND
;
212 RuntimeDriverConvertInternalPointer (
213 IN OUT VOID
**ConvertAddress
219 Determines the new virtual address that is to be used on subsequent memory accesses
220 for internal pointers.
224 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
225 for the new virtual address mappings being applied.
229 EFI_SUCCESS - The pointer pointed to by Address was modified.
230 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
231 of the current memory map. This is normally fatal.
232 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
236 return RuntimeDriverConvertPointer (0x0, ConvertAddress
);
241 RuntimeDriverSetVirtualAddressMap (
242 IN UINTN MemoryMapSize
,
243 IN UINTN DescriptorSize
,
244 IN UINT32 DescriptorVersion
,
245 IN EFI_MEMORY_DESCRIPTOR
*VirtualMap
251 Changes the runtime addressing mode of EFI firmware from physical to virtual.
255 MemoryMapSize - The size in bytes of VirtualMap.
256 DescriptorSize - The size in bytes of an entry in the VirtualMap.
257 DescriptorVersion - The version of the structure entries in VirtualMap.
258 VirtualMap - An array of memory descriptors which contain new virtual
259 address mapping information for all runtime ranges.
263 EFI_SUCCESS - The virtual address map has been applied.
264 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in
265 virtual address mapped mode.
266 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.
267 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory
268 map that requires a mapping.
269 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found
275 EFI_RUNTIME_EVENT_ENTRY
*RuntimeEvent
;
276 EFI_RUNTIME_IMAGE_ENTRY
*RuntimeImage
;
278 EFI_PHYSICAL_ADDRESS VirtImageBase
;
281 // Can only switch to virtual addresses once the memory map is locked down,
282 // and can only set it once
284 if (!mRuntime
.AtRuntime
|| mRuntime
.VirtualMode
) {
285 return EFI_UNSUPPORTED
;
288 // Only understand the original descriptor format
290 if (DescriptorVersion
!= EFI_MEMORY_DESCRIPTOR_VERSION
|| DescriptorSize
< sizeof (EFI_MEMORY_DESCRIPTOR
)) {
291 return EFI_INVALID_PARAMETER
;
294 // We are now committed to go to virtual mode, so lets get to it!
296 mRuntime
.VirtualMode
= TRUE
;
299 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
300 // globals we need to parse the virtual address map.
302 mVirtualMapDescriptorSize
= DescriptorSize
;
303 mVirtualMapMaxIndex
= MemoryMapSize
/ DescriptorSize
;
304 mVirtualMap
= VirtualMap
;
307 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
308 // check whether in Runtime or not (this is judged by looking at
309 // mEfiAtRuntime global So this ReportStatusCode will work
311 REPORT_STATUS_CODE (EFI_PROGRESS_CODE
, PcdGet32 (PcdStatusCodeValueSetVirtualAddressMap
));
314 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
315 // All runtime events are stored in a list in Runtime AP.
317 for (Link
= mRuntime
.EventHead
.ForwardLink
; Link
!= &mRuntime
.EventHead
; Link
= Link
->ForwardLink
) {
318 RuntimeEvent
= _CR (Link
, EFI_RUNTIME_EVENT_ENTRY
, Link
);
319 if ((RuntimeEvent
->Type
& EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE
) {
320 RuntimeEvent
->NotifyFunction (
322 RuntimeEvent
->NotifyContext
328 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
330 for (Link
= mRuntime
.ImageHead
.ForwardLink
; Link
!= &mRuntime
.ImageHead
; Link
= Link
->ForwardLink
) {
331 RuntimeImage
= _CR (Link
, EFI_RUNTIME_IMAGE_ENTRY
, Link
);
333 // We don't want to relocate our selves, as we only run in physical mode.
335 if (mMyImageBase
!= RuntimeImage
->ImageBase
) {
337 VirtImageBase
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
;
338 Status
= RuntimeDriverConvertPointer (0, (VOID
**) &VirtImageBase
);
339 ASSERT_EFI_ERROR (Status
);
341 PeCoffLoaderRelocateImageForRuntime (
342 (EFI_PHYSICAL_ADDRESS
) (UINTN
) RuntimeImage
->ImageBase
,
344 (UINTN
) RuntimeImage
->ImageSize
,
345 RuntimeImage
->RelocationData
348 InvalidateInstructionCacheRange (RuntimeImage
->ImageBase
, (UINTN
)RuntimeImage
->ImageSize
);
353 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
354 // and recompute the CRC-32
356 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetTime
);
357 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetTime
);
358 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetWakeupTime
);
359 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetWakeupTime
);
360 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->ResetSystem
);
361 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextHighMonotonicCount
);
362 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetVariable
);
363 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->SetVariable
);
364 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->GetNextVariableName
);
365 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryVariableInfo
);
366 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->UpdateCapsule
);
367 RuntimeDriverConvertInternalPointer ((VOID
**) &gRT
->QueryCapsuleCapabilities
);
368 RuntimeDriverCalculateEfiHdrCrc (&gRT
->Hdr
);
371 // BugBug: PI requires System Configuration Tables Conversion.
372 // Currently, we do not implement it.
376 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
378 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->FirmwareVendor
);
379 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->ConfigurationTable
);
380 RuntimeDriverConvertInternalPointer ((VOID
**) &gST
->RuntimeServices
);
381 RuntimeDriverCalculateEfiHdrCrc (&gST
->Hdr
);
384 // At this point, gRT and gST are physical pointers, but the contents of these tables
385 // have been converted to runtime.
388 // mVirtualMap is only valid during SetVirtualAddressMap() call
397 RuntimeDriverInitialize (
398 IN EFI_HANDLE ImageHandle
,
399 IN EFI_SYSTEM_TABLE
*SystemTable
404 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
405 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
406 ASSERT macros (they do ReportStatusCode).
409 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
413 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
415 Other - Return value from gBS->InstallMultipleProtocolInterfaces
420 EFI_LOADED_IMAGE_PROTOCOL
*MyLoadedImage
;
423 // This image needs to be exclued from relocation for virtual mode, so cache
424 // a copy of the Loaded Image protocol to test later.
426 Status
= gBS
->HandleProtocol (
428 &gEfiLoadedImageProtocolGuid
,
429 (VOID
**)&MyLoadedImage
431 ASSERT_EFI_ERROR (Status
);
432 mMyImageBase
= MyLoadedImage
->ImageBase
;
435 // Initialize the table used to compute 32-bit CRCs
437 RuntimeDriverInitializeCrc32Table ();
440 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
442 gBS
->CalculateCrc32
= RuntimeDriverCalculateCrc32
;
443 gRT
->SetVirtualAddressMap
= RuntimeDriverSetVirtualAddressMap
;
444 gRT
->ConvertPointer
= RuntimeDriverConvertPointer
;
447 // Install the Runtime Architectural Protocol onto a new handle
449 Status
= gBS
->InstallMultipleProtocolInterfaces (
451 &gEfiRuntimeArchProtocolGuid
,
455 ASSERT_EFI_ERROR (Status
);