]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/Ia32/Thunk16.nasm
MdePkg BaseLib NASM Thunk16: Use bits 16 for 16-bit code
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / Thunk16.nasm
index c00e3ab07864fcbeb3d291303ba280d4866e709f..a1093e0107a0c059eb7954a261d4ec2e332e338e 100644 (file)
@@ -82,23 +82,24 @@ _BackFromUserCode:
     ; in IA32_REGS structure. This facilitates wrapper function to extract them\r
     ; into that structure.\r
     ;\r
+BITS    16\r
     push    ss\r
     push    cs\r
-    DB      66h\r
-    call    .Base                       ; push eip\r
+o32 call    dword .Base                 ; push eip\r
 .Base:\r
-    pushfw                              ; pushfd actually\r
+    pushfd\r
     cli                                 ; disable interrupts\r
     push    gs\r
     push    fs\r
     push    es\r
     push    ds\r
-    pushaw                              ; pushad actually\r
+    pushad\r
     DB      66h, 0bah                   ; mov edx, imm32\r
 .ThunkAttr: dd   0\r
     test    dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15\r
     jz      .1\r
-    mov     eax, 15cd2401h              ; mov ax, 2401h & int 15h\r
+    mov     ax, 2401h\r
+    int     15h\r
     cli                                 ; disable interrupts\r
     jnc     .2\r
 .1:\r
@@ -108,24 +109,17 @@ _BackFromUserCode:
     or      al, 2\r
     out     92h, al                     ; deactivate A20M#\r
 .2:\r
-    xor     ax, ax                      ; xor eax, eax\r
-    mov     eax, ss                     ; mov ax, ss\r
-    DB      67h\r
-    lea     bp, [esp + IA32_REGS.size]\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     [esi - IA32_REGS.size + IA32_REGS._ESP], bp\r
-    mov     ebx, [esi - IA32_REGS.size + IA32_REGS._EIP]\r
-    shl     ax, 4                       ; shl eax, 4\r
-    add     bp, ax                      ; add ebp, eax\r
+    xor     eax, eax\r
+    mov     ax, ss\r
+    lea     ebp, [esp + IA32_REGS.size]\r
+    mov     [bp - IA32_REGS.size + IA32_REGS._ESP], ebp\r
+    mov     bx, [bp - IA32_REGS.size + IA32_REGS._EIP]\r
+    shl     eax, 4                      ; shl eax, 4\r
+    add     ebp, eax                    ; add ebp, eax\r
     DB      66h, 0b8h                   ; mov eax, imm32\r
 .SavedCr4:  DD      0\r
     mov     cr4, eax\r
-    DB      66h\r
-    lgdt    [cs:edi + (SavedGdt - .Base)]\r
+o32 lgdt [cs:bx + (SavedGdt - .Base)]\r
     DB      66h, 0b8h                   ; mov eax, imm32\r
 .SavedCr0:  DD      0\r
     mov     cr0, eax\r
@@ -134,8 +128,7 @@ _BackFromUserCode:
     mov     ss, eax\r
     DB      66h, 0bch                   ; mov esp, imm32\r
 .SavedEsp   DD      0\r
-    DB      66h\r
-    retf                                ; return to protected mode\r
+o32 retf                                ; return to protected mode\r
 \r
 _EntryPoint:\r
         DD      _ToUserCode - ASM_PFX(m16Start)\r
@@ -153,45 +146,34 @@ _16GdtrBase:
 ; It will be shadowed to somewhere in memory below 1MB.\r
 ;------------------------------------------------------------------------------\r
 _ToUserCode:\r
-    mov     edx, ss\r
-    mov     ss, ecx                     ; set new segment selectors\r
-    mov     ds, ecx\r
-    mov     es, ecx\r
-    mov     fs, ecx\r
-    mov     gs, ecx\r
+BITS    16\r
+    mov     dx, ss\r
+    mov     ss, cx                      ; set new segment selectors\r
+    mov     ds, cx\r
+    mov     es, cx\r
+    mov     fs, cx\r
+    mov     gs, cx\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\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
-\r
-;   mov     bp, [esp + sizeof(IA32_REGS)\r
-    DB      67h\r
-    mov     ebp, [esp + IA32_REGS.size] ; BackFromUserCode address from stack\r
-\r
-;   mov     cs:[bp + (_BackFromUserCode.SavedSs - _BackFromUserCode)], dx\r
-    mov     [cs:esi + (_BackFromUserCode.SavedSs - _BackFromUserCode)], edx\r
+    mov     ss, si                      ; set up 16-bit stack segment\r
+    xchg    esp, ebx                    ; set up 16-bit stack pointer\r
+    mov     bp, [esp + IA32_REGS.size]\r
+    mov     [cs:bp + (_BackFromUserCode.SavedSs - _BackFromUserCode)], dx\r
+    mov     [cs:bp + (_BackFromUserCode.SavedEsp - _BackFromUserCode)], ebx\r
+    lidt    [cs:bp + (_16Idtr - _BackFromUserCode)]\r
 \r
-;   mov     cs:[bp + (_BackFromUserCode.SavedEsp - _BackFromUserCode)], ebx\r
-    DB      2eh, 66h, 89h, 9eh\r
-    DW      _BackFromUserCode.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
+    popad\r
     pop     ds\r
     pop     es\r
     pop     fs\r
     pop     gs\r
-    popfw                                ; popfd\r
+    popfd\r
 \r
-    DB      66h                         ; Use 32-bit addressing for "retf" below\r
-    retf                                ; transfer control to user code\r
+o32 retf                                ; transfer control to user code\r
 \r
 ALIGN   16\r
 _NullSegDesc    DQ      0\r
@@ -221,6 +203,7 @@ GdtEnd:
 ;------------------------------------------------------------------------------\r
 global ASM_PFX(InternalAsmThunk16)\r
 ASM_PFX(InternalAsmThunk16):\r
+BITS    32\r
     push    ebp\r
     push    ebx\r
     push    esi\r