1 ;------------------------------------------------------------------------------
3 ; Copyright (c) 2006, 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 ;------------------------------------------------------------------------------
25 EXTERNDEF C m16Start:BYTE
26 EXTERNDEF C m16Size:WORD
27 EXTERNDEF C mThunk16Attr:WORD
28 EXTERNDEF C m16Gdt:WORD
29 EXTERNDEF C m16GdtrBase:WORD
30 EXTERNDEF C mTransition:WORD
53 m16Size DW InternalAsmThunk16 - m16Start
54 mThunk16Attr DW _ThunkAttr - m16Start
55 m16Gdt DW _NullSegDesc - m16Start
56 m16GdtrBase DW _16GdtrBase - m16Start
57 mTransition DW _EntryPoint - m16Start
67 _BackFromUserCode PROC
73 pushf ; pushfd actually
74 cli ; disable interrupts
79 pushaw ; pushad actually
80 DB 66h, 0bah ; mov edx, imm32
82 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
84 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
85 cli ; disable interrupts
88 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
92 out 92h, al ; deactivate A20M#
96 lea bp, [esp + sizeof (IA32_REGS)]
97 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
98 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
99 shl ax, 4 ; shl eax, 4
100 add bp, ax ; add ebp, eax
101 DB 66h, 0b8h ; mov eax, imm32
105 lgdt fword ptr cs:[edi + (SavedGdt - @Base)]
106 DB 66h, 0b8h ; mov eax, imm32
109 DB 0b8h ; mov ax, imm16
112 DB 66h, 0bch ; mov esp, imm32
115 retf ; return to protected mode
116 _BackFromUserCode ENDP
118 _EntryPoint DD _ToUserCode - m16Start
120 _16Idtr FWORD (1 SHL 10) - 1
122 DW GdtEnd - _NullSegDesc - 1
123 _16GdtrBase DD _NullSegDesc
127 mov ss, ecx ; set new segment selectors
133 mov cr4, ebp ; real mode starts at next instruction
134 mov ss, esi ; set up 16-bit stack segment
135 xchg sp, bx ; set up 16-bit stack pointer
137 call @Base ; push eip
139 pop bp ; ebp <- address of @Base
140 DB 67h ; address size override
141 push [esp + sizeof (IA32_REGS) + 2]
142 lea eax, [esi + (@RealMode - @Base)]
146 mov cs:[esi + (SavedSs - @Base)], edx
147 mov cs:[esi + (SavedEsp - @Base)], bx
149 lidt fword ptr cs:[esi + (_16Idtr - @Base)]
150 popaw ; popad actually
157 retf ; transfer control to user code
161 _16CsDesc LABEL QWORD
166 DB 8fh ; 16-bit segment, 4GB limit
168 _16DsDesc LABEL QWORD
173 DB 8fh ; 16-bit segment, 4GB limit
178 ; @param RegSet Pointer to a IA32_DWORD_REGS structure
179 ; @param Transition Pointer to the transition code
180 ; @return The address of the 16-bit stack after returning from user code
182 InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
183 mov esi, [esp + 36] ; esi <- RegSet
184 movzx edx, (IA32_REGS ptr [esi])._SS
185 mov edi, (IA32_REGS ptr [esi])._ESP
186 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
187 mov ebx, edi ; ebx <- stack offset
188 imul eax, edx, 16 ; eax <- edx * 16
189 push sizeof (IA32_REGS) / 4
190 add edi, eax ; edi <- linear address of 16-bit stack
192 rep movsd ; copy RegSet
193 mov eax, [esp + 40] ; eax <- address of transition code
194 mov esi, edx ; esi <- 16-bit stack segment
195 lea edx, [eax + (SavedCr0 - m16Start)]
199 lea ecx, [ecx + (_BackFromUserCode - m16Start)]
201 stosd ; [edi] <- return address of user code
202 sgdt fword ptr [edx + (SavedGdt - SavedCr0)]
203 sidt fword ptr [esp + 36] ; save IDT stack in argument space
205 mov [edx], eax ; save CR0 in SavedCr0
206 and eax, 7ffffffeh ; clear PE, PG bits
208 mov [edx + (SavedCr4 - SavedCr0)], ebp
209 and ebp, 300h ; clear all but PCE and OSFXSR bits
211 pop ecx ; ecx <- selector for data segments
212 lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]
214 call fword ptr [edx + (_EntryPoint - SavedCr0)]
216 lidt fword ptr [esp + 36] ; restore protected mode IDTR
217 lea eax, [ebp - sizeof (IA32_REGS)]
219 InternalAsmThunk16 ENDP