]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IODataQueue.cpp
79c97e1af1145073b5acdbfe01cdeb5eb94aed2c
[apple/xnu.git] / iokit / Kernel / IODataQueue.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #define DISABLE_DATAQUEUE_WARNING
30
31 #include <IOKit/IODataQueue.h>
32
33 #undef DISABLE_DATAQUEUE_WARNING
34
35 #include <IOKit/IODataQueueShared.h>
36 #include <IOKit/IOLib.h>
37 #include <IOKit/IOMemoryDescriptor.h>
38 #include <libkern/OSAtomic.h>
39
40 #ifdef enqueue
41 #undef enqueue
42 #endif
43
44 #ifdef dequeue
45 #undef dequeue
46 #endif
47
48 #define super OSObject
49
50 OSDefineMetaClassAndStructors(IODataQueue, OSObject)
51
52 IODataQueue *IODataQueue::withCapacity(UInt32 size)
53 {
54 IODataQueue *dataQueue = new IODataQueue;
55
56 if (dataQueue) {
57 if (!dataQueue->initWithCapacity(size)) {
58 dataQueue->release();
59 dataQueue = 0;
60 }
61 }
62
63 return dataQueue;
64 }
65
66 IODataQueue *IODataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
67 {
68 IODataQueue *dataQueue = new IODataQueue;
69
70 if (dataQueue) {
71 if (!dataQueue->initWithEntries(numEntries, entrySize)) {
72 dataQueue->release();
73 dataQueue = 0;
74 }
75 }
76
77 return dataQueue;
78 }
79
80 Boolean IODataQueue::initWithCapacity(UInt32 size)
81 {
82 vm_size_t allocSize = 0;
83
84 if (!super::init()) {
85 return false;
86 }
87
88 if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE) {
89 return false;
90 }
91
92 allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE);
93
94 if (allocSize < size) {
95 return false;
96 }
97
98 dataQueue = (IODataQueueMemory *)IOMallocAligned(allocSize, PAGE_SIZE);
99 if (dataQueue == 0) {
100 return false;
101 }
102 bzero(dataQueue, allocSize);
103
104 dataQueue->queueSize = size;
105 // dataQueue->head = 0;
106 // dataQueue->tail = 0;
107
108 if (!notifyMsg) {
109 notifyMsg = IOMalloc(sizeof(mach_msg_header_t));
110 if (!notifyMsg)
111 return false;
112 }
113 bzero(notifyMsg, sizeof(mach_msg_header_t));
114
115 return true;
116 }
117
118 Boolean IODataQueue::initWithEntries(UInt32 numEntries, UInt32 entrySize)
119 {
120 // Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE):
121 // check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
122 if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
123 // check (numEntries + 1)
124 (numEntries > UINT32_MAX-1) ||
125 // check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE)
126 (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX/(numEntries+1))) {
127 return false;
128 }
129
130 return (initWithCapacity((numEntries + 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE + entrySize)));
131 }
132
133 void IODataQueue::free()
134 {
135 if (dataQueue) {
136 IOFreeAligned(dataQueue, round_page(dataQueue->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE));
137 dataQueue = NULL;
138
139 if (notifyMsg) {
140 IOFree(notifyMsg, sizeof(mach_msg_header_t));
141 notifyMsg = NULL;
142 }
143 }
144
145 super::free();
146
147 return;
148 }
149
150 Boolean IODataQueue::enqueue(void * data, UInt32 dataSize)
151 {
152 const UInt32 head = dataQueue->head; // volatile
153 const UInt32 tail = dataQueue->tail;
154 const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
155 IODataQueueEntry * entry;
156
157 // Check for overflow of entrySize
158 if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) {
159 return false;
160 }
161 // Check for underflow of (dataQueue->queueSize - tail)
162 if (dataQueue->queueSize < tail) {
163 return false;
164 }
165
166 if ( tail >= head )
167 {
168 // Is there enough room at the end for the entry?
169 if ((entrySize <= UINT32_MAX - tail) &&
170 ((tail + entrySize) <= dataQueue->queueSize) )
171 {
172 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
173
174 entry->size = dataSize;
175 memcpy(&entry->data, data, dataSize);
176
177 // The tail can be out of bound when the size of the new entry
178 // exactly matches the available space at the end of the queue.
179 // The tail can range from 0 to dataQueue->queueSize inclusive.
180
181 OSAddAtomic(entrySize, (SInt32 *)&dataQueue->tail);
182 }
183 else if ( head > entrySize ) // Is there enough room at the beginning?
184 {
185 // Wrap around to the beginning, but do not allow the tail to catch
186 // up to the head.
187
188 dataQueue->queue->size = dataSize;
189
190 // We need to make sure that there is enough room to set the size before
191 // doing this. The user client checks for this and will look for the size
192 // at the beginning if there isn't room for it at the end.
193
194 if ( ( dataQueue->queueSize - tail ) >= DATA_QUEUE_ENTRY_HEADER_SIZE )
195 {
196 ((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
197 }
198
199 memcpy(&dataQueue->queue->data, data, dataSize);
200 OSCompareAndSwap(dataQueue->tail, entrySize, &dataQueue->tail);
201 }
202 else
203 {
204 return false; // queue is full
205 }
206 }
207 else
208 {
209 // Do not allow the tail to catch up to the head when the queue is full.
210 // That's why the comparison uses a '>' rather than '>='.
211
212 if ( (head - tail) > entrySize )
213 {
214 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
215
216 entry->size = dataSize;
217 memcpy(&entry->data, data, dataSize);
218 OSAddAtomic(entrySize, (SInt32 *)&dataQueue->tail);
219 }
220 else
221 {
222 return false; // queue is full
223 }
224 }
225
226 // Send notification (via mach message) that data is available.
227
228 if ( ( head == tail ) /* queue was empty prior to enqueue() */
229 || ( dataQueue->head == tail ) ) /* queue was emptied during enqueue() */
230 {
231 sendDataAvailableNotification();
232 }
233
234 return true;
235 }
236
237 void IODataQueue::setNotificationPort(mach_port_t port)
238 {
239 mach_msg_header_t * msgh = (mach_msg_header_t *) notifyMsg;
240
241 if (msgh) {
242 bzero(msgh, sizeof(mach_msg_header_t));
243 msgh->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
244 msgh->msgh_size = sizeof(mach_msg_header_t);
245 msgh->msgh_remote_port = port;
246 }
247 }
248
249 void IODataQueue::sendDataAvailableNotification()
250 {
251 kern_return_t kr;
252 mach_msg_header_t * msgh;
253
254 msgh = (mach_msg_header_t *) notifyMsg;
255 if (msgh && msgh->msgh_remote_port) {
256 kr = mach_msg_send_from_kernel_with_options(msgh, msgh->msgh_size, MACH_SEND_TIMEOUT, MACH_MSG_TIMEOUT_NONE);
257 switch(kr) {
258 case MACH_SEND_TIMED_OUT: // Notification already sent
259 case MACH_MSG_SUCCESS:
260 case MACH_SEND_NO_BUFFER:
261 break;
262 default:
263 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr);
264 break;
265 }
266 }
267 }
268
269 IOMemoryDescriptor *IODataQueue::getMemoryDescriptor()
270 {
271 IOMemoryDescriptor *descriptor = 0;
272
273 if (dataQueue != 0) {
274 descriptor = IOMemoryDescriptor::withAddress(dataQueue, dataQueue->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE, kIODirectionOutIn);
275 }
276
277 return descriptor;
278 }
279
280