1 | ; $Id: bs3-mode-SwitchToLM32.asm 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToLM32
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox base platform packages, as
|
---|
10 | ; available from https://www.virtualbox.org.
|
---|
11 | ;
|
---|
12 | ; This program is free software; you can redistribute it and/or
|
---|
13 | ; modify it under the terms of the GNU General Public License
|
---|
14 | ; as published by the Free Software Foundation, in version 3 of the
|
---|
15 | ; License.
|
---|
16 | ;
|
---|
17 | ; This program is distributed in the hope that it will be useful, but
|
---|
18 | ; WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | ; General Public License for more details.
|
---|
21 | ;
|
---|
22 | ; You should have received a copy of the GNU General Public License
|
---|
23 | ; along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | ;
|
---|
25 | ; The contents of this file may alternatively be used under the terms
|
---|
26 | ; of the Common Development and Distribution License Version 1.0
|
---|
27 | ; (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | ; in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | ; CDDL are applicable instead of those of the GPL.
|
---|
30 | ;
|
---|
31 | ; You may elect to license modified versions of this file under the
|
---|
32 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | ;
|
---|
34 | ; SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | ;
|
---|
36 |
|
---|
37 | %include "bs3kit-template-header.mac"
|
---|
38 |
|
---|
39 |
|
---|
40 | ;;
|
---|
41 | ; Switch to 32-bit long mode from any other mode.
|
---|
42 | ;
|
---|
43 | ; @cproto BS3_DECL(void) Bs3SwitchToLM32(void);
|
---|
44 | ;
|
---|
45 | ; @uses Nothing (except possibly high 32-bit and/or upper 64-bit register parts).
|
---|
46 | ;
|
---|
47 | ; @remarks There are no IDT or TSS differences between LM16, LM32 and LM64 (unlike
|
---|
48 | ; PE16 & PE32, PP16 & PP32, and PAE16 & PAE32).
|
---|
49 | ;
|
---|
50 | ; @remarks Obviously returns to 32-bit mode, even if the caller was in 16-bit
|
---|
51 | ; or 64-bit mode. It doesn't not preserve the callers ring, but
|
---|
52 | ; instead changes to ring-0.
|
---|
53 | ;
|
---|
54 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
55 | ;
|
---|
56 | BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToLM32_Safe), function, 0
|
---|
57 | BS3_PROC_BEGIN_MODE Bs3SwitchToLM32, BS3_PBC_NEAR
|
---|
58 | %ifdef TMPL_LM32
|
---|
59 | ret
|
---|
60 |
|
---|
61 | %elifdef TMPL_CMN_LM
|
---|
62 | ;
|
---|
63 | ; Already in long mode, just switch to 32-bit.
|
---|
64 | ;
|
---|
65 | extern BS3_CMN_NM(Bs3SwitchTo32Bit)
|
---|
66 | jmp BS3_CMN_NM(Bs3SwitchTo32Bit)
|
---|
67 |
|
---|
68 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
69 | ;
|
---|
70 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
71 | ;
|
---|
72 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
73 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
74 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToLM32)
|
---|
75 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToLM32)
|
---|
76 |
|
---|
77 | %else
|
---|
78 | %if TMPL_BITS == 16
|
---|
79 | push word 0 ; save space for extending the return value.
|
---|
80 | %endif
|
---|
81 |
|
---|
82 | ;
|
---|
83 | ; Switch to 32-bit protected mode (for identify mapped pages).
|
---|
84 | ;
|
---|
85 | extern TMPL_NM(Bs3SwitchToPE32)
|
---|
86 | call TMPL_NM(Bs3SwitchToPE32)
|
---|
87 | BS3_SET_BITS 32
|
---|
88 | %if TMPL_BITS == 16
|
---|
89 | jmp .thirty_two_bit_segment
|
---|
90 | BS3_BEGIN_TEXT32
|
---|
91 | BS3_GLOBAL_LOCAL_LABEL .thirty_two_bit_segment
|
---|
92 | %endif
|
---|
93 |
|
---|
94 | push eax
|
---|
95 | push ecx
|
---|
96 | push edx
|
---|
97 | pushfd
|
---|
98 |
|
---|
99 | ;
|
---|
100 | ; Make sure both PAE and PSE are enabled (requires pentium pro).
|
---|
101 | ;
|
---|
102 | mov eax, cr4
|
---|
103 | mov ecx, eax
|
---|
104 | or eax, X86_CR4_PAE | X86_CR4_PSE
|
---|
105 | cmp eax, ecx
|
---|
106 | je .cr4_is_fine
|
---|
107 | mov cr4, eax
|
---|
108 | .cr4_is_fine:
|
---|
109 |
|
---|
110 | ;
|
---|
111 | ; Get the page directory (returned in eax).
|
---|
112 | ; Will lazy init page tables.
|
---|
113 | ;
|
---|
114 | extern NAME(Bs3PagingGetRootForLM64_pe32)
|
---|
115 | call NAME(Bs3PagingGetRootForLM64_pe32)
|
---|
116 |
|
---|
117 | cli
|
---|
118 | mov cr3, eax
|
---|
119 |
|
---|
120 | ;
|
---|
121 | ; Enable long mode in EFER.
|
---|
122 | ;
|
---|
123 | mov ecx, MSR_K6_EFER
|
---|
124 | rdmsr
|
---|
125 | or eax, MSR_K6_EFER_LME
|
---|
126 | wrmsr
|
---|
127 |
|
---|
128 | ;
|
---|
129 | ; Enable paging and thereby activating LM64.
|
---|
130 | ;
|
---|
131 | BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
|
---|
132 | BS3_BEGIN_TEXT32
|
---|
133 | mov eax, cr0
|
---|
134 | or eax, X86_CR0_PG
|
---|
135 | mov cr0, eax
|
---|
136 | jmp .in_lm32
|
---|
137 | .in_lm32:
|
---|
138 |
|
---|
139 | ;
|
---|
140 | ; Call rountine for doing mode specific setups.
|
---|
141 | ;
|
---|
142 | extern NAME(Bs3EnteredMode_lm32)
|
---|
143 | call NAME(Bs3EnteredMode_lm32)
|
---|
144 |
|
---|
145 | ;
|
---|
146 | ; Load full 64-bit GDT base address from 64-bit segment.
|
---|
147 | ;
|
---|
148 | jmp dword BS3_SEL_R0_CS64:.load_full_gdt_base wrt FLAT
|
---|
149 | .load_full_gdt_base:
|
---|
150 | BS3_SET_BITS 64
|
---|
151 | lgdt [Bs3Lgdt_Gdt wrt FLAT]
|
---|
152 | push BS3_SEL_R0_CS32
|
---|
153 | push .back_to_32bit wrt FLAT
|
---|
154 | o64 retf
|
---|
155 | .back_to_32bit:
|
---|
156 | BS3_SET_BITS 32
|
---|
157 |
|
---|
158 | ;
|
---|
159 | ; Restore ecx, eax and flags (IF).
|
---|
160 | ;
|
---|
161 | %if TMPL_BITS == 16
|
---|
162 | movzx eax, word [esp + 16 + 2] ; Load return address.
|
---|
163 | add eax, BS3_ADDR_BS3TEXT16 ; Convert it to a flat address.
|
---|
164 | mov [esp + 16], eax ; Store it in the place right for 32-bit returns.
|
---|
165 | %endif
|
---|
166 | popfd
|
---|
167 | pop edx
|
---|
168 | pop ecx
|
---|
169 | pop eax
|
---|
170 | ret
|
---|
171 |
|
---|
172 | %if TMPL_BITS != 32
|
---|
173 | TMPL_BEGIN_TEXT
|
---|
174 | %endif
|
---|
175 | %endif
|
---|
176 | BS3_PROC_END_MODE Bs3SwitchToLM32
|
---|
177 |
|
---|
178 |
|
---|
179 | %if TMPL_BITS == 16
|
---|
180 | ;;
|
---|
181 | ; Custom far stub.
|
---|
182 | BS3_BEGIN_TEXT16_FARSTUBS
|
---|
183 | BS3_PROC_BEGIN_MODE Bs3SwitchToLM32, BS3_PBC_FAR
|
---|
184 | inc bp
|
---|
185 | push bp
|
---|
186 | mov bp, sp
|
---|
187 |
|
---|
188 | ; Call the real thing.
|
---|
189 | call TMPL_NM(Bs3SwitchToLM32)
|
---|
190 | BS3_SET_BITS 32
|
---|
191 |
|
---|
192 | ; Jmp to common code for the tedious conversion.
|
---|
193 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
194 | extern _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
195 | jmp _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
196 | %else
|
---|
197 | extern _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
198 | jmp _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
199 | %endif
|
---|
200 | BS3_SET_BITS 16
|
---|
201 | BS3_PROC_END_MODE Bs3SwitchToLM32
|
---|
202 | %endif
|
---|
203 |
|
---|