]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/X64/Thunk16.asm
Update copyright for files modified in this year
[mirror_edk2.git] / MdePkg / Library / BaseLib / X64 / Thunk16.asm
1
2 #include "BaseLibInternals.h"
3
4 ;------------------------------------------------------------------------------
5 ;
6 ; Copyright (c) 2006 - 2008, Intel Corporation
7 ; All rights reserved. This program and the accompanying materials
8 ; are licensed and made available under the terms and conditions of the BSD License
9 ; which accompanies this distribution. The full text of the license may be found at
10 ; http://opensource.org/licenses/bsd-license.php
11 ;
12 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 ;
15 ; Module Name:
16 ;
17 ; Thunk.asm
18 ;
19 ; Abstract:
20 ;
21 ; Real mode thunk
22 ;
23 ;------------------------------------------------------------------------------
24
25 EXTERNDEF m16Start:BYTE
26 EXTERNDEF m16Size:WORD
27 EXTERNDEF mThunk16Attr:WORD
28 EXTERNDEF m16Gdt:WORD
29 EXTERNDEF m16GdtrBase:WORD
30 EXTERNDEF 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 DQ ?
46 _EIP DD ?
47 _CS DW ?
48 _SS DW ?
49 IA32_REGS ENDS
50
51 .const
52
53 m16Size DW InternalAsmThunk16 - m16Start
54 mThunk16Attr DW _ThunkAttr - m16Start
55 m16Gdt DW _NullSeg - m16Start
56 m16GdtrBase DW _16GdtrBase - m16Start
57 mTransition DW _EntryPoint - m16Start
58
59 .code
60
61 m16Start LABEL BYTE
62
63 SavedGdt LABEL FWORD
64 DW ?
65 DQ ?
66
67 ;------------------------------------------------------------------------------
68 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
69 ; by user code. It will be shadowed to somewhere in memory below 1MB.
70 ;------------------------------------------------------------------------------
71 _BackFromUserCode PROC
72 ;
73 ; The order of saved registers on the stack matches the order they appears
74 ; in IA32_REGS structure. This facilitates wrapper function to extract them
75 ; into that structure.
76 ;
77 ; Some instructions for manipulation of segment registers have to be written
78 ; in opcode since 64-bit MASM prevents accesses to those registers.
79 ;
80 DB 16h ; push ss
81 DB 0eh ; push cs
82 DB 66h
83 call @Base ; push eip
84 @Base:
85 DB 66h
86 push 0 ; reserved high order 32 bits of EFlags
87 pushf ; pushfd actually
88 cli ; disable interrupts
89 push gs
90 push fs
91 DB 6 ; push es
92 DB 1eh ; push ds
93 DB 66h, 60h ; pushad
94 DB 66h, 0bah ; mov edx, imm32
95 _ThunkAttr DD ?
96 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
97 jz @1
98 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
99 cli ; disable interrupts
100 jnc @2
101 @1:
102 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
103 jz @2
104 in al, 92h
105 or al, 2
106 out 92h, al ; deactivate A20M#
107 @2:
108 mov eax, ss
109 lea bp, [esp + sizeof (IA32_REGS)]
110 ;
111 ; rsi in the following 2 instructions is indeed bp in 16-bit code
112 ;
113 mov word ptr (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._ESP, bp
114 DB 66h
115 mov ebx, (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._EIP
116 shl ax, 4 ; shl eax, 4
117 add bp, ax ; add ebp, eax
118 mov ax, cs
119 shl ax, 4
120 lea ax, [eax + ebx + (@64BitCode - @Base)]
121 DB 66h, 2eh, 89h, 87h ; mov cs:[bx + (@64Eip - @Base)], eax
122 DW @64Eip - @Base
123 DB 66h, 0b8h ; mov eax, imm32
124 SavedCr4 DD ?
125 mov cr4, rax
126 ;
127 ; rdi in the instruction below is indeed bx in 16-bit code
128 ;
129 DB 66h, 2eh ; 2eh is "cs:" segment override
130 lgdt fword ptr [rdi + (SavedGdt - @Base)]
131 DB 66h
132 mov ecx, 0c0000080h
133 rdmsr
134 or ah, 1
135 wrmsr
136 DB 66h, 0b8h ; mov eax, imm32
137 SavedCr0 DD ?
138 mov cr0, rax
139 DB 66h, 0eah ; jmp far cs:@64Bit
140 @64Eip DD ?
141 SavedCs DW ?
142 @64BitCode:
143 mov rsp, r8 ; restore stack
144 ret
145 _BackFromUserCode ENDP
146
147 _EntryPoint DD _ToUserCode - m16Start
148 DW CODE16
149 _16Gdtr LABEL FWORD
150 DW GDT_SIZE - 1
151 _16GdtrBase DQ _NullSeg
152 _16Idtr FWORD (1 SHL 10) - 1
153
154 ;------------------------------------------------------------------------------
155 ; _ToUserCode() takes control in real mode before passing control to user code.
156 ; It will be shadowed to somewhere in memory below 1MB.
157 ;------------------------------------------------------------------------------
158 _ToUserCode PROC
159 mov ss, edx ; set new segment selectors
160 mov ds, edx
161 mov es, edx
162 mov fs, edx
163 mov gs, edx
164 DB 66h
165 mov ecx, 0c0000080h
166 mov cr0, rax ; real mode starts at next instruction
167 rdmsr
168 and ah, NOT 1
169 wrmsr
170 mov cr4, rbp
171 mov ss, esi ; set up 16-bit stack segment
172 mov sp, bx ; set up 16-bit stack pointer
173 DB 66h ; make the following call 32-bit
174 call @Base ; push eip
175 @Base:
176 pop bp ; ebp <- address of @Base
177 push [esp + sizeof (IA32_REGS) + 2]
178 lea eax, [rsi + (@RealMode - @Base)] ; rsi is "bp" in 16-bit code
179 push rax
180 retf ; execution begins at next instruction
181 @RealMode:
182 DB 66h, 2eh ; CS and operand size override
183 lidt fword ptr [rsi + (_16Idtr - @Base)]
184 DB 66h, 61h ; popad
185 DB 1fh ; pop ds
186 DB 07h ; pop es
187 pop fs
188 pop gs
189 popf ; popfd
190 lea sp, [esp + 4] ; skip high order 32 bits of EFlags
191 DB 66h ; make the following retf 32-bit
192 retf ; transfer control to user code
193 _ToUserCode ENDP
194
195 CODE16 = _16Code - $
196 DATA16 = _16Data - $
197 DATA32 = _32Data - $
198
199 _NullSeg DQ 0
200 _16Code LABEL QWORD
201 DW -1
202 DW 0
203 DB 0
204 DB 9bh
205 DB 8fh ; 16-bit segment, 4GB limit
206 DB 0
207 _16Data LABEL QWORD
208 DW -1
209 DW 0
210 DB 0
211 DB 93h
212 DB 8fh ; 16-bit segment, 4GB limit
213 DB 0
214 _32Data LABEL QWORD
215 DW -1
216 DW 0
217 DB 0
218 DB 93h
219 DB 0cfh ; 16-bit segment, 4GB limit
220 DB 0
221
222 GDT_SIZE = $ - _NullSeg
223
224 ;------------------------------------------------------------------------------
225 ; IA32_REGISTER_SET *
226 ; EFIAPI
227 ; InternalAsmThunk16 (
228 ; IN IA32_REGISTER_SET *RegisterSet,
229 ; IN OUT VOID *Transition
230 ; );
231 ;------------------------------------------------------------------------------
232 InternalAsmThunk16 PROC USES rbp rbx rsi rdi
233 mov r10d, ds ; r9 ~ r11 are not accessible in 16-bit
234 mov r11d, es ; so use them for saving seg registers
235 mov r9d, ss
236 push fs
237 push gs
238 mov rsi, rcx
239 movzx r8d, (IA32_REGS ptr [rsi])._SS
240 mov edi, (IA32_REGS ptr [rsi])._ESP
241 lea rdi, [edi - (sizeof (IA32_REGS) + 4)]
242 imul eax, r8d, 16 ; eax <- r8d(stack segment) * 16
243 mov ebx, edi ; ebx <- stack for 16-bit code
244 push sizeof (IA32_REGS) / 4
245 add edi, eax ; edi <- linear address of 16-bit stack
246 pop rcx
247 rep movsd ; copy RegSet
248 lea ecx, [rdx + (SavedCr4 - m16Start)]
249 mov eax, edx ; eax <- transition code address
250 and edx, 0fh
251 shl eax, 12 ; segment address in high order 16 bits
252 lea ax, [rdx + (_BackFromUserCode - m16Start)] ; offset address
253 stosd ; [edi] <- return address of user code
254 sgdt fword ptr [rcx + (SavedGdt - SavedCr4)]
255 sidt fword ptr [rsp + 38h] ; save IDT stack in argument space
256 mov rax, cr0
257 mov [rcx + (SavedCr0 - SavedCr4)], eax
258 and eax, 7ffffffeh ; clear PE, PG bits
259 mov rbp, cr4
260 mov [rcx], ebp ; save CR4 in SavedCr4
261 and ebp, 300h ; clear all but PCE and OSFXSR bits
262 mov esi, r8d ; esi <- 16-bit stack segment
263 DB 6ah, DATA32 ; push DATA32
264 pop rdx ; rdx <- 32-bit data segment selector
265 lgdt fword ptr [rcx + (_16Gdtr - SavedCr4)]
266 mov ss, edx
267 pushfq
268 lea edx, [rdx + DATA16 - DATA32]
269 lea r8, @RetFromRealMode
270 push r8
271 mov r8d, cs
272 mov [rcx + (SavedCs - SavedCr4)], r8w
273 mov r8, rsp
274 jmp fword ptr [rcx + (_EntryPoint - SavedCr4)]
275 @RetFromRealMode:
276 popfq
277 lidt fword ptr [rsp + 38h] ; restore protected mode IDTR
278 lea eax, [rbp - sizeof (IA32_REGS)]
279 pop gs
280 pop fs
281 mov ss, r9d
282 mov es, r11d
283 mov ds, r10d
284 ret
285 InternalAsmThunk16 ENDP
286
287 END