2 SMM Base Helper SMM driver.
4 This driver is the counterpart of the SMM Base On SMM Base2 Thunk driver. It
5 provides helping services in SMM to the SMM Base On SMM Base2 Thunk driver.
7 Copyright (c) 2009 - 2010, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #include <Library/DebugLib.h>
20 #include <Library/UefiBootServicesTableLib.h>
21 #include <Library/SmmServicesTableLib.h>
22 #include <Library/BaseLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/PeCoffLib.h>
25 #include <Library/DevicePathLib.h>
26 #include <Library/CacheMaintenanceLib.h>
27 #include <Library/MemoryAllocationLib.h>
28 #include <Library/SynchronizationLib.h>
29 #include <Library/CpuLib.h>
30 #include <Guid/SmmBaseThunkCommunication.h>
31 #include <Protocol/SmmBaseHelperReady.h>
32 #include <Protocol/SmmCpu.h>
33 #include <Protocol/LoadedImage.h>
34 #include <Protocol/SmmCpuSaveState.h>
35 #include <Protocol/MpService.h>
36 #include <Protocol/LoadPe32Image.h>
37 #include <Protocol/SmmReadyToLock.h>
40 /// Structure for tracking paired information of registered Framework SMI handler
41 /// and correpsonding dispatch handle for SMI handler thunk.
45 EFI_HANDLE DispatchHandle
;
46 EFI_HANDLE SmmImageHandle
;
47 EFI_SMM_CALLBACK_ENTRY_POINT CallbackAddress
;
48 VOID
*CommunicationBuffer
;
54 /// PI SMM CPU Save State register index
56 EFI_SMM_SAVE_STATE_REGISTER Register
;
58 /// Offset in Framework SMST
61 } CPU_SAVE_STATE_CONVERSION
;
63 #define CPU_SAVE_STATE_GET_OFFSET(Field) (UINTN)(&(((EFI_SMM_CPU_SAVE_STATE *) 0)->Ia32SaveState.Field))
66 EFI_HANDLE mDispatchHandle
;
67 EFI_SMM_CPU_PROTOCOL
*mSmmCpu
;
68 EFI_PE32_IMAGE_PROTOCOL
*mLoadPe32Image
;
69 EFI_GUID mEfiSmmCpuIoGuid
= EFI_SMM_CPU_IO_GUID
;
70 EFI_SMM_BASE_HELPER_READY_PROTOCOL
*mSmmBaseHelperReady
;
71 EFI_SMM_SYSTEM_TABLE
*mFrameworkSmst
;
72 UINTN mNumberOfProcessors
;
73 BOOLEAN mLocked
= FALSE
;
74 BOOLEAN mPageTableHookEnabled
;
75 BOOLEAN mHookInitialized
;
76 UINT64
*mCpuStatePageTable
;
79 VOID
*mOriginalHandler
;
80 EFI_SMM_CPU_SAVE_STATE
*mShadowSaveState
;
82 LIST_ENTRY mCallbackInfoListHead
= INITIALIZE_LIST_HEAD_VARIABLE (mCallbackInfoListHead
);
84 CPU_SAVE_STATE_CONVERSION mCpuSaveStateConvTable
[] = {
85 {EFI_SMM_SAVE_STATE_REGISTER_LDTBASE
, CPU_SAVE_STATE_GET_OFFSET(LDTBase
)},
86 {EFI_SMM_SAVE_STATE_REGISTER_ES
, CPU_SAVE_STATE_GET_OFFSET(ES
)},
87 {EFI_SMM_SAVE_STATE_REGISTER_CS
, CPU_SAVE_STATE_GET_OFFSET(CS
)},
88 {EFI_SMM_SAVE_STATE_REGISTER_SS
, CPU_SAVE_STATE_GET_OFFSET(SS
)},
89 {EFI_SMM_SAVE_STATE_REGISTER_DS
, CPU_SAVE_STATE_GET_OFFSET(DS
)},
90 {EFI_SMM_SAVE_STATE_REGISTER_FS
, CPU_SAVE_STATE_GET_OFFSET(FS
)},
91 {EFI_SMM_SAVE_STATE_REGISTER_GS
, CPU_SAVE_STATE_GET_OFFSET(GS
)},
92 {EFI_SMM_SAVE_STATE_REGISTER_TR_SEL
, CPU_SAVE_STATE_GET_OFFSET(TR
)},
93 {EFI_SMM_SAVE_STATE_REGISTER_DR7
, CPU_SAVE_STATE_GET_OFFSET(DR7
)},
94 {EFI_SMM_SAVE_STATE_REGISTER_DR6
, CPU_SAVE_STATE_GET_OFFSET(DR6
)},
95 {EFI_SMM_SAVE_STATE_REGISTER_RAX
, CPU_SAVE_STATE_GET_OFFSET(EAX
)},
96 {EFI_SMM_SAVE_STATE_REGISTER_RBX
, CPU_SAVE_STATE_GET_OFFSET(EBX
)},
97 {EFI_SMM_SAVE_STATE_REGISTER_RCX
, CPU_SAVE_STATE_GET_OFFSET(ECX
)},
98 {EFI_SMM_SAVE_STATE_REGISTER_RDX
, CPU_SAVE_STATE_GET_OFFSET(EDX
)},
99 {EFI_SMM_SAVE_STATE_REGISTER_RSP
, CPU_SAVE_STATE_GET_OFFSET(ESP
)},
100 {EFI_SMM_SAVE_STATE_REGISTER_RBP
, CPU_SAVE_STATE_GET_OFFSET(EBP
)},
101 {EFI_SMM_SAVE_STATE_REGISTER_RSI
, CPU_SAVE_STATE_GET_OFFSET(ESI
)},
102 {EFI_SMM_SAVE_STATE_REGISTER_RDI
, CPU_SAVE_STATE_GET_OFFSET(EDI
)},
103 {EFI_SMM_SAVE_STATE_REGISTER_RIP
, CPU_SAVE_STATE_GET_OFFSET(EIP
)},
104 {EFI_SMM_SAVE_STATE_REGISTER_RFLAGS
, CPU_SAVE_STATE_GET_OFFSET(EFLAGS
)},
105 {EFI_SMM_SAVE_STATE_REGISTER_CR0
, CPU_SAVE_STATE_GET_OFFSET(CR0
)},
106 {EFI_SMM_SAVE_STATE_REGISTER_CR3
, CPU_SAVE_STATE_GET_OFFSET(CR3
)}
114 PageFaultHandlerHook (
119 Read CpuSaveStates from PI for Framework use.
121 The function reads PI style CpuSaveStates of CpuIndex-th CPU for Framework driver use. If
122 ToRead is specified, the CpuSaveStates will be copied to ToRead, otherwise copied to
123 mFrameworkSmst->CpuSaveState[CpuIndex].
125 @param[in] CpuIndex The zero-based CPU index.
126 @param[in, out] ToRead If not NULL, CpuSaveStates will be copied to it.
132 IN OUT EFI_SMM_CPU_SAVE_STATE
*ToRead
137 EFI_SMM_CPU_STATE
*State
;
138 EFI_SMI_CPU_SAVE_STATE
*SaveState
;
140 State
= (EFI_SMM_CPU_STATE
*)gSmst
->CpuSaveState
[CpuIndex
];
141 if (ToRead
!= NULL
) {
142 SaveState
= &ToRead
->Ia32SaveState
;
144 SaveState
= &mFrameworkSmst
->CpuSaveState
[CpuIndex
].Ia32SaveState
;
147 if (State
->x86
.SMMRevId
< EFI_SMM_MIN_REV_ID_x64
) {
148 SaveState
->SMBASE
= State
->x86
.SMBASE
;
149 SaveState
->SMMRevId
= State
->x86
.SMMRevId
;
150 SaveState
->IORestart
= State
->x86
.IORestart
;
151 SaveState
->AutoHALTRestart
= State
->x86
.AutoHALTRestart
;
153 SaveState
->SMBASE
= State
->x64
.SMBASE
;
154 SaveState
->SMMRevId
= State
->x64
.SMMRevId
;
155 SaveState
->IORestart
= State
->x64
.IORestart
;
156 SaveState
->AutoHALTRestart
= State
->x64
.AutoHALTRestart
;
159 for (Index
= 0; Index
< sizeof (mCpuSaveStateConvTable
) / sizeof (CPU_SAVE_STATE_CONVERSION
); Index
++) {
161 /// Try to use SMM CPU Protocol to access CPU save states if possible
163 Status
= mSmmCpu
->ReadSaveState (
165 (UINTN
)sizeof (UINT32
),
166 mCpuSaveStateConvTable
[Index
].Register
,
168 ((UINT8
*)SaveState
) + mCpuSaveStateConvTable
[Index
].Offset
170 ASSERT_EFI_ERROR (Status
);
175 Write CpuSaveStates from Framework into PI.
177 The function writes back CpuSaveStates of CpuIndex-th CPU from PI to Framework. If
178 ToWrite is specified, it contains the CpuSaveStates to write from, otherwise CpuSaveStates
179 to write from mFrameworkSmst->CpuSaveState[CpuIndex].
181 @param[in] CpuIndex The zero-based CPU index.
182 @param[in] ToWrite If not NULL, CpuSaveStates to write from.
188 IN EFI_SMM_CPU_SAVE_STATE
*ToWrite
193 EFI_SMI_CPU_SAVE_STATE
*SaveState
;
195 if (ToWrite
!= NULL
) {
196 SaveState
= &ToWrite
->Ia32SaveState
;
198 SaveState
= &mFrameworkSmst
->CpuSaveState
[CpuIndex
].Ia32SaveState
;
201 for (Index
= 0; Index
< sizeof (mCpuSaveStateConvTable
) / sizeof (CPU_SAVE_STATE_CONVERSION
); Index
++) {
202 Status
= mSmmCpu
->WriteSaveState (
204 (UINTN
)sizeof (UINT32
),
205 mCpuSaveStateConvTable
[Index
].Register
,
207 ((UINT8
*)SaveState
) +
208 mCpuSaveStateConvTable
[Index
].Offset
214 Read or write a page that contains CpuSaveStates. Read is from PI to Framework.
215 Write is from Framework to PI.
217 This function reads or writes a page that contains CpuSaveStates. The page contains Framework
218 CpuSaveStates. On read, it reads PI style CpuSaveStates and fill the page up. On write, it
219 writes back from the page content to PI CpuSaveStates struct.
220 The first Framework CpuSaveStates (for CPU 0) is from mFrameworkSmst->CpuSaveState which is
221 page aligned. Because Framework CpuSaveStates are continuous, we can know which CPUs' SaveStates
222 are in the page start from PageAddress.
224 @param[in] PageAddress The base address for a page.
225 @param[in] IsRead TRUE for Read, FALSE for Write.
229 ReadWriteCpuStatePage (
230 IN UINT64 PageAddress
,
234 UINTN FirstSSIndex
; // Index of first CpuSaveState in the page
235 UINTN LastSSIndex
; // Index of last CpuSaveState in the page
236 BOOLEAN FirstSSAligned
; // Whether first CpuSaveState is page-aligned
237 BOOLEAN LastSSAligned
; // Whether the end of last CpuSaveState is page-aligned
241 FirstSSIndex
= ((UINTN
)PageAddress
- (UINTN
)mFrameworkSmst
->CpuSaveState
) / sizeof (EFI_SMM_CPU_SAVE_STATE
);
242 FirstSSAligned
= TRUE
;
243 if (((UINTN
)PageAddress
- (UINTN
)mFrameworkSmst
->CpuSaveState
) % sizeof (EFI_SMM_CPU_SAVE_STATE
) != 0) {
245 FirstSSAligned
= FALSE
;
247 LastSSIndex
= ((UINTN
)PageAddress
+ SIZE_4KB
- (UINTN
)mFrameworkSmst
->CpuSaveState
- 1) / sizeof (EFI_SMM_CPU_SAVE_STATE
);
248 LastSSAligned
= TRUE
;
249 if (((UINTN
)PageAddress
+ SIZE_4KB
- (UINTN
)mFrameworkSmst
->CpuSaveState
) % sizeof (EFI_SMM_CPU_SAVE_STATE
) != 0) {
251 LastSSAligned
= FALSE
;
253 for (CpuIndex
= FirstSSIndex
; CpuIndex
<= LastSSIndex
&& CpuIndex
< mNumberOfProcessors
; CpuIndex
++) {
255 ReadCpuSaveState (CpuIndex
, NULL
);
257 WriteCpuSaveState (CpuIndex
, NULL
);
260 if (!FirstSSAligned
) {
261 ReadCpuSaveState (FirstSSIndex
- 1, mShadowSaveState
);
262 ClippedSize
= (UINTN
)&mFrameworkSmst
->CpuSaveState
[FirstSSIndex
] & (SIZE_4KB
- 1);
264 CopyMem ((VOID
*)(UINTN
)PageAddress
, (VOID
*)((UINTN
)(mShadowSaveState
+ 1) - ClippedSize
), ClippedSize
);
266 CopyMem ((VOID
*)((UINTN
)(mShadowSaveState
+ 1) - ClippedSize
), (VOID
*)(UINTN
)PageAddress
, ClippedSize
);
267 WriteCpuSaveState (FirstSSIndex
- 1, mShadowSaveState
);
270 if (!LastSSAligned
&& LastSSIndex
+ 1 < mNumberOfProcessors
) {
271 ReadCpuSaveState (LastSSIndex
+ 1, mShadowSaveState
);
272 ClippedSize
= SIZE_4KB
- ((UINTN
)&mFrameworkSmst
->CpuSaveState
[LastSSIndex
+ 1] & (SIZE_4KB
- 1));
274 CopyMem (&mFrameworkSmst
->CpuSaveState
[LastSSIndex
+ 1], mShadowSaveState
, ClippedSize
);
276 CopyMem (mShadowSaveState
, &mFrameworkSmst
->CpuSaveState
[LastSSIndex
+ 1], ClippedSize
);
277 WriteCpuSaveState (LastSSIndex
+ 1, mShadowSaveState
);
283 The page fault handler that on-demand read PI CpuSaveStates for framework use. If the fault
284 is not targeted to mFrameworkSmst->CpuSaveState range, the function will return FALSE to let
285 PageFaultHandlerHook know it needs to pass the fault over to original page fault handler.
287 @retval TRUE The page fault is correctly handled.
288 @retval FALSE The page fault is not handled and is passed through to original handler.
299 UINTN NumCpuStatePages
;
301 ASSERT (mPageTableHookEnabled
);
302 AcquireSpinLock (&mPFLock
);
304 PageTable
= (UINT64
*)(UINTN
)(AsmReadCr3 () & mPhyMask
);
305 PFAddress
= AsmReadCr2 ();
306 NumCpuStatePages
= EFI_SIZE_TO_PAGES (mNumberOfProcessors
* sizeof (EFI_SMM_CPU_SAVE_STATE
));
308 if (((UINTN
)mFrameworkSmst
->CpuSaveState
& ~(SIZE_2MB
-1)) == (PFAddress
& ~(SIZE_2MB
-1))) {
309 if ((UINTN
)mFrameworkSmst
->CpuSaveState
<= PFAddress
&&
310 PFAddress
< (UINTN
)mFrameworkSmst
->CpuSaveState
+ EFI_PAGES_TO_SIZE (NumCpuStatePages
)
312 mCpuStatePageTable
[BitFieldRead64 (PFAddress
, 12, 20)] |= BIT0
| BIT1
; // present and rw
314 ReadWriteCpuStatePage (PFAddress
& ~(SIZE_4KB
-1), TRUE
);
321 ReleaseSpinLock (&mPFLock
);
326 Write back the dirty Framework CpuSaveStates to PI.
328 The function scans the page table for dirty pages in mFrameworkSmst->CpuSaveState
329 to write back to PI CpuSaveStates. It is meant to be called on each SmmBaseHelper SMI
330 callback after Framework handler is called.
334 WriteBackDirtyPages (
338 UINTN NumCpuStatePages
;
343 NumCpuStatePages
= EFI_SIZE_TO_PAGES (mNumberOfProcessors
* sizeof (EFI_SMM_CPU_SAVE_STATE
));
344 PTStartIndex
= (UINTN
)BitFieldRead64 ((UINT64
)mFrameworkSmst
->CpuSaveState
, 12, 20);
345 PTEndIndex
= (UINTN
)BitFieldRead64 ((UINT64
)mFrameworkSmst
->CpuSaveState
+ EFI_PAGES_TO_SIZE(NumCpuStatePages
) - 1, 12, 20);
346 for (PTIndex
= PTStartIndex
; PTIndex
<= PTEndIndex
; PTIndex
++) {
347 if ((mCpuStatePageTable
[PTIndex
] & (BIT0
|BIT6
)) == (BIT0
|BIT6
)) { // present and dirty?
348 ReadWriteCpuStatePage (mCpuStatePageTable
[PTIndex
] & mPhyMask
, FALSE
);
354 Hook IDT with our page fault handler so that the on-demand paging works on page fault.
356 The function hooks the IDT with PageFaultHandlerHook to get on-demand paging work for
357 PI<->Framework CpuSaveStates marshalling. It also saves original handler for pass-through
362 HookPageFaultHandler (
366 IA32_DESCRIPTOR Idtr
;
367 IA32_IDT_GATE_DESCRIPTOR
*IdtGateDesc
;
370 InitializeSpinLock (&mPFLock
);
373 IdtGateDesc
= (IA32_IDT_GATE_DESCRIPTOR
*) Idtr
.Base
;
374 OffsetUpper
= *(UINT32
*)((UINT64
*)IdtGateDesc
+ 1);
375 mOriginalHandler
= (VOID
*)(UINTN
)(LShiftU64 (OffsetUpper
, 32) + IdtGateDesc
[14].Bits
.OffsetLow
+ (IdtGateDesc
[14].Bits
.OffsetHigh
<< 16));
376 IdtGateDesc
[14].Bits
.OffsetLow
= (UINT32
)((UINTN
)PageFaultHandlerHook
& ((1 << 16) - 1));
377 IdtGateDesc
[14].Bits
.OffsetHigh
= (UINT32
)(((UINTN
)PageFaultHandlerHook
>> 16) & ((1 << 16) - 1));
381 Initialize page table for pages contain HookData.
383 The function initialize PDE for 2MB range that contains HookData. If the related PDE points
384 to a 2MB page, a page table will be allocated and initialized for 4KB pages. Otherwise we juse
385 use the original page table.
387 @param[in] HookData Based on which to initialize page table.
389 @return The pointer to a Page Table that points to 4KB pages which contain HookData.
392 InitCpuStatePageTable (
404 // Initialize physical address mask
405 // NOTE: Physical memory above virtual address limit is not supported !!!
407 AsmCpuid (0x80000008, (UINT32
*)&Index
, NULL
, NULL
, NULL
);
408 mPhyMask
= LShiftU64 (1, (UINT8
)Index
) - 1;
409 mPhyMask
&= (1ull << 48) - EFI_PAGE_SIZE
;
411 HookAddress
= (UINT64
)(UINTN
)HookData
;
412 PageTable
= (UINT64
*)(UINTN
)(AsmReadCr3 () & mPhyMask
);
413 PageTable
= (UINT64
*)(UINTN
)(PageTable
[BitFieldRead64 (HookAddress
, 39, 47)] & mPhyMask
);
414 PageTable
= (UINT64
*)(UINTN
)(PageTable
[BitFieldRead64 (HookAddress
, 30, 38)] & mPhyMask
);
416 Pdpte
= (UINT64
*)(UINTN
)PageTable
;
417 Pde
= Pdpte
[BitFieldRead64 (HookAddress
, 21, 29)];
418 ASSERT ((Pde
& BIT0
) != 0); // Present and 2M Page
420 if ((Pde
& BIT7
) == 0) { // 4KB Page Directory
421 PageTable
= (UINT64
*)(UINTN
)(Pde
& mPhyMask
);
423 ASSERT ((Pde
& mPhyMask
) == (HookAddress
& ~(SIZE_2MB
-1))); // 2MB Page Point to HookAddress
424 PageTable
= AllocatePages (1);
425 Address
= HookAddress
& ~(SIZE_2MB
-1);
426 for (Index
= 0; Index
< 512; Index
++) {
427 PageTable
[Index
] = Address
| BIT0
| BIT1
; // Present and RW
430 Pdpte
[BitFieldRead64 (HookAddress
, 21, 29)] = (UINT64
)(UINTN
)PageTable
| BIT0
| BIT1
; // Present and RW
436 Mark all the CpuSaveStates as not present.
438 The function marks all CpuSaveStates memory range as not present so that page fault can be triggered
439 on CpuSaveStates access. It is meant to be called on each SmmBaseHelper SMI callback before Framework
442 @param[in] CpuSaveState The base of CpuSaveStates.
447 IN EFI_SMM_CPU_SAVE_STATE
*CpuSaveState
454 PTStartIndex
= BitFieldRead64 ((UINTN
)CpuSaveState
, 12, 20);
455 PTEndIndex
= BitFieldRead64 ((UINTN
)CpuSaveState
+ mNumberOfProcessors
* sizeof (EFI_SMM_CPU_SAVE_STATE
) - 1, 12, 20);
456 for (Index
= PTStartIndex
; Index
<= PTEndIndex
; Index
++) {
457 mCpuStatePageTable
[Index
] &= ~(BIT0
|BIT5
|BIT6
); // not present nor accessed nor dirty
462 Framework SMST SmmInstallConfigurationTable() Thunk.
464 This thunk calls the PI SMM SmmInstallConfigurationTable() and then update the configuration
465 table related fields in the Framework SMST because the PI SMM SmmInstallConfigurationTable()
466 function may modify these fields.
468 @param[in] SystemTable A pointer to the SMM System Table.
469 @param[in] Guid A pointer to the GUID for the entry to add, update, or remove.
470 @param[in] Table A pointer to the buffer of the table to add.
471 @param[in] TableSize The size of the table to install.
473 @retval EFI_SUCCESS The (Guid, Table) pair was added, updated, or removed.
474 @retval EFI_INVALID_PARAMETER Guid is not valid.
475 @retval EFI_NOT_FOUND An attempt was made to delete a non-existent entry.
476 @retval EFI_OUT_OF_RESOURCES There is not enough memory available to complete the operation.
480 SmmInstallConfigurationTable (
481 IN EFI_SMM_SYSTEM_TABLE
*SystemTable
,
489 Status
= gSmst
->SmmInstallConfigurationTable (gSmst
, Guid
, Table
, TableSize
);
490 if (!EFI_ERROR (Status
)) {
491 mFrameworkSmst
->NumberOfTableEntries
= gSmst
->NumberOfTableEntries
;
492 mFrameworkSmst
->SmmConfigurationTable
= gSmst
->SmmConfigurationTable
;
498 Initialize all the stuff needed for on-demand paging hooks for PI<->Framework
499 CpuSaveStates marshalling.
501 @param[in] FrameworkSmst Framework SMM system table pointer.
506 IN EFI_SMM_SYSTEM_TABLE
*FrameworkSmst
509 UINTN NumCpuStatePages
;
514 mPageTableHookEnabled
= FALSE
;
515 NumCpuStatePages
= EFI_SIZE_TO_PAGES (mNumberOfProcessors
* sizeof (EFI_SMM_CPU_SAVE_STATE
));
517 // Only hook page table for X64 image and less than 2MB needed to hold all CPU Save States
519 if (EFI_IMAGE_MACHINE_TYPE_SUPPORTED(EFI_IMAGE_MACHINE_X64
) && NumCpuStatePages
<= EFI_SIZE_TO_PAGES (SIZE_2MB
)) {
521 // Allocate double page size to make sure all CPU Save States are in one 2MB page.
523 CpuStatePage
= (UINTN
)AllocatePages (NumCpuStatePages
* 2);
524 ASSERT (CpuStatePage
!= 0);
525 Bottom2MPage
= CpuStatePage
& ~(SIZE_2MB
-1);
526 Top2MPage
= (CpuStatePage
+ EFI_PAGES_TO_SIZE (NumCpuStatePages
* 2) - 1) & ~(SIZE_2MB
-1);
527 if (Bottom2MPage
== Top2MPage
||
528 CpuStatePage
+ EFI_PAGES_TO_SIZE (NumCpuStatePages
* 2) - Top2MPage
>= EFI_PAGES_TO_SIZE (NumCpuStatePages
)
531 // If the allocated 4KB pages are within the same 2MB page or higher portion is larger, use higher portion pages.
533 FrameworkSmst
->CpuSaveState
= (EFI_SMM_CPU_SAVE_STATE
*)(CpuStatePage
+ EFI_PAGES_TO_SIZE (NumCpuStatePages
));
534 FreePages ((VOID
*)CpuStatePage
, NumCpuStatePages
);
536 FrameworkSmst
->CpuSaveState
= (EFI_SMM_CPU_SAVE_STATE
*)CpuStatePage
;
537 FreePages ((VOID
*)(CpuStatePage
+ EFI_PAGES_TO_SIZE (NumCpuStatePages
)), NumCpuStatePages
);
540 // Add temporary working buffer for hooking
542 mShadowSaveState
= (EFI_SMM_CPU_SAVE_STATE
*) AllocatePool (sizeof (EFI_SMM_CPU_SAVE_STATE
));
543 ASSERT (mShadowSaveState
!= NULL
);
545 // Allocate and initialize 4KB Page Table for hooking CpuSaveState.
546 // Replace the original 2MB PDE with new 4KB page table.
548 mCpuStatePageTable
= InitCpuStatePageTable (FrameworkSmst
->CpuSaveState
);
550 // Mark PTE for CpuSaveState as non-exist.
552 HookCpuStateMemory (FrameworkSmst
->CpuSaveState
);
553 HookPageFaultHandler ();
555 mPageTableHookEnabled
= TRUE
;
557 mHookInitialized
= TRUE
;
561 Construct a Framework SMST based on the PI SMM SMST.
563 @return Pointer to the constructed Framework SMST.
565 EFI_SMM_SYSTEM_TABLE
*
566 ConstructFrameworkSmst (
570 EFI_SMM_SYSTEM_TABLE
*FrameworkSmst
;
572 FrameworkSmst
= (EFI_SMM_SYSTEM_TABLE
*)AllocatePool (sizeof (EFI_SMM_SYSTEM_TABLE
));
573 ASSERT (FrameworkSmst
!= NULL
);
576 /// Copy same things from PI SMST to Framework SMST
578 CopyMem (FrameworkSmst
, gSmst
, (UINTN
)(&((EFI_SMM_SYSTEM_TABLE
*)0)->SmmIo
));
580 &FrameworkSmst
->SmmIo
,
582 sizeof (EFI_SMM_SYSTEM_TABLE
) - (UINTN
)(&((EFI_SMM_SYSTEM_TABLE
*)0)->SmmIo
)
586 /// Update Framework SMST
588 FrameworkSmst
->Hdr
.Revision
= EFI_SMM_SYSTEM_TABLE_REVISION
;
589 CopyGuid (&FrameworkSmst
->EfiSmmCpuIoGuid
, &mEfiSmmCpuIoGuid
);
591 mHookInitialized
= FALSE
;
592 FrameworkSmst
->CpuSaveState
= (EFI_SMM_CPU_SAVE_STATE
*)AllocateZeroPool (mNumberOfProcessors
* sizeof (EFI_SMM_CPU_SAVE_STATE
));
593 ASSERT (FrameworkSmst
->CpuSaveState
!= NULL
);
596 /// Do not support floating point state now
598 FrameworkSmst
->CpuOptionalFloatingPointState
= NULL
;
600 FrameworkSmst
->SmmInstallConfigurationTable
= SmmInstallConfigurationTable
;
602 return FrameworkSmst
;
606 Load a given Framework SMM driver into SMRAM and invoke its entry point.
608 @param[in] ParentImageHandle Parent Image Handle.
609 @param[in] FilePath Location of the image to be installed as the handler.
610 @param[in] SourceBuffer Optional source buffer in case the image file
612 @param[in] SourceSize Size of the source image file, if in memory.
613 @param[out] ImageHandle The handle that the base driver uses to decode
614 the handler. Unique among SMM handlers only,
615 not unique across DXE/EFI.
617 @retval EFI_SUCCESS The operation was successful.
618 @retval EFI_OUT_OF_RESOURCES There were no additional SMRAM resources to load the handler
619 @retval EFI_UNSUPPORTED Can not find its copy in normal memory.
620 @retval EFI_INVALID_PARAMETER The handlers was not the correct image type
624 IN EFI_HANDLE ParentImageHandle
,
625 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
626 IN VOID
*SourceBuffer
,
628 OUT EFI_HANDLE
*ImageHandle
634 EFI_PHYSICAL_ADDRESS DstBuffer
;
636 if (FilePath
== NULL
|| ImageHandle
== NULL
) {
637 return EFI_INVALID_PARAMETER
;
642 OrgPageCount
= PageCount
;
643 DstBuffer
= (UINTN
)-1;
644 Status
= gSmst
->SmmAllocatePages (
646 EfiRuntimeServicesCode
,
650 if (EFI_ERROR (Status
)) {
654 Status
= mLoadPe32Image
->LoadPeImage (
664 EFI_LOAD_PE_IMAGE_ATTRIBUTE_NONE
666 if (EFI_ERROR (Status
)) {
667 FreePages ((VOID
*)(UINTN
)DstBuffer
, OrgPageCount
);
669 } while (Status
== EFI_BUFFER_TOO_SMALL
);
671 if (!EFI_ERROR (Status
)) {
673 /// Update MP state in Framework SMST before transferring control to Framework SMM driver entry point
674 /// in case it may invoke AP
676 mFrameworkSmst
->CurrentlyExecutingCpu
= gSmst
->CurrentlyExecutingCpu
;
678 Status
= gBS
->StartImage (*ImageHandle
, NULL
, NULL
);
679 if (EFI_ERROR (Status
)) {
680 mLoadPe32Image
->UnLoadPeImage (mLoadPe32Image
, *ImageHandle
);
682 FreePages ((VOID
*)(UINTN
)DstBuffer
, PageCount
);
691 Thunk service of EFI_SMM_BASE_PROTOCOL.Register().
693 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
697 IN OUT SMMBASE_FUNCTION_DATA
*FunctionData
702 if (mLocked
|| FunctionData
->Args
.Register
.LegacyIA32Binary
) {
703 Status
= EFI_UNSUPPORTED
;
706 FunctionData
->SmmBaseImageHandle
,
707 FunctionData
->Args
.Register
.FilePath
,
708 FunctionData
->Args
.Register
.SourceBuffer
,
709 FunctionData
->Args
.Register
.SourceSize
,
710 FunctionData
->Args
.Register
.ImageHandle
713 FunctionData
->Status
= Status
;
717 Thunk service of EFI_SMM_BASE_PROTOCOL.UnRegister().
719 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
723 IN OUT SMMBASE_FUNCTION_DATA
*FunctionData
727 /// Unregister not supported now
729 FunctionData
->Status
= EFI_UNSUPPORTED
;
733 Search for Framework SMI handler information according to specific PI SMM dispatch handle.
735 @param[in] DispatchHandle The unique handle assigned by SmiHandlerRegister().
737 @return Pointer to CALLBACK_INFO. If NULL, no callback info record is found.
741 IN EFI_HANDLE DispatchHandle
746 Node
= GetFirstNode (&mCallbackInfoListHead
);
747 while (!IsNull (&mCallbackInfoListHead
, Node
)) {
748 if (((CALLBACK_INFO
*)Node
)->DispatchHandle
== DispatchHandle
) {
749 return (CALLBACK_INFO
*)Node
;
751 Node
= GetNextNode (&mCallbackInfoListHead
, Node
);
757 Callback thunk for Framework SMI handler.
759 This thunk functions calls the Framework SMI handler and converts the return value
760 defined from Framework SMI handlers to a correpsonding return value defined by PI SMM.
762 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
763 @param[in] Context Points to an optional handler context which was specified when the
764 handler was registered.
765 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
766 be conveyed from a non-SMM environment into an SMM environment.
767 @param[in, out] CommBufferSize The size of the CommBuffer.
769 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
770 should still be called.
771 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
773 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
775 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
780 IN EFI_HANDLE DispatchHandle
,
781 IN CONST VOID
*Context OPTIONAL
,
782 IN OUT VOID
*CommBuffer OPTIONAL
,
783 IN OUT UINTN
*CommBufferSize OPTIONAL
787 CALLBACK_INFO
*CallbackInfo
;
791 /// Before transferring the control into the Framework SMI handler, update CPU Save States
792 /// and MP states in the Framework SMST.
795 if (!mHookInitialized
) {
796 InitHook (mFrameworkSmst
);
798 if (mPageTableHookEnabled
) {
799 HookCpuStateMemory (mFrameworkSmst
->CpuSaveState
);
802 for (CpuIndex
= 0; CpuIndex
< mNumberOfProcessors
; CpuIndex
++) {
803 ReadCpuSaveState (CpuIndex
, NULL
);
807 mFrameworkSmst
->SmmStartupThisAp
= gSmst
->SmmStartupThisAp
;
808 mFrameworkSmst
->NumberOfCpus
= mNumberOfProcessors
;
809 mFrameworkSmst
->CurrentlyExecutingCpu
= gSmst
->CurrentlyExecutingCpu
;
812 /// Search for Framework SMI handler information
814 CallbackInfo
= GetCallbackInfo (DispatchHandle
);
815 ASSERT (CallbackInfo
!= NULL
);
818 /// Thunk into original Framwork SMI handler
820 Status
= (CallbackInfo
->CallbackAddress
) (
821 CallbackInfo
->SmmImageHandle
,
822 CallbackInfo
->CommunicationBuffer
,
823 CallbackInfo
->SourceSize
826 /// Save CPU Save States in case any of them was modified
828 if (mPageTableHookEnabled
) {
829 WriteBackDirtyPages ();
831 for (CpuIndex
= 0; CpuIndex
< mNumberOfProcessors
; CpuIndex
++) {
832 WriteCpuSaveState (CpuIndex
, NULL
);
837 /// Conversion of returned status code
840 case EFI_HANDLER_SUCCESS
:
841 Status
= EFI_WARN_INTERRUPT_SOURCE_QUIESCED
;
843 case EFI_HANDLER_CRITICAL_EXIT
:
844 case EFI_HANDLER_SOURCE_QUIESCED
:
845 Status
= EFI_SUCCESS
;
847 case EFI_HANDLER_SOURCE_PENDING
:
848 Status
= EFI_WARN_INTERRUPT_SOURCE_PENDING
;
855 Thunk service of EFI_SMM_BASE_PROTOCOL.RegisterCallback().
857 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
861 IN OUT SMMBASE_FUNCTION_DATA
*FunctionData
864 CALLBACK_INFO
*Buffer
;
867 FunctionData
->Status
= EFI_UNSUPPORTED
;
872 /// Note that MakeLast and FloatingPointSave options are not supported in PI SMM
876 /// Allocate buffer for callback thunk information
878 Buffer
= (CALLBACK_INFO
*)AllocateZeroPool (sizeof (CALLBACK_INFO
));
879 if (Buffer
== NULL
) {
880 FunctionData
->Status
= EFI_OUT_OF_RESOURCES
;
885 /// Fill SmmImageHandle and CallbackAddress into the thunk
887 Buffer
->SmmImageHandle
= FunctionData
->Args
.RegisterCallback
.SmmImageHandle
;
888 Buffer
->CallbackAddress
= FunctionData
->Args
.RegisterCallback
.CallbackAddress
;
891 /// Register the thunk code as a root SMI handler
893 FunctionData
->Status
= gSmst
->SmiHandlerRegister (
896 &Buffer
->DispatchHandle
898 if (EFI_ERROR (FunctionData
->Status
)) {
904 /// Save this callback info
906 InsertTailList (&mCallbackInfoListHead
, &Buffer
->Link
);
911 Thunk service of EFI_SMM_BASE_PROTOCOL.SmmAllocatePool().
913 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
917 IN OUT SMMBASE_FUNCTION_DATA
*FunctionData
921 FunctionData
->Status
= EFI_UNSUPPORTED
;
923 FunctionData
->Status
= gSmst
->SmmAllocatePool (
924 FunctionData
->Args
.AllocatePool
.PoolType
,
925 FunctionData
->Args
.AllocatePool
.Size
,
926 FunctionData
->Args
.AllocatePool
.Buffer
932 Thunk service of EFI_SMM_BASE_PROTOCOL.SmmFreePool().
934 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
938 IN OUT SMMBASE_FUNCTION_DATA
*FunctionData
942 FunctionData
->Status
= EFI_UNSUPPORTED
;
944 FreePool (FunctionData
->Args
.FreePool
.Buffer
);
945 FunctionData
->Status
= EFI_SUCCESS
;
950 Thunk service of EFI_SMM_BASE_PROTOCOL.Communicate().
952 @param[in, out] FunctionData Pointer to SMMBASE_FUNCTION_DATA.
956 IN OUT SMMBASE_FUNCTION_DATA
*FunctionData
960 CALLBACK_INFO
*CallbackInfo
;
962 if (FunctionData
->Args
.Communicate
.CommunicationBuffer
== NULL
) {
963 FunctionData
->Status
= EFI_INVALID_PARAMETER
;
967 Node
= GetFirstNode (&mCallbackInfoListHead
);
968 while (!IsNull (&mCallbackInfoListHead
, Node
)) {
969 CallbackInfo
= (CALLBACK_INFO
*)Node
;
971 if (FunctionData
->Args
.Communicate
.ImageHandle
== CallbackInfo
->SmmImageHandle
) {
972 CallbackInfo
->CommunicationBuffer
= FunctionData
->Args
.Communicate
.CommunicationBuffer
;
973 CallbackInfo
->SourceSize
= FunctionData
->Args
.Communicate
.SourceSize
;
976 /// The message was successfully posted.
978 FunctionData
->Status
= EFI_SUCCESS
;
981 Node
= GetNextNode (&mCallbackInfoListHead
, Node
);
984 FunctionData
->Status
= EFI_INVALID_PARAMETER
;
988 Communication service SMI Handler entry.
990 This SMI handler provides services for the SMM Base Thunk driver.
992 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
993 @param[in] RegisterContext Points to an optional handler context which was specified when the
994 handler was registered.
995 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
996 be conveyed from a non-SMM environment into an SMM environment.
997 @param[in, out] CommBufferSize The size of the CommBuffer.
999 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
1000 should still be called.
1001 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
1003 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
1005 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
1010 IN EFI_HANDLE DispatchHandle
,
1011 IN CONST VOID
*RegisterContext
,
1012 IN OUT VOID
*CommBuffer
,
1013 IN OUT UINTN
*CommBufferSize
1016 SMMBASE_FUNCTION_DATA
*FunctionData
;
1018 ASSERT (CommBuffer
!= NULL
);
1019 ASSERT (*CommBufferSize
== sizeof (SMMBASE_FUNCTION_DATA
));
1021 FunctionData
= (SMMBASE_FUNCTION_DATA
*)CommBuffer
;
1023 switch (FunctionData
->Function
) {
1024 case SmmBaseFunctionRegister
:
1025 Register (FunctionData
);
1027 case SmmBaseFunctionUnregister
:
1028 UnRegister (FunctionData
);
1030 case SmmBaseFunctionRegisterCallback
:
1031 RegisterCallback (FunctionData
);
1033 case SmmBaseFunctionAllocatePool
:
1034 HelperAllocatePool (FunctionData
);
1036 case SmmBaseFunctionFreePool
:
1037 HelperFreePool (FunctionData
);
1039 case SmmBaseFunctionCommunicate
:
1040 HelperCommunicate (FunctionData
);
1044 FunctionData
->Status
= EFI_UNSUPPORTED
;
1050 Smm Ready To Lock event notification handler.
1052 It sets a flag indicating that SMRAM has been locked.
1054 @param[in] Protocol Points to the protocol's unique identifier.
1055 @param[in] Interface Points to the interface instance.
1056 @param[in] Handle The handle on which the interface was installed.
1058 @retval EFI_SUCCESS Notification handler runs successfully.
1062 SmmReadyToLockEventNotify (
1063 IN CONST EFI_GUID
*Protocol
,
1065 IN EFI_HANDLE Handle
1073 Entry point function of the SMM Base Helper SMM driver.
1075 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1076 @param[in] SystemTable A pointer to the EFI System Table.
1078 @retval EFI_SUCCESS The entry point is executed successfully.
1079 @retval other Some error occurs when executing this entry point.
1084 IN EFI_HANDLE ImageHandle
,
1085 IN EFI_SYSTEM_TABLE
*SystemTable
1089 EFI_MP_SERVICES_PROTOCOL
*MpServices
;
1091 UINTN NumberOfEnabledProcessors
;
1096 /// Locate SMM CPU Protocol which is used later to retrieve/update CPU Save States
1098 Status
= gSmst
->SmmLocateProtocol (&gEfiSmmCpuProtocolGuid
, NULL
, (VOID
**) &mSmmCpu
);
1099 ASSERT_EFI_ERROR (Status
);
1102 /// Locate PE32 Image Protocol which is used later to load Framework SMM driver
1104 Status
= SystemTable
->BootServices
->LocateProtocol (&gEfiLoadPeImageProtocolGuid
, NULL
, (VOID
**) &mLoadPe32Image
);
1105 ASSERT_EFI_ERROR (Status
);
1108 // Get MP Services Protocol
1110 Status
= SystemTable
->BootServices
->LocateProtocol (&gEfiMpServiceProtocolGuid
, NULL
, (VOID
**)&MpServices
);
1111 ASSERT_EFI_ERROR (Status
);
1114 // Use MP Services Protocol to retrieve the number of processors and number of enabled processors
1116 Status
= MpServices
->GetNumberOfProcessors (MpServices
, &mNumberOfProcessors
, &NumberOfEnabledProcessors
);
1117 ASSERT_EFI_ERROR (Status
);
1120 /// Interface structure of SMM BASE Helper Ready Protocol is allocated from UEFI pool
1121 /// instead of SMM pool so that SMM Base Thunk driver can access it in Non-SMM mode.
1123 Status
= gBS
->AllocatePool (
1124 EfiBootServicesData
,
1125 sizeof (EFI_SMM_BASE_HELPER_READY_PROTOCOL
),
1126 (VOID
**)&mSmmBaseHelperReady
1128 ASSERT_EFI_ERROR (Status
);
1131 /// Construct Framework SMST from PI SMST
1133 mFrameworkSmst
= ConstructFrameworkSmst ();
1134 mSmmBaseHelperReady
->FrameworkSmst
= mFrameworkSmst
;
1135 mSmmBaseHelperReady
->ServiceEntry
= SmmHandlerEntry
;
1138 // Register SMM Ready To Lock Protocol notification
1140 Status
= gSmst
->SmmRegisterProtocolNotify (
1141 &gEfiSmmReadyToLockProtocolGuid
,
1142 SmmReadyToLockEventNotify
,
1145 ASSERT_EFI_ERROR (Status
);
1148 /// Register SMM Base Helper services for SMM Base Thunk driver
1150 Status
= gSmst
->SmiHandlerRegister (SmmHandlerEntry
, &gEfiSmmBaseThunkCommunicationGuid
, &mDispatchHandle
);
1151 ASSERT_EFI_ERROR (Status
);
1154 /// Install EFI SMM Base Helper Protocol in the UEFI handle database
1156 Status
= gBS
->InstallProtocolInterface (
1158 &gEfiSmmBaseHelperReadyProtocolGuid
,
1159 EFI_NATIVE_INTERFACE
,
1162 ASSERT_EFI_ERROR (Status
);