VirtualBox

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

Last change on this file since 57076 was 57066, checked in by vboxsync, 9 years ago

VMM/HM: nit.

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