VirtualBox

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

Last change on this file since 50590 was 49486, checked in by vboxsync, 11 years ago

VMM: Warnings.

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