]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/RuntimeDxe/Runtime.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 //
289 // Work around the bug in the Platform Init specification (v1.7),
290 // reported as Mantis#2017: "EFI_RUNTIME_EVENT_ENTRY.Event" should have
291 // type EFI_EVENT, not (EFI_EVENT*). The PI spec documents the field
292 // correctly as "The EFI_EVENT returned by CreateEvent()", but the type
293 // of the field doesn't match the natural language description. Therefore
294 // we need an explicit cast here.
295 //
296 RuntimeEvent->NotifyFunction (
297 (EFI_EVENT) RuntimeEvent->Event,
298 RuntimeEvent->NotifyContext
299 );
300 }
301 }
302
303 //
304 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
305 //
306 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {
307 RuntimeImage = BASE_CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);
308 //
309 // We don't want to relocate our selves, as we only run in physical mode.
310 //
311 if (mMyImageBase != RuntimeImage->ImageBase) {
312
313 VirtImageBase = (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase;
314 Status = RuntimeDriverConvertPointer (0, (VOID **) &VirtImageBase);
315 ASSERT_EFI_ERROR (Status);
316
317 PeCoffLoaderRelocateImageForRuntime (
318 (EFI_PHYSICAL_ADDRESS) (UINTN) RuntimeImage->ImageBase,
319 VirtImageBase,
320 (UINTN) RuntimeImage->ImageSize,
321 RuntimeImage->RelocationData
322 );
323
324 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN) RuntimeImage->ImageSize);
325 }
326 }
327
328 //
329 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
330 // and recompute the CRC-32
331 //
332 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetTime);
333 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetTime);
334 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetWakeupTime);
335 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetWakeupTime);
336 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->ResetSystem);
337 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextHighMonotonicCount);
338 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetVariable);
339 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->SetVariable);
340 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->GetNextVariableName);
341 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryVariableInfo);
342 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->UpdateCapsule);
343 RuntimeDriverConvertInternalPointer ((VOID **) &gRT->QueryCapsuleCapabilities);
344 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);
345
346 //
347 // UEFI don't require System Configuration Tables Conversion.
348 //
349
350 //
351 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
352 //
353 RuntimeDriverConvertInternalPointer ((VOID **) &gST->FirmwareVendor);
354 RuntimeDriverConvertInternalPointer ((VOID **) &gST->ConfigurationTable);
355 RuntimeDriverConvertInternalPointer ((VOID **) &gST->RuntimeServices);
356 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);
357
358 //
359 // At this point, gRT and gST are physical pointers, but the contents of these tables
360 // have been converted to runtime.
361 //
362 //
363 // mVirtualMap is only valid during SetVirtualAddressMap() call
364 //
365 mVirtualMap = NULL;
366
367 return EFI_SUCCESS;
368 }
369
370 /**
371 Entry Point for Runtime driver.
372
373 This function installs Runtime Architectural Protocol and registers CalculateCrc32 boot services table,
374 SetVirtualAddressMap & ConvertPointer runtime services table.
375
376 @param ImageHandle Image handle of this driver.
377 @param SystemTable a Pointer to the EFI System Table.
378
379 @retval EFI_SUCEESS Runtime Driver Architectural Protocol is successfully installed
380 @return Others Some error occurs when installing Runtime Driver Architectural Protocol.
381
382 **/
383 EFI_STATUS
384 EFIAPI
385 RuntimeDriverInitialize (
386 IN EFI_HANDLE ImageHandle,
387 IN EFI_SYSTEM_TABLE *SystemTable
388 )
389 {
390 EFI_STATUS Status;
391 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;
392
393 //
394 // This image needs to be excluded from relocation for virtual mode, so cache
395 // a copy of the Loaded Image protocol to test later.
396 //
397 Status = gBS->HandleProtocol (
398 ImageHandle,
399 &gEfiLoadedImageProtocolGuid,
400 (VOID**)&MyLoadedImage
401 );
402 ASSERT_EFI_ERROR (Status);
403 mMyImageBase = MyLoadedImage->ImageBase;
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 Status;
424 }