VirtualBox

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

Last change on this file since 72487 was 72470, checked in by vboxsync, 7 years ago

NEM/win: Looks like we can get a stop confirmation first time around in nemHCWinStopCpu, so just deal with it. Did the necessary #UD forwarding to GIM (not needed for NEM/win). bugref:9044

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 KB
Line 
1/* $Id: NEMR3.cpp 72470 2018-06-07 11:41:23Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager.
4 */
5
6/*
7 * Copyright (C) 2018 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
39#include <iprt/asm.h>
40
41
42
43/**
44 * Basic init and configuration reading.
45 *
46 * Always call NEMR3Term after calling this.
47 *
48 * @returns VBox status code.
49 * @param pVM The cross context VM structure.
50 */
51VMMR3_INT_DECL(int) NEMR3InitConfig(PVM pVM)
52{
53 LogFlow(("NEMR3Init\n"));
54
55 /*
56 * Assert alignment and sizes.
57 */
58 AssertCompileMemberAlignment(VM, nem.s, 64);
59 AssertCompile(sizeof(pVM->nem.s) <= sizeof(pVM->nem.padding));
60
61 /*
62 * Initialize state info so NEMR3Term will always be happy.
63 * No returning prior to setting magics!
64 */
65 pVM->nem.s.u32Magic = NEM_MAGIC;
66 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
67 pVM->aCpus[iCpu].nem.s.u32Magic = NEMCPU_MAGIC;
68
69 /*
70 * Read configuration.
71 */
72 PCFGMNODE pCfgNem = CFGMR3GetChild(CFGMR3GetRoot(pVM), "NEM/");
73
74 /*
75 * Validate the NEM settings.
76 */
77 int rc = CFGMR3ValidateConfig(pCfgNem,
78 "/NEM/",
79 "Enabled"
80 "|Allow64BitGuests",
81 "" /* pszValidNodes */, "NEM" /* pszWho */, 0 /* uInstance */);
82 if (RT_FAILURE(rc))
83 return rc;
84
85 /** @cfgm{/NEM/NEMEnabled, bool, true}
86 * Whether NEM is enabled. */
87 rc = CFGMR3QueryBoolDef(pCfgNem, "Enabled", &pVM->nem.s.fEnabled, true);
88 AssertLogRelRCReturn(rc, rc);
89
90
91#ifdef VBOX_WITH_64_BITS_GUESTS
92 /** @cfgm{/HM/Allow64BitGuests, bool, 32-bit:false, 64-bit:true}
93 * Enables AMD64 CPU features.
94 * On 32-bit hosts this isn't default and require host CPU support. 64-bit hosts
95 * already have the support. */
96 rc = CFGMR3QueryBoolDef(pCfgNem, "Allow64BitGuests", &pVM->nem.s.fAllow64BitGuests, HC_ARCH_BITS == 64);
97 AssertLogRelRCReturn(rc, rc);
98#else
99 pVM->nem.s.fAllow64BitGuests = false;
100#endif
101
102
103 return VINF_SUCCESS;
104}
105
106
107/**
108 * This is called by HMR3Init() when HM cannot be used.
109 *
110 * Sets VM::bMainExecutionEngine to VM_EXEC_ENGINE_NATIVE_API if we can use a
111 * native hypervisor API to execute the VM.
112 *
113 * @returns VBox status code.
114 * @param pVM The cross context VM structure.
115 * @param fFallback Whether this is a fallback call. Cleared if the VM is
116 * configured to use NEM instead of HM.
117 * @param fForced Whether /HM/HMForced was set. If set and we fail to
118 * enable NEM, we'll return a failure status code.
119 * Otherwise we'll assume HMR3Init falls back on raw-mode.
120 */
121VMMR3_INT_DECL(int) NEMR3Init(PVM pVM, bool fFallback, bool fForced)
122{
123 Assert(pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API);
124 int rc;
125 if (pVM->nem.s.fEnabled)
126 {
127#ifdef VBOX_WITH_NATIVE_NEM
128 rc = nemR3NativeInit(pVM, fFallback, fForced);
129 ASMCompilerBarrier(); /* May have changed bMainExecutionEngine. */
130#else
131 RT_NOREF(fFallback);
132 rc = VINF_SUCCESS;
133#endif
134 if (RT_SUCCESS(rc))
135 {
136 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
137 LogRel(("NEM: NEMR3Init: Active.\n"));
138 else
139 {
140 LogRel(("NEM: NEMR3Init: Not available.\n"));
141 if (fForced)
142 rc = VERR_NEM_NOT_AVAILABLE;
143 }
144 }
145 else
146 LogRel(("NEM: NEMR3Init: Native init failed: %Rrc.\n", rc));
147 }
148 else
149 {
150 LogRel(("NEM: NEMR3Init: Disabled.\n"));
151 rc = fForced ? VERR_NEM_NOT_ENABLED : VINF_SUCCESS;
152 }
153 return rc;
154}
155
156
157/**
158 * Perform initialization that depends on CPUM working.
159 *
160 * This is a noop if NEM wasn't activated by a previous NEMR3Init() call.
161 *
162 * @returns VBox status code.
163 * @param pVM The cross context VM structure.
164 */
165VMMR3_INT_DECL(int) NEMR3InitAfterCPUM(PVM pVM)
166{
167 int rc = VINF_SUCCESS;
168 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
169 {
170 /*
171 * Enable CPU features making general ASSUMPTIONS (there are two similar
172 * blocks of code in HM.cpp), to avoid duplicating this code. The
173 * native backend can make check capabilities and adjust as needed.
174 */
175 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
176 if (CPUMGetGuestCpuVendor(pVM) == CPUMCPUVENDOR_AMD)
177 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL); /* 64 bits only on Intel CPUs */
178 if (pVM->nem.s.fAllow64BitGuests)
179 {
180 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL);
181 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
182 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
183 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
184 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
185 }
186 /* Turn on NXE if PAE has been enabled. */
187 else if (CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
188 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
189
190 /*
191 * Do native after-CPUM init.
192 */
193#ifdef VBOX_WITH_NATIVE_NEM
194 rc = nemR3NativeInitAfterCPUM(pVM);
195#else
196 RT_NOREF(pVM);
197#endif
198 }
199 return rc;
200}
201
202
203/**
204 * Called when a init phase has completed.
205 *
206 * @returns VBox status code.
207 * @param pVM The cross context VM structure.
208 * @param enmWhat The phase that completed.
209 */
210VMMR3_INT_DECL(int) NEMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
211{
212 /*
213 * Check if GIM needs #UD, since that applies to everyone.
214 */
215 if (enmWhat == VMINITCOMPLETED_RING3)
216 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
217 pVM->aCpus[iCpu].nem.s.fGIMTrapXcptUD = GIMShouldTrapXcptUD(&pVM->aCpus[iCpu]);
218
219 /*
220 * Call native code.
221 */
222 int rc = VINF_SUCCESS;
223#ifdef VBOX_WITH_NATIVE_NEM
224 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
225 rc = nemR3NativeInitCompleted(pVM, enmWhat);
226#else
227 RT_NOREF(pVM, enmWhat);
228#endif
229 return rc;
230}
231
232
233/**
234 *
235 * @returns VBox status code.
236 * @param pVM The cross context VM structure.
237 */
238VMMR3_INT_DECL(int) NEMR3Term(PVM pVM)
239{
240 AssertReturn(pVM->nem.s.u32Magic == NEM_MAGIC, VERR_WRONG_ORDER);
241 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
242 AssertReturn(pVM->aCpus[iCpu].nem.s.u32Magic == NEMCPU_MAGIC, VERR_WRONG_ORDER);
243
244 /* Do native termination. */
245 int rc = VINF_SUCCESS;
246#ifdef VBOX_WITH_NATIVE_NEM
247 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
248 rc = nemR3NativeTerm(pVM);
249#endif
250
251 /* Mark it as terminated. */
252 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
253 pVM->aCpus[iCpu].nem.s.u32Magic = NEMCPU_MAGIC_DEAD;
254 pVM->nem.s.u32Magic = NEM_MAGIC_DEAD;
255 return rc;
256}
257
258/**
259 * External interface for querying whether native execution API is used.
260 *
261 * @returns true if NEM is being used, otherwise false.
262 * @param pUVM The user mode VM handle.
263 * @sa HMR3IsEnabled
264 */
265VMMR3DECL(bool) NEMR3IsEnabled(PUVM pUVM)
266{
267 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
268 PVM pVM = pUVM->pVM;
269 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
270 return VM_IS_NEM_ENABLED(pVM);
271}
272
273
274/**
275 * The VM is being reset.
276 *
277 * @param pVM The cross context VM structure.
278 */
279VMMR3_INT_DECL(void) NEMR3Reset(PVM pVM)
280{
281#ifdef VBOX_WITH_NATIVE_NEM
282 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
283 nemR3NativeReset(pVM);
284#else
285 RT_NOREF(pVM);
286#endif
287}
288
289
290/**
291 * Resets a virtual CPU.
292 *
293 * Used to bring up secondary CPUs on SMP as well as CPU hot plugging.
294 *
295 * @param pVCpu The cross context virtual CPU structure to reset.
296 * @param fInitIpi Set if being reset due to INIT IPI.
297 */
298VMMR3_INT_DECL(void) NEMR3ResetCpu(PVMCPU pVCpu, bool fInitIpi)
299{
300#ifdef VBOX_WITH_NATIVE_NEM
301 if (pVCpu->pVMR3->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
302 nemR3NativeResetCpu(pVCpu, fInitIpi);
303#else
304 RT_NOREF(pVCpu, fInitIpi);
305#endif
306}
307
308
309VMMR3_INT_DECL(VBOXSTRICTRC) NEMR3RunGC(PVM pVM, PVMCPU pVCpu)
310{
311 Assert(VM_IS_NEM_ENABLED(pVM));
312#ifdef VBOX_WITH_NATIVE_NEM
313 return nemR3NativeRunGC(pVM, pVCpu);
314#else
315 NOREF(pVM); NOREF(pVCpu);
316 return VERR_INTERNAL_ERROR_3;
317#endif
318}
319
320
321VMMR3_INT_DECL(bool) NEMR3CanExecuteGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
322{
323 Assert(VM_IS_NEM_ENABLED(pVM));
324#ifdef VBOX_WITH_NATIVE_NEM
325 return nemR3NativeCanExecuteGuest(pVM, pVCpu, pCtx);
326#else
327 NOREF(pVM); NOREF(pVCpu); NOREF(pCtx);
328 return false;
329#endif
330}
331
332
333VMMR3_INT_DECL(bool) NEMR3SetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
334{
335 Assert(VM_IS_NEM_ENABLED(pVM));
336#ifdef VBOX_WITH_NATIVE_NEM
337 return nemR3NativeSetSingleInstruction(pVM, pVCpu, fEnable);
338#else
339 NOREF(pVM); NOREF(pVCpu); NOREF(fEnable);
340 return false;
341#endif
342}
343
344
345VMMR3_INT_DECL(void) NEMR3NotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
346{
347 AssertLogRelReturnVoid(VM_IS_NEM_ENABLED(pVM));
348#ifdef VBOX_WITH_NATIVE_NEM
349 nemR3NativeNotifyFF(pVM, pVCpu, fFlags);
350#else
351 RT_NOREF(pVM, pVCpu, fFlags);
352#endif
353}
354
355
356
357
358VMMR3_INT_DECL(int) NEMR3NotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb)
359{
360 int rc = VINF_SUCCESS;
361#ifdef VBOX_WITH_NATIVE_NEM
362 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
363 rc = nemR3NativeNotifyPhysRamRegister(pVM, GCPhys, cb);
364#else
365 NOREF(pVM); NOREF(GCPhys); NOREF(cb);
366#endif
367 return rc;
368}
369
370
371VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExMap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvMmio2)
372{
373 int rc = VINF_SUCCESS;
374#ifdef VBOX_WITH_NATIVE_NEM
375 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
376 rc = nemR3NativeNotifyPhysMmioExMap(pVM, GCPhys, cb, fFlags, pvMmio2);
377#else
378 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags); NOREF(pvMmio2);
379#endif
380 return rc;
381}
382
383
384VMMR3_INT_DECL(int) NEMR3NotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
385{
386 int rc = VINF_SUCCESS;
387#ifdef VBOX_WITH_NATIVE_NEM
388 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
389 rc = nemR3NativeNotifyPhysMmioExUnmap(pVM, GCPhys, cb, fFlags);
390#else
391 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
392#endif
393 return rc;
394}
395
396
397VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
398{
399 int rc = VINF_SUCCESS;
400#ifdef VBOX_WITH_NATIVE_NEM
401 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
402 rc = nemR3NativeNotifyPhysRomRegisterEarly(pVM, GCPhys, cb, fFlags);
403#else
404 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
405#endif
406 return rc;
407}
408
409
410/**
411 * Called after the ROM range has been fully completed.
412 *
413 * This will be preceeded by a NEMR3NotifyPhysRomRegisterEarly() call as well a
414 * number of NEMHCNotifyPhysPageProtChanged calls.
415 *
416 * @returns VBox status code
417 * @param pVM The cross context VM structure.
418 * @param GCPhys The ROM address (page aligned).
419 * @param cb The size (page aligned).
420 * @param fFlags NEM_NOTIFY_PHYS_ROM_F_XXX.
421 */
422VMMR3_INT_DECL(int) NEMR3NotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
423{
424 int rc = VINF_SUCCESS;
425#ifdef VBOX_WITH_NATIVE_NEM
426 if (pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
427 rc = nemR3NativeNotifyPhysRomRegisterLate(pVM, GCPhys, cb, fFlags);
428#else
429 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
430#endif
431 return rc;
432}
433
434
435VMMR3_INT_DECL(void) NEMR3NotifySetA20(PVMCPU pVCpu, bool fEnabled)
436{
437#ifdef VBOX_WITH_NATIVE_NEM
438 if (pVCpu->pVMR3->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API)
439 nemR3NativeNotifySetA20(pVCpu, fEnabled);
440#else
441 NOREF(pVCpu); NOREF(fEnabled);
442#endif
443}
444
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette