1 | /* $Id: HWACCMAll.cpp 19910 2009-05-22 12:31:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * HWACCM - All contexts.
|
---|
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 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_HWACCM
|
---|
27 | #include <VBox/hwaccm.h>
|
---|
28 | #include "HWACCMInternal.h"
|
---|
29 | #include <VBox/vm.h>
|
---|
30 | #include <VBox/x86.h>
|
---|
31 | #include <VBox/hwacc_vmx.h>
|
---|
32 | #include <VBox/hwacc_svm.h>
|
---|
33 | #include <VBox/pgm.h>
|
---|
34 | #include <VBox/pdm.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 | #include <VBox/log.h>
|
---|
37 | #include <VBox/selm.h>
|
---|
38 | #include <VBox/iom.h>
|
---|
39 | #include <iprt/param.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/memobj.h>
|
---|
44 | #include <iprt/cpuset.h>
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Queues a page for invalidation
|
---|
48 | *
|
---|
49 | * @returns VBox status code.
|
---|
50 | * @param pVCpu The VMCPU to operate on.
|
---|
51 | * @param GCVirt Page to invalidate
|
---|
52 | */
|
---|
53 | void hwaccmQueueInvlPage(PVMCPU pVCpu, RTGCPTR GCVirt)
|
---|
54 | {
|
---|
55 | Assert(HWACCMIsEnabled(pVCpu->CTX_SUFF(pVM)));
|
---|
56 |
|
---|
57 | /* Nothing to do if a TLB flush is already pending */
|
---|
58 | if (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_TLB_FLUSH))
|
---|
59 | return;
|
---|
60 | #if 1
|
---|
61 | VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
|
---|
62 | #else
|
---|
63 | if (iPage == RT_ELEMENTS(pVCpu->hwaccm.s.TlbShootdown.aPages))
|
---|
64 | VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
|
---|
65 | else
|
---|
66 | VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_SHOOTDOWN);
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Invalidates a guest page
|
---|
72 | *
|
---|
73 | * @returns VBox status code.
|
---|
74 | * @param pVCpu The VMCPU to operate on.
|
---|
75 | * @param GCVirt Page to invalidate
|
---|
76 | */
|
---|
77 | VMMDECL(int) HWACCMInvalidatePage(PVMCPU pVCpu, RTGCPTR GCVirt)
|
---|
78 | {
|
---|
79 | STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushPageManual);
|
---|
80 | #ifdef IN_RING0
|
---|
81 | PVM pVM = pVCpu->CTX_SUFF(pVM);
|
---|
82 | if (pVM->hwaccm.s.vmx.fSupported)
|
---|
83 | return VMXR0InvalidatePage(pVM, pVCpu, GCVirt);
|
---|
84 |
|
---|
85 | Assert(pVM->hwaccm.s.svm.fSupported);
|
---|
86 | return SVMR0InvalidatePage(pVM, pVCpu, GCVirt);
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | hwaccmQueueInvlPage(pVCpu, GCVirt);
|
---|
90 | return VINF_SUCCESS;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Flushes the guest TLB
|
---|
95 | *
|
---|
96 | * @returns VBox status code.
|
---|
97 | * @param pVCpu The VMCPU to operate on.
|
---|
98 | */
|
---|
99 | VMMDECL(int) HWACCMFlushTLB(PVMCPU pVCpu)
|
---|
100 | {
|
---|
101 | LogFlow(("HWACCMFlushTLB\n"));
|
---|
102 |
|
---|
103 | VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
|
---|
104 | STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushTLBManual);
|
---|
105 | return VINF_SUCCESS;
|
---|
106 | }
|
---|
107 |
|
---|
108 | #ifndef IN_RC
|
---|
109 | /**
|
---|
110 | * Invalidates a guest page on all VCPUs.
|
---|
111 | *
|
---|
112 | * @returns VBox status code.
|
---|
113 | * @param pVM The VM to operate on.
|
---|
114 | * @param GCVirt Page to invalidate
|
---|
115 | */
|
---|
116 | VMMDECL(int) HWACCMInvalidatePageOnAllVCpus(PVM pVM, RTGCPTR GCPtr)
|
---|
117 | {
|
---|
118 | VMCPUID idCurCpu = VMMGetCpuId(pVM);
|
---|
119 |
|
---|
120 | for (unsigned idCpu = 0; idCpu < pVM->cCPUs; idCpu++)
|
---|
121 | {
|
---|
122 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
123 |
|
---|
124 | if (pVCpu->idCpu == idCurCpu)
|
---|
125 | {
|
---|
126 | HWACCMInvalidatePage(pVCpu, GCPtr);
|
---|
127 | }
|
---|
128 | else
|
---|
129 | {
|
---|
130 | hwaccmQueueInvlPage(pVCpu, GCPtr);
|
---|
131 | if (VMCPU_GET_STATE(pVCpu) == VMCPUSTATE_STARTED_EXEC)
|
---|
132 | {
|
---|
133 | STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatTlbShootdown);
|
---|
134 | #ifdef IN_RING0
|
---|
135 | RTCPUID idHostCpu = pVCpu->hwaccm.s.idEnteredCpu;
|
---|
136 | if (idHostCpu != NIL_RTCPUID)
|
---|
137 | RTMpPokeCpu(idHostCpu);
|
---|
138 | #else
|
---|
139 | VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_POKE);
|
---|
140 | #endif
|
---|
141 | }
|
---|
142 | else
|
---|
143 | STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushPageManual);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | return VINF_SUCCESS;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Flush the TLBs of all VCPUs
|
---|
152 | *
|
---|
153 | * @returns VBox status code.
|
---|
154 | * @param pVM The VM to operate on.
|
---|
155 | */
|
---|
156 | VMMDECL(int) HWACCMFlushTLBOnAllVCpus(PVM pVM)
|
---|
157 | {
|
---|
158 | if (pVM->cCPUs == 1)
|
---|
159 | return HWACCMFlushTLB(&pVM->aCpus[0]);
|
---|
160 |
|
---|
161 | VMCPUID idThisCpu = VMMGetCpuId(pVM);
|
---|
162 |
|
---|
163 | for (unsigned idCpu = 0; idCpu < pVM->cCPUs; idCpu++)
|
---|
164 | {
|
---|
165 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
166 |
|
---|
167 | VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
|
---|
168 | if (idThisCpu == idCpu)
|
---|
169 | continue;
|
---|
170 |
|
---|
171 | if (VMCPU_GET_STATE(pVCpu) == VMCPUSTATE_STARTED_EXEC)
|
---|
172 | {
|
---|
173 | STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatTlbShootdownFlush);
|
---|
174 | #ifdef IN_RING0
|
---|
175 | RTCPUID idHostCpu = pVCpu->hwaccm.s.idEnteredCpu;
|
---|
176 | if (idHostCpu != NIL_RTCPUID)
|
---|
177 | RTMpPokeCpu(idHostCpu);
|
---|
178 | #else
|
---|
179 | VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_POKE);
|
---|
180 | #endif
|
---|
181 | }
|
---|
182 | else
|
---|
183 | STAM_COUNTER_INC(&pVCpu->hwaccm.s.StatFlushTLBManual);
|
---|
184 | }
|
---|
185 | return VINF_SUCCESS;
|
---|
186 | }
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Checks if nested paging is enabled
|
---|
191 | *
|
---|
192 | * @returns boolean
|
---|
193 | * @param pVM The VM to operate on.
|
---|
194 | */
|
---|
195 | VMMDECL(bool) HWACCMIsNestedPagingActive(PVM pVM)
|
---|
196 | {
|
---|
197 | return HWACCMIsEnabled(pVM) && pVM->hwaccm.s.fNestedPaging;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Return the shadow paging mode for nested paging/ept
|
---|
202 | *
|
---|
203 | * @returns shadow paging mode
|
---|
204 | * @param pVM The VM to operate on.
|
---|
205 | */
|
---|
206 | VMMDECL(PGMMODE) HWACCMGetShwPagingMode(PVM pVM)
|
---|
207 | {
|
---|
208 | Assert(HWACCMIsNestedPagingActive(pVM));
|
---|
209 | if (pVM->hwaccm.s.svm.fSupported)
|
---|
210 | return PGMMODE_NESTED;
|
---|
211 |
|
---|
212 | Assert(pVM->hwaccm.s.vmx.fSupported);
|
---|
213 | return PGMMODE_EPT;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Invalidates a guest page by physical address
|
---|
218 | *
|
---|
219 | * NOTE: Assumes the current instruction references this physical page though a virtual address!!
|
---|
220 | *
|
---|
221 | * @returns VBox status code.
|
---|
222 | * @param pVM The VM to operate on.
|
---|
223 | * @param GCPhys Page to invalidate
|
---|
224 | */
|
---|
225 | VMMDECL(int) HWACCMInvalidatePhysPage(PVM pVM, RTGCPHYS GCPhys)
|
---|
226 | {
|
---|
227 | if (!HWACCMIsNestedPagingActive(pVM))
|
---|
228 | return VINF_SUCCESS;
|
---|
229 |
|
---|
230 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
231 | #ifdef IN_RING0
|
---|
232 | if (pVM->hwaccm.s.vmx.fSupported)
|
---|
233 | {
|
---|
234 | /* @todo for all vcpus */
|
---|
235 | return VMXR0InvalidatePhysPage(pVM, pVCpu, GCPhys);
|
---|
236 | }
|
---|
237 |
|
---|
238 | Assert(pVM->hwaccm.s.svm.fSupported);
|
---|
239 | /* AMD-V doesn't support invalidation with guest physical addresses; see comment in SVMR0InvalidatePhysPage. */
|
---|
240 | HWACCMFlushTLBOnAllVCpus(pVM);
|
---|
241 | #else
|
---|
242 | HWACCMFlushTLBOnAllVCpus(pVM);
|
---|
243 | #endif
|
---|
244 | return VINF_SUCCESS;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Checks if an interrupt event is currently pending.
|
---|
249 | *
|
---|
250 | * @returns Interrupt event pending state.
|
---|
251 | * @param pVM The VM to operate on.
|
---|
252 | */
|
---|
253 | VMMDECL(bool) HWACCMHasPendingIrq(PVM pVM)
|
---|
254 | {
|
---|
255 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
256 | return !!pVCpu->hwaccm.s.Event.fPending;
|
---|
257 | }
|
---|
258 |
|
---|