VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/GVMMR0.cpp@ 57546

Last change on this file since 57546 was 57358, checked in by vboxsync, 10 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 90.9 KB
Line 
1/* $Id: GVMMR0.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * GVMM - Global VM Manager.
4 */
5
6/*
7 * Copyright (C) 2007-2015 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/** @page pg_gvmm GVMM - The Global VM Manager
20 *
21 * The Global VM Manager lives in ring-0. Its main function at the moment is
22 * to manage a list of all running VMs, keep a ring-0 only structure (GVM) for
23 * each of them, and assign them unique identifiers (so GMM can track page
24 * owners). The GVMM also manage some of the host CPU resources, like the
25 * periodic preemption timer.
26 *
27 * The GVMM will create a ring-0 object for each VM when it is registered, this
28 * is both for session cleanup purposes and for having a point where it is
29 * possible to implement usage polices later (in SUPR0ObjRegister).
30 *
31 *
32 * @section sec_gvmm_ppt Periodic Preemption Timer (PPT)
33 *
34 * On system that sports a high resolution kernel timer API, we use per-cpu
35 * timers to generate interrupts that preempts VT-x, AMD-V and raw-mode guest
36 * execution. The timer frequency is calculating by taking the max
37 * TMCalcHostTimerFrequency for all VMs running on a CPU for the last ~160 ms
38 * (RT_ELEMENTS((PGVMMHOSTCPU)0, Ppt.aHzHistory) *
39 * GVMMHOSTCPU_PPT_HIST_INTERVAL_NS).
40 *
41 * The TMCalcHostTimerFrequency() part of the things gets its takes the max
42 * TMTimerSetFrequencyHint() value and adjusts by the current catch-up percent,
43 * warp drive percent and some fudge factors. VMMR0.cpp reports the result via
44 * GVMMR0SchedUpdatePeriodicPreemptionTimer() before switching to the VT-x,
45 * AMD-V and raw-mode execution environments.
46 */
47
48
49/*********************************************************************************************************************************
50* Header Files *
51*********************************************************************************************************************************/
52#define LOG_GROUP LOG_GROUP_GVMM
53#include <VBox/vmm/gvmm.h>
54#include <VBox/vmm/gmm.h>
55#include "GVMMR0Internal.h"
56#include <VBox/vmm/gvm.h>
57#include <VBox/vmm/vm.h>
58#include <VBox/vmm/vmcpuset.h>
59#include <VBox/vmm/vmm.h>
60#include <VBox/param.h>
61#include <VBox/err.h>
62
63#include <iprt/asm.h>
64#include <iprt/asm-amd64-x86.h>
65#include <iprt/mem.h>
66#include <iprt/semaphore.h>
67#include <iprt/time.h>
68#include <VBox/log.h>
69#include <iprt/thread.h>
70#include <iprt/process.h>
71#include <iprt/param.h>
72#include <iprt/string.h>
73#include <iprt/assert.h>
74#include <iprt/mem.h>
75#include <iprt/memobj.h>
76#include <iprt/mp.h>
77#include <iprt/cpuset.h>
78#include <iprt/spinlock.h>
79#include <iprt/timer.h>
80
81#include "dtrace/VBoxVMM.h"
82
83
84/*********************************************************************************************************************************
85* Defined Constants And Macros *
86*********************************************************************************************************************************/
87#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS) || defined(DOXYGEN_RUNNING)
88/** Define this to enable the periodic preemption timer. */
89# define GVMM_SCHED_WITH_PPT
90#endif
91
92
93/** @def GVMM_CHECK_SMAP_SETUP
94 * SMAP check setup. */
95/** @def GVMM_CHECK_SMAP_CHECK
96 * Checks that the AC flag is set if SMAP is enabled. If AC is not set,
97 * it will be logged and @a a_BadExpr is executed. */
98/** @def GVMM_CHECK_SMAP_CHECK2
99 * Checks that the AC flag is set if SMAP is enabled. If AC is not set, it will
100 * be logged, written to the VMs assertion text buffer, and @a a_BadExpr is
101 * executed. */
102#if defined(VBOX_STRICT) || 1
103# define GVMM_CHECK_SMAP_SETUP() uint32_t const fKernelFeatures = SUPR0GetKernelFeatures()
104# define GVMM_CHECK_SMAP_CHECK(a_BadExpr) \
105 do { \
106 if (fKernelFeatures & SUPKERNELFEATURES_SMAP) \
107 { \
108 RTCCUINTREG fEflCheck = ASMGetFlags(); \
109 if (RT_LIKELY(fEflCheck & X86_EFL_AC)) \
110 { /* likely */ } \
111 else \
112 { \
113 SUPR0Printf("%s, line %d: EFLAGS.AC is clear! (%#x)\n", __FUNCTION__, __LINE__, (uint32_t)fEflCheck); \
114 a_BadExpr; \
115 } \
116 } \
117 } while (0)
118# define GVMM_CHECK_SMAP_CHECK2(a_pVM, a_BadExpr) \
119 do { \
120 if (fKernelFeatures & SUPKERNELFEATURES_SMAP) \
121 { \
122 RTCCUINTREG fEflCheck = ASMGetFlags(); \
123 if (RT_LIKELY(fEflCheck & X86_EFL_AC)) \
124 { /* likely */ } \
125 else \
126 { \
127 SUPR0BadContext((a_pVM) ? (a_pVM)->pSession : NULL, __FILE__, __LINE__, "EFLAGS.AC is zero!"); \
128 a_BadExpr; \
129 } \
130 } \
131 } while (0)
132#else
133# define GVMM_CHECK_SMAP_SETUP() uint32_t const fKernelFeatures = 0
134# define GVMM_CHECK_SMAP_CHECK(a_BadExpr) NOREF(fKernelFeatures)
135# define GVMM_CHECK_SMAP_CHECK2(a_pVM, a_BadExpr) NOREF(fKernelFeatures)
136#endif
137
138
139
140/*********************************************************************************************************************************
141* Structures and Typedefs *
142*********************************************************************************************************************************/
143
144/**
145 * Global VM handle.
146 */
147typedef struct GVMHANDLE
148{
149 /** The index of the next handle in the list (free or used). (0 is nil.) */
150 uint16_t volatile iNext;
151 /** Our own index / handle value. */
152 uint16_t iSelf;
153 /** The process ID of the handle owner.
154 * This is used for access checks. */
155 RTPROCESS ProcId;
156 /** The pointer to the ring-0 only (aka global) VM structure. */
157 PGVM pGVM;
158 /** The ring-0 mapping of the shared VM instance data. */
159 PVM pVM;
160 /** The virtual machine object. */
161 void *pvObj;
162 /** The session this VM is associated with. */
163 PSUPDRVSESSION pSession;
164 /** The ring-0 handle of the EMT0 thread.
165 * This is used for ownership checks as well as looking up a VM handle by thread
166 * at times like assertions. */
167 RTNATIVETHREAD hEMT0;
168} GVMHANDLE;
169/** Pointer to a global VM handle. */
170typedef GVMHANDLE *PGVMHANDLE;
171
172/** Number of GVM handles (including the NIL handle). */
173#if HC_ARCH_BITS == 64
174# define GVMM_MAX_HANDLES 8192
175#else
176# define GVMM_MAX_HANDLES 128
177#endif
178
179/**
180 * Per host CPU GVMM data.
181 */
182typedef struct GVMMHOSTCPU
183{
184 /** Magic number (GVMMHOSTCPU_MAGIC). */
185 uint32_t volatile u32Magic;
186 /** The CPU ID. */
187 RTCPUID idCpu;
188 /** The CPU set index. */
189 uint32_t idxCpuSet;
190
191#ifdef GVMM_SCHED_WITH_PPT
192 /** Periodic preemption timer data. */
193 struct
194 {
195 /** The handle to the periodic preemption timer. */
196 PRTTIMER pTimer;
197 /** Spinlock protecting the data below. */
198 RTSPINLOCK hSpinlock;
199 /** The smalles Hz that we need to care about. (static) */
200 uint32_t uMinHz;
201 /** The number of ticks between each historization. */
202 uint32_t cTicksHistoriziationInterval;
203 /** The current historization tick (counting up to
204 * cTicksHistoriziationInterval and then resetting). */
205 uint32_t iTickHistorization;
206 /** The current timer interval. This is set to 0 when inactive. */
207 uint32_t cNsInterval;
208 /** The current timer frequency. This is set to 0 when inactive. */
209 uint32_t uTimerHz;
210 /** The current max frequency reported by the EMTs.
211 * This gets historicize and reset by the timer callback. This is
212 * read without holding the spinlock, so needs atomic updating. */
213 uint32_t volatile uDesiredHz;
214 /** Whether the timer was started or not. */
215 bool volatile fStarted;
216 /** Set if we're starting timer. */
217 bool volatile fStarting;
218 /** The index of the next history entry (mod it). */
219 uint32_t iHzHistory;
220 /** Historicized uDesiredHz values. The array wraps around, new entries
221 * are added at iHzHistory. This is updated approximately every
222 * GVMMHOSTCPU_PPT_HIST_INTERVAL_NS by the timer callback. */
223 uint32_t aHzHistory[8];
224 /** Statistics counter for recording the number of interval changes. */
225 uint32_t cChanges;
226 /** Statistics counter for recording the number of timer starts. */
227 uint32_t cStarts;
228 } Ppt;
229#endif /* GVMM_SCHED_WITH_PPT */
230
231} GVMMHOSTCPU;
232/** Pointer to the per host CPU GVMM data. */
233typedef GVMMHOSTCPU *PGVMMHOSTCPU;
234/** The GVMMHOSTCPU::u32Magic value (Petra, Tanya & Rachel Haden). */
235#define GVMMHOSTCPU_MAGIC UINT32_C(0x19711011)
236/** The interval on history entry should cover (approximately) give in
237 * nanoseconds. */
238#define GVMMHOSTCPU_PPT_HIST_INTERVAL_NS UINT32_C(20000000)
239
240
241/**
242 * The GVMM instance data.
243 */
244typedef struct GVMM
245{
246 /** Eyecatcher / magic. */
247 uint32_t u32Magic;
248 /** The index of the head of the free handle chain. (0 is nil.) */
249 uint16_t volatile iFreeHead;
250 /** The index of the head of the active handle chain. (0 is nil.) */
251 uint16_t volatile iUsedHead;
252 /** The number of VMs. */
253 uint16_t volatile cVMs;
254 /** Alignment padding. */
255 uint16_t u16Reserved;
256 /** The number of EMTs. */
257 uint32_t volatile cEMTs;
258 /** The number of EMTs that have halted in GVMMR0SchedHalt. */
259 uint32_t volatile cHaltedEMTs;
260 /** Alignment padding. */
261 uint32_t u32Alignment;
262 /** When the next halted or sleeping EMT will wake up.
263 * This is set to 0 when it needs recalculating and to UINT64_MAX when
264 * there are no halted or sleeping EMTs in the GVMM. */
265 uint64_t uNsNextEmtWakeup;
266 /** The lock used to serialize VM creation, destruction and associated events that
267 * isn't performance critical. Owners may acquire the list lock. */
268 RTSEMFASTMUTEX CreateDestroyLock;
269 /** The lock used to serialize used list updates and accesses.
270 * This indirectly includes scheduling since the scheduler will have to walk the
271 * used list to examin running VMs. Owners may not acquire any other locks. */
272 RTSEMFASTMUTEX UsedLock;
273 /** The handle array.
274 * The size of this array defines the maximum number of currently running VMs.
275 * The first entry is unused as it represents the NIL handle. */
276 GVMHANDLE aHandles[GVMM_MAX_HANDLES];
277
278 /** @gcfgm{/GVMM/cEMTsMeansCompany, 32-bit, 0, UINT32_MAX, 1}
279 * The number of EMTs that means we no longer consider ourselves alone on a
280 * CPU/Core.
281 */
282 uint32_t cEMTsMeansCompany;
283 /** @gcfgm{/GVMM/MinSleepAlone,32-bit, 0, 100000000, 750000, ns}
284 * The minimum sleep time for when we're alone, in nano seconds.
285 */
286 uint32_t nsMinSleepAlone;
287 /** @gcfgm{/GVMM/MinSleepCompany,32-bit,0, 100000000, 15000, ns}
288 * The minimum sleep time for when we've got company, in nano seconds.
289 */
290 uint32_t nsMinSleepCompany;
291 /** @gcfgm{/GVMM/EarlyWakeUp1, 32-bit, 0, 100000000, 25000, ns}
292 * The limit for the first round of early wakeups, given in nano seconds.
293 */
294 uint32_t nsEarlyWakeUp1;
295 /** @gcfgm{/GVMM/EarlyWakeUp2, 32-bit, 0, 100000000, 50000, ns}
296 * The limit for the second round of early wakeups, given in nano seconds.
297 */
298 uint32_t nsEarlyWakeUp2;
299
300 /** The number of entries in the host CPU array (aHostCpus). */
301 uint32_t cHostCpus;
302 /** Per host CPU data (variable length). */
303 GVMMHOSTCPU aHostCpus[1];
304} GVMM;
305/** Pointer to the GVMM instance data. */
306typedef GVMM *PGVMM;
307
308/** The GVMM::u32Magic value (Charlie Haden). */
309#define GVMM_MAGIC UINT32_C(0x19370806)
310
311
312
313/*********************************************************************************************************************************
314* Global Variables *
315*********************************************************************************************************************************/
316/** Pointer to the GVMM instance data.
317 * (Just my general dislike for global variables.) */
318static PGVMM g_pGVMM = NULL;
319
320/** Macro for obtaining and validating the g_pGVMM pointer.
321 * On failure it will return from the invoking function with the specified return value.
322 *
323 * @param pGVMM The name of the pGVMM variable.
324 * @param rc The return value on failure. Use VERR_GVMM_INSTANCE for VBox
325 * status codes.
326 */
327#define GVMM_GET_VALID_INSTANCE(pGVMM, rc) \
328 do { \
329 (pGVMM) = g_pGVMM;\
330 AssertPtrReturn((pGVMM), (rc)); \
331 AssertMsgReturn((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic), (rc)); \
332 } while (0)
333
334/** Macro for obtaining and validating the g_pGVMM pointer, void function variant.
335 * On failure it will return from the invoking function.
336 *
337 * @param pGVMM The name of the pGVMM variable.
338 */
339#define GVMM_GET_VALID_INSTANCE_VOID(pGVMM) \
340 do { \
341 (pGVMM) = g_pGVMM;\
342 AssertPtrReturnVoid((pGVMM)); \
343 AssertMsgReturnVoid((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic)); \
344 } while (0)
345
346
347/*********************************************************************************************************************************
348* Internal Functions *
349*********************************************************************************************************************************/
350static void gvmmR0InitPerVMData(PGVM pGVM);
351static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle);
352static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock);
353static int gvmmR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM, PGVMM *ppGVMM);
354#ifdef GVMM_SCHED_WITH_PPT
355static DECLCALLBACK(void) gvmmR0SchedPeriodicPreemptionTimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick);
356#endif
357
358
359/**
360 * Initializes the GVMM.
361 *
362 * This is called while owning the loader semaphore (see supdrvIOCtl_LdrLoad()).
363 *
364 * @returns VBox status code.
365 */
366GVMMR0DECL(int) GVMMR0Init(void)
367{
368 LogFlow(("GVMMR0Init:\n"));
369
370 /*
371 * Allocate and initialize the instance data.
372 */
373 uint32_t cHostCpus = RTMpGetArraySize();
374 AssertMsgReturn(cHostCpus > 0 && cHostCpus < _64K, ("%d", (int)cHostCpus), VERR_GVMM_HOST_CPU_RANGE);
375
376 PGVMM pGVMM = (PGVMM)RTMemAllocZ(RT_UOFFSETOF(GVMM, aHostCpus[cHostCpus]));
377 if (!pGVMM)
378 return VERR_NO_MEMORY;
379 int rc = RTSemFastMutexCreate(&pGVMM->CreateDestroyLock);
380 if (RT_SUCCESS(rc))
381 {
382 rc = RTSemFastMutexCreate(&pGVMM->UsedLock);
383 if (RT_SUCCESS(rc))
384 {
385 pGVMM->u32Magic = GVMM_MAGIC;
386 pGVMM->iUsedHead = 0;
387 pGVMM->iFreeHead = 1;
388
389 /* the nil handle */
390 pGVMM->aHandles[0].iSelf = 0;
391 pGVMM->aHandles[0].iNext = 0;
392
393 /* the tail */
394 unsigned i = RT_ELEMENTS(pGVMM->aHandles) - 1;
395 pGVMM->aHandles[i].iSelf = i;
396 pGVMM->aHandles[i].iNext = 0; /* nil */
397
398 /* the rest */
399 while (i-- > 1)
400 {
401 pGVMM->aHandles[i].iSelf = i;
402 pGVMM->aHandles[i].iNext = i + 1;
403 }
404
405 /* The default configuration values. */
406 uint32_t cNsResolution = RTSemEventMultiGetResolution();
407 pGVMM->cEMTsMeansCompany = 1; /** @todo should be adjusted to relative to the cpu count or something... */
408 if (cNsResolution >= 5*RT_NS_100US)
409 {
410 pGVMM->nsMinSleepAlone = 750000 /* ns (0.750 ms) */; /** @todo this should be adjusted to be 75% (or something) of the scheduler granularity... */
411 pGVMM->nsMinSleepCompany = 15000 /* ns (0.015 ms) */;
412 pGVMM->nsEarlyWakeUp1 = 25000 /* ns (0.025 ms) */;
413 pGVMM->nsEarlyWakeUp2 = 50000 /* ns (0.050 ms) */;
414 }
415 else if (cNsResolution > RT_NS_100US)
416 {
417 pGVMM->nsMinSleepAlone = cNsResolution / 2;
418 pGVMM->nsMinSleepCompany = cNsResolution / 4;
419 pGVMM->nsEarlyWakeUp1 = 0;
420 pGVMM->nsEarlyWakeUp2 = 0;
421 }
422 else
423 {
424 pGVMM->nsMinSleepAlone = 2000;
425 pGVMM->nsMinSleepCompany = 2000;
426 pGVMM->nsEarlyWakeUp1 = 0;
427 pGVMM->nsEarlyWakeUp2 = 0;
428 }
429
430 /* The host CPU data. */
431 pGVMM->cHostCpus = cHostCpus;
432 uint32_t iCpu = cHostCpus;
433 RTCPUSET PossibleSet;
434 RTMpGetSet(&PossibleSet);
435 while (iCpu-- > 0)
436 {
437 pGVMM->aHostCpus[iCpu].idxCpuSet = iCpu;
438#ifdef GVMM_SCHED_WITH_PPT
439 pGVMM->aHostCpus[iCpu].Ppt.pTimer = NULL;
440 pGVMM->aHostCpus[iCpu].Ppt.hSpinlock = NIL_RTSPINLOCK;
441 pGVMM->aHostCpus[iCpu].Ppt.uMinHz = 5; /** @todo Add some API which figures this one out. (not *that* important) */
442 pGVMM->aHostCpus[iCpu].Ppt.cTicksHistoriziationInterval = 1;
443 //pGVMM->aHostCpus[iCpu].Ppt.iTickHistorization = 0;
444 //pGVMM->aHostCpus[iCpu].Ppt.cNsInterval = 0;
445 //pGVMM->aHostCpus[iCpu].Ppt.uTimerHz = 0;
446 //pGVMM->aHostCpus[iCpu].Ppt.uDesiredHz = 0;
447 //pGVMM->aHostCpus[iCpu].Ppt.fStarted = false;
448 //pGVMM->aHostCpus[iCpu].Ppt.fStarting = false;
449 //pGVMM->aHostCpus[iCpu].Ppt.iHzHistory = 0;
450 //pGVMM->aHostCpus[iCpu].Ppt.aHzHistory = {0};
451#endif
452
453 if (RTCpuSetIsMember(&PossibleSet, iCpu))
454 {
455 pGVMM->aHostCpus[iCpu].idCpu = RTMpCpuIdFromSetIndex(iCpu);
456 pGVMM->aHostCpus[iCpu].u32Magic = GVMMHOSTCPU_MAGIC;
457
458#ifdef GVMM_SCHED_WITH_PPT
459 rc = RTTimerCreateEx(&pGVMM->aHostCpus[iCpu].Ppt.pTimer,
460 50*1000*1000 /* whatever */,
461 RTTIMER_FLAGS_CPU(iCpu) | RTTIMER_FLAGS_HIGH_RES,
462 gvmmR0SchedPeriodicPreemptionTimerCallback,
463 &pGVMM->aHostCpus[iCpu]);
464 if (RT_SUCCESS(rc))
465 rc = RTSpinlockCreate(&pGVMM->aHostCpus[iCpu].Ppt.hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "GVMM/CPU");
466 if (RT_FAILURE(rc))
467 {
468 while (iCpu < cHostCpus)
469 {
470 RTTimerDestroy(pGVMM->aHostCpus[iCpu].Ppt.pTimer);
471 RTSpinlockDestroy(pGVMM->aHostCpus[iCpu].Ppt.hSpinlock);
472 pGVMM->aHostCpus[iCpu].Ppt.hSpinlock = NIL_RTSPINLOCK;
473 iCpu++;
474 }
475 break;
476 }
477#endif
478 }
479 else
480 {
481 pGVMM->aHostCpus[iCpu].idCpu = NIL_RTCPUID;
482 pGVMM->aHostCpus[iCpu].u32Magic = 0;
483 }
484 }
485 if (RT_SUCCESS(rc))
486 {
487 g_pGVMM = pGVMM;
488 LogFlow(("GVMMR0Init: pGVMM=%p cHostCpus=%u\n", pGVMM, cHostCpus));
489 return VINF_SUCCESS;
490 }
491
492 /* bail out. */
493 RTSemFastMutexDestroy(pGVMM->UsedLock);
494 pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
495 }
496 RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
497 pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
498 }
499
500 RTMemFree(pGVMM);
501 return rc;
502}
503
504
505/**
506 * Terminates the GVM.
507 *
508 * This is called while owning the loader semaphore (see supdrvLdrFree()).
509 * And unless something is wrong, there should be absolutely no VMs
510 * registered at this point.
511 */
512GVMMR0DECL(void) GVMMR0Term(void)
513{
514 LogFlow(("GVMMR0Term:\n"));
515
516 PGVMM pGVMM = g_pGVMM;
517 g_pGVMM = NULL;
518 if (RT_UNLIKELY(!VALID_PTR(pGVMM)))
519 {
520 SUPR0Printf("GVMMR0Term: pGVMM=%p\n", pGVMM);
521 return;
522 }
523
524 /*
525 * First of all, stop all active timers.
526 */
527 uint32_t cActiveTimers = 0;
528 uint32_t iCpu = pGVMM->cHostCpus;
529 while (iCpu-- > 0)
530 {
531 ASMAtomicWriteU32(&pGVMM->aHostCpus[iCpu].u32Magic, ~GVMMHOSTCPU_MAGIC);
532#ifdef GVMM_SCHED_WITH_PPT
533 if ( pGVMM->aHostCpus[iCpu].Ppt.pTimer != NULL
534 && RT_SUCCESS(RTTimerStop(pGVMM->aHostCpus[iCpu].Ppt.pTimer)))
535 cActiveTimers++;
536#endif
537 }
538 if (cActiveTimers)
539 RTThreadSleep(1); /* fudge */
540
541 /*
542 * Invalidate the and free resources.
543 */
544 pGVMM->u32Magic = ~GVMM_MAGIC;
545 RTSemFastMutexDestroy(pGVMM->UsedLock);
546 pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
547 RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
548 pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
549
550 pGVMM->iFreeHead = 0;
551 if (pGVMM->iUsedHead)
552 {
553 SUPR0Printf("GVMMR0Term: iUsedHead=%#x! (cVMs=%#x cEMTs=%#x)\n", pGVMM->iUsedHead, pGVMM->cVMs, pGVMM->cEMTs);
554 pGVMM->iUsedHead = 0;
555 }
556
557#ifdef GVMM_SCHED_WITH_PPT
558 iCpu = pGVMM->cHostCpus;
559 while (iCpu-- > 0)
560 {
561 RTTimerDestroy(pGVMM->aHostCpus[iCpu].Ppt.pTimer);
562 pGVMM->aHostCpus[iCpu].Ppt.pTimer = NULL;
563 RTSpinlockDestroy(pGVMM->aHostCpus[iCpu].Ppt.hSpinlock);
564 pGVMM->aHostCpus[iCpu].Ppt.hSpinlock = NIL_RTSPINLOCK;
565 }
566#endif
567
568 RTMemFree(pGVMM);
569}
570
571
572/**
573 * A quick hack for setting global config values.
574 *
575 * @returns VBox status code.
576 *
577 * @param pSession The session handle. Used for authentication.
578 * @param pszName The variable name.
579 * @param u64Value The new value.
580 */
581GVMMR0DECL(int) GVMMR0SetConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t u64Value)
582{
583 /*
584 * Validate input.
585 */
586 PGVMM pGVMM;
587 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
588 AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
589 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
590
591 /*
592 * String switch time!
593 */
594 if (strncmp(pszName, RT_STR_TUPLE("/GVMM/")))
595 return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
596 int rc = VINF_SUCCESS;
597 pszName += sizeof("/GVMM/") - 1;
598 if (!strcmp(pszName, "cEMTsMeansCompany"))
599 {
600 if (u64Value <= UINT32_MAX)
601 pGVMM->cEMTsMeansCompany = u64Value;
602 else
603 rc = VERR_OUT_OF_RANGE;
604 }
605 else if (!strcmp(pszName, "MinSleepAlone"))
606 {
607 if (u64Value <= RT_NS_100MS)
608 pGVMM->nsMinSleepAlone = u64Value;
609 else
610 rc = VERR_OUT_OF_RANGE;
611 }
612 else if (!strcmp(pszName, "MinSleepCompany"))
613 {
614 if (u64Value <= RT_NS_100MS)
615 pGVMM->nsMinSleepCompany = u64Value;
616 else
617 rc = VERR_OUT_OF_RANGE;
618 }
619 else if (!strcmp(pszName, "EarlyWakeUp1"))
620 {
621 if (u64Value <= RT_NS_100MS)
622 pGVMM->nsEarlyWakeUp1 = u64Value;
623 else
624 rc = VERR_OUT_OF_RANGE;
625 }
626 else if (!strcmp(pszName, "EarlyWakeUp2"))
627 {
628 if (u64Value <= RT_NS_100MS)
629 pGVMM->nsEarlyWakeUp2 = u64Value;
630 else
631 rc = VERR_OUT_OF_RANGE;
632 }
633 else
634 rc = VERR_CFGM_VALUE_NOT_FOUND;
635 return rc;
636}
637
638
639/**
640 * A quick hack for getting global config values.
641 *
642 * @returns VBox status code.
643 *
644 * @param pSession The session handle. Used for authentication.
645 * @param pszName The variable name.
646 * @param u64Value The new value.
647 */
648GVMMR0DECL(int) GVMMR0QueryConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t *pu64Value)
649{
650 /*
651 * Validate input.
652 */
653 PGVMM pGVMM;
654 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
655 AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
656 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
657 AssertPtrReturn(pu64Value, VERR_INVALID_POINTER);
658
659 /*
660 * String switch time!
661 */
662 if (strncmp(pszName, RT_STR_TUPLE("/GVMM/")))
663 return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
664 int rc = VINF_SUCCESS;
665 pszName += sizeof("/GVMM/") - 1;
666 if (!strcmp(pszName, "cEMTsMeansCompany"))
667 *pu64Value = pGVMM->cEMTsMeansCompany;
668 else if (!strcmp(pszName, "MinSleepAlone"))
669 *pu64Value = pGVMM->nsMinSleepAlone;
670 else if (!strcmp(pszName, "MinSleepCompany"))
671 *pu64Value = pGVMM->nsMinSleepCompany;
672 else if (!strcmp(pszName, "EarlyWakeUp1"))
673 *pu64Value = pGVMM->nsEarlyWakeUp1;
674 else if (!strcmp(pszName, "EarlyWakeUp2"))
675 *pu64Value = pGVMM->nsEarlyWakeUp2;
676 else
677 rc = VERR_CFGM_VALUE_NOT_FOUND;
678 return rc;
679}
680
681
682/**
683 * Try acquire the 'used' lock.
684 *
685 * @returns IPRT status code, see RTSemFastMutexRequest.
686 * @param pGVMM The GVMM instance data.
687 */
688DECLINLINE(int) gvmmR0UsedLock(PGVMM pGVMM)
689{
690 LogFlow(("++gvmmR0UsedLock(%p)\n", pGVMM));
691 int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
692 LogFlow(("gvmmR0UsedLock(%p)->%Rrc\n", pGVMM, rc));
693 return rc;
694}
695
696
697/**
698 * Release the 'used' lock.
699 *
700 * @returns IPRT status code, see RTSemFastMutexRelease.
701 * @param pGVMM The GVMM instance data.
702 */
703DECLINLINE(int) gvmmR0UsedUnlock(PGVMM pGVMM)
704{
705 LogFlow(("--gvmmR0UsedUnlock(%p)\n", pGVMM));
706 int rc = RTSemFastMutexRelease(pGVMM->UsedLock);
707 AssertRC(rc);
708 return rc;
709}
710
711
712/**
713 * Try acquire the 'create & destroy' lock.
714 *
715 * @returns IPRT status code, see RTSemFastMutexRequest.
716 * @param pGVMM The GVMM instance data.
717 */
718DECLINLINE(int) gvmmR0CreateDestroyLock(PGVMM pGVMM)
719{
720 LogFlow(("++gvmmR0CreateDestroyLock(%p)\n", pGVMM));
721 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
722 LogFlow(("gvmmR0CreateDestroyLock(%p)->%Rrc\n", pGVMM, rc));
723 return rc;
724}
725
726
727/**
728 * Release the 'create & destroy' lock.
729 *
730 * @returns IPRT status code, see RTSemFastMutexRequest.
731 * @param pGVMM The GVMM instance data.
732 */
733DECLINLINE(int) gvmmR0CreateDestroyUnlock(PGVMM pGVMM)
734{
735 LogFlow(("--gvmmR0CreateDestroyUnlock(%p)\n", pGVMM));
736 int rc = RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
737 AssertRC(rc);
738 return rc;
739}
740
741
742/**
743 * Request wrapper for the GVMMR0CreateVM API.
744 *
745 * @returns VBox status code.
746 * @param pReq The request buffer.
747 */
748GVMMR0DECL(int) GVMMR0CreateVMReq(PGVMMCREATEVMREQ pReq)
749{
750 /*
751 * Validate the request.
752 */
753 if (!VALID_PTR(pReq))
754 return VERR_INVALID_POINTER;
755 if (pReq->Hdr.cbReq != sizeof(*pReq))
756 return VERR_INVALID_PARAMETER;
757 if (!VALID_PTR(pReq->pSession))
758 return VERR_INVALID_POINTER;
759
760 /*
761 * Execute it.
762 */
763 PVM pVM;
764 pReq->pVMR0 = NULL;
765 pReq->pVMR3 = NIL_RTR3PTR;
766 int rc = GVMMR0CreateVM(pReq->pSession, pReq->cCpus, &pVM);
767 if (RT_SUCCESS(rc))
768 {
769 pReq->pVMR0 = pVM;
770 pReq->pVMR3 = pVM->pVMR3;
771 }
772 return rc;
773}
774
775
776/**
777 * Allocates the VM structure and registers it with GVM.
778 *
779 * The caller will become the VM owner and there by the EMT.
780 *
781 * @returns VBox status code.
782 * @param pSession The support driver session.
783 * @param cCpus Number of virtual CPUs for the new VM.
784 * @param ppVM Where to store the pointer to the VM structure.
785 *
786 * @thread EMT.
787 */
788GVMMR0DECL(int) GVMMR0CreateVM(PSUPDRVSESSION pSession, uint32_t cCpus, PVM *ppVM)
789{
790 LogFlow(("GVMMR0CreateVM: pSession=%p\n", pSession));
791 PGVMM pGVMM;
792 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
793
794 AssertPtrReturn(ppVM, VERR_INVALID_POINTER);
795 *ppVM = NULL;
796
797 if ( cCpus == 0
798 || cCpus > VMM_MAX_CPU_COUNT)
799 return VERR_INVALID_PARAMETER;
800
801 RTNATIVETHREAD hEMT0 = RTThreadNativeSelf();
802 AssertReturn(hEMT0 != NIL_RTNATIVETHREAD, VERR_GVMM_BROKEN_IPRT);
803 RTPROCESS ProcId = RTProcSelf();
804 AssertReturn(ProcId != NIL_RTPROCESS, VERR_GVMM_BROKEN_IPRT);
805
806 /*
807 * The whole allocation process is protected by the lock.
808 */
809 int rc = gvmmR0CreateDestroyLock(pGVMM);
810 AssertRCReturn(rc, rc);
811
812 /*
813 * Allocate a handle first so we don't waste resources unnecessarily.
814 */
815 uint16_t iHandle = pGVMM->iFreeHead;
816 if (iHandle)
817 {
818 PGVMHANDLE pHandle = &pGVMM->aHandles[iHandle];
819
820 /* consistency checks, a bit paranoid as always. */
821 if ( !pHandle->pVM
822 && !pHandle->pGVM
823 && !pHandle->pvObj
824 && pHandle->iSelf == iHandle)
825 {
826 pHandle->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_VM, gvmmR0HandleObjDestructor, pGVMM, pHandle);
827 if (pHandle->pvObj)
828 {
829 /*
830 * Move the handle from the free to used list and perform permission checks.
831 */
832 rc = gvmmR0UsedLock(pGVMM);
833 AssertRC(rc);
834
835 pGVMM->iFreeHead = pHandle->iNext;
836 pHandle->iNext = pGVMM->iUsedHead;
837 pGVMM->iUsedHead = iHandle;
838 pGVMM->cVMs++;
839
840 pHandle->pVM = NULL;
841 pHandle->pGVM = NULL;
842 pHandle->pSession = pSession;
843 pHandle->hEMT0 = NIL_RTNATIVETHREAD;
844 pHandle->ProcId = NIL_RTPROCESS;
845
846 gvmmR0UsedUnlock(pGVMM);
847
848 rc = SUPR0ObjVerifyAccess(pHandle->pvObj, pSession, NULL);
849 if (RT_SUCCESS(rc))
850 {
851 /*
852 * Allocate the global VM structure (GVM) and initialize it.
853 */
854 PGVM pGVM = (PGVM)RTMemAllocZ(RT_UOFFSETOF(GVM, aCpus[cCpus]));
855 if (pGVM)
856 {
857 pGVM->u32Magic = GVM_MAGIC;
858 pGVM->hSelf = iHandle;
859 pGVM->pVM = NULL;
860 pGVM->cCpus = cCpus;
861
862 gvmmR0InitPerVMData(pGVM);
863 GMMR0InitPerVMData(pGVM);
864
865 /*
866 * Allocate the shared VM structure and associated page array.
867 */
868 const uint32_t cbVM = RT_UOFFSETOF(VM, aCpus[cCpus]);
869 const uint32_t cPages = RT_ALIGN_32(cbVM, PAGE_SIZE) >> PAGE_SHIFT;
870 rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
871 if (RT_SUCCESS(rc))
872 {
873 PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
874 memset(pVM, 0, cPages << PAGE_SHIFT);
875 pVM->enmVMState = VMSTATE_CREATING;
876 pVM->pVMR0 = pVM;
877 pVM->pSession = pSession;
878 pVM->hSelf = iHandle;
879 pVM->cbSelf = cbVM;
880 pVM->cCpus = cCpus;
881 pVM->uCpuExecutionCap = 100; /* default is no cap. */
882 pVM->offVMCPU = RT_UOFFSETOF(VM, aCpus);
883 AssertCompileMemberAlignment(VM, cpum, 64);
884 AssertCompileMemberAlignment(VM, tm, 64);
885 AssertCompileMemberAlignment(VM, aCpus, PAGE_SIZE);
886
887 rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
888 if (RT_SUCCESS(rc))
889 {
890 PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
891 for (uint32_t iPage = 0; iPage < cPages; iPage++)
892 {
893 paPages[iPage].uReserved = 0;
894 paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
895 Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
896 }
897
898 /*
899 * Map them into ring-3.
900 */
901 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
902 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
903 if (RT_SUCCESS(rc))
904 {
905 pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
906 AssertPtr((void *)pVM->pVMR3);
907
908 /* Initialize all the VM pointers. */
909 for (uint32_t i = 0; i < cCpus; i++)
910 {
911 pVM->aCpus[i].pVMR0 = pVM;
912 pVM->aCpus[i].pVMR3 = pVM->pVMR3;
913 pVM->aCpus[i].idHostCpu = NIL_RTCPUID;
914 pVM->aCpus[i].hNativeThreadR0 = NIL_RTNATIVETHREAD;
915 }
916
917 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1,
918 0 /* uAlignment */, RTMEM_PROT_READ | RTMEM_PROT_WRITE,
919 NIL_RTR0PROCESS);
920 if (RT_SUCCESS(rc))
921 {
922 pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
923 AssertPtr((void *)pVM->paVMPagesR3);
924
925 /* complete the handle - take the UsedLock sem just to be careful. */
926 rc = gvmmR0UsedLock(pGVMM);
927 AssertRC(rc);
928
929 pHandle->pVM = pVM;
930 pHandle->pGVM = pGVM;
931 pHandle->hEMT0 = hEMT0;
932 pHandle->ProcId = ProcId;
933 pGVM->pVM = pVM;
934 pGVM->aCpus[0].hEMT = hEMT0;
935 pVM->aCpus[0].hNativeThreadR0 = hEMT0;
936 pGVMM->cEMTs += cCpus;
937
938 rc = VMMR0ThreadCtxHookCreateForEmt(&pVM->aCpus[0]);
939 if (RT_SUCCESS(rc))
940 {
941 VBOXVMM_R0_GVMM_VM_CREATED(pGVM, pVM, ProcId, (void *)hEMT0, cCpus);
942
943 gvmmR0UsedUnlock(pGVMM);
944 gvmmR0CreateDestroyUnlock(pGVMM);
945
946 *ppVM = pVM;
947 Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
948 return VINF_SUCCESS;
949 }
950 }
951
952 RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
953 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
954 }
955 RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
956 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
957 }
958 RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
959 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
960 }
961 }
962 }
963 /* else: The user wasn't permitted to create this VM. */
964
965 /*
966 * The handle will be freed by gvmmR0HandleObjDestructor as we release the
967 * object reference here. A little extra mess because of non-recursive lock.
968 */
969 void *pvObj = pHandle->pvObj;
970 pHandle->pvObj = NULL;
971 gvmmR0CreateDestroyUnlock(pGVMM);
972
973 SUPR0ObjRelease(pvObj, pSession);
974
975 SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
976 return rc;
977 }
978
979 rc = VERR_NO_MEMORY;
980 }
981 else
982 rc = VERR_GVMM_IPE_1;
983 }
984 else
985 rc = VERR_GVM_TOO_MANY_VMS;
986
987 gvmmR0CreateDestroyUnlock(pGVMM);
988 return rc;
989}
990
991
992/**
993 * Initializes the per VM data belonging to GVMM.
994 *
995 * @param pGVM Pointer to the global VM structure.
996 */
997static void gvmmR0InitPerVMData(PGVM pGVM)
998{
999 AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
1000 AssertCompile(RT_SIZEOFMEMB(GVMCPU,gvmm.s) <= RT_SIZEOFMEMB(GVMCPU,gvmm.padding));
1001 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
1002 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
1003 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
1004 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
1005 pGVM->gvmm.s.fDoneVMMR0Init = false;
1006 pGVM->gvmm.s.fDoneVMMR0Term = false;
1007
1008 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
1009 {
1010 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
1011 pGVM->aCpus[i].hEMT = NIL_RTNATIVETHREAD;
1012 }
1013}
1014
1015
1016/**
1017 * Does the VM initialization.
1018 *
1019 * @returns VBox status code.
1020 * @param pVM Pointer to the VM.
1021 */
1022GVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
1023{
1024 LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
1025
1026 /*
1027 * Validate the VM structure, state and handle.
1028 */
1029 PGVM pGVM;
1030 PGVMM pGVMM;
1031 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
1032 if (RT_SUCCESS(rc))
1033 {
1034 if ( !pGVM->gvmm.s.fDoneVMMR0Init
1035 && pGVM->aCpus[0].gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
1036 {
1037 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
1038 {
1039 rc = RTSemEventMultiCreate(&pGVM->aCpus[i].gvmm.s.HaltEventMulti);
1040 if (RT_FAILURE(rc))
1041 {
1042 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
1043 break;
1044 }
1045 }
1046 }
1047 else
1048 rc = VERR_WRONG_ORDER;
1049 }
1050
1051 LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
1052 return rc;
1053}
1054
1055
1056/**
1057 * Indicates that we're done with the ring-0 initialization
1058 * of the VM.
1059 *
1060 * @param pVM Pointer to the VM.
1061 * @thread EMT(0)
1062 */
1063GVMMR0DECL(void) GVMMR0DoneInitVM(PVM pVM)
1064{
1065 /* Validate the VM structure, state and handle. */
1066 PGVM pGVM;
1067 PGVMM pGVMM;
1068 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
1069 AssertRCReturnVoid(rc);
1070
1071 /* Set the indicator. */
1072 pGVM->gvmm.s.fDoneVMMR0Init = true;
1073}
1074
1075
1076/**
1077 * Indicates that we're doing the ring-0 termination of the VM.
1078 *
1079 * @returns true if termination hasn't been done already, false if it has.
1080 * @param pVM Pointer to the VM.
1081 * @param pGVM Pointer to the global VM structure. Optional.
1082 * @thread EMT(0)
1083 */
1084GVMMR0DECL(bool) GVMMR0DoingTermVM(PVM pVM, PGVM pGVM)
1085{
1086 /* Validate the VM structure, state and handle. */
1087 AssertPtrNullReturn(pGVM, false);
1088 AssertReturn(!pGVM || pGVM->u32Magic == GVM_MAGIC, false);
1089 if (!pGVM)
1090 {
1091 PGVMM pGVMM;
1092 int rc = gvmmR0ByVMAndEMT(pVM, 0 /* idCpu */, &pGVM, &pGVMM);
1093 AssertRCReturn(rc, false);
1094 }
1095
1096 /* Set the indicator. */
1097 if (pGVM->gvmm.s.fDoneVMMR0Term)
1098 return false;
1099 pGVM->gvmm.s.fDoneVMMR0Term = true;
1100 return true;
1101}
1102
1103
1104/**
1105 * Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
1106 *
1107 * This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
1108 * and the caller is not the EMT thread, unfortunately. For security reasons, it
1109 * would've been nice if the caller was actually the EMT thread or that we somehow
1110 * could've associated the calling thread with the VM up front.
1111 *
1112 * @returns VBox status code.
1113 * @param pVM Pointer to the VM.
1114 *
1115 * @thread EMT(0) if it's associated with the VM, otherwise any thread.
1116 */
1117GVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
1118{
1119 LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
1120 PGVMM pGVMM;
1121 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
1122
1123 /*
1124 * Validate the VM structure, state and caller.
1125 */
1126 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1127 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1128 AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState),
1129 VERR_WRONG_ORDER);
1130
1131 uint32_t hGVM = pVM->hSelf;
1132 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
1133 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
1134
1135 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1136 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
1137
1138 RTPROCESS ProcId = RTProcSelf();
1139 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
1140 AssertReturn( ( pHandle->hEMT0 == hSelf
1141 && pHandle->ProcId == ProcId)
1142 || pHandle->hEMT0 == NIL_RTNATIVETHREAD, VERR_NOT_OWNER);
1143
1144 /*
1145 * Lookup the handle and destroy the object.
1146 * Since the lock isn't recursive and we'll have to leave it before dereferencing the
1147 * object, we take some precautions against racing callers just in case...
1148 */
1149 int rc = gvmmR0CreateDestroyLock(pGVMM);
1150 AssertRC(rc);
1151
1152 /* Be careful here because we might theoretically be racing someone else cleaning up. */
1153 if ( pHandle->pVM == pVM
1154 && ( ( pHandle->hEMT0 == hSelf
1155 && pHandle->ProcId == ProcId)
1156 || pHandle->hEMT0 == NIL_RTNATIVETHREAD)
1157 && VALID_PTR(pHandle->pvObj)
1158 && VALID_PTR(pHandle->pSession)
1159 && VALID_PTR(pHandle->pGVM)
1160 && pHandle->pGVM->u32Magic == GVM_MAGIC)
1161 {
1162 void *pvObj = pHandle->pvObj;
1163 pHandle->pvObj = NULL;
1164 gvmmR0CreateDestroyUnlock(pGVMM);
1165
1166 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
1167 {
1168 /** @todo Can we busy wait here for all thread-context hooks to be
1169 * deregistered before releasing (destroying) it? Only until we find a
1170 * solution for not deregistering hooks everytime we're leaving HMR0
1171 * context. */
1172 VMMR0ThreadCtxHookDestroyForEmt(&pVM->aCpus[idCpu]);
1173 }
1174
1175 SUPR0ObjRelease(pvObj, pHandle->pSession);
1176 }
1177 else
1178 {
1179 SUPR0Printf("GVMMR0DestroyVM: pHandle=%p:{.pVM=%p, .hEMT0=%p, .ProcId=%u, .pvObj=%p} pVM=%p hSelf=%p\n",
1180 pHandle, pHandle->pVM, pHandle->hEMT0, pHandle->ProcId, pHandle->pvObj, pVM, hSelf);
1181 gvmmR0CreateDestroyUnlock(pGVMM);
1182 rc = VERR_GVMM_IPE_2;
1183 }
1184
1185 return rc;
1186}
1187
1188
1189/**
1190 * Performs VM cleanup task as part of object destruction.
1191 *
1192 * @param pGVM The GVM pointer.
1193 */
1194static void gvmmR0CleanupVM(PGVM pGVM)
1195{
1196 if ( pGVM->gvmm.s.fDoneVMMR0Init
1197 && !pGVM->gvmm.s.fDoneVMMR0Term)
1198 {
1199 if ( pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ
1200 && RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj) == pGVM->pVM)
1201 {
1202 LogFlow(("gvmmR0CleanupVM: Calling VMMR0TermVM\n"));
1203 VMMR0TermVM(pGVM->pVM, pGVM);
1204 }
1205 else
1206 AssertMsgFailed(("gvmmR0CleanupVM: VMMemObj=%p pVM=%p\n", pGVM->gvmm.s.VMMemObj, pGVM->pVM));
1207 }
1208
1209 GMMR0CleanupVM(pGVM);
1210}
1211
1212
1213/**
1214 * Handle destructor.
1215 *
1216 * @param pvGVMM The GVM instance pointer.
1217 * @param pvHandle The handle pointer.
1218 */
1219static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle)
1220{
1221 LogFlow(("gvmmR0HandleObjDestructor: %p %p %p\n", pvObj, pvGVMM, pvHandle));
1222
1223 /*
1224 * Some quick, paranoid, input validation.
1225 */
1226 PGVMHANDLE pHandle = (PGVMHANDLE)pvHandle;
1227 AssertPtr(pHandle);
1228 PGVMM pGVMM = (PGVMM)pvGVMM;
1229 Assert(pGVMM == g_pGVMM);
1230 const uint16_t iHandle = pHandle - &pGVMM->aHandles[0];
1231 if ( !iHandle
1232 || iHandle >= RT_ELEMENTS(pGVMM->aHandles)
1233 || iHandle != pHandle->iSelf)
1234 {
1235 SUPR0Printf("GVM: handle %d is out of range or corrupt (iSelf=%d)!\n", iHandle, pHandle->iSelf);
1236 return;
1237 }
1238
1239 int rc = gvmmR0CreateDestroyLock(pGVMM);
1240 AssertRC(rc);
1241 rc = gvmmR0UsedLock(pGVMM);
1242 AssertRC(rc);
1243
1244 /*
1245 * This is a tad slow but a doubly linked list is too much hassle.
1246 */
1247 if (RT_UNLIKELY(pHandle->iNext >= RT_ELEMENTS(pGVMM->aHandles)))
1248 {
1249 SUPR0Printf("GVM: used list index %d is out of range!\n", pHandle->iNext);
1250 gvmmR0UsedUnlock(pGVMM);
1251 gvmmR0CreateDestroyUnlock(pGVMM);
1252 return;
1253 }
1254
1255 if (pGVMM->iUsedHead == iHandle)
1256 pGVMM->iUsedHead = pHandle->iNext;
1257 else
1258 {
1259 uint16_t iPrev = pGVMM->iUsedHead;
1260 int c = RT_ELEMENTS(pGVMM->aHandles) + 2;
1261 while (iPrev)
1262 {
1263 if (RT_UNLIKELY(iPrev >= RT_ELEMENTS(pGVMM->aHandles)))
1264 {
1265 SUPR0Printf("GVM: used list index %d is out of range!\n", iPrev);
1266 gvmmR0UsedUnlock(pGVMM);
1267 gvmmR0CreateDestroyUnlock(pGVMM);
1268 return;
1269 }
1270 if (RT_UNLIKELY(c-- <= 0))
1271 {
1272 iPrev = 0;
1273 break;
1274 }
1275
1276 if (pGVMM->aHandles[iPrev].iNext == iHandle)
1277 break;
1278 iPrev = pGVMM->aHandles[iPrev].iNext;
1279 }
1280 if (!iPrev)
1281 {
1282 SUPR0Printf("GVM: can't find the handle previous previous of %d!\n", pHandle->iSelf);
1283 gvmmR0UsedUnlock(pGVMM);
1284 gvmmR0CreateDestroyUnlock(pGVMM);
1285 return;
1286 }
1287
1288 Assert(pGVMM->aHandles[iPrev].iNext == iHandle);
1289 pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
1290 }
1291 pHandle->iNext = 0;
1292 pGVMM->cVMs--;
1293
1294 /*
1295 * Do the global cleanup round.
1296 */
1297 PGVM pGVM = pHandle->pGVM;
1298 if ( VALID_PTR(pGVM)
1299 && pGVM->u32Magic == GVM_MAGIC)
1300 {
1301 pGVMM->cEMTs -= pGVM->cCpus;
1302 gvmmR0UsedUnlock(pGVMM);
1303
1304 gvmmR0CleanupVM(pGVM);
1305
1306 /*
1307 * Do the GVMM cleanup - must be done last.
1308 */
1309 /* The VM and VM pages mappings/allocations. */
1310 if (pGVM->gvmm.s.VMPagesMapObj != NIL_RTR0MEMOBJ)
1311 {
1312 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMapObj, false /* fFreeMappings */); AssertRC(rc);
1313 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
1314 }
1315
1316 if (pGVM->gvmm.s.VMMapObj != NIL_RTR0MEMOBJ)
1317 {
1318 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */); AssertRC(rc);
1319 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
1320 }
1321
1322 if (pGVM->gvmm.s.VMPagesMemObj != NIL_RTR0MEMOBJ)
1323 {
1324 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */); AssertRC(rc);
1325 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
1326 }
1327
1328 if (pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ)
1329 {
1330 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */); AssertRC(rc);
1331 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
1332 }
1333
1334 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
1335 {
1336 if (pGVM->aCpus[i].gvmm.s.HaltEventMulti != NIL_RTSEMEVENTMULTI)
1337 {
1338 rc = RTSemEventMultiDestroy(pGVM->aCpus[i].gvmm.s.HaltEventMulti); AssertRC(rc);
1339 pGVM->aCpus[i].gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
1340 }
1341 }
1342
1343 /* the GVM structure itself. */
1344 pGVM->u32Magic |= UINT32_C(0x80000000);
1345 RTMemFree(pGVM);
1346
1347 /* Re-acquire the UsedLock before freeing the handle since we're updating handle fields. */
1348 rc = gvmmR0UsedLock(pGVMM);
1349 AssertRC(rc);
1350 }
1351 /* else: GVMMR0CreateVM cleanup. */
1352
1353 /*
1354 * Free the handle.
1355 */
1356 pHandle->iNext = pGVMM->iFreeHead;
1357 pGVMM->iFreeHead = iHandle;
1358 ASMAtomicWriteNullPtr(&pHandle->pGVM);
1359 ASMAtomicWriteNullPtr(&pHandle->pVM);
1360 ASMAtomicWriteNullPtr(&pHandle->pvObj);
1361 ASMAtomicWriteNullPtr(&pHandle->pSession);
1362 ASMAtomicWriteHandle(&pHandle->hEMT0, NIL_RTNATIVETHREAD);
1363 ASMAtomicWriteU32(&pHandle->ProcId, NIL_RTPROCESS);
1364
1365 gvmmR0UsedUnlock(pGVMM);
1366 gvmmR0CreateDestroyUnlock(pGVMM);
1367 LogFlow(("gvmmR0HandleObjDestructor: returns\n"));
1368}
1369
1370
1371/**
1372 * Registers the calling thread as the EMT of a Virtual CPU.
1373 *
1374 * Note that VCPU 0 is automatically registered during VM creation.
1375 *
1376 * @returns VBox status code
1377 * @param pVM Pointer to the VM.
1378 * @param idCpu VCPU id.
1379 */
1380GVMMR0DECL(int) GVMMR0RegisterVCpu(PVM pVM, VMCPUID idCpu)
1381{
1382 AssertReturn(idCpu != 0, VERR_NOT_OWNER);
1383
1384 /*
1385 * Validate the VM structure, state and handle.
1386 */
1387 PGVM pGVM;
1388 PGVMM pGVMM;
1389 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
1390 if (RT_FAILURE(rc))
1391 return rc;
1392
1393 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
1394 AssertReturn(pGVM->aCpus[idCpu].hEMT == NIL_RTNATIVETHREAD, VERR_ACCESS_DENIED);
1395 Assert(pGVM->cCpus == pVM->cCpus);
1396 Assert(pVM->aCpus[idCpu].hNativeThreadR0 == NIL_RTNATIVETHREAD);
1397
1398 pVM->aCpus[idCpu].hNativeThreadR0 = pGVM->aCpus[idCpu].hEMT = RTThreadNativeSelf();
1399
1400 return VMMR0ThreadCtxHookCreateForEmt(&pVM->aCpus[idCpu]);
1401}
1402
1403
1404/**
1405 * Lookup a GVM structure by its handle.
1406 *
1407 * @returns The GVM pointer on success, NULL on failure.
1408 * @param hGVM The global VM handle. Asserts on bad handle.
1409 */
1410GVMMR0DECL(PGVM) GVMMR0ByHandle(uint32_t hGVM)
1411{
1412 PGVMM pGVMM;
1413 GVMM_GET_VALID_INSTANCE(pGVMM, NULL);
1414
1415 /*
1416 * Validate.
1417 */
1418 AssertReturn(hGVM != NIL_GVM_HANDLE, NULL);
1419 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), NULL);
1420
1421 /*
1422 * Look it up.
1423 */
1424 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1425 AssertPtrReturn(pHandle->pVM, NULL);
1426 AssertPtrReturn(pHandle->pvObj, NULL);
1427 PGVM pGVM = pHandle->pGVM;
1428 AssertPtrReturn(pGVM, NULL);
1429 AssertReturn(pGVM->pVM == pHandle->pVM, NULL);
1430
1431 return pHandle->pGVM;
1432}
1433
1434
1435/**
1436 * Lookup a GVM structure by the shared VM structure.
1437 *
1438 * The calling thread must be in the same process as the VM. All current lookups
1439 * are by threads inside the same process, so this will not be an issue.
1440 *
1441 * @returns VBox status code.
1442 * @param pVM Pointer to the VM.
1443 * @param ppGVM Where to store the GVM pointer.
1444 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1445 * @param fTakeUsedLock Whether to take the used lock or not.
1446 * Be very careful if not taking the lock as it's possible that
1447 * the VM will disappear then.
1448 *
1449 * @remark This will not assert on an invalid pVM but try return silently.
1450 */
1451static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock)
1452{
1453 RTPROCESS ProcId = RTProcSelf();
1454 PGVMM pGVMM;
1455 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
1456
1457 /*
1458 * Validate.
1459 */
1460 if (RT_UNLIKELY( !VALID_PTR(pVM)
1461 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1462 return VERR_INVALID_POINTER;
1463 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1464 || pVM->enmVMState >= VMSTATE_TERMINATED))
1465 return VERR_INVALID_POINTER;
1466
1467 uint16_t hGVM = pVM->hSelf;
1468 if (RT_UNLIKELY( hGVM == NIL_GVM_HANDLE
1469 || hGVM >= RT_ELEMENTS(pGVMM->aHandles)))
1470 return VERR_INVALID_HANDLE;
1471
1472 /*
1473 * Look it up.
1474 */
1475 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1476 PGVM pGVM;
1477 if (fTakeUsedLock)
1478 {
1479 int rc = gvmmR0UsedLock(pGVMM);
1480 AssertRCReturn(rc, rc);
1481
1482 pGVM = pHandle->pGVM;
1483 if (RT_UNLIKELY( pHandle->pVM != pVM
1484 || pHandle->ProcId != ProcId
1485 || !VALID_PTR(pHandle->pvObj)
1486 || !VALID_PTR(pGVM)
1487 || pGVM->pVM != pVM))
1488 {
1489 gvmmR0UsedUnlock(pGVMM);
1490 return VERR_INVALID_HANDLE;
1491 }
1492 }
1493 else
1494 {
1495 if (RT_UNLIKELY(pHandle->pVM != pVM))
1496 return VERR_INVALID_HANDLE;
1497 if (RT_UNLIKELY(pHandle->ProcId != ProcId))
1498 return VERR_INVALID_HANDLE;
1499 if (RT_UNLIKELY(!VALID_PTR(pHandle->pvObj)))
1500 return VERR_INVALID_HANDLE;
1501
1502 pGVM = pHandle->pGVM;
1503 if (RT_UNLIKELY(!VALID_PTR(pGVM)))
1504 return VERR_INVALID_HANDLE;
1505 if (RT_UNLIKELY(pGVM->pVM != pVM))
1506 return VERR_INVALID_HANDLE;
1507 }
1508
1509 *ppGVM = pGVM;
1510 *ppGVMM = pGVMM;
1511 return VINF_SUCCESS;
1512}
1513
1514
1515/**
1516 * Lookup a GVM structure by the shared VM structure.
1517 *
1518 * @returns VBox status code.
1519 * @param pVM Pointer to the VM.
1520 * @param ppGVM Where to store the GVM pointer.
1521 *
1522 * @remark This will not take the 'used'-lock because it doesn't do
1523 * nesting and this function will be used from under the lock.
1524 */
1525GVMMR0DECL(int) GVMMR0ByVM(PVM pVM, PGVM *ppGVM)
1526{
1527 PGVMM pGVMM;
1528 return gvmmR0ByVM(pVM, ppGVM, &pGVMM, false /* fTakeUsedLock */);
1529}
1530
1531
1532/**
1533 * Lookup a GVM structure by the shared VM structure and ensuring that the
1534 * caller is an EMT thread.
1535 *
1536 * @returns VBox status code.
1537 * @param pVM Pointer to the VM.
1538 * @param idCpu The Virtual CPU ID of the calling EMT.
1539 * @param ppGVM Where to store the GVM pointer.
1540 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1541 * @thread EMT
1542 *
1543 * @remark This will assert in all failure paths.
1544 */
1545static int gvmmR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM, PGVMM *ppGVMM)
1546{
1547 PGVMM pGVMM;
1548 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
1549
1550 /*
1551 * Validate.
1552 */
1553 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1554 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1555
1556 uint16_t hGVM = pVM->hSelf;
1557 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
1558 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
1559
1560 /*
1561 * Look it up.
1562 */
1563 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1564 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
1565 RTPROCESS ProcId = RTProcSelf();
1566 AssertReturn(pHandle->ProcId == ProcId, VERR_NOT_OWNER);
1567 AssertPtrReturn(pHandle->pvObj, VERR_NOT_OWNER);
1568
1569 PGVM pGVM = pHandle->pGVM;
1570 AssertPtrReturn(pGVM, VERR_NOT_OWNER);
1571 AssertReturn(pGVM->pVM == pVM, VERR_NOT_OWNER);
1572 RTNATIVETHREAD hAllegedEMT = RTThreadNativeSelf();
1573 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
1574 AssertReturn(pGVM->aCpus[idCpu].hEMT == hAllegedEMT, VERR_NOT_OWNER);
1575
1576 *ppGVM = pGVM;
1577 *ppGVMM = pGVMM;
1578 return VINF_SUCCESS;
1579}
1580
1581
1582/**
1583 * Lookup a GVM structure by the shared VM structure
1584 * and ensuring that the caller is the EMT thread.
1585 *
1586 * @returns VBox status code.
1587 * @param pVM Pointer to the VM.
1588 * @param idCpu The Virtual CPU ID of the calling EMT.
1589 * @param ppGVM Where to store the GVM pointer.
1590 * @thread EMT
1591 */
1592GVMMR0DECL(int) GVMMR0ByVMAndEMT(PVM pVM, VMCPUID idCpu, PGVM *ppGVM)
1593{
1594 AssertPtrReturn(ppGVM, VERR_INVALID_POINTER);
1595 PGVMM pGVMM;
1596 return gvmmR0ByVMAndEMT(pVM, idCpu, ppGVM, &pGVMM);
1597}
1598
1599
1600/**
1601 * Lookup a VM by its global handle.
1602 *
1603 * @returns Pointer to the VM on success, NULL on failure.
1604 * @param hGVM The global VM handle. Asserts on bad handle.
1605 */
1606GVMMR0DECL(PVM) GVMMR0GetVMByHandle(uint32_t hGVM)
1607{
1608 PGVM pGVM = GVMMR0ByHandle(hGVM);
1609 return pGVM ? pGVM->pVM : NULL;
1610}
1611
1612
1613/**
1614 * Looks up the VM belonging to the specified EMT thread.
1615 *
1616 * This is used by the assertion machinery in VMMR0.cpp to avoid causing
1617 * unnecessary kernel panics when the EMT thread hits an assertion. The
1618 * call may or not be an EMT thread.
1619 *
1620 * @returns Pointer to the VM on success, NULL on failure.
1621 * @param hEMT The native thread handle of the EMT.
1622 * NIL_RTNATIVETHREAD means the current thread
1623 */
1624GVMMR0DECL(PVM) GVMMR0GetVMByEMT(RTNATIVETHREAD hEMT)
1625{
1626 /*
1627 * No Assertions here as we're usually called in a AssertMsgN or
1628 * RTAssert* context.
1629 */
1630 PGVMM pGVMM = g_pGVMM;
1631 if ( !VALID_PTR(pGVMM)
1632 || pGVMM->u32Magic != GVMM_MAGIC)
1633 return NULL;
1634
1635 if (hEMT == NIL_RTNATIVETHREAD)
1636 hEMT = RTThreadNativeSelf();
1637 RTPROCESS ProcId = RTProcSelf();
1638
1639 /*
1640 * Search the handles in a linear fashion as we don't dare to take the lock (assert).
1641 */
1642 for (unsigned i = 1; i < RT_ELEMENTS(pGVMM->aHandles); i++)
1643 {
1644 if ( pGVMM->aHandles[i].iSelf == i
1645 && pGVMM->aHandles[i].ProcId == ProcId
1646 && VALID_PTR(pGVMM->aHandles[i].pvObj)
1647 && VALID_PTR(pGVMM->aHandles[i].pVM)
1648 && VALID_PTR(pGVMM->aHandles[i].pGVM))
1649 {
1650 if (pGVMM->aHandles[i].hEMT0 == hEMT)
1651 return pGVMM->aHandles[i].pVM;
1652
1653 /* This is fearly safe with the current process per VM approach. */
1654 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1655 VMCPUID const cCpus = pGVM->cCpus;
1656 if ( cCpus < 1
1657 || cCpus > VMM_MAX_CPU_COUNT)
1658 continue;
1659 for (VMCPUID idCpu = 1; idCpu < cCpus; idCpu++)
1660 if (pGVM->aCpus[idCpu].hEMT == hEMT)
1661 return pGVMM->aHandles[i].pVM;
1662 }
1663 }
1664 return NULL;
1665}
1666
1667
1668/**
1669 * This is will wake up expired and soon-to-be expired VMs.
1670 *
1671 * @returns Number of VMs that has been woken up.
1672 * @param pGVMM Pointer to the GVMM instance data.
1673 * @param u64Now The current time.
1674 */
1675static unsigned gvmmR0SchedDoWakeUps(PGVMM pGVMM, uint64_t u64Now)
1676{
1677 /*
1678 * Skip this if we've got disabled because of high resolution wakeups or by
1679 * the user.
1680 */
1681 if ( !pGVMM->nsEarlyWakeUp1
1682 && !pGVMM->nsEarlyWakeUp2)
1683 return 0;
1684
1685/** @todo Rewrite this algorithm. See performance defect XYZ. */
1686
1687 /*
1688 * A cheap optimization to stop wasting so much time here on big setups.
1689 */
1690 const uint64_t uNsEarlyWakeUp2 = u64Now + pGVMM->nsEarlyWakeUp2;
1691 if ( pGVMM->cHaltedEMTs == 0
1692 || uNsEarlyWakeUp2 > pGVMM->uNsNextEmtWakeup)
1693 return 0;
1694
1695 /*
1696 * The first pass will wake up VMs which have actually expired
1697 * and look for VMs that should be woken up in the 2nd and 3rd passes.
1698 */
1699 const uint64_t uNsEarlyWakeUp1 = u64Now + pGVMM->nsEarlyWakeUp1;
1700 uint64_t u64Min = UINT64_MAX;
1701 unsigned cWoken = 0;
1702 unsigned cHalted = 0;
1703 unsigned cTodo2nd = 0;
1704 unsigned cTodo3rd = 0;
1705 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1706 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1707 i = pGVMM->aHandles[i].iNext)
1708 {
1709 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1710 if ( VALID_PTR(pCurGVM)
1711 && pCurGVM->u32Magic == GVM_MAGIC)
1712 {
1713 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1714 {
1715 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1716 uint64_t u64 = ASMAtomicUoReadU64(&pCurGVCpu->gvmm.s.u64HaltExpire);
1717 if (u64)
1718 {
1719 if (u64 <= u64Now)
1720 {
1721 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1722 {
1723 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1724 AssertRC(rc);
1725 cWoken++;
1726 }
1727 }
1728 else
1729 {
1730 cHalted++;
1731 if (u64 <= uNsEarlyWakeUp1)
1732 cTodo2nd++;
1733 else if (u64 <= uNsEarlyWakeUp2)
1734 cTodo3rd++;
1735 else if (u64 < u64Min)
1736 u64 = u64Min;
1737 }
1738 }
1739 }
1740 }
1741 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1742 }
1743
1744 if (cTodo2nd)
1745 {
1746 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1747 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1748 i = pGVMM->aHandles[i].iNext)
1749 {
1750 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1751 if ( VALID_PTR(pCurGVM)
1752 && pCurGVM->u32Magic == GVM_MAGIC)
1753 {
1754 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1755 {
1756 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1757 uint64_t u64 = ASMAtomicUoReadU64(&pCurGVCpu->gvmm.s.u64HaltExpire);
1758 if ( u64
1759 && u64 <= uNsEarlyWakeUp1)
1760 {
1761 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1762 {
1763 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1764 AssertRC(rc);
1765 cWoken++;
1766 }
1767 }
1768 }
1769 }
1770 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1771 }
1772 }
1773
1774 if (cTodo3rd)
1775 {
1776 for (unsigned i = pGVMM->iUsedHead, cGuard = 0;
1777 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1778 i = pGVMM->aHandles[i].iNext)
1779 {
1780 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1781 if ( VALID_PTR(pCurGVM)
1782 && pCurGVM->u32Magic == GVM_MAGIC)
1783 {
1784 for (VMCPUID idCpu = 0; idCpu < pCurGVM->cCpus; idCpu++)
1785 {
1786 PGVMCPU pCurGVCpu = &pCurGVM->aCpus[idCpu];
1787 uint64_t u64 = ASMAtomicUoReadU64(&pCurGVCpu->gvmm.s.u64HaltExpire);
1788 if ( u64
1789 && u64 <= uNsEarlyWakeUp2)
1790 {
1791 if (ASMAtomicXchgU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0))
1792 {
1793 int rc = RTSemEventMultiSignal(pCurGVCpu->gvmm.s.HaltEventMulti);
1794 AssertRC(rc);
1795 cWoken++;
1796 }
1797 }
1798 }
1799 }
1800 AssertLogRelBreak(cGuard++ < RT_ELEMENTS(pGVMM->aHandles));
1801 }
1802 }
1803
1804 /*
1805 * Set the minimum value.
1806 */
1807 pGVMM->uNsNextEmtWakeup = u64Min;
1808
1809 return cWoken;
1810}
1811
1812
1813/**
1814 * Halt the EMT thread.
1815 *
1816 * @returns VINF_SUCCESS normal wakeup (timeout or kicked by other thread).
1817 * VERR_INTERRUPTED if a signal was scheduled for the thread.
1818 * @param pVM Pointer to the VM.
1819 * @param idCpu The Virtual CPU ID of the calling EMT.
1820 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
1821 * @thread EMT(idCpu).
1822 */
1823GVMMR0DECL(int) GVMMR0SchedHalt(PVM pVM, VMCPUID idCpu, uint64_t u64ExpireGipTime)
1824{
1825 LogFlow(("GVMMR0SchedHalt: pVM=%p\n", pVM));
1826 GVMM_CHECK_SMAP_SETUP();
1827 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1828
1829 /*
1830 * Validate the VM structure, state and handle.
1831 */
1832 PGVM pGVM;
1833 PGVMM pGVMM;
1834 int rc = gvmmR0ByVMAndEMT(pVM, idCpu, &pGVM, &pGVMM);
1835 if (RT_FAILURE(rc))
1836 return rc;
1837 pGVM->gvmm.s.StatsSched.cHaltCalls++;
1838 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1839
1840 PGVMCPU pCurGVCpu = &pGVM->aCpus[idCpu];
1841 Assert(!pCurGVCpu->gvmm.s.u64HaltExpire);
1842
1843 /*
1844 * Take the UsedList semaphore, get the current time
1845 * and check if anyone needs waking up.
1846 * Interrupts must NOT be disabled at this point because we ask for GIP time!
1847 */
1848 rc = gvmmR0UsedLock(pGVMM);
1849 AssertRC(rc);
1850 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1851
1852 pCurGVCpu->gvmm.s.iCpuEmt = ASMGetApicId();
1853
1854 /* GIP hack: We might are frequently sleeping for short intervals where the
1855 difference between GIP and system time matters on systems with high resolution
1856 system time. So, convert the input from GIP to System time in that case. */
1857 Assert(ASMGetFlags() & X86_EFL_IF);
1858 const uint64_t u64NowSys = RTTimeSystemNanoTS();
1859 const uint64_t u64NowGip = RTTimeNanoTS();
1860 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1861 pGVM->gvmm.s.StatsSched.cHaltWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64NowGip);
1862 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1863
1864 /*
1865 * Go to sleep if we must...
1866 * Cap the sleep time to 1 second to be on the safe side.
1867 */
1868 uint64_t cNsInterval = u64ExpireGipTime - u64NowGip;
1869 if ( u64NowGip < u64ExpireGipTime
1870 && cNsInterval >= (pGVMM->cEMTs > pGVMM->cEMTsMeansCompany
1871 ? pGVMM->nsMinSleepCompany
1872 : pGVMM->nsMinSleepAlone))
1873 {
1874 pGVM->gvmm.s.StatsSched.cHaltBlocking++;
1875 if (cNsInterval > RT_NS_1SEC)
1876 u64ExpireGipTime = u64NowGip + RT_NS_1SEC;
1877 if (u64ExpireGipTime < pGVMM->uNsNextEmtWakeup)
1878 pGVMM->uNsNextEmtWakeup = u64ExpireGipTime;
1879 ASMAtomicWriteU64(&pCurGVCpu->gvmm.s.u64HaltExpire, u64ExpireGipTime);
1880 ASMAtomicIncU32(&pGVMM->cHaltedEMTs);
1881 gvmmR0UsedUnlock(pGVMM);
1882 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1883
1884 rc = RTSemEventMultiWaitEx(pCurGVCpu->gvmm.s.HaltEventMulti,
1885 RTSEMWAIT_FLAGS_ABSOLUTE | RTSEMWAIT_FLAGS_NANOSECS | RTSEMWAIT_FLAGS_INTERRUPTIBLE,
1886 u64NowGip > u64NowSys ? u64ExpireGipTime : u64NowSys + cNsInterval);
1887 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1888
1889 ASMAtomicWriteU64(&pCurGVCpu->gvmm.s.u64HaltExpire, 0);
1890 ASMAtomicDecU32(&pGVMM->cHaltedEMTs);
1891
1892 /* Reset the semaphore to try prevent a few false wake-ups. */
1893 if (rc == VINF_SUCCESS)
1894 {
1895 RTSemEventMultiReset(pCurGVCpu->gvmm.s.HaltEventMulti);
1896 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1897 }
1898 else if (rc == VERR_TIMEOUT)
1899 {
1900 pGVM->gvmm.s.StatsSched.cHaltTimeouts++;
1901 rc = VINF_SUCCESS;
1902 }
1903 }
1904 else
1905 {
1906 pGVM->gvmm.s.StatsSched.cHaltNotBlocking++;
1907 gvmmR0UsedUnlock(pGVMM);
1908 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1909 RTSemEventMultiReset(pCurGVCpu->gvmm.s.HaltEventMulti);
1910 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1911 }
1912
1913 return rc;
1914}
1915
1916
1917/**
1918 * Worker for GVMMR0SchedWakeUp and GVMMR0SchedWakeUpAndPokeCpus that wakes up
1919 * the a sleeping EMT.
1920 *
1921 * @retval VINF_SUCCESS if successfully woken up.
1922 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1923 *
1924 * @param pGVM The global (ring-0) VM structure.
1925 * @param pGVCpu The global (ring-0) VCPU structure.
1926 */
1927DECLINLINE(int) gvmmR0SchedWakeUpOne(PGVM pGVM, PGVMCPU pGVCpu)
1928{
1929 pGVM->gvmm.s.StatsSched.cWakeUpCalls++;
1930
1931 /*
1932 * Signal the semaphore regardless of whether it's current blocked on it.
1933 *
1934 * The reason for this is that there is absolutely no way we can be 100%
1935 * certain that it isn't *about* go to go to sleep on it and just got
1936 * delayed a bit en route. So, we will always signal the semaphore when
1937 * the it is flagged as halted in the VMM.
1938 */
1939/** @todo we can optimize some of that by means of the pVCpu->enmState now. */
1940 int rc;
1941 if (pGVCpu->gvmm.s.u64HaltExpire)
1942 {
1943 rc = VINF_SUCCESS;
1944 ASMAtomicWriteU64(&pGVCpu->gvmm.s.u64HaltExpire, 0);
1945 }
1946 else
1947 {
1948 rc = VINF_GVM_NOT_BLOCKED;
1949 pGVM->gvmm.s.StatsSched.cWakeUpNotHalted++;
1950 }
1951
1952 int rc2 = RTSemEventMultiSignal(pGVCpu->gvmm.s.HaltEventMulti);
1953 AssertRC(rc2);
1954
1955 return rc;
1956}
1957
1958
1959/**
1960 * Wakes up the halted EMT thread so it can service a pending request.
1961 *
1962 * @returns VBox status code.
1963 * @retval VINF_SUCCESS if successfully woken up.
1964 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
1965 *
1966 * @param pVM Pointer to the VM.
1967 * @param idCpu The Virtual CPU ID of the EMT to wake up.
1968 * @param fTakeUsedLock Take the used lock or not
1969 * @thread Any but EMT.
1970 */
1971GVMMR0DECL(int) GVMMR0SchedWakeUpEx(PVM pVM, VMCPUID idCpu, bool fTakeUsedLock)
1972{
1973 GVMM_CHECK_SMAP_SETUP();
1974 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1975
1976 /*
1977 * Validate input and take the UsedLock.
1978 */
1979 PGVM pGVM;
1980 PGVMM pGVMM;
1981 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, fTakeUsedLock);
1982 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1983 if (RT_SUCCESS(rc))
1984 {
1985 if (idCpu < pGVM->cCpus)
1986 {
1987 /*
1988 * Do the actual job.
1989 */
1990 rc = gvmmR0SchedWakeUpOne(pGVM, &pGVM->aCpus[idCpu]);
1991 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
1992
1993 if (fTakeUsedLock)
1994 {
1995 /*
1996 * While we're here, do a round of scheduling.
1997 */
1998 Assert(ASMGetFlags() & X86_EFL_IF);
1999 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
2000 pGVM->gvmm.s.StatsSched.cWakeUpWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
2001 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2002 }
2003 }
2004 else
2005 rc = VERR_INVALID_CPU_ID;
2006
2007 if (fTakeUsedLock)
2008 {
2009 int rc2 = gvmmR0UsedUnlock(pGVMM);
2010 AssertRC(rc2);
2011 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2012 }
2013 }
2014
2015 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
2016 return rc;
2017}
2018
2019
2020/**
2021 * Wakes up the halted EMT thread so it can service a pending request.
2022 *
2023 * @returns VBox status code.
2024 * @retval VINF_SUCCESS if successfully woken up.
2025 * @retval VINF_GVM_NOT_BLOCKED if the EMT wasn't blocked.
2026 *
2027 * @param pVM Pointer to the VM.
2028 * @param idCpu The Virtual CPU ID of the EMT to wake up.
2029 * @thread Any but EMT.
2030 */
2031GVMMR0DECL(int) GVMMR0SchedWakeUp(PVM pVM, VMCPUID idCpu)
2032{
2033 return GVMMR0SchedWakeUpEx(pVM, idCpu, true /* fTakeUsedLock */);
2034}
2035
2036/**
2037 * Worker common to GVMMR0SchedPoke and GVMMR0SchedWakeUpAndPokeCpus that pokes
2038 * the Virtual CPU if it's still busy executing guest code.
2039 *
2040 * @returns VBox status code.
2041 * @retval VINF_SUCCESS if poked successfully.
2042 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
2043 *
2044 * @param pGVM The global (ring-0) VM structure.
2045 * @param pVCpu Pointer to the VMCPU.
2046 */
2047DECLINLINE(int) gvmmR0SchedPokeOne(PGVM pGVM, PVMCPU pVCpu)
2048{
2049 pGVM->gvmm.s.StatsSched.cPokeCalls++;
2050
2051 RTCPUID idHostCpu = pVCpu->idHostCpu;
2052 if ( idHostCpu == NIL_RTCPUID
2053 || VMCPU_GET_STATE(pVCpu) != VMCPUSTATE_STARTED_EXEC)
2054 {
2055 pGVM->gvmm.s.StatsSched.cPokeNotBusy++;
2056 return VINF_GVM_NOT_BUSY_IN_GC;
2057 }
2058
2059 /* Note: this function is not implemented on Darwin and Linux (kernel < 2.6.19) */
2060 RTMpPokeCpu(idHostCpu);
2061 return VINF_SUCCESS;
2062}
2063
2064/**
2065 * Pokes an EMT if it's still busy running guest code.
2066 *
2067 * @returns VBox status code.
2068 * @retval VINF_SUCCESS if poked successfully.
2069 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
2070 *
2071 * @param pVM Pointer to the VM.
2072 * @param idCpu The ID of the virtual CPU to poke.
2073 * @param fTakeUsedLock Take the used lock or not
2074 */
2075GVMMR0DECL(int) GVMMR0SchedPokeEx(PVM pVM, VMCPUID idCpu, bool fTakeUsedLock)
2076{
2077 /*
2078 * Validate input and take the UsedLock.
2079 */
2080 PGVM pGVM;
2081 PGVMM pGVMM;
2082 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, fTakeUsedLock);
2083 if (RT_SUCCESS(rc))
2084 {
2085 if (idCpu < pGVM->cCpus)
2086 rc = gvmmR0SchedPokeOne(pGVM, &pVM->aCpus[idCpu]);
2087 else
2088 rc = VERR_INVALID_CPU_ID;
2089
2090 if (fTakeUsedLock)
2091 {
2092 int rc2 = gvmmR0UsedUnlock(pGVMM);
2093 AssertRC(rc2);
2094 }
2095 }
2096
2097 LogFlow(("GVMMR0SchedWakeUpAndPokeCpus: returns %Rrc\n", rc));
2098 return rc;
2099}
2100
2101
2102/**
2103 * Pokes an EMT if it's still busy running guest code.
2104 *
2105 * @returns VBox status code.
2106 * @retval VINF_SUCCESS if poked successfully.
2107 * @retval VINF_GVM_NOT_BUSY_IN_GC if the EMT wasn't busy in GC.
2108 *
2109 * @param pVM Pointer to the VM.
2110 * @param idCpu The ID of the virtual CPU to poke.
2111 */
2112GVMMR0DECL(int) GVMMR0SchedPoke(PVM pVM, VMCPUID idCpu)
2113{
2114 return GVMMR0SchedPokeEx(pVM, idCpu, true /* fTakeUsedLock */);
2115}
2116
2117
2118/**
2119 * Wakes up a set of halted EMT threads so they can service pending request.
2120 *
2121 * @returns VBox status code, no informational stuff.
2122 *
2123 * @param pVM Pointer to the VM.
2124 * @param pSleepSet The set of sleepers to wake up.
2125 * @param pPokeSet The set of CPUs to poke.
2126 */
2127GVMMR0DECL(int) GVMMR0SchedWakeUpAndPokeCpus(PVM pVM, PCVMCPUSET pSleepSet, PCVMCPUSET pPokeSet)
2128{
2129 AssertPtrReturn(pSleepSet, VERR_INVALID_POINTER);
2130 AssertPtrReturn(pPokeSet, VERR_INVALID_POINTER);
2131 GVMM_CHECK_SMAP_SETUP();
2132 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2133 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
2134
2135 /*
2136 * Validate input and take the UsedLock.
2137 */
2138 PGVM pGVM;
2139 PGVMM pGVMM;
2140 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
2141 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2142 if (RT_SUCCESS(rc))
2143 {
2144 rc = VINF_SUCCESS;
2145 VMCPUID idCpu = pGVM->cCpus;
2146 while (idCpu-- > 0)
2147 {
2148 /* Don't try poke or wake up ourselves. */
2149 if (pGVM->aCpus[idCpu].hEMT == hSelf)
2150 continue;
2151
2152 /* just ignore errors for now. */
2153 if (VMCPUSET_IS_PRESENT(pSleepSet, idCpu))
2154 {
2155 gvmmR0SchedWakeUpOne(pGVM, &pGVM->aCpus[idCpu]);
2156 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2157 }
2158 else if (VMCPUSET_IS_PRESENT(pPokeSet, idCpu))
2159 {
2160 gvmmR0SchedPokeOne(pGVM, &pVM->aCpus[idCpu]);
2161 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2162 }
2163 }
2164
2165 int rc2 = gvmmR0UsedUnlock(pGVMM);
2166 AssertRC(rc2);
2167 GVMM_CHECK_SMAP_CHECK2(pVM, RT_NOTHING);
2168 }
2169
2170 LogFlow(("GVMMR0SchedWakeUpAndPokeCpus: returns %Rrc\n", rc));
2171 return rc;
2172}
2173
2174
2175/**
2176 * VMMR0 request wrapper for GVMMR0SchedWakeUpAndPokeCpus.
2177 *
2178 * @returns see GVMMR0SchedWakeUpAndPokeCpus.
2179 * @param pVM Pointer to the VM.
2180 * @param pReq Pointer to the request packet.
2181 */
2182GVMMR0DECL(int) GVMMR0SchedWakeUpAndPokeCpusReq(PVM pVM, PGVMMSCHEDWAKEUPANDPOKECPUSREQ pReq)
2183{
2184 /*
2185 * Validate input and pass it on.
2186 */
2187 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2188 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2189
2190 return GVMMR0SchedWakeUpAndPokeCpus(pVM, &pReq->SleepSet, &pReq->PokeSet);
2191}
2192
2193
2194
2195/**
2196 * Poll the schedule to see if someone else should get a chance to run.
2197 *
2198 * This is a bit hackish and will not work too well if the machine is
2199 * under heavy load from non-VM processes.
2200 *
2201 * @returns VINF_SUCCESS if not yielded.
2202 * VINF_GVM_YIELDED if an attempt to switch to a different VM task was made.
2203 * @param pVM Pointer to the VM.
2204 * @param idCpu The Virtual CPU ID of the calling EMT.
2205 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
2206 * @param fYield Whether to yield or not.
2207 * This is for when we're spinning in the halt loop.
2208 * @thread EMT(idCpu).
2209 */
2210GVMMR0DECL(int) GVMMR0SchedPoll(PVM pVM, VMCPUID idCpu, bool fYield)
2211{
2212 /*
2213 * Validate input.
2214 */
2215 PGVM pGVM;
2216 PGVMM pGVMM;
2217 int rc = gvmmR0ByVMAndEMT(pVM, idCpu, &pGVM, &pGVMM);
2218 if (RT_SUCCESS(rc))
2219 {
2220 rc = gvmmR0UsedLock(pGVMM);
2221 AssertRC(rc);
2222 pGVM->gvmm.s.StatsSched.cPollCalls++;
2223
2224 Assert(ASMGetFlags() & X86_EFL_IF);
2225 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
2226
2227 if (!fYield)
2228 pGVM->gvmm.s.StatsSched.cPollWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
2229 else
2230 {
2231 /** @todo implement this... */
2232 rc = VERR_NOT_IMPLEMENTED;
2233 }
2234
2235 gvmmR0UsedUnlock(pGVMM);
2236 }
2237
2238 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
2239 return rc;
2240}
2241
2242
2243#ifdef GVMM_SCHED_WITH_PPT
2244/**
2245 * Timer callback for the periodic preemption timer.
2246 *
2247 * @param pTimer The timer handle.
2248 * @param pvUser Pointer to the per cpu structure.
2249 * @param iTick The current tick.
2250 */
2251static DECLCALLBACK(void) gvmmR0SchedPeriodicPreemptionTimerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick)
2252{
2253 PGVMMHOSTCPU pCpu = (PGVMMHOSTCPU)pvUser;
2254 NOREF(pTimer); NOREF(iTick);
2255
2256 /*
2257 * Termination check
2258 */
2259 if (pCpu->u32Magic != GVMMHOSTCPU_MAGIC)
2260 return;
2261
2262 /*
2263 * Do the house keeping.
2264 */
2265 RTSpinlockAcquire(pCpu->Ppt.hSpinlock);
2266
2267 if (++pCpu->Ppt.iTickHistorization >= pCpu->Ppt.cTicksHistoriziationInterval)
2268 {
2269 /*
2270 * Historicize the max frequency.
2271 */
2272 uint32_t iHzHistory = ++pCpu->Ppt.iHzHistory % RT_ELEMENTS(pCpu->Ppt.aHzHistory);
2273 pCpu->Ppt.aHzHistory[iHzHistory] = pCpu->Ppt.uDesiredHz;
2274 pCpu->Ppt.iTickHistorization = 0;
2275 pCpu->Ppt.uDesiredHz = 0;
2276
2277 /*
2278 * Check if the current timer frequency.
2279 */
2280 uint32_t uHistMaxHz = 0;
2281 for (uint32_t i = 0; i < RT_ELEMENTS(pCpu->Ppt.aHzHistory); i++)
2282 if (pCpu->Ppt.aHzHistory[i] > uHistMaxHz)
2283 uHistMaxHz = pCpu->Ppt.aHzHistory[i];
2284 if (uHistMaxHz == pCpu->Ppt.uTimerHz)
2285 RTSpinlockRelease(pCpu->Ppt.hSpinlock);
2286 else if (uHistMaxHz)
2287 {
2288 /*
2289 * Reprogram it.
2290 */
2291 pCpu->Ppt.cChanges++;
2292 pCpu->Ppt.iTickHistorization = 0;
2293 pCpu->Ppt.uTimerHz = uHistMaxHz;
2294 uint32_t const cNsInterval = RT_NS_1SEC / uHistMaxHz;
2295 pCpu->Ppt.cNsInterval = cNsInterval;
2296 if (cNsInterval < GVMMHOSTCPU_PPT_HIST_INTERVAL_NS)
2297 pCpu->Ppt.cTicksHistoriziationInterval = ( GVMMHOSTCPU_PPT_HIST_INTERVAL_NS
2298 + GVMMHOSTCPU_PPT_HIST_INTERVAL_NS / 2 - 1)
2299 / cNsInterval;
2300 else
2301 pCpu->Ppt.cTicksHistoriziationInterval = 1;
2302 RTSpinlockRelease(pCpu->Ppt.hSpinlock);
2303
2304 /*SUPR0Printf("Cpu%u: change to %u Hz / %u ns\n", pCpu->idxCpuSet, uHistMaxHz, cNsInterval);*/
2305 RTTimerChangeInterval(pTimer, cNsInterval);
2306 }
2307 else
2308 {
2309 /*
2310 * Stop it.
2311 */
2312 pCpu->Ppt.fStarted = false;
2313 pCpu->Ppt.uTimerHz = 0;
2314 pCpu->Ppt.cNsInterval = 0;
2315 RTSpinlockRelease(pCpu->Ppt.hSpinlock);
2316
2317 /*SUPR0Printf("Cpu%u: stopping (%u Hz)\n", pCpu->idxCpuSet, uHistMaxHz);*/
2318 RTTimerStop(pTimer);
2319 }
2320 }
2321 else
2322 RTSpinlockRelease(pCpu->Ppt.hSpinlock);
2323}
2324#endif /* GVMM_SCHED_WITH_PPT */
2325
2326
2327/**
2328 * Updates the periodic preemption timer for the calling CPU.
2329 *
2330 * The caller must have disabled preemption!
2331 * The caller must check that the host can do high resolution timers.
2332 *
2333 * @param pVM Pointer to the VM.
2334 * @param idHostCpu The current host CPU id.
2335 * @param uHz The desired frequency.
2336 */
2337GVMMR0DECL(void) GVMMR0SchedUpdatePeriodicPreemptionTimer(PVM pVM, RTCPUID idHostCpu, uint32_t uHz)
2338{
2339 NOREF(pVM);
2340#ifdef GVMM_SCHED_WITH_PPT
2341 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2342 Assert(RTTimerCanDoHighResolution());
2343
2344 /*
2345 * Resolve the per CPU data.
2346 */
2347 uint32_t iCpu = RTMpCpuIdToSetIndex(idHostCpu);
2348 PGVMM pGVMM = g_pGVMM;
2349 if ( !VALID_PTR(pGVMM)
2350 || pGVMM->u32Magic != GVMM_MAGIC)
2351 return;
2352 AssertMsgReturnVoid(iCpu < pGVMM->cHostCpus, ("iCpu=%d cHostCpus=%d\n", iCpu, pGVMM->cHostCpus));
2353 PGVMMHOSTCPU pCpu = &pGVMM->aHostCpus[iCpu];
2354 AssertMsgReturnVoid( pCpu->u32Magic == GVMMHOSTCPU_MAGIC
2355 && pCpu->idCpu == idHostCpu,
2356 ("u32Magic=%#x idCpu=% idHostCpu=%d\n", pCpu->u32Magic, pCpu->idCpu, idHostCpu));
2357
2358 /*
2359 * Check whether we need to do anything about the timer.
2360 * We have to be a little bit careful since we might be race the timer
2361 * callback here.
2362 */
2363 if (uHz > 16384)
2364 uHz = 16384; /** @todo add a query method for this! */
2365 if (RT_UNLIKELY( uHz > ASMAtomicReadU32(&pCpu->Ppt.uDesiredHz)
2366 && uHz >= pCpu->Ppt.uMinHz
2367 && !pCpu->Ppt.fStarting /* solaris paranoia */))
2368 {
2369 RTSpinlockAcquire(pCpu->Ppt.hSpinlock);
2370
2371 pCpu->Ppt.uDesiredHz = uHz;
2372 uint32_t cNsInterval = 0;
2373 if (!pCpu->Ppt.fStarted)
2374 {
2375 pCpu->Ppt.cStarts++;
2376 pCpu->Ppt.fStarted = true;
2377 pCpu->Ppt.fStarting = true;
2378 pCpu->Ppt.iTickHistorization = 0;
2379 pCpu->Ppt.uTimerHz = uHz;
2380 pCpu->Ppt.cNsInterval = cNsInterval = RT_NS_1SEC / uHz;
2381 if (cNsInterval < GVMMHOSTCPU_PPT_HIST_INTERVAL_NS)
2382 pCpu->Ppt.cTicksHistoriziationInterval = ( GVMMHOSTCPU_PPT_HIST_INTERVAL_NS
2383 + GVMMHOSTCPU_PPT_HIST_INTERVAL_NS / 2 - 1)
2384 / cNsInterval;
2385 else
2386 pCpu->Ppt.cTicksHistoriziationInterval = 1;
2387 }
2388
2389 RTSpinlockRelease(pCpu->Ppt.hSpinlock);
2390
2391 if (cNsInterval)
2392 {
2393 RTTimerChangeInterval(pCpu->Ppt.pTimer, cNsInterval);
2394 int rc = RTTimerStart(pCpu->Ppt.pTimer, cNsInterval);
2395 AssertRC(rc);
2396
2397 RTSpinlockAcquire(pCpu->Ppt.hSpinlock);
2398 if (RT_FAILURE(rc))
2399 pCpu->Ppt.fStarted = false;
2400 pCpu->Ppt.fStarting = false;
2401 RTSpinlockRelease(pCpu->Ppt.hSpinlock);
2402 }
2403 }
2404#else /* !GVMM_SCHED_WITH_PPT */
2405 NOREF(idHostCpu); NOREF(uHz);
2406#endif /* !GVMM_SCHED_WITH_PPT */
2407}
2408
2409
2410/**
2411 * Retrieves the GVMM statistics visible to the caller.
2412 *
2413 * @returns VBox status code.
2414 *
2415 * @param pStats Where to put the statistics.
2416 * @param pSession The current session.
2417 * @param pVM The VM to obtain statistics for. Optional.
2418 */
2419GVMMR0DECL(int) GVMMR0QueryStatistics(PGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
2420{
2421 LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
2422
2423 /*
2424 * Validate input.
2425 */
2426 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
2427 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
2428 pStats->cVMs = 0; /* (crash before taking the sem...) */
2429
2430 /*
2431 * Take the lock and get the VM statistics.
2432 */
2433 PGVMM pGVMM;
2434 if (pVM)
2435 {
2436 PGVM pGVM;
2437 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
2438 if (RT_FAILURE(rc))
2439 return rc;
2440 pStats->SchedVM = pGVM->gvmm.s.StatsSched;
2441 }
2442 else
2443 {
2444 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
2445 memset(&pStats->SchedVM, 0, sizeof(pStats->SchedVM));
2446
2447 int rc = gvmmR0UsedLock(pGVMM);
2448 AssertRCReturn(rc, rc);
2449 }
2450
2451 /*
2452 * Enumerate the VMs and add the ones visible to the statistics.
2453 */
2454 pStats->cVMs = 0;
2455 pStats->cEMTs = 0;
2456 memset(&pStats->SchedSum, 0, sizeof(pStats->SchedSum));
2457
2458 for (unsigned i = pGVMM->iUsedHead;
2459 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
2460 i = pGVMM->aHandles[i].iNext)
2461 {
2462 PGVM pGVM = pGVMM->aHandles[i].pGVM;
2463 void *pvObj = pGVMM->aHandles[i].pvObj;
2464 if ( VALID_PTR(pvObj)
2465 && VALID_PTR(pGVM)
2466 && pGVM->u32Magic == GVM_MAGIC
2467 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
2468 {
2469 pStats->cVMs++;
2470 pStats->cEMTs += pGVM->cCpus;
2471
2472 pStats->SchedSum.cHaltCalls += pGVM->gvmm.s.StatsSched.cHaltCalls;
2473 pStats->SchedSum.cHaltBlocking += pGVM->gvmm.s.StatsSched.cHaltBlocking;
2474 pStats->SchedSum.cHaltTimeouts += pGVM->gvmm.s.StatsSched.cHaltTimeouts;
2475 pStats->SchedSum.cHaltNotBlocking += pGVM->gvmm.s.StatsSched.cHaltNotBlocking;
2476 pStats->SchedSum.cHaltWakeUps += pGVM->gvmm.s.StatsSched.cHaltWakeUps;
2477
2478 pStats->SchedSum.cWakeUpCalls += pGVM->gvmm.s.StatsSched.cWakeUpCalls;
2479 pStats->SchedSum.cWakeUpNotHalted += pGVM->gvmm.s.StatsSched.cWakeUpNotHalted;
2480 pStats->SchedSum.cWakeUpWakeUps += pGVM->gvmm.s.StatsSched.cWakeUpWakeUps;
2481
2482 pStats->SchedSum.cPokeCalls += pGVM->gvmm.s.StatsSched.cPokeCalls;
2483 pStats->SchedSum.cPokeNotBusy += pGVM->gvmm.s.StatsSched.cPokeNotBusy;
2484
2485 pStats->SchedSum.cPollCalls += pGVM->gvmm.s.StatsSched.cPollCalls;
2486 pStats->SchedSum.cPollHalts += pGVM->gvmm.s.StatsSched.cPollHalts;
2487 pStats->SchedSum.cPollWakeUps += pGVM->gvmm.s.StatsSched.cPollWakeUps;
2488 }
2489 }
2490
2491 /*
2492 * Copy out the per host CPU statistics.
2493 */
2494 uint32_t iDstCpu = 0;
2495 uint32_t cSrcCpus = pGVMM->cHostCpus;
2496 for (uint32_t iSrcCpu = 0; iSrcCpu < cSrcCpus; iSrcCpu++)
2497 {
2498 if (pGVMM->aHostCpus[iSrcCpu].idCpu != NIL_RTCPUID)
2499 {
2500 pStats->aHostCpus[iDstCpu].idCpu = pGVMM->aHostCpus[iSrcCpu].idCpu;
2501 pStats->aHostCpus[iDstCpu].idxCpuSet = pGVMM->aHostCpus[iSrcCpu].idxCpuSet;
2502#ifdef GVMM_SCHED_WITH_PPT
2503 pStats->aHostCpus[iDstCpu].uDesiredHz = pGVMM->aHostCpus[iSrcCpu].Ppt.uDesiredHz;
2504 pStats->aHostCpus[iDstCpu].uTimerHz = pGVMM->aHostCpus[iSrcCpu].Ppt.uTimerHz;
2505 pStats->aHostCpus[iDstCpu].cChanges = pGVMM->aHostCpus[iSrcCpu].Ppt.cChanges;
2506 pStats->aHostCpus[iDstCpu].cStarts = pGVMM->aHostCpus[iSrcCpu].Ppt.cStarts;
2507#else
2508 pStats->aHostCpus[iDstCpu].uDesiredHz = 0;
2509 pStats->aHostCpus[iDstCpu].uTimerHz = 0;
2510 pStats->aHostCpus[iDstCpu].cChanges = 0;
2511 pStats->aHostCpus[iDstCpu].cStarts = 0;
2512#endif
2513 iDstCpu++;
2514 if (iDstCpu >= RT_ELEMENTS(pStats->aHostCpus))
2515 break;
2516 }
2517 }
2518 pStats->cHostCpus = iDstCpu;
2519
2520 gvmmR0UsedUnlock(pGVMM);
2521
2522 return VINF_SUCCESS;
2523}
2524
2525
2526/**
2527 * VMMR0 request wrapper for GVMMR0QueryStatistics.
2528 *
2529 * @returns see GVMMR0QueryStatistics.
2530 * @param pVM Pointer to the VM. Optional.
2531 * @param pReq Pointer to the request packet.
2532 */
2533GVMMR0DECL(int) GVMMR0QueryStatisticsReq(PVM pVM, PGVMMQUERYSTATISTICSSREQ pReq)
2534{
2535 /*
2536 * Validate input and pass it on.
2537 */
2538 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2539 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2540
2541 return GVMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pVM);
2542}
2543
2544
2545/**
2546 * Resets the specified GVMM statistics.
2547 *
2548 * @returns VBox status code.
2549 *
2550 * @param pStats Which statistics to reset, that is, non-zero fields indicates which to reset.
2551 * @param pSession The current session.
2552 * @param pVM The VM to reset statistics for. Optional.
2553 */
2554GVMMR0DECL(int) GVMMR0ResetStatistics(PCGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
2555{
2556 LogFlow(("GVMMR0ResetStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
2557
2558 /*
2559 * Validate input.
2560 */
2561 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
2562 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
2563
2564 /*
2565 * Take the lock and get the VM statistics.
2566 */
2567 PGVMM pGVMM;
2568 if (pVM)
2569 {
2570 PGVM pGVM;
2571 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
2572 if (RT_FAILURE(rc))
2573 return rc;
2574# define MAYBE_RESET_FIELD(field) \
2575 do { if (pStats->SchedVM. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
2576 MAYBE_RESET_FIELD(cHaltCalls);
2577 MAYBE_RESET_FIELD(cHaltBlocking);
2578 MAYBE_RESET_FIELD(cHaltTimeouts);
2579 MAYBE_RESET_FIELD(cHaltNotBlocking);
2580 MAYBE_RESET_FIELD(cHaltWakeUps);
2581 MAYBE_RESET_FIELD(cWakeUpCalls);
2582 MAYBE_RESET_FIELD(cWakeUpNotHalted);
2583 MAYBE_RESET_FIELD(cWakeUpWakeUps);
2584 MAYBE_RESET_FIELD(cPokeCalls);
2585 MAYBE_RESET_FIELD(cPokeNotBusy);
2586 MAYBE_RESET_FIELD(cPollCalls);
2587 MAYBE_RESET_FIELD(cPollHalts);
2588 MAYBE_RESET_FIELD(cPollWakeUps);
2589# undef MAYBE_RESET_FIELD
2590 }
2591 else
2592 {
2593 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_GVMM_INSTANCE);
2594
2595 int rc = gvmmR0UsedLock(pGVMM);
2596 AssertRCReturn(rc, rc);
2597 }
2598
2599 /*
2600 * Enumerate the VMs and add the ones visible to the statistics.
2601 */
2602 if (ASMMemIsAll8(&pStats->SchedSum, sizeof(pStats->SchedSum), 0))
2603 {
2604 for (unsigned i = pGVMM->iUsedHead;
2605 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
2606 i = pGVMM->aHandles[i].iNext)
2607 {
2608 PGVM pGVM = pGVMM->aHandles[i].pGVM;
2609 void *pvObj = pGVMM->aHandles[i].pvObj;
2610 if ( VALID_PTR(pvObj)
2611 && VALID_PTR(pGVM)
2612 && pGVM->u32Magic == GVM_MAGIC
2613 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
2614 {
2615# define MAYBE_RESET_FIELD(field) \
2616 do { if (pStats->SchedSum. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
2617 MAYBE_RESET_FIELD(cHaltCalls);
2618 MAYBE_RESET_FIELD(cHaltBlocking);
2619 MAYBE_RESET_FIELD(cHaltTimeouts);
2620 MAYBE_RESET_FIELD(cHaltNotBlocking);
2621 MAYBE_RESET_FIELD(cHaltWakeUps);
2622 MAYBE_RESET_FIELD(cWakeUpCalls);
2623 MAYBE_RESET_FIELD(cWakeUpNotHalted);
2624 MAYBE_RESET_FIELD(cWakeUpWakeUps);
2625 MAYBE_RESET_FIELD(cPokeCalls);
2626 MAYBE_RESET_FIELD(cPokeNotBusy);
2627 MAYBE_RESET_FIELD(cPollCalls);
2628 MAYBE_RESET_FIELD(cPollHalts);
2629 MAYBE_RESET_FIELD(cPollWakeUps);
2630# undef MAYBE_RESET_FIELD
2631 }
2632 }
2633 }
2634
2635 gvmmR0UsedUnlock(pGVMM);
2636
2637 return VINF_SUCCESS;
2638}
2639
2640
2641/**
2642 * VMMR0 request wrapper for GVMMR0ResetStatistics.
2643 *
2644 * @returns see GVMMR0ResetStatistics.
2645 * @param pVM Pointer to the VM. Optional.
2646 * @param pReq Pointer to the request packet.
2647 */
2648GVMMR0DECL(int) GVMMR0ResetStatisticsReq(PVM pVM, PGVMMRESETSTATISTICSSREQ pReq)
2649{
2650 /*
2651 * Validate input and pass it on.
2652 */
2653 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
2654 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
2655
2656 return GVMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pVM);
2657}
2658
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