1 | /* $Id: VMMAll.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VMM All Contexts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
33 | #include <VBox/vmm/vmm.h>
|
---|
34 | #include "VMMInternal.h"
|
---|
35 | #include <VBox/vmm/vmcc.h>
|
---|
36 | #ifdef IN_RING0
|
---|
37 | # include <VBox/vmm/gvm.h>
|
---|
38 | #endif
|
---|
39 | #include <VBox/vmm/hm.h>
|
---|
40 | #include <VBox/vmm/vmcpuset.h>
|
---|
41 | #include <VBox/param.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 | #include <iprt/mp.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*********************************************************************************************************************************
|
---|
47 | * Global Variables *
|
---|
48 | *********************************************************************************************************************************/
|
---|
49 | /** User counter for the vmmInitFormatTypes function (pro forma). */
|
---|
50 | static volatile uint32_t g_cFormatTypeUsers = 0;
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Helper that formats a decimal number in the range 0..9999.
|
---|
55 | *
|
---|
56 | * @returns The length of the formatted number.
|
---|
57 | * @param pszBuf Output buffer with sufficient space.
|
---|
58 | * @param uNumber The number to format.
|
---|
59 | */
|
---|
60 | static unsigned vmmFormatTypeShortNumber(char *pszBuf, uint32_t uNumber)
|
---|
61 | {
|
---|
62 | unsigned off = 0;
|
---|
63 | if (uNumber >= 10)
|
---|
64 | {
|
---|
65 | if (uNumber >= 100)
|
---|
66 | {
|
---|
67 | if (uNumber >= 1000)
|
---|
68 | pszBuf[off++] = ((uNumber / 1000) % 10) + '0';
|
---|
69 | pszBuf[off++] = ((uNumber / 100) % 10) + '0';
|
---|
70 | }
|
---|
71 | pszBuf[off++] = ((uNumber / 10) % 10) + '0';
|
---|
72 | }
|
---|
73 | pszBuf[off++] = (uNumber % 10) + '0';
|
---|
74 | pszBuf[off] = '\0';
|
---|
75 | return off;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * @callback_method_impl{FNRTSTRFORMATTYPE, vmsetcpu}
|
---|
81 | */
|
---|
82 | static DECLCALLBACK(size_t) vmmFormatTypeVmCpuSet(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
83 | const char *pszType, void const *pvValue,
|
---|
84 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
85 | void *pvUser)
|
---|
86 | {
|
---|
87 | NOREF(pszType); NOREF(cchWidth); NOREF(cchPrecision); NOREF(fFlags);
|
---|
88 |
|
---|
89 | PCVMCPUSET pSet = (PCVMCPUSET)pvValue;
|
---|
90 | uint32_t cCpus = 0;
|
---|
91 | uint32_t iCpu = RT_ELEMENTS(pSet->au32Bitmap) * 32;
|
---|
92 | while (iCpu--)
|
---|
93 | if (VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
94 | cCpus++;
|
---|
95 |
|
---|
96 | char szTmp[32];
|
---|
97 | AssertCompile(RT_ELEMENTS(pSet->au32Bitmap) * 32 < 999);
|
---|
98 | if (cCpus == 1)
|
---|
99 | {
|
---|
100 | iCpu = RT_ELEMENTS(pSet->au32Bitmap) * 32;
|
---|
101 | while (iCpu--)
|
---|
102 | if (VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
103 | {
|
---|
104 | szTmp[0] = 'c';
|
---|
105 | szTmp[1] = 'p';
|
---|
106 | szTmp[2] = 'u';
|
---|
107 | return pfnOutput(pvArgOutput, szTmp, 3 + vmmFormatTypeShortNumber(&szTmp[3], iCpu));
|
---|
108 | }
|
---|
109 | cCpus = 0;
|
---|
110 | }
|
---|
111 | if (cCpus == 0)
|
---|
112 | return pfnOutput(pvArgOutput, RT_STR_TUPLE("<empty>"));
|
---|
113 | if (cCpus == RT_ELEMENTS(pSet->au32Bitmap) * 32)
|
---|
114 | return pfnOutput(pvArgOutput, RT_STR_TUPLE("<full>"));
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Print cpus that are present: {1,2,7,9 ... }
|
---|
118 | */
|
---|
119 | size_t cchRet = pfnOutput(pvArgOutput, "{", 1);
|
---|
120 |
|
---|
121 | cCpus = 0;
|
---|
122 | iCpu = 0;
|
---|
123 | while (iCpu < RT_ELEMENTS(pSet->au32Bitmap) * 32)
|
---|
124 | {
|
---|
125 | if (VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
126 | {
|
---|
127 | /* Output the first cpu number. */
|
---|
128 | int off = 0;
|
---|
129 | if (cCpus != 0)
|
---|
130 | szTmp[off++] = ',';
|
---|
131 | cCpus++;
|
---|
132 | off += vmmFormatTypeShortNumber(&szTmp[off], iCpu);
|
---|
133 |
|
---|
134 | /* Check for sequence. */
|
---|
135 | uint32_t const iStart = ++iCpu;
|
---|
136 | while ( iCpu < RT_ELEMENTS(pSet->au32Bitmap) * 32
|
---|
137 | && VMCPUSET_IS_PRESENT(pSet, iCpu))
|
---|
138 | {
|
---|
139 | iCpu++;
|
---|
140 | cCpus++;
|
---|
141 | }
|
---|
142 | if (iCpu != iStart)
|
---|
143 | {
|
---|
144 | szTmp[off++] = '-';
|
---|
145 | off += vmmFormatTypeShortNumber(&szTmp[off], iCpu);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* Terminate and output. */
|
---|
149 | szTmp[off] = '\0';
|
---|
150 | cchRet += pfnOutput(pvArgOutput, szTmp, off);
|
---|
151 | }
|
---|
152 | iCpu++;
|
---|
153 | }
|
---|
154 |
|
---|
155 | cchRet += pfnOutput(pvArgOutput, "}", 1);
|
---|
156 | NOREF(pvUser);
|
---|
157 | return cchRet;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Registers the VMM wide format types.
|
---|
163 | *
|
---|
164 | * Called by VMMR3Init, VMMR0Init and VMMRCInit.
|
---|
165 | */
|
---|
166 | int vmmInitFormatTypes(void)
|
---|
167 | {
|
---|
168 | int rc = VINF_SUCCESS;
|
---|
169 | if (ASMAtomicIncU32(&g_cFormatTypeUsers) == 1)
|
---|
170 | rc = RTStrFormatTypeRegister("vmcpuset", vmmFormatTypeVmCpuSet, NULL);
|
---|
171 | return rc;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Counterpart to vmmInitFormatTypes, called by VMMR3Term and VMMR0Term.
|
---|
177 | */
|
---|
178 | void vmmTermFormatTypes(void)
|
---|
179 | {
|
---|
180 | if (ASMAtomicDecU32(&g_cFormatTypeUsers) == 0)
|
---|
181 | RTStrFormatTypeDeregister("vmcpuset");
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Gets the ID of the virtual CPU associated with the calling thread.
|
---|
187 | *
|
---|
188 | * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
|
---|
189 | *
|
---|
190 | * @param pVM The cross context VM structure.
|
---|
191 | * @internal
|
---|
192 | */
|
---|
193 | VMMDECL(VMCPUID) VMMGetCpuId(PVMCC pVM)
|
---|
194 | {
|
---|
195 | #if defined(IN_RING3)
|
---|
196 | return VMR3GetVMCPUId(pVM);
|
---|
197 |
|
---|
198 | #elif defined(IN_RING0)
|
---|
199 | PVMCPUCC pVCpu = GVMMR0GetGVCpuByGVMandEMT(pVM, NIL_RTNATIVETHREAD);
|
---|
200 | return pVCpu ? pVCpu->idCpu : NIL_VMCPUID;
|
---|
201 |
|
---|
202 | #else /* RC: Always EMT(0) */
|
---|
203 | NOREF(pVM);
|
---|
204 | return 0;
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Returns the VMCPU of the calling EMT.
|
---|
211 | *
|
---|
212 | * @returns The VMCPU pointer. NULL if not an EMT.
|
---|
213 | *
|
---|
214 | * @param pVM The cross context VM structure.
|
---|
215 | * @internal
|
---|
216 | */
|
---|
217 | VMMDECL(PVMCPUCC) VMMGetCpu(PVMCC pVM)
|
---|
218 | {
|
---|
219 | #ifdef IN_RING3
|
---|
220 | VMCPUID idCpu = VMR3GetVMCPUId(pVM);
|
---|
221 | if (idCpu == NIL_VMCPUID)
|
---|
222 | return NULL;
|
---|
223 | Assert(idCpu < pVM->cCpus);
|
---|
224 | return VMCC_GET_CPU(pVM, idCpu);
|
---|
225 |
|
---|
226 | #elif defined(IN_RING0)
|
---|
227 | return GVMMR0GetGVCpuByGVMandEMT(pVM, NIL_RTNATIVETHREAD);
|
---|
228 |
|
---|
229 | #else /* RC: Always EMT(0) */
|
---|
230 | RT_NOREF(pVM);
|
---|
231 | return &g_VCpu0;
|
---|
232 | #endif /* IN_RING0 */
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Returns the VMCPU of the first EMT thread.
|
---|
238 | *
|
---|
239 | * @returns The VMCPU pointer.
|
---|
240 | * @param pVM The cross context VM structure.
|
---|
241 | * @internal
|
---|
242 | */
|
---|
243 | VMMDECL(PVMCPUCC) VMMGetCpu0(PVMCC pVM)
|
---|
244 | {
|
---|
245 | Assert(pVM->cCpus == 1);
|
---|
246 | return VMCC_GET_CPU_0(pVM);
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Returns the VMCPU of the specified virtual CPU.
|
---|
252 | *
|
---|
253 | * @returns The VMCPU pointer. NULL if idCpu is invalid.
|
---|
254 | *
|
---|
255 | * @param pVM The cross context VM structure.
|
---|
256 | * @param idCpu The ID of the virtual CPU.
|
---|
257 | * @internal
|
---|
258 | */
|
---|
259 | VMMDECL(PVMCPUCC) VMMGetCpuById(PVMCC pVM, RTCPUID idCpu)
|
---|
260 | {
|
---|
261 | AssertReturn(idCpu < pVM->cCpus, NULL);
|
---|
262 | return VMCC_GET_CPU(pVM, idCpu);
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Gets the VBOX_SVN_REV.
|
---|
268 | *
|
---|
269 | * This is just to avoid having to compile a bunch of big files
|
---|
270 | * and requires less Makefile mess.
|
---|
271 | *
|
---|
272 | * @returns VBOX_SVN_REV.
|
---|
273 | */
|
---|
274 | VMM_INT_DECL(uint32_t) VMMGetSvnRev(void)
|
---|
275 | {
|
---|
276 | return VBOX_SVN_REV;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Returns the build type for matching components.
|
---|
282 | *
|
---|
283 | * @returns Build type value.
|
---|
284 | */
|
---|
285 | uint32_t vmmGetBuildType(void)
|
---|
286 | {
|
---|
287 | uint32_t uRet = 0xbeef0000;
|
---|
288 | #ifdef DEBUG
|
---|
289 | uRet |= RT_BIT_32(0);
|
---|
290 | #endif
|
---|
291 | #ifdef VBOX_WITH_STATISTICS
|
---|
292 | uRet |= RT_BIT_32(1);
|
---|
293 | #endif
|
---|
294 | return uRet;
|
---|
295 | }
|
---|
296 |
|
---|