VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/HMAll.cpp@ 45887

Last change on this file since 45887 was 45701, checked in by vboxsync, 12 years ago

VMM: SELM and VMM early HM init changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: HMAll.cpp 45701 2013-04-24 14:21:09Z vboxsync $ */
2/** @file
3 * HM - All contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_HM
23#include <VBox/vmm/hm.h>
24#include <VBox/vmm/pgm.h>
25#include "HMInternal.h"
26#include <VBox/vmm/vm.h>
27#include <VBox/vmm/hm_vmx.h>
28#include <VBox/vmm/hm_svm.h>
29#include <VBox/err.h>
30#include <VBox/log.h>
31#include <iprt/param.h>
32#include <iprt/assert.h>
33#include <iprt/asm.h>
34#include <iprt/string.h>
35#include <iprt/x86.h>
36
37
38
39/**
40 * Checks whether HM (VT-x/AMD-V) is being used by this VM.
41 *
42 * @retval @c true if used.
43 * @retval @c false if software virtualization (raw-mode) is used.
44 * @param pVM The cross context VM structure.
45 * @sa HMIsEnabled, HMR3IsEnabled
46 * @internal
47 */
48VMMDECL(bool) HMIsEnabledNotMacro(PVM pVM)
49{
50 Assert(pVM->fHMEnabledFixed);
51 return pVM->fHMEnabled;
52}
53
54
55/**
56 * Queues a page for invalidation
57 *
58 * @returns VBox status code.
59 * @param pVCpu Pointer to the VMCPU.
60 * @param GCVirt Page to invalidate
61 */
62static void hmQueueInvlPage(PVMCPU pVCpu, RTGCPTR GCVirt)
63{
64 /* Nothing to do if a TLB flush is already pending */
65 if (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_TLB_FLUSH))
66 return;
67#if 1
68 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
69 NOREF(GCVirt);
70#else
71 /* Be very careful when activating this code! */
72 if (iPage == RT_ELEMENTS(pVCpu->hm.s.TlbShootdown.aPages))
73 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
74 else
75 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_SHOOTDOWN);
76#endif
77}
78
79/**
80 * Invalidates a guest page
81 *
82 * @returns VBox status code.
83 * @param pVCpu Pointer to the VMCPU.
84 * @param GCVirt Page to invalidate
85 */
86VMM_INT_DECL(int) HMInvalidatePage(PVMCPU pVCpu, RTGCPTR GCVirt)
87{
88 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushPageManual);
89#ifdef IN_RING0
90 PVM pVM = pVCpu->CTX_SUFF(pVM);
91 if (pVM->hm.s.vmx.fSupported)
92 return VMXR0InvalidatePage(pVM, pVCpu, GCVirt);
93
94 Assert(pVM->hm.s.svm.fSupported);
95 return SVMR0InvalidatePage(pVM, pVCpu, GCVirt);
96
97#else
98 hmQueueInvlPage(pVCpu, GCVirt);
99 return VINF_SUCCESS;
100#endif
101}
102
103/**
104 * Flushes the guest TLB
105 *
106 * @returns VBox status code.
107 * @param pVCpu Pointer to the VMCPU.
108 */
109VMM_INT_DECL(int) HMFlushTLB(PVMCPU pVCpu)
110{
111 LogFlow(("HMFlushTLB\n"));
112
113 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
114 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushTlbManual);
115 return VINF_SUCCESS;
116}
117
118#ifdef IN_RING0
119
120/**
121 * Dummy RTMpOnSpecific handler since RTMpPokeCpu couldn't be used.
122 *
123 */
124static DECLCALLBACK(void) hmFlushHandler(RTCPUID idCpu, void *pvUser1, void *pvUser2)
125{
126 NOREF(idCpu); NOREF(pvUser1); NOREF(pvUser2);
127 return;
128}
129
130/**
131 * Wrapper for RTMpPokeCpu to deal with VERR_NOT_SUPPORTED.
132 */
133static void hmR0PokeCpu(PVMCPU pVCpu, RTCPUID idHostCpu)
134{
135 uint32_t cWorldSwitchExits = ASMAtomicUoReadU32(&pVCpu->hm.s.cWorldSwitchExits);
136
137 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatPoke, x);
138 int rc = RTMpPokeCpu(idHostCpu);
139 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatPoke, x);
140
141 /* Not implemented on some platforms (Darwin, Linux kernel < 2.6.19); fall
142 back to a less efficient implementation (broadcast). */
143 if (rc == VERR_NOT_SUPPORTED)
144 {
145 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatSpinPoke, z);
146 /* synchronous. */
147 RTMpOnSpecific(idHostCpu, hmFlushHandler, 0, 0);
148 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatSpinPoke, z);
149 }
150 else
151 {
152 if (rc == VINF_SUCCESS)
153 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatSpinPoke, z);
154 else
155 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatSpinPokeFailed, z);
156
157/** @todo If more than one CPU is going to be poked, we could optimize this
158 * operation by poking them first and wait afterwards. Would require
159 * recording who to poke and their current cWorldSwitchExits values,
160 * that's something not suitable for stack... So, pVCpu->hm.s.something
161 * then. */
162 /* Spin until the VCPU has switched back (poking is async). */
163 while ( ASMAtomicUoReadBool(&pVCpu->hm.s.fCheckedTLBFlush)
164 && cWorldSwitchExits == ASMAtomicUoReadU32(&pVCpu->hm.s.cWorldSwitchExits))
165 ASMNopPause();
166
167 if (rc == VINF_SUCCESS)
168 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatSpinPoke, z);
169 else
170 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatSpinPokeFailed, z);
171 }
172}
173
174#endif /* IN_RING0 */
175#ifndef IN_RC
176
177/**
178 * Poke an EMT so it can perform the appropriate TLB shootdowns.
179 *
180 * @param pVCpu The handle of the virtual CPU to poke.
181 * @param fAccountFlushStat Whether to account the call to
182 * StatTlbShootdownFlush or StatTlbShootdown.
183 */
184static void hmPokeCpuForTlbFlush(PVMCPU pVCpu, bool fAccountFlushStat)
185{
186 if (ASMAtomicUoReadBool(&pVCpu->hm.s.fCheckedTLBFlush))
187 {
188 if (fAccountFlushStat)
189 STAM_COUNTER_INC(&pVCpu->hm.s.StatTlbShootdownFlush);
190 else
191 STAM_COUNTER_INC(&pVCpu->hm.s.StatTlbShootdown);
192#ifdef IN_RING0
193 RTCPUID idHostCpu = pVCpu->hm.s.idEnteredCpu;
194 if (idHostCpu != NIL_RTCPUID)
195 hmR0PokeCpu(pVCpu, idHostCpu);
196#else
197 VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_POKE);
198#endif
199 }
200 else
201 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushPageManual);
202}
203
204
205/**
206 * Invalidates a guest page on all VCPUs.
207 *
208 * @returns VBox status code.
209 * @param pVM Pointer to the VM.
210 * @param GCVirt Page to invalidate
211 */
212VMM_INT_DECL(int) HMInvalidatePageOnAllVCpus(PVM pVM, RTGCPTR GCPtr)
213{
214 VMCPUID idCurCpu = VMMGetCpuId(pVM);
215 STAM_COUNTER_INC(&pVM->aCpus[idCurCpu].hm.s.StatFlushPage);
216
217 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
218 {
219 PVMCPU pVCpu = &pVM->aCpus[idCpu];
220
221 /* Nothing to do if a TLB flush is already pending; the VCPU should
222 have already been poked if it were active. */
223 if (VMCPU_FF_ISSET(pVCpu, VMCPU_FF_TLB_FLUSH))
224 continue;
225
226 if (pVCpu->idCpu == idCurCpu)
227 HMInvalidatePage(pVCpu, GCPtr);
228 else
229 {
230 hmQueueInvlPage(pVCpu, GCPtr);
231 hmPokeCpuForTlbFlush(pVCpu, false /*fAccountFlushStat*/);
232 }
233 }
234
235 return VINF_SUCCESS;
236}
237
238
239/**
240 * Flush the TLBs of all VCPUs
241 *
242 * @returns VBox status code.
243 * @param pVM Pointer to the VM.
244 */
245VMM_INT_DECL(int) HMFlushTLBOnAllVCpus(PVM pVM)
246{
247 if (pVM->cCpus == 1)
248 return HMFlushTLB(&pVM->aCpus[0]);
249
250 VMCPUID idThisCpu = VMMGetCpuId(pVM);
251
252 STAM_COUNTER_INC(&pVM->aCpus[idThisCpu].hm.s.StatFlushTlb);
253
254 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
255 {
256 PVMCPU pVCpu = &pVM->aCpus[idCpu];
257
258 /* Nothing to do if a TLB flush is already pending; the VCPU should
259 have already been poked if it were active. */
260 if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_TLB_FLUSH))
261 {
262 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
263 if (idThisCpu != idCpu)
264 hmPokeCpuForTlbFlush(pVCpu, true /*fAccountFlushStat*/);
265 }
266 }
267
268 return VINF_SUCCESS;
269}
270
271#endif /* !IN_RC */
272
273/**
274 * Checks if nested paging is enabled
275 *
276 * @returns boolean
277 * @param pVM Pointer to the VM.
278 */
279VMM_INT_DECL(bool) HMIsNestedPagingActive(PVM pVM)
280{
281 return HMIsEnabled(pVM) && pVM->hm.s.fNestedPaging;
282}
283
284/**
285 * Return the shadow paging mode for nested paging/ept
286 *
287 * @returns shadow paging mode
288 * @param pVM Pointer to the VM.
289 */
290VMM_INT_DECL(PGMMODE) HMGetShwPagingMode(PVM pVM)
291{
292 Assert(HMIsNestedPagingActive(pVM));
293 if (pVM->hm.s.svm.fSupported)
294 return PGMMODE_NESTED;
295
296 Assert(pVM->hm.s.vmx.fSupported);
297 return PGMMODE_EPT;
298}
299
300/**
301 * Invalidates a guest page by physical address
302 *
303 * NOTE: Assumes the current instruction references this physical page though a virtual address!!
304 *
305 * @returns VBox status code.
306 * @param pVM Pointer to the VM.
307 * @param GCPhys Page to invalidate
308 */
309VMM_INT_DECL(int) HMInvalidatePhysPage(PVM pVM, RTGCPHYS GCPhys)
310{
311 if (!HMIsNestedPagingActive(pVM))
312 return VINF_SUCCESS;
313
314#ifdef IN_RING0
315 if (pVM->hm.s.vmx.fSupported)
316 {
317 VMCPUID idThisCpu = VMMGetCpuId(pVM);
318
319 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
320 {
321 PVMCPU pVCpu = &pVM->aCpus[idCpu];
322
323 if (idThisCpu == idCpu)
324 {
325 /** @todo r=ramshankar: Intel does not support flushing by guest physical
326 * address either. See comment in VMXR0InvalidatePhysPage(). Fix this. */
327 VMXR0InvalidatePhysPage(pVM, pVCpu, GCPhys);
328 }
329 else
330 {
331 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
332 hmPokeCpuForTlbFlush(pVCpu, true /*fAccountFlushStat*/);
333 }
334 }
335 return VINF_SUCCESS;
336 }
337
338 /* AMD-V doesn't support invalidation with guest physical addresses; see
339 comment in SVMR0InvalidatePhysPage. */
340 Assert(pVM->hm.s.svm.fSupported);
341#else
342 NOREF(GCPhys);
343#endif
344
345 HMFlushTLBOnAllVCpus(pVM);
346 return VINF_SUCCESS;
347}
348
349/**
350 * Checks if an interrupt event is currently pending.
351 *
352 * @returns Interrupt event pending state.
353 * @param pVM Pointer to the VM.
354 */
355VMM_INT_DECL(bool) HMHasPendingIrq(PVM pVM)
356{
357 PVMCPU pVCpu = VMMGetCpu(pVM);
358 return !!pVCpu->hm.s.Event.fPending;
359}
360
361
362/**
363 * Return the PAE PDPE entries.
364 *
365 * @returns Pointer to the PAE PDPE array.
366 * @param pVCpu Pointer to the VMCPU.
367 */
368VMM_INT_DECL(PX86PDPE) HMGetPaePdpes(PVMCPU pVCpu)
369{
370 return &pVCpu->hm.s.aPdpes[0];
371}
372
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette