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 #------------------------------------------------------------------------------
22 #include <Library/BaseLib.h>
24 # define the structure of IA32_REGS
37 .equ _EFLAGS, 40 #size 8
41 .equ IA32_REGS_SIZE, 56
45 m16Size: .word _InternalAsmThunk16 - m16Start
46 mThunk16Attr: .word _ThunkAttr - m16Start
47 m16Gdt: .word _NullSeg - m16Start
48 m16GdtrBase: .word _16GdtrBase - m16Start
49 mTransition: .word _EntryPoint - m16Start
57 #------------------------------------------------------------------------------
58 # _BackFromUserCode() takes control in real mode after 'retf' has been executed
59 # by user code. It will be shadowed to somewhere in memory below 1MB.
60 #------------------------------------------------------------------------------
61 .globl ASM_PFX(BackFromUserCode)
62 ASM_PFX(BackFromUserCode):
64 # The order of saved registers on the stack matches the order they appears
65 # in IA32_REGS structure. This facilitates wrapper function to extract them
66 # into that structure.
68 # Some instructions for manipulation of segment registers have to be written
69 # in opcode since 64-bit MASM prevents accesses to those registers.
77 pushq $0 # reserved high order 32 bits of EFlags
78 .byte 0x66, 0x9c # pushfd actually
79 cli # disable interrupts
84 .byte 0x66,0x60 # pushad
85 .byte 0x66,0xba # mov edx, imm32
87 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl
89 movl $0x15cd2401,%eax # mov ax, 2401h & int 15h
90 cli # disable interrupts
93 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl
97 outb %al, $0x92 # deactivate A20M#
100 lea IA32_REGS_SIZE(%esp), %bp
102 # rsi in the following 2 instructions is indeed bp in 16-bit code
104 movw %bp, (_ESP - IA32_REGS_SIZE)(%rsi)
106 movl (_EIP - IA32_REGS_SIZE)(%rsi), %ebx
107 shlw $4,%ax # shl eax, 4
108 addw %ax,%bp # add ebp, eax
111 lea (@64BitCode - @Base)(%ebx, %eax), %ax
112 .byte 0x66,0x2e,0x89,0x87 # mov cs:[bx + (@64Eip - @Base)], eax
114 .byte 0x66,0xb8 # mov eax, imm32
118 # rdi in the instruction below is indeed bx in 16-bit code
120 .byte 0x66,0x2e # 2eh is "cs:" segment override
121 lgdt (SavedGdt - @Base)(%rdi)
123 movl $0xc0000080,%ecx
127 .byte 0x66,0xb8 # mov eax, imm32
130 .byte 0x66,0xea # jmp far cs:@64Bit
137 _EntryPoint: .long ASM_PFX(ToUserCode) - m16Start
139 _16Gdtr: .word GDT_SIZE - 1
140 _16GdtrBase: .quad $_NullSeg
144 #------------------------------------------------------------------------------
145 # _ToUserCode() takes control in real mode before passing control to user code.
146 # It will be shadowed to somewhere in memory below 1MB.
147 #------------------------------------------------------------------------------
148 .globl ASM_PFX(ToUserCode)
150 movl %edx,%ss # set new segment selectors
156 movl $0xc0000080,%ecx
159 andb $0b11111110, %ah
162 movl %esi,%ss # set up 16-bit stack segment
163 movw %bx,%sp # set up 16-bit stack pointer
164 .byte 0x66 # make the following call 32-bit
165 call @Base1 # push eip
167 popw %bp # ebp <- address of @Base1
168 pushq (IA32_REGS_SIZE + 2)(%esp)
171 lret # execution begins at next instruction
173 .byte 0x66,0x2e # CS and operand size override
174 lidt (_16Idtr - @Base1)(%rsi)
175 .byte 0x66,0x61 # popad
178 .byte 0x0f, 0xa1 # pop fs
179 .byte 0x0f, 0xa9 # pop gs
180 .byte 0x66, 0x9d # popfd
181 leaw 4(%esp),%sp # skip high order 32 bits of EFlags
182 .byte 0x66 # make the following retf 32-bit
183 lret # transfer control to user code
185 .equ CODE16, ASM_PFX(16Code) - .
186 .equ DATA16, ASM_PFX(16Data) - .
187 .equ DATA32, ASM_PFX(32Data) - .
195 .byte 0x8f # 16-bit segment, 4GB limit
202 .byte 0x8f # 16-bit segment, 4GB limit
209 .byte 0xcf # 16-bit segment, 4GB limit
212 .equ GDT_SIZE, . - ASM_PFX(NullSeg)
214 #------------------------------------------------------------------------------
215 # IA32_REGISTER_SET *
217 # InternalAsmThunk16 (
218 # IN IA32_REGISTER_SET *RegisterSet,
219 # IN OUT VOID *Transition
221 #------------------------------------------------------------------------------
222 # MISMATCH: "InternalAsmThunk16 PROC USES rbp rbx rsi rdi"
224 .globl ASM_PFX(InternalAsmThunk16)
225 ASM_PFX(InternalAsmThunk16):
231 movl %ds, %r10d # r9 ~ r11 are not accessible in 16-bit
232 movl %es, %r11d # so use them for saving seg registers
234 .byte 0x0f, 0xa0 #push fs
235 .byte 0x0f, 0xa8 #push gs
237 movzwl _SS(%rsi), %r8d
238 movl _ESP(%rsi), %edi
239 lea -(IA32_REGS_SIZE + 4)(%edi), %rdi
241 movl %edi,%ebx # ebx <- stack for 16-bit code
242 pushq $(IA32_REGS_SIZE / 4)
243 addl %eax,%edi # edi <- linear address of 16-bit stack
247 lea (SavedCr4 - m16Start)(%rdx), %ecx
248 movl %edx,%eax # eax <- transition code address
250 shll $12,%eax # segment address in high order 16 bits
251 lea (_BackFromUserCode - m16Start)(%rdx), %ax
252 stosl # [edi] <- return address of user code
253 sgdt (SavedGdt - SavedCr4)(%rcx)
256 movl %eax, (SavedCr0 - SavedCr4)(%rcx)
257 andl $0x7ffffffe,%eax # clear PE, PG bits
259 movl %ebp, (%rcx) # save CR4 in SavedCr4
260 andl $0x300,%ebp # clear all but PCE and OSFXSR bits
261 movl %r8d, %esi # esi <- 16-bit stack segment
264 lgdt (_16Gdtr - SavedCr4)(%rcx)
268 lea @RetFromRealMode, %r8
271 movw %r8w, (SavedCs - SavedCr4)(%rcx)
273 .byte 0xff, 0x69 # jmp (_EntryPoint - SavedCr4)(%rcx)
274 .byte _EntryPoint - SavedCr4
278 lea -IA32_REGS_SIZE(%rbp), %eax
279 .byte 0x0f, 0xa9 # pop gs
280 .byte 0x0f, 0xa1 # pop fs