VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PDMQueue.cpp@ 45733

Last change on this file since 45733 was 44504, checked in by vboxsync, 12 years ago

small cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 28.9 KB
Line 
1/* $Id: PDMQueue.cpp 44504 2013-02-01 10:09:30Z vboxsync $ */
2/** @file
3 * PDM Queue - Transport data and tasks to EMT and R3.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_PDM_QUEUE
23#include "PDMInternal.h"
24#include <VBox/vmm/pdm.h>
25#include <VBox/vmm/mm.h>
26#ifdef VBOX_WITH_REM
27# include <VBox/vmm/rem.h>
28#endif
29#include <VBox/vmm/vm.h>
30#include <VBox/vmm/uvm.h>
31#include <VBox/err.h>
32
33#include <VBox/log.h>
34#include <iprt/asm.h>
35#include <iprt/assert.h>
36#include <iprt/thread.h>
37
38
39/*******************************************************************************
40* Internal Functions *
41*******************************************************************************/
42DECLINLINE(void) pdmR3QueueFreeItem(PPDMQUEUE pQueue, PPDMQUEUEITEMCORE pItem);
43static bool pdmR3QueueFlush(PPDMQUEUE pQueue);
44static DECLCALLBACK(void) pdmR3QueueTimer(PVM pVM, PTMTIMER pTimer, void *pvUser);
45
46
47
48/**
49 * Internal worker for the queue creation apis.
50 *
51 * @returns VBox status.
52 * @param pVM Pointer to the VM.
53 * @param cbItem Item size.
54 * @param cItems Number of items.
55 * @param cMilliesInterval Number of milliseconds between polling the queue.
56 * If 0 then the emulation thread will be notified whenever an item arrives.
57 * @param fRZEnabled Set if the queue will be used from RC/R0 and need to be allocated from the hyper heap.
58 * @param pszName The queue name. Unique. Not copied.
59 * @param ppQueue Where to store the queue handle.
60 */
61static int pdmR3QueueCreate(PVM pVM, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval, bool fRZEnabled,
62 const char *pszName, PPDMQUEUE *ppQueue)
63{
64 PUVM pUVM = pVM->pUVM;
65
66 /*
67 * Validate input.
68 */
69 AssertMsgReturn(cbItem >= sizeof(PDMQUEUEITEMCORE) && cbItem < _1M, ("cbItem=%zu\n", cbItem), VERR_OUT_OF_RANGE);
70 AssertMsgReturn(cItems >= 1 && cItems <= _64K, ("cItems=%u\n", cItems), VERR_OUT_OF_RANGE);
71
72 /*
73 * Align the item size and calculate the structure size.
74 */
75 cbItem = RT_ALIGN(cbItem, sizeof(RTUINTPTR));
76 size_t cb = cbItem * cItems + RT_ALIGN_Z(RT_OFFSETOF(PDMQUEUE, aFreeItems[cItems + PDMQUEUE_FREE_SLACK]), 16);
77 PPDMQUEUE pQueue;
78 int rc;
79 if (fRZEnabled)
80 rc = MMHyperAlloc(pVM, cb, 0, MM_TAG_PDM_QUEUE, (void **)&pQueue );
81 else
82 rc = MMR3HeapAllocZEx(pVM, MM_TAG_PDM_QUEUE, cb, (void **)&pQueue);
83 if (RT_FAILURE(rc))
84 return rc;
85
86 /*
87 * Initialize the data fields.
88 */
89 pQueue->pVMR3 = pVM;
90 pQueue->pVMR0 = fRZEnabled ? pVM->pVMR0 : NIL_RTR0PTR;
91 pQueue->pVMRC = fRZEnabled ? pVM->pVMRC : NIL_RTRCPTR;
92 pQueue->pszName = pszName;
93 pQueue->cMilliesInterval = cMilliesInterval;
94 //pQueue->pTimer = NULL;
95 pQueue->cbItem = (uint32_t)cbItem;
96 pQueue->cItems = cItems;
97 //pQueue->pPendingR3 = NULL;
98 //pQueue->pPendingR0 = NULL;
99 //pQueue->pPendingRC = NULL;
100 pQueue->iFreeHead = cItems;
101 //pQueue->iFreeTail = 0;
102 PPDMQUEUEITEMCORE pItem = (PPDMQUEUEITEMCORE)((char *)pQueue + RT_ALIGN_Z(RT_OFFSETOF(PDMQUEUE, aFreeItems[cItems + PDMQUEUE_FREE_SLACK]), 16));
103 for (unsigned i = 0; i < cItems; i++, pItem = (PPDMQUEUEITEMCORE)((char *)pItem + cbItem))
104 {
105 pQueue->aFreeItems[i].pItemR3 = pItem;
106 if (fRZEnabled)
107 {
108 pQueue->aFreeItems[i].pItemR0 = MMHyperR3ToR0(pVM, pItem);
109 pQueue->aFreeItems[i].pItemRC = MMHyperR3ToRC(pVM, pItem);
110 }
111 }
112
113 /*
114 * Create timer?
115 */
116 if (cMilliesInterval)
117 {
118 rc = TMR3TimerCreateInternal(pVM, TMCLOCK_REAL, pdmR3QueueTimer, pQueue, "Queue timer", &pQueue->pTimer);
119 if (RT_SUCCESS(rc))
120 {
121 rc = TMTimerSetMillies(pQueue->pTimer, cMilliesInterval);
122 if (RT_FAILURE(rc))
123 {
124 AssertMsgFailed(("TMTimerSetMillies failed rc=%Rrc\n", rc));
125 int rc2 = TMR3TimerDestroy(pQueue->pTimer); AssertRC(rc2);
126 }
127 }
128 else
129 AssertMsgFailed(("TMR3TimerCreateInternal failed rc=%Rrc\n", rc));
130 if (RT_FAILURE(rc))
131 {
132 if (fRZEnabled)
133 MMHyperFree(pVM, pQueue);
134 else
135 MMR3HeapFree(pQueue);
136 return rc;
137 }
138
139 /*
140 * Insert into the queue list for timer driven queues.
141 */
142 pdmLock(pVM);
143 pQueue->pNext = pUVM->pdm.s.pQueuesTimer;
144 pUVM->pdm.s.pQueuesTimer = pQueue;
145 pdmUnlock(pVM);
146 }
147 else
148 {
149 /*
150 * Insert into the queue list for forced action driven queues.
151 * This is a FIFO, so insert at the end.
152 */
153 /** @todo we should add a priority to the queues so we don't have to rely on
154 * the initialization order to deal with problems like @bugref{1605} (pgm/pcnet
155 * deadlock caused by the critsect queue to be last in the chain).
156 * - Update, the critical sections are no longer using queues, so this isn't a real
157 * problem any longer. The priority might be a nice feature for later though.
158 */
159 pdmLock(pVM);
160 if (!pUVM->pdm.s.pQueuesForced)
161 pUVM->pdm.s.pQueuesForced = pQueue;
162 else
163 {
164 PPDMQUEUE pPrev = pUVM->pdm.s.pQueuesForced;
165 while (pPrev->pNext)
166 pPrev = pPrev->pNext;
167 pPrev->pNext = pQueue;
168 }
169 pdmUnlock(pVM);
170 }
171
172 /*
173 * Register the statistics.
174 */
175 STAMR3RegisterF(pVM, &pQueue->cbItem, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Item size.", "/PDM/Queue/%s/cbItem", pQueue->pszName);
176 STAMR3RegisterF(pVM, &pQueue->cItems, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Queue size.", "/PDM/Queue/%s/cItems", pQueue->pszName);
177 STAMR3RegisterF(pVM, &pQueue->StatAllocFailures, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "PDMQueueAlloc failures.", "/PDM/Queue/%s/AllocFailures", pQueue->pszName);
178 STAMR3RegisterF(pVM, &pQueue->StatInsert, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS, "Calls to PDMQueueInsert.", "/PDM/Queue/%s/Insert", pQueue->pszName);
179 STAMR3RegisterF(pVM, &pQueue->StatFlush, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS, "Calls to pdmR3QueueFlush.", "/PDM/Queue/%s/Flush", pQueue->pszName);
180 STAMR3RegisterF(pVM, &pQueue->StatFlushLeftovers, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Left over items after flush.", "/PDM/Queue/%s/FlushLeftovers", pQueue->pszName);
181#ifdef VBOX_WITH_STATISTICS
182 STAMR3RegisterF(pVM, &pQueue->StatFlushPrf, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_CALLS, "Profiling pdmR3QueueFlush.", "/PDM/Queue/%s/FlushPrf", pQueue->pszName);
183 STAMR3RegisterF(pVM, (void *)&pQueue->cStatPending, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT, "Pending items.", "/PDM/Queue/%s/Pending", pQueue->pszName);
184#endif
185
186 *ppQueue = pQueue;
187 return VINF_SUCCESS;
188}
189
190
191/**
192 * Create a queue with a device owner.
193 *
194 * @returns VBox status code.
195 * @param pVM Pointer to the VM.
196 * @param pDevIns Device instance.
197 * @param cbItem Size a queue item.
198 * @param cItems Number of items in the queue.
199 * @param cMilliesInterval Number of milliseconds between polling the queue.
200 * If 0 then the emulation thread will be notified whenever an item arrives.
201 * @param pfnCallback The consumer function.
202 * @param fRZEnabled Set if the queue must be usable from RC/R0.
203 * @param pszName The queue name. Unique. Not copied.
204 * @param ppQueue Where to store the queue handle on success.
205 * @thread Emulation thread only.
206 */
207VMMR3_INT_DECL(int) PDMR3QueueCreateDevice(PVM pVM, PPDMDEVINS pDevIns, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
208 PFNPDMQUEUEDEV pfnCallback, bool fRZEnabled, const char *pszName, PPDMQUEUE *ppQueue)
209{
210 LogFlow(("PDMR3QueueCreateDevice: pDevIns=%p cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p fRZEnabled=%RTbool pszName=%s\n",
211 pDevIns, cbItem, cItems, cMilliesInterval, pfnCallback, fRZEnabled, pszName));
212
213 /*
214 * Validate input.
215 */
216 VMCPU_ASSERT_EMT(&pVM->aCpus[0]);
217 if (!pfnCallback)
218 {
219 AssertMsgFailed(("No consumer callback!\n"));
220 return VERR_INVALID_PARAMETER;
221 }
222
223 /*
224 * Create the queue.
225 */
226 PPDMQUEUE pQueue;
227 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, fRZEnabled, pszName, &pQueue);
228 if (RT_SUCCESS(rc))
229 {
230 pQueue->enmType = PDMQUEUETYPE_DEV;
231 pQueue->u.Dev.pDevIns = pDevIns;
232 pQueue->u.Dev.pfnCallback = pfnCallback;
233
234 *ppQueue = pQueue;
235 Log(("PDM: Created device queue %p; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p pDevIns=%p\n",
236 cbItem, cItems, cMilliesInterval, pfnCallback, pDevIns));
237 }
238 return rc;
239}
240
241
242/**
243 * Create a queue with a driver owner.
244 *
245 * @returns VBox status code.
246 * @param pVM Pointer to the VM.
247 * @param pDrvIns Driver instance.
248 * @param cbItem Size a queue item.
249 * @param cItems Number of items in the queue.
250 * @param cMilliesInterval Number of milliseconds between polling the queue.
251 * If 0 then the emulation thread will be notified whenever an item arrives.
252 * @param pfnCallback The consumer function.
253 * @param pszName The queue name. Unique. Not copied.
254 * @param ppQueue Where to store the queue handle on success.
255 * @thread Emulation thread only.
256 */
257VMMR3_INT_DECL(int) PDMR3QueueCreateDriver(PVM pVM, PPDMDRVINS pDrvIns, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
258 PFNPDMQUEUEDRV pfnCallback, const char *pszName, PPDMQUEUE *ppQueue)
259{
260 LogFlow(("PDMR3QueueCreateDriver: pDrvIns=%p cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p pszName=%s\n",
261 pDrvIns, cbItem, cItems, cMilliesInterval, pfnCallback, pszName));
262
263 /*
264 * Validate input.
265 */
266 VMCPU_ASSERT_EMT(&pVM->aCpus[0]);
267 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
268
269 /*
270 * Create the queue.
271 */
272 PPDMQUEUE pQueue;
273 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, false, pszName, &pQueue);
274 if (RT_SUCCESS(rc))
275 {
276 pQueue->enmType = PDMQUEUETYPE_DRV;
277 pQueue->u.Drv.pDrvIns = pDrvIns;
278 pQueue->u.Drv.pfnCallback = pfnCallback;
279
280 *ppQueue = pQueue;
281 Log(("PDM: Created driver queue %p; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p pDrvIns=%p\n",
282 cbItem, cItems, cMilliesInterval, pfnCallback, pDrvIns));
283 }
284 return rc;
285}
286
287
288/**
289 * Create a queue with an internal owner.
290 *
291 * @returns VBox status code.
292 * @param pVM Pointer to the VM.
293 * @param cbItem Size a queue item.
294 * @param cItems Number of items in the queue.
295 * @param cMilliesInterval Number of milliseconds between polling the queue.
296 * If 0 then the emulation thread will be notified whenever an item arrives.
297 * @param pfnCallback The consumer function.
298 * @param fRZEnabled Set if the queue must be usable from RC/R0.
299 * @param pszName The queue name. Unique. Not copied.
300 * @param ppQueue Where to store the queue handle on success.
301 * @thread Emulation thread only.
302 */
303VMMR3_INT_DECL(int) PDMR3QueueCreateInternal(PVM pVM, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
304 PFNPDMQUEUEINT pfnCallback, bool fRZEnabled, const char *pszName, PPDMQUEUE *ppQueue)
305{
306 LogFlow(("PDMR3QueueCreateInternal: cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p fRZEnabled=%RTbool pszName=%s\n",
307 cbItem, cItems, cMilliesInterval, pfnCallback, fRZEnabled, pszName));
308
309 /*
310 * Validate input.
311 */
312 VMCPU_ASSERT_EMT(&pVM->aCpus[0]);
313 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
314
315 /*
316 * Create the queue.
317 */
318 PPDMQUEUE pQueue;
319 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, fRZEnabled, pszName, &pQueue);
320 if (RT_SUCCESS(rc))
321 {
322 pQueue->enmType = PDMQUEUETYPE_INTERNAL;
323 pQueue->u.Int.pfnCallback = pfnCallback;
324
325 *ppQueue = pQueue;
326 Log(("PDM: Created internal queue %p; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p\n",
327 cbItem, cItems, cMilliesInterval, pfnCallback));
328 }
329 return rc;
330}
331
332
333/**
334 * Create a queue with an external owner.
335 *
336 * @returns VBox status code.
337 * @param pVM Pointer to the VM.
338 * @param cbItem Size a queue item.
339 * @param cItems Number of items in the queue.
340 * @param cMilliesInterval Number of milliseconds between polling the queue.
341 * If 0 then the emulation thread will be notified whenever an item arrives.
342 * @param pfnCallback The consumer function.
343 * @param pvUser The user argument to the consumer function.
344 * @param pszName The queue name. Unique. Not copied.
345 * @param ppQueue Where to store the queue handle on success.
346 * @thread Emulation thread only.
347 */
348VMMR3_INT_DECL(int) PDMR3QueueCreateExternal(PVM pVM, size_t cbItem, uint32_t cItems, uint32_t cMilliesInterval,
349 PFNPDMQUEUEEXT pfnCallback, void *pvUser, const char *pszName, PPDMQUEUE *ppQueue)
350{
351 LogFlow(("PDMR3QueueCreateExternal: cbItem=%d cItems=%d cMilliesInterval=%d pfnCallback=%p pszName=%s\n", cbItem, cItems, cMilliesInterval, pfnCallback, pszName));
352
353 /*
354 * Validate input.
355 */
356 VMCPU_ASSERT_EMT(&pVM->aCpus[0]);
357 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
358
359 /*
360 * Create the queue.
361 */
362 PPDMQUEUE pQueue;
363 int rc = pdmR3QueueCreate(pVM, cbItem, cItems, cMilliesInterval, false, pszName, &pQueue);
364 if (RT_SUCCESS(rc))
365 {
366 pQueue->enmType = PDMQUEUETYPE_EXTERNAL;
367 pQueue->u.Ext.pvUser = pvUser;
368 pQueue->u.Ext.pfnCallback = pfnCallback;
369
370 *ppQueue = pQueue;
371 Log(("PDM: Created external queue %p; cbItem=%d cItems=%d cMillies=%d pfnCallback=%p pvUser=%p\n",
372 cbItem, cItems, cMilliesInterval, pfnCallback, pvUser));
373 }
374 return rc;
375}
376
377
378/**
379 * Destroy a queue.
380 *
381 * @returns VBox status code.
382 * @param pQueue Queue to destroy.
383 * @thread Emulation thread only.
384 */
385VMMR3_INT_DECL(int) PDMR3QueueDestroy(PPDMQUEUE pQueue)
386{
387 LogFlow(("PDMR3QueueDestroy: pQueue=%p\n", pQueue));
388
389 /*
390 * Validate input.
391 */
392 if (!pQueue)
393 return VERR_INVALID_PARAMETER;
394 Assert(pQueue && pQueue->pVMR3);
395 PVM pVM = pQueue->pVMR3;
396 PUVM pUVM = pVM->pUVM;
397
398 pdmLock(pVM);
399
400 /*
401 * Unlink it.
402 */
403 if (pQueue->pTimer)
404 {
405 if (pUVM->pdm.s.pQueuesTimer != pQueue)
406 {
407 PPDMQUEUE pCur = pUVM->pdm.s.pQueuesTimer;
408 while (pCur)
409 {
410 if (pCur->pNext == pQueue)
411 {
412 pCur->pNext = pQueue->pNext;
413 break;
414 }
415 pCur = pCur->pNext;
416 }
417 AssertMsg(pCur, ("Didn't find the queue!\n"));
418 }
419 else
420 pUVM->pdm.s.pQueuesTimer = pQueue->pNext;
421 }
422 else
423 {
424 if (pUVM->pdm.s.pQueuesForced != pQueue)
425 {
426 PPDMQUEUE pCur = pUVM->pdm.s.pQueuesForced;
427 while (pCur)
428 {
429 if (pCur->pNext == pQueue)
430 {
431 pCur->pNext = pQueue->pNext;
432 break;
433 }
434 pCur = pCur->pNext;
435 }
436 AssertMsg(pCur, ("Didn't find the queue!\n"));
437 }
438 else
439 pUVM->pdm.s.pQueuesForced = pQueue->pNext;
440 }
441 pQueue->pNext = NULL;
442 pQueue->pVMR3 = NULL;
443 pdmUnlock(pVM);
444
445 /*
446 * Deregister statistics.
447 */
448 STAMR3Deregister(pVM, &pQueue->cbItem);
449 STAMR3Deregister(pVM, &pQueue->cbItem);
450 STAMR3Deregister(pVM, &pQueue->StatAllocFailures);
451 STAMR3Deregister(pVM, &pQueue->StatInsert);
452 STAMR3Deregister(pVM, &pQueue->StatFlush);
453 STAMR3Deregister(pVM, &pQueue->StatFlushLeftovers);
454#ifdef VBOX_WITH_STATISTICS
455 STAMR3Deregister(pVM, &pQueue->StatFlushPrf);
456 STAMR3Deregister(pVM, (void *)&pQueue->cStatPending);
457#endif
458
459 /*
460 * Destroy the timer and free it.
461 */
462 if (pQueue->pTimer)
463 {
464 TMR3TimerDestroy(pQueue->pTimer);
465 pQueue->pTimer = NULL;
466 }
467 if (pQueue->pVMRC)
468 {
469 pQueue->pVMRC = NIL_RTRCPTR;
470 pQueue->pVMR0 = NIL_RTR0PTR;
471 MMHyperFree(pVM, pQueue);
472 }
473 else
474 MMR3HeapFree(pQueue);
475
476 return VINF_SUCCESS;
477}
478
479
480/**
481 * Destroy a all queues owned by the specified device.
482 *
483 * @returns VBox status code.
484 * @param pVM Pointer to the VM.
485 * @param pDevIns Device instance.
486 * @thread Emulation thread only.
487 */
488VMMR3_INT_DECL(int) PDMR3QueueDestroyDevice(PVM pVM, PPDMDEVINS pDevIns)
489{
490 LogFlow(("PDMR3QueueDestroyDevice: pDevIns=%p\n", pDevIns));
491
492 /*
493 * Validate input.
494 */
495 if (!pDevIns)
496 return VERR_INVALID_PARAMETER;
497
498 PUVM pUVM = pVM->pUVM;
499 pdmLock(pVM);
500
501 /*
502 * Unlink it.
503 */
504 PPDMQUEUE pQueueNext = pUVM->pdm.s.pQueuesTimer;
505 PPDMQUEUE pQueue = pUVM->pdm.s.pQueuesForced;
506 do
507 {
508 while (pQueue)
509 {
510 if ( pQueue->enmType == PDMQUEUETYPE_DEV
511 && pQueue->u.Dev.pDevIns == pDevIns)
512 {
513 PPDMQUEUE pQueueDestroy = pQueue;
514 pQueue = pQueue->pNext;
515 int rc = PDMR3QueueDestroy(pQueueDestroy);
516 AssertRC(rc);
517 }
518 else
519 pQueue = pQueue->pNext;
520 }
521
522 /* next queue list */
523 pQueue = pQueueNext;
524 pQueueNext = NULL;
525 } while (pQueue);
526
527 pdmUnlock(pVM);
528 return VINF_SUCCESS;
529}
530
531
532/**
533 * Destroy a all queues owned by the specified driver.
534 *
535 * @returns VBox status code.
536 * @param pVM Pointer to the VM.
537 * @param pDrvIns Driver instance.
538 * @thread Emulation thread only.
539 */
540VMMR3_INT_DECL(int) PDMR3QueueDestroyDriver(PVM pVM, PPDMDRVINS pDrvIns)
541{
542 LogFlow(("PDMR3QueueDestroyDriver: pDrvIns=%p\n", pDrvIns));
543
544 /*
545 * Validate input.
546 */
547 if (!pDrvIns)
548 return VERR_INVALID_PARAMETER;
549
550 PUVM pUVM = pVM->pUVM;
551 pdmLock(pVM);
552
553 /*
554 * Unlink it.
555 */
556 PPDMQUEUE pQueueNext = pUVM->pdm.s.pQueuesTimer;
557 PPDMQUEUE pQueue = pUVM->pdm.s.pQueuesForced;
558 do
559 {
560 while (pQueue)
561 {
562 if ( pQueue->enmType == PDMQUEUETYPE_DRV
563 && pQueue->u.Drv.pDrvIns == pDrvIns)
564 {
565 PPDMQUEUE pQueueDestroy = pQueue;
566 pQueue = pQueue->pNext;
567 int rc = PDMR3QueueDestroy(pQueueDestroy);
568 AssertRC(rc);
569 }
570 else
571 pQueue = pQueue->pNext;
572 }
573
574 /* next queue list */
575 pQueue = pQueueNext;
576 pQueueNext = NULL;
577 } while (pQueue);
578
579 pdmUnlock(pVM);
580 return VINF_SUCCESS;
581}
582
583
584/**
585 * Relocate the queues.
586 *
587 * @param pVM Pointer to the VM.
588 * @param offDelta The relocation delta.
589 */
590void pdmR3QueueRelocate(PVM pVM, RTGCINTPTR offDelta)
591{
592 /*
593 * Process the queues.
594 */
595 PUVM pUVM = pVM->pUVM;
596 PPDMQUEUE pQueueNext = pUVM->pdm.s.pQueuesTimer;
597 PPDMQUEUE pQueue = pUVM->pdm.s.pQueuesForced;
598 do
599 {
600 while (pQueue)
601 {
602 if (pQueue->pVMRC)
603 {
604 pQueue->pVMRC = pVM->pVMRC;
605
606 /* Pending RC items. */
607 if (pQueue->pPendingRC)
608 {
609 pQueue->pPendingRC += offDelta;
610 PPDMQUEUEITEMCORE pCur = (PPDMQUEUEITEMCORE)MMHyperRCToR3(pVM, pQueue->pPendingRC);
611 while (pCur->pNextRC)
612 {
613 pCur->pNextRC += offDelta;
614 pCur = (PPDMQUEUEITEMCORE)MMHyperRCToR3(pVM, pCur->pNextRC);
615 }
616 }
617
618 /* The free items. */
619 uint32_t i = pQueue->iFreeTail;
620 while (i != pQueue->iFreeHead)
621 {
622 pQueue->aFreeItems[i].pItemRC = MMHyperR3ToRC(pVM, pQueue->aFreeItems[i].pItemR3);
623 i = (i + 1) % (pQueue->cItems + PDMQUEUE_FREE_SLACK);
624 }
625 }
626
627 /* next queue */
628 pQueue = pQueue->pNext;
629 }
630
631 /* next queue list */
632 pQueue = pQueueNext;
633 pQueueNext = NULL;
634 } while (pQueue);
635}
636
637
638/**
639 * Flush pending queues.
640 * This is a forced action callback.
641 *
642 * @param pVM Pointer to the VM.
643 * @thread Emulation thread only.
644 */
645VMMR3_INT_DECL(void) PDMR3QueueFlushAll(PVM pVM)
646{
647 VM_ASSERT_EMT(pVM);
648 LogFlow(("PDMR3QueuesFlush:\n"));
649
650 /*
651 * Only let one EMT flushing queues at any one time to preserve the order
652 * and to avoid wasting time. The FF is always cleared here, because it's
653 * only used to get someones attention. Queue inserts occurring during the
654 * flush are caught using the pending bit.
655 *
656 * Note! We must check the force action and pending flags after clearing
657 * the active bit!
658 */
659 VM_FF_CLEAR(pVM, VM_FF_PDM_QUEUES);
660 while (!ASMAtomicBitTestAndSet(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_ACTIVE_BIT))
661 {
662 ASMAtomicBitClear(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_PENDING_BIT);
663
664 for (PPDMQUEUE pCur = pVM->pUVM->pdm.s.pQueuesForced; pCur; pCur = pCur->pNext)
665 if ( pCur->pPendingR3
666 || pCur->pPendingR0
667 || pCur->pPendingRC)
668 pdmR3QueueFlush(pCur);
669
670 ASMAtomicBitClear(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_ACTIVE_BIT);
671
672 /* We're done if there were no inserts while we were busy. */
673 if ( !ASMBitTest(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_PENDING_BIT)
674 && !VM_FF_ISPENDING(pVM, VM_FF_PDM_QUEUES))
675 break;
676 VM_FF_CLEAR(pVM, VM_FF_PDM_QUEUES);
677 }
678}
679
680
681/**
682 * Process pending items in one queue.
683 *
684 * @returns Success indicator.
685 * If false the item the consumer said "enough!".
686 * @param pQueue The queue.
687 */
688static bool pdmR3QueueFlush(PPDMQUEUE pQueue)
689{
690 STAM_PROFILE_START(&pQueue->StatFlushPrf,p);
691
692 /*
693 * Get the lists.
694 */
695 PPDMQUEUEITEMCORE pItems = ASMAtomicXchgPtrT(&pQueue->pPendingR3, NULL, PPDMQUEUEITEMCORE);
696 RTRCPTR pItemsRC = ASMAtomicXchgRCPtr(&pQueue->pPendingRC, NIL_RTRCPTR);
697 RTR0PTR pItemsR0 = ASMAtomicXchgR0Ptr(&pQueue->pPendingR0, NIL_RTR0PTR);
698
699 AssertMsgReturn( pItemsR0
700 || pItemsRC
701 || pItems,
702 ("Someone is racing us? This shouldn't happen!\n"),
703 true);
704
705 /*
706 * Reverse the list (it's inserted in LIFO order to avoid semaphores, remember).
707 */
708 PPDMQUEUEITEMCORE pCur = pItems;
709 pItems = NULL;
710 while (pCur)
711 {
712 PPDMQUEUEITEMCORE pInsert = pCur;
713 pCur = pCur->pNextR3;
714 pInsert->pNextR3 = pItems;
715 pItems = pInsert;
716 }
717
718 /*
719 * Do the same for any pending RC items.
720 */
721 while (pItemsRC)
722 {
723 PPDMQUEUEITEMCORE pInsert = (PPDMQUEUEITEMCORE)MMHyperRCToR3(pQueue->pVMR3, pItemsRC);
724 pItemsRC = pInsert->pNextRC;
725 pInsert->pNextRC = NIL_RTRCPTR;
726 pInsert->pNextR3 = pItems;
727 pItems = pInsert;
728 }
729
730 /*
731 * Do the same for any pending R0 items.
732 */
733 while (pItemsR0)
734 {
735 PPDMQUEUEITEMCORE pInsert = (PPDMQUEUEITEMCORE)MMHyperR0ToR3(pQueue->pVMR3, pItemsR0);
736 pItemsR0 = pInsert->pNextR0;
737 pInsert->pNextR0 = NIL_RTR0PTR;
738 pInsert->pNextR3 = pItems;
739 pItems = pInsert;
740 }
741
742 /*
743 * Feed the items to the consumer function.
744 */
745 Log2(("pdmR3QueueFlush: pQueue=%p enmType=%d pItems=%p\n", pQueue, pQueue->enmType, pItems));
746 switch (pQueue->enmType)
747 {
748 case PDMQUEUETYPE_DEV:
749 while (pItems)
750 {
751 if (!pQueue->u.Dev.pfnCallback(pQueue->u.Dev.pDevIns, pItems))
752 break;
753 pCur = pItems;
754 pItems = pItems->pNextR3;
755 pdmR3QueueFreeItem(pQueue, pCur);
756 }
757 break;
758
759 case PDMQUEUETYPE_DRV:
760 while (pItems)
761 {
762 if (!pQueue->u.Drv.pfnCallback(pQueue->u.Drv.pDrvIns, pItems))
763 break;
764 pCur = pItems;
765 pItems = pItems->pNextR3;
766 pdmR3QueueFreeItem(pQueue, pCur);
767 }
768 break;
769
770 case PDMQUEUETYPE_INTERNAL:
771 while (pItems)
772 {
773 if (!pQueue->u.Int.pfnCallback(pQueue->pVMR3, pItems))
774 break;
775 pCur = pItems;
776 pItems = pItems->pNextR3;
777 pdmR3QueueFreeItem(pQueue, pCur);
778 }
779 break;
780
781 case PDMQUEUETYPE_EXTERNAL:
782 while (pItems)
783 {
784 if (!pQueue->u.Ext.pfnCallback(pQueue->u.Ext.pvUser, pItems))
785 break;
786 pCur = pItems;
787 pItems = pItems->pNextR3;
788 pdmR3QueueFreeItem(pQueue, pCur);
789 }
790 break;
791
792 default:
793 AssertMsgFailed(("Invalid queue type %d\n", pQueue->enmType));
794 break;
795 }
796
797 /*
798 * Success?
799 */
800 if (pItems)
801 {
802 /*
803 * Reverse the list.
804 */
805 pCur = pItems;
806 pItems = NULL;
807 while (pCur)
808 {
809 PPDMQUEUEITEMCORE pInsert = pCur;
810 pCur = pInsert->pNextR3;
811 pInsert->pNextR3 = pItems;
812 pItems = pInsert;
813 }
814
815 /*
816 * Insert the list at the tail of the pending list.
817 */
818 for (;;)
819 {
820 if (ASMAtomicCmpXchgPtr(&pQueue->pPendingR3, pItems, NULL))
821 break;
822 PPDMQUEUEITEMCORE pPending = ASMAtomicXchgPtrT(&pQueue->pPendingR3, NULL, PPDMQUEUEITEMCORE);
823 if (pPending)
824 {
825 pCur = pPending;
826 while (pCur->pNextR3)
827 pCur = pCur->pNextR3;
828 pCur->pNextR3 = pItems;
829 pItems = pPending;
830 }
831 }
832
833 STAM_REL_COUNTER_INC(&pQueue->StatFlushLeftovers);
834 STAM_PROFILE_STOP(&pQueue->StatFlushPrf,p);
835 return false;
836 }
837
838 STAM_PROFILE_STOP(&pQueue->StatFlushPrf,p);
839 return true;
840}
841
842
843/**
844 * Free an item.
845 *
846 * @param pQueue The queue.
847 * @param pItem The item.
848 */
849DECLINLINE(void) pdmR3QueueFreeItem(PPDMQUEUE pQueue, PPDMQUEUEITEMCORE pItem)
850{
851 VM_ASSERT_EMT(pQueue->pVMR3);
852
853 int i = pQueue->iFreeHead;
854 int iNext = (i + 1) % (pQueue->cItems + PDMQUEUE_FREE_SLACK);
855
856 pQueue->aFreeItems[i].pItemR3 = pItem;
857 if (pQueue->pVMRC)
858 {
859 pQueue->aFreeItems[i].pItemRC = MMHyperR3ToRC(pQueue->pVMR3, pItem);
860 pQueue->aFreeItems[i].pItemR0 = MMHyperR3ToR0(pQueue->pVMR3, pItem);
861 }
862
863 if (!ASMAtomicCmpXchgU32(&pQueue->iFreeHead, iNext, i))
864 AssertMsgFailed(("huh? i=%d iNext=%d iFreeHead=%d iFreeTail=%d\n", i, iNext, pQueue->iFreeHead, pQueue->iFreeTail));
865 STAM_STATS({ ASMAtomicDecU32(&pQueue->cStatPending); });
866}
867
868
869/**
870 * Timer handler for PDM queues.
871 * This is called by for a single queue.
872 *
873 * @param pVM Pointer to the VM.
874 * @param pTimer Pointer to timer.
875 * @param pvUser Pointer to the queue.
876 */
877static DECLCALLBACK(void) pdmR3QueueTimer(PVM pVM, PTMTIMER pTimer, void *pvUser)
878{
879 PPDMQUEUE pQueue = (PPDMQUEUE)pvUser;
880 Assert(pTimer == pQueue->pTimer); NOREF(pTimer); NOREF(pVM);
881
882 if ( pQueue->pPendingR3
883 || pQueue->pPendingR0
884 || pQueue->pPendingRC)
885 pdmR3QueueFlush(pQueue);
886 int rc = TMTimerSetMillies(pQueue->pTimer, pQueue->cMilliesInterval);
887 AssertRC(rc);
888}
889
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