]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IODataQueue.cpp
xnu-2782.10.72.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@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
fe8ab488
A
29#define DISABLE_DATAQUEUE_WARNING
30
1c79356b 31#include <IOKit/IODataQueue.h>
fe8ab488
A
32
33#undef DISABLE_DATAQUEUE_WARNING
34
1c79356b
A
35#include <IOKit/IODataQueueShared.h>
36#include <IOKit/IOLib.h>
37#include <IOKit/IOMemoryDescriptor.h>
143464d5 38#include <libkern/OSAtomic.h>
1c79356b
A
39
40#ifdef enqueue
41#undef enqueue
42#endif
43
44#ifdef dequeue
45#undef dequeue
46#endif
47
48#define super OSObject
49
50OSDefineMetaClassAndStructors(IODataQueue, OSObject)
51
52IODataQueue *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
66IODataQueue *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
80Boolean IODataQueue::initWithCapacity(UInt32 size)
81{
316670eb
A
82 vm_size_t allocSize = 0;
83
1c79356b
A
84 if (!super::init()) {
85 return false;
86 }
87
143464d5
A
88 if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE) {
89 return false;
90 }
91
316670eb
A
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);
1c79356b
A
99 if (dataQueue == 0) {
100 return false;
101 }
a1c7dba1 102 bzero(dataQueue, allocSize);
1c79356b 103
2d21ac55 104 dataQueue->queueSize = size;
a1c7dba1
A
105// dataQueue->head = 0;
106// dataQueue->tail = 0;
1c79356b 107
fe8ab488
A
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
1c79356b
A
115 return true;
116}
117
118Boolean IODataQueue::initWithEntries(UInt32 numEntries, UInt32 entrySize)
119{
143464d5
A
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
1c79356b
A
130 return (initWithCapacity((numEntries + 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE + entrySize)));
131}
132
133void IODataQueue::free()
134{
135 if (dataQueue) {
b0d623f7 136 IOFreeAligned(dataQueue, round_page(dataQueue->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE));
fe8ab488
A
137 dataQueue = NULL;
138
139 if (notifyMsg) {
140 IOFree(notifyMsg, sizeof(mach_msg_header_t));
141 notifyMsg = NULL;
142 }
1c79356b
A
143 }
144
145 super::free();
146
147 return;
148}
149
150Boolean 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;
9bccf70c 156
143464d5
A
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
1c79356b
A
166 if ( tail >= head )
167 {
9bccf70c 168 // Is there enough room at the end for the entry?
143464d5
A
169 if ((entrySize <= UINT32_MAX - tail) &&
170 ((tail + entrySize) <= dataQueue->queueSize) )
1c79356b
A
171 {
172 entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
173
174 entry->size = dataSize;
175 memcpy(&entry->data, data, dataSize);
9bccf70c
A
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.
143464d5
A
180
181 OSAddAtomic(entrySize, (SInt32 *)&dataQueue->tail);
1c79356b 182 }
fe8ab488 183 else if ( head > entrySize ) // Is there enough room at the beginning?
1c79356b
A
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;
9bccf70c
A
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
1c79356b 199 memcpy(&dataQueue->queue->data, data, dataSize);
143464d5 200 OSCompareAndSwap(dataQueue->tail, entrySize, &dataQueue->tail);
1c79356b
A
201 }
202 else
203 {
fe8ab488 204 return false; // queue is full
1c79356b
A
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);
143464d5 218 OSAddAtomic(entrySize, (SInt32 *)&dataQueue->tail);
1c79356b
A
219 }
220 else
221 {
fe8ab488 222 return false; // queue is full
1c79356b
A
223 }
224 }
225
226 // Send notification (via mach message) that data is available.
227
fe8ab488 228 if ( ( head == tail ) /* queue was empty prior to enqueue() */
1c79356b
A
229 || ( dataQueue->head == tail ) ) /* queue was emptied during enqueue() */
230 {
231 sendDataAvailableNotification();
232 }
233
234 return true;
235}
236
237void IODataQueue::setNotificationPort(mach_port_t port)
238{
fe8ab488 239 mach_msg_header_t * msgh = (mach_msg_header_t *) notifyMsg;
1c79356b 240
fe8ab488
A
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 }
1c79356b
A
247}
248
249void IODataQueue::sendDataAvailableNotification()
250{
fe8ab488
A
251 kern_return_t kr;
252 mach_msg_header_t * msgh;
1c79356b 253
fe8ab488 254 msgh = (mach_msg_header_t *) notifyMsg;
2d21ac55 255 if (msgh && msgh->msgh_remote_port) {
39236c6e 256 kr = mach_msg_send_from_kernel_with_options(msgh, msgh->msgh_size, MACH_SEND_TIMEOUT, MACH_MSG_TIMEOUT_NONE);
1c79356b 257 switch(kr) {
fe8ab488 258 case MACH_SEND_TIMED_OUT: // Notification already sent
1c79356b 259 case MACH_MSG_SUCCESS:
39236c6e 260 case MACH_SEND_NO_BUFFER:
1c79356b
A
261 break;
262 default:
263 IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr);
264 break;
265 }
266 }
267}
268
269IOMemoryDescriptor *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
fe8ab488 280