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