]>
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 |
a9a812a0 | 78 | ;\r |
79 | ; The order of saved registers on the stack matches the order they appears\r | |
80 | ; in IA32_REGS structure. This facilitates wrapper function to extract them\r | |
81 | ; into that structure.\r | |
82 | ;\r | |
97d92bda | 83 | push ss\r |
84 | push cs\r | |
878ddf1f | 85 | DB 66h\r |
97d92bda | 86 | call @Base ; push eip\r |
87 | @Base:\r | |
878ddf1f | 88 | pushf ; pushfd actually\r |
97d92bda | 89 | cli ; disable interrupts\r |
878ddf1f | 90 | push gs\r |
91 | push fs\r | |
92 | push es\r | |
93 | push ds\r | |
94 | pushaw ; pushad actually\r | |
97d92bda | 95 | DB 66h, 0bah ; mov edx, imm32\r |
96 | _ThunkAttr DD ?\r | |
97 | test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15\r | |
98 | jz @1\r | |
99 | mov eax, 15cd2401h ; mov ax, 2401h & int 15h\r | |
100 | cli ; disable interrupts\r | |
101 | jnc @2\r | |
102 | @1:\r | |
103 | test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL\r | |
104 | jz @2\r | |
105 | in al, 92h\r | |
106 | or al, 2\r | |
107 | out 92h, al ; deactivate A20M#\r | |
108 | @2:\r | |
878ddf1f | 109 | mov eax, ss\r |
110 | DB 67h\r | |
97d92bda | 111 | lea bp, [esp + sizeof (IA32_REGS)]\r |
a9a812a0 | 112 | ;\r |
113 | ; esi's in the following 2 instructions are indeed bp in 16-bit code. Fact\r | |
114 | ; is "esi" in 32-bit addressing mode has the same encoding of "bp" in 16-\r | |
115 | ; bit addressing mode.\r | |
116 | ;\r | |
97d92bda | 117 | mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp\r |
118 | mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP\r | |
119 | shl ax, 4 ; shl eax, 4\r | |
120 | add bp, ax ; add ebp, eax\r | |
121 | DB 66h, 0b8h ; mov eax, imm32\r | |
122 | SavedCr4 DD ?\r | |
123 | mov cr4, eax\r | |
124 | DB 66h\r | |
bbe4ba81 | 125 | lgdt fword ptr cs:[edi + (SavedGdt - @Base)]\r |
97d92bda | 126 | DB 66h, 0b8h ; mov eax, imm32\r |
127 | SavedCr0 DD ?\r | |
128 | mov cr0, eax\r | |
129 | DB 0b8h ; mov ax, imm16\r | |
130 | SavedSs DW ?\r | |
131 | mov ss, eax\r | |
132 | DB 66h, 0bch ; mov esp, imm32\r | |
133 | SavedEsp DD ?\r | |
878ddf1f | 134 | DB 66h\r |
97d92bda | 135 | retf ; return to protected mode\r |
136 | _BackFromUserCode ENDP\r | |
137 | \r | |
bbe4ba81 | 138 | _EntryPoint DD _ToUserCode - m16Start\r |
139 | DW 8h\r | |
140 | _16Idtr FWORD (1 SHL 10) - 1\r | |
141 | _16Gdtr LABEL FWORD\r | |
142 | DW GdtEnd - _NullSegDesc - 1\r | |
143 | _16GdtrBase DD _NullSegDesc\r | |
97d92bda | 144 | \r |
3f566587 | 145 | ;------------------------------------------------------------------------------\r |
146 | ; _ToUserCode() takes control in real mode before passing control to user code.\r | |
147 | ; It will be shadowed to somewhere in memory below 1MB.\r | |
148 | ;------------------------------------------------------------------------------\r | |
97d92bda | 149 | _ToUserCode PROC\r |
150 | mov edx, ss\r | |
151 | mov ss, ecx ; set new segment selectors\r | |
152 | mov ds, ecx\r | |
153 | mov es, ecx\r | |
154 | mov fs, ecx\r | |
155 | mov gs, ecx\r | |
156 | mov cr0, eax\r | |
157 | mov cr4, ebp ; real mode starts at next instruction\r | |
158 | mov ss, esi ; set up 16-bit stack segment\r | |
159 | xchg sp, bx ; set up 16-bit stack pointer\r | |
160 | DB 66h\r | |
161 | call @Base ; push eip\r | |
162 | @Base:\r | |
bbe4ba81 | 163 | pop bp ; ebp <- address of @Base\r |
18c319ae | 164 | DB 67h ; address size override\r |
165 | push [esp + sizeof (IA32_REGS) + 2]\r | |
bbe4ba81 | 166 | lea eax, [esi + (@RealMode - @Base)]\r |
18c319ae | 167 | push eax\r |
168 | retf\r | |
169 | @RealMode:\r | |
bbe4ba81 | 170 | mov cs:[esi + (SavedSs - @Base)], edx\r |
171 | mov cs:[esi + (SavedEsp - @Base)], bx\r | |
97d92bda | 172 | DB 66h\r |
bbe4ba81 | 173 | lidt fword ptr cs:[esi + (_16Idtr - @Base)]\r |
97d92bda | 174 | popaw ; popad actually\r |
175 | pop ds\r | |
176 | pop es\r | |
177 | pop fs\r | |
178 | pop gs\r | |
179 | popf ; popfd\r | |
a9a812a0 | 180 | DB 66h ; Use 32-bit addressing for "retf" below\r |
97d92bda | 181 | retf ; transfer control to user code\r |
182 | _ToUserCode ENDP\r | |
183 | \r | |
184 | _NullSegDesc DQ 0\r | |
185 | _16CsDesc LABEL QWORD\r | |
186 | DW -1\r | |
187 | DW 0\r | |
188 | DB 0\r | |
189 | DB 9bh\r | |
190 | DB 8fh ; 16-bit segment, 4GB limit\r | |
191 | DB 0\r | |
192 | _16DsDesc LABEL QWORD\r | |
193 | DW -1\r | |
194 | DW 0\r | |
195 | DB 0\r | |
196 | DB 93h\r | |
197 | DB 8fh ; 16-bit segment, 4GB limit\r | |
198 | DB 0\r | |
199 | GdtEnd LABEL QWORD\r | |
200 | \r | |
3f566587 | 201 | ;------------------------------------------------------------------------------\r |
202 | ; IA32_REGISTER_SET *\r | |
203 | ; EFIAPI\r | |
204 | ; InternalAsmThunk16 (\r | |
205 | ; IN IA32_REGISTER_SET *RegisterSet,\r | |
206 | ; IN OUT VOID *Transition\r | |
207 | ; );\r | |
208 | ;------------------------------------------------------------------------------\r | |
97d92bda | 209 | InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs\r |
a9a812a0 | 210 | mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter\r |
97d92bda | 211 | movzx edx, (IA32_REGS ptr [esi])._SS\r |
212 | mov edi, (IA32_REGS ptr [esi])._ESP\r | |
213 | add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space\r | |
214 | mov ebx, edi ; ebx <- stack offset\r | |
215 | imul eax, edx, 16 ; eax <- edx * 16\r | |
216 | push sizeof (IA32_REGS) / 4\r | |
217 | add edi, eax ; edi <- linear address of 16-bit stack\r | |
218 | pop ecx\r | |
219 | rep movsd ; copy RegSet\r | |
220 | mov eax, [esp + 40] ; eax <- address of transition code\r | |
221 | mov esi, edx ; esi <- 16-bit stack segment\r | |
bbe4ba81 | 222 | lea edx, [eax + (SavedCr0 - m16Start)]\r |
97d92bda | 223 | mov ecx, eax\r |
224 | and ecx, 0fh\r | |
225 | shl eax, 12\r | |
bbe4ba81 | 226 | lea ecx, [ecx + (_BackFromUserCode - m16Start)]\r |
97d92bda | 227 | mov ax, cx\r |
228 | stosd ; [edi] <- return address of user code\r | |
bbe4ba81 | 229 | sgdt fword ptr [edx + (SavedGdt - SavedCr0)]\r |
97d92bda | 230 | sidt fword ptr [esp + 36] ; save IDT stack in argument space\r |
231 | mov eax, cr0\r | |
232 | mov [edx], eax ; save CR0 in SavedCr0\r | |
233 | and eax, 7ffffffeh ; clear PE, PG bits\r | |
234 | mov ebp, cr4\r | |
bbe4ba81 | 235 | mov [edx + (SavedCr4 - SavedCr0)], ebp\r |
97d92bda | 236 | and ebp, 300h ; clear all but PCE and OSFXSR bits\r |
237 | push 10h\r | |
238 | pop ecx ; ecx <- selector for data segments\r | |
bbe4ba81 | 239 | lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]\r |
a9a812a0 | 240 | pushfd ; Save df/if indeed\r |
bbe4ba81 | 241 | call fword ptr [edx + (_EntryPoint - SavedCr0)]\r |
18c319ae | 242 | popfd\r |
878ddf1f | 243 | lidt fword ptr [esp + 36] ; restore protected mode IDTR\r |
a9a812a0 | 244 | lea eax, [ebp - sizeof (IA32_REGS)] ; eax <- the address of IA32_REGS\r |
878ddf1f | 245 | ret\r |
246 | InternalAsmThunk16 ENDP\r | |
247 | \r | |
248 | END\r |