]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/X64/Thunk16.asm
Update copyright for files modified in this year
[mirror_edk2.git] / MdePkg / Library / BaseLib / X64 / Thunk16.asm
index 2c35c72a57713c78774211b06d9e8b25053df31f..d93af4459766d49cf04cc06fb1c9364216e5f5e2 100644 (file)
@@ -1,6 +1,9 @@
+\r
+#include "BaseLibInternals.h"\r
+\r
 ;------------------------------------------------------------------------------\r
 ;\r
-; Copyright (c) 2006, Intel Corporation\r
+; Copyright (c) 2006 - 2008, Intel Corporation\r
 ; All rights reserved. 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
@@ -49,7 +52,7 @@ IA32_REGS   ENDS
 \r
 m16Size         DW      InternalAsmThunk16 - m16Start\r
 mThunk16Attr    DW      _ThunkAttr - m16Start\r
-m16Gdt          DW      _NullSegDesc - m16Start\r
+m16Gdt          DW      _NullSeg - m16Start\r
 m16GdtrBase     DW      _16GdtrBase - m16Start\r
 mTransition     DW      _EntryPoint - m16Start\r
 \r
@@ -66,6 +69,14 @@ SavedGdt    LABEL   FWORD
 ; by user code. It will be shadowed to somewhere in memory below 1MB.\r
 ;------------------------------------------------------------------------------\r
 _BackFromUserCode   PROC\r
+    ;\r
+    ; The order of saved registers on the stack matches the order they appears\r
+    ; in IA32_REGS structure. This facilitates wrapper function to extract them\r
+    ; into that structure.\r
+    ;\r
+    ; Some instructions for manipulation of segment registers have to be written\r
+    ; in opcode since 64-bit MASM prevents accesses to those registers.\r
+    ;\r
     DB      16h                         ; push ss\r
     DB      0eh                         ; push cs\r
     DB      66h\r
@@ -96,14 +107,26 @@ _ThunkAttr  DD      ?
 @2:\r
     mov     eax, ss\r
     lea     bp, [esp + sizeof (IA32_REGS)]\r
