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