]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/Ia32/Thunk16.asm
ArmPkg/ArmDisassemblerLib: fix check for MSR instruction
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / Thunk16.asm
index 9fba03ac05072d7540ef95f7188d2726356fc931..08955d4e91d4c91babd9e2646de0264cd46b7b24 100644 (file)
@@ -1,10 +1,13 @@
+\r
+#include "BaseLibInternals.h"\r
+\r
 ;------------------------------------------------------------------------------\r
 ;\r
-; Copyright (c) 2006, Intel Corporation\r
-; All rights reserved. This program and the accompanying materials\r
+; Copyright (c) 2006 - 2013, 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
-; http://opensource.org/licenses/bsd-license.php\r
+; http://opensource.org/licenses/bsd-license.php.\r
 ;\r
 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
@@ -29,6 +32,10 @@ EXTERNDEF   C   m16Gdt:WORD
 EXTERNDEF   C   m16GdtrBase:WORD\r
 EXTERNDEF   C   mTransition:WORD\r
 \r
+;\r
+; Here is the layout of the real mode stack. _ToUserCode() is responsible for\r
+; loading all these registers from real mode stack.\r
+;\r
 IA32_REGS   STRUC   4t\r
 _EDI        DD      ?\r
 _ESI        DD      ?\r
@@ -50,6 +57,9 @@ IA32_REGS   ENDS
 \r
     .const\r
 \r
+;\r
+; These are global constant to convey information to C code.\r
+;\r
 m16Size         DW      InternalAsmThunk16 - m16Start\r
 mThunk16Attr    DW      _ThunkAttr - m16Start\r
 m16Gdt          DW      _NullSegDesc - m16Start\r
@@ -63,8 +73,16 @@ m16Start    LABEL   BYTE
 SavedGdt    LABEL   FWORD\r
             DW      ?\r
             DD      ?\r
-\r
+;------------------------------------------------------------------------------\r
+; _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
 _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
     push    ss\r
     push    cs\r
     DB      66h\r
@@ -91,9 +109,15 @@ _ThunkAttr  DD      ?
     or      al, 2\r
     out     92h, al                     ; deactivate A20M#\r
 @2:\r
-    mov     eax, ss\r
+    xor     ax, ax                      ; xor eax, eax\r
+    mov     eax, ss                     ; mov ax, ss\r
     DB      67h\r
     lea     bp, [esp + sizeof (IA32_REGS)]\r
+    ;\r
+    ; esi's in the following 2 instructions are indeed bp in 16-bit code. Fact\r
+    ; is "esi" in 32-bit addressing mode has the same encoding of "bp" in 16-\r
+    ; bit addressing mode.\r
+    ;\r
     mov     word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp\r
     mov     ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP\r
     shl     ax, 4                       ; shl eax, 4\r
@@ -122,6 +146,10 @@ _16Gdtr     LABEL   FWORD
             DW      GdtEnd - _NullSegDesc - 1\r
 _16GdtrBase DD      _NullSegDesc\r
 \r
+;------------------------------------------------------------------------------\r
+; _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
 _ToUserCode PROC\r
     mov     edx, ss\r
     mov     ss, ecx                     ; set new segment selectors\r
@@ -129,31 +157,37 @@ _ToUserCode PROC
     mov     es, ecx\r
     mov     fs, ecx\r
     mov     gs, ecx\r
-    mov     cr0, eax\r
-    mov     cr4, ebp                    ; real mode starts at next instruction\r
+    mov     cr0, eax                    ; real mode starts at next instruction\r
+                                        ;  which (per SDM) *must* be a far JMP.\r
+    DB      0eah\r
+_RealAddr DW 0,0                       ; filled in by InternalAsmThunk16\r
+\r
+    mov     cr4, ebp\r
     mov     ss, esi                     ; set up 16-bit stack segment\r
     xchg    sp, bx                      ; set up 16-bit stack pointer\r
