2 ; Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
3 ; This program and the accompanying materials
4 ; are licensed and made available under the terms and conditions of the BSD License
5 ; which accompanies this distribution. The full text of the license may be found at
6 ; http://opensource.org/licenses/bsd-license.php.
8 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 ; This is the assembly code to transition from long mode to compatibility mode to execute 32-bit code and then
19 ; transit back to long mode.
21 ;-------------------------------------------------------------------------------
23 ;----------------------------------------------------------------------------
24 ; Procedure: AsmExecute32BitCode
31 ; AsmExecute32BitCode (
35 ; IN IA32_DESCRIPTOR *InternalGdtr
39 ; Description: A thunk function to execute 32-bit code in long mode.
41 ;----------------------------------------------------------------------------
42 AsmExecute32BitCode PROC
44 ; save IFLAG and disable it
50 ; save orignal GDTR and CS
63 ; Save general purpose register and rflag register
78 ; Prepare the CS and return address for the transition from 32-bit to 64-bit mode
80 mov rax, 10h ; load long mode selector
82 mov r9, OFFSET ReloadCS ;Assume the ReloadCS is under 4G
86 ; Save parameters for 32-bit function call
93 ; save the 32-bit function entry and the return address into stack which will be
94 ; retrieve in compatibility mode.
96 mov rax, OFFSET ReturnBack ;Assume the ReloadCS is under 4G
107 ; Change to Compatible Segment
109 mov rcx, 08h ; load compatible mode selector
111 mov rdx, OFFSET Compatible ; assume address < 4G
117 ; reload DS/ES/SS to make sure they are correct referred to current GDT
136 ; Now we are in protected mode
138 ; Call 32-bit function. Assume the function entry address and parameter value is less than 4G
140 pop rax ; Here is the function entry
142 ; Now the parameter is at the bottom of the stack, then call in to IA32 function.
146 mov ebx, eax ; save return status
147 pop rcx ; drop param1
148 pop rcx ; drop param2
164 ; Set EFER.LME to re-enable ia32-e
176 ; Now we are in compatible mode
184 ; Now we're in Long Mode
187 ; Restore C register and eax hold the return status from 32-bit function.
188 ; Note: Do not touch rax from now which hold the return value from IA32 function
190 mov eax, ebx ; put return status to EAX
197 ; Switch to orignal GDT and CS. here rsp is pointer to the orignal GDT descriptor.
201 ; drop GDT descriptor in stack
205 ; switch to orignal CS and GDTR
208 shl r9, 32 ; rcx[32..47] <- Cs
215 ; Reload original DS/ES/SS
228 AsmExecute32BitCode ENDP