]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/X64/Thunk16.S
Fix the bug in x64 thunk code that it cannot handle the case when GDT is loaded in...
[mirror_edk2.git] / MdePkg / Library / BaseLib / X64 / Thunk16.S
index b829685b43ca341fb5a109a514019063409867ab..3f94759ceff22c7b38b535c13d69b8fce140bf4e 100644 (file)
 \r
 #include <Library/BaseLib.h>\r
 \r
-.globl ASM_PFX(m16Start)\r
-.globl ASM_PFX(m16Size)\r
-.globl ASM_PFX(mThunk16Attr)\r
-.globl ASM_PFX(m16Gdt)\r
-.globl ASM_PFX(m16GdtrBase)\r
-.globl ASM_PFX(mTransition)\r
-.globl ASM_PFX(InternalAsmThunk16)\r
+ASM_GLOBAL ASM_PFX(m16Start)\r
+ASM_GLOBAL ASM_PFX(m16Size)\r
+ASM_GLOBAL ASM_PFX(mThunk16Attr)\r
+ASM_GLOBAL ASM_PFX(m16Gdt)\r
+ASM_GLOBAL ASM_PFX(m16GdtrBase)\r
+ASM_GLOBAL ASM_PFX(mTransition)\r
+ASM_GLOBAL ASM_PFX(InternalAsmThunk16)\r
 \r
 # define the structure of IA32_REGS\r
 .equ  _EDI, 0       #size 4\r
@@ -66,7 +66,7 @@ SavedGdt:    .space 10
 # _BackFromUserCode() takes control in real mode after 'retf' has been executed\r
 # by user code. It will be shadowed to somewhere in memory below 1MB.\r
 #------------------------------------------------------------------------------\r
-.globl ASM_PFX(BackFromUserCode)\r
+ASM_GLOBAL ASM_PFX(BackFromUserCode)\r
 ASM_PFX(BackFromUserCode):\r
     #\r
     # The order of saved registers on the stack matches the order they appears\r
@@ -136,10 +136,13 @@ SavedCr4:   .space  4
 SavedCr0:    .space      4\r
     movq    %rax, %cr0\r
     .byte 0x66,0xea                     # jmp far cs:L_64Bit\r
-L_64Eip:      .space      4\r
+L_64Eip:     .space      4\r
 SavedCs:     .space      2\r
 L_64BitCode: \r
-    movq     %r8, %rsp\r
+    .byte   0x90\r
+    .byte   0x67,0xbc                  # mov esp, imm32\r
+SavedSp:    .space 4                   # restore stack\r
+    nop\r
     ret\r
 \r
 _EntryPoint: .long      ASM_PFX(ToUserCode) - ASM_PFX(m16Start)\r
@@ -153,7 +156,7 @@ _16Idtr:     .word      0x3ff
 # _ToUserCode() takes control in real mode before passing control to user code.\r
 # It will be shadowed to somewhere in memory below 1MB.\r
 #------------------------------------------------------------------------------\r
-.globl ASM_PFX(ToUserCode)\r
+ASM_GLOBAL ASM_PFX(ToUserCode)\r
 ASM_PFX(ToUserCode):\r
     movl    %edx,%ss                    # set new segment selectors\r
     movl    %edx,%ds\r
@@ -227,18 +230,21 @@ ASM_PFX(_32Data):
 #   IN OUT  VOID                      *Transition\r
 #   );\r
 #------------------------------------------------------------------------------\r
-# MISMATCH: "InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi"\r
 \r
-.globl ASM_PFX(InternalAsmThunk16)\r
+ASM_GLOBAL ASM_PFX(InternalAsmThunk16)\r
 ASM_PFX(InternalAsmThunk16):\r
     pushq   %rbp\r
     pushq   %rbx\r
     pushq   %rsi\r
     pushq   %rdi\r
     \r
