[edk2] [PATCH 8/8] MdePkg/BaseLib NASM Thunk16: Remove remaining 'DB' code

Subject: [edk2] [PATCH 8/8] MdePkg/BaseLib NASM Thunk16: Remove remaining 'DB' code

From: Jordan Justen <jordan.l.justen@intel.com>

To: edk2-devel@lists.sourceforge.net

Date: 2014-08-20 00:57:37

  • 2014-08-20 00:57:37  Jordan Justen   [edk2] [PATCH 8/8] MdePkg/BaseLib NASM Thunk16: Remove remaining 'DB' code
Convert remaining 'DB' code to assembly code by:
* Move instruction immediate data labels to end of instruction
* Use strict keyword to make sure immediate data size is not optimized

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen 
---
 MdePkg/Library/BaseLib/Ia32/Thunk16.nasm | 47 +++++++++++++++----------------
 MdePkg/Library/BaseLib/X64/Thunk16.nasm  | 48 +++++++++++++++-----------------
 2 files changed, 46 insertions(+), 49 deletions(-)

diff --git a/MdePkg/Library/BaseLib/Ia32/Thunk16.nasm b/MdePkg/Library/BaseLib/Ia32/Thunk16.nasm
index 998cf2b..2e5a580 100644
--- a/MdePkg/Library/BaseLib/Ia32/Thunk16.nasm
+++ b/MdePkg/Library/BaseLib/Ia32/Thunk16.nasm
@@ -59,7 +59,7 @@ SECTION .data
 ; These are global constant to convey information to C code.
 ;
 ASM_PFX(m16Size)         DW      InternalAsmThunk16 - m16Start
-ASM_PFX(mThunk16Attr)    DW      _BackFromUserCode.ThunkAttr - m16Start
+ASM_PFX(mThunk16Attr)    DW      _BackFromUserCode.ThunkAttrEnd - 4 - m16Start
 ASM_PFX(m16Gdt)          DW      _NullSegDesc - m16Start
 ASM_PFX(m16GdtrBase)     DW      _16GdtrBase - m16Start
 ASM_PFX(mTransition)     DW      _EntryPoint - m16Start
@@ -94,8 +94,8 @@ a32 call    .Base                       ; push eip
     push    es
     push    ds
     pushad
-    DB      66h, 0bah                   ; mov edx, imm32
-.ThunkAttr: dd   0
+    mov     edx, strict dword 0
+.ThunkAttrEnd:
     test    dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
     jz      .1
     mov     ax, 2401h
@@ -116,18 +116,18 @@ a32 call    .Base                       ; push eip
     mov     bx, [bp - IA32_REGS.size + IA32_REGS._EIP]
     shl     eax, 4                      ; shl eax, 4
     add     ebp, eax                    ; add ebp, eax
-    DB      66h, 0b8h                   ; mov eax, imm32
-.SavedCr4:  DD      0
+    mov     eax, strict dword 0
+.SavedCr4End:
     mov     cr4, eax
 o32 lgdt [cs:bx + (SavedGdt - .Base)]
-    DB      66h, 0b8h                   ; mov eax, imm32
-.SavedCr0:  DD      0
+    mov     eax, strict dword 0
+.SavedCr0End:
     mov     cr0, eax
-    DB      0b8h                        ; mov ax, imm16
-.SavedSs    DW      0
+    mov     ax, strict word 0
+.SavedSsEnd:
     mov     ss, eax
-    DB      66h, 0bch                   ; mov esp, imm32
-.SavedEsp   DD      0
+    mov     esp, strict dword 0
+.SavedEspEnd:
 o32 retf                                ; return to protected mode
 
 _EntryPoint:
@@ -155,15 +155,14 @@ BITS    16
     mov     gs, cx
     mov     cr0, eax                    ; real mode starts at next instruction
                                         ;  which (per SDM) *must* be a far JMP.
-    DB      0eah
-.RealAddr: DW 0, 0
-
+    jmp     0:strict word 0
+.RealAddrEnd:
     mov     cr4, ebp
     mov     ss, si                      ; set up 16-bit stack segment
     xchg    esp, ebx                    ; set up 16-bit stack pointer
     mov     bp, [esp + IA32_REGS.size]
-    mov     [cs:bp + (_BackFromUserCode.SavedSs - _BackFromUserCode)], dx
-    mov     [cs:bp + (_BackFromUserCode.SavedEsp - _BackFromUserCode)], ebx
+    mov     [cs:bp + (_BackFromUserCode.SavedSsEnd - 2 - _BackFromUserCode)], dx
+    mov     [cs:bp + (_BackFromUserCode.SavedEspEnd - 4 - _BackFromUserCode)], ebx
     lidt    [cs:bp + (_16Idtr - _BackFromUserCode)]
 
     popad
