]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/RuntimeDxe/Runtime.c
Add BaseMemoryTestDxe in MdeModulePkg.dsc
[mirror_edk2.git] / MdeModulePkg / Universal / 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 // The package level header files this module uses
56 //
57 #include <PiDxe.h>
58 #include <FrameworkDxe.h>
59 //
60 // The protocols, PPI and GUID defintions for this module
61 //
62 #include <Protocol/LoadedImage.h>
63 #include <Protocol/Runtime.h>
64 #include <Protocol/UgaIo.h>
65
66 //
67 // The Library classes this module consumes
68 //
69 #include <Library/BaseLib.h>
70 #include <Library/UefiDriverEntryPoint.h>
71 #include <Library/DebugLib.h>
72 #include <Library/ReportStatusCodeLib.h>
73 #include <Library/BaseMemoryLib.h>
74 #include <Library/UefiRuntimeServicesTableLib.h>
75 #include <Library/UefiBootServicesTableLib.h>
76 #include <Library/CacheMaintenanceLib.h>
77 #include <Library/PeCoffLib.h>
78
79 #include "Runtime.h"
80
81 //
82 // Global Variables
83 //
84 EFI_MEMORY_DESCRIPTOR *mVirtualMap = NULL;
85 UINTN mVirtualMapDescriptorSize;
86 UINTN mVirtualMapMaxIndex;
87 VOID *mMyImageBase;
88
89 //
90 // The handle onto which the Runtime Architectural Protocol instance is installed
91 //
92 EFI_HANDLE mRuntimeHandle = NULL;
93
94 //
95 // The Runtime Architectural Protocol instance produced by this driver
96 //
97 EFI_RUNTIME_ARCH_PROTOCOL mRuntime = {
98 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.ImageHead),
99 INITIALIZE_LIST_HEAD_VARIABLE (mRuntime.EventHead),
100
101 //
102 // Make sure Size != sizeof (EFI_MEMORY_DESCRIPTOR). This will
103 // prevent people from having pointer math bugs in their code.
104 // now you have to use *DescriptorSize to make things work.
105 //
106 sizeof (EFI_MEMORY_DESCRIPTOR) + sizeof (UINT64) - (sizeof (EFI_MEMORY_DESCRIPTOR) % sizeof (UINT64)),
107 EFI_MEMORY_DESCRIPTOR_VERSION,
108 0,
109 NULL,
110 NULL,
111 FALSE,
112 FALSE
113 };
114
115 //
116 // Worker Functions
117 //
118 STATIC
119 VOID
120 RuntimeDriverCalculateEfiHdrCrc (
121 IN OUT EFI_TABLE_HEADER *Hdr
122 )
123 /*++
124
125 Routine Description:
126
127 Calcualte the 32-bit CRC in a EFI table using the Runtime Drivers
128 internal function. The EFI Boot Services Table can not be used because
129 the EFI Boot Services Table was destroyed at ExitBootServices()
130
131 Arguments:
132
133 Hdr - Pointer to an EFI standard header
134
135 Returns:
136
137 None
138
139 --*/
140 {
141 UINT32 Crc;
142
143 Hdr->CRC32 = 0;
144
145 Crc = 0;
146 RuntimeDriverCalculateCrc32 ((UINT8 *) Hdr, Hdr->HeaderSize, &Crc);
147 Hdr->CRC32 = Crc;
148 }
149
150 EFI_STATUS
151 EFIAPI
152 RuntimeDriverConvertPointer (
153 IN UINTN DebugDisposition,
154 IN OUT VOID **ConvertAddress
155 )
156 /*++
157
158 Routine Description:
159
160 Determines the new virtual address that is to be used on subsequent memory accesses.
161
162 Arguments:
163
164 DebugDisposition - Supplies type information for the pointer being converted.
165 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
166 for the new virtual address mappings being applied.
167
168 Returns:
169
170 EFI_SUCCESS - The pointer pointed to by Address was modified.
171 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
172 of the current memory map. This is normally fatal.
173 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
174
175 --*/
176 {
177 UINTN Address;
178 VOID *PlabelConvertAddress;
179 UINT64 VirtEndOfRange;
180 EFI_MEMORY_DESCRIPTOR *VirtEntry;
181 UINTN Index;
182
183 //
184 // Make sure ConvertAddress is a valid pointer
185 //
186 if (ConvertAddress == NULL) {
187 return EFI_INVALID_PARAMETER;
188 }
189 //
190 // Get the address to convert
191 //
192 Address = (UINTN) *ConvertAddress;
193
194 //
195 // If this is a null pointer, return if it's allowed
196 //
197 if (Address == 0) {
198 if (DebugDisposition & EFI_OPTIONAL_POINTER) {
199 return EFI_SUCCESS;
200 }
201
202 return EFI_INVALID_PARAMETER;
203 }
204
205 PlabelConvertAddress = NULL;
206 VirtEntry = mVirtualMap;
207 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
208 //
209 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
210 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
211 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
212 // multiply.
213 //
214 ASSERT (((UINTN) VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));
215
216 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
217 if (Address >= VirtEntry->PhysicalStart) {
218 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN) VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);
219 if (Address < VirtEndOfRange) {
220 //
221 // Compute new address
222 //
223 *ConvertAddress = (VOID *) (Address - (UINTN) VirtEntry->PhysicalStart + (UINTN) VirtEntry->VirtualStart);
224 return EFI_SUCCESS;
225 }
226 }
227 }
228
229 VirtEntry = NextMemoryDescriptor (VirtEntry, mVirtualMapDescriptorSize);
230 }
231
232 return EFI_NOT_FOUND;
233 }
234
235 STATIC
236 EFI_STATUS
237 RuntimeDriverConvertInternalPointer (
238 IN OUT VOID **ConvertAddress
239 )
240 /*++
241
242 Routine Description:
243
244 Determines the new virtual address that is to be used on subsequent memory accesses
245 for internal pointers.
246
247 Arguments:
248
249 ConvertAddress - A pointer to a pointer that is to be fixed to be the value needed
250 for the new virtual address mappings being applied.
251
252 Returns:
253
254 EFI_SUCCESS - The pointer pointed to by Address was modified.
255 EFI_NOT_FOUND - The pointer pointed to by Address was not found to be part
256 of the current memory map. This is normally fatal.
257 EFI_INVALID_PARAMETER - One of the parameters has an invalid value.
258
259 --*/
260 {
261 return RuntimeDriverConvertPointer (0x0, ConvertAddress);
262 }
263
264 EFI_STATUS
265 EFIAPI
266 RuntimeDriverSetVirtualAddressMap (
267 IN UINTN MemoryMapSize,
268 IN UINTN DescriptorSize,
269 IN UINT32 DescriptorVersion,
270 IN EFI_MEMORY_DESCRIPTOR *VirtualMap
271 )
272 /*++
273
274 Routine Description:
275
276 Changes the runtime addressing mode of EFI firmware from physical to virtual.
277
278 Arguments:
279
280 MemoryMapSize - The size in bytes of VirtualMap.
281 DescriptorSize - The size in bytes of an entry in the VirtualMap.
282 DescriptorVersion - The version of the structure entries in VirtualMap.
283 VirtualMap - An array of memory descriptors which contain new virtual
284 address mapping information for all runtime ranges.
285
286 Returns:
287
288 EFI_SUCCESS - The virtual address map has been applied.
289 EFI_UNSUPPORTED - EFI firmware is not at runtime, or the EFI firmware is already in
290 virtual address mapped mode.
291 EFI_INVALID_PARAMETER - DescriptorSize or DescriptorVersion is invalid.
292 EFI_NO_MAPPING - A virtual address was not supplied for a range in the memory
293 map that requires a mapping.
294 EFI_NOT_FOUND - A virtual address was supplied for an address that is not found
295 in the memory map.
296
297 --*/
298 {
299 EFI_STATUS Status;
300 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;
301 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;
302 LIST_ENTRY *Link;
303 EFI_PHYSICAL_ADDRESS VirtImageBase;
304
305 //
306 // Can only switch to virtual addresses once the memory map is locked down,
307 // and can only set it once
308 //
309 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {
310 return EFI_UNSUPPORTED;
311 }
312 //
313 // Only understand the original descriptor format
314 //
315 if (DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION || DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR)) {
316 return EFI_INVALID_PARAMETER;
317 }
318 //
319 // We are now committed to go to virtual mode, so lets get to it!
320 //
321 mRuntime.VirtualMode = TRUE;
322
323 //
324 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
325 // globals we need to parse the virtual address map.
326 //
327 mVirtualMapDescriptorSize = DescriptorSize;
328 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;
329 mVirtualMap = VirtualMap;
330
331 //
332 // Currently the bug in StatusCode/RuntimeLib has been fixed, it will
333 // check whether in Runtime or not (this is judged by looking at
334 // mEfiAtRuntime global So this ReportStatusCode will work
335 //
336 REPORT_STATUS_CODE (
337 EFI_PROGRESS_CODE,
338 (EFI_SOFTWARE_EFI_BOOT_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP)
339 );
340
341 //
342 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
343 // All runtime events are stored in a list in Runtime AP.
344 //
345 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {
346 RuntimeEvent = _CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);
347 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {
348 RuntimeEvent->NotifyFunction (
349 RuntimeEvent->Event,
350 RuntimeEvent->NotifyContext
351 );
352 }
353 }
354
355 //
356 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
357 //
358 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {
359 RuntimeImage = _CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);
360 //
361 // We don't want to relocate our selves, as we only run in physical mode.
362 //
363 if (mMyImageBase != RuntimeImage->ImageBase) {
364
365 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;
366 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);
367 ASSERT_EFI_ERROR (Status);
368
369 PeCoffLoaderRelocateImageForRuntime (
370 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,
371 VirtImageBase,
372 (UINTN) RuntimeImage->ImageSize,
373 RuntimeImage->RelocationData
374 );
375
376 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);
377 }
378 }
379
380 //
381 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
382 // and recompute the CRC-32
383 //
384 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);
385 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);
386 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);
387 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);
388 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);
389 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);
390 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);
391 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);
392 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);
393 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);
394 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);
395 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);
396 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);
397
398 //
399 // BugBug: PI requires System Configuration Tables Conversion.
400 // Currently, we do not implement it.
401 //
402
403 //
404 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
405 //
406 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);
407 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);
408 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);
409 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);
410
411 //
412 // At this point, gRT and gST are physical pointers, but the contents of these tables
413 // have been converted to runtime.
414 //
415 //
416 // mVirtualMap is only valid during SetVirtualAddressMap() call
417 //
418 mVirtualMap = NULL;
419
420 return EFI_SUCCESS;
421 }
422
423 EFI_STATUS
424 EFIAPI
425 RuntimeDriverInitialize (
426 IN EFI_HANDLE ImageHandle,
427 IN EFI_SYSTEM_TABLE *SystemTable
428 )
429 /*++
430
431 Routine Description:
432 Install Runtime AP. This code includes the EfiDriverLib, but it functions at
433 RT in physical mode. The only Lib services are gBS, gRT, and the DEBUG and
434 ASSERT macros (they do ReportStatusCode).
435
436 Arguments:
437 (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)
438
439 Returns:
440
441 EFI_SUCEESS - Runtime Driver Architectural Protocol Installed
442
443 Other - Return value from gBS->InstallMultipleProtocolInterfaces
444
445 --*/
446 {
447 EFI_STATUS Status;
448 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;
449
450 //
451 // This image needs to be exclued from relocation for virtual mode, so cache
452 // a copy of the Loaded Image protocol to test later.
453 //
454 Status = gBS->HandleProtocol (
455 ImageHandle,
456 &gEfiLoadedImageProtocolGuid,
457 (VOID**)&MyLoadedImage
458 );
459 ASSERT_EFI_ERROR (Status);
460 mMyImageBase = MyLoadedImage->ImageBase;
461
462 //
463 // Initialize the table used to compute 32-bit CRCs
464 //
465 RuntimeDriverInitializeCrc32Table ();
466
467 //
468 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
469 //
470 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;
471 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;
472 gRT->ConvertPointer = RuntimeDriverConvertPointer;
473
474 //
475 // Install the Runtime Architectural Protocol onto a new handle
476 //
477 Status = gBS->InstallMultipleProtocolInterfaces (
478 &mRuntimeHandle,
479 &gEfiRuntimeArchProtocolGuid,
480 &mRuntime,
481 NULL
482 );
483 ASSERT_EFI_ERROR (Status);
484
485 return EFI_SUCCESS;
486 }