1 | /* $Id: PDMAllQueue.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PDM Queue - Transport data and tasks to EMT and R3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 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/pdm.h>
|
---|
25 | #ifndef IN_RC
|
---|
26 | # include <VBox/rem.h>
|
---|
27 | # include <VBox/mm.h>
|
---|
28 | #endif
|
---|
29 | #include <VBox/vm.h>
|
---|
30 | #include <VBox/err.h>
|
---|
31 | #include <VBox/log.h>
|
---|
32 | #include <iprt/asm.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Allocate an item from a queue.
|
---|
38 | * The allocated item must be handed on to PDMR3QueueInsert() after the
|
---|
39 | * data have been filled in.
|
---|
40 | *
|
---|
41 | * @returns Pointer to allocated queue item.
|
---|
42 | * @returns NULL on failure. The queue is exhausted.
|
---|
43 | * @param pQueue The queue handle.
|
---|
44 | * @thread Any thread.
|
---|
45 | */
|
---|
46 | VMMDECL(PPDMQUEUEITEMCORE) PDMQueueAlloc(PPDMQUEUE pQueue)
|
---|
47 | {
|
---|
48 | Assert(VALID_PTR(pQueue) && pQueue->CTX_SUFF(pVM));
|
---|
49 | PPDMQUEUEITEMCORE pNew;
|
---|
50 | uint32_t iNext;
|
---|
51 | uint32_t i;
|
---|
52 | do
|
---|
53 | {
|
---|
54 | i = pQueue->iFreeTail;
|
---|
55 | if (i == pQueue->iFreeHead)
|
---|
56 | {
|
---|
57 | STAM_REL_COUNTER_INC(&pQueue->StatAllocFailures);
|
---|
58 | return NULL;
|
---|
59 | }
|
---|
60 | pNew = pQueue->aFreeItems[i].CTX_SUFF(pItem);
|
---|
61 | iNext = (i + 1) % (pQueue->cItems + PDMQUEUE_FREE_SLACK);
|
---|
62 | } while (!ASMAtomicCmpXchgU32(&pQueue->iFreeTail, iNext, i));
|
---|
63 | return pNew;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Queue an item.
|
---|
69 | * The item must have been obtained using PDMQueueAlloc(). Once the item
|
---|
70 | * have been passed to this function it must not be touched!
|
---|
71 | *
|
---|
72 | * @param pQueue The queue handle.
|
---|
73 | * @param pItem The item to insert.
|
---|
74 | * @thread Any thread.
|
---|
75 | */
|
---|
76 | VMMDECL(void) PDMQueueInsert(PPDMQUEUE pQueue, PPDMQUEUEITEMCORE pItem)
|
---|
77 | {
|
---|
78 | Assert(VALID_PTR(pQueue) && pQueue->CTX_SUFF(pVM));
|
---|
79 | Assert(VALID_PTR(pItem));
|
---|
80 |
|
---|
81 | PPDMQUEUEITEMCORE pNext;
|
---|
82 | do
|
---|
83 | {
|
---|
84 | pNext = pQueue->CTX_SUFF(pPending);
|
---|
85 | pItem->CTX_SUFF(pNext) = pNext;
|
---|
86 | } while (!ASMAtomicCmpXchgPtr((void * volatile *)&pQueue->CTX_SUFF(pPending), pItem, pNext));
|
---|
87 |
|
---|
88 | if (!pQueue->pTimer)
|
---|
89 | {
|
---|
90 | PVM pVM = pQueue->CTX_SUFF(pVM);
|
---|
91 | Log2(("PDMQueueInsert: VM_FF_PDM_QUEUES %d -> 1\n", VM_FF_ISSET(pVM, VM_FF_PDM_QUEUES)));
|
---|
92 | VM_FF_SET(pVM, VM_FF_PDM_QUEUES);
|
---|
93 | ASMAtomicBitSet(&pVM->pdm.s.fQueueFlushing, PDM_QUEUE_FLUSH_FLAG_PENDING_BIT);
|
---|
94 | #ifdef IN_RING3
|
---|
95 | REMR3NotifyQueuePending(pVM); /** @todo r=bird: we can remove REMR3NotifyQueuePending and let VMR3NotifyFF do the work. */
|
---|
96 | VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_DONE_REM);
|
---|
97 | #endif
|
---|
98 | }
|
---|
99 | STAM_REL_COUNTER_INC(&pQueue->StatInsert);
|
---|
100 | STAM_STATS({ ASMAtomicIncU32(&pQueue->cStatPending); });
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Queue an item.
|
---|
106 | * The item must have been obtained using PDMQueueAlloc(). Once the item
|
---|
107 | * have been passed to this function it must not be touched!
|
---|
108 | *
|
---|
109 | * @param pQueue The queue handle.
|
---|
110 | * @param pItem The item to insert.
|
---|
111 | * @param NanoMaxDelay The maximum delay before processing the queue, in nanoseconds.
|
---|
112 | * This applies only to GC.
|
---|
113 | * @thread Any thread.
|
---|
114 | */
|
---|
115 | VMMDECL(void) PDMQueueInsertEx(PPDMQUEUE pQueue, PPDMQUEUEITEMCORE pItem, uint64_t NanoMaxDelay)
|
---|
116 | {
|
---|
117 | PDMQueueInsert(pQueue, pItem);
|
---|
118 | #ifdef IN_RC
|
---|
119 | PVM pVM = pQueue->CTX_SUFF(pVM);
|
---|
120 | /** @todo figure out where to put this, the next bit should go there too.
|
---|
121 | if (NanoMaxDelay)
|
---|
122 | {
|
---|
123 |
|
---|
124 | }
|
---|
125 | else */
|
---|
126 | {
|
---|
127 | VMCPU_FF_SET(VMMGetCpu0(pVM), VMCPU_FF_TO_R3);
|
---|
128 | Log2(("PDMQueueInsertEx: Setting VMCPU_FF_TO_R3\n"));
|
---|
129 | }
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Gets the RC pointer for the specified queue.
|
---|
137 | *
|
---|
138 | * @returns The RC address of the queue.
|
---|
139 | * @returns NULL if pQueue is invalid.
|
---|
140 | * @param pQueue The queue handle.
|
---|
141 | */
|
---|
142 | VMMDECL(RCPTRTYPE(PPDMQUEUE)) PDMQueueRCPtr(PPDMQUEUE pQueue)
|
---|
143 | {
|
---|
144 | Assert(VALID_PTR(pQueue));
|
---|
145 | Assert(pQueue->pVMR3 && pQueue->pVMRC);
|
---|
146 | #ifdef IN_RC
|
---|
147 | return pQueue;
|
---|
148 | #else
|
---|
149 | return MMHyperCCToRC(pQueue->CTX_SUFF(pVM), pQueue);
|
---|
150 | #endif
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Gets the ring-0 pointer for the specified queue.
|
---|
156 | *
|
---|
157 | * @returns The ring-0 address of the queue.
|
---|
158 | * @returns NULL if pQueue is invalid.
|
---|
159 | * @param pQueue The queue handle.
|
---|
160 | */
|
---|
161 | VMMDECL(R0PTRTYPE(PPDMQUEUE)) PDMQueueR0Ptr(PPDMQUEUE pQueue)
|
---|
162 | {
|
---|
163 | Assert(VALID_PTR(pQueue));
|
---|
164 | Assert(pQueue->pVMR3 && pQueue->pVMR0);
|
---|
165 | #ifdef IN_RING0
|
---|
166 | return pQueue;
|
---|
167 | #else
|
---|
168 | return MMHyperCCToR0(pQueue->CTX_SUFF(pVM), pQueue);
|
---|
169 | #endif
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Flushes a PDM queue.
|
---|
175 | *
|
---|
176 | * @param pQueue The queue handle.
|
---|
177 | */
|
---|
178 | VMMDECL(void) PDMQueueFlush(PPDMQUEUE pQueue)
|
---|
179 | {
|
---|
180 | Assert(VALID_PTR(pQueue));
|
---|
181 | Assert(pQueue->pVMR3);
|
---|
182 | PVM pVM = pQueue->CTX_SUFF(pVM);
|
---|
183 |
|
---|
184 | #if defined(IN_RC) || defined(IN_RING0)
|
---|
185 | Assert(pQueue->CTX_SUFF(pVM));
|
---|
186 | pVM->pdm.s.CTX_SUFF(pQueueFlush) = pQueue;
|
---|
187 | VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PDM_QUEUE_FLUSH, (uintptr_t)pQueue);
|
---|
188 |
|
---|
189 | #else /* IN_RING3: */
|
---|
190 | VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)PDMR3QueueFlushWorker, 2, pVM, pQueue);
|
---|
191 | #endif
|
---|
192 | }
|
---|