VirtualBox

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

Last change on this file since 80333 was 80333, checked in by vboxsync, 5 years ago

VMM: Eliminating the VBOX_BUGREF_9217_PART_I preprocessor macro. bugref:9217

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 KB
Line 
1/* $Id: NEMR3.cpp 80333 2019-08-16 20:28:38Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager.
4 */
5
6/*
7 * Copyright (C) 2018-2019 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/** @page pg_nem NEM - Native Execution Manager.
19 *
20 * This is an alternative execution manage to HM and raw-mode. On one host
21 * (Windows) we're forced to use this, on the others we just do it because we
22 * can. Since this is host specific in nature, information about an
23 * implementation is contained in the NEMR3Native-xxxx.cpp files.
24 *
25 * @ref pg_nem_win
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_NEM
33#include <VBox/vmm/nem.h>
34#include <VBox/vmm/gim.h>
35#include "NEMInternal.h"
36#include <VBox/vmm/vm.h>
37#include <VBox/vmm/uvm.h>
38#include <VBox/err.h>
39
40#include <iprt/asm.h>
41
42
43
44/**
45 * Basic init and configuration reading.
46 *
47 * Always call NEMR3Term after calling this.
48 *
49 * @returns VBox status code.
50 * @param pVM The cross context VM structure.
51 */
52VMMR3_INT_DECL(int) NEMR3InitConfig(PVM pVM)
53{
54 LogFlow(("NEMR3Init\n"));
55
56 /*
57 * Assert alignment and sizes.
58 */
59 AssertCompileMemberAlignment(VM, nem.s, 64);
60 AssertCompile(sizeof(pVM->nem.s) <= sizeof(pVM->nem.padding));
61
62 /*
63 * Initialize state info so NEMR3Term will always be happy.
64 * No returning prior to setting magics!
65 */
66 pVM->nem.s.u32Magic = NEM_MAGIC;
67 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
68 {
69 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
70 pVCpu->nem.s.u32Magic = NEMCPU_MAGIC;
71 }
72
73 /*
74 * Read configuration.
75 */
76 PCFGMNODE pCfgNem = CFGMR3GetChild(CFGMR3GetRoot(pVM), "NEM/");
77
78 /*
79 * Validate the NEM settings.
80 */
81 int rc = CFGMR3ValidateConfig(pCfgNem,
82 "/NEM/",
83 "Enabled"
84 "|Allow64BitGuests"
85#ifdef RT_OS_WINDOWS
86 "|UseRing0Runloop"
87#endif
88 ,
89 "" /* pszValidNodes */, "NEM" /* pszWho */, 0 /* uInstance */);
90 if (RT_FAILURE(rc))
91 return rc;
92
93 /** @cfgm{/NEM/NEMEnabled, bool, true}
94 * Whether NEM is enabled. */
95 rc = CFGMR3QueryBoolDef(pCfgNem, "Enabled", &pVM->nem.s.fEnabled, true);
96 AssertLogRelRCReturn(rc, rc);
97
98
99#ifdef VBOX_WITH_64_BITS_GUESTS
100 /** @cfgm{/NEM/Allow64BitGuests, bool, 32-bit:false, 64-bit:true}
101 * Enables AMD64 CPU features.
102 * On 32-bit hosts this isn't default and require host CPU support. 64-bit hosts
103 * already have the support. */
104 rc = CFGMR3QueryBoolDef(pCfgNem, "Allow64BitGuests", &pVM->nem.s.fAllow64BitGuests, HC_ARCH_BITS == 64);
105 AssertLogRelRCReturn(rc, rc);
106#else
107 pVM->nem.s.fAllow64BitGuests = false;
108#endif
109
110#ifdef RT_OS_WINDOWS
111 /** @cfgm{/NEM/UseRing0Runloop, bool, true}
112 * Whether to use the ring-0 runloop (if enabled in the build) or the ring-3 one.
113 * The latter is generally slower. This option serves as a way out in case
114 * something breaks in the ring-0 loop. */
115# ifdef NEM_WIN_USE_RING0_RUNLOOP_BY_DEFAULT
116 bool fUseRing0Runloop = true;
117# else
118 bool fUseRing0Runloop = false;
119# endif
120 rc = CFGMR3QueryBoolDef(pCfgNem, "UseRing0Runloop", &fUseRing0Runloop, fUseRing0Runloop);
121 AssertLogRelRCReturn(rc, rc);
122 pVM->nem.s.fUseRing0Runloop = fUseRing0Runloop;
123#endif
124
125 return VINF_SUCCESS;
126}
127
128
129/**
130 * This is called by HMR3Init() when HM cannot be used.
131 *
132 * Sets VM::bMainExecutionEngine to VM_EXEC_ENGINE_NATIVE_API if we can use a
133 * native hypervisor API to execute the VM.
134 *
135 * @returns VBox status code.
136 * @param pVM The cross context VM structure.
137 * @param fFallback Whether this is a fallback call. Cleared if the VM is
138 * configured to use NEM instead of HM.
139 * @param fForced Whether /HM/HMForced was set. If set and we fail to
140 * enable NEM, we'll return a failure status code.
141 * Otherwise we'll assume HMR3Init falls back on raw-mode.
142 */
143VMMR3_INT_DECL(int) NEMR3Init(PVM pVM, bool fFallback, bool fForced)
144{
145 Assert(pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API);
146 int rc;
147 if (pVM->nem.s.fEnabled)
148 {
149#ifdef VBOX_WITH_NATIVE_NEM
150 rc = nemR3NativeInit(pVM, fFallback, fForced);
151 ASMCompilerBarrier(); /* May have changed bMainExecutionEngine. */
152#else
153 RT_NOREF(fFallback);
154 rc = VINF_SUCCESS;
155#endif
156 if (RT_SUCCESS(rc))
157 {
158 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
159 LogRel(("NEM: NEMR3Init: Active.\n"));
160 else
161 {
162 LogRel(("NEM: NEMR3Init: Not available.\n"));
163 if (fForced)
164 rc = VERR_NEM_NOT_AVAILABLE;
165 }
166 }
167 else
168 LogRel(("NEM: NEMR3Init: Native init failed: %Rrc.\n", rc));
169 }
170 else
171 {
172 LogRel(("NEM: NEMR3Init: Disabled.\n"));
173 rc = fForced ? VERR_NEM_NOT_ENABLED : VINF_SUCCESS;
174 }
175 return rc;
176}
177
178
179/**
180 * Perform initialization that depends on CPUM working.
181 *
182 * This is a noop if NEM wasn't activated by a previous NEMR3Init() call.
183 *
184 * @returns VBox status code.
185 * @param pVM The cross context VM structure.
186 */
187VMMR3_INT_DECL(int) NEMR3InitAfterCPUM(PVM pVM)
188{
189 int rc = VINF_SUCCESS;
190 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
191 {
192 /*
193 * Enable CPU features making general ASSUMPTIONS (there are two similar
194 * blocks of code in HM.cpp), to avoid duplicating this code. The
195 * native backend can make check capabilities and adjust as needed.
196 */
197 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
198 if (CPUMGetGuestCpuVendor(pVM) == CPUMCPUVENDOR_AMD)
199 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL); /* 64 bits only on Intel CPUs */
200 if (pVM->nem.s.fAllow64BitGuests)
201 {
202 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL);
203 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
204 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
205 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
206 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
207 }
208 /* Turn on NXE if PAE has been enabled. */
209 else if (CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
210 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
211
212 /*
213 * Do native after-CPUM init.
214 */
215#ifdef VBOX_WITH_NATIVE_NEM
216 rc = nemR3NativeInitAfterCPUM(pVM);
217#else
218 RT_NOREF(pVM);
219#endif
220 }
221 return rc;
222}
223
224
225/**
226 * Called when a init phase has completed.
227 *
228 * @returns VBox status code.
229 * @param pVM The cross context VM structure.
230 * @param enmWhat The phase that completed.
231 */
232VMMR3_INT_DECL(int) NEMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
233{
234 /*
235 * Check if GIM needs #UD, since that applies to everyone.
236 */
237 if (enmWhat == VMINITCOMPLETED_RING3)
238 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
239 {
240 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
241 pVCpu->nem.s.fGIMTrapXcptUD = GIMShouldTrapXcptUD(pVCpu);
242 }
243
244 /*
245 * Call native code.
246 */
247 int rc = VINF_SUCCESS;
248#ifdef VBOX_WITH_NATIVE_NEM
249 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
250 rc = nemR3NativeInitCompleted(pVM, enmWhat);
251#else
252 RT_NOREF(pVM, enmWhat);
253#endif
254 return rc;
255}
256
257
258/**
259 *
260 * @returns VBox status code.
261 * @param pVM The cross context VM structure.
262 */
263VMMR3_INT_DECL(int) NEMR3Term(PVM pVM)
264{
265 AssertReturn(pVM->nem.s.u32Magic == NEM_MAGIC, VERR_WRONG_ORDER);
266 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
267 AssertReturn(pVM->apCpusR3[idCpu]->nem.s.u32Magic == NEMCPU_MAGIC, VERR_WRONG_ORDER);
268
269 /* Do native termination. */
270 int rc = VINF_SUCCESS;
271#ifdef VBOX_WITH_NATIVE_NEM
272 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
273 rc = nemR3NativeTerm(pVM);
274#endif
275
276 /* Mark it as terminated. */
277 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
278 {
279 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
280 pVCpu->nem.s.u32Magic = NEMCPU_MAGIC_DEAD;
281 }
282 pVM->nem.s.u32Magic = NEM_MAGIC_DEAD;
283 return rc;
284}
285
286/**
287 * External interface for querying whether native execution API is used.
288 *
289 * @returns true if NEM is being used, otherwise false.
290 * @param pUVM The user mode VM handle.
291 * @sa HMR3IsEnabled
292 */
293VMMR3DECL(bool) NEMR3IsEnabled(PUVM pUVM)
294{
295 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
296 PVM pVM = pUVM->pVM;
297 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
298 return VM_IS_NEM_ENABLED(pVM);
299}
300
301
302/**
303 * The VM is being reset.
304 *
305 * @param pVM The cross context VM structure.
306 */
307VMMR3_INT_DECL(void) NEMR3Reset(PVM pVM)
308{
309#ifdef VBOX_WITH_NATIVE_NEM
310 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
311 nemR3NativeReset(pVM);
312#else
313 RT_NOREF(pVM);
314#endif
315}
316
317
318/**
319 * Resets a virtual CPU.
320 *
321 * Used to bring up secondary CPUs on SMP as well as CPU hot plugging.
322 *
323 * @param pVCpu The cross context virtual CPU structure to reset.
324 * @param fInitIpi Set if being reset due to INIT IPI.
325 */
326VMMR3_INT_DECL(void) NEMR3ResetCpu(PVMCPU pVCpu, bool fInitIpi)
327{
328#ifdef VBOX_WITH_NATIVE_NEM
329 if (pVCpu->pVMR3->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
330 nemR3NativeResetCpu(pVCpu, fInitIpi);
331#else
332 RT_NOREF(pVCpu, fInitIpi);
333#endif
334}
335
336
337/**
338 * Indicates to TM that TMTSCMODE_NATIVE_API should be used for TSC.
339 *
340 * @returns true if TMTSCMODE_NATIVE_API must be used, otherwise @c false.
341 * @param pVM The cross context VM structure.
342 */
343VMMR3_INT_DECL(bool) NEMR3NeedSpecialTscMode(PVM pVM)
344{
345#ifdef VBOX_WITH_NATIVE_NEM
346# ifdef RT_OS_WINDOWS
347 if (VM_IS_NEM_ENABLED(pVM))
348 return true;
349# endif
350#else
351 RT_NOREF(pVM);
352#endif
353 return false;
354}
355
356
357/**
358 * Gets the name of a generic NEM exit code.
359 *
360 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
361 * @param uExit The NEM exit to name.
362 */
363VMMR3DECL(const char *) NEMR3GetExitName(uint32_t uExit)
364{
365 switch ((NEMEXITTYPE)uExit)
366 {
367 case NEMEXITTYPE_UNRECOVERABLE_EXCEPTION: return "NEM unrecoverable exception";
368 case NEMEXITTYPE_INVALID_VP_REGISTER_VALUE: return "NEM invalid vp register value";
369 case NEMEXITTYPE_INTTERRUPT_WINDOW: return "NEM interrupt window";
370 case NEMEXITTYPE_HALT: return "NEM halt";
371 case NEMEXITTYPE_XCPT_UD: return "NEM #UD";
372 case NEMEXITTYPE_XCPT_DB: return "NEM #DB";
373 case NEMEXITTYPE_XCPT_BP: return "NEM #BP";
374 case NEMEXITTYPE_CANCELED: return "NEM canceled";
375 case NEMEXITTYPE_MEMORY_ACCESS: return "NEM memory access";
376 }
377
378 return NULL;
379}
380
381
382VMMR3_INT_DECL(VBOXSTRICTRC) NEMR3RunGC(PVM pVM, PVMCPU pVCpu)
383{
384 Assert(VM_IS_NEM_ENABLED(pVM));
385#ifdef VBOX_WITH_NATIVE_NEM
386 return nemR3NativeRunGC(pVM, pVCpu);
387#else
388 NOREF(pVM); NOREF(pVCpu);
389 return VERR_INTERNAL_ERROR_3;
390#endif
391}
392
393
394VMMR3_INT_DECL(bool) NEMR3CanExecuteGuest(PVM pVM, PVMCPU pVCpu)
395{
396 Assert(VM_IS_NEM_ENABLED(pVM));
397#ifdef VBOX_WITH_NATIVE_NEM
398 return nemR3NativeCanExecuteGuest(pVM, pVCpu);
399#else
400 NOREF(pVM); NOREF(pVCpu);
401 return false;
402#endif
403}
404
405
406VMMR3_INT_DECL(bool) NEMR3SetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
407{
408 Assert(VM_IS_NEM_ENABLED(pVM));
409#ifdef VBOX_WITH_NATIVE_NEM
410 return nemR3NativeSetSingleInstruction(pVM, pVCpu, fEnable);
411#else
412 NOREF(pVM); NOREF(pVCpu); NOREF(fEnable);
413 return false;
414#endif
415}
416
417
418VMMR3_INT_DECL(void) NEMR3NotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
419{
420 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
421#ifdef VBOX_WITH_NATIVE_NEM
422 nemR3NativeNotifyFF(pVM, pVCpu, fFlags);
423#else
424 RT_NOREF(pVM, pVCpu, fFlags);
425#endif
426}
427
428
429
430
431VMMR3_INT_DECL(int) NEMR3NotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb)
432{
433 int rc = VINF_SUCCESS;
434#ifdef VBOX_WITH_NATIVE_NEM
435 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
436 rc = nemR3NativeNotifyPhysRamRegister(pVM, GCPhys, cb);
437#else
438 NOREF(pVM); NOREF(GCPhys); NOREF(cb);
439#endif
440 return rc;
441}
442
443
444VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvMmio2)
445{
446 int rc = VINF_SUCCESS;
447#ifdef VBOX_WITH_NATIVE_NEM
448 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
449 rc = nemR3NativeNotifyPhysMmioExMap(pVM, GCPhys, cb, fFlags, pvMmio2);
450#else
451 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags); NOREF(pvMmio2);
452#endif
453 return rc;
454}
455
456
457VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
458{
459 int rc = VINF_SUCCESS;
460#ifdef VBOX_WITH_NATIVE_NEM
461 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
462 rc = nemR3NativeNotifyPhysMmioExUnmap(pVM, GCPhys, cb, fFlags);
463#else
464 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
465#endif
466 return rc;
467}
468
469
470VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
471{
472 int rc = VINF_SUCCESS;
473#ifdef VBOX_WITH_NATIVE_NEM
474 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
475 rc = nemR3NativeNotifyPhysRomRegisterEarly(pVM, GCPhys, cb, fFlags);
476#else
477 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
478#endif
479 return rc;
480}
481
482
483/**
484 * Called after the ROM range has been fully completed.
485 *
486 * This will be preceeded by a NEMR3NotifyPhysRomRegisterEarly() call as well a
487 * number of NEMHCNotifyPhysPageProtChanged calls.
488 *
489 * @returns VBox status code
490 * @param pVM The cross context VM structure.
491 * @param GCPhys The ROM address (page aligned).
492 * @param cb The size (page aligned).
493 * @param fFlags NEM_NOTIFY_PHYS_ROM_F_XXX.
494 */
495VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
496{
497 int rc = VINF_SUCCESS;
498#ifdef VBOX_WITH_NATIVE_NEM
499 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
500 rc = nemR3NativeNotifyPhysRomRegisterLate(pVM, GCPhys, cb, fFlags);
501#else
502 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
503#endif
504 return rc;
505}
506
507
508VMMR3_INT_DECL(void) NEMR3NotifySetA20(PVMCPU pVCpu, bool fEnabled)
509{
510#ifdef VBOX_WITH_NATIVE_NEM
511 if (pVCpu->pVMR3->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
512 nemR3NativeNotifySetA20(pVCpu, fEnabled);
513#else
514 NOREF(pVCpu); NOREF(fEnabled);
515#endif
516}
517
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use