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