1 | ; $Id: CPUMAllA.asm 16108 2009-01-21 00:16:27Z 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 | BEGINCODE
|
---|
44 |
|
---|
45 |
|
---|
46 | ;;
|
---|
47 | ; Handles lazy FPU saving and restoring.
|
---|
48 | ;
|
---|
49 | ; This handler will implement lazy fpu (sse/mmx/stuff) saving.
|
---|
50 | ; Two actions may be taken in this handler since the Guest OS may
|
---|
51 | ; be doing lazy fpu switching. So, we'll have to generate those
|
---|
52 | ; traps which the Guest CPU CTX shall have according to the
|
---|
53 | ; its CR0 flags. If no traps for the Guest OS, we'll save the host
|
---|
54 | ; context and restore the guest context.
|
---|
55 | ;
|
---|
56 | ; @returns 0 if caller should continue execution.
|
---|
57 | ; @returns VINF_EM_RAW_GUEST_TRAP if a guest trap should be generated.
|
---|
58 | ; @param pCPUMCPU x86:[esp+4] GCC:rdi MSC:rcx CPUMCPU pointer
|
---|
59 | ;
|
---|
60 | align 16
|
---|
61 | BEGINPROC cpumHandleLazyFPUAsm
|
---|
62 | ;
|
---|
63 | ; Figure out what to do.
|
---|
64 | ;
|
---|
65 | ; There are two basic actions:
|
---|
66 | ; 1. Save host fpu and restore guest fpu.
|
---|
67 | ; 2. Generate guest trap.
|
---|
68 | ;
|
---|
69 | ; When entering the hypervisor we'll always enable MP (for proper wait
|
---|
70 | ; trapping) and TS (for intercepting all fpu/mmx/sse stuff). The EM flag
|
---|
71 | ; is taken from the guest OS in order to get proper SSE handling.
|
---|
72 | ;
|
---|
73 | ;
|
---|
74 | ; Actions taken depending on the guest CR0 flags:
|
---|
75 | ;
|
---|
76 | ; 3 2 1
|
---|
77 | ; TS | EM | MP | FPUInstr | WAIT :: VMM Action
|
---|
78 | ; ------------------------------------------------------------------------
|
---|
79 | ; 0 | 0 | 0 | Exec | Exec :: Clear TS & MP, Save HC, Load GC.
|
---|
80 | ; 0 | 0 | 1 | Exec | Exec :: Clear TS, Save HC, Load GC.
|
---|
81 | ; 0 | 1 | 0 | #NM | Exec :: Clear TS & MP, Save HC, Load GC;
|
---|
82 | ; 0 | 1 | 1 | #NM | Exec :: Clear TS, Save HC, Load GC.
|
---|
83 | ; 1 | 0 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already cleared.)
|
---|
84 | ; 1 | 0 | 1 | #NM | #NM :: Go to host taking trap there.
|
---|
85 | ; 1 | 1 | 0 | #NM | Exec :: Clear MP, Save HC, Load GC. (EM is already set.)
|
---|
86 | ; 1 | 1 | 1 | #NM | #NM :: Go to host taking trap there.
|
---|
87 |
|
---|
88 | ;
|
---|
89 | ; Before taking any of these actions we're checking if we have already
|
---|
90 | ; loaded the GC FPU. Because if we have, this is an trap for the guest - raw ring-3.
|
---|
91 | ;
|
---|
92 | %ifdef RT_ARCH_AMD64
|
---|
93 | %ifdef RT_OS_WINDOWS
|
---|
94 | mov xDX, rcx
|
---|
95 | %else
|
---|
96 | mov xDX, rdi
|
---|
97 | %endif
|
---|
98 | %else
|
---|
99 | mov xDX, dword [esp + 4]
|
---|
100 | %endif
|
---|
101 | test dword [xDX + CPUMCPU.fUseFlags], CPUM_USED_FPU
|
---|
102 | jz hlfpua_not_loaded
|
---|
103 | jmp hlfpua_to_host
|
---|
104 |
|
---|
105 | ;
|
---|
106 | ; Take action.
|
---|
107 | ;
|
---|
108 | align 16
|
---|
109 | hlfpua_not_loaded:
|
---|
110 | mov eax, [xDX + CPUMCPU.Guest.cr0]
|
---|
111 | and eax, X86_CR0_MP | X86_CR0_EM | X86_CR0_TS
|
---|
112 | %ifdef RT_ARCH_AMD64
|
---|
113 | lea r8, [hlfpuajmp1 wrt rip]
|
---|
114 | jmp qword [rax*4 + r8]
|
---|
115 | %else
|
---|
116 | jmp dword [eax*2 + hlfpuajmp1]
|
---|
117 | %endif
|
---|
118 | align 16
|
---|
119 | ;; jump table using fpu related cr0 flags as index.
|
---|
120 | hlfpuajmp1:
|
---|
121 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
122 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
123 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
124 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
125 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
126 | RTCCPTR_DEF hlfpua_to_host
|
---|
127 | RTCCPTR_DEF hlfpua_switch_fpu_ctx
|
---|
128 | RTCCPTR_DEF hlfpua_to_host
|
---|
129 | ;; and mask for cr0.
|
---|
130 | hlfpu_afFlags:
|
---|
131 | RTCCPTR_DEF ~(X86_CR0_TS | X86_CR0_MP)
|
---|
132 | RTCCPTR_DEF ~(X86_CR0_TS)
|
---|
133 | RTCCPTR_DEF ~(X86_CR0_TS | X86_CR0_MP)
|
---|
134 | RTCCPTR_DEF ~(X86_CR0_TS)
|
---|
135 | RTCCPTR_DEF ~(X86_CR0_MP)
|
---|
136 | RTCCPTR_DEF 0
|
---|
137 | RTCCPTR_DEF ~(X86_CR0_MP)
|
---|
138 | RTCCPTR_DEF 0
|
---|
139 |
|
---|
140 | ;
|
---|
141 | ; Action - switch FPU context and change cr0 flags.
|
---|
142 | ;
|
---|
143 | align 16
|
---|
144 | hlfpua_switch_fpu_ctx:
|
---|
145 | %ifndef IN_RING3 ; IN_RC or IN_RING0
|
---|
146 | mov xCX, cr0
|
---|
147 | %ifdef RT_ARCH_AMD64
|
---|
148 | lea r8, [hlfpu_afFlags wrt rip]
|
---|
149 | and rcx, [rax*4 + r8] ; calc the new cr0 flags.
|
---|
150 | %else
|
---|
151 | and ecx, [eax*2 + hlfpu_afFlags] ; calc the new cr0 flags.
|
---|
152 | %endif
|
---|
153 | mov xAX, cr0
|
---|
154 | and xAX, ~(X86_CR0_TS | X86_CR0_EM)
|
---|
155 | mov cr0, xAX ; clear flags so we don't trap here.
|
---|
156 | %endif
|
---|
157 | %ifndef RT_ARCH_AMD64
|
---|
158 | mov eax, edx ; Calculate the PCPUM pointer
|
---|
159 | sub eax, [edx + CPUMCPU.ulOffCPUM]
|
---|
160 | test dword [eax + CPUM.CPUFeatures.edx], X86_CPUID_FEATURE_EDX_FXSR
|
---|
161 | jz short hlfpua_no_fxsave
|
---|
162 | %endif
|
---|
163 |
|
---|
164 | fxsave [xDX + CPUMCPU.Host.fpu]
|
---|
165 | or dword [xDX + CPUMCPU.fUseFlags], (CPUM_USED_FPU | CPUM_USED_FPU_SINCE_REM)
|
---|
166 | fxrstor [xDX + CPUMCPU.Guest.fpu]
|
---|
167 | hlfpua_finished_switch:
|
---|
168 | %ifdef IN_RC
|
---|
169 | mov cr0, xCX ; load the new cr0 flags.
|
---|
170 | %endif
|
---|
171 | ; return continue execution.
|
---|
172 | xor eax, eax
|
---|
173 | ret
|
---|
174 |
|
---|
175 | %ifndef RT_ARCH_AMD64
|
---|
176 | ; legacy support.
|
---|
177 | hlfpua_no_fxsave:
|
---|
178 | fnsave [xDX + CPUMCPU.Host.fpu]
|
---|
179 | or dword [xDX + CPUMCPU.fUseFlags], dword (CPUM_USED_FPU | CPUM_USED_FPU_SINCE_REM) ; yasm / nasm
|
---|
180 | mov eax, [xDX + CPUMCPU.Guest.fpu] ; control word
|
---|
181 | not eax ; 1 means exception ignored (6 LS bits)
|
---|
182 | and eax, byte 03Fh ; 6 LS bits only
|
---|
183 | test eax, [xDX + CPUMCPU.Guest.fpu + 4] ; status word
|
---|
184 | jz short hlfpua_no_exceptions_pending
|
---|
185 | ; technically incorrect, but we certainly don't want any exceptions now!!
|
---|
186 | and dword [xDX + CPUMCPU.Guest.fpu + 4], ~03Fh
|
---|
187 | hlfpua_no_exceptions_pending:
|
---|
188 | frstor [xDX + CPUMCPU.Guest.fpu]
|
---|
189 | jmp near hlfpua_finished_switch
|
---|
190 | %endif ; !RT_ARCH_AMD64
|
---|
191 |
|
---|
192 |
|
---|
193 | ;
|
---|
194 | ; Action - Generate Guest trap.
|
---|
195 | ;
|
---|
196 | hlfpua_action_4:
|
---|
197 | hlfpua_to_host:
|
---|
198 | mov eax, VINF_EM_RAW_GUEST_TRAP
|
---|
199 | ret
|
---|
200 | ENDPROC cpumHandleLazyFPUAsm
|
---|
201 |
|
---|
202 |
|
---|