This driver is the counterpart of the SMM Base On SMM Base2 Thunk driver. It\r
provides helping services in SMM to the SMM Base On SMM Base2 Thunk driver.\r
\r
- Copyright (c) 2009 - 2010, Intel Corporation\r
- All rights reserved. This program and the accompanying materials\r
+ Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
http://opensource.org/licenses/bsd-license.php\r
{EFI_SMM_SAVE_STATE_REGISTER_CR3 , CPU_SAVE_STATE_GET_OFFSET(CR3)}\r
};\r
\r
+/**\r
+ Page fault handler.\r
+\r
+**/\r
VOID\r
PageFaultHandlerHook (\r
VOID\r
);\r
\r
+/**\r
+ Read CpuSaveStates from PI for Framework use.\r
+\r
+ The function reads PI style CpuSaveStates of CpuIndex-th CPU for Framework driver use. If\r
+ ToRead is specified, the CpuSaveStates will be copied to ToRead, otherwise copied to\r
+ mFrameworkSmst->CpuSaveState[CpuIndex].\r
+\r
+ @param[in] CpuIndex The zero-based CPU index.\r
+ @param[in, out] ToRead If not NULL, CpuSaveStates will be copied to it.\r
+\r
+**/\r
VOID\r
ReadCpuSaveState (\r
- UINTN CpuIndex,\r
- EFI_SMM_CPU_SAVE_STATE *ToRead\r
+ IN UINTN CpuIndex,\r
+ IN OUT EFI_SMM_CPU_SAVE_STATE *ToRead\r
)\r
{\r
EFI_STATUS Status;\r
}\r
}\r
\r
+/**\r
+ Write CpuSaveStates from Framework into PI.\r
+\r
+ The function writes back CpuSaveStates of CpuIndex-th CPU from PI to Framework. If\r
+ ToWrite is specified, it contains the CpuSaveStates to write from, otherwise CpuSaveStates\r
+ to write from mFrameworkSmst->CpuSaveState[CpuIndex].\r
+\r
+ @param[in] CpuIndex The zero-based CPU index.\r
+ @param[in] ToWrite If not NULL, CpuSaveStates to write from.\r
+\r
+**/\r
VOID\r
WriteCpuSaveState (\r
- UINTN CpuIndex,\r
- EFI_SMM_CPU_SAVE_STATE *ToWrite\r
+ IN UINTN CpuIndex,\r
+ IN EFI_SMM_CPU_SAVE_STATE *ToWrite\r
)\r
{\r
EFI_STATUS Status;\r
}\r
}\r
\r
+/**\r
+ Read or write a page that contains CpuSaveStates. Read is from PI to Framework.\r
+ Write is from Framework to PI.\r
+\r
+ This function reads or writes a page that contains CpuSaveStates. The page contains Framework\r
+ CpuSaveStates. On read, it reads PI style CpuSaveStates and fill the page up. On write, it\r
+ writes back from the page content to PI CpuSaveStates struct.\r
+ The first Framework CpuSaveStates (for CPU 0) is from mFrameworkSmst->CpuSaveState which is\r
+ page aligned. Because Framework CpuSaveStates are continuous, we can know which CPUs' SaveStates\r
+ are in the page start from PageAddress.\r
+\r
+ @param[in] PageAddress The base address for a page.\r
+ @param[in] IsRead TRUE for Read, FALSE for Write.\r
+\r
+**/\r
VOID\r
ReadWriteCpuStatePage (\r
- UINT64 PageAddress,\r
- BOOLEAN IsRead\r
+ IN UINT64 PageAddress,\r
+ IN BOOLEAN IsRead\r
)\r
{\r
UINTN FirstSSIndex; // Index of first CpuSaveState in the page\r
}\r
}\r
\r
+/**\r
+ The page fault handler that on-demand read PI CpuSaveStates for framework use. If the fault\r
+ is not targeted to mFrameworkSmst->CpuSaveState range, the function will return FALSE to let\r
+ PageFaultHandlerHook know it needs to pass the fault over to original page fault handler.\r
+ \r
+ @retval TRUE The page fault is correctly handled.\r
+ @retval FALSE The page fault is not handled and is passed through to original handler.\r
+\r
+**/\r
BOOLEAN\r
PageFaultHandler (\r
VOID\r
return IsHandled;\r
}\r
\r
+/**\r
+ Write back the dirty Framework CpuSaveStates to PI.\r
+ \r
+ The function scans the page table for dirty pages in mFrameworkSmst->CpuSaveState\r
+ to write back to PI CpuSaveStates. It is meant to be called on each SmmBaseHelper SMI\r
+ callback after Framework handler is called.\r
+\r
+**/\r
VOID\r
WriteBackDirtyPages (\r
VOID\r
UINTN PTEndIndex;\r
\r
NumCpuStatePages = EFI_SIZE_TO_PAGES (mNumberOfProcessors * sizeof (EFI_SMM_CPU_SAVE_STATE));\r
- PTStartIndex = (UINTN)BitFieldRead64 ((UINT64)mFrameworkSmst->CpuSaveState, 12, 20);\r
- PTEndIndex = (UINTN)BitFieldRead64 ((UINT64)mFrameworkSmst->CpuSaveState + EFI_PAGES_TO_SIZE(NumCpuStatePages) - 1, 12, 20);\r
+ PTStartIndex = (UINTN)BitFieldRead64 ((UINT64) (UINTN) mFrameworkSmst->CpuSaveState, 12, 20);\r
+ PTEndIndex = (UINTN)BitFieldRead64 ((UINT64) (UINTN) mFrameworkSmst->CpuSaveState + EFI_PAGES_TO_SIZE(NumCpuStatePages) - 1, 12, 20);\r
for (PTIndex = PTStartIndex; PTIndex <= PTEndIndex; PTIndex++) {\r
if ((mCpuStatePageTable[PTIndex] & (BIT0|BIT6)) == (BIT0|BIT6)) { // present and dirty?\r
ReadWriteCpuStatePage (mCpuStatePageTable[PTIndex] & mPhyMask, FALSE);\r
}\r
}\r
\r
+/**\r
+ Hook IDT with our page fault handler so that the on-demand paging works on page fault.\r
+ \r
+ The function hooks the IDT with PageFaultHandlerHook to get on-demand paging work for\r
+ PI<->Framework CpuSaveStates marshalling. It also saves original handler for pass-through\r
+ purpose.\r
+\r
+**/\r
VOID\r
HookPageFaultHandler (\r
VOID\r
IdtGateDesc[14].Bits.OffsetHigh = (UINT32)(((UINTN)PageFaultHandlerHook >> 16) & ((1 << 16) - 1));\r
}\r
\r
+/**\r
+ Initialize page table for pages contain HookData.\r
+ \r
+ The function initialize PDE for 2MB range that contains HookData. If the related PDE points\r
+ to a 2MB page, a page table will be allocated and initialized for 4KB pages. Otherwise we juse\r
+ use the original page table.\r
+\r
+ @param[in] HookData Based on which to initialize page table.\r
+\r
+ @return The pointer to a Page Table that points to 4KB pages which contain HookData.\r
+**/\r
UINT64 *\r
InitCpuStatePageTable (\r
- VOID *HookData\r
+ IN VOID *HookData\r
)\r
{\r
UINTN Index;\r
UINT64 *PageTable;\r
- UINT64 *PDPTE;\r
+ UINT64 *Pdpte;\r
UINT64 HookAddress;\r
- UINT64 PDE;\r
+ UINT64 Pde;\r
UINT64 Address;\r
\r
//\r
PageTable = (UINT64 *)(UINTN)(PageTable[BitFieldRead64 (HookAddress, 39, 47)] & mPhyMask);\r
PageTable = (UINT64 *)(UINTN)(PageTable[BitFieldRead64 (HookAddress, 30, 38)] & mPhyMask);\r
\r
- PDPTE = (UINT64 *)(UINTN)PageTable;\r
- PDE = PDPTE[BitFieldRead64 (HookAddress, 21, 29)];\r
- ASSERT ((PDE & BIT0) != 0); // Present and 2M Page\r
+ Pdpte = (UINT64 *)(UINTN)PageTable;\r
+ Pde = Pdpte[BitFieldRead64 (HookAddress, 21, 29)];\r
+ ASSERT ((Pde & BIT0) != 0); // Present and 2M Page\r
\r
- if ((PDE & BIT7) == 0) { // 4KB Page Directory\r
- PageTable = (UINT64 *)(UINTN)(PDE & mPhyMask);\r
+ if ((Pde & BIT7) == 0) { // 4KB Page Directory\r
+ PageTable = (UINT64 *)(UINTN)(Pde & mPhyMask);\r
} else {\r
- ASSERT ((PDE & mPhyMask) == (HookAddress & ~(SIZE_2MB-1))); // 2MB Page Point to HookAddress\r
+ ASSERT ((Pde & mPhyMask) == (HookAddress & ~(SIZE_2MB-1))); // 2MB Page Point to HookAddress\r
PageTable = AllocatePages (1);\r
+ ASSERT (PageTable != NULL);\r
Address = HookAddress & ~(SIZE_2MB-1);\r
for (Index = 0; Index < 512; Index++) {\r
PageTable[Index] = Address | BIT0 | BIT1; // Present and RW\r
Address += SIZE_4KB;\r
}\r
- PDPTE[BitFieldRead64 (HookAddress, 21, 29)] = (UINT64)(UINTN)PageTable | BIT0 | BIT1; // Present and RW\r
+ Pdpte[BitFieldRead64 (HookAddress, 21, 29)] = (UINT64)(UINTN)PageTable | BIT0 | BIT1; // Present and RW\r
}\r
return PageTable;\r
}\r
\r
+/**\r
+ Mark all the CpuSaveStates as not present.\r
+ \r
+ The function marks all CpuSaveStates memory range as not present so that page fault can be triggered\r
+ on CpuSaveStates access. It is meant to be called on each SmmBaseHelper SMI callback before Framework\r
+ handler is called.\r
+\r
+ @param[in] CpuSaveState The base of CpuSaveStates.\r
+\r
+**/\r
VOID\r
HookCpuStateMemory (\r
- EFI_SMM_CPU_SAVE_STATE *CpuSaveState\r
+ IN EFI_SMM_CPU_SAVE_STATE *CpuSaveState\r
)\r
{\r
UINT64 Index;\r
return Status; \r
}\r
\r
+/**\r
+ Initialize all the stuff needed for on-demand paging hooks for PI<->Framework\r
+ CpuSaveStates marshalling.\r
+\r
+ @param[in] FrameworkSmst Framework SMM system table pointer.\r
+\r
+**/\r
VOID\r
InitHook (\r
- EFI_SMM_SYSTEM_TABLE *FrameworkSmst\r
+ IN EFI_SMM_SYSTEM_TABLE *FrameworkSmst\r
)\r
{\r
UINTN NumCpuStatePages;\r
if (!EFI_ERROR (Status)) {\r
///\r
/// Update MP state in Framework SMST before transferring control to Framework SMM driver entry point\r
- /// in case it may invoke AP\r
///\r
+ mFrameworkSmst->SmmStartupThisAp = gSmst->SmmStartupThisAp;\r
+ mFrameworkSmst->NumberOfCpus = mNumberOfProcessors;\r
mFrameworkSmst->CurrentlyExecutingCpu = gSmst->CurrentlyExecutingCpu;\r
\r
Status = gBS->StartImage (*ImageHandle, NULL, NULL);\r