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