1 ;*****************************************************************************
3 ;* Copyright (c) 2006 - 2007, 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 ;*****************************************************************************
24 EXTERNDEF C mCode16Size:DWORD
26 CONST SEGMENT FLAT "DATA" READONLY
28 mCode16Size DD _TEXT16SIZE
32 _DATA SEGMENT FLAT "DATA"
35 _16BitCsSel LABEL QWORD
40 DB 8fh ; 16-bit segment
42 _16BitSsSel LABEL QWORD
47 DB 8fh ; 16-bit segment
51 DW $ - offset NullSegSel - 1
56 _TEXT SEGMENT FLAT "CODE" PARA
90 __Thunk16 PROC USES ebp ebx esi edi ds es fs gs
91 ASSUME esi:PTR IA32_REGS
95 add edi, - sizeof (_STK16) - sizeof (IA32_REGS)
96 push edi ; save stack offset
97 imul eax, edx, 16 ; eax <- edx*16
98 add edi, eax ; edi <- linear address of 16-bit stack
99 push sizeof (IA32_REGS) / 4
101 rep movsd ; copy context to 16-bit stack
102 pop ebx ; ebx <- 16-bit stack offset
103 mov eax, offset @F ; return offset
105 mov eax, cs ; return segment
107 mov eax, [esp + 40] ; THUNK flags
111 mov eax, ss ; save ss
113 sgdt fword ptr [edi] ; save GDTR
114 sidt fword ptr [esp + 36] ; save IDTR
116 mov [edi + 6], esi ; save CR0
117 and esi, NOT 80000001h ; esi <- CR0 to set
119 mov [edi + 10], eax ; save CR4
120 and al, NOT 30h ; clear PAE & PSE
121 mov edi, edx ; edi <- 16-bit stack segment
126 mov dx, _16BitSsSel - NullSegSel
127 lgdt _16Gdtr ; load 16-bit GDTR
130 DW _16BitCsSel - NullSegSel ; jmp far 8:@16Bit
133 mov cr0, esi ; disable protected mode
134 mov cr4, eax ; disable PAE & PSE
135 db 67h, 0FFh, 06Ch, 024h, 0FCh ; jmp dword ptr [esp-4]
139 add eax, esp ; eax <- address of 16-bit stack
140 lss esp, fword ptr (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedEsp
141 lidt fword ptr [esp + 36] ; restore IDTR
147 _TEXT16 SEGMENT USE16 "CODE" PARA
153 mov ss, di ; set up stack
155 lidt fword ptr cs:[_16Idtr - _Code16Addr]
161 add sp, 4 ; skip EFlags
162 test (_STK16 ptr [esp + 8]).ThunkFlags, 1
167 ; push @FarCallRet - _Code16Addr
169 DW @FarCallRet - _Code16Addr
171 jmp fword ptr [esp + 6]
173 jmp fword ptr [esp + 4]
183 lgdt (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedGdtr
184 mov eax, (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedCr4
186 mov eax, (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedCr0
187 mov cr0, eax ; restore CR0
188 jmp fword ptr (_STK16 ptr [esp + sizeof (IA32_REGS)]).RetEip
191 _16Idtr FWORD (1 SHL 10) - 1
195 _TEXT16SIZE = _TEXT16END - _Code16Addr