-    DB      66h\r
-    call    @Base                       ; push eip\r
-@Base:\r
-    pop     bp                          ; ebp <- address of @Base\r
-    DB      67h                         ; address size override\r
-    push    [esp + sizeof (IA32_REGS) + 2]\r
-    lea     eax, [esi + (@RealMode - @Base)]\r
-    push    eax\r
-    retf\r
-@RealMode:\r
-    mov     cs:[esi + (SavedSs - @Base)], edx\r
-    mov     cs:[esi + (SavedEsp - @Base)], bx\r
-    DB      66h\r
-    lidt    fword ptr cs:[esi + (_16Idtr - @Base)]\r
+\r
+;   mov     bp, [esp + sizeof(IA32_REGS)\r
+    DB      67h\r
+    mov     ebp, [esp + sizeof(IA32_REGS)] ; BackFromUserCode address from stack\r
+\r
+;   mov     cs:[bp + (SavedSs - _BackFromUserCode)], dx\r
+    mov     cs:[esi + (SavedSs - _BackFromUserCode)], edx\r
+\r
+;   mov     cs:[bp + (SavedEsp - _BackFromUserCode)], ebx\r
+    DB      2eh, 66h, 89h, 9eh\r
+    DW      SavedEsp - _BackFromUserCode\r
+\r
+;   lidt    cs:[bp + (_16Idtr - _BackFromUserCode)]\r
+    DB      2eh, 66h, 0fh, 01h, 9eh\r
+    DW      _16Idtr - _BackFromUserCode\r
+\r
     popaw                               ; popad actually\r
     pop     ds\r
     pop     es\r
     pop     fs\r
     pop     gs\r
     popf                                ; popfd\r
-    DB      66h\r
+    DB      66h                         ; Use 32-bit addressing for "retf" below\r
     retf                                ; transfer control to user code\r
 _ToUserCode ENDP\r
 \r
@@ -174,13 +208,16 @@ _16DsDesc       LABEL   QWORD
                 DB      0\r
 GdtEnd          LABEL   QWORD\r
 \r
-;\r
-;   @param  RegSet  Pointer to a IA32_DWORD_REGS structure\r
-;   @param  Transition  Pointer to the transition code\r
-;   @return The address of the 16-bit stack after returning from user code\r
-;\r
+;------------------------------------------------------------------------------\r
+; IA32_REGISTER_SET *\r
+; EFIAPI\r
+; InternalAsmThunk16 (\r
+;   IN      IA32_REGISTER_SET         *RegisterSet,\r
+;   IN OUT  VOID                      *Transition\r
+;   );\r
+;------------------------------------------------------------------------------\r
 InternalAsmThunk16  PROC    USES    ebp ebx esi edi ds  es  fs  gs\r
-    mov     esi, [esp + 36]             ; esi <- RegSet\r
+    mov     esi, [esp + 36]             ; esi <- RegSet, the 1st parameter\r
     movzx   edx, (IA32_REGS ptr [esi])._SS\r
     mov     edi, (IA32_REGS ptr [esi])._ESP\r
     add     edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space\r
@@ -199,6 +236,8 @@ InternalAsmThunk16  PROC    USES    ebp ebx esi edi ds  es  fs  gs
     lea     ecx, [ecx + (_BackFromUserCode - m16Start)]\r
     mov     ax, cx\r
     stosd                               ; [edi] <- return address of user code\r
+    add     eax, _RealAddr + 4 - _BackFromUserCode\r
+    mov     dword ptr [edx + (_RealAddr - SavedCr0)], eax\r
     sgdt    fword ptr [edx + (SavedGdt - SavedCr0)]\r
     sidt    fword ptr [esp + 36]        ; save IDT stack in argument space\r
     mov     eax, cr0\r
@@ -206,15 +245,15 @@ InternalAsmThunk16  PROC    USES    ebp ebx esi edi ds  es  fs  gs
     and     eax, 7ffffffeh              ; clear PE, PG bits\r
     mov     ebp, cr4\r
     mov     [edx + (SavedCr4 - SavedCr0)], ebp\r
-    and     ebp, 300h                   ; clear all but PCE and OSFXSR bits\r
+    and     ebp, NOT 30h                ; clear PAE, PSE bits\r
     push    10h\r
     pop     ecx                         ; ecx <- selector for data segments\r
     lgdt    fword ptr [edx + (_16Gdtr - SavedCr0)]\r
-    pushfd\r
+    pushfd                              ; Save df/if indeed\r
     call    fword ptr [edx + (_EntryPoint - SavedCr0)]\r
     popfd\r
     lidt    fword ptr [esp + 36]        ; restore protected mode IDTR\r
-    lea     eax, [ebp - sizeof (IA32_REGS)]\r
+    lea     eax, [ebp - sizeof (IA32_REGS)] ; eax <- the address of IA32_REGS\r
     ret\r
 InternalAsmThunk16  ENDP\r
 \r