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