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