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