S3Resume.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/** @file
This module produces the EFI_PEI_S3_RESUME_PPI.
This module works with StandAloneBootScriptExecutor to S3 resume to OS.
This module will excute the boot script saved during last boot and after that,
control is passed to OS waking up handler.
Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions
of the BSD License which accompanies this distribution. The
full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <PiPei.h>
#include <Guid/AcpiS3Context.h>
#include <Guid/BootScriptExecutorVariable.h>
#include <Guid/Performance.h>
#include <Ppi/ReadOnlyVariable2.h>
#include <Ppi/S3Resume2.h>
#include <Ppi/SmmAccess.h>
#include <Ppi/PostBootScriptTable.h>
#include <Ppi/EndOfPeiPhase.h>
#include <Library/DebugLib.h>
#include <Library/TimerLib.h>
#include <Library/PeimEntryPoint.h>
#include <Library/PeiServicesLib.h>
#include <Library/PerformanceLib.h>
#include <Library/PeiServicesTablePointerLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/DebugAgentLib.h>
#include <Library/LocalApicLib.h>
#include <Library/ReportStatusCodeLib.h>
#include <Library/PrintLib.h>
#include <Library/LockBoxLib.h>
#include <IndustryStandard/Acpi.h>
#pragma pack(1)
typedef union {
struct {
} Bits;
} IA32_GDT;
//
// Page-Map Level-4 Offset (PML4) and
// Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB
//
typedef union {
struct {
} Bits;
//
// Page Table Entry 2MB
//
typedef union {
struct {
} Bits;
//
// Page Table Entry 1GB
//
typedef union {
struct {
} Bits;
#pragma pack()
//
// Function prototypes
//
/**
a ASM function to transfer control to OS.
@param S3WakingVector The S3 waking up vector saved in ACPI Facs table
@param AcpiLowMemoryBase a buffer under 1M which could be used during the transfer
**/
typedef
);
/**
Restores the platform to its preboot configuration for an S3 resume and
jumps to the OS waking vector.
This function will restore the platform to its pre-boot configuration that was
pre-stored in the boot script table and transfer control to OS waking vector.
Upon invocation, this function is responsible for locating the following
information before jumping to OS waking vector:
- ACPI tables
- boot script table
- any other information that it needs
The S3RestoreConfig() function then executes the pre-stored boot script table
and transitions the platform to the pre-boot state. The boot script is recorded
during regular boot using the EFI_S3_SAVE_STATE_PROTOCOL.Write() and
EFI_S3_SMM_SAVE_STATE_PROTOCOL.Write() functions. Finally, this function
transfers control to the OS waking vector. If the OS supports only a real-mode
waking vector, this function will switch from flat mode to real mode before
jumping to the waking vector. If all platform pre-boot configurations are
successfully restored and all other necessary information is ready, this
function will never return and instead will directly jump to the OS waking
vector. If this function returns, it indicates that the attempt to resume
from the ACPI S3 sleep state failed.
@param[in] This Pointer to this instance of the PEI_S3_RESUME_PPI
@retval EFI_ABORTED Execution of the S3 resume boot script table failed.
@retval EFI_NOT_FOUND Some necessary information that is used for the S3
resume boot path could not be located.
**/
);
//
// Globals
//
};
0
};
0
};
//
// Global Descriptor Table (GDT)
//
/* selector { Global Segment Descriptor } */
/* 0x00 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* 0x08 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* 0x10 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}},
/* 0x18 */ {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 1, 1, 0}},
/* 0x20 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
/* 0x28 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 0, 1, 0}},
/* 0x30 */ {{0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 0, 1, 0}},
/* 0x38 */ {{0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 1, 0, 1, 0}},
/* 0x40 */ {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
};
//
// IA32 Gdt register
//
sizeof (mGdtEntries) - 1,
};
/**
Performance measure function to get S3 detailed performance data.
This function will getS3 detailed performance data and saved in pre-reserved ACPI memory.
**/
)
{
//
// Retrive time stamp count as early as possilbe
//
Ticker = GetPerformanceCounter ();
0,
NULL,
(VOID **) &VariableServices
);
VarSize = sizeof (EFI_PHYSICAL_ADDRESS);
L"PerfDataMemAddr",
NULL,
&VarSize,
);
return;
}
return;
}
//
// Record total S3 resume time.
//
if (EndValue >= StartValue) {
} else {
}
//
// Get S3 detailed performance data
//
Index = 0;
LogEntryKey = 0;
while ((LogEntryKey = GetPerformanceMeasurement (
&Handle,
&Token,
&Module,
&EndTicker)) != 0) {
if (EndTicker != 0) {
//
// Use File Handle to specify the different performance log for PEIM.
// File Handle is the base address of PEIM FFS file.
//
} else {
}
if (StartTicker == 1) {
}
if (EndTicker == 1) {
}
//
// Only Record > 1ms performance data so that more big performance can be recorded.
//
//
// Reach the maximum number of PEI performance log entries.
//
break;
}
}
}
}
/**
Jump to OS waking vector.
The function will install boot script done PPI, report S3 resume status code, and then jump to OS waking vector.
@param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
@param PeiS3ResumeState a pointer to a structure of PEI_S3_RESUME_STATE
**/
)
{
//
// Restore IDT
//
//
// Install BootScriptDonePpi
//
//
// Get ACPI Table Address
//
CpuDeadLoop ();
return ;
}
//
// report status code on S3 resume
//
//
// Install EndOfPeiPpi
//
);
if (Facs->XFirmwareWakingVector != 0) {
//
// Switch to native waking vector
//
//
// X64 long mode waking vector
//
DEBUG (( EFI_D_ERROR, "Transfer to 64bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));
if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
0x38,
0,
0,
);
} else {
}
} else {
//
// IA32 protected mode waking vector (Page disabled)
//
DEBUG (( EFI_D_ERROR, "Transfer to 32bit OS waking vector - %x\r\n", (UINTN)Facs->XFirmwareWakingVector));
NULL,
NULL,
);
}
} else {
//
// 16bit Realmode waking vector
//
DEBUG (( EFI_D_ERROR, "Transfer to 16bit OS waking vector - %x\r\n", (UINTN)Facs->FirmwareWakingVector));
}
//
// Never run to here
//
CpuDeadLoop();
}
/**
Restore S3 page table because we do not trust ACPINvs content.
If BootScriptExector driver will not run in 64-bit mode, this function will do nothing.
@param S3NvsPageTableAddress PageTableAddress in ACPINvs
**/
)
{
if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
//
// NOTE: We have to ASSUME the page table generation format, because we do not know whole page table information.
// The whole page table is too large to be saved in SMRAM.
//
// The assumption is : whole page table is allocated in CONTINOUS memory and CR3 points to TOP page.
//
//
// By architecture only one PageMapLevel4 exists - so lets allocate storgage for it.
//
if (PcdGetBool(PcdUse1GPageTable)) {
if (RegEax >= 0x80000001) {
}
}
}
//
// Get physical address bits supported.
//
} else {
if (RegEax >= 0x80000008) {
} else {
PhysicalAddressBits = 36;
}
}
//
// IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses.
//
if (PhysicalAddressBits > 48) {
PhysicalAddressBits = 48;
}
//
// Calculate the table entries needed.
//
if (PhysicalAddressBits <= 39) {
} else {
NumberOfPdpEntriesNeeded = 512;
}
PageAddress = 0;
for (IndexOfPml4Entries = 0; IndexOfPml4Entries < NumberOfPml4EntriesNeeded; IndexOfPml4Entries++, PageMapLevel4Entry++) {
//
// Each PML4 entry points to a page of Page Directory Pointer entires.
// So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
//
//
// Make a PML4 Entry
//
if (Page1GSupport) {
for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {
//
// Fill in the Page Directory entries
//
}
} else {
for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
//
// Each Directory Pointer entries points to a page of Page Directory entires.
// So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
//
//
// Fill in a Page Directory Pointer Entries
//
for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {
//
// Fill in the Page Directory entries
//
}
}
}
}
return ;
} else {
//
// If DXE is running 32-bit mode, no need to establish page table.
//
return ;
}
}
/**
Jump to boot script executor driver.
The function will close and lock SMRAM and then jump to boot script execute driver to executing S3 boot script table.
@param AcpiS3Context a pointer to a structure of ACPI_S3_CONTEXT
@param EfiBootScriptExecutorVariable The function entry to executing S3 boot Script table. This function is build in
boot script execute driver
**/
)
{
//
// Attempt to use content from SMRAM first
//
//
// Last step for SMM - send SMI for initialization
//
//
// Send SMI to APs
//
//
// Send SMI to BSP
//
SendSmiIpi (GetApicId ());
0,
NULL,
);
}
}
}
if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
}
//
// On some platform, such as ECP, a dispatch node in boot script table may execute a 32-bit PEIM which may need PeiServices
// pointer. So PeiServices need preserve in (IDTBase- sizeof (UINTN)).
//
//
// Make sure the newly allcated IDT align with 16-bytes
//
}
//
// Need to make sure the GDT is loaded with values that support long mode and real mode.
//
AsmWriteGdtr (&mGdt);
//
// Prepare data for return back
//
//
// Save IDT
//
if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
//
// X64 S3 Resume
//
//
// Switch to long mode to complete resume.
//
0x38,
);
} else {
//
// IA32 S3 Resume
//
(VOID *)AcpiS3Context,
(VOID *)PeiS3ResumeState,
);
}
//
// Never run to here
//
CpuDeadLoop();
}
/**
Restores the platform to its preboot configuration for an S3 resume and
jumps to the OS waking vector.
This function will restore the platform to its pre-boot configuration that was
pre-stored in the boot script table and transfer control to OS waking vector.
Upon invocation, this function is responsible for locating the following
information before jumping to OS waking vector:
- ACPI tables
- boot script table
- any other information that it needs
The S3RestoreConfig() function then executes the pre-stored boot script table
and transitions the platform to the pre-boot state. The boot script is recorded
during regular boot using the EFI_S3_SAVE_STATE_PROTOCOL.Write() and
EFI_S3_SMM_SAVE_STATE_PROTOCOL.Write() functions. Finally, this function
transfers control to the OS waking vector. If the OS supports only a real-mode
waking vector, this function will switch from flat mode to real mode before
jumping to the waking vector. If all platform pre-boot configurations are
successfully restored and all other necessary information is ready, this
function will never return and instead will directly jump to the OS waking
vector. If this function returns, it indicates that the attempt to resume
from the ACPI S3 sleep state failed.
@param[in] This Pointer to this instance of the PEI_S3_RESUME_PPI
@retval EFI_ABORTED Execution of the S3 resume boot script table failed.
@retval EFI_NOT_FOUND Some necessary information that is used for the S3
resume boot path could not be located.
**/
)
{
0,
NULL,
);
}
0,
NULL,
(VOID **) &VariableServices
);
return Status;
}
VarSize = sizeof (EFI_PHYSICAL_ADDRESS);
Status = RestoreLockBox (
);
Status = RestoreLockBox (
NULL,
);
VarSize = sizeof (TempEfiBootScriptExecutorVariable);
Status = RestoreLockBox (
);
Status = RestoreLockBox (
NULL,
);
EfiBootScriptExecutorVariable = (BOOT_SCRIPT_EXECUTOR_VARIABLE *) (UINTN) TempEfiBootScriptExecutorVariable;
DEBUG (( EFI_D_ERROR, "Waking Vector = %x\n", ((EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) ((UINTN) (AcpiS3Context->AcpiFacsTable)))->FirmwareWakingVector));
DEBUG (( EFI_D_ERROR, "AcpiS3Context->S3NvsPageTableAddress = %x\n", AcpiS3Context->S3NvsPageTableAddress));
DEBUG (( EFI_D_ERROR, "AcpiS3Context->S3DebugBufferAddress = %x\n", AcpiS3Context->S3DebugBufferAddress));
DEBUG (( EFI_D_ERROR, "EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint = %x\n", EfiBootScriptExecutorVariable->BootScriptExecutorEntrypoint));
//
// Additional step for BootScript integrity - we only handle BootScript and BootScriptExecutor.
// Script dispatch image and context (parameter) are handled by platform.
// We just use restore all lock box in place, no need restore one by one.
//
// Something wrong
CpuDeadLoop ();
}
if (FeaturePcdGet (PcdDxeIplSwitchToLongMode)) {
//
// Need reconstruct page table here, since we do not trust ACPINvs.
//
}
//
// Attempt to use content from SMRAM first
//
DEBUG (( EFI_D_ERROR, "SMM S3 Resume Entry Point = %x\n", SmmS3ResumeState->SmmS3ResumeEntryPoint));
//
// Disable interrupt of Debug timer.
//
(VOID *)AcpiS3Context,
0,
);
}
//
// Switch to long mode to complete resume.
//
//
// Need to make sure the GDT is loaded with values that support long mode and real mode.
//
AsmWriteGdtr (&mGdt);
0x38,
0,
);
}
}
return EFI_SUCCESS;
}
/**
Main entry for S3 Resume PEIM.
This routine is to install EFI_PEI_S3_RESUME2_PPI.
@param FileHandle Handle of the file being invoked.
@param PeiServices Pointer to PEI Services table.
@retval EFI_SUCCESS S3Resume Ppi is installed successfully.
**/
)
{
//
// Install S3 Resume Ppi
//
return EFI_SUCCESS;
}