]>
Commit | Line | Data |
---|---|---|
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 | 25 | EXTERNDEF C m16Start:BYTE\r |
26 | EXTERNDEF C m16Size:WORD\r | |
27 | EXTERNDEF C mThunk16Attr:WORD\r | |
28 | EXTERNDEF C m16Gdt:WORD\r | |
29 | EXTERNDEF C m16GdtrBase:WORD\r | |
30 | EXTERNDEF C mTransition:WORD\r | |
878ddf1f | 31 | \r |
3f566587 | 32 | ;\r |
33 | ; Here is the layout of the real mode stack. _ToUserCode() is responsible for\r | |
34 | ; loading all these registers from real mode stack.\r | |
35 | ;\r | |
878ddf1f | 36 | IA32_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 | |
53 | IA32_REGS ENDS\r | |
54 | \r | |
97d92bda | 55 | .const\r |
56 | \r | |
3f566587 | 57 | ;\r |
58 | ; These are global constant to convey information to C code.\r | |
59 | ;\r | |
bbe4ba81 | 60 | m16Size DW InternalAsmThunk16 - m16Start\r |
61 | mThunk16Attr DW _ThunkAttr - m16Start\r | |
62 | m16Gdt DW _NullSegDesc - m16Start\r | |
63 | m16GdtrBase DW _16GdtrBase - m16Start\r | |
64 | mTransition DW _EntryPoint - m16Start\r | |
97d92bda | 65 | \r |
66 | .code\r | |
67 | \r | |
68 | m16Start LABEL BYTE\r | |
69 | \r | |
bbe4ba81 | 70 | SavedGdt LABEL FWORD\r |
71 | DW ?\r | |
72 | DD ?\r | |
3f566587 | 73 | ;------------------------------------------------------------------------------\r |
74 | ; _BackFromUserCode() takes control in real mode after 'retf' has been executed\r | |
75 | ; by user code. It will be shadowed to somewhere in memory below 1MB.\r | |
76 | ;------------------------------------------------------------------------------\r | |
97d92bda | 77 | _BackFromUserCode PROC\r |
78 | push ss\r | |
79 | push cs\r | |
878ddf1f | 80 | DB 66h\r |
97d92bda | 81 | call @Base ; push eip\r |
82 | @Base:\r | |
878ddf1f | 83 | pushf ; pushfd actually\r |
97d92bda | 84 | cli ; disable interrupts\r |
878ddf1f | 85 | push gs\r |
86 | push fs\r | |
87 | push es\r | |
88 | push ds\r | |
89 | pushaw ; pushad actually\r | |
97d92bda | 90 | DB 66h, 0bah ; mov edx, imm32\r |
91 | _ThunkAttr DD ?\r | |
92 | test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15\r | |
93 | jz @1\r | |
94 | mov eax, 15cd2401h ; mov ax, 2401h & int 15h\r | |
95 | cli ; disable interrupts\r | |
96 | jnc @2\r | |
97 | @1:\r | |
98 | test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL\r | |
99 | jz @2\r | |
100 | in al, 92h\r | |
101 | or al, 2\r | |
102 | out 92h, al ; deactivate A20M#\r | |
103 | @2:\r | |
878ddf1f | 104 | mov eax, ss\r |
105 | DB 67h\r | |
97d92bda | 106 | lea bp, [esp + sizeof (IA32_REGS)]\r |
107 | mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp\r | |
108 | mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP\r | |
109 | shl ax, 4 ; shl eax, 4\r | |
110 | add bp, ax ; add ebp, eax\r | |
111 | DB 66h, 0b8h ; mov eax, imm32\r | |
112 | SavedCr4 DD ?\r | |
113 | mov cr4, eax\r | |
114 | DB 66h\r | |
bbe4ba81 | 115 | lgdt fword ptr cs:[edi + (SavedGdt - @Base)]\r |
97d92bda | 116 | DB 66h, 0b8h ; mov eax, imm32\r |
117 | SavedCr0 DD ?\r | |
118 | mov cr0, eax\r | |
119 | DB 0b8h ; mov ax, imm16\r | |
120 | SavedSs DW ?\r | |
121 | mov ss, eax\r | |
122 | DB 66h, 0bch ; mov esp, imm32\r | |
123 | SavedEsp DD ?\r | |
878ddf1f | 124 | DB 66h\r |
97d92bda | 125 | retf ; return to protected mode\r |
126 | _BackFromUserCode ENDP\r | |
127 | \r | |
bbe4ba81 | 128 | _EntryPoint DD _ToUserCode - m16Start\r |
129 | DW 8h\r | |
130 | _16Idtr FWORD (1 SHL 10) - 1\r | |
131 | _16Gdtr LABEL FWORD\r | |
132 | DW GdtEnd - _NullSegDesc - 1\r | |
133 | _16GdtrBase DD _NullSegDesc\r | |
97d92bda | 134 | \r |
3f566587 | 135 | ;------------------------------------------------------------------------------\r |
136 | ; _ToUserCode() takes control in real mode before passing control to user code.\r | |
137 | ; It will be shadowed to somewhere in memory below 1MB.\r | |
138 | ;------------------------------------------------------------------------------\r | |
97d92bda | 139 | _ToUserCode PROC\r |
140 | mov edx, ss\r | |
141 | mov ss, ecx ; set new segment selectors\r | |
142 | mov ds, ecx\r | |
143 | mov es, ecx\r | |
144 | mov fs, ecx\r | |
145 | mov gs, ecx\r | |
146 | mov cr0, eax\r | |
147 | mov cr4, ebp ; real mode starts at next instruction\r | |
148 | mov ss, esi ; set up 16-bit stack segment\r | |
149 | xchg sp, bx ; set up 16-bit stack pointer\r | |
150 | DB 66h\r | |
151 | call @Base ; push eip\r | |
152 | @Base:\r | |
bbe4ba81 | 153 | pop bp ; ebp <- address of @Base\r |
18c319ae | 154 | DB 67h ; address size override\r |
155 | push [esp + sizeof (IA32_REGS) + 2]\r | |
bbe4ba81 | 156 | lea eax, [esi + (@RealMode - @Base)]\r |
18c319ae | 157 | push eax\r |
158 | retf\r | |
159 | @RealMode:\r | |
bbe4ba81 | 160 | mov cs:[esi + (SavedSs - @Base)], edx\r |
161 | mov cs:[esi + (SavedEsp - @Base)], bx\r | |
97d92bda | 162 | DB 66h\r |
bbe4ba81 | 163 | lidt fword ptr cs:[esi + (_16Idtr - @Base)]\r |
97d92bda | 164 | popaw ; popad actually\r |
165 | pop ds\r | |
166 | pop es\r | |
167 | pop fs\r | |
168 | pop gs\r | |
169 | popf ; popfd\r | |
170 | DB 66h\r | |
171 | retf ; transfer control to user code\r | |
172 | _ToUserCode ENDP\r | |
173 | \r | |
174 | _NullSegDesc DQ 0\r | |
175 | _16CsDesc LABEL QWORD\r | |
176 | DW -1\r | |
177 | DW 0\r | |
178 | DB 0\r | |
179 | DB 9bh\r | |
180 | DB 8fh ; 16-bit segment, 4GB limit\r | |
181 | DB 0\r | |
182 | _16DsDesc LABEL QWORD\r | |
183 | DW -1\r | |
184 | DW 0\r | |
185 | DB 0\r | |
186 | DB 93h\r | |
187 | DB 8fh ; 16-bit segment, 4GB limit\r | |
188 | DB 0\r | |
189 | GdtEnd LABEL QWORD\r | |
190 | \r | |
3f566587 | 191 | ;------------------------------------------------------------------------------\r |
192 | ; IA32_REGISTER_SET *\r | |
193 | ; EFIAPI\r | |
194 | ; InternalAsmThunk16 (\r | |
195 | ; IN IA32_REGISTER_SET *RegisterSet,\r | |
196 | ; IN OUT VOID *Transition\r | |
197 | ; );\r | |
198 | ;------------------------------------------------------------------------------\r | |
97d92bda | 199 | InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs\r |
200 | mov esi, [esp + 36] ; esi <- RegSet\r | |
201 | movzx edx, (IA32_REGS ptr [esi])._SS\r | |
202 | mov edi, (IA32_REGS ptr [esi])._ESP\r | |
203 | add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space\r | |
204 | mov ebx, edi ; ebx <- stack offset\r | |
205 | imul eax, edx, 16 ; eax <- edx * 16\r | |
206 | push sizeof (IA32_REGS) / 4\r | |
207 | add edi, eax ; edi <- linear address of 16-bit stack\r | |
208 | pop ecx\r | |
209 | rep movsd ; copy RegSet\r | |
210 | mov eax, [esp + 40] ; eax <- address of transition code\r | |
211 | mov esi, edx ; esi <- 16-bit stack segment\r | |
bbe4ba81 | 212 | lea edx, [eax + (SavedCr0 - m16Start)]\r |
97d92bda | 213 | mov ecx, eax\r |
214 | and ecx, 0fh\r | |
215 | shl eax, 12\r | |
bbe4ba81 | 216 | lea ecx, [ecx + (_BackFromUserCode - m16Start)]\r |
97d92bda | 217 | mov ax, cx\r |
218 | stosd ; [edi] <- return address of user code\r | |
bbe4ba81 | 219 | sgdt fword ptr [edx + (SavedGdt - SavedCr0)]\r |
97d92bda | 220 | sidt fword ptr [esp + 36] ; save IDT stack in argument space\r |
221 | mov eax, cr0\r | |
222 | mov [edx], eax ; save CR0 in SavedCr0\r | |
223 | and eax, 7ffffffeh ; clear PE, PG bits\r | |
224 | mov ebp, cr4\r | |
bbe4ba81 | 225 | mov [edx + (SavedCr4 - SavedCr0)], ebp\r |
97d92bda | 226 | and ebp, 300h ; clear all but PCE and OSFXSR bits\r |
227 | push 10h\r | |
228 | pop ecx ; ecx <- selector for data segments\r | |
bbe4ba81 | 229 | lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]\r |
18c319ae | 230 | pushfd\r |
bbe4ba81 | 231 | call fword ptr [edx + (_EntryPoint - SavedCr0)]\r |
18c319ae | 232 | popfd\r |
878ddf1f | 233 | lidt fword ptr [esp + 36] ; restore protected mode IDTR\r |
97d92bda | 234 | lea eax, [ebp - sizeof (IA32_REGS)]\r |
878ddf1f | 235 | ret\r |
236 | InternalAsmThunk16 ENDP\r | |
237 | \r | |
238 | END\r |