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
32 THUNK_ATTRIBUTE_BIG_REAL_MODE EQU 1
33 THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 EQU 2
34 THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL EQU 4
57 m16Size DW offset InternalAsmThunk16 - offset m16Start
58 mThunk16Attr DW offset _ThunkAttr - offset m16Start
59 m16Gdt DW offset _NullSegDesc - offset m16Start
60 m16GdtrBase DW offset _16GdtrBase - offset m16Start
61 mTransition DW offset _EntryPoint - offset m16Start
71 _BackFromUserCode PROC
77 pushf ; pushfd actually
78 cli ; disable interrupts
83 pushaw ; pushad actually
84 DB 66h, 0bah ; mov edx, imm32
86 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
88 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
89 cli ; disable interrupts
92 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
96 out 92h, al ; deactivate A20M#
100 lea bp, [esp + sizeof (IA32_REGS)]
101 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
102 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
103 shl ax, 4 ; shl eax, 4
104 add bp, ax ; add ebp, eax
105 DB 66h, 0b8h ; mov eax, imm32
109 lgdt fword ptr cs:[edi + (offset SavedGdt - offset @Base)]
110 DB 66h, 0b8h ; mov eax, imm32
113 DB 0b8h ; mov ax, imm16
116 DB 66h, 0bch ; mov esp, imm32
119 retf ; return to protected mode
120 _BackFromUserCode ENDP
122 _EntryPoint DD offset _ToUserCode - offset m16Start
124 _16Idtr FWORD (1 SHL 10) - 1
126 DW offset GdtEnd - offset _NullSegDesc - 1
127 _16GdtrBase DD offset _NullSegDesc
131 mov ss, ecx ; set new segment selectors
137 mov cr4, ebp ; real mode starts at next instruction
138 mov ss, esi ; set up 16-bit stack segment
139 xchg sp, bx ; set up 16-bit stack pointer
141 call @Base ; push eip
143 pop bp ; ebp <- offset @Base
144 DB 67h ; address size override
145 push [esp + sizeof (IA32_REGS) + 2]
146 lea eax, [esi + (offset @RealMode - offset @Base)]
150 mov cs:[esi + (offset SavedSs - offset @Base)], edx
151 mov cs:[esi + (offset SavedEsp - offset @Base)], bx
153 lidt fword ptr cs:[esi + (offset _16Idtr - offset @Base)]
154 popaw ; popad actually
161 retf ; transfer control to user code
165 _16CsDesc LABEL QWORD
170 DB 8fh ; 16-bit segment, 4GB limit
172 _16DsDesc LABEL QWORD
177 DB 8fh ; 16-bit segment, 4GB limit
182 ; @param RegSet Pointer to a IA32_DWORD_REGS structure
183 ; @param Transition Pointer to the transition code
184 ; @return The address of the 16-bit stack after returning from user code
186 InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
187 mov esi, [esp + 36] ; esi <- RegSet
188 movzx edx, (IA32_REGS ptr [esi])._SS
189 mov edi, (IA32_REGS ptr [esi])._ESP
190 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
191 mov ebx, edi ; ebx <- stack offset
192 imul eax, edx, 16 ; eax <- edx * 16
193 push sizeof (IA32_REGS) / 4
194 add edi, eax ; edi <- linear address of 16-bit stack
196 rep movsd ; copy RegSet
197 mov eax, [esp + 40] ; eax <- address of transition code
198 mov esi, edx ; esi <- 16-bit stack segment
199 lea edx, [eax + (offset SavedCr0 - offset m16Start)]
203 lea ecx, [ecx + (offset _BackFromUserCode - offset m16Start)]
205 stosd ; [edi] <- return address of user code
206 sgdt fword ptr [edx + (offset SavedGdt - offset SavedCr0)]
207 sidt fword ptr [esp + 36] ; save IDT stack in argument space
209 mov [edx], eax ; save CR0 in SavedCr0
210 and eax, 7ffffffeh ; clear PE, PG bits
212 mov [edx + (offset SavedCr4 - offset SavedCr0)], ebp
213 and ebp, 300h ; clear all but PCE and OSFXSR bits
215 pop ecx ; ecx <- selector for data segments
216 lgdt fword ptr [edx + (offset _16Gdtr - offset SavedCr0)]
218 call fword ptr [edx + (offset _EntryPoint - offset SavedCr0)]
220 lidt fword ptr [esp + 36] ; restore protected mode IDTR
221 lea eax, [ebp - sizeof (IA32_REGS)]
223 InternalAsmThunk16 ENDP