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