VirtualBox

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

Last change on this file since 90533 was 90533, checked in by vboxsync, 3 years ago

VMM/VMMGetCpu: Assert that we return the right one.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.6 KB
Line 
1/* $Id: VMMAll.cpp 90533 2021-08-05 20:55:28Z 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). */
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/**
166 * Counterpart to vmmInitFormatTypes, called by VMMR3Term and VMMR0Term.
167 */
168void 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 */
183VMMDECL(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 */
239VMMDECL(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 {
252 PVMCPUCC pVCpu = VMCC_GET_CPU_0(pVM);
253 Assert(pVCpu->hNativeThreadR0 == RTThreadNativeSelf());
254 return pVCpu;
255 }
256
257 /*
258 * Search first by host cpu id (most common case)
259 * and then by native thread id (page fusion case).
260 */
261 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
262 {
263 /** @todo r=ramshankar: This doesn't buy us anything in terms of performance
264 * leaving it here for hysterical raisins and as a reference if we
265 * implemented a hashing approach in the future. */
266 RTCPUID idHostCpu = RTMpCpuId();
267
268 /** @todo optimize for large number of VCPUs when that becomes more common. */
269 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
270 {
271 PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
272 if (pVCpu->idHostCpu == idHostCpu)
273 {
274 Assert(pVCpu->hNativeThreadR0 == RTThreadNativeSelf());
275 return pVCpu;
276 }
277 }
278 }
279
280 /* RTThreadGetNativeSelf had better be cheap. */
281 RTNATIVETHREAD hThread = RTThreadNativeSelf();
282
283 /** @todo optimize for large number of VCPUs when that becomes more common.
284 * Use a map like GIP does that's indexed by the host CPU index. */
285 for (VMCPUID idCpu = 0; idCpu < cCpus; idCpu++)
286 {
287 PVMCPUCC pVCpu = VMCC_GET_CPU(pVM, idCpu);
288 if (pVCpu->hNativeThreadR0 == hThread)
289 return pVCpu;
290 }
291 return NULL;
292
293#else /* RC: Always EMT(0) */
294 RT_NOREF(pVM);
295 return &g_VCpu0;
296#endif /* IN_RING0 */
297}
298
299
300/**
301 * Returns the VMCPU of the first EMT thread.
302 *
303 * @returns The VMCPU pointer.
304 * @param pVM The cross context VM structure.
305 * @internal
306 */
307VMMDECL(PVMCPUCC) VMMGetCpu0(PVMCC pVM)
308{
309 Assert(pVM->cCpus == 1);
310 return VMCC_GET_CPU_0(pVM);
311}
312
313
314/**
315 * Returns the VMCPU of the specified virtual CPU.
316 *
317 * @returns The VMCPU pointer. NULL if idCpu is invalid.
318 *
319 * @param pVM The cross context VM structure.
320 * @param idCpu The ID of the virtual CPU.
321 * @internal
322 */
323VMMDECL(PVMCPUCC) VMMGetCpuById(PVMCC pVM, RTCPUID idCpu)
324{
325 AssertReturn(idCpu < pVM->cCpus, NULL);
326 return VMCC_GET_CPU(pVM, idCpu);
327}
328
329
330/**
331 * Gets the VBOX_SVN_REV.
332 *
333 * This is just to avoid having to compile a bunch of big files
334 * and requires less Makefile mess.
335 *
336 * @returns VBOX_SVN_REV.
337 */
338VMM_INT_DECL(uint32_t) VMMGetSvnRev(void)
339{
340 return VBOX_SVN_REV;
341}
342
343
344/**
345 * Checks whether we're in a ring-3 call or not.
346 *
347 * @returns true / false.
348 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
349 * @thread EMT
350 */
351VMM_INT_DECL(bool) VMMIsInRing3Call(PVMCPU pVCpu)
352{
353#ifdef RT_ARCH_X86
354 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
355#else
356 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
357#endif
358}
359
360
361/**
362 * Returns the build type for matching components.
363 *
364 * @returns Build type value.
365 */
366uint32_t vmmGetBuildType(void)
367{
368 uint32_t uRet = 0xbeef0000;
369#ifdef DEBUG
370 uRet |= RT_BIT_32(0);
371#endif
372#ifdef VBOX_WITH_STATISTICS
373 uRet |= RT_BIT_32(1);
374#endif
375 return uRet;
376}
377
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