]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/Library/SmmCpuFeaturesLib/SmmCpuFeaturesLib.c
OvmfPkg/SmmCpuFeaturesLib: call CPU hot-eject handler
[mirror_edk2.git] / OvmfPkg / Library / SmmCpuFeaturesLib / SmmCpuFeaturesLib.c
CommitLineData
86d71589 1/** @file\r
b1bfdd65 2 The CPU specific programming for PiSmmCpuDxeSmm module.\r
86d71589 3\r
b1bfdd65 4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
86d71589 5\r
b26f0cf9 6 SPDX-License-Identifier: BSD-2-Clause-Patent\r
86d71589
PB
7**/\r
8\r
300aae11 9#include <IndustryStandard/Q35MchIch9.h>\r
86d71589 10#include <Library/BaseLib.h>\r
4036b4e5 11#include <Library/BaseMemoryLib.h>\r
4a9b250b 12#include <Library/DebugLib.h>\r
5ef3b66f 13#include <Library/MemEncryptSevLib.h>\r
b6d59967 14#include <Library/MemoryAllocationLib.h>\r
300aae11 15#include <Library/PcdLib.h>\r
b6d59967 16#include <Library/SafeIntLib.h>\r
4a9b250b 17#include <Library/SmmCpuFeaturesLib.h>\r
4036b4e5 18#include <Library/SmmServicesTableLib.h>\r
5ef3b66f 19#include <Library/UefiBootServicesTableLib.h>\r
b6d59967 20#include <Pcd/CpuHotEjectData.h>\r
4a9b250b 21#include <PiSmm.h>\r
300aae11 22#include <Register/Intel/SmramSaveStateMap.h>\r
c1fcd80b 23#include <Register/QemuSmramSaveStateMap.h>\r
86d71589 24\r
4036b4e5
PB
25//\r
26// EFER register LMA bit\r
27//\r
28#define LMA BIT10\r
29\r
86d71589
PB
30/**\r
31 The constructor function\r
32\r
33 @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
34 @param[in] SystemTable A pointer to the EFI System Table.\r
35\r
36 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
37\r
38**/\r
39EFI_STATUS\r
40EFIAPI\r
41SmmCpuFeaturesLibConstructor (\r
42 IN EFI_HANDLE ImageHandle,\r
43 IN EFI_SYSTEM_TABLE *SystemTable\r
44 )\r
45{\r
86d71589 46 //\r
d7e71b29 47 // No need to program SMRRs on our virtual platform.\r
86d71589 48 //\r
86d71589
PB
49 return EFI_SUCCESS;\r
50}\r
51\r
52/**\r
53 Called during the very first SMI into System Management Mode to initialize\r
54 CPU features, including SMBASE, for the currently executing CPU. Since this\r
55 is the first SMI, the SMRAM Save State Map is at the default address of\r
56 SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET. The currently executing\r
57 CPU is specified by CpuIndex and CpuIndex can be used to access information\r
58 about the currently executing CPU in the ProcessorInfo array and the\r
59 HotPlugCpuData data structure.\r
60\r
61 @param[in] CpuIndex The index of the CPU to initialize. The value\r
62 must be between 0 and the NumberOfCpus field in\r
63 the System Management System Table (SMST).\r
64 @param[in] IsMonarch TRUE if the CpuIndex is the index of the CPU that\r
65 was elected as monarch during System Management\r
66 Mode initialization.\r
67 FALSE if the CpuIndex is not the index of the CPU\r
68 that was elected as monarch during System\r
69 Management Mode initialization.\r
70 @param[in] ProcessorInfo Pointer to an array of EFI_PROCESSOR_INFORMATION\r
71 structures. ProcessorInfo[CpuIndex] contains the\r
72 information for the currently executing CPU.\r
73 @param[in] CpuHotPlugData Pointer to the CPU_HOT_PLUG_DATA structure that\r
74 contains the ApidId and SmBase arrays.\r
75**/\r
76VOID\r
77EFIAPI\r
78SmmCpuFeaturesInitializeProcessor (\r
79 IN UINTN CpuIndex,\r
80 IN BOOLEAN IsMonarch,\r
81 IN EFI_PROCESSOR_INFORMATION *ProcessorInfo,\r
82 IN CPU_HOT_PLUG_DATA *CpuHotPlugData\r
83 )\r
84{\r
c1fcd80b 85 QEMU_SMRAM_SAVE_STATE_MAP *CpuState;\r
86d71589
PB
86\r
87 //\r
88 // Configure SMBASE.\r
89 //\r
b1bfdd65
LE
90 CpuState = (QEMU_SMRAM_SAVE_STATE_MAP *)(UINTN)(\r
91 SMM_DEFAULT_SMBASE +\r
92 SMRAM_SAVE_STATE_MAP_OFFSET\r
93 );\r
c1fcd80b
PB
94 if ((CpuState->x86.SMMRevId & 0xFFFF) == 0) {\r
95 CpuState->x86.SMBASE = (UINT32)CpuHotPlugData->SmBase[CpuIndex];\r
96 } else {\r
97 CpuState->x64.SMBASE = (UINT32)CpuHotPlugData->SmBase[CpuIndex];\r
98 }\r
86d71589
PB
99\r
100 //\r
d7e71b29 101 // No need to program SMRRs on our virtual platform.\r
86d71589 102 //\r
86d71589
PB
103}\r
104\r
105/**\r
106 This function updates the SMRAM save state on the currently executing CPU\r
107 to resume execution at a specific address after an RSM instruction. This\r
108 function must evaluate the SMRAM save state to determine the execution mode\r
109 the RSM instruction resumes and update the resume execution address with\r
110 either NewInstructionPointer32 or NewInstructionPoint. The auto HALT restart\r
111 flag in the SMRAM save state must always be cleared. This function returns\r
112 the value of the instruction pointer from the SMRAM save state that was\r
113 replaced. If this function returns 0, then the SMRAM save state was not\r
114 modified.\r
115\r
116 This function is called during the very first SMI on each CPU after\r
117 SmmCpuFeaturesInitializeProcessor() to set a flag in normal execution mode\r
118 to signal that the SMBASE of each CPU has been updated before the default\r
119 SMBASE address is used for the first SMI to the next CPU.\r
120\r
121 @param[in] CpuIndex The index of the CPU to hook. The value\r
122 must be between 0 and the NumberOfCpus\r
b1bfdd65
LE
123 field in the System Management System\r
124 Table (SMST).\r
86d71589
PB
125 @param[in] CpuState Pointer to SMRAM Save State Map for the\r
126 currently executing CPU.\r
127 @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to\r
128 32-bit execution mode from 64-bit SMM.\r
129 @param[in] NewInstructionPointer Instruction pointer to use if resuming to\r
130 same execution mode as SMM.\r
131\r
132 @retval 0 This function did modify the SMRAM save state.\r
133 @retval > 0 The original instruction pointer value from the SMRAM save state\r
134 before it was replaced.\r
135**/\r
136UINT64\r
137EFIAPI\r
138SmmCpuFeaturesHookReturnFromSmm (\r
139 IN UINTN CpuIndex,\r
140 IN SMRAM_SAVE_STATE_MAP *CpuState,\r
141 IN UINT64 NewInstructionPointer32,\r
142 IN UINT64 NewInstructionPointer\r
143 )\r
144{\r
c1fcd80b 145 UINT64 OriginalInstructionPointer;\r
b1bfdd65 146 QEMU_SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
4036b4e5 147\r
b1bfdd65 148 CpuSaveState = (QEMU_SMRAM_SAVE_STATE_MAP *)CpuState;\r
4036b4e5
PB
149 if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) {\r
150 OriginalInstructionPointer = (UINT64)CpuSaveState->x86._EIP;\r
151 CpuSaveState->x86._EIP = (UINT32)NewInstructionPointer;\r
152 //\r
153 // Clear the auto HALT restart flag so the RSM instruction returns\r
154 // program control to the instruction following the HLT instruction.\r
155 //\r
156 if ((CpuSaveState->x86.AutoHALTRestart & BIT0) != 0) {\r
157 CpuSaveState->x86.AutoHALTRestart &= ~BIT0;\r
158 }\r
159 } else {\r
160 OriginalInstructionPointer = CpuSaveState->x64._RIP;\r
161 if ((CpuSaveState->x64.IA32_EFER & LMA) == 0) {\r
162 CpuSaveState->x64._RIP = (UINT32)NewInstructionPointer32;\r
163 } else {\r
164 CpuSaveState->x64._RIP = (UINT32)NewInstructionPointer;\r
165 }\r
166 //\r
167 // Clear the auto HALT restart flag so the RSM instruction returns\r
168 // program control to the instruction following the HLT instruction.\r
169 //\r
170 if ((CpuSaveState->x64.AutoHALTRestart & BIT0) != 0) {\r
171 CpuSaveState->x64.AutoHALTRestart &= ~BIT0;\r
172 }\r
173 }\r
174 return OriginalInstructionPointer;\r
86d71589
PB
175}\r
176\r
b6d59967
AA
177STATIC CPU_HOT_EJECT_DATA *mCpuHotEjectData = NULL;\r
178\r
179/**\r
180 Initialize mCpuHotEjectData if PcdCpuMaxLogicalProcessorNumber > 1.\r
181\r
182 Also setup the corresponding PcdCpuHotEjectDataAddress.\r
183**/\r
184STATIC\r
185VOID\r
186InitCpuHotEjectData (\r
187 VOID\r
188 )\r
189{\r
190 UINTN Size;\r
191 UINT32 Idx;\r
192 UINT32 MaxNumberOfCpus;\r
193 RETURN_STATUS PcdStatus;\r
194\r
195 MaxNumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
196 if (MaxNumberOfCpus == 1) {\r
197 return;\r
198 }\r
199\r
200 //\r
201 // We allocate CPU_HOT_EJECT_DATA and CPU_HOT_EJECT_DATA->QemuSelectorMap[]\r
202 // in a single allocation, and explicitly align the QemuSelectorMap[] (which\r
203 // is a UINT64 array) at its natural boundary.\r
204 // Accordingly, allocate:\r
205 // sizeof(*mCpuHotEjectData) + (MaxNumberOfCpus * sizeof(UINT64))\r
206 // and, add sizeof(UINT64) - 1 to use as padding if needed.\r
207 //\r
208\r
209 if (RETURN_ERROR (SafeUintnMult (MaxNumberOfCpus, sizeof (UINT64), &Size)) ||\r
210 RETURN_ERROR (SafeUintnAdd (Size, sizeof (*mCpuHotEjectData), &Size)) ||\r
211 RETURN_ERROR (SafeUintnAdd (Size, sizeof (UINT64) - 1, &Size))) {\r
212 DEBUG ((DEBUG_ERROR, "%a: invalid CPU_HOT_EJECT_DATA\n", __FUNCTION__));\r
213 goto Fatal;\r
214 }\r
215\r
216 mCpuHotEjectData = AllocatePool (Size);\r
217 if (mCpuHotEjectData == NULL) {\r
218 ASSERT (mCpuHotEjectData != NULL);\r
219 goto Fatal;\r
220 }\r
221\r
222 mCpuHotEjectData->Handler = NULL;\r
223 mCpuHotEjectData->ArrayLength = MaxNumberOfCpus;\r
224\r
225 mCpuHotEjectData->QemuSelectorMap = ALIGN_POINTER (mCpuHotEjectData + 1,\r
226 sizeof (UINT64));\r
227 //\r
228 // We use mCpuHotEjectData->QemuSelectorMap to map\r
229 // ProcessorNum -> QemuSelector. Initialize to invalid values.\r
230 //\r
231 for (Idx = 0; Idx < mCpuHotEjectData->ArrayLength; Idx++) {\r
232 mCpuHotEjectData->QemuSelectorMap[Idx] = CPU_EJECT_QEMU_SELECTOR_INVALID;\r
233 }\r
234\r
235 //\r
236 // Expose address of CPU Hot eject Data structure\r
237 //\r
238 PcdStatus = PcdSet64S (PcdCpuHotEjectDataAddress,\r
239 (UINTN)(VOID *)mCpuHotEjectData);\r
240 ASSERT_RETURN_ERROR (PcdStatus);\r
241\r
242 return;\r
243\r
244Fatal:\r
245 CpuDeadLoop ();\r
246}\r
247\r
86d71589
PB
248/**\r
249 Hook point in normal execution mode that allows the one CPU that was elected\r
250 as monarch during System Management Mode initialization to perform additional\r
251 initialization actions immediately after all of the CPUs have processed their\r
252 first SMI and called SmmCpuFeaturesInitializeProcessor() relocating SMBASE\r
253 into a buffer in SMRAM and called SmmCpuFeaturesHookReturnFromSmm().\r
254**/\r
255VOID\r
256EFIAPI\r
257SmmCpuFeaturesSmmRelocationComplete (\r
258 VOID\r
259 )\r
260{\r
5ef3b66f
LE
261 EFI_STATUS Status;\r
262 UINTN MapPagesBase;\r
263 UINTN MapPagesCount;\r
264\r
b6d59967
AA
265\r
266 InitCpuHotEjectData ();\r
267\r
5ef3b66f
LE
268 if (!MemEncryptSevIsEnabled ()) {\r
269 return;\r
270 }\r
271\r
272 //\r
273 // Now that SMBASE relocation is complete, re-encrypt the original SMRAM save\r
274 // state map's container pages, and release the pages to DXE. (The pages were\r
275 // allocated in PlatformPei.)\r
276 //\r
277 Status = MemEncryptSevLocateInitialSmramSaveStateMapPages (\r
278 &MapPagesBase,\r
279 &MapPagesCount\r
280 );\r
281 ASSERT_EFI_ERROR (Status);\r
282\r
283 Status = MemEncryptSevSetPageEncMask (\r
284 0, // Cr3BaseAddress -- use current CR3\r
285 MapPagesBase, // BaseAddress\r
286 MapPagesCount, // NumPages\r
287 TRUE // Flush\r
288 );\r
289 if (EFI_ERROR (Status)) {\r
290 DEBUG ((DEBUG_ERROR, "%a: MemEncryptSevSetPageEncMask(): %r\n",\r
291 __FUNCTION__, Status));\r
292 ASSERT (FALSE);\r
293 CpuDeadLoop ();\r
294 }\r
295\r
296 ZeroMem ((VOID *)MapPagesBase, EFI_PAGES_TO_SIZE (MapPagesCount));\r
297\r
300aae11
LE
298 if (PcdGetBool (PcdQ35SmramAtDefaultSmbase)) {\r
299 //\r
300 // The initial SMRAM Save State Map has been covered as part of a larger\r
301 // reserved memory allocation in PlatformPei's InitializeRamRegions(). That\r
302 // allocation is supposed to survive into OS runtime; we must not release\r
303 // any part of it. Only re-assert the containment here.\r
304 //\r
305 ASSERT (SMM_DEFAULT_SMBASE <= MapPagesBase);\r
306 ASSERT (\r
307 (MapPagesBase + EFI_PAGES_TO_SIZE (MapPagesCount) <=\r
308 SMM_DEFAULT_SMBASE + MCH_DEFAULT_SMBASE_SIZE)\r
309 );\r
310 } else {\r
311 Status = gBS->FreePages (MapPagesBase, MapPagesCount);\r
312 ASSERT_EFI_ERROR (Status);\r
313 }\r
86d71589
PB
314}\r
315\r
316/**\r
317 Return the size, in bytes, of a custom SMI Handler in bytes. If 0 is\r
318 returned, then a custom SMI handler is not provided by this library,\r
319 and the default SMI handler must be used.\r
320\r
321 @retval 0 Use the default SMI handler.\r
b1bfdd65
LE
322 @retval > 0 Use the SMI handler installed by\r
323 SmmCpuFeaturesInstallSmiHandler(). The caller is required to\r
324 allocate enough SMRAM for each CPU to support the size of the\r
325 custom SMI handler.\r
86d71589
PB
326**/\r
327UINTN\r
328EFIAPI\r
329SmmCpuFeaturesGetSmiHandlerSize (\r
330 VOID\r
331 )\r
332{\r
333 return 0;\r
334}\r
335\r
336/**\r
b1bfdd65
LE
337 Install a custom SMI handler for the CPU specified by CpuIndex. This\r
338 function is only called if SmmCpuFeaturesGetSmiHandlerSize() returns a size\r
339 is greater than zero and is called by the CPU that was elected as monarch\r
340 during System Management Mode initialization.\r
86d71589
PB
341\r
342 @param[in] CpuIndex The index of the CPU to install the custom SMI handler.\r
343 The value must be between 0 and the NumberOfCpus field\r
344 in the System Management System Table (SMST).\r
345 @param[in] SmBase The SMBASE address for the CPU specified by CpuIndex.\r
346 @param[in] SmiStack The stack to use when an SMI is processed by the\r
347 the CPU specified by CpuIndex.\r
348 @param[in] StackSize The size, in bytes, if the stack used when an SMI is\r
349 processed by the CPU specified by CpuIndex.\r
350 @param[in] GdtBase The base address of the GDT to use when an SMI is\r
351 processed by the CPU specified by CpuIndex.\r
352 @param[in] GdtSize The size, in bytes, of the GDT used when an SMI is\r
353 processed by the CPU specified by CpuIndex.\r
354 @param[in] IdtBase The base address of the IDT to use when an SMI is\r
355 processed by the CPU specified by CpuIndex.\r
356 @param[in] IdtSize The size, in bytes, of the IDT used when an SMI is\r
357 processed by the CPU specified by CpuIndex.\r
358 @param[in] Cr3 The base address of the page tables to use when an SMI\r
359 is processed by the CPU specified by CpuIndex.\r
360**/\r
361VOID\r
362EFIAPI\r
363SmmCpuFeaturesInstallSmiHandler (\r
364 IN UINTN CpuIndex,\r
365 IN UINT32 SmBase,\r
366 IN VOID *SmiStack,\r
367 IN UINTN StackSize,\r
368 IN UINTN GdtBase,\r
369 IN UINTN GdtSize,\r
370 IN UINTN IdtBase,\r
371 IN UINTN IdtSize,\r
372 IN UINT32 Cr3\r
373 )\r
374{\r
375}\r
376\r
377/**\r
378 Determines if MTRR registers must be configured to set SMRAM cache-ability\r
379 when executing in System Management Mode.\r
380\r
381 @retval TRUE MTRR registers must be configured to set SMRAM cache-ability.\r
382 @retval FALSE MTRR registers do not need to be configured to set SMRAM\r
383 cache-ability.\r
384**/\r
385BOOLEAN\r
386EFIAPI\r
387SmmCpuFeaturesNeedConfigureMtrrs (\r
388 VOID\r
389 )\r
390{\r
d7e71b29 391 return FALSE;\r
86d71589
PB
392}\r
393\r
394/**\r
b1bfdd65
LE
395 Disable SMRR register if SMRR is supported and\r
396 SmmCpuFeaturesNeedConfigureMtrrs() returns TRUE.\r
86d71589
PB
397**/\r
398VOID\r
399EFIAPI\r
400SmmCpuFeaturesDisableSmrr (\r
401 VOID\r
402 )\r
403{\r
d7e71b29
PB
404 //\r
405 // No SMRR support, nothing to do\r
406 //\r
86d71589
PB
407}\r
408\r
409/**\r
b1bfdd65
LE
410 Enable SMRR register if SMRR is supported and\r
411 SmmCpuFeaturesNeedConfigureMtrrs() returns TRUE.\r
86d71589
PB
412**/\r
413VOID\r
414EFIAPI\r
415SmmCpuFeaturesReenableSmrr (\r
416 VOID\r
417 )\r
418{\r
d7e71b29
PB
419 //\r
420 // No SMRR support, nothing to do\r
421 //\r
86d71589
PB
422}\r
423\r
424/**\r
425 Processor specific hook point each time a CPU enters System Management Mode.\r
426\r
427 @param[in] CpuIndex The index of the CPU that has entered SMM. The value\r
428 must be between 0 and the NumberOfCpus field in the\r
429 System Management System Table (SMST).\r
430**/\r
431VOID\r
432EFIAPI\r
433SmmCpuFeaturesRendezvousEntry (\r
434 IN UINTN CpuIndex\r
435 )\r
436{\r
437 //\r
d7e71b29 438 // No SMRR support, nothing to do\r
86d71589 439 //\r
86d71589
PB
440}\r
441\r
442/**\r
443 Processor specific hook point each time a CPU exits System Management Mode.\r
444\r
b1bfdd65
LE
445 @param[in] CpuIndex The index of the CPU that is exiting SMM. The value\r
446 must be between 0 and the NumberOfCpus field in the\r
447 System Management System Table (SMST).\r
86d71589
PB
448**/\r
449VOID\r
450EFIAPI\r
451SmmCpuFeaturesRendezvousExit (\r
452 IN UINTN CpuIndex\r
453 )\r
454{\r
af9c77e1
AA
455 //\r
456 // We only call the Handler if CPU hot-eject is enabled\r
457 // (PcdCpuMaxLogicalProcessorNumber > 1), and hot-eject is needed\r
458 // in this SMI exit (otherwise mCpuHotEjectData->Handler is not armed.)\r
459 //\r
460\r
461 if (mCpuHotEjectData != NULL) {\r
462 CPU_HOT_EJECT_HANDLER Handler;\r
463\r
464 //\r
465 // As the comment above mentions, mCpuHotEjectData->Handler might be\r
466 // written to on the BSP as part of handling of the CPU-ejection.\r
467 //\r
468 // We know that any initial assignment to mCpuHotEjectData->Handler\r
469 // (on the BSP, in the CpuHotplugMmi() context) is ordered-before the\r
470 // load below, since it is guaranteed to happen before the\r
471 // control-dependency of the BSP's SMI exit signal -- by way of a store\r
472 // to AllCpusInSync (on the BSP, in BspHandler()) and the corresponding\r
473 // AllCpusInSync loop (on the APs, in SmiRendezvous()) which depends on\r
474 // that store.\r
475 //\r
476 // This guarantees that these pieces of code can never execute\r
477 // simultaneously. In addition, we ensure that the following load is\r
478 // ordered-after the AllCpusInSync loop by using a MemoryFence() with\r
479 // acquire semantics.\r
480 //\r
481 MemoryFence();\r
482\r
483 Handler = mCpuHotEjectData->Handler;\r
484\r
485 if (Handler != NULL) {\r
486 Handler (CpuIndex);\r
487 }\r
488 }\r
86d71589
PB
489}\r
490\r
491/**\r
492 Check to see if an SMM register is supported by a specified CPU.\r
493\r
494 @param[in] CpuIndex The index of the CPU to check for SMM register support.\r
495 The value must be between 0 and the NumberOfCpus field\r
496 in the System Management System Table (SMST).\r
497 @param[in] RegName Identifies the SMM register to check for support.\r
498\r
499 @retval TRUE The SMM register specified by RegName is supported by the CPU\r
500 specified by CpuIndex.\r
501 @retval FALSE The SMM register specified by RegName is not supported by the\r
502 CPU specified by CpuIndex.\r
503**/\r
504BOOLEAN\r
505EFIAPI\r
506SmmCpuFeaturesIsSmmRegisterSupported (\r
507 IN UINTN CpuIndex,\r
508 IN SMM_REG_NAME RegName\r
509 )\r
510{\r
d7e71b29 511 ASSERT (RegName == SmmRegFeatureControl);\r
86d71589
PB
512 return FALSE;\r
513}\r
514\r
515/**\r
516 Returns the current value of the SMM register for the specified CPU.\r
517 If the SMM register is not supported, then 0 is returned.\r
518\r
519 @param[in] CpuIndex The index of the CPU to read the SMM register. The\r
520 value must be between 0 and the NumberOfCpus field in\r
521 the System Management System Table (SMST).\r
522 @param[in] RegName Identifies the SMM register to read.\r
523\r
524 @return The value of the SMM register specified by RegName from the CPU\r
525 specified by CpuIndex.\r
526**/\r
527UINT64\r
528EFIAPI\r
529SmmCpuFeaturesGetSmmRegister (\r
530 IN UINTN CpuIndex,\r
531 IN SMM_REG_NAME RegName\r
532 )\r
533{\r
d7e71b29
PB
534 //\r
535 // This is called for SmmRegSmmDelayed, SmmRegSmmBlocked, SmmRegSmmEnable.\r
536 // The last of these should actually be SmmRegSmmDisable, so we can just\r
537 // return FALSE.\r
538 //\r
86d71589
PB
539 return 0;\r
540}\r
541\r
542/**\r
543 Sets the value of an SMM register on a specified CPU.\r
544 If the SMM register is not supported, then no action is performed.\r
545\r
546 @param[in] CpuIndex The index of the CPU to write the SMM register. The\r
547 value must be between 0 and the NumberOfCpus field in\r
548 the System Management System Table (SMST).\r
549 @param[in] RegName Identifies the SMM register to write.\r
550 registers are read-only.\r
551 @param[in] Value The value to write to the SMM register.\r
552**/\r
553VOID\r
554EFIAPI\r
555SmmCpuFeaturesSetSmmRegister (\r
556 IN UINTN CpuIndex,\r
557 IN SMM_REG_NAME RegName,\r
558 IN UINT64 Value\r
559 )\r
560{\r
d7e71b29 561 ASSERT (FALSE);\r
86d71589
PB
562}\r
563\r
4036b4e5 564///\r
b1bfdd65
LE
565/// Macro used to simplify the lookup table entries of type\r
566/// CPU_SMM_SAVE_STATE_LOOKUP_ENTRY\r
4036b4e5 567///\r
c1fcd80b 568#define SMM_CPU_OFFSET(Field) OFFSET_OF (QEMU_SMRAM_SAVE_STATE_MAP, Field)\r
4036b4e5
PB
569\r
570///\r
b1bfdd65
LE
571/// Macro used to simplify the lookup table entries of type\r
572/// CPU_SMM_SAVE_STATE_REGISTER_RANGE\r
4036b4e5
PB
573///\r
574#define SMM_REGISTER_RANGE(Start, End) { Start, End, End - Start + 1 }\r
575\r
576///\r
577/// Structure used to describe a range of registers\r
578///\r
579typedef struct {\r
580 EFI_SMM_SAVE_STATE_REGISTER Start;\r
581 EFI_SMM_SAVE_STATE_REGISTER End;\r
582 UINTN Length;\r
583} CPU_SMM_SAVE_STATE_REGISTER_RANGE;\r
584\r
585///\r
586/// Structure used to build a lookup table to retrieve the widths and offsets\r
587/// associated with each supported EFI_SMM_SAVE_STATE_REGISTER value\r
588///\r
589\r
590#define SMM_SAVE_STATE_REGISTER_FIRST_INDEX 1\r
591\r
592typedef struct {\r
593 UINT8 Width32;\r
594 UINT8 Width64;\r
595 UINT16 Offset32;\r
596 UINT16 Offset64Lo;\r
597 UINT16 Offset64Hi;\r
598 BOOLEAN Writeable;\r
599} CPU_SMM_SAVE_STATE_LOOKUP_ENTRY;\r
600\r
601///\r
b1bfdd65 602/// Table used by GetRegisterIndex() to convert an EFI_SMM_SAVE_STATE_REGISTER\r
4036b4e5
PB
603/// value to an index into a table of type CPU_SMM_SAVE_STATE_LOOKUP_ENTRY\r
604///\r
ea992760 605STATIC CONST CPU_SMM_SAVE_STATE_REGISTER_RANGE mSmmCpuRegisterRanges[] = {\r
b1bfdd65
LE
606 SMM_REGISTER_RANGE (\r
607 EFI_SMM_SAVE_STATE_REGISTER_GDTBASE,\r
608 EFI_SMM_SAVE_STATE_REGISTER_LDTINFO\r
609 ),\r
610 SMM_REGISTER_RANGE (\r
611 EFI_SMM_SAVE_STATE_REGISTER_ES,\r
612 EFI_SMM_SAVE_STATE_REGISTER_RIP\r
613 ),\r
614 SMM_REGISTER_RANGE (\r
615 EFI_SMM_SAVE_STATE_REGISTER_RFLAGS,\r
616 EFI_SMM_SAVE_STATE_REGISTER_CR4\r
617 ),\r
4036b4e5
PB
618 { (EFI_SMM_SAVE_STATE_REGISTER)0, (EFI_SMM_SAVE_STATE_REGISTER)0, 0 }\r
619};\r
620\r
621///\r
b1bfdd65
LE
622/// Lookup table used to retrieve the widths and offsets associated with each\r
623/// supported EFI_SMM_SAVE_STATE_REGISTER value\r
4036b4e5 624///\r
ea992760 625STATIC CONST CPU_SMM_SAVE_STATE_LOOKUP_ENTRY mSmmCpuWidthOffset[] = {\r
b1bfdd65
LE
626 {\r
627 0, // Width32\r
628 0, // Width64\r
629 0, // Offset32\r
630 0, // Offset64Lo\r
631 0, // Offset64Hi\r
632 FALSE // Writeable\r
633 }, // Reserved\r
4036b4e5
PB
634\r
635 //\r
636 // CPU Save State registers defined in PI SMM CPU Protocol.\r
637 //\r
b1bfdd65
LE
638 {\r
639 0, // Width32\r
640 8, // Width64\r
641 0, // Offset32\r
642 SMM_CPU_OFFSET (x64._GDTRBase), // Offset64Lo\r
643 SMM_CPU_OFFSET (x64._GDTRBase) + 4, // Offset64Hi\r
644 FALSE // Writeable\r
645 }, // EFI_SMM_SAVE_STATE_REGISTER_GDTBASE = 4\r
646\r
647 {\r
648 0, // Width32\r
649 8, // Width64\r
650 0, // Offset32\r
651 SMM_CPU_OFFSET (x64._IDTRBase), // Offset64Lo\r
652 SMM_CPU_OFFSET (x64._IDTRBase) + 4, // Offset64Hi\r
653 FALSE // Writeable\r
654 }, // EFI_SMM_SAVE_STATE_REGISTER_IDTBASE = 5\r
655\r
656 {\r
657 0, // Width32\r
658 8, // Width64\r
659 0, // Offset32\r
660 SMM_CPU_OFFSET (x64._LDTRBase), // Offset64Lo\r
661 SMM_CPU_OFFSET (x64._LDTRBase) + 4, // Offset64Hi\r
662 FALSE // Writeable\r
663 }, // EFI_SMM_SAVE_STATE_REGISTER_LDTBASE = 6\r
664\r
665 {\r
666 0, // Width32\r
667 0, // Width64\r
668 0, // Offset32\r
669 SMM_CPU_OFFSET (x64._GDTRLimit), // Offset64Lo\r
670 SMM_CPU_OFFSET (x64._GDTRLimit) + 4, // Offset64Hi\r
671 FALSE // Writeable\r
672 }, // EFI_SMM_SAVE_STATE_REGISTER_GDTLIMIT = 7\r
673\r
674 {\r
675 0, // Width32\r
676 0, // Width64\r
677 0, // Offset32\r
678 SMM_CPU_OFFSET (x64._IDTRLimit), // Offset64Lo\r
679 SMM_CPU_OFFSET (x64._IDTRLimit) + 4, // Offset64Hi\r
680 FALSE // Writeable\r
681 }, // EFI_SMM_SAVE_STATE_REGISTER_IDTLIMIT = 8\r
682\r
683 {\r
684 0, // Width32\r
685 0, // Width64\r
686 0, // Offset32\r
687 SMM_CPU_OFFSET (x64._LDTRLimit), // Offset64Lo\r
688 SMM_CPU_OFFSET (x64._LDTRLimit) + 4, // Offset64Hi\r
689 FALSE // Writeable\r
690 }, // EFI_SMM_SAVE_STATE_REGISTER_LDTLIMIT = 9\r
691\r
692 {\r
693 0, // Width32\r
694 0, // Width64\r
695 0, // Offset32\r
696 0, // Offset64Lo\r
697 0 + 4, // Offset64Hi\r
698 FALSE // Writeable\r
699 }, // EFI_SMM_SAVE_STATE_REGISTER_LDTINFO = 10\r
700\r
701 {\r
702 4, // Width32\r
703 4, // Width64\r
704 SMM_CPU_OFFSET (x86._ES), // Offset32\r
705 SMM_CPU_OFFSET (x64._ES), // Offset64Lo\r
706 0, // Offset64Hi\r
707 FALSE // Writeable\r
708 }, // EFI_SMM_SAVE_STATE_REGISTER_ES = 20\r
709\r
710 {\r
711 4, // Width32\r
712 4, // Width64\r
713 SMM_CPU_OFFSET (x86._CS), // Offset32\r
714 SMM_CPU_OFFSET (x64._CS), // Offset64Lo\r
715 0, // Offset64Hi\r
716 FALSE // Writeable\r
717 }, // EFI_SMM_SAVE_STATE_REGISTER_CS = 21\r
718\r
719 {\r
720 4, // Width32\r
721 4, // Width64\r
722 SMM_CPU_OFFSET (x86._SS), // Offset32\r
723 SMM_CPU_OFFSET (x64._SS), // Offset64Lo\r
724 0, // Offset64Hi\r
725 FALSE // Writeable\r
726 }, // EFI_SMM_SAVE_STATE_REGISTER_SS = 22\r
727\r
728 {\r
729 4, // Width32\r
730 4, // Width64\r
731 SMM_CPU_OFFSET (x86._DS), // Offset32\r
732 SMM_CPU_OFFSET (x64._DS), // Offset64Lo\r
733 0, // Offset64Hi\r
734 FALSE // Writeable\r
735 }, // EFI_SMM_SAVE_STATE_REGISTER_DS = 23\r
736\r
737 {\r
738 4, // Width32\r
739 4, // Width64\r
740 SMM_CPU_OFFSET (x86._FS), // Offset32\r
741 SMM_CPU_OFFSET (x64._FS), // Offset64Lo\r
742 0, // Offset64Hi\r
743 FALSE // Writeable\r
744 }, // EFI_SMM_SAVE_STATE_REGISTER_FS = 24\r
745\r
746 {\r
747 4, // Width32\r
748 4, // Width64\r
749 SMM_CPU_OFFSET (x86._GS), // Offset32\r
750 SMM_CPU_OFFSET (x64._GS), // Offset64Lo\r
751 0, // Offset64Hi\r
752 FALSE // Writeable\r
753 }, // EFI_SMM_SAVE_STATE_REGISTER_GS = 25\r
754\r
755 {\r
756 0, // Width32\r
757 4, // Width64\r
758 0, // Offset32\r
759 SMM_CPU_OFFSET (x64._LDTR), // Offset64Lo\r
760 0, // Offset64Hi\r
761 FALSE // Writeable\r
762 }, // EFI_SMM_SAVE_STATE_REGISTER_LDTR_SEL = 26\r
763\r
764 {\r
765 4, // Width32\r
766 4, // Width64\r
767 SMM_CPU_OFFSET (x86._TR), // Offset32\r
768 SMM_CPU_OFFSET (x64._TR), // Offset64Lo\r
769 0, // Offset64Hi\r
770 FALSE // Writeable\r
771 }, // EFI_SMM_SAVE_STATE_REGISTER_TR_SEL = 27\r
772\r
773 {\r
774 4, // Width32\r
775 8, // Width64\r
776 SMM_CPU_OFFSET (x86._DR7), // Offset32\r
777 SMM_CPU_OFFSET (x64._DR7), // Offset64Lo\r
778 SMM_CPU_OFFSET (x64._DR7) + 4, // Offset64Hi\r
779 FALSE // Writeable\r
780 }, // EFI_SMM_SAVE_STATE_REGISTER_DR7 = 28\r
781\r
782 {\r
783 4, // Width32\r
784 8, // Width64\r
785 SMM_CPU_OFFSET (x86._DR6), // Offset32\r
786 SMM_CPU_OFFSET (x64._DR6), // Offset64Lo\r
787 SMM_CPU_OFFSET (x64._DR6) + 4, // Offset64Hi\r
788 FALSE // Writeable\r
789 }, // EFI_SMM_SAVE_STATE_REGISTER_DR6 = 29\r
790\r
791 {\r
792 0, // Width32\r
793 8, // Width64\r
794 0, // Offset32\r
795 SMM_CPU_OFFSET (x64._R8), // Offset64Lo\r
796 SMM_CPU_OFFSET (x64._R8) + 4, // Offset64Hi\r
797 TRUE // Writeable\r
798 }, // EFI_SMM_SAVE_STATE_REGISTER_R8 = 30\r
799\r
800 {\r
801 0, // Width32\r
802 8, // Width64\r
803 0, // Offset32\r
804 SMM_CPU_OFFSET (x64._R9), // Offset64Lo\r
805 SMM_CPU_OFFSET (x64._R9) + 4, // Offset64Hi\r
806 TRUE // Writeable\r
807 }, // EFI_SMM_SAVE_STATE_REGISTER_R9 = 31\r
808\r
809 {\r
810 0, // Width32\r
811 8, // Width64\r
812 0, // Offset32\r
813 SMM_CPU_OFFSET (x64._R10), // Offset64Lo\r
814 SMM_CPU_OFFSET (x64._R10) + 4, // Offset64Hi\r
815 TRUE // Writeable\r
816 }, // EFI_SMM_SAVE_STATE_REGISTER_R10 = 32\r
817\r
818 {\r
819 0, // Width32\r
820 8, // Width64\r
821 0, // Offset32\r
822 SMM_CPU_OFFSET (x64._R11), // Offset64Lo\r
823 SMM_CPU_OFFSET (x64._R11) + 4, // Offset64Hi\r
824 TRUE // Writeable\r
825 }, // EFI_SMM_SAVE_STATE_REGISTER_R11 = 33\r
826\r
827 {\r
828 0, // Width32\r
829 8, // Width64\r
830 0, // Offset32\r
831 SMM_CPU_OFFSET (x64._R12), // Offset64Lo\r
832 SMM_CPU_OFFSET (x64._R12) + 4, // Offset64Hi\r
833 TRUE // Writeable\r
834 }, // EFI_SMM_SAVE_STATE_REGISTER_R12 = 34\r
835\r
836 {\r
837 0, // Width32\r
838 8, // Width64\r
839 0, // Offset32\r
840 SMM_CPU_OFFSET (x64._R13), // Offset64Lo\r
841 SMM_CPU_OFFSET (x64._R13) + 4, // Offset64Hi\r
842 TRUE // Writeable\r
843 }, // EFI_SMM_SAVE_STATE_REGISTER_R13 = 35\r
844\r
845 {\r
846 0, // Width32\r
847 8, // Width64\r
848 0, // Offset32\r
849 SMM_CPU_OFFSET (x64._R14), // Offset64Lo\r
850 SMM_CPU_OFFSET (x64._R14) + 4, // Offset64Hi\r
851 TRUE // Writeable\r
852 }, // EFI_SMM_SAVE_STATE_REGISTER_R14 = 36\r
853\r
854 {\r
855 0, // Width32\r
856 8, // Width64\r
857 0, // Offset32\r
858 SMM_CPU_OFFSET (x64._R15), // Offset64Lo\r
859 SMM_CPU_OFFSET (x64._R15) + 4, // Offset64Hi\r
860 TRUE // Writeable\r
861 }, // EFI_SMM_SAVE_STATE_REGISTER_R15 = 37\r
862\r
863 {\r
864 4, // Width32\r
865 8, // Width64\r
866 SMM_CPU_OFFSET (x86._EAX), // Offset32\r
867 SMM_CPU_OFFSET (x64._RAX), // Offset64Lo\r
868 SMM_CPU_OFFSET (x64._RAX) + 4, // Offset64Hi\r
869 TRUE // Writeable\r
870 }, // EFI_SMM_SAVE_STATE_REGISTER_RAX = 38\r
871\r
872 {\r
873 4, // Width32\r
874 8, // Width64\r
875 SMM_CPU_OFFSET (x86._EBX), // Offset32\r
876 SMM_CPU_OFFSET (x64._RBX), // Offset64Lo\r
877 SMM_CPU_OFFSET (x64._RBX) + 4, // Offset64Hi\r
878 TRUE // Writeable\r
879 }, // EFI_SMM_SAVE_STATE_REGISTER_RBX = 39\r
880\r
881 {\r
882 4, // Width32\r
883 8, // Width64\r
884 SMM_CPU_OFFSET (x86._ECX), // Offset32\r
885 SMM_CPU_OFFSET (x64._RCX), // Offset64Lo\r
886 SMM_CPU_OFFSET (x64._RCX) + 4, // Offset64Hi\r
887 TRUE // Writeable\r
888 }, // EFI_SMM_SAVE_STATE_REGISTER_RCX = 40\r
889\r
890 {\r
891 4, // Width32\r
892 8, // Width64\r
893 SMM_CPU_OFFSET (x86._EDX), // Offset32\r
894 SMM_CPU_OFFSET (x64._RDX), // Offset64Lo\r
895 SMM_CPU_OFFSET (x64._RDX) + 4, // Offset64Hi\r
896 TRUE // Writeable\r
897 }, // EFI_SMM_SAVE_STATE_REGISTER_RDX = 41\r
898\r
899 {\r
900 4, // Width32\r
901 8, // Width64\r
902 SMM_CPU_OFFSET (x86._ESP), // Offset32\r
903 SMM_CPU_OFFSET (x64._RSP), // Offset64Lo\r
904 SMM_CPU_OFFSET (x64._RSP) + 4, // Offset64Hi\r
905 TRUE // Writeable\r
906 }, // EFI_SMM_SAVE_STATE_REGISTER_RSP = 42\r
907\r
908 {\r
909 4, // Width32\r
910 8, // Width64\r
911 SMM_CPU_OFFSET (x86._EBP), // Offset32\r
912 SMM_CPU_OFFSET (x64._RBP), // Offset64Lo\r
913 SMM_CPU_OFFSET (x64._RBP) + 4, // Offset64Hi\r
914 TRUE // Writeable\r
915 }, // EFI_SMM_SAVE_STATE_REGISTER_RBP = 43\r
916\r
917 {\r
918 4, // Width32\r
919 8, // Width64\r
920 SMM_CPU_OFFSET (x86._ESI), // Offset32\r
921 SMM_CPU_OFFSET (x64._RSI), // Offset64Lo\r
922 SMM_CPU_OFFSET (x64._RSI) + 4, // Offset64Hi\r
923 TRUE // Writeable\r
924 }, // EFI_SMM_SAVE_STATE_REGISTER_RSI = 44\r
925\r
926 {\r
927 4, // Width32\r
928 8, // Width64\r
929 SMM_CPU_OFFSET (x86._EDI), // Offset32\r
930 SMM_CPU_OFFSET (x64._RDI), // Offset64Lo\r
931 SMM_CPU_OFFSET (x64._RDI) + 4, // Offset64Hi\r
932 TRUE // Writeable\r
933 }, // EFI_SMM_SAVE_STATE_REGISTER_RDI = 45\r
934\r
935 {\r
936 4, // Width32\r
937 8, // Width64\r
938 SMM_CPU_OFFSET (x86._EIP), // Offset32\r
939 SMM_CPU_OFFSET (x64._RIP), // Offset64Lo\r
940 SMM_CPU_OFFSET (x64._RIP) + 4, // Offset64Hi\r
941 TRUE // Writeable\r
942 }, // EFI_SMM_SAVE_STATE_REGISTER_RIP = 46\r
943\r
944 {\r
945 4, // Width32\r
946 8, // Width64\r
947 SMM_CPU_OFFSET (x86._EFLAGS), // Offset32\r
948 SMM_CPU_OFFSET (x64._RFLAGS), // Offset64Lo\r
949 SMM_CPU_OFFSET (x64._RFLAGS) + 4, // Offset64Hi\r
950 TRUE // Writeable\r
951 }, // EFI_SMM_SAVE_STATE_REGISTER_RFLAGS = 51\r
952\r
953 {\r
954 4, // Width32\r
955 8, // Width64\r
956 SMM_CPU_OFFSET (x86._CR0), // Offset32\r
957 SMM_CPU_OFFSET (x64._CR0), // Offset64Lo\r
958 SMM_CPU_OFFSET (x64._CR0) + 4, // Offset64Hi\r
959 FALSE // Writeable\r
960 }, // EFI_SMM_SAVE_STATE_REGISTER_CR0 = 52\r
961\r
962 {\r
963 4, // Width32\r
964 8, // Width64\r
965 SMM_CPU_OFFSET (x86._CR3), // Offset32\r
966 SMM_CPU_OFFSET (x64._CR3), // Offset64Lo\r
967 SMM_CPU_OFFSET (x64._CR3) + 4, // Offset64Hi\r
968 FALSE // Writeable\r
969 }, // EFI_SMM_SAVE_STATE_REGISTER_CR3 = 53\r
970\r
971 {\r
972 0, // Width32\r
973 4, // Width64\r
974 0, // Offset32\r
975 SMM_CPU_OFFSET (x64._CR4), // Offset64Lo\r
976 SMM_CPU_OFFSET (x64._CR4) + 4, // Offset64Hi\r
977 FALSE // Writeable\r
978 }, // EFI_SMM_SAVE_STATE_REGISTER_CR4 = 54\r
4036b4e5
PB
979};\r
980\r
981//\r
982// No support for I/O restart\r
983//\r
984\r
985/**\r
986 Read information from the CPU save state.\r
987\r
988 @param Register Specifies the CPU register to read form the save state.\r
989\r
990 @retval 0 Register is not valid\r
991 @retval >0 Index into mSmmCpuWidthOffset[] associated with Register\r
992\r
993**/\r
ea992760
LE
994STATIC\r
995UINTN\r
4036b4e5
PB
996GetRegisterIndex (\r
997 IN EFI_SMM_SAVE_STATE_REGISTER Register\r
998 )\r
999{\r
1000 UINTN Index;\r
1001 UINTN Offset;\r
1002\r
b1bfdd65
LE
1003 for (Index = 0, Offset = SMM_SAVE_STATE_REGISTER_FIRST_INDEX;\r
1004 mSmmCpuRegisterRanges[Index].Length != 0;\r
1005 Index++) {\r
1006 if (Register >= mSmmCpuRegisterRanges[Index].Start &&\r
1007 Register <= mSmmCpuRegisterRanges[Index].End) {\r
4036b4e5
PB
1008 return Register - mSmmCpuRegisterRanges[Index].Start + Offset;\r
1009 }\r
1010 Offset += mSmmCpuRegisterRanges[Index].Length;\r
1011 }\r
1012 return 0;\r
1013}\r
1014\r
1015/**\r
1016 Read a CPU Save State register on the target processor.\r
1017\r
b1bfdd65
LE
1018 This function abstracts the differences that whether the CPU Save State\r
1019 register is in the IA32 CPU Save State Map or X64 CPU Save State Map.\r
4036b4e5 1020\r
b1bfdd65
LE
1021 This function supports reading a CPU Save State register in SMBase relocation\r
1022 handler.\r
4036b4e5 1023\r
b1bfdd65
LE
1024 @param[in] CpuIndex Specifies the zero-based index of the CPU save\r
1025 state.\r
4036b4e5 1026 @param[in] RegisterIndex Index into mSmmCpuWidthOffset[] look up table.\r
b1bfdd65
LE
1027 @param[in] Width The number of bytes to read from the CPU save\r
1028 state.\r
1029 @param[out] Buffer Upon return, this holds the CPU register value\r
1030 read from the save state.\r
4036b4e5
PB
1031\r
1032 @retval EFI_SUCCESS The register was read from Save State.\r
b1bfdd65
LE
1033 @retval EFI_NOT_FOUND The register is not defined for the Save State\r
1034 of Processor.\r
4036b4e5
PB
1035 @retval EFI_INVALID_PARAMTER This or Buffer is NULL.\r
1036\r
1037**/\r
ea992760
LE
1038STATIC\r
1039EFI_STATUS\r
4036b4e5
PB
1040ReadSaveStateRegisterByIndex (\r
1041 IN UINTN CpuIndex,\r
1042 IN UINTN RegisterIndex,\r
1043 IN UINTN Width,\r
1044 OUT VOID *Buffer\r
1045 )\r
1046{\r
c1fcd80b 1047 QEMU_SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
4036b4e5 1048\r
c1fcd80b 1049 CpuSaveState = (QEMU_SMRAM_SAVE_STATE_MAP *)gSmst->CpuSaveState[CpuIndex];\r
4036b4e5
PB
1050\r
1051 if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) {\r
1052 //\r
b1bfdd65
LE
1053 // If 32-bit mode width is zero, then the specified register can not be\r
1054 // accessed\r
4036b4e5
PB
1055 //\r
1056 if (mSmmCpuWidthOffset[RegisterIndex].Width32 == 0) {\r
1057 return EFI_NOT_FOUND;\r
1058 }\r
1059\r
1060 //\r
b1bfdd65
LE
1061 // If Width is bigger than the 32-bit mode width, then the specified\r
1062 // register can not be accessed\r
4036b4e5
PB
1063 //\r
1064 if (Width > mSmmCpuWidthOffset[RegisterIndex].Width32) {\r
1065 return EFI_INVALID_PARAMETER;\r
1066 }\r
1067\r
1068 //\r
1069 // Write return buffer\r
1070 //\r
1071 ASSERT(CpuSaveState != NULL);\r
b1bfdd65
LE
1072 CopyMem (\r
1073 Buffer,\r
1074 (UINT8 *)CpuSaveState + mSmmCpuWidthOffset[RegisterIndex].Offset32,\r
1075 Width\r
1076 );\r
4036b4e5
PB
1077 } else {\r
1078 //\r
b1bfdd65
LE
1079 // If 64-bit mode width is zero, then the specified register can not be\r
1080 // accessed\r
4036b4e5
PB
1081 //\r
1082 if (mSmmCpuWidthOffset[RegisterIndex].Width64 == 0) {\r
1083 return EFI_NOT_FOUND;\r
1084 }\r
1085\r
1086 //\r
b1bfdd65
LE
1087 // If Width is bigger than the 64-bit mode width, then the specified\r
1088 // register can not be accessed\r
4036b4e5
PB
1089 //\r
1090 if (Width > mSmmCpuWidthOffset[RegisterIndex].Width64) {\r
1091 return EFI_INVALID_PARAMETER;\r
1092 }\r
1093\r
1094 //\r
1095 // Write lower 32-bits of return buffer\r
1096 //\r
b1bfdd65
LE
1097 CopyMem (\r
1098 Buffer,\r
1099 (UINT8 *)CpuSaveState + mSmmCpuWidthOffset[RegisterIndex].Offset64Lo,\r
1100 MIN (4, Width)\r
1101 );\r
4036b4e5
PB
1102 if (Width >= 4) {\r
1103 //\r
1104 // Write upper 32-bits of return buffer\r
1105 //\r
b1bfdd65
LE
1106 CopyMem (\r
1107 (UINT8 *)Buffer + 4,\r
1108 (UINT8 *)CpuSaveState + mSmmCpuWidthOffset[RegisterIndex].Offset64Hi,\r
1109 Width - 4\r
1110 );\r
4036b4e5
PB
1111 }\r
1112 }\r
1113 return EFI_SUCCESS;\r
1114}\r
1115\r
86d71589
PB
1116/**\r
1117 Read an SMM Save State register on the target processor. If this function\r
1118 returns EFI_UNSUPPORTED, then the caller is responsible for reading the\r
1119 SMM Save Sate register.\r
1120\r
1121 @param[in] CpuIndex The index of the CPU to read the SMM Save State. The\r
1122 value must be between 0 and the NumberOfCpus field in\r
1123 the System Management System Table (SMST).\r
1124 @param[in] Register The SMM Save State register to read.\r
1125 @param[in] Width The number of bytes to read from the CPU save state.\r
1126 @param[out] Buffer Upon return, this holds the CPU register value read\r
1127 from the save state.\r
1128\r
1129 @retval EFI_SUCCESS The register was read from Save State.\r
1130 @retval EFI_INVALID_PARAMTER Buffer is NULL.\r
b1bfdd65
LE
1131 @retval EFI_UNSUPPORTED This function does not support reading\r
1132 Register.\r
86d71589
PB
1133**/\r
1134EFI_STATUS\r
1135EFIAPI\r
1136SmmCpuFeaturesReadSaveStateRegister (\r
1137 IN UINTN CpuIndex,\r
1138 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
1139 IN UINTN Width,\r
1140 OUT VOID *Buffer\r
1141 )\r
1142{\r
c1fcd80b
PB
1143 UINTN RegisterIndex;\r
1144 QEMU_SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
4036b4e5
PB
1145\r
1146 //\r
1147 // Check for special EFI_SMM_SAVE_STATE_REGISTER_LMA\r
1148 //\r
1149 if (Register == EFI_SMM_SAVE_STATE_REGISTER_LMA) {\r
1150 //\r
1151 // Only byte access is supported for this register\r
1152 //\r
1153 if (Width != 1) {\r
1154 return EFI_INVALID_PARAMETER;\r
1155 }\r
1156\r
c1fcd80b 1157 CpuSaveState = (QEMU_SMRAM_SAVE_STATE_MAP *)gSmst->CpuSaveState[CpuIndex];\r
4036b4e5
PB
1158\r
1159 //\r
1160 // Check CPU mode\r
1161 //\r
1162 if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) {\r
1163 *(UINT8 *)Buffer = 32;\r
1164 } else {\r
1165 *(UINT8 *)Buffer = 64;\r
1166 }\r
1167\r
1168 return EFI_SUCCESS;\r
1169 }\r
1170\r
1171 //\r
1172 // Check for special EFI_SMM_SAVE_STATE_REGISTER_IO\r
1173 //\r
1174 if (Register == EFI_SMM_SAVE_STATE_REGISTER_IO) {\r
1175 return EFI_NOT_FOUND;\r
1176 }\r
1177\r
1178 //\r
1179 // Convert Register to a register lookup table index. Let\r
1180 // PiSmmCpuDxeSmm implement other special registers (currently\r
1181 // there is only EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID).\r
1182 //\r
1183 RegisterIndex = GetRegisterIndex (Register);\r
1184 if (RegisterIndex == 0) {\r
b1bfdd65
LE
1185 return (Register < EFI_SMM_SAVE_STATE_REGISTER_IO ?\r
1186 EFI_NOT_FOUND :\r
1187 EFI_UNSUPPORTED);\r
4036b4e5
PB
1188 }\r
1189\r
1190 return ReadSaveStateRegisterByIndex (CpuIndex, RegisterIndex, Width, Buffer);\r
86d71589
PB
1191}\r
1192\r
1193/**\r
1194 Writes an SMM Save State register on the target processor. If this function\r
1195 returns EFI_UNSUPPORTED, then the caller is responsible for writing the\r
1196 SMM Save Sate register.\r
1197\r
1198 @param[in] CpuIndex The index of the CPU to write the SMM Save State. The\r
1199 value must be between 0 and the NumberOfCpus field in\r
1200 the System Management System Table (SMST).\r
1201 @param[in] Register The SMM Save State register to write.\r
1202 @param[in] Width The number of bytes to write to the CPU save state.\r
1203 @param[in] Buffer Upon entry, this holds the new CPU register value.\r
1204\r
1205 @retval EFI_SUCCESS The register was written to Save State.\r
1206 @retval EFI_INVALID_PARAMTER Buffer is NULL.\r
b1bfdd65
LE
1207 @retval EFI_UNSUPPORTED This function does not support writing\r
1208 Register.\r
86d71589
PB
1209**/\r
1210EFI_STATUS\r
1211EFIAPI\r
1212SmmCpuFeaturesWriteSaveStateRegister (\r
1213 IN UINTN CpuIndex,\r
1214 IN EFI_SMM_SAVE_STATE_REGISTER Register,\r
1215 IN UINTN Width,\r
1216 IN CONST VOID *Buffer\r
1217 )\r
1218{\r
c1fcd80b
PB
1219 UINTN RegisterIndex;\r
1220 QEMU_SMRAM_SAVE_STATE_MAP *CpuSaveState;\r
4036b4e5
PB
1221\r
1222 //\r
1223 // Writes to EFI_SMM_SAVE_STATE_REGISTER_LMA are ignored\r
1224 //\r
1225 if (Register == EFI_SMM_SAVE_STATE_REGISTER_LMA) {\r
1226 return EFI_SUCCESS;\r
1227 }\r
1228\r
1229 //\r
1230 // Writes to EFI_SMM_SAVE_STATE_REGISTER_IO are not supported\r
1231 //\r
1232 if (Register == EFI_SMM_SAVE_STATE_REGISTER_IO) {\r
1233 return EFI_NOT_FOUND;\r
1234 }\r
1235\r
1236 //\r
1237 // Convert Register to a register lookup table index. Let\r
1238 // PiSmmCpuDxeSmm implement other special registers (currently\r
1239 // there is only EFI_SMM_SAVE_STATE_REGISTER_PROCESSOR_ID).\r
1240 //\r
1241 RegisterIndex = GetRegisterIndex (Register);\r
1242 if (RegisterIndex == 0) {\r
b1bfdd65
LE
1243 return (Register < EFI_SMM_SAVE_STATE_REGISTER_IO ?\r
1244 EFI_NOT_FOUND :\r
1245 EFI_UNSUPPORTED);\r
4036b4e5
PB
1246 }\r
1247\r
c1fcd80b 1248 CpuSaveState = (QEMU_SMRAM_SAVE_STATE_MAP *)gSmst->CpuSaveState[CpuIndex];\r
4036b4e5
PB
1249\r
1250 //\r
1251 // Do not write non-writable SaveState, because it will cause exception.\r
b1bfdd65 1252 //\r
4036b4e5
PB
1253 if (!mSmmCpuWidthOffset[RegisterIndex].Writeable) {\r
1254 return EFI_UNSUPPORTED;\r
1255 }\r
1256\r
1257 //\r
1258 // Check CPU mode\r
1259 //\r
1260 if ((CpuSaveState->x86.SMMRevId & 0xFFFF) == 0) {\r
1261 //\r
b1bfdd65
LE
1262 // If 32-bit mode width is zero, then the specified register can not be\r
1263 // accessed\r
4036b4e5
PB
1264 //\r
1265 if (mSmmCpuWidthOffset[RegisterIndex].Width32 == 0) {\r
1266 return EFI_NOT_FOUND;\r
1267 }\r
1268\r
1269 //\r
b1bfdd65
LE
1270 // If Width is bigger than the 32-bit mode width, then the specified\r
1271 // register can not be accessed\r
4036b4e5
PB
1272 //\r
1273 if (Width > mSmmCpuWidthOffset[RegisterIndex].Width32) {\r
1274 return EFI_INVALID_PARAMETER;\r
1275 }\r
1276 //\r
1277 // Write SMM State register\r
1278 //\r
1279 ASSERT (CpuSaveState != NULL);\r
b1bfdd65
LE
1280 CopyMem (\r
1281 (UINT8 *)CpuSaveState + mSmmCpuWidthOffset[RegisterIndex].Offset32,\r
1282 Buffer,\r
1283 Width\r
1284 );\r
4036b4e5
PB
1285 } else {\r
1286 //\r
b1bfdd65
LE
1287 // If 64-bit mode width is zero, then the specified register can not be\r
1288 // accessed\r
4036b4e5
PB
1289 //\r
1290 if (mSmmCpuWidthOffset[RegisterIndex].Width64 == 0) {\r
1291 return EFI_NOT_FOUND;\r
1292 }\r
1293\r
1294 //\r
b1bfdd65
LE
1295 // If Width is bigger than the 64-bit mode width, then the specified\r
1296 // register can not be accessed\r
4036b4e5
PB
1297 //\r
1298 if (Width > mSmmCpuWidthOffset[RegisterIndex].Width64) {\r
1299 return EFI_INVALID_PARAMETER;\r
1300 }\r
1301\r
1302 //\r
1303 // Write lower 32-bits of SMM State register\r
1304 //\r
b1bfdd65
LE
1305 CopyMem (\r
1306 (UINT8 *)CpuSaveState + mSmmCpuWidthOffset[RegisterIndex].Offset64Lo,\r
1307 Buffer,\r
1308 MIN (4, Width)\r
1309 );\r
4036b4e5
PB
1310 if (Width >= 4) {\r
1311 //\r
1312 // Write upper 32-bits of SMM State register\r
1313 //\r
b1bfdd65
LE
1314 CopyMem (\r
1315 (UINT8 *)CpuSaveState + mSmmCpuWidthOffset[RegisterIndex].Offset64Hi,\r
1316 (UINT8 *)Buffer + 4,\r
1317 Width - 4\r
1318 );\r
4036b4e5
PB
1319 }\r
1320 }\r
1321 return EFI_SUCCESS;\r
86d71589
PB
1322}\r
1323\r
1324/**\r
1325 This function is hook point called after the gEfiSmmReadyToLockProtocolGuid\r
1326 notification is completely processed.\r
1327**/\r
1328VOID\r
1329EFIAPI\r
1330SmmCpuFeaturesCompleteSmmReadyToLock (\r
1331 VOID\r
1332 )\r
1333{\r
1334}\r
1335\r
1336/**\r
b1bfdd65
LE
1337 This API provides a method for a CPU to allocate a specific region for\r
1338 storing page tables.\r
86d71589
PB
1339\r
1340 This API can be called more once to allocate memory for page tables.\r
1341\r
b1bfdd65
LE
1342 Allocates the number of 4KB pages of type EfiRuntimeServicesData and returns\r
1343 a pointer to the allocated buffer. The buffer returned is aligned on a 4KB\r
1344 boundary. If Pages is 0, then NULL is returned. If there is not enough\r
1345 memory remaining to satisfy the request, then NULL is returned.\r
86d71589 1346\r
b1bfdd65
LE
1347 This function can also return NULL if there is no preference on where the\r
1348 page tables are allocated in SMRAM.\r
86d71589
PB
1349\r
1350 @param Pages The number of 4 KB pages to allocate.\r
1351\r
1352 @return A pointer to the allocated buffer for page tables.\r
1353 @retval NULL Fail to allocate a specific region for storing page tables,\r
b1bfdd65
LE
1354 Or there is no preference on where the page tables are\r
1355 allocated in SMRAM.\r
86d71589
PB
1356\r
1357**/\r
1358VOID *\r
1359EFIAPI\r
1360SmmCpuFeaturesAllocatePageTableMemory (\r
1361 IN UINTN Pages\r
1362 )\r
1363{\r
1364 return NULL;\r
1365}\r
1366\r