]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Core/RuntimeDxe/Runtime.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Core / RuntimeDxe / Runtime.c
CommitLineData
fb0b259e 1/** @file\r
48557c65 2 This file implements Runtime Architectural Protocol as defined in the\r
3 Platform Initialization specification 1.0 VOLUME 2 DXE Core Interface.\r
f2abdc91 4\r
f2abdc91 5 This code is used to produce the EFI runtime virtual switch over\r
6\r
7 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT\r
8\r
9 The transition for calling EFI Runtime functions in physical mode to calling\r
10 them in virtual mode is very very complex. Every pointer in needs to be\r
11 converted from physical mode to virtual mode. Be very careful walking linked\r
12 lists! Then to make it really hard the code it's self needs be relocated into\r
13 the new virtual address space.\r
14\r
15 So here is the concept. The code in this module will never ever be called in\r
16 virtual mode. This is the code that collects the information needed to convert\r
17 to virtual mode (DXE core registers runtime stuff with this code). Since this\r
48557c65 18 code is used to fix up all runtime images, it CAN NOT fix it's self up. So some\r
f2abdc91 19 code has to stay behind and that is us.\r
20\r
21 Also you need to be careful about when you allocate memory, as once we are in\r
22 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer\r
23 allocate memory.\r
24\r
25 Any runtime driver that gets loaded before us will not be callable in virtual\r
26 mode. This is due to the fact that the DXE core can not register the info\r
27 needed with us. This is good, since it keeps the code in this file from\r
28 getting registered.\r
29\r
30\r
31Revision History:\r
32\r
33 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.\r
34 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service\r
35 Table now contains an item named CalculateCrc32.\r
36\r
9fc78752 37\r
bd1957b4 38Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
9d510e61 39SPDX-License-Identifier: BSD-2-Clause-Patent\r
9fc78752 40\r
fb0b259e 41**/\r
f2abdc91 42\r
43#include "Runtime.h"\r
44\r
45//\r
46// Global Variables\r
47//\r
1436aea4
MK
48EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r
49UINTN mVirtualMapDescriptorSize;\r
50UINTN mVirtualMapMaxIndex;\r
51VOID *mMyImageBase;\r
f2abdc91 52\r
53//\r
54// The handle onto which the Runtime Architectural Protocol instance is installed\r
55//\r
1436aea4 56EFI_HANDLE mRuntimeHandle = NULL;\r
f2abdc91 57\r
58//\r
59// The Runtime Architectural Protocol instance produced by this driver\r
60//\r
1436aea4 61EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {\r
f2abdc91 62 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),\r
63 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),\r
64\r
65 //\r
66 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will\r
67 // prevent people from having pointer math bugs in their code.\r
68 // now you have to use *DescriptorSize to make things work.\r
69 //\r
70 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),\r
71 EFI_MEMORY_DESCRIPTOR_VERSION,\r
72 0,\r
73 NULL,\r
74 NULL,\r
75 FALSE,\r
76 FALSE\r
77};\r
78\r
79//\r
80// Worker Functions\r
81//\r
1436aea4 82\r
9fc78752 83/**\r
f2abdc91 84\r
48557c65 85 Calculate the 32-bit CRC in a EFI table using the Runtime Drivers\r
f2abdc91 86 internal function. The EFI Boot Services Table can not be used because\r
9fc78752 87 the EFI Boot Services Table was destroyed at ExitBootServices().\r
88 This is a internal function.\r
f2abdc91 89\r
f2abdc91 90\r
9fc78752 91 @param Hdr Pointer to an EFI standard header\r
f2abdc91 92\r
9fc78752 93**/\r
94VOID\r
95RuntimeDriverCalculateEfiHdrCrc (\r
96 IN OUT EFI_TABLE_HEADER *Hdr\r
97 )\r
f2abdc91 98{\r
99 UINT32 Crc;\r
100\r
1436aea4 101 Hdr->CRC32 = 0;\r
f2abdc91 102\r
1436aea4
MK
103 Crc = 0;\r
104 RuntimeDriverCalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);\r
f2abdc91 105 Hdr->CRC32 = Crc;\r
106}\r
107\r
9fc78752 108/**\r
f2abdc91 109\r
110 Determines the new virtual address that is to be used on subsequent memory accesses.\r
111\r
f2abdc91 112\r
9fc78752 113 @param DebugDisposition Supplies type information for the pointer being converted.\r
114 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed\r
115 for the new virtual address mappings being applied.\r
f2abdc91 116\r
9fc78752 117 @retval EFI_SUCCESS The pointer pointed to by Address was modified.\r
118 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part\r
119 of the current memory map. This is normally fatal.\r
120 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
f2abdc91 121\r
9fc78752 122**/\r
123EFI_STATUS\r
124EFIAPI\r
125RuntimeDriverConvertPointer (\r
126 IN UINTN DebugDisposition,\r
127 IN OUT VOID **ConvertAddress\r
128 )\r
f2abdc91 129{\r
1436aea4
MK
130 UINTN Address;\r
131 UINT64 VirtEndOfRange;\r
132 EFI_MEMORY_DESCRIPTOR *VirtEntry;\r
133 UINTN Index;\r
f2abdc91 134\r
135 //\r
136 // Make sure ConvertAddress is a valid pointer\r
137 //\r
138 if (ConvertAddress == NULL) {\r
139 return EFI_INVALID_PARAMETER;\r
140 }\r
1436aea4 141\r
f2abdc91 142 //\r
143 // Get the address to convert\r
144 //\r
1436aea4 145 Address = (UINTN)*ConvertAddress;\r
f2abdc91 146\r
147 //\r
148 // If this is a null pointer, return if it's allowed\r
149 //\r
150 if (Address == 0) {\r
57dba20b 151 if ((DebugDisposition & EFI_OPTIONAL_PTR) != 0) {\r
f2abdc91 152 return EFI_SUCCESS;\r
153 }\r
154\r
155 return EFI_INVALID_PARAMETER;\r
156 }\r
157\r
48557c65 158 VirtEntry = mVirtualMap;\r
f2abdc91 159 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {\r
160 //\r
48557c65 161 // To prevent the inclusion of 64-bit math functions a UINTN was placed in\r
f2abdc91 162 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32\r
163 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit\r
164 // multiply.\r
165 //\r
1436aea4 166 ASSERT (((UINTN)VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r
f2abdc91 167\r
168 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
169 if (Address >= VirtEntry->PhysicalStart) {\r
1436aea4 170 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN)VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);\r
f2abdc91 171 if (Address < VirtEndOfRange) {\r
172 //\r
173 // Compute new address\r
174 //\r
1436aea4 175 *ConvertAddress = (VOID *)(Address - (UINTN)VirtEntry->PhysicalStart + (UINTN)VirtEntry->VirtualStart);\r
f2abdc91 176 return EFI_SUCCESS;\r
177 }\r
178 }\r
179 }\r
180\r
43025b29 181 VirtEntry = NEXT_MEMORY_DESCRIPTOR (VirtEntry, mVirtualMapDescriptorSize);\r
f2abdc91 182 }\r
183\r
184 return EFI_NOT_FOUND;\r
185}\r
186\r
9fc78752 187/**\r
f2abdc91 188\r
189 Determines the new virtual address that is to be used on subsequent memory accesses\r
190 for internal pointers.\r
9fc78752 191 This is a internal function.\r
f2abdc91 192\r
f2abdc91 193\r
9fc78752 194 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed\r
195 for the new virtual address mappings being applied.\r
f2abdc91 196\r
9fc78752 197 @retval EFI_SUCCESS The pointer pointed to by Address was modified.\r
198 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part\r
199 of the current memory map. This is normally fatal.\r
200 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r
f2abdc91 201\r
9fc78752 202**/\r
203EFI_STATUS\r
204RuntimeDriverConvertInternalPointer (\r
1436aea4 205 IN OUT VOID **ConvertAddress\r
9fc78752 206 )\r
f2abdc91 207{\r
208 return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r
209}\r
210\r
9fc78752 211/**\r
212\r
213 Changes the runtime addressing mode of EFI firmware from physical to virtual.\r
214\r
215\r
216 @param MemoryMapSize The size in bytes of VirtualMap.\r
217 @param DescriptorSize The size in bytes of an entry in the VirtualMap.\r
218 @param DescriptorVersion The version of the structure entries in VirtualMap.\r
219 @param VirtualMap An array of memory descriptors which contain new virtual\r
220 address mapping information for all runtime ranges.\r
221\r
222 @retval EFI_SUCCESS The virtual address map has been applied.\r
223 @retval EFI_UNSUPPORTED EFI firmware is not at runtime, or the EFI firmware is already in\r
224 virtual address mapped mode.\r
225 @retval EFI_INVALID_PARAMETER DescriptorSize or DescriptorVersion is invalid.\r
226 @retval EFI_NO_MAPPING A virtual address was not supplied for a range in the memory\r
227 map that requires a mapping.\r
228 @retval EFI_NOT_FOUND A virtual address was supplied for an address that is not found\r
229 in the memory map.\r
230\r
231**/\r
f2abdc91 232EFI_STATUS\r
233EFIAPI\r
234RuntimeDriverSetVirtualAddressMap (\r
235 IN UINTN MemoryMapSize,\r
236 IN UINTN DescriptorSize,\r
237 IN UINT32 DescriptorVersion,\r
238 IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r
239 )\r
f2abdc91 240{\r
1436aea4
MK
241 EFI_STATUS Status;\r
242 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r
243 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r
244 LIST_ENTRY *Link;\r
245 EFI_PHYSICAL_ADDRESS VirtImageBase;\r
f2abdc91 246\r
247 //\r
248 // Can only switch to virtual addresses once the memory map is locked down,\r
249 // and can only set it once\r
250 //\r
251 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r
252 return EFI_UNSUPPORTED;\r
253 }\r
1436aea4 254\r
f2abdc91 255 //\r
256 // Only understand the original descriptor format\r
257 //\r
1436aea4 258 if ((DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION) || (DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR))) {\r
f2abdc91 259 return EFI_INVALID_PARAMETER;\r
260 }\r
1436aea4 261\r
f2abdc91 262 //\r
263 // We are now committed to go to virtual mode, so lets get to it!\r
264 //\r
265 mRuntime.VirtualMode = TRUE;\r
266\r
267 //\r
268 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r
269 // globals we need to parse the virtual address map.\r
270 //\r
271 mVirtualMapDescriptorSize = DescriptorSize;\r
272 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
273 mVirtualMap = VirtualMap;\r
274\r
275 //\r
9d3dae39 276 // ReporstStatusCodeLib will check and make sure this service can be called in runtime mode.\r
f2abdc91 277 //\r
f9876ecf 278 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_EFI_RUNTIME_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP));\r
f2abdc91 279\r
b0121d76
EL
280 //\r
281 // Report Status Code here since EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event will be signalled.\r
282 //\r
283 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_VIRTUAL_ADDRESS_CHANGE_EVENT));\r
284\r
f2abdc91 285 //\r
286 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
287 // All runtime events are stored in a list in Runtime AP.\r
288 //\r
289 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r
50d7ebad 290 RuntimeEvent = BASE_CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r
f2abdc91 291 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r
d342d318
LE
292 //\r
293 // Work around the bug in the Platform Init specification (v1.7),\r
294 // reported as Mantis#2017: "EFI_RUNTIME_EVENT_ENTRY.Event" should have\r
295 // type EFI_EVENT, not (EFI_EVENT*). The PI spec documents the field\r
296 // correctly as "The EFI_EVENT returned by CreateEvent()", but the type\r
297 // of the field doesn't match the natural language description. Therefore\r
298 // we need an explicit cast here.\r
299 //\r
f2abdc91 300 RuntimeEvent->NotifyFunction (\r
1436aea4 301 (EFI_EVENT)RuntimeEvent->Event,\r
f2abdc91 302 RuntimeEvent->NotifyContext\r
303 );\r
304 }\r
305 }\r
306\r
307 //\r
308 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r
309 //\r
310 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r
50d7ebad 311 RuntimeImage = BASE_CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r
f2abdc91 312 //\r
313 // We don't want to relocate our selves, as we only run in physical mode.\r
314 //\r
315 if (mMyImageBase != RuntimeImage->ImageBase) {\r
1436aea4
MK
316 VirtImageBase = (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase;\r
317 Status = RuntimeDriverConvertPointer (0, (VOID **)&VirtImageBase);\r
f2abdc91 318 ASSERT_EFI_ERROR (Status);\r
319\r
320 PeCoffLoaderRelocateImageForRuntime (\r
1436aea4 321 (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase,\r
f2abdc91 322 VirtImageBase,\r
1436aea4 323 (UINTN)RuntimeImage->ImageSize,\r
f2abdc91 324 RuntimeImage->RelocationData\r
325 );\r
326\r
1436aea4 327 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);\r
f2abdc91 328 }\r
329 }\r
330\r
331 //\r
332 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r
333 // and recompute the CRC-32\r
334 //\r
1436aea4
MK
335 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetTime);\r
336 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->SetTime);\r
337 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetWakeupTime);\r
338 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->SetWakeupTime);\r
339 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->ResetSystem);\r
340 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetNextHighMonotonicCount);\r
341 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetVariable);\r
342 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->SetVariable);\r
343 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetNextVariableName);\r
344 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->QueryVariableInfo);\r
345 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->UpdateCapsule);\r
346 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->QueryCapsuleCapabilities);\r
f2abdc91 347 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r
348\r
349 //\r
b010fb77 350 // UEFI don't require System Configuration Tables Conversion.\r
fb0b259e 351 //\r
f2abdc91 352\r
353 //\r
354 // Convert the runtime fields of the EFI System Table and recompute the CRC-32\r
355 //\r
1436aea4
MK
356 RuntimeDriverConvertInternalPointer ((VOID **)&gST->FirmwareVendor);\r
357 RuntimeDriverConvertInternalPointer ((VOID **)&gST->ConfigurationTable);\r
358 RuntimeDriverConvertInternalPointer ((VOID **)&gST->RuntimeServices);\r
f2abdc91 359 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);\r
360\r
361 //\r
362 // At this point, gRT and gST are physical pointers, but the contents of these tables\r
363 // have been converted to runtime.\r
364 //\r
365 //\r
366 // mVirtualMap is only valid during SetVirtualAddressMap() call\r
367 //\r
f78b937c
GH
368 mVirtualMap = NULL;\r
369 mVirtualMapMaxIndex = 0;\r
f2abdc91 370\r
371 return EFI_SUCCESS;\r
372}\r
373\r
9fc78752 374/**\r
48557c65 375 Entry Point for Runtime driver.\r
f2abdc91 376\r
48557c65 377 This function installs Runtime Architectural Protocol and registers CalculateCrc32 boot services table,\r
378 SetVirtualAddressMap & ConvertPointer runtime services table.\r
f2abdc91 379\r
9fc78752 380 @param ImageHandle Image handle of this driver.\r
48557c65 381 @param SystemTable a Pointer to the EFI System Table.\r
f2abdc91 382\r
48557c65 383 @retval EFI_SUCEESS Runtime Driver Architectural Protocol is successfully installed\r
384 @return Others Some error occurs when installing Runtime Driver Architectural Protocol.\r
f2abdc91 385\r
9fc78752 386**/\r
387EFI_STATUS\r
388EFIAPI\r
389RuntimeDriverInitialize (\r
1436aea4
MK
390 IN EFI_HANDLE ImageHandle,\r
391 IN EFI_SYSTEM_TABLE *SystemTable\r
9fc78752 392 )\r
f2abdc91 393{\r
1436aea4
MK
394 EFI_STATUS Status;\r
395 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r
f2abdc91 396\r
397 //\r
48557c65 398 // This image needs to be excluded from relocation for virtual mode, so cache\r
f2abdc91 399 // a copy of the Loaded Image protocol to test later.\r
400 //\r
401 Status = gBS->HandleProtocol (\r
402 ImageHandle,\r
403 &gEfiLoadedImageProtocolGuid,\r
1436aea4 404 (VOID **)&MyLoadedImage\r
f2abdc91 405 );\r
406 ASSERT_EFI_ERROR (Status);\r
407 mMyImageBase = MyLoadedImage->ImageBase;\r
408\r
f2abdc91 409 //\r
410 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r
411 //\r
412 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
413 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r
414 gRT->ConvertPointer = RuntimeDriverConvertPointer;\r
415\r
416 //\r
417 // Install the Runtime Architectural Protocol onto a new handle\r
418 //\r
419 Status = gBS->InstallMultipleProtocolInterfaces (\r
420 &mRuntimeHandle,\r
421 &gEfiRuntimeArchProtocolGuid,\r
422 &mRuntime,\r
423 NULL\r
424 );\r
425 ASSERT_EFI_ERROR (Status);\r
426\r
48557c65 427 return Status;\r
f2abdc91 428}\r