]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Core/Pei/PeiMain/PeiMain.c
1) Call ProcessNotifyList() to process all Dispatch Notifications after the Memory...
[mirror_edk2.git] / MdeModulePkg / Core / Pei / PeiMain / PeiMain.c
1 /** @file
2 Pei Core Main Entry Point
3
4 Copyright (c) 2006 - 2010, 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 "PeiMain.h"
16
17 EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi = {
18 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
19 &gEfiPeiMemoryDiscoveredPpiGuid,
20 NULL
21 };
22
23 ///
24 /// Pei service instance
25 ///
26 EFI_PEI_SERVICES gPs = {
27 {
28 PEI_SERVICES_SIGNATURE,
29 PEI_SERVICES_REVISION,
30 sizeof (EFI_PEI_SERVICES),
31 0,
32 0
33 },
34 PeiInstallPpi,
35 PeiReInstallPpi,
36 PeiLocatePpi,
37 PeiNotifyPpi,
38
39 PeiGetBootMode,
40 PeiSetBootMode,
41
42 PeiGetHobList,
43 PeiCreateHob,
44
45 PeiFfsFindNextVolume,
46 PeiFfsFindNextFile,
47 PeiFfsFindSectionData,
48
49 PeiInstallPeiMemory,
50 PeiAllocatePages,
51 PeiAllocatePool,
52 (EFI_PEI_COPY_MEM)CopyMem,
53 (EFI_PEI_SET_MEM)SetMem,
54
55 PeiReportStatusCode,
56 PeiResetSystem,
57
58 &gPeiDefaultCpuIoPpi,
59 &gPeiDefaultPciCfg2Ppi,
60
61 PeiFfsFindFileByName,
62 PeiFfsGetFileInfo,
63 PeiFfsGetVolumeInfo,
64 PeiRegisterForShadow
65 };
66
67 /**
68 Shadow PeiCore module from flash to installed memory.
69
70 @param PrivateData PeiCore's private data structure
71
72 @return PeiCore function address after shadowing.
73 **/
74 PEICORE_FUNCTION_POINTER
75 ShadowPeiCore (
76 IN PEI_CORE_INSTANCE *PrivateData
77 )
78 {
79 EFI_PEI_FILE_HANDLE PeiCoreFileHandle;
80 EFI_PHYSICAL_ADDRESS EntryPoint;
81 EFI_STATUS Status;
82 UINT32 AuthenticationState;
83
84 PeiCoreFileHandle = NULL;
85
86 //
87 // Find the PEI Core in the BFV
88 //
89 Status = PrivateData->Fv[0].FvPpi->FindFileByType (
90 PrivateData->Fv[0].FvPpi,
91 EFI_FV_FILETYPE_PEI_CORE,
92 PrivateData->Fv[0].FvHandle,
93 &PeiCoreFileHandle
94 );
95 ASSERT_EFI_ERROR (Status);
96
97 //
98 // Shadow PEI Core into memory so it will run faster
99 //
100 Status = PeiLoadImage (
101 GetPeiServicesTablePointer (),
102 *((EFI_PEI_FILE_HANDLE*)&PeiCoreFileHandle),
103 PEIM_STATE_REGISITER_FOR_SHADOW,
104 &EntryPoint,
105 &AuthenticationState
106 );
107 ASSERT_EFI_ERROR (Status);
108
109 //
110 // Compute the PeiCore's function address after shaowed PeiCore.
111 // _ModuleEntryPoint is PeiCore main function entry
112 //
113 return (PEICORE_FUNCTION_POINTER)((UINTN) EntryPoint + (UINTN) PeiCore - (UINTN) _ModuleEntryPoint);
114 }
115
116 /**
117 This routine is invoked by main entry of PeiMain module during transition
118 from SEC to PEI. After switching stack in the PEI core, it will restart
119 with the old core data.
120
121 @param SecCoreData Points to a data structure containing information about the PEI core's operating
122 environment, such as the size and location of temporary RAM, the stack location and
123 the BFV location.
124 @param PpiList Points to a list of one or more PPI descriptors to be installed initially by the PEI core.
125 An empty PPI list consists of a single descriptor with the end-tag
126 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST. As part of its initialization
127 phase, the PEI Foundation will add these SEC-hosted PPIs to its PPI database such
128 that both the PEI Foundation and any modules can leverage the associated service
129 calls and/or code in these early PPIs
130 @param Data Pointer to old core data that is used to initialize the
131 core's data areas.
132 If NULL, it is first PeiCore entering.
133
134 **/
135 VOID
136 EFIAPI
137 PeiCore (
138 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
139 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
140 IN VOID *Data
141 )
142 {
143 PEI_CORE_INSTANCE PrivateData;
144 EFI_STATUS Status;
145 PEI_CORE_TEMP_POINTERS TempPtr;
146 PEI_CORE_INSTANCE *OldCoreData;
147 EFI_PEI_CPU_IO_PPI *CpuIo;
148 EFI_PEI_PCI_CFG2_PPI *PciCfg;
149 EFI_HOB_HANDOFF_INFO_TABLE *HandoffInformationTable;
150
151 //
152 // Retrieve context passed into PEI Core
153 //
154 OldCoreData = (PEI_CORE_INSTANCE *)Data;
155
156 //
157 // Perform PEI Core phase specific actions.
158 //
159 if (OldCoreData == NULL) {
160 //
161 // If OldCoreData is NULL, means current is the first entry into the PEI Core before memory is available.
162 //
163 ZeroMem (&PrivateData, sizeof (PEI_CORE_INSTANCE));
164 PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;
165 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
166 } else {
167 //
168 // Memory is available to the PEI Core. See if the PEI Core has been shadowed to memory yet.
169 //
170 if (OldCoreData->ShadowedPeiCore == NULL) {
171 //
172 // Fixup the PeiCore's private data
173 //
174 OldCoreData->Ps = &OldCoreData->ServiceTableShadow;
175 OldCoreData->CpuIo = &OldCoreData->ServiceTableShadow.CpuIo;
176 if (OldCoreData->HeapOffsetPositive) {
177 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw + OldCoreData->HeapOffset);
178 } else {
179 OldCoreData->HobList.Raw = (VOID *)(OldCoreData->HobList.Raw - OldCoreData->HeapOffset);
180 }
181
182 //
183 // Initialize libraries that the PEI Core is linked against
184 //
185 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
186
187 //
188 // Fixup for PeiService's address
189 //
190 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&OldCoreData->Ps);
191
192 //
193 // Update HandOffHob for new installed permenent memory
194 //
195 HandoffInformationTable = OldCoreData->HobList.HandoffInformationTable;
196 if (OldCoreData->HeapOffsetPositive) {
197 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList + OldCoreData->HeapOffset;
198 } else {
199 HandoffInformationTable->EfiEndOfHobList = HandoffInformationTable->EfiEndOfHobList - OldCoreData->HeapOffset;
200 }
201 HandoffInformationTable->EfiMemoryTop = OldCoreData->PhysicalMemoryBegin + OldCoreData->PhysicalMemoryLength;
202 HandoffInformationTable->EfiMemoryBottom = OldCoreData->PhysicalMemoryBegin;
203 HandoffInformationTable->EfiFreeMemoryTop = OldCoreData->FreePhysicalMemoryTop;
204 HandoffInformationTable->EfiFreeMemoryBottom = HandoffInformationTable->EfiEndOfHobList + sizeof (EFI_HOB_GENERIC_HEADER);
205
206 //
207 // We need convert the PPI desciptor's pointer
208 //
209 ConvertPpiPointers (OldCoreData, (UINTN)SecCoreData->TemporaryRamBase, (UINTN)SecCoreData->TemporaryRamBase + SecCoreData->TemporaryRamSize, OldCoreData->HeapOffset, OldCoreData->HeapOffsetPositive);
210
211 //
212 // After the whole temporary memory is migrated, then we can allocate page in
213 // permenent memory.
214 //
215 OldCoreData->PeiMemoryInstalled = TRUE;
216
217 //
218 // Indicate that PeiCore reenter
219 //
220 OldCoreData->PeimDispatcherReenter = TRUE;
221
222 if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (OldCoreData->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
223 //
224 // if Loading Module at Fixed Address is enabled, allocate the PEI code memory range usage bit map array.
225 // Every bit in the array indicate the status of the corresponding memory page available or not
226 //
227 OldCoreData->PeiCodeMemoryRangeUsageBitMap = AllocateZeroPool (((PcdGet32(PcdLoadFixAddressPeiCodePageNumber)>>6) + 1)*sizeof(UINT64));
228 }
229
230 //
231 // Shadow PEI Core. When permanent memory is avaiable, shadow
232 // PEI Core and PEIMs to get high performance.
233 //
234 OldCoreData->ShadowedPeiCore = ShadowPeiCore (OldCoreData);
235
236 //
237 // PEI Core has now been shadowed to memory. Restart PEI Core in memory.
238 //
239 OldCoreData->ShadowedPeiCore (SecCoreData, PpiList, OldCoreData);
240
241 //
242 // Should never reach here.
243 //
244 ASSERT (FALSE);
245 CpuDeadLoop();
246 }
247
248 //
249 // Memory is available to the PEI Core and the PEI Core has been shadowed to memory.
250 //
251
252 CopyMem (&PrivateData, OldCoreData, sizeof (PrivateData));
253
254 CpuIo = (VOID*)PrivateData.ServiceTableShadow.CpuIo;
255 PciCfg = (VOID*)PrivateData.ServiceTableShadow.PciCfg;
256
257 CopyMem (&PrivateData.ServiceTableShadow, &gPs, sizeof (gPs));
258
259 PrivateData.ServiceTableShadow.CpuIo = CpuIo;
260 PrivateData.ServiceTableShadow.PciCfg = PciCfg;
261 }
262
263 //
264 // Cache a pointer to the PEI Services Table that is either in temporary memory or permanent memory
265 //
266 PrivateData.Ps = &PrivateData.ServiceTableShadow;
267
268 //
269 // Initialize libraries that the PEI Core is linked against
270 //
271 ProcessLibraryConstructorList (NULL, (CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
272
273 //
274 // Save PeiServicePointer so that it can be retrieved anywhere.
275 //
276 SetPeiServicesTablePointer ((CONST EFI_PEI_SERVICES **)&PrivateData.Ps);
277
278 //
279 // Initialize PEI Core Services
280 //
281 InitializeMemoryServices (&PrivateData, SecCoreData, OldCoreData);
282 InitializePpiServices (&PrivateData, OldCoreData);
283
284 //
285 // Update performance measurements
286 //
287 if (OldCoreData == NULL) {
288 PERF_START (NULL, "SEC", NULL, 1);
289 PERF_END (NULL, "SEC", NULL, 0);
290
291 //
292 // If first pass, start performance measurement.
293 //
294 PERF_START (NULL,"PEI", NULL, 0);
295 PERF_START (NULL,"PreMem", NULL, 0);
296
297 } else {
298 PERF_END (NULL,"PreMem", NULL, 0);
299 PERF_START (NULL,"PostMem", NULL, 0);
300 }
301
302 //
303 // Complete PEI Core Service initialization
304 //
305 InitializeSecurityServices (&PrivateData.Ps, OldCoreData);
306 InitializeDispatcherData (&PrivateData, OldCoreData, SecCoreData);
307 InitializeImageServices (&PrivateData, OldCoreData);
308
309 //
310 // Perform PEI Core Phase specific actions
311 //
312 if (OldCoreData == NULL) {
313 //
314 // Report Status Code EFI_SW_PC_INIT
315 //
316 REPORT_STATUS_CODE (
317 EFI_PROGRESS_CODE,
318 (EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT)
319 );
320
321 //
322 // If SEC provided any PPI services to PEI, install them.
323 //
324 if (PpiList != NULL) {
325 Status = PeiServicesInstallPpi (PpiList);
326 ASSERT_EFI_ERROR (Status);
327 }
328 } else {
329 //
330 // Alert any listeners that there is permanent memory available
331 //
332 PERF_START (NULL,"DisMem", NULL, 0);
333 Status = PeiServicesInstallPpi (&mMemoryDiscoveredPpi);
334
335 //
336 // Process the Notify list and dispatch any notifies for the Memory Discovered PPI
337 //
338 ProcessNotifyList (&PrivateData);
339
340 PERF_END (NULL,"DisMem", NULL, 0);
341 }
342
343 //
344 // Call PEIM dispatcher
345 //
346 PeiDispatcher (SecCoreData, &PrivateData);
347
348 //
349 // Check if InstallPeiMemory service was called.
350 //
351 ASSERT(PrivateData.PeiMemoryInstalled == TRUE);
352
353 //
354 // Measure PEI Core execution time.
355 //
356 PERF_END (NULL, "PostMem", NULL, 0);
357
358 //
359 // Lookup DXE IPL PPI
360 //
361 Status = PeiServicesLocatePpi (
362 &gEfiDxeIplPpiGuid,
363 0,
364 NULL,
365 (VOID **)&TempPtr.DxeIpl
366 );
367 ASSERT_EFI_ERROR (Status);
368
369 //
370 // Enter DxeIpl to load Dxe core.
371 //
372 DEBUG ((EFI_D_INFO, "DXE IPL Entry\n"));
373 Status = TempPtr.DxeIpl->Entry (
374 TempPtr.DxeIpl,
375 &PrivateData.Ps,
376 PrivateData.HobList
377 );
378 //
379 // Should never reach here.
380 //
381 ASSERT_EFI_ERROR (Status);
382 CpuDeadLoop();
383 }