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