2 The CPU specific programming for PiSmmCpuDxeSmm module.
4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/SmmCpuFeaturesLib.h>
17 #include <Library/BaseLib.h>
18 #include <Library/PcdLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/DebugLib.h>
21 #include <Register/SmramSaveStateMap.h>
24 The constructor function
26 @param[in] ImageHandle The firmware allocated handle for the EFI image.
27 @param[in] SystemTable A pointer to the EFI System Table.
29 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.
34 SmmCpuFeaturesLibConstructor (
35 IN EFI_HANDLE ImageHandle
,
36 IN EFI_SYSTEM_TABLE
*SystemTable
40 // No need to program SMRRs on our virtual platform.
46 Called during the very first SMI into System Management Mode to initialize
47 CPU features, including SMBASE, for the currently executing CPU. Since this
48 is the first SMI, the SMRAM Save State Map is at the default address of
49 SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET. The currently executing
50 CPU is specified by CpuIndex and CpuIndex can be used to access information
51 about the currently executing CPU in the ProcessorInfo array and the
52 HotPlugCpuData data structure.
54 @param[in] CpuIndex The index of the CPU to initialize. The value
55 must be between 0 and the NumberOfCpus field in
56 the System Management System Table (SMST).
57 @param[in] IsMonarch TRUE if the CpuIndex is the index of the CPU that
58 was elected as monarch during System Management
60 FALSE if the CpuIndex is not the index of the CPU
61 that was elected as monarch during System
62 Management Mode initialization.
63 @param[in] ProcessorInfo Pointer to an array of EFI_PROCESSOR_INFORMATION
64 structures. ProcessorInfo[CpuIndex] contains the
65 information for the currently executing CPU.
66 @param[in] CpuHotPlugData Pointer to the CPU_HOT_PLUG_DATA structure that
67 contains the ApidId and SmBase arrays.
71 SmmCpuFeaturesInitializeProcessor (
74 IN EFI_PROCESSOR_INFORMATION
*ProcessorInfo
,
75 IN CPU_HOT_PLUG_DATA
*CpuHotPlugData
78 SMRAM_SAVE_STATE_MAP
*CpuState
;
83 CpuState
= (SMRAM_SAVE_STATE_MAP
*)(UINTN
)(SMM_DEFAULT_SMBASE
+ SMRAM_SAVE_STATE_MAP_OFFSET
);
84 CpuState
->x86
.SMBASE
= (UINT32
)CpuHotPlugData
->SmBase
[CpuIndex
];
87 // No need to program SMRRs on our virtual platform.
92 This function updates the SMRAM save state on the currently executing CPU
93 to resume execution at a specific address after an RSM instruction. This
94 function must evaluate the SMRAM save state to determine the execution mode
95 the RSM instruction resumes and update the resume execution address with
96 either NewInstructionPointer32 or NewInstructionPoint. The auto HALT restart
97 flag in the SMRAM save state must always be cleared. This function returns
98 the value of the instruction pointer from the SMRAM save state that was
99 replaced. If this function returns 0, then the SMRAM save state was not
102 This function is called during the very first SMI on each CPU after
103 SmmCpuFeaturesInitializeProcessor() to set a flag in normal execution mode
104 to signal that the SMBASE of each CPU has been updated before the default
105 SMBASE address is used for the first SMI to the next CPU.
107 @param[in] CpuIndex The index of the CPU to hook. The value
108 must be between 0 and the NumberOfCpus
109 field in the System Management System Table
111 @param[in] CpuState Pointer to SMRAM Save State Map for the
112 currently executing CPU.
113 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to
114 32-bit execution mode from 64-bit SMM.
115 @param[in] NewInstructionPointer Instruction pointer to use if resuming to
116 same execution mode as SMM.
118 @retval 0 This function did modify the SMRAM save state.
119 @retval > 0 The original instruction pointer value from the SMRAM save state
120 before it was replaced.
124 SmmCpuFeaturesHookReturnFromSmm (
126 IN SMRAM_SAVE_STATE_MAP
*CpuState
,
127 IN UINT64 NewInstructionPointer32
,
128 IN UINT64 NewInstructionPointer
135 Hook point in normal execution mode that allows the one CPU that was elected
136 as monarch during System Management Mode initialization to perform additional
137 initialization actions immediately after all of the CPUs have processed their
138 first SMI and called SmmCpuFeaturesInitializeProcessor() relocating SMBASE
139 into a buffer in SMRAM and called SmmCpuFeaturesHookReturnFromSmm().
143 SmmCpuFeaturesSmmRelocationComplete (
150 Return the size, in bytes, of a custom SMI Handler in bytes. If 0 is
151 returned, then a custom SMI handler is not provided by this library,
152 and the default SMI handler must be used.
154 @retval 0 Use the default SMI handler.
155 @retval > 0 Use the SMI handler installed by SmmCpuFeaturesInstallSmiHandler()
156 The caller is required to allocate enough SMRAM for each CPU to
157 support the size of the custom SMI handler.
161 SmmCpuFeaturesGetSmiHandlerSize (
169 Install a custom SMI handler for the CPU specified by CpuIndex. This function
170 is only called if SmmCpuFeaturesGetSmiHandlerSize() returns a size is greater
171 than zero and is called by the CPU that was elected as monarch during System
172 Management Mode initialization.
174 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.
175 The value must be between 0 and the NumberOfCpus field
176 in the System Management System Table (SMST).
177 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.
178 @param[in] SmiStack The stack to use when an SMI is processed by the
179 the CPU specified by CpuIndex.
180 @param[in] StackSize The size, in bytes, if the stack used when an SMI is
181 processed by the CPU specified by CpuIndex.
182 @param[in] GdtBase The base address of the GDT to use when an SMI is
183 processed by the CPU specified by CpuIndex.
184 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is
185 processed by the CPU specified by CpuIndex.
186 @param[in] IdtBase The base address of the IDT to use when an SMI is
187 processed by the CPU specified by CpuIndex.
188 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is
189 processed by the CPU specified by CpuIndex.
190 @param[in] Cr3 The base address of the page tables to use when an SMI
191 is processed by the CPU specified by CpuIndex.
195 SmmCpuFeaturesInstallSmiHandler (
210 Determines if MTRR registers must be configured to set SMRAM cache-ability
211 when executing in System Management Mode.
213 @retval TRUE MTRR registers must be configured to set SMRAM cache-ability.
214 @retval FALSE MTRR registers do not need to be configured to set SMRAM
219 SmmCpuFeaturesNeedConfigureMtrrs (
227 Disable SMRR register if SMRR is supported and SmmCpuFeaturesNeedConfigureMtrrs()
232 SmmCpuFeaturesDisableSmrr (
237 // No SMRR support, nothing to do
242 Enable SMRR register if SMRR is supported and SmmCpuFeaturesNeedConfigureMtrrs()
247 SmmCpuFeaturesReenableSmrr (
252 // No SMRR support, nothing to do
257 Processor specific hook point each time a CPU enters System Management Mode.
259 @param[in] CpuIndex The index of the CPU that has entered SMM. The value
260 must be between 0 and the NumberOfCpus field in the
261 System Management System Table (SMST).
265 SmmCpuFeaturesRendezvousEntry (
270 // No SMRR support, nothing to do
275 Processor specific hook point each time a CPU exits System Management Mode.
277 @param[in] CpuIndex The index of the CPU that is exiting SMM. The value must
278 be between 0 and the NumberOfCpus field in the System
279 Management System Table (SMST).
283 SmmCpuFeaturesRendezvousExit (
290 Check to see if an SMM register is supported by a specified CPU.
292 @param[in] CpuIndex The index of the CPU to check for SMM register support.
293 The value must be between 0 and the NumberOfCpus field
294 in the System Management System Table (SMST).
295 @param[in] RegName Identifies the SMM register to check for support.
297 @retval TRUE The SMM register specified by RegName is supported by the CPU
298 specified by CpuIndex.
299 @retval FALSE The SMM register specified by RegName is not supported by the
300 CPU specified by CpuIndex.
304 SmmCpuFeaturesIsSmmRegisterSupported (
306 IN SMM_REG_NAME RegName
309 ASSERT (RegName
== SmmRegFeatureControl
);
314 Returns the current value of the SMM register for the specified CPU.
315 If the SMM register is not supported, then 0 is returned.
317 @param[in] CpuIndex The index of the CPU to read the SMM register. The
318 value must be between 0 and the NumberOfCpus field in
319 the System Management System Table (SMST).
320 @param[in] RegName Identifies the SMM register to read.
322 @return The value of the SMM register specified by RegName from the CPU
323 specified by CpuIndex.
327 SmmCpuFeaturesGetSmmRegister (
329 IN SMM_REG_NAME RegName
333 // This is called for SmmRegSmmDelayed, SmmRegSmmBlocked, SmmRegSmmEnable.
334 // The last of these should actually be SmmRegSmmDisable, so we can just
341 Sets the value of an SMM register on a specified CPU.
342 If the SMM register is not supported, then no action is performed.
344 @param[in] CpuIndex The index of the CPU to write the SMM register. The
345 value must be between 0 and the NumberOfCpus field in
346 the System Management System Table (SMST).
347 @param[in] RegName Identifies the SMM register to write.
348 registers are read-only.
349 @param[in] Value The value to write to the SMM register.
353 SmmCpuFeaturesSetSmmRegister (
355 IN SMM_REG_NAME RegName
,
363 Read an SMM Save State register on the target processor. If this function
364 returns EFI_UNSUPPORTED, then the caller is responsible for reading the
365 SMM Save Sate register.
367 @param[in] CpuIndex The index of the CPU to read the SMM Save State. The
368 value must be between 0 and the NumberOfCpus field in
369 the System Management System Table (SMST).
370 @param[in] Register The SMM Save State register to read.
371 @param[in] Width The number of bytes to read from the CPU save state.
372 @param[out] Buffer Upon return, this holds the CPU register value read
375 @retval EFI_SUCCESS The register was read from Save State.
376 @retval EFI_INVALID_PARAMTER Buffer is NULL.
377 @retval EFI_UNSUPPORTED This function does not support reading Register.
382 SmmCpuFeaturesReadSaveStateRegister (
384 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
389 return EFI_UNSUPPORTED
;
393 Writes an SMM Save State register on the target processor. If this function
394 returns EFI_UNSUPPORTED, then the caller is responsible for writing the
395 SMM Save Sate register.
397 @param[in] CpuIndex The index of the CPU to write the SMM Save State. The
398 value must be between 0 and the NumberOfCpus field in
399 the System Management System Table (SMST).
400 @param[in] Register The SMM Save State register to write.
401 @param[in] Width The number of bytes to write to the CPU save state.
402 @param[in] Buffer Upon entry, this holds the new CPU register value.
404 @retval EFI_SUCCESS The register was written to Save State.
405 @retval EFI_INVALID_PARAMTER Buffer is NULL.
406 @retval EFI_UNSUPPORTED This function does not support writing Register.
410 SmmCpuFeaturesWriteSaveStateRegister (
412 IN EFI_SMM_SAVE_STATE_REGISTER Register
,
414 IN CONST VOID
*Buffer
417 return EFI_UNSUPPORTED
;
421 This function is hook point called after the gEfiSmmReadyToLockProtocolGuid
422 notification is completely processed.
426 SmmCpuFeaturesCompleteSmmReadyToLock (
433 This API provides a method for a CPU to allocate a specific region for storing page tables.
435 This API can be called more once to allocate memory for page tables.
437 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns a pointer to the
438 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
439 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
442 This function can also return NULL if there is no preference on where the page tables are allocated in SMRAM.
444 @param Pages The number of 4 KB pages to allocate.
446 @return A pointer to the allocated buffer for page tables.
447 @retval NULL Fail to allocate a specific region for storing page tables,
448 Or there is no preference on where the page tables are allocated in SMRAM.
453 SmmCpuFeaturesAllocatePageTableMemory (