]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IODataQueue.cpp
xnu-7195.101.1.tar.gz
[apple/xnu.git] / iokit / Kernel / IODataQueue.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
f427ee49
A
29#define IOKIT_ENABLE_SHARED_PTR
30
fe8ab488
A
31#define DISABLE_DATAQUEUE_WARNING
32
1c79356b 33#include <IOKit/IODataQueue.h>
fe8ab488
A
34
35#undef DISABLE_DATAQUEUE_WARNING
36
1c79356b
A
37#include <IOKit/IODataQueueShared.h>
38#include <IOKit/IOLib.h>
39#include <IOKit/IOMemoryDescriptor.h>
143464d5 40#include <libkern/OSAtomic.h>
f427ee49 41#include <libkern/c++/OSSharedPtr.h>
1c79356b 42
0a7de745
A
43struct IODataQueueInternal {
44 mach_msg_header_t msg;
45 UInt32 queueSize;
3e170ce0
A
46};
47
1c79356b
A
48#ifdef enqueue
49#undef enqueue
50#endif
51
52#ifdef dequeue
53#undef dequeue
54#endif
55
56#define super OSObject
57
58OSDefineMetaClassAndStructors(IODataQueue, OSObject)
59
f427ee49
A
60OSSharedPtr<IODataQueue>
61IODataQueue::withCapacity(UInt32 size)
1c79356b 62{
f427ee49 63 OSSharedPtr<IODataQueue> dataQueue = OSMakeShared<IODataQueue>();
1c79356b 64
0a7de745
A
65 if (dataQueue) {
66 if (!dataQueue->initWithCapacity(size)) {
f427ee49 67 return nullptr;
0a7de745
A
68 }
69 }
1c79356b 70
0a7de745 71 return dataQueue;
1c79356b
A
72}
73
f427ee49 74OSSharedPtr<IODataQueue>
0a7de745 75IODataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
1c79356b 76{
f427ee49 77 OSSharedPtr<IODataQueue> dataQueue = OSMakeShared<IODataQueue>();
1c79356b 78
0a7de745
A
79 if (dataQueue) {
80 if (!dataQueue->initWithEntries(numEntries, entrySize)) {
f427ee49 81 return nullptr;
0a7de745
A
82 }
83 }
1c79356b 84
0a7de745 85 return dataQueue;
1c79356b
A
86}
87
0a7de745
A
88Boolean
89IODataQueue::initWithCapacity(UInt32 size)
1c79356b 90{
0a7de745 91 vm_size_t allocSize = 0;
316670eb 92
0a7de745
A
93 if (!super::init()) {
94 return false;
95 }
1c79356b 96
0a7de745
A
97 if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE) {
98 return false;
99 }
100
101 allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE);
316670eb 102
0a7de745
A
103 if (allocSize < size) {
104 return false;
105 }
316670eb 106
0a7de745
A
107 assert(!notifyMsg);
108 notifyMsg = IONew(IODataQueueInternal, 1);
3e170ce0
A
109 if (!notifyMsg) {
110 return false;
111 }
0a7de745
A
112 bzero(notifyMsg, sizeof(IODataQueueInternal));
113 ((IODataQueueInternal *)notifyMsg)->queueSize = size;
3e170ce0 114
0a7de745 115 dataQueue = (IODataQueueMemory *)IOMallocAligned(allocSize, PAGE_SIZE);
cb323159 116 if (dataQueue == NULL) {
0a7de745
A
117 return false;
118 }
119 bzero(dataQueue, allocSize);
1c79356b 120
0a7de745 121 dataQueue->queueSize = size;
a1c7dba1
A
122// dataQueue->head = 0;
123// dataQueue->tail = 0;
1c79356b 124
0a7de745 125 return true;
1c79356b
A
126}
127
0a7de745
A
128Boolean
129IODataQueue::initWithEntries(UInt32 numEntries, UInt32 entrySize)
1c79356b 130{
0a7de745
A
131 // Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE):
132 // check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
133 if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
134 // check (numEntries + 1)
135 (numEntries > UINT32_MAX - 1) ||
136 // check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
137 (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX / (numEntries + 1))) {
138 return false;
139 }
140
141 return initWithCapacity((numEntries + 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE + entrySize));
1c79356b
A
142}
143
0a7de745
A
144void
145IODataQueue::free()
1c79356b 146{
3e170ce0
A
147 if (notifyMsg) {
148 if (dataQueue) {
149 IOFreeAligned(dataQueue, round_page(((IODataQueueInternal *)notifyMsg)->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE));
150 dataQueue = NULL;
151 }
152
153 IODelete(notifyMsg, IODataQueueInternal, 1);
154 notifyMsg = NULL;
0a7de745 155 }
1c79356b 156
0a7de745 157 super::free();
1c79356b 158
0a7de745 159 return;
1c79356b
A
160}
161
0a7de745
A
162Boolean
163IODataQueue::enqueue(void * data, UInt32 dataSize)
1c79356b 164{
0a7de745
A
165 UInt32 head;
166 UInt32 tail;
167 UInt32 newTail;
168 const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
169 UInt32 queueSize;
170 IODataQueueEntry * entry;
171
172 // Check for overflow of entrySize
173 if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) {
174 return false;
175 }
176
177 // Force a single read of head and tail
178 // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
179 tail = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_RELAXED);
180 head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_ACQUIRE);
181
182 // Check for underflow of (dataQueue->queueSize - tail)
183 queueSize = ((IODataQueueInternal *) notifyMsg)->queueSize;
184 if ((queueSize < tail) || (queueSize < head)) {
185 return false;
186 }
187
188 if (tail >= head) {
189 // Is there enough room at the end for the entry?
190 if ((entrySize <= UINT32_MAX - tail) &&
191 ((tail + entrySize) <= queueSize)) {
192 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
193
194 entry->size = dataSize;
cb323159 195 __nochk_memcpy(&entry->data, data, dataSize);
0a7de745
A
196
197 // The tail can be out of bound when the size of the new entry
198 // exactly matches the available space at the end of the queue.
199 // The tail can range from 0 to dataQueue->queueSize inclusive.
200
201 newTail = tail + entrySize;
202 } else if (head > entrySize) { // Is there enough room at the beginning?
203 // Wrap around to the beginning, but do not allow the tail to catch
204 // up to the head.
205
206 dataQueue->queue->size = dataSize;
207
208 // We need to make sure that there is enough room to set the size before
209 // doing this. The user client checks for this and will look for the size
210 // at the beginning if there isn't room for it at the end.
211
212 if ((queueSize - tail) >= DATA_QUEUE_ENTRY_HEADER_SIZE) {
213 ((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
214 }
215
cb323159 216 __nochk_memcpy(&dataQueue->queue->data, data, dataSize);
0a7de745
A
217 newTail = entrySize;
218 } else {
219 return false; // queue is full
220 }
221 } else {
222 // Do not allow the tail to catch up to the head when the queue is full.
223 // That's why the comparison uses a '>' rather than '>='.
224
225 if ((head - tail) > entrySize) {
226 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
227
228 entry->size = dataSize;
cb323159 229 __nochk_memcpy(&entry->data, data, dataSize);
0a7de745
A
230 newTail = tail + entrySize;
231 } else {
232 return false; // queue is full
233 }
234 }
1c79356b 235
d9a64523
A
236 // Publish the data we just enqueued
237 __c11_atomic_store((_Atomic UInt32 *)&dataQueue->tail, newTail, __ATOMIC_RELEASE);
238
239 if (tail != head) {
240 //
241 // The memory barrier below paris with the one in ::dequeue
242 // so that either our store to the tail cannot be missed by
243 // the next dequeue attempt, or we will observe the dequeuer
244 // making the queue empty.
245 //
246 // Of course, if we already think the queue is empty,
247 // there's no point paying this extra cost.
248 //
249 __c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
250 head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
251 }
1c79356b 252
d9a64523
A
253 if (tail == head) {
254 // Send notification (via mach message) that data is now available.
255 sendDataAvailableNotification();
256 }
257 return true;
1c79356b
A
258}
259
0a7de745
A
260void
261IODataQueue::setNotificationPort(mach_port_t port)
1c79356b 262{
0a7de745 263 mach_msg_header_t * msgh;
1c79356b 264
0a7de745 265 msgh = &((IODataQueueInternal *) notifyMsg)->msg;
3e170ce0
A
266 bzero(msgh, sizeof(mach_msg_header_t));
267 msgh->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
268 msgh->msgh_size = sizeof(mach_msg_header_t);
269 msgh->msgh_remote_port = port;
1c79356b
A
270}
271
0a7de745
A
272void
273IODataQueue::sendDataAvailableNotification()
1c79356b 274{
0a7de745
A
275 kern_return_t kr;
276 mach_msg_header_t * msgh;
277
278 msgh = &((IODataQueueInternal *) notifyMsg)->msg;
279 if (msgh->msgh_remote_port) {
280 kr = mach_msg_send_from_kernel_with_options(msgh, msgh->msgh_size, MACH_SEND_TIMEOUT, MACH_MSG_TIMEOUT_NONE);
281 switch (kr) {
282 case MACH_SEND_TIMED_OUT: // Notification already sent
283 case MACH_MSG_SUCCESS:
284 case MACH_SEND_NO_BUFFER:
285 break;
286 default:
287 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/ "IODataQueue", kr);
288 break;
289 }
290 }
1c79356b
A
291}
292
f427ee49 293OSSharedPtr<IOMemoryDescriptor>
0a7de745 294IODataQueue::getMemoryDescriptor()
1c79356b 295{
f427ee49 296 OSSharedPtr<IOMemoryDescriptor> descriptor;
0a7de745 297 UInt32 queueSize;
1c79356b 298
0a7de745 299 queueSize = ((IODataQueueInternal *) notifyMsg)->queueSize;
cb323159 300 if (dataQueue != NULL) {
0a7de745
A
301 descriptor = IOMemoryDescriptor::withAddress(dataQueue, queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE, kIODirectionOutIn);
302 }
1c79356b 303
0a7de745 304 return descriptor;
1c79356b 305}