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