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