remove some comments introduced by tools.
[mirror_edk2.git] / MdeModulePkg / Core / RuntimeDxe / Runtime.c
CommitLineData
f2abdc91 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#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 UINT64 VirtEndOfRange;\r
154 EFI_MEMORY_DESCRIPTOR *VirtEntry;\r
155 UINTN Index;\r
156\r
157 //\r
158 // Make sure ConvertAddress is a valid pointer\r
159 //\r
160 if (ConvertAddress == NULL) {\r
161 return EFI_INVALID_PARAMETER;\r
162 }\r
163 //\r
164 // Get the address to convert\r
165 //\r
166 Address = (UINTN) *ConvertAddress;\r
167\r
168 //\r
169 // If this is a null pointer, return if it's allowed\r
170 //\r
171 if (Address == 0) {\r
172 if (DebugDisposition & EFI_OPTIONAL_POINTER) {\r
173 return EFI_SUCCESS;\r
174 }\r
175\r
176 return EFI_INVALID_PARAMETER;\r
177 }\r
178\r
179 VirtEntry = mVirtualMap;\r
180 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {\r
181 //\r
182 // To prevent the inclusion of 64-bit math functions a UINTN was placed in\r
183 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32\r
184 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit\r
185 // multiply.\r
186 //\r
187 ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));\r
188\r
189 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {\r
190 if (Address >= VirtEntry->PhysicalStart) {\r
191 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);\r
192 if (Address < VirtEndOfRange) {\r
193 //\r
194 // Compute new address\r
195 //\r
196 *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);\r
197 return EFI_SUCCESS;\r
198 }\r
199 }\r
200 }\r
201\r
202 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);\r
203 }\r
204\r
205 return EFI_NOT_FOUND;\r
206}\r
207\r
208STATIC\r
209EFI_STATUS\r
210RuntimeDriverConvertInternalPointer (\r
211 IN OUT VOID **ConvertAddress\r
212 )\r
213/*++\r
214\r
215Routine Description:\r
216\r
217 Determines the new virtual address that is to be used on subsequent memory accesses\r
218 for internal pointers.\r
219\r
220Arguments:\r
221\r
222 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed\r
223 for the new virtual address mappings being applied.\r
224\r
225Returns:\r
226\r
227 EFI_SUCCESS - The pointer pointed to by Address was modified.\r
228 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part\r
229 of the current memory map. This is normally fatal.\r
230 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.\r
231\r
232--*/\r
233{\r
234 return RuntimeDriverConvertPointer (0x0, ConvertAddress);\r
235}\r
236\r
237EFI_STATUS\r
238EFIAPI\r
239RuntimeDriverSetVirtualAddressMap (\r
240 IN UINTN MemoryMapSize,\r
241 IN UINTN DescriptorSize,\r
242 IN UINT32 DescriptorVersion,\r
243 IN EFI_MEMORY_DESCRIPTOR *VirtualMap\r
244 )\r
245/*++\r
246\r
247Routine Description:\r
248\r
249 Changes the runtime addressing mode of EFI firmware from physical to virtual.\r
250\r
251Arguments:\r
252\r
253 MemoryMapSize - The size in bytes of VirtualMap.\r
254 DescriptorSize - The size in bytes of an entry in the VirtualMap.\r
255 DescriptorVersion - The version of the structure entries in VirtualMap.\r
256 VirtualMap - An array of memory descriptors which contain new virtual\r
257 address mapping information for all runtime ranges.\r
258\r
259Returns:\r
260\r
261 EFI_SUCCESS - The virtual address map has been applied.\r
262 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in\r
263 virtual address mapped mode.\r
264 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.\r
265 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory\r
266 map that requires a mapping.\r
267 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found\r
268 in the memory map.\r
269\r
270--*/\r
271{\r
272 EFI_STATUS Status;\r
273 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;\r
274 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;\r
275 LIST_ENTRY *Link;\r
276 EFI_PHYSICAL_ADDRESS VirtImageBase;\r
277\r
278 //\r
279 // Can only switch to virtual addresses once the memory map is locked down,\r
280 // and can only set it once\r
281 //\r
282 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {\r
283 return EFI_UNSUPPORTED;\r
284 }\r
285 //\r
286 // Only understand the original descriptor format\r
287 //\r
288 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {\r
289 return EFI_INVALID_PARAMETER;\r
290 }\r
291 //\r
292 // We are now committed to go to virtual mode, so lets get to it!\r
293 //\r
294 mRuntime.VirtualMode = TRUE;\r
295\r
296 //\r
297 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up\r
298 // globals we need to parse the virtual address map.\r
299 //\r
300 mVirtualMapDescriptorSize = DescriptorSize;\r
301 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;\r
302 mVirtualMap = VirtualMap;\r
303\r
304 //\r
305 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will\r
306 // check whether in Runtime or not (this is judged by looking at\r
307 // mEfiAtRuntime global So this ReportStatusCode will work\r
308 //\r
309 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, PcdGet32 (PcdStatusCodeValueSetVirtualAddressMap));\r
310\r
311 //\r
312 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.\r
313 // All runtime events are stored in a list in Runtime AP.\r
314 //\r
315 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {\r
316 RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);\r
317 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {\r
318 RuntimeEvent->NotifyFunction (\r
319 RuntimeEvent->Event,\r
320 RuntimeEvent->NotifyContext\r
321 );\r
322 }\r
323 }\r
324\r
325 //\r
326 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.\r
327 //\r
328 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {\r
329 RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);\r
330 //\r
331 // We don't want to relocate our selves, as we only run in physical mode.\r
332 //\r
333 if (mMyImageBase != RuntimeImage->ImageBase) {\r
334\r
335 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;\r
336 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);\r
337 ASSERT_EFI_ERROR (Status);\r
338\r
339 PeCoffLoaderRelocateImageForRuntime (\r
340 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,\r
341 VirtImageBase,\r
342 (UINTN) RuntimeImage->ImageSize,\r
343 RuntimeImage->RelocationData\r
344 );\r
345\r
346 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);\r
347 }\r
348 }\r
349\r
350 //\r
351 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()\r
352 // and recompute the CRC-32\r
353 //\r
354 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);\r
355 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);\r
356 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);\r
357 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);\r
358 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);\r
359 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);\r
360 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);\r
361 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);\r
362 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);\r
363 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);\r
364 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);\r
365 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);\r
366 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);\r
367\r
368 //\r
369 // BugBug: PI requires System Configuration Tables Conversion.\r
370 // Currently, we do not implement it.\r
371 //\r
372\r
373 //\r
374 // Convert the runtime fields of the EFI System Table and recompute the CRC-32\r
375 //\r
376 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);\r
377 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);\r
378 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);\r
379 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);\r
380\r
381 //\r
382 // At this point, gRT and gST are physical pointers, but the contents of these tables\r
383 // have been converted to runtime.\r
384 //\r
385 //\r
386 // mVirtualMap is only valid during SetVirtualAddressMap() call\r
387 //\r
388 mVirtualMap = NULL;\r
389\r
390 return EFI_SUCCESS;\r
391}\r
392\r
393EFI_STATUS\r
394EFIAPI\r
395RuntimeDriverInitialize (\r
396 IN EFI_HANDLE ImageHandle,\r
397 IN EFI_SYSTEM_TABLE *SystemTable\r
398 )\r
399/*++\r
400\r
401Routine Description:\r
402 Install Runtime AP. This code includes the EfiDriverLib, but it functions at\r
403 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and\r
404 ASSERT macros (they do ReportStatusCode).\r
405\r
406Arguments:\r
407 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
408\r
409Returns:\r
410\r
411 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed\r
412\r
413 Other - Return value from gBS->InstallMultipleProtocolInterfaces\r
414\r
415--*/\r
416{\r
417 EFI_STATUS Status;\r
418 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;\r
419\r
420 //\r
421 // This image needs to be exclued from relocation for virtual mode, so cache\r
422 // a copy of the Loaded Image protocol to test later.\r
423 //\r
424 Status = gBS->HandleProtocol (\r
425 ImageHandle,\r
426 &gEfiLoadedImageProtocolGuid,\r
427 (VOID**)&MyLoadedImage\r
428 );\r
429 ASSERT_EFI_ERROR (Status);\r
430 mMyImageBase = MyLoadedImage->ImageBase;\r
431\r
432 //\r
433 // Initialize the table used to compute 32-bit CRCs\r
434 //\r
435 RuntimeDriverInitializeCrc32Table ();\r
436\r
437 //\r
438 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables\r
439 //\r
440 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;\r
441 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;\r
442 gRT->ConvertPointer = RuntimeDriverConvertPointer;\r
443\r
444 //\r
445 // Install the Runtime Architectural Protocol onto a new handle\r
446 //\r
447 Status = gBS->InstallMultipleProtocolInterfaces (\r
448 &mRuntimeHandle,\r
449 &gEfiRuntimeArchProtocolGuid,\r
450 &mRuntime,\r
451 NULL\r
452 );\r
453 ASSERT_EFI_ERROR (Status);\r
454\r
455 return EFI_SUCCESS;\r
456}\r