VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/VMMAll.cpp@ 79257

Last change on this file since 79257 was 78438, checked in by vboxsync, 6 years ago

VMM: More refactoring of GVM & VM structures for bugref:9217

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.1 KB
Line 
1/* $Id: VMMAll.cpp 78438 2019-05-07 15:57:37Z vboxsync $ */
2/** @file
3 * VMM All Contexts.
4 */
5
6/*
7 * Copyright (C) 2006-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
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/vm.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). */
40static 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 */
50static 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 */
72static 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 */
156int 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#ifndef IN_RC
166/**
167 * Counterpart to vmmInitFormatTypes, called by VMMR3Term and VMMR0Term.
168 */
169void vmmTermFormatTypes(void)
170{
171 if (ASMAtomicDecU32(&g_cFormatTypeUsers) == 0)
172 RTStrFormatTypeDeregister("vmcpuset");
173}
174#endif
175
176
177/**
178 * Gets the bottom of the hypervisor stack - RC Ptr.
179 *
180 * (The returned address is not actually writable, only after it's decremented
181 * by a push/ret/whatever does it become writable.)
182 *
183 * @returns bottom of the stack.
184 * @param pVCpu The cross context virtual CPU structure.
185 */
186VMM_INT_DECL(RTRCPTR) VMMGetStackRC(PVMCPU pVCpu)
187{
188 return (RTRCPTR)pVCpu->vmm.s.pbEMTStackBottomRC;
189}
190
191
192/**
193 * Gets the ID of the virtual CPU associated with the calling thread.
194 *
195 * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
196 *
197 * @param pVM The cross context VM structure.
198 * @internal
199 */
200VMMDECL(VMCPUID) VMMGetCpuId(PVM pVM)
201{
202#if defined(IN_RING3)
203 return VMR3GetVMCPUId(pVM);
204
205#elif defined(IN_RING0)
206 if (pVM->cCpus == 1)
207 return 0;
208# ifdef VBOX_BUGREF_9217
209 PGVM pGVM = (PGVM)pVM;
210 VMCPUID const cCpus = pGVM->cCpusSafe;
211# else
212 VMCPUID const cCpus = pVM->cCpus;
213# endif
214
215 /* Search first by host cpu id (most common case)
216 * and then by native thread id (page fusion case).
217 */
218 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
219 {
220 /** @todo r=ramshankar: This doesn't buy us anything in terms of performance
221 * leaving it here for hysterical raisins and as a reference if we
222 * implemented a hashing approach in the future. */
223 RTCPUID idHostCpu = RTMpCpuId();
224
225 /** @todo optimize for large number of VCPUs when that becomes more common. */
226 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
227 {
228# ifdef VBOX_BUGREF_9217
229 PVMCPU pVCpu = &pGVM->aCpus[idCpu];
230# else
231 PVMCPU pVCpu = &pVM->aCpus[idCpu];
232# endif
233
234 if (pVCpu->idHostCpu == idHostCpu)
235 return pVCpu->idCpu;
236 }
237 }
238
239 /* RTThreadGetNativeSelf had better be cheap. */
240 RTNATIVETHREAD hThread = RTThreadNativeSelf();
241
242 /** @todo optimize for large number of VCPUs when that becomes more common. */
243 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
244 {
245# ifdef VBOX_BUGREF_9217
246 PVMCPU pVCpu = &pGVM->aCpus[idCpu];
247# else
248 PVMCPU pVCpu = &pVM->aCpus[idCpu];
249# endif
250
251 if (pVCpu->hNativeThreadR0 == hThread)
252 return pVCpu->idCpu;
253 }
254 return NIL_VMCPUID;
255
256#else /* RC: Always EMT(0) */
257 NOREF(pVM);
258 return 0;
259#endif
260}
261
262
263/**
264 * Returns the VMCPU of the calling EMT.
265 *
266 * @returns The VMCPU pointer. NULL if not an EMT.
267 *
268 * @param pVM The cross context VM structure.
269 * @internal
270 */
271VMMDECL(PVMCPU) VMMGetCpu(PVM pVM)
272{
273#ifdef IN_RING3
274 VMCPUID idCpu = VMR3GetVMCPUId(pVM);
275 if (idCpu == NIL_VMCPUID)
276 return NULL;
277 Assert(idCpu < pVM->cCpus);
278# ifdef VBOX_BUGREF_9217
279 return pVM->apCpus[idCpu];
280# else
281 return &pVM->aCpus[idCpu];
282# endif
283
284#elif defined(IN_RING0)
285# ifdef VBOX_BUGREF_9217
286 PGVM pGVM = (PGVM)pVM;
287 VMCPUID const cCpus = pGVM->cCpusSafe;
288# else
289 VMCPUID const cCpus = pVM->cCpus;
290# endif
291 if (pVM->cCpus == 1)
292# ifdef VBOX_BUGREF_9217
293 return &pGVM->aCpus[0];
294# else
295 return &pVM->aCpus[0];
296# endif
297
298 /*
299 * Search first by host cpu id (most common case)
300 * and then by native thread id (page fusion case).
301 */
302 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
303 {
304 /** @todo r=ramshankar: This doesn't buy us anything in terms of performance
305 * leaving it here for hysterical raisins and as a reference if we
306 * implemented a hashing approach in the future. */
307 RTCPUID idHostCpu = RTMpCpuId();
308
309 /** @todo optimize for large number of VCPUs when that becomes more common. */
310 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
311 {
312# ifdef VBOX_BUGREF_9217
313 PVMCPU pVCpu = &pGVM->aCpus[idCpu];
314# else
315 PVMCPU pVCpu = &pVM->aCpus[idCpu];
316# endif
317 if (pVCpu->idHostCpu == idHostCpu)
318 return pVCpu;
319 }
320 }
321
322 /* RTThreadGetNativeSelf had better be cheap. */
323 RTNATIVETHREAD hThread = RTThreadNativeSelf();
324
325 /** @todo optimize for large number of VCPUs when that becomes more common.
326 * Use a map like GIP does that's indexed by the host CPU index. */
327 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
328 {
329# ifdef VBOX_BUGREF_9217
330 PVMCPU pVCpu = &pGVM->aCpus[idCpu];
331# else
332 PVMCPU pVCpu = &pVM->aCpus[idCpu];
333# endif
334 if (pVCpu->hNativeThreadR0 == hThread)
335 return pVCpu;
336 }
337 return NULL;
338
339#else /* RC: Always EMT(0) */
340 RT_NOREF(pVM);
341 return &g_VCpu0;
342#endif /* IN_RING0 */
343}
344
345
346/**
347 * Returns the VMCPU of the first EMT thread.
348 *
349 * @returns The VMCPU pointer.
350 * @param pVM The cross context VM structure.
351 * @internal
352 */
353VMMDECL(PVMCPU) VMMGetCpu0(PVM pVM)
354{
355 Assert(pVM->cCpus == 1);
356#ifdef VBOX_BUGREF_9217
357# ifdef IN_RING3
358 return pVM->apCpus[0];
359# elif defined(IN_RING0)
360 return &((PGVM)pVM)->aCpus[0];
361# else /* RC */
362 RT_NOREF(pVM);
363 return &g_VCpu0;
364# endif
365#else
366 return &pVM->aCpus[0];
367#endif
368}
369
370
371/**
372 * Returns the VMCPU of the specified virtual CPU.
373 *
374 * @returns The VMCPU pointer. NULL if idCpu is invalid.
375 *
376 * @param pVM The cross context VM structure.
377 * @param idCpu The ID of the virtual CPU.
378 * @internal
379 */
380VMMDECL(PVMCPU) VMMGetCpuById(PVM pVM, RTCPUID idCpu)
381{
382 AssertReturn(idCpu < pVM->cCpus, NULL);
383#ifdef VBOX_BUGREF_9217
384# ifdef IN_RING3
385 return pVM->apCpus[idCpu];
386# elif defined(IN_RING0)
387 return &((PGVM)pVM)->aCpus[0];
388# else /* RC */
389 RT_NOREF(pVM, idCpu);
390 Assert(idCpu == 0);
391 return &g_VCpu0;
392# endif
393#else
394 return &pVM->aCpus[idCpu];
395#endif
396}
397
398
399/**
400 * Gets the VBOX_SVN_REV.
401 *
402 * This is just to avoid having to compile a bunch of big files
403 * and requires less Makefile mess.
404 *
405 * @returns VBOX_SVN_REV.
406 */
407VMM_INT_DECL(uint32_t) VMMGetSvnRev(void)
408{
409 return VBOX_SVN_REV;
410}
411
412
413/**
414 * Queries the current switcher
415 *
416 * @returns active switcher
417 * @param pVM The cross context VM structure.
418 */
419VMM_INT_DECL(VMMSWITCHER) VMMGetSwitcher(PVM pVM)
420{
421 return pVM->vmm.s.enmSwitcher;
422}
423
424
425/**
426 * Checks whether we're in a ring-3 call or not.
427 *
428 * @returns true / false.
429 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
430 * @thread EMT
431 */
432VMM_INT_DECL(bool) VMMIsInRing3Call(PVMCPU pVCpu)
433{
434#ifdef RT_ARCH_X86
435 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
436#else
437 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
438#endif
439}
440
441
442/**
443 * Returns the build type for matching components.
444 *
445 * @returns Build type value.
446 */
447uint32_t vmmGetBuildType(void)
448{
449 uint32_t uRet = 0xbeef0000;
450#ifdef DEBUG
451 uRet |= RT_BIT_32(0);
452#endif
453#ifdef VBOX_WITH_STATISTICS
454 uRet |= RT_BIT_32(1);
455#endif
456 return uRet;
457}
458
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