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