VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/NEMR3.cpp@ 99051

Last change on this file since 99051 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: NEMR3.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager.
4 */
5
6/*
7 * Copyright (C) 2018-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28/** @page pg_nem NEM - Native Execution Manager.
29 *
30 * This is an alternative execution manage to HM and raw-mode. On one host
31 * (Windows) we're forced to use this, on the others we just do it because we
32 * can. Since this is host specific in nature, information about an
33 * implementation is contained in the NEMR3Native-xxxx.cpp files.
34 *
35 * @ref pg_nem_win
36 */
37
38
39/*********************************************************************************************************************************
40* Header Files *
41*********************************************************************************************************************************/
42#define LOG_GROUP LOG_GROUP_NEM
43#include <VBox/vmm/dbgf.h>
44#include <VBox/vmm/nem.h>
45#include <VBox/vmm/gim.h>
46#include "NEMInternal.h"
47#include <VBox/vmm/vm.h>
48#include <VBox/vmm/uvm.h>
49#include <VBox/err.h>
50
51#include <iprt/asm.h>
52
53
54
55/**
56 * Basic init and configuration reading.
57 *
58 * Always call NEMR3Term after calling this.
59 *
60 * @returns VBox status code.
61 * @param pVM The cross context VM structure.
62 */
63VMMR3_INT_DECL(int) NEMR3InitConfig(PVM pVM)
64{
65 LogFlow(("NEMR3Init\n"));
66
67 /*
68 * Assert alignment and sizes.
69 */
70 AssertCompileMemberAlignment(VM, nem.s, 64);
71 AssertCompile(sizeof(pVM->nem.s) <= sizeof(pVM->nem.padding));
72
73 /*
74 * Initialize state info so NEMR3Term will always be happy.
75 * No returning prior to setting magics!
76 */
77 pVM->nem.s.u32Magic = NEM_MAGIC;
78 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
79 {
80 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
81 pVCpu->nem.s.u32Magic = NEMCPU_MAGIC;
82 }
83
84 /*
85 * Read configuration.
86 */
87 PCFGMNODE pCfgNem = CFGMR3GetChild(CFGMR3GetRoot(pVM), "NEM/");
88
89 /*
90 * Validate the NEM settings.
91 */
92 int rc = CFGMR3ValidateConfig(pCfgNem,
93 "/NEM/",
94 "Enabled"
95 "|Allow64BitGuests"
96 "|LovelyMesaDrvWorkaround"
97#ifdef RT_OS_WINDOWS
98 "|UseRing0Runloop"
99#elif defined(RT_OS_DARWIN)
100 "|VmxPleGap"
101 "|VmxPleWindow"
102 "|VmxLbr"
103#endif
104 ,
105 "" /* pszValidNodes */, "NEM" /* pszWho */, 0 /* uInstance */);
106 if (RT_FAILURE(rc))
107 return rc;
108
109 /** @cfgm{/NEM/NEMEnabled, bool, true}
110 * Whether NEM is enabled. */
111 rc = CFGMR3QueryBoolDef(pCfgNem, "Enabled", &pVM->nem.s.fEnabled, true);
112 AssertLogRelRCReturn(rc, rc);
113
114
115#ifdef VBOX_WITH_64_BITS_GUESTS
116 /** @cfgm{/NEM/Allow64BitGuests, bool, 32-bit:false, 64-bit:true}
117 * Enables AMD64 CPU features.
118 * On 32-bit hosts this isn't default and require host CPU support. 64-bit hosts
119 * already have the support. */
120 rc = CFGMR3QueryBoolDef(pCfgNem, "Allow64BitGuests", &pVM->nem.s.fAllow64BitGuests, HC_ARCH_BITS == 64);
121 AssertLogRelRCReturn(rc, rc);
122#else
123 pVM->nem.s.fAllow64BitGuests = false;
124#endif
125
126 /** @cfgm{/NEM/LovelyMesaDrvWorkaround, bool, false}
127 * Workaround for mesa vmsvga 3d driver making incorrect assumptions about
128 * the hypervisor it is running under. */
129 bool f;
130 rc = CFGMR3QueryBoolDef(pCfgNem, "LovelyMesaDrvWorkaround", &f, false);
131 AssertLogRelRCReturn(rc, rc);
132 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
133 {
134 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
135 pVCpu->nem.s.fTrapXcptGpForLovelyMesaDrv = f;
136 }
137
138 return VINF_SUCCESS;
139}
140
141
142/**
143 * This is called by HMR3Init() when HM cannot be used.
144 *
145 * Sets VM::bMainExecutionEngine to VM_EXEC_ENGINE_NATIVE_API if we can use a
146 * native hypervisor API to execute the VM.
147 *
148 * @returns VBox status code.
149 * @param pVM The cross context VM structure.
150 * @param fFallback Whether this is a fallback call. Cleared if the VM is
151 * configured to use NEM instead of HM.
152 * @param fForced Whether /HM/HMForced was set. If set and we fail to
153 * enable NEM, we'll return a failure status code.
154 * Otherwise we'll assume HMR3Init falls back on raw-mode.
155 */
156VMMR3_INT_DECL(int) NEMR3Init(PVM pVM, bool fFallback, bool fForced)
157{
158 Assert(pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API);
159 int rc;
160 if (pVM->nem.s.fEnabled)
161 {
162#ifdef VBOX_WITH_NATIVE_NEM
163 rc = nemR3NativeInit(pVM, fFallback, fForced);
164 ASMCompilerBarrier(); /* May have changed bMainExecutionEngine. */
165#else
166 RT_NOREF(fFallback);
167 rc = VINF_SUCCESS;
168#endif
169 if (RT_SUCCESS(rc))
170 {
171 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
172 {
173#ifdef RT_OS_WINDOWS /* The WHv* API is extremely slow at handling VM exits. The AppleHv and
174 KVM APIs are much faster, thus the different mode name. :-) */
175 LogRel(("NEM:\n"
176 "NEM: NEMR3Init: Snail execution mode is active!\n"
177 "NEM: Note! VirtualBox is not able to run at its full potential in this execution mode.\n"
178 "NEM: To see VirtualBox run at max speed you need to disable all Windows features\n"
179 "NEM: making use of Hyper-V. That is a moving target, so google how and carefully\n"
180 "NEM: consider the consequences of disabling these features.\n"
181 "NEM:\n"));
182#else
183 LogRel(("NEM:\n"
184 "NEM: NEMR3Init: Turtle execution mode is active!\n"
185 "NEM: Note! VirtualBox is not able to run at its full potential in this execution mode.\n"
186 "NEM:\n"));
187#endif
188 }
189 else
190 {
191 LogRel(("NEM: NEMR3Init: Not available.\n"));
192 if (fForced)
193 rc = VERR_NEM_NOT_AVAILABLE;
194 }
195 }
196 else
197 LogRel(("NEM: NEMR3Init: Native init failed: %Rrc.\n", rc));
198 }
199 else
200 {
201 LogRel(("NEM: NEMR3Init: Disabled.\n"));
202 rc = fForced ? VERR_NEM_NOT_ENABLED : VINF_SUCCESS;
203 }
204 return rc;
205}
206
207
208/**
209 * Perform initialization that depends on CPUM working.
210 *
211 * This is a noop if NEM wasn't activated by a previous NEMR3Init() call.
212 *
213 * @returns VBox status code.
214 * @param pVM The cross context VM structure.
215 */
216VMMR3_INT_DECL(int) NEMR3InitAfterCPUM(PVM pVM)
217{
218 int rc = VINF_SUCCESS;
219 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
220 {
221 /*
222 * Do native after-CPUM init.
223 */
224#ifdef VBOX_WITH_NATIVE_NEM
225 rc = nemR3NativeInitAfterCPUM(pVM);
226#else
227 RT_NOREF(pVM);
228#endif
229 }
230 return rc;
231}
232
233
234/**
235 * Called when a init phase has completed.
236 *
237 * @returns VBox status code.
238 * @param pVM The cross context VM structure.
239 * @param enmWhat The phase that completed.
240 */
241VMMR3_INT_DECL(int) NEMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
242{
243 /*
244 * Check if GIM needs #UD, since that applies to everyone.
245 */
246 if (enmWhat == VMINITCOMPLETED_RING3)
247 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
248 {
249 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
250 pVCpu->nem.s.fGIMTrapXcptUD = GIMShouldTrapXcptUD(pVCpu);
251 }
252
253 /*
254 * Call native code.
255 */
256 int rc = VINF_SUCCESS;
257#ifdef VBOX_WITH_NATIVE_NEM
258 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
259 rc = nemR3NativeInitCompleted(pVM, enmWhat);
260#else
261 RT_NOREF(pVM, enmWhat);
262#endif
263 return rc;
264}
265
266
267/**
268 *
269 * @returns VBox status code.
270 * @param pVM The cross context VM structure.
271 */
272VMMR3_INT_DECL(int) NEMR3Term(PVM pVM)
273{
274 AssertReturn(pVM->nem.s.u32Magic == NEM_MAGIC, VERR_WRONG_ORDER);
275 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
276 AssertReturn(pVM->apCpusR3[idCpu]->nem.s.u32Magic == NEMCPU_MAGIC, VERR_WRONG_ORDER);
277
278 /* Do native termination. */
279 int rc = VINF_SUCCESS;
280#ifdef VBOX_WITH_NATIVE_NEM
281 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
282 rc = nemR3NativeTerm(pVM);
283#endif
284
285 /* Mark it as terminated. */
286 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
287 {
288 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
289 pVCpu->nem.s.u32Magic = NEMCPU_MAGIC_DEAD;
290 }
291 pVM->nem.s.u32Magic = NEM_MAGIC_DEAD;
292 return rc;
293}
294
295/**
296 * External interface for querying whether native execution API is used.
297 *
298 * @returns true if NEM is being used, otherwise false.
299 * @param pUVM The user mode VM handle.
300 * @sa HMR3IsEnabled
301 */
302VMMR3DECL(bool) NEMR3IsEnabled(PUVM pUVM)
303{
304 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
305 PVM pVM = pUVM->pVM;
306 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
307 return VM_IS_NEM_ENABLED(pVM);
308}
309
310
311/**
312 * The VM is being reset.
313 *
314 * @param pVM The cross context VM structure.
315 */
316VMMR3_INT_DECL(void) NEMR3Reset(PVM pVM)
317{
318#ifdef VBOX_WITH_NATIVE_NEM
319 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
320 nemR3NativeReset(pVM);
321#else
322 RT_NOREF(pVM);
323#endif
324}
325
326
327/**
328 * Resets a virtual CPU.
329 *
330 * Used to bring up secondary CPUs on SMP as well as CPU hot plugging.
331 *
332 * @param pVCpu The cross context virtual CPU structure to reset.
333 * @param fInitIpi Set if being reset due to INIT IPI.
334 */
335VMMR3_INT_DECL(void) NEMR3ResetCpu(PVMCPU pVCpu, bool fInitIpi)
336{
337#ifdef VBOX_WITH_NATIVE_NEM
338 if (pVCpu->pVMR3->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
339 nemR3NativeResetCpu(pVCpu, fInitIpi);
340#else
341 RT_NOREF(pVCpu, fInitIpi);
342#endif
343}
344
345
346/**
347 * Indicates to TM that TMTSCMODE_NATIVE_API should be used for TSC.
348 *
349 * @returns true if TMTSCMODE_NATIVE_API must be used, otherwise @c false.
350 * @param pVM The cross context VM structure.
351 */
352VMMR3_INT_DECL(bool) NEMR3NeedSpecialTscMode(PVM pVM)
353{
354#ifdef VBOX_WITH_NATIVE_NEM
355 if (VM_IS_NEM_ENABLED(pVM))
356 return true;
357#else
358 RT_NOREF(pVM);
359#endif
360 return false;
361}
362
363
364/**
365 * Gets the name of a generic NEM exit code.
366 *
367 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
368 * @param uExit The NEM exit to name.
369 */
370VMMR3DECL(const char *) NEMR3GetExitName(uint32_t uExit)
371{
372 switch ((NEMEXITTYPE)uExit)
373 {
374 case NEMEXITTYPE_INTTERRUPT_WINDOW: return "NEM interrupt window";
375 case NEMEXITTYPE_HALT: return "NEM halt";
376
377 case NEMEXITTYPE_UNRECOVERABLE_EXCEPTION: return "NEM unrecoverable exception";
378 case NEMEXITTYPE_INVALID_VP_REGISTER_VALUE: return "NEM invalid vp register value";
379 case NEMEXITTYPE_XCPT_UD: return "NEM #UD";
380 case NEMEXITTYPE_XCPT_DB: return "NEM #DB";
381 case NEMEXITTYPE_XCPT_BP: return "NEM #BP";
382 case NEMEXITTYPE_CANCELED: return "NEM canceled";
383 case NEMEXITTYPE_MEMORY_ACCESS: return "NEM memory access";
384
385 case NEMEXITTYPE_INTERNAL_ERROR_EMULATION: return "NEM emulation IPE";
386 case NEMEXITTYPE_INTERNAL_ERROR_FATAL: return "NEM fatal IPE";
387 case NEMEXITTYPE_INTERRUPTED: return "NEM interrupted";
388 case NEMEXITTYPE_FAILED_ENTRY: return "NEM failed VT-x/AMD-V entry";
389
390 case NEMEXITTYPE_INVALID:
391 case NEMEXITTYPE_END:
392 break;
393 }
394
395 return NULL;
396}
397
398
399VMMR3_INT_DECL(VBOXSTRICTRC) NEMR3RunGC(PVM pVM, PVMCPU pVCpu)
400{
401 Assert(VM_IS_NEM_ENABLED(pVM));
402#ifdef VBOX_WITH_NATIVE_NEM
403 return nemR3NativeRunGC(pVM, pVCpu);
404#else
405 NOREF(pVM); NOREF(pVCpu);
406 return VERR_INTERNAL_ERROR_3;
407#endif
408}
409
410
411#ifndef VBOX_WITH_NATIVE_NEM
412VMMR3_INT_DECL(bool) NEMR3CanExecuteGuest(PVM pVM, PVMCPU pVCpu)
413{
414 RT_NOREF(pVM, pVCpu);
415 return false;
416}
417#endif
418
419
420VMMR3_INT_DECL(bool) NEMR3SetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
421{
422 Assert(VM_IS_NEM_ENABLED(pVM));
423#ifdef VBOX_WITH_NATIVE_NEM
424 return nemR3NativeSetSingleInstruction(pVM, pVCpu, fEnable);
425#else
426 NOREF(pVM); NOREF(pVCpu); NOREF(fEnable);
427 return false;
428#endif
429}
430
431
432VMMR3_INT_DECL(void) NEMR3NotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
433{
434 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
435#ifdef VBOX_WITH_NATIVE_NEM
436 nemR3NativeNotifyFF(pVM, pVCpu, fFlags);
437#else
438 RT_NOREF(pVM, pVCpu, fFlags);
439#endif
440}
441
442#ifndef VBOX_WITH_NATIVE_NEM
443
444VMMR3_INT_DECL(void) NEMR3NotifySetA20(PVMCPU pVCpu, bool fEnabled)
445{
446 RT_NOREF(pVCpu, fEnabled);
447}
448
449# ifdef VBOX_WITH_PGM_NEM_MODE
450
451VMMR3_INT_DECL(bool) NEMR3IsMmio2DirtyPageTrackingSupported(PVM pVM)
452{
453 RT_NOREF(pVM);
454 return false;
455}
456
457
458VMMR3_INT_DECL(int) NEMR3PhysMmio2QueryAndResetDirtyBitmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t uNemRange,
459 void *pvBitmap, size_t cbBitmap)
460{
461 RT_NOREF(pVM, GCPhys, cb, uNemRange, pvBitmap, cbBitmap);
462 AssertFailed();
463 return VERR_INTERNAL_ERROR_2;
464}
465
466
467VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMapEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags,
468 void *pvRam, void *pvMmio2, uint8_t *pu2State, uint32_t *puNemRange)
469{
470 RT_NOREF(pVM, GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State, puNemRange);
471 AssertFailed();
472 return VERR_INTERNAL_ERROR_2;
473}
474
475# endif /* VBOX_WITH_PGM_NEM_MODE */
476#endif /* !VBOX_WITH_NATIVE_NEM */
477
478/**
479 * Notification callback from DBGF when interrupt breakpoints or generic debug
480 * event settings changes.
481 *
482 * DBGF will call NEMR3NotifyDebugEventChangedPerCpu on each CPU afterwards, this
483 * function is just updating the VM globals.
484 *
485 * @param pVM The VM cross context VM structure.
486 * @thread EMT(0)
487 */
488VMMR3_INT_DECL(void) NEMR3NotifyDebugEventChanged(PVM pVM)
489{
490 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
491
492#ifdef VBOX_WITH_NATIVE_NEM
493 /* Interrupts. */
494 bool fUseDebugLoop = pVM->dbgf.ro.cSoftIntBreakpoints > 0
495 || pVM->dbgf.ro.cHardIntBreakpoints > 0;
496
497 /* CPU Exceptions. */
498 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_XCPT_FIRST;
499 !fUseDebugLoop && enmEvent <= DBGFEVENT_XCPT_LAST;
500 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
501 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
502
503 /* Common VM exits. */
504 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_FIRST;
505 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_LAST_COMMON;
506 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
507 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
508
509 /* Done. */
510 pVM->nem.s.fUseDebugLoop = nemR3NativeNotifyDebugEventChanged(pVM, fUseDebugLoop);
511#else
512 RT_NOREF(pVM);
513#endif
514}
515
516
517/**
518 * Follow up notification callback to NEMR3NotifyDebugEventChanged for each CPU.
519 *
520 * NEM uses this to combine the decision made NEMR3NotifyDebugEventChanged with
521 * per CPU settings.
522 *
523 * @param pVM The VM cross context VM structure.
524 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
525 */
526VMMR3_INT_DECL(void) NEMR3NotifyDebugEventChangedPerCpu(PVM pVM, PVMCPU pVCpu)
527{
528 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
529
530#ifdef VBOX_WITH_NATIVE_NEM
531 pVCpu->nem.s.fUseDebugLoop = nemR3NativeNotifyDebugEventChangedPerCpu(pVM, pVCpu,
532 pVCpu->nem.s.fSingleInstruction | pVM->nem.s.fUseDebugLoop);
533#else
534 RT_NOREF(pVM, pVCpu);
535#endif
536}
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