1 ; Copyright (c) 2004, Intel Corporation
2 ; All rights reserved. This program and the accompanying materials
3 ; are licensed and made available under the terms and conditions of the BSD License
4 ; which accompanies this distribution. The full text of the license may be found at
5 ; http://opensource.org/licenses/bsd-license.php
7 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
8 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 ;------------------------------------------------------------------------------
20 EXTERNDEF m16Start:BYTE
21 EXTERNDEF m16Size:WORD
22 EXTERNDEF mThunk16Attr:WORD
24 EXTERNDEF m16GdtrBase:WORD
25 EXTERNDEF mTransition:WORD
27 THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 EQU 2
28 THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL EQU 4
51 m16Size DW InternalAsmThunk16 - m16Start
52 mThunk16Attr DW _ThunkAttr - m16Start
53 m16Gdt DW _NullSeg - m16Start
54 m16GdtrBase DW _16GdtrBase - m16Start
55 mTransition DW _EntryPoint - m16Start
65 ;------------------------------------------------------------------------------
66 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
67 ; by user code. It will be shadowed to somewhere in memory below 1MB.
68 ;------------------------------------------------------------------------------
69 _BackFromUserCode PROC
71 ; The order of saved registers on the stack matches the order they appears
72 ; in IA32_REGS structure. This facilitates wrapper function to extract them
73 ; into that structure.
75 ; Some instructions for manipulation of segment registers have to be written
76 ; in opcode since 64-bit MASM prevents accesses to those registers.
84 push 0 ; reserved high order 32 bits of EFlags
85 pushf ; pushfd actually
86 cli ; disable interrupts
92 DB 66h, 0bah ; mov edx, imm32
94 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
96 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
97 cli ; disable interrupts
100 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
104 out 92h, al ; deactivate A20M#
107 lea bp, [esp + sizeof (IA32_REGS)]
109 ; rsi in the following 2 instructions is indeed bp in 16-bit code
111 mov word ptr (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._ESP, bp
113 mov ebx, (IA32_REGS ptr [rsi - sizeof (IA32_REGS)])._EIP
114 shl ax, 4 ; shl eax, 4
115 add bp, ax ; add ebp, eax
118 lea ax, [eax + ebx + (@64BitCode - @Base)]
119 DB 66h, 2eh, 89h, 87h ; mov cs:[bx + (@64Eip - @Base)], eax
121 DB 66h, 0b8h ; mov eax, imm32
125 ; rdi in the instruction below is indeed bx in 16-bit code
127 DB 66h, 2eh ; 2eh is "cs:" segment override
128 lgdt fword ptr [rdi + (SavedGdt - @Base)]
134 DB 66h, 0b8h ; mov eax, imm32
137 DB 66h, 0eah ; jmp far cs:@64Bit
141 mov rsp, r8 ; restore stack
143 _BackFromUserCode ENDP
145 _EntryPoint DD _ToUserCode - m16Start
149 _16GdtrBase DQ _NullSeg
150 _16Idtr FWORD (1 SHL 10) - 1
152 ;------------------------------------------------------------------------------
153 ; _ToUserCode() takes control in real mode before passing control to user code.
154 ; It will be shadowed to somewhere in memory below 1MB.
155 ;------------------------------------------------------------------------------
157 mov ss, edx ; set new segment selectors
164 mov cr0, rax ; real mode starts at next instruction
169 mov ss, esi ; set up 16-bit stack segment
170 mov sp, bx ; set up 16-bit stack pointer
171 DB 66h ; make the following call 32-bit
172 call @Base ; push eip
174 pop bp ; ebp <- address of @Base
175 push [esp + sizeof (IA32_REGS) + 2]
176 lea eax, [rsi + (@RealMode - @Base)] ; rsi is "bp" in 16-bit code
178 retf ; execution begins at next instruction
180 DB 66h, 2eh ; CS and operand size override
181 lidt fword ptr [rsi + (_16Idtr - @Base)]
188 lea sp, [esp + 4] ; skip high order 32 bits of EFlags
189 DB 66h ; make the following retf 32-bit
190 retf ; transfer control to user code
203 DB 8fh ; 16-bit segment, 4GB limit
210 DB 8fh ; 16-bit segment, 4GB limit
217 DB 0cfh ; 16-bit segment, 4GB limit
220 GDT_SIZE = $ - _NullSeg
222 ;------------------------------------------------------------------------------
223 ; IA32_REGISTER_SET *
225 ; InternalAsmThunk16 (
226 ; IN IA32_REGISTER_SET *RegisterSet,
227 ; IN OUT VOID *Transition
229 ;------------------------------------------------------------------------------
230 InternalAsmThunk16 PROC USES rbp rbx rsi rdi
231 mov r10d, ds ; r9 ~ r11 are not accessible in 16-bit
232 mov r11d, es ; so use them for saving seg registers
237 movzx r8d, (IA32_REGS ptr [rsi])._SS
238 mov edi, (IA32_REGS ptr [rsi])._ESP
239 lea rdi, [edi - (sizeof (IA32_REGS) + 4)]
240 imul eax, r8d, 16 ; eax <- r8d(stack segment) * 16
241 mov ebx, edi ; ebx <- stack for 16-bit code
242 push sizeof (IA32_REGS) / 4
243 add edi, eax ; edi <- linear address of 16-bit stack
245 rep movsd ; copy RegSet
246 lea ecx, [rdx + (SavedCr4 - m16Start)]
247 mov eax, edx ; eax <- transition code address
249 shl eax, 12 ; segment address in high order 16 bits
250 lea ax, [rdx + (_BackFromUserCode - m16Start)] ; offset address
251 stosd ; [edi] <- return address of user code
252 sgdt fword ptr [rcx + (SavedGdt - SavedCr4)]
253 sidt fword ptr [rsp + 38h] ; save IDT stack in argument space
255 mov [rcx + (SavedCr0 - SavedCr4)], eax
256 and eax, 7ffffffeh ; clear PE, PG bits
258 mov [rcx], ebp ; save CR4 in SavedCr4
259 and ebp, 300h ; clear all but PCE and OSFXSR bits
260 mov esi, r8d ; esi <- 16-bit stack segment
261 DB 6ah, DATA32 ; push DATA32
262 pop rdx ; rdx <- 32-bit data segment selector
263 lgdt fword ptr [rcx + (_16Gdtr - SavedCr4)]
266 lea edx, [rdx + DATA16 - DATA32]
267 lea r8, @RetFromRealMode
270 mov [rcx + (SavedCs - SavedCr4)], r8w
272 jmp fword ptr [rcx + (_EntryPoint - SavedCr4)]
275 lidt fword ptr [rsp + 38h] ; restore protected mode IDTR
276 lea eax, [rbp - sizeof (IA32_REGS)]
283 InternalAsmThunk16 ENDP