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