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