1 | ; $Id: bs3-mode-SwitchToPP16.asm 60686 2016-04-25 12:51:41Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToPP16
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2016 Oracle Corporation
|
---|
8 | ;
|
---|
9 | ; This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | ; available from http://www.virtualbox.org. This file is free software;
|
---|
11 | ; you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | ; General Public License (GPL) as published by the Free Software
|
---|
13 | ; Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | ; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | ; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | ;
|
---|
17 | ; The contents of this file may alternatively be used under the terms
|
---|
18 | ; of the Common Development and Distribution License Version 1.0
|
---|
19 | ; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | ; VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | ; CDDL are applicable instead of those of the GPL.
|
---|
22 | ;
|
---|
23 | ; You may elect to license modified versions of this file under the
|
---|
24 | ; terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | ;
|
---|
26 |
|
---|
27 | %include "bs3kit-template-header.mac"
|
---|
28 |
|
---|
29 | %ifndef TMPL_PP16
|
---|
30 | extern NAME(Bs3EnteredMode_pp16)
|
---|
31 | %ifdef TMPL_PP32
|
---|
32 | BS3_EXTERN_CMN Bs3SwitchTo16Bit
|
---|
33 | %else
|
---|
34 | %endif
|
---|
35 | %endif
|
---|
36 |
|
---|
37 | ;;
|
---|
38 | ; Switch to 16-bit paged protected mode from any other mode.
|
---|
39 | ;
|
---|
40 | ; @cproto BS3_DECL(void) Bs3SwitchToPP16(void);
|
---|
41 | ;
|
---|
42 | ; @uses Nothing (except high 32-bit register parts).
|
---|
43 | ;
|
---|
44 | ; @remarks Obviously returns to 16-bit mode, even if the caller was
|
---|
45 | ; in 32-bit or 64-bit mode.
|
---|
46 | ;
|
---|
47 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
48 | ;
|
---|
49 | %if TMPL_BITS == 16
|
---|
50 | BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToPP16_Safe), function , 0
|
---|
51 | %endif
|
---|
52 | BS3_PROC_BEGIN_MODE Bs3SwitchToPP16, BS3_PBC_NEAR
|
---|
53 | %ifdef TMPL_PP16
|
---|
54 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
55 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
56 | push ax
|
---|
57 | mov ax, BS3_SEL_R0_DS16
|
---|
58 | mov ds, ax
|
---|
59 | mov es, ax
|
---|
60 | pop ax
|
---|
61 | ret
|
---|
62 |
|
---|
63 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
64 | ;
|
---|
65 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
66 | ;
|
---|
67 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
68 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
69 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPP16)
|
---|
70 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToPP16)
|
---|
71 |
|
---|
72 | %else
|
---|
73 |
|
---|
74 | ;
|
---|
75 | ; Switch to 16-bit text segment and prepare for returning in 16-bit mode.
|
---|
76 | ;
|
---|
77 | %if TMPL_BITS != 16
|
---|
78 | shl xPRE [xSP + xCB], TMPL_BITS - 16 ; Adjust the return address.
|
---|
79 | add xSP, xCB - 2
|
---|
80 |
|
---|
81 | ; Must be in 16-bit segment when calling Bs3SwitchToRM and Bs3SwitchTo16Bit.
|
---|
82 | jmp .sixteen_bit_segment
|
---|
83 | BS3_BEGIN_TEXT16
|
---|
84 | BS3_SET_BITS TMPL_BITS
|
---|
85 | .sixteen_bit_segment:
|
---|
86 | %endif
|
---|
87 |
|
---|
88 | %ifdef TMPL_PP32
|
---|
89 | ;
|
---|
90 | ; No need to go to real-mode here, we use the same CR3 and stuff.
|
---|
91 | ; Just switch to 32-bit mode and call the Bs3EnteredMode routine to
|
---|
92 | ; load the right descriptor tables.
|
---|
93 | ;
|
---|
94 | call Bs3SwitchTo16Bit
|
---|
95 | BS3_SET_BITS 16
|
---|
96 | call NAME(Bs3EnteredMode_pp16)
|
---|
97 | ret
|
---|
98 | %else
|
---|
99 |
|
---|
100 | ;
|
---|
101 | ; Switch to real mode.
|
---|
102 | ;
|
---|
103 | extern TMPL_NM(Bs3SwitchToRM)
|
---|
104 | call TMPL_NM(Bs3SwitchToRM)
|
---|
105 | BS3_SET_BITS 16
|
---|
106 |
|
---|
107 | push eax
|
---|
108 | push ecx
|
---|
109 | pushfd
|
---|
110 | %ifdef BS3_STRICT
|
---|
111 | mov ax, ds
|
---|
112 | cmp ax, BS3_ADDR_BS3DATA16 >> 4
|
---|
113 | je .real_mode_ds_ok
|
---|
114 | hlt
|
---|
115 | .real_mode_ds_ok:
|
---|
116 | %endif
|
---|
117 |
|
---|
118 | ;
|
---|
119 | ; Get the page directory (returned in eax).
|
---|
120 | ; Will lazy init page tables (in 16-bit prot mode).
|
---|
121 | ;
|
---|
122 | extern NAME(Bs3PagingGetRootForPP16_rm)
|
---|
123 | call NAME(Bs3PagingGetRootForPP16_rm)
|
---|
124 |
|
---|
125 | cli
|
---|
126 | mov cr3, eax
|
---|
127 |
|
---|
128 | ;
|
---|
129 | ; Make sure PAE is really off and that PSE is enabled when supported.
|
---|
130 | ;
|
---|
131 | BS3_EXTERN_DATA16 g_uBs3CpuDetected
|
---|
132 | BS3_BEGIN_TEXT16
|
---|
133 | test byte [1 + BS3_DATA16_WRT(g_uBs3CpuDetected)], (BS3CPU_F_CPUID >> 8)
|
---|
134 | jz .cr4_is_fine
|
---|
135 | mov eax, cr4
|
---|
136 | mov ecx, eax
|
---|
137 | and eax, ~(X86_CR4_PAE | X86_CR4_PSE)
|
---|
138 | test byte [1 + BS3_DATA16_WRT(g_uBs3CpuDetected)], (BS3CPU_F_PSE >> 8)
|
---|
139 | jz .no_pse
|
---|
140 | or eax, X86_CR4_PSE
|
---|
141 | .no_pse:
|
---|
142 | cmp eax, ecx
|
---|
143 | je .cr4_is_fine
|
---|
144 | mov cr4, eax
|
---|
145 | .cr4_is_fine:
|
---|
146 |
|
---|
147 | ;
|
---|
148 | ; Load the GDT and enable PP16.
|
---|
149 | ;
|
---|
150 | BS3_EXTERN_SYSTEM16 Bs3LgdtDef_Gdt
|
---|
151 | BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
|
---|
152 | BS3_BEGIN_TEXT16
|
---|
153 | mov ax, BS3SYSTEM16
|
---|
154 | mov ds, ax
|
---|
155 | lgdt [Bs3LgdtDef_Gdt] ; Will only load 24-bit base!
|
---|
156 |
|
---|
157 | mov eax, cr0
|
---|
158 | or eax, X86_CR0_PE | X86_CR0_PG
|
---|
159 | mov cr0, eax
|
---|
160 | jmp BS3_SEL_R0_CS16:.reload_cs_and_stuff
|
---|
161 | .reload_cs_and_stuff:
|
---|
162 |
|
---|
163 | ;
|
---|
164 | ; Convert the (now) real mode stack to 16-bit.
|
---|
165 | ;
|
---|
166 | mov ax, .stack_fix_return
|
---|
167 | extern NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
|
---|
168 | jmp NAME(Bs3ConvertRMStackToP16UsingCxReturnToAx_c16)
|
---|
169 | .stack_fix_return:
|
---|
170 |
|
---|
171 | ;
|
---|
172 | ; Call rountine for doing mode specific setups.
|
---|
173 | ;
|
---|
174 | call NAME(Bs3EnteredMode_pp16)
|
---|
175 |
|
---|
176 | ;
|
---|
177 | ; Load full 32-bit GDT base address from 32-bit segment.
|
---|
178 | ;
|
---|
179 | push ds
|
---|
180 | mov ax, BS3_SEL_SYSTEM16
|
---|
181 | mov ds, ax
|
---|
182 | jmp dword BS3_SEL_R0_CS32:.load_full_gdt_base wrt FLAT
|
---|
183 | .load_full_gdt_base:
|
---|
184 | BS3_SET_BITS 32
|
---|
185 | lgdt [Bs3Lgdt_Gdt wrt BS3SYSTEM16]
|
---|
186 | jmp BS3_SEL_R0_CS16:.back_to_16bit
|
---|
187 | .back_to_16bit:
|
---|
188 | BS3_SET_BITS 16
|
---|
189 | pop ds
|
---|
190 |
|
---|
191 | popfd
|
---|
192 | pop ecx
|
---|
193 | pop eax
|
---|
194 | ret
|
---|
195 |
|
---|
196 | %endif ; !TMPL_PP32
|
---|
197 | %if TMPL_BITS != 16
|
---|
198 | TMPL_BEGIN_TEXT
|
---|
199 | %endif
|
---|
200 | %endif
|
---|
201 | BS3_PROC_END_MODE Bs3SwitchToPP16
|
---|
202 |
|
---|
203 |
|
---|
204 | %if TMPL_BITS == 16
|
---|
205 | ;;
|
---|
206 | ; Custom far stub.
|
---|
207 | BS3_BEGIN_TEXT16_FARSTUBS
|
---|
208 | BS3_PROC_BEGIN_MODE Bs3SwitchToPP16, BS3_PBC_FAR
|
---|
209 | inc bp
|
---|
210 | push bp
|
---|
211 | mov bp, sp
|
---|
212 |
|
---|
213 | ; Call the real thing.
|
---|
214 | call TMPL_NM(Bs3SwitchToPP16)
|
---|
215 |
|
---|
216 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
217 | ; Jmp to common code for the tedious conversion.
|
---|
218 | BS3_EXTERN_CMN Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
|
---|
219 | jmp Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn
|
---|
220 | %else
|
---|
221 | pop bp
|
---|
222 | dec bp
|
---|
223 | retf
|
---|
224 | %endif
|
---|
225 | BS3_PROC_END_MODE Bs3SwitchToPP16
|
---|
226 |
|
---|
227 | %else
|
---|
228 | ;;
|
---|
229 | ; Safe far return to non-BS3TEXT16 code.
|
---|
230 | BS3_EXTERN_CMN Bs3SwitchHlpConvFlatRetToRetfProtMode
|
---|
231 | BS3_BEGIN_TEXT16
|
---|
232 | BS3_SET_BITS TMPL_BITS
|
---|
233 | BS3_PROC_BEGIN_MODE Bs3SwitchToPP16_Safe, BS3_PBC_NEAR
|
---|
234 | call Bs3SwitchHlpConvFlatRetToRetfProtMode ; Special internal function. Uses nothing, but modifies the stack.
|
---|
235 | call TMPL_NM(Bs3SwitchToPP16)
|
---|
236 | BS3_SET_BITS 16
|
---|
237 | retf
|
---|
238 | BS3_PROC_END_MODE Bs3SwitchToPP16_Safe
|
---|
239 | %endif
|
---|
240 |
|
---|