1 ;*****************************************************************************
3 ;* Copyright (c) 2006, 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
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 EXTERNDEF mCode16Size:QWORD
26 mCode16Size DQ _Code16End - _Code16Addr
31 _16CsSegSel LABEL QWORD
36 DB 8fh ; 16-bit segment
38 _16DsSegSel LABEL QWORD
43 DB 8fh ; 16-bit segment
47 DW $ - offset NullSegSel - 1
81 _Thunk16 PROC USES rbp rbx rsi rdi r12 r13 r14 r15
91 movzx r10, (IA32_REGS ptr [rsi])._SS
93 mov edi, (IA32_REGS ptr [rsi])._ESP
94 add rdi, - sizeof (IA32_REGS) - sizeof (_STK16)
98 push sizeof (IA32_REGS) / 4
101 pop rbx ; rbx <- 16-bit stack offset
102 lea eax, @F ; return offset
104 mov eax, cs ; return segment
106 mov eax, edx ; THUNK Flags
108 sgdt fword ptr [rsp + 58h] ; save GDTR
111 mov rax, cr0 ; save CR0
112 mov esi, eax ; esi <- CR0 to set
114 mov rax, cr4 ; save CR4
116 sidt fword ptr [rsp + 58h] ; save IDTR
117 and esi, 07ffffffeh ; clear PE & PG bits
118 mov rdi, r10 ; rdi <- 16-bit stack segment
121 push r8 ; far jmp address
124 mov word ptr [rsp + 4], 8
130 mov cr0, rsi ; disable PE & PG
133 wrmsr ; clear LME bit
135 and al, NOT 30h ; clear PAE & PSE
142 add eax, esp ; rax <- address of 16-bit stack
144 lidt fword ptr [rsp + 58h] ; restore IDTR
160 mov sp, bx ; set up 16-bit stack
162 DW _16Idtr - _Code16Addr ; lidt _16Idtr
169 add esp, 8 ; skip RFLAGS
170 DB 67h, 0f7h, 44h, 24h, 0eh, 1, 0 ; test [esp + 0eh], 1
172 pushfq ; pushf, actually
176 DW @FarCallRet - _Code16Addr
179 jmp fword ptr [esp + 6]
182 jmp fword ptr [esp + 4]
185 push 0 ; push a dword of zero
186 pushf ; pushfd, actually
195 lgdt (_STK16 ptr [esp + sizeof(IA32_REGS)]).SavedGdtr
197 mov eax, (_STK16 ptr [esp + sizeof(IA32_REGS)]).SavedCr4
205 mov eax, (_STK16 ptr [esp + sizeof(IA32_REGS)]).SavedCr0
208 jmp fword ptr (_STK16 ptr [esp + sizeof(IA32_REGS)]).RetEip
212 _16Idtr FWORD (1 SHL 10) - 1