1 | /* $Id: NEMR3.cpp 70918 2018-02-08 16:11:47Z 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 | * Later.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 |
|
---|
25 | /*********************************************************************************************************************************
|
---|
26 | * Header Files *
|
---|
27 | *********************************************************************************************************************************/
|
---|
28 | #define LOG_GROUP LOG_GROUP_NEM
|
---|
29 | #include <VBox/vmm/nem.h>
|
---|
30 | #include "NEMInternal.h"
|
---|
31 | #include <VBox/vmm/vm.h>
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Basic init and configuration reading.
|
---|
37 | *
|
---|
38 | * Always call NEMR3Term after calling this.
|
---|
39 | *
|
---|
40 | * @returns VBox status code.
|
---|
41 | * @param pVM The VM handle.
|
---|
42 | */
|
---|
43 | VMMR3_INT_DECL(int) NEMR3InitConfig(PVM pVM)
|
---|
44 | {
|
---|
45 | LogFlow(("NEMR3Init\n"));
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Assert alignment and sizes.
|
---|
49 | */
|
---|
50 | AssertCompileMemberAlignment(VM, nem.s, 64);
|
---|
51 | AssertCompile(sizeof(pVM->nem.s) <= sizeof(pVM->nem.padding));
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Initialize state info so NEMR3Term will always be happy.
|
---|
55 | * No returning prior to setting magics!
|
---|
56 | */
|
---|
57 | pVM->nem.s.u32Magic = NEM_MAGIC;
|
---|
58 | for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
59 | pVM->aCpus[iCpu].nem.s.u32Magic = NEMCPU_MAGIC;
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Read configuration.
|
---|
63 | */
|
---|
64 | PCFGMNODE pCfgNem = CFGMR3GetChild(CFGMR3GetRoot(pVM), "NEM/");
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Validate the NEM settings.
|
---|
68 | */
|
---|
69 | int rc = CFGMR3ValidateConfig(pCfgNem,
|
---|
70 | "/NEM/",
|
---|
71 | "Enabled",
|
---|
72 | "" /* pszValidNodes */, "NEM" /* pszWho */, 0 /* uInstance */);
|
---|
73 | if (RT_FAILURE(rc))
|
---|
74 | return rc;
|
---|
75 |
|
---|
76 | /** @cfgm{/NEM/NEMEnabled, bool, true}
|
---|
77 | * Whether NEM is enabled. */
|
---|
78 | rc = CFGMR3QueryBoolDef(pCfgNem, "Enabled", &pVM->nem.s.fEnabled, true);
|
---|
79 | AssertLogRelRCReturn(rc, rc);
|
---|
80 |
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * This is called by HMR3Init() when HM cannot be used.
|
---|
87 | *
|
---|
88 | * Sets VM::fNEMActive if we can use a native hypervisor API to execute the VM.
|
---|
89 | *
|
---|
90 | * @returns VBox status code.
|
---|
91 | * @param pVM The VM handle.
|
---|
92 | * @param fFallback Whether this is a fallback call. Cleared if the VM is
|
---|
93 | * configured to use NEM instead of HM.
|
---|
94 | * @param fForced Whether /HM/HMForced was set. If set and we fail to
|
---|
95 | * enable NEM, we'll return a failure status code.
|
---|
96 | * Otherwise we'll assume HMR3Init falls back on raw-mode.
|
---|
97 | */
|
---|
98 | VMMR3_INT_DECL(int) NEMR3Init(PVM pVM, bool fFallback, bool fForced)
|
---|
99 | {
|
---|
100 | Assert(!pVM->fNEMActive);
|
---|
101 | int rc;
|
---|
102 | if (pVM->nem.s.fEnabled)
|
---|
103 | {
|
---|
104 | #ifdef VBOX_WITH_NATIVE_NEM
|
---|
105 | rc = nemR3NativeInit(pVM, fFallback, fForced);
|
---|
106 | #else
|
---|
107 | RT_NOREF(fFallback);
|
---|
108 | rc = VINF_SUCCESS;
|
---|
109 | #endif
|
---|
110 | if (RT_SUCCESS(rc))
|
---|
111 | {
|
---|
112 | if (pVM->fNEMActive)
|
---|
113 | LogRel(("NEM: NEMR3Init: Active.\n"));
|
---|
114 | else
|
---|
115 | {
|
---|
116 | LogRel(("NEM: NEMR3Init: Not available.\n"));
|
---|
117 | if (fForced)
|
---|
118 | rc = VERR_NEM_NOT_AVAILABLE;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | else
|
---|
122 | LogRel(("NEM: NEMR3Init: Native init failed: %Rrc.\n", rc));
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | LogRel(("NEM: NEMR3Init: Disabled.\n"));
|
---|
127 | rc = fForced ? VERR_NEM_NOT_ENABLED : VINF_SUCCESS;
|
---|
128 | }
|
---|
129 | return rc;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Called when a init phase has completed.
|
---|
135 | *
|
---|
136 | * @returns VBox status code.
|
---|
137 | * @param pVM The cross context VM structure.
|
---|
138 | * @param enmWhat The phase that completed.
|
---|
139 | */
|
---|
140 | VMMR3_INT_DECL(int) NEMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
|
---|
141 | {
|
---|
142 | int rc = VINF_SUCCESS;
|
---|
143 | #ifdef VBOX_WITH_NATIVE_NEM
|
---|
144 | if (pVM->fNEMActive)
|
---|
145 | rc = nemR3NativeInitCompleted(pVM, enmWhat);
|
---|
146 | #else
|
---|
147 | RT_NOREF(pVM, enmWhat);
|
---|
148 | #endif
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | *
|
---|
155 | * @returns VBox status code.
|
---|
156 | * @param pVM The VM handle.
|
---|
157 | */
|
---|
158 | VMMR3_INT_DECL(int) NEMR3Term(PVM pVM)
|
---|
159 | {
|
---|
160 | AssertReturn(pVM->nem.s.u32Magic == NEM_MAGIC, VERR_WRONG_ORDER);
|
---|
161 | for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
162 | AssertReturn(pVM->aCpus[iCpu].nem.s.u32Magic == NEMCPU_MAGIC, VERR_WRONG_ORDER);
|
---|
163 |
|
---|
164 | /* Do native termination. */
|
---|
165 | int rc = VINF_SUCCESS;
|
---|
166 | #ifdef VBOX_WITH_NATIVE_NEM
|
---|
167 | if (pVM->fNEMActive)
|
---|
168 | rc = nemR3NativeTerm(pVM);
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | /* Mark it as terminated. */
|
---|
172 | for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
173 | pVM->aCpus[iCpu].nem.s.u32Magic = NEMCPU_MAGIC_DEAD;
|
---|
174 | pVM->nem.s.u32Magic = NEM_MAGIC_DEAD;
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * The VM is being reset.
|
---|
181 | *
|
---|
182 | * @param pVM The cross context VM structure.
|
---|
183 | */
|
---|
184 | VMMR3_INT_DECL(void) NEMR3Reset(PVM pVM)
|
---|
185 | {
|
---|
186 | #ifdef VBOX_WITH_NATIVE_NEM
|
---|
187 | if (pVM->fNEMActive)
|
---|
188 | nemR3NativeReset(pVM);
|
---|
189 | #else
|
---|
190 | RT_NOREF(pVM);
|
---|
191 | #endif
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Resets a virtual CPU.
|
---|
197 | *
|
---|
198 | * Used to bring up secondary CPUs on SMP as well as CPU hot plugging.
|
---|
199 | *
|
---|
200 | * @param pVCpu The cross context virtual CPU structure to reset.
|
---|
201 | */
|
---|
202 | VMMR3_INT_DECL(void) NEMR3ResetCpu(PVMCPU pVCpu)
|
---|
203 | {
|
---|
204 | #ifdef VBOX_WITH_NATIVE_NEM
|
---|
205 | if (pVCpu->pVMR3->fNEMActive)
|
---|
206 | nemR3NativeResetCpu(pVCpu);
|
---|
207 | #else
|
---|
208 | RT_NOREF(pVCpu);
|
---|
209 | #endif
|
---|
210 | }
|
---|
211 |
|
---|