1 | ; $Id: bs3-mode-EnteredMode.asm 98103 2023-01-17 14:15:46Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; BS3Kit - Bs3EnteredMode
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2007-2023 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 | %if TMPL_BITS == 16
|
---|
40 | BS3_EXTERN_DATA16 g_uBs3CpuDetected
|
---|
41 | %endif
|
---|
42 | BS3_EXTERN_DATA16 g_bBs3CurrentMode
|
---|
43 | TMPL_BEGIN_TEXT
|
---|
44 |
|
---|
45 | ;;
|
---|
46 | ; @cproto BS3_DECL(void) Bs3EnteredMode(void);
|
---|
47 | ;
|
---|
48 | ; @uses Nothing.
|
---|
49 | ;
|
---|
50 | ; @remarks ASSUMES we're in ring-0 when not in some kind of real mode.
|
---|
51 | ;
|
---|
52 | BS3_PROC_BEGIN_MODE Bs3EnteredMode, BS3_PBC_NEAR ; won't need this outside the switchers, so always near.
|
---|
53 | push xBP
|
---|
54 | mov xBP, xSP
|
---|
55 | push xAX
|
---|
56 | push xCX
|
---|
57 | push xDX
|
---|
58 | TONLY16 push xBX
|
---|
59 | %if BS3_MODE_IS_64BIT_CODE(TMPL_MODE)
|
---|
60 | push r8
|
---|
61 | push r9
|
---|
62 | %endif
|
---|
63 |
|
---|
64 | ;
|
---|
65 | ; Load stack selector (not always necessary) and sometimes CS too.
|
---|
66 | ;
|
---|
67 | %if BS3_MODE_IS_RM_SYS(TMPL_MODE)
|
---|
68 | xor ax, ax
|
---|
69 | %elif BS3_MODE_IS_V86(TMPL_MODE)
|
---|
70 | extern v86_versions_of_Bs3EnteredMode_should_not_be_dragged_into_the_link
|
---|
71 | call v86_versions_of_Bs3EnteredMode_should_not_be_dragged_into_the_link
|
---|
72 | %elif BS3_MODE_IS_16BIT_CODE(TMPL_MODE)
|
---|
73 | jmp BS3_SEL_R0_CS16:.reloaded_cs
|
---|
74 | .reloaded_cs:
|
---|
75 | mov ax, BS3_SEL_R0_SS16
|
---|
76 | %elif BS3_MODE_IS_32BIT_CODE(TMPL_MODE)
|
---|
77 | mov ax, BS3_SEL_R0_SS32
|
---|
78 | %elif BS3_MODE_IS_64BIT_CODE(TMPL_MODE)
|
---|
79 | mov ax, BS3_SEL_R0_DS64
|
---|
80 | %else
|
---|
81 | %error "TMPL_MODE"
|
---|
82 | %endif
|
---|
83 | mov ss, ax
|
---|
84 |
|
---|
85 | ;
|
---|
86 | ; Load selector appropriate for accessing BS3SYSTEM16 data.
|
---|
87 | ;
|
---|
88 | %if BS3_MODE_IS_16BIT_CODE(TMPL_MODE)
|
---|
89 | mov ax, BS3_SEL_SYSTEM16
|
---|
90 | %else
|
---|
91 | mov ax, RT_CONCAT(BS3_SEL_R0_DS,TMPL_BITS)
|
---|
92 | %endif
|
---|
93 | mov ds, ax
|
---|
94 |
|
---|
95 | ;
|
---|
96 | ; Load the appropritate IDT or IVT.
|
---|
97 | ; Always 64-bit in long mode, otherwise according to TMPL_BITS.
|
---|
98 | ;
|
---|
99 | %if BS3_MODE_IS_RM_SYS(TMPL_MODE)
|
---|
100 | BS3_EXTERN_SYSTEM16 Bs3Lidt_Ivt
|
---|
101 | TMPL_BEGIN_TEXT
|
---|
102 | lidt [Bs3Lidt_Ivt]
|
---|
103 |
|
---|
104 | %elif BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
|
---|
105 | BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt16
|
---|
106 | TMPL_BEGIN_TEXT
|
---|
107 | lidt [Bs3Lidt_Idt16 TMPL_WRT_SYSTEM16_OR_FLAT]
|
---|
108 |
|
---|
109 | %elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
|
---|
110 | BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt32
|
---|
111 | TMPL_BEGIN_TEXT
|
---|
112 | lidt [Bs3Lidt_Idt32 TMPL_WRT_SYSTEM16_OR_FLAT]
|
---|
113 |
|
---|
114 | %elif BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
|
---|
115 | BS3_EXTERN_SYSTEM16 Bs3Lidt_Idt64
|
---|
116 | TMPL_BEGIN_TEXT
|
---|
117 | lidt [Bs3Lidt_Idt64 TMPL_WRT_SYSTEM16_OR_FLAT]
|
---|
118 | %else
|
---|
119 | %error "TMPL_MODE"
|
---|
120 | %endif
|
---|
121 |
|
---|
122 | %if !BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
123 | ;
|
---|
124 | ; Load the appropriate task selector.
|
---|
125 | ; Always 64-bit in long mode, otherwise according to TMPL_BITS.
|
---|
126 | ;
|
---|
127 | %if BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
|
---|
128 | BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss64
|
---|
129 | TMPL_BEGIN_TEXT
|
---|
130 | and byte [5 + Bs3Gdte_Tss64 TMPL_WRT_SYSTEM16_OR_FLAT], ~X86_SEL_TYPE_SYS_TSS_BUSY_MASK
|
---|
131 | mov ax, BS3_SEL_TSS64
|
---|
132 |
|
---|
133 | %elif BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
|
---|
134 | BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss16
|
---|
135 | BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss16DoubleFault
|
---|
136 | TMPL_BEGIN_TEXT
|
---|
137 | and byte [5 + Bs3Gdte_Tss16 TMPL_WRT_SYSTEM16_OR_FLAT], ~X86_SEL_TYPE_SYS_TSS_BUSY_MASK
|
---|
138 | and byte [5 + Bs3Gdte_Tss16DoubleFault TMPL_WRT_SYSTEM16_OR_FLAT], ~X86_SEL_TYPE_SYS_TSS_BUSY_MASK
|
---|
139 | mov ax, BS3_SEL_TSS16
|
---|
140 |
|
---|
141 | %elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
|
---|
142 | BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss32
|
---|
143 | BS3_EXTERN_SYSTEM16 Bs3Gdte_Tss32DoubleFault
|
---|
144 | BS3_EXTERN_SYSTEM16 Bs3Tss32
|
---|
145 | BS3_EXTERN_SYSTEM16 Bs3Tss32DoubleFault
|
---|
146 | TMPL_BEGIN_TEXT
|
---|
147 | and byte [5 + Bs3Gdte_Tss32 TMPL_WRT_SYSTEM16_OR_FLAT], ~X86_SEL_TYPE_SYS_TSS_BUSY_MASK
|
---|
148 | and byte [5 + Bs3Gdte_Tss32DoubleFault TMPL_WRT_SYSTEM16_OR_FLAT], ~X86_SEL_TYPE_SYS_TSS_BUSY_MASK
|
---|
149 | mov eax, cr3
|
---|
150 | mov [X86TSS32.cr3 + Bs3Tss32 TMPL_WRT_SYSTEM16_OR_FLAT], eax
|
---|
151 | mov [X86TSS32.cr3 + Bs3Tss32DoubleFault TMPL_WRT_SYSTEM16_OR_FLAT], eax
|
---|
152 | mov ax, BS3_SEL_TSS32
|
---|
153 | %else
|
---|
154 | %error "TMPL_BITS"
|
---|
155 | %endif
|
---|
156 | ltr ax
|
---|
157 | %endif ; !TMPL_CMN_R86
|
---|
158 |
|
---|
159 | %if !BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
160 | ;
|
---|
161 | ; Load the LDT.
|
---|
162 | ;
|
---|
163 | mov ax, BS3_SEL_LDT
|
---|
164 | lldt ax
|
---|
165 | %endif
|
---|
166 |
|
---|
167 | ;
|
---|
168 | ; Load ds and es; clear fs and gs.
|
---|
169 | ;
|
---|
170 | %if BS3_MODE_IS_RM_OR_V86(TMPL_MODE)
|
---|
171 | mov ax, BS3_SEL_DATA16
|
---|
172 | %else
|
---|
173 | mov ax, RT_CONCAT(BS3_SEL_R0_DS,TMPL_BITS)
|
---|
174 | %endif
|
---|
175 | mov ds, ax
|
---|
176 | mov es, ax
|
---|
177 |
|
---|
178 | %if TMPL_BITS == 16
|
---|
179 | ; For restoring after Bs3Trap* calls below.
|
---|
180 | push ax
|
---|
181 | push ax
|
---|
182 |
|
---|
183 | cmp byte [BS3_DATA16_WRT(g_uBs3CpuDetected)], BS3CPU_80286
|
---|
184 | jbe .skip_fs_gs
|
---|
185 | %endif
|
---|
186 | xor ax, ax
|
---|
187 | mov fs, ax
|
---|
188 | mov gs, ax
|
---|
189 | .skip_fs_gs:
|
---|
190 |
|
---|
191 | ;
|
---|
192 | ; Set global indicating CPU mode.
|
---|
193 | ;
|
---|
194 | mov byte [BS3_DATA16_WRT(g_bBs3CurrentMode)], TMPL_MODE
|
---|
195 |
|
---|
196 | ;
|
---|
197 | ; Install system call handler.
|
---|
198 | ; Always 64-bit in long mode, otherwise according to TMPL_BITS.
|
---|
199 | ;
|
---|
200 | %if BS3_MODE_IS_RM_SYS(TMPL_MODE)
|
---|
201 | extern _Bs3TrapSystemCallHandler_rm
|
---|
202 | mov word [ss: BS3_TRAP_SYSCALL*4], _Bs3TrapSystemCallHandler_rm wrt CGROUP16
|
---|
203 | mov word [ss: BS3_TRAP_SYSCALL*4 + 2], CGROUP16
|
---|
204 |
|
---|
205 | %elif BS3_MODE_IS_16BIT_SYS(TMPL_MODE)
|
---|
206 | BS3_EXTERN_CMN Bs3Trap16SetGate
|
---|
207 | extern TMPL_NM(Bs3TrapSystemCallHandler)
|
---|
208 | BS3_BEGIN_TEXT16
|
---|
209 | TMPL_BEGIN_TEXT
|
---|
210 | push 0 ; cParams
|
---|
211 | push TMPL_NM(Bs3TrapSystemCallHandler) wrt CGROUP16
|
---|
212 | push BS3_SEL_R0_CS16
|
---|
213 | push 3 ; DPL
|
---|
214 | push X86_SEL_TYPE_SYS_286_INT_GATE
|
---|
215 | push BS3_TRAP_SYSCALL
|
---|
216 | BS3_CALL Bs3Trap16SetGate,6
|
---|
217 | add xSP, xCB * 6
|
---|
218 |
|
---|
219 | %elif BS3_MODE_IS_32BIT_SYS(TMPL_MODE)
|
---|
220 | BS3_EXTERN_CMN Bs3Trap32SetGate
|
---|
221 | extern TMPL_NM(Bs3TrapSystemCallHandler)
|
---|
222 | TMPL_BEGIN_TEXT
|
---|
223 | push 0 ; cParams
|
---|
224 | push dword TMPL_NM(Bs3TrapSystemCallHandler) wrt FLAT
|
---|
225 | push BS3_SEL_R0_CS32
|
---|
226 | push 3 ; DPL
|
---|
227 | push X86_SEL_TYPE_SYS_386_INT_GATE
|
---|
228 | push BS3_TRAP_SYSCALL
|
---|
229 | BS3_CALL Bs3Trap32SetGate,6
|
---|
230 | add xSP, xCB * 6
|
---|
231 |
|
---|
232 | %elif BS3_MODE_IS_64BIT_SYS(TMPL_MODE)
|
---|
233 | BS3_EXTERN_CMN Bs3Trap64SetGate
|
---|
234 | extern _Bs3TrapSystemCallHandler_lm64
|
---|
235 | TMPL_BEGIN_TEXT
|
---|
236 | push 0 ; bIst
|
---|
237 | %if BS3_MODE_IS_64BIT_CODE(TMPL_MODE)
|
---|
238 | push _Bs3TrapSystemCallHandler_lm64 wrt FLAT
|
---|
239 | %else
|
---|
240 | push dword 0 ; upper offset
|
---|
241 | push dword _Bs3TrapSystemCallHandler_lm64 wrt FLAT
|
---|
242 | %endif
|
---|
243 | push BS3_SEL_R0_CS64
|
---|
244 | push 3 ; DPL
|
---|
245 | push AMD64_SEL_TYPE_SYS_INT_GATE
|
---|
246 | push BS3_TRAP_SYSCALL
|
---|
247 | BS3_CALL Bs3Trap64SetGate,6
|
---|
248 | add xSP, xCB * 5 + 8
|
---|
249 | %else
|
---|
250 | %error "TMPL_BITS"
|
---|
251 | %endif
|
---|
252 |
|
---|
253 | %if TMPL_BITS == 16
|
---|
254 | ; Restoring ds and es after the above calls.
|
---|
255 | pop es
|
---|
256 | pop ds
|
---|
257 | %endif
|
---|
258 |
|
---|
259 | ;
|
---|
260 | ; Epilogue.
|
---|
261 | ;
|
---|
262 | %if TMPL_BITS == 64
|
---|
263 | pop r9
|
---|
264 | pop r8
|
---|
265 | %endif
|
---|
266 | TONLY16 pop xBX
|
---|
267 | pop xDX
|
---|
268 | pop xCX
|
---|
269 | pop xAX
|
---|
270 | %ifdef BS3_STRICT
|
---|
271 | cmp xBP, xSP
|
---|
272 | je .return_stack_ok
|
---|
273 | int3
|
---|
274 | .return_stack_ok:
|
---|
275 | %endif
|
---|
276 | pop xBP
|
---|
277 | ret
|
---|
278 | BS3_PROC_END_MODE Bs3EnteredMode
|
---|
279 |
|
---|