1 | /* $Id: VMMAll.cpp 92408 2021-11-12 21:49:06Z 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 | PVMCPUCC pVCpu = GVMMR0GetGVCpuByGVMandEMT(pVM, NIL_RTNATIVETHREAD);
|
---|
190 | return pVCpu ? pVCpu->idCpu : NIL_VMCPUID;
|
---|
191 |
|
---|
192 | #else /* RC: Always EMT(0) */
|
---|
193 | NOREF(pVM);
|
---|
194 | return 0;
|
---|
195 | #endif
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Returns the VMCPU of the calling EMT.
|
---|
201 | *
|
---|
202 | * @returns The VMCPU pointer. NULL if not an EMT.
|
---|
203 | *
|
---|
204 | * @param pVM The cross context VM structure.
|
---|
205 | * @internal
|
---|
206 | */
|
---|
207 | VMMDECL(PVMCPUCC) VMMGetCpu(PVMCC pVM)
|
---|
208 | {
|
---|
209 | #ifdef IN_RING3
|
---|
210 | VMCPUID idCpu = VMR3GetVMCPUId(pVM);
|
---|
211 | if (idCpu == NIL_VMCPUID)
|
---|
212 | return NULL;
|
---|
213 | Assert(idCpu < pVM->cCpus);
|
---|
214 | return VMCC_GET_CPU(pVM, idCpu);
|
---|
215 |
|
---|
216 | #elif defined(IN_RING0)
|
---|
217 | return GVMMR0GetGVCpuByGVMandEMT(pVM, NIL_RTNATIVETHREAD);
|
---|
218 |
|
---|
219 | #else /* RC: Always EMT(0) */
|
---|
220 | RT_NOREF(pVM);
|
---|
221 | return &g_VCpu0;
|
---|
222 | #endif /* IN_RING0 */
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Returns the VMCPU of the first EMT thread.
|
---|
228 | *
|
---|
229 | * @returns The VMCPU pointer.
|
---|
230 | * @param pVM The cross context VM structure.
|
---|
231 | * @internal
|
---|
232 | */
|
---|
233 | VMMDECL(PVMCPUCC) VMMGetCpu0(PVMCC pVM)
|
---|
234 | {
|
---|
235 | Assert(pVM->cCpus == 1);
|
---|
236 | return VMCC_GET_CPU_0(pVM);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Returns the VMCPU of the specified virtual CPU.
|
---|
242 | *
|
---|
243 | * @returns The VMCPU pointer. NULL if idCpu is invalid.
|
---|
244 | *
|
---|
245 | * @param pVM The cross context VM structure.
|
---|
246 | * @param idCpu The ID of the virtual CPU.
|
---|
247 | * @internal
|
---|
248 | */
|
---|
249 | VMMDECL(PVMCPUCC) VMMGetCpuById(PVMCC pVM, RTCPUID idCpu)
|
---|
250 | {
|
---|
251 | AssertReturn(idCpu < pVM->cCpus, NULL);
|
---|
252 | return VMCC_GET_CPU(pVM, idCpu);
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * Gets the VBOX_SVN_REV.
|
---|
258 | *
|
---|
259 | * This is just to avoid having to compile a bunch of big files
|
---|
260 | * and requires less Makefile mess.
|
---|
261 | *
|
---|
262 | * @returns VBOX_SVN_REV.
|
---|
263 | */
|
---|
264 | VMM_INT_DECL(uint32_t) VMMGetSvnRev(void)
|
---|
265 | {
|
---|
266 | return VBOX_SVN_REV;
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Returns the build type for matching components.
|
---|
272 | *
|
---|
273 | * @returns Build type value.
|
---|
274 | */
|
---|
275 | uint32_t vmmGetBuildType(void)
|
---|
276 | {
|
---|
277 | uint32_t uRet = 0xbeef0000;
|
---|
278 | #ifdef DEBUG
|
---|
279 | uRet |= RT_BIT_32(0);
|
---|
280 | #endif
|
---|
281 | #ifdef VBOX_WITH_STATISTICS
|
---|
282 | uRet |= RT_BIT_32(1);
|
---|
283 | #endif
|
---|
284 | return uRet;
|
---|
285 | }
|
---|
286 |
|
---|