]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/CapsuleRuntimeDxe/X64/SaveLongModeContext.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Universal / CapsuleRuntimeDxe / X64 / SaveLongModeContext.c
1 /** @file
2 Create the variable to save the base address of page table and stack
3 for transferring into long mode in IA32 capsule PEI.
4
5 Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Uefi.h>
11
12 #include <Protocol/Capsule.h>
13 #include <Protocol/DxeSmmReadyToLock.h>
14 #include <Protocol/VariableLock.h>
15
16 #include <Guid/CapsuleVendor.h>
17 #include <Guid/AcpiS3Context.h>
18
19 #include <Library/DebugLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiRuntimeServicesTableLib.h>
23 #include <Library/UefiRuntimeLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/UefiLib.h>
26 #include <Library/BaseMemoryLib.h>
27
28 //
29 // 8 extra pages for PF handler.
30 //
31 #define EXTRA_PAGE_TABLE_PAGES 8
32
33 /**
34 Allocate EfiReservedMemoryType below 4G memory address.
35
36 This function allocates EfiReservedMemoryType below 4G memory address.
37
38 @param Size Size of memory to allocate.
39
40 @return Allocated Address for output.
41
42 **/
43 VOID *
44 AllocateReservedMemoryBelow4G (
45 IN UINTN Size
46 )
47 {
48 UINTN Pages;
49 EFI_PHYSICAL_ADDRESS Address;
50 EFI_STATUS Status;
51 VOID *Buffer;
52
53 Pages = EFI_SIZE_TO_PAGES (Size);
54 Address = 0xffffffff;
55
56 Status = gBS->AllocatePages (
57 AllocateMaxAddress,
58 EfiReservedMemoryType,
59 Pages,
60 &Address
61 );
62 ASSERT_EFI_ERROR (Status);
63
64 Buffer = (VOID *)(UINTN)Address;
65 ZeroMem (Buffer, Size);
66
67 return Buffer;
68 }
69
70 /**
71 Register callback function upon VariableLockProtocol
72 to lock EFI_CAPSULE_LONG_MODE_BUFFER_NAME variable to avoid malicious code to update it.
73
74 @param[in] Event Event whose notification function is being invoked.
75 @param[in] Context Pointer to the notification function's context.
76 **/
77 VOID
78 EFIAPI
79 VariableLockCapsuleLongModeBufferVariable (
80 IN EFI_EVENT Event,
81 IN VOID *Context
82 )
83 {
84 EFI_STATUS Status;
85 EDKII_VARIABLE_LOCK_PROTOCOL *VariableLock;
86
87 //
88 // Mark EFI_CAPSULE_LONG_MODE_BUFFER_NAME variable to read-only if the Variable Lock protocol exists
89 //
90 Status = gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (VOID **)&VariableLock);
91 if (!EFI_ERROR (Status)) {
92 Status = VariableLock->RequestToLock (VariableLock, EFI_CAPSULE_LONG_MODE_BUFFER_NAME, &gEfiCapsuleVendorGuid);
93 ASSERT_EFI_ERROR (Status);
94 }
95 }
96
97 /**
98 1. Allocate Reserved memory for capsule PEIM to establish a 1:1 Virtual to Physical mapping.
99 2. Allocate Reserved memroy as a stack for capsule PEIM to transfer from 32-bit mdoe to 64-bit mode.
100
101 **/
102 VOID
103 EFIAPI
104 PrepareContextForCapsulePei (
105 VOID
106 )
107 {
108 UINTN ExtraPageTablePages;
109 UINT32 RegEax;
110 UINT32 RegEdx;
111 UINTN TotalPagesNum;
112 UINT8 PhysicalAddressBits;
113 UINT32 NumberOfPml4EntriesNeeded;
114 UINT32 NumberOfPdpEntriesNeeded;
115 BOOLEAN Page1GSupport;
116 EFI_CAPSULE_LONG_MODE_BUFFER LongModeBuffer;
117 EFI_STATUS Status;
118 VOID *Registration;
119
120 //
121 // Calculate the size of page table, allocate the memory.
122 //
123 Page1GSupport = FALSE;
124 if (PcdGetBool (PcdUse1GPageTable)) {
125 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
126 if (RegEax >= 0x80000001) {
127 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
128 if ((RegEdx & BIT26) != 0) {
129 Page1GSupport = TRUE;
130 }
131 }
132 }
133
134 //
135 // Create 4G page table by default,
136 // and let PF handler to handle > 4G request.
137 //
138 PhysicalAddressBits = 32;
139 ExtraPageTablePages = EXTRA_PAGE_TABLE_PAGES;
140
141 //
142 // Calculate the table entries needed.
143 //
144 if (PhysicalAddressBits <= 39 ) {
145 NumberOfPml4EntriesNeeded = 1;
146 NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));
147 } else {
148 NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 39));
149 NumberOfPdpEntriesNeeded = 512;
150 }
151
152 if (!Page1GSupport) {
153 TotalPagesNum = (NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1;
154 } else {
155 TotalPagesNum = NumberOfPml4EntriesNeeded + 1;
156 }
157
158 TotalPagesNum += ExtraPageTablePages;
159 DEBUG ((DEBUG_INFO, "CapsuleRuntimeDxe X64 TotalPagesNum - 0x%x pages\n", TotalPagesNum));
160
161 LongModeBuffer.PageTableAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateReservedMemoryBelow4G (EFI_PAGES_TO_SIZE (TotalPagesNum));
162 ASSERT (LongModeBuffer.PageTableAddress != 0);
163
164 //
165 // Allocate stack
166 //
167 LongModeBuffer.StackSize = PcdGet32 (PcdCapsulePeiLongModeStackSize);
168 LongModeBuffer.StackBaseAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateReservedMemoryBelow4G (PcdGet32 (PcdCapsulePeiLongModeStackSize));
169 ASSERT (LongModeBuffer.StackBaseAddress != 0);
170
171 Status = gRT->SetVariable (
172 EFI_CAPSULE_LONG_MODE_BUFFER_NAME,
173 &gEfiCapsuleVendorGuid,
174 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS,
175 sizeof (EFI_CAPSULE_LONG_MODE_BUFFER),
176 &LongModeBuffer
177 );
178 if (!EFI_ERROR (Status)) {
179 //
180 // Register callback function upon VariableLockProtocol
181 // to lock EFI_CAPSULE_LONG_MODE_BUFFER_NAME variable to avoid malicious code to update it.
182 //
183 EfiCreateProtocolNotifyEvent (
184 &gEdkiiVariableLockProtocolGuid,
185 TPL_CALLBACK,
186 VariableLockCapsuleLongModeBufferVariable,
187 NULL,
188 &Registration
189 );
190 } else {
191 DEBUG ((DEBUG_ERROR, "FATAL ERROR: CapsuleLongModeBuffer cannot be saved: %r. Capsule in PEI may fail!\n", Status));
192 gBS->FreePages (LongModeBuffer.StackBaseAddress, EFI_SIZE_TO_PAGES (LongModeBuffer.StackSize));
193 }
194 }
195
196 /**
197 Create the variable to save the base address of page table and stack
198 for transferring into long mode in IA32 capsule PEI.
199 **/
200 VOID
201 SaveLongModeContext (
202 VOID
203 )
204 {
205 if ((FeaturePcdGet (PcdSupportUpdateCapsuleReset)) && (FeaturePcdGet (PcdDxeIplSwitchToLongMode))) {
206 //
207 // Allocate memory for Capsule IA32 PEIM, it will create page table to transfer to long mode to access capsule above 4GB.
208 //
209 PrepareContextForCapsulePei ();
210 }
211 }