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