VirtualBox

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

Last change on this file since 97175 was 97175, checked in by vboxsync, 2 years ago

VMM/NEMR3Native-darwin: Addendum for r154132, unmapping doesn't unwedge the map. Check whether it would be possible to make it work when we set g_fAppleHvNoWX upfront by determining the macOS version, bugref:9044

  • 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 97175 2022-10-17 17:47:19Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager.
4 */
5
6/*
7 * Copyright (C) 2018-2022 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 "|CatalinaWxWorkaround"
104#endif
105 ,
106 "" /* pszValidNodes */, "NEM" /* pszWho */, 0 /* uInstance */);
107 if (RT_FAILURE(rc))
108 return rc;
109
110 /** @cfgm{/NEM/NEMEnabled, bool, true}
111 * Whether NEM is enabled. */
112 rc = CFGMR3QueryBoolDef(pCfgNem, "Enabled", &pVM->nem.s.fEnabled, true);
113 AssertLogRelRCReturn(rc, rc);
114
115
116#ifdef VBOX_WITH_64_BITS_GUESTS
117 /** @cfgm{/NEM/Allow64BitGuests, bool, 32-bit:false, 64-bit:true}
118 * Enables AMD64 CPU features.
119 * On 32-bit hosts this isn't default and require host CPU support. 64-bit hosts
120 * already have the support. */
121 rc = CFGMR3QueryBoolDef(pCfgNem, "Allow64BitGuests", &pVM->nem.s.fAllow64BitGuests, HC_ARCH_BITS == 64);
122 AssertLogRelRCReturn(rc, rc);
123#else
124 pVM->nem.s.fAllow64BitGuests = false;
125#endif
126
127 /** @cfgm{/NEM/LovelyMesaDrvWorkaround, bool, false}
128 * Workaround for mesa vmsvga 3d driver making incorrect assumptions about
129 * the hypervisor it is running under. */
130 bool f;
131 rc = CFGMR3QueryBoolDef(pCfgNem, "LovelyMesaDrvWorkaround", &f, false);
132 AssertLogRelRCReturn(rc, rc);
133 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
134 {
135 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
136 pVCpu->nem.s.fTrapXcptGpForLovelyMesaDrv = f;
137 }
138
139 return VINF_SUCCESS;
140}
141
142
143/**
144 * This is called by HMR3Init() when HM cannot be used.
145 *
146 * Sets VM::bMainExecutionEngine to VM_EXEC_ENGINE_NATIVE_API if we can use a
147 * native hypervisor API to execute the VM.
148 *
149 * @returns VBox status code.
150 * @param pVM The cross context VM structure.
151 * @param fFallback Whether this is a fallback call. Cleared if the VM is
152 * configured to use NEM instead of HM.
153 * @param fForced Whether /HM/HMForced was set. If set and we fail to
154 * enable NEM, we'll return a failure status code.
155 * Otherwise we'll assume HMR3Init falls back on raw-mode.
156 */
157VMMR3_INT_DECL(int) NEMR3Init(PVM pVM, bool fFallback, bool fForced)
158{
159 Assert(pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API);
160 int rc;
161 if (pVM->nem.s.fEnabled)
162 {
163#ifdef VBOX_WITH_NATIVE_NEM
164 rc = nemR3NativeInit(pVM, fFallback, fForced);
165 ASMCompilerBarrier(); /* May have changed bMainExecutionEngine. */
166#else
167 RT_NOREF(fFallback);
168 rc = VINF_SUCCESS;
169#endif
170 if (RT_SUCCESS(rc))
171 {
172 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
173 {
174#ifdef RT_OS_WINDOWS /* The WHv* API is extremely slow at handling VM exits. The AppleHv and
175 KVM APIs are much faster, thus the different mode name. :-) */
176 LogRel(("NEM:\n"
177 "NEM: NEMR3Init: Snail execution mode is active!\n"
178 "NEM: Note! VirtualBox is not able to run at its full potential in this execution mode.\n"
179 "NEM: To see VirtualBox run at max speed you need to disable all Windows features\n"
180 "NEM: making use of Hyper-V. That is a moving target, so google how and carefully\n"
181 "NEM: consider the consequences of disabling these features.\n"
182 "NEM:\n"));
183#else
184 LogRel(("NEM:\n"
185 "NEM: NEMR3Init: Turtle execution mode is active!\n"
186 "NEM: Note! VirtualBox is not able to run at its full potential in this execution mode.\n"
187 "NEM:\n"));
188#endif
189 }
190 else
191 {
192 LogRel(("NEM: NEMR3Init: Not available.\n"));
193 if (fForced)
194 rc = VERR_NEM_NOT_AVAILABLE;
195 }
196 }
197 else
198 LogRel(("NEM: NEMR3Init: Native init failed: %Rrc.\n", rc));
199 }
200 else
201 {
202 LogRel(("NEM: NEMR3Init: Disabled.\n"));
203 rc = fForced ? VERR_NEM_NOT_ENABLED : VINF_SUCCESS;
204 }
205 return rc;
206}
207
208
209/**
210 * Perform initialization that depends on CPUM working.
211 *
212 * This is a noop if NEM wasn't activated by a previous NEMR3Init() call.
213 *
214 * @returns VBox status code.
215 * @param pVM The cross context VM structure.
216 */
217VMMR3_INT_DECL(int) NEMR3InitAfterCPUM(PVM pVM)
218{
219 int rc = VINF_SUCCESS;
220 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
221 {
222 /*
223 * Do native after-CPUM init.
224 */
225#ifdef VBOX_WITH_NATIVE_NEM
226 rc = nemR3NativeInitAfterCPUM(pVM);
227#else
228 RT_NOREF(pVM);
229#endif
230 }
231 return rc;
232}
233
234
235/**
236 * Called when a init phase has completed.
237 *
238 * @returns VBox status code.
239 * @param pVM The cross context VM structure.
240 * @param enmWhat The phase that completed.
241 */
242VMMR3_INT_DECL(int) NEMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
243{
244 /*
245 * Check if GIM needs #UD, since that applies to everyone.
246 */
247 if (enmWhat == VMINITCOMPLETED_RING3)
248 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
249 {
250 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
251 pVCpu->nem.s.fGIMTrapXcptUD = GIMShouldTrapXcptUD(pVCpu);
252 }
253
254 /*
255 * Call native code.
256 */
257 int rc = VINF_SUCCESS;
258#ifdef VBOX_WITH_NATIVE_NEM
259 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
260 rc = nemR3NativeInitCompleted(pVM, enmWhat);
261#else
262 RT_NOREF(pVM, enmWhat);
263#endif
264 return rc;
265}
266
267
268/**
269 *
270 * @returns VBox status code.
271 * @param pVM The cross context VM structure.
272 */
273VMMR3_INT_DECL(int) NEMR3Term(PVM pVM)
274{
275 AssertReturn(pVM->nem.s.u32Magic == NEM_MAGIC, VERR_WRONG_ORDER);
276 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
277 AssertReturn(pVM->apCpusR3[idCpu]->nem.s.u32Magic == NEMCPU_MAGIC, VERR_WRONG_ORDER);
278
279 /* Do native termination. */
280 int rc = VINF_SUCCESS;
281#ifdef VBOX_WITH_NATIVE_NEM
282 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
283 rc = nemR3NativeTerm(pVM);
284#endif
285
286 /* Mark it as terminated. */
287 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
288 {
289 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
290 pVCpu->nem.s.u32Magic = NEMCPU_MAGIC_DEAD;
291 }
292 pVM->nem.s.u32Magic = NEM_MAGIC_DEAD;
293 return rc;
294}
295
296/**
297 * External interface for querying whether native execution API is used.
298 *
299 * @returns true if NEM is being used, otherwise false.
300 * @param pUVM The user mode VM handle.
301 * @sa HMR3IsEnabled
302 */
303VMMR3DECL(bool) NEMR3IsEnabled(PUVM pUVM)
304{
305 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
306 PVM pVM = pUVM->pVM;
307 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
308 return VM_IS_NEM_ENABLED(pVM);
309}
310
311
312/**
313 * The VM is being reset.
314 *
315 * @param pVM The cross context VM structure.
316 */
317VMMR3_INT_DECL(void) NEMR3Reset(PVM pVM)
318{
319#ifdef VBOX_WITH_NATIVE_NEM
320 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
321 nemR3NativeReset(pVM);
322#else
323 RT_NOREF(pVM);
324#endif
325}
326
327
328/**
329 * Resets a virtual CPU.
330 *
331 * Used to bring up secondary CPUs on SMP as well as CPU hot plugging.
332 *
333 * @param pVCpu The cross context virtual CPU structure to reset.
334 * @param fInitIpi Set if being reset due to INIT IPI.
335 */
336VMMR3_INT_DECL(void) NEMR3ResetCpu(PVMCPU pVCpu, bool fInitIpi)
337{
338#ifdef VBOX_WITH_NATIVE_NEM
339 if (pVCpu->pVMR3->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
340 nemR3NativeResetCpu(pVCpu, fInitIpi);
341#else
342 RT_NOREF(pVCpu, fInitIpi);
343#endif
344}
345
346
347/**
348 * Indicates to TM that TMTSCMODE_NATIVE_API should be used for TSC.
349 *
350 * @returns true if TMTSCMODE_NATIVE_API must be used, otherwise @c false.
351 * @param pVM The cross context VM structure.
352 */
353VMMR3_INT_DECL(bool) NEMR3NeedSpecialTscMode(PVM pVM)
354{
355#ifdef VBOX_WITH_NATIVE_NEM
356 if (VM_IS_NEM_ENABLED(pVM))
357 return true;
358#else
359 RT_NOREF(pVM);
360#endif
361 return false;
362}
363
364
365/**
366 * Gets the name of a generic NEM exit code.
367 *
368 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
369 * @param uExit The NEM exit to name.
370 */
371VMMR3DECL(const char *) NEMR3GetExitName(uint32_t uExit)
372{
373 switch ((NEMEXITTYPE)uExit)
374 {
375 case NEMEXITTYPE_INTTERRUPT_WINDOW: return "NEM interrupt window";
376 case NEMEXITTYPE_HALT: return "NEM halt";
377
378 case NEMEXITTYPE_UNRECOVERABLE_EXCEPTION: return "NEM unrecoverable exception";
379 case NEMEXITTYPE_INVALID_VP_REGISTER_VALUE: return "NEM invalid vp register value";
380 case NEMEXITTYPE_XCPT_UD: return "NEM #UD";
381 case NEMEXITTYPE_XCPT_DB: return "NEM #DB";
382 case NEMEXITTYPE_XCPT_BP: return "NEM #BP";
383 case NEMEXITTYPE_CANCELED: return "NEM canceled";
384 case NEMEXITTYPE_MEMORY_ACCESS: return "NEM memory access";
385
386 case NEMEXITTYPE_INTERNAL_ERROR_EMULATION: return "NEM emulation IPE";
387 case NEMEXITTYPE_INTERNAL_ERROR_FATAL: return "NEM fatal IPE";
388 case NEMEXITTYPE_INTERRUPTED: return "NEM interrupted";
389 case NEMEXITTYPE_FAILED_ENTRY: return "NEM failed VT-x/AMD-V entry";
390
391 case NEMEXITTYPE_INVALID:
392 case NEMEXITTYPE_END:
393 break;
394 }
395
396 return NULL;
397}
398
399
400VMMR3_INT_DECL(VBOXSTRICTRC) NEMR3RunGC(PVM pVM, PVMCPU pVCpu)
401{
402 Assert(VM_IS_NEM_ENABLED(pVM));
403#ifdef VBOX_WITH_NATIVE_NEM
404 return nemR3NativeRunGC(pVM, pVCpu);
405#else
406 NOREF(pVM); NOREF(pVCpu);
407 return VERR_INTERNAL_ERROR_3;
408#endif
409}
410
411
412#ifndef VBOX_WITH_NATIVE_NEM
413VMMR3_INT_DECL(bool) NEMR3CanExecuteGuest(PVM pVM, PVMCPU pVCpu)
414{
415 RT_NOREF(pVM, pVCpu);
416 return false;
417}
418#endif
419
420
421VMMR3_INT_DECL(bool) NEMR3SetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
422{
423 Assert(VM_IS_NEM_ENABLED(pVM));
424#ifdef VBOX_WITH_NATIVE_NEM
425 return nemR3NativeSetSingleInstruction(pVM, pVCpu, fEnable);
426#else
427 NOREF(pVM); NOREF(pVCpu); NOREF(fEnable);
428 return false;
429#endif
430}
431
432
433VMMR3_INT_DECL(void) NEMR3NotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
434{
435 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
436#ifdef VBOX_WITH_NATIVE_NEM
437 nemR3NativeNotifyFF(pVM, pVCpu, fFlags);
438#else
439 RT_NOREF(pVM, pVCpu, fFlags);
440#endif
441}
442
443#ifndef VBOX_WITH_NATIVE_NEM
444
445VMMR3_INT_DECL(void) NEMR3NotifySetA20(PVMCPU pVCpu, bool fEnabled)
446{
447 RT_NOREF(pVCpu, fEnabled);
448}
449
450# ifdef VBOX_WITH_PGM_NEM_MODE
451
452VMMR3_INT_DECL(bool) NEMR3IsMmio2DirtyPageTrackingSupported(PVM pVM)
453{
454 RT_NOREF(pVM);
455 return false;
456}
457
458
459VMMR3_INT_DECL(int) NEMR3PhysMmio2QueryAndResetDirtyBitmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t uNemRange,
460 void *pvBitmap, size_t cbBitmap)
461{
462 RT_NOREF(pVM, GCPhys, cb, uNemRange, pvBitmap, cbBitmap);
463 AssertFailed();
464 return VERR_INTERNAL_ERROR_2;
465}
466
467
468VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMapEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags,
469 void *pvRam, void *pvMmio2, uint8_t *pu2State, uint32_t *puNemRange)
470{
471 RT_NOREF(pVM, GCPhys, cb, fFlags, pvRam, pvMmio2, pu2State, puNemRange);
472 AssertFailed();
473 return VERR_INTERNAL_ERROR_2;
474}
475
476# endif /* VBOX_WITH_PGM_NEM_MODE */
477#endif /* !VBOX_WITH_NATIVE_NEM */
478
479/**
480 * Notification callback from DBGF when interrupt breakpoints or generic debug
481 * event settings changes.
482 *
483 * DBGF will call NEMR3NotifyDebugEventChangedPerCpu on each CPU afterwards, this
484 * function is just updating the VM globals.
485 *
486 * @param pVM The VM cross context VM structure.
487 * @thread EMT(0)
488 */
489VMMR3_INT_DECL(void) NEMR3NotifyDebugEventChanged(PVM pVM)
490{
491 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
492
493#ifdef VBOX_WITH_NATIVE_NEM
494 /* Interrupts. */
495 bool fUseDebugLoop = pVM->dbgf.ro.cSoftIntBreakpoints > 0
496 || pVM->dbgf.ro.cHardIntBreakpoints > 0;
497
498 /* CPU Exceptions. */
499 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_XCPT_FIRST;
500 !fUseDebugLoop && enmEvent <= DBGFEVENT_XCPT_LAST;
501 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
502 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
503
504 /* Common VM exits. */
505 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_FIRST;
506 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_LAST_COMMON;
507 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
508 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
509
510 /* Done. */
511 pVM->nem.s.fUseDebugLoop = nemR3NativeNotifyDebugEventChanged(pVM, fUseDebugLoop);
512#else
513 RT_NOREF(pVM);
514#endif
515}
516
517
518/**
519 * Follow up notification callback to NEMR3NotifyDebugEventChanged for each CPU.
520 *
521 * NEM uses this to combine the decision made NEMR3NotifyDebugEventChanged with
522 * per CPU settings.
523 *
524 * @param pVM The VM cross context VM structure.
525 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
526 */
527VMMR3_INT_DECL(void) NEMR3NotifyDebugEventChangedPerCpu(PVM pVM, PVMCPU pVCpu)
528{
529 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
530
531#ifdef VBOX_WITH_NATIVE_NEM
532 pVCpu->nem.s.fUseDebugLoop = nemR3NativeNotifyDebugEventChangedPerCpu(pVM, pVCpu,
533 pVCpu->nem.s.fSingleInstruction | pVM->nem.s.fUseDebugLoop);
534#else
535 RT_NOREF(pVM, pVCpu);
536#endif
537}
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