]> git.proxmox.com Git - mirror_edk2.git/blob - EdkModulePkg/Universal/Runtime/RuntimeDxe/Runtime.c
5a21d4961ef35fbf431af5623b036393b8b9b5d3
[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 // This is a only short term solution.
59 // There is a change coming to the Runtime AP that
60 // will make it so the Runtime driver will not have to allocate any buffers.
61 //
62 #define MAX_RUNTIME_IMAGE_NUM (64)
63 #define MAX_RUNTIME_EVENT_NUM (64)
64 RUNTIME_IMAGE_RELOCATION_DATA mRuntimeImageBuffer[MAX_RUNTIME_IMAGE_NUM];
65 RUNTIME_NOTIFY_EVENT_DATA mRuntimeEventBuffer[MAX_RUNTIME_EVENT_NUM];
66 UINTN mRuntimeImageNumber;
67 UINTN mRuntimeEventNumber;
68
69 //
70 // The handle onto which the Runtime Architectural Protocol instance is installed
71 //
72 EFI_HANDLE mRuntimeHandle = NULL;
73
74 //
75 // The Runtime Architectural Protocol instance produced by this driver
76 //
77 EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {
78 RuntimeDriverRegisterImage,
79 RuntimeDriverRegisterEvent
80 };
81
82 //
83 // Global Variables
84 //
85 LIST_ENTRY mRelocationList = INITIALIZE_LIST_HEAD_VARIABLE(mRelocationList);
86 LIST_ENTRY mEventList = INITIALIZE_LIST_HEAD_VARIABLE(mEventList);
87 BOOLEAN mEfiVirtualMode = FALSE;
88 EFI_GUID mLocalEfiUgaIoProtocolGuid = EFI_UGA_IO_PROTOCOL_GUID;
89 EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;
90 UINTN mVirtualMapDescriptorSize;
91 UINTN mVirtualMapMaxIndex;
92
93 EFI_LOADED_IMAGE_PROTOCOL *mMyLoadedImage;
94
95 //
96 // Worker Functions
97 //
98 VOID
99 RuntimeDriverCalculateEfiHdrCrc (
100 IN OUT EFI_TABLE_HEADER *Hdr
101 )
102 /*++
103
104 Routine Description:
105
106 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
107 internal function. The EFI Boot Services Table can not be used because
108 the EFI Boot Services Table was destroyed at ExitBootServices()
109
110 Arguments:
111
112 Hdr - Pointer to an EFI standard header
113
114 Returns:
115
116 None
117
118 --*/
119 {
120 UINT32 Crc;
121
122 Hdr->CRC32 = 0;
123
124 Crc = 0;
125 RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);
126 Hdr->CRC32 = Crc;
127 }
128
129 EFI_STATUS
130 EFIAPI
131 RuntimeDriverRegisterImage (
132 IN EFI_RUNTIME_ARCH_PROTOCOL *This,
133 IN EFI_PHYSICAL_ADDRESS ImageBase,
134 IN UINTN ImageSize,
135 IN VOID *RelocationData
136 )
137 /*++
138
139 Routine Description:
140
141 When a SetVirtualAddressMap() is performed all the runtime images loaded by
142 DXE must be fixed up with the new virtual address map. To facilitate this the
143 Runtime Architectural Protocol needs to be informed of every runtime driver
144 that is registered. All the runtime images loaded by DXE should be registered
145 with this service by the DXE Core when ExitBootServices() is called. The
146 images that are registered with this service must have successfully been
147 loaded into memory with the Boot Service LoadImage(). As a result, no
148 parameter checking needs to be performed.
149
150 Arguments:
151
152 This - The EFI_RUNTIME_ARCH_PROTOCOL instance.
153
154 ImageBase - Start of image that has been loaded in memory. It is either
155 a pointer to the DOS or PE header of the image.
156
157 ImageSize - Size of the image in bytes.
158
159 RelocationData - Information about the fixups that were performed on ImageBase
160 when it was loaded into memory. This information is needed
161 when the virtual mode fix-ups are reapplied so that data that
162 has been programmatically updated will not be fixed up. If
163 code updates a global variable the code is responsible for
164 fixing up the variable for virtual mode.
165
166 Returns:
167
168 EFI_SUCCESS - The ImageBase has been registered.
169
170 --*/
171 {
172 RUNTIME_IMAGE_RELOCATION_DATA *RuntimeImage;
173
174 if (mMyLoadedImage->ImageBase == (VOID *) (UINTN) ImageBase) {
175 //
176 // We don't want to relocate our selves, as we only run in physical mode.
177 //
178 return EFI_SUCCESS;
179 }
180
181 RuntimeImage = &mRuntimeImageBuffer[mRuntimeImageNumber];
182 mRuntimeImageNumber++;
183 ASSERT (mRuntimeImageNumber < MAX_RUNTIME_IMAGE_NUM);
184
185 RuntimeImage->Valid = TRUE;
186 RuntimeImage->ImageBase = ImageBase;
187 RuntimeImage->ImageSize = ImageSize;
188 RuntimeImage->RelocationData = RelocationData;
189
190 InsertTailList (&mRelocationList, &RuntimeImage->Link);
191 return EFI_SUCCESS;
192 }
193
194 EFI_STATUS
195 EFIAPI
196 RuntimeDriverRegisterEvent (
197 IN EFI_RUNTIME_ARCH_PROTOCOL *This,
198 IN UINT32 Type,
199 IN EFI_TPL NotifyTpl,
200 IN EFI_EVENT_NOTIFY NotifyFunction,
201 IN VOID *NotifyContext,
202 IN EFI_EVENT *Event
203 )
204 /*++
205
206 Routine Description:
207
208 This function is used to support the required runtime events. Currently only
209 runtime events of type EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE needs to be
210 registered with this service. All the runtime events that exist in the DXE
211 Core should be registered with this service when ExitBootServices() is called.
212 All the events that are registered with this service must have been created
213 with the Boot Service CreateEvent(). As a result, no parameter checking needs
214 to be performed.
215
216 Arguments:
217
218 This - The EFI_RUNTIME_ARCH_PROTOCOL instance.
219
220 Type - The same as Type passed into CreateEvent().
221
222 NotifyTpl - The same as NotifyTpl passed into CreateEvent().
223
224 NotifyFunction - The same as NotifyFunction passed into CreateEvent().
225
226 NotifyContext - The same as NotifyContext passed into CreateEvent().
227
228 Event - The EFI_EVENT returned by CreateEvent(). Event must be in
229 runtime memory.
230
231 Returns:
232
233 EFI_SUCCESS - The Event has been registered.
234
235 --*/
236 {
237 RUNTIME_NOTIFY_EVENT_DATA *RuntimeEvent;
238
239 RuntimeEvent = &mRuntimeEventBuffer[mRuntimeEventNumber];
240 mRuntimeEventNumber++;
241 ASSERT (mRuntimeEventNumber < MAX_RUNTIME_EVENT_NUM);
242
243 RuntimeEvent->Type = Type;
244 RuntimeEvent->NotifyTpl = NotifyTpl;
245 RuntimeEvent->NotifyFunction = NotifyFunction;
246 RuntimeEvent->NotifyContext = NotifyContext;
247 RuntimeEvent->Event = Event;
248
249 InsertTailList (&mEventList, &RuntimeEvent->Link);
250 return EFI_SUCCESS;
251 }
252
253 EFI_STATUS
254 EFIAPI
255 RuntimeDriverConvertPointer (
256 IN UINTN DebugDisposition,
257 IN OUT VOID **ConvertAddress
258 )
259 {
260 UINTN Address;
261 VOID *PlabelConvertAddress;
262 UINT64 VirtEndOfRange;
263 EFI_MEMORY_DESCRIPTOR *VirtEntry;
264 UINTN Index;
265
266 //
267 // Make sure ConvertAddress is a valid pointer
268 //
269 if (ConvertAddress == NULL) {
270 return EFI_INVALID_PARAMETER;
271 }
272 //
273 // Get the address to convert
274 //
275 Address = (UINTN) *ConvertAddress;
276
277 //
278 // If this is a null pointer, return if it's allowed
279 //
280 if (Address == 0) {
281 if (DebugDisposition & EFI_OPTIONAL_POINTER) {
282 return EFI_SUCCESS;
283 }
284
285 return EFI_INVALID_PARAMETER;
286 }
287
288 PlabelConvertAddress = NULL;
289 VirtEntry = mVirtualMap;
290 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
291 //
292 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
293 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
294 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
295 // multiply.
296 //
297 ASSERT ((VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));
298
299 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
300 if (Address >= VirtEntry->PhysicalStart) {
301 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);
302 if (Address < VirtEndOfRange) {
303 //
304 // Compute new address
305 //
306 *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);
307 return EFI_SUCCESS;
308 } else if (Address < (VirtEndOfRange + 0x200000)) {
309 //
310 // On Itanium GP defines a window +/- 2 MB inside an image.
311 // The compiler may asign a GP value outside of the image. Thus
312 // it could fall out side of any of our valid regions
313 //
314 PlabelConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);
315 }
316 }
317 }
318
319 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);
320 }
321
322 if (DebugDisposition & EFI_IPF_GP_POINTER) {
323 //
324 // If it's an IPF GP and the GP was outside the image handle that case.
325 //
326 if (PlabelConvertAddress != NULL) {
327 *ConvertAddress = PlabelConvertAddress;
328 return EFI_SUCCESS;
329 }
330 }
331
332 return EFI_NOT_FOUND;
333 }
334
335 EFI_STATUS
336 RuntimeDriverConvertInternalPointer (
337 IN OUT VOID **ConvertAddress
338 )
339 {
340 return RuntimeDriverConvertPointer (0x0, ConvertAddress);
341 }
342
343 EFI_STATUS
344 EFIAPI
345 RuntimeDriverSetVirtualAddressMap (
346 IN UINTN MemoryMapSize,
347 IN UINTN DescriptorSize,
348 IN UINT32 DescriptorVersion,
349 IN EFI_MEMORY_DESCRIPTOR *VirtualMap
350 )
351 {
352 RUNTIME_NOTIFY_EVENT_DATA *RuntimeEvent;
353 RUNTIME_IMAGE_RELOCATION_DATA *RuntimeImage;
354 LIST_ENTRY *Link;
355 UINTN Index;
356 UINTN Index1;
357 EFI_DRIVER_OS_HANDOFF_HEADER *DriverOsHandoffHeader;
358 EFI_DRIVER_OS_HANDOFF *DriverOsHandoff;
359
360 //
361 // Can only switch to virtual addresses once the memory map is locked down,
362 // and can only set it once
363 //
364 if (!EfiAtRuntime () || mEfiVirtualMode) {
365 return EFI_UNSUPPORTED;
366 }
367 //
368 // Only understand the original descriptor format
369 //
370 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {
371 return EFI_INVALID_PARAMETER;
372 }
373 //
374 // BugBug: Add code here to verify the memory map. We should
375 // cache a copy of the system memory map in the EFI System Table
376 // as a GUID pointer pair.
377 //
378 //
379 // Make sure all virtual translations are satisfied
380 //
381 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;
382
383 //
384 // BugBug :The following code does not work hence commented out.
385 // Need to be replaced by something that works.
386 //
387 // VirtEntry = VirtualMap;
388 // for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
389 // if (((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) &&
390 // (VirtEntry->VirtualStart != 0) ) {
391 // return EFI_NO_MAPPING;
392 // }
393 // VirtEntry = NextMemoryDescriptor(VirtEntry, DescriptorSize);
394 // }
395 //
396 // We are now committed to go to virtual mode, so lets get to it!
397 //
398 mEfiVirtualMode = TRUE;
399
400 //
401 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
402 // globals we need to parse the virtual address map.
403 //
404 mVirtualMapDescriptorSize = DescriptorSize;
405 mVirtualMap = VirtualMap;
406
407 //
408 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
409 // The core call RuntimeDriverRegisterEvent() for
410 // every runtime event and we stored them in the mEventList
411 //
412 //
413 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
414 // check whether in Runtime or not (this is judged by looking at
415 // mEfiAtRuntime global So this ReportStatusCode will work
416 //
417 REPORT_STATUS_CODE (
418 EFI_PROGRESS_CODE,
419 (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP)
420 );
421
422 //
423 // BugBug - Commented out for now because the status code driver is not
424 // ready for runtime yet. The Status Code driver calls data hub with does
425 // a bunch of Boot Service things (locks, AllocatePool) and hangs somewhere
426 // on the way.
427 //
428 // ReportStatusCode (
429 // EfiProgressCode, EfiMaxErrorSeverity,
430 // 0x03, 0x01, 12, // ReadyToBoot Progress code
431 // 0x00, 30, L"ConvertPointer"
432 // );
433 //
434 for (Link = mEventList.ForwardLink; Link != &mEventList; Link = Link->ForwardLink) {
435 RuntimeEvent = _CR (Link, RUNTIME_NOTIFY_EVENT_DATA, Link);
436 if ((RuntimeEvent->Type & EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EFI_EVENT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {
437 RuntimeEvent->NotifyFunction (
438 RuntimeEvent->Event,
439 RuntimeEvent->NotifyContext
440 );
441 }
442 }
443 //
444 // Relocate runtime images. Runtime images loaded before the runtime AP was
445 // started will not be relocated, since they would not have gotten registered.
446 // This includes the code in this file.
447 //
448 for (Link = mRelocationList.ForwardLink; Link != &mRelocationList; Link = Link->ForwardLink) {
449 RuntimeImage = _CR (Link, RUNTIME_IMAGE_RELOCATION_DATA, Link);
450 if (RuntimeImage->Valid) {
451 RelocatePeImageForRuntime (RuntimeImage);
452 }
453 }
454 //
455 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
456 // and recompute the CRC-32
457 //
458 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);
459 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);
460 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);
461 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);
462 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);
463 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);
464 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);
465 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);
466 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);
467 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);
468
469 //
470 // Convert the UGA OS Handoff Table if it is present in the Configuration Table
471 //
472 for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {
473 if (CompareGuid (&mLocalEfiUgaIoProtocolGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {
474 DriverOsHandoffHeader = gST->ConfigurationTable[Index].VendorTable;
475 for (Index1 = 0; Index1 < DriverOsHandoffHeader->NumberOfEntries; Index1++) {
476 DriverOsHandoff = (EFI_DRIVER_OS_HANDOFF *)
477 (
478 (UINTN) DriverOsHandoffHeader +
479 DriverOsHandoffHeader->HeaderSize +
480 Index1 *
481 DriverOsHandoffHeader->SizeOfEntries
482 );
483 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &DriverOsHandoff->DevicePath);
484 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &DriverOsHandoff->PciRomImage);
485 }
486
487 RuntimeDriverConvertPointer (EFI_OPTIONAL_POINTER, (VOID **) &(gST->ConfigurationTable[Index].VendorTable));
488 }
489 }
490 //
491 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
492 //
493 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);
494 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);
495 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);
496 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);
497
498 //
499 // At this point, gRT and gST are physical pointers, but the contents of these tables
500 // have been converted to runtime.
501 //
502 //
503 // mVirtualMap is only valid during SetVirtualAddressMap() call
504 //
505 mVirtualMap = NULL;
506
507 return EFI_SUCCESS;
508 }
509
510 EFI_STATUS
511 EFIAPI
512 RuntimeDriverInitialize (
513 IN EFI_HANDLE ImageHandle,
514 IN EFI_SYSTEM_TABLE *SystemTable
515 )
516 /*++
517
518 Routine Description:
519 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
520 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
521 ASSERT macros (they do ReportStatusCode).
522
523 Arguments:
524 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
525
526 Returns:
527
528 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
529
530 Other - Return value from gBS->InstallMultipleProtocolInterfaces
531
532 --*/
533 {
534 EFI_STATUS Status;
535
536 //
537 // This image needs to be exclued from relocation for virtual mode, so cache
538 // a copy of the Loaded Image protocol to test later.
539 //
540 Status = gBS->HandleProtocol (
541 ImageHandle,
542 &gEfiLoadedImageProtocolGuid,
543 (VOID **) &mMyLoadedImage
544 );
545 ASSERT_EFI_ERROR (Status);
546
547 //
548 // Initialize the table used to compute 32-bit CRCs
549 //
550 RuntimeDriverInitializeCrc32Table ();
551
552 //
553 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
554 //
555 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;
556 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;
557 gRT->ConvertPointer = RuntimeDriverConvertPointer;
558
559 //
560 // Install the Runtime Architectural Protocol onto a new handle
561 //
562 Status = gBS->InstallMultipleProtocolInterfaces (
563 &mRuntimeHandle,
564 &gEfiRuntimeArchProtocolGuid,
565 &mRuntime,
566 NULL
567 );
568 ASSERT_EFI_ERROR (Status);
569
570 mRuntimeImageNumber = 0;
571 mRuntimeEventNumber = 0;
572
573 return Status;
574 }