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