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