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 .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)
32 # define the structure of IA32_REGS
45 .equ _EFLAGS, 40 #size 8
49 .equ IA32_REGS_SIZE, 56
53 ASM_PFX(m16Size): .word ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start)
54 ASM_PFX(mThunk16Attr): .word _ThunkAttr - ASM_PFX(m16Start)
55 ASM_PFX(m16Gdt): .word _NullSeg - ASM_PFX(m16Start)
56 ASM_PFX(m16GdtrBase): .word _16GdtrBase - ASM_PFX(m16Start)
57 ASM_PFX(mTransition): .word _EntryPoint - ASM_PFX(m16Start)
65 #------------------------------------------------------------------------------
66 # _BackFromUserCode() takes control in real mode after 'retf' has been executed
67 # by user code. It will be shadowed to somewhere in memory below 1MB.
68 #------------------------------------------------------------------------------
69 .globl ASM_PFX(BackFromUserCode)
70 ASM_PFX(BackFromUserCode):
72 # The order of saved registers on the stack matches the order they appears
73 # in IA32_REGS structure. This facilitates wrapper function to extract them
74 # into that structure.
76 # Some instructions for manipulation of segment registers have to be written
77 # in opcode since 64-bit MASM prevents accesses to those registers.
82 call L_Base # push eip
85 pushq $0 # reserved high order 32 bits of EFlags
86 .byte 0x66, 0x9c # pushfd actually
87 cli # disable interrupts
92 .byte 0x66,0x60 # pushad
93 .byte 0x66,0xba # mov edx, imm32
95 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl
97 movl $0x15cd2401,%eax # mov ax, 2401h & int 15h
98 cli # disable interrupts
101 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl
105 outb %al, $0x92 # deactivate A20M#
108 lea IA32_REGS_SIZE(%esp), %bp
110 # rsi in the following 2 instructions is indeed bp in 16-bit code
112 movw %bp, (_ESP - IA32_REGS_SIZE)(%rsi)
114 movl (_EIP - IA32_REGS_SIZE)(%rsi), %ebx
115 shlw $4,%ax # shl eax, 4
116 addw %ax,%bp # add ebp, eax
119 lea (L_64BitCode - L_Base)(%ebx, %eax), %ax
120 .byte 0x66,0x2e,0x89,0x87 # mov cs:[bx + (L_64Eip - L_Base)], eax
121 .word L_64Eip - L_Base
122 .byte 0x66,0xb8 # mov eax, imm32
126 # rdi in the instruction below is indeed bx in 16-bit code
128 .byte 0x66,0x2e # 2eh is "cs:" segment override
129 lgdt (SavedGdt - L_Base)(%rdi)
131 movl $0xc0000080,%ecx
135 .byte 0x66,0xb8 # mov eax, imm32
138 .byte 0x66,0xea # jmp far cs:L_64Bit
145 _EntryPoint: .long ASM_PFX(ToUserCode) - ASM_PFX(m16Start)
147 _16Gdtr: .word GDT_SIZE - 1
148 _16GdtrBase: .quad _NullSeg
152 #------------------------------------------------------------------------------
153 # _ToUserCode() takes control in real mode before passing control to user code.
154 # It will be shadowed to somewhere in memory below 1MB.
155 #------------------------------------------------------------------------------
156 .globl ASM_PFX(ToUserCode)
158 movl %edx,%ss # set new segment selectors
164 movl $0xc0000080,%ecx
167 andb $0b11111110, %ah
170 movl %esi,%ss # set up 16-bit stack segment
171 movw %bx,%sp # set up 16-bit stack pointer
172 .byte 0x66 # make the following call 32-bit
173 call L_Base1 # push eip
175 popw %bp # ebp <- address of L_Base1
176 pushq (IA32_REGS_SIZE + 2)(%esp)
179 lret # execution begins at next instruction
181 .byte 0x66,0x2e # CS and operand size override
182 lidt (_16Idtr - L_Base1)(%rsi)
183 .byte 0x66,0x61 # popad
186 .byte 0x0f, 0xa1 # pop fs
187 .byte 0x0f, 0xa9 # pop gs
188 .byte 0x66, 0x9d # popfd
189 leaw 4(%esp),%sp # skip high order 32 bits of EFlags
190 .byte 0x66 # make the following retf 32-bit
191 lret # transfer control to user code
193 .equ CODE16, ASM_PFX(16Code) - .
194 .equ DATA16, ASM_PFX(16Data) - .
195 .equ DATA32, ASM_PFX(32Data) - .
203 .byte 0x8f # 16-bit segment, 4GB limit
210 .byte 0x8f # 16-bit segment, 4GB limit
217 .byte 0xcf # 16-bit segment, 4GB limit
220 .equ GDT_SIZE, . - ASM_PFX(NullSeg)
222 #------------------------------------------------------------------------------
223 # IA32_REGISTER_SET *
225 # InternalAsmThunk16 (
226 # IN IA32_REGISTER_SET *RegisterSet,
227 # IN OUT VOID *Transition
229 #------------------------------------------------------------------------------
230 # MISMATCH: "InternalAsmThunk16 PROC USES rbp rbx rsi rdi"
232 .globl ASM_PFX(InternalAsmThunk16)
233 ASM_PFX(InternalAsmThunk16):
239 movl %ds, %r10d # r9 ~ r11 are not accessible in 16-bit
240 movl %es, %r11d # so use them for saving seg registers
242 .byte 0x0f, 0xa0 #push fs
243 .byte 0x0f, 0xa8 #push gs
245 movzwl _SS(%rsi), %r8d
246 movl _ESP(%rsi), %edi
247 lea -(IA32_REGS_SIZE + 4)(%edi), %rdi
249 movl %edi,%ebx # ebx <- stack for 16-bit code
250 pushq $(IA32_REGS_SIZE / 4)
251 addl %eax,%edi # edi <- linear address of 16-bit stack
255 lea (SavedCr4 - ASM_PFX(m16Start))(%rdx), %ecx
256 movl %edx,%eax # eax <- transition code address
258 shll $12,%eax # segment address in high order 16 bits
259 lea (_BackFromUserCode - ASM_PFX(m16Start))(%rdx), %ax
260 stosl # [edi] <- return address of user code
261 sgdt (SavedGdt - SavedCr4)(%rcx)
264 movl %eax, (SavedCr0 - SavedCr4)(%rcx)
265 andl $0x7ffffffe,%eax # clear PE, PG bits
267 movl %ebp, (%rcx) # save CR4 in SavedCr4
268 andl $0x300,%ebp # clear all but PCE and OSFXSR bits
269 movl %r8d, %esi # esi <- 16-bit stack segment
272 lgdt (_16Gdtr - SavedCr4)(%rcx)
276 lea L_RetFromRealMode, %r8
279 movw %r8w, (SavedCs - SavedCr4)(%rcx)
281 .byte 0xff, 0x69 # jmp (_EntryPoint - SavedCr4)(%rcx)
282 .byte _EntryPoint - SavedCr4
286 lea -IA32_REGS_SIZE(%rbp), %eax
287 .byte 0x0f, 0xa9 # pop gs
288 .byte 0x0f, 0xa1 # pop fs