-    movl    %ds, %r10d                  # r9 ~ r11 are not accessible in 16-bit\r
-    movl    %es, %r11d                  # so use them for saving seg registers\r
-    movl    %ss, %r9d\r
+    movq    %ds, %rbx\r
+    pushq   %rbx      # Save ds segment register on the stack\r
+    movq    %es, %rbx\r
+    pushq   %rbx      # Save es segment register on the stack\r
+    movq    %ss, %rbx\r
+    pushq   %rbx      # Save ss segment register on the stack\r
+\r
     .byte   0x0f, 0xa0                  #push   fs\r
     .byte   0x0f, 0xa8                  #push   gs\r
     movq    %rcx, %rsi\r
@@ -258,8 +264,24 @@ ASM_PFX(InternalAsmThunk16):
     shll    $12,%eax                    # segment address in high order 16 bits\r
     lea     (_BackFromUserCode - ASM_PFX(m16Start))(%rdx), %ax\r
     stosl                               # [edi] <- return address of user code\r
-    sgdt    (SavedGdt - SavedCr4)(%rcx) \r
-    sidt    0x38(%rsp)\r
+    sgdt    0x60(%rsp)                  # save GDT stack in argument space\r
+    movzwq  0x60(%rsp), %r10            # r10 <- GDT limit \r
+    lea     ((ASM_PFX(InternalAsmThunk16) - SavedCr4) + 0xf)(%rcx), %r11 \r
+    andl    $0xfffffff0, r11            # r11 <- 16-byte aligned shadowed GDT table in real mode buffer      \r
+    \r
+    movw    %r10w, (SavedGdt - SavedCr4)(%rcx)       # save the limit of shadowed GDT table\r
+    movq    %r11, (SavedGdt - SavedCr4 + 0x2)(%rcx)  # save the base address of shadowed GDT table\r
+    \r
+    movq    0x62(%rsp) ,%rsi            # rsi <- the original GDT base address\r
+    xchg   %r10, %rcx                   # save rcx to r10 and initialize rcx to be the limit of GDT table \r
+    incq   %rcx                         # rcx <- the size of memory to copy\r
+    xchg   %r11, %rdi                   # save rdi to r11 and initialize rdi to the base address of shadowed GDT table\r
+    rep\r
+    movsb                               # perform memory copy to shadow GDT table\r
+    movq   %r10, %rcx                   # restore the orignal rcx before memory copy\r
+    movq   %r11, %rdi                   # restore the original rdi before memory copy\r
+       \r
+    sidt    0x50(%rsp)\r
     movq    %cr0, %rax\r
     movl    %eax, (SavedCr0 - SavedCr4)(%rcx)\r
     andl    $0x7ffffffe,%eax            # clear PE, PG bits\r
@@ -277,18 +299,23 @@ ASM_PFX(InternalAsmThunk16):
     pushq   %r8\r
     movl    %cs, %r8d\r
     movw    %r8w, (SavedCs - SavedCr4)(%rcx)\r
-    movq     %rsp, %r8\r
+    movl    %esp, (SavedSp - SavedCr4)(%rcx)\r
     .byte   0xff, 0x69                  #  jmp (_EntryPoint - SavedCr4)(%rcx)\r
     .byte   _EntryPoint - SavedCr4\r
 L_RetFromRealMode: \r
     popfq\r
-    lidt    0x38(%rsp)\r
+    lgdt    0x60(%rsp)                  # restore protected mode GDTR\r
+    lidt    0x50(%rsp)                  # restore protected mode IDTR\r
     lea     -IA32_REGS_SIZE(%rbp), %eax\r
     .byte 0x0f, 0xa9                    # pop gs\r
     .byte 0x0f, 0xa1                    # pop fs\r
-    movl    %r9d, %ss\r
-    movl    %r11d, %es\r
-    movl    %r10d, %ds\r
+    \r
+    popq     %rbx\r
+    movq     %rbx, %ss\r
+    popq     %rbx\r
+    movq     %rbx, %es\r
+    popq     %rbx\r
+    movq     %rbx, %ds\r
     \r
     popq    %rdi\r
     popq    %rsi\r