VirtualBox

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

Last change on this file since 66289 was 64694, checked in by vboxsync, 8 years ago

bs3kit: Added Bs3SwitchTo32BitAndCallC and fixed a few problems switching from PE32 to other mode and back again.

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