]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Fix the bug in x64 thunk code that it cannot handle the case when GDT is loaded in...
authorqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 26 May 2009 11:41:12 +0000 (11:41 +0000)
committerqhuang8 <qhuang8@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 26 May 2009 11:41:12 +0000 (11:41 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@8390 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Library/BaseLib/X64/Thunk16.S
MdePkg/Library/BaseLib/X64/Thunk16.asm

index a1af5128f25ebee9770c95661393a1ad553c3df5..3f94759ceff22c7b38b535c13d69b8fce140bf4e 100644 (file)
@@ -264,7 +264,23 @@ 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
+    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
@@ -288,7 +304,8 @@ ASM_PFX(InternalAsmThunk16):
     .byte   _EntryPoint - SavedCr4\r
 L_RetFromRealMode: \r
     popfq\r
-    lidt    0x50(%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
index cef63d98e1f41612574c124620e5ed545090a155..7d9136d4ab206e72e47644bece037f6ed4da1ca8 100644 (file)
@@ -258,7 +258,23 @@ InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi
     shl     eax, 12                     ; segment address in high order 16 bits\r
     lea     ax, [rdx + (_BackFromUserCode - m16Start)]  ; offset address\r
     stosd                               ; [edi] <- return address of user code\r
-    sgdt    fword ptr [rcx + (SavedGdt - SavedCr4)]\r
+  \r
+    sgdt    fword ptr [rsp + 60h]       ; save GDT stack in argument space\r
+    movzx   r10, word ptr [rsp + 60h]   ; r10 <- GDT limit \r
+    lea     r11, [rcx + (InternalAsmThunk16 - SavedCr4) + 0xf]\r
+    and     r11, 0xfffffff0             ; r11 <- 16-byte aligned shadowed GDT table in real mode buffer\r
+    \r
+    mov     word ptr [rcx + (SavedGdt - SavedCr4)], r10w      ; save the limit of shadowed GDT table\r
+    mov     qword ptr [rcx + (SavedGdt - SavedCr4) + 2], r11  ; save the base address of shadowed GDT table\r
+    \r
+    mov     rsi, qword ptr [rsp + 62h]  ; rsi <- the original GDT base address\r
+    xchg    rcx, r10                    ; save rcx to r10 and initialize rcx to be the limit of GDT table\r
+    inc     rcx                         ; rcx <- the size of memory to copy\r
+    xchg    rdi, r11                    ; save rdi to r11 and initialize rdi to the base address of shadowed GDT table\r
+    rep     movsb                       ; perform memory copy to shadow GDT table\r
+    mov     rcx, r10                    ; restore the orignal rcx before memory copy\r
+    mov     rdi, r11                    ; restore the original rdi before memory copy\r
+    \r
     sidt    fword ptr [rsp + 50h]       ; save IDT stack in argument space\r
     mov     rax, cr0\r
     mov     [rcx + (SavedCr0 - SavedCr4)], eax\r
@@ -281,6 +297,7 @@ InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi
     jmp     fword ptr [rcx + (_EntryPoint - SavedCr4)]\r
 @RetFromRealMode:\r
     popfq\r
+    lgdt    fword ptr [rsp + 60h]       ; restore protected mode GDTR\r
     lidt    fword ptr [rsp + 50h]       ; restore protected mode IDTR\r
     lea     eax, [rbp - sizeof (IA32_REGS)]\r
     pop     gs\r