[23] | 1 | /* $Id: VMMAll.cpp 41965 2012-06-29 02:52:49Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
| 3 | * VMM All Contexts.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[41417] | 7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | /*******************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *******************************************************************************/
|
---|
| 22 | #define LOG_GROUP LOG_GROUP_VMM
|
---|
[35346] | 23 | #include <VBox/vmm/vmm.h>
|
---|
[1] | 24 | #include "VMMInternal.h"
|
---|
[35346] | 25 | #include <VBox/vmm/vm.h>
|
---|
[39303] | 26 | #include <VBox/vmm/vmcpuset.h>
|
---|
[1] | 27 | #include <VBox/param.h>
|
---|
[31353] | 28 | #include <iprt/thread.h>
|
---|
[31356] | 29 | #include <iprt/mp.h>
|
---|
[1] | 30 |
|
---|
| 31 |
|
---|
[39303] | 32 | /*******************************************************************************
|
---|
| 33 | * Global Variables *
|
---|
| 34 | *******************************************************************************/
|
---|
| 35 | /** User counter for the vmmInitFormatTypes function (pro forma). */
|
---|
| 36 | static volatile uint32_t g_cFormatTypeUsers = 0;
|
---|
| 37 |
|
---|
| 38 |
|
---|
[1] | 39 | /**
|
---|
[39303] | 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, "<empty>", sizeof("<empty>") - 1);
|
---|
| 97 | if (cCpus == RT_ELEMENTS(pSet->au32Bitmap) * 32)
|
---|
| 98 | return pfnOutput(pvArgOutput, "<full>", sizeof("<full>") - 1);
|
---|
| 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 | /**
|
---|
[13698] | 168 | * Gets the bottom of the hypervisor stack - RC Ptr.
|
---|
[1] | 169 | *
|
---|
[13698] | 170 | * (The returned address is not actually writable, only after it's decremented
|
---|
| 171 | * by a push/ret/whatever does it become writable.)
|
---|
[1] | 172 | *
|
---|
| 173 | * @returns bottom of the stack.
|
---|
[41802] | 174 | * @param pVCpu Pointer to the VMCPU.
|
---|
[1] | 175 | */
|
---|
[30160] | 176 | VMMDECL(RTRCPTR) VMMGetStackRC(PVMCPU pVCpu)
|
---|
[1] | 177 | {
|
---|
[19434] | 178 | return (RTRCPTR)pVCpu->vmm.s.pbEMTStackBottomRC;
|
---|
[1] | 179 | }
|
---|
[10450] | 180 |
|
---|
[13797] | 181 |
|
---|
[12573] | 182 | /**
|
---|
[41213] | 183 | * Gets the ID of the virtual CPU associated with the calling thread.
|
---|
[12573] | 184 | *
|
---|
[19293] | 185 | * @returns The CPU ID. NIL_VMCPUID if the thread isn't an EMT.
|
---|
| 186 | *
|
---|
[41783] | 187 | * @param pVM Pointer to the VM.
|
---|
[12573] | 188 | */
|
---|
[18927] | 189 | VMMDECL(VMCPUID) VMMGetCpuId(PVM pVM)
|
---|
[12573] | 190 | {
|
---|
[19293] | 191 | #if defined(IN_RING3)
|
---|
| 192 | return VMR3GetVMCPUId(pVM);
|
---|
[13974] | 193 |
|
---|
[19293] | 194 | #elif defined(IN_RING0)
|
---|
[22890] | 195 | if (pVM->cCpus == 1)
|
---|
[19228] | 196 | return 0;
|
---|
| 197 |
|
---|
[31360] | 198 | /* Search first by host cpu id (most common case)
|
---|
| 199 | * and then by native thread id (page fusion case).
|
---|
| 200 | */
|
---|
[32431] | 201 | /* RTMpCpuId had better be cheap. */
|
---|
| 202 | RTCPUID idHostCpu = RTMpCpuId();
|
---|
[31360] | 203 |
|
---|
| 204 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
| 205 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 206 | {
|
---|
| 207 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
| 208 |
|
---|
| 209 | if (pVCpu->idHostCpu == idHostCpu)
|
---|
| 210 | return pVCpu->idCpu;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[31352] | 213 | /* RTThreadGetNativeSelf had better be cheap. */
|
---|
| 214 | RTNATIVETHREAD hThread = RTThreadNativeSelf();
|
---|
[30241] | 215 |
|
---|
| 216 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
| 217 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 218 | {
|
---|
| 219 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
| 220 |
|
---|
[31361] | 221 | if (pVCpu->hNativeThreadR0 == hThread)
|
---|
[30241] | 222 | return pVCpu->idCpu;
|
---|
| 223 | }
|
---|
| 224 | return NIL_VMCPUID;
|
---|
| 225 |
|
---|
[19293] | 226 | #else /* RC: Always EMT(0) */
|
---|
[39078] | 227 | NOREF(pVM);
|
---|
[19228] | 228 | return 0;
|
---|
[19293] | 229 | #endif
|
---|
[13898] | 230 | }
|
---|
[12573] | 231 |
|
---|
[19293] | 232 |
|
---|
[13898] | 233 | /**
|
---|
[19293] | 234 | * Returns the VMCPU of the calling EMT.
|
---|
[13898] | 235 | *
|
---|
[19293] | 236 | * @returns The VMCPU pointer. NULL if not an EMT.
|
---|
| 237 | *
|
---|
[41783] | 238 | * @param pVM Pointer to the VM.
|
---|
[13898] | 239 | */
|
---|
[18927] | 240 | VMMDECL(PVMCPU) VMMGetCpu(PVM pVM)
|
---|
[13898] | 241 | {
|
---|
[13974] | 242 | #ifdef IN_RING3
|
---|
[19293] | 243 | VMCPUID idCpu = VMR3GetVMCPUId(pVM);
|
---|
| 244 | if (idCpu == NIL_VMCPUID)
|
---|
| 245 | return NULL;
|
---|
[22890] | 246 | Assert(idCpu < pVM->cCpus);
|
---|
[30329] | 247 | return &pVM->aCpus[idCpu];
|
---|
[19293] | 248 |
|
---|
| 249 | #elif defined(IN_RING0)
|
---|
[22890] | 250 | if (pVM->cCpus == 1)
|
---|
[19228] | 251 | return &pVM->aCpus[0];
|
---|
| 252 |
|
---|
[31360] | 253 | /* Search first by host cpu id (most common case)
|
---|
| 254 | * and then by native thread id (page fusion case).
|
---|
| 255 | */
|
---|
| 256 |
|
---|
[32431] | 257 | /* RTMpCpuId had better be cheap. */
|
---|
| 258 | RTCPUID idHostCpu = RTMpCpuId();
|
---|
[31360] | 259 |
|
---|
| 260 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
| 261 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 262 | {
|
---|
| 263 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
| 264 |
|
---|
| 265 | if (pVCpu->idHostCpu == idHostCpu)
|
---|
| 266 | return pVCpu;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[31352] | 269 | /* RTThreadGetNativeSelf had better be cheap. */
|
---|
| 270 | RTNATIVETHREAD hThread = RTThreadNativeSelf();
|
---|
[30241] | 271 |
|
---|
| 272 | /** @todo optimize for large number of VCPUs when that becomes more common. */
|
---|
| 273 | for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
|
---|
| 274 | {
|
---|
| 275 | PVMCPU pVCpu = &pVM->aCpus[idCpu];
|
---|
| 276 |
|
---|
[31361] | 277 | if (pVCpu->hNativeThreadR0 == hThread)
|
---|
[30241] | 278 | return pVCpu;
|
---|
| 279 | }
|
---|
| 280 | return NULL;
|
---|
| 281 |
|
---|
[19293] | 282 | #else /* RC: Always EMT(0) */
|
---|
[19228] | 283 | return &pVM->aCpus[0];
|
---|
[19293] | 284 | #endif /* IN_RING0 */
|
---|
[12573] | 285 | }
|
---|
| 286 |
|
---|
[19293] | 287 |
|
---|
[10450] | 288 | /**
|
---|
[18927] | 289 | * Returns the VMCPU of the first EMT thread.
|
---|
| 290 | *
|
---|
| 291 | * @returns The VMCPU pointer.
|
---|
[41783] | 292 | * @param pVM Pointer to the VM.
|
---|
[18927] | 293 | */
|
---|
| 294 | VMMDECL(PVMCPU) VMMGetCpu0(PVM pVM)
|
---|
| 295 | {
|
---|
[22890] | 296 | Assert(pVM->cCpus == 1);
|
---|
[18927] | 297 | return &pVM->aCpus[0];
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[19293] | 300 |
|
---|
[18927] | 301 | /**
|
---|
[13972] | 302 | * Returns the VMCPU of the specified virtual CPU.
|
---|
| 303 | *
|
---|
[19293] | 304 | * @returns The VMCPU pointer. NULL if idCpu is invalid.
|
---|
| 305 | *
|
---|
[41783] | 306 | * @param pVM Pointer to the VM.
|
---|
[19293] | 307 | * @param idCpu The ID of the virtual CPU.
|
---|
[13972] | 308 | */
|
---|
[19293] | 309 | VMMDECL(PVMCPU) VMMGetCpuById(PVM pVM, RTCPUID idCpu)
|
---|
[13972] | 310 | {
|
---|
[22890] | 311 | AssertReturn(idCpu < pVM->cCpus, NULL);
|
---|
[13972] | 312 | return &pVM->aCpus[idCpu];
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[19293] | 315 |
|
---|
[13972] | 316 | /**
|
---|
[10450] | 317 | * Gets the VBOX_SVN_REV.
|
---|
| 318 | *
|
---|
| 319 | * This is just to avoid having to compile a bunch of big files
|
---|
| 320 | * and requires less Makefile mess.
|
---|
| 321 | *
|
---|
| 322 | * @returns VBOX_SVN_REV.
|
---|
| 323 | */
|
---|
| 324 | VMMDECL(uint32_t) VMMGetSvnRev(void)
|
---|
| 325 | {
|
---|
[22906] | 326 | return VBOX_SVN_REV;
|
---|
[10450] | 327 | }
|
---|
[12573] | 328 |
|
---|
[19293] | 329 |
|
---|
[17246] | 330 | /**
|
---|
| 331 | * Queries the current switcher
|
---|
| 332 | *
|
---|
| 333 | * @returns active switcher
|
---|
[41783] | 334 | * @param pVM Pointer to the VM.
|
---|
[17246] | 335 | */
|
---|
| 336 | VMMDECL(VMMSWITCHER) VMMGetSwitcher(PVM pVM)
|
---|
| 337 | {
|
---|
| 338 | return pVM->vmm.s.enmSwitcher;
|
---|
| 339 | }
|
---|
[39303] | 340 |
|
---|