+++ /dev/null
-#------------------------------------------------------------------------------\r
-#\r
-# Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution. The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php.\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-#\r
-# Module Name:\r
-#\r
-# SmiEntry.S\r
-#\r
-# Abstract:\r
-#\r
-# Code template of the SMI handler for a particular processor\r
-#\r
-#------------------------------------------------------------------------------\r
-\r
-ASM_GLOBAL ASM_PFX(gcStmSmiHandlerTemplate)\r
-ASM_GLOBAL ASM_PFX(gcStmSmiHandlerSize)\r
-ASM_GLOBAL ASM_PFX(gcStmSmiHandlerOffset)\r
-ASM_GLOBAL ASM_PFX(gStmSmiCr3)\r
-ASM_GLOBAL ASM_PFX(gStmSmiStack)\r
-ASM_GLOBAL ASM_PFX(gStmSmbase)\r
-ASM_GLOBAL ASM_PFX(gStmXdSupported)\r
-ASM_GLOBAL ASM_PFX(gStmSmiHandlerIdtr)\r
-\r
-.equ MSR_IA32_MISC_ENABLE, 0x1A0\r
-.equ MSR_EFER, 0xc0000080\r
-.equ MSR_EFER_XD, 0x800\r
-\r
-#\r
-# Constants relating to TXT_PROCESSOR_SMM_DESCRIPTOR\r
-#\r
-.equ DSC_OFFSET, 0xfb00\r
-.equ DSC_GDTPTR, 0x48\r
-.equ DSC_GDTSIZ, 0x50\r
-.equ DSC_CS, 0x14\r
-.equ DSC_DS, 0x16\r
-.equ DSC_SS, 0x18\r
-.equ DSC_OTHERSEG, 0x1a\r
-#\r
-# Constants relating to CPU State Save Area\r
-#\r
-.equ SSM_DR6, 0xffd0\r
-.equ SSM_DR7, 0xffc8\r
-\r
-.equ PROTECT_MODE_CS, 0x08\r
-.equ PROTECT_MODE_DS, 0x20\r
-.equ LONG_MODE_CS, 0x38\r
-.equ TSS_SEGMENT, 0x40\r
-.equ GDT_SIZE, 0x50\r
-\r
- .text\r
-\r
-ASM_PFX(gcStmSmiHandlerTemplate):\r
-\r
-_StmSmiEntryPoint:\r
- #\r
- # The encoding of BX in 16-bit addressing mode is the same as of RDI in 64-\r
- # bit addressing mode. And that coincidence has been used in the following\r
- # "64-bit like" 16-bit code. Be aware that once RDI is referenced as a\r
- # base address register, it is actually BX that is referenced.\r
- #\r
- .byte 0xbb # mov bx, imm16\r
- .word _StmGdtDesc - _StmSmiEntryPoint + 0x8000\r
- #\r
- # fix GDT descriptor\r
- #\r
- .byte 0x2e,0xa1 # mov ax, cs:[offset16]\r
- .word DSC_OFFSET + DSC_GDTSIZ\r
- .byte 0x48 # dec ax\r
- .byte 0x2e\r
- movl %eax, (%rdi) # mov cs:[bx], ax\r
- .byte 0x66,0x2e,0xa1 # mov eax, cs:[offset16]\r
- .word DSC_OFFSET + DSC_GDTPTR\r
- .byte 0x2e\r
- movw %ax, 2(%rdi)\r
- .byte 0x66,0x2e\r
- lgdt (%rdi)\r
- #\r
- # Patch ProtectedMode Segment\r
- #\r
- .byte 0xb8\r
- .word PROTECT_MODE_CS\r
- .byte 0x2e\r
- movl %eax, -2(%rdi)\r
- #\r
- # Patch ProtectedMode entry\r
- #\r
- .byte 0x66, 0xbf # mov edi, SMBASE\r
-ASM_PFX(gStmSmbase): .space 4\r
- lea ((ProtectedMode - _StmSmiEntryPoint) + 0x8000)(%edi), %ax\r
- .byte 0x2e\r
- movw %ax, -6(%rdi)\r
- #\r
- # Switch into ProtectedMode\r
- #\r
- movq %cr0, %rbx\r
- .byte 0x66\r
- andl $0x9ffafff3, %ebx\r
- .byte 0x66\r
- orl $0x00000023, %ebx\r
-\r
- movq %rbx, %cr0\r
- .byte 0x66, 0xea\r
- .space 6\r
-\r
-_StmGdtDesc: .space 6\r
-\r
-ProtectedMode:\r
- movw $PROTECT_MODE_DS, %ax\r
- movl %eax, %ds\r
- movl %eax, %es\r
- movl %eax, %fs\r
- movl %eax, %gs\r
- movl %eax, %ss\r
- .byte 0xbc # mov esp, imm32\r
-ASM_PFX(gStmSmiStack): .space 4\r
- jmp ProtFlatMode\r
-\r
-ProtFlatMode:\r
- .byte 0xb8\r
-ASM_PFX(gStmSmiCr3): .space 4\r
- movq %rax, %cr3\r
- movl $0x668,%eax # as cr4.PGE is not set here, refresh cr3\r
- movq %rax, %cr4 # in PreModifyMtrrs() to flush TLB.\r
-# Load TSS\r
- subl $8, %esp # reserve room in stack\r
- sgdt (%rsp)\r
- movl 2(%rsp), %eax # eax = GDT base\r
- addl $8, %esp\r
- movb $0x89, %dl\r
- movb %dl, (TSS_SEGMENT + 5)(%rax) # clear busy flag\r
- movl $TSS_SEGMENT, %eax\r
- ltr %ax\r
-\r
-# enable NXE if supported\r
- .byte 0xb0 # mov al, imm8\r
-ASM_PFX(gStmXdSupported): .byte 1\r
- cmpb $0, %al\r
- jz SkipXd\r
-#\r
-# Check XD disable bit\r
-#\r
- movl $MSR_IA32_MISC_ENABLE, %ecx\r
- rdmsr\r
- subl $4, %esp\r
- pushq %rdx # save MSR_IA32_MISC_ENABLE[63-32]\r
- testl $BIT2, %edx # MSR_IA32_MISC_ENABLE[34]\r
- jz L13\r
- andw $0x0FFFB, %dx # clear XD Disable bit if it is set\r
- wrmsr\r
-L13:\r
- movl $MSR_EFER, %ecx\r
- rdmsr\r
- orw $MSR_EFER_XD,%ax # enable NXE\r
- wrmsr\r
- jmp XdDone\r
-SkipXd:\r
- subl $8, %esp\r
-XdDone:\r
-\r
- #\r
- # Switch to LongMode\r
- #\r
- pushq $LONG_MODE_CS # push cs hardcore here\r
- call Base # push return address for retf later\r
-Base:\r
- addl $(LongMode - Base), (%rsp) # offset for far retf, seg is the 1st arg\r
-\r
- movl $MSR_EFER, %ecx\r
- rdmsr\r
- orb $1,%ah # enable LME\r
- wrmsr\r
- movq %cr0, %rbx\r
- orl $0x080010023, %ebx # enable paging + WP + NE + MP + PE\r
- movq %rbx, %cr0\r
- retf\r
-LongMode: # long mode (64-bit code) starts here\r
- movabsq $ASM_PFX(gStmSmiHandlerIdtr), %rax\r
- lidt (%rax)\r
- lea (DSC_OFFSET)(%rdi), %ebx\r
- movw DSC_DS(%rbx), %ax\r
- movl %eax,%ds\r
- movw DSC_OTHERSEG(%rbx), %ax\r
- movl %eax,%es\r
- movl %eax,%fs\r
- movl %eax,%gs\r
- movw DSC_SS(%rbx), %ax\r
- movl %eax,%ss\r
-\r
-CommonHandler:\r
- movq 8(%rsp), %rbx\r
- # Save FP registers\r
-\r
- subq $0x200, %rsp\r
- .byte 0x48 # FXSAVE64\r
- fxsave (%rsp)\r
-\r
- addq $-0x20, %rsp\r
-\r
- movq %rbx, %rcx\r
- movabsq $ASM_PFX(CpuSmmDebugEntry), %rax\r
- call *%rax\r
-\r
- movq %rbx, %rcx\r
- movabsq $ASM_PFX(SmiRendezvous), %rax\r
- call *%rax\r
-\r
- movq %rbx, %rcx\r
- movabsq $ASM_PFX(CpuSmmDebugExit), %rax\r
- call *%rax\r
-\r
- addq $0x20, %rsp\r
-\r
- #\r
- # Restore FP registers\r
- #\r
- .byte 0x48 # FXRSTOR64\r
- fxrstor (%rsp)\r
-\r
- addq $0x200, %rsp\r
-\r
- movabsq $ASM_PFX(gStmXdSupported), %rax\r
- movb (%rax), %al\r
- cmpb $0, %al\r
- jz L16\r
- popq %rdx # get saved MSR_IA32_MISC_ENABLE[63-32]\r
- testl $BIT2, %edx\r
- jz L16\r
- movl $MSR_IA32_MISC_ENABLE, %ecx\r
- rdmsr\r
- orw $BIT2, %dx # set XD Disable bit if it was set before entering into SMM\r
- wrmsr\r
-\r
-L16:\r
- rsm\r
-\r
-_StmSmiHandler:\r
-#\r
-# Check XD disable bit\r
-#\r
- xorq %r8, %r8\r
- movabsq $ASM_PFX(gStmXdSupported), %rax\r
- movb (%rax), %al\r
- cmpb $0, %al\r
- jz StmXdDone\r
- movl $MSR_IA32_MISC_ENABLE, %ecx\r
- rdmsr\r
- movq %rdx, %r8 # save MSR_IA32_MISC_ENABLE[63-32]\r
- testl $BIT2, %edx # MSR_IA32_MISC_ENABLE[34]\r
- jz L14\r
- andw $0x0FFFB, %dx # clear XD Disable bit if it is set\r
- wrmsr\r
-L14:\r
- movl $MSR_EFER, %ecx\r
- rdmsr\r
- orw $MSR_EFER_XD,%ax # enable NXE\r
- wrmsr\r
-StmXdDone:\r
- pushq %r8\r
-\r
- # below step is needed, because STM does not run above code.\r
- # we have to run below code to set IDT/CR0/CR4\r
- movabsq $ASM_PFX(gStmSmiHandlerIdtr), %rax\r
- lidt (%rax)\r
-\r
- movq %cr0, %rax\r
- orl $0x80010023, %eax\r
- movq %rax, %cr0\r
- movq %cr4, %rax\r
- movl $0x668, %eax # as cr4.PGE is not set here, refresh cr3\r
- movq %rax, %cr4 # in PreModifyMtrrs() to flush TLB.\r
- # STM init finish\r
- jmp CommonHandler\r
-\r
-ASM_PFX(gcStmSmiHandlerSize) : .word . - _StmSmiEntryPoint\r
-ASM_PFX(gcStmSmiHandlerOffset): .word _StmSmiHandler - _StmSmiEntryPoint\r