1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>
3 ; Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>
4 ; SPDX-License-Identifier: BSD-2-Clause-Patent
12 ; Code template of the SMI handler for a particular processor
14 ;-------------------------------------------------------------------------------
16 %include "StuffRsbNasm.inc"
19 %define MSR_IA32_S_CET 0x6A2
20 %define MSR_IA32_CET_SH_STK_EN 0x1
21 %define MSR_IA32_CET_WR_SHSTK_EN 0x2
22 %define MSR_IA32_CET_ENDBR_EN 0x4
23 %define MSR_IA32_CET_LEG_IW_EN 0x8
24 %define MSR_IA32_CET_NO_TRACK_EN 0x10
25 %define MSR_IA32_CET_SUPPRESS_DIS 0x20
26 %define MSR_IA32_CET_SUPPRESS 0x400
27 %define MSR_IA32_CET_TRACKER 0x800
28 %define MSR_IA32_PL0_SSP 0x6A4
30 %define CR4_CET 0x800000
32 %define MSR_IA32_MISC_ENABLE 0x1A0
33 %define MSR_EFER 0xc0000080
34 %define MSR_EFER_XD 0x800
37 ; Constants relating to PROCESSOR_SMM_DESCRIPTOR
39 %define DSC_OFFSET 0xfb00
40 %define DSC_GDTPTR 0x30
41 %define DSC_GDTSIZ 0x38
45 %define DSC_OTHERSEG 20
47 %define PROTECT_MODE_CS 0x8
48 %define PROTECT_MODE_DS 0x20
49 %define TSS_SEGMENT 0x40
51 extern ASM_PFX(SmiRendezvous)
52 extern ASM_PFX(FeaturePcdGet (PcdCpuSmmStackGuard))
53 extern ASM_PFX(CpuSmmDebugEntry)
54 extern ASM_PFX(CpuSmmDebugExit)
56 global ASM_PFX(gcSmiHandlerTemplate)
57 global ASM_PFX(gcSmiHandlerSize)
58 global ASM_PFX(gPatchSmiCr3)
59 global ASM_PFX(gPatchSmiStack)
60 global ASM_PFX(gPatchSmbase)
61 extern ASM_PFX(mXdSupported)
62 global ASM_PFX(gPatchXdSupported)
63 global ASM_PFX(gPatchMsrIa32MiscEnableSupported)
64 extern ASM_PFX(gSmiHandlerIdtr)
66 extern ASM_PFX(mCetSupported)
67 global ASM_PFX(mPatchCetSupported)
68 global ASM_PFX(mPatchCetPl0Ssp)
69 global ASM_PFX(mPatchCetInterruptSsp)
74 ASM_PFX(gcSmiHandlerTemplate):
76 mov bx, _GdtDesc - _SmiEntryPoint + 0x8000
77 mov ax,[cs:DSC_OFFSET + DSC_GDTSIZ]
80 mov eax, [cs:DSC_OFFSET + DSC_GDTPTR]
82 mov ebp, eax ; ebp = GDT base
83 o32 lgdt [cs:bx] ; lgdt fword ptr cs:[bx]
84 mov ax, PROTECT_MODE_CS
86 mov edi, strict dword 0 ; source operand will be patched
87 ASM_PFX(gPatchSmbase):
88 lea eax, [edi + (@32bit - _SmiEntryPoint) + 0x8000]
101 mov ax, PROTECT_MODE_DS
107 mov esp, strict dword 0 ; source operand will be patched
108 ASM_PFX(gPatchSmiStack):
109 mov eax, ASM_PFX(gSmiHandlerIdtr)
114 mov eax, strict dword 0 ; source operand will be patched
115 ASM_PFX(gPatchSmiCr3):
118 ; Need to test for CR4 specific bit support
121 cpuid ; use CPUID to determine if specific CR4 bits are supported
122 xor eax, eax ; Clear EAX
123 test edx, BIT2 ; Check for DE capabilities
127 test edx, BIT6 ; Check for PAE capabilities
131 test edx, BIT7 ; Check for MCE capabilities
135 test edx, BIT24 ; Check for FXSR capabilities
139 test edx, BIT25 ; Check for SSE capabilities
142 .4: ; as cr4.PGE is not set here, refresh cr3
143 mov cr4, eax ; in PreModifyMtrrs() to flush TLB.
145 cmp byte [dword ASM_PFX(FeaturePcdGet (PcdCpuSmmStackGuard))], 0
148 mov byte [ebp + TSS_SEGMENT + 5], 0x89 ; clear busy flag
153 ; enable NXE if supported
154 mov al, strict byte 1 ; source operand may be patched
155 ASM_PFX(gPatchXdSupported):
159 ; If MSR_IA32_MISC_ENABLE is supported, clear XD Disable bit
160 mov al, strict byte 1 ; source operand may be patched
161 ASM_PFX(gPatchMsrIa32MiscEnableSupported):
163 jz MsrIa32MiscEnableSupported
165 ; MSR_IA32_MISC_ENABLE not supported
167 push edx ; don't try to restore the XD Disable bit just before RSM
171 ; Check XD disable bit
173 MsrIa32MiscEnableSupported:
174 mov ecx, MSR_IA32_MISC_ENABLE
176 push edx ; save MSR_IA32_MISC_ENABLE[63-32]
177 test edx, BIT2 ; MSR_IA32_MISC_ENABLE[34]
179 and dx, 0xFFFB ; clear XD Disable bit if it is set
184 or ax, MSR_EFER_XD ; enable NXE
192 or ebx, 0x80010023 ; enable paging + WP + NE + MP + PE
194 lea ebx, [edi + DSC_OFFSET]
195 mov ax, [ebx + DSC_DS]
197 mov ax, [ebx + DSC_OTHERSEG]
201 mov ax, [ebx + DSC_SS]
204 mov ebx, [esp + 4] ; ebx <- CpuIndex
206 ; enable CET if supported
207 mov al, strict byte 1 ; source operand may be patched
208 ASM_PFX(mPatchCetSupported):
212 mov ecx, MSR_IA32_S_CET
217 mov ecx, MSR_IA32_PL0_SSP
222 mov ecx, MSR_IA32_S_CET
223 mov eax, MSR_IA32_CET_SH_STK_EN
227 mov ecx, MSR_IA32_PL0_SSP
228 mov eax, strict dword 0 ; source operand will be patched
229 ASM_PFX(mPatchCetPl0Ssp):
233 btr ecx, 16 ; clear WP
235 mov [eax], eax ; reload SSP, and clear busyflag.
239 mov eax, strict dword 0 ; source operand will be patched
240 ASM_PFX(mPatchCetInterruptSsp):
243 mov [eax], eax ; reload SSP, and clear busyflag.
252 mov eax, 0x668 | CR4_CET
260 mov eax, ASM_PFX(CpuSmmDebugEntry)
265 mov eax, ASM_PFX(SmiRendezvous)
270 mov eax, ASM_PFX(CpuSmmDebugExit)
274 mov eax, ASM_PFX(mCetSupported)
280 mov cr4, eax ; disable CET
282 mov ecx, MSR_IA32_PL0_SSP
287 mov ecx, MSR_IA32_S_CET
293 mov eax, ASM_PFX(mXdSupported)
297 pop edx ; get saved MSR_IA32_MISC_ENABLE[63-32]
300 mov ecx, MSR_IA32_MISC_ENABLE
302 or dx, BIT2 ; set XD Disable bit if it was set before entering into SMM
310 ASM_PFX(gcSmiHandlerSize): DW $ - _SmiEntryPoint
312 global ASM_PFX(PiSmmCpuSmiEntryFixupAddress)
313 ASM_PFX(PiSmmCpuSmiEntryFixupAddress):