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
83 pushf ; pushfd actually
84 cli ; disable interrupts
89 pushaw ; pushad actually
90 DB 66h, 0bah ; mov edx, imm32
92 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
94 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
95 cli ; disable interrupts
98 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
102 out 92h, al ; deactivate A20M#
106 lea bp, [esp + sizeof (IA32_REGS)]
107 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
108 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
109 shl ax, 4 ; shl eax, 4
110 add bp, ax ; add ebp, eax
111 DB 66h, 0b8h ; mov eax, imm32
115 lgdt fword ptr cs:[edi + (SavedGdt - @Base)]
116 DB 66h, 0b8h ; mov eax, imm32
119 DB 0b8h ; mov ax, imm16
122 DB 66h, 0bch ; mov esp, imm32
125 retf ; return to protected mode
126 _BackFromUserCode ENDP
128 _EntryPoint DD _ToUserCode - m16Start
130 _16Idtr FWORD (1 SHL 10) - 1
132 DW GdtEnd - _NullSegDesc - 1
133 _16GdtrBase DD _NullSegDesc
135 ;------------------------------------------------------------------------------
136 ; _ToUserCode() takes control in real mode before passing control to user code.
137 ; It will be shadowed to somewhere in memory below 1MB.
138 ;------------------------------------------------------------------------------
141 mov ss, ecx ; set new segment selectors
147 mov cr4, ebp ; real mode starts at next instruction
148 mov ss, esi ; set up 16-bit stack segment
149 xchg sp, bx ; set up 16-bit stack pointer
151 call @Base ; push eip
153 pop bp ; ebp <- address of @Base
154 DB 67h ; address size override
155 push [esp + sizeof (IA32_REGS) + 2]
156 lea eax, [esi + (@RealMode - @Base)]
160 mov cs:[esi + (SavedSs - @Base)], edx
161 mov cs:[esi + (SavedEsp - @Base)], bx
163 lidt fword ptr cs:[esi + (_16Idtr - @Base)]
164 popaw ; popad actually
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
191 ;------------------------------------------------------------------------------
192 ; IA32_REGISTER_SET *
194 ; InternalAsmThunk16 (
195 ; IN IA32_REGISTER_SET *RegisterSet,
196 ; IN OUT VOID *Transition
198 ;------------------------------------------------------------------------------
199 InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
200 mov esi, [esp + 36] ; esi <- RegSet
201 movzx edx, (IA32_REGS ptr [esi])._SS
202 mov edi, (IA32_REGS ptr [esi])._ESP
203 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
204 mov ebx, edi ; ebx <- stack offset
205 imul eax, edx, 16 ; eax <- edx * 16
206 push sizeof (IA32_REGS) / 4
207 add edi, eax ; edi <- linear address of 16-bit stack
209 rep movsd ; copy RegSet
210 mov eax, [esp + 40] ; eax <- address of transition code
211 mov esi, edx ; esi <- 16-bit stack segment
212 lea edx, [eax + (SavedCr0 - m16Start)]
216 lea ecx, [ecx + (_BackFromUserCode - m16Start)]
218 stosd ; [edi] <- return address of user code
219 sgdt fword ptr [edx + (SavedGdt - SavedCr0)]
220 sidt fword ptr [esp + 36] ; save IDT stack in argument space
222 mov [edx], eax ; save CR0 in SavedCr0
223 and eax, 7ffffffeh ; clear PE, PG bits
225 mov [edx + (SavedCr4 - SavedCr0)], ebp
226 and ebp, 300h ; clear all but PCE and OSFXSR bits
228 pop ecx ; ecx <- selector for data segments
229 lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]
231 call fword ptr [edx + (_EntryPoint - SavedCr0)]
233 lidt fword ptr [esp + 36] ; restore protected mode IDTR
234 lea eax, [ebp - sizeof (IA32_REGS)]
236 InternalAsmThunk16 ENDP