]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/Thunk16.nasm
MdePkg BaseLib NASM Thunk16: Remove remaining 'DB' code
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / Thunk16.nasm
1
2 #include "BaseLibInternals.h"
3
4 ;------------------------------------------------------------------------------
5 ;
6 ; Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
7 ; This program and the accompanying materials
8 ; are licensed and made available under the terms and conditions of the BSD License
9 ; which accompanies this distribution. The full text of the license may be found at
10 ; http://opensource.org/licenses/bsd-license.php.
11 ;
12 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 ;
15 ; Module Name:
16 ;
17 ; Thunk.asm
18 ;
19 ; Abstract:
20 ;
21 ; Real mode thunk
22 ;
23 ;------------------------------------------------------------------------------
24
25 global ASM_PFX(m16Size)
26 global ASM_PFX(mThunk16Attr)
27 global ASM_PFX(m16Gdt)
28 global ASM_PFX(m16GdtrBase)
29 global ASM_PFX(mTransition)
30 global ASM_PFX(m16Start)
31
32 struc IA32_REGS
33
34 ._EDI: resd 1
35 ._ESI: resd 1
36 ._EBP: resd 1
37 ._ESP: resd 1
38 ._EBX: resd 1
39 ._EDX: resd 1
40 ._ECX: resd 1
41 ._EAX: resd 1
42 ._DS: resw 1
43 ._ES: resw 1
44 ._FS: resw 1
45 ._GS: resw 1
46 ._EFLAGS: resd 1
47 ._EIP: resd 1
48 ._CS: resw 1
49 ._SS: resw 1
50 .size:
51
52 endstruc
53
54 ;; .const
55
56 SECTION .data
57
58 ;
59 ; These are global constant to convey information to C code.
60 ;
61 ASM_PFX(m16Size) DW InternalAsmThunk16 - ASM_PFX(m16Start)
62 ASM_PFX(mThunk16Attr) DW _BackFromUserCode.ThunkAttrEnd - 4 - ASM_PFX(m16Start)
63 ASM_PFX(m16Gdt) DW _NullSegDesc - ASM_PFX(m16Start)
64 ASM_PFX(m16GdtrBase) DW _16GdtrBase - ASM_PFX(m16Start)
65 ASM_PFX(mTransition) DW _EntryPoint - ASM_PFX(m16Start)
66
67 SECTION .text
68
69 ASM_PFX(m16Start):
70
71 SavedGdt:
72 dw 0
73 dd 0
74
75 ;------------------------------------------------------------------------------
76 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
77 ; by user code. It will be shadowed to somewhere in memory below 1MB.
78 ;------------------------------------------------------------------------------
79 _BackFromUserCode:
80 ;
81 ; The order of saved registers on the stack matches the order they appears
82 ; in IA32_REGS structure. This facilitates wrapper function to extract them
83 ; into that structure.
84 ;
85 BITS 16
86 push ss
87 push cs
88 o32 call dword .Base ; push eip
89 .Base:
90 pushfd
91 cli ; disable interrupts
92 push gs
93 push fs
94 push es
95 push ds
96 pushad
97 mov edx, strict dword 0
98 .ThunkAttrEnd:
99 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
100 jz .1
101 mov ax, 2401h
102 int 15h
103 cli ; disable interrupts
104 jnc .2
105 .1:
106 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
107 jz .2
108 in al, 92h
109 or al, 2
110 out 92h, al ; deactivate A20M#
111 .2:
112 xor eax, eax
113 mov ax, ss
114 lea ebp, [esp + IA32_REGS.size]
115 mov [bp - IA32_REGS.size + IA32_REGS._ESP], ebp
116 mov bx, [bp - IA32_REGS.size + IA32_REGS._EIP]
117 shl eax, 4 ; shl eax, 4
118 add ebp, eax ; add ebp, eax
119 mov eax, strict dword 0
120 .SavedCr4End:
121 mov cr4, eax
122 o32 lgdt [cs:bx + (SavedGdt - .Base)]
123 mov eax, strict dword 0
124 .SavedCr0End:
125 mov cr0, eax
126 mov ax, strict word 0
127 .SavedSsEnd:
128 mov ss, eax
129 mov esp, strict dword 0
130 .SavedEspEnd:
131 o32 retf ; return to protected mode
132
133 _EntryPoint:
134 DD _ToUserCode - ASM_PFX(m16Start)
135 DW 8h
136 _16Idtr:
137 DW (1 << 10) - 1
138 DD 0
139 _16Gdtr:
140 DW GdtEnd - _NullSegDesc - 1
141 _16GdtrBase:
142 DD _NullSegDesc
143
144 ;------------------------------------------------------------------------------
145 ; _ToUserCode() takes control in real mode before passing control to user code.
146 ; It will be shadowed to somewhere in memory below 1MB.
147 ;------------------------------------------------------------------------------
148 _ToUserCode:
149 BITS 16
150 mov dx, ss
151 mov ss, cx ; set new segment selectors
152 mov ds, cx
153 mov es, cx
154 mov fs, cx
155 mov gs, cx
156 mov cr0, eax ; real mode starts at next instruction
157 ; which (per SDM) *must* be a far JMP.
158 jmp 0:strict word 0
159 .RealAddrEnd:
160 mov cr4, ebp
161 mov ss, si ; set up 16-bit stack segment
162 xchg esp, ebx ; set up 16-bit stack pointer
163 mov bp, [esp + IA32_REGS.size]
164 mov [cs:bp + (_BackFromUserCode.SavedSsEnd - 2 - _BackFromUserCode)], dx
165 mov [cs:bp + (_BackFromUserCode.SavedEspEnd - 4 - _BackFromUserCode)], ebx
166 lidt [cs:bp + (_16Idtr - _BackFromUserCode)]
167
168 popad
169 pop ds
170 pop es
171 pop fs
172 pop gs
173 popfd
174
175 o32 retf ; transfer control to user code
176
177 ALIGN 16
178 _NullSegDesc DQ 0
179 _16CsDesc:
180 DW -1
181 DW 0
182 DB 0
183 DB 9bh
184 DB 8fh ; 16-bit segment, 4GB limit
185 DB 0
186 _16DsDesc:
187 DW -1
188 DW 0
189 DB 0
190 DB 93h
191 DB 8fh ; 16-bit segment, 4GB limit
192 DB 0
193 GdtEnd:
194
195 ;------------------------------------------------------------------------------
196 ; IA32_REGISTER_SET *
197 ; EFIAPI
198 ; InternalAsmThunk16 (
199 ; IN IA32_REGISTER_SET *RegisterSet,
200 ; IN OUT VOID *Transition
201 ; );
202 ;------------------------------------------------------------------------------
203 global ASM_PFX(InternalAsmThunk16)
204 ASM_PFX(InternalAsmThunk16):
205 BITS 32
206 push ebp
207 push ebx
208 push esi
209 push edi
210 push ds
211 push es
212 push fs
213 push gs
214 mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter
215 movzx edx, word [esi + IA32_REGS._SS]
216 mov edi, [esi + IA32_REGS._ESP]
217 add edi, - (IA32_REGS.size + 4) ; reserve stack space
218 mov ebx, edi ; ebx <- stack offset
219 imul eax, edx, 16 ; eax <- edx * 16
220 push IA32_REGS.size / 4
221 add edi, eax ; edi <- linear address of 16-bit stack
222 pop ecx
223 rep movsd ; copy RegSet
224 mov eax, [esp + 40] ; eax <- address of transition code
225 mov esi, edx ; esi <- 16-bit stack segment
226 lea edx, [eax + (_BackFromUserCode.SavedCr0End - ASM_PFX(m16Start))]
227 mov ecx, eax
228 and ecx, 0fh
229 shl eax, 12
230 lea ecx, [ecx + (_BackFromUserCode - ASM_PFX(m16Start))]
231 mov ax, cx
232 stosd ; [edi] <- return address of user code
233 add eax, _ToUserCode.RealAddrEnd - _BackFromUserCode
234 mov [edx + (_ToUserCode.RealAddrEnd - 4 - _BackFromUserCode.SavedCr0End)], eax
235 sgdt [edx + (SavedGdt - _BackFromUserCode.SavedCr0End)]
236 sidt [esp + 36] ; save IDT stack in argument space
237 mov eax, cr0
238 mov [edx - 4], eax ; save CR0 in _BackFromUserCode.SavedCr0End - 4
239 and eax, 7ffffffeh ; clear PE, PG bits
240 mov ebp, cr4
241 mov [edx + (_BackFromUserCode.SavedCr4End - 4 - _BackFromUserCode.SavedCr0End)], ebp
242 and ebp, ~30h ; clear PAE, PSE bits
243 push 10h
244 pop ecx ; ecx <- selector for data segments
245 lgdt [edx + (_16Gdtr - _BackFromUserCode.SavedCr0End)]
246 pushfd ; Save df/if indeed
247 call dword far [edx + (_EntryPoint - _BackFromUserCode.SavedCr0End)]
248 popfd
249 lidt [esp + 36] ; restore protected mode IDTR
250 lea eax, [ebp - IA32_REGS.size] ; eax <- the address of IA32_REGS
251 pop gs
252 pop fs
253 pop es
254 pop ds
255 pop edi
256 pop esi
257 pop ebx
258 pop ebp
259 ret