2 #include "BaseLibInternals.h"
4 ;------------------------------------------------------------------------------
6 ; Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
7 ; 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 ;------------------------------------------------------------------------------
25 global ASM_PFX(m16Size)
26 global ASM_PFX(mThunk16Attr)
27 global ASM_PFX(m16Gdt)
28 global ASM_PFX(m16GdtrBase)
29 global ASM_PFX(mTransition)
30 global ASM_PFX(m16Start)
59 ; These are global constant to convey information to C code.
61 ASM_PFX(m16Size) DW InternalAsmThunk16 - ASM_PFX(m16Start)
62 ASM_PFX(mThunk16Attr) DW _BackFromUserCode.ThunkAttr - ASM_PFX(m16Start)
63 ASM_PFX(m16Gdt) DW _NullSegDesc - ASM_PFX(m16Start)
64 ASM_PFX(m16GdtrBase) DW _16GdtrBase - ASM_PFX(m16Start)
65 ASM_PFX(mTransition) DW _EntryPoint - ASM_PFX(m16Start)
75 ;------------------------------------------------------------------------------
76 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
77 ; by user code. It will be shadowed to somewhere in memory below 1MB.
78 ;------------------------------------------------------------------------------
81 ; The order of saved registers on the stack matches the order they appears
82 ; in IA32_REGS structure. This facilitates wrapper function to extract them
83 ; into that structure.
90 pushfw ; pushfd actually
91 cli ; disable interrupts
96 pushaw ; pushad actually
97 DB 66h, 0bah ; mov edx, imm32
99 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
101 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
102 cli ; disable interrupts
105 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
109 out 92h, al ; deactivate A20M#
111 xor ax, ax ; xor eax, eax
112 mov eax, ss ; mov ax, ss
114 lea bp, [esp + IA32_REGS.size]
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 [esi - IA32_REGS.size + IA32_REGS._ESP], bp
121 mov ebx, [esi - IA32_REGS.size + 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 [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
141 DD _ToUserCode - ASM_PFX(m16Start)
147 DW GdtEnd - _NullSegDesc - 1
151 ;------------------------------------------------------------------------------
152 ; _ToUserCode() takes control in real mode before passing control to user code.
153 ; It will be shadowed to somewhere in memory below 1MB.
154 ;------------------------------------------------------------------------------
157 mov ss, ecx ; set new segment selectors
162 mov cr0, eax ; real mode starts at next instruction
163 ; which (per SDM) *must* be a far JMP.
168 mov ss, esi ; set up 16-bit stack segment
169 xchg sp, bx ; set up 16-bit stack pointer
171 ; mov bp, [esp + sizeof(IA32_REGS)
173 mov ebp, [esp + IA32_REGS.size] ; BackFromUserCode address from stack
175 ; mov cs:[bp + (_BackFromUserCode.SavedSs - _BackFromUserCode)], dx
176 mov [cs:esi + (_BackFromUserCode.SavedSs - _BackFromUserCode)], edx
178 ; mov cs:[bp + (_BackFromUserCode.SavedEsp - _BackFromUserCode)], ebx
179 DB 2eh, 66h, 89h, 9eh
180 DW _BackFromUserCode.SavedEsp - _BackFromUserCode
182 ; lidt cs:[bp + (_16Idtr - _BackFromUserCode)]
183 DB 2eh, 66h, 0fh, 01h, 9eh
184 DW _16Idtr - _BackFromUserCode
186 popaw ; popad actually
193 DB 66h ; Use 32-bit addressing for "retf" below
194 retf ; transfer control to user code
203 DB 8fh ; 16-bit segment, 4GB limit
210 DB 8fh ; 16-bit segment, 4GB limit
214 ;------------------------------------------------------------------------------
215 ; IA32_REGISTER_SET *
217 ; InternalAsmThunk16 (
218 ; IN IA32_REGISTER_SET *RegisterSet,
219 ; IN OUT VOID *Transition
221 ;------------------------------------------------------------------------------
222 global ASM_PFX(InternalAsmThunk16)
223 ASM_PFX(InternalAsmThunk16):
232 mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter
233 movzx edx, word [esi + IA32_REGS._SS]
234 mov edi, [esi + IA32_REGS._ESP]
235 add edi, - (IA32_REGS.size + 4) ; reserve stack space
236 mov ebx, edi ; ebx <- stack offset
237 imul eax, edx, 16 ; eax <- edx * 16
238 push IA32_REGS.size / 4
239 add edi, eax ; edi <- linear address of 16-bit stack
241 rep movsd ; copy RegSet
242 mov eax, [esp + 40] ; eax <- address of transition code
243 mov esi, edx ; esi <- 16-bit stack segment
244 lea edx, [eax + (_BackFromUserCode.SavedCr0 - ASM_PFX(m16Start))]
248 lea ecx, [ecx + (_BackFromUserCode - ASM_PFX(m16Start))]
250 stosd ; [edi] <- return address of user code
251 add eax, _ToUserCode.RealAddr + 4 - _BackFromUserCode
252 mov [edx + (_ToUserCode.RealAddr - _BackFromUserCode.SavedCr0)], eax
253 sgdt [edx + (SavedGdt - _BackFromUserCode.SavedCr0)]
254 sidt [esp + 36] ; save IDT stack in argument space
256 mov [edx], eax ; save CR0 in _BackFromUserCode.SavedCr0
257 and eax, 7ffffffeh ; clear PE, PG bits
259 mov [edx + (_BackFromUserCode.SavedCr4 - _BackFromUserCode.SavedCr0)], ebp
260 and ebp, ~30h ; clear PAE, PSE bits
262 pop ecx ; ecx <- selector for data segments
263 lgdt [edx + (_16Gdtr - _BackFromUserCode.SavedCr0)]
264 pushfd ; Save df/if indeed
265 call dword far [edx + (_EntryPoint - _BackFromUserCode.SavedCr0)]
267 lidt [esp + 36] ; restore protected mode IDTR
268 lea eax, [ebp - IA32_REGS.size] ; eax <- the address of IA32_REGS