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