UefiCpuPkg/CpuDxe: Enable protection for newly added page table
[mirror_edk2.git] / UefiCpuPkg / SecCore / SecMain.c
1 /** @file
2 C functions in SEC
3
4 Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "SecMain.h"
16
17 EFI_PEI_TEMPORARY_RAM_DONE_PPI gSecTemporaryRamDonePpi = {
18 SecTemporaryRamDone
19 };
20
21 EFI_SEC_PLATFORM_INFORMATION_PPI mSecPlatformInformationPpi = { SecPlatformInformation };
22
23 EFI_PEI_PPI_DESCRIPTOR mPeiSecPlatformInformationPpi[] = {
24 {
25 //
26 // SecPerformance PPI notify descriptor.
27 //
28 EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK,
29 &gPeiSecPerformancePpiGuid,
30 (VOID *) (UINTN) SecPerformancePpiCallBack
31 },
32 {
33 EFI_PEI_PPI_DESCRIPTOR_PPI,
34 &gEfiTemporaryRamDonePpiGuid,
35 &gSecTemporaryRamDonePpi
36 },
37 {
38 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
39 &gEfiSecPlatformInformationPpiGuid,
40 &mSecPlatformInformationPpi
41 }
42 };
43
44 //
45 // These are IDT entries pointing to 10:FFFFFFE4h.
46 //
47 UINT64 mIdtEntryTemplate = 0xffff8e000010ffe4ULL;
48
49 /**
50 Caller provided function to be invoked at the end of InitializeDebugAgent().
51
52 Entry point to the C language phase of SEC. After the SEC assembly
53 code has initialized some temporary memory and set up the stack,
54 the control is transferred to this function.
55
56 @param[in] Context The first input parameter of InitializeDebugAgent().
57
58 **/
59 VOID
60 NORETURN
61 EFIAPI
62 SecStartupPhase2(
63 IN VOID *Context
64 );
65
66 /**
67 Entry point of the notification callback function itself within the PEIM.
68 It is to get SEC performance data and build HOB to convey the SEC performance
69 data to DXE phase.
70
71 @param PeiServices Indirect reference to the PEI Services Table.
72 @param NotifyDescriptor Address of the notification descriptor data structure.
73 @param Ppi Address of the PPI that was installed.
74
75 @return Status of the notification.
76 The status code returned from this function is ignored.
77 **/
78 EFI_STATUS
79 EFIAPI
80 SecPerformancePpiCallBack (
81 IN EFI_PEI_SERVICES **PeiServices,
82 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
83 IN VOID *Ppi
84 )
85 {
86 EFI_STATUS Status;
87 PEI_SEC_PERFORMANCE_PPI *SecPerf;
88 FIRMWARE_SEC_PERFORMANCE Performance;
89
90 SecPerf = (PEI_SEC_PERFORMANCE_PPI *) Ppi;
91 Status = SecPerf->GetPerformance ((CONST EFI_PEI_SERVICES **) PeiServices, SecPerf, &Performance);
92 if (!EFI_ERROR (Status)) {
93 BuildGuidDataHob (
94 &gEfiFirmwarePerformanceGuid,
95 &Performance,
96 sizeof (FIRMWARE_SEC_PERFORMANCE)
97 );
98 DEBUG ((DEBUG_INFO, "FPDT: SEC Performance Hob ResetEnd = %ld\n", Performance.ResetEnd));
99 }
100
101 return Status;
102 }
103
104 /**
105
106 Entry point to the C language phase of SEC. After the SEC assembly
107 code has initialized some temporary memory and set up the stack,
108 the control is transferred to this function.
109
110
111 @param SizeOfRam Size of the temporary memory available for use.
112 @param TempRamBase Base address of temporary ram
113 @param BootFirmwareVolume Base address of the Boot Firmware Volume.
114 **/
115 VOID
116 EFIAPI
117 SecStartup (
118 IN UINT32 SizeOfRam,
119 IN UINT32 TempRamBase,
120 IN VOID *BootFirmwareVolume
121 )
122 {
123 EFI_SEC_PEI_HAND_OFF SecCoreData;
124 IA32_DESCRIPTOR IdtDescriptor;
125 SEC_IDT_TABLE IdtTableInStack;
126 UINT32 Index;
127 UINT32 PeiStackSize;
128 EFI_STATUS Status;
129
130 //
131 // Report Status Code to indicate entering SEC core
132 //
133 REPORT_STATUS_CODE (
134 EFI_PROGRESS_CODE,
135 EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_ENTRY_POINT
136 );
137
138 PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);
139 if (PeiStackSize == 0) {
140 PeiStackSize = (SizeOfRam >> 1);
141 }
142
143 ASSERT (PeiStackSize < SizeOfRam);
144
145 //
146 // Process all libraries constructor function linked to SecCore.
147 //
148 ProcessLibraryConstructorList ();
149
150 //
151 // Initialize floating point operating environment
152 // to be compliant with UEFI spec.
153 //
154 InitializeFloatingPointUnits ();
155
156 // |-------------------|---->
157 // |IDT Table |
158 // |-------------------|
159 // |PeiService Pointer | PeiStackSize
160 // |-------------------|
161 // | |
162 // | Stack |
163 // |-------------------|---->
164 // | |
165 // | |
166 // | Heap | PeiTemporayRamSize
167 // | |
168 // | |
169 // |-------------------|----> TempRamBase
170
171 IdtTableInStack.PeiService = 0;
172 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {
173 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));
174 }
175
176 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;
177 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);
178
179 AsmWriteIdtr (&IdtDescriptor);
180
181 //
182 // Setup the default exception handlers
183 //
184 Status = InitializeCpuExceptionHandlers (NULL);
185 ASSERT_EFI_ERROR (Status);
186
187 //
188 // Update the base address and length of Pei temporary memory
189 //
190 SecCoreData.DataSize = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);
191 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;
192 SecCoreData.BootFirmwareVolumeSize = (UINTN)((EFI_FIRMWARE_VOLUME_HEADER *) BootFirmwareVolume)->FvLength;
193 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;
194 SecCoreData.TemporaryRamSize = SizeOfRam;
195 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;
196 SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;
197 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);
198 SecCoreData.StackSize = PeiStackSize;
199
200 //
201 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.
202 //
203 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);
204 }
205
206 /**
207 Caller provided function to be invoked at the end of InitializeDebugAgent().
208
209 Entry point to the C language phase of SEC. After the SEC assembly
210 code has initialized some temporary memory and set up the stack,
211 the control is transferred to this function.
212
213 @param[in] Context The first input parameter of InitializeDebugAgent().
214
215 **/
216 VOID
217 NORETURN
218 EFIAPI
219 SecStartupPhase2(
220 IN VOID *Context
221 )
222 {
223 EFI_SEC_PEI_HAND_OFF *SecCoreData;
224 EFI_PEI_PPI_DESCRIPTOR *PpiList;
225 UINT32 Index;
226 EFI_PEI_PPI_DESCRIPTOR *AllSecPpiList;
227 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;
228
229 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;
230 AllSecPpiList = (EFI_PEI_PPI_DESCRIPTOR *) SecCoreData->PeiTemporaryRamBase;
231 //
232 // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug
233 // is enabled.
234 //
235 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);
236 if (PeiCoreEntryPoint == NULL)
237 {
238 CpuDeadLoop ();
239 }
240
241 //
242 // Perform platform specific initialization before entering PeiCore.
243 //
244 PpiList = SecPlatformMain (SecCoreData);
245 if (PpiList != NULL) {
246 //
247 // Remove the terminal flag from the terminal PPI
248 //
249 CopyMem (AllSecPpiList, mPeiSecPlatformInformationPpi, sizeof (mPeiSecPlatformInformationPpi));
250 Index = sizeof (mPeiSecPlatformInformationPpi) / sizeof (EFI_PEI_PPI_DESCRIPTOR) - 1;
251 AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);
252
253 //
254 // Append the platform additional PPI list
255 //
256 Index += 1;
257 while (((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {
258 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));
259 Index++;
260 PpiList++;
261 }
262
263 //
264 // Add the terminal PPI
265 //
266 CopyMem (&AllSecPpiList[Index ++], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));
267
268 //
269 // Set PpiList to the total PPI
270 //
271 PpiList = AllSecPpiList;
272
273 //
274 // Adjust PEI TEMP RAM Range.
275 //
276 ASSERT (SecCoreData->PeiTemporaryRamSize > Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));
277 SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN) SecCoreData->PeiTemporaryRamBase + Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));
278 SecCoreData->PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize - Index * sizeof (EFI_PEI_PPI_DESCRIPTOR);
279 //
280 // Adjust the Base and Size to be 8-byte aligned as HOB which has 8byte aligned requirement
281 // will be built based on them in PEI phase.
282 //
283 SecCoreData->PeiTemporaryRamBase = (VOID *)(((UINTN)SecCoreData->PeiTemporaryRamBase + 7) & ~0x07);
284 SecCoreData->PeiTemporaryRamSize &= ~(UINTN)0x07;
285 } else {
286 //
287 // No addition PPI, PpiList directly point to the common PPI list.
288 //
289 PpiList = &mPeiSecPlatformInformationPpi[0];
290 }
291
292 DEBUG ((
293 DEBUG_INFO,
294 "%a() Stack Base: 0x%p, Stack Size: 0x%x\n",
295 __FUNCTION__,
296 SecCoreData->StackBase,
297 (UINT32) SecCoreData->StackSize
298 ));
299
300 //
301 // Report Status Code to indicate transferring to PEI core
302 //
303 REPORT_STATUS_CODE (
304 EFI_PROGRESS_CODE,
305 EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_HANDOFF_TO_NEXT
306 );
307
308 //
309 // Transfer the control to the PEI core
310 //
311 ASSERT (PeiCoreEntryPoint != NULL);
312 (*PeiCoreEntryPoint) (SecCoreData, PpiList);
313
314 //
315 // Should not come here.
316 //
317 UNREACHABLE ();
318 }
319
320 /**
321 TemporaryRamDone() disables the use of Temporary RAM. If present, this service is invoked
322 by the PEI Foundation after the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.
323
324 @retval EFI_SUCCESS Use of Temporary RAM was disabled.
325 @retval EFI_INVALID_PARAMETER Temporary RAM could not be disabled.
326
327 **/
328 EFI_STATUS
329 EFIAPI
330 SecTemporaryRamDone (
331 VOID
332 )
333 {
334 BOOLEAN State;
335
336 //
337 // Republish Sec Platform Information(2) PPI
338 //
339 RepublishSecPlatformInformationPpi ();
340
341 //
342 // Migrate DebugAgentContext.
343 //
344 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);
345
346 //
347 // Disable interrupts and save current interrupt state
348 //
349 State = SaveAndDisableInterrupts();
350
351 //
352 // Disable Temporary RAM after Stack and Heap have been migrated at this point.
353 //
354 SecPlatformDisableTemporaryMemory ();
355
356 //
357 // Restore original interrupt state
358 //
359 SetInterruptState (State);
360
361 return EFI_SUCCESS;
362 }