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