@@ -224,7 +223,7 @@ BITS    32
     rep     movsd                       ; copy RegSet
     mov     eax, [esp + 40]             ; eax <- address of transition code
     mov     esi, edx                    ; esi <- 16-bit stack segment
-    lea     edx, [eax + (_BackFromUserCode.SavedCr0 - m16Start)]
+    lea     edx, [eax + (_BackFromUserCode.SavedCr0End - m16Start)]
     nop
     mov     ecx, eax
     and     ecx, 0fh
@@ -232,21 +231,21 @@ BITS    32
     lea     ecx, [ecx + (_BackFromUserCode - m16Start)]
     mov     ax, cx
     stosd                               ; [edi] <- return address of user code
-    add     eax, _ToUserCode.RealAddr + 4 - _BackFromUserCode
-    mov     [edx + (_ToUserCode.RealAddr - _BackFromUserCode.SavedCr0)], eax
-    sgdt    [edx + (SavedGdt - _BackFromUserCode.SavedCr0)]
+    add     eax, _ToUserCode.RealAddrEnd - _BackFromUserCode
+    mov     [edx + (_ToUserCode.RealAddrEnd - 4 - _BackFromUserCode.SavedCr0End)], eax
+    sgdt    [edx + (SavedGdt - _BackFromUserCode.SavedCr0End)]
     sidt    [esp + 36]        ; save IDT stack in argument space
     mov     eax, cr0
-    mov     [edx], eax                  ; save CR0 in _BackFromUserCode.SavedCr0
+    mov     [edx - 4], eax                  ; save CR0 in _BackFromUserCode.SavedCr0End - 4
     and     eax, 7ffffffeh              ; clear PE, PG bits
     mov     ebp, cr4
-    mov     [edx + (_BackFromUserCode.SavedCr4 - _BackFromUserCode.SavedCr0)], ebp
+    mov     [edx + (_BackFromUserCode.SavedCr4End - 4 - _BackFromUserCode.SavedCr0End)], ebp
     and     ebp, ~30h                ; clear PAE, PSE bits
     push    10h
     pop     ecx                         ; ecx <- selector for data segments
-    lgdt    [edx + (_16Gdtr - _BackFromUserCode.SavedCr0)]
+    lgdt    [edx + (_16Gdtr - _BackFromUserCode.SavedCr0End)]
     pushfd                              ; Save df/if indeed
-    call    dword far [edx + (_EntryPoint - _BackFromUserCode.SavedCr0)]
+    call    dword far [edx + (_EntryPoint - _BackFromUserCode.SavedCr0End)]
     popfd
     lidt    [esp + 36]        ; restore protected mode IDTR
     lea     eax, [ebp - IA32_REGS.size] ; eax <- the address of IA32_REGS
diff --git a/MdePkg/Library/BaseLib/X64/Thunk16.nasm b/MdePkg/Library/BaseLib/X64/Thunk16.nasm
index 4073cdb..8858e74 100644
--- a/MdePkg/Library/BaseLib/X64/Thunk16.nasm
+++ b/MdePkg/Library/BaseLib/X64/Thunk16.nasm
@@ -57,7 +57,7 @@ SECTION .data
 ; These are global constant to convey information to C code.
 ;
 ASM_PFX(m16Size)         DW      InternalAsmThunk16 - m16Start
-ASM_PFX(mThunk16Attr)    DW      _BackFromUserCode.ThunkAttr - m16Start
+ASM_PFX(mThunk16Attr)    DW      _BackFromUserCode.ThunkAttrEnd - 4 - m16Start
 ASM_PFX(m16Gdt)          DW      _NullSeg - m16Start
 ASM_PFX(m16GdtrBase)     DW      _16GdtrBase - m16Start
 ASM_PFX(mTransition)     DW      _EntryPoint - m16Start
@@ -93,8 +93,8 @@ o32 call    dword .Base                 ; push eip
     push    es
     push    ds
     pushad
-    DB      66h, 0bah                   ; mov edx, imm32
-.ThunkAttr: dd   0
+    mov     edx, strict dword 0
+.ThunkAttrEnd:
     test    dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
     jz      .1
     mov     ax, 2401h
@@ -117,27 +117,25 @@ o32 call    dword .Base                 ; push eip
     add     ebp, eax                    ; add ebp, eax
     mov     eax, cs
     shl     eax, 4
-    lea     eax, [eax + ebx + (.64BitCode - .Base)]
-    mov     [cs:bx + (.64Eip - .Base)], eax
-    DB      66h, 0b8h                   ; mov eax, imm32
-.SavedCr4:  DD      0
+    lea     eax, [eax + ebx + (.X64JmpEnd - .Base)]
+    mov     [cs:bx + (.X64JmpEnd - 6 - .Base)], eax
+    mov     eax, strict dword 0
+.SavedCr4End:
     mov     cr4, eax
 o32 lgdt [cs:bx + (SavedGdt - .Base)]
     mov     ecx, 0c0000080h
     rdmsr
     or      ah, 1
     wrmsr
