1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions
29*2c2f96dcSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*2c2f96dcSApple OSS Distributions
31*2c2f96dcSApple OSS Distributions #include <IOKit/IOSharedDataQueue.h>
32*2c2f96dcSApple OSS Distributions #include <IOKit/IODataQueueShared.h>
33*2c2f96dcSApple OSS Distributions #include <IOKit/IOLib.h>
34*2c2f96dcSApple OSS Distributions #include <IOKit/IOMemoryDescriptor.h>
35*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
36*2c2f96dcSApple OSS Distributions
37*2c2f96dcSApple OSS Distributions #ifdef enqueue
38*2c2f96dcSApple OSS Distributions #undef enqueue
39*2c2f96dcSApple OSS Distributions #endif
40*2c2f96dcSApple OSS Distributions
41*2c2f96dcSApple OSS Distributions #ifdef dequeue
42*2c2f96dcSApple OSS Distributions #undef dequeue
43*2c2f96dcSApple OSS Distributions #endif
44*2c2f96dcSApple OSS Distributions
45*2c2f96dcSApple OSS Distributions #define super IODataQueue
46*2c2f96dcSApple OSS Distributions
OSDefineMetaClassAndStructors(IOSharedDataQueue,IODataQueue)47*2c2f96dcSApple OSS Distributions OSDefineMetaClassAndStructors(IOSharedDataQueue, IODataQueue)
48*2c2f96dcSApple OSS Distributions
49*2c2f96dcSApple OSS Distributions OSSharedPtr<IOSharedDataQueue>
50*2c2f96dcSApple OSS Distributions IOSharedDataQueue::withCapacity(UInt32 size)
51*2c2f96dcSApple OSS Distributions {
52*2c2f96dcSApple OSS Distributions OSSharedPtr<IOSharedDataQueue> dataQueue = OSMakeShared<IOSharedDataQueue>();
53*2c2f96dcSApple OSS Distributions
54*2c2f96dcSApple OSS Distributions if (dataQueue) {
55*2c2f96dcSApple OSS Distributions if (!dataQueue->initWithCapacity(size)) {
56*2c2f96dcSApple OSS Distributions return nullptr;
57*2c2f96dcSApple OSS Distributions }
58*2c2f96dcSApple OSS Distributions }
59*2c2f96dcSApple OSS Distributions
60*2c2f96dcSApple OSS Distributions return dataQueue;
61*2c2f96dcSApple OSS Distributions }
62*2c2f96dcSApple OSS Distributions
63*2c2f96dcSApple OSS Distributions OSSharedPtr<IOSharedDataQueue>
withEntries(UInt32 numEntries,UInt32 entrySize)64*2c2f96dcSApple OSS Distributions IOSharedDataQueue::withEntries(UInt32 numEntries, UInt32 entrySize)
65*2c2f96dcSApple OSS Distributions {
66*2c2f96dcSApple OSS Distributions OSSharedPtr<IOSharedDataQueue> dataQueue = OSMakeShared<IOSharedDataQueue>();
67*2c2f96dcSApple OSS Distributions
68*2c2f96dcSApple OSS Distributions if (dataQueue) {
69*2c2f96dcSApple OSS Distributions if (!dataQueue->initWithEntries(numEntries, entrySize)) {
70*2c2f96dcSApple OSS Distributions return nullptr;
71*2c2f96dcSApple OSS Distributions }
72*2c2f96dcSApple OSS Distributions }
73*2c2f96dcSApple OSS Distributions
74*2c2f96dcSApple OSS Distributions return dataQueue;
75*2c2f96dcSApple OSS Distributions }
76*2c2f96dcSApple OSS Distributions
77*2c2f96dcSApple OSS Distributions Boolean
initWithCapacity(UInt32 size)78*2c2f96dcSApple OSS Distributions IOSharedDataQueue::initWithCapacity(UInt32 size)
79*2c2f96dcSApple OSS Distributions {
80*2c2f96dcSApple OSS Distributions IODataQueueAppendix * appendix;
81*2c2f96dcSApple OSS Distributions vm_size_t allocSize;
82*2c2f96dcSApple OSS Distributions kern_return_t kr;
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions if (!super::init()) {
85*2c2f96dcSApple OSS Distributions return false;
86*2c2f96dcSApple OSS Distributions }
87*2c2f96dcSApple OSS Distributions
88*2c2f96dcSApple OSS Distributions _reserved = IOMallocType(ExpansionData);
89*2c2f96dcSApple OSS Distributions if (!_reserved) {
90*2c2f96dcSApple OSS Distributions return false;
91*2c2f96dcSApple OSS Distributions }
92*2c2f96dcSApple OSS Distributions
93*2c2f96dcSApple OSS Distributions if (size > UINT32_MAX - DATA_QUEUE_MEMORY_HEADER_SIZE - DATA_QUEUE_MEMORY_APPENDIX_SIZE) {
94*2c2f96dcSApple OSS Distributions return false;
95*2c2f96dcSApple OSS Distributions }
96*2c2f96dcSApple OSS Distributions
97*2c2f96dcSApple OSS Distributions allocSize = round_page(size + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE);
98*2c2f96dcSApple OSS Distributions
99*2c2f96dcSApple OSS Distributions if (allocSize < size) {
100*2c2f96dcSApple OSS Distributions return false;
101*2c2f96dcSApple OSS Distributions }
102*2c2f96dcSApple OSS Distributions
103*2c2f96dcSApple OSS Distributions kr = kmem_alloc(kernel_map, (vm_offset_t *)&dataQueue, allocSize,
104*2c2f96dcSApple OSS Distributions (kma_flags_t)(KMA_DATA | KMA_ZERO), IOMemoryTag(kernel_map));
105*2c2f96dcSApple OSS Distributions if (kr != KERN_SUCCESS) {
106*2c2f96dcSApple OSS Distributions return false;
107*2c2f96dcSApple OSS Distributions }
108*2c2f96dcSApple OSS Distributions
109*2c2f96dcSApple OSS Distributions dataQueue->queueSize = size;
110*2c2f96dcSApple OSS Distributions // dataQueue->head = 0;
111*2c2f96dcSApple OSS Distributions // dataQueue->tail = 0;
112*2c2f96dcSApple OSS Distributions
113*2c2f96dcSApple OSS Distributions if (!setQueueSize(size)) {
114*2c2f96dcSApple OSS Distributions return false;
115*2c2f96dcSApple OSS Distributions }
116*2c2f96dcSApple OSS Distributions
117*2c2f96dcSApple OSS Distributions appendix = (IODataQueueAppendix *)((UInt8 *)dataQueue + size + DATA_QUEUE_MEMORY_HEADER_SIZE);
118*2c2f96dcSApple OSS Distributions appendix->version = 0;
119*2c2f96dcSApple OSS Distributions
120*2c2f96dcSApple OSS Distributions if (!notifyMsg) {
121*2c2f96dcSApple OSS Distributions notifyMsg = IOMallocType(mach_msg_header_t);
122*2c2f96dcSApple OSS Distributions if (!notifyMsg) {
123*2c2f96dcSApple OSS Distributions return false;
124*2c2f96dcSApple OSS Distributions }
125*2c2f96dcSApple OSS Distributions }
126*2c2f96dcSApple OSS Distributions bzero(notifyMsg, sizeof(mach_msg_header_t));
127*2c2f96dcSApple OSS Distributions
128*2c2f96dcSApple OSS Distributions setNotificationPort(MACH_PORT_NULL);
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions return true;
131*2c2f96dcSApple OSS Distributions }
132*2c2f96dcSApple OSS Distributions
133*2c2f96dcSApple OSS Distributions void
free()134*2c2f96dcSApple OSS Distributions IOSharedDataQueue::free()
135*2c2f96dcSApple OSS Distributions {
136*2c2f96dcSApple OSS Distributions if (dataQueue) {
137*2c2f96dcSApple OSS Distributions kmem_free(kernel_map, (vm_offset_t)dataQueue, round_page(getQueueSize() +
138*2c2f96dcSApple OSS Distributions DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE));
139*2c2f96dcSApple OSS Distributions dataQueue = NULL;
140*2c2f96dcSApple OSS Distributions if (notifyMsg) {
141*2c2f96dcSApple OSS Distributions IOFreeType(notifyMsg, mach_msg_header_t);
142*2c2f96dcSApple OSS Distributions notifyMsg = NULL;
143*2c2f96dcSApple OSS Distributions }
144*2c2f96dcSApple OSS Distributions }
145*2c2f96dcSApple OSS Distributions
146*2c2f96dcSApple OSS Distributions if (_reserved) {
147*2c2f96dcSApple OSS Distributions IOFreeType(_reserved, ExpansionData);
148*2c2f96dcSApple OSS Distributions _reserved = NULL;
149*2c2f96dcSApple OSS Distributions }
150*2c2f96dcSApple OSS Distributions
151*2c2f96dcSApple OSS Distributions super::free();
152*2c2f96dcSApple OSS Distributions }
153*2c2f96dcSApple OSS Distributions
154*2c2f96dcSApple OSS Distributions OSSharedPtr<IOMemoryDescriptor>
getMemoryDescriptor()155*2c2f96dcSApple OSS Distributions IOSharedDataQueue::getMemoryDescriptor()
156*2c2f96dcSApple OSS Distributions {
157*2c2f96dcSApple OSS Distributions OSSharedPtr<IOMemoryDescriptor> descriptor;
158*2c2f96dcSApple OSS Distributions
159*2c2f96dcSApple OSS Distributions if (dataQueue != NULL) {
160*2c2f96dcSApple OSS Distributions descriptor = IOMemoryDescriptor::withAddress(dataQueue, getQueueSize() + DATA_QUEUE_MEMORY_HEADER_SIZE + DATA_QUEUE_MEMORY_APPENDIX_SIZE, kIODirectionOutIn);
161*2c2f96dcSApple OSS Distributions }
162*2c2f96dcSApple OSS Distributions
163*2c2f96dcSApple OSS Distributions return descriptor;
164*2c2f96dcSApple OSS Distributions }
165*2c2f96dcSApple OSS Distributions
166*2c2f96dcSApple OSS Distributions
167*2c2f96dcSApple OSS Distributions IODataQueueEntry *
peek()168*2c2f96dcSApple OSS Distributions IOSharedDataQueue::peek()
169*2c2f96dcSApple OSS Distributions {
170*2c2f96dcSApple OSS Distributions IODataQueueEntry *entry = NULL;
171*2c2f96dcSApple OSS Distributions UInt32 headOffset;
172*2c2f96dcSApple OSS Distributions UInt32 tailOffset;
173*2c2f96dcSApple OSS Distributions
174*2c2f96dcSApple OSS Distributions if (!dataQueue) {
175*2c2f96dcSApple OSS Distributions return NULL;
176*2c2f96dcSApple OSS Distributions }
177*2c2f96dcSApple OSS Distributions
178*2c2f96dcSApple OSS Distributions // Read head and tail with acquire barrier
179*2c2f96dcSApple OSS Distributions // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
180*2c2f96dcSApple OSS Distributions headOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
181*2c2f96dcSApple OSS Distributions tailOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_ACQUIRE);
182*2c2f96dcSApple OSS Distributions
183*2c2f96dcSApple OSS Distributions if (headOffset != tailOffset) {
184*2c2f96dcSApple OSS Distributions volatile IODataQueueEntry * head = NULL;
185*2c2f96dcSApple OSS Distributions UInt32 headSize = 0;
186*2c2f96dcSApple OSS Distributions UInt32 headOffset = dataQueue->head;
187*2c2f96dcSApple OSS Distributions UInt32 queueSize = getQueueSize();
188*2c2f96dcSApple OSS Distributions
189*2c2f96dcSApple OSS Distributions if (headOffset > queueSize) {
190*2c2f96dcSApple OSS Distributions return NULL;
191*2c2f96dcSApple OSS Distributions }
192*2c2f96dcSApple OSS Distributions
193*2c2f96dcSApple OSS Distributions head = (IODataQueueEntry *)((char *)dataQueue->queue + headOffset);
194*2c2f96dcSApple OSS Distributions headSize = head->size;
195*2c2f96dcSApple OSS Distributions
196*2c2f96dcSApple OSS Distributions // Check if there's enough room before the end of the queue for a header.
197*2c2f96dcSApple OSS Distributions // If there is room, check if there's enough room to hold the header and
198*2c2f96dcSApple OSS Distributions // the data.
199*2c2f96dcSApple OSS Distributions
200*2c2f96dcSApple OSS Distributions if ((headOffset > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
201*2c2f96dcSApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize) ||
202*2c2f96dcSApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headSize) ||
203*2c2f96dcSApple OSS Distributions (headOffset + headSize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
204*2c2f96dcSApple OSS Distributions // No room for the header or the data, wrap to the beginning of the queue.
205*2c2f96dcSApple OSS Distributions // Note: wrapping even with the UINT32_MAX checks, as we have to support
206*2c2f96dcSApple OSS Distributions // queueSize of UINT32_MAX
207*2c2f96dcSApple OSS Distributions entry = dataQueue->queue;
208*2c2f96dcSApple OSS Distributions } else {
209*2c2f96dcSApple OSS Distributions entry = (IODataQueueEntry *)head;
210*2c2f96dcSApple OSS Distributions }
211*2c2f96dcSApple OSS Distributions }
212*2c2f96dcSApple OSS Distributions
213*2c2f96dcSApple OSS Distributions return entry;
214*2c2f96dcSApple OSS Distributions }
215*2c2f96dcSApple OSS Distributions
216*2c2f96dcSApple OSS Distributions Boolean
enqueue(void * data,UInt32 dataSize)217*2c2f96dcSApple OSS Distributions IOSharedDataQueue::enqueue(void * data, UInt32 dataSize)
218*2c2f96dcSApple OSS Distributions {
219*2c2f96dcSApple OSS Distributions UInt32 head;
220*2c2f96dcSApple OSS Distributions UInt32 tail;
221*2c2f96dcSApple OSS Distributions UInt32 newTail;
222*2c2f96dcSApple OSS Distributions const UInt32 entrySize = dataSize + DATA_QUEUE_ENTRY_HEADER_SIZE;
223*2c2f96dcSApple OSS Distributions IODataQueueEntry * entry;
224*2c2f96dcSApple OSS Distributions
225*2c2f96dcSApple OSS Distributions // Force a single read of head and tail
226*2c2f96dcSApple OSS Distributions // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
227*2c2f96dcSApple OSS Distributions tail = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_RELAXED);
228*2c2f96dcSApple OSS Distributions head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_ACQUIRE);
229*2c2f96dcSApple OSS Distributions
230*2c2f96dcSApple OSS Distributions // Check for overflow of entrySize
231*2c2f96dcSApple OSS Distributions if (dataSize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) {
232*2c2f96dcSApple OSS Distributions return false;
233*2c2f96dcSApple OSS Distributions }
234*2c2f96dcSApple OSS Distributions // Check for underflow of (getQueueSize() - tail)
235*2c2f96dcSApple OSS Distributions if (getQueueSize() < tail || getQueueSize() < head) {
236*2c2f96dcSApple OSS Distributions return false;
237*2c2f96dcSApple OSS Distributions }
238*2c2f96dcSApple OSS Distributions
239*2c2f96dcSApple OSS Distributions if (tail >= head) {
240*2c2f96dcSApple OSS Distributions // Is there enough room at the end for the entry?
241*2c2f96dcSApple OSS Distributions if ((entrySize <= UINT32_MAX - tail) &&
242*2c2f96dcSApple OSS Distributions ((tail + entrySize) <= getQueueSize())) {
243*2c2f96dcSApple OSS Distributions entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
244*2c2f96dcSApple OSS Distributions
245*2c2f96dcSApple OSS Distributions entry->size = dataSize;
246*2c2f96dcSApple OSS Distributions __nochk_memcpy(&entry->data, data, dataSize);
247*2c2f96dcSApple OSS Distributions
248*2c2f96dcSApple OSS Distributions // The tail can be out of bound when the size of the new entry
249*2c2f96dcSApple OSS Distributions // exactly matches the available space at the end of the queue.
250*2c2f96dcSApple OSS Distributions // The tail can range from 0 to dataQueue->queueSize inclusive.
251*2c2f96dcSApple OSS Distributions
252*2c2f96dcSApple OSS Distributions newTail = tail + entrySize;
253*2c2f96dcSApple OSS Distributions } else if (head > entrySize) { // Is there enough room at the beginning?
254*2c2f96dcSApple OSS Distributions // Wrap around to the beginning, but do not allow the tail to catch
255*2c2f96dcSApple OSS Distributions // up to the head.
256*2c2f96dcSApple OSS Distributions
257*2c2f96dcSApple OSS Distributions dataQueue->queue->size = dataSize;
258*2c2f96dcSApple OSS Distributions
259*2c2f96dcSApple OSS Distributions // We need to make sure that there is enough room to set the size before
260*2c2f96dcSApple OSS Distributions // doing this. The user client checks for this and will look for the size
261*2c2f96dcSApple OSS Distributions // at the beginning if there isn't room for it at the end.
262*2c2f96dcSApple OSS Distributions
263*2c2f96dcSApple OSS Distributions if ((getQueueSize() - tail) >= DATA_QUEUE_ENTRY_HEADER_SIZE) {
264*2c2f96dcSApple OSS Distributions ((IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail))->size = dataSize;
265*2c2f96dcSApple OSS Distributions }
266*2c2f96dcSApple OSS Distributions
267*2c2f96dcSApple OSS Distributions __nochk_memcpy(&dataQueue->queue->data, data, dataSize);
268*2c2f96dcSApple OSS Distributions newTail = entrySize;
269*2c2f96dcSApple OSS Distributions } else {
270*2c2f96dcSApple OSS Distributions return false; // queue is full
271*2c2f96dcSApple OSS Distributions }
272*2c2f96dcSApple OSS Distributions } else {
273*2c2f96dcSApple OSS Distributions // Do not allow the tail to catch up to the head when the queue is full.
274*2c2f96dcSApple OSS Distributions // That's why the comparison uses a '>' rather than '>='.
275*2c2f96dcSApple OSS Distributions
276*2c2f96dcSApple OSS Distributions if ((head - tail) > entrySize) {
277*2c2f96dcSApple OSS Distributions entry = (IODataQueueEntry *)((UInt8 *)dataQueue->queue + tail);
278*2c2f96dcSApple OSS Distributions
279*2c2f96dcSApple OSS Distributions entry->size = dataSize;
280*2c2f96dcSApple OSS Distributions __nochk_memcpy(&entry->data, data, dataSize);
281*2c2f96dcSApple OSS Distributions newTail = tail + entrySize;
282*2c2f96dcSApple OSS Distributions } else {
283*2c2f96dcSApple OSS Distributions return false; // queue is full
284*2c2f96dcSApple OSS Distributions }
285*2c2f96dcSApple OSS Distributions }
286*2c2f96dcSApple OSS Distributions
287*2c2f96dcSApple OSS Distributions // Publish the data we just enqueued
288*2c2f96dcSApple OSS Distributions __c11_atomic_store((_Atomic UInt32 *)&dataQueue->tail, newTail, __ATOMIC_RELEASE);
289*2c2f96dcSApple OSS Distributions
290*2c2f96dcSApple OSS Distributions if (tail != head) {
291*2c2f96dcSApple OSS Distributions //
292*2c2f96dcSApple OSS Distributions // The memory barrier below paris with the one in ::dequeue
293*2c2f96dcSApple OSS Distributions // so that either our store to the tail cannot be missed by
294*2c2f96dcSApple OSS Distributions // the next dequeue attempt, or we will observe the dequeuer
295*2c2f96dcSApple OSS Distributions // making the queue empty.
296*2c2f96dcSApple OSS Distributions //
297*2c2f96dcSApple OSS Distributions // Of course, if we already think the queue is empty,
298*2c2f96dcSApple OSS Distributions // there's no point paying this extra cost.
299*2c2f96dcSApple OSS Distributions //
300*2c2f96dcSApple OSS Distributions __c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
301*2c2f96dcSApple OSS Distributions head = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
302*2c2f96dcSApple OSS Distributions }
303*2c2f96dcSApple OSS Distributions
304*2c2f96dcSApple OSS Distributions if (tail == head) {
305*2c2f96dcSApple OSS Distributions // Send notification (via mach message) that data is now available.
306*2c2f96dcSApple OSS Distributions sendDataAvailableNotification();
307*2c2f96dcSApple OSS Distributions }
308*2c2f96dcSApple OSS Distributions return true;
309*2c2f96dcSApple OSS Distributions }
310*2c2f96dcSApple OSS Distributions
311*2c2f96dcSApple OSS Distributions Boolean
dequeue(void * data,UInt32 * dataSize)312*2c2f96dcSApple OSS Distributions IOSharedDataQueue::dequeue(void *data, UInt32 *dataSize)
313*2c2f96dcSApple OSS Distributions {
314*2c2f96dcSApple OSS Distributions Boolean retVal = TRUE;
315*2c2f96dcSApple OSS Distributions volatile IODataQueueEntry * entry = NULL;
316*2c2f96dcSApple OSS Distributions UInt32 entrySize = 0;
317*2c2f96dcSApple OSS Distributions UInt32 headOffset = 0;
318*2c2f96dcSApple OSS Distributions UInt32 tailOffset = 0;
319*2c2f96dcSApple OSS Distributions UInt32 newHeadOffset = 0;
320*2c2f96dcSApple OSS Distributions
321*2c2f96dcSApple OSS Distributions if (!dataQueue || (data && !dataSize)) {
322*2c2f96dcSApple OSS Distributions return false;
323*2c2f96dcSApple OSS Distributions }
324*2c2f96dcSApple OSS Distributions
325*2c2f96dcSApple OSS Distributions // Read head and tail with acquire barrier
326*2c2f96dcSApple OSS Distributions // See rdar://problem/40780584 for an explanation of relaxed/acquire barriers
327*2c2f96dcSApple OSS Distributions headOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->head, __ATOMIC_RELAXED);
328*2c2f96dcSApple OSS Distributions tailOffset = __c11_atomic_load((_Atomic UInt32 *)&dataQueue->tail, __ATOMIC_ACQUIRE);
329*2c2f96dcSApple OSS Distributions
330*2c2f96dcSApple OSS Distributions if (headOffset != tailOffset) {
331*2c2f96dcSApple OSS Distributions volatile IODataQueueEntry * head = NULL;
332*2c2f96dcSApple OSS Distributions UInt32 headSize = 0;
333*2c2f96dcSApple OSS Distributions UInt32 queueSize = getQueueSize();
334*2c2f96dcSApple OSS Distributions
335*2c2f96dcSApple OSS Distributions if (headOffset > queueSize) {
336*2c2f96dcSApple OSS Distributions return false;
337*2c2f96dcSApple OSS Distributions }
338*2c2f96dcSApple OSS Distributions
339*2c2f96dcSApple OSS Distributions head = (IODataQueueEntry *)((char *)dataQueue->queue + headOffset);
340*2c2f96dcSApple OSS Distributions headSize = head->size;
341*2c2f96dcSApple OSS Distributions
342*2c2f96dcSApple OSS Distributions // we wrapped around to beginning, so read from there
343*2c2f96dcSApple OSS Distributions // either there was not even room for the header
344*2c2f96dcSApple OSS Distributions if ((headOffset > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
345*2c2f96dcSApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize) ||
346*2c2f96dcSApple OSS Distributions // or there was room for the header, but not for the data
347*2c2f96dcSApple OSS Distributions (headOffset + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headSize) ||
348*2c2f96dcSApple OSS Distributions (headOffset + headSize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
349*2c2f96dcSApple OSS Distributions // Note: we have to wrap to the beginning even with the UINT32_MAX checks
350*2c2f96dcSApple OSS Distributions // because we have to support a queueSize of UINT32_MAX.
351*2c2f96dcSApple OSS Distributions entry = dataQueue->queue;
352*2c2f96dcSApple OSS Distributions entrySize = entry->size;
353*2c2f96dcSApple OSS Distributions if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
354*2c2f96dcSApple OSS Distributions (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > queueSize)) {
355*2c2f96dcSApple OSS Distributions return false;
356*2c2f96dcSApple OSS Distributions }
357*2c2f96dcSApple OSS Distributions newHeadOffset = entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE;
358*2c2f96dcSApple OSS Distributions // else it is at the end
359*2c2f96dcSApple OSS Distributions } else {
360*2c2f96dcSApple OSS Distributions entry = head;
361*2c2f96dcSApple OSS Distributions entrySize = entry->size;
362*2c2f96dcSApple OSS Distributions if ((entrySize > UINT32_MAX - DATA_QUEUE_ENTRY_HEADER_SIZE) ||
363*2c2f96dcSApple OSS Distributions (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE > UINT32_MAX - headOffset) ||
364*2c2f96dcSApple OSS Distributions (entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE + headOffset > queueSize)) {
365*2c2f96dcSApple OSS Distributions return false;
366*2c2f96dcSApple OSS Distributions }
367*2c2f96dcSApple OSS Distributions newHeadOffset = headOffset + entrySize + DATA_QUEUE_ENTRY_HEADER_SIZE;
368*2c2f96dcSApple OSS Distributions }
369*2c2f96dcSApple OSS Distributions } else {
370*2c2f96dcSApple OSS Distributions // empty queue
371*2c2f96dcSApple OSS Distributions return false;
372*2c2f96dcSApple OSS Distributions }
373*2c2f96dcSApple OSS Distributions
374*2c2f96dcSApple OSS Distributions if (data) {
375*2c2f96dcSApple OSS Distributions if (entrySize > *dataSize) {
376*2c2f96dcSApple OSS Distributions // not enough space
377*2c2f96dcSApple OSS Distributions return false;
378*2c2f96dcSApple OSS Distributions }
379*2c2f96dcSApple OSS Distributions __nochk_memcpy(data, (void *)entry->data, entrySize);
380*2c2f96dcSApple OSS Distributions *dataSize = entrySize;
381*2c2f96dcSApple OSS Distributions }
382*2c2f96dcSApple OSS Distributions
383*2c2f96dcSApple OSS Distributions __c11_atomic_store((_Atomic UInt32 *)&dataQueue->head, newHeadOffset, __ATOMIC_RELEASE);
384*2c2f96dcSApple OSS Distributions
385*2c2f96dcSApple OSS Distributions if (newHeadOffset == tailOffset) {
386*2c2f96dcSApple OSS Distributions //
387*2c2f96dcSApple OSS Distributions // If we are making the queue empty, then we need to make sure
388*2c2f96dcSApple OSS Distributions // that either the enqueuer notices, or we notice the enqueue
389*2c2f96dcSApple OSS Distributions // that raced with our making of the queue empty.
390*2c2f96dcSApple OSS Distributions //
391*2c2f96dcSApple OSS Distributions __c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
392*2c2f96dcSApple OSS Distributions }
393*2c2f96dcSApple OSS Distributions
394*2c2f96dcSApple OSS Distributions return retVal;
395*2c2f96dcSApple OSS Distributions }
396*2c2f96dcSApple OSS Distributions
397*2c2f96dcSApple OSS Distributions UInt32
getQueueSize()398*2c2f96dcSApple OSS Distributions IOSharedDataQueue::getQueueSize()
399*2c2f96dcSApple OSS Distributions {
400*2c2f96dcSApple OSS Distributions if (!_reserved) {
401*2c2f96dcSApple OSS Distributions return 0;
402*2c2f96dcSApple OSS Distributions }
403*2c2f96dcSApple OSS Distributions return _reserved->queueSize;
404*2c2f96dcSApple OSS Distributions }
405*2c2f96dcSApple OSS Distributions
406*2c2f96dcSApple OSS Distributions Boolean
setQueueSize(UInt32 size)407*2c2f96dcSApple OSS Distributions IOSharedDataQueue::setQueueSize(UInt32 size)
408*2c2f96dcSApple OSS Distributions {
409*2c2f96dcSApple OSS Distributions if (!_reserved) {
410*2c2f96dcSApple OSS Distributions return false;
411*2c2f96dcSApple OSS Distributions }
412*2c2f96dcSApple OSS Distributions _reserved->queueSize = size;
413*2c2f96dcSApple OSS Distributions return true;
414*2c2f96dcSApple OSS Distributions }
415*2c2f96dcSApple OSS Distributions
416*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 0);
417*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 1);
418*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 2);
419*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 3);
420*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 4);
421*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 5);
422*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 6);
423*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(IOSharedDataQueue, 7);
424