1 | /* $Id: GVMMR0.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GVMM - Global VM Manager.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
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. It's main function at the moment
|
---|
22 | * is to manage a list of all running VMs, keep a ring-0 only structure (GVM)
|
---|
23 | * for each of them, and assign them unique identifiers (so GMM can track
|
---|
24 | * page owners). The idea for the future is to add an idle priority kernel
|
---|
25 | * thread that can take care of tasks like page sharing.
|
---|
26 | *
|
---|
27 | * The GVMM will create a ring-0 object for each VM when it's registered,
|
---|
28 | * this is both for session cleanup purposes and for having a point where
|
---|
29 | * it's possible to implement usage polices later (in SUPR0ObjRegister).
|
---|
30 | */
|
---|
31 |
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Header Files *
|
---|
35 | *******************************************************************************/
|
---|
36 | #define LOG_GROUP LOG_GROUP_GVMM
|
---|
37 | #include <VBox/gvmm.h>
|
---|
38 | #include "GVMMR0Internal.h"
|
---|
39 | #include <VBox/gvm.h>
|
---|
40 | #include <VBox/vm.h>
|
---|
41 | #include <VBox/err.h>
|
---|
42 | #include <iprt/alloc.h>
|
---|
43 | #include <iprt/semaphore.h>
|
---|
44 | #include <iprt/time.h>
|
---|
45 | #include <VBox/log.h>
|
---|
46 | #include <iprt/thread.h>
|
---|
47 | #include <iprt/param.h>
|
---|
48 | #include <iprt/string.h>
|
---|
49 | #include <iprt/assert.h>
|
---|
50 | #include <iprt/mem.h>
|
---|
51 | #include <iprt/memobj.h>
|
---|
52 |
|
---|
53 |
|
---|
54 | /*******************************************************************************
|
---|
55 | * Structures and Typedefs *
|
---|
56 | *******************************************************************************/
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Global VM handle.
|
---|
60 | */
|
---|
61 | typedef struct GVMHANDLE
|
---|
62 | {
|
---|
63 | /** The index of the next handle in the list (free or used). (0 is nil.) */
|
---|
64 | uint16_t volatile iNext;
|
---|
65 | /** Our own index / handle value. */
|
---|
66 | uint16_t iSelf;
|
---|
67 | /** The pointer to the ring-0 only (aka global) VM structure. */
|
---|
68 | PGVM pGVM;
|
---|
69 | /** The ring-0 mapping of the shared VM instance data. */
|
---|
70 | PVM pVM;
|
---|
71 | /** The virtual machine object. */
|
---|
72 | void *pvObj;
|
---|
73 | /** The session this VM is associated with. */
|
---|
74 | PSUPDRVSESSION pSession;
|
---|
75 | /** The ring-0 handle of the EMT thread.
|
---|
76 | * This is used for assertions and similar cases where we need to find the VM handle. */
|
---|
77 | RTNATIVETHREAD hEMT;
|
---|
78 | } GVMHANDLE;
|
---|
79 | /** Pointer to a global VM handle. */
|
---|
80 | typedef GVMHANDLE *PGVMHANDLE;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * The GVMM instance data.
|
---|
84 | */
|
---|
85 | typedef struct GVMM
|
---|
86 | {
|
---|
87 | /** Eyecatcher / magic. */
|
---|
88 | uint32_t u32Magic;
|
---|
89 | /** The index of the head of the free handle chain. (0 is nil.) */
|
---|
90 | uint16_t volatile iFreeHead;
|
---|
91 | /** The index of the head of the active handle chain. (0 is nil.) */
|
---|
92 | uint16_t volatile iUsedHead;
|
---|
93 | /** The number of VMs. */
|
---|
94 | uint16_t volatile cVMs;
|
---|
95 | // /** The number of halted EMT threads. */
|
---|
96 | // uint16_t volatile cHaltedEMTs;
|
---|
97 | /** The lock used to serialize VM creation, destruction and associated events that
|
---|
98 | * isn't performance critical. Owners may acquire the list lock. */
|
---|
99 | RTSEMFASTMUTEX CreateDestroyLock;
|
---|
100 | /** The lock used to serialize used list updates and accesses.
|
---|
101 | * This indirectly includes scheduling since the scheduler will have to walk the
|
---|
102 | * used list to examin running VMs. Owners may not acquire any other locks. */
|
---|
103 | RTSEMFASTMUTEX UsedLock;
|
---|
104 | /** The handle array.
|
---|
105 | * The size of this array defines the maximum number of currently running VMs.
|
---|
106 | * The first entry is unused as it represents the NIL handle. */
|
---|
107 | GVMHANDLE aHandles[128];
|
---|
108 |
|
---|
109 | /** The number of VMs that means we no longer considers ourselves along on a CPU/Core.
|
---|
110 | * @gcfgm /GVMM/cVMsMeansCompany 32-bit 0..UINT32_MAX
|
---|
111 | */
|
---|
112 | uint32_t cVMsMeansCompany;
|
---|
113 | /** The minimum sleep time for when we're alone, in nano seconds.
|
---|
114 | * @gcfgm /GVMM/MinSleepAlone 32-bit 0..100000000
|
---|
115 | */
|
---|
116 | uint32_t nsMinSleepAlone;
|
---|
117 | /** The minimum sleep time for when we've got company, in nano seconds.
|
---|
118 | * @gcfgm /GVMM/MinSleepCompany 32-bit 0..100000000
|
---|
119 | */
|
---|
120 | uint32_t nsMinSleepCompany;
|
---|
121 | /** The limit for the first round of early wakeups, given in nano seconds.
|
---|
122 | * @gcfgm /GVMM/EarlyWakeUp1 32-bit 0..100000000
|
---|
123 | */
|
---|
124 | uint32_t nsEarlyWakeUp1;
|
---|
125 | /** The limit for the second round of early wakeups, given in nano seconds.
|
---|
126 | * @gcfgm /GVMM/EarlyWakeUp2 32-bit 0..100000000
|
---|
127 | */
|
---|
128 | uint32_t nsEarlyWakeUp2;
|
---|
129 | } GVMM;
|
---|
130 | /** Pointer to the GVMM instance data. */
|
---|
131 | typedef GVMM *PGVMM;
|
---|
132 |
|
---|
133 | /** The GVMM::u32Magic value (Charlie Haden). */
|
---|
134 | #define GVMM_MAGIC 0x19370806
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 | /*******************************************************************************
|
---|
139 | * Global Variables *
|
---|
140 | *******************************************************************************/
|
---|
141 | /** Pointer to the GVMM instance data.
|
---|
142 | * (Just my general dislike for global variables.) */
|
---|
143 | static PGVMM g_pGVMM = NULL;
|
---|
144 |
|
---|
145 | /** Macro for obtaining and validating the g_pGVMM pointer.
|
---|
146 | * On failure it will return from the invoking function with the specified return value.
|
---|
147 | *
|
---|
148 | * @param pGVMM The name of the pGVMM variable.
|
---|
149 | * @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
|
---|
150 | * VBox status codes.
|
---|
151 | */
|
---|
152 | #define GVMM_GET_VALID_INSTANCE(pGVMM, rc) \
|
---|
153 | do { \
|
---|
154 | (pGVMM) = g_pGVMM;\
|
---|
155 | AssertPtrReturn((pGVMM), (rc)); \
|
---|
156 | AssertMsgReturn((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic), (rc)); \
|
---|
157 | } while (0)
|
---|
158 |
|
---|
159 | /** Macro for obtaining and validating the g_pGVMM pointer, void function variant.
|
---|
160 | * On failure it will return from the invoking function.
|
---|
161 | *
|
---|
162 | * @param pGVMM The name of the pGVMM variable.
|
---|
163 | */
|
---|
164 | #define GVMM_GET_VALID_INSTANCE_VOID(pGVMM) \
|
---|
165 | do { \
|
---|
166 | (pGVMM) = g_pGVMM;\
|
---|
167 | AssertPtrReturnVoid((pGVMM)); \
|
---|
168 | AssertMsgReturnVoid((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic)); \
|
---|
169 | } while (0)
|
---|
170 |
|
---|
171 |
|
---|
172 | /*******************************************************************************
|
---|
173 | * Internal Functions *
|
---|
174 | *******************************************************************************/
|
---|
175 | static void gvmmR0InitPerVMData(PGVM pGVM);
|
---|
176 | static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle);
|
---|
177 | static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock);
|
---|
178 | static int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM);
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Initializes the GVMM.
|
---|
183 | *
|
---|
184 | * This is called while owninng the loader sempahore (see supdrvIOCtl_LdrLoad()).
|
---|
185 | *
|
---|
186 | * @returns VBox status code.
|
---|
187 | */
|
---|
188 | GVMMR0DECL(int) GVMMR0Init(void)
|
---|
189 | {
|
---|
190 | LogFlow(("GVMMR0Init:\n"));
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * Allocate and initialize the instance data.
|
---|
194 | */
|
---|
195 | PGVMM pGVMM = (PGVMM)RTMemAllocZ(sizeof(*pGVMM));
|
---|
196 | if (!pGVMM)
|
---|
197 | return VERR_NO_MEMORY;
|
---|
198 | int rc = RTSemFastMutexCreate(&pGVMM->CreateDestroyLock);
|
---|
199 | if (RT_SUCCESS(rc))
|
---|
200 | {
|
---|
201 | rc = RTSemFastMutexCreate(&pGVMM->UsedLock);
|
---|
202 | if (RT_SUCCESS(rc))
|
---|
203 | {
|
---|
204 | pGVMM->u32Magic = GVMM_MAGIC;
|
---|
205 | pGVMM->iUsedHead = 0;
|
---|
206 | pGVMM->iFreeHead = 1;
|
---|
207 |
|
---|
208 | /* the nil handle */
|
---|
209 | pGVMM->aHandles[0].iSelf = 0;
|
---|
210 | pGVMM->aHandles[0].iNext = 0;
|
---|
211 |
|
---|
212 | /* the tail */
|
---|
213 | unsigned i = RT_ELEMENTS(pGVMM->aHandles) - 1;
|
---|
214 | pGVMM->aHandles[i].iSelf = i;
|
---|
215 | pGVMM->aHandles[i].iNext = 0; /* nil */
|
---|
216 |
|
---|
217 | /* the rest */
|
---|
218 | while (i-- > 1)
|
---|
219 | {
|
---|
220 | pGVMM->aHandles[i].iSelf = i;
|
---|
221 | pGVMM->aHandles[i].iNext = i + 1;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /* The default configuration values. */
|
---|
225 | pGVMM->cVMsMeansCompany = 1; /** @todo should be adjusted to relative to the cpu count or something... */
|
---|
226 | pGVMM->nsMinSleepAlone = 750000 /* ns (0.750 ms) */; /** @todo this should be adjusted to be 75% (or something) of the scheduler granularity... */
|
---|
227 | pGVMM->nsMinSleepCompany = 15000 /* ns (0.015 ms) */;
|
---|
228 | pGVMM->nsEarlyWakeUp1 = 25000 /* ns (0.025 ms) */;
|
---|
229 | pGVMM->nsEarlyWakeUp2 = 50000 /* ns (0.050 ms) */;
|
---|
230 |
|
---|
231 | g_pGVMM = pGVMM;
|
---|
232 | LogFlow(("GVMMR0Init: pGVMM=%p\n", pGVMM));
|
---|
233 | return VINF_SUCCESS;
|
---|
234 | }
|
---|
235 |
|
---|
236 | RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
|
---|
237 | }
|
---|
238 |
|
---|
239 | RTMemFree(pGVMM);
|
---|
240 | return rc;
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Terminates the GVM.
|
---|
246 | *
|
---|
247 | * This is called while owning the loader semaphore (see supdrvLdrFree()).
|
---|
248 | * And unless something is wrong, there should be absolutely no VMs
|
---|
249 | * registered at this point.
|
---|
250 | */
|
---|
251 | GVMMR0DECL(void) GVMMR0Term(void)
|
---|
252 | {
|
---|
253 | LogFlow(("GVMMR0Term:\n"));
|
---|
254 |
|
---|
255 | PGVMM pGVMM = g_pGVMM;
|
---|
256 | g_pGVMM = NULL;
|
---|
257 | if (RT_UNLIKELY(!VALID_PTR(pGVMM)))
|
---|
258 | {
|
---|
259 | SUPR0Printf("GVMMR0Term: pGVMM=%p\n", pGVMM);
|
---|
260 | return;
|
---|
261 | }
|
---|
262 |
|
---|
263 | pGVMM->u32Magic++;
|
---|
264 |
|
---|
265 | RTSemFastMutexDestroy(pGVMM->UsedLock);
|
---|
266 | pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
|
---|
267 | RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
|
---|
268 | pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
|
---|
269 |
|
---|
270 | pGVMM->iFreeHead = 0;
|
---|
271 | if (pGVMM->iUsedHead)
|
---|
272 | {
|
---|
273 | SUPR0Printf("GVMMR0Term: iUsedHead=%#x! (cVMs=%#x)\n", pGVMM->iUsedHead, pGVMM->cVMs);
|
---|
274 | pGVMM->iUsedHead = 0;
|
---|
275 | }
|
---|
276 |
|
---|
277 | RTMemFree(pGVMM);
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * A quick hack for setting global config values.
|
---|
283 | *
|
---|
284 | * @returns VBox status code.
|
---|
285 | *
|
---|
286 | * @param pSession The session handle. Used for authentication.
|
---|
287 | * @param pszName The variable name.
|
---|
288 | * @param u64Value The new value.
|
---|
289 | */
|
---|
290 | GVMMR0DECL(int) GVMMR0SetConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t u64Value)
|
---|
291 | {
|
---|
292 | /*
|
---|
293 | * Validate input.
|
---|
294 | */
|
---|
295 | PGVMM pGVMM;
|
---|
296 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
297 | AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
|
---|
298 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * String switch time!
|
---|
302 | */
|
---|
303 | if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
|
---|
304 | return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
|
---|
305 | int rc = VINF_SUCCESS;
|
---|
306 | pszName += sizeof("/GVMM/") - 1;
|
---|
307 | if (!strcmp(pszName, "cVMsMeansCompany"))
|
---|
308 | {
|
---|
309 | if (u64Value <= UINT32_MAX)
|
---|
310 | pGVMM->cVMsMeansCompany = u64Value;
|
---|
311 | else
|
---|
312 | rc = VERR_OUT_OF_RANGE;
|
---|
313 | }
|
---|
314 | else if (!strcmp(pszName, "MinSleepAlone"))
|
---|
315 | {
|
---|
316 | if (u64Value <= 100000000)
|
---|
317 | pGVMM->nsMinSleepAlone = u64Value;
|
---|
318 | else
|
---|
319 | rc = VERR_OUT_OF_RANGE;
|
---|
320 | }
|
---|
321 | else if (!strcmp(pszName, "MinSleepCompany"))
|
---|
322 | {
|
---|
323 | if (u64Value <= 100000000)
|
---|
324 | pGVMM->nsMinSleepCompany = u64Value;
|
---|
325 | else
|
---|
326 | rc = VERR_OUT_OF_RANGE;
|
---|
327 | }
|
---|
328 | else if (!strcmp(pszName, "EarlyWakeUp1"))
|
---|
329 | {
|
---|
330 | if (u64Value <= 100000000)
|
---|
331 | pGVMM->nsEarlyWakeUp1 = u64Value;
|
---|
332 | else
|
---|
333 | rc = VERR_OUT_OF_RANGE;
|
---|
334 | }
|
---|
335 | else if (!strcmp(pszName, "EarlyWakeUp2"))
|
---|
336 | {
|
---|
337 | if (u64Value <= 100000000)
|
---|
338 | pGVMM->nsEarlyWakeUp2 = u64Value;
|
---|
339 | else
|
---|
340 | rc = VERR_OUT_OF_RANGE;
|
---|
341 | }
|
---|
342 | else
|
---|
343 | rc = VERR_CFGM_VALUE_NOT_FOUND;
|
---|
344 | return rc;
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * A quick hack for getting global config values.
|
---|
350 | *
|
---|
351 | * @returns VBox status code.
|
---|
352 | *
|
---|
353 | * @param pSession The session handle. Used for authentication.
|
---|
354 | * @param pszName The variable name.
|
---|
355 | * @param u64Value The new value.
|
---|
356 | */
|
---|
357 | GVMMR0DECL(int) GVMMR0QueryConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t *pu64Value)
|
---|
358 | {
|
---|
359 | /*
|
---|
360 | * Validate input.
|
---|
361 | */
|
---|
362 | PGVMM pGVMM;
|
---|
363 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
364 | AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
|
---|
365 | AssertPtrReturn(pszName, VERR_INVALID_POINTER);
|
---|
366 | AssertPtrReturn(pu64Value, VERR_INVALID_POINTER);
|
---|
367 |
|
---|
368 | /*
|
---|
369 | * String switch time!
|
---|
370 | */
|
---|
371 | if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
|
---|
372 | return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
|
---|
373 | int rc = VINF_SUCCESS;
|
---|
374 | pszName += sizeof("/GVMM/") - 1;
|
---|
375 | if (!strcmp(pszName, "cVMsMeansCompany"))
|
---|
376 | *pu64Value = pGVMM->cVMsMeansCompany;
|
---|
377 | else if (!strcmp(pszName, "MinSleepAlone"))
|
---|
378 | *pu64Value = pGVMM->nsMinSleepAlone;
|
---|
379 | else if (!strcmp(pszName, "MinSleepCompany"))
|
---|
380 | *pu64Value = pGVMM->nsMinSleepCompany;
|
---|
381 | else if (!strcmp(pszName, "EarlyWakeUp1"))
|
---|
382 | *pu64Value = pGVMM->nsEarlyWakeUp1;
|
---|
383 | else if (!strcmp(pszName, "EarlyWakeUp2"))
|
---|
384 | *pu64Value = pGVMM->nsEarlyWakeUp2;
|
---|
385 | else
|
---|
386 | rc = VERR_CFGM_VALUE_NOT_FOUND;
|
---|
387 | return rc;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | /**
|
---|
392 | * Request wrapper for the GVMMR0CreateVM API.
|
---|
393 | *
|
---|
394 | * @returns VBox status code.
|
---|
395 | * @param pReq The request buffer.
|
---|
396 | */
|
---|
397 | GVMMR0DECL(int) GVMMR0CreateVMReq(PGVMMCREATEVMREQ pReq)
|
---|
398 | {
|
---|
399 | /*
|
---|
400 | * Validate the request.
|
---|
401 | */
|
---|
402 | if (!VALID_PTR(pReq))
|
---|
403 | return VERR_INVALID_POINTER;
|
---|
404 | if (pReq->Hdr.cbReq != sizeof(*pReq))
|
---|
405 | return VERR_INVALID_PARAMETER;
|
---|
406 | if (!VALID_PTR(pReq->pSession))
|
---|
407 | return VERR_INVALID_POINTER;
|
---|
408 |
|
---|
409 | /*
|
---|
410 | * Execute it.
|
---|
411 | */
|
---|
412 | PVM pVM;
|
---|
413 | pReq->pVMR0 = NULL;
|
---|
414 | pReq->pVMR3 = NIL_RTR3PTR;
|
---|
415 | int rc = GVMMR0CreateVM(pReq->pSession, &pVM);
|
---|
416 | if (RT_SUCCESS(rc))
|
---|
417 | {
|
---|
418 | pReq->pVMR0 = pVM;
|
---|
419 | pReq->pVMR3 = pVM->pVMR3;
|
---|
420 | }
|
---|
421 | return rc;
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * Allocates the VM structure and registers it with GVM.
|
---|
427 | *
|
---|
428 | * @returns VBox status code.
|
---|
429 | * @param pSession The support driver session.
|
---|
430 | * @param ppVM Where to store the pointer to the VM structure.
|
---|
431 | *
|
---|
432 | * @thread Any thread.
|
---|
433 | */
|
---|
434 | GVMMR0DECL(int) GVMMR0CreateVM(PSUPDRVSESSION pSession, PVM *ppVM)
|
---|
435 | {
|
---|
436 | LogFlow(("GVMMR0CreateVM: pSession=%p\n", pSession));
|
---|
437 | PGVMM pGVMM;
|
---|
438 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
439 |
|
---|
440 | AssertPtrReturn(ppVM, VERR_INVALID_POINTER);
|
---|
441 | *ppVM = NULL;
|
---|
442 |
|
---|
443 | AssertReturn(RTThreadNativeSelf() != NIL_RTNATIVETHREAD, VERR_INTERNAL_ERROR);
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * The whole allocation process is protected by the lock.
|
---|
447 | */
|
---|
448 | int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
|
---|
449 | AssertRCReturn(rc, rc);
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Allocate a handle first so we don't waste resources unnecessarily.
|
---|
453 | */
|
---|
454 | uint16_t iHandle = pGVMM->iFreeHead;
|
---|
455 | if (iHandle)
|
---|
456 | {
|
---|
457 | PGVMHANDLE pHandle = &pGVMM->aHandles[iHandle];
|
---|
458 |
|
---|
459 | /* consistency checks, a bit paranoid as always. */
|
---|
460 | if ( !pHandle->pVM
|
---|
461 | && !pHandle->pGVM
|
---|
462 | && !pHandle->pvObj
|
---|
463 | && pHandle->iSelf == iHandle)
|
---|
464 | {
|
---|
465 | pHandle->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_VM, gvmmR0HandleObjDestructor, pGVMM, pHandle);
|
---|
466 | if (pHandle->pvObj)
|
---|
467 | {
|
---|
468 | /*
|
---|
469 | * Move the handle from the free to used list and perform permission checks.
|
---|
470 | */
|
---|
471 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
472 | AssertRC(rc);
|
---|
473 |
|
---|
474 | pGVMM->iFreeHead = pHandle->iNext;
|
---|
475 | pHandle->iNext = pGVMM->iUsedHead;
|
---|
476 | pGVMM->iUsedHead = iHandle;
|
---|
477 | pGVMM->cVMs++;
|
---|
478 |
|
---|
479 | pHandle->pVM = NULL;
|
---|
480 | pHandle->pGVM = NULL;
|
---|
481 | pHandle->pSession = pSession;
|
---|
482 | pHandle->hEMT = NIL_RTNATIVETHREAD;
|
---|
483 |
|
---|
484 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
485 |
|
---|
486 | rc = SUPR0ObjVerifyAccess(pHandle->pvObj, pSession, NULL);
|
---|
487 | if (RT_SUCCESS(rc))
|
---|
488 | {
|
---|
489 | /*
|
---|
490 | * Allocate the global VM structure (GVM) and initialize it.
|
---|
491 | */
|
---|
492 | PGVM pGVM = (PGVM)RTMemAllocZ(sizeof(*pGVM));
|
---|
493 | if (pGVM)
|
---|
494 | {
|
---|
495 | pGVM->u32Magic = GVM_MAGIC;
|
---|
496 | pGVM->hSelf = iHandle;
|
---|
497 | pGVM->hEMT = NIL_RTNATIVETHREAD;
|
---|
498 | pGVM->pVM = NULL;
|
---|
499 |
|
---|
500 | gvmmR0InitPerVMData(pGVM);
|
---|
501 | /* GMMR0InitPerVMData(pGVM); - later */
|
---|
502 |
|
---|
503 | /*
|
---|
504 | * Allocate the shared VM structure and associated page array.
|
---|
505 | */
|
---|
506 | const size_t cPages = RT_ALIGN(sizeof(VM), PAGE_SIZE) >> PAGE_SHIFT;
|
---|
507 | rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
|
---|
508 | if (RT_SUCCESS(rc))
|
---|
509 | {
|
---|
510 | PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
|
---|
511 | memset(pVM, 0, cPages << PAGE_SHIFT);
|
---|
512 | pVM->enmVMState = VMSTATE_CREATING;
|
---|
513 | pVM->pVMR0 = pVM;
|
---|
514 | pVM->pSession = pSession;
|
---|
515 | pVM->hSelf = iHandle;
|
---|
516 |
|
---|
517 | rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
|
---|
518 | if (RT_SUCCESS(rc))
|
---|
519 | {
|
---|
520 | PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
|
---|
521 | for (size_t iPage = 0; iPage < cPages; iPage++)
|
---|
522 | {
|
---|
523 | paPages[iPage].uReserved = 0;
|
---|
524 | paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
|
---|
525 | Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
|
---|
526 | }
|
---|
527 |
|
---|
528 | /*
|
---|
529 | * Map them into ring-3.
|
---|
530 | */
|
---|
531 | rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
|
---|
532 | RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
|
---|
533 | if (RT_SUCCESS(rc))
|
---|
534 | {
|
---|
535 | pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
|
---|
536 | AssertPtr((void *)pVM->pVMR3);
|
---|
537 |
|
---|
538 | rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1, 0,
|
---|
539 | RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
|
---|
540 | if (RT_SUCCESS(rc))
|
---|
541 | {
|
---|
542 | pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
|
---|
543 | AssertPtr((void *)pVM->paVMPagesR3);
|
---|
544 |
|
---|
545 | /* complete the handle - take the UsedLock sem just to be careful. */
|
---|
546 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
547 | AssertRC(rc);
|
---|
548 |
|
---|
549 | pHandle->pVM = pVM;
|
---|
550 | pHandle->pGVM = pGVM;
|
---|
551 | pGVM->pVM = pVM;
|
---|
552 |
|
---|
553 |
|
---|
554 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
555 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
556 |
|
---|
557 | *ppVM = pVM;
|
---|
558 | Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
|
---|
559 | return VINF_SUCCESS;
|
---|
560 | }
|
---|
561 |
|
---|
562 | RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
|
---|
563 | pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
|
---|
564 | }
|
---|
565 | RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
|
---|
566 | pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
|
---|
567 | }
|
---|
568 | RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
|
---|
569 | pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | }
|
---|
573 | /* else: The user wasn't permitted to create this VM. */
|
---|
574 |
|
---|
575 | /*
|
---|
576 | * The handle will be freed by gvmmR0HandleObjDestructor as we release the
|
---|
577 | * object reference here. A little extra mess because of non-recursive lock.
|
---|
578 | */
|
---|
579 | void *pvObj = pHandle->pvObj;
|
---|
580 | pHandle->pvObj = NULL;
|
---|
581 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
582 |
|
---|
583 | SUPR0ObjRelease(pvObj, pSession);
|
---|
584 |
|
---|
585 | SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
|
---|
586 | return rc;
|
---|
587 | }
|
---|
588 |
|
---|
589 | rc = VERR_NO_MEMORY;
|
---|
590 | }
|
---|
591 | else
|
---|
592 | rc = VERR_INTERNAL_ERROR;
|
---|
593 | }
|
---|
594 | else
|
---|
595 | rc = VERR_GVM_TOO_MANY_VMS;
|
---|
596 |
|
---|
597 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
598 | return rc;
|
---|
599 | }
|
---|
600 |
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Initializes the per VM data belonging to GVMM.
|
---|
604 | *
|
---|
605 | * @param pGVM Pointer to the global VM structure.
|
---|
606 | */
|
---|
607 | static void gvmmR0InitPerVMData(PGVM pGVM)
|
---|
608 | {
|
---|
609 | AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
|
---|
610 | Assert(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
|
---|
611 | pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
|
---|
612 | pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
|
---|
613 | pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
|
---|
614 | pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
|
---|
615 | pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Associates an EMT thread with a VM.
|
---|
621 | *
|
---|
622 | * This is called early during the ring-0 VM initialization so assertions later in
|
---|
623 | * the process can be handled gracefully.
|
---|
624 | *
|
---|
625 | * @returns VBox status code.
|
---|
626 | *
|
---|
627 | * @param pVM The VM instance data (aka handle), ring-0 mapping of course.
|
---|
628 | * @thread EMT.
|
---|
629 | */
|
---|
630 | GVMMR0DECL(int) GVMMR0AssociateEMTWithVM(PVM pVM)
|
---|
631 | {
|
---|
632 | LogFlow(("GVMMR0AssociateEMTWithVM: pVM=%p\n", pVM));
|
---|
633 | PGVMM pGVMM;
|
---|
634 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
635 |
|
---|
636 | /*
|
---|
637 | * Validate the VM structure, state and handle.
|
---|
638 | */
|
---|
639 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
640 | AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
|
---|
641 | AssertMsgReturn(pVM->enmVMState == VMSTATE_CREATING, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
|
---|
642 |
|
---|
643 | RTNATIVETHREAD hEMT = RTThreadNativeSelf();
|
---|
644 | AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_NOT_SUPPORTED);
|
---|
645 |
|
---|
646 | const uint16_t hGVM = pVM->hSelf;
|
---|
647 | AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
|
---|
648 | AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
|
---|
649 |
|
---|
650 | PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
|
---|
651 | AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
|
---|
652 |
|
---|
653 | /*
|
---|
654 | * Take the lock, validate the handle and update the structure members.
|
---|
655 | */
|
---|
656 | int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
|
---|
657 | AssertRCReturn(rc, rc);
|
---|
658 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
659 | AssertRC(rc);
|
---|
660 |
|
---|
661 | if ( pHandle->pVM == pVM
|
---|
662 | && VALID_PTR(pHandle->pvObj)
|
---|
663 | && VALID_PTR(pHandle->pSession)
|
---|
664 | && VALID_PTR(pHandle->pGVM)
|
---|
665 | && pHandle->pGVM->u32Magic == GVM_MAGIC)
|
---|
666 | {
|
---|
667 | pHandle->hEMT = hEMT;
|
---|
668 | pHandle->pGVM->hEMT = hEMT;
|
---|
669 | }
|
---|
670 | else
|
---|
671 | rc = VERR_INTERNAL_ERROR;
|
---|
672 |
|
---|
673 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
674 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
675 | LogFlow(("GVMMR0AssociateEMTWithVM: returns %Vrc (hEMT=%RTnthrd)\n", rc, hEMT));
|
---|
676 | return rc;
|
---|
677 | }
|
---|
678 |
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Does the VM initialization.
|
---|
682 | *
|
---|
683 | * @returns VBox status code.
|
---|
684 | * @param pVM Pointer to the shared VM structure.
|
---|
685 | */
|
---|
686 | GVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
|
---|
687 | {
|
---|
688 | LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * Validate the VM structure, state and handle.
|
---|
692 | */
|
---|
693 | PGVM pGVM;
|
---|
694 | PGVMM pGVMM;
|
---|
695 | int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
|
---|
696 | if (RT_SUCCESS(rc))
|
---|
697 | {
|
---|
698 | if (pGVM->gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
|
---|
699 | {
|
---|
700 | rc = RTSemEventMultiCreate(&pGVM->gvmm.s.HaltEventMulti);
|
---|
701 | if (RT_FAILURE(rc))
|
---|
702 | pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
|
---|
703 | }
|
---|
704 | else
|
---|
705 | rc = VERR_WRONG_ORDER;
|
---|
706 | }
|
---|
707 |
|
---|
708 | LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
|
---|
709 | return rc;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | /**
|
---|
714 | * Disassociates the EMT thread from a VM.
|
---|
715 | *
|
---|
716 | * This is called last in the ring-0 VM termination. After this point anyone is
|
---|
717 | * allowed to destroy the VM. Ideally, we should associate the VM with the thread
|
---|
718 | * that's going to call GVMMR0DestroyVM for optimal security, but that's impractical
|
---|
719 | * at present.
|
---|
720 | *
|
---|
721 | * @returns VBox status code.
|
---|
722 | *
|
---|
723 | * @param pVM The VM instance data (aka handle), ring-0 mapping of course.
|
---|
724 | * @thread EMT.
|
---|
725 | */
|
---|
726 | GVMMR0DECL(int) GVMMR0DisassociateEMTFromVM(PVM pVM)
|
---|
727 | {
|
---|
728 | LogFlow(("GVMMR0DisassociateEMTFromVM: pVM=%p\n", pVM));
|
---|
729 | PGVMM pGVMM;
|
---|
730 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
731 |
|
---|
732 | /*
|
---|
733 | * Validate the VM structure, state and handle.
|
---|
734 | */
|
---|
735 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
736 | AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
|
---|
737 | AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_DESTROYING, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
|
---|
738 |
|
---|
739 | RTNATIVETHREAD hEMT = RTThreadNativeSelf();
|
---|
740 | AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_NOT_SUPPORTED);
|
---|
741 |
|
---|
742 | const uint16_t hGVM = pVM->hSelf;
|
---|
743 | AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
|
---|
744 | AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
|
---|
745 |
|
---|
746 | PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
|
---|
747 | AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * Take the lock, validate the handle and update the structure members.
|
---|
751 | */
|
---|
752 | int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
|
---|
753 | AssertRCReturn(rc, rc);
|
---|
754 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
755 | AssertRC(rc);
|
---|
756 |
|
---|
757 | if ( VALID_PTR(pHandle->pvObj)
|
---|
758 | && VALID_PTR(pHandle->pSession)
|
---|
759 | && VALID_PTR(pHandle->pGVM)
|
---|
760 | && pHandle->pGVM->u32Magic == GVM_MAGIC)
|
---|
761 | {
|
---|
762 | if ( pHandle->pVM == pVM
|
---|
763 | && pHandle->hEMT == hEMT)
|
---|
764 | {
|
---|
765 | pHandle->hEMT = NIL_RTNATIVETHREAD;
|
---|
766 | pHandle->pGVM->hEMT = NIL_RTNATIVETHREAD;
|
---|
767 | }
|
---|
768 | else
|
---|
769 | rc = VERR_NOT_OWNER;
|
---|
770 | }
|
---|
771 | else
|
---|
772 | rc = VERR_INVALID_HANDLE;
|
---|
773 |
|
---|
774 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
775 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
776 | LogFlow(("GVMMR0DisassociateEMTFromVM: returns %Vrc (hEMT=%RTnthrd)\n", rc, hEMT));
|
---|
777 | return rc;
|
---|
778 | }
|
---|
779 |
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
|
---|
783 | *
|
---|
784 | * This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
|
---|
785 | * and the caller is not the EMT thread, unfortunately. For security reasons, it
|
---|
786 | * would've been nice if the caller was actually the EMT thread or that we somehow
|
---|
787 | * could've associated the calling thread with the VM up front.
|
---|
788 | *
|
---|
789 | * @returns VBox status code.
|
---|
790 | * @param pVM Where to store the pointer to the VM structure.
|
---|
791 | *
|
---|
792 | * @thread EMT if it's associated with the VM, otherwise any thread.
|
---|
793 | */
|
---|
794 | GVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
|
---|
795 | {
|
---|
796 | LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
|
---|
797 | PGVMM pGVMM;
|
---|
798 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
799 |
|
---|
800 |
|
---|
801 | /*
|
---|
802 | * Validate the VM structure, state and caller.
|
---|
803 | */
|
---|
804 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
805 | AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
|
---|
806 | AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
|
---|
807 |
|
---|
808 | uint32_t hGVM = pVM->hSelf;
|
---|
809 | AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
|
---|
810 | AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
|
---|
811 |
|
---|
812 | PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
|
---|
813 | AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
|
---|
814 |
|
---|
815 | RTNATIVETHREAD hSelf = RTThreadNativeSelf();
|
---|
816 | AssertReturn(pHandle->hEMT == hSelf || pHandle->hEMT == NIL_RTNATIVETHREAD, VERR_NOT_OWNER);
|
---|
817 |
|
---|
818 | /*
|
---|
819 | * Lookup the handle and destroy the object.
|
---|
820 | * Since the lock isn't recursive and we'll have to leave it before dereferencing the
|
---|
821 | * object, we take some precautions against racing callers just in case...
|
---|
822 | */
|
---|
823 | int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
|
---|
824 | AssertRC(rc);
|
---|
825 |
|
---|
826 | /* be careful here because we might theoretically be racing someone else cleaning up. */
|
---|
827 | if ( pHandle->pVM == pVM
|
---|
828 | && ( pHandle->hEMT == hSelf
|
---|
829 | || pHandle->hEMT == NIL_RTNATIVETHREAD)
|
---|
830 | && VALID_PTR(pHandle->pvObj)
|
---|
831 | && VALID_PTR(pHandle->pSession)
|
---|
832 | && VALID_PTR(pHandle->pGVM)
|
---|
833 | && pHandle->pGVM->u32Magic == GVM_MAGIC)
|
---|
834 | {
|
---|
835 | void *pvObj = pHandle->pvObj;
|
---|
836 | pHandle->pvObj = NULL;
|
---|
837 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
838 |
|
---|
839 | SUPR0ObjRelease(pvObj, pHandle->pSession);
|
---|
840 | }
|
---|
841 | else
|
---|
842 | {
|
---|
843 | SUPR0Printf("GVMMR0DestroyVM: pHandle=%p:{.pVM=%p, hEMT=%p, .pvObj=%p} pVM=%p hSelf=%p\n",
|
---|
844 | pHandle, pHandle->pVM, pHandle->hEMT, pHandle->pvObj, pVM, hSelf);
|
---|
845 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
846 | rc = VERR_INTERNAL_ERROR;
|
---|
847 | }
|
---|
848 |
|
---|
849 | return rc;
|
---|
850 | }
|
---|
851 |
|
---|
852 |
|
---|
853 | /**
|
---|
854 | * Handle destructor.
|
---|
855 | *
|
---|
856 | * @param pvGVMM The GVM instance pointer.
|
---|
857 | * @param pvHandle The handle pointer.
|
---|
858 | */
|
---|
859 | static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle)
|
---|
860 | {
|
---|
861 | LogFlow(("gvmmR0HandleObjDestructor: %p %p %p\n", pvObj, pvGVMM, pvHandle));
|
---|
862 |
|
---|
863 | /*
|
---|
864 | * Some quick, paranoid, input validation.
|
---|
865 | */
|
---|
866 | PGVMHANDLE pHandle = (PGVMHANDLE)pvHandle;
|
---|
867 | AssertPtr(pHandle);
|
---|
868 | PGVMM pGVMM = (PGVMM)pvGVMM;
|
---|
869 | Assert(pGVMM == g_pGVMM);
|
---|
870 | const uint16_t iHandle = pHandle - &pGVMM->aHandles[0];
|
---|
871 | if ( !iHandle
|
---|
872 | || iHandle >= RT_ELEMENTS(pGVMM->aHandles)
|
---|
873 | || iHandle != pHandle->iSelf)
|
---|
874 | {
|
---|
875 | SUPR0Printf("GVM: handle %d is out of range or corrupt (iSelf=%d)!\n", iHandle, pHandle->iSelf);
|
---|
876 | return;
|
---|
877 | }
|
---|
878 |
|
---|
879 | int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
|
---|
880 | AssertRC(rc);
|
---|
881 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
882 | AssertRC(rc);
|
---|
883 |
|
---|
884 | /*
|
---|
885 | * This is a tad slow but a doubly linked list is too much hazzle.
|
---|
886 | */
|
---|
887 | if (RT_UNLIKELY(pHandle->iNext >= RT_ELEMENTS(pGVMM->aHandles)))
|
---|
888 | {
|
---|
889 | SUPR0Printf("GVM: used list index %d is out of range!\n", pHandle->iNext);
|
---|
890 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
891 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
892 | return;
|
---|
893 | }
|
---|
894 |
|
---|
895 | if (pGVMM->iUsedHead == iHandle)
|
---|
896 | pGVMM->iUsedHead = pHandle->iNext;
|
---|
897 | else
|
---|
898 | {
|
---|
899 | uint16_t iPrev = pGVMM->iUsedHead;
|
---|
900 | int c = RT_ELEMENTS(pGVMM->aHandles) + 2;
|
---|
901 | while (!iPrev)
|
---|
902 | {
|
---|
903 | if (RT_UNLIKELY(iPrev >= RT_ELEMENTS(pGVMM->aHandles)))
|
---|
904 | {
|
---|
905 | SUPR0Printf("GVM: used list index %d is out of range!\n");
|
---|
906 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
907 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
908 | return;
|
---|
909 | }
|
---|
910 | if (RT_UNLIKELY(c-- <= 0))
|
---|
911 | {
|
---|
912 | iPrev = 0;
|
---|
913 | break;
|
---|
914 | }
|
---|
915 |
|
---|
916 | if (pGVMM->aHandles[iPrev].iNext == iHandle)
|
---|
917 | break;
|
---|
918 | iPrev = pGVMM->aHandles[iPrev].iNext;
|
---|
919 | }
|
---|
920 | if (!iPrev)
|
---|
921 | {
|
---|
922 | SUPR0Printf("GVM: can't find the handle previous previous of %d!\n", pHandle->iSelf);
|
---|
923 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
924 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
925 | return;
|
---|
926 | }
|
---|
927 |
|
---|
928 | pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
|
---|
929 | }
|
---|
930 | pHandle->iNext = 0;
|
---|
931 | pGVMM->cVMs--;
|
---|
932 |
|
---|
933 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
934 |
|
---|
935 | /*
|
---|
936 | * Do the global cleanup round.
|
---|
937 | */
|
---|
938 | PGVM pGVM = pHandle->pGVM;
|
---|
939 | if ( VALID_PTR(pGVM)
|
---|
940 | && pGVM->u32Magic == GVM_MAGIC)
|
---|
941 | {
|
---|
942 | /// @todo GMMR0CleanupVM(pGVM);
|
---|
943 |
|
---|
944 | /*
|
---|
945 | * Do the GVMM cleanup - must be done last.
|
---|
946 | */
|
---|
947 | /* The VM and VM pages mappings/allocations. */
|
---|
948 | if (pGVM->gvmm.s.VMPagesMapObj != NIL_RTR0MEMOBJ)
|
---|
949 | {
|
---|
950 | rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMapObj, false /* fFreeMappings */); AssertRC(rc);
|
---|
951 | pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
|
---|
952 | }
|
---|
953 |
|
---|
954 | if (pGVM->gvmm.s.VMMapObj != NIL_RTR0MEMOBJ)
|
---|
955 | {
|
---|
956 | rc = RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */); AssertRC(rc);
|
---|
957 | pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
|
---|
958 | }
|
---|
959 |
|
---|
960 | if (pGVM->gvmm.s.VMPagesMemObj != NIL_RTR0MEMOBJ)
|
---|
961 | {
|
---|
962 | rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */); AssertRC(rc);
|
---|
963 | pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
|
---|
964 | }
|
---|
965 |
|
---|
966 | if (pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ)
|
---|
967 | {
|
---|
968 | rc = RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */); AssertRC(rc);
|
---|
969 | pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
|
---|
970 | }
|
---|
971 |
|
---|
972 | /* the GVM structure itself. */
|
---|
973 | pGVM->u32Magic++;
|
---|
974 | RTMemFree(pGVM);
|
---|
975 | }
|
---|
976 | /* else: GVMMR0CreateVM cleanup. */
|
---|
977 |
|
---|
978 | /*
|
---|
979 | * Free the handle.
|
---|
980 | * Reacquire the UsedLock here to since we're updating handle fields.
|
---|
981 | */
|
---|
982 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
983 | AssertRC(rc);
|
---|
984 |
|
---|
985 | pHandle->iNext = pGVMM->iFreeHead;
|
---|
986 | pGVMM->iFreeHead = iHandle;
|
---|
987 | ASMAtomicXchgPtr((void * volatile *)&pHandle->pGVM, NULL);
|
---|
988 | ASMAtomicXchgPtr((void * volatile *)&pHandle->pVM, NULL);
|
---|
989 | ASMAtomicXchgPtr((void * volatile *)&pHandle->pvObj, NULL);
|
---|
990 | ASMAtomicXchgPtr((void * volatile *)&pHandle->pSession, NULL);
|
---|
991 | ASMAtomicXchgSize(&pHandle->hEMT, NIL_RTNATIVETHREAD);
|
---|
992 |
|
---|
993 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
994 | RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
|
---|
995 | LogFlow(("gvmmR0HandleObjDestructor: returns\n"));
|
---|
996 | }
|
---|
997 |
|
---|
998 |
|
---|
999 | /**
|
---|
1000 | * Lookup a GVM structure by its handle.
|
---|
1001 | *
|
---|
1002 | * @returns The GVM pointer on success, NULL on failure.
|
---|
1003 | * @param hGVM The global VM handle. Asserts on bad handle.
|
---|
1004 | */
|
---|
1005 | GVMMR0DECL(PGVM) GVMMR0ByHandle(uint32_t hGVM)
|
---|
1006 | {
|
---|
1007 | PGVMM pGVMM;
|
---|
1008 | GVMM_GET_VALID_INSTANCE(pGVMM, NULL);
|
---|
1009 |
|
---|
1010 | /*
|
---|
1011 | * Validate.
|
---|
1012 | */
|
---|
1013 | AssertReturn(hGVM != NIL_GVM_HANDLE, NULL);
|
---|
1014 | AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), NULL);
|
---|
1015 |
|
---|
1016 | /*
|
---|
1017 | * Look it up.
|
---|
1018 | */
|
---|
1019 | PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
|
---|
1020 | AssertPtrReturn(pHandle->pVM, NULL);
|
---|
1021 | AssertPtrReturn(pHandle->pvObj, NULL);
|
---|
1022 | PGVM pGVM = pHandle->pGVM;
|
---|
1023 | AssertPtrReturn(pGVM, NULL);
|
---|
1024 | AssertReturn(pGVM->pVM == pHandle->pVM, NULL);
|
---|
1025 |
|
---|
1026 | return pHandle->pGVM;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 |
|
---|
1030 | /**
|
---|
1031 | * Lookup a GVM structure by the shared VM structure.
|
---|
1032 | *
|
---|
1033 | * @returns VBox status code.
|
---|
1034 | * @param pVM The shared VM structure (the ring-0 mapping).
|
---|
1035 | * @param ppGVM Where to store the GVM pointer.
|
---|
1036 | * @param ppGVMM Where to store the pointer to the GVMM instance data.
|
---|
1037 | * @param fTakeUsedLock Whether to take the used lock or not.
|
---|
1038 | * Be very careful if not taking the lock as it's possible that
|
---|
1039 | * the VM will disappear then.
|
---|
1040 | *
|
---|
1041 | * @remark This will not assert on an invalid pVM but try return sliently.
|
---|
1042 | */
|
---|
1043 | static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock)
|
---|
1044 | {
|
---|
1045 | PGVMM pGVMM;
|
---|
1046 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
1047 |
|
---|
1048 | /*
|
---|
1049 | * Validate.
|
---|
1050 | */
|
---|
1051 | if (RT_UNLIKELY( !VALID_PTR(pVM)
|
---|
1052 | || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
|
---|
1053 | return VERR_INVALID_POINTER;
|
---|
1054 | if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
|
---|
1055 | || pVM->enmVMState >= VMSTATE_TERMINATED))
|
---|
1056 | return VERR_INVALID_POINTER;
|
---|
1057 |
|
---|
1058 | uint16_t hGVM = pVM->hSelf;
|
---|
1059 | if (RT_UNLIKELY( hGVM == NIL_GVM_HANDLE
|
---|
1060 | || hGVM >= RT_ELEMENTS(pGVMM->aHandles)))
|
---|
1061 | return VERR_INVALID_HANDLE;
|
---|
1062 |
|
---|
1063 | /*
|
---|
1064 | * Look it up.
|
---|
1065 | */
|
---|
1066 | PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
|
---|
1067 | PGVM pGVM;
|
---|
1068 | if (fTakeUsedLock)
|
---|
1069 | {
|
---|
1070 | int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
1071 | AssertRCReturn(rc, rc);
|
---|
1072 |
|
---|
1073 | pGVM = pHandle->pGVM;
|
---|
1074 | if (RT_UNLIKELY( pHandle->pVM != pVM
|
---|
1075 | || !VALID_PTR(pHandle->pvObj)
|
---|
1076 | || !VALID_PTR(pGVM)
|
---|
1077 | || pGVM->pVM != pVM))
|
---|
1078 | {
|
---|
1079 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1080 | return VERR_INVALID_HANDLE;
|
---|
1081 | }
|
---|
1082 | }
|
---|
1083 | else
|
---|
1084 | {
|
---|
1085 | if (RT_UNLIKELY(pHandle->pVM != pVM))
|
---|
1086 | return VERR_INVALID_HANDLE;
|
---|
1087 | if (RT_UNLIKELY(!VALID_PTR(pHandle->pvObj)))
|
---|
1088 | return VERR_INVALID_HANDLE;
|
---|
1089 |
|
---|
1090 | pGVM = pHandle->pGVM;
|
---|
1091 | if (!RT_UNLIKELY(!VALID_PTR(pGVM)))
|
---|
1092 | return VERR_INVALID_HANDLE;
|
---|
1093 | if (!RT_UNLIKELY(pGVM->pVM != pVM))
|
---|
1094 | return VERR_INVALID_HANDLE;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | *ppGVM = pGVM;
|
---|
1098 | *ppGVMM = pGVMM;
|
---|
1099 | return VINF_SUCCESS;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 |
|
---|
1103 | /**
|
---|
1104 | * Lookup a GVM structure by the shared VM structure.
|
---|
1105 | *
|
---|
1106 | * @returns The GVM pointer on success, NULL on failure.
|
---|
1107 | * @param pVM The shared VM structure (the ring-0 mapping).
|
---|
1108 | */
|
---|
1109 | GVMMR0DECL(PGVM) GVMMR0ByVM(PVM pVM)
|
---|
1110 | {
|
---|
1111 | PGVMM pGVMM;
|
---|
1112 | PGVM pGVM;
|
---|
1113 | int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
|
---|
1114 | if (RT_SUCCESS(rc))
|
---|
1115 | return pGVM;
|
---|
1116 | AssertRC(rc);
|
---|
1117 | return NULL;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 |
|
---|
1121 | /**
|
---|
1122 | * Lookup a GVM structure by the shared VM structure
|
---|
1123 | * and ensuring that the caller is the EMT thread.
|
---|
1124 | *
|
---|
1125 | * @returns VBox status code.
|
---|
1126 | * @param pVM The shared VM structure (the ring-0 mapping).
|
---|
1127 | * @param ppGVM Where to store the GVM pointer.
|
---|
1128 | * @param ppGVMM Where to store the pointer to the GVMM instance data.
|
---|
1129 | * @thread EMT
|
---|
1130 | *
|
---|
1131 | * @remark This will assert in failure paths.
|
---|
1132 | */
|
---|
1133 | static int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM)
|
---|
1134 | {
|
---|
1135 | PGVMM pGVMM;
|
---|
1136 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
1137 |
|
---|
1138 | /*
|
---|
1139 | * Validate.
|
---|
1140 | */
|
---|
1141 | AssertPtrReturn(pVM, VERR_INVALID_POINTER);
|
---|
1142 | AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
|
---|
1143 |
|
---|
1144 | uint16_t hGVM = pVM->hSelf;
|
---|
1145 | AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
|
---|
1146 | AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
|
---|
1147 |
|
---|
1148 | /*
|
---|
1149 | * Look it up.
|
---|
1150 | */
|
---|
1151 | PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
|
---|
1152 | RTNATIVETHREAD hAllegedEMT = RTThreadNativeSelf();
|
---|
1153 | AssertMsgReturn(pHandle->hEMT == hAllegedEMT, ("hEMT %x hAllegedEMT %x\n", pHandle->hEMT, hAllegedEMT), VERR_NOT_OWNER);
|
---|
1154 | AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
|
---|
1155 | AssertPtrReturn(pHandle->pvObj, VERR_INTERNAL_ERROR);
|
---|
1156 |
|
---|
1157 | PGVM pGVM = pHandle->pGVM;
|
---|
1158 | AssertPtrReturn(pGVM, VERR_INTERNAL_ERROR);
|
---|
1159 | AssertReturn(pGVM->pVM == pVM, VERR_INTERNAL_ERROR);
|
---|
1160 | AssertReturn(pGVM->hEMT == hAllegedEMT, VERR_INTERNAL_ERROR);
|
---|
1161 |
|
---|
1162 | *ppGVM = pGVM;
|
---|
1163 | *ppGVMM = pGVMM;
|
---|
1164 | return VINF_SUCCESS;
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 |
|
---|
1168 | /**
|
---|
1169 | * Lookup a GVM structure by the shared VM structure
|
---|
1170 | * and ensuring that the caller is the EMT thread.
|
---|
1171 | *
|
---|
1172 | * @returns VBox status code.
|
---|
1173 | * @param pVM The shared VM structure (the ring-0 mapping).
|
---|
1174 | * @param ppGVM Where to store the GVM pointer.
|
---|
1175 | * @thread EMT
|
---|
1176 | */
|
---|
1177 | GVMMR0DECL(int) GVMMR0ByVMAndEMT(PVM pVM, PGVM *ppGVM)
|
---|
1178 | {
|
---|
1179 | AssertPtrReturn(ppGVM, VERR_INVALID_POINTER);
|
---|
1180 | PGVMM pGVMM;
|
---|
1181 | return gvmmR0ByVMAndEMT(pVM, ppGVM, &pGVMM);
|
---|
1182 | }
|
---|
1183 |
|
---|
1184 |
|
---|
1185 | /**
|
---|
1186 | * Lookup a VM by its global handle.
|
---|
1187 | *
|
---|
1188 | * @returns The VM handle on success, NULL on failure.
|
---|
1189 | * @param hGVM The global VM handle. Asserts on bad handle.
|
---|
1190 | */
|
---|
1191 | GVMMR0DECL(PVM) GVMMR0GetVMByHandle(uint32_t hGVM)
|
---|
1192 | {
|
---|
1193 | PGVM pGVM = GVMMR0ByHandle(hGVM);
|
---|
1194 | return pGVM ? pGVM->pVM : NULL;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 |
|
---|
1198 | /**
|
---|
1199 | * Looks up the VM belonging to the specified EMT thread.
|
---|
1200 | *
|
---|
1201 | * This is used by the assertion machinery in VMMR0.cpp to avoid causing
|
---|
1202 | * unnecessary kernel panics when the EMT thread hits an assertion. The
|
---|
1203 | * call may or not be an EMT thread.
|
---|
1204 | *
|
---|
1205 | * @returns The VM handle on success, NULL on failure.
|
---|
1206 | * @param hEMT The native thread handle of the EMT.
|
---|
1207 | * NIL_RTNATIVETHREAD means the current thread
|
---|
1208 | */
|
---|
1209 | GVMMR0DECL(PVM) GVMMR0GetVMByEMT(RTNATIVETHREAD hEMT)
|
---|
1210 | {
|
---|
1211 | /*
|
---|
1212 | * No Assertions here as we're usually called in a AssertMsgN or
|
---|
1213 | * RTAssert* context.
|
---|
1214 | */
|
---|
1215 | PGVMM pGVMM = g_pGVMM;
|
---|
1216 | if ( !VALID_PTR(pGVMM)
|
---|
1217 | || pGVMM->u32Magic != GVMM_MAGIC)
|
---|
1218 | return NULL;
|
---|
1219 |
|
---|
1220 | if (hEMT == NIL_RTNATIVETHREAD)
|
---|
1221 | hEMT = RTThreadNativeSelf();
|
---|
1222 |
|
---|
1223 | /*
|
---|
1224 | * Search the handles in a linear fashion as we don't dare take the lock (assert).
|
---|
1225 | */
|
---|
1226 | for (unsigned i = 1; i < RT_ELEMENTS(pGVMM->aHandles); i++)
|
---|
1227 | if ( pGVMM->aHandles[i].hEMT == hEMT
|
---|
1228 | && pGVMM->aHandles[i].iSelf == i
|
---|
1229 | && VALID_PTR(pGVMM->aHandles[i].pvObj)
|
---|
1230 | && VALID_PTR(pGVMM->aHandles[i].pVM))
|
---|
1231 | return pGVMM->aHandles[i].pVM;
|
---|
1232 |
|
---|
1233 | return NULL;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 |
|
---|
1237 | /**
|
---|
1238 | * This is will wake up expired and soon-to-be expired VMs.
|
---|
1239 | *
|
---|
1240 | * @returns Number of VMs that has been woken up.
|
---|
1241 | * @param pGVMM Pointer to the GVMM instance data.
|
---|
1242 | * @param u64Now The current time.
|
---|
1243 | */
|
---|
1244 | static unsigned gvmmR0SchedDoWakeUps(PGVMM pGVMM, uint64_t u64Now)
|
---|
1245 | {
|
---|
1246 | /*
|
---|
1247 | * The first pass will wake up VMs which has actually expired
|
---|
1248 | * and look for VMs that should be woken up in the 2nd and 3rd passes.
|
---|
1249 | */
|
---|
1250 | unsigned cWoken = 0;
|
---|
1251 | unsigned cHalted = 0;
|
---|
1252 | unsigned cTodo2nd = 0;
|
---|
1253 | unsigned cTodo3rd = 0;
|
---|
1254 | for (unsigned i = pGVMM->iUsedHead;
|
---|
1255 | i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
|
---|
1256 | i = pGVMM->aHandles[i].iNext)
|
---|
1257 | {
|
---|
1258 | PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
|
---|
1259 | if ( VALID_PTR(pCurGVM)
|
---|
1260 | && pCurGVM->u32Magic == GVM_MAGIC)
|
---|
1261 | {
|
---|
1262 | uint64_t u64 = pCurGVM->gvmm.s.u64HaltExpire;
|
---|
1263 | if (u64)
|
---|
1264 | {
|
---|
1265 | if (u64 <= u64Now)
|
---|
1266 | {
|
---|
1267 | if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
|
---|
1268 | {
|
---|
1269 | int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
|
---|
1270 | AssertRC(rc);
|
---|
1271 | cWoken++;
|
---|
1272 | }
|
---|
1273 | }
|
---|
1274 | else
|
---|
1275 | {
|
---|
1276 | cHalted++;
|
---|
1277 | if (u64 <= u64Now + pGVMM->nsEarlyWakeUp1)
|
---|
1278 | cTodo2nd++;
|
---|
1279 | else if (u64 <= u64Now + pGVMM->nsEarlyWakeUp2)
|
---|
1280 | cTodo3rd++;
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 | }
|
---|
1284 | }
|
---|
1285 |
|
---|
1286 | if (cTodo2nd)
|
---|
1287 | {
|
---|
1288 | for (unsigned i = pGVMM->iUsedHead;
|
---|
1289 | i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
|
---|
1290 | i = pGVMM->aHandles[i].iNext)
|
---|
1291 | {
|
---|
1292 | PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
|
---|
1293 | if ( VALID_PTR(pCurGVM)
|
---|
1294 | && pCurGVM->u32Magic == GVM_MAGIC
|
---|
1295 | && pCurGVM->gvmm.s.u64HaltExpire
|
---|
1296 | && pCurGVM->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp1)
|
---|
1297 | {
|
---|
1298 | if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
|
---|
1299 | {
|
---|
1300 | int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
|
---|
1301 | AssertRC(rc);
|
---|
1302 | cWoken++;
|
---|
1303 | }
|
---|
1304 | }
|
---|
1305 | }
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | if (cTodo3rd)
|
---|
1309 | {
|
---|
1310 | for (unsigned i = pGVMM->iUsedHead;
|
---|
1311 | i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
|
---|
1312 | i = pGVMM->aHandles[i].iNext)
|
---|
1313 | {
|
---|
1314 | PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
|
---|
1315 | if ( VALID_PTR(pCurGVM)
|
---|
1316 | && pCurGVM->u32Magic == GVM_MAGIC
|
---|
1317 | && pCurGVM->gvmm.s.u64HaltExpire
|
---|
1318 | && pCurGVM->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp2)
|
---|
1319 | {
|
---|
1320 | if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
|
---|
1321 | {
|
---|
1322 | int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
|
---|
1323 | AssertRC(rc);
|
---|
1324 | cWoken++;
|
---|
1325 | }
|
---|
1326 | }
|
---|
1327 | }
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | return cWoken;
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 |
|
---|
1334 | /**
|
---|
1335 | * Halt the EMT thread.
|
---|
1336 | *
|
---|
1337 | * @returns VINF_SUCCESS normal wakeup (timeout or kicked by other thread).
|
---|
1338 | * VERR_INTERRUPTED if a signal was scheduled for the thread.
|
---|
1339 | * @param pVM Pointer to the shared VM structure.
|
---|
1340 | * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
|
---|
1341 | * @thread EMT.
|
---|
1342 | */
|
---|
1343 | GVMMR0DECL(int) GVMMR0SchedHalt(PVM pVM, uint64_t u64ExpireGipTime)
|
---|
1344 | {
|
---|
1345 | LogFlow(("GVMMR0DisassociateEMTFromVM: pVM=%p\n", pVM));
|
---|
1346 |
|
---|
1347 | /*
|
---|
1348 | * Validate the VM structure, state and handle.
|
---|
1349 | */
|
---|
1350 | PGVMM pGVMM;
|
---|
1351 | PGVM pGVM;
|
---|
1352 | int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
|
---|
1353 | if (RT_FAILURE(rc))
|
---|
1354 | return rc;
|
---|
1355 | pGVM->gvmm.s.StatsSched.cHaltCalls++;
|
---|
1356 |
|
---|
1357 | Assert(!pGVM->gvmm.s.u64HaltExpire);
|
---|
1358 |
|
---|
1359 | /*
|
---|
1360 | * Take the UsedList semaphore, get the current time
|
---|
1361 | * and check if anyone needs waking up.
|
---|
1362 | * Interrupts must NOT be disabled at this point because we ask for GIP time!
|
---|
1363 | */
|
---|
1364 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
1365 | AssertRC(rc);
|
---|
1366 |
|
---|
1367 | pGVM->gvmm.s.iCpuEmt = ASMGetApicId();
|
---|
1368 |
|
---|
1369 | Assert(ASMGetFlags() & X86_EFL_IF);
|
---|
1370 | const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
|
---|
1371 | pGVM->gvmm.s.StatsSched.cHaltWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
|
---|
1372 |
|
---|
1373 | /*
|
---|
1374 | * Go to sleep if we must...
|
---|
1375 | */
|
---|
1376 | if ( u64Now < u64ExpireGipTime
|
---|
1377 | && u64ExpireGipTime - u64Now > (pGVMM->cVMs > pGVMM->cVMsMeansCompany
|
---|
1378 | ? pGVMM->nsMinSleepCompany
|
---|
1379 | : pGVMM->nsMinSleepAlone))
|
---|
1380 | {
|
---|
1381 | pGVM->gvmm.s.StatsSched.cHaltBlocking++;
|
---|
1382 | ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, u64ExpireGipTime);
|
---|
1383 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1384 |
|
---|
1385 | uint32_t cMillies = (u64ExpireGipTime - u64Now) / 1000000;
|
---|
1386 | rc = RTSemEventMultiWaitNoResume(pGVM->gvmm.s.HaltEventMulti, cMillies ? cMillies : 1);
|
---|
1387 | ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, 0);
|
---|
1388 | if (rc == VERR_TIMEOUT)
|
---|
1389 | {
|
---|
1390 | pGVM->gvmm.s.StatsSched.cHaltTimeouts++;
|
---|
1391 | rc = VINF_SUCCESS;
|
---|
1392 | }
|
---|
1393 | }
|
---|
1394 | else
|
---|
1395 | {
|
---|
1396 | pGVM->gvmm.s.StatsSched.cHaltNotBlocking++;
|
---|
1397 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /* Make sure false wake up calls (gvmmR0SchedDoWakeUps) cause us to spin. */
|
---|
1401 | RTSemEventMultiReset(pGVM->gvmm.s.HaltEventMulti);
|
---|
1402 |
|
---|
1403 | return rc;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Wakes up the halted EMT thread so it can service a pending request.
|
---|
1409 | *
|
---|
1410 | * @returns VINF_SUCCESS if not yielded.
|
---|
1411 | * VINF_GVM_NOT_BLOCKED if the EMT thread wasn't blocked.
|
---|
1412 | * @param pVM Pointer to the shared VM structure.
|
---|
1413 | * @thread Any but EMT.
|
---|
1414 | */
|
---|
1415 | GVMMR0DECL(int) GVMMR0SchedWakeUp(PVM pVM)
|
---|
1416 | {
|
---|
1417 | /*
|
---|
1418 | * Validate input and take the UsedLock.
|
---|
1419 | */
|
---|
1420 | PGVM pGVM;
|
---|
1421 | PGVMM pGVMM;
|
---|
1422 | int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
|
---|
1423 | if (RT_SUCCESS(rc))
|
---|
1424 | {
|
---|
1425 | pGVM->gvmm.s.StatsSched.cWakeUpCalls++;
|
---|
1426 |
|
---|
1427 | /*
|
---|
1428 | * Signal the semaphore regardless of whether it's current blocked on it.
|
---|
1429 | *
|
---|
1430 | * The reason for this is that there is absolutely no way we can be 100%
|
---|
1431 | * certain that it isn't *about* go to go to sleep on it and just got
|
---|
1432 | * delayed a bit en route. So, we will always signal the semaphore when
|
---|
1433 | * the it is flagged as halted in the VMM.
|
---|
1434 | */
|
---|
1435 | if (pGVM->gvmm.s.u64HaltExpire)
|
---|
1436 | {
|
---|
1437 | rc = VINF_SUCCESS;
|
---|
1438 | ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, 0);
|
---|
1439 | }
|
---|
1440 | else
|
---|
1441 | {
|
---|
1442 | rc = VINF_GVM_NOT_BLOCKED;
|
---|
1443 | pGVM->gvmm.s.StatsSched.cWakeUpNotHalted++;
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | int rc2 = RTSemEventMultiSignal(pGVM->gvmm.s.HaltEventMulti);
|
---|
1447 | AssertRC(rc2);
|
---|
1448 |
|
---|
1449 | /*
|
---|
1450 | * While we're here, do a round of scheduling.
|
---|
1451 | */
|
---|
1452 | Assert(ASMGetFlags() & X86_EFL_IF);
|
---|
1453 | const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
|
---|
1454 | pGVM->gvmm.s.StatsSched.cWakeUpWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
|
---|
1455 |
|
---|
1456 |
|
---|
1457 | rc2 = RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1458 | AssertRC(rc2);
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
|
---|
1462 | return rc;
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 |
|
---|
1466 | /**
|
---|
1467 | * Poll the schedule to see if someone else should get a chance to run.
|
---|
1468 | *
|
---|
1469 | * This is a bit hackish and will not work too well if the machine is
|
---|
1470 | * under heavy load from non-VM processes.
|
---|
1471 | *
|
---|
1472 | * @returns VINF_SUCCESS if not yielded.
|
---|
1473 | * VINF_GVM_YIELDED if an attempt to switch to a different VM task was made.
|
---|
1474 | * @param pVM Pointer to the shared VM structure.
|
---|
1475 | * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
|
---|
1476 | * @param fYield Whether to yield or not.
|
---|
1477 | * This is for when we're spinning in the halt loop.
|
---|
1478 | * @thread EMT.
|
---|
1479 | */
|
---|
1480 | GVMMR0DECL(int) GVMMR0SchedPoll(PVM pVM, bool fYield)
|
---|
1481 | {
|
---|
1482 | /*
|
---|
1483 | * Validate input.
|
---|
1484 | */
|
---|
1485 | PGVM pGVM;
|
---|
1486 | PGVMM pGVMM;
|
---|
1487 | int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
|
---|
1488 | if (RT_SUCCESS(rc))
|
---|
1489 | {
|
---|
1490 | rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
1491 | AssertRC(rc);
|
---|
1492 | pGVM->gvmm.s.StatsSched.cPollCalls++;
|
---|
1493 |
|
---|
1494 | Assert(ASMGetFlags() & X86_EFL_IF);
|
---|
1495 | const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
|
---|
1496 |
|
---|
1497 | if (!fYield)
|
---|
1498 | pGVM->gvmm.s.StatsSched.cPollWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
|
---|
1499 | else
|
---|
1500 | {
|
---|
1501 | /** @todo implement this... */
|
---|
1502 | rc = VERR_NOT_IMPLEMENTED;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
|
---|
1509 | return rc;
|
---|
1510 | }
|
---|
1511 |
|
---|
1512 |
|
---|
1513 |
|
---|
1514 | /**
|
---|
1515 | * Retrieves the GVMM statistics visible to the caller.
|
---|
1516 | *
|
---|
1517 | * @returns VBox status code.
|
---|
1518 | *
|
---|
1519 | * @param pStats Where to put the statistics.
|
---|
1520 | * @param pSession The current session.
|
---|
1521 | * @param pVM The VM to obtain statistics for. Optional.
|
---|
1522 | */
|
---|
1523 | GVMMR0DECL(int) GVMMR0QueryStatistics(PGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
|
---|
1524 | {
|
---|
1525 | LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
|
---|
1526 |
|
---|
1527 | /*
|
---|
1528 | * Validate input.
|
---|
1529 | */
|
---|
1530 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1531 | AssertPtrReturn(pStats, VERR_INVALID_POINTER);
|
---|
1532 | pStats->cVMs = 0; /* (crash before taking the sem...) */
|
---|
1533 |
|
---|
1534 | /*
|
---|
1535 | * Take the lock and get the VM statistics.
|
---|
1536 | */
|
---|
1537 | PGVMM pGVMM;
|
---|
1538 | if (pVM)
|
---|
1539 | {
|
---|
1540 | PGVM pGVM;
|
---|
1541 | int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
|
---|
1542 | if (RT_FAILURE(rc))
|
---|
1543 | return rc;
|
---|
1544 | pStats->SchedVM = pGVM->gvmm.s.StatsSched;
|
---|
1545 | }
|
---|
1546 | else
|
---|
1547 | {
|
---|
1548 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
1549 | memset(&pStats->SchedVM, 0, sizeof(pStats->SchedVM));
|
---|
1550 |
|
---|
1551 | int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
1552 | AssertRCReturn(rc, rc);
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | /*
|
---|
1556 | * Enumerate the VMs and add the ones visibile to the statistics.
|
---|
1557 | */
|
---|
1558 | pStats->cVMs = 0;
|
---|
1559 | memset(&pStats->SchedSum, 0, sizeof(pStats->SchedSum));
|
---|
1560 |
|
---|
1561 | for (unsigned i = pGVMM->iUsedHead;
|
---|
1562 | i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
|
---|
1563 | i = pGVMM->aHandles[i].iNext)
|
---|
1564 | {
|
---|
1565 | PGVM pGVM = pGVMM->aHandles[i].pGVM;
|
---|
1566 | void *pvObj = pGVMM->aHandles[i].pvObj;
|
---|
1567 | if ( VALID_PTR(pvObj)
|
---|
1568 | && VALID_PTR(pGVM)
|
---|
1569 | && pGVM->u32Magic == GVM_MAGIC
|
---|
1570 | && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
|
---|
1571 | {
|
---|
1572 | pStats->cVMs++;
|
---|
1573 |
|
---|
1574 | pStats->SchedSum.cHaltCalls += pGVM->gvmm.s.StatsSched.cHaltCalls;
|
---|
1575 | pStats->SchedSum.cHaltBlocking += pGVM->gvmm.s.StatsSched.cHaltBlocking;
|
---|
1576 | pStats->SchedSum.cHaltTimeouts += pGVM->gvmm.s.StatsSched.cHaltTimeouts;
|
---|
1577 | pStats->SchedSum.cHaltNotBlocking += pGVM->gvmm.s.StatsSched.cHaltNotBlocking;
|
---|
1578 | pStats->SchedSum.cHaltWakeUps += pGVM->gvmm.s.StatsSched.cHaltWakeUps;
|
---|
1579 |
|
---|
1580 | pStats->SchedSum.cWakeUpCalls += pGVM->gvmm.s.StatsSched.cWakeUpCalls;
|
---|
1581 | pStats->SchedSum.cWakeUpNotHalted += pGVM->gvmm.s.StatsSched.cWakeUpNotHalted;
|
---|
1582 | pStats->SchedSum.cWakeUpWakeUps += pGVM->gvmm.s.StatsSched.cWakeUpWakeUps;
|
---|
1583 |
|
---|
1584 | pStats->SchedSum.cPollCalls += pGVM->gvmm.s.StatsSched.cPollCalls;
|
---|
1585 | pStats->SchedSum.cPollHalts += pGVM->gvmm.s.StatsSched.cPollHalts;
|
---|
1586 | pStats->SchedSum.cPollWakeUps += pGVM->gvmm.s.StatsSched.cPollWakeUps;
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1591 |
|
---|
1592 | return VINF_SUCCESS;
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 |
|
---|
1596 | /**
|
---|
1597 | * VMMR0 request wrapper for GVMMR0QueryStatistics.
|
---|
1598 | *
|
---|
1599 | * @returns see GVMMR0QueryStatistics.
|
---|
1600 | * @param pVM Pointer to the shared VM structure. Optional.
|
---|
1601 | * @param pReq The request packet.
|
---|
1602 | */
|
---|
1603 | GVMMR0DECL(int) GVMMR0QueryStatisticsReq(PVM pVM, PGVMMQUERYSTATISTICSSREQ pReq)
|
---|
1604 | {
|
---|
1605 | /*
|
---|
1606 | * Validate input and pass it on.
|
---|
1607 | */
|
---|
1608 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
1609 | AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
|
---|
1610 |
|
---|
1611 | return GVMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pVM);
|
---|
1612 | }
|
---|
1613 |
|
---|
1614 |
|
---|
1615 | /**
|
---|
1616 | * Resets the specified GVMM statistics.
|
---|
1617 | *
|
---|
1618 | * @returns VBox status code.
|
---|
1619 | *
|
---|
1620 | * @param pStats Which statistics to reset, that is, non-zero fields indicates which to reset.
|
---|
1621 | * @param pSession The current session.
|
---|
1622 | * @param pVM The VM to reset statistics for. Optional.
|
---|
1623 | */
|
---|
1624 | GVMMR0DECL(int) GVMMR0ResetStatistics(PCGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
|
---|
1625 | {
|
---|
1626 | LogFlow(("GVMMR0ResetStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
|
---|
1627 |
|
---|
1628 | /*
|
---|
1629 | * Validate input.
|
---|
1630 | */
|
---|
1631 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1632 | AssertPtrReturn(pStats, VERR_INVALID_POINTER);
|
---|
1633 |
|
---|
1634 | /*
|
---|
1635 | * Take the lock and get the VM statistics.
|
---|
1636 | */
|
---|
1637 | PGVMM pGVMM;
|
---|
1638 | if (pVM)
|
---|
1639 | {
|
---|
1640 | PGVM pGVM;
|
---|
1641 | int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
|
---|
1642 | if (RT_FAILURE(rc))
|
---|
1643 | return rc;
|
---|
1644 | # define MAYBE_RESET_FIELD(field) \
|
---|
1645 | do { if (pStats->SchedVM. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
|
---|
1646 | MAYBE_RESET_FIELD(cHaltCalls);
|
---|
1647 | MAYBE_RESET_FIELD(cHaltBlocking);
|
---|
1648 | MAYBE_RESET_FIELD(cHaltTimeouts);
|
---|
1649 | MAYBE_RESET_FIELD(cHaltNotBlocking);
|
---|
1650 | MAYBE_RESET_FIELD(cHaltWakeUps);
|
---|
1651 | MAYBE_RESET_FIELD(cWakeUpCalls);
|
---|
1652 | MAYBE_RESET_FIELD(cWakeUpNotHalted);
|
---|
1653 | MAYBE_RESET_FIELD(cWakeUpWakeUps);
|
---|
1654 | MAYBE_RESET_FIELD(cPollCalls);
|
---|
1655 | MAYBE_RESET_FIELD(cPollHalts);
|
---|
1656 | MAYBE_RESET_FIELD(cPollWakeUps);
|
---|
1657 | # undef MAYBE_RESET_FIELD
|
---|
1658 | }
|
---|
1659 | else
|
---|
1660 | {
|
---|
1661 | GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
|
---|
1662 |
|
---|
1663 | int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
|
---|
1664 | AssertRCReturn(rc, rc);
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | /*
|
---|
1668 | * Enumerate the VMs and add the ones visibile to the statistics.
|
---|
1669 | */
|
---|
1670 | if (ASMMemIsAll8(&pStats->SchedSum, sizeof(pStats->SchedSum), 0))
|
---|
1671 | {
|
---|
1672 | for (unsigned i = pGVMM->iUsedHead;
|
---|
1673 | i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
|
---|
1674 | i = pGVMM->aHandles[i].iNext)
|
---|
1675 | {
|
---|
1676 | PGVM pGVM = pGVMM->aHandles[i].pGVM;
|
---|
1677 | void *pvObj = pGVMM->aHandles[i].pvObj;
|
---|
1678 | if ( VALID_PTR(pvObj)
|
---|
1679 | && VALID_PTR(pGVM)
|
---|
1680 | && pGVM->u32Magic == GVM_MAGIC
|
---|
1681 | && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
|
---|
1682 | {
|
---|
1683 | # define MAYBE_RESET_FIELD(field) \
|
---|
1684 | do { if (pStats->SchedSum. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
|
---|
1685 | MAYBE_RESET_FIELD(cHaltCalls);
|
---|
1686 | MAYBE_RESET_FIELD(cHaltBlocking);
|
---|
1687 | MAYBE_RESET_FIELD(cHaltTimeouts);
|
---|
1688 | MAYBE_RESET_FIELD(cHaltNotBlocking);
|
---|
1689 | MAYBE_RESET_FIELD(cHaltWakeUps);
|
---|
1690 | MAYBE_RESET_FIELD(cWakeUpCalls);
|
---|
1691 | MAYBE_RESET_FIELD(cWakeUpNotHalted);
|
---|
1692 | MAYBE_RESET_FIELD(cWakeUpWakeUps);
|
---|
1693 | MAYBE_RESET_FIELD(cPollCalls);
|
---|
1694 | MAYBE_RESET_FIELD(cPollHalts);
|
---|
1695 | MAYBE_RESET_FIELD(cPollWakeUps);
|
---|
1696 | # undef MAYBE_RESET_FIELD
|
---|
1697 | }
|
---|
1698 | }
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | RTSemFastMutexRelease(pGVMM->UsedLock);
|
---|
1702 |
|
---|
1703 | return VINF_SUCCESS;
|
---|
1704 | }
|
---|
1705 |
|
---|
1706 |
|
---|
1707 | /**
|
---|
1708 | * VMMR0 request wrapper for GVMMR0ResetStatistics.
|
---|
1709 | *
|
---|
1710 | * @returns see GVMMR0ResetStatistics.
|
---|
1711 | * @param pVM Pointer to the shared VM structure. Optional.
|
---|
1712 | * @param pReq The request packet.
|
---|
1713 | */
|
---|
1714 | GVMMR0DECL(int) GVMMR0ResetStatisticsReq(PVM pVM, PGVMMRESETSTATISTICSSREQ pReq)
|
---|
1715 | {
|
---|
1716 | /*
|
---|
1717 | * Validate input and pass it on.
|
---|
1718 | */
|
---|
1719 | AssertPtrReturn(pReq, VERR_INVALID_POINTER);
|
---|
1720 | AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
|
---|
1721 |
|
---|
1722 | return GVMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pVM);
|
---|
1723 | }
|
---|
1724 |
|
---|