]>
Commit | Line | Data |
---|---|---|
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 | 39 | |
3e170ce0 A |
40 | struct IODataQueueInternal |
41 | { | |
42 | mach_msg_header_t msg; | |
43 | UInt32 queueSize; | |
44 | }; | |
45 | ||
1c79356b A |
46 | #ifdef enqueue |
47 | #undef enqueue | |
48 | #endif | |
49 | ||
50 | #ifdef dequeue | |
51 | #undef dequeue | |
52 | #endif | |
53 | ||
54 | #define super OSObject | |
55 | ||
56 | OSDefineMetaClassAndStructors(IODataQueue, OSObject) | |
57 | ||
58 | IODataQueue *IODataQueue::withCapacity(UInt32 size) | |
59 | { | |
60 | IODataQueue *dataQueue = new IODataQueue; | |
61 | ||
62 | if (dataQueue) { | |
63 | if (!dataQueue->initWithCapacity(size)) { | |
64 | dataQueue->release(); | |
65 | dataQueue = 0; | |
66 | } | |
67 | } | |
68 | ||
69 | return dataQueue; | |
70 | } | |
71 | ||
72 | IODataQueue *IODataQueue::withEntries(UInt32 numEntries, UInt32 entrySize) | |
73 | { | |
74 | IODataQueue *dataQueue = new IODataQueue; | |
75 | ||
76 | if (dataQueue) { | |
77 | if (!dataQueue->initWithEntries(numEntries, entrySize)) { | |
78 | dataQueue->release(); | |
79 | dataQueue = 0; | |
80 | } | |
81 | } | |
82 | ||
83 | return dataQueue; | |
84 | } | |
85 | ||
86 | Boolean IODataQueue::initWithCapacity(UInt32 size) | |
87 | { | |
316670eb A |
88 | vm_size_t allocSize = 0; |
89 | ||
1c79356b A |
90 | if (!super::init()) { |
91 | return false; | |
92 | } | |
93 | ||
143464d5 A |
94 | if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE) { |
95 | return false; | |
96 | } | |
97 | ||
316670eb A |
98 | allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE); |
99 | ||
100 | if (allocSize < size) { | |
101 | return false; | |
102 | } | |
103 | ||
3e170ce0 A |
104 | assert(!notifyMsg); |
105 | notifyMsg = IONew(IODataQueueInternal, 1); | |
106 | if (!notifyMsg) { | |
107 | return false; | |
108 | } | |
109 | bzero(notifyMsg, sizeof(IODataQueueInternal)); | |
110 | ((IODataQueueInternal *)notifyMsg)->queueSize = size; | |
111 | ||
316670eb | 112 | dataQueue = (IODataQueueMemory *)IOMallocAligned(allocSize, PAGE_SIZE); |
1c79356b A |
113 | if (dataQueue == 0) { |
114 | return false; | |
115 | } | |
a1c7dba1 | 116 | bzero(dataQueue, allocSize); |
1c79356b | 117 | |
2d21ac55 | 118 | dataQueue->queueSize = size; |
a1c7dba1 A |
119 | // dataQueue->head = 0; |
120 | // dataQueue->tail = 0; | |
1c79356b A |
121 | |
122 | return true; | |
123 | } | |
124 | ||
125 | Boolean IODataQueue::initWithEntries(UInt32 numEntries, UInt32 entrySize) | |
126 | { | |
143464d5 A |
127 | // Checking overflow for (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE): |
128 | // check (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE) | |
129 | if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) || | |
130 | // check (numEntries + 1) | |
131 | (numEntries > UINT32_MAX-1) || | |
132 | // check (numEntries + 1)*(entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE) | |
133 | (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX/(numEntries+1))) { | |
134 | return false; | |
135 | } | |
136 | ||
1c79356b A |
137 | return (initWithCapacity((numEntries + 1) * (DATA_QUEUE_ENTRY_HEADER_SIZE + entrySize))); |
138 | } | |
139 | ||
140 | void IODataQueue::free() | |
141 | { | |
3e170ce0 A |
142 | if (notifyMsg) { |
143 | if (dataQueue) { | |
144 | IOFreeAligned(dataQueue, round_page(((IODataQueueInternal *)notifyMsg)->queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE)); | |
145 | dataQueue = NULL; | |
146 | } | |
147 | ||
148 | IODelete(notifyMsg, IODataQueueInternal, 1); | |
149 | notifyMsg = NULL; | |
1c79356b A |
150 | } |
151 | ||
152 | super::free(); | |
153 | ||
154 | return; | |
155 | } | |
156 | ||
157 | Boolean IODataQueue::enqueue(void * data, UInt32 dataSize) | |
158 | { | |
159 | const UInt32 head = dataQueue->head; // volatile | |
160 | const UInt32 tail = dataQueue->tail; | |
161 | const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE; | |
3e170ce0 | 162 | UInt32 queueSize; |
1c79356b | 163 | IODataQueueEntry * entry; |
9bccf70c | 164 | |
143464d5 A |
165 | // Check for overflow of entrySize |
166 | if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) { | |
167 | return false; | |
168 | } | |
3e170ce0 | 169 | |
143464d5 | 170 | // Check for underflow of (dataQueue->queueSize - tail) |
3e170ce0 A |
171 | queueSize = ((IODataQueueInternal *) notifyMsg)->queueSize; |
172 | if ((queueSize < tail) || (queueSize < head)) { | |
143464d5 A |
173 | return false; |
174 | } | |
175 | ||
1c79356b A |
176 | if ( tail >= head ) |
177 | { | |
9bccf70c | 178 | // Is there enough room at the end for the entry? |
143464d5 | 179 | if ((entrySize <= UINT32_MAX - tail) && |
3e170ce0 | 180 | ((tail + entrySize) <= queueSize) ) |
1c79356b A |
181 | { |
182 | entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail); | |
183 | ||
184 | entry->size = dataSize; | |
185 | memcpy(&entry->data, data, dataSize); | |
9bccf70c A |
186 | |
187 | // The tail can be out of bound when the size of the new entry | |
188 | // exactly matches the available space at the end of the queue. | |
189 | // The tail can range from 0 to dataQueue->queueSize inclusive. | |
143464d5 A |
190 | |
191 | OSAddAtomic(entrySize, (SInt32 *)&dataQueue->tail); | |
1c79356b | 192 | } |
fe8ab488 | 193 | else if ( head > entrySize ) // Is there enough room at the beginning? |
1c79356b A |
194 | { |
195 | // Wrap around to the beginning, but do not allow the tail to catch | |
196 | // up to the head. | |
197 | ||
198 | dataQueue->queue->size = dataSize; | |
9bccf70c A |
199 | |
200 | // We need to make sure that there is enough room to set the size before | |
201 | // doing this. The user client checks for this and will look for the size | |
202 | // at the beginning if there isn't room for it at the end. | |
203 | ||
3e170ce0 | 204 | if ( ( queueSize - tail ) >= DATA_QUEUE_ENTRY_HEADER_SIZE ) |
9bccf70c A |
205 | { |
206 | ((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize; | |
207 | } | |
208 | ||
1c79356b | 209 | memcpy(&dataQueue->queue->data, data, dataSize); |
143464d5 | 210 | OSCompareAndSwap(dataQueue->tail, entrySize, &dataQueue->tail); |
1c79356b A |
211 | } |
212 | else | |
213 | { | |
fe8ab488 | 214 | return false; // queue is full |
1c79356b A |
215 | } |
216 | } | |
217 | else | |
218 | { | |
219 | // Do not allow the tail to catch up to the head when the queue is full. | |
220 | // That's why the comparison uses a '>' rather than '>='. | |
221 | ||
222 | if ( (head - tail) > entrySize ) | |
223 | { | |
224 | entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail); | |
225 | ||
226 | entry->size = dataSize; | |
227 | memcpy(&entry->data, data, dataSize); | |
143464d5 | 228 | OSAddAtomic(entrySize, (SInt32 *)&dataQueue->tail); |
1c79356b A |
229 | } |
230 | else | |
231 | { | |
fe8ab488 | 232 | return false; // queue is full |
1c79356b A |
233 | } |
234 | } | |
235 | ||
236 | // Send notification (via mach message) that data is available. | |
237 | ||
fe8ab488 | 238 | if ( ( head == tail ) /* queue was empty prior to enqueue() */ |
1c79356b A |
239 | || ( dataQueue->head == tail ) ) /* queue was emptied during enqueue() */ |
240 | { | |
241 | sendDataAvailableNotification(); | |
242 | } | |
243 | ||
244 | return true; | |
245 | } | |
246 | ||
247 | void IODataQueue::setNotificationPort(mach_port_t port) | |
248 | { | |
3e170ce0 | 249 | mach_msg_header_t * msgh; |
1c79356b | 250 | |
3e170ce0 A |
251 | msgh = &((IODataQueueInternal *) notifyMsg)->msg; |
252 | bzero(msgh, sizeof(mach_msg_header_t)); | |
253 | msgh->msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); | |
254 | msgh->msgh_size = sizeof(mach_msg_header_t); | |
255 | msgh->msgh_remote_port = port; | |
1c79356b A |
256 | } |
257 | ||
258 | void IODataQueue::sendDataAvailableNotification() | |
259 | { | |
fe8ab488 A |
260 | kern_return_t kr; |
261 | mach_msg_header_t * msgh; | |
1c79356b | 262 | |
3e170ce0 A |
263 | msgh = &((IODataQueueInternal *) notifyMsg)->msg; |
264 | if (msgh->msgh_remote_port) { | |
39236c6e | 265 | kr = mach_msg_send_from_kernel_with_options(msgh, msgh->msgh_size, MACH_SEND_TIMEOUT, MACH_MSG_TIMEOUT_NONE); |
1c79356b | 266 | switch(kr) { |
fe8ab488 | 267 | case MACH_SEND_TIMED_OUT: // Notification already sent |
1c79356b | 268 | case MACH_MSG_SUCCESS: |
39236c6e | 269 | case MACH_SEND_NO_BUFFER: |
1c79356b A |
270 | break; |
271 | default: | |
272 | IOLog("%s: dataAvailableNotification failed - msg_send returned: %d\n", /*getName()*/"IODataQueue", kr); | |
273 | break; | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | IOMemoryDescriptor *IODataQueue::getMemoryDescriptor() | |
279 | { | |
280 | IOMemoryDescriptor *descriptor = 0; | |
3e170ce0 | 281 | UInt32 queueSize; |
1c79356b | 282 | |
3e170ce0 | 283 | queueSize = ((IODataQueueInternal *) notifyMsg)->queueSize; |
1c79356b | 284 | if (dataQueue != 0) { |
3e170ce0 | 285 | descriptor = IOMemoryDescriptor::withAddress(dataQueue, queueSize + DATA_QUEUE_MEMORY_HEADER_SIZE, kIODirectionOutIn); |
1c79356b A |
286 | } |
287 | ||
288 | return descriptor; | |
289 | } | |
290 | ||
fe8ab488 | 291 |