1 #------------------------------------------------------------------------------
3 # Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
4 # This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php.
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 # Code template of the SMI handler for a particular processor
20 #------------------------------------------------------------------------------
22 ASM_GLOBAL ASM_PFX(gcSmiHandlerTemplate)
23 ASM_GLOBAL ASM_PFX(gcSmiHandlerSize)
24 ASM_GLOBAL ASM_PFX(gSmiCr3)
25 ASM_GLOBAL ASM_PFX(gSmiStack)
26 ASM_GLOBAL ASM_PFX(gSmbase)
27 ASM_GLOBAL ASM_PFX(gSmiHandlerIdtr)
30 # Constants relating to PROCESSOR_SMM_DESCRIPTOR
32 .equ DSC_OFFSET, 0xfb00
40 # Constants relating to CPU State Save Area
45 .equ PROTECT_MODE_CS, 0x08
46 .equ PROTECT_MODE_DS, 0x20
47 .equ LONG_MODE_CS, 0x38
48 .equ TSS_SEGMENT, 0x40
53 ASM_PFX(gcSmiHandlerTemplate):
57 # The encoding of BX in 16-bit addressing mode is the same as of RDI in 64-
58 # bit addressing mode. And that coincidence has been used in the following
59 # "64-bit like" 16-bit code. Be aware that once RDI is referenced as a
60 # base address register, it is actually BX that is referenced.
62 .byte 0xbb # mov bx, imm16
63 .word _GdtDesc - _SmiEntryPoint + 0x8000
67 .byte 0x2e,0xa1 # mov ax, cs:[offset16]
68 .word DSC_OFFSET + DSC_GDTSIZ
71 movl %eax, (%rdi) # mov cs:[bx], ax
72 .byte 0x66,0x2e,0xa1 # mov eax, cs:[offset16]
73 .word DSC_OFFSET + DSC_GDTPTR
79 # Patch ProtectedMode Segment
86 # Patch ProtectedMode entry
88 .byte 0x66, 0xbf # mov edi, SMBASE
89 ASM_PFX(gSmbase): .space 4
90 lea ((ProtectedMode - _SmiEntryPoint) + 0x8000)(%edi), %ax
94 # Switch into ProtectedMode
98 andl $0x9ffafff3, %ebx
100 orl $0x00000023, %ebx
109 movw $PROTECT_MODE_DS, %ax
115 .byte 0xbc # mov esp, imm32
116 ASM_PFX(gSmiStack): .space 4
121 ASM_PFX(gSmiCr3): .space 4
123 movl $0x668,%eax # as cr4.PGE is not set here, refresh cr3
124 movq %rax, %cr4 # in PreModifyMtrrs() to flush TLB.
126 subl $8, %esp # reserve room in stack
128 movl 2(%rsp), %eax # eax = GDT base
131 movb %dl, (TSS_SEGMENT + 5)(%rax) # clear busy flag
132 movl $TSS_SEGMENT, %eax
138 pushq $LONG_MODE_CS # push cs hardcore here
139 call Base # push return address for retf later
141 addl $(LongMode - Base), (%rsp) # offset for far retf, seg is the 1st arg
142 movl $0xc0000080, %ecx
147 orl $0x080010000, %ebx # enable paging + WP
150 LongMode: # long mode (64-bit code) starts here
151 movabsq $ASM_PFX(gSmiHandlerIdtr), %rax
153 lea (DSC_OFFSET)(%rdi), %ebx
154 movw DSC_DS(%rbx), %ax
156 movw DSC_OTHERSEG(%rbx), %ax
160 movw DSC_SS(%rbx), %ax
162 # jmp _SmiHandler ; instruction is not needed
169 .byte 0x48 # FXSAVE64
175 movabsq $ASM_PFX(CpuSmmDebugEntry), %rax
179 movabsq $ASM_PFX(SmiRendezvous), %rax
183 movabsq $ASM_PFX(CpuSmmDebugExit), %rax
189 # Restore FP registers
191 .byte 0x48 # FXRSTOR64
196 ASM_PFX(gcSmiHandlerSize): .word . - _SmiEntryPoint