]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmiEntry.S
MdePkg/BaseLib: add PatchInstructionX86()
[mirror_edk2.git] / UefiCpuPkg / PiSmmCpuDxeSmm / X64 / SmiEntry.S
index b488b74b70756af9d9f675fe9fb578e814a0d695..600d8623cd2690b6526d46c0d9e1823f170fb528 100644 (file)
@@ -1,6 +1,6 @@
 #------------------------------------------------------------------------------\r
 #\r
-# Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\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
@@ -24,8 +24,13 @@ ASM_GLOBAL  ASM_PFX(gcSmiHandlerSize)
 ASM_GLOBAL  ASM_PFX(gSmiCr3)\r
 ASM_GLOBAL  ASM_PFX(gSmiStack)\r
 ASM_GLOBAL  ASM_PFX(gSmbase)\r
+ASM_GLOBAL  ASM_PFX(mXdSupported)\r
 ASM_GLOBAL  ASM_PFX(gSmiHandlerIdtr)\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 PROCESSOR_SMM_DESCRIPTOR\r
 #\r
@@ -132,6 +137,32 @@ ASM_PFX(gSmiCr3):    .space  4
     movl    $TSS_SEGMENT, %eax\r
     ltr     %ax\r
 \r
+# enable NXE if supported\r
+    .byte   0xb0                        # mov al, imm8\r
+ASM_PFX(mXdSupported): .byte 1\r
+    cmpb    $0, %al\r
+    jz      SkipNxe\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     NxeDone\r
+SkipNxe:\r
+    subl    $8, %esp\r
+NxeDone:\r
+\r
     #\r
     # Switch to LongMode\r
     #\r
@@ -139,12 +170,13 @@ ASM_PFX(gSmiCr3):    .space  4
     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
-    movl    $0xc0000080, %ecx\r
+\r
+    movl    $MSR_EFER, %ecx\r
     rdmsr\r
-    orb     $1,%ah\r
+    orb     $1,%ah                      # enable LME\r
     wrmsr\r
     movq    %cr0, %rbx\r
-    btsl    $31, %ebx\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
@@ -162,10 +194,10 @@ LongMode:                               # long mode (64-bit code) starts here
 #   jmp     _SmiHandler                 ; instruction is not needed\r
 \r
 _SmiHandler:\r
-    movq    (%rsp), %rbx\r
+    movq    8(%rsp), %rbx\r
     # Save FP registers\r
 \r
-    subq    $0x208, %rsp\r
+    subq    $0x200, %rsp\r
     .byte   0x48                        # FXSAVE64\r
     fxsave  (%rsp)\r
 \r
@@ -191,6 +223,21 @@ _SmiHandler:
     .byte   0x48                        # FXRSTOR64\r
     fxrstor (%rsp)\r
 \r
+    addq    $0x200, %rsp\r
+\r
+    movabsq $ASM_PFX(mXdSupported), %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
 ASM_PFX(gcSmiHandlerSize):    .word      . - _SmiEntryPoint\r