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