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