Modified coding style, removed unnecessary comments and "offset" key words.
[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
878ddf1f 32IA32_REGS STRUC 4t\r
33_EDI DD ?\r
34_ESI DD ?\r
35_EBP DD ?\r
36_ESP DD ?\r
37_EBX DD ?\r
38_EDX DD ?\r
39_ECX DD ?\r
40_EAX DD ?\r
41_DS DW ?\r
42_ES DW ?\r
43_FS DW ?\r
44_GS DW ?\r
45_EFLAGS DD ?\r
46_EIP DD ?\r
47_CS DW ?\r
48_SS DW ?\r
49IA32_REGS ENDS\r
50\r
97d92bda 51 .const\r
52\r
bbe4ba81 53m16Size DW InternalAsmThunk16 - m16Start\r
54mThunk16Attr DW _ThunkAttr - m16Start\r
55m16Gdt DW _NullSegDesc - m16Start\r
56m16GdtrBase DW _16GdtrBase - m16Start\r
57mTransition DW _EntryPoint - m16Start\r
97d92bda 58\r
59 .code\r
60\r
61m16Start LABEL BYTE\r
62\r
bbe4ba81 63SavedGdt LABEL FWORD\r
64 DW ?\r
65 DD ?\r
97d92bda 66\r
67_BackFromUserCode PROC\r
68 push ss\r
69 push cs\r
878ddf1f 70 DB 66h\r
97d92bda 71 call @Base ; push eip\r
72@Base:\r
878ddf1f 73 pushf ; pushfd actually\r
97d92bda 74 cli ; disable interrupts\r
878ddf1f 75 push gs\r
76 push fs\r
77 push es\r
78 push ds\r
79 pushaw ; pushad actually\r
97d92bda 80 DB 66h, 0bah ; mov edx, imm32\r
81_ThunkAttr DD ?\r
82 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15\r
83 jz @1\r
84 mov eax, 15cd2401h ; mov ax, 2401h & int 15h\r
85 cli ; disable interrupts\r
86 jnc @2\r
87@1:\r
88 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL\r
89 jz @2\r
90 in al, 92h\r
91 or al, 2\r
92 out 92h, al ; deactivate A20M#\r
93@2:\r
878ddf1f 94 mov eax, ss\r
95 DB 67h\r
97d92bda 96 lea bp, [esp + sizeof (IA32_REGS)]\r
97 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp\r
98 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP\r
99 shl ax, 4 ; shl eax, 4\r
100 add bp, ax ; add ebp, eax\r
101 DB 66h, 0b8h ; mov eax, imm32\r
102SavedCr4 DD ?\r
103 mov cr4, eax\r
104 DB 66h\r
bbe4ba81 105 lgdt fword ptr cs:[edi + (SavedGdt - @Base)]\r
97d92bda 106 DB 66h, 0b8h ; mov eax, imm32\r
107SavedCr0 DD ?\r
108 mov cr0, eax\r
109 DB 0b8h ; mov ax, imm16\r
110SavedSs DW ?\r
111 mov ss, eax\r
112 DB 66h, 0bch ; mov esp, imm32\r
113SavedEsp DD ?\r
878ddf1f 114 DB 66h\r
97d92bda 115 retf ; return to protected mode\r
116_BackFromUserCode ENDP\r
117\r
bbe4ba81 118_EntryPoint DD _ToUserCode - m16Start\r
119 DW 8h\r
120_16Idtr FWORD (1 SHL 10) - 1\r
121_16Gdtr LABEL FWORD\r
122 DW GdtEnd - _NullSegDesc - 1\r
123_16GdtrBase DD _NullSegDesc\r
97d92bda 124\r
125_ToUserCode PROC\r
126 mov edx, ss\r
127 mov ss, ecx ; set new segment selectors\r
128 mov ds, ecx\r
129 mov es, ecx\r
130 mov fs, ecx\r
131 mov gs, ecx\r
132 mov cr0, eax\r
133 mov cr4, ebp ; real mode starts at next instruction\r
134 mov ss, esi ; set up 16-bit stack segment\r
135 xchg sp, bx ; set up 16-bit stack pointer\r
136 DB 66h\r
137 call @Base ; push eip\r
138@Base:\r
bbe4ba81 139 pop bp ; ebp <- address of @Base\r
18c319ae 140 DB 67h ; address size override\r
141 push [esp + sizeof (IA32_REGS) + 2]\r
bbe4ba81 142 lea eax, [esi + (@RealMode - @Base)]\r
18c319ae 143 push eax\r
144 retf\r
145@RealMode:\r
bbe4ba81 146 mov cs:[esi + (SavedSs - @Base)], edx\r
147 mov cs:[esi + (SavedEsp - @Base)], bx\r
97d92bda 148 DB 66h\r
bbe4ba81 149 lidt fword ptr cs:[esi + (_16Idtr - @Base)]\r
97d92bda 150 popaw ; popad actually\r
151 pop ds\r
152 pop es\r
153 pop fs\r
154 pop gs\r
155 popf ; popfd\r
156 DB 66h\r
157 retf ; transfer control to user code\r
158_ToUserCode ENDP\r
159\r
160_NullSegDesc DQ 0\r
161_16CsDesc LABEL QWORD\r
162 DW -1\r
163 DW 0\r
164 DB 0\r
165 DB 9bh\r
166 DB 8fh ; 16-bit segment, 4GB limit\r
167 DB 0\r
168_16DsDesc LABEL QWORD\r
169 DW -1\r
170 DW 0\r
171 DB 0\r
172 DB 93h\r
173 DB 8fh ; 16-bit segment, 4GB limit\r
174 DB 0\r
175GdtEnd LABEL QWORD\r
176\r
177;\r
178; @param RegSet Pointer to a IA32_DWORD_REGS structure\r
179; @param Transition Pointer to the transition code\r
180; @return The address of the 16-bit stack after returning from user code\r
181;\r
182InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs\r
183 mov esi, [esp + 36] ; esi <- RegSet\r
184 movzx edx, (IA32_REGS ptr [esi])._SS\r
185 mov edi, (IA32_REGS ptr [esi])._ESP\r
186 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space\r
187 mov ebx, edi ; ebx <- stack offset\r
188 imul eax, edx, 16 ; eax <- edx * 16\r
189 push sizeof (IA32_REGS) / 4\r
190 add edi, eax ; edi <- linear address of 16-bit stack\r
191 pop ecx\r
192 rep movsd ; copy RegSet\r
193 mov eax, [esp + 40] ; eax <- address of transition code\r
194 mov esi, edx ; esi <- 16-bit stack segment\r
bbe4ba81 195 lea edx, [eax + (SavedCr0 - m16Start)]\r
97d92bda 196 mov ecx, eax\r
197 and ecx, 0fh\r
198 shl eax, 12\r
bbe4ba81 199 lea ecx, [ecx + (_BackFromUserCode - m16Start)]\r
97d92bda 200 mov ax, cx\r
201 stosd ; [edi] <- return address of user code\r
bbe4ba81 202 sgdt fword ptr [edx + (SavedGdt - SavedCr0)]\r
97d92bda 203 sidt fword ptr [esp + 36] ; save IDT stack in argument space\r
204 mov eax, cr0\r
205 mov [edx], eax ; save CR0 in SavedCr0\r
206 and eax, 7ffffffeh ; clear PE, PG bits\r
207 mov ebp, cr4\r
bbe4ba81 208 mov [edx + (SavedCr4 - SavedCr0)], ebp\r
97d92bda 209 and ebp, 300h ; clear all but PCE and OSFXSR bits\r
210 push 10h\r
211 pop ecx ; ecx <- selector for data segments\r
bbe4ba81 212 lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]\r
18c319ae 213 pushfd\r
bbe4ba81 214 call fword ptr [edx + (_EntryPoint - SavedCr0)]\r
18c319ae 215 popfd\r
878ddf1f 216 lidt fword ptr [esp + 36] ; restore protected mode IDTR\r
97d92bda 217 lea eax, [ebp - sizeof (IA32_REGS)]\r
878ddf1f 218 ret\r
219InternalAsmThunk16 ENDP\r
220\r
221 END\r