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