1 #------------------------------------------------------------------------------
3 # Copyright (c) 2006 - 2008, 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
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.
20 #------------------------------------------------------------------------------
21 #include <EdkIIGlueBase.h>
23 .globl ASM_PFX(m16Start)
24 .globl ASM_PFX(m16Size)
25 .globl ASM_PFX(mThunk16Attr)
26 .globl ASM_PFX(m16Gdt)
27 .globl ASM_PFX(m16GdtrBase)
28 .globl ASM_PFX(mTransition)
29 .globl ASM_PFX(InternalAsmThunk16)
31 # define the structure of IA32_REGS
44 .equ _EFLAGS, 40 #size 8
48 .equ IA32_REGS_SIZE, 56
52 ASM_PFX(m16Size): .word ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start)
53 ASM_PFX(mThunk16Attr): .word _ThunkAttr - ASM_PFX(m16Start)
54 ASM_PFX(m16Gdt): .word _NullSeg - ASM_PFX(m16Start)
55 ASM_PFX(m16GdtrBase): .word _16GdtrBase - ASM_PFX(m16Start)
56 ASM_PFX(mTransition): .word _EntryPoint - ASM_PFX(m16Start)
64 #------------------------------------------------------------------------------
65 # _BackFromUserCode() takes control in real mode after 'retf' has been executed
66 # by user code. It will be shadowed to somewhere in memory below 1MB.
67 #------------------------------------------------------------------------------
68 .globl ASM_PFX(BackFromUserCode)
69 ASM_PFX(BackFromUserCode):
71 # The order of saved registers on the stack matches the order they appears
72 # in IA32_REGS structure. This facilitates wrapper function to extract them
73 # into that structure.
75 # Some instructions for manipulation of segment registers have to be written
76 # in opcode since 64-bit MASM prevents accesses to those registers.
84 pushq $0 # reserved high order 32 bits of EFlags
85 .byte 0x66, 0x9c # pushfd actually
86 cli # disable interrupts
91 .byte 0x66,0x60 # pushad
92 .byte 0x66,0xba # mov edx, imm32
94 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl
96 movl $0x15cd2401,%eax # mov ax, 2401h & int 15h
97 cli # disable interrupts
100 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl
104 outb %al, $0x92 # deactivate A20M#
107 lea IA32_REGS_SIZE(%esp), %bp
109 # rsi in the following 2 instructions is indeed bp in 16-bit code
111 movw %bp, (_ESP - IA32_REGS_SIZE)(%rsi)
113 movl (_EIP - IA32_REGS_SIZE)(%rsi), %ebx
114 shlw $4,%ax # shl eax, 4
115 addw %ax,%bp # add ebp, eax
118 lea (@64BitCode - @Base)(%ebx, %eax), %ax
119 .byte 0x66,0x2e,0x89,0x87 # mov cs:[bx + (@64Eip - @Base)], eax
121 .byte 0x66,0xb8 # mov eax, imm32
125 # rdi in the instruction below is indeed bx in 16-bit code
127 .byte 0x66,0x2e # 2eh is "cs:" segment override
128 lgdt (SavedGdt - @Base)(%rdi)
130 movl $0xc0000080,%ecx
134 .byte 0x66,0xb8 # mov eax, imm32
137 .byte 0x66,0xea # jmp far cs:@64Bit
144 _EntryPoint: .long ASM_PFX(ToUserCode) - ASM_PFX(m16Start)
146 _16Gdtr: .word GDT_SIZE - 1
147 _16GdtrBase: .quad _NullSeg
151 #------------------------------------------------------------------------------
152 # _ToUserCode() takes control in real mode before passing control to user code.
153 # It will be shadowed to somewhere in memory below 1MB.
154 #------------------------------------------------------------------------------
155 .globl ASM_PFX(ToUserCode)
157 movl %edx,%ss # set new segment selectors
163 movl $0xc0000080,%ecx
166 andb $0b11111110, %ah
169 movl %esi,%ss # set up 16-bit stack segment
170 movw %bx,%sp # set up 16-bit stack pointer
171 .byte 0x66 # make the following call 32-bit
172 call @Base1 # push eip
174 popw %bp # ebp <- address of @Base1
175 pushq (IA32_REGS_SIZE + 2)(%esp)
178 lret # execution begins at next instruction
180 .byte 0x66,0x2e # CS and operand size override
181 lidt (_16Idtr - @Base1)(%rsi)
182 .byte 0x66,0x61 # popad
185 .byte 0x0f, 0xa1 # pop fs
186 .byte 0x0f, 0xa9 # pop gs
187 .byte 0x66, 0x9d # popfd
188 leaw 4(%esp),%sp # skip high order 32 bits of EFlags
189 .byte 0x66 # make the following retf 32-bit
190 lret # transfer control to user code
192 .equ CODE16, ASM_PFX(16Code) - .
193 .equ DATA16, ASM_PFX(16Data) - .
194 .equ DATA32, ASM_PFX(32Data) - .
202 .byte 0x8f # 16-bit segment, 4GB limit
209 .byte 0x8f # 16-bit segment, 4GB limit
216 .byte 0xcf # 16-bit segment, 4GB limit
219 .equ GDT_SIZE, . - ASM_PFX(NullSeg)
221 #------------------------------------------------------------------------------
222 # IA32_REGISTER_SET *
224 # InternalAsmThunk16 (
225 # IN IA32_REGISTER_SET *RegisterSet,
226 # IN OUT VOID *Transition
228 #------------------------------------------------------------------------------
229 # MISMATCH: "InternalAsmThunk16 PROC USES rbp rbx rsi rdi"
231 .globl ASM_PFX(InternalAsmThunk16)
232 ASM_PFX(InternalAsmThunk16):
238 movl %ds, %r10d # r9 ~ r11 are not accessible in 16-bit
239 movl %es, %r11d # so use them for saving seg registers
241 .byte 0x0f, 0xa0 #push fs
242 .byte 0x0f, 0xa8 #push gs
244 movzwl _SS(%rsi), %r8d
245 movl _ESP(%rsi), %edi
246 lea -(IA32_REGS_SIZE + 4)(%edi), %rdi
248 movl %edi,%ebx # ebx <- stack for 16-bit code
249 pushq $(IA32_REGS_SIZE / 4)
250 addl %eax,%edi # edi <- linear address of 16-bit stack
254 lea (SavedCr4 - ASM_PFX(m16Start))(%rdx), %ecx
255 movl %edx,%eax # eax <- transition code address
257 shll $12,%eax # segment address in high order 16 bits
258 lea (_BackFromUserCode - ASM_PFX(m16Start))(%rdx), %ax
259 stosl # [edi] <- return address of user code
260 sgdt (SavedGdt - SavedCr4)(%rcx)
263 movl %eax, (SavedCr0 - SavedCr4)(%rcx)
264 andl $0x7ffffffe,%eax # clear PE, PG bits
266 movl %ebp, (%rcx) # save CR4 in SavedCr4
267 andl $0x300,%ebp # clear all but PCE and OSFXSR bits
268 movl %r8d, %esi # esi <- 16-bit stack segment
271 lgdt (_16Gdtr - SavedCr4)(%rcx)
275 lea @RetFromRealMode, %r8
278 movw %r8w, (SavedCs - SavedCr4)(%rcx)
280 .byte 0xff, 0x69 # jmp (_EntryPoint - SavedCr4)(%rcx)
281 .byte _EntryPoint - SavedCr4
285 lea -IA32_REGS_SIZE(%rbp), %eax
286 .byte 0x0f, 0xa9 # pop gs
287 .byte 0x0f, 0xa1 # pop fs