2008-11-03 10:25:10 +01:00
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
#
|
2013-01-07 08:12:19 +01:00
|
|
|
# Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
|
2010-04-23 18:00:47 +02:00
|
|
|
# This program and the accompanying materials
|
2008-11-03 10:25:10 +01:00
|
|
|
# are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
# which accompanies this distribution. The full text of the license may be found at
|
2010-06-25 23:56:02 +02:00
|
|
|
# http://opensource.org/licenses/bsd-license.php.
|
2008-11-03 10:25:10 +01:00
|
|
|
#
|
|
|
|
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
#
|
|
|
|
# Module Name:
|
|
|
|
#
|
|
|
|
# Thunk16.S
|
|
|
|
#
|
|
|
|
# Abstract:
|
|
|
|
#
|
|
|
|
# Real mode thunk
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <Library/BaseLib.h>
|
|
|
|
|
2009-05-20 12:22:09 +02:00
|
|
|
ASM_GLOBAL ASM_PFX(m16Start)
|
|
|
|
ASM_GLOBAL ASM_PFX(m16Size)
|
|
|
|
ASM_GLOBAL ASM_PFX(mThunk16Attr)
|
|
|
|
ASM_GLOBAL ASM_PFX(m16Gdt)
|
|
|
|
ASM_GLOBAL ASM_PFX(m16GdtrBase)
|
|
|
|
ASM_GLOBAL ASM_PFX(mTransition)
|
|
|
|
ASM_GLOBAL ASM_PFX(InternalAsmThunk16)
|
2008-11-03 10:25:10 +01:00
|
|
|
|
|
|
|
# define the structure of IA32_REGS
|
2009-12-06 03:01:54 +01:00
|
|
|
.set _EDI, 0 #size 4
|
|
|
|
.set _ESI, 4 #size 4
|
|
|
|
.set _EBP, 8 #size 4
|
|
|
|
.set _ESP, 12 #size 4
|
|
|
|
.set _EBX, 16 #size 4
|
|
|
|
.set _EDX, 20 #size 4
|
|
|
|
.set _ECX, 24 #size 4
|
|
|
|
.set _EAX, 28 #size 4
|
|
|
|
.set _DS, 32 #size 2
|
|
|
|
.set _ES, 34 #size 2
|
|
|
|
.set _FS, 36 #size 2
|
|
|
|
.set _GS, 38 #size 2
|
|
|
|
.set _EFLAGS, 40 #size 8
|
|
|
|
.set _EIP, 48 #size 4
|
|
|
|
.set _CS, 52 #size 2
|
|
|
|
.set _SS, 54 #size 2
|
|
|
|
.set IA32_REGS_SIZE, 56
|
2008-11-03 10:25:10 +01:00
|
|
|
|
|
|
|
.data
|
2011-07-12 05:01:34 +02:00
|
|
|
|
|
|
|
.set Lm16Size, ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start)
|
|
|
|
ASM_PFX(m16Size): .word Lm16Size
|
|
|
|
.set LmThunk16Attr, L_ThunkAttr - ASM_PFX(m16Start)
|
|
|
|
ASM_PFX(mThunk16Attr): .word LmThunk16Attr
|
|
|
|
.set Lm16Gdt, ASM_PFX(NullSeg) - ASM_PFX(m16Start)
|
|
|
|
ASM_PFX(m16Gdt): .word Lm16Gdt
|
|
|
|
.set Lm16GdtrBase, _16GdtrBase - ASM_PFX(m16Start)
|
|
|
|
ASM_PFX(m16GdtrBase): .word Lm16GdtrBase
|
|
|
|
.set LmTransition, _EntryPoint - ASM_PFX(m16Start)
|
|
|
|
ASM_PFX(mTransition): .word LmTransition
|
2008-11-03 10:25:10 +01:00
|
|
|
|
|
|
|
.text
|
|
|
|
|
|
|
|
ASM_PFX(m16Start):
|
|
|
|
|
|
|
|
SavedGdt: .space 10
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# _BackFromUserCode() takes control in real mode after 'retf' has been executed
|
|
|
|
# by user code. It will be shadowed to somewhere in memory below 1MB.
|
|
|
|
#------------------------------------------------------------------------------
|
2009-05-20 12:22:09 +02:00
|
|
|
ASM_GLOBAL ASM_PFX(BackFromUserCode)
|
2008-11-03 10:25:10 +01:00
|
|
|
ASM_PFX(BackFromUserCode):
|
|
|
|
#
|
|
|
|
# The order of saved registers on the stack matches the order they appears
|
|
|
|
# in IA32_REGS structure. This facilitates wrapper function to extract them
|
|
|
|
# into that structure.
|
|
|
|
#
|
|
|
|
# Some instructions for manipulation of segment registers have to be written
|
|
|
|
# in opcode since 64-bit MASM prevents accesses to those registers.
|
|
|
|
#
|
|
|
|
.byte 0x16 # push ss
|
|
|
|
.byte 0xe # push cs
|
|
|
|
.byte 0x66
|
|
|
|
call L_Base # push eip
|
|
|
|
L_Base:
|
|
|
|
.byte 0x66
|
|
|
|
pushq $0 # reserved high order 32 bits of EFlags
|
|
|
|
.byte 0x66, 0x9c # pushfd actually
|
|
|
|
cli # disable interrupts
|
|
|
|
push %gs
|
|
|
|
push %fs
|
|
|
|
.byte 6 # push es
|
|
|
|
.byte 0x1e # push ds
|
|
|
|
.byte 0x66,0x60 # pushad
|
|
|
|
.byte 0x66,0xba # mov edx, imm32
|
2011-07-12 05:01:34 +02:00
|
|
|
L_ThunkAttr: .space 4
|
2008-11-03 10:25:10 +01:00
|
|
|
testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl
|
|
|
|
jz L_1
|
|
|
|
movl $0x15cd2401,%eax # mov ax, 2401h & int 15h
|
|
|
|
cli # disable interrupts
|
|
|
|
jnc L_2
|
|
|
|
L_1:
|
|
|
|
testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl
|
|
|
|
jz L_2
|
|
|
|
inb $0x92,%al
|
|
|
|
orb $2,%al
|
|
|
|
outb %al, $0x92 # deactivate A20M#
|
|
|
|
L_2:
|
2011-12-15 09:27:17 +01:00
|
|
|
xorw %ax, %ax # xor eax, eax
|
|
|
|
movl %ss, %eax # mov ax, ss
|
2008-11-03 10:25:10 +01:00
|
|
|
lea IA32_REGS_SIZE(%esp), %bp
|
|
|
|
#
|
|
|
|
# rsi in the following 2 instructions is indeed bp in 16-bit code
|
|
|
|
#
|
|
|
|
movw %bp, (_ESP - IA32_REGS_SIZE)(%rsi)
|
|
|
|
.byte 0x66
|
|
|
|
movl (_EIP - IA32_REGS_SIZE)(%rsi), %ebx
|
|
|
|
shlw $4,%ax # shl eax, 4
|
|
|
|
addw %ax,%bp # add ebp, eax
|
|
|
|
movw %cs,%ax
|
|
|
|
shlw $4,%ax
|
|
|
|
lea (L_64BitCode - L_Base)(%ebx, %eax), %ax
|
|
|
|
.byte 0x66,0x2e,0x89,0x87 # mov cs:[bx + (L_64Eip - L_Base)], eax
|
|
|
|
.word L_64Eip - L_Base
|
|
|
|
.byte 0x66,0xb8 # mov eax, imm32
|
2011-08-03 00:29:50 +02:00
|
|
|
L_SavedCr4: .space 4
|
2008-11-03 10:25:10 +01:00
|
|
|
movq %rax, %cr4
|
|
|
|
#
|
|
|
|
# rdi in the instruction below is indeed bx in 16-bit code
|
|
|
|
#
|
|
|
|
.byte 0x66,0x2e # 2eh is "cs:" segment override
|
|
|
|
lgdt (SavedGdt - L_Base)(%rdi)
|
|
|
|
.byte 0x66
|
|
|
|
movl $0xc0000080,%ecx
|
|
|
|
rdmsr
|
|
|
|
orb $1,%ah
|
|
|
|
wrmsr
|
|
|
|
.byte 0x66,0xb8 # mov eax, imm32
|
2011-08-03 00:29:50 +02:00
|
|
|
L_SavedCr0: .space 4
|
2008-11-03 10:25:10 +01:00
|
|
|
movq %rax, %cr0
|
|
|
|
.byte 0x66,0xea # jmp far cs:L_64Bit
|
2011-08-03 00:29:50 +02:00
|
|
|
L_64Eip: .space 4
|
|
|
|
L_SavedCs: .space 2
|
|
|
|
L_64BitCode:
|
2009-04-17 09:38:57 +02:00
|
|
|
.byte 0x90
|
2013-10-29 07:52:25 +01:00
|
|
|
.byte 0x48,0xbc # mov rsp, imm64
|
|
|
|
L_SavedSp: .space 8 # restore stack
|
2009-04-17 09:38:57 +02:00
|
|
|
nop
|
2008-11-03 10:25:10 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
_EntryPoint: .long ASM_PFX(ToUserCode) - ASM_PFX(m16Start)
|
|
|
|
.word CODE16
|
|
|
|
_16Gdtr: .word GDT_SIZE - 1
|
2011-10-08 22:51:17 +02:00
|
|
|
_16GdtrBase: .quad 0
|
2008-11-03 10:25:10 +01:00
|
|
|
_16Idtr: .word 0x3ff
|
|
|
|
.long 0
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# _ToUserCode() takes control in real mode before passing control to user code.
|
|
|
|
# It will be shadowed to somewhere in memory below 1MB.
|
|
|
|
#------------------------------------------------------------------------------
|
2009-05-20 12:22:09 +02:00
|
|
|
ASM_GLOBAL ASM_PFX(ToUserCode)
|
2008-11-03 10:25:10 +01:00
|
|
|
ASM_PFX(ToUserCode):
|
|
|
|
movl %edx,%ss # set new segment selectors
|
|
|
|
movl %edx,%ds
|
|
|
|
movl %edx,%es
|
|
|
|
movl %edx,%fs
|
|
|
|
movl %edx,%gs
|
|
|
|
.byte 0x66
|
|
|
|
movl $0xc0000080,%ecx
|
|
|
|
movq %rax, %cr0
|
|
|
|
rdmsr
|
2009-12-06 03:01:54 +01:00
|
|
|
andb $0xfe, %ah # $0b11111110
|
2008-11-03 10:25:10 +01:00
|
|
|
wrmsr
|
|
|
|
movq %rbp, %cr4
|
|
|
|
movl %esi,%ss # set up 16-bit stack segment
|
|
|
|
movw %bx,%sp # set up 16-bit stack pointer
|
|
|
|
.byte 0x66 # make the following call 32-bit
|
|
|
|
call L_Base1 # push eip
|
|
|
|
L_Base1:
|
|
|
|
popw %bp # ebp <- address of L_Base1
|
|
|
|
pushq (IA32_REGS_SIZE + 2)(%esp)
|
|
|
|
lea 0x0c(%rsi), %eax
|
|
|
|
pushq %rax
|
|
|
|
lret # execution begins at next instruction
|
|
|
|
L_RealMode:
|
|
|
|
.byte 0x66,0x2e # CS and operand size override
|
|
|
|
lidt (_16Idtr - L_Base1)(%rsi)
|
|
|
|
.byte 0x66,0x61 # popad
|
|
|
|
.byte 0x1f # pop ds
|
|
|
|
.byte 0x7 # pop es
|
|
|
|
.byte 0x0f, 0xa1 # pop fs
|
|
|
|
.byte 0x0f, 0xa9 # pop gs
|
|
|
|
.byte 0x66, 0x9d # popfd
|
|
|
|
leaw 4(%esp),%sp # skip high order 32 bits of EFlags
|
|
|
|
.byte 0x66 # make the following retf 32-bit
|
|
|
|
lret # transfer control to user code
|
|
|
|
|
2009-12-06 03:01:54 +01:00
|
|
|
.set CODE16, ASM_PFX(_16Code) - .
|
|
|
|
.set DATA16, ASM_PFX(_16Data) - .
|
|
|
|
.set DATA32, ASM_PFX(_32Data) - .
|
2008-11-03 10:25:10 +01:00
|
|
|
|
2009-04-10 22:58:07 +02:00
|
|
|
ASM_PFX(NullSeg): .quad 0
|
|
|
|
ASM_PFX(_16Code):
|
2008-11-03 10:25:10 +01:00
|
|
|
.word -1
|
|
|
|
.word 0
|
|
|
|
.byte 0
|
|
|
|
.byte 0x9b
|
|
|
|
.byte 0x8f # 16-bit segment, 4GB limit
|
|
|
|
.byte 0
|
2009-04-10 22:58:07 +02:00
|
|
|
ASM_PFX(_16Data):
|
2008-11-03 10:25:10 +01:00
|
|
|
.word -1
|
|
|
|
.word 0
|
|
|
|
.byte 0
|
|
|
|
.byte 0x93
|
|
|
|
.byte 0x8f # 16-bit segment, 4GB limit
|
|
|
|
.byte 0
|
2009-04-10 22:58:07 +02:00
|
|
|
ASM_PFX(_32Data):
|
2008-11-03 10:25:10 +01:00
|
|
|
.word -1
|
|
|
|
.word 0
|
|
|
|
.byte 0
|
|
|
|
.byte 0x93
|
|
|
|
.byte 0xcf # 16-bit segment, 4GB limit
|
|
|
|
.byte 0
|
|
|
|
|
2009-12-06 03:01:54 +01:00
|
|
|
.set GDT_SIZE, . - ASM_PFX(NullSeg)
|
2008-11-03 10:25:10 +01:00
|
|
|
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
# IA32_REGISTER_SET *
|
|
|
|
# EFIAPI
|
|
|
|
# InternalAsmThunk16 (
|
|
|
|
# IN IA32_REGISTER_SET *RegisterSet,
|
|
|
|
# IN OUT VOID *Transition
|
|
|
|
# );
|
|
|
|
#------------------------------------------------------------------------------
|
|
|
|
|
2009-05-20 12:22:09 +02:00
|
|
|
ASM_GLOBAL ASM_PFX(InternalAsmThunk16)
|
2008-11-03 10:25:10 +01:00
|
|
|
ASM_PFX(InternalAsmThunk16):
|
|
|
|
pushq %rbp
|
|
|
|
pushq %rbx
|
|
|
|
pushq %rsi
|
|
|
|
pushq %rdi
|
|
|
|
|
2009-12-06 03:01:54 +01:00
|
|
|
movl %ds, %ebx
|
2009-04-17 09:38:57 +02:00
|
|
|
pushq %rbx # Save ds segment register on the stack
|
2009-12-06 03:01:54 +01:00
|
|
|
movl %es, %ebx
|
2009-04-17 09:38:57 +02:00
|
|
|
pushq %rbx # Save es segment register on the stack
|
2009-12-06 03:01:54 +01:00
|
|
|
movl %ss, %ebx
|
2009-04-17 09:38:57 +02:00
|
|
|
pushq %rbx # Save ss segment register on the stack
|
|
|
|
|
2008-11-03 10:25:10 +01:00
|
|
|
.byte 0x0f, 0xa0 #push fs
|
|
|
|
.byte 0x0f, 0xa8 #push gs
|
|
|
|
movq %rcx, %rsi
|
|
|
|
movzwl _SS(%rsi), %r8d
|
|
|
|
movl _ESP(%rsi), %edi
|
|
|
|
lea -(IA32_REGS_SIZE + 4)(%edi), %rdi
|
|
|
|
imul $16, %r8d, %eax
|
|
|
|
movl %edi,%ebx # ebx <- stack for 16-bit code
|
|
|
|
pushq $(IA32_REGS_SIZE / 4)
|
|
|
|
addl %eax,%edi # edi <- linear address of 16-bit stack
|
|
|
|
popq %rcx
|
|
|
|
rep
|
|
|
|
movsl # copy RegSet
|
2011-07-12 05:01:34 +02:00
|
|
|
lea (L_SavedCr4 - ASM_PFX(m16Start))(%rdx), %ecx
|
2008-11-03 10:25:10 +01:00
|
|
|
movl %edx,%eax # eax <- transition code address
|
|
|
|
andl $0xf,%edx
|
|
|
|
shll $12,%eax # segment address in high order 16 bits
|
2011-07-12 05:01:34 +02:00
|
|
|
.set LBackFromUserCodeDelta, ASM_PFX(BackFromUserCode) - ASM_PFX(m16Start)
|
|
|
|
lea (LBackFromUserCodeDelta)(%rdx), %ax
|
2008-11-03 10:25:10 +01:00
|
|
|
stosl # [edi] <- return address of user code
|
2009-05-26 13:41:12 +02:00
|
|
|
sgdt 0x60(%rsp) # save GDT stack in argument space
|
|
|
|
movzwq 0x60(%rsp), %r10 # r10 <- GDT limit
|
2011-07-12 05:01:34 +02:00
|
|
|
lea ((ASM_PFX(InternalAsmThunk16) - L_SavedCr4) + 0xf)(%rcx), %r11
|
2009-12-06 03:01:54 +01:00
|
|
|
andq $0xfffffffffffffff0, %r11 # r11 <- 16-byte aligned shadowed GDT table in real mode buffer
|
2009-05-26 13:41:12 +02:00
|
|
|
|
2011-07-12 05:01:34 +02:00
|
|
|
movw %r10w, (SavedGdt - L_SavedCr4)(%rcx) # save the limit of shadowed GDT table
|
|
|
|
movq %r11, (SavedGdt - L_SavedCr4 + 0x2)(%rcx) # save the base address of shadowed GDT table
|
2009-05-26 13:41:12 +02:00
|
|
|
|
|
|
|
movq 0x62(%rsp) ,%rsi # rsi <- the original GDT base address
|
|
|
|
xchg %r10, %rcx # save rcx to r10 and initialize rcx to be the limit of GDT table
|
|
|
|
incq %rcx # rcx <- the size of memory to copy
|
|
|
|
xchg %r11, %rdi # save rdi to r11 and initialize rdi to the base address of shadowed GDT table
|
|
|
|
rep
|
|
|
|
movsb # perform memory copy to shadow GDT table
|
|
|
|
movq %r10, %rcx # restore the orignal rcx before memory copy
|
|
|
|
movq %r11, %rdi # restore the original rdi before memory copy
|
|
|
|
|
2009-04-17 09:38:57 +02:00
|
|
|
sidt 0x50(%rsp)
|
2008-11-03 10:25:10 +01:00
|
|
|
movq %cr0, %rax
|
2011-07-12 05:01:34 +02:00
|
|
|
.set LSavedCrDelta, L_SavedCr0 - L_SavedCr4
|
|
|
|
movl %eax, (LSavedCrDelta)(%rcx)
|
2008-11-03 10:25:10 +01:00
|
|
|
andl $0x7ffffffe,%eax # clear PE, PG bits
|
|
|
|
movq %cr4, %rbp
|
|
|
|
movl %ebp, (%rcx) # save CR4 in SavedCr4
|
2013-01-07 08:12:19 +01:00
|
|
|
andl $0xffffffcf,%ebp # clear PAE, PSE bits
|
2008-11-03 10:25:10 +01:00
|
|
|
movl %r8d, %esi # esi <- 16-bit stack segment
|
|
|
|
.byte 0x6a, DATA32
|
|
|
|
popq %rdx
|
2011-07-12 05:01:34 +02:00
|
|
|
lgdt (_16Gdtr - L_SavedCr4)(%rcx)
|
2008-11-03 10:25:10 +01:00
|
|
|
movl %edx,%ss
|
|
|
|
pushfq
|
|
|
|
lea -8(%rdx), %edx
|
2009-06-03 01:40:06 +02:00
|
|
|
lea L_RetFromRealMode(%rip), %r8
|
2008-11-03 10:25:10 +01:00
|
|
|
pushq %r8
|
|
|
|
movl %cs, %r8d
|
2011-07-12 05:01:34 +02:00
|
|
|
movw %r8w, (L_SavedCs - L_SavedCr4)(%rcx)
|
2013-10-29 07:52:25 +01:00
|
|
|
movq %rsp, (L_SavedSp - L_SavedCr4)(%rcx)
|
2011-07-12 05:01:34 +02:00
|
|
|
.byte 0xff, 0x69 # jmp (_EntryPoint - L_SavedCr4)(%rcx)
|
|
|
|
.set Ltemp1, _EntryPoint - L_SavedCr4
|
|
|
|
.byte Ltemp1
|
2008-11-03 10:25:10 +01:00
|
|
|
L_RetFromRealMode:
|
|
|
|
popfq
|
2009-05-26 13:41:12 +02:00
|
|
|
lgdt 0x60(%rsp) # restore protected mode GDTR
|
|
|
|
lidt 0x50(%rsp) # restore protected mode IDTR
|
2008-11-03 10:25:10 +01:00
|
|
|
lea -IA32_REGS_SIZE(%rbp), %eax
|
|
|
|
.byte 0x0f, 0xa9 # pop gs
|
|
|
|
.byte 0x0f, 0xa1 # pop fs
|
2009-04-17 09:38:57 +02:00
|
|
|
|
2009-05-20 12:22:09 +02:00
|
|
|
popq %rbx
|
2009-12-06 03:01:54 +01:00
|
|
|
movl %ebx, %ss
|
2009-05-20 12:22:09 +02:00
|
|
|
popq %rbx
|
2009-12-06 03:01:54 +01:00
|
|
|
movl %ebx, %es
|
2009-05-20 12:22:09 +02:00
|
|
|
popq %rbx
|
2009-12-06 03:01:54 +01:00
|
|
|
movl %ebx, %ds
|
2008-11-03 10:25:10 +01:00
|
|
|
|
|
|
|
popq %rdi
|
|
|
|
popq %rsi
|
|
|
|
popq %rbx
|
|
|
|
popq %rbp
|
|
|
|
|
|
|
|
ret
|