]> git.saurik.com Git - apple/xnu.git/blame - iokit/Kernel/IOCommandGate.cpp
xnu-3247.1.106.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{
3e170ce0 114 if (workLoop) setWorkLoop(0);
0c530ab8
A
115 super::free();
116}
4452a7af 117
3e170ce0
A
118enum
119{
120 kSleepersRemoved = 0x00000001,
121 kSleepersWaitEnabled = 0x00000002,
122 kSleepersActions = 0x00000100,
123 kSleepersActionsMask = 0xffffff00,
124};
125
0c530ab8
A
126/* virtual */ void IOCommandGate::setWorkLoop(IOWorkLoop *inWorkLoop)
127{
3e170ce0
A
128 IOWorkLoop * wl;
129 uintptr_t * sleepersP = (uintptr_t *) &reserved;
130 bool defer;
131
132 if (!inWorkLoop && (wl = workLoop)) { // tearing down
133 wl->closeGate();
134 *sleepersP |= kSleepersRemoved;
135 while (*sleepersP & kSleepersWaitEnabled) {
0c530ab8
A
136 thread_wakeup_with_result(&enabled, THREAD_INTERRUPTED);
137 sleepGate(sleepersP, THREAD_UNINT);
138 }
3e170ce0
A
139 *sleepersP &= ~kSleepersWaitEnabled;
140 defer = (0 != (kSleepersActionsMask & *sleepersP));
141 if (!defer)
142 {
143 super::setWorkLoop(0);
144 *sleepersP &= ~kSleepersRemoved;
145 }
146 wl->openGate();
147 return;
0c530ab8 148 }
4452a7af 149
0c530ab8
A
150 super::setWorkLoop(inWorkLoop);
151}
152
153IOReturn IOCommandGate::runCommand(void *arg0, void *arg1,
154 void *arg2, void *arg3)
155{
156 return runAction((Action) action, arg0, arg1, arg2, arg3);
157}
158
159IOReturn IOCommandGate::attemptCommand(void *arg0, void *arg1,
160 void *arg2, void *arg3)
161{
162 return attemptAction((Action) action, arg0, arg1, arg2, arg3);
1c79356b
A
163}
164
165IOReturn IOCommandGate::runAction(Action inAction,
55e303ae
A
166 void *arg0, void *arg1,
167 void *arg2, void *arg3)
1c79356b 168{
3e170ce0
A
169 IOWorkLoop * wl;
170 uintptr_t * sleepersP;
171
1c79356b
A
172 if (!inAction)
173 return kIOReturnBadArgument;
3e170ce0
A
174 if (!(wl = workLoop))
175 return kIOReturnNotReady;
1c79356b 176
0c530ab8 177 // closeGate is recursive needn't worry if we already hold the lock.
3e170ce0
A
178 wl->closeGate();
179 sleepersP = (uintptr_t *) &reserved;
1c79356b 180
0c530ab8
A
181 // If the command gate is disabled and we aren't on the workloop thread
182 // itself then sleep until we get enabled.
1c79356b 183 IOReturn res;
3e170ce0
A
184 if (!wl->onThread())
185 {
186 while (!enabled)
187 {
188 IOReturn sleepResult = kIOReturnSuccess;
189 if (workLoop)
190 {
191 *sleepersP |= kSleepersWaitEnabled;
192 sleepResult = wl->sleepGate(&enabled, THREAD_ABORTSAFE);
193 *sleepersP &= ~kSleepersWaitEnabled;
194 }
195 bool wakeupTearDown = (!workLoop || (0 != (*sleepersP & kSleepersRemoved)));
196 if ((kIOReturnSuccess != sleepResult) || wakeupTearDown) {
197 wl->openGate();
1c79356b 198
0c530ab8 199 if (wakeupTearDown)
3e170ce0 200 wl->wakeupGate(sleepersP, false); // No further resources used
1c79356b 201
0c530ab8
A
202 return kIOReturnAborted;
203 }
204 }
205 }
1c79356b 206
060df5ea
A
207 bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false;
208
3e170ce0 209 if (trace) IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
316670eb 210 VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
060df5ea 211
6d2010ae
A
212 IOStatisticsActionCall();
213
0c530ab8 214 // Must be gated and on the work loop or enabled
3e170ce0
A
215
216 *sleepersP += kSleepersActions;
0c530ab8 217 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
3e170ce0
A
218 *sleepersP -= kSleepersActions;
219
220 if (trace) IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
316670eb 221 VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
3e170ce0
A
222
223 if (kSleepersRemoved == ((kSleepersActionsMask|kSleepersRemoved) & *sleepersP))
224 {
225 // no actions outstanding
226 *sleepersP &= ~kSleepersRemoved;
227 super::setWorkLoop(0);
228 }
060df5ea 229
3e170ce0 230 wl->openGate();
6d2010ae 231
1c79356b
A
232 return res;
233}
234
235IOReturn IOCommandGate::attemptAction(Action inAction,
55e303ae
A
236 void *arg0, void *arg1,
237 void *arg2, void *arg3)
1c79356b
A
238{
239 IOReturn res;
3e170ce0 240 IOWorkLoop * wl;
1c79356b 241
1c79356b
A
242 if (!inAction)
243 return kIOReturnBadArgument;
3e170ce0
A
244 if (!(wl = workLoop))
245 return kIOReturnNotReady;
1c79356b
A
246
247 // Try to close the gate if can't get return immediately.
3e170ce0 248 if (!wl->tryCloseGate())
1c79356b
A
249 return kIOReturnCannotLock;
250
0c530ab8 251 // If the command gate is disabled then sleep until we get a wakeup
3e170ce0 252 if (!wl->onThread() && !enabled)
0c530ab8
A
253 res = kIOReturnNotPermitted;
254 else {
060df5ea
A
255
256 bool trace = ( gIOKitTrace & kIOTraceCommandGates ) ? true : false;
257
258 if (trace)
259 IOTimeStampStartConstant(IODBG_CMDQ(IOCMDQ_ACTION),
316670eb 260 VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
6d2010ae
A
261
262 IOStatisticsActionCall();
263
264 res = (*inAction)(owner, arg0, arg1, arg2, arg3);
060df5ea
A
265
266 if (trace)
267 IOTimeStampEndConstant(IODBG_CMDQ(IOCMDQ_ACTION),
316670eb 268 VM_KERNEL_UNSLIDE(inAction), (uintptr_t) owner);
0c530ab8 269 }
1c79356b 270
3e170ce0 271 wl->openGate();
1c79356b
A
272
273 return res;
274}
275
276IOReturn IOCommandGate::commandSleep(void *event, UInt32 interruptible)
277{
1c79356b
A
278 if (!workLoop->inGate())
279 return kIOReturnNotPermitted;
280
281 return sleepGate(event, interruptible);
282}
283
b0d623f7
A
284IOReturn IOCommandGate::commandSleep(void *event, AbsoluteTime deadline, UInt32 interruptible)
285{
286 if (!workLoop->inGate())
287 return kIOReturnNotPermitted;
288
289 return sleepGate(event, deadline, interruptible);
290}
291
1c79356b
A
292void IOCommandGate::commandWakeup(void *event, bool oneThread)
293{
294 wakeupGate(event, oneThread);
295}