1 | ; $Id: CPUMAllA.asm 8155 2008-04-18 15:16:47Z vboxsync $
|
---|
2 | ;; @file
|
---|
3 | ; CPUM - Guest Context Assembly Routines.
|
---|
4 | ;
|
---|
5 |
|
---|
6 | ;
|
---|
7 | ; Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | ; Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | ; Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | ; additional information or have any questions.
|
---|
20 | ;
|
---|
21 |
|
---|
22 | ;*******************************************************************************
|
---|
23 | ;* Header Files *
|
---|
24 | ;*******************************************************************************
|
---|
25 | %include "VBox/asmdefs.mac"
|
---|
26 | %include "VBox/vm.mac"
|
---|
27 | %include "VBox/err.mac"
|
---|
28 | %include "VBox/stam.mac"
|
---|
29 | %include "CPUMInternal.mac"
|
---|
30 | %include "VBox/x86.mac"
|
---|
31 | %include "VBox/cpum.mac"
|
---|
32 |
|
---|
33 | %ifdef IN_RING3
|
---|
34 | %error "The jump table doesn't link on leopard."
|
---|
35 | %endif
|
---|
36 |
|
---|
37 | ;
|
---|
38 | ; Enables write protection of Hypervisor memory pages.
|
---|
39 | ; !note! Must be commented out for Trap8 debug handler.
|
---|
40 | ;
|
---|
41 | %define ENABLE_WRITE_PROTECTION 1
|
---|
42 |
|
---|
43 | ;; @def CPUM_REG
|
---|
44 | ; The register which we load the CPUM pointer into.
|
---|
45 | %ifdef RT_ARCH_AMD64
|
---|
46 | %define CPUM_REG rdx
|
---|
47 | %else
|
---|
48 | %define CPUM_REG edx
|
---|
49 | %endif
|
---|
50 |
|
---|
51 | BEGINCODE
|
---|
52 |
|
---|
53 |
|
---|
54 | ;;
|
---|
55 | ; Handles lazy FPU saving and restoring.
|
---|
56 | ;
|
---|
57 | ; This handler will implement lazy fpu (sse/mmx/stuff) saving.
|
---|
58 | ; Two actions may be taken in this handler since the Guest OS may
|
---|
59 | ; be doing lazy fpu switching. So, we'll have to generate those
|
---|
60 | ; traps which the Guest CPU CTX shall have according to the
|
---|
61 | ; its CR0 flags. If no traps for the Guest OS, we'll save the host
|
---|
62 | ; context and restore the guest context.
|
---|
63 | ;
|
---|
64 | ; @returns 0 if caller should continue execution.
|
---|
65 | ; @returns VINF_EM_RAW_GUEST_TRAP if a guest trap should be generated.
|
---|
66 | ; @param pCPUM x86:[esp+4] GCC:rdi MSC:rcx CPUM pointer
|
---|
67 | ;
|
---|
68 | align 16
|
---|
69 | BEGINPROC CPUMHandleLazyFPUAsm
|
---|
70 | ;
|
---|
71 | ; Figure out what to do.
|
---|
72 | ;
|
---|
73 | ; There are two basic actions:
|
---|
74 | ; 1. Save host fpu and restore guest fpu.
|
---|
75 | ; 2. Generate guest trap.
|
---|
76 | ;
|
---|
77 | ; When entering the hypervisor we'll always enable MP (for proper wait
|
---|
78 | ; trapping) and TS (for intercepting all fpu/mmx/sse stuff). The EM flag
|
---|
79 | ; is taken from the guest OS in order to get proper SSE handling.
|
---|
80 | ;
|
---|
81 | ;
|
---|
82 | ; Actions taken depending on the guest CR0 flags:
|
---|
83 | ;
|
---|
84 | ; 3 2 1
|
---|
85 | ; TS | EM | MP | FPUInstr | WAIT :: VMM Action
|
---|
86 | ; ------------------------------------------------------------------------
|
---|
87 | ; 0 | 0 | 0 | Exec | Exec :: Clear TS & MP, Save HC, Load GC.
|
---|
88 | ; 0 | 0 | 1 | Exec | Exec :: Clear TS, Save HC, Load GC.
|
---|
89 | ; 0 | 1 | 0 | #NM | Exec :: Clear TS & MP, Save HC, Load GC;
|
---|
90 | ; 0 | 1 | 1 | #NM | Exec :: Clear TS, Save HC, Load GC.
|
---|
91 | ; 1 | 0 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already cleared.)
|
---|
92 | ; 1 | 0 | 1 | #NM | #NM :: Go to host taking trap there.
|
---|
93 | ; 1 | 1 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already set.)
|
---|
94 | ; 1 | 1 | 1 | #NM | #NM :: Go to host taking trap there.
|
---|
95 |
|
---|
96 | ;
|
---|
97 | ; Before taking any of these actions we're checking if we have already
|
---|
98 | ; loaded the GC FPU. Because if we have, this is an trap for the guest - raw ring-3.
|
---|
99 | ;
|
---|
100 | %ifdef RT_ARCH_AMD64
|
---|
101 | %ifdef RT_OS_WINDOWS
|
---|
102 | mov xDX, rcx
|
---|
103 | %else
|
---|
104 | mov xDX, rdi
|
---|
105 | %endif
|
---|
106 | %else
|
---|
107 | mov xDX, dword [esp + 4]
|
---|
108 | %endif
|
---|
109 | test dword [xDX + CPUM.fUseFlags], CPUM_USED_FPU
|
---|
110 | jz hlfpua_not_loaded
|
---|
111 | jmp hlfpua_to_host
|
---|
112 |
|
---|
113 | ;
|
---|
114 | ; Take action.
|
---|
115 | ;
|
---|
116 | align 16
|
---|
117 | hlfpua_not_loaded:
|
---|
118 | mov eax, [xDX + CPUM.Guest.cr0]
|
---|
119 | and eax, X86_CR0_MP | X86_CR0_EM | X86_CR0_TS
|
---|
120 | %ifdef RT_ARCH_AMD64
|
---|
121 | lea r8, [hlfpuajmp1 wrt rip]
|
---|
122 | jmp qword [rax*4 + r8]
|
---|
123 | %else
|
---|
124 | jmp dword [eax*2 + hlfpuajmp1]
|
---|
125 | %endif
|
---|
126 | align 16
|
---|
127 | ;; jump table using fpu related cr0 flags as index.
|
---|
128 | hlfpuajmp1:
|
---|
129 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
130 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
131 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
132 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
133 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
134 | RTCCPTR_DEF hlfpua_to_host
|
---|
135 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
136 | RTCCPTR_DEF hlfpua_to_host
|
---|
137 | ;; and mask for cr0.
|
---|
138 | hlfpu_afFlags:
|
---|
139 | RTCCPTR_DEF ~(X86_CR0_TS | X86_CR0_MP)
|
---|
140 | RTCCPTR_DEF ~(X86_CR0_TS)
|
---|
141 | RTCCPTR_DEF ~(X86_CR0_TS | X86_CR0_MP)
|
---|
142 | RTCCPTR_DEF ~(X86_CR0_TS)
|
---|
143 | RTCCPTR_DEF ~(X86_CR0_MP)
|
---|
144 | RTCCPTR_DEF 0
|
---|
145 | RTCCPTR_DEF ~(X86_CR0_MP)
|
---|
146 | RTCCPTR_DEF 0
|
---|
147 |
|
---|
148 | ;
|
---|
149 | ; Action - switch FPU context and change cr0 flags.
|
---|
150 | ;
|
---|
151 | align 16
|
---|
152 | hlfpua_switch_fpu_ctx:
|
---|
153 | %ifndef IN_RING3 ; IN_GC or IN_RING0
|
---|
154 | mov xCX, cr0
|
---|
155 | %ifdef RT_ARCH_AMD64
|
---|
156 | lea r8, [hlfpu_afFlags wrt rip]
|
---|
157 | and rcx, [rax*4 + r8] ; calc the new cr0 flags.
|
---|
158 | %else
|
---|
159 | and ecx, [eax*2 + hlfpu_afFlags] ; calc the new cr0 flags.
|
---|
160 | %endif
|
---|
161 | mov xAX, cr0
|
---|
162 | and xAX, ~(X86_CR0_TS | X86_CR0_EM)
|
---|
163 | mov cr0, xAX ; clear flags so we don't trap here.
|
---|
164 | %endif
|
---|
165 | %ifndef RT_ARCH_AMD64
|
---|
166 | test dword [xDX + CPUM.CPUFeatures.edx], X86_CPUID_FEATURE_EDX_FXSR
|
---|
167 | jz short hlfpua_no_fxsave
|
---|
168 | %endif
|
---|
169 |
|
---|
170 | fxsave [xDX + CPUM.Host.fpu]
|
---|
171 | or dword [xDX + CPUM.fUseFlags], (CPUM_USED_FPU | CPUM_USED_FPU_SINCE_REM)
|
---|
172 | fxrstor [xDX + CPUM.Guest.fpu]
|
---|
173 | hlfpua_finished_switch:
|
---|
174 | %ifdef IN_GC
|
---|
175 | mov cr0, xCX ; load the new cr0 flags.
|
---|
176 | %endif
|
---|
177 | ; return continue execution.
|
---|
178 | xor eax, eax
|
---|
179 | ret
|
---|
180 |
|
---|
181 | %ifndef RT_ARCH_AMD64
|
---|
182 | ; legacy support.
|
---|
183 | hlfpua_no_fxsave:
|
---|
184 | fnsave [xDX + CPUM.Host.fpu]
|
---|
185 | or dword [xDX + CPUM.fUseFlags], dword (CPUM_USED_FPU | CPUM_USED_FPU_SINCE_REM) ; yasm / nasm
|
---|
186 | mov eax, [xDX + CPUM.Guest.fpu] ; control word
|
---|
187 | not eax ; 1 means exception ignored (6 LS bits)
|
---|
188 | and eax, byte 03Fh ; 6 LS bits only
|
---|
189 | test eax, [xDX + CPUM.Guest.fpu + 4]; status word
|
---|
190 | jz short hlfpua_no_exceptions_pending
|
---|
191 | ; technically incorrect, but we certainly don't want any exceptions now!!
|
---|
192 | and dword [xDX + CPUM.Guest.fpu + 4], ~03Fh
|
---|
193 | hlfpua_no_exceptions_pending:
|
---|
194 | frstor [xDX + CPUM.Guest.fpu]
|
---|
195 | jmp near hlfpua_finished_switch
|
---|
196 | %endif ; !RT_ARCH_AMD64
|
---|
197 |
|
---|
198 |
|
---|
199 | ;
|
---|
200 | ; Action - Generate Guest trap.
|
---|
201 | ;
|
---|
202 | hlfpua_action_4:
|
---|
203 | hlfpua_to_host:
|
---|
204 | mov eax, VINF_EM_RAW_GUEST_TRAP
|
---|
205 | ret
|
---|
206 | ENDPROC CPUMHandleLazyFPUAsm
|
---|
207 |
|
---|
208 |
|
---|
209 | ;;
|
---|
210 | ; Restores the host's FPU/XMM state
|
---|
211 | ;
|
---|
212 | ; @returns 0
|
---|
213 | ; @param pCPUM x86:[esp+4] GCC:rdi MSC:rcx CPUM pointer
|
---|
214 | ;
|
---|
215 | align 16
|
---|
216 | BEGINPROC CPUMRestoreHostFPUStateAsm
|
---|
217 | %ifdef RT_ARCH_AMD64
|
---|
218 | %ifdef RT_OS_WINDOWS
|
---|
219 | mov xDX, rcx
|
---|
220 | %else
|
---|
221 | mov xDX, rdi
|
---|
222 | %endif
|
---|
223 | %else
|
---|
224 | mov xDX, dword [esp + 4]
|
---|
225 | %endif
|
---|
226 |
|
---|
227 | ; Restore FPU if guest has used it.
|
---|
228 | ; Using fxrstor should ensure that we're not causing unwanted exception on the host.
|
---|
229 | test dword [xDX + CPUM.fUseFlags], CPUM_USED_FPU
|
---|
230 | jz short gth_fpu_no
|
---|
231 |
|
---|
232 | mov xAX, cr0
|
---|
233 | mov xCX, xAX ; save old CR0
|
---|
234 | and xAX, ~(X86_CR0_TS | X86_CR0_EM)
|
---|
235 | mov cr0, xAX
|
---|
236 |
|
---|
237 | fxsave [xDX + CPUM.Guest.fpu]
|
---|
238 | fxrstor [xDX + CPUM.Host.fpu]
|
---|
239 |
|
---|
240 | mov cr0, xCX ; and restore old CR0 again
|
---|
241 | and dword [xDX + CPUM.fUseFlags], ~CPUM_USED_FPU
|
---|
242 | gth_fpu_no:
|
---|
243 | xor eax, eax
|
---|
244 | ret
|
---|
245 | ENDPROC CPUMRestoreHostFPUStateAsm
|
---|
246 |
|
---|