2 #include "BaseLibInternals.h"
4 ;------------------------------------------------------------------------------
6 ; Copyright (c) 2006 - 2008, Intel Corporation
7 ; All rights reserved. This program and the accompanying materials
8 ; are licensed and made available under the terms and conditions of the BSD License
9 ; which accompanies this distribution. The full text of the license may be found at
10 ; http://opensource.org/licenses/bsd-license.php
12 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 ;------------------------------------------------------------------------------
28 EXTERNDEF C m16Start:BYTE
29 EXTERNDEF C m16Size:WORD
30 EXTERNDEF C mThunk16Attr:WORD
31 EXTERNDEF C m16Gdt:WORD
32 EXTERNDEF C m16GdtrBase:WORD
33 EXTERNDEF C mTransition:WORD
36 ; Here is the layout of the real mode stack. _ToUserCode() is responsible for
37 ; loading all these registers from real mode stack.
61 ; These are global constant to convey information to C code.
63 m16Size DW InternalAsmThunk16 - m16Start
64 mThunk16Attr DW _ThunkAttr - m16Start
65 m16Gdt DW _NullSegDesc - m16Start
66 m16GdtrBase DW _16GdtrBase - m16Start
67 mTransition DW _EntryPoint - m16Start
76 ;------------------------------------------------------------------------------
77 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
78 ; by user code. It will be shadowed to somewhere in memory below 1MB.
79 ;------------------------------------------------------------------------------
80 _BackFromUserCode PROC
82 ; The order of saved registers on the stack matches the order they appears
83 ; in IA32_REGS structure. This facilitates wrapper function to extract them
84 ; into that structure.
91 pushf ; pushfd actually
92 cli ; disable interrupts
97 pushaw ; pushad actually
98 DB 66h, 0bah ; mov edx, imm32
100 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
102 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
103 cli ; disable interrupts
106 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
110 out 92h, al ; deactivate A20M#
114 lea bp, [esp + sizeof (IA32_REGS)]
116 ; esi's in the following 2 instructions are indeed bp in 16-bit code. Fact
117 ; is "esi" in 32-bit addressing mode has the same encoding of "bp" in 16-
118 ; bit addressing mode.
120 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
121 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
122 shl ax, 4 ; shl eax, 4
123 add bp, ax ; add ebp, eax
124 DB 66h, 0b8h ; mov eax, imm32
128 lgdt fword ptr cs:[edi + (SavedGdt - @Base)]
129 DB 66h, 0b8h ; mov eax, imm32
132 DB 0b8h ; mov ax, imm16
135 DB 66h, 0bch ; mov esp, imm32
138 retf ; return to protected mode
139 _BackFromUserCode ENDP
141 _EntryPoint DD _ToUserCode - m16Start
143 _16Idtr FWORD (1 SHL 10) - 1
145 DW GdtEnd - _NullSegDesc - 1
146 _16GdtrBase DD _NullSegDesc
148 ;------------------------------------------------------------------------------
149 ; _ToUserCode() takes control in real mode before passing control to user code.
150 ; It will be shadowed to somewhere in memory below 1MB.
151 ;------------------------------------------------------------------------------
154 mov ss, ecx ; set new segment selectors
160 mov cr4, ebp ; real mode starts at next instruction
161 mov ss, esi ; set up 16-bit stack segment
162 xchg sp, bx ; set up 16-bit stack pointer
164 call @Base ; push eip
166 pop bp ; ebp <- address of @Base
167 DB 67h ; address size override
168 push [esp + sizeof (IA32_REGS) + 2]
169 lea eax, [esi + (@RealMode - @Base)]
173 mov cs:[esi + (SavedSs - @Base)], edx
174 mov cs:[esi + (SavedEsp - @Base)], bx
176 lidt fword ptr cs:[esi + (_16Idtr - @Base)]
177 popaw ; popad actually
183 DB 66h ; Use 32-bit addressing for "retf" below
184 retf ; transfer control to user code
188 _16CsDesc LABEL QWORD
193 DB 8fh ; 16-bit segment, 4GB limit
195 _16DsDesc LABEL QWORD
200 DB 8fh ; 16-bit segment, 4GB limit
204 ;------------------------------------------------------------------------------
205 ; IA32_REGISTER_SET *
207 ; InternalAsmThunk16 (
208 ; IN IA32_REGISTER_SET *RegisterSet,
209 ; IN OUT VOID *Transition
211 ;------------------------------------------------------------------------------
212 InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
213 mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter
214 movzx edx, (IA32_REGS ptr [esi])._SS
215 mov edi, (IA32_REGS ptr [esi])._ESP
216 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
217 mov ebx, edi ; ebx <- stack offset
218 imul eax, edx, 16 ; eax <- edx * 16
219 push sizeof (IA32_REGS) / 4
220 add edi, eax ; edi <- linear address of 16-bit stack
222 rep movsd ; copy RegSet
223 mov eax, [esp + 40] ; eax <- address of transition code
224 mov esi, edx ; esi <- 16-bit stack segment
225 lea edx, [eax + (SavedCr0 - m16Start)]
229 lea ecx, [ecx + (_BackFromUserCode - m16Start)]
231 stosd ; [edi] <- return address of user code
232 sgdt fword ptr [edx + (SavedGdt - SavedCr0)]
233 sidt fword ptr [esp + 36] ; save IDT stack in argument space
235 mov [edx], eax ; save CR0 in SavedCr0
236 and eax, 7ffffffeh ; clear PE, PG bits
238 mov [edx + (SavedCr4 - SavedCr0)], ebp
239 and ebp, 300h ; clear all but PCE and OSFXSR bits
241 pop ecx ; ecx <- selector for data segments
242 lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]
243 pushfd ; Save df/if indeed
244 call fword ptr [edx + (_EntryPoint - SavedCr0)]
246 lidt fword ptr [esp + 36] ; restore protected mode IDTR
247 lea eax, [ebp - sizeof (IA32_REGS)] ; eax <- the address of IA32_REGS
249 InternalAsmThunk16 ENDP