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 ;------------------------------------------------------------------------------
22 EXTERNDEF m16Start:BYTE
23 EXTERNDEF m16Size:WORD
24 EXTERNDEF mThunk16Attr:WORD
26 EXTERNDEF m16GdtrBase:WORD
27 EXTERNDEF mTransition:WORD
29 THUNK_ATTRIBUTE_BIG_REAL_MODE EQU 1
30 THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 EQU 2
31 THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL EQU 4
54 m16Size DW offset InternalAsmThunk16 - offset m16Start
55 mThunk16Attr DW offset _ThunkAttr - offset m16Start
56 m16Gdt DW offset _NullSegDesc - offset m16Start
57 m16GdtrBase DW offset _16GdtrBase - offset m16Start
58 mTransition DW offset _EntryPoint - offset m16Start
68 _BackFromUserCode PROC
75 push 0 ; reserved high order 32 bits of EFlags
76 pushf ; pushfd actually
77 cli ; disable interrupts
83 DB 66h, 0bah ; mov edx, imm32
85 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
87 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
88 cli ; disable interrupts
91 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
95 out 92h, al ; deactivate A20M#
98 lea bp, [esp + sizeof (IA32_REGS)]
99 mov word ptr (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._ESP, bp
100 mov ebx, (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._EIP
101 shl ax, 4 ; shl eax, 4
102 add bp, ax ; add ebp, eax
103 DB 66h, 0b8h ; mov eax, imm32
107 lgdt fword ptr [rdi + (offset SavedGdt - offset @Base)]
113 DB 66h, 0b8h ; mov eax, imm32
116 DB 0b8h ; mov ax, imm16
119 DB 66h, 0bch ; mov esp, imm32
122 retf ; return to protected mode
123 _BackFromUserCode ENDP
125 _EntryPoint DD offset _ToUserCode - offset m16Start
128 DW offset GdtEnd - offset _NullSegDesc - 1
129 _16GdtrBase DQ offset _NullSegDesc
130 _16Idtr FWORD (1 SHL 10) - 1
134 mov ss, edx ; set new segment selectors
141 mov cr0, rax ; real mode starts at next instruction
146 mov ss, esi ; set up 16-bit stack segment
147 xchg sp, bx ; set up 16-bit stack pointer
149 call @Base ; push eip
151 pop bp ; ebp <- offset @Base
152 push [esp + sizeof (IA32_REGS) + 2]
153 lea eax, [rsi + (offset @RealMode - offset @Base)]
158 mov [rsi + (offset SavedSs - offset @Base)], edi
160 mov [rsi + (offset SavedEsp - offset @Base)], bx
161 DB 66h, 2eh ; CS and operand size override
162 lidt fword ptr [rsi + (offset _16Idtr - offset @Base)]
169 lea sp, [esp + 4] ; skip high order 32 bits of EFlags
171 retf ; transfer control to user code
175 _16CsDesc LABEL QWORD
180 DB 8fh ; 16-bit segment, 4GB limit
182 _16DsDesc LABEL QWORD
187 DB 8fh ; 16-bit segment, 4GB limit
192 ; @param RegSet Pointer to a IA32_DWORD_REGS structure
193 ; @param Transition Pointer to the transition code
194 ; @return The address of the 16-bit stack after returning from user code
196 InternalAsmThunk16 PROC USES rbp rbx rsi rdi
202 movzx r8d, (IA32_REGS ptr [rsi])._SS
203 mov edi, (IA32_REGS ptr [rsi])._ESP
204 lea rdi, [edi - (sizeof (IA32_REGS) + 4)]
205 imul eax, r8d, 16 ; eax <- r8d(stack segment) * 16
206 mov ebx, edi ; ebx <- stack offset for 16-bit code
207 push sizeof (IA32_REGS) / 4
208 add edi, eax ; edi <- linear address of 16-bit stack
210 rep movsd ; copy RegSet
211 lea ecx, [rdx + (offset SavedCr4 - offset m16Start)]
212 mov eax, edx ; eax <- transition code address
215 lea edx, [rdx + (offset _BackFromUserCode - offset m16Start)]
217 stosd ; [edi] <- return address of user code
218 sgdt fword ptr [rcx + (offset SavedGdt - offset SavedCr4)]
219 sidt fword ptr [rsp + 38h] ; save IDT stack in argument space
221 mov [rcx + (offset SavedCr0 - offset SavedCr4)], eax
222 and eax, 7ffffffeh ; clear PE, PG bits
224 mov [rcx], ebp ; save CR4 in SavedCr4
225 and ebp, 300h ; clear all but PCE and OSFXSR bits
226 mov esi, r8d ; esi <- 16-bit stack segment
228 pop rdx ; rdx <- selector for data segments
229 lgdt fword ptr [rcx + (offset _16Gdtr - offset SavedCr4)]
231 call fword ptr [rcx + (offset _EntryPoint - offset SavedCr4)]
233 lidt fword ptr [rsp + 38h] ; restore protected mode IDTR
234 lea eax, [rbp - sizeof (IA32_REGS)]
240 InternalAsmThunk16 ENDP