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