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