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