]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/SecCore/SecMain.c
MdeModulePkg/Core: Decorate phase-transition functions with NORETURN.
[mirror_edk2.git] / UefiCpuPkg / SecCore / SecMain.c
CommitLineData
1921695e
MK
1/** @file\r
2 C functions in SEC\r
3\r
4 Copyright (c) 2008 - 2015, 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
52EFIAPI\r
53SecStartupPhase2(\r
54 IN VOID *Context\r
55 );\r
56\r
57/**\r
58\r
59 Entry point to the C language phase of SEC. After the SEC assembly\r
60 code has initialized some temporary memory and set up the stack,\r
61 the control is transferred to this function.\r
62\r
63\r
64 @param SizeOfRam Size of the temporary memory available for use.\r
65 @param TempRamBase Base address of temporary ram\r
66 @param BootFirmwareVolume Base address of the Boot Firmware Volume.\r
67**/\r
68VOID\r
69EFIAPI\r
70SecStartup (\r
71 IN UINT32 SizeOfRam,\r
72 IN UINT32 TempRamBase,\r
73 IN VOID *BootFirmwareVolume\r
74 )\r
75{\r
76 EFI_SEC_PEI_HAND_OFF SecCoreData;\r
77 IA32_DESCRIPTOR IdtDescriptor;\r
78 SEC_IDT_TABLE IdtTableInStack;\r
79 UINT32 Index;\r
80 UINT32 PeiStackSize;\r
81 EFI_STATUS Status;\r
82\r
83 //\r
84 // Report Status Code to indicate entering SEC core\r
85 //\r
86 REPORT_STATUS_CODE (\r
87 EFI_PROGRESS_CODE,\r
88 EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_ENTRY_POINT\r
89 );\r
90\r
91 PeiStackSize = PcdGet32 (PcdPeiTemporaryRamStackSize);\r
92 if (PeiStackSize == 0) {\r
93 PeiStackSize = (SizeOfRam >> 1);\r
94 }\r
95\r
96 ASSERT (PeiStackSize < SizeOfRam);\r
97\r
98 //\r
99 // Process all libraries constructor function linked to SecCore.\r
100 //\r
101 ProcessLibraryConstructorList ();\r
102\r
103 //\r
104 // Initialize floating point operating environment\r
105 // to be compliant with UEFI spec.\r
106 //\r
107 InitializeFloatingPointUnits ();\r
108\r
109 // |-------------------|---->\r
110 // |IDT Table |\r
111 // |-------------------|\r
112 // |PeiService Pointer | PeiStackSize\r
113 // |-------------------|\r
114 // | |\r
115 // | Stack |\r
116 // |-------------------|---->\r
117 // | |\r
118 // | |\r
119 // | Heap | PeiTemporayRamSize\r
120 // | |\r
121 // | |\r
122 // |-------------------|----> TempRamBase\r
123\r
124 IdtTableInStack.PeiService = 0;\r
125 for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index ++) {\r
126 CopyMem ((VOID*)&IdtTableInStack.IdtTable[Index], (VOID*)&mIdtEntryTemplate, sizeof (UINT64));\r
127 }\r
128\r
129 IdtDescriptor.Base = (UINTN) &IdtTableInStack.IdtTable;\r
130 IdtDescriptor.Limit = (UINT16)(sizeof (IdtTableInStack.IdtTable) - 1);\r
131\r
132 AsmWriteIdtr (&IdtDescriptor);\r
133\r
134 //\r
135 // Setup the default exception handlers\r
136 //\r
137 Status = InitializeCpuExceptionHandlers (NULL);\r
138 ASSERT_EFI_ERROR (Status);\r
139\r
140 //\r
141 // Update the base address and length of Pei temporary memory\r
142 //\r
143 SecCoreData.DataSize = (UINT16) sizeof (EFI_SEC_PEI_HAND_OFF);\r
144 SecCoreData.BootFirmwareVolumeBase = BootFirmwareVolume;\r
145 SecCoreData.BootFirmwareVolumeSize = (UINTN)(0x100000000ULL - (UINTN) BootFirmwareVolume);\r
146 SecCoreData.TemporaryRamBase = (VOID*)(UINTN) TempRamBase;\r
147 SecCoreData.TemporaryRamSize = SizeOfRam;\r
148 SecCoreData.PeiTemporaryRamBase = SecCoreData.TemporaryRamBase;\r
149 SecCoreData.PeiTemporaryRamSize = SizeOfRam - PeiStackSize;\r
150 SecCoreData.StackBase = (VOID*)(UINTN)(TempRamBase + SecCoreData.PeiTemporaryRamSize);\r
151 SecCoreData.StackSize = PeiStackSize;\r
152\r
153 //\r
154 // Initialize Debug Agent to support source level debug in SEC/PEI phases before memory ready.\r
155 //\r
156 InitializeDebugAgent (DEBUG_AGENT_INIT_PREMEM_SEC, &SecCoreData, SecStartupPhase2);\r
157}\r
158\r
159/**\r
160 Caller provided function to be invoked at the end of InitializeDebugAgent().\r
161\r
162 Entry point to the C language phase of SEC. After the SEC assembly\r
163 code has initialized some temporary memory and set up the stack,\r
164 the control is transferred to this function.\r
165\r
166 @param[in] Context The first input parameter of InitializeDebugAgent().\r
167\r
168**/\r
169VOID\r
170EFIAPI\r
171SecStartupPhase2(\r
172 IN VOID *Context\r
173 )\r
174{\r
175 EFI_SEC_PEI_HAND_OFF *SecCoreData;\r
176 EFI_PEI_PPI_DESCRIPTOR *PpiList;\r
177 UINT32 Index;\r
178 EFI_PEI_PPI_DESCRIPTOR *AllSecPpiList;\r
179 EFI_PEI_CORE_ENTRY_POINT PeiCoreEntryPoint;\r
180\r
181 SecCoreData = (EFI_SEC_PEI_HAND_OFF *) Context;\r
182 AllSecPpiList = (EFI_PEI_PPI_DESCRIPTOR *) SecCoreData->PeiTemporaryRamBase;\r
183 //\r
184 // Find Pei Core entry point. It will report SEC and Pei Core debug information if remote debug\r
185 // is enabled.\r
186 //\r
187 FindAndReportEntryPoints ((EFI_FIRMWARE_VOLUME_HEADER *) SecCoreData->BootFirmwareVolumeBase, &PeiCoreEntryPoint);\r
188 if (PeiCoreEntryPoint == NULL)\r
189 {\r
190 CpuDeadLoop ();\r
191 }\r
192\r
193 //\r
194 // Perform platform specific initialization before entering PeiCore.\r
195 //\r
196 PpiList = SecPlatformMain (SecCoreData);\r
197 if (PpiList != NULL) {\r
198 //\r
199 // Remove the terminal flag from the terminal PPI\r
200 //\r
201 CopyMem (AllSecPpiList, mPeiSecPlatformInformationPpi, sizeof (mPeiSecPlatformInformationPpi));\r
202 Index = sizeof (mPeiSecPlatformInformationPpi) / sizeof (EFI_PEI_PPI_DESCRIPTOR) - 1;\r
203 AllSecPpiList[Index].Flags = AllSecPpiList[Index].Flags & (~EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST);\r
204\r
205 //\r
206 // Append the platform additional PPI list\r
207 //\r
208 Index += 1;\r
209 while (((PpiList->Flags & EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST) != EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST)) {\r
210 CopyMem (&AllSecPpiList[Index], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
211 Index++;\r
212 PpiList++;\r
213 }\r
214\r
215 //\r
216 // Add the terminal PPI\r
217 //\r
218 CopyMem (&AllSecPpiList[Index ++], PpiList, sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
219\r
220 //\r
221 // Set PpiList to the total PPI\r
222 //\r
223 PpiList = AllSecPpiList;\r
224\r
225 //\r
226 // Adjust PEI TEMP RAM Range.\r
227 //\r
228 ASSERT (SecCoreData->PeiTemporaryRamSize > Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
229 SecCoreData->PeiTemporaryRamBase = (VOID *)((UINTN) SecCoreData->PeiTemporaryRamBase + Index * sizeof (EFI_PEI_PPI_DESCRIPTOR));\r
230 SecCoreData->PeiTemporaryRamSize = SecCoreData->PeiTemporaryRamSize - Index * sizeof (EFI_PEI_PPI_DESCRIPTOR);\r
231 } else {\r
232 //\r
233 // No addition PPI, PpiList directly point to the common PPI list.\r
234 //\r
235 PpiList = &mPeiSecPlatformInformationPpi[0];\r
236 }\r
237\r
238 //\r
239 // Report Status Code to indicate transferring to PEI core\r
240 //\r
241 REPORT_STATUS_CODE (\r
242 EFI_PROGRESS_CODE,\r
243 EFI_SOFTWARE_SEC | EFI_SW_SEC_PC_HANDOFF_TO_NEXT\r
244 );\r
245\r
246 //\r
247 // Transfer the control to the PEI core\r
248 //\r
249 ASSERT (PeiCoreEntryPoint != NULL);\r
250 (*PeiCoreEntryPoint) (SecCoreData, PpiList);\r
251\r
252 //\r
253 // Should not come here.\r
254 //\r
255 return;\r
256}\r
257\r
258/**\r
259 TemporaryRamDone() disables the use of Temporary RAM. If present, this service is invoked\r
260 by the PEI Foundation after the EFI_PEI_PERMANANT_MEMORY_INSTALLED_PPI is installed.\r
261\r
262 @retval EFI_SUCCESS Use of Temporary RAM was disabled.\r
263 @retval EFI_INVALID_PARAMETER Temporary RAM could not be disabled.\r
264\r
265**/\r
266EFI_STATUS\r
267EFIAPI\r
268SecTemporaryRamDone (\r
269 VOID\r
270 )\r
271{\r
272 BOOLEAN State;\r
273\r
274 //\r
275 // Migrate DebugAgentContext.\r
276 //\r
277 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, NULL, NULL);\r
278\r
279 //\r
280 // Disable interrupts and save current interrupt state\r
281 //\r
282 State = SaveAndDisableInterrupts();\r
283\r
284 //\r
285 // Disable Temporary RAM after Stack and Heap have been migrated at this point.\r
286 //\r
287 SecPlatformDisableTemporaryMemory ();\r
288\r
289 //\r
290 // Restore original interrupt state\r
291 //\r
292 SetInterruptState (State);\r
293\r
294 return EFI_SUCCESS;\r
295}\r