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