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
33 ; Here is the layout of the real mode stack. _ToUserCode() is responsible for
34 ; loading all these registers from real mode stack.
58 ; These are global constant to convey information to C code.
60 m16Size DW InternalAsmThunk16 - m16Start
61 mThunk16Attr DW _ThunkAttr - m16Start
62 m16Gdt DW _NullSegDesc - m16Start
63 m16GdtrBase DW _16GdtrBase - m16Start
64 mTransition DW _EntryPoint - m16Start
73 ;------------------------------------------------------------------------------
74 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
75 ; by user code. It will be shadowed to somewhere in memory below 1MB.
76 ;------------------------------------------------------------------------------
77 _BackFromUserCode PROC
79 ; The order of saved registers on the stack matches the order they appears
80 ; in IA32_REGS structure. This facilitates wrapper function to extract them
81 ; into that structure.
88 pushf ; pushfd actually
89 cli ; disable interrupts
94 pushaw ; pushad actually
95 DB 66h, 0bah ; mov edx, imm32
97 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
99 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
100 cli ; disable interrupts
103 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
107 out 92h, al ; deactivate A20M#
111 lea bp, [esp + sizeof (IA32_REGS)]
113 ; esi's in the following 2 instructions are indeed bp in 16-bit code. Fact
114 ; is "esi" in 32-bit addressing mode has the same encoding of "bp" in 16-
115 ; bit addressing mode.
117 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
118 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
119 shl ax, 4 ; shl eax, 4
120 add bp, ax ; add ebp, eax
121 DB 66h, 0b8h ; mov eax, imm32
125 lgdt fword ptr cs:[edi + (SavedGdt - @Base)]
126 DB 66h, 0b8h ; mov eax, imm32
129 DB 0b8h ; mov ax, imm16
132 DB 66h, 0bch ; mov esp, imm32
135 retf ; return to protected mode
136 _BackFromUserCode ENDP
138 _EntryPoint DD _ToUserCode - m16Start
140 _16Idtr FWORD (1 SHL 10) - 1
142 DW GdtEnd - _NullSegDesc - 1
143 _16GdtrBase DD _NullSegDesc
145 ;------------------------------------------------------------------------------
146 ; _ToUserCode() takes control in real mode before passing control to user code.
147 ; It will be shadowed to somewhere in memory below 1MB.
148 ;------------------------------------------------------------------------------
151 mov ss, ecx ; set new segment selectors
157 mov cr4, ebp ; real mode starts at next instruction
158 mov ss, esi ; set up 16-bit stack segment
159 xchg sp, bx ; set up 16-bit stack pointer
161 call @Base ; push eip
163 pop bp ; ebp <- address of @Base
164 DB 67h ; address size override
165 push [esp + sizeof (IA32_REGS) + 2]
166 lea eax, [esi + (@RealMode - @Base)]
170 mov cs:[esi + (SavedSs - @Base)], edx
171 mov cs:[esi + (SavedEsp - @Base)], bx
173 lidt fword ptr cs:[esi + (_16Idtr - @Base)]
174 popaw ; popad actually
180 DB 66h ; Use 32-bit addressing for "retf" below
181 retf ; transfer control to user code
185 _16CsDesc LABEL QWORD
190 DB 8fh ; 16-bit segment, 4GB limit
192 _16DsDesc LABEL QWORD
197 DB 8fh ; 16-bit segment, 4GB limit
201 ;------------------------------------------------------------------------------
202 ; IA32_REGISTER_SET *
204 ; InternalAsmThunk16 (
205 ; IN IA32_REGISTER_SET *RegisterSet,
206 ; IN OUT VOID *Transition
208 ;------------------------------------------------------------------------------
209 InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
210 mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter
211 movzx edx, (IA32_REGS ptr [esi])._SS
212 mov edi, (IA32_REGS ptr [esi])._ESP
213 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
214 mov ebx, edi ; ebx <- stack offset
215 imul eax, edx, 16 ; eax <- edx * 16
216 push sizeof (IA32_REGS) / 4
217 add edi, eax ; edi <- linear address of 16-bit stack
219 rep movsd ; copy RegSet
220 mov eax, [esp + 40] ; eax <- address of transition code
221 mov esi, edx ; esi <- 16-bit stack segment
222 lea edx, [eax + (SavedCr0 - m16Start)]
226 lea ecx, [ecx + (_BackFromUserCode - m16Start)]
228 stosd ; [edi] <- return address of user code
229 sgdt fword ptr [edx + (SavedGdt - SavedCr0)]
230 sidt fword ptr [esp + 36] ; save IDT stack in argument space
232 mov [edx], eax ; save CR0 in SavedCr0
233 and eax, 7ffffffeh ; clear PE, PG bits
235 mov [edx + (SavedCr4 - SavedCr0)], ebp
236 and ebp, 300h ; clear all but PCE and OSFXSR bits
238 pop ecx ; ecx <- selector for data segments
239 lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]
240 pushfd ; Save df/if indeed
241 call fword ptr [edx + (_EntryPoint - SavedCr0)]
243 lidt fword ptr [esp + 36] ; restore protected mode IDTR
244 lea eax, [ebp - sizeof (IA32_REGS)] ; eax <- the address of IA32_REGS
246 InternalAsmThunk16 ENDP