1 ;------------------------------------------------------------------------------ ;
2 ; Copyright (c) 2009 - 2015, 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 referenced by C code
24 EXTERNDEF SmiRendezvous:PROC
25 EXTERNDEF gcSmiHandlerTemplate:BYTE
26 EXTERNDEF gcSmiHandlerSize:WORD
27 EXTERNDEF gSmiCr3:DWORD
28 EXTERNDEF gSmiStack:DWORD
29 EXTERNDEF gSmbase:DWORD
30 EXTERNDEF FeaturePcdGet (PcdCpuSmmDebug):BYTE
31 EXTERNDEF gSmiHandlerIdtr:FWORD
35 ; Constants relating to PROCESSOR_SMM_DESCRIPTOR
45 ; Constants relating to CPU State Save Area
50 PROTECT_MODE_CS EQU 08h
51 PROTECT_MODE_DS EQU 20h
58 gcSmiHandlerTemplate LABEL BYTE
62 ; The encoding of BX in 16-bit addressing mode is the same as of RDI in 64-
63 ; bit addressing mode. And that coincidence has been used in the following
64 ; "64-bit like" 16-bit code. Be aware that once RDI is referenced as a
65 ; base address register, it is actually BX that is referenced.
67 DB 0bbh ; mov bx, imm16
68 DW offset _GdtDesc - _SmiEntryPoint + 8000h ; bx = GdtDesc offset
70 DB 2eh, 0a1h ; mov ax, cs:[offset16]
71 DW DSC_OFFSET + DSC_GDTSIZ
74 mov [rdi], eax ; mov cs:[bx], ax
75 DB 66h, 2eh, 0a1h ; mov eax, cs:[offset16]
76 DW DSC_OFFSET + DSC_GDTPTR
78 mov [rdi + 2], ax ; mov cs:[bx + 2], eax
80 lgdt fword ptr [rdi] ; lgdt fword ptr cs:[bx]
81 ; Patch ProtectedMode Segment
82 DB 0b8h ; mov ax, imm16
83 DW PROTECT_MODE_CS ; set AX for segment directly
85 mov [rdi - 2], eax ; mov cs:[bx - 2], ax
86 ; Patch ProtectedMode entry
87 DB 66h, 0bfh ; mov edi, SMBASE
89 lea ax, [edi + (@ProtectedMode - _SmiEntryPoint) + 8000h]
91 mov [rdi - 6], ax ; mov cs:[bx - 6], eax
92 ; Switch into @ProtectedMode
106 mov ax, PROTECT_MODE_DS
112 DB 0bch ; mov esp, imm32
117 DB 0b8h ; mov eax, offset gSmiCr3
120 mov eax, 668h ; as cr4.PGE is not set here, refresh cr3
121 mov cr4, rax ; in PreModifyMtrrs() to flush TLB.
123 sub esp, 8 ; reserve room in stack
125 mov eax, [rsp + 2] ; eax = GDT base
129 mov [rax + TSS_SEGMENT + 2], dl
130 mov [rax + TSS_SEGMENT + 3], dh
131 DB 0c1h, 0eah, 10h ; shr edx, 16
132 mov [rax + TSS_SEGMENT + 4], dl
133 mov [rax + TSS_SEGMENT + 7], dh
136 mov [rax + TSS_SEGMENT + 5], dl ; clear busy flag
140 ; Switch into @LongMode
141 push LONG_MODE_CS ; push cs hardcore here
142 call Base ; push return address for retf later
144 add dword ptr [rsp], @LongMode - Base; offset for far retf, seg is the 1st arg
153 @LongMode: ; long mode (64-bit code) starts here
154 mov rax, offset gSmiHandlerIdtr
156 lea ebx, [rdi + DSC_OFFSET]
157 mov ax, [rbx + DSC_DS]
159 mov ax, [rbx + DSC_OTHERSEG]
163 mov ax, [rbx + DSC_SS]
165 ; jmp _SmiHandler ; instruction is not needed
169 ; The following lines restore DR6 & DR7 before running C code. They are useful
170 ; when you want to enable hardware breakpoints in SMM.
172 ; NOTE: These lines might not be appreciated in runtime since they might
173 ; conflict with OS debugging facilities. Turn them off in RELEASE.
175 mov rax, offset FeaturePcdGet (PcdCpuSmmDebug) ;Get absolute address. Avoid RIP relative addressing
176 cmp byte ptr [rax], 0
179 DB 48h, 8bh, 0dh ; mov rcx, [rip + disp32]
180 DD SSM_DR6 - ($ + 4 - _SmiEntryPoint + 8000h)
181 DB 48h, 8bh, 15h ; mov rdx, [rip + disp32]
182 DD SSM_DR7 - ($ + 4 - _SmiEntryPoint + 8000h)
186 mov rcx, [rsp] ; rcx <- CpuIndex
187 mov rax, SmiRendezvous ; rax <- absolute addr of SmiRedezvous
201 ; Restore FP registers
206 mov rax, offset FeaturePcdGet (PcdCpuSmmDebug) ;Get absolute address. Avoid RIP relative addressing
207 cmp byte ptr [rax], 0
212 DB 48h, 89h, 15h ; mov [rip + disp32], rdx
213 DD SSM_DR7 - ($ + 4 - _SmiEntryPoint + 8000h)
214 DB 48h, 89h, 0dh ; mov [rip + disp32], rcx
215 DD SSM_DR6 - ($ + 4 - _SmiEntryPoint + 8000h)
219 gcSmiHandlerSize DW $ - _SmiEntryPoint