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