]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.c
Fix iMac build issue.
[mirror_edk2.git] / EdkModulePkg / Universal / Runtime / RuntimeDxe / Runtime.c
... / ...
CommitLineData
1/*++\r
2\r
3Copyright (c) 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 Runtime.c\r
15\r
16Abstract:\r
17\r
18 Runtime Architectural Protocol as defined in the DXE CIS\r
19\r
20 This code is used to produce the EFI runtime virtual switch over\r
21\r
22 THIS IS VERY DANGEROUS CODE BE VERY CAREFUL IF YOU CHANGE IT\r
23\r
24 The transition for calling EFI Runtime functions in physical mode to calling\r
25 them in virtual mode is very very complex. Every pointer in needs to be \r
26 converted from physical mode to virtual mode. Be very careful walking linked\r
27 lists! Then to make it really hard the code it's self needs be relocated into\r
28 the new virtual address space.\r
29\r
30 So here is the concept. The code in this module will never ever be called in\r
31 virtual mode. This is the code that collects the information needed to convert\r
32 to virtual mode (DXE core registers runtime stuff with this code). Since this \r
33 code is used to fixup all runtime images, it CAN NOT fix it's self up. So some\r
34 code has to stay behind and that is us.\r
35\r
36 Also you need to be careful about when you allocate memory, as once we are in \r
37 runtime (including our EVT_SIGNAL_EXIT_BOOT_SERVICES event) you can no longer \r
38 allocate memory.\r
39\r
40 Any runtime driver that gets loaded before us will not be callable in virtual \r
41 mode. This is due to the fact that the DXE core can not register the info \r
42 needed with us. This is good, since it keeps the code in this file from \r
43 getting registered.\r
44\r
45\r
46Revision History:\r
47\r
48 - Move the CalculateCrc32 function from Runtime Arch Protocol to Boot Service.\r
49 Runtime Arch Protocol definition no longer contains CalculateCrc32. Boot Service\r
50 Table now contains an item named CalculateCrc32.\r
51\r
52--*/\r
53\r
54\r
55#include "Runtime.h"\r
56\r
57//\r
58// Global Variables\r
59//\r
60EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r
61UINTN mVirtualMapDescriptorSize;\r
62UINTN mVirtualMapMaxIndex;\r
63VOID *mMyImageBase;\r
64\r
65//\r
66// The handle onto which the Runtime Architectural Protocol instance is installed\r
67//\r
68EFI_HANDLE mRuntimeHandle = NULL;\r
69\r
70//\r
71// The Runtime Architectural Protocol instance produced by this driver\r
72//\r
73EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {\r
74 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),\r
75 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),\r
76\r
77 //\r
78 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will\r
79 // prevent people from having pointer math bugs in their code.\r
80 // now you have to use *DescriptorSize to make things work.\r
81 //\r
82 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)), \r
83 EFI_MEMORY_DESCRIPTOR_VERSION, \r
84 0,\r
85 NULL,\r
86 NULL,\r
87 FALSE,\r
88 FALSE\r
89};\r
90\r
91//\r
92// Worker Functions\r
93//\r
94VOID\r
95RuntimeDriverCalculateEfiHdrCrc (\r
96 IN OUT EFI_TABLE_HEADER *Hdr\r
97 )\r
98/*++\r
99\r
100Routine Description:\r
101\r
102 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers\r
103 internal function. The EFI Boot Services Table can not be used because\r
104 the EFI Boot Services Table was destroyed at ExitBootServices()\r
105\r
106Arguments:\r
107\r
108 Hdr - Pointer to an EFI standard header\r
109\r
110Returns:\r
111\r
112 None\r
113\r
114--*/\r
115{\r
116 UINT32 Crc;\r
117\r
118 Hdr->CRC32 = 0;\r
119\r
120 Crc = 0;\r
121 RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);\r
122 Hdr->CRC32 = Crc;\r
123}\r
124\r
125EFI_STATUS\r
126EFIAPI\r
127RuntimeDriverConvertPointer (\r
128 IN UINTN DebugDisposition,\r
129 IN OUT VOID **ConvertAddress\r
130 )\r
131/*++\r
132\r
133Routine Description:\r
134\r
135 Determines the new virtual address that is to be used on subsequent memory accesses.\r
136\r
137Arguments:\r
138 \r
139 DebugDisposition - Supplies type information for the pointer being converted.\r
140 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
141 for the new virtual address mappings being applied.\r
142\r
143Returns:\r
144\r
145 EFI_SUCCESS - The pointer pointed to by Address was modified.\r
146 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
147 of the current memory map. This is normally fatal.\r
148 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
149\r
150--*/\r
151{\r
152 UINTN Address;\r
153 VOID *PlabelConvertAddress;\r
154 UINT64 VirtEndOfRange;\r
155 EFI_MEMORY_DESCRIPTOR *VirtEntry;\r
156 UINTN Index;\r
157\r
158 //\r
159 // Make sure ConvertAddress is a valid pointer\r
160 //\r
161 if (ConvertAddress == NULL) {\r
162 return EFI_INVALID_PARAMETER;\r
163 }\r
164 //\r
165 // Get the address to convert\r
166 //\r
167 Address = (UINTN) *ConvertAddress;\r
168\r
169 //\r
170 // If this is a null pointer, return if it's allowed\r
171 //\r
172 if (Address == 0) {\r
173 if (DebugDisposition & EFI_OPTIONAL_POINTER) {\r
174 return EFI_SUCCESS;\r
175 }\r
176\r
177 return EFI_INVALID_PARAMETER;\r
178 }\r
179\r
180 PlabelConvertAddress = NULL;\r
181 VirtEntry = mVirtualMap;\r
182 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {\r
183 //\r
184 // To prevent the inclusion of 64-bit math functions a UINTN was placed in\r
185 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32\r
186 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit\r
187 // multiply.\r
188 //\r
189 ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r
190\r
191 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
192 if (Address >= VirtEntry->PhysicalStart) {\r
193 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);\r
194 if (Address < VirtEndOfRange) {\r
195 //\r
196 // Compute new address\r
197 //\r
198 *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);\r
199 return EFI_SUCCESS;\r
200 } else if (Address < (VirtEndOfRange + 0x200000)) {\r
201 //\r
202 // On Itanium GP defines a window +/- 2 MB inside an image.\r
203 // The compiler may asign a GP value outside of the image. Thus\r
204 // it could fall out side of any of our valid regions\r
205 //\r
206 PlabelConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);\r
207 }\r
208 }\r
209 }\r
210\r
211 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);\r
212 }\r
213\r
214 if (DebugDisposition & EFI_IPF_GP_POINTER) {\r
215 //\r
216 // If it's an IPF GP and the GP was outside the image handle that case.\r
217 //\r
218 if (PlabelConvertAddress != NULL) {\r
219 *ConvertAddress = PlabelConvertAddress;\r
220 return EFI_SUCCESS;\r
221 }\r
222 }\r
223\r
224 return EFI_NOT_FOUND;\r
225}\r
226\r
227EFI_STATUS\r
228RuntimeDriverConvertInternalPointer (\r
229 IN OUT VOID **ConvertAddress\r
230 )\r
231/*++\r
232\r
233Routine Description:\r
234\r
235 Determines the new virtual address that is to be used on subsequent memory accesses \r
236 for internal pointers.\r
237\r
238Arguments:\r
239 \r
240 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
241 for the new virtual address mappings being applied.\r
242\r
243Returns:\r
244\r
245 EFI_SUCCESS - The pointer pointed to by Address was modified.\r
246 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
247 of the current memory map. This is normally fatal.\r
248 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
249\r
250--*/\r
251{\r
252 return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r
253}\r
254\r
255EFI_STATUS\r
256EFIAPI\r
257RuntimeDriverSetVirtualAddressMap (\r
258 IN UINTN MemoryMapSize,\r
259 IN UINTN DescriptorSize,\r
260 IN UINT32 DescriptorVersion,\r
261 IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r
262 )\r
263/*++\r
264\r
265Routine Description:\r
266\r
267 Changes the runtime addressing mode of EFI firmware from physical to virtual.\r
268\r
269Arguments:\r
270 \r
271 MemoryMapSize - The size in bytes of VirtualMap.\r
272 DescriptorSize - The size in bytes of an entry in the VirtualMap.\r
273 DescriptorVersion - The version of the structure entries in VirtualMap.\r
274 VirtualMap - An array of memory descriptors which contain new virtual\r
275 address mapping information for all runtime ranges.\r
276\r
277Returns:\r
278\r
279 EFI_SUCCESS - The virtual address map has been applied.\r
280 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in\r
281 virtual address mapped mode.\r
282 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.\r
283 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory\r
284 map that requires a mapping.\r
285 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found\r
286 in the memory map.\r
287\r
288--*/ \r
289{\r
290 EFI_STATUS Status;\r
291 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r
292 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r
293 LIST_ENTRY *Link;\r
294 UINTN Index;\r
295 UINTN Index1;\r
296 EFI_DRIVER_OS_HANDOFF_HEADER *DriverOsHandoffHeader;\r
297 EFI_DRIVER_OS_HANDOFF *DriverOsHandoff;\r
298 EFI_PHYSICAL_ADDRESS VirtImageBase;\r
299#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
300 EFI_CAPSULE_TABLE *CapsuleTable; \r
301#endif\r
302\r
303 //\r
304 // Can only switch to virtual addresses once the memory map is locked down,\r
305 // and can only set it once\r
306 //\r
307 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r
308 return EFI_UNSUPPORTED;\r
309 }\r
310 //\r
311 // Only understand the original descriptor format\r
312 //\r
313 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {\r
314 return EFI_INVALID_PARAMETER;\r
315 }\r
316 //\r
317 // We are now committed to go to virtual mode, so lets get to it!\r
318 //\r
319 mRuntime.VirtualMode = TRUE;\r
320\r
321 //\r
322 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r
323 // globals we need to parse the virtual address map.\r
324 //\r
325 mVirtualMapDescriptorSize = DescriptorSize;\r
326 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
327 mVirtualMap = VirtualMap;\r
328\r
329 //\r
330 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will\r
331 // check whether in Runtime or not (this is judged by looking at\r
332 // mEfiAtRuntime global So this ReportStatusCode will work\r
333 //\r
334 REPORT_STATUS_CODE (\r
335 EFI_PROGRESS_CODE,\r
336 (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP)\r
337 );\r
338\r
339 //\r
340 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
341 // All runtime events are stored in a list in Runtime AP.\r
342 //\r
343 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r
344 RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r
345 if ((RuntimeEvent->Type & EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r
346 RuntimeEvent->NotifyFunction (\r
347 RuntimeEvent->Event,\r
348 RuntimeEvent->NotifyContext\r
349 );\r
350 }\r
351 }\r
352\r
353 //\r
354 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r
355 //\r
356 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r
357 RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r
358 //\r
359 // We don't want to relocate our selves, as we only run in physical mode.\r
360 //\r
361 if (mMyImageBase != RuntimeImage->ImageBase) {\r
362\r
363 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;\r
364 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);\r
365 ASSERT_EFI_ERROR (Status);\r
366\r
367 PeCoffLoaderRelocateImageForRuntime (\r
368 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,\r
369 VirtImageBase,\r
370 (UINTN) RuntimeImage->ImageSize,\r
371 RuntimeImage->RelocationData\r
372 );\r
373 \r
374 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);\r
375 }\r
376 }\r
377\r
378 //\r
379 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r
380 // and recompute the CRC-32\r
381 //\r
382 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);\r
383 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);\r
384 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);\r
385 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);\r
386 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);\r
387#if (EFI_SPECIFICATION_VERSION < 0x00020000)\r
388 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ReportStatusCode);\r
389#endif\r
390 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);\r
391 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);\r
392 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);\r
393 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);\r
394#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
395 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);\r
396 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);\r
397 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);\r
398#endif\r
399 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r
400\r
401 //\r
402 // Convert the UGA OS Handoff Table if it is present in the Configuration Table\r
403 //\r
404 for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {\r
405 if (CompareGuid (&gEfiUgaIoProtocolGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
406 DriverOsHandoffHeader = gST->ConfigurationTable[Index].VendorTable;\r
407 for (Index1 = 0; Index1 < DriverOsHandoffHeader->NumberOfEntries; Index1++) {\r
408 DriverOsHandoff = (EFI_DRIVER_OS_HANDOFF *)\r
409 (\r
410 (UINTN) DriverOsHandoffHeader +\r
411 DriverOsHandoffHeader->HeaderSize +\r
412 Index1 *\r
413 DriverOsHandoffHeader->SizeOfEntries\r
414 );\r
415 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &DriverOsHandoff->DevicePath);\r
416 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &DriverOsHandoff->PciRomImage);\r
417 }\r
418\r
419 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &(gST->ConfigurationTable[Index].VendorTable));\r
420 }\r
421\r
422#if (EFI_SPECIFICATION_VERSION >= 0x00020000)\r
423 if (CompareGuid (&gEfiCapsuleGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {\r
424 CapsuleTable = gST->ConfigurationTable[Index].VendorTable;\r
425 for (Index1 = 0; Index1 < CapsuleTable->CapsuleArrayNumber; Index1++) {\r
426 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &CapsuleTable->CapsulePtr[Index1]);\r
427 } \r
428 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &(gST->ConfigurationTable[Index].VendorTable));\r
429 }\r
430#endif\r
431 }\r
432 //\r
433 // Convert the runtime fields of the EFI System Table and recompute the CRC-32\r
434 //\r
435 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);\r
436 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);\r
437 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);\r
438 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);\r
439\r
440 //\r
441 // At this point, gRT and gST are physical pointers, but the contents of these tables\r
442 // have been converted to runtime.\r
443 //\r
444 //\r
445 // mVirtualMap is only valid during SetVirtualAddressMap() call\r
446 //\r
447 mVirtualMap = NULL;\r
448\r
449 return EFI_SUCCESS;\r
450}\r
451\r
452EFI_STATUS\r
453EFIAPI\r
454RuntimeDriverInitialize (\r
455 IN EFI_HANDLE ImageHandle,\r
456 IN EFI_SYSTEM_TABLE *SystemTable\r
457 )\r
458/*++\r
459\r
460Routine Description:\r
461 Install Runtime AP. This code includes the EfiDriverLib, but it functions at\r
462 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and\r
463 ASSERT macros (they do ReportStatusCode).\r
464\r
465Arguments:\r
466 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
467\r
468Returns:\r
469\r
470 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed\r
471\r
472 Other - Return value from gBS->InstallMultipleProtocolInterfaces\r
473\r
474--*/\r
475{\r
476 EFI_STATUS Status;\r
477 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r
478\r
479 //\r
480 // This image needs to be exclued from relocation for virtual mode, so cache\r
481 // a copy of the Loaded Image protocol to test later.\r
482 //\r
483 Status = gBS->HandleProtocol (\r
484 ImageHandle,\r
485 &gEfiLoadedImageProtocolGuid,\r
486 (VOID**)&MyLoadedImage\r
487 );\r
488 ASSERT_EFI_ERROR (Status);\r
489 mMyImageBase = MyLoadedImage->ImageBase;\r
490\r
491 //\r
492 // Initialize the table used to compute 32-bit CRCs\r
493 //\r
494 RuntimeDriverInitializeCrc32Table ();\r
495\r
496 //\r
497 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r
498 //\r
499 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
500 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r
501 gRT->ConvertPointer = RuntimeDriverConvertPointer;\r
502\r
503 //\r
504 // Install the Runtime Architectural Protocol onto a new handle\r
505 //\r
506 Status = gBS->InstallMultipleProtocolInterfaces (\r
507 &mRuntimeHandle,\r
508 &gEfiRuntimeArchProtocolGuid,\r
509 &mRuntime,\r
510 NULL\r
511 );\r
512 ASSERT_EFI_ERROR (Status);\r
513 \r
514 return EFI_SUCCESS;\r
515}\r