]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/RuntimeDxe/Runtime.c
Removed IntelframeworkPkg contamination from MdeModulePkg modules.
[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
ce2f5557 54#include "Runtime.h"\r
55\r
56//\r
57// Global Variables\r
58//\r
59EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;\r
60UINTN mVirtualMapDescriptorSize;\r
61UINTN mVirtualMapMaxIndex;\r
62VOID *mMyImageBase;\r
63\r
64//\r
65// The handle onto which the Runtime Architectural Protocol instance is installed\r
66//\r
67EFI_HANDLE mRuntimeHandle = NULL;\r
68\r
69//\r
70// The Runtime Architectural Protocol instance produced by this driver\r
71//\r
72EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {\r
73 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),\r
74 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),\r
75\r
76 //\r
77 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will\r
78 // prevent people from having pointer math bugs in their code.\r
79 // now you have to use *DescriptorSize to make things work.\r
80 //\r
81 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),\r
82 EFI_MEMORY_DESCRIPTOR_VERSION,\r
83 0,\r
84 NULL,\r
85 NULL,\r
86 FALSE,\r
87 FALSE\r
88};\r
89\r
90//\r
91// Worker Functions\r
92//\r
93STATIC\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 }\r
201 }\r
202 }\r
203\r
204 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);\r
205 }\r
206\r
207 return EFI_NOT_FOUND;\r
208}\r
209\r
210STATIC\r
211EFI_STATUS\r
212RuntimeDriverConvertInternalPointer (\r
213 IN OUT VOID **ConvertAddress\r
214 )\r
215/*++\r
216\r
217Routine Description:\r
218\r
219 Determines the new virtual address that is to be used on subsequent memory accesses\r
220 for internal pointers.\r
221\r
222Arguments:\r
223\r
224 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
225 for the new virtual address mappings being applied.\r
226\r
227Returns:\r
228\r
229 EFI_SUCCESS - The pointer pointed to by Address was modified.\r
230 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
231 of the current memory map. This is normally fatal.\r
232 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
233\r
234--*/\r
235{\r
236 return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r
237}\r
238\r
239EFI_STATUS\r
240EFIAPI\r
241RuntimeDriverSetVirtualAddressMap (\r
242 IN UINTN MemoryMapSize,\r
243 IN UINTN DescriptorSize,\r
244 IN UINT32 DescriptorVersion,\r
245 IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r
246 )\r
247/*++\r
248\r
249Routine Description:\r
250\r
251 Changes the runtime addressing mode of EFI firmware from physical to virtual.\r
252\r
253Arguments:\r
254\r
255 MemoryMapSize - The size in bytes of VirtualMap.\r
256 DescriptorSize - The size in bytes of an entry in the VirtualMap.\r
257 DescriptorVersion - The version of the structure entries in VirtualMap.\r
258 VirtualMap - An array of memory descriptors which contain new virtual\r
259 address mapping information for all runtime ranges.\r
260\r
261Returns:\r
262\r
263 EFI_SUCCESS - The virtual address map has been applied.\r
264 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in\r
265 virtual address mapped mode.\r
266 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.\r
267 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory\r
268 map that requires a mapping.\r
269 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found\r
270 in the memory map.\r
271\r
272--*/\r
273{\r
274 EFI_STATUS Status;\r
275 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r
276 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r
277 LIST_ENTRY *Link;\r
278 EFI_PHYSICAL_ADDRESS VirtImageBase;\r
279\r
280 //\r
281 // Can only switch to virtual addresses once the memory map is locked down,\r
282 // and can only set it once\r
283 //\r
284 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r
285 return EFI_UNSUPPORTED;\r
286 }\r
287 //\r
288 // Only understand the original descriptor format\r
289 //\r
290 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {\r
291 return EFI_INVALID_PARAMETER;\r
292 }\r
293 //\r
294 // We are now committed to go to virtual mode, so lets get to it!\r
295 //\r
296 mRuntime.VirtualMode = TRUE;\r
297\r
298 //\r
299 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r
300 // globals we need to parse the virtual address map.\r
301 //\r
302 mVirtualMapDescriptorSize = DescriptorSize;\r
303 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
304 mVirtualMap = VirtualMap;\r
305\r
306 //\r
307 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will\r
308 // check whether in Runtime or not (this is judged by looking at\r
309 // mEfiAtRuntime global So this ReportStatusCode will work\r
310 //\r
ba237732 311 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdStatusCodeValueSetVirtualAddressMap));\r
ce2f5557 312\r
313 //\r
314 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
315 // All runtime events are stored in a list in Runtime AP.\r
316 //\r
317 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r
318 RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r
319 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r
320 RuntimeEvent->NotifyFunction (\r
321 RuntimeEvent->Event,\r
322 RuntimeEvent->NotifyContext\r
323 );\r
324 }\r
325 }\r
326\r
327 //\r
328 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r
329 //\r
330 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r
331 RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r
332 //\r
333 // We don't want to relocate our selves, as we only run in physical mode.\r
334 //\r
335 if (mMyImageBase != RuntimeImage->ImageBase) {\r
336\r
337 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;\r
338 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);\r
339 ASSERT_EFI_ERROR (Status);\r
340\r
341 PeCoffLoaderRelocateImageForRuntime (\r
342 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,\r
343 VirtImageBase,\r
344 (UINTN) RuntimeImage->ImageSize,\r
345 RuntimeImage->RelocationData\r
346 );\r
347\r
348 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);\r
349 }\r
350 }\r
351\r
352 //\r
353 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r
354 // and recompute the CRC-32\r
355 //\r
356 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);\r
357 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);\r
358 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);\r
359 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);\r
360 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);\r
361 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);\r
362 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);\r
363 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);\r
364 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);\r
365 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);\r
366 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);\r
367 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);\r
368 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r
369\r
370 //\r
371 // BugBug: PI requires System Configuration Tables Conversion. \r
372 // Currently, we do not implement it.\r
373 // \r
374\r
375 //\r
376 // Convert the runtime fields of the EFI System Table and recompute the CRC-32\r
377 //\r
378 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);\r
379 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);\r
380 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);\r
381 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);\r
382\r
383 //\r
384 // At this point, gRT and gST are physical pointers, but the contents of these tables\r
385 // have been converted to runtime.\r
386 //\r
387 //\r
388 // mVirtualMap is only valid during SetVirtualAddressMap() call\r
389 //\r
390 mVirtualMap = NULL;\r
391\r
392 return EFI_SUCCESS;\r
393}\r
394\r
395EFI_STATUS\r
396EFIAPI\r
397RuntimeDriverInitialize (\r
398 IN EFI_HANDLE ImageHandle,\r
399 IN EFI_SYSTEM_TABLE *SystemTable\r
400 )\r
401/*++\r
402\r
403Routine Description:\r
404 Install Runtime AP. This code includes the EfiDriverLib, but it functions at\r
405 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and\r
406 ASSERT macros (they do ReportStatusCode).\r
407\r
408Arguments:\r
409 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
410\r
411Returns:\r
412\r
413 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed\r
414\r
415 Other - Return value from gBS->InstallMultipleProtocolInterfaces\r
416\r
417--*/\r
418{\r
419 EFI_STATUS Status;\r
420 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r
421\r
422 //\r
423 // This image needs to be exclued from relocation for virtual mode, so cache\r
424 // a copy of the Loaded Image protocol to test later.\r
425 //\r
426 Status = gBS->HandleProtocol (\r
427 ImageHandle,\r
428 &gEfiLoadedImageProtocolGuid,\r
429 (VOID**)&MyLoadedImage\r
430 );\r
431 ASSERT_EFI_ERROR (Status);\r
432 mMyImageBase = MyLoadedImage->ImageBase;\r
433\r
434 //\r
435 // Initialize the table used to compute 32-bit CRCs\r
436 //\r
437 RuntimeDriverInitializeCrc32Table ();\r
438\r
439 //\r
440 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r
441 //\r
442 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
443 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r
444 gRT->ConvertPointer = RuntimeDriverConvertPointer;\r
445\r
446 //\r
447 // Install the Runtime Architectural Protocol onto a new handle\r
448 //\r
449 Status = gBS->InstallMultipleProtocolInterfaces (\r
450 &mRuntimeHandle,\r
451 &gEfiRuntimeArchProtocolGuid,\r
452 &mRuntime,\r
453 NULL\r
454 );\r
455 ASSERT_EFI_ERROR (Status);\r
456\r
457 return EFI_SUCCESS;\r
458}\r