1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
3 ; This program and the accompanying materials
4 ; are licensed and made available under the terms and conditions of the BSD License
5 ; which accompanies this distribution. The full text of the license may be found at
6 ; http://opensource.org/licenses/bsd-license.php.
8 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 ; Code template of the SMI handler for a particular processor
19 ;-------------------------------------------------------------------------------
22 ; Variables referrenced by C code
25 %define MSR_IA32_MISC_ENABLE 0x1A0
26 %define MSR_EFER 0xc0000080
27 %define MSR_EFER_XD 0x800
30 ; Constants relating to TXT_PROCESSOR_SMM_DESCRIPTOR
32 %define DSC_OFFSET 0xfb00
33 %define DSC_GDTPTR 0x48
34 %define DSC_GDTSIZ 0x50
38 %define DSC_OTHERSEG 0x1a
40 ; Constants relating to CPU State Save Area
42 %define SSM_DR6 0xffd0
43 %define SSM_DR7 0xffc8
45 %define PROTECT_MODE_CS 0x8
46 %define PROTECT_MODE_DS 0x20
47 %define LONG_MODE_CS 0x38
48 %define TSS_SEGMENT 0x40
51 extern ASM_PFX(SmiRendezvous)
52 extern ASM_PFX(gStmSmiHandlerIdtr)
53 extern ASM_PFX(CpuSmmDebugEntry)
54 extern ASM_PFX(CpuSmmDebugExit)
56 global ASM_PFX(gStmSmbase)
57 global ASM_PFX(gStmXdSupported)
58 global ASM_PFX(gStmSmiStack)
59 global ASM_PFX(gStmSmiCr3)
60 global ASM_PFX(gcStmSmiHandlerTemplate)
61 global ASM_PFX(gcStmSmiHandlerSize)
62 global ASM_PFX(gcStmSmiHandlerOffset)
64 ASM_PFX(gStmSmbase) EQU StmSmbasePatch - 4
65 ASM_PFX(gStmSmiStack) EQU StmSmiStackPatch - 4
66 ASM_PFX(gStmSmiCr3) EQU StmSmiCr3Patch - 4
67 ASM_PFX(gStmXdSupported) EQU StmXdSupportedPatch - 1
73 ASM_PFX(gcStmSmiHandlerTemplate):
75 mov bx, _StmGdtDesc - _StmSmiEntryPoint + 0x8000
76 mov ax,[cs:DSC_OFFSET + DSC_GDTSIZ]
79 mov eax, [cs:DSC_OFFSET + DSC_GDTPTR]
81 o32 lgdt [cs:bx] ; lgdt fword ptr cs:[bx]
82 mov ax, PROTECT_MODE_CS
84 o32 mov edi, strict dword 0
86 lea eax, [edi + (@ProtectedMode - _StmSmiEntryPoint) + 0x8000]
99 mov ax, PROTECT_MODE_DS
105 mov esp, strict dword 0
111 mov eax, strict dword 0
114 mov eax, 0x668 ; as cr4.PGE is not set here, refresh cr3
115 mov cr4, rax ; in PreModifyMtrrs() to flush TLB.
117 sub esp, 8 ; reserve room in stack
119 mov eax, [rsp + 2] ; eax = GDT base
122 mov [rax + TSS_SEGMENT + 5], dl ; clear busy flag
126 ; enable NXE if supported
127 mov al, strict byte 1
132 ; Check XD disable bit
134 mov ecx, MSR_IA32_MISC_ENABLE
137 push rdx ; save MSR_IA32_MISC_ENABLE[63-32]
138 test edx, BIT2 ; MSR_IA32_MISC_ENABLE[34]
140 and dx, 0xFFFB ; clear XD Disable bit if it is set
145 or ax, MSR_EFER_XD ; enable NXE
152 ; Switch into @LongMode
153 push LONG_MODE_CS ; push cs hardcore here
154 call Base ; push return address for retf later
156 add dword [rsp], @LongMode - Base; offset for far retf, seg is the 1st arg
160 or ah, 1 ; enable LME
163 or ebx, 0x80010023 ; enable paging + WP + NE + MP + PE
166 @LongMode: ; long mode (64-bit code) starts here
167 mov rax, ASM_PFX(gStmSmiHandlerIdtr)
169 lea ebx, [rdi + DSC_OFFSET]
170 mov ax, [rbx + DSC_DS]
172 mov ax, [rbx + DSC_OTHERSEG]
176 mov ax, [rbx + DSC_SS]
180 mov rbx, [rsp + 0x08] ; rbx <- CpuIndex
191 mov rax, ASM_PFX(CpuSmmDebugEntry)
195 mov rax, ASM_PFX(SmiRendezvous) ; rax <- absolute addr of SmiRedezvous
199 mov rax, ASM_PFX(CpuSmmDebugExit)
205 ; Restore FP registers
211 mov rax, ASM_PFX(gStmXdSupported)
215 pop rdx ; get saved MSR_IA32_MISC_ENABLE[63-32]
218 mov ecx, MSR_IA32_MISC_ENABLE
220 or dx, BIT2 ; set XD Disable bit if it was set before entering into SMM
228 ; Check XD disable bit
231 mov rax, ASM_PFX(gStmXdSupported)
235 mov ecx, MSR_IA32_MISC_ENABLE
237 mov r8, rdx ; save MSR_IA32_MISC_ENABLE[63-32]
238 test edx, BIT2 ; MSR_IA32_MISC_ENABLE[34]
240 and dx, 0xFFFB ; clear XD Disable bit if it is set
245 or ax, MSR_EFER_XD ; enable NXE
250 ; below step is needed, because STM does not run above code.
251 ; we have to run below code to set IDT/CR0/CR4
253 mov rax, ASM_PFX(gStmSmiHandlerIdtr)
257 or eax, 0x80010023 ; enable paging + WP + NE + MP + PE
260 mov eax, 0x668 ; as cr4.PGE is not set here, refresh cr3
261 mov cr4, rax ; in PreModifyMtrrs() to flush TLB.
265 ASM_PFX(gcStmSmiHandlerSize) : DW $ - _StmSmiEntryPoint
266 ASM_PFX(gcStmSmiHandlerOffset) : DW _StmSmiHandler - _StmSmiEntryPoint