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