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