xref: /xnu-11215/iokit/Kernel/IODataQueue.cpp (revision 8d741a5d)
1c1dac77fSApple OSS Distributions /*
2c1dac77fSApple OSS Distributions  * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3c1dac77fSApple OSS Distributions  *
4e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5c1dac77fSApple 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.
14c1dac77fSApple 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
20c1dac77fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21c1dac77fSApple 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.
25c1dac77fSApple OSS Distributions  *
26e13b1fa5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27c1dac77fSApple OSS Distributions  */
28c1dac77fSApple OSS Distributions 
29bb611c8fSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30bb611c8fSApple OSS Distributions 
31a3bb9fccSApple OSS Distributions #define DISABLE_DATAQUEUE_WARNING
32a3bb9fccSApple OSS Distributions 
33c1dac77fSApple OSS Distributions #include <IOKit/IODataQueue.h>
34a3bb9fccSApple OSS Distributions 
35a3bb9fccSApple OSS Distributions #undef DISABLE_DATAQUEUE_WARNING
36*8d741a5dSApple OSS Distributions #include <vm/vm_kern_xnu.h>
37a3bb9fccSApple OSS Distributions 
38c1dac77fSApple OSS Distributions #include <IOKit/IODataQueueShared.h>
39c1dac77fSApple OSS Distributions #include <IOKit/IOLib.h>
40c1dac77fSApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
41a3bb9fccSApple OSS Distributions #include <libkern/OSAtomic.h>
42bb611c8fSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
43c1dac77fSApple OSS Distributions 
44a5e72196SApple OSS Distributions struct IODataQueueInternal {
450f3703acSApple OSS Distributions 	mach_msg_header_t msg;
460f3703acSApple OSS Distributions 	UInt32            queueSize;
470f3703acSApple OSS Distributions };
480f3703acSApple OSS Distributions 
49c1dac77fSApple OSS Distributions #ifdef enqueue
50c1dac77fSApple OSS Distributions #undef enqueue
51c1dac77fSApple OSS Distributions #endif
52c1dac77fSApple OSS Distributions 
53c1dac77fSApple OSS Distributions #ifdef dequeue
54c1dac77fSApple OSS Distributions #undef dequeue
55c1dac77fSApple OSS Distributions #endif
56c1dac77fSApple OSS Distributions 
57c1dac77fSApple OSS Distributions #define super OSObject
58c1dac77fSApple OSS Distributions 
OSDefineMetaClassAndStructors(IODataQueue,OSObject)59c1dac77fSApple OSS Distributions OSDefineMetaClassAndStructors(IODataQueue, OSObject)
60c1dac77fSApple OSS Distributions 
61bb611c8fSApple OSS Distributions OSSharedPtr<IODataQueue>
62bb611c8fSApple OSS Distributions IODataQueue::withCapacity(UInt32 size)
63c1dac77fSApple OSS Distributions {
64bb611c8fSApple OSS Distributions 	OSSharedPtr<IODataQueue> dataQueue = OSMakeShared<IODataQueue>();
65c1dac77fSApple OSS Distributions 
66c1dac77fSApple OSS Distributions 	if (dataQueue) {
67c1dac77fSApple OSS Distributions 		if (!dataQueue->initWithCapacity(size)) {
68bb611c8fSApple OSS Distributions 			return nullptr;
69c1dac77fSApple OSS Distributions 		}
70c1dac77fSApple OSS Distributions 	}
71c1dac77fSApple OSS Distributions 
72c1dac77fSApple OSS Distributions 	return dataQueue;
73c1dac77fSApple OSS Distributions }
74c1dac77fSApple OSS Distributions 
75bb611c8fSApple OSS Distributions OSSharedPtr<IODataQueue>
withEntries(UInt32 numEntries,UInt32 entrySize)76a5e72196SApple OSS Distributions IODataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
77c1dac77fSApple OSS Distributions {
78bb611c8fSApple OSS Distributions 	OSSharedPtr<IODataQueue> dataQueue = OSMakeShared<IODataQueue>();
79c1dac77fSApple OSS Distributions 
80c1dac77fSApple OSS Distributions 	if (dataQueue) {
81c1dac77fSApple OSS Distributions 		if (!dataQueue->initWithEntries(numEntries, entrySize)) {
82bb611c8fSApple OSS Distributions 			return nullptr;
83c1dac77fSApple OSS Distributions 		}
84c1dac77fSApple OSS Distributions 	}
85c1dac77fSApple OSS Distributions 
86c1dac77fSApple OSS Distributions 	return dataQueue;
87c1dac77fSApple OSS Distributions }
88c1dac77fSApple OSS Distributions 
89a5e72196SApple OSS Distributions Boolean
initWithCapacity(UInt32 size)90a5e72196SApple OSS Distributions IODataQueue::initWithCapacity(UInt32 size)
91c1dac77fSApple OSS Distributions {
92d0c1fef6SApple OSS Distributions 	vm_size_t allocSize = 0;
935c2921b0SApple OSS Distributions 	kern_return_t kr;
94d0c1fef6SApple OSS Distributions 
95c1dac77fSApple OSS Distributions 	if (!super::init()) {
96c1dac77fSApple OSS Distributions 		return false;
97c1dac77fSApple OSS Distributions 	}
98c1dac77fSApple OSS Distributions 
99a3bb9fccSApple OSS Distributions 	if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE) {
100a3bb9fccSApple OSS Distributions 		return false;
101a3bb9fccSApple OSS Distributions 	}
102a3bb9fccSApple OSS Distributions 
103d0c1fef6SApple OSS Distributions 	allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE);
104d0c1fef6SApple OSS Distributions 
105d0c1fef6SApple OSS Distributions 	if (allocSize < size) {
106d0c1fef6SApple OSS Distributions 		return false;
107d0c1fef6SApple OSS Distributions 	}
108d0c1fef6SApple OSS Distributions 
1090f3703acSApple OSS Distributions 	assert(!notifyMsg);
110e6231be0SApple OSS Distributions 	notifyMsg = IOMallocType(IODataQueueInternal);
1110f3703acSApple OSS Distributions 	((IODataQueueInternal *)notifyMsg)->queueSize = size;
1120f3703acSApple OSS Distributions 
1135c2921b0SApple OSS Distributions 	kr = kmem_alloc(kernel_map, (vm_offset_t *)&dataQueue, allocSize,
1145c2921b0SApple OSS Distributions 	    (kma_flags_t)(KMA_DATA | KMA_ZERO), IOMemoryTag(kernel_map));
1155c2921b0SApple OSS Distributions 	if (kr != KERN_SUCCESS) {
116c1dac77fSApple OSS Distributions 		return false;
117c1dac77fSApple OSS Distributions 	}
118c1dac77fSApple OSS Distributions 
119c1dac77fSApple OSS Distributions 	dataQueue->queueSize    = size;
1200f3703acSApple OSS Distributions //  dataQueue->head         = 0;
1210f3703acSApple OSS Distributions //  dataQueue->tail         = 0;
122a3bb9fccSApple OSS Distributions 
123c1dac77fSApple OSS Distributions 	return true;
124c1dac77fSApple OSS Distributions }
125c1dac77fSApple OSS Distributions 
126a5e72196SApple OSS Distributions Boolean
initWithEntries(UInt32 numEntries,UInt32 entrySize)127a5e72196SApple OSS Distributions IODataQueue::initWithEntries(UInt32 numEntries, UInt32 entrySize)
128c1dac77fSApple OSS Distributions {
129a3bb9fccSApple OSS Distributions 	// Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE):
130a3bb9fccSApple OSS Distributions 	//  check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
131a3bb9fccSApple OSS Distributions 	if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
132a3bb9fccSApple OSS Distributions 	    //  check (numEntries + 1)
133a3bb9fccSApple OSS Distributions 	    (numEntries > UINT32_MAX - 1) ||
134a3bb9fccSApple OSS Distributions 	    //  check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
135a3bb9fccSApple OSS Distributions 	    (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX / (numEntries + 1))) {
136a3bb9fccSApple OSS Distributions 		return false;
137a3bb9fccSApple OSS Distributions 	}
138a3bb9fccSApple OSS Distributions 
139a5e72196SApple OSS Distributions 	return initWithCapacity((numEntries + 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE + entrySize));
140c1dac77fSApple OSS Distributions }
141c1dac77fSApple OSS Distributions 
142a5e72196SApple OSS Distributions void
free()143a5e72196SApple OSS Distributions IODataQueue::free()
144c1dac77fSApple OSS Distributions {
145a3bb9fccSApple OSS Distributions 	if (notifyMsg) {
1460f3703acSApple OSS Distributions 		if (dataQueue) {
1475c2921b0SApple OSS Distributions 			kmem_free(kernel_map, (vm_offset_t)dataQueue,
1485c2921b0SApple OSS Distributions 			    round_page(((IODataQueueInternal *)notifyMsg)->queueSize +
1495c2921b0SApple OSS Distributions 			    DATA_QUEUE_MEMORY_HEADER_SIZE));
1500f3703acSApple OSS Distributions 			dataQueue = NULL;
151a3bb9fccSApple OSS Distributions 		}
1520f3703acSApple OSS Distributions 
153e6231be0SApple OSS Distributions 		IOFreeType(notifyMsg, IODataQueueInternal);
1540f3703acSApple OSS Distributions 		notifyMsg = NULL;
155c1dac77fSApple OSS Distributions 	}
156c1dac77fSApple OSS Distributions 
157c1dac77fSApple OSS Distributions 	super::free();
158c1dac77fSApple OSS Distributions 
159c1dac77fSApple OSS Distributions 	return;
160c1dac77fSApple OSS Distributions }
161c1dac77fSApple OSS Distributions 
162a5e72196SApple OSS Distributions Boolean
enqueue(void * data,UInt32 dataSize)163a5e72196SApple OSS Distributions IODataQueue::enqueue(void * data, UInt32 dataSize)
164c1dac77fSApple OSS Distributions {
16588cc0b97SApple OSS Distributions 	UInt32             head;
16688cc0b97SApple OSS Distributions 	UInt32             tail;
16788cc0b97SApple OSS Distributions 	UInt32             newTail;
168c1dac77fSApple OSS Distributions 	const UInt32       entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
1690f3703acSApple OSS Distributions 	UInt32             queueSize;
170c1dac77fSApple OSS Distributions 	IODataQueueEntry * entry;
171c1dac77fSApple OSS Distributions 
172a3bb9fccSApple OSS Distributions 	// Check for overflow of entrySize
173a3bb9fccSApple OSS Distributions 	if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) {
174a3bb9fccSApple OSS Distributions 		return false;
175a3bb9fccSApple OSS Distributions 	}
1760f3703acSApple OSS Distributions 
17788cc0b97SApple OSS Distributions 	// Force a single read of head and tail
178cc9a6355SApple OSS Distributions 	// See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
17988cc0b97SApple OSS Distributions 	tail = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_RELAXED);
180cc9a6355SApple OSS Distributions 	head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_ACQUIRE);
18188cc0b97SApple OSS Distributions 
182a3bb9fccSApple OSS Distributions 	// Check for underflow of (dataQueue->queueSize - tail)
1830f3703acSApple OSS Distributions 	queueSize = ((IODataQueueInternal *) notifyMsg)->queueSize;
1840f3703acSApple OSS Distributions 	if ((queueSize < tail) || (queueSize < head)) {
185a3bb9fccSApple OSS Distributions 		return false;
186a3bb9fccSApple OSS Distributions 	}
187a3bb9fccSApple OSS Distributions 
188a5e72196SApple OSS Distributions 	if (tail >= head) {
189fad439e7SApple OSS Distributions 		// Is there enough room at the end for the entry?
190a3bb9fccSApple OSS Distributions 		if ((entrySize <= UINT32_MAX - tail) &&
191a5e72196SApple OSS Distributions 		    ((tail + entrySize) <= queueSize)) {
192c1dac77fSApple OSS Distributions 			entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
193c1dac77fSApple OSS Distributions 
194c1dac77fSApple OSS Distributions 			entry->size = dataSize;
195a5e72196SApple OSS Distributions 			__nochk_memcpy(&entry->data, data, dataSize);
196fad439e7SApple OSS Distributions 
197fad439e7SApple OSS Distributions 			// The tail can be out of bound when the size of the new entry
198fad439e7SApple OSS Distributions 			// exactly matches the available space at the end of the queue.
199fad439e7SApple OSS Distributions 			// The tail can range from 0 to dataQueue->queueSize inclusive.
200fad439e7SApple OSS Distributions 
20188cc0b97SApple OSS Distributions 			newTail = tail + entrySize;
202a5e72196SApple OSS Distributions 		} else if (head > entrySize) { // Is there enough room at the beginning?
203c1dac77fSApple OSS Distributions 			// Wrap around to the beginning, but do not allow the tail to catch
204c1dac77fSApple OSS Distributions 			// up to the head.
205c1dac77fSApple OSS Distributions 
206c1dac77fSApple OSS Distributions 			dataQueue->queue->size = dataSize;
207fad439e7SApple OSS Distributions 
208fad439e7SApple OSS Distributions 			// We need to make sure that there is enough room to set the size before
209fad439e7SApple OSS Distributions 			// doing this. The user client checks for this and will look for the size
210fad439e7SApple OSS Distributions 			// at the beginning if there isn't room for it at the end.
211fad439e7SApple OSS Distributions 
212a5e72196SApple OSS Distributions 			if ((queueSize - tail) >= DATA_QUEUE_ENTRY_HEADER_SIZE) {
213c1dac77fSApple OSS Distributions 				((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
214fad439e7SApple OSS Distributions 			}
215fad439e7SApple OSS Distributions 
216a5e72196SApple OSS Distributions 			__nochk_memcpy(&dataQueue->queue->data, data, dataSize);
21788cc0b97SApple OSS Distributions 			newTail = entrySize;
218a5e72196SApple OSS Distributions 		} else {
219c1dac77fSApple OSS Distributions 			return false; // queue is full
220c1dac77fSApple OSS Distributions 		}
221a5e72196SApple OSS Distributions 	} else {
222c1dac77fSApple OSS Distributions 		// Do not allow the tail to catch up to the head when the queue is full.
223c1dac77fSApple OSS Distributions 		// That's why the comparison uses a '>' rather than '>='.
224c1dac77fSApple OSS Distributions 
225a5e72196SApple OSS Distributions 		if ((head - tail) > entrySize) {
226c1dac77fSApple OSS Distributions 			entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
227c1dac77fSApple OSS Distributions 
228c1dac77fSApple OSS Distributions 			entry->size = dataSize;
229a5e72196SApple OSS Distributions 			__nochk_memcpy(&entry->data, data, dataSize);
23088cc0b97SApple OSS Distributions 			newTail = tail + entrySize;
231a5e72196SApple OSS Distributions 		} else {
232c1dac77fSApple OSS Distributions 			return false; // queue is full
233c1dac77fSApple OSS Distributions 		}
234c1dac77fSApple OSS Distributions 	}
235c1dac77fSApple OSS Distributions 
236cc9a6355SApple OSS Distributions 	// Publish the data we just enqueued
23788cc0b97SApple OSS Distributions 	__c11_atomic_store((_Atomic UInt32 *)&dataQueue->tail, newTail, __ATOMIC_RELEASE);
23888cc0b97SApple OSS Distributions 
239cc9a6355SApple OSS Distributions 	if (tail != head) {
240cc9a6355SApple OSS Distributions 		//
241cc9a6355SApple OSS Distributions 		// The memory barrier below paris with the one in ::dequeue
242cc9a6355SApple OSS Distributions 		// so that either our store to the tail cannot be missed by
243cc9a6355SApple OSS Distributions 		// the next dequeue attempt, or we will observe the dequeuer
244cc9a6355SApple OSS Distributions 		// making the queue empty.
245cc9a6355SApple OSS Distributions 		//
246cc9a6355SApple OSS Distributions 		// Of course, if we already think the queue is empty,
247cc9a6355SApple OSS Distributions 		// there's no point paying this extra cost.
248cc9a6355SApple OSS Distributions 		//
249cc9a6355SApple OSS Distributions 		__c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
250cc9a6355SApple OSS Distributions 		head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
251c1dac77fSApple OSS Distributions 	}
252c1dac77fSApple OSS Distributions 
253cc9a6355SApple OSS Distributions 	if (tail == head) {
254cc9a6355SApple OSS Distributions 		// Send notification (via mach message) that data is now available.
255cc9a6355SApple OSS Distributions 		sendDataAvailableNotification();
256cc9a6355SApple OSS Distributions 	}
257c1dac77fSApple OSS Distributions 	return true;
258c1dac77fSApple OSS Distributions }
259c1dac77fSApple OSS Distributions 
260a5e72196SApple OSS Distributions void
setNotificationPort(mach_port_t port)261a5e72196SApple OSS Distributions IODataQueue::setNotificationPort(mach_port_t port)
262c1dac77fSApple OSS Distributions {
2630f3703acSApple OSS Distributions 	mach_msg_header_t * msgh;
264c1dac77fSApple OSS Distributions 
2650f3703acSApple OSS Distributions 	msgh = &((IODataQueueInternal *) notifyMsg)->msg;
266a3bb9fccSApple OSS Distributions 	bzero(msgh, sizeof(mach_msg_header_t));
267a3bb9fccSApple OSS Distributions 	msgh->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
268a3bb9fccSApple OSS Distributions 	msgh->msgh_size = sizeof(mach_msg_header_t);
269a3bb9fccSApple OSS Distributions 	msgh->msgh_remote_port = port;
270c1dac77fSApple OSS Distributions }
271c1dac77fSApple OSS Distributions 
272a5e72196SApple OSS Distributions void
sendDataAvailableNotification()273a5e72196SApple OSS Distributions IODataQueue::sendDataAvailableNotification()
274c1dac77fSApple OSS Distributions {
275c1dac77fSApple OSS Distributions 	kern_return_t       kr;
276c1dac77fSApple OSS Distributions 	mach_msg_header_t * msgh;
277c1dac77fSApple OSS Distributions 
2780f3703acSApple OSS Distributions 	msgh = &((IODataQueueInternal *) notifyMsg)->msg;
2790f3703acSApple OSS Distributions 	if (msgh->msgh_remote_port) {
280*8d741a5dSApple OSS Distributions 		kr = mach_msg_send_from_kernel_with_options(msgh, msgh->msgh_size,
281*8d741a5dSApple OSS Distributions 		    MACH64_SEND_TIMEOUT, MACH_MSG_TIMEOUT_NONE);
282c1dac77fSApple OSS Distributions 		switch (kr) {
283c1dac77fSApple OSS Distributions 		case MACH_SEND_TIMED_OUT: // Notification already sent
284c1dac77fSApple OSS Distributions 		case MACH_MSG_SUCCESS:
285186b8fceSApple OSS Distributions 		case MACH_SEND_NO_BUFFER:
286c1dac77fSApple OSS Distributions 			break;
287c1dac77fSApple OSS Distributions 		default:
288c1dac77fSApple OSS Distributions 			IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/ "IODataQueue", kr);
289c1dac77fSApple OSS Distributions 			break;
290c1dac77fSApple OSS Distributions 		}
291c1dac77fSApple OSS Distributions 	}
292c1dac77fSApple OSS Distributions }
293c1dac77fSApple OSS Distributions 
294bb611c8fSApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
getMemoryDescriptor()295a5e72196SApple OSS Distributions IODataQueue::getMemoryDescriptor()
296c1dac77fSApple OSS Distributions {
297bb611c8fSApple OSS Distributions 	OSSharedPtr<IOMemoryDescriptor> descriptor;
2980f3703acSApple OSS Distributions 	UInt32              queueSize;
299c1dac77fSApple OSS Distributions 
3000f3703acSApple OSS Distributions 	queueSize = ((IODataQueueInternal *) notifyMsg)->queueSize;
301a5e72196SApple OSS Distributions 	if (dataQueue != NULL) {
3020f3703acSApple OSS Distributions 		descriptor = IOMemoryDescriptor::withAddress(dataQueue, queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE, kIODirectionOutIn);
303c1dac77fSApple OSS Distributions 	}
304c1dac77fSApple OSS Distributions 
305c1dac77fSApple OSS Distributions 	return descriptor;
306c1dac77fSApple OSS Distributions }
307