1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions *
4*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions *
6*5e3eaea3SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions *
15*5e3eaea3SApple OSS Distributions * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions *
18*5e3eaea3SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions * limitations under the License.
25*5e3eaea3SApple OSS Distributions *
26*5e3eaea3SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions */
28*5e3eaea3SApple OSS Distributions
29*5e3eaea3SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*5e3eaea3SApple OSS Distributions
31*5e3eaea3SApple OSS Distributions #include <IOKit/IOSharedDataQueue.h>
32*5e3eaea3SApple OSS Distributions #include <IOKit/IODataQueueShared.h>
33*5e3eaea3SApple OSS Distributions #include <IOKit/IOLib.h>
34*5e3eaea3SApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
35*5e3eaea3SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
36*5e3eaea3SApple OSS Distributions
37*5e3eaea3SApple OSS Distributions #ifdef enqueue
38*5e3eaea3SApple OSS Distributions #undef enqueue
39*5e3eaea3SApple OSS Distributions #endif
40*5e3eaea3SApple OSS Distributions
41*5e3eaea3SApple OSS Distributions #ifdef dequeue
42*5e3eaea3SApple OSS Distributions #undef dequeue
43*5e3eaea3SApple OSS Distributions #endif
44*5e3eaea3SApple OSS Distributions
45*5e3eaea3SApple OSS Distributions #define super IODataQueue
46*5e3eaea3SApple OSS Distributions
OSDefineMetaClassAndStructors(IOSharedDataQueue,IODataQueue)47*5e3eaea3SApple OSS Distributions OSDefineMetaClassAndStructors(IOSharedDataQueue, IODataQueue)
48*5e3eaea3SApple OSS Distributions
49*5e3eaea3SApple OSS Distributions OSSharedPtr<IOSharedDataQueue>
50*5e3eaea3SApple OSS Distributions IOSharedDataQueue::withCapacity(UInt32 size)
51*5e3eaea3SApple OSS Distributions {
52*5e3eaea3SApple OSS Distributions OSSharedPtr<IOSharedDataQueue> dataQueue = OSMakeShared<IOSharedDataQueue>();
53*5e3eaea3SApple OSS Distributions
54*5e3eaea3SApple OSS Distributions if (dataQueue) {
55*5e3eaea3SApple OSS Distributions if (!dataQueue->initWithCapacity(size)) {
56*5e3eaea3SApple OSS Distributions return nullptr;
57*5e3eaea3SApple OSS Distributions }
58*5e3eaea3SApple OSS Distributions }
59*5e3eaea3SApple OSS Distributions
60*5e3eaea3SApple OSS Distributions return dataQueue;
61*5e3eaea3SApple OSS Distributions }
62*5e3eaea3SApple OSS Distributions
63*5e3eaea3SApple OSS Distributions OSSharedPtr<IOSharedDataQueue>
withEntries(UInt32 numEntries,UInt32 entrySize)64*5e3eaea3SApple OSS Distributions IOSharedDataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
65*5e3eaea3SApple OSS Distributions {
66*5e3eaea3SApple OSS Distributions OSSharedPtr<IOSharedDataQueue> dataQueue = OSMakeShared<IOSharedDataQueue>();
67*5e3eaea3SApple OSS Distributions
68*5e3eaea3SApple OSS Distributions if (dataQueue) {
69*5e3eaea3SApple OSS Distributions if (!dataQueue->initWithEntries(numEntries, entrySize)) {
70*5e3eaea3SApple OSS Distributions return nullptr;
71*5e3eaea3SApple OSS Distributions }
72*5e3eaea3SApple OSS Distributions }
73*5e3eaea3SApple OSS Distributions
74*5e3eaea3SApple OSS Distributions return dataQueue;
75*5e3eaea3SApple OSS Distributions }
76*5e3eaea3SApple OSS Distributions
77*5e3eaea3SApple OSS Distributions Boolean
initWithCapacity(UInt32 size)78*5e3eaea3SApple OSS Distributions IOSharedDataQueue::initWithCapacity(UInt32 size)
79*5e3eaea3SApple OSS Distributions {
80*5e3eaea3SApple OSS Distributions IODataQueueAppendix * appendix;
81*5e3eaea3SApple OSS Distributions vm_size_t allocSize;
82*5e3eaea3SApple OSS Distributions kern_return_t kr;
83*5e3eaea3SApple OSS Distributions
84*5e3eaea3SApple OSS Distributions if (!super::init()) {
85*5e3eaea3SApple OSS Distributions return false;
86*5e3eaea3SApple OSS Distributions }
87*5e3eaea3SApple OSS Distributions
88*5e3eaea3SApple OSS Distributions _reserved = IOMallocType(ExpansionData);
89*5e3eaea3SApple OSS Distributions if (!_reserved) {
90*5e3eaea3SApple OSS Distributions return false;
91*5e3eaea3SApple OSS Distributions }
92*5e3eaea3SApple OSS Distributions
93*5e3eaea3SApple OSS Distributions if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE - DATA_QUEUE_MEMORY_APPENDIX_SIZE) {
94*5e3eaea3SApple OSS Distributions return false;
95*5e3eaea3SApple OSS Distributions }
96*5e3eaea3SApple OSS Distributions
97*5e3eaea3SApple OSS Distributions allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE);
98*5e3eaea3SApple OSS Distributions
99*5e3eaea3SApple OSS Distributions if (allocSize < size) {
100*5e3eaea3SApple OSS Distributions return false;
101*5e3eaea3SApple OSS Distributions }
102*5e3eaea3SApple OSS Distributions
103*5e3eaea3SApple OSS Distributions kr = kmem_alloc(kernel_map, (vm_offset_t *)&dataQueue, allocSize,
104*5e3eaea3SApple OSS Distributions (kma_flags_t)(KMA_DATA | KMA_ZERO), IOMemoryTag(kernel_map));
105*5e3eaea3SApple OSS Distributions if (kr != KERN_SUCCESS) {
106*5e3eaea3SApple OSS Distributions return false;
107*5e3eaea3SApple OSS Distributions }
108*5e3eaea3SApple OSS Distributions
109*5e3eaea3SApple OSS Distributions dataQueue->queueSize = size;
110*5e3eaea3SApple OSS Distributions // dataQueue->head = 0;
111*5e3eaea3SApple OSS Distributions // dataQueue->tail = 0;
112*5e3eaea3SApple OSS Distributions
113*5e3eaea3SApple OSS Distributions if (!setQueueSize(size)) {
114*5e3eaea3SApple OSS Distributions return false;
115*5e3eaea3SApple OSS Distributions }
116*5e3eaea3SApple OSS Distributions
117*5e3eaea3SApple OSS Distributions appendix = (IODataQueueAppendix *)((UInt8 *)dataQueue + size + DATA_QUEUE_MEMORY_HEADER_SIZE);
118*5e3eaea3SApple OSS Distributions appendix->version = 0;
119*5e3eaea3SApple OSS Distributions
120*5e3eaea3SApple OSS Distributions if (!notifyMsg) {
121*5e3eaea3SApple OSS Distributions notifyMsg = IOMallocType(mach_msg_header_t);
122*5e3eaea3SApple OSS Distributions if (!notifyMsg) {
123*5e3eaea3SApple OSS Distributions return false;
124*5e3eaea3SApple OSS Distributions }
125*5e3eaea3SApple OSS Distributions }
126*5e3eaea3SApple OSS Distributions bzero(notifyMsg, sizeof(mach_msg_header_t));
127*5e3eaea3SApple OSS Distributions
128*5e3eaea3SApple OSS Distributions setNotificationPort(MACH_PORT_NULL);
129*5e3eaea3SApple OSS Distributions
130*5e3eaea3SApple OSS Distributions return true;
131*5e3eaea3SApple OSS Distributions }
132*5e3eaea3SApple OSS Distributions
133*5e3eaea3SApple OSS Distributions void
free()134*5e3eaea3SApple OSS Distributions IOSharedDataQueue::free()
135*5e3eaea3SApple OSS Distributions {
136*5e3eaea3SApple OSS Distributions if (dataQueue) {
137*5e3eaea3SApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)dataQueue, round_page(getQueueSize() +
138*5e3eaea3SApple OSS Distributions DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE));
139*5e3eaea3SApple OSS Distributions dataQueue = NULL;
140*5e3eaea3SApple OSS Distributions if (notifyMsg) {
141*5e3eaea3SApple OSS Distributions IOFreeType(notifyMsg, mach_msg_header_t);
142*5e3eaea3SApple OSS Distributions notifyMsg = NULL;
143*5e3eaea3SApple OSS Distributions }
144*5e3eaea3SApple OSS Distributions }
145*5e3eaea3SApple OSS Distributions
146*5e3eaea3SApple OSS Distributions if (_reserved) {
147*5e3eaea3SApple OSS Distributions IOFreeType(_reserved, ExpansionData);
148*5e3eaea3SApple OSS Distributions _reserved = NULL;
149*5e3eaea3SApple OSS Distributions }
150*5e3eaea3SApple OSS Distributions
151*5e3eaea3SApple OSS Distributions super::free();
152*5e3eaea3SApple OSS Distributions }
153*5e3eaea3SApple OSS Distributions
154*5e3eaea3SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
getMemoryDescriptor()155*5e3eaea3SApple OSS Distributions IOSharedDataQueue::getMemoryDescriptor()
156*5e3eaea3SApple OSS Distributions {
157*5e3eaea3SApple OSS Distributions OSSharedPtr<IOMemoryDescriptor> descriptor;
158*5e3eaea3SApple OSS Distributions
159*5e3eaea3SApple OSS Distributions if (dataQueue != NULL) {
160*5e3eaea3SApple OSS Distributions descriptor = IOMemoryDescriptor::withAddress(dataQueue, getQueueSize() + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE, kIODirectionOutIn);
161*5e3eaea3SApple OSS Distributions }
162*5e3eaea3SApple OSS Distributions
163*5e3eaea3SApple OSS Distributions return descriptor;
164*5e3eaea3SApple OSS Distributions }
165*5e3eaea3SApple OSS Distributions
166*5e3eaea3SApple OSS Distributions
167*5e3eaea3SApple OSS Distributions IODataQueueEntry *
peek()168*5e3eaea3SApple OSS Distributions IOSharedDataQueue::peek()
169*5e3eaea3SApple OSS Distributions {
170*5e3eaea3SApple OSS Distributions IODataQueueEntry *entry = NULL;
171*5e3eaea3SApple OSS Distributions UInt32 headOffset;
172*5e3eaea3SApple OSS Distributions UInt32 tailOffset;
173*5e3eaea3SApple OSS Distributions
174*5e3eaea3SApple OSS Distributions if (!dataQueue) {
175*5e3eaea3SApple OSS Distributions return NULL;
176*5e3eaea3SApple OSS Distributions }
177*5e3eaea3SApple OSS Distributions
178*5e3eaea3SApple OSS Distributions // Read head and tail with acquire barrier
179*5e3eaea3SApple OSS Distributions // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
180*5e3eaea3SApple OSS Distributions headOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
181*5e3eaea3SApple OSS Distributions tailOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_ACQUIRE);
182*5e3eaea3SApple OSS Distributions
183*5e3eaea3SApple OSS Distributions if (headOffset != tailOffset) {
184*5e3eaea3SApple OSS Distributions volatile IODataQueueEntry * head = NULL;
185*5e3eaea3SApple OSS Distributions UInt32 headSize = 0;
186*5e3eaea3SApple OSS Distributions UInt32 headOffset = dataQueue->head;
187*5e3eaea3SApple OSS Distributions UInt32 queueSize = getQueueSize();
188*5e3eaea3SApple OSS Distributions
189*5e3eaea3SApple OSS Distributions if (headOffset > queueSize) {
190*5e3eaea3SApple OSS Distributions return NULL;
191*5e3eaea3SApple OSS Distributions }
192*5e3eaea3SApple OSS Distributions
193*5e3eaea3SApple OSS Distributions head = (IODataQueueEntry *)((char *)dataQueue->queue + headOffset);
194*5e3eaea3SApple OSS Distributions headSize = head->size;
195*5e3eaea3SApple OSS Distributions
196*5e3eaea3SApple OSS Distributions // Check if there's enough room before the end of the queue for a header.
197*5e3eaea3SApple OSS Distributions // If there is room, check if there's enough room to hold the header and
198*5e3eaea3SApple OSS Distributions // the data.
199*5e3eaea3SApple OSS Distributions
200*5e3eaea3SApple OSS Distributions if ((headOffset > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
201*5e3eaea3SApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize) ||
202*5e3eaea3SApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headSize) ||
203*5e3eaea3SApple OSS Distributions (headOffset + headSize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
204*5e3eaea3SApple OSS Distributions // No room for the header or the data, wrap to the beginning of the queue.
205*5e3eaea3SApple OSS Distributions // Note: wrapping even with the UINT32_MAX checks, as we have to support
206*5e3eaea3SApple OSS Distributions // queueSize of UINT32_MAX
207*5e3eaea3SApple OSS Distributions entry = dataQueue->queue;
208*5e3eaea3SApple OSS Distributions } else {
209*5e3eaea3SApple OSS Distributions entry = (IODataQueueEntry *)head;
210*5e3eaea3SApple OSS Distributions }
211*5e3eaea3SApple OSS Distributions }
212*5e3eaea3SApple OSS Distributions
213*5e3eaea3SApple OSS Distributions return entry;
214*5e3eaea3SApple OSS Distributions }
215*5e3eaea3SApple OSS Distributions
216*5e3eaea3SApple OSS Distributions Boolean
enqueue(void * data,UInt32 dataSize)217*5e3eaea3SApple OSS Distributions IOSharedDataQueue::enqueue(void * data, UInt32 dataSize)
218*5e3eaea3SApple OSS Distributions {
219*5e3eaea3SApple OSS Distributions UInt32 head;
220*5e3eaea3SApple OSS Distributions UInt32 tail;
221*5e3eaea3SApple OSS Distributions UInt32 newTail;
222*5e3eaea3SApple OSS Distributions const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
223*5e3eaea3SApple OSS Distributions IODataQueueEntry * entry;
224*5e3eaea3SApple OSS Distributions
225*5e3eaea3SApple OSS Distributions // Force a single read of head and tail
226*5e3eaea3SApple OSS Distributions // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
227*5e3eaea3SApple OSS Distributions tail = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_RELAXED);
228*5e3eaea3SApple OSS Distributions head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_ACQUIRE);
229*5e3eaea3SApple OSS Distributions
230*5e3eaea3SApple OSS Distributions // Check for overflow of entrySize
231*5e3eaea3SApple OSS Distributions if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) {
232*5e3eaea3SApple OSS Distributions return false;
233*5e3eaea3SApple OSS Distributions }
234*5e3eaea3SApple OSS Distributions // Check for underflow of (getQueueSize() - tail)
235*5e3eaea3SApple OSS Distributions if (getQueueSize() < tail || getQueueSize() < head) {
236*5e3eaea3SApple OSS Distributions return false;
237*5e3eaea3SApple OSS Distributions }
238*5e3eaea3SApple OSS Distributions
239*5e3eaea3SApple OSS Distributions if (tail >= head) {
240*5e3eaea3SApple OSS Distributions // Is there enough room at the end for the entry?
241*5e3eaea3SApple OSS Distributions if ((entrySize <= UINT32_MAX - tail) &&
242*5e3eaea3SApple OSS Distributions ((tail + entrySize) <= getQueueSize())) {
243*5e3eaea3SApple OSS Distributions entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
244*5e3eaea3SApple OSS Distributions
245*5e3eaea3SApple OSS Distributions entry->size = dataSize;
246*5e3eaea3SApple OSS Distributions __nochk_memcpy(&entry->data, data, dataSize);
247*5e3eaea3SApple OSS Distributions
248*5e3eaea3SApple OSS Distributions // The tail can be out of bound when the size of the new entry
249*5e3eaea3SApple OSS Distributions // exactly matches the available space at the end of the queue.
250*5e3eaea3SApple OSS Distributions // The tail can range from 0 to dataQueue->queueSize inclusive.
251*5e3eaea3SApple OSS Distributions
252*5e3eaea3SApple OSS Distributions newTail = tail + entrySize;
253*5e3eaea3SApple OSS Distributions } else if (head > entrySize) { // Is there enough room at the beginning?
254*5e3eaea3SApple OSS Distributions // Wrap around to the beginning, but do not allow the tail to catch
255*5e3eaea3SApple OSS Distributions // up to the head.
256*5e3eaea3SApple OSS Distributions
257*5e3eaea3SApple OSS Distributions dataQueue->queue->size = dataSize;
258*5e3eaea3SApple OSS Distributions
259*5e3eaea3SApple OSS Distributions // We need to make sure that there is enough room to set the size before
260*5e3eaea3SApple OSS Distributions // doing this. The user client checks for this and will look for the size
261*5e3eaea3SApple OSS Distributions // at the beginning if there isn't room for it at the end.
262*5e3eaea3SApple OSS Distributions
263*5e3eaea3SApple OSS Distributions if ((getQueueSize() - tail) >= DATA_QUEUE_ENTRY_HEADER_SIZE) {
264*5e3eaea3SApple OSS Distributions ((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
265*5e3eaea3SApple OSS Distributions }
266*5e3eaea3SApple OSS Distributions
267*5e3eaea3SApple OSS Distributions __nochk_memcpy(&dataQueue->queue->data, data, dataSize);
268*5e3eaea3SApple OSS Distributions newTail = entrySize;
269*5e3eaea3SApple OSS Distributions } else {
270*5e3eaea3SApple OSS Distributions return false; // queue is full
271*5e3eaea3SApple OSS Distributions }
272*5e3eaea3SApple OSS Distributions } else {
273*5e3eaea3SApple OSS Distributions // Do not allow the tail to catch up to the head when the queue is full.
274*5e3eaea3SApple OSS Distributions // That's why the comparison uses a '>' rather than '>='.
275*5e3eaea3SApple OSS Distributions
276*5e3eaea3SApple OSS Distributions if ((head - tail) > entrySize) {
277*5e3eaea3SApple OSS Distributions entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
278*5e3eaea3SApple OSS Distributions
279*5e3eaea3SApple OSS Distributions entry->size = dataSize;
280*5e3eaea3SApple OSS Distributions __nochk_memcpy(&entry->data, data, dataSize);
281*5e3eaea3SApple OSS Distributions newTail = tail + entrySize;
282*5e3eaea3SApple OSS Distributions } else {
283*5e3eaea3SApple OSS Distributions return false; // queue is full
284*5e3eaea3SApple OSS Distributions }
285*5e3eaea3SApple OSS Distributions }
286*5e3eaea3SApple OSS Distributions
287*5e3eaea3SApple OSS Distributions // Publish the data we just enqueued
288*5e3eaea3SApple OSS Distributions __c11_atomic_store((_Atomic UInt32 *)&dataQueue->tail, newTail, __ATOMIC_RELEASE);
289*5e3eaea3SApple OSS Distributions
290*5e3eaea3SApple OSS Distributions if (tail != head) {
291*5e3eaea3SApple OSS Distributions //
292*5e3eaea3SApple OSS Distributions // The memory barrier below paris with the one in ::dequeue
293*5e3eaea3SApple OSS Distributions // so that either our store to the tail cannot be missed by
294*5e3eaea3SApple OSS Distributions // the next dequeue attempt, or we will observe the dequeuer
295*5e3eaea3SApple OSS Distributions // making the queue empty.
296*5e3eaea3SApple OSS Distributions //
297*5e3eaea3SApple OSS Distributions // Of course, if we already think the queue is empty,
298*5e3eaea3SApple OSS Distributions // there's no point paying this extra cost.
299*5e3eaea3SApple OSS Distributions //
300*5e3eaea3SApple OSS Distributions __c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
301*5e3eaea3SApple OSS Distributions head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
302*5e3eaea3SApple OSS Distributions }
303*5e3eaea3SApple OSS Distributions
304*5e3eaea3SApple OSS Distributions if (tail == head) {
305*5e3eaea3SApple OSS Distributions // Send notification (via mach message) that data is now available.
306*5e3eaea3SApple OSS Distributions sendDataAvailableNotification();
307*5e3eaea3SApple OSS Distributions }
308*5e3eaea3SApple OSS Distributions return true;
309*5e3eaea3SApple OSS Distributions }
310*5e3eaea3SApple OSS Distributions
311*5e3eaea3SApple OSS Distributions Boolean
dequeue(void * data,UInt32 * dataSize)312*5e3eaea3SApple OSS Distributions IOSharedDataQueue::dequeue(void *data, UInt32 *dataSize)
313*5e3eaea3SApple OSS Distributions {
314*5e3eaea3SApple OSS Distributions Boolean retVal = TRUE;
315*5e3eaea3SApple OSS Distributions volatile IODataQueueEntry * entry = NULL;
316*5e3eaea3SApple OSS Distributions UInt32 entrySize = 0;
317*5e3eaea3SApple OSS Distributions UInt32 headOffset = 0;
318*5e3eaea3SApple OSS Distributions UInt32 tailOffset = 0;
319*5e3eaea3SApple OSS Distributions UInt32 newHeadOffset = 0;
320*5e3eaea3SApple OSS Distributions
321*5e3eaea3SApple OSS Distributions if (!dataQueue || (data && !dataSize)) {
322*5e3eaea3SApple OSS Distributions return false;
323*5e3eaea3SApple OSS Distributions }
324*5e3eaea3SApple OSS Distributions
325*5e3eaea3SApple OSS Distributions // Read head and tail with acquire barrier
326*5e3eaea3SApple OSS Distributions // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
327*5e3eaea3SApple OSS Distributions headOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
328*5e3eaea3SApple OSS Distributions tailOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_ACQUIRE);
329*5e3eaea3SApple OSS Distributions
330*5e3eaea3SApple OSS Distributions if (headOffset != tailOffset) {
331*5e3eaea3SApple OSS Distributions volatile IODataQueueEntry * head = NULL;
332*5e3eaea3SApple OSS Distributions UInt32 headSize = 0;
333*5e3eaea3SApple OSS Distributions UInt32 queueSize = getQueueSize();
334*5e3eaea3SApple OSS Distributions
335*5e3eaea3SApple OSS Distributions if (headOffset > queueSize) {
336*5e3eaea3SApple OSS Distributions return false;
337*5e3eaea3SApple OSS Distributions }
338*5e3eaea3SApple OSS Distributions
339*5e3eaea3SApple OSS Distributions head = (IODataQueueEntry *)((char *)dataQueue->queue + headOffset);
340*5e3eaea3SApple OSS Distributions headSize = head->size;
341*5e3eaea3SApple OSS Distributions
342*5e3eaea3SApple OSS Distributions // we wrapped around to beginning, so read from there
343*5e3eaea3SApple OSS Distributions // either there was not even room for the header
344*5e3eaea3SApple OSS Distributions if ((headOffset > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
345*5e3eaea3SApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize) ||
346*5e3eaea3SApple OSS Distributions // or there was room for the header, but not for the data
347*5e3eaea3SApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headSize) ||
348*5e3eaea3SApple OSS Distributions (headOffset + headSize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
349*5e3eaea3SApple OSS Distributions // Note: we have to wrap to the beginning even with the UINT32_MAX checks
350*5e3eaea3SApple OSS Distributions // because we have to support a queueSize of UINT32_MAX.
351*5e3eaea3SApple OSS Distributions entry = dataQueue->queue;
352*5e3eaea3SApple OSS Distributions entrySize = entry->size;
353*5e3eaea3SApple OSS Distributions if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
354*5e3eaea3SApple OSS Distributions (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
355*5e3eaea3SApple OSS Distributions return false;
356*5e3eaea3SApple OSS Distributions }
357*5e3eaea3SApple OSS Distributions newHeadOffset = entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE;
358*5e3eaea3SApple OSS Distributions // else it is at the end
359*5e3eaea3SApple OSS Distributions } else {
360*5e3eaea3SApple OSS Distributions entry = head;
361*5e3eaea3SApple OSS Distributions entrySize = entry->size;
362*5e3eaea3SApple OSS Distributions if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
363*5e3eaea3SApple OSS Distributions (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headOffset) ||
364*5e3eaea3SApple OSS Distributions (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE + headOffset > queueSize)) {
365*5e3eaea3SApple OSS Distributions return false;
366*5e3eaea3SApple OSS Distributions }
367*5e3eaea3SApple OSS Distributions newHeadOffset = headOffset + entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE;
368*5e3eaea3SApple OSS Distributions }
369*5e3eaea3SApple OSS Distributions } else {
370*5e3eaea3SApple OSS Distributions // empty queue
371*5e3eaea3SApple OSS Distributions return false;
372*5e3eaea3SApple OSS Distributions }
373*5e3eaea3SApple OSS Distributions
374*5e3eaea3SApple OSS Distributions if (data) {
375*5e3eaea3SApple OSS Distributions if (entrySize > *dataSize) {
376*5e3eaea3SApple OSS Distributions // not enough space
377*5e3eaea3SApple OSS Distributions return false;
378*5e3eaea3SApple OSS Distributions }
379*5e3eaea3SApple OSS Distributions __nochk_memcpy(data, (void *)entry->data, entrySize);
380*5e3eaea3SApple OSS Distributions *dataSize = entrySize;
381*5e3eaea3SApple OSS Distributions }
382*5e3eaea3SApple OSS Distributions
383*5e3eaea3SApple OSS Distributions __c11_atomic_store((_Atomic UInt32 *)&dataQueue->head, newHeadOffset, __ATOMIC_RELEASE);
384*5e3eaea3SApple OSS Distributions
385*5e3eaea3SApple OSS Distributions if (newHeadOffset == tailOffset) {
386*5e3eaea3SApple OSS Distributions //
387*5e3eaea3SApple OSS Distributions // If we are making the queue empty, then we need to make sure
388*5e3eaea3SApple OSS Distributions // that either the enqueuer notices, or we notice the enqueue
389*5e3eaea3SApple OSS Distributions // that raced with our making of the queue empty.
390*5e3eaea3SApple OSS Distributions //
391*5e3eaea3SApple OSS Distributions __c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
392*5e3eaea3SApple OSS Distributions }
393*5e3eaea3SApple OSS Distributions
394*5e3eaea3SApple OSS Distributions return retVal;
395*5e3eaea3SApple OSS Distributions }
396*5e3eaea3SApple OSS Distributions
397*5e3eaea3SApple OSS Distributions UInt32
getQueueSize()398*5e3eaea3SApple OSS Distributions IOSharedDataQueue::getQueueSize()
399*5e3eaea3SApple OSS Distributions {
400*5e3eaea3SApple OSS Distributions if (!_reserved) {
401*5e3eaea3SApple OSS Distributions return 0;
402*5e3eaea3SApple OSS Distributions }
403*5e3eaea3SApple OSS Distributions return _reserved->queueSize;
404*5e3eaea3SApple OSS Distributions }
405*5e3eaea3SApple OSS Distributions
406*5e3eaea3SApple OSS Distributions Boolean
setQueueSize(UInt32 size)407*5e3eaea3SApple OSS Distributions IOSharedDataQueue::setQueueSize(UInt32 size)
408*5e3eaea3SApple OSS Distributions {
409*5e3eaea3SApple OSS Distributions if (!_reserved) {
410*5e3eaea3SApple OSS Distributions return false;
411*5e3eaea3SApple OSS Distributions }
412*5e3eaea3SApple OSS Distributions _reserved->queueSize = size;
413*5e3eaea3SApple OSS Distributions return true;
414*5e3eaea3SApple OSS Distributions }
415*5e3eaea3SApple OSS Distributions
416*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 0);
417*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 1);
418*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 2);
419*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 3);
420*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 4);
421*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 5);
422*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 6);
423*5e3eaea3SApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 7);
424