1 | ; $Id: bs3-mode-SwitchToPE16.asm 106061 2024-09-16 14:03:52Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToPE16
|
---|
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 16-bit unpaged protected mode from any other mode.
|
---|
42 | ;
|
---|
43 | ; @cproto BS3_DECL(void) Bs3SwitchToPE16(void);
|
---|
44 | ;
|
---|
45 | ; @uses Nothing (except high 32-bit register parts).
|
---|
46 | ;
|
---|
47 | ; @remarks Obviously returns to 16-bit mode, even if the caller was
|
---|
48 | ; in 32-bit or 64-bit mode.
|
---|
49 | ;
|
---|
50 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
51 | ;
|
---|
52 | %if TMPL_BITS == 16
|
---|
53 | BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToPE16_Safe), function , 0
|
---|
54 | %endif
|
---|
55 | BS3_PROC_BEGIN_MODE Bs3SwitchToPE16, BS3_PBC_NEAR
|
---|
56 | %ifdef TMPL_PE16
|
---|
57 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
58 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
59 | push ax
|
---|
60 | mov ax, BS3_SEL_R0_DS16
|
---|
61 | mov ds, ax
|
---|
62 | mov es, ax
|
---|
63 | pop ax
|
---|
64 | ret
|
---|
65 |
|
---|
66 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
67 | ;
|
---|
68 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
69 | ;
|
---|
70 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
71 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
72 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPE16)
|
---|
73 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPE16)
|
---|
74 |
|
---|
75 | %else
|
---|
76 | ;
|
---|
77 | ; Switch to 16-bit mode and prepare for returning in 16-bit mode.
|
---|
78 | ;
|
---|
79 | %if TMPL_BITS != 16
|
---|
80 | shl xPRE [xSP], TMPL_BITS - 16 ; Adjust the return address.
|
---|
81 | add xSP, xCB - 2
|
---|
82 |
|
---|
83 | ; Must be in 16-bit segment when calling Bs3SwitchTo16Bit.
|
---|
84 | jmp .sixteen_bit_segment
|
---|
85 | BS3_BEGIN_TEXT16
|
---|
86 | BS3_SET_BITS TMPL_BITS
|
---|
87 | BS3_GLOBAL_LOCAL_LABEL .sixteen_bit_segment
|
---|
88 | %endif
|
---|
89 |
|
---|
90 | ;
|
---|
91 | ; Switch to real mode.
|
---|
92 | ;
|
---|
93 | extern TMPL_NM(Bs3SwitchToRM)
|
---|
94 | call TMPL_NM(Bs3SwitchToRM)
|
---|
95 | BS3_SET_BITS 16
|
---|
96 |
|
---|
97 | push ax
|
---|
98 | push cx
|
---|
99 | pushf
|
---|
100 | cli
|
---|
101 |
|
---|
102 | ;
|
---|
103 | ; Load the GDT and enable PE16.
|
---|
104 | ;
|
---|
105 | BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
|
---|
106 | BS3_EXTERN_SYSTEM16 Bs3LgdtDef_Gdt
|
---|
107 | BS3_BEGIN_TEXT16
|
---|
108 | mov ax, BS3SYSTEM16
|
---|
109 | mov ds, ax
|
---|
110 | lgdt [Bs3LgdtDef_Gdt] ; Will only load 24-bit base!
|
---|
111 |
|
---|
112 | smsw ax
|
---|
113 | or ax, X86_CR0_PE
|
---|
114 | lmsw ax
|
---|
115 |
|
---|
116 | ;
|
---|
117 | ; Convert from real mode stack to protected mode stack.
|
---|
118 | ;
|
---|
119 | mov ax, .p16_stack
|
---|
120 | extern NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
|
---|
121 | jmp NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
|
---|
122 | .p16_stack:
|
---|
123 |
|
---|
124 | ;
|
---|
125 | ; Call routine for doing mode specific setups.
|
---|
126 | ;
|
---|
127 | extern NAME(Bs3EnteredMode_pe16)
|
---|
128 | call NAME(Bs3EnteredMode_pe16)
|
---|
129 |
|
---|
130 | ;
|
---|
131 | ; Load full 32-bit GDT base address from 32-bit segment, if 386+ CPU.
|
---|
132 | ;
|
---|
133 | BS3_EXTERN_DATA16 g_uBs3CpuDetected
|
---|
134 | BS3_BEGIN_TEXT16
|
---|
135 | cmp byte [g_uBs3CpuDetected], BS3CPU_80386
|
---|
136 | jb .old_cpu_skip_32bit_lgdt
|
---|
137 | push ds
|
---|
138 | mov ax, BS3_SEL_SYSTEM16
|
---|
139 | mov ds, ax
|
---|
140 | jmp dword BS3_SEL_R0_CS32:.load_full_gdt_base wrt FLAT
|
---|
141 | .load_full_gdt_base:
|
---|
142 | BS3_SET_BITS 32
|
---|
143 | lgdt [Bs3Lgdt_Gdt wrt BS3SYSTEM16]
|
---|
144 | jmp BS3_SEL_R0_CS16:.back_to_16bit
|
---|
145 | .back_to_16bit:
|
---|
146 | BS3_SET_BITS 16
|
---|
147 | pop ds
|
---|
148 | .old_cpu_skip_32bit_lgdt:
|
---|
149 |
|
---|
150 | popf
|
---|
151 | pop cx
|
---|
152 | pop ax
|
---|
153 | ret
|
---|
154 |
|
---|
155 | %if TMPL_BITS != 16
|
---|
156 | TMPL_BEGIN_TEXT
|
---|
157 | %endif
|
---|
158 | %endif
|
---|
159 | BS3_PROC_END_MODE Bs3SwitchToPE16
|
---|
160 |
|
---|
161 |
|
---|
162 | %if TMPL_BITS == 16
|
---|
163 | ;;
|
---|
164 | ; Custom far stub.
|
---|
165 | BS3_BEGIN_TEXT16_FARSTUBS
|
---|
166 | BS3_PROC_BEGIN_MODE Bs3SwitchToPE16, BS3_PBC_FAR
|
---|
167 | inc bp
|
---|
168 | push bp
|
---|
169 | mov bp, sp
|
---|
170 |
|
---|
171 | ; Call the real thing.
|
---|
172 | call TMPL_NM(Bs3SwitchToPE16)
|
---|
173 |
|
---|
174 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
175 | ; Jmp to common code for the tedious conversion.
|
---|
176 | BS3_EXTERN_CMN Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
|
---|
177 | jmp Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
|
---|
178 | %else
|
---|
179 | pop bp
|
---|
180 | dec bp
|
---|
181 | retf
|
---|
182 | %endif
|
---|
183 | BS3_PROC_END_MODE Bs3SwitchToPE16
|
---|
184 |
|
---|
185 | %else
|
---|
186 | ;;
|
---|
187 | ; Safe far return to non-BS3TEXT16 code.
|
---|
188 | BS3_EXTERN_CMN Bs3SwitchHlpConvFlatRetToRetfProtMode
|
---|
189 | BS3_BEGIN_TEXT16
|
---|
190 | BS3_SET_BITS TMPL_BITS
|
---|
191 | BS3_PROC_BEGIN_MODE Bs3SwitchToPE16_Safe, BS3_PBC_NEAR
|
---|
192 | call Bs3SwitchHlpConvFlatRetToRetfProtMode ; Special internal function. Uses nothing, but modifies the stack.
|
---|
193 | call TMPL_NM(Bs3SwitchToPE16)
|
---|
194 | BS3_SET_BITS 16
|
---|
195 | retf
|
---|
196 | BS3_PROC_END_MODE Bs3SwitchToPE16_Safe
|
---|
197 | %endif
|
---|
198 |
|
---|