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