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