+    ;\r
+    ; rsi in the following 2 instructions is indeed bp in 16-bit code\r
+    ;\r
     mov     word ptr (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._ESP, bp\r
+    DB      66h\r
     mov     ebx, (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._EIP\r
     shl     ax, 4                       ; shl eax, 4\r
     add     bp, ax                      ; add ebp, eax\r
+    mov     ax, cs\r
+    shl     ax, 4\r
+    lea     ax, [eax + ebx + (@64BitCode - @Base)]\r
+    DB      66h, 2eh, 89h, 87h          ; mov cs:[bx + (@64Eip - @Base)], eax\r
+    DW      @64Eip - @Base\r
     DB      66h, 0b8h                   ; mov eax, imm32\r
 SavedCr4    DD      ?\r
     mov     cr4, rax\r
-    DB      66h, 2eh\r
+    ;\r
+    ; rdi in the instruction below is indeed bx in 16-bit code\r
+    ;\r
+    DB      66h, 2eh                    ; 2eh is "cs:" segment override\r
     lgdt    fword ptr [rdi + (SavedGdt - @Base)]\r
     DB      66h\r
     mov     ecx, 0c0000080h\r
@@ -113,20 +136,19 @@ SavedCr4    DD      ?
     DB      66h, 0b8h                   ; mov eax, imm32\r
 SavedCr0    DD      ?\r
     mov     cr0, rax\r
-    DB      0b8h                        ; mov ax, imm16\r
-SavedSs     DW      ?\r
-    mov     ss, eax\r
-    DB      66h, 0bch                   ; mov esp, imm32\r
-SavedEsp    DD      ?\r
-    DB      66h\r
-    retf                                ; return to protected mode\r
+    DB      66h, 0eah                   ; jmp far cs:@64Bit\r
+@64Eip      DD      ?\r
+SavedCs     DW      ?\r
+@64BitCode:\r
+    mov     rsp, r8                     ; restore stack\r
+    ret\r
 _BackFromUserCode   ENDP\r
 \r
 _EntryPoint DD      _ToUserCode - m16Start\r
-            DW      8h\r
+            DW      CODE16\r
 _16Gdtr     LABEL   FWORD\r
-            DW      GdtEnd - _NullSegDesc - 1\r
-_16GdtrBase DQ      _NullSegDesc\r
+            DW      GDT_SIZE - 1\r
+_16GdtrBase DQ      _NullSeg\r
 _16Idtr     FWORD   (1 SHL 10) - 1\r
 \r
 ;------------------------------------------------------------------------------\r
@@ -134,7 +156,6 @@ _16Idtr     FWORD   (1 SHL 10) - 1
 ; It will be shadowed to somewhere in memory below 1MB.\r
 ;------------------------------------------------------------------------------\r
 _ToUserCode PROC\r
-    mov     edi, ss\r
     mov     ss, edx                     ; set new segment selectors\r
     mov     ds, edx\r
     mov     es, edx\r
@@ -148,20 +169,16 @@ _ToUserCode PROC
     wrmsr\r
     mov     cr4, rbp\r
     mov     ss, esi                     ; set up 16-bit stack segment\r
-    xchg    sp, bx                      ; set up 16-bit stack pointer\r
-    DB      66h\r
+    mov     sp, bx                      ; set up 16-bit stack pointer\r
+    DB      66h                         ; make the following call 32-bit\r
     call    @Base                       ; push eip\r
 @Base:\r
     pop     bp                          ; ebp <- address of @Base\r
     push    [esp + sizeof (IA32_REGS) + 2]\r
-    lea     eax, [rsi + (@RealMode - @Base)]\r
+    lea     eax, [rsi + (@RealMode - @Base)]    ; rsi is "bp" in 16-bit code\r
     push    rax\r
-    retf\r
+    retf                                ; execution begins at next instruction\r
 @RealMode:\r
-    DB      2eh                         ; cs:\r
-    mov     [rsi + (SavedSs - @Base)], edi\r
-    DB      2eh                         ; cs:\r
-    mov     [rsi + (SavedEsp - @Base)], bx\r
     DB      66h, 2eh                    ; CS and operand size override\r
     lidt    fword ptr [rsi + (_16Idtr - @Base)]\r
     DB      66h, 61h                    ; popad\r
@@ -171,26 +188,38 @@ _ToUserCode PROC
     pop     gs\r
     popf                                ; popfd\r
     lea     sp, [esp + 4]               ; skip high order 32 bits of EFlags\r
-    DB      66h\r
+    DB      66h                         ; make the following retf 32-bit\r
     retf                                ; transfer control to user code\r
 _ToUserCode ENDP\r
 \r
-_NullSegDesc    DQ      0\r
-_16CsDesc       LABEL   QWORD\r
-                DW      -1\r
-                DW      0\r
-                DB      0\r
-                DB      9bh\r
-                DB      8fh             ; 16-bit segment, 4GB limit\r
-                DB      0\r
-_16DsDesc       LABEL   QWORD\r
-                DW      -1\r
-                DW      0\r
-                DB      0\r
-                DB      93h\r
-                DB      8fh             ; 16-bit segment, 4GB limit\r
-                DB      0\r
-GdtEnd          LABEL   QWORD\r
+CODE16  = _16Code - $\r
+DATA16  = _16Data - $\r
+DATA32  = _32Data - $\r
+\r
+_NullSeg    DQ      0\r
+_16Code     LABEL   QWORD\r
+            DW      -1\r
+            DW      0\r
+            DB      0\r
+            DB      9bh\r
+            DB      8fh                 ; 16-bit segment, 4GB limit\r
+            DB      0\r
+_16Data     LABEL   QWORD\r
+            DW      -1\r
+            DW      0\r
+            DB      0\r
+            DB      93h\r
+            DB      8fh                 ; 16-bit segment, 4GB limit\r
+            DB      0\r
+_32Data     LABEL   QWORD\r
+            DW      -1\r
+            DW      0\r
+            DB      0\r
+            DB      93h\r
+            DB      0cfh                ; 16-bit segment, 4GB limit\r
+            DB      0\r
+\r
+GDT_SIZE = $ - _NullSeg\r
 \r
 ;------------------------------------------------------------------------------\r
 ; IA32_REGISTER_SET *\r
@@ -201,8 +230,9 @@ GdtEnd          LABEL   QWORD
 ;   );\r
 ;------------------------------------------------------------------------------\r
 InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi\r
-    mov     r10d, ds\r
-    mov     r11d, es\r
+    mov     r10d, ds                    ; r9 ~ r11 are not accessible in 16-bit\r
+    mov     r11d, es                    ; so use them for saving seg registers\r
+    mov     r9d, ss\r
     push    fs\r
     push    gs\r
     mov     rsi, rcx\r
@@ -218,9 +248,8 @@ InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi
     lea     ecx, [rdx + (SavedCr4 - m16Start)]\r
     mov     eax, edx                    ; eax <- transition code address\r
     and     edx, 0fh\r
-    shl     eax, 12\r
-    lea     edx, [rdx + (_BackFromUserCode - m16Start)]\r
-    mov     ax, dx\r
+    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
     sidt    fword ptr [rsp + 38h]       ; save IDT stack in argument space\r
@@ -231,16 +260,25 @@ InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi
     mov     [rcx], ebp                  ; save CR4 in SavedCr4\r
     and     ebp, 300h                   ; clear all but PCE and OSFXSR bits\r
     mov     esi, r8d                    ; esi <- 16-bit stack segment\r
-    push    10h\r
-    pop     rdx                         ; rdx <- selector for data segments\r
+    DB      6ah, DATA32                 ; push DATA32\r
+    pop     rdx                         ; rdx <- 32-bit data segment selector\r
     lgdt    fword ptr [rcx + (_16Gdtr - SavedCr4)]\r
+    mov     ss, edx\r
     pushfq\r
-    call    fword ptr [rcx + (_EntryPoint - SavedCr4)]\r
+    lea     edx, [rdx + DATA16 - DATA32]\r
+    lea     r8, @RetFromRealMode\r
+    push    r8\r
+    mov     r8d, cs\r
+    mov     [rcx + (SavedCs - SavedCr4)], r8w\r
+    mov     r8, rsp\r
+    jmp     fword ptr [rcx + (_EntryPoint - SavedCr4)]\r
+@RetFromRealMode:\r
     popfq\r
     lidt    fword ptr [rsp + 38h]       ; restore protected mode IDTR\r
     lea     eax, [rbp - sizeof (IA32_REGS)]\r
     pop     gs\r
     pop     fs\r
+    mov     ss, r9d\r
     mov     es, r11d\r
     mov     ds, r10d\r
     ret\r