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