2 #include "BaseLibInternals.h"
4 ;------------------------------------------------------------------------------
6 ; Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
7 ; SPDX-License-Identifier: BSD-2-Clause-Patent
17 ;------------------------------------------------------------------------------
19 global ASM_PFX(m16Size)
20 global ASM_PFX(mThunk16Attr)
21 global ASM_PFX(m16Gdt)
22 global ASM_PFX(m16GdtrBase)
23 global ASM_PFX(mTransition)
24 global ASM_PFX(m16Start)
53 ; These are global constant to convey information to C code.
55 ASM_PFX(m16Size) DW ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start)
56 ASM_PFX(mThunk16Attr) DW _BackFromUserCode.ThunkAttrEnd - 4 - ASM_PFX(m16Start)
57 ASM_PFX(m16Gdt) DW _NullSegDesc - ASM_PFX(m16Start)
58 ASM_PFX(m16GdtrBase) DW _16GdtrBase - ASM_PFX(m16Start)
59 ASM_PFX(mTransition) DW _EntryPoint - ASM_PFX(m16Start)
69 ;------------------------------------------------------------------------------
70 ; _BackFromUserCode() takes control in real mode after 'retf' has been executed
71 ; by user code. It will be shadowed to somewhere in memory below 1MB.
72 ;------------------------------------------------------------------------------
75 ; The order of saved registers on the stack matches the order they appears
76 ; in IA32_REGS structure. This facilitates wrapper function to extract them
77 ; into that structure.
83 ; Note: We can't use o32 on the next instruction because of a bug
84 ; in NASM 2.09.04 through 2.10rc1.
86 call dword .Base ; push eip
89 cli ; disable interrupts
95 mov edx, strict dword 0
97 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
101 cli ; disable interrupts
104 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
108 out 92h, al ; deactivate A20M#
112 lea ebp, [esp + IA32_REGS.size]
113 mov [bp - IA32_REGS.size + IA32_REGS._ESP], ebp
114 mov bx, [bp - IA32_REGS.size + IA32_REGS._EIP]
115 shl eax, 4 ; shl eax, 4
116 add ebp, eax ; add ebp, eax
117 mov eax, strict dword 0
120 o32 lgdt [cs:bx + (SavedGdt - .Base)]
121 mov eax, strict dword 0
124 mov ax, strict word 0
127 mov esp, strict dword 0
129 o32 retf ; return to protected mode
132 DD _ToUserCode - ASM_PFX(m16Start)
138 DW GdtEnd - _NullSegDesc - 1
142 ;------------------------------------------------------------------------------
143 ; _ToUserCode() takes control in real mode before passing control to user code.
144 ; It will be shadowed to somewhere in memory below 1MB.
145 ;------------------------------------------------------------------------------
149 mov ss, cx ; set new segment selectors
154 mov cr0, eax ; real mode starts at next instruction
155 ; which (per SDM) *must* be a far JMP.
159 mov ss, si ; set up 16-bit stack segment
160 xchg esp, ebx ; set up 16-bit stack pointer
161 mov bp, [esp + IA32_REGS.size]
162 mov [cs:bp + (_BackFromUserCode.SavedSsEnd - 2 - _BackFromUserCode)], dx
163 mov [cs:bp + (_BackFromUserCode.SavedEspEnd - 4 - _BackFromUserCode)], ebx
164 lidt [cs:bp + (_16Idtr - _BackFromUserCode)]
173 o32 retf ; transfer control to user code
182 DB 8fh ; 16-bit segment, 4GB limit
189 DB 8fh ; 16-bit segment, 4GB limit
193 ;------------------------------------------------------------------------------
194 ; IA32_REGISTER_SET *
196 ; InternalAsmThunk16 (
197 ; IN IA32_REGISTER_SET *RegisterSet,
198 ; IN OUT VOID *Transition
200 ;------------------------------------------------------------------------------
201 global ASM_PFX(InternalAsmThunk16)
202 ASM_PFX(InternalAsmThunk16):
212 mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter
213 movzx edx, word [esi + IA32_REGS._SS]
214 mov edi, [esi + IA32_REGS._ESP]
215 add edi, - (IA32_REGS.size + 4) ; reserve stack space
216 mov ebx, edi ; ebx <- stack offset
217 imul eax, edx, 16 ; eax <- edx * 16
218 push IA32_REGS.size / 4
219 add edi, eax ; edi <- linear address of 16-bit stack
221 rep movsd ; copy RegSet
222 mov eax, [esp + 40] ; eax <- address of transition code
223 mov esi, edx ; esi <- 16-bit stack segment
224 lea edx, [eax + (_BackFromUserCode.SavedCr0End - ASM_PFX(m16Start))]
228 lea ecx, [ecx + (_BackFromUserCode - ASM_PFX(m16Start))]
230 stosd ; [edi] <- return address of user code
231 add eax, _ToUserCode.RealAddrEnd - _BackFromUserCode
232 mov [edx + (_ToUserCode.RealAddrEnd - 4 - _BackFromUserCode.SavedCr0End)], eax
233 sgdt [edx + (SavedGdt - _BackFromUserCode.SavedCr0End)]
234 sidt [esp + 36] ; save IDT stack in argument space
236 mov [edx - 4], eax ; save CR0 in _BackFromUserCode.SavedCr0End - 4
237 and eax, 7ffffffeh ; clear PE, PG bits
239 mov [edx + (_BackFromUserCode.SavedCr4End - 4 - _BackFromUserCode.SavedCr0End)], ebp
240 and ebp, ~30h ; clear PAE, PSE bits
242 pop ecx ; ecx <- selector for data segments
243 lgdt [edx + (_16Gdtr - _BackFromUserCode.SavedCr0End)]
244 pushfd ; Save df/if indeed
245 call dword far [edx + (_EntryPoint - _BackFromUserCode.SavedCr0End)]
247 lidt [esp + 36] ; restore protected mode IDTR
248 lea eax, [ebp - IA32_REGS.size] ; eax <- the address of IA32_REGS