]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
UefiCpuPkg: Error Level is not used correctly
[mirror_edk2.git] / UefiCpuPkg / Universal / Acpi / S3Resume2Pei / S3Resume.c
... / ...
CommitLineData
1/** @file\r
2 This module produces the EFI_PEI_S3_RESUME2_PPI.\r
3 This module works with StandAloneBootScriptExecutor to S3 resume to OS.\r
4 This module will execute the boot script saved during last boot and after that,\r
5 control is passed to OS waking up handler.\r
6\r
7 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
8 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
9\r
10 This program and the accompanying materials\r
11 are licensed and made available under the terms and conditions\r
12 of the BSD License which accompanies this distribution. The\r
13 full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.php\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include <PiPei.h>\r
22\r
23#include <Guid/AcpiS3Context.h>\r
24#include <Guid/BootScriptExecutorVariable.h>\r
25#include <Guid/Performance.h>\r
26#include <Ppi/ReadOnlyVariable2.h>\r
27#include <Ppi/S3Resume2.h>\r
28#include <Ppi/SmmAccess.h>\r
29#include <Ppi/PostBootScriptTable.h>\r
30#include <Ppi/EndOfPeiPhase.h>\r
31\r
32#include <Library/DebugLib.h>\r
33#include <Library/BaseLib.h>\r
34#include <Library/TimerLib.h>\r
35#include <Library/PeimEntryPoint.h>\r
36#include <Library/PeiServicesLib.h>\r
37#include <Library/HobLib.h>\r
38#include <Library/PerformanceLib.h>\r
39#include <Library/PeiServicesTablePointerLib.h>\r
40#include <Library/IoLib.h>\r
41#include <Library/BaseMemoryLib.h>\r
42#include <Library/MemoryAllocationLib.h>\r
43#include <Library/PcdLib.h>\r
44#include <Library/DebugAgentLib.h>\r
45#include <Library/LocalApicLib.h>\r
46#include <Library/ReportStatusCodeLib.h>\r
47#include <Library/PrintLib.h>\r
48#include <Library/HobLib.h>\r
49#include <Library/LockBoxLib.h>\r
50#include <IndustryStandard/Acpi.h>\r
51\r
52/**\r
53 This macro aligns the address of a variable with auto storage\r
54 duration down to CPU_STACK_ALIGNMENT.\r
55\r
56 Since the stack grows downward, the result preserves more of the\r
57 stack than the original address (or the same amount), not less.\r
58**/\r
59#define STACK_ALIGN_DOWN(Ptr) \\r
60 ((UINTN)(Ptr) & ~(UINTN)(CPU_STACK_ALIGNMENT - 1))\r
61\r
62#define PAGING_1G_ADDRESS_MASK_64 0x000FFFFFC0000000ull\r
63\r
64#pragma pack(1)\r
65typedef union {\r
66 struct {\r
67 UINT32 LimitLow : 16;\r
68 UINT32 BaseLow : 16;\r
69 UINT32 BaseMid : 8;\r
70 UINT32 Type : 4;\r
71 UINT32 System : 1;\r
72 UINT32 Dpl : 2;\r
73 UINT32 Present : 1;\r
74 UINT32 LimitHigh : 4;\r
75 UINT32 Software : 1;\r
76 UINT32 Reserved : 1;\r
77 UINT32 DefaultSize : 1;\r
78 UINT32 Granularity : 1;\r
79 UINT32 BaseHigh : 8;\r
80 } Bits;\r
81 UINT64 Uint64;\r
82} IA32_GDT;\r
83\r
84//\r
85// Page-Map Level-4 Offset (PML4) and\r
86// Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB\r
87//\r
88typedef union {\r
89 struct {\r
90 UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory\r
91 UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write\r
92 UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User\r
93 UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching\r
94 UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached\r
95 UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r
96 UINT64 Reserved:1; // Reserved\r
97 UINT64 MustBeZero:2; // Must Be Zero\r
98 UINT64 Available:3; // Available for use by system software\r
99 UINT64 PageTableBaseAddress:40; // Page Table Base Address\r
100 UINT64 AvabilableHigh:11; // Available for use by system software\r
101 UINT64 Nx:1; // No Execute bit\r
102 } Bits;\r
103 UINT64 Uint64;\r
104} PAGE_MAP_AND_DIRECTORY_POINTER;\r
105\r
106//\r
107// Page Table Entry 2MB\r
108//\r
109typedef union {\r
110 struct {\r
111 UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory\r
112 UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write\r
113 UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User\r
114 UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching\r
115 UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached\r
116 UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r
117 UINT64 Dirty:1; // 0 = Not Dirty, 1 = written by processor on access to page\r
118 UINT64 MustBe1:1; // Must be 1 \r
119 UINT64 Global:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write\r
120 UINT64 Available:3; // Available for use by system software\r
121 UINT64 PAT:1; //\r
122 UINT64 MustBeZero:8; // Must be zero;\r
123 UINT64 PageTableBaseAddress:31; // Page Table Base Address\r
124 UINT64 AvabilableHigh:11; // Available for use by system software\r
125 UINT64 Nx:1; // 0 = Execute Code, 1 = No Code Execution\r
126 } Bits;\r
127 UINT64 Uint64;\r
128} PAGE_TABLE_ENTRY;\r
129\r
130//\r
131// Page Table Entry 1GB\r
132//\r
133typedef union {\r
134 struct {\r
135 UINT64 Present:1; // 0 = Not present in memory, 1 = Present in memory\r
136 UINT64 ReadWrite:1; // 0 = Read-Only, 1= Read/Write\r
137 UINT64 UserSupervisor:1; // 0 = Supervisor, 1=User\r
138 UINT64 WriteThrough:1; // 0 = Write-Back caching, 1=Write-Through caching\r
139 UINT64 CacheDisabled:1; // 0 = Cached, 1=Non-Cached\r
140 UINT64 Accessed:1; // 0 = Not accessed, 1 = Accessed (set by CPU)\r
141 UINT64 Dirty:1; // 0 = Not Dirty, 1 = written by processor on access to page\r
142 UINT64 MustBe1:1; // Must be 1 \r
143 UINT64 Global:1; // 0 = Not global page, 1 = global page TLB not cleared on CR3 write\r
144 UINT64 Available:3; // Available for use by system software\r
145 UINT64 PAT:1; //\r
146 UINT64 MustBeZero:17; // Must be zero;\r
147 UINT64 PageTableBaseAddress:22; // Page Table Base Address\r
148 UINT64 AvabilableHigh:11; // Available for use by system software\r
149 UINT64 Nx:1; // 0 = Execute Code, 1 = No Code Execution\r
150 } Bits;\r
151 UINT64 Uint64;\r
152} PAGE_TABLE_1G_ENTRY;\r
153\r
154#pragma pack()\r
155\r
156//\r
157// Function prototypes\r
158//\r
159/**\r
160 a ASM function to transfer control to OS.\r
161 \r
162 @param S3WakingVector The S3 waking up vector saved in ACPI Facs table\r
163 @param AcpiLowMemoryBase a buffer under 1M which could be used during the transfer \r
164**/\r
165typedef\r
166VOID\r
167(EFIAPI *ASM_TRANSFER_CONTROL) (\r
168 IN UINT32 S3WakingVector,\r
169 IN UINT32 AcpiLowMemoryBase\r
170 );\r
171\r
172/**\r
173 Restores the platform to its preboot configuration for an S3 resume and\r
174 jumps to the OS waking vector.\r
175\r
176 This function will restore the platform to its pre-boot configuration that was\r
177 pre-stored in the boot script table and transfer control to OS waking vector.\r
178 Upon invocation, this function is responsible for locating the following\r
179 information before jumping to OS waking vector:\r
180 - ACPI tables\r
181 - boot script table\r
182 - any other information that it needs\r
183\r
184 The S3RestoreConfig() function then executes the pre-stored boot script table\r
185 and transitions the platform to the pre-boot state. The boot script is recorded\r
186 during regular boot using the EFI_S3_SAVE_STATE_PROTOCOL.Write() and\r
187 EFI_S3_SMM_SAVE_STATE_PROTOCOL.Write() functions. Finally, this function\r
188 transfers control to the OS waking vector. If the OS supports only a real-mode\r
189 waking vector, this function will switch from flat mode to real mode before\r
190 jumping to the waking vector. If all platform pre-boot configurations are\r
191 successfully restored and all other necessary information is ready, this\r
192 function will never return and instead will directly jump to the OS waking\r
193 vector. If this function returns, it indicates that the attempt to resume\r
194 from the ACPI S3 sleep state failed.\r
195\r
196 @param[in] This Pointer to this instance of the PEI_S3_RESUME_PPI\r
197\r
198 @retval EFI_ABORTED Execution of the S3 resume boot script table failed.\r
199 @retval EFI_NOT_FOUND Some necessary information that is used for the S3\r
200 resume boot path could not be located.\r
201\r
202**/\r
203EFI_STATUS\r
204EFIAPI\r
205S3RestoreConfig2 (\r
206 IN EFI_PEI_S3_RESUME2_PPI *This\r
207 );\r
208\r
209/**\r
210 Set data segment selectors value including DS/ES/FS/GS/SS.\r
211\r
212 @param[in] SelectorValue Segment selector value to be set.\r
213\r
214**/\r
215VOID\r
216EFIAPI\r
217AsmSetDataSelectors (\r
218 IN UINT16 SelectorValue\r
219 );\r
220\r
221//\r
222// Globals\r
223//\r
224EFI_PEI_S3_RESUME2_PPI mS3ResumePpi = { S3RestoreConfig2 };\r
225\r
226EFI_PEI_PPI_DESCRIPTOR mPpiList = {\r
227 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
228 &gEfiPeiS3Resume2PpiGuid,\r
229 &mS3ResumePpi\r
230};\r
231\r
232EFI_PEI_PPI_DESCRIPTOR mPpiListPostScriptTable = {\r
233 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
234 &gPeiPostScriptTablePpiGuid,\r
235 0\r
236};\r
237\r
238EFI_PEI_PPI_DESCRIPTOR mPpiListEndOfPeiTable = {\r
239 (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
240 &gEfiEndOfPeiSignalPpiGuid,\r
241 0\r
242};\r
243\r
244//\r
245// Global Descriptor Table (GDT)\r
246//\r
247GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries[] = {\r
248/* selector { Global Segment Descriptor } */\r
249/* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},\r
250/* 0x08 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},\r
251/* 0x10 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}},\r
252/* 0x18 */ {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}},\r
253/* 0x20 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},\r
254/* 0x28 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 0, 1, 0}},\r
255/* 0x30 */ {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 0, 1, 0}},\r
256/* 0x38 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 1, 0, 1, 0}},\r
257/* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},\r
258};\r
259\r
260#define DATA_SEGEMENT_SELECTOR 0x18\r
261\r
262//\r
263// IA32 Gdt register\r
264//\r
265GLOBAL_REMOVE_IF_UNREFERENCED CONST IA32_DESCRIPTOR mGdt = {\r
266 sizeof (mGdtEntries) - 1,\r
267 (UINTN) mGdtEntries\r
268 };\r
269\r
270/**\r
271 Performance measure function to get S3 detailed performance data.\r
272\r
273 This function will getS3 detailed performance data and saved in pre-reserved ACPI memory.\r
274**/\r
275VOID\r
276WriteToOsS3PerformanceData (\r
277 VOID\r
278 )\r
279{\r
280 EFI_STATUS Status;\r
281 EFI_PHYSICAL_ADDRESS mAcpiLowMemoryBase;\r
282 PERF_HEADER *PerfHeader;\r
283 PERF_DATA *PerfData;\r
284 UINT64 Ticker;\r
285 UINTN Index;\r
286 EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariableServices;\r
287 UINTN VarSize;\r
288 UINTN LogEntryKey;\r
289 CONST VOID *Handle;\r
290 CONST CHAR8 *Token;\r
291 CONST CHAR8 *Module;\r
292 UINT64 StartTicker;\r
293 UINT64 EndTicker;\r
294 UINT64 StartValue;\r
295 UINT64 EndValue;\r
296 BOOLEAN CountUp;\r
297 UINT64 Freq;\r
298\r
299 //\r
300 // Retrieve time stamp count as early as possible\r
301 //\r
302 Ticker = GetPerformanceCounter ();\r
303\r
304 Freq = GetPerformanceCounterProperties (&StartValue, &EndValue);\r
305\r
306 Freq = DivU64x32 (Freq, 1000);\r
307\r
308 Status = PeiServicesLocatePpi (\r
309 &gEfiPeiReadOnlyVariable2PpiGuid,\r
310 0,\r
311 NULL,\r
312 (VOID **) &VariableServices\r
313 );\r
314 if (EFI_ERROR (Status)) {\r
315 return;\r
316 }\r
317\r
318 VarSize = sizeof (EFI_PHYSICAL_ADDRESS);\r
319 Status = VariableServices->GetVariable (\r
320 VariableServices,\r
321 L"PerfDataMemAddr",\r
322 &gPerformanceProtocolGuid,\r
323 NULL,\r
324 &VarSize,\r
325 &mAcpiLowMemoryBase\r
326 );\r
327 if (EFI_ERROR (Status)) {\r
328 DEBUG ((EFI_D_ERROR, "Fail to retrieve variable to log S3 performance data \n"));\r
329 return;\r
330 }\r
331\r
332 PerfHeader = (PERF_HEADER *) (UINTN) mAcpiLowMemoryBase;\r
333\r
334 if (PerfHeader->Signiture != PERFORMANCE_SIGNATURE) {\r
335 DEBUG ((EFI_D_ERROR, "Performance data in ACPI memory get corrupted! \n"));\r
336 return;\r
337 }\r
338\r
339 //\r
340 // Record total S3 resume time.\r
341 //\r
342 if (EndValue >= StartValue) {\r
343 PerfHeader->S3Resume = Ticker - StartValue;\r
344 CountUp = TRUE;\r
345 } else {\r
346 PerfHeader->S3Resume = StartValue - Ticker;\r
347 CountUp = FALSE;\r
348 }\r
349\r
350 //\r
351 // Get S3 detailed performance data\r
352 //\r
353 Index = 0;\r
354 LogEntryKey = 0;\r
355 while ((LogEntryKey = GetPerformanceMeasurement (\r
356 LogEntryKey,\r
357 &Handle,\r
358 &Token,\r
359 &Module,\r
360 &StartTicker,\r
361 &EndTicker)) != 0) {\r
362 if (EndTicker != 0) {\r
363 PerfData = &PerfHeader->S3Entry[Index];\r
364\r
365 //\r
366 // Use File Handle to specify the different performance log for PEIM.\r
367 // File Handle is the base address of PEIM FFS file.\r
368 //\r
369 if ((AsciiStrnCmp (Token, "PEIM", PEI_PERFORMANCE_STRING_SIZE) == 0) && (Handle != NULL)) {\r
370 AsciiSPrint (PerfData->Token, PERF_TOKEN_LENGTH, "0x%11p", Handle);\r
371 } else {\r
372 AsciiStrnCpyS (PerfData->Token, PERF_TOKEN_SIZE, Token, PERF_TOKEN_LENGTH);\r
373 }\r
374 if (StartTicker == 1) {\r
375 StartTicker = StartValue;\r
376 }\r
377 if (EndTicker == 1) {\r
378 EndTicker = StartValue;\r
379 }\r
380 Ticker = CountUp? (EndTicker - StartTicker) : (StartTicker - EndTicker);\r
381 PerfData->Duration = (UINT32) DivU64x32 (Ticker, (UINT32) Freq);\r
382\r
383 //\r
384 // Only Record > 1ms performance data so that more big performance can be recorded.\r
385 //\r
386 if ((Ticker > Freq) && (++Index >= PERF_PEI_ENTRY_MAX_NUM)) {\r
387 //\r
388 // Reach the maximum number of PEI performance log entries.\r
389 //\r
390 break;\r
391 }\r
392 }\r
393 }\r
394 PerfHeader->S3EntryNum = (UINT32) Index;\r
395}\r
396\r
397/**\r
398 The function will check if current waking vector is long mode.\r
399\r
400 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT\r
401\r
402 @retval TRUE Current context need long mode waking vector.\r
403 @retval FALSE Current context need not long mode waking vector.\r
404**/\r
405BOOLEAN\r
406IsLongModeWakingVector (\r
407 IN ACPI_S3_CONTEXT *AcpiS3Context\r
408 )\r
409{\r
410 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *Facs;\r
411\r
412 Facs = (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) (AcpiS3Context->AcpiFacsTable));\r
413 if ((Facs == NULL) ||\r
414 (Facs->Signature != EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||\r
415 ((Facs->FirmwareWakingVector == 0) && (Facs->XFirmwareWakingVector == 0)) ) {\r
416 // Something wrong with FACS\r
417 return FALSE;\r
418 }\r
419 if (Facs->XFirmwareWakingVector != 0) {\r
420 if ((Facs->Version == EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION) &&\r
421 ((Facs->Flags & EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F) != 0) &&\r
422 ((Facs->Flags & EFI_ACPI_4_0_OSPM_64BIT_WAKE__F) != 0)) {\r
423 // Both BIOS and OS wants 64bit vector\r
424 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
425 return TRUE;\r
426 }\r
427 }\r
428 }\r
429 return FALSE;\r
430}\r
431\r
432/**\r
433 Jump to OS waking vector.\r
434 The function will install boot script done PPI, report S3 resume status code, and then jump to OS waking vector.\r
435\r
436 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT\r
437 @param PeiS3ResumeState a pointer to a structure of PEI_S3_RESUME_STATE\r
438**/\r
439VOID\r
440EFIAPI\r
441S3ResumeBootOs (\r
442 IN ACPI_S3_CONTEXT *AcpiS3Context,\r
443 IN PEI_S3_RESUME_STATE *PeiS3ResumeState\r
444 )\r
445{\r
446 EFI_STATUS Status;\r
447 EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *Facs;\r
448 ASM_TRANSFER_CONTROL AsmTransferControl;\r
449 UINTN TempStackTop;\r
450 UINTN TempStack[0x10];\r
451\r
452 //\r
453 // Restore IDT\r
454 //\r
455 AsmWriteIdtr (&PeiS3ResumeState->Idtr);\r
456\r
457 if (PeiS3ResumeState->ReturnStatus != EFI_SUCCESS) {\r
458 //\r
459 // Report Status code that boot script execution is failed\r
460 //\r
461 REPORT_STATUS_CODE (\r
462 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
463 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_BOOT_SCRIPT_ERROR)\r
464 );\r
465 }\r
466\r
467 //\r
468 // NOTE: Because Debug Timer interrupt and system interrupts will be disabled \r
469 // in BootScriptExecuteDxe, the rest code in S3ResumeBootOs() cannot be halted\r
470 // by soft debugger.\r
471 //\r
472\r
473 PERF_END (NULL, "ScriptExec", NULL, 0);\r
474\r
475 //\r
476 // Install BootScriptDonePpi\r
477 //\r
478 Status = PeiServicesInstallPpi (&mPpiListPostScriptTable);\r
479 ASSERT_EFI_ERROR (Status);\r
480\r
481 //\r
482 // Get ACPI Table Address\r
483 //\r
484 Facs = (EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) (AcpiS3Context->AcpiFacsTable));\r
485\r
486 if ((Facs == NULL) ||\r
487 (Facs->Signature != EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||\r
488 ((Facs->FirmwareWakingVector == 0) && (Facs->XFirmwareWakingVector == 0)) ) {\r
489 //\r
490 // Report Status code that no valid vector is found\r
491 //\r
492 REPORT_STATUS_CODE (\r
493 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
494 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_OS_WAKE_ERROR)\r
495 );\r
496 CpuDeadLoop ();\r
497 return ;\r
498 }\r
499\r
500 //\r
501 // Install EndOfPeiPpi\r
502 //\r
503 Status = PeiServicesInstallPpi (&mPpiListEndOfPeiTable);\r
504 ASSERT_EFI_ERROR (Status);\r
505\r
506 //\r
507 // report status code on S3 resume\r
508 //\r
509 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_OS_WAKE);\r
510\r
511 PERF_CODE (\r
512 WriteToOsS3PerformanceData ();\r
513 );\r
514\r
515 AsmTransferControl = (ASM_TRANSFER_CONTROL)(UINTN)PeiS3ResumeState->AsmTransferControl;\r
516 if (Facs->XFirmwareWakingVector != 0) {\r
517 //\r
518 // Switch to native waking vector\r
519 //\r
520 TempStackTop = (UINTN)&TempStack + sizeof(TempStack);\r
521 DEBUG ((\r
522 DEBUG_INFO,\r
523 "%a() Stack Base: 0x%x, Stack Size: 0x%x\n",\r
524 __FUNCTION__,\r
525 TempStackTop,\r
526 sizeof (TempStack)\r
527 ));\r
528 if ((Facs->Version == EFI_ACPI_4_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION) &&\r
529 ((Facs->Flags & EFI_ACPI_4_0_64BIT_WAKE_SUPPORTED_F) != 0) &&\r
530 ((Facs->Flags & EFI_ACPI_4_0_OSPM_64BIT_WAKE__F) != 0)) {\r
531 //\r
532 // X64 long mode waking vector\r
533 //\r
534 DEBUG ((DEBUG_INFO, "Transfer to 64bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));\r
535 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
536 AsmEnablePaging64 (\r
537 0x38,\r
538 Facs->XFirmwareWakingVector,\r
539 0,\r
540 0,\r
541 (UINT64)(UINTN)TempStackTop\r
542 );\r
543 } else {\r
544 //\r
545 // Report Status code that no valid waking vector is found\r
546 //\r
547 REPORT_STATUS_CODE (\r
548 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
549 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_OS_WAKE_ERROR)\r
550 );\r
551 DEBUG (( EFI_D_ERROR, "Unsupported for 32bit DXE transfer to 64bit OS waking vector!\r\n"));\r
552 ASSERT (FALSE);\r
553 CpuDeadLoop ();\r
554 return ;\r
555 }\r
556 } else {\r
557 //\r
558 // IA32 protected mode waking vector (Page disabled)\r
559 //\r
560 DEBUG ((DEBUG_INFO, "Transfer to 32bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));\r
561 SwitchStack (\r
562 (SWITCH_STACK_ENTRY_POINT) (UINTN) Facs->XFirmwareWakingVector,\r
563 NULL,\r
564 NULL,\r
565 (VOID *)(UINTN)TempStackTop\r
566 );\r
567 }\r
568 } else {\r
569 //\r
570 // 16bit Realmode waking vector\r
571 //\r
572 DEBUG ((DEBUG_INFO, "Transfer to 16bit OS waking vector - %x\r\n", (UINTN)Facs->FirmwareWakingVector));\r
573 AsmTransferControl (Facs->FirmwareWakingVector, 0x0);\r
574 }\r
575\r
576 //\r
577 // Report Status code the failure of S3Resume\r
578 //\r
579 REPORT_STATUS_CODE (\r
580 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
581 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_OS_WAKE_ERROR)\r
582 );\r
583\r
584 //\r
585 // Never run to here\r
586 //\r
587 CpuDeadLoop();\r
588}\r
589\r
590/**\r
591 Restore S3 page table because we do not trust ACPINvs content.\r
592 If BootScriptExector driver will not run in 64-bit mode, this function will do nothing. \r
593\r
594 @param S3NvsPageTableAddress PageTableAddress in ACPINvs\r
595 @param Build4GPageTableOnly If BIOS just build 4G page table only\r
596**/\r
597VOID\r
598RestoreS3PageTables (\r
599 IN UINTN S3NvsPageTableAddress,\r
600 IN BOOLEAN Build4GPageTableOnly\r
601 )\r
602{\r
603 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
604 UINT32 RegEax;\r
605 UINT32 RegEdx;\r
606 UINT8 PhysicalAddressBits;\r
607 EFI_PHYSICAL_ADDRESS PageAddress;\r
608 UINTN IndexOfPml4Entries;\r
609 UINTN IndexOfPdpEntries;\r
610 UINTN IndexOfPageDirectoryEntries;\r
611 UINT32 NumberOfPml4EntriesNeeded;\r
612 UINT32 NumberOfPdpEntriesNeeded;\r
613 PAGE_MAP_AND_DIRECTORY_POINTER *PageMapLevel4Entry;\r
614 PAGE_MAP_AND_DIRECTORY_POINTER *PageMap;\r
615 PAGE_MAP_AND_DIRECTORY_POINTER *PageDirectoryPointerEntry;\r
616 PAGE_TABLE_ENTRY *PageDirectoryEntry;\r
617 VOID *Hob;\r
618 BOOLEAN Page1GSupport;\r
619 PAGE_TABLE_1G_ENTRY *PageDirectory1GEntry;\r
620 UINT64 AddressEncMask;\r
621\r
622 //\r
623 // Make sure AddressEncMask is contained to smallest supported address field\r
624 //\r
625 AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;\r
626\r
627 //\r
628 // NOTE: We have to ASSUME the page table generation format, because we do not know whole page table information.\r
629 // The whole page table is too large to be saved in SMRAM.\r
630 //\r
631 // The assumption is : whole page table is allocated in CONTINUOUS memory and CR3 points to TOP page.\r
632 //\r
633 DEBUG ((DEBUG_INFO, "S3NvsPageTableAddress - %x (%x)\n", (UINTN)S3NvsPageTableAddress, (UINTN)Build4GPageTableOnly));\r
634\r
635 //\r
636 // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.\r
637 //\r
638 PageMap = (PAGE_MAP_AND_DIRECTORY_POINTER *)S3NvsPageTableAddress;\r
639 S3NvsPageTableAddress += SIZE_4KB;\r
640 \r
641 Page1GSupport = FALSE;\r
642 if (PcdGetBool(PcdUse1GPageTable)) {\r
643 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
644 if (RegEax >= 0x80000001) {\r
645 AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);\r
646 if ((RegEdx & BIT26) != 0) {\r
647 Page1GSupport = TRUE;\r
648 }\r
649 }\r
650 }\r
651 \r
652 //\r
653 // Get physical address bits supported.\r
654 //\r
655 Hob = GetFirstHob (EFI_HOB_TYPE_CPU);\r
656 if (Hob != NULL) {\r
657 PhysicalAddressBits = ((EFI_HOB_CPU *) Hob)->SizeOfMemorySpace;\r
658 } else {\r
659 AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);\r
660 if (RegEax >= 0x80000008) {\r
661 AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);\r
662 PhysicalAddressBits = (UINT8) RegEax;\r
663 } else {\r
664 PhysicalAddressBits = 36;\r
665 }\r
666 }\r
667 \r
668 //\r
669 // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.\r
670 //\r
671 ASSERT (PhysicalAddressBits <= 52);\r
672 if (PhysicalAddressBits > 48) {\r
673 PhysicalAddressBits = 48;\r
674 }\r
675\r
676 //\r
677 // NOTE: In order to save time to create full page table, we just create 4G page table by default.\r
678 // And let PF handler in BootScript driver to create more on request.\r
679 //\r
680 if (Build4GPageTableOnly) {\r
681 PhysicalAddressBits = 32;\r
682 ZeroMem (PageMap, EFI_PAGES_TO_SIZE(2));\r
683 }\r
684 //\r
685 // Calculate the table entries needed.\r
686 //\r
687 if (PhysicalAddressBits <= 39) {\r
688 NumberOfPml4EntriesNeeded = 1;\r
689 NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));\r
690 } else {\r
691 NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 39));\r
692 NumberOfPdpEntriesNeeded = 512;\r
693 }\r
694 \r
695 PageMapLevel4Entry = PageMap;\r
696 PageAddress = 0;\r
697 for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {\r
698 //\r
699 // Each PML4 entry points to a page of Page Directory Pointer entires.\r
700 // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.\r
701 //\r
702 PageDirectoryPointerEntry = (PAGE_MAP_AND_DIRECTORY_POINTER *)S3NvsPageTableAddress;\r
703 S3NvsPageTableAddress += SIZE_4KB;\r
704 \r
705 //\r
706 // Make a PML4 Entry\r
707 //\r
708 PageMapLevel4Entry->Uint64 = (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask;\r
709 PageMapLevel4Entry->Bits.ReadWrite = 1;\r
710 PageMapLevel4Entry->Bits.Present = 1;\r
711\r
712 if (Page1GSupport) {\r
713 PageDirectory1GEntry = (VOID *) PageDirectoryPointerEntry;\r
714 \r
715 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {\r
716 //\r
717 // Fill in the Page Directory entries\r
718 //\r
719 PageDirectory1GEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;\r
720 PageDirectory1GEntry->Bits.ReadWrite = 1;\r
721 PageDirectory1GEntry->Bits.Present = 1;\r
722 PageDirectory1GEntry->Bits.MustBe1 = 1;\r
723 }\r
724 } else {\r
725 for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {\r
726 //\r
727 // Each Directory Pointer entries points to a page of Page Directory entires.\r
728 // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.\r
729 // \r
730 PageDirectoryEntry = (PAGE_TABLE_ENTRY *)S3NvsPageTableAddress;\r
731 S3NvsPageTableAddress += SIZE_4KB;\r
732 \r
733 //\r
734 // Fill in a Page Directory Pointer Entries\r
735 //\r
736 PageDirectoryPointerEntry->Uint64 = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;\r
737 PageDirectoryPointerEntry->Bits.ReadWrite = 1;\r
738 PageDirectoryPointerEntry->Bits.Present = 1;\r
739 \r
740 for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {\r
741 //\r
742 // Fill in the Page Directory entries\r
743 //\r
744 PageDirectoryEntry->Uint64 = (UINT64)PageAddress | AddressEncMask;\r
745 PageDirectoryEntry->Bits.ReadWrite = 1;\r
746 PageDirectoryEntry->Bits.Present = 1;\r
747 PageDirectoryEntry->Bits.MustBe1 = 1;\r
748 }\r
749 }\r
750 }\r
751 }\r
752 return ;\r
753 } else {\r
754 //\r
755 // If DXE is running 32-bit mode, no need to establish page table.\r
756 //\r
757 return ;\r
758 }\r
759}\r
760\r
761/**\r
762 Jump to boot script executor driver.\r
763\r
764 The function will close and lock SMRAM and then jump to boot script execute driver to executing S3 boot script table.\r
765\r
766 @param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT\r
767 @param EfiBootScriptExecutorVariable The function entry to executing S3 boot Script table. This function is build in\r
768 boot script execute driver\r
769**/\r
770VOID\r
771EFIAPI\r
772S3ResumeExecuteBootScript (\r
773 IN ACPI_S3_CONTEXT *AcpiS3Context,\r
774 IN BOOT_SCRIPT_EXECUTOR_VARIABLE *EfiBootScriptExecutorVariable\r
775 )\r
776{\r
777 EFI_STATUS Status;\r
778 PEI_SMM_ACCESS_PPI *SmmAccess;\r
779 UINTN Index;\r
780 VOID *GuidHob;\r
781 IA32_DESCRIPTOR *IdtDescriptor;\r
782 VOID *IdtBuffer;\r
783 PEI_S3_RESUME_STATE *PeiS3ResumeState;\r
784 BOOLEAN InterruptStatus;\r
785\r
786 DEBUG ((DEBUG_INFO, "S3ResumeExecuteBootScript()\n"));\r
787\r
788 //\r
789 // Attempt to use content from SMRAM first\r
790 //\r
791 GuidHob = GetFirstGuidHob (&gEfiAcpiVariableGuid);\r
792 if (GuidHob != NULL) {\r
793 //\r
794 // Last step for SMM - send SMI for initialization\r
795 //\r
796\r
797 //\r
798 // Send SMI to APs\r
799 // \r
800 SendSmiIpiAllExcludingSelf ();\r
801 //\r
802 // Send SMI to BSP\r
803 //\r
804 SendSmiIpi (GetApicId ());\r
805\r
806 Status = PeiServicesLocatePpi (\r
807 &gPeiSmmAccessPpiGuid,\r
808 0,\r
809 NULL,\r
810 (VOID **) &SmmAccess\r
811 );\r
812 if (!EFI_ERROR (Status)) {\r
813 DEBUG ((DEBUG_INFO, "Close all SMRAM regions before executing boot script\n"));\r
814 \r
815 for (Index = 0, Status = EFI_SUCCESS; !EFI_ERROR (Status); Index++) {\r
816 Status = SmmAccess->Close ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), SmmAccess, Index);\r
817 }\r
818\r
819 DEBUG ((DEBUG_INFO, "Lock all SMRAM regions before executing boot script\n"));\r
820 \r
821 for (Index = 0, Status = EFI_SUCCESS; !EFI_ERROR (Status); Index++) {\r
822 Status = SmmAccess->Lock ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), SmmAccess, Index);\r
823 }\r
824 }\r
825 }\r
826\r
827 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
828 AsmWriteCr3 ((UINTN)AcpiS3Context->S3NvsPageTableAddress);\r
829 }\r
830\r
831 if (FeaturePcdGet (PcdFrameworkCompatibilitySupport)) {\r
832 //\r
833 // On some platform, such as ECP, a dispatch node in boot script table may execute a 32-bit PEIM which may need PeiServices\r
834 // pointer. So PeiServices need preserve in (IDTBase- sizeof (UINTN)). \r
835 //\r
836 IdtDescriptor = (IA32_DESCRIPTOR *) (UINTN) (AcpiS3Context->IdtrProfile);\r
837 //\r
838 // Make sure the newly allocated IDT align with 16-bytes\r
839 // \r
840 IdtBuffer = AllocatePages (EFI_SIZE_TO_PAGES((IdtDescriptor->Limit + 1) + 16));\r
841 if (IdtBuffer == NULL) {\r
842 REPORT_STATUS_CODE (\r
843 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
844 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_RESUME_FAILED)\r
845 );\r
846 ASSERT (FALSE);\r
847 }\r
848 //\r
849 // Additional 16 bytes allocated to save IA32 IDT descriptor and Pei Service Table Pointer\r
850 // IA32 IDT descriptor will be used to setup IA32 IDT table for 32-bit Framework Boot Script code\r
851 // \r
852 ZeroMem (IdtBuffer, 16);\r
853 AsmReadIdtr ((IA32_DESCRIPTOR *)IdtBuffer);\r
854 CopyMem ((VOID*)((UINT8*)IdtBuffer + 16),(VOID*)(IdtDescriptor->Base), (IdtDescriptor->Limit + 1));\r
855 IdtDescriptor->Base = (UINTN)((UINT8*)IdtBuffer + 16);\r
856 *(UINTN*)(IdtDescriptor->Base - sizeof(UINTN)) = (UINTN)GetPeiServicesTablePointer ();\r
857 }\r
858\r
859 InterruptStatus = SaveAndDisableInterrupts ();\r
860 //\r
861 // Need to make sure the GDT is loaded with values that support long mode and real mode.\r
862 //\r
863 AsmWriteGdtr (&mGdt);\r
864 //\r
865 // update segment selectors per the new GDT.\r
866 //\r
867 AsmSetDataSelectors (DATA_SEGEMENT_SELECTOR);\r
868 //\r
869 // Restore interrupt state.\r
870 //\r
871 SetInterruptState (InterruptStatus);\r
872\r
873 //\r
874 // Prepare data for return back\r
875 //\r
876 PeiS3ResumeState = AllocatePool (sizeof(*PeiS3ResumeState));\r
877 if (PeiS3ResumeState == NULL) {\r
878 REPORT_STATUS_CODE (\r
879 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
880 (EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_EC_S3_RESUME_FAILED)\r
881 );\r
882 ASSERT (FALSE);\r
883 }\r
884 DEBUG ((DEBUG_INFO, "PeiS3ResumeState - %x\r\n", PeiS3ResumeState));\r
885 PeiS3ResumeState->ReturnCs = 0x10;\r
886 PeiS3ResumeState->ReturnEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)S3ResumeBootOs;\r
887 PeiS3ResumeState->ReturnStackPointer = (EFI_PHYSICAL_ADDRESS)STACK_ALIGN_DOWN (&Status);\r
888 //\r
889 // Save IDT\r
890 //\r
891 AsmReadIdtr (&PeiS3ResumeState->Idtr);\r
892 \r
893 //\r
894 // Report Status Code to indicate S3 boot script execution\r
895 //\r
896 REPORT_STATUS_CODE (EFI_PROGRESS_CODE, EFI_SOFTWARE_PEI_MODULE | EFI_SW_PEI_PC_S3_BOOT_SCRIPT);\r
897\r
898 PERF_START (NULL, "ScriptExec", NULL, 0);\r
899\r
900 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
901 //\r
902 // X64 S3 Resume\r
903 //\r
904 DEBUG ((DEBUG_INFO, "Enable X64 and transfer control to Standalone Boot Script Executor\r\n"));\r
905\r
906 //\r
907 // Switch to long mode to complete resume.\r
908 //\r
909 AsmEnablePaging64 (\r
910 0x38,\r
911 EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint,\r
912 (UINT64)(UINTN)AcpiS3Context,\r
913 (UINT64)(UINTN)PeiS3ResumeState,\r
914 (UINT64)(UINTN)(AcpiS3Context->BootScriptStackBase + AcpiS3Context->BootScriptStackSize)\r
915 );\r
916 } else {\r
917 //\r
918 // IA32 S3 Resume\r
919 //\r
920 DEBUG ((DEBUG_INFO, "transfer control to Standalone Boot Script Executor\r\n"));\r
921 SwitchStack (\r
922 (SWITCH_STACK_ENTRY_POINT) (UINTN) EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint,\r
923 (VOID *)AcpiS3Context,\r
924 (VOID *)PeiS3ResumeState,\r
925 (VOID *)(UINTN)(AcpiS3Context->BootScriptStackBase + AcpiS3Context->BootScriptStackSize)\r
926 );\r
927 }\r
928\r
929 //\r
930 // Never run to here\r
931 //\r
932 CpuDeadLoop();\r
933}\r
934/**\r
935 Restores the platform to its preboot configuration for an S3 resume and\r
936 jumps to the OS waking vector.\r
937\r
938 This function will restore the platform to its pre-boot configuration that was\r
939 pre-stored in the boot script table and transfer control to OS waking vector.\r
940 Upon invocation, this function is responsible for locating the following\r
941 information before jumping to OS waking vector:\r
942 - ACPI tables\r
943 - boot script table\r
944 - any other information that it needs\r
945\r
946 The S3RestoreConfig() function then executes the pre-stored boot script table\r
947 and transitions the platform to the pre-boot state. The boot script is recorded\r
948 during regular boot using the EFI_S3_SAVE_STATE_PROTOCOL.Write() and\r
949 EFI_S3_SMM_SAVE_STATE_PROTOCOL.Write() functions. Finally, this function\r
950 transfers control to the OS waking vector. If the OS supports only a real-mode\r
951 waking vector, this function will switch from flat mode to real mode before\r
952 jumping to the waking vector. If all platform pre-boot configurations are\r
953 successfully restored and all other necessary information is ready, this\r
954 function will never return and instead will directly jump to the OS waking\r
955 vector. If this function returns, it indicates that the attempt to resume\r
956 from the ACPI S3 sleep state failed.\r
957\r
958 @param[in] This Pointer to this instance of the PEI_S3_RESUME_PPI\r
959\r
960 @retval EFI_ABORTED Execution of the S3 resume boot script table failed.\r
961 @retval EFI_NOT_FOUND Some necessary information that is used for the S3\r
962 resume boot path could not be located.\r
963\r
964**/\r
965EFI_STATUS\r
966EFIAPI\r
967S3RestoreConfig2 (\r
968 IN EFI_PEI_S3_RESUME2_PPI *This\r
969 )\r
970{\r
971 EFI_STATUS Status;\r
972 PEI_SMM_ACCESS_PPI *SmmAccess;\r
973 UINTN Index;\r
974 ACPI_S3_CONTEXT *AcpiS3Context;\r
975 EFI_PHYSICAL_ADDRESS TempEfiBootScriptExecutorVariable;\r
976 EFI_PHYSICAL_ADDRESS TempAcpiS3Context;\r
977 BOOT_SCRIPT_EXECUTOR_VARIABLE *EfiBootScriptExecutorVariable;\r
978 UINTN VarSize;\r
979 EFI_SMRAM_DESCRIPTOR *SmramDescriptor;\r
980 SMM_S3_RESUME_STATE *SmmS3ResumeState;\r
981 VOID *GuidHob;\r
982 BOOLEAN Build4GPageTableOnly;\r
983 BOOLEAN InterruptStatus;\r
984\r
985 TempAcpiS3Context = 0;\r
986 TempEfiBootScriptExecutorVariable = 0;\r
987\r
988 DEBUG ((DEBUG_INFO, "Enter S3 PEIM\r\n"));\r
989\r
990 VarSize = sizeof (EFI_PHYSICAL_ADDRESS);\r
991 Status = RestoreLockBox (\r
992 &gEfiAcpiVariableGuid,\r
993 &TempAcpiS3Context,\r
994 &VarSize\r
995 );\r
996 ASSERT_EFI_ERROR (Status);\r
997\r
998 Status = RestoreLockBox (\r
999 &gEfiAcpiS3ContextGuid,\r
1000 NULL,\r
1001 NULL\r
1002 );\r
1003 ASSERT_EFI_ERROR (Status);\r
1004\r
1005 AcpiS3Context = (ACPI_S3_CONTEXT *)(UINTN)TempAcpiS3Context;\r
1006 ASSERT (AcpiS3Context != NULL);\r
1007\r
1008 VarSize = sizeof (EFI_PHYSICAL_ADDRESS);\r
1009 Status = RestoreLockBox (\r
1010 &gEfiBootScriptExecutorVariableGuid,\r
1011 &TempEfiBootScriptExecutorVariable,\r
1012 &VarSize\r
1013 );\r
1014 ASSERT_EFI_ERROR (Status);\r
1015\r
1016 Status = RestoreLockBox (\r
1017 &gEfiBootScriptExecutorContextGuid,\r
1018 NULL,\r
1019 NULL\r
1020 );\r
1021 ASSERT_EFI_ERROR (Status);\r
1022\r
1023 EfiBootScriptExecutorVariable = (BOOT_SCRIPT_EXECUTOR_VARIABLE *) (UINTN) TempEfiBootScriptExecutorVariable;\r
1024 ASSERT (EfiBootScriptExecutorVariable != NULL);\r
1025\r
1026 DEBUG (( DEBUG_INFO, "AcpiS3Context = %x\n", AcpiS3Context));\r
1027 DEBUG (( DEBUG_INFO, "Waking Vector = %x\n", ((EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) (AcpiS3Context->AcpiFacsTable)))->FirmwareWakingVector));\r
1028 DEBUG (( DEBUG_INFO, "AcpiS3Context->AcpiFacsTable = %x\n", AcpiS3Context->AcpiFacsTable));\r
1029 DEBUG (( DEBUG_INFO, "AcpiS3Context->IdtrProfile = %x\n", AcpiS3Context->IdtrProfile)); \r
1030 DEBUG (( DEBUG_INFO, "AcpiS3Context->S3NvsPageTableAddress = %x\n", AcpiS3Context->S3NvsPageTableAddress));\r
1031 DEBUG (( DEBUG_INFO, "AcpiS3Context->S3DebugBufferAddress = %x\n", AcpiS3Context->S3DebugBufferAddress));\r
1032 DEBUG (( DEBUG_INFO, "AcpiS3Context->BootScriptStackBase = %x\n", AcpiS3Context->BootScriptStackBase));\r
1033 DEBUG (( DEBUG_INFO, "AcpiS3Context->BootScriptStackSize = %x\n", AcpiS3Context->BootScriptStackSize));\r
1034 DEBUG (( DEBUG_INFO, "EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint = %x\n", EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint));\r
1035\r
1036 //\r
1037 // Additional step for BootScript integrity - we only handle BootScript and BootScriptExecutor.\r
1038 // Script dispatch image and context (parameter) are handled by platform.\r
1039 // We just use restore all lock box in place, no need restore one by one.\r
1040 //\r
1041 Status = RestoreAllLockBoxInPlace ();\r
1042 ASSERT_EFI_ERROR (Status);\r
1043 if (EFI_ERROR (Status)) {\r
1044 // Something wrong\r
1045 CpuDeadLoop ();\r
1046 }\r
1047\r
1048 if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {\r
1049 //\r
1050 // Need reconstruct page table here, since we do not trust ACPINvs.\r
1051 //\r
1052 if (IsLongModeWakingVector (AcpiS3Context)) {\r
1053 Build4GPageTableOnly = FALSE;\r
1054 } else {\r
1055 Build4GPageTableOnly = TRUE;\r
1056 }\r
1057 RestoreS3PageTables ((UINTN)AcpiS3Context->S3NvsPageTableAddress, Build4GPageTableOnly);\r
1058 }\r
1059\r
1060 //\r
1061 // Attempt to use content from SMRAM first\r
1062 //\r
1063 GuidHob = GetFirstGuidHob (&gEfiAcpiVariableGuid);\r
1064 if (GuidHob != NULL) {\r
1065 Status = PeiServicesLocatePpi (\r
1066 &gPeiSmmAccessPpiGuid,\r
1067 0,\r
1068 NULL,\r
1069 (VOID **) &SmmAccess\r
1070 );\r
1071 for (Index = 0; !EFI_ERROR (Status); Index++) {\r
1072 Status = SmmAccess->Open ((EFI_PEI_SERVICES **)GetPeiServicesTablePointer (), SmmAccess, Index);\r
1073 }\r
1074\r
1075 SmramDescriptor = (EFI_SMRAM_DESCRIPTOR *) GET_GUID_HOB_DATA (GuidHob);\r
1076 SmmS3ResumeState = (SMM_S3_RESUME_STATE *)(UINTN)SmramDescriptor->CpuStart;\r
1077\r
1078 SmmS3ResumeState->ReturnCs = AsmReadCs ();\r
1079 SmmS3ResumeState->ReturnEntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)S3ResumeExecuteBootScript;\r
1080 SmmS3ResumeState->ReturnContext1 = (EFI_PHYSICAL_ADDRESS)(UINTN)AcpiS3Context;\r
1081 SmmS3ResumeState->ReturnContext2 = (EFI_PHYSICAL_ADDRESS)(UINTN)EfiBootScriptExecutorVariable;\r
1082 SmmS3ResumeState->ReturnStackPointer = (EFI_PHYSICAL_ADDRESS)STACK_ALIGN_DOWN (&Status);\r
1083\r
1084 DEBUG (( DEBUG_INFO, "SMM S3 Signature = %x\n", SmmS3ResumeState->Signature));\r
1085 DEBUG (( DEBUG_INFO, "SMM S3 Stack Base = %x\n", SmmS3ResumeState->SmmS3StackBase));\r
1086 DEBUG (( DEBUG_INFO, "SMM S3 Stack Size = %x\n", SmmS3ResumeState->SmmS3StackSize));\r
1087 DEBUG (( DEBUG_INFO, "SMM S3 Resume Entry Point = %x\n", SmmS3ResumeState->SmmS3ResumeEntryPoint));\r
1088 DEBUG (( DEBUG_INFO, "SMM S3 CR0 = %x\n", SmmS3ResumeState->SmmS3Cr0));\r
1089 DEBUG (( DEBUG_INFO, "SMM S3 CR3 = %x\n", SmmS3ResumeState->SmmS3Cr3));\r
1090 DEBUG (( DEBUG_INFO, "SMM S3 CR4 = %x\n", SmmS3ResumeState->SmmS3Cr4));\r
1091 DEBUG (( DEBUG_INFO, "SMM S3 Return CS = %x\n", SmmS3ResumeState->ReturnCs));\r
1092 DEBUG (( DEBUG_INFO, "SMM S3 Return Entry Point = %x\n", SmmS3ResumeState->ReturnEntryPoint));\r
1093 DEBUG (( DEBUG_INFO, "SMM S3 Return Context1 = %x\n", SmmS3ResumeState->ReturnContext1));\r
1094 DEBUG (( DEBUG_INFO, "SMM S3 Return Context2 = %x\n", SmmS3ResumeState->ReturnContext2));\r
1095 DEBUG (( DEBUG_INFO, "SMM S3 Return Stack Pointer = %x\n", SmmS3ResumeState->ReturnStackPointer));\r
1096 DEBUG (( DEBUG_INFO, "SMM S3 Smst = %x\n", SmmS3ResumeState->Smst));\r
1097\r
1098 if (SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_32) {\r
1099 SwitchStack (\r
1100 (SWITCH_STACK_ENTRY_POINT)(UINTN)SmmS3ResumeState->SmmS3ResumeEntryPoint,\r
1101 (VOID *)AcpiS3Context,\r
1102 0,\r
1103 (VOID *)(UINTN)(SmmS3ResumeState->SmmS3StackBase + SmmS3ResumeState->SmmS3StackSize)\r
1104 );\r
1105 }\r
1106 if (SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) {\r
1107 //\r
1108 // Switch to long mode to complete resume.\r
1109 //\r
1110\r
1111 InterruptStatus = SaveAndDisableInterrupts ();\r
1112 //\r
1113 // Need to make sure the GDT is loaded with values that support long mode and real mode.\r
1114 //\r
1115 AsmWriteGdtr (&mGdt);\r
1116 //\r
1117 // update segment selectors per the new GDT.\r
1118 // \r
1119 AsmSetDataSelectors (DATA_SEGEMENT_SELECTOR);\r
1120 //\r
1121 // Restore interrupt state.\r
1122 //\r
1123 SetInterruptState (InterruptStatus);\r
1124\r
1125 AsmWriteCr3 ((UINTN)SmmS3ResumeState->SmmS3Cr3);\r
1126\r
1127 //\r
1128 // Disable interrupt of Debug timer, since IDT table cannot work in long mode.\r
1129 // NOTE: On x64 platforms, because DisablePaging64() will disable interrupts,\r
1130 // the code in S3ResumeExecuteBootScript() cannot be halted by soft debugger.\r
1131 //\r
1132 SaveAndSetDebugTimerInterrupt (FALSE);\r
1133\r
1134 AsmEnablePaging64 (\r
1135 0x38,\r
1136 SmmS3ResumeState->SmmS3ResumeEntryPoint,\r
1137 (UINT64)(UINTN)AcpiS3Context,\r
1138 0,\r
1139 SmmS3ResumeState->SmmS3StackBase + SmmS3ResumeState->SmmS3StackSize\r
1140 );\r
1141 }\r
1142\r
1143 }\r
1144\r
1145 S3ResumeExecuteBootScript (AcpiS3Context, EfiBootScriptExecutorVariable );\r
1146 return EFI_SUCCESS;\r
1147}\r
1148/**\r
1149 Main entry for S3 Resume PEIM.\r
1150\r
1151 This routine is to install EFI_PEI_S3_RESUME2_PPI.\r
1152 \r
1153 @param FileHandle Handle of the file being invoked.\r
1154 @param PeiServices Pointer to PEI Services table.\r
1155\r
1156 @retval EFI_SUCCESS S3Resume Ppi is installed successfully.\r
1157\r
1158**/\r
1159EFI_STATUS\r
1160EFIAPI\r
1161PeimS3ResumeEntryPoint (\r
1162 IN EFI_PEI_FILE_HANDLE FileHandle,\r
1163 IN CONST EFI_PEI_SERVICES **PeiServices\r
1164 )\r
1165{\r
1166 EFI_STATUS Status;\r
1167\r
1168 //\r
1169 // Install S3 Resume Ppi\r
1170 //\r
1171 Status = (**PeiServices).InstallPpi (PeiServices, &mPpiList);\r
1172 ASSERT_EFI_ERROR (Status);\r
1173\r
1174 return EFI_SUCCESS;\r
1175}\r
1176\r