]> 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
85 Calculate the 32-bit CRC in a EFI table using the Runtime Drivers
86 internal function. The EFI Boot Services Table can not be used because
87 the EFI Boot Services Table was destroyed at ExitBootServices().
88 This is a internal function.
89
90
91 @param Hdr Pointer to an EFI standard header
92
93 **/
94 VOID
95 RuntimeDriverCalculateEfiHdrCrc (
96 IN OUT EFI_TABLE_HEADER *Hdr
97 )
98 {
99 UINT32 Crc;
100
101 Hdr->CRC32 = 0;
102
103 Crc = 0;
104 RuntimeDriverCalculateCrc32 ((UINT8 *)Hdr, Hdr->HeaderSize, &Crc);
105 Hdr->CRC32 = Crc;
106 }
107
108 /**
109
110 Determines the new virtual address that is to be used on subsequent memory accesses.
111
112
113 @param DebugDisposition Supplies type information for the pointer being converted.
114 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
115 for the new virtual address mappings being applied.
116
117 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
118 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
119 of the current memory map. This is normally fatal.
120 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
121
122 **/
123 EFI_STATUS
124 EFIAPI
125 RuntimeDriverConvertPointer (
126 IN UINTN DebugDisposition,
127 IN OUT VOID **ConvertAddress
128 )
129 {
130 UINTN Address;
131 UINT64 VirtEndOfRange;
132 EFI_MEMORY_DESCRIPTOR *VirtEntry;
133 UINTN Index;
134
135 //
136 // Make sure ConvertAddress is a valid pointer
137 //
138 if (ConvertAddress == NULL) {
139 return EFI_INVALID_PARAMETER;
140 }
141
142 //
143 // Get the address to convert
144 //
145 Address = (UINTN)*ConvertAddress;
146
147 //
148 // If this is a null pointer, return if it's allowed
149 //
150 if (Address == 0) {
151 if ((DebugDisposition & EFI_OPTIONAL_PTR) != 0) {
152 return EFI_SUCCESS;
153 }
154
155 return EFI_INVALID_PARAMETER;
156 }
157
158 VirtEntry = mVirtualMap;
159 for (Index = 0; Index < mVirtualMapMaxIndex; Index++) {
160 //
161 // To prevent the inclusion of 64-bit math functions a UINTN was placed in
162 // front of VirtEntry->NumberOfPages to cast it to a 32-bit thing on IA-32
163 // platforms. If you get this ASSERT remove the UINTN and do a 64-bit
164 // multiply.
165 //
166 ASSERT (((UINTN)VirtEntry->NumberOfPages < 0xffffffff) || (sizeof (UINTN) > 4));
167
168 if ((VirtEntry->Attribute & EFI_MEMORY_RUNTIME) == EFI_MEMORY_RUNTIME) {
169 if (Address >= VirtEntry->PhysicalStart) {
170 VirtEndOfRange = VirtEntry->PhysicalStart + (((UINTN)VirtEntry->NumberOfPages) * EFI_PAGE_SIZE);
171 if (Address < VirtEndOfRange) {
172 //
173 // Compute new address
174 //
175 *ConvertAddress = (VOID *)(Address - (UINTN)VirtEntry->PhysicalStart + (UINTN)VirtEntry->VirtualStart);
176 return EFI_SUCCESS;
177 }
178 }
179 }
180
181 VirtEntry = NEXT_MEMORY_DESCRIPTOR (VirtEntry, mVirtualMapDescriptorSize);
182 }
183
184 return EFI_NOT_FOUND;
185 }
186
187 /**
188
189 Determines the new virtual address that is to be used on subsequent memory accesses
190 for internal pointers.
191 This is a internal function.
192
193
194 @param ConvertAddress A pointer to a pointer that is to be fixed to be the value needed
195 for the new virtual address mappings being applied.
196
197 @retval EFI_SUCCESS The pointer pointed to by Address was modified.
198 @retval EFI_NOT_FOUND The pointer pointed to by Address was not found to be part
199 of the current memory map. This is normally fatal.
200 @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
201
202 **/
203 EFI_STATUS
204 RuntimeDriverConvertInternalPointer (
205 IN OUT VOID **ConvertAddress
206 )
207 {
208 return RuntimeDriverConvertPointer (0x0, ConvertAddress);
209 }
210
211 /**
212
213 Changes the runtime addressing mode of EFI firmware from physical to virtual.
214
215
216 @param MemoryMapSize The size in bytes of VirtualMap.
217 @param DescriptorSize The size in bytes of an entry in the VirtualMap.
218 @param DescriptorVersion The version of the structure entries in VirtualMap.
219 @param VirtualMap An array of memory descriptors which contain new virtual
220 address mapping information for all runtime ranges.
221
222 @retval EFI_SUCCESS The virtual address map has been applied.
223 @retval EFI_UNSUPPORTED EFI firmware is not at runtime, or the EFI firmware is already in
224 virtual address mapped mode.
225 @retval EFI_INVALID_PARAMETER DescriptorSize or DescriptorVersion is invalid.
226 @retval EFI_NO_MAPPING A virtual address was not supplied for a range in the memory
227 map that requires a mapping.
228 @retval EFI_NOT_FOUND A virtual address was supplied for an address that is not found
229 in the memory map.
230
231 **/
232 EFI_STATUS
233 EFIAPI
234 RuntimeDriverSetVirtualAddressMap (
235 IN UINTN MemoryMapSize,
236 IN UINTN DescriptorSize,
237 IN UINT32 DescriptorVersion,
238 IN EFI_MEMORY_DESCRIPTOR *VirtualMap
239 )
240 {
241 EFI_STATUS Status;
242 EFI_RUNTIME_EVENT_ENTRY *RuntimeEvent;
243 EFI_RUNTIME_IMAGE_ENTRY *RuntimeImage;
244 LIST_ENTRY *Link;
245 EFI_PHYSICAL_ADDRESS VirtImageBase;
246
247 //
248 // Can only switch to virtual addresses once the memory map is locked down,
249 // and can only set it once
250 //
251 if (!mRuntime.AtRuntime || mRuntime.VirtualMode) {
252 return EFI_UNSUPPORTED;
253 }
254
255 //
256 // Only understand the original descriptor format
257 //
258 if ((DescriptorVersion != EFI_MEMORY_DESCRIPTOR_VERSION) || (DescriptorSize < sizeof (EFI_MEMORY_DESCRIPTOR))) {
259 return EFI_INVALID_PARAMETER;
260 }
261
262 //
263 // We are now committed to go to virtual mode, so lets get to it!
264 //
265 mRuntime.VirtualMode = TRUE;
266
267 //
268 // ConvertPointer() needs this mVirtualMap to do the conversion. So set up
269 // globals we need to parse the virtual address map.
270 //
271 mVirtualMapDescriptorSize = DescriptorSize;
272 mVirtualMapMaxIndex = MemoryMapSize / DescriptorSize;
273 mVirtualMap = VirtualMap;
274
275 //
276 // ReporstStatusCodeLib will check and make sure this service can be called in runtime mode.
277 //
278 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_EFI_RUNTIME_SERVICE | EFI_SW_RS_PC_SET_VIRTUAL_ADDRESS_MAP));
279
280 //
281 // Report Status Code here since EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event will be signalled.
282 //
283 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, (EFI_SOFTWARE_DXE_BS_DRIVER | EFI_SW_DXE_BS_PC_VIRTUAL_ADDRESS_CHANGE_EVENT));
284
285 //
286 // Signal all the EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE events.
287 // All runtime events are stored in a list in Runtime AP.
288 //
289 for (Link = mRuntime.EventHead.ForwardLink; Link != &mRuntime.EventHead; Link = Link->ForwardLink) {
290 RuntimeEvent = BASE_CR (Link, EFI_RUNTIME_EVENT_ENTRY, Link);
291 if ((RuntimeEvent->Type & EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) == EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE) {
292 //
293 // Work around the bug in the Platform Init specification (v1.7),
294 // reported as Mantis#2017: "EFI_RUNTIME_EVENT_ENTRY.Event" should have
295 // type EFI_EVENT, not (EFI_EVENT*). The PI spec documents the field
296 // correctly as "The EFI_EVENT returned by CreateEvent()", but the type
297 // of the field doesn't match the natural language description. Therefore
298 // we need an explicit cast here.
299 //
300 RuntimeEvent->NotifyFunction (
301 (EFI_EVENT)RuntimeEvent->Event,
302 RuntimeEvent->NotifyContext
303 );
304 }
305 }
306
307 //
308 // Relocate runtime images. All runtime images are stored in a list in Runtime AP.
309 //
310 for (Link = mRuntime.ImageHead.ForwardLink; Link != &mRuntime.ImageHead; Link = Link->ForwardLink) {
311 RuntimeImage = BASE_CR (Link, EFI_RUNTIME_IMAGE_ENTRY, Link);
312 //
313 // We don't want to relocate our selves, as we only run in physical mode.
314 //
315 if (mMyImageBase != RuntimeImage->ImageBase) {
316 VirtImageBase = (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase;
317 Status = RuntimeDriverConvertPointer (0, (VOID **)&VirtImageBase);
318 ASSERT_EFI_ERROR (Status);
319
320 PeCoffLoaderRelocateImageForRuntime (
321 (EFI_PHYSICAL_ADDRESS)(UINTN)RuntimeImage->ImageBase,
322 VirtImageBase,
323 (UINTN)RuntimeImage->ImageSize,
324 RuntimeImage->RelocationData
325 );
326
327 InvalidateInstructionCacheRange (RuntimeImage->ImageBase, (UINTN)RuntimeImage->ImageSize);
328 }
329 }
330
331 //
332 // Convert all the Runtime Services except ConvertPointer() and SetVirtualAddressMap()
333 // and recompute the CRC-32
334 //
335 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetTime);
336 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->SetTime);
337 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetWakeupTime);
338 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->SetWakeupTime);
339 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->ResetSystem);
340 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetNextHighMonotonicCount);
341 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetVariable);
342 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->SetVariable);
343 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->GetNextVariableName);
344 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->QueryVariableInfo);
345 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->UpdateCapsule);
346 RuntimeDriverConvertInternalPointer ((VOID **)&gRT->QueryCapsuleCapabilities);
347 RuntimeDriverCalculateEfiHdrCrc (&gRT->Hdr);
348
349 //
350 // UEFI don't require System Configuration Tables Conversion.
351 //
352
353 //
354 // Convert the runtime fields of the EFI System Table and recompute the CRC-32
355 //
356 RuntimeDriverConvertInternalPointer ((VOID **)&gST->FirmwareVendor);
357 RuntimeDriverConvertInternalPointer ((VOID **)&gST->ConfigurationTable);
358 RuntimeDriverConvertInternalPointer ((VOID **)&gST->RuntimeServices);
359 RuntimeDriverCalculateEfiHdrCrc (&gST->Hdr);
360
361 //
362 // At this point, gRT and gST are physical pointers, but the contents of these tables
363 // have been converted to runtime.
364 //
365 //
366 // mVirtualMap is only valid during SetVirtualAddressMap() call
367 //
368 mVirtualMap = NULL;
369 mVirtualMapMaxIndex = 0;
370
371 return EFI_SUCCESS;
372 }
373
374 /**
375 Entry Point for Runtime driver.
376
377 This function installs Runtime Architectural Protocol and registers CalculateCrc32 boot services table,
378 SetVirtualAddressMap & ConvertPointer runtime services table.
379
380 @param ImageHandle Image handle of this driver.
381 @param SystemTable a Pointer to the EFI System Table.
382
383 @retval EFI_SUCEESS Runtime Driver Architectural Protocol is successfully installed
384 @return Others Some error occurs when installing Runtime Driver Architectural Protocol.
385
386 **/
387 EFI_STATUS
388 EFIAPI
389 RuntimeDriverInitialize (
390 IN EFI_HANDLE ImageHandle,
391 IN EFI_SYSTEM_TABLE *SystemTable
392 )
393 {
394 EFI_STATUS Status;
395 EFI_LOADED_IMAGE_PROTOCOL *MyLoadedImage;
396
397 //
398 // This image needs to be excluded from relocation for virtual mode, so cache
399 // a copy of the Loaded Image protocol to test later.
400 //
401 Status = gBS->HandleProtocol (
402 ImageHandle,
403 &gEfiLoadedImageProtocolGuid,
404 (VOID **)&MyLoadedImage
405 );
406 ASSERT_EFI_ERROR (Status);
407 mMyImageBase = MyLoadedImage->ImageBase;
408
409 //
410 // Fill in the entries of the EFI Boot Services and EFI Runtime Services Tables
411 //
412 gBS->CalculateCrc32 = RuntimeDriverCalculateCrc32;
413 gRT->SetVirtualAddressMap = RuntimeDriverSetVirtualAddressMap;
414 gRT->ConvertPointer = RuntimeDriverConvertPointer;
415
416 //
417 // Install the Runtime Architectural Protocol onto a new handle
418 //
419 Status = gBS->InstallMultipleProtocolInterfaces (
420 &mRuntimeHandle,
421 &gEfiRuntimeArchProtocolGuid,
422 &mRuntime,
423 NULL
424 );
425 ASSERT_EFI_ERROR (Status);
426
427 return Status;
428 }