]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOCommandGate.cpp
xnu-1699.22.81.tar.gz
[apple/xnu.git] / iokit / Kernel / IOCommandGate.cpp
CommitLineData
1c79356b 1/*
6d2010ae 2 * Copyright (c) 1998-2000, 2009-2010 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
0c530ab8
A
28#include <libkern/OSDebug.h>
29
1c79356b
A
30#include <IOKit/IOCommandGate.h>
31#include <IOKit/IOWorkLoop.h>
32#include <IOKit/IOReturn.h>
33#include <IOKit/IOTimeStamp.h>
060df5ea 34#include <IOKit/IOKitDebug.h>
1c79356b
A
35
36#define super IOEventSource
37
38OSDefineMetaClassAndStructors(IOCommandGate, IOEventSource)
b0d623f7 39#if __LP64__
1c79356b 40OSMetaClassDefineReservedUnused(IOCommandGate, 0);
b0d623f7
A
41#else
42OSMetaClassDefineReservedUsed(IOCommandGate, 0);
43#endif
1c79356b
A
44OSMetaClassDefineReservedUnused(IOCommandGate, 1);
45OSMetaClassDefineReservedUnused(IOCommandGate, 2);
46OSMetaClassDefineReservedUnused(IOCommandGate, 3);
47OSMetaClassDefineReservedUnused(IOCommandGate, 4);
48OSMetaClassDefineReservedUnused(IOCommandGate, 5);
49OSMetaClassDefineReservedUnused(IOCommandGate, 6);
50OSMetaClassDefineReservedUnused(IOCommandGate, 7);
51
6d2010ae
A
52#if IOKITSTATS
53
54#define IOStatisticsInitializeCounter() \
55do { \
56 IOStatistics::setCounterType(IOEventSource::reserved->counter, kIOStatisticsCommandGateCounter); \
57} while (0)
58
59#define IOStatisticsActionCall() \
60do { \
61 IOStatistics::countCommandGateActionCall(IOEventSource::reserved->counter); \
62} while (0)
63
64#else
65
66#define IOStatisticsInitializeCounter()
67#define IOStatisticsActionCall()
68
69#endif /* IOKITSTATS */
1c79356b 70
55e303ae 71bool IOCommandGate::init(OSObject *inOwner, Action inAction)
1c79356b 72{
6d2010ae
A
73 bool res = super::init(inOwner, (IOEventSource::Action) inAction);
74 if (res) {
75 IOStatisticsInitializeCounter();
76 }
77
78 return res;
1c79356b
A
79}
80
81IOCommandGate *
55e303ae 82IOCommandGate::commandGate(OSObject *inOwner, Action inAction)
1c79356b
A
83{
84 IOCommandGate *me = new IOCommandGate;
85
86 if (me && !me->init(inOwner, inAction)) {
55e303ae 87 me->release();
1c79356b
A
88 return 0;
89 }
90
91 return me;
92}
93
0c530ab8 94/* virtual */ void IOCommandGate::disable()
89b3af67 95{
0c530ab8
A
96 if (workLoop && !workLoop->inGate())
97 OSReportWithBacktrace("IOCommandGate::disable() called when not gated");
5d5c5d0d 98
0c530ab8
A
99 super::disable();
100}
5d5c5d0d 101
0c530ab8
A
102/* virtual */ void IOCommandGate::enable()
103{
104 if (workLoop) {
105 closeGate();
106 super::enable();
107 wakeupGate(&enabled, /* oneThread */ false); // Unblock sleeping threads
108 openGate();
109 }
110}
89b3af67 111
0c530ab8
A
112/* virtual */ void IOCommandGate::free()
113{
114 setWorkLoop(0);
115 super::free();
116}
4452a7af 117
0c530ab8
A
118/* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop)
119{
120 uintptr_t *sleepersP = (uintptr_t *) &reserved;
121 if (!inWorkLoop && workLoop) { // tearing down
122 closeGate();
123 *sleepersP |= 1;
124 while (*sleepersP >> 1) {
125 thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED);
126 sleepGate(sleepersP, THREAD_UNINT);
127 }
128 *sleepersP = 0;
129 openGate();
130 }
131 else
4452a7af 132
0c530ab8
A
133 super::setWorkLoop(inWorkLoop);
134}
135
136IOReturn IOCommandGate::runCommand(void *arg0, void *arg1,
137 void *arg2, void *arg3)
138{
139 return runAction((Action) action, arg0, arg1, arg2, arg3);
140}
141
142IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1,
143 void *arg2, void *arg3)
144{
145 return attemptAction((Action) action, arg0, arg1, arg2, arg3);
1c79356b
A
146}
147
148IOReturn IOCommandGate::runAction(Action inAction,
55e303ae
A
149 void *arg0, void *arg1,
150 void *arg2, void *arg3)
1c79356b 151{
1c79356b
A
152 if (!inAction)
153 return kIOReturnBadArgument;
154
0c530ab8 155 // closeGate is recursive needn't worry if we already hold the lock.
1c79356b 156 closeGate();
1c79356b 157
0c530ab8
A
158 // If the command gate is disabled and we aren't on the workloop thread
159 // itself then sleep until we get enabled.
1c79356b 160 IOReturn res;
0c530ab8
A
161 if (!workLoop->onThread()) {
162 while (!enabled) {
163 uintptr_t *sleepersP = (uintptr_t *) &reserved;
1c79356b 164
0c530ab8
A
165 *sleepersP += 2;
166 IOReturn res = sleepGate(&enabled, THREAD_ABORTSAFE);
167 *sleepersP -= 2;
1c79356b 168
0c530ab8
A
169 bool wakeupTearDown = (*sleepersP & 1);
170 if (res || wakeupTearDown) {
171 openGate();
1c79356b 172
0c530ab8
A
173 if (wakeupTearDown)
174 commandWakeup(sleepersP); // No further resources used
1c79356b 175
0c530ab8
A
176 return kIOReturnAborted;
177 }
178 }
179 }
1c79356b 180
060df5ea
A
181 bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false;
182
183 if (trace)
184 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
185 (uintptr_t) inAction, (uintptr_t) owner);
186
6d2010ae
A
187 IOStatisticsActionCall();
188
0c530ab8
A
189 // Must be gated and on the work loop or enabled
190 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
060df5ea
A
191
192 if (trace)
193 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
194 (uintptr_t) inAction, (uintptr_t) owner);
195
1c79356b 196 openGate();
6d2010ae 197
1c79356b
A
198 return res;
199}
200
201IOReturn IOCommandGate::attemptAction(Action inAction,
55e303ae
A
202 void *arg0, void *arg1,
203 void *arg2, void *arg3)
1c79356b
A
204{
205 IOReturn res;
206
1c79356b
A
207 if (!inAction)
208 return kIOReturnBadArgument;
209
210 // Try to close the gate if can't get return immediately.
211 if (!tryCloseGate())
212 return kIOReturnCannotLock;
213
0c530ab8
A
214 // If the command gate is disabled then sleep until we get a wakeup
215 if (!workLoop->onThread() && !enabled)
216 res = kIOReturnNotPermitted;
217 else {
060df5ea
A
218
219 bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false;
220
221 if (trace)
222 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
6d2010ae
A
223 (uintptr_t) inAction, (uintptr_t) owner);
224
225 IOStatisticsActionCall();
226
227 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
060df5ea
A
228
229 if (trace)
230 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
231 (uintptr_t) inAction, (uintptr_t) owner);
0c530ab8 232 }
1c79356b 233
1c79356b
A
234 openGate();
235
236 return res;
237}
238
239IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible)
240{
1c79356b
A
241 if (!workLoop->inGate())
242 return kIOReturnNotPermitted;
243
244 return sleepGate(event, interruptible);
245}
246
b0d623f7
A
247IOReturn IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible)
248{
249 if (!workLoop->inGate())
250 return kIOReturnNotPermitted;
251
252 return sleepGate(event, deadline, interruptible);
253}
254
1c79356b
A
255void IOCommandGate::commandWakeup(void *event, bool oneThread)
256{
257 wakeupGate(event, oneThread);
258}