]>
Commit | Line | Data |
---|---|---|
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 | |
31 | Revision 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 | 38 | Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 39 | SPDX-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 | |
48 | EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r | |
49 | UINTN mVirtualMapDescriptorSize;\r | |
50 | UINTN mVirtualMapMaxIndex;\r | |
51 | VOID *mMyImageBase;\r | |
52 | \r | |
53 | //\r | |
54 | // The handle onto which the Runtime Architectural Protocol instance is installed\r | |
55 | //\r | |
56 | EFI_HANDLE mRuntimeHandle = NULL;\r | |
57 | \r | |
58 | //\r | |
59 | // The Runtime Architectural Protocol instance produced by this driver\r | |
60 | //\r | |
61 | EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {\r | |
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 | |
9fc78752 | 82 | /**\r |
f2abdc91 | 83 | \r |
48557c65 | 84 | Calculate the 32-bit CRC in a EFI table using the Runtime Drivers\r |
f2abdc91 | 85 | internal function. The EFI Boot Services Table can not be used because\r |
9fc78752 | 86 | the EFI Boot Services Table was destroyed at ExitBootServices().\r |
87 | This is a internal function.\r | |
f2abdc91 | 88 | \r |
f2abdc91 | 89 | \r |
9fc78752 | 90 | @param Hdr Pointer to an EFI standard header\r |
f2abdc91 | 91 | \r |
9fc78752 | 92 | **/\r |
93 | VOID\r | |
94 | RuntimeDriverCalculateEfiHdrCrc (\r | |
95 | IN OUT EFI_TABLE_HEADER *Hdr\r | |
96 | )\r | |
f2abdc91 | 97 | {\r |
98 | UINT32 Crc;\r | |
99 | \r | |
100 | Hdr->CRC32 = 0;\r | |
101 | \r | |
102 | Crc = 0;\r | |
103 | RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);\r | |
104 | Hdr->CRC32 = Crc;\r | |
105 | }\r | |
106 | \r | |
9fc78752 | 107 | /**\r |
f2abdc91 | 108 | \r |
109 | Determines the new virtual address that is to be used on subsequent memory accesses.\r | |
110 | \r | |
f2abdc91 | 111 | \r |
9fc78752 | 112 | @param DebugDisposition Supplies type information for the pointer being converted.\r |
113 | @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed\r | |
114 | for the new virtual address mappings being applied.\r | |
f2abdc91 | 115 | \r |
9fc78752 | 116 | @retval EFI_SUCCESS The pointer pointed to by Address was modified.\r |
117 | @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part\r | |
118 | of the current memory map. This is normally fatal.\r | |
119 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r | |
f2abdc91 | 120 | \r |
9fc78752 | 121 | **/\r |
122 | EFI_STATUS\r | |
123 | EFIAPI\r | |
124 | RuntimeDriverConvertPointer (\r | |
125 | IN UINTN DebugDisposition,\r | |
126 | IN OUT VOID **ConvertAddress\r | |
127 | )\r | |
f2abdc91 | 128 | {\r |
129 | UINTN Address;\r | |
130 | UINT64 VirtEndOfRange;\r | |
131 | EFI_MEMORY_DESCRIPTOR *VirtEntry;\r | |
132 | UINTN Index;\r | |
133 | \r | |
134 | //\r | |
135 | // Make sure ConvertAddress is a valid pointer\r | |
136 | //\r | |
137 | if (ConvertAddress == NULL) {\r | |
138 | return EFI_INVALID_PARAMETER;\r | |
139 | }\r | |
140 | //\r | |
141 | // Get the address to convert\r | |
142 | //\r | |
143 | Address = (UINTN) *ConvertAddress;\r | |
144 | \r | |
145 | //\r | |
146 | // If this is a null pointer, return if it's allowed\r | |
147 | //\r | |
148 | if (Address == 0) {\r | |
57dba20b | 149 | if ((DebugDisposition & EFI_OPTIONAL_PTR) != 0) {\r |
f2abdc91 | 150 | return EFI_SUCCESS;\r |
151 | }\r | |
152 | \r | |
153 | return EFI_INVALID_PARAMETER;\r | |
154 | }\r | |
155 | \r | |
48557c65 | 156 | VirtEntry = mVirtualMap;\r |
f2abdc91 | 157 | for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {\r |
158 | //\r | |
48557c65 | 159 | // To prevent the inclusion of 64-bit math functions a UINTN was placed in\r |
f2abdc91 | 160 | // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32\r |
161 | // platforms. If you get this ASSERT remove the UINTN and do a 64-bit\r | |
162 | // multiply.\r | |
163 | //\r | |
164 | ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r | |
165 | \r | |
166 | if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r | |
167 | if (Address >= VirtEntry->PhysicalStart) {\r | |
168 | VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);\r | |
169 | if (Address < VirtEndOfRange) {\r | |
170 | //\r | |
171 | // Compute new address\r | |
172 | //\r | |
173 | *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);\r | |
174 | return EFI_SUCCESS;\r | |
175 | }\r | |
176 | }\r | |
177 | }\r | |
178 | \r | |
43025b29 | 179 | VirtEntry = NEXT_MEMORY_DESCRIPTOR (VirtEntry, mVirtualMapDescriptorSize);\r |
f2abdc91 | 180 | }\r |
181 | \r | |
182 | return EFI_NOT_FOUND;\r | |
183 | }\r | |
184 | \r | |
9fc78752 | 185 | /**\r |
f2abdc91 | 186 | \r |
187 | Determines the new virtual address that is to be used on subsequent memory accesses\r | |
188 | for internal pointers.\r | |
9fc78752 | 189 | This is a internal function.\r |
f2abdc91 | 190 | \r |
f2abdc91 | 191 | \r |
9fc78752 | 192 | @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed\r |
193 | for the new virtual address mappings being applied.\r | |
f2abdc91 | 194 | \r |
9fc78752 | 195 | @retval EFI_SUCCESS The pointer pointed to by Address was modified.\r |
196 | @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part\r | |
197 | of the current memory map. This is normally fatal.\r | |
198 | @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.\r | |
f2abdc91 | 199 | \r |
9fc78752 | 200 | **/\r |
201 | EFI_STATUS\r | |
202 | RuntimeDriverConvertInternalPointer (\r | |
203 | IN OUT VOID **ConvertAddress\r | |
204 | )\r | |
f2abdc91 | 205 | {\r |
206 | return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r | |
207 | }\r | |
208 | \r | |
9fc78752 | 209 | /**\r |
210 | \r | |
211 | Changes the runtime addressing mode of EFI firmware from physical to virtual.\r | |
212 | \r | |
213 | \r | |
214 | @param MemoryMapSize The size in bytes of VirtualMap.\r | |
215 | @param DescriptorSize The size in bytes of an entry in the VirtualMap.\r | |
216 | @param DescriptorVersion The version of the structure entries in VirtualMap.\r | |
217 | @param VirtualMap An array of memory descriptors which contain new virtual\r | |
218 | address mapping information for all runtime ranges.\r | |
219 | \r | |
220 | @retval EFI_SUCCESS The virtual address map has been applied.\r | |
221 | @retval EFI_UNSUPPORTED EFI firmware is not at runtime, or the EFI firmware is already in\r | |
222 | virtual address mapped mode.\r | |
223 | @retval EFI_INVALID_PARAMETER DescriptorSize or DescriptorVersion is invalid.\r | |
224 | @retval EFI_NO_MAPPING A virtual address was not supplied for a range in the memory\r | |
225 | map that requires a mapping.\r | |
226 | @retval EFI_NOT_FOUND A virtual address was supplied for an address that is not found\r | |
227 | in the memory map.\r | |
228 | \r | |
229 | **/\r | |
f2abdc91 | 230 | EFI_STATUS\r |
231 | EFIAPI\r | |
232 | RuntimeDriverSetVirtualAddressMap (\r | |
233 | IN UINTN MemoryMapSize,\r | |
234 | IN UINTN DescriptorSize,\r | |
235 | IN UINT32 DescriptorVersion,\r | |
236 | IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r | |
237 | )\r | |
f2abdc91 | 238 | {\r |
239 | EFI_STATUS Status;\r | |
240 | EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r | |
241 | EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r | |
242 | LIST_ENTRY *Link;\r | |
243 | EFI_PHYSICAL_ADDRESS VirtImageBase;\r | |
244 | \r | |
245 | //\r | |
246 | // Can only switch to virtual addresses once the memory map is locked down,\r | |
247 | // and can only set it once\r | |
248 | //\r | |
249 | if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r | |
250 | return EFI_UNSUPPORTED;\r | |
251 | }\r | |
252 | //\r | |
253 | // Only understand the original descriptor format\r | |
254 | //\r | |
255 | if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {\r | |
256 | return EFI_INVALID_PARAMETER;\r | |
257 | }\r | |
258 | //\r | |
259 | // We are now committed to go to virtual mode, so lets get to it!\r | |
260 | //\r | |
261 | mRuntime.VirtualMode = TRUE;\r | |
262 | \r | |
263 | //\r | |
264 | // ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r | |
265 | // globals we need to parse the virtual address map.\r | |
266 | //\r | |
267 | mVirtualMapDescriptorSize = DescriptorSize;\r | |
268 | mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r | |
269 | mVirtualMap = VirtualMap;\r | |
270 | \r | |
271 | //\r | |
9d3dae39 | 272 | // ReporstStatusCodeLib will check and make sure this service can be called in runtime mode.\r |
f2abdc91 | 273 | //\r |
f9876ecf | 274 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_EFI_RUNTIME_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP));\r |
f2abdc91 | 275 | \r |
b0121d76 EL |
276 | //\r |
277 | // Report Status Code here since EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event will be signalled.\r | |
278 | //\r | |
279 | REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_VIRTUAL_ADDRESS_CHANGE_EVENT));\r | |
280 | \r | |
f2abdc91 | 281 | //\r |
282 | // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r | |
283 | // All runtime events are stored in a list in Runtime AP.\r | |
284 | //\r | |
285 | for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r | |
50d7ebad | 286 | RuntimeEvent = BASE_CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r |
f2abdc91 | 287 | if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r |
288 | RuntimeEvent->NotifyFunction (\r | |
289 | RuntimeEvent->Event,\r | |
290 | RuntimeEvent->NotifyContext\r | |
291 | );\r | |
292 | }\r | |
293 | }\r | |
294 | \r | |
295 | //\r | |
296 | // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r | |
297 | //\r | |
298 | for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r | |
50d7ebad | 299 | RuntimeImage = BASE_CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r |
f2abdc91 | 300 | //\r |
301 | // We don't want to relocate our selves, as we only run in physical mode.\r | |
302 | //\r | |
303 | if (mMyImageBase != RuntimeImage->ImageBase) {\r | |
304 | \r | |
305 | VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;\r | |
306 | Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);\r | |
307 | ASSERT_EFI_ERROR (Status);\r | |
308 | \r | |
309 | PeCoffLoaderRelocateImageForRuntime (\r | |
310 | (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,\r | |
311 | VirtImageBase,\r | |
312 | (UINTN) RuntimeImage->ImageSize,\r | |
313 | RuntimeImage->RelocationData\r | |
314 | );\r | |
315 | \r | |
48557c65 | 316 | InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN) RuntimeImage->ImageSize);\r |
f2abdc91 | 317 | }\r |
318 | }\r | |
319 | \r | |
320 | //\r | |
321 | // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r | |
322 | // and recompute the CRC-32\r | |
323 | //\r | |
324 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);\r | |
325 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);\r | |
326 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);\r | |
327 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);\r | |
328 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);\r | |
329 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);\r | |
330 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);\r | |
331 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);\r | |
332 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);\r | |
333 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);\r | |
334 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);\r | |
335 | RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);\r | |
336 | RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r | |
337 | \r | |
338 | //\r | |
b010fb77 | 339 | // UEFI don't require System Configuration Tables Conversion.\r |
fb0b259e | 340 | //\r |
f2abdc91 | 341 | \r |
342 | //\r | |
343 | // Convert the runtime fields of the EFI System Table and recompute the CRC-32\r | |
344 | //\r | |
345 | RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);\r | |
346 | RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);\r | |
347 | RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);\r | |
348 | RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);\r | |
349 | \r | |
350 | //\r | |
351 | // At this point, gRT and gST are physical pointers, but the contents of these tables\r | |
352 | // have been converted to runtime.\r | |
353 | //\r | |
354 | //\r | |
355 | // mVirtualMap is only valid during SetVirtualAddressMap() call\r | |
356 | //\r | |
357 | mVirtualMap = NULL;\r | |
358 | \r | |
359 | return EFI_SUCCESS;\r | |
360 | }\r | |
361 | \r | |
9fc78752 | 362 | /**\r |
48557c65 | 363 | Entry Point for Runtime driver.\r |
f2abdc91 | 364 | \r |
48557c65 | 365 | This function installs Runtime Architectural Protocol and registers CalculateCrc32 boot services table,\r |
366 | SetVirtualAddressMap & ConvertPointer runtime services table.\r | |
f2abdc91 | 367 | \r |
9fc78752 | 368 | @param ImageHandle Image handle of this driver.\r |
48557c65 | 369 | @param SystemTable a Pointer to the EFI System Table.\r |
f2abdc91 | 370 | \r |
48557c65 | 371 | @retval EFI_SUCEESS Runtime Driver Architectural Protocol is successfully installed\r |
372 | @return Others Some error occurs when installing Runtime Driver Architectural Protocol.\r | |
f2abdc91 | 373 | \r |
9fc78752 | 374 | **/\r |
375 | EFI_STATUS\r | |
376 | EFIAPI\r | |
377 | RuntimeDriverInitialize (\r | |
378 | IN EFI_HANDLE ImageHandle,\r | |
379 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
380 | )\r | |
f2abdc91 | 381 | {\r |
382 | EFI_STATUS Status;\r | |
383 | EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r | |
384 | \r | |
385 | //\r | |
48557c65 | 386 | // This image needs to be excluded from relocation for virtual mode, so cache\r |
f2abdc91 | 387 | // a copy of the Loaded Image protocol to test later.\r |
388 | //\r | |
389 | Status = gBS->HandleProtocol (\r | |
390 | ImageHandle,\r | |
391 | &gEfiLoadedImageProtocolGuid,\r | |
392 | (VOID**)&MyLoadedImage\r | |
393 | );\r | |
394 | ASSERT_EFI_ERROR (Status);\r | |
395 | mMyImageBase = MyLoadedImage->ImageBase;\r | |
396 | \r | |
f2abdc91 | 397 | //\r |
398 | // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r | |
399 | //\r | |
400 | gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r | |
401 | gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r | |
402 | gRT->ConvertPointer = RuntimeDriverConvertPointer;\r | |
403 | \r | |
404 | //\r | |
405 | // Install the Runtime Architectural Protocol onto a new handle\r | |
406 | //\r | |
407 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
408 | &mRuntimeHandle,\r | |
409 | &gEfiRuntimeArchProtocolGuid,\r | |
410 | &mRuntime,\r | |
411 | NULL\r | |
412 | );\r | |
413 | ASSERT_EFI_ERROR (Status);\r | |
414 | \r | |
48557c65 | 415 | return Status;\r |
f2abdc91 | 416 | }\r |