VirtualBox

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

Last change on this file since 58022 was 57482, checked in by vboxsync, 9 years ago

VMM: doxygen and nits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 KB
Line 
1/* $Id: HMAll.cpp 57482 2015-08-20 16:48:33Z vboxsync $ */
2/** @file
3 * HM - All contexts.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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/thread.h>
36#include <iprt/x86.h>
37#include <iprt/asm-amd64-x86.h>
38
39
40/**
41 * Checks whether HM (VT-x/AMD-V) is being used by this VM.
42 *
43 * @retval @c true if used.
44 * @retval @c false if software virtualization (raw-mode) is used.
45 * @param pVM The cross context VM structure.
46 * @sa HMIsEnabled, HMR3IsEnabled
47 * @internal
48 */
49VMMDECL(bool) HMIsEnabledNotMacro(PVM pVM)
50{
51 Assert(pVM->fHMEnabledFixed);
52 return pVM->fHMEnabled;
53}
54
55
56/**
57 * Queues a guest page for invalidation.
58 *
59 * @returns VBox status code.
60 * @param pVCpu Pointer to the VMCPU.
61 * @param GCVirt Page to invalidate.
62 */
63static void hmQueueInvlPage(PVMCPU pVCpu, RTGCPTR GCVirt)
64{
65 /* Nothing to do if a TLB flush is already pending */
66 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TLB_FLUSH))
67 return;
68 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
69 NOREF(GCVirt);
70}
71
72
73/**
74 * Invalidates a guest page.
75 *
76 * @returns VBox status code.
77 * @param pVCpu Pointer to the VMCPU.
78 * @param GCVirt Page to invalidate.
79 */
80VMM_INT_DECL(int) HMInvalidatePage(PVMCPU pVCpu, RTGCPTR GCVirt)
81{
82 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushPageManual);
83#ifdef IN_RING0
84 PVM pVM = pVCpu->CTX_SUFF(pVM);
85 if (pVM->hm.s.vmx.fSupported)
86 return VMXR0InvalidatePage(pVM, pVCpu, GCVirt);
87
88 Assert(pVM->hm.s.svm.fSupported);
89 return SVMR0InvalidatePage(pVM, pVCpu, GCVirt);
90
91#else
92 hmQueueInvlPage(pVCpu, GCVirt);
93 return VINF_SUCCESS;
94#endif
95}
96
97
98/**
99 * Flushes the guest TLB.
100 *
101 * @returns VBox status code.
102 * @param pVCpu Pointer to the VMCPU.
103 */
104VMM_INT_DECL(int) HMFlushTLB(PVMCPU pVCpu)
105{
106 LogFlow(("HMFlushTLB\n"));
107
108 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
109 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushTlbManual);
110 return VINF_SUCCESS;
111}
112
113
114#ifdef IN_RING0
115/**
116 * Dummy RTMpOnSpecific handler since RTMpPokeCpu couldn't be used.
117 *
118 */
119static DECLCALLBACK(void) hmFlushHandler(RTCPUID idCpu, void *pvUser1, void *pvUser2)
120{
121 NOREF(idCpu); NOREF(pvUser1); NOREF(pvUser2);
122 return;
123}
124
125
126/**
127 * Wrapper for RTMpPokeCpu to deal with VERR_NOT_SUPPORTED.
128 */
129static void hmR0PokeCpu(PVMCPU pVCpu, RTCPUID idHostCpu)
130{
131 uint32_t cWorldSwitchExits = ASMAtomicUoReadU32(&pVCpu->hm.s.cWorldSwitchExits);
132
133 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatPoke, x);
134 int rc = RTMpPokeCpu(idHostCpu);
135 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatPoke, x);
136
137 /* Not implemented on some platforms (Darwin, Linux kernel < 2.6.19); fall
138 back to a less efficient implementation (broadcast). */
139 if (rc == VERR_NOT_SUPPORTED)
140 {
141 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatSpinPoke, z);
142 /* synchronous. */
143 RTMpOnSpecific(idHostCpu, hmFlushHandler, 0, 0);
144 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatSpinPoke, z);
145 }
146 else
147 {
148 if (rc == VINF_SUCCESS)
149 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatSpinPoke, z);
150 else
151 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatSpinPokeFailed, z);
152
153/** @todo If more than one CPU is going to be poked, we could optimize this
154 * operation by poking them first and wait afterwards. Would require
155 * recording who to poke and their current cWorldSwitchExits values,
156 * that's something not suitable for stack... So, pVCpu->hm.s.something
157 * then. */
158 /* Spin until the VCPU has switched back (poking is async). */
159 while ( ASMAtomicUoReadBool(&pVCpu->hm.s.fCheckedTLBFlush)
160 && cWorldSwitchExits == ASMAtomicUoReadU32(&pVCpu->hm.s.cWorldSwitchExits))
161 ASMNopPause();
162
163 if (rc == VINF_SUCCESS)
164 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatSpinPoke, z);
165 else
166 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatSpinPokeFailed, z);
167 }
168}
169#endif /* IN_RING0 */
170
171
172#ifndef IN_RC
173/**
174 * Poke an EMT so it can perform the appropriate TLB shootdowns.
175 *
176 * @param pVCpu The handle of the virtual CPU to poke.
177 * @param fAccountFlushStat Whether to account the call to
178 * StatTlbShootdownFlush or StatTlbShootdown.
179 */
180static void hmPokeCpuForTlbFlush(PVMCPU pVCpu, bool fAccountFlushStat)
181{
182 if (ASMAtomicUoReadBool(&pVCpu->hm.s.fCheckedTLBFlush))
183 {
184 if (fAccountFlushStat)
185 STAM_COUNTER_INC(&pVCpu->hm.s.StatTlbShootdownFlush);
186 else
187 STAM_COUNTER_INC(&pVCpu->hm.s.StatTlbShootdown);
188#ifdef IN_RING0
189 RTCPUID idHostCpu = pVCpu->hm.s.idEnteredCpu;
190 if (idHostCpu != NIL_RTCPUID)
191 hmR0PokeCpu(pVCpu, idHostCpu);
192#else
193 VMR3NotifyCpuFFU(pVCpu->pUVCpu, VMNOTIFYFF_FLAGS_POKE);
194#endif
195 }
196 else
197 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushPageManual);
198}
199
200
201/**
202 * Invalidates a guest page on all VCPUs.
203 *
204 * @returns VBox status code.
205 * @param pVM Pointer to the VM.
206 * @param GCVirt Page to invalidate.
207 */
208VMM_INT_DECL(int) HMInvalidatePageOnAllVCpus(PVM pVM, RTGCPTR GCVirt)
209{
210 /*
211 * The VT-x/AMD-V code will be flushing TLB each time a VCPU migrates to a different
212 * host CPU, see hmR0VmxFlushTaggedTlbBoth() and hmR0SvmFlushTaggedTlb().
213 *
214 * This is the reason why we do not care about thread preemption here and just
215 * execute HMInvalidatePage() assuming it might be the 'right' CPU.
216 */
217 VMCPUID idCurCpu = VMMGetCpuId(pVM);
218 STAM_COUNTER_INC(&pVM->aCpus[idCurCpu].hm.s.StatFlushPage);
219
220 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
221 {
222 PVMCPU pVCpu = &pVM->aCpus[idCpu];
223
224 /* Nothing to do if a TLB flush is already pending; the VCPU should
225 have already been poked if it were active. */
226 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TLB_FLUSH))
227 continue;
228
229 if (pVCpu->idCpu == idCurCpu)
230 HMInvalidatePage(pVCpu, GCVirt);
231 else
232 {
233 hmQueueInvlPage(pVCpu, GCVirt);
234 hmPokeCpuForTlbFlush(pVCpu, false /* fAccountFlushStat */);
235 }
236 }
237
238 return VINF_SUCCESS;
239}
240
241
242/**
243 * Flush the TLBs of all VCPUs.
244 *
245 * @returns VBox status code.
246 * @param pVM Pointer to the VM.
247 */
248VMM_INT_DECL(int) HMFlushTLBOnAllVCpus(PVM pVM)
249{
250 if (pVM->cCpus == 1)
251 return HMFlushTLB(&pVM->aCpus[0]);
252
253 VMCPUID idThisCpu = VMMGetCpuId(pVM);
254
255 STAM_COUNTER_INC(&pVM->aCpus[idThisCpu].hm.s.StatFlushTlb);
256
257 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
258 {
259 PVMCPU pVCpu = &pVM->aCpus[idCpu];
260
261 /* Nothing to do if a TLB flush is already pending; the VCPU should
262 have already been poked if it were active. */
263 if (!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TLB_FLUSH))
264 {
265 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
266 if (idThisCpu != idCpu)
267 hmPokeCpuForTlbFlush(pVCpu, true /* fAccountFlushStat */);
268 }
269 }
270
271 return VINF_SUCCESS;
272}
273#endif /* !IN_RC */
274
275/**
276 * Checks if nested paging is enabled.
277 *
278 * @returns true if nested paging is active, false otherwise.
279 * @param pVM Pointer to the VM.
280 *
281 * @remarks Works before hmR3InitFinalizeR0.
282 */
283VMM_INT_DECL(bool) HMIsNestedPagingActive(PVM pVM)
284{
285 return HMIsEnabled(pVM) && pVM->hm.s.fNestedPaging;
286}
287
288
289/**
290 * Checks if both nested paging and unhampered guest execution are enabled.
291 *
292 * The almost complete guest execution in hardware is only applicable to VT-x.
293 *
294 * @returns true if we have both enabled, otherwise false.
295 * @param pVM Pointer to the VM.
296 *
297 * @remarks Works before hmR3InitFinalizeR0.
298 */
299VMM_INT_DECL(bool) HMAreNestedPagingAndFullGuestExecEnabled(PVM pVM)
300{
301 return HMIsEnabled(pVM)
302 && pVM->hm.s.fNestedPaging
303 && ( pVM->hm.s.vmx.fUnrestrictedGuest
304 || pVM->hm.s.svm.fSupported);
305}
306
307
308/**
309 * Checks if this VM is long-mode capable.
310 *
311 * @returns true if long mode is allowed, false otherwise.
312 * @param pUVM The user mode VM handle.
313 */
314VMM_INT_DECL(bool) HMIsLongModeAllowed(PVM pVM)
315{
316 return HMIsEnabled(pVM) && pVM->hm.s.fAllow64BitGuests;
317}
318
319
320/**
321 * Checks if MSR bitmaps are available. It is assumed that when it's available
322 * it will be used as well.
323 *
324 * @returns true if MSR bitmaps are available, false otherwise.
325 * @param pVM Pointer to the VM.
326 */
327VMM_INT_DECL(bool) HMAreMsrBitmapsAvailable(PVM pVM)
328{
329 if (HMIsEnabled(pVM))
330 {
331 if (pVM->hm.s.svm.fSupported)
332 return true;
333
334 if ( pVM->hm.s.vmx.fSupported
335 && (pVM->hm.s.vmx.Msrs.VmxProcCtls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_MSR_BITMAPS))
336 {
337 return true;
338 }
339 }
340 return false;
341}
342
343
344/**
345 * Return the shadow paging mode for nested paging/ept
346 *
347 * @returns shadow paging mode
348 * @param pVM Pointer to the VM.
349 */
350VMM_INT_DECL(PGMMODE) HMGetShwPagingMode(PVM pVM)
351{
352 Assert(HMIsNestedPagingActive(pVM));
353 if (pVM->hm.s.svm.fSupported)
354 return PGMMODE_NESTED;
355
356 Assert(pVM->hm.s.vmx.fSupported);
357 return PGMMODE_EPT;
358}
359
360
361/**
362 * Invalidates a guest page by physical address.
363 *
364 * @returns VBox status code.
365 * @param pVM Pointer to the VM.
366 * @param GCPhys Page to invalidate.
367 *
368 * @remarks Assumes the current instruction references this physical page
369 * though a virtual address!
370 */
371VMM_INT_DECL(int) HMInvalidatePhysPage(PVM pVM, RTGCPHYS GCPhys)
372{
373 if (!HMIsNestedPagingActive(pVM))
374 return VINF_SUCCESS;
375
376#ifdef IN_RING0
377 if (pVM->hm.s.vmx.fSupported)
378 {
379 VMCPUID idThisCpu = VMMGetCpuId(pVM);
380
381 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
382 {
383 PVMCPU pVCpu = &pVM->aCpus[idCpu];
384
385 if (idThisCpu == idCpu)
386 {
387 /** @todo r=ramshankar: Intel does not support flushing by guest physical
388 * address either. See comment in VMXR0InvalidatePhysPage(). Fix this. */
389 VMXR0InvalidatePhysPage(pVM, pVCpu, GCPhys);
390 }
391 else
392 {
393 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
394 hmPokeCpuForTlbFlush(pVCpu, true /*fAccountFlushStat*/);
395 }
396 }
397 return VINF_SUCCESS;
398 }
399
400 /* AMD-V doesn't support invalidation with guest physical addresses; see
401 comment in SVMR0InvalidatePhysPage. */
402 Assert(pVM->hm.s.svm.fSupported);
403#else
404 NOREF(GCPhys);
405#endif
406
407 HMFlushTLBOnAllVCpus(pVM);
408 return VINF_SUCCESS;
409}
410
411/**
412 * Checks if an interrupt event is currently pending.
413 *
414 * @returns Interrupt event pending state.
415 * @param pVM Pointer to the VM.
416 */
417VMM_INT_DECL(bool) HMHasPendingIrq(PVM pVM)
418{
419 PVMCPU pVCpu = VMMGetCpu(pVM);
420 return !!pVCpu->hm.s.Event.fPending;
421}
422
423
424/**
425 * Return the PAE PDPE entries.
426 *
427 * @returns Pointer to the PAE PDPE array.
428 * @param pVCpu Pointer to the VMCPU.
429 */
430VMM_INT_DECL(PX86PDPE) HMGetPaePdpes(PVMCPU pVCpu)
431{
432 return &pVCpu->hm.s.aPdpes[0];
433}
434
435
436/**
437 * Checks if the current AMD CPU is subject to erratum 170 "In SVM mode,
438 * incorrect code bytes may be fetched after a world-switch".
439 *
440 * @param pu32Family Where to store the CPU family (can be NULL).
441 * @param pu32Model Where to store the CPU model (can be NULL).
442 * @param pu32Stepping Where to store the CPU stepping (can be NULL).
443 * @returns true if the erratum applies, false otherwise.
444 */
445VMM_INT_DECL(int) HMAmdIsSubjectToErratum170(uint32_t *pu32Family, uint32_t *pu32Model, uint32_t *pu32Stepping)
446{
447 /*
448 * Erratum 170 which requires a forced TLB flush for each world switch:
449 * See AMD spec. "Revision Guide for AMD NPT Family 0Fh Processors".
450 *
451 * All BH-G1/2 and DH-G1/2 models include a fix:
452 * Athlon X2: 0x6b 1/2
453 * 0x68 1/2
454 * Athlon 64: 0x7f 1
455 * 0x6f 2
456 * Sempron: 0x7f 1/2
457 * 0x6f 2
458 * 0x6c 2
459 * 0x7c 2
460 * Turion 64: 0x68 2
461 */
462 uint32_t u32Dummy;
463 uint32_t u32Version, u32Family, u32Model, u32Stepping, u32BaseFamily;
464 ASMCpuId(1, &u32Version, &u32Dummy, &u32Dummy, &u32Dummy);
465 u32BaseFamily = (u32Version >> 8) & 0xf;
466 u32Family = u32BaseFamily + (u32BaseFamily == 0xf ? ((u32Version >> 20) & 0x7f) : 0);
467 u32Model = ((u32Version >> 4) & 0xf);
468 u32Model = u32Model | ((u32BaseFamily == 0xf ? (u32Version >> 16) & 0x0f : 0) << 4);
469 u32Stepping = u32Version & 0xf;
470
471 bool fErratumApplies = false;
472 if ( u32Family == 0xf
473 && !((u32Model == 0x68 || u32Model == 0x6b || u32Model == 0x7f) && u32Stepping >= 1)
474 && !((u32Model == 0x6f || u32Model == 0x6c || u32Model == 0x7c) && u32Stepping >= 2))
475 {
476 fErratumApplies = true;
477 }
478
479 if (pu32Family)
480 *pu32Family = u32Family;
481 if (pu32Model)
482 *pu32Model = u32Model;
483 if (pu32Stepping)
484 *pu32Stepping = u32Stepping;
485
486 return fErratumApplies;
487}
488
489
490/**
491 * Sets or clears the single instruction flag.
492 *
493 * When set, HM will try its best to return to ring-3 after executing a single
494 * instruction. This can be used for debugging. See also
495 * EMR3HmSingleInstruction.
496 *
497 * @returns The old flag state.
498 * @param pVCpu Pointer to the cross context CPU structure of
499 * the calling EMT.
500 * @param fEnable The new flag state.
501 */
502VMM_INT_DECL(bool) HMSetSingleInstruction(PVMCPU pVCpu, bool fEnable)
503{
504 VMCPU_ASSERT_EMT(pVCpu);
505 bool fOld = pVCpu->hm.s.fSingleInstruction;
506 pVCpu->hm.s.fSingleInstruction = fEnable;
507 return fOld;
508}
509
510
511/**
512 * Notifies HM that paravirtualized hypercalls are now enabled.
513 *
514 * @param pVCpu Pointer to the VMCPU.
515 */
516VMM_INT_DECL(void) HMHypercallsEnable(PVMCPU pVCpu)
517{
518 pVCpu->hm.s.fHypercallsEnabled = true;
519}
520
521
522/**
523 * Notifies HM that paravirtualized hypercalls are now disabled.
524 *
525 * @param pVCpu Pointer to the VMCPU.
526 */
527VMM_INT_DECL(void) HMHypercallsDisable(PVMCPU pVCpu)
528{
529 pVCpu->hm.s.fHypercallsEnabled = false;
530}
531
532
533/**
534 * Notifies HM that GIM provider wants to trap #UD.
535 *
536 * @param pVCpu Pointer to the VMCPU.
537 */
538VMM_INT_DECL(void) HMTrapXcptUDForGIMEnable(PVMCPU pVCpu)
539{
540 pVCpu->hm.s.fGIMTrapXcptUD = true;
541 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_XCPT_INTERCEPTS);
542}
543
544
545/**
546 * Notifies HM that GIM provider no longer wants to trap #UD.
547 *
548 * @param pVCpu Pointer to the VMCPU.
549 */
550VMM_INT_DECL(void) HMTrapXcptUDForGIMDisable(PVMCPU pVCpu)
551{
552 pVCpu->hm.s.fGIMTrapXcptUD = false;
553 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_XCPT_INTERCEPTS);
554}
555
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