]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/X64/Thunk16.S
EdkCompatabilityPkg: Fix build issues with X64 clang
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / BaseLib / X64 / Thunk16.S
1 #------------------------------------------------------------------------------
2 #
3 # Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
4 # 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 # Thunk16.S
15 #
16 # Abstract:
17 #
18 # Real mode thunk
19 #
20 #------------------------------------------------------------------------------
21
22 #include <EdkIIGlueBase.h>
23
24 .globl ASM_PFX(m16Start)
25 .globl ASM_PFX(m16Size)
26 .globl ASM_PFX(mThunk16Attr)
27 .globl ASM_PFX(m16Gdt)
28 .globl ASM_PFX(m16GdtrBase)
29 .globl ASM_PFX(mTransition)
30 .globl ASM_PFX(InternalAsmThunk16)
31
32 # define the structure of IA32_REGS
33 .set _EDI, 0 #size 4
34 .set _ESI, 4 #size 4
35 .set _EBP, 8 #size 4
36 .set _ESP, 12 #size 4
37 .set _EBX, 16 #size 4
38 .set _EDX, 20 #size 4
39 .set _ECX, 24 #size 4
40 .set _EAX, 28 #size 4
41 .set _DS, 32 #size 2
42 .set _ES, 34 #size 2
43 .set _FS, 36 #size 2
44 .set _GS, 38 #size 2
45 .set _EFLAGS, 40 #size 8
46 .set _EIP, 48 #size 4
47 .set _CS, 52 #size 2
48 .set _SS, 54 #size 2
49 .set IA32_REGS_SIZE, 56
50
51 .data
52
53 #ifndef __APPLE__
54 ASM_PFX(m16Size): .word ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start)
55 ASM_PFX(mThunk16Attr): .word _ThunkAttr - ASM_PFX(m16Start)
56 ASM_PFX(m16Gdt): .word ASM_PFX(NullSeg) - ASM_PFX(m16Start)
57 ASM_PFX(m16GdtrBase): .word _16GdtrBase - ASM_PFX(m16Start)
58 ASM_PFX(mTransition): .word _EntryPoint - ASM_PFX(m16Start)
59 #endif
60
61 .text
62
63 ASM_PFX(m16Start):
64
65 SavedGdt: .space 10
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 .globl ASM_PFX(BackFromUserCode)
72 ASM_PFX(BackFromUserCode):
73 #ifdef __APPLE__
74 int $3
75 #else
76 #
77 # The order of saved registers on the stack matches the order they appears
78 # in IA32_REGS structure. This facilitates wrapper function to extract them
79 # into that structure.
80 #
81 # Some instructions for manipulation of segment registers have to be written
82 # in opcode since 64-bit MASM prevents accesses to those registers.
83 #
84 .byte 0x16 # push ss
85 .byte 0xe # push cs
86 .byte 0x66
87 call L_Base # push eip
88 L_Base:
89 .byte 0x66
90 pushq $0 # reserved high order 32 bits of EFlags
91 .byte 0x66, 0x9c # pushfd actually
92 cli # disable interrupts
93 push %gs
94 push %fs
95 .byte 6 # push es
96 .byte 0x1e # push ds
97 .byte 0x66,0x60 # pushad
98 .byte 0x66,0xba # mov edx, imm32
99 _ThunkAttr: .space 4
100 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl
101 jz L_1
102 movl $0x15cd2401,%eax # mov ax, 2401h & int 15h
103 cli # disable interrupts
104 jnc L_2
105 L_1:
106 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl
107 jz L_2
108 inb $0x92,%al
109 orb $2,%al
110 outb %al, $0x92 # deactivate A20M#
111 L_2:
112 movw %ss,%ax
113 lea IA32_REGS_SIZE(%esp), %bp
114 #
115 # rsi in the following 2 instructions is indeed bp in 16-bit code
116 #
117 movw %bp, (_ESP - IA32_REGS_SIZE)(%rsi)
118 .byte 0x66
119 movl (_EIP - IA32_REGS_SIZE)(%rsi), %ebx
120 shlw $4,%ax # shl eax, 4
121 addw %ax,%bp # add ebp, eax
122 movw %cs,%ax
123 shlw $4,%ax
124 lea (L_64BitCode - L_Base)(%ebx, %eax), %ax
125 .byte 0x66,0x2e,0x89,0x87 # mov cs:[bx + (L_64Eip - L_Base)], eax
126 .word L_64Eip - L_Base
127 .byte 0x66,0xb8 # mov eax, imm32
128 SavedCr4: .space 4
129 movq %rax, %cr4
130 #
131 # rdi in the instruction below is indeed bx in 16-bit code
132 #
133 .byte 0x66,0x2e # 2eh is "cs:" segment override
134 lgdt (SavedGdt - L_Base)(%rdi)
135 .byte 0x66
136 movl $0xc0000080,%ecx
137 rdmsr
138 orb $1,%ah
139 wrmsr
140 .byte 0x66,0xb8 # mov eax, imm32
141 SavedCr0: .space 4
142 movq %rax, %cr0
143 .byte 0x66,0xea # jmp far cs:L_64Bit
144 L_64Eip: .space 4
145 SavedCs: .space 2
146 L_64BitCode:
147 .byte 0x90
148 .byte 0x67,0xbc # mov esp, imm32
149 SavedSp: .space 4 # restore stack
150 nop
151 #endif
152 ret
153
154 #ifndef __APPLE__
155 _EntryPoint: .long ASM_PFX(ToUserCode) - ASM_PFX(m16Start)
156 .word CODE16
157 _16Gdtr: .word GDT_SIZE - 1
158 _16GdtrBase: .quad ASM_PFX(NullSeg)
159 _16Idtr: .word 0x3ff
160 .long 0
161 #endif
162
163 #------------------------------------------------------------------------------
164 # _ToUserCode() takes control in real mode before passing control to user code.
165 # It will be shadowed to somewhere in memory below 1MB.
166 #------------------------------------------------------------------------------
167 .globl ASM_PFX(ToUserCode)
168 ASM_PFX(ToUserCode):
169 #ifdef __APPLE__
170 int $3
171 #else
172 movl %edx,%ss # set new segment selectors
173 movl %edx,%ds
174 movl %edx,%es
175 movl %edx,%fs
176 movl %edx,%gs
177 .byte 0x66
178 movl $0xc0000080,%ecx
179 movq %rax, %cr0
180 rdmsr
181 andb $0b11111110, %ah
182 wrmsr
183 movq %rbp, %cr4
184 movl %esi,%ss # set up 16-bit stack segment
185 movw %bx,%sp # set up 16-bit stack pointer
186 .byte 0x66 # make the following call 32-bit
187 call L_Base1 # push eip
188 L_Base1:
189 popw %bp # ebp <- address of L_Base1
190 pushq (IA32_REGS_SIZE + 2)(%esp)
191 lea 0x0c(%rsi), %eax
192 pushq %rax
193 lret # execution begins at next instruction
194 L_RealMode:
195 .byte 0x66,0x2e # CS and operand size override
196 lidt (_16Idtr - L_Base1)(%rsi)
197 .byte 0x66,0x61 # popad
198 .byte 0x1f # pop ds
199 .byte 0x7 # pop es
200 .byte 0x0f, 0xa1 # pop fs
201 .byte 0x0f, 0xa9 # pop gs
202 .byte 0x66, 0x9d # popfd
203 leaw 4(%esp),%sp # skip high order 32 bits of EFlags
204 .byte 0x66 # make the following retf 32-bit
205 #endif
206 lret # transfer control to user code
207
208 .set CODE16, ASM_PFX(_16Code) - .
209 .set DATA16, ASM_PFX(_16Data) - .
210 .set DATA32, ASM_PFX(_32Data) - .
211
212 ASM_PFX(NullSeg): .quad 0
213 ASM_PFX(_16Code):
214 .word -1
215 .word 0
216 .byte 0
217 .byte 0x9b
218 .byte 0x8f # 16-bit segment, 4GB limit
219 .byte 0
220 ASM_PFX(_16Data):
221 .word -1
222 .word 0
223 .byte 0
224 .byte 0x93
225 .byte 0x8f # 16-bit segment, 4GB limit
226 .byte 0
227 ASM_PFX(_32Data):
228 .word -1
229 .word 0
230 .byte 0
231 .byte 0x93
232 .byte 0xcf # 16-bit segment, 4GB limit
233 .byte 0
234
235 .set GDT_SIZE, . - ASM_PFX(NullSeg)
236
237 #------------------------------------------------------------------------------
238 # IA32_REGISTER_SET *
239 # EFIAPI
240 # InternalAsmThunk16 (
241 # IN IA32_REGISTER_SET *RegisterSet,
242 # IN OUT VOID *Transition
243 # );
244 #------------------------------------------------------------------------------
245
246 .globl ASM_PFX(InternalAsmThunk16)
247 ASM_PFX(InternalAsmThunk16):
248 #ifdef __APPLE__
249 int $3
250 #else
251 pushq %rbp
252 pushq %rbx
253 pushq %rsi
254 pushq %rdi
255
256 movq %ds, %rbx
257 pushq %rbx # Save ds segment register on the stack
258 movq %es, %rbx
259 pushq %rbx # Save es segment register on the stack
260 movq %ss, %rbx
261 pushq %rbx # Save ss segment register on the stack
262
263 .byte 0x0f, 0xa0 #push fs
264 .byte 0x0f, 0xa8 #push gs
265 movq %rcx, %rsi
266 movzwl _SS(%rsi), %r8d
267 movl _ESP(%rsi), %edi
268 lea -(IA32_REGS_SIZE + 4)(%edi), %rdi
269 imul $16, %r8d, %eax
270 movl %edi,%ebx # ebx <- stack for 16-bit code
271 pushq $(IA32_REGS_SIZE / 4)
272 addl %eax,%edi # edi <- linear address of 16-bit stack
273 popq %rcx
274 rep
275 movsl # copy RegSet
276 lea (SavedCr4 - ASM_PFX(m16Start))(%rdx), %ecx
277 movl %edx,%eax # eax <- transition code address
278 andl $0xf,%edx
279 shll $12,%eax # segment address in high order 16 bits
280 lea (_BackFromUserCode - ASM_PFX(m16Start))(%rdx), %ax
281 stosl # [edi] <- return address of user code
282 sgdt (SavedGdt - SavedCr4)(%rcx)
283 sidt 0x50(%rsp)
284 movq %cr0, %rax
285 movl %eax, (SavedCr0 - SavedCr4)(%rcx)
286 andl $0x7ffffffe,%eax # clear PE, PG bits
287 movq %cr4, %rbp
288 movl %ebp, (%rcx) # save CR4 in SavedCr4
289 andl $0x300,%ebp # clear all but PCE and OSFXSR bits
290 movl %r8d, %esi # esi <- 16-bit stack segment
291 .byte 0x6a, DATA32
292 popq %rdx
293 lgdt (_16Gdtr - SavedCr4)(%rcx)
294 movl %edx,%ss
295 pushfq
296 lea -8(%rdx), %edx
297 lea L_RetFromRealMode, %r8
298 pushq %r8
299 movl %cs, %r8d
300 movw %r8w, (SavedCs - SavedCr4)(%rcx)
301 movl %esp, (SavedSp - SavedCr4)(%rcx)
302 .byte 0xff, 0x69 # jmp (_EntryPoint - SavedCr4)(%rcx)
303 .byte _EntryPoint - SavedCr4
304 L_RetFromRealMode:
305 popfq
306 lidt 0x50(%rsp)
307 lea -IA32_REGS_SIZE(%rbp), %eax
308 .byte 0x0f, 0xa9 # pop gs
309 .byte 0x0f, 0xa1 # pop fs
310
311 popq %rbx
312 movq %rbx, %ss
313 popq %rbx
314 movq %rbx, %es
315 popq %rbx
316 movq %rbx, %ds
317
318 popq %rdi
319 popq %rsi
320 popq %rbx
321 popq %rbp
322 #endif
323 ret