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