]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/RuntimeDxe/Runtime.c
Remove package dependency of IntelFrameworkPkg
[mirror_edk2.git] / MdeModulePkg / Universal / RuntimeDxe / Runtime.c
CommitLineData
ce2f5557 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// The package level header files this module uses\r
56//\r
57#include <PiDxe.h>\r
58#include <FrameworkDxe.h>\r
59//\r
60// The protocols, PPI and GUID defintions for this module\r
61//\r
62#include <Protocol/LoadedImage.h>\r
63#include <Protocol/Runtime.h>\r
64#include <Protocol/UgaIo.h>\r
65\r
66//\r
67// The Library classes this module consumes\r
68//\r
69#include <Library/BaseLib.h>\r
70#include <Library/UefiDriverEntryPoint.h>\r
71#include <Library/DebugLib.h>\r
72#include <Library/ReportStatusCodeLib.h>\r
73#include <Library/BaseMemoryLib.h>\r
74#include <Library/UefiRuntimeServicesTableLib.h>\r
75#include <Library/UefiBootServicesTableLib.h>\r
76#include <Library/CacheMaintenanceLib.h>\r
77#include <Library/PeCoffLib.h>\r
78\r
79#include "Runtime.h"\r
80\r
81//\r
82// Global Variables\r
83//\r
84EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r
85UINTN mVirtualMapDescriptorSize;\r
86UINTN mVirtualMapMaxIndex;\r
87VOID *mMyImageBase;\r
88\r
89//\r
90// The handle onto which the Runtime Architectural Protocol instance is installed\r
91//\r
92EFI_HANDLE mRuntimeHandle = NULL;\r
93\r
94//\r
95// The Runtime Architectural Protocol instance produced by this driver\r
96//\r
97EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {\r
98 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),\r
99 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),\r
100\r
101 //\r
102 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will\r
103 // prevent people from having pointer math bugs in their code.\r
104 // now you have to use *DescriptorSize to make things work.\r
105 //\r
106 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),\r
107 EFI_MEMORY_DESCRIPTOR_VERSION,\r
108 0,\r
109 NULL,\r
110 NULL,\r
111 FALSE,\r
112 FALSE\r
113};\r
114\r
115//\r
116// Worker Functions\r
117//\r
118STATIC\r
119VOID\r
120RuntimeDriverCalculateEfiHdrCrc (\r
121 IN OUT EFI_TABLE_HEADER *Hdr\r
122 )\r
123/*++\r
124\r
125Routine Description:\r
126\r
127 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers\r
128 internal function. The EFI Boot Services Table can not be used because\r
129 the EFI Boot Services Table was destroyed at ExitBootServices()\r
130\r
131Arguments:\r
132\r
133 Hdr - Pointer to an EFI standard header\r
134\r
135Returns:\r
136\r
137 None\r
138\r
139--*/\r
140{\r
141 UINT32 Crc;\r
142\r
143 Hdr->CRC32 = 0;\r
144\r
145 Crc = 0;\r
146 RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);\r
147 Hdr->CRC32 = Crc;\r
148}\r
149\r
150EFI_STATUS\r
151EFIAPI\r
152RuntimeDriverConvertPointer (\r
153 IN UINTN DebugDisposition,\r
154 IN OUT VOID **ConvertAddress\r
155 )\r
156/*++\r
157\r
158Routine Description:\r
159\r
160 Determines the new virtual address that is to be used on subsequent memory accesses.\r
161\r
162Arguments:\r
163\r
164 DebugDisposition - Supplies type information for the pointer being converted.\r
165 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
166 for the new virtual address mappings being applied.\r
167\r
168Returns:\r
169\r
170 EFI_SUCCESS - The pointer pointed to by Address was modified.\r
171 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
172 of the current memory map. This is normally fatal.\r
173 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
174\r
175--*/\r
176{\r
177 UINTN Address;\r
178 VOID *PlabelConvertAddress;\r
179 UINT64 VirtEndOfRange;\r
180 EFI_MEMORY_DESCRIPTOR *VirtEntry;\r
181 UINTN Index;\r
182\r
183 //\r
184 // Make sure ConvertAddress is a valid pointer\r
185 //\r
186 if (ConvertAddress == NULL) {\r
187 return EFI_INVALID_PARAMETER;\r
188 }\r
189 //\r
190 // Get the address to convert\r
191 //\r
192 Address = (UINTN) *ConvertAddress;\r
193\r
194 //\r
195 // If this is a null pointer, return if it's allowed\r
196 //\r
197 if (Address == 0) {\r
198 if (DebugDisposition & EFI_OPTIONAL_POINTER) {\r
199 return EFI_SUCCESS;\r
200 }\r
201\r
202 return EFI_INVALID_PARAMETER;\r
203 }\r
204\r
205 PlabelConvertAddress = NULL;\r
206 VirtEntry = mVirtualMap;\r
207 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {\r
208 //\r
209 // To prevent the inclusion of 64-bit math functions a UINTN was placed in\r
210 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32\r
211 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit\r
212 // multiply.\r
213 //\r
214 ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r
215\r
216 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
217 if (Address >= VirtEntry->PhysicalStart) {\r
218 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);\r
219 if (Address < VirtEndOfRange) {\r
220 //\r
221 // Compute new address\r
222 //\r
223 *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);\r
224 return EFI_SUCCESS;\r
225 }\r
226 }\r
227 }\r
228\r
229 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);\r
230 }\r
231\r
232 return EFI_NOT_FOUND;\r
233}\r
234\r
235STATIC\r
236EFI_STATUS\r
237RuntimeDriverConvertInternalPointer (\r
238 IN OUT VOID **ConvertAddress\r
239 )\r
240/*++\r
241\r
242Routine Description:\r
243\r
244 Determines the new virtual address that is to be used on subsequent memory accesses\r
245 for internal pointers.\r
246\r
247Arguments:\r
248\r
249 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
250 for the new virtual address mappings being applied.\r
251\r
252Returns:\r
253\r
254 EFI_SUCCESS - The pointer pointed to by Address was modified.\r
255 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
256 of the current memory map. This is normally fatal.\r
257 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
258\r
259--*/\r
260{\r
261 return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r
262}\r
263\r
264EFI_STATUS\r
265EFIAPI\r
266RuntimeDriverSetVirtualAddressMap (\r
267 IN UINTN MemoryMapSize,\r
268 IN UINTN DescriptorSize,\r
269 IN UINT32 DescriptorVersion,\r
270 IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r
271 )\r
272/*++\r
273\r
274Routine Description:\r
275\r
276 Changes the runtime addressing mode of EFI firmware from physical to virtual.\r
277\r
278Arguments:\r
279\r
280 MemoryMapSize - The size in bytes of VirtualMap.\r
281 DescriptorSize - The size in bytes of an entry in the VirtualMap.\r
282 DescriptorVersion - The version of the structure entries in VirtualMap.\r
283 VirtualMap - An array of memory descriptors which contain new virtual\r
284 address mapping information for all runtime ranges.\r
285\r
286Returns:\r
287\r
288 EFI_SUCCESS - The virtual address map has been applied.\r
289 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in\r
290 virtual address mapped mode.\r
291 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.\r
292 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory\r
293 map that requires a mapping.\r
294 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found\r
295 in the memory map.\r
296\r
297--*/\r
298{\r
299 EFI_STATUS Status;\r
300 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r
301 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r
302 LIST_ENTRY *Link;\r
303 EFI_PHYSICAL_ADDRESS VirtImageBase;\r
304\r
305 //\r
306 // Can only switch to virtual addresses once the memory map is locked down,\r
307 // and can only set it once\r
308 //\r
309 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r
310 return EFI_UNSUPPORTED;\r
311 }\r
312 //\r
313 // Only understand the original descriptor format\r
314 //\r
315 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {\r
316 return EFI_INVALID_PARAMETER;\r
317 }\r
318 //\r
319 // We are now committed to go to virtual mode, so lets get to it!\r
320 //\r
321 mRuntime.VirtualMode = TRUE;\r
322\r
323 //\r
324 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r
325 // globals we need to parse the virtual address map.\r
326 //\r
327 mVirtualMapDescriptorSize = DescriptorSize;\r
328 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
329 mVirtualMap = VirtualMap;\r
330\r
331 //\r
332 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will\r
333 // check whether in Runtime or not (this is judged by looking at\r
334 // mEfiAtRuntime global So this ReportStatusCode will work\r
335 //\r
336 REPORT_STATUS_CODE (\r
337 EFI_PROGRESS_CODE,\r
338 (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP)\r
339 );\r
340\r
341 //\r
342 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
343 // All runtime events are stored in a list in Runtime AP.\r
344 //\r
345 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r
346 RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r
347 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r
348 RuntimeEvent->NotifyFunction (\r
349 RuntimeEvent->Event,\r
350 RuntimeEvent->NotifyContext\r
351 );\r
352 }\r
353 }\r
354\r
355 //\r
356 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r
357 //\r
358 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r
359 RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r
360 //\r
361 // We don't want to relocate our selves, as we only run in physical mode.\r
362 //\r
363 if (mMyImageBase != RuntimeImage->ImageBase) {\r
364\r
365 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;\r
366 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);\r
367 ASSERT_EFI_ERROR (Status);\r
368\r
369 PeCoffLoaderRelocateImageForRuntime (\r
370 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,\r
371 VirtImageBase,\r
372 (UINTN) RuntimeImage->ImageSize,\r
373 RuntimeImage->RelocationData\r
374 );\r
375\r
376 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);\r
377 }\r
378 }\r
379\r
380 //\r
381 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r
382 // and recompute the CRC-32\r
383 //\r
384 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);\r
385 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);\r
386 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);\r
387 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);\r
388 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);\r
389 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);\r
390 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);\r
391 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);\r
392 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);\r
393 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);\r
394 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);\r
395 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);\r
396 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r
397\r
398 //\r
399 // BugBug: PI requires System Configuration Tables Conversion. \r
400 // Currently, we do not implement it.\r
401 // \r
402\r
403 //\r
404 // Convert the runtime fields of the EFI System Table and recompute the CRC-32\r
405 //\r
406 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);\r
407 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);\r
408 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);\r
409 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);\r
410\r
411 //\r
412 // At this point, gRT and gST are physical pointers, but the contents of these tables\r
413 // have been converted to runtime.\r
414 //\r
415 //\r
416 // mVirtualMap is only valid during SetVirtualAddressMap() call\r
417 //\r
418 mVirtualMap = NULL;\r
419\r
420 return EFI_SUCCESS;\r
421}\r
422\r
423EFI_STATUS\r
424EFIAPI\r
425RuntimeDriverInitialize (\r
426 IN EFI_HANDLE ImageHandle,\r
427 IN EFI_SYSTEM_TABLE *SystemTable\r
428 )\r
429/*++\r
430\r
431Routine Description:\r
432 Install Runtime AP. This code includes the EfiDriverLib, but it functions at\r
433 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and\r
434 ASSERT macros (they do ReportStatusCode).\r
435\r
436Arguments:\r
437 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
438\r
439Returns:\r
440\r
441 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed\r
442\r
443 Other - Return value from gBS->InstallMultipleProtocolInterfaces\r
444\r
445--*/\r
446{\r
447 EFI_STATUS Status;\r
448 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r
449\r
450 //\r
451 // This image needs to be exclued from relocation for virtual mode, so cache\r
452 // a copy of the Loaded Image protocol to test later.\r
453 //\r
454 Status = gBS->HandleProtocol (\r
455 ImageHandle,\r
456 &gEfiLoadedImageProtocolGuid,\r
457 (VOID**)&MyLoadedImage\r
458 );\r
459 ASSERT_EFI_ERROR (Status);\r
460 mMyImageBase = MyLoadedImage->ImageBase;\r
461\r
462 //\r
463 // Initialize the table used to compute 32-bit CRCs\r
464 //\r
465 RuntimeDriverInitializeCrc32Table ();\r
466\r
467 //\r
468 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r
469 //\r
470 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
471 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r
472 gRT->ConvertPointer = RuntimeDriverConvertPointer;\r
473\r
474 //\r
475 // Install the Runtime Architectural Protocol onto a new handle\r
476 //\r
477 Status = gBS->InstallMultipleProtocolInterfaces (\r
478 &mRuntimeHandle,\r
479 &gEfiRuntimeArchProtocolGuid,\r
480 &mRuntime,\r
481 NULL\r
482 );\r
483 ASSERT_EFI_ERROR (Status);\r
484\r
485 return EFI_SUCCESS;\r
486}\r