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