1 | ; $Id: bs3-mode-SwitchToPAE32.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToPAE32
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2024 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 PAE paged protected mode from any other mode.
|
---|
42 | ;
|
---|
43 | ; @cproto BS3_DECL(void) Bs3SwitchToPAE32(void);
|
---|
44 | ;
|
---|
45 | ; @uses Nothing (except high 32-bit register parts), upper part of ESP is
|
---|
46 | ; cleared if caller is in 16-bit mode.
|
---|
47 | ;
|
---|
48 | ; @remarks Obviously returns to 32-bit mode, even if the caller was
|
---|
49 | ; in 16-bit or 64-bit mode. It doesn't not preserve the callers
|
---|
50 | ; ring, but instead changes to ring-0.
|
---|
51 | ;
|
---|
52 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
53 | ;
|
---|
54 | BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToPAE32_Safe), function, 0
|
---|
55 | BS3_PROC_BEGIN_MODE Bs3SwitchToPAE32, BS3_PBC_NEAR
|
---|
56 | %ifdef TMPL_PAE32
|
---|
57 | ret
|
---|
58 |
|
---|
59 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
60 | ;
|
---|
61 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
62 | ;
|
---|
63 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
64 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
65 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPAE32)
|
---|
66 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPAE32)
|
---|
67 |
|
---|
68 | %else
|
---|
69 | ;
|
---|
70 | ; Switch to real mode.
|
---|
71 | ;
|
---|
72 | %if TMPL_BITS != 32
|
---|
73 | %if TMPL_BITS > 32
|
---|
74 | shl xPRE [xSP], 32 ; Adjust the return address from 64-bit to 32-bit.
|
---|
75 | add rsp, xCB - 4
|
---|
76 | %else
|
---|
77 | push word 0 ; Reserve space to expand the return address.
|
---|
78 | %endif
|
---|
79 | %endif
|
---|
80 | %if TMPL_BITS != 16
|
---|
81 | ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
|
---|
82 | jmp .sixteen_bit_segment
|
---|
83 | BS3_BEGIN_TEXT16
|
---|
84 | BS3_SET_BITS TMPL_BITS
|
---|
85 | BS3_GLOBAL_LOCAL_LABEL .sixteen_bit_segment
|
---|
86 | %endif
|
---|
87 |
|
---|
88 | ;
|
---|
89 | ; Switch to real mode.
|
---|
90 | ;
|
---|
91 | extern TMPL_NM(Bs3SwitchToRM)
|
---|
92 | call TMPL_NM(Bs3SwitchToRM)
|
---|
93 | BS3_SET_BITS 16
|
---|
94 |
|
---|
95 | push eax
|
---|
96 | push ecx
|
---|
97 | pushfd
|
---|
98 |
|
---|
99 | ;
|
---|
100 | ; Get the page directory (returned in eax).
|
---|
101 | ; Will lazy init page tables (in 16-bit prot mode).
|
---|
102 | ;
|
---|
103 | extern NAME(Bs3PagingGetRootForPAE32_rm)
|
---|
104 | call NAME(Bs3PagingGetRootForPAE32_rm)
|
---|
105 |
|
---|
106 | cli
|
---|
107 | mov cr3, eax
|
---|
108 |
|
---|
109 | ;
|
---|
110 | ; Make sure PAE, PSE, and VME are enabled (former two require pentium pro, latter 486).
|
---|
111 | ;
|
---|
112 | mov eax, cr4
|
---|
113 | mov ecx, eax
|
---|
114 | or eax, X86_CR4_PAE | X86_CR4_PSE | X86_CR4_VME
|
---|
115 | cmp eax, ecx
|
---|
116 | je .cr4_is_fine
|
---|
117 | mov cr4, eax
|
---|
118 | .cr4_is_fine:
|
---|
119 |
|
---|
120 | ;
|
---|
121 | ; Load the GDT and enable PE32.
|
---|
122 | ;
|
---|
123 | BS3_EXTERN_SYSTEM16 Bs3LgdtDef_Gdt
|
---|
124 | BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
|
---|
125 | BS3_BEGIN_TEXT16
|
---|
126 | mov ax, BS3SYSTEM16
|
---|
127 | mov ds, ax
|
---|
128 | lgdt [Bs3LgdtDef_Gdt] ; Will only load 24-bit base!
|
---|
129 |
|
---|
130 | mov eax, cr0
|
---|
131 | or eax, X86_CR0_PE | X86_CR0_PG
|
---|
132 | mov cr0, eax
|
---|
133 | jmp BS3_SEL_R0_CS32:dword .thirty_two_bit wrt FLAT
|
---|
134 | BS3_BEGIN_TEXT32
|
---|
135 | BS3_GLOBAL_LOCAL_LABEL .thirty_two_bit
|
---|
136 |
|
---|
137 | ;
|
---|
138 | ; Convert the (now) real mode stack pointer to 32-bit flat.
|
---|
139 | ;
|
---|
140 | xor eax, eax
|
---|
141 | mov ax, ss
|
---|
142 | shl eax, 4
|
---|
143 | and esp, 0ffffh
|
---|
144 | add esp, eax
|
---|
145 |
|
---|
146 | mov ax, BS3_SEL_R0_SS32
|
---|
147 | mov ss, ax
|
---|
148 |
|
---|
149 | ;
|
---|
150 | ; Call rountine for doing mode specific setups.
|
---|
151 | ;
|
---|
152 | extern NAME(Bs3EnteredMode_pae32)
|
---|
153 | call NAME(Bs3EnteredMode_pae32)
|
---|
154 |
|
---|
155 | ; Load full 32-bit GDT base address.
|
---|
156 | lgdt [Bs3Lgdt_Gdt wrt FLAT]
|
---|
157 |
|
---|
158 | ;
|
---|
159 | ; Restore ecx, eax and flags (IF).
|
---|
160 | ;
|
---|
161 | %if TMPL_BITS < 32
|
---|
162 | movzx eax, word [esp + 12 + 2] ; Load return address.
|
---|
163 | add eax, BS3_ADDR_BS3TEXT16 ; Convert it to a flat address.
|
---|
164 | mov [esp + 12], eax ; Store it in the place right for 32-bit returns.
|
---|
165 | %endif
|
---|
166 | popfd
|
---|
167 | pop ecx
|
---|
168 | pop eax
|
---|
169 | ret
|
---|
170 |
|
---|
171 | %if TMPL_BITS != 32
|
---|
172 | TMPL_BEGIN_TEXT
|
---|
173 | %endif
|
---|
174 | %endif
|
---|
175 | BS3_PROC_END_MODE Bs3SwitchToPAE32
|
---|
176 |
|
---|
177 |
|
---|
178 | %if TMPL_BITS == 16
|
---|
179 | ;;
|
---|
180 | ; Custom far stub.
|
---|
181 | BS3_BEGIN_TEXT16_FARSTUBS
|
---|
182 | BS3_PROC_BEGIN_MODE Bs3SwitchToPAE32, BS3_PBC_FAR
|
---|
183 | inc bp
|
---|
184 | push bp
|
---|
185 | mov bp, sp
|
---|
186 |
|
---|
187 | ; Call the real thing.
|
---|
188 | call TMPL_NM(Bs3SwitchToPAE32)
|
---|
189 | BS3_SET_BITS 32
|
---|
190 |
|
---|
191 | ; Jmp to common code for the tedious conversion.
|
---|
192 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
193 | extern _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
194 | jmp _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
195 | %else
|
---|
196 | extern _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
197 | jmp _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
198 | %endif
|
---|
199 | BS3_SET_BITS 16
|
---|
200 | BS3_PROC_END_MODE Bs3SwitchToPAE32
|
---|
201 | %endif
|
---|
202 |
|
---|