1 | /** @file
|
---|
2 | Config SMRAM Save State for SmmBases Relocation.
|
---|
3 |
|
---|
4 | Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 | #include "InternalSmmRelocationLib.h"
|
---|
9 | #include <Library/CpuLib.h>
|
---|
10 |
|
---|
11 | /**
|
---|
12 | Get the mode of the CPU at the time an SMI occurs
|
---|
13 |
|
---|
14 | @retval EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT 32 bit.
|
---|
15 | @retval EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT 64 bit.
|
---|
16 |
|
---|
17 | **/
|
---|
18 | UINT8
|
---|
19 | GetMmSaveStateRegisterLma (
|
---|
20 | VOID
|
---|
21 | )
|
---|
22 | {
|
---|
23 | CPUID_VERSION_INFO_EAX RegEax;
|
---|
24 | CPUID_EXTENDED_CPU_SIG_EDX RegEdx;
|
---|
25 | UINTN FamilyId;
|
---|
26 | UINTN ModelId;
|
---|
27 | UINT32 Eax;
|
---|
28 | UINT8 SmmSaveStateRegisterLma;
|
---|
29 |
|
---|
30 | //
|
---|
31 | // Determine the mode of the CPU at the time an SMI occurs
|
---|
32 | // Intel(R) 64 and IA-32 Architectures Software Developer's Manual
|
---|
33 | // Volume 3C, Section 34.4.1.1
|
---|
34 | //
|
---|
35 | RegEax.Uint32 = GetCpuFamilyModel ();
|
---|
36 | FamilyId = RegEax.Bits.FamilyId;
|
---|
37 | ModelId = RegEax.Bits.Model;
|
---|
38 | if ((FamilyId == 0x06) || (FamilyId == 0x0f)) {
|
---|
39 | ModelId = ModelId | RegEax.Bits.ExtendedModelId << 4;
|
---|
40 | }
|
---|
41 |
|
---|
42 | RegEdx.Uint32 = 0;
|
---|
43 | AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL);
|
---|
44 | if (Eax >= CPUID_EXTENDED_CPU_SIG) {
|
---|
45 | AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &(RegEdx.Uint32));
|
---|
46 | }
|
---|
47 |
|
---|
48 | SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT;
|
---|
49 | if (RegEdx.Bits.LM) {
|
---|
50 | SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (FamilyId == 0x06) {
|
---|
54 | if ((ModelId == 0x17) || (ModelId == 0x0f) || (ModelId == 0x1c)) {
|
---|
55 | SmmSaveStateRegisterLma = (UINT8)EFI_MM_SAVE_STATE_REGISTER_LMA_64BIT;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | return SmmSaveStateRegisterLma;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | This function configures the SmBase on the currently executing CPU.
|
---|
64 |
|
---|
65 | @param[in] SmBase The SmBase on the currently executing CPU.
|
---|
66 |
|
---|
67 | **/
|
---|
68 | VOID
|
---|
69 | EFIAPI
|
---|
70 | ConfigureSmBase (
|
---|
71 | IN UINT64 SmBase
|
---|
72 | )
|
---|
73 | {
|
---|
74 | SMRAM_SAVE_STATE_MAP *CpuState;
|
---|
75 |
|
---|
76 | CpuState = (SMRAM_SAVE_STATE_MAP *)(UINTN)(SMM_DEFAULT_SMBASE + SMRAM_SAVE_STATE_MAP_OFFSET);
|
---|
77 |
|
---|
78 | CpuState->x86.SMBASE = (UINT32)SmBase;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | Hook the code executed immediately after an RSM instruction on the currently
|
---|
83 | executing CPU. The mode of code executed immediately after RSM must be
|
---|
84 | detected, and the appropriate hook must be selected. Always clear the auto
|
---|
85 | HALT restart flag if it is set.
|
---|
86 |
|
---|
87 | @param[in,out] CpuState Pointer to SMRAM Save State Map for the
|
---|
88 | currently executing CPU.
|
---|
89 | @param[in] NewInstructionPointer32 Instruction pointer to use if resuming to
|
---|
90 | 32-bit mode from 64-bit SMM.
|
---|
91 | @param[in] NewInstructionPointer Instruction pointer to use if resuming to
|
---|
92 | same mode as SMM.
|
---|
93 |
|
---|
94 | @retval The value of the original instruction pointer before it was hooked.
|
---|
95 |
|
---|
96 | **/
|
---|
97 | UINT64
|
---|
98 | EFIAPI
|
---|
99 | HookReturnFromSmm (
|
---|
100 | IN OUT SMRAM_SAVE_STATE_MAP *CpuState,
|
---|
101 | IN UINT64 NewInstructionPointer32,
|
---|
102 | IN UINT64 NewInstructionPointer
|
---|
103 | )
|
---|
104 | {
|
---|
105 | UINT64 OriginalInstructionPointer;
|
---|
106 |
|
---|
107 | if (GetMmSaveStateRegisterLma () == EFI_MM_SAVE_STATE_REGISTER_LMA_32BIT) {
|
---|
108 | OriginalInstructionPointer = (UINT64)CpuState->x86._EIP;
|
---|
109 | CpuState->x86._EIP = (UINT32)NewInstructionPointer;
|
---|
110 |
|
---|
111 | //
|
---|
112 | // Clear the auto HALT restart flag so the RSM instruction returns
|
---|
113 | // program control to the instruction following the HLT instruction.
|
---|
114 | //
|
---|
115 | if ((CpuState->x86.AutoHALTRestart & BIT0) != 0) {
|
---|
116 | CpuState->x86.AutoHALTRestart &= ~BIT0;
|
---|
117 | }
|
---|
118 | } else {
|
---|
119 | OriginalInstructionPointer = CpuState->x64._RIP;
|
---|
120 | if ((CpuState->x64.IA32_EFER & LMA) == 0) {
|
---|
121 | CpuState->x64._RIP = (UINT32)NewInstructionPointer32;
|
---|
122 | } else {
|
---|
123 | CpuState->x64._RIP = (UINT32)NewInstructionPointer;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //
|
---|
127 | // Clear the auto HALT restart flag so the RSM instruction returns
|
---|
128 | // program control to the instruction following the HLT instruction.
|
---|
129 | //
|
---|
130 | if ((CpuState->x64.AutoHALTRestart & BIT0) != 0) {
|
---|
131 | CpuState->x64.AutoHALTRestart &= ~BIT0;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return OriginalInstructionPointer;
|
---|
136 | }
|
---|