-    DB      66h, 0b8h                   ; mov eax, imm32
-.SavedCr0:  DD      0
+    mov     eax, strict dword 0
+.SavedCr0End:
     mov     cr0, eax
-    DB      66h, 0eah                   ; jmp far cs:.64Bit
-.64Eip:     DD      0
-.SavedCs:   DW      0
-.64BitCode:
+    jmp     0:strict dword 0
+.X64JmpEnd:
 BITS    64
     nop
-    db      048h, 0bch                 ; mov rsp, imm64
-.SavedSp:   DQ   0                     ; restore stack
+    mov rsp, strict qword 0
+.SavedSpEnd:
     nop
     ret
 
@@ -258,7 +256,7 @@ BITS    64
     add     edi, eax                    ; edi <- linear address of 16-bit stack
     pop     rcx
     rep     movsd                       ; copy RegSet
-    lea     ecx, [rdx + (_BackFromUserCode.SavedCr4 - m16Start)]
+    lea     ecx, [rdx + (_BackFromUserCode.SavedCr4End - m16Start)]
     mov     eax, edx                    ; eax <- transition code address
     and     edx, 0fh
     shl     eax, 12                     ; segment address in high order 16 bits
@@ -267,11 +265,11 @@ BITS    64
   
     sgdt    [rsp + 60h]       ; save GDT stack in argument space
     movzx   r10, word [rsp + 60h]   ; r10 <- GDT limit 
-    lea     r11, [rcx + (InternalAsmThunk16 - _BackFromUserCode.SavedCr4) + 0xf]
+    lea     r11, [rcx + (InternalAsmThunk16 - _BackFromUserCode.SavedCr4End) + 0xf]
     and     r11, ~0xf            ; r11 <- 16-byte aligned shadowed GDT table in real mode buffer
     
-    mov     [rcx + (SavedGdt - _BackFromUserCode.SavedCr4)], r10w      ; save the limit of shadowed GDT table
-    mov     [rcx + (SavedGdt - _BackFromUserCode.SavedCr4) + 2], r11  ; save the base address of shadowed GDT table
+    mov     [rcx + (SavedGdt - _BackFromUserCode.SavedCr4End)], r10w      ; save the limit of shadowed GDT table
+    mov     [rcx + (SavedGdt - _BackFromUserCode.SavedCr4End) + 2], r11  ; save the base address of shadowed GDT table
     
     mov     rsi, [rsp + 62h]  ; rsi <- the original GDT base address
     xchg    rcx, r10                    ; save rcx to r10 and initialize rcx to be the limit of GDT table
@@ -283,24 +281,24 @@ BITS    64
     
     sidt    [rsp + 50h]       ; save IDT stack in argument space
     mov     rax, cr0
-    mov     [rcx + (_BackFromUserCode.SavedCr0 - _BackFromUserCode.SavedCr4)], eax
+    mov     [rcx + (_BackFromUserCode.SavedCr0End - 4 - _BackFromUserCode.SavedCr4End)], eax
     and     eax, 7ffffffeh              ; clear PE, PG bits
     mov     rbp, cr4
-    mov     [rcx], ebp                  ; save CR4 in _BackFromUserCode.SavedCr4
+    mov     [rcx - 4], ebp              ; save CR4 in _BackFromUserCode.SavedCr4End - 4
     and     ebp, ~30h                ; clear PAE, PSE bits
     mov     esi, r8d                    ; esi <- 16-bit stack segment
     push    DATA32
     pop     rdx                         ; rdx <- 32-bit data segment selector
-    lgdt    [rcx + (_16Gdtr - _BackFromUserCode.SavedCr4)]
+    lgdt    [rcx + (_16Gdtr - _BackFromUserCode.SavedCr4End)]
     mov     ss, edx
     pushfq
     lea     edx, [rdx + DATA16 - DATA32]
     lea     r8, [.RetFromRealMode]
     push    r8
     mov     r8d, cs
-    mov     [rcx + (_BackFromUserCode.SavedCs - _BackFromUserCode.SavedCr4)], r8w
-    mov     [rcx + (_BackFromUserCode.SavedSp - _BackFromUserCode.SavedCr4)], rsp
-    jmp     dword far [rcx + (_EntryPoint - _BackFromUserCode.SavedCr4)]
+    mov     [rcx + (_BackFromUserCode.X64JmpEnd - 2 - _BackFromUserCode.SavedCr4End)], r8w
+    mov     [rcx + (_BackFromUserCode.SavedSpEnd - 8 - _BackFromUserCode.SavedCr4End)], rsp
+    jmp     dword far [rcx + (_EntryPoint - _BackFromUserCode.SavedCr4End)]
 .RetFromRealMode:
     popfq
     lgdt    [rsp + 60h]       ; restore protected mode GDTR
-- 
2.1.0


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel