VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-SwitchToPP16.asm@ 106560

Last change on this file since 106560 was 106061, checked in by vboxsync, 4 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1; $Id: bs3-mode-SwitchToPP16.asm 106061 2024-09-16 14:03:52Z vboxsync $
2;; @file
3; BS3Kit - Bs3SwitchToPP16
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;*********************************************************************************************************************************
38;* Header Files *
39;*********************************************************************************************************************************
40%include "bs3kit-template-header.mac"
41
42;*********************************************************************************************************************************
43;* External Symbols *
44;*********************************************************************************************************************************
45%ifndef TMPL_PP16
46BS3_BEGIN_TEXT16
47extern NAME(Bs3EnteredMode_pp16)
48 %ifdef TMPL_PP32
49 BS3_EXTERN_CMN Bs3SwitchTo16Bit
50 %endif
51TMPL_BEGIN_TEXT
52%endif
53
54
55;;
56; Switch to 16-bit paged protected mode from any other mode.
57;
58; @cproto BS3_DECL(void) Bs3SwitchToPP16(void);
59;
60; @uses Nothing (except high 32-bit register parts).
61;
62; @remarks Obviously returns to 16-bit mode, even if the caller was
63; in 32-bit or 64-bit mode.
64;
65; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
66;
67%if TMPL_BITS == 16
68BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToPP16_Safe), function , 0
69%endif
70BS3_PROC_BEGIN_MODE Bs3SwitchToPP16, BS3_PBC_NEAR
71%ifdef TMPL_PP16
72 extern BS3_CMN_NM(Bs3SwitchToRing0)
73 call BS3_CMN_NM(Bs3SwitchToRing0)
74 push ax
75 mov ax, BS3_SEL_R0_DS16
76 mov ds, ax
77 mov es, ax
78 pop ax
79 ret
80
81%elif BS3_MODE_IS_V86(TMPL_MODE)
82 ;
83 ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
84 ;
85 extern BS3_CMN_NM(Bs3SwitchToRing0)
86 call BS3_CMN_NM(Bs3SwitchToRing0)
87 extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPP16)
88 jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPP16)
89
90%else
91
92 ;
93 ; Switch to 16-bit text segment and prepare for returning in 16-bit mode.
94 ;
95 %if TMPL_BITS != 16
96 shl xPRE [xSP], TMPL_BITS - 16 ; Adjust the return address.
97 add xSP, xCB - 2
98
99 ; Must be in 16-bit segment when calling Bs3SwitchToRM and Bs3SwitchTo16Bit.
100 jmp .sixteen_bit_segment
101BS3_BEGIN_TEXT16
102 BS3_SET_BITS TMPL_BITS
103BS3_GLOBAL_LOCAL_LABEL .sixteen_bit_segment
104 %endif
105
106 %ifdef TMPL_PP32
107 ;
108 ; No need to go to real-mode here, we use the same CR3 and stuff.
109 ; Just switch to 32-bit mode and call the Bs3EnteredMode routine to
110 ; load the right descriptor tables.
111 ;
112 call Bs3SwitchTo16Bit
113 BS3_SET_BITS 16
114 call NAME(Bs3EnteredMode_pp16)
115 ret
116 %else
117
118 ;
119 ; Switch to real mode.
120 ;
121 extern TMPL_NM(Bs3SwitchToRM)
122 call TMPL_NM(Bs3SwitchToRM)
123 BS3_SET_BITS 16
124
125 push eax
126 push ecx
127 pushfd
128%ifdef BS3_STRICT
129 mov ax, ds
130 cmp ax, BS3_ADDR_BS3DATA16 >> 4
131 je .real_mode_ds_ok
132 hlt
133.real_mode_ds_ok:
134%endif
135
136 ;
137 ; Get the page directory (returned in eax).
138 ; Will lazy init page tables (in 16-bit prot mode).
139 ;
140 extern NAME(Bs3PagingGetRootForPP16_rm)
141 call NAME(Bs3PagingGetRootForPP16_rm)
142
143 cli
144 mov cr3, eax
145
146 ;
147 ; Make sure PAE is really off and that PSE is enabled when supported.
148 ;
149BS3_EXTERN_DATA16 g_uBs3CpuDetected
150BS3_BEGIN_TEXT16
151 test byte [1 + BS3_DATA16_WRT(g_uBs3CpuDetected)], (BS3CPU_F_CPUID >> 8)
152 jz .cr4_is_fine
153 mov eax, cr4
154 mov ecx, eax
155 and eax, ~(X86_CR4_PAE | X86_CR4_PSE)
156 test byte [1 + BS3_DATA16_WRT(g_uBs3CpuDetected)], (BS3CPU_F_PSE >> 8)
157 jz .no_pse
158 or eax, X86_CR4_PSE
159.no_pse:
160 cmp eax, ecx
161 je .cr4_is_fine
162 mov cr4, eax
163.cr4_is_fine:
164
165 ;
166 ; Load the GDT and enable PP16.
167 ;
168BS3_EXTERN_SYSTEM16 Bs3LgdtDef_Gdt
169BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
170BS3_BEGIN_TEXT16
171 mov ax, BS3SYSTEM16
172 mov ds, ax
173 lgdt [Bs3LgdtDef_Gdt] ; Will only load 24-bit base!
174
175 mov eax, cr0
176 or eax, X86_CR0_PE | X86_CR0_PG
177 mov cr0, eax
178 jmp BS3_SEL_R0_CS16:.reload_cs_and_stuff
179.reload_cs_and_stuff:
180
181 ;
182 ; Convert the (now) real mode stack to 16-bit.
183 ;
184 mov ax, .stack_fix_return
185 extern NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
186 jmp NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
187.stack_fix_return:
188
189 ;
190 ; Call rountine for doing mode specific setups.
191 ;
192 call NAME(Bs3EnteredMode_pp16)
193
194 ;
195 ; Load full 32-bit GDT base address from 32-bit segment.
196 ;
197 push ds
198 mov ax, BS3_SEL_SYSTEM16
199 mov ds, ax
200 jmp dword BS3_SEL_R0_CS32:.load_full_gdt_base wrt FLAT
201.load_full_gdt_base:
202 BS3_SET_BITS 32
203 lgdt [Bs3Lgdt_Gdt wrt BS3SYSTEM16]
204 jmp BS3_SEL_R0_CS16:.back_to_16bit
205.back_to_16bit:
206 BS3_SET_BITS 16
207 pop ds
208
209 popfd
210 pop ecx
211 pop eax
212 ret
213
214 %endif ; !TMPL_PP32
215 %if TMPL_BITS != 16
216TMPL_BEGIN_TEXT
217 %endif
218%endif
219BS3_PROC_END_MODE Bs3SwitchToPP16
220
221
222%if TMPL_BITS == 16
223;;
224; Custom far stub.
225BS3_BEGIN_TEXT16_FARSTUBS
226BS3_PROC_BEGIN_MODE Bs3SwitchToPP16, BS3_PBC_FAR
227 inc bp
228 push bp
229 mov bp, sp
230
231 ; Call the real thing.
232 call TMPL_NM(Bs3SwitchToPP16)
233
234 %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
235 ; Jmp to common code for the tedious conversion.
236 BS3_EXTERN_CMN Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
237 jmp Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
238 %else
239 pop bp
240 dec bp
241 retf
242 %endif
243BS3_PROC_END_MODE Bs3SwitchToPP16
244
245%else
246;;
247; Safe far return to non-BS3TEXT16 code.
248BS3_EXTERN_CMN Bs3SwitchHlpConvFlatRetToRetfProtMode
249BS3_BEGIN_TEXT16
250BS3_SET_BITS TMPL_BITS
251BS3_PROC_BEGIN_MODE Bs3SwitchToPP16_Safe, BS3_PBC_NEAR
252 call Bs3SwitchHlpConvFlatRetToRetfProtMode ; Special internal function. Uses nothing, but modifies the stack.
253 call TMPL_NM(Bs3SwitchToPP16)
254 BS3_SET_BITS 16
255 retf
256BS3_PROC_END_MODE Bs3SwitchToPP16_Safe
257%endif
258
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette