1 | /* $Id: VMMAll.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM All Contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
23 | #include <VBox/vmm/vmm.h>
|
---|
24 | #include "VMMInternal.h"
|
---|
25 | #include <VBox/vmm/vmcc.h>
|
---|
26 | #ifdef IN_RING0
|
---|
27 | # include <VBox/vmm/gvm.h>
|
---|
28 | #endif
|
---|
29 | #include <VBox/vmm/hm.h>
|
---|
30 | #include <VBox/vmm/vmcpuset.h>
|
---|
31 | #include <VBox/param.h>
|
---|
32 | #include <iprt/thread.h>
|
---|
33 | #include <iprt/mp.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /*********************************************************************************************************************************
|
---|
37 | * Global Variables *
|
---|
38 | *********************************************************************************************************************************/
|
---|
39 | /** User counter for the vmmInitFormatTypes function (pro forma). */
|
---|
40 | static volatile uint32_t g_cFormatTypeUsers = 0;
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Helper that formats a decimal number in the range 0..9999.
|
---|
45 | *
|
---|
46 | * @returns The length of the formatted number.
|
---|
47 | * @param pszBuf Output buffer with sufficient space.
|
---|
48 | * @param uNumber The number to format.
|
---|
49 | */
|
---|
50 | static unsigned vmmFormatTypeShortNumber(char *pszBuf, uint32_t uNumber)
|
---|
51 | {
|
---|
52 | unsigned off = 0;
|
---|
53 | if (uNumber >= 10)
|
---|
54 | {
|
---|
55 | if (uNumber >= 100)
|
---|
56 | {
|
---|
57 | if (uNumber >= 1000)
|
---|
58 | pszBuf[off++] = ((uNumber / 1000) % 10) + '0';
|
---|
59 | pszBuf[off++] = ((uNumber / 100) % 10) + '0';
|
---|
60 | }
|
---|
61 | pszBuf[off++] = ((uNumber / 10) % 10) + '0';
|
---|
62 | }
|
---|
63 | pszBuf[off++] = (uNumber % 10) + '0';
|
---|
64 | pszBuf[off] = '\0';
|
---|
65 | return off;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * @callback_method_impl{FNRTSTRFORMATTYPE, vmsetcpu}
|
---|
71 | */
|
---|
72 | static DECLCALLBACK(size_t) vmmFormatTypeVmCpuSet(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
73 | const char *pszType, void const *pvValue,
|
---|
74 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
75 | void *pvUser)
|
---|
76 | {
|
---|
77 | NOREF(pszType); NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
|
---|
78 |
|
---|
79 | PCVMCPUSET pSet = (PCVMCPUSET)pvValue;
|
---|
80 | uint32_t cCpus = 0;
|
---|
81 | uint32_t iCpu = RT_ELEMENTS(pSet->au32Bitmap) * 32;
|
---|
82 | while (iCpu--)
|
---|
83 | if (VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
84 | cCpus++;
|
---|
85 |
|
---|
86 | char szTmp[32];
|
---|
87 | AssertCompile(RT_ELEMENTS(pSet->au32Bitmap) * 32 < 999);
|
---|
88 | if (cCpus == 1)
|
---|
89 | {
|
---|
90 | iCpu = RT_ELEMENTS(pSet->au32Bitmap) * 32;
|
---|
91 | while (iCpu--)
|
---|
92 | if (VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
93 | {
|
---|
94 | szTmp[0] = 'c';
|
---|
95 | szTmp[1] = 'p';
|
---|
96 | szTmp[2] = 'u';
|
---|
97 | return pfnOutput(pvArgOutput, szTmp, 3 + vmmFormatTypeShortNumber(&szTmp[3], iCpu));
|
---|
98 | }
|
---|
99 | cCpus = 0;
|
---|
100 | }
|
---|
101 | if (cCpus == 0)
|
---|
102 | return pfnOutput(pvArgOutput, RT_STR_TUPLE("<empty>"));
|
---|
103 | if (cCpus == RT_ELEMENTS(pSet->au32Bitmap) * 32)
|
---|
104 | return pfnOutput(pvArgOutput, RT_STR_TUPLE("<full>"));
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Print cpus that are present: {1,2,7,9 ... }
|
---|
108 | */
|
---|
109 | size_t cchRet = pfnOutput(pvArgOutput, "{", 1);
|
---|
110 |
|
---|
111 | cCpus = 0;
|
---|
112 | iCpu = 0;
|
---|
113 | while (iCpu < RT_ELEMENTS(pSet->au32Bitmap) * 32)
|
---|
114 | {
|
---|
115 | if (VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
116 | {
|
---|
117 | /* Output the first cpu number. */
|
---|
118 | int off = 0;
|
---|
119 | if (cCpus != 0)
|
---|
120 | szTmp[off++] = ',';
|
---|
121 | cCpus++;
|
---|
122 | off += vmmFormatTypeShortNumber(&szTmp[off], iCpu);
|
---|
123 |
|
---|
124 | /* Check for sequence. */
|
---|
125 | uint32_t const iStart = ++iCpu;
|
---|
126 | while ( iCpu < RT_ELEMENTS(pSet->au32Bitmap) * 32
|
---|
127 | && VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
128 | {
|
---|
129 | iCpu++;
|
---|
130 | cCpus++;
|
---|
131 | }
|
---|
132 | if (iCpu != iStart)
|
---|
133 | {
|
---|
134 | szTmp[off++] = '-';
|
---|
135 | off += vmmFormatTypeShortNumber(&szTmp[off], iCpu);
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* Terminate and output. */
|
---|
139 | szTmp[off] = '\0';
|
---|
140 | cchRet += pfnOutput(pvArgOutput, szTmp, off);
|
---|
141 | }
|
---|
142 | iCpu++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | cchRet += pfnOutput(pvArgOutput, "}", 1);
|
---|
146 | NOREF(pvUser);
|
---|
147 | return cchRet;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Registers the VMM wide format types.
|
---|
153 | *
|
---|
154 | * Called by VMMR3Init, VMMR0Init and VMMRCInit.
|
---|
155 | */
|
---|
156 | int vmmInitFormatTypes(void)
|
---|
157 | {
|
---|
158 | int rc = VINF_SUCCESS;
|
---|
159 | if (ASMAtomicIncU32(&g_cFormatTypeUsers) == 1)
|
---|
160 | rc = RTStrFormatTypeRegister("vmcpuset", vmmFormatTypeVmCpuSet, NULL);
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Counterpart to vmmInitFormatTypes, called by VMMR3Term and VMMR0Term.
|
---|
167 | */
|
---|
168 | void vmmTermFormatTypes(void)
|
---|
169 | {
|
---|
170 | if (ASMAtomicDecU32(&g_cFormatTypeUsers) == 0)
|
---|
171 | RTStrFormatTypeDeregister("vmcpuset");
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Gets the ID of the virtual CPU associated with the calling thread.
|
---|
177 | *
|
---|
178 | * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
|
---|
179 | *
|
---|
180 | * @param pVM The cross context VM structure.
|
---|
181 | * @internal
|
---|
182 | */
|
---|
183 | VMMDECL(VMCPUID) VMMGetCpuId(PVMCC pVM)
|
---|
184 | {
|
---|
185 | #if defined(IN_RING3)
|
---|
186 | return VMR3GetVMCPUId(pVM);
|
---|
187 |
|
---|
188 | #elif defined(IN_RING0)
|
---|
189 | if (pVM->cCpus == 1)
|
---|
190 | return 0;
|
---|
191 | VMCPUID const cCpus = pVM->cCpus;
|
---|
192 |
|
---|
193 | /* Search first by host cpu id (most common case)
|
---|
194 | * and then by native thread id (page fusion case).
|
---|
195 | */
|
---|
196 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
197 | {
|
---|
198 | /** @todo r=ramshankar: This doesn't buy us anything in terms of performance
|
---|
199 | * leaving it here for hysterical raisins and as a reference if we
|
---|
200 | * implemented a hashing approach in the future. */
|
---|
201 | RTCPUID idHostCpu = RTMpCpuId();
|
---|
202 |
|
---|
203 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
204 | for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
|
---|
205 | {
|
---|
206 | PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
|
---|
207 | if (pVCpu->idHostCpu == idHostCpu)
|
---|
208 | return pVCpu->idCpu;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /* RTThreadGetNativeSelf had better be cheap. */
|
---|
213 | RTNATIVETHREAD hThread = RTThreadNativeSelf();
|
---|
214 |
|
---|
215 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
216 | for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
|
---|
217 | {
|
---|
218 | PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
|
---|
219 | if (pVCpu->hNativeThreadR0 == hThread)
|
---|
220 | return pVCpu->idCpu;
|
---|
221 | }
|
---|
222 | return NIL_VMCPUID;
|
---|
223 |
|
---|
224 | #else /* RC: Always EMT(0) */
|
---|
225 | NOREF(pVM);
|
---|
226 | return 0;
|
---|
227 | #endif
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Returns the VMCPU of the calling EMT.
|
---|
233 | *
|
---|
234 | * @returns The VMCPU pointer. NULL if not an EMT.
|
---|
235 | *
|
---|
236 | * @param pVM The cross context VM structure.
|
---|
237 | * @internal
|
---|
238 | */
|
---|
239 | VMMDECL(PVMCPUCC) VMMGetCpu(PVMCC pVM)
|
---|
240 | {
|
---|
241 | #ifdef IN_RING3
|
---|
242 | VMCPUID idCpu = VMR3GetVMCPUId(pVM);
|
---|
243 | if (idCpu == NIL_VMCPUID)
|
---|
244 | return NULL;
|
---|
245 | Assert(idCpu < pVM->cCpus);
|
---|
246 | return VMCC_GET_CPU(pVM, idCpu);
|
---|
247 |
|
---|
248 | #elif defined(IN_RING0)
|
---|
249 | VMCPUID const cCpus = pVM->cCpus;
|
---|
250 | if (pVM->cCpus == 1)
|
---|
251 | return VMCC_GET_CPU_0(pVM);
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Search first by host cpu id (most common case)
|
---|
255 | * and then by native thread id (page fusion case).
|
---|
256 | */
|
---|
257 | if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
|
---|
258 | {
|
---|
259 | /** @todo r=ramshankar: This doesn't buy us anything in terms of performance
|
---|
260 | * leaving it here for hysterical raisins and as a reference if we
|
---|
261 | * implemented a hashing approach in the future. */
|
---|
262 | RTCPUID idHostCpu = RTMpCpuId();
|
---|
263 |
|
---|
264 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
265 | for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
|
---|
266 | {
|
---|
267 | PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
|
---|
268 | if (pVCpu->idHostCpu == idHostCpu)
|
---|
269 | return pVCpu;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* RTThreadGetNativeSelf had better be cheap. */
|
---|
274 | RTNATIVETHREAD hThread = RTThreadNativeSelf();
|
---|
275 |
|
---|
276 | /** @todo optimize for large number of VCPUs when that becomes more common.
|
---|
277 | * Use a map like GIP does that's indexed by the host CPU index. */
|
---|
278 | for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
|
---|
279 | {
|
---|
280 | PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
|
---|
281 | if (pVCpu->hNativeThreadR0 == hThread)
|
---|
282 | return pVCpu;
|
---|
283 | }
|
---|
284 | return NULL;
|
---|
285 |
|
---|
286 | #else /* RC: Always EMT(0) */
|
---|
287 | RT_NOREF(pVM);
|
---|
288 | return &g_VCpu0;
|
---|
289 | #endif /* IN_RING0 */
|
---|
290 | }
|
---|
291 |
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Returns the VMCPU of the first EMT thread.
|
---|
295 | *
|
---|
296 | * @returns The VMCPU pointer.
|
---|
297 | * @param pVM The cross context VM structure.
|
---|
298 | * @internal
|
---|
299 | */
|
---|
300 | VMMDECL(PVMCPUCC) VMMGetCpu0(PVMCC pVM)
|
---|
301 | {
|
---|
302 | Assert(pVM->cCpus == 1);
|
---|
303 | return VMCC_GET_CPU_0(pVM);
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * Returns the VMCPU of the specified virtual CPU.
|
---|
309 | *
|
---|
310 | * @returns The VMCPU pointer. NULL if idCpu is invalid.
|
---|
311 | *
|
---|
312 | * @param pVM The cross context VM structure.
|
---|
313 | * @param idCpu The ID of the virtual CPU.
|
---|
314 | * @internal
|
---|
315 | */
|
---|
316 | VMMDECL(PVMCPUCC) VMMGetCpuById(PVMCC pVM, RTCPUID idCpu)
|
---|
317 | {
|
---|
318 | AssertReturn(idCpu < pVM->cCpus, NULL);
|
---|
319 | return VMCC_GET_CPU(pVM, idCpu);
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 | /**
|
---|
324 | * Gets the VBOX_SVN_REV.
|
---|
325 | *
|
---|
326 | * This is just to avoid having to compile a bunch of big files
|
---|
327 | * and requires less Makefile mess.
|
---|
328 | *
|
---|
329 | * @returns VBOX_SVN_REV.
|
---|
330 | */
|
---|
331 | VMM_INT_DECL(uint32_t) VMMGetSvnRev(void)
|
---|
332 | {
|
---|
333 | return VBOX_SVN_REV;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Checks whether we're in a ring-3 call or not.
|
---|
339 | *
|
---|
340 | * @returns true / false.
|
---|
341 | * @param pVCpu The cross context virtual CPU structure of the calling EMT.
|
---|
342 | * @thread EMT
|
---|
343 | */
|
---|
344 | VMM_INT_DECL(bool) VMMIsInRing3Call(PVMCPU pVCpu)
|
---|
345 | {
|
---|
346 | #ifdef RT_ARCH_X86
|
---|
347 | return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
|
---|
348 | #else
|
---|
349 | return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
|
---|
350 | #endif
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | /**
|
---|
355 | * Returns the build type for matching components.
|
---|
356 | *
|
---|
357 | * @returns Build type value.
|
---|
358 | */
|
---|
359 | uint32_t vmmGetBuildType(void)
|
---|
360 | {
|
---|
361 | uint32_t uRet = 0xbeef0000;
|
---|
362 | #ifdef DEBUG
|
---|
363 | uRet |= RT_BIT_32(0);
|
---|
364 | #endif
|
---|
365 | #ifdef VBOX_WITH_STATISTICS
|
---|
366 | uRet |= RT_BIT_32(1);
|
---|
367 | #endif
|
---|
368 | return uRet;
|
---|
369 | }
|
---|
370 |
|
---|