Added an extra GDT entry to the GDT used by thunk code. SS register will always be...
authorbxing <bxing@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 14 Sep 2006 16:04:47 +0000 (16:04 +0000)
committerbxing <bxing@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 14 Sep 2006 16:04:47 +0000 (16:04 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1541 6f19259b-4bc3-4df7-8a09-765794883524

MdePkg/Library/BaseLib/X64/Thunk16.asm

index 2c35c72..631b0fd 100644 (file)
@@ -49,7 +49,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
@@ -123,10 +123,10 @@ SavedEsp    DD      ?
 _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 +134,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
@@ -158,8 +157,9 @@ _ToUserCode PROC
     push    rax\r
     retf\r
 @RealMode:\r
+    DB      6ah, DATA32\r
     DB      2eh                         ; cs:\r
-    mov     [rsi + (SavedSs - @Base)], edi\r
+    pop     [rsi + (SavedSs - @Base)]\r
     DB      2eh                         ; cs:\r
     mov     [rsi + (SavedEsp - @Base)], bx\r
     DB      66h, 2eh                    ; CS and operand size override\r
@@ -175,22 +175,34 @@ _ToUserCode PROC
     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
@@ -203,6 +215,7 @@ GdtEnd          LABEL   QWORD
 InternalAsmThunk16  PROC    USES    rbp rbx rsi rdi\r
     mov     r10d, ds\r
     mov     r11d, es\r
+    mov     r9d, ss\r
     push    fs\r
     push    gs\r
     mov     rsi, rcx\r
@@ -231,16 +244,19 @@ 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
+    lea     edx, [rdx + DATA16 - DATA32]\r
     call    fword ptr [rcx + (_EntryPoint - SavedCr4)]\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