1 | ; $Id: bs3-mode-SwitchToLM32.asm 82968 2020-02-04 10:35:17Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3SwitchToLM32
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2020 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 long mode from any other mode.
|
---|
32 | ;
|
---|
33 | ; @cproto BS3_DECL(void) Bs3SwitchToLM32(void);
|
---|
34 | ;
|
---|
35 | ; @uses Nothing (except possibly high 32-bit and/or upper 64-bit register parts).
|
---|
36 | ;
|
---|
37 | ; @remarks There are no IDT or TSS differences between LM16, LM32 and LM64 (unlike
|
---|
38 | ; PE16 & PE32, PP16 & PP32, and PAE16 & PAE32).
|
---|
39 | ;
|
---|
40 | ; @remarks Obviously returns to 32-bit mode, even if the caller was in 16-bit
|
---|
41 | ; or 64-bit mode. It doesn't not preserve the callers ring, but
|
---|
42 | ; instead changes to ring-0.
|
---|
43 | ;
|
---|
44 | ; @remarks Does not require 20h of parameter scratch space in 64-bit mode.
|
---|
45 | ;
|
---|
46 | BS3_GLOBAL_NAME_EX TMPL_NM(Bs3SwitchToLM32_Safe), function, 0
|
---|
47 | BS3_PROC_BEGIN_MODE Bs3SwitchToLM32, BS3_PBC_NEAR
|
---|
48 | %ifdef TMPL_LM32
|
---|
49 | ret
|
---|
50 |
|
---|
51 | %elifdef TMPL_CMN_LM
|
---|
52 | ;
|
---|
53 | ; Already in long mode, just switch to 32-bit.
|
---|
54 | ;
|
---|
55 | extern BS3_CMN_NM(Bs3SwitchTo32Bit)
|
---|
56 | jmp BS3_CMN_NM(Bs3SwitchTo32Bit)
|
---|
57 |
|
---|
58 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
59 | ;
|
---|
60 | ; V8086 - Switch to 16-bit ring-0 and call worker for that mode.
|
---|
61 | ;
|
---|
62 | extern BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
63 | call BS3_CMN_NM(Bs3SwitchToRing0)
|
---|
64 | extern %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToLM32)
|
---|
65 | jmp %[BS3_MODE_R0_NM_ %+ TMPL_MODE](Bs3SwitchToLM32)
|
---|
66 |
|
---|
67 | %else
|
---|
68 | %if TMPL_BITS == 16
|
---|
69 | push word 0 ; save space for extending the return value.
|
---|
70 | %endif
|
---|
71 |
|
---|
72 | ;
|
---|
73 | ; Switch to 32-bit protected mode (for identify mapped pages).
|
---|
74 | ;
|
---|
75 | extern TMPL_NM(Bs3SwitchToPE32)
|
---|
76 | call TMPL_NM(Bs3SwitchToPE32)
|
---|
77 | BS3_SET_BITS 32
|
---|
78 | %if TMPL_BITS == 16
|
---|
79 | jmp .thirty_two_bit_segment
|
---|
80 | BS3_BEGIN_TEXT32
|
---|
81 | BS3_GLOBAL_LOCAL_LABEL .thirty_two_bit_segment
|
---|
82 | %endif
|
---|
83 |
|
---|
84 | push eax
|
---|
85 | push ecx
|
---|
86 | push edx
|
---|
87 | pushfd
|
---|
88 |
|
---|
89 | ;
|
---|
90 | ; Make sure both PAE and PSE are enabled (requires pentium pro).
|
---|
91 | ;
|
---|
92 | mov eax, cr4
|
---|
93 | mov ecx, eax
|
---|
94 | or eax, X86_CR4_PAE | X86_CR4_PSE
|
---|
95 | cmp eax, ecx
|
---|
96 | je .cr4_is_fine
|
---|
97 | mov cr4, eax
|
---|
98 | .cr4_is_fine:
|
---|
99 |
|
---|
100 | ;
|
---|
101 | ; Get the page directory (returned in eax).
|
---|
102 | ; Will lazy init page tables.
|
---|
103 | ;
|
---|
104 | extern NAME(Bs3PagingGetRootForLM64_pe32)
|
---|
105 | call NAME(Bs3PagingGetRootForLM64_pe32)
|
---|
106 |
|
---|
107 | cli
|
---|
108 | mov cr3, eax
|
---|
109 |
|
---|
110 | ;
|
---|
111 | ; Enable long mode in EFER.
|
---|
112 | ;
|
---|
113 | mov ecx, MSR_K6_EFER
|
---|
114 | rdmsr
|
---|
115 | or eax, MSR_K6_EFER_LME
|
---|
116 | wrmsr
|
---|
117 |
|
---|
118 | ;
|
---|
119 | ; Enable paging and thereby activating LM64.
|
---|
120 | ;
|
---|
121 | BS3_EXTERN_SYSTEM16 Bs3Lgdt_Gdt
|
---|
122 | BS3_BEGIN_TEXT32
|
---|
123 | mov eax, cr0
|
---|
124 | or eax, X86_CR0_PG
|
---|
125 | mov cr0, eax
|
---|
126 | jmp .in_lm32
|
---|
127 | .in_lm32:
|
---|
128 |
|
---|
129 | ;
|
---|
130 | ; Call rountine for doing mode specific setups.
|
---|
131 | ;
|
---|
132 | extern NAME(Bs3EnteredMode_lm32)
|
---|
133 | call NAME(Bs3EnteredMode_lm32)
|
---|
134 |
|
---|
135 | ;
|
---|
136 | ; Load full 64-bit GDT base address from 64-bit segment.
|
---|
137 | ;
|
---|
138 | jmp dword BS3_SEL_R0_CS64:.load_full_gdt_base wrt FLAT
|
---|
139 | .load_full_gdt_base:
|
---|
140 | BS3_SET_BITS 64
|
---|
141 | lgdt [Bs3Lgdt_Gdt wrt FLAT]
|
---|
142 | push BS3_SEL_R0_CS32
|
---|
143 | push .back_to_32bit wrt FLAT
|
---|
144 | o64 retf
|
---|
145 | .back_to_32bit:
|
---|
146 | BS3_SET_BITS 32
|
---|
147 |
|
---|
148 | ;
|
---|
149 | ; Restore ecx, eax and flags (IF).
|
---|
150 | ;
|
---|
151 | %if TMPL_BITS == 16
|
---|
152 | movzx eax, word [esp + 16 + 2] ; Load return address.
|
---|
153 | add eax, BS3_ADDR_BS3TEXT16 ; Convert it to a flat address.
|
---|
154 | mov [esp + 16], eax ; Store it in the place right for 32-bit returns.
|
---|
155 | %endif
|
---|
156 | popfd
|
---|
157 | pop edx
|
---|
158 | pop ecx
|
---|
159 | pop eax
|
---|
160 | ret
|
---|
161 |
|
---|
162 | %if TMPL_BITS != 32
|
---|
163 | TMPL_BEGIN_TEXT
|
---|
164 | %endif
|
---|
165 | %endif
|
---|
166 | BS3_PROC_END_MODE Bs3SwitchToLM32
|
---|
167 |
|
---|
168 |
|
---|
169 | %if TMPL_BITS == 16
|
---|
170 | ;;
|
---|
171 | ; Custom far stub.
|
---|
172 | BS3_BEGIN_TEXT16_FARSTUBS
|
---|
173 | BS3_PROC_BEGIN_MODE Bs3SwitchToLM32, BS3_PBC_FAR
|
---|
174 | inc bp
|
---|
175 | push bp
|
---|
176 | mov bp, sp
|
---|
177 |
|
---|
178 | ; Call the real thing.
|
---|
179 | call TMPL_NM(Bs3SwitchToLM32)
|
---|
180 | BS3_SET_BITS 32
|
---|
181 |
|
---|
182 | ; Jmp to common code for the tedious conversion.
|
---|
183 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
184 | extern _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
185 | jmp _Bs3SwitchHlpConvRealModeRetfPopBpDecBpAndReturn_c32
|
---|
186 | %else
|
---|
187 | extern _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
188 | jmp _Bs3SwitchHlpConvProtModeRetfPopBpDecBpAndReturn_c32
|
---|
189 | %endif
|
---|
190 | BS3_SET_BITS 16
|
---|
191 | BS3_PROC_END_MODE Bs3SwitchToLM32
|
---|
192 | %endif
|
---|
193 |
|
---|