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